Fix compilation failure with C++98 compilers
[official-gcc.git] / gcc / vr-values.c
blob32392a11618dfd30fd4369c769dc890f4e702dbc
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"
50 #include "cfghooks.h"
52 /* Set value range VR to a non-negative range of type TYPE. */
54 static inline void
55 set_value_range_to_nonnegative (value_range *vr, tree type)
57 tree zero = build_int_cst (type, 0);
58 set_value_range (vr, VR_RANGE, zero, vrp_val_max (type), vr->equiv);
61 /* Set value range VR to a range of a truthvalue of type TYPE. */
63 static inline void
64 set_value_range_to_truthvalue (value_range *vr, tree type)
66 if (TYPE_PRECISION (type) == 1)
67 set_value_range_to_varying (vr);
68 else
69 set_value_range (vr, VR_RANGE,
70 build_int_cst (type, 0), build_int_cst (type, 1),
71 vr->equiv);
75 /* Return value range information for VAR.
77 If we have no values ranges recorded (ie, VRP is not running), then
78 return NULL. Otherwise create an empty range if none existed for VAR. */
80 value_range *
81 vr_values::get_value_range (const_tree var)
83 static const value_range vr_const_varying
84 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
85 value_range *vr;
86 tree sym;
87 unsigned ver = SSA_NAME_VERSION (var);
89 /* If we have no recorded ranges, then return NULL. */
90 if (! vr_value)
91 return NULL;
93 /* If we query the range for a new SSA name return an unmodifiable VARYING.
94 We should get here at most from the substitute-and-fold stage which
95 will never try to change values. */
96 if (ver >= num_vr_values)
97 return CONST_CAST (value_range *, &vr_const_varying);
99 vr = vr_value[ver];
100 if (vr)
101 return vr;
103 /* After propagation finished do not allocate new value-ranges. */
104 if (values_propagated)
105 return CONST_CAST (value_range *, &vr_const_varying);
107 /* Create a default value range. */
108 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
109 memset (vr, 0, sizeof (*vr));
111 /* Defer allocating the equivalence set. */
112 vr->equiv = NULL;
114 /* If VAR is a default definition of a parameter, the variable can
115 take any value in VAR's type. */
116 if (SSA_NAME_IS_DEFAULT_DEF (var))
118 sym = SSA_NAME_VAR (var);
119 if (TREE_CODE (sym) == PARM_DECL)
121 /* Try to use the "nonnull" attribute to create ~[0, 0]
122 anti-ranges for pointers. Note that this is only valid with
123 default definitions of PARM_DECLs. */
124 if (POINTER_TYPE_P (TREE_TYPE (sym))
125 && (nonnull_arg_p (sym)
126 || get_ptr_nonnull (var)))
127 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
128 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
130 wide_int min, max;
131 value_range_type rtype = get_range_info (var, &min, &max);
132 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
133 set_value_range (vr, rtype,
134 wide_int_to_tree (TREE_TYPE (var), min),
135 wide_int_to_tree (TREE_TYPE (var), max),
136 NULL);
137 else
138 set_value_range_to_varying (vr);
140 else
141 set_value_range_to_varying (vr);
143 else if (TREE_CODE (sym) == RESULT_DECL
144 && DECL_BY_REFERENCE (sym))
145 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
148 return vr;
151 /* Set value-ranges of all SSA names defined by STMT to varying. */
153 void
154 vr_values::set_defs_to_varying (gimple *stmt)
156 ssa_op_iter i;
157 tree def;
158 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
160 value_range *vr = get_value_range (def);
161 /* Avoid writing to vr_const_varying get_value_range may return. */
162 if (vr->type != VR_VARYING)
163 set_value_range_to_varying (vr);
167 /* Update the value range and equivalence set for variable VAR to
168 NEW_VR. Return true if NEW_VR is different from VAR's previous
169 value.
171 NOTE: This function assumes that NEW_VR is a temporary value range
172 object created for the sole purpose of updating VAR's range. The
173 storage used by the equivalence set from NEW_VR will be freed by
174 this function. Do not call update_value_range when NEW_VR
175 is the range object associated with another SSA name. */
177 bool
178 vr_values::update_value_range (const_tree var, value_range *new_vr)
180 value_range *old_vr;
181 bool is_new;
183 /* If there is a value-range on the SSA name from earlier analysis
184 factor that in. */
185 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
187 wide_int min, max;
188 value_range_type rtype = get_range_info (var, &min, &max);
189 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
191 tree nr_min, nr_max;
192 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
193 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
194 value_range nr = VR_INITIALIZER;
195 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
196 vrp_intersect_ranges (new_vr, &nr);
200 /* Update the value range, if necessary. */
201 old_vr = get_value_range (var);
202 is_new = old_vr->type != new_vr->type
203 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
204 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
205 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
207 if (is_new)
209 /* Do not allow transitions up the lattice. The following
210 is slightly more awkward than just new_vr->type < old_vr->type
211 because VR_RANGE and VR_ANTI_RANGE need to be considered
212 the same. We may not have is_new when transitioning to
213 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
214 called. */
215 if (new_vr->type == VR_UNDEFINED)
217 BITMAP_FREE (new_vr->equiv);
218 set_value_range_to_varying (old_vr);
219 set_value_range_to_varying (new_vr);
220 return true;
222 else
223 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
224 new_vr->equiv);
227 BITMAP_FREE (new_vr->equiv);
229 return is_new;
233 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
234 point where equivalence processing can be turned on/off. */
236 void
237 vr_values::add_equivalence (bitmap *equiv, const_tree var)
239 unsigned ver = SSA_NAME_VERSION (var);
240 value_range *vr = get_value_range (var);
242 if (*equiv == NULL)
243 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
244 bitmap_set_bit (*equiv, ver);
245 if (vr && vr->equiv)
246 bitmap_ior_into (*equiv, vr->equiv);
249 /* Return true if value range VR involves exactly one symbol SYM. */
251 static bool
252 symbolic_range_based_on_p (value_range *vr, const_tree sym)
254 bool neg, min_has_symbol, max_has_symbol;
255 tree inv;
257 if (is_gimple_min_invariant (vr->min))
258 min_has_symbol = false;
259 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
260 min_has_symbol = true;
261 else
262 return false;
264 if (is_gimple_min_invariant (vr->max))
265 max_has_symbol = false;
266 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
267 max_has_symbol = true;
268 else
269 return false;
271 return (min_has_symbol || max_has_symbol);
274 /* Return true if the result of assignment STMT is know to be non-zero. */
276 static bool
277 gimple_assign_nonzero_p (gimple *stmt)
279 enum tree_code code = gimple_assign_rhs_code (stmt);
280 bool strict_overflow_p;
281 switch (get_gimple_rhs_class (code))
283 case GIMPLE_UNARY_RHS:
284 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
285 gimple_expr_type (stmt),
286 gimple_assign_rhs1 (stmt),
287 &strict_overflow_p);
288 case GIMPLE_BINARY_RHS:
289 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
290 gimple_expr_type (stmt),
291 gimple_assign_rhs1 (stmt),
292 gimple_assign_rhs2 (stmt),
293 &strict_overflow_p);
294 case GIMPLE_TERNARY_RHS:
295 return false;
296 case GIMPLE_SINGLE_RHS:
297 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
298 &strict_overflow_p);
299 case GIMPLE_INVALID_RHS:
300 gcc_unreachable ();
301 default:
302 gcc_unreachable ();
306 /* Return true if STMT is known to compute a non-zero value. */
308 static bool
309 gimple_stmt_nonzero_p (gimple *stmt)
311 switch (gimple_code (stmt))
313 case GIMPLE_ASSIGN:
314 return gimple_assign_nonzero_p (stmt);
315 case GIMPLE_CALL:
317 gcall *call_stmt = as_a<gcall *> (stmt);
318 return (gimple_call_nonnull_result_p (call_stmt)
319 || gimple_call_nonnull_arg (call_stmt));
321 default:
322 gcc_unreachable ();
325 /* Like tree_expr_nonzero_p, but this function uses value ranges
326 obtained so far. */
328 bool
329 vr_values::vrp_stmt_computes_nonzero (gimple *stmt)
331 if (gimple_stmt_nonzero_p (stmt))
332 return true;
334 /* If we have an expression of the form &X->a, then the expression
335 is nonnull if X is nonnull. */
336 if (is_gimple_assign (stmt)
337 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
339 tree expr = gimple_assign_rhs1 (stmt);
340 tree base = get_base_address (TREE_OPERAND (expr, 0));
342 if (base != NULL_TREE
343 && TREE_CODE (base) == MEM_REF
344 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
346 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
347 if (!range_includes_zero_p (vr))
348 return true;
352 return false;
355 /* Returns true if EXPR is a valid value (as expected by compare_values) --
356 a gimple invariant, or SSA_NAME +- CST. */
358 static bool
359 valid_value_p (tree expr)
361 if (TREE_CODE (expr) == SSA_NAME)
362 return true;
364 if (TREE_CODE (expr) == PLUS_EXPR
365 || TREE_CODE (expr) == MINUS_EXPR)
366 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
367 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
369 return is_gimple_min_invariant (expr);
372 /* If OP has a value range with a single constant value return that,
373 otherwise return NULL_TREE. This returns OP itself if OP is a
374 constant. */
376 tree
377 vr_values::op_with_constant_singleton_value_range (tree op)
379 if (is_gimple_min_invariant (op))
380 return op;
382 if (TREE_CODE (op) != SSA_NAME)
383 return NULL_TREE;
385 return value_range_constant_singleton (get_value_range (op));
388 /* Return true if op is in a boolean [0, 1] value-range. */
390 bool
391 vr_values::op_with_boolean_value_range_p (tree op)
393 value_range *vr;
395 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
396 return true;
398 if (integer_zerop (op)
399 || integer_onep (op))
400 return true;
402 if (TREE_CODE (op) != SSA_NAME)
403 return false;
405 vr = get_value_range (op);
406 return (vr->type == VR_RANGE
407 && integer_zerop (vr->min)
408 && integer_onep (vr->max));
411 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
412 true and store it in *VR_P. */
414 void
415 vr_values::extract_range_for_var_from_comparison_expr (tree var,
416 enum tree_code cond_code,
417 tree op, tree limit,
418 value_range *vr_p)
420 tree min, max, type;
421 value_range *limit_vr;
422 type = TREE_TYPE (var);
424 /* For pointer arithmetic, we only keep track of pointer equality
425 and inequality. If we arrive here with unfolded conditions like
426 _1 > _1 do not derive anything. */
427 if ((POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
428 || limit == var)
430 set_value_range_to_varying (vr_p);
431 return;
434 /* If LIMIT is another SSA name and LIMIT has a range of its own,
435 try to use LIMIT's range to avoid creating symbolic ranges
436 unnecessarily. */
437 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
439 /* LIMIT's range is only interesting if it has any useful information. */
440 if (! limit_vr
441 || limit_vr->type == VR_UNDEFINED
442 || limit_vr->type == VR_VARYING
443 || (symbolic_range_p (limit_vr)
444 && ! (limit_vr->type == VR_RANGE
445 && (limit_vr->min == limit_vr->max
446 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
447 limit_vr = NULL;
449 /* Initially, the new range has the same set of equivalences of
450 VAR's range. This will be revised before returning the final
451 value. Since assertions may be chained via mutually exclusive
452 predicates, we will need to trim the set of equivalences before
453 we are done. */
454 gcc_assert (vr_p->equiv == NULL);
455 add_equivalence (&vr_p->equiv, var);
457 /* Extract a new range based on the asserted comparison for VAR and
458 LIMIT's value range. Notice that if LIMIT has an anti-range, we
459 will only use it for equality comparisons (EQ_EXPR). For any
460 other kind of assertion, we cannot derive a range from LIMIT's
461 anti-range that can be used to describe the new range. For
462 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
463 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
464 no single range for x_2 that could describe LE_EXPR, so we might
465 as well build the range [b_4, +INF] for it.
466 One special case we handle is extracting a range from a
467 range test encoded as (unsigned)var + CST <= limit. */
468 if (TREE_CODE (op) == NOP_EXPR
469 || TREE_CODE (op) == PLUS_EXPR)
471 if (TREE_CODE (op) == PLUS_EXPR)
473 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
474 TREE_OPERAND (op, 1));
475 max = int_const_binop (PLUS_EXPR, limit, min);
476 op = TREE_OPERAND (op, 0);
478 else
480 min = build_int_cst (TREE_TYPE (var), 0);
481 max = limit;
484 /* Make sure to not set TREE_OVERFLOW on the final type
485 conversion. We are willingly interpreting large positive
486 unsigned values as negative signed values here. */
487 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
488 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
490 /* We can transform a max, min range to an anti-range or
491 vice-versa. Use set_and_canonicalize_value_range which does
492 this for us. */
493 if (cond_code == LE_EXPR)
494 set_and_canonicalize_value_range (vr_p, VR_RANGE,
495 min, max, vr_p->equiv);
496 else if (cond_code == GT_EXPR)
497 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
498 min, max, vr_p->equiv);
499 else
500 gcc_unreachable ();
502 else if (cond_code == EQ_EXPR)
504 enum value_range_type range_type;
506 if (limit_vr)
508 range_type = limit_vr->type;
509 min = limit_vr->min;
510 max = limit_vr->max;
512 else
514 range_type = VR_RANGE;
515 min = limit;
516 max = limit;
519 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
521 /* When asserting the equality VAR == LIMIT and LIMIT is another
522 SSA name, the new range will also inherit the equivalence set
523 from LIMIT. */
524 if (TREE_CODE (limit) == SSA_NAME)
525 add_equivalence (&vr_p->equiv, limit);
527 else if (cond_code == NE_EXPR)
529 /* As described above, when LIMIT's range is an anti-range and
530 this assertion is an inequality (NE_EXPR), then we cannot
531 derive anything from the anti-range. For instance, if
532 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
533 not imply that VAR's range is [0, 0]. So, in the case of
534 anti-ranges, we just assert the inequality using LIMIT and
535 not its anti-range.
537 If LIMIT_VR is a range, we can only use it to build a new
538 anti-range if LIMIT_VR is a single-valued range. For
539 instance, if LIMIT_VR is [0, 1], the predicate
540 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
541 Rather, it means that for value 0 VAR should be ~[0, 0]
542 and for value 1, VAR should be ~[1, 1]. We cannot
543 represent these ranges.
545 The only situation in which we can build a valid
546 anti-range is when LIMIT_VR is a single-valued range
547 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
548 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
549 if (limit_vr
550 && limit_vr->type == VR_RANGE
551 && compare_values (limit_vr->min, limit_vr->max) == 0)
553 min = limit_vr->min;
554 max = limit_vr->max;
556 else
558 /* In any other case, we cannot use LIMIT's range to build a
559 valid anti-range. */
560 min = max = limit;
563 /* If MIN and MAX cover the whole range for their type, then
564 just use the original LIMIT. */
565 if (INTEGRAL_TYPE_P (type)
566 && vrp_val_is_min (min)
567 && vrp_val_is_max (max))
568 min = max = limit;
570 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
571 min, max, vr_p->equiv);
573 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
575 min = TYPE_MIN_VALUE (type);
577 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
578 max = limit;
579 else
581 /* If LIMIT_VR is of the form [N1, N2], we need to build the
582 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
583 LT_EXPR. */
584 max = limit_vr->max;
587 /* If the maximum value forces us to be out of bounds, simply punt.
588 It would be pointless to try and do anything more since this
589 all should be optimized away above us. */
590 if (cond_code == LT_EXPR
591 && compare_values (max, min) == 0)
592 set_value_range_to_varying (vr_p);
593 else
595 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
596 if (cond_code == LT_EXPR)
598 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
599 && !TYPE_UNSIGNED (TREE_TYPE (max)))
600 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
601 build_int_cst (TREE_TYPE (max), -1));
602 else
603 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
604 build_int_cst (TREE_TYPE (max), 1));
605 /* Signal to compare_values_warnv this expr doesn't overflow. */
606 if (EXPR_P (max))
607 TREE_NO_WARNING (max) = 1;
610 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
613 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
615 max = TYPE_MAX_VALUE (type);
617 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
618 min = limit;
619 else
621 /* If LIMIT_VR is of the form [N1, N2], we need to build the
622 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
623 GT_EXPR. */
624 min = limit_vr->min;
627 /* If the minimum value forces us to be out of bounds, simply punt.
628 It would be pointless to try and do anything more since this
629 all should be optimized away above us. */
630 if (cond_code == GT_EXPR
631 && compare_values (min, max) == 0)
632 set_value_range_to_varying (vr_p);
633 else
635 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
636 if (cond_code == GT_EXPR)
638 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
639 && !TYPE_UNSIGNED (TREE_TYPE (min)))
640 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
641 build_int_cst (TREE_TYPE (min), -1));
642 else
643 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
644 build_int_cst (TREE_TYPE (min), 1));
645 /* Signal to compare_values_warnv this expr doesn't overflow. */
646 if (EXPR_P (min))
647 TREE_NO_WARNING (min) = 1;
650 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
653 else
654 gcc_unreachable ();
656 /* Finally intersect the new range with what we already know about var. */
657 vrp_intersect_ranges (vr_p, get_value_range (var));
660 /* Extract value range information from an ASSERT_EXPR EXPR and store
661 it in *VR_P. */
663 void
664 vr_values::extract_range_from_assert (value_range *vr_p, tree expr)
666 tree var = ASSERT_EXPR_VAR (expr);
667 tree cond = ASSERT_EXPR_COND (expr);
668 tree limit, op;
669 enum tree_code cond_code;
670 gcc_assert (COMPARISON_CLASS_P (cond));
672 /* Find VAR in the ASSERT_EXPR conditional. */
673 if (var == TREE_OPERAND (cond, 0)
674 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
675 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
677 /* If the predicate is of the form VAR COMP LIMIT, then we just
678 take LIMIT from the RHS and use the same comparison code. */
679 cond_code = TREE_CODE (cond);
680 limit = TREE_OPERAND (cond, 1);
681 op = TREE_OPERAND (cond, 0);
683 else
685 /* If the predicate is of the form LIMIT COMP VAR, then we need
686 to flip around the comparison code to create the proper range
687 for VAR. */
688 cond_code = swap_tree_comparison (TREE_CODE (cond));
689 limit = TREE_OPERAND (cond, 0);
690 op = TREE_OPERAND (cond, 1);
692 extract_range_for_var_from_comparison_expr (var, cond_code, op,
693 limit, vr_p);
696 /* Extract range information from SSA name VAR and store it in VR. If
697 VAR has an interesting range, use it. Otherwise, create the
698 range [VAR, VAR] and return it. This is useful in situations where
699 we may have conditionals testing values of VARYING names. For
700 instance,
702 x_3 = y_5;
703 if (x_3 > y_5)
706 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
707 always false. */
709 void
710 vr_values::extract_range_from_ssa_name (value_range *vr, tree var)
712 value_range *var_vr = get_value_range (var);
714 if (var_vr->type != VR_VARYING)
715 copy_value_range (vr, var_vr);
716 else
717 set_value_range (vr, VR_RANGE, var, var, NULL);
719 add_equivalence (&vr->equiv, var);
722 /* Extract range information from a binary expression OP0 CODE OP1 based on
723 the ranges of each of its operands with resulting type EXPR_TYPE.
724 The resulting range is stored in *VR. */
726 void
727 vr_values::extract_range_from_binary_expr (value_range *vr,
728 enum tree_code code,
729 tree expr_type, tree op0, tree op1)
731 value_range vr0 = VR_INITIALIZER;
732 value_range vr1 = VR_INITIALIZER;
734 /* Get value ranges for each operand. For constant operands, create
735 a new value range with the operand to simplify processing. */
736 if (TREE_CODE (op0) == SSA_NAME)
737 vr0 = *(get_value_range (op0));
738 else if (is_gimple_min_invariant (op0))
739 set_value_range_to_value (&vr0, op0, NULL);
740 else
741 set_value_range_to_varying (&vr0);
743 if (TREE_CODE (op1) == SSA_NAME)
744 vr1 = *(get_value_range (op1));
745 else if (is_gimple_min_invariant (op1))
746 set_value_range_to_value (&vr1, op1, NULL);
747 else
748 set_value_range_to_varying (&vr1);
750 /* If one argument is varying, we can sometimes still deduce a
751 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
752 if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
753 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
755 if (vr0.type == VR_VARYING && vr1.type != VR_VARYING)
757 vr0.type = VR_RANGE;
758 vr0.min = vrp_val_min (expr_type);
759 vr0.max = vrp_val_max (expr_type);
761 else if (vr1.type == VR_VARYING && vr0.type != VR_VARYING)
763 vr1.type = VR_RANGE;
764 vr1.min = vrp_val_min (expr_type);
765 vr1.max = vrp_val_max (expr_type);
769 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
771 /* Set value_range for n in following sequence:
772 def = __builtin_memchr (arg, 0, sz)
773 n = def - arg
774 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
776 if (vr->type == VR_VARYING
777 && code == POINTER_DIFF_EXPR
778 && TREE_CODE (op0) == SSA_NAME
779 && TREE_CODE (op1) == SSA_NAME)
781 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
782 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
783 gcall *call_stmt = NULL;
785 if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
786 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
787 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
788 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
789 && (call_stmt = dyn_cast<gcall *>(SSA_NAME_DEF_STMT (op0)))
790 && gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR)
791 && operand_equal_p (op0, gimple_call_lhs (call_stmt), 0)
792 && operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0)
793 && integer_zerop (gimple_call_arg (call_stmt, 1)))
795 tree max = vrp_val_max (ptrdiff_type_node);
796 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
797 tree range_min = build_zero_cst (expr_type);
798 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
799 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
800 return;
804 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
805 and based on the other operand, for example if it was deduced from a
806 symbolic comparison. When a bound of the range of the first operand
807 is invariant, we set the corresponding bound of the new range to INF
808 in order to avoid recursing on the range of the second operand. */
809 if (vr->type == VR_VARYING
810 && (code == PLUS_EXPR || code == MINUS_EXPR)
811 && TREE_CODE (op1) == SSA_NAME
812 && vr0.type == VR_RANGE
813 && symbolic_range_based_on_p (&vr0, op1))
815 const bool minus_p = (code == MINUS_EXPR);
816 value_range n_vr1 = VR_INITIALIZER;
818 /* Try with VR0 and [-INF, OP1]. */
819 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
820 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
822 /* Try with VR0 and [OP1, +INF]. */
823 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
824 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
826 /* Try with VR0 and [OP1, OP1]. */
827 else
828 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
830 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
833 if (vr->type == VR_VARYING
834 && (code == PLUS_EXPR || code == MINUS_EXPR)
835 && TREE_CODE (op0) == SSA_NAME
836 && vr1.type == VR_RANGE
837 && symbolic_range_based_on_p (&vr1, op0))
839 const bool minus_p = (code == MINUS_EXPR);
840 value_range n_vr0 = VR_INITIALIZER;
842 /* Try with [-INF, OP0] and VR1. */
843 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
844 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
846 /* Try with [OP0, +INF] and VR1. */
847 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
848 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
850 /* Try with [OP0, OP0] and VR1. */
851 else
852 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
854 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
857 /* If we didn't derive a range for MINUS_EXPR, and
858 op1's range is ~[op0,op0] or vice-versa, then we
859 can derive a non-null range. This happens often for
860 pointer subtraction. */
861 if (vr->type == VR_VARYING
862 && (code == MINUS_EXPR || code == POINTER_DIFF_EXPR)
863 && TREE_CODE (op0) == SSA_NAME
864 && ((vr0.type == VR_ANTI_RANGE
865 && vr0.min == op1
866 && vr0.min == vr0.max)
867 || (vr1.type == VR_ANTI_RANGE
868 && vr1.min == op0
869 && vr1.min == vr1.max)))
870 set_value_range_to_nonnull (vr, expr_type);
873 /* Extract range information from a unary expression CODE OP0 based on
874 the range of its operand with resulting type TYPE.
875 The resulting range is stored in *VR. */
877 void
878 vr_values::extract_range_from_unary_expr (value_range *vr, enum tree_code code,
879 tree type, tree op0)
881 value_range vr0 = VR_INITIALIZER;
883 /* Get value ranges for the operand. For constant operands, create
884 a new value range with the operand to simplify processing. */
885 if (TREE_CODE (op0) == SSA_NAME)
886 vr0 = *(get_value_range (op0));
887 else if (is_gimple_min_invariant (op0))
888 set_value_range_to_value (&vr0, op0, NULL);
889 else
890 set_value_range_to_varying (&vr0);
892 ::extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
896 /* Extract range information from a conditional expression STMT based on
897 the ranges of each of its operands and the expression code. */
899 void
900 vr_values::extract_range_from_cond_expr (value_range *vr, gassign *stmt)
902 tree op0, op1;
903 value_range vr0 = VR_INITIALIZER;
904 value_range vr1 = VR_INITIALIZER;
906 /* Get value ranges for each operand. For constant operands, create
907 a new value range with the operand to simplify processing. */
908 op0 = gimple_assign_rhs2 (stmt);
909 if (TREE_CODE (op0) == SSA_NAME)
910 vr0 = *(get_value_range (op0));
911 else if (is_gimple_min_invariant (op0))
912 set_value_range_to_value (&vr0, op0, NULL);
913 else
914 set_value_range_to_varying (&vr0);
916 op1 = gimple_assign_rhs3 (stmt);
917 if (TREE_CODE (op1) == SSA_NAME)
918 vr1 = *(get_value_range (op1));
919 else if (is_gimple_min_invariant (op1))
920 set_value_range_to_value (&vr1, op1, NULL);
921 else
922 set_value_range_to_varying (&vr1);
924 /* The resulting value range is the union of the operand ranges */
925 copy_value_range (vr, &vr0);
926 vrp_meet (vr, &vr1);
930 /* Extract range information from a comparison expression EXPR based
931 on the range of its operand and the expression code. */
933 void
934 vr_values::extract_range_from_comparison (value_range *vr, enum tree_code code,
935 tree type, tree op0, tree op1)
937 bool sop;
938 tree val;
940 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
941 NULL);
942 if (val)
944 /* Since this expression was found on the RHS of an assignment,
945 its type may be different from _Bool. Convert VAL to EXPR's
946 type. */
947 val = fold_convert (type, val);
948 if (is_gimple_min_invariant (val))
949 set_value_range_to_value (vr, val, vr->equiv);
950 else
951 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
953 else
954 /* The result of a comparison is always true or false. */
955 set_value_range_to_truthvalue (vr, type);
958 /* Helper function for simplify_internal_call_using_ranges and
959 extract_range_basic. Return true if OP0 SUBCODE OP1 for
960 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
961 always overflow. Set *OVF to true if it is known to always
962 overflow. */
964 bool
965 vr_values::check_for_binary_op_overflow (enum tree_code subcode, tree type,
966 tree op0, tree op1, bool *ovf)
968 value_range vr0 = VR_INITIALIZER;
969 value_range vr1 = VR_INITIALIZER;
970 if (TREE_CODE (op0) == SSA_NAME)
971 vr0 = *get_value_range (op0);
972 else if (TREE_CODE (op0) == INTEGER_CST)
973 set_value_range_to_value (&vr0, op0, NULL);
974 else
975 set_value_range_to_varying (&vr0);
977 if (TREE_CODE (op1) == SSA_NAME)
978 vr1 = *get_value_range (op1);
979 else if (TREE_CODE (op1) == INTEGER_CST)
980 set_value_range_to_value (&vr1, op1, NULL);
981 else
982 set_value_range_to_varying (&vr1);
984 if (!range_int_cst_p (&vr0)
985 || TREE_OVERFLOW (vr0.min)
986 || TREE_OVERFLOW (vr0.max))
988 vr0.min = vrp_val_min (TREE_TYPE (op0));
989 vr0.max = vrp_val_max (TREE_TYPE (op0));
991 if (!range_int_cst_p (&vr1)
992 || TREE_OVERFLOW (vr1.min)
993 || TREE_OVERFLOW (vr1.max))
995 vr1.min = vrp_val_min (TREE_TYPE (op1));
996 vr1.max = vrp_val_max (TREE_TYPE (op1));
998 *ovf = arith_overflowed_p (subcode, type, vr0.min,
999 subcode == MINUS_EXPR ? vr1.max : vr1.min);
1000 if (arith_overflowed_p (subcode, type, vr0.max,
1001 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
1002 return false;
1003 if (subcode == MULT_EXPR)
1005 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
1006 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
1007 return false;
1009 if (*ovf)
1011 /* So far we found that there is an overflow on the boundaries.
1012 That doesn't prove that there is an overflow even for all values
1013 in between the boundaries. For that compute widest_int range
1014 of the result and see if it doesn't overlap the range of
1015 type. */
1016 widest_int wmin, wmax;
1017 widest_int w[4];
1018 int i;
1019 w[0] = wi::to_widest (vr0.min);
1020 w[1] = wi::to_widest (vr0.max);
1021 w[2] = wi::to_widest (vr1.min);
1022 w[3] = wi::to_widest (vr1.max);
1023 for (i = 0; i < 4; i++)
1025 widest_int wt;
1026 switch (subcode)
1028 case PLUS_EXPR:
1029 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
1030 break;
1031 case MINUS_EXPR:
1032 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
1033 break;
1034 case MULT_EXPR:
1035 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
1036 break;
1037 default:
1038 gcc_unreachable ();
1040 if (i == 0)
1042 wmin = wt;
1043 wmax = wt;
1045 else
1047 wmin = wi::smin (wmin, wt);
1048 wmax = wi::smax (wmax, wt);
1051 /* The result of op0 CODE op1 is known to be in range
1052 [wmin, wmax]. */
1053 widest_int wtmin = wi::to_widest (vrp_val_min (type));
1054 widest_int wtmax = wi::to_widest (vrp_val_max (type));
1055 /* If all values in [wmin, wmax] are smaller than
1056 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1057 the arithmetic operation will always overflow. */
1058 if (wmax < wtmin || wmin > wtmax)
1059 return true;
1060 return false;
1062 return true;
1065 /* Try to derive a nonnegative or nonzero range out of STMT relying
1066 primarily on generic routines in fold in conjunction with range data.
1067 Store the result in *VR */
1069 void
1070 vr_values::extract_range_basic (value_range *vr, gimple *stmt)
1072 bool sop;
1073 tree type = gimple_expr_type (stmt);
1075 if (is_gimple_call (stmt))
1077 tree arg;
1078 int mini, maxi, zerov = 0, prec;
1079 enum tree_code subcode = ERROR_MARK;
1080 combined_fn cfn = gimple_call_combined_fn (stmt);
1081 scalar_int_mode mode;
1083 switch (cfn)
1085 case CFN_BUILT_IN_CONSTANT_P:
1086 /* If the call is __builtin_constant_p and the argument is a
1087 function parameter resolve it to false. This avoids bogus
1088 array bound warnings.
1089 ??? We could do this as early as inlining is finished. */
1090 arg = gimple_call_arg (stmt, 0);
1091 if (TREE_CODE (arg) == SSA_NAME
1092 && SSA_NAME_IS_DEFAULT_DEF (arg)
1093 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
1094 && cfun->after_inlining)
1096 set_value_range_to_null (vr, type);
1097 return;
1099 break;
1100 /* Both __builtin_ffs* and __builtin_popcount return
1101 [0, prec]. */
1102 CASE_CFN_FFS:
1103 CASE_CFN_POPCOUNT:
1104 arg = gimple_call_arg (stmt, 0);
1105 prec = TYPE_PRECISION (TREE_TYPE (arg));
1106 mini = 0;
1107 maxi = prec;
1108 if (TREE_CODE (arg) == SSA_NAME)
1110 value_range *vr0 = get_value_range (arg);
1111 /* If arg is non-zero, then ffs or popcount are non-zero. */
1112 if (range_includes_zero_p (vr0) == 0)
1113 mini = 1;
1114 /* If some high bits are known to be zero,
1115 we can decrease the maximum. */
1116 if (vr0->type == VR_RANGE
1117 && TREE_CODE (vr0->max) == INTEGER_CST
1118 && !operand_less_p (vr0->min,
1119 build_zero_cst (TREE_TYPE (vr0->min))))
1120 maxi = tree_floor_log2 (vr0->max) + 1;
1122 goto bitop_builtin;
1123 /* __builtin_parity* returns [0, 1]. */
1124 CASE_CFN_PARITY:
1125 mini = 0;
1126 maxi = 1;
1127 goto bitop_builtin;
1128 /* __builtin_c[lt]z* return [0, prec-1], except for
1129 when the argument is 0, but that is undefined behavior.
1130 On many targets where the CLZ RTL or optab value is defined
1131 for 0 the value is prec, so include that in the range
1132 by default. */
1133 CASE_CFN_CLZ:
1134 arg = gimple_call_arg (stmt, 0);
1135 prec = TYPE_PRECISION (TREE_TYPE (arg));
1136 mini = 0;
1137 maxi = prec;
1138 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1139 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1140 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov)
1141 /* Handle only the single common value. */
1142 && zerov != prec)
1143 /* Magic value to give up, unless vr0 proves
1144 arg is non-zero. */
1145 mini = -2;
1146 if (TREE_CODE (arg) == SSA_NAME)
1148 value_range *vr0 = get_value_range (arg);
1149 /* From clz of VR_RANGE minimum we can compute
1150 result maximum. */
1151 if (vr0->type == VR_RANGE
1152 && TREE_CODE (vr0->min) == INTEGER_CST)
1154 maxi = prec - 1 - tree_floor_log2 (vr0->min);
1155 if (maxi != prec)
1156 mini = 0;
1158 else if (vr0->type == VR_ANTI_RANGE
1159 && integer_zerop (vr0->min))
1161 maxi = prec - 1;
1162 mini = 0;
1164 if (mini == -2)
1165 break;
1166 /* From clz of VR_RANGE maximum we can compute
1167 result minimum. */
1168 if (vr0->type == VR_RANGE
1169 && TREE_CODE (vr0->max) == INTEGER_CST)
1171 mini = prec - 1 - tree_floor_log2 (vr0->max);
1172 if (mini == prec)
1173 break;
1176 if (mini == -2)
1177 break;
1178 goto bitop_builtin;
1179 /* __builtin_ctz* return [0, prec-1], except for
1180 when the argument is 0, but that is undefined behavior.
1181 If there is a ctz optab for this mode and
1182 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1183 otherwise just assume 0 won't be seen. */
1184 CASE_CFN_CTZ:
1185 arg = gimple_call_arg (stmt, 0);
1186 prec = TYPE_PRECISION (TREE_TYPE (arg));
1187 mini = 0;
1188 maxi = prec - 1;
1189 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1190 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1191 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov))
1193 /* Handle only the two common values. */
1194 if (zerov == -1)
1195 mini = -1;
1196 else if (zerov == prec)
1197 maxi = prec;
1198 else
1199 /* Magic value to give up, unless vr0 proves
1200 arg is non-zero. */
1201 mini = -2;
1203 if (TREE_CODE (arg) == SSA_NAME)
1205 value_range *vr0 = get_value_range (arg);
1206 /* If arg is non-zero, then use [0, prec - 1]. */
1207 if ((vr0->type == VR_RANGE
1208 && integer_nonzerop (vr0->min))
1209 || (vr0->type == VR_ANTI_RANGE
1210 && integer_zerop (vr0->min)))
1212 mini = 0;
1213 maxi = prec - 1;
1215 /* If some high bits are known to be zero,
1216 we can decrease the result maximum. */
1217 if (vr0->type == VR_RANGE
1218 && TREE_CODE (vr0->max) == INTEGER_CST)
1220 maxi = tree_floor_log2 (vr0->max);
1221 /* For vr0 [0, 0] give up. */
1222 if (maxi == -1)
1223 break;
1226 if (mini == -2)
1227 break;
1228 goto bitop_builtin;
1229 /* __builtin_clrsb* returns [0, prec-1]. */
1230 CASE_CFN_CLRSB:
1231 arg = gimple_call_arg (stmt, 0);
1232 prec = TYPE_PRECISION (TREE_TYPE (arg));
1233 mini = 0;
1234 maxi = prec - 1;
1235 goto bitop_builtin;
1236 bitop_builtin:
1237 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
1238 build_int_cst (type, maxi), NULL);
1239 return;
1240 case CFN_UBSAN_CHECK_ADD:
1241 subcode = PLUS_EXPR;
1242 break;
1243 case CFN_UBSAN_CHECK_SUB:
1244 subcode = MINUS_EXPR;
1245 break;
1246 case CFN_UBSAN_CHECK_MUL:
1247 subcode = MULT_EXPR;
1248 break;
1249 case CFN_GOACC_DIM_SIZE:
1250 case CFN_GOACC_DIM_POS:
1251 /* Optimizing these two internal functions helps the loop
1252 optimizer eliminate outer comparisons. Size is [1,N]
1253 and pos is [0,N-1]. */
1255 bool is_pos = cfn == CFN_GOACC_DIM_POS;
1256 int axis = oacc_get_ifn_dim_arg (stmt);
1257 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1259 if (!size)
1260 /* If it's dynamic, the backend might know a hardware
1261 limitation. */
1262 size = targetm.goacc.dim_limit (axis);
1264 tree type = TREE_TYPE (gimple_call_lhs (stmt));
1265 set_value_range (vr, VR_RANGE,
1266 build_int_cst (type, is_pos ? 0 : 1),
1267 size ? build_int_cst (type, size - is_pos)
1268 : vrp_val_max (type), NULL);
1270 return;
1271 case CFN_BUILT_IN_STRLEN:
1272 if (tree lhs = gimple_call_lhs (stmt))
1273 if (ptrdiff_type_node
1274 && (TYPE_PRECISION (ptrdiff_type_node)
1275 == TYPE_PRECISION (TREE_TYPE (lhs))))
1277 tree type = TREE_TYPE (lhs);
1278 tree max = vrp_val_max (ptrdiff_type_node);
1279 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1280 tree range_min = build_zero_cst (type);
1281 tree range_max = wide_int_to_tree (type, wmax - 1);
1282 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
1283 return;
1285 break;
1286 default:
1287 break;
1289 if (subcode != ERROR_MARK)
1291 bool saved_flag_wrapv = flag_wrapv;
1292 /* Pretend the arithmetics is wrapping. If there is
1293 any overflow, we'll complain, but will actually do
1294 wrapping operation. */
1295 flag_wrapv = 1;
1296 extract_range_from_binary_expr (vr, subcode, type,
1297 gimple_call_arg (stmt, 0),
1298 gimple_call_arg (stmt, 1));
1299 flag_wrapv = saved_flag_wrapv;
1301 /* If for both arguments vrp_valueize returned non-NULL,
1302 this should have been already folded and if not, it
1303 wasn't folded because of overflow. Avoid removing the
1304 UBSAN_CHECK_* calls in that case. */
1305 if (vr->type == VR_RANGE
1306 && (vr->min == vr->max
1307 || operand_equal_p (vr->min, vr->max, 0)))
1308 set_value_range_to_varying (vr);
1309 return;
1312 /* Handle extraction of the two results (result of arithmetics and
1313 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1314 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1315 else if (is_gimple_assign (stmt)
1316 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1317 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1318 && INTEGRAL_TYPE_P (type))
1320 enum tree_code code = gimple_assign_rhs_code (stmt);
1321 tree op = gimple_assign_rhs1 (stmt);
1322 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
1324 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
1325 if (is_gimple_call (g) && gimple_call_internal_p (g))
1327 enum tree_code subcode = ERROR_MARK;
1328 switch (gimple_call_internal_fn (g))
1330 case IFN_ADD_OVERFLOW:
1331 subcode = PLUS_EXPR;
1332 break;
1333 case IFN_SUB_OVERFLOW:
1334 subcode = MINUS_EXPR;
1335 break;
1336 case IFN_MUL_OVERFLOW:
1337 subcode = MULT_EXPR;
1338 break;
1339 case IFN_ATOMIC_COMPARE_EXCHANGE:
1340 if (code == IMAGPART_EXPR)
1342 /* This is the boolean return value whether compare and
1343 exchange changed anything or not. */
1344 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
1345 build_int_cst (type, 1), NULL);
1346 return;
1348 break;
1349 default:
1350 break;
1352 if (subcode != ERROR_MARK)
1354 tree op0 = gimple_call_arg (g, 0);
1355 tree op1 = gimple_call_arg (g, 1);
1356 if (code == IMAGPART_EXPR)
1358 bool ovf = false;
1359 if (check_for_binary_op_overflow (subcode, type,
1360 op0, op1, &ovf))
1361 set_value_range_to_value (vr,
1362 build_int_cst (type, ovf),
1363 NULL);
1364 else if (TYPE_PRECISION (type) == 1
1365 && !TYPE_UNSIGNED (type))
1366 set_value_range_to_varying (vr);
1367 else
1368 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
1369 build_int_cst (type, 1), NULL);
1371 else if (types_compatible_p (type, TREE_TYPE (op0))
1372 && types_compatible_p (type, TREE_TYPE (op1)))
1374 bool saved_flag_wrapv = flag_wrapv;
1375 /* Pretend the arithmetics is wrapping. If there is
1376 any overflow, IMAGPART_EXPR will be set. */
1377 flag_wrapv = 1;
1378 extract_range_from_binary_expr (vr, subcode, type,
1379 op0, op1);
1380 flag_wrapv = saved_flag_wrapv;
1382 else
1384 value_range vr0 = VR_INITIALIZER;
1385 value_range vr1 = VR_INITIALIZER;
1386 bool saved_flag_wrapv = flag_wrapv;
1387 /* Pretend the arithmetics is wrapping. If there is
1388 any overflow, IMAGPART_EXPR will be set. */
1389 flag_wrapv = 1;
1390 extract_range_from_unary_expr (&vr0, NOP_EXPR,
1391 type, op0);
1392 extract_range_from_unary_expr (&vr1, NOP_EXPR,
1393 type, op1);
1394 extract_range_from_binary_expr_1 (vr, subcode, type,
1395 &vr0, &vr1);
1396 flag_wrapv = saved_flag_wrapv;
1398 return;
1403 if (INTEGRAL_TYPE_P (type)
1404 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
1405 set_value_range_to_nonnegative (vr, type);
1406 else if (vrp_stmt_computes_nonzero (stmt))
1407 set_value_range_to_nonnull (vr, type);
1408 else
1409 set_value_range_to_varying (vr);
1413 /* Try to compute a useful range out of assignment STMT and store it
1414 in *VR. */
1416 void
1417 vr_values::extract_range_from_assignment (value_range *vr, gassign *stmt)
1419 enum tree_code code = gimple_assign_rhs_code (stmt);
1421 if (code == ASSERT_EXPR)
1422 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
1423 else if (code == SSA_NAME)
1424 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
1425 else if (TREE_CODE_CLASS (code) == tcc_binary)
1426 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
1427 gimple_expr_type (stmt),
1428 gimple_assign_rhs1 (stmt),
1429 gimple_assign_rhs2 (stmt));
1430 else if (TREE_CODE_CLASS (code) == tcc_unary)
1431 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
1432 gimple_expr_type (stmt),
1433 gimple_assign_rhs1 (stmt));
1434 else if (code == COND_EXPR)
1435 extract_range_from_cond_expr (vr, stmt);
1436 else if (TREE_CODE_CLASS (code) == tcc_comparison)
1437 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
1438 gimple_expr_type (stmt),
1439 gimple_assign_rhs1 (stmt),
1440 gimple_assign_rhs2 (stmt));
1441 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
1442 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1443 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
1444 else
1445 set_value_range_to_varying (vr);
1447 if (vr->type == VR_VARYING)
1448 extract_range_basic (vr, stmt);
1451 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1453 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1454 all the values in the ranges.
1456 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1458 - Return NULL_TREE if it is not always possible to determine the
1459 value of the comparison.
1461 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1462 assumed signed overflow is undefined. */
1465 static tree
1466 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
1467 bool *strict_overflow_p)
1469 /* VARYING or UNDEFINED ranges cannot be compared. */
1470 if (vr0->type == VR_VARYING
1471 || vr0->type == VR_UNDEFINED
1472 || vr1->type == VR_VARYING
1473 || vr1->type == VR_UNDEFINED)
1474 return NULL_TREE;
1476 /* Anti-ranges need to be handled separately. */
1477 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
1479 /* If both are anti-ranges, then we cannot compute any
1480 comparison. */
1481 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
1482 return NULL_TREE;
1484 /* These comparisons are never statically computable. */
1485 if (comp == GT_EXPR
1486 || comp == GE_EXPR
1487 || comp == LT_EXPR
1488 || comp == LE_EXPR)
1489 return NULL_TREE;
1491 /* Equality can be computed only between a range and an
1492 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1493 if (vr0->type == VR_RANGE)
1495 /* To simplify processing, make VR0 the anti-range. */
1496 value_range *tmp = vr0;
1497 vr0 = vr1;
1498 vr1 = tmp;
1501 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
1503 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
1504 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
1505 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1507 return NULL_TREE;
1510 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1511 operands around and change the comparison code. */
1512 if (comp == GT_EXPR || comp == GE_EXPR)
1514 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
1515 std::swap (vr0, vr1);
1518 if (comp == EQ_EXPR)
1520 /* Equality may only be computed if both ranges represent
1521 exactly one value. */
1522 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
1523 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
1525 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
1526 strict_overflow_p);
1527 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
1528 strict_overflow_p);
1529 if (cmp_min == 0 && cmp_max == 0)
1530 return boolean_true_node;
1531 else if (cmp_min != -2 && cmp_max != -2)
1532 return boolean_false_node;
1534 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1535 else if (compare_values_warnv (vr0->min, vr1->max,
1536 strict_overflow_p) == 1
1537 || compare_values_warnv (vr1->min, vr0->max,
1538 strict_overflow_p) == 1)
1539 return boolean_false_node;
1541 return NULL_TREE;
1543 else if (comp == NE_EXPR)
1545 int cmp1, cmp2;
1547 /* If VR0 is completely to the left or completely to the right
1548 of VR1, they are always different. Notice that we need to
1549 make sure that both comparisons yield similar results to
1550 avoid comparing values that cannot be compared at
1551 compile-time. */
1552 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
1553 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
1554 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
1555 return boolean_true_node;
1557 /* If VR0 and VR1 represent a single value and are identical,
1558 return false. */
1559 else if (compare_values_warnv (vr0->min, vr0->max,
1560 strict_overflow_p) == 0
1561 && compare_values_warnv (vr1->min, vr1->max,
1562 strict_overflow_p) == 0
1563 && compare_values_warnv (vr0->min, vr1->min,
1564 strict_overflow_p) == 0
1565 && compare_values_warnv (vr0->max, vr1->max,
1566 strict_overflow_p) == 0)
1567 return boolean_false_node;
1569 /* Otherwise, they may or may not be different. */
1570 else
1571 return NULL_TREE;
1573 else if (comp == LT_EXPR || comp == LE_EXPR)
1575 int tst;
1577 /* If VR0 is to the left of VR1, return true. */
1578 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
1579 if ((comp == LT_EXPR && tst == -1)
1580 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1581 return boolean_true_node;
1583 /* If VR0 is to the right of VR1, return false. */
1584 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
1585 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1586 || (comp == LE_EXPR && tst == 1))
1587 return boolean_false_node;
1589 /* Otherwise, we don't know. */
1590 return NULL_TREE;
1593 gcc_unreachable ();
1596 /* Given a value range VR, a value VAL and a comparison code COMP, return
1597 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1598 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1599 always returns false. Return NULL_TREE if it is not always
1600 possible to determine the value of the comparison. Also set
1601 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1602 assumed signed overflow is undefined. */
1604 static tree
1605 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
1606 bool *strict_overflow_p)
1608 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
1609 return NULL_TREE;
1611 /* Anti-ranges need to be handled separately. */
1612 if (vr->type == VR_ANTI_RANGE)
1614 /* For anti-ranges, the only predicates that we can compute at
1615 compile time are equality and inequality. */
1616 if (comp == GT_EXPR
1617 || comp == GE_EXPR
1618 || comp == LT_EXPR
1619 || comp == LE_EXPR)
1620 return NULL_TREE;
1622 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1623 if (value_inside_range (val, vr->min, vr->max) == 1)
1624 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1626 return NULL_TREE;
1629 if (comp == EQ_EXPR)
1631 /* EQ_EXPR may only be computed if VR represents exactly
1632 one value. */
1633 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
1635 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
1636 if (cmp == 0)
1637 return boolean_true_node;
1638 else if (cmp == -1 || cmp == 1 || cmp == 2)
1639 return boolean_false_node;
1641 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
1642 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
1643 return boolean_false_node;
1645 return NULL_TREE;
1647 else if (comp == NE_EXPR)
1649 /* If VAL is not inside VR, then they are always different. */
1650 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
1651 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
1652 return boolean_true_node;
1654 /* If VR represents exactly one value equal to VAL, then return
1655 false. */
1656 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
1657 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
1658 return boolean_false_node;
1660 /* Otherwise, they may or may not be different. */
1661 return NULL_TREE;
1663 else if (comp == LT_EXPR || comp == LE_EXPR)
1665 int tst;
1667 /* If VR is to the left of VAL, return true. */
1668 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
1669 if ((comp == LT_EXPR && tst == -1)
1670 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1671 return boolean_true_node;
1673 /* If VR is to the right of VAL, return false. */
1674 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
1675 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1676 || (comp == LE_EXPR && tst == 1))
1677 return boolean_false_node;
1679 /* Otherwise, we don't know. */
1680 return NULL_TREE;
1682 else if (comp == GT_EXPR || comp == GE_EXPR)
1684 int tst;
1686 /* If VR is to the right of VAL, return true. */
1687 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
1688 if ((comp == GT_EXPR && tst == 1)
1689 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
1690 return boolean_true_node;
1692 /* If VR is to the left of VAL, return false. */
1693 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
1694 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
1695 || (comp == GE_EXPR && tst == -1))
1696 return boolean_false_node;
1698 /* Otherwise, we don't know. */
1699 return NULL_TREE;
1702 gcc_unreachable ();
1704 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1705 would be profitable to adjust VR using scalar evolution information
1706 for VAR. If so, update VR with the new limits. */
1708 void
1709 vr_values::adjust_range_with_scev (value_range *vr, struct loop *loop,
1710 gimple *stmt, tree var)
1712 tree init, step, chrec, tmin, tmax, min, max, type, tem;
1713 enum ev_direction dir;
1715 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1716 better opportunities than a regular range, but I'm not sure. */
1717 if (vr->type == VR_ANTI_RANGE)
1718 return;
1720 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
1722 /* Like in PR19590, scev can return a constant function. */
1723 if (is_gimple_min_invariant (chrec))
1725 set_value_range_to_value (vr, chrec, vr->equiv);
1726 return;
1729 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1730 return;
1732 init = initial_condition_in_loop_num (chrec, loop->num);
1733 tem = op_with_constant_singleton_value_range (init);
1734 if (tem)
1735 init = tem;
1736 step = evolution_part_in_loop_num (chrec, loop->num);
1737 tem = op_with_constant_singleton_value_range (step);
1738 if (tem)
1739 step = tem;
1741 /* If STEP is symbolic, we can't know whether INIT will be the
1742 minimum or maximum value in the range. Also, unless INIT is
1743 a simple expression, compare_values and possibly other functions
1744 in tree-vrp won't be able to handle it. */
1745 if (step == NULL_TREE
1746 || !is_gimple_min_invariant (step)
1747 || !valid_value_p (init))
1748 return;
1750 dir = scev_direction (chrec);
1751 if (/* Do not adjust ranges if we do not know whether the iv increases
1752 or decreases, ... */
1753 dir == EV_DIR_UNKNOWN
1754 /* ... or if it may wrap. */
1755 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
1756 get_chrec_loop (chrec), true))
1757 return;
1759 type = TREE_TYPE (var);
1760 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1761 tmin = lower_bound_in_type (type, type);
1762 else
1763 tmin = TYPE_MIN_VALUE (type);
1764 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1765 tmax = upper_bound_in_type (type, type);
1766 else
1767 tmax = TYPE_MAX_VALUE (type);
1769 /* Try to use estimated number of iterations for the loop to constrain the
1770 final value in the evolution. */
1771 if (TREE_CODE (step) == INTEGER_CST
1772 && is_gimple_val (init)
1773 && (TREE_CODE (init) != SSA_NAME
1774 || get_value_range (init)->type == VR_RANGE))
1776 widest_int nit;
1778 /* We are only entering here for loop header PHI nodes, so using
1779 the number of latch executions is the correct thing to use. */
1780 if (max_loop_iterations (loop, &nit))
1782 value_range maxvr = VR_INITIALIZER;
1783 signop sgn = TYPE_SIGN (TREE_TYPE (step));
1784 wi::overflow_type overflow;
1786 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
1787 &overflow);
1788 /* If the multiplication overflowed we can't do a meaningful
1789 adjustment. Likewise if the result doesn't fit in the type
1790 of the induction variable. For a signed type we have to
1791 check whether the result has the expected signedness which
1792 is that of the step as number of iterations is unsigned. */
1793 if (!overflow
1794 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
1795 && (sgn == UNSIGNED
1796 || wi::gts_p (wtmp, 0) == wi::gts_p (wi::to_wide (step), 0)))
1798 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
1799 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
1800 TREE_TYPE (init), init, tem);
1801 /* Likewise if the addition did. */
1802 if (maxvr.type == VR_RANGE)
1804 value_range initvr = VR_INITIALIZER;
1806 if (TREE_CODE (init) == SSA_NAME)
1807 initvr = *(get_value_range (init));
1808 else if (is_gimple_min_invariant (init))
1809 set_value_range_to_value (&initvr, init, NULL);
1810 else
1811 return;
1813 /* Check if init + nit * step overflows. Though we checked
1814 scev {init, step}_loop doesn't wrap, it is not enough
1815 because the loop may exit immediately. Overflow could
1816 happen in the plus expression in this case. */
1817 if ((dir == EV_DIR_DECREASES
1818 && compare_values (maxvr.min, initvr.min) != -1)
1819 || (dir == EV_DIR_GROWS
1820 && compare_values (maxvr.max, initvr.max) != 1))
1821 return;
1823 tmin = maxvr.min;
1824 tmax = maxvr.max;
1830 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
1832 min = tmin;
1833 max = tmax;
1835 /* For VARYING or UNDEFINED ranges, just about anything we get
1836 from scalar evolutions should be better. */
1838 if (dir == EV_DIR_DECREASES)
1839 max = init;
1840 else
1841 min = init;
1843 else if (vr->type == VR_RANGE)
1845 min = vr->min;
1846 max = vr->max;
1848 if (dir == EV_DIR_DECREASES)
1850 /* INIT is the maximum value. If INIT is lower than VR->MAX
1851 but no smaller than VR->MIN, set VR->MAX to INIT. */
1852 if (compare_values (init, max) == -1)
1853 max = init;
1855 /* According to the loop information, the variable does not
1856 overflow. */
1857 if (compare_values (min, tmin) == -1)
1858 min = tmin;
1861 else
1863 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
1864 if (compare_values (init, min) == 1)
1865 min = init;
1867 if (compare_values (tmax, max) == -1)
1868 max = tmax;
1871 else
1872 return;
1874 /* If we just created an invalid range with the minimum
1875 greater than the maximum, we fail conservatively.
1876 This should happen only in unreachable
1877 parts of code, or for invalid programs. */
1878 if (compare_values (min, max) == 1)
1879 return;
1881 /* Even for valid range info, sometimes overflow flag will leak in.
1882 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1883 drop them. */
1884 if (TREE_OVERFLOW_P (min))
1885 min = drop_tree_overflow (min);
1886 if (TREE_OVERFLOW_P (max))
1887 max = drop_tree_overflow (max);
1889 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
1892 /* Dump value ranges of all SSA_NAMEs to FILE. */
1894 void
1895 vr_values::dump_all_value_ranges (FILE *file)
1897 size_t i;
1899 for (i = 0; i < num_vr_values; i++)
1901 if (vr_value[i])
1903 print_generic_expr (file, ssa_name (i));
1904 fprintf (file, ": ");
1905 dump_value_range (file, vr_value[i]);
1906 fprintf (file, "\n");
1910 fprintf (file, "\n");
1913 /* Initialize VRP lattice. */
1915 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1917 values_propagated = false;
1918 num_vr_values = num_ssa_names;
1919 vr_value = XCNEWVEC (value_range *, num_vr_values);
1920 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
1921 bitmap_obstack_initialize (&vrp_equiv_obstack);
1922 to_remove_edges = vNULL;
1923 to_update_switch_stmts = vNULL;
1926 /* Free VRP lattice. */
1928 vr_values::~vr_values ()
1930 /* Free allocated memory. */
1931 free (vr_value);
1932 free (vr_phi_edge_counts);
1933 bitmap_obstack_release (&vrp_equiv_obstack);
1934 vrp_value_range_pool.release ();
1936 /* So that we can distinguish between VRP data being available
1937 and not available. */
1938 vr_value = NULL;
1939 vr_phi_edge_counts = NULL;
1941 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1942 then an EVRP client did not clean up properly. Catch it now rather
1943 than seeing something more obscure later. */
1944 gcc_assert (to_remove_edges.is_empty ()
1945 && to_update_switch_stmts.is_empty ());
1949 /* A hack. */
1950 static class vr_values *x_vr_values;
1952 /* Return the singleton value-range for NAME or NAME. */
1954 static inline tree
1955 vrp_valueize (tree name)
1957 if (TREE_CODE (name) == SSA_NAME)
1959 value_range *vr = x_vr_values->get_value_range (name);
1960 if (vr->type == VR_RANGE
1961 && (TREE_CODE (vr->min) == SSA_NAME
1962 || is_gimple_min_invariant (vr->min))
1963 && vrp_operand_equal_p (vr->min, vr->max))
1964 return vr->min;
1966 return name;
1969 /* Return the singleton value-range for NAME if that is a constant
1970 but signal to not follow SSA edges. */
1972 static inline tree
1973 vrp_valueize_1 (tree name)
1975 if (TREE_CODE (name) == SSA_NAME)
1977 /* If the definition may be simulated again we cannot follow
1978 this SSA edge as the SSA propagator does not necessarily
1979 re-visit the use. */
1980 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1981 if (!gimple_nop_p (def_stmt)
1982 && prop_simulate_again_p (def_stmt))
1983 return NULL_TREE;
1984 value_range *vr = x_vr_values->get_value_range (name);
1985 if (range_int_cst_singleton_p (vr))
1986 return vr->min;
1988 return name;
1991 /* Given STMT, an assignment or call, return its LHS if the type
1992 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
1994 tree
1995 get_output_for_vrp (gimple *stmt)
1997 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
1998 return NULL_TREE;
2000 /* We only keep track of ranges in integral and pointer types. */
2001 tree lhs = gimple_get_lhs (stmt);
2002 if (TREE_CODE (lhs) == SSA_NAME
2003 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2004 /* It is valid to have NULL MIN/MAX values on a type. See
2005 build_range_type. */
2006 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
2007 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
2008 || POINTER_TYPE_P (TREE_TYPE (lhs))))
2009 return lhs;
2011 return NULL_TREE;
2014 /* Visit assignment STMT. If it produces an interesting range, record
2015 the range in VR and set LHS to OUTPUT_P. */
2017 void
2018 vr_values::vrp_visit_assignment_or_call (gimple *stmt, tree *output_p,
2019 value_range *vr)
2021 tree lhs = get_output_for_vrp (stmt);
2022 *output_p = lhs;
2024 /* We only keep track of ranges in integral and pointer types. */
2025 if (lhs)
2027 enum gimple_code code = gimple_code (stmt);
2029 /* Try folding the statement to a constant first. */
2030 x_vr_values = this;
2031 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
2032 vrp_valueize_1);
2033 x_vr_values = NULL;
2034 if (tem)
2036 if (TREE_CODE (tem) == SSA_NAME
2037 && (SSA_NAME_IS_DEFAULT_DEF (tem)
2038 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
2040 extract_range_from_ssa_name (vr, tem);
2041 return;
2043 else if (is_gimple_min_invariant (tem))
2045 set_value_range_to_value (vr, tem, NULL);
2046 return;
2049 /* Then dispatch to value-range extracting functions. */
2050 if (code == GIMPLE_CALL)
2051 extract_range_basic (vr, stmt);
2052 else
2053 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
2057 /* Helper that gets the value range of the SSA_NAME with version I
2058 or a symbolic range containing the SSA_NAME only if the value range
2059 is varying or undefined. */
2061 value_range
2062 vr_values::get_vr_for_comparison (int i)
2064 value_range vr = *get_value_range (ssa_name (i));
2066 /* If name N_i does not have a valid range, use N_i as its own
2067 range. This allows us to compare against names that may
2068 have N_i in their ranges. */
2069 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
2071 vr.type = VR_RANGE;
2072 vr.min = ssa_name (i);
2073 vr.max = ssa_name (i);
2076 return vr;
2079 /* Compare all the value ranges for names equivalent to VAR with VAL
2080 using comparison code COMP. Return the same value returned by
2081 compare_range_with_value, including the setting of
2082 *STRICT_OVERFLOW_P. */
2084 tree
2085 vr_values::compare_name_with_value (enum tree_code comp, tree var, tree val,
2086 bool *strict_overflow_p, bool use_equiv_p)
2088 bitmap_iterator bi;
2089 unsigned i;
2090 bitmap e;
2091 tree retval, t;
2092 int used_strict_overflow;
2093 bool sop;
2094 value_range equiv_vr;
2096 /* Get the set of equivalences for VAR. */
2097 e = get_value_range (var)->equiv;
2099 /* Start at -1. Set it to 0 if we do a comparison without relying
2100 on overflow, or 1 if all comparisons rely on overflow. */
2101 used_strict_overflow = -1;
2103 /* Compare vars' value range with val. */
2104 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
2105 sop = false;
2106 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
2107 if (retval)
2108 used_strict_overflow = sop ? 1 : 0;
2110 /* If the equiv set is empty we have done all work we need to do. */
2111 if (e == NULL)
2113 if (retval
2114 && used_strict_overflow > 0)
2115 *strict_overflow_p = true;
2116 return retval;
2119 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
2121 tree name = ssa_name (i);
2122 if (! name)
2123 continue;
2125 if (! use_equiv_p
2126 && ! SSA_NAME_IS_DEFAULT_DEF (name)
2127 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
2128 continue;
2130 equiv_vr = get_vr_for_comparison (i);
2131 sop = false;
2132 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
2133 if (t)
2135 /* If we get different answers from different members
2136 of the equivalence set this check must be in a dead
2137 code region. Folding it to a trap representation
2138 would be correct here. For now just return don't-know. */
2139 if (retval != NULL
2140 && t != retval)
2142 retval = NULL_TREE;
2143 break;
2145 retval = t;
2147 if (!sop)
2148 used_strict_overflow = 0;
2149 else if (used_strict_overflow < 0)
2150 used_strict_overflow = 1;
2154 if (retval
2155 && used_strict_overflow > 0)
2156 *strict_overflow_p = true;
2158 return retval;
2162 /* Given a comparison code COMP and names N1 and N2, compare all the
2163 ranges equivalent to N1 against all the ranges equivalent to N2
2164 to determine the value of N1 COMP N2. Return the same value
2165 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2166 whether we relied on undefined signed overflow in the comparison. */
2169 tree
2170 vr_values::compare_names (enum tree_code comp, tree n1, tree n2,
2171 bool *strict_overflow_p)
2173 tree t, retval;
2174 bitmap e1, e2;
2175 bitmap_iterator bi1, bi2;
2176 unsigned i1, i2;
2177 int used_strict_overflow;
2178 static bitmap_obstack *s_obstack = NULL;
2179 static bitmap s_e1 = NULL, s_e2 = NULL;
2181 /* Compare the ranges of every name equivalent to N1 against the
2182 ranges of every name equivalent to N2. */
2183 e1 = get_value_range (n1)->equiv;
2184 e2 = get_value_range (n2)->equiv;
2186 /* Use the fake bitmaps if e1 or e2 are not available. */
2187 if (s_obstack == NULL)
2189 s_obstack = XNEW (bitmap_obstack);
2190 bitmap_obstack_initialize (s_obstack);
2191 s_e1 = BITMAP_ALLOC (s_obstack);
2192 s_e2 = BITMAP_ALLOC (s_obstack);
2194 if (e1 == NULL)
2195 e1 = s_e1;
2196 if (e2 == NULL)
2197 e2 = s_e2;
2199 /* Add N1 and N2 to their own set of equivalences to avoid
2200 duplicating the body of the loop just to check N1 and N2
2201 ranges. */
2202 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
2203 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
2205 /* If the equivalence sets have a common intersection, then the two
2206 names can be compared without checking their ranges. */
2207 if (bitmap_intersect_p (e1, e2))
2209 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2210 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2212 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
2213 ? boolean_true_node
2214 : boolean_false_node;
2217 /* Start at -1. Set it to 0 if we do a comparison without relying
2218 on overflow, or 1 if all comparisons rely on overflow. */
2219 used_strict_overflow = -1;
2221 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2222 N2 to their own set of equivalences to avoid duplicating the body
2223 of the loop just to check N1 and N2 ranges. */
2224 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
2226 if (! ssa_name (i1))
2227 continue;
2229 value_range vr1 = get_vr_for_comparison (i1);
2231 t = retval = NULL_TREE;
2232 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
2234 if (! ssa_name (i2))
2235 continue;
2237 bool sop = false;
2239 value_range vr2 = get_vr_for_comparison (i2);
2241 t = compare_ranges (comp, &vr1, &vr2, &sop);
2242 if (t)
2244 /* If we get different answers from different members
2245 of the equivalence set this check must be in a dead
2246 code region. Folding it to a trap representation
2247 would be correct here. For now just return don't-know. */
2248 if (retval != NULL
2249 && t != retval)
2251 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2252 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2253 return NULL_TREE;
2255 retval = t;
2257 if (!sop)
2258 used_strict_overflow = 0;
2259 else if (used_strict_overflow < 0)
2260 used_strict_overflow = 1;
2264 if (retval)
2266 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2267 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2268 if (used_strict_overflow > 0)
2269 *strict_overflow_p = true;
2270 return retval;
2274 /* None of the equivalent ranges are useful in computing this
2275 comparison. */
2276 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2277 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2278 return NULL_TREE;
2281 /* Helper function for vrp_evaluate_conditional_warnv & other
2282 optimizers. */
2284 tree
2285 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2286 (enum tree_code code, tree op0, tree op1, bool * strict_overflow_p)
2288 value_range *vr0, *vr1;
2290 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
2291 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
2293 tree res = NULL_TREE;
2294 if (vr0 && vr1)
2295 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
2296 if (!res && vr0)
2297 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
2298 if (!res && vr1)
2299 res = (compare_range_with_value
2300 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
2301 return res;
2304 /* Helper function for vrp_evaluate_conditional_warnv. */
2306 tree
2307 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code,
2308 tree op0, tree op1,
2309 bool use_equiv_p,
2310 bool *strict_overflow_p,
2311 bool *only_ranges)
2313 tree ret;
2314 if (only_ranges)
2315 *only_ranges = true;
2317 /* We only deal with integral and pointer types. */
2318 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2319 && !POINTER_TYPE_P (TREE_TYPE (op0)))
2320 return NULL_TREE;
2322 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2323 as a simple equality test, then prefer that over its current form
2324 for evaluation.
2326 An overflow test which collapses to an equality test can always be
2327 expressed as a comparison of one argument against zero. Overflow
2328 occurs when the chosen argument is zero and does not occur if the
2329 chosen argument is not zero. */
2330 tree x;
2331 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
2333 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
2334 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2335 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2336 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2337 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2338 if (integer_zerop (x))
2340 op1 = x;
2341 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
2343 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2344 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2345 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2346 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2347 else if (wi::to_wide (x) == max - 1)
2349 op0 = op1;
2350 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
2351 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
2355 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
2356 (code, op0, op1, strict_overflow_p)))
2357 return ret;
2358 if (only_ranges)
2359 *only_ranges = false;
2360 /* Do not use compare_names during propagation, it's quadratic. */
2361 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
2362 && use_equiv_p)
2363 return compare_names (code, op0, op1, strict_overflow_p);
2364 else if (TREE_CODE (op0) == SSA_NAME)
2365 return compare_name_with_value (code, op0, op1,
2366 strict_overflow_p, use_equiv_p);
2367 else if (TREE_CODE (op1) == SSA_NAME)
2368 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
2369 strict_overflow_p, use_equiv_p);
2370 return NULL_TREE;
2373 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2374 information. Return NULL if the conditional can not be evaluated.
2375 The ranges of all the names equivalent with the operands in COND
2376 will be used when trying to compute the value. If the result is
2377 based on undefined signed overflow, issue a warning if
2378 appropriate. */
2380 tree
2381 vr_values::vrp_evaluate_conditional (tree_code code, tree op0,
2382 tree op1, gimple *stmt)
2384 bool sop;
2385 tree ret;
2386 bool only_ranges;
2388 /* Some passes and foldings leak constants with overflow flag set
2389 into the IL. Avoid doing wrong things with these and bail out. */
2390 if ((TREE_CODE (op0) == INTEGER_CST
2391 && TREE_OVERFLOW (op0))
2392 || (TREE_CODE (op1) == INTEGER_CST
2393 && TREE_OVERFLOW (op1)))
2394 return NULL_TREE;
2396 sop = false;
2397 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
2398 &only_ranges);
2400 if (ret && sop)
2402 enum warn_strict_overflow_code wc;
2403 const char* warnmsg;
2405 if (is_gimple_min_invariant (ret))
2407 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
2408 warnmsg = G_("assuming signed overflow does not occur when "
2409 "simplifying conditional to constant");
2411 else
2413 wc = WARN_STRICT_OVERFLOW_COMPARISON;
2414 warnmsg = G_("assuming signed overflow does not occur when "
2415 "simplifying conditional");
2418 if (issue_strict_overflow_warning (wc))
2420 location_t location;
2422 if (!gimple_has_location (stmt))
2423 location = input_location;
2424 else
2425 location = gimple_location (stmt);
2426 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
2430 if (warn_type_limits
2431 && ret && only_ranges
2432 && TREE_CODE_CLASS (code) == tcc_comparison
2433 && TREE_CODE (op0) == SSA_NAME)
2435 /* If the comparison is being folded and the operand on the LHS
2436 is being compared against a constant value that is outside of
2437 the natural range of OP0's type, then the predicate will
2438 always fold regardless of the value of OP0. If -Wtype-limits
2439 was specified, emit a warning. */
2440 tree type = TREE_TYPE (op0);
2441 value_range *vr0 = get_value_range (op0);
2443 if (vr0->type == VR_RANGE
2444 && INTEGRAL_TYPE_P (type)
2445 && vrp_val_is_min (vr0->min)
2446 && vrp_val_is_max (vr0->max)
2447 && is_gimple_min_invariant (op1))
2449 location_t location;
2451 if (!gimple_has_location (stmt))
2452 location = input_location;
2453 else
2454 location = gimple_location (stmt);
2456 warning_at (location, OPT_Wtype_limits,
2457 integer_zerop (ret)
2458 ? G_("comparison always false "
2459 "due to limited range of data type")
2460 : G_("comparison always true "
2461 "due to limited range of data type"));
2465 return ret;
2469 /* Visit conditional statement STMT. If we can determine which edge
2470 will be taken out of STMT's basic block, record it in
2471 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2473 void
2474 vr_values::vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
2476 tree val;
2478 *taken_edge_p = NULL;
2480 if (dump_file && (dump_flags & TDF_DETAILS))
2482 tree use;
2483 ssa_op_iter i;
2485 fprintf (dump_file, "\nVisiting conditional with predicate: ");
2486 print_gimple_stmt (dump_file, stmt, 0);
2487 fprintf (dump_file, "\nWith known ranges\n");
2489 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
2491 fprintf (dump_file, "\t");
2492 print_generic_expr (dump_file, use);
2493 fprintf (dump_file, ": ");
2494 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
2497 fprintf (dump_file, "\n");
2500 /* Compute the value of the predicate COND by checking the known
2501 ranges of each of its operands.
2503 Note that we cannot evaluate all the equivalent ranges here
2504 because those ranges may not yet be final and with the current
2505 propagation strategy, we cannot determine when the value ranges
2506 of the names in the equivalence set have changed.
2508 For instance, given the following code fragment
2510 i_5 = PHI <8, i_13>
2512 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2513 if (i_14 == 1)
2516 Assume that on the first visit to i_14, i_5 has the temporary
2517 range [8, 8] because the second argument to the PHI function is
2518 not yet executable. We derive the range ~[0, 0] for i_14 and the
2519 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2520 the first time, since i_14 is equivalent to the range [8, 8], we
2521 determine that the predicate is always false.
2523 On the next round of propagation, i_13 is determined to be
2524 VARYING, which causes i_5 to drop down to VARYING. So, another
2525 visit to i_14 is scheduled. In this second visit, we compute the
2526 exact same range and equivalence set for i_14, namely ~[0, 0] and
2527 { i_5 }. But we did not have the previous range for i_5
2528 registered, so vrp_visit_assignment thinks that the range for
2529 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2530 is not visited again, which stops propagation from visiting
2531 statements in the THEN clause of that if().
2533 To properly fix this we would need to keep the previous range
2534 value for the names in the equivalence set. This way we would've
2535 discovered that from one visit to the other i_5 changed from
2536 range [8, 8] to VR_VARYING.
2538 However, fixing this apparent limitation may not be worth the
2539 additional checking. Testing on several code bases (GCC, DLV,
2540 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2541 4 more predicates folded in SPEC. */
2543 bool sop;
2544 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
2545 gimple_cond_lhs (stmt),
2546 gimple_cond_rhs (stmt),
2547 false, &sop, NULL);
2548 if (val)
2549 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
2551 if (dump_file && (dump_flags & TDF_DETAILS))
2553 fprintf (dump_file, "\nPredicate evaluates to: ");
2554 if (val == NULL_TREE)
2555 fprintf (dump_file, "DON'T KNOW\n");
2556 else
2557 print_generic_stmt (dump_file, val);
2561 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2562 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2563 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2564 Returns true if the default label is not needed. */
2566 static bool
2567 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
2568 size_t *max_idx1, size_t *min_idx2,
2569 size_t *max_idx2)
2571 size_t i, j, k, l;
2572 unsigned int n = gimple_switch_num_labels (stmt);
2573 bool take_default;
2574 tree case_low, case_high;
2575 tree min = vr->min, max = vr->max;
2577 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
2579 take_default = !find_case_label_range (stmt, min, max, &i, &j);
2581 /* Set second range to emtpy. */
2582 *min_idx2 = 1;
2583 *max_idx2 = 0;
2585 if (vr->type == VR_RANGE)
2587 *min_idx1 = i;
2588 *max_idx1 = j;
2589 return !take_default;
2592 /* Set first range to all case labels. */
2593 *min_idx1 = 1;
2594 *max_idx1 = n - 1;
2596 if (i > j)
2597 return false;
2599 /* Make sure all the values of case labels [i , j] are contained in
2600 range [MIN, MAX]. */
2601 case_low = CASE_LOW (gimple_switch_label (stmt, i));
2602 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
2603 if (tree_int_cst_compare (case_low, min) < 0)
2604 i += 1;
2605 if (case_high != NULL_TREE
2606 && tree_int_cst_compare (max, case_high) < 0)
2607 j -= 1;
2609 if (i > j)
2610 return false;
2612 /* If the range spans case labels [i, j], the corresponding anti-range spans
2613 the labels [1, i - 1] and [j + 1, n - 1]. */
2614 k = j + 1;
2615 l = n - 1;
2616 if (k > l)
2618 k = 1;
2619 l = 0;
2622 j = i - 1;
2623 i = 1;
2624 if (i > j)
2626 i = k;
2627 j = l;
2628 k = 1;
2629 l = 0;
2632 *min_idx1 = i;
2633 *max_idx1 = j;
2634 *min_idx2 = k;
2635 *max_idx2 = l;
2636 return false;
2639 /* Visit switch statement STMT. If we can determine which edge
2640 will be taken out of STMT's basic block, record it in
2641 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2643 void
2644 vr_values::vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
2646 tree op, val;
2647 value_range *vr;
2648 size_t i = 0, j = 0, k, l;
2649 bool take_default;
2651 *taken_edge_p = NULL;
2652 op = gimple_switch_index (stmt);
2653 if (TREE_CODE (op) != SSA_NAME)
2654 return;
2656 vr = get_value_range (op);
2657 if (dump_file && (dump_flags & TDF_DETAILS))
2659 fprintf (dump_file, "\nVisiting switch expression with operand ");
2660 print_generic_expr (dump_file, op);
2661 fprintf (dump_file, " with known range ");
2662 dump_value_range (dump_file, vr);
2663 fprintf (dump_file, "\n");
2666 if ((vr->type != VR_RANGE
2667 && vr->type != VR_ANTI_RANGE)
2668 || symbolic_range_p (vr))
2669 return;
2671 /* Find the single edge that is taken from the switch expression. */
2672 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
2674 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2675 label */
2676 if (j < i)
2678 gcc_assert (take_default);
2679 val = gimple_switch_default_label (stmt);
2681 else
2683 /* Check if labels with index i to j and maybe the default label
2684 are all reaching the same label. */
2686 val = gimple_switch_label (stmt, i);
2687 if (take_default
2688 && CASE_LABEL (gimple_switch_default_label (stmt))
2689 != 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;
2696 for (++i; i <= j; ++i)
2698 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
2700 if (dump_file && (dump_flags & TDF_DETAILS))
2701 fprintf (dump_file, " not a single destination for this "
2702 "range\n");
2703 return;
2706 for (; k <= l; ++k)
2708 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
2710 if (dump_file && (dump_flags & TDF_DETAILS))
2711 fprintf (dump_file, " not a single destination for this "
2712 "range\n");
2713 return;
2718 *taken_edge_p = find_edge (gimple_bb (stmt),
2719 label_to_block (cfun, CASE_LABEL (val)));
2721 if (dump_file && (dump_flags & TDF_DETAILS))
2723 fprintf (dump_file, " will take edge to ");
2724 print_generic_stmt (dump_file, CASE_LABEL (val));
2729 /* Evaluate statement STMT. If the statement produces a useful range,
2730 set VR and corepsponding OUTPUT_P.
2732 If STMT is a conditional branch and we can determine its truth
2733 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2735 void
2736 vr_values::extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
2737 tree *output_p, value_range *vr)
2740 if (dump_file && (dump_flags & TDF_DETAILS))
2742 fprintf (dump_file, "\nVisiting statement:\n");
2743 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2746 if (!stmt_interesting_for_vrp (stmt))
2747 gcc_assert (stmt_ends_bb_p (stmt));
2748 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
2749 vrp_visit_assignment_or_call (stmt, output_p, vr);
2750 else if (gimple_code (stmt) == GIMPLE_COND)
2751 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
2752 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2753 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
2756 /* Visit all arguments for PHI node PHI that flow through executable
2757 edges. If a valid value range can be derived from all the incoming
2758 value ranges, set a new range in VR_RESULT. */
2760 void
2761 vr_values::extract_range_from_phi_node (gphi *phi, value_range *vr_result)
2763 size_t i;
2764 tree lhs = PHI_RESULT (phi);
2765 value_range *lhs_vr = get_value_range (lhs);
2766 bool first = true;
2767 int edges, old_edges;
2768 struct loop *l;
2770 if (dump_file && (dump_flags & TDF_DETAILS))
2772 fprintf (dump_file, "\nVisiting PHI node: ");
2773 print_gimple_stmt (dump_file, phi, 0, dump_flags);
2776 bool may_simulate_backedge_again = false;
2777 edges = 0;
2778 for (i = 0; i < gimple_phi_num_args (phi); i++)
2780 edge e = gimple_phi_arg_edge (phi, i);
2782 if (dump_file && (dump_flags & TDF_DETAILS))
2784 fprintf (dump_file,
2785 " Argument #%d (%d -> %d %sexecutable)\n",
2786 (int) i, e->src->index, e->dest->index,
2787 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
2790 if (e->flags & EDGE_EXECUTABLE)
2792 tree arg = PHI_ARG_DEF (phi, i);
2793 value_range vr_arg;
2795 ++edges;
2797 if (TREE_CODE (arg) == SSA_NAME)
2799 /* See if we are eventually going to change one of the args. */
2800 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
2801 if (! gimple_nop_p (def_stmt)
2802 && prop_simulate_again_p (def_stmt)
2803 && e->flags & EDGE_DFS_BACK)
2804 may_simulate_backedge_again = true;
2806 vr_arg = *(get_value_range (arg));
2807 /* Do not allow equivalences or symbolic ranges to leak in from
2808 backedges. That creates invalid equivalencies.
2809 See PR53465 and PR54767. */
2810 if (e->flags & EDGE_DFS_BACK)
2812 if (vr_arg.type == VR_RANGE
2813 || vr_arg.type == VR_ANTI_RANGE)
2815 vr_arg.equiv = NULL;
2816 if (symbolic_range_p (&vr_arg))
2818 vr_arg.type = VR_VARYING;
2819 vr_arg.min = NULL_TREE;
2820 vr_arg.max = NULL_TREE;
2824 else
2826 /* If the non-backedge arguments range is VR_VARYING then
2827 we can still try recording a simple equivalence. */
2828 if (vr_arg.type == VR_VARYING)
2830 vr_arg.type = VR_RANGE;
2831 vr_arg.min = arg;
2832 vr_arg.max = arg;
2833 vr_arg.equiv = NULL;
2837 else
2839 if (TREE_OVERFLOW_P (arg))
2840 arg = drop_tree_overflow (arg);
2842 vr_arg.type = VR_RANGE;
2843 vr_arg.min = arg;
2844 vr_arg.max = arg;
2845 vr_arg.equiv = NULL;
2848 if (dump_file && (dump_flags & TDF_DETAILS))
2850 fprintf (dump_file, "\t");
2851 print_generic_expr (dump_file, arg, dump_flags);
2852 fprintf (dump_file, ": ");
2853 dump_value_range (dump_file, &vr_arg);
2854 fprintf (dump_file, "\n");
2857 if (first)
2858 copy_value_range (vr_result, &vr_arg);
2859 else
2860 vrp_meet (vr_result, &vr_arg);
2861 first = false;
2863 if (vr_result->type == VR_VARYING)
2864 break;
2868 if (vr_result->type == VR_VARYING)
2869 goto varying;
2870 else if (vr_result->type == VR_UNDEFINED)
2871 goto update_range;
2873 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
2874 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
2876 /* To prevent infinite iterations in the algorithm, derive ranges
2877 when the new value is slightly bigger or smaller than the
2878 previous one. We don't do this if we have seen a new executable
2879 edge; this helps us avoid an infinity for conditionals
2880 which are not in a loop. If the old value-range was VR_UNDEFINED
2881 use the updated range and iterate one more time. If we will not
2882 simulate this PHI again via the backedge allow us to iterate. */
2883 if (edges > 0
2884 && gimple_phi_num_args (phi) > 1
2885 && edges == old_edges
2886 && lhs_vr->type != VR_UNDEFINED
2887 && may_simulate_backedge_again)
2889 /* Compare old and new ranges, fall back to varying if the
2890 values are not comparable. */
2891 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
2892 if (cmp_min == -2)
2893 goto varying;
2894 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
2895 if (cmp_max == -2)
2896 goto varying;
2898 /* For non VR_RANGE or for pointers fall back to varying if
2899 the range changed. */
2900 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
2901 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2902 && (cmp_min != 0 || cmp_max != 0))
2903 goto varying;
2905 /* If the new minimum is larger than the previous one
2906 retain the old value. If the new minimum value is smaller
2907 than the previous one and not -INF go all the way to -INF + 1.
2908 In the first case, to avoid infinite bouncing between different
2909 minimums, and in the other case to avoid iterating millions of
2910 times to reach -INF. Going to -INF + 1 also lets the following
2911 iteration compute whether there will be any overflow, at the
2912 expense of one additional iteration. */
2913 if (cmp_min < 0)
2914 vr_result->min = lhs_vr->min;
2915 else if (cmp_min > 0
2916 && !vrp_val_is_min (vr_result->min))
2917 vr_result->min
2918 = int_const_binop (PLUS_EXPR,
2919 vrp_val_min (TREE_TYPE (vr_result->min)),
2920 build_int_cst (TREE_TYPE (vr_result->min), 1));
2922 /* Similarly for the maximum value. */
2923 if (cmp_max > 0)
2924 vr_result->max = lhs_vr->max;
2925 else if (cmp_max < 0
2926 && !vrp_val_is_max (vr_result->max))
2927 vr_result->max
2928 = int_const_binop (MINUS_EXPR,
2929 vrp_val_max (TREE_TYPE (vr_result->min)),
2930 build_int_cst (TREE_TYPE (vr_result->min), 1));
2932 /* If we dropped either bound to +-INF then if this is a loop
2933 PHI node SCEV may known more about its value-range. */
2934 if (cmp_min > 0 || cmp_min < 0
2935 || cmp_max < 0 || cmp_max > 0)
2936 goto scev_check;
2938 goto infinite_check;
2941 goto update_range;
2943 varying:
2944 set_value_range_to_varying (vr_result);
2946 scev_check:
2947 /* If this is a loop PHI node SCEV may known more about its value-range.
2948 scev_check can be reached from two paths, one is a fall through from above
2949 "varying" label, the other is direct goto from code block which tries to
2950 avoid infinite simulation. */
2951 if (scev_initialized_p ()
2952 && (l = loop_containing_stmt (phi))
2953 && l->header == gimple_bb (phi))
2954 adjust_range_with_scev (vr_result, l, phi, lhs);
2956 infinite_check:
2957 /* If we will end up with a (-INF, +INF) range, set it to
2958 VARYING. Same if the previous max value was invalid for
2959 the type and we end up with vr_result.min > vr_result.max. */
2960 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
2961 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
2962 || compare_values (vr_result->min, vr_result->max) > 0))
2964 else
2965 set_value_range_to_varying (vr_result);
2967 /* If the new range is different than the previous value, keep
2968 iterating. */
2969 update_range:
2970 return;
2973 /* Simplify boolean operations if the source is known
2974 to be already a boolean. */
2975 bool
2976 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi,
2977 gimple *stmt)
2979 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
2980 tree lhs, op0, op1;
2981 bool need_conversion;
2983 /* We handle only !=/== case here. */
2984 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
2986 op0 = gimple_assign_rhs1 (stmt);
2987 if (!op_with_boolean_value_range_p (op0))
2988 return false;
2990 op1 = gimple_assign_rhs2 (stmt);
2991 if (!op_with_boolean_value_range_p (op1))
2992 return false;
2994 /* Reduce number of cases to handle to NE_EXPR. As there is no
2995 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
2996 if (rhs_code == EQ_EXPR)
2998 if (TREE_CODE (op1) == INTEGER_CST)
2999 op1 = int_const_binop (BIT_XOR_EXPR, op1,
3000 build_int_cst (TREE_TYPE (op1), 1));
3001 else
3002 return false;
3005 lhs = gimple_assign_lhs (stmt);
3006 need_conversion
3007 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
3009 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
3010 if (need_conversion
3011 && !TYPE_UNSIGNED (TREE_TYPE (op0))
3012 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
3013 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
3014 return false;
3016 /* For A != 0 we can substitute A itself. */
3017 if (integer_zerop (op1))
3018 gimple_assign_set_rhs_with_ops (gsi,
3019 need_conversion
3020 ? NOP_EXPR : TREE_CODE (op0), op0);
3021 /* For A != B we substitute A ^ B. Either with conversion. */
3022 else if (need_conversion)
3024 tree tem = make_ssa_name (TREE_TYPE (op0));
3025 gassign *newop
3026 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
3027 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
3028 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
3029 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
3030 set_range_info (tem, VR_RANGE,
3031 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
3032 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
3033 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
3035 /* Or without. */
3036 else
3037 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
3038 update_stmt (gsi_stmt (*gsi));
3039 fold_stmt (gsi, follow_single_use_edges);
3041 return true;
3044 /* Simplify a division or modulo operator to a right shift or bitwise and
3045 if the first operand is unsigned or is greater than zero and the second
3046 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3047 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3048 optimize it into just op0 if op0's range is known to be a subset of
3049 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3050 modulo. */
3052 bool
3053 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi,
3054 gimple *stmt)
3056 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3057 tree val = NULL;
3058 tree op0 = gimple_assign_rhs1 (stmt);
3059 tree op1 = gimple_assign_rhs2 (stmt);
3060 tree op0min = NULL_TREE, op0max = NULL_TREE;
3061 tree op1min = op1;
3062 value_range *vr = NULL;
3064 if (TREE_CODE (op0) == INTEGER_CST)
3066 op0min = op0;
3067 op0max = op0;
3069 else
3071 vr = get_value_range (op0);
3072 if (range_int_cst_p (vr))
3074 op0min = vr->min;
3075 op0max = vr->max;
3079 if (rhs_code == TRUNC_MOD_EXPR
3080 && TREE_CODE (op1) == SSA_NAME)
3082 value_range *vr1 = get_value_range (op1);
3083 if (range_int_cst_p (vr1))
3084 op1min = vr1->min;
3086 if (rhs_code == TRUNC_MOD_EXPR
3087 && TREE_CODE (op1min) == INTEGER_CST
3088 && tree_int_cst_sgn (op1min) == 1
3089 && op0max
3090 && tree_int_cst_lt (op0max, op1min))
3092 if (TYPE_UNSIGNED (TREE_TYPE (op0))
3093 || tree_int_cst_sgn (op0min) >= 0
3094 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
3095 op0min))
3097 /* If op0 already has the range op0 % op1 has,
3098 then TRUNC_MOD_EXPR won't change anything. */
3099 gimple_assign_set_rhs_from_tree (gsi, op0);
3100 return true;
3104 if (TREE_CODE (op0) != SSA_NAME)
3105 return false;
3107 if (!integer_pow2p (op1))
3109 /* X % -Y can be only optimized into X % Y either if
3110 X is not INT_MIN, or Y is not -1. Fold it now, as after
3111 remove_range_assertions the range info might be not available
3112 anymore. */
3113 if (rhs_code == TRUNC_MOD_EXPR
3114 && fold_stmt (gsi, follow_single_use_edges))
3115 return true;
3116 return false;
3119 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
3120 val = integer_one_node;
3121 else
3123 bool sop = false;
3125 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
3127 if (val
3128 && sop
3129 && integer_onep (val)
3130 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3132 location_t location;
3134 if (!gimple_has_location (stmt))
3135 location = input_location;
3136 else
3137 location = gimple_location (stmt);
3138 warning_at (location, OPT_Wstrict_overflow,
3139 "assuming signed overflow does not occur when "
3140 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3144 if (val && integer_onep (val))
3146 tree t;
3148 if (rhs_code == TRUNC_DIV_EXPR)
3150 t = build_int_cst (integer_type_node, tree_log2 (op1));
3151 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
3152 gimple_assign_set_rhs1 (stmt, op0);
3153 gimple_assign_set_rhs2 (stmt, t);
3155 else
3157 t = build_int_cst (TREE_TYPE (op1), 1);
3158 t = int_const_binop (MINUS_EXPR, op1, t);
3159 t = fold_convert (TREE_TYPE (op0), t);
3161 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
3162 gimple_assign_set_rhs1 (stmt, op0);
3163 gimple_assign_set_rhs2 (stmt, t);
3166 update_stmt (stmt);
3167 fold_stmt (gsi, follow_single_use_edges);
3168 return true;
3171 return false;
3174 /* Simplify a min or max if the ranges of the two operands are
3175 disjoint. Return true if we do simplify. */
3177 bool
3178 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi,
3179 gimple *stmt)
3181 tree op0 = gimple_assign_rhs1 (stmt);
3182 tree op1 = gimple_assign_rhs2 (stmt);
3183 bool sop = false;
3184 tree val;
3186 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3187 (LE_EXPR, op0, op1, &sop));
3188 if (!val)
3190 sop = false;
3191 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3192 (LT_EXPR, op0, op1, &sop));
3195 if (val)
3197 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3199 location_t location;
3201 if (!gimple_has_location (stmt))
3202 location = input_location;
3203 else
3204 location = gimple_location (stmt);
3205 warning_at (location, OPT_Wstrict_overflow,
3206 "assuming signed overflow does not occur when "
3207 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3210 /* VAL == TRUE -> OP0 < or <= op1
3211 VAL == FALSE -> OP0 > or >= op1. */
3212 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
3213 == integer_zerop (val)) ? op0 : op1;
3214 gimple_assign_set_rhs_from_tree (gsi, res);
3215 return true;
3218 return false;
3221 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3222 ABS_EXPR. If the operand is <= 0, then simplify the
3223 ABS_EXPR into a NEGATE_EXPR. */
3225 bool
3226 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3228 tree op = gimple_assign_rhs1 (stmt);
3229 value_range *vr = get_value_range (op);
3231 if (vr)
3233 tree val = NULL;
3234 bool sop = false;
3236 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
3237 if (!val)
3239 /* The range is neither <= 0 nor > 0. Now see if it is
3240 either < 0 or >= 0. */
3241 sop = false;
3242 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
3243 &sop);
3246 if (val)
3248 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3250 location_t location;
3252 if (!gimple_has_location (stmt))
3253 location = input_location;
3254 else
3255 location = gimple_location (stmt);
3256 warning_at (location, OPT_Wstrict_overflow,
3257 "assuming signed overflow does not occur when "
3258 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3261 gimple_assign_set_rhs1 (stmt, op);
3262 if (integer_zerop (val))
3263 gimple_assign_set_rhs_code (stmt, SSA_NAME);
3264 else
3265 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
3266 update_stmt (stmt);
3267 fold_stmt (gsi, follow_single_use_edges);
3268 return true;
3272 return false;
3275 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3276 If all the bits that are being cleared by & are already
3277 known to be zero from VR, or all the bits that are being
3278 set by | are already known to be one from VR, the bit
3279 operation is redundant. */
3281 bool
3282 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi,
3283 gimple *stmt)
3285 tree op0 = gimple_assign_rhs1 (stmt);
3286 tree op1 = gimple_assign_rhs2 (stmt);
3287 tree op = NULL_TREE;
3288 value_range vr0 = VR_INITIALIZER;
3289 value_range vr1 = VR_INITIALIZER;
3290 wide_int may_be_nonzero0, may_be_nonzero1;
3291 wide_int must_be_nonzero0, must_be_nonzero1;
3292 wide_int mask;
3294 if (TREE_CODE (op0) == SSA_NAME)
3295 vr0 = *(get_value_range (op0));
3296 else if (is_gimple_min_invariant (op0))
3297 set_value_range_to_value (&vr0, op0, NULL);
3298 else
3299 return false;
3301 if (TREE_CODE (op1) == SSA_NAME)
3302 vr1 = *(get_value_range (op1));
3303 else if (is_gimple_min_invariant (op1))
3304 set_value_range_to_value (&vr1, op1, NULL);
3305 else
3306 return false;
3308 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
3309 &must_be_nonzero0))
3310 return false;
3311 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
3312 &must_be_nonzero1))
3313 return false;
3315 switch (gimple_assign_rhs_code (stmt))
3317 case BIT_AND_EXPR:
3318 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3319 if (mask == 0)
3321 op = op0;
3322 break;
3324 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3325 if (mask == 0)
3327 op = op1;
3328 break;
3330 break;
3331 case BIT_IOR_EXPR:
3332 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3333 if (mask == 0)
3335 op = op1;
3336 break;
3338 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3339 if (mask == 0)
3341 op = op0;
3342 break;
3344 break;
3345 default:
3346 gcc_unreachable ();
3349 if (op == NULL_TREE)
3350 return false;
3352 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
3353 update_stmt (gsi_stmt (*gsi));
3354 return true;
3357 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3358 a known value range VR.
3360 If there is one and only one value which will satisfy the
3361 conditional, then return that value. Else return NULL.
3363 If signed overflow must be undefined for the value to satisfy
3364 the conditional, then set *STRICT_OVERFLOW_P to true. */
3366 static tree
3367 test_for_singularity (enum tree_code cond_code, tree op0,
3368 tree op1, value_range *vr)
3370 tree min = NULL;
3371 tree max = NULL;
3373 /* Extract minimum/maximum values which satisfy the conditional as it was
3374 written. */
3375 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
3377 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
3379 max = op1;
3380 if (cond_code == LT_EXPR)
3382 tree one = build_int_cst (TREE_TYPE (op0), 1);
3383 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
3384 /* Signal to compare_values_warnv this expr doesn't overflow. */
3385 if (EXPR_P (max))
3386 TREE_NO_WARNING (max) = 1;
3389 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
3391 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
3393 min = op1;
3394 if (cond_code == GT_EXPR)
3396 tree one = build_int_cst (TREE_TYPE (op0), 1);
3397 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
3398 /* Signal to compare_values_warnv this expr doesn't overflow. */
3399 if (EXPR_P (min))
3400 TREE_NO_WARNING (min) = 1;
3404 /* Now refine the minimum and maximum values using any
3405 value range information we have for op0. */
3406 if (min && max)
3408 if (compare_values (vr->min, min) == 1)
3409 min = vr->min;
3410 if (compare_values (vr->max, max) == -1)
3411 max = vr->max;
3413 /* If the new min/max values have converged to a single value,
3414 then there is only one value which can satisfy the condition,
3415 return that value. */
3416 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
3417 return min;
3419 return NULL;
3422 /* Return whether the value range *VR fits in an integer type specified
3423 by PRECISION and UNSIGNED_P. */
3425 static bool
3426 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
3428 tree src_type;
3429 unsigned src_precision;
3430 widest_int tem;
3431 signop src_sgn;
3433 /* We can only handle integral and pointer types. */
3434 src_type = TREE_TYPE (vr->min);
3435 if (!INTEGRAL_TYPE_P (src_type)
3436 && !POINTER_TYPE_P (src_type))
3437 return false;
3439 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3440 and so is an identity transform. */
3441 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
3442 src_sgn = TYPE_SIGN (src_type);
3443 if ((src_precision < dest_precision
3444 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
3445 || (src_precision == dest_precision && src_sgn == dest_sgn))
3446 return true;
3448 /* Now we can only handle ranges with constant bounds. */
3449 if (vr->type != VR_RANGE
3450 || TREE_CODE (vr->min) != INTEGER_CST
3451 || TREE_CODE (vr->max) != INTEGER_CST)
3452 return false;
3454 /* For sign changes, the MSB of the wide_int has to be clear.
3455 An unsigned value with its MSB set cannot be represented by
3456 a signed wide_int, while a negative value cannot be represented
3457 by an unsigned wide_int. */
3458 if (src_sgn != dest_sgn
3459 && (wi::lts_p (wi::to_wide (vr->min), 0)
3460 || wi::lts_p (wi::to_wide (vr->max), 0)))
3461 return false;
3463 /* Then we can perform the conversion on both ends and compare
3464 the result for equality. */
3465 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
3466 if (tem != wi::to_widest (vr->min))
3467 return false;
3468 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
3469 if (tem != wi::to_widest (vr->max))
3470 return false;
3472 return true;
3475 /* Simplify a conditional using a relational operator to an equality
3476 test if the range information indicates only one value can satisfy
3477 the original conditional. */
3479 bool
3480 vr_values::simplify_cond_using_ranges_1 (gcond *stmt)
3482 tree op0 = gimple_cond_lhs (stmt);
3483 tree op1 = gimple_cond_rhs (stmt);
3484 enum tree_code cond_code = gimple_cond_code (stmt);
3486 if (cond_code != NE_EXPR
3487 && cond_code != EQ_EXPR
3488 && TREE_CODE (op0) == SSA_NAME
3489 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3490 && is_gimple_min_invariant (op1))
3492 value_range *vr = get_value_range (op0);
3494 /* If we have range information for OP0, then we might be
3495 able to simplify this conditional. */
3496 if (vr->type == VR_RANGE)
3498 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
3499 if (new_tree)
3501 if (dump_file)
3503 fprintf (dump_file, "Simplified relational ");
3504 print_gimple_stmt (dump_file, stmt, 0);
3505 fprintf (dump_file, " into ");
3508 gimple_cond_set_code (stmt, EQ_EXPR);
3509 gimple_cond_set_lhs (stmt, op0);
3510 gimple_cond_set_rhs (stmt, new_tree);
3512 update_stmt (stmt);
3514 if (dump_file)
3516 print_gimple_stmt (dump_file, stmt, 0);
3517 fprintf (dump_file, "\n");
3520 return true;
3523 /* Try again after inverting the condition. We only deal
3524 with integral types here, so no need to worry about
3525 issues with inverting FP comparisons. */
3526 new_tree = test_for_singularity
3527 (invert_tree_comparison (cond_code, false),
3528 op0, op1, vr);
3529 if (new_tree)
3531 if (dump_file)
3533 fprintf (dump_file, "Simplified relational ");
3534 print_gimple_stmt (dump_file, stmt, 0);
3535 fprintf (dump_file, " into ");
3538 gimple_cond_set_code (stmt, NE_EXPR);
3539 gimple_cond_set_lhs (stmt, op0);
3540 gimple_cond_set_rhs (stmt, new_tree);
3542 update_stmt (stmt);
3544 if (dump_file)
3546 print_gimple_stmt (dump_file, stmt, 0);
3547 fprintf (dump_file, "\n");
3550 return true;
3554 return false;
3557 /* STMT is a conditional at the end of a basic block.
3559 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3560 was set via a type conversion, try to replace the SSA_NAME with the RHS
3561 of the type conversion. Doing so makes the conversion dead which helps
3562 subsequent passes. */
3564 void
3565 vr_values::simplify_cond_using_ranges_2 (gcond *stmt)
3567 tree op0 = gimple_cond_lhs (stmt);
3568 tree op1 = gimple_cond_rhs (stmt);
3570 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3571 see if OP0 was set by a type conversion where the source of
3572 the conversion is another SSA_NAME with a range that fits
3573 into the range of OP0's type.
3575 If so, the conversion is redundant as the earlier SSA_NAME can be
3576 used for the comparison directly if we just massage the constant in the
3577 comparison. */
3578 if (TREE_CODE (op0) == SSA_NAME
3579 && TREE_CODE (op1) == INTEGER_CST)
3581 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
3582 tree innerop;
3584 if (!is_gimple_assign (def_stmt)
3585 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3586 return;
3588 innerop = gimple_assign_rhs1 (def_stmt);
3590 if (TREE_CODE (innerop) == SSA_NAME
3591 && !POINTER_TYPE_P (TREE_TYPE (innerop))
3592 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
3593 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
3595 value_range *vr = get_value_range (innerop);
3597 if (range_int_cst_p (vr)
3598 && range_fits_type_p (vr,
3599 TYPE_PRECISION (TREE_TYPE (op0)),
3600 TYPE_SIGN (TREE_TYPE (op0)))
3601 && int_fits_type_p (op1, TREE_TYPE (innerop)))
3603 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
3604 gimple_cond_set_lhs (stmt, innerop);
3605 gimple_cond_set_rhs (stmt, newconst);
3606 update_stmt (stmt);
3607 if (dump_file && (dump_flags & TDF_DETAILS))
3609 fprintf (dump_file, "Folded into: ");
3610 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3611 fprintf (dump_file, "\n");
3618 /* Simplify a switch statement using the value range of the switch
3619 argument. */
3621 bool
3622 vr_values::simplify_switch_using_ranges (gswitch *stmt)
3624 tree op = gimple_switch_index (stmt);
3625 value_range *vr = NULL;
3626 bool take_default;
3627 edge e;
3628 edge_iterator ei;
3629 size_t i = 0, j = 0, n, n2;
3630 tree vec2;
3631 switch_update su;
3632 size_t k = 1, l = 0;
3634 if (TREE_CODE (op) == SSA_NAME)
3636 vr = get_value_range (op);
3638 /* We can only handle integer ranges. */
3639 if ((vr->type != VR_RANGE
3640 && vr->type != VR_ANTI_RANGE)
3641 || symbolic_range_p (vr))
3642 return false;
3644 /* Find case label for min/max of the value range. */
3645 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
3647 else if (TREE_CODE (op) == INTEGER_CST)
3649 take_default = !find_case_label_index (stmt, 1, op, &i);
3650 if (take_default)
3652 i = 1;
3653 j = 0;
3655 else
3657 j = i;
3660 else
3661 return false;
3663 n = gimple_switch_num_labels (stmt);
3665 /* We can truncate the case label ranges that partially overlap with OP's
3666 value range. */
3667 size_t min_idx = 1, max_idx = 0;
3668 if (vr != NULL)
3669 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
3670 if (min_idx <= max_idx)
3672 tree min_label = gimple_switch_label (stmt, min_idx);
3673 tree max_label = gimple_switch_label (stmt, max_idx);
3675 /* Avoid changing the type of the case labels when truncating. */
3676 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
3677 tree vr_min = fold_convert (case_label_type, vr->min);
3678 tree vr_max = fold_convert (case_label_type, vr->max);
3680 if (vr->type == VR_RANGE)
3682 /* If OP's value range is [2,8] and the low label range is
3683 0 ... 3, truncate the label's range to 2 .. 3. */
3684 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3685 && CASE_HIGH (min_label) != NULL_TREE
3686 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3687 CASE_LOW (min_label) = vr_min;
3689 /* If OP's value range is [2,8] and the high label range is
3690 7 ... 10, truncate the label's range to 7 .. 8. */
3691 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3692 && CASE_HIGH (max_label) != NULL_TREE
3693 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3694 CASE_HIGH (max_label) = vr_max;
3696 else if (vr->type == VR_ANTI_RANGE)
3698 tree one_cst = build_one_cst (case_label_type);
3700 if (min_label == max_label)
3702 /* If OP's value range is ~[7,8] and the label's range is
3703 7 ... 10, truncate the label's range to 9 ... 10. */
3704 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
3705 && CASE_HIGH (min_label) != NULL_TREE
3706 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
3707 CASE_LOW (min_label)
3708 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3710 /* If OP's value range is ~[7,8] and the label's range is
3711 5 ... 8, truncate the label's range to 5 ... 6. */
3712 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3713 && CASE_HIGH (min_label) != NULL_TREE
3714 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
3715 CASE_HIGH (min_label)
3716 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3718 else
3720 /* If OP's value range is ~[2,8] and the low label range is
3721 0 ... 3, truncate the label's range to 0 ... 1. */
3722 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3723 && CASE_HIGH (min_label) != NULL_TREE
3724 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3725 CASE_HIGH (min_label)
3726 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3728 /* If OP's value range is ~[2,8] and the high label range is
3729 7 ... 10, truncate the label's range to 9 ... 10. */
3730 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3731 && CASE_HIGH (max_label) != NULL_TREE
3732 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3733 CASE_LOW (max_label)
3734 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3738 /* Canonicalize singleton case ranges. */
3739 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
3740 CASE_HIGH (min_label) = NULL_TREE;
3741 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
3742 CASE_HIGH (max_label) = NULL_TREE;
3745 /* We can also eliminate case labels that lie completely outside OP's value
3746 range. */
3748 /* Bail out if this is just all edges taken. */
3749 if (i == 1
3750 && j == n - 1
3751 && take_default)
3752 return false;
3754 /* Build a new vector of taken case labels. */
3755 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
3756 n2 = 0;
3758 /* Add the default edge, if necessary. */
3759 if (take_default)
3760 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
3762 for (; i <= j; ++i, ++n2)
3763 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
3765 for (; k <= l; ++k, ++n2)
3766 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
3768 /* Mark needed edges. */
3769 for (i = 0; i < n2; ++i)
3771 e = find_edge (gimple_bb (stmt),
3772 label_to_block (cfun,
3773 CASE_LABEL (TREE_VEC_ELT (vec2, i))));
3774 e->aux = (void *)-1;
3777 /* Queue not needed edges for later removal. */
3778 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3780 if (e->aux == (void *)-1)
3782 e->aux = NULL;
3783 continue;
3786 if (dump_file && (dump_flags & TDF_DETAILS))
3788 fprintf (dump_file, "removing unreachable case label\n");
3790 to_remove_edges.safe_push (e);
3791 e->flags &= ~EDGE_EXECUTABLE;
3792 e->flags |= EDGE_IGNORE;
3795 /* And queue an update for the stmt. */
3796 su.stmt = stmt;
3797 su.vec = vec2;
3798 to_update_switch_stmts.safe_push (su);
3799 return false;
3802 void
3803 vr_values::cleanup_edges_and_switches (void)
3805 int i;
3806 edge e;
3807 switch_update *su;
3809 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3810 CFG in a broken state and requires a cfg_cleanup run. */
3811 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
3812 remove_edge (e);
3814 /* Update SWITCH_EXPR case label vector. */
3815 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
3817 size_t j;
3818 size_t n = TREE_VEC_LENGTH (su->vec);
3819 tree label;
3820 gimple_switch_set_num_labels (su->stmt, n);
3821 for (j = 0; j < n; j++)
3822 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
3823 /* As we may have replaced the default label with a regular one
3824 make sure to make it a real default label again. This ensures
3825 optimal expansion. */
3826 label = gimple_switch_label (su->stmt, 0);
3827 CASE_LOW (label) = NULL_TREE;
3828 CASE_HIGH (label) = NULL_TREE;
3831 if (!to_remove_edges.is_empty ())
3833 free_dominance_info (CDI_DOMINATORS);
3834 loops_state_set (LOOPS_NEED_FIXUP);
3837 to_remove_edges.release ();
3838 to_update_switch_stmts.release ();
3841 /* Simplify an integral conversion from an SSA name in STMT. */
3843 static bool
3844 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3846 tree innerop, middleop, finaltype;
3847 gimple *def_stmt;
3848 signop inner_sgn, middle_sgn, final_sgn;
3849 unsigned inner_prec, middle_prec, final_prec;
3850 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
3852 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
3853 if (!INTEGRAL_TYPE_P (finaltype))
3854 return false;
3855 middleop = gimple_assign_rhs1 (stmt);
3856 def_stmt = SSA_NAME_DEF_STMT (middleop);
3857 if (!is_gimple_assign (def_stmt)
3858 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3859 return false;
3860 innerop = gimple_assign_rhs1 (def_stmt);
3861 if (TREE_CODE (innerop) != SSA_NAME
3862 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
3863 return false;
3865 /* Get the value-range of the inner operand. Use get_range_info in
3866 case innerop was created during substitute-and-fold. */
3867 wide_int imin, imax;
3868 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
3869 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
3870 return false;
3871 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
3872 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
3874 /* Simulate the conversion chain to check if the result is equal if
3875 the middle conversion is removed. */
3876 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
3877 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
3878 final_prec = TYPE_PRECISION (finaltype);
3880 /* If the first conversion is not injective, the second must not
3881 be widening. */
3882 if (wi::gtu_p (innermax - innermin,
3883 wi::mask <widest_int> (middle_prec, false))
3884 && middle_prec < final_prec)
3885 return false;
3886 /* We also want a medium value so that we can track the effect that
3887 narrowing conversions with sign change have. */
3888 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
3889 if (inner_sgn == UNSIGNED)
3890 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
3891 else
3892 innermed = 0;
3893 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
3894 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
3895 innermed = innermin;
3897 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
3898 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
3899 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
3900 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
3902 /* Require that the final conversion applied to both the original
3903 and the intermediate range produces the same result. */
3904 final_sgn = TYPE_SIGN (finaltype);
3905 if (wi::ext (middlemin, final_prec, final_sgn)
3906 != wi::ext (innermin, final_prec, final_sgn)
3907 || wi::ext (middlemed, final_prec, final_sgn)
3908 != wi::ext (innermed, final_prec, final_sgn)
3909 || wi::ext (middlemax, final_prec, final_sgn)
3910 != wi::ext (innermax, final_prec, final_sgn))
3911 return false;
3913 gimple_assign_set_rhs1 (stmt, innerop);
3914 fold_stmt (gsi, follow_single_use_edges);
3915 return true;
3918 /* Simplify a conversion from integral SSA name to float in STMT. */
3920 bool
3921 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
3922 gimple *stmt)
3924 tree rhs1 = gimple_assign_rhs1 (stmt);
3925 value_range *vr = get_value_range (rhs1);
3926 scalar_float_mode fltmode
3927 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
3928 scalar_int_mode mode;
3929 tree tem;
3930 gassign *conv;
3932 /* We can only handle constant ranges. */
3933 if (vr->type != VR_RANGE
3934 || TREE_CODE (vr->min) != INTEGER_CST
3935 || TREE_CODE (vr->max) != INTEGER_CST)
3936 return false;
3938 /* First check if we can use a signed type in place of an unsigned. */
3939 scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
3940 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
3941 && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
3942 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
3943 mode = rhs_mode;
3944 /* If we can do the conversion in the current input mode do nothing. */
3945 else if (can_float_p (fltmode, rhs_mode,
3946 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
3947 return false;
3948 /* Otherwise search for a mode we can use, starting from the narrowest
3949 integer mode available. */
3950 else
3952 mode = NARROWEST_INT_MODE;
3953 for (;;)
3955 /* If we cannot do a signed conversion to float from mode
3956 or if the value-range does not fit in the signed type
3957 try with a wider mode. */
3958 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
3959 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
3960 break;
3962 /* But do not widen the input. Instead leave that to the
3963 optabs expansion code. */
3964 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
3965 || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
3966 return false;
3970 /* It works, insert a truncation or sign-change before the
3971 float conversion. */
3972 tem = make_ssa_name (build_nonstandard_integer_type
3973 (GET_MODE_PRECISION (mode), 0));
3974 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
3975 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
3976 gimple_assign_set_rhs1 (stmt, tem);
3977 fold_stmt (gsi, follow_single_use_edges);
3979 return true;
3982 /* Simplify an internal fn call using ranges if possible. */
3984 bool
3985 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi,
3986 gimple *stmt)
3988 enum tree_code subcode;
3989 bool is_ubsan = false;
3990 bool ovf = false;
3991 switch (gimple_call_internal_fn (stmt))
3993 case IFN_UBSAN_CHECK_ADD:
3994 subcode = PLUS_EXPR;
3995 is_ubsan = true;
3996 break;
3997 case IFN_UBSAN_CHECK_SUB:
3998 subcode = MINUS_EXPR;
3999 is_ubsan = true;
4000 break;
4001 case IFN_UBSAN_CHECK_MUL:
4002 subcode = MULT_EXPR;
4003 is_ubsan = true;
4004 break;
4005 case IFN_ADD_OVERFLOW:
4006 subcode = PLUS_EXPR;
4007 break;
4008 case IFN_SUB_OVERFLOW:
4009 subcode = MINUS_EXPR;
4010 break;
4011 case IFN_MUL_OVERFLOW:
4012 subcode = MULT_EXPR;
4013 break;
4014 default:
4015 return false;
4018 tree op0 = gimple_call_arg (stmt, 0);
4019 tree op1 = gimple_call_arg (stmt, 1);
4020 tree type;
4021 if (is_ubsan)
4023 type = TREE_TYPE (op0);
4024 if (VECTOR_TYPE_P (type))
4025 return false;
4027 else if (gimple_call_lhs (stmt) == NULL_TREE)
4028 return false;
4029 else
4030 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
4031 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
4032 || (is_ubsan && ovf))
4033 return false;
4035 gimple *g;
4036 location_t loc = gimple_location (stmt);
4037 if (is_ubsan)
4038 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
4039 else
4041 int prec = TYPE_PRECISION (type);
4042 tree utype = type;
4043 if (ovf
4044 || !useless_type_conversion_p (type, TREE_TYPE (op0))
4045 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
4046 utype = build_nonstandard_integer_type (prec, 1);
4047 if (TREE_CODE (op0) == INTEGER_CST)
4048 op0 = fold_convert (utype, op0);
4049 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
4051 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
4052 gimple_set_location (g, loc);
4053 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4054 op0 = gimple_assign_lhs (g);
4056 if (TREE_CODE (op1) == INTEGER_CST)
4057 op1 = fold_convert (utype, op1);
4058 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
4060 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
4061 gimple_set_location (g, loc);
4062 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4063 op1 = gimple_assign_lhs (g);
4065 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
4066 gimple_set_location (g, loc);
4067 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4068 if (utype != type)
4070 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
4071 gimple_assign_lhs (g));
4072 gimple_set_location (g, loc);
4073 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4075 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
4076 gimple_assign_lhs (g),
4077 build_int_cst (type, ovf));
4079 gimple_set_location (g, loc);
4080 gsi_replace (gsi, g, false);
4081 return true;
4084 /* Return true if VAR is a two-valued variable. Set a and b with the
4085 two-values when it is true. Return false otherwise. */
4087 bool
4088 vr_values::two_valued_val_range_p (tree var, tree *a, tree *b)
4090 value_range *vr = get_value_range (var);
4091 if ((vr->type != VR_RANGE
4092 && vr->type != VR_ANTI_RANGE)
4093 || TREE_CODE (vr->min) != INTEGER_CST
4094 || TREE_CODE (vr->max) != INTEGER_CST)
4095 return false;
4097 if (vr->type == VR_RANGE
4098 && wi::to_wide (vr->max) - wi::to_wide (vr->min) == 1)
4100 *a = vr->min;
4101 *b = vr->max;
4102 return true;
4105 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4106 if (vr->type == VR_ANTI_RANGE
4107 && (wi::to_wide (vr->min)
4108 - wi::to_wide (vrp_val_min (TREE_TYPE (var)))) == 1
4109 && (wi::to_wide (vrp_val_max (TREE_TYPE (var)))
4110 - wi::to_wide (vr->max)) == 1)
4112 *a = vrp_val_min (TREE_TYPE (var));
4113 *b = vrp_val_max (TREE_TYPE (var));
4114 return true;
4117 return false;
4120 /* Simplify STMT using ranges if possible. */
4122 bool
4123 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
4125 gimple *stmt = gsi_stmt (*gsi);
4126 if (is_gimple_assign (stmt))
4128 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4129 tree rhs1 = gimple_assign_rhs1 (stmt);
4130 tree rhs2 = gimple_assign_rhs2 (stmt);
4131 tree lhs = gimple_assign_lhs (stmt);
4132 tree val1 = NULL_TREE, val2 = NULL_TREE;
4133 use_operand_p use_p;
4134 gimple *use_stmt;
4136 /* Convert:
4137 LHS = CST BINOP VAR
4138 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4140 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4142 Also handles:
4143 LHS = VAR BINOP CST
4144 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4146 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4148 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
4149 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4150 && ((TREE_CODE (rhs1) == INTEGER_CST
4151 && TREE_CODE (rhs2) == SSA_NAME)
4152 || (TREE_CODE (rhs2) == INTEGER_CST
4153 && TREE_CODE (rhs1) == SSA_NAME))
4154 && single_imm_use (lhs, &use_p, &use_stmt)
4155 && gimple_code (use_stmt) == GIMPLE_COND)
4158 tree new_rhs1 = NULL_TREE;
4159 tree new_rhs2 = NULL_TREE;
4160 tree cmp_var = NULL_TREE;
4162 if (TREE_CODE (rhs2) == SSA_NAME
4163 && two_valued_val_range_p (rhs2, &val1, &val2))
4165 /* Optimize RHS1 OP [VAL1, VAL2]. */
4166 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
4167 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
4168 cmp_var = rhs2;
4170 else if (TREE_CODE (rhs1) == SSA_NAME
4171 && two_valued_val_range_p (rhs1, &val1, &val2))
4173 /* Optimize [VAL1, VAL2] OP RHS2. */
4174 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
4175 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
4176 cmp_var = rhs1;
4179 /* If we could not find two-vals or the optimzation is invalid as
4180 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4181 if (new_rhs1 && new_rhs2)
4183 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
4184 gimple_assign_set_rhs_with_ops (gsi,
4185 COND_EXPR, cond,
4186 new_rhs1,
4187 new_rhs2);
4188 update_stmt (gsi_stmt (*gsi));
4189 fold_stmt (gsi, follow_single_use_edges);
4190 return true;
4194 switch (rhs_code)
4196 case EQ_EXPR:
4197 case NE_EXPR:
4198 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4199 if the RHS is zero or one, and the LHS are known to be boolean
4200 values. */
4201 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4202 return simplify_truth_ops_using_ranges (gsi, stmt);
4203 break;
4205 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4206 and BIT_AND_EXPR respectively if the first operand is greater
4207 than zero and the second operand is an exact power of two.
4208 Also optimize TRUNC_MOD_EXPR away if the second operand is
4209 constant and the first operand already has the right value
4210 range. */
4211 case TRUNC_DIV_EXPR:
4212 case TRUNC_MOD_EXPR:
4213 if ((TREE_CODE (rhs1) == SSA_NAME
4214 || TREE_CODE (rhs1) == INTEGER_CST)
4215 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4216 return simplify_div_or_mod_using_ranges (gsi, stmt);
4217 break;
4219 /* Transform ABS (X) into X or -X as appropriate. */
4220 case ABS_EXPR:
4221 if (TREE_CODE (rhs1) == SSA_NAME
4222 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4223 return simplify_abs_using_ranges (gsi, stmt);
4224 break;
4226 case BIT_AND_EXPR:
4227 case BIT_IOR_EXPR:
4228 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4229 if all the bits being cleared are already cleared or
4230 all the bits being set are already set. */
4231 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4232 return simplify_bit_ops_using_ranges (gsi, stmt);
4233 break;
4235 CASE_CONVERT:
4236 if (TREE_CODE (rhs1) == SSA_NAME
4237 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4238 return simplify_conversion_using_ranges (gsi, stmt);
4239 break;
4241 case FLOAT_EXPR:
4242 if (TREE_CODE (rhs1) == SSA_NAME
4243 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4244 return simplify_float_conversion_using_ranges (gsi, stmt);
4245 break;
4247 case MIN_EXPR:
4248 case MAX_EXPR:
4249 return simplify_min_or_max_using_ranges (gsi, stmt);
4251 default:
4252 break;
4255 else if (gimple_code (stmt) == GIMPLE_COND)
4256 return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
4257 else if (gimple_code (stmt) == GIMPLE_SWITCH)
4258 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
4259 else if (is_gimple_call (stmt)
4260 && gimple_call_internal_p (stmt))
4261 return simplify_internal_call_using_ranges (gsi, stmt);
4263 return false;
4266 void
4267 vr_values::set_vr_value (tree var, value_range *vr)
4269 if (SSA_NAME_VERSION (var) >= num_vr_values)
4270 return;
4271 vr_value[SSA_NAME_VERSION (var)] = vr;