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