1 /* Code for range operators.
2    Copyright (C) 2017-2022 Free Software Foundation, Inc.
3    Contributed by Andrew MacLeod <amacleod@redhat.com>
4    and Aldy Hernandez <aldyh@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 "backend.h"
26 #include "insn-codes.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
36 #include "flags.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "cfganal.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "tree-cfg.h"
46 #include "wide-int.h"
47 #include "value-relation.h"
48 #include "range-op.h"
49 
50 // Return the upper limit for a type.
51 
52 static inline wide_int
max_limit(const_tree type)53 max_limit (const_tree type)
54 {
55   return wi::max_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
56 }
57 
58 // Return the lower limit for a type.
59 
60 static inline wide_int
min_limit(const_tree type)61 min_limit (const_tree type)
62 {
63   return wi::min_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
64 }
65 
66 // If the range of either op1 or op2 is undefined, set the result to
67 // varying and return TRUE.  If the caller truely cares about a result,
68 // they should pass in a varying if it has an undefined that it wants
69 // treated as a varying.
70 
71 inline bool
empty_range_varying(irange & r,tree type,const irange & op1,const irange & op2)72 empty_range_varying (irange &r, tree type,
73                          const irange &op1, const irange & op2)
74 {
75   if (op1.undefined_p () || op2.undefined_p ())
76     {
77       r.set_varying (type);
78       return true;
79     }
80   else
81     return false;
82 }
83 
84 // Return false if shifting by OP is undefined behavior.  Otherwise, return
85 // true and the range it is to be shifted by.  This allows trimming out of
86 // undefined ranges, leaving only valid ranges if there are any.
87 
88 static inline bool
get_shift_range(irange & r,tree type,const irange & op)89 get_shift_range (irange &r, tree type, const irange &op)
90 {
91   if (op.undefined_p ())
92     return false;
93 
94   // Build valid range and intersect it with the shift range.
95   r = value_range (build_int_cst_type (op.type (), 0),
96                        build_int_cst_type (op.type (), TYPE_PRECISION (type) - 1));
97   r.intersect (op);
98 
99   // If there are no valid ranges in the shift range, returned false.
100   if (r.undefined_p ())
101     return false;
102   return true;
103 }
104 
105 // Return TRUE if 0 is within [WMIN, WMAX].
106 
107 static inline bool
wi_includes_zero_p(tree type,const wide_int & wmin,const wide_int & wmax)108 wi_includes_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
109 {
110   signop sign = TYPE_SIGN (type);
111   return wi::le_p (wmin, 0, sign) && wi::ge_p (wmax, 0, sign);
112 }
113 
114 // Return TRUE if [WMIN, WMAX] is the singleton 0.
115 
116 static inline bool
wi_zero_p(tree type,const wide_int & wmin,const wide_int & wmax)117 wi_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
118 {
119   unsigned prec = TYPE_PRECISION (type);
120   return wmin == wmax && wi::eq_p (wmin, wi::zero (prec));
121 }
122 
123 // Default wide_int fold operation returns [MIN, MAX].
124 
125 void
wi_fold(irange & r,tree type,const wide_int & lh_lb ATTRIBUTE_UNUSED,const wide_int & lh_ub ATTRIBUTE_UNUSED,const wide_int & rh_lb ATTRIBUTE_UNUSED,const wide_int & rh_ub ATTRIBUTE_UNUSED) const126 range_operator::wi_fold (irange &r, tree type,
127                                const wide_int &lh_lb ATTRIBUTE_UNUSED,
128                                const wide_int &lh_ub ATTRIBUTE_UNUSED,
129                                const wide_int &rh_lb ATTRIBUTE_UNUSED,
130                                const wide_int &rh_ub ATTRIBUTE_UNUSED) const
131 {
132   gcc_checking_assert (irange::supports_type_p (type));
133   r.set_varying (type);
134 }
135 
136 // Call wi_fold, except further split small subranges into constants.
137 // This can provide better precision. For something   8 >> [0,1]
138 // Instead of [8, 16], we will produce [8,8][16,16]
139 
140 void
wi_fold_in_parts(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const141 range_operator::wi_fold_in_parts (irange &r, tree type,
142                                           const wide_int &lh_lb,
143                                           const wide_int &lh_ub,
144                                           const wide_int &rh_lb,
145                                           const wide_int &rh_ub) const
146 {
147   int_range_max tmp;
148   widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
149                                          widest_int::from (rh_lb, TYPE_SIGN (type)));
150   widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
151                                          widest_int::from (lh_lb, TYPE_SIGN (type)));
152   // If there are 2, 3, or 4 values in the RH range, do them separately.
153   // Call wi_fold_in_parts to check the RH side.
154   if (rh_range > 0 && rh_range < 4)
155     {
156       wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
157       if (rh_range > 1)
158           {
159             wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
160             r.union_ (tmp);
161             if (rh_range == 3)
162               {
163                 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
164                 r.union_ (tmp);
165               }
166           }
167       wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
168       r.union_ (tmp);
169     }
170   // Otherise check for 2, 3, or 4 values in the LH range and split them up.
171   // The RH side has been checked, so no recursion needed.
172   else if (lh_range > 0 && lh_range < 4)
173     {
174       wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
175       if (lh_range > 1)
176           {
177             wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
178             r.union_ (tmp);
179             if (lh_range == 3)
180               {
181                 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
182                 r.union_ (tmp);
183               }
184           }
185       wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
186       r.union_ (tmp);
187     }
188   // Otherwise just call wi_fold.
189   else
190     wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
191 }
192 
193 // The default for fold is to break all ranges into sub-ranges and
194 // invoke the wi_fold method on each sub-range pair.
195 
196 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh,relation_kind rel) const197 range_operator::fold_range (irange &r, tree type,
198                                   const irange &lh,
199                                   const irange &rh,
200                                   relation_kind rel) const
201 {
202   gcc_checking_assert (irange::supports_type_p (type));
203   if (empty_range_varying (r, type, lh, rh))
204     return true;
205 
206   unsigned num_lh = lh.num_pairs ();
207   unsigned num_rh = rh.num_pairs ();
208 
209   // If both ranges are single pairs, fold directly into the result range.
210   // If the number of subranges grows too high, produce a summary result as the
211   // loop becomes exponential with little benefit.  See PR 103821.
212   if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
213     {
214       wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
215                               rh.lower_bound (), rh.upper_bound ());
216       op1_op2_relation_effect (r, type, lh, rh, rel);
217       return true;
218     }
219 
220   int_range_max tmp;
221   r.set_undefined ();
222   for (unsigned x = 0; x < num_lh; ++x)
223     for (unsigned y = 0; y < num_rh; ++y)
224       {
225           wide_int lh_lb = lh.lower_bound (x);
226           wide_int lh_ub = lh.upper_bound (x);
227           wide_int rh_lb = rh.lower_bound (y);
228           wide_int rh_ub = rh.upper_bound (y);
229           wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
230           r.union_ (tmp);
231           if (r.varying_p ())
232             {
233               op1_op2_relation_effect (r, type, lh, rh, rel);
234               return true;
235             }
236       }
237   op1_op2_relation_effect (r, type, lh, rh, rel);
238   return true;
239 }
240 
241 // The default for op1_range is to return false.
242 
243 bool
op1_range(irange & r ATTRIBUTE_UNUSED,tree type ATTRIBUTE_UNUSED,const irange & lhs ATTRIBUTE_UNUSED,const irange & op2 ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const244 range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
245                                  tree type ATTRIBUTE_UNUSED,
246                                  const irange &lhs ATTRIBUTE_UNUSED,
247                                  const irange &op2 ATTRIBUTE_UNUSED,
248                                  relation_kind rel ATTRIBUTE_UNUSED) const
249 {
250   return false;
251 }
252 
253 // The default for op2_range is to return false.
254 
255 bool
op2_range(irange & r ATTRIBUTE_UNUSED,tree type ATTRIBUTE_UNUSED,const irange & lhs ATTRIBUTE_UNUSED,const irange & op1 ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const256 range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
257                                  tree type ATTRIBUTE_UNUSED,
258                                  const irange &lhs ATTRIBUTE_UNUSED,
259                                  const irange &op1 ATTRIBUTE_UNUSED,
260                                  relation_kind rel ATTRIBUTE_UNUSED) const
261 {
262   return false;
263 }
264 
265 // The default relation routines return VREL_NONE.
266 
267 enum tree_code
lhs_op1_relation(const irange & lhs ATTRIBUTE_UNUSED,const irange & op1 ATTRIBUTE_UNUSED,const irange & op2 ATTRIBUTE_UNUSED) const268 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
269                                           const irange &op1 ATTRIBUTE_UNUSED,
270                                           const irange &op2 ATTRIBUTE_UNUSED) const
271 {
272   return VREL_NONE;
273 }
274 
275 enum tree_code
lhs_op2_relation(const irange & lhs ATTRIBUTE_UNUSED,const irange & op1 ATTRIBUTE_UNUSED,const irange & op2 ATTRIBUTE_UNUSED) const276 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
277                                           const irange &op1 ATTRIBUTE_UNUSED,
278                                           const irange &op2 ATTRIBUTE_UNUSED) const
279 {
280   return VREL_NONE;
281 }
282 
283 enum tree_code
op1_op2_relation(const irange & lhs ATTRIBUTE_UNUSED) const284 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
285 {
286   return VREL_NONE;
287 }
288 
289 // Default is no relation affects the LHS.
290 
291 bool
op1_op2_relation_effect(irange & lhs_range ATTRIBUTE_UNUSED,tree type ATTRIBUTE_UNUSED,const irange & op1_range ATTRIBUTE_UNUSED,const irange & op2_range ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const292 range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
293                                                tree type ATTRIBUTE_UNUSED,
294                                                const irange &op1_range ATTRIBUTE_UNUSED,
295                                                const irange &op2_range ATTRIBUTE_UNUSED,
296                                                relation_kind rel ATTRIBUTE_UNUSED) const
297 {
298   return false;
299 }
300 
301 // Create and return a range from a pair of wide-ints that are known
302 // to have overflowed (or underflowed).
303 
304 static void
value_range_from_overflowed_bounds(irange & r,tree type,const wide_int & wmin,const wide_int & wmax)305 value_range_from_overflowed_bounds (irange &r, tree type,
306                                             const wide_int &wmin,
307                                             const wide_int &wmax)
308 {
309   const signop sgn = TYPE_SIGN (type);
310   const unsigned int prec = TYPE_PRECISION (type);
311 
312   wide_int tmin = wide_int::from (wmin, prec, sgn);
313   wide_int tmax = wide_int::from (wmax, prec, sgn);
314 
315   bool covers = false;
316   wide_int tem = tmin;
317   tmin = tmax + 1;
318   if (wi::cmp (tmin, tmax, sgn) < 0)
319     covers = true;
320   tmax = tem - 1;
321   if (wi::cmp (tmax, tem, sgn) > 0)
322     covers = true;
323 
324   // If the anti-range would cover nothing, drop to varying.
325   // Likewise if the anti-range bounds are outside of the types
326   // values.
327   if (covers || wi::cmp (tmin, tmax, sgn) > 0)
328     r.set_varying (type);
329   else
330     {
331       tree tree_min = wide_int_to_tree (type, tmin);
332       tree tree_max = wide_int_to_tree (type, tmax);
333       r.set (tree_min, tree_max, VR_ANTI_RANGE);
334     }
335 }
336 
337 // Create and return a range from a pair of wide-ints.  MIN_OVF and
338 // MAX_OVF describe any overflow that might have occurred while
339 // calculating WMIN and WMAX respectively.
340 
341 static void
value_range_with_overflow(irange & r,tree type,const wide_int & wmin,const wide_int & wmax,wi::overflow_type min_ovf=wi::OVF_NONE,wi::overflow_type max_ovf=wi::OVF_NONE)342 value_range_with_overflow (irange &r, tree type,
343                                  const wide_int &wmin, const wide_int &wmax,
344                                  wi::overflow_type min_ovf = wi::OVF_NONE,
345                                  wi::overflow_type max_ovf = wi::OVF_NONE)
346 {
347   const signop sgn = TYPE_SIGN (type);
348   const unsigned int prec = TYPE_PRECISION (type);
349   const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
350 
351   // For one bit precision if max != min, then the range covers all
352   // values.
353   if (prec == 1 && wi::ne_p (wmax, wmin))
354     {
355       r.set_varying (type);
356       return;
357     }
358 
359   if (overflow_wraps)
360     {
361       // If overflow wraps, truncate the values and adjust the range,
362       // kind, and bounds appropriately.
363       if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
364           {
365             wide_int tmin = wide_int::from (wmin, prec, sgn);
366             wide_int tmax = wide_int::from (wmax, prec, sgn);
367             // If the limits are swapped, we wrapped around and cover
368             // the entire range.
369             if (wi::gt_p (tmin, tmax, sgn))
370               r.set_varying (type);
371             else
372               // No overflow or both overflow or underflow.  The range
373               // kind stays normal.
374               r.set (wide_int_to_tree (type, tmin),
375                        wide_int_to_tree (type, tmax));
376             return;
377           }
378 
379       if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
380             || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
381           value_range_from_overflowed_bounds (r, type, wmin, wmax);
382       else
383           // Other underflow and/or overflow, drop to VR_VARYING.
384           r.set_varying (type);
385     }
386   else
387     {
388       // If both bounds either underflowed or overflowed, then the result
389       // is undefined.
390       if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
391             || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
392           {
393             r.set_undefined ();
394             return;
395           }
396 
397       // If overflow does not wrap, saturate to [MIN, MAX].
398       wide_int new_lb, new_ub;
399       if (min_ovf == wi::OVF_UNDERFLOW)
400           new_lb = wi::min_value (prec, sgn);
401       else if (min_ovf == wi::OVF_OVERFLOW)
402           new_lb = wi::max_value (prec, sgn);
403       else
404         new_lb = wmin;
405 
406       if (max_ovf == wi::OVF_UNDERFLOW)
407           new_ub = wi::min_value (prec, sgn);
408       else if (max_ovf == wi::OVF_OVERFLOW)
409           new_ub = wi::max_value (prec, sgn);
410       else
411         new_ub = wmax;
412 
413       r.set (wide_int_to_tree (type, new_lb),
414                wide_int_to_tree (type, new_ub));
415     }
416 }
417 
418 // Create and return a range from a pair of wide-ints.  Canonicalize
419 // the case where the bounds are swapped.  In which case, we transform
420 // [10,5] into [MIN,5][10,MAX].
421 
422 static inline void
create_possibly_reversed_range(irange & r,tree type,const wide_int & new_lb,const wide_int & new_ub)423 create_possibly_reversed_range (irange &r, tree type,
424                                         const wide_int &new_lb, const wide_int &new_ub)
425 {
426   signop s = TYPE_SIGN (type);
427   // If the bounds are swapped, treat the result as if an overflow occured.
428   if (wi::gt_p (new_lb, new_ub, s))
429     value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
430   else
431     // Otherwise it's just a normal range.
432     r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
433 }
434 
435 // Return an irange instance that is a boolean TRUE.
436 
437 static inline int_range<1>
range_true(tree type)438 range_true (tree type)
439 {
440   unsigned prec = TYPE_PRECISION (type);
441   return int_range<1> (type, wi::one (prec), wi::one (prec));
442 }
443 
444 // Return an irange instance that is a boolean FALSE.
445 
446 static inline int_range<1>
range_false(tree type)447 range_false (tree type)
448 {
449   unsigned prec = TYPE_PRECISION (type);
450   return int_range<1> (type, wi::zero (prec), wi::zero (prec));
451 }
452 
453 // Return an irange that covers both true and false.
454 
455 static inline int_range<1>
range_true_and_false(tree type)456 range_true_and_false (tree type)
457 {
458   unsigned prec = TYPE_PRECISION (type);
459   return int_range<1> (type, wi::zero (prec), wi::one (prec));
460 }
461 
462 enum bool_range_state { BRS_FALSE, BRS_TRUE, BRS_EMPTY, BRS_FULL };
463 
464 // Return the summary information about boolean range LHS.  If EMPTY/FULL,
465 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
466 
467 static bool_range_state
get_bool_state(irange & r,const irange & lhs,tree val_type)468 get_bool_state (irange &r, const irange &lhs, tree val_type)
469 {
470   // If there is no result, then this is unexecutable.
471   if (lhs.undefined_p ())
472     {
473       r.set_undefined ();
474       return BRS_EMPTY;
475     }
476 
477   if (lhs.zero_p ())
478     return BRS_FALSE;
479 
480   // For TRUE, we can't just test for [1,1] because Ada can have
481   // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
482   if (lhs.contains_p (build_zero_cst (lhs.type ())))
483     {
484       r.set_varying (val_type);
485       return BRS_FULL;
486     }
487 
488   return BRS_TRUE;
489 }
490 
491 // For relation opcodes, first try to see if the supplied relation
492 // forces a true or false result, and return that.
493 // Then check for undefined operands.  If none of this applies,
494 // return false.
495 
496 static inline bool
relop_early_resolve(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel,relation_kind my_rel)497 relop_early_resolve (irange &r, tree type, const irange &op1,
498                          const irange &op2, relation_kind rel,
499                          relation_kind my_rel)
500 {
501   // If known relation is a complete subset of this relation, always true.
502   if (relation_union (rel, my_rel) == my_rel)
503     {
504       r = range_true (type);
505       return true;
506     }
507 
508   // If known relation has no subset of this relation, always false.
509   if (relation_intersect (rel, my_rel) == VREL_EMPTY)
510     {
511       r = range_false (type);
512       return true;
513     }
514 
515   // If either operand is undefined, return VARYING.
516   if (empty_range_varying (r, type, op1, op2))
517     return true;
518 
519   return false;
520 }
521 
522 
523 class operator_equal : public range_operator
524 {
525 public:
526   virtual bool fold_range (irange &r, tree type,
527                                  const irange &op1,
528                                  const irange &op2,
529                                  relation_kind rel = VREL_NONE) const;
530   virtual bool op1_range (irange &r, tree type,
531                                 const irange &lhs,
532                                 const irange &val,
533                                 relation_kind rel = VREL_NONE) const;
534   virtual bool op2_range (irange &r, tree type,
535                                 const irange &lhs,
536                                 const irange &val,
537                                 relation_kind rel = VREL_NONE) const;
538   virtual enum tree_code op1_op2_relation (const irange &lhs) const;
539 } op_equal;
540 
541 // Check if the LHS range indicates a relation between OP1 and OP2.
542 
543 enum tree_code
op1_op2_relation(const irange & lhs) const544 operator_equal::op1_op2_relation (const irange &lhs) const
545 {
546   if (lhs.undefined_p ())
547     return VREL_EMPTY;
548 
549   // FALSE = op1 == op2 indicates NE_EXPR.
550   if (lhs.zero_p ())
551     return NE_EXPR;
552 
553   // TRUE = op1 == op2 indicates EQ_EXPR.
554   if (!lhs.contains_p (build_zero_cst (lhs.type ())))
555     return EQ_EXPR;
556   return VREL_NONE;
557 }
558 
559 
560 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const561 operator_equal::fold_range (irange &r, tree type,
562                                   const irange &op1,
563                                   const irange &op2,
564                                   relation_kind rel) const
565 {
566   if (relop_early_resolve (r, type, op1, op2, rel, EQ_EXPR))
567     return true;
568 
569   // We can be sure the values are always equal or not if both ranges
570   // consist of a single value, and then compare them.
571   if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
572       && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
573     {
574       if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
575           r = range_true (type);
576       else
577           r = range_false (type);
578     }
579   else
580     {
581       // If ranges do not intersect, we know the range is not equal,
582       // otherwise we don't know anything for sure.
583       int_range_max tmp = op1;
584       tmp.intersect (op2);
585       if (tmp.undefined_p ())
586           r = range_false (type);
587       else
588           r = range_true_and_false (type);
589     }
590   return true;
591 }
592 
593 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const594 operator_equal::op1_range (irange &r, tree type,
595                                  const irange &lhs,
596                                  const irange &op2,
597                                  relation_kind rel ATTRIBUTE_UNUSED) const
598 {
599   switch (get_bool_state (r, lhs, type))
600     {
601     case BRS_FALSE:
602       // If the result is false, the only time we know anything is
603       // if OP2 is a constant.
604       if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
605           {
606             r = op2;
607             r.invert ();
608           }
609       else
610           r.set_varying (type);
611       break;
612 
613     case BRS_TRUE:
614       // If it's true, the result is the same as OP2.
615       r = op2;
616       break;
617 
618     default:
619       break;
620     }
621   return true;
622 }
623 
624 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel) const625 operator_equal::op2_range (irange &r, tree type,
626                                  const irange &lhs,
627                                  const irange &op1,
628                                  relation_kind rel) const
629 {
630   return operator_equal::op1_range (r, type, lhs, op1, rel);
631 }
632 
633 class operator_not_equal : public range_operator
634 {
635 public:
636   virtual bool fold_range (irange &r, tree type,
637                                  const irange &op1,
638                                  const irange &op2,
639                                  relation_kind rel = VREL_NONE) const;
640   virtual bool op1_range (irange &r, tree type,
641                                 const irange &lhs,
642                                 const irange &op2,
643                                 relation_kind rel = VREL_NONE) const;
644   virtual bool op2_range (irange &r, tree type,
645                                 const irange &lhs,
646                                 const irange &op1,
647                                 relation_kind rel = VREL_NONE) const;
648   virtual enum tree_code op1_op2_relation (const irange &lhs) const;
649 } op_not_equal;
650 
651 // Check if the LHS range indicates a relation between OP1 and OP2.
652 
653 enum tree_code
op1_op2_relation(const irange & lhs) const654 operator_not_equal::op1_op2_relation (const irange &lhs) const
655 {
656   if (lhs.undefined_p ())
657     return VREL_EMPTY;
658 
659   // FALSE = op1 != op2  indicates EQ_EXPR.
660   if (lhs.zero_p ())
661     return EQ_EXPR;
662 
663   // TRUE = op1 != op2  indicates NE_EXPR.
664   if (!lhs.contains_p (build_zero_cst (lhs.type ())))
665     return NE_EXPR;
666   return VREL_NONE;
667 }
668 
669 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const670 operator_not_equal::fold_range (irange &r, tree type,
671                                         const irange &op1,
672                                         const irange &op2,
673                                         relation_kind rel) const
674 {
675   if (relop_early_resolve (r, type, op1, op2, rel, NE_EXPR))
676     return true;
677 
678   // We can be sure the values are always equal or not if both ranges
679   // consist of a single value, and then compare them.
680   if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
681       && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
682     {
683       if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
684           r = range_true (type);
685       else
686           r = range_false (type);
687     }
688   else
689     {
690       // If ranges do not intersect, we know the range is not equal,
691       // otherwise we don't know anything for sure.
692       int_range_max tmp = op1;
693       tmp.intersect (op2);
694       if (tmp.undefined_p ())
695           r = range_true (type);
696       else
697           r = range_true_and_false (type);
698     }
699   return true;
700 }
701 
702 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const703 operator_not_equal::op1_range (irange &r, tree type,
704                                      const irange &lhs,
705                                      const irange &op2,
706                                      relation_kind rel ATTRIBUTE_UNUSED) const
707 {
708   switch (get_bool_state (r, lhs, type))
709     {
710     case BRS_TRUE:
711       // If the result is true, the only time we know anything is if
712       // OP2 is a constant.
713       if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
714           {
715             r = op2;
716             r.invert ();
717           }
718       else
719           r.set_varying (type);
720       break;
721 
722     case BRS_FALSE:
723       // If it's false, the result is the same as OP2.
724       r = op2;
725       break;
726 
727     default:
728       break;
729     }
730   return true;
731 }
732 
733 
734 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel) const735 operator_not_equal::op2_range (irange &r, tree type,
736                                      const irange &lhs,
737                                      const irange &op1,
738                                      relation_kind rel) const
739 {
740   return operator_not_equal::op1_range (r, type, lhs, op1, rel);
741 }
742 
743 // (X < VAL) produces the range of [MIN, VAL - 1].
744 
745 static void
build_lt(irange & r,tree type,const wide_int & val)746 build_lt (irange &r, tree type, const wide_int &val)
747 {
748   wi::overflow_type ov;
749   wide_int lim;
750   signop sgn = TYPE_SIGN (type);
751 
752   // Signed 1 bit cannot represent 1 for subtraction.
753   if (sgn == SIGNED)
754     lim = wi::add (val, -1, sgn, &ov);
755   else
756     lim = wi::sub (val, 1, sgn, &ov);
757 
758   // If val - 1 underflows, check if X < MIN, which is an empty range.
759   if (ov)
760     r.set_undefined ();
761   else
762     r = int_range<1> (type, min_limit (type), lim);
763 }
764 
765 // (X <= VAL) produces the range of [MIN, VAL].
766 
767 static void
build_le(irange & r,tree type,const wide_int & val)768 build_le (irange &r, tree type, const wide_int &val)
769 {
770   r = int_range<1> (type, min_limit (type), val);
771 }
772 
773 // (X > VAL) produces the range of [VAL + 1, MAX].
774 
775 static void
build_gt(irange & r,tree type,const wide_int & val)776 build_gt (irange &r, tree type, const wide_int &val)
777 {
778   wi::overflow_type ov;
779   wide_int lim;
780   signop sgn = TYPE_SIGN (type);
781 
782   // Signed 1 bit cannot represent 1 for addition.
783   if (sgn == SIGNED)
784     lim = wi::sub (val, -1, sgn, &ov);
785   else
786     lim = wi::add (val, 1, sgn, &ov);
787   // If val + 1 overflows, check is for X > MAX, which is an empty range.
788   if (ov)
789     r.set_undefined ();
790   else
791     r = int_range<1> (type, lim, max_limit (type));
792 }
793 
794 // (X >= val) produces the range of [VAL, MAX].
795 
796 static void
build_ge(irange & r,tree type,const wide_int & val)797 build_ge (irange &r, tree type, const wide_int &val)
798 {
799   r = int_range<1> (type, val, max_limit (type));
800 }
801 
802 
803 class operator_lt :  public range_operator
804 {
805 public:
806   virtual bool fold_range (irange &r, tree type,
807                                  const irange &op1,
808                                  const irange &op2,
809                                  relation_kind rel = VREL_NONE) const;
810   virtual bool op1_range (irange &r, tree type,
811                                 const irange &lhs,
812                                 const irange &op2,
813                                 relation_kind rel = VREL_NONE) const;
814   virtual bool op2_range (irange &r, tree type,
815                                 const irange &lhs,
816                                 const irange &op1,
817                                 relation_kind rel = VREL_NONE) const;
818   virtual enum tree_code op1_op2_relation (const irange &lhs) const;
819 } op_lt;
820 
821 // Check if the LHS range indicates a relation between OP1 and OP2.
822 
823 enum tree_code
op1_op2_relation(const irange & lhs) const824 operator_lt::op1_op2_relation (const irange &lhs) const
825 {
826   if (lhs.undefined_p ())
827     return VREL_EMPTY;
828 
829   // FALSE = op1 < op2 indicates GE_EXPR.
830   if (lhs.zero_p ())
831     return GE_EXPR;
832 
833   // TRUE = op1 < op2 indicates LT_EXPR.
834   if (!lhs.contains_p (build_zero_cst (lhs.type ())))
835     return LT_EXPR;
836   return VREL_NONE;
837 }
838 
839 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const840 operator_lt::fold_range (irange &r, tree type,
841                                const irange &op1,
842                                const irange &op2,
843                                relation_kind rel) const
844 {
845   if (relop_early_resolve (r, type, op1, op2, rel, LT_EXPR))
846     return true;
847 
848   signop sign = TYPE_SIGN (op1.type ());
849   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
850 
851   if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
852     r = range_true (type);
853   else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
854     r = range_false (type);
855   else
856     r = range_true_and_false (type);
857   return true;
858 }
859 
860 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const861 operator_lt::op1_range (irange &r, tree type,
862                               const irange &lhs,
863                               const irange &op2,
864                               relation_kind rel ATTRIBUTE_UNUSED) const
865 {
866   switch (get_bool_state (r, lhs, type))
867     {
868     case BRS_TRUE:
869       build_lt (r, type, op2.upper_bound ());
870       break;
871 
872     case BRS_FALSE:
873       build_ge (r, type, op2.lower_bound ());
874       break;
875 
876     default:
877       break;
878     }
879   return true;
880 }
881 
882 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const883 operator_lt::op2_range (irange &r, tree type,
884                               const irange &lhs,
885                               const irange &op1,
886                               relation_kind rel ATTRIBUTE_UNUSED) const
887 {
888   switch (get_bool_state (r, lhs, type))
889     {
890     case BRS_FALSE:
891       build_le (r, type, op1.upper_bound ());
892       break;
893 
894     case BRS_TRUE:
895       build_gt (r, type, op1.lower_bound ());
896       break;
897 
898     default:
899       break;
900     }
901   return true;
902 }
903 
904 
905 class operator_le :  public range_operator
906 {
907 public:
908   virtual bool fold_range (irange &r, tree type,
909                                  const irange &op1,
910                                  const irange &op2,
911                                  relation_kind rel = VREL_NONE) const;
912   virtual bool op1_range (irange &r, tree type,
913                                 const irange &lhs,
914                                 const irange &op2,
915                                 relation_kind rel = VREL_NONE) const;
916   virtual bool op2_range (irange &r, tree type,
917                                 const irange &lhs,
918                                 const irange &op1,
919                                 relation_kind rel = VREL_NONE) const;
920   virtual enum tree_code op1_op2_relation (const irange &lhs) const;
921 } op_le;
922 
923 // Check if the LHS range indicates a relation between OP1 and OP2.
924 
925 enum tree_code
op1_op2_relation(const irange & lhs) const926 operator_le::op1_op2_relation (const irange &lhs) const
927 {
928   if (lhs.undefined_p ())
929     return VREL_EMPTY;
930 
931   // FALSE = op1 <= op2 indicates GT_EXPR.
932   if (lhs.zero_p ())
933     return GT_EXPR;
934 
935   // TRUE = op1 <= op2 indicates LE_EXPR.
936   if (!lhs.contains_p (build_zero_cst (lhs.type ())))
937     return LE_EXPR;
938   return VREL_NONE;
939 }
940 
941 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const942 operator_le::fold_range (irange &r, tree type,
943                                const irange &op1,
944                                const irange &op2,
945                                relation_kind rel) const
946 {
947   if (relop_early_resolve (r, type, op1, op2, rel, LE_EXPR))
948     return true;
949 
950   signop sign = TYPE_SIGN (op1.type ());
951   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
952 
953   if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
954     r = range_true (type);
955   else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
956     r = range_false (type);
957   else
958     r = range_true_and_false (type);
959   return true;
960 }
961 
962 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const963 operator_le::op1_range (irange &r, tree type,
964                               const irange &lhs,
965                               const irange &op2,
966                               relation_kind rel ATTRIBUTE_UNUSED) const
967 {
968   switch (get_bool_state (r, lhs, type))
969     {
970     case BRS_TRUE:
971       build_le (r, type, op2.upper_bound ());
972       break;
973 
974     case BRS_FALSE:
975       build_gt (r, type, op2.lower_bound ());
976       break;
977 
978     default:
979       break;
980     }
981   return true;
982 }
983 
984 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const985 operator_le::op2_range (irange &r, tree type,
986                               const irange &lhs,
987                               const irange &op1,
988                               relation_kind rel ATTRIBUTE_UNUSED) const
989 {
990   switch (get_bool_state (r, lhs, type))
991     {
992     case BRS_FALSE:
993       build_lt (r, type, op1.upper_bound ());
994       break;
995 
996     case BRS_TRUE:
997       build_ge (r, type, op1.lower_bound ());
998       break;
999 
1000     default:
1001       break;
1002     }
1003   return true;
1004 }
1005 
1006 
1007 class operator_gt :  public range_operator
1008 {
1009 public:
1010   virtual bool fold_range (irange &r, tree type,
1011                                  const irange &op1,
1012                                  const irange &op2,
1013                                  relation_kind rel = VREL_NONE) const;
1014   virtual bool op1_range (irange &r, tree type,
1015                                 const irange &lhs,
1016                                 const irange &op2,
1017                                 relation_kind rel = VREL_NONE) const;
1018   virtual bool op2_range (irange &r, tree type,
1019                                 const irange &lhs,
1020                                 const irange &op1,
1021                                 relation_kind rel = VREL_NONE) const;
1022   virtual enum tree_code op1_op2_relation (const irange &lhs) const;
1023 } op_gt;
1024 
1025 // Check if the LHS range indicates a relation between OP1 and OP2.
1026 
1027 enum tree_code
op1_op2_relation(const irange & lhs) const1028 operator_gt::op1_op2_relation (const irange &lhs) const
1029 {
1030   if (lhs.undefined_p ())
1031     return VREL_EMPTY;
1032 
1033   // FALSE = op1 > op2 indicates LE_EXPR.
1034   if (lhs.zero_p ())
1035     return LE_EXPR;
1036 
1037   // TRUE = op1 > op2 indicates GT_EXPR.
1038   if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1039     return GT_EXPR;
1040   return VREL_NONE;
1041 }
1042 
1043 
1044 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const1045 operator_gt::fold_range (irange &r, tree type,
1046                                const irange &op1, const irange &op2,
1047                                relation_kind rel) const
1048 {
1049   if (relop_early_resolve (r, type, op1, op2, rel, GT_EXPR))
1050     return true;
1051 
1052   signop sign = TYPE_SIGN (op1.type ());
1053   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1054 
1055   if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1056     r = range_true (type);
1057   else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1058     r = range_false (type);
1059   else
1060     r = range_true_and_false (type);
1061   return true;
1062 }
1063 
1064 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const1065 operator_gt::op1_range (irange &r, tree type,
1066                               const irange &lhs, const irange &op2,
1067                               relation_kind rel ATTRIBUTE_UNUSED) const
1068 {
1069   switch (get_bool_state (r, lhs, type))
1070     {
1071     case BRS_TRUE:
1072       build_gt (r, type, op2.lower_bound ());
1073       break;
1074 
1075     case BRS_FALSE:
1076       build_le (r, type, op2.upper_bound ());
1077       break;
1078 
1079     default:
1080       break;
1081     }
1082   return true;
1083 }
1084 
1085 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const1086 operator_gt::op2_range (irange &r, tree type,
1087                               const irange &lhs,
1088                               const irange &op1,
1089                               relation_kind rel ATTRIBUTE_UNUSED) const
1090 {
1091   switch (get_bool_state (r, lhs, type))
1092     {
1093     case BRS_FALSE:
1094       build_ge (r, type, op1.lower_bound ());
1095       break;
1096 
1097     case BRS_TRUE:
1098       build_lt (r, type, op1.upper_bound ());
1099       break;
1100 
1101     default:
1102       break;
1103     }
1104   return true;
1105 }
1106 
1107 
1108 class operator_ge :  public range_operator
1109 {
1110 public:
1111   virtual bool fold_range (irange &r, tree type,
1112                                  const irange &op1,
1113                                  const irange &op2,
1114                                  relation_kind rel = VREL_NONE) const;
1115   virtual bool op1_range (irange &r, tree type,
1116                                 const irange &lhs,
1117                                 const irange &op2,
1118                                 relation_kind rel = VREL_NONE) const;
1119   virtual bool op2_range (irange &r, tree type,
1120                                 const irange &lhs,
1121                                 const irange &op1,
1122                                 relation_kind rel = VREL_NONE) const;
1123   virtual enum tree_code op1_op2_relation (const irange &lhs) const;
1124 } op_ge;
1125 
1126 // Check if the LHS range indicates a relation between OP1 and OP2.
1127 
1128 enum tree_code
op1_op2_relation(const irange & lhs) const1129 operator_ge::op1_op2_relation (const irange &lhs) const
1130 {
1131   if (lhs.undefined_p ())
1132     return VREL_EMPTY;
1133 
1134   // FALSE = op1 >= op2 indicates LT_EXPR.
1135   if (lhs.zero_p ())
1136     return LT_EXPR;
1137 
1138   // TRUE = op1 >= op2 indicates GE_EXPR.
1139   if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1140     return GE_EXPR;
1141   return VREL_NONE;
1142 }
1143 
1144 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const1145 operator_ge::fold_range (irange &r, tree type,
1146                                const irange &op1,
1147                                const irange &op2,
1148                                relation_kind rel) const
1149 {
1150   if (relop_early_resolve (r, type, op1, op2, rel, GE_EXPR))
1151     return true;
1152 
1153   signop sign = TYPE_SIGN (op1.type ());
1154   gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1155 
1156   if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1157     r = range_true (type);
1158   else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1159     r = range_false (type);
1160   else
1161     r = range_true_and_false (type);
1162   return true;
1163 }
1164 
1165 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const1166 operator_ge::op1_range (irange &r, tree type,
1167                               const irange &lhs,
1168                               const irange &op2,
1169                               relation_kind rel ATTRIBUTE_UNUSED) const
1170 {
1171   switch (get_bool_state (r, lhs, type))
1172     {
1173     case BRS_TRUE:
1174       build_ge (r, type, op2.lower_bound ());
1175       break;
1176 
1177     case BRS_FALSE:
1178       build_lt (r, type, op2.upper_bound ());
1179       break;
1180 
1181     default:
1182       break;
1183     }
1184   return true;
1185 }
1186 
1187 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const1188 operator_ge::op2_range (irange &r, tree type,
1189                               const irange &lhs,
1190                               const irange &op1,
1191                               relation_kind rel ATTRIBUTE_UNUSED) const
1192 {
1193   switch (get_bool_state (r, lhs, type))
1194     {
1195     case BRS_FALSE:
1196       build_gt (r, type, op1.lower_bound ());
1197       break;
1198 
1199     case BRS_TRUE:
1200       build_le (r, type, op1.upper_bound ());
1201       break;
1202 
1203     default:
1204       break;
1205     }
1206   return true;
1207 }
1208 
1209 
1210 class operator_plus : public range_operator
1211 {
1212 public:
1213   virtual bool op1_range (irange &r, tree type,
1214                                 const irange &lhs,
1215                                 const irange &op2,
1216                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1217   virtual bool op2_range (irange &r, tree type,
1218                                 const irange &lhs,
1219                                 const irange &op1,
1220                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1221   virtual void wi_fold (irange &r, tree type,
1222                             const wide_int &lh_lb,
1223                             const wide_int &lh_ub,
1224                             const wide_int &rh_lb,
1225                             const wide_int &rh_ub) const;
1226   virtual enum tree_code lhs_op1_relation (const irange &lhs, const irange &op1,
1227                                                      const irange &op2) const;
1228   virtual enum tree_code lhs_op2_relation (const irange &lhs, const irange &op1,
1229                                                      const irange &op2) const;
1230 } op_plus;
1231 
1232 // Check to see if the range of OP2 indicates anything about the relation
1233 // between LHS and OP1.
1234 
1235 enum tree_code
lhs_op1_relation(const irange & lhs,const irange & op1,const irange & op2) const1236 operator_plus::lhs_op1_relation (const irange &lhs,
1237                                          const irange &op1,
1238                                          const irange &op2) const
1239 {
1240   if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1241     return VREL_NONE;
1242 
1243   tree type = lhs.type ();
1244   unsigned prec = TYPE_PRECISION (type);
1245   wi::overflow_type ovf1, ovf2;
1246   signop sign = TYPE_SIGN (type);
1247 
1248   // LHS = OP1 + 0  indicates LHS == OP1.
1249   if (op2.zero_p ())
1250     return EQ_EXPR;
1251 
1252   if (TYPE_OVERFLOW_WRAPS (type))
1253     {
1254       wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1255       wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1256     }
1257   else
1258     ovf1 = ovf2 = wi::OVF_NONE;
1259 
1260   // Never wrapping additions.
1261   if (!ovf1 && !ovf2)
1262     {
1263       // Positive op2 means lhs > op1.
1264       if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1265           return GT_EXPR;
1266       if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1267           return GE_EXPR;
1268 
1269       // Negative op2 means lhs < op1.
1270       if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1271           return LT_EXPR;
1272       if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1273           return LE_EXPR;
1274     }
1275   // Always wrapping additions.
1276   else if (ovf1 && ovf1 == ovf2)
1277     {
1278       // Positive op2 means lhs < op1.
1279       if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1280           return LT_EXPR;
1281       if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1282           return LE_EXPR;
1283 
1284       // Negative op2 means lhs > op1.
1285       if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1286           return GT_EXPR;
1287       if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1288           return GE_EXPR;
1289     }
1290 
1291   // If op2 does not contain 0, then LHS and OP1 can never be equal.
1292   if (!range_includes_zero_p (&op2))
1293     return NE_EXPR;
1294 
1295   return VREL_NONE;
1296 }
1297 
1298 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1299 // operands.
1300 
1301 enum tree_code
lhs_op2_relation(const irange & lhs,const irange & op1,const irange & op2) const1302 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1303                                          const irange &op2) const
1304 {
1305   return lhs_op1_relation (lhs, op2, op1);
1306 }
1307 
1308 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1309 operator_plus::wi_fold (irange &r, tree type,
1310                               const wide_int &lh_lb, const wide_int &lh_ub,
1311                               const wide_int &rh_lb, const wide_int &rh_ub) const
1312 {
1313   wi::overflow_type ov_lb, ov_ub;
1314   signop s = TYPE_SIGN (type);
1315   wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1316   wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1317   value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1318 }
1319 
1320 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const1321 operator_plus::op1_range (irange &r, tree type,
1322                                 const irange &lhs,
1323                                 const irange &op2,
1324                                 relation_kind rel ATTRIBUTE_UNUSED) const
1325 {
1326   return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op2);
1327 }
1328 
1329 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const1330 operator_plus::op2_range (irange &r, tree type,
1331                                 const irange &lhs,
1332                                 const irange &op1,
1333                                 relation_kind rel ATTRIBUTE_UNUSED) const
1334 {
1335   return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op1);
1336 }
1337 
1338 
1339 class operator_minus : public range_operator
1340 {
1341 public:
1342   virtual bool op1_range (irange &r, tree type,
1343                                 const irange &lhs,
1344                                 const irange &op2,
1345                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1346   virtual bool op2_range (irange &r, tree type,
1347                                 const irange &lhs,
1348                                 const irange &op1,
1349                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1350   virtual void wi_fold (irange &r, tree type,
1351                             const wide_int &lh_lb,
1352                             const wide_int &lh_ub,
1353                             const wide_int &rh_lb,
1354                             const wide_int &rh_ub) const;
1355   virtual bool op1_op2_relation_effect (irange &lhs_range,
1356                                                   tree type,
1357                                                   const irange &op1_range,
1358                                                   const irange &op2_range,
1359                                                   relation_kind rel) const;
1360 } op_minus;
1361 
1362 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1363 operator_minus::wi_fold (irange &r, tree type,
1364                                const wide_int &lh_lb, const wide_int &lh_ub,
1365                                const wide_int &rh_lb, const wide_int &rh_ub) const
1366 {
1367   wi::overflow_type ov_lb, ov_ub;
1368   signop s = TYPE_SIGN (type);
1369   wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1370   wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1371   value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1372 }
1373 
1374 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1375 // LHS of the expression.  If so, apply it to LHS_RANGE.  This is a helper
1376 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1377 
1378 static bool
minus_op1_op2_relation_effect(irange & lhs_range,tree type,const irange & op1_range ATTRIBUTE_UNUSED,const irange & op2_range ATTRIBUTE_UNUSED,relation_kind rel)1379 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1380                                      const irange &op1_range ATTRIBUTE_UNUSED,
1381                                      const irange &op2_range ATTRIBUTE_UNUSED,
1382                                      relation_kind rel)
1383 {
1384   if (rel == VREL_NONE)
1385     return false;
1386 
1387   int_range<2> rel_range;
1388   unsigned prec = TYPE_PRECISION (type);
1389   signop sgn = TYPE_SIGN (type);
1390 
1391   // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1392   if (rel == EQ_EXPR)
1393     rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1394   else if (rel == NE_EXPR)
1395     rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1396                                     VR_ANTI_RANGE);
1397   else if (TYPE_OVERFLOW_WRAPS (type))
1398     {
1399       switch (rel)
1400           {
1401             // For wrapping signed values and unsigned, if op1 > op2 or
1402             // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1403             case GT_EXPR:
1404             case LT_EXPR:
1405                 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1406                                                   VR_ANTI_RANGE);
1407               break;
1408             default:
1409               return false;
1410           }
1411     }
1412   else
1413     {
1414       switch (rel)
1415           {
1416             // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1417             case GT_EXPR:
1418               rel_range = int_range<2> (type, wi::one (prec),
1419                                               wi::max_value (prec, sgn));
1420               break;
1421             // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1422             case GE_EXPR:
1423               rel_range = int_range<2> (type, wi::zero (prec),
1424                                               wi::max_value (prec, sgn));
1425               break;
1426             // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1427             case LT_EXPR:
1428               rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1429                                               wi::minus_one (prec));
1430               break;
1431             // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1432             case LE_EXPR:
1433               rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1434                                               wi::zero (prec));
1435               break;
1436             default:
1437               return false;
1438           }
1439     }
1440   lhs_range.intersect (rel_range);
1441   return true;
1442 }
1443 
1444 bool
op1_op2_relation_effect(irange & lhs_range,tree type,const irange & op1_range,const irange & op2_range,relation_kind rel) const1445 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1446                                                    const irange &op1_range,
1447                                                    const irange &op2_range,
1448                                                    relation_kind rel) const
1449 {
1450   return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1451                                                   rel);
1452 }
1453 
1454 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const1455 operator_minus::op1_range (irange &r, tree type,
1456                                  const irange &lhs,
1457                                  const irange &op2,
1458                                  relation_kind rel ATTRIBUTE_UNUSED) const
1459 {
1460   return range_op_handler (PLUS_EXPR, type)->fold_range (r, type, lhs, op2);
1461 }
1462 
1463 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const1464 operator_minus::op2_range (irange &r, tree type,
1465                                  const irange &lhs,
1466                                  const irange &op1,
1467                                  relation_kind rel ATTRIBUTE_UNUSED) const
1468 {
1469   return fold_range (r, type, op1, lhs);
1470 }
1471 
1472 
1473 class operator_pointer_diff : public range_operator
1474 {
1475   virtual bool op1_op2_relation_effect (irange &lhs_range,
1476                                                   tree type,
1477                                                   const irange &op1_range,
1478                                                   const irange &op2_range,
1479                                                   relation_kind rel) const;
1480 } op_pointer_diff;
1481 
1482 bool
op1_op2_relation_effect(irange & lhs_range,tree type,const irange & op1_range,const irange & op2_range,relation_kind rel) const1483 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1484                                                             const irange &op1_range,
1485                                                             const irange &op2_range,
1486                                                             relation_kind rel) const
1487 {
1488   return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1489                                                   rel);
1490 }
1491 
1492 
1493 class operator_min : public range_operator
1494 {
1495 public:
1496   virtual void wi_fold (irange &r, tree type,
1497                             const wide_int &lh_lb,
1498                             const wide_int &lh_ub,
1499                             const wide_int &rh_lb,
1500                             const wide_int &rh_ub) const;
1501 } op_min;
1502 
1503 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1504 operator_min::wi_fold (irange &r, tree type,
1505                            const wide_int &lh_lb, const wide_int &lh_ub,
1506                            const wide_int &rh_lb, const wide_int &rh_ub) const
1507 {
1508   signop s = TYPE_SIGN (type);
1509   wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1510   wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1511   value_range_with_overflow (r, type, new_lb, new_ub);
1512 }
1513 
1514 
1515 class operator_max : public range_operator
1516 {
1517 public:
1518   virtual void wi_fold (irange &r, tree type,
1519                             const wide_int &lh_lb,
1520                             const wide_int &lh_ub,
1521                             const wide_int &rh_lb,
1522                             const wide_int &rh_ub) const;
1523 } op_max;
1524 
1525 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1526 operator_max::wi_fold (irange &r, tree type,
1527                            const wide_int &lh_lb, const wide_int &lh_ub,
1528                            const wide_int &rh_lb, const wide_int &rh_ub) const
1529 {
1530   signop s = TYPE_SIGN (type);
1531   wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1532   wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1533   value_range_with_overflow (r, type, new_lb, new_ub);
1534 }
1535 
1536 
1537 class cross_product_operator : public range_operator
1538 {
1539 public:
1540   // Perform an operation between two wide-ints and place the result
1541   // in R.  Return true if the operation overflowed.
1542   virtual bool wi_op_overflows (wide_int &r,
1543                                         tree type,
1544                                         const wide_int &,
1545                                         const wide_int &) const = 0;
1546 
1547   // Calculate the cross product of two sets of sub-ranges and return it.
1548   void wi_cross_product (irange &r, tree type,
1549                                const wide_int &lh_lb,
1550                                const wide_int &lh_ub,
1551                                const wide_int &rh_lb,
1552                                const wide_int &rh_ub) const;
1553 };
1554 
1555 // Calculate the cross product of two sets of ranges and return it.
1556 //
1557 // Multiplications, divisions and shifts are a bit tricky to handle,
1558 // depending on the mix of signs we have in the two ranges, we need to
1559 // operate on different values to get the minimum and maximum values
1560 // for the new range.  One approach is to figure out all the
1561 // variations of range combinations and do the operations.
1562 //
1563 // However, this involves several calls to compare_values and it is
1564 // pretty convoluted.  It's simpler to do the 4 operations (MIN0 OP
1565 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1566 // figure the smallest and largest values to form the new range.
1567 
1568 void
wi_cross_product(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1569 cross_product_operator::wi_cross_product (irange &r, tree type,
1570                                                     const wide_int &lh_lb,
1571                                                     const wide_int &lh_ub,
1572                                                     const wide_int &rh_lb,
1573                                                     const wide_int &rh_ub) const
1574 {
1575   wide_int cp1, cp2, cp3, cp4;
1576   // Default to varying.
1577   r.set_varying (type);
1578 
1579   // Compute the 4 cross operations, bailing if we get an overflow we
1580   // can't handle.
1581   if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1582     return;
1583   if (wi::eq_p (lh_lb, lh_ub))
1584     cp3 = cp1;
1585   else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1586     return;
1587   if (wi::eq_p (rh_lb, rh_ub))
1588     cp2 = cp1;
1589   else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1590     return;
1591   if (wi::eq_p (lh_lb, lh_ub))
1592     cp4 = cp2;
1593   else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1594     return;
1595 
1596   // Order pairs.
1597   signop sign = TYPE_SIGN (type);
1598   if (wi::gt_p (cp1, cp2, sign))
1599     std::swap (cp1, cp2);
1600   if (wi::gt_p (cp3, cp4, sign))
1601     std::swap (cp3, cp4);
1602 
1603   // Choose min and max from the ordered pairs.
1604   wide_int res_lb = wi::min (cp1, cp3, sign);
1605   wide_int res_ub = wi::max (cp2, cp4, sign);
1606   value_range_with_overflow (r, type, res_lb, res_ub);
1607 }
1608 
1609 
1610 class operator_mult : public cross_product_operator
1611 {
1612 public:
1613   virtual void wi_fold (irange &r, tree type,
1614                             const wide_int &lh_lb,
1615                             const wide_int &lh_ub,
1616                             const wide_int &rh_lb,
1617                             const wide_int &rh_ub) const;
1618   virtual bool wi_op_overflows (wide_int &res, tree type,
1619                                         const wide_int &w0, const wide_int &w1) const;
1620   virtual bool op1_range (irange &r, tree type,
1621                                 const irange &lhs,
1622                                 const irange &op2,
1623                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1624   virtual bool op2_range (irange &r, tree type,
1625                                 const irange &lhs,
1626                                 const irange &op1,
1627                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1628 } op_mult;
1629 
1630 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const1631 operator_mult::op1_range (irange &r, tree type,
1632                                 const irange &lhs, const irange &op2,
1633                                 relation_kind rel ATTRIBUTE_UNUSED) const
1634 {
1635   tree offset;
1636 
1637   // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1638   // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1639   // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1640   if (TYPE_OVERFLOW_WRAPS (type))
1641     return false;
1642 
1643   if (op2.singleton_p (&offset) && !integer_zerop (offset))
1644     return range_op_handler (TRUNC_DIV_EXPR, type)->fold_range (r, type,
1645                                                                                 lhs, op2);
1646   return false;
1647 }
1648 
1649 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel) const1650 operator_mult::op2_range (irange &r, tree type,
1651                                 const irange &lhs, const irange &op1,
1652                                 relation_kind rel) const
1653 {
1654   return operator_mult::op1_range (r, type, lhs, op1, rel);
1655 }
1656 
1657 bool
wi_op_overflows(wide_int & res,tree type,const wide_int & w0,const wide_int & w1) const1658 operator_mult::wi_op_overflows (wide_int &res, tree type,
1659                                         const wide_int &w0, const wide_int &w1) const
1660 {
1661   wi::overflow_type overflow = wi::OVF_NONE;
1662   signop sign = TYPE_SIGN (type);
1663   res = wi::mul (w0, w1, sign, &overflow);
1664    if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1665      {
1666        // For multiplication, the sign of the overflow is given
1667        // by the comparison of the signs of the operands.
1668        if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1669            res = wi::max_value (w0.get_precision (), sign);
1670        else
1671            res = wi::min_value (w0.get_precision (), sign);
1672        return false;
1673      }
1674    return overflow;
1675 }
1676 
1677 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1678 operator_mult::wi_fold (irange &r, tree type,
1679                               const wide_int &lh_lb, const wide_int &lh_ub,
1680                               const wide_int &rh_lb, const wide_int &rh_ub) const
1681 {
1682   if (TYPE_OVERFLOW_UNDEFINED (type))
1683     {
1684       wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1685       return;
1686     }
1687 
1688   // Multiply the ranges when overflow wraps.  This is basically fancy
1689   // code so we don't drop to varying with an unsigned
1690   // [-3,-1]*[-3,-1].
1691   //
1692   // This test requires 2*prec bits if both operands are signed and
1693   // 2*prec + 2 bits if either is not.  Therefore, extend the values
1694   // using the sign of the result to PREC2.  From here on out,
1695   // everthing is just signed math no matter what the input types
1696   // were.
1697 
1698   signop sign = TYPE_SIGN (type);
1699   unsigned prec = TYPE_PRECISION (type);
1700   widest2_int min0 = widest2_int::from (lh_lb, sign);
1701   widest2_int max0 = widest2_int::from (lh_ub, sign);
1702   widest2_int min1 = widest2_int::from (rh_lb, sign);
1703   widest2_int max1 = widest2_int::from (rh_ub, sign);
1704   widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1705   widest2_int size = sizem1 + 1;
1706 
1707   // Canonicalize the intervals.
1708   if (sign == UNSIGNED)
1709     {
1710       if (wi::ltu_p (size, min0 + max0))
1711           {
1712             min0 -= size;
1713             max0 -= size;
1714           }
1715       if (wi::ltu_p (size, min1 + max1))
1716           {
1717             min1 -= size;
1718             max1 -= size;
1719           }
1720     }
1721 
1722   // Sort the 4 products so that min is in prod0 and max is in
1723   // prod3.
1724   widest2_int prod0 = min0 * min1;
1725   widest2_int prod1 = min0 * max1;
1726   widest2_int prod2 = max0 * min1;
1727   widest2_int prod3 = max0 * max1;
1728 
1729   // min0min1 > max0max1
1730   if (prod0 > prod3)
1731     std::swap (prod0, prod3);
1732 
1733   // min0max1 > max0min1
1734   if (prod1 > prod2)
1735     std::swap (prod1, prod2);
1736 
1737   if (prod0 > prod1)
1738     std::swap (prod0, prod1);
1739 
1740   if (prod2 > prod3)
1741     std::swap (prod2, prod3);
1742 
1743   // diff = max - min
1744   prod2 = prod3 - prod0;
1745   if (wi::geu_p (prod2, sizem1))
1746     // The range covers all values.
1747     r.set_varying (type);
1748   else
1749     {
1750       wide_int new_lb = wide_int::from (prod0, prec, sign);
1751       wide_int new_ub = wide_int::from (prod3, prec, sign);
1752       create_possibly_reversed_range (r, type, new_lb, new_ub);
1753     }
1754 }
1755 
1756 
1757 class operator_div : public cross_product_operator
1758 {
1759 public:
operator_div(enum tree_code c)1760   operator_div (enum tree_code c)  { code = c; }
1761   virtual void wi_fold (irange &r, tree type,
1762                             const wide_int &lh_lb,
1763                             const wide_int &lh_ub,
1764                             const wide_int &rh_lb,
1765                             const wide_int &rh_ub) const;
1766   virtual bool wi_op_overflows (wide_int &res, tree type,
1767                                         const wide_int &, const wide_int &) const;
1768 private:
1769   enum tree_code code;
1770 };
1771 
1772 bool
wi_op_overflows(wide_int & res,tree type,const wide_int & w0,const wide_int & w1) const1773 operator_div::wi_op_overflows (wide_int &res, tree type,
1774                                      const wide_int &w0, const wide_int &w1) const
1775 {
1776   if (w1 == 0)
1777     return true;
1778 
1779   wi::overflow_type overflow = wi::OVF_NONE;
1780   signop sign = TYPE_SIGN (type);
1781 
1782   switch (code)
1783     {
1784     case EXACT_DIV_EXPR:
1785       // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1786       // operator_exact_divide.  No need to handle it here.
1787       gcc_unreachable ();
1788       break;
1789     case TRUNC_DIV_EXPR:
1790       res = wi::div_trunc (w0, w1, sign, &overflow);
1791       break;
1792     case FLOOR_DIV_EXPR:
1793       res = wi::div_floor (w0, w1, sign, &overflow);
1794       break;
1795     case ROUND_DIV_EXPR:
1796       res = wi::div_round (w0, w1, sign, &overflow);
1797       break;
1798     case CEIL_DIV_EXPR:
1799       res = wi::div_ceil (w0, w1, sign, &overflow);
1800       break;
1801     default:
1802       gcc_unreachable ();
1803     }
1804 
1805   if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1806     {
1807       // For division, the only case is -INF / -1 = +INF.
1808       res = wi::max_value (w0.get_precision (), sign);
1809       return false;
1810     }
1811   return overflow;
1812 }
1813 
1814 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const1815 operator_div::wi_fold (irange &r, tree type,
1816                            const wide_int &lh_lb, const wide_int &lh_ub,
1817                            const wide_int &rh_lb, const wide_int &rh_ub) const
1818 {
1819   const wide_int dividend_min = lh_lb;
1820   const wide_int dividend_max = lh_ub;
1821   const wide_int divisor_min = rh_lb;
1822   const wide_int divisor_max = rh_ub;
1823   signop sign = TYPE_SIGN (type);
1824   unsigned prec = TYPE_PRECISION (type);
1825   wide_int extra_min, extra_max;
1826 
1827   // If we know we won't divide by zero, just do the division.
1828   if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1829     {
1830       wi_cross_product (r, type, dividend_min, dividend_max,
1831                            divisor_min, divisor_max);
1832       return;
1833     }
1834 
1835   // If we're definitely dividing by zero, there's nothing to do.
1836   if (wi_zero_p (type, divisor_min, divisor_max))
1837     {
1838       r.set_undefined ();
1839       return;
1840     }
1841 
1842   // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1843   // skip any division by zero.
1844 
1845   // First divide by the negative numbers, if any.
1846   if (wi::neg_p (divisor_min, sign))
1847     wi_cross_product (r, type, dividend_min, dividend_max,
1848                           divisor_min, wi::minus_one (prec));
1849   else
1850     r.set_undefined ();
1851 
1852   // Then divide by the non-zero positive numbers, if any.
1853   if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1854     {
1855       int_range_max tmp;
1856       wi_cross_product (tmp, type, dividend_min, dividend_max,
1857                               wi::one (prec), divisor_max);
1858       r.union_ (tmp);
1859     }
1860   // We shouldn't still have undefined here.
1861   gcc_checking_assert (!r.undefined_p ());
1862 }
1863 
1864 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1865 operator_div op_floor_div (FLOOR_DIV_EXPR);
1866 operator_div op_round_div (ROUND_DIV_EXPR);
1867 operator_div op_ceil_div (CEIL_DIV_EXPR);
1868 
1869 
1870 class operator_exact_divide : public operator_div
1871 {
1872 public:
operator_exact_divide()1873   operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1874   virtual bool op1_range (irange &r, tree type,
1875                                 const irange &lhs,
1876                                 const irange &op2,
1877                                 relation_kind rel ATTRIBUTE_UNUSED) const;
1878 
1879 } op_exact_div;
1880 
1881 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const1882 operator_exact_divide::op1_range (irange &r, tree type,
1883                                           const irange &lhs,
1884                                           const irange &op2,
1885                                           relation_kind rel ATTRIBUTE_UNUSED) const
1886 {
1887   tree offset;
1888   // [2, 4] = op1 / [3,3]   since its exact divide, no need to worry about
1889   // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1890   // We wont bother trying to enumerate all the in between stuff :-P
1891   // TRUE accuraacy is [6,6][9,9][12,12].  This is unlikely to matter most of
1892   // the time however.
1893   // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1894   if (op2.singleton_p (&offset)
1895       && !integer_zerop (offset))
1896     return range_op_handler (MULT_EXPR, type)->fold_range (r, type, lhs, op2);
1897   return false;
1898 }
1899 
1900 
1901 class operator_lshift : public cross_product_operator
1902 {
1903 public:
1904   virtual bool op1_range (irange &r, tree type,
1905                                 const irange &lhs,
1906                                 const irange &op2,
1907                                 relation_kind rel = VREL_NONE) const;
1908   virtual bool fold_range (irange &r, tree type,
1909                                  const irange &op1,
1910                                  const irange &op2,
1911                                  relation_kind rel = VREL_NONE) const;
1912 
1913   virtual void wi_fold (irange &r, tree type,
1914                               const wide_int &lh_lb, const wide_int &lh_ub,
1915                               const wide_int &rh_lb, const wide_int &rh_ub) const;
1916   virtual bool wi_op_overflows (wide_int &res,
1917                                         tree type,
1918                                         const wide_int &,
1919                                         const wide_int &) const;
1920 } op_lshift;
1921 
1922 class operator_rshift : public cross_product_operator
1923 {
1924 public:
1925   virtual bool fold_range (irange &r, tree type,
1926                                  const irange &op1,
1927                                  const irange &op2,
1928                                  relation_kind rel = VREL_NONE) const;
1929   virtual void wi_fold (irange &r, tree type,
1930                               const wide_int &lh_lb,
1931                               const wide_int &lh_ub,
1932                               const wide_int &rh_lb,
1933                               const wide_int &rh_ub) const;
1934   virtual bool wi_op_overflows (wide_int &res,
1935                                         tree type,
1936                                         const wide_int &w0,
1937                                         const wide_int &w1) const;
1938   virtual bool op1_range (irange &, tree type,
1939                                 const irange &lhs,
1940                                 const irange &op2,
1941                                 relation_kind rel = VREL_NONE) const;
1942   virtual enum tree_code lhs_op1_relation (const irange &lhs,
1943                                                      const irange &op1,
1944                                                      const irange &op2) const;
1945 } op_rshift;
1946 
1947 
1948 enum tree_code
lhs_op1_relation(const irange & lhs ATTRIBUTE_UNUSED,const irange & op1,const irange & op2) const1949 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
1950                                            const irange &op1,
1951                                            const irange &op2) const
1952 {
1953   // If both operands range are >= 0, then the LHS <= op1.
1954   if (!op1.undefined_p () && !op2.undefined_p ()
1955       && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
1956       && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
1957     return LE_EXPR;
1958   return VREL_NONE;
1959 }
1960 
1961 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const1962 operator_lshift::fold_range (irange &r, tree type,
1963                                    const irange &op1,
1964                                    const irange &op2,
1965                                    relation_kind rel) const
1966 {
1967   int_range_max shift_range;
1968   if (!get_shift_range (shift_range, type, op2))
1969     {
1970       if (op2.undefined_p ())
1971           r.set_undefined ();
1972       else
1973           r.set_zero (type);
1974       return true;
1975     }
1976 
1977   // Transform left shifts by constants into multiplies.
1978   if (shift_range.singleton_p ())
1979     {
1980       unsigned shift = shift_range.lower_bound ().to_uhwi ();
1981       wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
1982       int_range<1> mult (type, tmp, tmp);
1983 
1984       // Force wrapping multiplication.
1985       bool saved_flag_wrapv = flag_wrapv;
1986       bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
1987       flag_wrapv = 1;
1988       flag_wrapv_pointer = 1;
1989       bool b = op_mult.fold_range (r, type, op1, mult);
1990       flag_wrapv = saved_flag_wrapv;
1991       flag_wrapv_pointer = saved_flag_wrapv_pointer;
1992       return b;
1993     }
1994   else
1995     // Otherwise, invoke the generic fold routine.
1996     return range_operator::fold_range (r, type, op1, shift_range, rel);
1997 }
1998 
1999 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const2000 operator_lshift::wi_fold (irange &r, tree type,
2001                                 const wide_int &lh_lb, const wide_int &lh_ub,
2002                                 const wide_int &rh_lb, const wide_int &rh_ub) const
2003 {
2004   signop sign = TYPE_SIGN (type);
2005   unsigned prec = TYPE_PRECISION (type);
2006   int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2007   int bound_shift = overflow_pos - rh_ub.to_shwi ();
2008   // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2009   // overflow.  However, for that to happen, rh.max needs to be zero,
2010   // which means rh is a singleton range of zero, which means we simply return
2011   // [lh_lb, lh_ub] as the range.
2012   if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2013     {
2014       r = int_range<2> (type, lh_lb, lh_ub);
2015       return;
2016     }
2017 
2018   wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2019   wide_int complement = ~(bound - 1);
2020   wide_int low_bound, high_bound;
2021   bool in_bounds = false;
2022 
2023   if (sign == UNSIGNED)
2024     {
2025       low_bound = bound;
2026       high_bound = complement;
2027       if (wi::ltu_p (lh_ub, low_bound))
2028           {
2029             // [5, 6] << [1, 2] == [10, 24].
2030             // We're shifting out only zeroes, the value increases
2031             // monotonically.
2032             in_bounds = true;
2033           }
2034       else if (wi::ltu_p (high_bound, lh_lb))
2035           {
2036             // [0xffffff00, 0xffffffff] << [1, 2]
2037             // == [0xfffffc00, 0xfffffffe].
2038             // We're shifting out only ones, the value decreases
2039             // monotonically.
2040             in_bounds = true;
2041           }
2042     }
2043   else
2044     {
2045       // [-1, 1] << [1, 2] == [-4, 4]
2046       low_bound = complement;
2047       high_bound = bound;
2048       if (wi::lts_p (lh_ub, high_bound)
2049             && wi::lts_p (low_bound, lh_lb))
2050           {
2051             // For non-negative numbers, we're shifting out only zeroes,
2052             // the value increases monotonically.  For negative numbers,
2053             // we're shifting out only ones, the value decreases
2054             // monotonically.
2055             in_bounds = true;
2056           }
2057     }
2058 
2059   if (in_bounds)
2060     wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2061   else
2062    r.set_varying (type);
2063 }
2064 
2065 bool
wi_op_overflows(wide_int & res,tree type,const wide_int & w0,const wide_int & w1) const2066 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2067                                           const wide_int &w0, const wide_int &w1) const
2068 {
2069   signop sign = TYPE_SIGN (type);
2070   if (wi::neg_p (w1))
2071     {
2072       // It's unclear from the C standard whether shifts can overflow.
2073       // The following code ignores overflow; perhaps a C standard
2074       // interpretation ruling is needed.
2075       res = wi::rshift (w0, -w1, sign);
2076     }
2077   else
2078     res = wi::lshift (w0, w1);
2079   return false;
2080 }
2081 
2082 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const2083 operator_lshift::op1_range (irange &r,
2084                                   tree type,
2085                                   const irange &lhs,
2086                                   const irange &op2,
2087                                   relation_kind rel ATTRIBUTE_UNUSED) const
2088 {
2089   tree shift_amount;
2090 
2091   if (!lhs.contains_p (build_zero_cst (type)))
2092     r.set_nonzero (type);
2093   else
2094     r.set_varying (type);
2095 
2096   if (op2.singleton_p (&shift_amount))
2097     {
2098       wide_int shift = wi::to_wide (shift_amount);
2099       if (wi::lt_p (shift, 0, SIGNED))
2100           return false;
2101       if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2102                                              TYPE_PRECISION (op2.type ())),
2103                         UNSIGNED))
2104           return false;
2105       if (shift == 0)
2106           {
2107             r.intersect (lhs);
2108             return true;
2109           }
2110 
2111       // Work completely in unsigned mode to start.
2112       tree utype = type;
2113       int_range_max tmp_range;
2114       if (TYPE_SIGN (type) == SIGNED)
2115           {
2116             int_range_max tmp = lhs;
2117             utype = unsigned_type_for (type);
2118             range_cast (tmp, utype);
2119             op_rshift.fold_range (tmp_range, utype, tmp, op2);
2120           }
2121       else
2122           op_rshift.fold_range (tmp_range, utype, lhs, op2);
2123 
2124       // Start with ranges which can produce the LHS by right shifting the
2125       // result by the shift amount.
2126       // ie   [0x08, 0xF0] = op1 << 2 will start with
2127       //      [00001000, 11110000] = op1 << 2
2128       //  [0x02, 0x4C] aka [00000010, 00111100]
2129 
2130       // Then create a range from the LB with the least significant upper bit
2131       // set, to the upper bound with all the bits set.
2132       // This would be [0x42, 0xFC] aka [01000010, 11111100].
2133 
2134       // Ideally we do this for each subrange, but just lump them all for now.
2135       unsigned low_bits = TYPE_PRECISION (utype)
2136                                 - TREE_INT_CST_LOW (shift_amount);
2137       wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2138       wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2139       wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2140       int_range<2> fill_range (utype, new_lb, new_ub);
2141       tmp_range.union_ (fill_range);
2142 
2143       if (utype != type)
2144           range_cast (tmp_range, type);
2145 
2146       r.intersect (tmp_range);
2147       return true;
2148     }
2149 
2150   return !r.varying_p ();
2151 }
2152 
2153 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const2154 operator_rshift::op1_range (irange &r,
2155                                   tree type,
2156                                   const irange &lhs,
2157                                   const irange &op2,
2158                                   relation_kind rel ATTRIBUTE_UNUSED) const
2159 {
2160   tree shift;
2161   if (op2.singleton_p (&shift))
2162     {
2163       // Ignore nonsensical shifts.
2164       unsigned prec = TYPE_PRECISION (type);
2165       if (wi::ge_p (wi::to_wide (shift),
2166                         wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2167                         UNSIGNED))
2168           return false;
2169       if (wi::to_wide (shift) == 0)
2170           {
2171             r = lhs;
2172             return true;
2173           }
2174 
2175       // Folding the original operation may discard some impossible
2176       // ranges from the LHS.
2177       int_range_max lhs_refined;
2178       op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2179       lhs_refined.intersect (lhs);
2180       if (lhs_refined.undefined_p ())
2181           {
2182             r.set_undefined ();
2183             return true;
2184           }
2185       int_range_max shift_range (shift, shift);
2186       int_range_max lb, ub;
2187       op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2188       //    LHS
2189       // 0000 0111 = OP1 >> 3
2190       //
2191       // OP1 is anything from 0011 1000 to 0011 1111.  That is, a
2192       // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2193       // right hand side (0x07).
2194       tree mask = fold_build1 (BIT_NOT_EXPR, type,
2195                                      fold_build2 (LSHIFT_EXPR, type,
2196                                                       build_minus_one_cst (type),
2197                                                       shift));
2198       int_range_max mask_range (build_zero_cst (type), mask);
2199       op_plus.fold_range (ub, type, lb, mask_range);
2200       r = lb;
2201       r.union_ (ub);
2202       if (!lhs_refined.contains_p (build_zero_cst (type)))
2203           {
2204             mask_range.invert ();
2205             r.intersect (mask_range);
2206           }
2207       return true;
2208     }
2209   return false;
2210 }
2211 
2212 bool
wi_op_overflows(wide_int & res,tree type,const wide_int & w0,const wide_int & w1) const2213 operator_rshift::wi_op_overflows (wide_int &res,
2214                                           tree type,
2215                                           const wide_int &w0,
2216                                           const wide_int &w1) const
2217 {
2218   signop sign = TYPE_SIGN (type);
2219   if (wi::neg_p (w1))
2220     res = wi::lshift (w0, -w1);
2221   else
2222     {
2223       // It's unclear from the C standard whether shifts can overflow.
2224       // The following code ignores overflow; perhaps a C standard
2225       // interpretation ruling is needed.
2226       res = wi::rshift (w0, w1, sign);
2227     }
2228   return false;
2229 }
2230 
2231 bool
fold_range(irange & r,tree type,const irange & op1,const irange & op2,relation_kind rel) const2232 operator_rshift::fold_range (irange &r, tree type,
2233                                    const irange &op1,
2234                                    const irange &op2,
2235                                    relation_kind rel) const
2236 {
2237   int_range_max shift;
2238   if (!get_shift_range (shift, type, op2))
2239     {
2240       if (op2.undefined_p ())
2241           r.set_undefined ();
2242       else
2243           r.set_zero (type);
2244       return true;
2245     }
2246 
2247   return range_operator::fold_range (r, type, op1, shift, rel);
2248 }
2249 
2250 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const2251 operator_rshift::wi_fold (irange &r, tree type,
2252                                 const wide_int &lh_lb, const wide_int &lh_ub,
2253                                 const wide_int &rh_lb, const wide_int &rh_ub) const
2254 {
2255   wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2256 }
2257 
2258 
2259 class operator_cast: public range_operator
2260 {
2261 public:
2262   virtual bool fold_range (irange &r, tree type,
2263                                  const irange &op1,
2264                                  const irange &op2,
2265                                  relation_kind rel = VREL_NONE) const;
2266   virtual bool op1_range (irange &r, tree type,
2267                                 const irange &lhs,
2268                                 const irange &op2,
2269                                 relation_kind rel = VREL_NONE) const;
2270 private:
2271   bool truncating_cast_p (const irange &inner, const irange &outer) const;
2272   bool inside_domain_p (const wide_int &min, const wide_int &max,
2273                               const irange &outer) const;
2274   void fold_pair (irange &r, unsigned index, const irange &inner,
2275                                  const irange &outer) const;
2276 } op_convert;
2277 
2278 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2279 
2280 inline bool
truncating_cast_p(const irange & inner,const irange & outer) const2281 operator_cast::truncating_cast_p (const irange &inner,
2282                                           const irange &outer) const
2283 {
2284   return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2285 }
2286 
2287 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2288 
2289 bool
inside_domain_p(const wide_int & min,const wide_int & max,const irange & range) const2290 operator_cast::inside_domain_p (const wide_int &min,
2291                                         const wide_int &max,
2292                                         const irange &range) const
2293 {
2294   wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2295   wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2296   signop domain_sign = TYPE_SIGN (range.type ());
2297   return (wi::le_p (min, domain_max, domain_sign)
2298             && wi::le_p (max, domain_max, domain_sign)
2299             && wi::ge_p (min, domain_min, domain_sign)
2300             && wi::ge_p (max, domain_min, domain_sign));
2301 }
2302 
2303 
2304 // Helper for fold_range which work on a pair at a time.
2305 
2306 void
fold_pair(irange & r,unsigned index,const irange & inner,const irange & outer) const2307 operator_cast::fold_pair (irange &r, unsigned index,
2308                                  const irange &inner,
2309                                  const irange &outer) const
2310 {
2311   tree inner_type = inner.type ();
2312   tree outer_type = outer.type ();
2313   signop inner_sign = TYPE_SIGN (inner_type);
2314   unsigned outer_prec = TYPE_PRECISION (outer_type);
2315 
2316   // check to see if casting from INNER to OUTER is a conversion that
2317   // fits in the resulting OUTER type.
2318   wide_int inner_lb = inner.lower_bound (index);
2319   wide_int inner_ub = inner.upper_bound (index);
2320   if (truncating_cast_p (inner, outer))
2321     {
2322       // We may be able to accomodate a truncating cast if the
2323       // resulting range can be represented in the target type...
2324       if (wi::rshift (wi::sub (inner_ub, inner_lb),
2325                           wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2326                                         inner_sign) != 0)
2327           {
2328             r.set_varying (outer_type);
2329             return;
2330           }
2331     }
2332   // ...but we must still verify that the final range fits in the
2333   // domain.  This catches -fstrict-enum restrictions where the domain
2334   // range is smaller than what fits in the underlying type.
2335   wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2336   wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2337   if (inside_domain_p (min, max, outer))
2338     create_possibly_reversed_range (r, outer_type, min, max);
2339   else
2340     r.set_varying (outer_type);
2341 }
2342 
2343 
2344 bool
fold_range(irange & r,tree type ATTRIBUTE_UNUSED,const irange & inner,const irange & outer,relation_kind rel ATTRIBUTE_UNUSED) const2345 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2346                                  const irange &inner,
2347                                  const irange &outer,
2348                                  relation_kind rel ATTRIBUTE_UNUSED) const
2349 {
2350   if (empty_range_varying (r, type, inner, outer))
2351     return true;
2352 
2353   gcc_checking_assert (outer.varying_p ());
2354   gcc_checking_assert (inner.num_pairs () > 0);
2355 
2356   // Avoid a temporary by folding the first pair directly into the result.
2357   fold_pair (r, 0, inner, outer);
2358 
2359   // Then process any additonal pairs by unioning with their results.
2360   for (unsigned x = 1; x < inner.num_pairs (); ++x)
2361     {
2362       int_range_max tmp;
2363       fold_pair (tmp, x, inner, outer);
2364       r.union_ (tmp);
2365       if (r.varying_p ())
2366           return true;
2367     }
2368   return true;
2369 }
2370 
2371 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const2372 operator_cast::op1_range (irange &r, tree type,
2373                                 const irange &lhs,
2374                                 const irange &op2,
2375                                 relation_kind rel ATTRIBUTE_UNUSED) const
2376 {
2377   tree lhs_type = lhs.type ();
2378   gcc_checking_assert (types_compatible_p (op2.type(), type));
2379 
2380   // If we are calculating a pointer, shortcut to what we really care about.
2381   if (POINTER_TYPE_P (type))
2382     {
2383       // Conversion from other pointers or a constant (including 0/NULL)
2384       // are straightforward.
2385       if (POINTER_TYPE_P (lhs.type ())
2386             || (lhs.singleton_p ()
2387                 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2388           {
2389             r = lhs;
2390             range_cast (r, type);
2391           }
2392       else
2393           {
2394             // If the LHS is not a pointer nor a singleton, then it is
2395             // either VARYING or non-zero.
2396             if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2397               r.set_nonzero (type);
2398             else
2399               r.set_varying (type);
2400           }
2401       r.intersect (op2);
2402       return true;
2403     }
2404 
2405   if (truncating_cast_p (op2, lhs))
2406     {
2407       if (lhs.varying_p ())
2408           r.set_varying (type);
2409       else
2410         {
2411             // We want to insert the LHS as an unsigned value since it
2412             // would not trigger the signed bit of the larger type.
2413             int_range_max converted_lhs = lhs;
2414             range_cast (converted_lhs, unsigned_type_for (lhs_type));
2415             range_cast (converted_lhs, type);
2416             // Start by building the positive signed outer range for the type.
2417             wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2418                                                         TYPE_PRECISION (type));
2419             r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2420                                                                   SIGNED));
2421             // For the signed part, we need to simply union the 2 ranges now.
2422             r.union_ (converted_lhs);
2423 
2424             // Create maximal negative number outside of LHS bits.
2425             lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2426                                 TYPE_PRECISION (type));
2427             // Add this to the unsigned LHS range(s).
2428             int_range_max lim_range (type, lim, lim);
2429             int_range_max lhs_neg;
2430             range_op_handler (PLUS_EXPR, type)->fold_range (lhs_neg,
2431                                                                         type,
2432                                                                         converted_lhs,
2433                                                                         lim_range);
2434             // lhs_neg now has all the negative versions of the LHS.
2435             // Now union in all the values from SIGNED MIN (0x80000) to
2436             // lim-1 in order to fill in all the ranges with the upper
2437             // bits set.
2438 
2439             // PR 97317.  If the lhs has only 1 bit less precision than the rhs,
2440             // we don't need to create a range from min to lim-1
2441             // calculate neg range traps trying to create [lim, lim - 1].
2442             wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2443             if (lim != min_val)
2444               {
2445                 int_range_max neg (type,
2446                                          wi::min_value (TYPE_PRECISION (type),
2447                                                             SIGNED),
2448                                          lim - 1);
2449                 lhs_neg.union_ (neg);
2450               }
2451             // And finally, munge the signed and unsigned portions.
2452             r.union_ (lhs_neg);
2453           }
2454       // And intersect with any known value passed in the extra operand.
2455       r.intersect (op2);
2456       return true;
2457     }
2458 
2459   int_range_max tmp;
2460   if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2461     tmp = lhs;
2462   else
2463     {
2464       // The cast is not truncating, and the range is restricted to
2465       // the range of the RHS by this assignment.
2466       //
2467       // Cast the range of the RHS to the type of the LHS.
2468       fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2469       // Intersect this with the LHS range will produce the range,
2470       // which will be cast to the RHS type before returning.
2471       tmp.intersect (lhs);
2472     }
2473 
2474   // Cast the calculated range to the type of the RHS.
2475   fold_range (r, type, tmp, int_range<1> (type));
2476   return true;
2477 }
2478 
2479 
2480 class operator_logical_and : public range_operator
2481 {
2482 public:
2483   virtual bool fold_range (irange &r, tree type,
2484                                  const irange &lh,
2485                                  const irange &rh,
2486                                  relation_kind rel = VREL_NONE) const;
2487   virtual bool op1_range (irange &r, tree type,
2488                                 const irange &lhs,
2489                                 const irange &op2,
2490                                 relation_kind rel = VREL_NONE) const;
2491   virtual bool op2_range (irange &r, tree type,
2492                                 const irange &lhs,
2493                                 const irange &op1,
2494                                 relation_kind rel = VREL_NONE) const;
2495 } op_logical_and;
2496 
2497 
2498 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh,relation_kind rel ATTRIBUTE_UNUSED) const2499 operator_logical_and::fold_range (irange &r, tree type,
2500                                           const irange &lh,
2501                                           const irange &rh,
2502                                           relation_kind rel ATTRIBUTE_UNUSED) const
2503 {
2504   if (empty_range_varying (r, type, lh, rh))
2505     return true;
2506 
2507   // 0 && anything is 0.
2508   if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2509       || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2510     r = range_false (type);
2511   else if (lh.contains_p (build_zero_cst (lh.type ()))
2512              || rh.contains_p (build_zero_cst (rh.type ())))
2513     // To reach this point, there must be a logical 1 on each side, and
2514     // the only remaining question is whether there is a zero or not.
2515     r = range_true_and_false (type);
2516   else
2517     r = range_true (type);
2518   return true;
2519 }
2520 
2521 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2 ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const2522 operator_logical_and::op1_range (irange &r, tree type,
2523                                          const irange &lhs,
2524                                          const irange &op2 ATTRIBUTE_UNUSED,
2525                                          relation_kind rel ATTRIBUTE_UNUSED) const
2526 {
2527    switch (get_bool_state (r, lhs, type))
2528      {
2529      case BRS_TRUE:
2530        // A true result means both sides of the AND must be true.
2531        r = range_true (type);
2532        break;
2533      default:
2534        // Any other result means only one side has to be false, the
2535        // other side can be anything.  So we cannot be sure of any
2536        // result here.
2537        r = range_true_and_false (type);
2538        break;
2539      }
2540   return true;
2541 }
2542 
2543 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const2544 operator_logical_and::op2_range (irange &r, tree type,
2545                                          const irange &lhs,
2546                                          const irange &op1,
2547                                          relation_kind rel ATTRIBUTE_UNUSED) const
2548 {
2549   return operator_logical_and::op1_range (r, type, lhs, op1);
2550 }
2551 
2552 
2553 class operator_bitwise_and : public range_operator
2554 {
2555 public:
2556   virtual bool fold_range (irange &r, tree type,
2557                                  const irange &lh,
2558                                  const irange &rh,
2559                                  relation_kind rel = VREL_NONE) const;
2560   virtual bool op1_range (irange &r, tree type,
2561                                 const irange &lhs,
2562                                 const irange &op2,
2563                                 relation_kind rel = VREL_NONE) const;
2564   virtual bool op2_range (irange &r, tree type,
2565                                 const irange &lhs,
2566                                 const irange &op1,
2567                                 relation_kind rel = VREL_NONE) const;
2568   virtual void wi_fold (irange &r, tree type,
2569                             const wide_int &lh_lb,
2570                             const wide_int &lh_ub,
2571                             const wide_int &rh_lb,
2572                             const wide_int &rh_ub) const;
2573 private:
2574   void simple_op1_range_solver (irange &r, tree type,
2575                                         const irange &lhs,
2576                                         const irange &op2) const;
2577   void remove_impossible_ranges (irange &r, const irange &rh) const;
2578 } op_bitwise_and;
2579 
2580 static bool
unsigned_singleton_p(const irange & op)2581 unsigned_singleton_p (const irange &op)
2582 {
2583   tree mask;
2584   if (op.singleton_p (&mask))
2585     {
2586       wide_int x = wi::to_wide (mask);
2587       return wi::ge_p (x, 0, TYPE_SIGN (op.type ()));
2588     }
2589   return false;
2590 }
2591 
2592 // Remove any ranges from R that are known to be impossible when an
2593 // range is ANDed with MASK.
2594 
2595 void
remove_impossible_ranges(irange & r,const irange & rmask) const2596 operator_bitwise_and::remove_impossible_ranges (irange &r,
2597                                                             const irange &rmask) const
2598 {
2599   if (r.undefined_p () || !unsigned_singleton_p (rmask))
2600     return;
2601 
2602   wide_int mask = rmask.lower_bound ();
2603   tree type = r.type ();
2604   int prec = TYPE_PRECISION (type);
2605   int leading_zeros = wi::clz (mask);
2606   int_range_max impossible_ranges;
2607 
2608   /* We know that starting at the most significant bit, any 0 in the
2609      mask means the resulting range cannot contain a 1 in that same
2610      position.  This means the following ranges are impossible:
2611 
2612           x & 0b1001 1010
2613                                 IMPOSSIBLE RANGES
2614                 01xx xxxx   [0100 0000, 0111 1111]
2615                 001x xxxx   [0010 0000, 0011 1111]
2616                 0000 01xx   [0000 0100, 0000 0111]
2617                 0000 0001   [0000 0001, 0000 0001]
2618   */
2619   wide_int one = wi::one (prec);
2620   for (int i = 0; i < prec - leading_zeros - 1; ++i)
2621     if (wi::bit_and (mask, wi::lshift (one, wi::uhwi (i, prec))) == 0)
2622       {
2623           tree lb = fold_build2 (LSHIFT_EXPR, type,
2624                                      build_one_cst (type),
2625                                      build_int_cst (type, i));
2626           tree ub_left = fold_build1 (BIT_NOT_EXPR, type,
2627                                             fold_build2 (LSHIFT_EXPR, type,
2628                                                              build_minus_one_cst (type),
2629                                                              build_int_cst (type, i)));
2630           tree ub_right = fold_build2 (LSHIFT_EXPR, type,
2631                                              build_one_cst (type),
2632                                              build_int_cst (type, i));
2633           tree ub = fold_build2 (BIT_IOR_EXPR, type, ub_left, ub_right);
2634           impossible_ranges.union_ (int_range<1> (lb, ub));
2635       }
2636   if (!impossible_ranges.undefined_p ())
2637     {
2638       impossible_ranges.invert ();
2639       r.intersect (impossible_ranges);
2640     }
2641 }
2642 
2643 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh,relation_kind rel ATTRIBUTE_UNUSED) const2644 operator_bitwise_and::fold_range (irange &r, tree type,
2645                                           const irange &lh,
2646                                           const irange &rh,
2647                                           relation_kind rel ATTRIBUTE_UNUSED) const
2648 {
2649   if (range_operator::fold_range (r, type, lh, rh))
2650     {
2651       // FIXME: This is temporarily disabled because, though it
2652       // generates better ranges, it's noticeably slower for evrp.
2653       // remove_impossible_ranges (r, rh);
2654       return true;
2655     }
2656   return false;
2657 }
2658 
2659 
2660 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2661 // possible.  Basically, see if we can optimize:
2662 //
2663 //        [LB, UB] op Z
2664 //   into:
2665 //        [LB op Z, UB op Z]
2666 //
2667 // If the optimization was successful, accumulate the range in R and
2668 // return TRUE.
2669 
2670 static bool
wi_optimize_and_or(irange & r,enum tree_code code,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub)2671 wi_optimize_and_or (irange &r,
2672                         enum tree_code code,
2673                         tree type,
2674                         const wide_int &lh_lb, const wide_int &lh_ub,
2675                         const wide_int &rh_lb, const wide_int &rh_ub)
2676 {
2677   // Calculate the singleton mask among the ranges, if any.
2678   wide_int lower_bound, upper_bound, mask;
2679   if (wi::eq_p (rh_lb, rh_ub))
2680     {
2681       mask = rh_lb;
2682       lower_bound = lh_lb;
2683       upper_bound = lh_ub;
2684     }
2685   else if (wi::eq_p (lh_lb, lh_ub))
2686     {
2687       mask = lh_lb;
2688       lower_bound = rh_lb;
2689       upper_bound = rh_ub;
2690     }
2691   else
2692     return false;
2693 
2694   // If Z is a constant which (for op | its bitwise not) has n
2695   // consecutive least significant bits cleared followed by m 1
2696   // consecutive bits set immediately above it and either
2697   // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2698   //
2699   // The least significant n bits of all the values in the range are
2700   // cleared or set, the m bits above it are preserved and any bits
2701   // above these are required to be the same for all values in the
2702   // range.
2703   wide_int w = mask;
2704   int m = 0, n = 0;
2705   if (code == BIT_IOR_EXPR)
2706     w = ~w;
2707   if (wi::eq_p (w, 0))
2708     n = w.get_precision ();
2709   else
2710     {
2711       n = wi::ctz (w);
2712       w = ~(w | wi::mask (n, false, w.get_precision ()));
2713       if (wi::eq_p (w, 0))
2714           m = w.get_precision () - n;
2715       else
2716           m = wi::ctz (w) - n;
2717     }
2718   wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2719   if ((new_mask & lower_bound) != (new_mask & upper_bound))
2720     return false;
2721 
2722   wide_int res_lb, res_ub;
2723   if (code == BIT_AND_EXPR)
2724     {
2725       res_lb = wi::bit_and (lower_bound, mask);
2726       res_ub = wi::bit_and (upper_bound, mask);
2727     }
2728   else if (code == BIT_IOR_EXPR)
2729     {
2730       res_lb = wi::bit_or (lower_bound, mask);
2731       res_ub = wi::bit_or (upper_bound, mask);
2732     }
2733   else
2734     gcc_unreachable ();
2735   value_range_with_overflow (r, type, res_lb, res_ub);
2736 
2737   // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2738   if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2739     {
2740       int_range<2> tmp;
2741       tmp.set_nonzero (type);
2742       r.intersect (tmp);
2743     }
2744   return true;
2745 }
2746 
2747 // For range [LB, UB] compute two wide_int bit masks.
2748 //
2749 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2750 // for all numbers in the range the bit is 0, otherwise it might be 0
2751 // or 1.
2752 //
2753 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2754 // for all numbers in the range the bit is 1, otherwise it might be 0
2755 // or 1.
2756 
2757 void
wi_set_zero_nonzero_bits(tree type,const wide_int & lb,const wide_int & ub,wide_int & maybe_nonzero,wide_int & mustbe_nonzero)2758 wi_set_zero_nonzero_bits (tree type,
2759                                 const wide_int &lb, const wide_int &ub,
2760                                 wide_int &maybe_nonzero,
2761                                 wide_int &mustbe_nonzero)
2762 {
2763   signop sign = TYPE_SIGN (type);
2764 
2765   if (wi::eq_p (lb, ub))
2766     maybe_nonzero = mustbe_nonzero = lb;
2767   else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2768     {
2769       wide_int xor_mask = lb ^ ub;
2770       maybe_nonzero = lb | ub;
2771       mustbe_nonzero = lb & ub;
2772       if (xor_mask != 0)
2773           {
2774             wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2775                                             maybe_nonzero.get_precision ());
2776             maybe_nonzero = maybe_nonzero | mask;
2777             mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2778           }
2779     }
2780   else
2781     {
2782       maybe_nonzero = wi::minus_one (lb.get_precision ());
2783       mustbe_nonzero = wi::zero (lb.get_precision ());
2784     }
2785 }
2786 
2787 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const2788 operator_bitwise_and::wi_fold (irange &r, tree type,
2789                                      const wide_int &lh_lb,
2790                                      const wide_int &lh_ub,
2791                                      const wide_int &rh_lb,
2792                                      const wide_int &rh_ub) const
2793 {
2794   if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2795     return;
2796 
2797   wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2798   wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2799   wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2800                                   maybe_nonzero_lh, mustbe_nonzero_lh);
2801   wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2802                                   maybe_nonzero_rh, mustbe_nonzero_rh);
2803 
2804   wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2805   wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2806   signop sign = TYPE_SIGN (type);
2807   unsigned prec = TYPE_PRECISION (type);
2808   // If both input ranges contain only negative values, we can
2809   // truncate the result range maximum to the minimum of the
2810   // input range maxima.
2811   if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2812     {
2813       new_ub = wi::min (new_ub, lh_ub, sign);
2814       new_ub = wi::min (new_ub, rh_ub, sign);
2815     }
2816   // If either input range contains only non-negative values
2817   // we can truncate the result range maximum to the respective
2818   // maximum of the input range.
2819   if (wi::ge_p (lh_lb, 0, sign))
2820     new_ub = wi::min (new_ub, lh_ub, sign);
2821   if (wi::ge_p (rh_lb, 0, sign))
2822     new_ub = wi::min (new_ub, rh_ub, sign);
2823   // PR68217: In case of signed & sign-bit-CST should
2824   // result in [-INF, 0] instead of [-INF, INF].
2825   if (wi::gt_p (new_lb, new_ub, sign))
2826     {
2827       wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2828       if (sign == SIGNED
2829             && ((wi::eq_p (lh_lb, lh_ub)
2830                  && !wi::cmps (lh_lb, sign_bit))
2831                 || (wi::eq_p (rh_lb, rh_ub)
2832                       && !wi::cmps (rh_lb, sign_bit))))
2833           {
2834             new_lb = wi::min_value (prec, sign);
2835             new_ub = wi::zero (prec);
2836           }
2837     }
2838   // If the limits got swapped around, return varying.
2839   if (wi::gt_p (new_lb, new_ub,sign))
2840     r.set_varying (type);
2841   else
2842     value_range_with_overflow (r, type, new_lb, new_ub);
2843 }
2844 
2845 static void
set_nonzero_range_from_mask(irange & r,tree type,const irange & lhs)2846 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2847 {
2848   if (!lhs.contains_p (build_zero_cst (type)))
2849     r = range_nonzero (type);
2850   else
2851     r.set_varying (type);
2852 }
2853 
2854 // This was shamelessly stolen from register_edge_assert_for_2 and
2855 // adjusted to work with iranges.
2856 
2857 void
simple_op1_range_solver(irange & r,tree type,const irange & lhs,const irange & op2) const2858 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2859                                                          const irange &lhs,
2860                                                          const irange &op2) const
2861 {
2862   if (!op2.singleton_p ())
2863     {
2864       set_nonzero_range_from_mask (r, type, lhs);
2865       return;
2866     }
2867   unsigned int nprec = TYPE_PRECISION (type);
2868   wide_int cst2v = op2.lower_bound ();
2869   bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2870   wide_int sgnbit;
2871   if (cst2n)
2872     sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2873   else
2874     sgnbit = wi::zero (nprec);
2875 
2876   // Solve [lhs.lower_bound (), +INF] = x & MASK.
2877   //
2878   // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2879   // maximum unsigned value is ~0.  For signed comparison, if CST2
2880   // doesn't have the most significant bit set, handle it similarly.  If
2881   // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2882   wide_int valv = lhs.lower_bound ();
2883   wide_int minv = valv & cst2v, maxv;
2884   bool we_know_nothing = false;
2885   if (minv != valv)
2886     {
2887       // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2888       minv = masked_increment (valv, cst2v, sgnbit, nprec);
2889       if (minv == valv)
2890           {
2891             // If we can't determine anything on this bound, fall
2892             // through and conservatively solve for the other end point.
2893             we_know_nothing = true;
2894           }
2895     }
2896   maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2897   if (we_know_nothing)
2898     r.set_varying (type);
2899   else
2900     r = int_range<1> (type, minv, maxv);
2901 
2902   // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2903   //
2904   // Minimum unsigned value for <= is 0 and maximum unsigned value is
2905   // VAL | ~CST2 if (VAL & CST2) == VAL.  Otherwise, find smallest
2906   // VAL2 where
2907   // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2908   // as maximum.
2909   // For signed comparison, if CST2 doesn't have most significant bit
2910   // set, handle it similarly.  If CST2 has MSB set, the maximum is
2911   // the same and minimum is INT_MIN.
2912   valv = lhs.upper_bound ();
2913   minv = valv & cst2v;
2914   if (minv == valv)
2915     maxv = valv;
2916   else
2917     {
2918       maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2919       if (maxv == valv)
2920           {
2921             // If we couldn't determine anything on either bound, return
2922             // undefined.
2923             if (we_know_nothing)
2924               r.set_undefined ();
2925             return;
2926           }
2927       maxv -= 1;
2928     }
2929   maxv |= ~cst2v;
2930   minv = sgnbit;
2931   int_range<1> upper_bits (type, minv, maxv);
2932   r.intersect (upper_bits);
2933 }
2934 
2935 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const2936 operator_bitwise_and::op1_range (irange &r, tree type,
2937                                          const irange &lhs,
2938                                          const irange &op2,
2939                                          relation_kind rel ATTRIBUTE_UNUSED) const
2940 {
2941   if (types_compatible_p (type, boolean_type_node))
2942     return op_logical_and.op1_range (r, type, lhs, op2);
2943 
2944   r.set_undefined ();
2945   for (unsigned i = 0; i < lhs.num_pairs (); ++i)
2946     {
2947       int_range_max chunk (lhs.type (),
2948                                  lhs.lower_bound (i),
2949                                  lhs.upper_bound (i));
2950       int_range_max res;
2951       simple_op1_range_solver (res, type, chunk, op2);
2952       r.union_ (res);
2953     }
2954   if (r.undefined_p ())
2955     set_nonzero_range_from_mask (r, type, lhs);
2956   return true;
2957 }
2958 
2959 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const2960 operator_bitwise_and::op2_range (irange &r, tree type,
2961                                          const irange &lhs,
2962                                          const irange &op1,
2963                                          relation_kind rel ATTRIBUTE_UNUSED) const
2964 {
2965   return operator_bitwise_and::op1_range (r, type, lhs, op1);
2966 }
2967 
2968 
2969 class operator_logical_or : public range_operator
2970 {
2971 public:
2972   virtual bool fold_range (irange &r, tree type,
2973                                  const irange &lh,
2974                                  const irange &rh,
2975                                  relation_kind rel = VREL_NONE) const;
2976   virtual bool op1_range (irange &r, tree type,
2977                                 const irange &lhs,
2978                                 const irange &op2,
2979                                 relation_kind rel = VREL_NONE) const;
2980   virtual bool op2_range (irange &r, tree type,
2981                                 const irange &lhs,
2982                                 const irange &op1,
2983                                 relation_kind rel = VREL_NONE) const;
2984 } op_logical_or;
2985 
2986 bool
fold_range(irange & r,tree type ATTRIBUTE_UNUSED,const irange & lh,const irange & rh,relation_kind rel ATTRIBUTE_UNUSED) const2987 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2988                                          const irange &lh,
2989                                          const irange &rh,
2990                                          relation_kind rel ATTRIBUTE_UNUSED) const
2991 {
2992   if (empty_range_varying (r, type, lh, rh))
2993     return true;
2994 
2995   r = lh;
2996   r.union_ (rh);
2997   return true;
2998 }
2999 
3000 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2 ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3001 operator_logical_or::op1_range (irange &r, tree type,
3002                                         const irange &lhs,
3003                                         const irange &op2 ATTRIBUTE_UNUSED,
3004                                         relation_kind rel ATTRIBUTE_UNUSED) const
3005 {
3006    switch (get_bool_state (r, lhs, type))
3007      {
3008      case BRS_FALSE:
3009        // A false result means both sides of the OR must be false.
3010        r = range_false (type);
3011        break;
3012      default:
3013        // Any other result means only one side has to be true, the
3014        // other side can be anything. so we can't be sure of any result
3015        // here.
3016        r = range_true_and_false (type);
3017        break;
3018     }
3019   return true;
3020 }
3021 
3022 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const3023 operator_logical_or::op2_range (irange &r, tree type,
3024                                         const irange &lhs,
3025                                         const irange &op1,
3026                                         relation_kind rel ATTRIBUTE_UNUSED) const
3027 {
3028   return operator_logical_or::op1_range (r, type, lhs, op1);
3029 }
3030 
3031 
3032 class operator_bitwise_or : public range_operator
3033 {
3034 public:
3035   virtual bool op1_range (irange &r, tree type,
3036                                 const irange &lhs,
3037                                 const irange &op2,
3038                                 relation_kind rel = VREL_NONE) const;
3039   virtual bool op2_range (irange &r, tree type,
3040                                 const irange &lhs,
3041                                 const irange &op1,
3042                                 relation_kind rel= VREL_NONE) const;
3043   virtual void wi_fold (irange &r, tree type,
3044                             const wide_int &lh_lb,
3045                             const wide_int &lh_ub,
3046                             const wide_int &rh_lb,
3047                             const wide_int &rh_ub) const;
3048 } op_bitwise_or;
3049 
3050 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const3051 operator_bitwise_or::wi_fold (irange &r, tree type,
3052                                     const wide_int &lh_lb,
3053                                     const wide_int &lh_ub,
3054                                     const wide_int &rh_lb,
3055                                     const wide_int &rh_ub) const
3056 {
3057   if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3058     return;
3059 
3060   wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3061   wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3062   wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3063                                   maybe_nonzero_lh, mustbe_nonzero_lh);
3064   wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3065                                   maybe_nonzero_rh, mustbe_nonzero_rh);
3066   wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3067   wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3068   signop sign = TYPE_SIGN (type);
3069   // If the input ranges contain only positive values we can
3070   // truncate the minimum of the result range to the maximum
3071   // of the input range minima.
3072   if (wi::ge_p (lh_lb, 0, sign)
3073       && wi::ge_p (rh_lb, 0, sign))
3074     {
3075       new_lb = wi::max (new_lb, lh_lb, sign);
3076       new_lb = wi::max (new_lb, rh_lb, sign);
3077     }
3078   // If either input range contains only negative values
3079   // we can truncate the minimum of the result range to the
3080   // respective minimum range.
3081   if (wi::lt_p (lh_ub, 0, sign))
3082     new_lb = wi::max (new_lb, lh_lb, sign);
3083   if (wi::lt_p (rh_ub, 0, sign))
3084     new_lb = wi::max (new_lb, rh_lb, sign);
3085   // If the limits got swapped around, return a conservative range.
3086   if (wi::gt_p (new_lb, new_ub, sign))
3087     {
3088       // Make sure that nonzero|X is nonzero.
3089       if (wi::gt_p (lh_lb, 0, sign)
3090             || wi::gt_p (rh_lb, 0, sign)
3091             || wi::lt_p (lh_ub, 0, sign)
3092             || wi::lt_p (rh_ub, 0, sign))
3093           r.set_nonzero (type);
3094       else
3095           r.set_varying (type);
3096       return;
3097     }
3098   value_range_with_overflow (r, type, new_lb, new_ub);
3099 }
3100 
3101 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3102 operator_bitwise_or::op1_range (irange &r, tree type,
3103                                         const irange &lhs,
3104                                         const irange &op2,
3105                                         relation_kind rel ATTRIBUTE_UNUSED) const
3106 {
3107   // If this is really a logical wi_fold, call that.
3108   if (types_compatible_p (type, boolean_type_node))
3109     return op_logical_or.op1_range (r, type, lhs, op2);
3110 
3111   if (lhs.zero_p ())
3112     {
3113       tree zero = build_zero_cst (type);
3114       r = int_range<1> (zero, zero);
3115       return true;
3116     }
3117   r.set_varying (type);
3118   return true;
3119 }
3120 
3121 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const3122 operator_bitwise_or::op2_range (irange &r, tree type,
3123                                         const irange &lhs,
3124                                         const irange &op1,
3125                                         relation_kind rel ATTRIBUTE_UNUSED) const
3126 {
3127   return operator_bitwise_or::op1_range (r, type, lhs, op1);
3128 }
3129 
3130 
3131 class operator_bitwise_xor : public range_operator
3132 {
3133 public:
3134   virtual void wi_fold (irange &r, tree type,
3135                             const wide_int &lh_lb,
3136                             const wide_int &lh_ub,
3137                             const wide_int &rh_lb,
3138                             const wide_int &rh_ub) const;
3139   virtual bool op1_range (irange &r, tree type,
3140                                 const irange &lhs,
3141                                 const irange &op2,
3142                                 relation_kind rel = VREL_NONE) const;
3143   virtual bool op2_range (irange &r, tree type,
3144                                 const irange &lhs,
3145                                 const irange &op1,
3146                                 relation_kind rel = VREL_NONE) const;
3147   virtual bool op1_op2_relation_effect (irange &lhs_range,
3148                                                   tree type,
3149                                                   const irange &op1_range,
3150                                                   const irange &op2_range,
3151                                                   relation_kind rel) const;
3152 } op_bitwise_xor;
3153 
3154 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const3155 operator_bitwise_xor::wi_fold (irange &r, tree type,
3156                                      const wide_int &lh_lb,
3157                                      const wide_int &lh_ub,
3158                                      const wide_int &rh_lb,
3159                                      const wide_int &rh_ub) const
3160 {
3161   signop sign = TYPE_SIGN (type);
3162   wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3163   wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3164   wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3165                                   maybe_nonzero_lh, mustbe_nonzero_lh);
3166   wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3167                                   maybe_nonzero_rh, mustbe_nonzero_rh);
3168 
3169   wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3170                                      | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3171   wide_int result_one_bits
3172     = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3173        | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3174   wide_int new_ub = ~result_zero_bits;
3175   wide_int new_lb = result_one_bits;
3176 
3177   // If the range has all positive or all negative values, the result
3178   // is better than VARYING.
3179   if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3180     value_range_with_overflow (r, type, new_lb, new_ub);
3181   else
3182     r.set_varying (type);
3183 }
3184 
3185 bool
op1_op2_relation_effect(irange & lhs_range,tree type,const irange &,const irange &,relation_kind rel) const3186 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3187                                                          tree type,
3188                                                          const irange &,
3189                                                          const irange &,
3190                                                          relation_kind rel) const
3191 {
3192   if (rel == VREL_NONE)
3193     return false;
3194 
3195   int_range<2> rel_range;
3196 
3197   switch (rel)
3198     {
3199     case EQ_EXPR:
3200       rel_range.set_zero (type);
3201       break;
3202     case NE_EXPR:
3203       rel_range.set_nonzero (type);
3204       break;
3205     default:
3206       return false;
3207     }
3208 
3209   lhs_range.intersect (rel_range);
3210   return true;
3211 }
3212 
3213 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3214 operator_bitwise_xor::op1_range (irange &r, tree type,
3215                                          const irange &lhs,
3216                                          const irange &op2,
3217                                          relation_kind rel ATTRIBUTE_UNUSED) const
3218 {
3219   if (lhs.undefined_p () || lhs.varying_p ())
3220     {
3221       r = lhs;
3222       return true;
3223     }
3224   if (types_compatible_p (type, boolean_type_node))
3225     {
3226       switch (get_bool_state (r, lhs, type))
3227           {
3228           case BRS_TRUE:
3229             if (op2.varying_p ())
3230               r.set_varying (type);
3231             else if (op2.zero_p ())
3232               r = range_true (type);
3233             // See get_bool_state for the rationale
3234             else if (op2.contains_p (build_zero_cst (op2.type ())))
3235               r = range_true_and_false (type);
3236             else
3237               r = range_false (type);
3238             break;
3239           case BRS_FALSE:
3240             r = op2;
3241             break;
3242           default:
3243             break;
3244           }
3245       return true;
3246     }
3247   r.set_varying (type);
3248   return true;
3249 }
3250 
3251 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const3252 operator_bitwise_xor::op2_range (irange &r, tree type,
3253                                          const irange &lhs,
3254                                          const irange &op1,
3255                                          relation_kind rel ATTRIBUTE_UNUSED) const
3256 {
3257   return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3258 }
3259 
3260 class operator_trunc_mod : public range_operator
3261 {
3262 public:
3263   virtual void wi_fold (irange &r, tree type,
3264                             const wide_int &lh_lb,
3265                             const wide_int &lh_ub,
3266                             const wide_int &rh_lb,
3267                             const wide_int &rh_ub) const;
3268   virtual bool op1_range (irange &r, tree type,
3269                                 const irange &lhs,
3270                                 const irange &op2,
3271                                 relation_kind rel ATTRIBUTE_UNUSED) const;
3272   virtual bool op2_range (irange &r, tree type,
3273                                 const irange &lhs,
3274                                 const irange &op1,
3275                                 relation_kind rel ATTRIBUTE_UNUSED) const;
3276 } op_trunc_mod;
3277 
3278 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const3279 operator_trunc_mod::wi_fold (irange &r, tree type,
3280                                    const wide_int &lh_lb,
3281                                    const wide_int &lh_ub,
3282                                    const wide_int &rh_lb,
3283                                    const wide_int &rh_ub) const
3284 {
3285   wide_int new_lb, new_ub, tmp;
3286   signop sign = TYPE_SIGN (type);
3287   unsigned prec = TYPE_PRECISION (type);
3288 
3289   // Mod 0 is undefined.
3290   if (wi_zero_p (type, rh_lb, rh_ub))
3291     {
3292       r.set_undefined ();
3293       return;
3294     }
3295 
3296   // Check for constant and try to fold.
3297   if (lh_lb == lh_ub && rh_lb == rh_ub)
3298     {
3299       wi::overflow_type ov = wi::OVF_NONE;
3300       tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3301       if (ov == wi::OVF_NONE)
3302           {
3303             r = int_range<2> (type, tmp, tmp);
3304             return;
3305           }
3306     }
3307 
3308   // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3309   new_ub = rh_ub - 1;
3310   if (sign == SIGNED)
3311     {
3312       tmp = -1 - rh_lb;
3313       new_ub = wi::smax (new_ub, tmp);
3314     }
3315 
3316   if (sign == UNSIGNED)
3317     new_lb = wi::zero (prec);
3318   else
3319     {
3320       new_lb = -new_ub;
3321       tmp = lh_lb;
3322       if (wi::gts_p (tmp, 0))
3323           tmp = wi::zero (prec);
3324       new_lb = wi::smax (new_lb, tmp);
3325     }
3326   tmp = lh_ub;
3327   if (sign == SIGNED && wi::neg_p (tmp))
3328     tmp = wi::zero (prec);
3329   new_ub = wi::min (new_ub, tmp, sign);
3330 
3331   value_range_with_overflow (r, type, new_lb, new_ub);
3332 }
3333 
3334 bool
op1_range(irange & r,tree type,const irange & lhs,const irange &,relation_kind rel ATTRIBUTE_UNUSED) const3335 operator_trunc_mod::op1_range (irange &r, tree type,
3336                                      const irange &lhs,
3337                                      const irange &,
3338                                      relation_kind rel ATTRIBUTE_UNUSED) const
3339 {
3340   // PR 91029.
3341   signop sign = TYPE_SIGN (type);
3342   unsigned prec = TYPE_PRECISION (type);
3343   // (a % b) >= x && x > 0 , then a >= x.
3344   if (wi::gt_p (lhs.lower_bound (), 0, sign))
3345     {
3346       r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3347       return true;
3348     }
3349   // (a % b) <= x && x < 0 , then a <= x.
3350   if (wi::lt_p (lhs.upper_bound (), 0, sign))
3351     {
3352       r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3353       return true;
3354     }
3355   return false;
3356 }
3357 
3358 bool
op2_range(irange & r,tree type,const irange & lhs,const irange &,relation_kind rel ATTRIBUTE_UNUSED) const3359 operator_trunc_mod::op2_range (irange &r, tree type,
3360                                      const irange &lhs,
3361                                      const irange &,
3362                                      relation_kind rel ATTRIBUTE_UNUSED) const
3363 {
3364   // PR 91029.
3365   signop sign = TYPE_SIGN (type);
3366   unsigned prec = TYPE_PRECISION (type);
3367   // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3368   //                                 or b > x for unsigned.
3369   if (wi::gt_p (lhs.lower_bound (), 0, sign))
3370     {
3371       if (sign == SIGNED)
3372           r = value_range (type, wi::neg (lhs.lower_bound ()),
3373                                lhs.lower_bound (), VR_ANTI_RANGE);
3374       else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3375                                sign))
3376           r = value_range (type, lhs.lower_bound () + 1,
3377                                wi::max_value (prec, sign));
3378       else
3379           return false;
3380       return true;
3381     }
3382   // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3383   if (wi::lt_p (lhs.upper_bound (), 0, sign))
3384     {
3385       if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3386           r = value_range (type, lhs.upper_bound (),
3387                                wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3388       else
3389           return false;
3390       return true;
3391     }
3392   return false;
3393 }
3394 
3395 
3396 class operator_logical_not : public range_operator
3397 {
3398 public:
3399   virtual bool fold_range (irange &r, tree type,
3400                                  const irange &lh,
3401                                  const irange &rh,
3402                                  relation_kind rel = VREL_NONE) const;
3403   virtual bool op1_range (irange &r, tree type,
3404                                 const irange &lhs,
3405                                 const irange &op2,
3406                                 relation_kind rel = VREL_NONE) const;
3407 } op_logical_not;
3408 
3409 // Folding a logical NOT, oddly enough, involves doing nothing on the
3410 // forward pass through.  During the initial walk backwards, the
3411 // logical NOT reversed the desired outcome on the way back, so on the
3412 // way forward all we do is pass the range forward.
3413 //
3414 //        b_2 = x_1 < 20
3415 //        b_3 = !b_2
3416 //        if (b_3)
3417 //  to determine the TRUE branch, walking  backward
3418 //       if (b_3)             if ([1,1])
3419 //       b_3 = !b_2           [1,1] = ![0,0]
3420 //         b_2 = x_1 < 20               [0,0] = x_1 < 20,   false, so x_1 == [20, 255]
3421 //   which is the result we are looking for.. so.. pass it through.
3422 
3423 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3424 operator_logical_not::fold_range (irange &r, tree type,
3425                                           const irange &lh,
3426                                           const irange &rh ATTRIBUTE_UNUSED,
3427                                           relation_kind rel ATTRIBUTE_UNUSED) const
3428 {
3429   if (empty_range_varying (r, type, lh, rh))
3430     return true;
3431 
3432   r = lh;
3433   if (!lh.varying_p () && !lh.undefined_p ())
3434     r.invert ();
3435 
3436   return true;
3437 }
3438 
3439 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3440 operator_logical_not::op1_range (irange &r,
3441                                          tree type,
3442                                          const irange &lhs,
3443                                          const irange &op2,
3444                                          relation_kind rel ATTRIBUTE_UNUSED) const
3445 {
3446   // Logical NOT is involutary...do it again.
3447   return fold_range (r, type, lhs, op2);
3448 }
3449 
3450 
3451 class operator_bitwise_not : public range_operator
3452 {
3453 public:
3454   virtual bool fold_range (irange &r, tree type,
3455                                  const irange &lh,
3456                                  const irange &rh,
3457                                  relation_kind rel = VREL_NONE) const;
3458   virtual bool op1_range (irange &r, tree type,
3459                                 const irange &lhs,
3460                                 const irange &op2,
3461                                 relation_kind rel = VREL_NONE) const;
3462 } op_bitwise_not;
3463 
3464 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh,relation_kind rel ATTRIBUTE_UNUSED) const3465 operator_bitwise_not::fold_range (irange &r, tree type,
3466                                           const irange &lh,
3467                                           const irange &rh,
3468                                           relation_kind rel ATTRIBUTE_UNUSED) const
3469 {
3470   if (empty_range_varying (r, type, lh, rh))
3471     return true;
3472 
3473   if (types_compatible_p (type, boolean_type_node))
3474     return op_logical_not.fold_range (r, type, lh, rh);
3475 
3476   // ~X is simply -1 - X.
3477   int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3478                                wi::minus_one (TYPE_PRECISION (type)));
3479   return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, minusone,
3480                                                                         lh);
3481 }
3482 
3483 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3484 operator_bitwise_not::op1_range (irange &r, tree type,
3485                                          const irange &lhs,
3486                                          const irange &op2,
3487                                          relation_kind rel ATTRIBUTE_UNUSED) const
3488 {
3489   if (types_compatible_p (type, boolean_type_node))
3490     return op_logical_not.op1_range (r, type, lhs, op2);
3491 
3492   // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3493   return fold_range (r, type, lhs, op2);
3494 }
3495 
3496 
3497 class operator_cst : public range_operator
3498 {
3499 public:
3500   virtual bool fold_range (irange &r, tree type,
3501                                  const irange &op1,
3502                                  const irange &op2,
3503                                  relation_kind rel = VREL_NONE) const;
3504 } op_integer_cst;
3505 
3506 bool
fold_range(irange & r,tree type ATTRIBUTE_UNUSED,const irange & lh,const irange & rh ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3507 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3508                                 const irange &lh,
3509                                 const irange &rh ATTRIBUTE_UNUSED,
3510                                 relation_kind rel ATTRIBUTE_UNUSED) const
3511 {
3512   r = lh;
3513   return true;
3514 }
3515 
3516 
3517 class operator_identity : public range_operator
3518 {
3519 public:
3520   virtual bool fold_range (irange &r, tree type,
3521                                  const irange &op1,
3522                                  const irange &op2,
3523                                  relation_kind rel = VREL_NONE) const;
3524   virtual bool op1_range (irange &r, tree type,
3525                                 const irange &lhs,
3526                                 const irange &op2,
3527                                 relation_kind rel = VREL_NONE) const;
3528   virtual enum tree_code lhs_op1_relation (const irange &lhs,
3529                                                      const irange &op1,
3530                                                      const irange &op2) const;
3531 } op_identity;
3532 
3533 // Determine if there is a relationship between LHS and OP1.
3534 
3535 enum tree_code
lhs_op1_relation(const irange & lhs,const irange & op1 ATTRIBUTE_UNUSED,const irange & op2 ATTRIBUTE_UNUSED) const3536 operator_identity::lhs_op1_relation (const irange &lhs,
3537                                              const irange &op1 ATTRIBUTE_UNUSED,
3538                                              const irange &op2 ATTRIBUTE_UNUSED) const
3539 {
3540   if (lhs.undefined_p ())
3541     return VREL_NONE;
3542   // Simply a copy, so they are equivalent.
3543   return EQ_EXPR;
3544 }
3545 
3546 bool
fold_range(irange & r,tree type ATTRIBUTE_UNUSED,const irange & lh,const irange & rh ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3547 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3548                                      const irange &lh,
3549                                      const irange &rh ATTRIBUTE_UNUSED,
3550                                      relation_kind rel ATTRIBUTE_UNUSED) const
3551 {
3552   r = lh;
3553   return true;
3554 }
3555 
3556 bool
op1_range(irange & r,tree type ATTRIBUTE_UNUSED,const irange & lhs,const irange & op2 ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3557 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3558                                     const irange &lhs,
3559                                     const irange &op2 ATTRIBUTE_UNUSED,
3560                                     relation_kind rel ATTRIBUTE_UNUSED) const
3561 {
3562   r = lhs;
3563   return true;
3564 }
3565 
3566 
3567 class operator_unknown : public range_operator
3568 {
3569 public:
3570   virtual bool fold_range (irange &r, tree type,
3571                                  const irange &op1,
3572                                  const irange &op2,
3573                                  relation_kind rel = VREL_NONE) const;
3574 } op_unknown;
3575 
3576 bool
fold_range(irange & r,tree type,const irange & lh ATTRIBUTE_UNUSED,const irange & rh ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3577 operator_unknown::fold_range (irange &r, tree type,
3578                                     const irange &lh ATTRIBUTE_UNUSED,
3579                                     const irange &rh ATTRIBUTE_UNUSED,
3580                                     relation_kind rel ATTRIBUTE_UNUSED) const
3581 {
3582   r.set_varying (type);
3583   return true;
3584 }
3585 
3586 
3587 class operator_abs : public range_operator
3588 {
3589  public:
3590   virtual void wi_fold (irange &r, tree type,
3591                             const wide_int &lh_lb,
3592                             const wide_int &lh_ub,
3593                             const wide_int &rh_lb,
3594                             const wide_int &rh_ub) const;
3595   virtual bool op1_range (irange &r, tree type,
3596                                 const irange &lhs,
3597                                 const irange &op2,
3598                                 relation_kind rel ATTRIBUTE_UNUSED) const;
3599 } op_abs;
3600 
3601 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb ATTRIBUTE_UNUSED,const wide_int & rh_ub ATTRIBUTE_UNUSED) const3602 operator_abs::wi_fold (irange &r, tree type,
3603                            const wide_int &lh_lb, const wide_int &lh_ub,
3604                            const wide_int &rh_lb ATTRIBUTE_UNUSED,
3605                            const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3606 {
3607   wide_int min, max;
3608   signop sign = TYPE_SIGN (type);
3609   unsigned prec = TYPE_PRECISION (type);
3610 
3611   // Pass through LH for the easy cases.
3612   if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3613     {
3614       r = int_range<1> (type, lh_lb, lh_ub);
3615       return;
3616     }
3617 
3618   // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3619   // a useful range.
3620   wide_int min_value = wi::min_value (prec, sign);
3621   wide_int max_value = wi::max_value (prec, sign);
3622   if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3623     {
3624       r.set_varying (type);
3625       return;
3626     }
3627 
3628   // ABS_EXPR may flip the range around, if the original range
3629   // included negative values.
3630   if (wi::eq_p (lh_lb, min_value))
3631     {
3632       // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3633       // returned [-MIN,-MIN] so this preserves that behaviour.  PR37078
3634       if (wi::eq_p (lh_ub, min_value))
3635           {
3636             r = int_range<1> (type, min_value, min_value);
3637             return;
3638           }
3639       min = max_value;
3640     }
3641   else
3642     min = wi::abs (lh_lb);
3643 
3644   if (wi::eq_p (lh_ub, min_value))
3645     max = max_value;
3646   else
3647     max = wi::abs (lh_ub);
3648 
3649   // If the range contains zero then we know that the minimum value in the
3650   // range will be zero.
3651   if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3652     {
3653       if (wi::gt_p (min, max, sign))
3654           max = min;
3655       min = wi::zero (prec);
3656     }
3657   else
3658     {
3659       // If the range was reversed, swap MIN and MAX.
3660       if (wi::gt_p (min, max, sign))
3661           std::swap (min, max);
3662     }
3663 
3664   // If the new range has its limits swapped around (MIN > MAX), then
3665   // the operation caused one of them to wrap around.  The only thing
3666   // we know is that the result is positive.
3667   if (wi::gt_p (min, max, sign))
3668     {
3669       min = wi::zero (prec);
3670       max = max_value;
3671     }
3672   r = int_range<1> (type, min, max);
3673 }
3674 
3675 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3676 operator_abs::op1_range (irange &r, tree type,
3677                                const irange &lhs,
3678                                const irange &op2,
3679                                relation_kind rel ATTRIBUTE_UNUSED) const
3680 {
3681   if (empty_range_varying (r, type, lhs, op2))
3682     return true;
3683   if (TYPE_UNSIGNED (type))
3684     {
3685       r = lhs;
3686       return true;
3687     }
3688   // Start with the positives because negatives are an impossible result.
3689   int_range_max positives = range_positives (type);
3690   positives.intersect (lhs);
3691   r = positives;
3692   // Then add the negative of each pair:
3693   // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3694   for (unsigned i = 0; i < positives.num_pairs (); ++i)
3695     r.union_ (int_range<1> (type,
3696                                   -positives.upper_bound (i),
3697                                   -positives.lower_bound (i)));
3698   // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3699   // unrepresentable.  Add -TYPE_MIN_VALUE in this case.
3700   wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3701   wide_int lb = lhs.lower_bound ();
3702   if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3703     r.union_ (int_range<2> (type, lb, lb));
3704   return true;
3705 }
3706 
3707 
3708 class operator_absu : public range_operator
3709 {
3710  public:
3711   virtual void wi_fold (irange &r, tree type,
3712                               const wide_int &lh_lb, const wide_int &lh_ub,
3713                               const wide_int &rh_lb, const wide_int &rh_ub) const;
3714 } op_absu;
3715 
3716 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb ATTRIBUTE_UNUSED,const wide_int & rh_ub ATTRIBUTE_UNUSED) const3717 operator_absu::wi_fold (irange &r, tree type,
3718                               const wide_int &lh_lb, const wide_int &lh_ub,
3719                               const wide_int &rh_lb ATTRIBUTE_UNUSED,
3720                               const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3721 {
3722   wide_int new_lb, new_ub;
3723 
3724   // Pass through VR0 the easy cases.
3725   if (wi::ges_p (lh_lb, 0))
3726     {
3727       new_lb = lh_lb;
3728       new_ub = lh_ub;
3729     }
3730   else
3731     {
3732       new_lb = wi::abs (lh_lb);
3733       new_ub = wi::abs (lh_ub);
3734 
3735       // If the range contains zero then we know that the minimum
3736       // value in the range will be zero.
3737       if (wi::ges_p (lh_ub, 0))
3738           {
3739             if (wi::gtu_p (new_lb, new_ub))
3740               new_ub = new_lb;
3741             new_lb = wi::zero (TYPE_PRECISION (type));
3742           }
3743       else
3744           std::swap (new_lb, new_ub);
3745     }
3746 
3747   gcc_checking_assert (TYPE_UNSIGNED (type));
3748   r = int_range<1> (type, new_lb, new_ub);
3749 }
3750 
3751 
3752 class operator_negate : public range_operator
3753 {
3754  public:
3755   virtual bool fold_range (irange &r, tree type,
3756                                  const irange &op1,
3757                                  const irange &op2,
3758                                  relation_kind rel = VREL_NONE) const;
3759   virtual bool op1_range (irange &r, tree type,
3760                                 const irange &lhs,
3761                                 const irange &op2,
3762                                 relation_kind rel = VREL_NONE) const;
3763 } op_negate;
3764 
3765 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh,relation_kind rel ATTRIBUTE_UNUSED) const3766 operator_negate::fold_range (irange &r, tree type,
3767                                    const irange &lh,
3768                                    const irange &rh,
3769                                    relation_kind rel ATTRIBUTE_UNUSED) const
3770 {
3771   if (empty_range_varying (r, type, lh, rh))
3772     return true;
3773   // -X is simply 0 - X.
3774   return range_op_handler (MINUS_EXPR, type)->fold_range (r, type,
3775                                                                         range_zero (type),
3776                                                                         lh);
3777 }
3778 
3779 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3780 operator_negate::op1_range (irange &r, tree type,
3781                                   const irange &lhs,
3782                                   const irange &op2,
3783                                   relation_kind rel ATTRIBUTE_UNUSED) const
3784 {
3785   // NEGATE is involutory.
3786   return fold_range (r, type, lhs, op2);
3787 }
3788 
3789 
3790 class operator_addr_expr : public range_operator
3791 {
3792 public:
3793   virtual bool fold_range (irange &r, tree type,
3794                                  const irange &op1,
3795                                  const irange &op2,
3796                                  relation_kind rel = VREL_NONE) const;
3797   virtual bool op1_range (irange &r, tree type,
3798                                 const irange &lhs,
3799                                 const irange &op2,
3800                                 relation_kind rel = VREL_NONE) const;
3801 } op_addr;
3802 
3803 bool
fold_range(irange & r,tree type,const irange & lh,const irange & rh,relation_kind rel ATTRIBUTE_UNUSED) const3804 operator_addr_expr::fold_range (irange &r, tree type,
3805                                         const irange &lh,
3806                                         const irange &rh,
3807                                         relation_kind rel ATTRIBUTE_UNUSED) const
3808 {
3809   if (empty_range_varying (r, type, lh, rh))
3810     return true;
3811 
3812   // Return a non-null pointer of the LHS type (passed in op2).
3813   if (lh.zero_p ())
3814     r = range_zero (type);
3815   else if (!lh.contains_p (build_zero_cst (lh.type ())))
3816     r = range_nonzero (type);
3817   else
3818     r.set_varying (type);
3819   return true;
3820 }
3821 
3822 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2,relation_kind rel ATTRIBUTE_UNUSED) const3823 operator_addr_expr::op1_range (irange &r, tree type,
3824                                      const irange &lhs,
3825                                      const irange &op2,
3826                                      relation_kind rel ATTRIBUTE_UNUSED) const
3827 {
3828    if (empty_range_varying (r, type, lhs, op2))
3829     return true;
3830 
3831   // Return a non-null pointer of the LHS type (passed in op2), but only
3832   // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
3833   // See PR 111009.
3834   if (!contains_zero_p (lhs) && TYPE_OVERFLOW_UNDEFINED (type))
3835     r = range_nonzero (type);
3836   else
3837     r.set_varying (type);
3838   return true;
3839 }
3840 
3841 
3842 class pointer_plus_operator : public range_operator
3843 {
3844 public:
3845   virtual void wi_fold (irange &r, tree type,
3846                             const wide_int &lh_lb,
3847                             const wide_int &lh_ub,
3848                             const wide_int &rh_lb,
3849                             const wide_int &rh_ub) const;
3850 } op_pointer_plus;
3851 
3852 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const3853 pointer_plus_operator::wi_fold (irange &r, tree type,
3854                                         const wide_int &lh_lb,
3855                                         const wide_int &lh_ub,
3856                                         const wide_int &rh_lb,
3857                                         const wide_int &rh_ub) const
3858 {
3859   // Check for [0,0] + const, and simply return the const.
3860   if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3861     {
3862       tree val = wide_int_to_tree (type, rh_lb);
3863       r.set (val, val);
3864       return;
3865     }
3866 
3867   // For pointer types, we are really only interested in asserting
3868   // whether the expression evaluates to non-NULL.
3869   //
3870   // With -fno-delete-null-pointer-checks we need to be more
3871   // conservative.  As some object might reside at address 0,
3872   // then some offset could be added to it and the same offset
3873   // subtracted again and the result would be NULL.
3874   // E.g.
3875   // static int a[12]; where &a[0] is NULL and
3876   // ptr = &a[6];
3877   // ptr -= 6;
3878   // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3879   // where the first range doesn't include zero and the second one
3880   // doesn't either.  As the second operand is sizetype (unsigned),
3881   // consider all ranges where the MSB could be set as possible
3882   // subtractions where the result might be NULL.
3883   if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3884        || !wi_includes_zero_p (type, rh_lb, rh_ub))
3885       && !TYPE_OVERFLOW_WRAPS (type)
3886       && (flag_delete_null_pointer_checks
3887             || !wi::sign_mask (rh_ub)))
3888     r = range_nonzero (type);
3889   else if (lh_lb == lh_ub && lh_lb == 0
3890              && rh_lb == rh_ub && rh_lb == 0)
3891     r = range_zero (type);
3892   else
3893    r.set_varying (type);
3894 }
3895 
3896 
3897 class pointer_min_max_operator : public range_operator
3898 {
3899 public:
3900   virtual void wi_fold (irange & r, tree type,
3901                               const wide_int &lh_lb, const wide_int &lh_ub,
3902                               const wide_int &rh_lb, const wide_int &rh_ub) const;
3903 } op_ptr_min_max;
3904 
3905 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const3906 pointer_min_max_operator::wi_fold (irange &r, tree type,
3907                                            const wide_int &lh_lb,
3908                                            const wide_int &lh_ub,
3909                                            const wide_int &rh_lb,
3910                                            const wide_int &rh_ub) const
3911 {
3912   // For MIN/MAX expressions with pointers, we only care about
3913   // nullness.  If both are non null, then the result is nonnull.
3914   // If both are null, then the result is null.  Otherwise they
3915   // are varying.
3916   if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3917       && !wi_includes_zero_p (type, rh_lb, rh_ub))
3918     r = range_nonzero (type);
3919   else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3920     r = range_zero (type);
3921   else
3922     r.set_varying (type);
3923 }
3924 
3925 
3926 class pointer_and_operator : public range_operator
3927 {
3928 public:
3929   virtual void wi_fold (irange &r, tree type,
3930                               const wide_int &lh_lb, const wide_int &lh_ub,
3931                               const wide_int &rh_lb, const wide_int &rh_ub) const;
3932 } op_pointer_and;
3933 
3934 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb ATTRIBUTE_UNUSED,const wide_int & rh_ub ATTRIBUTE_UNUSED) const3935 pointer_and_operator::wi_fold (irange &r, tree type,
3936                                      const wide_int &lh_lb,
3937                                      const wide_int &lh_ub,
3938                                      const wide_int &rh_lb ATTRIBUTE_UNUSED,
3939                                      const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3940 {
3941   // For pointer types, we are really only interested in asserting
3942   // whether the expression evaluates to non-NULL.
3943   if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
3944     r = range_zero (type);
3945   else
3946     r.set_varying (type);
3947 }
3948 
3949 
3950 class pointer_or_operator : public range_operator
3951 {
3952 public:
3953   virtual bool op1_range (irange &r, tree type,
3954                                 const irange &lhs,
3955                                 const irange &op2,
3956                                 relation_kind rel = VREL_NONE) const;
3957   virtual bool op2_range (irange &r, tree type,
3958                                 const irange &lhs,
3959                                 const irange &op1,
3960                                 relation_kind rel = VREL_NONE) const;
3961   virtual void wi_fold (irange &r, tree type,
3962                               const wide_int &lh_lb, const wide_int &lh_ub,
3963                               const wide_int &rh_lb, const wide_int &rh_ub) const;
3964 } op_pointer_or;
3965 
3966 bool
op1_range(irange & r,tree type,const irange & lhs,const irange & op2 ATTRIBUTE_UNUSED,relation_kind rel ATTRIBUTE_UNUSED) const3967 pointer_or_operator::op1_range (irange &r, tree type,
3968                                         const irange &lhs,
3969                                         const irange &op2 ATTRIBUTE_UNUSED,
3970                                         relation_kind rel ATTRIBUTE_UNUSED) const
3971 {
3972   if (lhs.zero_p ())
3973     {
3974       tree zero = build_zero_cst (type);
3975       r = int_range<1> (zero, zero);
3976       return true;
3977     }
3978   r.set_varying (type);
3979   return true;
3980 }
3981 
3982 bool
op2_range(irange & r,tree type,const irange & lhs,const irange & op1,relation_kind rel ATTRIBUTE_UNUSED) const3983 pointer_or_operator::op2_range (irange &r, tree type,
3984                                         const irange &lhs,
3985                                         const irange &op1,
3986                                         relation_kind rel ATTRIBUTE_UNUSED) const
3987 {
3988   return pointer_or_operator::op1_range (r, type, lhs, op1);
3989 }
3990 
3991 void
wi_fold(irange & r,tree type,const wide_int & lh_lb,const wide_int & lh_ub,const wide_int & rh_lb,const wide_int & rh_ub) const3992 pointer_or_operator::wi_fold (irange &r, tree type,
3993                                     const wide_int &lh_lb,
3994                                     const wide_int &lh_ub,
3995                                     const wide_int &rh_lb,
3996                                     const wide_int &rh_ub) const
3997 {
3998   // For pointer types, we are really only interested in asserting
3999   // whether the expression evaluates to non-NULL.
4000   if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4001       && !wi_includes_zero_p (type, rh_lb, rh_ub))
4002     r = range_nonzero (type);
4003   else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4004     r = range_zero (type);
4005   else
4006     r.set_varying (type);
4007 }
4008 
4009 // This implements the range operator tables as local objects in this file.
4010 
4011 class range_op_table
4012 {
4013 public:
4014   inline range_operator *operator[] (enum tree_code code);
4015 protected:
4016   void set (enum tree_code code, range_operator &op);
4017 private:
4018   range_operator *m_range_tree[MAX_TREE_CODES];
4019 };
4020 
4021 // Return a pointer to the range_operator instance, if there is one
4022 // associated with tree_code CODE.
4023 
4024 range_operator *
operator [](enum tree_code code)4025 range_op_table::operator[] (enum tree_code code)
4026 {
4027   gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4028   return m_range_tree[code];
4029 }
4030 
4031 // Add OP to the handler table for CODE.
4032 
4033 void
set(enum tree_code code,range_operator & op)4034 range_op_table::set (enum tree_code code, range_operator &op)
4035 {
4036   gcc_checking_assert (m_range_tree[code] == NULL);
4037   m_range_tree[code] = &op;
4038 }
4039 
4040 // Instantiate a range op table for integral operations.
4041 
4042 class integral_table : public range_op_table
4043 {
4044 public:
4045   integral_table ();
4046 } integral_tree_table;
4047 
integral_table()4048 integral_table::integral_table ()
4049 {
4050   set (EQ_EXPR, op_equal);
4051   set (NE_EXPR, op_not_equal);
4052   set (LT_EXPR, op_lt);
4053   set (LE_EXPR, op_le);
4054   set (GT_EXPR, op_gt);
4055   set (GE_EXPR, op_ge);
4056   set (PLUS_EXPR, op_plus);
4057   set (MINUS_EXPR, op_minus);
4058   set (MIN_EXPR, op_min);
4059   set (MAX_EXPR, op_max);
4060   set (MULT_EXPR, op_mult);
4061   set (TRUNC_DIV_EXPR, op_trunc_div);
4062   set (FLOOR_DIV_EXPR, op_floor_div);
4063   set (ROUND_DIV_EXPR, op_round_div);
4064   set (CEIL_DIV_EXPR, op_ceil_div);
4065   set (EXACT_DIV_EXPR, op_exact_div);
4066   set (LSHIFT_EXPR, op_lshift);
4067   set (RSHIFT_EXPR, op_rshift);
4068   set (NOP_EXPR, op_convert);
4069   set (CONVERT_EXPR, op_convert);
4070   set (TRUTH_AND_EXPR, op_logical_and);
4071   set (BIT_AND_EXPR, op_bitwise_and);
4072   set (TRUTH_OR_EXPR, op_logical_or);
4073   set (BIT_IOR_EXPR, op_bitwise_or);
4074   set (BIT_XOR_EXPR, op_bitwise_xor);
4075   set (TRUNC_MOD_EXPR, op_trunc_mod);
4076   set (TRUTH_NOT_EXPR, op_logical_not);
4077   set (BIT_NOT_EXPR, op_bitwise_not);
4078   set (INTEGER_CST, op_integer_cst);
4079   set (SSA_NAME, op_identity);
4080   set (PAREN_EXPR, op_identity);
4081   set (OBJ_TYPE_REF, op_identity);
4082   set (IMAGPART_EXPR, op_unknown);
4083   set (REALPART_EXPR, op_unknown);
4084   set (POINTER_DIFF_EXPR, op_pointer_diff);
4085   set (ABS_EXPR, op_abs);
4086   set (ABSU_EXPR, op_absu);
4087   set (NEGATE_EXPR, op_negate);
4088   set (ADDR_EXPR, op_addr);
4089 }
4090 
4091 // Instantiate a range op table for pointer operations.
4092 
4093 class pointer_table : public range_op_table
4094 {
4095 public:
4096   pointer_table ();
4097 } pointer_tree_table;
4098 
pointer_table()4099 pointer_table::pointer_table ()
4100 {
4101   set (BIT_AND_EXPR, op_pointer_and);
4102   set (BIT_IOR_EXPR, op_pointer_or);
4103   set (MIN_EXPR, op_ptr_min_max);
4104   set (MAX_EXPR, op_ptr_min_max);
4105   set (POINTER_PLUS_EXPR, op_pointer_plus);
4106 
4107   set (EQ_EXPR, op_equal);
4108   set (NE_EXPR, op_not_equal);
4109   set (LT_EXPR, op_lt);
4110   set (LE_EXPR, op_le);
4111   set (GT_EXPR, op_gt);
4112   set (GE_EXPR, op_ge);
4113   set (SSA_NAME, op_identity);
4114   set (INTEGER_CST, op_integer_cst);
4115   set (ADDR_EXPR, op_addr);
4116   set (NOP_EXPR, op_convert);
4117   set (CONVERT_EXPR, op_convert);
4118 
4119   set (BIT_NOT_EXPR, op_bitwise_not);
4120   set (BIT_XOR_EXPR, op_bitwise_xor);
4121 }
4122 
4123 // The tables are hidden and accessed via a simple extern function.
4124 
4125 range_operator *
range_op_handler(enum tree_code code,tree type)4126 range_op_handler (enum tree_code code, tree type)
4127 {
4128   // First check if there is a pointer specialization.
4129   if (POINTER_TYPE_P (type))
4130     return pointer_tree_table[code];
4131   if (INTEGRAL_TYPE_P (type))
4132     return integral_tree_table[code];
4133   return NULL;
4134 }
4135 
4136 // Cast the range in R to TYPE.
4137 
4138 void
range_cast(irange & r,tree type)4139 range_cast (irange &r, tree type)
4140 {
4141   int_range_max tmp = r;
4142   range_operator *op = range_op_handler (CONVERT_EXPR, type);
4143   // Call op_convert, if it fails, the result is varying.
4144   if (!op->fold_range (r, type, tmp, int_range<1> (type)))
4145     r.set_varying (type);
4146 }
4147 
4148 #if CHECKING_P
4149 #include "selftest.h"
4150 
4151 namespace selftest
4152 {
4153 #define INT(N) build_int_cst (integer_type_node, (N))
4154 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4155 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4156 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4157 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4158 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4159 
4160 static void
range_op_cast_tests()4161 range_op_cast_tests ()
4162 {
4163   int_range<1> r0, r1, r2, rold;
4164   r0.set_varying (integer_type_node);
4165   tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4166 
4167   // If a range is in any way outside of the range for the converted
4168   // to range, default to the range for the new type.
4169   r0.set_varying (short_integer_type_node);
4170   tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4171   tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4172   if (TYPE_PRECISION (TREE_TYPE (maxint))
4173       > TYPE_PRECISION (short_integer_type_node))
4174     {
4175       r1 = int_range<1> (integer_zero_node, maxint);
4176       range_cast (r1, short_integer_type_node);
4177       ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4178                        && r1.upper_bound() == wi::to_wide (maxshort));
4179     }
4180 
4181   // (unsigned char)[-5,-1] => [251,255].
4182   r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4183   range_cast (r0, unsigned_char_type_node);
4184   ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4185   range_cast (r0, signed_char_type_node);
4186   ASSERT_TRUE (r0 == rold);
4187 
4188   // (signed char)[15, 150] => [-128,-106][15,127].
4189   r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4190   range_cast (r0, signed_char_type_node);
4191   r1 = int_range<1> (SCHAR (15), SCHAR (127));
4192   r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4193   r1.union_ (r2);
4194   ASSERT_TRUE (r1 == r0);
4195   range_cast (r0, unsigned_char_type_node);
4196   ASSERT_TRUE (r0 == rold);
4197 
4198   // (unsigned char)[-5, 5] => [0,5][251,255].
4199   r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4200   range_cast (r0, unsigned_char_type_node);
4201   r1 = int_range<1> (UCHAR (251), UCHAR (255));
4202   r2 = int_range<1> (UCHAR (0), UCHAR (5));
4203   r1.union_ (r2);
4204   ASSERT_TRUE (r0 == r1);
4205   range_cast (r0, signed_char_type_node);
4206   ASSERT_TRUE (r0 == rold);
4207 
4208   // (unsigned char)[-5,5] => [0,5][251,255].
4209   r0 = int_range<1> (INT (-5), INT (5));
4210   range_cast (r0, unsigned_char_type_node);
4211   r1 = int_range<1> (UCHAR (0), UCHAR (5));
4212   r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4213   ASSERT_TRUE (r0 == r1);
4214 
4215   // (unsigned char)[5U,1974U] => [0,255].
4216   r0 = int_range<1> (UINT (5), UINT (1974));
4217   range_cast (r0, unsigned_char_type_node);
4218   ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4219   range_cast (r0, integer_type_node);
4220   // Going to a wider range should not sign extend.
4221   ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4222 
4223   // (unsigned char)[-350,15] => [0,255].
4224   r0 = int_range<1> (INT (-350), INT (15));
4225   range_cast (r0, unsigned_char_type_node);
4226   ASSERT_TRUE (r0 == (int_range<1>
4227                           (TYPE_MIN_VALUE (unsigned_char_type_node),
4228                            TYPE_MAX_VALUE (unsigned_char_type_node))));
4229 
4230   // Casting [-120,20] from signed char to unsigned short.
4231   // => [0, 20][0xff88, 0xffff].
4232   r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4233   range_cast (r0, short_unsigned_type_node);
4234   r1 = int_range<1> (UINT16 (0), UINT16 (20));
4235   r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4236   r1.union_ (r2);
4237   ASSERT_TRUE (r0 == r1);
4238   // A truncating cast back to signed char will work because [-120, 20]
4239   // is representable in signed char.
4240   range_cast (r0, signed_char_type_node);
4241   ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4242 
4243   // unsigned char -> signed short
4244   //      (signed short)[(unsigned char)25, (unsigned char)250]
4245   // => [(signed short)25, (signed short)250]
4246   r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4247   range_cast (r0, short_integer_type_node);
4248   r1 = int_range<1> (INT16 (25), INT16 (250));
4249   ASSERT_TRUE (r0 == r1);
4250   range_cast (r0, unsigned_char_type_node);
4251   ASSERT_TRUE (r0 == rold);
4252 
4253   // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4254   r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4255                  TYPE_MAX_VALUE (long_long_integer_type_node));
4256   range_cast (r0, short_unsigned_type_node);
4257   r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4258                  TYPE_MAX_VALUE (short_unsigned_type_node));
4259   ASSERT_TRUE (r0 == r1);
4260 
4261   // Casting NONZERO to a narrower type will wrap/overflow so
4262   // it's just the entire range for the narrower type.
4263   //
4264   // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32].  This is
4265   // is outside of the range of a smaller range, return the full
4266   // smaller range.
4267   if (TYPE_PRECISION (integer_type_node)
4268       > TYPE_PRECISION (short_integer_type_node))
4269     {
4270       r0 = range_nonzero (integer_type_node);
4271       range_cast (r0, short_integer_type_node);
4272       r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4273                                TYPE_MAX_VALUE (short_integer_type_node));
4274       ASSERT_TRUE (r0 == r1);
4275     }
4276 
4277   // Casting NONZERO from a narrower signed to a wider signed.
4278   //
4279   // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4280   // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4281   r0 = range_nonzero (short_integer_type_node);
4282   range_cast (r0, integer_type_node);
4283   r1 = int_range<1> (INT (-32768), INT (-1));
4284   r2 = int_range<1> (INT (1), INT (32767));
4285   r1.union_ (r2);
4286   ASSERT_TRUE (r0 == r1);
4287 }
4288 
4289 static void
range_op_lshift_tests()4290 range_op_lshift_tests ()
4291 {
4292   // Test that 0x808.... & 0x8.... still contains 0x8....
4293   // for a large set of numbers.
4294   {
4295     int_range_max res;
4296     tree big_type = long_long_unsigned_type_node;
4297     // big_num = 0x808,0000,0000,0000
4298     tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4299                                         build_int_cst (big_type, 0x808),
4300                                         build_int_cst (big_type, 48));
4301     op_bitwise_and.fold_range (res, big_type,
4302                                      int_range <1> (big_type),
4303                                      int_range <1> (big_num, big_num));
4304     // val = 0x8,0000,0000,0000
4305     tree val = fold_build2 (LSHIFT_EXPR, big_type,
4306                                   build_int_cst (big_type, 0x8),
4307                                   build_int_cst (big_type, 48));
4308     ASSERT_TRUE (res.contains_p (val));
4309   }
4310 
4311   if (TYPE_PRECISION (unsigned_type_node) > 31)
4312     {
4313       // unsigned VARYING = op1 << 1 should be VARYING.
4314       int_range<2> lhs (unsigned_type_node);
4315       int_range<2> shift (INT (1), INT (1));
4316       int_range_max op1;
4317       op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4318       ASSERT_TRUE (op1.varying_p ());
4319 
4320       // 0 = op1 << 1  should be [0,0], [0x8000000, 0x8000000].
4321       int_range<2> zero (UINT (0), UINT (0));
4322       op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4323       ASSERT_TRUE (op1.num_pairs () == 2);
4324       // Remove the [0,0] range.
4325       op1.intersect (zero);
4326       ASSERT_TRUE (op1.num_pairs () == 1);
4327       //  op1 << 1   should be [0x8000,0x8000] << 1,
4328       //  which should result in [0,0].
4329       int_range_max result;
4330       op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4331       ASSERT_TRUE (result == zero);
4332     }
4333   // signed VARYING = op1 << 1 should be VARYING.
4334   if (TYPE_PRECISION (integer_type_node) > 31)
4335     {
4336       // unsigned VARYING = op1 << 1  hould be VARYING.
4337       int_range<2> lhs (integer_type_node);
4338       int_range<2> shift (INT (1), INT (1));
4339       int_range_max op1;
4340       op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4341       ASSERT_TRUE (op1.varying_p ());
4342 
4343       //  0 = op1 << 1  should be [0,0], [0x8000000, 0x8000000].
4344       int_range<2> zero (INT (0), INT (0));
4345       op_lshift.op1_range (op1, integer_type_node, zero, shift);
4346       ASSERT_TRUE (op1.num_pairs () == 2);
4347       // Remove the [0,0] range.
4348       op1.intersect (zero);
4349       ASSERT_TRUE (op1.num_pairs () == 1);
4350       //  op1 << 1   shuould be [0x8000,0x8000] << 1,
4351       //  which should result in [0,0].
4352       int_range_max result;
4353       op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4354       ASSERT_TRUE (result == zero);
4355     }
4356 }
4357 
4358 static void
range_op_rshift_tests()4359 range_op_rshift_tests ()
4360 {
4361   // unsigned: [3, MAX] = OP1 >> 1
4362   {
4363     int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4364                            TYPE_MAX_VALUE (unsigned_type_node));
4365     int_range_max one (build_one_cst (unsigned_type_node),
4366                            build_one_cst (unsigned_type_node));
4367     int_range_max op1;
4368     op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4369     ASSERT_FALSE (op1.contains_p (UINT (3)));
4370   }
4371 
4372   // signed: [3, MAX] = OP1 >> 1
4373   {
4374     int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4375     int_range_max one (INT (1), INT (1));
4376     int_range_max op1;
4377     op_rshift.op1_range (op1, integer_type_node, lhs, one);
4378     ASSERT_FALSE (op1.contains_p (INT (-2)));
4379   }
4380 
4381   // This is impossible, so OP1 should be [].
4382   // signed: [MIN, MIN] = OP1 >> 1
4383   {
4384     int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4385                            TYPE_MIN_VALUE (integer_type_node));
4386     int_range_max one (INT (1), INT (1));
4387     int_range_max op1;
4388     op_rshift.op1_range (op1, integer_type_node, lhs, one);
4389     ASSERT_TRUE (op1.undefined_p ());
4390   }
4391 
4392   // signed: ~[-1] = OP1 >> 31
4393   if (TYPE_PRECISION (integer_type_node) > 31)
4394     {
4395       int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4396       int_range_max shift (INT (31), INT (31));
4397       int_range_max op1;
4398       op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4399       int_range_max negatives = range_negatives (integer_type_node);
4400       negatives.intersect (op1);
4401       ASSERT_TRUE (negatives.undefined_p ());
4402     }
4403 }
4404 
4405 static void
range_op_bitwise_and_tests()4406 range_op_bitwise_and_tests ()
4407 {
4408   int_range_max res;
4409   tree min = vrp_val_min (integer_type_node);
4410   tree max = vrp_val_max (integer_type_node);
4411   tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4412                                  build_one_cst (integer_type_node));
4413   int_range_max i1 (tiny, max);
4414   int_range_max i2 (build_int_cst (integer_type_node, 255),
4415                         build_int_cst (integer_type_node, 255));
4416 
4417   // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4418   op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4419   ASSERT_TRUE (res == int_range<1> (integer_type_node));
4420 
4421   // VARYING = OP1 & 255: OP1 is VARYING
4422   i1 = int_range<1> (integer_type_node);
4423   op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4424   ASSERT_TRUE (res == int_range<1> (integer_type_node));
4425 
4426   // (NONZERO | X) is nonzero.
4427   i1.set_nonzero (integer_type_node);
4428   i2.set_varying (integer_type_node);
4429   op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4430   ASSERT_TRUE (res.nonzero_p ());
4431 
4432   // (NEGATIVE | X) is nonzero.
4433   i1 = int_range<1> (INT (-5), INT (-3));
4434   i2.set_varying (integer_type_node);
4435   op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4436   ASSERT_FALSE (res.contains_p (INT (0)));
4437 }
4438 
4439 static void
range_relational_tests()4440 range_relational_tests ()
4441 {
4442   int_range<2> lhs (unsigned_char_type_node);
4443   int_range<2> op1 (UCHAR (8), UCHAR (10));
4444   int_range<2> op2 (UCHAR (20), UCHAR (20));
4445 
4446   // Never wrapping additions mean LHS > OP1.
4447   tree_code code = op_plus.lhs_op1_relation (lhs, op1, op2);
4448   ASSERT_TRUE (code == GT_EXPR);
4449 
4450   // Most wrapping additions mean nothing...
4451   op1 = int_range<2> (UCHAR (8), UCHAR (10));
4452   op2 = int_range<2> (UCHAR (0), UCHAR (255));
4453   code = op_plus.lhs_op1_relation (lhs, op1, op2);
4454   ASSERT_TRUE (code == VREL_NONE);
4455 
4456   // However, always wrapping additions mean LHS < OP1.
4457   op1 = int_range<2> (UCHAR (1), UCHAR (255));
4458   op2 = int_range<2> (UCHAR (255), UCHAR (255));
4459   code = op_plus.lhs_op1_relation (lhs, op1, op2);
4460   ASSERT_TRUE (code == LT_EXPR);
4461 }
4462 
4463 void
range_op_tests()4464 range_op_tests ()
4465 {
4466   range_op_rshift_tests ();
4467   range_op_lshift_tests ();
4468   range_op_bitwise_and_tests ();
4469   range_op_cast_tests ();
4470   range_relational_tests ();
4471 }
4472 
4473 } // namespace selftest
4474 
4475 #endif // CHECKING_P
4476