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