]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/vr-values.c
Enforce canonicalization in value_range.
[thirdparty/gcc.git] / gcc / vr-values.c
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2019 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
51
52 /* Set value range VR to a non-negative range of type TYPE. */
53
54 static inline void
55 set_value_range_to_nonnegative (value_range *vr, tree type)
56 {
57 tree zero = build_int_cst (type, 0);
58 vr->update (VR_RANGE, zero, vrp_val_max (type));
59 }
60
61 /* Set value range VR to a range of a truthvalue of type TYPE. */
62
63 static inline void
64 set_value_range_to_truthvalue (value_range *vr, tree type)
65 {
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));
70 }
71
72 /* Return the lattice entry for VAR or NULL if it doesn't exist or cannot
73 be initialized. */
74
75 value_range *
76 vr_values::get_lattice_entry (const_tree var)
77 {
78 value_range *vr;
79 tree sym;
80 unsigned ver = SSA_NAME_VERSION (var);
81
82 /* If we query the entry for a new SSA name avoid reallocating the lattice
83 since we should get here at most from the substitute-and-fold stage which
84 will never try to change values. */
85 if (ver >= num_vr_values)
86 return NULL;
87
88 vr = vr_value[ver];
89 if (vr)
90 return vr;
91
92 /* After propagation finished do not allocate new value-ranges. */
93 if (values_propagated)
94 return NULL;
95
96 /* Create a default value range. */
97 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
98 vr->set_undefined ();
99
100 /* If VAR is a default definition of a parameter, the variable can
101 take any value in VAR's type. */
102 if (SSA_NAME_IS_DEFAULT_DEF (var))
103 {
104 sym = SSA_NAME_VAR (var);
105 if (TREE_CODE (sym) == PARM_DECL)
106 {
107 /* Try to use the "nonnull" attribute to create ~[0, 0]
108 anti-ranges for pointers. Note that this is only valid with
109 default definitions of PARM_DECLs. */
110 if (POINTER_TYPE_P (TREE_TYPE (sym))
111 && (nonnull_arg_p (sym)
112 || get_ptr_nonnull (var)))
113 {
114 vr->set_nonzero (TREE_TYPE (sym));
115 vr->equiv_clear ();
116 }
117 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
118 {
119 get_range_info (var, *vr);
120 if (vr->undefined_p ())
121 vr->set_varying ();
122 }
123 else
124 vr->set_varying ();
125 }
126 else if (TREE_CODE (sym) == RESULT_DECL
127 && DECL_BY_REFERENCE (sym))
128 {
129 vr->set_nonzero (TREE_TYPE (sym));
130 vr->equiv_clear ();
131 }
132 }
133
134 return vr;
135 }
136
137 /* Return value range information for VAR.
138
139 If we have no values ranges recorded (ie, VRP is not running), then
140 return NULL. Otherwise create an empty range if none existed for VAR. */
141
142 const value_range *
143 vr_values::get_value_range (const_tree var)
144 {
145 static const value_range vr_const_varying (VR_VARYING, NULL, NULL);
146
147 /* If we have no recorded ranges, then return NULL. */
148 if (!vr_value)
149 return NULL;
150
151 value_range *vr = get_lattice_entry (var);
152 if (!vr)
153 return &vr_const_varying;
154
155 return vr;
156 }
157
158 /* Set the lattice entry for DEF to VARYING. */
159
160 void
161 vr_values::set_def_to_varying (const_tree def)
162 {
163 value_range *vr = get_lattice_entry (def);
164 if (vr)
165 vr->set_varying ();
166 }
167
168 /* Set value-ranges of all SSA names defined by STMT to varying. */
169
170 void
171 vr_values::set_defs_to_varying (gimple *stmt)
172 {
173 ssa_op_iter i;
174 tree def;
175 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
176 set_def_to_varying (def);
177 }
178
179 /* Update the value range and equivalence set for variable VAR to
180 NEW_VR. Return true if NEW_VR is different from VAR's previous
181 value.
182
183 NOTE: This function assumes that NEW_VR is a temporary value range
184 object created for the sole purpose of updating VAR's range. The
185 storage used by the equivalence set from NEW_VR will be freed by
186 this function. Do not call update_value_range when NEW_VR
187 is the range object associated with another SSA name. */
188
189 bool
190 vr_values::update_value_range (const_tree var, value_range *new_vr)
191 {
192 value_range *old_vr;
193 bool is_new;
194
195 /* If there is a value-range on the SSA name from earlier analysis
196 factor that in. */
197 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
198 {
199 value_range nr;
200 value_range_kind rtype = get_range_info (var, nr);
201 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
202 new_vr->intersect (&nr);
203 }
204
205 /* Update the value range, if necessary. If we cannot allocate a lattice
206 entry for VAR keep it at VARYING. This happens when DOM feeds us stmts
207 with SSA names allocated after setting up the lattice. */
208 old_vr = get_lattice_entry (var);
209 if (!old_vr)
210 return false;
211 is_new = !old_vr->equal_p (*new_vr, /*ignore_equivs=*/false);
212
213 if (is_new)
214 {
215 /* Do not allow transitions up the lattice. The following
216 is slightly more awkward than just new_vr->type < old_vr->type
217 because VR_RANGE and VR_ANTI_RANGE need to be considered
218 the same. We may not have is_new when transitioning to
219 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
220 called, if we are anyway, keep it VARYING. */
221 if (old_vr->varying_p ())
222 {
223 new_vr->set_varying ();
224 is_new = false;
225 }
226 else if (new_vr->undefined_p ())
227 {
228 old_vr->set_varying ();
229 new_vr->set_varying ();
230 return true;
231 }
232 else
233 old_vr->set (new_vr->kind (),
234 new_vr->min (), new_vr->max (), new_vr->equiv ());
235 }
236
237 new_vr->equiv_clear ();
238
239 return is_new;
240 }
241
242 /* Return true if value range VR involves exactly one symbol SYM. */
243
244 static bool
245 symbolic_range_based_on_p (value_range_base *vr, const_tree sym)
246 {
247 bool neg, min_has_symbol, max_has_symbol;
248 tree inv;
249
250 if (is_gimple_min_invariant (vr->min ()))
251 min_has_symbol = false;
252 else if (get_single_symbol (vr->min (), &neg, &inv) == sym)
253 min_has_symbol = true;
254 else
255 return false;
256
257 if (is_gimple_min_invariant (vr->max ()))
258 max_has_symbol = false;
259 else if (get_single_symbol (vr->max (), &neg, &inv) == sym)
260 max_has_symbol = true;
261 else
262 return false;
263
264 return (min_has_symbol || max_has_symbol);
265 }
266
267 /* Return true if the result of assignment STMT is know to be non-zero. */
268
269 static bool
270 gimple_assign_nonzero_p (gimple *stmt)
271 {
272 enum tree_code code = gimple_assign_rhs_code (stmt);
273 bool strict_overflow_p;
274 switch (get_gimple_rhs_class (code))
275 {
276 case GIMPLE_UNARY_RHS:
277 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
278 gimple_expr_type (stmt),
279 gimple_assign_rhs1 (stmt),
280 &strict_overflow_p);
281 case GIMPLE_BINARY_RHS:
282 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
283 gimple_expr_type (stmt),
284 gimple_assign_rhs1 (stmt),
285 gimple_assign_rhs2 (stmt),
286 &strict_overflow_p);
287 case GIMPLE_TERNARY_RHS:
288 return false;
289 case GIMPLE_SINGLE_RHS:
290 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
291 &strict_overflow_p);
292 case GIMPLE_INVALID_RHS:
293 gcc_unreachable ();
294 default:
295 gcc_unreachable ();
296 }
297 }
298
299 /* Return true if STMT is known to compute a non-zero value. */
300
301 static bool
302 gimple_stmt_nonzero_p (gimple *stmt)
303 {
304 switch (gimple_code (stmt))
305 {
306 case GIMPLE_ASSIGN:
307 return gimple_assign_nonzero_p (stmt);
308 case GIMPLE_CALL:
309 {
310 gcall *call_stmt = as_a<gcall *> (stmt);
311 return (gimple_call_nonnull_result_p (call_stmt)
312 || gimple_call_nonnull_arg (call_stmt));
313 }
314 default:
315 gcc_unreachable ();
316 }
317 }
318 /* Like tree_expr_nonzero_p, but this function uses value ranges
319 obtained so far. */
320
321 bool
322 vr_values::vrp_stmt_computes_nonzero (gimple *stmt)
323 {
324 if (gimple_stmt_nonzero_p (stmt))
325 return true;
326
327 /* If we have an expression of the form &X->a, then the expression
328 is nonnull if X is nonnull. */
329 if (is_gimple_assign (stmt)
330 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
331 {
332 tree expr = gimple_assign_rhs1 (stmt);
333 poly_int64 bitsize, bitpos;
334 tree offset;
335 machine_mode mode;
336 int unsignedp, reversep, volatilep;
337 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
338 &bitpos, &offset, &mode, &unsignedp,
339 &reversep, &volatilep);
340
341 if (base != NULL_TREE
342 && TREE_CODE (base) == MEM_REF
343 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
344 {
345 poly_offset_int off = 0;
346 bool off_cst = false;
347 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
348 {
349 off = mem_ref_offset (base);
350 if (offset)
351 off += poly_offset_int::from (wi::to_poly_wide (offset),
352 SIGNED);
353 off <<= LOG2_BITS_PER_UNIT;
354 off += bitpos;
355 off_cst = true;
356 }
357 /* If &X->a is equal to X and X is ~[0, 0], the result is too.
358 For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
359 allow going from non-NULL pointer to NULL. */
360 if ((off_cst && known_eq (off, 0))
361 || (flag_delete_null_pointer_checks
362 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))))
363 {
364 const value_range *vr = get_value_range (TREE_OPERAND (base, 0));
365 if (!range_includes_zero_p (vr))
366 return true;
367 }
368 /* If MEM_REF has a "positive" offset, consider it non-NULL
369 always, for -fdelete-null-pointer-checks also "negative"
370 ones. Punt for unknown offsets (e.g. variable ones). */
371 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
372 && off_cst
373 && known_ne (off, 0)
374 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
375 return true;
376 }
377 }
378
379 return false;
380 }
381
382 /* Returns true if EXPR is a valid value (as expected by compare_values) --
383 a gimple invariant, or SSA_NAME +- CST. */
384
385 static bool
386 valid_value_p (tree expr)
387 {
388 if (TREE_CODE (expr) == SSA_NAME)
389 return true;
390
391 if (TREE_CODE (expr) == PLUS_EXPR
392 || TREE_CODE (expr) == MINUS_EXPR)
393 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
394 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
395
396 return is_gimple_min_invariant (expr);
397 }
398
399 /* If OP has a value range with a single constant value return that,
400 otherwise return NULL_TREE. This returns OP itself if OP is a
401 constant. */
402
403 tree
404 vr_values::op_with_constant_singleton_value_range (tree op)
405 {
406 if (is_gimple_min_invariant (op))
407 return op;
408
409 if (TREE_CODE (op) != SSA_NAME)
410 return NULL_TREE;
411
412 tree t;
413 if (get_value_range (op)->singleton_p (&t))
414 return t;
415 return NULL;
416 }
417
418 /* Return true if op is in a boolean [0, 1] value-range. */
419
420 bool
421 vr_values::op_with_boolean_value_range_p (tree op)
422 {
423 const value_range *vr;
424
425 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
426 return true;
427
428 if (integer_zerop (op)
429 || integer_onep (op))
430 return true;
431
432 if (TREE_CODE (op) != SSA_NAME)
433 return false;
434
435 vr = get_value_range (op);
436 return (vr->kind () == VR_RANGE
437 && integer_zerop (vr->min ())
438 && integer_onep (vr->max ()));
439 }
440
441 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
442 true and store it in *VR_P. */
443
444 void
445 vr_values::extract_range_for_var_from_comparison_expr (tree var,
446 enum tree_code cond_code,
447 tree op, tree limit,
448 value_range *vr_p)
449 {
450 tree min, max, type;
451 const value_range *limit_vr;
452 type = TREE_TYPE (var);
453
454 /* For pointer arithmetic, we only keep track of pointer equality
455 and inequality. If we arrive here with unfolded conditions like
456 _1 > _1 do not derive anything. */
457 if ((POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
458 || limit == var)
459 {
460 vr_p->set_varying ();
461 return;
462 }
463
464 /* If LIMIT is another SSA name and LIMIT has a range of its own,
465 try to use LIMIT's range to avoid creating symbolic ranges
466 unnecessarily. */
467 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
468
469 /* LIMIT's range is only interesting if it has any useful information. */
470 if (! limit_vr
471 || limit_vr->undefined_p ()
472 || limit_vr->varying_p ()
473 || (limit_vr->symbolic_p ()
474 && ! (limit_vr->kind () == VR_RANGE
475 && (limit_vr->min () == limit_vr->max ()
476 || operand_equal_p (limit_vr->min (),
477 limit_vr->max (), 0)))))
478 limit_vr = NULL;
479
480 /* Initially, the new range has the same set of equivalences of
481 VAR's range. This will be revised before returning the final
482 value. Since assertions may be chained via mutually exclusive
483 predicates, we will need to trim the set of equivalences before
484 we are done. */
485 gcc_assert (vr_p->equiv () == NULL);
486 vr_p->equiv_add (var, get_value_range (var), &vrp_equiv_obstack);
487
488 /* Extract a new range based on the asserted comparison for VAR and
489 LIMIT's value range. Notice that if LIMIT has an anti-range, we
490 will only use it for equality comparisons (EQ_EXPR). For any
491 other kind of assertion, we cannot derive a range from LIMIT's
492 anti-range that can be used to describe the new range. For
493 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
494 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
495 no single range for x_2 that could describe LE_EXPR, so we might
496 as well build the range [b_4, +INF] for it.
497 One special case we handle is extracting a range from a
498 range test encoded as (unsigned)var + CST <= limit. */
499 if (TREE_CODE (op) == NOP_EXPR
500 || TREE_CODE (op) == PLUS_EXPR)
501 {
502 if (TREE_CODE (op) == PLUS_EXPR)
503 {
504 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
505 TREE_OPERAND (op, 1));
506 max = int_const_binop (PLUS_EXPR, limit, min);
507 op = TREE_OPERAND (op, 0);
508 }
509 else
510 {
511 min = build_int_cst (TREE_TYPE (var), 0);
512 max = limit;
513 }
514
515 /* Make sure to not set TREE_OVERFLOW on the final type
516 conversion. We are willingly interpreting large positive
517 unsigned values as negative signed values here. */
518 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
519 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
520
521 /* We can transform a max, min range to an anti-range or
522 vice-versa. Use set_and_canonicalize which does this for
523 us. */
524 if (cond_code == LE_EXPR)
525 vr_p->set (VR_RANGE, min, max, vr_p->equiv ());
526 else if (cond_code == GT_EXPR)
527 vr_p->set (VR_ANTI_RANGE, min, max, vr_p->equiv ());
528 else
529 gcc_unreachable ();
530 }
531 else if (cond_code == EQ_EXPR)
532 {
533 enum value_range_kind range_type;
534
535 if (limit_vr)
536 {
537 range_type = limit_vr->kind ();
538 min = limit_vr->min ();
539 max = limit_vr->max ();
540 }
541 else
542 {
543 range_type = VR_RANGE;
544 min = limit;
545 max = limit;
546 }
547
548 vr_p->update (range_type, min, max);
549
550 /* When asserting the equality VAR == LIMIT and LIMIT is another
551 SSA name, the new range will also inherit the equivalence set
552 from LIMIT. */
553 if (TREE_CODE (limit) == SSA_NAME)
554 vr_p->equiv_add (limit, get_value_range (limit), &vrp_equiv_obstack);
555 }
556 else if (cond_code == NE_EXPR)
557 {
558 /* As described above, when LIMIT's range is an anti-range and
559 this assertion is an inequality (NE_EXPR), then we cannot
560 derive anything from the anti-range. For instance, if
561 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
562 not imply that VAR's range is [0, 0]. So, in the case of
563 anti-ranges, we just assert the inequality using LIMIT and
564 not its anti-range.
565
566 If LIMIT_VR is a range, we can only use it to build a new
567 anti-range if LIMIT_VR is a single-valued range. For
568 instance, if LIMIT_VR is [0, 1], the predicate
569 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
570 Rather, it means that for value 0 VAR should be ~[0, 0]
571 and for value 1, VAR should be ~[1, 1]. We cannot
572 represent these ranges.
573
574 The only situation in which we can build a valid
575 anti-range is when LIMIT_VR is a single-valued range
576 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
577 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
578 if (limit_vr
579 && limit_vr->kind () == VR_RANGE
580 && compare_values (limit_vr->min (), limit_vr->max ()) == 0)
581 {
582 min = limit_vr->min ();
583 max = limit_vr->max ();
584 }
585 else
586 {
587 /* In any other case, we cannot use LIMIT's range to build a
588 valid anti-range. */
589 min = max = limit;
590 }
591
592 /* If MIN and MAX cover the whole range for their type, then
593 just use the original LIMIT. */
594 if (INTEGRAL_TYPE_P (type)
595 && vrp_val_is_min (min)
596 && vrp_val_is_max (max))
597 min = max = limit;
598
599 vr_p->set (VR_ANTI_RANGE, min, max, vr_p->equiv ());
600 }
601 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
602 {
603 min = TYPE_MIN_VALUE (type);
604
605 if (limit_vr == NULL || limit_vr->kind () == VR_ANTI_RANGE)
606 max = limit;
607 else
608 {
609 /* If LIMIT_VR is of the form [N1, N2], we need to build the
610 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
611 LT_EXPR. */
612 max = limit_vr->max ();
613 }
614
615 /* If the maximum value forces us to be out of bounds, simply punt.
616 It would be pointless to try and do anything more since this
617 all should be optimized away above us. */
618 if (cond_code == LT_EXPR
619 && compare_values (max, min) == 0)
620 vr_p->set_varying ();
621 else
622 {
623 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
624 if (cond_code == LT_EXPR)
625 {
626 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
627 && !TYPE_UNSIGNED (TREE_TYPE (max)))
628 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
629 build_int_cst (TREE_TYPE (max), -1));
630 else
631 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
632 build_int_cst (TREE_TYPE (max), 1));
633 /* Signal to compare_values_warnv this expr doesn't overflow. */
634 if (EXPR_P (max))
635 TREE_NO_WARNING (max) = 1;
636 }
637
638 vr_p->update (VR_RANGE, min, max);
639 }
640 }
641 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
642 {
643 max = TYPE_MAX_VALUE (type);
644
645 if (limit_vr == NULL || limit_vr->kind () == VR_ANTI_RANGE)
646 min = limit;
647 else
648 {
649 /* If LIMIT_VR is of the form [N1, N2], we need to build the
650 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
651 GT_EXPR. */
652 min = limit_vr->min ();
653 }
654
655 /* If the minimum value forces us to be out of bounds, simply punt.
656 It would be pointless to try and do anything more since this
657 all should be optimized away above us. */
658 if (cond_code == GT_EXPR
659 && compare_values (min, max) == 0)
660 vr_p->set_varying ();
661 else
662 {
663 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
664 if (cond_code == GT_EXPR)
665 {
666 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
667 && !TYPE_UNSIGNED (TREE_TYPE (min)))
668 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
669 build_int_cst (TREE_TYPE (min), -1));
670 else
671 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
672 build_int_cst (TREE_TYPE (min), 1));
673 /* Signal to compare_values_warnv this expr doesn't overflow. */
674 if (EXPR_P (min))
675 TREE_NO_WARNING (min) = 1;
676 }
677
678 vr_p->update (VR_RANGE, min, max);
679 }
680 }
681 else
682 gcc_unreachable ();
683
684 /* Finally intersect the new range with what we already know about var. */
685 vr_p->intersect (get_value_range (var));
686 }
687
688 /* Extract value range information from an ASSERT_EXPR EXPR and store
689 it in *VR_P. */
690
691 void
692 vr_values::extract_range_from_assert (value_range *vr_p, tree expr)
693 {
694 tree var = ASSERT_EXPR_VAR (expr);
695 tree cond = ASSERT_EXPR_COND (expr);
696 tree limit, op;
697 enum tree_code cond_code;
698 gcc_assert (COMPARISON_CLASS_P (cond));
699
700 /* Find VAR in the ASSERT_EXPR conditional. */
701 if (var == TREE_OPERAND (cond, 0)
702 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
703 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
704 {
705 /* If the predicate is of the form VAR COMP LIMIT, then we just
706 take LIMIT from the RHS and use the same comparison code. */
707 cond_code = TREE_CODE (cond);
708 limit = TREE_OPERAND (cond, 1);
709 op = TREE_OPERAND (cond, 0);
710 }
711 else
712 {
713 /* If the predicate is of the form LIMIT COMP VAR, then we need
714 to flip around the comparison code to create the proper range
715 for VAR. */
716 cond_code = swap_tree_comparison (TREE_CODE (cond));
717 limit = TREE_OPERAND (cond, 0);
718 op = TREE_OPERAND (cond, 1);
719 }
720 extract_range_for_var_from_comparison_expr (var, cond_code, op,
721 limit, vr_p);
722 }
723
724 /* Extract range information from SSA name VAR and store it in VR. If
725 VAR has an interesting range, use it. Otherwise, create the
726 range [VAR, VAR] and return it. This is useful in situations where
727 we may have conditionals testing values of VARYING names. For
728 instance,
729
730 x_3 = y_5;
731 if (x_3 > y_5)
732 ...
733
734 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
735 always false. */
736
737 void
738 vr_values::extract_range_from_ssa_name (value_range *vr, tree var)
739 {
740 const value_range *var_vr = get_value_range (var);
741
742 if (!var_vr->varying_p ())
743 vr->deep_copy (var_vr);
744 else
745 vr->set (var);
746
747 if (!vr->undefined_p ())
748 vr->equiv_add (var, get_value_range (var), &vrp_equiv_obstack);
749 }
750
751 /* Extract range information from a binary expression OP0 CODE OP1 based on
752 the ranges of each of its operands with resulting type EXPR_TYPE.
753 The resulting range is stored in *VR. */
754
755 void
756 vr_values::extract_range_from_binary_expr (value_range *vr,
757 enum tree_code code,
758 tree expr_type, tree op0, tree op1)
759 {
760 /* Get value ranges for each operand. For constant operands, create
761 a new value range with the operand to simplify processing. */
762 value_range_base vr0, vr1;
763 if (TREE_CODE (op0) == SSA_NAME)
764 vr0 = *(get_value_range (op0));
765 else if (is_gimple_min_invariant (op0))
766 vr0.set (op0);
767 else
768 vr0.set_varying ();
769
770 if (TREE_CODE (op1) == SSA_NAME)
771 vr1 = *(get_value_range (op1));
772 else if (is_gimple_min_invariant (op1))
773 vr1.set (op1);
774 else
775 vr1.set_varying ();
776
777 /* If one argument is varying, we can sometimes still deduce a
778 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
779 if (INTEGRAL_TYPE_P (TREE_TYPE (op0))
780 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
781 {
782 if (vr0.varying_p () && !vr1.varying_p ())
783 vr0 = value_range (VR_RANGE,
784 vrp_val_min (expr_type),
785 vrp_val_max (expr_type));
786 else if (vr1.varying_p () && !vr0.varying_p ())
787 vr1 = value_range (VR_RANGE,
788 vrp_val_min (expr_type),
789 vrp_val_max (expr_type));
790 }
791
792 ::extract_range_from_binary_expr (vr, code, expr_type, &vr0, &vr1);
793
794 /* Set value_range for n in following sequence:
795 def = __builtin_memchr (arg, 0, sz)
796 n = def - arg
797 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
798
799 if (vr->varying_p ()
800 && code == POINTER_DIFF_EXPR
801 && TREE_CODE (op0) == SSA_NAME
802 && TREE_CODE (op1) == SSA_NAME)
803 {
804 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
805 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
806 gcall *call_stmt = NULL;
807
808 if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
809 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
810 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
811 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
812 && (call_stmt = dyn_cast<gcall *>(SSA_NAME_DEF_STMT (op0)))
813 && gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR)
814 && operand_equal_p (op0, gimple_call_lhs (call_stmt), 0)
815 && operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0)
816 && integer_zerop (gimple_call_arg (call_stmt, 1)))
817 {
818 tree max = vrp_val_max (ptrdiff_type_node);
819 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
820 tree range_min = build_zero_cst (expr_type);
821 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
822 vr->set (VR_RANGE, range_min, range_max);
823 return;
824 }
825 }
826
827 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
828 and based on the other operand, for example if it was deduced from a
829 symbolic comparison. When a bound of the range of the first operand
830 is invariant, we set the corresponding bound of the new range to INF
831 in order to avoid recursing on the range of the second operand. */
832 if (vr->varying_p ()
833 && (code == PLUS_EXPR || code == MINUS_EXPR)
834 && TREE_CODE (op1) == SSA_NAME
835 && vr0.kind () == VR_RANGE
836 && symbolic_range_based_on_p (&vr0, op1))
837 {
838 const bool minus_p = (code == MINUS_EXPR);
839 value_range n_vr1;
840
841 /* Try with VR0 and [-INF, OP1]. */
842 if (is_gimple_min_invariant (minus_p ? vr0.max () : vr0.min ()))
843 n_vr1.set (VR_RANGE, vrp_val_min (expr_type), op1);
844
845 /* Try with VR0 and [OP1, +INF]. */
846 else if (is_gimple_min_invariant (minus_p ? vr0.min () : vr0.max ()))
847 n_vr1.set (VR_RANGE, op1, vrp_val_max (expr_type));
848
849 /* Try with VR0 and [OP1, OP1]. */
850 else
851 n_vr1.set (VR_RANGE, op1, op1);
852
853 ::extract_range_from_binary_expr (vr, code, expr_type, &vr0, &n_vr1);
854 }
855
856 if (vr->varying_p ()
857 && (code == PLUS_EXPR || code == MINUS_EXPR)
858 && TREE_CODE (op0) == SSA_NAME
859 && vr1.kind () == VR_RANGE
860 && symbolic_range_based_on_p (&vr1, op0))
861 {
862 const bool minus_p = (code == MINUS_EXPR);
863 value_range n_vr0;
864
865 /* Try with [-INF, OP0] and VR1. */
866 if (is_gimple_min_invariant (minus_p ? vr1.max () : vr1.min ()))
867 n_vr0.set (VR_RANGE, vrp_val_min (expr_type), op0);
868
869 /* Try with [OP0, +INF] and VR1. */
870 else if (is_gimple_min_invariant (minus_p ? vr1.min (): vr1.max ()))
871 n_vr0.set (VR_RANGE, op0, vrp_val_max (expr_type));
872
873 /* Try with [OP0, OP0] and VR1. */
874 else
875 n_vr0.set (op0);
876
877 ::extract_range_from_binary_expr (vr, code, expr_type, &n_vr0, &vr1);
878 }
879
880 /* If we didn't derive a range for MINUS_EXPR, and
881 op1's range is ~[op0,op0] or vice-versa, then we
882 can derive a non-null range. This happens often for
883 pointer subtraction. */
884 if (vr->varying_p ()
885 && (code == MINUS_EXPR || code == POINTER_DIFF_EXPR)
886 && TREE_CODE (op0) == SSA_NAME
887 && ((vr0.kind () == VR_ANTI_RANGE
888 && vr0.min () == op1
889 && vr0.min () == vr0.max ())
890 || (vr1.kind () == VR_ANTI_RANGE
891 && vr1.min () == op0
892 && vr1.min () == vr1.max ())))
893 {
894 vr->set_nonzero (expr_type);
895 vr->equiv_clear ();
896 }
897 }
898
899 /* Extract range information from a unary expression CODE OP0 based on
900 the range of its operand with resulting type TYPE.
901 The resulting range is stored in *VR. */
902
903 void
904 vr_values::extract_range_from_unary_expr (value_range *vr, enum tree_code code,
905 tree type, tree op0)
906 {
907 value_range_base vr0;
908
909 /* Get value ranges for the operand. For constant operands, create
910 a new value range with the operand to simplify processing. */
911 if (TREE_CODE (op0) == SSA_NAME)
912 vr0 = *(get_value_range (op0));
913 else if (is_gimple_min_invariant (op0))
914 vr0.set (op0);
915 else
916 vr0.set_varying ();
917
918 ::extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
919 }
920
921
922 /* Extract range information from a conditional expression STMT based on
923 the ranges of each of its operands and the expression code. */
924
925 void
926 vr_values::extract_range_from_cond_expr (value_range *vr, gassign *stmt)
927 {
928 /* Get value ranges for each operand. For constant operands, create
929 a new value range with the operand to simplify processing. */
930 tree op0 = gimple_assign_rhs2 (stmt);
931 value_range tem0;
932 const value_range *vr0 = &tem0;
933 if (TREE_CODE (op0) == SSA_NAME)
934 vr0 = get_value_range (op0);
935 else if (is_gimple_min_invariant (op0))
936 tem0.set (op0);
937 else
938 tem0.set_varying ();
939
940 tree op1 = gimple_assign_rhs3 (stmt);
941 value_range tem1;
942 const value_range *vr1 = &tem1;
943 if (TREE_CODE (op1) == SSA_NAME)
944 vr1 = get_value_range (op1);
945 else if (is_gimple_min_invariant (op1))
946 tem1.set (op1);
947 else
948 tem1.set_varying ();
949
950 /* The resulting value range is the union of the operand ranges */
951 vr->deep_copy (vr0);
952 vr->union_ (vr1);
953 }
954
955
956 /* Extract range information from a comparison expression EXPR based
957 on the range of its operand and the expression code. */
958
959 void
960 vr_values::extract_range_from_comparison (value_range *vr, enum tree_code code,
961 tree type, tree op0, tree op1)
962 {
963 bool sop;
964 tree val;
965
966 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
967 NULL);
968 if (val)
969 {
970 /* Since this expression was found on the RHS of an assignment,
971 its type may be different from _Bool. Convert VAL to EXPR's
972 type. */
973 val = fold_convert (type, val);
974 if (is_gimple_min_invariant (val))
975 vr->set (val);
976 else
977 vr->update (VR_RANGE, val, val);
978 }
979 else
980 /* The result of a comparison is always true or false. */
981 set_value_range_to_truthvalue (vr, type);
982 }
983
984 /* Helper function for simplify_internal_call_using_ranges and
985 extract_range_basic. Return true if OP0 SUBCODE OP1 for
986 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
987 always overflow. Set *OVF to true if it is known to always
988 overflow. */
989
990 bool
991 vr_values::check_for_binary_op_overflow (enum tree_code subcode, tree type,
992 tree op0, tree op1, bool *ovf)
993 {
994 value_range_base vr0, vr1;
995 if (TREE_CODE (op0) == SSA_NAME)
996 vr0 = *get_value_range (op0);
997 else if (TREE_CODE (op0) == INTEGER_CST)
998 vr0.set (op0);
999 else
1000 vr0.set_varying ();
1001
1002 if (TREE_CODE (op1) == SSA_NAME)
1003 vr1 = *get_value_range (op1);
1004 else if (TREE_CODE (op1) == INTEGER_CST)
1005 vr1.set (op1);
1006 else
1007 vr1.set_varying ();
1008
1009 tree vr0min = vr0.min (), vr0max = vr0.max ();
1010 tree vr1min = vr1.min (), vr1max = vr1.max ();
1011 if (!range_int_cst_p (&vr0)
1012 || TREE_OVERFLOW (vr0min)
1013 || TREE_OVERFLOW (vr0max))
1014 {
1015 vr0min = vrp_val_min (TREE_TYPE (op0));
1016 vr0max = vrp_val_max (TREE_TYPE (op0));
1017 }
1018 if (!range_int_cst_p (&vr1)
1019 || TREE_OVERFLOW (vr1min)
1020 || TREE_OVERFLOW (vr1max))
1021 {
1022 vr1min = vrp_val_min (TREE_TYPE (op1));
1023 vr1max = vrp_val_max (TREE_TYPE (op1));
1024 }
1025 *ovf = arith_overflowed_p (subcode, type, vr0min,
1026 subcode == MINUS_EXPR ? vr1max : vr1min);
1027 if (arith_overflowed_p (subcode, type, vr0max,
1028 subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
1029 return false;
1030 if (subcode == MULT_EXPR)
1031 {
1032 if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
1033 || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
1034 return false;
1035 }
1036 if (*ovf)
1037 {
1038 /* So far we found that there is an overflow on the boundaries.
1039 That doesn't prove that there is an overflow even for all values
1040 in between the boundaries. For that compute widest_int range
1041 of the result and see if it doesn't overlap the range of
1042 type. */
1043 widest_int wmin, wmax;
1044 widest_int w[4];
1045 int i;
1046 w[0] = wi::to_widest (vr0min);
1047 w[1] = wi::to_widest (vr0max);
1048 w[2] = wi::to_widest (vr1min);
1049 w[3] = wi::to_widest (vr1max);
1050 for (i = 0; i < 4; i++)
1051 {
1052 widest_int wt;
1053 switch (subcode)
1054 {
1055 case PLUS_EXPR:
1056 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
1057 break;
1058 case MINUS_EXPR:
1059 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
1060 break;
1061 case MULT_EXPR:
1062 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
1063 break;
1064 default:
1065 gcc_unreachable ();
1066 }
1067 if (i == 0)
1068 {
1069 wmin = wt;
1070 wmax = wt;
1071 }
1072 else
1073 {
1074 wmin = wi::smin (wmin, wt);
1075 wmax = wi::smax (wmax, wt);
1076 }
1077 }
1078 /* The result of op0 CODE op1 is known to be in range
1079 [wmin, wmax]. */
1080 widest_int wtmin = wi::to_widest (vrp_val_min (type));
1081 widest_int wtmax = wi::to_widest (vrp_val_max (type));
1082 /* If all values in [wmin, wmax] are smaller than
1083 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1084 the arithmetic operation will always overflow. */
1085 if (wmax < wtmin || wmin > wtmax)
1086 return true;
1087 return false;
1088 }
1089 return true;
1090 }
1091
1092 /* Try to derive a nonnegative or nonzero range out of STMT relying
1093 primarily on generic routines in fold in conjunction with range data.
1094 Store the result in *VR */
1095
1096 void
1097 vr_values::extract_range_basic (value_range *vr, gimple *stmt)
1098 {
1099 bool sop;
1100 tree type = gimple_expr_type (stmt);
1101
1102 if (is_gimple_call (stmt))
1103 {
1104 tree arg;
1105 int mini, maxi, zerov = 0, prec;
1106 enum tree_code subcode = ERROR_MARK;
1107 combined_fn cfn = gimple_call_combined_fn (stmt);
1108 scalar_int_mode mode;
1109
1110 switch (cfn)
1111 {
1112 case CFN_BUILT_IN_CONSTANT_P:
1113 /* If the call is __builtin_constant_p and the argument is a
1114 function parameter resolve it to false. This avoids bogus
1115 array bound warnings.
1116 ??? We could do this as early as inlining is finished. */
1117 arg = gimple_call_arg (stmt, 0);
1118 if (TREE_CODE (arg) == SSA_NAME
1119 && SSA_NAME_IS_DEFAULT_DEF (arg)
1120 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
1121 && cfun->after_inlining)
1122 {
1123 vr->set_zero (type);
1124 vr->equiv_clear ();
1125 return;
1126 }
1127 break;
1128 /* Both __builtin_ffs* and __builtin_popcount return
1129 [0, prec]. */
1130 CASE_CFN_FFS:
1131 CASE_CFN_POPCOUNT:
1132 arg = gimple_call_arg (stmt, 0);
1133 prec = TYPE_PRECISION (TREE_TYPE (arg));
1134 mini = 0;
1135 maxi = prec;
1136 if (TREE_CODE (arg) == SSA_NAME)
1137 {
1138 const value_range *vr0 = get_value_range (arg);
1139 /* If arg is non-zero, then ffs or popcount are non-zero. */
1140 if (range_includes_zero_p (vr0) == 0)
1141 mini = 1;
1142 /* If some high bits are known to be zero,
1143 we can decrease the maximum. */
1144 if (vr0->kind () == VR_RANGE
1145 && TREE_CODE (vr0->max ()) == INTEGER_CST
1146 && !operand_less_p (vr0->min (),
1147 build_zero_cst (TREE_TYPE (vr0->min ()))))
1148 maxi = tree_floor_log2 (vr0->max ()) + 1;
1149 }
1150 goto bitop_builtin;
1151 /* __builtin_parity* returns [0, 1]. */
1152 CASE_CFN_PARITY:
1153 mini = 0;
1154 maxi = 1;
1155 goto bitop_builtin;
1156 /* __builtin_c[lt]z* return [0, prec-1], except for
1157 when the argument is 0, but that is undefined behavior.
1158 On many targets where the CLZ RTL or optab value is defined
1159 for 0 the value is prec, so include that in the range
1160 by default. */
1161 CASE_CFN_CLZ:
1162 arg = gimple_call_arg (stmt, 0);
1163 prec = TYPE_PRECISION (TREE_TYPE (arg));
1164 mini = 0;
1165 maxi = prec;
1166 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1167 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1168 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov)
1169 /* Handle only the single common value. */
1170 && zerov != prec)
1171 /* Magic value to give up, unless vr0 proves
1172 arg is non-zero. */
1173 mini = -2;
1174 if (TREE_CODE (arg) == SSA_NAME)
1175 {
1176 const value_range *vr0 = get_value_range (arg);
1177 /* From clz of VR_RANGE minimum we can compute
1178 result maximum. */
1179 if (vr0->kind () == VR_RANGE
1180 && TREE_CODE (vr0->min ()) == INTEGER_CST)
1181 {
1182 maxi = prec - 1 - tree_floor_log2 (vr0->min ());
1183 if (maxi != prec)
1184 mini = 0;
1185 }
1186 else if (vr0->kind () == VR_ANTI_RANGE
1187 && integer_zerop (vr0->min ()))
1188 {
1189 maxi = prec - 1;
1190 mini = 0;
1191 }
1192 if (mini == -2)
1193 break;
1194 /* From clz of VR_RANGE maximum we can compute
1195 result minimum. */
1196 if (vr0->kind () == VR_RANGE
1197 && TREE_CODE (vr0->max ()) == INTEGER_CST)
1198 {
1199 mini = prec - 1 - tree_floor_log2 (vr0->max ());
1200 if (mini == prec)
1201 break;
1202 }
1203 }
1204 if (mini == -2)
1205 break;
1206 goto bitop_builtin;
1207 /* __builtin_ctz* return [0, prec-1], except for
1208 when the argument is 0, but that is undefined behavior.
1209 If there is a ctz optab for this mode and
1210 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1211 otherwise just assume 0 won't be seen. */
1212 CASE_CFN_CTZ:
1213 arg = gimple_call_arg (stmt, 0);
1214 prec = TYPE_PRECISION (TREE_TYPE (arg));
1215 mini = 0;
1216 maxi = prec - 1;
1217 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1218 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1219 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov))
1220 {
1221 /* Handle only the two common values. */
1222 if (zerov == -1)
1223 mini = -1;
1224 else if (zerov == prec)
1225 maxi = prec;
1226 else
1227 /* Magic value to give up, unless vr0 proves
1228 arg is non-zero. */
1229 mini = -2;
1230 }
1231 if (TREE_CODE (arg) == SSA_NAME)
1232 {
1233 const value_range *vr0 = get_value_range (arg);
1234 /* If arg is non-zero, then use [0, prec - 1]. */
1235 if ((vr0->kind () == VR_RANGE
1236 && integer_nonzerop (vr0->min ()))
1237 || (vr0->kind () == VR_ANTI_RANGE
1238 && integer_zerop (vr0->min ())))
1239 {
1240 mini = 0;
1241 maxi = prec - 1;
1242 }
1243 /* If some high bits are known to be zero,
1244 we can decrease the result maximum. */
1245 if (vr0->kind () == VR_RANGE
1246 && TREE_CODE (vr0->max ()) == INTEGER_CST)
1247 {
1248 maxi = tree_floor_log2 (vr0->max ());
1249 /* For vr0 [0, 0] give up. */
1250 if (maxi == -1)
1251 break;
1252 }
1253 }
1254 if (mini == -2)
1255 break;
1256 goto bitop_builtin;
1257 /* __builtin_clrsb* returns [0, prec-1]. */
1258 CASE_CFN_CLRSB:
1259 arg = gimple_call_arg (stmt, 0);
1260 prec = TYPE_PRECISION (TREE_TYPE (arg));
1261 mini = 0;
1262 maxi = prec - 1;
1263 goto bitop_builtin;
1264 bitop_builtin:
1265 vr->set (VR_RANGE, build_int_cst (type, mini),
1266 build_int_cst (type, maxi));
1267 return;
1268 case CFN_UBSAN_CHECK_ADD:
1269 subcode = PLUS_EXPR;
1270 break;
1271 case CFN_UBSAN_CHECK_SUB:
1272 subcode = MINUS_EXPR;
1273 break;
1274 case CFN_UBSAN_CHECK_MUL:
1275 subcode = MULT_EXPR;
1276 break;
1277 case CFN_GOACC_DIM_SIZE:
1278 case CFN_GOACC_DIM_POS:
1279 /* Optimizing these two internal functions helps the loop
1280 optimizer eliminate outer comparisons. Size is [1,N]
1281 and pos is [0,N-1]. */
1282 {
1283 bool is_pos = cfn == CFN_GOACC_DIM_POS;
1284 int axis = oacc_get_ifn_dim_arg (stmt);
1285 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1286
1287 if (!size)
1288 /* If it's dynamic, the backend might know a hardware
1289 limitation. */
1290 size = targetm.goacc.dim_limit (axis);
1291
1292 tree type = TREE_TYPE (gimple_call_lhs (stmt));
1293 vr->set(VR_RANGE, build_int_cst (type, is_pos ? 0 : 1),
1294 size
1295 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1296 }
1297 return;
1298 case CFN_BUILT_IN_STRLEN:
1299 if (tree lhs = gimple_call_lhs (stmt))
1300 if (ptrdiff_type_node
1301 && (TYPE_PRECISION (ptrdiff_type_node)
1302 == TYPE_PRECISION (TREE_TYPE (lhs))))
1303 {
1304 tree type = TREE_TYPE (lhs);
1305 tree max = vrp_val_max (ptrdiff_type_node);
1306 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1307 tree range_min = build_zero_cst (type);
1308 tree range_max = wide_int_to_tree (type, wmax - 1);
1309 vr->set (VR_RANGE, range_min, range_max);
1310 return;
1311 }
1312 break;
1313 default:
1314 break;
1315 }
1316 if (subcode != ERROR_MARK)
1317 {
1318 bool saved_flag_wrapv = flag_wrapv;
1319 /* Pretend the arithmetics is wrapping. If there is
1320 any overflow, we'll complain, but will actually do
1321 wrapping operation. */
1322 flag_wrapv = 1;
1323 extract_range_from_binary_expr (vr, subcode, type,
1324 gimple_call_arg (stmt, 0),
1325 gimple_call_arg (stmt, 1));
1326 flag_wrapv = saved_flag_wrapv;
1327
1328 /* If for both arguments vrp_valueize returned non-NULL,
1329 this should have been already folded and if not, it
1330 wasn't folded because of overflow. Avoid removing the
1331 UBSAN_CHECK_* calls in that case. */
1332 if (vr->kind () == VR_RANGE
1333 && (vr->min () == vr->max ()
1334 || operand_equal_p (vr->min (), vr->max (), 0)))
1335 vr->set_varying ();
1336 return;
1337 }
1338 }
1339 /* Handle extraction of the two results (result of arithmetics and
1340 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1341 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1342 else if (is_gimple_assign (stmt)
1343 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
1344 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
1345 && INTEGRAL_TYPE_P (type))
1346 {
1347 enum tree_code code = gimple_assign_rhs_code (stmt);
1348 tree op = gimple_assign_rhs1 (stmt);
1349 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
1350 {
1351 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
1352 if (is_gimple_call (g) && gimple_call_internal_p (g))
1353 {
1354 enum tree_code subcode = ERROR_MARK;
1355 switch (gimple_call_internal_fn (g))
1356 {
1357 case IFN_ADD_OVERFLOW:
1358 subcode = PLUS_EXPR;
1359 break;
1360 case IFN_SUB_OVERFLOW:
1361 subcode = MINUS_EXPR;
1362 break;
1363 case IFN_MUL_OVERFLOW:
1364 subcode = MULT_EXPR;
1365 break;
1366 case IFN_ATOMIC_COMPARE_EXCHANGE:
1367 if (code == IMAGPART_EXPR)
1368 {
1369 /* This is the boolean return value whether compare and
1370 exchange changed anything or not. */
1371 vr->set (VR_RANGE, build_int_cst (type, 0),
1372 build_int_cst (type, 1));
1373 return;
1374 }
1375 break;
1376 default:
1377 break;
1378 }
1379 if (subcode != ERROR_MARK)
1380 {
1381 tree op0 = gimple_call_arg (g, 0);
1382 tree op1 = gimple_call_arg (g, 1);
1383 if (code == IMAGPART_EXPR)
1384 {
1385 bool ovf = false;
1386 if (check_for_binary_op_overflow (subcode, type,
1387 op0, op1, &ovf))
1388 vr->set (build_int_cst (type, ovf));
1389 else if (TYPE_PRECISION (type) == 1
1390 && !TYPE_UNSIGNED (type))
1391 vr->set_varying ();
1392 else
1393 vr->set (VR_RANGE, build_int_cst (type, 0),
1394 build_int_cst (type, 1));
1395 }
1396 else if (types_compatible_p (type, TREE_TYPE (op0))
1397 && types_compatible_p (type, TREE_TYPE (op1)))
1398 {
1399 bool saved_flag_wrapv = flag_wrapv;
1400 /* Pretend the arithmetics is wrapping. If there is
1401 any overflow, IMAGPART_EXPR will be set. */
1402 flag_wrapv = 1;
1403 extract_range_from_binary_expr (vr, subcode, type,
1404 op0, op1);
1405 flag_wrapv = saved_flag_wrapv;
1406 }
1407 else
1408 {
1409 value_range vr0, vr1;
1410 bool saved_flag_wrapv = flag_wrapv;
1411 /* Pretend the arithmetics is wrapping. If there is
1412 any overflow, IMAGPART_EXPR will be set. */
1413 flag_wrapv = 1;
1414 extract_range_from_unary_expr (&vr0, NOP_EXPR,
1415 type, op0);
1416 extract_range_from_unary_expr (&vr1, NOP_EXPR,
1417 type, op1);
1418 ::extract_range_from_binary_expr (vr, subcode, type,
1419 &vr0, &vr1);
1420 flag_wrapv = saved_flag_wrapv;
1421 }
1422 return;
1423 }
1424 }
1425 }
1426 }
1427 if (INTEGRAL_TYPE_P (type)
1428 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
1429 set_value_range_to_nonnegative (vr, type);
1430 else if (vrp_stmt_computes_nonzero (stmt))
1431 {
1432 vr->set_nonzero (type);
1433 vr->equiv_clear ();
1434 }
1435 else
1436 vr->set_varying ();
1437 }
1438
1439
1440 /* Try to compute a useful range out of assignment STMT and store it
1441 in *VR. */
1442
1443 void
1444 vr_values::extract_range_from_assignment (value_range *vr, gassign *stmt)
1445 {
1446 enum tree_code code = gimple_assign_rhs_code (stmt);
1447
1448 if (code == ASSERT_EXPR)
1449 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
1450 else if (code == SSA_NAME)
1451 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
1452 else if (TREE_CODE_CLASS (code) == tcc_binary)
1453 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
1454 gimple_expr_type (stmt),
1455 gimple_assign_rhs1 (stmt),
1456 gimple_assign_rhs2 (stmt));
1457 else if (TREE_CODE_CLASS (code) == tcc_unary)
1458 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
1459 gimple_expr_type (stmt),
1460 gimple_assign_rhs1 (stmt));
1461 else if (code == COND_EXPR)
1462 extract_range_from_cond_expr (vr, stmt);
1463 else if (TREE_CODE_CLASS (code) == tcc_comparison)
1464 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
1465 gimple_expr_type (stmt),
1466 gimple_assign_rhs1 (stmt),
1467 gimple_assign_rhs2 (stmt));
1468 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
1469 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1470 vr->set (gimple_assign_rhs1 (stmt));
1471 else
1472 vr->set_varying ();
1473
1474 if (vr->varying_p ())
1475 extract_range_basic (vr, stmt);
1476 }
1477
1478 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1479
1480 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1481 all the values in the ranges.
1482
1483 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1484
1485 - Return NULL_TREE if it is not always possible to determine the
1486 value of the comparison.
1487
1488 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1489 assumed signed overflow is undefined. */
1490
1491
1492 static tree
1493 compare_ranges (enum tree_code comp, const value_range *vr0,
1494 const value_range *vr1, bool *strict_overflow_p)
1495 {
1496 /* VARYING or UNDEFINED ranges cannot be compared. */
1497 if (vr0->varying_p ()
1498 || vr0->undefined_p ()
1499 || vr1->varying_p ()
1500 || vr1->undefined_p ())
1501 return NULL_TREE;
1502
1503 /* Anti-ranges need to be handled separately. */
1504 if (vr0->kind () == VR_ANTI_RANGE || vr1->kind () == VR_ANTI_RANGE)
1505 {
1506 /* If both are anti-ranges, then we cannot compute any
1507 comparison. */
1508 if (vr0->kind () == VR_ANTI_RANGE && vr1->kind () == VR_ANTI_RANGE)
1509 return NULL_TREE;
1510
1511 /* These comparisons are never statically computable. */
1512 if (comp == GT_EXPR
1513 || comp == GE_EXPR
1514 || comp == LT_EXPR
1515 || comp == LE_EXPR)
1516 return NULL_TREE;
1517
1518 /* Equality can be computed only between a range and an
1519 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1520 if (vr0->kind () == VR_RANGE)
1521 /* To simplify processing, make VR0 the anti-range. */
1522 std::swap (vr0, vr1);
1523
1524 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
1525
1526 if (compare_values_warnv (vr0->min (), vr1->min (), strict_overflow_p) == 0
1527 && compare_values_warnv (vr0->max (), vr1->max (), strict_overflow_p) == 0)
1528 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1529
1530 return NULL_TREE;
1531 }
1532
1533 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1534 operands around and change the comparison code. */
1535 if (comp == GT_EXPR || comp == GE_EXPR)
1536 {
1537 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
1538 std::swap (vr0, vr1);
1539 }
1540
1541 if (comp == EQ_EXPR)
1542 {
1543 /* Equality may only be computed if both ranges represent
1544 exactly one value. */
1545 if (compare_values_warnv (vr0->min (), vr0->max (), strict_overflow_p) == 0
1546 && compare_values_warnv (vr1->min (), vr1->max (), strict_overflow_p) == 0)
1547 {
1548 int cmp_min = compare_values_warnv (vr0->min (), vr1->min (),
1549 strict_overflow_p);
1550 int cmp_max = compare_values_warnv (vr0->max (), vr1->max (),
1551 strict_overflow_p);
1552 if (cmp_min == 0 && cmp_max == 0)
1553 return boolean_true_node;
1554 else if (cmp_min != -2 && cmp_max != -2)
1555 return boolean_false_node;
1556 }
1557 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1558 else if (compare_values_warnv (vr0->min (), vr1->max (),
1559 strict_overflow_p) == 1
1560 || compare_values_warnv (vr1->min (), vr0->max (),
1561 strict_overflow_p) == 1)
1562 return boolean_false_node;
1563
1564 return NULL_TREE;
1565 }
1566 else if (comp == NE_EXPR)
1567 {
1568 int cmp1, cmp2;
1569
1570 /* If VR0 is completely to the left or completely to the right
1571 of VR1, they are always different. Notice that we need to
1572 make sure that both comparisons yield similar results to
1573 avoid comparing values that cannot be compared at
1574 compile-time. */
1575 cmp1 = compare_values_warnv (vr0->max (), vr1->min (), strict_overflow_p);
1576 cmp2 = compare_values_warnv (vr0->min (), vr1->max (), strict_overflow_p);
1577 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
1578 return boolean_true_node;
1579
1580 /* If VR0 and VR1 represent a single value and are identical,
1581 return false. */
1582 else if (compare_values_warnv (vr0->min (), vr0->max (),
1583 strict_overflow_p) == 0
1584 && compare_values_warnv (vr1->min (), vr1->max (),
1585 strict_overflow_p) == 0
1586 && compare_values_warnv (vr0->min (), vr1->min (),
1587 strict_overflow_p) == 0
1588 && compare_values_warnv (vr0->max (), vr1->max (),
1589 strict_overflow_p) == 0)
1590 return boolean_false_node;
1591
1592 /* Otherwise, they may or may not be different. */
1593 else
1594 return NULL_TREE;
1595 }
1596 else if (comp == LT_EXPR || comp == LE_EXPR)
1597 {
1598 int tst;
1599
1600 /* If VR0 is to the left of VR1, return true. */
1601 tst = compare_values_warnv (vr0->max (), vr1->min (), strict_overflow_p);
1602 if ((comp == LT_EXPR && tst == -1)
1603 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1604 return boolean_true_node;
1605
1606 /* If VR0 is to the right of VR1, return false. */
1607 tst = compare_values_warnv (vr0->min (), vr1->max (), strict_overflow_p);
1608 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1609 || (comp == LE_EXPR && tst == 1))
1610 return boolean_false_node;
1611
1612 /* Otherwise, we don't know. */
1613 return NULL_TREE;
1614 }
1615
1616 gcc_unreachable ();
1617 }
1618
1619 /* Given a value range VR, a value VAL and a comparison code COMP, return
1620 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1621 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1622 always returns false. Return NULL_TREE if it is not always
1623 possible to determine the value of the comparison. Also set
1624 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1625 assumed signed overflow is undefined. */
1626
1627 static tree
1628 compare_range_with_value (enum tree_code comp, const value_range *vr, tree val,
1629 bool *strict_overflow_p)
1630 {
1631 if (vr->varying_p () || vr->undefined_p ())
1632 return NULL_TREE;
1633
1634 /* Anti-ranges need to be handled separately. */
1635 if (vr->kind () == VR_ANTI_RANGE)
1636 {
1637 /* For anti-ranges, the only predicates that we can compute at
1638 compile time are equality and inequality. */
1639 if (comp == GT_EXPR
1640 || comp == GE_EXPR
1641 || comp == LT_EXPR
1642 || comp == LE_EXPR)
1643 return NULL_TREE;
1644
1645 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1646 if (!vr->may_contain_p (val))
1647 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
1648
1649 return NULL_TREE;
1650 }
1651
1652 if (comp == EQ_EXPR)
1653 {
1654 /* EQ_EXPR may only be computed if VR represents exactly
1655 one value. */
1656 if (compare_values_warnv (vr->min (), vr->max (), strict_overflow_p) == 0)
1657 {
1658 int cmp = compare_values_warnv (vr->min (), val, strict_overflow_p);
1659 if (cmp == 0)
1660 return boolean_true_node;
1661 else if (cmp == -1 || cmp == 1 || cmp == 2)
1662 return boolean_false_node;
1663 }
1664 else if (compare_values_warnv (val, vr->min (), strict_overflow_p) == -1
1665 || compare_values_warnv (vr->max (), val, strict_overflow_p) == -1)
1666 return boolean_false_node;
1667
1668 return NULL_TREE;
1669 }
1670 else if (comp == NE_EXPR)
1671 {
1672 /* If VAL is not inside VR, then they are always different. */
1673 if (compare_values_warnv (vr->max (), val, strict_overflow_p) == -1
1674 || compare_values_warnv (vr->min (), val, strict_overflow_p) == 1)
1675 return boolean_true_node;
1676
1677 /* If VR represents exactly one value equal to VAL, then return
1678 false. */
1679 if (compare_values_warnv (vr->min (), vr->max (), strict_overflow_p) == 0
1680 && compare_values_warnv (vr->min (), val, strict_overflow_p) == 0)
1681 return boolean_false_node;
1682
1683 /* Otherwise, they may or may not be different. */
1684 return NULL_TREE;
1685 }
1686 else if (comp == LT_EXPR || comp == LE_EXPR)
1687 {
1688 int tst;
1689
1690 /* If VR is to the left of VAL, return true. */
1691 tst = compare_values_warnv (vr->max (), val, strict_overflow_p);
1692 if ((comp == LT_EXPR && tst == -1)
1693 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
1694 return boolean_true_node;
1695
1696 /* If VR is to the right of VAL, return false. */
1697 tst = compare_values_warnv (vr->min (), val, strict_overflow_p);
1698 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
1699 || (comp == LE_EXPR && tst == 1))
1700 return boolean_false_node;
1701
1702 /* Otherwise, we don't know. */
1703 return NULL_TREE;
1704 }
1705 else if (comp == GT_EXPR || comp == GE_EXPR)
1706 {
1707 int tst;
1708
1709 /* If VR is to the right of VAL, return true. */
1710 tst = compare_values_warnv (vr->min (), val, strict_overflow_p);
1711 if ((comp == GT_EXPR && tst == 1)
1712 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
1713 return boolean_true_node;
1714
1715 /* If VR is to the left of VAL, return false. */
1716 tst = compare_values_warnv (vr->max (), val, strict_overflow_p);
1717 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
1718 || (comp == GE_EXPR && tst == -1))
1719 return boolean_false_node;
1720
1721 /* Otherwise, we don't know. */
1722 return NULL_TREE;
1723 }
1724
1725 gcc_unreachable ();
1726 }
1727 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1728 would be profitable to adjust VR using scalar evolution information
1729 for VAR. If so, update VR with the new limits. */
1730
1731 void
1732 vr_values::adjust_range_with_scev (value_range *vr, class loop *loop,
1733 gimple *stmt, tree var)
1734 {
1735 tree init, step, chrec, tmin, tmax, min, max, type, tem;
1736 enum ev_direction dir;
1737
1738 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1739 better opportunities than a regular range, but I'm not sure. */
1740 if (vr->kind () == VR_ANTI_RANGE)
1741 return;
1742
1743 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
1744
1745 /* Like in PR19590, scev can return a constant function. */
1746 if (is_gimple_min_invariant (chrec))
1747 {
1748 vr->set (chrec);
1749 return;
1750 }
1751
1752 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1753 return;
1754
1755 init = initial_condition_in_loop_num (chrec, loop->num);
1756 tem = op_with_constant_singleton_value_range (init);
1757 if (tem)
1758 init = tem;
1759 step = evolution_part_in_loop_num (chrec, loop->num);
1760 tem = op_with_constant_singleton_value_range (step);
1761 if (tem)
1762 step = tem;
1763
1764 /* If STEP is symbolic, we can't know whether INIT will be the
1765 minimum or maximum value in the range. Also, unless INIT is
1766 a simple expression, compare_values and possibly other functions
1767 in tree-vrp won't be able to handle it. */
1768 if (step == NULL_TREE
1769 || !is_gimple_min_invariant (step)
1770 || !valid_value_p (init))
1771 return;
1772
1773 dir = scev_direction (chrec);
1774 if (/* Do not adjust ranges if we do not know whether the iv increases
1775 or decreases, ... */
1776 dir == EV_DIR_UNKNOWN
1777 /* ... or if it may wrap. */
1778 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
1779 get_chrec_loop (chrec), true))
1780 return;
1781
1782 type = TREE_TYPE (var);
1783 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1784 tmin = lower_bound_in_type (type, type);
1785 else
1786 tmin = TYPE_MIN_VALUE (type);
1787 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1788 tmax = upper_bound_in_type (type, type);
1789 else
1790 tmax = TYPE_MAX_VALUE (type);
1791
1792 /* Try to use estimated number of iterations for the loop to constrain the
1793 final value in the evolution. */
1794 if (TREE_CODE (step) == INTEGER_CST
1795 && is_gimple_val (init)
1796 && (TREE_CODE (init) != SSA_NAME
1797 || get_value_range (init)->kind () == VR_RANGE))
1798 {
1799 widest_int nit;
1800
1801 /* We are only entering here for loop header PHI nodes, so using
1802 the number of latch executions is the correct thing to use. */
1803 if (max_loop_iterations (loop, &nit))
1804 {
1805 value_range maxvr;
1806 signop sgn = TYPE_SIGN (TREE_TYPE (step));
1807 wi::overflow_type overflow;
1808
1809 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
1810 &overflow);
1811 /* If the multiplication overflowed we can't do a meaningful
1812 adjustment. Likewise if the result doesn't fit in the type
1813 of the induction variable. For a signed type we have to
1814 check whether the result has the expected signedness which
1815 is that of the step as number of iterations is unsigned. */
1816 if (!overflow
1817 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
1818 && (sgn == UNSIGNED
1819 || wi::gts_p (wtmp, 0) == wi::gts_p (wi::to_wide (step), 0)))
1820 {
1821 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
1822 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
1823 TREE_TYPE (init), init, tem);
1824 /* Likewise if the addition did. */
1825 if (maxvr.kind () == VR_RANGE)
1826 {
1827 value_range_base initvr;
1828
1829 if (TREE_CODE (init) == SSA_NAME)
1830 initvr = *(get_value_range (init));
1831 else if (is_gimple_min_invariant (init))
1832 initvr.set (init);
1833 else
1834 return;
1835
1836 /* Check if init + nit * step overflows. Though we checked
1837 scev {init, step}_loop doesn't wrap, it is not enough
1838 because the loop may exit immediately. Overflow could
1839 happen in the plus expression in this case. */
1840 if ((dir == EV_DIR_DECREASES
1841 && compare_values (maxvr.min (), initvr.min ()) != -1)
1842 || (dir == EV_DIR_GROWS
1843 && compare_values (maxvr.max (), initvr.max ()) != 1))
1844 return;
1845
1846 tmin = maxvr.min ();
1847 tmax = maxvr.max ();
1848 }
1849 }
1850 }
1851 }
1852
1853 if (vr->varying_p () || vr->undefined_p ())
1854 {
1855 min = tmin;
1856 max = tmax;
1857
1858 /* For VARYING or UNDEFINED ranges, just about anything we get
1859 from scalar evolutions should be better. */
1860
1861 if (dir == EV_DIR_DECREASES)
1862 max = init;
1863 else
1864 min = init;
1865 }
1866 else if (vr->kind () == VR_RANGE)
1867 {
1868 min = vr->min ();
1869 max = vr->max ();
1870
1871 if (dir == EV_DIR_DECREASES)
1872 {
1873 /* INIT is the maximum value. If INIT is lower than VR->MAX ()
1874 but no smaller than VR->MIN (), set VR->MAX () to INIT. */
1875 if (compare_values (init, max) == -1)
1876 max = init;
1877
1878 /* According to the loop information, the variable does not
1879 overflow. */
1880 if (compare_values (min, tmin) == -1)
1881 min = tmin;
1882
1883 }
1884 else
1885 {
1886 /* If INIT is bigger than VR->MIN (), set VR->MIN () to INIT. */
1887 if (compare_values (init, min) == 1)
1888 min = init;
1889
1890 if (compare_values (tmax, max) == -1)
1891 max = tmax;
1892 }
1893 }
1894 else
1895 return;
1896
1897 /* If we just created an invalid range with the minimum
1898 greater than the maximum, we fail conservatively.
1899 This should happen only in unreachable
1900 parts of code, or for invalid programs. */
1901 if (compare_values (min, max) == 1)
1902 return;
1903
1904 /* Even for valid range info, sometimes overflow flag will leak in.
1905 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1906 drop them. */
1907 if (TREE_OVERFLOW_P (min))
1908 min = drop_tree_overflow (min);
1909 if (TREE_OVERFLOW_P (max))
1910 max = drop_tree_overflow (max);
1911
1912 vr->update (VR_RANGE, min, max);
1913 }
1914
1915 /* Dump value ranges of all SSA_NAMEs to FILE. */
1916
1917 void
1918 vr_values::dump_all_value_ranges (FILE *file)
1919 {
1920 size_t i;
1921
1922 for (i = 0; i < num_vr_values; i++)
1923 {
1924 if (vr_value[i])
1925 {
1926 print_generic_expr (file, ssa_name (i));
1927 fprintf (file, ": ");
1928 dump_value_range (file, vr_value[i]);
1929 fprintf (file, "\n");
1930 }
1931 }
1932
1933 fprintf (file, "\n");
1934 }
1935
1936 /* Initialize VRP lattice. */
1937
1938 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1939 {
1940 values_propagated = false;
1941 num_vr_values = num_ssa_names;
1942 vr_value = XCNEWVEC (value_range *, num_vr_values);
1943 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
1944 bitmap_obstack_initialize (&vrp_equiv_obstack);
1945 to_remove_edges = vNULL;
1946 to_update_switch_stmts = vNULL;
1947 }
1948
1949 /* Free VRP lattice. */
1950
1951 vr_values::~vr_values ()
1952 {
1953 /* Free allocated memory. */
1954 free (vr_value);
1955 free (vr_phi_edge_counts);
1956 bitmap_obstack_release (&vrp_equiv_obstack);
1957 vrp_value_range_pool.release ();
1958
1959 /* So that we can distinguish between VRP data being available
1960 and not available. */
1961 vr_value = NULL;
1962 vr_phi_edge_counts = NULL;
1963
1964 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1965 then an EVRP client did not clean up properly. Catch it now rather
1966 than seeing something more obscure later. */
1967 gcc_assert (to_remove_edges.is_empty ()
1968 && to_update_switch_stmts.is_empty ());
1969 }
1970
1971
1972 /* A hack. */
1973 static class vr_values *x_vr_values;
1974
1975 /* Return the singleton value-range for NAME or NAME. */
1976
1977 static inline tree
1978 vrp_valueize (tree name)
1979 {
1980 if (TREE_CODE (name) == SSA_NAME)
1981 {
1982 const value_range *vr = x_vr_values->get_value_range (name);
1983 if (vr->kind () == VR_RANGE
1984 && (TREE_CODE (vr->min ()) == SSA_NAME
1985 || is_gimple_min_invariant (vr->min ()))
1986 && vrp_operand_equal_p (vr->min (), vr->max ()))
1987 return vr->min ();
1988 }
1989 return name;
1990 }
1991
1992 /* Return the singleton value-range for NAME if that is a constant
1993 but signal to not follow SSA edges. */
1994
1995 static inline tree
1996 vrp_valueize_1 (tree name)
1997 {
1998 if (TREE_CODE (name) == SSA_NAME)
1999 {
2000 /* If the definition may be simulated again we cannot follow
2001 this SSA edge as the SSA propagator does not necessarily
2002 re-visit the use. */
2003 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
2004 if (!gimple_nop_p (def_stmt)
2005 && prop_simulate_again_p (def_stmt))
2006 return NULL_TREE;
2007 const value_range *vr = x_vr_values->get_value_range (name);
2008 tree singleton;
2009 if (vr->singleton_p (&singleton))
2010 return singleton;
2011 }
2012 return name;
2013 }
2014
2015 /* Given STMT, an assignment or call, return its LHS if the type
2016 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
2017
2018 tree
2019 get_output_for_vrp (gimple *stmt)
2020 {
2021 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
2022 return NULL_TREE;
2023
2024 /* We only keep track of ranges in integral and pointer types. */
2025 tree lhs = gimple_get_lhs (stmt);
2026 if (TREE_CODE (lhs) == SSA_NAME
2027 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
2028 /* It is valid to have NULL MIN/MAX values on a type. See
2029 build_range_type. */
2030 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
2031 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
2032 || POINTER_TYPE_P (TREE_TYPE (lhs))))
2033 return lhs;
2034
2035 return NULL_TREE;
2036 }
2037
2038 /* Visit assignment STMT. If it produces an interesting range, record
2039 the range in VR and set LHS to OUTPUT_P. */
2040
2041 void
2042 vr_values::vrp_visit_assignment_or_call (gimple *stmt, tree *output_p,
2043 value_range *vr)
2044 {
2045 tree lhs = get_output_for_vrp (stmt);
2046 *output_p = lhs;
2047
2048 /* We only keep track of ranges in integral and pointer types. */
2049 if (lhs)
2050 {
2051 enum gimple_code code = gimple_code (stmt);
2052
2053 /* Try folding the statement to a constant first. */
2054 x_vr_values = this;
2055 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
2056 vrp_valueize_1);
2057 x_vr_values = NULL;
2058 if (tem)
2059 {
2060 if (TREE_CODE (tem) == SSA_NAME
2061 && (SSA_NAME_IS_DEFAULT_DEF (tem)
2062 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
2063 {
2064 extract_range_from_ssa_name (vr, tem);
2065 return;
2066 }
2067 else if (is_gimple_min_invariant (tem))
2068 {
2069 vr->set (tem);
2070 return;
2071 }
2072 }
2073 /* Then dispatch to value-range extracting functions. */
2074 if (code == GIMPLE_CALL)
2075 extract_range_basic (vr, stmt);
2076 else
2077 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
2078 }
2079 }
2080
2081 /* Helper that gets the value range of the SSA_NAME with version I
2082 or a symbolic range containing the SSA_NAME only if the value range
2083 is varying or undefined. Uses TEM as storage for the alternate range. */
2084
2085 const value_range *
2086 vr_values::get_vr_for_comparison (int i, value_range *tem)
2087 {
2088 /* Shallow-copy equiv bitmap. */
2089 const value_range *vr = get_value_range (ssa_name (i));
2090
2091 /* If name N_i does not have a valid range, use N_i as its own
2092 range. This allows us to compare against names that may
2093 have N_i in their ranges. */
2094 if (vr->varying_p () || vr->undefined_p ())
2095 {
2096 tem->set (ssa_name (i));
2097 return tem;
2098 }
2099
2100 return vr;
2101 }
2102
2103 /* Compare all the value ranges for names equivalent to VAR with VAL
2104 using comparison code COMP. Return the same value returned by
2105 compare_range_with_value, including the setting of
2106 *STRICT_OVERFLOW_P. */
2107
2108 tree
2109 vr_values::compare_name_with_value (enum tree_code comp, tree var, tree val,
2110 bool *strict_overflow_p, bool use_equiv_p)
2111 {
2112 bitmap_iterator bi;
2113 unsigned i;
2114 bitmap e;
2115 tree retval, t;
2116 int used_strict_overflow;
2117 bool sop;
2118 const value_range *equiv_vr;
2119 value_range tem_vr;
2120
2121 /* Get the set of equivalences for VAR. */
2122 e = get_value_range (var)->equiv ();
2123
2124 /* Start at -1. Set it to 0 if we do a comparison without relying
2125 on overflow, or 1 if all comparisons rely on overflow. */
2126 used_strict_overflow = -1;
2127
2128 /* Compare vars' value range with val. */
2129 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var), &tem_vr);
2130 sop = false;
2131 retval = compare_range_with_value (comp, equiv_vr, val, &sop);
2132 if (retval)
2133 used_strict_overflow = sop ? 1 : 0;
2134
2135 /* If the equiv set is empty we have done all work we need to do. */
2136 if (e == NULL)
2137 {
2138 if (retval
2139 && used_strict_overflow > 0)
2140 *strict_overflow_p = true;
2141 return retval;
2142 }
2143
2144 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
2145 {
2146 tree name = ssa_name (i);
2147 if (! name)
2148 continue;
2149
2150 if (! use_equiv_p
2151 && ! SSA_NAME_IS_DEFAULT_DEF (name)
2152 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
2153 continue;
2154
2155 equiv_vr = get_vr_for_comparison (i, &tem_vr);
2156 sop = false;
2157 t = compare_range_with_value (comp, equiv_vr, val, &sop);
2158 if (t)
2159 {
2160 /* If we get different answers from different members
2161 of the equivalence set this check must be in a dead
2162 code region. Folding it to a trap representation
2163 would be correct here. For now just return don't-know. */
2164 if (retval != NULL
2165 && t != retval)
2166 {
2167 retval = NULL_TREE;
2168 break;
2169 }
2170 retval = t;
2171
2172 if (!sop)
2173 used_strict_overflow = 0;
2174 else if (used_strict_overflow < 0)
2175 used_strict_overflow = 1;
2176 }
2177 }
2178
2179 if (retval
2180 && used_strict_overflow > 0)
2181 *strict_overflow_p = true;
2182
2183 return retval;
2184 }
2185
2186
2187 /* Given a comparison code COMP and names N1 and N2, compare all the
2188 ranges equivalent to N1 against all the ranges equivalent to N2
2189 to determine the value of N1 COMP N2. Return the same value
2190 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2191 whether we relied on undefined signed overflow in the comparison. */
2192
2193
2194 tree
2195 vr_values::compare_names (enum tree_code comp, tree n1, tree n2,
2196 bool *strict_overflow_p)
2197 {
2198 tree t, retval;
2199 bitmap e1, e2;
2200 bitmap_iterator bi1, bi2;
2201 unsigned i1, i2;
2202 int used_strict_overflow;
2203 static bitmap_obstack *s_obstack = NULL;
2204 static bitmap s_e1 = NULL, s_e2 = NULL;
2205
2206 /* Compare the ranges of every name equivalent to N1 against the
2207 ranges of every name equivalent to N2. */
2208 e1 = get_value_range (n1)->equiv ();
2209 e2 = get_value_range (n2)->equiv ();
2210
2211 /* Use the fake bitmaps if e1 or e2 are not available. */
2212 if (s_obstack == NULL)
2213 {
2214 s_obstack = XNEW (bitmap_obstack);
2215 bitmap_obstack_initialize (s_obstack);
2216 s_e1 = BITMAP_ALLOC (s_obstack);
2217 s_e2 = BITMAP_ALLOC (s_obstack);
2218 }
2219 if (e1 == NULL)
2220 e1 = s_e1;
2221 if (e2 == NULL)
2222 e2 = s_e2;
2223
2224 /* Add N1 and N2 to their own set of equivalences to avoid
2225 duplicating the body of the loop just to check N1 and N2
2226 ranges. */
2227 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
2228 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
2229
2230 /* If the equivalence sets have a common intersection, then the two
2231 names can be compared without checking their ranges. */
2232 if (bitmap_intersect_p (e1, e2))
2233 {
2234 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2235 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2236
2237 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
2238 ? boolean_true_node
2239 : boolean_false_node;
2240 }
2241
2242 /* Start at -1. Set it to 0 if we do a comparison without relying
2243 on overflow, or 1 if all comparisons rely on overflow. */
2244 used_strict_overflow = -1;
2245
2246 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2247 N2 to their own set of equivalences to avoid duplicating the body
2248 of the loop just to check N1 and N2 ranges. */
2249 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
2250 {
2251 if (! ssa_name (i1))
2252 continue;
2253
2254 value_range tem_vr1;
2255 const value_range *vr1 = get_vr_for_comparison (i1, &tem_vr1);
2256
2257 t = retval = NULL_TREE;
2258 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
2259 {
2260 if (! ssa_name (i2))
2261 continue;
2262
2263 bool sop = false;
2264
2265 value_range tem_vr2;
2266 const value_range *vr2 = get_vr_for_comparison (i2, &tem_vr2);
2267
2268 t = compare_ranges (comp, vr1, vr2, &sop);
2269 if (t)
2270 {
2271 /* If we get different answers from different members
2272 of the equivalence set this check must be in a dead
2273 code region. Folding it to a trap representation
2274 would be correct here. For now just return don't-know. */
2275 if (retval != NULL
2276 && t != retval)
2277 {
2278 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2279 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2280 return NULL_TREE;
2281 }
2282 retval = t;
2283
2284 if (!sop)
2285 used_strict_overflow = 0;
2286 else if (used_strict_overflow < 0)
2287 used_strict_overflow = 1;
2288 }
2289 }
2290
2291 if (retval)
2292 {
2293 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2294 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2295 if (used_strict_overflow > 0)
2296 *strict_overflow_p = true;
2297 return retval;
2298 }
2299 }
2300
2301 /* None of the equivalent ranges are useful in computing this
2302 comparison. */
2303 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
2304 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
2305 return NULL_TREE;
2306 }
2307
2308 /* Helper function for vrp_evaluate_conditional_warnv & other
2309 optimizers. */
2310
2311 tree
2312 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2313 (enum tree_code code, tree op0, tree op1, bool * strict_overflow_p)
2314 {
2315 const value_range *vr0, *vr1;
2316
2317 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
2318 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
2319
2320 tree res = NULL_TREE;
2321 if (vr0 && vr1)
2322 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
2323 if (!res && vr0)
2324 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
2325 if (!res && vr1)
2326 res = (compare_range_with_value
2327 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
2328 return res;
2329 }
2330
2331 /* Helper function for vrp_evaluate_conditional_warnv. */
2332
2333 tree
2334 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code,
2335 tree op0, tree op1,
2336 bool use_equiv_p,
2337 bool *strict_overflow_p,
2338 bool *only_ranges)
2339 {
2340 tree ret;
2341 if (only_ranges)
2342 *only_ranges = true;
2343
2344 /* We only deal with integral and pointer types. */
2345 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
2346 && !POINTER_TYPE_P (TREE_TYPE (op0)))
2347 return NULL_TREE;
2348
2349 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2350 as a simple equality test, then prefer that over its current form
2351 for evaluation.
2352
2353 An overflow test which collapses to an equality test can always be
2354 expressed as a comparison of one argument against zero. Overflow
2355 occurs when the chosen argument is zero and does not occur if the
2356 chosen argument is not zero. */
2357 tree x;
2358 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
2359 {
2360 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
2361 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2362 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2363 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2364 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2365 if (integer_zerop (x))
2366 {
2367 op1 = x;
2368 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
2369 }
2370 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2371 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2372 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2373 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2374 else if (wi::to_wide (x) == max - 1)
2375 {
2376 op0 = op1;
2377 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
2378 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
2379 }
2380 else
2381 {
2382 value_range_base vro, vri;
2383 if (code == GT_EXPR || code == GE_EXPR)
2384 {
2385 vro.set (VR_ANTI_RANGE, TYPE_MIN_VALUE (TREE_TYPE (op0)), x);
2386 vri.set (VR_RANGE, TYPE_MIN_VALUE (TREE_TYPE (op0)), x);
2387 }
2388 else if (code == LT_EXPR || code == LE_EXPR)
2389 {
2390 vro.set (VR_RANGE, TYPE_MIN_VALUE (TREE_TYPE (op0)), x);
2391 vri.set (VR_ANTI_RANGE, TYPE_MIN_VALUE (TREE_TYPE (op0)), x);
2392 }
2393 else
2394 gcc_unreachable ();
2395 const value_range *vr0 = get_value_range (op0);
2396 /* If vro, the range for OP0 to pass the overflow test, has
2397 no intersection with *vr0, OP0's known range, then the
2398 overflow test can't pass, so return the node for false.
2399 If it is the inverted range, vri, that has no
2400 intersection, then the overflow test must pass, so return
2401 the node for true. In other cases, we could proceed with
2402 a simplified condition comparing OP0 and X, with LE_EXPR
2403 for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
2404 the comments next to the enclosing if suggest it's not
2405 generally profitable to do so. */
2406 vro.intersect (vr0);
2407 if (vro.undefined_p ())
2408 return boolean_false_node;
2409 vri.intersect (vr0);
2410 if (vri.undefined_p ())
2411 return boolean_true_node;
2412 }
2413 }
2414
2415 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
2416 (code, op0, op1, strict_overflow_p)))
2417 return ret;
2418 if (only_ranges)
2419 *only_ranges = false;
2420 /* Do not use compare_names during propagation, it's quadratic. */
2421 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
2422 && use_equiv_p)
2423 return compare_names (code, op0, op1, strict_overflow_p);
2424 else if (TREE_CODE (op0) == SSA_NAME)
2425 return compare_name_with_value (code, op0, op1,
2426 strict_overflow_p, use_equiv_p);
2427 else if (TREE_CODE (op1) == SSA_NAME)
2428 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
2429 strict_overflow_p, use_equiv_p);
2430 return NULL_TREE;
2431 }
2432
2433 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2434 information. Return NULL if the conditional cannot be evaluated.
2435 The ranges of all the names equivalent with the operands in COND
2436 will be used when trying to compute the value. If the result is
2437 based on undefined signed overflow, issue a warning if
2438 appropriate. */
2439
2440 tree
2441 vr_values::vrp_evaluate_conditional (tree_code code, tree op0,
2442 tree op1, gimple *stmt)
2443 {
2444 bool sop;
2445 tree ret;
2446 bool only_ranges;
2447
2448 /* Some passes and foldings leak constants with overflow flag set
2449 into the IL. Avoid doing wrong things with these and bail out. */
2450 if ((TREE_CODE (op0) == INTEGER_CST
2451 && TREE_OVERFLOW (op0))
2452 || (TREE_CODE (op1) == INTEGER_CST
2453 && TREE_OVERFLOW (op1)))
2454 return NULL_TREE;
2455
2456 sop = false;
2457 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
2458 &only_ranges);
2459
2460 if (ret && sop)
2461 {
2462 enum warn_strict_overflow_code wc;
2463 const char* warnmsg;
2464
2465 if (is_gimple_min_invariant (ret))
2466 {
2467 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
2468 warnmsg = G_("assuming signed overflow does not occur when "
2469 "simplifying conditional to constant");
2470 }
2471 else
2472 {
2473 wc = WARN_STRICT_OVERFLOW_COMPARISON;
2474 warnmsg = G_("assuming signed overflow does not occur when "
2475 "simplifying conditional");
2476 }
2477
2478 if (issue_strict_overflow_warning (wc))
2479 {
2480 location_t location;
2481
2482 if (!gimple_has_location (stmt))
2483 location = input_location;
2484 else
2485 location = gimple_location (stmt);
2486 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
2487 }
2488 }
2489
2490 if (warn_type_limits
2491 && ret && only_ranges
2492 && TREE_CODE_CLASS (code) == tcc_comparison
2493 && TREE_CODE (op0) == SSA_NAME)
2494 {
2495 /* If the comparison is being folded and the operand on the LHS
2496 is being compared against a constant value that is outside of
2497 the natural range of OP0's type, then the predicate will
2498 always fold regardless of the value of OP0. If -Wtype-limits
2499 was specified, emit a warning. */
2500 tree type = TREE_TYPE (op0);
2501 const value_range *vr0 = get_value_range (op0);
2502
2503 if (vr0->kind () == VR_RANGE
2504 && INTEGRAL_TYPE_P (type)
2505 && vrp_val_is_min (vr0->min ())
2506 && vrp_val_is_max (vr0->max ())
2507 && is_gimple_min_invariant (op1))
2508 {
2509 location_t location;
2510
2511 if (!gimple_has_location (stmt))
2512 location = input_location;
2513 else
2514 location = gimple_location (stmt);
2515
2516 warning_at (location, OPT_Wtype_limits,
2517 integer_zerop (ret)
2518 ? G_("comparison always false "
2519 "due to limited range of data type")
2520 : G_("comparison always true "
2521 "due to limited range of data type"));
2522 }
2523 }
2524
2525 return ret;
2526 }
2527
2528
2529 /* Visit conditional statement STMT. If we can determine which edge
2530 will be taken out of STMT's basic block, record it in
2531 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2532
2533 void
2534 vr_values::vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
2535 {
2536 tree val;
2537
2538 *taken_edge_p = NULL;
2539
2540 if (dump_file && (dump_flags & TDF_DETAILS))
2541 {
2542 tree use;
2543 ssa_op_iter i;
2544
2545 fprintf (dump_file, "\nVisiting conditional with predicate: ");
2546 print_gimple_stmt (dump_file, stmt, 0);
2547 fprintf (dump_file, "\nWith known ranges\n");
2548
2549 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
2550 {
2551 fprintf (dump_file, "\t");
2552 print_generic_expr (dump_file, use);
2553 fprintf (dump_file, ": ");
2554 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
2555 }
2556
2557 fprintf (dump_file, "\n");
2558 }
2559
2560 /* Compute the value of the predicate COND by checking the known
2561 ranges of each of its operands.
2562
2563 Note that we cannot evaluate all the equivalent ranges here
2564 because those ranges may not yet be final and with the current
2565 propagation strategy, we cannot determine when the value ranges
2566 of the names in the equivalence set have changed.
2567
2568 For instance, given the following code fragment
2569
2570 i_5 = PHI <8, i_13>
2571 ...
2572 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2573 if (i_14 == 1)
2574 ...
2575
2576 Assume that on the first visit to i_14, i_5 has the temporary
2577 range [8, 8] because the second argument to the PHI function is
2578 not yet executable. We derive the range ~[0, 0] for i_14 and the
2579 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2580 the first time, since i_14 is equivalent to the range [8, 8], we
2581 determine that the predicate is always false.
2582
2583 On the next round of propagation, i_13 is determined to be
2584 VARYING, which causes i_5 to drop down to VARYING. So, another
2585 visit to i_14 is scheduled. In this second visit, we compute the
2586 exact same range and equivalence set for i_14, namely ~[0, 0] and
2587 { i_5 }. But we did not have the previous range for i_5
2588 registered, so vrp_visit_assignment thinks that the range for
2589 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2590 is not visited again, which stops propagation from visiting
2591 statements in the THEN clause of that if().
2592
2593 To properly fix this we would need to keep the previous range
2594 value for the names in the equivalence set. This way we would've
2595 discovered that from one visit to the other i_5 changed from
2596 range [8, 8] to VR_VARYING.
2597
2598 However, fixing this apparent limitation may not be worth the
2599 additional checking. Testing on several code bases (GCC, DLV,
2600 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2601 4 more predicates folded in SPEC. */
2602
2603 bool sop;
2604 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
2605 gimple_cond_lhs (stmt),
2606 gimple_cond_rhs (stmt),
2607 false, &sop, NULL);
2608 if (val)
2609 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
2610
2611 if (dump_file && (dump_flags & TDF_DETAILS))
2612 {
2613 fprintf (dump_file, "\nPredicate evaluates to: ");
2614 if (val == NULL_TREE)
2615 fprintf (dump_file, "DON'T KNOW\n");
2616 else
2617 print_generic_stmt (dump_file, val);
2618 }
2619 }
2620
2621 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2622 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2623 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2624 Returns true if the default label is not needed. */
2625
2626 static bool
2627 find_case_label_ranges (gswitch *stmt, const value_range *vr, size_t *min_idx1,
2628 size_t *max_idx1, size_t *min_idx2,
2629 size_t *max_idx2)
2630 {
2631 size_t i, j, k, l;
2632 unsigned int n = gimple_switch_num_labels (stmt);
2633 bool take_default;
2634 tree case_low, case_high;
2635 tree min = vr->min (), max = vr->max ();
2636
2637 gcc_checking_assert (!vr->varying_p () && !vr->undefined_p ());
2638
2639 take_default = !find_case_label_range (stmt, min, max, &i, &j);
2640
2641 /* Set second range to empty. */
2642 *min_idx2 = 1;
2643 *max_idx2 = 0;
2644
2645 if (vr->kind () == VR_RANGE)
2646 {
2647 *min_idx1 = i;
2648 *max_idx1 = j;
2649 return !take_default;
2650 }
2651
2652 /* Set first range to all case labels. */
2653 *min_idx1 = 1;
2654 *max_idx1 = n - 1;
2655
2656 if (i > j)
2657 return false;
2658
2659 /* Make sure all the values of case labels [i , j] are contained in
2660 range [MIN, MAX]. */
2661 case_low = CASE_LOW (gimple_switch_label (stmt, i));
2662 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
2663 if (tree_int_cst_compare (case_low, min) < 0)
2664 i += 1;
2665 if (case_high != NULL_TREE
2666 && tree_int_cst_compare (max, case_high) < 0)
2667 j -= 1;
2668
2669 if (i > j)
2670 return false;
2671
2672 /* If the range spans case labels [i, j], the corresponding anti-range spans
2673 the labels [1, i - 1] and [j + 1, n - 1]. */
2674 k = j + 1;
2675 l = n - 1;
2676 if (k > l)
2677 {
2678 k = 1;
2679 l = 0;
2680 }
2681
2682 j = i - 1;
2683 i = 1;
2684 if (i > j)
2685 {
2686 i = k;
2687 j = l;
2688 k = 1;
2689 l = 0;
2690 }
2691
2692 *min_idx1 = i;
2693 *max_idx1 = j;
2694 *min_idx2 = k;
2695 *max_idx2 = l;
2696 return false;
2697 }
2698
2699 /* Visit switch statement STMT. If we can determine which edge
2700 will be taken out of STMT's basic block, record it in
2701 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2702
2703 void
2704 vr_values::vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
2705 {
2706 tree op, val;
2707 const value_range *vr;
2708 size_t i = 0, j = 0, k, l;
2709 bool take_default;
2710
2711 *taken_edge_p = NULL;
2712 op = gimple_switch_index (stmt);
2713 if (TREE_CODE (op) != SSA_NAME)
2714 return;
2715
2716 vr = get_value_range (op);
2717 if (dump_file && (dump_flags & TDF_DETAILS))
2718 {
2719 fprintf (dump_file, "\nVisiting switch expression with operand ");
2720 print_generic_expr (dump_file, op);
2721 fprintf (dump_file, " with known range ");
2722 dump_value_range (dump_file, vr);
2723 fprintf (dump_file, "\n");
2724 }
2725
2726 if (vr->undefined_p ()
2727 || vr->varying_p ()
2728 || vr->symbolic_p ())
2729 return;
2730
2731 /* Find the single edge that is taken from the switch expression. */
2732 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
2733
2734 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2735 label */
2736 if (j < i)
2737 {
2738 gcc_assert (take_default);
2739 val = gimple_switch_default_label (stmt);
2740 }
2741 else
2742 {
2743 /* Check if labels with index i to j and maybe the default label
2744 are all reaching the same label. */
2745
2746 val = gimple_switch_label (stmt, i);
2747 if (take_default
2748 && CASE_LABEL (gimple_switch_default_label (stmt))
2749 != CASE_LABEL (val))
2750 {
2751 if (dump_file && (dump_flags & TDF_DETAILS))
2752 fprintf (dump_file, " not a single destination for this "
2753 "range\n");
2754 return;
2755 }
2756 for (++i; i <= j; ++i)
2757 {
2758 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
2759 {
2760 if (dump_file && (dump_flags & TDF_DETAILS))
2761 fprintf (dump_file, " not a single destination for this "
2762 "range\n");
2763 return;
2764 }
2765 }
2766 for (; k <= l; ++k)
2767 {
2768 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
2769 {
2770 if (dump_file && (dump_flags & TDF_DETAILS))
2771 fprintf (dump_file, " not a single destination for this "
2772 "range\n");
2773 return;
2774 }
2775 }
2776 }
2777
2778 *taken_edge_p = find_edge (gimple_bb (stmt),
2779 label_to_block (cfun, CASE_LABEL (val)));
2780
2781 if (dump_file && (dump_flags & TDF_DETAILS))
2782 {
2783 fprintf (dump_file, " will take edge to ");
2784 print_generic_stmt (dump_file, CASE_LABEL (val));
2785 }
2786 }
2787
2788
2789 /* Evaluate statement STMT. If the statement produces a useful range,
2790 set VR and corepsponding OUTPUT_P.
2791
2792 If STMT is a conditional branch and we can determine its truth
2793 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2794
2795 void
2796 vr_values::extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
2797 tree *output_p, value_range *vr)
2798 {
2799
2800 if (dump_file && (dump_flags & TDF_DETAILS))
2801 {
2802 fprintf (dump_file, "\nVisiting statement:\n");
2803 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2804 }
2805
2806 if (!stmt_interesting_for_vrp (stmt))
2807 gcc_assert (stmt_ends_bb_p (stmt));
2808 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
2809 vrp_visit_assignment_or_call (stmt, output_p, vr);
2810 else if (gimple_code (stmt) == GIMPLE_COND)
2811 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
2812 else if (gimple_code (stmt) == GIMPLE_SWITCH)
2813 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
2814 }
2815
2816 /* Visit all arguments for PHI node PHI that flow through executable
2817 edges. If a valid value range can be derived from all the incoming
2818 value ranges, set a new range in VR_RESULT. */
2819
2820 void
2821 vr_values::extract_range_from_phi_node (gphi *phi, value_range *vr_result)
2822 {
2823 size_t i;
2824 tree lhs = PHI_RESULT (phi);
2825 const value_range *lhs_vr = get_value_range (lhs);
2826 bool first = true;
2827 int edges, old_edges;
2828 class loop *l;
2829
2830 if (dump_file && (dump_flags & TDF_DETAILS))
2831 {
2832 fprintf (dump_file, "\nVisiting PHI node: ");
2833 print_gimple_stmt (dump_file, phi, 0, dump_flags);
2834 }
2835
2836 bool may_simulate_backedge_again = false;
2837 edges = 0;
2838 for (i = 0; i < gimple_phi_num_args (phi); i++)
2839 {
2840 edge e = gimple_phi_arg_edge (phi, i);
2841
2842 if (dump_file && (dump_flags & TDF_DETAILS))
2843 {
2844 fprintf (dump_file,
2845 " Argument #%d (%d -> %d %sexecutable)\n",
2846 (int) i, e->src->index, e->dest->index,
2847 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
2848 }
2849
2850 if (e->flags & EDGE_EXECUTABLE)
2851 {
2852 tree arg = PHI_ARG_DEF (phi, i);
2853 value_range vr_arg_tem;
2854 const value_range *vr_arg = &vr_arg_tem;
2855
2856 ++edges;
2857
2858 if (TREE_CODE (arg) == SSA_NAME)
2859 {
2860 /* See if we are eventually going to change one of the args. */
2861 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
2862 if (! gimple_nop_p (def_stmt)
2863 && prop_simulate_again_p (def_stmt)
2864 && e->flags & EDGE_DFS_BACK)
2865 may_simulate_backedge_again = true;
2866
2867 const value_range *vr_arg_ = get_value_range (arg);
2868 /* Do not allow equivalences or symbolic ranges to leak in from
2869 backedges. That creates invalid equivalencies.
2870 See PR53465 and PR54767. */
2871 if (e->flags & EDGE_DFS_BACK)
2872 {
2873 if (!vr_arg_->varying_p () && !vr_arg_->undefined_p ())
2874 {
2875 vr_arg_tem.set (vr_arg_->kind (), vr_arg_->min (),
2876 vr_arg_->max (), NULL);
2877 if (vr_arg_tem.symbolic_p ())
2878 vr_arg_tem.set_varying ();
2879 }
2880 else
2881 vr_arg = vr_arg_;
2882 }
2883 /* If the non-backedge arguments range is VR_VARYING then
2884 we can still try recording a simple equivalence. */
2885 else if (vr_arg_->varying_p ())
2886 vr_arg_tem.set (arg);
2887 else
2888 vr_arg = vr_arg_;
2889 }
2890 else
2891 {
2892 if (TREE_OVERFLOW_P (arg))
2893 arg = drop_tree_overflow (arg);
2894
2895 vr_arg_tem.set (arg);
2896 }
2897
2898 if (dump_file && (dump_flags & TDF_DETAILS))
2899 {
2900 fprintf (dump_file, "\t");
2901 print_generic_expr (dump_file, arg, dump_flags);
2902 fprintf (dump_file, ": ");
2903 dump_value_range (dump_file, vr_arg);
2904 fprintf (dump_file, "\n");
2905 }
2906
2907 if (first)
2908 vr_result->deep_copy (vr_arg);
2909 else
2910 vr_result->union_ (vr_arg);
2911 first = false;
2912
2913 if (vr_result->varying_p ())
2914 break;
2915 }
2916 }
2917
2918 if (vr_result->varying_p ())
2919 goto varying;
2920 else if (vr_result->undefined_p ())
2921 goto update_range;
2922
2923 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
2924 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
2925
2926 /* To prevent infinite iterations in the algorithm, derive ranges
2927 when the new value is slightly bigger or smaller than the
2928 previous one. We don't do this if we have seen a new executable
2929 edge; this helps us avoid an infinity for conditionals
2930 which are not in a loop. If the old value-range was VR_UNDEFINED
2931 use the updated range and iterate one more time. If we will not
2932 simulate this PHI again via the backedge allow us to iterate. */
2933 if (edges > 0
2934 && gimple_phi_num_args (phi) > 1
2935 && edges == old_edges
2936 && !lhs_vr->undefined_p ()
2937 && may_simulate_backedge_again)
2938 {
2939 /* Compare old and new ranges, fall back to varying if the
2940 values are not comparable. */
2941 int cmp_min = compare_values (lhs_vr->min (), vr_result->min ());
2942 if (cmp_min == -2)
2943 goto varying;
2944 int cmp_max = compare_values (lhs_vr->max (), vr_result->max ());
2945 if (cmp_max == -2)
2946 goto varying;
2947
2948 /* For non VR_RANGE or for pointers fall back to varying if
2949 the range changed. */
2950 if ((lhs_vr->kind () != VR_RANGE || vr_result->kind () != VR_RANGE
2951 || POINTER_TYPE_P (TREE_TYPE (lhs)))
2952 && (cmp_min != 0 || cmp_max != 0))
2953 goto varying;
2954
2955 /* If the new minimum is larger than the previous one
2956 retain the old value. If the new minimum value is smaller
2957 than the previous one and not -INF go all the way to -INF + 1.
2958 In the first case, to avoid infinite bouncing between different
2959 minimums, and in the other case to avoid iterating millions of
2960 times to reach -INF. Going to -INF + 1 also lets the following
2961 iteration compute whether there will be any overflow, at the
2962 expense of one additional iteration. */
2963 tree new_min = vr_result->min ();
2964 tree new_max = vr_result->max ();
2965 if (cmp_min < 0)
2966 new_min = lhs_vr->min ();
2967 else if (cmp_min > 0
2968 && (TREE_CODE (vr_result->min ()) != INTEGER_CST
2969 || tree_int_cst_lt (vrp_val_min (vr_result->type ()),
2970 vr_result->min ())))
2971 new_min = int_const_binop (PLUS_EXPR,
2972 vrp_val_min (vr_result->type ()),
2973 build_int_cst (vr_result->type (), 1));
2974
2975 /* Similarly for the maximum value. */
2976 if (cmp_max > 0)
2977 new_max = lhs_vr->max ();
2978 else if (cmp_max < 0
2979 && (TREE_CODE (vr_result->max ()) != INTEGER_CST
2980 || tree_int_cst_lt (vr_result->max (),
2981 vrp_val_max (vr_result->type ()))))
2982 new_max = int_const_binop (MINUS_EXPR,
2983 vrp_val_max (vr_result->type ()),
2984 build_int_cst (vr_result->type (), 1));
2985
2986 vr_result->update (vr_result->kind (), new_min, new_max);
2987
2988 /* If we dropped either bound to +-INF then if this is a loop
2989 PHI node SCEV may known more about its value-range. */
2990 if (cmp_min > 0 || cmp_min < 0
2991 || cmp_max < 0 || cmp_max > 0)
2992 goto scev_check;
2993
2994 goto infinite_check;
2995 }
2996
2997 goto update_range;
2998
2999 varying:
3000 vr_result->set_varying ();
3001
3002 scev_check:
3003 /* If this is a loop PHI node SCEV may known more about its value-range.
3004 scev_check can be reached from two paths, one is a fall through from above
3005 "varying" label, the other is direct goto from code block which tries to
3006 avoid infinite simulation. */
3007 if (scev_initialized_p ()
3008 && (l = loop_containing_stmt (phi))
3009 && l->header == gimple_bb (phi))
3010 adjust_range_with_scev (vr_result, l, phi, lhs);
3011
3012 infinite_check:
3013 /* If we will end up with a (-INF, +INF) range, set it to
3014 VARYING. Same if the previous max value was invalid for
3015 the type and we end up with vr_result.min > vr_result.max. */
3016 if ((!vr_result->varying_p () && !vr_result->undefined_p ())
3017 && !((vrp_val_is_max (vr_result->max ()) && vrp_val_is_min (vr_result->min ()))
3018 || compare_values (vr_result->min (), vr_result->max ()) > 0))
3019 ;
3020 else
3021 vr_result->set_varying ();
3022
3023 /* If the new range is different than the previous value, keep
3024 iterating. */
3025 update_range:
3026 return;
3027 }
3028
3029 /* Simplify boolean operations if the source is known
3030 to be already a boolean. */
3031 bool
3032 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi,
3033 gimple *stmt)
3034 {
3035 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3036 tree lhs, op0, op1;
3037 bool need_conversion;
3038
3039 /* We handle only !=/== case here. */
3040 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
3041
3042 op0 = gimple_assign_rhs1 (stmt);
3043 if (!op_with_boolean_value_range_p (op0))
3044 return false;
3045
3046 op1 = gimple_assign_rhs2 (stmt);
3047 if (!op_with_boolean_value_range_p (op1))
3048 return false;
3049
3050 /* Reduce number of cases to handle to NE_EXPR. As there is no
3051 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
3052 if (rhs_code == EQ_EXPR)
3053 {
3054 if (TREE_CODE (op1) == INTEGER_CST)
3055 op1 = int_const_binop (BIT_XOR_EXPR, op1,
3056 build_int_cst (TREE_TYPE (op1), 1));
3057 else
3058 return false;
3059 }
3060
3061 lhs = gimple_assign_lhs (stmt);
3062 need_conversion
3063 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
3064
3065 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
3066 if (need_conversion
3067 && !TYPE_UNSIGNED (TREE_TYPE (op0))
3068 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
3069 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
3070 return false;
3071
3072 /* For A != 0 we can substitute A itself. */
3073 if (integer_zerop (op1))
3074 gimple_assign_set_rhs_with_ops (gsi,
3075 need_conversion
3076 ? NOP_EXPR : TREE_CODE (op0), op0);
3077 /* For A != B we substitute A ^ B. Either with conversion. */
3078 else if (need_conversion)
3079 {
3080 tree tem = make_ssa_name (TREE_TYPE (op0));
3081 gassign *newop
3082 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
3083 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
3084 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
3085 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
3086 set_range_info (tem, VR_RANGE,
3087 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
3088 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
3089 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
3090 }
3091 /* Or without. */
3092 else
3093 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
3094 update_stmt (gsi_stmt (*gsi));
3095 fold_stmt (gsi, follow_single_use_edges);
3096
3097 return true;
3098 }
3099
3100 /* Simplify a division or modulo operator to a right shift or bitwise and
3101 if the first operand is unsigned or is greater than zero and the second
3102 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3103 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3104 optimize it into just op0 if op0's range is known to be a subset of
3105 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3106 modulo. */
3107
3108 bool
3109 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi,
3110 gimple *stmt)
3111 {
3112 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
3113 tree val = NULL;
3114 tree op0 = gimple_assign_rhs1 (stmt);
3115 tree op1 = gimple_assign_rhs2 (stmt);
3116 tree op0min = NULL_TREE, op0max = NULL_TREE;
3117 tree op1min = op1;
3118 const value_range *vr = NULL;
3119
3120 if (TREE_CODE (op0) == INTEGER_CST)
3121 {
3122 op0min = op0;
3123 op0max = op0;
3124 }
3125 else
3126 {
3127 vr = get_value_range (op0);
3128 if (range_int_cst_p (vr))
3129 {
3130 op0min = vr->min ();
3131 op0max = vr->max ();
3132 }
3133 }
3134
3135 if (rhs_code == TRUNC_MOD_EXPR
3136 && TREE_CODE (op1) == SSA_NAME)
3137 {
3138 const value_range *vr1 = get_value_range (op1);
3139 if (range_int_cst_p (vr1))
3140 op1min = vr1->min ();
3141 }
3142 if (rhs_code == TRUNC_MOD_EXPR
3143 && TREE_CODE (op1min) == INTEGER_CST
3144 && tree_int_cst_sgn (op1min) == 1
3145 && op0max
3146 && tree_int_cst_lt (op0max, op1min))
3147 {
3148 if (TYPE_UNSIGNED (TREE_TYPE (op0))
3149 || tree_int_cst_sgn (op0min) >= 0
3150 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
3151 op0min))
3152 {
3153 /* If op0 already has the range op0 % op1 has,
3154 then TRUNC_MOD_EXPR won't change anything. */
3155 gimple_assign_set_rhs_from_tree (gsi, op0);
3156 return true;
3157 }
3158 }
3159
3160 if (TREE_CODE (op0) != SSA_NAME)
3161 return false;
3162
3163 if (!integer_pow2p (op1))
3164 {
3165 /* X % -Y can be only optimized into X % Y either if
3166 X is not INT_MIN, or Y is not -1. Fold it now, as after
3167 remove_range_assertions the range info might be not available
3168 anymore. */
3169 if (rhs_code == TRUNC_MOD_EXPR
3170 && fold_stmt (gsi, follow_single_use_edges))
3171 return true;
3172 return false;
3173 }
3174
3175 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
3176 val = integer_one_node;
3177 else
3178 {
3179 bool sop = false;
3180
3181 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
3182
3183 if (val
3184 && sop
3185 && integer_onep (val)
3186 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3187 {
3188 location_t location;
3189
3190 if (!gimple_has_location (stmt))
3191 location = input_location;
3192 else
3193 location = gimple_location (stmt);
3194 warning_at (location, OPT_Wstrict_overflow,
3195 "assuming signed overflow does not occur when "
3196 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3197 }
3198 }
3199
3200 if (val && integer_onep (val))
3201 {
3202 tree t;
3203
3204 if (rhs_code == TRUNC_DIV_EXPR)
3205 {
3206 t = build_int_cst (integer_type_node, tree_log2 (op1));
3207 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
3208 gimple_assign_set_rhs1 (stmt, op0);
3209 gimple_assign_set_rhs2 (stmt, t);
3210 }
3211 else
3212 {
3213 t = build_int_cst (TREE_TYPE (op1), 1);
3214 t = int_const_binop (MINUS_EXPR, op1, t);
3215 t = fold_convert (TREE_TYPE (op0), t);
3216
3217 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
3218 gimple_assign_set_rhs1 (stmt, op0);
3219 gimple_assign_set_rhs2 (stmt, t);
3220 }
3221
3222 update_stmt (stmt);
3223 fold_stmt (gsi, follow_single_use_edges);
3224 return true;
3225 }
3226
3227 return false;
3228 }
3229
3230 /* Simplify a min or max if the ranges of the two operands are
3231 disjoint. Return true if we do simplify. */
3232
3233 bool
3234 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi,
3235 gimple *stmt)
3236 {
3237 tree op0 = gimple_assign_rhs1 (stmt);
3238 tree op1 = gimple_assign_rhs2 (stmt);
3239 bool sop = false;
3240 tree val;
3241
3242 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3243 (LE_EXPR, op0, op1, &sop));
3244 if (!val)
3245 {
3246 sop = false;
3247 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3248 (LT_EXPR, op0, op1, &sop));
3249 }
3250
3251 if (val)
3252 {
3253 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3254 {
3255 location_t location;
3256
3257 if (!gimple_has_location (stmt))
3258 location = input_location;
3259 else
3260 location = gimple_location (stmt);
3261 warning_at (location, OPT_Wstrict_overflow,
3262 "assuming signed overflow does not occur when "
3263 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3264 }
3265
3266 /* VAL == TRUE -> OP0 < or <= op1
3267 VAL == FALSE -> OP0 > or >= op1. */
3268 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
3269 == integer_zerop (val)) ? op0 : op1;
3270 gimple_assign_set_rhs_from_tree (gsi, res);
3271 return true;
3272 }
3273
3274 return false;
3275 }
3276
3277 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3278 ABS_EXPR. If the operand is <= 0, then simplify the
3279 ABS_EXPR into a NEGATE_EXPR. */
3280
3281 bool
3282 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3283 {
3284 tree op = gimple_assign_rhs1 (stmt);
3285 const value_range *vr = get_value_range (op);
3286
3287 if (vr)
3288 {
3289 tree val = NULL;
3290 bool sop = false;
3291
3292 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
3293 if (!val)
3294 {
3295 /* The range is neither <= 0 nor > 0. Now see if it is
3296 either < 0 or >= 0. */
3297 sop = false;
3298 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
3299 &sop);
3300 }
3301
3302 if (val)
3303 {
3304 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
3305 {
3306 location_t location;
3307
3308 if (!gimple_has_location (stmt))
3309 location = input_location;
3310 else
3311 location = gimple_location (stmt);
3312 warning_at (location, OPT_Wstrict_overflow,
3313 "assuming signed overflow does not occur when "
3314 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3315 }
3316
3317 gimple_assign_set_rhs1 (stmt, op);
3318 if (integer_zerop (val))
3319 gimple_assign_set_rhs_code (stmt, SSA_NAME);
3320 else
3321 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
3322 update_stmt (stmt);
3323 fold_stmt (gsi, follow_single_use_edges);
3324 return true;
3325 }
3326 }
3327
3328 return false;
3329 }
3330
3331 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3332 If all the bits that are being cleared by & are already
3333 known to be zero from VR, or all the bits that are being
3334 set by | are already known to be one from VR, the bit
3335 operation is redundant. */
3336
3337 bool
3338 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi,
3339 gimple *stmt)
3340 {
3341 tree op0 = gimple_assign_rhs1 (stmt);
3342 tree op1 = gimple_assign_rhs2 (stmt);
3343 tree op = NULL_TREE;
3344 value_range_base vr0, vr1;
3345 wide_int may_be_nonzero0, may_be_nonzero1;
3346 wide_int must_be_nonzero0, must_be_nonzero1;
3347 wide_int mask;
3348
3349 if (TREE_CODE (op0) == SSA_NAME)
3350 vr0 = *(get_value_range (op0));
3351 else if (is_gimple_min_invariant (op0))
3352 vr0.set (op0);
3353 else
3354 return false;
3355
3356 if (TREE_CODE (op1) == SSA_NAME)
3357 vr1 = *(get_value_range (op1));
3358 else if (is_gimple_min_invariant (op1))
3359 vr1.set (op1);
3360 else
3361 return false;
3362
3363 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
3364 &must_be_nonzero0))
3365 return false;
3366 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
3367 &must_be_nonzero1))
3368 return false;
3369
3370 switch (gimple_assign_rhs_code (stmt))
3371 {
3372 case BIT_AND_EXPR:
3373 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3374 if (mask == 0)
3375 {
3376 op = op0;
3377 break;
3378 }
3379 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3380 if (mask == 0)
3381 {
3382 op = op1;
3383 break;
3384 }
3385 break;
3386 case BIT_IOR_EXPR:
3387 mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
3388 if (mask == 0)
3389 {
3390 op = op1;
3391 break;
3392 }
3393 mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
3394 if (mask == 0)
3395 {
3396 op = op0;
3397 break;
3398 }
3399 break;
3400 default:
3401 gcc_unreachable ();
3402 }
3403
3404 if (op == NULL_TREE)
3405 return false;
3406
3407 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
3408 update_stmt (gsi_stmt (*gsi));
3409 return true;
3410 }
3411
3412 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3413 a known value range VR.
3414
3415 If there is one and only one value which will satisfy the
3416 conditional, then return that value. Else return NULL.
3417
3418 If signed overflow must be undefined for the value to satisfy
3419 the conditional, then set *STRICT_OVERFLOW_P to true. */
3420
3421 static tree
3422 test_for_singularity (enum tree_code cond_code, tree op0,
3423 tree op1, const value_range *vr)
3424 {
3425 tree min = NULL;
3426 tree max = NULL;
3427
3428 /* Extract minimum/maximum values which satisfy the conditional as it was
3429 written. */
3430 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
3431 {
3432 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
3433
3434 max = op1;
3435 if (cond_code == LT_EXPR)
3436 {
3437 tree one = build_int_cst (TREE_TYPE (op0), 1);
3438 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
3439 /* Signal to compare_values_warnv this expr doesn't overflow. */
3440 if (EXPR_P (max))
3441 TREE_NO_WARNING (max) = 1;
3442 }
3443 }
3444 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
3445 {
3446 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
3447
3448 min = op1;
3449 if (cond_code == GT_EXPR)
3450 {
3451 tree one = build_int_cst (TREE_TYPE (op0), 1);
3452 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
3453 /* Signal to compare_values_warnv this expr doesn't overflow. */
3454 if (EXPR_P (min))
3455 TREE_NO_WARNING (min) = 1;
3456 }
3457 }
3458
3459 /* Now refine the minimum and maximum values using any
3460 value range information we have for op0. */
3461 if (min && max)
3462 {
3463 if (compare_values (vr->min (), min) == 1)
3464 min = vr->min ();
3465 if (compare_values (vr->max (), max) == -1)
3466 max = vr->max ();
3467
3468 /* If the new min/max values have converged to a single value,
3469 then there is only one value which can satisfy the condition,
3470 return that value. */
3471 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
3472 return min;
3473 }
3474 return NULL;
3475 }
3476
3477 /* Return whether the value range *VR fits in an integer type specified
3478 by PRECISION and UNSIGNED_P. */
3479
3480 static bool
3481 range_fits_type_p (const value_range *vr,
3482 unsigned dest_precision, signop dest_sgn)
3483 {
3484 tree src_type;
3485 unsigned src_precision;
3486 widest_int tem;
3487 signop src_sgn;
3488
3489 /* We can only handle integral and pointer types. */
3490 src_type = vr->type ();
3491 if (!INTEGRAL_TYPE_P (src_type)
3492 && !POINTER_TYPE_P (src_type))
3493 return false;
3494
3495 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3496 and so is an identity transform. */
3497 src_precision = TYPE_PRECISION (vr->type ());
3498 src_sgn = TYPE_SIGN (src_type);
3499 if ((src_precision < dest_precision
3500 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
3501 || (src_precision == dest_precision && src_sgn == dest_sgn))
3502 return true;
3503
3504 /* Now we can only handle ranges with constant bounds. */
3505 if (!range_int_cst_p (vr))
3506 return false;
3507
3508 /* For sign changes, the MSB of the wide_int has to be clear.
3509 An unsigned value with its MSB set cannot be represented by
3510 a signed wide_int, while a negative value cannot be represented
3511 by an unsigned wide_int. */
3512 if (src_sgn != dest_sgn
3513 && (wi::lts_p (wi::to_wide (vr->min ()), 0)
3514 || wi::lts_p (wi::to_wide (vr->max ()), 0)))
3515 return false;
3516
3517 /* Then we can perform the conversion on both ends and compare
3518 the result for equality. */
3519 tem = wi::ext (wi::to_widest (vr->min ()), dest_precision, dest_sgn);
3520 if (tem != wi::to_widest (vr->min ()))
3521 return false;
3522 tem = wi::ext (wi::to_widest (vr->max ()), dest_precision, dest_sgn);
3523 if (tem != wi::to_widest (vr->max ()))
3524 return false;
3525
3526 return true;
3527 }
3528
3529 /* Simplify a conditional using a relational operator to an equality
3530 test if the range information indicates only one value can satisfy
3531 the original conditional. */
3532
3533 bool
3534 vr_values::simplify_cond_using_ranges_1 (gcond *stmt)
3535 {
3536 tree op0 = gimple_cond_lhs (stmt);
3537 tree op1 = gimple_cond_rhs (stmt);
3538 enum tree_code cond_code = gimple_cond_code (stmt);
3539
3540 if (cond_code != NE_EXPR
3541 && cond_code != EQ_EXPR
3542 && TREE_CODE (op0) == SSA_NAME
3543 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3544 && is_gimple_min_invariant (op1))
3545 {
3546 const value_range *vr = get_value_range (op0);
3547
3548 /* If we have range information for OP0, then we might be
3549 able to simplify this conditional. */
3550 if (vr->kind () == VR_RANGE)
3551 {
3552 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
3553 if (new_tree)
3554 {
3555 if (dump_file)
3556 {
3557 fprintf (dump_file, "Simplified relational ");
3558 print_gimple_stmt (dump_file, stmt, 0);
3559 fprintf (dump_file, " into ");
3560 }
3561
3562 gimple_cond_set_code (stmt, EQ_EXPR);
3563 gimple_cond_set_lhs (stmt, op0);
3564 gimple_cond_set_rhs (stmt, new_tree);
3565
3566 update_stmt (stmt);
3567
3568 if (dump_file)
3569 {
3570 print_gimple_stmt (dump_file, stmt, 0);
3571 fprintf (dump_file, "\n");
3572 }
3573
3574 return true;
3575 }
3576
3577 /* Try again after inverting the condition. We only deal
3578 with integral types here, so no need to worry about
3579 issues with inverting FP comparisons. */
3580 new_tree = test_for_singularity
3581 (invert_tree_comparison (cond_code, false),
3582 op0, op1, vr);
3583 if (new_tree)
3584 {
3585 if (dump_file)
3586 {
3587 fprintf (dump_file, "Simplified relational ");
3588 print_gimple_stmt (dump_file, stmt, 0);
3589 fprintf (dump_file, " into ");
3590 }
3591
3592 gimple_cond_set_code (stmt, NE_EXPR);
3593 gimple_cond_set_lhs (stmt, op0);
3594 gimple_cond_set_rhs (stmt, new_tree);
3595
3596 update_stmt (stmt);
3597
3598 if (dump_file)
3599 {
3600 print_gimple_stmt (dump_file, stmt, 0);
3601 fprintf (dump_file, "\n");
3602 }
3603
3604 return true;
3605 }
3606 }
3607 }
3608 return false;
3609 }
3610
3611 /* STMT is a conditional at the end of a basic block.
3612
3613 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3614 was set via a type conversion, try to replace the SSA_NAME with the RHS
3615 of the type conversion. Doing so makes the conversion dead which helps
3616 subsequent passes. */
3617
3618 void
3619 vr_values::simplify_cond_using_ranges_2 (gcond *stmt)
3620 {
3621 tree op0 = gimple_cond_lhs (stmt);
3622 tree op1 = gimple_cond_rhs (stmt);
3623
3624 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3625 see if OP0 was set by a type conversion where the source of
3626 the conversion is another SSA_NAME with a range that fits
3627 into the range of OP0's type.
3628
3629 If so, the conversion is redundant as the earlier SSA_NAME can be
3630 used for the comparison directly if we just massage the constant in the
3631 comparison. */
3632 if (TREE_CODE (op0) == SSA_NAME
3633 && TREE_CODE (op1) == INTEGER_CST)
3634 {
3635 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
3636 tree innerop;
3637
3638 if (!is_gimple_assign (def_stmt)
3639 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3640 return;
3641
3642 innerop = gimple_assign_rhs1 (def_stmt);
3643
3644 if (TREE_CODE (innerop) == SSA_NAME
3645 && !POINTER_TYPE_P (TREE_TYPE (innerop))
3646 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
3647 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
3648 {
3649 const value_range *vr = get_value_range (innerop);
3650
3651 if (range_int_cst_p (vr)
3652 && range_fits_type_p (vr,
3653 TYPE_PRECISION (TREE_TYPE (op0)),
3654 TYPE_SIGN (TREE_TYPE (op0)))
3655 && int_fits_type_p (op1, TREE_TYPE (innerop)))
3656 {
3657 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
3658 gimple_cond_set_lhs (stmt, innerop);
3659 gimple_cond_set_rhs (stmt, newconst);
3660 update_stmt (stmt);
3661 if (dump_file && (dump_flags & TDF_DETAILS))
3662 {
3663 fprintf (dump_file, "Folded into: ");
3664 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3665 fprintf (dump_file, "\n");
3666 }
3667 }
3668 }
3669 }
3670 }
3671
3672 /* Simplify a switch statement using the value range of the switch
3673 argument. */
3674
3675 bool
3676 vr_values::simplify_switch_using_ranges (gswitch *stmt)
3677 {
3678 tree op = gimple_switch_index (stmt);
3679 const value_range *vr = NULL;
3680 bool take_default;
3681 edge e;
3682 edge_iterator ei;
3683 size_t i = 0, j = 0, n, n2;
3684 tree vec2;
3685 switch_update su;
3686 size_t k = 1, l = 0;
3687
3688 if (TREE_CODE (op) == SSA_NAME)
3689 {
3690 vr = get_value_range (op);
3691
3692 /* We can only handle integer ranges. */
3693 if (vr->varying_p ()
3694 || vr->undefined_p ()
3695 || vr->symbolic_p ())
3696 return false;
3697
3698 /* Find case label for min/max of the value range. */
3699 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
3700 }
3701 else if (TREE_CODE (op) == INTEGER_CST)
3702 {
3703 take_default = !find_case_label_index (stmt, 1, op, &i);
3704 if (take_default)
3705 {
3706 i = 1;
3707 j = 0;
3708 }
3709 else
3710 {
3711 j = i;
3712 }
3713 }
3714 else
3715 return false;
3716
3717 n = gimple_switch_num_labels (stmt);
3718
3719 /* We can truncate the case label ranges that partially overlap with OP's
3720 value range. */
3721 size_t min_idx = 1, max_idx = 0;
3722 if (vr != NULL)
3723 find_case_label_range (stmt, vr->min (), vr->max (), &min_idx, &max_idx);
3724 if (min_idx <= max_idx)
3725 {
3726 tree min_label = gimple_switch_label (stmt, min_idx);
3727 tree max_label = gimple_switch_label (stmt, max_idx);
3728
3729 /* Avoid changing the type of the case labels when truncating. */
3730 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
3731 tree vr_min = fold_convert (case_label_type, vr->min ());
3732 tree vr_max = fold_convert (case_label_type, vr->max ());
3733
3734 if (vr->kind () == VR_RANGE)
3735 {
3736 /* If OP's value range is [2,8] and the low label range is
3737 0 ... 3, truncate the label's range to 2 .. 3. */
3738 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3739 && CASE_HIGH (min_label) != NULL_TREE
3740 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3741 CASE_LOW (min_label) = vr_min;
3742
3743 /* If OP's value range is [2,8] and the high label range is
3744 7 ... 10, truncate the label's range to 7 .. 8. */
3745 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3746 && CASE_HIGH (max_label) != NULL_TREE
3747 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3748 CASE_HIGH (max_label) = vr_max;
3749 }
3750 else if (vr->kind () == VR_ANTI_RANGE)
3751 {
3752 tree one_cst = build_one_cst (case_label_type);
3753
3754 if (min_label == max_label)
3755 {
3756 /* If OP's value range is ~[7,8] and the label's range is
3757 7 ... 10, truncate the label's range to 9 ... 10. */
3758 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
3759 && CASE_HIGH (min_label) != NULL_TREE
3760 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
3761 CASE_LOW (min_label)
3762 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3763
3764 /* If OP's value range is ~[7,8] and the label's range is
3765 5 ... 8, truncate the label's range to 5 ... 6. */
3766 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3767 && CASE_HIGH (min_label) != NULL_TREE
3768 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
3769 CASE_HIGH (min_label)
3770 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3771 }
3772 else
3773 {
3774 /* If OP's value range is ~[2,8] and the low label range is
3775 0 ... 3, truncate the label's range to 0 ... 1. */
3776 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
3777 && CASE_HIGH (min_label) != NULL_TREE
3778 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
3779 CASE_HIGH (min_label)
3780 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
3781
3782 /* If OP's value range is ~[2,8] and the high label range is
3783 7 ... 10, truncate the label's range to 9 ... 10. */
3784 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
3785 && CASE_HIGH (max_label) != NULL_TREE
3786 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
3787 CASE_LOW (max_label)
3788 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
3789 }
3790 }
3791
3792 /* Canonicalize singleton case ranges. */
3793 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
3794 CASE_HIGH (min_label) = NULL_TREE;
3795 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
3796 CASE_HIGH (max_label) = NULL_TREE;
3797 }
3798
3799 /* We can also eliminate case labels that lie completely outside OP's value
3800 range. */
3801
3802 /* Bail out if this is just all edges taken. */
3803 if (i == 1
3804 && j == n - 1
3805 && take_default)
3806 return false;
3807
3808 /* Build a new vector of taken case labels. */
3809 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
3810 n2 = 0;
3811
3812 /* Add the default edge, if necessary. */
3813 if (take_default)
3814 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
3815
3816 for (; i <= j; ++i, ++n2)
3817 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
3818
3819 for (; k <= l; ++k, ++n2)
3820 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
3821
3822 /* Mark needed edges. */
3823 for (i = 0; i < n2; ++i)
3824 {
3825 e = find_edge (gimple_bb (stmt),
3826 label_to_block (cfun,
3827 CASE_LABEL (TREE_VEC_ELT (vec2, i))));
3828 e->aux = (void *)-1;
3829 }
3830
3831 /* Queue not needed edges for later removal. */
3832 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
3833 {
3834 if (e->aux == (void *)-1)
3835 {
3836 e->aux = NULL;
3837 continue;
3838 }
3839
3840 if (dump_file && (dump_flags & TDF_DETAILS))
3841 {
3842 fprintf (dump_file, "removing unreachable case label\n");
3843 }
3844 to_remove_edges.safe_push (e);
3845 e->flags &= ~EDGE_EXECUTABLE;
3846 e->flags |= EDGE_IGNORE;
3847 }
3848
3849 /* And queue an update for the stmt. */
3850 su.stmt = stmt;
3851 su.vec = vec2;
3852 to_update_switch_stmts.safe_push (su);
3853 return false;
3854 }
3855
3856 void
3857 vr_values::cleanup_edges_and_switches (void)
3858 {
3859 int i;
3860 edge e;
3861 switch_update *su;
3862
3863 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3864 CFG in a broken state and requires a cfg_cleanup run. */
3865 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
3866 remove_edge (e);
3867
3868 /* Update SWITCH_EXPR case label vector. */
3869 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
3870 {
3871 size_t j;
3872 size_t n = TREE_VEC_LENGTH (su->vec);
3873 tree label;
3874 gimple_switch_set_num_labels (su->stmt, n);
3875 for (j = 0; j < n; j++)
3876 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
3877 /* As we may have replaced the default label with a regular one
3878 make sure to make it a real default label again. This ensures
3879 optimal expansion. */
3880 label = gimple_switch_label (su->stmt, 0);
3881 CASE_LOW (label) = NULL_TREE;
3882 CASE_HIGH (label) = NULL_TREE;
3883 }
3884
3885 if (!to_remove_edges.is_empty ())
3886 {
3887 free_dominance_info (CDI_DOMINATORS);
3888 loops_state_set (LOOPS_NEED_FIXUP);
3889 }
3890
3891 to_remove_edges.release ();
3892 to_update_switch_stmts.release ();
3893 }
3894
3895 /* Simplify an integral conversion from an SSA name in STMT. */
3896
3897 static bool
3898 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
3899 {
3900 tree innerop, middleop, finaltype;
3901 gimple *def_stmt;
3902 signop inner_sgn, middle_sgn, final_sgn;
3903 unsigned inner_prec, middle_prec, final_prec;
3904 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
3905
3906 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
3907 if (!INTEGRAL_TYPE_P (finaltype))
3908 return false;
3909 middleop = gimple_assign_rhs1 (stmt);
3910 def_stmt = SSA_NAME_DEF_STMT (middleop);
3911 if (!is_gimple_assign (def_stmt)
3912 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
3913 return false;
3914 innerop = gimple_assign_rhs1 (def_stmt);
3915 if (TREE_CODE (innerop) != SSA_NAME
3916 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
3917 return false;
3918
3919 /* Get the value-range of the inner operand. Use get_range_info in
3920 case innerop was created during substitute-and-fold. */
3921 wide_int imin, imax;
3922 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
3923 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
3924 return false;
3925 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
3926 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
3927
3928 /* Simulate the conversion chain to check if the result is equal if
3929 the middle conversion is removed. */
3930 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
3931 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
3932 final_prec = TYPE_PRECISION (finaltype);
3933
3934 /* If the first conversion is not injective, the second must not
3935 be widening. */
3936 if (wi::gtu_p (innermax - innermin,
3937 wi::mask <widest_int> (middle_prec, false))
3938 && middle_prec < final_prec)
3939 return false;
3940 /* We also want a medium value so that we can track the effect that
3941 narrowing conversions with sign change have. */
3942 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
3943 if (inner_sgn == UNSIGNED)
3944 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
3945 else
3946 innermed = 0;
3947 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
3948 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
3949 innermed = innermin;
3950
3951 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
3952 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
3953 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
3954 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
3955
3956 /* Require that the final conversion applied to both the original
3957 and the intermediate range produces the same result. */
3958 final_sgn = TYPE_SIGN (finaltype);
3959 if (wi::ext (middlemin, final_prec, final_sgn)
3960 != wi::ext (innermin, final_prec, final_sgn)
3961 || wi::ext (middlemed, final_prec, final_sgn)
3962 != wi::ext (innermed, final_prec, final_sgn)
3963 || wi::ext (middlemax, final_prec, final_sgn)
3964 != wi::ext (innermax, final_prec, final_sgn))
3965 return false;
3966
3967 gimple_assign_set_rhs1 (stmt, innerop);
3968 fold_stmt (gsi, follow_single_use_edges);
3969 return true;
3970 }
3971
3972 /* Simplify a conversion from integral SSA name to float in STMT. */
3973
3974 bool
3975 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
3976 gimple *stmt)
3977 {
3978 tree rhs1 = gimple_assign_rhs1 (stmt);
3979 const value_range *vr = get_value_range (rhs1);
3980 scalar_float_mode fltmode
3981 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
3982 scalar_int_mode mode;
3983 tree tem;
3984 gassign *conv;
3985
3986 /* We can only handle constant ranges. */
3987 if (!range_int_cst_p (vr))
3988 return false;
3989
3990 /* First check if we can use a signed type in place of an unsigned. */
3991 scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
3992 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
3993 && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
3994 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
3995 mode = rhs_mode;
3996 /* If we can do the conversion in the current input mode do nothing. */
3997 else if (can_float_p (fltmode, rhs_mode,
3998 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
3999 return false;
4000 /* Otherwise search for a mode we can use, starting from the narrowest
4001 integer mode available. */
4002 else
4003 {
4004 mode = NARROWEST_INT_MODE;
4005 for (;;)
4006 {
4007 /* If we cannot do a signed conversion to float from mode
4008 or if the value-range does not fit in the signed type
4009 try with a wider mode. */
4010 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
4011 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
4012 break;
4013
4014 /* But do not widen the input. Instead leave that to the
4015 optabs expansion code. */
4016 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
4017 || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
4018 return false;
4019 }
4020 }
4021
4022 /* It works, insert a truncation or sign-change before the
4023 float conversion. */
4024 tem = make_ssa_name (build_nonstandard_integer_type
4025 (GET_MODE_PRECISION (mode), 0));
4026 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
4027 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
4028 gimple_assign_set_rhs1 (stmt, tem);
4029 fold_stmt (gsi, follow_single_use_edges);
4030
4031 return true;
4032 }
4033
4034 /* Simplify an internal fn call using ranges if possible. */
4035
4036 bool
4037 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi,
4038 gimple *stmt)
4039 {
4040 enum tree_code subcode;
4041 bool is_ubsan = false;
4042 bool ovf = false;
4043 switch (gimple_call_internal_fn (stmt))
4044 {
4045 case IFN_UBSAN_CHECK_ADD:
4046 subcode = PLUS_EXPR;
4047 is_ubsan = true;
4048 break;
4049 case IFN_UBSAN_CHECK_SUB:
4050 subcode = MINUS_EXPR;
4051 is_ubsan = true;
4052 break;
4053 case IFN_UBSAN_CHECK_MUL:
4054 subcode = MULT_EXPR;
4055 is_ubsan = true;
4056 break;
4057 case IFN_ADD_OVERFLOW:
4058 subcode = PLUS_EXPR;
4059 break;
4060 case IFN_SUB_OVERFLOW:
4061 subcode = MINUS_EXPR;
4062 break;
4063 case IFN_MUL_OVERFLOW:
4064 subcode = MULT_EXPR;
4065 break;
4066 default:
4067 return false;
4068 }
4069
4070 tree op0 = gimple_call_arg (stmt, 0);
4071 tree op1 = gimple_call_arg (stmt, 1);
4072 tree type;
4073 if (is_ubsan)
4074 {
4075 type = TREE_TYPE (op0);
4076 if (VECTOR_TYPE_P (type))
4077 return false;
4078 }
4079 else if (gimple_call_lhs (stmt) == NULL_TREE)
4080 return false;
4081 else
4082 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
4083 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
4084 || (is_ubsan && ovf))
4085 return false;
4086
4087 gimple *g;
4088 location_t loc = gimple_location (stmt);
4089 if (is_ubsan)
4090 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
4091 else
4092 {
4093 int prec = TYPE_PRECISION (type);
4094 tree utype = type;
4095 if (ovf
4096 || !useless_type_conversion_p (type, TREE_TYPE (op0))
4097 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
4098 utype = build_nonstandard_integer_type (prec, 1);
4099 if (TREE_CODE (op0) == INTEGER_CST)
4100 op0 = fold_convert (utype, op0);
4101 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
4102 {
4103 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
4104 gimple_set_location (g, loc);
4105 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4106 op0 = gimple_assign_lhs (g);
4107 }
4108 if (TREE_CODE (op1) == INTEGER_CST)
4109 op1 = fold_convert (utype, op1);
4110 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
4111 {
4112 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
4113 gimple_set_location (g, loc);
4114 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4115 op1 = gimple_assign_lhs (g);
4116 }
4117 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
4118 gimple_set_location (g, loc);
4119 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4120 if (utype != type)
4121 {
4122 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
4123 gimple_assign_lhs (g));
4124 gimple_set_location (g, loc);
4125 gsi_insert_before (gsi, g, GSI_SAME_STMT);
4126 }
4127 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
4128 gimple_assign_lhs (g),
4129 build_int_cst (type, ovf));
4130 }
4131 gimple_set_location (g, loc);
4132 gsi_replace (gsi, g, false);
4133 return true;
4134 }
4135
4136 /* Return true if VAR is a two-valued variable. Set a and b with the
4137 two-values when it is true. Return false otherwise. */
4138
4139 bool
4140 vr_values::two_valued_val_range_p (tree var, tree *a, tree *b)
4141 {
4142 const value_range *vr = get_value_range (var);
4143 if (vr->varying_p ()
4144 || vr->undefined_p ()
4145 || TREE_CODE (vr->min ()) != INTEGER_CST
4146 || TREE_CODE (vr->max ()) != INTEGER_CST)
4147 return false;
4148
4149 if (vr->kind () == VR_RANGE
4150 && wi::to_wide (vr->max ()) - wi::to_wide (vr->min ()) == 1)
4151 {
4152 *a = vr->min ();
4153 *b = vr->max ();
4154 return true;
4155 }
4156
4157 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4158 if (vr->kind () == VR_ANTI_RANGE
4159 && (wi::to_wide (vr->min ())
4160 - wi::to_wide (vrp_val_min (TREE_TYPE (var)))) == 1
4161 && (wi::to_wide (vrp_val_max (TREE_TYPE (var)))
4162 - wi::to_wide (vr->max ())) == 1)
4163 {
4164 *a = vrp_val_min (TREE_TYPE (var));
4165 *b = vrp_val_max (TREE_TYPE (var));
4166 return true;
4167 }
4168
4169 return false;
4170 }
4171
4172 /* Simplify STMT using ranges if possible. */
4173
4174 bool
4175 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
4176 {
4177 gimple *stmt = gsi_stmt (*gsi);
4178 if (is_gimple_assign (stmt))
4179 {
4180 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
4181 tree rhs1 = gimple_assign_rhs1 (stmt);
4182 tree rhs2 = gimple_assign_rhs2 (stmt);
4183 tree lhs = gimple_assign_lhs (stmt);
4184 tree val1 = NULL_TREE, val2 = NULL_TREE;
4185 use_operand_p use_p;
4186 gimple *use_stmt;
4187
4188 /* Convert:
4189 LHS = CST BINOP VAR
4190 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4191 To:
4192 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4193
4194 Also handles:
4195 LHS = VAR BINOP CST
4196 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4197 To:
4198 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4199
4200 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
4201 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4202 && ((TREE_CODE (rhs1) == INTEGER_CST
4203 && TREE_CODE (rhs2) == SSA_NAME)
4204 || (TREE_CODE (rhs2) == INTEGER_CST
4205 && TREE_CODE (rhs1) == SSA_NAME))
4206 && single_imm_use (lhs, &use_p, &use_stmt)
4207 && gimple_code (use_stmt) == GIMPLE_COND)
4208
4209 {
4210 tree new_rhs1 = NULL_TREE;
4211 tree new_rhs2 = NULL_TREE;
4212 tree cmp_var = NULL_TREE;
4213
4214 if (TREE_CODE (rhs2) == SSA_NAME
4215 && two_valued_val_range_p (rhs2, &val1, &val2))
4216 {
4217 /* Optimize RHS1 OP [VAL1, VAL2]. */
4218 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
4219 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
4220 cmp_var = rhs2;
4221 }
4222 else if (TREE_CODE (rhs1) == SSA_NAME
4223 && two_valued_val_range_p (rhs1, &val1, &val2))
4224 {
4225 /* Optimize [VAL1, VAL2] OP RHS2. */
4226 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
4227 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
4228 cmp_var = rhs1;
4229 }
4230
4231 /* If we could not find two-vals or the optimzation is invalid as
4232 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4233 if (new_rhs1 && new_rhs2)
4234 {
4235 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
4236 gimple_assign_set_rhs_with_ops (gsi,
4237 COND_EXPR, cond,
4238 new_rhs1,
4239 new_rhs2);
4240 update_stmt (gsi_stmt (*gsi));
4241 fold_stmt (gsi, follow_single_use_edges);
4242 return true;
4243 }
4244 }
4245
4246 switch (rhs_code)
4247 {
4248 case EQ_EXPR:
4249 case NE_EXPR:
4250 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4251 if the RHS is zero or one, and the LHS are known to be boolean
4252 values. */
4253 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4254 return simplify_truth_ops_using_ranges (gsi, stmt);
4255 break;
4256
4257 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4258 and BIT_AND_EXPR respectively if the first operand is greater
4259 than zero and the second operand is an exact power of two.
4260 Also optimize TRUNC_MOD_EXPR away if the second operand is
4261 constant and the first operand already has the right value
4262 range. */
4263 case TRUNC_DIV_EXPR:
4264 case TRUNC_MOD_EXPR:
4265 if ((TREE_CODE (rhs1) == SSA_NAME
4266 || TREE_CODE (rhs1) == INTEGER_CST)
4267 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4268 return simplify_div_or_mod_using_ranges (gsi, stmt);
4269 break;
4270
4271 /* Transform ABS (X) into X or -X as appropriate. */
4272 case ABS_EXPR:
4273 if (TREE_CODE (rhs1) == SSA_NAME
4274 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4275 return simplify_abs_using_ranges (gsi, stmt);
4276 break;
4277
4278 case BIT_AND_EXPR:
4279 case BIT_IOR_EXPR:
4280 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4281 if all the bits being cleared are already cleared or
4282 all the bits being set are already set. */
4283 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4284 return simplify_bit_ops_using_ranges (gsi, stmt);
4285 break;
4286
4287 CASE_CONVERT:
4288 if (TREE_CODE (rhs1) == SSA_NAME
4289 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4290 return simplify_conversion_using_ranges (gsi, stmt);
4291 break;
4292
4293 case FLOAT_EXPR:
4294 if (TREE_CODE (rhs1) == SSA_NAME
4295 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
4296 return simplify_float_conversion_using_ranges (gsi, stmt);
4297 break;
4298
4299 case MIN_EXPR:
4300 case MAX_EXPR:
4301 return simplify_min_or_max_using_ranges (gsi, stmt);
4302
4303 default:
4304 break;
4305 }
4306 }
4307 else if (gimple_code (stmt) == GIMPLE_COND)
4308 return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
4309 else if (gimple_code (stmt) == GIMPLE_SWITCH)
4310 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
4311 else if (is_gimple_call (stmt)
4312 && gimple_call_internal_p (stmt))
4313 return simplify_internal_call_using_ranges (gsi, stmt);
4314
4315 return false;
4316 }
4317
4318 /* Set the lattice entry for VAR to VR. */
4319
4320 void
4321 vr_values::set_vr_value (tree var, value_range *vr)
4322 {
4323 if (SSA_NAME_VERSION (var) >= num_vr_values)
4324 return;
4325 vr_value[SSA_NAME_VERSION (var)] = vr;
4326 }
4327
4328 /* Swap the lattice entry for VAR with VR and return the old entry. */
4329
4330 value_range *
4331 vr_values::swap_vr_value (tree var, value_range *vr)
4332 {
4333 if (SSA_NAME_VERSION (var) >= num_vr_values)
4334 return NULL;
4335 std::swap (vr_value[SSA_NAME_VERSION (var)], vr);
4336 return vr;
4337 }