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