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