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