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