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"
49 #include "vr-values.h"
52 /* Set value range VR to a non-negative range of type TYPE. */
55 set_value_range_to_nonnegative (value_range
*vr
, tree type
)
57 tree zero
= build_int_cst (type
, 0);
58 vr
->update (VR_RANGE
, zero
, vrp_val_max (type
));
61 /* Set value range VR to a range of a truthvalue of type TYPE. */
64 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
66 if (TYPE_PRECISION (type
) == 1)
67 vr
->set_varying (type
);
69 vr
->update (VR_RANGE
, build_int_cst (type
, 0), build_int_cst (type
, 1));
72 /* Return the lattice entry for VAR or NULL if it doesn't exist or cannot
76 vr_values::get_lattice_entry (const_tree var
)
80 unsigned ver
= SSA_NAME_VERSION (var
);
82 /* If we query the entry for a new SSA name avoid reallocating the lattice
83 since we should get here at most from the substitute-and-fold stage which
84 will never try to change values. */
85 if (ver
>= num_vr_values
)
92 /* Create a default value range. */
93 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
95 /* After propagation finished return varying. */
96 if (values_propagated
)
98 vr
->set_varying (TREE_TYPE (var
));
102 vr
->set_undefined ();
104 /* If VAR is a default definition of a parameter, the variable can
105 take any value in VAR's type. */
106 if (SSA_NAME_IS_DEFAULT_DEF (var
))
108 sym
= SSA_NAME_VAR (var
);
109 if (TREE_CODE (sym
) == PARM_DECL
)
111 /* Try to use the "nonnull" attribute to create ~[0, 0]
112 anti-ranges for pointers. Note that this is only valid with
113 default definitions of PARM_DECLs. */
114 if (POINTER_TYPE_P (TREE_TYPE (sym
))
115 && (nonnull_arg_p (sym
)
116 || get_ptr_nonnull (var
)))
118 vr
->set_nonzero (TREE_TYPE (sym
));
121 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
123 get_range_info (var
, *vr
);
124 if (vr
->undefined_p ())
125 vr
->set_varying (TREE_TYPE (sym
));
128 vr
->set_varying (TREE_TYPE (sym
));
130 else if (TREE_CODE (sym
) == RESULT_DECL
131 && DECL_BY_REFERENCE (sym
))
133 vr
->set_nonzero (TREE_TYPE (sym
));
141 /* Return value range information for VAR.
143 If we have no values ranges recorded (ie, VRP is not running), then
144 return NULL. Otherwise create an empty range if none existed for VAR. */
147 vr_values::get_value_range (const_tree var
)
149 /* If we have no recorded ranges, then return NULL. */
153 value_range
*vr
= get_lattice_entry (var
);
155 /* Reallocate the lattice if needed. */
158 unsigned int old_sz
= num_vr_values
;
159 num_vr_values
= num_ssa_names
+ num_ssa_names
/ 10;
160 vr_value
= XRESIZEVEC (value_range
*, vr_value
, num_vr_values
);
161 for ( ; old_sz
< num_vr_values
; old_sz
++)
162 vr_value
[old_sz
] = NULL
;
164 /* Now that the lattice has been resized, we should never fail. */
165 vr
= get_lattice_entry (var
);
172 /* Set the lattice entry for DEF to VARYING. */
175 vr_values::set_def_to_varying (const_tree def
)
177 value_range
*vr
= get_lattice_entry (def
);
179 vr
->set_varying (TREE_TYPE (def
));
182 /* Set value-ranges of all SSA names defined by STMT to varying. */
185 vr_values::set_defs_to_varying (gimple
*stmt
)
189 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
190 set_def_to_varying (def
);
193 /* Update the value range and equivalence set for variable VAR to
194 NEW_VR. Return true if NEW_VR is different from VAR's previous
197 NOTE: This function assumes that NEW_VR is a temporary value range
198 object created for the sole purpose of updating VAR's range. The
199 storage used by the equivalence set from NEW_VR will be freed by
200 this function. Do not call update_value_range when NEW_VR
201 is the range object associated with another SSA name. */
204 vr_values::update_value_range (const_tree var
, value_range
*new_vr
)
209 /* If there is a value-range on the SSA name from earlier analysis
211 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
214 value_range_kind rtype
= get_range_info (var
, nr
);
215 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
216 new_vr
->intersect (&nr
);
219 /* Update the value range, if necessary. If we cannot allocate a lattice
220 entry for VAR keep it at VARYING. This happens when DOM feeds us stmts
221 with SSA names allocated after setting up the lattice. */
222 old_vr
= get_lattice_entry (var
);
225 is_new
= !old_vr
->equal_p (*new_vr
, /*ignore_equivs=*/false);
229 /* Do not allow transitions up the lattice. The following
230 is slightly more awkward than just new_vr->type < old_vr->type
231 because VR_RANGE and VR_ANTI_RANGE need to be considered
232 the same. We may not have is_new when transitioning to
233 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
234 called, if we are anyway, keep it VARYING. */
235 if (old_vr
->varying_p ())
237 new_vr
->set_varying (new_vr
->type ());
240 else if (new_vr
->undefined_p ())
242 old_vr
->set_varying (TREE_TYPE (var
));
243 new_vr
->set_varying (TREE_TYPE (var
));
247 old_vr
->set (new_vr
->kind (),
248 new_vr
->min (), new_vr
->max (), new_vr
->equiv ());
251 new_vr
->equiv_clear ();
256 /* Return true if value range VR involves exactly one symbol SYM. */
259 symbolic_range_based_on_p (value_range_base
*vr
, const_tree sym
)
261 bool neg
, min_has_symbol
, max_has_symbol
;
264 if (is_gimple_min_invariant (vr
->min ()))
265 min_has_symbol
= false;
266 else if (get_single_symbol (vr
->min (), &neg
, &inv
) == sym
)
267 min_has_symbol
= true;
271 if (is_gimple_min_invariant (vr
->max ()))
272 max_has_symbol
= false;
273 else if (get_single_symbol (vr
->max (), &neg
, &inv
) == sym
)
274 max_has_symbol
= true;
278 return (min_has_symbol
|| max_has_symbol
);
281 /* Return true if the result of assignment STMT is know to be non-zero. */
284 gimple_assign_nonzero_p (gimple
*stmt
)
286 enum tree_code code
= gimple_assign_rhs_code (stmt
);
287 bool strict_overflow_p
;
288 switch (get_gimple_rhs_class (code
))
290 case GIMPLE_UNARY_RHS
:
291 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
292 gimple_expr_type (stmt
),
293 gimple_assign_rhs1 (stmt
),
295 case GIMPLE_BINARY_RHS
:
296 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
297 gimple_expr_type (stmt
),
298 gimple_assign_rhs1 (stmt
),
299 gimple_assign_rhs2 (stmt
),
301 case GIMPLE_TERNARY_RHS
:
303 case GIMPLE_SINGLE_RHS
:
304 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
306 case GIMPLE_INVALID_RHS
:
313 /* Return true if STMT is known to compute a non-zero value. */
316 gimple_stmt_nonzero_p (gimple
*stmt
)
318 switch (gimple_code (stmt
))
321 return gimple_assign_nonzero_p (stmt
);
324 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
325 return (gimple_call_nonnull_result_p (call_stmt
)
326 || gimple_call_nonnull_arg (call_stmt
));
332 /* Like tree_expr_nonzero_p, but this function uses value ranges
336 vr_values::vrp_stmt_computes_nonzero (gimple
*stmt
)
338 if (gimple_stmt_nonzero_p (stmt
))
341 /* If we have an expression of the form &X->a, then the expression
342 is nonnull if X is nonnull. */
343 if (is_gimple_assign (stmt
)
344 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
346 tree expr
= gimple_assign_rhs1 (stmt
);
347 poly_int64 bitsize
, bitpos
;
350 int unsignedp
, reversep
, volatilep
;
351 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
352 &bitpos
, &offset
, &mode
, &unsignedp
,
353 &reversep
, &volatilep
);
355 if (base
!= NULL_TREE
356 && TREE_CODE (base
) == MEM_REF
357 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
359 poly_offset_int off
= 0;
360 bool off_cst
= false;
361 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
363 off
= mem_ref_offset (base
);
365 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
367 off
<<= LOG2_BITS_PER_UNIT
;
371 /* If &X->a is equal to X and X is ~[0, 0], the result is too.
372 For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
373 allow going from non-NULL pointer to NULL. */
374 if ((off_cst
&& known_eq (off
, 0))
375 || (flag_delete_null_pointer_checks
376 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))))
378 const value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
379 if (!range_includes_zero_p (vr
))
382 /* If MEM_REF has a "positive" offset, consider it non-NULL
383 always, for -fdelete-null-pointer-checks also "negative"
384 ones. Punt for unknown offsets (e.g. variable ones). */
385 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
388 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
396 /* Returns true if EXPR is a valid value (as expected by compare_values) --
397 a gimple invariant, or SSA_NAME +- CST. */
400 valid_value_p (tree expr
)
402 if (TREE_CODE (expr
) == SSA_NAME
)
405 if (TREE_CODE (expr
) == PLUS_EXPR
406 || TREE_CODE (expr
) == MINUS_EXPR
)
407 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
408 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
410 return is_gimple_min_invariant (expr
);
413 /* If OP has a value range with a single constant value return that,
414 otherwise return NULL_TREE. This returns OP itself if OP is a
418 vr_values::op_with_constant_singleton_value_range (tree op
)
420 if (is_gimple_min_invariant (op
))
423 if (TREE_CODE (op
) != SSA_NAME
)
427 if (get_value_range (op
)->singleton_p (&t
))
432 /* Return true if op is in a boolean [0, 1] value-range. */
435 vr_values::op_with_boolean_value_range_p (tree op
)
437 const value_range
*vr
;
439 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
442 if (integer_zerop (op
)
443 || integer_onep (op
))
446 if (TREE_CODE (op
) != SSA_NAME
)
449 vr
= get_value_range (op
);
450 return (vr
->kind () == VR_RANGE
451 && integer_zerop (vr
->min ())
452 && integer_onep (vr
->max ()));
455 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
456 true and store it in *VR_P. */
459 vr_values::extract_range_for_var_from_comparison_expr (tree var
,
460 enum tree_code cond_code
,
465 const value_range
*limit_vr
;
466 type
= TREE_TYPE (var
);
468 /* For pointer arithmetic, we only keep track of pointer equality
469 and inequality. If we arrive here with unfolded conditions like
470 _1 > _1 do not derive anything. */
471 if ((POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
474 vr_p
->set_varying (type
);
478 /* If LIMIT is another SSA name and LIMIT has a range of its own,
479 try to use LIMIT's range to avoid creating symbolic ranges
481 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
483 /* LIMIT's range is only interesting if it has any useful information. */
485 || limit_vr
->undefined_p ()
486 || limit_vr
->varying_p ()
487 || (limit_vr
->symbolic_p ()
488 && ! (limit_vr
->kind () == VR_RANGE
489 && (limit_vr
->min () == limit_vr
->max ()
490 || operand_equal_p (limit_vr
->min (),
491 limit_vr
->max (), 0)))))
494 /* Initially, the new range has the same set of equivalences of
495 VAR's range. This will be revised before returning the final
496 value. Since assertions may be chained via mutually exclusive
497 predicates, we will need to trim the set of equivalences before
499 gcc_assert (vr_p
->equiv () == NULL
);
500 vr_p
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
502 /* Extract a new range based on the asserted comparison for VAR and
503 LIMIT's value range. Notice that if LIMIT has an anti-range, we
504 will only use it for equality comparisons (EQ_EXPR). For any
505 other kind of assertion, we cannot derive a range from LIMIT's
506 anti-range that can be used to describe the new range. For
507 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
508 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
509 no single range for x_2 that could describe LE_EXPR, so we might
510 as well build the range [b_4, +INF] for it.
511 One special case we handle is extracting a range from a
512 range test encoded as (unsigned)var + CST <= limit. */
513 if (TREE_CODE (op
) == NOP_EXPR
514 || TREE_CODE (op
) == PLUS_EXPR
)
516 if (TREE_CODE (op
) == PLUS_EXPR
)
518 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
519 TREE_OPERAND (op
, 1));
520 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
521 op
= TREE_OPERAND (op
, 0);
525 min
= build_int_cst (TREE_TYPE (var
), 0);
529 /* Make sure to not set TREE_OVERFLOW on the final type
530 conversion. We are willingly interpreting large positive
531 unsigned values as negative signed values here. */
532 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
533 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
535 /* We can transform a max, min range to an anti-range or
536 vice-versa. Use set_and_canonicalize which does this for
538 if (cond_code
== LE_EXPR
)
539 vr_p
->set (VR_RANGE
, min
, max
, vr_p
->equiv ());
540 else if (cond_code
== GT_EXPR
)
541 vr_p
->set (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
545 else if (cond_code
== EQ_EXPR
)
547 enum value_range_kind range_type
;
551 range_type
= limit_vr
->kind ();
552 min
= limit_vr
->min ();
553 max
= limit_vr
->max ();
557 range_type
= VR_RANGE
;
562 vr_p
->update (range_type
, min
, max
);
564 /* When asserting the equality VAR == LIMIT and LIMIT is another
565 SSA name, the new range will also inherit the equivalence set
567 if (TREE_CODE (limit
) == SSA_NAME
)
568 vr_p
->equiv_add (limit
, get_value_range (limit
), &vrp_equiv_obstack
);
570 else if (cond_code
== NE_EXPR
)
572 /* As described above, when LIMIT's range is an anti-range and
573 this assertion is an inequality (NE_EXPR), then we cannot
574 derive anything from the anti-range. For instance, if
575 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
576 not imply that VAR's range is [0, 0]. So, in the case of
577 anti-ranges, we just assert the inequality using LIMIT and
580 If LIMIT_VR is a range, we can only use it to build a new
581 anti-range if LIMIT_VR is a single-valued range. For
582 instance, if LIMIT_VR is [0, 1], the predicate
583 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
584 Rather, it means that for value 0 VAR should be ~[0, 0]
585 and for value 1, VAR should be ~[1, 1]. We cannot
586 represent these ranges.
588 The only situation in which we can build a valid
589 anti-range is when LIMIT_VR is a single-valued range
590 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
591 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
593 && limit_vr
->kind () == VR_RANGE
594 && compare_values (limit_vr
->min (), limit_vr
->max ()) == 0)
596 min
= limit_vr
->min ();
597 max
= limit_vr
->max ();
601 /* In any other case, we cannot use LIMIT's range to build a
606 /* If MIN and MAX cover the whole range for their type, then
607 just use the original LIMIT. */
608 if (INTEGRAL_TYPE_P (type
)
609 && vrp_val_is_min (min
)
610 && vrp_val_is_max (max
))
613 vr_p
->set (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
615 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
617 min
= TYPE_MIN_VALUE (type
);
619 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
623 /* If LIMIT_VR is of the form [N1, N2], we need to build the
624 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
626 max
= limit_vr
->max ();
629 /* If the maximum value forces us to be out of bounds, simply punt.
630 It would be pointless to try and do anything more since this
631 all should be optimized away above us. */
632 if (cond_code
== LT_EXPR
633 && compare_values (max
, min
) == 0)
634 vr_p
->set_varying (TREE_TYPE (min
));
637 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
638 if (cond_code
== LT_EXPR
)
640 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
641 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
642 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
643 build_int_cst (TREE_TYPE (max
), -1));
645 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
646 build_int_cst (TREE_TYPE (max
), 1));
647 /* Signal to compare_values_warnv this expr doesn't overflow. */
649 TREE_NO_WARNING (max
) = 1;
652 vr_p
->update (VR_RANGE
, min
, max
);
655 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
657 max
= TYPE_MAX_VALUE (type
);
659 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
663 /* If LIMIT_VR is of the form [N1, N2], we need to build the
664 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
666 min
= limit_vr
->min ();
669 /* If the minimum value forces us to be out of bounds, simply punt.
670 It would be pointless to try and do anything more since this
671 all should be optimized away above us. */
672 if (cond_code
== GT_EXPR
673 && compare_values (min
, max
) == 0)
674 vr_p
->set_varying (TREE_TYPE (min
));
677 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
678 if (cond_code
== GT_EXPR
)
680 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
681 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
682 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
683 build_int_cst (TREE_TYPE (min
), -1));
685 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
686 build_int_cst (TREE_TYPE (min
), 1));
687 /* Signal to compare_values_warnv this expr doesn't overflow. */
689 TREE_NO_WARNING (min
) = 1;
692 vr_p
->update (VR_RANGE
, min
, max
);
698 /* Finally intersect the new range with what we already know about var. */
699 vr_p
->intersect (get_value_range (var
));
702 /* Extract value range information from an ASSERT_EXPR EXPR and store
706 vr_values::extract_range_from_assert (value_range
*vr_p
, tree expr
)
708 tree var
= ASSERT_EXPR_VAR (expr
);
709 tree cond
= ASSERT_EXPR_COND (expr
);
711 enum tree_code cond_code
;
712 gcc_assert (COMPARISON_CLASS_P (cond
));
714 /* Find VAR in the ASSERT_EXPR conditional. */
715 if (var
== TREE_OPERAND (cond
, 0)
716 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
717 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
719 /* If the predicate is of the form VAR COMP LIMIT, then we just
720 take LIMIT from the RHS and use the same comparison code. */
721 cond_code
= TREE_CODE (cond
);
722 limit
= TREE_OPERAND (cond
, 1);
723 op
= TREE_OPERAND (cond
, 0);
727 /* If the predicate is of the form LIMIT COMP VAR, then we need
728 to flip around the comparison code to create the proper range
730 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
731 limit
= TREE_OPERAND (cond
, 0);
732 op
= TREE_OPERAND (cond
, 1);
734 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
738 /* Extract range information from SSA name VAR and store it in VR. If
739 VAR has an interesting range, use it. Otherwise, create the
740 range [VAR, VAR] and return it. This is useful in situations where
741 we may have conditionals testing values of VARYING names. For
748 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
752 vr_values::extract_range_from_ssa_name (value_range
*vr
, tree var
)
754 const value_range
*var_vr
= get_value_range (var
);
756 if (!var_vr
->varying_p ())
757 vr
->deep_copy (var_vr
);
761 if (!vr
->undefined_p ())
762 vr
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
765 /* Extract range information from a binary expression OP0 CODE OP1 based on
766 the ranges of each of its operands with resulting type EXPR_TYPE.
767 The resulting range is stored in *VR. */
770 vr_values::extract_range_from_binary_expr (value_range
*vr
,
772 tree expr_type
, tree op0
, tree op1
)
774 /* Get value ranges for each operand. For constant operands, create
775 a new value range with the operand to simplify processing. */
776 value_range_base vr0
, vr1
;
777 if (TREE_CODE (op0
) == SSA_NAME
)
778 vr0
= *(get_value_range (op0
));
779 else if (is_gimple_min_invariant (op0
))
782 vr0
.set_varying (TREE_TYPE (op0
));
784 if (TREE_CODE (op1
) == SSA_NAME
)
785 vr1
= *(get_value_range (op1
));
786 else if (is_gimple_min_invariant (op1
))
789 vr1
.set_varying (TREE_TYPE (op1
));
791 /* If one argument is varying, we can sometimes still deduce a
792 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
793 if (INTEGRAL_TYPE_P (TREE_TYPE (op0
))
794 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
796 if (vr0
.varying_p () && !vr1
.varying_p ())
797 vr0
= value_range (VR_RANGE
,
798 vrp_val_min (expr_type
),
799 vrp_val_max (expr_type
));
800 else if (vr1
.varying_p () && !vr0
.varying_p ())
801 vr1
= value_range (VR_RANGE
,
802 vrp_val_min (expr_type
),
803 vrp_val_max (expr_type
));
806 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &vr0
, &vr1
);
808 /* Set value_range for n in following sequence:
809 def = __builtin_memchr (arg, 0, sz)
811 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
814 && code
== POINTER_DIFF_EXPR
815 && TREE_CODE (op0
) == SSA_NAME
816 && TREE_CODE (op1
) == SSA_NAME
)
818 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
819 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
820 gcall
*call_stmt
= NULL
;
822 if (TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
823 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
824 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
825 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
826 && (call_stmt
= dyn_cast
<gcall
*>(SSA_NAME_DEF_STMT (op0
)))
827 && gimple_call_builtin_p (call_stmt
, BUILT_IN_MEMCHR
)
828 && operand_equal_p (op0
, gimple_call_lhs (call_stmt
), 0)
829 && operand_equal_p (op1
, gimple_call_arg (call_stmt
, 0), 0)
830 && integer_zerop (gimple_call_arg (call_stmt
, 1)))
832 tree max
= vrp_val_max (ptrdiff_type_node
);
833 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
834 tree range_min
= build_zero_cst (expr_type
);
835 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
836 vr
->set (VR_RANGE
, range_min
, range_max
);
841 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
842 and based on the other operand, for example if it was deduced from a
843 symbolic comparison. When a bound of the range of the first operand
844 is invariant, we set the corresponding bound of the new range to INF
845 in order to avoid recursing on the range of the second operand. */
847 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
848 && TREE_CODE (op1
) == SSA_NAME
849 && vr0
.kind () == VR_RANGE
850 && symbolic_range_based_on_p (&vr0
, op1
))
852 const bool minus_p
= (code
== MINUS_EXPR
);
855 /* Try with VR0 and [-INF, OP1]. */
856 if (is_gimple_min_invariant (minus_p
? vr0
.max () : vr0
.min ()))
857 n_vr1
.set (VR_RANGE
, vrp_val_min (expr_type
), op1
);
859 /* Try with VR0 and [OP1, +INF]. */
860 else if (is_gimple_min_invariant (minus_p
? vr0
.min () : vr0
.max ()))
861 n_vr1
.set (VR_RANGE
, op1
, vrp_val_max (expr_type
));
863 /* Try with VR0 and [OP1, OP1]. */
865 n_vr1
.set (VR_RANGE
, op1
, op1
);
867 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &vr0
, &n_vr1
);
871 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
872 && TREE_CODE (op0
) == SSA_NAME
873 && vr1
.kind () == VR_RANGE
874 && symbolic_range_based_on_p (&vr1
, op0
))
876 const bool minus_p
= (code
== MINUS_EXPR
);
879 /* Try with [-INF, OP0] and VR1. */
880 if (is_gimple_min_invariant (minus_p
? vr1
.max () : vr1
.min ()))
881 n_vr0
.set (VR_RANGE
, vrp_val_min (expr_type
), op0
);
883 /* Try with [OP0, +INF] and VR1. */
884 else if (is_gimple_min_invariant (minus_p
? vr1
.min (): vr1
.max ()))
885 n_vr0
.set (VR_RANGE
, op0
, vrp_val_max (expr_type
));
887 /* Try with [OP0, OP0] and VR1. */
891 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &n_vr0
, &vr1
);
894 /* If we didn't derive a range for MINUS_EXPR, and
895 op1's range is ~[op0,op0] or vice-versa, then we
896 can derive a non-null range. This happens often for
897 pointer subtraction. */
899 && (code
== MINUS_EXPR
|| code
== POINTER_DIFF_EXPR
)
900 && TREE_CODE (op0
) == SSA_NAME
901 && ((vr0
.kind () == VR_ANTI_RANGE
903 && vr0
.min () == vr0
.max ())
904 || (vr1
.kind () == VR_ANTI_RANGE
906 && vr1
.min () == vr1
.max ())))
908 vr
->set_nonzero (expr_type
);
913 /* Extract range information from a unary expression CODE OP0 based on
914 the range of its operand with resulting type TYPE.
915 The resulting range is stored in *VR. */
918 vr_values::extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
921 value_range_base vr0
;
923 /* Get value ranges for the operand. For constant operands, create
924 a new value range with the operand to simplify processing. */
925 if (TREE_CODE (op0
) == SSA_NAME
)
926 vr0
= *(get_value_range (op0
));
927 else if (is_gimple_min_invariant (op0
))
930 vr0
.set_varying (type
);
932 ::extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
936 /* Extract range information from a conditional expression STMT based on
937 the ranges of each of its operands and the expression code. */
940 vr_values::extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
942 /* Get value ranges for each operand. For constant operands, create
943 a new value range with the operand to simplify processing. */
944 tree op0
= gimple_assign_rhs2 (stmt
);
946 const value_range
*vr0
= &tem0
;
947 if (TREE_CODE (op0
) == SSA_NAME
)
948 vr0
= get_value_range (op0
);
949 else if (is_gimple_min_invariant (op0
))
952 tem0
.set_varying (TREE_TYPE (op0
));
954 tree op1
= gimple_assign_rhs3 (stmt
);
956 const value_range
*vr1
= &tem1
;
957 if (TREE_CODE (op1
) == SSA_NAME
)
958 vr1
= get_value_range (op1
);
959 else if (is_gimple_min_invariant (op1
))
962 tem1
.set_varying (TREE_TYPE (op1
));
964 /* The resulting value range is the union of the operand ranges */
970 /* Extract range information from a comparison expression EXPR based
971 on the range of its operand and the expression code. */
974 vr_values::extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
975 tree type
, tree op0
, tree op1
)
980 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
984 /* Since this expression was found on the RHS of an assignment,
985 its type may be different from _Bool. Convert VAL to EXPR's
987 val
= fold_convert (type
, val
);
988 if (is_gimple_min_invariant (val
))
991 vr
->update (VR_RANGE
, val
, val
);
994 /* The result of a comparison is always true or false. */
995 set_value_range_to_truthvalue (vr
, type
);
998 /* Helper function for simplify_internal_call_using_ranges and
999 extract_range_basic. Return true if OP0 SUBCODE OP1 for
1000 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
1001 always overflow. Set *OVF to true if it is known to always
1005 vr_values::check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
1006 tree op0
, tree op1
, bool *ovf
)
1008 value_range_base vr0
, vr1
;
1009 if (TREE_CODE (op0
) == SSA_NAME
)
1010 vr0
= *get_value_range (op0
);
1011 else if (TREE_CODE (op0
) == INTEGER_CST
)
1014 vr0
.set_varying (TREE_TYPE (op0
));
1016 if (TREE_CODE (op1
) == SSA_NAME
)
1017 vr1
= *get_value_range (op1
);
1018 else if (TREE_CODE (op1
) == INTEGER_CST
)
1021 vr1
.set_varying (TREE_TYPE (op1
));
1023 tree vr0min
= vr0
.min (), vr0max
= vr0
.max ();
1024 tree vr1min
= vr1
.min (), vr1max
= vr1
.max ();
1025 if (!range_int_cst_p (&vr0
)
1026 || TREE_OVERFLOW (vr0min
)
1027 || TREE_OVERFLOW (vr0max
))
1029 vr0min
= vrp_val_min (TREE_TYPE (op0
));
1030 vr0max
= vrp_val_max (TREE_TYPE (op0
));
1032 if (!range_int_cst_p (&vr1
)
1033 || TREE_OVERFLOW (vr1min
)
1034 || TREE_OVERFLOW (vr1max
))
1036 vr1min
= vrp_val_min (TREE_TYPE (op1
));
1037 vr1max
= vrp_val_max (TREE_TYPE (op1
));
1039 *ovf
= arith_overflowed_p (subcode
, type
, vr0min
,
1040 subcode
== MINUS_EXPR
? vr1max
: vr1min
);
1041 if (arith_overflowed_p (subcode
, type
, vr0max
,
1042 subcode
== MINUS_EXPR
? vr1min
: vr1max
) != *ovf
)
1044 if (subcode
== MULT_EXPR
)
1046 if (arith_overflowed_p (subcode
, type
, vr0min
, vr1max
) != *ovf
1047 || arith_overflowed_p (subcode
, type
, vr0max
, vr1min
) != *ovf
)
1052 /* So far we found that there is an overflow on the boundaries.
1053 That doesn't prove that there is an overflow even for all values
1054 in between the boundaries. For that compute widest_int range
1055 of the result and see if it doesn't overlap the range of
1057 widest_int wmin
, wmax
;
1060 w
[0] = wi::to_widest (vr0min
);
1061 w
[1] = wi::to_widest (vr0max
);
1062 w
[2] = wi::to_widest (vr1min
);
1063 w
[3] = wi::to_widest (vr1max
);
1064 for (i
= 0; i
< 4; i
++)
1070 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1073 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1076 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1088 wmin
= wi::smin (wmin
, wt
);
1089 wmax
= wi::smax (wmax
, wt
);
1092 /* The result of op0 CODE op1 is known to be in range
1094 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
1095 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
1096 /* If all values in [wmin, wmax] are smaller than
1097 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1098 the arithmetic operation will always overflow. */
1099 if (wmax
< wtmin
|| wmin
> wtmax
)
1106 /* Try to derive a nonnegative or nonzero range out of STMT relying
1107 primarily on generic routines in fold in conjunction with range data.
1108 Store the result in *VR */
1111 vr_values::extract_range_basic (value_range
*vr
, gimple
*stmt
)
1114 tree type
= gimple_expr_type (stmt
);
1116 if (is_gimple_call (stmt
))
1119 int mini
, maxi
, zerov
= 0, prec
;
1120 enum tree_code subcode
= ERROR_MARK
;
1121 combined_fn cfn
= gimple_call_combined_fn (stmt
);
1122 scalar_int_mode mode
;
1126 case CFN_BUILT_IN_CONSTANT_P
:
1127 /* If the call is __builtin_constant_p and the argument is a
1128 function parameter resolve it to false. This avoids bogus
1129 array bound warnings.
1130 ??? We could do this as early as inlining is finished. */
1131 arg
= gimple_call_arg (stmt
, 0);
1132 if (TREE_CODE (arg
) == SSA_NAME
1133 && SSA_NAME_IS_DEFAULT_DEF (arg
)
1134 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
1135 && cfun
->after_inlining
)
1137 vr
->set_zero (type
);
1142 /* Both __builtin_ffs* and __builtin_popcount return
1146 arg
= gimple_call_arg (stmt
, 0);
1147 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1150 if (TREE_CODE (arg
) == SSA_NAME
)
1152 const value_range
*vr0
= get_value_range (arg
);
1153 /* If arg is non-zero, then ffs or popcount are non-zero. */
1154 if (range_includes_zero_p (vr0
) == 0)
1156 /* If some high bits are known to be zero,
1157 we can decrease the maximum. */
1158 if (vr0
->kind () == VR_RANGE
1159 && TREE_CODE (vr0
->max ()) == INTEGER_CST
1160 && !operand_less_p (vr0
->min (),
1161 build_zero_cst (TREE_TYPE (vr0
->min ()))))
1162 maxi
= tree_floor_log2 (vr0
->max ()) + 1;
1165 /* __builtin_parity* returns [0, 1]. */
1170 /* __builtin_c[lt]z* return [0, prec-1], except for
1171 when the argument is 0, but that is undefined behavior.
1172 On many targets where the CLZ RTL or optab value is defined
1173 for 0 the value is prec, so include that in the range
1176 arg
= gimple_call_arg (stmt
, 0);
1177 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1180 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1181 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
1182 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
)
1183 /* Handle only the single common value. */
1185 /* Magic value to give up, unless vr0 proves
1188 if (TREE_CODE (arg
) == SSA_NAME
)
1190 const value_range
*vr0
= get_value_range (arg
);
1191 /* From clz of VR_RANGE minimum we can compute
1193 if (vr0
->kind () == VR_RANGE
1194 && TREE_CODE (vr0
->min ()) == INTEGER_CST
)
1196 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min ());
1200 else if (vr0
->kind () == VR_ANTI_RANGE
1201 && integer_zerop (vr0
->min ()))
1208 /* From clz of VR_RANGE maximum we can compute
1210 if (vr0
->kind () == VR_RANGE
1211 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1213 mini
= prec
- 1 - tree_floor_log2 (vr0
->max ());
1221 /* __builtin_ctz* return [0, prec-1], except for
1222 when the argument is 0, but that is undefined behavior.
1223 If there is a ctz optab for this mode and
1224 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1225 otherwise just assume 0 won't be seen. */
1227 arg
= gimple_call_arg (stmt
, 0);
1228 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1231 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1232 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
1233 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
))
1235 /* Handle only the two common values. */
1238 else if (zerov
== prec
)
1241 /* Magic value to give up, unless vr0 proves
1245 if (TREE_CODE (arg
) == SSA_NAME
)
1247 const value_range
*vr0
= get_value_range (arg
);
1248 /* If arg is non-zero, then use [0, prec - 1]. */
1249 if ((vr0
->kind () == VR_RANGE
1250 && integer_nonzerop (vr0
->min ()))
1251 || (vr0
->kind () == VR_ANTI_RANGE
1252 && integer_zerop (vr0
->min ())))
1257 /* If some high bits are known to be zero,
1258 we can decrease the result maximum. */
1259 if (vr0
->kind () == VR_RANGE
1260 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1262 maxi
= tree_floor_log2 (vr0
->max ());
1263 /* For vr0 [0, 0] give up. */
1271 /* __builtin_clrsb* returns [0, prec-1]. */
1273 arg
= gimple_call_arg (stmt
, 0);
1274 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1279 vr
->set (VR_RANGE
, build_int_cst (type
, mini
),
1280 build_int_cst (type
, maxi
));
1282 case CFN_UBSAN_CHECK_ADD
:
1283 subcode
= PLUS_EXPR
;
1285 case CFN_UBSAN_CHECK_SUB
:
1286 subcode
= MINUS_EXPR
;
1288 case CFN_UBSAN_CHECK_MUL
:
1289 subcode
= MULT_EXPR
;
1291 case CFN_GOACC_DIM_SIZE
:
1292 case CFN_GOACC_DIM_POS
:
1293 /* Optimizing these two internal functions helps the loop
1294 optimizer eliminate outer comparisons. Size is [1,N]
1295 and pos is [0,N-1]. */
1297 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
1298 int axis
= oacc_get_ifn_dim_arg (stmt
);
1299 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1302 /* If it's dynamic, the backend might know a hardware
1304 size
= targetm
.goacc
.dim_limit (axis
);
1306 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
1307 vr
->set(VR_RANGE
, build_int_cst (type
, is_pos
? 0 : 1),
1309 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1312 case CFN_BUILT_IN_STRLEN
:
1313 if (tree lhs
= gimple_call_lhs (stmt
))
1314 if (ptrdiff_type_node
1315 && (TYPE_PRECISION (ptrdiff_type_node
)
1316 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1318 tree type
= TREE_TYPE (lhs
);
1319 tree max
= vrp_val_max (ptrdiff_type_node
);
1320 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1321 tree range_min
= build_zero_cst (type
);
1322 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
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 ::extract_range_from_binary_expr (vr
, subcode
, type
,
1434 flag_wrapv
= saved_flag_wrapv
;
1441 if (INTEGRAL_TYPE_P (type
)
1442 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
1443 set_value_range_to_nonnegative (vr
, type
);
1444 else if (vrp_stmt_computes_nonzero (stmt
))
1446 vr
->set_nonzero (type
);
1450 vr
->set_varying (type
);
1454 /* Try to compute a useful range out of assignment STMT and store it
1458 vr_values::extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
1460 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1462 if (code
== ASSERT_EXPR
)
1463 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
1464 else if (code
== SSA_NAME
)
1465 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
1466 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
1467 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
1468 gimple_expr_type (stmt
),
1469 gimple_assign_rhs1 (stmt
),
1470 gimple_assign_rhs2 (stmt
));
1471 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
1472 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
1473 gimple_expr_type (stmt
),
1474 gimple_assign_rhs1 (stmt
));
1475 else if (code
== COND_EXPR
)
1476 extract_range_from_cond_expr (vr
, stmt
);
1477 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1478 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
1479 gimple_expr_type (stmt
),
1480 gimple_assign_rhs1 (stmt
),
1481 gimple_assign_rhs2 (stmt
));
1482 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
1483 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
1484 vr
->set (gimple_assign_rhs1 (stmt
));
1486 vr
->set_varying (TREE_TYPE (gimple_assign_lhs (stmt
)));
1488 if (vr
->varying_p ())
1489 extract_range_basic (vr
, stmt
);
1492 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1494 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1495 all the values in the ranges.
1497 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1499 - Return NULL_TREE if it is not always possible to determine the
1500 value of the comparison.
1502 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1503 assumed signed overflow is undefined. */
1507 compare_ranges (enum tree_code comp
, const value_range
*vr0
,
1508 const value_range
*vr1
, bool *strict_overflow_p
)
1510 /* VARYING or UNDEFINED ranges cannot be compared. */
1511 if (vr0
->varying_p ()
1512 || vr0
->undefined_p ()
1513 || vr1
->varying_p ()
1514 || vr1
->undefined_p ())
1517 /* Anti-ranges need to be handled separately. */
1518 if (vr0
->kind () == VR_ANTI_RANGE
|| vr1
->kind () == VR_ANTI_RANGE
)
1520 /* If both are anti-ranges, then we cannot compute any
1522 if (vr0
->kind () == VR_ANTI_RANGE
&& vr1
->kind () == VR_ANTI_RANGE
)
1525 /* These comparisons are never statically computable. */
1532 /* Equality can be computed only between a range and an
1533 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1534 if (vr0
->kind () == VR_RANGE
)
1535 /* To simplify processing, make VR0 the anti-range. */
1536 std::swap (vr0
, vr1
);
1538 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
1540 if (compare_values_warnv (vr0
->min (), vr1
->min (), strict_overflow_p
) == 0
1541 && compare_values_warnv (vr0
->max (), vr1
->max (), strict_overflow_p
) == 0)
1542 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1547 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1548 operands around and change the comparison code. */
1549 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1551 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
1552 std::swap (vr0
, vr1
);
1555 if (comp
== EQ_EXPR
)
1557 /* Equality may only be computed if both ranges represent
1558 exactly one value. */
1559 if (compare_values_warnv (vr0
->min (), vr0
->max (), strict_overflow_p
) == 0
1560 && compare_values_warnv (vr1
->min (), vr1
->max (), strict_overflow_p
) == 0)
1562 int cmp_min
= compare_values_warnv (vr0
->min (), vr1
->min (),
1564 int cmp_max
= compare_values_warnv (vr0
->max (), vr1
->max (),
1566 if (cmp_min
== 0 && cmp_max
== 0)
1567 return boolean_true_node
;
1568 else if (cmp_min
!= -2 && cmp_max
!= -2)
1569 return boolean_false_node
;
1571 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1572 else if (compare_values_warnv (vr0
->min (), vr1
->max (),
1573 strict_overflow_p
) == 1
1574 || compare_values_warnv (vr1
->min (), vr0
->max (),
1575 strict_overflow_p
) == 1)
1576 return boolean_false_node
;
1580 else if (comp
== NE_EXPR
)
1584 /* If VR0 is completely to the left or completely to the right
1585 of VR1, they are always different. Notice that we need to
1586 make sure that both comparisons yield similar results to
1587 avoid comparing values that cannot be compared at
1589 cmp1
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1590 cmp2
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1591 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
1592 return boolean_true_node
;
1594 /* If VR0 and VR1 represent a single value and are identical,
1596 else if (compare_values_warnv (vr0
->min (), vr0
->max (),
1597 strict_overflow_p
) == 0
1598 && compare_values_warnv (vr1
->min (), vr1
->max (),
1599 strict_overflow_p
) == 0
1600 && compare_values_warnv (vr0
->min (), vr1
->min (),
1601 strict_overflow_p
) == 0
1602 && compare_values_warnv (vr0
->max (), vr1
->max (),
1603 strict_overflow_p
) == 0)
1604 return boolean_false_node
;
1606 /* Otherwise, they may or may not be different. */
1610 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1614 /* If VR0 is to the left of VR1, return true. */
1615 tst
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1616 if ((comp
== LT_EXPR
&& tst
== -1)
1617 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1618 return boolean_true_node
;
1620 /* If VR0 is to the right of VR1, return false. */
1621 tst
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1622 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1623 || (comp
== LE_EXPR
&& tst
== 1))
1624 return boolean_false_node
;
1626 /* Otherwise, we don't know. */
1633 /* Given a value range VR, a value VAL and a comparison code COMP, return
1634 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1635 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1636 always returns false. Return NULL_TREE if it is not always
1637 possible to determine the value of the comparison. Also set
1638 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1639 assumed signed overflow is undefined. */
1642 compare_range_with_value (enum tree_code comp
, const value_range
*vr
, tree val
,
1643 bool *strict_overflow_p
)
1645 if (vr
->varying_p () || vr
->undefined_p ())
1648 /* Anti-ranges need to be handled separately. */
1649 if (vr
->kind () == VR_ANTI_RANGE
)
1651 /* For anti-ranges, the only predicates that we can compute at
1652 compile time are equality and inequality. */
1659 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1660 if (!vr
->may_contain_p (val
))
1661 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1666 if (comp
== EQ_EXPR
)
1668 /* EQ_EXPR may only be computed if VR represents exactly
1670 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0)
1672 int cmp
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1674 return boolean_true_node
;
1675 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
1676 return boolean_false_node
;
1678 else if (compare_values_warnv (val
, vr
->min (), strict_overflow_p
) == -1
1679 || compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1)
1680 return boolean_false_node
;
1684 else if (comp
== NE_EXPR
)
1686 /* If VAL is not inside VR, then they are always different. */
1687 if (compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1
1688 || compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 1)
1689 return boolean_true_node
;
1691 /* If VR represents exactly one value equal to VAL, then return
1693 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0
1694 && compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 0)
1695 return boolean_false_node
;
1697 /* Otherwise, they may or may not be different. */
1700 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1704 /* If VR is to the left of VAL, return true. */
1705 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1706 if ((comp
== LT_EXPR
&& tst
== -1)
1707 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1708 return boolean_true_node
;
1710 /* If VR is to the right of VAL, return false. */
1711 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1712 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1713 || (comp
== LE_EXPR
&& tst
== 1))
1714 return boolean_false_node
;
1716 /* Otherwise, we don't know. */
1719 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1723 /* If VR is to the right of VAL, return true. */
1724 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1725 if ((comp
== GT_EXPR
&& tst
== 1)
1726 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
1727 return boolean_true_node
;
1729 /* If VR is to the left of VAL, return false. */
1730 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1731 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
1732 || (comp
== GE_EXPR
&& tst
== -1))
1733 return boolean_false_node
;
1735 /* Otherwise, we don't know. */
1741 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1742 would be profitable to adjust VR using scalar evolution information
1743 for VAR. If so, update VR with the new limits. */
1746 vr_values::adjust_range_with_scev (value_range
*vr
, class loop
*loop
,
1747 gimple
*stmt
, tree var
)
1749 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
1750 enum ev_direction dir
;
1752 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1753 better opportunities than a regular range, but I'm not sure. */
1754 if (vr
->kind () == VR_ANTI_RANGE
)
1757 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
1759 /* Like in PR19590, scev can return a constant function. */
1760 if (is_gimple_min_invariant (chrec
))
1766 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1769 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
1770 tem
= op_with_constant_singleton_value_range (init
);
1773 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
1774 tem
= op_with_constant_singleton_value_range (step
);
1778 /* If STEP is symbolic, we can't know whether INIT will be the
1779 minimum or maximum value in the range. Also, unless INIT is
1780 a simple expression, compare_values and possibly other functions
1781 in tree-vrp won't be able to handle it. */
1782 if (step
== NULL_TREE
1783 || !is_gimple_min_invariant (step
)
1784 || !valid_value_p (init
))
1787 dir
= scev_direction (chrec
);
1788 if (/* Do not adjust ranges if we do not know whether the iv increases
1789 or decreases, ... */
1790 dir
== EV_DIR_UNKNOWN
1791 /* ... or if it may wrap. */
1792 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
1793 get_chrec_loop (chrec
), true))
1796 type
= TREE_TYPE (var
);
1797 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1798 tmin
= lower_bound_in_type (type
, type
);
1800 tmin
= TYPE_MIN_VALUE (type
);
1801 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1802 tmax
= upper_bound_in_type (type
, type
);
1804 tmax
= TYPE_MAX_VALUE (type
);
1806 /* Try to use estimated number of iterations for the loop to constrain the
1807 final value in the evolution. */
1808 if (TREE_CODE (step
) == INTEGER_CST
1809 && is_gimple_val (init
)
1810 && (TREE_CODE (init
) != SSA_NAME
1811 || get_value_range (init
)->kind () == VR_RANGE
))
1815 /* We are only entering here for loop header PHI nodes, so using
1816 the number of latch executions is the correct thing to use. */
1817 if (max_loop_iterations (loop
, &nit
))
1820 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
1821 wi::overflow_type overflow
;
1823 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
1825 /* If the multiplication overflowed we can't do a meaningful
1826 adjustment. Likewise if the result doesn't fit in the type
1827 of the induction variable. For a signed type we have to
1828 check whether the result has the expected signedness which
1829 is that of the step as number of iterations is unsigned. */
1831 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
1833 || wi::gts_p (wtmp
, 0) == wi::gts_p (wi::to_wide (step
), 0)))
1835 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
1836 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
1837 TREE_TYPE (init
), init
, tem
);
1838 /* Likewise if the addition did. */
1839 if (maxvr
.kind () == VR_RANGE
)
1841 value_range_base initvr
;
1843 if (TREE_CODE (init
) == SSA_NAME
)
1844 initvr
= *(get_value_range (init
));
1845 else if (is_gimple_min_invariant (init
))
1850 /* Check if init + nit * step overflows. Though we checked
1851 scev {init, step}_loop doesn't wrap, it is not enough
1852 because the loop may exit immediately. Overflow could
1853 happen in the plus expression in this case. */
1854 if ((dir
== EV_DIR_DECREASES
1855 && compare_values (maxvr
.min (), initvr
.min ()) != -1)
1856 || (dir
== EV_DIR_GROWS
1857 && compare_values (maxvr
.max (), initvr
.max ()) != 1))
1860 tmin
= maxvr
.min ();
1861 tmax
= maxvr
.max ();
1867 if (vr
->varying_p () || vr
->undefined_p ())
1872 /* For VARYING or UNDEFINED ranges, just about anything we get
1873 from scalar evolutions should be better. */
1875 if (dir
== EV_DIR_DECREASES
)
1880 else if (vr
->kind () == VR_RANGE
)
1885 if (dir
== EV_DIR_DECREASES
)
1887 /* INIT is the maximum value. If INIT is lower than VR->MAX ()
1888 but no smaller than VR->MIN (), set VR->MAX () to INIT. */
1889 if (compare_values (init
, max
) == -1)
1892 /* According to the loop information, the variable does not
1894 if (compare_values (min
, tmin
) == -1)
1900 /* If INIT is bigger than VR->MIN (), set VR->MIN () to INIT. */
1901 if (compare_values (init
, min
) == 1)
1904 if (compare_values (tmax
, max
) == -1)
1911 /* If we just created an invalid range with the minimum
1912 greater than the maximum, we fail conservatively.
1913 This should happen only in unreachable
1914 parts of code, or for invalid programs. */
1915 if (compare_values (min
, max
) == 1)
1918 /* Even for valid range info, sometimes overflow flag will leak in.
1919 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1921 if (TREE_OVERFLOW_P (min
))
1922 min
= drop_tree_overflow (min
);
1923 if (TREE_OVERFLOW_P (max
))
1924 max
= drop_tree_overflow (max
);
1926 vr
->update (VR_RANGE
, min
, max
);
1929 /* Dump value ranges of all SSA_NAMEs to FILE. */
1932 vr_values::dump_all_value_ranges (FILE *file
)
1936 for (i
= 0; i
< num_vr_values
; i
++)
1940 print_generic_expr (file
, ssa_name (i
));
1941 fprintf (file
, ": ");
1942 dump_value_range (file
, vr_value
[i
]);
1943 fprintf (file
, "\n");
1947 fprintf (file
, "\n");
1950 /* Initialize VRP lattice. */
1952 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1954 values_propagated
= false;
1955 num_vr_values
= num_ssa_names
* 2;
1956 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
1957 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
1958 bitmap_obstack_initialize (&vrp_equiv_obstack
);
1959 to_remove_edges
= vNULL
;
1960 to_update_switch_stmts
= vNULL
;
1963 /* Free VRP lattice. */
1965 vr_values::~vr_values ()
1967 /* Free allocated memory. */
1969 free (vr_phi_edge_counts
);
1970 bitmap_obstack_release (&vrp_equiv_obstack
);
1971 vrp_value_range_pool
.release ();
1973 /* So that we can distinguish between VRP data being available
1974 and not available. */
1976 vr_phi_edge_counts
= NULL
;
1978 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1979 then an EVRP client did not clean up properly. Catch it now rather
1980 than seeing something more obscure later. */
1981 gcc_assert (to_remove_edges
.is_empty ()
1982 && to_update_switch_stmts
.is_empty ());
1987 static class vr_values
*x_vr_values
;
1989 /* Return the singleton value-range for NAME or NAME. */
1992 vrp_valueize (tree name
)
1994 if (TREE_CODE (name
) == SSA_NAME
)
1996 const value_range
*vr
= x_vr_values
->get_value_range (name
);
1997 if (vr
->kind () == VR_RANGE
1998 && (TREE_CODE (vr
->min ()) == SSA_NAME
1999 || is_gimple_min_invariant (vr
->min ()))
2000 && vrp_operand_equal_p (vr
->min (), vr
->max ()))
2006 /* Return the singleton value-range for NAME if that is a constant
2007 but signal to not follow SSA edges. */
2010 vrp_valueize_1 (tree name
)
2012 if (TREE_CODE (name
) == SSA_NAME
)
2014 /* If the definition may be simulated again we cannot follow
2015 this SSA edge as the SSA propagator does not necessarily
2016 re-visit the use. */
2017 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2018 if (!gimple_nop_p (def_stmt
)
2019 && prop_simulate_again_p (def_stmt
))
2021 const value_range
*vr
= x_vr_values
->get_value_range (name
);
2023 if (vr
->singleton_p (&singleton
))
2029 /* Given STMT, an assignment or call, return its LHS if the type
2030 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
2033 get_output_for_vrp (gimple
*stmt
)
2035 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
2038 /* We only keep track of ranges in integral and pointer types. */
2039 tree lhs
= gimple_get_lhs (stmt
);
2040 if (TREE_CODE (lhs
) == SSA_NAME
2041 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
2042 /* It is valid to have NULL MIN/MAX values on a type. See
2043 build_range_type. */
2044 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
2045 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
2046 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
2052 /* Visit assignment STMT. If it produces an interesting range, record
2053 the range in VR and set LHS to OUTPUT_P. */
2056 vr_values::vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
,
2059 tree lhs
= get_output_for_vrp (stmt
);
2062 /* We only keep track of ranges in integral and pointer types. */
2065 enum gimple_code code
= gimple_code (stmt
);
2067 /* Try folding the statement to a constant first. */
2069 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
2074 if (TREE_CODE (tem
) == SSA_NAME
2075 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
2076 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
2078 extract_range_from_ssa_name (vr
, tem
);
2081 else if (is_gimple_min_invariant (tem
))
2087 /* Then dispatch to value-range extracting functions. */
2088 if (code
== GIMPLE_CALL
)
2089 extract_range_basic (vr
, stmt
);
2091 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
2095 /* Helper that gets the value range of the SSA_NAME with version I
2096 or a symbolic range containing the SSA_NAME only if the value range
2097 is varying or undefined. Uses TEM as storage for the alternate range. */
2100 vr_values::get_vr_for_comparison (int i
, value_range
*tem
)
2102 /* Shallow-copy equiv bitmap. */
2103 const value_range
*vr
= get_value_range (ssa_name (i
));
2105 /* If name N_i does not have a valid range, use N_i as its own
2106 range. This allows us to compare against names that may
2107 have N_i in their ranges. */
2108 if (vr
->varying_p () || vr
->undefined_p ())
2110 tem
->set (ssa_name (i
));
2117 /* Compare all the value ranges for names equivalent to VAR with VAL
2118 using comparison code COMP. Return the same value returned by
2119 compare_range_with_value, including the setting of
2120 *STRICT_OVERFLOW_P. */
2123 vr_values::compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
2124 bool *strict_overflow_p
, bool use_equiv_p
)
2130 int used_strict_overflow
;
2132 const value_range
*equiv_vr
;
2135 /* Get the set of equivalences for VAR. */
2136 e
= get_value_range (var
)->equiv ();
2138 /* Start at -1. Set it to 0 if we do a comparison without relying
2139 on overflow, or 1 if all comparisons rely on overflow. */
2140 used_strict_overflow
= -1;
2142 /* Compare vars' value range with val. */
2143 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
), &tem_vr
);
2145 retval
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2147 used_strict_overflow
= sop
? 1 : 0;
2149 /* If the equiv set is empty we have done all work we need to do. */
2153 && used_strict_overflow
> 0)
2154 *strict_overflow_p
= true;
2158 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
2160 tree name
= ssa_name (i
);
2165 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
2166 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
2169 equiv_vr
= get_vr_for_comparison (i
, &tem_vr
);
2171 t
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2174 /* If we get different answers from different members
2175 of the equivalence set this check must be in a dead
2176 code region. Folding it to a trap representation
2177 would be correct here. For now just return don't-know. */
2187 used_strict_overflow
= 0;
2188 else if (used_strict_overflow
< 0)
2189 used_strict_overflow
= 1;
2194 && used_strict_overflow
> 0)
2195 *strict_overflow_p
= true;
2201 /* Given a comparison code COMP and names N1 and N2, compare all the
2202 ranges equivalent to N1 against all the ranges equivalent to N2
2203 to determine the value of N1 COMP N2. Return the same value
2204 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2205 whether we relied on undefined signed overflow in the comparison. */
2209 vr_values::compare_names (enum tree_code comp
, tree n1
, tree n2
,
2210 bool *strict_overflow_p
)
2214 bitmap_iterator bi1
, bi2
;
2216 int used_strict_overflow
;
2217 static bitmap_obstack
*s_obstack
= NULL
;
2218 static bitmap s_e1
= NULL
, s_e2
= NULL
;
2220 /* Compare the ranges of every name equivalent to N1 against the
2221 ranges of every name equivalent to N2. */
2222 e1
= get_value_range (n1
)->equiv ();
2223 e2
= get_value_range (n2
)->equiv ();
2225 /* Use the fake bitmaps if e1 or e2 are not available. */
2226 if (s_obstack
== NULL
)
2228 s_obstack
= XNEW (bitmap_obstack
);
2229 bitmap_obstack_initialize (s_obstack
);
2230 s_e1
= BITMAP_ALLOC (s_obstack
);
2231 s_e2
= BITMAP_ALLOC (s_obstack
);
2238 /* Add N1 and N2 to their own set of equivalences to avoid
2239 duplicating the body of the loop just to check N1 and N2
2241 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
2242 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
2244 /* If the equivalence sets have a common intersection, then the two
2245 names can be compared without checking their ranges. */
2246 if (bitmap_intersect_p (e1
, e2
))
2248 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2249 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2251 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
2253 : boolean_false_node
;
2256 /* Start at -1. Set it to 0 if we do a comparison without relying
2257 on overflow, or 1 if all comparisons rely on overflow. */
2258 used_strict_overflow
= -1;
2260 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2261 N2 to their own set of equivalences to avoid duplicating the body
2262 of the loop just to check N1 and N2 ranges. */
2263 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
2265 if (! ssa_name (i1
))
2268 value_range tem_vr1
;
2269 const value_range
*vr1
= get_vr_for_comparison (i1
, &tem_vr1
);
2271 t
= retval
= NULL_TREE
;
2272 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
2274 if (! ssa_name (i2
))
2279 value_range tem_vr2
;
2280 const value_range
*vr2
= get_vr_for_comparison (i2
, &tem_vr2
);
2282 t
= compare_ranges (comp
, vr1
, vr2
, &sop
);
2285 /* If we get different answers from different members
2286 of the equivalence set this check must be in a dead
2287 code region. Folding it to a trap representation
2288 would be correct here. For now just return don't-know. */
2292 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2293 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2299 used_strict_overflow
= 0;
2300 else if (used_strict_overflow
< 0)
2301 used_strict_overflow
= 1;
2307 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2308 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2309 if (used_strict_overflow
> 0)
2310 *strict_overflow_p
= true;
2315 /* None of the equivalent ranges are useful in computing this
2317 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2318 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2322 /* Helper function for vrp_evaluate_conditional_warnv & other
2326 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2327 (enum tree_code code
, tree op0
, tree op1
, bool * strict_overflow_p
)
2329 const value_range
*vr0
, *vr1
;
2331 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
2332 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
2334 tree res
= NULL_TREE
;
2336 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
2338 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
2340 res
= (compare_range_with_value
2341 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
2345 /* Helper function for vrp_evaluate_conditional_warnv. */
2348 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
,
2351 bool *strict_overflow_p
,
2356 *only_ranges
= true;
2358 /* We only deal with integral and pointer types. */
2359 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2360 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
2363 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2364 as a simple equality test, then prefer that over its current form
2367 An overflow test which collapses to an equality test can always be
2368 expressed as a comparison of one argument against zero. Overflow
2369 occurs when the chosen argument is zero and does not occur if the
2370 chosen argument is not zero. */
2372 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
2374 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
2375 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2376 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2377 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2378 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2379 if (integer_zerop (x
))
2382 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2384 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2385 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2386 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2387 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2388 else if (wi::to_wide (x
) == max
- 1)
2391 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
2392 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2396 value_range_base vro
, vri
;
2397 if (code
== GT_EXPR
|| code
== GE_EXPR
)
2399 vro
.set (VR_ANTI_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2400 vri
.set (VR_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2402 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
2404 vro
.set (VR_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2405 vri
.set (VR_ANTI_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2409 const value_range
*vr0
= get_value_range (op0
);
2410 /* If vro, the range for OP0 to pass the overflow test, has
2411 no intersection with *vr0, OP0's known range, then the
2412 overflow test can't pass, so return the node for false.
2413 If it is the inverted range, vri, that has no
2414 intersection, then the overflow test must pass, so return
2415 the node for true. In other cases, we could proceed with
2416 a simplified condition comparing OP0 and X, with LE_EXPR
2417 for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
2418 the comments next to the enclosing if suggest it's not
2419 generally profitable to do so. */
2420 vro
.intersect (vr0
);
2421 if (vro
.undefined_p ())
2422 return boolean_false_node
;
2423 vri
.intersect (vr0
);
2424 if (vri
.undefined_p ())
2425 return boolean_true_node
;
2429 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
2430 (code
, op0
, op1
, strict_overflow_p
)))
2433 *only_ranges
= false;
2434 /* Do not use compare_names during propagation, it's quadratic. */
2435 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
2437 return compare_names (code
, op0
, op1
, strict_overflow_p
);
2438 else if (TREE_CODE (op0
) == SSA_NAME
)
2439 return compare_name_with_value (code
, op0
, op1
,
2440 strict_overflow_p
, use_equiv_p
);
2441 else if (TREE_CODE (op1
) == SSA_NAME
)
2442 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
2443 strict_overflow_p
, use_equiv_p
);
2447 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2448 information. Return NULL if the conditional cannot be evaluated.
2449 The ranges of all the names equivalent with the operands in COND
2450 will be used when trying to compute the value. If the result is
2451 based on undefined signed overflow, issue a warning if
2455 vr_values::vrp_evaluate_conditional (tree_code code
, tree op0
,
2456 tree op1
, gimple
*stmt
)
2462 /* Some passes and foldings leak constants with overflow flag set
2463 into the IL. Avoid doing wrong things with these and bail out. */
2464 if ((TREE_CODE (op0
) == INTEGER_CST
2465 && TREE_OVERFLOW (op0
))
2466 || (TREE_CODE (op1
) == INTEGER_CST
2467 && TREE_OVERFLOW (op1
)))
2471 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
2476 enum warn_strict_overflow_code wc
;
2477 const char* warnmsg
;
2479 if (is_gimple_min_invariant (ret
))
2481 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
2482 warnmsg
= G_("assuming signed overflow does not occur when "
2483 "simplifying conditional to constant");
2487 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
2488 warnmsg
= G_("assuming signed overflow does not occur when "
2489 "simplifying conditional");
2492 if (issue_strict_overflow_warning (wc
))
2494 location_t location
;
2496 if (!gimple_has_location (stmt
))
2497 location
= input_location
;
2499 location
= gimple_location (stmt
);
2500 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
2504 if (warn_type_limits
2505 && ret
&& only_ranges
2506 && TREE_CODE_CLASS (code
) == tcc_comparison
2507 && TREE_CODE (op0
) == SSA_NAME
)
2509 /* If the comparison is being folded and the operand on the LHS
2510 is being compared against a constant value that is outside of
2511 the natural range of OP0's type, then the predicate will
2512 always fold regardless of the value of OP0. If -Wtype-limits
2513 was specified, emit a warning. */
2514 tree type
= TREE_TYPE (op0
);
2515 const value_range
*vr0
= get_value_range (op0
);
2517 if (vr0
->kind () == VR_RANGE
2518 && INTEGRAL_TYPE_P (type
)
2519 && vrp_val_is_min (vr0
->min ())
2520 && vrp_val_is_max (vr0
->max ())
2521 && is_gimple_min_invariant (op1
))
2523 location_t location
;
2525 if (!gimple_has_location (stmt
))
2526 location
= input_location
;
2528 location
= gimple_location (stmt
);
2530 warning_at (location
, OPT_Wtype_limits
,
2532 ? G_("comparison always false "
2533 "due to limited range of data type")
2534 : G_("comparison always true "
2535 "due to limited range of data type"));
2543 /* Visit conditional statement STMT. If we can determine which edge
2544 will be taken out of STMT's basic block, record it in
2545 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2548 vr_values::vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
2552 *taken_edge_p
= NULL
;
2554 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2559 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
2560 print_gimple_stmt (dump_file
, stmt
, 0);
2561 fprintf (dump_file
, "\nWith known ranges\n");
2563 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
2565 fprintf (dump_file
, "\t");
2566 print_generic_expr (dump_file
, use
);
2567 fprintf (dump_file
, ": ");
2568 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
2571 fprintf (dump_file
, "\n");
2574 /* Compute the value of the predicate COND by checking the known
2575 ranges of each of its operands.
2577 Note that we cannot evaluate all the equivalent ranges here
2578 because those ranges may not yet be final and with the current
2579 propagation strategy, we cannot determine when the value ranges
2580 of the names in the equivalence set have changed.
2582 For instance, given the following code fragment
2586 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2590 Assume that on the first visit to i_14, i_5 has the temporary
2591 range [8, 8] because the second argument to the PHI function is
2592 not yet executable. We derive the range ~[0, 0] for i_14 and the
2593 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2594 the first time, since i_14 is equivalent to the range [8, 8], we
2595 determine that the predicate is always false.
2597 On the next round of propagation, i_13 is determined to be
2598 VARYING, which causes i_5 to drop down to VARYING. So, another
2599 visit to i_14 is scheduled. In this second visit, we compute the
2600 exact same range and equivalence set for i_14, namely ~[0, 0] and
2601 { i_5 }. But we did not have the previous range for i_5
2602 registered, so vrp_visit_assignment thinks that the range for
2603 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2604 is not visited again, which stops propagation from visiting
2605 statements in the THEN clause of that if().
2607 To properly fix this we would need to keep the previous range
2608 value for the names in the equivalence set. This way we would've
2609 discovered that from one visit to the other i_5 changed from
2610 range [8, 8] to VR_VARYING.
2612 However, fixing this apparent limitation may not be worth the
2613 additional checking. Testing on several code bases (GCC, DLV,
2614 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2615 4 more predicates folded in SPEC. */
2618 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
2619 gimple_cond_lhs (stmt
),
2620 gimple_cond_rhs (stmt
),
2623 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
2625 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2627 fprintf (dump_file
, "\nPredicate evaluates to: ");
2628 if (val
== NULL_TREE
)
2629 fprintf (dump_file
, "DON'T KNOW\n");
2631 print_generic_stmt (dump_file
, val
);
2635 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2636 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2637 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2638 Returns true if the default label is not needed. */
2641 find_case_label_ranges (gswitch
*stmt
, const value_range
*vr
, size_t *min_idx1
,
2642 size_t *max_idx1
, size_t *min_idx2
,
2646 unsigned int n
= gimple_switch_num_labels (stmt
);
2648 tree case_low
, case_high
;
2649 tree min
= vr
->min (), max
= vr
->max ();
2651 gcc_checking_assert (!vr
->varying_p () && !vr
->undefined_p ());
2653 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
2655 /* Set second range to empty. */
2659 if (vr
->kind () == VR_RANGE
)
2663 return !take_default
;
2666 /* Set first range to all case labels. */
2673 /* Make sure all the values of case labels [i , j] are contained in
2674 range [MIN, MAX]. */
2675 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
2676 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
2677 if (tree_int_cst_compare (case_low
, min
) < 0)
2679 if (case_high
!= NULL_TREE
2680 && tree_int_cst_compare (max
, case_high
) < 0)
2686 /* If the range spans case labels [i, j], the corresponding anti-range spans
2687 the labels [1, i - 1] and [j + 1, n - 1]. */
2713 /* Visit switch statement STMT. If we can determine which edge
2714 will be taken out of STMT's basic block, record it in
2715 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2718 vr_values::vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
2721 const value_range
*vr
;
2722 size_t i
= 0, j
= 0, k
, l
;
2725 *taken_edge_p
= NULL
;
2726 op
= gimple_switch_index (stmt
);
2727 if (TREE_CODE (op
) != SSA_NAME
)
2730 vr
= get_value_range (op
);
2731 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2733 fprintf (dump_file
, "\nVisiting switch expression with operand ");
2734 print_generic_expr (dump_file
, op
);
2735 fprintf (dump_file
, " with known range ");
2736 dump_value_range (dump_file
, vr
);
2737 fprintf (dump_file
, "\n");
2740 if (vr
->undefined_p ()
2742 || vr
->symbolic_p ())
2745 /* Find the single edge that is taken from the switch expression. */
2746 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
2748 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2752 gcc_assert (take_default
);
2753 val
= gimple_switch_default_label (stmt
);
2757 /* Check if labels with index i to j and maybe the default label
2758 are all reaching the same label. */
2760 val
= gimple_switch_label (stmt
, i
);
2762 && CASE_LABEL (gimple_switch_default_label (stmt
))
2763 != CASE_LABEL (val
))
2765 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2766 fprintf (dump_file
, " not a single destination for this "
2770 for (++i
; i
<= j
; ++i
)
2772 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
2774 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2775 fprintf (dump_file
, " not a single destination for this "
2782 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
2784 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2785 fprintf (dump_file
, " not a single destination for this "
2792 *taken_edge_p
= find_edge (gimple_bb (stmt
),
2793 label_to_block (cfun
, CASE_LABEL (val
)));
2795 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2797 fprintf (dump_file
, " will take edge to ");
2798 print_generic_stmt (dump_file
, CASE_LABEL (val
));
2803 /* Evaluate statement STMT. If the statement produces a useful range,
2804 set VR and corepsponding OUTPUT_P.
2806 If STMT is a conditional branch and we can determine its truth
2807 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2810 vr_values::extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
2811 tree
*output_p
, value_range
*vr
)
2814 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2816 fprintf (dump_file
, "\nVisiting statement:\n");
2817 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2820 if (!stmt_interesting_for_vrp (stmt
))
2821 gcc_assert (stmt_ends_bb_p (stmt
));
2822 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
2823 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
2824 else if (gimple_code (stmt
) == GIMPLE_COND
)
2825 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
2826 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
2827 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
2830 /* Visit all arguments for PHI node PHI that flow through executable
2831 edges. If a valid value range can be derived from all the incoming
2832 value ranges, set a new range in VR_RESULT. */
2835 vr_values::extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
2838 tree lhs
= PHI_RESULT (phi
);
2839 const value_range
*lhs_vr
= get_value_range (lhs
);
2841 int edges
, old_edges
;
2844 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2846 fprintf (dump_file
, "\nVisiting PHI node: ");
2847 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
2850 bool may_simulate_backedge_again
= false;
2852 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2854 edge e
= gimple_phi_arg_edge (phi
, i
);
2856 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2859 " Argument #%d (%d -> %d %sexecutable)\n",
2860 (int) i
, e
->src
->index
, e
->dest
->index
,
2861 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
2864 if (e
->flags
& EDGE_EXECUTABLE
)
2866 tree arg
= PHI_ARG_DEF (phi
, i
);
2867 value_range vr_arg_tem
;
2868 const value_range
*vr_arg
= &vr_arg_tem
;
2872 if (TREE_CODE (arg
) == SSA_NAME
)
2874 /* See if we are eventually going to change one of the args. */
2875 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
2876 if (! gimple_nop_p (def_stmt
)
2877 && prop_simulate_again_p (def_stmt
)
2878 && e
->flags
& EDGE_DFS_BACK
)
2879 may_simulate_backedge_again
= true;
2881 const value_range
*vr_arg_
= get_value_range (arg
);
2882 /* Do not allow equivalences or symbolic ranges to leak in from
2883 backedges. That creates invalid equivalencies.
2884 See PR53465 and PR54767. */
2885 if (e
->flags
& EDGE_DFS_BACK
)
2887 if (!vr_arg_
->varying_p () && !vr_arg_
->undefined_p ())
2889 vr_arg_tem
.set (vr_arg_
->kind (), vr_arg_
->min (),
2890 vr_arg_
->max (), NULL
);
2891 if (vr_arg_tem
.symbolic_p ())
2892 vr_arg_tem
.set_varying (TREE_TYPE (arg
));
2897 /* If the non-backedge arguments range is VR_VARYING then
2898 we can still try recording a simple equivalence. */
2899 else if (vr_arg_
->varying_p ())
2900 vr_arg_tem
.set (arg
);
2906 if (TREE_OVERFLOW_P (arg
))
2907 arg
= drop_tree_overflow (arg
);
2909 vr_arg_tem
.set (arg
);
2912 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2914 fprintf (dump_file
, "\t");
2915 print_generic_expr (dump_file
, arg
, dump_flags
);
2916 fprintf (dump_file
, ": ");
2917 dump_value_range (dump_file
, vr_arg
);
2918 fprintf (dump_file
, "\n");
2922 vr_result
->deep_copy (vr_arg
);
2924 vr_result
->union_ (vr_arg
);
2927 if (vr_result
->varying_p ())
2932 if (vr_result
->varying_p ())
2934 else if (vr_result
->undefined_p ())
2937 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
2938 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
2940 /* To prevent infinite iterations in the algorithm, derive ranges
2941 when the new value is slightly bigger or smaller than the
2942 previous one. We don't do this if we have seen a new executable
2943 edge; this helps us avoid an infinity for conditionals
2944 which are not in a loop. If the old value-range was VR_UNDEFINED
2945 use the updated range and iterate one more time. If we will not
2946 simulate this PHI again via the backedge allow us to iterate. */
2948 && gimple_phi_num_args (phi
) > 1
2949 && edges
== old_edges
2950 && !lhs_vr
->undefined_p ()
2951 && may_simulate_backedge_again
)
2953 /* Compare old and new ranges, fall back to varying if the
2954 values are not comparable. */
2955 int cmp_min
= compare_values (lhs_vr
->min (), vr_result
->min ());
2958 int cmp_max
= compare_values (lhs_vr
->max (), vr_result
->max ());
2962 /* For non VR_RANGE or for pointers fall back to varying if
2963 the range changed. */
2964 if ((lhs_vr
->kind () != VR_RANGE
|| vr_result
->kind () != VR_RANGE
2965 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2966 && (cmp_min
!= 0 || cmp_max
!= 0))
2969 /* If the new minimum is larger than the previous one
2970 retain the old value. If the new minimum value is smaller
2971 than the previous one and not -INF go all the way to -INF + 1.
2972 In the first case, to avoid infinite bouncing between different
2973 minimums, and in the other case to avoid iterating millions of
2974 times to reach -INF. Going to -INF + 1 also lets the following
2975 iteration compute whether there will be any overflow, at the
2976 expense of one additional iteration. */
2977 tree new_min
= vr_result
->min ();
2978 tree new_max
= vr_result
->max ();
2980 new_min
= lhs_vr
->min ();
2981 else if (cmp_min
> 0
2982 && (TREE_CODE (vr_result
->min ()) != INTEGER_CST
2983 || tree_int_cst_lt (vrp_val_min (vr_result
->type ()),
2984 vr_result
->min ())))
2985 new_min
= int_const_binop (PLUS_EXPR
,
2986 vrp_val_min (vr_result
->type ()),
2987 build_int_cst (vr_result
->type (), 1));
2989 /* Similarly for the maximum value. */
2991 new_max
= lhs_vr
->max ();
2992 else if (cmp_max
< 0
2993 && (TREE_CODE (vr_result
->max ()) != INTEGER_CST
2994 || tree_int_cst_lt (vr_result
->max (),
2995 vrp_val_max (vr_result
->type ()))))
2996 new_max
= int_const_binop (MINUS_EXPR
,
2997 vrp_val_max (vr_result
->type ()),
2998 build_int_cst (vr_result
->type (), 1));
3000 vr_result
->update (vr_result
->kind (), new_min
, new_max
);
3002 /* If we dropped either bound to +-INF then if this is a loop
3003 PHI node SCEV may known more about its value-range. */
3004 if (cmp_min
> 0 || cmp_min
< 0
3005 || cmp_max
< 0 || cmp_max
> 0)
3008 goto infinite_check
;
3014 vr_result
->set_varying (TREE_TYPE (lhs
));
3017 /* If this is a loop PHI node SCEV may known more about its value-range.
3018 scev_check can be reached from two paths, one is a fall through from above
3019 "varying" label, the other is direct goto from code block which tries to
3020 avoid infinite simulation. */
3021 if (scev_initialized_p ()
3022 && (l
= loop_containing_stmt (phi
))
3023 && l
->header
== gimple_bb (phi
))
3024 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
3027 /* If we will end up with a (-INF, +INF) range, set it to
3028 VARYING. Same if the previous max value was invalid for
3029 the type and we end up with vr_result.min > vr_result.max. */
3030 if ((!vr_result
->varying_p () && !vr_result
->undefined_p ())
3031 && !((vrp_val_is_max (vr_result
->max ()) && vrp_val_is_min (vr_result
->min ()))
3032 || compare_values (vr_result
->min (), vr_result
->max ()) > 0))
3035 vr_result
->set_varying (TREE_TYPE (lhs
));
3037 /* If the new range is different than the previous value, keep
3043 /* Simplify boolean operations if the source is known
3044 to be already a boolean. */
3046 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
,
3049 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3051 bool need_conversion
;
3053 /* We handle only !=/== case here. */
3054 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
3056 op0
= gimple_assign_rhs1 (stmt
);
3057 if (!op_with_boolean_value_range_p (op0
))
3060 op1
= gimple_assign_rhs2 (stmt
);
3061 if (!op_with_boolean_value_range_p (op1
))
3064 /* Reduce number of cases to handle to NE_EXPR. As there is no
3065 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
3066 if (rhs_code
== EQ_EXPR
)
3068 if (TREE_CODE (op1
) == INTEGER_CST
)
3069 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
3070 build_int_cst (TREE_TYPE (op1
), 1));
3075 lhs
= gimple_assign_lhs (stmt
);
3077 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
3079 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
3081 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
3082 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
3083 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
3086 /* For A != 0 we can substitute A itself. */
3087 if (integer_zerop (op1
))
3088 gimple_assign_set_rhs_with_ops (gsi
,
3090 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
3091 /* For A != B we substitute A ^ B. Either with conversion. */
3092 else if (need_conversion
)
3094 tree tem
= make_ssa_name (TREE_TYPE (op0
));
3096 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
3097 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
3098 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
3099 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
3100 set_range_info (tem
, VR_RANGE
,
3101 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
3102 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
3103 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
3107 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
3108 update_stmt (gsi_stmt (*gsi
));
3109 fold_stmt (gsi
, follow_single_use_edges
);
3114 /* Simplify a division or modulo operator to a right shift or bitwise and
3115 if the first operand is unsigned or is greater than zero and the second
3116 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3117 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3118 optimize it into just op0 if op0's range is known to be a subset of
3119 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3123 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
,
3126 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3128 tree op0
= gimple_assign_rhs1 (stmt
);
3129 tree op1
= gimple_assign_rhs2 (stmt
);
3130 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
3132 const value_range
*vr
= NULL
;
3134 if (TREE_CODE (op0
) == INTEGER_CST
)
3141 vr
= get_value_range (op0
);
3142 if (range_int_cst_p (vr
))
3144 op0min
= vr
->min ();
3145 op0max
= vr
->max ();
3149 if (rhs_code
== TRUNC_MOD_EXPR
3150 && TREE_CODE (op1
) == SSA_NAME
)
3152 const value_range
*vr1
= get_value_range (op1
);
3153 if (range_int_cst_p (vr1
))
3154 op1min
= vr1
->min ();
3156 if (rhs_code
== TRUNC_MOD_EXPR
3157 && TREE_CODE (op1min
) == INTEGER_CST
3158 && tree_int_cst_sgn (op1min
) == 1
3160 && tree_int_cst_lt (op0max
, op1min
))
3162 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
3163 || tree_int_cst_sgn (op0min
) >= 0
3164 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
3167 /* If op0 already has the range op0 % op1 has,
3168 then TRUNC_MOD_EXPR won't change anything. */
3169 gimple_assign_set_rhs_from_tree (gsi
, op0
);
3174 if (TREE_CODE (op0
) != SSA_NAME
)
3177 if (!integer_pow2p (op1
))
3179 /* X % -Y can be only optimized into X % Y either if
3180 X is not INT_MIN, or Y is not -1. Fold it now, as after
3181 remove_range_assertions the range info might be not available
3183 if (rhs_code
== TRUNC_MOD_EXPR
3184 && fold_stmt (gsi
, follow_single_use_edges
))
3189 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
3190 val
= integer_one_node
;
3195 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
3199 && integer_onep (val
)
3200 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3202 location_t location
;
3204 if (!gimple_has_location (stmt
))
3205 location
= input_location
;
3207 location
= gimple_location (stmt
);
3208 warning_at (location
, OPT_Wstrict_overflow
,
3209 "assuming signed overflow does not occur when "
3210 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3214 if (val
&& integer_onep (val
))
3218 if (rhs_code
== TRUNC_DIV_EXPR
)
3220 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
3221 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
3222 gimple_assign_set_rhs1 (stmt
, op0
);
3223 gimple_assign_set_rhs2 (stmt
, t
);
3227 t
= build_int_cst (TREE_TYPE (op1
), 1);
3228 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
3229 t
= fold_convert (TREE_TYPE (op0
), t
);
3231 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
3232 gimple_assign_set_rhs1 (stmt
, op0
);
3233 gimple_assign_set_rhs2 (stmt
, t
);
3237 fold_stmt (gsi
, follow_single_use_edges
);
3244 /* Simplify a min or max if the ranges of the two operands are
3245 disjoint. Return true if we do simplify. */
3248 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
,
3251 tree op0
= gimple_assign_rhs1 (stmt
);
3252 tree op1
= gimple_assign_rhs2 (stmt
);
3256 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3257 (LE_EXPR
, op0
, op1
, &sop
));
3261 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3262 (LT_EXPR
, op0
, op1
, &sop
));
3267 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3269 location_t location
;
3271 if (!gimple_has_location (stmt
))
3272 location
= input_location
;
3274 location
= gimple_location (stmt
);
3275 warning_at (location
, OPT_Wstrict_overflow
,
3276 "assuming signed overflow does not occur when "
3277 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3280 /* VAL == TRUE -> OP0 < or <= op1
3281 VAL == FALSE -> OP0 > or >= op1. */
3282 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
3283 == integer_zerop (val
)) ? op0
: op1
;
3284 gimple_assign_set_rhs_from_tree (gsi
, res
);
3291 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3292 ABS_EXPR. If the operand is <= 0, then simplify the
3293 ABS_EXPR into a NEGATE_EXPR. */
3296 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3298 tree op
= gimple_assign_rhs1 (stmt
);
3299 const value_range
*vr
= get_value_range (op
);
3306 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
3309 /* The range is neither <= 0 nor > 0. Now see if it is
3310 either < 0 or >= 0. */
3312 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
3318 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3320 location_t location
;
3322 if (!gimple_has_location (stmt
))
3323 location
= input_location
;
3325 location
= gimple_location (stmt
);
3326 warning_at (location
, OPT_Wstrict_overflow
,
3327 "assuming signed overflow does not occur when "
3328 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3331 gimple_assign_set_rhs1 (stmt
, op
);
3332 if (integer_zerop (val
))
3333 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
3335 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
3337 fold_stmt (gsi
, follow_single_use_edges
);
3345 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3346 If all the bits that are being cleared by & are already
3347 known to be zero from VR, or all the bits that are being
3348 set by | are already known to be one from VR, the bit
3349 operation is redundant. */
3352 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
,
3355 tree op0
= gimple_assign_rhs1 (stmt
);
3356 tree op1
= gimple_assign_rhs2 (stmt
);
3357 tree op
= NULL_TREE
;
3358 value_range_base vr0
, vr1
;
3359 wide_int may_be_nonzero0
, may_be_nonzero1
;
3360 wide_int must_be_nonzero0
, must_be_nonzero1
;
3363 if (TREE_CODE (op0
) == SSA_NAME
)
3364 vr0
= *(get_value_range (op0
));
3365 else if (is_gimple_min_invariant (op0
))
3370 if (TREE_CODE (op1
) == SSA_NAME
)
3371 vr1
= *(get_value_range (op1
));
3372 else if (is_gimple_min_invariant (op1
))
3377 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
3380 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
3384 switch (gimple_assign_rhs_code (stmt
))
3387 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3393 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3401 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3407 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3418 if (op
== NULL_TREE
)
3421 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
3422 update_stmt (gsi_stmt (*gsi
));
3426 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3427 a known value range VR.
3429 If there is one and only one value which will satisfy the
3430 conditional, then return that value. Else return NULL.
3432 If signed overflow must be undefined for the value to satisfy
3433 the conditional, then set *STRICT_OVERFLOW_P to true. */
3436 test_for_singularity (enum tree_code cond_code
, tree op0
,
3437 tree op1
, const value_range
*vr
)
3442 /* Extract minimum/maximum values which satisfy the conditional as it was
3444 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
3446 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
3449 if (cond_code
== LT_EXPR
)
3451 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3452 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
3453 /* Signal to compare_values_warnv this expr doesn't overflow. */
3455 TREE_NO_WARNING (max
) = 1;
3458 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
3460 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
3463 if (cond_code
== GT_EXPR
)
3465 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3466 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
3467 /* Signal to compare_values_warnv this expr doesn't overflow. */
3469 TREE_NO_WARNING (min
) = 1;
3473 /* Now refine the minimum and maximum values using any
3474 value range information we have for op0. */
3477 if (compare_values (vr
->min (), min
) == 1)
3479 if (compare_values (vr
->max (), max
) == -1)
3482 /* If the new min/max values have converged to a single value,
3483 then there is only one value which can satisfy the condition,
3484 return that value. */
3485 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
3491 /* Return whether the value range *VR fits in an integer type specified
3492 by PRECISION and UNSIGNED_P. */
3495 range_fits_type_p (const value_range
*vr
,
3496 unsigned dest_precision
, signop dest_sgn
)
3499 unsigned src_precision
;
3503 /* We can only handle integral and pointer types. */
3504 src_type
= vr
->type ();
3505 if (!INTEGRAL_TYPE_P (src_type
)
3506 && !POINTER_TYPE_P (src_type
))
3509 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3510 and so is an identity transform. */
3511 src_precision
= TYPE_PRECISION (vr
->type ());
3512 src_sgn
= TYPE_SIGN (src_type
);
3513 if ((src_precision
< dest_precision
3514 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
3515 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
3518 /* Now we can only handle ranges with constant bounds. */
3519 if (!range_int_cst_p (vr
))
3522 /* For sign changes, the MSB of the wide_int has to be clear.
3523 An unsigned value with its MSB set cannot be represented by
3524 a signed wide_int, while a negative value cannot be represented
3525 by an unsigned wide_int. */
3526 if (src_sgn
!= dest_sgn
3527 && (wi::lts_p (wi::to_wide (vr
->min ()), 0)
3528 || wi::lts_p (wi::to_wide (vr
->max ()), 0)))
3531 /* Then we can perform the conversion on both ends and compare
3532 the result for equality. */
3533 tem
= wi::ext (wi::to_widest (vr
->min ()), dest_precision
, dest_sgn
);
3534 if (tem
!= wi::to_widest (vr
->min ()))
3536 tem
= wi::ext (wi::to_widest (vr
->max ()), dest_precision
, dest_sgn
);
3537 if (tem
!= wi::to_widest (vr
->max ()))
3543 /* Simplify a conditional using a relational operator to an equality
3544 test if the range information indicates only one value can satisfy
3545 the original conditional. */
3548 vr_values::simplify_cond_using_ranges_1 (gcond
*stmt
)
3550 tree op0
= gimple_cond_lhs (stmt
);
3551 tree op1
= gimple_cond_rhs (stmt
);
3552 enum tree_code cond_code
= gimple_cond_code (stmt
);
3554 if (cond_code
!= NE_EXPR
3555 && cond_code
!= EQ_EXPR
3556 && TREE_CODE (op0
) == SSA_NAME
3557 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3558 && is_gimple_min_invariant (op1
))
3560 const value_range
*vr
= get_value_range (op0
);
3562 /* If we have range information for OP0, then we might be
3563 able to simplify this conditional. */
3564 if (vr
->kind () == VR_RANGE
)
3566 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
3571 fprintf (dump_file
, "Simplified relational ");
3572 print_gimple_stmt (dump_file
, stmt
, 0);
3573 fprintf (dump_file
, " into ");
3576 gimple_cond_set_code (stmt
, EQ_EXPR
);
3577 gimple_cond_set_lhs (stmt
, op0
);
3578 gimple_cond_set_rhs (stmt
, new_tree
);
3584 print_gimple_stmt (dump_file
, stmt
, 0);
3585 fprintf (dump_file
, "\n");
3591 /* Try again after inverting the condition. We only deal
3592 with integral types here, so no need to worry about
3593 issues with inverting FP comparisons. */
3594 new_tree
= test_for_singularity
3595 (invert_tree_comparison (cond_code
, false),
3601 fprintf (dump_file
, "Simplified relational ");
3602 print_gimple_stmt (dump_file
, stmt
, 0);
3603 fprintf (dump_file
, " into ");
3606 gimple_cond_set_code (stmt
, NE_EXPR
);
3607 gimple_cond_set_lhs (stmt
, op0
);
3608 gimple_cond_set_rhs (stmt
, new_tree
);
3614 print_gimple_stmt (dump_file
, stmt
, 0);
3615 fprintf (dump_file
, "\n");
3625 /* STMT is a conditional at the end of a basic block.
3627 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3628 was set via a type conversion, try to replace the SSA_NAME with the RHS
3629 of the type conversion. Doing so makes the conversion dead which helps
3630 subsequent passes. */
3633 vr_values::simplify_cond_using_ranges_2 (gcond
*stmt
)
3635 tree op0
= gimple_cond_lhs (stmt
);
3636 tree op1
= gimple_cond_rhs (stmt
);
3638 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3639 see if OP0 was set by a type conversion where the source of
3640 the conversion is another SSA_NAME with a range that fits
3641 into the range of OP0's type.
3643 If so, the conversion is redundant as the earlier SSA_NAME can be
3644 used for the comparison directly if we just massage the constant in the
3646 if (TREE_CODE (op0
) == SSA_NAME
3647 && TREE_CODE (op1
) == INTEGER_CST
)
3649 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
3652 if (!is_gimple_assign (def_stmt
)
3653 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3656 innerop
= gimple_assign_rhs1 (def_stmt
);
3658 if (TREE_CODE (innerop
) == SSA_NAME
3659 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
3660 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
3661 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
3663 const value_range
*vr
= get_value_range (innerop
);
3665 if (range_int_cst_p (vr
)
3666 && range_fits_type_p (vr
,
3667 TYPE_PRECISION (TREE_TYPE (op0
)),
3668 TYPE_SIGN (TREE_TYPE (op0
)))
3669 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
3671 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
3672 gimple_cond_set_lhs (stmt
, innerop
);
3673 gimple_cond_set_rhs (stmt
, newconst
);
3675 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3677 fprintf (dump_file
, "Folded into: ");
3678 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3679 fprintf (dump_file
, "\n");
3686 /* Simplify a switch statement using the value range of the switch
3690 vr_values::simplify_switch_using_ranges (gswitch
*stmt
)
3692 tree op
= gimple_switch_index (stmt
);
3693 const value_range
*vr
= NULL
;
3697 size_t i
= 0, j
= 0, n
, n2
;
3700 size_t k
= 1, l
= 0;
3702 if (TREE_CODE (op
) == SSA_NAME
)
3704 vr
= get_value_range (op
);
3706 /* We can only handle integer ranges. */
3707 if (vr
->varying_p ()
3708 || vr
->undefined_p ()
3709 || vr
->symbolic_p ())
3712 /* Find case label for min/max of the value range. */
3713 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
3715 else if (TREE_CODE (op
) == INTEGER_CST
)
3717 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
3731 n
= gimple_switch_num_labels (stmt
);
3733 /* We can truncate the case label ranges that partially overlap with OP's
3735 size_t min_idx
= 1, max_idx
= 0;
3737 find_case_label_range (stmt
, vr
->min (), vr
->max (), &min_idx
, &max_idx
);
3738 if (min_idx
<= max_idx
)
3740 tree min_label
= gimple_switch_label (stmt
, min_idx
);
3741 tree max_label
= gimple_switch_label (stmt
, max_idx
);
3743 /* Avoid changing the type of the case labels when truncating. */
3744 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
3745 tree vr_min
= fold_convert (case_label_type
, vr
->min ());
3746 tree vr_max
= fold_convert (case_label_type
, vr
->max ());
3748 if (vr
->kind () == VR_RANGE
)
3750 /* If OP's value range is [2,8] and the low label range is
3751 0 ... 3, truncate the label's range to 2 .. 3. */
3752 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3753 && CASE_HIGH (min_label
) != NULL_TREE
3754 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3755 CASE_LOW (min_label
) = vr_min
;
3757 /* If OP's value range is [2,8] and the high label range is
3758 7 ... 10, truncate the label's range to 7 .. 8. */
3759 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3760 && CASE_HIGH (max_label
) != NULL_TREE
3761 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3762 CASE_HIGH (max_label
) = vr_max
;
3764 else if (vr
->kind () == VR_ANTI_RANGE
)
3766 tree one_cst
= build_one_cst (case_label_type
);
3768 if (min_label
== max_label
)
3770 /* If OP's value range is ~[7,8] and the label's range is
3771 7 ... 10, truncate the label's range to 9 ... 10. */
3772 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
3773 && CASE_HIGH (min_label
) != NULL_TREE
3774 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
3775 CASE_LOW (min_label
)
3776 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3778 /* If OP's value range is ~[7,8] and the label's range is
3779 5 ... 8, truncate the label's range to 5 ... 6. */
3780 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3781 && CASE_HIGH (min_label
) != NULL_TREE
3782 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
3783 CASE_HIGH (min_label
)
3784 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3788 /* If OP's value range is ~[2,8] and the low label range is
3789 0 ... 3, truncate the label's range to 0 ... 1. */
3790 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3791 && CASE_HIGH (min_label
) != NULL_TREE
3792 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3793 CASE_HIGH (min_label
)
3794 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3796 /* If OP's value range is ~[2,8] and the high label range is
3797 7 ... 10, truncate the label's range to 9 ... 10. */
3798 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3799 && CASE_HIGH (max_label
) != NULL_TREE
3800 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3801 CASE_LOW (max_label
)
3802 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3806 /* Canonicalize singleton case ranges. */
3807 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
3808 CASE_HIGH (min_label
) = NULL_TREE
;
3809 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
3810 CASE_HIGH (max_label
) = NULL_TREE
;
3813 /* We can also eliminate case labels that lie completely outside OP's value
3816 /* Bail out if this is just all edges taken. */
3822 /* Build a new vector of taken case labels. */
3823 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
3826 /* Add the default edge, if necessary. */
3828 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
3830 for (; i
<= j
; ++i
, ++n2
)
3831 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
3833 for (; k
<= l
; ++k
, ++n2
)
3834 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
3836 /* Mark needed edges. */
3837 for (i
= 0; i
< n2
; ++i
)
3839 e
= find_edge (gimple_bb (stmt
),
3840 label_to_block (cfun
,
3841 CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
3842 e
->aux
= (void *)-1;
3845 /* Queue not needed edges for later removal. */
3846 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
3848 if (e
->aux
== (void *)-1)
3854 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3856 fprintf (dump_file
, "removing unreachable case label\n");
3858 to_remove_edges
.safe_push (e
);
3859 e
->flags
&= ~EDGE_EXECUTABLE
;
3860 e
->flags
|= EDGE_IGNORE
;
3863 /* And queue an update for the stmt. */
3866 to_update_switch_stmts
.safe_push (su
);
3871 vr_values::cleanup_edges_and_switches (void)
3877 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3878 CFG in a broken state and requires a cfg_cleanup run. */
3879 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
3882 /* Update SWITCH_EXPR case label vector. */
3883 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
3886 size_t n
= TREE_VEC_LENGTH (su
->vec
);
3888 gimple_switch_set_num_labels (su
->stmt
, n
);
3889 for (j
= 0; j
< n
; j
++)
3890 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
3891 /* As we may have replaced the default label with a regular one
3892 make sure to make it a real default label again. This ensures
3893 optimal expansion. */
3894 label
= gimple_switch_label (su
->stmt
, 0);
3895 CASE_LOW (label
) = NULL_TREE
;
3896 CASE_HIGH (label
) = NULL_TREE
;
3899 if (!to_remove_edges
.is_empty ())
3901 free_dominance_info (CDI_DOMINATORS
);
3902 loops_state_set (LOOPS_NEED_FIXUP
);
3905 to_remove_edges
.release ();
3906 to_update_switch_stmts
.release ();
3909 /* Simplify an integral conversion from an SSA name in STMT. */
3912 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3914 tree innerop
, middleop
, finaltype
;
3916 signop inner_sgn
, middle_sgn
, final_sgn
;
3917 unsigned inner_prec
, middle_prec
, final_prec
;
3918 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
3920 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
3921 if (!INTEGRAL_TYPE_P (finaltype
))
3923 middleop
= gimple_assign_rhs1 (stmt
);
3924 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
3925 if (!is_gimple_assign (def_stmt
)
3926 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3928 innerop
= gimple_assign_rhs1 (def_stmt
);
3929 if (TREE_CODE (innerop
) != SSA_NAME
3930 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
3933 /* Get the value-range of the inner operand. Use get_range_info in
3934 case innerop was created during substitute-and-fold. */
3935 wide_int imin
, imax
;
3936 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
3937 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
3939 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
3940 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
3942 /* Simulate the conversion chain to check if the result is equal if
3943 the middle conversion is removed. */
3944 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
3945 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
3946 final_prec
= TYPE_PRECISION (finaltype
);
3948 /* If the first conversion is not injective, the second must not
3950 if (wi::gtu_p (innermax
- innermin
,
3951 wi::mask
<widest_int
> (middle_prec
, false))
3952 && middle_prec
< final_prec
)
3954 /* We also want a medium value so that we can track the effect that
3955 narrowing conversions with sign change have. */
3956 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
3957 if (inner_sgn
== UNSIGNED
)
3958 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
3961 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
3962 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
3963 innermed
= innermin
;
3965 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
3966 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
3967 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
3968 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
3970 /* Require that the final conversion applied to both the original
3971 and the intermediate range produces the same result. */
3972 final_sgn
= TYPE_SIGN (finaltype
);
3973 if (wi::ext (middlemin
, final_prec
, final_sgn
)
3974 != wi::ext (innermin
, final_prec
, final_sgn
)
3975 || wi::ext (middlemed
, final_prec
, final_sgn
)
3976 != wi::ext (innermed
, final_prec
, final_sgn
)
3977 || wi::ext (middlemax
, final_prec
, final_sgn
)
3978 != wi::ext (innermax
, final_prec
, final_sgn
))
3981 gimple_assign_set_rhs1 (stmt
, innerop
);
3982 fold_stmt (gsi
, follow_single_use_edges
);
3986 /* Simplify a conversion from integral SSA name to float in STMT. */
3989 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
3992 tree rhs1
= gimple_assign_rhs1 (stmt
);
3993 const value_range
*vr
= get_value_range (rhs1
);
3994 scalar_float_mode fltmode
3995 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
3996 scalar_int_mode mode
;
4000 /* We can only handle constant ranges. */
4001 if (!range_int_cst_p (vr
))
4004 /* First check if we can use a signed type in place of an unsigned. */
4005 scalar_int_mode rhs_mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1
));
4006 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
4007 && can_float_p (fltmode
, rhs_mode
, 0) != CODE_FOR_nothing
4008 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
4010 /* If we can do the conversion in the current input mode do nothing. */
4011 else if (can_float_p (fltmode
, rhs_mode
,
4012 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
4014 /* Otherwise search for a mode we can use, starting from the narrowest
4015 integer mode available. */
4018 mode
= NARROWEST_INT_MODE
;
4021 /* If we cannot do a signed conversion to float from mode
4022 or if the value-range does not fit in the signed type
4023 try with a wider mode. */
4024 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
4025 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
4028 /* But do not widen the input. Instead leave that to the
4029 optabs expansion code. */
4030 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
4031 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
4036 /* It works, insert a truncation or sign-change before the
4037 float conversion. */
4038 tem
= make_ssa_name (build_nonstandard_integer_type
4039 (GET_MODE_PRECISION (mode
), 0));
4040 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
4041 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
4042 gimple_assign_set_rhs1 (stmt
, tem
);
4043 fold_stmt (gsi
, follow_single_use_edges
);
4048 /* Simplify an internal fn call using ranges if possible. */
4051 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
,
4054 enum tree_code subcode
;
4055 bool is_ubsan
= false;
4057 switch (gimple_call_internal_fn (stmt
))
4059 case IFN_UBSAN_CHECK_ADD
:
4060 subcode
= PLUS_EXPR
;
4063 case IFN_UBSAN_CHECK_SUB
:
4064 subcode
= MINUS_EXPR
;
4067 case IFN_UBSAN_CHECK_MUL
:
4068 subcode
= MULT_EXPR
;
4071 case IFN_ADD_OVERFLOW
:
4072 subcode
= PLUS_EXPR
;
4074 case IFN_SUB_OVERFLOW
:
4075 subcode
= MINUS_EXPR
;
4077 case IFN_MUL_OVERFLOW
:
4078 subcode
= MULT_EXPR
;
4084 tree op0
= gimple_call_arg (stmt
, 0);
4085 tree op1
= gimple_call_arg (stmt
, 1);
4089 type
= TREE_TYPE (op0
);
4090 if (VECTOR_TYPE_P (type
))
4093 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
4096 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
4097 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
4098 || (is_ubsan
&& ovf
))
4102 location_t loc
= gimple_location (stmt
);
4104 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
4107 int prec
= TYPE_PRECISION (type
);
4110 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
4111 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
4112 utype
= build_nonstandard_integer_type (prec
, 1);
4113 if (TREE_CODE (op0
) == INTEGER_CST
)
4114 op0
= fold_convert (utype
, op0
);
4115 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
4117 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
4118 gimple_set_location (g
, loc
);
4119 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4120 op0
= gimple_assign_lhs (g
);
4122 if (TREE_CODE (op1
) == INTEGER_CST
)
4123 op1
= fold_convert (utype
, op1
);
4124 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
4126 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
4127 gimple_set_location (g
, loc
);
4128 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4129 op1
= gimple_assign_lhs (g
);
4131 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
4132 gimple_set_location (g
, loc
);
4133 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4136 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
4137 gimple_assign_lhs (g
));
4138 gimple_set_location (g
, loc
);
4139 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4141 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
4142 gimple_assign_lhs (g
),
4143 build_int_cst (type
, ovf
));
4145 gimple_set_location (g
, loc
);
4146 gsi_replace (gsi
, g
, false);
4150 /* Return true if VAR is a two-valued variable. Set a and b with the
4151 two-values when it is true. Return false otherwise. */
4154 vr_values::two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
4156 const value_range
*vr
= get_value_range (var
);
4157 if (vr
->varying_p ()
4158 || vr
->undefined_p ()
4159 || TREE_CODE (vr
->min ()) != INTEGER_CST
4160 || TREE_CODE (vr
->max ()) != INTEGER_CST
)
4163 if (vr
->kind () == VR_RANGE
4164 && wi::to_wide (vr
->max ()) - wi::to_wide (vr
->min ()) == 1)
4171 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4172 if (vr
->kind () == VR_ANTI_RANGE
4173 && (wi::to_wide (vr
->min ())
4174 - wi::to_wide (vrp_val_min (TREE_TYPE (var
)))) == 1
4175 && (wi::to_wide (vrp_val_max (TREE_TYPE (var
)))
4176 - wi::to_wide (vr
->max ())) == 1)
4178 *a
= vrp_val_min (TREE_TYPE (var
));
4179 *b
= vrp_val_max (TREE_TYPE (var
));
4186 /* Simplify STMT using ranges if possible. */
4189 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
4191 gimple
*stmt
= gsi_stmt (*gsi
);
4192 if (is_gimple_assign (stmt
))
4194 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
4195 tree rhs1
= gimple_assign_rhs1 (stmt
);
4196 tree rhs2
= gimple_assign_rhs2 (stmt
);
4197 tree lhs
= gimple_assign_lhs (stmt
);
4198 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
4199 use_operand_p use_p
;
4204 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4206 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4210 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4212 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4214 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
4215 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
4216 && ((TREE_CODE (rhs1
) == INTEGER_CST
4217 && TREE_CODE (rhs2
) == SSA_NAME
)
4218 || (TREE_CODE (rhs2
) == INTEGER_CST
4219 && TREE_CODE (rhs1
) == SSA_NAME
))
4220 && single_imm_use (lhs
, &use_p
, &use_stmt
)
4221 && gimple_code (use_stmt
) == GIMPLE_COND
)
4224 tree new_rhs1
= NULL_TREE
;
4225 tree new_rhs2
= NULL_TREE
;
4226 tree cmp_var
= NULL_TREE
;
4228 if (TREE_CODE (rhs2
) == SSA_NAME
4229 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
4231 /* Optimize RHS1 OP [VAL1, VAL2]. */
4232 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
4233 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
4236 else if (TREE_CODE (rhs1
) == SSA_NAME
4237 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
4239 /* Optimize [VAL1, VAL2] OP RHS2. */
4240 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
4241 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
4245 /* If we could not find two-vals or the optimzation is invalid as
4246 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4247 if (new_rhs1
&& new_rhs2
)
4249 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
4250 gimple_assign_set_rhs_with_ops (gsi
,
4254 update_stmt (gsi_stmt (*gsi
));
4255 fold_stmt (gsi
, follow_single_use_edges
);
4264 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4265 if the RHS is zero or one, and the LHS are known to be boolean
4267 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4268 return simplify_truth_ops_using_ranges (gsi
, stmt
);
4271 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4272 and BIT_AND_EXPR respectively if the first operand is greater
4273 than zero and the second operand is an exact power of two.
4274 Also optimize TRUNC_MOD_EXPR away if the second operand is
4275 constant and the first operand already has the right value
4277 case TRUNC_DIV_EXPR
:
4278 case TRUNC_MOD_EXPR
:
4279 if ((TREE_CODE (rhs1
) == SSA_NAME
4280 || TREE_CODE (rhs1
) == INTEGER_CST
)
4281 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4282 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
4285 /* Transform ABS (X) into X or -X as appropriate. */
4287 if (TREE_CODE (rhs1
) == SSA_NAME
4288 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4289 return simplify_abs_using_ranges (gsi
, stmt
);
4294 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4295 if all the bits being cleared are already cleared or
4296 all the bits being set are already set. */
4297 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4298 return simplify_bit_ops_using_ranges (gsi
, stmt
);
4302 if (TREE_CODE (rhs1
) == SSA_NAME
4303 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4304 return simplify_conversion_using_ranges (gsi
, stmt
);
4308 if (TREE_CODE (rhs1
) == SSA_NAME
4309 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4310 return simplify_float_conversion_using_ranges (gsi
, stmt
);
4315 return simplify_min_or_max_using_ranges (gsi
, stmt
);
4321 else if (gimple_code (stmt
) == GIMPLE_COND
)
4322 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
4323 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
4324 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
4325 else if (is_gimple_call (stmt
)
4326 && gimple_call_internal_p (stmt
))
4327 return simplify_internal_call_using_ranges (gsi
, stmt
);
4332 /* Set the lattice entry for VAR to VR. */
4335 vr_values::set_vr_value (tree var
, value_range
*vr
)
4337 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
4339 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
4342 /* Swap the lattice entry for VAR with VR and return the old entry. */
4345 vr_values::swap_vr_value (tree var
, value_range
*vr
)
4347 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
4349 std::swap (vr_value
[SSA_NAME_VERSION (var
)], vr
);