Support for aliasing with variable strides
[gcc.git] / gcc / tree-vrp.c
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "tree-dfa.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-ssa-loop-niter.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
50 #include "tree-ssa.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 "tree-ssa-scopedtables.h"
58 #include "tree-ssa-threadedge.h"
59 #include "omp-general.h"
60 #include "target.h"
61 #include "case-cfn-macros.h"
62 #include "params.h"
63 #include "alloc-pool.h"
64 #include "domwalk.h"
65 #include "tree-cfgcleanup.h"
66 #include "stringpool.h"
67 #include "attribs.h"
68 #include "vr-values.h"
69 #include "builtins.h"
70
71 /* Set of SSA names found live during the RPO traversal of the function
72 for still active basic-blocks. */
73 static sbitmap *live;
74
75 /* Return true if the SSA name NAME is live on the edge E. */
76
77 static bool
78 live_on_edge (edge e, tree name)
79 {
80 return (live[e->dest->index]
81 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
82 }
83
84 /* Location information for ASSERT_EXPRs. Each instance of this
85 structure describes an ASSERT_EXPR for an SSA name. Since a single
86 SSA name may have more than one assertion associated with it, these
87 locations are kept in a linked list attached to the corresponding
88 SSA name. */
89 struct assert_locus
90 {
91 /* Basic block where the assertion would be inserted. */
92 basic_block bb;
93
94 /* Some assertions need to be inserted on an edge (e.g., assertions
95 generated by COND_EXPRs). In those cases, BB will be NULL. */
96 edge e;
97
98 /* Pointer to the statement that generated this assertion. */
99 gimple_stmt_iterator si;
100
101 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
102 enum tree_code comp_code;
103
104 /* Value being compared against. */
105 tree val;
106
107 /* Expression to compare. */
108 tree expr;
109
110 /* Next node in the linked list. */
111 assert_locus *next;
112 };
113
114 /* If bit I is present, it means that SSA name N_i has a list of
115 assertions that should be inserted in the IL. */
116 static bitmap need_assert_for;
117
118 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
119 holds a list of ASSERT_LOCUS_T nodes that describe where
120 ASSERT_EXPRs for SSA name N_I should be inserted. */
121 static assert_locus **asserts_for;
122
123 vec<edge> to_remove_edges;
124 vec<switch_update> to_update_switch_stmts;
125
126
127 /* Return the maximum value for TYPE. */
128
129 tree
130 vrp_val_max (const_tree type)
131 {
132 if (!INTEGRAL_TYPE_P (type))
133 return NULL_TREE;
134
135 return TYPE_MAX_VALUE (type);
136 }
137
138 /* Return the minimum value for TYPE. */
139
140 tree
141 vrp_val_min (const_tree type)
142 {
143 if (!INTEGRAL_TYPE_P (type))
144 return NULL_TREE;
145
146 return TYPE_MIN_VALUE (type);
147 }
148
149 /* Return whether VAL is equal to the maximum value of its type.
150 We can't do a simple equality comparison with TYPE_MAX_VALUE because
151 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
152 is not == to the integer constant with the same value in the type. */
153
154 bool
155 vrp_val_is_max (const_tree val)
156 {
157 tree type_max = vrp_val_max (TREE_TYPE (val));
158 return (val == type_max
159 || (type_max != NULL_TREE
160 && operand_equal_p (val, type_max, 0)));
161 }
162
163 /* Return whether VAL is equal to the minimum value of its type. */
164
165 bool
166 vrp_val_is_min (const_tree val)
167 {
168 tree type_min = vrp_val_min (TREE_TYPE (val));
169 return (val == type_min
170 || (type_min != NULL_TREE
171 && operand_equal_p (val, type_min, 0)));
172 }
173
174
175 /* Set value range VR to VR_UNDEFINED. */
176
177 static inline void
178 set_value_range_to_undefined (value_range *vr)
179 {
180 vr->type = VR_UNDEFINED;
181 vr->min = vr->max = NULL_TREE;
182 if (vr->equiv)
183 bitmap_clear (vr->equiv);
184 }
185
186 /* Set value range VR to VR_VARYING. */
187
188 void
189 set_value_range_to_varying (value_range *vr)
190 {
191 vr->type = VR_VARYING;
192 vr->min = vr->max = NULL_TREE;
193 if (vr->equiv)
194 bitmap_clear (vr->equiv);
195 }
196
197 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
198
199 void
200 set_value_range (value_range *vr, enum value_range_type t, tree min,
201 tree max, bitmap equiv)
202 {
203 /* Check the validity of the range. */
204 if (flag_checking
205 && (t == VR_RANGE || t == VR_ANTI_RANGE))
206 {
207 int cmp;
208
209 gcc_assert (min && max);
210
211 gcc_assert (!TREE_OVERFLOW_P (min) && !TREE_OVERFLOW_P (max));
212
213 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
214 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
215
216 cmp = compare_values (min, max);
217 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
218 }
219
220 if (flag_checking
221 && (t == VR_UNDEFINED || t == VR_VARYING))
222 {
223 gcc_assert (min == NULL_TREE && max == NULL_TREE);
224 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
225 }
226
227 vr->type = t;
228 vr->min = min;
229 vr->max = max;
230
231 /* Since updating the equivalence set involves deep copying the
232 bitmaps, only do it if absolutely necessary.
233
234 All equivalence bitmaps are allocated from the same obstack. So
235 we can use the obstack associated with EQUIV to allocate vr->equiv. */
236 if (vr->equiv == NULL
237 && equiv != NULL)
238 vr->equiv = BITMAP_ALLOC (equiv->obstack);
239
240 if (equiv != vr->equiv)
241 {
242 if (equiv && !bitmap_empty_p (equiv))
243 bitmap_copy (vr->equiv, equiv);
244 else
245 bitmap_clear (vr->equiv);
246 }
247 }
248
249
250 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
251 This means adjusting T, MIN and MAX representing the case of a
252 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
253 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
254 In corner cases where MAX+1 or MIN-1 wraps this will fall back
255 to varying.
256 This routine exists to ease canonicalization in the case where we
257 extract ranges from var + CST op limit. */
258
259 void
260 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
261 tree min, tree max, bitmap equiv)
262 {
263 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
264 if (t == VR_UNDEFINED)
265 {
266 set_value_range_to_undefined (vr);
267 return;
268 }
269 else if (t == VR_VARYING)
270 {
271 set_value_range_to_varying (vr);
272 return;
273 }
274
275 /* Nothing to canonicalize for symbolic ranges. */
276 if (TREE_CODE (min) != INTEGER_CST
277 || TREE_CODE (max) != INTEGER_CST)
278 {
279 set_value_range (vr, t, min, max, equiv);
280 return;
281 }
282
283 /* Wrong order for min and max, to swap them and the VR type we need
284 to adjust them. */
285 if (tree_int_cst_lt (max, min))
286 {
287 tree one, tmp;
288
289 /* For one bit precision if max < min, then the swapped
290 range covers all values, so for VR_RANGE it is varying and
291 for VR_ANTI_RANGE empty range, so drop to varying as well. */
292 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
293 {
294 set_value_range_to_varying (vr);
295 return;
296 }
297
298 one = build_int_cst (TREE_TYPE (min), 1);
299 tmp = int_const_binop (PLUS_EXPR, max, one);
300 max = int_const_binop (MINUS_EXPR, min, one);
301 min = tmp;
302
303 /* There's one corner case, if we had [C+1, C] before we now have
304 that again. But this represents an empty value range, so drop
305 to varying in this case. */
306 if (tree_int_cst_lt (max, min))
307 {
308 set_value_range_to_varying (vr);
309 return;
310 }
311
312 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
313 }
314
315 /* Anti-ranges that can be represented as ranges should be so. */
316 if (t == VR_ANTI_RANGE)
317 {
318 bool is_min = vrp_val_is_min (min);
319 bool is_max = vrp_val_is_max (max);
320
321 if (is_min && is_max)
322 {
323 /* We cannot deal with empty ranges, drop to varying.
324 ??? This could be VR_UNDEFINED instead. */
325 set_value_range_to_varying (vr);
326 return;
327 }
328 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
329 && (is_min || is_max))
330 {
331 /* Non-empty boolean ranges can always be represented
332 as a singleton range. */
333 if (is_min)
334 min = max = vrp_val_max (TREE_TYPE (min));
335 else
336 min = max = vrp_val_min (TREE_TYPE (min));
337 t = VR_RANGE;
338 }
339 else if (is_min
340 /* As a special exception preserve non-null ranges. */
341 && !(TYPE_UNSIGNED (TREE_TYPE (min))
342 && integer_zerop (max)))
343 {
344 tree one = build_int_cst (TREE_TYPE (max), 1);
345 min = int_const_binop (PLUS_EXPR, max, one);
346 max = vrp_val_max (TREE_TYPE (max));
347 t = VR_RANGE;
348 }
349 else if (is_max)
350 {
351 tree one = build_int_cst (TREE_TYPE (min), 1);
352 max = int_const_binop (MINUS_EXPR, min, one);
353 min = vrp_val_min (TREE_TYPE (min));
354 t = VR_RANGE;
355 }
356 }
357
358 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
359 to make sure VRP iteration terminates, otherwise we can get into
360 oscillations. */
361
362 set_value_range (vr, t, min, max, equiv);
363 }
364
365 /* Copy value range FROM into value range TO. */
366
367 void
368 copy_value_range (value_range *to, value_range *from)
369 {
370 set_value_range (to, from->type, from->min, from->max, from->equiv);
371 }
372
373 /* Set value range VR to a single value. This function is only called
374 with values we get from statements, and exists to clear the
375 TREE_OVERFLOW flag. */
376
377 void
378 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
379 {
380 gcc_assert (is_gimple_min_invariant (val));
381 if (TREE_OVERFLOW_P (val))
382 val = drop_tree_overflow (val);
383 set_value_range (vr, VR_RANGE, val, val, equiv);
384 }
385
386 /* Set value range VR to a non-NULL range of type TYPE. */
387
388 void
389 set_value_range_to_nonnull (value_range *vr, tree type)
390 {
391 tree zero = build_int_cst (type, 0);
392 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
393 }
394
395
396 /* Set value range VR to a NULL range of type TYPE. */
397
398 void
399 set_value_range_to_null (value_range *vr, tree type)
400 {
401 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
402 }
403
404
405 /* If abs (min) < abs (max), set VR to [-max, max], if
406 abs (min) >= abs (max), set VR to [-min, min]. */
407
408 static void
409 abs_extent_range (value_range *vr, tree min, tree max)
410 {
411 int cmp;
412
413 gcc_assert (TREE_CODE (min) == INTEGER_CST);
414 gcc_assert (TREE_CODE (max) == INTEGER_CST);
415 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
416 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
417 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
418 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
419 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
420 {
421 set_value_range_to_varying (vr);
422 return;
423 }
424 cmp = compare_values (min, max);
425 if (cmp == -1)
426 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
427 else if (cmp == 0 || cmp == 1)
428 {
429 max = min;
430 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
431 }
432 else
433 {
434 set_value_range_to_varying (vr);
435 return;
436 }
437 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
438 }
439
440 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
441
442 bool
443 vrp_operand_equal_p (const_tree val1, const_tree val2)
444 {
445 if (val1 == val2)
446 return true;
447 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
448 return false;
449 return true;
450 }
451
452 /* Return true, if the bitmaps B1 and B2 are equal. */
453
454 bool
455 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
456 {
457 return (b1 == b2
458 || ((!b1 || bitmap_empty_p (b1))
459 && (!b2 || bitmap_empty_p (b2)))
460 || (b1 && b2
461 && bitmap_equal_p (b1, b2)));
462 }
463
464 /* Return true if VR is ~[0, 0]. */
465
466 bool
467 range_is_nonnull (value_range *vr)
468 {
469 return vr->type == VR_ANTI_RANGE
470 && integer_zerop (vr->min)
471 && integer_zerop (vr->max);
472 }
473
474
475 /* Return true if VR is [0, 0]. */
476
477 static inline bool
478 range_is_null (value_range *vr)
479 {
480 return vr->type == VR_RANGE
481 && integer_zerop (vr->min)
482 && integer_zerop (vr->max);
483 }
484
485 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
486 a singleton. */
487
488 bool
489 range_int_cst_p (value_range *vr)
490 {
491 return (vr->type == VR_RANGE
492 && TREE_CODE (vr->max) == INTEGER_CST
493 && TREE_CODE (vr->min) == INTEGER_CST);
494 }
495
496 /* Return true if VR is a INTEGER_CST singleton. */
497
498 bool
499 range_int_cst_singleton_p (value_range *vr)
500 {
501 return (range_int_cst_p (vr)
502 && tree_int_cst_equal (vr->min, vr->max));
503 }
504
505 /* Return true if value range VR involves at least one symbol. */
506
507 bool
508 symbolic_range_p (value_range *vr)
509 {
510 return (!is_gimple_min_invariant (vr->min)
511 || !is_gimple_min_invariant (vr->max));
512 }
513
514 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
515 otherwise. We only handle additive operations and set NEG to true if the
516 symbol is negated and INV to the invariant part, if any. */
517
518 tree
519 get_single_symbol (tree t, bool *neg, tree *inv)
520 {
521 bool neg_;
522 tree inv_;
523
524 *inv = NULL_TREE;
525 *neg = false;
526
527 if (TREE_CODE (t) == PLUS_EXPR
528 || TREE_CODE (t) == POINTER_PLUS_EXPR
529 || TREE_CODE (t) == MINUS_EXPR)
530 {
531 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
532 {
533 neg_ = (TREE_CODE (t) == MINUS_EXPR);
534 inv_ = TREE_OPERAND (t, 0);
535 t = TREE_OPERAND (t, 1);
536 }
537 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
538 {
539 neg_ = false;
540 inv_ = TREE_OPERAND (t, 1);
541 t = TREE_OPERAND (t, 0);
542 }
543 else
544 return NULL_TREE;
545 }
546 else
547 {
548 neg_ = false;
549 inv_ = NULL_TREE;
550 }
551
552 if (TREE_CODE (t) == NEGATE_EXPR)
553 {
554 t = TREE_OPERAND (t, 0);
555 neg_ = !neg_;
556 }
557
558 if (TREE_CODE (t) != SSA_NAME)
559 return NULL_TREE;
560
561 if (inv_ && TREE_OVERFLOW_P (inv_))
562 inv_ = drop_tree_overflow (inv_);
563
564 *neg = neg_;
565 *inv = inv_;
566 return t;
567 }
568
569 /* The reverse operation: build a symbolic expression with TYPE
570 from symbol SYM, negated according to NEG, and invariant INV. */
571
572 static tree
573 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
574 {
575 const bool pointer_p = POINTER_TYPE_P (type);
576 tree t = sym;
577
578 if (neg)
579 t = build1 (NEGATE_EXPR, type, t);
580
581 if (integer_zerop (inv))
582 return t;
583
584 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
585 }
586
587 /* Return
588 1 if VAL < VAL2
589 0 if !(VAL < VAL2)
590 -2 if those are incomparable. */
591 int
592 operand_less_p (tree val, tree val2)
593 {
594 /* LT is folded faster than GE and others. Inline the common case. */
595 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
596 return tree_int_cst_lt (val, val2);
597 else
598 {
599 tree tcmp;
600
601 fold_defer_overflow_warnings ();
602
603 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
604
605 fold_undefer_and_ignore_overflow_warnings ();
606
607 if (!tcmp
608 || TREE_CODE (tcmp) != INTEGER_CST)
609 return -2;
610
611 if (!integer_zerop (tcmp))
612 return 1;
613 }
614
615 return 0;
616 }
617
618 /* Compare two values VAL1 and VAL2. Return
619
620 -2 if VAL1 and VAL2 cannot be compared at compile-time,
621 -1 if VAL1 < VAL2,
622 0 if VAL1 == VAL2,
623 +1 if VAL1 > VAL2, and
624 +2 if VAL1 != VAL2
625
626 This is similar to tree_int_cst_compare but supports pointer values
627 and values that cannot be compared at compile time.
628
629 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
630 true if the return value is only valid if we assume that signed
631 overflow is undefined. */
632
633 int
634 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
635 {
636 if (val1 == val2)
637 return 0;
638
639 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
640 both integers. */
641 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
642 == POINTER_TYPE_P (TREE_TYPE (val2)));
643
644 /* Convert the two values into the same type. This is needed because
645 sizetype causes sign extension even for unsigned types. */
646 val2 = fold_convert (TREE_TYPE (val1), val2);
647 STRIP_USELESS_TYPE_CONVERSION (val2);
648
649 const bool overflow_undefined
650 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
651 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
652 tree inv1, inv2;
653 bool neg1, neg2;
654 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
655 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
656
657 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
658 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
659 if (sym1 && sym2)
660 {
661 /* Both values must use the same name with the same sign. */
662 if (sym1 != sym2 || neg1 != neg2)
663 return -2;
664
665 /* [-]NAME + CST == [-]NAME + CST. */
666 if (inv1 == inv2)
667 return 0;
668
669 /* If overflow is defined we cannot simplify more. */
670 if (!overflow_undefined)
671 return -2;
672
673 if (strict_overflow_p != NULL
674 /* Symbolic range building sets TREE_NO_WARNING to declare
675 that overflow doesn't happen. */
676 && (!inv1 || !TREE_NO_WARNING (val1))
677 && (!inv2 || !TREE_NO_WARNING (val2)))
678 *strict_overflow_p = true;
679
680 if (!inv1)
681 inv1 = build_int_cst (TREE_TYPE (val1), 0);
682 if (!inv2)
683 inv2 = build_int_cst (TREE_TYPE (val2), 0);
684
685 return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
686 TYPE_SIGN (TREE_TYPE (val1)));
687 }
688
689 const bool cst1 = is_gimple_min_invariant (val1);
690 const bool cst2 = is_gimple_min_invariant (val2);
691
692 /* If one is of the form '[-]NAME + CST' and the other is constant, then
693 it might be possible to say something depending on the constants. */
694 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
695 {
696 if (!overflow_undefined)
697 return -2;
698
699 if (strict_overflow_p != NULL
700 /* Symbolic range building sets TREE_NO_WARNING to declare
701 that overflow doesn't happen. */
702 && (!sym1 || !TREE_NO_WARNING (val1))
703 && (!sym2 || !TREE_NO_WARNING (val2)))
704 *strict_overflow_p = true;
705
706 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
707 tree cst = cst1 ? val1 : val2;
708 tree inv = cst1 ? inv2 : inv1;
709
710 /* Compute the difference between the constants. If it overflows or
711 underflows, this means that we can trivially compare the NAME with
712 it and, consequently, the two values with each other. */
713 wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
714 if (wi::cmp (0, wi::to_wide (inv), sgn)
715 != wi::cmp (diff, wi::to_wide (cst), sgn))
716 {
717 const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
718 return cst1 ? res : -res;
719 }
720
721 return -2;
722 }
723
724 /* We cannot say anything more for non-constants. */
725 if (!cst1 || !cst2)
726 return -2;
727
728 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
729 {
730 /* We cannot compare overflowed values. */
731 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
732 return -2;
733
734 if (TREE_CODE (val1) == INTEGER_CST
735 && TREE_CODE (val2) == INTEGER_CST)
736 return tree_int_cst_compare (val1, val2);
737
738 if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
739 {
740 if (known_eq (wi::to_poly_widest (val1),
741 wi::to_poly_widest (val2)))
742 return 0;
743 if (known_lt (wi::to_poly_widest (val1),
744 wi::to_poly_widest (val2)))
745 return -1;
746 if (known_gt (wi::to_poly_widest (val1),
747 wi::to_poly_widest (val2)))
748 return 1;
749 }
750
751 return -2;
752 }
753 else
754 {
755 tree t;
756
757 /* First see if VAL1 and VAL2 are not the same. */
758 if (val1 == val2 || operand_equal_p (val1, val2, 0))
759 return 0;
760
761 /* If VAL1 is a lower address than VAL2, return -1. */
762 if (operand_less_p (val1, val2) == 1)
763 return -1;
764
765 /* If VAL1 is a higher address than VAL2, return +1. */
766 if (operand_less_p (val2, val1) == 1)
767 return 1;
768
769 /* If VAL1 is different than VAL2, return +2.
770 For integer constants we either have already returned -1 or 1
771 or they are equivalent. We still might succeed in proving
772 something about non-trivial operands. */
773 if (TREE_CODE (val1) != INTEGER_CST
774 || TREE_CODE (val2) != INTEGER_CST)
775 {
776 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
777 if (t && integer_onep (t))
778 return 2;
779 }
780
781 return -2;
782 }
783 }
784
785 /* Compare values like compare_values_warnv. */
786
787 int
788 compare_values (tree val1, tree val2)
789 {
790 bool sop;
791 return compare_values_warnv (val1, val2, &sop);
792 }
793
794
795 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
796 0 if VAL is not inside [MIN, MAX],
797 -2 if we cannot tell either way.
798
799 Benchmark compile/20001226-1.c compilation time after changing this
800 function. */
801
802 int
803 value_inside_range (tree val, tree min, tree max)
804 {
805 int cmp1, cmp2;
806
807 cmp1 = operand_less_p (val, min);
808 if (cmp1 == -2)
809 return -2;
810 if (cmp1 == 1)
811 return 0;
812
813 cmp2 = operand_less_p (max, val);
814 if (cmp2 == -2)
815 return -2;
816
817 return !cmp2;
818 }
819
820
821 /* Return true if value ranges VR0 and VR1 have a non-empty
822 intersection.
823
824 Benchmark compile/20001226-1.c compilation time after changing this
825 function.
826 */
827
828 static inline bool
829 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
830 {
831 /* The value ranges do not intersect if the maximum of the first range is
832 less than the minimum of the second range or vice versa.
833 When those relations are unknown, we can't do any better. */
834 if (operand_less_p (vr0->max, vr1->min) != 0)
835 return false;
836 if (operand_less_p (vr1->max, vr0->min) != 0)
837 return false;
838 return true;
839 }
840
841
842 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
843 include the value zero, -2 if we cannot tell. */
844
845 int
846 range_includes_zero_p (tree min, tree max)
847 {
848 tree zero = build_int_cst (TREE_TYPE (min), 0);
849 return value_inside_range (zero, min, max);
850 }
851
852 /* Return true if *VR is know to only contain nonnegative values. */
853
854 static inline bool
855 value_range_nonnegative_p (value_range *vr)
856 {
857 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
858 which would return a useful value should be encoded as a
859 VR_RANGE. */
860 if (vr->type == VR_RANGE)
861 {
862 int result = compare_values (vr->min, integer_zero_node);
863 return (result == 0 || result == 1);
864 }
865
866 return false;
867 }
868
869 /* If *VR has a value rante that is a single constant value return that,
870 otherwise return NULL_TREE. */
871
872 tree
873 value_range_constant_singleton (value_range *vr)
874 {
875 if (vr->type == VR_RANGE
876 && vrp_operand_equal_p (vr->min, vr->max)
877 && is_gimple_min_invariant (vr->min))
878 return vr->min;
879
880 return NULL_TREE;
881 }
882
883 /* Wrapper around int_const_binop. Return true if we can compute the
884 result; i.e. if the operation doesn't overflow or if the overflow is
885 undefined. In the latter case (if the operation overflows and
886 overflow is undefined), then adjust the result to be -INF or +INF
887 depending on CODE, VAL1 and VAL2. Return the value in *RES.
888
889 Return false for division by zero, for which the result is
890 indeterminate. */
891
892 static bool
893 vrp_int_const_binop (enum tree_code code, tree val1, tree val2, wide_int *res)
894 {
895 bool overflow = false;
896 signop sign = TYPE_SIGN (TREE_TYPE (val1));
897
898 switch (code)
899 {
900 case RSHIFT_EXPR:
901 case LSHIFT_EXPR:
902 {
903 wide_int wval2 = wi::to_wide (val2, TYPE_PRECISION (TREE_TYPE (val1)));
904 if (wi::neg_p (wval2))
905 {
906 wval2 = -wval2;
907 if (code == RSHIFT_EXPR)
908 code = LSHIFT_EXPR;
909 else
910 code = RSHIFT_EXPR;
911 }
912
913 if (code == RSHIFT_EXPR)
914 /* It's unclear from the C standard whether shifts can overflow.
915 The following code ignores overflow; perhaps a C standard
916 interpretation ruling is needed. */
917 *res = wi::rshift (wi::to_wide (val1), wval2, sign);
918 else
919 *res = wi::lshift (wi::to_wide (val1), wval2);
920 break;
921 }
922
923 case MULT_EXPR:
924 *res = wi::mul (wi::to_wide (val1),
925 wi::to_wide (val2), sign, &overflow);
926 break;
927
928 case TRUNC_DIV_EXPR:
929 case EXACT_DIV_EXPR:
930 if (val2 == 0)
931 return false;
932 else
933 *res = wi::div_trunc (wi::to_wide (val1),
934 wi::to_wide (val2), sign, &overflow);
935 break;
936
937 case FLOOR_DIV_EXPR:
938 if (val2 == 0)
939 return false;
940 *res = wi::div_floor (wi::to_wide (val1),
941 wi::to_wide (val2), sign, &overflow);
942 break;
943
944 case CEIL_DIV_EXPR:
945 if (val2 == 0)
946 return false;
947 *res = wi::div_ceil (wi::to_wide (val1),
948 wi::to_wide (val2), sign, &overflow);
949 break;
950
951 case ROUND_DIV_EXPR:
952 if (val2 == 0)
953 return false;
954 *res = wi::div_round (wi::to_wide (val1),
955 wi::to_wide (val2), sign, &overflow);
956 break;
957
958 default:
959 gcc_unreachable ();
960 }
961
962 if (overflow
963 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
964 {
965 /* If the operation overflowed return -INF or +INF depending
966 on the operation and the combination of signs of the operands. */
967 int sgn1 = tree_int_cst_sgn (val1);
968 int sgn2 = tree_int_cst_sgn (val2);
969
970 /* Notice that we only need to handle the restricted set of
971 operations handled by extract_range_from_binary_expr.
972 Among them, only multiplication, addition and subtraction
973 can yield overflow without overflown operands because we
974 are working with integral types only... except in the
975 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
976 for division too. */
977
978 /* For multiplication, the sign of the overflow is given
979 by the comparison of the signs of the operands. */
980 if ((code == MULT_EXPR && sgn1 == sgn2)
981 /* For addition, the operands must be of the same sign
982 to yield an overflow. Its sign is therefore that
983 of one of the operands, for example the first. */
984 || (code == PLUS_EXPR && sgn1 >= 0)
985 /* For subtraction, operands must be of
986 different signs to yield an overflow. Its sign is
987 therefore that of the first operand or the opposite of
988 that of the second operand. A first operand of 0 counts
989 as positive here, for the corner case 0 - (-INF), which
990 overflows, but must yield +INF. */
991 || (code == MINUS_EXPR && sgn1 >= 0)
992 /* For division, the only case is -INF / -1 = +INF. */
993 || code == TRUNC_DIV_EXPR
994 || code == FLOOR_DIV_EXPR
995 || code == CEIL_DIV_EXPR
996 || code == EXACT_DIV_EXPR
997 || code == ROUND_DIV_EXPR)
998 *res = wi::max_value (TYPE_PRECISION (TREE_TYPE (val1)),
999 TYPE_SIGN (TREE_TYPE (val1)));
1000 else
1001 *res = wi::min_value (TYPE_PRECISION (TREE_TYPE (val1)),
1002 TYPE_SIGN (TREE_TYPE (val1)));
1003 return true;
1004 }
1005
1006 return !overflow;
1007 }
1008
1009
1010 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1011 bitmask if some bit is unset, it means for all numbers in the range
1012 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1013 bitmask if some bit is set, it means for all numbers in the range
1014 the bit is 1, otherwise it might be 0 or 1. */
1015
1016 bool
1017 zero_nonzero_bits_from_vr (const tree expr_type,
1018 value_range *vr,
1019 wide_int *may_be_nonzero,
1020 wide_int *must_be_nonzero)
1021 {
1022 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1023 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1024 if (!range_int_cst_p (vr))
1025 return false;
1026
1027 if (range_int_cst_singleton_p (vr))
1028 {
1029 *may_be_nonzero = wi::to_wide (vr->min);
1030 *must_be_nonzero = *may_be_nonzero;
1031 }
1032 else if (tree_int_cst_sgn (vr->min) >= 0
1033 || tree_int_cst_sgn (vr->max) < 0)
1034 {
1035 wide_int xor_mask = wi::to_wide (vr->min) ^ wi::to_wide (vr->max);
1036 *may_be_nonzero = wi::to_wide (vr->min) | wi::to_wide (vr->max);
1037 *must_be_nonzero = wi::to_wide (vr->min) & wi::to_wide (vr->max);
1038 if (xor_mask != 0)
1039 {
1040 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1041 may_be_nonzero->get_precision ());
1042 *may_be_nonzero = *may_be_nonzero | mask;
1043 *must_be_nonzero = wi::bit_and_not (*must_be_nonzero, mask);
1044 }
1045 }
1046
1047 return true;
1048 }
1049
1050 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1051 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1052 false otherwise. If *AR can be represented with a single range
1053 *VR1 will be VR_UNDEFINED. */
1054
1055 static bool
1056 ranges_from_anti_range (value_range *ar,
1057 value_range *vr0, value_range *vr1)
1058 {
1059 tree type = TREE_TYPE (ar->min);
1060
1061 vr0->type = VR_UNDEFINED;
1062 vr1->type = VR_UNDEFINED;
1063
1064 if (ar->type != VR_ANTI_RANGE
1065 || TREE_CODE (ar->min) != INTEGER_CST
1066 || TREE_CODE (ar->max) != INTEGER_CST
1067 || !vrp_val_min (type)
1068 || !vrp_val_max (type))
1069 return false;
1070
1071 if (!vrp_val_is_min (ar->min))
1072 {
1073 vr0->type = VR_RANGE;
1074 vr0->min = vrp_val_min (type);
1075 vr0->max = wide_int_to_tree (type, wi::to_wide (ar->min) - 1);
1076 }
1077 if (!vrp_val_is_max (ar->max))
1078 {
1079 vr1->type = VR_RANGE;
1080 vr1->min = wide_int_to_tree (type, wi::to_wide (ar->max) + 1);
1081 vr1->max = vrp_val_max (type);
1082 }
1083 if (vr0->type == VR_UNDEFINED)
1084 {
1085 *vr0 = *vr1;
1086 vr1->type = VR_UNDEFINED;
1087 }
1088
1089 return vr0->type != VR_UNDEFINED;
1090 }
1091
1092 /* Helper to extract a value-range *VR for a multiplicative operation
1093 *VR0 CODE *VR1. */
1094
1095 static void
1096 extract_range_from_multiplicative_op_1 (value_range *vr,
1097 enum tree_code code,
1098 value_range *vr0, value_range *vr1)
1099 {
1100 enum value_range_type rtype;
1101 wide_int val, min, max;
1102 tree type;
1103
1104 /* Multiplications, divisions and shifts are a bit tricky to handle,
1105 depending on the mix of signs we have in the two ranges, we
1106 need to operate on different values to get the minimum and
1107 maximum values for the new range. One approach is to figure
1108 out all the variations of range combinations and do the
1109 operations.
1110
1111 However, this involves several calls to compare_values and it
1112 is pretty convoluted. It's simpler to do the 4 operations
1113 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1114 MAX1) and then figure the smallest and largest values to form
1115 the new range. */
1116 gcc_assert (code == MULT_EXPR
1117 || code == TRUNC_DIV_EXPR
1118 || code == FLOOR_DIV_EXPR
1119 || code == CEIL_DIV_EXPR
1120 || code == EXACT_DIV_EXPR
1121 || code == ROUND_DIV_EXPR
1122 || code == RSHIFT_EXPR
1123 || code == LSHIFT_EXPR);
1124 gcc_assert (vr0->type == VR_RANGE
1125 && vr0->type == vr1->type);
1126
1127 rtype = vr0->type;
1128 type = TREE_TYPE (vr0->min);
1129 signop sgn = TYPE_SIGN (type);
1130
1131 /* Compute the 4 cross operations and their minimum and maximum value. */
1132 if (!vrp_int_const_binop (code, vr0->min, vr1->min, &val))
1133 {
1134 set_value_range_to_varying (vr);
1135 return;
1136 }
1137 min = max = val;
1138
1139 if (vr1->max != vr1->min)
1140 {
1141 if (!vrp_int_const_binop (code, vr0->min, vr1->max, &val))
1142 {
1143 set_value_range_to_varying (vr);
1144 return;
1145 }
1146 if (wi::lt_p (val, min, sgn))
1147 min = val;
1148 else if (wi::gt_p (val, max, sgn))
1149 max = val;
1150 }
1151
1152 if (vr0->max != vr0->min)
1153 {
1154 if (!vrp_int_const_binop (code, vr0->max, vr1->min, &val))
1155 {
1156 set_value_range_to_varying (vr);
1157 return;
1158 }
1159 if (wi::lt_p (val, min, sgn))
1160 min = val;
1161 else if (wi::gt_p (val, max, sgn))
1162 max = val;
1163 }
1164
1165 if (vr0->min != vr0->max && vr1->min != vr1->max)
1166 {
1167 if (!vrp_int_const_binop (code, vr0->max, vr1->max, &val))
1168 {
1169 set_value_range_to_varying (vr);
1170 return;
1171 }
1172 if (wi::lt_p (val, min, sgn))
1173 min = val;
1174 else if (wi::gt_p (val, max, sgn))
1175 max = val;
1176 }
1177
1178 /* If the new range has its limits swapped around (MIN > MAX),
1179 then the operation caused one of them to wrap around, mark
1180 the new range VARYING. */
1181 if (wi::gt_p (min, max, sgn))
1182 {
1183 set_value_range_to_varying (vr);
1184 return;
1185 }
1186
1187 /* We punt for [-INF, +INF].
1188 We learn nothing when we have INF on both sides.
1189 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1190 if (wi::eq_p (min, wi::min_value (TYPE_PRECISION (type), sgn))
1191 && wi::eq_p (max, wi::max_value (TYPE_PRECISION (type), sgn)))
1192 {
1193 set_value_range_to_varying (vr);
1194 return;
1195 }
1196
1197 set_value_range (vr, rtype,
1198 wide_int_to_tree (type, min),
1199 wide_int_to_tree (type, max), NULL);
1200 }
1201
1202 /* Extract range information from a binary operation CODE based on
1203 the ranges of each of its operands *VR0 and *VR1 with resulting
1204 type EXPR_TYPE. The resulting range is stored in *VR. */
1205
1206 void
1207 extract_range_from_binary_expr_1 (value_range *vr,
1208 enum tree_code code, tree expr_type,
1209 value_range *vr0_, value_range *vr1_)
1210 {
1211 value_range vr0 = *vr0_, vr1 = *vr1_;
1212 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
1213 enum value_range_type type;
1214 tree min = NULL_TREE, max = NULL_TREE;
1215 int cmp;
1216
1217 if (!INTEGRAL_TYPE_P (expr_type)
1218 && !POINTER_TYPE_P (expr_type))
1219 {
1220 set_value_range_to_varying (vr);
1221 return;
1222 }
1223
1224 /* Not all binary expressions can be applied to ranges in a
1225 meaningful way. Handle only arithmetic operations. */
1226 if (code != PLUS_EXPR
1227 && code != MINUS_EXPR
1228 && code != POINTER_PLUS_EXPR
1229 && code != MULT_EXPR
1230 && code != TRUNC_DIV_EXPR
1231 && code != FLOOR_DIV_EXPR
1232 && code != CEIL_DIV_EXPR
1233 && code != EXACT_DIV_EXPR
1234 && code != ROUND_DIV_EXPR
1235 && code != TRUNC_MOD_EXPR
1236 && code != RSHIFT_EXPR
1237 && code != LSHIFT_EXPR
1238 && code != MIN_EXPR
1239 && code != MAX_EXPR
1240 && code != BIT_AND_EXPR
1241 && code != BIT_IOR_EXPR
1242 && code != BIT_XOR_EXPR)
1243 {
1244 set_value_range_to_varying (vr);
1245 return;
1246 }
1247
1248 /* If both ranges are UNDEFINED, so is the result. */
1249 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
1250 {
1251 set_value_range_to_undefined (vr);
1252 return;
1253 }
1254 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1255 code. At some point we may want to special-case operations that
1256 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1257 operand. */
1258 else if (vr0.type == VR_UNDEFINED)
1259 set_value_range_to_varying (&vr0);
1260 else if (vr1.type == VR_UNDEFINED)
1261 set_value_range_to_varying (&vr1);
1262
1263 /* We get imprecise results from ranges_from_anti_range when
1264 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1265 range, but then we also need to hack up vrp_meet. It's just
1266 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1267 if (code == EXACT_DIV_EXPR
1268 && vr0.type == VR_ANTI_RANGE
1269 && vr0.min == vr0.max
1270 && integer_zerop (vr0.min))
1271 {
1272 set_value_range_to_nonnull (vr, expr_type);
1273 return;
1274 }
1275
1276 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1277 and express ~[] op X as ([]' op X) U ([]'' op X). */
1278 if (vr0.type == VR_ANTI_RANGE
1279 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
1280 {
1281 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
1282 if (vrtem1.type != VR_UNDEFINED)
1283 {
1284 value_range vrres = VR_INITIALIZER;
1285 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
1286 &vrtem1, vr1_);
1287 vrp_meet (vr, &vrres);
1288 }
1289 return;
1290 }
1291 /* Likewise for X op ~[]. */
1292 if (vr1.type == VR_ANTI_RANGE
1293 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
1294 {
1295 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
1296 if (vrtem1.type != VR_UNDEFINED)
1297 {
1298 value_range vrres = VR_INITIALIZER;
1299 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
1300 vr0_, &vrtem1);
1301 vrp_meet (vr, &vrres);
1302 }
1303 return;
1304 }
1305
1306 /* The type of the resulting value range defaults to VR0.TYPE. */
1307 type = vr0.type;
1308
1309 /* Refuse to operate on VARYING ranges, ranges of different kinds
1310 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
1311 because we may be able to derive a useful range even if one of
1312 the operands is VR_VARYING or symbolic range. Similarly for
1313 divisions, MIN/MAX and PLUS/MINUS.
1314
1315 TODO, we may be able to derive anti-ranges in some cases. */
1316 if (code != BIT_AND_EXPR
1317 && code != BIT_IOR_EXPR
1318 && code != TRUNC_DIV_EXPR
1319 && code != FLOOR_DIV_EXPR
1320 && code != CEIL_DIV_EXPR
1321 && code != EXACT_DIV_EXPR
1322 && code != ROUND_DIV_EXPR
1323 && code != TRUNC_MOD_EXPR
1324 && code != MIN_EXPR
1325 && code != MAX_EXPR
1326 && code != PLUS_EXPR
1327 && code != MINUS_EXPR
1328 && code != RSHIFT_EXPR
1329 && (vr0.type == VR_VARYING
1330 || vr1.type == VR_VARYING
1331 || vr0.type != vr1.type
1332 || symbolic_range_p (&vr0)
1333 || symbolic_range_p (&vr1)))
1334 {
1335 set_value_range_to_varying (vr);
1336 return;
1337 }
1338
1339 /* Now evaluate the expression to determine the new range. */
1340 if (POINTER_TYPE_P (expr_type))
1341 {
1342 if (code == MIN_EXPR || code == MAX_EXPR)
1343 {
1344 /* For MIN/MAX expressions with pointers, we only care about
1345 nullness, if both are non null, then the result is nonnull.
1346 If both are null, then the result is null. Otherwise they
1347 are varying. */
1348 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
1349 set_value_range_to_nonnull (vr, expr_type);
1350 else if (range_is_null (&vr0) && range_is_null (&vr1))
1351 set_value_range_to_null (vr, expr_type);
1352 else
1353 set_value_range_to_varying (vr);
1354 }
1355 else if (code == POINTER_PLUS_EXPR)
1356 {
1357 /* For pointer types, we are really only interested in asserting
1358 whether the expression evaluates to non-NULL. */
1359 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
1360 set_value_range_to_nonnull (vr, expr_type);
1361 else if (range_is_null (&vr0) && range_is_null (&vr1))
1362 set_value_range_to_null (vr, expr_type);
1363 else
1364 set_value_range_to_varying (vr);
1365 }
1366 else if (code == BIT_AND_EXPR)
1367 {
1368 /* For pointer types, we are really only interested in asserting
1369 whether the expression evaluates to non-NULL. */
1370 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
1371 set_value_range_to_nonnull (vr, expr_type);
1372 else if (range_is_null (&vr0) || range_is_null (&vr1))
1373 set_value_range_to_null (vr, expr_type);
1374 else
1375 set_value_range_to_varying (vr);
1376 }
1377 else
1378 set_value_range_to_varying (vr);
1379
1380 return;
1381 }
1382
1383 /* For integer ranges, apply the operation to each end of the
1384 range and see what we end up with. */
1385 if (code == PLUS_EXPR || code == MINUS_EXPR)
1386 {
1387 const bool minus_p = (code == MINUS_EXPR);
1388 tree min_op0 = vr0.min;
1389 tree min_op1 = minus_p ? vr1.max : vr1.min;
1390 tree max_op0 = vr0.max;
1391 tree max_op1 = minus_p ? vr1.min : vr1.max;
1392 tree sym_min_op0 = NULL_TREE;
1393 tree sym_min_op1 = NULL_TREE;
1394 tree sym_max_op0 = NULL_TREE;
1395 tree sym_max_op1 = NULL_TREE;
1396 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
1397
1398 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1399 single-symbolic ranges, try to compute the precise resulting range,
1400 but only if we know that this resulting range will also be constant
1401 or single-symbolic. */
1402 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
1403 && (TREE_CODE (min_op0) == INTEGER_CST
1404 || (sym_min_op0
1405 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
1406 && (TREE_CODE (min_op1) == INTEGER_CST
1407 || (sym_min_op1
1408 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
1409 && (!(sym_min_op0 && sym_min_op1)
1410 || (sym_min_op0 == sym_min_op1
1411 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
1412 && (TREE_CODE (max_op0) == INTEGER_CST
1413 || (sym_max_op0
1414 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
1415 && (TREE_CODE (max_op1) == INTEGER_CST
1416 || (sym_max_op1
1417 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
1418 && (!(sym_max_op0 && sym_max_op1)
1419 || (sym_max_op0 == sym_max_op1
1420 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
1421 {
1422 const signop sgn = TYPE_SIGN (expr_type);
1423 const unsigned int prec = TYPE_PRECISION (expr_type);
1424 wide_int type_min, type_max, wmin, wmax;
1425 int min_ovf = 0;
1426 int max_ovf = 0;
1427
1428 /* Get the lower and upper bounds of the type. */
1429 if (TYPE_OVERFLOW_WRAPS (expr_type))
1430 {
1431 type_min = wi::min_value (prec, sgn);
1432 type_max = wi::max_value (prec, sgn);
1433 }
1434 else
1435 {
1436 type_min = wi::to_wide (vrp_val_min (expr_type));
1437 type_max = wi::to_wide (vrp_val_max (expr_type));
1438 }
1439
1440 /* Combine the lower bounds, if any. */
1441 if (min_op0 && min_op1)
1442 {
1443 if (minus_p)
1444 {
1445 wmin = wi::to_wide (min_op0) - wi::to_wide (min_op1);
1446
1447 /* Check for overflow. */
1448 if (wi::cmp (0, wi::to_wide (min_op1), sgn)
1449 != wi::cmp (wmin, wi::to_wide (min_op0), sgn))
1450 min_ovf = wi::cmp (wi::to_wide (min_op0),
1451 wi::to_wide (min_op1), sgn);
1452 }
1453 else
1454 {
1455 wmin = wi::to_wide (min_op0) + wi::to_wide (min_op1);
1456
1457 /* Check for overflow. */
1458 if (wi::cmp (wi::to_wide (min_op1), 0, sgn)
1459 != wi::cmp (wmin, wi::to_wide (min_op0), sgn))
1460 min_ovf = wi::cmp (wi::to_wide (min_op0), wmin, sgn);
1461 }
1462 }
1463 else if (min_op0)
1464 wmin = wi::to_wide (min_op0);
1465 else if (min_op1)
1466 {
1467 if (minus_p)
1468 {
1469 wmin = -wi::to_wide (min_op1);
1470
1471 /* Check for overflow. */
1472 if (sgn == SIGNED
1473 && wi::neg_p (wi::to_wide (min_op1))
1474 && wi::neg_p (wmin))
1475 min_ovf = 1;
1476 else if (sgn == UNSIGNED && wi::to_wide (min_op1) != 0)
1477 min_ovf = -1;
1478 }
1479 else
1480 wmin = wi::to_wide (min_op1);
1481 }
1482 else
1483 wmin = wi::shwi (0, prec);
1484
1485 /* Combine the upper bounds, if any. */
1486 if (max_op0 && max_op1)
1487 {
1488 if (minus_p)
1489 {
1490 wmax = wi::to_wide (max_op0) - wi::to_wide (max_op1);
1491
1492 /* Check for overflow. */
1493 if (wi::cmp (0, wi::to_wide (max_op1), sgn)
1494 != wi::cmp (wmax, wi::to_wide (max_op0), sgn))
1495 max_ovf = wi::cmp (wi::to_wide (max_op0),
1496 wi::to_wide (max_op1), sgn);
1497 }
1498 else
1499 {
1500 wmax = wi::to_wide (max_op0) + wi::to_wide (max_op1);
1501
1502 if (wi::cmp (wi::to_wide (max_op1), 0, sgn)
1503 != wi::cmp (wmax, wi::to_wide (max_op0), sgn))
1504 max_ovf = wi::cmp (wi::to_wide (max_op0), wmax, sgn);
1505 }
1506 }
1507 else if (max_op0)
1508 wmax = wi::to_wide (max_op0);
1509 else if (max_op1)
1510 {
1511 if (minus_p)
1512 {
1513 wmax = -wi::to_wide (max_op1);
1514
1515 /* Check for overflow. */
1516 if (sgn == SIGNED
1517 && wi::neg_p (wi::to_wide (max_op1))
1518 && wi::neg_p (wmax))
1519 max_ovf = 1;
1520 else if (sgn == UNSIGNED && wi::to_wide (max_op1) != 0)
1521 max_ovf = -1;
1522 }
1523 else
1524 wmax = wi::to_wide (max_op1);
1525 }
1526 else
1527 wmax = wi::shwi (0, prec);
1528
1529 /* Check for type overflow. */
1530 if (min_ovf == 0)
1531 {
1532 if (wi::cmp (wmin, type_min, sgn) == -1)
1533 min_ovf = -1;
1534 else if (wi::cmp (wmin, type_max, sgn) == 1)
1535 min_ovf = 1;
1536 }
1537 if (max_ovf == 0)
1538 {
1539 if (wi::cmp (wmax, type_min, sgn) == -1)
1540 max_ovf = -1;
1541 else if (wi::cmp (wmax, type_max, sgn) == 1)
1542 max_ovf = 1;
1543 }
1544
1545 /* If we have overflow for the constant part and the resulting
1546 range will be symbolic, drop to VR_VARYING. */
1547 if ((min_ovf && sym_min_op0 != sym_min_op1)
1548 || (max_ovf && sym_max_op0 != sym_max_op1))
1549 {
1550 set_value_range_to_varying (vr);
1551 return;
1552 }
1553
1554 if (TYPE_OVERFLOW_WRAPS (expr_type))
1555 {
1556 /* If overflow wraps, truncate the values and adjust the
1557 range kind and bounds appropriately. */
1558 wide_int tmin = wide_int::from (wmin, prec, sgn);
1559 wide_int tmax = wide_int::from (wmax, prec, sgn);
1560 if (min_ovf == max_ovf)
1561 {
1562 /* No overflow or both overflow or underflow. The
1563 range kind stays VR_RANGE. */
1564 min = wide_int_to_tree (expr_type, tmin);
1565 max = wide_int_to_tree (expr_type, tmax);
1566 }
1567 else if ((min_ovf == -1 && max_ovf == 0)
1568 || (max_ovf == 1 && min_ovf == 0))
1569 {
1570 /* Min underflow or max overflow. The range kind
1571 changes to VR_ANTI_RANGE. */
1572 bool covers = false;
1573 wide_int tem = tmin;
1574 type = VR_ANTI_RANGE;
1575 tmin = tmax + 1;
1576 if (wi::cmp (tmin, tmax, sgn) < 0)
1577 covers = true;
1578 tmax = tem - 1;
1579 if (wi::cmp (tmax, tem, sgn) > 0)
1580 covers = true;
1581 /* If the anti-range would cover nothing, drop to varying.
1582 Likewise if the anti-range bounds are outside of the
1583 types values. */
1584 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
1585 {
1586 set_value_range_to_varying (vr);
1587 return;
1588 }
1589 min = wide_int_to_tree (expr_type, tmin);
1590 max = wide_int_to_tree (expr_type, tmax);
1591 }
1592 else
1593 {
1594 /* Other underflow and/or overflow, drop to VR_VARYING. */
1595 set_value_range_to_varying (vr);
1596 return;
1597 }
1598 }
1599 else
1600 {
1601 /* If overflow does not wrap, saturate to the types min/max
1602 value. */
1603 if (min_ovf == -1)
1604 min = wide_int_to_tree (expr_type, type_min);
1605 else if (min_ovf == 1)
1606 min = wide_int_to_tree (expr_type, type_max);
1607 else
1608 min = wide_int_to_tree (expr_type, wmin);
1609
1610 if (max_ovf == -1)
1611 max = wide_int_to_tree (expr_type, type_min);
1612 else if (max_ovf == 1)
1613 max = wide_int_to_tree (expr_type, type_max);
1614 else
1615 max = wide_int_to_tree (expr_type, wmax);
1616 }
1617
1618 /* If the result lower bound is constant, we're done;
1619 otherwise, build the symbolic lower bound. */
1620 if (sym_min_op0 == sym_min_op1)
1621 ;
1622 else if (sym_min_op0)
1623 min = build_symbolic_expr (expr_type, sym_min_op0,
1624 neg_min_op0, min);
1625 else if (sym_min_op1)
1626 {
1627 /* We may not negate if that might introduce
1628 undefined overflow. */
1629 if (! minus_p
1630 || neg_min_op1
1631 || TYPE_OVERFLOW_WRAPS (expr_type))
1632 min = build_symbolic_expr (expr_type, sym_min_op1,
1633 neg_min_op1 ^ minus_p, min);
1634 else
1635 min = NULL_TREE;
1636 }
1637
1638 /* Likewise for the upper bound. */
1639 if (sym_max_op0 == sym_max_op1)
1640 ;
1641 else if (sym_max_op0)
1642 max = build_symbolic_expr (expr_type, sym_max_op0,
1643 neg_max_op0, max);
1644 else if (sym_max_op1)
1645 {
1646 /* We may not negate if that might introduce
1647 undefined overflow. */
1648 if (! minus_p
1649 || neg_max_op1
1650 || TYPE_OVERFLOW_WRAPS (expr_type))
1651 max = build_symbolic_expr (expr_type, sym_max_op1,
1652 neg_max_op1 ^ minus_p, max);
1653 else
1654 max = NULL_TREE;
1655 }
1656 }
1657 else
1658 {
1659 /* For other cases, for example if we have a PLUS_EXPR with two
1660 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1661 to compute a precise range for such a case.
1662 ??? General even mixed range kind operations can be expressed
1663 by for example transforming ~[3, 5] + [1, 2] to range-only
1664 operations and a union primitive:
1665 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1666 [-INF+1, 4] U [6, +INF(OVF)]
1667 though usually the union is not exactly representable with
1668 a single range or anti-range as the above is
1669 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1670 but one could use a scheme similar to equivalences for this. */
1671 set_value_range_to_varying (vr);
1672 return;
1673 }
1674 }
1675 else if (code == MIN_EXPR
1676 || code == MAX_EXPR)
1677 {
1678 if (vr0.type == VR_RANGE
1679 && !symbolic_range_p (&vr0))
1680 {
1681 type = VR_RANGE;
1682 if (vr1.type == VR_RANGE
1683 && !symbolic_range_p (&vr1))
1684 {
1685 /* For operations that make the resulting range directly
1686 proportional to the original ranges, apply the operation to
1687 the same end of each range. */
1688 min = int_const_binop (code, vr0.min, vr1.min);
1689 max = int_const_binop (code, vr0.max, vr1.max);
1690 }
1691 else if (code == MIN_EXPR)
1692 {
1693 min = vrp_val_min (expr_type);
1694 max = vr0.max;
1695 }
1696 else if (code == MAX_EXPR)
1697 {
1698 min = vr0.min;
1699 max = vrp_val_max (expr_type);
1700 }
1701 }
1702 else if (vr1.type == VR_RANGE
1703 && !symbolic_range_p (&vr1))
1704 {
1705 type = VR_RANGE;
1706 if (code == MIN_EXPR)
1707 {
1708 min = vrp_val_min (expr_type);
1709 max = vr1.max;
1710 }
1711 else if (code == MAX_EXPR)
1712 {
1713 min = vr1.min;
1714 max = vrp_val_max (expr_type);
1715 }
1716 }
1717 else
1718 {
1719 set_value_range_to_varying (vr);
1720 return;
1721 }
1722 }
1723 else if (code == MULT_EXPR)
1724 {
1725 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
1726 drop to varying. This test requires 2*prec bits if both
1727 operands are signed and 2*prec + 2 bits if either is not. */
1728
1729 signop sign = TYPE_SIGN (expr_type);
1730 unsigned int prec = TYPE_PRECISION (expr_type);
1731
1732 if (!range_int_cst_p (&vr0)
1733 || !range_int_cst_p (&vr1))
1734 {
1735 set_value_range_to_varying (vr);
1736 return;
1737 }
1738
1739 if (TYPE_OVERFLOW_WRAPS (expr_type))
1740 {
1741 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
1742 typedef generic_wide_int
1743 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
1744 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
1745 vrp_int size = sizem1 + 1;
1746
1747 /* Extend the values using the sign of the result to PREC2.
1748 From here on out, everthing is just signed math no matter
1749 what the input types were. */
1750 vrp_int min0 = vrp_int_cst (vr0.min);
1751 vrp_int max0 = vrp_int_cst (vr0.max);
1752 vrp_int min1 = vrp_int_cst (vr1.min);
1753 vrp_int max1 = vrp_int_cst (vr1.max);
1754 /* Canonicalize the intervals. */
1755 if (sign == UNSIGNED)
1756 {
1757 if (wi::ltu_p (size, min0 + max0))
1758 {
1759 min0 -= size;
1760 max0 -= size;
1761 }
1762
1763 if (wi::ltu_p (size, min1 + max1))
1764 {
1765 min1 -= size;
1766 max1 -= size;
1767 }
1768 }
1769
1770 vrp_int prod0 = min0 * min1;
1771 vrp_int prod1 = min0 * max1;
1772 vrp_int prod2 = max0 * min1;
1773 vrp_int prod3 = max0 * max1;
1774
1775 /* Sort the 4 products so that min is in prod0 and max is in
1776 prod3. */
1777 /* min0min1 > max0max1 */
1778 if (prod0 > prod3)
1779 std::swap (prod0, prod3);
1780
1781 /* min0max1 > max0min1 */
1782 if (prod1 > prod2)
1783 std::swap (prod1, prod2);
1784
1785 if (prod0 > prod1)
1786 std::swap (prod0, prod1);
1787
1788 if (prod2 > prod3)
1789 std::swap (prod2, prod3);
1790
1791 /* diff = max - min. */
1792 prod2 = prod3 - prod0;
1793 if (wi::geu_p (prod2, sizem1))
1794 {
1795 /* the range covers all values. */
1796 set_value_range_to_varying (vr);
1797 return;
1798 }
1799
1800 /* The following should handle the wrapping and selecting
1801 VR_ANTI_RANGE for us. */
1802 min = wide_int_to_tree (expr_type, prod0);
1803 max = wide_int_to_tree (expr_type, prod3);
1804 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
1805 return;
1806 }
1807
1808 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1809 drop to VR_VARYING. It would take more effort to compute a
1810 precise range for such a case. For example, if we have
1811 op0 == 65536 and op1 == 65536 with their ranges both being
1812 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1813 we cannot claim that the product is in ~[0,0]. Note that we
1814 are guaranteed to have vr0.type == vr1.type at this
1815 point. */
1816 if (vr0.type == VR_ANTI_RANGE
1817 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
1818 {
1819 set_value_range_to_varying (vr);
1820 return;
1821 }
1822
1823 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
1824 return;
1825 }
1826 else if (code == RSHIFT_EXPR
1827 || code == LSHIFT_EXPR)
1828 {
1829 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
1830 then drop to VR_VARYING. Outside of this range we get undefined
1831 behavior from the shift operation. We cannot even trust
1832 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
1833 shifts, and the operation at the tree level may be widened. */
1834 if (range_int_cst_p (&vr1)
1835 && compare_tree_int (vr1.min, 0) >= 0
1836 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
1837 {
1838 if (code == RSHIFT_EXPR)
1839 {
1840 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1841 useful ranges just from the shift count. E.g.
1842 x >> 63 for signed 64-bit x is always [-1, 0]. */
1843 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
1844 {
1845 vr0.type = type = VR_RANGE;
1846 vr0.min = vrp_val_min (expr_type);
1847 vr0.max = vrp_val_max (expr_type);
1848 }
1849 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
1850 return;
1851 }
1852 /* We can map lshifts by constants to MULT_EXPR handling. */
1853 else if (code == LSHIFT_EXPR
1854 && range_int_cst_singleton_p (&vr1))
1855 {
1856 bool saved_flag_wrapv;
1857 value_range vr1p = VR_INITIALIZER;
1858 vr1p.type = VR_RANGE;
1859 vr1p.min = (wide_int_to_tree
1860 (expr_type,
1861 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
1862 TYPE_PRECISION (expr_type))));
1863 vr1p.max = vr1p.min;
1864 /* We have to use a wrapping multiply though as signed overflow
1865 on lshifts is implementation defined in C89. */
1866 saved_flag_wrapv = flag_wrapv;
1867 flag_wrapv = 1;
1868 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
1869 &vr0, &vr1p);
1870 flag_wrapv = saved_flag_wrapv;
1871 return;
1872 }
1873 else if (code == LSHIFT_EXPR
1874 && range_int_cst_p (&vr0))
1875 {
1876 int prec = TYPE_PRECISION (expr_type);
1877 int overflow_pos = prec;
1878 int bound_shift;
1879 wide_int low_bound, high_bound;
1880 bool uns = TYPE_UNSIGNED (expr_type);
1881 bool in_bounds = false;
1882
1883 if (!uns)
1884 overflow_pos -= 1;
1885
1886 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
1887 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1888 overflow. However, for that to happen, vr1.max needs to be
1889 zero, which means vr1 is a singleton range of zero, which
1890 means it should be handled by the previous LSHIFT_EXPR
1891 if-clause. */
1892 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
1893 wide_int complement = ~(bound - 1);
1894
1895 if (uns)
1896 {
1897 low_bound = bound;
1898 high_bound = complement;
1899 if (wi::ltu_p (wi::to_wide (vr0.max), low_bound))
1900 {
1901 /* [5, 6] << [1, 2] == [10, 24]. */
1902 /* We're shifting out only zeroes, the value increases
1903 monotonically. */
1904 in_bounds = true;
1905 }
1906 else if (wi::ltu_p (high_bound, wi::to_wide (vr0.min)))
1907 {
1908 /* [0xffffff00, 0xffffffff] << [1, 2]
1909 == [0xfffffc00, 0xfffffffe]. */
1910 /* We're shifting out only ones, the value decreases
1911 monotonically. */
1912 in_bounds = true;
1913 }
1914 }
1915 else
1916 {
1917 /* [-1, 1] << [1, 2] == [-4, 4]. */
1918 low_bound = complement;
1919 high_bound = bound;
1920 if (wi::lts_p (wi::to_wide (vr0.max), high_bound)
1921 && wi::lts_p (low_bound, wi::to_wide (vr0.min)))
1922 {
1923 /* For non-negative numbers, we're shifting out only
1924 zeroes, the value increases monotonically.
1925 For negative numbers, we're shifting out only ones, the
1926 value decreases monotomically. */
1927 in_bounds = true;
1928 }
1929 }
1930
1931 if (in_bounds)
1932 {
1933 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
1934 return;
1935 }
1936 }
1937 }
1938 set_value_range_to_varying (vr);
1939 return;
1940 }
1941 else if (code == TRUNC_DIV_EXPR
1942 || code == FLOOR_DIV_EXPR
1943 || code == CEIL_DIV_EXPR
1944 || code == EXACT_DIV_EXPR
1945 || code == ROUND_DIV_EXPR)
1946 {
1947 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
1948 {
1949 /* For division, if op1 has VR_RANGE but op0 does not, something
1950 can be deduced just from that range. Say [min, max] / [4, max]
1951 gives [min / 4, max / 4] range. */
1952 if (vr1.type == VR_RANGE
1953 && !symbolic_range_p (&vr1)
1954 && range_includes_zero_p (vr1.min, vr1.max) == 0)
1955 {
1956 vr0.type = type = VR_RANGE;
1957 vr0.min = vrp_val_min (expr_type);
1958 vr0.max = vrp_val_max (expr_type);
1959 }
1960 else
1961 {
1962 set_value_range_to_varying (vr);
1963 return;
1964 }
1965 }
1966
1967 /* For divisions, if flag_non_call_exceptions is true, we must
1968 not eliminate a division by zero. */
1969 if (cfun->can_throw_non_call_exceptions
1970 && (vr1.type != VR_RANGE
1971 || range_includes_zero_p (vr1.min, vr1.max) != 0))
1972 {
1973 set_value_range_to_varying (vr);
1974 return;
1975 }
1976
1977 /* For divisions, if op0 is VR_RANGE, we can deduce a range
1978 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
1979 include 0. */
1980 if (vr0.type == VR_RANGE
1981 && (vr1.type != VR_RANGE
1982 || range_includes_zero_p (vr1.min, vr1.max) != 0))
1983 {
1984 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
1985 int cmp;
1986
1987 min = NULL_TREE;
1988 max = NULL_TREE;
1989 if (TYPE_UNSIGNED (expr_type)
1990 || value_range_nonnegative_p (&vr1))
1991 {
1992 /* For unsigned division or when divisor is known
1993 to be non-negative, the range has to cover
1994 all numbers from 0 to max for positive max
1995 and all numbers from min to 0 for negative min. */
1996 cmp = compare_values (vr0.max, zero);
1997 if (cmp == -1)
1998 {
1999 /* When vr0.max < 0, vr1.min != 0 and value
2000 ranges for dividend and divisor are available. */
2001 if (vr1.type == VR_RANGE
2002 && !symbolic_range_p (&vr0)
2003 && !symbolic_range_p (&vr1)
2004 && compare_values (vr1.min, zero) != 0)
2005 max = int_const_binop (code, vr0.max, vr1.min);
2006 else
2007 max = zero;
2008 }
2009 else if (cmp == 0 || cmp == 1)
2010 max = vr0.max;
2011 else
2012 type = VR_VARYING;
2013 cmp = compare_values (vr0.min, zero);
2014 if (cmp == 1)
2015 {
2016 /* For unsigned division when value ranges for dividend
2017 and divisor are available. */
2018 if (vr1.type == VR_RANGE
2019 && !symbolic_range_p (&vr0)
2020 && !symbolic_range_p (&vr1)
2021 && compare_values (vr1.max, zero) != 0)
2022 min = int_const_binop (code, vr0.min, vr1.max);
2023 else
2024 min = zero;
2025 }
2026 else if (cmp == 0 || cmp == -1)
2027 min = vr0.min;
2028 else
2029 type = VR_VARYING;
2030 }
2031 else
2032 {
2033 /* Otherwise the range is -max .. max or min .. -min
2034 depending on which bound is bigger in absolute value,
2035 as the division can change the sign. */
2036 abs_extent_range (vr, vr0.min, vr0.max);
2037 return;
2038 }
2039 if (type == VR_VARYING)
2040 {
2041 set_value_range_to_varying (vr);
2042 return;
2043 }
2044 }
2045 else if (range_int_cst_p (&vr0) && range_int_cst_p (&vr1))
2046 {
2047 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2048 return;
2049 }
2050 }
2051 else if (code == TRUNC_MOD_EXPR)
2052 {
2053 if (range_is_null (&vr1))
2054 {
2055 set_value_range_to_undefined (vr);
2056 return;
2057 }
2058 /* ABS (A % B) < ABS (B) and either
2059 0 <= A % B <= A or A <= A % B <= 0. */
2060 type = VR_RANGE;
2061 signop sgn = TYPE_SIGN (expr_type);
2062 unsigned int prec = TYPE_PRECISION (expr_type);
2063 wide_int wmin, wmax, tmp;
2064 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2065 {
2066 wmax = wi::to_wide (vr1.max) - 1;
2067 if (sgn == SIGNED)
2068 {
2069 tmp = -1 - wi::to_wide (vr1.min);
2070 wmax = wi::smax (wmax, tmp);
2071 }
2072 }
2073 else
2074 {
2075 wmax = wi::max_value (prec, sgn);
2076 /* X % INT_MIN may be INT_MAX. */
2077 if (sgn == UNSIGNED)
2078 wmax = wmax - 1;
2079 }
2080
2081 if (sgn == UNSIGNED)
2082 wmin = wi::zero (prec);
2083 else
2084 {
2085 wmin = -wmax;
2086 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
2087 {
2088 tmp = wi::to_wide (vr0.min);
2089 if (wi::gts_p (tmp, 0))
2090 tmp = wi::zero (prec);
2091 wmin = wi::smax (wmin, tmp);
2092 }
2093 }
2094
2095 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
2096 {
2097 tmp = wi::to_wide (vr0.max);
2098 if (sgn == SIGNED && wi::neg_p (tmp))
2099 tmp = wi::zero (prec);
2100 wmax = wi::min (wmax, tmp, sgn);
2101 }
2102
2103 min = wide_int_to_tree (expr_type, wmin);
2104 max = wide_int_to_tree (expr_type, wmax);
2105 }
2106 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2107 {
2108 bool int_cst_range0, int_cst_range1;
2109 wide_int may_be_nonzero0, may_be_nonzero1;
2110 wide_int must_be_nonzero0, must_be_nonzero1;
2111
2112 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
2113 &may_be_nonzero0,
2114 &must_be_nonzero0);
2115 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
2116 &may_be_nonzero1,
2117 &must_be_nonzero1);
2118
2119 if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
2120 {
2121 value_range *vr0p = NULL, *vr1p = NULL;
2122 if (range_int_cst_singleton_p (&vr1))
2123 {
2124 vr0p = &vr0;
2125 vr1p = &vr1;
2126 }
2127 else if (range_int_cst_singleton_p (&vr0))
2128 {
2129 vr0p = &vr1;
2130 vr1p = &vr0;
2131 }
2132 /* For op & or | attempt to optimize:
2133 [x, y] op z into [x op z, y op z]
2134 if z is a constant which (for op | its bitwise not) has n
2135 consecutive least significant bits cleared followed by m 1
2136 consecutive bits set immediately above it and either
2137 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2138 The least significant n bits of all the values in the range are
2139 cleared or set, the m bits above it are preserved and any bits
2140 above these are required to be the same for all values in the
2141 range. */
2142 if (vr0p && range_int_cst_p (vr0p))
2143 {
2144 wide_int w = wi::to_wide (vr1p->min);
2145 int m = 0, n = 0;
2146 if (code == BIT_IOR_EXPR)
2147 w = ~w;
2148 if (wi::eq_p (w, 0))
2149 n = TYPE_PRECISION (expr_type);
2150 else
2151 {
2152 n = wi::ctz (w);
2153 w = ~(w | wi::mask (n, false, w.get_precision ()));
2154 if (wi::eq_p (w, 0))
2155 m = TYPE_PRECISION (expr_type) - n;
2156 else
2157 m = wi::ctz (w) - n;
2158 }
2159 wide_int mask = wi::mask (m + n, true, w.get_precision ());
2160 if ((mask & wi::to_wide (vr0p->min))
2161 == (mask & wi::to_wide (vr0p->max)))
2162 {
2163 min = int_const_binop (code, vr0p->min, vr1p->min);
2164 max = int_const_binop (code, vr0p->max, vr1p->min);
2165 }
2166 }
2167 }
2168
2169 type = VR_RANGE;
2170 if (min && max)
2171 /* Optimized above already. */;
2172 else if (code == BIT_AND_EXPR)
2173 {
2174 min = wide_int_to_tree (expr_type,
2175 must_be_nonzero0 & must_be_nonzero1);
2176 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
2177 /* If both input ranges contain only negative values we can
2178 truncate the result range maximum to the minimum of the
2179 input range maxima. */
2180 if (int_cst_range0 && int_cst_range1
2181 && tree_int_cst_sgn (vr0.max) < 0
2182 && tree_int_cst_sgn (vr1.max) < 0)
2183 {
2184 wmax = wi::min (wmax, wi::to_wide (vr0.max),
2185 TYPE_SIGN (expr_type));
2186 wmax = wi::min (wmax, wi::to_wide (vr1.max),
2187 TYPE_SIGN (expr_type));
2188 }
2189 /* If either input range contains only non-negative values
2190 we can truncate the result range maximum to the respective
2191 maximum of the input range. */
2192 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2193 wmax = wi::min (wmax, wi::to_wide (vr0.max),
2194 TYPE_SIGN (expr_type));
2195 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2196 wmax = wi::min (wmax, wi::to_wide (vr1.max),
2197 TYPE_SIGN (expr_type));
2198 max = wide_int_to_tree (expr_type, wmax);
2199 cmp = compare_values (min, max);
2200 /* PR68217: In case of signed & sign-bit-CST should
2201 result in [-INF, 0] instead of [-INF, INF]. */
2202 if (cmp == -2 || cmp == 1)
2203 {
2204 wide_int sign_bit
2205 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
2206 TYPE_PRECISION (expr_type));
2207 if (!TYPE_UNSIGNED (expr_type)
2208 && ((int_cst_range0
2209 && value_range_constant_singleton (&vr0)
2210 && !wi::cmps (wi::to_wide (vr0.min), sign_bit))
2211 || (int_cst_range1
2212 && value_range_constant_singleton (&vr1)
2213 && !wi::cmps (wi::to_wide (vr1.min), sign_bit))))
2214 {
2215 min = TYPE_MIN_VALUE (expr_type);
2216 max = build_int_cst (expr_type, 0);
2217 }
2218 }
2219 }
2220 else if (code == BIT_IOR_EXPR)
2221 {
2222 max = wide_int_to_tree (expr_type,
2223 may_be_nonzero0 | may_be_nonzero1);
2224 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
2225 /* If the input ranges contain only positive values we can
2226 truncate the minimum of the result range to the maximum
2227 of the input range minima. */
2228 if (int_cst_range0 && int_cst_range1
2229 && tree_int_cst_sgn (vr0.min) >= 0
2230 && tree_int_cst_sgn (vr1.min) >= 0)
2231 {
2232 wmin = wi::max (wmin, wi::to_wide (vr0.min),
2233 TYPE_SIGN (expr_type));
2234 wmin = wi::max (wmin, wi::to_wide (vr1.min),
2235 TYPE_SIGN (expr_type));
2236 }
2237 /* If either input range contains only negative values
2238 we can truncate the minimum of the result range to the
2239 respective minimum range. */
2240 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
2241 wmin = wi::max (wmin, wi::to_wide (vr0.min),
2242 TYPE_SIGN (expr_type));
2243 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
2244 wmin = wi::max (wmin, wi::to_wide (vr1.min),
2245 TYPE_SIGN (expr_type));
2246 min = wide_int_to_tree (expr_type, wmin);
2247 }
2248 else if (code == BIT_XOR_EXPR)
2249 {
2250 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
2251 | ~(may_be_nonzero0 | may_be_nonzero1));
2252 wide_int result_one_bits
2253 = (wi::bit_and_not (must_be_nonzero0, may_be_nonzero1)
2254 | wi::bit_and_not (must_be_nonzero1, may_be_nonzero0));
2255 max = wide_int_to_tree (expr_type, ~result_zero_bits);
2256 min = wide_int_to_tree (expr_type, result_one_bits);
2257 /* If the range has all positive or all negative values the
2258 result is better than VARYING. */
2259 if (tree_int_cst_sgn (min) < 0
2260 || tree_int_cst_sgn (max) >= 0)
2261 ;
2262 else
2263 max = min = NULL_TREE;
2264 }
2265 }
2266 else
2267 gcc_unreachable ();
2268
2269 /* If either MIN or MAX overflowed, then set the resulting range to
2270 VARYING. */
2271 if (min == NULL_TREE
2272 || TREE_OVERFLOW_P (min)
2273 || max == NULL_TREE
2274 || TREE_OVERFLOW_P (max))
2275 {
2276 set_value_range_to_varying (vr);
2277 return;
2278 }
2279
2280 /* We punt for [-INF, +INF].
2281 We learn nothing when we have INF on both sides.
2282 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2283 if (vrp_val_is_min (min) && vrp_val_is_max (max))
2284 {
2285 set_value_range_to_varying (vr);
2286 return;
2287 }
2288
2289 cmp = compare_values (min, max);
2290 if (cmp == -2 || cmp == 1)
2291 {
2292 /* If the new range has its limits swapped around (MIN > MAX),
2293 then the operation caused one of them to wrap around, mark
2294 the new range VARYING. */
2295 set_value_range_to_varying (vr);
2296 }
2297 else
2298 set_value_range (vr, type, min, max, NULL);
2299 }
2300
2301 /* Extract range information from a unary operation CODE based on
2302 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
2303 The resulting range is stored in *VR. */
2304
2305 void
2306 extract_range_from_unary_expr (value_range *vr,
2307 enum tree_code code, tree type,
2308 value_range *vr0_, tree op0_type)
2309 {
2310 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2311
2312 /* VRP only operates on integral and pointer types. */
2313 if (!(INTEGRAL_TYPE_P (op0_type)
2314 || POINTER_TYPE_P (op0_type))
2315 || !(INTEGRAL_TYPE_P (type)
2316 || POINTER_TYPE_P (type)))
2317 {
2318 set_value_range_to_varying (vr);
2319 return;
2320 }
2321
2322 /* If VR0 is UNDEFINED, so is the result. */
2323 if (vr0.type == VR_UNDEFINED)
2324 {
2325 set_value_range_to_undefined (vr);
2326 return;
2327 }
2328
2329 /* Handle operations that we express in terms of others. */
2330 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
2331 {
2332 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
2333 copy_value_range (vr, &vr0);
2334 return;
2335 }
2336 else if (code == NEGATE_EXPR)
2337 {
2338 /* -X is simply 0 - X, so re-use existing code that also handles
2339 anti-ranges fine. */
2340 value_range zero = VR_INITIALIZER;
2341 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
2342 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
2343 return;
2344 }
2345 else if (code == BIT_NOT_EXPR)
2346 {
2347 /* ~X is simply -1 - X, so re-use existing code that also handles
2348 anti-ranges fine. */
2349 value_range minusone = VR_INITIALIZER;
2350 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
2351 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
2352 type, &minusone, &vr0);
2353 return;
2354 }
2355
2356 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2357 and express op ~[] as (op []') U (op []''). */
2358 if (vr0.type == VR_ANTI_RANGE
2359 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2360 {
2361 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
2362 if (vrtem1.type != VR_UNDEFINED)
2363 {
2364 value_range vrres = VR_INITIALIZER;
2365 extract_range_from_unary_expr (&vrres, code, type,
2366 &vrtem1, op0_type);
2367 vrp_meet (vr, &vrres);
2368 }
2369 return;
2370 }
2371
2372 if (CONVERT_EXPR_CODE_P (code))
2373 {
2374 tree inner_type = op0_type;
2375 tree outer_type = type;
2376
2377 /* If the expression evaluates to a pointer, we are only interested in
2378 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2379 if (POINTER_TYPE_P (type))
2380 {
2381 if (range_is_nonnull (&vr0))
2382 set_value_range_to_nonnull (vr, type);
2383 else if (range_is_null (&vr0))
2384 set_value_range_to_null (vr, type);
2385 else
2386 set_value_range_to_varying (vr);
2387 return;
2388 }
2389
2390 /* If VR0 is varying and we increase the type precision, assume
2391 a full range for the following transformation. */
2392 if (vr0.type == VR_VARYING
2393 && INTEGRAL_TYPE_P (inner_type)
2394 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
2395 {
2396 vr0.type = VR_RANGE;
2397 vr0.min = TYPE_MIN_VALUE (inner_type);
2398 vr0.max = TYPE_MAX_VALUE (inner_type);
2399 }
2400
2401 /* If VR0 is a constant range or anti-range and the conversion is
2402 not truncating we can convert the min and max values and
2403 canonicalize the resulting range. Otherwise we can do the
2404 conversion if the size of the range is less than what the
2405 precision of the target type can represent and the range is
2406 not an anti-range. */
2407 if ((vr0.type == VR_RANGE
2408 || vr0.type == VR_ANTI_RANGE)
2409 && TREE_CODE (vr0.min) == INTEGER_CST
2410 && TREE_CODE (vr0.max) == INTEGER_CST
2411 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
2412 || (vr0.type == VR_RANGE
2413 && integer_zerop (int_const_binop (RSHIFT_EXPR,
2414 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
2415 size_int (TYPE_PRECISION (outer_type)))))))
2416 {
2417 tree new_min, new_max;
2418 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
2419 0, false);
2420 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
2421 0, false);
2422 set_and_canonicalize_value_range (vr, vr0.type,
2423 new_min, new_max, NULL);
2424 return;
2425 }
2426
2427 set_value_range_to_varying (vr);
2428 return;
2429 }
2430 else if (code == ABS_EXPR)
2431 {
2432 tree min, max;
2433 int cmp;
2434
2435 /* Pass through vr0 in the easy cases. */
2436 if (TYPE_UNSIGNED (type)
2437 || value_range_nonnegative_p (&vr0))
2438 {
2439 copy_value_range (vr, &vr0);
2440 return;
2441 }
2442
2443 /* For the remaining varying or symbolic ranges we can't do anything
2444 useful. */
2445 if (vr0.type == VR_VARYING
2446 || symbolic_range_p (&vr0))
2447 {
2448 set_value_range_to_varying (vr);
2449 return;
2450 }
2451
2452 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2453 useful range. */
2454 if (!TYPE_OVERFLOW_UNDEFINED (type)
2455 && ((vr0.type == VR_RANGE
2456 && vrp_val_is_min (vr0.min))
2457 || (vr0.type == VR_ANTI_RANGE
2458 && !vrp_val_is_min (vr0.min))))
2459 {
2460 set_value_range_to_varying (vr);
2461 return;
2462 }
2463
2464 /* ABS_EXPR may flip the range around, if the original range
2465 included negative values. */
2466 if (!vrp_val_is_min (vr0.min))
2467 min = fold_unary_to_constant (code, type, vr0.min);
2468 else
2469 min = TYPE_MAX_VALUE (type);
2470
2471 if (!vrp_val_is_min (vr0.max))
2472 max = fold_unary_to_constant (code, type, vr0.max);
2473 else
2474 max = TYPE_MAX_VALUE (type);
2475
2476 cmp = compare_values (min, max);
2477
2478 /* If a VR_ANTI_RANGEs contains zero, then we have
2479 ~[-INF, min(MIN, MAX)]. */
2480 if (vr0.type == VR_ANTI_RANGE)
2481 {
2482 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
2483 {
2484 /* Take the lower of the two values. */
2485 if (cmp != 1)
2486 max = min;
2487
2488 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2489 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2490 flag_wrapv is set and the original anti-range doesn't include
2491 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2492 if (TYPE_OVERFLOW_WRAPS (type))
2493 {
2494 tree type_min_value = TYPE_MIN_VALUE (type);
2495
2496 min = (vr0.min != type_min_value
2497 ? int_const_binop (PLUS_EXPR, type_min_value,
2498 build_int_cst (TREE_TYPE (type_min_value), 1))
2499 : type_min_value);
2500 }
2501 else
2502 min = TYPE_MIN_VALUE (type);
2503 }
2504 else
2505 {
2506 /* All else has failed, so create the range [0, INF], even for
2507 flag_wrapv since TYPE_MIN_VALUE is in the original
2508 anti-range. */
2509 vr0.type = VR_RANGE;
2510 min = build_int_cst (type, 0);
2511 max = TYPE_MAX_VALUE (type);
2512 }
2513 }
2514
2515 /* If the range contains zero then we know that the minimum value in the
2516 range will be zero. */
2517 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
2518 {
2519 if (cmp == 1)
2520 max = min;
2521 min = build_int_cst (type, 0);
2522 }
2523 else
2524 {
2525 /* If the range was reversed, swap MIN and MAX. */
2526 if (cmp == 1)
2527 std::swap (min, max);
2528 }
2529
2530 cmp = compare_values (min, max);
2531 if (cmp == -2 || cmp == 1)
2532 {
2533 /* If the new range has its limits swapped around (MIN > MAX),
2534 then the operation caused one of them to wrap around, mark
2535 the new range VARYING. */
2536 set_value_range_to_varying (vr);
2537 }
2538 else
2539 set_value_range (vr, vr0.type, min, max, NULL);
2540 return;
2541 }
2542
2543 /* For unhandled operations fall back to varying. */
2544 set_value_range_to_varying (vr);
2545 return;
2546 }
2547
2548 /* Debugging dumps. */
2549
2550 void dump_value_range (FILE *, const value_range *);
2551 void debug_value_range (value_range *);
2552 void dump_all_value_ranges (FILE *);
2553 void dump_vr_equiv (FILE *, bitmap);
2554 void debug_vr_equiv (bitmap);
2555
2556
2557 /* Dump value range VR to FILE. */
2558
2559 void
2560 dump_value_range (FILE *file, const value_range *vr)
2561 {
2562 if (vr == NULL)
2563 fprintf (file, "[]");
2564 else if (vr->type == VR_UNDEFINED)
2565 fprintf (file, "UNDEFINED");
2566 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
2567 {
2568 tree type = TREE_TYPE (vr->min);
2569
2570 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
2571
2572 if (INTEGRAL_TYPE_P (type)
2573 && !TYPE_UNSIGNED (type)
2574 && vrp_val_is_min (vr->min))
2575 fprintf (file, "-INF");
2576 else
2577 print_generic_expr (file, vr->min);
2578
2579 fprintf (file, ", ");
2580
2581 if (INTEGRAL_TYPE_P (type)
2582 && vrp_val_is_max (vr->max))
2583 fprintf (file, "+INF");
2584 else
2585 print_generic_expr (file, vr->max);
2586
2587 fprintf (file, "]");
2588
2589 if (vr->equiv)
2590 {
2591 bitmap_iterator bi;
2592 unsigned i, c = 0;
2593
2594 fprintf (file, " EQUIVALENCES: { ");
2595
2596 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
2597 {
2598 print_generic_expr (file, ssa_name (i));
2599 fprintf (file, " ");
2600 c++;
2601 }
2602
2603 fprintf (file, "} (%u elements)", c);
2604 }
2605 }
2606 else if (vr->type == VR_VARYING)
2607 fprintf (file, "VARYING");
2608 else
2609 fprintf (file, "INVALID RANGE");
2610 }
2611
2612
2613 /* Dump value range VR to stderr. */
2614
2615 DEBUG_FUNCTION void
2616 debug_value_range (value_range *vr)
2617 {
2618 dump_value_range (stderr, vr);
2619 fprintf (stderr, "\n");
2620 }
2621
2622
2623 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2624 create a new SSA name N and return the assertion assignment
2625 'N = ASSERT_EXPR <V, V OP W>'. */
2626
2627 static gimple *
2628 build_assert_expr_for (tree cond, tree v)
2629 {
2630 tree a;
2631 gassign *assertion;
2632
2633 gcc_assert (TREE_CODE (v) == SSA_NAME
2634 && COMPARISON_CLASS_P (cond));
2635
2636 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
2637 assertion = gimple_build_assign (NULL_TREE, a);
2638
2639 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2640 operand of the ASSERT_EXPR. Create it so the new name and the old one
2641 are registered in the replacement table so that we can fix the SSA web
2642 after adding all the ASSERT_EXPRs. */
2643 tree new_def = create_new_def_for (v, assertion, NULL);
2644 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2645 given we have to be able to fully propagate those out to re-create
2646 valid SSA when removing the asserts. */
2647 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v))
2648 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def) = 1;
2649
2650 return assertion;
2651 }
2652
2653
2654 /* Return false if EXPR is a predicate expression involving floating
2655 point values. */
2656
2657 static inline bool
2658 fp_predicate (gimple *stmt)
2659 {
2660 GIMPLE_CHECK (stmt, GIMPLE_COND);
2661
2662 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
2663 }
2664
2665 /* If the range of values taken by OP can be inferred after STMT executes,
2666 return the comparison code (COMP_CODE_P) and value (VAL_P) that
2667 describes the inferred range. Return true if a range could be
2668 inferred. */
2669
2670 bool
2671 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
2672 {
2673 *val_p = NULL_TREE;
2674 *comp_code_p = ERROR_MARK;
2675
2676 /* Do not attempt to infer anything in names that flow through
2677 abnormal edges. */
2678 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
2679 return false;
2680
2681 /* If STMT is the last statement of a basic block with no normal
2682 successors, there is no point inferring anything about any of its
2683 operands. We would not be able to find a proper insertion point
2684 for the assertion, anyway. */
2685 if (stmt_ends_bb_p (stmt))
2686 {
2687 edge_iterator ei;
2688 edge e;
2689
2690 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
2691 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
2692 break;
2693 if (e == NULL)
2694 return false;
2695 }
2696
2697 if (infer_nonnull_range (stmt, op))
2698 {
2699 *val_p = build_int_cst (TREE_TYPE (op), 0);
2700 *comp_code_p = NE_EXPR;
2701 return true;
2702 }
2703
2704 return false;
2705 }
2706
2707
2708 void dump_asserts_for (FILE *, tree);
2709 void debug_asserts_for (tree);
2710 void dump_all_asserts (FILE *);
2711 void debug_all_asserts (void);
2712
2713 /* Dump all the registered assertions for NAME to FILE. */
2714
2715 void
2716 dump_asserts_for (FILE *file, tree name)
2717 {
2718 assert_locus *loc;
2719
2720 fprintf (file, "Assertions to be inserted for ");
2721 print_generic_expr (file, name);
2722 fprintf (file, "\n");
2723
2724 loc = asserts_for[SSA_NAME_VERSION (name)];
2725 while (loc)
2726 {
2727 fprintf (file, "\t");
2728 print_gimple_stmt (file, gsi_stmt (loc->si), 0);
2729 fprintf (file, "\n\tBB #%d", loc->bb->index);
2730 if (loc->e)
2731 {
2732 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
2733 loc->e->dest->index);
2734 dump_edge_info (file, loc->e, dump_flags, 0);
2735 }
2736 fprintf (file, "\n\tPREDICATE: ");
2737 print_generic_expr (file, loc->expr);
2738 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
2739 print_generic_expr (file, loc->val);
2740 fprintf (file, "\n\n");
2741 loc = loc->next;
2742 }
2743
2744 fprintf (file, "\n");
2745 }
2746
2747
2748 /* Dump all the registered assertions for NAME to stderr. */
2749
2750 DEBUG_FUNCTION void
2751 debug_asserts_for (tree name)
2752 {
2753 dump_asserts_for (stderr, name);
2754 }
2755
2756
2757 /* Dump all the registered assertions for all the names to FILE. */
2758
2759 void
2760 dump_all_asserts (FILE *file)
2761 {
2762 unsigned i;
2763 bitmap_iterator bi;
2764
2765 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
2766 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
2767 dump_asserts_for (file, ssa_name (i));
2768 fprintf (file, "\n");
2769 }
2770
2771
2772 /* Dump all the registered assertions for all the names to stderr. */
2773
2774 DEBUG_FUNCTION void
2775 debug_all_asserts (void)
2776 {
2777 dump_all_asserts (stderr);
2778 }
2779
2780 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2781
2782 static void
2783 add_assert_info (vec<assert_info> &asserts,
2784 tree name, tree expr, enum tree_code comp_code, tree val)
2785 {
2786 assert_info info;
2787 info.comp_code = comp_code;
2788 info.name = name;
2789 if (TREE_OVERFLOW_P (val))
2790 val = drop_tree_overflow (val);
2791 info.val = val;
2792 info.expr = expr;
2793 asserts.safe_push (info);
2794 }
2795
2796 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2797 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2798 E->DEST, then register this location as a possible insertion point
2799 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2800
2801 BB, E and SI provide the exact insertion point for the new
2802 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2803 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2804 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2805 must not be NULL. */
2806
2807 static void
2808 register_new_assert_for (tree name, tree expr,
2809 enum tree_code comp_code,
2810 tree val,
2811 basic_block bb,
2812 edge e,
2813 gimple_stmt_iterator si)
2814 {
2815 assert_locus *n, *loc, *last_loc;
2816 basic_block dest_bb;
2817
2818 gcc_checking_assert (bb == NULL || e == NULL);
2819
2820 if (e == NULL)
2821 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
2822 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
2823
2824 /* Never build an assert comparing against an integer constant with
2825 TREE_OVERFLOW set. This confuses our undefined overflow warning
2826 machinery. */
2827 if (TREE_OVERFLOW_P (val))
2828 val = drop_tree_overflow (val);
2829
2830 /* The new assertion A will be inserted at BB or E. We need to
2831 determine if the new location is dominated by a previously
2832 registered location for A. If we are doing an edge insertion,
2833 assume that A will be inserted at E->DEST. Note that this is not
2834 necessarily true.
2835
2836 If E is a critical edge, it will be split. But even if E is
2837 split, the new block will dominate the same set of blocks that
2838 E->DEST dominates.
2839
2840 The reverse, however, is not true, blocks dominated by E->DEST
2841 will not be dominated by the new block created to split E. So,
2842 if the insertion location is on a critical edge, we will not use
2843 the new location to move another assertion previously registered
2844 at a block dominated by E->DEST. */
2845 dest_bb = (bb) ? bb : e->dest;
2846
2847 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2848 VAL at a block dominating DEST_BB, then we don't need to insert a new
2849 one. Similarly, if the same assertion already exists at a block
2850 dominated by DEST_BB and the new location is not on a critical
2851 edge, then update the existing location for the assertion (i.e.,
2852 move the assertion up in the dominance tree).
2853
2854 Note, this is implemented as a simple linked list because there
2855 should not be more than a handful of assertions registered per
2856 name. If this becomes a performance problem, a table hashed by
2857 COMP_CODE and VAL could be implemented. */
2858 loc = asserts_for[SSA_NAME_VERSION (name)];
2859 last_loc = loc;
2860 while (loc)
2861 {
2862 if (loc->comp_code == comp_code
2863 && (loc->val == val
2864 || operand_equal_p (loc->val, val, 0))
2865 && (loc->expr == expr
2866 || operand_equal_p (loc->expr, expr, 0)))
2867 {
2868 /* If E is not a critical edge and DEST_BB
2869 dominates the existing location for the assertion, move
2870 the assertion up in the dominance tree by updating its
2871 location information. */
2872 if ((e == NULL || !EDGE_CRITICAL_P (e))
2873 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
2874 {
2875 loc->bb = dest_bb;
2876 loc->e = e;
2877 loc->si = si;
2878 return;
2879 }
2880 }
2881
2882 /* Update the last node of the list and move to the next one. */
2883 last_loc = loc;
2884 loc = loc->next;
2885 }
2886
2887 /* If we didn't find an assertion already registered for
2888 NAME COMP_CODE VAL, add a new one at the end of the list of
2889 assertions associated with NAME. */
2890 n = XNEW (struct assert_locus);
2891 n->bb = dest_bb;
2892 n->e = e;
2893 n->si = si;
2894 n->comp_code = comp_code;
2895 n->val = val;
2896 n->expr = expr;
2897 n->next = NULL;
2898
2899 if (last_loc)
2900 last_loc->next = n;
2901 else
2902 asserts_for[SSA_NAME_VERSION (name)] = n;
2903
2904 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
2905 }
2906
2907 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
2908 Extract a suitable test code and value and store them into *CODE_P and
2909 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
2910
2911 If no extraction was possible, return FALSE, otherwise return TRUE.
2912
2913 If INVERT is true, then we invert the result stored into *CODE_P. */
2914
2915 static bool
2916 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
2917 tree cond_op0, tree cond_op1,
2918 bool invert, enum tree_code *code_p,
2919 tree *val_p)
2920 {
2921 enum tree_code comp_code;
2922 tree val;
2923
2924 /* Otherwise, we have a comparison of the form NAME COMP VAL
2925 or VAL COMP NAME. */
2926 if (name == cond_op1)
2927 {
2928 /* If the predicate is of the form VAL COMP NAME, flip
2929 COMP around because we need to register NAME as the
2930 first operand in the predicate. */
2931 comp_code = swap_tree_comparison (cond_code);
2932 val = cond_op0;
2933 }
2934 else if (name == cond_op0)
2935 {
2936 /* The comparison is of the form NAME COMP VAL, so the
2937 comparison code remains unchanged. */
2938 comp_code = cond_code;
2939 val = cond_op1;
2940 }
2941 else
2942 gcc_unreachable ();
2943
2944 /* Invert the comparison code as necessary. */
2945 if (invert)
2946 comp_code = invert_tree_comparison (comp_code, 0);
2947
2948 /* VRP only handles integral and pointer types. */
2949 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
2950 && ! POINTER_TYPE_P (TREE_TYPE (val)))
2951 return false;
2952
2953 /* Do not register always-false predicates.
2954 FIXME: this works around a limitation in fold() when dealing with
2955 enumerations. Given 'enum { N1, N2 } x;', fold will not
2956 fold 'if (x > N2)' to 'if (0)'. */
2957 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
2958 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2959 {
2960 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
2961 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
2962
2963 if (comp_code == GT_EXPR
2964 && (!max
2965 || compare_values (val, max) == 0))
2966 return false;
2967
2968 if (comp_code == LT_EXPR
2969 && (!min
2970 || compare_values (val, min) == 0))
2971 return false;
2972 }
2973 *code_p = comp_code;
2974 *val_p = val;
2975 return true;
2976 }
2977
2978 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
2979 (otherwise return VAL). VAL and MASK must be zero-extended for
2980 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
2981 (to transform signed values into unsigned) and at the end xor
2982 SGNBIT back. */
2983
2984 static wide_int
2985 masked_increment (const wide_int &val_in, const wide_int &mask,
2986 const wide_int &sgnbit, unsigned int prec)
2987 {
2988 wide_int bit = wi::one (prec), res;
2989 unsigned int i;
2990
2991 wide_int val = val_in ^ sgnbit;
2992 for (i = 0; i < prec; i++, bit += bit)
2993 {
2994 res = mask;
2995 if ((res & bit) == 0)
2996 continue;
2997 res = bit - 1;
2998 res = wi::bit_and_not (val + bit, res);
2999 res &= mask;
3000 if (wi::gtu_p (res, val))
3001 return res ^ sgnbit;
3002 }
3003 return val ^ sgnbit;
3004 }
3005
3006 /* Helper for overflow_comparison_p
3007
3008 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3009 OP1's defining statement to see if it ultimately has the form
3010 OP0 CODE (OP0 PLUS INTEGER_CST)
3011
3012 If so, return TRUE indicating this is an overflow test and store into
3013 *NEW_CST an updated constant that can be used in a narrowed range test.
3014
3015 REVERSED indicates if the comparison was originally:
3016
3017 OP1 CODE' OP0.
3018
3019 This affects how we build the updated constant. */
3020
3021 static bool
3022 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
3023 bool follow_assert_exprs, bool reversed, tree *new_cst)
3024 {
3025 /* See if this is a relational operation between two SSA_NAMES with
3026 unsigned, overflow wrapping values. If so, check it more deeply. */
3027 if ((code == LT_EXPR || code == LE_EXPR
3028 || code == GE_EXPR || code == GT_EXPR)
3029 && TREE_CODE (op0) == SSA_NAME
3030 && TREE_CODE (op1) == SSA_NAME
3031 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
3032 && TYPE_UNSIGNED (TREE_TYPE (op0))
3033 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
3034 {
3035 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
3036
3037 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
3038 if (follow_assert_exprs)
3039 {
3040 while (gimple_assign_single_p (op1_def)
3041 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
3042 {
3043 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
3044 if (TREE_CODE (op1) != SSA_NAME)
3045 break;
3046 op1_def = SSA_NAME_DEF_STMT (op1);
3047 }
3048 }
3049
3050 /* Now look at the defining statement of OP1 to see if it adds
3051 or subtracts a nonzero constant from another operand. */
3052 if (op1_def
3053 && is_gimple_assign (op1_def)
3054 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
3055 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
3056 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
3057 {
3058 tree target = gimple_assign_rhs1 (op1_def);
3059
3060 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
3061 for one where TARGET appears on the RHS. */
3062 if (follow_assert_exprs)
3063 {
3064 /* Now see if that "other operand" is op0, following the chain
3065 of ASSERT_EXPRs if necessary. */
3066 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
3067 while (op0 != target
3068 && gimple_assign_single_p (op0_def)
3069 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
3070 {
3071 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
3072 if (TREE_CODE (op0) != SSA_NAME)
3073 break;
3074 op0_def = SSA_NAME_DEF_STMT (op0);
3075 }
3076 }
3077
3078 /* If we did not find our target SSA_NAME, then this is not
3079 an overflow test. */
3080 if (op0 != target)
3081 return false;
3082
3083 tree type = TREE_TYPE (op0);
3084 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
3085 tree inc = gimple_assign_rhs2 (op1_def);
3086 if (reversed)
3087 *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
3088 else
3089 *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
3090 return true;
3091 }
3092 }
3093 return false;
3094 }
3095
3096 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
3097 OP1's defining statement to see if it ultimately has the form
3098 OP0 CODE (OP0 PLUS INTEGER_CST)
3099
3100 If so, return TRUE indicating this is an overflow test and store into
3101 *NEW_CST an updated constant that can be used in a narrowed range test.
3102
3103 These statements are left as-is in the IL to facilitate discovery of
3104 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
3105 the alternate range representation is often useful within VRP. */
3106
3107 bool
3108 overflow_comparison_p (tree_code code, tree name, tree val,
3109 bool use_equiv_p, tree *new_cst)
3110 {
3111 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
3112 return true;
3113 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
3114 use_equiv_p, true, new_cst);
3115 }
3116
3117
3118 /* Try to register an edge assertion for SSA name NAME on edge E for
3119 the condition COND contributing to the conditional jump pointed to by BSI.
3120 Invert the condition COND if INVERT is true. */
3121
3122 static void
3123 register_edge_assert_for_2 (tree name, edge e,
3124 enum tree_code cond_code,
3125 tree cond_op0, tree cond_op1, bool invert,
3126 vec<assert_info> &asserts)
3127 {
3128 tree val;
3129 enum tree_code comp_code;
3130
3131 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
3132 cond_op0,
3133 cond_op1,
3134 invert, &comp_code, &val))
3135 return;
3136
3137 /* Queue the assert. */
3138 tree x;
3139 if (overflow_comparison_p (comp_code, name, val, false, &x))
3140 {
3141 enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
3142 ? GT_EXPR : LE_EXPR);
3143 add_assert_info (asserts, name, name, new_code, x);
3144 }
3145 add_assert_info (asserts, name, name, comp_code, val);
3146
3147 /* In the case of NAME <= CST and NAME being defined as
3148 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
3149 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
3150 This catches range and anti-range tests. */
3151 if ((comp_code == LE_EXPR
3152 || comp_code == GT_EXPR)
3153 && TREE_CODE (val) == INTEGER_CST
3154 && TYPE_UNSIGNED (TREE_TYPE (val)))
3155 {
3156 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3157 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
3158
3159 /* Extract CST2 from the (optional) addition. */
3160 if (is_gimple_assign (def_stmt)
3161 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
3162 {
3163 name2 = gimple_assign_rhs1 (def_stmt);
3164 cst2 = gimple_assign_rhs2 (def_stmt);
3165 if (TREE_CODE (name2) == SSA_NAME
3166 && TREE_CODE (cst2) == INTEGER_CST)
3167 def_stmt = SSA_NAME_DEF_STMT (name2);
3168 }
3169
3170 /* Extract NAME2 from the (optional) sign-changing cast. */
3171 if (gimple_assign_cast_p (def_stmt))
3172 {
3173 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
3174 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
3175 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
3176 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
3177 name3 = gimple_assign_rhs1 (def_stmt);
3178 }
3179
3180 /* If name3 is used later, create an ASSERT_EXPR for it. */
3181 if (name3 != NULL_TREE
3182 && TREE_CODE (name3) == SSA_NAME
3183 && (cst2 == NULL_TREE
3184 || TREE_CODE (cst2) == INTEGER_CST)
3185 && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
3186 {
3187 tree tmp;
3188
3189 /* Build an expression for the range test. */
3190 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
3191 if (cst2 != NULL_TREE)
3192 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
3193
3194 if (dump_file)
3195 {
3196 fprintf (dump_file, "Adding assert for ");
3197 print_generic_expr (dump_file, name3);
3198 fprintf (dump_file, " from ");
3199 print_generic_expr (dump_file, tmp);
3200 fprintf (dump_file, "\n");
3201 }
3202
3203 add_assert_info (asserts, name3, tmp, comp_code, val);
3204 }
3205
3206 /* If name2 is used later, create an ASSERT_EXPR for it. */
3207 if (name2 != NULL_TREE
3208 && TREE_CODE (name2) == SSA_NAME
3209 && TREE_CODE (cst2) == INTEGER_CST
3210 && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
3211 {
3212 tree tmp;
3213
3214 /* Build an expression for the range test. */
3215 tmp = name2;
3216 if (TREE_TYPE (name) != TREE_TYPE (name2))
3217 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
3218 if (cst2 != NULL_TREE)
3219 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
3220
3221 if (dump_file)
3222 {
3223 fprintf (dump_file, "Adding assert for ");
3224 print_generic_expr (dump_file, name2);
3225 fprintf (dump_file, " from ");
3226 print_generic_expr (dump_file, tmp);
3227 fprintf (dump_file, "\n");
3228 }
3229
3230 add_assert_info (asserts, name2, tmp, comp_code, val);
3231 }
3232 }
3233
3234 /* In the case of post-in/decrement tests like if (i++) ... and uses
3235 of the in/decremented value on the edge the extra name we want to
3236 assert for is not on the def chain of the name compared. Instead
3237 it is in the set of use stmts.
3238 Similar cases happen for conversions that were simplified through
3239 fold_{sign_changed,widened}_comparison. */
3240 if ((comp_code == NE_EXPR
3241 || comp_code == EQ_EXPR)
3242 && TREE_CODE (val) == INTEGER_CST)
3243 {
3244 imm_use_iterator ui;
3245 gimple *use_stmt;
3246 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
3247 {
3248 if (!is_gimple_assign (use_stmt))
3249 continue;
3250
3251 /* Cut off to use-stmts that are dominating the predecessor. */
3252 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
3253 continue;
3254
3255 tree name2 = gimple_assign_lhs (use_stmt);
3256 if (TREE_CODE (name2) != SSA_NAME)
3257 continue;
3258
3259 enum tree_code code = gimple_assign_rhs_code (use_stmt);
3260 tree cst;
3261 if (code == PLUS_EXPR
3262 || code == MINUS_EXPR)
3263 {
3264 cst = gimple_assign_rhs2 (use_stmt);
3265 if (TREE_CODE (cst) != INTEGER_CST)
3266 continue;
3267 cst = int_const_binop (code, val, cst);
3268 }
3269 else if (CONVERT_EXPR_CODE_P (code))
3270 {
3271 /* For truncating conversions we cannot record
3272 an inequality. */
3273 if (comp_code == NE_EXPR
3274 && (TYPE_PRECISION (TREE_TYPE (name2))
3275 < TYPE_PRECISION (TREE_TYPE (name))))
3276 continue;
3277 cst = fold_convert (TREE_TYPE (name2), val);
3278 }
3279 else
3280 continue;
3281
3282 if (TREE_OVERFLOW_P (cst))
3283 cst = drop_tree_overflow (cst);
3284 add_assert_info (asserts, name2, name2, comp_code, cst);
3285 }
3286 }
3287
3288 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
3289 && TREE_CODE (val) == INTEGER_CST)
3290 {
3291 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3292 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
3293 tree val2 = NULL_TREE;
3294 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
3295 wide_int mask = wi::zero (prec);
3296 unsigned int nprec = prec;
3297 enum tree_code rhs_code = ERROR_MARK;
3298
3299 if (is_gimple_assign (def_stmt))
3300 rhs_code = gimple_assign_rhs_code (def_stmt);
3301
3302 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
3303 assert that A != CST1 -+ CST2. */
3304 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
3305 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
3306 {
3307 tree op0 = gimple_assign_rhs1 (def_stmt);
3308 tree op1 = gimple_assign_rhs2 (def_stmt);
3309 if (TREE_CODE (op0) == SSA_NAME
3310 && TREE_CODE (op1) == INTEGER_CST)
3311 {
3312 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
3313 ? MINUS_EXPR : PLUS_EXPR);
3314 op1 = int_const_binop (reverse_op, val, op1);
3315 if (TREE_OVERFLOW (op1))
3316 op1 = drop_tree_overflow (op1);
3317 add_assert_info (asserts, op0, op0, comp_code, op1);
3318 }
3319 }
3320
3321 /* Add asserts for NAME cmp CST and NAME being defined
3322 as NAME = (int) NAME2. */
3323 if (!TYPE_UNSIGNED (TREE_TYPE (val))
3324 && (comp_code == LE_EXPR || comp_code == LT_EXPR
3325 || comp_code == GT_EXPR || comp_code == GE_EXPR)
3326 && gimple_assign_cast_p (def_stmt))
3327 {
3328 name2 = gimple_assign_rhs1 (def_stmt);
3329 if (CONVERT_EXPR_CODE_P (rhs_code)
3330 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
3331 && TYPE_UNSIGNED (TREE_TYPE (name2))
3332 && prec == TYPE_PRECISION (TREE_TYPE (name2))
3333 && (comp_code == LE_EXPR || comp_code == GT_EXPR
3334 || !tree_int_cst_equal (val,
3335 TYPE_MIN_VALUE (TREE_TYPE (val)))))
3336 {
3337 tree tmp, cst;
3338 enum tree_code new_comp_code = comp_code;
3339
3340 cst = fold_convert (TREE_TYPE (name2),
3341 TYPE_MIN_VALUE (TREE_TYPE (val)));
3342 /* Build an expression for the range test. */
3343 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
3344 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
3345 fold_convert (TREE_TYPE (name2), val));
3346 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
3347 {
3348 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
3349 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
3350 build_int_cst (TREE_TYPE (name2), 1));
3351 }
3352
3353 if (dump_file)
3354 {
3355 fprintf (dump_file, "Adding assert for ");
3356 print_generic_expr (dump_file, name2);
3357 fprintf (dump_file, " from ");
3358 print_generic_expr (dump_file, tmp);
3359 fprintf (dump_file, "\n");
3360 }
3361
3362 add_assert_info (asserts, name2, tmp, new_comp_code, cst);
3363 }
3364 }
3365
3366 /* Add asserts for NAME cmp CST and NAME being defined as
3367 NAME = NAME2 >> CST2.
3368
3369 Extract CST2 from the right shift. */
3370 if (rhs_code == RSHIFT_EXPR)
3371 {
3372 name2 = gimple_assign_rhs1 (def_stmt);
3373 cst2 = gimple_assign_rhs2 (def_stmt);
3374 if (TREE_CODE (name2) == SSA_NAME
3375 && tree_fits_uhwi_p (cst2)
3376 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
3377 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
3378 && type_has_mode_precision_p (TREE_TYPE (val)))
3379 {
3380 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
3381 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
3382 }
3383 }
3384 if (val2 != NULL_TREE
3385 && TREE_CODE (val2) == INTEGER_CST
3386 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
3387 TREE_TYPE (val),
3388 val2, cst2), val))
3389 {
3390 enum tree_code new_comp_code = comp_code;
3391 tree tmp, new_val;
3392
3393 tmp = name2;
3394 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
3395 {
3396 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
3397 {
3398 tree type = build_nonstandard_integer_type (prec, 1);
3399 tmp = build1 (NOP_EXPR, type, name2);
3400 val2 = fold_convert (type, val2);
3401 }
3402 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
3403 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
3404 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
3405 }
3406 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
3407 {
3408 wide_int minval
3409 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
3410 new_val = val2;
3411 if (minval == wi::to_wide (new_val))
3412 new_val = NULL_TREE;
3413 }
3414 else
3415 {
3416 wide_int maxval
3417 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
3418 mask |= wi::to_wide (val2);
3419 if (wi::eq_p (mask, maxval))
3420 new_val = NULL_TREE;
3421 else
3422 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
3423 }
3424
3425 if (new_val)
3426 {
3427 if (dump_file)
3428 {
3429 fprintf (dump_file, "Adding assert for ");
3430 print_generic_expr (dump_file, name2);
3431 fprintf (dump_file, " from ");
3432 print_generic_expr (dump_file, tmp);
3433 fprintf (dump_file, "\n");
3434 }
3435
3436 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
3437 }
3438 }
3439
3440 /* Add asserts for NAME cmp CST and NAME being defined as
3441 NAME = NAME2 & CST2.
3442
3443 Extract CST2 from the and.
3444
3445 Also handle
3446 NAME = (unsigned) NAME2;
3447 casts where NAME's type is unsigned and has smaller precision
3448 than NAME2's type as if it was NAME = NAME2 & MASK. */
3449 names[0] = NULL_TREE;
3450 names[1] = NULL_TREE;
3451 cst2 = NULL_TREE;
3452 if (rhs_code == BIT_AND_EXPR
3453 || (CONVERT_EXPR_CODE_P (rhs_code)
3454 && INTEGRAL_TYPE_P (TREE_TYPE (val))
3455 && TYPE_UNSIGNED (TREE_TYPE (val))
3456 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
3457 > prec))
3458 {
3459 name2 = gimple_assign_rhs1 (def_stmt);
3460 if (rhs_code == BIT_AND_EXPR)
3461 cst2 = gimple_assign_rhs2 (def_stmt);
3462 else
3463 {
3464 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
3465 nprec = TYPE_PRECISION (TREE_TYPE (name2));
3466 }
3467 if (TREE_CODE (name2) == SSA_NAME
3468 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
3469 && TREE_CODE (cst2) == INTEGER_CST
3470 && !integer_zerop (cst2)
3471 && (nprec > 1
3472 || TYPE_UNSIGNED (TREE_TYPE (val))))
3473 {
3474 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
3475 if (gimple_assign_cast_p (def_stmt2))
3476 {
3477 names[1] = gimple_assign_rhs1 (def_stmt2);
3478 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
3479 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
3480 || (TYPE_PRECISION (TREE_TYPE (name2))
3481 != TYPE_PRECISION (TREE_TYPE (names[1]))))
3482 names[1] = NULL_TREE;
3483 }
3484 names[0] = name2;
3485 }
3486 }
3487 if (names[0] || names[1])
3488 {
3489 wide_int minv, maxv, valv, cst2v;
3490 wide_int tem, sgnbit;
3491 bool valid_p = false, valn, cst2n;
3492 enum tree_code ccode = comp_code;
3493
3494 valv = wide_int::from (wi::to_wide (val), nprec, UNSIGNED);
3495 cst2v = wide_int::from (wi::to_wide (cst2), nprec, UNSIGNED);
3496 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
3497 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
3498 /* If CST2 doesn't have most significant bit set,
3499 but VAL is negative, we have comparison like
3500 if ((x & 0x123) > -4) (always true). Just give up. */
3501 if (!cst2n && valn)
3502 ccode = ERROR_MARK;
3503 if (cst2n)
3504 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3505 else
3506 sgnbit = wi::zero (nprec);
3507 minv = valv & cst2v;
3508 switch (ccode)
3509 {
3510 case EQ_EXPR:
3511 /* Minimum unsigned value for equality is VAL & CST2
3512 (should be equal to VAL, otherwise we probably should
3513 have folded the comparison into false) and
3514 maximum unsigned value is VAL | ~CST2. */
3515 maxv = valv | ~cst2v;
3516 valid_p = true;
3517 break;
3518
3519 case NE_EXPR:
3520 tem = valv | ~cst2v;
3521 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
3522 if (valv == 0)
3523 {
3524 cst2n = false;
3525 sgnbit = wi::zero (nprec);
3526 goto gt_expr;
3527 }
3528 /* If (VAL | ~CST2) is all ones, handle it as
3529 (X & CST2) < VAL. */
3530 if (tem == -1)
3531 {
3532 cst2n = false;
3533 valn = false;
3534 sgnbit = wi::zero (nprec);
3535 goto lt_expr;
3536 }
3537 if (!cst2n && wi::neg_p (cst2v))
3538 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3539 if (sgnbit != 0)
3540 {
3541 if (valv == sgnbit)
3542 {
3543 cst2n = true;
3544 valn = true;
3545 goto gt_expr;
3546 }
3547 if (tem == wi::mask (nprec - 1, false, nprec))
3548 {
3549 cst2n = true;
3550 goto lt_expr;
3551 }
3552 if (!cst2n)
3553 sgnbit = wi::zero (nprec);
3554 }
3555 break;
3556
3557 case GE_EXPR:
3558 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
3559 is VAL and maximum unsigned value is ~0. For signed
3560 comparison, if CST2 doesn't have most significant bit
3561 set, handle it similarly. If CST2 has MSB set,
3562 the minimum is the same, and maximum is ~0U/2. */
3563 if (minv != valv)
3564 {
3565 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
3566 VAL. */
3567 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3568 if (minv == valv)
3569 break;
3570 }
3571 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3572 valid_p = true;
3573 break;
3574
3575 case GT_EXPR:
3576 gt_expr:
3577 /* Find out smallest MINV where MINV > VAL
3578 && (MINV & CST2) == MINV, if any. If VAL is signed and
3579 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
3580 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3581 if (minv == valv)
3582 break;
3583 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3584 valid_p = true;
3585 break;
3586
3587 case LE_EXPR:
3588 /* Minimum unsigned value for <= is 0 and maximum
3589 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
3590 Otherwise, find smallest VAL2 where VAL2 > VAL
3591 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3592 as maximum.
3593 For signed comparison, if CST2 doesn't have most
3594 significant bit set, handle it similarly. If CST2 has
3595 MSB set, the maximum is the same and minimum is INT_MIN. */
3596 if (minv == valv)
3597 maxv = valv;
3598 else
3599 {
3600 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3601 if (maxv == valv)
3602 break;
3603 maxv -= 1;
3604 }
3605 maxv |= ~cst2v;
3606 minv = sgnbit;
3607 valid_p = true;
3608 break;
3609
3610 case LT_EXPR:
3611 lt_expr:
3612 /* Minimum unsigned value for < is 0 and maximum
3613 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
3614 Otherwise, find smallest VAL2 where VAL2 > VAL
3615 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3616 as maximum.
3617 For signed comparison, if CST2 doesn't have most
3618 significant bit set, handle it similarly. If CST2 has
3619 MSB set, the maximum is the same and minimum is INT_MIN. */
3620 if (minv == valv)
3621 {
3622 if (valv == sgnbit)
3623 break;
3624 maxv = valv;
3625 }
3626 else
3627 {
3628 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3629 if (maxv == valv)
3630 break;
3631 }
3632 maxv -= 1;
3633 maxv |= ~cst2v;
3634 minv = sgnbit;
3635 valid_p = true;
3636 break;
3637
3638 default:
3639 break;
3640 }
3641 if (valid_p
3642 && (maxv - minv) != -1)
3643 {
3644 tree tmp, new_val, type;
3645 int i;
3646
3647 for (i = 0; i < 2; i++)
3648 if (names[i])
3649 {
3650 wide_int maxv2 = maxv;
3651 tmp = names[i];
3652 type = TREE_TYPE (names[i]);
3653 if (!TYPE_UNSIGNED (type))
3654 {
3655 type = build_nonstandard_integer_type (nprec, 1);
3656 tmp = build1 (NOP_EXPR, type, names[i]);
3657 }
3658 if (minv != 0)
3659 {
3660 tmp = build2 (PLUS_EXPR, type, tmp,
3661 wide_int_to_tree (type, -minv));
3662 maxv2 = maxv - minv;
3663 }
3664 new_val = wide_int_to_tree (type, maxv2);
3665
3666 if (dump_file)
3667 {
3668 fprintf (dump_file, "Adding assert for ");
3669 print_generic_expr (dump_file, names[i]);
3670 fprintf (dump_file, " from ");
3671 print_generic_expr (dump_file, tmp);
3672 fprintf (dump_file, "\n");
3673 }
3674
3675 add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
3676 }
3677 }
3678 }
3679 }
3680 }
3681
3682 /* OP is an operand of a truth value expression which is known to have
3683 a particular value. Register any asserts for OP and for any
3684 operands in OP's defining statement.
3685
3686 If CODE is EQ_EXPR, then we want to register OP is zero (false),
3687 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
3688
3689 static void
3690 register_edge_assert_for_1 (tree op, enum tree_code code,
3691 edge e, vec<assert_info> &asserts)
3692 {
3693 gimple *op_def;
3694 tree val;
3695 enum tree_code rhs_code;
3696
3697 /* We only care about SSA_NAMEs. */
3698 if (TREE_CODE (op) != SSA_NAME)
3699 return;
3700
3701 /* We know that OP will have a zero or nonzero value. */
3702 val = build_int_cst (TREE_TYPE (op), 0);
3703 add_assert_info (asserts, op, op, code, val);
3704
3705 /* Now look at how OP is set. If it's set from a comparison,
3706 a truth operation or some bit operations, then we may be able
3707 to register information about the operands of that assignment. */
3708 op_def = SSA_NAME_DEF_STMT (op);
3709 if (gimple_code (op_def) != GIMPLE_ASSIGN)
3710 return;
3711
3712 rhs_code = gimple_assign_rhs_code (op_def);
3713
3714 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
3715 {
3716 bool invert = (code == EQ_EXPR ? true : false);
3717 tree op0 = gimple_assign_rhs1 (op_def);
3718 tree op1 = gimple_assign_rhs2 (op_def);
3719
3720 if (TREE_CODE (op0) == SSA_NAME)
3721 register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
3722 if (TREE_CODE (op1) == SSA_NAME)
3723 register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
3724 }
3725 else if ((code == NE_EXPR
3726 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
3727 || (code == EQ_EXPR
3728 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
3729 {
3730 /* Recurse on each operand. */
3731 tree op0 = gimple_assign_rhs1 (op_def);
3732 tree op1 = gimple_assign_rhs2 (op_def);
3733 if (TREE_CODE (op0) == SSA_NAME
3734 && has_single_use (op0))
3735 register_edge_assert_for_1 (op0, code, e, asserts);
3736 if (TREE_CODE (op1) == SSA_NAME
3737 && has_single_use (op1))
3738 register_edge_assert_for_1 (op1, code, e, asserts);
3739 }
3740 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
3741 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
3742 {
3743 /* Recurse, flipping CODE. */
3744 code = invert_tree_comparison (code, false);
3745 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
3746 }
3747 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
3748 {
3749 /* Recurse through the copy. */
3750 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
3751 }
3752 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
3753 {
3754 /* Recurse through the type conversion, unless it is a narrowing
3755 conversion or conversion from non-integral type. */
3756 tree rhs = gimple_assign_rhs1 (op_def);
3757 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
3758 && (TYPE_PRECISION (TREE_TYPE (rhs))
3759 <= TYPE_PRECISION (TREE_TYPE (op))))
3760 register_edge_assert_for_1 (rhs, code, e, asserts);
3761 }
3762 }
3763
3764 /* Check if comparison
3765 NAME COND_OP INTEGER_CST
3766 has a form of
3767 (X & 11...100..0) COND_OP XX...X00...0
3768 Such comparison can yield assertions like
3769 X >= XX...X00...0
3770 X <= XX...X11...1
3771 in case of COND_OP being NE_EXPR or
3772 X < XX...X00...0
3773 X > XX...X11...1
3774 in case of EQ_EXPR. */
3775
3776 static bool
3777 is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
3778 tree *new_name, tree *low, enum tree_code *low_code,
3779 tree *high, enum tree_code *high_code)
3780 {
3781 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3782
3783 if (!is_gimple_assign (def_stmt)
3784 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
3785 return false;
3786
3787 tree t = gimple_assign_rhs1 (def_stmt);
3788 tree maskt = gimple_assign_rhs2 (def_stmt);
3789 if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
3790 return false;
3791
3792 wi::tree_to_wide_ref mask = wi::to_wide (maskt);
3793 wide_int inv_mask = ~mask;
3794 /* Assume VALT is INTEGER_CST. */
3795 wi::tree_to_wide_ref val = wi::to_wide (valt);
3796
3797 if ((inv_mask & (inv_mask + 1)) != 0
3798 || (val & mask) != val)
3799 return false;
3800
3801 bool is_range = cond_code == EQ_EXPR;
3802
3803 tree type = TREE_TYPE (t);
3804 wide_int min = wi::min_value (type),
3805 max = wi::max_value (type);
3806
3807 if (is_range)
3808 {
3809 *low_code = val == min ? ERROR_MARK : GE_EXPR;
3810 *high_code = val == max ? ERROR_MARK : LE_EXPR;
3811 }
3812 else
3813 {
3814 /* We can still generate assertion if one of alternatives
3815 is known to always be false. */
3816 if (val == min)
3817 {
3818 *low_code = (enum tree_code) 0;
3819 *high_code = GT_EXPR;
3820 }
3821 else if ((val | inv_mask) == max)
3822 {
3823 *low_code = LT_EXPR;
3824 *high_code = (enum tree_code) 0;
3825 }
3826 else
3827 return false;
3828 }
3829
3830 *new_name = t;
3831 *low = wide_int_to_tree (type, val);
3832 *high = wide_int_to_tree (type, val | inv_mask);
3833
3834 if (wi::neg_p (val, TYPE_SIGN (type)))
3835 std::swap (*low, *high);
3836
3837 return true;
3838 }
3839
3840 /* Try to register an edge assertion for SSA name NAME on edge E for
3841 the condition COND contributing to the conditional jump pointed to by
3842 SI. */
3843
3844 void
3845 register_edge_assert_for (tree name, edge e,
3846 enum tree_code cond_code, tree cond_op0,
3847 tree cond_op1, vec<assert_info> &asserts)
3848 {
3849 tree val;
3850 enum tree_code comp_code;
3851 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
3852
3853 /* Do not attempt to infer anything in names that flow through
3854 abnormal edges. */
3855 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3856 return;
3857
3858 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
3859 cond_op0, cond_op1,
3860 is_else_edge,
3861 &comp_code, &val))
3862 return;
3863
3864 /* Register ASSERT_EXPRs for name. */
3865 register_edge_assert_for_2 (name, e, cond_code, cond_op0,
3866 cond_op1, is_else_edge, asserts);
3867
3868
3869 /* If COND is effectively an equality test of an SSA_NAME against
3870 the value zero or one, then we may be able to assert values
3871 for SSA_NAMEs which flow into COND. */
3872
3873 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
3874 statement of NAME we can assert both operands of the BIT_AND_EXPR
3875 have nonzero value. */
3876 if (((comp_code == EQ_EXPR && integer_onep (val))
3877 || (comp_code == NE_EXPR && integer_zerop (val))))
3878 {
3879 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3880
3881 if (is_gimple_assign (def_stmt)
3882 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
3883 {
3884 tree op0 = gimple_assign_rhs1 (def_stmt);
3885 tree op1 = gimple_assign_rhs2 (def_stmt);
3886 register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
3887 register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
3888 }
3889 }
3890
3891 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
3892 statement of NAME we can assert both operands of the BIT_IOR_EXPR
3893 have zero value. */
3894 if (((comp_code == EQ_EXPR && integer_zerop (val))
3895 || (comp_code == NE_EXPR && integer_onep (val))))
3896 {
3897 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
3898
3899 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
3900 necessarily zero value, or if type-precision is one. */
3901 if (is_gimple_assign (def_stmt)
3902 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
3903 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
3904 || comp_code == EQ_EXPR)))
3905 {
3906 tree op0 = gimple_assign_rhs1 (def_stmt);
3907 tree op1 = gimple_assign_rhs2 (def_stmt);
3908 register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
3909 register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
3910 }
3911 }
3912
3913 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
3914 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
3915 && TREE_CODE (val) == INTEGER_CST)
3916 {
3917 enum tree_code low_code, high_code;
3918 tree low, high;
3919 if (is_masked_range_test (name, val, comp_code, &name, &low,
3920 &low_code, &high, &high_code))
3921 {
3922 if (low_code != ERROR_MARK)
3923 register_edge_assert_for_2 (name, e, low_code, name,
3924 low, /*invert*/false, asserts);
3925 if (high_code != ERROR_MARK)
3926 register_edge_assert_for_2 (name, e, high_code, name,
3927 high, /*invert*/false, asserts);
3928 }
3929 }
3930 }
3931
3932 /* Finish found ASSERTS for E and register them at GSI. */
3933
3934 static void
3935 finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
3936 vec<assert_info> &asserts)
3937 {
3938 for (unsigned i = 0; i < asserts.length (); ++i)
3939 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
3940 reachable from E. */
3941 if (live_on_edge (e, asserts[i].name))
3942 register_new_assert_for (asserts[i].name, asserts[i].expr,
3943 asserts[i].comp_code, asserts[i].val,
3944 NULL, e, gsi);
3945 }
3946
3947
3948
3949 /* Determine whether the outgoing edges of BB should receive an
3950 ASSERT_EXPR for each of the operands of BB's LAST statement.
3951 The last statement of BB must be a COND_EXPR.
3952
3953 If any of the sub-graphs rooted at BB have an interesting use of
3954 the predicate operands, an assert location node is added to the
3955 list of assertions for the corresponding operands. */
3956
3957 static void
3958 find_conditional_asserts (basic_block bb, gcond *last)
3959 {
3960 gimple_stmt_iterator bsi;
3961 tree op;
3962 edge_iterator ei;
3963 edge e;
3964 ssa_op_iter iter;
3965
3966 bsi = gsi_for_stmt (last);
3967
3968 /* Look for uses of the operands in each of the sub-graphs
3969 rooted at BB. We need to check each of the outgoing edges
3970 separately, so that we know what kind of ASSERT_EXPR to
3971 insert. */
3972 FOR_EACH_EDGE (e, ei, bb->succs)
3973 {
3974 if (e->dest == bb)
3975 continue;
3976
3977 /* Register the necessary assertions for each operand in the
3978 conditional predicate. */
3979 auto_vec<assert_info, 8> asserts;
3980 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
3981 register_edge_assert_for (op, e,
3982 gimple_cond_code (last),
3983 gimple_cond_lhs (last),
3984 gimple_cond_rhs (last), asserts);
3985 finish_register_edge_assert_for (e, bsi, asserts);
3986 }
3987 }
3988
3989 struct case_info
3990 {
3991 tree expr;
3992 basic_block bb;
3993 };
3994
3995 /* Compare two case labels sorting first by the destination bb index
3996 and then by the case value. */
3997
3998 static int
3999 compare_case_labels (const void *p1, const void *p2)
4000 {
4001 const struct case_info *ci1 = (const struct case_info *) p1;
4002 const struct case_info *ci2 = (const struct case_info *) p2;
4003 int idx1 = ci1->bb->index;
4004 int idx2 = ci2->bb->index;
4005
4006 if (idx1 < idx2)
4007 return -1;
4008 else if (idx1 == idx2)
4009 {
4010 /* Make sure the default label is first in a group. */
4011 if (!CASE_LOW (ci1->expr))
4012 return -1;
4013 else if (!CASE_LOW (ci2->expr))
4014 return 1;
4015 else
4016 return tree_int_cst_compare (CASE_LOW (ci1->expr),
4017 CASE_LOW (ci2->expr));
4018 }
4019 else
4020 return 1;
4021 }
4022
4023 /* Determine whether the outgoing edges of BB should receive an
4024 ASSERT_EXPR for each of the operands of BB's LAST statement.
4025 The last statement of BB must be a SWITCH_EXPR.
4026
4027 If any of the sub-graphs rooted at BB have an interesting use of
4028 the predicate operands, an assert location node is added to the
4029 list of assertions for the corresponding operands. */
4030
4031 static void
4032 find_switch_asserts (basic_block bb, gswitch *last)
4033 {
4034 gimple_stmt_iterator bsi;
4035 tree op;
4036 edge e;
4037 struct case_info *ci;
4038 size_t n = gimple_switch_num_labels (last);
4039 #if GCC_VERSION >= 4000
4040 unsigned int idx;
4041 #else
4042 /* Work around GCC 3.4 bug (PR 37086). */
4043 volatile unsigned int idx;
4044 #endif
4045
4046 bsi = gsi_for_stmt (last);
4047 op = gimple_switch_index (last);
4048 if (TREE_CODE (op) != SSA_NAME)
4049 return;
4050
4051 /* Build a vector of case labels sorted by destination label. */
4052 ci = XNEWVEC (struct case_info, n);
4053 for (idx = 0; idx < n; ++idx)
4054 {
4055 ci[idx].expr = gimple_switch_label (last, idx);
4056 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
4057 }
4058 edge default_edge = find_edge (bb, ci[0].bb);
4059 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
4060
4061 for (idx = 0; idx < n; ++idx)
4062 {
4063 tree min, max;
4064 tree cl = ci[idx].expr;
4065 basic_block cbb = ci[idx].bb;
4066
4067 min = CASE_LOW (cl);
4068 max = CASE_HIGH (cl);
4069
4070 /* If there are multiple case labels with the same destination
4071 we need to combine them to a single value range for the edge. */
4072 if (idx + 1 < n && cbb == ci[idx + 1].bb)
4073 {
4074 /* Skip labels until the last of the group. */
4075 do {
4076 ++idx;
4077 } while (idx < n && cbb == ci[idx].bb);
4078 --idx;
4079
4080 /* Pick up the maximum of the case label range. */
4081 if (CASE_HIGH (ci[idx].expr))
4082 max = CASE_HIGH (ci[idx].expr);
4083 else
4084 max = CASE_LOW (ci[idx].expr);
4085 }
4086
4087 /* Can't extract a useful assertion out of a range that includes the
4088 default label. */
4089 if (min == NULL_TREE)
4090 continue;
4091
4092 /* Find the edge to register the assert expr on. */
4093 e = find_edge (bb, cbb);
4094
4095 /* Register the necessary assertions for the operand in the
4096 SWITCH_EXPR. */
4097 auto_vec<assert_info, 8> asserts;
4098 register_edge_assert_for (op, e,
4099 max ? GE_EXPR : EQ_EXPR,
4100 op, fold_convert (TREE_TYPE (op), min),
4101 asserts);
4102 if (max)
4103 register_edge_assert_for (op, e, LE_EXPR, op,
4104 fold_convert (TREE_TYPE (op), max),
4105 asserts);
4106 finish_register_edge_assert_for (e, bsi, asserts);
4107 }
4108
4109 XDELETEVEC (ci);
4110
4111 if (!live_on_edge (default_edge, op))
4112 return;
4113
4114 /* Now register along the default label assertions that correspond to the
4115 anti-range of each label. */
4116 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
4117 if (insertion_limit == 0)
4118 return;
4119
4120 /* We can't do this if the default case shares a label with another case. */
4121 tree default_cl = gimple_switch_default_label (last);
4122 for (idx = 1; idx < n; idx++)
4123 {
4124 tree min, max;
4125 tree cl = gimple_switch_label (last, idx);
4126 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
4127 continue;
4128
4129 min = CASE_LOW (cl);
4130 max = CASE_HIGH (cl);
4131
4132 /* Combine contiguous case ranges to reduce the number of assertions
4133 to insert. */
4134 for (idx = idx + 1; idx < n; idx++)
4135 {
4136 tree next_min, next_max;
4137 tree next_cl = gimple_switch_label (last, idx);
4138 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
4139 break;
4140
4141 next_min = CASE_LOW (next_cl);
4142 next_max = CASE_HIGH (next_cl);
4143
4144 wide_int difference = (wi::to_wide (next_min)
4145 - wi::to_wide (max ? max : min));
4146 if (wi::eq_p (difference, 1))
4147 max = next_max ? next_max : next_min;
4148 else
4149 break;
4150 }
4151 idx--;
4152
4153 if (max == NULL_TREE)
4154 {
4155 /* Register the assertion OP != MIN. */
4156 auto_vec<assert_info, 8> asserts;
4157 min = fold_convert (TREE_TYPE (op), min);
4158 register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
4159 asserts);
4160 finish_register_edge_assert_for (default_edge, bsi, asserts);
4161 }
4162 else
4163 {
4164 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
4165 which will give OP the anti-range ~[MIN,MAX]. */
4166 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
4167 min = fold_convert (TREE_TYPE (uop), min);
4168 max = fold_convert (TREE_TYPE (uop), max);
4169
4170 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
4171 tree rhs = int_const_binop (MINUS_EXPR, max, min);
4172 register_new_assert_for (op, lhs, GT_EXPR, rhs,
4173 NULL, default_edge, bsi);
4174 }
4175
4176 if (--insertion_limit == 0)
4177 break;
4178 }
4179 }
4180
4181
4182 /* Traverse all the statements in block BB looking for statements that
4183 may generate useful assertions for the SSA names in their operand.
4184 If a statement produces a useful assertion A for name N_i, then the
4185 list of assertions already generated for N_i is scanned to
4186 determine if A is actually needed.
4187
4188 If N_i already had the assertion A at a location dominating the
4189 current location, then nothing needs to be done. Otherwise, the
4190 new location for A is recorded instead.
4191
4192 1- For every statement S in BB, all the variables used by S are
4193 added to bitmap FOUND_IN_SUBGRAPH.
4194
4195 2- If statement S uses an operand N in a way that exposes a known
4196 value range for N, then if N was not already generated by an
4197 ASSERT_EXPR, create a new assert location for N. For instance,
4198 if N is a pointer and the statement dereferences it, we can
4199 assume that N is not NULL.
4200
4201 3- COND_EXPRs are a special case of #2. We can derive range
4202 information from the predicate but need to insert different
4203 ASSERT_EXPRs for each of the sub-graphs rooted at the
4204 conditional block. If the last statement of BB is a conditional
4205 expression of the form 'X op Y', then
4206
4207 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
4208
4209 b) If the conditional is the only entry point to the sub-graph
4210 corresponding to the THEN_CLAUSE, recurse into it. On
4211 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
4212 an ASSERT_EXPR is added for the corresponding variable.
4213
4214 c) Repeat step (b) on the ELSE_CLAUSE.
4215
4216 d) Mark X and Y in FOUND_IN_SUBGRAPH.
4217
4218 For instance,
4219
4220 if (a == 9)
4221 b = a;
4222 else
4223 b = c + 1;
4224
4225 In this case, an assertion on the THEN clause is useful to
4226 determine that 'a' is always 9 on that edge. However, an assertion
4227 on the ELSE clause would be unnecessary.
4228
4229 4- If BB does not end in a conditional expression, then we recurse
4230 into BB's dominator children.
4231
4232 At the end of the recursive traversal, every SSA name will have a
4233 list of locations where ASSERT_EXPRs should be added. When a new
4234 location for name N is found, it is registered by calling
4235 register_new_assert_for. That function keeps track of all the
4236 registered assertions to prevent adding unnecessary assertions.
4237 For instance, if a pointer P_4 is dereferenced more than once in a
4238 dominator tree, only the location dominating all the dereference of
4239 P_4 will receive an ASSERT_EXPR. */
4240
4241 static void
4242 find_assert_locations_1 (basic_block bb, sbitmap live)
4243 {
4244 gimple *last;
4245
4246 last = last_stmt (bb);
4247
4248 /* If BB's last statement is a conditional statement involving integer
4249 operands, determine if we need to add ASSERT_EXPRs. */
4250 if (last
4251 && gimple_code (last) == GIMPLE_COND
4252 && !fp_predicate (last)
4253 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
4254 find_conditional_asserts (bb, as_a <gcond *> (last));
4255
4256 /* If BB's last statement is a switch statement involving integer
4257 operands, determine if we need to add ASSERT_EXPRs. */
4258 if (last
4259 && gimple_code (last) == GIMPLE_SWITCH
4260 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
4261 find_switch_asserts (bb, as_a <gswitch *> (last));
4262
4263 /* Traverse all the statements in BB marking used names and looking
4264 for statements that may infer assertions for their used operands. */
4265 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
4266 gsi_prev (&si))
4267 {
4268 gimple *stmt;
4269 tree op;
4270 ssa_op_iter i;
4271
4272 stmt = gsi_stmt (si);
4273
4274 if (is_gimple_debug (stmt))
4275 continue;
4276
4277 /* See if we can derive an assertion for any of STMT's operands. */
4278 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4279 {
4280 tree value;
4281 enum tree_code comp_code;
4282
4283 /* If op is not live beyond this stmt, do not bother to insert
4284 asserts for it. */
4285 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
4286 continue;
4287
4288 /* If OP is used in such a way that we can infer a value
4289 range for it, and we don't find a previous assertion for
4290 it, create a new assertion location node for OP. */
4291 if (infer_value_range (stmt, op, &comp_code, &value))
4292 {
4293 /* If we are able to infer a nonzero value range for OP,
4294 then walk backwards through the use-def chain to see if OP
4295 was set via a typecast.
4296
4297 If so, then we can also infer a nonzero value range
4298 for the operand of the NOP_EXPR. */
4299 if (comp_code == NE_EXPR && integer_zerop (value))
4300 {
4301 tree t = op;
4302 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
4303
4304 while (is_gimple_assign (def_stmt)
4305 && CONVERT_EXPR_CODE_P
4306 (gimple_assign_rhs_code (def_stmt))
4307 && TREE_CODE
4308 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
4309 && POINTER_TYPE_P
4310 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
4311 {
4312 t = gimple_assign_rhs1 (def_stmt);
4313 def_stmt = SSA_NAME_DEF_STMT (t);
4314
4315 /* Note we want to register the assert for the
4316 operand of the NOP_EXPR after SI, not after the
4317 conversion. */
4318 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
4319 register_new_assert_for (t, t, comp_code, value,
4320 bb, NULL, si);
4321 }
4322 }
4323
4324 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
4325 }
4326 }
4327
4328 /* Update live. */
4329 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4330 bitmap_set_bit (live, SSA_NAME_VERSION (op));
4331 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
4332 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
4333 }
4334
4335 /* Traverse all PHI nodes in BB, updating live. */
4336 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
4337 gsi_next (&si))
4338 {
4339 use_operand_p arg_p;
4340 ssa_op_iter i;
4341 gphi *phi = si.phi ();
4342 tree res = gimple_phi_result (phi);
4343
4344 if (virtual_operand_p (res))
4345 continue;
4346
4347 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
4348 {
4349 tree arg = USE_FROM_PTR (arg_p);
4350 if (TREE_CODE (arg) == SSA_NAME)
4351 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
4352 }
4353
4354 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
4355 }
4356 }
4357
4358 /* Do an RPO walk over the function computing SSA name liveness
4359 on-the-fly and deciding on assert expressions to insert. */
4360
4361 static void
4362 find_assert_locations (void)
4363 {
4364 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
4365 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
4366 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
4367 int rpo_cnt, i;
4368
4369 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
4370 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
4371 for (i = 0; i < rpo_cnt; ++i)
4372 bb_rpo[rpo[i]] = i;
4373
4374 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
4375 the order we compute liveness and insert asserts we otherwise
4376 fail to insert asserts into the loop latch. */
4377 loop_p loop;
4378 FOR_EACH_LOOP (loop, 0)
4379 {
4380 i = loop->latch->index;
4381 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
4382 for (gphi_iterator gsi = gsi_start_phis (loop->header);
4383 !gsi_end_p (gsi); gsi_next (&gsi))
4384 {
4385 gphi *phi = gsi.phi ();
4386 if (virtual_operand_p (gimple_phi_result (phi)))
4387 continue;
4388 tree arg = gimple_phi_arg_def (phi, j);
4389 if (TREE_CODE (arg) == SSA_NAME)
4390 {
4391 if (live[i] == NULL)
4392 {
4393 live[i] = sbitmap_alloc (num_ssa_names);
4394 bitmap_clear (live[i]);
4395 }
4396 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
4397 }
4398 }
4399 }
4400
4401 for (i = rpo_cnt - 1; i >= 0; --i)
4402 {
4403 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
4404 edge e;
4405 edge_iterator ei;
4406
4407 if (!live[rpo[i]])
4408 {
4409 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
4410 bitmap_clear (live[rpo[i]]);
4411 }
4412
4413 /* Process BB and update the live information with uses in
4414 this block. */
4415 find_assert_locations_1 (bb, live[rpo[i]]);
4416
4417 /* Merge liveness into the predecessor blocks and free it. */
4418 if (!bitmap_empty_p (live[rpo[i]]))
4419 {
4420 int pred_rpo = i;
4421 FOR_EACH_EDGE (e, ei, bb->preds)
4422 {
4423 int pred = e->src->index;
4424 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
4425 continue;
4426
4427 if (!live[pred])
4428 {
4429 live[pred] = sbitmap_alloc (num_ssa_names);
4430 bitmap_clear (live[pred]);
4431 }
4432 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
4433
4434 if (bb_rpo[pred] < pred_rpo)
4435 pred_rpo = bb_rpo[pred];
4436 }
4437
4438 /* Record the RPO number of the last visited block that needs
4439 live information from this block. */
4440 last_rpo[rpo[i]] = pred_rpo;
4441 }
4442 else
4443 {
4444 sbitmap_free (live[rpo[i]]);
4445 live[rpo[i]] = NULL;
4446 }
4447
4448 /* We can free all successors live bitmaps if all their
4449 predecessors have been visited already. */
4450 FOR_EACH_EDGE (e, ei, bb->succs)
4451 if (last_rpo[e->dest->index] == i
4452 && live[e->dest->index])
4453 {
4454 sbitmap_free (live[e->dest->index]);
4455 live[e->dest->index] = NULL;
4456 }
4457 }
4458
4459 XDELETEVEC (rpo);
4460 XDELETEVEC (bb_rpo);
4461 XDELETEVEC (last_rpo);
4462 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
4463 if (live[i])
4464 sbitmap_free (live[i]);
4465 XDELETEVEC (live);
4466 }
4467
4468 /* Create an ASSERT_EXPR for NAME and insert it in the location
4469 indicated by LOC. Return true if we made any edge insertions. */
4470
4471 static bool
4472 process_assert_insertions_for (tree name, assert_locus *loc)
4473 {
4474 /* Build the comparison expression NAME_i COMP_CODE VAL. */
4475 gimple *stmt;
4476 tree cond;
4477 gimple *assert_stmt;
4478 edge_iterator ei;
4479 edge e;
4480
4481 /* If we have X <=> X do not insert an assert expr for that. */
4482 if (loc->expr == loc->val)
4483 return false;
4484
4485 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
4486 assert_stmt = build_assert_expr_for (cond, name);
4487 if (loc->e)
4488 {
4489 /* We have been asked to insert the assertion on an edge. This
4490 is used only by COND_EXPR and SWITCH_EXPR assertions. */
4491 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
4492 || (gimple_code (gsi_stmt (loc->si))
4493 == GIMPLE_SWITCH));
4494
4495 gsi_insert_on_edge (loc->e, assert_stmt);
4496 return true;
4497 }
4498
4499 /* If the stmt iterator points at the end then this is an insertion
4500 at the beginning of a block. */
4501 if (gsi_end_p (loc->si))
4502 {
4503 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
4504 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
4505 return false;
4506
4507 }
4508 /* Otherwise, we can insert right after LOC->SI iff the
4509 statement must not be the last statement in the block. */
4510 stmt = gsi_stmt (loc->si);
4511 if (!stmt_ends_bb_p (stmt))
4512 {
4513 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
4514 return false;
4515 }
4516
4517 /* If STMT must be the last statement in BB, we can only insert new
4518 assertions on the non-abnormal edge out of BB. Note that since
4519 STMT is not control flow, there may only be one non-abnormal/eh edge
4520 out of BB. */
4521 FOR_EACH_EDGE (e, ei, loc->bb->succs)
4522 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4523 {
4524 gsi_insert_on_edge (e, assert_stmt);
4525 return true;
4526 }
4527
4528 gcc_unreachable ();
4529 }
4530
4531 /* Qsort helper for sorting assert locations. If stable is true, don't
4532 use iterative_hash_expr because it can be unstable for -fcompare-debug,
4533 on the other side some pointers might be NULL. */
4534
4535 template <bool stable>
4536 static int
4537 compare_assert_loc (const void *pa, const void *pb)
4538 {
4539 assert_locus * const a = *(assert_locus * const *)pa;
4540 assert_locus * const b = *(assert_locus * const *)pb;
4541
4542 /* If stable, some asserts might be optimized away already, sort
4543 them last. */
4544 if (stable)
4545 {
4546 if (a == NULL)
4547 return b != NULL;
4548 else if (b == NULL)
4549 return -1;
4550 }
4551
4552 if (a->e == NULL && b->e != NULL)
4553 return 1;
4554 else if (a->e != NULL && b->e == NULL)
4555 return -1;
4556
4557 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
4558 no need to test both a->e and b->e. */
4559
4560 /* Sort after destination index. */
4561 if (a->e == NULL)
4562 ;
4563 else if (a->e->dest->index > b->e->dest->index)
4564 return 1;
4565 else if (a->e->dest->index < b->e->dest->index)
4566 return -1;
4567
4568 /* Sort after comp_code. */
4569 if (a->comp_code > b->comp_code)
4570 return 1;
4571 else if (a->comp_code < b->comp_code)
4572 return -1;
4573
4574 hashval_t ha, hb;
4575
4576 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
4577 uses DECL_UID of the VAR_DECL, so sorting might differ between
4578 -g and -g0. When doing the removal of redundant assert exprs
4579 and commonization to successors, this does not matter, but for
4580 the final sort needs to be stable. */
4581 if (stable)
4582 {
4583 ha = 0;
4584 hb = 0;
4585 }
4586 else
4587 {
4588 ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
4589 hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
4590 }
4591
4592 /* Break the tie using hashing and source/bb index. */
4593 if (ha == hb)
4594 return (a->e != NULL
4595 ? a->e->src->index - b->e->src->index
4596 : a->bb->index - b->bb->index);
4597 return ha > hb ? 1 : -1;
4598 }
4599
4600 /* Process all the insertions registered for every name N_i registered
4601 in NEED_ASSERT_FOR. The list of assertions to be inserted are
4602 found in ASSERTS_FOR[i]. */
4603
4604 static void
4605 process_assert_insertions (void)
4606 {
4607 unsigned i;
4608 bitmap_iterator bi;
4609 bool update_edges_p = false;
4610 int num_asserts = 0;
4611
4612 if (dump_file && (dump_flags & TDF_DETAILS))
4613 dump_all_asserts (dump_file);
4614
4615 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4616 {
4617 assert_locus *loc = asserts_for[i];
4618 gcc_assert (loc);
4619
4620 auto_vec<assert_locus *, 16> asserts;
4621 for (; loc; loc = loc->next)
4622 asserts.safe_push (loc);
4623 asserts.qsort (compare_assert_loc<false>);
4624
4625 /* Push down common asserts to successors and remove redundant ones. */
4626 unsigned ecnt = 0;
4627 assert_locus *common = NULL;
4628 unsigned commonj = 0;
4629 for (unsigned j = 0; j < asserts.length (); ++j)
4630 {
4631 loc = asserts[j];
4632 if (! loc->e)
4633 common = NULL;
4634 else if (! common
4635 || loc->e->dest != common->e->dest
4636 || loc->comp_code != common->comp_code
4637 || ! operand_equal_p (loc->val, common->val, 0)
4638 || ! operand_equal_p (loc->expr, common->expr, 0))
4639 {
4640 commonj = j;
4641 common = loc;
4642 ecnt = 1;
4643 }
4644 else if (loc->e == asserts[j-1]->e)
4645 {
4646 /* Remove duplicate asserts. */
4647 if (commonj == j - 1)
4648 {
4649 commonj = j;
4650 common = loc;
4651 }
4652 free (asserts[j-1]);
4653 asserts[j-1] = NULL;
4654 }
4655 else
4656 {
4657 ecnt++;
4658 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
4659 {
4660 /* We have the same assertion on all incoming edges of a BB.
4661 Insert it at the beginning of that block. */
4662 loc->bb = loc->e->dest;
4663 loc->e = NULL;
4664 loc->si = gsi_none ();
4665 common = NULL;
4666 /* Clear asserts commoned. */
4667 for (; commonj != j; ++commonj)
4668 if (asserts[commonj])
4669 {
4670 free (asserts[commonj]);
4671 asserts[commonj] = NULL;
4672 }
4673 }
4674 }
4675 }
4676
4677 /* The asserts vector sorting above might be unstable for
4678 -fcompare-debug, sort again to ensure a stable sort. */
4679 asserts.qsort (compare_assert_loc<true>);
4680 for (unsigned j = 0; j < asserts.length (); ++j)
4681 {
4682 loc = asserts[j];
4683 if (! loc)
4684 break;
4685 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
4686 num_asserts++;
4687 free (loc);
4688 }
4689 }
4690
4691 if (update_edges_p)
4692 gsi_commit_edge_inserts ();
4693
4694 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
4695 num_asserts);
4696 }
4697
4698
4699 /* Traverse the flowgraph looking for conditional jumps to insert range
4700 expressions. These range expressions are meant to provide information
4701 to optimizations that need to reason in terms of value ranges. They
4702 will not be expanded into RTL. For instance, given:
4703
4704 x = ...
4705 y = ...
4706 if (x < y)
4707 y = x - 2;
4708 else
4709 x = y + 3;
4710
4711 this pass will transform the code into:
4712
4713 x = ...
4714 y = ...
4715 if (x < y)
4716 {
4717 x = ASSERT_EXPR <x, x < y>
4718 y = x - 2
4719 }
4720 else
4721 {
4722 y = ASSERT_EXPR <y, x >= y>
4723 x = y + 3
4724 }
4725
4726 The idea is that once copy and constant propagation have run, other
4727 optimizations will be able to determine what ranges of values can 'x'
4728 take in different paths of the code, simply by checking the reaching
4729 definition of 'x'. */
4730
4731 static void
4732 insert_range_assertions (void)
4733 {
4734 need_assert_for = BITMAP_ALLOC (NULL);
4735 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
4736
4737 calculate_dominance_info (CDI_DOMINATORS);
4738
4739 find_assert_locations ();
4740 if (!bitmap_empty_p (need_assert_for))
4741 {
4742 process_assert_insertions ();
4743 update_ssa (TODO_update_ssa_no_phi);
4744 }
4745
4746 if (dump_file && (dump_flags & TDF_DETAILS))
4747 {
4748 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
4749 dump_function_to_file (current_function_decl, dump_file, dump_flags);
4750 }
4751
4752 free (asserts_for);
4753 BITMAP_FREE (need_assert_for);
4754 }
4755
4756 class vrp_prop : public ssa_propagation_engine
4757 {
4758 public:
4759 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
4760 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
4761
4762 void vrp_initialize (void);
4763 void vrp_finalize (bool);
4764 void check_all_array_refs (void);
4765 void check_array_ref (location_t, tree, bool);
4766 void search_for_addr_array (tree, location_t);
4767
4768 class vr_values vr_values;
4769 /* Temporary delegator to minimize code churn. */
4770 value_range *get_value_range (const_tree op)
4771 { return vr_values.get_value_range (op); }
4772 void set_defs_to_varying (gimple *stmt)
4773 { return vr_values.set_defs_to_varying (stmt); }
4774 void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
4775 tree *output_p, value_range *vr)
4776 { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); }
4777 bool update_value_range (const_tree op, value_range *vr)
4778 { return vr_values.update_value_range (op, vr); }
4779 void extract_range_basic (value_range *vr, gimple *stmt)
4780 { vr_values.extract_range_basic (vr, stmt); }
4781 void extract_range_from_phi_node (gphi *phi, value_range *vr)
4782 { vr_values.extract_range_from_phi_node (phi, vr); }
4783 };
4784 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4785 and "struct" hacks. If VRP can determine that the
4786 array subscript is a constant, check if it is outside valid
4787 range. If the array subscript is a RANGE, warn if it is
4788 non-overlapping with valid range.
4789 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
4790
4791 void
4792 vrp_prop::check_array_ref (location_t location, tree ref,
4793 bool ignore_off_by_one)
4794 {
4795 value_range *vr = NULL;
4796 tree low_sub, up_sub;
4797 tree low_bound, up_bound, up_bound_p1;
4798
4799 if (TREE_NO_WARNING (ref))
4800 return;
4801
4802 low_sub = up_sub = TREE_OPERAND (ref, 1);
4803 up_bound = array_ref_up_bound (ref);
4804
4805 if (!up_bound
4806 || TREE_CODE (up_bound) != INTEGER_CST
4807 || (warn_array_bounds < 2
4808 && array_at_struct_end_p (ref)))
4809 {
4810 /* Accesses to trailing arrays via pointers may access storage
4811 beyond the types array bounds. For such arrays, or for flexible
4812 array members, as well as for other arrays of an unknown size,
4813 replace the upper bound with a more permissive one that assumes
4814 the size of the largest object is PTRDIFF_MAX. */
4815 tree eltsize = array_ref_element_size (ref);
4816
4817 if (TREE_CODE (eltsize) != INTEGER_CST
4818 || integer_zerop (eltsize))
4819 {
4820 up_bound = NULL_TREE;
4821 up_bound_p1 = NULL_TREE;
4822 }
4823 else
4824 {
4825 tree maxbound = TYPE_MAX_VALUE (ptrdiff_type_node);
4826 tree arg = TREE_OPERAND (ref, 0);
4827 poly_int64 off;
4828
4829 if (get_addr_base_and_unit_offset (arg, &off) && known_gt (off, 0))
4830 maxbound = wide_int_to_tree (sizetype,
4831 wi::sub (wi::to_wide (maxbound),
4832 off));
4833 else
4834 maxbound = fold_convert (sizetype, maxbound);
4835
4836 up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize);
4837
4838 up_bound = int_const_binop (MINUS_EXPR, up_bound_p1,
4839 build_int_cst (ptrdiff_type_node, 1));
4840 }
4841 }
4842 else
4843 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
4844 build_int_cst (TREE_TYPE (up_bound), 1));
4845
4846 low_bound = array_ref_low_bound (ref);
4847
4848 tree artype = TREE_TYPE (TREE_OPERAND (ref, 0));
4849
4850 /* Empty array. */
4851 if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1))
4852 {
4853 warning_at (location, OPT_Warray_bounds,
4854 "array subscript %E is above array bounds of %qT",
4855 low_bound, artype);
4856 TREE_NO_WARNING (ref) = 1;
4857 }
4858
4859 if (TREE_CODE (low_sub) == SSA_NAME)
4860 {
4861 vr = get_value_range (low_sub);
4862 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4863 {
4864 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
4865 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
4866 }
4867 }
4868
4869 if (vr && vr->type == VR_ANTI_RANGE)
4870 {
4871 if (up_bound
4872 && TREE_CODE (up_sub) == INTEGER_CST
4873 && (ignore_off_by_one
4874 ? tree_int_cst_lt (up_bound, up_sub)
4875 : tree_int_cst_le (up_bound, up_sub))
4876 && TREE_CODE (low_sub) == INTEGER_CST
4877 && tree_int_cst_le (low_sub, low_bound))
4878 {
4879 warning_at (location, OPT_Warray_bounds,
4880 "array subscript [%E, %E] is outside array bounds of %qT",
4881 low_sub, up_sub, artype);
4882 TREE_NO_WARNING (ref) = 1;
4883 }
4884 }
4885 else if (up_bound
4886 && TREE_CODE (up_sub) == INTEGER_CST
4887 && (ignore_off_by_one
4888 ? !tree_int_cst_le (up_sub, up_bound_p1)
4889 : !tree_int_cst_le (up_sub, up_bound)))
4890 {
4891 if (dump_file && (dump_flags & TDF_DETAILS))
4892 {
4893 fprintf (dump_file, "Array bound warning for ");
4894 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4895 fprintf (dump_file, "\n");
4896 }
4897 warning_at (location, OPT_Warray_bounds,
4898 "array subscript %E is above array bounds of %qT",
4899 up_sub, artype);
4900 TREE_NO_WARNING (ref) = 1;
4901 }
4902 else if (TREE_CODE (low_sub) == INTEGER_CST
4903 && tree_int_cst_lt (low_sub, low_bound))
4904 {
4905 if (dump_file && (dump_flags & TDF_DETAILS))
4906 {
4907 fprintf (dump_file, "Array bound warning for ");
4908 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4909 fprintf (dump_file, "\n");
4910 }
4911 warning_at (location, OPT_Warray_bounds,
4912 "array subscript %E is below array bounds of %qT",
4913 low_sub, artype);
4914 TREE_NO_WARNING (ref) = 1;
4915 }
4916 }
4917
4918 /* Searches if the expr T, located at LOCATION computes
4919 address of an ARRAY_REF, and call check_array_ref on it. */
4920
4921 void
4922 vrp_prop::search_for_addr_array (tree t, location_t location)
4923 {
4924 /* Check each ARRAY_REFs in the reference chain. */
4925 do
4926 {
4927 if (TREE_CODE (t) == ARRAY_REF)
4928 check_array_ref (location, t, true /*ignore_off_by_one*/);
4929
4930 t = TREE_OPERAND (t, 0);
4931 }
4932 while (handled_component_p (t));
4933
4934 if (TREE_CODE (t) == MEM_REF
4935 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4936 && !TREE_NO_WARNING (t))
4937 {
4938 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
4939 tree low_bound, up_bound, el_sz;
4940 offset_int idx;
4941 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
4942 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
4943 || !TYPE_DOMAIN (TREE_TYPE (tem)))
4944 return;
4945
4946 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
4947 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
4948 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
4949 if (!low_bound
4950 || TREE_CODE (low_bound) != INTEGER_CST
4951 || !up_bound
4952 || TREE_CODE (up_bound) != INTEGER_CST
4953 || !el_sz
4954 || TREE_CODE (el_sz) != INTEGER_CST)
4955 return;
4956
4957 if (!mem_ref_offset (t).is_constant (&idx))
4958 return;
4959
4960 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
4961 if (idx < 0)
4962 {
4963 if (dump_file && (dump_flags & TDF_DETAILS))
4964 {
4965 fprintf (dump_file, "Array bound warning for ");
4966 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
4967 fprintf (dump_file, "\n");
4968 }
4969 warning_at (location, OPT_Warray_bounds,
4970 "array subscript %wi is below array bounds of %qT",
4971 idx.to_shwi (), TREE_TYPE (tem));
4972 TREE_NO_WARNING (t) = 1;
4973 }
4974 else if (idx > (wi::to_offset (up_bound)
4975 - wi::to_offset (low_bound) + 1))
4976 {
4977 if (dump_file && (dump_flags & TDF_DETAILS))
4978 {
4979 fprintf (dump_file, "Array bound warning for ");
4980 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
4981 fprintf (dump_file, "\n");
4982 }
4983 warning_at (location, OPT_Warray_bounds,
4984 "array subscript %wu is above array bounds of %qT",
4985 idx.to_uhwi (), TREE_TYPE (tem));
4986 TREE_NO_WARNING (t) = 1;
4987 }
4988 }
4989 }
4990
4991 /* walk_tree() callback that checks if *TP is
4992 an ARRAY_REF inside an ADDR_EXPR (in which an array
4993 subscript one outside the valid range is allowed). Call
4994 check_array_ref for each ARRAY_REF found. The location is
4995 passed in DATA. */
4996
4997 static tree
4998 check_array_bounds (tree *tp, int *walk_subtree, void *data)
4999 {
5000 tree t = *tp;
5001 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
5002 location_t location;
5003
5004 if (EXPR_HAS_LOCATION (t))
5005 location = EXPR_LOCATION (t);
5006 else
5007 location = gimple_location (wi->stmt);
5008
5009 *walk_subtree = TRUE;
5010
5011 vrp_prop *vrp_prop = (class vrp_prop *)wi->info;
5012 if (TREE_CODE (t) == ARRAY_REF)
5013 vrp_prop->check_array_ref (location, t, false /*ignore_off_by_one*/);
5014
5015 else if (TREE_CODE (t) == ADDR_EXPR)
5016 {
5017 vrp_prop->search_for_addr_array (t, location);
5018 *walk_subtree = FALSE;
5019 }
5020
5021 return NULL_TREE;
5022 }
5023
5024 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
5025 to walk over all statements of all reachable BBs and call
5026 check_array_bounds on them. */
5027
5028 class check_array_bounds_dom_walker : public dom_walker
5029 {
5030 public:
5031 check_array_bounds_dom_walker (vrp_prop *prop)
5032 : dom_walker (CDI_DOMINATORS, true), m_prop (prop) {}
5033 ~check_array_bounds_dom_walker () {}
5034
5035 edge before_dom_children (basic_block) FINAL OVERRIDE;
5036
5037 private:
5038 vrp_prop *m_prop;
5039 };
5040
5041 /* Implementation of dom_walker::before_dom_children.
5042
5043 Walk over all statements of BB and call check_array_bounds on them,
5044 and determine if there's a unique successor edge. */
5045
5046 edge
5047 check_array_bounds_dom_walker::before_dom_children (basic_block bb)
5048 {
5049 gimple_stmt_iterator si;
5050 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5051 {
5052 gimple *stmt = gsi_stmt (si);
5053 struct walk_stmt_info wi;
5054 if (!gimple_has_location (stmt)
5055 || is_gimple_debug (stmt))
5056 continue;
5057
5058 memset (&wi, 0, sizeof (wi));
5059
5060 wi.info = m_prop;
5061
5062 walk_gimple_op (stmt, check_array_bounds, &wi);
5063 }
5064
5065 /* Determine if there's a unique successor edge, and if so, return
5066 that back to dom_walker, ensuring that we don't visit blocks that
5067 became unreachable during the VRP propagation
5068 (PR tree-optimization/83312). */
5069 return find_taken_edge (bb, NULL_TREE);
5070 }
5071
5072 /* Walk over all statements of all reachable BBs and call check_array_bounds
5073 on them. */
5074
5075 void
5076 vrp_prop::check_all_array_refs ()
5077 {
5078 check_array_bounds_dom_walker w (this);
5079 w.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5080 }
5081
5082 /* Return true if all imm uses of VAR are either in STMT, or
5083 feed (optionally through a chain of single imm uses) GIMPLE_COND
5084 in basic block COND_BB. */
5085
5086 static bool
5087 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
5088 {
5089 use_operand_p use_p, use2_p;
5090 imm_use_iterator iter;
5091
5092 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
5093 if (USE_STMT (use_p) != stmt)
5094 {
5095 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
5096 if (is_gimple_debug (use_stmt))
5097 continue;
5098 while (is_gimple_assign (use_stmt)
5099 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
5100 && single_imm_use (gimple_assign_lhs (use_stmt),
5101 &use2_p, &use_stmt2))
5102 use_stmt = use_stmt2;
5103 if (gimple_code (use_stmt) != GIMPLE_COND
5104 || gimple_bb (use_stmt) != cond_bb)
5105 return false;
5106 }
5107 return true;
5108 }
5109
5110 /* Handle
5111 _4 = x_3 & 31;
5112 if (_4 != 0)
5113 goto <bb 6>;
5114 else
5115 goto <bb 7>;
5116 <bb 6>:
5117 __builtin_unreachable ();
5118 <bb 7>:
5119 x_5 = ASSERT_EXPR <x_3, ...>;
5120 If x_3 has no other immediate uses (checked by caller),
5121 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
5122 from the non-zero bitmask. */
5123
5124 void
5125 maybe_set_nonzero_bits (edge e, tree var)
5126 {
5127 basic_block cond_bb = e->src;
5128 gimple *stmt = last_stmt (cond_bb);
5129 tree cst;
5130
5131 if (stmt == NULL
5132 || gimple_code (stmt) != GIMPLE_COND
5133 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
5134 ? EQ_EXPR : NE_EXPR)
5135 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
5136 || !integer_zerop (gimple_cond_rhs (stmt)))
5137 return;
5138
5139 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
5140 if (!is_gimple_assign (stmt)
5141 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
5142 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
5143 return;
5144 if (gimple_assign_rhs1 (stmt) != var)
5145 {
5146 gimple *stmt2;
5147
5148 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
5149 return;
5150 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
5151 if (!gimple_assign_cast_p (stmt2)
5152 || gimple_assign_rhs1 (stmt2) != var
5153 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
5154 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
5155 != TYPE_PRECISION (TREE_TYPE (var))))
5156 return;
5157 }
5158 cst = gimple_assign_rhs2 (stmt);
5159 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
5160 wi::to_wide (cst)));
5161 }
5162
5163 /* Convert range assertion expressions into the implied copies and
5164 copy propagate away the copies. Doing the trivial copy propagation
5165 here avoids the need to run the full copy propagation pass after
5166 VRP.
5167
5168 FIXME, this will eventually lead to copy propagation removing the
5169 names that had useful range information attached to them. For
5170 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
5171 then N_i will have the range [3, +INF].
5172
5173 However, by converting the assertion into the implied copy
5174 operation N_i = N_j, we will then copy-propagate N_j into the uses
5175 of N_i and lose the range information. We may want to hold on to
5176 ASSERT_EXPRs a little while longer as the ranges could be used in
5177 things like jump threading.
5178
5179 The problem with keeping ASSERT_EXPRs around is that passes after
5180 VRP need to handle them appropriately.
5181
5182 Another approach would be to make the range information a first
5183 class property of the SSA_NAME so that it can be queried from
5184 any pass. This is made somewhat more complex by the need for
5185 multiple ranges to be associated with one SSA_NAME. */
5186
5187 static void
5188 remove_range_assertions (void)
5189 {
5190 basic_block bb;
5191 gimple_stmt_iterator si;
5192 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
5193 a basic block preceeded by GIMPLE_COND branching to it and
5194 __builtin_trap, -1 if not yet checked, 0 otherwise. */
5195 int is_unreachable;
5196
5197 /* Note that the BSI iterator bump happens at the bottom of the
5198 loop and no bump is necessary if we're removing the statement
5199 referenced by the current BSI. */
5200 FOR_EACH_BB_FN (bb, cfun)
5201 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
5202 {
5203 gimple *stmt = gsi_stmt (si);
5204
5205 if (is_gimple_assign (stmt)
5206 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
5207 {
5208 tree lhs = gimple_assign_lhs (stmt);
5209 tree rhs = gimple_assign_rhs1 (stmt);
5210 tree var;
5211
5212 var = ASSERT_EXPR_VAR (rhs);
5213
5214 if (TREE_CODE (var) == SSA_NAME
5215 && !POINTER_TYPE_P (TREE_TYPE (lhs))
5216 && SSA_NAME_RANGE_INFO (lhs))
5217 {
5218 if (is_unreachable == -1)
5219 {
5220 is_unreachable = 0;
5221 if (single_pred_p (bb)
5222 && assert_unreachable_fallthru_edge_p
5223 (single_pred_edge (bb)))
5224 is_unreachable = 1;
5225 }
5226 /* Handle
5227 if (x_7 >= 10 && x_7 < 20)
5228 __builtin_unreachable ();
5229 x_8 = ASSERT_EXPR <x_7, ...>;
5230 if the only uses of x_7 are in the ASSERT_EXPR and
5231 in the condition. In that case, we can copy the
5232 range info from x_8 computed in this pass also
5233 for x_7. */
5234 if (is_unreachable
5235 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
5236 single_pred (bb)))
5237 {
5238 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
5239 SSA_NAME_RANGE_INFO (lhs)->get_min (),
5240 SSA_NAME_RANGE_INFO (lhs)->get_max ());
5241 maybe_set_nonzero_bits (single_pred_edge (bb), var);
5242 }
5243 }
5244
5245 /* Propagate the RHS into every use of the LHS. For SSA names
5246 also propagate abnormals as it merely restores the original
5247 IL in this case (an replace_uses_by would assert). */
5248 if (TREE_CODE (var) == SSA_NAME)
5249 {
5250 imm_use_iterator iter;
5251 use_operand_p use_p;
5252 gimple *use_stmt;
5253 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5254 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
5255 SET_USE (use_p, var);
5256 }
5257 else
5258 replace_uses_by (lhs, var);
5259
5260 /* And finally, remove the copy, it is not needed. */
5261 gsi_remove (&si, true);
5262 release_defs (stmt);
5263 }
5264 else
5265 {
5266 if (!is_gimple_debug (gsi_stmt (si)))
5267 is_unreachable = 0;
5268 gsi_next (&si);
5269 }
5270 }
5271 }
5272
5273 /* Return true if STMT is interesting for VRP. */
5274
5275 bool
5276 stmt_interesting_for_vrp (gimple *stmt)
5277 {
5278 if (gimple_code (stmt) == GIMPLE_PHI)
5279 {
5280 tree res = gimple_phi_result (stmt);
5281 return (!virtual_operand_p (res)
5282 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
5283 || POINTER_TYPE_P (TREE_TYPE (res))));
5284 }
5285 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
5286 {
5287 tree lhs = gimple_get_lhs (stmt);
5288
5289 /* In general, assignments with virtual operands are not useful
5290 for deriving ranges, with the obvious exception of calls to
5291 builtin functions. */
5292 if (lhs && TREE_CODE (lhs) == SSA_NAME
5293 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
5294 || POINTER_TYPE_P (TREE_TYPE (lhs)))
5295 && (is_gimple_call (stmt)
5296 || !gimple_vuse (stmt)))
5297 return true;
5298 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
5299 switch (gimple_call_internal_fn (stmt))
5300 {
5301 case IFN_ADD_OVERFLOW:
5302 case IFN_SUB_OVERFLOW:
5303 case IFN_MUL_OVERFLOW:
5304 case IFN_ATOMIC_COMPARE_EXCHANGE:
5305 /* These internal calls return _Complex integer type,
5306 but are interesting to VRP nevertheless. */
5307 if (lhs && TREE_CODE (lhs) == SSA_NAME)
5308 return true;
5309 break;
5310 default:
5311 break;
5312 }
5313 }
5314 else if (gimple_code (stmt) == GIMPLE_COND
5315 || gimple_code (stmt) == GIMPLE_SWITCH)
5316 return true;
5317
5318 return false;
5319 }
5320
5321 /* Initialization required by ssa_propagate engine. */
5322
5323 void
5324 vrp_prop::vrp_initialize ()
5325 {
5326 basic_block bb;
5327
5328 FOR_EACH_BB_FN (bb, cfun)
5329 {
5330 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
5331 gsi_next (&si))
5332 {
5333 gphi *phi = si.phi ();
5334 if (!stmt_interesting_for_vrp (phi))
5335 {
5336 tree lhs = PHI_RESULT (phi);
5337 set_value_range_to_varying (get_value_range (lhs));
5338 prop_set_simulate_again (phi, false);
5339 }
5340 else
5341 prop_set_simulate_again (phi, true);
5342 }
5343
5344 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
5345 gsi_next (&si))
5346 {
5347 gimple *stmt = gsi_stmt (si);
5348
5349 /* If the statement is a control insn, then we do not
5350 want to avoid simulating the statement once. Failure
5351 to do so means that those edges will never get added. */
5352 if (stmt_ends_bb_p (stmt))
5353 prop_set_simulate_again (stmt, true);
5354 else if (!stmt_interesting_for_vrp (stmt))
5355 {
5356 set_defs_to_varying (stmt);
5357 prop_set_simulate_again (stmt, false);
5358 }
5359 else
5360 prop_set_simulate_again (stmt, true);
5361 }
5362 }
5363 }
5364
5365 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
5366 that includes the value VAL. The search is restricted to the range
5367 [START_IDX, n - 1] where n is the size of VEC.
5368
5369 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
5370 returned.
5371
5372 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
5373 it is placed in IDX and false is returned.
5374
5375 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
5376 returned. */
5377
5378 bool
5379 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
5380 {
5381 size_t n = gimple_switch_num_labels (stmt);
5382 size_t low, high;
5383
5384 /* Find case label for minimum of the value range or the next one.
5385 At each iteration we are searching in [low, high - 1]. */
5386
5387 for (low = start_idx, high = n; high != low; )
5388 {
5389 tree t;
5390 int cmp;
5391 /* Note that i != high, so we never ask for n. */
5392 size_t i = (high + low) / 2;
5393 t = gimple_switch_label (stmt, i);
5394
5395 /* Cache the result of comparing CASE_LOW and val. */
5396 cmp = tree_int_cst_compare (CASE_LOW (t), val);
5397
5398 if (cmp == 0)
5399 {
5400 /* Ranges cannot be empty. */
5401 *idx = i;
5402 return true;
5403 }
5404 else if (cmp > 0)
5405 high = i;
5406 else
5407 {
5408 low = i + 1;
5409 if (CASE_HIGH (t) != NULL
5410 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
5411 {
5412 *idx = i;
5413 return true;
5414 }
5415 }
5416 }
5417
5418 *idx = high;
5419 return false;
5420 }
5421
5422 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
5423 for values between MIN and MAX. The first index is placed in MIN_IDX. The
5424 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
5425 then MAX_IDX < MIN_IDX.
5426 Returns true if the default label is not needed. */
5427
5428 bool
5429 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
5430 size_t *max_idx)
5431 {
5432 size_t i, j;
5433 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
5434 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
5435
5436 if (i == j
5437 && min_take_default
5438 && max_take_default)
5439 {
5440 /* Only the default case label reached.
5441 Return an empty range. */
5442 *min_idx = 1;
5443 *max_idx = 0;
5444 return false;
5445 }
5446 else
5447 {
5448 bool take_default = min_take_default || max_take_default;
5449 tree low, high;
5450 size_t k;
5451
5452 if (max_take_default)
5453 j--;
5454
5455 /* If the case label range is continuous, we do not need
5456 the default case label. Verify that. */
5457 high = CASE_LOW (gimple_switch_label (stmt, i));
5458 if (CASE_HIGH (gimple_switch_label (stmt, i)))
5459 high = CASE_HIGH (gimple_switch_label (stmt, i));
5460 for (k = i + 1; k <= j; ++k)
5461 {
5462 low = CASE_LOW (gimple_switch_label (stmt, k));
5463 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
5464 {
5465 take_default = true;
5466 break;
5467 }
5468 high = low;
5469 if (CASE_HIGH (gimple_switch_label (stmt, k)))
5470 high = CASE_HIGH (gimple_switch_label (stmt, k));
5471 }
5472
5473 *min_idx = i;
5474 *max_idx = j;
5475 return !take_default;
5476 }
5477 }
5478
5479 /* Evaluate statement STMT. If the statement produces a useful range,
5480 return SSA_PROP_INTERESTING and record the SSA name with the
5481 interesting range into *OUTPUT_P.
5482
5483 If STMT is a conditional branch and we can determine its truth
5484 value, the taken edge is recorded in *TAKEN_EDGE_P.
5485
5486 If STMT produces a varying value, return SSA_PROP_VARYING. */
5487
5488 enum ssa_prop_result
5489 vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
5490 {
5491 value_range vr = VR_INITIALIZER;
5492 tree lhs = gimple_get_lhs (stmt);
5493 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
5494
5495 if (*output_p)
5496 {
5497 if (update_value_range (*output_p, &vr))
5498 {
5499 if (dump_file && (dump_flags & TDF_DETAILS))
5500 {
5501 fprintf (dump_file, "Found new range for ");
5502 print_generic_expr (dump_file, *output_p);
5503 fprintf (dump_file, ": ");
5504 dump_value_range (dump_file, &vr);
5505 fprintf (dump_file, "\n");
5506 }
5507
5508 if (vr.type == VR_VARYING)
5509 return SSA_PROP_VARYING;
5510
5511 return SSA_PROP_INTERESTING;
5512 }
5513 return SSA_PROP_NOT_INTERESTING;
5514 }
5515
5516 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
5517 switch (gimple_call_internal_fn (stmt))
5518 {
5519 case IFN_ADD_OVERFLOW:
5520 case IFN_SUB_OVERFLOW:
5521 case IFN_MUL_OVERFLOW:
5522 case IFN_ATOMIC_COMPARE_EXCHANGE:
5523 /* These internal calls return _Complex integer type,
5524 which VRP does not track, but the immediate uses
5525 thereof might be interesting. */
5526 if (lhs && TREE_CODE (lhs) == SSA_NAME)
5527 {
5528 imm_use_iterator iter;
5529 use_operand_p use_p;
5530 enum ssa_prop_result res = SSA_PROP_VARYING;
5531
5532 set_value_range_to_varying (get_value_range (lhs));
5533
5534 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5535 {
5536 gimple *use_stmt = USE_STMT (use_p);
5537 if (!is_gimple_assign (use_stmt))
5538 continue;
5539 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
5540 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
5541 continue;
5542 tree rhs1 = gimple_assign_rhs1 (use_stmt);
5543 tree use_lhs = gimple_assign_lhs (use_stmt);
5544 if (TREE_CODE (rhs1) != rhs_code
5545 || TREE_OPERAND (rhs1, 0) != lhs
5546 || TREE_CODE (use_lhs) != SSA_NAME
5547 || !stmt_interesting_for_vrp (use_stmt)
5548 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
5549 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
5550 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
5551 continue;
5552
5553 /* If there is a change in the value range for any of the
5554 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
5555 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
5556 or IMAGPART_EXPR immediate uses, but none of them have
5557 a change in their value ranges, return
5558 SSA_PROP_NOT_INTERESTING. If there are no
5559 {REAL,IMAG}PART_EXPR uses at all,
5560 return SSA_PROP_VARYING. */
5561 value_range new_vr = VR_INITIALIZER;
5562 extract_range_basic (&new_vr, use_stmt);
5563 value_range *old_vr = get_value_range (use_lhs);
5564 if (old_vr->type != new_vr.type
5565 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
5566 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
5567 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
5568 res = SSA_PROP_INTERESTING;
5569 else
5570 res = SSA_PROP_NOT_INTERESTING;
5571 BITMAP_FREE (new_vr.equiv);
5572 if (res == SSA_PROP_INTERESTING)
5573 {
5574 *output_p = lhs;
5575 return res;
5576 }
5577 }
5578
5579 return res;
5580 }
5581 break;
5582 default:
5583 break;
5584 }
5585
5586 /* All other statements produce nothing of interest for VRP, so mark
5587 their outputs varying and prevent further simulation. */
5588 set_defs_to_varying (stmt);
5589
5590 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
5591 }
5592
5593 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5594 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5595 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5596 possible such range. The resulting range is not canonicalized. */
5597
5598 static void
5599 union_ranges (enum value_range_type *vr0type,
5600 tree *vr0min, tree *vr0max,
5601 enum value_range_type vr1type,
5602 tree vr1min, tree vr1max)
5603 {
5604 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5605 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5606
5607 /* [] is vr0, () is vr1 in the following classification comments. */
5608 if (mineq && maxeq)
5609 {
5610 /* [( )] */
5611 if (*vr0type == vr1type)
5612 /* Nothing to do for equal ranges. */
5613 ;
5614 else if ((*vr0type == VR_RANGE
5615 && vr1type == VR_ANTI_RANGE)
5616 || (*vr0type == VR_ANTI_RANGE
5617 && vr1type == VR_RANGE))
5618 {
5619 /* For anti-range with range union the result is varying. */
5620 goto give_up;
5621 }
5622 else
5623 gcc_unreachable ();
5624 }
5625 else if (operand_less_p (*vr0max, vr1min) == 1
5626 || operand_less_p (vr1max, *vr0min) == 1)
5627 {
5628 /* [ ] ( ) or ( ) [ ]
5629 If the ranges have an empty intersection, result of the union
5630 operation is the anti-range or if both are anti-ranges
5631 it covers all. */
5632 if (*vr0type == VR_ANTI_RANGE
5633 && vr1type == VR_ANTI_RANGE)
5634 goto give_up;
5635 else if (*vr0type == VR_ANTI_RANGE
5636 && vr1type == VR_RANGE)
5637 ;
5638 else if (*vr0type == VR_RANGE
5639 && vr1type == VR_ANTI_RANGE)
5640 {
5641 *vr0type = vr1type;
5642 *vr0min = vr1min;
5643 *vr0max = vr1max;
5644 }
5645 else if (*vr0type == VR_RANGE
5646 && vr1type == VR_RANGE)
5647 {
5648 /* The result is the convex hull of both ranges. */
5649 if (operand_less_p (*vr0max, vr1min) == 1)
5650 {
5651 /* If the result can be an anti-range, create one. */
5652 if (TREE_CODE (*vr0max) == INTEGER_CST
5653 && TREE_CODE (vr1min) == INTEGER_CST
5654 && vrp_val_is_min (*vr0min)
5655 && vrp_val_is_max (vr1max))
5656 {
5657 tree min = int_const_binop (PLUS_EXPR,
5658 *vr0max,
5659 build_int_cst (TREE_TYPE (*vr0max), 1));
5660 tree max = int_const_binop (MINUS_EXPR,
5661 vr1min,
5662 build_int_cst (TREE_TYPE (vr1min), 1));
5663 if (!operand_less_p (max, min))
5664 {
5665 *vr0type = VR_ANTI_RANGE;
5666 *vr0min = min;
5667 *vr0max = max;
5668 }
5669 else
5670 *vr0max = vr1max;
5671 }
5672 else
5673 *vr0max = vr1max;
5674 }
5675 else
5676 {
5677 /* If the result can be an anti-range, create one. */
5678 if (TREE_CODE (vr1max) == INTEGER_CST
5679 && TREE_CODE (*vr0min) == INTEGER_CST
5680 && vrp_val_is_min (vr1min)
5681 && vrp_val_is_max (*vr0max))
5682 {
5683 tree min = int_const_binop (PLUS_EXPR,
5684 vr1max,
5685 build_int_cst (TREE_TYPE (vr1max), 1));
5686 tree max = int_const_binop (MINUS_EXPR,
5687 *vr0min,
5688 build_int_cst (TREE_TYPE (*vr0min), 1));
5689 if (!operand_less_p (max, min))
5690 {
5691 *vr0type = VR_ANTI_RANGE;
5692 *vr0min = min;
5693 *vr0max = max;
5694 }
5695 else
5696 *vr0min = vr1min;
5697 }
5698 else
5699 *vr0min = vr1min;
5700 }
5701 }
5702 else
5703 gcc_unreachable ();
5704 }
5705 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
5706 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
5707 {
5708 /* [ ( ) ] or [( ) ] or [ ( )] */
5709 if (*vr0type == VR_RANGE
5710 && vr1type == VR_RANGE)
5711 ;
5712 else if (*vr0type == VR_ANTI_RANGE
5713 && vr1type == VR_ANTI_RANGE)
5714 {
5715 *vr0type = vr1type;
5716 *vr0min = vr1min;
5717 *vr0max = vr1max;
5718 }
5719 else if (*vr0type == VR_ANTI_RANGE
5720 && vr1type == VR_RANGE)
5721 {
5722 /* Arbitrarily choose the right or left gap. */
5723 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
5724 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5725 build_int_cst (TREE_TYPE (vr1min), 1));
5726 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
5727 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5728 build_int_cst (TREE_TYPE (vr1max), 1));
5729 else
5730 goto give_up;
5731 }
5732 else if (*vr0type == VR_RANGE
5733 && vr1type == VR_ANTI_RANGE)
5734 /* The result covers everything. */
5735 goto give_up;
5736 else
5737 gcc_unreachable ();
5738 }
5739 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
5740 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
5741 {
5742 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5743 if (*vr0type == VR_RANGE
5744 && vr1type == VR_RANGE)
5745 {
5746 *vr0type = vr1type;
5747 *vr0min = vr1min;
5748 *vr0max = vr1max;
5749 }
5750 else if (*vr0type == VR_ANTI_RANGE
5751 && vr1type == VR_ANTI_RANGE)
5752 ;
5753 else if (*vr0type == VR_RANGE
5754 && vr1type == VR_ANTI_RANGE)
5755 {
5756 *vr0type = VR_ANTI_RANGE;
5757 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
5758 {
5759 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5760 build_int_cst (TREE_TYPE (*vr0min), 1));
5761 *vr0min = vr1min;
5762 }
5763 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
5764 {
5765 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5766 build_int_cst (TREE_TYPE (*vr0max), 1));
5767 *vr0max = vr1max;
5768 }
5769 else
5770 goto give_up;
5771 }
5772 else if (*vr0type == VR_ANTI_RANGE
5773 && vr1type == VR_RANGE)
5774 /* The result covers everything. */
5775 goto give_up;
5776 else
5777 gcc_unreachable ();
5778 }
5779 else if ((operand_less_p (vr1min, *vr0max) == 1
5780 || operand_equal_p (vr1min, *vr0max, 0))
5781 && operand_less_p (*vr0min, vr1min) == 1
5782 && operand_less_p (*vr0max, vr1max) == 1)
5783 {
5784 /* [ ( ] ) or [ ]( ) */
5785 if (*vr0type == VR_RANGE
5786 && vr1type == VR_RANGE)
5787 *vr0max = vr1max;
5788 else if (*vr0type == VR_ANTI_RANGE
5789 && vr1type == VR_ANTI_RANGE)
5790 *vr0min = vr1min;
5791 else if (*vr0type == VR_ANTI_RANGE
5792 && vr1type == VR_RANGE)
5793 {
5794 if (TREE_CODE (vr1min) == INTEGER_CST)
5795 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5796 build_int_cst (TREE_TYPE (vr1min), 1));
5797 else
5798 goto give_up;
5799 }
5800 else if (*vr0type == VR_RANGE
5801 && vr1type == VR_ANTI_RANGE)
5802 {
5803 if (TREE_CODE (*vr0max) == INTEGER_CST)
5804 {
5805 *vr0type = vr1type;
5806 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5807 build_int_cst (TREE_TYPE (*vr0max), 1));
5808 *vr0max = vr1max;
5809 }
5810 else
5811 goto give_up;
5812 }
5813 else
5814 gcc_unreachable ();
5815 }
5816 else if ((operand_less_p (*vr0min, vr1max) == 1
5817 || operand_equal_p (*vr0min, vr1max, 0))
5818 && operand_less_p (vr1min, *vr0min) == 1
5819 && operand_less_p (vr1max, *vr0max) == 1)
5820 {
5821 /* ( [ ) ] or ( )[ ] */
5822 if (*vr0type == VR_RANGE
5823 && vr1type == VR_RANGE)
5824 *vr0min = vr1min;
5825 else if (*vr0type == VR_ANTI_RANGE
5826 && vr1type == VR_ANTI_RANGE)
5827 *vr0max = vr1max;
5828 else if (*vr0type == VR_ANTI_RANGE
5829 && vr1type == VR_RANGE)
5830 {
5831 if (TREE_CODE (vr1max) == INTEGER_CST)
5832 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5833 build_int_cst (TREE_TYPE (vr1max), 1));
5834 else
5835 goto give_up;
5836 }
5837 else if (*vr0type == VR_RANGE
5838 && vr1type == VR_ANTI_RANGE)
5839 {
5840 if (TREE_CODE (*vr0min) == INTEGER_CST)
5841 {
5842 *vr0type = vr1type;
5843 *vr0min = vr1min;
5844 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5845 build_int_cst (TREE_TYPE (*vr0min), 1));
5846 }
5847 else
5848 goto give_up;
5849 }
5850 else
5851 gcc_unreachable ();
5852 }
5853 else
5854 goto give_up;
5855
5856 return;
5857
5858 give_up:
5859 *vr0type = VR_VARYING;
5860 *vr0min = NULL_TREE;
5861 *vr0max = NULL_TREE;
5862 }
5863
5864 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5865 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5866 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5867 possible such range. The resulting range is not canonicalized. */
5868
5869 static void
5870 intersect_ranges (enum value_range_type *vr0type,
5871 tree *vr0min, tree *vr0max,
5872 enum value_range_type vr1type,
5873 tree vr1min, tree vr1max)
5874 {
5875 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5876 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5877
5878 /* [] is vr0, () is vr1 in the following classification comments. */
5879 if (mineq && maxeq)
5880 {
5881 /* [( )] */
5882 if (*vr0type == vr1type)
5883 /* Nothing to do for equal ranges. */
5884 ;
5885 else if ((*vr0type == VR_RANGE
5886 && vr1type == VR_ANTI_RANGE)
5887 || (*vr0type == VR_ANTI_RANGE
5888 && vr1type == VR_RANGE))
5889 {
5890 /* For anti-range with range intersection the result is empty. */
5891 *vr0type = VR_UNDEFINED;
5892 *vr0min = NULL_TREE;
5893 *vr0max = NULL_TREE;
5894 }
5895 else
5896 gcc_unreachable ();
5897 }
5898 else if (operand_less_p (*vr0max, vr1min) == 1
5899 || operand_less_p (vr1max, *vr0min) == 1)
5900 {
5901 /* [ ] ( ) or ( ) [ ]
5902 If the ranges have an empty intersection, the result of the
5903 intersect operation is the range for intersecting an
5904 anti-range with a range or empty when intersecting two ranges. */
5905 if (*vr0type == VR_RANGE
5906 && vr1type == VR_ANTI_RANGE)
5907 ;
5908 else if (*vr0type == VR_ANTI_RANGE
5909 && vr1type == VR_RANGE)
5910 {
5911 *vr0type = vr1type;
5912 *vr0min = vr1min;
5913 *vr0max = vr1max;
5914 }
5915 else if (*vr0type == VR_RANGE
5916 && vr1type == VR_RANGE)
5917 {
5918 *vr0type = VR_UNDEFINED;
5919 *vr0min = NULL_TREE;
5920 *vr0max = NULL_TREE;
5921 }
5922 else if (*vr0type == VR_ANTI_RANGE
5923 && vr1type == VR_ANTI_RANGE)
5924 {
5925 /* If the anti-ranges are adjacent to each other merge them. */
5926 if (TREE_CODE (*vr0max) == INTEGER_CST
5927 && TREE_CODE (vr1min) == INTEGER_CST
5928 && operand_less_p (*vr0max, vr1min) == 1
5929 && integer_onep (int_const_binop (MINUS_EXPR,
5930 vr1min, *vr0max)))
5931 *vr0max = vr1max;
5932 else if (TREE_CODE (vr1max) == INTEGER_CST
5933 && TREE_CODE (*vr0min) == INTEGER_CST
5934 && operand_less_p (vr1max, *vr0min) == 1
5935 && integer_onep (int_const_binop (MINUS_EXPR,
5936 *vr0min, vr1max)))
5937 *vr0min = vr1min;
5938 /* Else arbitrarily take VR0. */
5939 }
5940 }
5941 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
5942 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
5943 {
5944 /* [ ( ) ] or [( ) ] or [ ( )] */
5945 if (*vr0type == VR_RANGE
5946 && vr1type == VR_RANGE)
5947 {
5948 /* If both are ranges the result is the inner one. */
5949 *vr0type = vr1type;
5950 *vr0min = vr1min;
5951 *vr0max = vr1max;
5952 }
5953 else if (*vr0type == VR_RANGE
5954 && vr1type == VR_ANTI_RANGE)
5955 {
5956 /* Choose the right gap if the left one is empty. */
5957 if (mineq)
5958 {
5959 if (TREE_CODE (vr1max) != INTEGER_CST)
5960 *vr0min = vr1max;
5961 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
5962 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
5963 *vr0min
5964 = int_const_binop (MINUS_EXPR, vr1max,
5965 build_int_cst (TREE_TYPE (vr1max), -1));
5966 else
5967 *vr0min
5968 = int_const_binop (PLUS_EXPR, vr1max,
5969 build_int_cst (TREE_TYPE (vr1max), 1));
5970 }
5971 /* Choose the left gap if the right one is empty. */
5972 else if (maxeq)
5973 {
5974 if (TREE_CODE (vr1min) != INTEGER_CST)
5975 *vr0max = vr1min;
5976 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
5977 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
5978 *vr0max
5979 = int_const_binop (PLUS_EXPR, vr1min,
5980 build_int_cst (TREE_TYPE (vr1min), -1));
5981 else
5982 *vr0max
5983 = int_const_binop (MINUS_EXPR, vr1min,
5984 build_int_cst (TREE_TYPE (vr1min), 1));
5985 }
5986 /* Choose the anti-range if the range is effectively varying. */
5987 else if (vrp_val_is_min (*vr0min)
5988 && vrp_val_is_max (*vr0max))
5989 {
5990 *vr0type = vr1type;
5991 *vr0min = vr1min;
5992 *vr0max = vr1max;
5993 }
5994 /* Else choose the range. */
5995 }
5996 else if (*vr0type == VR_ANTI_RANGE
5997 && vr1type == VR_ANTI_RANGE)
5998 /* If both are anti-ranges the result is the outer one. */
5999 ;
6000 else if (*vr0type == VR_ANTI_RANGE
6001 && vr1type == VR_RANGE)
6002 {
6003 /* The intersection is empty. */
6004 *vr0type = VR_UNDEFINED;
6005 *vr0min = NULL_TREE;
6006 *vr0max = NULL_TREE;
6007 }
6008 else
6009 gcc_unreachable ();
6010 }
6011 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
6012 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
6013 {
6014 /* ( [ ] ) or ([ ] ) or ( [ ]) */
6015 if (*vr0type == VR_RANGE
6016 && vr1type == VR_RANGE)
6017 /* Choose the inner range. */
6018 ;
6019 else if (*vr0type == VR_ANTI_RANGE
6020 && vr1type == VR_RANGE)
6021 {
6022 /* Choose the right gap if the left is empty. */
6023 if (mineq)
6024 {
6025 *vr0type = VR_RANGE;
6026 if (TREE_CODE (*vr0max) != INTEGER_CST)
6027 *vr0min = *vr0max;
6028 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
6029 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
6030 *vr0min
6031 = int_const_binop (MINUS_EXPR, *vr0max,
6032 build_int_cst (TREE_TYPE (*vr0max), -1));
6033 else
6034 *vr0min
6035 = int_const_binop (PLUS_EXPR, *vr0max,
6036 build_int_cst (TREE_TYPE (*vr0max), 1));
6037 *vr0max = vr1max;
6038 }
6039 /* Choose the left gap if the right is empty. */
6040 else if (maxeq)
6041 {
6042 *vr0type = VR_RANGE;
6043 if (TREE_CODE (*vr0min) != INTEGER_CST)
6044 *vr0max = *vr0min;
6045 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
6046 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
6047 *vr0max
6048 = int_const_binop (PLUS_EXPR, *vr0min,
6049 build_int_cst (TREE_TYPE (*vr0min), -1));
6050 else
6051 *vr0max
6052 = int_const_binop (MINUS_EXPR, *vr0min,
6053 build_int_cst (TREE_TYPE (*vr0min), 1));
6054 *vr0min = vr1min;
6055 }
6056 /* Choose the anti-range if the range is effectively varying. */
6057 else if (vrp_val_is_min (vr1min)
6058 && vrp_val_is_max (vr1max))
6059 ;
6060 /* Choose the anti-range if it is ~[0,0], that range is special
6061 enough to special case when vr1's range is relatively wide.
6062 At least for types bigger than int - this covers pointers
6063 and arguments to functions like ctz. */
6064 else if (*vr0min == *vr0max
6065 && integer_zerop (*vr0min)
6066 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
6067 >= TYPE_PRECISION (integer_type_node))
6068 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
6069 && TREE_CODE (vr1max) == INTEGER_CST
6070 && TREE_CODE (vr1min) == INTEGER_CST
6071 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
6072 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
6073 ;
6074 /* Else choose the range. */
6075 else
6076 {
6077 *vr0type = vr1type;
6078 *vr0min = vr1min;
6079 *vr0max = vr1max;
6080 }
6081 }
6082 else if (*vr0type == VR_ANTI_RANGE
6083 && vr1type == VR_ANTI_RANGE)
6084 {
6085 /* If both are anti-ranges the result is the outer one. */
6086 *vr0type = vr1type;
6087 *vr0min = vr1min;
6088 *vr0max = vr1max;
6089 }
6090 else if (vr1type == VR_ANTI_RANGE
6091 && *vr0type == VR_RANGE)
6092 {
6093 /* The intersection is empty. */
6094 *vr0type = VR_UNDEFINED;
6095 *vr0min = NULL_TREE;
6096 *vr0max = NULL_TREE;
6097 }
6098 else
6099 gcc_unreachable ();
6100 }
6101 else if ((operand_less_p (vr1min, *vr0max) == 1
6102 || operand_equal_p (vr1min, *vr0max, 0))
6103 && operand_less_p (*vr0min, vr1min) == 1)
6104 {
6105 /* [ ( ] ) or [ ]( ) */
6106 if (*vr0type == VR_ANTI_RANGE
6107 && vr1type == VR_ANTI_RANGE)
6108 *vr0max = vr1max;
6109 else if (*vr0type == VR_RANGE
6110 && vr1type == VR_RANGE)
6111 *vr0min = vr1min;
6112 else if (*vr0type == VR_RANGE
6113 && vr1type == VR_ANTI_RANGE)
6114 {
6115 if (TREE_CODE (vr1min) == INTEGER_CST)
6116 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
6117 build_int_cst (TREE_TYPE (vr1min), 1));
6118 else
6119 *vr0max = vr1min;
6120 }
6121 else if (*vr0type == VR_ANTI_RANGE
6122 && vr1type == VR_RANGE)
6123 {
6124 *vr0type = VR_RANGE;
6125 if (TREE_CODE (*vr0max) == INTEGER_CST)
6126 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
6127 build_int_cst (TREE_TYPE (*vr0max), 1));
6128 else
6129 *vr0min = *vr0max;
6130 *vr0max = vr1max;
6131 }
6132 else
6133 gcc_unreachable ();
6134 }
6135 else if ((operand_less_p (*vr0min, vr1max) == 1
6136 || operand_equal_p (*vr0min, vr1max, 0))
6137 && operand_less_p (vr1min, *vr0min) == 1)
6138 {
6139 /* ( [ ) ] or ( )[ ] */
6140 if (*vr0type == VR_ANTI_RANGE
6141 && vr1type == VR_ANTI_RANGE)
6142 *vr0min = vr1min;
6143 else if (*vr0type == VR_RANGE
6144 && vr1type == VR_RANGE)
6145 *vr0max = vr1max;
6146 else if (*vr0type == VR_RANGE
6147 && vr1type == VR_ANTI_RANGE)
6148 {
6149 if (TREE_CODE (vr1max) == INTEGER_CST)
6150 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
6151 build_int_cst (TREE_TYPE (vr1max), 1));
6152 else
6153 *vr0min = vr1max;
6154 }
6155 else if (*vr0type == VR_ANTI_RANGE
6156 && vr1type == VR_RANGE)
6157 {
6158 *vr0type = VR_RANGE;
6159 if (TREE_CODE (*vr0min) == INTEGER_CST)
6160 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
6161 build_int_cst (TREE_TYPE (*vr0min), 1));
6162 else
6163 *vr0max = *vr0min;
6164 *vr0min = vr1min;
6165 }
6166 else
6167 gcc_unreachable ();
6168 }
6169
6170 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
6171 result for the intersection. That's always a conservative
6172 correct estimate unless VR1 is a constant singleton range
6173 in which case we choose that. */
6174 if (vr1type == VR_RANGE
6175 && is_gimple_min_invariant (vr1min)
6176 && vrp_operand_equal_p (vr1min, vr1max))
6177 {
6178 *vr0type = vr1type;
6179 *vr0min = vr1min;
6180 *vr0max = vr1max;
6181 }
6182
6183 return;
6184 }
6185
6186
6187 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
6188 in *VR0. This may not be the smallest possible such range. */
6189
6190 static void
6191 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
6192 {
6193 value_range saved;
6194
6195 /* If either range is VR_VARYING the other one wins. */
6196 if (vr1->type == VR_VARYING)
6197 return;
6198 if (vr0->type == VR_VARYING)
6199 {
6200 copy_value_range (vr0, vr1);
6201 return;
6202 }
6203
6204 /* When either range is VR_UNDEFINED the resulting range is
6205 VR_UNDEFINED, too. */
6206 if (vr0->type == VR_UNDEFINED)
6207 return;
6208 if (vr1->type == VR_UNDEFINED)
6209 {
6210 set_value_range_to_undefined (vr0);
6211 return;
6212 }
6213
6214 /* Save the original vr0 so we can return it as conservative intersection
6215 result when our worker turns things to varying. */
6216 saved = *vr0;
6217 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
6218 vr1->type, vr1->min, vr1->max);
6219 /* Make sure to canonicalize the result though as the inversion of a
6220 VR_RANGE can still be a VR_RANGE. */
6221 set_and_canonicalize_value_range (vr0, vr0->type,
6222 vr0->min, vr0->max, vr0->equiv);
6223 /* If that failed, use the saved original VR0. */
6224 if (vr0->type == VR_VARYING)
6225 {
6226 *vr0 = saved;
6227 return;
6228 }
6229 /* If the result is VR_UNDEFINED there is no need to mess with
6230 the equivalencies. */
6231 if (vr0->type == VR_UNDEFINED)
6232 return;
6233
6234 /* The resulting set of equivalences for range intersection is the union of
6235 the two sets. */
6236 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
6237 bitmap_ior_into (vr0->equiv, vr1->equiv);
6238 else if (vr1->equiv && !vr0->equiv)
6239 {
6240 /* All equivalence bitmaps are allocated from the same obstack. So
6241 we can use the obstack associated with VR to allocate vr0->equiv. */
6242 vr0->equiv = BITMAP_ALLOC (vr1->equiv->obstack);
6243 bitmap_copy (vr0->equiv, vr1->equiv);
6244 }
6245 }
6246
6247 void
6248 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
6249 {
6250 if (dump_file && (dump_flags & TDF_DETAILS))
6251 {
6252 fprintf (dump_file, "Intersecting\n ");
6253 dump_value_range (dump_file, vr0);
6254 fprintf (dump_file, "\nand\n ");
6255 dump_value_range (dump_file, vr1);
6256 fprintf (dump_file, "\n");
6257 }
6258 vrp_intersect_ranges_1 (vr0, vr1);
6259 if (dump_file && (dump_flags & TDF_DETAILS))
6260 {
6261 fprintf (dump_file, "to\n ");
6262 dump_value_range (dump_file, vr0);
6263 fprintf (dump_file, "\n");
6264 }
6265 }
6266
6267 /* Meet operation for value ranges. Given two value ranges VR0 and
6268 VR1, store in VR0 a range that contains both VR0 and VR1. This
6269 may not be the smallest possible such range. */
6270
6271 static void
6272 vrp_meet_1 (value_range *vr0, const value_range *vr1)
6273 {
6274 value_range saved;
6275
6276 if (vr0->type == VR_UNDEFINED)
6277 {
6278 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
6279 return;
6280 }
6281
6282 if (vr1->type == VR_UNDEFINED)
6283 {
6284 /* VR0 already has the resulting range. */
6285 return;
6286 }
6287
6288 if (vr0->type == VR_VARYING)
6289 {
6290 /* Nothing to do. VR0 already has the resulting range. */
6291 return;
6292 }
6293
6294 if (vr1->type == VR_VARYING)
6295 {
6296 set_value_range_to_varying (vr0);
6297 return;
6298 }
6299
6300 saved = *vr0;
6301 union_ranges (&vr0->type, &vr0->min, &vr0->max,
6302 vr1->type, vr1->min, vr1->max);
6303 if (vr0->type == VR_VARYING)
6304 {
6305 /* Failed to find an efficient meet. Before giving up and setting
6306 the result to VARYING, see if we can at least derive a useful
6307 anti-range. FIXME, all this nonsense about distinguishing
6308 anti-ranges from ranges is necessary because of the odd
6309 semantics of range_includes_zero_p and friends. */
6310 if (((saved.type == VR_RANGE
6311 && range_includes_zero_p (saved.min, saved.max) == 0)
6312 || (saved.type == VR_ANTI_RANGE
6313 && range_includes_zero_p (saved.min, saved.max) == 1))
6314 && ((vr1->type == VR_RANGE
6315 && range_includes_zero_p (vr1->min, vr1->max) == 0)
6316 || (vr1->type == VR_ANTI_RANGE
6317 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
6318 {
6319 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
6320
6321 /* Since this meet operation did not result from the meeting of
6322 two equivalent names, VR0 cannot have any equivalences. */
6323 if (vr0->equiv)
6324 bitmap_clear (vr0->equiv);
6325 return;
6326 }
6327
6328 set_value_range_to_varying (vr0);
6329 return;
6330 }
6331 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
6332 vr0->equiv);
6333 if (vr0->type == VR_VARYING)
6334 return;
6335
6336 /* The resulting set of equivalences is always the intersection of
6337 the two sets. */
6338 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
6339 bitmap_and_into (vr0->equiv, vr1->equiv);
6340 else if (vr0->equiv && !vr1->equiv)
6341 bitmap_clear (vr0->equiv);
6342 }
6343
6344 void
6345 vrp_meet (value_range *vr0, const value_range *vr1)
6346 {
6347 if (dump_file && (dump_flags & TDF_DETAILS))
6348 {
6349 fprintf (dump_file, "Meeting\n ");
6350 dump_value_range (dump_file, vr0);
6351 fprintf (dump_file, "\nand\n ");
6352 dump_value_range (dump_file, vr1);
6353 fprintf (dump_file, "\n");
6354 }
6355 vrp_meet_1 (vr0, vr1);
6356 if (dump_file && (dump_flags & TDF_DETAILS))
6357 {
6358 fprintf (dump_file, "to\n ");
6359 dump_value_range (dump_file, vr0);
6360 fprintf (dump_file, "\n");
6361 }
6362 }
6363
6364
6365 /* Visit all arguments for PHI node PHI that flow through executable
6366 edges. If a valid value range can be derived from all the incoming
6367 value ranges, set a new range for the LHS of PHI. */
6368
6369 enum ssa_prop_result
6370 vrp_prop::visit_phi (gphi *phi)
6371 {
6372 tree lhs = PHI_RESULT (phi);
6373 value_range vr_result = VR_INITIALIZER;
6374 extract_range_from_phi_node (phi, &vr_result);
6375 if (update_value_range (lhs, &vr_result))
6376 {
6377 if (dump_file && (dump_flags & TDF_DETAILS))
6378 {
6379 fprintf (dump_file, "Found new range for ");
6380 print_generic_expr (dump_file, lhs);
6381 fprintf (dump_file, ": ");
6382 dump_value_range (dump_file, &vr_result);
6383 fprintf (dump_file, "\n");
6384 }
6385
6386 if (vr_result.type == VR_VARYING)
6387 return SSA_PROP_VARYING;
6388
6389 return SSA_PROP_INTERESTING;
6390 }
6391
6392 /* Nothing changed, don't add outgoing edges. */
6393 return SSA_PROP_NOT_INTERESTING;
6394 }
6395
6396 class vrp_folder : public substitute_and_fold_engine
6397 {
6398 public:
6399 tree get_value (tree) FINAL OVERRIDE;
6400 bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE;
6401 bool fold_predicate_in (gimple_stmt_iterator *);
6402
6403 class vr_values *vr_values;
6404
6405 /* Delegators. */
6406 tree vrp_evaluate_conditional (tree_code code, tree op0,
6407 tree op1, gimple *stmt)
6408 { return vr_values->vrp_evaluate_conditional (code, op0, op1, stmt); }
6409 bool simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
6410 { return vr_values->simplify_stmt_using_ranges (gsi); }
6411 tree op_with_constant_singleton_value_range (tree op)
6412 { return vr_values->op_with_constant_singleton_value_range (op); }
6413 };
6414
6415 /* If the statement pointed by SI has a predicate whose value can be
6416 computed using the value range information computed by VRP, compute
6417 its value and return true. Otherwise, return false. */
6418
6419 bool
6420 vrp_folder::fold_predicate_in (gimple_stmt_iterator *si)
6421 {
6422 bool assignment_p = false;
6423 tree val;
6424 gimple *stmt = gsi_stmt (*si);
6425
6426 if (is_gimple_assign (stmt)
6427 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
6428 {
6429 assignment_p = true;
6430 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
6431 gimple_assign_rhs1 (stmt),
6432 gimple_assign_rhs2 (stmt),
6433 stmt);
6434 }
6435 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6436 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
6437 gimple_cond_lhs (cond_stmt),
6438 gimple_cond_rhs (cond_stmt),
6439 stmt);
6440 else
6441 return false;
6442
6443 if (val)
6444 {
6445 if (assignment_p)
6446 val = fold_convert (gimple_expr_type (stmt), val);
6447
6448 if (dump_file)
6449 {
6450 fprintf (dump_file, "Folding predicate ");
6451 print_gimple_expr (dump_file, stmt, 0);
6452 fprintf (dump_file, " to ");
6453 print_generic_expr (dump_file, val);
6454 fprintf (dump_file, "\n");
6455 }
6456
6457 if (is_gimple_assign (stmt))
6458 gimple_assign_set_rhs_from_tree (si, val);
6459 else
6460 {
6461 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
6462 gcond *cond_stmt = as_a <gcond *> (stmt);
6463 if (integer_zerop (val))
6464 gimple_cond_make_false (cond_stmt);
6465 else if (integer_onep (val))
6466 gimple_cond_make_true (cond_stmt);
6467 else
6468 gcc_unreachable ();
6469 }
6470
6471 return true;
6472 }
6473
6474 return false;
6475 }
6476
6477 /* Callback for substitute_and_fold folding the stmt at *SI. */
6478
6479 bool
6480 vrp_folder::fold_stmt (gimple_stmt_iterator *si)
6481 {
6482 if (fold_predicate_in (si))
6483 return true;
6484
6485 return simplify_stmt_using_ranges (si);
6486 }
6487
6488 /* If OP has a value range with a single constant value return that,
6489 otherwise return NULL_TREE. This returns OP itself if OP is a
6490 constant.
6491
6492 Implemented as a pure wrapper right now, but this will change. */
6493
6494 tree
6495 vrp_folder::get_value (tree op)
6496 {
6497 return op_with_constant_singleton_value_range (op);
6498 }
6499
6500 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
6501 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
6502 BB. If no such ASSERT_EXPR is found, return OP. */
6503
6504 static tree
6505 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
6506 {
6507 imm_use_iterator imm_iter;
6508 gimple *use_stmt;
6509 use_operand_p use_p;
6510
6511 if (TREE_CODE (op) == SSA_NAME)
6512 {
6513 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
6514 {
6515 use_stmt = USE_STMT (use_p);
6516 if (use_stmt != stmt
6517 && gimple_assign_single_p (use_stmt)
6518 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
6519 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
6520 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
6521 return gimple_assign_lhs (use_stmt);
6522 }
6523 }
6524 return op;
6525 }
6526
6527 /* A hack. */
6528 static class vr_values *x_vr_values;
6529
6530 /* A trivial wrapper so that we can present the generic jump threading
6531 code with a simple API for simplifying statements. STMT is the
6532 statement we want to simplify, WITHIN_STMT provides the location
6533 for any overflow warnings. */
6534
6535 static tree
6536 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
6537 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
6538 basic_block bb)
6539 {
6540 /* First see if the conditional is in the hash table. */
6541 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
6542 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
6543 return cached_lhs;
6544
6545 vr_values *vr_values = x_vr_values;
6546 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6547 {
6548 tree op0 = gimple_cond_lhs (cond_stmt);
6549 op0 = lhs_of_dominating_assert (op0, bb, stmt);
6550
6551 tree op1 = gimple_cond_rhs (cond_stmt);
6552 op1 = lhs_of_dominating_assert (op1, bb, stmt);
6553
6554 return vr_values->vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
6555 op0, op1, within_stmt);
6556 }
6557
6558 /* We simplify a switch statement by trying to determine which case label
6559 will be taken. If we are successful then we return the corresponding
6560 CASE_LABEL_EXPR. */
6561 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
6562 {
6563 tree op = gimple_switch_index (switch_stmt);
6564 if (TREE_CODE (op) != SSA_NAME)
6565 return NULL_TREE;
6566
6567 op = lhs_of_dominating_assert (op, bb, stmt);
6568
6569 value_range *vr = vr_values->get_value_range (op);
6570 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
6571 || symbolic_range_p (vr))
6572 return NULL_TREE;
6573
6574 if (vr->type == VR_RANGE)
6575 {
6576 size_t i, j;
6577 /* Get the range of labels that contain a part of the operand's
6578 value range. */
6579 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
6580
6581 /* Is there only one such label? */
6582 if (i == j)
6583 {
6584 tree label = gimple_switch_label (switch_stmt, i);
6585
6586 /* The i'th label will be taken only if the value range of the
6587 operand is entirely within the bounds of this label. */
6588 if (CASE_HIGH (label) != NULL_TREE
6589 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
6590 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
6591 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
6592 && tree_int_cst_equal (vr->min, vr->max)))
6593 return label;
6594 }
6595
6596 /* If there are no such labels then the default label will be
6597 taken. */
6598 if (i > j)
6599 return gimple_switch_label (switch_stmt, 0);
6600 }
6601
6602 if (vr->type == VR_ANTI_RANGE)
6603 {
6604 unsigned n = gimple_switch_num_labels (switch_stmt);
6605 tree min_label = gimple_switch_label (switch_stmt, 1);
6606 tree max_label = gimple_switch_label (switch_stmt, n - 1);
6607
6608 /* The default label will be taken only if the anti-range of the
6609 operand is entirely outside the bounds of all the (non-default)
6610 case labels. */
6611 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
6612 && (CASE_HIGH (max_label) != NULL_TREE
6613 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
6614 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
6615 return gimple_switch_label (switch_stmt, 0);
6616 }
6617
6618 return NULL_TREE;
6619 }
6620
6621 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
6622 {
6623 tree lhs = gimple_assign_lhs (assign_stmt);
6624 if (TREE_CODE (lhs) == SSA_NAME
6625 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6626 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6627 && stmt_interesting_for_vrp (stmt))
6628 {
6629 edge dummy_e;
6630 tree dummy_tree;
6631 value_range new_vr = VR_INITIALIZER;
6632 vr_values->extract_range_from_stmt (stmt, &dummy_e,
6633 &dummy_tree, &new_vr);
6634 if (range_int_cst_singleton_p (&new_vr))
6635 return new_vr.min;
6636 }
6637 }
6638
6639 return NULL_TREE;
6640 }
6641
6642 class vrp_dom_walker : public dom_walker
6643 {
6644 public:
6645 vrp_dom_walker (cdi_direction direction,
6646 class const_and_copies *const_and_copies,
6647 class avail_exprs_stack *avail_exprs_stack)
6648 : dom_walker (direction, true),
6649 m_const_and_copies (const_and_copies),
6650 m_avail_exprs_stack (avail_exprs_stack),
6651 m_dummy_cond (NULL) {}
6652
6653 virtual edge before_dom_children (basic_block);
6654 virtual void after_dom_children (basic_block);
6655
6656 class vr_values *vr_values;
6657
6658 private:
6659 class const_and_copies *m_const_and_copies;
6660 class avail_exprs_stack *m_avail_exprs_stack;
6661
6662 gcond *m_dummy_cond;
6663
6664 };
6665
6666 /* Called before processing dominator children of BB. We want to look
6667 at ASSERT_EXPRs and record information from them in the appropriate
6668 tables.
6669
6670 We could look at other statements here. It's not seen as likely
6671 to significantly increase the jump threads we discover. */
6672
6673 edge
6674 vrp_dom_walker::before_dom_children (basic_block bb)
6675 {
6676 gimple_stmt_iterator gsi;
6677
6678 m_avail_exprs_stack->push_marker ();
6679 m_const_and_copies->push_marker ();
6680 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6681 {
6682 gimple *stmt = gsi_stmt (gsi);
6683 if (gimple_assign_single_p (stmt)
6684 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
6685 {
6686 tree rhs1 = gimple_assign_rhs1 (stmt);
6687 tree cond = TREE_OPERAND (rhs1, 1);
6688 tree inverted = invert_truthvalue (cond);
6689 vec<cond_equivalence> p;
6690 p.create (3);
6691 record_conditions (&p, cond, inverted);
6692 for (unsigned int i = 0; i < p.length (); i++)
6693 m_avail_exprs_stack->record_cond (&p[i]);
6694
6695 tree lhs = gimple_assign_lhs (stmt);
6696 m_const_and_copies->record_const_or_copy (lhs,
6697 TREE_OPERAND (rhs1, 0));
6698 p.release ();
6699 continue;
6700 }
6701 break;
6702 }
6703 return NULL;
6704 }
6705
6706 /* Called after processing dominator children of BB. This is where we
6707 actually call into the threader. */
6708 void
6709 vrp_dom_walker::after_dom_children (basic_block bb)
6710 {
6711 if (!m_dummy_cond)
6712 m_dummy_cond = gimple_build_cond (NE_EXPR,
6713 integer_zero_node, integer_zero_node,
6714 NULL, NULL);
6715
6716 x_vr_values = vr_values;
6717 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
6718 m_avail_exprs_stack, NULL,
6719 simplify_stmt_for_jump_threading);
6720 x_vr_values = NULL;
6721
6722 m_avail_exprs_stack->pop_to_marker ();
6723 m_const_and_copies->pop_to_marker ();
6724 }
6725
6726 /* Blocks which have more than one predecessor and more than
6727 one successor present jump threading opportunities, i.e.,
6728 when the block is reached from a specific predecessor, we
6729 may be able to determine which of the outgoing edges will
6730 be traversed. When this optimization applies, we are able
6731 to avoid conditionals at runtime and we may expose secondary
6732 optimization opportunities.
6733
6734 This routine is effectively a driver for the generic jump
6735 threading code. It basically just presents the generic code
6736 with edges that may be suitable for jump threading.
6737
6738 Unlike DOM, we do not iterate VRP if jump threading was successful.
6739 While iterating may expose new opportunities for VRP, it is expected
6740 those opportunities would be very limited and the compile time cost
6741 to expose those opportunities would be significant.
6742
6743 As jump threading opportunities are discovered, they are registered
6744 for later realization. */
6745
6746 static void
6747 identify_jump_threads (class vr_values *vr_values)
6748 {
6749 int i;
6750 edge e;
6751
6752 /* Ugh. When substituting values earlier in this pass we can
6753 wipe the dominance information. So rebuild the dominator
6754 information as we need it within the jump threading code. */
6755 calculate_dominance_info (CDI_DOMINATORS);
6756
6757 /* We do not allow VRP information to be used for jump threading
6758 across a back edge in the CFG. Otherwise it becomes too
6759 difficult to avoid eliminating loop exit tests. Of course
6760 EDGE_DFS_BACK is not accurate at this time so we have to
6761 recompute it. */
6762 mark_dfs_back_edges ();
6763
6764 /* Do not thread across edges we are about to remove. Just marking
6765 them as EDGE_IGNORE will do. */
6766 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
6767 e->flags |= EDGE_IGNORE;
6768
6769 /* Allocate our unwinder stack to unwind any temporary equivalences
6770 that might be recorded. */
6771 const_and_copies *equiv_stack = new const_and_copies ();
6772
6773 hash_table<expr_elt_hasher> *avail_exprs
6774 = new hash_table<expr_elt_hasher> (1024);
6775 avail_exprs_stack *avail_exprs_stack
6776 = new class avail_exprs_stack (avail_exprs);
6777
6778 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
6779 walker.vr_values = vr_values;
6780 walker.walk (cfun->cfg->x_entry_block_ptr);
6781
6782 /* Clear EDGE_IGNORE. */
6783 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
6784 e->flags &= ~EDGE_IGNORE;
6785
6786 /* We do not actually update the CFG or SSA graphs at this point as
6787 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
6788 handle ASSERT_EXPRs gracefully. */
6789 delete equiv_stack;
6790 delete avail_exprs;
6791 delete avail_exprs_stack;
6792 }
6793
6794 /* Traverse all the blocks folding conditionals with known ranges. */
6795
6796 void
6797 vrp_prop::vrp_finalize (bool warn_array_bounds_p)
6798 {
6799 size_t i;
6800
6801 /* We have completed propagating through the lattice. */
6802 vr_values.set_lattice_propagation_complete ();
6803
6804 if (dump_file)
6805 {
6806 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
6807 vr_values.dump_all_value_ranges (dump_file);
6808 fprintf (dump_file, "\n");
6809 }
6810
6811 /* Set value range to non pointer SSA_NAMEs. */
6812 for (i = 0; i < num_ssa_names; i++)
6813 {
6814 tree name = ssa_name (i);
6815 if (!name)
6816 continue;
6817
6818 value_range *vr = get_value_range (name);
6819 if (!name
6820 || (vr->type == VR_VARYING)
6821 || (vr->type == VR_UNDEFINED)
6822 || (TREE_CODE (vr->min) != INTEGER_CST)
6823 || (TREE_CODE (vr->max) != INTEGER_CST))
6824 continue;
6825
6826 if (POINTER_TYPE_P (TREE_TYPE (name))
6827 && ((vr->type == VR_RANGE
6828 && range_includes_zero_p (vr->min, vr->max) == 0)
6829 || (vr->type == VR_ANTI_RANGE
6830 && range_includes_zero_p (vr->min, vr->max) == 1)))
6831 set_ptr_nonnull (name);
6832 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
6833 set_range_info (name, vr->type,
6834 wi::to_wide (vr->min),
6835 wi::to_wide (vr->max));
6836 }
6837
6838 class vrp_folder vrp_folder;
6839 vrp_folder.vr_values = &vr_values;
6840 vrp_folder.substitute_and_fold ();
6841
6842 if (warn_array_bounds && warn_array_bounds_p)
6843 check_all_array_refs ();
6844 }
6845
6846 /* Main entry point to VRP (Value Range Propagation). This pass is
6847 loosely based on J. R. C. Patterson, ``Accurate Static Branch
6848 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
6849 Programming Language Design and Implementation, pp. 67-78, 1995.
6850 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
6851
6852 This is essentially an SSA-CCP pass modified to deal with ranges
6853 instead of constants.
6854
6855 While propagating ranges, we may find that two or more SSA name
6856 have equivalent, though distinct ranges. For instance,
6857
6858 1 x_9 = p_3->a;
6859 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
6860 3 if (p_4 == q_2)
6861 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
6862 5 endif
6863 6 if (q_2)
6864
6865 In the code above, pointer p_5 has range [q_2, q_2], but from the
6866 code we can also determine that p_5 cannot be NULL and, if q_2 had
6867 a non-varying range, p_5's range should also be compatible with it.
6868
6869 These equivalences are created by two expressions: ASSERT_EXPR and
6870 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
6871 result of another assertion, then we can use the fact that p_5 and
6872 p_4 are equivalent when evaluating p_5's range.
6873
6874 Together with value ranges, we also propagate these equivalences
6875 between names so that we can take advantage of information from
6876 multiple ranges when doing final replacement. Note that this
6877 equivalency relation is transitive but not symmetric.
6878
6879 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
6880 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
6881 in contexts where that assertion does not hold (e.g., in line 6).
6882
6883 TODO, the main difference between this pass and Patterson's is that
6884 we do not propagate edge probabilities. We only compute whether
6885 edges can be taken or not. That is, instead of having a spectrum
6886 of jump probabilities between 0 and 1, we only deal with 0, 1 and
6887 DON'T KNOW. In the future, it may be worthwhile to propagate
6888 probabilities to aid branch prediction. */
6889
6890 static unsigned int
6891 execute_vrp (bool warn_array_bounds_p)
6892 {
6893 int i;
6894 edge e;
6895 switch_update *su;
6896
6897 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
6898 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
6899 scev_initialize ();
6900
6901 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
6902 Inserting assertions may split edges which will invalidate
6903 EDGE_DFS_BACK. */
6904 insert_range_assertions ();
6905
6906 to_remove_edges.create (10);
6907 to_update_switch_stmts.create (5);
6908 threadedge_initialize_values ();
6909
6910 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
6911 mark_dfs_back_edges ();
6912
6913 class vrp_prop vrp_prop;
6914 vrp_prop.vrp_initialize ();
6915 vrp_prop.ssa_propagate ();
6916 vrp_prop.vrp_finalize (warn_array_bounds_p);
6917
6918 /* We must identify jump threading opportunities before we release
6919 the datastructures built by VRP. */
6920 identify_jump_threads (&vrp_prop.vr_values);
6921
6922 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
6923 was set by a type conversion can often be rewritten to use the
6924 RHS of the type conversion.
6925
6926 However, doing so inhibits jump threading through the comparison.
6927 So that transformation is not performed until after jump threading
6928 is complete. */
6929 basic_block bb;
6930 FOR_EACH_BB_FN (bb, cfun)
6931 {
6932 gimple *last = last_stmt (bb);
6933 if (last && gimple_code (last) == GIMPLE_COND)
6934 vrp_prop.vr_values.simplify_cond_using_ranges_2 (as_a <gcond *> (last));
6935 }
6936
6937 free_numbers_of_iterations_estimates (cfun);
6938
6939 /* ASSERT_EXPRs must be removed before finalizing jump threads
6940 as finalizing jump threads calls the CFG cleanup code which
6941 does not properly handle ASSERT_EXPRs. */
6942 remove_range_assertions ();
6943
6944 /* If we exposed any new variables, go ahead and put them into
6945 SSA form now, before we handle jump threading. This simplifies
6946 interactions between rewriting of _DECL nodes into SSA form
6947 and rewriting SSA_NAME nodes into SSA form after block
6948 duplication and CFG manipulation. */
6949 update_ssa (TODO_update_ssa);
6950
6951 /* We identified all the jump threading opportunities earlier, but could
6952 not transform the CFG at that time. This routine transforms the
6953 CFG and arranges for the dominator tree to be rebuilt if necessary.
6954
6955 Note the SSA graph update will occur during the normal TODO
6956 processing by the pass manager. */
6957 thread_through_all_blocks (false);
6958
6959 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
6960 CFG in a broken state and requires a cfg_cleanup run. */
6961 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
6962 remove_edge (e);
6963 /* Update SWITCH_EXPR case label vector. */
6964 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
6965 {
6966 size_t j;
6967 size_t n = TREE_VEC_LENGTH (su->vec);
6968 tree label;
6969 gimple_switch_set_num_labels (su->stmt, n);
6970 for (j = 0; j < n; j++)
6971 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
6972 /* As we may have replaced the default label with a regular one
6973 make sure to make it a real default label again. This ensures
6974 optimal expansion. */
6975 label = gimple_switch_label (su->stmt, 0);
6976 CASE_LOW (label) = NULL_TREE;
6977 CASE_HIGH (label) = NULL_TREE;
6978 }
6979
6980 if (to_remove_edges.length () > 0)
6981 {
6982 free_dominance_info (CDI_DOMINATORS);
6983 loops_state_set (LOOPS_NEED_FIXUP);
6984 }
6985
6986 to_remove_edges.release ();
6987 to_update_switch_stmts.release ();
6988 threadedge_finalize_values ();
6989
6990 scev_finalize ();
6991 loop_optimizer_finalize ();
6992 return 0;
6993 }
6994
6995 namespace {
6996
6997 const pass_data pass_data_vrp =
6998 {
6999 GIMPLE_PASS, /* type */
7000 "vrp", /* name */
7001 OPTGROUP_NONE, /* optinfo_flags */
7002 TV_TREE_VRP, /* tv_id */
7003 PROP_ssa, /* properties_required */
7004 0, /* properties_provided */
7005 0, /* properties_destroyed */
7006 0, /* todo_flags_start */
7007 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
7008 };
7009
7010 class pass_vrp : public gimple_opt_pass
7011 {
7012 public:
7013 pass_vrp (gcc::context *ctxt)
7014 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
7015 {}
7016
7017 /* opt_pass methods: */
7018 opt_pass * clone () { return new pass_vrp (m_ctxt); }
7019 void set_pass_param (unsigned int n, bool param)
7020 {
7021 gcc_assert (n == 0);
7022 warn_array_bounds_p = param;
7023 }
7024 virtual bool gate (function *) { return flag_tree_vrp != 0; }
7025 virtual unsigned int execute (function *)
7026 { return execute_vrp (warn_array_bounds_p); }
7027
7028 private:
7029 bool warn_array_bounds_p;
7030 }; // class pass_vrp
7031
7032 } // anon namespace
7033
7034 gimple_opt_pass *
7035 make_pass_vrp (gcc::context *ctxt)
7036 {
7037 return new pass_vrp (ctxt);
7038 }