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