OSDN Git Service

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