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