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