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