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