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