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