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