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)
69 vr
->update (VR_RANGE
, build_int_cst (type
, 0), build_int_cst (type
, 1));
73 /* Return value range information for VAR.
75 If we have no values ranges recorded (ie, VRP is not running), then
76 return NULL. Otherwise create an empty range if none existed for VAR. */
79 vr_values::get_value_range (const_tree var
)
81 static const value_range
vr_const_varying (VR_VARYING
, NULL
, NULL
);
84 unsigned ver
= SSA_NAME_VERSION (var
);
86 /* If we have no recorded ranges, then return NULL. */
90 /* If we query the range for a new SSA name return an unmodifiable VARYING.
91 We should get here at most from the substitute-and-fold stage which
92 will never try to change values. */
93 if (ver
>= num_vr_values
)
94 return CONST_CAST (value_range
*, &vr_const_varying
);
100 /* After propagation finished do not allocate new value-ranges. */
101 if (values_propagated
)
102 return CONST_CAST (value_range
*, &vr_const_varying
);
104 /* Create a default value range. */
105 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
106 vr
->set_undefined ();
108 /* If VAR is a default definition of a parameter, the variable can
109 take any value in VAR's type. */
110 if (SSA_NAME_IS_DEFAULT_DEF (var
))
112 sym
= SSA_NAME_VAR (var
);
113 if (TREE_CODE (sym
) == PARM_DECL
)
115 /* Try to use the "nonnull" attribute to create ~[0, 0]
116 anti-ranges for pointers. Note that this is only valid with
117 default definitions of PARM_DECLs. */
118 if (POINTER_TYPE_P (TREE_TYPE (sym
))
119 && (nonnull_arg_p (sym
)
120 || get_ptr_nonnull (var
)))
121 vr
->set_nonnull (TREE_TYPE (sym
));
122 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
124 get_range_info (var
, *vr
);
125 if (vr
->undefined_p ())
131 else if (TREE_CODE (sym
) == RESULT_DECL
132 && DECL_BY_REFERENCE (sym
))
133 vr
->set_nonnull (TREE_TYPE (sym
));
139 /* Set value-ranges of all SSA names defined by STMT to varying. */
142 vr_values::set_defs_to_varying (gimple
*stmt
)
146 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
148 value_range
*vr
= get_value_range (def
);
149 /* Avoid writing to vr_const_varying get_value_range may return. */
150 if (!vr
->varying_p ())
155 /* Update the value range and equivalence set for variable VAR to
156 NEW_VR. Return true if NEW_VR is different from VAR's previous
159 NOTE: This function assumes that NEW_VR is a temporary value range
160 object created for the sole purpose of updating VAR's range. The
161 storage used by the equivalence set from NEW_VR will be freed by
162 this function. Do not call update_value_range when NEW_VR
163 is the range object associated with another SSA name. */
166 vr_values::update_value_range (const_tree var
, value_range
*new_vr
)
171 /* If there is a value-range on the SSA name from earlier analysis
173 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
176 value_range_kind rtype
= get_range_info (var
, nr
);
177 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
178 new_vr
->intersect (&nr
);
181 /* Update the value range, if necessary. */
182 old_vr
= get_value_range (var
);
183 is_new
= !old_vr
->equal_p (*new_vr
, /*ignore_equivs=*/false);
187 /* Do not allow transitions up the lattice. The following
188 is slightly more awkward than just new_vr->type < old_vr->type
189 because VR_RANGE and VR_ANTI_RANGE need to be considered
190 the same. We may not have is_new when transitioning to
191 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
192 called, if we are anyway, keep it VARYING. */
193 if (old_vr
->varying_p ())
195 new_vr
->set_varying ();
198 else if (new_vr
->undefined_p ())
200 old_vr
->set_varying ();
201 new_vr
->set_varying ();
205 old_vr
->set (new_vr
->kind (),
206 new_vr
->min (), new_vr
->max (), new_vr
->equiv ());
209 new_vr
->equiv_clear ();
214 /* Return true if value range VR involves exactly one symbol SYM. */
217 symbolic_range_based_on_p (value_range_base
*vr
, const_tree sym
)
219 bool neg
, min_has_symbol
, max_has_symbol
;
222 if (is_gimple_min_invariant (vr
->min ()))
223 min_has_symbol
= false;
224 else if (get_single_symbol (vr
->min (), &neg
, &inv
) == sym
)
225 min_has_symbol
= true;
229 if (is_gimple_min_invariant (vr
->max ()))
230 max_has_symbol
= false;
231 else if (get_single_symbol (vr
->max (), &neg
, &inv
) == sym
)
232 max_has_symbol
= true;
236 return (min_has_symbol
|| max_has_symbol
);
239 /* Return true if the result of assignment STMT is know to be non-zero. */
242 gimple_assign_nonzero_p (gimple
*stmt
)
244 enum tree_code code
= gimple_assign_rhs_code (stmt
);
245 bool strict_overflow_p
;
246 switch (get_gimple_rhs_class (code
))
248 case GIMPLE_UNARY_RHS
:
249 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
250 gimple_expr_type (stmt
),
251 gimple_assign_rhs1 (stmt
),
253 case GIMPLE_BINARY_RHS
:
254 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
255 gimple_expr_type (stmt
),
256 gimple_assign_rhs1 (stmt
),
257 gimple_assign_rhs2 (stmt
),
259 case GIMPLE_TERNARY_RHS
:
261 case GIMPLE_SINGLE_RHS
:
262 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
264 case GIMPLE_INVALID_RHS
:
271 /* Return true if STMT is known to compute a non-zero value. */
274 gimple_stmt_nonzero_p (gimple
*stmt
)
276 switch (gimple_code (stmt
))
279 return gimple_assign_nonzero_p (stmt
);
282 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
283 return (gimple_call_nonnull_result_p (call_stmt
)
284 || gimple_call_nonnull_arg (call_stmt
));
290 /* Like tree_expr_nonzero_p, but this function uses value ranges
294 vr_values::vrp_stmt_computes_nonzero (gimple
*stmt
)
296 if (gimple_stmt_nonzero_p (stmt
))
299 /* If we have an expression of the form &X->a, then the expression
300 is nonnull if X is nonnull. */
301 if (is_gimple_assign (stmt
)
302 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
304 tree expr
= gimple_assign_rhs1 (stmt
);
305 poly_int64 bitsize
, bitpos
;
308 int unsignedp
, reversep
, volatilep
;
309 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
310 &bitpos
, &offset
, &mode
, &unsignedp
,
311 &reversep
, &volatilep
);
313 if (base
!= NULL_TREE
314 && TREE_CODE (base
) == MEM_REF
315 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
317 poly_offset_int off
= 0;
318 bool off_cst
= false;
319 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
321 off
= mem_ref_offset (base
);
323 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
325 off
<<= LOG2_BITS_PER_UNIT
;
329 /* If &X->a is equal to X and X is ~[0, 0], the result is too.
330 For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
331 allow going from non-NULL pointer to NULL. */
332 if ((off_cst
&& known_eq (off
, 0))
333 || (flag_delete_null_pointer_checks
334 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))))
336 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
337 if (!range_includes_zero_p (vr
))
340 /* If MEM_REF has a "positive" offset, consider it non-NULL
341 always, for -fdelete-null-pointer-checks also "negative"
342 ones. Punt for unknown offsets (e.g. variable ones). */
343 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
346 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
354 /* Returns true if EXPR is a valid value (as expected by compare_values) --
355 a gimple invariant, or SSA_NAME +- CST. */
358 valid_value_p (tree expr
)
360 if (TREE_CODE (expr
) == SSA_NAME
)
363 if (TREE_CODE (expr
) == PLUS_EXPR
364 || TREE_CODE (expr
) == MINUS_EXPR
)
365 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
366 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
368 return is_gimple_min_invariant (expr
);
371 /* If OP has a value range with a single constant value return that,
372 otherwise return NULL_TREE. This returns OP itself if OP is a
376 vr_values::op_with_constant_singleton_value_range (tree op
)
378 if (is_gimple_min_invariant (op
))
381 if (TREE_CODE (op
) != SSA_NAME
)
384 return value_range_constant_singleton (get_value_range (op
));
387 /* Return true if op is in a boolean [0, 1] value-range. */
390 vr_values::op_with_boolean_value_range_p (tree op
)
394 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
397 if (integer_zerop (op
)
398 || integer_onep (op
))
401 if (TREE_CODE (op
) != SSA_NAME
)
404 vr
= get_value_range (op
);
405 return (vr
->kind () == VR_RANGE
406 && integer_zerop (vr
->min ())
407 && integer_onep (vr
->max ()));
410 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
411 true and store it in *VR_P. */
414 vr_values::extract_range_for_var_from_comparison_expr (tree var
,
415 enum tree_code cond_code
,
420 value_range
*limit_vr
;
421 type
= TREE_TYPE (var
);
423 /* For pointer arithmetic, we only keep track of pointer equality
424 and inequality. If we arrive here with unfolded conditions like
425 _1 > _1 do not derive anything. */
426 if ((POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
429 vr_p
->set_varying ();
433 /* If LIMIT is another SSA name and LIMIT has a range of its own,
434 try to use LIMIT's range to avoid creating symbolic ranges
436 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
438 /* LIMIT's range is only interesting if it has any useful information. */
440 || limit_vr
->undefined_p ()
441 || limit_vr
->varying_p ()
442 || (limit_vr
->symbolic_p ()
443 && ! (limit_vr
->kind () == VR_RANGE
444 && (limit_vr
->min () == limit_vr
->max ()
445 || operand_equal_p (limit_vr
->min (),
446 limit_vr
->max (), 0)))))
449 /* Initially, the new range has the same set of equivalences of
450 VAR's range. This will be revised before returning the final
451 value. Since assertions may be chained via mutually exclusive
452 predicates, we will need to trim the set of equivalences before
454 gcc_assert (vr_p
->equiv () == NULL
);
455 vr_p
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
457 /* Extract a new range based on the asserted comparison for VAR and
458 LIMIT's value range. Notice that if LIMIT has an anti-range, we
459 will only use it for equality comparisons (EQ_EXPR). For any
460 other kind of assertion, we cannot derive a range from LIMIT's
461 anti-range that can be used to describe the new range. For
462 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
463 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
464 no single range for x_2 that could describe LE_EXPR, so we might
465 as well build the range [b_4, +INF] for it.
466 One special case we handle is extracting a range from a
467 range test encoded as (unsigned)var + CST <= limit. */
468 if (TREE_CODE (op
) == NOP_EXPR
469 || TREE_CODE (op
) == PLUS_EXPR
)
471 if (TREE_CODE (op
) == PLUS_EXPR
)
473 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
474 TREE_OPERAND (op
, 1));
475 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
476 op
= TREE_OPERAND (op
, 0);
480 min
= build_int_cst (TREE_TYPE (var
), 0);
484 /* Make sure to not set TREE_OVERFLOW on the final type
485 conversion. We are willingly interpreting large positive
486 unsigned values as negative signed values here. */
487 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
488 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
490 /* We can transform a max, min range to an anti-range or
491 vice-versa. Use set_and_canonicalize which does this for
493 if (cond_code
== LE_EXPR
)
494 vr_p
->set_and_canonicalize (VR_RANGE
, min
, max
, vr_p
->equiv ());
495 else if (cond_code
== GT_EXPR
)
496 vr_p
->set_and_canonicalize (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
500 else if (cond_code
== EQ_EXPR
)
502 enum value_range_kind range_type
;
506 range_type
= limit_vr
->kind ();
507 min
= limit_vr
->min ();
508 max
= limit_vr
->max ();
512 range_type
= VR_RANGE
;
517 vr_p
->update (range_type
, min
, max
);
519 /* When asserting the equality VAR == LIMIT and LIMIT is another
520 SSA name, the new range will also inherit the equivalence set
522 if (TREE_CODE (limit
) == SSA_NAME
)
523 vr_p
->equiv_add (limit
, get_value_range (limit
), &vrp_equiv_obstack
);
525 else if (cond_code
== NE_EXPR
)
527 /* As described above, when LIMIT's range is an anti-range and
528 this assertion is an inequality (NE_EXPR), then we cannot
529 derive anything from the anti-range. For instance, if
530 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
531 not imply that VAR's range is [0, 0]. So, in the case of
532 anti-ranges, we just assert the inequality using LIMIT and
535 If LIMIT_VR is a range, we can only use it to build a new
536 anti-range if LIMIT_VR is a single-valued range. For
537 instance, if LIMIT_VR is [0, 1], the predicate
538 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
539 Rather, it means that for value 0 VAR should be ~[0, 0]
540 and for value 1, VAR should be ~[1, 1]. We cannot
541 represent these ranges.
543 The only situation in which we can build a valid
544 anti-range is when LIMIT_VR is a single-valued range
545 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
546 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
548 && limit_vr
->kind () == VR_RANGE
549 && compare_values (limit_vr
->min (), limit_vr
->max ()) == 0)
551 min
= limit_vr
->min ();
552 max
= limit_vr
->max ();
556 /* In any other case, we cannot use LIMIT's range to build a
561 /* If MIN and MAX cover the whole range for their type, then
562 just use the original LIMIT. */
563 if (INTEGRAL_TYPE_P (type
)
564 && vrp_val_is_min (min
)
565 && vrp_val_is_max (max
))
568 vr_p
->set_and_canonicalize (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
570 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
572 min
= TYPE_MIN_VALUE (type
);
574 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
578 /* If LIMIT_VR is of the form [N1, N2], we need to build the
579 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
581 max
= limit_vr
->max ();
584 /* If the maximum value forces us to be out of bounds, simply punt.
585 It would be pointless to try and do anything more since this
586 all should be optimized away above us. */
587 if (cond_code
== LT_EXPR
588 && compare_values (max
, min
) == 0)
589 vr_p
->set_varying ();
592 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
593 if (cond_code
== LT_EXPR
)
595 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
596 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
597 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
598 build_int_cst (TREE_TYPE (max
), -1));
600 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
601 build_int_cst (TREE_TYPE (max
), 1));
602 /* Signal to compare_values_warnv this expr doesn't overflow. */
604 TREE_NO_WARNING (max
) = 1;
607 vr_p
->update (VR_RANGE
, min
, max
);
610 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
612 max
= TYPE_MAX_VALUE (type
);
614 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
618 /* If LIMIT_VR is of the form [N1, N2], we need to build the
619 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
621 min
= limit_vr
->min ();
624 /* If the minimum value forces us to be out of bounds, simply punt.
625 It would be pointless to try and do anything more since this
626 all should be optimized away above us. */
627 if (cond_code
== GT_EXPR
628 && compare_values (min
, max
) == 0)
629 vr_p
->set_varying ();
632 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
633 if (cond_code
== GT_EXPR
)
635 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
636 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
637 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
638 build_int_cst (TREE_TYPE (min
), -1));
640 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
641 build_int_cst (TREE_TYPE (min
), 1));
642 /* Signal to compare_values_warnv this expr doesn't overflow. */
644 TREE_NO_WARNING (min
) = 1;
647 vr_p
->update (VR_RANGE
, min
, max
);
653 /* Finally intersect the new range with what we already know about var. */
654 vr_p
->intersect (get_value_range (var
));
657 /* Extract value range information from an ASSERT_EXPR EXPR and store
661 vr_values::extract_range_from_assert (value_range
*vr_p
, tree expr
)
663 tree var
= ASSERT_EXPR_VAR (expr
);
664 tree cond
= ASSERT_EXPR_COND (expr
);
666 enum tree_code cond_code
;
667 gcc_assert (COMPARISON_CLASS_P (cond
));
669 /* Find VAR in the ASSERT_EXPR conditional. */
670 if (var
== TREE_OPERAND (cond
, 0)
671 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
672 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
674 /* If the predicate is of the form VAR COMP LIMIT, then we just
675 take LIMIT from the RHS and use the same comparison code. */
676 cond_code
= TREE_CODE (cond
);
677 limit
= TREE_OPERAND (cond
, 1);
678 op
= TREE_OPERAND (cond
, 0);
682 /* If the predicate is of the form LIMIT COMP VAR, then we need
683 to flip around the comparison code to create the proper range
685 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
686 limit
= TREE_OPERAND (cond
, 0);
687 op
= TREE_OPERAND (cond
, 1);
689 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
693 /* Extract range information from SSA name VAR and store it in VR. If
694 VAR has an interesting range, use it. Otherwise, create the
695 range [VAR, VAR] and return it. This is useful in situations where
696 we may have conditionals testing values of VARYING names. For
703 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
707 vr_values::extract_range_from_ssa_name (value_range
*vr
, tree var
)
709 value_range
*var_vr
= get_value_range (var
);
711 if (!var_vr
->varying_p ())
712 vr
->deep_copy (var_vr
);
716 vr
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
719 /* Extract range information from a binary expression OP0 CODE OP1 based on
720 the ranges of each of its operands with resulting type EXPR_TYPE.
721 The resulting range is stored in *VR. */
724 vr_values::extract_range_from_binary_expr (value_range
*vr
,
726 tree expr_type
, tree op0
, tree op1
)
728 /* Get value ranges for each operand. For constant operands, create
729 a new value range with the operand to simplify processing. */
730 value_range_base vr0
, vr1
;
731 if (TREE_CODE (op0
) == SSA_NAME
)
732 vr0
= *(get_value_range (op0
));
733 else if (is_gimple_min_invariant (op0
))
738 if (TREE_CODE (op1
) == SSA_NAME
)
739 vr1
= *(get_value_range (op1
));
740 else if (is_gimple_min_invariant (op1
))
745 /* If one argument is varying, we can sometimes still deduce a
746 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
747 if (INTEGRAL_TYPE_P (TREE_TYPE (op0
))
748 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
750 if (vr0
.varying_p () && !vr1
.varying_p ())
751 vr0
= value_range (VR_RANGE
,
752 vrp_val_min (expr_type
),
753 vrp_val_max (expr_type
));
754 else if (vr1
.varying_p () && !vr0
.varying_p ())
755 vr1
= value_range (VR_RANGE
,
756 vrp_val_min (expr_type
),
757 vrp_val_max (expr_type
));
760 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &vr0
, &vr1
);
762 /* Set value_range for n in following sequence:
763 def = __builtin_memchr (arg, 0, sz)
765 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
768 && code
== POINTER_DIFF_EXPR
769 && TREE_CODE (op0
) == SSA_NAME
770 && TREE_CODE (op1
) == SSA_NAME
)
772 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
773 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
774 gcall
*call_stmt
= NULL
;
776 if (TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
777 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
778 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
779 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
780 && (call_stmt
= dyn_cast
<gcall
*>(SSA_NAME_DEF_STMT (op0
)))
781 && gimple_call_builtin_p (call_stmt
, BUILT_IN_MEMCHR
)
782 && operand_equal_p (op0
, gimple_call_lhs (call_stmt
), 0)
783 && operand_equal_p (op1
, gimple_call_arg (call_stmt
, 0), 0)
784 && integer_zerop (gimple_call_arg (call_stmt
, 1)))
786 tree max
= vrp_val_max (ptrdiff_type_node
);
787 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
788 tree range_min
= build_zero_cst (expr_type
);
789 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
790 vr
->set (VR_RANGE
, range_min
, range_max
);
795 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
796 and based on the other operand, for example if it was deduced from a
797 symbolic comparison. When a bound of the range of the first operand
798 is invariant, we set the corresponding bound of the new range to INF
799 in order to avoid recursing on the range of the second operand. */
801 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
802 && TREE_CODE (op1
) == SSA_NAME
803 && vr0
.kind () == VR_RANGE
804 && symbolic_range_based_on_p (&vr0
, op1
))
806 const bool minus_p
= (code
== MINUS_EXPR
);
809 /* Try with VR0 and [-INF, OP1]. */
810 if (is_gimple_min_invariant (minus_p
? vr0
.max () : vr0
.min ()))
811 n_vr1
.set (VR_RANGE
, vrp_val_min (expr_type
), op1
);
813 /* Try with VR0 and [OP1, +INF]. */
814 else if (is_gimple_min_invariant (minus_p
? vr0
.min () : vr0
.max ()))
815 n_vr1
.set (VR_RANGE
, op1
, vrp_val_max (expr_type
));
817 /* Try with VR0 and [OP1, OP1]. */
819 n_vr1
.set (VR_RANGE
, op1
, op1
);
821 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &vr0
, &n_vr1
);
825 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
826 && TREE_CODE (op0
) == SSA_NAME
827 && vr1
.kind () == VR_RANGE
828 && symbolic_range_based_on_p (&vr1
, op0
))
830 const bool minus_p
= (code
== MINUS_EXPR
);
833 /* Try with [-INF, OP0] and VR1. */
834 if (is_gimple_min_invariant (minus_p
? vr1
.max () : vr1
.min ()))
835 n_vr0
.set (VR_RANGE
, vrp_val_min (expr_type
), op0
);
837 /* Try with [OP0, +INF] and VR1. */
838 else if (is_gimple_min_invariant (minus_p
? vr1
.min (): vr1
.max ()))
839 n_vr0
.set (VR_RANGE
, op0
, vrp_val_max (expr_type
));
841 /* Try with [OP0, OP0] and VR1. */
845 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &n_vr0
, &vr1
);
848 /* If we didn't derive a range for MINUS_EXPR, and
849 op1's range is ~[op0,op0] or vice-versa, then we
850 can derive a non-null range. This happens often for
851 pointer subtraction. */
853 && (code
== MINUS_EXPR
|| code
== POINTER_DIFF_EXPR
)
854 && TREE_CODE (op0
) == SSA_NAME
855 && ((vr0
.kind () == VR_ANTI_RANGE
857 && vr0
.min () == vr0
.max ())
858 || (vr1
.kind () == VR_ANTI_RANGE
860 && vr1
.min () == vr1
.max ())))
861 vr
->set_nonnull (expr_type
);
864 /* Extract range information from a unary expression CODE OP0 based on
865 the range of its operand with resulting type TYPE.
866 The resulting range is stored in *VR. */
869 vr_values::extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
872 value_range_base vr0
;
874 /* Get value ranges for the operand. For constant operands, create
875 a new value range with the operand to simplify processing. */
876 if (TREE_CODE (op0
) == SSA_NAME
)
877 vr0
= *(get_value_range (op0
));
878 else if (is_gimple_min_invariant (op0
))
883 ::extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
887 /* Extract range information from a conditional expression STMT based on
888 the ranges of each of its operands and the expression code. */
891 vr_values::extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
893 /* Get value ranges for each operand. For constant operands, create
894 a new value range with the operand to simplify processing. */
895 tree op0
= gimple_assign_rhs2 (stmt
);
897 value_range
*vr0
= &tem0
;
898 if (TREE_CODE (op0
) == SSA_NAME
)
899 vr0
= get_value_range (op0
);
900 else if (is_gimple_min_invariant (op0
))
905 tree op1
= gimple_assign_rhs3 (stmt
);
907 value_range
*vr1
= &tem1
;
908 if (TREE_CODE (op1
) == SSA_NAME
)
909 vr1
= get_value_range (op1
);
910 else if (is_gimple_min_invariant (op1
))
915 /* The resulting value range is the union of the operand ranges */
921 /* Extract range information from a comparison expression EXPR based
922 on the range of its operand and the expression code. */
925 vr_values::extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
926 tree type
, tree op0
, tree op1
)
931 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
935 /* Since this expression was found on the RHS of an assignment,
936 its type may be different from _Bool. Convert VAL to EXPR's
938 val
= fold_convert (type
, val
);
939 if (is_gimple_min_invariant (val
))
942 vr
->update (VR_RANGE
, val
, val
);
945 /* The result of a comparison is always true or false. */
946 set_value_range_to_truthvalue (vr
, type
);
949 /* Helper function for simplify_internal_call_using_ranges and
950 extract_range_basic. Return true if OP0 SUBCODE OP1 for
951 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
952 always overflow. Set *OVF to true if it is known to always
956 vr_values::check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
957 tree op0
, tree op1
, bool *ovf
)
959 value_range_base vr0
, vr1
;
960 if (TREE_CODE (op0
) == SSA_NAME
)
961 vr0
= *get_value_range (op0
);
962 else if (TREE_CODE (op0
) == INTEGER_CST
)
967 if (TREE_CODE (op1
) == SSA_NAME
)
968 vr1
= *get_value_range (op1
);
969 else if (TREE_CODE (op1
) == INTEGER_CST
)
974 tree vr0min
= vr0
.min (), vr0max
= vr0
.max ();
975 tree vr1min
= vr1
.min (), vr1max
= vr1
.max ();
976 if (!range_int_cst_p (&vr0
)
977 || TREE_OVERFLOW (vr0min
)
978 || TREE_OVERFLOW (vr0max
))
980 vr0min
= vrp_val_min (TREE_TYPE (op0
));
981 vr0max
= vrp_val_max (TREE_TYPE (op0
));
983 if (!range_int_cst_p (&vr1
)
984 || TREE_OVERFLOW (vr1min
)
985 || TREE_OVERFLOW (vr1max
))
987 vr1min
= vrp_val_min (TREE_TYPE (op1
));
988 vr1max
= vrp_val_max (TREE_TYPE (op1
));
990 *ovf
= arith_overflowed_p (subcode
, type
, vr0min
,
991 subcode
== MINUS_EXPR
? vr1max
: vr1min
);
992 if (arith_overflowed_p (subcode
, type
, vr0max
,
993 subcode
== MINUS_EXPR
? vr1min
: vr1max
) != *ovf
)
995 if (subcode
== MULT_EXPR
)
997 if (arith_overflowed_p (subcode
, type
, vr0min
, vr1max
) != *ovf
998 || arith_overflowed_p (subcode
, type
, vr0max
, vr1min
) != *ovf
)
1003 /* So far we found that there is an overflow on the boundaries.
1004 That doesn't prove that there is an overflow even for all values
1005 in between the boundaries. For that compute widest_int range
1006 of the result and see if it doesn't overlap the range of
1008 widest_int wmin
, wmax
;
1011 w
[0] = wi::to_widest (vr0min
);
1012 w
[1] = wi::to_widest (vr0max
);
1013 w
[2] = wi::to_widest (vr1min
);
1014 w
[3] = wi::to_widest (vr1max
);
1015 for (i
= 0; i
< 4; i
++)
1021 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1024 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1027 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1039 wmin
= wi::smin (wmin
, wt
);
1040 wmax
= wi::smax (wmax
, wt
);
1043 /* The result of op0 CODE op1 is known to be in range
1045 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
1046 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
1047 /* If all values in [wmin, wmax] are smaller than
1048 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1049 the arithmetic operation will always overflow. */
1050 if (wmax
< wtmin
|| wmin
> wtmax
)
1057 /* Try to derive a nonnegative or nonzero range out of STMT relying
1058 primarily on generic routines in fold in conjunction with range data.
1059 Store the result in *VR */
1062 vr_values::extract_range_basic (value_range
*vr
, gimple
*stmt
)
1065 tree type
= gimple_expr_type (stmt
);
1067 if (is_gimple_call (stmt
))
1070 int mini
, maxi
, zerov
= 0, prec
;
1071 enum tree_code subcode
= ERROR_MARK
;
1072 combined_fn cfn
= gimple_call_combined_fn (stmt
);
1073 scalar_int_mode mode
;
1077 case CFN_BUILT_IN_CONSTANT_P
:
1078 /* If the call is __builtin_constant_p and the argument is a
1079 function parameter resolve it to false. This avoids bogus
1080 array bound warnings.
1081 ??? We could do this as early as inlining is finished. */
1082 arg
= gimple_call_arg (stmt
, 0);
1083 if (TREE_CODE (arg
) == SSA_NAME
1084 && SSA_NAME_IS_DEFAULT_DEF (arg
)
1085 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
1086 && cfun
->after_inlining
)
1088 vr
->set_null (type
);
1092 /* Both __builtin_ffs* and __builtin_popcount return
1096 arg
= gimple_call_arg (stmt
, 0);
1097 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1100 if (TREE_CODE (arg
) == SSA_NAME
)
1102 value_range
*vr0
= get_value_range (arg
);
1103 /* If arg is non-zero, then ffs or popcount are non-zero. */
1104 if (range_includes_zero_p (vr0
) == 0)
1106 /* If some high bits are known to be zero,
1107 we can decrease the maximum. */
1108 if (vr0
->kind () == VR_RANGE
1109 && TREE_CODE (vr0
->max ()) == INTEGER_CST
1110 && !operand_less_p (vr0
->min (),
1111 build_zero_cst (TREE_TYPE (vr0
->min ()))))
1112 maxi
= tree_floor_log2 (vr0
->max ()) + 1;
1115 /* __builtin_parity* returns [0, 1]. */
1120 /* __builtin_c[lt]z* return [0, prec-1], except for
1121 when the argument is 0, but that is undefined behavior.
1122 On many targets where the CLZ RTL or optab value is defined
1123 for 0 the value is prec, so include that in the range
1126 arg
= gimple_call_arg (stmt
, 0);
1127 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1130 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1131 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
1132 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
)
1133 /* Handle only the single common value. */
1135 /* Magic value to give up, unless vr0 proves
1138 if (TREE_CODE (arg
) == SSA_NAME
)
1140 value_range
*vr0
= get_value_range (arg
);
1141 /* From clz of VR_RANGE minimum we can compute
1143 if (vr0
->kind () == VR_RANGE
1144 && TREE_CODE (vr0
->min ()) == INTEGER_CST
)
1146 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min ());
1150 else if (vr0
->kind () == VR_ANTI_RANGE
1151 && integer_zerop (vr0
->min ()))
1158 /* From clz of VR_RANGE maximum we can compute
1160 if (vr0
->kind () == VR_RANGE
1161 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1163 mini
= prec
- 1 - tree_floor_log2 (vr0
->max ());
1171 /* __builtin_ctz* return [0, prec-1], except for
1172 when the argument is 0, but that is undefined behavior.
1173 If there is a ctz optab for this mode and
1174 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1175 otherwise just assume 0 won't be seen. */
1177 arg
= gimple_call_arg (stmt
, 0);
1178 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1181 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1182 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
1183 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
))
1185 /* Handle only the two common values. */
1188 else if (zerov
== prec
)
1191 /* Magic value to give up, unless vr0 proves
1195 if (TREE_CODE (arg
) == SSA_NAME
)
1197 value_range
*vr0
= get_value_range (arg
);
1198 /* If arg is non-zero, then use [0, prec - 1]. */
1199 if ((vr0
->kind () == VR_RANGE
1200 && integer_nonzerop (vr0
->min ()))
1201 || (vr0
->kind () == VR_ANTI_RANGE
1202 && integer_zerop (vr0
->min ())))
1207 /* If some high bits are known to be zero,
1208 we can decrease the result maximum. */
1209 if (vr0
->kind () == VR_RANGE
1210 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1212 maxi
= tree_floor_log2 (vr0
->max ());
1213 /* For vr0 [0, 0] give up. */
1221 /* __builtin_clrsb* returns [0, prec-1]. */
1223 arg
= gimple_call_arg (stmt
, 0);
1224 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1229 vr
->set (VR_RANGE
, build_int_cst (type
, mini
),
1230 build_int_cst (type
, maxi
));
1232 case CFN_UBSAN_CHECK_ADD
:
1233 subcode
= PLUS_EXPR
;
1235 case CFN_UBSAN_CHECK_SUB
:
1236 subcode
= MINUS_EXPR
;
1238 case CFN_UBSAN_CHECK_MUL
:
1239 subcode
= MULT_EXPR
;
1241 case CFN_GOACC_DIM_SIZE
:
1242 case CFN_GOACC_DIM_POS
:
1243 /* Optimizing these two internal functions helps the loop
1244 optimizer eliminate outer comparisons. Size is [1,N]
1245 and pos is [0,N-1]. */
1247 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
1248 int axis
= oacc_get_ifn_dim_arg (stmt
);
1249 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1252 /* If it's dynamic, the backend might know a hardware
1254 size
= targetm
.goacc
.dim_limit (axis
);
1256 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
1257 vr
->set(VR_RANGE
, build_int_cst (type
, is_pos
? 0 : 1),
1259 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1262 case CFN_BUILT_IN_STRLEN
:
1263 if (tree lhs
= gimple_call_lhs (stmt
))
1264 if (ptrdiff_type_node
1265 && (TYPE_PRECISION (ptrdiff_type_node
)
1266 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1268 tree type
= TREE_TYPE (lhs
);
1269 tree max
= vrp_val_max (ptrdiff_type_node
);
1270 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1271 tree range_min
= build_zero_cst (type
);
1272 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
1273 vr
->set (VR_RANGE
, range_min
, range_max
);
1280 if (subcode
!= ERROR_MARK
)
1282 bool saved_flag_wrapv
= flag_wrapv
;
1283 /* Pretend the arithmetics is wrapping. If there is
1284 any overflow, we'll complain, but will actually do
1285 wrapping operation. */
1287 extract_range_from_binary_expr (vr
, subcode
, type
,
1288 gimple_call_arg (stmt
, 0),
1289 gimple_call_arg (stmt
, 1));
1290 flag_wrapv
= saved_flag_wrapv
;
1292 /* If for both arguments vrp_valueize returned non-NULL,
1293 this should have been already folded and if not, it
1294 wasn't folded because of overflow. Avoid removing the
1295 UBSAN_CHECK_* calls in that case. */
1296 if (vr
->kind () == VR_RANGE
1297 && (vr
->min () == vr
->max ()
1298 || operand_equal_p (vr
->min (), vr
->max (), 0)))
1303 /* Handle extraction of the two results (result of arithmetics and
1304 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1305 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1306 else if (is_gimple_assign (stmt
)
1307 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1308 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1309 && INTEGRAL_TYPE_P (type
))
1311 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1312 tree op
= gimple_assign_rhs1 (stmt
);
1313 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
1315 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
1316 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
1318 enum tree_code subcode
= ERROR_MARK
;
1319 switch (gimple_call_internal_fn (g
))
1321 case IFN_ADD_OVERFLOW
:
1322 subcode
= PLUS_EXPR
;
1324 case IFN_SUB_OVERFLOW
:
1325 subcode
= MINUS_EXPR
;
1327 case IFN_MUL_OVERFLOW
:
1328 subcode
= MULT_EXPR
;
1330 case IFN_ATOMIC_COMPARE_EXCHANGE
:
1331 if (code
== IMAGPART_EXPR
)
1333 /* This is the boolean return value whether compare and
1334 exchange changed anything or not. */
1335 vr
->set (VR_RANGE
, build_int_cst (type
, 0),
1336 build_int_cst (type
, 1));
1343 if (subcode
!= ERROR_MARK
)
1345 tree op0
= gimple_call_arg (g
, 0);
1346 tree op1
= gimple_call_arg (g
, 1);
1347 if (code
== IMAGPART_EXPR
)
1350 if (check_for_binary_op_overflow (subcode
, type
,
1352 vr
->set (build_int_cst (type
, ovf
));
1353 else if (TYPE_PRECISION (type
) == 1
1354 && !TYPE_UNSIGNED (type
))
1357 vr
->set (VR_RANGE
, build_int_cst (type
, 0),
1358 build_int_cst (type
, 1));
1360 else if (types_compatible_p (type
, TREE_TYPE (op0
))
1361 && types_compatible_p (type
, TREE_TYPE (op1
)))
1363 bool saved_flag_wrapv
= flag_wrapv
;
1364 /* Pretend the arithmetics is wrapping. If there is
1365 any overflow, IMAGPART_EXPR will be set. */
1367 extract_range_from_binary_expr (vr
, subcode
, type
,
1369 flag_wrapv
= saved_flag_wrapv
;
1373 value_range vr0
, vr1
;
1374 bool saved_flag_wrapv
= flag_wrapv
;
1375 /* Pretend the arithmetics is wrapping. If there is
1376 any overflow, IMAGPART_EXPR will be set. */
1378 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
1380 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
1382 ::extract_range_from_binary_expr (vr
, subcode
, type
,
1384 flag_wrapv
= saved_flag_wrapv
;
1391 if (INTEGRAL_TYPE_P (type
)
1392 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
1393 set_value_range_to_nonnegative (vr
, type
);
1394 else if (vrp_stmt_computes_nonzero (stmt
))
1395 vr
->set_nonnull (type
);
1401 /* Try to compute a useful range out of assignment STMT and store it
1405 vr_values::extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
1407 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1409 if (code
== ASSERT_EXPR
)
1410 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
1411 else if (code
== SSA_NAME
)
1412 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
1413 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
1414 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
1415 gimple_expr_type (stmt
),
1416 gimple_assign_rhs1 (stmt
),
1417 gimple_assign_rhs2 (stmt
));
1418 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
1419 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
1420 gimple_expr_type (stmt
),
1421 gimple_assign_rhs1 (stmt
));
1422 else if (code
== COND_EXPR
)
1423 extract_range_from_cond_expr (vr
, stmt
);
1424 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1425 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
1426 gimple_expr_type (stmt
),
1427 gimple_assign_rhs1 (stmt
),
1428 gimple_assign_rhs2 (stmt
));
1429 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
1430 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
1431 vr
->set (gimple_assign_rhs1 (stmt
));
1435 if (vr
->varying_p ())
1436 extract_range_basic (vr
, stmt
);
1439 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1441 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1442 all the values in the ranges.
1444 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1446 - Return NULL_TREE if it is not always possible to determine the
1447 value of the comparison.
1449 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1450 assumed signed overflow is undefined. */
1454 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
1455 bool *strict_overflow_p
)
1457 /* VARYING or UNDEFINED ranges cannot be compared. */
1458 if (vr0
->varying_p ()
1459 || vr0
->undefined_p ()
1460 || vr1
->varying_p ()
1461 || vr1
->undefined_p ())
1464 /* Anti-ranges need to be handled separately. */
1465 if (vr0
->kind () == VR_ANTI_RANGE
|| vr1
->kind () == VR_ANTI_RANGE
)
1467 /* If both are anti-ranges, then we cannot compute any
1469 if (vr0
->kind () == VR_ANTI_RANGE
&& vr1
->kind () == VR_ANTI_RANGE
)
1472 /* These comparisons are never statically computable. */
1479 /* Equality can be computed only between a range and an
1480 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1481 if (vr0
->kind () == VR_RANGE
)
1483 /* To simplify processing, make VR0 the anti-range. */
1484 value_range
*tmp
= vr0
;
1489 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
1491 if (compare_values_warnv (vr0
->min (), vr1
->min (), strict_overflow_p
) == 0
1492 && compare_values_warnv (vr0
->max (), vr1
->max (), strict_overflow_p
) == 0)
1493 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1498 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1499 operands around and change the comparison code. */
1500 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1502 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
1503 std::swap (vr0
, vr1
);
1506 if (comp
== EQ_EXPR
)
1508 /* Equality may only be computed if both ranges represent
1509 exactly one value. */
1510 if (compare_values_warnv (vr0
->min (), vr0
->max (), strict_overflow_p
) == 0
1511 && compare_values_warnv (vr1
->min (), vr1
->max (), strict_overflow_p
) == 0)
1513 int cmp_min
= compare_values_warnv (vr0
->min (), vr1
->min (),
1515 int cmp_max
= compare_values_warnv (vr0
->max (), vr1
->max (),
1517 if (cmp_min
== 0 && cmp_max
== 0)
1518 return boolean_true_node
;
1519 else if (cmp_min
!= -2 && cmp_max
!= -2)
1520 return boolean_false_node
;
1522 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1523 else if (compare_values_warnv (vr0
->min (), vr1
->max (),
1524 strict_overflow_p
) == 1
1525 || compare_values_warnv (vr1
->min (), vr0
->max (),
1526 strict_overflow_p
) == 1)
1527 return boolean_false_node
;
1531 else if (comp
== NE_EXPR
)
1535 /* If VR0 is completely to the left or completely to the right
1536 of VR1, they are always different. Notice that we need to
1537 make sure that both comparisons yield similar results to
1538 avoid comparing values that cannot be compared at
1540 cmp1
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1541 cmp2
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1542 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
1543 return boolean_true_node
;
1545 /* If VR0 and VR1 represent a single value and are identical,
1547 else if (compare_values_warnv (vr0
->min (), vr0
->max (),
1548 strict_overflow_p
) == 0
1549 && compare_values_warnv (vr1
->min (), vr1
->max (),
1550 strict_overflow_p
) == 0
1551 && compare_values_warnv (vr0
->min (), vr1
->min (),
1552 strict_overflow_p
) == 0
1553 && compare_values_warnv (vr0
->max (), vr1
->max (),
1554 strict_overflow_p
) == 0)
1555 return boolean_false_node
;
1557 /* Otherwise, they may or may not be different. */
1561 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1565 /* If VR0 is to the left of VR1, return true. */
1566 tst
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1567 if ((comp
== LT_EXPR
&& tst
== -1)
1568 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1569 return boolean_true_node
;
1571 /* If VR0 is to the right of VR1, return false. */
1572 tst
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1573 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1574 || (comp
== LE_EXPR
&& tst
== 1))
1575 return boolean_false_node
;
1577 /* Otherwise, we don't know. */
1584 /* Given a value range VR, a value VAL and a comparison code COMP, return
1585 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1586 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1587 always returns false. Return NULL_TREE if it is not always
1588 possible to determine the value of the comparison. Also set
1589 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1590 assumed signed overflow is undefined. */
1593 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
1594 bool *strict_overflow_p
)
1596 if (vr
->varying_p () || vr
->undefined_p ())
1599 /* Anti-ranges need to be handled separately. */
1600 if (vr
->kind () == VR_ANTI_RANGE
)
1602 /* For anti-ranges, the only predicates that we can compute at
1603 compile time are equality and inequality. */
1610 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1611 if (value_inside_range (val
, vr
->min (), vr
->max ()) == 1)
1612 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1617 if (comp
== EQ_EXPR
)
1619 /* EQ_EXPR may only be computed if VR represents exactly
1621 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0)
1623 int cmp
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1625 return boolean_true_node
;
1626 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
1627 return boolean_false_node
;
1629 else if (compare_values_warnv (val
, vr
->min (), strict_overflow_p
) == -1
1630 || compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1)
1631 return boolean_false_node
;
1635 else if (comp
== NE_EXPR
)
1637 /* If VAL is not inside VR, then they are always different. */
1638 if (compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1
1639 || compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 1)
1640 return boolean_true_node
;
1642 /* If VR represents exactly one value equal to VAL, then return
1644 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0
1645 && compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 0)
1646 return boolean_false_node
;
1648 /* Otherwise, they may or may not be different. */
1651 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1655 /* If VR is to the left of VAL, return true. */
1656 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1657 if ((comp
== LT_EXPR
&& tst
== -1)
1658 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1659 return boolean_true_node
;
1661 /* If VR is to the right of VAL, return false. */
1662 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1663 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1664 || (comp
== LE_EXPR
&& tst
== 1))
1665 return boolean_false_node
;
1667 /* Otherwise, we don't know. */
1670 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1674 /* If VR is to the right of VAL, return true. */
1675 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1676 if ((comp
== GT_EXPR
&& tst
== 1)
1677 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
1678 return boolean_true_node
;
1680 /* If VR is to the left of VAL, return false. */
1681 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1682 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
1683 || (comp
== GE_EXPR
&& tst
== -1))
1684 return boolean_false_node
;
1686 /* Otherwise, we don't know. */
1692 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1693 would be profitable to adjust VR using scalar evolution information
1694 for VAR. If so, update VR with the new limits. */
1697 vr_values::adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
1698 gimple
*stmt
, tree var
)
1700 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
1701 enum ev_direction dir
;
1703 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1704 better opportunities than a regular range, but I'm not sure. */
1705 if (vr
->kind () == VR_ANTI_RANGE
)
1708 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
1710 /* Like in PR19590, scev can return a constant function. */
1711 if (is_gimple_min_invariant (chrec
))
1717 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1720 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
1721 tem
= op_with_constant_singleton_value_range (init
);
1724 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
1725 tem
= op_with_constant_singleton_value_range (step
);
1729 /* If STEP is symbolic, we can't know whether INIT will be the
1730 minimum or maximum value in the range. Also, unless INIT is
1731 a simple expression, compare_values and possibly other functions
1732 in tree-vrp won't be able to handle it. */
1733 if (step
== NULL_TREE
1734 || !is_gimple_min_invariant (step
)
1735 || !valid_value_p (init
))
1738 dir
= scev_direction (chrec
);
1739 if (/* Do not adjust ranges if we do not know whether the iv increases
1740 or decreases, ... */
1741 dir
== EV_DIR_UNKNOWN
1742 /* ... or if it may wrap. */
1743 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
1744 get_chrec_loop (chrec
), true))
1747 type
= TREE_TYPE (var
);
1748 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1749 tmin
= lower_bound_in_type (type
, type
);
1751 tmin
= TYPE_MIN_VALUE (type
);
1752 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1753 tmax
= upper_bound_in_type (type
, type
);
1755 tmax
= TYPE_MAX_VALUE (type
);
1757 /* Try to use estimated number of iterations for the loop to constrain the
1758 final value in the evolution. */
1759 if (TREE_CODE (step
) == INTEGER_CST
1760 && is_gimple_val (init
)
1761 && (TREE_CODE (init
) != SSA_NAME
1762 || get_value_range (init
)->kind () == VR_RANGE
))
1766 /* We are only entering here for loop header PHI nodes, so using
1767 the number of latch executions is the correct thing to use. */
1768 if (max_loop_iterations (loop
, &nit
))
1771 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
1772 wi::overflow_type overflow
;
1774 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
1776 /* If the multiplication overflowed we can't do a meaningful
1777 adjustment. Likewise if the result doesn't fit in the type
1778 of the induction variable. For a signed type we have to
1779 check whether the result has the expected signedness which
1780 is that of the step as number of iterations is unsigned. */
1782 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
1784 || wi::gts_p (wtmp
, 0) == wi::gts_p (wi::to_wide (step
), 0)))
1786 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
1787 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
1788 TREE_TYPE (init
), init
, tem
);
1789 /* Likewise if the addition did. */
1790 if (maxvr
.kind () == VR_RANGE
)
1792 value_range_base initvr
;
1794 if (TREE_CODE (init
) == SSA_NAME
)
1795 initvr
= *(get_value_range (init
));
1796 else if (is_gimple_min_invariant (init
))
1801 /* Check if init + nit * step overflows. Though we checked
1802 scev {init, step}_loop doesn't wrap, it is not enough
1803 because the loop may exit immediately. Overflow could
1804 happen in the plus expression in this case. */
1805 if ((dir
== EV_DIR_DECREASES
1806 && compare_values (maxvr
.min (), initvr
.min ()) != -1)
1807 || (dir
== EV_DIR_GROWS
1808 && compare_values (maxvr
.max (), initvr
.max ()) != 1))
1811 tmin
= maxvr
.min ();
1812 tmax
= maxvr
.max ();
1818 if (vr
->varying_p () || vr
->undefined_p ())
1823 /* For VARYING or UNDEFINED ranges, just about anything we get
1824 from scalar evolutions should be better. */
1826 if (dir
== EV_DIR_DECREASES
)
1831 else if (vr
->kind () == VR_RANGE
)
1836 if (dir
== EV_DIR_DECREASES
)
1838 /* INIT is the maximum value. If INIT is lower than VR->MAX ()
1839 but no smaller than VR->MIN (), set VR->MAX () to INIT. */
1840 if (compare_values (init
, max
) == -1)
1843 /* According to the loop information, the variable does not
1845 if (compare_values (min
, tmin
) == -1)
1851 /* If INIT is bigger than VR->MIN (), set VR->MIN () to INIT. */
1852 if (compare_values (init
, min
) == 1)
1855 if (compare_values (tmax
, max
) == -1)
1862 /* If we just created an invalid range with the minimum
1863 greater than the maximum, we fail conservatively.
1864 This should happen only in unreachable
1865 parts of code, or for invalid programs. */
1866 if (compare_values (min
, max
) == 1)
1869 /* Even for valid range info, sometimes overflow flag will leak in.
1870 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1872 if (TREE_OVERFLOW_P (min
))
1873 min
= drop_tree_overflow (min
);
1874 if (TREE_OVERFLOW_P (max
))
1875 max
= drop_tree_overflow (max
);
1877 vr
->update (VR_RANGE
, min
, max
);
1880 /* Dump value ranges of all SSA_NAMEs to FILE. */
1883 vr_values::dump_all_value_ranges (FILE *file
)
1887 for (i
= 0; i
< num_vr_values
; i
++)
1891 print_generic_expr (file
, ssa_name (i
));
1892 fprintf (file
, ": ");
1893 dump_value_range (file
, vr_value
[i
]);
1894 fprintf (file
, "\n");
1898 fprintf (file
, "\n");
1901 /* Initialize VRP lattice. */
1903 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1905 values_propagated
= false;
1906 num_vr_values
= num_ssa_names
;
1907 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
1908 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
1909 bitmap_obstack_initialize (&vrp_equiv_obstack
);
1910 to_remove_edges
= vNULL
;
1911 to_update_switch_stmts
= vNULL
;
1914 /* Free VRP lattice. */
1916 vr_values::~vr_values ()
1918 /* Free allocated memory. */
1920 free (vr_phi_edge_counts
);
1921 bitmap_obstack_release (&vrp_equiv_obstack
);
1922 vrp_value_range_pool
.release ();
1924 /* So that we can distinguish between VRP data being available
1925 and not available. */
1927 vr_phi_edge_counts
= NULL
;
1929 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1930 then an EVRP client did not clean up properly. Catch it now rather
1931 than seeing something more obscure later. */
1932 gcc_assert (to_remove_edges
.is_empty ()
1933 && to_update_switch_stmts
.is_empty ());
1938 static class vr_values
*x_vr_values
;
1940 /* Return the singleton value-range for NAME or NAME. */
1943 vrp_valueize (tree name
)
1945 if (TREE_CODE (name
) == SSA_NAME
)
1947 value_range
*vr
= x_vr_values
->get_value_range (name
);
1948 if (vr
->kind () == VR_RANGE
1949 && (TREE_CODE (vr
->min ()) == SSA_NAME
1950 || is_gimple_min_invariant (vr
->min ()))
1951 && vrp_operand_equal_p (vr
->min (), vr
->max ()))
1957 /* Return the singleton value-range for NAME if that is a constant
1958 but signal to not follow SSA edges. */
1961 vrp_valueize_1 (tree name
)
1963 if (TREE_CODE (name
) == SSA_NAME
)
1965 /* If the definition may be simulated again we cannot follow
1966 this SSA edge as the SSA propagator does not necessarily
1967 re-visit the use. */
1968 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1969 if (!gimple_nop_p (def_stmt
)
1970 && prop_simulate_again_p (def_stmt
))
1972 value_range
*vr
= x_vr_values
->get_value_range (name
);
1974 if (vr
->singleton_p (&singleton
))
1980 /* Given STMT, an assignment or call, return its LHS if the type
1981 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
1984 get_output_for_vrp (gimple
*stmt
)
1986 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
1989 /* We only keep track of ranges in integral and pointer types. */
1990 tree lhs
= gimple_get_lhs (stmt
);
1991 if (TREE_CODE (lhs
) == SSA_NAME
1992 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
1993 /* It is valid to have NULL MIN/MAX values on a type. See
1994 build_range_type. */
1995 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
1996 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
1997 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
2003 /* Visit assignment STMT. If it produces an interesting range, record
2004 the range in VR and set LHS to OUTPUT_P. */
2007 vr_values::vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
,
2010 tree lhs
= get_output_for_vrp (stmt
);
2013 /* We only keep track of ranges in integral and pointer types. */
2016 enum gimple_code code
= gimple_code (stmt
);
2018 /* Try folding the statement to a constant first. */
2020 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
2025 if (TREE_CODE (tem
) == SSA_NAME
2026 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
2027 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
2029 extract_range_from_ssa_name (vr
, tem
);
2032 else if (is_gimple_min_invariant (tem
))
2038 /* Then dispatch to value-range extracting functions. */
2039 if (code
== GIMPLE_CALL
)
2040 extract_range_basic (vr
, stmt
);
2042 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
2046 /* Helper that gets the value range of the SSA_NAME with version I
2047 or a symbolic range containing the SSA_NAME only if the value range
2048 is varying or undefined. Uses TEM as storage for the alternate range. */
2051 vr_values::get_vr_for_comparison (int i
, value_range
*tem
)
2053 /* Shallow-copy equiv bitmap. */
2054 value_range
*vr
= get_value_range (ssa_name (i
));
2056 /* If name N_i does not have a valid range, use N_i as its own
2057 range. This allows us to compare against names that may
2058 have N_i in their ranges. */
2059 if (vr
->varying_p () || vr
->undefined_p ())
2061 tem
->set (ssa_name (i
));
2068 /* Compare all the value ranges for names equivalent to VAR with VAL
2069 using comparison code COMP. Return the same value returned by
2070 compare_range_with_value, including the setting of
2071 *STRICT_OVERFLOW_P. */
2074 vr_values::compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
2075 bool *strict_overflow_p
, bool use_equiv_p
)
2081 int used_strict_overflow
;
2083 value_range
*equiv_vr
, tem_vr
;
2085 /* Get the set of equivalences for VAR. */
2086 e
= get_value_range (var
)->equiv ();
2088 /* Start at -1. Set it to 0 if we do a comparison without relying
2089 on overflow, or 1 if all comparisons rely on overflow. */
2090 used_strict_overflow
= -1;
2092 /* Compare vars' value range with val. */
2093 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
), &tem_vr
);
2095 retval
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2097 used_strict_overflow
= sop
? 1 : 0;
2099 /* If the equiv set is empty we have done all work we need to do. */
2103 && used_strict_overflow
> 0)
2104 *strict_overflow_p
= true;
2108 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
2110 tree name
= ssa_name (i
);
2115 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
2116 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
2119 equiv_vr
= get_vr_for_comparison (i
, &tem_vr
);
2121 t
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2124 /* If we get different answers from different members
2125 of the equivalence set this check must be in a dead
2126 code region. Folding it to a trap representation
2127 would be correct here. For now just return don't-know. */
2137 used_strict_overflow
= 0;
2138 else if (used_strict_overflow
< 0)
2139 used_strict_overflow
= 1;
2144 && used_strict_overflow
> 0)
2145 *strict_overflow_p
= true;
2151 /* Given a comparison code COMP and names N1 and N2, compare all the
2152 ranges equivalent to N1 against all the ranges equivalent to N2
2153 to determine the value of N1 COMP N2. Return the same value
2154 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2155 whether we relied on undefined signed overflow in the comparison. */
2159 vr_values::compare_names (enum tree_code comp
, tree n1
, tree n2
,
2160 bool *strict_overflow_p
)
2164 bitmap_iterator bi1
, bi2
;
2166 int used_strict_overflow
;
2167 static bitmap_obstack
*s_obstack
= NULL
;
2168 static bitmap s_e1
= NULL
, s_e2
= NULL
;
2170 /* Compare the ranges of every name equivalent to N1 against the
2171 ranges of every name equivalent to N2. */
2172 e1
= get_value_range (n1
)->equiv ();
2173 e2
= get_value_range (n2
)->equiv ();
2175 /* Use the fake bitmaps if e1 or e2 are not available. */
2176 if (s_obstack
== NULL
)
2178 s_obstack
= XNEW (bitmap_obstack
);
2179 bitmap_obstack_initialize (s_obstack
);
2180 s_e1
= BITMAP_ALLOC (s_obstack
);
2181 s_e2
= BITMAP_ALLOC (s_obstack
);
2188 /* Add N1 and N2 to their own set of equivalences to avoid
2189 duplicating the body of the loop just to check N1 and N2
2191 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
2192 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
2194 /* If the equivalence sets have a common intersection, then the two
2195 names can be compared without checking their ranges. */
2196 if (bitmap_intersect_p (e1
, e2
))
2198 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2199 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2201 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
2203 : boolean_false_node
;
2206 /* Start at -1. Set it to 0 if we do a comparison without relying
2207 on overflow, or 1 if all comparisons rely on overflow. */
2208 used_strict_overflow
= -1;
2210 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2211 N2 to their own set of equivalences to avoid duplicating the body
2212 of the loop just to check N1 and N2 ranges. */
2213 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
2215 if (! ssa_name (i1
))
2218 value_range tem_vr1
;
2219 value_range
*vr1
= get_vr_for_comparison (i1
, &tem_vr1
);
2221 t
= retval
= NULL_TREE
;
2222 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
2224 if (! ssa_name (i2
))
2229 value_range tem_vr2
;
2230 value_range
*vr2
= get_vr_for_comparison (i2
, &tem_vr2
);
2232 t
= compare_ranges (comp
, vr1
, vr2
, &sop
);
2235 /* If we get different answers from different members
2236 of the equivalence set this check must be in a dead
2237 code region. Folding it to a trap representation
2238 would be correct here. For now just return don't-know. */
2242 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2243 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2249 used_strict_overflow
= 0;
2250 else if (used_strict_overflow
< 0)
2251 used_strict_overflow
= 1;
2257 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2258 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2259 if (used_strict_overflow
> 0)
2260 *strict_overflow_p
= true;
2265 /* None of the equivalent ranges are useful in computing this
2267 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2268 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2272 /* Helper function for vrp_evaluate_conditional_warnv & other
2276 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2277 (enum tree_code code
, tree op0
, tree op1
, bool * strict_overflow_p
)
2279 value_range
*vr0
, *vr1
;
2281 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
2282 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
2284 tree res
= NULL_TREE
;
2286 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
2288 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
2290 res
= (compare_range_with_value
2291 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
2295 /* Helper function for vrp_evaluate_conditional_warnv. */
2298 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
,
2301 bool *strict_overflow_p
,
2306 *only_ranges
= true;
2308 /* We only deal with integral and pointer types. */
2309 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2310 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
2313 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2314 as a simple equality test, then prefer that over its current form
2317 An overflow test which collapses to an equality test can always be
2318 expressed as a comparison of one argument against zero. Overflow
2319 occurs when the chosen argument is zero and does not occur if the
2320 chosen argument is not zero. */
2322 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
2324 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
2325 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2326 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2327 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2328 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2329 if (integer_zerop (x
))
2332 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2334 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2335 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2336 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2337 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2338 else if (wi::to_wide (x
) == max
- 1)
2341 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
2342 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2346 value_range vro
, vri
;
2347 if (code
== GT_EXPR
|| code
== GE_EXPR
)
2349 vro
.set (VR_ANTI_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2350 vri
.set (VR_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2352 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
2354 vro
.set (VR_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2355 vri
.set (VR_ANTI_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2359 value_range
*vr0
= get_value_range (op0
);
2360 /* If vro, the range for OP0 to pass the overflow test, has
2361 no intersection with *vr0, OP0's known range, then the
2362 overflow test can't pass, so return the node for false.
2363 If it is the inverted range, vri, that has no
2364 intersection, then the overflow test must pass, so return
2365 the node for true. In other cases, we could proceed with
2366 a simplified condition comparing OP0 and X, with LE_EXPR
2367 for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
2368 the comments next to the enclosing if suggest it's not
2369 generally profitable to do so. */
2370 vro
.intersect (vr0
);
2371 if (vro
.undefined_p ())
2372 return boolean_false_node
;
2373 vri
.intersect (vr0
);
2374 if (vri
.undefined_p ())
2375 return boolean_true_node
;
2379 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
2380 (code
, op0
, op1
, strict_overflow_p
)))
2383 *only_ranges
= false;
2384 /* Do not use compare_names during propagation, it's quadratic. */
2385 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
2387 return compare_names (code
, op0
, op1
, strict_overflow_p
);
2388 else if (TREE_CODE (op0
) == SSA_NAME
)
2389 return compare_name_with_value (code
, op0
, op1
,
2390 strict_overflow_p
, use_equiv_p
);
2391 else if (TREE_CODE (op1
) == SSA_NAME
)
2392 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
2393 strict_overflow_p
, use_equiv_p
);
2397 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2398 information. Return NULL if the conditional cannot be evaluated.
2399 The ranges of all the names equivalent with the operands in COND
2400 will be used when trying to compute the value. If the result is
2401 based on undefined signed overflow, issue a warning if
2405 vr_values::vrp_evaluate_conditional (tree_code code
, tree op0
,
2406 tree op1
, gimple
*stmt
)
2412 /* Some passes and foldings leak constants with overflow flag set
2413 into the IL. Avoid doing wrong things with these and bail out. */
2414 if ((TREE_CODE (op0
) == INTEGER_CST
2415 && TREE_OVERFLOW (op0
))
2416 || (TREE_CODE (op1
) == INTEGER_CST
2417 && TREE_OVERFLOW (op1
)))
2421 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
2426 enum warn_strict_overflow_code wc
;
2427 const char* warnmsg
;
2429 if (is_gimple_min_invariant (ret
))
2431 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
2432 warnmsg
= G_("assuming signed overflow does not occur when "
2433 "simplifying conditional to constant");
2437 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
2438 warnmsg
= G_("assuming signed overflow does not occur when "
2439 "simplifying conditional");
2442 if (issue_strict_overflow_warning (wc
))
2444 location_t location
;
2446 if (!gimple_has_location (stmt
))
2447 location
= input_location
;
2449 location
= gimple_location (stmt
);
2450 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
2454 if (warn_type_limits
2455 && ret
&& only_ranges
2456 && TREE_CODE_CLASS (code
) == tcc_comparison
2457 && TREE_CODE (op0
) == SSA_NAME
)
2459 /* If the comparison is being folded and the operand on the LHS
2460 is being compared against a constant value that is outside of
2461 the natural range of OP0's type, then the predicate will
2462 always fold regardless of the value of OP0. If -Wtype-limits
2463 was specified, emit a warning. */
2464 tree type
= TREE_TYPE (op0
);
2465 value_range
*vr0
= get_value_range (op0
);
2467 if (vr0
->kind () == VR_RANGE
2468 && INTEGRAL_TYPE_P (type
)
2469 && vrp_val_is_min (vr0
->min ())
2470 && vrp_val_is_max (vr0
->max ())
2471 && is_gimple_min_invariant (op1
))
2473 location_t location
;
2475 if (!gimple_has_location (stmt
))
2476 location
= input_location
;
2478 location
= gimple_location (stmt
);
2480 warning_at (location
, OPT_Wtype_limits
,
2482 ? G_("comparison always false "
2483 "due to limited range of data type")
2484 : G_("comparison always true "
2485 "due to limited range of data type"));
2493 /* Visit conditional statement STMT. If we can determine which edge
2494 will be taken out of STMT's basic block, record it in
2495 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2498 vr_values::vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
2502 *taken_edge_p
= NULL
;
2504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2509 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
2510 print_gimple_stmt (dump_file
, stmt
, 0);
2511 fprintf (dump_file
, "\nWith known ranges\n");
2513 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
2515 fprintf (dump_file
, "\t");
2516 print_generic_expr (dump_file
, use
);
2517 fprintf (dump_file
, ": ");
2518 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
2521 fprintf (dump_file
, "\n");
2524 /* Compute the value of the predicate COND by checking the known
2525 ranges of each of its operands.
2527 Note that we cannot evaluate all the equivalent ranges here
2528 because those ranges may not yet be final and with the current
2529 propagation strategy, we cannot determine when the value ranges
2530 of the names in the equivalence set have changed.
2532 For instance, given the following code fragment
2536 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2540 Assume that on the first visit to i_14, i_5 has the temporary
2541 range [8, 8] because the second argument to the PHI function is
2542 not yet executable. We derive the range ~[0, 0] for i_14 and the
2543 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2544 the first time, since i_14 is equivalent to the range [8, 8], we
2545 determine that the predicate is always false.
2547 On the next round of propagation, i_13 is determined to be
2548 VARYING, which causes i_5 to drop down to VARYING. So, another
2549 visit to i_14 is scheduled. In this second visit, we compute the
2550 exact same range and equivalence set for i_14, namely ~[0, 0] and
2551 { i_5 }. But we did not have the previous range for i_5
2552 registered, so vrp_visit_assignment thinks that the range for
2553 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2554 is not visited again, which stops propagation from visiting
2555 statements in the THEN clause of that if().
2557 To properly fix this we would need to keep the previous range
2558 value for the names in the equivalence set. This way we would've
2559 discovered that from one visit to the other i_5 changed from
2560 range [8, 8] to VR_VARYING.
2562 However, fixing this apparent limitation may not be worth the
2563 additional checking. Testing on several code bases (GCC, DLV,
2564 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2565 4 more predicates folded in SPEC. */
2568 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
2569 gimple_cond_lhs (stmt
),
2570 gimple_cond_rhs (stmt
),
2573 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
2575 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2577 fprintf (dump_file
, "\nPredicate evaluates to: ");
2578 if (val
== NULL_TREE
)
2579 fprintf (dump_file
, "DON'T KNOW\n");
2581 print_generic_stmt (dump_file
, val
);
2585 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2586 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2587 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2588 Returns true if the default label is not needed. */
2591 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
2592 size_t *max_idx1
, size_t *min_idx2
,
2596 unsigned int n
= gimple_switch_num_labels (stmt
);
2598 tree case_low
, case_high
;
2599 tree min
= vr
->min (), max
= vr
->max ();
2601 gcc_checking_assert (!vr
->varying_p () && !vr
->undefined_p ());
2603 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
2605 /* Set second range to empty. */
2609 if (vr
->kind () == VR_RANGE
)
2613 return !take_default
;
2616 /* Set first range to all case labels. */
2623 /* Make sure all the values of case labels [i , j] are contained in
2624 range [MIN, MAX]. */
2625 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
2626 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
2627 if (tree_int_cst_compare (case_low
, min
) < 0)
2629 if (case_high
!= NULL_TREE
2630 && tree_int_cst_compare (max
, case_high
) < 0)
2636 /* If the range spans case labels [i, j], the corresponding anti-range spans
2637 the labels [1, i - 1] and [j + 1, n - 1]. */
2663 /* Visit switch statement STMT. If we can determine which edge
2664 will be taken out of STMT's basic block, record it in
2665 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2668 vr_values::vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
2672 size_t i
= 0, j
= 0, k
, l
;
2675 *taken_edge_p
= NULL
;
2676 op
= gimple_switch_index (stmt
);
2677 if (TREE_CODE (op
) != SSA_NAME
)
2680 vr
= get_value_range (op
);
2681 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2683 fprintf (dump_file
, "\nVisiting switch expression with operand ");
2684 print_generic_expr (dump_file
, op
);
2685 fprintf (dump_file
, " with known range ");
2686 dump_value_range (dump_file
, vr
);
2687 fprintf (dump_file
, "\n");
2690 if (vr
->undefined_p ()
2692 || vr
->symbolic_p ())
2695 /* Find the single edge that is taken from the switch expression. */
2696 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
2698 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2702 gcc_assert (take_default
);
2703 val
= gimple_switch_default_label (stmt
);
2707 /* Check if labels with index i to j and maybe the default label
2708 are all reaching the same label. */
2710 val
= gimple_switch_label (stmt
, i
);
2712 && CASE_LABEL (gimple_switch_default_label (stmt
))
2713 != CASE_LABEL (val
))
2715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2716 fprintf (dump_file
, " not a single destination for this "
2720 for (++i
; i
<= j
; ++i
)
2722 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
2724 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2725 fprintf (dump_file
, " not a single destination for this "
2732 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
2734 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2735 fprintf (dump_file
, " not a single destination for this "
2742 *taken_edge_p
= find_edge (gimple_bb (stmt
),
2743 label_to_block (cfun
, CASE_LABEL (val
)));
2745 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2747 fprintf (dump_file
, " will take edge to ");
2748 print_generic_stmt (dump_file
, CASE_LABEL (val
));
2753 /* Evaluate statement STMT. If the statement produces a useful range,
2754 set VR and corepsponding OUTPUT_P.
2756 If STMT is a conditional branch and we can determine its truth
2757 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2760 vr_values::extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
2761 tree
*output_p
, value_range
*vr
)
2764 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2766 fprintf (dump_file
, "\nVisiting statement:\n");
2767 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2770 if (!stmt_interesting_for_vrp (stmt
))
2771 gcc_assert (stmt_ends_bb_p (stmt
));
2772 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
2773 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
2774 else if (gimple_code (stmt
) == GIMPLE_COND
)
2775 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
2776 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
2777 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
2780 /* Visit all arguments for PHI node PHI that flow through executable
2781 edges. If a valid value range can be derived from all the incoming
2782 value ranges, set a new range in VR_RESULT. */
2785 vr_values::extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
2788 tree lhs
= PHI_RESULT (phi
);
2789 value_range
*lhs_vr
= get_value_range (lhs
);
2791 int edges
, old_edges
;
2794 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2796 fprintf (dump_file
, "\nVisiting PHI node: ");
2797 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
2800 bool may_simulate_backedge_again
= false;
2802 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2804 edge e
= gimple_phi_arg_edge (phi
, i
);
2806 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2809 " Argument #%d (%d -> %d %sexecutable)\n",
2810 (int) i
, e
->src
->index
, e
->dest
->index
,
2811 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
2814 if (e
->flags
& EDGE_EXECUTABLE
)
2816 tree arg
= PHI_ARG_DEF (phi
, i
);
2817 value_range vr_arg_tem
;
2818 value_range
*vr_arg
= &vr_arg_tem
;
2822 if (TREE_CODE (arg
) == SSA_NAME
)
2824 /* See if we are eventually going to change one of the args. */
2825 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
2826 if (! gimple_nop_p (def_stmt
)
2827 && prop_simulate_again_p (def_stmt
)
2828 && e
->flags
& EDGE_DFS_BACK
)
2829 may_simulate_backedge_again
= true;
2831 value_range
*vr_arg_
= get_value_range (arg
);
2832 /* Do not allow equivalences or symbolic ranges to leak in from
2833 backedges. That creates invalid equivalencies.
2834 See PR53465 and PR54767. */
2835 if (e
->flags
& EDGE_DFS_BACK
)
2837 if (!vr_arg_
->varying_p () && !vr_arg_
->undefined_p ())
2839 vr_arg_tem
.set (vr_arg_
->kind (), vr_arg_
->min (),
2840 vr_arg_
->max (), NULL
);
2841 if (vr_arg_tem
.symbolic_p ())
2842 vr_arg_tem
.set_varying ();
2847 /* If the non-backedge arguments range is VR_VARYING then
2848 we can still try recording a simple equivalence. */
2849 else if (vr_arg_
->varying_p ())
2850 vr_arg_tem
.set (arg
);
2856 if (TREE_OVERFLOW_P (arg
))
2857 arg
= drop_tree_overflow (arg
);
2859 vr_arg_tem
.set (arg
);
2862 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2864 fprintf (dump_file
, "\t");
2865 print_generic_expr (dump_file
, arg
, dump_flags
);
2866 fprintf (dump_file
, ": ");
2867 dump_value_range (dump_file
, vr_arg
);
2868 fprintf (dump_file
, "\n");
2872 vr_result
->deep_copy (vr_arg
);
2874 vr_result
->union_ (vr_arg
);
2877 if (vr_result
->varying_p ())
2882 if (vr_result
->varying_p ())
2884 else if (vr_result
->undefined_p ())
2887 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
2888 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
2890 /* To prevent infinite iterations in the algorithm, derive ranges
2891 when the new value is slightly bigger or smaller than the
2892 previous one. We don't do this if we have seen a new executable
2893 edge; this helps us avoid an infinity for conditionals
2894 which are not in a loop. If the old value-range was VR_UNDEFINED
2895 use the updated range and iterate one more time. If we will not
2896 simulate this PHI again via the backedge allow us to iterate. */
2898 && gimple_phi_num_args (phi
) > 1
2899 && edges
== old_edges
2900 && !lhs_vr
->undefined_p ()
2901 && may_simulate_backedge_again
)
2903 /* Compare old and new ranges, fall back to varying if the
2904 values are not comparable. */
2905 int cmp_min
= compare_values (lhs_vr
->min (), vr_result
->min ());
2908 int cmp_max
= compare_values (lhs_vr
->max (), vr_result
->max ());
2912 /* For non VR_RANGE or for pointers fall back to varying if
2913 the range changed. */
2914 if ((lhs_vr
->kind () != VR_RANGE
|| vr_result
->kind () != VR_RANGE
2915 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2916 && (cmp_min
!= 0 || cmp_max
!= 0))
2919 /* If the new minimum is larger than the previous one
2920 retain the old value. If the new minimum value is smaller
2921 than the previous one and not -INF go all the way to -INF + 1.
2922 In the first case, to avoid infinite bouncing between different
2923 minimums, and in the other case to avoid iterating millions of
2924 times to reach -INF. Going to -INF + 1 also lets the following
2925 iteration compute whether there will be any overflow, at the
2926 expense of one additional iteration. */
2927 tree new_min
= vr_result
->min ();
2928 tree new_max
= vr_result
->max ();
2930 new_min
= lhs_vr
->min ();
2931 else if (cmp_min
> 0
2932 && (TREE_CODE (vr_result
->min ()) != INTEGER_CST
2933 || tree_int_cst_lt (vrp_val_min (vr_result
->type ()),
2934 vr_result
->min ())))
2935 new_min
= int_const_binop (PLUS_EXPR
,
2936 vrp_val_min (vr_result
->type ()),
2937 build_int_cst (vr_result
->type (), 1));
2939 /* Similarly for the maximum value. */
2941 new_max
= lhs_vr
->max ();
2942 else if (cmp_max
< 0
2943 && (TREE_CODE (vr_result
->max ()) != INTEGER_CST
2944 || tree_int_cst_lt (vr_result
->max (),
2945 vrp_val_max (vr_result
->type ()))))
2946 new_max
= int_const_binop (MINUS_EXPR
,
2947 vrp_val_max (vr_result
->type ()),
2948 build_int_cst (vr_result
->type (), 1));
2950 vr_result
->update (vr_result
->kind (), new_min
, new_max
);
2952 /* If we dropped either bound to +-INF then if this is a loop
2953 PHI node SCEV may known more about its value-range. */
2954 if (cmp_min
> 0 || cmp_min
< 0
2955 || cmp_max
< 0 || cmp_max
> 0)
2958 goto infinite_check
;
2964 vr_result
->set_varying ();
2967 /* If this is a loop PHI node SCEV may known more about its value-range.
2968 scev_check can be reached from two paths, one is a fall through from above
2969 "varying" label, the other is direct goto from code block which tries to
2970 avoid infinite simulation. */
2971 if (scev_initialized_p ()
2972 && (l
= loop_containing_stmt (phi
))
2973 && l
->header
== gimple_bb (phi
))
2974 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
2977 /* If we will end up with a (-INF, +INF) range, set it to
2978 VARYING. Same if the previous max value was invalid for
2979 the type and we end up with vr_result.min > vr_result.max. */
2980 if ((!vr_result
->varying_p () && !vr_result
->undefined_p ())
2981 && !((vrp_val_is_max (vr_result
->max ()) && vrp_val_is_min (vr_result
->min ()))
2982 || compare_values (vr_result
->min (), vr_result
->max ()) > 0))
2985 vr_result
->set_varying ();
2987 /* If the new range is different than the previous value, keep
2993 /* Simplify boolean operations if the source is known
2994 to be already a boolean. */
2996 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
,
2999 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3001 bool need_conversion
;
3003 /* We handle only !=/== case here. */
3004 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
3006 op0
= gimple_assign_rhs1 (stmt
);
3007 if (!op_with_boolean_value_range_p (op0
))
3010 op1
= gimple_assign_rhs2 (stmt
);
3011 if (!op_with_boolean_value_range_p (op1
))
3014 /* Reduce number of cases to handle to NE_EXPR. As there is no
3015 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
3016 if (rhs_code
== EQ_EXPR
)
3018 if (TREE_CODE (op1
) == INTEGER_CST
)
3019 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
3020 build_int_cst (TREE_TYPE (op1
), 1));
3025 lhs
= gimple_assign_lhs (stmt
);
3027 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
3029 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
3031 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
3032 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
3033 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
3036 /* For A != 0 we can substitute A itself. */
3037 if (integer_zerop (op1
))
3038 gimple_assign_set_rhs_with_ops (gsi
,
3040 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
3041 /* For A != B we substitute A ^ B. Either with conversion. */
3042 else if (need_conversion
)
3044 tree tem
= make_ssa_name (TREE_TYPE (op0
));
3046 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
3047 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
3048 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
3049 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
3050 set_range_info (tem
, VR_RANGE
,
3051 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
3052 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
3053 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
3057 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
3058 update_stmt (gsi_stmt (*gsi
));
3059 fold_stmt (gsi
, follow_single_use_edges
);
3064 /* Simplify a division or modulo operator to a right shift or bitwise and
3065 if the first operand is unsigned or is greater than zero and the second
3066 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3067 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3068 optimize it into just op0 if op0's range is known to be a subset of
3069 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3073 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
,
3076 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3078 tree op0
= gimple_assign_rhs1 (stmt
);
3079 tree op1
= gimple_assign_rhs2 (stmt
);
3080 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
3082 value_range
*vr
= NULL
;
3084 if (TREE_CODE (op0
) == INTEGER_CST
)
3091 vr
= get_value_range (op0
);
3092 if (range_int_cst_p (vr
))
3094 op0min
= vr
->min ();
3095 op0max
= vr
->max ();
3099 if (rhs_code
== TRUNC_MOD_EXPR
3100 && TREE_CODE (op1
) == SSA_NAME
)
3102 value_range
*vr1
= get_value_range (op1
);
3103 if (range_int_cst_p (vr1
))
3104 op1min
= vr1
->min ();
3106 if (rhs_code
== TRUNC_MOD_EXPR
3107 && TREE_CODE (op1min
) == INTEGER_CST
3108 && tree_int_cst_sgn (op1min
) == 1
3110 && tree_int_cst_lt (op0max
, op1min
))
3112 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
3113 || tree_int_cst_sgn (op0min
) >= 0
3114 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
3117 /* If op0 already has the range op0 % op1 has,
3118 then TRUNC_MOD_EXPR won't change anything. */
3119 gimple_assign_set_rhs_from_tree (gsi
, op0
);
3124 if (TREE_CODE (op0
) != SSA_NAME
)
3127 if (!integer_pow2p (op1
))
3129 /* X % -Y can be only optimized into X % Y either if
3130 X is not INT_MIN, or Y is not -1. Fold it now, as after
3131 remove_range_assertions the range info might be not available
3133 if (rhs_code
== TRUNC_MOD_EXPR
3134 && fold_stmt (gsi
, follow_single_use_edges
))
3139 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
3140 val
= integer_one_node
;
3145 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
3149 && integer_onep (val
)
3150 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3152 location_t location
;
3154 if (!gimple_has_location (stmt
))
3155 location
= input_location
;
3157 location
= gimple_location (stmt
);
3158 warning_at (location
, OPT_Wstrict_overflow
,
3159 "assuming signed overflow does not occur when "
3160 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3164 if (val
&& integer_onep (val
))
3168 if (rhs_code
== TRUNC_DIV_EXPR
)
3170 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
3171 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
3172 gimple_assign_set_rhs1 (stmt
, op0
);
3173 gimple_assign_set_rhs2 (stmt
, t
);
3177 t
= build_int_cst (TREE_TYPE (op1
), 1);
3178 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
3179 t
= fold_convert (TREE_TYPE (op0
), t
);
3181 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
3182 gimple_assign_set_rhs1 (stmt
, op0
);
3183 gimple_assign_set_rhs2 (stmt
, t
);
3187 fold_stmt (gsi
, follow_single_use_edges
);
3194 /* Simplify a min or max if the ranges of the two operands are
3195 disjoint. Return true if we do simplify. */
3198 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
,
3201 tree op0
= gimple_assign_rhs1 (stmt
);
3202 tree op1
= gimple_assign_rhs2 (stmt
);
3206 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3207 (LE_EXPR
, op0
, op1
, &sop
));
3211 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3212 (LT_EXPR
, op0
, op1
, &sop
));
3217 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3219 location_t location
;
3221 if (!gimple_has_location (stmt
))
3222 location
= input_location
;
3224 location
= gimple_location (stmt
);
3225 warning_at (location
, OPT_Wstrict_overflow
,
3226 "assuming signed overflow does not occur when "
3227 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3230 /* VAL == TRUE -> OP0 < or <= op1
3231 VAL == FALSE -> OP0 > or >= op1. */
3232 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
3233 == integer_zerop (val
)) ? op0
: op1
;
3234 gimple_assign_set_rhs_from_tree (gsi
, res
);
3241 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3242 ABS_EXPR. If the operand is <= 0, then simplify the
3243 ABS_EXPR into a NEGATE_EXPR. */
3246 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3248 tree op
= gimple_assign_rhs1 (stmt
);
3249 value_range
*vr
= get_value_range (op
);
3256 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
3259 /* The range is neither <= 0 nor > 0. Now see if it is
3260 either < 0 or >= 0. */
3262 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
3268 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3270 location_t location
;
3272 if (!gimple_has_location (stmt
))
3273 location
= input_location
;
3275 location
= gimple_location (stmt
);
3276 warning_at (location
, OPT_Wstrict_overflow
,
3277 "assuming signed overflow does not occur when "
3278 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3281 gimple_assign_set_rhs1 (stmt
, op
);
3282 if (integer_zerop (val
))
3283 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
3285 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
3287 fold_stmt (gsi
, follow_single_use_edges
);
3295 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3296 If all the bits that are being cleared by & are already
3297 known to be zero from VR, or all the bits that are being
3298 set by | are already known to be one from VR, the bit
3299 operation is redundant. */
3302 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
,
3305 tree op0
= gimple_assign_rhs1 (stmt
);
3306 tree op1
= gimple_assign_rhs2 (stmt
);
3307 tree op
= NULL_TREE
;
3308 value_range_base vr0
, vr1
;
3309 wide_int may_be_nonzero0
, may_be_nonzero1
;
3310 wide_int must_be_nonzero0
, must_be_nonzero1
;
3313 if (TREE_CODE (op0
) == SSA_NAME
)
3314 vr0
= *(get_value_range (op0
));
3315 else if (is_gimple_min_invariant (op0
))
3320 if (TREE_CODE (op1
) == SSA_NAME
)
3321 vr1
= *(get_value_range (op1
));
3322 else if (is_gimple_min_invariant (op1
))
3327 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
3330 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
3334 switch (gimple_assign_rhs_code (stmt
))
3337 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3343 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3351 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3357 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3368 if (op
== NULL_TREE
)
3371 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
3372 update_stmt (gsi_stmt (*gsi
));
3376 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3377 a known value range VR.
3379 If there is one and only one value which will satisfy the
3380 conditional, then return that value. Else return NULL.
3382 If signed overflow must be undefined for the value to satisfy
3383 the conditional, then set *STRICT_OVERFLOW_P to true. */
3386 test_for_singularity (enum tree_code cond_code
, tree op0
,
3387 tree op1
, value_range
*vr
)
3392 /* Extract minimum/maximum values which satisfy the conditional as it was
3394 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
3396 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
3399 if (cond_code
== LT_EXPR
)
3401 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3402 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
3403 /* Signal to compare_values_warnv this expr doesn't overflow. */
3405 TREE_NO_WARNING (max
) = 1;
3408 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
3410 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
3413 if (cond_code
== GT_EXPR
)
3415 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3416 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
3417 /* Signal to compare_values_warnv this expr doesn't overflow. */
3419 TREE_NO_WARNING (min
) = 1;
3423 /* Now refine the minimum and maximum values using any
3424 value range information we have for op0. */
3427 if (compare_values (vr
->min (), min
) == 1)
3429 if (compare_values (vr
->max (), max
) == -1)
3432 /* If the new min/max values have converged to a single value,
3433 then there is only one value which can satisfy the condition,
3434 return that value. */
3435 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
3441 /* Return whether the value range *VR fits in an integer type specified
3442 by PRECISION and UNSIGNED_P. */
3445 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
3448 unsigned src_precision
;
3452 /* We can only handle integral and pointer types. */
3453 src_type
= vr
->type ();
3454 if (!INTEGRAL_TYPE_P (src_type
)
3455 && !POINTER_TYPE_P (src_type
))
3458 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3459 and so is an identity transform. */
3460 src_precision
= TYPE_PRECISION (vr
->type ());
3461 src_sgn
= TYPE_SIGN (src_type
);
3462 if ((src_precision
< dest_precision
3463 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
3464 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
3467 /* Now we can only handle ranges with constant bounds. */
3468 if (!range_int_cst_p (vr
))
3471 /* For sign changes, the MSB of the wide_int has to be clear.
3472 An unsigned value with its MSB set cannot be represented by
3473 a signed wide_int, while a negative value cannot be represented
3474 by an unsigned wide_int. */
3475 if (src_sgn
!= dest_sgn
3476 && (wi::lts_p (wi::to_wide (vr
->min ()), 0)
3477 || wi::lts_p (wi::to_wide (vr
->max ()), 0)))
3480 /* Then we can perform the conversion on both ends and compare
3481 the result for equality. */
3482 tem
= wi::ext (wi::to_widest (vr
->min ()), dest_precision
, dest_sgn
);
3483 if (tem
!= wi::to_widest (vr
->min ()))
3485 tem
= wi::ext (wi::to_widest (vr
->max ()), dest_precision
, dest_sgn
);
3486 if (tem
!= wi::to_widest (vr
->max ()))
3492 /* Simplify a conditional using a relational operator to an equality
3493 test if the range information indicates only one value can satisfy
3494 the original conditional. */
3497 vr_values::simplify_cond_using_ranges_1 (gcond
*stmt
)
3499 tree op0
= gimple_cond_lhs (stmt
);
3500 tree op1
= gimple_cond_rhs (stmt
);
3501 enum tree_code cond_code
= gimple_cond_code (stmt
);
3503 if (cond_code
!= NE_EXPR
3504 && cond_code
!= EQ_EXPR
3505 && TREE_CODE (op0
) == SSA_NAME
3506 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3507 && is_gimple_min_invariant (op1
))
3509 value_range
*vr
= get_value_range (op0
);
3511 /* If we have range information for OP0, then we might be
3512 able to simplify this conditional. */
3513 if (vr
->kind () == VR_RANGE
)
3515 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
3520 fprintf (dump_file
, "Simplified relational ");
3521 print_gimple_stmt (dump_file
, stmt
, 0);
3522 fprintf (dump_file
, " into ");
3525 gimple_cond_set_code (stmt
, EQ_EXPR
);
3526 gimple_cond_set_lhs (stmt
, op0
);
3527 gimple_cond_set_rhs (stmt
, new_tree
);
3533 print_gimple_stmt (dump_file
, stmt
, 0);
3534 fprintf (dump_file
, "\n");
3540 /* Try again after inverting the condition. We only deal
3541 with integral types here, so no need to worry about
3542 issues with inverting FP comparisons. */
3543 new_tree
= test_for_singularity
3544 (invert_tree_comparison (cond_code
, false),
3550 fprintf (dump_file
, "Simplified relational ");
3551 print_gimple_stmt (dump_file
, stmt
, 0);
3552 fprintf (dump_file
, " into ");
3555 gimple_cond_set_code (stmt
, NE_EXPR
);
3556 gimple_cond_set_lhs (stmt
, op0
);
3557 gimple_cond_set_rhs (stmt
, new_tree
);
3563 print_gimple_stmt (dump_file
, stmt
, 0);
3564 fprintf (dump_file
, "\n");
3574 /* STMT is a conditional at the end of a basic block.
3576 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3577 was set via a type conversion, try to replace the SSA_NAME with the RHS
3578 of the type conversion. Doing so makes the conversion dead which helps
3579 subsequent passes. */
3582 vr_values::simplify_cond_using_ranges_2 (gcond
*stmt
)
3584 tree op0
= gimple_cond_lhs (stmt
);
3585 tree op1
= gimple_cond_rhs (stmt
);
3587 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3588 see if OP0 was set by a type conversion where the source of
3589 the conversion is another SSA_NAME with a range that fits
3590 into the range of OP0's type.
3592 If so, the conversion is redundant as the earlier SSA_NAME can be
3593 used for the comparison directly if we just massage the constant in the
3595 if (TREE_CODE (op0
) == SSA_NAME
3596 && TREE_CODE (op1
) == INTEGER_CST
)
3598 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
3601 if (!is_gimple_assign (def_stmt
)
3602 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3605 innerop
= gimple_assign_rhs1 (def_stmt
);
3607 if (TREE_CODE (innerop
) == SSA_NAME
3608 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
3609 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
3610 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
3612 value_range
*vr
= get_value_range (innerop
);
3614 if (range_int_cst_p (vr
)
3615 && range_fits_type_p (vr
,
3616 TYPE_PRECISION (TREE_TYPE (op0
)),
3617 TYPE_SIGN (TREE_TYPE (op0
)))
3618 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
3620 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
3621 gimple_cond_set_lhs (stmt
, innerop
);
3622 gimple_cond_set_rhs (stmt
, newconst
);
3624 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3626 fprintf (dump_file
, "Folded into: ");
3627 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3628 fprintf (dump_file
, "\n");
3635 /* Simplify a switch statement using the value range of the switch
3639 vr_values::simplify_switch_using_ranges (gswitch
*stmt
)
3641 tree op
= gimple_switch_index (stmt
);
3642 value_range
*vr
= NULL
;
3646 size_t i
= 0, j
= 0, n
, n2
;
3649 size_t k
= 1, l
= 0;
3651 if (TREE_CODE (op
) == SSA_NAME
)
3653 vr
= get_value_range (op
);
3655 /* We can only handle integer ranges. */
3656 if (vr
->varying_p ()
3657 || vr
->undefined_p ()
3658 || vr
->symbolic_p ())
3661 /* Find case label for min/max of the value range. */
3662 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
3664 else if (TREE_CODE (op
) == INTEGER_CST
)
3666 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
3680 n
= gimple_switch_num_labels (stmt
);
3682 /* We can truncate the case label ranges that partially overlap with OP's
3684 size_t min_idx
= 1, max_idx
= 0;
3686 find_case_label_range (stmt
, vr
->min (), vr
->max (), &min_idx
, &max_idx
);
3687 if (min_idx
<= max_idx
)
3689 tree min_label
= gimple_switch_label (stmt
, min_idx
);
3690 tree max_label
= gimple_switch_label (stmt
, max_idx
);
3692 /* Avoid changing the type of the case labels when truncating. */
3693 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
3694 tree vr_min
= fold_convert (case_label_type
, vr
->min ());
3695 tree vr_max
= fold_convert (case_label_type
, vr
->max ());
3697 if (vr
->kind () == VR_RANGE
)
3699 /* If OP's value range is [2,8] and the low label range is
3700 0 ... 3, truncate the label's range to 2 .. 3. */
3701 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3702 && CASE_HIGH (min_label
) != NULL_TREE
3703 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3704 CASE_LOW (min_label
) = vr_min
;
3706 /* If OP's value range is [2,8] and the high label range is
3707 7 ... 10, truncate the label's range to 7 .. 8. */
3708 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3709 && CASE_HIGH (max_label
) != NULL_TREE
3710 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3711 CASE_HIGH (max_label
) = vr_max
;
3713 else if (vr
->kind () == VR_ANTI_RANGE
)
3715 tree one_cst
= build_one_cst (case_label_type
);
3717 if (min_label
== max_label
)
3719 /* If OP's value range is ~[7,8] and the label's range is
3720 7 ... 10, truncate the label's range to 9 ... 10. */
3721 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
3722 && CASE_HIGH (min_label
) != NULL_TREE
3723 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
3724 CASE_LOW (min_label
)
3725 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3727 /* If OP's value range is ~[7,8] and the label's range is
3728 5 ... 8, truncate the label's range to 5 ... 6. */
3729 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3730 && CASE_HIGH (min_label
) != NULL_TREE
3731 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
3732 CASE_HIGH (min_label
)
3733 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3737 /* If OP's value range is ~[2,8] and the low label range is
3738 0 ... 3, truncate the label's range to 0 ... 1. */
3739 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3740 && CASE_HIGH (min_label
) != NULL_TREE
3741 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3742 CASE_HIGH (min_label
)
3743 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3745 /* If OP's value range is ~[2,8] and the high label range is
3746 7 ... 10, truncate the label's range to 9 ... 10. */
3747 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3748 && CASE_HIGH (max_label
) != NULL_TREE
3749 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3750 CASE_LOW (max_label
)
3751 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3755 /* Canonicalize singleton case ranges. */
3756 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
3757 CASE_HIGH (min_label
) = NULL_TREE
;
3758 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
3759 CASE_HIGH (max_label
) = NULL_TREE
;
3762 /* We can also eliminate case labels that lie completely outside OP's value
3765 /* Bail out if this is just all edges taken. */
3771 /* Build a new vector of taken case labels. */
3772 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
3775 /* Add the default edge, if necessary. */
3777 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
3779 for (; i
<= j
; ++i
, ++n2
)
3780 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
3782 for (; k
<= l
; ++k
, ++n2
)
3783 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
3785 /* Mark needed edges. */
3786 for (i
= 0; i
< n2
; ++i
)
3788 e
= find_edge (gimple_bb (stmt
),
3789 label_to_block (cfun
,
3790 CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
3791 e
->aux
= (void *)-1;
3794 /* Queue not needed edges for later removal. */
3795 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
3797 if (e
->aux
== (void *)-1)
3803 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3805 fprintf (dump_file
, "removing unreachable case label\n");
3807 to_remove_edges
.safe_push (e
);
3808 e
->flags
&= ~EDGE_EXECUTABLE
;
3809 e
->flags
|= EDGE_IGNORE
;
3812 /* And queue an update for the stmt. */
3815 to_update_switch_stmts
.safe_push (su
);
3820 vr_values::cleanup_edges_and_switches (void)
3826 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3827 CFG in a broken state and requires a cfg_cleanup run. */
3828 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
3831 /* Update SWITCH_EXPR case label vector. */
3832 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
3835 size_t n
= TREE_VEC_LENGTH (su
->vec
);
3837 gimple_switch_set_num_labels (su
->stmt
, n
);
3838 for (j
= 0; j
< n
; j
++)
3839 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
3840 /* As we may have replaced the default label with a regular one
3841 make sure to make it a real default label again. This ensures
3842 optimal expansion. */
3843 label
= gimple_switch_label (su
->stmt
, 0);
3844 CASE_LOW (label
) = NULL_TREE
;
3845 CASE_HIGH (label
) = NULL_TREE
;
3848 if (!to_remove_edges
.is_empty ())
3850 free_dominance_info (CDI_DOMINATORS
);
3851 loops_state_set (LOOPS_NEED_FIXUP
);
3854 to_remove_edges
.release ();
3855 to_update_switch_stmts
.release ();
3858 /* Simplify an integral conversion from an SSA name in STMT. */
3861 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3863 tree innerop
, middleop
, finaltype
;
3865 signop inner_sgn
, middle_sgn
, final_sgn
;
3866 unsigned inner_prec
, middle_prec
, final_prec
;
3867 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
3869 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
3870 if (!INTEGRAL_TYPE_P (finaltype
))
3872 middleop
= gimple_assign_rhs1 (stmt
);
3873 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
3874 if (!is_gimple_assign (def_stmt
)
3875 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3877 innerop
= gimple_assign_rhs1 (def_stmt
);
3878 if (TREE_CODE (innerop
) != SSA_NAME
3879 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
3882 /* Get the value-range of the inner operand. Use get_range_info in
3883 case innerop was created during substitute-and-fold. */
3884 wide_int imin
, imax
;
3885 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
3886 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
3888 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
3889 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
3891 /* Simulate the conversion chain to check if the result is equal if
3892 the middle conversion is removed. */
3893 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
3894 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
3895 final_prec
= TYPE_PRECISION (finaltype
);
3897 /* If the first conversion is not injective, the second must not
3899 if (wi::gtu_p (innermax
- innermin
,
3900 wi::mask
<widest_int
> (middle_prec
, false))
3901 && middle_prec
< final_prec
)
3903 /* We also want a medium value so that we can track the effect that
3904 narrowing conversions with sign change have. */
3905 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
3906 if (inner_sgn
== UNSIGNED
)
3907 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
3910 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
3911 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
3912 innermed
= innermin
;
3914 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
3915 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
3916 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
3917 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
3919 /* Require that the final conversion applied to both the original
3920 and the intermediate range produces the same result. */
3921 final_sgn
= TYPE_SIGN (finaltype
);
3922 if (wi::ext (middlemin
, final_prec
, final_sgn
)
3923 != wi::ext (innermin
, final_prec
, final_sgn
)
3924 || wi::ext (middlemed
, final_prec
, final_sgn
)
3925 != wi::ext (innermed
, final_prec
, final_sgn
)
3926 || wi::ext (middlemax
, final_prec
, final_sgn
)
3927 != wi::ext (innermax
, final_prec
, final_sgn
))
3930 gimple_assign_set_rhs1 (stmt
, innerop
);
3931 fold_stmt (gsi
, follow_single_use_edges
);
3935 /* Simplify a conversion from integral SSA name to float in STMT. */
3938 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
3941 tree rhs1
= gimple_assign_rhs1 (stmt
);
3942 value_range
*vr
= get_value_range (rhs1
);
3943 scalar_float_mode fltmode
3944 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
3945 scalar_int_mode mode
;
3949 /* We can only handle constant ranges. */
3950 if (!range_int_cst_p (vr
))
3953 /* First check if we can use a signed type in place of an unsigned. */
3954 scalar_int_mode rhs_mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1
));
3955 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
3956 && can_float_p (fltmode
, rhs_mode
, 0) != CODE_FOR_nothing
3957 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
3959 /* If we can do the conversion in the current input mode do nothing. */
3960 else if (can_float_p (fltmode
, rhs_mode
,
3961 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
3963 /* Otherwise search for a mode we can use, starting from the narrowest
3964 integer mode available. */
3967 mode
= NARROWEST_INT_MODE
;
3970 /* If we cannot do a signed conversion to float from mode
3971 or if the value-range does not fit in the signed type
3972 try with a wider mode. */
3973 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
3974 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
3977 /* But do not widen the input. Instead leave that to the
3978 optabs expansion code. */
3979 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
3980 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
3985 /* It works, insert a truncation or sign-change before the
3986 float conversion. */
3987 tem
= make_ssa_name (build_nonstandard_integer_type
3988 (GET_MODE_PRECISION (mode
), 0));
3989 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
3990 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
3991 gimple_assign_set_rhs1 (stmt
, tem
);
3992 fold_stmt (gsi
, follow_single_use_edges
);
3997 /* Simplify an internal fn call using ranges if possible. */
4000 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
,
4003 enum tree_code subcode
;
4004 bool is_ubsan
= false;
4006 switch (gimple_call_internal_fn (stmt
))
4008 case IFN_UBSAN_CHECK_ADD
:
4009 subcode
= PLUS_EXPR
;
4012 case IFN_UBSAN_CHECK_SUB
:
4013 subcode
= MINUS_EXPR
;
4016 case IFN_UBSAN_CHECK_MUL
:
4017 subcode
= MULT_EXPR
;
4020 case IFN_ADD_OVERFLOW
:
4021 subcode
= PLUS_EXPR
;
4023 case IFN_SUB_OVERFLOW
:
4024 subcode
= MINUS_EXPR
;
4026 case IFN_MUL_OVERFLOW
:
4027 subcode
= MULT_EXPR
;
4033 tree op0
= gimple_call_arg (stmt
, 0);
4034 tree op1
= gimple_call_arg (stmt
, 1);
4038 type
= TREE_TYPE (op0
);
4039 if (VECTOR_TYPE_P (type
))
4042 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
4045 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
4046 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
4047 || (is_ubsan
&& ovf
))
4051 location_t loc
= gimple_location (stmt
);
4053 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
4056 int prec
= TYPE_PRECISION (type
);
4059 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
4060 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
4061 utype
= build_nonstandard_integer_type (prec
, 1);
4062 if (TREE_CODE (op0
) == INTEGER_CST
)
4063 op0
= fold_convert (utype
, op0
);
4064 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
4066 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
4067 gimple_set_location (g
, loc
);
4068 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4069 op0
= gimple_assign_lhs (g
);
4071 if (TREE_CODE (op1
) == INTEGER_CST
)
4072 op1
= fold_convert (utype
, op1
);
4073 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
4075 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
4076 gimple_set_location (g
, loc
);
4077 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4078 op1
= gimple_assign_lhs (g
);
4080 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
4081 gimple_set_location (g
, loc
);
4082 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4085 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
4086 gimple_assign_lhs (g
));
4087 gimple_set_location (g
, loc
);
4088 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4090 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
4091 gimple_assign_lhs (g
),
4092 build_int_cst (type
, ovf
));
4094 gimple_set_location (g
, loc
);
4095 gsi_replace (gsi
, g
, false);
4099 /* Return true if VAR is a two-valued variable. Set a and b with the
4100 two-values when it is true. Return false otherwise. */
4103 vr_values::two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
4105 value_range
*vr
= get_value_range (var
);
4106 if (vr
->varying_p ()
4107 || vr
->undefined_p ()
4108 || TREE_CODE (vr
->min ()) != INTEGER_CST
4109 || TREE_CODE (vr
->max ()) != INTEGER_CST
)
4112 if (vr
->kind () == VR_RANGE
4113 && wi::to_wide (vr
->max ()) - wi::to_wide (vr
->min ()) == 1)
4120 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4121 if (vr
->kind () == VR_ANTI_RANGE
4122 && (wi::to_wide (vr
->min ())
4123 - wi::to_wide (vrp_val_min (TREE_TYPE (var
)))) == 1
4124 && (wi::to_wide (vrp_val_max (TREE_TYPE (var
)))
4125 - wi::to_wide (vr
->max ())) == 1)
4127 *a
= vrp_val_min (TREE_TYPE (var
));
4128 *b
= vrp_val_max (TREE_TYPE (var
));
4135 /* Simplify STMT using ranges if possible. */
4138 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
4140 gimple
*stmt
= gsi_stmt (*gsi
);
4141 if (is_gimple_assign (stmt
))
4143 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
4144 tree rhs1
= gimple_assign_rhs1 (stmt
);
4145 tree rhs2
= gimple_assign_rhs2 (stmt
);
4146 tree lhs
= gimple_assign_lhs (stmt
);
4147 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
4148 use_operand_p use_p
;
4153 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4155 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4159 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4161 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4163 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
4164 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
4165 && ((TREE_CODE (rhs1
) == INTEGER_CST
4166 && TREE_CODE (rhs2
) == SSA_NAME
)
4167 || (TREE_CODE (rhs2
) == INTEGER_CST
4168 && TREE_CODE (rhs1
) == SSA_NAME
))
4169 && single_imm_use (lhs
, &use_p
, &use_stmt
)
4170 && gimple_code (use_stmt
) == GIMPLE_COND
)
4173 tree new_rhs1
= NULL_TREE
;
4174 tree new_rhs2
= NULL_TREE
;
4175 tree cmp_var
= NULL_TREE
;
4177 if (TREE_CODE (rhs2
) == SSA_NAME
4178 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
4180 /* Optimize RHS1 OP [VAL1, VAL2]. */
4181 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
4182 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
4185 else if (TREE_CODE (rhs1
) == SSA_NAME
4186 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
4188 /* Optimize [VAL1, VAL2] OP RHS2. */
4189 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
4190 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
4194 /* If we could not find two-vals or the optimzation is invalid as
4195 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4196 if (new_rhs1
&& new_rhs2
)
4198 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
4199 gimple_assign_set_rhs_with_ops (gsi
,
4203 update_stmt (gsi_stmt (*gsi
));
4204 fold_stmt (gsi
, follow_single_use_edges
);
4213 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4214 if the RHS is zero or one, and the LHS are known to be boolean
4216 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4217 return simplify_truth_ops_using_ranges (gsi
, stmt
);
4220 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4221 and BIT_AND_EXPR respectively if the first operand is greater
4222 than zero and the second operand is an exact power of two.
4223 Also optimize TRUNC_MOD_EXPR away if the second operand is
4224 constant and the first operand already has the right value
4226 case TRUNC_DIV_EXPR
:
4227 case TRUNC_MOD_EXPR
:
4228 if ((TREE_CODE (rhs1
) == SSA_NAME
4229 || TREE_CODE (rhs1
) == INTEGER_CST
)
4230 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4231 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
4234 /* Transform ABS (X) into X or -X as appropriate. */
4236 if (TREE_CODE (rhs1
) == SSA_NAME
4237 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4238 return simplify_abs_using_ranges (gsi
, stmt
);
4243 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4244 if all the bits being cleared are already cleared or
4245 all the bits being set are already set. */
4246 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4247 return simplify_bit_ops_using_ranges (gsi
, stmt
);
4251 if (TREE_CODE (rhs1
) == SSA_NAME
4252 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4253 return simplify_conversion_using_ranges (gsi
, stmt
);
4257 if (TREE_CODE (rhs1
) == SSA_NAME
4258 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4259 return simplify_float_conversion_using_ranges (gsi
, stmt
);
4264 return simplify_min_or_max_using_ranges (gsi
, stmt
);
4270 else if (gimple_code (stmt
) == GIMPLE_COND
)
4271 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
4272 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
4273 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
4274 else if (is_gimple_call (stmt
)
4275 && gimple_call_internal_p (stmt
))
4276 return simplify_internal_call_using_ranges (gsi
, stmt
);
4282 vr_values::set_vr_value (tree var
, value_range
*vr
)
4284 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
4286 vr_value
[SSA_NAME_VERSION (var
)] = vr
;