VRP: Simplify logic for checking if any asserts need to be inserted
[gcc.git] / gcc / tree-vrp.c
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "calls.h"
29 #include "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "cfganal.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
60 #include "tree-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
65 #include "intl.h"
66 #include "cfgloop.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
71 #include "expr.h"
72 #include "insn-codes.h"
73 #include "optabs.h"
74 #include "tree-ssa-threadedge.h"
75 #include "wide-int.h"
76
77
78
79 /* Range of values that can be associated with an SSA_NAME after VRP
80 has executed. */
81 struct value_range_d
82 {
83 /* Lattice value represented by this range. */
84 enum value_range_type type;
85
86 /* Minimum and maximum values represented by this range. These
87 values should be interpreted as follows:
88
89 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
90 be NULL.
91
92 - If TYPE == VR_RANGE then MIN holds the minimum value and
93 MAX holds the maximum value of the range [MIN, MAX].
94
95 - If TYPE == ANTI_RANGE the variable is known to NOT
96 take any values in the range [MIN, MAX]. */
97 tree min;
98 tree max;
99
100 /* Set of SSA names whose value ranges are equivalent to this one.
101 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
102 bitmap equiv;
103 };
104
105 typedef struct value_range_d value_range_t;
106
107 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
108
109 /* Set of SSA names found live during the RPO traversal of the function
110 for still active basic-blocks. */
111 static sbitmap *live;
112
113 /* Return true if the SSA name NAME is live on the edge E. */
114
115 static bool
116 live_on_edge (edge e, tree name)
117 {
118 return (live[e->dest->index]
119 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
120 }
121
122 /* Local functions. */
123 static int compare_values (tree val1, tree val2);
124 static int compare_values_warnv (tree val1, tree val2, bool *);
125 static void vrp_meet (value_range_t *, value_range_t *);
126 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
127 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
128 tree, tree, bool, bool *,
129 bool *);
130
131 /* Location information for ASSERT_EXPRs. Each instance of this
132 structure describes an ASSERT_EXPR for an SSA name. Since a single
133 SSA name may have more than one assertion associated with it, these
134 locations are kept in a linked list attached to the corresponding
135 SSA name. */
136 struct assert_locus_d
137 {
138 /* Basic block where the assertion would be inserted. */
139 basic_block bb;
140
141 /* Some assertions need to be inserted on an edge (e.g., assertions
142 generated by COND_EXPRs). In those cases, BB will be NULL. */
143 edge e;
144
145 /* Pointer to the statement that generated this assertion. */
146 gimple_stmt_iterator si;
147
148 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
149 enum tree_code comp_code;
150
151 /* Value being compared against. */
152 tree val;
153
154 /* Expression to compare. */
155 tree expr;
156
157 /* Next node in the linked list. */
158 struct assert_locus_d *next;
159 };
160
161 typedef struct assert_locus_d *assert_locus_t;
162
163 /* If bit I is present, it means that SSA name N_i has a list of
164 assertions that should be inserted in the IL. */
165 static bitmap need_assert_for;
166
167 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
168 holds a list of ASSERT_LOCUS_T nodes that describe where
169 ASSERT_EXPRs for SSA name N_I should be inserted. */
170 static assert_locus_t *asserts_for;
171
172 /* Value range array. After propagation, VR_VALUE[I] holds the range
173 of values that SSA name N_I may take. */
174 static unsigned num_vr_values;
175 static value_range_t **vr_value;
176 static bool values_propagated;
177
178 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
179 number of executable edges we saw the last time we visited the
180 node. */
181 static int *vr_phi_edge_counts;
182
183 typedef struct {
184 gimple stmt;
185 tree vec;
186 } switch_update;
187
188 static vec<edge> to_remove_edges;
189 static vec<switch_update> to_update_switch_stmts;
190
191
192 /* Return the maximum value for TYPE. */
193
194 static inline tree
195 vrp_val_max (const_tree type)
196 {
197 if (!INTEGRAL_TYPE_P (type))
198 return NULL_TREE;
199
200 return TYPE_MAX_VALUE (type);
201 }
202
203 /* Return the minimum value for TYPE. */
204
205 static inline tree
206 vrp_val_min (const_tree type)
207 {
208 if (!INTEGRAL_TYPE_P (type))
209 return NULL_TREE;
210
211 return TYPE_MIN_VALUE (type);
212 }
213
214 /* Return whether VAL is equal to the maximum value of its type. This
215 will be true for a positive overflow infinity. We can't do a
216 simple equality comparison with TYPE_MAX_VALUE because C typedefs
217 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
218 to the integer constant with the same value in the type. */
219
220 static inline bool
221 vrp_val_is_max (const_tree val)
222 {
223 tree type_max = vrp_val_max (TREE_TYPE (val));
224 return (val == type_max
225 || (type_max != NULL_TREE
226 && operand_equal_p (val, type_max, 0)));
227 }
228
229 /* Return whether VAL is equal to the minimum value of its type. This
230 will be true for a negative overflow infinity. */
231
232 static inline bool
233 vrp_val_is_min (const_tree val)
234 {
235 tree type_min = vrp_val_min (TREE_TYPE (val));
236 return (val == type_min
237 || (type_min != NULL_TREE
238 && operand_equal_p (val, type_min, 0)));
239 }
240
241
242 /* Return whether TYPE should use an overflow infinity distinct from
243 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
244 represent a signed overflow during VRP computations. An infinity
245 is distinct from a half-range, which will go from some number to
246 TYPE_{MIN,MAX}_VALUE. */
247
248 static inline bool
249 needs_overflow_infinity (const_tree type)
250 {
251 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
252 }
253
254 /* Return whether TYPE can support our overflow infinity
255 representation: we use the TREE_OVERFLOW flag, which only exists
256 for constants. If TYPE doesn't support this, we don't optimize
257 cases which would require signed overflow--we drop them to
258 VARYING. */
259
260 static inline bool
261 supports_overflow_infinity (const_tree type)
262 {
263 tree min = vrp_val_min (type), max = vrp_val_max (type);
264 #ifdef ENABLE_CHECKING
265 gcc_assert (needs_overflow_infinity (type));
266 #endif
267 return (min != NULL_TREE
268 && CONSTANT_CLASS_P (min)
269 && max != NULL_TREE
270 && CONSTANT_CLASS_P (max));
271 }
272
273 /* VAL is the maximum or minimum value of a type. Return a
274 corresponding overflow infinity. */
275
276 static inline tree
277 make_overflow_infinity (tree val)
278 {
279 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
280 val = copy_node (val);
281 TREE_OVERFLOW (val) = 1;
282 return val;
283 }
284
285 /* Return a negative overflow infinity for TYPE. */
286
287 static inline tree
288 negative_overflow_infinity (tree type)
289 {
290 gcc_checking_assert (supports_overflow_infinity (type));
291 return make_overflow_infinity (vrp_val_min (type));
292 }
293
294 /* Return a positive overflow infinity for TYPE. */
295
296 static inline tree
297 positive_overflow_infinity (tree type)
298 {
299 gcc_checking_assert (supports_overflow_infinity (type));
300 return make_overflow_infinity (vrp_val_max (type));
301 }
302
303 /* Return whether VAL is a negative overflow infinity. */
304
305 static inline bool
306 is_negative_overflow_infinity (const_tree val)
307 {
308 return (TREE_OVERFLOW_P (val)
309 && needs_overflow_infinity (TREE_TYPE (val))
310 && vrp_val_is_min (val));
311 }
312
313 /* Return whether VAL is a positive overflow infinity. */
314
315 static inline bool
316 is_positive_overflow_infinity (const_tree val)
317 {
318 return (TREE_OVERFLOW_P (val)
319 && needs_overflow_infinity (TREE_TYPE (val))
320 && vrp_val_is_max (val));
321 }
322
323 /* Return whether VAL is a positive or negative overflow infinity. */
324
325 static inline bool
326 is_overflow_infinity (const_tree val)
327 {
328 return (TREE_OVERFLOW_P (val)
329 && needs_overflow_infinity (TREE_TYPE (val))
330 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
331 }
332
333 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
334
335 static inline bool
336 stmt_overflow_infinity (gimple stmt)
337 {
338 if (is_gimple_assign (stmt)
339 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
340 GIMPLE_SINGLE_RHS)
341 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
342 return false;
343 }
344
345 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
346 the same value with TREE_OVERFLOW clear. This can be used to avoid
347 confusing a regular value with an overflow value. */
348
349 static inline tree
350 avoid_overflow_infinity (tree val)
351 {
352 if (!is_overflow_infinity (val))
353 return val;
354
355 if (vrp_val_is_max (val))
356 return vrp_val_max (TREE_TYPE (val));
357 else
358 {
359 gcc_checking_assert (vrp_val_is_min (val));
360 return vrp_val_min (TREE_TYPE (val));
361 }
362 }
363
364
365 /* Return true if ARG is marked with the nonnull attribute in the
366 current function signature. */
367
368 static bool
369 nonnull_arg_p (const_tree arg)
370 {
371 tree t, attrs, fntype;
372 unsigned HOST_WIDE_INT arg_num;
373
374 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
375
376 /* The static chain decl is always non null. */
377 if (arg == cfun->static_chain_decl)
378 return true;
379
380 fntype = TREE_TYPE (current_function_decl);
381 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
382 {
383 attrs = lookup_attribute ("nonnull", attrs);
384
385 /* If "nonnull" wasn't specified, we know nothing about the argument. */
386 if (attrs == NULL_TREE)
387 return false;
388
389 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
390 if (TREE_VALUE (attrs) == NULL_TREE)
391 return true;
392
393 /* Get the position number for ARG in the function signature. */
394 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
395 t;
396 t = DECL_CHAIN (t), arg_num++)
397 {
398 if (t == arg)
399 break;
400 }
401
402 gcc_assert (t == arg);
403
404 /* Now see if ARG_NUM is mentioned in the nonnull list. */
405 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
406 {
407 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
408 return true;
409 }
410 }
411
412 return false;
413 }
414
415
416 /* Set value range VR to VR_UNDEFINED. */
417
418 static inline void
419 set_value_range_to_undefined (value_range_t *vr)
420 {
421 vr->type = VR_UNDEFINED;
422 vr->min = vr->max = NULL_TREE;
423 if (vr->equiv)
424 bitmap_clear (vr->equiv);
425 }
426
427
428 /* Set value range VR to VR_VARYING. */
429
430 static inline void
431 set_value_range_to_varying (value_range_t *vr)
432 {
433 vr->type = VR_VARYING;
434 vr->min = vr->max = NULL_TREE;
435 if (vr->equiv)
436 bitmap_clear (vr->equiv);
437 }
438
439
440 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
441
442 static void
443 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
444 tree max, bitmap equiv)
445 {
446 #if defined ENABLE_CHECKING
447 /* Check the validity of the range. */
448 if (t == VR_RANGE || t == VR_ANTI_RANGE)
449 {
450 int cmp;
451
452 gcc_assert (min && max);
453
454 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
455 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
456
457 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
458 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
459
460 cmp = compare_values (min, max);
461 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
462
463 if (needs_overflow_infinity (TREE_TYPE (min)))
464 gcc_assert (!is_overflow_infinity (min)
465 || !is_overflow_infinity (max));
466 }
467
468 if (t == VR_UNDEFINED || t == VR_VARYING)
469 gcc_assert (min == NULL_TREE && max == NULL_TREE);
470
471 if (t == VR_UNDEFINED || t == VR_VARYING)
472 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
473 #endif
474
475 vr->type = t;
476 vr->min = min;
477 vr->max = max;
478
479 /* Since updating the equivalence set involves deep copying the
480 bitmaps, only do it if absolutely necessary. */
481 if (vr->equiv == NULL
482 && equiv != NULL)
483 vr->equiv = BITMAP_ALLOC (NULL);
484
485 if (equiv != vr->equiv)
486 {
487 if (equiv && !bitmap_empty_p (equiv))
488 bitmap_copy (vr->equiv, equiv);
489 else
490 bitmap_clear (vr->equiv);
491 }
492 }
493
494
495 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
496 This means adjusting T, MIN and MAX representing the case of a
497 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
498 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
499 In corner cases where MAX+1 or MIN-1 wraps this will fall back
500 to varying.
501 This routine exists to ease canonicalization in the case where we
502 extract ranges from var + CST op limit. */
503
504 static void
505 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
506 tree min, tree max, bitmap equiv)
507 {
508 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
509 if (t == VR_UNDEFINED)
510 {
511 set_value_range_to_undefined (vr);
512 return;
513 }
514 else if (t == VR_VARYING)
515 {
516 set_value_range_to_varying (vr);
517 return;
518 }
519
520 /* Nothing to canonicalize for symbolic ranges. */
521 if (TREE_CODE (min) != INTEGER_CST
522 || TREE_CODE (max) != INTEGER_CST)
523 {
524 set_value_range (vr, t, min, max, equiv);
525 return;
526 }
527
528 /* Wrong order for min and max, to swap them and the VR type we need
529 to adjust them. */
530 if (tree_int_cst_lt (max, min))
531 {
532 tree one, tmp;
533
534 /* For one bit precision if max < min, then the swapped
535 range covers all values, so for VR_RANGE it is varying and
536 for VR_ANTI_RANGE empty range, so drop to varying as well. */
537 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
538 {
539 set_value_range_to_varying (vr);
540 return;
541 }
542
543 one = build_int_cst (TREE_TYPE (min), 1);
544 tmp = int_const_binop (PLUS_EXPR, max, one);
545 max = int_const_binop (MINUS_EXPR, min, one);
546 min = tmp;
547
548 /* There's one corner case, if we had [C+1, C] before we now have
549 that again. But this represents an empty value range, so drop
550 to varying in this case. */
551 if (tree_int_cst_lt (max, min))
552 {
553 set_value_range_to_varying (vr);
554 return;
555 }
556
557 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
558 }
559
560 /* Anti-ranges that can be represented as ranges should be so. */
561 if (t == VR_ANTI_RANGE)
562 {
563 bool is_min = vrp_val_is_min (min);
564 bool is_max = vrp_val_is_max (max);
565
566 if (is_min && is_max)
567 {
568 /* We cannot deal with empty ranges, drop to varying.
569 ??? This could be VR_UNDEFINED instead. */
570 set_value_range_to_varying (vr);
571 return;
572 }
573 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
574 && (is_min || is_max))
575 {
576 /* Non-empty boolean ranges can always be represented
577 as a singleton range. */
578 if (is_min)
579 min = max = vrp_val_max (TREE_TYPE (min));
580 else
581 min = max = vrp_val_min (TREE_TYPE (min));
582 t = VR_RANGE;
583 }
584 else if (is_min
585 /* As a special exception preserve non-null ranges. */
586 && !(TYPE_UNSIGNED (TREE_TYPE (min))
587 && integer_zerop (max)))
588 {
589 tree one = build_int_cst (TREE_TYPE (max), 1);
590 min = int_const_binop (PLUS_EXPR, max, one);
591 max = vrp_val_max (TREE_TYPE (max));
592 t = VR_RANGE;
593 }
594 else if (is_max)
595 {
596 tree one = build_int_cst (TREE_TYPE (min), 1);
597 max = int_const_binop (MINUS_EXPR, min, one);
598 min = vrp_val_min (TREE_TYPE (min));
599 t = VR_RANGE;
600 }
601 }
602
603 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
604 if (needs_overflow_infinity (TREE_TYPE (min))
605 && is_overflow_infinity (min)
606 && is_overflow_infinity (max))
607 {
608 set_value_range_to_varying (vr);
609 return;
610 }
611
612 set_value_range (vr, t, min, max, equiv);
613 }
614
615 /* Copy value range FROM into value range TO. */
616
617 static inline void
618 copy_value_range (value_range_t *to, value_range_t *from)
619 {
620 set_value_range (to, from->type, from->min, from->max, from->equiv);
621 }
622
623 /* Set value range VR to a single value. This function is only called
624 with values we get from statements, and exists to clear the
625 TREE_OVERFLOW flag so that we don't think we have an overflow
626 infinity when we shouldn't. */
627
628 static inline void
629 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
630 {
631 gcc_assert (is_gimple_min_invariant (val));
632 if (TREE_OVERFLOW_P (val))
633 val = drop_tree_overflow (val);
634 set_value_range (vr, VR_RANGE, val, val, equiv);
635 }
636
637 /* Set value range VR to a non-negative range of type TYPE.
638 OVERFLOW_INFINITY indicates whether to use an overflow infinity
639 rather than TYPE_MAX_VALUE; this should be true if we determine
640 that the range is nonnegative based on the assumption that signed
641 overflow does not occur. */
642
643 static inline void
644 set_value_range_to_nonnegative (value_range_t *vr, tree type,
645 bool overflow_infinity)
646 {
647 tree zero;
648
649 if (overflow_infinity && !supports_overflow_infinity (type))
650 {
651 set_value_range_to_varying (vr);
652 return;
653 }
654
655 zero = build_int_cst (type, 0);
656 set_value_range (vr, VR_RANGE, zero,
657 (overflow_infinity
658 ? positive_overflow_infinity (type)
659 : TYPE_MAX_VALUE (type)),
660 vr->equiv);
661 }
662
663 /* Set value range VR to a non-NULL range of type TYPE. */
664
665 static inline void
666 set_value_range_to_nonnull (value_range_t *vr, tree type)
667 {
668 tree zero = build_int_cst (type, 0);
669 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
670 }
671
672
673 /* Set value range VR to a NULL range of type TYPE. */
674
675 static inline void
676 set_value_range_to_null (value_range_t *vr, tree type)
677 {
678 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
679 }
680
681
682 /* Set value range VR to a range of a truthvalue of type TYPE. */
683
684 static inline void
685 set_value_range_to_truthvalue (value_range_t *vr, tree type)
686 {
687 if (TYPE_PRECISION (type) == 1)
688 set_value_range_to_varying (vr);
689 else
690 set_value_range (vr, VR_RANGE,
691 build_int_cst (type, 0), build_int_cst (type, 1),
692 vr->equiv);
693 }
694
695
696 /* If abs (min) < abs (max), set VR to [-max, max], if
697 abs (min) >= abs (max), set VR to [-min, min]. */
698
699 static void
700 abs_extent_range (value_range_t *vr, tree min, tree max)
701 {
702 int cmp;
703
704 gcc_assert (TREE_CODE (min) == INTEGER_CST);
705 gcc_assert (TREE_CODE (max) == INTEGER_CST);
706 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
707 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
708 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
709 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
710 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
711 {
712 set_value_range_to_varying (vr);
713 return;
714 }
715 cmp = compare_values (min, max);
716 if (cmp == -1)
717 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
718 else if (cmp == 0 || cmp == 1)
719 {
720 max = min;
721 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
722 }
723 else
724 {
725 set_value_range_to_varying (vr);
726 return;
727 }
728 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
729 }
730
731
732 /* Return value range information for VAR.
733
734 If we have no values ranges recorded (ie, VRP is not running), then
735 return NULL. Otherwise create an empty range if none existed for VAR. */
736
737 static value_range_t *
738 get_value_range (const_tree var)
739 {
740 static const struct value_range_d vr_const_varying
741 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
742 value_range_t *vr;
743 tree sym;
744 unsigned ver = SSA_NAME_VERSION (var);
745
746 /* If we have no recorded ranges, then return NULL. */
747 if (! vr_value)
748 return NULL;
749
750 /* If we query the range for a new SSA name return an unmodifiable VARYING.
751 We should get here at most from the substitute-and-fold stage which
752 will never try to change values. */
753 if (ver >= num_vr_values)
754 return CONST_CAST (value_range_t *, &vr_const_varying);
755
756 vr = vr_value[ver];
757 if (vr)
758 return vr;
759
760 /* After propagation finished do not allocate new value-ranges. */
761 if (values_propagated)
762 return CONST_CAST (value_range_t *, &vr_const_varying);
763
764 /* Create a default value range. */
765 vr_value[ver] = vr = XCNEW (value_range_t);
766
767 /* Defer allocating the equivalence set. */
768 vr->equiv = NULL;
769
770 /* If VAR is a default definition of a parameter, the variable can
771 take any value in VAR's type. */
772 if (SSA_NAME_IS_DEFAULT_DEF (var))
773 {
774 sym = SSA_NAME_VAR (var);
775 if (TREE_CODE (sym) == PARM_DECL)
776 {
777 /* Try to use the "nonnull" attribute to create ~[0, 0]
778 anti-ranges for pointers. Note that this is only valid with
779 default definitions of PARM_DECLs. */
780 if (POINTER_TYPE_P (TREE_TYPE (sym))
781 && nonnull_arg_p (sym))
782 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
783 else
784 set_value_range_to_varying (vr);
785 }
786 else if (TREE_CODE (sym) == RESULT_DECL
787 && DECL_BY_REFERENCE (sym))
788 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
789 }
790
791 return vr;
792 }
793
794 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
795
796 static inline bool
797 vrp_operand_equal_p (const_tree val1, const_tree val2)
798 {
799 if (val1 == val2)
800 return true;
801 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
802 return false;
803 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
804 }
805
806 /* Return true, if the bitmaps B1 and B2 are equal. */
807
808 static inline bool
809 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
810 {
811 return (b1 == b2
812 || ((!b1 || bitmap_empty_p (b1))
813 && (!b2 || bitmap_empty_p (b2)))
814 || (b1 && b2
815 && bitmap_equal_p (b1, b2)));
816 }
817
818 /* Update the value range and equivalence set for variable VAR to
819 NEW_VR. Return true if NEW_VR is different from VAR's previous
820 value.
821
822 NOTE: This function assumes that NEW_VR is a temporary value range
823 object created for the sole purpose of updating VAR's range. The
824 storage used by the equivalence set from NEW_VR will be freed by
825 this function. Do not call update_value_range when NEW_VR
826 is the range object associated with another SSA name. */
827
828 static inline bool
829 update_value_range (const_tree var, value_range_t *new_vr)
830 {
831 value_range_t *old_vr;
832 bool is_new;
833
834 /* Update the value range, if necessary. */
835 old_vr = get_value_range (var);
836 is_new = old_vr->type != new_vr->type
837 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
838 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
839 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
840
841 if (is_new)
842 {
843 /* Do not allow transitions up the lattice. The following
844 is slightly more awkward than just new_vr->type < old_vr->type
845 because VR_RANGE and VR_ANTI_RANGE need to be considered
846 the same. We may not have is_new when transitioning to
847 UNDEFINED or from VARYING. */
848 if (new_vr->type == VR_UNDEFINED
849 || old_vr->type == VR_VARYING)
850 set_value_range_to_varying (old_vr);
851 else
852 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
853 new_vr->equiv);
854 }
855
856 BITMAP_FREE (new_vr->equiv);
857
858 return is_new;
859 }
860
861
862 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
863 point where equivalence processing can be turned on/off. */
864
865 static void
866 add_equivalence (bitmap *equiv, const_tree var)
867 {
868 unsigned ver = SSA_NAME_VERSION (var);
869 value_range_t *vr = vr_value[ver];
870
871 if (*equiv == NULL)
872 *equiv = BITMAP_ALLOC (NULL);
873 bitmap_set_bit (*equiv, ver);
874 if (vr && vr->equiv)
875 bitmap_ior_into (*equiv, vr->equiv);
876 }
877
878
879 /* Return true if VR is ~[0, 0]. */
880
881 static inline bool
882 range_is_nonnull (value_range_t *vr)
883 {
884 return vr->type == VR_ANTI_RANGE
885 && integer_zerop (vr->min)
886 && integer_zerop (vr->max);
887 }
888
889
890 /* Return true if VR is [0, 0]. */
891
892 static inline bool
893 range_is_null (value_range_t *vr)
894 {
895 return vr->type == VR_RANGE
896 && integer_zerop (vr->min)
897 && integer_zerop (vr->max);
898 }
899
900 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
901 a singleton. */
902
903 static inline bool
904 range_int_cst_p (value_range_t *vr)
905 {
906 return (vr->type == VR_RANGE
907 && TREE_CODE (vr->max) == INTEGER_CST
908 && TREE_CODE (vr->min) == INTEGER_CST);
909 }
910
911 /* Return true if VR is a INTEGER_CST singleton. */
912
913 static inline bool
914 range_int_cst_singleton_p (value_range_t *vr)
915 {
916 return (range_int_cst_p (vr)
917 && !is_overflow_infinity (vr->min)
918 && !is_overflow_infinity (vr->max)
919 && tree_int_cst_equal (vr->min, vr->max));
920 }
921
922 /* Return true if value range VR involves at least one symbol. */
923
924 static inline bool
925 symbolic_range_p (value_range_t *vr)
926 {
927 return (!is_gimple_min_invariant (vr->min)
928 || !is_gimple_min_invariant (vr->max));
929 }
930
931 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
932 otherwise. We only handle additive operations and set NEG to true if the
933 symbol is negated and INV to the invariant part, if any. */
934
935 static tree
936 get_single_symbol (tree t, bool *neg, tree *inv)
937 {
938 bool neg_;
939 tree inv_;
940
941 if (TREE_CODE (t) == PLUS_EXPR
942 || TREE_CODE (t) == POINTER_PLUS_EXPR
943 || TREE_CODE (t) == MINUS_EXPR)
944 {
945 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
946 {
947 neg_ = (TREE_CODE (t) == MINUS_EXPR);
948 inv_ = TREE_OPERAND (t, 0);
949 t = TREE_OPERAND (t, 1);
950 }
951 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
952 {
953 neg_ = false;
954 inv_ = TREE_OPERAND (t, 1);
955 t = TREE_OPERAND (t, 0);
956 }
957 else
958 return NULL_TREE;
959 }
960 else
961 {
962 neg_ = false;
963 inv_ = NULL_TREE;
964 }
965
966 if (TREE_CODE (t) == NEGATE_EXPR)
967 {
968 t = TREE_OPERAND (t, 0);
969 neg_ = !neg_;
970 }
971
972 if (TREE_CODE (t) != SSA_NAME)
973 return NULL_TREE;
974
975 *neg = neg_;
976 *inv = inv_;
977 return t;
978 }
979
980 /* The reverse operation: build a symbolic expression with TYPE
981 from symbol SYM, negated according to NEG, and invariant INV. */
982
983 static tree
984 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
985 {
986 const bool pointer_p = POINTER_TYPE_P (type);
987 tree t = sym;
988
989 if (neg)
990 t = build1 (NEGATE_EXPR, type, t);
991
992 if (integer_zerop (inv))
993 return t;
994
995 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
996 }
997
998 /* Return true if value range VR involves exactly one symbol SYM. */
999
1000 static bool
1001 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1002 {
1003 bool neg, min_has_symbol, max_has_symbol;
1004 tree inv;
1005
1006 if (is_gimple_min_invariant (vr->min))
1007 min_has_symbol = false;
1008 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1009 min_has_symbol = true;
1010 else
1011 return false;
1012
1013 if (is_gimple_min_invariant (vr->max))
1014 max_has_symbol = false;
1015 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1016 max_has_symbol = true;
1017 else
1018 return false;
1019
1020 return (min_has_symbol || max_has_symbol);
1021 }
1022
1023 /* Return true if value range VR uses an overflow infinity. */
1024
1025 static inline bool
1026 overflow_infinity_range_p (value_range_t *vr)
1027 {
1028 return (vr->type == VR_RANGE
1029 && (is_overflow_infinity (vr->min)
1030 || is_overflow_infinity (vr->max)));
1031 }
1032
1033 /* Return false if we can not make a valid comparison based on VR;
1034 this will be the case if it uses an overflow infinity and overflow
1035 is not undefined (i.e., -fno-strict-overflow is in effect).
1036 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1037 uses an overflow infinity. */
1038
1039 static bool
1040 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1041 {
1042 gcc_assert (vr->type == VR_RANGE);
1043 if (is_overflow_infinity (vr->min))
1044 {
1045 *strict_overflow_p = true;
1046 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1047 return false;
1048 }
1049 if (is_overflow_infinity (vr->max))
1050 {
1051 *strict_overflow_p = true;
1052 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1053 return false;
1054 }
1055 return true;
1056 }
1057
1058
1059 /* Return true if the result of assignment STMT is know to be non-negative.
1060 If the return value is based on the assumption that signed overflow is
1061 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1062 *STRICT_OVERFLOW_P.*/
1063
1064 static bool
1065 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1066 {
1067 enum tree_code code = gimple_assign_rhs_code (stmt);
1068 switch (get_gimple_rhs_class (code))
1069 {
1070 case GIMPLE_UNARY_RHS:
1071 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1072 gimple_expr_type (stmt),
1073 gimple_assign_rhs1 (stmt),
1074 strict_overflow_p);
1075 case GIMPLE_BINARY_RHS:
1076 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1077 gimple_expr_type (stmt),
1078 gimple_assign_rhs1 (stmt),
1079 gimple_assign_rhs2 (stmt),
1080 strict_overflow_p);
1081 case GIMPLE_TERNARY_RHS:
1082 return false;
1083 case GIMPLE_SINGLE_RHS:
1084 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1085 strict_overflow_p);
1086 case GIMPLE_INVALID_RHS:
1087 gcc_unreachable ();
1088 default:
1089 gcc_unreachable ();
1090 }
1091 }
1092
1093 /* Return true if return value of call STMT is know to be non-negative.
1094 If the return value is based on the assumption that signed overflow is
1095 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1096 *STRICT_OVERFLOW_P.*/
1097
1098 static bool
1099 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1100 {
1101 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1102 gimple_call_arg (stmt, 0) : NULL_TREE;
1103 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1104 gimple_call_arg (stmt, 1) : NULL_TREE;
1105
1106 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1107 gimple_call_fndecl (stmt),
1108 arg0,
1109 arg1,
1110 strict_overflow_p);
1111 }
1112
1113 /* Return true if STMT is know to to compute a non-negative value.
1114 If the return value is based on the assumption that signed overflow is
1115 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1116 *STRICT_OVERFLOW_P.*/
1117
1118 static bool
1119 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1120 {
1121 switch (gimple_code (stmt))
1122 {
1123 case GIMPLE_ASSIGN:
1124 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1125 case GIMPLE_CALL:
1126 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1127 default:
1128 gcc_unreachable ();
1129 }
1130 }
1131
1132 /* Return true if the result of assignment STMT is know to be non-zero.
1133 If the return value is based on the assumption that signed overflow is
1134 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1135 *STRICT_OVERFLOW_P.*/
1136
1137 static bool
1138 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1139 {
1140 enum tree_code code = gimple_assign_rhs_code (stmt);
1141 switch (get_gimple_rhs_class (code))
1142 {
1143 case GIMPLE_UNARY_RHS:
1144 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1145 gimple_expr_type (stmt),
1146 gimple_assign_rhs1 (stmt),
1147 strict_overflow_p);
1148 case GIMPLE_BINARY_RHS:
1149 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1150 gimple_expr_type (stmt),
1151 gimple_assign_rhs1 (stmt),
1152 gimple_assign_rhs2 (stmt),
1153 strict_overflow_p);
1154 case GIMPLE_TERNARY_RHS:
1155 return false;
1156 case GIMPLE_SINGLE_RHS:
1157 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1158 strict_overflow_p);
1159 case GIMPLE_INVALID_RHS:
1160 gcc_unreachable ();
1161 default:
1162 gcc_unreachable ();
1163 }
1164 }
1165
1166 /* Return true if STMT is known to compute a non-zero value.
1167 If the return value is based on the assumption that signed overflow is
1168 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1169 *STRICT_OVERFLOW_P.*/
1170
1171 static bool
1172 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1173 {
1174 switch (gimple_code (stmt))
1175 {
1176 case GIMPLE_ASSIGN:
1177 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1178 case GIMPLE_CALL:
1179 {
1180 tree fndecl = gimple_call_fndecl (stmt);
1181 if (!fndecl) return false;
1182 if (flag_delete_null_pointer_checks && !flag_check_new
1183 && DECL_IS_OPERATOR_NEW (fndecl)
1184 && !TREE_NOTHROW (fndecl))
1185 return true;
1186 if (flag_delete_null_pointer_checks &&
1187 lookup_attribute ("returns_nonnull",
1188 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1189 return true;
1190 return gimple_alloca_call_p (stmt);
1191 }
1192 default:
1193 gcc_unreachable ();
1194 }
1195 }
1196
1197 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1198 obtained so far. */
1199
1200 static bool
1201 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1202 {
1203 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1204 return true;
1205
1206 /* If we have an expression of the form &X->a, then the expression
1207 is nonnull if X is nonnull. */
1208 if (is_gimple_assign (stmt)
1209 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1210 {
1211 tree expr = gimple_assign_rhs1 (stmt);
1212 tree base = get_base_address (TREE_OPERAND (expr, 0));
1213
1214 if (base != NULL_TREE
1215 && TREE_CODE (base) == MEM_REF
1216 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1217 {
1218 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1219 if (range_is_nonnull (vr))
1220 return true;
1221 }
1222 }
1223
1224 return false;
1225 }
1226
1227 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1228 a gimple invariant, or SSA_NAME +- CST. */
1229
1230 static bool
1231 valid_value_p (tree expr)
1232 {
1233 if (TREE_CODE (expr) == SSA_NAME)
1234 return true;
1235
1236 if (TREE_CODE (expr) == PLUS_EXPR
1237 || TREE_CODE (expr) == MINUS_EXPR)
1238 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1239 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1240
1241 return is_gimple_min_invariant (expr);
1242 }
1243
1244 /* Return
1245 1 if VAL < VAL2
1246 0 if !(VAL < VAL2)
1247 -2 if those are incomparable. */
1248 static inline int
1249 operand_less_p (tree val, tree val2)
1250 {
1251 /* LT is folded faster than GE and others. Inline the common case. */
1252 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1253 return tree_int_cst_lt (val, val2);
1254 else
1255 {
1256 tree tcmp;
1257
1258 fold_defer_overflow_warnings ();
1259
1260 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1261
1262 fold_undefer_and_ignore_overflow_warnings ();
1263
1264 if (!tcmp
1265 || TREE_CODE (tcmp) != INTEGER_CST)
1266 return -2;
1267
1268 if (!integer_zerop (tcmp))
1269 return 1;
1270 }
1271
1272 /* val >= val2, not considering overflow infinity. */
1273 if (is_negative_overflow_infinity (val))
1274 return is_negative_overflow_infinity (val2) ? 0 : 1;
1275 else if (is_positive_overflow_infinity (val2))
1276 return is_positive_overflow_infinity (val) ? 0 : 1;
1277
1278 return 0;
1279 }
1280
1281 /* Compare two values VAL1 and VAL2. Return
1282
1283 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1284 -1 if VAL1 < VAL2,
1285 0 if VAL1 == VAL2,
1286 +1 if VAL1 > VAL2, and
1287 +2 if VAL1 != VAL2
1288
1289 This is similar to tree_int_cst_compare but supports pointer values
1290 and values that cannot be compared at compile time.
1291
1292 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1293 true if the return value is only valid if we assume that signed
1294 overflow is undefined. */
1295
1296 static int
1297 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1298 {
1299 if (val1 == val2)
1300 return 0;
1301
1302 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1303 both integers. */
1304 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1305 == POINTER_TYPE_P (TREE_TYPE (val2)));
1306
1307 /* Convert the two values into the same type. This is needed because
1308 sizetype causes sign extension even for unsigned types. */
1309 val2 = fold_convert (TREE_TYPE (val1), val2);
1310 STRIP_USELESS_TYPE_CONVERSION (val2);
1311
1312 if ((TREE_CODE (val1) == SSA_NAME
1313 || (TREE_CODE (val1) == NEGATE_EXPR
1314 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1315 || TREE_CODE (val1) == PLUS_EXPR
1316 || TREE_CODE (val1) == MINUS_EXPR)
1317 && (TREE_CODE (val2) == SSA_NAME
1318 || (TREE_CODE (val2) == NEGATE_EXPR
1319 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1320 || TREE_CODE (val2) == PLUS_EXPR
1321 || TREE_CODE (val2) == MINUS_EXPR))
1322 {
1323 tree n1, c1, n2, c2;
1324 enum tree_code code1, code2;
1325
1326 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1327 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1328 same name, return -2. */
1329 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1330 {
1331 code1 = SSA_NAME;
1332 n1 = val1;
1333 c1 = NULL_TREE;
1334 }
1335 else
1336 {
1337 code1 = TREE_CODE (val1);
1338 n1 = TREE_OPERAND (val1, 0);
1339 c1 = TREE_OPERAND (val1, 1);
1340 if (tree_int_cst_sgn (c1) == -1)
1341 {
1342 if (is_negative_overflow_infinity (c1))
1343 return -2;
1344 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1345 if (!c1)
1346 return -2;
1347 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1348 }
1349 }
1350
1351 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1352 {
1353 code2 = SSA_NAME;
1354 n2 = val2;
1355 c2 = NULL_TREE;
1356 }
1357 else
1358 {
1359 code2 = TREE_CODE (val2);
1360 n2 = TREE_OPERAND (val2, 0);
1361 c2 = TREE_OPERAND (val2, 1);
1362 if (tree_int_cst_sgn (c2) == -1)
1363 {
1364 if (is_negative_overflow_infinity (c2))
1365 return -2;
1366 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1367 if (!c2)
1368 return -2;
1369 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1370 }
1371 }
1372
1373 /* Both values must use the same name. */
1374 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1375 {
1376 n1 = TREE_OPERAND (n1, 0);
1377 n2 = TREE_OPERAND (n2, 0);
1378 }
1379 if (n1 != n2)
1380 return -2;
1381
1382 if (code1 == SSA_NAME && code2 == SSA_NAME)
1383 /* NAME == NAME */
1384 return 0;
1385
1386 /* If overflow is defined we cannot simplify more. */
1387 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1388 return -2;
1389
1390 if (strict_overflow_p != NULL
1391 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1392 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1393 *strict_overflow_p = true;
1394
1395 if (code1 == SSA_NAME)
1396 {
1397 if (code2 == PLUS_EXPR)
1398 /* NAME < NAME + CST */
1399 return -1;
1400 else if (code2 == MINUS_EXPR)
1401 /* NAME > NAME - CST */
1402 return 1;
1403 }
1404 else if (code1 == PLUS_EXPR)
1405 {
1406 if (code2 == SSA_NAME)
1407 /* NAME + CST > NAME */
1408 return 1;
1409 else if (code2 == PLUS_EXPR)
1410 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1411 return compare_values_warnv (c1, c2, strict_overflow_p);
1412 else if (code2 == MINUS_EXPR)
1413 /* NAME + CST1 > NAME - CST2 */
1414 return 1;
1415 }
1416 else if (code1 == MINUS_EXPR)
1417 {
1418 if (code2 == SSA_NAME)
1419 /* NAME - CST < NAME */
1420 return -1;
1421 else if (code2 == PLUS_EXPR)
1422 /* NAME - CST1 < NAME + CST2 */
1423 return -1;
1424 else if (code2 == MINUS_EXPR)
1425 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1426 C1 and C2 are swapped in the call to compare_values. */
1427 return compare_values_warnv (c2, c1, strict_overflow_p);
1428 }
1429
1430 gcc_unreachable ();
1431 }
1432
1433 /* We cannot compare non-constants. */
1434 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1435 return -2;
1436
1437 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1438 {
1439 /* We cannot compare overflowed values, except for overflow
1440 infinities. */
1441 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1442 {
1443 if (strict_overflow_p != NULL)
1444 *strict_overflow_p = true;
1445 if (is_negative_overflow_infinity (val1))
1446 return is_negative_overflow_infinity (val2) ? 0 : -1;
1447 else if (is_negative_overflow_infinity (val2))
1448 return 1;
1449 else if (is_positive_overflow_infinity (val1))
1450 return is_positive_overflow_infinity (val2) ? 0 : 1;
1451 else if (is_positive_overflow_infinity (val2))
1452 return -1;
1453 return -2;
1454 }
1455
1456 return tree_int_cst_compare (val1, val2);
1457 }
1458 else
1459 {
1460 tree t;
1461
1462 /* First see if VAL1 and VAL2 are not the same. */
1463 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1464 return 0;
1465
1466 /* If VAL1 is a lower address than VAL2, return -1. */
1467 if (operand_less_p (val1, val2) == 1)
1468 return -1;
1469
1470 /* If VAL1 is a higher address than VAL2, return +1. */
1471 if (operand_less_p (val2, val1) == 1)
1472 return 1;
1473
1474 /* If VAL1 is different than VAL2, return +2.
1475 For integer constants we either have already returned -1 or 1
1476 or they are equivalent. We still might succeed in proving
1477 something about non-trivial operands. */
1478 if (TREE_CODE (val1) != INTEGER_CST
1479 || TREE_CODE (val2) != INTEGER_CST)
1480 {
1481 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1482 if (t && integer_onep (t))
1483 return 2;
1484 }
1485
1486 return -2;
1487 }
1488 }
1489
1490 /* Compare values like compare_values_warnv, but treat comparisons of
1491 nonconstants which rely on undefined overflow as incomparable. */
1492
1493 static int
1494 compare_values (tree val1, tree val2)
1495 {
1496 bool sop;
1497 int ret;
1498
1499 sop = false;
1500 ret = compare_values_warnv (val1, val2, &sop);
1501 if (sop
1502 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1503 ret = -2;
1504 return ret;
1505 }
1506
1507
1508 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1509 0 if VAL is not inside [MIN, MAX],
1510 -2 if we cannot tell either way.
1511
1512 Benchmark compile/20001226-1.c compilation time after changing this
1513 function. */
1514
1515 static inline int
1516 value_inside_range (tree val, tree min, tree max)
1517 {
1518 int cmp1, cmp2;
1519
1520 cmp1 = operand_less_p (val, min);
1521 if (cmp1 == -2)
1522 return -2;
1523 if (cmp1 == 1)
1524 return 0;
1525
1526 cmp2 = operand_less_p (max, val);
1527 if (cmp2 == -2)
1528 return -2;
1529
1530 return !cmp2;
1531 }
1532
1533
1534 /* Return true if value ranges VR0 and VR1 have a non-empty
1535 intersection.
1536
1537 Benchmark compile/20001226-1.c compilation time after changing this
1538 function.
1539 */
1540
1541 static inline bool
1542 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1543 {
1544 /* The value ranges do not intersect if the maximum of the first range is
1545 less than the minimum of the second range or vice versa.
1546 When those relations are unknown, we can't do any better. */
1547 if (operand_less_p (vr0->max, vr1->min) != 0)
1548 return false;
1549 if (operand_less_p (vr1->max, vr0->min) != 0)
1550 return false;
1551 return true;
1552 }
1553
1554
1555 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1556 include the value zero, -2 if we cannot tell. */
1557
1558 static inline int
1559 range_includes_zero_p (tree min, tree max)
1560 {
1561 tree zero = build_int_cst (TREE_TYPE (min), 0);
1562 return value_inside_range (zero, min, max);
1563 }
1564
1565 /* Return true if *VR is know to only contain nonnegative values. */
1566
1567 static inline bool
1568 value_range_nonnegative_p (value_range_t *vr)
1569 {
1570 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1571 which would return a useful value should be encoded as a
1572 VR_RANGE. */
1573 if (vr->type == VR_RANGE)
1574 {
1575 int result = compare_values (vr->min, integer_zero_node);
1576 return (result == 0 || result == 1);
1577 }
1578
1579 return false;
1580 }
1581
1582 /* If *VR has a value rante that is a single constant value return that,
1583 otherwise return NULL_TREE. */
1584
1585 static tree
1586 value_range_constant_singleton (value_range_t *vr)
1587 {
1588 if (vr->type == VR_RANGE
1589 && operand_equal_p (vr->min, vr->max, 0)
1590 && is_gimple_min_invariant (vr->min))
1591 return vr->min;
1592
1593 return NULL_TREE;
1594 }
1595
1596 /* If OP has a value range with a single constant value return that,
1597 otherwise return NULL_TREE. This returns OP itself if OP is a
1598 constant. */
1599
1600 static tree
1601 op_with_constant_singleton_value_range (tree op)
1602 {
1603 if (is_gimple_min_invariant (op))
1604 return op;
1605
1606 if (TREE_CODE (op) != SSA_NAME)
1607 return NULL_TREE;
1608
1609 return value_range_constant_singleton (get_value_range (op));
1610 }
1611
1612 /* Return true if op is in a boolean [0, 1] value-range. */
1613
1614 static bool
1615 op_with_boolean_value_range_p (tree op)
1616 {
1617 value_range_t *vr;
1618
1619 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1620 return true;
1621
1622 if (integer_zerop (op)
1623 || integer_onep (op))
1624 return true;
1625
1626 if (TREE_CODE (op) != SSA_NAME)
1627 return false;
1628
1629 vr = get_value_range (op);
1630 return (vr->type == VR_RANGE
1631 && integer_zerop (vr->min)
1632 && integer_onep (vr->max));
1633 }
1634
1635 /* Extract value range information from an ASSERT_EXPR EXPR and store
1636 it in *VR_P. */
1637
1638 static void
1639 extract_range_from_assert (value_range_t *vr_p, tree expr)
1640 {
1641 tree var, cond, limit, min, max, type;
1642 value_range_t *limit_vr;
1643 enum tree_code cond_code;
1644
1645 var = ASSERT_EXPR_VAR (expr);
1646 cond = ASSERT_EXPR_COND (expr);
1647
1648 gcc_assert (COMPARISON_CLASS_P (cond));
1649
1650 /* Find VAR in the ASSERT_EXPR conditional. */
1651 if (var == TREE_OPERAND (cond, 0)
1652 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1653 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1654 {
1655 /* If the predicate is of the form VAR COMP LIMIT, then we just
1656 take LIMIT from the RHS and use the same comparison code. */
1657 cond_code = TREE_CODE (cond);
1658 limit = TREE_OPERAND (cond, 1);
1659 cond = TREE_OPERAND (cond, 0);
1660 }
1661 else
1662 {
1663 /* If the predicate is of the form LIMIT COMP VAR, then we need
1664 to flip around the comparison code to create the proper range
1665 for VAR. */
1666 cond_code = swap_tree_comparison (TREE_CODE (cond));
1667 limit = TREE_OPERAND (cond, 0);
1668 cond = TREE_OPERAND (cond, 1);
1669 }
1670
1671 limit = avoid_overflow_infinity (limit);
1672
1673 type = TREE_TYPE (var);
1674 gcc_assert (limit != var);
1675
1676 /* For pointer arithmetic, we only keep track of pointer equality
1677 and inequality. */
1678 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1679 {
1680 set_value_range_to_varying (vr_p);
1681 return;
1682 }
1683
1684 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1685 try to use LIMIT's range to avoid creating symbolic ranges
1686 unnecessarily. */
1687 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1688
1689 /* LIMIT's range is only interesting if it has any useful information. */
1690 if (limit_vr
1691 && (limit_vr->type == VR_UNDEFINED
1692 || limit_vr->type == VR_VARYING
1693 || symbolic_range_p (limit_vr)))
1694 limit_vr = NULL;
1695
1696 /* Initially, the new range has the same set of equivalences of
1697 VAR's range. This will be revised before returning the final
1698 value. Since assertions may be chained via mutually exclusive
1699 predicates, we will need to trim the set of equivalences before
1700 we are done. */
1701 gcc_assert (vr_p->equiv == NULL);
1702 add_equivalence (&vr_p->equiv, var);
1703
1704 /* Extract a new range based on the asserted comparison for VAR and
1705 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1706 will only use it for equality comparisons (EQ_EXPR). For any
1707 other kind of assertion, we cannot derive a range from LIMIT's
1708 anti-range that can be used to describe the new range. For
1709 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1710 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1711 no single range for x_2 that could describe LE_EXPR, so we might
1712 as well build the range [b_4, +INF] for it.
1713 One special case we handle is extracting a range from a
1714 range test encoded as (unsigned)var + CST <= limit. */
1715 if (TREE_CODE (cond) == NOP_EXPR
1716 || TREE_CODE (cond) == PLUS_EXPR)
1717 {
1718 if (TREE_CODE (cond) == PLUS_EXPR)
1719 {
1720 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1721 TREE_OPERAND (cond, 1));
1722 max = int_const_binop (PLUS_EXPR, limit, min);
1723 cond = TREE_OPERAND (cond, 0);
1724 }
1725 else
1726 {
1727 min = build_int_cst (TREE_TYPE (var), 0);
1728 max = limit;
1729 }
1730
1731 /* Make sure to not set TREE_OVERFLOW on the final type
1732 conversion. We are willingly interpreting large positive
1733 unsigned values as negative signed values here. */
1734 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1735 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1736
1737 /* We can transform a max, min range to an anti-range or
1738 vice-versa. Use set_and_canonicalize_value_range which does
1739 this for us. */
1740 if (cond_code == LE_EXPR)
1741 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1742 min, max, vr_p->equiv);
1743 else if (cond_code == GT_EXPR)
1744 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1745 min, max, vr_p->equiv);
1746 else
1747 gcc_unreachable ();
1748 }
1749 else if (cond_code == EQ_EXPR)
1750 {
1751 enum value_range_type range_type;
1752
1753 if (limit_vr)
1754 {
1755 range_type = limit_vr->type;
1756 min = limit_vr->min;
1757 max = limit_vr->max;
1758 }
1759 else
1760 {
1761 range_type = VR_RANGE;
1762 min = limit;
1763 max = limit;
1764 }
1765
1766 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1767
1768 /* When asserting the equality VAR == LIMIT and LIMIT is another
1769 SSA name, the new range will also inherit the equivalence set
1770 from LIMIT. */
1771 if (TREE_CODE (limit) == SSA_NAME)
1772 add_equivalence (&vr_p->equiv, limit);
1773 }
1774 else if (cond_code == NE_EXPR)
1775 {
1776 /* As described above, when LIMIT's range is an anti-range and
1777 this assertion is an inequality (NE_EXPR), then we cannot
1778 derive anything from the anti-range. For instance, if
1779 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1780 not imply that VAR's range is [0, 0]. So, in the case of
1781 anti-ranges, we just assert the inequality using LIMIT and
1782 not its anti-range.
1783
1784 If LIMIT_VR is a range, we can only use it to build a new
1785 anti-range if LIMIT_VR is a single-valued range. For
1786 instance, if LIMIT_VR is [0, 1], the predicate
1787 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1788 Rather, it means that for value 0 VAR should be ~[0, 0]
1789 and for value 1, VAR should be ~[1, 1]. We cannot
1790 represent these ranges.
1791
1792 The only situation in which we can build a valid
1793 anti-range is when LIMIT_VR is a single-valued range
1794 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1795 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1796 if (limit_vr
1797 && limit_vr->type == VR_RANGE
1798 && compare_values (limit_vr->min, limit_vr->max) == 0)
1799 {
1800 min = limit_vr->min;
1801 max = limit_vr->max;
1802 }
1803 else
1804 {
1805 /* In any other case, we cannot use LIMIT's range to build a
1806 valid anti-range. */
1807 min = max = limit;
1808 }
1809
1810 /* If MIN and MAX cover the whole range for their type, then
1811 just use the original LIMIT. */
1812 if (INTEGRAL_TYPE_P (type)
1813 && vrp_val_is_min (min)
1814 && vrp_val_is_max (max))
1815 min = max = limit;
1816
1817 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1818 min, max, vr_p->equiv);
1819 }
1820 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1821 {
1822 min = TYPE_MIN_VALUE (type);
1823
1824 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1825 max = limit;
1826 else
1827 {
1828 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1829 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1830 LT_EXPR. */
1831 max = limit_vr->max;
1832 }
1833
1834 /* If the maximum value forces us to be out of bounds, simply punt.
1835 It would be pointless to try and do anything more since this
1836 all should be optimized away above us. */
1837 if ((cond_code == LT_EXPR
1838 && compare_values (max, min) == 0)
1839 || is_overflow_infinity (max))
1840 set_value_range_to_varying (vr_p);
1841 else
1842 {
1843 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1844 if (cond_code == LT_EXPR)
1845 {
1846 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1847 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1848 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1849 build_int_cst (TREE_TYPE (max), -1));
1850 else
1851 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1852 build_int_cst (TREE_TYPE (max), 1));
1853 if (EXPR_P (max))
1854 TREE_NO_WARNING (max) = 1;
1855 }
1856
1857 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1858 }
1859 }
1860 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1861 {
1862 max = TYPE_MAX_VALUE (type);
1863
1864 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1865 min = limit;
1866 else
1867 {
1868 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1869 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1870 GT_EXPR. */
1871 min = limit_vr->min;
1872 }
1873
1874 /* If the minimum value forces us to be out of bounds, simply punt.
1875 It would be pointless to try and do anything more since this
1876 all should be optimized away above us. */
1877 if ((cond_code == GT_EXPR
1878 && compare_values (min, max) == 0)
1879 || is_overflow_infinity (min))
1880 set_value_range_to_varying (vr_p);
1881 else
1882 {
1883 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1884 if (cond_code == GT_EXPR)
1885 {
1886 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1887 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1888 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1889 build_int_cst (TREE_TYPE (min), -1));
1890 else
1891 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1892 build_int_cst (TREE_TYPE (min), 1));
1893 if (EXPR_P (min))
1894 TREE_NO_WARNING (min) = 1;
1895 }
1896
1897 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1898 }
1899 }
1900 else
1901 gcc_unreachable ();
1902
1903 /* Finally intersect the new range with what we already know about var. */
1904 vrp_intersect_ranges (vr_p, get_value_range (var));
1905 }
1906
1907
1908 /* Extract range information from SSA name VAR and store it in VR. If
1909 VAR has an interesting range, use it. Otherwise, create the
1910 range [VAR, VAR] and return it. This is useful in situations where
1911 we may have conditionals testing values of VARYING names. For
1912 instance,
1913
1914 x_3 = y_5;
1915 if (x_3 > y_5)
1916 ...
1917
1918 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1919 always false. */
1920
1921 static void
1922 extract_range_from_ssa_name (value_range_t *vr, tree var)
1923 {
1924 value_range_t *var_vr = get_value_range (var);
1925
1926 if (var_vr->type != VR_VARYING)
1927 copy_value_range (vr, var_vr);
1928 else
1929 set_value_range (vr, VR_RANGE, var, var, NULL);
1930
1931 add_equivalence (&vr->equiv, var);
1932 }
1933
1934
1935 /* Wrapper around int_const_binop. If the operation overflows and we
1936 are not using wrapping arithmetic, then adjust the result to be
1937 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1938 NULL_TREE if we need to use an overflow infinity representation but
1939 the type does not support it. */
1940
1941 static tree
1942 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1943 {
1944 tree res;
1945
1946 res = int_const_binop (code, val1, val2);
1947
1948 /* If we are using unsigned arithmetic, operate symbolically
1949 on -INF and +INF as int_const_binop only handles signed overflow. */
1950 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1951 {
1952 int checkz = compare_values (res, val1);
1953 bool overflow = false;
1954
1955 /* Ensure that res = val1 [+*] val2 >= val1
1956 or that res = val1 - val2 <= val1. */
1957 if ((code == PLUS_EXPR
1958 && !(checkz == 1 || checkz == 0))
1959 || (code == MINUS_EXPR
1960 && !(checkz == 0 || checkz == -1)))
1961 {
1962 overflow = true;
1963 }
1964 /* Checking for multiplication overflow is done by dividing the
1965 output of the multiplication by the first input of the
1966 multiplication. If the result of that division operation is
1967 not equal to the second input of the multiplication, then the
1968 multiplication overflowed. */
1969 else if (code == MULT_EXPR && !integer_zerop (val1))
1970 {
1971 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1972 res,
1973 val1);
1974 int check = compare_values (tmp, val2);
1975
1976 if (check != 0)
1977 overflow = true;
1978 }
1979
1980 if (overflow)
1981 {
1982 res = copy_node (res);
1983 TREE_OVERFLOW (res) = 1;
1984 }
1985
1986 }
1987 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1988 /* If the singed operation wraps then int_const_binop has done
1989 everything we want. */
1990 ;
1991 /* Signed division of -1/0 overflows and by the time it gets here
1992 returns NULL_TREE. */
1993 else if (!res)
1994 return NULL_TREE;
1995 else if ((TREE_OVERFLOW (res)
1996 && !TREE_OVERFLOW (val1)
1997 && !TREE_OVERFLOW (val2))
1998 || is_overflow_infinity (val1)
1999 || is_overflow_infinity (val2))
2000 {
2001 /* If the operation overflowed but neither VAL1 nor VAL2 are
2002 overflown, return -INF or +INF depending on the operation
2003 and the combination of signs of the operands. */
2004 int sgn1 = tree_int_cst_sgn (val1);
2005 int sgn2 = tree_int_cst_sgn (val2);
2006
2007 if (needs_overflow_infinity (TREE_TYPE (res))
2008 && !supports_overflow_infinity (TREE_TYPE (res)))
2009 return NULL_TREE;
2010
2011 /* We have to punt on adding infinities of different signs,
2012 since we can't tell what the sign of the result should be.
2013 Likewise for subtracting infinities of the same sign. */
2014 if (((code == PLUS_EXPR && sgn1 != sgn2)
2015 || (code == MINUS_EXPR && sgn1 == sgn2))
2016 && is_overflow_infinity (val1)
2017 && is_overflow_infinity (val2))
2018 return NULL_TREE;
2019
2020 /* Don't try to handle division or shifting of infinities. */
2021 if ((code == TRUNC_DIV_EXPR
2022 || code == FLOOR_DIV_EXPR
2023 || code == CEIL_DIV_EXPR
2024 || code == EXACT_DIV_EXPR
2025 || code == ROUND_DIV_EXPR
2026 || code == RSHIFT_EXPR)
2027 && (is_overflow_infinity (val1)
2028 || is_overflow_infinity (val2)))
2029 return NULL_TREE;
2030
2031 /* Notice that we only need to handle the restricted set of
2032 operations handled by extract_range_from_binary_expr.
2033 Among them, only multiplication, addition and subtraction
2034 can yield overflow without overflown operands because we
2035 are working with integral types only... except in the
2036 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2037 for division too. */
2038
2039 /* For multiplication, the sign of the overflow is given
2040 by the comparison of the signs of the operands. */
2041 if ((code == MULT_EXPR && sgn1 == sgn2)
2042 /* For addition, the operands must be of the same sign
2043 to yield an overflow. Its sign is therefore that
2044 of one of the operands, for example the first. For
2045 infinite operands X + -INF is negative, not positive. */
2046 || (code == PLUS_EXPR
2047 && (sgn1 >= 0
2048 ? !is_negative_overflow_infinity (val2)
2049 : is_positive_overflow_infinity (val2)))
2050 /* For subtraction, non-infinite operands must be of
2051 different signs to yield an overflow. Its sign is
2052 therefore that of the first operand or the opposite of
2053 that of the second operand. A first operand of 0 counts
2054 as positive here, for the corner case 0 - (-INF), which
2055 overflows, but must yield +INF. For infinite operands 0
2056 - INF is negative, not positive. */
2057 || (code == MINUS_EXPR
2058 && (sgn1 >= 0
2059 ? !is_positive_overflow_infinity (val2)
2060 : is_negative_overflow_infinity (val2)))
2061 /* We only get in here with positive shift count, so the
2062 overflow direction is the same as the sign of val1.
2063 Actually rshift does not overflow at all, but we only
2064 handle the case of shifting overflowed -INF and +INF. */
2065 || (code == RSHIFT_EXPR
2066 && sgn1 >= 0)
2067 /* For division, the only case is -INF / -1 = +INF. */
2068 || code == TRUNC_DIV_EXPR
2069 || code == FLOOR_DIV_EXPR
2070 || code == CEIL_DIV_EXPR
2071 || code == EXACT_DIV_EXPR
2072 || code == ROUND_DIV_EXPR)
2073 return (needs_overflow_infinity (TREE_TYPE (res))
2074 ? positive_overflow_infinity (TREE_TYPE (res))
2075 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2076 else
2077 return (needs_overflow_infinity (TREE_TYPE (res))
2078 ? negative_overflow_infinity (TREE_TYPE (res))
2079 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2080 }
2081
2082 return res;
2083 }
2084
2085
2086 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2087 bitmask if some bit is unset, it means for all numbers in the range
2088 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2089 bitmask if some bit is set, it means for all numbers in the range
2090 the bit is 1, otherwise it might be 0 or 1. */
2091
2092 static bool
2093 zero_nonzero_bits_from_vr (const tree expr_type,
2094 value_range_t *vr,
2095 wide_int *may_be_nonzero,
2096 wide_int *must_be_nonzero)
2097 {
2098 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2099 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2100 if (!range_int_cst_p (vr)
2101 || is_overflow_infinity (vr->min)
2102 || is_overflow_infinity (vr->max))
2103 return false;
2104
2105 if (range_int_cst_singleton_p (vr))
2106 {
2107 *may_be_nonzero = vr->min;
2108 *must_be_nonzero = *may_be_nonzero;
2109 }
2110 else if (tree_int_cst_sgn (vr->min) >= 0
2111 || tree_int_cst_sgn (vr->max) < 0)
2112 {
2113 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2114 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2115 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2116 if (xor_mask != 0)
2117 {
2118 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2119 may_be_nonzero->get_precision ());
2120 *may_be_nonzero = *may_be_nonzero | mask;
2121 *must_be_nonzero = must_be_nonzero->and_not (mask);
2122 }
2123 }
2124
2125 return true;
2126 }
2127
2128 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2129 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2130 false otherwise. If *AR can be represented with a single range
2131 *VR1 will be VR_UNDEFINED. */
2132
2133 static bool
2134 ranges_from_anti_range (value_range_t *ar,
2135 value_range_t *vr0, value_range_t *vr1)
2136 {
2137 tree type = TREE_TYPE (ar->min);
2138
2139 vr0->type = VR_UNDEFINED;
2140 vr1->type = VR_UNDEFINED;
2141
2142 if (ar->type != VR_ANTI_RANGE
2143 || TREE_CODE (ar->min) != INTEGER_CST
2144 || TREE_CODE (ar->max) != INTEGER_CST
2145 || !vrp_val_min (type)
2146 || !vrp_val_max (type))
2147 return false;
2148
2149 if (!vrp_val_is_min (ar->min))
2150 {
2151 vr0->type = VR_RANGE;
2152 vr0->min = vrp_val_min (type);
2153 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2154 }
2155 if (!vrp_val_is_max (ar->max))
2156 {
2157 vr1->type = VR_RANGE;
2158 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2159 vr1->max = vrp_val_max (type);
2160 }
2161 if (vr0->type == VR_UNDEFINED)
2162 {
2163 *vr0 = *vr1;
2164 vr1->type = VR_UNDEFINED;
2165 }
2166
2167 return vr0->type != VR_UNDEFINED;
2168 }
2169
2170 /* Helper to extract a value-range *VR for a multiplicative operation
2171 *VR0 CODE *VR1. */
2172
2173 static void
2174 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2175 enum tree_code code,
2176 value_range_t *vr0, value_range_t *vr1)
2177 {
2178 enum value_range_type type;
2179 tree val[4];
2180 size_t i;
2181 tree min, max;
2182 bool sop;
2183 int cmp;
2184
2185 /* Multiplications, divisions and shifts are a bit tricky to handle,
2186 depending on the mix of signs we have in the two ranges, we
2187 need to operate on different values to get the minimum and
2188 maximum values for the new range. One approach is to figure
2189 out all the variations of range combinations and do the
2190 operations.
2191
2192 However, this involves several calls to compare_values and it
2193 is pretty convoluted. It's simpler to do the 4 operations
2194 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2195 MAX1) and then figure the smallest and largest values to form
2196 the new range. */
2197 gcc_assert (code == MULT_EXPR
2198 || code == TRUNC_DIV_EXPR
2199 || code == FLOOR_DIV_EXPR
2200 || code == CEIL_DIV_EXPR
2201 || code == EXACT_DIV_EXPR
2202 || code == ROUND_DIV_EXPR
2203 || code == RSHIFT_EXPR
2204 || code == LSHIFT_EXPR);
2205 gcc_assert ((vr0->type == VR_RANGE
2206 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2207 && vr0->type == vr1->type);
2208
2209 type = vr0->type;
2210
2211 /* Compute the 4 cross operations. */
2212 sop = false;
2213 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2214 if (val[0] == NULL_TREE)
2215 sop = true;
2216
2217 if (vr1->max == vr1->min)
2218 val[1] = NULL_TREE;
2219 else
2220 {
2221 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2222 if (val[1] == NULL_TREE)
2223 sop = true;
2224 }
2225
2226 if (vr0->max == vr0->min)
2227 val[2] = NULL_TREE;
2228 else
2229 {
2230 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2231 if (val[2] == NULL_TREE)
2232 sop = true;
2233 }
2234
2235 if (vr0->min == vr0->max || vr1->min == vr1->max)
2236 val[3] = NULL_TREE;
2237 else
2238 {
2239 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2240 if (val[3] == NULL_TREE)
2241 sop = true;
2242 }
2243
2244 if (sop)
2245 {
2246 set_value_range_to_varying (vr);
2247 return;
2248 }
2249
2250 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2251 of VAL[i]. */
2252 min = val[0];
2253 max = val[0];
2254 for (i = 1; i < 4; i++)
2255 {
2256 if (!is_gimple_min_invariant (min)
2257 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2258 || !is_gimple_min_invariant (max)
2259 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2260 break;
2261
2262 if (val[i])
2263 {
2264 if (!is_gimple_min_invariant (val[i])
2265 || (TREE_OVERFLOW (val[i])
2266 && !is_overflow_infinity (val[i])))
2267 {
2268 /* If we found an overflowed value, set MIN and MAX
2269 to it so that we set the resulting range to
2270 VARYING. */
2271 min = max = val[i];
2272 break;
2273 }
2274
2275 if (compare_values (val[i], min) == -1)
2276 min = val[i];
2277
2278 if (compare_values (val[i], max) == 1)
2279 max = val[i];
2280 }
2281 }
2282
2283 /* If either MIN or MAX overflowed, then set the resulting range to
2284 VARYING. But we do accept an overflow infinity
2285 representation. */
2286 if (min == NULL_TREE
2287 || !is_gimple_min_invariant (min)
2288 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2289 || max == NULL_TREE
2290 || !is_gimple_min_invariant (max)
2291 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2292 {
2293 set_value_range_to_varying (vr);
2294 return;
2295 }
2296
2297 /* We punt if:
2298 1) [-INF, +INF]
2299 2) [-INF, +-INF(OVF)]
2300 3) [+-INF(OVF), +INF]
2301 4) [+-INF(OVF), +-INF(OVF)]
2302 We learn nothing when we have INF and INF(OVF) on both sides.
2303 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2304 overflow. */
2305 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2306 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2307 {
2308 set_value_range_to_varying (vr);
2309 return;
2310 }
2311
2312 cmp = compare_values (min, max);
2313 if (cmp == -2 || cmp == 1)
2314 {
2315 /* If the new range has its limits swapped around (MIN > MAX),
2316 then the operation caused one of them to wrap around, mark
2317 the new range VARYING. */
2318 set_value_range_to_varying (vr);
2319 }
2320 else
2321 set_value_range (vr, type, min, max, NULL);
2322 }
2323
2324 /* Extract range information from a binary operation CODE based on
2325 the ranges of each of its operands *VR0 and *VR1 with resulting
2326 type EXPR_TYPE. The resulting range is stored in *VR. */
2327
2328 static void
2329 extract_range_from_binary_expr_1 (value_range_t *vr,
2330 enum tree_code code, tree expr_type,
2331 value_range_t *vr0_, value_range_t *vr1_)
2332 {
2333 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2334 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2335 enum value_range_type type;
2336 tree min = NULL_TREE, max = NULL_TREE;
2337 int cmp;
2338
2339 if (!INTEGRAL_TYPE_P (expr_type)
2340 && !POINTER_TYPE_P (expr_type))
2341 {
2342 set_value_range_to_varying (vr);
2343 return;
2344 }
2345
2346 /* Not all binary expressions can be applied to ranges in a
2347 meaningful way. Handle only arithmetic operations. */
2348 if (code != PLUS_EXPR
2349 && code != MINUS_EXPR
2350 && code != POINTER_PLUS_EXPR
2351 && code != MULT_EXPR
2352 && code != TRUNC_DIV_EXPR
2353 && code != FLOOR_DIV_EXPR
2354 && code != CEIL_DIV_EXPR
2355 && code != EXACT_DIV_EXPR
2356 && code != ROUND_DIV_EXPR
2357 && code != TRUNC_MOD_EXPR
2358 && code != RSHIFT_EXPR
2359 && code != LSHIFT_EXPR
2360 && code != MIN_EXPR
2361 && code != MAX_EXPR
2362 && code != BIT_AND_EXPR
2363 && code != BIT_IOR_EXPR
2364 && code != BIT_XOR_EXPR)
2365 {
2366 set_value_range_to_varying (vr);
2367 return;
2368 }
2369
2370 /* If both ranges are UNDEFINED, so is the result. */
2371 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2372 {
2373 set_value_range_to_undefined (vr);
2374 return;
2375 }
2376 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2377 code. At some point we may want to special-case operations that
2378 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2379 operand. */
2380 else if (vr0.type == VR_UNDEFINED)
2381 set_value_range_to_varying (&vr0);
2382 else if (vr1.type == VR_UNDEFINED)
2383 set_value_range_to_varying (&vr1);
2384
2385 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2386 and express ~[] op X as ([]' op X) U ([]'' op X). */
2387 if (vr0.type == VR_ANTI_RANGE
2388 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2389 {
2390 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2391 if (vrtem1.type != VR_UNDEFINED)
2392 {
2393 value_range_t vrres = VR_INITIALIZER;
2394 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2395 &vrtem1, vr1_);
2396 vrp_meet (vr, &vrres);
2397 }
2398 return;
2399 }
2400 /* Likewise for X op ~[]. */
2401 if (vr1.type == VR_ANTI_RANGE
2402 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2403 {
2404 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2405 if (vrtem1.type != VR_UNDEFINED)
2406 {
2407 value_range_t vrres = VR_INITIALIZER;
2408 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2409 vr0_, &vrtem1);
2410 vrp_meet (vr, &vrres);
2411 }
2412 return;
2413 }
2414
2415 /* The type of the resulting value range defaults to VR0.TYPE. */
2416 type = vr0.type;
2417
2418 /* Refuse to operate on VARYING ranges, ranges of different kinds
2419 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2420 because we may be able to derive a useful range even if one of
2421 the operands is VR_VARYING or symbolic range. Similarly for
2422 divisions, MIN/MAX and PLUS/MINUS.
2423
2424 TODO, we may be able to derive anti-ranges in some cases. */
2425 if (code != BIT_AND_EXPR
2426 && code != BIT_IOR_EXPR
2427 && code != TRUNC_DIV_EXPR
2428 && code != FLOOR_DIV_EXPR
2429 && code != CEIL_DIV_EXPR
2430 && code != EXACT_DIV_EXPR
2431 && code != ROUND_DIV_EXPR
2432 && code != TRUNC_MOD_EXPR
2433 && code != MIN_EXPR
2434 && code != MAX_EXPR
2435 && code != PLUS_EXPR
2436 && code != MINUS_EXPR
2437 && (vr0.type == VR_VARYING
2438 || vr1.type == VR_VARYING
2439 || vr0.type != vr1.type
2440 || symbolic_range_p (&vr0)
2441 || symbolic_range_p (&vr1)))
2442 {
2443 set_value_range_to_varying (vr);
2444 return;
2445 }
2446
2447 /* Now evaluate the expression to determine the new range. */
2448 if (POINTER_TYPE_P (expr_type))
2449 {
2450 if (code == MIN_EXPR || code == MAX_EXPR)
2451 {
2452 /* For MIN/MAX expressions with pointers, we only care about
2453 nullness, if both are non null, then the result is nonnull.
2454 If both are null, then the result is null. Otherwise they
2455 are varying. */
2456 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2457 set_value_range_to_nonnull (vr, expr_type);
2458 else if (range_is_null (&vr0) && range_is_null (&vr1))
2459 set_value_range_to_null (vr, expr_type);
2460 else
2461 set_value_range_to_varying (vr);
2462 }
2463 else if (code == POINTER_PLUS_EXPR)
2464 {
2465 /* For pointer types, we are really only interested in asserting
2466 whether the expression evaluates to non-NULL. */
2467 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2468 set_value_range_to_nonnull (vr, expr_type);
2469 else if (range_is_null (&vr0) && range_is_null (&vr1))
2470 set_value_range_to_null (vr, expr_type);
2471 else
2472 set_value_range_to_varying (vr);
2473 }
2474 else if (code == BIT_AND_EXPR)
2475 {
2476 /* For pointer types, we are really only interested in asserting
2477 whether the expression evaluates to non-NULL. */
2478 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2479 set_value_range_to_nonnull (vr, expr_type);
2480 else if (range_is_null (&vr0) || range_is_null (&vr1))
2481 set_value_range_to_null (vr, expr_type);
2482 else
2483 set_value_range_to_varying (vr);
2484 }
2485 else
2486 set_value_range_to_varying (vr);
2487
2488 return;
2489 }
2490
2491 /* For integer ranges, apply the operation to each end of the
2492 range and see what we end up with. */
2493 if (code == PLUS_EXPR || code == MINUS_EXPR)
2494 {
2495 const bool minus_p = (code == MINUS_EXPR);
2496 tree min_op0 = vr0.min;
2497 tree min_op1 = minus_p ? vr1.max : vr1.min;
2498 tree max_op0 = vr0.max;
2499 tree max_op1 = minus_p ? vr1.min : vr1.max;
2500 tree sym_min_op0 = NULL_TREE;
2501 tree sym_min_op1 = NULL_TREE;
2502 tree sym_max_op0 = NULL_TREE;
2503 tree sym_max_op1 = NULL_TREE;
2504 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2505
2506 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2507 single-symbolic ranges, try to compute the precise resulting range,
2508 but only if we know that this resulting range will also be constant
2509 or single-symbolic. */
2510 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2511 && (TREE_CODE (min_op0) == INTEGER_CST
2512 || (sym_min_op0
2513 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2514 && (TREE_CODE (min_op1) == INTEGER_CST
2515 || (sym_min_op1
2516 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2517 && (!(sym_min_op0 && sym_min_op1)
2518 || (sym_min_op0 == sym_min_op1
2519 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2520 && (TREE_CODE (max_op0) == INTEGER_CST
2521 || (sym_max_op0
2522 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2523 && (TREE_CODE (max_op1) == INTEGER_CST
2524 || (sym_max_op1
2525 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2526 && (!(sym_max_op0 && sym_max_op1)
2527 || (sym_max_op0 == sym_max_op1
2528 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2529 {
2530 const signop sgn = TYPE_SIGN (expr_type);
2531 const unsigned int prec = TYPE_PRECISION (expr_type);
2532 wide_int type_min, type_max, wmin, wmax;
2533 int min_ovf = 0;
2534 int max_ovf = 0;
2535
2536 /* Get the lower and upper bounds of the type. */
2537 if (TYPE_OVERFLOW_WRAPS (expr_type))
2538 {
2539 type_min = wi::min_value (prec, sgn);
2540 type_max = wi::max_value (prec, sgn);
2541 }
2542 else
2543 {
2544 type_min = vrp_val_min (expr_type);
2545 type_max = vrp_val_max (expr_type);
2546 }
2547
2548 /* Combine the lower bounds, if any. */
2549 if (min_op0 && min_op1)
2550 {
2551 if (minus_p)
2552 {
2553 wmin = wi::sub (min_op0, min_op1);
2554
2555 /* Check for overflow. */
2556 if (wi::cmp (0, min_op1, sgn)
2557 != wi::cmp (wmin, min_op0, sgn))
2558 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2559 }
2560 else
2561 {
2562 wmin = wi::add (min_op0, min_op1);
2563
2564 /* Check for overflow. */
2565 if (wi::cmp (min_op1, 0, sgn)
2566 != wi::cmp (wmin, min_op0, sgn))
2567 min_ovf = wi::cmp (min_op0, wmin, sgn);
2568 }
2569 }
2570 else if (min_op0)
2571 wmin = min_op0;
2572 else if (min_op1)
2573 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2574 else
2575 wmin = wi::shwi (0, prec);
2576
2577 /* Combine the upper bounds, if any. */
2578 if (max_op0 && max_op1)
2579 {
2580 if (minus_p)
2581 {
2582 wmax = wi::sub (max_op0, max_op1);
2583
2584 /* Check for overflow. */
2585 if (wi::cmp (0, max_op1, sgn)
2586 != wi::cmp (wmax, max_op0, sgn))
2587 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2588 }
2589 else
2590 {
2591 wmax = wi::add (max_op0, max_op1);
2592
2593 if (wi::cmp (max_op1, 0, sgn)
2594 != wi::cmp (wmax, max_op0, sgn))
2595 max_ovf = wi::cmp (max_op0, wmax, sgn);
2596 }
2597 }
2598 else if (max_op0)
2599 wmax = max_op0;
2600 else if (max_op1)
2601 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2602 else
2603 wmax = wi::shwi (0, prec);
2604
2605 /* Check for type overflow. */
2606 if (min_ovf == 0)
2607 {
2608 if (wi::cmp (wmin, type_min, sgn) == -1)
2609 min_ovf = -1;
2610 else if (wi::cmp (wmin, type_max, sgn) == 1)
2611 min_ovf = 1;
2612 }
2613 if (max_ovf == 0)
2614 {
2615 if (wi::cmp (wmax, type_min, sgn) == -1)
2616 max_ovf = -1;
2617 else if (wi::cmp (wmax, type_max, sgn) == 1)
2618 max_ovf = 1;
2619 }
2620
2621 /* If we have overflow for the constant part and the resulting
2622 range will be symbolic, drop to VR_VARYING. */
2623 if ((min_ovf && sym_min_op0 != sym_min_op1)
2624 || (max_ovf && sym_max_op0 != sym_max_op1))
2625 {
2626 set_value_range_to_varying (vr);
2627 return;
2628 }
2629
2630 if (TYPE_OVERFLOW_WRAPS (expr_type))
2631 {
2632 /* If overflow wraps, truncate the values and adjust the
2633 range kind and bounds appropriately. */
2634 wide_int tmin = wide_int::from (wmin, prec, sgn);
2635 wide_int tmax = wide_int::from (wmax, prec, sgn);
2636 if (min_ovf == max_ovf)
2637 {
2638 /* No overflow or both overflow or underflow. The
2639 range kind stays VR_RANGE. */
2640 min = wide_int_to_tree (expr_type, tmin);
2641 max = wide_int_to_tree (expr_type, tmax);
2642 }
2643 else if (min_ovf == -1 && max_ovf == 1)
2644 {
2645 /* Underflow and overflow, drop to VR_VARYING. */
2646 set_value_range_to_varying (vr);
2647 return;
2648 }
2649 else
2650 {
2651 /* Min underflow or max overflow. The range kind
2652 changes to VR_ANTI_RANGE. */
2653 bool covers = false;
2654 wide_int tem = tmin;
2655 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2656 || (max_ovf == 1 && min_ovf == 0));
2657 type = VR_ANTI_RANGE;
2658 tmin = tmax + 1;
2659 if (wi::cmp (tmin, tmax, sgn) < 0)
2660 covers = true;
2661 tmax = tem - 1;
2662 if (wi::cmp (tmax, tem, sgn) > 0)
2663 covers = true;
2664 /* If the anti-range would cover nothing, drop to varying.
2665 Likewise if the anti-range bounds are outside of the
2666 types values. */
2667 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2668 {
2669 set_value_range_to_varying (vr);
2670 return;
2671 }
2672 min = wide_int_to_tree (expr_type, tmin);
2673 max = wide_int_to_tree (expr_type, tmax);
2674 }
2675 }
2676 else
2677 {
2678 /* If overflow does not wrap, saturate to the types min/max
2679 value. */
2680 if (min_ovf == -1)
2681 {
2682 if (needs_overflow_infinity (expr_type)
2683 && supports_overflow_infinity (expr_type))
2684 min = negative_overflow_infinity (expr_type);
2685 else
2686 min = wide_int_to_tree (expr_type, type_min);
2687 }
2688 else if (min_ovf == 1)
2689 {
2690 if (needs_overflow_infinity (expr_type)
2691 && supports_overflow_infinity (expr_type))
2692 min = positive_overflow_infinity (expr_type);
2693 else
2694 min = wide_int_to_tree (expr_type, type_max);
2695 }
2696 else
2697 min = wide_int_to_tree (expr_type, wmin);
2698
2699 if (max_ovf == -1)
2700 {
2701 if (needs_overflow_infinity (expr_type)
2702 && supports_overflow_infinity (expr_type))
2703 max = negative_overflow_infinity (expr_type);
2704 else
2705 max = wide_int_to_tree (expr_type, type_min);
2706 }
2707 else if (max_ovf == 1)
2708 {
2709 if (needs_overflow_infinity (expr_type)
2710 && supports_overflow_infinity (expr_type))
2711 max = positive_overflow_infinity (expr_type);
2712 else
2713 max = wide_int_to_tree (expr_type, type_max);
2714 }
2715 else
2716 max = wide_int_to_tree (expr_type, wmax);
2717 }
2718
2719 if (needs_overflow_infinity (expr_type)
2720 && supports_overflow_infinity (expr_type))
2721 {
2722 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2723 || (min_op1
2724 && (minus_p
2725 ? is_positive_overflow_infinity (min_op1)
2726 : is_negative_overflow_infinity (min_op1))))
2727 min = negative_overflow_infinity (expr_type);
2728 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2729 || (max_op1
2730 && (minus_p
2731 ? is_negative_overflow_infinity (max_op1)
2732 : is_positive_overflow_infinity (max_op1))))
2733 max = positive_overflow_infinity (expr_type);
2734 }
2735
2736 /* If the result lower bound is constant, we're done;
2737 otherwise, build the symbolic lower bound. */
2738 if (sym_min_op0 == sym_min_op1)
2739 ;
2740 else if (sym_min_op0)
2741 min = build_symbolic_expr (expr_type, sym_min_op0,
2742 neg_min_op0, min);
2743 else if (sym_min_op1)
2744 min = build_symbolic_expr (expr_type, sym_min_op1,
2745 neg_min_op1 ^ minus_p, min);
2746
2747 /* Likewise for the upper bound. */
2748 if (sym_max_op0 == sym_max_op1)
2749 ;
2750 else if (sym_max_op0)
2751 max = build_symbolic_expr (expr_type, sym_max_op0,
2752 neg_max_op0, max);
2753 else if (sym_max_op1)
2754 max = build_symbolic_expr (expr_type, sym_max_op1,
2755 neg_max_op1 ^ minus_p, max);
2756 }
2757 else
2758 {
2759 /* For other cases, for example if we have a PLUS_EXPR with two
2760 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2761 to compute a precise range for such a case.
2762 ??? General even mixed range kind operations can be expressed
2763 by for example transforming ~[3, 5] + [1, 2] to range-only
2764 operations and a union primitive:
2765 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2766 [-INF+1, 4] U [6, +INF(OVF)]
2767 though usually the union is not exactly representable with
2768 a single range or anti-range as the above is
2769 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2770 but one could use a scheme similar to equivalences for this. */
2771 set_value_range_to_varying (vr);
2772 return;
2773 }
2774 }
2775 else if (code == MIN_EXPR
2776 || code == MAX_EXPR)
2777 {
2778 if (vr0.type == VR_RANGE
2779 && !symbolic_range_p (&vr0))
2780 {
2781 type = VR_RANGE;
2782 if (vr1.type == VR_RANGE
2783 && !symbolic_range_p (&vr1))
2784 {
2785 /* For operations that make the resulting range directly
2786 proportional to the original ranges, apply the operation to
2787 the same end of each range. */
2788 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2789 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2790 }
2791 else if (code == MIN_EXPR)
2792 {
2793 min = vrp_val_min (expr_type);
2794 max = vr0.max;
2795 }
2796 else if (code == MAX_EXPR)
2797 {
2798 min = vr0.min;
2799 max = vrp_val_max (expr_type);
2800 }
2801 }
2802 else if (vr1.type == VR_RANGE
2803 && !symbolic_range_p (&vr1))
2804 {
2805 type = VR_RANGE;
2806 if (code == MIN_EXPR)
2807 {
2808 min = vrp_val_min (expr_type);
2809 max = vr1.max;
2810 }
2811 else if (code == MAX_EXPR)
2812 {
2813 min = vr1.min;
2814 max = vrp_val_max (expr_type);
2815 }
2816 }
2817 else
2818 {
2819 set_value_range_to_varying (vr);
2820 return;
2821 }
2822 }
2823 else if (code == MULT_EXPR)
2824 {
2825 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2826 drop to varying. This test requires 2*prec bits if both
2827 operands are signed and 2*prec + 2 bits if either is not. */
2828
2829 signop sign = TYPE_SIGN (expr_type);
2830 unsigned int prec = TYPE_PRECISION (expr_type);
2831
2832 if (range_int_cst_p (&vr0)
2833 && range_int_cst_p (&vr1)
2834 && TYPE_OVERFLOW_WRAPS (expr_type))
2835 {
2836 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2837 typedef generic_wide_int
2838 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2839 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2840 vrp_int size = sizem1 + 1;
2841
2842 /* Extend the values using the sign of the result to PREC2.
2843 From here on out, everthing is just signed math no matter
2844 what the input types were. */
2845 vrp_int min0 = vrp_int_cst (vr0.min);
2846 vrp_int max0 = vrp_int_cst (vr0.max);
2847 vrp_int min1 = vrp_int_cst (vr1.min);
2848 vrp_int max1 = vrp_int_cst (vr1.max);
2849 /* Canonicalize the intervals. */
2850 if (sign == UNSIGNED)
2851 {
2852 if (wi::ltu_p (size, min0 + max0))
2853 {
2854 min0 -= size;
2855 max0 -= size;
2856 }
2857
2858 if (wi::ltu_p (size, min1 + max1))
2859 {
2860 min1 -= size;
2861 max1 -= size;
2862 }
2863 }
2864
2865 vrp_int prod0 = min0 * min1;
2866 vrp_int prod1 = min0 * max1;
2867 vrp_int prod2 = max0 * min1;
2868 vrp_int prod3 = max0 * max1;
2869
2870 /* Sort the 4 products so that min is in prod0 and max is in
2871 prod3. */
2872 /* min0min1 > max0max1 */
2873 if (wi::gts_p (prod0, prod3))
2874 {
2875 vrp_int tmp = prod3;
2876 prod3 = prod0;
2877 prod0 = tmp;
2878 }
2879
2880 /* min0max1 > max0min1 */
2881 if (wi::gts_p (prod1, prod2))
2882 {
2883 vrp_int tmp = prod2;
2884 prod2 = prod1;
2885 prod1 = tmp;
2886 }
2887
2888 if (wi::gts_p (prod0, prod1))
2889 {
2890 vrp_int tmp = prod1;
2891 prod1 = prod0;
2892 prod0 = tmp;
2893 }
2894
2895 if (wi::gts_p (prod2, prod3))
2896 {
2897 vrp_int tmp = prod3;
2898 prod3 = prod2;
2899 prod2 = tmp;
2900 }
2901
2902 /* diff = max - min. */
2903 prod2 = prod3 - prod0;
2904 if (wi::geu_p (prod2, sizem1))
2905 {
2906 /* the range covers all values. */
2907 set_value_range_to_varying (vr);
2908 return;
2909 }
2910
2911 /* The following should handle the wrapping and selecting
2912 VR_ANTI_RANGE for us. */
2913 min = wide_int_to_tree (expr_type, prod0);
2914 max = wide_int_to_tree (expr_type, prod3);
2915 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2916 return;
2917 }
2918
2919 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2920 drop to VR_VARYING. It would take more effort to compute a
2921 precise range for such a case. For example, if we have
2922 op0 == 65536 and op1 == 65536 with their ranges both being
2923 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2924 we cannot claim that the product is in ~[0,0]. Note that we
2925 are guaranteed to have vr0.type == vr1.type at this
2926 point. */
2927 if (vr0.type == VR_ANTI_RANGE
2928 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2929 {
2930 set_value_range_to_varying (vr);
2931 return;
2932 }
2933
2934 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2935 return;
2936 }
2937 else if (code == RSHIFT_EXPR
2938 || code == LSHIFT_EXPR)
2939 {
2940 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2941 then drop to VR_VARYING. Outside of this range we get undefined
2942 behavior from the shift operation. We cannot even trust
2943 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2944 shifts, and the operation at the tree level may be widened. */
2945 if (range_int_cst_p (&vr1)
2946 && compare_tree_int (vr1.min, 0) >= 0
2947 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2948 {
2949 if (code == RSHIFT_EXPR)
2950 {
2951 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2952 return;
2953 }
2954 /* We can map lshifts by constants to MULT_EXPR handling. */
2955 else if (code == LSHIFT_EXPR
2956 && range_int_cst_singleton_p (&vr1))
2957 {
2958 bool saved_flag_wrapv;
2959 value_range_t vr1p = VR_INITIALIZER;
2960 vr1p.type = VR_RANGE;
2961 vr1p.min = (wide_int_to_tree
2962 (expr_type,
2963 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2964 TYPE_PRECISION (expr_type))));
2965 vr1p.max = vr1p.min;
2966 /* We have to use a wrapping multiply though as signed overflow
2967 on lshifts is implementation defined in C89. */
2968 saved_flag_wrapv = flag_wrapv;
2969 flag_wrapv = 1;
2970 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2971 &vr0, &vr1p);
2972 flag_wrapv = saved_flag_wrapv;
2973 return;
2974 }
2975 else if (code == LSHIFT_EXPR
2976 && range_int_cst_p (&vr0))
2977 {
2978 int prec = TYPE_PRECISION (expr_type);
2979 int overflow_pos = prec;
2980 int bound_shift;
2981 wide_int low_bound, high_bound;
2982 bool uns = TYPE_UNSIGNED (expr_type);
2983 bool in_bounds = false;
2984
2985 if (!uns)
2986 overflow_pos -= 1;
2987
2988 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2989 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2990 overflow. However, for that to happen, vr1.max needs to be
2991 zero, which means vr1 is a singleton range of zero, which
2992 means it should be handled by the previous LSHIFT_EXPR
2993 if-clause. */
2994 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2995 wide_int complement = ~(bound - 1);
2996
2997 if (uns)
2998 {
2999 low_bound = bound;
3000 high_bound = complement;
3001 if (wi::ltu_p (vr0.max, low_bound))
3002 {
3003 /* [5, 6] << [1, 2] == [10, 24]. */
3004 /* We're shifting out only zeroes, the value increases
3005 monotonically. */
3006 in_bounds = true;
3007 }
3008 else if (wi::ltu_p (high_bound, vr0.min))
3009 {
3010 /* [0xffffff00, 0xffffffff] << [1, 2]
3011 == [0xfffffc00, 0xfffffffe]. */
3012 /* We're shifting out only ones, the value decreases
3013 monotonically. */
3014 in_bounds = true;
3015 }
3016 }
3017 else
3018 {
3019 /* [-1, 1] << [1, 2] == [-4, 4]. */
3020 low_bound = complement;
3021 high_bound = bound;
3022 if (wi::lts_p (vr0.max, high_bound)
3023 && wi::lts_p (low_bound, vr0.min))
3024 {
3025 /* For non-negative numbers, we're shifting out only
3026 zeroes, the value increases monotonically.
3027 For negative numbers, we're shifting out only ones, the
3028 value decreases monotomically. */
3029 in_bounds = true;
3030 }
3031 }
3032
3033 if (in_bounds)
3034 {
3035 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3036 return;
3037 }
3038 }
3039 }
3040 set_value_range_to_varying (vr);
3041 return;
3042 }
3043 else if (code == TRUNC_DIV_EXPR
3044 || code == FLOOR_DIV_EXPR
3045 || code == CEIL_DIV_EXPR
3046 || code == EXACT_DIV_EXPR
3047 || code == ROUND_DIV_EXPR)
3048 {
3049 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3050 {
3051 /* For division, if op1 has VR_RANGE but op0 does not, something
3052 can be deduced just from that range. Say [min, max] / [4, max]
3053 gives [min / 4, max / 4] range. */
3054 if (vr1.type == VR_RANGE
3055 && !symbolic_range_p (&vr1)
3056 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3057 {
3058 vr0.type = type = VR_RANGE;
3059 vr0.min = vrp_val_min (expr_type);
3060 vr0.max = vrp_val_max (expr_type);
3061 }
3062 else
3063 {
3064 set_value_range_to_varying (vr);
3065 return;
3066 }
3067 }
3068
3069 /* For divisions, if flag_non_call_exceptions is true, we must
3070 not eliminate a division by zero. */
3071 if (cfun->can_throw_non_call_exceptions
3072 && (vr1.type != VR_RANGE
3073 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3074 {
3075 set_value_range_to_varying (vr);
3076 return;
3077 }
3078
3079 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3080 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3081 include 0. */
3082 if (vr0.type == VR_RANGE
3083 && (vr1.type != VR_RANGE
3084 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3085 {
3086 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3087 int cmp;
3088
3089 min = NULL_TREE;
3090 max = NULL_TREE;
3091 if (TYPE_UNSIGNED (expr_type)
3092 || value_range_nonnegative_p (&vr1))
3093 {
3094 /* For unsigned division or when divisor is known
3095 to be non-negative, the range has to cover
3096 all numbers from 0 to max for positive max
3097 and all numbers from min to 0 for negative min. */
3098 cmp = compare_values (vr0.max, zero);
3099 if (cmp == -1)
3100 max = zero;
3101 else if (cmp == 0 || cmp == 1)
3102 max = vr0.max;
3103 else
3104 type = VR_VARYING;
3105 cmp = compare_values (vr0.min, zero);
3106 if (cmp == 1)
3107 min = zero;
3108 else if (cmp == 0 || cmp == -1)
3109 min = vr0.min;
3110 else
3111 type = VR_VARYING;
3112 }
3113 else
3114 {
3115 /* Otherwise the range is -max .. max or min .. -min
3116 depending on which bound is bigger in absolute value,
3117 as the division can change the sign. */
3118 abs_extent_range (vr, vr0.min, vr0.max);
3119 return;
3120 }
3121 if (type == VR_VARYING)
3122 {
3123 set_value_range_to_varying (vr);
3124 return;
3125 }
3126 }
3127 else
3128 {
3129 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3130 return;
3131 }
3132 }
3133 else if (code == TRUNC_MOD_EXPR)
3134 {
3135 if (vr1.type != VR_RANGE
3136 || range_includes_zero_p (vr1.min, vr1.max) != 0
3137 || vrp_val_is_min (vr1.min))
3138 {
3139 set_value_range_to_varying (vr);
3140 return;
3141 }
3142 type = VR_RANGE;
3143 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3144 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3145 if (tree_int_cst_lt (max, vr1.max))
3146 max = vr1.max;
3147 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3148 /* If the dividend is non-negative the modulus will be
3149 non-negative as well. */
3150 if (TYPE_UNSIGNED (expr_type)
3151 || value_range_nonnegative_p (&vr0))
3152 min = build_int_cst (TREE_TYPE (max), 0);
3153 else
3154 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3155 }
3156 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3157 {
3158 bool int_cst_range0, int_cst_range1;
3159 wide_int may_be_nonzero0, may_be_nonzero1;
3160 wide_int must_be_nonzero0, must_be_nonzero1;
3161
3162 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3163 &may_be_nonzero0,
3164 &must_be_nonzero0);
3165 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3166 &may_be_nonzero1,
3167 &must_be_nonzero1);
3168
3169 type = VR_RANGE;
3170 if (code == BIT_AND_EXPR)
3171 {
3172 min = wide_int_to_tree (expr_type,
3173 must_be_nonzero0 & must_be_nonzero1);
3174 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3175 /* If both input ranges contain only negative values we can
3176 truncate the result range maximum to the minimum of the
3177 input range maxima. */
3178 if (int_cst_range0 && int_cst_range1
3179 && tree_int_cst_sgn (vr0.max) < 0
3180 && tree_int_cst_sgn (vr1.max) < 0)
3181 {
3182 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3183 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3184 }
3185 /* If either input range contains only non-negative values
3186 we can truncate the result range maximum to the respective
3187 maximum of the input range. */
3188 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3189 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3190 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3191 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3192 max = wide_int_to_tree (expr_type, wmax);
3193 }
3194 else if (code == BIT_IOR_EXPR)
3195 {
3196 max = wide_int_to_tree (expr_type,
3197 may_be_nonzero0 | may_be_nonzero1);
3198 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3199 /* If the input ranges contain only positive values we can
3200 truncate the minimum of the result range to the maximum
3201 of the input range minima. */
3202 if (int_cst_range0 && int_cst_range1
3203 && tree_int_cst_sgn (vr0.min) >= 0
3204 && tree_int_cst_sgn (vr1.min) >= 0)
3205 {
3206 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3207 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3208 }
3209 /* If either input range contains only negative values
3210 we can truncate the minimum of the result range to the
3211 respective minimum range. */
3212 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3213 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3214 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3215 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3216 min = wide_int_to_tree (expr_type, wmin);
3217 }
3218 else if (code == BIT_XOR_EXPR)
3219 {
3220 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3221 | ~(may_be_nonzero0 | may_be_nonzero1));
3222 wide_int result_one_bits
3223 = (must_be_nonzero0.and_not (may_be_nonzero1)
3224 | must_be_nonzero1.and_not (may_be_nonzero0));
3225 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3226 min = wide_int_to_tree (expr_type, result_one_bits);
3227 /* If the range has all positive or all negative values the
3228 result is better than VARYING. */
3229 if (tree_int_cst_sgn (min) < 0
3230 || tree_int_cst_sgn (max) >= 0)
3231 ;
3232 else
3233 max = min = NULL_TREE;
3234 }
3235 }
3236 else
3237 gcc_unreachable ();
3238
3239 /* If either MIN or MAX overflowed, then set the resulting range to
3240 VARYING. But we do accept an overflow infinity representation. */
3241 if (min == NULL_TREE
3242 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3243 || max == NULL_TREE
3244 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3245 {
3246 set_value_range_to_varying (vr);
3247 return;
3248 }
3249
3250 /* We punt if:
3251 1) [-INF, +INF]
3252 2) [-INF, +-INF(OVF)]
3253 3) [+-INF(OVF), +INF]
3254 4) [+-INF(OVF), +-INF(OVF)]
3255 We learn nothing when we have INF and INF(OVF) on both sides.
3256 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3257 overflow. */
3258 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3259 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3260 {
3261 set_value_range_to_varying (vr);
3262 return;
3263 }
3264
3265 cmp = compare_values (min, max);
3266 if (cmp == -2 || cmp == 1)
3267 {
3268 /* If the new range has its limits swapped around (MIN > MAX),
3269 then the operation caused one of them to wrap around, mark
3270 the new range VARYING. */
3271 set_value_range_to_varying (vr);
3272 }
3273 else
3274 set_value_range (vr, type, min, max, NULL);
3275 }
3276
3277 /* Extract range information from a binary expression OP0 CODE OP1 based on
3278 the ranges of each of its operands with resulting type EXPR_TYPE.
3279 The resulting range is stored in *VR. */
3280
3281 static void
3282 extract_range_from_binary_expr (value_range_t *vr,
3283 enum tree_code code,
3284 tree expr_type, tree op0, tree op1)
3285 {
3286 value_range_t vr0 = VR_INITIALIZER;
3287 value_range_t vr1 = VR_INITIALIZER;
3288
3289 /* Get value ranges for each operand. For constant operands, create
3290 a new value range with the operand to simplify processing. */
3291 if (TREE_CODE (op0) == SSA_NAME)
3292 vr0 = *(get_value_range (op0));
3293 else if (is_gimple_min_invariant (op0))
3294 set_value_range_to_value (&vr0, op0, NULL);
3295 else
3296 set_value_range_to_varying (&vr0);
3297
3298 if (TREE_CODE (op1) == SSA_NAME)
3299 vr1 = *(get_value_range (op1));
3300 else if (is_gimple_min_invariant (op1))
3301 set_value_range_to_value (&vr1, op1, NULL);
3302 else
3303 set_value_range_to_varying (&vr1);
3304
3305 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3306
3307 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3308 and based on the other operand, for example if it was deduced from a
3309 symbolic comparison. When a bound of the range of the first operand
3310 is invariant, we set the corresponding bound of the new range to INF
3311 in order to avoid recursing on the range of the second operand. */
3312 if (vr->type == VR_VARYING
3313 && (code == PLUS_EXPR || code == MINUS_EXPR)
3314 && TREE_CODE (op1) == SSA_NAME
3315 && vr0.type == VR_RANGE
3316 && symbolic_range_based_on_p (&vr0, op1))
3317 {
3318 const bool minus_p = (code == MINUS_EXPR);
3319 value_range_t n_vr1 = VR_INITIALIZER;
3320
3321 /* Try with VR0 and [-INF, OP1]. */
3322 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3323 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3324
3325 /* Try with VR0 and [OP1, +INF]. */
3326 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3327 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3328
3329 /* Try with VR0 and [OP1, OP1]. */
3330 else
3331 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3332
3333 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3334 }
3335
3336 if (vr->type == VR_VARYING
3337 && (code == PLUS_EXPR || code == MINUS_EXPR)
3338 && TREE_CODE (op0) == SSA_NAME
3339 && vr1.type == VR_RANGE
3340 && symbolic_range_based_on_p (&vr1, op0))
3341 {
3342 const bool minus_p = (code == MINUS_EXPR);
3343 value_range_t n_vr0 = VR_INITIALIZER;
3344
3345 /* Try with [-INF, OP0] and VR1. */
3346 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3347 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3348
3349 /* Try with [OP0, +INF] and VR1. */
3350 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3351 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3352
3353 /* Try with [OP0, OP0] and VR1. */
3354 else
3355 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3356
3357 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3358 }
3359 }
3360
3361 /* Extract range information from a unary operation CODE based on
3362 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3363 The The resulting range is stored in *VR. */
3364
3365 static void
3366 extract_range_from_unary_expr_1 (value_range_t *vr,
3367 enum tree_code code, tree type,
3368 value_range_t *vr0_, tree op0_type)
3369 {
3370 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3371
3372 /* VRP only operates on integral and pointer types. */
3373 if (!(INTEGRAL_TYPE_P (op0_type)
3374 || POINTER_TYPE_P (op0_type))
3375 || !(INTEGRAL_TYPE_P (type)
3376 || POINTER_TYPE_P (type)))
3377 {
3378 set_value_range_to_varying (vr);
3379 return;
3380 }
3381
3382 /* If VR0 is UNDEFINED, so is the result. */
3383 if (vr0.type == VR_UNDEFINED)
3384 {
3385 set_value_range_to_undefined (vr);
3386 return;
3387 }
3388
3389 /* Handle operations that we express in terms of others. */
3390 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3391 {
3392 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3393 copy_value_range (vr, &vr0);
3394 return;
3395 }
3396 else if (code == NEGATE_EXPR)
3397 {
3398 /* -X is simply 0 - X, so re-use existing code that also handles
3399 anti-ranges fine. */
3400 value_range_t zero = VR_INITIALIZER;
3401 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3402 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3403 return;
3404 }
3405 else if (code == BIT_NOT_EXPR)
3406 {
3407 /* ~X is simply -1 - X, so re-use existing code that also handles
3408 anti-ranges fine. */
3409 value_range_t minusone = VR_INITIALIZER;
3410 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3411 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3412 type, &minusone, &vr0);
3413 return;
3414 }
3415
3416 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3417 and express op ~[] as (op []') U (op []''). */
3418 if (vr0.type == VR_ANTI_RANGE
3419 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3420 {
3421 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3422 if (vrtem1.type != VR_UNDEFINED)
3423 {
3424 value_range_t vrres = VR_INITIALIZER;
3425 extract_range_from_unary_expr_1 (&vrres, code, type,
3426 &vrtem1, op0_type);
3427 vrp_meet (vr, &vrres);
3428 }
3429 return;
3430 }
3431
3432 if (CONVERT_EXPR_CODE_P (code))
3433 {
3434 tree inner_type = op0_type;
3435 tree outer_type = type;
3436
3437 /* If the expression evaluates to a pointer, we are only interested in
3438 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3439 if (POINTER_TYPE_P (type))
3440 {
3441 if (range_is_nonnull (&vr0))
3442 set_value_range_to_nonnull (vr, type);
3443 else if (range_is_null (&vr0))
3444 set_value_range_to_null (vr, type);
3445 else
3446 set_value_range_to_varying (vr);
3447 return;
3448 }
3449
3450 /* If VR0 is varying and we increase the type precision, assume
3451 a full range for the following transformation. */
3452 if (vr0.type == VR_VARYING
3453 && INTEGRAL_TYPE_P (inner_type)
3454 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3455 {
3456 vr0.type = VR_RANGE;
3457 vr0.min = TYPE_MIN_VALUE (inner_type);
3458 vr0.max = TYPE_MAX_VALUE (inner_type);
3459 }
3460
3461 /* If VR0 is a constant range or anti-range and the conversion is
3462 not truncating we can convert the min and max values and
3463 canonicalize the resulting range. Otherwise we can do the
3464 conversion if the size of the range is less than what the
3465 precision of the target type can represent and the range is
3466 not an anti-range. */
3467 if ((vr0.type == VR_RANGE
3468 || vr0.type == VR_ANTI_RANGE)
3469 && TREE_CODE (vr0.min) == INTEGER_CST
3470 && TREE_CODE (vr0.max) == INTEGER_CST
3471 && (!is_overflow_infinity (vr0.min)
3472 || (vr0.type == VR_RANGE
3473 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3474 && needs_overflow_infinity (outer_type)
3475 && supports_overflow_infinity (outer_type)))
3476 && (!is_overflow_infinity (vr0.max)
3477 || (vr0.type == VR_RANGE
3478 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3479 && needs_overflow_infinity (outer_type)
3480 && supports_overflow_infinity (outer_type)))
3481 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3482 || (vr0.type == VR_RANGE
3483 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3484 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3485 size_int (TYPE_PRECISION (outer_type)))))))
3486 {
3487 tree new_min, new_max;
3488 if (is_overflow_infinity (vr0.min))
3489 new_min = negative_overflow_infinity (outer_type);
3490 else
3491 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3492 0, false);
3493 if (is_overflow_infinity (vr0.max))
3494 new_max = positive_overflow_infinity (outer_type);
3495 else
3496 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3497 0, false);
3498 set_and_canonicalize_value_range (vr, vr0.type,
3499 new_min, new_max, NULL);
3500 return;
3501 }
3502
3503 set_value_range_to_varying (vr);
3504 return;
3505 }
3506 else if (code == ABS_EXPR)
3507 {
3508 tree min, max;
3509 int cmp;
3510
3511 /* Pass through vr0 in the easy cases. */
3512 if (TYPE_UNSIGNED (type)
3513 || value_range_nonnegative_p (&vr0))
3514 {
3515 copy_value_range (vr, &vr0);
3516 return;
3517 }
3518
3519 /* For the remaining varying or symbolic ranges we can't do anything
3520 useful. */
3521 if (vr0.type == VR_VARYING
3522 || symbolic_range_p (&vr0))
3523 {
3524 set_value_range_to_varying (vr);
3525 return;
3526 }
3527
3528 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3529 useful range. */
3530 if (!TYPE_OVERFLOW_UNDEFINED (type)
3531 && ((vr0.type == VR_RANGE
3532 && vrp_val_is_min (vr0.min))
3533 || (vr0.type == VR_ANTI_RANGE
3534 && !vrp_val_is_min (vr0.min))))
3535 {
3536 set_value_range_to_varying (vr);
3537 return;
3538 }
3539
3540 /* ABS_EXPR may flip the range around, if the original range
3541 included negative values. */
3542 if (is_overflow_infinity (vr0.min))
3543 min = positive_overflow_infinity (type);
3544 else if (!vrp_val_is_min (vr0.min))
3545 min = fold_unary_to_constant (code, type, vr0.min);
3546 else if (!needs_overflow_infinity (type))
3547 min = TYPE_MAX_VALUE (type);
3548 else if (supports_overflow_infinity (type))
3549 min = positive_overflow_infinity (type);
3550 else
3551 {
3552 set_value_range_to_varying (vr);
3553 return;
3554 }
3555
3556 if (is_overflow_infinity (vr0.max))
3557 max = positive_overflow_infinity (type);
3558 else if (!vrp_val_is_min (vr0.max))
3559 max = fold_unary_to_constant (code, type, vr0.max);
3560 else if (!needs_overflow_infinity (type))
3561 max = TYPE_MAX_VALUE (type);
3562 else if (supports_overflow_infinity (type)
3563 /* We shouldn't generate [+INF, +INF] as set_value_range
3564 doesn't like this and ICEs. */
3565 && !is_positive_overflow_infinity (min))
3566 max = positive_overflow_infinity (type);
3567 else
3568 {
3569 set_value_range_to_varying (vr);
3570 return;
3571 }
3572
3573 cmp = compare_values (min, max);
3574
3575 /* If a VR_ANTI_RANGEs contains zero, then we have
3576 ~[-INF, min(MIN, MAX)]. */
3577 if (vr0.type == VR_ANTI_RANGE)
3578 {
3579 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3580 {
3581 /* Take the lower of the two values. */
3582 if (cmp != 1)
3583 max = min;
3584
3585 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3586 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3587 flag_wrapv is set and the original anti-range doesn't include
3588 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3589 if (TYPE_OVERFLOW_WRAPS (type))
3590 {
3591 tree type_min_value = TYPE_MIN_VALUE (type);
3592
3593 min = (vr0.min != type_min_value
3594 ? int_const_binop (PLUS_EXPR, type_min_value,
3595 build_int_cst (TREE_TYPE (type_min_value), 1))
3596 : type_min_value);
3597 }
3598 else
3599 {
3600 if (overflow_infinity_range_p (&vr0))
3601 min = negative_overflow_infinity (type);
3602 else
3603 min = TYPE_MIN_VALUE (type);
3604 }
3605 }
3606 else
3607 {
3608 /* All else has failed, so create the range [0, INF], even for
3609 flag_wrapv since TYPE_MIN_VALUE is in the original
3610 anti-range. */
3611 vr0.type = VR_RANGE;
3612 min = build_int_cst (type, 0);
3613 if (needs_overflow_infinity (type))
3614 {
3615 if (supports_overflow_infinity (type))
3616 max = positive_overflow_infinity (type);
3617 else
3618 {
3619 set_value_range_to_varying (vr);
3620 return;
3621 }
3622 }
3623 else
3624 max = TYPE_MAX_VALUE (type);
3625 }
3626 }
3627
3628 /* If the range contains zero then we know that the minimum value in the
3629 range will be zero. */
3630 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3631 {
3632 if (cmp == 1)
3633 max = min;
3634 min = build_int_cst (type, 0);
3635 }
3636 else
3637 {
3638 /* If the range was reversed, swap MIN and MAX. */
3639 if (cmp == 1)
3640 {
3641 tree t = min;
3642 min = max;
3643 max = t;
3644 }
3645 }
3646
3647 cmp = compare_values (min, max);
3648 if (cmp == -2 || cmp == 1)
3649 {
3650 /* If the new range has its limits swapped around (MIN > MAX),
3651 then the operation caused one of them to wrap around, mark
3652 the new range VARYING. */
3653 set_value_range_to_varying (vr);
3654 }
3655 else
3656 set_value_range (vr, vr0.type, min, max, NULL);
3657 return;
3658 }
3659
3660 /* For unhandled operations fall back to varying. */
3661 set_value_range_to_varying (vr);
3662 return;
3663 }
3664
3665
3666 /* Extract range information from a unary expression CODE OP0 based on
3667 the range of its operand with resulting type TYPE.
3668 The resulting range is stored in *VR. */
3669
3670 static void
3671 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3672 tree type, tree op0)
3673 {
3674 value_range_t vr0 = VR_INITIALIZER;
3675
3676 /* Get value ranges for the operand. For constant operands, create
3677 a new value range with the operand to simplify processing. */
3678 if (TREE_CODE (op0) == SSA_NAME)
3679 vr0 = *(get_value_range (op0));
3680 else if (is_gimple_min_invariant (op0))
3681 set_value_range_to_value (&vr0, op0, NULL);
3682 else
3683 set_value_range_to_varying (&vr0);
3684
3685 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3686 }
3687
3688
3689 /* Extract range information from a conditional expression STMT based on
3690 the ranges of each of its operands and the expression code. */
3691
3692 static void
3693 extract_range_from_cond_expr (value_range_t *vr, gimple stmt)
3694 {
3695 tree op0, op1;
3696 value_range_t vr0 = VR_INITIALIZER;
3697 value_range_t vr1 = VR_INITIALIZER;
3698
3699 /* Get value ranges for each operand. For constant operands, create
3700 a new value range with the operand to simplify processing. */
3701 op0 = gimple_assign_rhs2 (stmt);
3702 if (TREE_CODE (op0) == SSA_NAME)
3703 vr0 = *(get_value_range (op0));
3704 else if (is_gimple_min_invariant (op0))
3705 set_value_range_to_value (&vr0, op0, NULL);
3706 else
3707 set_value_range_to_varying (&vr0);
3708
3709 op1 = gimple_assign_rhs3 (stmt);
3710 if (TREE_CODE (op1) == SSA_NAME)
3711 vr1 = *(get_value_range (op1));
3712 else if (is_gimple_min_invariant (op1))
3713 set_value_range_to_value (&vr1, op1, NULL);
3714 else
3715 set_value_range_to_varying (&vr1);
3716
3717 /* The resulting value range is the union of the operand ranges */
3718 copy_value_range (vr, &vr0);
3719 vrp_meet (vr, &vr1);
3720 }
3721
3722
3723 /* Extract range information from a comparison expression EXPR based
3724 on the range of its operand and the expression code. */
3725
3726 static void
3727 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3728 tree type, tree op0, tree op1)
3729 {
3730 bool sop = false;
3731 tree val;
3732
3733 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3734 NULL);
3735
3736 /* A disadvantage of using a special infinity as an overflow
3737 representation is that we lose the ability to record overflow
3738 when we don't have an infinity. So we have to ignore a result
3739 which relies on overflow. */
3740
3741 if (val && !is_overflow_infinity (val) && !sop)
3742 {
3743 /* Since this expression was found on the RHS of an assignment,
3744 its type may be different from _Bool. Convert VAL to EXPR's
3745 type. */
3746 val = fold_convert (type, val);
3747 if (is_gimple_min_invariant (val))
3748 set_value_range_to_value (vr, val, vr->equiv);
3749 else
3750 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3751 }
3752 else
3753 /* The result of a comparison is always true or false. */
3754 set_value_range_to_truthvalue (vr, type);
3755 }
3756
3757 /* Try to derive a nonnegative or nonzero range out of STMT relying
3758 primarily on generic routines in fold in conjunction with range data.
3759 Store the result in *VR */
3760
3761 static void
3762 extract_range_basic (value_range_t *vr, gimple stmt)
3763 {
3764 bool sop = false;
3765 tree type = gimple_expr_type (stmt);
3766
3767 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3768 {
3769 tree fndecl = gimple_call_fndecl (stmt), arg;
3770 int mini, maxi, zerov = 0, prec;
3771
3772 switch (DECL_FUNCTION_CODE (fndecl))
3773 {
3774 case BUILT_IN_CONSTANT_P:
3775 /* If the call is __builtin_constant_p and the argument is a
3776 function parameter resolve it to false. This avoids bogus
3777 array bound warnings.
3778 ??? We could do this as early as inlining is finished. */
3779 arg = gimple_call_arg (stmt, 0);
3780 if (TREE_CODE (arg) == SSA_NAME
3781 && SSA_NAME_IS_DEFAULT_DEF (arg)
3782 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3783 {
3784 set_value_range_to_null (vr, type);
3785 return;
3786 }
3787 break;
3788 /* Both __builtin_ffs* and __builtin_popcount return
3789 [0, prec]. */
3790 CASE_INT_FN (BUILT_IN_FFS):
3791 CASE_INT_FN (BUILT_IN_POPCOUNT):
3792 arg = gimple_call_arg (stmt, 0);
3793 prec = TYPE_PRECISION (TREE_TYPE (arg));
3794 mini = 0;
3795 maxi = prec;
3796 if (TREE_CODE (arg) == SSA_NAME)
3797 {
3798 value_range_t *vr0 = get_value_range (arg);
3799 /* If arg is non-zero, then ffs or popcount
3800 are non-zero. */
3801 if (((vr0->type == VR_RANGE
3802 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3803 || (vr0->type == VR_ANTI_RANGE
3804 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3805 && !is_overflow_infinity (vr0->min)
3806 && !is_overflow_infinity (vr0->max))
3807 mini = 1;
3808 /* If some high bits are known to be zero,
3809 we can decrease the maximum. */
3810 if (vr0->type == VR_RANGE
3811 && TREE_CODE (vr0->max) == INTEGER_CST
3812 && !operand_less_p (vr0->min,
3813 build_zero_cst (TREE_TYPE (vr0->min)))
3814 && !is_overflow_infinity (vr0->max))
3815 maxi = tree_floor_log2 (vr0->max) + 1;
3816 }
3817 goto bitop_builtin;
3818 /* __builtin_parity* returns [0, 1]. */
3819 CASE_INT_FN (BUILT_IN_PARITY):
3820 mini = 0;
3821 maxi = 1;
3822 goto bitop_builtin;
3823 /* __builtin_c[lt]z* return [0, prec-1], except for
3824 when the argument is 0, but that is undefined behavior.
3825 On many targets where the CLZ RTL or optab value is defined
3826 for 0 the value is prec, so include that in the range
3827 by default. */
3828 CASE_INT_FN (BUILT_IN_CLZ):
3829 arg = gimple_call_arg (stmt, 0);
3830 prec = TYPE_PRECISION (TREE_TYPE (arg));
3831 mini = 0;
3832 maxi = prec;
3833 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3834 != CODE_FOR_nothing
3835 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3836 zerov)
3837 /* Handle only the single common value. */
3838 && zerov != prec)
3839 /* Magic value to give up, unless vr0 proves
3840 arg is non-zero. */
3841 mini = -2;
3842 if (TREE_CODE (arg) == SSA_NAME)
3843 {
3844 value_range_t *vr0 = get_value_range (arg);
3845 /* From clz of VR_RANGE minimum we can compute
3846 result maximum. */
3847 if (vr0->type == VR_RANGE
3848 && TREE_CODE (vr0->min) == INTEGER_CST
3849 && !is_overflow_infinity (vr0->min))
3850 {
3851 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3852 if (maxi != prec)
3853 mini = 0;
3854 }
3855 else if (vr0->type == VR_ANTI_RANGE
3856 && integer_zerop (vr0->min)
3857 && !is_overflow_infinity (vr0->min))
3858 {
3859 maxi = prec - 1;
3860 mini = 0;
3861 }
3862 if (mini == -2)
3863 break;
3864 /* From clz of VR_RANGE maximum we can compute
3865 result minimum. */
3866 if (vr0->type == VR_RANGE
3867 && TREE_CODE (vr0->max) == INTEGER_CST
3868 && !is_overflow_infinity (vr0->max))
3869 {
3870 mini = prec - 1 - tree_floor_log2 (vr0->max);
3871 if (mini == prec)
3872 break;
3873 }
3874 }
3875 if (mini == -2)
3876 break;
3877 goto bitop_builtin;
3878 /* __builtin_ctz* return [0, prec-1], except for
3879 when the argument is 0, but that is undefined behavior.
3880 If there is a ctz optab for this mode and
3881 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3882 otherwise just assume 0 won't be seen. */
3883 CASE_INT_FN (BUILT_IN_CTZ):
3884 arg = gimple_call_arg (stmt, 0);
3885 prec = TYPE_PRECISION (TREE_TYPE (arg));
3886 mini = 0;
3887 maxi = prec - 1;
3888 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3889 != CODE_FOR_nothing
3890 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3891 zerov))
3892 {
3893 /* Handle only the two common values. */
3894 if (zerov == -1)
3895 mini = -1;
3896 else if (zerov == prec)
3897 maxi = prec;
3898 else
3899 /* Magic value to give up, unless vr0 proves
3900 arg is non-zero. */
3901 mini = -2;
3902 }
3903 if (TREE_CODE (arg) == SSA_NAME)
3904 {
3905 value_range_t *vr0 = get_value_range (arg);
3906 /* If arg is non-zero, then use [0, prec - 1]. */
3907 if (((vr0->type == VR_RANGE
3908 && integer_nonzerop (vr0->min))
3909 || (vr0->type == VR_ANTI_RANGE
3910 && integer_zerop (vr0->min)))
3911 && !is_overflow_infinity (vr0->min))
3912 {
3913 mini = 0;
3914 maxi = prec - 1;
3915 }
3916 /* If some high bits are known to be zero,
3917 we can decrease the result maximum. */
3918 if (vr0->type == VR_RANGE
3919 && TREE_CODE (vr0->max) == INTEGER_CST
3920 && !is_overflow_infinity (vr0->max))
3921 {
3922 maxi = tree_floor_log2 (vr0->max);
3923 /* For vr0 [0, 0] give up. */
3924 if (maxi == -1)
3925 break;
3926 }
3927 }
3928 if (mini == -2)
3929 break;
3930 goto bitop_builtin;
3931 /* __builtin_clrsb* returns [0, prec-1]. */
3932 CASE_INT_FN (BUILT_IN_CLRSB):
3933 arg = gimple_call_arg (stmt, 0);
3934 prec = TYPE_PRECISION (TREE_TYPE (arg));
3935 mini = 0;
3936 maxi = prec - 1;
3937 goto bitop_builtin;
3938 bitop_builtin:
3939 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3940 build_int_cst (type, maxi), NULL);
3941 return;
3942 default:
3943 break;
3944 }
3945 }
3946 else if (is_gimple_call (stmt)
3947 && gimple_call_internal_p (stmt))
3948 {
3949 enum tree_code subcode = ERROR_MARK;
3950 switch (gimple_call_internal_fn (stmt))
3951 {
3952 case IFN_UBSAN_CHECK_ADD:
3953 subcode = PLUS_EXPR;
3954 break;
3955 case IFN_UBSAN_CHECK_SUB:
3956 subcode = MINUS_EXPR;
3957 break;
3958 case IFN_UBSAN_CHECK_MUL:
3959 subcode = MULT_EXPR;
3960 break;
3961 default:
3962 break;
3963 }
3964 if (subcode != ERROR_MARK)
3965 {
3966 bool saved_flag_wrapv = flag_wrapv;
3967 /* Pretend the arithmetics is wrapping. If there is
3968 any overflow, we'll complain, but will actually do
3969 wrapping operation. */
3970 flag_wrapv = 1;
3971 extract_range_from_binary_expr (vr, subcode, type,
3972 gimple_call_arg (stmt, 0),
3973 gimple_call_arg (stmt, 1));
3974 flag_wrapv = saved_flag_wrapv;
3975
3976 /* If for both arguments vrp_valueize returned non-NULL,
3977 this should have been already folded and if not, it
3978 wasn't folded because of overflow. Avoid removing the
3979 UBSAN_CHECK_* calls in that case. */
3980 if (vr->type == VR_RANGE
3981 && (vr->min == vr->max
3982 || operand_equal_p (vr->min, vr->max, 0)))
3983 set_value_range_to_varying (vr);
3984 return;
3985 }
3986 }
3987 if (INTEGRAL_TYPE_P (type)
3988 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3989 set_value_range_to_nonnegative (vr, type,
3990 sop || stmt_overflow_infinity (stmt));
3991 else if (vrp_stmt_computes_nonzero (stmt, &sop)
3992 && !sop)
3993 set_value_range_to_nonnull (vr, type);
3994 else
3995 set_value_range_to_varying (vr);
3996 }
3997
3998
3999 /* Try to compute a useful range out of assignment STMT and store it
4000 in *VR. */
4001
4002 static void
4003 extract_range_from_assignment (value_range_t *vr, gimple stmt)
4004 {
4005 enum tree_code code = gimple_assign_rhs_code (stmt);
4006
4007 if (code == ASSERT_EXPR)
4008 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4009 else if (code == SSA_NAME)
4010 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4011 else if (TREE_CODE_CLASS (code) == tcc_binary)
4012 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4013 gimple_expr_type (stmt),
4014 gimple_assign_rhs1 (stmt),
4015 gimple_assign_rhs2 (stmt));
4016 else if (TREE_CODE_CLASS (code) == tcc_unary)
4017 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4018 gimple_expr_type (stmt),
4019 gimple_assign_rhs1 (stmt));
4020 else if (code == COND_EXPR)
4021 extract_range_from_cond_expr (vr, stmt);
4022 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4023 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4024 gimple_expr_type (stmt),
4025 gimple_assign_rhs1 (stmt),
4026 gimple_assign_rhs2 (stmt));
4027 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4028 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4029 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4030 else
4031 set_value_range_to_varying (vr);
4032
4033 if (vr->type == VR_VARYING)
4034 extract_range_basic (vr, stmt);
4035 }
4036
4037 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4038 would be profitable to adjust VR using scalar evolution information
4039 for VAR. If so, update VR with the new limits. */
4040
4041 static void
4042 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4043 gimple stmt, tree var)
4044 {
4045 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4046 enum ev_direction dir;
4047
4048 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4049 better opportunities than a regular range, but I'm not sure. */
4050 if (vr->type == VR_ANTI_RANGE)
4051 return;
4052
4053 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4054
4055 /* Like in PR19590, scev can return a constant function. */
4056 if (is_gimple_min_invariant (chrec))
4057 {
4058 set_value_range_to_value (vr, chrec, vr->equiv);
4059 return;
4060 }
4061
4062 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4063 return;
4064
4065 init = initial_condition_in_loop_num (chrec, loop->num);
4066 tem = op_with_constant_singleton_value_range (init);
4067 if (tem)
4068 init = tem;
4069 step = evolution_part_in_loop_num (chrec, loop->num);
4070 tem = op_with_constant_singleton_value_range (step);
4071 if (tem)
4072 step = tem;
4073
4074 /* If STEP is symbolic, we can't know whether INIT will be the
4075 minimum or maximum value in the range. Also, unless INIT is
4076 a simple expression, compare_values and possibly other functions
4077 in tree-vrp won't be able to handle it. */
4078 if (step == NULL_TREE
4079 || !is_gimple_min_invariant (step)
4080 || !valid_value_p (init))
4081 return;
4082
4083 dir = scev_direction (chrec);
4084 if (/* Do not adjust ranges if we do not know whether the iv increases
4085 or decreases, ... */
4086 dir == EV_DIR_UNKNOWN
4087 /* ... or if it may wrap. */
4088 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4089 true))
4090 return;
4091
4092 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4093 negative_overflow_infinity and positive_overflow_infinity,
4094 because we have concluded that the loop probably does not
4095 wrap. */
4096
4097 type = TREE_TYPE (var);
4098 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4099 tmin = lower_bound_in_type (type, type);
4100 else
4101 tmin = TYPE_MIN_VALUE (type);
4102 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4103 tmax = upper_bound_in_type (type, type);
4104 else
4105 tmax = TYPE_MAX_VALUE (type);
4106
4107 /* Try to use estimated number of iterations for the loop to constrain the
4108 final value in the evolution. */
4109 if (TREE_CODE (step) == INTEGER_CST
4110 && is_gimple_val (init)
4111 && (TREE_CODE (init) != SSA_NAME
4112 || get_value_range (init)->type == VR_RANGE))
4113 {
4114 widest_int nit;
4115
4116 /* We are only entering here for loop header PHI nodes, so using
4117 the number of latch executions is the correct thing to use. */
4118 if (max_loop_iterations (loop, &nit))
4119 {
4120 value_range_t maxvr = VR_INITIALIZER;
4121 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4122 bool overflow;
4123
4124 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4125 &overflow);
4126 /* If the multiplication overflowed we can't do a meaningful
4127 adjustment. Likewise if the result doesn't fit in the type
4128 of the induction variable. For a signed type we have to
4129 check whether the result has the expected signedness which
4130 is that of the step as number of iterations is unsigned. */
4131 if (!overflow
4132 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4133 && (sgn == UNSIGNED
4134 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4135 {
4136 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4137 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4138 TREE_TYPE (init), init, tem);
4139 /* Likewise if the addition did. */
4140 if (maxvr.type == VR_RANGE)
4141 {
4142 tmin = maxvr.min;
4143 tmax = maxvr.max;
4144 }
4145 }
4146 }
4147 }
4148
4149 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4150 {
4151 min = tmin;
4152 max = tmax;
4153
4154 /* For VARYING or UNDEFINED ranges, just about anything we get
4155 from scalar evolutions should be better. */
4156
4157 if (dir == EV_DIR_DECREASES)
4158 max = init;
4159 else
4160 min = init;
4161 }
4162 else if (vr->type == VR_RANGE)
4163 {
4164 min = vr->min;
4165 max = vr->max;
4166
4167 if (dir == EV_DIR_DECREASES)
4168 {
4169 /* INIT is the maximum value. If INIT is lower than VR->MAX
4170 but no smaller than VR->MIN, set VR->MAX to INIT. */
4171 if (compare_values (init, max) == -1)
4172 max = init;
4173
4174 /* According to the loop information, the variable does not
4175 overflow. If we think it does, probably because of an
4176 overflow due to arithmetic on a different INF value,
4177 reset now. */
4178 if (is_negative_overflow_infinity (min)
4179 || compare_values (min, tmin) == -1)
4180 min = tmin;
4181
4182 }
4183 else
4184 {
4185 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4186 if (compare_values (init, min) == 1)
4187 min = init;
4188
4189 if (is_positive_overflow_infinity (max)
4190 || compare_values (tmax, max) == -1)
4191 max = tmax;
4192 }
4193 }
4194 else
4195 return;
4196
4197 /* If we just created an invalid range with the minimum
4198 greater than the maximum, we fail conservatively.
4199 This should happen only in unreachable
4200 parts of code, or for invalid programs. */
4201 if (compare_values (min, max) == 1
4202 || (is_negative_overflow_infinity (min)
4203 && is_positive_overflow_infinity (max)))
4204 return;
4205
4206 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4207 }
4208
4209
4210 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4211
4212 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4213 all the values in the ranges.
4214
4215 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4216
4217 - Return NULL_TREE if it is not always possible to determine the
4218 value of the comparison.
4219
4220 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4221 overflow infinity was used in the test. */
4222
4223
4224 static tree
4225 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4226 bool *strict_overflow_p)
4227 {
4228 /* VARYING or UNDEFINED ranges cannot be compared. */
4229 if (vr0->type == VR_VARYING
4230 || vr0->type == VR_UNDEFINED
4231 || vr1->type == VR_VARYING
4232 || vr1->type == VR_UNDEFINED)
4233 return NULL_TREE;
4234
4235 /* Anti-ranges need to be handled separately. */
4236 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4237 {
4238 /* If both are anti-ranges, then we cannot compute any
4239 comparison. */
4240 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4241 return NULL_TREE;
4242
4243 /* These comparisons are never statically computable. */
4244 if (comp == GT_EXPR
4245 || comp == GE_EXPR
4246 || comp == LT_EXPR
4247 || comp == LE_EXPR)
4248 return NULL_TREE;
4249
4250 /* Equality can be computed only between a range and an
4251 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4252 if (vr0->type == VR_RANGE)
4253 {
4254 /* To simplify processing, make VR0 the anti-range. */
4255 value_range_t *tmp = vr0;
4256 vr0 = vr1;
4257 vr1 = tmp;
4258 }
4259
4260 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4261
4262 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4263 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4264 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4265
4266 return NULL_TREE;
4267 }
4268
4269 if (!usable_range_p (vr0, strict_overflow_p)
4270 || !usable_range_p (vr1, strict_overflow_p))
4271 return NULL_TREE;
4272
4273 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4274 operands around and change the comparison code. */
4275 if (comp == GT_EXPR || comp == GE_EXPR)
4276 {
4277 value_range_t *tmp;
4278 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4279 tmp = vr0;
4280 vr0 = vr1;
4281 vr1 = tmp;
4282 }
4283
4284 if (comp == EQ_EXPR)
4285 {
4286 /* Equality may only be computed if both ranges represent
4287 exactly one value. */
4288 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4289 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4290 {
4291 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4292 strict_overflow_p);
4293 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4294 strict_overflow_p);
4295 if (cmp_min == 0 && cmp_max == 0)
4296 return boolean_true_node;
4297 else if (cmp_min != -2 && cmp_max != -2)
4298 return boolean_false_node;
4299 }
4300 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4301 else if (compare_values_warnv (vr0->min, vr1->max,
4302 strict_overflow_p) == 1
4303 || compare_values_warnv (vr1->min, vr0->max,
4304 strict_overflow_p) == 1)
4305 return boolean_false_node;
4306
4307 return NULL_TREE;
4308 }
4309 else if (comp == NE_EXPR)
4310 {
4311 int cmp1, cmp2;
4312
4313 /* If VR0 is completely to the left or completely to the right
4314 of VR1, they are always different. Notice that we need to
4315 make sure that both comparisons yield similar results to
4316 avoid comparing values that cannot be compared at
4317 compile-time. */
4318 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4319 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4320 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4321 return boolean_true_node;
4322
4323 /* If VR0 and VR1 represent a single value and are identical,
4324 return false. */
4325 else if (compare_values_warnv (vr0->min, vr0->max,
4326 strict_overflow_p) == 0
4327 && compare_values_warnv (vr1->min, vr1->max,
4328 strict_overflow_p) == 0
4329 && compare_values_warnv (vr0->min, vr1->min,
4330 strict_overflow_p) == 0
4331 && compare_values_warnv (vr0->max, vr1->max,
4332 strict_overflow_p) == 0)
4333 return boolean_false_node;
4334
4335 /* Otherwise, they may or may not be different. */
4336 else
4337 return NULL_TREE;
4338 }
4339 else if (comp == LT_EXPR || comp == LE_EXPR)
4340 {
4341 int tst;
4342
4343 /* If VR0 is to the left of VR1, return true. */
4344 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4345 if ((comp == LT_EXPR && tst == -1)
4346 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4347 {
4348 if (overflow_infinity_range_p (vr0)
4349 || overflow_infinity_range_p (vr1))
4350 *strict_overflow_p = true;
4351 return boolean_true_node;
4352 }
4353
4354 /* If VR0 is to the right of VR1, return false. */
4355 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4356 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4357 || (comp == LE_EXPR && tst == 1))
4358 {
4359 if (overflow_infinity_range_p (vr0)
4360 || overflow_infinity_range_p (vr1))
4361 *strict_overflow_p = true;
4362 return boolean_false_node;
4363 }
4364
4365 /* Otherwise, we don't know. */
4366 return NULL_TREE;
4367 }
4368
4369 gcc_unreachable ();
4370 }
4371
4372
4373 /* Given a value range VR, a value VAL and a comparison code COMP, return
4374 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4375 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4376 always returns false. Return NULL_TREE if it is not always
4377 possible to determine the value of the comparison. Also set
4378 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4379 infinity was used in the test. */
4380
4381 static tree
4382 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4383 bool *strict_overflow_p)
4384 {
4385 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4386 return NULL_TREE;
4387
4388 /* Anti-ranges need to be handled separately. */
4389 if (vr->type == VR_ANTI_RANGE)
4390 {
4391 /* For anti-ranges, the only predicates that we can compute at
4392 compile time are equality and inequality. */
4393 if (comp == GT_EXPR
4394 || comp == GE_EXPR
4395 || comp == LT_EXPR
4396 || comp == LE_EXPR)
4397 return NULL_TREE;
4398
4399 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4400 if (value_inside_range (val, vr->min, vr->max) == 1)
4401 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4402
4403 return NULL_TREE;
4404 }
4405
4406 if (!usable_range_p (vr, strict_overflow_p))
4407 return NULL_TREE;
4408
4409 if (comp == EQ_EXPR)
4410 {
4411 /* EQ_EXPR may only be computed if VR represents exactly
4412 one value. */
4413 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4414 {
4415 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4416 if (cmp == 0)
4417 return boolean_true_node;
4418 else if (cmp == -1 || cmp == 1 || cmp == 2)
4419 return boolean_false_node;
4420 }
4421 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4422 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4423 return boolean_false_node;
4424
4425 return NULL_TREE;
4426 }
4427 else if (comp == NE_EXPR)
4428 {
4429 /* If VAL is not inside VR, then they are always different. */
4430 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4431 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4432 return boolean_true_node;
4433
4434 /* If VR represents exactly one value equal to VAL, then return
4435 false. */
4436 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4437 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4438 return boolean_false_node;
4439
4440 /* Otherwise, they may or may not be different. */
4441 return NULL_TREE;
4442 }
4443 else if (comp == LT_EXPR || comp == LE_EXPR)
4444 {
4445 int tst;
4446
4447 /* If VR is to the left of VAL, return true. */
4448 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4449 if ((comp == LT_EXPR && tst == -1)
4450 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4451 {
4452 if (overflow_infinity_range_p (vr))
4453 *strict_overflow_p = true;
4454 return boolean_true_node;
4455 }
4456
4457 /* If VR is to the right of VAL, return false. */
4458 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4459 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4460 || (comp == LE_EXPR && tst == 1))
4461 {
4462 if (overflow_infinity_range_p (vr))
4463 *strict_overflow_p = true;
4464 return boolean_false_node;
4465 }
4466
4467 /* Otherwise, we don't know. */
4468 return NULL_TREE;
4469 }
4470 else if (comp == GT_EXPR || comp == GE_EXPR)
4471 {
4472 int tst;
4473
4474 /* If VR is to the right of VAL, return true. */
4475 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4476 if ((comp == GT_EXPR && tst == 1)
4477 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4478 {
4479 if (overflow_infinity_range_p (vr))
4480 *strict_overflow_p = true;
4481 return boolean_true_node;
4482 }
4483
4484 /* If VR is to the left of VAL, return false. */
4485 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4486 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4487 || (comp == GE_EXPR && tst == -1))
4488 {
4489 if (overflow_infinity_range_p (vr))
4490 *strict_overflow_p = true;
4491 return boolean_false_node;
4492 }
4493
4494 /* Otherwise, we don't know. */
4495 return NULL_TREE;
4496 }
4497
4498 gcc_unreachable ();
4499 }
4500
4501
4502 /* Debugging dumps. */
4503
4504 void dump_value_range (FILE *, value_range_t *);
4505 void debug_value_range (value_range_t *);
4506 void dump_all_value_ranges (FILE *);
4507 void debug_all_value_ranges (void);
4508 void dump_vr_equiv (FILE *, bitmap);
4509 void debug_vr_equiv (bitmap);
4510
4511
4512 /* Dump value range VR to FILE. */
4513
4514 void
4515 dump_value_range (FILE *file, value_range_t *vr)
4516 {
4517 if (vr == NULL)
4518 fprintf (file, "[]");
4519 else if (vr->type == VR_UNDEFINED)
4520 fprintf (file, "UNDEFINED");
4521 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4522 {
4523 tree type = TREE_TYPE (vr->min);
4524
4525 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4526
4527 if (is_negative_overflow_infinity (vr->min))
4528 fprintf (file, "-INF(OVF)");
4529 else if (INTEGRAL_TYPE_P (type)
4530 && !TYPE_UNSIGNED (type)
4531 && vrp_val_is_min (vr->min))
4532 fprintf (file, "-INF");
4533 else
4534 print_generic_expr (file, vr->min, 0);
4535
4536 fprintf (file, ", ");
4537
4538 if (is_positive_overflow_infinity (vr->max))
4539 fprintf (file, "+INF(OVF)");
4540 else if (INTEGRAL_TYPE_P (type)
4541 && vrp_val_is_max (vr->max))
4542 fprintf (file, "+INF");
4543 else
4544 print_generic_expr (file, vr->max, 0);
4545
4546 fprintf (file, "]");
4547
4548 if (vr->equiv)
4549 {
4550 bitmap_iterator bi;
4551 unsigned i, c = 0;
4552
4553 fprintf (file, " EQUIVALENCES: { ");
4554
4555 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4556 {
4557 print_generic_expr (file, ssa_name (i), 0);
4558 fprintf (file, " ");
4559 c++;
4560 }
4561
4562 fprintf (file, "} (%u elements)", c);
4563 }
4564 }
4565 else if (vr->type == VR_VARYING)
4566 fprintf (file, "VARYING");
4567 else
4568 fprintf (file, "INVALID RANGE");
4569 }
4570
4571
4572 /* Dump value range VR to stderr. */
4573
4574 DEBUG_FUNCTION void
4575 debug_value_range (value_range_t *vr)
4576 {
4577 dump_value_range (stderr, vr);
4578 fprintf (stderr, "\n");
4579 }
4580
4581
4582 /* Dump value ranges of all SSA_NAMEs to FILE. */
4583
4584 void
4585 dump_all_value_ranges (FILE *file)
4586 {
4587 size_t i;
4588
4589 for (i = 0; i < num_vr_values; i++)
4590 {
4591 if (vr_value[i])
4592 {
4593 print_generic_expr (file, ssa_name (i), 0);
4594 fprintf (file, ": ");
4595 dump_value_range (file, vr_value[i]);
4596 fprintf (file, "\n");
4597 }
4598 }
4599
4600 fprintf (file, "\n");
4601 }
4602
4603
4604 /* Dump all value ranges to stderr. */
4605
4606 DEBUG_FUNCTION void
4607 debug_all_value_ranges (void)
4608 {
4609 dump_all_value_ranges (stderr);
4610 }
4611
4612
4613 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4614 create a new SSA name N and return the assertion assignment
4615 'N = ASSERT_EXPR <V, V OP W>'. */
4616
4617 static gimple
4618 build_assert_expr_for (tree cond, tree v)
4619 {
4620 tree a;
4621 gimple assertion;
4622
4623 gcc_assert (TREE_CODE (v) == SSA_NAME
4624 && COMPARISON_CLASS_P (cond));
4625
4626 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4627 assertion = gimple_build_assign (NULL_TREE, a);
4628
4629 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4630 operand of the ASSERT_EXPR. Create it so the new name and the old one
4631 are registered in the replacement table so that we can fix the SSA web
4632 after adding all the ASSERT_EXPRs. */
4633 create_new_def_for (v, assertion, NULL);
4634
4635 return assertion;
4636 }
4637
4638
4639 /* Return false if EXPR is a predicate expression involving floating
4640 point values. */
4641
4642 static inline bool
4643 fp_predicate (gimple stmt)
4644 {
4645 GIMPLE_CHECK (stmt, GIMPLE_COND);
4646
4647 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4648 }
4649
4650 /* If the range of values taken by OP can be inferred after STMT executes,
4651 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4652 describes the inferred range. Return true if a range could be
4653 inferred. */
4654
4655 static bool
4656 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4657 {
4658 *val_p = NULL_TREE;
4659 *comp_code_p = ERROR_MARK;
4660
4661 /* Do not attempt to infer anything in names that flow through
4662 abnormal edges. */
4663 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4664 return false;
4665
4666 /* Similarly, don't infer anything from statements that may throw
4667 exceptions. ??? Relax this requirement? */
4668 if (stmt_could_throw_p (stmt))
4669 return false;
4670
4671 /* If STMT is the last statement of a basic block with no normal
4672 successors, there is no point inferring anything about any of its
4673 operands. We would not be able to find a proper insertion point
4674 for the assertion, anyway. */
4675 if (stmt_ends_bb_p (stmt))
4676 {
4677 edge_iterator ei;
4678 edge e;
4679
4680 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4681 if (!(e->flags & EDGE_ABNORMAL))
4682 break;
4683 if (e == NULL)
4684 return false;
4685 }
4686
4687 if (infer_nonnull_range (stmt, op, true, true))
4688 {
4689 *val_p = build_int_cst (TREE_TYPE (op), 0);
4690 *comp_code_p = NE_EXPR;
4691 return true;
4692 }
4693
4694 return false;
4695 }
4696
4697
4698 void dump_asserts_for (FILE *, tree);
4699 void debug_asserts_for (tree);
4700 void dump_all_asserts (FILE *);
4701 void debug_all_asserts (void);
4702
4703 /* Dump all the registered assertions for NAME to FILE. */
4704
4705 void
4706 dump_asserts_for (FILE *file, tree name)
4707 {
4708 assert_locus_t loc;
4709
4710 fprintf (file, "Assertions to be inserted for ");
4711 print_generic_expr (file, name, 0);
4712 fprintf (file, "\n");
4713
4714 loc = asserts_for[SSA_NAME_VERSION (name)];
4715 while (loc)
4716 {
4717 fprintf (file, "\t");
4718 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4719 fprintf (file, "\n\tBB #%d", loc->bb->index);
4720 if (loc->e)
4721 {
4722 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4723 loc->e->dest->index);
4724 dump_edge_info (file, loc->e, dump_flags, 0);
4725 }
4726 fprintf (file, "\n\tPREDICATE: ");
4727 print_generic_expr (file, name, 0);
4728 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4729 print_generic_expr (file, loc->val, 0);
4730 fprintf (file, "\n\n");
4731 loc = loc->next;
4732 }
4733
4734 fprintf (file, "\n");
4735 }
4736
4737
4738 /* Dump all the registered assertions for NAME to stderr. */
4739
4740 DEBUG_FUNCTION void
4741 debug_asserts_for (tree name)
4742 {
4743 dump_asserts_for (stderr, name);
4744 }
4745
4746
4747 /* Dump all the registered assertions for all the names to FILE. */
4748
4749 void
4750 dump_all_asserts (FILE *file)
4751 {
4752 unsigned i;
4753 bitmap_iterator bi;
4754
4755 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4756 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4757 dump_asserts_for (file, ssa_name (i));
4758 fprintf (file, "\n");
4759 }
4760
4761
4762 /* Dump all the registered assertions for all the names to stderr. */
4763
4764 DEBUG_FUNCTION void
4765 debug_all_asserts (void)
4766 {
4767 dump_all_asserts (stderr);
4768 }
4769
4770
4771 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4772 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4773 E->DEST, then register this location as a possible insertion point
4774 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4775
4776 BB, E and SI provide the exact insertion point for the new
4777 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4778 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4779 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4780 must not be NULL. */
4781
4782 static void
4783 register_new_assert_for (tree name, tree expr,
4784 enum tree_code comp_code,
4785 tree val,
4786 basic_block bb,
4787 edge e,
4788 gimple_stmt_iterator si)
4789 {
4790 assert_locus_t n, loc, last_loc;
4791 basic_block dest_bb;
4792
4793 gcc_checking_assert (bb == NULL || e == NULL);
4794
4795 if (e == NULL)
4796 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4797 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4798
4799 /* Never build an assert comparing against an integer constant with
4800 TREE_OVERFLOW set. This confuses our undefined overflow warning
4801 machinery. */
4802 if (TREE_OVERFLOW_P (val))
4803 val = drop_tree_overflow (val);
4804
4805 /* The new assertion A will be inserted at BB or E. We need to
4806 determine if the new location is dominated by a previously
4807 registered location for A. If we are doing an edge insertion,
4808 assume that A will be inserted at E->DEST. Note that this is not
4809 necessarily true.
4810
4811 If E is a critical edge, it will be split. But even if E is
4812 split, the new block will dominate the same set of blocks that
4813 E->DEST dominates.
4814
4815 The reverse, however, is not true, blocks dominated by E->DEST
4816 will not be dominated by the new block created to split E. So,
4817 if the insertion location is on a critical edge, we will not use
4818 the new location to move another assertion previously registered
4819 at a block dominated by E->DEST. */
4820 dest_bb = (bb) ? bb : e->dest;
4821
4822 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4823 VAL at a block dominating DEST_BB, then we don't need to insert a new
4824 one. Similarly, if the same assertion already exists at a block
4825 dominated by DEST_BB and the new location is not on a critical
4826 edge, then update the existing location for the assertion (i.e.,
4827 move the assertion up in the dominance tree).
4828
4829 Note, this is implemented as a simple linked list because there
4830 should not be more than a handful of assertions registered per
4831 name. If this becomes a performance problem, a table hashed by
4832 COMP_CODE and VAL could be implemented. */
4833 loc = asserts_for[SSA_NAME_VERSION (name)];
4834 last_loc = loc;
4835 while (loc)
4836 {
4837 if (loc->comp_code == comp_code
4838 && (loc->val == val
4839 || operand_equal_p (loc->val, val, 0))
4840 && (loc->expr == expr
4841 || operand_equal_p (loc->expr, expr, 0)))
4842 {
4843 /* If E is not a critical edge and DEST_BB
4844 dominates the existing location for the assertion, move
4845 the assertion up in the dominance tree by updating its
4846 location information. */
4847 if ((e == NULL || !EDGE_CRITICAL_P (e))
4848 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4849 {
4850 loc->bb = dest_bb;
4851 loc->e = e;
4852 loc->si = si;
4853 return;
4854 }
4855 }
4856
4857 /* Update the last node of the list and move to the next one. */
4858 last_loc = loc;
4859 loc = loc->next;
4860 }
4861
4862 /* If we didn't find an assertion already registered for
4863 NAME COMP_CODE VAL, add a new one at the end of the list of
4864 assertions associated with NAME. */
4865 n = XNEW (struct assert_locus_d);
4866 n->bb = dest_bb;
4867 n->e = e;
4868 n->si = si;
4869 n->comp_code = comp_code;
4870 n->val = val;
4871 n->expr = expr;
4872 n->next = NULL;
4873
4874 if (last_loc)
4875 last_loc->next = n;
4876 else
4877 asserts_for[SSA_NAME_VERSION (name)] = n;
4878
4879 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4880 }
4881
4882 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4883 Extract a suitable test code and value and store them into *CODE_P and
4884 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4885
4886 If no extraction was possible, return FALSE, otherwise return TRUE.
4887
4888 If INVERT is true, then we invert the result stored into *CODE_P. */
4889
4890 static bool
4891 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4892 tree cond_op0, tree cond_op1,
4893 bool invert, enum tree_code *code_p,
4894 tree *val_p)
4895 {
4896 enum tree_code comp_code;
4897 tree val;
4898
4899 /* Otherwise, we have a comparison of the form NAME COMP VAL
4900 or VAL COMP NAME. */
4901 if (name == cond_op1)
4902 {
4903 /* If the predicate is of the form VAL COMP NAME, flip
4904 COMP around because we need to register NAME as the
4905 first operand in the predicate. */
4906 comp_code = swap_tree_comparison (cond_code);
4907 val = cond_op0;
4908 }
4909 else
4910 {
4911 /* The comparison is of the form NAME COMP VAL, so the
4912 comparison code remains unchanged. */
4913 comp_code = cond_code;
4914 val = cond_op1;
4915 }
4916
4917 /* Invert the comparison code as necessary. */
4918 if (invert)
4919 comp_code = invert_tree_comparison (comp_code, 0);
4920
4921 /* VRP does not handle float types. */
4922 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
4923 return false;
4924
4925 /* Do not register always-false predicates.
4926 FIXME: this works around a limitation in fold() when dealing with
4927 enumerations. Given 'enum { N1, N2 } x;', fold will not
4928 fold 'if (x > N2)' to 'if (0)'. */
4929 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4930 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4931 {
4932 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4933 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4934
4935 if (comp_code == GT_EXPR
4936 && (!max
4937 || compare_values (val, max) == 0))
4938 return false;
4939
4940 if (comp_code == LT_EXPR
4941 && (!min
4942 || compare_values (val, min) == 0))
4943 return false;
4944 }
4945 *code_p = comp_code;
4946 *val_p = val;
4947 return true;
4948 }
4949
4950 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4951 (otherwise return VAL). VAL and MASK must be zero-extended for
4952 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4953 (to transform signed values into unsigned) and at the end xor
4954 SGNBIT back. */
4955
4956 static wide_int
4957 masked_increment (const wide_int &val_in, const wide_int &mask,
4958 const wide_int &sgnbit, unsigned int prec)
4959 {
4960 wide_int bit = wi::one (prec), res;
4961 unsigned int i;
4962
4963 wide_int val = val_in ^ sgnbit;
4964 for (i = 0; i < prec; i++, bit += bit)
4965 {
4966 res = mask;
4967 if ((res & bit) == 0)
4968 continue;
4969 res = bit - 1;
4970 res = (val + bit).and_not (res);
4971 res &= mask;
4972 if (wi::gtu_p (res, val))
4973 return res ^ sgnbit;
4974 }
4975 return val ^ sgnbit;
4976 }
4977
4978 /* Try to register an edge assertion for SSA name NAME on edge E for
4979 the condition COND contributing to the conditional jump pointed to by BSI.
4980 Invert the condition COND if INVERT is true. */
4981
4982 static void
4983 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
4984 enum tree_code cond_code,
4985 tree cond_op0, tree cond_op1, bool invert)
4986 {
4987 tree val;
4988 enum tree_code comp_code;
4989
4990 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4991 cond_op0,
4992 cond_op1,
4993 invert, &comp_code, &val))
4994 return;
4995
4996 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4997 reachable from E. */
4998 if (live_on_edge (e, name)
4999 && !has_single_use (name))
5000 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5001
5002 /* In the case of NAME <= CST and NAME being defined as
5003 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5004 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5005 This catches range and anti-range tests. */
5006 if ((comp_code == LE_EXPR
5007 || comp_code == GT_EXPR)
5008 && TREE_CODE (val) == INTEGER_CST
5009 && TYPE_UNSIGNED (TREE_TYPE (val)))
5010 {
5011 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5012 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5013
5014 /* Extract CST2 from the (optional) addition. */
5015 if (is_gimple_assign (def_stmt)
5016 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5017 {
5018 name2 = gimple_assign_rhs1 (def_stmt);
5019 cst2 = gimple_assign_rhs2 (def_stmt);
5020 if (TREE_CODE (name2) == SSA_NAME
5021 && TREE_CODE (cst2) == INTEGER_CST)
5022 def_stmt = SSA_NAME_DEF_STMT (name2);
5023 }
5024
5025 /* Extract NAME2 from the (optional) sign-changing cast. */
5026 if (gimple_assign_cast_p (def_stmt))
5027 {
5028 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5029 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5030 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5031 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5032 name3 = gimple_assign_rhs1 (def_stmt);
5033 }
5034
5035 /* If name3 is used later, create an ASSERT_EXPR for it. */
5036 if (name3 != NULL_TREE
5037 && TREE_CODE (name3) == SSA_NAME
5038 && (cst2 == NULL_TREE
5039 || TREE_CODE (cst2) == INTEGER_CST)
5040 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5041 && live_on_edge (e, name3)
5042 && !has_single_use (name3))
5043 {
5044 tree tmp;
5045
5046 /* Build an expression for the range test. */
5047 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5048 if (cst2 != NULL_TREE)
5049 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5050
5051 if (dump_file)
5052 {
5053 fprintf (dump_file, "Adding assert for ");
5054 print_generic_expr (dump_file, name3, 0);
5055 fprintf (dump_file, " from ");
5056 print_generic_expr (dump_file, tmp, 0);
5057 fprintf (dump_file, "\n");
5058 }
5059
5060 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5061 }
5062
5063 /* If name2 is used later, create an ASSERT_EXPR for it. */
5064 if (name2 != NULL_TREE
5065 && TREE_CODE (name2) == SSA_NAME
5066 && TREE_CODE (cst2) == INTEGER_CST
5067 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5068 && live_on_edge (e, name2)
5069 && !has_single_use (name2))
5070 {
5071 tree tmp;
5072
5073 /* Build an expression for the range test. */
5074 tmp = name2;
5075 if (TREE_TYPE (name) != TREE_TYPE (name2))
5076 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5077 if (cst2 != NULL_TREE)
5078 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5079
5080 if (dump_file)
5081 {
5082 fprintf (dump_file, "Adding assert for ");
5083 print_generic_expr (dump_file, name2, 0);
5084 fprintf (dump_file, " from ");
5085 print_generic_expr (dump_file, tmp, 0);
5086 fprintf (dump_file, "\n");
5087 }
5088
5089 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5090 }
5091 }
5092
5093 /* In the case of post-in/decrement tests like if (i++) ... and uses
5094 of the in/decremented value on the edge the extra name we want to
5095 assert for is not on the def chain of the name compared. Instead
5096 it is in the set of use stmts. */
5097 if ((comp_code == NE_EXPR
5098 || comp_code == EQ_EXPR)
5099 && TREE_CODE (val) == INTEGER_CST)
5100 {
5101 imm_use_iterator ui;
5102 gimple use_stmt;
5103 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5104 {
5105 /* Cut off to use-stmts that are in the predecessor. */
5106 if (gimple_bb (use_stmt) != e->src)
5107 continue;
5108
5109 if (!is_gimple_assign (use_stmt))
5110 continue;
5111
5112 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5113 if (code != PLUS_EXPR
5114 && code != MINUS_EXPR)
5115 continue;
5116
5117 tree cst = gimple_assign_rhs2 (use_stmt);
5118 if (TREE_CODE (cst) != INTEGER_CST)
5119 continue;
5120
5121 tree name2 = gimple_assign_lhs (use_stmt);
5122 if (live_on_edge (e, name2))
5123 {
5124 cst = int_const_binop (code, val, cst);
5125 register_new_assert_for (name2, name2, comp_code, cst,
5126 NULL, e, bsi);
5127 }
5128 }
5129 }
5130
5131 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5132 && TREE_CODE (val) == INTEGER_CST)
5133 {
5134 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5135 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5136 tree val2 = NULL_TREE;
5137 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5138 wide_int mask = wi::zero (prec);
5139 unsigned int nprec = prec;
5140 enum tree_code rhs_code = ERROR_MARK;
5141
5142 if (is_gimple_assign (def_stmt))
5143 rhs_code = gimple_assign_rhs_code (def_stmt);
5144
5145 /* Add asserts for NAME cmp CST and NAME being defined
5146 as NAME = (int) NAME2. */
5147 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5148 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5149 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5150 && gimple_assign_cast_p (def_stmt))
5151 {
5152 name2 = gimple_assign_rhs1 (def_stmt);
5153 if (CONVERT_EXPR_CODE_P (rhs_code)
5154 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5155 && TYPE_UNSIGNED (TREE_TYPE (name2))
5156 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5157 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5158 || !tree_int_cst_equal (val,
5159 TYPE_MIN_VALUE (TREE_TYPE (val))))
5160 && live_on_edge (e, name2)
5161 && !has_single_use (name2))
5162 {
5163 tree tmp, cst;
5164 enum tree_code new_comp_code = comp_code;
5165
5166 cst = fold_convert (TREE_TYPE (name2),
5167 TYPE_MIN_VALUE (TREE_TYPE (val)));
5168 /* Build an expression for the range test. */
5169 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5170 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5171 fold_convert (TREE_TYPE (name2), val));
5172 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5173 {
5174 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5175 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5176 build_int_cst (TREE_TYPE (name2), 1));
5177 }
5178
5179 if (dump_file)
5180 {
5181 fprintf (dump_file, "Adding assert for ");
5182 print_generic_expr (dump_file, name2, 0);
5183 fprintf (dump_file, " from ");
5184 print_generic_expr (dump_file, tmp, 0);
5185 fprintf (dump_file, "\n");
5186 }
5187
5188 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5189 e, bsi);
5190 }
5191 }
5192
5193 /* Add asserts for NAME cmp CST and NAME being defined as
5194 NAME = NAME2 >> CST2.
5195
5196 Extract CST2 from the right shift. */
5197 if (rhs_code == RSHIFT_EXPR)
5198 {
5199 name2 = gimple_assign_rhs1 (def_stmt);
5200 cst2 = gimple_assign_rhs2 (def_stmt);
5201 if (TREE_CODE (name2) == SSA_NAME
5202 && tree_fits_uhwi_p (cst2)
5203 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5204 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5205 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5206 && live_on_edge (e, name2)
5207 && !has_single_use (name2))
5208 {
5209 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5210 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5211 }
5212 }
5213 if (val2 != NULL_TREE
5214 && TREE_CODE (val2) == INTEGER_CST
5215 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5216 TREE_TYPE (val),
5217 val2, cst2), val))
5218 {
5219 enum tree_code new_comp_code = comp_code;
5220 tree tmp, new_val;
5221
5222 tmp = name2;
5223 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5224 {
5225 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5226 {
5227 tree type = build_nonstandard_integer_type (prec, 1);
5228 tmp = build1 (NOP_EXPR, type, name2);
5229 val2 = fold_convert (type, val2);
5230 }
5231 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5232 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5233 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5234 }
5235 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5236 {
5237 wide_int minval
5238 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5239 new_val = val2;
5240 if (minval == new_val)
5241 new_val = NULL_TREE;
5242 }
5243 else
5244 {
5245 wide_int maxval
5246 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5247 mask |= val2;
5248 if (mask == maxval)
5249 new_val = NULL_TREE;
5250 else
5251 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5252 }
5253
5254 if (new_val)
5255 {
5256 if (dump_file)
5257 {
5258 fprintf (dump_file, "Adding assert for ");
5259 print_generic_expr (dump_file, name2, 0);
5260 fprintf (dump_file, " from ");
5261 print_generic_expr (dump_file, tmp, 0);
5262 fprintf (dump_file, "\n");
5263 }
5264
5265 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5266 NULL, e, bsi);
5267 }
5268 }
5269
5270 /* Add asserts for NAME cmp CST and NAME being defined as
5271 NAME = NAME2 & CST2.
5272
5273 Extract CST2 from the and.
5274
5275 Also handle
5276 NAME = (unsigned) NAME2;
5277 casts where NAME's type is unsigned and has smaller precision
5278 than NAME2's type as if it was NAME = NAME2 & MASK. */
5279 names[0] = NULL_TREE;
5280 names[1] = NULL_TREE;
5281 cst2 = NULL_TREE;
5282 if (rhs_code == BIT_AND_EXPR
5283 || (CONVERT_EXPR_CODE_P (rhs_code)
5284 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5285 && TYPE_UNSIGNED (TREE_TYPE (val))
5286 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5287 > prec))
5288 {
5289 name2 = gimple_assign_rhs1 (def_stmt);
5290 if (rhs_code == BIT_AND_EXPR)
5291 cst2 = gimple_assign_rhs2 (def_stmt);
5292 else
5293 {
5294 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5295 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5296 }
5297 if (TREE_CODE (name2) == SSA_NAME
5298 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5299 && TREE_CODE (cst2) == INTEGER_CST
5300 && !integer_zerop (cst2)
5301 && (nprec > 1
5302 || TYPE_UNSIGNED (TREE_TYPE (val))))
5303 {
5304 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5305 if (gimple_assign_cast_p (def_stmt2))
5306 {
5307 names[1] = gimple_assign_rhs1 (def_stmt2);
5308 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5309 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5310 || (TYPE_PRECISION (TREE_TYPE (name2))
5311 != TYPE_PRECISION (TREE_TYPE (names[1])))
5312 || !live_on_edge (e, names[1])
5313 || has_single_use (names[1]))
5314 names[1] = NULL_TREE;
5315 }
5316 if (live_on_edge (e, name2)
5317 && !has_single_use (name2))
5318 names[0] = name2;
5319 }
5320 }
5321 if (names[0] || names[1])
5322 {
5323 wide_int minv, maxv, valv, cst2v;
5324 wide_int tem, sgnbit;
5325 bool valid_p = false, valn, cst2n;
5326 enum tree_code ccode = comp_code;
5327
5328 valv = wide_int::from (val, nprec, UNSIGNED);
5329 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5330 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5331 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5332 /* If CST2 doesn't have most significant bit set,
5333 but VAL is negative, we have comparison like
5334 if ((x & 0x123) > -4) (always true). Just give up. */
5335 if (!cst2n && valn)
5336 ccode = ERROR_MARK;
5337 if (cst2n)
5338 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5339 else
5340 sgnbit = wi::zero (nprec);
5341 minv = valv & cst2v;
5342 switch (ccode)
5343 {
5344 case EQ_EXPR:
5345 /* Minimum unsigned value for equality is VAL & CST2
5346 (should be equal to VAL, otherwise we probably should
5347 have folded the comparison into false) and
5348 maximum unsigned value is VAL | ~CST2. */
5349 maxv = valv | ~cst2v;
5350 valid_p = true;
5351 break;
5352
5353 case NE_EXPR:
5354 tem = valv | ~cst2v;
5355 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5356 if (valv == 0)
5357 {
5358 cst2n = false;
5359 sgnbit = wi::zero (nprec);
5360 goto gt_expr;
5361 }
5362 /* If (VAL | ~CST2) is all ones, handle it as
5363 (X & CST2) < VAL. */
5364 if (tem == -1)
5365 {
5366 cst2n = false;
5367 valn = false;
5368 sgnbit = wi::zero (nprec);
5369 goto lt_expr;
5370 }
5371 if (!cst2n && wi::neg_p (cst2v))
5372 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5373 if (sgnbit != 0)
5374 {
5375 if (valv == sgnbit)
5376 {
5377 cst2n = true;
5378 valn = true;
5379 goto gt_expr;
5380 }
5381 if (tem == wi::mask (nprec - 1, false, nprec))
5382 {
5383 cst2n = true;
5384 goto lt_expr;
5385 }
5386 if (!cst2n)
5387 sgnbit = wi::zero (nprec);
5388 }
5389 break;
5390
5391 case GE_EXPR:
5392 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5393 is VAL and maximum unsigned value is ~0. For signed
5394 comparison, if CST2 doesn't have most significant bit
5395 set, handle it similarly. If CST2 has MSB set,
5396 the minimum is the same, and maximum is ~0U/2. */
5397 if (minv != valv)
5398 {
5399 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5400 VAL. */
5401 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5402 if (minv == valv)
5403 break;
5404 }
5405 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5406 valid_p = true;
5407 break;
5408
5409 case GT_EXPR:
5410 gt_expr:
5411 /* Find out smallest MINV where MINV > VAL
5412 && (MINV & CST2) == MINV, if any. If VAL is signed and
5413 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5414 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5415 if (minv == valv)
5416 break;
5417 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5418 valid_p = true;
5419 break;
5420
5421 case LE_EXPR:
5422 /* Minimum unsigned value for <= is 0 and maximum
5423 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5424 Otherwise, find smallest VAL2 where VAL2 > VAL
5425 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5426 as maximum.
5427 For signed comparison, if CST2 doesn't have most
5428 significant bit set, handle it similarly. If CST2 has
5429 MSB set, the maximum is the same and minimum is INT_MIN. */
5430 if (minv == valv)
5431 maxv = valv;
5432 else
5433 {
5434 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5435 if (maxv == valv)
5436 break;
5437 maxv -= 1;
5438 }
5439 maxv |= ~cst2v;
5440 minv = sgnbit;
5441 valid_p = true;
5442 break;
5443
5444 case LT_EXPR:
5445 lt_expr:
5446 /* Minimum unsigned value for < is 0 and maximum
5447 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5448 Otherwise, find smallest VAL2 where VAL2 > VAL
5449 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5450 as maximum.
5451 For signed comparison, if CST2 doesn't have most
5452 significant bit set, handle it similarly. If CST2 has
5453 MSB set, the maximum is the same and minimum is INT_MIN. */
5454 if (minv == valv)
5455 {
5456 if (valv == sgnbit)
5457 break;
5458 maxv = valv;
5459 }
5460 else
5461 {
5462 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5463 if (maxv == valv)
5464 break;
5465 }
5466 maxv -= 1;
5467 maxv |= ~cst2v;
5468 minv = sgnbit;
5469 valid_p = true;
5470 break;
5471
5472 default:
5473 break;
5474 }
5475 if (valid_p
5476 && (maxv - minv) != -1)
5477 {
5478 tree tmp, new_val, type;
5479 int i;
5480
5481 for (i = 0; i < 2; i++)
5482 if (names[i])
5483 {
5484 wide_int maxv2 = maxv;
5485 tmp = names[i];
5486 type = TREE_TYPE (names[i]);
5487 if (!TYPE_UNSIGNED (type))
5488 {
5489 type = build_nonstandard_integer_type (nprec, 1);
5490 tmp = build1 (NOP_EXPR, type, names[i]);
5491 }
5492 if (minv != 0)
5493 {
5494 tmp = build2 (PLUS_EXPR, type, tmp,
5495 wide_int_to_tree (type, -minv));
5496 maxv2 = maxv - minv;
5497 }
5498 new_val = wide_int_to_tree (type, maxv2);
5499
5500 if (dump_file)
5501 {
5502 fprintf (dump_file, "Adding assert for ");
5503 print_generic_expr (dump_file, names[i], 0);
5504 fprintf (dump_file, " from ");
5505 print_generic_expr (dump_file, tmp, 0);
5506 fprintf (dump_file, "\n");
5507 }
5508
5509 register_new_assert_for (names[i], tmp, LE_EXPR,
5510 new_val, NULL, e, bsi);
5511 }
5512 }
5513 }
5514 }
5515 }
5516
5517 /* OP is an operand of a truth value expression which is known to have
5518 a particular value. Register any asserts for OP and for any
5519 operands in OP's defining statement.
5520
5521 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5522 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5523
5524 static void
5525 register_edge_assert_for_1 (tree op, enum tree_code code,
5526 edge e, gimple_stmt_iterator bsi)
5527 {
5528 gimple op_def;
5529 tree val;
5530 enum tree_code rhs_code;
5531
5532 /* We only care about SSA_NAMEs. */
5533 if (TREE_CODE (op) != SSA_NAME)
5534 return;
5535
5536 /* We know that OP will have a zero or nonzero value. If OP is used
5537 more than once go ahead and register an assert for OP. */
5538 if (live_on_edge (e, op)
5539 && !has_single_use (op))
5540 {
5541 val = build_int_cst (TREE_TYPE (op), 0);
5542 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5543 }
5544
5545 /* Now look at how OP is set. If it's set from a comparison,
5546 a truth operation or some bit operations, then we may be able
5547 to register information about the operands of that assignment. */
5548 op_def = SSA_NAME_DEF_STMT (op);
5549 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5550 return;
5551
5552 rhs_code = gimple_assign_rhs_code (op_def);
5553
5554 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5555 {
5556 bool invert = (code == EQ_EXPR ? true : false);
5557 tree op0 = gimple_assign_rhs1 (op_def);
5558 tree op1 = gimple_assign_rhs2 (op_def);
5559
5560 if (TREE_CODE (op0) == SSA_NAME)
5561 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5562 if (TREE_CODE (op1) == SSA_NAME)
5563 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5564 }
5565 else if ((code == NE_EXPR
5566 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5567 || (code == EQ_EXPR
5568 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5569 {
5570 /* Recurse on each operand. */
5571 tree op0 = gimple_assign_rhs1 (op_def);
5572 tree op1 = gimple_assign_rhs2 (op_def);
5573 if (TREE_CODE (op0) == SSA_NAME
5574 && has_single_use (op0))
5575 register_edge_assert_for_1 (op0, code, e, bsi);
5576 if (TREE_CODE (op1) == SSA_NAME
5577 && has_single_use (op1))
5578 register_edge_assert_for_1 (op1, code, e, bsi);
5579 }
5580 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5581 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5582 {
5583 /* Recurse, flipping CODE. */
5584 code = invert_tree_comparison (code, false);
5585 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5586 }
5587 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5588 {
5589 /* Recurse through the copy. */
5590 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5591 }
5592 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5593 {
5594 /* Recurse through the type conversion, unless it is a narrowing
5595 conversion or conversion from non-integral type. */
5596 tree rhs = gimple_assign_rhs1 (op_def);
5597 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5598 && (TYPE_PRECISION (TREE_TYPE (rhs))
5599 <= TYPE_PRECISION (TREE_TYPE (op))))
5600 register_edge_assert_for_1 (rhs, code, e, bsi);
5601 }
5602 }
5603
5604 /* Try to register an edge assertion for SSA name NAME on edge E for
5605 the condition COND contributing to the conditional jump pointed to by
5606 SI. */
5607
5608 static void
5609 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5610 enum tree_code cond_code, tree cond_op0,
5611 tree cond_op1)
5612 {
5613 tree val;
5614 enum tree_code comp_code;
5615 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5616
5617 /* Do not attempt to infer anything in names that flow through
5618 abnormal edges. */
5619 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5620 return;
5621
5622 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5623 cond_op0, cond_op1,
5624 is_else_edge,
5625 &comp_code, &val))
5626 return;
5627
5628 /* Register ASSERT_EXPRs for name. */
5629 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5630 cond_op1, is_else_edge);
5631
5632
5633 /* If COND is effectively an equality test of an SSA_NAME against
5634 the value zero or one, then we may be able to assert values
5635 for SSA_NAMEs which flow into COND. */
5636
5637 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5638 statement of NAME we can assert both operands of the BIT_AND_EXPR
5639 have nonzero value. */
5640 if (((comp_code == EQ_EXPR && integer_onep (val))
5641 || (comp_code == NE_EXPR && integer_zerop (val))))
5642 {
5643 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5644
5645 if (is_gimple_assign (def_stmt)
5646 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5647 {
5648 tree op0 = gimple_assign_rhs1 (def_stmt);
5649 tree op1 = gimple_assign_rhs2 (def_stmt);
5650 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5651 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5652 }
5653 }
5654
5655 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5656 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5657 have zero value. */
5658 if (((comp_code == EQ_EXPR && integer_zerop (val))
5659 || (comp_code == NE_EXPR && integer_onep (val))))
5660 {
5661 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5662
5663 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5664 necessarily zero value, or if type-precision is one. */
5665 if (is_gimple_assign (def_stmt)
5666 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5667 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5668 || comp_code == EQ_EXPR)))
5669 {
5670 tree op0 = gimple_assign_rhs1 (def_stmt);
5671 tree op1 = gimple_assign_rhs2 (def_stmt);
5672 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5673 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5674 }
5675 }
5676 }
5677
5678
5679 /* Determine whether the outgoing edges of BB should receive an
5680 ASSERT_EXPR for each of the operands of BB's LAST statement.
5681 The last statement of BB must be a COND_EXPR.
5682
5683 If any of the sub-graphs rooted at BB have an interesting use of
5684 the predicate operands, an assert location node is added to the
5685 list of assertions for the corresponding operands. */
5686
5687 static void
5688 find_conditional_asserts (basic_block bb, gimple last)
5689 {
5690 gimple_stmt_iterator bsi;
5691 tree op;
5692 edge_iterator ei;
5693 edge e;
5694 ssa_op_iter iter;
5695
5696 bsi = gsi_for_stmt (last);
5697
5698 /* Look for uses of the operands in each of the sub-graphs
5699 rooted at BB. We need to check each of the outgoing edges
5700 separately, so that we know what kind of ASSERT_EXPR to
5701 insert. */
5702 FOR_EACH_EDGE (e, ei, bb->succs)
5703 {
5704 if (e->dest == bb)
5705 continue;
5706
5707 /* Register the necessary assertions for each operand in the
5708 conditional predicate. */
5709 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5710 register_edge_assert_for (op, e, bsi,
5711 gimple_cond_code (last),
5712 gimple_cond_lhs (last),
5713 gimple_cond_rhs (last));
5714 }
5715 }
5716
5717 struct case_info
5718 {
5719 tree expr;
5720 basic_block bb;
5721 };
5722
5723 /* Compare two case labels sorting first by the destination bb index
5724 and then by the case value. */
5725
5726 static int
5727 compare_case_labels (const void *p1, const void *p2)
5728 {
5729 const struct case_info *ci1 = (const struct case_info *) p1;
5730 const struct case_info *ci2 = (const struct case_info *) p2;
5731 int idx1 = ci1->bb->index;
5732 int idx2 = ci2->bb->index;
5733
5734 if (idx1 < idx2)
5735 return -1;
5736 else if (idx1 == idx2)
5737 {
5738 /* Make sure the default label is first in a group. */
5739 if (!CASE_LOW (ci1->expr))
5740 return -1;
5741 else if (!CASE_LOW (ci2->expr))
5742 return 1;
5743 else
5744 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5745 CASE_LOW (ci2->expr));
5746 }
5747 else
5748 return 1;
5749 }
5750
5751 /* Determine whether the outgoing edges of BB should receive an
5752 ASSERT_EXPR for each of the operands of BB's LAST statement.
5753 The last statement of BB must be a SWITCH_EXPR.
5754
5755 If any of the sub-graphs rooted at BB have an interesting use of
5756 the predicate operands, an assert location node is added to the
5757 list of assertions for the corresponding operands. */
5758
5759 static void
5760 find_switch_asserts (basic_block bb, gimple last)
5761 {
5762 gimple_stmt_iterator bsi;
5763 tree op;
5764 edge e;
5765 struct case_info *ci;
5766 size_t n = gimple_switch_num_labels (last);
5767 #if GCC_VERSION >= 4000
5768 unsigned int idx;
5769 #else
5770 /* Work around GCC 3.4 bug (PR 37086). */
5771 volatile unsigned int idx;
5772 #endif
5773
5774 bsi = gsi_for_stmt (last);
5775 op = gimple_switch_index (last);
5776 if (TREE_CODE (op) != SSA_NAME)
5777 return;
5778
5779 /* Build a vector of case labels sorted by destination label. */
5780 ci = XNEWVEC (struct case_info, n);
5781 for (idx = 0; idx < n; ++idx)
5782 {
5783 ci[idx].expr = gimple_switch_label (last, idx);
5784 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5785 }
5786 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5787
5788 for (idx = 0; idx < n; ++idx)
5789 {
5790 tree min, max;
5791 tree cl = ci[idx].expr;
5792 basic_block cbb = ci[idx].bb;
5793
5794 min = CASE_LOW (cl);
5795 max = CASE_HIGH (cl);
5796
5797 /* If there are multiple case labels with the same destination
5798 we need to combine them to a single value range for the edge. */
5799 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5800 {
5801 /* Skip labels until the last of the group. */
5802 do {
5803 ++idx;
5804 } while (idx < n && cbb == ci[idx].bb);
5805 --idx;
5806
5807 /* Pick up the maximum of the case label range. */
5808 if (CASE_HIGH (ci[idx].expr))
5809 max = CASE_HIGH (ci[idx].expr);
5810 else
5811 max = CASE_LOW (ci[idx].expr);
5812 }
5813
5814 /* Nothing to do if the range includes the default label until we
5815 can register anti-ranges. */
5816 if (min == NULL_TREE)
5817 continue;
5818
5819 /* Find the edge to register the assert expr on. */
5820 e = find_edge (bb, cbb);
5821
5822 /* Register the necessary assertions for the operand in the
5823 SWITCH_EXPR. */
5824 register_edge_assert_for (op, e, bsi,
5825 max ? GE_EXPR : EQ_EXPR,
5826 op, fold_convert (TREE_TYPE (op), min));
5827 if (max)
5828 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5829 fold_convert (TREE_TYPE (op), max));
5830 }
5831
5832 XDELETEVEC (ci);
5833 }
5834
5835
5836 /* Traverse all the statements in block BB looking for statements that
5837 may generate useful assertions for the SSA names in their operand.
5838 If a statement produces a useful assertion A for name N_i, then the
5839 list of assertions already generated for N_i is scanned to
5840 determine if A is actually needed.
5841
5842 If N_i already had the assertion A at a location dominating the
5843 current location, then nothing needs to be done. Otherwise, the
5844 new location for A is recorded instead.
5845
5846 1- For every statement S in BB, all the variables used by S are
5847 added to bitmap FOUND_IN_SUBGRAPH.
5848
5849 2- If statement S uses an operand N in a way that exposes a known
5850 value range for N, then if N was not already generated by an
5851 ASSERT_EXPR, create a new assert location for N. For instance,
5852 if N is a pointer and the statement dereferences it, we can
5853 assume that N is not NULL.
5854
5855 3- COND_EXPRs are a special case of #2. We can derive range
5856 information from the predicate but need to insert different
5857 ASSERT_EXPRs for each of the sub-graphs rooted at the
5858 conditional block. If the last statement of BB is a conditional
5859 expression of the form 'X op Y', then
5860
5861 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5862
5863 b) If the conditional is the only entry point to the sub-graph
5864 corresponding to the THEN_CLAUSE, recurse into it. On
5865 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5866 an ASSERT_EXPR is added for the corresponding variable.
5867
5868 c) Repeat step (b) on the ELSE_CLAUSE.
5869
5870 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5871
5872 For instance,
5873
5874 if (a == 9)
5875 b = a;
5876 else
5877 b = c + 1;
5878
5879 In this case, an assertion on the THEN clause is useful to
5880 determine that 'a' is always 9 on that edge. However, an assertion
5881 on the ELSE clause would be unnecessary.
5882
5883 4- If BB does not end in a conditional expression, then we recurse
5884 into BB's dominator children.
5885
5886 At the end of the recursive traversal, every SSA name will have a
5887 list of locations where ASSERT_EXPRs should be added. When a new
5888 location for name N is found, it is registered by calling
5889 register_new_assert_for. That function keeps track of all the
5890 registered assertions to prevent adding unnecessary assertions.
5891 For instance, if a pointer P_4 is dereferenced more than once in a
5892 dominator tree, only the location dominating all the dereference of
5893 P_4 will receive an ASSERT_EXPR. */
5894
5895 static void
5896 find_assert_locations_1 (basic_block bb, sbitmap live)
5897 {
5898 gimple_stmt_iterator si;
5899 gimple last;
5900
5901 last = last_stmt (bb);
5902
5903 /* If BB's last statement is a conditional statement involving integer
5904 operands, determine if we need to add ASSERT_EXPRs. */
5905 if (last
5906 && gimple_code (last) == GIMPLE_COND
5907 && !fp_predicate (last)
5908 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5909 find_conditional_asserts (bb, last);
5910
5911 /* If BB's last statement is a switch statement involving integer
5912 operands, determine if we need to add ASSERT_EXPRs. */
5913 if (last
5914 && gimple_code (last) == GIMPLE_SWITCH
5915 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
5916 find_switch_asserts (bb, last);
5917
5918 /* Traverse all the statements in BB marking used names and looking
5919 for statements that may infer assertions for their used operands. */
5920 for (si = gsi_last_bb (bb); !gsi_end_p (si); gsi_prev (&si))
5921 {
5922 gimple stmt;
5923 tree op;
5924 ssa_op_iter i;
5925
5926 stmt = gsi_stmt (si);
5927
5928 if (is_gimple_debug (stmt))
5929 continue;
5930
5931 /* See if we can derive an assertion for any of STMT's operands. */
5932 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5933 {
5934 tree value;
5935 enum tree_code comp_code;
5936
5937 /* If op is not live beyond this stmt, do not bother to insert
5938 asserts for it. */
5939 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
5940 continue;
5941
5942 /* If OP is used in such a way that we can infer a value
5943 range for it, and we don't find a previous assertion for
5944 it, create a new assertion location node for OP. */
5945 if (infer_value_range (stmt, op, &comp_code, &value))
5946 {
5947 /* If we are able to infer a nonzero value range for OP,
5948 then walk backwards through the use-def chain to see if OP
5949 was set via a typecast.
5950
5951 If so, then we can also infer a nonzero value range
5952 for the operand of the NOP_EXPR. */
5953 if (comp_code == NE_EXPR && integer_zerop (value))
5954 {
5955 tree t = op;
5956 gimple def_stmt = SSA_NAME_DEF_STMT (t);
5957
5958 while (is_gimple_assign (def_stmt)
5959 && CONVERT_EXPR_CODE_P
5960 (gimple_assign_rhs_code (def_stmt))
5961 && TREE_CODE
5962 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
5963 && POINTER_TYPE_P
5964 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
5965 {
5966 t = gimple_assign_rhs1 (def_stmt);
5967 def_stmt = SSA_NAME_DEF_STMT (t);
5968
5969 /* Note we want to register the assert for the
5970 operand of the NOP_EXPR after SI, not after the
5971 conversion. */
5972 if (! has_single_use (t))
5973 register_new_assert_for (t, t, comp_code, value,
5974 bb, NULL, si);
5975 }
5976 }
5977
5978 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
5979 }
5980 }
5981
5982 /* Update live. */
5983 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
5984 bitmap_set_bit (live, SSA_NAME_VERSION (op));
5985 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
5986 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
5987 }
5988
5989 /* Traverse all PHI nodes in BB, updating live. */
5990 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5991 {
5992 use_operand_p arg_p;
5993 ssa_op_iter i;
5994 gimple phi = gsi_stmt (si);
5995 tree res = gimple_phi_result (phi);
5996
5997 if (virtual_operand_p (res))
5998 continue;
5999
6000 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6001 {
6002 tree arg = USE_FROM_PTR (arg_p);
6003 if (TREE_CODE (arg) == SSA_NAME)
6004 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6005 }
6006
6007 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6008 }
6009 }
6010
6011 /* Do an RPO walk over the function computing SSA name liveness
6012 on-the-fly and deciding on assert expressions to insert. */
6013
6014 static void
6015 find_assert_locations (void)
6016 {
6017 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6018 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6019 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6020 int rpo_cnt, i;
6021
6022 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6023 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6024 for (i = 0; i < rpo_cnt; ++i)
6025 bb_rpo[rpo[i]] = i;
6026
6027 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6028 the order we compute liveness and insert asserts we otherwise
6029 fail to insert asserts into the loop latch. */
6030 loop_p loop;
6031 FOR_EACH_LOOP (loop, 0)
6032 {
6033 i = loop->latch->index;
6034 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6035 for (gimple_stmt_iterator gsi = gsi_start_phis (loop->header);
6036 !gsi_end_p (gsi); gsi_next (&gsi))
6037 {
6038 gimple phi = gsi_stmt (gsi);
6039 if (virtual_operand_p (gimple_phi_result (phi)))
6040 continue;
6041 tree arg = gimple_phi_arg_def (phi, j);
6042 if (TREE_CODE (arg) == SSA_NAME)
6043 {
6044 if (live[i] == NULL)
6045 {
6046 live[i] = sbitmap_alloc (num_ssa_names);
6047 bitmap_clear (live[i]);
6048 }
6049 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6050 }
6051 }
6052 }
6053
6054 for (i = rpo_cnt - 1; i >= 0; --i)
6055 {
6056 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6057 edge e;
6058 edge_iterator ei;
6059
6060 if (!live[rpo[i]])
6061 {
6062 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6063 bitmap_clear (live[rpo[i]]);
6064 }
6065
6066 /* Process BB and update the live information with uses in
6067 this block. */
6068 find_assert_locations_1 (bb, live[rpo[i]]);
6069
6070 /* Merge liveness into the predecessor blocks and free it. */
6071 if (!bitmap_empty_p (live[rpo[i]]))
6072 {
6073 int pred_rpo = i;
6074 FOR_EACH_EDGE (e, ei, bb->preds)
6075 {
6076 int pred = e->src->index;
6077 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6078 continue;
6079
6080 if (!live[pred])
6081 {
6082 live[pred] = sbitmap_alloc (num_ssa_names);
6083 bitmap_clear (live[pred]);
6084 }
6085 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6086
6087 if (bb_rpo[pred] < pred_rpo)
6088 pred_rpo = bb_rpo[pred];
6089 }
6090
6091 /* Record the RPO number of the last visited block that needs
6092 live information from this block. */
6093 last_rpo[rpo[i]] = pred_rpo;
6094 }
6095 else
6096 {
6097 sbitmap_free (live[rpo[i]]);
6098 live[rpo[i]] = NULL;
6099 }
6100
6101 /* We can free all successors live bitmaps if all their
6102 predecessors have been visited already. */
6103 FOR_EACH_EDGE (e, ei, bb->succs)
6104 if (last_rpo[e->dest->index] == i
6105 && live[e->dest->index])
6106 {
6107 sbitmap_free (live[e->dest->index]);
6108 live[e->dest->index] = NULL;
6109 }
6110 }
6111
6112 XDELETEVEC (rpo);
6113 XDELETEVEC (bb_rpo);
6114 XDELETEVEC (last_rpo);
6115 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6116 if (live[i])
6117 sbitmap_free (live[i]);
6118 XDELETEVEC (live);
6119 }
6120
6121 /* Create an ASSERT_EXPR for NAME and insert it in the location
6122 indicated by LOC. Return true if we made any edge insertions. */
6123
6124 static bool
6125 process_assert_insertions_for (tree name, assert_locus_t loc)
6126 {
6127 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6128 gimple stmt;
6129 tree cond;
6130 gimple assert_stmt;
6131 edge_iterator ei;
6132 edge e;
6133
6134 /* If we have X <=> X do not insert an assert expr for that. */
6135 if (loc->expr == loc->val)
6136 return false;
6137
6138 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6139 assert_stmt = build_assert_expr_for (cond, name);
6140 if (loc->e)
6141 {
6142 /* We have been asked to insert the assertion on an edge. This
6143 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6144 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6145 || (gimple_code (gsi_stmt (loc->si))
6146 == GIMPLE_SWITCH));
6147
6148 gsi_insert_on_edge (loc->e, assert_stmt);
6149 return true;
6150 }
6151
6152 /* Otherwise, we can insert right after LOC->SI iff the
6153 statement must not be the last statement in the block. */
6154 stmt = gsi_stmt (loc->si);
6155 if (!stmt_ends_bb_p (stmt))
6156 {
6157 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6158 return false;
6159 }
6160
6161 /* If STMT must be the last statement in BB, we can only insert new
6162 assertions on the non-abnormal edge out of BB. Note that since
6163 STMT is not control flow, there may only be one non-abnormal edge
6164 out of BB. */
6165 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6166 if (!(e->flags & EDGE_ABNORMAL))
6167 {
6168 gsi_insert_on_edge (e, assert_stmt);
6169 return true;
6170 }
6171
6172 gcc_unreachable ();
6173 }
6174
6175
6176 /* Process all the insertions registered for every name N_i registered
6177 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6178 found in ASSERTS_FOR[i]. */
6179
6180 static void
6181 process_assert_insertions (void)
6182 {
6183 unsigned i;
6184 bitmap_iterator bi;
6185 bool update_edges_p = false;
6186 int num_asserts = 0;
6187
6188 if (dump_file && (dump_flags & TDF_DETAILS))
6189 dump_all_asserts (dump_file);
6190
6191 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6192 {
6193 assert_locus_t loc = asserts_for[i];
6194 gcc_assert (loc);
6195
6196 while (loc)
6197 {
6198 assert_locus_t next = loc->next;
6199 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6200 free (loc);
6201 loc = next;
6202 num_asserts++;
6203 }
6204 }
6205
6206 if (update_edges_p)
6207 gsi_commit_edge_inserts ();
6208
6209 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6210 num_asserts);
6211 }
6212
6213
6214 /* Traverse the flowgraph looking for conditional jumps to insert range
6215 expressions. These range expressions are meant to provide information
6216 to optimizations that need to reason in terms of value ranges. They
6217 will not be expanded into RTL. For instance, given:
6218
6219 x = ...
6220 y = ...
6221 if (x < y)
6222 y = x - 2;
6223 else
6224 x = y + 3;
6225
6226 this pass will transform the code into:
6227
6228 x = ...
6229 y = ...
6230 if (x < y)
6231 {
6232 x = ASSERT_EXPR <x, x < y>
6233 y = x - 2
6234 }
6235 else
6236 {
6237 y = ASSERT_EXPR <y, x >= y>
6238 x = y + 3
6239 }
6240
6241 The idea is that once copy and constant propagation have run, other
6242 optimizations will be able to determine what ranges of values can 'x'
6243 take in different paths of the code, simply by checking the reaching
6244 definition of 'x'. */
6245
6246 static void
6247 insert_range_assertions (void)
6248 {
6249 need_assert_for = BITMAP_ALLOC (NULL);
6250 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6251
6252 calculate_dominance_info (CDI_DOMINATORS);
6253
6254 find_assert_locations ();
6255 if (!bitmap_empty_p (need_assert_for))
6256 {
6257 process_assert_insertions ();
6258 update_ssa (TODO_update_ssa_no_phi);
6259 }
6260
6261 if (dump_file && (dump_flags & TDF_DETAILS))
6262 {
6263 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6264 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6265 }
6266
6267 free (asserts_for);
6268 BITMAP_FREE (need_assert_for);
6269 }
6270
6271 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6272 and "struct" hacks. If VRP can determine that the
6273 array subscript is a constant, check if it is outside valid
6274 range. If the array subscript is a RANGE, warn if it is
6275 non-overlapping with valid range.
6276 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6277
6278 static void
6279 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6280 {
6281 value_range_t* vr = NULL;
6282 tree low_sub, up_sub;
6283 tree low_bound, up_bound, up_bound_p1;
6284 tree base;
6285
6286 if (TREE_NO_WARNING (ref))
6287 return;
6288
6289 low_sub = up_sub = TREE_OPERAND (ref, 1);
6290 up_bound = array_ref_up_bound (ref);
6291
6292 /* Can not check flexible arrays. */
6293 if (!up_bound
6294 || TREE_CODE (up_bound) != INTEGER_CST)
6295 return;
6296
6297 /* Accesses to trailing arrays via pointers may access storage
6298 beyond the types array bounds. */
6299 base = get_base_address (ref);
6300 if (base && TREE_CODE (base) == MEM_REF)
6301 {
6302 tree cref, next = NULL_TREE;
6303
6304 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6305 return;
6306
6307 cref = TREE_OPERAND (ref, 0);
6308 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6309 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6310 next && TREE_CODE (next) != FIELD_DECL;
6311 next = DECL_CHAIN (next))
6312 ;
6313
6314 /* If this is the last field in a struct type or a field in a
6315 union type do not warn. */
6316 if (!next)
6317 return;
6318 }
6319
6320 low_bound = array_ref_low_bound (ref);
6321 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6322 build_int_cst (TREE_TYPE (up_bound), 1));
6323
6324 if (TREE_CODE (low_sub) == SSA_NAME)
6325 {
6326 vr = get_value_range (low_sub);
6327 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6328 {
6329 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6330 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6331 }
6332 }
6333
6334 if (vr && vr->type == VR_ANTI_RANGE)
6335 {
6336 if (TREE_CODE (up_sub) == INTEGER_CST
6337 && tree_int_cst_lt (up_bound, up_sub)
6338 && TREE_CODE (low_sub) == INTEGER_CST
6339 && tree_int_cst_lt (low_sub, low_bound))
6340 {
6341 warning_at (location, OPT_Warray_bounds,
6342 "array subscript is outside array bounds");
6343 TREE_NO_WARNING (ref) = 1;
6344 }
6345 }
6346 else if (TREE_CODE (up_sub) == INTEGER_CST
6347 && (ignore_off_by_one
6348 ? (tree_int_cst_lt (up_bound, up_sub)
6349 && !tree_int_cst_equal (up_bound_p1, up_sub))
6350 : (tree_int_cst_lt (up_bound, up_sub)
6351 || tree_int_cst_equal (up_bound_p1, up_sub))))
6352 {
6353 if (dump_file && (dump_flags & TDF_DETAILS))
6354 {
6355 fprintf (dump_file, "Array bound warning for ");
6356 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6357 fprintf (dump_file, "\n");
6358 }
6359 warning_at (location, OPT_Warray_bounds,
6360 "array subscript is above array bounds");
6361 TREE_NO_WARNING (ref) = 1;
6362 }
6363 else if (TREE_CODE (low_sub) == INTEGER_CST
6364 && tree_int_cst_lt (low_sub, low_bound))
6365 {
6366 if (dump_file && (dump_flags & TDF_DETAILS))
6367 {
6368 fprintf (dump_file, "Array bound warning for ");
6369 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6370 fprintf (dump_file, "\n");
6371 }
6372 warning_at (location, OPT_Warray_bounds,
6373 "array subscript is below array bounds");
6374 TREE_NO_WARNING (ref) = 1;
6375 }
6376 }
6377
6378 /* Searches if the expr T, located at LOCATION computes
6379 address of an ARRAY_REF, and call check_array_ref on it. */
6380
6381 static void
6382 search_for_addr_array (tree t, location_t location)
6383 {
6384 while (TREE_CODE (t) == SSA_NAME)
6385 {
6386 gimple g = SSA_NAME_DEF_STMT (t);
6387
6388 if (gimple_code (g) != GIMPLE_ASSIGN)
6389 return;
6390
6391 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6392 != GIMPLE_SINGLE_RHS)
6393 return;
6394
6395 t = gimple_assign_rhs1 (g);
6396 }
6397
6398
6399 /* We are only interested in addresses of ARRAY_REF's. */
6400 if (TREE_CODE (t) != ADDR_EXPR)
6401 return;
6402
6403 /* Check each ARRAY_REFs in the reference chain. */
6404 do
6405 {
6406 if (TREE_CODE (t) == ARRAY_REF)
6407 check_array_ref (location, t, true /*ignore_off_by_one*/);
6408
6409 t = TREE_OPERAND (t, 0);
6410 }
6411 while (handled_component_p (t));
6412
6413 if (TREE_CODE (t) == MEM_REF
6414 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6415 && !TREE_NO_WARNING (t))
6416 {
6417 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6418 tree low_bound, up_bound, el_sz;
6419 offset_int idx;
6420 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6421 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6422 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6423 return;
6424
6425 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6426 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6427 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6428 if (!low_bound
6429 || TREE_CODE (low_bound) != INTEGER_CST
6430 || !up_bound
6431 || TREE_CODE (up_bound) != INTEGER_CST
6432 || !el_sz
6433 || TREE_CODE (el_sz) != INTEGER_CST)
6434 return;
6435
6436 idx = mem_ref_offset (t);
6437 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6438 if (wi::lts_p (idx, 0))
6439 {
6440 if (dump_file && (dump_flags & TDF_DETAILS))
6441 {
6442 fprintf (dump_file, "Array bound warning for ");
6443 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6444 fprintf (dump_file, "\n");
6445 }
6446 warning_at (location, OPT_Warray_bounds,
6447 "array subscript is below array bounds");
6448 TREE_NO_WARNING (t) = 1;
6449 }
6450 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6451 - wi::to_offset (low_bound) + 1)))
6452 {
6453 if (dump_file && (dump_flags & TDF_DETAILS))
6454 {
6455 fprintf (dump_file, "Array bound warning for ");
6456 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6457 fprintf (dump_file, "\n");
6458 }
6459 warning_at (location, OPT_Warray_bounds,
6460 "array subscript is above array bounds");
6461 TREE_NO_WARNING (t) = 1;
6462 }
6463 }
6464 }
6465
6466 /* walk_tree() callback that checks if *TP is
6467 an ARRAY_REF inside an ADDR_EXPR (in which an array
6468 subscript one outside the valid range is allowed). Call
6469 check_array_ref for each ARRAY_REF found. The location is
6470 passed in DATA. */
6471
6472 static tree
6473 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6474 {
6475 tree t = *tp;
6476 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6477 location_t location;
6478
6479 if (EXPR_HAS_LOCATION (t))
6480 location = EXPR_LOCATION (t);
6481 else
6482 {
6483 location_t *locp = (location_t *) wi->info;
6484 location = *locp;
6485 }
6486
6487 *walk_subtree = TRUE;
6488
6489 if (TREE_CODE (t) == ARRAY_REF)
6490 check_array_ref (location, t, false /*ignore_off_by_one*/);
6491
6492 if (TREE_CODE (t) == MEM_REF
6493 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6494 search_for_addr_array (TREE_OPERAND (t, 0), location);
6495
6496 if (TREE_CODE (t) == ADDR_EXPR)
6497 *walk_subtree = FALSE;
6498
6499 return NULL_TREE;
6500 }
6501
6502 /* Walk over all statements of all reachable BBs and call check_array_bounds
6503 on them. */
6504
6505 static void
6506 check_all_array_refs (void)
6507 {
6508 basic_block bb;
6509 gimple_stmt_iterator si;
6510
6511 FOR_EACH_BB_FN (bb, cfun)
6512 {
6513 edge_iterator ei;
6514 edge e;
6515 bool executable = false;
6516
6517 /* Skip blocks that were found to be unreachable. */
6518 FOR_EACH_EDGE (e, ei, bb->preds)
6519 executable |= !!(e->flags & EDGE_EXECUTABLE);
6520 if (!executable)
6521 continue;
6522
6523 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6524 {
6525 gimple stmt = gsi_stmt (si);
6526 struct walk_stmt_info wi;
6527 if (!gimple_has_location (stmt))
6528 continue;
6529
6530 if (is_gimple_call (stmt))
6531 {
6532 size_t i;
6533 size_t n = gimple_call_num_args (stmt);
6534 for (i = 0; i < n; i++)
6535 {
6536 tree arg = gimple_call_arg (stmt, i);
6537 search_for_addr_array (arg, gimple_location (stmt));
6538 }
6539 }
6540 else
6541 {
6542 memset (&wi, 0, sizeof (wi));
6543 wi.info = CONST_CAST (void *, (const void *)
6544 gimple_location_ptr (stmt));
6545
6546 walk_gimple_op (gsi_stmt (si),
6547 check_array_bounds,
6548 &wi);
6549 }
6550 }
6551 }
6552 }
6553
6554 /* Return true if all imm uses of VAR are either in STMT, or
6555 feed (optionally through a chain of single imm uses) GIMPLE_COND
6556 in basic block COND_BB. */
6557
6558 static bool
6559 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6560 {
6561 use_operand_p use_p, use2_p;
6562 imm_use_iterator iter;
6563
6564 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6565 if (USE_STMT (use_p) != stmt)
6566 {
6567 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6568 if (is_gimple_debug (use_stmt))
6569 continue;
6570 while (is_gimple_assign (use_stmt)
6571 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6572 && single_imm_use (gimple_assign_lhs (use_stmt),
6573 &use2_p, &use_stmt2))
6574 use_stmt = use_stmt2;
6575 if (gimple_code (use_stmt) != GIMPLE_COND
6576 || gimple_bb (use_stmt) != cond_bb)
6577 return false;
6578 }
6579 return true;
6580 }
6581
6582 /* Handle
6583 _4 = x_3 & 31;
6584 if (_4 != 0)
6585 goto <bb 6>;
6586 else
6587 goto <bb 7>;
6588 <bb 6>:
6589 __builtin_unreachable ();
6590 <bb 7>:
6591 x_5 = ASSERT_EXPR <x_3, ...>;
6592 If x_3 has no other immediate uses (checked by caller),
6593 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6594 from the non-zero bitmask. */
6595
6596 static void
6597 maybe_set_nonzero_bits (basic_block bb, tree var)
6598 {
6599 edge e = single_pred_edge (bb);
6600 basic_block cond_bb = e->src;
6601 gimple stmt = last_stmt (cond_bb);
6602 tree cst;
6603
6604 if (stmt == NULL
6605 || gimple_code (stmt) != GIMPLE_COND
6606 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6607 ? EQ_EXPR : NE_EXPR)
6608 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6609 || !integer_zerop (gimple_cond_rhs (stmt)))
6610 return;
6611
6612 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6613 if (!is_gimple_assign (stmt)
6614 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6615 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6616 return;
6617 if (gimple_assign_rhs1 (stmt) != var)
6618 {
6619 gimple stmt2;
6620
6621 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6622 return;
6623 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6624 if (!gimple_assign_cast_p (stmt2)
6625 || gimple_assign_rhs1 (stmt2) != var
6626 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6627 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6628 != TYPE_PRECISION (TREE_TYPE (var))))
6629 return;
6630 }
6631 cst = gimple_assign_rhs2 (stmt);
6632 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6633 }
6634
6635 /* Convert range assertion expressions into the implied copies and
6636 copy propagate away the copies. Doing the trivial copy propagation
6637 here avoids the need to run the full copy propagation pass after
6638 VRP.
6639
6640 FIXME, this will eventually lead to copy propagation removing the
6641 names that had useful range information attached to them. For
6642 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6643 then N_i will have the range [3, +INF].
6644
6645 However, by converting the assertion into the implied copy
6646 operation N_i = N_j, we will then copy-propagate N_j into the uses
6647 of N_i and lose the range information. We may want to hold on to
6648 ASSERT_EXPRs a little while longer as the ranges could be used in
6649 things like jump threading.
6650
6651 The problem with keeping ASSERT_EXPRs around is that passes after
6652 VRP need to handle them appropriately.
6653
6654 Another approach would be to make the range information a first
6655 class property of the SSA_NAME so that it can be queried from
6656 any pass. This is made somewhat more complex by the need for
6657 multiple ranges to be associated with one SSA_NAME. */
6658
6659 static void
6660 remove_range_assertions (void)
6661 {
6662 basic_block bb;
6663 gimple_stmt_iterator si;
6664 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6665 a basic block preceeded by GIMPLE_COND branching to it and
6666 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6667 int is_unreachable;
6668
6669 /* Note that the BSI iterator bump happens at the bottom of the
6670 loop and no bump is necessary if we're removing the statement
6671 referenced by the current BSI. */
6672 FOR_EACH_BB_FN (bb, cfun)
6673 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6674 {
6675 gimple stmt = gsi_stmt (si);
6676 gimple use_stmt;
6677
6678 if (is_gimple_assign (stmt)
6679 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6680 {
6681 tree lhs = gimple_assign_lhs (stmt);
6682 tree rhs = gimple_assign_rhs1 (stmt);
6683 tree var;
6684 tree cond = fold (ASSERT_EXPR_COND (rhs));
6685 use_operand_p use_p;
6686 imm_use_iterator iter;
6687
6688 gcc_assert (cond != boolean_false_node);
6689
6690 var = ASSERT_EXPR_VAR (rhs);
6691 gcc_assert (TREE_CODE (var) == SSA_NAME);
6692
6693 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6694 && SSA_NAME_RANGE_INFO (lhs))
6695 {
6696 if (is_unreachable == -1)
6697 {
6698 is_unreachable = 0;
6699 if (single_pred_p (bb)
6700 && assert_unreachable_fallthru_edge_p
6701 (single_pred_edge (bb)))
6702 is_unreachable = 1;
6703 }
6704 /* Handle
6705 if (x_7 >= 10 && x_7 < 20)
6706 __builtin_unreachable ();
6707 x_8 = ASSERT_EXPR <x_7, ...>;
6708 if the only uses of x_7 are in the ASSERT_EXPR and
6709 in the condition. In that case, we can copy the
6710 range info from x_8 computed in this pass also
6711 for x_7. */
6712 if (is_unreachable
6713 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6714 single_pred (bb)))
6715 {
6716 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6717 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6718 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6719 maybe_set_nonzero_bits (bb, var);
6720 }
6721 }
6722
6723 /* Propagate the RHS into every use of the LHS. */
6724 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6725 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6726 SET_USE (use_p, var);
6727
6728 /* And finally, remove the copy, it is not needed. */
6729 gsi_remove (&si, true);
6730 release_defs (stmt);
6731 }
6732 else
6733 {
6734 if (!is_gimple_debug (gsi_stmt (si)))
6735 is_unreachable = 0;
6736 gsi_next (&si);
6737 }
6738 }
6739 }
6740
6741
6742 /* Return true if STMT is interesting for VRP. */
6743
6744 static bool
6745 stmt_interesting_for_vrp (gimple stmt)
6746 {
6747 if (gimple_code (stmt) == GIMPLE_PHI)
6748 {
6749 tree res = gimple_phi_result (stmt);
6750 return (!virtual_operand_p (res)
6751 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6752 || POINTER_TYPE_P (TREE_TYPE (res))));
6753 }
6754 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6755 {
6756 tree lhs = gimple_get_lhs (stmt);
6757
6758 /* In general, assignments with virtual operands are not useful
6759 for deriving ranges, with the obvious exception of calls to
6760 builtin functions. */
6761 if (lhs && TREE_CODE (lhs) == SSA_NAME
6762 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6763 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6764 && (is_gimple_call (stmt)
6765 || !gimple_vuse (stmt)))
6766 return true;
6767 }
6768 else if (gimple_code (stmt) == GIMPLE_COND
6769 || gimple_code (stmt) == GIMPLE_SWITCH)
6770 return true;
6771
6772 return false;
6773 }
6774
6775
6776 /* Initialize local data structures for VRP. */
6777
6778 static void
6779 vrp_initialize (void)
6780 {
6781 basic_block bb;
6782
6783 values_propagated = false;
6784 num_vr_values = num_ssa_names;
6785 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6786 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6787
6788 FOR_EACH_BB_FN (bb, cfun)
6789 {
6790 gimple_stmt_iterator si;
6791
6792 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
6793 {
6794 gimple phi = gsi_stmt (si);
6795 if (!stmt_interesting_for_vrp (phi))
6796 {
6797 tree lhs = PHI_RESULT (phi);
6798 set_value_range_to_varying (get_value_range (lhs));
6799 prop_set_simulate_again (phi, false);
6800 }
6801 else
6802 prop_set_simulate_again (phi, true);
6803 }
6804
6805 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6806 {
6807 gimple stmt = gsi_stmt (si);
6808
6809 /* If the statement is a control insn, then we do not
6810 want to avoid simulating the statement once. Failure
6811 to do so means that those edges will never get added. */
6812 if (stmt_ends_bb_p (stmt))
6813 prop_set_simulate_again (stmt, true);
6814 else if (!stmt_interesting_for_vrp (stmt))
6815 {
6816 ssa_op_iter i;
6817 tree def;
6818 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6819 set_value_range_to_varying (get_value_range (def));
6820 prop_set_simulate_again (stmt, false);
6821 }
6822 else
6823 prop_set_simulate_again (stmt, true);
6824 }
6825 }
6826 }
6827
6828 /* Return the singleton value-range for NAME or NAME. */
6829
6830 static inline tree
6831 vrp_valueize (tree name)
6832 {
6833 if (TREE_CODE (name) == SSA_NAME)
6834 {
6835 value_range_t *vr = get_value_range (name);
6836 if (vr->type == VR_RANGE
6837 && (vr->min == vr->max
6838 || operand_equal_p (vr->min, vr->max, 0)))
6839 return vr->min;
6840 }
6841 return name;
6842 }
6843
6844 /* Visit assignment STMT. If it produces an interesting range, record
6845 the SSA name in *OUTPUT_P. */
6846
6847 static enum ssa_prop_result
6848 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
6849 {
6850 tree def, lhs;
6851 ssa_op_iter iter;
6852 enum gimple_code code = gimple_code (stmt);
6853 lhs = gimple_get_lhs (stmt);
6854
6855 /* We only keep track of ranges in integral and pointer types. */
6856 if (TREE_CODE (lhs) == SSA_NAME
6857 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6858 /* It is valid to have NULL MIN/MAX values on a type. See
6859 build_range_type. */
6860 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6861 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6862 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6863 {
6864 value_range_t new_vr = VR_INITIALIZER;
6865
6866 /* Try folding the statement to a constant first. */
6867 tree tem = gimple_fold_stmt_to_constant (stmt, vrp_valueize);
6868 if (tem)
6869 set_value_range_to_value (&new_vr, tem, NULL);
6870 /* Then dispatch to value-range extracting functions. */
6871 else if (code == GIMPLE_CALL)
6872 extract_range_basic (&new_vr, stmt);
6873 else
6874 extract_range_from_assignment (&new_vr, stmt);
6875
6876 if (update_value_range (lhs, &new_vr))
6877 {
6878 *output_p = lhs;
6879
6880 if (dump_file && (dump_flags & TDF_DETAILS))
6881 {
6882 fprintf (dump_file, "Found new range for ");
6883 print_generic_expr (dump_file, lhs, 0);
6884 fprintf (dump_file, ": ");
6885 dump_value_range (dump_file, &new_vr);
6886 fprintf (dump_file, "\n");
6887 }
6888
6889 if (new_vr.type == VR_VARYING)
6890 return SSA_PROP_VARYING;
6891
6892 return SSA_PROP_INTERESTING;
6893 }
6894
6895 return SSA_PROP_NOT_INTERESTING;
6896 }
6897
6898 /* Every other statement produces no useful ranges. */
6899 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
6900 set_value_range_to_varying (get_value_range (def));
6901
6902 return SSA_PROP_VARYING;
6903 }
6904
6905 /* Helper that gets the value range of the SSA_NAME with version I
6906 or a symbolic range containing the SSA_NAME only if the value range
6907 is varying or undefined. */
6908
6909 static inline value_range_t
6910 get_vr_for_comparison (int i)
6911 {
6912 value_range_t vr = *get_value_range (ssa_name (i));
6913
6914 /* If name N_i does not have a valid range, use N_i as its own
6915 range. This allows us to compare against names that may
6916 have N_i in their ranges. */
6917 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
6918 {
6919 vr.type = VR_RANGE;
6920 vr.min = ssa_name (i);
6921 vr.max = ssa_name (i);
6922 }
6923
6924 return vr;
6925 }
6926
6927 /* Compare all the value ranges for names equivalent to VAR with VAL
6928 using comparison code COMP. Return the same value returned by
6929 compare_range_with_value, including the setting of
6930 *STRICT_OVERFLOW_P. */
6931
6932 static tree
6933 compare_name_with_value (enum tree_code comp, tree var, tree val,
6934 bool *strict_overflow_p)
6935 {
6936 bitmap_iterator bi;
6937 unsigned i;
6938 bitmap e;
6939 tree retval, t;
6940 int used_strict_overflow;
6941 bool sop;
6942 value_range_t equiv_vr;
6943
6944 /* Get the set of equivalences for VAR. */
6945 e = get_value_range (var)->equiv;
6946
6947 /* Start at -1. Set it to 0 if we do a comparison without relying
6948 on overflow, or 1 if all comparisons rely on overflow. */
6949 used_strict_overflow = -1;
6950
6951 /* Compare vars' value range with val. */
6952 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
6953 sop = false;
6954 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
6955 if (retval)
6956 used_strict_overflow = sop ? 1 : 0;
6957
6958 /* If the equiv set is empty we have done all work we need to do. */
6959 if (e == NULL)
6960 {
6961 if (retval
6962 && used_strict_overflow > 0)
6963 *strict_overflow_p = true;
6964 return retval;
6965 }
6966
6967 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
6968 {
6969 equiv_vr = get_vr_for_comparison (i);
6970 sop = false;
6971 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
6972 if (t)
6973 {
6974 /* If we get different answers from different members
6975 of the equivalence set this check must be in a dead
6976 code region. Folding it to a trap representation
6977 would be correct here. For now just return don't-know. */
6978 if (retval != NULL
6979 && t != retval)
6980 {
6981 retval = NULL_TREE;
6982 break;
6983 }
6984 retval = t;
6985
6986 if (!sop)
6987 used_strict_overflow = 0;
6988 else if (used_strict_overflow < 0)
6989 used_strict_overflow = 1;
6990 }
6991 }
6992
6993 if (retval
6994 && used_strict_overflow > 0)
6995 *strict_overflow_p = true;
6996
6997 return retval;
6998 }
6999
7000
7001 /* Given a comparison code COMP and names N1 and N2, compare all the
7002 ranges equivalent to N1 against all the ranges equivalent to N2
7003 to determine the value of N1 COMP N2. Return the same value
7004 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7005 whether we relied on an overflow infinity in the comparison. */
7006
7007
7008 static tree
7009 compare_names (enum tree_code comp, tree n1, tree n2,
7010 bool *strict_overflow_p)
7011 {
7012 tree t, retval;
7013 bitmap e1, e2;
7014 bitmap_iterator bi1, bi2;
7015 unsigned i1, i2;
7016 int used_strict_overflow;
7017 static bitmap_obstack *s_obstack = NULL;
7018 static bitmap s_e1 = NULL, s_e2 = NULL;
7019
7020 /* Compare the ranges of every name equivalent to N1 against the
7021 ranges of every name equivalent to N2. */
7022 e1 = get_value_range (n1)->equiv;
7023 e2 = get_value_range (n2)->equiv;
7024
7025 /* Use the fake bitmaps if e1 or e2 are not available. */
7026 if (s_obstack == NULL)
7027 {
7028 s_obstack = XNEW (bitmap_obstack);
7029 bitmap_obstack_initialize (s_obstack);
7030 s_e1 = BITMAP_ALLOC (s_obstack);
7031 s_e2 = BITMAP_ALLOC (s_obstack);
7032 }
7033 if (e1 == NULL)
7034 e1 = s_e1;
7035 if (e2 == NULL)
7036 e2 = s_e2;
7037
7038 /* Add N1 and N2 to their own set of equivalences to avoid
7039 duplicating the body of the loop just to check N1 and N2
7040 ranges. */
7041 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7042 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7043
7044 /* If the equivalence sets have a common intersection, then the two
7045 names can be compared without checking their ranges. */
7046 if (bitmap_intersect_p (e1, e2))
7047 {
7048 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7049 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7050
7051 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7052 ? boolean_true_node
7053 : boolean_false_node;
7054 }
7055
7056 /* Start at -1. Set it to 0 if we do a comparison without relying
7057 on overflow, or 1 if all comparisons rely on overflow. */
7058 used_strict_overflow = -1;
7059
7060 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7061 N2 to their own set of equivalences to avoid duplicating the body
7062 of the loop just to check N1 and N2 ranges. */
7063 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7064 {
7065 value_range_t vr1 = get_vr_for_comparison (i1);
7066
7067 t = retval = NULL_TREE;
7068 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7069 {
7070 bool sop = false;
7071
7072 value_range_t vr2 = get_vr_for_comparison (i2);
7073
7074 t = compare_ranges (comp, &vr1, &vr2, &sop);
7075 if (t)
7076 {
7077 /* If we get different answers from different members
7078 of the equivalence set this check must be in a dead
7079 code region. Folding it to a trap representation
7080 would be correct here. For now just return don't-know. */
7081 if (retval != NULL
7082 && t != retval)
7083 {
7084 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7085 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7086 return NULL_TREE;
7087 }
7088 retval = t;
7089
7090 if (!sop)
7091 used_strict_overflow = 0;
7092 else if (used_strict_overflow < 0)
7093 used_strict_overflow = 1;
7094 }
7095 }
7096
7097 if (retval)
7098 {
7099 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7100 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7101 if (used_strict_overflow > 0)
7102 *strict_overflow_p = true;
7103 return retval;
7104 }
7105 }
7106
7107 /* None of the equivalent ranges are useful in computing this
7108 comparison. */
7109 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7110 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7111 return NULL_TREE;
7112 }
7113
7114 /* Helper function for vrp_evaluate_conditional_warnv. */
7115
7116 static tree
7117 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7118 tree op0, tree op1,
7119 bool * strict_overflow_p)
7120 {
7121 value_range_t *vr0, *vr1;
7122
7123 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7124 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7125
7126 tree res = NULL_TREE;
7127 if (vr0 && vr1)
7128 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7129 if (!res && vr0)
7130 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7131 if (!res && vr1)
7132 res = (compare_range_with_value
7133 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7134 return res;
7135 }
7136
7137 /* Helper function for vrp_evaluate_conditional_warnv. */
7138
7139 static tree
7140 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7141 tree op1, bool use_equiv_p,
7142 bool *strict_overflow_p, bool *only_ranges)
7143 {
7144 tree ret;
7145 if (only_ranges)
7146 *only_ranges = true;
7147
7148 /* We only deal with integral and pointer types. */
7149 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7150 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7151 return NULL_TREE;
7152
7153 if (use_equiv_p)
7154 {
7155 if (only_ranges
7156 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7157 (code, op0, op1, strict_overflow_p)))
7158 return ret;
7159 *only_ranges = false;
7160 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7161 return compare_names (code, op0, op1, strict_overflow_p);
7162 else if (TREE_CODE (op0) == SSA_NAME)
7163 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7164 else if (TREE_CODE (op1) == SSA_NAME)
7165 return (compare_name_with_value
7166 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7167 }
7168 else
7169 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7170 strict_overflow_p);
7171 return NULL_TREE;
7172 }
7173
7174 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7175 information. Return NULL if the conditional can not be evaluated.
7176 The ranges of all the names equivalent with the operands in COND
7177 will be used when trying to compute the value. If the result is
7178 based on undefined signed overflow, issue a warning if
7179 appropriate. */
7180
7181 static tree
7182 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7183 {
7184 bool sop;
7185 tree ret;
7186 bool only_ranges;
7187
7188 /* Some passes and foldings leak constants with overflow flag set
7189 into the IL. Avoid doing wrong things with these and bail out. */
7190 if ((TREE_CODE (op0) == INTEGER_CST
7191 && TREE_OVERFLOW (op0))
7192 || (TREE_CODE (op1) == INTEGER_CST
7193 && TREE_OVERFLOW (op1)))
7194 return NULL_TREE;
7195
7196 sop = false;
7197 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7198 &only_ranges);
7199
7200 if (ret && sop)
7201 {
7202 enum warn_strict_overflow_code wc;
7203 const char* warnmsg;
7204
7205 if (is_gimple_min_invariant (ret))
7206 {
7207 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7208 warnmsg = G_("assuming signed overflow does not occur when "
7209 "simplifying conditional to constant");
7210 }
7211 else
7212 {
7213 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7214 warnmsg = G_("assuming signed overflow does not occur when "
7215 "simplifying conditional");
7216 }
7217
7218 if (issue_strict_overflow_warning (wc))
7219 {
7220 location_t location;
7221
7222 if (!gimple_has_location (stmt))
7223 location = input_location;
7224 else
7225 location = gimple_location (stmt);
7226 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7227 }
7228 }
7229
7230 if (warn_type_limits
7231 && ret && only_ranges
7232 && TREE_CODE_CLASS (code) == tcc_comparison
7233 && TREE_CODE (op0) == SSA_NAME)
7234 {
7235 /* If the comparison is being folded and the operand on the LHS
7236 is being compared against a constant value that is outside of
7237 the natural range of OP0's type, then the predicate will
7238 always fold regardless of the value of OP0. If -Wtype-limits
7239 was specified, emit a warning. */
7240 tree type = TREE_TYPE (op0);
7241 value_range_t *vr0 = get_value_range (op0);
7242
7243 if (vr0->type != VR_VARYING
7244 && INTEGRAL_TYPE_P (type)
7245 && vrp_val_is_min (vr0->min)
7246 && vrp_val_is_max (vr0->max)
7247 && is_gimple_min_invariant (op1))
7248 {
7249 location_t location;
7250
7251 if (!gimple_has_location (stmt))
7252 location = input_location;
7253 else
7254 location = gimple_location (stmt);
7255
7256 warning_at (location, OPT_Wtype_limits,
7257 integer_zerop (ret)
7258 ? G_("comparison always false "
7259 "due to limited range of data type")
7260 : G_("comparison always true "
7261 "due to limited range of data type"));
7262 }
7263 }
7264
7265 return ret;
7266 }
7267
7268
7269 /* Visit conditional statement STMT. If we can determine which edge
7270 will be taken out of STMT's basic block, record it in
7271 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7272 SSA_PROP_VARYING. */
7273
7274 static enum ssa_prop_result
7275 vrp_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
7276 {
7277 tree val;
7278 bool sop;
7279
7280 *taken_edge_p = NULL;
7281
7282 if (dump_file && (dump_flags & TDF_DETAILS))
7283 {
7284 tree use;
7285 ssa_op_iter i;
7286
7287 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7288 print_gimple_stmt (dump_file, stmt, 0, 0);
7289 fprintf (dump_file, "\nWith known ranges\n");
7290
7291 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7292 {
7293 fprintf (dump_file, "\t");
7294 print_generic_expr (dump_file, use, 0);
7295 fprintf (dump_file, ": ");
7296 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7297 }
7298
7299 fprintf (dump_file, "\n");
7300 }
7301
7302 /* Compute the value of the predicate COND by checking the known
7303 ranges of each of its operands.
7304
7305 Note that we cannot evaluate all the equivalent ranges here
7306 because those ranges may not yet be final and with the current
7307 propagation strategy, we cannot determine when the value ranges
7308 of the names in the equivalence set have changed.
7309
7310 For instance, given the following code fragment
7311
7312 i_5 = PHI <8, i_13>
7313 ...
7314 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7315 if (i_14 == 1)
7316 ...
7317
7318 Assume that on the first visit to i_14, i_5 has the temporary
7319 range [8, 8] because the second argument to the PHI function is
7320 not yet executable. We derive the range ~[0, 0] for i_14 and the
7321 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7322 the first time, since i_14 is equivalent to the range [8, 8], we
7323 determine that the predicate is always false.
7324
7325 On the next round of propagation, i_13 is determined to be
7326 VARYING, which causes i_5 to drop down to VARYING. So, another
7327 visit to i_14 is scheduled. In this second visit, we compute the
7328 exact same range and equivalence set for i_14, namely ~[0, 0] and
7329 { i_5 }. But we did not have the previous range for i_5
7330 registered, so vrp_visit_assignment thinks that the range for
7331 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7332 is not visited again, which stops propagation from visiting
7333 statements in the THEN clause of that if().
7334
7335 To properly fix this we would need to keep the previous range
7336 value for the names in the equivalence set. This way we would've
7337 discovered that from one visit to the other i_5 changed from
7338 range [8, 8] to VR_VARYING.
7339
7340 However, fixing this apparent limitation may not be worth the
7341 additional checking. Testing on several code bases (GCC, DLV,
7342 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7343 4 more predicates folded in SPEC. */
7344 sop = false;
7345
7346 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7347 gimple_cond_lhs (stmt),
7348 gimple_cond_rhs (stmt),
7349 false, &sop, NULL);
7350 if (val)
7351 {
7352 if (!sop)
7353 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7354 else
7355 {
7356 if (dump_file && (dump_flags & TDF_DETAILS))
7357 fprintf (dump_file,
7358 "\nIgnoring predicate evaluation because "
7359 "it assumes that signed overflow is undefined");
7360 val = NULL_TREE;
7361 }
7362 }
7363
7364 if (dump_file && (dump_flags & TDF_DETAILS))
7365 {
7366 fprintf (dump_file, "\nPredicate evaluates to: ");
7367 if (val == NULL_TREE)
7368 fprintf (dump_file, "DON'T KNOW\n");
7369 else
7370 print_generic_stmt (dump_file, val, 0);
7371 }
7372
7373 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7374 }
7375
7376 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7377 that includes the value VAL. The search is restricted to the range
7378 [START_IDX, n - 1] where n is the size of VEC.
7379
7380 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7381 returned.
7382
7383 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7384 it is placed in IDX and false is returned.
7385
7386 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7387 returned. */
7388
7389 static bool
7390 find_case_label_index (gimple stmt, size_t start_idx, tree val, size_t *idx)
7391 {
7392 size_t n = gimple_switch_num_labels (stmt);
7393 size_t low, high;
7394
7395 /* Find case label for minimum of the value range or the next one.
7396 At each iteration we are searching in [low, high - 1]. */
7397
7398 for (low = start_idx, high = n; high != low; )
7399 {
7400 tree t;
7401 int cmp;
7402 /* Note that i != high, so we never ask for n. */
7403 size_t i = (high + low) / 2;
7404 t = gimple_switch_label (stmt, i);
7405
7406 /* Cache the result of comparing CASE_LOW and val. */
7407 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7408
7409 if (cmp == 0)
7410 {
7411 /* Ranges cannot be empty. */
7412 *idx = i;
7413 return true;
7414 }
7415 else if (cmp > 0)
7416 high = i;
7417 else
7418 {
7419 low = i + 1;
7420 if (CASE_HIGH (t) != NULL
7421 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7422 {
7423 *idx = i;
7424 return true;
7425 }
7426 }
7427 }
7428
7429 *idx = high;
7430 return false;
7431 }
7432
7433 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7434 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7435 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7436 then MAX_IDX < MIN_IDX.
7437 Returns true if the default label is not needed. */
7438
7439 static bool
7440 find_case_label_range (gimple stmt, tree min, tree max, size_t *min_idx,
7441 size_t *max_idx)
7442 {
7443 size_t i, j;
7444 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7445 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7446
7447 if (i == j
7448 && min_take_default
7449 && max_take_default)
7450 {
7451 /* Only the default case label reached.
7452 Return an empty range. */
7453 *min_idx = 1;
7454 *max_idx = 0;
7455 return false;
7456 }
7457 else
7458 {
7459 bool take_default = min_take_default || max_take_default;
7460 tree low, high;
7461 size_t k;
7462
7463 if (max_take_default)
7464 j--;
7465
7466 /* If the case label range is continuous, we do not need
7467 the default case label. Verify that. */
7468 high = CASE_LOW (gimple_switch_label (stmt, i));
7469 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7470 high = CASE_HIGH (gimple_switch_label (stmt, i));
7471 for (k = i + 1; k <= j; ++k)
7472 {
7473 low = CASE_LOW (gimple_switch_label (stmt, k));
7474 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7475 {
7476 take_default = true;
7477 break;
7478 }
7479 high = low;
7480 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7481 high = CASE_HIGH (gimple_switch_label (stmt, k));
7482 }
7483
7484 *min_idx = i;
7485 *max_idx = j;
7486 return !take_default;
7487 }
7488 }
7489
7490 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7491 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7492 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7493 Returns true if the default label is not needed. */
7494
7495 static bool
7496 find_case_label_ranges (gimple stmt, value_range_t *vr, size_t *min_idx1,
7497 size_t *max_idx1, size_t *min_idx2,
7498 size_t *max_idx2)
7499 {
7500 size_t i, j, k, l;
7501 unsigned int n = gimple_switch_num_labels (stmt);
7502 bool take_default;
7503 tree case_low, case_high;
7504 tree min = vr->min, max = vr->max;
7505
7506 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7507
7508 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7509
7510 /* Set second range to emtpy. */
7511 *min_idx2 = 1;
7512 *max_idx2 = 0;
7513
7514 if (vr->type == VR_RANGE)
7515 {
7516 *min_idx1 = i;
7517 *max_idx1 = j;
7518 return !take_default;
7519 }
7520
7521 /* Set first range to all case labels. */
7522 *min_idx1 = 1;
7523 *max_idx1 = n - 1;
7524
7525 if (i > j)
7526 return false;
7527
7528 /* Make sure all the values of case labels [i , j] are contained in
7529 range [MIN, MAX]. */
7530 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7531 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7532 if (tree_int_cst_compare (case_low, min) < 0)
7533 i += 1;
7534 if (case_high != NULL_TREE
7535 && tree_int_cst_compare (max, case_high) < 0)
7536 j -= 1;
7537
7538 if (i > j)
7539 return false;
7540
7541 /* If the range spans case labels [i, j], the corresponding anti-range spans
7542 the labels [1, i - 1] and [j + 1, n - 1]. */
7543 k = j + 1;
7544 l = n - 1;
7545 if (k > l)
7546 {
7547 k = 1;
7548 l = 0;
7549 }
7550
7551 j = i - 1;
7552 i = 1;
7553 if (i > j)
7554 {
7555 i = k;
7556 j = l;
7557 k = 1;
7558 l = 0;
7559 }
7560
7561 *min_idx1 = i;
7562 *max_idx1 = j;
7563 *min_idx2 = k;
7564 *max_idx2 = l;
7565 return false;
7566 }
7567
7568 /* Visit switch statement STMT. If we can determine which edge
7569 will be taken out of STMT's basic block, record it in
7570 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7571 SSA_PROP_VARYING. */
7572
7573 static enum ssa_prop_result
7574 vrp_visit_switch_stmt (gimple stmt, edge *taken_edge_p)
7575 {
7576 tree op, val;
7577 value_range_t *vr;
7578 size_t i = 0, j = 0, k, l;
7579 bool take_default;
7580
7581 *taken_edge_p = NULL;
7582 op = gimple_switch_index (stmt);
7583 if (TREE_CODE (op) != SSA_NAME)
7584 return SSA_PROP_VARYING;
7585
7586 vr = get_value_range (op);
7587 if (dump_file && (dump_flags & TDF_DETAILS))
7588 {
7589 fprintf (dump_file, "\nVisiting switch expression with operand ");
7590 print_generic_expr (dump_file, op, 0);
7591 fprintf (dump_file, " with known range ");
7592 dump_value_range (dump_file, vr);
7593 fprintf (dump_file, "\n");
7594 }
7595
7596 if ((vr->type != VR_RANGE
7597 && vr->type != VR_ANTI_RANGE)
7598 || symbolic_range_p (vr))
7599 return SSA_PROP_VARYING;
7600
7601 /* Find the single edge that is taken from the switch expression. */
7602 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7603
7604 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7605 label */
7606 if (j < i)
7607 {
7608 gcc_assert (take_default);
7609 val = gimple_switch_default_label (stmt);
7610 }
7611 else
7612 {
7613 /* Check if labels with index i to j and maybe the default label
7614 are all reaching the same label. */
7615
7616 val = gimple_switch_label (stmt, i);
7617 if (take_default
7618 && CASE_LABEL (gimple_switch_default_label (stmt))
7619 != CASE_LABEL (val))
7620 {
7621 if (dump_file && (dump_flags & TDF_DETAILS))
7622 fprintf (dump_file, " not a single destination for this "
7623 "range\n");
7624 return SSA_PROP_VARYING;
7625 }
7626 for (++i; i <= j; ++i)
7627 {
7628 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7629 {
7630 if (dump_file && (dump_flags & TDF_DETAILS))
7631 fprintf (dump_file, " not a single destination for this "
7632 "range\n");
7633 return SSA_PROP_VARYING;
7634 }
7635 }
7636 for (; k <= l; ++k)
7637 {
7638 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7639 {
7640 if (dump_file && (dump_flags & TDF_DETAILS))
7641 fprintf (dump_file, " not a single destination for this "
7642 "range\n");
7643 return SSA_PROP_VARYING;
7644 }
7645 }
7646 }
7647
7648 *taken_edge_p = find_edge (gimple_bb (stmt),
7649 label_to_block (CASE_LABEL (val)));
7650
7651 if (dump_file && (dump_flags & TDF_DETAILS))
7652 {
7653 fprintf (dump_file, " will take edge to ");
7654 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7655 }
7656
7657 return SSA_PROP_INTERESTING;
7658 }
7659
7660
7661 /* Evaluate statement STMT. If the statement produces a useful range,
7662 return SSA_PROP_INTERESTING and record the SSA name with the
7663 interesting range into *OUTPUT_P.
7664
7665 If STMT is a conditional branch and we can determine its truth
7666 value, the taken edge is recorded in *TAKEN_EDGE_P.
7667
7668 If STMT produces a varying value, return SSA_PROP_VARYING. */
7669
7670 static enum ssa_prop_result
7671 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7672 {
7673 tree def;
7674 ssa_op_iter iter;
7675
7676 if (dump_file && (dump_flags & TDF_DETAILS))
7677 {
7678 fprintf (dump_file, "\nVisiting statement:\n");
7679 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7680 }
7681
7682 if (!stmt_interesting_for_vrp (stmt))
7683 gcc_assert (stmt_ends_bb_p (stmt));
7684 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7685 return vrp_visit_assignment_or_call (stmt, output_p);
7686 else if (gimple_code (stmt) == GIMPLE_COND)
7687 return vrp_visit_cond_stmt (stmt, taken_edge_p);
7688 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7689 return vrp_visit_switch_stmt (stmt, taken_edge_p);
7690
7691 /* All other statements produce nothing of interest for VRP, so mark
7692 their outputs varying and prevent further simulation. */
7693 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7694 set_value_range_to_varying (get_value_range (def));
7695
7696 return SSA_PROP_VARYING;
7697 }
7698
7699 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7700 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7701 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7702 possible such range. The resulting range is not canonicalized. */
7703
7704 static void
7705 union_ranges (enum value_range_type *vr0type,
7706 tree *vr0min, tree *vr0max,
7707 enum value_range_type vr1type,
7708 tree vr1min, tree vr1max)
7709 {
7710 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7711 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7712
7713 /* [] is vr0, () is vr1 in the following classification comments. */
7714 if (mineq && maxeq)
7715 {
7716 /* [( )] */
7717 if (*vr0type == vr1type)
7718 /* Nothing to do for equal ranges. */
7719 ;
7720 else if ((*vr0type == VR_RANGE
7721 && vr1type == VR_ANTI_RANGE)
7722 || (*vr0type == VR_ANTI_RANGE
7723 && vr1type == VR_RANGE))
7724 {
7725 /* For anti-range with range union the result is varying. */
7726 goto give_up;
7727 }
7728 else
7729 gcc_unreachable ();
7730 }
7731 else if (operand_less_p (*vr0max, vr1min) == 1
7732 || operand_less_p (vr1max, *vr0min) == 1)
7733 {
7734 /* [ ] ( ) or ( ) [ ]
7735 If the ranges have an empty intersection, result of the union
7736 operation is the anti-range or if both are anti-ranges
7737 it covers all. */
7738 if (*vr0type == VR_ANTI_RANGE
7739 && vr1type == VR_ANTI_RANGE)
7740 goto give_up;
7741 else if (*vr0type == VR_ANTI_RANGE
7742 && vr1type == VR_RANGE)
7743 ;
7744 else if (*vr0type == VR_RANGE
7745 && vr1type == VR_ANTI_RANGE)
7746 {
7747 *vr0type = vr1type;
7748 *vr0min = vr1min;
7749 *vr0max = vr1max;
7750 }
7751 else if (*vr0type == VR_RANGE
7752 && vr1type == VR_RANGE)
7753 {
7754 /* The result is the convex hull of both ranges. */
7755 if (operand_less_p (*vr0max, vr1min) == 1)
7756 {
7757 /* If the result can be an anti-range, create one. */
7758 if (TREE_CODE (*vr0max) == INTEGER_CST
7759 && TREE_CODE (vr1min) == INTEGER_CST
7760 && vrp_val_is_min (*vr0min)
7761 && vrp_val_is_max (vr1max))
7762 {
7763 tree min = int_const_binop (PLUS_EXPR,
7764 *vr0max,
7765 build_int_cst (TREE_TYPE (*vr0max), 1));
7766 tree max = int_const_binop (MINUS_EXPR,
7767 vr1min,
7768 build_int_cst (TREE_TYPE (vr1min), 1));
7769 if (!operand_less_p (max, min))
7770 {
7771 *vr0type = VR_ANTI_RANGE;
7772 *vr0min = min;
7773 *vr0max = max;
7774 }
7775 else
7776 *vr0max = vr1max;
7777 }
7778 else
7779 *vr0max = vr1max;
7780 }
7781 else
7782 {
7783 /* If the result can be an anti-range, create one. */
7784 if (TREE_CODE (vr1max) == INTEGER_CST
7785 && TREE_CODE (*vr0min) == INTEGER_CST
7786 && vrp_val_is_min (vr1min)
7787 && vrp_val_is_max (*vr0max))
7788 {
7789 tree min = int_const_binop (PLUS_EXPR,
7790 vr1max,
7791 build_int_cst (TREE_TYPE (vr1max), 1));
7792 tree max = int_const_binop (MINUS_EXPR,
7793 *vr0min,
7794 build_int_cst (TREE_TYPE (*vr0min), 1));
7795 if (!operand_less_p (max, min))
7796 {
7797 *vr0type = VR_ANTI_RANGE;
7798 *vr0min = min;
7799 *vr0max = max;
7800 }
7801 else
7802 *vr0min = vr1min;
7803 }
7804 else
7805 *vr0min = vr1min;
7806 }
7807 }
7808 else
7809 gcc_unreachable ();
7810 }
7811 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
7812 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
7813 {
7814 /* [ ( ) ] or [( ) ] or [ ( )] */
7815 if (*vr0type == VR_RANGE
7816 && vr1type == VR_RANGE)
7817 ;
7818 else if (*vr0type == VR_ANTI_RANGE
7819 && vr1type == VR_ANTI_RANGE)
7820 {
7821 *vr0type = vr1type;
7822 *vr0min = vr1min;
7823 *vr0max = vr1max;
7824 }
7825 else if (*vr0type == VR_ANTI_RANGE
7826 && vr1type == VR_RANGE)
7827 {
7828 /* Arbitrarily choose the right or left gap. */
7829 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
7830 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7831 build_int_cst (TREE_TYPE (vr1min), 1));
7832 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
7833 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7834 build_int_cst (TREE_TYPE (vr1max), 1));
7835 else
7836 goto give_up;
7837 }
7838 else if (*vr0type == VR_RANGE
7839 && vr1type == VR_ANTI_RANGE)
7840 /* The result covers everything. */
7841 goto give_up;
7842 else
7843 gcc_unreachable ();
7844 }
7845 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
7846 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
7847 {
7848 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7849 if (*vr0type == VR_RANGE
7850 && vr1type == VR_RANGE)
7851 {
7852 *vr0type = vr1type;
7853 *vr0min = vr1min;
7854 *vr0max = vr1max;
7855 }
7856 else if (*vr0type == VR_ANTI_RANGE
7857 && vr1type == VR_ANTI_RANGE)
7858 ;
7859 else if (*vr0type == VR_RANGE
7860 && vr1type == VR_ANTI_RANGE)
7861 {
7862 *vr0type = VR_ANTI_RANGE;
7863 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
7864 {
7865 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7866 build_int_cst (TREE_TYPE (*vr0min), 1));
7867 *vr0min = vr1min;
7868 }
7869 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
7870 {
7871 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7872 build_int_cst (TREE_TYPE (*vr0max), 1));
7873 *vr0max = vr1max;
7874 }
7875 else
7876 goto give_up;
7877 }
7878 else if (*vr0type == VR_ANTI_RANGE
7879 && vr1type == VR_RANGE)
7880 /* The result covers everything. */
7881 goto give_up;
7882 else
7883 gcc_unreachable ();
7884 }
7885 else if ((operand_less_p (vr1min, *vr0max) == 1
7886 || operand_equal_p (vr1min, *vr0max, 0))
7887 && operand_less_p (*vr0min, vr1min) == 1
7888 && operand_less_p (*vr0max, vr1max) == 1)
7889 {
7890 /* [ ( ] ) or [ ]( ) */
7891 if (*vr0type == VR_RANGE
7892 && vr1type == VR_RANGE)
7893 *vr0max = vr1max;
7894 else if (*vr0type == VR_ANTI_RANGE
7895 && vr1type == VR_ANTI_RANGE)
7896 *vr0min = vr1min;
7897 else if (*vr0type == VR_ANTI_RANGE
7898 && vr1type == VR_RANGE)
7899 {
7900 if (TREE_CODE (vr1min) == INTEGER_CST)
7901 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
7902 build_int_cst (TREE_TYPE (vr1min), 1));
7903 else
7904 goto give_up;
7905 }
7906 else if (*vr0type == VR_RANGE
7907 && vr1type == VR_ANTI_RANGE)
7908 {
7909 if (TREE_CODE (*vr0max) == INTEGER_CST)
7910 {
7911 *vr0type = vr1type;
7912 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
7913 build_int_cst (TREE_TYPE (*vr0max), 1));
7914 *vr0max = vr1max;
7915 }
7916 else
7917 goto give_up;
7918 }
7919 else
7920 gcc_unreachable ();
7921 }
7922 else if ((operand_less_p (*vr0min, vr1max) == 1
7923 || operand_equal_p (*vr0min, vr1max, 0))
7924 && operand_less_p (vr1min, *vr0min) == 1
7925 && operand_less_p (vr1max, *vr0max) == 1)
7926 {
7927 /* ( [ ) ] or ( )[ ] */
7928 if (*vr0type == VR_RANGE
7929 && vr1type == VR_RANGE)
7930 *vr0min = vr1min;
7931 else if (*vr0type == VR_ANTI_RANGE
7932 && vr1type == VR_ANTI_RANGE)
7933 *vr0max = vr1max;
7934 else if (*vr0type == VR_ANTI_RANGE
7935 && vr1type == VR_RANGE)
7936 {
7937 if (TREE_CODE (vr1max) == INTEGER_CST)
7938 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
7939 build_int_cst (TREE_TYPE (vr1max), 1));
7940 else
7941 goto give_up;
7942 }
7943 else if (*vr0type == VR_RANGE
7944 && vr1type == VR_ANTI_RANGE)
7945 {
7946 if (TREE_CODE (*vr0min) == INTEGER_CST)
7947 {
7948 *vr0type = vr1type;
7949 *vr0min = vr1min;
7950 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
7951 build_int_cst (TREE_TYPE (*vr0min), 1));
7952 }
7953 else
7954 goto give_up;
7955 }
7956 else
7957 gcc_unreachable ();
7958 }
7959 else
7960 goto give_up;
7961
7962 return;
7963
7964 give_up:
7965 *vr0type = VR_VARYING;
7966 *vr0min = NULL_TREE;
7967 *vr0max = NULL_TREE;
7968 }
7969
7970 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7971 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7972 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7973 possible such range. The resulting range is not canonicalized. */
7974
7975 static void
7976 intersect_ranges (enum value_range_type *vr0type,
7977 tree *vr0min, tree *vr0max,
7978 enum value_range_type vr1type,
7979 tree vr1min, tree vr1max)
7980 {
7981 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7982 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7983
7984 /* [] is vr0, () is vr1 in the following classification comments. */
7985 if (mineq && maxeq)
7986 {
7987 /* [( )] */
7988 if (*vr0type == vr1type)
7989 /* Nothing to do for equal ranges. */
7990 ;
7991 else if ((*vr0type == VR_RANGE
7992 && vr1type == VR_ANTI_RANGE)
7993 || (*vr0type == VR_ANTI_RANGE
7994 && vr1type == VR_RANGE))
7995 {
7996 /* For anti-range with range intersection the result is empty. */
7997 *vr0type = VR_UNDEFINED;
7998 *vr0min = NULL_TREE;
7999 *vr0max = NULL_TREE;
8000 }
8001 else
8002 gcc_unreachable ();
8003 }
8004 else if (operand_less_p (*vr0max, vr1min) == 1
8005 || operand_less_p (vr1max, *vr0min) == 1)
8006 {
8007 /* [ ] ( ) or ( ) [ ]
8008 If the ranges have an empty intersection, the result of the
8009 intersect operation is the range for intersecting an
8010 anti-range with a range or empty when intersecting two ranges. */
8011 if (*vr0type == VR_RANGE
8012 && vr1type == VR_ANTI_RANGE)
8013 ;
8014 else if (*vr0type == VR_ANTI_RANGE
8015 && vr1type == VR_RANGE)
8016 {
8017 *vr0type = vr1type;
8018 *vr0min = vr1min;
8019 *vr0max = vr1max;
8020 }
8021 else if (*vr0type == VR_RANGE
8022 && vr1type == VR_RANGE)
8023 {
8024 *vr0type = VR_UNDEFINED;
8025 *vr0min = NULL_TREE;
8026 *vr0max = NULL_TREE;
8027 }
8028 else if (*vr0type == VR_ANTI_RANGE
8029 && vr1type == VR_ANTI_RANGE)
8030 {
8031 /* If the anti-ranges are adjacent to each other merge them. */
8032 if (TREE_CODE (*vr0max) == INTEGER_CST
8033 && TREE_CODE (vr1min) == INTEGER_CST
8034 && operand_less_p (*vr0max, vr1min) == 1
8035 && integer_onep (int_const_binop (MINUS_EXPR,
8036 vr1min, *vr0max)))
8037 *vr0max = vr1max;
8038 else if (TREE_CODE (vr1max) == INTEGER_CST
8039 && TREE_CODE (*vr0min) == INTEGER_CST
8040 && operand_less_p (vr1max, *vr0min) == 1
8041 && integer_onep (int_const_binop (MINUS_EXPR,
8042 *vr0min, vr1max)))
8043 *vr0min = vr1min;
8044 /* Else arbitrarily take VR0. */
8045 }
8046 }
8047 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8048 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8049 {
8050 /* [ ( ) ] or [( ) ] or [ ( )] */
8051 if (*vr0type == VR_RANGE
8052 && vr1type == VR_RANGE)
8053 {
8054 /* If both are ranges the result is the inner one. */
8055 *vr0type = vr1type;
8056 *vr0min = vr1min;
8057 *vr0max = vr1max;
8058 }
8059 else if (*vr0type == VR_RANGE
8060 && vr1type == VR_ANTI_RANGE)
8061 {
8062 /* Choose the right gap if the left one is empty. */
8063 if (mineq)
8064 {
8065 if (TREE_CODE (vr1max) == INTEGER_CST)
8066 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8067 build_int_cst (TREE_TYPE (vr1max), 1));
8068 else
8069 *vr0min = vr1max;
8070 }
8071 /* Choose the left gap if the right one is empty. */
8072 else if (maxeq)
8073 {
8074 if (TREE_CODE (vr1min) == INTEGER_CST)
8075 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8076 build_int_cst (TREE_TYPE (vr1min), 1));
8077 else
8078 *vr0max = vr1min;
8079 }
8080 /* Choose the anti-range if the range is effectively varying. */
8081 else if (vrp_val_is_min (*vr0min)
8082 && vrp_val_is_max (*vr0max))
8083 {
8084 *vr0type = vr1type;
8085 *vr0min = vr1min;
8086 *vr0max = vr1max;
8087 }
8088 /* Else choose the range. */
8089 }
8090 else if (*vr0type == VR_ANTI_RANGE
8091 && vr1type == VR_ANTI_RANGE)
8092 /* If both are anti-ranges the result is the outer one. */
8093 ;
8094 else if (*vr0type == VR_ANTI_RANGE
8095 && vr1type == VR_RANGE)
8096 {
8097 /* The intersection is empty. */
8098 *vr0type = VR_UNDEFINED;
8099 *vr0min = NULL_TREE;
8100 *vr0max = NULL_TREE;
8101 }
8102 else
8103 gcc_unreachable ();
8104 }
8105 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8106 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8107 {
8108 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8109 if (*vr0type == VR_RANGE
8110 && vr1type == VR_RANGE)
8111 /* Choose the inner range. */
8112 ;
8113 else if (*vr0type == VR_ANTI_RANGE
8114 && vr1type == VR_RANGE)
8115 {
8116 /* Choose the right gap if the left is empty. */
8117 if (mineq)
8118 {
8119 *vr0type = VR_RANGE;
8120 if (TREE_CODE (*vr0max) == INTEGER_CST)
8121 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8122 build_int_cst (TREE_TYPE (*vr0max), 1));
8123 else
8124 *vr0min = *vr0max;
8125 *vr0max = vr1max;
8126 }
8127 /* Choose the left gap if the right is empty. */
8128 else if (maxeq)
8129 {
8130 *vr0type = VR_RANGE;
8131 if (TREE_CODE (*vr0min) == INTEGER_CST)
8132 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8133 build_int_cst (TREE_TYPE (*vr0min), 1));
8134 else
8135 *vr0max = *vr0min;
8136 *vr0min = vr1min;
8137 }
8138 /* Choose the anti-range if the range is effectively varying. */
8139 else if (vrp_val_is_min (vr1min)
8140 && vrp_val_is_max (vr1max))
8141 ;
8142 /* Else choose the range. */
8143 else
8144 {
8145 *vr0type = vr1type;
8146 *vr0min = vr1min;
8147 *vr0max = vr1max;
8148 }
8149 }
8150 else if (*vr0type == VR_ANTI_RANGE
8151 && vr1type == VR_ANTI_RANGE)
8152 {
8153 /* If both are anti-ranges the result is the outer one. */
8154 *vr0type = vr1type;
8155 *vr0min = vr1min;
8156 *vr0max = vr1max;
8157 }
8158 else if (vr1type == VR_ANTI_RANGE
8159 && *vr0type == VR_RANGE)
8160 {
8161 /* The intersection is empty. */
8162 *vr0type = VR_UNDEFINED;
8163 *vr0min = NULL_TREE;
8164 *vr0max = NULL_TREE;
8165 }
8166 else
8167 gcc_unreachable ();
8168 }
8169 else if ((operand_less_p (vr1min, *vr0max) == 1
8170 || operand_equal_p (vr1min, *vr0max, 0))
8171 && operand_less_p (*vr0min, vr1min) == 1)
8172 {
8173 /* [ ( ] ) or [ ]( ) */
8174 if (*vr0type == VR_ANTI_RANGE
8175 && vr1type == VR_ANTI_RANGE)
8176 *vr0max = vr1max;
8177 else if (*vr0type == VR_RANGE
8178 && vr1type == VR_RANGE)
8179 *vr0min = vr1min;
8180 else if (*vr0type == VR_RANGE
8181 && vr1type == VR_ANTI_RANGE)
8182 {
8183 if (TREE_CODE (vr1min) == INTEGER_CST)
8184 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8185 build_int_cst (TREE_TYPE (vr1min), 1));
8186 else
8187 *vr0max = vr1min;
8188 }
8189 else if (*vr0type == VR_ANTI_RANGE
8190 && vr1type == VR_RANGE)
8191 {
8192 *vr0type = VR_RANGE;
8193 if (TREE_CODE (*vr0max) == INTEGER_CST)
8194 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8195 build_int_cst (TREE_TYPE (*vr0max), 1));
8196 else
8197 *vr0min = *vr0max;
8198 *vr0max = vr1max;
8199 }
8200 else
8201 gcc_unreachable ();
8202 }
8203 else if ((operand_less_p (*vr0min, vr1max) == 1
8204 || operand_equal_p (*vr0min, vr1max, 0))
8205 && operand_less_p (vr1min, *vr0min) == 1)
8206 {
8207 /* ( [ ) ] or ( )[ ] */
8208 if (*vr0type == VR_ANTI_RANGE
8209 && vr1type == VR_ANTI_RANGE)
8210 *vr0min = vr1min;
8211 else if (*vr0type == VR_RANGE
8212 && vr1type == VR_RANGE)
8213 *vr0max = vr1max;
8214 else if (*vr0type == VR_RANGE
8215 && vr1type == VR_ANTI_RANGE)
8216 {
8217 if (TREE_CODE (vr1max) == INTEGER_CST)
8218 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8219 build_int_cst (TREE_TYPE (vr1max), 1));
8220 else
8221 *vr0min = vr1max;
8222 }
8223 else if (*vr0type == VR_ANTI_RANGE
8224 && vr1type == VR_RANGE)
8225 {
8226 *vr0type = VR_RANGE;
8227 if (TREE_CODE (*vr0min) == INTEGER_CST)
8228 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8229 build_int_cst (TREE_TYPE (*vr0min), 1));
8230 else
8231 *vr0max = *vr0min;
8232 *vr0min = vr1min;
8233 }
8234 else
8235 gcc_unreachable ();
8236 }
8237
8238 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8239 result for the intersection. That's always a conservative
8240 correct estimate. */
8241
8242 return;
8243 }
8244
8245
8246 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8247 in *VR0. This may not be the smallest possible such range. */
8248
8249 static void
8250 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8251 {
8252 value_range_t saved;
8253
8254 /* If either range is VR_VARYING the other one wins. */
8255 if (vr1->type == VR_VARYING)
8256 return;
8257 if (vr0->type == VR_VARYING)
8258 {
8259 copy_value_range (vr0, vr1);
8260 return;
8261 }
8262
8263 /* When either range is VR_UNDEFINED the resulting range is
8264 VR_UNDEFINED, too. */
8265 if (vr0->type == VR_UNDEFINED)
8266 return;
8267 if (vr1->type == VR_UNDEFINED)
8268 {
8269 set_value_range_to_undefined (vr0);
8270 return;
8271 }
8272
8273 /* Save the original vr0 so we can return it as conservative intersection
8274 result when our worker turns things to varying. */
8275 saved = *vr0;
8276 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8277 vr1->type, vr1->min, vr1->max);
8278 /* Make sure to canonicalize the result though as the inversion of a
8279 VR_RANGE can still be a VR_RANGE. */
8280 set_and_canonicalize_value_range (vr0, vr0->type,
8281 vr0->min, vr0->max, vr0->equiv);
8282 /* If that failed, use the saved original VR0. */
8283 if (vr0->type == VR_VARYING)
8284 {
8285 *vr0 = saved;
8286 return;
8287 }
8288 /* If the result is VR_UNDEFINED there is no need to mess with
8289 the equivalencies. */
8290 if (vr0->type == VR_UNDEFINED)
8291 return;
8292
8293 /* The resulting set of equivalences for range intersection is the union of
8294 the two sets. */
8295 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8296 bitmap_ior_into (vr0->equiv, vr1->equiv);
8297 else if (vr1->equiv && !vr0->equiv)
8298 bitmap_copy (vr0->equiv, vr1->equiv);
8299 }
8300
8301 static void
8302 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8303 {
8304 if (dump_file && (dump_flags & TDF_DETAILS))
8305 {
8306 fprintf (dump_file, "Intersecting\n ");
8307 dump_value_range (dump_file, vr0);
8308 fprintf (dump_file, "\nand\n ");
8309 dump_value_range (dump_file, vr1);
8310 fprintf (dump_file, "\n");
8311 }
8312 vrp_intersect_ranges_1 (vr0, vr1);
8313 if (dump_file && (dump_flags & TDF_DETAILS))
8314 {
8315 fprintf (dump_file, "to\n ");
8316 dump_value_range (dump_file, vr0);
8317 fprintf (dump_file, "\n");
8318 }
8319 }
8320
8321 /* Meet operation for value ranges. Given two value ranges VR0 and
8322 VR1, store in VR0 a range that contains both VR0 and VR1. This
8323 may not be the smallest possible such range. */
8324
8325 static void
8326 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8327 {
8328 value_range_t saved;
8329
8330 if (vr0->type == VR_UNDEFINED)
8331 {
8332 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8333 return;
8334 }
8335
8336 if (vr1->type == VR_UNDEFINED)
8337 {
8338 /* VR0 already has the resulting range. */
8339 return;
8340 }
8341
8342 if (vr0->type == VR_VARYING)
8343 {
8344 /* Nothing to do. VR0 already has the resulting range. */
8345 return;
8346 }
8347
8348 if (vr1->type == VR_VARYING)
8349 {
8350 set_value_range_to_varying (vr0);
8351 return;
8352 }
8353
8354 saved = *vr0;
8355 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8356 vr1->type, vr1->min, vr1->max);
8357 if (vr0->type == VR_VARYING)
8358 {
8359 /* Failed to find an efficient meet. Before giving up and setting
8360 the result to VARYING, see if we can at least derive a useful
8361 anti-range. FIXME, all this nonsense about distinguishing
8362 anti-ranges from ranges is necessary because of the odd
8363 semantics of range_includes_zero_p and friends. */
8364 if (((saved.type == VR_RANGE
8365 && range_includes_zero_p (saved.min, saved.max) == 0)
8366 || (saved.type == VR_ANTI_RANGE
8367 && range_includes_zero_p (saved.min, saved.max) == 1))
8368 && ((vr1->type == VR_RANGE
8369 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8370 || (vr1->type == VR_ANTI_RANGE
8371 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8372 {
8373 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8374
8375 /* Since this meet operation did not result from the meeting of
8376 two equivalent names, VR0 cannot have any equivalences. */
8377 if (vr0->equiv)
8378 bitmap_clear (vr0->equiv);
8379 return;
8380 }
8381
8382 set_value_range_to_varying (vr0);
8383 return;
8384 }
8385 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8386 vr0->equiv);
8387 if (vr0->type == VR_VARYING)
8388 return;
8389
8390 /* The resulting set of equivalences is always the intersection of
8391 the two sets. */
8392 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8393 bitmap_and_into (vr0->equiv, vr1->equiv);
8394 else if (vr0->equiv && !vr1->equiv)
8395 bitmap_clear (vr0->equiv);
8396 }
8397
8398 static void
8399 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8400 {
8401 if (dump_file && (dump_flags & TDF_DETAILS))
8402 {
8403 fprintf (dump_file, "Meeting\n ");
8404 dump_value_range (dump_file, vr0);
8405 fprintf (dump_file, "\nand\n ");
8406 dump_value_range (dump_file, vr1);
8407 fprintf (dump_file, "\n");
8408 }
8409 vrp_meet_1 (vr0, vr1);
8410 if (dump_file && (dump_flags & TDF_DETAILS))
8411 {
8412 fprintf (dump_file, "to\n ");
8413 dump_value_range (dump_file, vr0);
8414 fprintf (dump_file, "\n");
8415 }
8416 }
8417
8418
8419 /* Visit all arguments for PHI node PHI that flow through executable
8420 edges. If a valid value range can be derived from all the incoming
8421 value ranges, set a new range for the LHS of PHI. */
8422
8423 static enum ssa_prop_result
8424 vrp_visit_phi_node (gimple phi)
8425 {
8426 size_t i;
8427 tree lhs = PHI_RESULT (phi);
8428 value_range_t *lhs_vr = get_value_range (lhs);
8429 value_range_t vr_result = VR_INITIALIZER;
8430 bool first = true;
8431 int edges, old_edges;
8432 struct loop *l;
8433
8434 if (dump_file && (dump_flags & TDF_DETAILS))
8435 {
8436 fprintf (dump_file, "\nVisiting PHI node: ");
8437 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8438 }
8439
8440 edges = 0;
8441 for (i = 0; i < gimple_phi_num_args (phi); i++)
8442 {
8443 edge e = gimple_phi_arg_edge (phi, i);
8444
8445 if (dump_file && (dump_flags & TDF_DETAILS))
8446 {
8447 fprintf (dump_file,
8448 " Argument #%d (%d -> %d %sexecutable)\n",
8449 (int) i, e->src->index, e->dest->index,
8450 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8451 }
8452
8453 if (e->flags & EDGE_EXECUTABLE)
8454 {
8455 tree arg = PHI_ARG_DEF (phi, i);
8456 value_range_t vr_arg;
8457
8458 ++edges;
8459
8460 if (TREE_CODE (arg) == SSA_NAME)
8461 {
8462 vr_arg = *(get_value_range (arg));
8463 /* Do not allow equivalences or symbolic ranges to leak in from
8464 backedges. That creates invalid equivalencies.
8465 See PR53465 and PR54767. */
8466 if (e->flags & EDGE_DFS_BACK)
8467 {
8468 if (vr_arg.type == VR_RANGE
8469 || vr_arg.type == VR_ANTI_RANGE)
8470 {
8471 vr_arg.equiv = NULL;
8472 if (symbolic_range_p (&vr_arg))
8473 {
8474 vr_arg.type = VR_VARYING;
8475 vr_arg.min = NULL_TREE;
8476 vr_arg.max = NULL_TREE;
8477 }
8478 }
8479 }
8480 else
8481 {
8482 /* If the non-backedge arguments range is VR_VARYING then
8483 we can still try recording a simple equivalence. */
8484 if (vr_arg.type == VR_VARYING)
8485 {
8486 vr_arg.type = VR_RANGE;
8487 vr_arg.min = arg;
8488 vr_arg.max = arg;
8489 vr_arg.equiv = NULL;
8490 }
8491 }
8492 }
8493 else
8494 {
8495 if (TREE_OVERFLOW_P (arg))
8496 arg = drop_tree_overflow (arg);
8497
8498 vr_arg.type = VR_RANGE;
8499 vr_arg.min = arg;
8500 vr_arg.max = arg;
8501 vr_arg.equiv = NULL;
8502 }
8503
8504 if (dump_file && (dump_flags & TDF_DETAILS))
8505 {
8506 fprintf (dump_file, "\t");
8507 print_generic_expr (dump_file, arg, dump_flags);
8508 fprintf (dump_file, ": ");
8509 dump_value_range (dump_file, &vr_arg);
8510 fprintf (dump_file, "\n");
8511 }
8512
8513 if (first)
8514 copy_value_range (&vr_result, &vr_arg);
8515 else
8516 vrp_meet (&vr_result, &vr_arg);
8517 first = false;
8518
8519 if (vr_result.type == VR_VARYING)
8520 break;
8521 }
8522 }
8523
8524 if (vr_result.type == VR_VARYING)
8525 goto varying;
8526 else if (vr_result.type == VR_UNDEFINED)
8527 goto update_range;
8528
8529 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8530 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8531
8532 /* To prevent infinite iterations in the algorithm, derive ranges
8533 when the new value is slightly bigger or smaller than the
8534 previous one. We don't do this if we have seen a new executable
8535 edge; this helps us avoid an overflow infinity for conditionals
8536 which are not in a loop. If the old value-range was VR_UNDEFINED
8537 use the updated range and iterate one more time. */
8538 if (edges > 0
8539 && gimple_phi_num_args (phi) > 1
8540 && edges == old_edges
8541 && lhs_vr->type != VR_UNDEFINED)
8542 {
8543 /* Compare old and new ranges, fall back to varying if the
8544 values are not comparable. */
8545 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8546 if (cmp_min == -2)
8547 goto varying;
8548 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8549 if (cmp_max == -2)
8550 goto varying;
8551
8552 /* For non VR_RANGE or for pointers fall back to varying if
8553 the range changed. */
8554 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8555 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8556 && (cmp_min != 0 || cmp_max != 0))
8557 goto varying;
8558
8559 /* If the new minimum is larger than than the previous one
8560 retain the old value. If the new minimum value is smaller
8561 than the previous one and not -INF go all the way to -INF + 1.
8562 In the first case, to avoid infinite bouncing between different
8563 minimums, and in the other case to avoid iterating millions of
8564 times to reach -INF. Going to -INF + 1 also lets the following
8565 iteration compute whether there will be any overflow, at the
8566 expense of one additional iteration. */
8567 if (cmp_min < 0)
8568 vr_result.min = lhs_vr->min;
8569 else if (cmp_min > 0
8570 && !vrp_val_is_min (vr_result.min))
8571 vr_result.min
8572 = int_const_binop (PLUS_EXPR,
8573 vrp_val_min (TREE_TYPE (vr_result.min)),
8574 build_int_cst (TREE_TYPE (vr_result.min), 1));
8575
8576 /* Similarly for the maximum value. */
8577 if (cmp_max > 0)
8578 vr_result.max = lhs_vr->max;
8579 else if (cmp_max < 0
8580 && !vrp_val_is_max (vr_result.max))
8581 vr_result.max
8582 = int_const_binop (MINUS_EXPR,
8583 vrp_val_max (TREE_TYPE (vr_result.min)),
8584 build_int_cst (TREE_TYPE (vr_result.min), 1));
8585
8586 /* If we dropped either bound to +-INF then if this is a loop
8587 PHI node SCEV may known more about its value-range. */
8588 if ((cmp_min > 0 || cmp_min < 0
8589 || cmp_max < 0 || cmp_max > 0)
8590 && (l = loop_containing_stmt (phi))
8591 && l->header == gimple_bb (phi))
8592 adjust_range_with_scev (&vr_result, l, phi, lhs);
8593
8594 /* If we will end up with a (-INF, +INF) range, set it to
8595 VARYING. Same if the previous max value was invalid for
8596 the type and we end up with vr_result.min > vr_result.max. */
8597 if ((vrp_val_is_max (vr_result.max)
8598 && vrp_val_is_min (vr_result.min))
8599 || compare_values (vr_result.min,
8600 vr_result.max) > 0)
8601 goto varying;
8602 }
8603
8604 /* If the new range is different than the previous value, keep
8605 iterating. */
8606 update_range:
8607 if (update_value_range (lhs, &vr_result))
8608 {
8609 if (dump_file && (dump_flags & TDF_DETAILS))
8610 {
8611 fprintf (dump_file, "Found new range for ");
8612 print_generic_expr (dump_file, lhs, 0);
8613 fprintf (dump_file, ": ");
8614 dump_value_range (dump_file, &vr_result);
8615 fprintf (dump_file, "\n");
8616 }
8617
8618 return SSA_PROP_INTERESTING;
8619 }
8620
8621 /* Nothing changed, don't add outgoing edges. */
8622 return SSA_PROP_NOT_INTERESTING;
8623
8624 /* No match found. Set the LHS to VARYING. */
8625 varying:
8626 set_value_range_to_varying (lhs_vr);
8627 return SSA_PROP_VARYING;
8628 }
8629
8630 /* Simplify boolean operations if the source is known
8631 to be already a boolean. */
8632 static bool
8633 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8634 {
8635 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8636 tree lhs, op0, op1;
8637 bool need_conversion;
8638
8639 /* We handle only !=/== case here. */
8640 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8641
8642 op0 = gimple_assign_rhs1 (stmt);
8643 if (!op_with_boolean_value_range_p (op0))
8644 return false;
8645
8646 op1 = gimple_assign_rhs2 (stmt);
8647 if (!op_with_boolean_value_range_p (op1))
8648 return false;
8649
8650 /* Reduce number of cases to handle to NE_EXPR. As there is no
8651 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8652 if (rhs_code == EQ_EXPR)
8653 {
8654 if (TREE_CODE (op1) == INTEGER_CST)
8655 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8656 build_int_cst (TREE_TYPE (op1), 1));
8657 else
8658 return false;
8659 }
8660
8661 lhs = gimple_assign_lhs (stmt);
8662 need_conversion
8663 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8664
8665 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8666 if (need_conversion
8667 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8668 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8669 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8670 return false;
8671
8672 /* For A != 0 we can substitute A itself. */
8673 if (integer_zerop (op1))
8674 gimple_assign_set_rhs_with_ops (gsi,
8675 need_conversion
8676 ? NOP_EXPR : TREE_CODE (op0),
8677 op0, NULL_TREE);
8678 /* For A != B we substitute A ^ B. Either with conversion. */
8679 else if (need_conversion)
8680 {
8681 tree tem = make_ssa_name (TREE_TYPE (op0), NULL);
8682 gimple newop = gimple_build_assign_with_ops (BIT_XOR_EXPR, tem, op0, op1);
8683 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8684 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem, NULL_TREE);
8685 }
8686 /* Or without. */
8687 else
8688 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8689 update_stmt (gsi_stmt (*gsi));
8690
8691 return true;
8692 }
8693
8694 /* Simplify a division or modulo operator to a right shift or
8695 bitwise and if the first operand is unsigned or is greater
8696 than zero and the second operand is an exact power of two. */
8697
8698 static bool
8699 simplify_div_or_mod_using_ranges (gimple stmt)
8700 {
8701 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8702 tree val = NULL;
8703 tree op0 = gimple_assign_rhs1 (stmt);
8704 tree op1 = gimple_assign_rhs2 (stmt);
8705 value_range_t *vr = get_value_range (gimple_assign_rhs1 (stmt));
8706
8707 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8708 {
8709 val = integer_one_node;
8710 }
8711 else
8712 {
8713 bool sop = false;
8714
8715 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8716
8717 if (val
8718 && sop
8719 && integer_onep (val)
8720 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8721 {
8722 location_t location;
8723
8724 if (!gimple_has_location (stmt))
8725 location = input_location;
8726 else
8727 location = gimple_location (stmt);
8728 warning_at (location, OPT_Wstrict_overflow,
8729 "assuming signed overflow does not occur when "
8730 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8731 }
8732 }
8733
8734 if (val && integer_onep (val))
8735 {
8736 tree t;
8737
8738 if (rhs_code == TRUNC_DIV_EXPR)
8739 {
8740 t = build_int_cst (integer_type_node, tree_log2 (op1));
8741 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8742 gimple_assign_set_rhs1 (stmt, op0);
8743 gimple_assign_set_rhs2 (stmt, t);
8744 }
8745 else
8746 {
8747 t = build_int_cst (TREE_TYPE (op1), 1);
8748 t = int_const_binop (MINUS_EXPR, op1, t);
8749 t = fold_convert (TREE_TYPE (op0), t);
8750
8751 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8752 gimple_assign_set_rhs1 (stmt, op0);
8753 gimple_assign_set_rhs2 (stmt, t);
8754 }
8755
8756 update_stmt (stmt);
8757 return true;
8758 }
8759
8760 return false;
8761 }
8762
8763 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8764 ABS_EXPR. If the operand is <= 0, then simplify the
8765 ABS_EXPR into a NEGATE_EXPR. */
8766
8767 static bool
8768 simplify_abs_using_ranges (gimple stmt)
8769 {
8770 tree val = NULL;
8771 tree op = gimple_assign_rhs1 (stmt);
8772 tree type = TREE_TYPE (op);
8773 value_range_t *vr = get_value_range (op);
8774
8775 if (TYPE_UNSIGNED (type))
8776 {
8777 val = integer_zero_node;
8778 }
8779 else if (vr)
8780 {
8781 bool sop = false;
8782
8783 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
8784 if (!val)
8785 {
8786 sop = false;
8787 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
8788 &sop);
8789
8790 if (val)
8791 {
8792 if (integer_zerop (val))
8793 val = integer_one_node;
8794 else if (integer_onep (val))
8795 val = integer_zero_node;
8796 }
8797 }
8798
8799 if (val
8800 && (integer_onep (val) || integer_zerop (val)))
8801 {
8802 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8803 {
8804 location_t location;
8805
8806 if (!gimple_has_location (stmt))
8807 location = input_location;
8808 else
8809 location = gimple_location (stmt);
8810 warning_at (location, OPT_Wstrict_overflow,
8811 "assuming signed overflow does not occur when "
8812 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8813 }
8814
8815 gimple_assign_set_rhs1 (stmt, op);
8816 if (integer_onep (val))
8817 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
8818 else
8819 gimple_assign_set_rhs_code (stmt, SSA_NAME);
8820 update_stmt (stmt);
8821 return true;
8822 }
8823 }
8824
8825 return false;
8826 }
8827
8828 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8829 If all the bits that are being cleared by & are already
8830 known to be zero from VR, or all the bits that are being
8831 set by | are already known to be one from VR, the bit
8832 operation is redundant. */
8833
8834 static bool
8835 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8836 {
8837 tree op0 = gimple_assign_rhs1 (stmt);
8838 tree op1 = gimple_assign_rhs2 (stmt);
8839 tree op = NULL_TREE;
8840 value_range_t vr0 = VR_INITIALIZER;
8841 value_range_t vr1 = VR_INITIALIZER;
8842 wide_int may_be_nonzero0, may_be_nonzero1;
8843 wide_int must_be_nonzero0, must_be_nonzero1;
8844 wide_int mask;
8845
8846 if (TREE_CODE (op0) == SSA_NAME)
8847 vr0 = *(get_value_range (op0));
8848 else if (is_gimple_min_invariant (op0))
8849 set_value_range_to_value (&vr0, op0, NULL);
8850 else
8851 return false;
8852
8853 if (TREE_CODE (op1) == SSA_NAME)
8854 vr1 = *(get_value_range (op1));
8855 else if (is_gimple_min_invariant (op1))
8856 set_value_range_to_value (&vr1, op1, NULL);
8857 else
8858 return false;
8859
8860 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
8861 &must_be_nonzero0))
8862 return false;
8863 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
8864 &must_be_nonzero1))
8865 return false;
8866
8867 switch (gimple_assign_rhs_code (stmt))
8868 {
8869 case BIT_AND_EXPR:
8870 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8871 if (mask == 0)
8872 {
8873 op = op0;
8874 break;
8875 }
8876 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8877 if (mask == 0)
8878 {
8879 op = op1;
8880 break;
8881 }
8882 break;
8883 case BIT_IOR_EXPR:
8884 mask = may_be_nonzero0.and_not (must_be_nonzero1);
8885 if (mask == 0)
8886 {
8887 op = op1;
8888 break;
8889 }
8890 mask = may_be_nonzero1.and_not (must_be_nonzero0);
8891 if (mask == 0)
8892 {
8893 op = op0;
8894 break;
8895 }
8896 break;
8897 default:
8898 gcc_unreachable ();
8899 }
8900
8901 if (op == NULL_TREE)
8902 return false;
8903
8904 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op, NULL);
8905 update_stmt (gsi_stmt (*gsi));
8906 return true;
8907 }
8908
8909 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8910 a known value range VR.
8911
8912 If there is one and only one value which will satisfy the
8913 conditional, then return that value. Else return NULL. */
8914
8915 static tree
8916 test_for_singularity (enum tree_code cond_code, tree op0,
8917 tree op1, value_range_t *vr)
8918 {
8919 tree min = NULL;
8920 tree max = NULL;
8921
8922 /* Extract minimum/maximum values which satisfy the
8923 the conditional as it was written. */
8924 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
8925 {
8926 /* This should not be negative infinity; there is no overflow
8927 here. */
8928 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
8929
8930 max = op1;
8931 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
8932 {
8933 tree one = build_int_cst (TREE_TYPE (op0), 1);
8934 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
8935 if (EXPR_P (max))
8936 TREE_NO_WARNING (max) = 1;
8937 }
8938 }
8939 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
8940 {
8941 /* This should not be positive infinity; there is no overflow
8942 here. */
8943 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
8944
8945 min = op1;
8946 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
8947 {
8948 tree one = build_int_cst (TREE_TYPE (op0), 1);
8949 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
8950 if (EXPR_P (min))
8951 TREE_NO_WARNING (min) = 1;
8952 }
8953 }
8954
8955 /* Now refine the minimum and maximum values using any
8956 value range information we have for op0. */
8957 if (min && max)
8958 {
8959 if (compare_values (vr->min, min) == 1)
8960 min = vr->min;
8961 if (compare_values (vr->max, max) == -1)
8962 max = vr->max;
8963
8964 /* If the new min/max values have converged to a single value,
8965 then there is only one value which can satisfy the condition,
8966 return that value. */
8967 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
8968 return min;
8969 }
8970 return NULL;
8971 }
8972
8973 /* Return whether the value range *VR fits in an integer type specified
8974 by PRECISION and UNSIGNED_P. */
8975
8976 static bool
8977 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
8978 {
8979 tree src_type;
8980 unsigned src_precision;
8981 widest_int tem;
8982 signop src_sgn;
8983
8984 /* We can only handle integral and pointer types. */
8985 src_type = TREE_TYPE (vr->min);
8986 if (!INTEGRAL_TYPE_P (src_type)
8987 && !POINTER_TYPE_P (src_type))
8988 return false;
8989
8990 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8991 and so is an identity transform. */
8992 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
8993 src_sgn = TYPE_SIGN (src_type);
8994 if ((src_precision < dest_precision
8995 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
8996 || (src_precision == dest_precision && src_sgn == dest_sgn))
8997 return true;
8998
8999 /* Now we can only handle ranges with constant bounds. */
9000 if (vr->type != VR_RANGE
9001 || TREE_CODE (vr->min) != INTEGER_CST
9002 || TREE_CODE (vr->max) != INTEGER_CST)
9003 return false;
9004
9005 /* For sign changes, the MSB of the wide_int has to be clear.
9006 An unsigned value with its MSB set cannot be represented by
9007 a signed wide_int, while a negative value cannot be represented
9008 by an unsigned wide_int. */
9009 if (src_sgn != dest_sgn
9010 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9011 return false;
9012
9013 /* Then we can perform the conversion on both ends and compare
9014 the result for equality. */
9015 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9016 if (tem != wi::to_widest (vr->min))
9017 return false;
9018 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9019 if (tem != wi::to_widest (vr->max))
9020 return false;
9021
9022 return true;
9023 }
9024
9025 /* Simplify a conditional using a relational operator to an equality
9026 test if the range information indicates only one value can satisfy
9027 the original conditional. */
9028
9029 static bool
9030 simplify_cond_using_ranges (gimple stmt)
9031 {
9032 tree op0 = gimple_cond_lhs (stmt);
9033 tree op1 = gimple_cond_rhs (stmt);
9034 enum tree_code cond_code = gimple_cond_code (stmt);
9035
9036 if (cond_code != NE_EXPR
9037 && cond_code != EQ_EXPR
9038 && TREE_CODE (op0) == SSA_NAME
9039 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9040 && is_gimple_min_invariant (op1))
9041 {
9042 value_range_t *vr = get_value_range (op0);
9043
9044 /* If we have range information for OP0, then we might be
9045 able to simplify this conditional. */
9046 if (vr->type == VR_RANGE)
9047 {
9048 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9049
9050 if (new_tree)
9051 {
9052 if (dump_file)
9053 {
9054 fprintf (dump_file, "Simplified relational ");
9055 print_gimple_stmt (dump_file, stmt, 0, 0);
9056 fprintf (dump_file, " into ");
9057 }
9058
9059 gimple_cond_set_code (stmt, EQ_EXPR);
9060 gimple_cond_set_lhs (stmt, op0);
9061 gimple_cond_set_rhs (stmt, new_tree);
9062
9063 update_stmt (stmt);
9064
9065 if (dump_file)
9066 {
9067 print_gimple_stmt (dump_file, stmt, 0, 0);
9068 fprintf (dump_file, "\n");
9069 }
9070
9071 return true;
9072 }
9073
9074 /* Try again after inverting the condition. We only deal
9075 with integral types here, so no need to worry about
9076 issues with inverting FP comparisons. */
9077 cond_code = invert_tree_comparison (cond_code, false);
9078 new_tree = test_for_singularity (cond_code, op0, op1, vr);
9079
9080 if (new_tree)
9081 {
9082 if (dump_file)
9083 {
9084 fprintf (dump_file, "Simplified relational ");
9085 print_gimple_stmt (dump_file, stmt, 0, 0);
9086 fprintf (dump_file, " into ");
9087 }
9088
9089 gimple_cond_set_code (stmt, NE_EXPR);
9090 gimple_cond_set_lhs (stmt, op0);
9091 gimple_cond_set_rhs (stmt, new_tree);
9092
9093 update_stmt (stmt);
9094
9095 if (dump_file)
9096 {
9097 print_gimple_stmt (dump_file, stmt, 0, 0);
9098 fprintf (dump_file, "\n");
9099 }
9100
9101 return true;
9102 }
9103 }
9104 }
9105
9106 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9107 see if OP0 was set by a type conversion where the source of
9108 the conversion is another SSA_NAME with a range that fits
9109 into the range of OP0's type.
9110
9111 If so, the conversion is redundant as the earlier SSA_NAME can be
9112 used for the comparison directly if we just massage the constant in the
9113 comparison. */
9114 if (TREE_CODE (op0) == SSA_NAME
9115 && TREE_CODE (op1) == INTEGER_CST)
9116 {
9117 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9118 tree innerop;
9119
9120 if (!is_gimple_assign (def_stmt)
9121 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9122 return false;
9123
9124 innerop = gimple_assign_rhs1 (def_stmt);
9125
9126 if (TREE_CODE (innerop) == SSA_NAME
9127 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9128 {
9129 value_range_t *vr = get_value_range (innerop);
9130
9131 if (range_int_cst_p (vr)
9132 && range_fits_type_p (vr,
9133 TYPE_PRECISION (TREE_TYPE (op0)),
9134 TYPE_SIGN (TREE_TYPE (op0)))
9135 && int_fits_type_p (op1, TREE_TYPE (innerop))
9136 /* The range must not have overflowed, or if it did overflow
9137 we must not be wrapping/trapping overflow and optimizing
9138 with strict overflow semantics. */
9139 && ((!is_negative_overflow_infinity (vr->min)
9140 && !is_positive_overflow_infinity (vr->max))
9141 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9142 {
9143 /* If the range overflowed and the user has asked for warnings
9144 when strict overflow semantics were used to optimize code,
9145 issue an appropriate warning. */
9146 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9147 && (is_negative_overflow_infinity (vr->min)
9148 || is_positive_overflow_infinity (vr->max))
9149 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9150 {
9151 location_t location;
9152
9153 if (!gimple_has_location (stmt))
9154 location = input_location;
9155 else
9156 location = gimple_location (stmt);
9157 warning_at (location, OPT_Wstrict_overflow,
9158 "assuming signed overflow does not occur when "
9159 "simplifying conditional");
9160 }
9161
9162 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9163 gimple_cond_set_lhs (stmt, innerop);
9164 gimple_cond_set_rhs (stmt, newconst);
9165 return true;
9166 }
9167 }
9168 }
9169
9170 return false;
9171 }
9172
9173 /* Simplify a switch statement using the value range of the switch
9174 argument. */
9175
9176 static bool
9177 simplify_switch_using_ranges (gimple stmt)
9178 {
9179 tree op = gimple_switch_index (stmt);
9180 value_range_t *vr;
9181 bool take_default;
9182 edge e;
9183 edge_iterator ei;
9184 size_t i = 0, j = 0, n, n2;
9185 tree vec2;
9186 switch_update su;
9187 size_t k = 1, l = 0;
9188
9189 if (TREE_CODE (op) == SSA_NAME)
9190 {
9191 vr = get_value_range (op);
9192
9193 /* We can only handle integer ranges. */
9194 if ((vr->type != VR_RANGE
9195 && vr->type != VR_ANTI_RANGE)
9196 || symbolic_range_p (vr))
9197 return false;
9198
9199 /* Find case label for min/max of the value range. */
9200 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9201 }
9202 else if (TREE_CODE (op) == INTEGER_CST)
9203 {
9204 take_default = !find_case_label_index (stmt, 1, op, &i);
9205 if (take_default)
9206 {
9207 i = 1;
9208 j = 0;
9209 }
9210 else
9211 {
9212 j = i;
9213 }
9214 }
9215 else
9216 return false;
9217
9218 n = gimple_switch_num_labels (stmt);
9219
9220 /* Bail out if this is just all edges taken. */
9221 if (i == 1
9222 && j == n - 1
9223 && take_default)
9224 return false;
9225
9226 /* Build a new vector of taken case labels. */
9227 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9228 n2 = 0;
9229
9230 /* Add the default edge, if necessary. */
9231 if (take_default)
9232 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9233
9234 for (; i <= j; ++i, ++n2)
9235 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9236
9237 for (; k <= l; ++k, ++n2)
9238 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9239
9240 /* Mark needed edges. */
9241 for (i = 0; i < n2; ++i)
9242 {
9243 e = find_edge (gimple_bb (stmt),
9244 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9245 e->aux = (void *)-1;
9246 }
9247
9248 /* Queue not needed edges for later removal. */
9249 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9250 {
9251 if (e->aux == (void *)-1)
9252 {
9253 e->aux = NULL;
9254 continue;
9255 }
9256
9257 if (dump_file && (dump_flags & TDF_DETAILS))
9258 {
9259 fprintf (dump_file, "removing unreachable case label\n");
9260 }
9261 to_remove_edges.safe_push (e);
9262 e->flags &= ~EDGE_EXECUTABLE;
9263 }
9264
9265 /* And queue an update for the stmt. */
9266 su.stmt = stmt;
9267 su.vec = vec2;
9268 to_update_switch_stmts.safe_push (su);
9269 return false;
9270 }
9271
9272 /* Simplify an integral conversion from an SSA name in STMT. */
9273
9274 static bool
9275 simplify_conversion_using_ranges (gimple stmt)
9276 {
9277 tree innerop, middleop, finaltype;
9278 gimple def_stmt;
9279 value_range_t *innervr;
9280 signop inner_sgn, middle_sgn, final_sgn;
9281 unsigned inner_prec, middle_prec, final_prec;
9282 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9283
9284 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9285 if (!INTEGRAL_TYPE_P (finaltype))
9286 return false;
9287 middleop = gimple_assign_rhs1 (stmt);
9288 def_stmt = SSA_NAME_DEF_STMT (middleop);
9289 if (!is_gimple_assign (def_stmt)
9290 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9291 return false;
9292 innerop = gimple_assign_rhs1 (def_stmt);
9293 if (TREE_CODE (innerop) != SSA_NAME
9294 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9295 return false;
9296
9297 /* Get the value-range of the inner operand. */
9298 innervr = get_value_range (innerop);
9299 if (innervr->type != VR_RANGE
9300 || TREE_CODE (innervr->min) != INTEGER_CST
9301 || TREE_CODE (innervr->max) != INTEGER_CST)
9302 return false;
9303
9304 /* Simulate the conversion chain to check if the result is equal if
9305 the middle conversion is removed. */
9306 innermin = wi::to_widest (innervr->min);
9307 innermax = wi::to_widest (innervr->max);
9308
9309 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9310 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9311 final_prec = TYPE_PRECISION (finaltype);
9312
9313 /* If the first conversion is not injective, the second must not
9314 be widening. */
9315 if (wi::gtu_p (innermax - innermin,
9316 wi::mask <widest_int> (middle_prec, false))
9317 && middle_prec < final_prec)
9318 return false;
9319 /* We also want a medium value so that we can track the effect that
9320 narrowing conversions with sign change have. */
9321 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9322 if (inner_sgn == UNSIGNED)
9323 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9324 else
9325 innermed = 0;
9326 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9327 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9328 innermed = innermin;
9329
9330 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9331 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9332 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9333 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9334
9335 /* Require that the final conversion applied to both the original
9336 and the intermediate range produces the same result. */
9337 final_sgn = TYPE_SIGN (finaltype);
9338 if (wi::ext (middlemin, final_prec, final_sgn)
9339 != wi::ext (innermin, final_prec, final_sgn)
9340 || wi::ext (middlemed, final_prec, final_sgn)
9341 != wi::ext (innermed, final_prec, final_sgn)
9342 || wi::ext (middlemax, final_prec, final_sgn)
9343 != wi::ext (innermax, final_prec, final_sgn))
9344 return false;
9345
9346 gimple_assign_set_rhs1 (stmt, innerop);
9347 update_stmt (stmt);
9348 return true;
9349 }
9350
9351 /* Simplify a conversion from integral SSA name to float in STMT. */
9352
9353 static bool
9354 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9355 {
9356 tree rhs1 = gimple_assign_rhs1 (stmt);
9357 value_range_t *vr = get_value_range (rhs1);
9358 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9359 machine_mode mode;
9360 tree tem;
9361 gimple conv;
9362
9363 /* We can only handle constant ranges. */
9364 if (vr->type != VR_RANGE
9365 || TREE_CODE (vr->min) != INTEGER_CST
9366 || TREE_CODE (vr->max) != INTEGER_CST)
9367 return false;
9368
9369 /* First check if we can use a signed type in place of an unsigned. */
9370 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9371 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9372 != CODE_FOR_nothing)
9373 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9374 mode = TYPE_MODE (TREE_TYPE (rhs1));
9375 /* If we can do the conversion in the current input mode do nothing. */
9376 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9377 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9378 return false;
9379 /* Otherwise search for a mode we can use, starting from the narrowest
9380 integer mode available. */
9381 else
9382 {
9383 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9384 do
9385 {
9386 /* If we cannot do a signed conversion to float from mode
9387 or if the value-range does not fit in the signed type
9388 try with a wider mode. */
9389 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9390 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9391 break;
9392
9393 mode = GET_MODE_WIDER_MODE (mode);
9394 /* But do not widen the input. Instead leave that to the
9395 optabs expansion code. */
9396 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9397 return false;
9398 }
9399 while (mode != VOIDmode);
9400 if (mode == VOIDmode)
9401 return false;
9402 }
9403
9404 /* It works, insert a truncation or sign-change before the
9405 float conversion. */
9406 tem = make_ssa_name (build_nonstandard_integer_type
9407 (GET_MODE_PRECISION (mode), 0), NULL);
9408 conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
9409 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9410 gimple_assign_set_rhs1 (stmt, tem);
9411 update_stmt (stmt);
9412
9413 return true;
9414 }
9415
9416 /* Simplify an internal fn call using ranges if possible. */
9417
9418 static bool
9419 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9420 {
9421 enum tree_code subcode;
9422 switch (gimple_call_internal_fn (stmt))
9423 {
9424 case IFN_UBSAN_CHECK_ADD:
9425 subcode = PLUS_EXPR;
9426 break;
9427 case IFN_UBSAN_CHECK_SUB:
9428 subcode = MINUS_EXPR;
9429 break;
9430 case IFN_UBSAN_CHECK_MUL:
9431 subcode = MULT_EXPR;
9432 break;
9433 default:
9434 return false;
9435 }
9436
9437 value_range_t vr0 = VR_INITIALIZER;
9438 value_range_t vr1 = VR_INITIALIZER;
9439 tree op0 = gimple_call_arg (stmt, 0);
9440 tree op1 = gimple_call_arg (stmt, 1);
9441
9442 if (TREE_CODE (op0) == SSA_NAME)
9443 vr0 = *get_value_range (op0);
9444 else if (TREE_CODE (op0) == INTEGER_CST)
9445 set_value_range_to_value (&vr0, op0, NULL);
9446 else
9447 set_value_range_to_varying (&vr0);
9448
9449 if (TREE_CODE (op1) == SSA_NAME)
9450 vr1 = *get_value_range (op1);
9451 else if (TREE_CODE (op1) == INTEGER_CST)
9452 set_value_range_to_value (&vr1, op1, NULL);
9453 else
9454 set_value_range_to_varying (&vr1);
9455
9456 if (!range_int_cst_p (&vr0))
9457 {
9458 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9459 optimize at least x = y + 0; x = y - 0; x = y * 0;
9460 and x = y * 1; which never overflow. */
9461 if (!range_int_cst_p (&vr1))
9462 return false;
9463 if (tree_int_cst_sgn (vr1.min) == -1)
9464 return false;
9465 if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1)
9466 return false;
9467 }
9468 else if (!range_int_cst_p (&vr1))
9469 {
9470 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9471 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9472 which never overflow. */
9473 if (subcode == MINUS_EXPR)
9474 return false;
9475 if (!range_int_cst_p (&vr0))
9476 return false;
9477 if (tree_int_cst_sgn (vr0.min) == -1)
9478 return false;
9479 if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1)
9480 return false;
9481 }
9482 else
9483 {
9484 tree r1 = int_const_binop (subcode, vr0.min,
9485 subcode == MINUS_EXPR ? vr1.max : vr1.min);
9486 tree r2 = int_const_binop (subcode, vr0.max,
9487 subcode == MINUS_EXPR ? vr1.min : vr1.max);
9488 if (r1 == NULL_TREE || TREE_OVERFLOW (r1)
9489 || r2 == NULL_TREE || TREE_OVERFLOW (r2))
9490 return false;
9491 if (subcode == MULT_EXPR)
9492 {
9493 tree r3 = int_const_binop (subcode, vr0.min, vr1.max);
9494 tree r4 = int_const_binop (subcode, vr0.max, vr1.min);
9495 if (r3 == NULL_TREE || TREE_OVERFLOW (r3)
9496 || r4 == NULL_TREE || TREE_OVERFLOW (r4))
9497 return false;
9498 }
9499 }
9500
9501 gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt),
9502 op0, op1);
9503 gsi_replace (gsi, g, false);
9504 return true;
9505 }
9506
9507 /* Simplify STMT using ranges if possible. */
9508
9509 static bool
9510 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9511 {
9512 gimple stmt = gsi_stmt (*gsi);
9513 if (is_gimple_assign (stmt))
9514 {
9515 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9516 tree rhs1 = gimple_assign_rhs1 (stmt);
9517
9518 switch (rhs_code)
9519 {
9520 case EQ_EXPR:
9521 case NE_EXPR:
9522 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9523 if the RHS is zero or one, and the LHS are known to be boolean
9524 values. */
9525 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9526 return simplify_truth_ops_using_ranges (gsi, stmt);
9527 break;
9528
9529 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9530 and BIT_AND_EXPR respectively if the first operand is greater
9531 than zero and the second operand is an exact power of two. */
9532 case TRUNC_DIV_EXPR:
9533 case TRUNC_MOD_EXPR:
9534 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
9535 && integer_pow2p (gimple_assign_rhs2 (stmt)))
9536 return simplify_div_or_mod_using_ranges (stmt);
9537 break;
9538
9539 /* Transform ABS (X) into X or -X as appropriate. */
9540 case ABS_EXPR:
9541 if (TREE_CODE (rhs1) == SSA_NAME
9542 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9543 return simplify_abs_using_ranges (stmt);
9544 break;
9545
9546 case BIT_AND_EXPR:
9547 case BIT_IOR_EXPR:
9548 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9549 if all the bits being cleared are already cleared or
9550 all the bits being set are already set. */
9551 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9552 return simplify_bit_ops_using_ranges (gsi, stmt);
9553 break;
9554
9555 CASE_CONVERT:
9556 if (TREE_CODE (rhs1) == SSA_NAME
9557 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9558 return simplify_conversion_using_ranges (stmt);
9559 break;
9560
9561 case FLOAT_EXPR:
9562 if (TREE_CODE (rhs1) == SSA_NAME
9563 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9564 return simplify_float_conversion_using_ranges (gsi, stmt);
9565 break;
9566
9567 default:
9568 break;
9569 }
9570 }
9571 else if (gimple_code (stmt) == GIMPLE_COND)
9572 return simplify_cond_using_ranges (stmt);
9573 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9574 return simplify_switch_using_ranges (stmt);
9575 else if (is_gimple_call (stmt)
9576 && gimple_call_internal_p (stmt))
9577 return simplify_internal_call_using_ranges (gsi, stmt);
9578
9579 return false;
9580 }
9581
9582 /* If the statement pointed by SI has a predicate whose value can be
9583 computed using the value range information computed by VRP, compute
9584 its value and return true. Otherwise, return false. */
9585
9586 static bool
9587 fold_predicate_in (gimple_stmt_iterator *si)
9588 {
9589 bool assignment_p = false;
9590 tree val;
9591 gimple stmt = gsi_stmt (*si);
9592
9593 if (is_gimple_assign (stmt)
9594 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9595 {
9596 assignment_p = true;
9597 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9598 gimple_assign_rhs1 (stmt),
9599 gimple_assign_rhs2 (stmt),
9600 stmt);
9601 }
9602 else if (gimple_code (stmt) == GIMPLE_COND)
9603 val = vrp_evaluate_conditional (gimple_cond_code (stmt),
9604 gimple_cond_lhs (stmt),
9605 gimple_cond_rhs (stmt),
9606 stmt);
9607 else
9608 return false;
9609
9610 if (val)
9611 {
9612 if (assignment_p)
9613 val = fold_convert (gimple_expr_type (stmt), val);
9614
9615 if (dump_file)
9616 {
9617 fprintf (dump_file, "Folding predicate ");
9618 print_gimple_expr (dump_file, stmt, 0, 0);
9619 fprintf (dump_file, " to ");
9620 print_generic_expr (dump_file, val, 0);
9621 fprintf (dump_file, "\n");
9622 }
9623
9624 if (is_gimple_assign (stmt))
9625 gimple_assign_set_rhs_from_tree (si, val);
9626 else
9627 {
9628 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9629 if (integer_zerop (val))
9630 gimple_cond_make_false (stmt);
9631 else if (integer_onep (val))
9632 gimple_cond_make_true (stmt);
9633 else
9634 gcc_unreachable ();
9635 }
9636
9637 return true;
9638 }
9639
9640 return false;
9641 }
9642
9643 /* Callback for substitute_and_fold folding the stmt at *SI. */
9644
9645 static bool
9646 vrp_fold_stmt (gimple_stmt_iterator *si)
9647 {
9648 if (fold_predicate_in (si))
9649 return true;
9650
9651 return simplify_stmt_using_ranges (si);
9652 }
9653
9654 /* Stack of dest,src equivalency pairs that need to be restored after
9655 each attempt to thread a block's incoming edge to an outgoing edge.
9656
9657 A NULL entry is used to mark the end of pairs which need to be
9658 restored. */
9659 static vec<tree> equiv_stack;
9660
9661 /* A trivial wrapper so that we can present the generic jump threading
9662 code with a simple API for simplifying statements. STMT is the
9663 statement we want to simplify, WITHIN_STMT provides the location
9664 for any overflow warnings. */
9665
9666 static tree
9667 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
9668 {
9669 if (gimple_code (stmt) == GIMPLE_COND)
9670 return vrp_evaluate_conditional (gimple_cond_code (stmt),
9671 gimple_cond_lhs (stmt),
9672 gimple_cond_rhs (stmt), within_stmt);
9673
9674 if (gimple_code (stmt) == GIMPLE_ASSIGN)
9675 {
9676 value_range_t new_vr = VR_INITIALIZER;
9677 tree lhs = gimple_assign_lhs (stmt);
9678
9679 if (TREE_CODE (lhs) == SSA_NAME
9680 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
9681 || POINTER_TYPE_P (TREE_TYPE (lhs))))
9682 {
9683 extract_range_from_assignment (&new_vr, stmt);
9684 if (range_int_cst_singleton_p (&new_vr))
9685 return new_vr.min;
9686 }
9687 }
9688
9689 return NULL_TREE;
9690 }
9691
9692 /* Blocks which have more than one predecessor and more than
9693 one successor present jump threading opportunities, i.e.,
9694 when the block is reached from a specific predecessor, we
9695 may be able to determine which of the outgoing edges will
9696 be traversed. When this optimization applies, we are able
9697 to avoid conditionals at runtime and we may expose secondary
9698 optimization opportunities.
9699
9700 This routine is effectively a driver for the generic jump
9701 threading code. It basically just presents the generic code
9702 with edges that may be suitable for jump threading.
9703
9704 Unlike DOM, we do not iterate VRP if jump threading was successful.
9705 While iterating may expose new opportunities for VRP, it is expected
9706 those opportunities would be very limited and the compile time cost
9707 to expose those opportunities would be significant.
9708
9709 As jump threading opportunities are discovered, they are registered
9710 for later realization. */
9711
9712 static void
9713 identify_jump_threads (void)
9714 {
9715 basic_block bb;
9716 gimple dummy;
9717 int i;
9718 edge e;
9719
9720 /* Ugh. When substituting values earlier in this pass we can
9721 wipe the dominance information. So rebuild the dominator
9722 information as we need it within the jump threading code. */
9723 calculate_dominance_info (CDI_DOMINATORS);
9724
9725 /* We do not allow VRP information to be used for jump threading
9726 across a back edge in the CFG. Otherwise it becomes too
9727 difficult to avoid eliminating loop exit tests. Of course
9728 EDGE_DFS_BACK is not accurate at this time so we have to
9729 recompute it. */
9730 mark_dfs_back_edges ();
9731
9732 /* Do not thread across edges we are about to remove. Just marking
9733 them as EDGE_DFS_BACK will do. */
9734 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9735 e->flags |= EDGE_DFS_BACK;
9736
9737 /* Allocate our unwinder stack to unwind any temporary equivalences
9738 that might be recorded. */
9739 equiv_stack.create (20);
9740
9741 /* To avoid lots of silly node creation, we create a single
9742 conditional and just modify it in-place when attempting to
9743 thread jumps. */
9744 dummy = gimple_build_cond (EQ_EXPR,
9745 integer_zero_node, integer_zero_node,
9746 NULL, NULL);
9747
9748 /* Walk through all the blocks finding those which present a
9749 potential jump threading opportunity. We could set this up
9750 as a dominator walker and record data during the walk, but
9751 I doubt it's worth the effort for the classes of jump
9752 threading opportunities we are trying to identify at this
9753 point in compilation. */
9754 FOR_EACH_BB_FN (bb, cfun)
9755 {
9756 gimple last;
9757
9758 /* If the generic jump threading code does not find this block
9759 interesting, then there is nothing to do. */
9760 if (! potentially_threadable_block (bb))
9761 continue;
9762
9763 /* We only care about blocks ending in a COND_EXPR. While there
9764 may be some value in handling SWITCH_EXPR here, I doubt it's
9765 terribly important. */
9766 last = gsi_stmt (gsi_last_bb (bb));
9767
9768 /* We're basically looking for a switch or any kind of conditional with
9769 integral or pointer type arguments. Note the type of the second
9770 argument will be the same as the first argument, so no need to
9771 check it explicitly. */
9772 if (gimple_code (last) == GIMPLE_SWITCH
9773 || (gimple_code (last) == GIMPLE_COND
9774 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
9775 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
9776 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
9777 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
9778 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
9779 {
9780 edge_iterator ei;
9781
9782 /* We've got a block with multiple predecessors and multiple
9783 successors which also ends in a suitable conditional or
9784 switch statement. For each predecessor, see if we can thread
9785 it to a specific successor. */
9786 FOR_EACH_EDGE (e, ei, bb->preds)
9787 {
9788 /* Do not thread across back edges or abnormal edges
9789 in the CFG. */
9790 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
9791 continue;
9792
9793 thread_across_edge (dummy, e, true, &equiv_stack,
9794 simplify_stmt_for_jump_threading);
9795 }
9796 }
9797 }
9798
9799 /* We do not actually update the CFG or SSA graphs at this point as
9800 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9801 handle ASSERT_EXPRs gracefully. */
9802 }
9803
9804 /* We identified all the jump threading opportunities earlier, but could
9805 not transform the CFG at that time. This routine transforms the
9806 CFG and arranges for the dominator tree to be rebuilt if necessary.
9807
9808 Note the SSA graph update will occur during the normal TODO
9809 processing by the pass manager. */
9810 static void
9811 finalize_jump_threads (void)
9812 {
9813 thread_through_all_blocks (false);
9814 equiv_stack.release ();
9815 }
9816
9817
9818 /* Traverse all the blocks folding conditionals with known ranges. */
9819
9820 static void
9821 vrp_finalize (void)
9822 {
9823 size_t i;
9824
9825 values_propagated = true;
9826
9827 if (dump_file)
9828 {
9829 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
9830 dump_all_value_ranges (dump_file);
9831 fprintf (dump_file, "\n");
9832 }
9833
9834 substitute_and_fold (op_with_constant_singleton_value_range,
9835 vrp_fold_stmt, false);
9836
9837 if (warn_array_bounds)
9838 check_all_array_refs ();
9839
9840 /* We must identify jump threading opportunities before we release
9841 the datastructures built by VRP. */
9842 identify_jump_threads ();
9843
9844 /* Set value range to non pointer SSA_NAMEs. */
9845 for (i = 0; i < num_vr_values; i++)
9846 if (vr_value[i])
9847 {
9848 tree name = ssa_name (i);
9849
9850 if (!name
9851 || POINTER_TYPE_P (TREE_TYPE (name))
9852 || (vr_value[i]->type == VR_VARYING)
9853 || (vr_value[i]->type == VR_UNDEFINED))
9854 continue;
9855
9856 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
9857 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
9858 && (vr_value[i]->type == VR_RANGE
9859 || vr_value[i]->type == VR_ANTI_RANGE))
9860 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
9861 vr_value[i]->max);
9862 }
9863
9864 /* Free allocated memory. */
9865 for (i = 0; i < num_vr_values; i++)
9866 if (vr_value[i])
9867 {
9868 BITMAP_FREE (vr_value[i]->equiv);
9869 free (vr_value[i]);
9870 }
9871
9872 free (vr_value);
9873 free (vr_phi_edge_counts);
9874
9875 /* So that we can distinguish between VRP data being available
9876 and not available. */
9877 vr_value = NULL;
9878 vr_phi_edge_counts = NULL;
9879 }
9880
9881
9882 /* Main entry point to VRP (Value Range Propagation). This pass is
9883 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9884 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9885 Programming Language Design and Implementation, pp. 67-78, 1995.
9886 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9887
9888 This is essentially an SSA-CCP pass modified to deal with ranges
9889 instead of constants.
9890
9891 While propagating ranges, we may find that two or more SSA name
9892 have equivalent, though distinct ranges. For instance,
9893
9894 1 x_9 = p_3->a;
9895 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9896 3 if (p_4 == q_2)
9897 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9898 5 endif
9899 6 if (q_2)
9900
9901 In the code above, pointer p_5 has range [q_2, q_2], but from the
9902 code we can also determine that p_5 cannot be NULL and, if q_2 had
9903 a non-varying range, p_5's range should also be compatible with it.
9904
9905 These equivalences are created by two expressions: ASSERT_EXPR and
9906 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9907 result of another assertion, then we can use the fact that p_5 and
9908 p_4 are equivalent when evaluating p_5's range.
9909
9910 Together with value ranges, we also propagate these equivalences
9911 between names so that we can take advantage of information from
9912 multiple ranges when doing final replacement. Note that this
9913 equivalency relation is transitive but not symmetric.
9914
9915 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9916 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9917 in contexts where that assertion does not hold (e.g., in line 6).
9918
9919 TODO, the main difference between this pass and Patterson's is that
9920 we do not propagate edge probabilities. We only compute whether
9921 edges can be taken or not. That is, instead of having a spectrum
9922 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9923 DON'T KNOW. In the future, it may be worthwhile to propagate
9924 probabilities to aid branch prediction. */
9925
9926 static unsigned int
9927 execute_vrp (void)
9928 {
9929 int i;
9930 edge e;
9931 switch_update *su;
9932
9933 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
9934 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
9935 scev_initialize ();
9936
9937 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9938 Inserting assertions may split edges which will invalidate
9939 EDGE_DFS_BACK. */
9940 insert_range_assertions ();
9941
9942 to_remove_edges.create (10);
9943 to_update_switch_stmts.create (5);
9944 threadedge_initialize_values ();
9945
9946 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9947 mark_dfs_back_edges ();
9948
9949 vrp_initialize ();
9950 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
9951 vrp_finalize ();
9952
9953 free_numbers_of_iterations_estimates ();
9954
9955 /* ASSERT_EXPRs must be removed before finalizing jump threads
9956 as finalizing jump threads calls the CFG cleanup code which
9957 does not properly handle ASSERT_EXPRs. */
9958 remove_range_assertions ();
9959
9960 /* If we exposed any new variables, go ahead and put them into
9961 SSA form now, before we handle jump threading. This simplifies
9962 interactions between rewriting of _DECL nodes into SSA form
9963 and rewriting SSA_NAME nodes into SSA form after block
9964 duplication and CFG manipulation. */
9965 update_ssa (TODO_update_ssa);
9966
9967 finalize_jump_threads ();
9968
9969 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9970 CFG in a broken state and requires a cfg_cleanup run. */
9971 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
9972 remove_edge (e);
9973 /* Update SWITCH_EXPR case label vector. */
9974 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
9975 {
9976 size_t j;
9977 size_t n = TREE_VEC_LENGTH (su->vec);
9978 tree label;
9979 gimple_switch_set_num_labels (su->stmt, n);
9980 for (j = 0; j < n; j++)
9981 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
9982 /* As we may have replaced the default label with a regular one
9983 make sure to make it a real default label again. This ensures
9984 optimal expansion. */
9985 label = gimple_switch_label (su->stmt, 0);
9986 CASE_LOW (label) = NULL_TREE;
9987 CASE_HIGH (label) = NULL_TREE;
9988 }
9989
9990 if (to_remove_edges.length () > 0)
9991 {
9992 free_dominance_info (CDI_DOMINATORS);
9993 loops_state_set (LOOPS_NEED_FIXUP);
9994 }
9995
9996 to_remove_edges.release ();
9997 to_update_switch_stmts.release ();
9998 threadedge_finalize_values ();
9999
10000 scev_finalize ();
10001 loop_optimizer_finalize ();
10002 return 0;
10003 }
10004
10005 namespace {
10006
10007 const pass_data pass_data_vrp =
10008 {
10009 GIMPLE_PASS, /* type */
10010 "vrp", /* name */
10011 OPTGROUP_NONE, /* optinfo_flags */
10012 TV_TREE_VRP, /* tv_id */
10013 PROP_ssa, /* properties_required */
10014 0, /* properties_provided */
10015 0, /* properties_destroyed */
10016 0, /* todo_flags_start */
10017 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10018 };
10019
10020 class pass_vrp : public gimple_opt_pass
10021 {
10022 public:
10023 pass_vrp (gcc::context *ctxt)
10024 : gimple_opt_pass (pass_data_vrp, ctxt)
10025 {}
10026
10027 /* opt_pass methods: */
10028 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10029 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10030 virtual unsigned int execute (function *) { return execute_vrp (); }
10031
10032 }; // class pass_vrp
10033
10034 } // anon namespace
10035
10036 gimple_opt_pass *
10037 make_pass_vrp (gcc::context *ctxt)
10038 {
10039 return new pass_vrp (ctxt);
10040 }