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