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