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