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