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