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