1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 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
193 if (new_vr
->undefined_p ())
195 old_vr
->set_varying ();
196 new_vr
->set_varying ();
200 old_vr
->set (new_vr
->kind (),
201 new_vr
->min (), new_vr
->max (), new_vr
->equiv ());
204 new_vr
->equiv_clear ();
209 /* Return true if value range VR involves exactly one symbol SYM. */
212 symbolic_range_based_on_p (value_range_base
*vr
, const_tree sym
)
214 bool neg
, min_has_symbol
, max_has_symbol
;
217 if (is_gimple_min_invariant (vr
->min ()))
218 min_has_symbol
= false;
219 else if (get_single_symbol (vr
->min (), &neg
, &inv
) == sym
)
220 min_has_symbol
= true;
224 if (is_gimple_min_invariant (vr
->max ()))
225 max_has_symbol
= false;
226 else if (get_single_symbol (vr
->max (), &neg
, &inv
) == sym
)
227 max_has_symbol
= true;
231 return (min_has_symbol
|| max_has_symbol
);
234 /* Return true if the result of assignment STMT is know to be non-zero. */
237 gimple_assign_nonzero_p (gimple
*stmt
)
239 enum tree_code code
= gimple_assign_rhs_code (stmt
);
240 bool strict_overflow_p
;
241 switch (get_gimple_rhs_class (code
))
243 case GIMPLE_UNARY_RHS
:
244 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
245 gimple_expr_type (stmt
),
246 gimple_assign_rhs1 (stmt
),
248 case GIMPLE_BINARY_RHS
:
249 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
250 gimple_expr_type (stmt
),
251 gimple_assign_rhs1 (stmt
),
252 gimple_assign_rhs2 (stmt
),
254 case GIMPLE_TERNARY_RHS
:
256 case GIMPLE_SINGLE_RHS
:
257 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
259 case GIMPLE_INVALID_RHS
:
266 /* Return true if STMT is known to compute a non-zero value. */
269 gimple_stmt_nonzero_p (gimple
*stmt
)
271 switch (gimple_code (stmt
))
274 return gimple_assign_nonzero_p (stmt
);
277 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
278 return (gimple_call_nonnull_result_p (call_stmt
)
279 || gimple_call_nonnull_arg (call_stmt
));
285 /* Like tree_expr_nonzero_p, but this function uses value ranges
289 vr_values::vrp_stmt_computes_nonzero (gimple
*stmt
)
291 if (gimple_stmt_nonzero_p (stmt
))
294 /* If we have an expression of the form &X->a, then the expression
295 is nonnull if X is nonnull. */
296 if (is_gimple_assign (stmt
)
297 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
299 tree expr
= gimple_assign_rhs1 (stmt
);
300 poly_int64 bitsize
, bitpos
;
303 int unsignedp
, reversep
, volatilep
;
304 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
305 &bitpos
, &offset
, &mode
, &unsignedp
,
306 &reversep
, &volatilep
);
308 if (base
!= NULL_TREE
309 && TREE_CODE (base
) == MEM_REF
310 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
312 poly_offset_int off
= 0;
313 bool off_cst
= false;
314 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
316 off
= mem_ref_offset (base
);
318 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
320 off
<<= LOG2_BITS_PER_UNIT
;
324 /* If &X->a is equal to X and X is ~[0, 0], the result is too.
325 For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
326 allow going from non-NULL pointer to NULL. */
327 if ((off_cst
&& known_eq (off
, 0))
328 || (flag_delete_null_pointer_checks
329 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))))
331 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
332 if (!range_includes_zero_p (vr
))
335 /* If MEM_REF has a "positive" offset, consider it non-NULL
336 always, for -fdelete-null-pointer-checks also "negative"
337 ones. Punt for unknown offsets (e.g. variable ones). */
338 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
341 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
349 /* Returns true if EXPR is a valid value (as expected by compare_values) --
350 a gimple invariant, or SSA_NAME +- CST. */
353 valid_value_p (tree expr
)
355 if (TREE_CODE (expr
) == SSA_NAME
)
358 if (TREE_CODE (expr
) == PLUS_EXPR
359 || TREE_CODE (expr
) == MINUS_EXPR
)
360 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
361 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
363 return is_gimple_min_invariant (expr
);
366 /* If OP has a value range with a single constant value return that,
367 otherwise return NULL_TREE. This returns OP itself if OP is a
371 vr_values::op_with_constant_singleton_value_range (tree op
)
373 if (is_gimple_min_invariant (op
))
376 if (TREE_CODE (op
) != SSA_NAME
)
379 return value_range_constant_singleton (get_value_range (op
));
382 /* Return true if op is in a boolean [0, 1] value-range. */
385 vr_values::op_with_boolean_value_range_p (tree op
)
389 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
392 if (integer_zerop (op
)
393 || integer_onep (op
))
396 if (TREE_CODE (op
) != SSA_NAME
)
399 vr
= get_value_range (op
);
400 return (vr
->kind () == VR_RANGE
401 && integer_zerop (vr
->min ())
402 && integer_onep (vr
->max ()));
405 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
406 true and store it in *VR_P. */
409 vr_values::extract_range_for_var_from_comparison_expr (tree var
,
410 enum tree_code cond_code
,
415 value_range
*limit_vr
;
416 type
= TREE_TYPE (var
);
418 /* For pointer arithmetic, we only keep track of pointer equality
419 and inequality. If we arrive here with unfolded conditions like
420 _1 > _1 do not derive anything. */
421 if ((POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
424 vr_p
->set_varying ();
428 /* If LIMIT is another SSA name and LIMIT has a range of its own,
429 try to use LIMIT's range to avoid creating symbolic ranges
431 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
433 /* LIMIT's range is only interesting if it has any useful information. */
435 || limit_vr
->undefined_p ()
436 || limit_vr
->varying_p ()
437 || (limit_vr
->symbolic_p ()
438 && ! (limit_vr
->kind () == VR_RANGE
439 && (limit_vr
->min () == limit_vr
->max ()
440 || operand_equal_p (limit_vr
->min (),
441 limit_vr
->max (), 0)))))
444 /* Initially, the new range has the same set of equivalences of
445 VAR's range. This will be revised before returning the final
446 value. Since assertions may be chained via mutually exclusive
447 predicates, we will need to trim the set of equivalences before
449 gcc_assert (vr_p
->equiv () == NULL
);
450 vr_p
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
452 /* Extract a new range based on the asserted comparison for VAR and
453 LIMIT's value range. Notice that if LIMIT has an anti-range, we
454 will only use it for equality comparisons (EQ_EXPR). For any
455 other kind of assertion, we cannot derive a range from LIMIT's
456 anti-range that can be used to describe the new range. For
457 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
458 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
459 no single range for x_2 that could describe LE_EXPR, so we might
460 as well build the range [b_4, +INF] for it.
461 One special case we handle is extracting a range from a
462 range test encoded as (unsigned)var + CST <= limit. */
463 if (TREE_CODE (op
) == NOP_EXPR
464 || TREE_CODE (op
) == PLUS_EXPR
)
466 if (TREE_CODE (op
) == PLUS_EXPR
)
468 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
469 TREE_OPERAND (op
, 1));
470 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
471 op
= TREE_OPERAND (op
, 0);
475 min
= build_int_cst (TREE_TYPE (var
), 0);
479 /* Make sure to not set TREE_OVERFLOW on the final type
480 conversion. We are willingly interpreting large positive
481 unsigned values as negative signed values here. */
482 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
483 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
485 /* We can transform a max, min range to an anti-range or
486 vice-versa. Use set_and_canonicalize which does this for
488 if (cond_code
== LE_EXPR
)
489 vr_p
->set_and_canonicalize (VR_RANGE
, min
, max
, vr_p
->equiv ());
490 else if (cond_code
== GT_EXPR
)
491 vr_p
->set_and_canonicalize (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
495 else if (cond_code
== EQ_EXPR
)
497 enum value_range_kind range_type
;
501 range_type
= limit_vr
->kind ();
502 min
= limit_vr
->min ();
503 max
= limit_vr
->max ();
507 range_type
= VR_RANGE
;
512 vr_p
->update (range_type
, min
, max
);
514 /* When asserting the equality VAR == LIMIT and LIMIT is another
515 SSA name, the new range will also inherit the equivalence set
517 if (TREE_CODE (limit
) == SSA_NAME
)
518 vr_p
->equiv_add (limit
, get_value_range (limit
), &vrp_equiv_obstack
);
520 else if (cond_code
== NE_EXPR
)
522 /* As described above, when LIMIT's range is an anti-range and
523 this assertion is an inequality (NE_EXPR), then we cannot
524 derive anything from the anti-range. For instance, if
525 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
526 not imply that VAR's range is [0, 0]. So, in the case of
527 anti-ranges, we just assert the inequality using LIMIT and
530 If LIMIT_VR is a range, we can only use it to build a new
531 anti-range if LIMIT_VR is a single-valued range. For
532 instance, if LIMIT_VR is [0, 1], the predicate
533 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
534 Rather, it means that for value 0 VAR should be ~[0, 0]
535 and for value 1, VAR should be ~[1, 1]. We cannot
536 represent these ranges.
538 The only situation in which we can build a valid
539 anti-range is when LIMIT_VR is a single-valued range
540 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
541 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
543 && limit_vr
->kind () == VR_RANGE
544 && compare_values (limit_vr
->min (), limit_vr
->max ()) == 0)
546 min
= limit_vr
->min ();
547 max
= limit_vr
->max ();
551 /* In any other case, we cannot use LIMIT's range to build a
556 /* If MIN and MAX cover the whole range for their type, then
557 just use the original LIMIT. */
558 if (INTEGRAL_TYPE_P (type
)
559 && vrp_val_is_min (min
)
560 && vrp_val_is_max (max
))
563 vr_p
->set_and_canonicalize (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
565 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
567 min
= TYPE_MIN_VALUE (type
);
569 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
573 /* If LIMIT_VR is of the form [N1, N2], we need to build the
574 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
576 max
= limit_vr
->max ();
579 /* If the maximum value forces us to be out of bounds, simply punt.
580 It would be pointless to try and do anything more since this
581 all should be optimized away above us. */
582 if (cond_code
== LT_EXPR
583 && compare_values (max
, min
) == 0)
584 vr_p
->set_varying ();
587 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
588 if (cond_code
== LT_EXPR
)
590 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
591 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
592 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
593 build_int_cst (TREE_TYPE (max
), -1));
595 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
596 build_int_cst (TREE_TYPE (max
), 1));
597 /* Signal to compare_values_warnv this expr doesn't overflow. */
599 TREE_NO_WARNING (max
) = 1;
602 vr_p
->update (VR_RANGE
, min
, max
);
605 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
607 max
= TYPE_MAX_VALUE (type
);
609 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
613 /* If LIMIT_VR is of the form [N1, N2], we need to build the
614 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
616 min
= limit_vr
->min ();
619 /* If the minimum value forces us to be out of bounds, simply punt.
620 It would be pointless to try and do anything more since this
621 all should be optimized away above us. */
622 if (cond_code
== GT_EXPR
623 && compare_values (min
, max
) == 0)
624 vr_p
->set_varying ();
627 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
628 if (cond_code
== GT_EXPR
)
630 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
631 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
632 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
633 build_int_cst (TREE_TYPE (min
), -1));
635 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
636 build_int_cst (TREE_TYPE (min
), 1));
637 /* Signal to compare_values_warnv this expr doesn't overflow. */
639 TREE_NO_WARNING (min
) = 1;
642 vr_p
->update (VR_RANGE
, min
, max
);
648 /* Finally intersect the new range with what we already know about var. */
649 vr_p
->intersect (get_value_range (var
));
652 /* Extract value range information from an ASSERT_EXPR EXPR and store
656 vr_values::extract_range_from_assert (value_range
*vr_p
, tree expr
)
658 tree var
= ASSERT_EXPR_VAR (expr
);
659 tree cond
= ASSERT_EXPR_COND (expr
);
661 enum tree_code cond_code
;
662 gcc_assert (COMPARISON_CLASS_P (cond
));
664 /* Find VAR in the ASSERT_EXPR conditional. */
665 if (var
== TREE_OPERAND (cond
, 0)
666 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
667 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
669 /* If the predicate is of the form VAR COMP LIMIT, then we just
670 take LIMIT from the RHS and use the same comparison code. */
671 cond_code
= TREE_CODE (cond
);
672 limit
= TREE_OPERAND (cond
, 1);
673 op
= TREE_OPERAND (cond
, 0);
677 /* If the predicate is of the form LIMIT COMP VAR, then we need
678 to flip around the comparison code to create the proper range
680 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
681 limit
= TREE_OPERAND (cond
, 0);
682 op
= TREE_OPERAND (cond
, 1);
684 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
688 /* Extract range information from SSA name VAR and store it in VR. If
689 VAR has an interesting range, use it. Otherwise, create the
690 range [VAR, VAR] and return it. This is useful in situations where
691 we may have conditionals testing values of VARYING names. For
698 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
702 vr_values::extract_range_from_ssa_name (value_range
*vr
, tree var
)
704 value_range
*var_vr
= get_value_range (var
);
706 if (!var_vr
->varying_p ())
707 vr
->deep_copy (var_vr
);
711 vr
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
714 /* Extract range information from a binary expression OP0 CODE OP1 based on
715 the ranges of each of its operands with resulting type EXPR_TYPE.
716 The resulting range is stored in *VR. */
719 vr_values::extract_range_from_binary_expr (value_range
*vr
,
721 tree expr_type
, tree op0
, tree op1
)
723 /* Get value ranges for each operand. For constant operands, create
724 a new value range with the operand to simplify processing. */
725 value_range_base vr0
, vr1
;
726 if (TREE_CODE (op0
) == SSA_NAME
)
727 vr0
= *(get_value_range (op0
));
728 else if (is_gimple_min_invariant (op0
))
733 if (TREE_CODE (op1
) == SSA_NAME
)
734 vr1
= *(get_value_range (op1
));
735 else if (is_gimple_min_invariant (op1
))
740 /* If one argument is varying, we can sometimes still deduce a
741 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
742 if (INTEGRAL_TYPE_P (TREE_TYPE (op0
))
743 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
745 if (vr0
.varying_p () && !vr1
.varying_p ())
746 vr0
= value_range (VR_RANGE
,
747 vrp_val_min (expr_type
),
748 vrp_val_max (expr_type
));
749 else if (vr1
.varying_p () && !vr0
.varying_p ())
750 vr1
= value_range (VR_RANGE
,
751 vrp_val_min (expr_type
),
752 vrp_val_max (expr_type
));
755 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &vr0
, &vr1
);
757 /* Set value_range for n in following sequence:
758 def = __builtin_memchr (arg, 0, sz)
760 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
763 && code
== POINTER_DIFF_EXPR
764 && TREE_CODE (op0
) == SSA_NAME
765 && TREE_CODE (op1
) == SSA_NAME
)
767 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
768 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
769 gcall
*call_stmt
= NULL
;
771 if (TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
772 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
773 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
774 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
775 && (call_stmt
= dyn_cast
<gcall
*>(SSA_NAME_DEF_STMT (op0
)))
776 && gimple_call_builtin_p (call_stmt
, BUILT_IN_MEMCHR
)
777 && operand_equal_p (op0
, gimple_call_lhs (call_stmt
), 0)
778 && operand_equal_p (op1
, gimple_call_arg (call_stmt
, 0), 0)
779 && integer_zerop (gimple_call_arg (call_stmt
, 1)))
781 tree max
= vrp_val_max (ptrdiff_type_node
);
782 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
783 tree range_min
= build_zero_cst (expr_type
);
784 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
785 vr
->set (VR_RANGE
, range_min
, range_max
);
790 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
791 and based on the other operand, for example if it was deduced from a
792 symbolic comparison. When a bound of the range of the first operand
793 is invariant, we set the corresponding bound of the new range to INF
794 in order to avoid recursing on the range of the second operand. */
796 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
797 && TREE_CODE (op1
) == SSA_NAME
798 && vr0
.kind () == VR_RANGE
799 && symbolic_range_based_on_p (&vr0
, op1
))
801 const bool minus_p
= (code
== MINUS_EXPR
);
804 /* Try with VR0 and [-INF, OP1]. */
805 if (is_gimple_min_invariant (minus_p
? vr0
.max () : vr0
.min ()))
806 n_vr1
.set (VR_RANGE
, vrp_val_min (expr_type
), op1
);
808 /* Try with VR0 and [OP1, +INF]. */
809 else if (is_gimple_min_invariant (minus_p
? vr0
.min () : vr0
.max ()))
810 n_vr1
.set (VR_RANGE
, op1
, vrp_val_max (expr_type
));
812 /* Try with VR0 and [OP1, OP1]. */
814 n_vr1
.set (VR_RANGE
, op1
, op1
);
816 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &vr0
, &n_vr1
);
820 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
821 && TREE_CODE (op0
) == SSA_NAME
822 && vr1
.kind () == VR_RANGE
823 && symbolic_range_based_on_p (&vr1
, op0
))
825 const bool minus_p
= (code
== MINUS_EXPR
);
828 /* Try with [-INF, OP0] and VR1. */
829 if (is_gimple_min_invariant (minus_p
? vr1
.max () : vr1
.min ()))
830 n_vr0
.set (VR_RANGE
, vrp_val_min (expr_type
), op0
);
832 /* Try with [OP0, +INF] and VR1. */
833 else if (is_gimple_min_invariant (minus_p
? vr1
.min (): vr1
.max ()))
834 n_vr0
.set (VR_RANGE
, op0
, vrp_val_max (expr_type
));
836 /* Try with [OP0, OP0] and VR1. */
840 ::extract_range_from_binary_expr (vr
, code
, expr_type
, &n_vr0
, &vr1
);
843 /* If we didn't derive a range for MINUS_EXPR, and
844 op1's range is ~[op0,op0] or vice-versa, then we
845 can derive a non-null range. This happens often for
846 pointer subtraction. */
848 && (code
== MINUS_EXPR
|| code
== POINTER_DIFF_EXPR
)
849 && TREE_CODE (op0
) == SSA_NAME
850 && ((vr0
.kind () == VR_ANTI_RANGE
852 && vr0
.min () == vr0
.max ())
853 || (vr1
.kind () == VR_ANTI_RANGE
855 && vr1
.min () == vr1
.max ())))
856 vr
->set_nonnull (expr_type
);
859 /* Extract range information from a unary expression CODE OP0 based on
860 the range of its operand with resulting type TYPE.
861 The resulting range is stored in *VR. */
864 vr_values::extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
867 value_range_base vr0
;
869 /* Get value ranges for the operand. For constant operands, create
870 a new value range with the operand to simplify processing. */
871 if (TREE_CODE (op0
) == SSA_NAME
)
872 vr0
= *(get_value_range (op0
));
873 else if (is_gimple_min_invariant (op0
))
878 ::extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
882 /* Extract range information from a conditional expression STMT based on
883 the ranges of each of its operands and the expression code. */
886 vr_values::extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
888 /* Get value ranges for each operand. For constant operands, create
889 a new value range with the operand to simplify processing. */
890 tree op0
= gimple_assign_rhs2 (stmt
);
892 value_range
*vr0
= &tem0
;
893 if (TREE_CODE (op0
) == SSA_NAME
)
894 vr0
= get_value_range (op0
);
895 else if (is_gimple_min_invariant (op0
))
900 tree op1
= gimple_assign_rhs3 (stmt
);
902 value_range
*vr1
= &tem1
;
903 if (TREE_CODE (op1
) == SSA_NAME
)
904 vr1
= get_value_range (op1
);
905 else if (is_gimple_min_invariant (op1
))
910 /* The resulting value range is the union of the operand ranges */
916 /* Extract range information from a comparison expression EXPR based
917 on the range of its operand and the expression code. */
920 vr_values::extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
921 tree type
, tree op0
, tree op1
)
926 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
930 /* Since this expression was found on the RHS of an assignment,
931 its type may be different from _Bool. Convert VAL to EXPR's
933 val
= fold_convert (type
, val
);
934 if (is_gimple_min_invariant (val
))
937 vr
->update (VR_RANGE
, val
, val
);
940 /* The result of a comparison is always true or false. */
941 set_value_range_to_truthvalue (vr
, type
);
944 /* Helper function for simplify_internal_call_using_ranges and
945 extract_range_basic. Return true if OP0 SUBCODE OP1 for
946 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
947 always overflow. Set *OVF to true if it is known to always
951 vr_values::check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
952 tree op0
, tree op1
, bool *ovf
)
954 value_range_base vr0
, vr1
;
955 if (TREE_CODE (op0
) == SSA_NAME
)
956 vr0
= *get_value_range (op0
);
957 else if (TREE_CODE (op0
) == INTEGER_CST
)
962 if (TREE_CODE (op1
) == SSA_NAME
)
963 vr1
= *get_value_range (op1
);
964 else if (TREE_CODE (op1
) == INTEGER_CST
)
969 tree vr0min
= vr0
.min (), vr0max
= vr0
.max ();
970 tree vr1min
= vr1
.min (), vr1max
= vr1
.max ();
971 if (!range_int_cst_p (&vr0
)
972 || TREE_OVERFLOW (vr0min
)
973 || TREE_OVERFLOW (vr0max
))
975 vr0min
= vrp_val_min (TREE_TYPE (op0
));
976 vr0max
= vrp_val_max (TREE_TYPE (op0
));
978 if (!range_int_cst_p (&vr1
)
979 || TREE_OVERFLOW (vr1min
)
980 || TREE_OVERFLOW (vr1max
))
982 vr1min
= vrp_val_min (TREE_TYPE (op1
));
983 vr1max
= vrp_val_max (TREE_TYPE (op1
));
985 *ovf
= arith_overflowed_p (subcode
, type
, vr0min
,
986 subcode
== MINUS_EXPR
? vr1max
: vr1min
);
987 if (arith_overflowed_p (subcode
, type
, vr0max
,
988 subcode
== MINUS_EXPR
? vr1min
: vr1max
) != *ovf
)
990 if (subcode
== MULT_EXPR
)
992 if (arith_overflowed_p (subcode
, type
, vr0min
, vr1max
) != *ovf
993 || arith_overflowed_p (subcode
, type
, vr0max
, vr1min
) != *ovf
)
998 /* So far we found that there is an overflow on the boundaries.
999 That doesn't prove that there is an overflow even for all values
1000 in between the boundaries. For that compute widest_int range
1001 of the result and see if it doesn't overlap the range of
1003 widest_int wmin
, wmax
;
1006 w
[0] = wi::to_widest (vr0min
);
1007 w
[1] = wi::to_widest (vr0max
);
1008 w
[2] = wi::to_widest (vr1min
);
1009 w
[3] = wi::to_widest (vr1max
);
1010 for (i
= 0; i
< 4; i
++)
1016 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1019 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1022 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1034 wmin
= wi::smin (wmin
, wt
);
1035 wmax
= wi::smax (wmax
, wt
);
1038 /* The result of op0 CODE op1 is known to be in range
1040 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
1041 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
1042 /* If all values in [wmin, wmax] are smaller than
1043 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1044 the arithmetic operation will always overflow. */
1045 if (wmax
< wtmin
|| wmin
> wtmax
)
1052 /* Try to derive a nonnegative or nonzero range out of STMT relying
1053 primarily on generic routines in fold in conjunction with range data.
1054 Store the result in *VR */
1057 vr_values::extract_range_basic (value_range
*vr
, gimple
*stmt
)
1060 tree type
= gimple_expr_type (stmt
);
1062 if (is_gimple_call (stmt
))
1065 int mini
, maxi
, zerov
= 0, prec
;
1066 enum tree_code subcode
= ERROR_MARK
;
1067 combined_fn cfn
= gimple_call_combined_fn (stmt
);
1068 scalar_int_mode mode
;
1072 case CFN_BUILT_IN_CONSTANT_P
:
1073 /* If the call is __builtin_constant_p and the argument is a
1074 function parameter resolve it to false. This avoids bogus
1075 array bound warnings.
1076 ??? We could do this as early as inlining is finished. */
1077 arg
= gimple_call_arg (stmt
, 0);
1078 if (TREE_CODE (arg
) == SSA_NAME
1079 && SSA_NAME_IS_DEFAULT_DEF (arg
)
1080 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
1081 && cfun
->after_inlining
)
1083 vr
->set_null (type
);
1087 /* Both __builtin_ffs* and __builtin_popcount return
1091 arg
= gimple_call_arg (stmt
, 0);
1092 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1095 if (TREE_CODE (arg
) == SSA_NAME
)
1097 value_range
*vr0
= get_value_range (arg
);
1098 /* If arg is non-zero, then ffs or popcount are non-zero. */
1099 if (range_includes_zero_p (vr0
) == 0)
1101 /* If some high bits are known to be zero,
1102 we can decrease the maximum. */
1103 if (vr0
->kind () == VR_RANGE
1104 && TREE_CODE (vr0
->max ()) == INTEGER_CST
1105 && !operand_less_p (vr0
->min (),
1106 build_zero_cst (TREE_TYPE (vr0
->min ()))))
1107 maxi
= tree_floor_log2 (vr0
->max ()) + 1;
1110 /* __builtin_parity* returns [0, 1]. */
1115 /* __builtin_c[lt]z* return [0, prec-1], except for
1116 when the argument is 0, but that is undefined behavior.
1117 On many targets where the CLZ RTL or optab value is defined
1118 for 0 the value is prec, so include that in the range
1121 arg
= gimple_call_arg (stmt
, 0);
1122 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1125 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1126 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
1127 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
)
1128 /* Handle only the single common value. */
1130 /* Magic value to give up, unless vr0 proves
1133 if (TREE_CODE (arg
) == SSA_NAME
)
1135 value_range
*vr0
= get_value_range (arg
);
1136 /* From clz of VR_RANGE minimum we can compute
1138 if (vr0
->kind () == VR_RANGE
1139 && TREE_CODE (vr0
->min ()) == INTEGER_CST
)
1141 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min ());
1145 else if (vr0
->kind () == VR_ANTI_RANGE
1146 && integer_zerop (vr0
->min ()))
1153 /* From clz of VR_RANGE maximum we can compute
1155 if (vr0
->kind () == VR_RANGE
1156 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1158 mini
= prec
- 1 - tree_floor_log2 (vr0
->max ());
1166 /* __builtin_ctz* return [0, prec-1], except for
1167 when the argument is 0, but that is undefined behavior.
1168 If there is a ctz optab for this mode and
1169 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1170 otherwise just assume 0 won't be seen. */
1172 arg
= gimple_call_arg (stmt
, 0);
1173 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1176 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1177 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
1178 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
))
1180 /* Handle only the two common values. */
1183 else if (zerov
== prec
)
1186 /* Magic value to give up, unless vr0 proves
1190 if (TREE_CODE (arg
) == SSA_NAME
)
1192 value_range
*vr0
= get_value_range (arg
);
1193 /* If arg is non-zero, then use [0, prec - 1]. */
1194 if ((vr0
->kind () == VR_RANGE
1195 && integer_nonzerop (vr0
->min ()))
1196 || (vr0
->kind () == VR_ANTI_RANGE
1197 && integer_zerop (vr0
->min ())))
1202 /* If some high bits are known to be zero,
1203 we can decrease the result maximum. */
1204 if (vr0
->kind () == VR_RANGE
1205 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1207 maxi
= tree_floor_log2 (vr0
->max ());
1208 /* For vr0 [0, 0] give up. */
1216 /* __builtin_clrsb* returns [0, prec-1]. */
1218 arg
= gimple_call_arg (stmt
, 0);
1219 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1224 vr
->set (VR_RANGE
, build_int_cst (type
, mini
),
1225 build_int_cst (type
, maxi
));
1227 case CFN_UBSAN_CHECK_ADD
:
1228 subcode
= PLUS_EXPR
;
1230 case CFN_UBSAN_CHECK_SUB
:
1231 subcode
= MINUS_EXPR
;
1233 case CFN_UBSAN_CHECK_MUL
:
1234 subcode
= MULT_EXPR
;
1236 case CFN_GOACC_DIM_SIZE
:
1237 case CFN_GOACC_DIM_POS
:
1238 /* Optimizing these two internal functions helps the loop
1239 optimizer eliminate outer comparisons. Size is [1,N]
1240 and pos is [0,N-1]. */
1242 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
1243 int axis
= oacc_get_ifn_dim_arg (stmt
);
1244 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1247 /* If it's dynamic, the backend might know a hardware
1249 size
= targetm
.goacc
.dim_limit (axis
);
1251 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
1252 vr
->set(VR_RANGE
, build_int_cst (type
, is_pos
? 0 : 1),
1254 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1257 case CFN_BUILT_IN_STRLEN
:
1258 if (tree lhs
= gimple_call_lhs (stmt
))
1259 if (ptrdiff_type_node
1260 && (TYPE_PRECISION (ptrdiff_type_node
)
1261 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1263 tree type
= TREE_TYPE (lhs
);
1264 tree max
= vrp_val_max (ptrdiff_type_node
);
1265 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1266 tree range_min
= build_zero_cst (type
);
1267 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
1268 vr
->set (VR_RANGE
, range_min
, range_max
);
1275 if (subcode
!= ERROR_MARK
)
1277 bool saved_flag_wrapv
= flag_wrapv
;
1278 /* Pretend the arithmetics is wrapping. If there is
1279 any overflow, we'll complain, but will actually do
1280 wrapping operation. */
1282 extract_range_from_binary_expr (vr
, subcode
, type
,
1283 gimple_call_arg (stmt
, 0),
1284 gimple_call_arg (stmt
, 1));
1285 flag_wrapv
= saved_flag_wrapv
;
1287 /* If for both arguments vrp_valueize returned non-NULL,
1288 this should have been already folded and if not, it
1289 wasn't folded because of overflow. Avoid removing the
1290 UBSAN_CHECK_* calls in that case. */
1291 if (vr
->kind () == VR_RANGE
1292 && (vr
->min () == vr
->max ()
1293 || operand_equal_p (vr
->min (), vr
->max (), 0)))
1298 /* Handle extraction of the two results (result of arithmetics and
1299 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1300 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1301 else if (is_gimple_assign (stmt
)
1302 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1303 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1304 && INTEGRAL_TYPE_P (type
))
1306 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1307 tree op
= gimple_assign_rhs1 (stmt
);
1308 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
1310 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
1311 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
1313 enum tree_code subcode
= ERROR_MARK
;
1314 switch (gimple_call_internal_fn (g
))
1316 case IFN_ADD_OVERFLOW
:
1317 subcode
= PLUS_EXPR
;
1319 case IFN_SUB_OVERFLOW
:
1320 subcode
= MINUS_EXPR
;
1322 case IFN_MUL_OVERFLOW
:
1323 subcode
= MULT_EXPR
;
1325 case IFN_ATOMIC_COMPARE_EXCHANGE
:
1326 if (code
== IMAGPART_EXPR
)
1328 /* This is the boolean return value whether compare and
1329 exchange changed anything or not. */
1330 vr
->set (VR_RANGE
, build_int_cst (type
, 0),
1331 build_int_cst (type
, 1));
1338 if (subcode
!= ERROR_MARK
)
1340 tree op0
= gimple_call_arg (g
, 0);
1341 tree op1
= gimple_call_arg (g
, 1);
1342 if (code
== IMAGPART_EXPR
)
1345 if (check_for_binary_op_overflow (subcode
, type
,
1347 vr
->set (build_int_cst (type
, ovf
));
1348 else if (TYPE_PRECISION (type
) == 1
1349 && !TYPE_UNSIGNED (type
))
1352 vr
->set (VR_RANGE
, build_int_cst (type
, 0),
1353 build_int_cst (type
, 1));
1355 else if (types_compatible_p (type
, TREE_TYPE (op0
))
1356 && types_compatible_p (type
, TREE_TYPE (op1
)))
1358 bool saved_flag_wrapv
= flag_wrapv
;
1359 /* Pretend the arithmetics is wrapping. If there is
1360 any overflow, IMAGPART_EXPR will be set. */
1362 extract_range_from_binary_expr (vr
, subcode
, type
,
1364 flag_wrapv
= saved_flag_wrapv
;
1368 value_range vr0
, vr1
;
1369 bool saved_flag_wrapv
= flag_wrapv
;
1370 /* Pretend the arithmetics is wrapping. If there is
1371 any overflow, IMAGPART_EXPR will be set. */
1373 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
1375 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
1377 ::extract_range_from_binary_expr (vr
, subcode
, type
,
1379 flag_wrapv
= saved_flag_wrapv
;
1386 if (INTEGRAL_TYPE_P (type
)
1387 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
1388 set_value_range_to_nonnegative (vr
, type
);
1389 else if (vrp_stmt_computes_nonzero (stmt
))
1390 vr
->set_nonnull (type
);
1396 /* Try to compute a useful range out of assignment STMT and store it
1400 vr_values::extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
1402 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1404 if (code
== ASSERT_EXPR
)
1405 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
1406 else if (code
== SSA_NAME
)
1407 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
1408 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
1409 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
1410 gimple_expr_type (stmt
),
1411 gimple_assign_rhs1 (stmt
),
1412 gimple_assign_rhs2 (stmt
));
1413 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
1414 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
1415 gimple_expr_type (stmt
),
1416 gimple_assign_rhs1 (stmt
));
1417 else if (code
== COND_EXPR
)
1418 extract_range_from_cond_expr (vr
, stmt
);
1419 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1420 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
1421 gimple_expr_type (stmt
),
1422 gimple_assign_rhs1 (stmt
),
1423 gimple_assign_rhs2 (stmt
));
1424 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
1425 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
1426 vr
->set (gimple_assign_rhs1 (stmt
));
1430 if (vr
->varying_p ())
1431 extract_range_basic (vr
, stmt
);
1434 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1436 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1437 all the values in the ranges.
1439 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1441 - Return NULL_TREE if it is not always possible to determine the
1442 value of the comparison.
1444 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1445 assumed signed overflow is undefined. */
1449 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
1450 bool *strict_overflow_p
)
1452 /* VARYING or UNDEFINED ranges cannot be compared. */
1453 if (vr0
->varying_p ()
1454 || vr0
->undefined_p ()
1455 || vr1
->varying_p ()
1456 || vr1
->undefined_p ())
1459 /* Anti-ranges need to be handled separately. */
1460 if (vr0
->kind () == VR_ANTI_RANGE
|| vr1
->kind () == VR_ANTI_RANGE
)
1462 /* If both are anti-ranges, then we cannot compute any
1464 if (vr0
->kind () == VR_ANTI_RANGE
&& vr1
->kind () == VR_ANTI_RANGE
)
1467 /* These comparisons are never statically computable. */
1474 /* Equality can be computed only between a range and an
1475 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1476 if (vr0
->kind () == VR_RANGE
)
1478 /* To simplify processing, make VR0 the anti-range. */
1479 value_range
*tmp
= vr0
;
1484 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
1486 if (compare_values_warnv (vr0
->min (), vr1
->min (), strict_overflow_p
) == 0
1487 && compare_values_warnv (vr0
->max (), vr1
->max (), strict_overflow_p
) == 0)
1488 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1493 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1494 operands around and change the comparison code. */
1495 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1497 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
1498 std::swap (vr0
, vr1
);
1501 if (comp
== EQ_EXPR
)
1503 /* Equality may only be computed if both ranges represent
1504 exactly one value. */
1505 if (compare_values_warnv (vr0
->min (), vr0
->max (), strict_overflow_p
) == 0
1506 && compare_values_warnv (vr1
->min (), vr1
->max (), strict_overflow_p
) == 0)
1508 int cmp_min
= compare_values_warnv (vr0
->min (), vr1
->min (),
1510 int cmp_max
= compare_values_warnv (vr0
->max (), vr1
->max (),
1512 if (cmp_min
== 0 && cmp_max
== 0)
1513 return boolean_true_node
;
1514 else if (cmp_min
!= -2 && cmp_max
!= -2)
1515 return boolean_false_node
;
1517 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1518 else if (compare_values_warnv (vr0
->min (), vr1
->max (),
1519 strict_overflow_p
) == 1
1520 || compare_values_warnv (vr1
->min (), vr0
->max (),
1521 strict_overflow_p
) == 1)
1522 return boolean_false_node
;
1526 else if (comp
== NE_EXPR
)
1530 /* If VR0 is completely to the left or completely to the right
1531 of VR1, they are always different. Notice that we need to
1532 make sure that both comparisons yield similar results to
1533 avoid comparing values that cannot be compared at
1535 cmp1
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1536 cmp2
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1537 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
1538 return boolean_true_node
;
1540 /* If VR0 and VR1 represent a single value and are identical,
1542 else if (compare_values_warnv (vr0
->min (), vr0
->max (),
1543 strict_overflow_p
) == 0
1544 && compare_values_warnv (vr1
->min (), vr1
->max (),
1545 strict_overflow_p
) == 0
1546 && compare_values_warnv (vr0
->min (), vr1
->min (),
1547 strict_overflow_p
) == 0
1548 && compare_values_warnv (vr0
->max (), vr1
->max (),
1549 strict_overflow_p
) == 0)
1550 return boolean_false_node
;
1552 /* Otherwise, they may or may not be different. */
1556 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1560 /* If VR0 is to the left of VR1, return true. */
1561 tst
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1562 if ((comp
== LT_EXPR
&& tst
== -1)
1563 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1564 return boolean_true_node
;
1566 /* If VR0 is to the right of VR1, return false. */
1567 tst
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1568 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1569 || (comp
== LE_EXPR
&& tst
== 1))
1570 return boolean_false_node
;
1572 /* Otherwise, we don't know. */
1579 /* Given a value range VR, a value VAL and a comparison code COMP, return
1580 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1581 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1582 always returns false. Return NULL_TREE if it is not always
1583 possible to determine the value of the comparison. Also set
1584 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1585 assumed signed overflow is undefined. */
1588 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
1589 bool *strict_overflow_p
)
1591 if (vr
->varying_p () || vr
->undefined_p ())
1594 /* Anti-ranges need to be handled separately. */
1595 if (vr
->kind () == VR_ANTI_RANGE
)
1597 /* For anti-ranges, the only predicates that we can compute at
1598 compile time are equality and inequality. */
1605 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1606 if (value_inside_range (val
, vr
->min (), vr
->max ()) == 1)
1607 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1612 if (comp
== EQ_EXPR
)
1614 /* EQ_EXPR may only be computed if VR represents exactly
1616 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0)
1618 int cmp
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1620 return boolean_true_node
;
1621 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
1622 return boolean_false_node
;
1624 else if (compare_values_warnv (val
, vr
->min (), strict_overflow_p
) == -1
1625 || compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1)
1626 return boolean_false_node
;
1630 else if (comp
== NE_EXPR
)
1632 /* If VAL is not inside VR, then they are always different. */
1633 if (compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1
1634 || compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 1)
1635 return boolean_true_node
;
1637 /* If VR represents exactly one value equal to VAL, then return
1639 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0
1640 && compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 0)
1641 return boolean_false_node
;
1643 /* Otherwise, they may or may not be different. */
1646 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1650 /* If VR is to the left of VAL, return true. */
1651 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1652 if ((comp
== LT_EXPR
&& tst
== -1)
1653 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1654 return boolean_true_node
;
1656 /* If VR is to the right of VAL, return false. */
1657 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1658 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1659 || (comp
== LE_EXPR
&& tst
== 1))
1660 return boolean_false_node
;
1662 /* Otherwise, we don't know. */
1665 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1669 /* If VR is to the right of VAL, return true. */
1670 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1671 if ((comp
== GT_EXPR
&& tst
== 1)
1672 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
1673 return boolean_true_node
;
1675 /* If VR is to the left of VAL, return false. */
1676 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1677 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
1678 || (comp
== GE_EXPR
&& tst
== -1))
1679 return boolean_false_node
;
1681 /* Otherwise, we don't know. */
1687 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1688 would be profitable to adjust VR using scalar evolution information
1689 for VAR. If so, update VR with the new limits. */
1692 vr_values::adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
1693 gimple
*stmt
, tree var
)
1695 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
1696 enum ev_direction dir
;
1698 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1699 better opportunities than a regular range, but I'm not sure. */
1700 if (vr
->kind () == VR_ANTI_RANGE
)
1703 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
1705 /* Like in PR19590, scev can return a constant function. */
1706 if (is_gimple_min_invariant (chrec
))
1712 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1715 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
1716 tem
= op_with_constant_singleton_value_range (init
);
1719 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
1720 tem
= op_with_constant_singleton_value_range (step
);
1724 /* If STEP is symbolic, we can't know whether INIT will be the
1725 minimum or maximum value in the range. Also, unless INIT is
1726 a simple expression, compare_values and possibly other functions
1727 in tree-vrp won't be able to handle it. */
1728 if (step
== NULL_TREE
1729 || !is_gimple_min_invariant (step
)
1730 || !valid_value_p (init
))
1733 dir
= scev_direction (chrec
);
1734 if (/* Do not adjust ranges if we do not know whether the iv increases
1735 or decreases, ... */
1736 dir
== EV_DIR_UNKNOWN
1737 /* ... or if it may wrap. */
1738 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
1739 get_chrec_loop (chrec
), true))
1742 type
= TREE_TYPE (var
);
1743 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1744 tmin
= lower_bound_in_type (type
, type
);
1746 tmin
= TYPE_MIN_VALUE (type
);
1747 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1748 tmax
= upper_bound_in_type (type
, type
);
1750 tmax
= TYPE_MAX_VALUE (type
);
1752 /* Try to use estimated number of iterations for the loop to constrain the
1753 final value in the evolution. */
1754 if (TREE_CODE (step
) == INTEGER_CST
1755 && is_gimple_val (init
)
1756 && (TREE_CODE (init
) != SSA_NAME
1757 || get_value_range (init
)->kind () == VR_RANGE
))
1761 /* We are only entering here for loop header PHI nodes, so using
1762 the number of latch executions is the correct thing to use. */
1763 if (max_loop_iterations (loop
, &nit
))
1766 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
1767 wi::overflow_type overflow
;
1769 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
1771 /* If the multiplication overflowed we can't do a meaningful
1772 adjustment. Likewise if the result doesn't fit in the type
1773 of the induction variable. For a signed type we have to
1774 check whether the result has the expected signedness which
1775 is that of the step as number of iterations is unsigned. */
1777 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
1779 || wi::gts_p (wtmp
, 0) == wi::gts_p (wi::to_wide (step
), 0)))
1781 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
1782 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
1783 TREE_TYPE (init
), init
, tem
);
1784 /* Likewise if the addition did. */
1785 if (maxvr
.kind () == VR_RANGE
)
1787 value_range_base initvr
;
1789 if (TREE_CODE (init
) == SSA_NAME
)
1790 initvr
= *(get_value_range (init
));
1791 else if (is_gimple_min_invariant (init
))
1796 /* Check if init + nit * step overflows. Though we checked
1797 scev {init, step}_loop doesn't wrap, it is not enough
1798 because the loop may exit immediately. Overflow could
1799 happen in the plus expression in this case. */
1800 if ((dir
== EV_DIR_DECREASES
1801 && compare_values (maxvr
.min (), initvr
.min ()) != -1)
1802 || (dir
== EV_DIR_GROWS
1803 && compare_values (maxvr
.max (), initvr
.max ()) != 1))
1806 tmin
= maxvr
.min ();
1807 tmax
= maxvr
.max ();
1813 if (vr
->varying_p () || vr
->undefined_p ())
1818 /* For VARYING or UNDEFINED ranges, just about anything we get
1819 from scalar evolutions should be better. */
1821 if (dir
== EV_DIR_DECREASES
)
1826 else if (vr
->kind () == VR_RANGE
)
1831 if (dir
== EV_DIR_DECREASES
)
1833 /* INIT is the maximum value. If INIT is lower than VR->MAX ()
1834 but no smaller than VR->MIN (), set VR->MAX () to INIT. */
1835 if (compare_values (init
, max
) == -1)
1838 /* According to the loop information, the variable does not
1840 if (compare_values (min
, tmin
) == -1)
1846 /* If INIT is bigger than VR->MIN (), set VR->MIN () to INIT. */
1847 if (compare_values (init
, min
) == 1)
1850 if (compare_values (tmax
, max
) == -1)
1857 /* If we just created an invalid range with the minimum
1858 greater than the maximum, we fail conservatively.
1859 This should happen only in unreachable
1860 parts of code, or for invalid programs. */
1861 if (compare_values (min
, max
) == 1)
1864 /* Even for valid range info, sometimes overflow flag will leak in.
1865 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1867 if (TREE_OVERFLOW_P (min
))
1868 min
= drop_tree_overflow (min
);
1869 if (TREE_OVERFLOW_P (max
))
1870 max
= drop_tree_overflow (max
);
1872 vr
->update (VR_RANGE
, min
, max
);
1875 /* Dump value ranges of all SSA_NAMEs to FILE. */
1878 vr_values::dump_all_value_ranges (FILE *file
)
1882 for (i
= 0; i
< num_vr_values
; i
++)
1886 print_generic_expr (file
, ssa_name (i
));
1887 fprintf (file
, ": ");
1888 dump_value_range (file
, vr_value
[i
]);
1889 fprintf (file
, "\n");
1893 fprintf (file
, "\n");
1896 /* Initialize VRP lattice. */
1898 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1900 values_propagated
= false;
1901 num_vr_values
= num_ssa_names
;
1902 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
1903 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
1904 bitmap_obstack_initialize (&vrp_equiv_obstack
);
1905 to_remove_edges
= vNULL
;
1906 to_update_switch_stmts
= vNULL
;
1909 /* Free VRP lattice. */
1911 vr_values::~vr_values ()
1913 /* Free allocated memory. */
1915 free (vr_phi_edge_counts
);
1916 bitmap_obstack_release (&vrp_equiv_obstack
);
1917 vrp_value_range_pool
.release ();
1919 /* So that we can distinguish between VRP data being available
1920 and not available. */
1922 vr_phi_edge_counts
= NULL
;
1924 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1925 then an EVRP client did not clean up properly. Catch it now rather
1926 than seeing something more obscure later. */
1927 gcc_assert (to_remove_edges
.is_empty ()
1928 && to_update_switch_stmts
.is_empty ());
1933 static class vr_values
*x_vr_values
;
1935 /* Return the singleton value-range for NAME or NAME. */
1938 vrp_valueize (tree name
)
1940 if (TREE_CODE (name
) == SSA_NAME
)
1942 value_range
*vr
= x_vr_values
->get_value_range (name
);
1943 if (vr
->kind () == VR_RANGE
1944 && (TREE_CODE (vr
->min ()) == SSA_NAME
1945 || is_gimple_min_invariant (vr
->min ()))
1946 && vrp_operand_equal_p (vr
->min (), vr
->max ()))
1952 /* Return the singleton value-range for NAME if that is a constant
1953 but signal to not follow SSA edges. */
1956 vrp_valueize_1 (tree name
)
1958 if (TREE_CODE (name
) == SSA_NAME
)
1960 /* If the definition may be simulated again we cannot follow
1961 this SSA edge as the SSA propagator does not necessarily
1962 re-visit the use. */
1963 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1964 if (!gimple_nop_p (def_stmt
)
1965 && prop_simulate_again_p (def_stmt
))
1967 value_range
*vr
= x_vr_values
->get_value_range (name
);
1969 if (vr
->singleton_p (&singleton
))
1975 /* Given STMT, an assignment or call, return its LHS if the type
1976 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
1979 get_output_for_vrp (gimple
*stmt
)
1981 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
1984 /* We only keep track of ranges in integral and pointer types. */
1985 tree lhs
= gimple_get_lhs (stmt
);
1986 if (TREE_CODE (lhs
) == SSA_NAME
1987 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
1988 /* It is valid to have NULL MIN/MAX values on a type. See
1989 build_range_type. */
1990 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
1991 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
1992 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
1998 /* Visit assignment STMT. If it produces an interesting range, record
1999 the range in VR and set LHS to OUTPUT_P. */
2002 vr_values::vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
,
2005 tree lhs
= get_output_for_vrp (stmt
);
2008 /* We only keep track of ranges in integral and pointer types. */
2011 enum gimple_code code
= gimple_code (stmt
);
2013 /* Try folding the statement to a constant first. */
2015 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
2020 if (TREE_CODE (tem
) == SSA_NAME
2021 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
2022 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
2024 extract_range_from_ssa_name (vr
, tem
);
2027 else if (is_gimple_min_invariant (tem
))
2033 /* Then dispatch to value-range extracting functions. */
2034 if (code
== GIMPLE_CALL
)
2035 extract_range_basic (vr
, stmt
);
2037 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
2041 /* Helper that gets the value range of the SSA_NAME with version I
2042 or a symbolic range containing the SSA_NAME only if the value range
2043 is varying or undefined. Uses TEM as storage for the alternate range. */
2046 vr_values::get_vr_for_comparison (int i
, value_range
*tem
)
2048 /* Shallow-copy equiv bitmap. */
2049 value_range
*vr
= get_value_range (ssa_name (i
));
2051 /* If name N_i does not have a valid range, use N_i as its own
2052 range. This allows us to compare against names that may
2053 have N_i in their ranges. */
2054 if (vr
->varying_p () || vr
->undefined_p ())
2056 tem
->set (ssa_name (i
));
2063 /* Compare all the value ranges for names equivalent to VAR with VAL
2064 using comparison code COMP. Return the same value returned by
2065 compare_range_with_value, including the setting of
2066 *STRICT_OVERFLOW_P. */
2069 vr_values::compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
2070 bool *strict_overflow_p
, bool use_equiv_p
)
2076 int used_strict_overflow
;
2078 value_range
*equiv_vr
, tem_vr
;
2080 /* Get the set of equivalences for VAR. */
2081 e
= get_value_range (var
)->equiv ();
2083 /* Start at -1. Set it to 0 if we do a comparison without relying
2084 on overflow, or 1 if all comparisons rely on overflow. */
2085 used_strict_overflow
= -1;
2087 /* Compare vars' value range with val. */
2088 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
), &tem_vr
);
2090 retval
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2092 used_strict_overflow
= sop
? 1 : 0;
2094 /* If the equiv set is empty we have done all work we need to do. */
2098 && used_strict_overflow
> 0)
2099 *strict_overflow_p
= true;
2103 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
2105 tree name
= ssa_name (i
);
2110 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
2111 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
2114 equiv_vr
= get_vr_for_comparison (i
, &tem_vr
);
2116 t
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2119 /* If we get different answers from different members
2120 of the equivalence set this check must be in a dead
2121 code region. Folding it to a trap representation
2122 would be correct here. For now just return don't-know. */
2132 used_strict_overflow
= 0;
2133 else if (used_strict_overflow
< 0)
2134 used_strict_overflow
= 1;
2139 && used_strict_overflow
> 0)
2140 *strict_overflow_p
= true;
2146 /* Given a comparison code COMP and names N1 and N2, compare all the
2147 ranges equivalent to N1 against all the ranges equivalent to N2
2148 to determine the value of N1 COMP N2. Return the same value
2149 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2150 whether we relied on undefined signed overflow in the comparison. */
2154 vr_values::compare_names (enum tree_code comp
, tree n1
, tree n2
,
2155 bool *strict_overflow_p
)
2159 bitmap_iterator bi1
, bi2
;
2161 int used_strict_overflow
;
2162 static bitmap_obstack
*s_obstack
= NULL
;
2163 static bitmap s_e1
= NULL
, s_e2
= NULL
;
2165 /* Compare the ranges of every name equivalent to N1 against the
2166 ranges of every name equivalent to N2. */
2167 e1
= get_value_range (n1
)->equiv ();
2168 e2
= get_value_range (n2
)->equiv ();
2170 /* Use the fake bitmaps if e1 or e2 are not available. */
2171 if (s_obstack
== NULL
)
2173 s_obstack
= XNEW (bitmap_obstack
);
2174 bitmap_obstack_initialize (s_obstack
);
2175 s_e1
= BITMAP_ALLOC (s_obstack
);
2176 s_e2
= BITMAP_ALLOC (s_obstack
);
2183 /* Add N1 and N2 to their own set of equivalences to avoid
2184 duplicating the body of the loop just to check N1 and N2
2186 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
2187 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
2189 /* If the equivalence sets have a common intersection, then the two
2190 names can be compared without checking their ranges. */
2191 if (bitmap_intersect_p (e1
, e2
))
2193 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2194 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2196 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
2198 : boolean_false_node
;
2201 /* Start at -1. Set it to 0 if we do a comparison without relying
2202 on overflow, or 1 if all comparisons rely on overflow. */
2203 used_strict_overflow
= -1;
2205 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2206 N2 to their own set of equivalences to avoid duplicating the body
2207 of the loop just to check N1 and N2 ranges. */
2208 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
2210 if (! ssa_name (i1
))
2213 value_range tem_vr1
;
2214 value_range
*vr1
= get_vr_for_comparison (i1
, &tem_vr1
);
2216 t
= retval
= NULL_TREE
;
2217 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
2219 if (! ssa_name (i2
))
2224 value_range tem_vr2
;
2225 value_range
*vr2
= get_vr_for_comparison (i2
, &tem_vr2
);
2227 t
= compare_ranges (comp
, vr1
, vr2
, &sop
);
2230 /* If we get different answers from different members
2231 of the equivalence set this check must be in a dead
2232 code region. Folding it to a trap representation
2233 would be correct here. For now just return don't-know. */
2237 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2238 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2244 used_strict_overflow
= 0;
2245 else if (used_strict_overflow
< 0)
2246 used_strict_overflow
= 1;
2252 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2253 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2254 if (used_strict_overflow
> 0)
2255 *strict_overflow_p
= true;
2260 /* None of the equivalent ranges are useful in computing this
2262 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2263 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2267 /* Helper function for vrp_evaluate_conditional_warnv & other
2271 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2272 (enum tree_code code
, tree op0
, tree op1
, bool * strict_overflow_p
)
2274 value_range
*vr0
, *vr1
;
2276 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
2277 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
2279 tree res
= NULL_TREE
;
2281 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
2283 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
2285 res
= (compare_range_with_value
2286 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
2290 /* Helper function for vrp_evaluate_conditional_warnv. */
2293 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
,
2296 bool *strict_overflow_p
,
2301 *only_ranges
= true;
2303 /* We only deal with integral and pointer types. */
2304 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2305 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
2308 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2309 as a simple equality test, then prefer that over its current form
2312 An overflow test which collapses to an equality test can always be
2313 expressed as a comparison of one argument against zero. Overflow
2314 occurs when the chosen argument is zero and does not occur if the
2315 chosen argument is not zero. */
2317 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
2319 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
2320 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2321 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2322 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2323 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2324 if (integer_zerop (x
))
2327 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2329 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2330 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2331 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2332 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2333 else if (wi::to_wide (x
) == max
- 1)
2336 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
2337 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2341 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
2342 (code
, op0
, op1
, strict_overflow_p
)))
2345 *only_ranges
= false;
2346 /* Do not use compare_names during propagation, it's quadratic. */
2347 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
2349 return compare_names (code
, op0
, op1
, strict_overflow_p
);
2350 else if (TREE_CODE (op0
) == SSA_NAME
)
2351 return compare_name_with_value (code
, op0
, op1
,
2352 strict_overflow_p
, use_equiv_p
);
2353 else if (TREE_CODE (op1
) == SSA_NAME
)
2354 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
2355 strict_overflow_p
, use_equiv_p
);
2359 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2360 information. Return NULL if the conditional can not be evaluated.
2361 The ranges of all the names equivalent with the operands in COND
2362 will be used when trying to compute the value. If the result is
2363 based on undefined signed overflow, issue a warning if
2367 vr_values::vrp_evaluate_conditional (tree_code code
, tree op0
,
2368 tree op1
, gimple
*stmt
)
2374 /* Some passes and foldings leak constants with overflow flag set
2375 into the IL. Avoid doing wrong things with these and bail out. */
2376 if ((TREE_CODE (op0
) == INTEGER_CST
2377 && TREE_OVERFLOW (op0
))
2378 || (TREE_CODE (op1
) == INTEGER_CST
2379 && TREE_OVERFLOW (op1
)))
2383 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
2388 enum warn_strict_overflow_code wc
;
2389 const char* warnmsg
;
2391 if (is_gimple_min_invariant (ret
))
2393 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
2394 warnmsg
= G_("assuming signed overflow does not occur when "
2395 "simplifying conditional to constant");
2399 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
2400 warnmsg
= G_("assuming signed overflow does not occur when "
2401 "simplifying conditional");
2404 if (issue_strict_overflow_warning (wc
))
2406 location_t location
;
2408 if (!gimple_has_location (stmt
))
2409 location
= input_location
;
2411 location
= gimple_location (stmt
);
2412 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
2416 if (warn_type_limits
2417 && ret
&& only_ranges
2418 && TREE_CODE_CLASS (code
) == tcc_comparison
2419 && TREE_CODE (op0
) == SSA_NAME
)
2421 /* If the comparison is being folded and the operand on the LHS
2422 is being compared against a constant value that is outside of
2423 the natural range of OP0's type, then the predicate will
2424 always fold regardless of the value of OP0. If -Wtype-limits
2425 was specified, emit a warning. */
2426 tree type
= TREE_TYPE (op0
);
2427 value_range
*vr0
= get_value_range (op0
);
2429 if (vr0
->kind () == VR_RANGE
2430 && INTEGRAL_TYPE_P (type
)
2431 && vrp_val_is_min (vr0
->min ())
2432 && vrp_val_is_max (vr0
->max ())
2433 && is_gimple_min_invariant (op1
))
2435 location_t location
;
2437 if (!gimple_has_location (stmt
))
2438 location
= input_location
;
2440 location
= gimple_location (stmt
);
2442 warning_at (location
, OPT_Wtype_limits
,
2444 ? G_("comparison always false "
2445 "due to limited range of data type")
2446 : G_("comparison always true "
2447 "due to limited range of data type"));
2455 /* Visit conditional statement STMT. If we can determine which edge
2456 will be taken out of STMT's basic block, record it in
2457 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2460 vr_values::vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
2464 *taken_edge_p
= NULL
;
2466 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2471 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
2472 print_gimple_stmt (dump_file
, stmt
, 0);
2473 fprintf (dump_file
, "\nWith known ranges\n");
2475 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
2477 fprintf (dump_file
, "\t");
2478 print_generic_expr (dump_file
, use
);
2479 fprintf (dump_file
, ": ");
2480 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
2483 fprintf (dump_file
, "\n");
2486 /* Compute the value of the predicate COND by checking the known
2487 ranges of each of its operands.
2489 Note that we cannot evaluate all the equivalent ranges here
2490 because those ranges may not yet be final and with the current
2491 propagation strategy, we cannot determine when the value ranges
2492 of the names in the equivalence set have changed.
2494 For instance, given the following code fragment
2498 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2502 Assume that on the first visit to i_14, i_5 has the temporary
2503 range [8, 8] because the second argument to the PHI function is
2504 not yet executable. We derive the range ~[0, 0] for i_14 and the
2505 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2506 the first time, since i_14 is equivalent to the range [8, 8], we
2507 determine that the predicate is always false.
2509 On the next round of propagation, i_13 is determined to be
2510 VARYING, which causes i_5 to drop down to VARYING. So, another
2511 visit to i_14 is scheduled. In this second visit, we compute the
2512 exact same range and equivalence set for i_14, namely ~[0, 0] and
2513 { i_5 }. But we did not have the previous range for i_5
2514 registered, so vrp_visit_assignment thinks that the range for
2515 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2516 is not visited again, which stops propagation from visiting
2517 statements in the THEN clause of that if().
2519 To properly fix this we would need to keep the previous range
2520 value for the names in the equivalence set. This way we would've
2521 discovered that from one visit to the other i_5 changed from
2522 range [8, 8] to VR_VARYING.
2524 However, fixing this apparent limitation may not be worth the
2525 additional checking. Testing on several code bases (GCC, DLV,
2526 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2527 4 more predicates folded in SPEC. */
2530 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
2531 gimple_cond_lhs (stmt
),
2532 gimple_cond_rhs (stmt
),
2535 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
2537 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2539 fprintf (dump_file
, "\nPredicate evaluates to: ");
2540 if (val
== NULL_TREE
)
2541 fprintf (dump_file
, "DON'T KNOW\n");
2543 print_generic_stmt (dump_file
, val
);
2547 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2548 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2549 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2550 Returns true if the default label is not needed. */
2553 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
2554 size_t *max_idx1
, size_t *min_idx2
,
2558 unsigned int n
= gimple_switch_num_labels (stmt
);
2560 tree case_low
, case_high
;
2561 tree min
= vr
->min (), max
= vr
->max ();
2563 gcc_checking_assert (!vr
->varying_p () && !vr
->undefined_p ());
2565 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
2567 /* Set second range to emtpy. */
2571 if (vr
->kind () == VR_RANGE
)
2575 return !take_default
;
2578 /* Set first range to all case labels. */
2585 /* Make sure all the values of case labels [i , j] are contained in
2586 range [MIN, MAX]. */
2587 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
2588 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
2589 if (tree_int_cst_compare (case_low
, min
) < 0)
2591 if (case_high
!= NULL_TREE
2592 && tree_int_cst_compare (max
, case_high
) < 0)
2598 /* If the range spans case labels [i, j], the corresponding anti-range spans
2599 the labels [1, i - 1] and [j + 1, n - 1]. */
2625 /* Visit switch statement STMT. If we can determine which edge
2626 will be taken out of STMT's basic block, record it in
2627 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2630 vr_values::vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
2634 size_t i
= 0, j
= 0, k
, l
;
2637 *taken_edge_p
= NULL
;
2638 op
= gimple_switch_index (stmt
);
2639 if (TREE_CODE (op
) != SSA_NAME
)
2642 vr
= get_value_range (op
);
2643 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2645 fprintf (dump_file
, "\nVisiting switch expression with operand ");
2646 print_generic_expr (dump_file
, op
);
2647 fprintf (dump_file
, " with known range ");
2648 dump_value_range (dump_file
, vr
);
2649 fprintf (dump_file
, "\n");
2652 if (vr
->undefined_p ()
2654 || vr
->symbolic_p ())
2657 /* Find the single edge that is taken from the switch expression. */
2658 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
2660 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2664 gcc_assert (take_default
);
2665 val
= gimple_switch_default_label (stmt
);
2669 /* Check if labels with index i to j and maybe the default label
2670 are all reaching the same label. */
2672 val
= gimple_switch_label (stmt
, i
);
2674 && CASE_LABEL (gimple_switch_default_label (stmt
))
2675 != CASE_LABEL (val
))
2677 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2678 fprintf (dump_file
, " not a single destination for this "
2682 for (++i
; i
<= j
; ++i
)
2684 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
2686 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2687 fprintf (dump_file
, " not a single destination for this "
2694 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
2696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2697 fprintf (dump_file
, " not a single destination for this "
2704 *taken_edge_p
= find_edge (gimple_bb (stmt
),
2705 label_to_block (cfun
, CASE_LABEL (val
)));
2707 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2709 fprintf (dump_file
, " will take edge to ");
2710 print_generic_stmt (dump_file
, CASE_LABEL (val
));
2715 /* Evaluate statement STMT. If the statement produces a useful range,
2716 set VR and corepsponding OUTPUT_P.
2718 If STMT is a conditional branch and we can determine its truth
2719 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2722 vr_values::extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
2723 tree
*output_p
, value_range
*vr
)
2726 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2728 fprintf (dump_file
, "\nVisiting statement:\n");
2729 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2732 if (!stmt_interesting_for_vrp (stmt
))
2733 gcc_assert (stmt_ends_bb_p (stmt
));
2734 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
2735 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
2736 else if (gimple_code (stmt
) == GIMPLE_COND
)
2737 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
2738 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
2739 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
2742 /* Visit all arguments for PHI node PHI that flow through executable
2743 edges. If a valid value range can be derived from all the incoming
2744 value ranges, set a new range in VR_RESULT. */
2747 vr_values::extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
2750 tree lhs
= PHI_RESULT (phi
);
2751 value_range
*lhs_vr
= get_value_range (lhs
);
2753 int edges
, old_edges
;
2756 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2758 fprintf (dump_file
, "\nVisiting PHI node: ");
2759 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
2762 bool may_simulate_backedge_again
= false;
2764 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2766 edge e
= gimple_phi_arg_edge (phi
, i
);
2768 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2771 " Argument #%d (%d -> %d %sexecutable)\n",
2772 (int) i
, e
->src
->index
, e
->dest
->index
,
2773 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
2776 if (e
->flags
& EDGE_EXECUTABLE
)
2778 tree arg
= PHI_ARG_DEF (phi
, i
);
2779 value_range vr_arg_tem
;
2780 value_range
*vr_arg
= &vr_arg_tem
;
2784 if (TREE_CODE (arg
) == SSA_NAME
)
2786 /* See if we are eventually going to change one of the args. */
2787 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
2788 if (! gimple_nop_p (def_stmt
)
2789 && prop_simulate_again_p (def_stmt
)
2790 && e
->flags
& EDGE_DFS_BACK
)
2791 may_simulate_backedge_again
= true;
2793 value_range
*vr_arg_
= get_value_range (arg
);
2794 /* Do not allow equivalences or symbolic ranges to leak in from
2795 backedges. That creates invalid equivalencies.
2796 See PR53465 and PR54767. */
2797 if (e
->flags
& EDGE_DFS_BACK
)
2799 if (!vr_arg_
->varying_p () && !vr_arg_
->undefined_p ())
2801 vr_arg_tem
.set (vr_arg_
->kind (), vr_arg_
->min (),
2802 vr_arg_
->max (), NULL
);
2803 if (vr_arg_tem
.symbolic_p ())
2804 vr_arg_tem
.set_varying ();
2809 /* If the non-backedge arguments range is VR_VARYING then
2810 we can still try recording a simple equivalence. */
2811 else if (vr_arg_
->varying_p ())
2812 vr_arg_tem
.set (arg
);
2818 if (TREE_OVERFLOW_P (arg
))
2819 arg
= drop_tree_overflow (arg
);
2821 vr_arg_tem
.set (arg
);
2824 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2826 fprintf (dump_file
, "\t");
2827 print_generic_expr (dump_file
, arg
, dump_flags
);
2828 fprintf (dump_file
, ": ");
2829 dump_value_range (dump_file
, vr_arg
);
2830 fprintf (dump_file
, "\n");
2834 vr_result
->deep_copy (vr_arg
);
2836 vr_result
->union_ (vr_arg
);
2839 if (vr_result
->varying_p ())
2844 if (vr_result
->varying_p ())
2846 else if (vr_result
->undefined_p ())
2849 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
2850 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
2852 /* To prevent infinite iterations in the algorithm, derive ranges
2853 when the new value is slightly bigger or smaller than the
2854 previous one. We don't do this if we have seen a new executable
2855 edge; this helps us avoid an infinity for conditionals
2856 which are not in a loop. If the old value-range was VR_UNDEFINED
2857 use the updated range and iterate one more time. If we will not
2858 simulate this PHI again via the backedge allow us to iterate. */
2860 && gimple_phi_num_args (phi
) > 1
2861 && edges
== old_edges
2862 && !lhs_vr
->undefined_p ()
2863 && may_simulate_backedge_again
)
2865 /* Compare old and new ranges, fall back to varying if the
2866 values are not comparable. */
2867 int cmp_min
= compare_values (lhs_vr
->min (), vr_result
->min ());
2870 int cmp_max
= compare_values (lhs_vr
->max (), vr_result
->max ());
2874 /* For non VR_RANGE or for pointers fall back to varying if
2875 the range changed. */
2876 if ((lhs_vr
->kind () != VR_RANGE
|| vr_result
->kind () != VR_RANGE
2877 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2878 && (cmp_min
!= 0 || cmp_max
!= 0))
2881 /* If the new minimum is larger than the previous one
2882 retain the old value. If the new minimum value is smaller
2883 than the previous one and not -INF go all the way to -INF + 1.
2884 In the first case, to avoid infinite bouncing between different
2885 minimums, and in the other case to avoid iterating millions of
2886 times to reach -INF. Going to -INF + 1 also lets the following
2887 iteration compute whether there will be any overflow, at the
2888 expense of one additional iteration. */
2889 tree new_min
= vr_result
->min ();
2890 tree new_max
= vr_result
->max ();
2892 new_min
= lhs_vr
->min ();
2893 else if (cmp_min
> 0
2894 && (TREE_CODE (vr_result
->min ()) != INTEGER_CST
2895 || tree_int_cst_lt (vrp_val_min (vr_result
->type ()),
2896 vr_result
->min ())))
2897 new_min
= int_const_binop (PLUS_EXPR
,
2898 vrp_val_min (vr_result
->type ()),
2899 build_int_cst (vr_result
->type (), 1));
2901 /* Similarly for the maximum value. */
2903 new_max
= lhs_vr
->max ();
2904 else if (cmp_max
< 0
2905 && (TREE_CODE (vr_result
->max ()) != INTEGER_CST
2906 || tree_int_cst_lt (vr_result
->max (),
2907 vrp_val_max (vr_result
->type ()))))
2908 new_max
= int_const_binop (MINUS_EXPR
,
2909 vrp_val_max (vr_result
->type ()),
2910 build_int_cst (vr_result
->type (), 1));
2912 vr_result
->update (vr_result
->kind (), new_min
, new_max
);
2914 /* If we dropped either bound to +-INF then if this is a loop
2915 PHI node SCEV may known more about its value-range. */
2916 if (cmp_min
> 0 || cmp_min
< 0
2917 || cmp_max
< 0 || cmp_max
> 0)
2920 goto infinite_check
;
2926 vr_result
->set_varying ();
2929 /* If this is a loop PHI node SCEV may known more about its value-range.
2930 scev_check can be reached from two paths, one is a fall through from above
2931 "varying" label, the other is direct goto from code block which tries to
2932 avoid infinite simulation. */
2933 if (scev_initialized_p ()
2934 && (l
= loop_containing_stmt (phi
))
2935 && l
->header
== gimple_bb (phi
))
2936 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
2939 /* If we will end up with a (-INF, +INF) range, set it to
2940 VARYING. Same if the previous max value was invalid for
2941 the type and we end up with vr_result.min > vr_result.max. */
2942 if ((!vr_result
->varying_p () && !vr_result
->undefined_p ())
2943 && !((vrp_val_is_max (vr_result
->max ()) && vrp_val_is_min (vr_result
->min ()))
2944 || compare_values (vr_result
->min (), vr_result
->max ()) > 0))
2947 vr_result
->set_varying ();
2949 /* If the new range is different than the previous value, keep
2955 /* Simplify boolean operations if the source is known
2956 to be already a boolean. */
2958 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
,
2961 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
2963 bool need_conversion
;
2965 /* We handle only !=/== case here. */
2966 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
2968 op0
= gimple_assign_rhs1 (stmt
);
2969 if (!op_with_boolean_value_range_p (op0
))
2972 op1
= gimple_assign_rhs2 (stmt
);
2973 if (!op_with_boolean_value_range_p (op1
))
2976 /* Reduce number of cases to handle to NE_EXPR. As there is no
2977 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
2978 if (rhs_code
== EQ_EXPR
)
2980 if (TREE_CODE (op1
) == INTEGER_CST
)
2981 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
2982 build_int_cst (TREE_TYPE (op1
), 1));
2987 lhs
= gimple_assign_lhs (stmt
);
2989 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
2991 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
2993 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
2994 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
2995 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
2998 /* For A != 0 we can substitute A itself. */
2999 if (integer_zerop (op1
))
3000 gimple_assign_set_rhs_with_ops (gsi
,
3002 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
3003 /* For A != B we substitute A ^ B. Either with conversion. */
3004 else if (need_conversion
)
3006 tree tem
= make_ssa_name (TREE_TYPE (op0
));
3008 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
3009 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
3010 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
3011 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
3012 set_range_info (tem
, VR_RANGE
,
3013 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
3014 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
3015 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
3019 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
3020 update_stmt (gsi_stmt (*gsi
));
3021 fold_stmt (gsi
, follow_single_use_edges
);
3026 /* Simplify a division or modulo operator to a right shift or bitwise and
3027 if the first operand is unsigned or is greater than zero and the second
3028 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3029 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3030 optimize it into just op0 if op0's range is known to be a subset of
3031 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3035 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
,
3038 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3040 tree op0
= gimple_assign_rhs1 (stmt
);
3041 tree op1
= gimple_assign_rhs2 (stmt
);
3042 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
3044 value_range
*vr
= NULL
;
3046 if (TREE_CODE (op0
) == INTEGER_CST
)
3053 vr
= get_value_range (op0
);
3054 if (range_int_cst_p (vr
))
3056 op0min
= vr
->min ();
3057 op0max
= vr
->max ();
3061 if (rhs_code
== TRUNC_MOD_EXPR
3062 && TREE_CODE (op1
) == SSA_NAME
)
3064 value_range
*vr1
= get_value_range (op1
);
3065 if (range_int_cst_p (vr1
))
3066 op1min
= vr1
->min ();
3068 if (rhs_code
== TRUNC_MOD_EXPR
3069 && TREE_CODE (op1min
) == INTEGER_CST
3070 && tree_int_cst_sgn (op1min
) == 1
3072 && tree_int_cst_lt (op0max
, op1min
))
3074 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
3075 || tree_int_cst_sgn (op0min
) >= 0
3076 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
3079 /* If op0 already has the range op0 % op1 has,
3080 then TRUNC_MOD_EXPR won't change anything. */
3081 gimple_assign_set_rhs_from_tree (gsi
, op0
);
3086 if (TREE_CODE (op0
) != SSA_NAME
)
3089 if (!integer_pow2p (op1
))
3091 /* X % -Y can be only optimized into X % Y either if
3092 X is not INT_MIN, or Y is not -1. Fold it now, as after
3093 remove_range_assertions the range info might be not available
3095 if (rhs_code
== TRUNC_MOD_EXPR
3096 && fold_stmt (gsi
, follow_single_use_edges
))
3101 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
3102 val
= integer_one_node
;
3107 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
3111 && integer_onep (val
)
3112 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3114 location_t location
;
3116 if (!gimple_has_location (stmt
))
3117 location
= input_location
;
3119 location
= gimple_location (stmt
);
3120 warning_at (location
, OPT_Wstrict_overflow
,
3121 "assuming signed overflow does not occur when "
3122 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3126 if (val
&& integer_onep (val
))
3130 if (rhs_code
== TRUNC_DIV_EXPR
)
3132 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
3133 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
3134 gimple_assign_set_rhs1 (stmt
, op0
);
3135 gimple_assign_set_rhs2 (stmt
, t
);
3139 t
= build_int_cst (TREE_TYPE (op1
), 1);
3140 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
3141 t
= fold_convert (TREE_TYPE (op0
), t
);
3143 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
3144 gimple_assign_set_rhs1 (stmt
, op0
);
3145 gimple_assign_set_rhs2 (stmt
, t
);
3149 fold_stmt (gsi
, follow_single_use_edges
);
3156 /* Simplify a min or max if the ranges of the two operands are
3157 disjoint. Return true if we do simplify. */
3160 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
,
3163 tree op0
= gimple_assign_rhs1 (stmt
);
3164 tree op1
= gimple_assign_rhs2 (stmt
);
3168 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3169 (LE_EXPR
, op0
, op1
, &sop
));
3173 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3174 (LT_EXPR
, op0
, op1
, &sop
));
3179 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3181 location_t location
;
3183 if (!gimple_has_location (stmt
))
3184 location
= input_location
;
3186 location
= gimple_location (stmt
);
3187 warning_at (location
, OPT_Wstrict_overflow
,
3188 "assuming signed overflow does not occur when "
3189 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3192 /* VAL == TRUE -> OP0 < or <= op1
3193 VAL == FALSE -> OP0 > or >= op1. */
3194 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
3195 == integer_zerop (val
)) ? op0
: op1
;
3196 gimple_assign_set_rhs_from_tree (gsi
, res
);
3203 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3204 ABS_EXPR. If the operand is <= 0, then simplify the
3205 ABS_EXPR into a NEGATE_EXPR. */
3208 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3210 tree op
= gimple_assign_rhs1 (stmt
);
3211 value_range
*vr
= get_value_range (op
);
3218 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
3221 /* The range is neither <= 0 nor > 0. Now see if it is
3222 either < 0 or >= 0. */
3224 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
3230 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3232 location_t location
;
3234 if (!gimple_has_location (stmt
))
3235 location
= input_location
;
3237 location
= gimple_location (stmt
);
3238 warning_at (location
, OPT_Wstrict_overflow
,
3239 "assuming signed overflow does not occur when "
3240 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3243 gimple_assign_set_rhs1 (stmt
, op
);
3244 if (integer_zerop (val
))
3245 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
3247 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
3249 fold_stmt (gsi
, follow_single_use_edges
);
3257 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3258 If all the bits that are being cleared by & are already
3259 known to be zero from VR, or all the bits that are being
3260 set by | are already known to be one from VR, the bit
3261 operation is redundant. */
3264 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
,
3267 tree op0
= gimple_assign_rhs1 (stmt
);
3268 tree op1
= gimple_assign_rhs2 (stmt
);
3269 tree op
= NULL_TREE
;
3270 value_range_base vr0
, vr1
;
3271 wide_int may_be_nonzero0
, may_be_nonzero1
;
3272 wide_int must_be_nonzero0
, must_be_nonzero1
;
3275 if (TREE_CODE (op0
) == SSA_NAME
)
3276 vr0
= *(get_value_range (op0
));
3277 else if (is_gimple_min_invariant (op0
))
3282 if (TREE_CODE (op1
) == SSA_NAME
)
3283 vr1
= *(get_value_range (op1
));
3284 else if (is_gimple_min_invariant (op1
))
3289 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
3292 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
3296 switch (gimple_assign_rhs_code (stmt
))
3299 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3305 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3313 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3319 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3330 if (op
== NULL_TREE
)
3333 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
3334 update_stmt (gsi_stmt (*gsi
));
3338 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3339 a known value range VR.
3341 If there is one and only one value which will satisfy the
3342 conditional, then return that value. Else return NULL.
3344 If signed overflow must be undefined for the value to satisfy
3345 the conditional, then set *STRICT_OVERFLOW_P to true. */
3348 test_for_singularity (enum tree_code cond_code
, tree op0
,
3349 tree op1
, value_range
*vr
)
3354 /* Extract minimum/maximum values which satisfy the conditional as it was
3356 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
3358 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
3361 if (cond_code
== LT_EXPR
)
3363 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3364 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
3365 /* Signal to compare_values_warnv this expr doesn't overflow. */
3367 TREE_NO_WARNING (max
) = 1;
3370 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
3372 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
3375 if (cond_code
== GT_EXPR
)
3377 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3378 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
3379 /* Signal to compare_values_warnv this expr doesn't overflow. */
3381 TREE_NO_WARNING (min
) = 1;
3385 /* Now refine the minimum and maximum values using any
3386 value range information we have for op0. */
3389 if (compare_values (vr
->min (), min
) == 1)
3391 if (compare_values (vr
->max (), max
) == -1)
3394 /* If the new min/max values have converged to a single value,
3395 then there is only one value which can satisfy the condition,
3396 return that value. */
3397 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
3403 /* Return whether the value range *VR fits in an integer type specified
3404 by PRECISION and UNSIGNED_P. */
3407 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
3410 unsigned src_precision
;
3414 /* We can only handle integral and pointer types. */
3415 src_type
= vr
->type ();
3416 if (!INTEGRAL_TYPE_P (src_type
)
3417 && !POINTER_TYPE_P (src_type
))
3420 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3421 and so is an identity transform. */
3422 src_precision
= TYPE_PRECISION (vr
->type ());
3423 src_sgn
= TYPE_SIGN (src_type
);
3424 if ((src_precision
< dest_precision
3425 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
3426 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
3429 /* Now we can only handle ranges with constant bounds. */
3430 if (!range_int_cst_p (vr
))
3433 /* For sign changes, the MSB of the wide_int has to be clear.
3434 An unsigned value with its MSB set cannot be represented by
3435 a signed wide_int, while a negative value cannot be represented
3436 by an unsigned wide_int. */
3437 if (src_sgn
!= dest_sgn
3438 && (wi::lts_p (wi::to_wide (vr
->min ()), 0)
3439 || wi::lts_p (wi::to_wide (vr
->max ()), 0)))
3442 /* Then we can perform the conversion on both ends and compare
3443 the result for equality. */
3444 tem
= wi::ext (wi::to_widest (vr
->min ()), dest_precision
, dest_sgn
);
3445 if (tem
!= wi::to_widest (vr
->min ()))
3447 tem
= wi::ext (wi::to_widest (vr
->max ()), dest_precision
, dest_sgn
);
3448 if (tem
!= wi::to_widest (vr
->max ()))
3454 /* Simplify a conditional using a relational operator to an equality
3455 test if the range information indicates only one value can satisfy
3456 the original conditional. */
3459 vr_values::simplify_cond_using_ranges_1 (gcond
*stmt
)
3461 tree op0
= gimple_cond_lhs (stmt
);
3462 tree op1
= gimple_cond_rhs (stmt
);
3463 enum tree_code cond_code
= gimple_cond_code (stmt
);
3465 if (cond_code
!= NE_EXPR
3466 && cond_code
!= EQ_EXPR
3467 && TREE_CODE (op0
) == SSA_NAME
3468 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3469 && is_gimple_min_invariant (op1
))
3471 value_range
*vr
= get_value_range (op0
);
3473 /* If we have range information for OP0, then we might be
3474 able to simplify this conditional. */
3475 if (vr
->kind () == VR_RANGE
)
3477 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
3482 fprintf (dump_file
, "Simplified relational ");
3483 print_gimple_stmt (dump_file
, stmt
, 0);
3484 fprintf (dump_file
, " into ");
3487 gimple_cond_set_code (stmt
, EQ_EXPR
);
3488 gimple_cond_set_lhs (stmt
, op0
);
3489 gimple_cond_set_rhs (stmt
, new_tree
);
3495 print_gimple_stmt (dump_file
, stmt
, 0);
3496 fprintf (dump_file
, "\n");
3502 /* Try again after inverting the condition. We only deal
3503 with integral types here, so no need to worry about
3504 issues with inverting FP comparisons. */
3505 new_tree
= test_for_singularity
3506 (invert_tree_comparison (cond_code
, false),
3512 fprintf (dump_file
, "Simplified relational ");
3513 print_gimple_stmt (dump_file
, stmt
, 0);
3514 fprintf (dump_file
, " into ");
3517 gimple_cond_set_code (stmt
, NE_EXPR
);
3518 gimple_cond_set_lhs (stmt
, op0
);
3519 gimple_cond_set_rhs (stmt
, new_tree
);
3525 print_gimple_stmt (dump_file
, stmt
, 0);
3526 fprintf (dump_file
, "\n");
3536 /* STMT is a conditional at the end of a basic block.
3538 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3539 was set via a type conversion, try to replace the SSA_NAME with the RHS
3540 of the type conversion. Doing so makes the conversion dead which helps
3541 subsequent passes. */
3544 vr_values::simplify_cond_using_ranges_2 (gcond
*stmt
)
3546 tree op0
= gimple_cond_lhs (stmt
);
3547 tree op1
= gimple_cond_rhs (stmt
);
3549 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3550 see if OP0 was set by a type conversion where the source of
3551 the conversion is another SSA_NAME with a range that fits
3552 into the range of OP0's type.
3554 If so, the conversion is redundant as the earlier SSA_NAME can be
3555 used for the comparison directly if we just massage the constant in the
3557 if (TREE_CODE (op0
) == SSA_NAME
3558 && TREE_CODE (op1
) == INTEGER_CST
)
3560 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
3563 if (!is_gimple_assign (def_stmt
)
3564 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3567 innerop
= gimple_assign_rhs1 (def_stmt
);
3569 if (TREE_CODE (innerop
) == SSA_NAME
3570 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
3571 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
3572 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
3574 value_range
*vr
= get_value_range (innerop
);
3576 if (range_int_cst_p (vr
)
3577 && range_fits_type_p (vr
,
3578 TYPE_PRECISION (TREE_TYPE (op0
)),
3579 TYPE_SIGN (TREE_TYPE (op0
)))
3580 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
3582 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
3583 gimple_cond_set_lhs (stmt
, innerop
);
3584 gimple_cond_set_rhs (stmt
, newconst
);
3586 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3588 fprintf (dump_file
, "Folded into: ");
3589 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3590 fprintf (dump_file
, "\n");
3597 /* Simplify a switch statement using the value range of the switch
3601 vr_values::simplify_switch_using_ranges (gswitch
*stmt
)
3603 tree op
= gimple_switch_index (stmt
);
3604 value_range
*vr
= NULL
;
3608 size_t i
= 0, j
= 0, n
, n2
;
3611 size_t k
= 1, l
= 0;
3613 if (TREE_CODE (op
) == SSA_NAME
)
3615 vr
= get_value_range (op
);
3617 /* We can only handle integer ranges. */
3618 if (vr
->varying_p ()
3619 || vr
->undefined_p ()
3620 || vr
->symbolic_p ())
3623 /* Find case label for min/max of the value range. */
3624 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
3626 else if (TREE_CODE (op
) == INTEGER_CST
)
3628 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
3642 n
= gimple_switch_num_labels (stmt
);
3644 /* We can truncate the case label ranges that partially overlap with OP's
3646 size_t min_idx
= 1, max_idx
= 0;
3648 find_case_label_range (stmt
, vr
->min (), vr
->max (), &min_idx
, &max_idx
);
3649 if (min_idx
<= max_idx
)
3651 tree min_label
= gimple_switch_label (stmt
, min_idx
);
3652 tree max_label
= gimple_switch_label (stmt
, max_idx
);
3654 /* Avoid changing the type of the case labels when truncating. */
3655 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
3656 tree vr_min
= fold_convert (case_label_type
, vr
->min ());
3657 tree vr_max
= fold_convert (case_label_type
, vr
->max ());
3659 if (vr
->kind () == VR_RANGE
)
3661 /* If OP's value range is [2,8] and the low label range is
3662 0 ... 3, truncate the label's range to 2 .. 3. */
3663 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3664 && CASE_HIGH (min_label
) != NULL_TREE
3665 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3666 CASE_LOW (min_label
) = vr_min
;
3668 /* If OP's value range is [2,8] and the high label range is
3669 7 ... 10, truncate the label's range to 7 .. 8. */
3670 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3671 && CASE_HIGH (max_label
) != NULL_TREE
3672 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3673 CASE_HIGH (max_label
) = vr_max
;
3675 else if (vr
->kind () == VR_ANTI_RANGE
)
3677 tree one_cst
= build_one_cst (case_label_type
);
3679 if (min_label
== max_label
)
3681 /* If OP's value range is ~[7,8] and the label's range is
3682 7 ... 10, truncate the label's range to 9 ... 10. */
3683 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
3684 && CASE_HIGH (min_label
) != NULL_TREE
3685 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
3686 CASE_LOW (min_label
)
3687 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3689 /* If OP's value range is ~[7,8] and the label's range is
3690 5 ... 8, truncate the label's range to 5 ... 6. */
3691 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3692 && CASE_HIGH (min_label
) != NULL_TREE
3693 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
3694 CASE_HIGH (min_label
)
3695 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3699 /* If OP's value range is ~[2,8] and the low label range is
3700 0 ... 3, truncate the label's range to 0 ... 1. */
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_HIGH (min_label
)
3705 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3707 /* If OP's value range is ~[2,8] and the high label range is
3708 7 ... 10, truncate the label's range to 9 ... 10. */
3709 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3710 && CASE_HIGH (max_label
) != NULL_TREE
3711 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3712 CASE_LOW (max_label
)
3713 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3717 /* Canonicalize singleton case ranges. */
3718 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
3719 CASE_HIGH (min_label
) = NULL_TREE
;
3720 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
3721 CASE_HIGH (max_label
) = NULL_TREE
;
3724 /* We can also eliminate case labels that lie completely outside OP's value
3727 /* Bail out if this is just all edges taken. */
3733 /* Build a new vector of taken case labels. */
3734 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
3737 /* Add the default edge, if necessary. */
3739 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
3741 for (; i
<= j
; ++i
, ++n2
)
3742 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
3744 for (; k
<= l
; ++k
, ++n2
)
3745 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
3747 /* Mark needed edges. */
3748 for (i
= 0; i
< n2
; ++i
)
3750 e
= find_edge (gimple_bb (stmt
),
3751 label_to_block (cfun
,
3752 CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
3753 e
->aux
= (void *)-1;
3756 /* Queue not needed edges for later removal. */
3757 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
3759 if (e
->aux
== (void *)-1)
3765 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3767 fprintf (dump_file
, "removing unreachable case label\n");
3769 to_remove_edges
.safe_push (e
);
3770 e
->flags
&= ~EDGE_EXECUTABLE
;
3771 e
->flags
|= EDGE_IGNORE
;
3774 /* And queue an update for the stmt. */
3777 to_update_switch_stmts
.safe_push (su
);
3782 vr_values::cleanup_edges_and_switches (void)
3788 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3789 CFG in a broken state and requires a cfg_cleanup run. */
3790 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
3793 /* Update SWITCH_EXPR case label vector. */
3794 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
3797 size_t n
= TREE_VEC_LENGTH (su
->vec
);
3799 gimple_switch_set_num_labels (su
->stmt
, n
);
3800 for (j
= 0; j
< n
; j
++)
3801 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
3802 /* As we may have replaced the default label with a regular one
3803 make sure to make it a real default label again. This ensures
3804 optimal expansion. */
3805 label
= gimple_switch_label (su
->stmt
, 0);
3806 CASE_LOW (label
) = NULL_TREE
;
3807 CASE_HIGH (label
) = NULL_TREE
;
3810 if (!to_remove_edges
.is_empty ())
3812 free_dominance_info (CDI_DOMINATORS
);
3813 loops_state_set (LOOPS_NEED_FIXUP
);
3816 to_remove_edges
.release ();
3817 to_update_switch_stmts
.release ();
3820 /* Simplify an integral conversion from an SSA name in STMT. */
3823 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3825 tree innerop
, middleop
, finaltype
;
3827 signop inner_sgn
, middle_sgn
, final_sgn
;
3828 unsigned inner_prec
, middle_prec
, final_prec
;
3829 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
3831 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
3832 if (!INTEGRAL_TYPE_P (finaltype
))
3834 middleop
= gimple_assign_rhs1 (stmt
);
3835 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
3836 if (!is_gimple_assign (def_stmt
)
3837 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3839 innerop
= gimple_assign_rhs1 (def_stmt
);
3840 if (TREE_CODE (innerop
) != SSA_NAME
3841 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
3844 /* Get the value-range of the inner operand. Use get_range_info in
3845 case innerop was created during substitute-and-fold. */
3846 wide_int imin
, imax
;
3847 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
3848 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
3850 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
3851 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
3853 /* Simulate the conversion chain to check if the result is equal if
3854 the middle conversion is removed. */
3855 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
3856 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
3857 final_prec
= TYPE_PRECISION (finaltype
);
3859 /* If the first conversion is not injective, the second must not
3861 if (wi::gtu_p (innermax
- innermin
,
3862 wi::mask
<widest_int
> (middle_prec
, false))
3863 && middle_prec
< final_prec
)
3865 /* We also want a medium value so that we can track the effect that
3866 narrowing conversions with sign change have. */
3867 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
3868 if (inner_sgn
== UNSIGNED
)
3869 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
3872 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
3873 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
3874 innermed
= innermin
;
3876 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
3877 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
3878 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
3879 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
3881 /* Require that the final conversion applied to both the original
3882 and the intermediate range produces the same result. */
3883 final_sgn
= TYPE_SIGN (finaltype
);
3884 if (wi::ext (middlemin
, final_prec
, final_sgn
)
3885 != wi::ext (innermin
, final_prec
, final_sgn
)
3886 || wi::ext (middlemed
, final_prec
, final_sgn
)
3887 != wi::ext (innermed
, final_prec
, final_sgn
)
3888 || wi::ext (middlemax
, final_prec
, final_sgn
)
3889 != wi::ext (innermax
, final_prec
, final_sgn
))
3892 gimple_assign_set_rhs1 (stmt
, innerop
);
3893 fold_stmt (gsi
, follow_single_use_edges
);
3897 /* Simplify a conversion from integral SSA name to float in STMT. */
3900 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
3903 tree rhs1
= gimple_assign_rhs1 (stmt
);
3904 value_range
*vr
= get_value_range (rhs1
);
3905 scalar_float_mode fltmode
3906 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
3907 scalar_int_mode mode
;
3911 /* We can only handle constant ranges. */
3912 if (!range_int_cst_p (vr
))
3915 /* First check if we can use a signed type in place of an unsigned. */
3916 scalar_int_mode rhs_mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1
));
3917 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
3918 && can_float_p (fltmode
, rhs_mode
, 0) != CODE_FOR_nothing
3919 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
3921 /* If we can do the conversion in the current input mode do nothing. */
3922 else if (can_float_p (fltmode
, rhs_mode
,
3923 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
3925 /* Otherwise search for a mode we can use, starting from the narrowest
3926 integer mode available. */
3929 mode
= NARROWEST_INT_MODE
;
3932 /* If we cannot do a signed conversion to float from mode
3933 or if the value-range does not fit in the signed type
3934 try with a wider mode. */
3935 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
3936 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
3939 /* But do not widen the input. Instead leave that to the
3940 optabs expansion code. */
3941 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
3942 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
3947 /* It works, insert a truncation or sign-change before the
3948 float conversion. */
3949 tem
= make_ssa_name (build_nonstandard_integer_type
3950 (GET_MODE_PRECISION (mode
), 0));
3951 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
3952 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
3953 gimple_assign_set_rhs1 (stmt
, tem
);
3954 fold_stmt (gsi
, follow_single_use_edges
);
3959 /* Simplify an internal fn call using ranges if possible. */
3962 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
,
3965 enum tree_code subcode
;
3966 bool is_ubsan
= false;
3968 switch (gimple_call_internal_fn (stmt
))
3970 case IFN_UBSAN_CHECK_ADD
:
3971 subcode
= PLUS_EXPR
;
3974 case IFN_UBSAN_CHECK_SUB
:
3975 subcode
= MINUS_EXPR
;
3978 case IFN_UBSAN_CHECK_MUL
:
3979 subcode
= MULT_EXPR
;
3982 case IFN_ADD_OVERFLOW
:
3983 subcode
= PLUS_EXPR
;
3985 case IFN_SUB_OVERFLOW
:
3986 subcode
= MINUS_EXPR
;
3988 case IFN_MUL_OVERFLOW
:
3989 subcode
= MULT_EXPR
;
3995 tree op0
= gimple_call_arg (stmt
, 0);
3996 tree op1
= gimple_call_arg (stmt
, 1);
4000 type
= TREE_TYPE (op0
);
4001 if (VECTOR_TYPE_P (type
))
4004 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
4007 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
4008 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
4009 || (is_ubsan
&& ovf
))
4013 location_t loc
= gimple_location (stmt
);
4015 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
4018 int prec
= TYPE_PRECISION (type
);
4021 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
4022 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
4023 utype
= build_nonstandard_integer_type (prec
, 1);
4024 if (TREE_CODE (op0
) == INTEGER_CST
)
4025 op0
= fold_convert (utype
, op0
);
4026 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
4028 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
4029 gimple_set_location (g
, loc
);
4030 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4031 op0
= gimple_assign_lhs (g
);
4033 if (TREE_CODE (op1
) == INTEGER_CST
)
4034 op1
= fold_convert (utype
, op1
);
4035 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
4037 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
4038 gimple_set_location (g
, loc
);
4039 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4040 op1
= gimple_assign_lhs (g
);
4042 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
4043 gimple_set_location (g
, loc
);
4044 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4047 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
4048 gimple_assign_lhs (g
));
4049 gimple_set_location (g
, loc
);
4050 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4052 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
4053 gimple_assign_lhs (g
),
4054 build_int_cst (type
, ovf
));
4056 gimple_set_location (g
, loc
);
4057 gsi_replace (gsi
, g
, false);
4061 /* Return true if VAR is a two-valued variable. Set a and b with the
4062 two-values when it is true. Return false otherwise. */
4065 vr_values::two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
4067 value_range
*vr
= get_value_range (var
);
4068 if (vr
->varying_p ()
4069 || vr
->undefined_p ()
4070 || TREE_CODE (vr
->min ()) != INTEGER_CST
4071 || TREE_CODE (vr
->max ()) != INTEGER_CST
)
4074 if (vr
->kind () == VR_RANGE
4075 && wi::to_wide (vr
->max ()) - wi::to_wide (vr
->min ()) == 1)
4082 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4083 if (vr
->kind () == VR_ANTI_RANGE
4084 && (wi::to_wide (vr
->min ())
4085 - wi::to_wide (vrp_val_min (TREE_TYPE (var
)))) == 1
4086 && (wi::to_wide (vrp_val_max (TREE_TYPE (var
)))
4087 - wi::to_wide (vr
->max ())) == 1)
4089 *a
= vrp_val_min (TREE_TYPE (var
));
4090 *b
= vrp_val_max (TREE_TYPE (var
));
4097 /* Simplify STMT using ranges if possible. */
4100 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
4102 gimple
*stmt
= gsi_stmt (*gsi
);
4103 if (is_gimple_assign (stmt
))
4105 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
4106 tree rhs1
= gimple_assign_rhs1 (stmt
);
4107 tree rhs2
= gimple_assign_rhs2 (stmt
);
4108 tree lhs
= gimple_assign_lhs (stmt
);
4109 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
4110 use_operand_p use_p
;
4115 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4117 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4121 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4123 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4125 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
4126 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
4127 && ((TREE_CODE (rhs1
) == INTEGER_CST
4128 && TREE_CODE (rhs2
) == SSA_NAME
)
4129 || (TREE_CODE (rhs2
) == INTEGER_CST
4130 && TREE_CODE (rhs1
) == SSA_NAME
))
4131 && single_imm_use (lhs
, &use_p
, &use_stmt
)
4132 && gimple_code (use_stmt
) == GIMPLE_COND
)
4135 tree new_rhs1
= NULL_TREE
;
4136 tree new_rhs2
= NULL_TREE
;
4137 tree cmp_var
= NULL_TREE
;
4139 if (TREE_CODE (rhs2
) == SSA_NAME
4140 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
4142 /* Optimize RHS1 OP [VAL1, VAL2]. */
4143 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
4144 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
4147 else if (TREE_CODE (rhs1
) == SSA_NAME
4148 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
4150 /* Optimize [VAL1, VAL2] OP RHS2. */
4151 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
4152 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
4156 /* If we could not find two-vals or the optimzation is invalid as
4157 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4158 if (new_rhs1
&& new_rhs2
)
4160 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
4161 gimple_assign_set_rhs_with_ops (gsi
,
4165 update_stmt (gsi_stmt (*gsi
));
4166 fold_stmt (gsi
, follow_single_use_edges
);
4175 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4176 if the RHS is zero or one, and the LHS are known to be boolean
4178 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4179 return simplify_truth_ops_using_ranges (gsi
, stmt
);
4182 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4183 and BIT_AND_EXPR respectively if the first operand is greater
4184 than zero and the second operand is an exact power of two.
4185 Also optimize TRUNC_MOD_EXPR away if the second operand is
4186 constant and the first operand already has the right value
4188 case TRUNC_DIV_EXPR
:
4189 case TRUNC_MOD_EXPR
:
4190 if ((TREE_CODE (rhs1
) == SSA_NAME
4191 || TREE_CODE (rhs1
) == INTEGER_CST
)
4192 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4193 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
4196 /* Transform ABS (X) into X or -X as appropriate. */
4198 if (TREE_CODE (rhs1
) == SSA_NAME
4199 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4200 return simplify_abs_using_ranges (gsi
, stmt
);
4205 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4206 if all the bits being cleared are already cleared or
4207 all the bits being set are already set. */
4208 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4209 return simplify_bit_ops_using_ranges (gsi
, stmt
);
4213 if (TREE_CODE (rhs1
) == SSA_NAME
4214 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4215 return simplify_conversion_using_ranges (gsi
, stmt
);
4219 if (TREE_CODE (rhs1
) == SSA_NAME
4220 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4221 return simplify_float_conversion_using_ranges (gsi
, stmt
);
4226 return simplify_min_or_max_using_ranges (gsi
, stmt
);
4232 else if (gimple_code (stmt
) == GIMPLE_COND
)
4233 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
4234 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
4235 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
4236 else if (is_gimple_call (stmt
)
4237 && gimple_call_internal_p (stmt
))
4238 return simplify_internal_call_using_ranges (gsi
, stmt
);
4244 vr_values::set_vr_value (tree var
, value_range
*vr
)
4246 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
4248 vr_value
[SSA_NAME_VERSION (var
)] = vr
;