i386: move alignment defaults to processor_costs.
[official-gcc.git] / gcc / vr-values.c
blob11df1023b6efa3618c670122a396f0cb67f0d94b
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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "insn-codes.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "ssa.h"
28 #include "optabs-tree.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
31 #include "flags.h"
32 #include "fold-const.h"
33 #include "calls.h"
34 #include "cfganal.h"
35 #include "gimple-fold.h"
36 #include "gimple-iterator.h"
37 #include "tree-cfg.h"
38 #include "tree-ssa-loop-niter.h"
39 #include "tree-ssa-loop.h"
40 #include "intl.h"
41 #include "cfgloop.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"
48 #include "attribs.h"
49 #include "vr-values.h"
51 /* Set value range VR to a non-negative range of type TYPE. */
53 static inline void
54 set_value_range_to_nonnegative (value_range *vr, tree type)
56 tree zero = build_int_cst (type, 0);
57 set_value_range (vr, VR_RANGE, zero, vrp_val_max (type), vr->equiv);
60 /* Set value range VR to a range of a truthvalue of type TYPE. */
62 static inline void
63 set_value_range_to_truthvalue (value_range *vr, tree type)
65 if (TYPE_PRECISION (type) == 1)
66 set_value_range_to_varying (vr);
67 else
68 set_value_range (vr, VR_RANGE,
69 build_int_cst (type, 0), build_int_cst (type, 1),
70 vr->equiv);
74 /* Return value range information for VAR.
76 If we have no values ranges recorded (ie, VRP is not running), then
77 return NULL. Otherwise create an empty range if none existed for VAR. */
79 value_range *
80 vr_values::get_value_range (const_tree var)
82 static const value_range vr_const_varying
83 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
84 value_range *vr;
85 tree sym;
86 unsigned ver = SSA_NAME_VERSION (var);
88 /* If we have no recorded ranges, then return NULL. */
89 if (! vr_value)
90 return NULL;
92 /* If we query the range for a new SSA name return an unmodifiable VARYING.
93 We should get here at most from the substitute-and-fold stage which
94 will never try to change values. */
95 if (ver >= num_vr_values)
96 return CONST_CAST (value_range *, &vr_const_varying);
98 vr = vr_value[ver];
99 if (vr)
100 return vr;
102 /* After propagation finished do not allocate new value-ranges. */
103 if (values_propagated)
104 return CONST_CAST (value_range *, &vr_const_varying);
106 /* Create a default value range. */
107 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
108 memset (vr, 0, sizeof (*vr));
110 /* Defer allocating the equivalence set. */
111 vr->equiv = NULL;
113 /* If VAR is a default definition of a parameter, the variable can
114 take any value in VAR's type. */
115 if (SSA_NAME_IS_DEFAULT_DEF (var))
117 sym = SSA_NAME_VAR (var);
118 if (TREE_CODE (sym) == PARM_DECL)
120 /* Try to use the "nonnull" attribute to create ~[0, 0]
121 anti-ranges for pointers. Note that this is only valid with
122 default definitions of PARM_DECLs. */
123 if (POINTER_TYPE_P (TREE_TYPE (sym))
124 && (nonnull_arg_p (sym)
125 || get_ptr_nonnull (var)))
126 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
127 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
129 wide_int min, max;
130 value_range_type rtype = get_range_info (var, &min, &max);
131 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
132 set_value_range (vr, rtype,
133 wide_int_to_tree (TREE_TYPE (var), min),
134 wide_int_to_tree (TREE_TYPE (var), max),
135 NULL);
136 else
137 set_value_range_to_varying (vr);
139 else
140 set_value_range_to_varying (vr);
142 else if (TREE_CODE (sym) == RESULT_DECL
143 && DECL_BY_REFERENCE (sym))
144 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
147 return vr;
150 /* Set value-ranges of all SSA names defined by STMT to varying. */
152 void
153 vr_values::set_defs_to_varying (gimple *stmt)
155 ssa_op_iter i;
156 tree def;
157 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
159 value_range *vr = get_value_range (def);
160 /* Avoid writing to vr_const_varying get_value_range may return. */
161 if (vr->type != VR_VARYING)
162 set_value_range_to_varying (vr);
166 /* Update the value range and equivalence set for variable VAR to
167 NEW_VR. Return true if NEW_VR is different from VAR's previous
168 value.
170 NOTE: This function assumes that NEW_VR is a temporary value range
171 object created for the sole purpose of updating VAR's range. The
172 storage used by the equivalence set from NEW_VR will be freed by
173 this function. Do not call update_value_range when NEW_VR
174 is the range object associated with another SSA name. */
176 bool
177 vr_values::update_value_range (const_tree var, value_range *new_vr)
179 value_range *old_vr;
180 bool is_new;
182 /* If there is a value-range on the SSA name from earlier analysis
183 factor that in. */
184 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
186 wide_int min, max;
187 value_range_type rtype = get_range_info (var, &min, &max);
188 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
190 tree nr_min, nr_max;
191 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
192 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
193 value_range nr = VR_INITIALIZER;
194 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
195 vrp_intersect_ranges (new_vr, &nr);
199 /* Update the value range, if necessary. */
200 old_vr = get_value_range (var);
201 is_new = old_vr->type != new_vr->type
202 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
203 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
204 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
206 if (is_new)
208 /* Do not allow transitions up the lattice. The following
209 is slightly more awkward than just new_vr->type < old_vr->type
210 because VR_RANGE and VR_ANTI_RANGE need to be considered
211 the same. We may not have is_new when transitioning to
212 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
213 called. */
214 if (new_vr->type == VR_UNDEFINED)
216 BITMAP_FREE (new_vr->equiv);
217 set_value_range_to_varying (old_vr);
218 set_value_range_to_varying (new_vr);
219 return true;
221 else
222 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
223 new_vr->equiv);
226 BITMAP_FREE (new_vr->equiv);
228 return is_new;
232 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
233 point where equivalence processing can be turned on/off. */
235 void
236 vr_values::add_equivalence (bitmap *equiv, const_tree var)
238 unsigned ver = SSA_NAME_VERSION (var);
239 value_range *vr = get_value_range (var);
241 if (*equiv == NULL)
242 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
243 bitmap_set_bit (*equiv, ver);
244 if (vr && vr->equiv)
245 bitmap_ior_into (*equiv, vr->equiv);
248 /* Return true if value range VR involves exactly one symbol SYM. */
250 static bool
251 symbolic_range_based_on_p (value_range *vr, const_tree sym)
253 bool neg, min_has_symbol, max_has_symbol;
254 tree inv;
256 if (is_gimple_min_invariant (vr->min))
257 min_has_symbol = false;
258 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
259 min_has_symbol = true;
260 else
261 return false;
263 if (is_gimple_min_invariant (vr->max))
264 max_has_symbol = false;
265 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
266 max_has_symbol = true;
267 else
268 return false;
270 return (min_has_symbol || max_has_symbol);
273 /* Return true if the result of assignment STMT is know to be non-zero. */
275 static bool
276 gimple_assign_nonzero_p (gimple *stmt)
278 enum tree_code code = gimple_assign_rhs_code (stmt);
279 bool strict_overflow_p;
280 switch (get_gimple_rhs_class (code))
282 case GIMPLE_UNARY_RHS:
283 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
284 gimple_expr_type (stmt),
285 gimple_assign_rhs1 (stmt),
286 &strict_overflow_p);
287 case GIMPLE_BINARY_RHS:
288 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
289 gimple_expr_type (stmt),
290 gimple_assign_rhs1 (stmt),
291 gimple_assign_rhs2 (stmt),
292 &strict_overflow_p);
293 case GIMPLE_TERNARY_RHS:
294 return false;
295 case GIMPLE_SINGLE_RHS:
296 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
297 &strict_overflow_p);
298 case GIMPLE_INVALID_RHS:
299 gcc_unreachable ();
300 default:
301 gcc_unreachable ();
305 /* Return true if STMT is known to compute a non-zero value. */
307 static bool
308 gimple_stmt_nonzero_p (gimple *stmt)
310 switch (gimple_code (stmt))
312 case GIMPLE_ASSIGN:
313 return gimple_assign_nonzero_p (stmt);
314 case GIMPLE_CALL:
316 gcall *call_stmt = as_a<gcall *> (stmt);
317 return (gimple_call_nonnull_result_p (call_stmt)
318 || gimple_call_nonnull_arg (call_stmt));
320 default:
321 gcc_unreachable ();
324 /* Like tree_expr_nonzero_p, but this function uses value ranges
325 obtained so far. */
327 bool
328 vr_values::vrp_stmt_computes_nonzero (gimple *stmt)
330 if (gimple_stmt_nonzero_p (stmt))
331 return true;
333 /* If we have an expression of the form &X->a, then the expression
334 is nonnull if X is nonnull. */
335 if (is_gimple_assign (stmt)
336 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
338 tree expr = gimple_assign_rhs1 (stmt);
339 tree base = get_base_address (TREE_OPERAND (expr, 0));
341 if (base != NULL_TREE
342 && TREE_CODE (base) == MEM_REF
343 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
345 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
346 if (!range_includes_zero_p (vr))
347 return true;
351 return false;
354 /* Returns true if EXPR is a valid value (as expected by compare_values) --
355 a gimple invariant, or SSA_NAME +- CST. */
357 static bool
358 valid_value_p (tree expr)
360 if (TREE_CODE (expr) == SSA_NAME)
361 return true;
363 if (TREE_CODE (expr) == PLUS_EXPR
364 || TREE_CODE (expr) == MINUS_EXPR)
365 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
366 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
368 return is_gimple_min_invariant (expr);
371 /* If OP has a value range with a single constant value return that,
372 otherwise return NULL_TREE. This returns OP itself if OP is a
373 constant. */
375 tree
376 vr_values::op_with_constant_singleton_value_range (tree op)
378 if (is_gimple_min_invariant (op))
379 return op;
381 if (TREE_CODE (op) != SSA_NAME)
382 return NULL_TREE;
384 return value_range_constant_singleton (get_value_range (op));
387 /* Return true if op is in a boolean [0, 1] value-range. */
389 bool
390 vr_values::op_with_boolean_value_range_p (tree op)
392 value_range *vr;
394 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
395 return true;
397 if (integer_zerop (op)
398 || integer_onep (op))
399 return true;
401 if (TREE_CODE (op) != SSA_NAME)
402 return false;
404 vr = get_value_range (op);
405 return (vr->type == VR_RANGE
406 && integer_zerop (vr->min)
407 && integer_onep (vr->max));
410 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
411 true and store it in *VR_P. */
413 void
414 vr_values::extract_range_for_var_from_comparison_expr (tree var,
415 enum tree_code cond_code,
416 tree op, tree limit,
417 value_range *vr_p)
419 tree min, max, type;
420 value_range *limit_vr;
421 type = TREE_TYPE (var);
423 /* For pointer arithmetic, we only keep track of pointer equality
424 and inequality. If we arrive here with unfolded conditions like
425 _1 > _1 do not derive anything. */
426 if ((POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
427 || limit == var)
429 set_value_range_to_varying (vr_p);
430 return;
433 /* If LIMIT is another SSA name and LIMIT has a range of its own,
434 try to use LIMIT's range to avoid creating symbolic ranges
435 unnecessarily. */
436 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
438 /* LIMIT's range is only interesting if it has any useful information. */
439 if (! limit_vr
440 || limit_vr->type == VR_UNDEFINED
441 || limit_vr->type == VR_VARYING
442 || (symbolic_range_p (limit_vr)
443 && ! (limit_vr->type == VR_RANGE
444 && (limit_vr->min == limit_vr->max
445 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
446 limit_vr = NULL;
448 /* Initially, the new range has the same set of equivalences of
449 VAR's range. This will be revised before returning the final
450 value. Since assertions may be chained via mutually exclusive
451 predicates, we will need to trim the set of equivalences before
452 we are done. */
453 gcc_assert (vr_p->equiv == NULL);
454 add_equivalence (&vr_p->equiv, var);
456 /* Extract a new range based on the asserted comparison for VAR and
457 LIMIT's value range. Notice that if LIMIT has an anti-range, we
458 will only use it for equality comparisons (EQ_EXPR). For any
459 other kind of assertion, we cannot derive a range from LIMIT's
460 anti-range that can be used to describe the new range. For
461 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
462 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
463 no single range for x_2 that could describe LE_EXPR, so we might
464 as well build the range [b_4, +INF] for it.
465 One special case we handle is extracting a range from a
466 range test encoded as (unsigned)var + CST <= limit. */
467 if (TREE_CODE (op) == NOP_EXPR
468 || TREE_CODE (op) == PLUS_EXPR)
470 if (TREE_CODE (op) == PLUS_EXPR)
472 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
473 TREE_OPERAND (op, 1));
474 max = int_const_binop (PLUS_EXPR, limit, min);
475 op = TREE_OPERAND (op, 0);
477 else
479 min = build_int_cst (TREE_TYPE (var), 0);
480 max = limit;
483 /* Make sure to not set TREE_OVERFLOW on the final type
484 conversion. We are willingly interpreting large positive
485 unsigned values as negative signed values here. */
486 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
487 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
489 /* We can transform a max, min range to an anti-range or
490 vice-versa. Use set_and_canonicalize_value_range which does
491 this for us. */
492 if (cond_code == LE_EXPR)
493 set_and_canonicalize_value_range (vr_p, VR_RANGE,
494 min, max, vr_p->equiv);
495 else if (cond_code == GT_EXPR)
496 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
497 min, max, vr_p->equiv);
498 else
499 gcc_unreachable ();
501 else if (cond_code == EQ_EXPR)
503 enum value_range_type range_type;
505 if (limit_vr)
507 range_type = limit_vr->type;
508 min = limit_vr->min;
509 max = limit_vr->max;
511 else
513 range_type = VR_RANGE;
514 min = limit;
515 max = limit;
518 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
520 /* When asserting the equality VAR == LIMIT and LIMIT is another
521 SSA name, the new range will also inherit the equivalence set
522 from LIMIT. */
523 if (TREE_CODE (limit) == SSA_NAME)
524 add_equivalence (&vr_p->equiv, limit);
526 else if (cond_code == NE_EXPR)
528 /* As described above, when LIMIT's range is an anti-range and
529 this assertion is an inequality (NE_EXPR), then we cannot
530 derive anything from the anti-range. For instance, if
531 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
532 not imply that VAR's range is [0, 0]. So, in the case of
533 anti-ranges, we just assert the inequality using LIMIT and
534 not its anti-range.
536 If LIMIT_VR is a range, we can only use it to build a new
537 anti-range if LIMIT_VR is a single-valued range. For
538 instance, if LIMIT_VR is [0, 1], the predicate
539 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
540 Rather, it means that for value 0 VAR should be ~[0, 0]
541 and for value 1, VAR should be ~[1, 1]. We cannot
542 represent these ranges.
544 The only situation in which we can build a valid
545 anti-range is when LIMIT_VR is a single-valued range
546 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
547 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
548 if (limit_vr
549 && limit_vr->type == VR_RANGE
550 && compare_values (limit_vr->min, limit_vr->max) == 0)
552 min = limit_vr->min;
553 max = limit_vr->max;
555 else
557 /* In any other case, we cannot use LIMIT's range to build a
558 valid anti-range. */
559 min = max = limit;
562 /* If MIN and MAX cover the whole range for their type, then
563 just use the original LIMIT. */
564 if (INTEGRAL_TYPE_P (type)
565 && vrp_val_is_min (min)
566 && vrp_val_is_max (max))
567 min = max = limit;
569 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
570 min, max, vr_p->equiv);
572 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
574 min = TYPE_MIN_VALUE (type);
576 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
577 max = limit;
578 else
580 /* If LIMIT_VR is of the form [N1, N2], we need to build the
581 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
582 LT_EXPR. */
583 max = limit_vr->max;
586 /* If the maximum value forces us to be out of bounds, simply punt.
587 It would be pointless to try and do anything more since this
588 all should be optimized away above us. */
589 if (cond_code == LT_EXPR
590 && compare_values (max, min) == 0)
591 set_value_range_to_varying (vr_p);
592 else
594 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
595 if (cond_code == LT_EXPR)
597 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
598 && !TYPE_UNSIGNED (TREE_TYPE (max)))
599 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
600 build_int_cst (TREE_TYPE (max), -1));
601 else
602 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
603 build_int_cst (TREE_TYPE (max), 1));
604 /* Signal to compare_values_warnv this expr doesn't overflow. */
605 if (EXPR_P (max))
606 TREE_NO_WARNING (max) = 1;
609 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
612 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
614 max = TYPE_MAX_VALUE (type);
616 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
617 min = limit;
618 else
620 /* If LIMIT_VR is of the form [N1, N2], we need to build the
621 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
622 GT_EXPR. */
623 min = limit_vr->min;
626 /* If the minimum value forces us to be out of bounds, simply punt.
627 It would be pointless to try and do anything more since this
628 all should be optimized away above us. */
629 if (cond_code == GT_EXPR
630 && compare_values (min, max) == 0)
631 set_value_range_to_varying (vr_p);
632 else
634 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
635 if (cond_code == GT_EXPR)
637 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
638 && !TYPE_UNSIGNED (TREE_TYPE (min)))
639 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
640 build_int_cst (TREE_TYPE (min), -1));
641 else
642 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
643 build_int_cst (TREE_TYPE (min), 1));
644 /* Signal to compare_values_warnv this expr doesn't overflow. */
645 if (EXPR_P (min))
646 TREE_NO_WARNING (min) = 1;
649 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
652 else
653 gcc_unreachable ();
655 /* Finally intersect the new range with what we already know about var. */
656 vrp_intersect_ranges (vr_p, get_value_range (var));
659 /* Extract value range information from an ASSERT_EXPR EXPR and store
660 it in *VR_P. */
662 void
663 vr_values::extract_range_from_assert (value_range *vr_p, tree expr)
665 tree var = ASSERT_EXPR_VAR (expr);
666 tree cond = ASSERT_EXPR_COND (expr);
667 tree limit, op;
668 enum tree_code cond_code;
669 gcc_assert (COMPARISON_CLASS_P (cond));
671 /* Find VAR in the ASSERT_EXPR conditional. */
672 if (var == TREE_OPERAND (cond, 0)
673 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
674 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
676 /* If the predicate is of the form VAR COMP LIMIT, then we just
677 take LIMIT from the RHS and use the same comparison code. */
678 cond_code = TREE_CODE (cond);
679 limit = TREE_OPERAND (cond, 1);
680 op = TREE_OPERAND (cond, 0);
682 else
684 /* If the predicate is of the form LIMIT COMP VAR, then we need
685 to flip around the comparison code to create the proper range
686 for VAR. */
687 cond_code = swap_tree_comparison (TREE_CODE (cond));
688 limit = TREE_OPERAND (cond, 0);
689 op = TREE_OPERAND (cond, 1);
691 extract_range_for_var_from_comparison_expr (var, cond_code, op,
692 limit, vr_p);
695 /* Extract range information from SSA name VAR and store it in VR. If
696 VAR has an interesting range, use it. Otherwise, create the
697 range [VAR, VAR] and return it. This is useful in situations where
698 we may have conditionals testing values of VARYING names. For
699 instance,
701 x_3 = y_5;
702 if (x_3 > y_5)
705 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
706 always false. */
708 void
709 vr_values::extract_range_from_ssa_name (value_range *vr, tree var)
711 value_range *var_vr = get_value_range (var);
713 if (var_vr->type != VR_VARYING)
714 copy_value_range (vr, var_vr);
715 else
716 set_value_range (vr, VR_RANGE, var, var, NULL);
718 add_equivalence (&vr->equiv, var);
721 /* Extract range information from a binary expression OP0 CODE OP1 based on
722 the ranges of each of its operands with resulting type EXPR_TYPE.
723 The resulting range is stored in *VR. */
725 void
726 vr_values::extract_range_from_binary_expr (value_range *vr,
727 enum tree_code code,
728 tree expr_type, tree op0, tree op1)
730 value_range vr0 = VR_INITIALIZER;
731 value_range vr1 = VR_INITIALIZER;
733 /* Get value ranges for each operand. For constant operands, create
734 a new value range with the operand to simplify processing. */
735 if (TREE_CODE (op0) == SSA_NAME)
736 vr0 = *(get_value_range (op0));
737 else if (is_gimple_min_invariant (op0))
738 set_value_range_to_value (&vr0, op0, NULL);
739 else
740 set_value_range_to_varying (&vr0);
742 if (TREE_CODE (op1) == SSA_NAME)
743 vr1 = *(get_value_range (op1));
744 else if (is_gimple_min_invariant (op1))
745 set_value_range_to_value (&vr1, op1, NULL);
746 else
747 set_value_range_to_varying (&vr1);
749 /* If one argument is varying, we can sometimes still deduce a
750 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
751 if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
752 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
754 if (vr0.type == VR_VARYING && vr1.type != VR_VARYING)
756 vr0.type = VR_RANGE;
757 vr0.min = vrp_val_min (expr_type);
758 vr0.max = vrp_val_max (expr_type);
760 else if (vr1.type == VR_VARYING && vr0.type != VR_VARYING)
762 vr1.type = VR_RANGE;
763 vr1.min = vrp_val_min (expr_type);
764 vr1.max = vrp_val_max (expr_type);
768 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
770 /* Set value_range for n in following sequence:
771 def = __builtin_memchr (arg, 0, sz)
772 n = def - arg
773 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
775 if (vr->type == VR_VARYING
776 && code == POINTER_DIFF_EXPR
777 && TREE_CODE (op0) == SSA_NAME
778 && TREE_CODE (op1) == SSA_NAME)
780 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
781 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
782 gcall *call_stmt = NULL;
784 if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
785 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
786 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
787 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
788 && (call_stmt = dyn_cast<gcall *>(SSA_NAME_DEF_STMT (op0)))
789 && gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR)
790 && operand_equal_p (op0, gimple_call_lhs (call_stmt), 0)
791 && operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0)
792 && integer_zerop (gimple_call_arg (call_stmt, 1)))
794 tree max = vrp_val_max (ptrdiff_type_node);
795 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
796 tree range_min = build_zero_cst (expr_type);
797 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
798 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
799 return;
803 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
804 and based on the other operand, for example if it was deduced from a
805 symbolic comparison. When a bound of the range of the first operand
806 is invariant, we set the corresponding bound of the new range to INF
807 in order to avoid recursing on the range of the second operand. */
808 if (vr->type == VR_VARYING
809 && (code == PLUS_EXPR || code == MINUS_EXPR)
810 && TREE_CODE (op1) == SSA_NAME
811 && vr0.type == VR_RANGE
812 && symbolic_range_based_on_p (&vr0, op1))
814 const bool minus_p = (code == MINUS_EXPR);
815 value_range n_vr1 = VR_INITIALIZER;
817 /* Try with VR0 and [-INF, OP1]. */
818 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
819 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
821 /* Try with VR0 and [OP1, +INF]. */
822 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
823 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
825 /* Try with VR0 and [OP1, OP1]. */
826 else
827 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
829 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
832 if (vr->type == VR_VARYING
833 && (code == PLUS_EXPR || code == MINUS_EXPR)
834 && TREE_CODE (op0) == SSA_NAME
835 && vr1.type == VR_RANGE
836 && symbolic_range_based_on_p (&vr1, op0))
838 const bool minus_p = (code == MINUS_EXPR);
839 value_range n_vr0 = VR_INITIALIZER;
841 /* Try with [-INF, OP0] and VR1. */
842 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
843 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
845 /* Try with [OP0, +INF] and VR1. */
846 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
847 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
849 /* Try with [OP0, OP0] and VR1. */
850 else
851 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
853 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
856 /* If we didn't derive a range for MINUS_EXPR, and
857 op1's range is ~[op0,op0] or vice-versa, then we
858 can derive a non-null range. This happens often for
859 pointer subtraction. */
860 if (vr->type == VR_VARYING
861 && (code == MINUS_EXPR || code == POINTER_DIFF_EXPR)
862 && TREE_CODE (op0) == SSA_NAME
863 && ((vr0.type == VR_ANTI_RANGE
864 && vr0.min == op1
865 && vr0.min == vr0.max)
866 || (vr1.type == VR_ANTI_RANGE
867 && vr1.min == op0
868 && vr1.min == vr1.max)))
869 set_value_range_to_nonnull (vr, expr_type);
872 /* Extract range information from a unary expression CODE OP0 based on
873 the range of its operand with resulting type TYPE.
874 The resulting range is stored in *VR. */
876 void
877 vr_values::extract_range_from_unary_expr (value_range *vr, enum tree_code code,
878 tree type, tree op0)
880 value_range vr0 = VR_INITIALIZER;
882 /* Get value ranges for the operand. For constant operands, create
883 a new value range with the operand to simplify processing. */
884 if (TREE_CODE (op0) == SSA_NAME)
885 vr0 = *(get_value_range (op0));
886 else if (is_gimple_min_invariant (op0))
887 set_value_range_to_value (&vr0, op0, NULL);
888 else
889 set_value_range_to_varying (&vr0);
891 ::extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
895 /* Extract range information from a conditional expression STMT based on
896 the ranges of each of its operands and the expression code. */
898 void
899 vr_values::extract_range_from_cond_expr (value_range *vr, gassign *stmt)
901 tree op0, op1;
902 value_range vr0 = VR_INITIALIZER;
903 value_range vr1 = VR_INITIALIZER;
905 /* Get value ranges for each operand. For constant operands, create
906 a new value range with the operand to simplify processing. */
907 op0 = gimple_assign_rhs2 (stmt);
908 if (TREE_CODE (op0) == SSA_NAME)
909 vr0 = *(get_value_range (op0));
910 else if (is_gimple_min_invariant (op0))
911 set_value_range_to_value (&vr0, op0, NULL);
912 else
913 set_value_range_to_varying (&vr0);
915 op1 = gimple_assign_rhs3 (stmt);
916 if (TREE_CODE (op1) == SSA_NAME)
917 vr1 = *(get_value_range (op1));
918 else if (is_gimple_min_invariant (op1))
919 set_value_range_to_value (&vr1, op1, NULL);
920 else
921 set_value_range_to_varying (&vr1);
923 /* The resulting value range is the union of the operand ranges */
924 copy_value_range (vr, &vr0);
925 vrp_meet (vr, &vr1);
929 /* Extract range information from a comparison expression EXPR based
930 on the range of its operand and the expression code. */
932 void
933 vr_values::extract_range_from_comparison (value_range *vr, enum tree_code code,
934 tree type, tree op0, tree op1)
936 bool sop;
937 tree val;
939 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
940 NULL);
941 if (val)
943 /* Since this expression was found on the RHS of an assignment,
944 its type may be different from _Bool. Convert VAL to EXPR's
945 type. */
946 val = fold_convert (type, val);
947 if (is_gimple_min_invariant (val))
948 set_value_range_to_value (vr, val, vr->equiv);
949 else
950 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
952 else
953 /* The result of a comparison is always true or false. */
954 set_value_range_to_truthvalue (vr, type);
957 /* Helper function for simplify_internal_call_using_ranges and
958 extract_range_basic. Return true if OP0 SUBCODE OP1 for
959 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
960 always overflow. Set *OVF to true if it is known to always
961 overflow. */
963 bool
964 vr_values::check_for_binary_op_overflow (enum tree_code subcode, tree type,
965 tree op0, tree op1, bool *ovf)
967 value_range vr0 = VR_INITIALIZER;
968 value_range vr1 = VR_INITIALIZER;
969 if (TREE_CODE (op0) == SSA_NAME)
970 vr0 = *get_value_range (op0);
971 else if (TREE_CODE (op0) == INTEGER_CST)
972 set_value_range_to_value (&vr0, op0, NULL);
973 else
974 set_value_range_to_varying (&vr0);
976 if (TREE_CODE (op1) == SSA_NAME)
977 vr1 = *get_value_range (op1);
978 else if (TREE_CODE (op1) == INTEGER_CST)
979 set_value_range_to_value (&vr1, op1, NULL);
980 else
981 set_value_range_to_varying (&vr1);
983 if (!range_int_cst_p (&vr0)
984 || TREE_OVERFLOW (vr0.min)
985 || TREE_OVERFLOW (vr0.max))
987 vr0.min = vrp_val_min (TREE_TYPE (op0));
988 vr0.max = vrp_val_max (TREE_TYPE (op0));
990 if (!range_int_cst_p (&vr1)
991 || TREE_OVERFLOW (vr1.min)
992 || TREE_OVERFLOW (vr1.max))
994 vr1.min = vrp_val_min (TREE_TYPE (op1));
995 vr1.max = vrp_val_max (TREE_TYPE (op1));
997 *ovf = arith_overflowed_p (subcode, type, vr0.min,
998 subcode == MINUS_EXPR ? vr1.max : vr1.min);
999 if (arith_overflowed_p (subcode, type, vr0.max,
1000 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
1001 return false;
1002 if (subcode == MULT_EXPR)
1004 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
1005 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
1006 return false;
1008 if (*ovf)
1010 /* So far we found that there is an overflow on the boundaries.
1011 That doesn't prove that there is an overflow even for all values
1012 in between the boundaries. For that compute widest_int range
1013 of the result and see if it doesn't overlap the range of
1014 type. */
1015 widest_int wmin, wmax;
1016 widest_int w[4];
1017 int i;
1018 w[0] = wi::to_widest (vr0.min);
1019 w[1] = wi::to_widest (vr0.max);
1020 w[2] = wi::to_widest (vr1.min);
1021 w[3] = wi::to_widest (vr1.max);
1022 for (i = 0; i < 4; i++)
1024 widest_int wt;
1025 switch (subcode)
1027 case PLUS_EXPR:
1028 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
1029 break;
1030 case MINUS_EXPR:
1031 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
1032 break;
1033 case MULT_EXPR:
1034 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
1035 break;
1036 default:
1037 gcc_unreachable ();
1039 if (i == 0)
1041 wmin = wt;
1042 wmax = wt;
1044 else
1046 wmin = wi::smin (wmin, wt);
1047 wmax = wi::smax (wmax, wt);
1050 /* The result of op0 CODE op1 is known to be in range
1051 [wmin, wmax]. */
1052 widest_int wtmin = wi::to_widest (vrp_val_min (type));
1053 widest_int wtmax = wi::to_widest (vrp_val_max (type));
1054 /* If all values in [wmin, wmax] are smaller than
1055 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1056 the arithmetic operation will always overflow. */
1057 if (wmax < wtmin || wmin > wtmax)
1058 return true;
1059 return false;
1061 return true;
1064 /* Try to derive a nonnegative or nonzero range out of STMT relying
1065 primarily on generic routines in fold in conjunction with range data.
1066 Store the result in *VR */
1068 void
1069 vr_values::extract_range_basic (value_range *vr, gimple *stmt)
1071 bool sop;
1072 tree type = gimple_expr_type (stmt);
1074 if (is_gimple_call (stmt))
1076 tree arg;
1077 int mini, maxi, zerov = 0, prec;
1078 enum tree_code subcode = ERROR_MARK;
1079 combined_fn cfn = gimple_call_combined_fn (stmt);
1080 scalar_int_mode mode;
1082 switch (cfn)
1084 case CFN_BUILT_IN_CONSTANT_P:
1085 /* If the call is __builtin_constant_p and the argument is a
1086 function parameter resolve it to false. This avoids bogus
1087 array bound warnings.
1088 ??? We could do this as early as inlining is finished. */
1089 arg = gimple_call_arg (stmt, 0);
1090 if (TREE_CODE (arg) == SSA_NAME
1091 && SSA_NAME_IS_DEFAULT_DEF (arg)
1092 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
1093 && cfun->after_inlining)
1095 set_value_range_to_null (vr, type);
1096 return;
1098 break;
1099 /* Both __builtin_ffs* and __builtin_popcount return
1100 [0, prec]. */
1101 CASE_CFN_FFS:
1102 CASE_CFN_POPCOUNT:
1103 arg = gimple_call_arg (stmt, 0);
1104 prec = TYPE_PRECISION (TREE_TYPE (arg));
1105 mini = 0;
1106 maxi = prec;
1107 if (TREE_CODE (arg) == SSA_NAME)
1109 value_range *vr0 = get_value_range (arg);
1110 /* If arg is non-zero, then ffs or popcount are non-zero. */
1111 if (range_includes_zero_p (vr0) == 0)
1112 mini = 1;
1113 /* If some high bits are known to be zero,
1114 we can decrease the maximum. */
1115 if (vr0->type == VR_RANGE
1116 && TREE_CODE (vr0->max) == INTEGER_CST
1117 && !operand_less_p (vr0->min,
1118 build_zero_cst (TREE_TYPE (vr0->min))))
1119 maxi = tree_floor_log2 (vr0->max) + 1;
1121 goto bitop_builtin;
1122 /* __builtin_parity* returns [0, 1]. */
1123 CASE_CFN_PARITY:
1124 mini = 0;
1125 maxi = 1;
1126 goto bitop_builtin;
1127 /* __builtin_c[lt]z* return [0, prec-1], except for
1128 when the argument is 0, but that is undefined behavior.
1129 On many targets where the CLZ RTL or optab value is defined
1130 for 0 the value is prec, so include that in the range
1131 by default. */
1132 CASE_CFN_CLZ:
1133 arg = gimple_call_arg (stmt, 0);
1134 prec = TYPE_PRECISION (TREE_TYPE (arg));
1135 mini = 0;
1136 maxi = prec;
1137 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1138 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1139 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov)
1140 /* Handle only the single common value. */
1141 && zerov != prec)
1142 /* Magic value to give up, unless vr0 proves
1143 arg is non-zero. */
1144 mini = -2;
1145 if (TREE_CODE (arg) == SSA_NAME)
1147 value_range *vr0 = get_value_range (arg);
1148 /* From clz of VR_RANGE minimum we can compute
1149 result maximum. */
1150 if (vr0->type == VR_RANGE
1151 && TREE_CODE (vr0->min) == INTEGER_CST)
1153 maxi = prec - 1 - tree_floor_log2 (vr0->min);
1154 if (maxi != prec)
1155 mini = 0;
1157 else if (vr0->type == VR_ANTI_RANGE
1158 && integer_zerop (vr0->min))
1160 maxi = prec - 1;
1161 mini = 0;
1163 if (mini == -2)
1164 break;
1165 /* From clz of VR_RANGE maximum we can compute
1166 result minimum. */
1167 if (vr0->type == VR_RANGE
1168 && TREE_CODE (vr0->max) == INTEGER_CST)
1170 mini = prec - 1 - tree_floor_log2 (vr0->max);
1171 if (mini == prec)
1172 break;
1175 if (mini == -2)
1176 break;
1177 goto bitop_builtin;
1178 /* __builtin_ctz* return [0, prec-1], except for
1179 when the argument is 0, but that is undefined behavior.
1180 If there is a ctz optab for this mode and
1181 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1182 otherwise just assume 0 won't be seen. */
1183 CASE_CFN_CTZ:
1184 arg = gimple_call_arg (stmt, 0);
1185 prec = TYPE_PRECISION (TREE_TYPE (arg));
1186 mini = 0;
1187 maxi = prec - 1;
1188 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1189 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1190 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov))
1192 /* Handle only the two common values. */
1193 if (zerov == -1)
1194 mini = -1;
1195 else if (zerov == prec)
1196 maxi = prec;
1197 else
1198 /* Magic value to give up, unless vr0 proves
1199 arg is non-zero. */
1200 mini = -2;
1202 if (TREE_CODE (arg) == SSA_NAME)
1204 value_range *vr0 = get_value_range (arg);
1205 /* If arg is non-zero, then use [0, prec - 1]. */
1206 if ((vr0->type == VR_RANGE
1207 && integer_nonzerop (vr0->min))
1208 || (vr0->type == VR_ANTI_RANGE
1209 && integer_zerop (vr0->min)))
1211 mini = 0;
1212 maxi = prec - 1;
1214 /* If some high bits are known to be zero,
1215 we can decrease the result maximum. */
1216 if (vr0->type == VR_RANGE
1217 && TREE_CODE (vr0->max) == INTEGER_CST)
1219 maxi = tree_floor_log2 (vr0->max);
1220 /* For vr0 [0, 0] give up. */
1221 if (maxi == -1)
1222 break;
1225 if (mini == -2)
1226 break;
1227 goto bitop_builtin;
1228 /* __builtin_clrsb* returns [0, prec-1]. */
1229 CASE_CFN_CLRSB:
1230 arg = gimple_call_arg (stmt, 0);
1231 prec = TYPE_PRECISION (TREE_TYPE (arg));
1232 mini = 0;
1233 maxi = prec - 1;
1234 goto bitop_builtin;
1235 bitop_builtin:
1236 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
1237 build_int_cst (type, maxi), NULL);
1238 return;
1239 case CFN_UBSAN_CHECK_ADD:
1240 subcode = PLUS_EXPR;
1241 break;
1242 case CFN_UBSAN_CHECK_SUB:
1243 subcode = MINUS_EXPR;
1244 break;
1245 case CFN_UBSAN_CHECK_MUL:
1246 subcode = MULT_EXPR;
1247 break;
1248 case CFN_GOACC_DIM_SIZE:
1249 case CFN_GOACC_DIM_POS:
1250 /* Optimizing these two internal functions helps the loop
1251 optimizer eliminate outer comparisons. Size is [1,N]
1252 and pos is [0,N-1]. */
1254 bool is_pos = cfn == CFN_GOACC_DIM_POS;
1255 int axis = oacc_get_ifn_dim_arg (stmt);
1256 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1258 if (!size)
1259 /* If it's dynamic, the backend might know a hardware
1260 limitation. */
1261 size = targetm.goacc.dim_limit (axis);
1263 tree type = TREE_TYPE (gimple_call_lhs (stmt));
1264 set_value_range (vr, VR_RANGE,
1265 build_int_cst (type, is_pos ? 0 : 1),
1266 size ? build_int_cst (type, size - is_pos)
1267 : vrp_val_max (type), NULL);
1269 return;
1270 case CFN_BUILT_IN_STRLEN:
1271 if (tree lhs = gimple_call_lhs (stmt))
1272 if (ptrdiff_type_node
1273 && (TYPE_PRECISION (ptrdiff_type_node)
1274 == TYPE_PRECISION (TREE_TYPE (lhs))))
1276 tree type = TREE_TYPE (lhs);
1277 tree max = vrp_val_max (ptrdiff_type_node);
1278 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1279 tree range_min = build_zero_cst (type);
1280 tree range_max = wide_int_to_tree (type, wmax - 1);
1281 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
1282 return;
1284 break;
1285 default:
1286 break;
1288 if (subcode != ERROR_MARK)
1290 bool saved_flag_wrapv = flag_wrapv;
1291 /* Pretend the arithmetics is wrapping. If there is
1292 any overflow, we'll complain, but will actually do
1293 wrapping operation. */
1294 flag_wrapv = 1;
1295 extract_range_from_binary_expr (vr, subcode, type,
1296 gimple_call_arg (stmt, 0),
1297 gimple_call_arg (stmt, 1));
1298 flag_wrapv = saved_flag_wrapv;
1300 /* If for both arguments vrp_valueize returned non-NULL,
1301 this should have been already folded and if not, it
1302 wasn't folded because of overflow. Avoid removing the
1303 UBSAN_CHECK_* calls in that case. */
1304 if (vr->type == VR_RANGE
1305 && (vr->min == vr->max
1306 || operand_equal_p (vr->min, vr->max, 0)))
1307 set_value_range_to_varying (vr);
1308 return;
1311 /* Handle extraction of the two results (result of arithmetics and
1312 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1313 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1314 else if (is_gimple_assign (stmt)
1315 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1316 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1317 && INTEGRAL_TYPE_P (type))
1319 enum tree_code code = gimple_assign_rhs_code (stmt);
1320 tree op = gimple_assign_rhs1 (stmt);
1321 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
1323 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
1324 if (is_gimple_call (g) && gimple_call_internal_p (g))
1326 enum tree_code subcode = ERROR_MARK;
1327 switch (gimple_call_internal_fn (g))
1329 case IFN_ADD_OVERFLOW:
1330 subcode = PLUS_EXPR;
1331 break;
1332 case IFN_SUB_OVERFLOW:
1333 subcode = MINUS_EXPR;
1334 break;
1335 case IFN_MUL_OVERFLOW:
1336 subcode = MULT_EXPR;
1337 break;
1338 case IFN_ATOMIC_COMPARE_EXCHANGE:
1339 if (code == IMAGPART_EXPR)
1341 /* This is the boolean return value whether compare and
1342 exchange changed anything or not. */
1343 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
1344 build_int_cst (type, 1), NULL);
1345 return;
1347 break;
1348 default:
1349 break;
1351 if (subcode != ERROR_MARK)
1353 tree op0 = gimple_call_arg (g, 0);
1354 tree op1 = gimple_call_arg (g, 1);
1355 if (code == IMAGPART_EXPR)
1357 bool ovf = false;
1358 if (check_for_binary_op_overflow (subcode, type,
1359 op0, op1, &ovf))
1360 set_value_range_to_value (vr,
1361 build_int_cst (type, ovf),
1362 NULL);
1363 else if (TYPE_PRECISION (type) == 1
1364 && !TYPE_UNSIGNED (type))
1365 set_value_range_to_varying (vr);
1366 else
1367 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
1368 build_int_cst (type, 1), NULL);
1370 else if (types_compatible_p (type, TREE_TYPE (op0))
1371 && types_compatible_p (type, TREE_TYPE (op1)))
1373 bool saved_flag_wrapv = flag_wrapv;
1374 /* Pretend the arithmetics is wrapping. If there is
1375 any overflow, IMAGPART_EXPR will be set. */
1376 flag_wrapv = 1;
1377 extract_range_from_binary_expr (vr, subcode, type,
1378 op0, op1);
1379 flag_wrapv = saved_flag_wrapv;
1381 else
1383 value_range vr0 = VR_INITIALIZER;
1384 value_range vr1 = VR_INITIALIZER;
1385 bool saved_flag_wrapv = flag_wrapv;
1386 /* Pretend the arithmetics is wrapping. If there is
1387 any overflow, IMAGPART_EXPR will be set. */
1388 flag_wrapv = 1;
1389 extract_range_from_unary_expr (&vr0, NOP_EXPR,
1390 type, op0);
1391 extract_range_from_unary_expr (&vr1, NOP_EXPR,
1392 type, op1);
1393 extract_range_from_binary_expr_1 (vr, subcode, type,
1394 &vr0, &vr1);
1395 flag_wrapv = saved_flag_wrapv;
1397 return;
1402 if (INTEGRAL_TYPE_P (type)
1403 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
1404 set_value_range_to_nonnegative (vr, type);
1405 else if (vrp_stmt_computes_nonzero (stmt))
1406 set_value_range_to_nonnull (vr, type);
1407 else
1408 set_value_range_to_varying (vr);
1412 /* Try to compute a useful range out of assignment STMT and store it
1413 in *VR. */
1415 void
1416 vr_values::extract_range_from_assignment (value_range *vr, gassign *stmt)
1418 enum tree_code code = gimple_assign_rhs_code (stmt);
1420 if (code == ASSERT_EXPR)
1421 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
1422 else if (code == SSA_NAME)
1423 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
1424 else if (TREE_CODE_CLASS (code) == tcc_binary)
1425 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
1426 gimple_expr_type (stmt),
1427 gimple_assign_rhs1 (stmt),
1428 gimple_assign_rhs2 (stmt));
1429 else if (TREE_CODE_CLASS (code) == tcc_unary)
1430 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
1431 gimple_expr_type (stmt),
1432 gimple_assign_rhs1 (stmt));
1433 else if (code == COND_EXPR)
1434 extract_range_from_cond_expr (vr, stmt);
1435 else if (TREE_CODE_CLASS (code) == tcc_comparison)
1436 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
1437 gimple_expr_type (stmt),
1438 gimple_assign_rhs1 (stmt),
1439 gimple_assign_rhs2 (stmt));
1440 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
1441 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1442 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
1443 else
1444 set_value_range_to_varying (vr);
1446 if (vr->type == VR_VARYING)
1447 extract_range_basic (vr, stmt);
1450 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1452 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1453 all the values in the ranges.
1455 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1457 - Return NULL_TREE if it is not always possible to determine the
1458 value of the comparison.
1460 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1461 assumed signed overflow is undefined. */
1464 static tree
1465 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
1466 bool *strict_overflow_p)
1468 /* VARYING or UNDEFINED ranges cannot be compared. */
1469 if (vr0->type == VR_VARYING
1470 || vr0->type == VR_UNDEFINED
1471 || vr1->type == VR_VARYING
1472 || vr1->type == VR_UNDEFINED)
1473 return NULL_TREE;
1475 /* Anti-ranges need to be handled separately. */
1476 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
1478 /* If both are anti-ranges, then we cannot compute any
1479 comparison. */
1480 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
1481 return NULL_TREE;
1483 /* These comparisons are never statically computable. */
1484 if (comp == GT_EXPR
1485 || comp == GE_EXPR
1486 || comp == LT_EXPR
1487 || comp == LE_EXPR)
1488 return NULL_TREE;
1490 /* Equality can be computed only between a range and an
1491 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1492 if (vr0->type == VR_RANGE)
1494 /* To simplify processing, make VR0 the anti-range. */
1495 value_range *tmp = vr0;
1496 vr0 = vr1;
1497 vr1 = tmp;
1500 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
1502 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
1503 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
1504 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1506 return NULL_TREE;
1509 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1510 operands around and change the comparison code. */
1511 if (comp == GT_EXPR || comp == GE_EXPR)
1513 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
1514 std::swap (vr0, vr1);
1517 if (comp == EQ_EXPR)
1519 /* Equality may only be computed if both ranges represent
1520 exactly one value. */
1521 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
1522 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
1524 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
1525 strict_overflow_p);
1526 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
1527 strict_overflow_p);
1528 if (cmp_min == 0 && cmp_max == 0)
1529 return boolean_true_node;
1530 else if (cmp_min != -2 && cmp_max != -2)
1531 return boolean_false_node;
1533 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1534 else if (compare_values_warnv (vr0->min, vr1->max,
1535 strict_overflow_p) == 1
1536 || compare_values_warnv (vr1->min, vr0->max,
1537 strict_overflow_p) == 1)
1538 return boolean_false_node;
1540 return NULL_TREE;
1542 else if (comp == NE_EXPR)
1544 int cmp1, cmp2;
1546 /* If VR0 is completely to the left or completely to the right
1547 of VR1, they are always different. Notice that we need to
1548 make sure that both comparisons yield similar results to
1549 avoid comparing values that cannot be compared at
1550 compile-time. */
1551 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
1552 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
1553 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
1554 return boolean_true_node;
1556 /* If VR0 and VR1 represent a single value and are identical,
1557 return false. */
1558 else if (compare_values_warnv (vr0->min, vr0->max,
1559 strict_overflow_p) == 0
1560 && compare_values_warnv (vr1->min, vr1->max,
1561 strict_overflow_p) == 0
1562 && compare_values_warnv (vr0->min, vr1->min,
1563 strict_overflow_p) == 0
1564 && compare_values_warnv (vr0->max, vr1->max,
1565 strict_overflow_p) == 0)
1566 return boolean_false_node;
1568 /* Otherwise, they may or may not be different. */
1569 else
1570 return NULL_TREE;
1572 else if (comp == LT_EXPR || comp == LE_EXPR)
1574 int tst;
1576 /* If VR0 is to the left of VR1, return true. */
1577 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
1578 if ((comp == LT_EXPR && tst == -1)
1579 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1580 return boolean_true_node;
1582 /* If VR0 is to the right of VR1, return false. */
1583 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
1584 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1585 || (comp == LE_EXPR && tst == 1))
1586 return boolean_false_node;
1588 /* Otherwise, we don't know. */
1589 return NULL_TREE;
1592 gcc_unreachable ();
1595 /* Given a value range VR, a value VAL and a comparison code COMP, return
1596 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1597 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1598 always returns false. Return NULL_TREE if it is not always
1599 possible to determine the value of the comparison. Also set
1600 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1601 assumed signed overflow is undefined. */
1603 static tree
1604 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
1605 bool *strict_overflow_p)
1607 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
1608 return NULL_TREE;
1610 /* Anti-ranges need to be handled separately. */
1611 if (vr->type == VR_ANTI_RANGE)
1613 /* For anti-ranges, the only predicates that we can compute at
1614 compile time are equality and inequality. */
1615 if (comp == GT_EXPR
1616 || comp == GE_EXPR
1617 || comp == LT_EXPR
1618 || comp == LE_EXPR)
1619 return NULL_TREE;
1621 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1622 if (value_inside_range (val, vr->min, vr->max) == 1)
1623 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1625 return NULL_TREE;
1628 if (comp == EQ_EXPR)
1630 /* EQ_EXPR may only be computed if VR represents exactly
1631 one value. */
1632 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
1634 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
1635 if (cmp == 0)
1636 return boolean_true_node;
1637 else if (cmp == -1 || cmp == 1 || cmp == 2)
1638 return boolean_false_node;
1640 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
1641 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
1642 return boolean_false_node;
1644 return NULL_TREE;
1646 else if (comp == NE_EXPR)
1648 /* If VAL is not inside VR, then they are always different. */
1649 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
1650 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
1651 return boolean_true_node;
1653 /* If VR represents exactly one value equal to VAL, then return
1654 false. */
1655 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
1656 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
1657 return boolean_false_node;
1659 /* Otherwise, they may or may not be different. */
1660 return NULL_TREE;
1662 else if (comp == LT_EXPR || comp == LE_EXPR)
1664 int tst;
1666 /* If VR is to the left of VAL, return true. */
1667 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
1668 if ((comp == LT_EXPR && tst == -1)
1669 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1670 return boolean_true_node;
1672 /* If VR is to the right of VAL, return false. */
1673 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
1674 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1675 || (comp == LE_EXPR && tst == 1))
1676 return boolean_false_node;
1678 /* Otherwise, we don't know. */
1679 return NULL_TREE;
1681 else if (comp == GT_EXPR || comp == GE_EXPR)
1683 int tst;
1685 /* If VR is to the right of VAL, return true. */
1686 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
1687 if ((comp == GT_EXPR && tst == 1)
1688 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
1689 return boolean_true_node;
1691 /* If VR is to the left of VAL, return false. */
1692 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
1693 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
1694 || (comp == GE_EXPR && tst == -1))
1695 return boolean_false_node;
1697 /* Otherwise, we don't know. */
1698 return NULL_TREE;
1701 gcc_unreachable ();
1703 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1704 would be profitable to adjust VR using scalar evolution information
1705 for VAR. If so, update VR with the new limits. */
1707 void
1708 vr_values::adjust_range_with_scev (value_range *vr, struct loop *loop,
1709 gimple *stmt, tree var)
1711 tree init, step, chrec, tmin, tmax, min, max, type, tem;
1712 enum ev_direction dir;
1714 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1715 better opportunities than a regular range, but I'm not sure. */
1716 if (vr->type == VR_ANTI_RANGE)
1717 return;
1719 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
1721 /* Like in PR19590, scev can return a constant function. */
1722 if (is_gimple_min_invariant (chrec))
1724 set_value_range_to_value (vr, chrec, vr->equiv);
1725 return;
1728 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1729 return;
1731 init = initial_condition_in_loop_num (chrec, loop->num);
1732 tem = op_with_constant_singleton_value_range (init);
1733 if (tem)
1734 init = tem;
1735 step = evolution_part_in_loop_num (chrec, loop->num);
1736 tem = op_with_constant_singleton_value_range (step);
1737 if (tem)
1738 step = tem;
1740 /* If STEP is symbolic, we can't know whether INIT will be the
1741 minimum or maximum value in the range. Also, unless INIT is
1742 a simple expression, compare_values and possibly other functions
1743 in tree-vrp won't be able to handle it. */
1744 if (step == NULL_TREE
1745 || !is_gimple_min_invariant (step)
1746 || !valid_value_p (init))
1747 return;
1749 dir = scev_direction (chrec);
1750 if (/* Do not adjust ranges if we do not know whether the iv increases
1751 or decreases, ... */
1752 dir == EV_DIR_UNKNOWN
1753 /* ... or if it may wrap. */
1754 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
1755 get_chrec_loop (chrec), true))
1756 return;
1758 type = TREE_TYPE (var);
1759 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1760 tmin = lower_bound_in_type (type, type);
1761 else
1762 tmin = TYPE_MIN_VALUE (type);
1763 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1764 tmax = upper_bound_in_type (type, type);
1765 else
1766 tmax = TYPE_MAX_VALUE (type);
1768 /* Try to use estimated number of iterations for the loop to constrain the
1769 final value in the evolution. */
1770 if (TREE_CODE (step) == INTEGER_CST
1771 && is_gimple_val (init)
1772 && (TREE_CODE (init) != SSA_NAME
1773 || get_value_range (init)->type == VR_RANGE))
1775 widest_int nit;
1777 /* We are only entering here for loop header PHI nodes, so using
1778 the number of latch executions is the correct thing to use. */
1779 if (max_loop_iterations (loop, &nit))
1781 value_range maxvr = VR_INITIALIZER;
1782 signop sgn = TYPE_SIGN (TREE_TYPE (step));
1783 wi::overflow_type overflow;
1785 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
1786 &overflow);
1787 /* If the multiplication overflowed we can't do a meaningful
1788 adjustment. Likewise if the result doesn't fit in the type
1789 of the induction variable. For a signed type we have to
1790 check whether the result has the expected signedness which
1791 is that of the step as number of iterations is unsigned. */
1792 if (!overflow
1793 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
1794 && (sgn == UNSIGNED
1795 || wi::gts_p (wtmp, 0) == wi::gts_p (wi::to_wide (step), 0)))
1797 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
1798 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
1799 TREE_TYPE (init), init, tem);
1800 /* Likewise if the addition did. */
1801 if (maxvr.type == VR_RANGE)
1803 value_range initvr = VR_INITIALIZER;
1805 if (TREE_CODE (init) == SSA_NAME)
1806 initvr = *(get_value_range (init));
1807 else if (is_gimple_min_invariant (init))
1808 set_value_range_to_value (&initvr, init, NULL);
1809 else
1810 return;
1812 /* Check if init + nit * step overflows. Though we checked
1813 scev {init, step}_loop doesn't wrap, it is not enough
1814 because the loop may exit immediately. Overflow could
1815 happen in the plus expression in this case. */
1816 if ((dir == EV_DIR_DECREASES
1817 && compare_values (maxvr.min, initvr.min) != -1)
1818 || (dir == EV_DIR_GROWS
1819 && compare_values (maxvr.max, initvr.max) != 1))
1820 return;
1822 tmin = maxvr.min;
1823 tmax = maxvr.max;
1829 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
1831 min = tmin;
1832 max = tmax;
1834 /* For VARYING or UNDEFINED ranges, just about anything we get
1835 from scalar evolutions should be better. */
1837 if (dir == EV_DIR_DECREASES)
1838 max = init;
1839 else
1840 min = init;
1842 else if (vr->type == VR_RANGE)
1844 min = vr->min;
1845 max = vr->max;
1847 if (dir == EV_DIR_DECREASES)
1849 /* INIT is the maximum value. If INIT is lower than VR->MAX
1850 but no smaller than VR->MIN, set VR->MAX to INIT. */
1851 if (compare_values (init, max) == -1)
1852 max = init;
1854 /* According to the loop information, the variable does not
1855 overflow. */
1856 if (compare_values (min, tmin) == -1)
1857 min = tmin;
1860 else
1862 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
1863 if (compare_values (init, min) == 1)
1864 min = init;
1866 if (compare_values (tmax, max) == -1)
1867 max = tmax;
1870 else
1871 return;
1873 /* If we just created an invalid range with the minimum
1874 greater than the maximum, we fail conservatively.
1875 This should happen only in unreachable
1876 parts of code, or for invalid programs. */
1877 if (compare_values (min, max) == 1)
1878 return;
1880 /* Even for valid range info, sometimes overflow flag will leak in.
1881 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1882 drop them. */
1883 if (TREE_OVERFLOW_P (min))
1884 min = drop_tree_overflow (min);
1885 if (TREE_OVERFLOW_P (max))
1886 max = drop_tree_overflow (max);
1888 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
1891 /* Dump value ranges of all SSA_NAMEs to FILE. */
1893 void
1894 vr_values::dump_all_value_ranges (FILE *file)
1896 size_t i;
1898 for (i = 0; i < num_vr_values; i++)
1900 if (vr_value[i])
1902 print_generic_expr (file, ssa_name (i));
1903 fprintf (file, ": ");
1904 dump_value_range (file, vr_value[i]);
1905 fprintf (file, "\n");
1909 fprintf (file, "\n");
1912 /* Initialize VRP lattice. */
1914 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1916 values_propagated = false;
1917 num_vr_values = num_ssa_names;
1918 vr_value = XCNEWVEC (value_range *, num_vr_values);
1919 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
1920 bitmap_obstack_initialize (&vrp_equiv_obstack);
1923 /* Free VRP lattice. */
1925 vr_values::~vr_values ()
1927 /* Free allocated memory. */
1928 free (vr_value);
1929 free (vr_phi_edge_counts);
1930 bitmap_obstack_release (&vrp_equiv_obstack);
1931 vrp_value_range_pool.release ();
1933 /* So that we can distinguish between VRP data being available
1934 and not available. */
1935 vr_value = NULL;
1936 vr_phi_edge_counts = NULL;
1940 /* A hack. */
1941 static class vr_values *x_vr_values;
1943 /* Return the singleton value-range for NAME or NAME. */
1945 static inline tree
1946 vrp_valueize (tree name)
1948 if (TREE_CODE (name) == SSA_NAME)
1950 value_range *vr = x_vr_values->get_value_range (name);
1951 if (vr->type == VR_RANGE
1952 && (TREE_CODE (vr->min) == SSA_NAME
1953 || is_gimple_min_invariant (vr->min))
1954 && vrp_operand_equal_p (vr->min, vr->max))
1955 return vr->min;
1957 return name;
1960 /* Return the singleton value-range for NAME if that is a constant
1961 but signal to not follow SSA edges. */
1963 static inline tree
1964 vrp_valueize_1 (tree name)
1966 if (TREE_CODE (name) == SSA_NAME)
1968 /* If the definition may be simulated again we cannot follow
1969 this SSA edge as the SSA propagator does not necessarily
1970 re-visit the use. */
1971 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1972 if (!gimple_nop_p (def_stmt)
1973 && prop_simulate_again_p (def_stmt))
1974 return NULL_TREE;
1975 value_range *vr = x_vr_values->get_value_range (name);
1976 if (range_int_cst_singleton_p (vr))
1977 return vr->min;
1979 return name;
1982 /* Given STMT, an assignment or call, return its LHS if the type
1983 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
1985 tree
1986 get_output_for_vrp (gimple *stmt)
1988 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
1989 return NULL_TREE;
1991 /* We only keep track of ranges in integral and pointer types. */
1992 tree lhs = gimple_get_lhs (stmt);
1993 if (TREE_CODE (lhs) == SSA_NAME
1994 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1995 /* It is valid to have NULL MIN/MAX values on a type. See
1996 build_range_type. */
1997 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
1998 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
1999 || POINTER_TYPE_P (TREE_TYPE (lhs))))
2000 return lhs;
2002 return NULL_TREE;
2005 /* Visit assignment STMT. If it produces an interesting range, record
2006 the range in VR and set LHS to OUTPUT_P. */
2008 void
2009 vr_values::vrp_visit_assignment_or_call (gimple *stmt, tree *output_p,
2010 value_range *vr)
2012 tree lhs = get_output_for_vrp (stmt);
2013 *output_p = lhs;
2015 /* We only keep track of ranges in integral and pointer types. */
2016 if (lhs)
2018 enum gimple_code code = gimple_code (stmt);
2020 /* Try folding the statement to a constant first. */
2021 x_vr_values = this;
2022 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
2023 vrp_valueize_1);
2024 x_vr_values = NULL;
2025 if (tem)
2027 if (TREE_CODE (tem) == SSA_NAME
2028 && (SSA_NAME_IS_DEFAULT_DEF (tem)
2029 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
2031 extract_range_from_ssa_name (vr, tem);
2032 return;
2034 else if (is_gimple_min_invariant (tem))
2036 set_value_range_to_value (vr, tem, NULL);
2037 return;
2040 /* Then dispatch to value-range extracting functions. */
2041 if (code == GIMPLE_CALL)
2042 extract_range_basic (vr, stmt);
2043 else
2044 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
2048 /* Helper that gets the value range of the SSA_NAME with version I
2049 or a symbolic range containing the SSA_NAME only if the value range
2050 is varying or undefined. */
2052 value_range
2053 vr_values::get_vr_for_comparison (int i)
2055 value_range vr = *get_value_range (ssa_name (i));
2057 /* If name N_i does not have a valid range, use N_i as its own
2058 range. This allows us to compare against names that may
2059 have N_i in their ranges. */
2060 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
2062 vr.type = VR_RANGE;
2063 vr.min = ssa_name (i);
2064 vr.max = ssa_name (i);
2067 return vr;
2070 /* Compare all the value ranges for names equivalent to VAR with VAL
2071 using comparison code COMP. Return the same value returned by
2072 compare_range_with_value, including the setting of
2073 *STRICT_OVERFLOW_P. */
2075 tree
2076 vr_values::compare_name_with_value (enum tree_code comp, tree var, tree val,
2077 bool *strict_overflow_p, bool use_equiv_p)
2079 bitmap_iterator bi;
2080 unsigned i;
2081 bitmap e;
2082 tree retval, t;
2083 int used_strict_overflow;
2084 bool sop;
2085 value_range equiv_vr;
2087 /* Get the set of equivalences for VAR. */
2088 e = get_value_range (var)->equiv;
2090 /* Start at -1. Set it to 0 if we do a comparison without relying
2091 on overflow, or 1 if all comparisons rely on overflow. */
2092 used_strict_overflow = -1;
2094 /* Compare vars' value range with val. */
2095 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
2096 sop = false;
2097 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
2098 if (retval)
2099 used_strict_overflow = sop ? 1 : 0;
2101 /* If the equiv set is empty we have done all work we need to do. */
2102 if (e == NULL)
2104 if (retval
2105 && used_strict_overflow > 0)
2106 *strict_overflow_p = true;
2107 return retval;
2110 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
2112 tree name = ssa_name (i);
2113 if (! name)
2114 continue;
2116 if (! use_equiv_p
2117 && ! SSA_NAME_IS_DEFAULT_DEF (name)
2118 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
2119 continue;
2121 equiv_vr = get_vr_for_comparison (i);
2122 sop = false;
2123 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
2124 if (t)
2126 /* If we get different answers from different members
2127 of the equivalence set this check must be in a dead
2128 code region. Folding it to a trap representation
2129 would be correct here. For now just return don't-know. */
2130 if (retval != NULL
2131 && t != retval)
2133 retval = NULL_TREE;
2134 break;
2136 retval = t;
2138 if (!sop)
2139 used_strict_overflow = 0;
2140 else if (used_strict_overflow < 0)
2141 used_strict_overflow = 1;
2145 if (retval
2146 && used_strict_overflow > 0)
2147 *strict_overflow_p = true;
2149 return retval;
2153 /* Given a comparison code COMP and names N1 and N2, compare all the
2154 ranges equivalent to N1 against all the ranges equivalent to N2
2155 to determine the value of N1 COMP N2. Return the same value
2156 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2157 whether we relied on undefined signed overflow in the comparison. */
2160 tree
2161 vr_values::compare_names (enum tree_code comp, tree n1, tree n2,
2162 bool *strict_overflow_p)
2164 tree t, retval;
2165 bitmap e1, e2;
2166 bitmap_iterator bi1, bi2;
2167 unsigned i1, i2;
2168 int used_strict_overflow;
2169 static bitmap_obstack *s_obstack = NULL;
2170 static bitmap s_e1 = NULL, s_e2 = NULL;
2172 /* Compare the ranges of every name equivalent to N1 against the
2173 ranges of every name equivalent to N2. */
2174 e1 = get_value_range (n1)->equiv;
2175 e2 = get_value_range (n2)->equiv;
2177 /* Use the fake bitmaps if e1 or e2 are not available. */
2178 if (s_obstack == NULL)
2180 s_obstack = XNEW (bitmap_obstack);
2181 bitmap_obstack_initialize (s_obstack);
2182 s_e1 = BITMAP_ALLOC (s_obstack);
2183 s_e2 = BITMAP_ALLOC (s_obstack);
2185 if (e1 == NULL)
2186 e1 = s_e1;
2187 if (e2 == NULL)
2188 e2 = s_e2;
2190 /* Add N1 and N2 to their own set of equivalences to avoid
2191 duplicating the body of the loop just to check N1 and N2
2192 ranges. */
2193 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
2194 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
2196 /* If the equivalence sets have a common intersection, then the two
2197 names can be compared without checking their ranges. */
2198 if (bitmap_intersect_p (e1, e2))
2200 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2201 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2203 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
2204 ? boolean_true_node
2205 : boolean_false_node;
2208 /* Start at -1. Set it to 0 if we do a comparison without relying
2209 on overflow, or 1 if all comparisons rely on overflow. */
2210 used_strict_overflow = -1;
2212 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2213 N2 to their own set of equivalences to avoid duplicating the body
2214 of the loop just to check N1 and N2 ranges. */
2215 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
2217 if (! ssa_name (i1))
2218 continue;
2220 value_range vr1 = get_vr_for_comparison (i1);
2222 t = retval = NULL_TREE;
2223 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
2225 if (! ssa_name (i2))
2226 continue;
2228 bool sop = false;
2230 value_range vr2 = get_vr_for_comparison (i2);
2232 t = compare_ranges (comp, &vr1, &vr2, &sop);
2233 if (t)
2235 /* If we get different answers from different members
2236 of the equivalence set this check must be in a dead
2237 code region. Folding it to a trap representation
2238 would be correct here. For now just return don't-know. */
2239 if (retval != NULL
2240 && t != retval)
2242 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2243 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2244 return NULL_TREE;
2246 retval = t;
2248 if (!sop)
2249 used_strict_overflow = 0;
2250 else if (used_strict_overflow < 0)
2251 used_strict_overflow = 1;
2255 if (retval)
2257 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2258 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2259 if (used_strict_overflow > 0)
2260 *strict_overflow_p = true;
2261 return retval;
2265 /* None of the equivalent ranges are useful in computing this
2266 comparison. */
2267 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2268 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2269 return NULL_TREE;
2272 /* Helper function for vrp_evaluate_conditional_warnv & other
2273 optimizers. */
2275 tree
2276 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2277 (enum tree_code code, tree op0, tree op1, bool * strict_overflow_p)
2279 value_range *vr0, *vr1;
2281 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
2282 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
2284 tree res = NULL_TREE;
2285 if (vr0 && vr1)
2286 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
2287 if (!res && vr0)
2288 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
2289 if (!res && vr1)
2290 res = (compare_range_with_value
2291 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
2292 return res;
2295 /* Helper function for vrp_evaluate_conditional_warnv. */
2297 tree
2298 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code,
2299 tree op0, tree op1,
2300 bool use_equiv_p,
2301 bool *strict_overflow_p,
2302 bool *only_ranges)
2304 tree ret;
2305 if (only_ranges)
2306 *only_ranges = true;
2308 /* We only deal with integral and pointer types. */
2309 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2310 && !POINTER_TYPE_P (TREE_TYPE (op0)))
2311 return NULL_TREE;
2313 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2314 as a simple equality test, then prefer that over its current form
2315 for evaluation.
2317 An overflow test which collapses to an equality test can always be
2318 expressed as a comparison of one argument against zero. Overflow
2319 occurs when the chosen argument is zero and does not occur if the
2320 chosen argument is not zero. */
2321 tree x;
2322 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
2324 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
2325 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2326 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2327 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2328 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2329 if (integer_zerop (x))
2331 op1 = x;
2332 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
2334 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2335 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2336 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2337 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2338 else if (wi::to_wide (x) == max - 1)
2340 op0 = op1;
2341 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
2342 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
2346 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
2347 (code, op0, op1, strict_overflow_p)))
2348 return ret;
2349 if (only_ranges)
2350 *only_ranges = false;
2351 /* Do not use compare_names during propagation, it's quadratic. */
2352 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
2353 && use_equiv_p)
2354 return compare_names (code, op0, op1, strict_overflow_p);
2355 else if (TREE_CODE (op0) == SSA_NAME)
2356 return compare_name_with_value (code, op0, op1,
2357 strict_overflow_p, use_equiv_p);
2358 else if (TREE_CODE (op1) == SSA_NAME)
2359 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
2360 strict_overflow_p, use_equiv_p);
2361 return NULL_TREE;
2364 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2365 information. Return NULL if the conditional can not be evaluated.
2366 The ranges of all the names equivalent with the operands in COND
2367 will be used when trying to compute the value. If the result is
2368 based on undefined signed overflow, issue a warning if
2369 appropriate. */
2371 tree
2372 vr_values::vrp_evaluate_conditional (tree_code code, tree op0,
2373 tree op1, gimple *stmt)
2375 bool sop;
2376 tree ret;
2377 bool only_ranges;
2379 /* Some passes and foldings leak constants with overflow flag set
2380 into the IL. Avoid doing wrong things with these and bail out. */
2381 if ((TREE_CODE (op0) == INTEGER_CST
2382 && TREE_OVERFLOW (op0))
2383 || (TREE_CODE (op1) == INTEGER_CST
2384 && TREE_OVERFLOW (op1)))
2385 return NULL_TREE;
2387 sop = false;
2388 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
2389 &only_ranges);
2391 if (ret && sop)
2393 enum warn_strict_overflow_code wc;
2394 const char* warnmsg;
2396 if (is_gimple_min_invariant (ret))
2398 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
2399 warnmsg = G_("assuming signed overflow does not occur when "
2400 "simplifying conditional to constant");
2402 else
2404 wc = WARN_STRICT_OVERFLOW_COMPARISON;
2405 warnmsg = G_("assuming signed overflow does not occur when "
2406 "simplifying conditional");
2409 if (issue_strict_overflow_warning (wc))
2411 location_t location;
2413 if (!gimple_has_location (stmt))
2414 location = input_location;
2415 else
2416 location = gimple_location (stmt);
2417 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
2421 if (warn_type_limits
2422 && ret && only_ranges
2423 && TREE_CODE_CLASS (code) == tcc_comparison
2424 && TREE_CODE (op0) == SSA_NAME)
2426 /* If the comparison is being folded and the operand on the LHS
2427 is being compared against a constant value that is outside of
2428 the natural range of OP0's type, then the predicate will
2429 always fold regardless of the value of OP0. If -Wtype-limits
2430 was specified, emit a warning. */
2431 tree type = TREE_TYPE (op0);
2432 value_range *vr0 = get_value_range (op0);
2434 if (vr0->type == VR_RANGE
2435 && INTEGRAL_TYPE_P (type)
2436 && vrp_val_is_min (vr0->min)
2437 && vrp_val_is_max (vr0->max)
2438 && is_gimple_min_invariant (op1))
2440 location_t location;
2442 if (!gimple_has_location (stmt))
2443 location = input_location;
2444 else
2445 location = gimple_location (stmt);
2447 warning_at (location, OPT_Wtype_limits,
2448 integer_zerop (ret)
2449 ? G_("comparison always false "
2450 "due to limited range of data type")
2451 : G_("comparison always true "
2452 "due to limited range of data type"));
2456 return ret;
2460 /* Visit conditional statement STMT. If we can determine which edge
2461 will be taken out of STMT's basic block, record it in
2462 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2464 void
2465 vr_values::vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
2467 tree val;
2469 *taken_edge_p = NULL;
2471 if (dump_file && (dump_flags & TDF_DETAILS))
2473 tree use;
2474 ssa_op_iter i;
2476 fprintf (dump_file, "\nVisiting conditional with predicate: ");
2477 print_gimple_stmt (dump_file, stmt, 0);
2478 fprintf (dump_file, "\nWith known ranges\n");
2480 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
2482 fprintf (dump_file, "\t");
2483 print_generic_expr (dump_file, use);
2484 fprintf (dump_file, ": ");
2485 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
2488 fprintf (dump_file, "\n");
2491 /* Compute the value of the predicate COND by checking the known
2492 ranges of each of its operands.
2494 Note that we cannot evaluate all the equivalent ranges here
2495 because those ranges may not yet be final and with the current
2496 propagation strategy, we cannot determine when the value ranges
2497 of the names in the equivalence set have changed.
2499 For instance, given the following code fragment
2501 i_5 = PHI <8, i_13>
2503 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2504 if (i_14 == 1)
2507 Assume that on the first visit to i_14, i_5 has the temporary
2508 range [8, 8] because the second argument to the PHI function is
2509 not yet executable. We derive the range ~[0, 0] for i_14 and the
2510 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2511 the first time, since i_14 is equivalent to the range [8, 8], we
2512 determine that the predicate is always false.
2514 On the next round of propagation, i_13 is determined to be
2515 VARYING, which causes i_5 to drop down to VARYING. So, another
2516 visit to i_14 is scheduled. In this second visit, we compute the
2517 exact same range and equivalence set for i_14, namely ~[0, 0] and
2518 { i_5 }. But we did not have the previous range for i_5
2519 registered, so vrp_visit_assignment thinks that the range for
2520 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2521 is not visited again, which stops propagation from visiting
2522 statements in the THEN clause of that if().
2524 To properly fix this we would need to keep the previous range
2525 value for the names in the equivalence set. This way we would've
2526 discovered that from one visit to the other i_5 changed from
2527 range [8, 8] to VR_VARYING.
2529 However, fixing this apparent limitation may not be worth the
2530 additional checking. Testing on several code bases (GCC, DLV,
2531 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2532 4 more predicates folded in SPEC. */
2534 bool sop;
2535 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
2536 gimple_cond_lhs (stmt),
2537 gimple_cond_rhs (stmt),
2538 false, &sop, NULL);
2539 if (val)
2540 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
2542 if (dump_file && (dump_flags & TDF_DETAILS))
2544 fprintf (dump_file, "\nPredicate evaluates to: ");
2545 if (val == NULL_TREE)
2546 fprintf (dump_file, "DON'T KNOW\n");
2547 else
2548 print_generic_stmt (dump_file, val);
2552 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2553 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2554 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2555 Returns true if the default label is not needed. */
2557 static bool
2558 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
2559 size_t *max_idx1, size_t *min_idx2,
2560 size_t *max_idx2)
2562 size_t i, j, k, l;
2563 unsigned int n = gimple_switch_num_labels (stmt);
2564 bool take_default;
2565 tree case_low, case_high;
2566 tree min = vr->min, max = vr->max;
2568 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
2570 take_default = !find_case_label_range (stmt, min, max, &i, &j);
2572 /* Set second range to emtpy. */
2573 *min_idx2 = 1;
2574 *max_idx2 = 0;
2576 if (vr->type == VR_RANGE)
2578 *min_idx1 = i;
2579 *max_idx1 = j;
2580 return !take_default;
2583 /* Set first range to all case labels. */
2584 *min_idx1 = 1;
2585 *max_idx1 = n - 1;
2587 if (i > j)
2588 return false;
2590 /* Make sure all the values of case labels [i , j] are contained in
2591 range [MIN, MAX]. */
2592 case_low = CASE_LOW (gimple_switch_label (stmt, i));
2593 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
2594 if (tree_int_cst_compare (case_low, min) < 0)
2595 i += 1;
2596 if (case_high != NULL_TREE
2597 && tree_int_cst_compare (max, case_high) < 0)
2598 j -= 1;
2600 if (i > j)
2601 return false;
2603 /* If the range spans case labels [i, j], the corresponding anti-range spans
2604 the labels [1, i - 1] and [j + 1, n - 1]. */
2605 k = j + 1;
2606 l = n - 1;
2607 if (k > l)
2609 k = 1;
2610 l = 0;
2613 j = i - 1;
2614 i = 1;
2615 if (i > j)
2617 i = k;
2618 j = l;
2619 k = 1;
2620 l = 0;
2623 *min_idx1 = i;
2624 *max_idx1 = j;
2625 *min_idx2 = k;
2626 *max_idx2 = l;
2627 return false;
2630 /* Visit switch statement STMT. If we can determine which edge
2631 will be taken out of STMT's basic block, record it in
2632 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2634 void
2635 vr_values::vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
2637 tree op, val;
2638 value_range *vr;
2639 size_t i = 0, j = 0, k, l;
2640 bool take_default;
2642 *taken_edge_p = NULL;
2643 op = gimple_switch_index (stmt);
2644 if (TREE_CODE (op) != SSA_NAME)
2645 return;
2647 vr = get_value_range (op);
2648 if (dump_file && (dump_flags & TDF_DETAILS))
2650 fprintf (dump_file, "\nVisiting switch expression with operand ");
2651 print_generic_expr (dump_file, op);
2652 fprintf (dump_file, " with known range ");
2653 dump_value_range (dump_file, vr);
2654 fprintf (dump_file, "\n");
2657 if ((vr->type != VR_RANGE
2658 && vr->type != VR_ANTI_RANGE)
2659 || symbolic_range_p (vr))
2660 return;
2662 /* Find the single edge that is taken from the switch expression. */
2663 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
2665 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2666 label */
2667 if (j < i)
2669 gcc_assert (take_default);
2670 val = gimple_switch_default_label (stmt);
2672 else
2674 /* Check if labels with index i to j and maybe the default label
2675 are all reaching the same label. */
2677 val = gimple_switch_label (stmt, i);
2678 if (take_default
2679 && CASE_LABEL (gimple_switch_default_label (stmt))
2680 != CASE_LABEL (val))
2682 if (dump_file && (dump_flags & TDF_DETAILS))
2683 fprintf (dump_file, " not a single destination for this "
2684 "range\n");
2685 return;
2687 for (++i; i <= j; ++i)
2689 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
2691 if (dump_file && (dump_flags & TDF_DETAILS))
2692 fprintf (dump_file, " not a single destination for this "
2693 "range\n");
2694 return;
2697 for (; k <= l; ++k)
2699 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
2701 if (dump_file && (dump_flags & TDF_DETAILS))
2702 fprintf (dump_file, " not a single destination for this "
2703 "range\n");
2704 return;
2709 *taken_edge_p = find_edge (gimple_bb (stmt),
2710 label_to_block (cfun, CASE_LABEL (val)));
2712 if (dump_file && (dump_flags & TDF_DETAILS))
2714 fprintf (dump_file, " will take edge to ");
2715 print_generic_stmt (dump_file, CASE_LABEL (val));
2720 /* Evaluate statement STMT. If the statement produces a useful range,
2721 set VR and corepsponding OUTPUT_P.
2723 If STMT is a conditional branch and we can determine its truth
2724 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2726 void
2727 vr_values::extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
2728 tree *output_p, value_range *vr)
2731 if (dump_file && (dump_flags & TDF_DETAILS))
2733 fprintf (dump_file, "\nVisiting statement:\n");
2734 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2737 if (!stmt_interesting_for_vrp (stmt))
2738 gcc_assert (stmt_ends_bb_p (stmt));
2739 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
2740 vrp_visit_assignment_or_call (stmt, output_p, vr);
2741 else if (gimple_code (stmt) == GIMPLE_COND)
2742 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
2743 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2744 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
2747 /* Visit all arguments for PHI node PHI that flow through executable
2748 edges. If a valid value range can be derived from all the incoming
2749 value ranges, set a new range in VR_RESULT. */
2751 void
2752 vr_values::extract_range_from_phi_node (gphi *phi, value_range *vr_result)
2754 size_t i;
2755 tree lhs = PHI_RESULT (phi);
2756 value_range *lhs_vr = get_value_range (lhs);
2757 bool first = true;
2758 int edges, old_edges;
2759 struct loop *l;
2761 if (dump_file && (dump_flags & TDF_DETAILS))
2763 fprintf (dump_file, "\nVisiting PHI node: ");
2764 print_gimple_stmt (dump_file, phi, 0, dump_flags);
2767 bool may_simulate_backedge_again = false;
2768 edges = 0;
2769 for (i = 0; i < gimple_phi_num_args (phi); i++)
2771 edge e = gimple_phi_arg_edge (phi, i);
2773 if (dump_file && (dump_flags & TDF_DETAILS))
2775 fprintf (dump_file,
2776 " Argument #%d (%d -> %d %sexecutable)\n",
2777 (int) i, e->src->index, e->dest->index,
2778 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
2781 if (e->flags & EDGE_EXECUTABLE)
2783 tree arg = PHI_ARG_DEF (phi, i);
2784 value_range vr_arg;
2786 ++edges;
2788 if (TREE_CODE (arg) == SSA_NAME)
2790 /* See if we are eventually going to change one of the args. */
2791 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
2792 if (! gimple_nop_p (def_stmt)
2793 && prop_simulate_again_p (def_stmt)
2794 && e->flags & EDGE_DFS_BACK)
2795 may_simulate_backedge_again = true;
2797 vr_arg = *(get_value_range (arg));
2798 /* Do not allow equivalences or symbolic ranges to leak in from
2799 backedges. That creates invalid equivalencies.
2800 See PR53465 and PR54767. */
2801 if (e->flags & EDGE_DFS_BACK)
2803 if (vr_arg.type == VR_RANGE
2804 || vr_arg.type == VR_ANTI_RANGE)
2806 vr_arg.equiv = NULL;
2807 if (symbolic_range_p (&vr_arg))
2809 vr_arg.type = VR_VARYING;
2810 vr_arg.min = NULL_TREE;
2811 vr_arg.max = NULL_TREE;
2815 else
2817 /* If the non-backedge arguments range is VR_VARYING then
2818 we can still try recording a simple equivalence. */
2819 if (vr_arg.type == VR_VARYING)
2821 vr_arg.type = VR_RANGE;
2822 vr_arg.min = arg;
2823 vr_arg.max = arg;
2824 vr_arg.equiv = NULL;
2828 else
2830 if (TREE_OVERFLOW_P (arg))
2831 arg = drop_tree_overflow (arg);
2833 vr_arg.type = VR_RANGE;
2834 vr_arg.min = arg;
2835 vr_arg.max = arg;
2836 vr_arg.equiv = NULL;
2839 if (dump_file && (dump_flags & TDF_DETAILS))
2841 fprintf (dump_file, "\t");
2842 print_generic_expr (dump_file, arg, dump_flags);
2843 fprintf (dump_file, ": ");
2844 dump_value_range (dump_file, &vr_arg);
2845 fprintf (dump_file, "\n");
2848 if (first)
2849 copy_value_range (vr_result, &vr_arg);
2850 else
2851 vrp_meet (vr_result, &vr_arg);
2852 first = false;
2854 if (vr_result->type == VR_VARYING)
2855 break;
2859 if (vr_result->type == VR_VARYING)
2860 goto varying;
2861 else if (vr_result->type == VR_UNDEFINED)
2862 goto update_range;
2864 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
2865 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
2867 /* To prevent infinite iterations in the algorithm, derive ranges
2868 when the new value is slightly bigger or smaller than the
2869 previous one. We don't do this if we have seen a new executable
2870 edge; this helps us avoid an infinity for conditionals
2871 which are not in a loop. If the old value-range was VR_UNDEFINED
2872 use the updated range and iterate one more time. If we will not
2873 simulate this PHI again via the backedge allow us to iterate. */
2874 if (edges > 0
2875 && gimple_phi_num_args (phi) > 1
2876 && edges == old_edges
2877 && lhs_vr->type != VR_UNDEFINED
2878 && may_simulate_backedge_again)
2880 /* Compare old and new ranges, fall back to varying if the
2881 values are not comparable. */
2882 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
2883 if (cmp_min == -2)
2884 goto varying;
2885 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
2886 if (cmp_max == -2)
2887 goto varying;
2889 /* For non VR_RANGE or for pointers fall back to varying if
2890 the range changed. */
2891 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
2892 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2893 && (cmp_min != 0 || cmp_max != 0))
2894 goto varying;
2896 /* If the new minimum is larger than the previous one
2897 retain the old value. If the new minimum value is smaller
2898 than the previous one and not -INF go all the way to -INF + 1.
2899 In the first case, to avoid infinite bouncing between different
2900 minimums, and in the other case to avoid iterating millions of
2901 times to reach -INF. Going to -INF + 1 also lets the following
2902 iteration compute whether there will be any overflow, at the
2903 expense of one additional iteration. */
2904 if (cmp_min < 0)
2905 vr_result->min = lhs_vr->min;
2906 else if (cmp_min > 0
2907 && !vrp_val_is_min (vr_result->min))
2908 vr_result->min
2909 = int_const_binop (PLUS_EXPR,
2910 vrp_val_min (TREE_TYPE (vr_result->min)),
2911 build_int_cst (TREE_TYPE (vr_result->min), 1));
2913 /* Similarly for the maximum value. */
2914 if (cmp_max > 0)
2915 vr_result->max = lhs_vr->max;
2916 else if (cmp_max < 0
2917 && !vrp_val_is_max (vr_result->max))
2918 vr_result->max
2919 = int_const_binop (MINUS_EXPR,
2920 vrp_val_max (TREE_TYPE (vr_result->min)),
2921 build_int_cst (TREE_TYPE (vr_result->min), 1));
2923 /* If we dropped either bound to +-INF then if this is a loop
2924 PHI node SCEV may known more about its value-range. */
2925 if (cmp_min > 0 || cmp_min < 0
2926 || cmp_max < 0 || cmp_max > 0)
2927 goto scev_check;
2929 goto infinite_check;
2932 goto update_range;
2934 varying:
2935 set_value_range_to_varying (vr_result);
2937 scev_check:
2938 /* If this is a loop PHI node SCEV may known more about its value-range.
2939 scev_check can be reached from two paths, one is a fall through from above
2940 "varying" label, the other is direct goto from code block which tries to
2941 avoid infinite simulation. */
2942 if (scev_initialized_p ()
2943 && (l = loop_containing_stmt (phi))
2944 && l->header == gimple_bb (phi))
2945 adjust_range_with_scev (vr_result, l, phi, lhs);
2947 infinite_check:
2948 /* If we will end up with a (-INF, +INF) range, set it to
2949 VARYING. Same if the previous max value was invalid for
2950 the type and we end up with vr_result.min > vr_result.max. */
2951 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
2952 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
2953 || compare_values (vr_result->min, vr_result->max) > 0))
2955 else
2956 set_value_range_to_varying (vr_result);
2958 /* If the new range is different than the previous value, keep
2959 iterating. */
2960 update_range:
2961 return;
2964 /* Simplify boolean operations if the source is known
2965 to be already a boolean. */
2966 bool
2967 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi,
2968 gimple *stmt)
2970 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
2971 tree lhs, op0, op1;
2972 bool need_conversion;
2974 /* We handle only !=/== case here. */
2975 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
2977 op0 = gimple_assign_rhs1 (stmt);
2978 if (!op_with_boolean_value_range_p (op0))
2979 return false;
2981 op1 = gimple_assign_rhs2 (stmt);
2982 if (!op_with_boolean_value_range_p (op1))
2983 return false;
2985 /* Reduce number of cases to handle to NE_EXPR. As there is no
2986 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
2987 if (rhs_code == EQ_EXPR)
2989 if (TREE_CODE (op1) == INTEGER_CST)
2990 op1 = int_const_binop (BIT_XOR_EXPR, op1,
2991 build_int_cst (TREE_TYPE (op1), 1));
2992 else
2993 return false;
2996 lhs = gimple_assign_lhs (stmt);
2997 need_conversion
2998 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
3000 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
3001 if (need_conversion
3002 && !TYPE_UNSIGNED (TREE_TYPE (op0))
3003 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
3004 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
3005 return false;
3007 /* For A != 0 we can substitute A itself. */
3008 if (integer_zerop (op1))
3009 gimple_assign_set_rhs_with_ops (gsi,
3010 need_conversion
3011 ? NOP_EXPR : TREE_CODE (op0), op0);
3012 /* For A != B we substitute A ^ B. Either with conversion. */
3013 else if (need_conversion)
3015 tree tem = make_ssa_name (TREE_TYPE (op0));
3016 gassign *newop
3017 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
3018 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
3019 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
3020 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
3021 set_range_info (tem, VR_RANGE,
3022 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
3023 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
3024 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
3026 /* Or without. */
3027 else
3028 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
3029 update_stmt (gsi_stmt (*gsi));
3030 fold_stmt (gsi, follow_single_use_edges);
3032 return true;
3035 /* Simplify a division or modulo operator to a right shift or bitwise and
3036 if the first operand is unsigned or is greater than zero and the second
3037 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3038 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3039 optimize it into just op0 if op0's range is known to be a subset of
3040 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3041 modulo. */
3043 bool
3044 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi,
3045 gimple *stmt)
3047 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3048 tree val = NULL;
3049 tree op0 = gimple_assign_rhs1 (stmt);
3050 tree op1 = gimple_assign_rhs2 (stmt);
3051 tree op0min = NULL_TREE, op0max = NULL_TREE;
3052 tree op1min = op1;
3053 value_range *vr = NULL;
3055 if (TREE_CODE (op0) == INTEGER_CST)
3057 op0min = op0;
3058 op0max = op0;
3060 else
3062 vr = get_value_range (op0);
3063 if (range_int_cst_p (vr))
3065 op0min = vr->min;
3066 op0max = vr->max;
3070 if (rhs_code == TRUNC_MOD_EXPR
3071 && TREE_CODE (op1) == SSA_NAME)
3073 value_range *vr1 = get_value_range (op1);
3074 if (range_int_cst_p (vr1))
3075 op1min = vr1->min;
3077 if (rhs_code == TRUNC_MOD_EXPR
3078 && TREE_CODE (op1min) == INTEGER_CST
3079 && tree_int_cst_sgn (op1min) == 1
3080 && op0max
3081 && tree_int_cst_lt (op0max, op1min))
3083 if (TYPE_UNSIGNED (TREE_TYPE (op0))
3084 || tree_int_cst_sgn (op0min) >= 0
3085 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
3086 op0min))
3088 /* If op0 already has the range op0 % op1 has,
3089 then TRUNC_MOD_EXPR won't change anything. */
3090 gimple_assign_set_rhs_from_tree (gsi, op0);
3091 return true;
3095 if (TREE_CODE (op0) != SSA_NAME)
3096 return false;
3098 if (!integer_pow2p (op1))
3100 /* X % -Y can be only optimized into X % Y either if
3101 X is not INT_MIN, or Y is not -1. Fold it now, as after
3102 remove_range_assertions the range info might be not available
3103 anymore. */
3104 if (rhs_code == TRUNC_MOD_EXPR
3105 && fold_stmt (gsi, follow_single_use_edges))
3106 return true;
3107 return false;
3110 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
3111 val = integer_one_node;
3112 else
3114 bool sop = false;
3116 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
3118 if (val
3119 && sop
3120 && integer_onep (val)
3121 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3123 location_t location;
3125 if (!gimple_has_location (stmt))
3126 location = input_location;
3127 else
3128 location = gimple_location (stmt);
3129 warning_at (location, OPT_Wstrict_overflow,
3130 "assuming signed overflow does not occur when "
3131 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3135 if (val && integer_onep (val))
3137 tree t;
3139 if (rhs_code == TRUNC_DIV_EXPR)
3141 t = build_int_cst (integer_type_node, tree_log2 (op1));
3142 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
3143 gimple_assign_set_rhs1 (stmt, op0);
3144 gimple_assign_set_rhs2 (stmt, t);
3146 else
3148 t = build_int_cst (TREE_TYPE (op1), 1);
3149 t = int_const_binop (MINUS_EXPR, op1, t);
3150 t = fold_convert (TREE_TYPE (op0), t);
3152 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
3153 gimple_assign_set_rhs1 (stmt, op0);
3154 gimple_assign_set_rhs2 (stmt, t);
3157 update_stmt (stmt);
3158 fold_stmt (gsi, follow_single_use_edges);
3159 return true;
3162 return false;
3165 /* Simplify a min or max if the ranges of the two operands are
3166 disjoint. Return true if we do simplify. */
3168 bool
3169 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi,
3170 gimple *stmt)
3172 tree op0 = gimple_assign_rhs1 (stmt);
3173 tree op1 = gimple_assign_rhs2 (stmt);
3174 bool sop = false;
3175 tree val;
3177 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3178 (LE_EXPR, op0, op1, &sop));
3179 if (!val)
3181 sop = false;
3182 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3183 (LT_EXPR, op0, op1, &sop));
3186 if (val)
3188 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3190 location_t location;
3192 if (!gimple_has_location (stmt))
3193 location = input_location;
3194 else
3195 location = gimple_location (stmt);
3196 warning_at (location, OPT_Wstrict_overflow,
3197 "assuming signed overflow does not occur when "
3198 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3201 /* VAL == TRUE -> OP0 < or <= op1
3202 VAL == FALSE -> OP0 > or >= op1. */
3203 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
3204 == integer_zerop (val)) ? op0 : op1;
3205 gimple_assign_set_rhs_from_tree (gsi, res);
3206 return true;
3209 return false;
3212 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3213 ABS_EXPR. If the operand is <= 0, then simplify the
3214 ABS_EXPR into a NEGATE_EXPR. */
3216 bool
3217 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3219 tree op = gimple_assign_rhs1 (stmt);
3220 value_range *vr = get_value_range (op);
3222 if (vr)
3224 tree val = NULL;
3225 bool sop = false;
3227 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
3228 if (!val)
3230 /* The range is neither <= 0 nor > 0. Now see if it is
3231 either < 0 or >= 0. */
3232 sop = false;
3233 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
3234 &sop);
3237 if (val)
3239 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3241 location_t location;
3243 if (!gimple_has_location (stmt))
3244 location = input_location;
3245 else
3246 location = gimple_location (stmt);
3247 warning_at (location, OPT_Wstrict_overflow,
3248 "assuming signed overflow does not occur when "
3249 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3252 gimple_assign_set_rhs1 (stmt, op);
3253 if (integer_zerop (val))
3254 gimple_assign_set_rhs_code (stmt, SSA_NAME);
3255 else
3256 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
3257 update_stmt (stmt);
3258 fold_stmt (gsi, follow_single_use_edges);
3259 return true;
3263 return false;
3266 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3267 If all the bits that are being cleared by & are already
3268 known to be zero from VR, or all the bits that are being
3269 set by | are already known to be one from VR, the bit
3270 operation is redundant. */
3272 bool
3273 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi,
3274 gimple *stmt)
3276 tree op0 = gimple_assign_rhs1 (stmt);
3277 tree op1 = gimple_assign_rhs2 (stmt);
3278 tree op = NULL_TREE;
3279 value_range vr0 = VR_INITIALIZER;
3280 value_range vr1 = VR_INITIALIZER;
3281 wide_int may_be_nonzero0, may_be_nonzero1;
3282 wide_int must_be_nonzero0, must_be_nonzero1;
3283 wide_int mask;
3285 if (TREE_CODE (op0) == SSA_NAME)
3286 vr0 = *(get_value_range (op0));
3287 else if (is_gimple_min_invariant (op0))
3288 set_value_range_to_value (&vr0, op0, NULL);
3289 else
3290 return false;
3292 if (TREE_CODE (op1) == SSA_NAME)
3293 vr1 = *(get_value_range (op1));
3294 else if (is_gimple_min_invariant (op1))
3295 set_value_range_to_value (&vr1, op1, NULL);
3296 else
3297 return false;
3299 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
3300 &must_be_nonzero0))
3301 return false;
3302 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
3303 &must_be_nonzero1))
3304 return false;
3306 switch (gimple_assign_rhs_code (stmt))
3308 case BIT_AND_EXPR:
3309 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3310 if (mask == 0)
3312 op = op0;
3313 break;
3315 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3316 if (mask == 0)
3318 op = op1;
3319 break;
3321 break;
3322 case BIT_IOR_EXPR:
3323 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3324 if (mask == 0)
3326 op = op1;
3327 break;
3329 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3330 if (mask == 0)
3332 op = op0;
3333 break;
3335 break;
3336 default:
3337 gcc_unreachable ();
3340 if (op == NULL_TREE)
3341 return false;
3343 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
3344 update_stmt (gsi_stmt (*gsi));
3345 return true;
3348 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3349 a known value range VR.
3351 If there is one and only one value which will satisfy the
3352 conditional, then return that value. Else return NULL.
3354 If signed overflow must be undefined for the value to satisfy
3355 the conditional, then set *STRICT_OVERFLOW_P to true. */
3357 static tree
3358 test_for_singularity (enum tree_code cond_code, tree op0,
3359 tree op1, value_range *vr)
3361 tree min = NULL;
3362 tree max = NULL;
3364 /* Extract minimum/maximum values which satisfy the conditional as it was
3365 written. */
3366 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
3368 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
3370 max = op1;
3371 if (cond_code == LT_EXPR)
3373 tree one = build_int_cst (TREE_TYPE (op0), 1);
3374 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
3375 /* Signal to compare_values_warnv this expr doesn't overflow. */
3376 if (EXPR_P (max))
3377 TREE_NO_WARNING (max) = 1;
3380 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
3382 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
3384 min = op1;
3385 if (cond_code == GT_EXPR)
3387 tree one = build_int_cst (TREE_TYPE (op0), 1);
3388 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
3389 /* Signal to compare_values_warnv this expr doesn't overflow. */
3390 if (EXPR_P (min))
3391 TREE_NO_WARNING (min) = 1;
3395 /* Now refine the minimum and maximum values using any
3396 value range information we have for op0. */
3397 if (min && max)
3399 if (compare_values (vr->min, min) == 1)
3400 min = vr->min;
3401 if (compare_values (vr->max, max) == -1)
3402 max = vr->max;
3404 /* If the new min/max values have converged to a single value,
3405 then there is only one value which can satisfy the condition,
3406 return that value. */
3407 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
3408 return min;
3410 return NULL;
3413 /* Return whether the value range *VR fits in an integer type specified
3414 by PRECISION and UNSIGNED_P. */
3416 static bool
3417 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
3419 tree src_type;
3420 unsigned src_precision;
3421 widest_int tem;
3422 signop src_sgn;
3424 /* We can only handle integral and pointer types. */
3425 src_type = TREE_TYPE (vr->min);
3426 if (!INTEGRAL_TYPE_P (src_type)
3427 && !POINTER_TYPE_P (src_type))
3428 return false;
3430 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3431 and so is an identity transform. */
3432 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
3433 src_sgn = TYPE_SIGN (src_type);
3434 if ((src_precision < dest_precision
3435 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
3436 || (src_precision == dest_precision && src_sgn == dest_sgn))
3437 return true;
3439 /* Now we can only handle ranges with constant bounds. */
3440 if (vr->type != VR_RANGE
3441 || TREE_CODE (vr->min) != INTEGER_CST
3442 || TREE_CODE (vr->max) != INTEGER_CST)
3443 return false;
3445 /* For sign changes, the MSB of the wide_int has to be clear.
3446 An unsigned value with its MSB set cannot be represented by
3447 a signed wide_int, while a negative value cannot be represented
3448 by an unsigned wide_int. */
3449 if (src_sgn != dest_sgn
3450 && (wi::lts_p (wi::to_wide (vr->min), 0)
3451 || wi::lts_p (wi::to_wide (vr->max), 0)))
3452 return false;
3454 /* Then we can perform the conversion on both ends and compare
3455 the result for equality. */
3456 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
3457 if (tem != wi::to_widest (vr->min))
3458 return false;
3459 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
3460 if (tem != wi::to_widest (vr->max))
3461 return false;
3463 return true;
3466 /* Simplify a conditional using a relational operator to an equality
3467 test if the range information indicates only one value can satisfy
3468 the original conditional. */
3470 bool
3471 vr_values::simplify_cond_using_ranges_1 (gcond *stmt)
3473 tree op0 = gimple_cond_lhs (stmt);
3474 tree op1 = gimple_cond_rhs (stmt);
3475 enum tree_code cond_code = gimple_cond_code (stmt);
3477 if (cond_code != NE_EXPR
3478 && cond_code != EQ_EXPR
3479 && TREE_CODE (op0) == SSA_NAME
3480 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3481 && is_gimple_min_invariant (op1))
3483 value_range *vr = get_value_range (op0);
3485 /* If we have range information for OP0, then we might be
3486 able to simplify this conditional. */
3487 if (vr->type == VR_RANGE)
3489 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
3490 if (new_tree)
3492 if (dump_file)
3494 fprintf (dump_file, "Simplified relational ");
3495 print_gimple_stmt (dump_file, stmt, 0);
3496 fprintf (dump_file, " into ");
3499 gimple_cond_set_code (stmt, EQ_EXPR);
3500 gimple_cond_set_lhs (stmt, op0);
3501 gimple_cond_set_rhs (stmt, new_tree);
3503 update_stmt (stmt);
3505 if (dump_file)
3507 print_gimple_stmt (dump_file, stmt, 0);
3508 fprintf (dump_file, "\n");
3511 return true;
3514 /* Try again after inverting the condition. We only deal
3515 with integral types here, so no need to worry about
3516 issues with inverting FP comparisons. */
3517 new_tree = test_for_singularity
3518 (invert_tree_comparison (cond_code, false),
3519 op0, op1, vr);
3520 if (new_tree)
3522 if (dump_file)
3524 fprintf (dump_file, "Simplified relational ");
3525 print_gimple_stmt (dump_file, stmt, 0);
3526 fprintf (dump_file, " into ");
3529 gimple_cond_set_code (stmt, NE_EXPR);
3530 gimple_cond_set_lhs (stmt, op0);
3531 gimple_cond_set_rhs (stmt, new_tree);
3533 update_stmt (stmt);
3535 if (dump_file)
3537 print_gimple_stmt (dump_file, stmt, 0);
3538 fprintf (dump_file, "\n");
3541 return true;
3545 return false;
3548 /* STMT is a conditional at the end of a basic block.
3550 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3551 was set via a type conversion, try to replace the SSA_NAME with the RHS
3552 of the type conversion. Doing so makes the conversion dead which helps
3553 subsequent passes. */
3555 void
3556 vr_values::simplify_cond_using_ranges_2 (gcond *stmt)
3558 tree op0 = gimple_cond_lhs (stmt);
3559 tree op1 = gimple_cond_rhs (stmt);
3561 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3562 see if OP0 was set by a type conversion where the source of
3563 the conversion is another SSA_NAME with a range that fits
3564 into the range of OP0's type.
3566 If so, the conversion is redundant as the earlier SSA_NAME can be
3567 used for the comparison directly if we just massage the constant in the
3568 comparison. */
3569 if (TREE_CODE (op0) == SSA_NAME
3570 && TREE_CODE (op1) == INTEGER_CST)
3572 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
3573 tree innerop;
3575 if (!is_gimple_assign (def_stmt)
3576 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3577 return;
3579 innerop = gimple_assign_rhs1 (def_stmt);
3581 if (TREE_CODE (innerop) == SSA_NAME
3582 && !POINTER_TYPE_P (TREE_TYPE (innerop))
3583 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
3584 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
3586 value_range *vr = get_value_range (innerop);
3588 if (range_int_cst_p (vr)
3589 && range_fits_type_p (vr,
3590 TYPE_PRECISION (TREE_TYPE (op0)),
3591 TYPE_SIGN (TREE_TYPE (op0)))
3592 && int_fits_type_p (op1, TREE_TYPE (innerop)))
3594 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
3595 gimple_cond_set_lhs (stmt, innerop);
3596 gimple_cond_set_rhs (stmt, newconst);
3597 update_stmt (stmt);
3598 if (dump_file && (dump_flags & TDF_DETAILS))
3600 fprintf (dump_file, "Folded into: ");
3601 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3602 fprintf (dump_file, "\n");
3609 /* Simplify a switch statement using the value range of the switch
3610 argument. */
3612 bool
3613 vr_values::simplify_switch_using_ranges (gswitch *stmt)
3615 tree op = gimple_switch_index (stmt);
3616 value_range *vr = NULL;
3617 bool take_default;
3618 edge e;
3619 edge_iterator ei;
3620 size_t i = 0, j = 0, n, n2;
3621 tree vec2;
3622 switch_update su;
3623 size_t k = 1, l = 0;
3625 if (TREE_CODE (op) == SSA_NAME)
3627 vr = get_value_range (op);
3629 /* We can only handle integer ranges. */
3630 if ((vr->type != VR_RANGE
3631 && vr->type != VR_ANTI_RANGE)
3632 || symbolic_range_p (vr))
3633 return false;
3635 /* Find case label for min/max of the value range. */
3636 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
3638 else if (TREE_CODE (op) == INTEGER_CST)
3640 take_default = !find_case_label_index (stmt, 1, op, &i);
3641 if (take_default)
3643 i = 1;
3644 j = 0;
3646 else
3648 j = i;
3651 else
3652 return false;
3654 n = gimple_switch_num_labels (stmt);
3656 /* We can truncate the case label ranges that partially overlap with OP's
3657 value range. */
3658 size_t min_idx = 1, max_idx = 0;
3659 if (vr != NULL)
3660 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
3661 if (min_idx <= max_idx)
3663 tree min_label = gimple_switch_label (stmt, min_idx);
3664 tree max_label = gimple_switch_label (stmt, max_idx);
3666 /* Avoid changing the type of the case labels when truncating. */
3667 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
3668 tree vr_min = fold_convert (case_label_type, vr->min);
3669 tree vr_max = fold_convert (case_label_type, vr->max);
3671 if (vr->type == VR_RANGE)
3673 /* If OP's value range is [2,8] and the low label range is
3674 0 ... 3, truncate the label's range to 2 .. 3. */
3675 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3676 && CASE_HIGH (min_label) != NULL_TREE
3677 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3678 CASE_LOW (min_label) = vr_min;
3680 /* If OP's value range is [2,8] and the high label range is
3681 7 ... 10, truncate the label's range to 7 .. 8. */
3682 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3683 && CASE_HIGH (max_label) != NULL_TREE
3684 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3685 CASE_HIGH (max_label) = vr_max;
3687 else if (vr->type == VR_ANTI_RANGE)
3689 tree one_cst = build_one_cst (case_label_type);
3691 if (min_label == max_label)
3693 /* If OP's value range is ~[7,8] and the label's range is
3694 7 ... 10, truncate the label's range to 9 ... 10. */
3695 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
3696 && CASE_HIGH (min_label) != NULL_TREE
3697 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
3698 CASE_LOW (min_label)
3699 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3701 /* If OP's value range is ~[7,8] and the label's range is
3702 5 ... 8, truncate the label's range to 5 ... 6. */
3703 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3704 && CASE_HIGH (min_label) != NULL_TREE
3705 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
3706 CASE_HIGH (min_label)
3707 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3709 else
3711 /* If OP's value range is ~[2,8] and the low label range is
3712 0 ... 3, truncate the label's range to 0 ... 1. */
3713 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3714 && CASE_HIGH (min_label) != NULL_TREE
3715 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3716 CASE_HIGH (min_label)
3717 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3719 /* If OP's value range is ~[2,8] and the high label range is
3720 7 ... 10, truncate the label's range to 9 ... 10. */
3721 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3722 && CASE_HIGH (max_label) != NULL_TREE
3723 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3724 CASE_LOW (max_label)
3725 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3729 /* Canonicalize singleton case ranges. */
3730 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
3731 CASE_HIGH (min_label) = NULL_TREE;
3732 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
3733 CASE_HIGH (max_label) = NULL_TREE;
3736 /* We can also eliminate case labels that lie completely outside OP's value
3737 range. */
3739 /* Bail out if this is just all edges taken. */
3740 if (i == 1
3741 && j == n - 1
3742 && take_default)
3743 return false;
3745 /* Build a new vector of taken case labels. */
3746 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
3747 n2 = 0;
3749 /* Add the default edge, if necessary. */
3750 if (take_default)
3751 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
3753 for (; i <= j; ++i, ++n2)
3754 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
3756 for (; k <= l; ++k, ++n2)
3757 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
3759 /* Mark needed edges. */
3760 for (i = 0; i < n2; ++i)
3762 e = find_edge (gimple_bb (stmt),
3763 label_to_block (cfun,
3764 CASE_LABEL (TREE_VEC_ELT (vec2, i))));
3765 e->aux = (void *)-1;
3768 /* Queue not needed edges for later removal. */
3769 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3771 if (e->aux == (void *)-1)
3773 e->aux = NULL;
3774 continue;
3777 if (dump_file && (dump_flags & TDF_DETAILS))
3779 fprintf (dump_file, "removing unreachable case label\n");
3781 to_remove_edges.safe_push (e);
3782 e->flags &= ~EDGE_EXECUTABLE;
3785 /* And queue an update for the stmt. */
3786 su.stmt = stmt;
3787 su.vec = vec2;
3788 to_update_switch_stmts.safe_push (su);
3789 return false;
3792 /* Simplify an integral conversion from an SSA name in STMT. */
3794 static bool
3795 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3797 tree innerop, middleop, finaltype;
3798 gimple *def_stmt;
3799 signop inner_sgn, middle_sgn, final_sgn;
3800 unsigned inner_prec, middle_prec, final_prec;
3801 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
3803 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
3804 if (!INTEGRAL_TYPE_P (finaltype))
3805 return false;
3806 middleop = gimple_assign_rhs1 (stmt);
3807 def_stmt = SSA_NAME_DEF_STMT (middleop);
3808 if (!is_gimple_assign (def_stmt)
3809 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3810 return false;
3811 innerop = gimple_assign_rhs1 (def_stmt);
3812 if (TREE_CODE (innerop) != SSA_NAME
3813 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
3814 return false;
3816 /* Get the value-range of the inner operand. Use get_range_info in
3817 case innerop was created during substitute-and-fold. */
3818 wide_int imin, imax;
3819 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
3820 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
3821 return false;
3822 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
3823 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
3825 /* Simulate the conversion chain to check if the result is equal if
3826 the middle conversion is removed. */
3827 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
3828 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
3829 final_prec = TYPE_PRECISION (finaltype);
3831 /* If the first conversion is not injective, the second must not
3832 be widening. */
3833 if (wi::gtu_p (innermax - innermin,
3834 wi::mask <widest_int> (middle_prec, false))
3835 && middle_prec < final_prec)
3836 return false;
3837 /* We also want a medium value so that we can track the effect that
3838 narrowing conversions with sign change have. */
3839 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
3840 if (inner_sgn == UNSIGNED)
3841 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
3842 else
3843 innermed = 0;
3844 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
3845 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
3846 innermed = innermin;
3848 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
3849 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
3850 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
3851 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
3853 /* Require that the final conversion applied to both the original
3854 and the intermediate range produces the same result. */
3855 final_sgn = TYPE_SIGN (finaltype);
3856 if (wi::ext (middlemin, final_prec, final_sgn)
3857 != wi::ext (innermin, final_prec, final_sgn)
3858 || wi::ext (middlemed, final_prec, final_sgn)
3859 != wi::ext (innermed, final_prec, final_sgn)
3860 || wi::ext (middlemax, final_prec, final_sgn)
3861 != wi::ext (innermax, final_prec, final_sgn))
3862 return false;
3864 gimple_assign_set_rhs1 (stmt, innerop);
3865 fold_stmt (gsi, follow_single_use_edges);
3866 return true;
3869 /* Simplify a conversion from integral SSA name to float in STMT. */
3871 bool
3872 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
3873 gimple *stmt)
3875 tree rhs1 = gimple_assign_rhs1 (stmt);
3876 value_range *vr = get_value_range (rhs1);
3877 scalar_float_mode fltmode
3878 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
3879 scalar_int_mode mode;
3880 tree tem;
3881 gassign *conv;
3883 /* We can only handle constant ranges. */
3884 if (vr->type != VR_RANGE
3885 || TREE_CODE (vr->min) != INTEGER_CST
3886 || TREE_CODE (vr->max) != INTEGER_CST)
3887 return false;
3889 /* First check if we can use a signed type in place of an unsigned. */
3890 scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
3891 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
3892 && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
3893 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
3894 mode = rhs_mode;
3895 /* If we can do the conversion in the current input mode do nothing. */
3896 else if (can_float_p (fltmode, rhs_mode,
3897 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
3898 return false;
3899 /* Otherwise search for a mode we can use, starting from the narrowest
3900 integer mode available. */
3901 else
3903 mode = NARROWEST_INT_MODE;
3904 for (;;)
3906 /* If we cannot do a signed conversion to float from mode
3907 or if the value-range does not fit in the signed type
3908 try with a wider mode. */
3909 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
3910 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
3911 break;
3913 /* But do not widen the input. Instead leave that to the
3914 optabs expansion code. */
3915 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
3916 || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
3917 return false;
3921 /* It works, insert a truncation or sign-change before the
3922 float conversion. */
3923 tem = make_ssa_name (build_nonstandard_integer_type
3924 (GET_MODE_PRECISION (mode), 0));
3925 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
3926 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
3927 gimple_assign_set_rhs1 (stmt, tem);
3928 fold_stmt (gsi, follow_single_use_edges);
3930 return true;
3933 /* Simplify an internal fn call using ranges if possible. */
3935 bool
3936 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi,
3937 gimple *stmt)
3939 enum tree_code subcode;
3940 bool is_ubsan = false;
3941 bool ovf = false;
3942 switch (gimple_call_internal_fn (stmt))
3944 case IFN_UBSAN_CHECK_ADD:
3945 subcode = PLUS_EXPR;
3946 is_ubsan = true;
3947 break;
3948 case IFN_UBSAN_CHECK_SUB:
3949 subcode = MINUS_EXPR;
3950 is_ubsan = true;
3951 break;
3952 case IFN_UBSAN_CHECK_MUL:
3953 subcode = MULT_EXPR;
3954 is_ubsan = true;
3955 break;
3956 case IFN_ADD_OVERFLOW:
3957 subcode = PLUS_EXPR;
3958 break;
3959 case IFN_SUB_OVERFLOW:
3960 subcode = MINUS_EXPR;
3961 break;
3962 case IFN_MUL_OVERFLOW:
3963 subcode = MULT_EXPR;
3964 break;
3965 default:
3966 return false;
3969 tree op0 = gimple_call_arg (stmt, 0);
3970 tree op1 = gimple_call_arg (stmt, 1);
3971 tree type;
3972 if (is_ubsan)
3974 type = TREE_TYPE (op0);
3975 if (VECTOR_TYPE_P (type))
3976 return false;
3978 else if (gimple_call_lhs (stmt) == NULL_TREE)
3979 return false;
3980 else
3981 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
3982 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
3983 || (is_ubsan && ovf))
3984 return false;
3986 gimple *g;
3987 location_t loc = gimple_location (stmt);
3988 if (is_ubsan)
3989 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
3990 else
3992 int prec = TYPE_PRECISION (type);
3993 tree utype = type;
3994 if (ovf
3995 || !useless_type_conversion_p (type, TREE_TYPE (op0))
3996 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
3997 utype = build_nonstandard_integer_type (prec, 1);
3998 if (TREE_CODE (op0) == INTEGER_CST)
3999 op0 = fold_convert (utype, op0);
4000 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
4002 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
4003 gimple_set_location (g, loc);
4004 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4005 op0 = gimple_assign_lhs (g);
4007 if (TREE_CODE (op1) == INTEGER_CST)
4008 op1 = fold_convert (utype, op1);
4009 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
4011 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
4012 gimple_set_location (g, loc);
4013 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4014 op1 = gimple_assign_lhs (g);
4016 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
4017 gimple_set_location (g, loc);
4018 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4019 if (utype != type)
4021 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
4022 gimple_assign_lhs (g));
4023 gimple_set_location (g, loc);
4024 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4026 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
4027 gimple_assign_lhs (g),
4028 build_int_cst (type, ovf));
4030 gimple_set_location (g, loc);
4031 gsi_replace (gsi, g, false);
4032 return true;
4035 /* Return true if VAR is a two-valued variable. Set a and b with the
4036 two-values when it is true. Return false otherwise. */
4038 bool
4039 vr_values::two_valued_val_range_p (tree var, tree *a, tree *b)
4041 value_range *vr = get_value_range (var);
4042 if ((vr->type != VR_RANGE
4043 && vr->type != VR_ANTI_RANGE)
4044 || TREE_CODE (vr->min) != INTEGER_CST
4045 || TREE_CODE (vr->max) != INTEGER_CST)
4046 return false;
4048 if (vr->type == VR_RANGE
4049 && wi::to_wide (vr->max) - wi::to_wide (vr->min) == 1)
4051 *a = vr->min;
4052 *b = vr->max;
4053 return true;
4056 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4057 if (vr->type == VR_ANTI_RANGE
4058 && (wi::to_wide (vr->min)
4059 - wi::to_wide (vrp_val_min (TREE_TYPE (var)))) == 1
4060 && (wi::to_wide (vrp_val_max (TREE_TYPE (var)))
4061 - wi::to_wide (vr->max)) == 1)
4063 *a = vrp_val_min (TREE_TYPE (var));
4064 *b = vrp_val_max (TREE_TYPE (var));
4065 return true;
4068 return false;
4071 /* Simplify STMT using ranges if possible. */
4073 bool
4074 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
4076 gimple *stmt = gsi_stmt (*gsi);
4077 if (is_gimple_assign (stmt))
4079 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4080 tree rhs1 = gimple_assign_rhs1 (stmt);
4081 tree rhs2 = gimple_assign_rhs2 (stmt);
4082 tree lhs = gimple_assign_lhs (stmt);
4083 tree val1 = NULL_TREE, val2 = NULL_TREE;
4084 use_operand_p use_p;
4085 gimple *use_stmt;
4087 /* Convert:
4088 LHS = CST BINOP VAR
4089 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4091 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4093 Also handles:
4094 LHS = VAR BINOP CST
4095 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4097 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4099 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
4100 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4101 && ((TREE_CODE (rhs1) == INTEGER_CST
4102 && TREE_CODE (rhs2) == SSA_NAME)
4103 || (TREE_CODE (rhs2) == INTEGER_CST
4104 && TREE_CODE (rhs1) == SSA_NAME))
4105 && single_imm_use (lhs, &use_p, &use_stmt)
4106 && gimple_code (use_stmt) == GIMPLE_COND)
4109 tree new_rhs1 = NULL_TREE;
4110 tree new_rhs2 = NULL_TREE;
4111 tree cmp_var = NULL_TREE;
4113 if (TREE_CODE (rhs2) == SSA_NAME
4114 && two_valued_val_range_p (rhs2, &val1, &val2))
4116 /* Optimize RHS1 OP [VAL1, VAL2]. */
4117 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
4118 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
4119 cmp_var = rhs2;
4121 else if (TREE_CODE (rhs1) == SSA_NAME
4122 && two_valued_val_range_p (rhs1, &val1, &val2))
4124 /* Optimize [VAL1, VAL2] OP RHS2. */
4125 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
4126 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
4127 cmp_var = rhs1;
4130 /* If we could not find two-vals or the optimzation is invalid as
4131 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4132 if (new_rhs1 && new_rhs2)
4134 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
4135 gimple_assign_set_rhs_with_ops (gsi,
4136 COND_EXPR, cond,
4137 new_rhs1,
4138 new_rhs2);
4139 update_stmt (gsi_stmt (*gsi));
4140 fold_stmt (gsi, follow_single_use_edges);
4141 return true;
4145 switch (rhs_code)
4147 case EQ_EXPR:
4148 case NE_EXPR:
4149 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4150 if the RHS is zero or one, and the LHS are known to be boolean
4151 values. */
4152 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4153 return simplify_truth_ops_using_ranges (gsi, stmt);
4154 break;
4156 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4157 and BIT_AND_EXPR respectively if the first operand is greater
4158 than zero and the second operand is an exact power of two.
4159 Also optimize TRUNC_MOD_EXPR away if the second operand is
4160 constant and the first operand already has the right value
4161 range. */
4162 case TRUNC_DIV_EXPR:
4163 case TRUNC_MOD_EXPR:
4164 if ((TREE_CODE (rhs1) == SSA_NAME
4165 || TREE_CODE (rhs1) == INTEGER_CST)
4166 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4167 return simplify_div_or_mod_using_ranges (gsi, stmt);
4168 break;
4170 /* Transform ABS (X) into X or -X as appropriate. */
4171 case ABS_EXPR:
4172 if (TREE_CODE (rhs1) == SSA_NAME
4173 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4174 return simplify_abs_using_ranges (gsi, stmt);
4175 break;
4177 case BIT_AND_EXPR:
4178 case BIT_IOR_EXPR:
4179 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4180 if all the bits being cleared are already cleared or
4181 all the bits being set are already set. */
4182 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4183 return simplify_bit_ops_using_ranges (gsi, stmt);
4184 break;
4186 CASE_CONVERT:
4187 if (TREE_CODE (rhs1) == SSA_NAME
4188 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4189 return simplify_conversion_using_ranges (gsi, stmt);
4190 break;
4192 case FLOAT_EXPR:
4193 if (TREE_CODE (rhs1) == SSA_NAME
4194 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4195 return simplify_float_conversion_using_ranges (gsi, stmt);
4196 break;
4198 case MIN_EXPR:
4199 case MAX_EXPR:
4200 return simplify_min_or_max_using_ranges (gsi, stmt);
4202 default:
4203 break;
4206 else if (gimple_code (stmt) == GIMPLE_COND)
4207 return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
4208 else if (gimple_code (stmt) == GIMPLE_SWITCH)
4209 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
4210 else if (is_gimple_call (stmt)
4211 && gimple_call_internal_p (stmt))
4212 return simplify_internal_call_using_ranges (gsi, stmt);
4214 return false;
4217 void
4218 vr_values::set_vr_value (tree var, value_range *vr)
4220 if (SSA_NAME_VERSION (var) >= num_vr_values)
4221 return;
4222 vr_value[SSA_NAME_VERSION (var)] = vr;