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