PR rtl-optimization/88018
[official-gcc.git] / gcc / vr-values.c
blob41862b976010cd50d1a256165d20bb1e633512a8
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 vr->update (VR_RANGE, zero, vrp_val_max (type));
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 vr->set_varying ();
68 else
69 vr->update (VR_RANGE, build_int_cst (type, 0), build_int_cst (type, 1));
73 /* Return value range information for VAR.
75 If we have no values ranges recorded (ie, VRP is not running), then
76 return NULL. Otherwise create an empty range if none existed for VAR. */
78 value_range *
79 vr_values::get_value_range (const_tree var)
81 static const value_range vr_const_varying (VR_VARYING, NULL, NULL);
82 value_range *vr;
83 tree sym;
84 unsigned ver = SSA_NAME_VERSION (var);
86 /* If we have no recorded ranges, then return NULL. */
87 if (! vr_value)
88 return NULL;
90 /* If we query the range for a new SSA name return an unmodifiable VARYING.
91 We should get here at most from the substitute-and-fold stage which
92 will never try to change values. */
93 if (ver >= num_vr_values)
94 return CONST_CAST (value_range *, &vr_const_varying);
96 vr = vr_value[ver];
97 if (vr)
98 return vr;
100 /* After propagation finished do not allocate new value-ranges. */
101 if (values_propagated)
102 return CONST_CAST (value_range *, &vr_const_varying);
104 /* Create a default value range. */
105 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
106 vr->set_undefined ();
108 /* If VAR is a default definition of a parameter, the variable can
109 take any value in VAR's type. */
110 if (SSA_NAME_IS_DEFAULT_DEF (var))
112 sym = SSA_NAME_VAR (var);
113 if (TREE_CODE (sym) == PARM_DECL)
115 /* Try to use the "nonnull" attribute to create ~[0, 0]
116 anti-ranges for pointers. Note that this is only valid with
117 default definitions of PARM_DECLs. */
118 if (POINTER_TYPE_P (TREE_TYPE (sym))
119 && (nonnull_arg_p (sym)
120 || get_ptr_nonnull (var)))
121 vr->set_nonnull (TREE_TYPE (sym));
122 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
124 get_range_info (var, *vr);
125 if (vr->undefined_p ())
126 vr->set_varying ();
128 else
129 vr->set_varying ();
131 else if (TREE_CODE (sym) == RESULT_DECL
132 && DECL_BY_REFERENCE (sym))
133 vr->set_nonnull (TREE_TYPE (sym));
136 return vr;
139 /* Set value-ranges of all SSA names defined by STMT to varying. */
141 void
142 vr_values::set_defs_to_varying (gimple *stmt)
144 ssa_op_iter i;
145 tree def;
146 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
148 value_range *vr = get_value_range (def);
149 /* Avoid writing to vr_const_varying get_value_range may return. */
150 if (!vr->varying_p ())
151 vr->set_varying ();
155 /* Update the value range and equivalence set for variable VAR to
156 NEW_VR. Return true if NEW_VR is different from VAR's previous
157 value.
159 NOTE: This function assumes that NEW_VR is a temporary value range
160 object created for the sole purpose of updating VAR's range. The
161 storage used by the equivalence set from NEW_VR will be freed by
162 this function. Do not call update_value_range when NEW_VR
163 is the range object associated with another SSA name. */
165 bool
166 vr_values::update_value_range (const_tree var, value_range *new_vr)
168 value_range *old_vr;
169 bool is_new;
171 /* If there is a value-range on the SSA name from earlier analysis
172 factor that in. */
173 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
175 value_range nr;
176 value_range_kind rtype = get_range_info (var, nr);
177 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
178 new_vr->intersect (&nr);
181 /* Update the value range, if necessary. */
182 old_vr = get_value_range (var);
183 is_new = !old_vr->equal_p (*new_vr, /*ignore_equivs=*/false);
185 if (is_new)
187 /* Do not allow transitions up the lattice. The following
188 is slightly more awkward than just new_vr->type < old_vr->type
189 because VR_RANGE and VR_ANTI_RANGE need to be considered
190 the same. We may not have is_new when transitioning to
191 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
192 called. */
193 if (new_vr->undefined_p ())
195 old_vr->set_varying ();
196 new_vr->set_varying ();
197 return true;
199 else
200 old_vr->set (new_vr->kind (),
201 new_vr->min (), new_vr->max (), new_vr->equiv ());
204 new_vr->equiv_clear ();
206 return is_new;
209 /* Return true if value range VR involves exactly one symbol SYM. */
211 static bool
212 symbolic_range_based_on_p (value_range_base *vr, const_tree sym)
214 bool neg, min_has_symbol, max_has_symbol;
215 tree inv;
217 if (is_gimple_min_invariant (vr->min ()))
218 min_has_symbol = false;
219 else if (get_single_symbol (vr->min (), &neg, &inv) == sym)
220 min_has_symbol = true;
221 else
222 return false;
224 if (is_gimple_min_invariant (vr->max ()))
225 max_has_symbol = false;
226 else if (get_single_symbol (vr->max (), &neg, &inv) == sym)
227 max_has_symbol = true;
228 else
229 return false;
231 return (min_has_symbol || max_has_symbol);
234 /* Return true if the result of assignment STMT is know to be non-zero. */
236 static bool
237 gimple_assign_nonzero_p (gimple *stmt)
239 enum tree_code code = gimple_assign_rhs_code (stmt);
240 bool strict_overflow_p;
241 switch (get_gimple_rhs_class (code))
243 case GIMPLE_UNARY_RHS:
244 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
245 gimple_expr_type (stmt),
246 gimple_assign_rhs1 (stmt),
247 &strict_overflow_p);
248 case GIMPLE_BINARY_RHS:
249 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
250 gimple_expr_type (stmt),
251 gimple_assign_rhs1 (stmt),
252 gimple_assign_rhs2 (stmt),
253 &strict_overflow_p);
254 case GIMPLE_TERNARY_RHS:
255 return false;
256 case GIMPLE_SINGLE_RHS:
257 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
258 &strict_overflow_p);
259 case GIMPLE_INVALID_RHS:
260 gcc_unreachable ();
261 default:
262 gcc_unreachable ();
266 /* Return true if STMT is known to compute a non-zero value. */
268 static bool
269 gimple_stmt_nonzero_p (gimple *stmt)
271 switch (gimple_code (stmt))
273 case GIMPLE_ASSIGN:
274 return gimple_assign_nonzero_p (stmt);
275 case GIMPLE_CALL:
277 gcall *call_stmt = as_a<gcall *> (stmt);
278 return (gimple_call_nonnull_result_p (call_stmt)
279 || gimple_call_nonnull_arg (call_stmt));
281 default:
282 gcc_unreachable ();
285 /* Like tree_expr_nonzero_p, but this function uses value ranges
286 obtained so far. */
288 bool
289 vr_values::vrp_stmt_computes_nonzero (gimple *stmt)
291 if (gimple_stmt_nonzero_p (stmt))
292 return true;
294 /* If we have an expression of the form &X->a, then the expression
295 is nonnull if X is nonnull. */
296 if (is_gimple_assign (stmt)
297 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
299 tree expr = gimple_assign_rhs1 (stmt);
300 tree base = get_base_address (TREE_OPERAND (expr, 0));
302 if (base != NULL_TREE
303 && TREE_CODE (base) == MEM_REF
304 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
306 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
307 if (!range_includes_zero_p (vr))
308 return true;
312 return false;
315 /* Returns true if EXPR is a valid value (as expected by compare_values) --
316 a gimple invariant, or SSA_NAME +- CST. */
318 static bool
319 valid_value_p (tree expr)
321 if (TREE_CODE (expr) == SSA_NAME)
322 return true;
324 if (TREE_CODE (expr) == PLUS_EXPR
325 || TREE_CODE (expr) == MINUS_EXPR)
326 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
327 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
329 return is_gimple_min_invariant (expr);
332 /* If OP has a value range with a single constant value return that,
333 otherwise return NULL_TREE. This returns OP itself if OP is a
334 constant. */
336 tree
337 vr_values::op_with_constant_singleton_value_range (tree op)
339 if (is_gimple_min_invariant (op))
340 return op;
342 if (TREE_CODE (op) != SSA_NAME)
343 return NULL_TREE;
345 return value_range_constant_singleton (get_value_range (op));
348 /* Return true if op is in a boolean [0, 1] value-range. */
350 bool
351 vr_values::op_with_boolean_value_range_p (tree op)
353 value_range *vr;
355 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
356 return true;
358 if (integer_zerop (op)
359 || integer_onep (op))
360 return true;
362 if (TREE_CODE (op) != SSA_NAME)
363 return false;
365 vr = get_value_range (op);
366 return (vr->kind () == VR_RANGE
367 && integer_zerop (vr->min ())
368 && integer_onep (vr->max ()));
371 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
372 true and store it in *VR_P. */
374 void
375 vr_values::extract_range_for_var_from_comparison_expr (tree var,
376 enum tree_code cond_code,
377 tree op, tree limit,
378 value_range *vr_p)
380 tree min, max, type;
381 value_range *limit_vr;
382 type = TREE_TYPE (var);
384 /* For pointer arithmetic, we only keep track of pointer equality
385 and inequality. If we arrive here with unfolded conditions like
386 _1 > _1 do not derive anything. */
387 if ((POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
388 || limit == var)
390 vr_p->set_varying ();
391 return;
394 /* If LIMIT is another SSA name and LIMIT has a range of its own,
395 try to use LIMIT's range to avoid creating symbolic ranges
396 unnecessarily. */
397 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
399 /* LIMIT's range is only interesting if it has any useful information. */
400 if (! limit_vr
401 || limit_vr->undefined_p ()
402 || limit_vr->varying_p ()
403 || (limit_vr->symbolic_p ()
404 && ! (limit_vr->kind () == VR_RANGE
405 && (limit_vr->min () == limit_vr->max ()
406 || operand_equal_p (limit_vr->min (),
407 limit_vr->max (), 0)))))
408 limit_vr = NULL;
410 /* Initially, the new range has the same set of equivalences of
411 VAR's range. This will be revised before returning the final
412 value. Since assertions may be chained via mutually exclusive
413 predicates, we will need to trim the set of equivalences before
414 we are done. */
415 gcc_assert (vr_p->equiv () == NULL);
416 vr_p->equiv_add (var, get_value_range (var), &vrp_equiv_obstack);
418 /* Extract a new range based on the asserted comparison for VAR and
419 LIMIT's value range. Notice that if LIMIT has an anti-range, we
420 will only use it for equality comparisons (EQ_EXPR). For any
421 other kind of assertion, we cannot derive a range from LIMIT's
422 anti-range that can be used to describe the new range. For
423 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
424 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
425 no single range for x_2 that could describe LE_EXPR, so we might
426 as well build the range [b_4, +INF] for it.
427 One special case we handle is extracting a range from a
428 range test encoded as (unsigned)var + CST <= limit. */
429 if (TREE_CODE (op) == NOP_EXPR
430 || TREE_CODE (op) == PLUS_EXPR)
432 if (TREE_CODE (op) == PLUS_EXPR)
434 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
435 TREE_OPERAND (op, 1));
436 max = int_const_binop (PLUS_EXPR, limit, min);
437 op = TREE_OPERAND (op, 0);
439 else
441 min = build_int_cst (TREE_TYPE (var), 0);
442 max = limit;
445 /* Make sure to not set TREE_OVERFLOW on the final type
446 conversion. We are willingly interpreting large positive
447 unsigned values as negative signed values here. */
448 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
449 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
451 /* We can transform a max, min range to an anti-range or
452 vice-versa. Use set_and_canonicalize which does this for
453 us. */
454 if (cond_code == LE_EXPR)
455 vr_p->set_and_canonicalize (VR_RANGE, min, max, vr_p->equiv ());
456 else if (cond_code == GT_EXPR)
457 vr_p->set_and_canonicalize (VR_ANTI_RANGE, min, max, vr_p->equiv ());
458 else
459 gcc_unreachable ();
461 else if (cond_code == EQ_EXPR)
463 enum value_range_kind range_type;
465 if (limit_vr)
467 range_type = limit_vr->kind ();
468 min = limit_vr->min ();
469 max = limit_vr->max ();
471 else
473 range_type = VR_RANGE;
474 min = limit;
475 max = limit;
478 vr_p->update (range_type, min, max);
480 /* When asserting the equality VAR == LIMIT and LIMIT is another
481 SSA name, the new range will also inherit the equivalence set
482 from LIMIT. */
483 if (TREE_CODE (limit) == SSA_NAME)
484 vr_p->equiv_add (limit, get_value_range (limit), &vrp_equiv_obstack);
486 else if (cond_code == NE_EXPR)
488 /* As described above, when LIMIT's range is an anti-range and
489 this assertion is an inequality (NE_EXPR), then we cannot
490 derive anything from the anti-range. For instance, if
491 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
492 not imply that VAR's range is [0, 0]. So, in the case of
493 anti-ranges, we just assert the inequality using LIMIT and
494 not its anti-range.
496 If LIMIT_VR is a range, we can only use it to build a new
497 anti-range if LIMIT_VR is a single-valued range. For
498 instance, if LIMIT_VR is [0, 1], the predicate
499 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
500 Rather, it means that for value 0 VAR should be ~[0, 0]
501 and for value 1, VAR should be ~[1, 1]. We cannot
502 represent these ranges.
504 The only situation in which we can build a valid
505 anti-range is when LIMIT_VR is a single-valued range
506 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
507 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
508 if (limit_vr
509 && limit_vr->kind () == VR_RANGE
510 && compare_values (limit_vr->min (), limit_vr->max ()) == 0)
512 min = limit_vr->min ();
513 max = limit_vr->max ();
515 else
517 /* In any other case, we cannot use LIMIT's range to build a
518 valid anti-range. */
519 min = max = limit;
522 /* If MIN and MAX cover the whole range for their type, then
523 just use the original LIMIT. */
524 if (INTEGRAL_TYPE_P (type)
525 && vrp_val_is_min (min)
526 && vrp_val_is_max (max))
527 min = max = limit;
529 vr_p->set_and_canonicalize (VR_ANTI_RANGE, min, max, vr_p->equiv ());
531 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
533 min = TYPE_MIN_VALUE (type);
535 if (limit_vr == NULL || limit_vr->kind () == VR_ANTI_RANGE)
536 max = limit;
537 else
539 /* If LIMIT_VR is of the form [N1, N2], we need to build the
540 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
541 LT_EXPR. */
542 max = limit_vr->max ();
545 /* If the maximum value forces us to be out of bounds, simply punt.
546 It would be pointless to try and do anything more since this
547 all should be optimized away above us. */
548 if (cond_code == LT_EXPR
549 && compare_values (max, min) == 0)
550 vr_p->set_varying ();
551 else
553 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
554 if (cond_code == LT_EXPR)
556 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
557 && !TYPE_UNSIGNED (TREE_TYPE (max)))
558 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
559 build_int_cst (TREE_TYPE (max), -1));
560 else
561 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
562 build_int_cst (TREE_TYPE (max), 1));
563 /* Signal to compare_values_warnv this expr doesn't overflow. */
564 if (EXPR_P (max))
565 TREE_NO_WARNING (max) = 1;
568 vr_p->update (VR_RANGE, min, max);
571 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
573 max = TYPE_MAX_VALUE (type);
575 if (limit_vr == NULL || limit_vr->kind () == VR_ANTI_RANGE)
576 min = limit;
577 else
579 /* If LIMIT_VR is of the form [N1, N2], we need to build the
580 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
581 GT_EXPR. */
582 min = limit_vr->min ();
585 /* If the minimum value forces us to be out of bounds, simply punt.
586 It would be pointless to try and do anything more since this
587 all should be optimized away above us. */
588 if (cond_code == GT_EXPR
589 && compare_values (min, max) == 0)
590 vr_p->set_varying ();
591 else
593 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
594 if (cond_code == GT_EXPR)
596 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
597 && !TYPE_UNSIGNED (TREE_TYPE (min)))
598 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
599 build_int_cst (TREE_TYPE (min), -1));
600 else
601 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
602 build_int_cst (TREE_TYPE (min), 1));
603 /* Signal to compare_values_warnv this expr doesn't overflow. */
604 if (EXPR_P (min))
605 TREE_NO_WARNING (min) = 1;
608 vr_p->update (VR_RANGE, min, max);
611 else
612 gcc_unreachable ();
614 /* Finally intersect the new range with what we already know about var. */
615 vr_p->intersect (get_value_range (var));
618 /* Extract value range information from an ASSERT_EXPR EXPR and store
619 it in *VR_P. */
621 void
622 vr_values::extract_range_from_assert (value_range *vr_p, tree expr)
624 tree var = ASSERT_EXPR_VAR (expr);
625 tree cond = ASSERT_EXPR_COND (expr);
626 tree limit, op;
627 enum tree_code cond_code;
628 gcc_assert (COMPARISON_CLASS_P (cond));
630 /* Find VAR in the ASSERT_EXPR conditional. */
631 if (var == TREE_OPERAND (cond, 0)
632 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
633 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
635 /* If the predicate is of the form VAR COMP LIMIT, then we just
636 take LIMIT from the RHS and use the same comparison code. */
637 cond_code = TREE_CODE (cond);
638 limit = TREE_OPERAND (cond, 1);
639 op = TREE_OPERAND (cond, 0);
641 else
643 /* If the predicate is of the form LIMIT COMP VAR, then we need
644 to flip around the comparison code to create the proper range
645 for VAR. */
646 cond_code = swap_tree_comparison (TREE_CODE (cond));
647 limit = TREE_OPERAND (cond, 0);
648 op = TREE_OPERAND (cond, 1);
650 extract_range_for_var_from_comparison_expr (var, cond_code, op,
651 limit, vr_p);
654 /* Extract range information from SSA name VAR and store it in VR. If
655 VAR has an interesting range, use it. Otherwise, create the
656 range [VAR, VAR] and return it. This is useful in situations where
657 we may have conditionals testing values of VARYING names. For
658 instance,
660 x_3 = y_5;
661 if (x_3 > y_5)
664 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
665 always false. */
667 void
668 vr_values::extract_range_from_ssa_name (value_range *vr, tree var)
670 value_range *var_vr = get_value_range (var);
672 if (!var_vr->varying_p ())
673 vr->deep_copy (var_vr);
674 else
675 vr->set (var);
677 vr->equiv_add (var, get_value_range (var), &vrp_equiv_obstack);
680 /* Extract range information from a binary expression OP0 CODE OP1 based on
681 the ranges of each of its operands with resulting type EXPR_TYPE.
682 The resulting range is stored in *VR. */
684 void
685 vr_values::extract_range_from_binary_expr (value_range *vr,
686 enum tree_code code,
687 tree expr_type, tree op0, tree op1)
689 /* Get value ranges for each operand. For constant operands, create
690 a new value range with the operand to simplify processing. */
691 value_range_base vr0, vr1;
692 if (TREE_CODE (op0) == SSA_NAME)
693 vr0 = *(get_value_range (op0));
694 else if (is_gimple_min_invariant (op0))
695 vr0.set (op0);
696 else
697 vr0.set_varying ();
699 if (TREE_CODE (op1) == SSA_NAME)
700 vr1 = *(get_value_range (op1));
701 else if (is_gimple_min_invariant (op1))
702 vr1.set (op1);
703 else
704 vr1.set_varying ();
706 /* If one argument is varying, we can sometimes still deduce a
707 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
708 if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
709 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
711 if (vr0.varying_p () && !vr1.varying_p ())
712 vr0 = value_range (VR_RANGE,
713 vrp_val_min (expr_type),
714 vrp_val_max (expr_type));
715 else if (vr1.varying_p () && !vr0.varying_p ())
716 vr1 = value_range (VR_RANGE,
717 vrp_val_min (expr_type),
718 vrp_val_max (expr_type));
721 ::extract_range_from_binary_expr (vr, code, expr_type, &vr0, &vr1);
723 /* Set value_range for n in following sequence:
724 def = __builtin_memchr (arg, 0, sz)
725 n = def - arg
726 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
728 if (vr->varying_p ()
729 && code == POINTER_DIFF_EXPR
730 && TREE_CODE (op0) == SSA_NAME
731 && TREE_CODE (op1) == SSA_NAME)
733 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
734 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
735 gcall *call_stmt = NULL;
737 if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
738 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
739 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
740 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
741 && (call_stmt = dyn_cast<gcall *>(SSA_NAME_DEF_STMT (op0)))
742 && gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR)
743 && operand_equal_p (op0, gimple_call_lhs (call_stmt), 0)
744 && operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0)
745 && integer_zerop (gimple_call_arg (call_stmt, 1)))
747 tree max = vrp_val_max (ptrdiff_type_node);
748 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
749 tree range_min = build_zero_cst (expr_type);
750 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
751 vr->set (VR_RANGE, range_min, range_max);
752 return;
756 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
757 and based on the other operand, for example if it was deduced from a
758 symbolic comparison. When a bound of the range of the first operand
759 is invariant, we set the corresponding bound of the new range to INF
760 in order to avoid recursing on the range of the second operand. */
761 if (vr->varying_p ()
762 && (code == PLUS_EXPR || code == MINUS_EXPR)
763 && TREE_CODE (op1) == SSA_NAME
764 && vr0.kind () == VR_RANGE
765 && symbolic_range_based_on_p (&vr0, op1))
767 const bool minus_p = (code == MINUS_EXPR);
768 value_range n_vr1;
770 /* Try with VR0 and [-INF, OP1]. */
771 if (is_gimple_min_invariant (minus_p ? vr0.max () : vr0.min ()))
772 n_vr1.set (VR_RANGE, vrp_val_min (expr_type), op1);
774 /* Try with VR0 and [OP1, +INF]. */
775 else if (is_gimple_min_invariant (minus_p ? vr0.min () : vr0.max ()))
776 n_vr1.set (VR_RANGE, op1, vrp_val_max (expr_type));
778 /* Try with VR0 and [OP1, OP1]. */
779 else
780 n_vr1.set (VR_RANGE, op1, op1);
782 ::extract_range_from_binary_expr (vr, code, expr_type, &vr0, &n_vr1);
785 if (vr->varying_p ()
786 && (code == PLUS_EXPR || code == MINUS_EXPR)
787 && TREE_CODE (op0) == SSA_NAME
788 && vr1.kind () == VR_RANGE
789 && symbolic_range_based_on_p (&vr1, op0))
791 const bool minus_p = (code == MINUS_EXPR);
792 value_range n_vr0;
794 /* Try with [-INF, OP0] and VR1. */
795 if (is_gimple_min_invariant (minus_p ? vr1.max () : vr1.min ()))
796 n_vr0.set (VR_RANGE, vrp_val_min (expr_type), op0);
798 /* Try with [OP0, +INF] and VR1. */
799 else if (is_gimple_min_invariant (minus_p ? vr1.min (): vr1.max ()))
800 n_vr0.set (VR_RANGE, op0, vrp_val_max (expr_type));
802 /* Try with [OP0, OP0] and VR1. */
803 else
804 n_vr0.set (op0);
806 ::extract_range_from_binary_expr (vr, code, expr_type, &n_vr0, &vr1);
809 /* If we didn't derive a range for MINUS_EXPR, and
810 op1's range is ~[op0,op0] or vice-versa, then we
811 can derive a non-null range. This happens often for
812 pointer subtraction. */
813 if (vr->varying_p ()
814 && (code == MINUS_EXPR || code == POINTER_DIFF_EXPR)
815 && TREE_CODE (op0) == SSA_NAME
816 && ((vr0.kind () == VR_ANTI_RANGE
817 && vr0.min () == op1
818 && vr0.min () == vr0.max ())
819 || (vr1.kind () == VR_ANTI_RANGE
820 && vr1.min () == op0
821 && vr1.min () == vr1.max ())))
822 vr->set_nonnull (expr_type);
825 /* Extract range information from a unary expression CODE OP0 based on
826 the range of its operand with resulting type TYPE.
827 The resulting range is stored in *VR. */
829 void
830 vr_values::extract_range_from_unary_expr (value_range *vr, enum tree_code code,
831 tree type, tree op0)
833 value_range_base vr0;
835 /* Get value ranges for the operand. For constant operands, create
836 a new value range with the operand to simplify processing. */
837 if (TREE_CODE (op0) == SSA_NAME)
838 vr0 = *(get_value_range (op0));
839 else if (is_gimple_min_invariant (op0))
840 vr0.set (op0);
841 else
842 vr0.set_varying ();
844 ::extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
848 /* Extract range information from a conditional expression STMT based on
849 the ranges of each of its operands and the expression code. */
851 void
852 vr_values::extract_range_from_cond_expr (value_range *vr, gassign *stmt)
854 /* Get value ranges for each operand. For constant operands, create
855 a new value range with the operand to simplify processing. */
856 tree op0 = gimple_assign_rhs2 (stmt);
857 value_range tem0;
858 value_range *vr0 = &tem0;
859 if (TREE_CODE (op0) == SSA_NAME)
860 vr0 = get_value_range (op0);
861 else if (is_gimple_min_invariant (op0))
862 tem0.set (op0);
863 else
864 tem0.set_varying ();
866 tree op1 = gimple_assign_rhs3 (stmt);
867 value_range tem1;
868 value_range *vr1 = &tem1;
869 if (TREE_CODE (op1) == SSA_NAME)
870 vr1 = get_value_range (op1);
871 else if (is_gimple_min_invariant (op1))
872 tem1.set (op1);
873 else
874 tem1.set_varying ();
876 /* The resulting value range is the union of the operand ranges */
877 vr->deep_copy (vr0);
878 vr->union_ (vr1);
882 /* Extract range information from a comparison expression EXPR based
883 on the range of its operand and the expression code. */
885 void
886 vr_values::extract_range_from_comparison (value_range *vr, enum tree_code code,
887 tree type, tree op0, tree op1)
889 bool sop;
890 tree val;
892 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
893 NULL);
894 if (val)
896 /* Since this expression was found on the RHS of an assignment,
897 its type may be different from _Bool. Convert VAL to EXPR's
898 type. */
899 val = fold_convert (type, val);
900 if (is_gimple_min_invariant (val))
901 vr->set (val);
902 else
903 vr->update (VR_RANGE, val, val);
905 else
906 /* The result of a comparison is always true or false. */
907 set_value_range_to_truthvalue (vr, type);
910 /* Helper function for simplify_internal_call_using_ranges and
911 extract_range_basic. Return true if OP0 SUBCODE OP1 for
912 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
913 always overflow. Set *OVF to true if it is known to always
914 overflow. */
916 bool
917 vr_values::check_for_binary_op_overflow (enum tree_code subcode, tree type,
918 tree op0, tree op1, bool *ovf)
920 value_range_base vr0, vr1;
921 if (TREE_CODE (op0) == SSA_NAME)
922 vr0 = *get_value_range (op0);
923 else if (TREE_CODE (op0) == INTEGER_CST)
924 vr0.set (op0);
925 else
926 vr0.set_varying ();
928 if (TREE_CODE (op1) == SSA_NAME)
929 vr1 = *get_value_range (op1);
930 else if (TREE_CODE (op1) == INTEGER_CST)
931 vr1.set (op1);
932 else
933 vr1.set_varying ();
935 tree vr0min = vr0.min (), vr0max = vr0.max ();
936 tree vr1min = vr1.min (), vr1max = vr1.max ();
937 if (!range_int_cst_p (&vr0)
938 || TREE_OVERFLOW (vr0min)
939 || TREE_OVERFLOW (vr0max))
941 vr0min = vrp_val_min (TREE_TYPE (op0));
942 vr0max = vrp_val_max (TREE_TYPE (op0));
944 if (!range_int_cst_p (&vr1)
945 || TREE_OVERFLOW (vr1min)
946 || TREE_OVERFLOW (vr1max))
948 vr1min = vrp_val_min (TREE_TYPE (op1));
949 vr1max = vrp_val_max (TREE_TYPE (op1));
951 *ovf = arith_overflowed_p (subcode, type, vr0min,
952 subcode == MINUS_EXPR ? vr1max : vr1min);
953 if (arith_overflowed_p (subcode, type, vr0max,
954 subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
955 return false;
956 if (subcode == MULT_EXPR)
958 if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
959 || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
960 return false;
962 if (*ovf)
964 /* So far we found that there is an overflow on the boundaries.
965 That doesn't prove that there is an overflow even for all values
966 in between the boundaries. For that compute widest_int range
967 of the result and see if it doesn't overlap the range of
968 type. */
969 widest_int wmin, wmax;
970 widest_int w[4];
971 int i;
972 w[0] = wi::to_widest (vr0min);
973 w[1] = wi::to_widest (vr0max);
974 w[2] = wi::to_widest (vr1min);
975 w[3] = wi::to_widest (vr1max);
976 for (i = 0; i < 4; i++)
978 widest_int wt;
979 switch (subcode)
981 case PLUS_EXPR:
982 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
983 break;
984 case MINUS_EXPR:
985 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
986 break;
987 case MULT_EXPR:
988 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
989 break;
990 default:
991 gcc_unreachable ();
993 if (i == 0)
995 wmin = wt;
996 wmax = wt;
998 else
1000 wmin = wi::smin (wmin, wt);
1001 wmax = wi::smax (wmax, wt);
1004 /* The result of op0 CODE op1 is known to be in range
1005 [wmin, wmax]. */
1006 widest_int wtmin = wi::to_widest (vrp_val_min (type));
1007 widest_int wtmax = wi::to_widest (vrp_val_max (type));
1008 /* If all values in [wmin, wmax] are smaller than
1009 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1010 the arithmetic operation will always overflow. */
1011 if (wmax < wtmin || wmin > wtmax)
1012 return true;
1013 return false;
1015 return true;
1018 /* Try to derive a nonnegative or nonzero range out of STMT relying
1019 primarily on generic routines in fold in conjunction with range data.
1020 Store the result in *VR */
1022 void
1023 vr_values::extract_range_basic (value_range *vr, gimple *stmt)
1025 bool sop;
1026 tree type = gimple_expr_type (stmt);
1028 if (is_gimple_call (stmt))
1030 tree arg;
1031 int mini, maxi, zerov = 0, prec;
1032 enum tree_code subcode = ERROR_MARK;
1033 combined_fn cfn = gimple_call_combined_fn (stmt);
1034 scalar_int_mode mode;
1036 switch (cfn)
1038 case CFN_BUILT_IN_CONSTANT_P:
1039 /* If the call is __builtin_constant_p and the argument is a
1040 function parameter resolve it to false. This avoids bogus
1041 array bound warnings.
1042 ??? We could do this as early as inlining is finished. */
1043 arg = gimple_call_arg (stmt, 0);
1044 if (TREE_CODE (arg) == SSA_NAME
1045 && SSA_NAME_IS_DEFAULT_DEF (arg)
1046 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
1047 && cfun->after_inlining)
1049 vr->set_null (type);
1050 return;
1052 break;
1053 /* Both __builtin_ffs* and __builtin_popcount return
1054 [0, prec]. */
1055 CASE_CFN_FFS:
1056 CASE_CFN_POPCOUNT:
1057 arg = gimple_call_arg (stmt, 0);
1058 prec = TYPE_PRECISION (TREE_TYPE (arg));
1059 mini = 0;
1060 maxi = prec;
1061 if (TREE_CODE (arg) == SSA_NAME)
1063 value_range *vr0 = get_value_range (arg);
1064 /* If arg is non-zero, then ffs or popcount are non-zero. */
1065 if (range_includes_zero_p (vr0) == 0)
1066 mini = 1;
1067 /* If some high bits are known to be zero,
1068 we can decrease the maximum. */
1069 if (vr0->kind () == VR_RANGE
1070 && TREE_CODE (vr0->max ()) == INTEGER_CST
1071 && !operand_less_p (vr0->min (),
1072 build_zero_cst (TREE_TYPE (vr0->min ()))))
1073 maxi = tree_floor_log2 (vr0->max ()) + 1;
1075 goto bitop_builtin;
1076 /* __builtin_parity* returns [0, 1]. */
1077 CASE_CFN_PARITY:
1078 mini = 0;
1079 maxi = 1;
1080 goto bitop_builtin;
1081 /* __builtin_c[lt]z* return [0, prec-1], except for
1082 when the argument is 0, but that is undefined behavior.
1083 On many targets where the CLZ RTL or optab value is defined
1084 for 0 the value is prec, so include that in the range
1085 by default. */
1086 CASE_CFN_CLZ:
1087 arg = gimple_call_arg (stmt, 0);
1088 prec = TYPE_PRECISION (TREE_TYPE (arg));
1089 mini = 0;
1090 maxi = prec;
1091 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1092 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1093 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov)
1094 /* Handle only the single common value. */
1095 && zerov != prec)
1096 /* Magic value to give up, unless vr0 proves
1097 arg is non-zero. */
1098 mini = -2;
1099 if (TREE_CODE (arg) == SSA_NAME)
1101 value_range *vr0 = get_value_range (arg);
1102 /* From clz of VR_RANGE minimum we can compute
1103 result maximum. */
1104 if (vr0->kind () == VR_RANGE
1105 && TREE_CODE (vr0->min ()) == INTEGER_CST)
1107 maxi = prec - 1 - tree_floor_log2 (vr0->min ());
1108 if (maxi != prec)
1109 mini = 0;
1111 else if (vr0->kind () == VR_ANTI_RANGE
1112 && integer_zerop (vr0->min ()))
1114 maxi = prec - 1;
1115 mini = 0;
1117 if (mini == -2)
1118 break;
1119 /* From clz of VR_RANGE maximum we can compute
1120 result minimum. */
1121 if (vr0->kind () == VR_RANGE
1122 && TREE_CODE (vr0->max ()) == INTEGER_CST)
1124 mini = prec - 1 - tree_floor_log2 (vr0->max ());
1125 if (mini == prec)
1126 break;
1129 if (mini == -2)
1130 break;
1131 goto bitop_builtin;
1132 /* __builtin_ctz* return [0, prec-1], except for
1133 when the argument is 0, but that is undefined behavior.
1134 If there is a ctz optab for this mode and
1135 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1136 otherwise just assume 0 won't be seen. */
1137 CASE_CFN_CTZ:
1138 arg = gimple_call_arg (stmt, 0);
1139 prec = TYPE_PRECISION (TREE_TYPE (arg));
1140 mini = 0;
1141 maxi = prec - 1;
1142 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1143 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1144 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov))
1146 /* Handle only the two common values. */
1147 if (zerov == -1)
1148 mini = -1;
1149 else if (zerov == prec)
1150 maxi = prec;
1151 else
1152 /* Magic value to give up, unless vr0 proves
1153 arg is non-zero. */
1154 mini = -2;
1156 if (TREE_CODE (arg) == SSA_NAME)
1158 value_range *vr0 = get_value_range (arg);
1159 /* If arg is non-zero, then use [0, prec - 1]. */
1160 if ((vr0->kind () == VR_RANGE
1161 && integer_nonzerop (vr0->min ()))
1162 || (vr0->kind () == VR_ANTI_RANGE
1163 && integer_zerop (vr0->min ())))
1165 mini = 0;
1166 maxi = prec - 1;
1168 /* If some high bits are known to be zero,
1169 we can decrease the result maximum. */
1170 if (vr0->kind () == VR_RANGE
1171 && TREE_CODE (vr0->max ()) == INTEGER_CST)
1173 maxi = tree_floor_log2 (vr0->max ());
1174 /* For vr0 [0, 0] give up. */
1175 if (maxi == -1)
1176 break;
1179 if (mini == -2)
1180 break;
1181 goto bitop_builtin;
1182 /* __builtin_clrsb* returns [0, prec-1]. */
1183 CASE_CFN_CLRSB:
1184 arg = gimple_call_arg (stmt, 0);
1185 prec = TYPE_PRECISION (TREE_TYPE (arg));
1186 mini = 0;
1187 maxi = prec - 1;
1188 goto bitop_builtin;
1189 bitop_builtin:
1190 vr->set (VR_RANGE, build_int_cst (type, mini),
1191 build_int_cst (type, maxi));
1192 return;
1193 case CFN_UBSAN_CHECK_ADD:
1194 subcode = PLUS_EXPR;
1195 break;
1196 case CFN_UBSAN_CHECK_SUB:
1197 subcode = MINUS_EXPR;
1198 break;
1199 case CFN_UBSAN_CHECK_MUL:
1200 subcode = MULT_EXPR;
1201 break;
1202 case CFN_GOACC_DIM_SIZE:
1203 case CFN_GOACC_DIM_POS:
1204 /* Optimizing these two internal functions helps the loop
1205 optimizer eliminate outer comparisons. Size is [1,N]
1206 and pos is [0,N-1]. */
1208 bool is_pos = cfn == CFN_GOACC_DIM_POS;
1209 int axis = oacc_get_ifn_dim_arg (stmt);
1210 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1212 if (!size)
1213 /* If it's dynamic, the backend might know a hardware
1214 limitation. */
1215 size = targetm.goacc.dim_limit (axis);
1217 tree type = TREE_TYPE (gimple_call_lhs (stmt));
1218 vr->set(VR_RANGE, build_int_cst (type, is_pos ? 0 : 1),
1219 size
1220 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1222 return;
1223 case CFN_BUILT_IN_STRLEN:
1224 if (tree lhs = gimple_call_lhs (stmt))
1225 if (ptrdiff_type_node
1226 && (TYPE_PRECISION (ptrdiff_type_node)
1227 == TYPE_PRECISION (TREE_TYPE (lhs))))
1229 tree type = TREE_TYPE (lhs);
1230 tree max = vrp_val_max (ptrdiff_type_node);
1231 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1232 tree range_min = build_zero_cst (type);
1233 tree range_max = wide_int_to_tree (type, wmax - 1);
1234 vr->set (VR_RANGE, range_min, range_max);
1235 return;
1237 break;
1238 default:
1239 break;
1241 if (subcode != ERROR_MARK)
1243 bool saved_flag_wrapv = flag_wrapv;
1244 /* Pretend the arithmetics is wrapping. If there is
1245 any overflow, we'll complain, but will actually do
1246 wrapping operation. */
1247 flag_wrapv = 1;
1248 extract_range_from_binary_expr (vr, subcode, type,
1249 gimple_call_arg (stmt, 0),
1250 gimple_call_arg (stmt, 1));
1251 flag_wrapv = saved_flag_wrapv;
1253 /* If for both arguments vrp_valueize returned non-NULL,
1254 this should have been already folded and if not, it
1255 wasn't folded because of overflow. Avoid removing the
1256 UBSAN_CHECK_* calls in that case. */
1257 if (vr->kind () == VR_RANGE
1258 && (vr->min () == vr->max ()
1259 || operand_equal_p (vr->min (), vr->max (), 0)))
1260 vr->set_varying ();
1261 return;
1264 /* Handle extraction of the two results (result of arithmetics and
1265 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1266 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1267 else if (is_gimple_assign (stmt)
1268 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1269 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1270 && INTEGRAL_TYPE_P (type))
1272 enum tree_code code = gimple_assign_rhs_code (stmt);
1273 tree op = gimple_assign_rhs1 (stmt);
1274 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
1276 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
1277 if (is_gimple_call (g) && gimple_call_internal_p (g))
1279 enum tree_code subcode = ERROR_MARK;
1280 switch (gimple_call_internal_fn (g))
1282 case IFN_ADD_OVERFLOW:
1283 subcode = PLUS_EXPR;
1284 break;
1285 case IFN_SUB_OVERFLOW:
1286 subcode = MINUS_EXPR;
1287 break;
1288 case IFN_MUL_OVERFLOW:
1289 subcode = MULT_EXPR;
1290 break;
1291 case IFN_ATOMIC_COMPARE_EXCHANGE:
1292 if (code == IMAGPART_EXPR)
1294 /* This is the boolean return value whether compare and
1295 exchange changed anything or not. */
1296 vr->set (VR_RANGE, build_int_cst (type, 0),
1297 build_int_cst (type, 1));
1298 return;
1300 break;
1301 default:
1302 break;
1304 if (subcode != ERROR_MARK)
1306 tree op0 = gimple_call_arg (g, 0);
1307 tree op1 = gimple_call_arg (g, 1);
1308 if (code == IMAGPART_EXPR)
1310 bool ovf = false;
1311 if (check_for_binary_op_overflow (subcode, type,
1312 op0, op1, &ovf))
1313 vr->set (build_int_cst (type, ovf));
1314 else if (TYPE_PRECISION (type) == 1
1315 && !TYPE_UNSIGNED (type))
1316 vr->set_varying ();
1317 else
1318 vr->set (VR_RANGE, build_int_cst (type, 0),
1319 build_int_cst (type, 1));
1321 else if (types_compatible_p (type, TREE_TYPE (op0))
1322 && types_compatible_p (type, TREE_TYPE (op1)))
1324 bool saved_flag_wrapv = flag_wrapv;
1325 /* Pretend the arithmetics is wrapping. If there is
1326 any overflow, IMAGPART_EXPR will be set. */
1327 flag_wrapv = 1;
1328 extract_range_from_binary_expr (vr, subcode, type,
1329 op0, op1);
1330 flag_wrapv = saved_flag_wrapv;
1332 else
1334 value_range vr0, vr1;
1335 bool saved_flag_wrapv = flag_wrapv;
1336 /* Pretend the arithmetics is wrapping. If there is
1337 any overflow, IMAGPART_EXPR will be set. */
1338 flag_wrapv = 1;
1339 extract_range_from_unary_expr (&vr0, NOP_EXPR,
1340 type, op0);
1341 extract_range_from_unary_expr (&vr1, NOP_EXPR,
1342 type, op1);
1343 ::extract_range_from_binary_expr (vr, subcode, type,
1344 &vr0, &vr1);
1345 flag_wrapv = saved_flag_wrapv;
1347 return;
1352 if (INTEGRAL_TYPE_P (type)
1353 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
1354 set_value_range_to_nonnegative (vr, type);
1355 else if (vrp_stmt_computes_nonzero (stmt))
1356 vr->set_nonnull (type);
1357 else
1358 vr->set_varying ();
1362 /* Try to compute a useful range out of assignment STMT and store it
1363 in *VR. */
1365 void
1366 vr_values::extract_range_from_assignment (value_range *vr, gassign *stmt)
1368 enum tree_code code = gimple_assign_rhs_code (stmt);
1370 if (code == ASSERT_EXPR)
1371 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
1372 else if (code == SSA_NAME)
1373 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
1374 else if (TREE_CODE_CLASS (code) == tcc_binary)
1375 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
1376 gimple_expr_type (stmt),
1377 gimple_assign_rhs1 (stmt),
1378 gimple_assign_rhs2 (stmt));
1379 else if (TREE_CODE_CLASS (code) == tcc_unary)
1380 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
1381 gimple_expr_type (stmt),
1382 gimple_assign_rhs1 (stmt));
1383 else if (code == COND_EXPR)
1384 extract_range_from_cond_expr (vr, stmt);
1385 else if (TREE_CODE_CLASS (code) == tcc_comparison)
1386 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
1387 gimple_expr_type (stmt),
1388 gimple_assign_rhs1 (stmt),
1389 gimple_assign_rhs2 (stmt));
1390 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
1391 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1392 vr->set (gimple_assign_rhs1 (stmt));
1393 else
1394 vr->set_varying ();
1396 if (vr->varying_p ())
1397 extract_range_basic (vr, stmt);
1400 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1402 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1403 all the values in the ranges.
1405 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1407 - Return NULL_TREE if it is not always possible to determine the
1408 value of the comparison.
1410 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1411 assumed signed overflow is undefined. */
1414 static tree
1415 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
1416 bool *strict_overflow_p)
1418 /* VARYING or UNDEFINED ranges cannot be compared. */
1419 if (vr0->varying_p ()
1420 || vr0->undefined_p ()
1421 || vr1->varying_p ()
1422 || vr1->undefined_p ())
1423 return NULL_TREE;
1425 /* Anti-ranges need to be handled separately. */
1426 if (vr0->kind () == VR_ANTI_RANGE || vr1->kind () == VR_ANTI_RANGE)
1428 /* If both are anti-ranges, then we cannot compute any
1429 comparison. */
1430 if (vr0->kind () == VR_ANTI_RANGE && vr1->kind () == VR_ANTI_RANGE)
1431 return NULL_TREE;
1433 /* These comparisons are never statically computable. */
1434 if (comp == GT_EXPR
1435 || comp == GE_EXPR
1436 || comp == LT_EXPR
1437 || comp == LE_EXPR)
1438 return NULL_TREE;
1440 /* Equality can be computed only between a range and an
1441 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1442 if (vr0->kind () == VR_RANGE)
1444 /* To simplify processing, make VR0 the anti-range. */
1445 value_range *tmp = vr0;
1446 vr0 = vr1;
1447 vr1 = tmp;
1450 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
1452 if (compare_values_warnv (vr0->min (), vr1->min (), strict_overflow_p) == 0
1453 && compare_values_warnv (vr0->max (), vr1->max (), strict_overflow_p) == 0)
1454 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1456 return NULL_TREE;
1459 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1460 operands around and change the comparison code. */
1461 if (comp == GT_EXPR || comp == GE_EXPR)
1463 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
1464 std::swap (vr0, vr1);
1467 if (comp == EQ_EXPR)
1469 /* Equality may only be computed if both ranges represent
1470 exactly one value. */
1471 if (compare_values_warnv (vr0->min (), vr0->max (), strict_overflow_p) == 0
1472 && compare_values_warnv (vr1->min (), vr1->max (), strict_overflow_p) == 0)
1474 int cmp_min = compare_values_warnv (vr0->min (), vr1->min (),
1475 strict_overflow_p);
1476 int cmp_max = compare_values_warnv (vr0->max (), vr1->max (),
1477 strict_overflow_p);
1478 if (cmp_min == 0 && cmp_max == 0)
1479 return boolean_true_node;
1480 else if (cmp_min != -2 && cmp_max != -2)
1481 return boolean_false_node;
1483 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1484 else if (compare_values_warnv (vr0->min (), vr1->max (),
1485 strict_overflow_p) == 1
1486 || compare_values_warnv (vr1->min (), vr0->max (),
1487 strict_overflow_p) == 1)
1488 return boolean_false_node;
1490 return NULL_TREE;
1492 else if (comp == NE_EXPR)
1494 int cmp1, cmp2;
1496 /* If VR0 is completely to the left or completely to the right
1497 of VR1, they are always different. Notice that we need to
1498 make sure that both comparisons yield similar results to
1499 avoid comparing values that cannot be compared at
1500 compile-time. */
1501 cmp1 = compare_values_warnv (vr0->max (), vr1->min (), strict_overflow_p);
1502 cmp2 = compare_values_warnv (vr0->min (), vr1->max (), strict_overflow_p);
1503 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
1504 return boolean_true_node;
1506 /* If VR0 and VR1 represent a single value and are identical,
1507 return false. */
1508 else if (compare_values_warnv (vr0->min (), vr0->max (),
1509 strict_overflow_p) == 0
1510 && compare_values_warnv (vr1->min (), vr1->max (),
1511 strict_overflow_p) == 0
1512 && compare_values_warnv (vr0->min (), vr1->min (),
1513 strict_overflow_p) == 0
1514 && compare_values_warnv (vr0->max (), vr1->max (),
1515 strict_overflow_p) == 0)
1516 return boolean_false_node;
1518 /* Otherwise, they may or may not be different. */
1519 else
1520 return NULL_TREE;
1522 else if (comp == LT_EXPR || comp == LE_EXPR)
1524 int tst;
1526 /* If VR0 is to the left of VR1, return true. */
1527 tst = compare_values_warnv (vr0->max (), vr1->min (), strict_overflow_p);
1528 if ((comp == LT_EXPR && tst == -1)
1529 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1530 return boolean_true_node;
1532 /* If VR0 is to the right of VR1, return false. */
1533 tst = compare_values_warnv (vr0->min (), vr1->max (), strict_overflow_p);
1534 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1535 || (comp == LE_EXPR && tst == 1))
1536 return boolean_false_node;
1538 /* Otherwise, we don't know. */
1539 return NULL_TREE;
1542 gcc_unreachable ();
1545 /* Given a value range VR, a value VAL and a comparison code COMP, return
1546 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1547 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1548 always returns false. Return NULL_TREE if it is not always
1549 possible to determine the value of the comparison. Also set
1550 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1551 assumed signed overflow is undefined. */
1553 static tree
1554 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
1555 bool *strict_overflow_p)
1557 if (vr->varying_p () || vr->undefined_p ())
1558 return NULL_TREE;
1560 /* Anti-ranges need to be handled separately. */
1561 if (vr->kind () == VR_ANTI_RANGE)
1563 /* For anti-ranges, the only predicates that we can compute at
1564 compile time are equality and inequality. */
1565 if (comp == GT_EXPR
1566 || comp == GE_EXPR
1567 || comp == LT_EXPR
1568 || comp == LE_EXPR)
1569 return NULL_TREE;
1571 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1572 if (value_inside_range (val, vr->min (), vr->max ()) == 1)
1573 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1575 return NULL_TREE;
1578 if (comp == EQ_EXPR)
1580 /* EQ_EXPR may only be computed if VR represents exactly
1581 one value. */
1582 if (compare_values_warnv (vr->min (), vr->max (), strict_overflow_p) == 0)
1584 int cmp = compare_values_warnv (vr->min (), val, strict_overflow_p);
1585 if (cmp == 0)
1586 return boolean_true_node;
1587 else if (cmp == -1 || cmp == 1 || cmp == 2)
1588 return boolean_false_node;
1590 else if (compare_values_warnv (val, vr->min (), strict_overflow_p) == -1
1591 || compare_values_warnv (vr->max (), val, strict_overflow_p) == -1)
1592 return boolean_false_node;
1594 return NULL_TREE;
1596 else if (comp == NE_EXPR)
1598 /* If VAL is not inside VR, then they are always different. */
1599 if (compare_values_warnv (vr->max (), val, strict_overflow_p) == -1
1600 || compare_values_warnv (vr->min (), val, strict_overflow_p) == 1)
1601 return boolean_true_node;
1603 /* If VR represents exactly one value equal to VAL, then return
1604 false. */
1605 if (compare_values_warnv (vr->min (), vr->max (), strict_overflow_p) == 0
1606 && compare_values_warnv (vr->min (), val, strict_overflow_p) == 0)
1607 return boolean_false_node;
1609 /* Otherwise, they may or may not be different. */
1610 return NULL_TREE;
1612 else if (comp == LT_EXPR || comp == LE_EXPR)
1614 int tst;
1616 /* If VR is to the left of VAL, return true. */
1617 tst = compare_values_warnv (vr->max (), val, strict_overflow_p);
1618 if ((comp == LT_EXPR && tst == -1)
1619 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1620 return boolean_true_node;
1622 /* If VR is to the right of VAL, return false. */
1623 tst = compare_values_warnv (vr->min (), val, strict_overflow_p);
1624 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1625 || (comp == LE_EXPR && tst == 1))
1626 return boolean_false_node;
1628 /* Otherwise, we don't know. */
1629 return NULL_TREE;
1631 else if (comp == GT_EXPR || comp == GE_EXPR)
1633 int tst;
1635 /* If VR is to the right of VAL, return true. */
1636 tst = compare_values_warnv (vr->min (), val, strict_overflow_p);
1637 if ((comp == GT_EXPR && tst == 1)
1638 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
1639 return boolean_true_node;
1641 /* If VR is to the left of VAL, return false. */
1642 tst = compare_values_warnv (vr->max (), val, strict_overflow_p);
1643 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
1644 || (comp == GE_EXPR && tst == -1))
1645 return boolean_false_node;
1647 /* Otherwise, we don't know. */
1648 return NULL_TREE;
1651 gcc_unreachable ();
1653 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1654 would be profitable to adjust VR using scalar evolution information
1655 for VAR. If so, update VR with the new limits. */
1657 void
1658 vr_values::adjust_range_with_scev (value_range *vr, struct loop *loop,
1659 gimple *stmt, tree var)
1661 tree init, step, chrec, tmin, tmax, min, max, type, tem;
1662 enum ev_direction dir;
1664 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1665 better opportunities than a regular range, but I'm not sure. */
1666 if (vr->kind () == VR_ANTI_RANGE)
1667 return;
1669 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
1671 /* Like in PR19590, scev can return a constant function. */
1672 if (is_gimple_min_invariant (chrec))
1674 vr->set (chrec);
1675 return;
1678 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1679 return;
1681 init = initial_condition_in_loop_num (chrec, loop->num);
1682 tem = op_with_constant_singleton_value_range (init);
1683 if (tem)
1684 init = tem;
1685 step = evolution_part_in_loop_num (chrec, loop->num);
1686 tem = op_with_constant_singleton_value_range (step);
1687 if (tem)
1688 step = tem;
1690 /* If STEP is symbolic, we can't know whether INIT will be the
1691 minimum or maximum value in the range. Also, unless INIT is
1692 a simple expression, compare_values and possibly other functions
1693 in tree-vrp won't be able to handle it. */
1694 if (step == NULL_TREE
1695 || !is_gimple_min_invariant (step)
1696 || !valid_value_p (init))
1697 return;
1699 dir = scev_direction (chrec);
1700 if (/* Do not adjust ranges if we do not know whether the iv increases
1701 or decreases, ... */
1702 dir == EV_DIR_UNKNOWN
1703 /* ... or if it may wrap. */
1704 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
1705 get_chrec_loop (chrec), true))
1706 return;
1708 type = TREE_TYPE (var);
1709 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1710 tmin = lower_bound_in_type (type, type);
1711 else
1712 tmin = TYPE_MIN_VALUE (type);
1713 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1714 tmax = upper_bound_in_type (type, type);
1715 else
1716 tmax = TYPE_MAX_VALUE (type);
1718 /* Try to use estimated number of iterations for the loop to constrain the
1719 final value in the evolution. */
1720 if (TREE_CODE (step) == INTEGER_CST
1721 && is_gimple_val (init)
1722 && (TREE_CODE (init) != SSA_NAME
1723 || get_value_range (init)->kind () == VR_RANGE))
1725 widest_int nit;
1727 /* We are only entering here for loop header PHI nodes, so using
1728 the number of latch executions is the correct thing to use. */
1729 if (max_loop_iterations (loop, &nit))
1731 value_range maxvr;
1732 signop sgn = TYPE_SIGN (TREE_TYPE (step));
1733 wi::overflow_type overflow;
1735 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
1736 &overflow);
1737 /* If the multiplication overflowed we can't do a meaningful
1738 adjustment. Likewise if the result doesn't fit in the type
1739 of the induction variable. For a signed type we have to
1740 check whether the result has the expected signedness which
1741 is that of the step as number of iterations is unsigned. */
1742 if (!overflow
1743 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
1744 && (sgn == UNSIGNED
1745 || wi::gts_p (wtmp, 0) == wi::gts_p (wi::to_wide (step), 0)))
1747 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
1748 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
1749 TREE_TYPE (init), init, tem);
1750 /* Likewise if the addition did. */
1751 if (maxvr.kind () == VR_RANGE)
1753 value_range_base initvr;
1755 if (TREE_CODE (init) == SSA_NAME)
1756 initvr = *(get_value_range (init));
1757 else if (is_gimple_min_invariant (init))
1758 initvr.set (init);
1759 else
1760 return;
1762 /* Check if init + nit * step overflows. Though we checked
1763 scev {init, step}_loop doesn't wrap, it is not enough
1764 because the loop may exit immediately. Overflow could
1765 happen in the plus expression in this case. */
1766 if ((dir == EV_DIR_DECREASES
1767 && compare_values (maxvr.min (), initvr.min ()) != -1)
1768 || (dir == EV_DIR_GROWS
1769 && compare_values (maxvr.max (), initvr.max ()) != 1))
1770 return;
1772 tmin = maxvr.min ();
1773 tmax = maxvr.max ();
1779 if (vr->varying_p () || vr->undefined_p ())
1781 min = tmin;
1782 max = tmax;
1784 /* For VARYING or UNDEFINED ranges, just about anything we get
1785 from scalar evolutions should be better. */
1787 if (dir == EV_DIR_DECREASES)
1788 max = init;
1789 else
1790 min = init;
1792 else if (vr->kind () == VR_RANGE)
1794 min = vr->min ();
1795 max = vr->max ();
1797 if (dir == EV_DIR_DECREASES)
1799 /* INIT is the maximum value. If INIT is lower than VR->MAX ()
1800 but no smaller than VR->MIN (), set VR->MAX () to INIT. */
1801 if (compare_values (init, max) == -1)
1802 max = init;
1804 /* According to the loop information, the variable does not
1805 overflow. */
1806 if (compare_values (min, tmin) == -1)
1807 min = tmin;
1810 else
1812 /* If INIT is bigger than VR->MIN (), set VR->MIN () to INIT. */
1813 if (compare_values (init, min) == 1)
1814 min = init;
1816 if (compare_values (tmax, max) == -1)
1817 max = tmax;
1820 else
1821 return;
1823 /* If we just created an invalid range with the minimum
1824 greater than the maximum, we fail conservatively.
1825 This should happen only in unreachable
1826 parts of code, or for invalid programs. */
1827 if (compare_values (min, max) == 1)
1828 return;
1830 /* Even for valid range info, sometimes overflow flag will leak in.
1831 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1832 drop them. */
1833 if (TREE_OVERFLOW_P (min))
1834 min = drop_tree_overflow (min);
1835 if (TREE_OVERFLOW_P (max))
1836 max = drop_tree_overflow (max);
1838 vr->update (VR_RANGE, min, max);
1841 /* Dump value ranges of all SSA_NAMEs to FILE. */
1843 void
1844 vr_values::dump_all_value_ranges (FILE *file)
1846 size_t i;
1848 for (i = 0; i < num_vr_values; i++)
1850 if (vr_value[i])
1852 print_generic_expr (file, ssa_name (i));
1853 fprintf (file, ": ");
1854 dump_value_range (file, vr_value[i]);
1855 fprintf (file, "\n");
1859 fprintf (file, "\n");
1862 /* Initialize VRP lattice. */
1864 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1866 values_propagated = false;
1867 num_vr_values = num_ssa_names;
1868 vr_value = XCNEWVEC (value_range *, num_vr_values);
1869 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
1870 bitmap_obstack_initialize (&vrp_equiv_obstack);
1871 to_remove_edges = vNULL;
1872 to_update_switch_stmts = vNULL;
1875 /* Free VRP lattice. */
1877 vr_values::~vr_values ()
1879 /* Free allocated memory. */
1880 free (vr_value);
1881 free (vr_phi_edge_counts);
1882 bitmap_obstack_release (&vrp_equiv_obstack);
1883 vrp_value_range_pool.release ();
1885 /* So that we can distinguish between VRP data being available
1886 and not available. */
1887 vr_value = NULL;
1888 vr_phi_edge_counts = NULL;
1890 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1891 then an EVRP client did not clean up properly. Catch it now rather
1892 than seeing something more obscure later. */
1893 gcc_assert (to_remove_edges.is_empty ()
1894 && to_update_switch_stmts.is_empty ());
1898 /* A hack. */
1899 static class vr_values *x_vr_values;
1901 /* Return the singleton value-range for NAME or NAME. */
1903 static inline tree
1904 vrp_valueize (tree name)
1906 if (TREE_CODE (name) == SSA_NAME)
1908 value_range *vr = x_vr_values->get_value_range (name);
1909 if (vr->kind () == VR_RANGE
1910 && (TREE_CODE (vr->min ()) == SSA_NAME
1911 || is_gimple_min_invariant (vr->min ()))
1912 && vrp_operand_equal_p (vr->min (), vr->max ()))
1913 return vr->min ();
1915 return name;
1918 /* Return the singleton value-range for NAME if that is a constant
1919 but signal to not follow SSA edges. */
1921 static inline tree
1922 vrp_valueize_1 (tree name)
1924 if (TREE_CODE (name) == SSA_NAME)
1926 /* If the definition may be simulated again we cannot follow
1927 this SSA edge as the SSA propagator does not necessarily
1928 re-visit the use. */
1929 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1930 if (!gimple_nop_p (def_stmt)
1931 && prop_simulate_again_p (def_stmt))
1932 return NULL_TREE;
1933 value_range *vr = x_vr_values->get_value_range (name);
1934 tree singleton;
1935 if (vr->singleton_p (&singleton))
1936 return singleton;
1938 return name;
1941 /* Given STMT, an assignment or call, return its LHS if the type
1942 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
1944 tree
1945 get_output_for_vrp (gimple *stmt)
1947 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
1948 return NULL_TREE;
1950 /* We only keep track of ranges in integral and pointer types. */
1951 tree lhs = gimple_get_lhs (stmt);
1952 if (TREE_CODE (lhs) == SSA_NAME
1953 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1954 /* It is valid to have NULL MIN/MAX values on a type. See
1955 build_range_type. */
1956 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
1957 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
1958 || POINTER_TYPE_P (TREE_TYPE (lhs))))
1959 return lhs;
1961 return NULL_TREE;
1964 /* Visit assignment STMT. If it produces an interesting range, record
1965 the range in VR and set LHS to OUTPUT_P. */
1967 void
1968 vr_values::vrp_visit_assignment_or_call (gimple *stmt, tree *output_p,
1969 value_range *vr)
1971 tree lhs = get_output_for_vrp (stmt);
1972 *output_p = lhs;
1974 /* We only keep track of ranges in integral and pointer types. */
1975 if (lhs)
1977 enum gimple_code code = gimple_code (stmt);
1979 /* Try folding the statement to a constant first. */
1980 x_vr_values = this;
1981 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
1982 vrp_valueize_1);
1983 x_vr_values = NULL;
1984 if (tem)
1986 if (TREE_CODE (tem) == SSA_NAME
1987 && (SSA_NAME_IS_DEFAULT_DEF (tem)
1988 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
1990 extract_range_from_ssa_name (vr, tem);
1991 return;
1993 else if (is_gimple_min_invariant (tem))
1995 vr->set (tem);
1996 return;
1999 /* Then dispatch to value-range extracting functions. */
2000 if (code == GIMPLE_CALL)
2001 extract_range_basic (vr, stmt);
2002 else
2003 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
2007 /* Helper that gets the value range of the SSA_NAME with version I
2008 or a symbolic range containing the SSA_NAME only if the value range
2009 is varying or undefined. Uses TEM as storage for the alternate range. */
2011 value_range *
2012 vr_values::get_vr_for_comparison (int i, value_range *tem)
2014 /* Shallow-copy equiv bitmap. */
2015 value_range *vr = get_value_range (ssa_name (i));
2017 /* If name N_i does not have a valid range, use N_i as its own
2018 range. This allows us to compare against names that may
2019 have N_i in their ranges. */
2020 if (vr->varying_p () || vr->undefined_p ())
2022 tem->set (ssa_name (i));
2023 return tem;
2026 return vr;
2029 /* Compare all the value ranges for names equivalent to VAR with VAL
2030 using comparison code COMP. Return the same value returned by
2031 compare_range_with_value, including the setting of
2032 *STRICT_OVERFLOW_P. */
2034 tree
2035 vr_values::compare_name_with_value (enum tree_code comp, tree var, tree val,
2036 bool *strict_overflow_p, bool use_equiv_p)
2038 bitmap_iterator bi;
2039 unsigned i;
2040 bitmap e;
2041 tree retval, t;
2042 int used_strict_overflow;
2043 bool sop;
2044 value_range *equiv_vr, tem_vr;
2046 /* Get the set of equivalences for VAR. */
2047 e = get_value_range (var)->equiv ();
2049 /* Start at -1. Set it to 0 if we do a comparison without relying
2050 on overflow, or 1 if all comparisons rely on overflow. */
2051 used_strict_overflow = -1;
2053 /* Compare vars' value range with val. */
2054 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var), &tem_vr);
2055 sop = false;
2056 retval = compare_range_with_value (comp, equiv_vr, val, &sop);
2057 if (retval)
2058 used_strict_overflow = sop ? 1 : 0;
2060 /* If the equiv set is empty we have done all work we need to do. */
2061 if (e == NULL)
2063 if (retval
2064 && used_strict_overflow > 0)
2065 *strict_overflow_p = true;
2066 return retval;
2069 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
2071 tree name = ssa_name (i);
2072 if (! name)
2073 continue;
2075 if (! use_equiv_p
2076 && ! SSA_NAME_IS_DEFAULT_DEF (name)
2077 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
2078 continue;
2080 equiv_vr = get_vr_for_comparison (i, &tem_vr);
2081 sop = false;
2082 t = compare_range_with_value (comp, equiv_vr, val, &sop);
2083 if (t)
2085 /* If we get different answers from different members
2086 of the equivalence set this check must be in a dead
2087 code region. Folding it to a trap representation
2088 would be correct here. For now just return don't-know. */
2089 if (retval != NULL
2090 && t != retval)
2092 retval = NULL_TREE;
2093 break;
2095 retval = t;
2097 if (!sop)
2098 used_strict_overflow = 0;
2099 else if (used_strict_overflow < 0)
2100 used_strict_overflow = 1;
2104 if (retval
2105 && used_strict_overflow > 0)
2106 *strict_overflow_p = true;
2108 return retval;
2112 /* Given a comparison code COMP and names N1 and N2, compare all the
2113 ranges equivalent to N1 against all the ranges equivalent to N2
2114 to determine the value of N1 COMP N2. Return the same value
2115 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2116 whether we relied on undefined signed overflow in the comparison. */
2119 tree
2120 vr_values::compare_names (enum tree_code comp, tree n1, tree n2,
2121 bool *strict_overflow_p)
2123 tree t, retval;
2124 bitmap e1, e2;
2125 bitmap_iterator bi1, bi2;
2126 unsigned i1, i2;
2127 int used_strict_overflow;
2128 static bitmap_obstack *s_obstack = NULL;
2129 static bitmap s_e1 = NULL, s_e2 = NULL;
2131 /* Compare the ranges of every name equivalent to N1 against the
2132 ranges of every name equivalent to N2. */
2133 e1 = get_value_range (n1)->equiv ();
2134 e2 = get_value_range (n2)->equiv ();
2136 /* Use the fake bitmaps if e1 or e2 are not available. */
2137 if (s_obstack == NULL)
2139 s_obstack = XNEW (bitmap_obstack);
2140 bitmap_obstack_initialize (s_obstack);
2141 s_e1 = BITMAP_ALLOC (s_obstack);
2142 s_e2 = BITMAP_ALLOC (s_obstack);
2144 if (e1 == NULL)
2145 e1 = s_e1;
2146 if (e2 == NULL)
2147 e2 = s_e2;
2149 /* Add N1 and N2 to their own set of equivalences to avoid
2150 duplicating the body of the loop just to check N1 and N2
2151 ranges. */
2152 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
2153 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
2155 /* If the equivalence sets have a common intersection, then the two
2156 names can be compared without checking their ranges. */
2157 if (bitmap_intersect_p (e1, e2))
2159 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2160 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2162 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
2163 ? boolean_true_node
2164 : boolean_false_node;
2167 /* Start at -1. Set it to 0 if we do a comparison without relying
2168 on overflow, or 1 if all comparisons rely on overflow. */
2169 used_strict_overflow = -1;
2171 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2172 N2 to their own set of equivalences to avoid duplicating the body
2173 of the loop just to check N1 and N2 ranges. */
2174 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
2176 if (! ssa_name (i1))
2177 continue;
2179 value_range tem_vr1;
2180 value_range *vr1 = get_vr_for_comparison (i1, &tem_vr1);
2182 t = retval = NULL_TREE;
2183 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
2185 if (! ssa_name (i2))
2186 continue;
2188 bool sop = false;
2190 value_range tem_vr2;
2191 value_range *vr2 = get_vr_for_comparison (i2, &tem_vr2);
2193 t = compare_ranges (comp, vr1, vr2, &sop);
2194 if (t)
2196 /* If we get different answers from different members
2197 of the equivalence set this check must be in a dead
2198 code region. Folding it to a trap representation
2199 would be correct here. For now just return don't-know. */
2200 if (retval != NULL
2201 && t != retval)
2203 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2204 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2205 return NULL_TREE;
2207 retval = t;
2209 if (!sop)
2210 used_strict_overflow = 0;
2211 else if (used_strict_overflow < 0)
2212 used_strict_overflow = 1;
2216 if (retval)
2218 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2219 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2220 if (used_strict_overflow > 0)
2221 *strict_overflow_p = true;
2222 return retval;
2226 /* None of the equivalent ranges are useful in computing this
2227 comparison. */
2228 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2229 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2230 return NULL_TREE;
2233 /* Helper function for vrp_evaluate_conditional_warnv & other
2234 optimizers. */
2236 tree
2237 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2238 (enum tree_code code, tree op0, tree op1, bool * strict_overflow_p)
2240 value_range *vr0, *vr1;
2242 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
2243 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
2245 tree res = NULL_TREE;
2246 if (vr0 && vr1)
2247 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
2248 if (!res && vr0)
2249 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
2250 if (!res && vr1)
2251 res = (compare_range_with_value
2252 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
2253 return res;
2256 /* Helper function for vrp_evaluate_conditional_warnv. */
2258 tree
2259 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code,
2260 tree op0, tree op1,
2261 bool use_equiv_p,
2262 bool *strict_overflow_p,
2263 bool *only_ranges)
2265 tree ret;
2266 if (only_ranges)
2267 *only_ranges = true;
2269 /* We only deal with integral and pointer types. */
2270 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2271 && !POINTER_TYPE_P (TREE_TYPE (op0)))
2272 return NULL_TREE;
2274 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2275 as a simple equality test, then prefer that over its current form
2276 for evaluation.
2278 An overflow test which collapses to an equality test can always be
2279 expressed as a comparison of one argument against zero. Overflow
2280 occurs when the chosen argument is zero and does not occur if the
2281 chosen argument is not zero. */
2282 tree x;
2283 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
2285 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
2286 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2287 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2288 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2289 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2290 if (integer_zerop (x))
2292 op1 = x;
2293 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
2295 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2296 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2297 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2298 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2299 else if (wi::to_wide (x) == max - 1)
2301 op0 = op1;
2302 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
2303 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
2307 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
2308 (code, op0, op1, strict_overflow_p)))
2309 return ret;
2310 if (only_ranges)
2311 *only_ranges = false;
2312 /* Do not use compare_names during propagation, it's quadratic. */
2313 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
2314 && use_equiv_p)
2315 return compare_names (code, op0, op1, strict_overflow_p);
2316 else if (TREE_CODE (op0) == SSA_NAME)
2317 return compare_name_with_value (code, op0, op1,
2318 strict_overflow_p, use_equiv_p);
2319 else if (TREE_CODE (op1) == SSA_NAME)
2320 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
2321 strict_overflow_p, use_equiv_p);
2322 return NULL_TREE;
2325 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2326 information. Return NULL if the conditional can not be evaluated.
2327 The ranges of all the names equivalent with the operands in COND
2328 will be used when trying to compute the value. If the result is
2329 based on undefined signed overflow, issue a warning if
2330 appropriate. */
2332 tree
2333 vr_values::vrp_evaluate_conditional (tree_code code, tree op0,
2334 tree op1, gimple *stmt)
2336 bool sop;
2337 tree ret;
2338 bool only_ranges;
2340 /* Some passes and foldings leak constants with overflow flag set
2341 into the IL. Avoid doing wrong things with these and bail out. */
2342 if ((TREE_CODE (op0) == INTEGER_CST
2343 && TREE_OVERFLOW (op0))
2344 || (TREE_CODE (op1) == INTEGER_CST
2345 && TREE_OVERFLOW (op1)))
2346 return NULL_TREE;
2348 sop = false;
2349 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
2350 &only_ranges);
2352 if (ret && sop)
2354 enum warn_strict_overflow_code wc;
2355 const char* warnmsg;
2357 if (is_gimple_min_invariant (ret))
2359 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
2360 warnmsg = G_("assuming signed overflow does not occur when "
2361 "simplifying conditional to constant");
2363 else
2365 wc = WARN_STRICT_OVERFLOW_COMPARISON;
2366 warnmsg = G_("assuming signed overflow does not occur when "
2367 "simplifying conditional");
2370 if (issue_strict_overflow_warning (wc))
2372 location_t location;
2374 if (!gimple_has_location (stmt))
2375 location = input_location;
2376 else
2377 location = gimple_location (stmt);
2378 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
2382 if (warn_type_limits
2383 && ret && only_ranges
2384 && TREE_CODE_CLASS (code) == tcc_comparison
2385 && TREE_CODE (op0) == SSA_NAME)
2387 /* If the comparison is being folded and the operand on the LHS
2388 is being compared against a constant value that is outside of
2389 the natural range of OP0's type, then the predicate will
2390 always fold regardless of the value of OP0. If -Wtype-limits
2391 was specified, emit a warning. */
2392 tree type = TREE_TYPE (op0);
2393 value_range *vr0 = get_value_range (op0);
2395 if (vr0->kind () == VR_RANGE
2396 && INTEGRAL_TYPE_P (type)
2397 && vrp_val_is_min (vr0->min ())
2398 && vrp_val_is_max (vr0->max ())
2399 && is_gimple_min_invariant (op1))
2401 location_t location;
2403 if (!gimple_has_location (stmt))
2404 location = input_location;
2405 else
2406 location = gimple_location (stmt);
2408 warning_at (location, OPT_Wtype_limits,
2409 integer_zerop (ret)
2410 ? G_("comparison always false "
2411 "due to limited range of data type")
2412 : G_("comparison always true "
2413 "due to limited range of data type"));
2417 return ret;
2421 /* Visit conditional statement STMT. If we can determine which edge
2422 will be taken out of STMT's basic block, record it in
2423 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2425 void
2426 vr_values::vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
2428 tree val;
2430 *taken_edge_p = NULL;
2432 if (dump_file && (dump_flags & TDF_DETAILS))
2434 tree use;
2435 ssa_op_iter i;
2437 fprintf (dump_file, "\nVisiting conditional with predicate: ");
2438 print_gimple_stmt (dump_file, stmt, 0);
2439 fprintf (dump_file, "\nWith known ranges\n");
2441 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
2443 fprintf (dump_file, "\t");
2444 print_generic_expr (dump_file, use);
2445 fprintf (dump_file, ": ");
2446 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
2449 fprintf (dump_file, "\n");
2452 /* Compute the value of the predicate COND by checking the known
2453 ranges of each of its operands.
2455 Note that we cannot evaluate all the equivalent ranges here
2456 because those ranges may not yet be final and with the current
2457 propagation strategy, we cannot determine when the value ranges
2458 of the names in the equivalence set have changed.
2460 For instance, given the following code fragment
2462 i_5 = PHI <8, i_13>
2464 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2465 if (i_14 == 1)
2468 Assume that on the first visit to i_14, i_5 has the temporary
2469 range [8, 8] because the second argument to the PHI function is
2470 not yet executable. We derive the range ~[0, 0] for i_14 and the
2471 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2472 the first time, since i_14 is equivalent to the range [8, 8], we
2473 determine that the predicate is always false.
2475 On the next round of propagation, i_13 is determined to be
2476 VARYING, which causes i_5 to drop down to VARYING. So, another
2477 visit to i_14 is scheduled. In this second visit, we compute the
2478 exact same range and equivalence set for i_14, namely ~[0, 0] and
2479 { i_5 }. But we did not have the previous range for i_5
2480 registered, so vrp_visit_assignment thinks that the range for
2481 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2482 is not visited again, which stops propagation from visiting
2483 statements in the THEN clause of that if().
2485 To properly fix this we would need to keep the previous range
2486 value for the names in the equivalence set. This way we would've
2487 discovered that from one visit to the other i_5 changed from
2488 range [8, 8] to VR_VARYING.
2490 However, fixing this apparent limitation may not be worth the
2491 additional checking. Testing on several code bases (GCC, DLV,
2492 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2493 4 more predicates folded in SPEC. */
2495 bool sop;
2496 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
2497 gimple_cond_lhs (stmt),
2498 gimple_cond_rhs (stmt),
2499 false, &sop, NULL);
2500 if (val)
2501 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
2503 if (dump_file && (dump_flags & TDF_DETAILS))
2505 fprintf (dump_file, "\nPredicate evaluates to: ");
2506 if (val == NULL_TREE)
2507 fprintf (dump_file, "DON'T KNOW\n");
2508 else
2509 print_generic_stmt (dump_file, val);
2513 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2514 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2515 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2516 Returns true if the default label is not needed. */
2518 static bool
2519 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
2520 size_t *max_idx1, size_t *min_idx2,
2521 size_t *max_idx2)
2523 size_t i, j, k, l;
2524 unsigned int n = gimple_switch_num_labels (stmt);
2525 bool take_default;
2526 tree case_low, case_high;
2527 tree min = vr->min (), max = vr->max ();
2529 gcc_checking_assert (!vr->varying_p () && !vr->undefined_p ());
2531 take_default = !find_case_label_range (stmt, min, max, &i, &j);
2533 /* Set second range to emtpy. */
2534 *min_idx2 = 1;
2535 *max_idx2 = 0;
2537 if (vr->kind () == VR_RANGE)
2539 *min_idx1 = i;
2540 *max_idx1 = j;
2541 return !take_default;
2544 /* Set first range to all case labels. */
2545 *min_idx1 = 1;
2546 *max_idx1 = n - 1;
2548 if (i > j)
2549 return false;
2551 /* Make sure all the values of case labels [i , j] are contained in
2552 range [MIN, MAX]. */
2553 case_low = CASE_LOW (gimple_switch_label (stmt, i));
2554 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
2555 if (tree_int_cst_compare (case_low, min) < 0)
2556 i += 1;
2557 if (case_high != NULL_TREE
2558 && tree_int_cst_compare (max, case_high) < 0)
2559 j -= 1;
2561 if (i > j)
2562 return false;
2564 /* If the range spans case labels [i, j], the corresponding anti-range spans
2565 the labels [1, i - 1] and [j + 1, n - 1]. */
2566 k = j + 1;
2567 l = n - 1;
2568 if (k > l)
2570 k = 1;
2571 l = 0;
2574 j = i - 1;
2575 i = 1;
2576 if (i > j)
2578 i = k;
2579 j = l;
2580 k = 1;
2581 l = 0;
2584 *min_idx1 = i;
2585 *max_idx1 = j;
2586 *min_idx2 = k;
2587 *max_idx2 = l;
2588 return false;
2591 /* Visit switch statement STMT. If we can determine which edge
2592 will be taken out of STMT's basic block, record it in
2593 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2595 void
2596 vr_values::vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
2598 tree op, val;
2599 value_range *vr;
2600 size_t i = 0, j = 0, k, l;
2601 bool take_default;
2603 *taken_edge_p = NULL;
2604 op = gimple_switch_index (stmt);
2605 if (TREE_CODE (op) != SSA_NAME)
2606 return;
2608 vr = get_value_range (op);
2609 if (dump_file && (dump_flags & TDF_DETAILS))
2611 fprintf (dump_file, "\nVisiting switch expression with operand ");
2612 print_generic_expr (dump_file, op);
2613 fprintf (dump_file, " with known range ");
2614 dump_value_range (dump_file, vr);
2615 fprintf (dump_file, "\n");
2618 if (vr->undefined_p ()
2619 || vr->varying_p ()
2620 || vr->symbolic_p ())
2621 return;
2623 /* Find the single edge that is taken from the switch expression. */
2624 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
2626 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2627 label */
2628 if (j < i)
2630 gcc_assert (take_default);
2631 val = gimple_switch_default_label (stmt);
2633 else
2635 /* Check if labels with index i to j and maybe the default label
2636 are all reaching the same label. */
2638 val = gimple_switch_label (stmt, i);
2639 if (take_default
2640 && CASE_LABEL (gimple_switch_default_label (stmt))
2641 != CASE_LABEL (val))
2643 if (dump_file && (dump_flags & TDF_DETAILS))
2644 fprintf (dump_file, " not a single destination for this "
2645 "range\n");
2646 return;
2648 for (++i; i <= j; ++i)
2650 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
2652 if (dump_file && (dump_flags & TDF_DETAILS))
2653 fprintf (dump_file, " not a single destination for this "
2654 "range\n");
2655 return;
2658 for (; k <= l; ++k)
2660 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
2662 if (dump_file && (dump_flags & TDF_DETAILS))
2663 fprintf (dump_file, " not a single destination for this "
2664 "range\n");
2665 return;
2670 *taken_edge_p = find_edge (gimple_bb (stmt),
2671 label_to_block (cfun, CASE_LABEL (val)));
2673 if (dump_file && (dump_flags & TDF_DETAILS))
2675 fprintf (dump_file, " will take edge to ");
2676 print_generic_stmt (dump_file, CASE_LABEL (val));
2681 /* Evaluate statement STMT. If the statement produces a useful range,
2682 set VR and corepsponding OUTPUT_P.
2684 If STMT is a conditional branch and we can determine its truth
2685 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2687 void
2688 vr_values::extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
2689 tree *output_p, value_range *vr)
2692 if (dump_file && (dump_flags & TDF_DETAILS))
2694 fprintf (dump_file, "\nVisiting statement:\n");
2695 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2698 if (!stmt_interesting_for_vrp (stmt))
2699 gcc_assert (stmt_ends_bb_p (stmt));
2700 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
2701 vrp_visit_assignment_or_call (stmt, output_p, vr);
2702 else if (gimple_code (stmt) == GIMPLE_COND)
2703 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
2704 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2705 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
2708 /* Visit all arguments for PHI node PHI that flow through executable
2709 edges. If a valid value range can be derived from all the incoming
2710 value ranges, set a new range in VR_RESULT. */
2712 void
2713 vr_values::extract_range_from_phi_node (gphi *phi, value_range *vr_result)
2715 size_t i;
2716 tree lhs = PHI_RESULT (phi);
2717 value_range *lhs_vr = get_value_range (lhs);
2718 bool first = true;
2719 int edges, old_edges;
2720 struct loop *l;
2722 if (dump_file && (dump_flags & TDF_DETAILS))
2724 fprintf (dump_file, "\nVisiting PHI node: ");
2725 print_gimple_stmt (dump_file, phi, 0, dump_flags);
2728 bool may_simulate_backedge_again = false;
2729 edges = 0;
2730 for (i = 0; i < gimple_phi_num_args (phi); i++)
2732 edge e = gimple_phi_arg_edge (phi, i);
2734 if (dump_file && (dump_flags & TDF_DETAILS))
2736 fprintf (dump_file,
2737 " Argument #%d (%d -> %d %sexecutable)\n",
2738 (int) i, e->src->index, e->dest->index,
2739 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
2742 if (e->flags & EDGE_EXECUTABLE)
2744 tree arg = PHI_ARG_DEF (phi, i);
2745 value_range vr_arg_tem;
2746 value_range *vr_arg = &vr_arg_tem;
2748 ++edges;
2750 if (TREE_CODE (arg) == SSA_NAME)
2752 /* See if we are eventually going to change one of the args. */
2753 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
2754 if (! gimple_nop_p (def_stmt)
2755 && prop_simulate_again_p (def_stmt)
2756 && e->flags & EDGE_DFS_BACK)
2757 may_simulate_backedge_again = true;
2759 value_range *vr_arg_ = get_value_range (arg);
2760 /* Do not allow equivalences or symbolic ranges to leak in from
2761 backedges. That creates invalid equivalencies.
2762 See PR53465 and PR54767. */
2763 if (e->flags & EDGE_DFS_BACK)
2765 if (!vr_arg_->varying_p () && !vr_arg_->undefined_p ())
2767 vr_arg_tem.set (vr_arg_->kind (), vr_arg_->min (),
2768 vr_arg_->max (), NULL);
2769 if (vr_arg_tem.symbolic_p ())
2770 vr_arg_tem.set_varying ();
2772 else
2773 vr_arg = vr_arg_;
2775 /* If the non-backedge arguments range is VR_VARYING then
2776 we can still try recording a simple equivalence. */
2777 else if (vr_arg_->varying_p ())
2778 vr_arg_tem.set (arg);
2779 else
2780 vr_arg = vr_arg_;
2782 else
2784 if (TREE_OVERFLOW_P (arg))
2785 arg = drop_tree_overflow (arg);
2787 vr_arg_tem.set (arg);
2790 if (dump_file && (dump_flags & TDF_DETAILS))
2792 fprintf (dump_file, "\t");
2793 print_generic_expr (dump_file, arg, dump_flags);
2794 fprintf (dump_file, ": ");
2795 dump_value_range (dump_file, vr_arg);
2796 fprintf (dump_file, "\n");
2799 if (first)
2800 vr_result->deep_copy (vr_arg);
2801 else
2802 vr_result->union_ (vr_arg);
2803 first = false;
2805 if (vr_result->varying_p ())
2806 break;
2810 if (vr_result->varying_p ())
2811 goto varying;
2812 else if (vr_result->undefined_p ())
2813 goto update_range;
2815 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
2816 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
2818 /* To prevent infinite iterations in the algorithm, derive ranges
2819 when the new value is slightly bigger or smaller than the
2820 previous one. We don't do this if we have seen a new executable
2821 edge; this helps us avoid an infinity for conditionals
2822 which are not in a loop. If the old value-range was VR_UNDEFINED
2823 use the updated range and iterate one more time. If we will not
2824 simulate this PHI again via the backedge allow us to iterate. */
2825 if (edges > 0
2826 && gimple_phi_num_args (phi) > 1
2827 && edges == old_edges
2828 && !lhs_vr->undefined_p ()
2829 && may_simulate_backedge_again)
2831 /* Compare old and new ranges, fall back to varying if the
2832 values are not comparable. */
2833 int cmp_min = compare_values (lhs_vr->min (), vr_result->min ());
2834 if (cmp_min == -2)
2835 goto varying;
2836 int cmp_max = compare_values (lhs_vr->max (), vr_result->max ());
2837 if (cmp_max == -2)
2838 goto varying;
2840 /* For non VR_RANGE or for pointers fall back to varying if
2841 the range changed. */
2842 if ((lhs_vr->kind () != VR_RANGE || vr_result->kind () != VR_RANGE
2843 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2844 && (cmp_min != 0 || cmp_max != 0))
2845 goto varying;
2847 /* If the new minimum is larger than the previous one
2848 retain the old value. If the new minimum value is smaller
2849 than the previous one and not -INF go all the way to -INF + 1.
2850 In the first case, to avoid infinite bouncing between different
2851 minimums, and in the other case to avoid iterating millions of
2852 times to reach -INF. Going to -INF + 1 also lets the following
2853 iteration compute whether there will be any overflow, at the
2854 expense of one additional iteration. */
2855 tree new_min = vr_result->min ();
2856 tree new_max = vr_result->max ();
2857 if (cmp_min < 0)
2858 new_min = lhs_vr->min ();
2859 else if (cmp_min > 0
2860 && !vrp_val_is_min (vr_result->min ()))
2861 new_min = int_const_binop (PLUS_EXPR,
2862 vrp_val_min (vr_result->type ()),
2863 build_int_cst (vr_result->type (), 1));
2865 /* Similarly for the maximum value. */
2866 if (cmp_max > 0)
2867 new_max = lhs_vr->max ();
2868 else if (cmp_max < 0
2869 && !vrp_val_is_max (vr_result->max ()))
2870 new_max = int_const_binop (MINUS_EXPR,
2871 vrp_val_max (vr_result->type ()),
2872 build_int_cst (vr_result->type (), 1));
2874 vr_result->update (vr_result->kind (), new_min, new_max);
2876 /* If we dropped either bound to +-INF then if this is a loop
2877 PHI node SCEV may known more about its value-range. */
2878 if (cmp_min > 0 || cmp_min < 0
2879 || cmp_max < 0 || cmp_max > 0)
2880 goto scev_check;
2882 goto infinite_check;
2885 goto update_range;
2887 varying:
2888 vr_result->set_varying ();
2890 scev_check:
2891 /* If this is a loop PHI node SCEV may known more about its value-range.
2892 scev_check can be reached from two paths, one is a fall through from above
2893 "varying" label, the other is direct goto from code block which tries to
2894 avoid infinite simulation. */
2895 if (scev_initialized_p ()
2896 && (l = loop_containing_stmt (phi))
2897 && l->header == gimple_bb (phi))
2898 adjust_range_with_scev (vr_result, l, phi, lhs);
2900 infinite_check:
2901 /* If we will end up with a (-INF, +INF) range, set it to
2902 VARYING. Same if the previous max value was invalid for
2903 the type and we end up with vr_result.min > vr_result.max. */
2904 if ((!vr_result->varying_p () && !vr_result->undefined_p ())
2905 && !((vrp_val_is_max (vr_result->max ()) && vrp_val_is_min (vr_result->min ()))
2906 || compare_values (vr_result->min (), vr_result->max ()) > 0))
2908 else
2909 vr_result->set_varying ();
2911 /* If the new range is different than the previous value, keep
2912 iterating. */
2913 update_range:
2914 return;
2917 /* Simplify boolean operations if the source is known
2918 to be already a boolean. */
2919 bool
2920 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi,
2921 gimple *stmt)
2923 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
2924 tree lhs, op0, op1;
2925 bool need_conversion;
2927 /* We handle only !=/== case here. */
2928 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
2930 op0 = gimple_assign_rhs1 (stmt);
2931 if (!op_with_boolean_value_range_p (op0))
2932 return false;
2934 op1 = gimple_assign_rhs2 (stmt);
2935 if (!op_with_boolean_value_range_p (op1))
2936 return false;
2938 /* Reduce number of cases to handle to NE_EXPR. As there is no
2939 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
2940 if (rhs_code == EQ_EXPR)
2942 if (TREE_CODE (op1) == INTEGER_CST)
2943 op1 = int_const_binop (BIT_XOR_EXPR, op1,
2944 build_int_cst (TREE_TYPE (op1), 1));
2945 else
2946 return false;
2949 lhs = gimple_assign_lhs (stmt);
2950 need_conversion
2951 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
2953 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
2954 if (need_conversion
2955 && !TYPE_UNSIGNED (TREE_TYPE (op0))
2956 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
2957 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
2958 return false;
2960 /* For A != 0 we can substitute A itself. */
2961 if (integer_zerop (op1))
2962 gimple_assign_set_rhs_with_ops (gsi,
2963 need_conversion
2964 ? NOP_EXPR : TREE_CODE (op0), op0);
2965 /* For A != B we substitute A ^ B. Either with conversion. */
2966 else if (need_conversion)
2968 tree tem = make_ssa_name (TREE_TYPE (op0));
2969 gassign *newop
2970 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
2971 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
2972 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
2973 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
2974 set_range_info (tem, VR_RANGE,
2975 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
2976 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
2977 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
2979 /* Or without. */
2980 else
2981 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
2982 update_stmt (gsi_stmt (*gsi));
2983 fold_stmt (gsi, follow_single_use_edges);
2985 return true;
2988 /* Simplify a division or modulo operator to a right shift or bitwise and
2989 if the first operand is unsigned or is greater than zero and the second
2990 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
2991 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
2992 optimize it into just op0 if op0's range is known to be a subset of
2993 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
2994 modulo. */
2996 bool
2997 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi,
2998 gimple *stmt)
3000 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3001 tree val = NULL;
3002 tree op0 = gimple_assign_rhs1 (stmt);
3003 tree op1 = gimple_assign_rhs2 (stmt);
3004 tree op0min = NULL_TREE, op0max = NULL_TREE;
3005 tree op1min = op1;
3006 value_range *vr = NULL;
3008 if (TREE_CODE (op0) == INTEGER_CST)
3010 op0min = op0;
3011 op0max = op0;
3013 else
3015 vr = get_value_range (op0);
3016 if (range_int_cst_p (vr))
3018 op0min = vr->min ();
3019 op0max = vr->max ();
3023 if (rhs_code == TRUNC_MOD_EXPR
3024 && TREE_CODE (op1) == SSA_NAME)
3026 value_range *vr1 = get_value_range (op1);
3027 if (range_int_cst_p (vr1))
3028 op1min = vr1->min ();
3030 if (rhs_code == TRUNC_MOD_EXPR
3031 && TREE_CODE (op1min) == INTEGER_CST
3032 && tree_int_cst_sgn (op1min) == 1
3033 && op0max
3034 && tree_int_cst_lt (op0max, op1min))
3036 if (TYPE_UNSIGNED (TREE_TYPE (op0))
3037 || tree_int_cst_sgn (op0min) >= 0
3038 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
3039 op0min))
3041 /* If op0 already has the range op0 % op1 has,
3042 then TRUNC_MOD_EXPR won't change anything. */
3043 gimple_assign_set_rhs_from_tree (gsi, op0);
3044 return true;
3048 if (TREE_CODE (op0) != SSA_NAME)
3049 return false;
3051 if (!integer_pow2p (op1))
3053 /* X % -Y can be only optimized into X % Y either if
3054 X is not INT_MIN, or Y is not -1. Fold it now, as after
3055 remove_range_assertions the range info might be not available
3056 anymore. */
3057 if (rhs_code == TRUNC_MOD_EXPR
3058 && fold_stmt (gsi, follow_single_use_edges))
3059 return true;
3060 return false;
3063 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
3064 val = integer_one_node;
3065 else
3067 bool sop = false;
3069 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
3071 if (val
3072 && sop
3073 && integer_onep (val)
3074 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3076 location_t location;
3078 if (!gimple_has_location (stmt))
3079 location = input_location;
3080 else
3081 location = gimple_location (stmt);
3082 warning_at (location, OPT_Wstrict_overflow,
3083 "assuming signed overflow does not occur when "
3084 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3088 if (val && integer_onep (val))
3090 tree t;
3092 if (rhs_code == TRUNC_DIV_EXPR)
3094 t = build_int_cst (integer_type_node, tree_log2 (op1));
3095 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
3096 gimple_assign_set_rhs1 (stmt, op0);
3097 gimple_assign_set_rhs2 (stmt, t);
3099 else
3101 t = build_int_cst (TREE_TYPE (op1), 1);
3102 t = int_const_binop (MINUS_EXPR, op1, t);
3103 t = fold_convert (TREE_TYPE (op0), t);
3105 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
3106 gimple_assign_set_rhs1 (stmt, op0);
3107 gimple_assign_set_rhs2 (stmt, t);
3110 update_stmt (stmt);
3111 fold_stmt (gsi, follow_single_use_edges);
3112 return true;
3115 return false;
3118 /* Simplify a min or max if the ranges of the two operands are
3119 disjoint. Return true if we do simplify. */
3121 bool
3122 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi,
3123 gimple *stmt)
3125 tree op0 = gimple_assign_rhs1 (stmt);
3126 tree op1 = gimple_assign_rhs2 (stmt);
3127 bool sop = false;
3128 tree val;
3130 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3131 (LE_EXPR, op0, op1, &sop));
3132 if (!val)
3134 sop = false;
3135 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3136 (LT_EXPR, op0, op1, &sop));
3139 if (val)
3141 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3143 location_t location;
3145 if (!gimple_has_location (stmt))
3146 location = input_location;
3147 else
3148 location = gimple_location (stmt);
3149 warning_at (location, OPT_Wstrict_overflow,
3150 "assuming signed overflow does not occur when "
3151 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3154 /* VAL == TRUE -> OP0 < or <= op1
3155 VAL == FALSE -> OP0 > or >= op1. */
3156 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
3157 == integer_zerop (val)) ? op0 : op1;
3158 gimple_assign_set_rhs_from_tree (gsi, res);
3159 return true;
3162 return false;
3165 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3166 ABS_EXPR. If the operand is <= 0, then simplify the
3167 ABS_EXPR into a NEGATE_EXPR. */
3169 bool
3170 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3172 tree op = gimple_assign_rhs1 (stmt);
3173 value_range *vr = get_value_range (op);
3175 if (vr)
3177 tree val = NULL;
3178 bool sop = false;
3180 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
3181 if (!val)
3183 /* The range is neither <= 0 nor > 0. Now see if it is
3184 either < 0 or >= 0. */
3185 sop = false;
3186 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
3187 &sop);
3190 if (val)
3192 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3194 location_t location;
3196 if (!gimple_has_location (stmt))
3197 location = input_location;
3198 else
3199 location = gimple_location (stmt);
3200 warning_at (location, OPT_Wstrict_overflow,
3201 "assuming signed overflow does not occur when "
3202 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3205 gimple_assign_set_rhs1 (stmt, op);
3206 if (integer_zerop (val))
3207 gimple_assign_set_rhs_code (stmt, SSA_NAME);
3208 else
3209 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
3210 update_stmt (stmt);
3211 fold_stmt (gsi, follow_single_use_edges);
3212 return true;
3216 return false;
3219 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3220 If all the bits that are being cleared by & are already
3221 known to be zero from VR, or all the bits that are being
3222 set by | are already known to be one from VR, the bit
3223 operation is redundant. */
3225 bool
3226 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi,
3227 gimple *stmt)
3229 tree op0 = gimple_assign_rhs1 (stmt);
3230 tree op1 = gimple_assign_rhs2 (stmt);
3231 tree op = NULL_TREE;
3232 value_range_base vr0, vr1;
3233 wide_int may_be_nonzero0, may_be_nonzero1;
3234 wide_int must_be_nonzero0, must_be_nonzero1;
3235 wide_int mask;
3237 if (TREE_CODE (op0) == SSA_NAME)
3238 vr0 = *(get_value_range (op0));
3239 else if (is_gimple_min_invariant (op0))
3240 vr0.set (op0);
3241 else
3242 return false;
3244 if (TREE_CODE (op1) == SSA_NAME)
3245 vr1 = *(get_value_range (op1));
3246 else if (is_gimple_min_invariant (op1))
3247 vr1.set (op1);
3248 else
3249 return false;
3251 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
3252 &must_be_nonzero0))
3253 return false;
3254 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
3255 &must_be_nonzero1))
3256 return false;
3258 switch (gimple_assign_rhs_code (stmt))
3260 case BIT_AND_EXPR:
3261 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3262 if (mask == 0)
3264 op = op0;
3265 break;
3267 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3268 if (mask == 0)
3270 op = op1;
3271 break;
3273 break;
3274 case BIT_IOR_EXPR:
3275 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3276 if (mask == 0)
3278 op = op1;
3279 break;
3281 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3282 if (mask == 0)
3284 op = op0;
3285 break;
3287 break;
3288 default:
3289 gcc_unreachable ();
3292 if (op == NULL_TREE)
3293 return false;
3295 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
3296 update_stmt (gsi_stmt (*gsi));
3297 return true;
3300 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3301 a known value range VR.
3303 If there is one and only one value which will satisfy the
3304 conditional, then return that value. Else return NULL.
3306 If signed overflow must be undefined for the value to satisfy
3307 the conditional, then set *STRICT_OVERFLOW_P to true. */
3309 static tree
3310 test_for_singularity (enum tree_code cond_code, tree op0,
3311 tree op1, value_range *vr)
3313 tree min = NULL;
3314 tree max = NULL;
3316 /* Extract minimum/maximum values which satisfy the conditional as it was
3317 written. */
3318 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
3320 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
3322 max = op1;
3323 if (cond_code == LT_EXPR)
3325 tree one = build_int_cst (TREE_TYPE (op0), 1);
3326 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
3327 /* Signal to compare_values_warnv this expr doesn't overflow. */
3328 if (EXPR_P (max))
3329 TREE_NO_WARNING (max) = 1;
3332 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
3334 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
3336 min = op1;
3337 if (cond_code == GT_EXPR)
3339 tree one = build_int_cst (TREE_TYPE (op0), 1);
3340 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
3341 /* Signal to compare_values_warnv this expr doesn't overflow. */
3342 if (EXPR_P (min))
3343 TREE_NO_WARNING (min) = 1;
3347 /* Now refine the minimum and maximum values using any
3348 value range information we have for op0. */
3349 if (min && max)
3351 if (compare_values (vr->min (), min) == 1)
3352 min = vr->min ();
3353 if (compare_values (vr->max (), max) == -1)
3354 max = vr->max ();
3356 /* If the new min/max values have converged to a single value,
3357 then there is only one value which can satisfy the condition,
3358 return that value. */
3359 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
3360 return min;
3362 return NULL;
3365 /* Return whether the value range *VR fits in an integer type specified
3366 by PRECISION and UNSIGNED_P. */
3368 static bool
3369 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
3371 tree src_type;
3372 unsigned src_precision;
3373 widest_int tem;
3374 signop src_sgn;
3376 /* We can only handle integral and pointer types. */
3377 src_type = vr->type ();
3378 if (!INTEGRAL_TYPE_P (src_type)
3379 && !POINTER_TYPE_P (src_type))
3380 return false;
3382 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3383 and so is an identity transform. */
3384 src_precision = TYPE_PRECISION (vr->type ());
3385 src_sgn = TYPE_SIGN (src_type);
3386 if ((src_precision < dest_precision
3387 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
3388 || (src_precision == dest_precision && src_sgn == dest_sgn))
3389 return true;
3391 /* Now we can only handle ranges with constant bounds. */
3392 if (!range_int_cst_p (vr))
3393 return false;
3395 /* For sign changes, the MSB of the wide_int has to be clear.
3396 An unsigned value with its MSB set cannot be represented by
3397 a signed wide_int, while a negative value cannot be represented
3398 by an unsigned wide_int. */
3399 if (src_sgn != dest_sgn
3400 && (wi::lts_p (wi::to_wide (vr->min ()), 0)
3401 || wi::lts_p (wi::to_wide (vr->max ()), 0)))
3402 return false;
3404 /* Then we can perform the conversion on both ends and compare
3405 the result for equality. */
3406 tem = wi::ext (wi::to_widest (vr->min ()), dest_precision, dest_sgn);
3407 if (tem != wi::to_widest (vr->min ()))
3408 return false;
3409 tem = wi::ext (wi::to_widest (vr->max ()), dest_precision, dest_sgn);
3410 if (tem != wi::to_widest (vr->max ()))
3411 return false;
3413 return true;
3416 /* Simplify a conditional using a relational operator to an equality
3417 test if the range information indicates only one value can satisfy
3418 the original conditional. */
3420 bool
3421 vr_values::simplify_cond_using_ranges_1 (gcond *stmt)
3423 tree op0 = gimple_cond_lhs (stmt);
3424 tree op1 = gimple_cond_rhs (stmt);
3425 enum tree_code cond_code = gimple_cond_code (stmt);
3427 if (cond_code != NE_EXPR
3428 && cond_code != EQ_EXPR
3429 && TREE_CODE (op0) == SSA_NAME
3430 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3431 && is_gimple_min_invariant (op1))
3433 value_range *vr = get_value_range (op0);
3435 /* If we have range information for OP0, then we might be
3436 able to simplify this conditional. */
3437 if (vr->kind () == VR_RANGE)
3439 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
3440 if (new_tree)
3442 if (dump_file)
3444 fprintf (dump_file, "Simplified relational ");
3445 print_gimple_stmt (dump_file, stmt, 0);
3446 fprintf (dump_file, " into ");
3449 gimple_cond_set_code (stmt, EQ_EXPR);
3450 gimple_cond_set_lhs (stmt, op0);
3451 gimple_cond_set_rhs (stmt, new_tree);
3453 update_stmt (stmt);
3455 if (dump_file)
3457 print_gimple_stmt (dump_file, stmt, 0);
3458 fprintf (dump_file, "\n");
3461 return true;
3464 /* Try again after inverting the condition. We only deal
3465 with integral types here, so no need to worry about
3466 issues with inverting FP comparisons. */
3467 new_tree = test_for_singularity
3468 (invert_tree_comparison (cond_code, false),
3469 op0, op1, vr);
3470 if (new_tree)
3472 if (dump_file)
3474 fprintf (dump_file, "Simplified relational ");
3475 print_gimple_stmt (dump_file, stmt, 0);
3476 fprintf (dump_file, " into ");
3479 gimple_cond_set_code (stmt, NE_EXPR);
3480 gimple_cond_set_lhs (stmt, op0);
3481 gimple_cond_set_rhs (stmt, new_tree);
3483 update_stmt (stmt);
3485 if (dump_file)
3487 print_gimple_stmt (dump_file, stmt, 0);
3488 fprintf (dump_file, "\n");
3491 return true;
3495 return false;
3498 /* STMT is a conditional at the end of a basic block.
3500 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3501 was set via a type conversion, try to replace the SSA_NAME with the RHS
3502 of the type conversion. Doing so makes the conversion dead which helps
3503 subsequent passes. */
3505 void
3506 vr_values::simplify_cond_using_ranges_2 (gcond *stmt)
3508 tree op0 = gimple_cond_lhs (stmt);
3509 tree op1 = gimple_cond_rhs (stmt);
3511 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3512 see if OP0 was set by a type conversion where the source of
3513 the conversion is another SSA_NAME with a range that fits
3514 into the range of OP0's type.
3516 If so, the conversion is redundant as the earlier SSA_NAME can be
3517 used for the comparison directly if we just massage the constant in the
3518 comparison. */
3519 if (TREE_CODE (op0) == SSA_NAME
3520 && TREE_CODE (op1) == INTEGER_CST)
3522 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
3523 tree innerop;
3525 if (!is_gimple_assign (def_stmt)
3526 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3527 return;
3529 innerop = gimple_assign_rhs1 (def_stmt);
3531 if (TREE_CODE (innerop) == SSA_NAME
3532 && !POINTER_TYPE_P (TREE_TYPE (innerop))
3533 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
3534 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
3536 value_range *vr = get_value_range (innerop);
3538 if (range_int_cst_p (vr)
3539 && range_fits_type_p (vr,
3540 TYPE_PRECISION (TREE_TYPE (op0)),
3541 TYPE_SIGN (TREE_TYPE (op0)))
3542 && int_fits_type_p (op1, TREE_TYPE (innerop)))
3544 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
3545 gimple_cond_set_lhs (stmt, innerop);
3546 gimple_cond_set_rhs (stmt, newconst);
3547 update_stmt (stmt);
3548 if (dump_file && (dump_flags & TDF_DETAILS))
3550 fprintf (dump_file, "Folded into: ");
3551 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3552 fprintf (dump_file, "\n");
3559 /* Simplify a switch statement using the value range of the switch
3560 argument. */
3562 bool
3563 vr_values::simplify_switch_using_ranges (gswitch *stmt)
3565 tree op = gimple_switch_index (stmt);
3566 value_range *vr = NULL;
3567 bool take_default;
3568 edge e;
3569 edge_iterator ei;
3570 size_t i = 0, j = 0, n, n2;
3571 tree vec2;
3572 switch_update su;
3573 size_t k = 1, l = 0;
3575 if (TREE_CODE (op) == SSA_NAME)
3577 vr = get_value_range (op);
3579 /* We can only handle integer ranges. */
3580 if (vr->varying_p ()
3581 || vr->undefined_p ()
3582 || vr->symbolic_p ())
3583 return false;
3585 /* Find case label for min/max of the value range. */
3586 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
3588 else if (TREE_CODE (op) == INTEGER_CST)
3590 take_default = !find_case_label_index (stmt, 1, op, &i);
3591 if (take_default)
3593 i = 1;
3594 j = 0;
3596 else
3598 j = i;
3601 else
3602 return false;
3604 n = gimple_switch_num_labels (stmt);
3606 /* We can truncate the case label ranges that partially overlap with OP's
3607 value range. */
3608 size_t min_idx = 1, max_idx = 0;
3609 if (vr != NULL)
3610 find_case_label_range (stmt, vr->min (), vr->max (), &min_idx, &max_idx);
3611 if (min_idx <= max_idx)
3613 tree min_label = gimple_switch_label (stmt, min_idx);
3614 tree max_label = gimple_switch_label (stmt, max_idx);
3616 /* Avoid changing the type of the case labels when truncating. */
3617 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
3618 tree vr_min = fold_convert (case_label_type, vr->min ());
3619 tree vr_max = fold_convert (case_label_type, vr->max ());
3621 if (vr->kind () == VR_RANGE)
3623 /* If OP's value range is [2,8] and the low label range is
3624 0 ... 3, truncate the label's range to 2 .. 3. */
3625 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3626 && CASE_HIGH (min_label) != NULL_TREE
3627 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3628 CASE_LOW (min_label) = vr_min;
3630 /* If OP's value range is [2,8] and the high label range is
3631 7 ... 10, truncate the label's range to 7 .. 8. */
3632 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3633 && CASE_HIGH (max_label) != NULL_TREE
3634 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3635 CASE_HIGH (max_label) = vr_max;
3637 else if (vr->kind () == VR_ANTI_RANGE)
3639 tree one_cst = build_one_cst (case_label_type);
3641 if (min_label == max_label)
3643 /* If OP's value range is ~[7,8] and the label's range is
3644 7 ... 10, truncate the label's range to 9 ... 10. */
3645 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
3646 && CASE_HIGH (min_label) != NULL_TREE
3647 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
3648 CASE_LOW (min_label)
3649 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3651 /* If OP's value range is ~[7,8] and the label's range is
3652 5 ... 8, truncate the label's range to 5 ... 6. */
3653 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3654 && CASE_HIGH (min_label) != NULL_TREE
3655 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
3656 CASE_HIGH (min_label)
3657 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3659 else
3661 /* If OP's value range is ~[2,8] and the low label range is
3662 0 ... 3, truncate the label's range to 0 ... 1. */
3663 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3664 && CASE_HIGH (min_label) != NULL_TREE
3665 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3666 CASE_HIGH (min_label)
3667 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3669 /* If OP's value range is ~[2,8] and the high label range is
3670 7 ... 10, truncate the label's range to 9 ... 10. */
3671 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3672 && CASE_HIGH (max_label) != NULL_TREE
3673 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3674 CASE_LOW (max_label)
3675 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3679 /* Canonicalize singleton case ranges. */
3680 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
3681 CASE_HIGH (min_label) = NULL_TREE;
3682 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
3683 CASE_HIGH (max_label) = NULL_TREE;
3686 /* We can also eliminate case labels that lie completely outside OP's value
3687 range. */
3689 /* Bail out if this is just all edges taken. */
3690 if (i == 1
3691 && j == n - 1
3692 && take_default)
3693 return false;
3695 /* Build a new vector of taken case labels. */
3696 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
3697 n2 = 0;
3699 /* Add the default edge, if necessary. */
3700 if (take_default)
3701 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
3703 for (; i <= j; ++i, ++n2)
3704 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
3706 for (; k <= l; ++k, ++n2)
3707 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
3709 /* Mark needed edges. */
3710 for (i = 0; i < n2; ++i)
3712 e = find_edge (gimple_bb (stmt),
3713 label_to_block (cfun,
3714 CASE_LABEL (TREE_VEC_ELT (vec2, i))));
3715 e->aux = (void *)-1;
3718 /* Queue not needed edges for later removal. */
3719 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3721 if (e->aux == (void *)-1)
3723 e->aux = NULL;
3724 continue;
3727 if (dump_file && (dump_flags & TDF_DETAILS))
3729 fprintf (dump_file, "removing unreachable case label\n");
3731 to_remove_edges.safe_push (e);
3732 e->flags &= ~EDGE_EXECUTABLE;
3733 e->flags |= EDGE_IGNORE;
3736 /* And queue an update for the stmt. */
3737 su.stmt = stmt;
3738 su.vec = vec2;
3739 to_update_switch_stmts.safe_push (su);
3740 return false;
3743 void
3744 vr_values::cleanup_edges_and_switches (void)
3746 int i;
3747 edge e;
3748 switch_update *su;
3750 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3751 CFG in a broken state and requires a cfg_cleanup run. */
3752 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
3753 remove_edge (e);
3755 /* Update SWITCH_EXPR case label vector. */
3756 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
3758 size_t j;
3759 size_t n = TREE_VEC_LENGTH (su->vec);
3760 tree label;
3761 gimple_switch_set_num_labels (su->stmt, n);
3762 for (j = 0; j < n; j++)
3763 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
3764 /* As we may have replaced the default label with a regular one
3765 make sure to make it a real default label again. This ensures
3766 optimal expansion. */
3767 label = gimple_switch_label (su->stmt, 0);
3768 CASE_LOW (label) = NULL_TREE;
3769 CASE_HIGH (label) = NULL_TREE;
3772 if (!to_remove_edges.is_empty ())
3774 free_dominance_info (CDI_DOMINATORS);
3775 loops_state_set (LOOPS_NEED_FIXUP);
3778 to_remove_edges.release ();
3779 to_update_switch_stmts.release ();
3782 /* Simplify an integral conversion from an SSA name in STMT. */
3784 static bool
3785 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3787 tree innerop, middleop, finaltype;
3788 gimple *def_stmt;
3789 signop inner_sgn, middle_sgn, final_sgn;
3790 unsigned inner_prec, middle_prec, final_prec;
3791 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
3793 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
3794 if (!INTEGRAL_TYPE_P (finaltype))
3795 return false;
3796 middleop = gimple_assign_rhs1 (stmt);
3797 def_stmt = SSA_NAME_DEF_STMT (middleop);
3798 if (!is_gimple_assign (def_stmt)
3799 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3800 return false;
3801 innerop = gimple_assign_rhs1 (def_stmt);
3802 if (TREE_CODE (innerop) != SSA_NAME
3803 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
3804 return false;
3806 /* Get the value-range of the inner operand. Use get_range_info in
3807 case innerop was created during substitute-and-fold. */
3808 wide_int imin, imax;
3809 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
3810 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
3811 return false;
3812 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
3813 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
3815 /* Simulate the conversion chain to check if the result is equal if
3816 the middle conversion is removed. */
3817 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
3818 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
3819 final_prec = TYPE_PRECISION (finaltype);
3821 /* If the first conversion is not injective, the second must not
3822 be widening. */
3823 if (wi::gtu_p (innermax - innermin,
3824 wi::mask <widest_int> (middle_prec, false))
3825 && middle_prec < final_prec)
3826 return false;
3827 /* We also want a medium value so that we can track the effect that
3828 narrowing conversions with sign change have. */
3829 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
3830 if (inner_sgn == UNSIGNED)
3831 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
3832 else
3833 innermed = 0;
3834 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
3835 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
3836 innermed = innermin;
3838 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
3839 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
3840 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
3841 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
3843 /* Require that the final conversion applied to both the original
3844 and the intermediate range produces the same result. */
3845 final_sgn = TYPE_SIGN (finaltype);
3846 if (wi::ext (middlemin, final_prec, final_sgn)
3847 != wi::ext (innermin, final_prec, final_sgn)
3848 || wi::ext (middlemed, final_prec, final_sgn)
3849 != wi::ext (innermed, final_prec, final_sgn)
3850 || wi::ext (middlemax, final_prec, final_sgn)
3851 != wi::ext (innermax, final_prec, final_sgn))
3852 return false;
3854 gimple_assign_set_rhs1 (stmt, innerop);
3855 fold_stmt (gsi, follow_single_use_edges);
3856 return true;
3859 /* Simplify a conversion from integral SSA name to float in STMT. */
3861 bool
3862 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
3863 gimple *stmt)
3865 tree rhs1 = gimple_assign_rhs1 (stmt);
3866 value_range *vr = get_value_range (rhs1);
3867 scalar_float_mode fltmode
3868 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
3869 scalar_int_mode mode;
3870 tree tem;
3871 gassign *conv;
3873 /* We can only handle constant ranges. */
3874 if (!range_int_cst_p (vr))
3875 return false;
3877 /* First check if we can use a signed type in place of an unsigned. */
3878 scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
3879 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
3880 && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
3881 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
3882 mode = rhs_mode;
3883 /* If we can do the conversion in the current input mode do nothing. */
3884 else if (can_float_p (fltmode, rhs_mode,
3885 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
3886 return false;
3887 /* Otherwise search for a mode we can use, starting from the narrowest
3888 integer mode available. */
3889 else
3891 mode = NARROWEST_INT_MODE;
3892 for (;;)
3894 /* If we cannot do a signed conversion to float from mode
3895 or if the value-range does not fit in the signed type
3896 try with a wider mode. */
3897 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
3898 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
3899 break;
3901 /* But do not widen the input. Instead leave that to the
3902 optabs expansion code. */
3903 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
3904 || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
3905 return false;
3909 /* It works, insert a truncation or sign-change before the
3910 float conversion. */
3911 tem = make_ssa_name (build_nonstandard_integer_type
3912 (GET_MODE_PRECISION (mode), 0));
3913 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
3914 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
3915 gimple_assign_set_rhs1 (stmt, tem);
3916 fold_stmt (gsi, follow_single_use_edges);
3918 return true;
3921 /* Simplify an internal fn call using ranges if possible. */
3923 bool
3924 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi,
3925 gimple *stmt)
3927 enum tree_code subcode;
3928 bool is_ubsan = false;
3929 bool ovf = false;
3930 switch (gimple_call_internal_fn (stmt))
3932 case IFN_UBSAN_CHECK_ADD:
3933 subcode = PLUS_EXPR;
3934 is_ubsan = true;
3935 break;
3936 case IFN_UBSAN_CHECK_SUB:
3937 subcode = MINUS_EXPR;
3938 is_ubsan = true;
3939 break;
3940 case IFN_UBSAN_CHECK_MUL:
3941 subcode = MULT_EXPR;
3942 is_ubsan = true;
3943 break;
3944 case IFN_ADD_OVERFLOW:
3945 subcode = PLUS_EXPR;
3946 break;
3947 case IFN_SUB_OVERFLOW:
3948 subcode = MINUS_EXPR;
3949 break;
3950 case IFN_MUL_OVERFLOW:
3951 subcode = MULT_EXPR;
3952 break;
3953 default:
3954 return false;
3957 tree op0 = gimple_call_arg (stmt, 0);
3958 tree op1 = gimple_call_arg (stmt, 1);
3959 tree type;
3960 if (is_ubsan)
3962 type = TREE_TYPE (op0);
3963 if (VECTOR_TYPE_P (type))
3964 return false;
3966 else if (gimple_call_lhs (stmt) == NULL_TREE)
3967 return false;
3968 else
3969 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
3970 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
3971 || (is_ubsan && ovf))
3972 return false;
3974 gimple *g;
3975 location_t loc = gimple_location (stmt);
3976 if (is_ubsan)
3977 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
3978 else
3980 int prec = TYPE_PRECISION (type);
3981 tree utype = type;
3982 if (ovf
3983 || !useless_type_conversion_p (type, TREE_TYPE (op0))
3984 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
3985 utype = build_nonstandard_integer_type (prec, 1);
3986 if (TREE_CODE (op0) == INTEGER_CST)
3987 op0 = fold_convert (utype, op0);
3988 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
3990 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
3991 gimple_set_location (g, loc);
3992 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3993 op0 = gimple_assign_lhs (g);
3995 if (TREE_CODE (op1) == INTEGER_CST)
3996 op1 = fold_convert (utype, op1);
3997 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
3999 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
4000 gimple_set_location (g, loc);
4001 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4002 op1 = gimple_assign_lhs (g);
4004 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
4005 gimple_set_location (g, loc);
4006 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4007 if (utype != type)
4009 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
4010 gimple_assign_lhs (g));
4011 gimple_set_location (g, loc);
4012 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4014 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
4015 gimple_assign_lhs (g),
4016 build_int_cst (type, ovf));
4018 gimple_set_location (g, loc);
4019 gsi_replace (gsi, g, false);
4020 return true;
4023 /* Return true if VAR is a two-valued variable. Set a and b with the
4024 two-values when it is true. Return false otherwise. */
4026 bool
4027 vr_values::two_valued_val_range_p (tree var, tree *a, tree *b)
4029 value_range *vr = get_value_range (var);
4030 if (vr->varying_p ()
4031 || vr->undefined_p ()
4032 || TREE_CODE (vr->min ()) != INTEGER_CST
4033 || TREE_CODE (vr->max ()) != INTEGER_CST)
4034 return false;
4036 if (vr->kind () == VR_RANGE
4037 && wi::to_wide (vr->max ()) - wi::to_wide (vr->min ()) == 1)
4039 *a = vr->min ();
4040 *b = vr->max ();
4041 return true;
4044 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4045 if (vr->kind () == VR_ANTI_RANGE
4046 && (wi::to_wide (vr->min ())
4047 - wi::to_wide (vrp_val_min (TREE_TYPE (var)))) == 1
4048 && (wi::to_wide (vrp_val_max (TREE_TYPE (var)))
4049 - wi::to_wide (vr->max ())) == 1)
4051 *a = vrp_val_min (TREE_TYPE (var));
4052 *b = vrp_val_max (TREE_TYPE (var));
4053 return true;
4056 return false;
4059 /* Simplify STMT using ranges if possible. */
4061 bool
4062 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
4064 gimple *stmt = gsi_stmt (*gsi);
4065 if (is_gimple_assign (stmt))
4067 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4068 tree rhs1 = gimple_assign_rhs1 (stmt);
4069 tree rhs2 = gimple_assign_rhs2 (stmt);
4070 tree lhs = gimple_assign_lhs (stmt);
4071 tree val1 = NULL_TREE, val2 = NULL_TREE;
4072 use_operand_p use_p;
4073 gimple *use_stmt;
4075 /* Convert:
4076 LHS = CST BINOP VAR
4077 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4079 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4081 Also handles:
4082 LHS = VAR BINOP CST
4083 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4085 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4087 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
4088 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4089 && ((TREE_CODE (rhs1) == INTEGER_CST
4090 && TREE_CODE (rhs2) == SSA_NAME)
4091 || (TREE_CODE (rhs2) == INTEGER_CST
4092 && TREE_CODE (rhs1) == SSA_NAME))
4093 && single_imm_use (lhs, &use_p, &use_stmt)
4094 && gimple_code (use_stmt) == GIMPLE_COND)
4097 tree new_rhs1 = NULL_TREE;
4098 tree new_rhs2 = NULL_TREE;
4099 tree cmp_var = NULL_TREE;
4101 if (TREE_CODE (rhs2) == SSA_NAME
4102 && two_valued_val_range_p (rhs2, &val1, &val2))
4104 /* Optimize RHS1 OP [VAL1, VAL2]. */
4105 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
4106 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
4107 cmp_var = rhs2;
4109 else if (TREE_CODE (rhs1) == SSA_NAME
4110 && two_valued_val_range_p (rhs1, &val1, &val2))
4112 /* Optimize [VAL1, VAL2] OP RHS2. */
4113 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
4114 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
4115 cmp_var = rhs1;
4118 /* If we could not find two-vals or the optimzation is invalid as
4119 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4120 if (new_rhs1 && new_rhs2)
4122 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
4123 gimple_assign_set_rhs_with_ops (gsi,
4124 COND_EXPR, cond,
4125 new_rhs1,
4126 new_rhs2);
4127 update_stmt (gsi_stmt (*gsi));
4128 fold_stmt (gsi, follow_single_use_edges);
4129 return true;
4133 switch (rhs_code)
4135 case EQ_EXPR:
4136 case NE_EXPR:
4137 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4138 if the RHS is zero or one, and the LHS are known to be boolean
4139 values. */
4140 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4141 return simplify_truth_ops_using_ranges (gsi, stmt);
4142 break;
4144 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4145 and BIT_AND_EXPR respectively if the first operand is greater
4146 than zero and the second operand is an exact power of two.
4147 Also optimize TRUNC_MOD_EXPR away if the second operand is
4148 constant and the first operand already has the right value
4149 range. */
4150 case TRUNC_DIV_EXPR:
4151 case TRUNC_MOD_EXPR:
4152 if ((TREE_CODE (rhs1) == SSA_NAME
4153 || TREE_CODE (rhs1) == INTEGER_CST)
4154 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4155 return simplify_div_or_mod_using_ranges (gsi, stmt);
4156 break;
4158 /* Transform ABS (X) into X or -X as appropriate. */
4159 case ABS_EXPR:
4160 if (TREE_CODE (rhs1) == SSA_NAME
4161 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4162 return simplify_abs_using_ranges (gsi, stmt);
4163 break;
4165 case BIT_AND_EXPR:
4166 case BIT_IOR_EXPR:
4167 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4168 if all the bits being cleared are already cleared or
4169 all the bits being set are already set. */
4170 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4171 return simplify_bit_ops_using_ranges (gsi, stmt);
4172 break;
4174 CASE_CONVERT:
4175 if (TREE_CODE (rhs1) == SSA_NAME
4176 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4177 return simplify_conversion_using_ranges (gsi, stmt);
4178 break;
4180 case FLOAT_EXPR:
4181 if (TREE_CODE (rhs1) == SSA_NAME
4182 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4183 return simplify_float_conversion_using_ranges (gsi, stmt);
4184 break;
4186 case MIN_EXPR:
4187 case MAX_EXPR:
4188 return simplify_min_or_max_using_ranges (gsi, stmt);
4190 default:
4191 break;
4194 else if (gimple_code (stmt) == GIMPLE_COND)
4195 return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
4196 else if (gimple_code (stmt) == GIMPLE_SWITCH)
4197 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
4198 else if (is_gimple_call (stmt)
4199 && gimple_call_internal_p (stmt))
4200 return simplify_internal_call_using_ranges (gsi, stmt);
4202 return false;
4205 void
4206 vr_values::set_vr_value (tree var, value_range *vr)
4208 if (SSA_NAME_VERSION (var) >= num_vr_values)
4209 return;
4210 vr_value[SSA_NAME_VERSION (var)] = vr;