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