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