c, c++: attribute format on a ctor with a vbase [PR101833, PR47634]
[official-gcc.git] / gcc / range-op.cc
blob47c6dff8f3ee835d816ea72c5a798746284b2451
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>.
6 This file is part of GCC.
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.
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.
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/>. */
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"
50 // Return the upper limit for a type.
52 static inline wide_int
53 max_limit (const_tree type)
55 return wi::max_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
58 // Return the lower limit for a type.
60 static inline wide_int
61 min_limit (const_tree type)
63 return wi::min_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
66 // Return false if shifting by OP is undefined behavior. Otherwise, return
67 // true and the range it is to be shifted by. This allows trimming out of
68 // undefined ranges, leaving only valid ranges if there are any.
70 static inline bool
71 get_shift_range (irange &r, tree type, const irange &op)
73 if (op.undefined_p ())
74 return false;
76 // Build valid range and intersect it with the shift range.
77 r = value_range (build_int_cst_type (op.type (), 0),
78 build_int_cst_type (op.type (), TYPE_PRECISION (type) - 1));
79 r.intersect (op);
81 // If there are no valid ranges in the shift range, returned false.
82 if (r.undefined_p ())
83 return false;
84 return true;
87 // Return TRUE if 0 is within [WMIN, WMAX].
89 static inline bool
90 wi_includes_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
92 signop sign = TYPE_SIGN (type);
93 return wi::le_p (wmin, 0, sign) && wi::ge_p (wmax, 0, sign);
96 // Return TRUE if [WMIN, WMAX] is the singleton 0.
98 static inline bool
99 wi_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
101 unsigned prec = TYPE_PRECISION (type);
102 return wmin == wmax && wi::eq_p (wmin, wi::zero (prec));
105 // Default wide_int fold operation returns [MIN, MAX].
107 void
108 range_operator::wi_fold (irange &r, tree type,
109 const wide_int &lh_lb ATTRIBUTE_UNUSED,
110 const wide_int &lh_ub ATTRIBUTE_UNUSED,
111 const wide_int &rh_lb ATTRIBUTE_UNUSED,
112 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
114 gcc_checking_assert (irange::supports_type_p (type));
115 r.set_varying (type);
118 // Call wi_fold, except further split small subranges into constants.
119 // This can provide better precision. For something 8 >> [0,1]
120 // Instead of [8, 16], we will produce [8,8][16,16]
122 void
123 range_operator::wi_fold_in_parts (irange &r, tree type,
124 const wide_int &lh_lb,
125 const wide_int &lh_ub,
126 const wide_int &rh_lb,
127 const wide_int &rh_ub) const
129 int_range_max tmp;
130 widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
131 widest_int::from (rh_lb, TYPE_SIGN (type)));
132 widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
133 widest_int::from (lh_lb, TYPE_SIGN (type)));
134 // If there are 2, 3, or 4 values in the RH range, do them separately.
135 // Call wi_fold_in_parts to check the RH side.
136 if (rh_range > 0 && rh_range < 4)
138 wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
139 if (rh_range > 1)
141 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
142 r.union_ (tmp);
143 if (rh_range == 3)
145 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
146 r.union_ (tmp);
149 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
150 r.union_ (tmp);
152 // Otherise check for 2, 3, or 4 values in the LH range and split them up.
153 // The RH side has been checked, so no recursion needed.
154 else if (lh_range > 0 && lh_range < 4)
156 wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
157 if (lh_range > 1)
159 wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
160 r.union_ (tmp);
161 if (lh_range == 3)
163 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
164 r.union_ (tmp);
167 wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
168 r.union_ (tmp);
170 // Otherwise just call wi_fold.
171 else
172 wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
175 // The default for fold is to break all ranges into sub-ranges and
176 // invoke the wi_fold method on each sub-range pair.
178 bool
179 range_operator::fold_range (irange &r, tree type,
180 const irange &lh,
181 const irange &rh,
182 relation_kind rel) const
184 gcc_checking_assert (irange::supports_type_p (type));
185 if (empty_range_varying (r, type, lh, rh))
186 return true;
188 unsigned num_lh = lh.num_pairs ();
189 unsigned num_rh = rh.num_pairs ();
191 // If both ranges are single pairs, fold directly into the result range.
192 // If the number of subranges grows too high, produce a summary result as the
193 // loop becomes exponential with little benefit. See PR 103821.
194 if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
196 wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
197 rh.lower_bound (), rh.upper_bound ());
198 op1_op2_relation_effect (r, type, lh, rh, rel);
199 return true;
202 int_range_max tmp;
203 r.set_undefined ();
204 for (unsigned x = 0; x < num_lh; ++x)
205 for (unsigned y = 0; y < num_rh; ++y)
207 wide_int lh_lb = lh.lower_bound (x);
208 wide_int lh_ub = lh.upper_bound (x);
209 wide_int rh_lb = rh.lower_bound (y);
210 wide_int rh_ub = rh.upper_bound (y);
211 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
212 r.union_ (tmp);
213 if (r.varying_p ())
215 op1_op2_relation_effect (r, type, lh, rh, rel);
216 return true;
219 op1_op2_relation_effect (r, type, lh, rh, rel);
220 return true;
223 // The default for op1_range is to return false.
225 bool
226 range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
227 tree type ATTRIBUTE_UNUSED,
228 const irange &lhs ATTRIBUTE_UNUSED,
229 const irange &op2 ATTRIBUTE_UNUSED,
230 relation_kind rel ATTRIBUTE_UNUSED) const
232 return false;
235 // The default for op2_range is to return false.
237 bool
238 range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
239 tree type ATTRIBUTE_UNUSED,
240 const irange &lhs ATTRIBUTE_UNUSED,
241 const irange &op1 ATTRIBUTE_UNUSED,
242 relation_kind rel ATTRIBUTE_UNUSED) const
244 return false;
247 // The default relation routines return VREL_NONE.
249 enum tree_code
250 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
251 const irange &op1 ATTRIBUTE_UNUSED,
252 const irange &op2 ATTRIBUTE_UNUSED) const
254 return VREL_NONE;
257 enum tree_code
258 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
259 const irange &op1 ATTRIBUTE_UNUSED,
260 const irange &op2 ATTRIBUTE_UNUSED) const
262 return VREL_NONE;
265 enum tree_code
266 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
268 return VREL_NONE;
271 // Default is no relation affects the LHS.
273 bool
274 range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
275 tree type ATTRIBUTE_UNUSED,
276 const irange &op1_range ATTRIBUTE_UNUSED,
277 const irange &op2_range ATTRIBUTE_UNUSED,
278 relation_kind rel ATTRIBUTE_UNUSED) const
280 return false;
283 // Create and return a range from a pair of wide-ints that are known
284 // to have overflowed (or underflowed).
286 static void
287 value_range_from_overflowed_bounds (irange &r, tree type,
288 const wide_int &wmin,
289 const wide_int &wmax)
291 const signop sgn = TYPE_SIGN (type);
292 const unsigned int prec = TYPE_PRECISION (type);
294 wide_int tmin = wide_int::from (wmin, prec, sgn);
295 wide_int tmax = wide_int::from (wmax, prec, sgn);
297 bool covers = false;
298 wide_int tem = tmin;
299 tmin = tmax + 1;
300 if (wi::cmp (tmin, tmax, sgn) < 0)
301 covers = true;
302 tmax = tem - 1;
303 if (wi::cmp (tmax, tem, sgn) > 0)
304 covers = true;
306 // If the anti-range would cover nothing, drop to varying.
307 // Likewise if the anti-range bounds are outside of the types
308 // values.
309 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
310 r.set_varying (type);
311 else
313 tree tree_min = wide_int_to_tree (type, tmin);
314 tree tree_max = wide_int_to_tree (type, tmax);
315 r.set (tree_min, tree_max, VR_ANTI_RANGE);
319 // Create and return a range from a pair of wide-ints. MIN_OVF and
320 // MAX_OVF describe any overflow that might have occurred while
321 // calculating WMIN and WMAX respectively.
323 static void
324 value_range_with_overflow (irange &r, tree type,
325 const wide_int &wmin, const wide_int &wmax,
326 wi::overflow_type min_ovf = wi::OVF_NONE,
327 wi::overflow_type max_ovf = wi::OVF_NONE)
329 const signop sgn = TYPE_SIGN (type);
330 const unsigned int prec = TYPE_PRECISION (type);
331 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
333 // For one bit precision if max != min, then the range covers all
334 // values.
335 if (prec == 1 && wi::ne_p (wmax, wmin))
337 r.set_varying (type);
338 return;
341 if (overflow_wraps)
343 // If overflow wraps, truncate the values and adjust the range,
344 // kind, and bounds appropriately.
345 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
347 wide_int tmin = wide_int::from (wmin, prec, sgn);
348 wide_int tmax = wide_int::from (wmax, prec, sgn);
349 // If the limits are swapped, we wrapped around and cover
350 // the entire range.
351 if (wi::gt_p (tmin, tmax, sgn))
352 r.set_varying (type);
353 else
354 // No overflow or both overflow or underflow. The range
355 // kind stays normal.
356 r.set (wide_int_to_tree (type, tmin),
357 wide_int_to_tree (type, tmax));
358 return;
361 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
362 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
363 value_range_from_overflowed_bounds (r, type, wmin, wmax);
364 else
365 // Other underflow and/or overflow, drop to VR_VARYING.
366 r.set_varying (type);
368 else
370 // If both bounds either underflowed or overflowed, then the result
371 // is undefined.
372 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
373 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
375 r.set_undefined ();
376 return;
379 // If overflow does not wrap, saturate to [MIN, MAX].
380 wide_int new_lb, new_ub;
381 if (min_ovf == wi::OVF_UNDERFLOW)
382 new_lb = wi::min_value (prec, sgn);
383 else if (min_ovf == wi::OVF_OVERFLOW)
384 new_lb = wi::max_value (prec, sgn);
385 else
386 new_lb = wmin;
388 if (max_ovf == wi::OVF_UNDERFLOW)
389 new_ub = wi::min_value (prec, sgn);
390 else if (max_ovf == wi::OVF_OVERFLOW)
391 new_ub = wi::max_value (prec, sgn);
392 else
393 new_ub = wmax;
395 r.set (wide_int_to_tree (type, new_lb),
396 wide_int_to_tree (type, new_ub));
400 // Create and return a range from a pair of wide-ints. Canonicalize
401 // the case where the bounds are swapped. In which case, we transform
402 // [10,5] into [MIN,5][10,MAX].
404 static inline void
405 create_possibly_reversed_range (irange &r, tree type,
406 const wide_int &new_lb, const wide_int &new_ub)
408 signop s = TYPE_SIGN (type);
409 // If the bounds are swapped, treat the result as if an overflow occured.
410 if (wi::gt_p (new_lb, new_ub, s))
411 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
412 else
413 // Otherwise it's just a normal range.
414 r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
417 // Return the summary information about boolean range LHS. If EMPTY/FULL,
418 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
420 bool_range_state
421 get_bool_state (irange &r, const irange &lhs, tree val_type)
423 // If there is no result, then this is unexecutable.
424 if (lhs.undefined_p ())
426 r.set_undefined ();
427 return BRS_EMPTY;
430 if (lhs.zero_p ())
431 return BRS_FALSE;
433 // For TRUE, we can't just test for [1,1] because Ada can have
434 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
435 if (lhs.contains_p (build_zero_cst (lhs.type ())))
437 r.set_varying (val_type);
438 return BRS_FULL;
441 return BRS_TRUE;
445 class operator_equal : public range_operator
447 public:
448 virtual bool fold_range (irange &r, tree type,
449 const irange &op1,
450 const irange &op2,
451 relation_kind rel = VREL_NONE) const;
452 virtual bool op1_range (irange &r, tree type,
453 const irange &lhs,
454 const irange &val,
455 relation_kind rel = VREL_NONE) const;
456 virtual bool op2_range (irange &r, tree type,
457 const irange &lhs,
458 const irange &val,
459 relation_kind rel = VREL_NONE) const;
460 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
461 } op_equal;
463 // Check if the LHS range indicates a relation between OP1 and OP2.
465 enum tree_code
466 equal_op1_op2_relation (const irange &lhs)
468 if (lhs.undefined_p ())
469 return VREL_EMPTY;
471 // FALSE = op1 == op2 indicates NE_EXPR.
472 if (lhs.zero_p ())
473 return NE_EXPR;
475 // TRUE = op1 == op2 indicates EQ_EXPR.
476 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
477 return EQ_EXPR;
478 return VREL_NONE;
481 enum tree_code
482 operator_equal::op1_op2_relation (const irange &lhs) const
484 return equal_op1_op2_relation (lhs);
488 bool
489 operator_equal::fold_range (irange &r, tree type,
490 const irange &op1,
491 const irange &op2,
492 relation_kind rel) const
494 if (relop_early_resolve (r, type, op1, op2, rel, EQ_EXPR))
495 return true;
497 // We can be sure the values are always equal or not if both ranges
498 // consist of a single value, and then compare them.
499 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
500 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
502 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
503 r = range_true (type);
504 else
505 r = range_false (type);
507 else
509 // If ranges do not intersect, we know the range is not equal,
510 // otherwise we don't know anything for sure.
511 int_range_max tmp = op1;
512 tmp.intersect (op2);
513 if (tmp.undefined_p ())
514 r = range_false (type);
515 else
516 r = range_true_and_false (type);
518 return true;
521 bool
522 operator_equal::op1_range (irange &r, tree type,
523 const irange &lhs,
524 const irange &op2,
525 relation_kind rel ATTRIBUTE_UNUSED) const
527 switch (get_bool_state (r, lhs, type))
529 case BRS_FALSE:
530 // If the result is false, the only time we know anything is
531 // if OP2 is a constant.
532 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
534 r = op2;
535 r.invert ();
537 else
538 r.set_varying (type);
539 break;
541 case BRS_TRUE:
542 // If it's true, the result is the same as OP2.
543 r = op2;
544 break;
546 default:
547 break;
549 return true;
552 bool
553 operator_equal::op2_range (irange &r, tree type,
554 const irange &lhs,
555 const irange &op1,
556 relation_kind rel) const
558 return operator_equal::op1_range (r, type, lhs, op1, rel);
561 class operator_not_equal : public range_operator
563 public:
564 virtual bool fold_range (irange &r, tree type,
565 const irange &op1,
566 const irange &op2,
567 relation_kind rel = VREL_NONE) const;
568 virtual bool op1_range (irange &r, tree type,
569 const irange &lhs,
570 const irange &op2,
571 relation_kind rel = VREL_NONE) const;
572 virtual bool op2_range (irange &r, tree type,
573 const irange &lhs,
574 const irange &op1,
575 relation_kind rel = VREL_NONE) const;
576 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
577 } op_not_equal;
579 // Check if the LHS range indicates a relation between OP1 and OP2.
581 enum tree_code
582 not_equal_op1_op2_relation (const irange &lhs)
584 if (lhs.undefined_p ())
585 return VREL_EMPTY;
587 // FALSE = op1 != op2 indicates EQ_EXPR.
588 if (lhs.zero_p ())
589 return EQ_EXPR;
591 // TRUE = op1 != op2 indicates NE_EXPR.
592 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
593 return NE_EXPR;
594 return VREL_NONE;
597 enum tree_code
598 operator_not_equal::op1_op2_relation (const irange &lhs) const
600 return not_equal_op1_op2_relation (lhs);
603 bool
604 operator_not_equal::fold_range (irange &r, tree type,
605 const irange &op1,
606 const irange &op2,
607 relation_kind rel) const
609 if (relop_early_resolve (r, type, op1, op2, rel, NE_EXPR))
610 return true;
612 // We can be sure the values are always equal or not if both ranges
613 // consist of a single value, and then compare them.
614 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
615 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
617 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
618 r = range_true (type);
619 else
620 r = range_false (type);
622 else
624 // If ranges do not intersect, we know the range is not equal,
625 // otherwise we don't know anything for sure.
626 int_range_max tmp = op1;
627 tmp.intersect (op2);
628 if (tmp.undefined_p ())
629 r = range_true (type);
630 else
631 r = range_true_and_false (type);
633 return true;
636 bool
637 operator_not_equal::op1_range (irange &r, tree type,
638 const irange &lhs,
639 const irange &op2,
640 relation_kind rel ATTRIBUTE_UNUSED) const
642 switch (get_bool_state (r, lhs, type))
644 case BRS_TRUE:
645 // If the result is true, the only time we know anything is if
646 // OP2 is a constant.
647 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
649 r = op2;
650 r.invert ();
652 else
653 r.set_varying (type);
654 break;
656 case BRS_FALSE:
657 // If it's false, the result is the same as OP2.
658 r = op2;
659 break;
661 default:
662 break;
664 return true;
668 bool
669 operator_not_equal::op2_range (irange &r, tree type,
670 const irange &lhs,
671 const irange &op1,
672 relation_kind rel) const
674 return operator_not_equal::op1_range (r, type, lhs, op1, rel);
677 // (X < VAL) produces the range of [MIN, VAL - 1].
679 static void
680 build_lt (irange &r, tree type, const wide_int &val)
682 wi::overflow_type ov;
683 wide_int lim;
684 signop sgn = TYPE_SIGN (type);
686 // Signed 1 bit cannot represent 1 for subtraction.
687 if (sgn == SIGNED)
688 lim = wi::add (val, -1, sgn, &ov);
689 else
690 lim = wi::sub (val, 1, sgn, &ov);
692 // If val - 1 underflows, check if X < MIN, which is an empty range.
693 if (ov)
694 r.set_undefined ();
695 else
696 r = int_range<1> (type, min_limit (type), lim);
699 // (X <= VAL) produces the range of [MIN, VAL].
701 static void
702 build_le (irange &r, tree type, const wide_int &val)
704 r = int_range<1> (type, min_limit (type), val);
707 // (X > VAL) produces the range of [VAL + 1, MAX].
709 static void
710 build_gt (irange &r, tree type, const wide_int &val)
712 wi::overflow_type ov;
713 wide_int lim;
714 signop sgn = TYPE_SIGN (type);
716 // Signed 1 bit cannot represent 1 for addition.
717 if (sgn == SIGNED)
718 lim = wi::sub (val, -1, sgn, &ov);
719 else
720 lim = wi::add (val, 1, sgn, &ov);
721 // If val + 1 overflows, check is for X > MAX, which is an empty range.
722 if (ov)
723 r.set_undefined ();
724 else
725 r = int_range<1> (type, lim, max_limit (type));
728 // (X >= val) produces the range of [VAL, MAX].
730 static void
731 build_ge (irange &r, tree type, const wide_int &val)
733 r = int_range<1> (type, val, max_limit (type));
737 class operator_lt : public range_operator
739 public:
740 virtual bool fold_range (irange &r, tree type,
741 const irange &op1,
742 const irange &op2,
743 relation_kind rel = VREL_NONE) const;
744 virtual bool op1_range (irange &r, tree type,
745 const irange &lhs,
746 const irange &op2,
747 relation_kind rel = VREL_NONE) const;
748 virtual bool op2_range (irange &r, tree type,
749 const irange &lhs,
750 const irange &op1,
751 relation_kind rel = VREL_NONE) const;
752 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
753 } op_lt;
755 // Check if the LHS range indicates a relation between OP1 and OP2.
757 enum tree_code
758 lt_op1_op2_relation (const irange &lhs)
760 if (lhs.undefined_p ())
761 return VREL_EMPTY;
763 // FALSE = op1 < op2 indicates GE_EXPR.
764 if (lhs.zero_p ())
765 return GE_EXPR;
767 // TRUE = op1 < op2 indicates LT_EXPR.
768 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
769 return LT_EXPR;
770 return VREL_NONE;
773 enum tree_code
774 operator_lt::op1_op2_relation (const irange &lhs) const
776 return lt_op1_op2_relation (lhs);
779 bool
780 operator_lt::fold_range (irange &r, tree type,
781 const irange &op1,
782 const irange &op2,
783 relation_kind rel) const
785 if (relop_early_resolve (r, type, op1, op2, rel, LT_EXPR))
786 return true;
788 signop sign = TYPE_SIGN (op1.type ());
789 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
791 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
792 r = range_true (type);
793 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
794 r = range_false (type);
795 else
796 r = range_true_and_false (type);
797 return true;
800 bool
801 operator_lt::op1_range (irange &r, tree type,
802 const irange &lhs,
803 const irange &op2,
804 relation_kind rel ATTRIBUTE_UNUSED) const
806 switch (get_bool_state (r, lhs, type))
808 case BRS_TRUE:
809 build_lt (r, type, op2.upper_bound ());
810 break;
812 case BRS_FALSE:
813 build_ge (r, type, op2.lower_bound ());
814 break;
816 default:
817 break;
819 return true;
822 bool
823 operator_lt::op2_range (irange &r, tree type,
824 const irange &lhs,
825 const irange &op1,
826 relation_kind rel ATTRIBUTE_UNUSED) const
828 switch (get_bool_state (r, lhs, type))
830 case BRS_FALSE:
831 build_le (r, type, op1.upper_bound ());
832 break;
834 case BRS_TRUE:
835 build_gt (r, type, op1.lower_bound ());
836 break;
838 default:
839 break;
841 return true;
845 class operator_le : public range_operator
847 public:
848 virtual bool fold_range (irange &r, tree type,
849 const irange &op1,
850 const irange &op2,
851 relation_kind rel = VREL_NONE) const;
852 virtual bool op1_range (irange &r, tree type,
853 const irange &lhs,
854 const irange &op2,
855 relation_kind rel = VREL_NONE) const;
856 virtual bool op2_range (irange &r, tree type,
857 const irange &lhs,
858 const irange &op1,
859 relation_kind rel = VREL_NONE) const;
860 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
861 } op_le;
863 // Check if the LHS range indicates a relation between OP1 and OP2.
865 enum tree_code
866 le_op1_op2_relation (const irange &lhs)
868 if (lhs.undefined_p ())
869 return VREL_EMPTY;
871 // FALSE = op1 <= op2 indicates GT_EXPR.
872 if (lhs.zero_p ())
873 return GT_EXPR;
875 // TRUE = op1 <= op2 indicates LE_EXPR.
876 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
877 return LE_EXPR;
878 return VREL_NONE;
881 enum tree_code
882 operator_le::op1_op2_relation (const irange &lhs) const
884 return le_op1_op2_relation (lhs);
887 bool
888 operator_le::fold_range (irange &r, tree type,
889 const irange &op1,
890 const irange &op2,
891 relation_kind rel) const
893 if (relop_early_resolve (r, type, op1, op2, rel, LE_EXPR))
894 return true;
896 signop sign = TYPE_SIGN (op1.type ());
897 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
899 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
900 r = range_true (type);
901 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
902 r = range_false (type);
903 else
904 r = range_true_and_false (type);
905 return true;
908 bool
909 operator_le::op1_range (irange &r, tree type,
910 const irange &lhs,
911 const irange &op2,
912 relation_kind rel ATTRIBUTE_UNUSED) const
914 switch (get_bool_state (r, lhs, type))
916 case BRS_TRUE:
917 build_le (r, type, op2.upper_bound ());
918 break;
920 case BRS_FALSE:
921 build_gt (r, type, op2.lower_bound ());
922 break;
924 default:
925 break;
927 return true;
930 bool
931 operator_le::op2_range (irange &r, tree type,
932 const irange &lhs,
933 const irange &op1,
934 relation_kind rel ATTRIBUTE_UNUSED) const
936 switch (get_bool_state (r, lhs, type))
938 case BRS_FALSE:
939 build_lt (r, type, op1.upper_bound ());
940 break;
942 case BRS_TRUE:
943 build_ge (r, type, op1.lower_bound ());
944 break;
946 default:
947 break;
949 return true;
953 class operator_gt : public range_operator
955 public:
956 virtual bool fold_range (irange &r, tree type,
957 const irange &op1,
958 const irange &op2,
959 relation_kind rel = VREL_NONE) const;
960 virtual bool op1_range (irange &r, tree type,
961 const irange &lhs,
962 const irange &op2,
963 relation_kind rel = VREL_NONE) const;
964 virtual bool op2_range (irange &r, tree type,
965 const irange &lhs,
966 const irange &op1,
967 relation_kind rel = VREL_NONE) const;
968 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
969 } op_gt;
971 // Check if the LHS range indicates a relation between OP1 and OP2.
973 enum tree_code
974 gt_op1_op2_relation (const irange &lhs)
976 if (lhs.undefined_p ())
977 return VREL_EMPTY;
979 // FALSE = op1 > op2 indicates LE_EXPR.
980 if (lhs.zero_p ())
981 return LE_EXPR;
983 // TRUE = op1 > op2 indicates GT_EXPR.
984 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
985 return GT_EXPR;
986 return VREL_NONE;
989 enum tree_code
990 operator_gt::op1_op2_relation (const irange &lhs) const
992 return gt_op1_op2_relation (lhs);
996 bool
997 operator_gt::fold_range (irange &r, tree type,
998 const irange &op1, const irange &op2,
999 relation_kind rel) const
1001 if (relop_early_resolve (r, type, op1, op2, rel, GT_EXPR))
1002 return true;
1004 signop sign = TYPE_SIGN (op1.type ());
1005 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1007 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1008 r = range_true (type);
1009 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1010 r = range_false (type);
1011 else
1012 r = range_true_and_false (type);
1013 return true;
1016 bool
1017 operator_gt::op1_range (irange &r, tree type,
1018 const irange &lhs, const irange &op2,
1019 relation_kind rel ATTRIBUTE_UNUSED) const
1021 switch (get_bool_state (r, lhs, type))
1023 case BRS_TRUE:
1024 build_gt (r, type, op2.lower_bound ());
1025 break;
1027 case BRS_FALSE:
1028 build_le (r, type, op2.upper_bound ());
1029 break;
1031 default:
1032 break;
1034 return true;
1037 bool
1038 operator_gt::op2_range (irange &r, tree type,
1039 const irange &lhs,
1040 const irange &op1,
1041 relation_kind rel ATTRIBUTE_UNUSED) const
1043 switch (get_bool_state (r, lhs, type))
1045 case BRS_FALSE:
1046 build_ge (r, type, op1.lower_bound ());
1047 break;
1049 case BRS_TRUE:
1050 build_lt (r, type, op1.upper_bound ());
1051 break;
1053 default:
1054 break;
1056 return true;
1060 class operator_ge : public range_operator
1062 public:
1063 virtual bool fold_range (irange &r, tree type,
1064 const irange &op1,
1065 const irange &op2,
1066 relation_kind rel = VREL_NONE) const;
1067 virtual bool op1_range (irange &r, tree type,
1068 const irange &lhs,
1069 const irange &op2,
1070 relation_kind rel = VREL_NONE) const;
1071 virtual bool op2_range (irange &r, tree type,
1072 const irange &lhs,
1073 const irange &op1,
1074 relation_kind rel = VREL_NONE) const;
1075 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
1076 } op_ge;
1078 // Check if the LHS range indicates a relation between OP1 and OP2.
1080 enum tree_code
1081 ge_op1_op2_relation (const irange &lhs)
1083 if (lhs.undefined_p ())
1084 return VREL_EMPTY;
1086 // FALSE = op1 >= op2 indicates LT_EXPR.
1087 if (lhs.zero_p ())
1088 return LT_EXPR;
1090 // TRUE = op1 >= op2 indicates GE_EXPR.
1091 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1092 return GE_EXPR;
1093 return VREL_NONE;
1096 enum tree_code
1097 operator_ge::op1_op2_relation (const irange &lhs) const
1099 return ge_op1_op2_relation (lhs);
1102 bool
1103 operator_ge::fold_range (irange &r, tree type,
1104 const irange &op1,
1105 const irange &op2,
1106 relation_kind rel) const
1108 if (relop_early_resolve (r, type, op1, op2, rel, GE_EXPR))
1109 return true;
1111 signop sign = TYPE_SIGN (op1.type ());
1112 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1114 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1115 r = range_true (type);
1116 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1117 r = range_false (type);
1118 else
1119 r = range_true_and_false (type);
1120 return true;
1123 bool
1124 operator_ge::op1_range (irange &r, tree type,
1125 const irange &lhs,
1126 const irange &op2,
1127 relation_kind rel ATTRIBUTE_UNUSED) const
1129 switch (get_bool_state (r, lhs, type))
1131 case BRS_TRUE:
1132 build_ge (r, type, op2.lower_bound ());
1133 break;
1135 case BRS_FALSE:
1136 build_lt (r, type, op2.upper_bound ());
1137 break;
1139 default:
1140 break;
1142 return true;
1145 bool
1146 operator_ge::op2_range (irange &r, tree type,
1147 const irange &lhs,
1148 const irange &op1,
1149 relation_kind rel ATTRIBUTE_UNUSED) const
1151 switch (get_bool_state (r, lhs, type))
1153 case BRS_FALSE:
1154 build_gt (r, type, op1.lower_bound ());
1155 break;
1157 case BRS_TRUE:
1158 build_le (r, type, op1.upper_bound ());
1159 break;
1161 default:
1162 break;
1164 return true;
1168 class operator_plus : public range_operator
1170 public:
1171 virtual bool op1_range (irange &r, tree type,
1172 const irange &lhs,
1173 const irange &op2,
1174 relation_kind rel ATTRIBUTE_UNUSED) const;
1175 virtual bool op2_range (irange &r, tree type,
1176 const irange &lhs,
1177 const irange &op1,
1178 relation_kind rel ATTRIBUTE_UNUSED) const;
1179 virtual void wi_fold (irange &r, tree type,
1180 const wide_int &lh_lb,
1181 const wide_int &lh_ub,
1182 const wide_int &rh_lb,
1183 const wide_int &rh_ub) const;
1184 virtual enum tree_code lhs_op1_relation (const irange &lhs, const irange &op1,
1185 const irange &op2) const;
1186 virtual enum tree_code lhs_op2_relation (const irange &lhs, const irange &op1,
1187 const irange &op2) const;
1188 } op_plus;
1190 // Check to see if the range of OP2 indicates anything about the relation
1191 // between LHS and OP1.
1193 enum tree_code
1194 operator_plus::lhs_op1_relation (const irange &lhs,
1195 const irange &op1,
1196 const irange &op2) const
1198 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1199 return VREL_NONE;
1201 tree type = lhs.type ();
1202 unsigned prec = TYPE_PRECISION (type);
1203 wi::overflow_type ovf1, ovf2;
1204 signop sign = TYPE_SIGN (type);
1206 // LHS = OP1 + 0 indicates LHS == OP1.
1207 if (op2.zero_p ())
1208 return EQ_EXPR;
1210 if (TYPE_OVERFLOW_WRAPS (type))
1212 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1213 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1215 else
1216 ovf1 = ovf2 = wi::OVF_NONE;
1218 // Never wrapping additions.
1219 if (!ovf1 && !ovf2)
1221 // Positive op2 means lhs > op1.
1222 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1223 return GT_EXPR;
1224 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1225 return GE_EXPR;
1227 // Negative op2 means lhs < op1.
1228 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1229 return LT_EXPR;
1230 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1231 return LE_EXPR;
1233 // Always wrapping additions.
1234 else if (ovf1 && ovf1 == ovf2)
1236 // Positive op2 means lhs < op1.
1237 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1238 return LT_EXPR;
1239 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1240 return LE_EXPR;
1242 // Negative op2 means lhs > op1.
1243 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1244 return GT_EXPR;
1245 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1246 return GE_EXPR;
1249 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1250 if (!range_includes_zero_p (&op2))
1251 return NE_EXPR;
1253 return VREL_NONE;
1256 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1257 // operands.
1259 enum tree_code
1260 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1261 const irange &op2) const
1263 return lhs_op1_relation (lhs, op2, op1);
1266 void
1267 operator_plus::wi_fold (irange &r, tree type,
1268 const wide_int &lh_lb, const wide_int &lh_ub,
1269 const wide_int &rh_lb, const wide_int &rh_ub) const
1271 wi::overflow_type ov_lb, ov_ub;
1272 signop s = TYPE_SIGN (type);
1273 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1274 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1275 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1278 bool
1279 operator_plus::op1_range (irange &r, tree type,
1280 const irange &lhs,
1281 const irange &op2,
1282 relation_kind rel ATTRIBUTE_UNUSED) const
1284 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op2);
1287 bool
1288 operator_plus::op2_range (irange &r, tree type,
1289 const irange &lhs,
1290 const irange &op1,
1291 relation_kind rel ATTRIBUTE_UNUSED) const
1293 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op1);
1297 class operator_minus : public range_operator
1299 public:
1300 virtual bool op1_range (irange &r, tree type,
1301 const irange &lhs,
1302 const irange &op2,
1303 relation_kind rel ATTRIBUTE_UNUSED) const;
1304 virtual bool op2_range (irange &r, tree type,
1305 const irange &lhs,
1306 const irange &op1,
1307 relation_kind rel ATTRIBUTE_UNUSED) const;
1308 virtual void wi_fold (irange &r, tree type,
1309 const wide_int &lh_lb,
1310 const wide_int &lh_ub,
1311 const wide_int &rh_lb,
1312 const wide_int &rh_ub) const;
1313 virtual bool op1_op2_relation_effect (irange &lhs_range,
1314 tree type,
1315 const irange &op1_range,
1316 const irange &op2_range,
1317 relation_kind rel) const;
1318 } op_minus;
1320 void
1321 operator_minus::wi_fold (irange &r, tree type,
1322 const wide_int &lh_lb, const wide_int &lh_ub,
1323 const wide_int &rh_lb, const wide_int &rh_ub) const
1325 wi::overflow_type ov_lb, ov_ub;
1326 signop s = TYPE_SIGN (type);
1327 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1328 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1329 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1332 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1333 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1334 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1336 static bool
1337 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1338 const irange &op1_range ATTRIBUTE_UNUSED,
1339 const irange &op2_range ATTRIBUTE_UNUSED,
1340 relation_kind rel)
1342 if (rel == VREL_NONE)
1343 return false;
1345 int_range<2> rel_range;
1346 unsigned prec = TYPE_PRECISION (type);
1347 signop sgn = TYPE_SIGN (type);
1349 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1350 if (rel == EQ_EXPR)
1351 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1352 else if (rel == NE_EXPR)
1353 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1354 VR_ANTI_RANGE);
1355 else if (TYPE_OVERFLOW_WRAPS (type))
1357 switch (rel)
1359 // For wrapping signed values and unsigned, if op1 > op2 or
1360 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1361 case GT_EXPR:
1362 case LT_EXPR:
1363 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1364 VR_ANTI_RANGE);
1365 break;
1366 default:
1367 return false;
1370 else
1372 switch (rel)
1374 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1375 case GT_EXPR:
1376 rel_range = int_range<2> (type, wi::one (prec),
1377 wi::max_value (prec, sgn));
1378 break;
1379 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1380 case GE_EXPR:
1381 rel_range = int_range<2> (type, wi::zero (prec),
1382 wi::max_value (prec, sgn));
1383 break;
1384 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1385 case LT_EXPR:
1386 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1387 wi::minus_one (prec));
1388 break;
1389 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1390 case LE_EXPR:
1391 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1392 wi::zero (prec));
1393 break;
1394 default:
1395 return false;
1398 lhs_range.intersect (rel_range);
1399 return true;
1402 bool
1403 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1404 const irange &op1_range,
1405 const irange &op2_range,
1406 relation_kind rel) const
1408 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1409 rel);
1412 bool
1413 operator_minus::op1_range (irange &r, tree type,
1414 const irange &lhs,
1415 const irange &op2,
1416 relation_kind rel ATTRIBUTE_UNUSED) const
1418 return range_op_handler (PLUS_EXPR, type)->fold_range (r, type, lhs, op2);
1421 bool
1422 operator_minus::op2_range (irange &r, tree type,
1423 const irange &lhs,
1424 const irange &op1,
1425 relation_kind rel ATTRIBUTE_UNUSED) const
1427 return fold_range (r, type, op1, lhs);
1431 class operator_pointer_diff : public range_operator
1433 virtual bool op1_op2_relation_effect (irange &lhs_range,
1434 tree type,
1435 const irange &op1_range,
1436 const irange &op2_range,
1437 relation_kind rel) const;
1438 } op_pointer_diff;
1440 bool
1441 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1442 const irange &op1_range,
1443 const irange &op2_range,
1444 relation_kind rel) const
1446 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1447 rel);
1451 class operator_min : public range_operator
1453 public:
1454 virtual void wi_fold (irange &r, tree type,
1455 const wide_int &lh_lb,
1456 const wide_int &lh_ub,
1457 const wide_int &rh_lb,
1458 const wide_int &rh_ub) const;
1459 } op_min;
1461 void
1462 operator_min::wi_fold (irange &r, tree type,
1463 const wide_int &lh_lb, const wide_int &lh_ub,
1464 const wide_int &rh_lb, const wide_int &rh_ub) const
1466 signop s = TYPE_SIGN (type);
1467 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1468 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1469 value_range_with_overflow (r, type, new_lb, new_ub);
1473 class operator_max : public range_operator
1475 public:
1476 virtual void wi_fold (irange &r, tree type,
1477 const wide_int &lh_lb,
1478 const wide_int &lh_ub,
1479 const wide_int &rh_lb,
1480 const wide_int &rh_ub) const;
1481 } op_max;
1483 void
1484 operator_max::wi_fold (irange &r, tree type,
1485 const wide_int &lh_lb, const wide_int &lh_ub,
1486 const wide_int &rh_lb, const wide_int &rh_ub) const
1488 signop s = TYPE_SIGN (type);
1489 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1490 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1491 value_range_with_overflow (r, type, new_lb, new_ub);
1495 class cross_product_operator : public range_operator
1497 public:
1498 // Perform an operation between two wide-ints and place the result
1499 // in R. Return true if the operation overflowed.
1500 virtual bool wi_op_overflows (wide_int &r,
1501 tree type,
1502 const wide_int &,
1503 const wide_int &) const = 0;
1505 // Calculate the cross product of two sets of sub-ranges and return it.
1506 void wi_cross_product (irange &r, tree type,
1507 const wide_int &lh_lb,
1508 const wide_int &lh_ub,
1509 const wide_int &rh_lb,
1510 const wide_int &rh_ub) const;
1513 // Calculate the cross product of two sets of ranges and return it.
1515 // Multiplications, divisions and shifts are a bit tricky to handle,
1516 // depending on the mix of signs we have in the two ranges, we need to
1517 // operate on different values to get the minimum and maximum values
1518 // for the new range. One approach is to figure out all the
1519 // variations of range combinations and do the operations.
1521 // However, this involves several calls to compare_values and it is
1522 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1523 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1524 // figure the smallest and largest values to form the new range.
1526 void
1527 cross_product_operator::wi_cross_product (irange &r, tree type,
1528 const wide_int &lh_lb,
1529 const wide_int &lh_ub,
1530 const wide_int &rh_lb,
1531 const wide_int &rh_ub) const
1533 wide_int cp1, cp2, cp3, cp4;
1534 // Default to varying.
1535 r.set_varying (type);
1537 // Compute the 4 cross operations, bailing if we get an overflow we
1538 // can't handle.
1539 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1540 return;
1541 if (wi::eq_p (lh_lb, lh_ub))
1542 cp3 = cp1;
1543 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1544 return;
1545 if (wi::eq_p (rh_lb, rh_ub))
1546 cp2 = cp1;
1547 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1548 return;
1549 if (wi::eq_p (lh_lb, lh_ub))
1550 cp4 = cp2;
1551 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1552 return;
1554 // Order pairs.
1555 signop sign = TYPE_SIGN (type);
1556 if (wi::gt_p (cp1, cp2, sign))
1557 std::swap (cp1, cp2);
1558 if (wi::gt_p (cp3, cp4, sign))
1559 std::swap (cp3, cp4);
1561 // Choose min and max from the ordered pairs.
1562 wide_int res_lb = wi::min (cp1, cp3, sign);
1563 wide_int res_ub = wi::max (cp2, cp4, sign);
1564 value_range_with_overflow (r, type, res_lb, res_ub);
1568 class operator_mult : public cross_product_operator
1570 public:
1571 virtual void wi_fold (irange &r, tree type,
1572 const wide_int &lh_lb,
1573 const wide_int &lh_ub,
1574 const wide_int &rh_lb,
1575 const wide_int &rh_ub) const;
1576 virtual bool wi_op_overflows (wide_int &res, tree type,
1577 const wide_int &w0, const wide_int &w1) const;
1578 virtual bool op1_range (irange &r, tree type,
1579 const irange &lhs,
1580 const irange &op2,
1581 relation_kind rel ATTRIBUTE_UNUSED) const;
1582 virtual bool op2_range (irange &r, tree type,
1583 const irange &lhs,
1584 const irange &op1,
1585 relation_kind rel ATTRIBUTE_UNUSED) const;
1586 } op_mult;
1588 bool
1589 operator_mult::op1_range (irange &r, tree type,
1590 const irange &lhs, const irange &op2,
1591 relation_kind rel ATTRIBUTE_UNUSED) const
1593 tree offset;
1595 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1596 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1597 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1598 if (TYPE_OVERFLOW_WRAPS (type))
1599 return false;
1601 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1602 return range_op_handler (TRUNC_DIV_EXPR, type)->fold_range (r, type,
1603 lhs, op2);
1604 return false;
1607 bool
1608 operator_mult::op2_range (irange &r, tree type,
1609 const irange &lhs, const irange &op1,
1610 relation_kind rel) const
1612 return operator_mult::op1_range (r, type, lhs, op1, rel);
1615 bool
1616 operator_mult::wi_op_overflows (wide_int &res, tree type,
1617 const wide_int &w0, const wide_int &w1) const
1619 wi::overflow_type overflow = wi::OVF_NONE;
1620 signop sign = TYPE_SIGN (type);
1621 res = wi::mul (w0, w1, sign, &overflow);
1622 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1624 // For multiplication, the sign of the overflow is given
1625 // by the comparison of the signs of the operands.
1626 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1627 res = wi::max_value (w0.get_precision (), sign);
1628 else
1629 res = wi::min_value (w0.get_precision (), sign);
1630 return false;
1632 return overflow;
1635 void
1636 operator_mult::wi_fold (irange &r, tree type,
1637 const wide_int &lh_lb, const wide_int &lh_ub,
1638 const wide_int &rh_lb, const wide_int &rh_ub) const
1640 if (TYPE_OVERFLOW_UNDEFINED (type))
1642 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1643 return;
1646 // Multiply the ranges when overflow wraps. This is basically fancy
1647 // code so we don't drop to varying with an unsigned
1648 // [-3,-1]*[-3,-1].
1650 // This test requires 2*prec bits if both operands are signed and
1651 // 2*prec + 2 bits if either is not. Therefore, extend the values
1652 // using the sign of the result to PREC2. From here on out,
1653 // everthing is just signed math no matter what the input types
1654 // were.
1656 signop sign = TYPE_SIGN (type);
1657 unsigned prec = TYPE_PRECISION (type);
1658 widest2_int min0 = widest2_int::from (lh_lb, sign);
1659 widest2_int max0 = widest2_int::from (lh_ub, sign);
1660 widest2_int min1 = widest2_int::from (rh_lb, sign);
1661 widest2_int max1 = widest2_int::from (rh_ub, sign);
1662 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1663 widest2_int size = sizem1 + 1;
1665 // Canonicalize the intervals.
1666 if (sign == UNSIGNED)
1668 if (wi::ltu_p (size, min0 + max0))
1670 min0 -= size;
1671 max0 -= size;
1673 if (wi::ltu_p (size, min1 + max1))
1675 min1 -= size;
1676 max1 -= size;
1680 // Sort the 4 products so that min is in prod0 and max is in
1681 // prod3.
1682 widest2_int prod0 = min0 * min1;
1683 widest2_int prod1 = min0 * max1;
1684 widest2_int prod2 = max0 * min1;
1685 widest2_int prod3 = max0 * max1;
1687 // min0min1 > max0max1
1688 if (prod0 > prod3)
1689 std::swap (prod0, prod3);
1691 // min0max1 > max0min1
1692 if (prod1 > prod2)
1693 std::swap (prod1, prod2);
1695 if (prod0 > prod1)
1696 std::swap (prod0, prod1);
1698 if (prod2 > prod3)
1699 std::swap (prod2, prod3);
1701 // diff = max - min
1702 prod2 = prod3 - prod0;
1703 if (wi::geu_p (prod2, sizem1))
1704 // The range covers all values.
1705 r.set_varying (type);
1706 else
1708 wide_int new_lb = wide_int::from (prod0, prec, sign);
1709 wide_int new_ub = wide_int::from (prod3, prec, sign);
1710 create_possibly_reversed_range (r, type, new_lb, new_ub);
1715 class operator_div : public cross_product_operator
1717 public:
1718 operator_div (enum tree_code c) { code = c; }
1719 virtual void wi_fold (irange &r, tree type,
1720 const wide_int &lh_lb,
1721 const wide_int &lh_ub,
1722 const wide_int &rh_lb,
1723 const wide_int &rh_ub) const;
1724 virtual bool wi_op_overflows (wide_int &res, tree type,
1725 const wide_int &, const wide_int &) const;
1726 private:
1727 enum tree_code code;
1730 bool
1731 operator_div::wi_op_overflows (wide_int &res, tree type,
1732 const wide_int &w0, const wide_int &w1) const
1734 if (w1 == 0)
1735 return true;
1737 wi::overflow_type overflow = wi::OVF_NONE;
1738 signop sign = TYPE_SIGN (type);
1740 switch (code)
1742 case EXACT_DIV_EXPR:
1743 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1744 // operator_exact_divide. No need to handle it here.
1745 gcc_unreachable ();
1746 break;
1747 case TRUNC_DIV_EXPR:
1748 res = wi::div_trunc (w0, w1, sign, &overflow);
1749 break;
1750 case FLOOR_DIV_EXPR:
1751 res = wi::div_floor (w0, w1, sign, &overflow);
1752 break;
1753 case ROUND_DIV_EXPR:
1754 res = wi::div_round (w0, w1, sign, &overflow);
1755 break;
1756 case CEIL_DIV_EXPR:
1757 res = wi::div_ceil (w0, w1, sign, &overflow);
1758 break;
1759 default:
1760 gcc_unreachable ();
1763 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1765 // For division, the only case is -INF / -1 = +INF.
1766 res = wi::max_value (w0.get_precision (), sign);
1767 return false;
1769 return overflow;
1772 void
1773 operator_div::wi_fold (irange &r, tree type,
1774 const wide_int &lh_lb, const wide_int &lh_ub,
1775 const wide_int &rh_lb, const wide_int &rh_ub) const
1777 const wide_int dividend_min = lh_lb;
1778 const wide_int dividend_max = lh_ub;
1779 const wide_int divisor_min = rh_lb;
1780 const wide_int divisor_max = rh_ub;
1781 signop sign = TYPE_SIGN (type);
1782 unsigned prec = TYPE_PRECISION (type);
1783 wide_int extra_min, extra_max;
1785 // If we know we won't divide by zero, just do the division.
1786 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1788 wi_cross_product (r, type, dividend_min, dividend_max,
1789 divisor_min, divisor_max);
1790 return;
1793 // If we're definitely dividing by zero, there's nothing to do.
1794 if (wi_zero_p (type, divisor_min, divisor_max))
1796 r.set_undefined ();
1797 return;
1800 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1801 // skip any division by zero.
1803 // First divide by the negative numbers, if any.
1804 if (wi::neg_p (divisor_min, sign))
1805 wi_cross_product (r, type, dividend_min, dividend_max,
1806 divisor_min, wi::minus_one (prec));
1807 else
1808 r.set_undefined ();
1810 // Then divide by the non-zero positive numbers, if any.
1811 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1813 int_range_max tmp;
1814 wi_cross_product (tmp, type, dividend_min, dividend_max,
1815 wi::one (prec), divisor_max);
1816 r.union_ (tmp);
1818 // We shouldn't still have undefined here.
1819 gcc_checking_assert (!r.undefined_p ());
1822 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1823 operator_div op_floor_div (FLOOR_DIV_EXPR);
1824 operator_div op_round_div (ROUND_DIV_EXPR);
1825 operator_div op_ceil_div (CEIL_DIV_EXPR);
1828 class operator_exact_divide : public operator_div
1830 public:
1831 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1832 virtual bool op1_range (irange &r, tree type,
1833 const irange &lhs,
1834 const irange &op2,
1835 relation_kind rel ATTRIBUTE_UNUSED) const;
1837 } op_exact_div;
1839 bool
1840 operator_exact_divide::op1_range (irange &r, tree type,
1841 const irange &lhs,
1842 const irange &op2,
1843 relation_kind rel ATTRIBUTE_UNUSED) const
1845 tree offset;
1846 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1847 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1848 // We wont bother trying to enumerate all the in between stuff :-P
1849 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1850 // the time however.
1851 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1852 if (op2.singleton_p (&offset)
1853 && !integer_zerop (offset))
1854 return range_op_handler (MULT_EXPR, type)->fold_range (r, type, lhs, op2);
1855 return false;
1859 class operator_lshift : public cross_product_operator
1861 public:
1862 virtual bool op1_range (irange &r, tree type,
1863 const irange &lhs,
1864 const irange &op2,
1865 relation_kind rel = VREL_NONE) const;
1866 virtual bool fold_range (irange &r, tree type,
1867 const irange &op1,
1868 const irange &op2,
1869 relation_kind rel = VREL_NONE) const;
1871 virtual void wi_fold (irange &r, tree type,
1872 const wide_int &lh_lb, const wide_int &lh_ub,
1873 const wide_int &rh_lb, const wide_int &rh_ub) const;
1874 virtual bool wi_op_overflows (wide_int &res,
1875 tree type,
1876 const wide_int &,
1877 const wide_int &) const;
1878 } op_lshift;
1880 class operator_rshift : public cross_product_operator
1882 public:
1883 virtual bool fold_range (irange &r, tree type,
1884 const irange &op1,
1885 const irange &op2,
1886 relation_kind rel = VREL_NONE) const;
1887 virtual void wi_fold (irange &r, tree type,
1888 const wide_int &lh_lb,
1889 const wide_int &lh_ub,
1890 const wide_int &rh_lb,
1891 const wide_int &rh_ub) const;
1892 virtual bool wi_op_overflows (wide_int &res,
1893 tree type,
1894 const wide_int &w0,
1895 const wide_int &w1) const;
1896 virtual bool op1_range (irange &, tree type,
1897 const irange &lhs,
1898 const irange &op2,
1899 relation_kind rel = VREL_NONE) const;
1900 virtual enum tree_code lhs_op1_relation (const irange &lhs,
1901 const irange &op1,
1902 const irange &op2) const;
1903 } op_rshift;
1906 enum tree_code
1907 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
1908 const irange &op1,
1909 const irange &op2) const
1911 // If both operands range are >= 0, then the LHS <= op1.
1912 if (!op1.undefined_p () && !op2.undefined_p ()
1913 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
1914 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
1915 return LE_EXPR;
1916 return VREL_NONE;
1919 bool
1920 operator_lshift::fold_range (irange &r, tree type,
1921 const irange &op1,
1922 const irange &op2,
1923 relation_kind rel) const
1925 int_range_max shift_range;
1926 if (!get_shift_range (shift_range, type, op2))
1928 if (op2.undefined_p ())
1929 r.set_undefined ();
1930 else
1931 r.set_varying (type);
1932 return true;
1935 // Transform left shifts by constants into multiplies.
1936 if (shift_range.singleton_p ())
1938 unsigned shift = shift_range.lower_bound ().to_uhwi ();
1939 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
1940 int_range<1> mult (type, tmp, tmp);
1942 // Force wrapping multiplication.
1943 bool saved_flag_wrapv = flag_wrapv;
1944 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
1945 flag_wrapv = 1;
1946 flag_wrapv_pointer = 1;
1947 bool b = op_mult.fold_range (r, type, op1, mult);
1948 flag_wrapv = saved_flag_wrapv;
1949 flag_wrapv_pointer = saved_flag_wrapv_pointer;
1950 return b;
1952 else
1953 // Otherwise, invoke the generic fold routine.
1954 return range_operator::fold_range (r, type, op1, shift_range, rel);
1957 void
1958 operator_lshift::wi_fold (irange &r, tree type,
1959 const wide_int &lh_lb, const wide_int &lh_ub,
1960 const wide_int &rh_lb, const wide_int &rh_ub) const
1962 signop sign = TYPE_SIGN (type);
1963 unsigned prec = TYPE_PRECISION (type);
1964 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
1965 int bound_shift = overflow_pos - rh_ub.to_shwi ();
1966 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1967 // overflow. However, for that to happen, rh.max needs to be zero,
1968 // which means rh is a singleton range of zero, which means we simply return
1969 // [lh_lb, lh_ub] as the range.
1970 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
1972 r = int_range<2> (type, lh_lb, lh_ub);
1973 return;
1976 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
1977 wide_int complement = ~(bound - 1);
1978 wide_int low_bound, high_bound;
1979 bool in_bounds = false;
1981 if (sign == UNSIGNED)
1983 low_bound = bound;
1984 high_bound = complement;
1985 if (wi::ltu_p (lh_ub, low_bound))
1987 // [5, 6] << [1, 2] == [10, 24].
1988 // We're shifting out only zeroes, the value increases
1989 // monotonically.
1990 in_bounds = true;
1992 else if (wi::ltu_p (high_bound, lh_lb))
1994 // [0xffffff00, 0xffffffff] << [1, 2]
1995 // == [0xfffffc00, 0xfffffffe].
1996 // We're shifting out only ones, the value decreases
1997 // monotonically.
1998 in_bounds = true;
2001 else
2003 // [-1, 1] << [1, 2] == [-4, 4]
2004 low_bound = complement;
2005 high_bound = bound;
2006 if (wi::lts_p (lh_ub, high_bound)
2007 && wi::lts_p (low_bound, lh_lb))
2009 // For non-negative numbers, we're shifting out only zeroes,
2010 // the value increases monotonically. For negative numbers,
2011 // we're shifting out only ones, the value decreases
2012 // monotonically.
2013 in_bounds = true;
2017 if (in_bounds)
2018 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2019 else
2020 r.set_varying (type);
2023 bool
2024 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2025 const wide_int &w0, const wide_int &w1) const
2027 signop sign = TYPE_SIGN (type);
2028 if (wi::neg_p (w1))
2030 // It's unclear from the C standard whether shifts can overflow.
2031 // The following code ignores overflow; perhaps a C standard
2032 // interpretation ruling is needed.
2033 res = wi::rshift (w0, -w1, sign);
2035 else
2036 res = wi::lshift (w0, w1);
2037 return false;
2040 bool
2041 operator_lshift::op1_range (irange &r,
2042 tree type,
2043 const irange &lhs,
2044 const irange &op2,
2045 relation_kind rel ATTRIBUTE_UNUSED) const
2047 tree shift_amount;
2049 if (!lhs.contains_p (build_zero_cst (type)))
2050 r.set_nonzero (type);
2051 else
2052 r.set_varying (type);
2054 if (op2.singleton_p (&shift_amount))
2056 wide_int shift = wi::to_wide (shift_amount);
2057 if (wi::lt_p (shift, 0, SIGNED))
2058 return false;
2059 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2060 TYPE_PRECISION (op2.type ())),
2061 UNSIGNED))
2062 return false;
2063 if (shift == 0)
2065 r.intersect (lhs);
2066 return true;
2069 // Work completely in unsigned mode to start.
2070 tree utype = type;
2071 int_range_max tmp_range;
2072 if (TYPE_SIGN (type) == SIGNED)
2074 int_range_max tmp = lhs;
2075 utype = unsigned_type_for (type);
2076 range_cast (tmp, utype);
2077 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2079 else
2080 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2082 // Start with ranges which can produce the LHS by right shifting the
2083 // result by the shift amount.
2084 // ie [0x08, 0xF0] = op1 << 2 will start with
2085 // [00001000, 11110000] = op1 << 2
2086 // [0x02, 0x4C] aka [00000010, 00111100]
2088 // Then create a range from the LB with the least significant upper bit
2089 // set, to the upper bound with all the bits set.
2090 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2092 // Ideally we do this for each subrange, but just lump them all for now.
2093 unsigned low_bits = TYPE_PRECISION (utype)
2094 - TREE_INT_CST_LOW (shift_amount);
2095 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2096 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2097 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2098 int_range<2> fill_range (utype, new_lb, new_ub);
2099 tmp_range.union_ (fill_range);
2101 if (utype != type)
2102 range_cast (tmp_range, type);
2104 r.intersect (tmp_range);
2105 return true;
2108 return !r.varying_p ();
2111 bool
2112 operator_rshift::op1_range (irange &r,
2113 tree type,
2114 const irange &lhs,
2115 const irange &op2,
2116 relation_kind rel ATTRIBUTE_UNUSED) const
2118 tree shift;
2119 if (op2.singleton_p (&shift))
2121 // Ignore nonsensical shifts.
2122 unsigned prec = TYPE_PRECISION (type);
2123 if (wi::ge_p (wi::to_wide (shift),
2124 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2125 UNSIGNED))
2126 return false;
2127 if (wi::to_wide (shift) == 0)
2129 r = lhs;
2130 return true;
2133 // Folding the original operation may discard some impossible
2134 // ranges from the LHS.
2135 int_range_max lhs_refined;
2136 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2137 lhs_refined.intersect (lhs);
2138 if (lhs_refined.undefined_p ())
2140 r.set_undefined ();
2141 return true;
2143 int_range_max shift_range (shift, shift);
2144 int_range_max lb, ub;
2145 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2146 // LHS
2147 // 0000 0111 = OP1 >> 3
2149 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2150 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2151 // right hand side (0x07).
2152 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2153 fold_build2 (LSHIFT_EXPR, type,
2154 build_minus_one_cst (type),
2155 shift));
2156 int_range_max mask_range (build_zero_cst (type), mask);
2157 op_plus.fold_range (ub, type, lb, mask_range);
2158 r = lb;
2159 r.union_ (ub);
2160 if (!lhs_refined.contains_p (build_zero_cst (type)))
2162 mask_range.invert ();
2163 r.intersect (mask_range);
2165 return true;
2167 return false;
2170 bool
2171 operator_rshift::wi_op_overflows (wide_int &res,
2172 tree type,
2173 const wide_int &w0,
2174 const wide_int &w1) const
2176 signop sign = TYPE_SIGN (type);
2177 if (wi::neg_p (w1))
2178 res = wi::lshift (w0, -w1);
2179 else
2181 // It's unclear from the C standard whether shifts can overflow.
2182 // The following code ignores overflow; perhaps a C standard
2183 // interpretation ruling is needed.
2184 res = wi::rshift (w0, w1, sign);
2186 return false;
2189 bool
2190 operator_rshift::fold_range (irange &r, tree type,
2191 const irange &op1,
2192 const irange &op2,
2193 relation_kind rel) const
2195 int_range_max shift;
2196 if (!get_shift_range (shift, type, op2))
2198 if (op2.undefined_p ())
2199 r.set_undefined ();
2200 else
2201 r.set_varying (type);
2202 return true;
2205 return range_operator::fold_range (r, type, op1, shift, rel);
2208 void
2209 operator_rshift::wi_fold (irange &r, tree type,
2210 const wide_int &lh_lb, const wide_int &lh_ub,
2211 const wide_int &rh_lb, const wide_int &rh_ub) const
2213 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2217 class operator_cast: public range_operator
2219 public:
2220 virtual bool fold_range (irange &r, tree type,
2221 const irange &op1,
2222 const irange &op2,
2223 relation_kind rel = VREL_NONE) const;
2224 virtual bool op1_range (irange &r, tree type,
2225 const irange &lhs,
2226 const irange &op2,
2227 relation_kind rel = VREL_NONE) const;
2228 private:
2229 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2230 bool inside_domain_p (const wide_int &min, const wide_int &max,
2231 const irange &outer) const;
2232 void fold_pair (irange &r, unsigned index, const irange &inner,
2233 const irange &outer) const;
2234 } op_convert;
2236 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2238 inline bool
2239 operator_cast::truncating_cast_p (const irange &inner,
2240 const irange &outer) const
2242 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2245 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2247 bool
2248 operator_cast::inside_domain_p (const wide_int &min,
2249 const wide_int &max,
2250 const irange &range) const
2252 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2253 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2254 signop domain_sign = TYPE_SIGN (range.type ());
2255 return (wi::le_p (min, domain_max, domain_sign)
2256 && wi::le_p (max, domain_max, domain_sign)
2257 && wi::ge_p (min, domain_min, domain_sign)
2258 && wi::ge_p (max, domain_min, domain_sign));
2262 // Helper for fold_range which work on a pair at a time.
2264 void
2265 operator_cast::fold_pair (irange &r, unsigned index,
2266 const irange &inner,
2267 const irange &outer) const
2269 tree inner_type = inner.type ();
2270 tree outer_type = outer.type ();
2271 signop inner_sign = TYPE_SIGN (inner_type);
2272 unsigned outer_prec = TYPE_PRECISION (outer_type);
2274 // check to see if casting from INNER to OUTER is a conversion that
2275 // fits in the resulting OUTER type.
2276 wide_int inner_lb = inner.lower_bound (index);
2277 wide_int inner_ub = inner.upper_bound (index);
2278 if (truncating_cast_p (inner, outer))
2280 // We may be able to accomodate a truncating cast if the
2281 // resulting range can be represented in the target type...
2282 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2283 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2284 inner_sign) != 0)
2286 r.set_varying (outer_type);
2287 return;
2290 // ...but we must still verify that the final range fits in the
2291 // domain. This catches -fstrict-enum restrictions where the domain
2292 // range is smaller than what fits in the underlying type.
2293 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2294 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2295 if (inside_domain_p (min, max, outer))
2296 create_possibly_reversed_range (r, outer_type, min, max);
2297 else
2298 r.set_varying (outer_type);
2302 bool
2303 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2304 const irange &inner,
2305 const irange &outer,
2306 relation_kind rel ATTRIBUTE_UNUSED) const
2308 if (empty_range_varying (r, type, inner, outer))
2309 return true;
2311 gcc_checking_assert (outer.varying_p ());
2312 gcc_checking_assert (inner.num_pairs () > 0);
2314 // Avoid a temporary by folding the first pair directly into the result.
2315 fold_pair (r, 0, inner, outer);
2317 // Then process any additonal pairs by unioning with their results.
2318 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2320 int_range_max tmp;
2321 fold_pair (tmp, x, inner, outer);
2322 r.union_ (tmp);
2323 if (r.varying_p ())
2324 return true;
2326 return true;
2329 bool
2330 operator_cast::op1_range (irange &r, tree type,
2331 const irange &lhs,
2332 const irange &op2,
2333 relation_kind rel ATTRIBUTE_UNUSED) const
2335 tree lhs_type = lhs.type ();
2336 gcc_checking_assert (types_compatible_p (op2.type(), type));
2338 // If we are calculating a pointer, shortcut to what we really care about.
2339 if (POINTER_TYPE_P (type))
2341 // Conversion from other pointers or a constant (including 0/NULL)
2342 // are straightforward.
2343 if (POINTER_TYPE_P (lhs.type ())
2344 || (lhs.singleton_p ()
2345 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2347 r = lhs;
2348 range_cast (r, type);
2350 else
2352 // If the LHS is not a pointer nor a singleton, then it is
2353 // either VARYING or non-zero.
2354 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2355 r.set_nonzero (type);
2356 else
2357 r.set_varying (type);
2359 r.intersect (op2);
2360 return true;
2363 if (truncating_cast_p (op2, lhs))
2365 if (lhs.varying_p ())
2366 r.set_varying (type);
2367 else
2369 // We want to insert the LHS as an unsigned value since it
2370 // would not trigger the signed bit of the larger type.
2371 int_range_max converted_lhs = lhs;
2372 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2373 range_cast (converted_lhs, type);
2374 // Start by building the positive signed outer range for the type.
2375 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2376 TYPE_PRECISION (type));
2377 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2378 SIGNED));
2379 // For the signed part, we need to simply union the 2 ranges now.
2380 r.union_ (converted_lhs);
2382 // Create maximal negative number outside of LHS bits.
2383 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2384 TYPE_PRECISION (type));
2385 // Add this to the unsigned LHS range(s).
2386 int_range_max lim_range (type, lim, lim);
2387 int_range_max lhs_neg;
2388 range_op_handler (PLUS_EXPR, type)->fold_range (lhs_neg,
2389 type,
2390 converted_lhs,
2391 lim_range);
2392 // lhs_neg now has all the negative versions of the LHS.
2393 // Now union in all the values from SIGNED MIN (0x80000) to
2394 // lim-1 in order to fill in all the ranges with the upper
2395 // bits set.
2397 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2398 // we don't need to create a range from min to lim-1
2399 // calculate neg range traps trying to create [lim, lim - 1].
2400 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2401 if (lim != min_val)
2403 int_range_max neg (type,
2404 wi::min_value (TYPE_PRECISION (type),
2405 SIGNED),
2406 lim - 1);
2407 lhs_neg.union_ (neg);
2409 // And finally, munge the signed and unsigned portions.
2410 r.union_ (lhs_neg);
2412 // And intersect with any known value passed in the extra operand.
2413 r.intersect (op2);
2414 return true;
2417 int_range_max tmp;
2418 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2419 tmp = lhs;
2420 else
2422 // The cast is not truncating, and the range is restricted to
2423 // the range of the RHS by this assignment.
2425 // Cast the range of the RHS to the type of the LHS.
2426 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2427 // Intersect this with the LHS range will produce the range,
2428 // which will be cast to the RHS type before returning.
2429 tmp.intersect (lhs);
2432 // Cast the calculated range to the type of the RHS.
2433 fold_range (r, type, tmp, int_range<1> (type));
2434 return true;
2438 class operator_logical_and : public range_operator
2440 public:
2441 virtual bool fold_range (irange &r, tree type,
2442 const irange &lh,
2443 const irange &rh,
2444 relation_kind rel = VREL_NONE) const;
2445 virtual bool op1_range (irange &r, tree type,
2446 const irange &lhs,
2447 const irange &op2,
2448 relation_kind rel = VREL_NONE) const;
2449 virtual bool op2_range (irange &r, tree type,
2450 const irange &lhs,
2451 const irange &op1,
2452 relation_kind rel = VREL_NONE) const;
2453 } op_logical_and;
2456 bool
2457 operator_logical_and::fold_range (irange &r, tree type,
2458 const irange &lh,
2459 const irange &rh,
2460 relation_kind rel ATTRIBUTE_UNUSED) const
2462 if (empty_range_varying (r, type, lh, rh))
2463 return true;
2465 // 0 && anything is 0.
2466 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2467 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2468 r = range_false (type);
2469 else if (lh.contains_p (build_zero_cst (lh.type ()))
2470 || rh.contains_p (build_zero_cst (rh.type ())))
2471 // To reach this point, there must be a logical 1 on each side, and
2472 // the only remaining question is whether there is a zero or not.
2473 r = range_true_and_false (type);
2474 else
2475 r = range_true (type);
2476 return true;
2479 bool
2480 operator_logical_and::op1_range (irange &r, tree type,
2481 const irange &lhs,
2482 const irange &op2 ATTRIBUTE_UNUSED,
2483 relation_kind rel ATTRIBUTE_UNUSED) const
2485 switch (get_bool_state (r, lhs, type))
2487 case BRS_TRUE:
2488 // A true result means both sides of the AND must be true.
2489 r = range_true (type);
2490 break;
2491 default:
2492 // Any other result means only one side has to be false, the
2493 // other side can be anything. So we cannot be sure of any
2494 // result here.
2495 r = range_true_and_false (type);
2496 break;
2498 return true;
2501 bool
2502 operator_logical_and::op2_range (irange &r, tree type,
2503 const irange &lhs,
2504 const irange &op1,
2505 relation_kind rel ATTRIBUTE_UNUSED) const
2507 return operator_logical_and::op1_range (r, type, lhs, op1);
2511 class operator_bitwise_and : public range_operator
2513 public:
2514 virtual bool fold_range (irange &r, tree type,
2515 const irange &lh,
2516 const irange &rh,
2517 relation_kind rel = VREL_NONE) const;
2518 virtual bool op1_range (irange &r, tree type,
2519 const irange &lhs,
2520 const irange &op2,
2521 relation_kind rel = VREL_NONE) const;
2522 virtual bool op2_range (irange &r, tree type,
2523 const irange &lhs,
2524 const irange &op1,
2525 relation_kind rel = VREL_NONE) const;
2526 virtual void wi_fold (irange &r, tree type,
2527 const wide_int &lh_lb,
2528 const wide_int &lh_ub,
2529 const wide_int &rh_lb,
2530 const wide_int &rh_ub) const;
2531 private:
2532 void simple_op1_range_solver (irange &r, tree type,
2533 const irange &lhs,
2534 const irange &op2) const;
2535 void remove_impossible_ranges (irange &r, const irange &rh) const;
2536 } op_bitwise_and;
2538 static bool
2539 unsigned_singleton_p (const irange &op)
2541 tree mask;
2542 if (op.singleton_p (&mask))
2544 wide_int x = wi::to_wide (mask);
2545 return wi::ge_p (x, 0, TYPE_SIGN (op.type ()));
2547 return false;
2550 // Remove any ranges from R that are known to be impossible when an
2551 // range is ANDed with MASK.
2553 void
2554 operator_bitwise_and::remove_impossible_ranges (irange &r,
2555 const irange &rmask) const
2557 if (r.undefined_p () || !unsigned_singleton_p (rmask))
2558 return;
2560 wide_int mask = rmask.lower_bound ();
2561 tree type = r.type ();
2562 int prec = TYPE_PRECISION (type);
2563 int leading_zeros = wi::clz (mask);
2564 int_range_max impossible_ranges;
2566 /* We know that starting at the most significant bit, any 0 in the
2567 mask means the resulting range cannot contain a 1 in that same
2568 position. This means the following ranges are impossible:
2570 x & 0b1001 1010
2571 IMPOSSIBLE RANGES
2572 01xx xxxx [0100 0000, 0111 1111]
2573 001x xxxx [0010 0000, 0011 1111]
2574 0000 01xx [0000 0100, 0000 0111]
2575 0000 0001 [0000 0001, 0000 0001]
2577 wide_int one = wi::one (prec);
2578 for (int i = 0; i < prec - leading_zeros - 1; ++i)
2579 if (wi::bit_and (mask, wi::lshift (one, wi::uhwi (i, prec))) == 0)
2581 tree lb = fold_build2 (LSHIFT_EXPR, type,
2582 build_one_cst (type),
2583 build_int_cst (type, i));
2584 tree ub_left = fold_build1 (BIT_NOT_EXPR, type,
2585 fold_build2 (LSHIFT_EXPR, type,
2586 build_minus_one_cst (type),
2587 build_int_cst (type, i)));
2588 tree ub_right = fold_build2 (LSHIFT_EXPR, type,
2589 build_one_cst (type),
2590 build_int_cst (type, i));
2591 tree ub = fold_build2 (BIT_IOR_EXPR, type, ub_left, ub_right);
2592 impossible_ranges.union_ (int_range<1> (lb, ub));
2594 if (!impossible_ranges.undefined_p ())
2596 impossible_ranges.invert ();
2597 r.intersect (impossible_ranges);
2601 bool
2602 operator_bitwise_and::fold_range (irange &r, tree type,
2603 const irange &lh,
2604 const irange &rh,
2605 relation_kind rel ATTRIBUTE_UNUSED) const
2607 if (range_operator::fold_range (r, type, lh, rh))
2609 // FIXME: This is temporarily disabled because, though it
2610 // generates better ranges, it's noticeably slower for evrp.
2611 // remove_impossible_ranges (r, rh);
2612 return true;
2614 return false;
2618 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2619 // by considering the number of leading redundant sign bit copies.
2620 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2621 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2622 static bool
2623 wi_optimize_signed_bitwise_op (irange &r, tree type,
2624 const wide_int &lh_lb, const wide_int &lh_ub,
2625 const wide_int &rh_lb, const wide_int &rh_ub)
2627 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2628 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2629 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2630 if (new_clrsb == 0)
2631 return false;
2632 int type_prec = TYPE_PRECISION (type);
2633 int rprec = (type_prec - new_clrsb) - 1;
2634 value_range_with_overflow (r, type,
2635 wi::mask (rprec, true, type_prec),
2636 wi::mask (rprec, false, type_prec));
2637 return true;
2641 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2642 // possible. Basically, see if we can optimize:
2644 // [LB, UB] op Z
2645 // into:
2646 // [LB op Z, UB op Z]
2648 // If the optimization was successful, accumulate the range in R and
2649 // return TRUE.
2651 static bool
2652 wi_optimize_and_or (irange &r,
2653 enum tree_code code,
2654 tree type,
2655 const wide_int &lh_lb, const wide_int &lh_ub,
2656 const wide_int &rh_lb, const wide_int &rh_ub)
2658 // Calculate the singleton mask among the ranges, if any.
2659 wide_int lower_bound, upper_bound, mask;
2660 if (wi::eq_p (rh_lb, rh_ub))
2662 mask = rh_lb;
2663 lower_bound = lh_lb;
2664 upper_bound = lh_ub;
2666 else if (wi::eq_p (lh_lb, lh_ub))
2668 mask = lh_lb;
2669 lower_bound = rh_lb;
2670 upper_bound = rh_ub;
2672 else
2673 return false;
2675 // If Z is a constant which (for op | its bitwise not) has n
2676 // consecutive least significant bits cleared followed by m 1
2677 // consecutive bits set immediately above it and either
2678 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2680 // The least significant n bits of all the values in the range are
2681 // cleared or set, the m bits above it are preserved and any bits
2682 // above these are required to be the same for all values in the
2683 // range.
2684 wide_int w = mask;
2685 int m = 0, n = 0;
2686 if (code == BIT_IOR_EXPR)
2687 w = ~w;
2688 if (wi::eq_p (w, 0))
2689 n = w.get_precision ();
2690 else
2692 n = wi::ctz (w);
2693 w = ~(w | wi::mask (n, false, w.get_precision ()));
2694 if (wi::eq_p (w, 0))
2695 m = w.get_precision () - n;
2696 else
2697 m = wi::ctz (w) - n;
2699 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2700 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2701 return false;
2703 wide_int res_lb, res_ub;
2704 if (code == BIT_AND_EXPR)
2706 res_lb = wi::bit_and (lower_bound, mask);
2707 res_ub = wi::bit_and (upper_bound, mask);
2709 else if (code == BIT_IOR_EXPR)
2711 res_lb = wi::bit_or (lower_bound, mask);
2712 res_ub = wi::bit_or (upper_bound, mask);
2714 else
2715 gcc_unreachable ();
2716 value_range_with_overflow (r, type, res_lb, res_ub);
2718 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2719 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2721 int_range<2> tmp;
2722 tmp.set_nonzero (type);
2723 r.intersect (tmp);
2725 return true;
2728 // For range [LB, UB] compute two wide_int bit masks.
2730 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2731 // for all numbers in the range the bit is 0, otherwise it might be 0
2732 // or 1.
2734 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2735 // for all numbers in the range the bit is 1, otherwise it might be 0
2736 // or 1.
2738 void
2739 wi_set_zero_nonzero_bits (tree type,
2740 const wide_int &lb, const wide_int &ub,
2741 wide_int &maybe_nonzero,
2742 wide_int &mustbe_nonzero)
2744 signop sign = TYPE_SIGN (type);
2746 if (wi::eq_p (lb, ub))
2747 maybe_nonzero = mustbe_nonzero = lb;
2748 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2750 wide_int xor_mask = lb ^ ub;
2751 maybe_nonzero = lb | ub;
2752 mustbe_nonzero = lb & ub;
2753 if (xor_mask != 0)
2755 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2756 maybe_nonzero.get_precision ());
2757 maybe_nonzero = maybe_nonzero | mask;
2758 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2761 else
2763 maybe_nonzero = wi::minus_one (lb.get_precision ());
2764 mustbe_nonzero = wi::zero (lb.get_precision ());
2768 void
2769 operator_bitwise_and::wi_fold (irange &r, tree type,
2770 const wide_int &lh_lb,
2771 const wide_int &lh_ub,
2772 const wide_int &rh_lb,
2773 const wide_int &rh_ub) const
2775 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2776 return;
2778 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2779 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2780 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2781 maybe_nonzero_lh, mustbe_nonzero_lh);
2782 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2783 maybe_nonzero_rh, mustbe_nonzero_rh);
2785 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2786 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2787 signop sign = TYPE_SIGN (type);
2788 unsigned prec = TYPE_PRECISION (type);
2789 // If both input ranges contain only negative values, we can
2790 // truncate the result range maximum to the minimum of the
2791 // input range maxima.
2792 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2794 new_ub = wi::min (new_ub, lh_ub, sign);
2795 new_ub = wi::min (new_ub, rh_ub, sign);
2797 // If either input range contains only non-negative values
2798 // we can truncate the result range maximum to the respective
2799 // maximum of the input range.
2800 if (wi::ge_p (lh_lb, 0, sign))
2801 new_ub = wi::min (new_ub, lh_ub, sign);
2802 if (wi::ge_p (rh_lb, 0, sign))
2803 new_ub = wi::min (new_ub, rh_ub, sign);
2804 // PR68217: In case of signed & sign-bit-CST should
2805 // result in [-INF, 0] instead of [-INF, INF].
2806 if (wi::gt_p (new_lb, new_ub, sign))
2808 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2809 if (sign == SIGNED
2810 && ((wi::eq_p (lh_lb, lh_ub)
2811 && !wi::cmps (lh_lb, sign_bit))
2812 || (wi::eq_p (rh_lb, rh_ub)
2813 && !wi::cmps (rh_lb, sign_bit))))
2815 new_lb = wi::min_value (prec, sign);
2816 new_ub = wi::zero (prec);
2819 // If the limits got swapped around, return varying.
2820 if (wi::gt_p (new_lb, new_ub,sign))
2822 if (sign == SIGNED
2823 && wi_optimize_signed_bitwise_op (r, type,
2824 lh_lb, lh_ub,
2825 rh_lb, rh_ub))
2826 return;
2827 r.set_varying (type);
2829 else
2830 value_range_with_overflow (r, type, new_lb, new_ub);
2833 static void
2834 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2836 if (!lhs.contains_p (build_zero_cst (type)))
2837 r = range_nonzero (type);
2838 else
2839 r.set_varying (type);
2842 // This was shamelessly stolen from register_edge_assert_for_2 and
2843 // adjusted to work with iranges.
2845 void
2846 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2847 const irange &lhs,
2848 const irange &op2) const
2850 if (!op2.singleton_p ())
2852 set_nonzero_range_from_mask (r, type, lhs);
2853 return;
2855 unsigned int nprec = TYPE_PRECISION (type);
2856 wide_int cst2v = op2.lower_bound ();
2857 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2858 wide_int sgnbit;
2859 if (cst2n)
2860 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2861 else
2862 sgnbit = wi::zero (nprec);
2864 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2866 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2867 // maximum unsigned value is ~0. For signed comparison, if CST2
2868 // doesn't have the most significant bit set, handle it similarly. If
2869 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2870 wide_int valv = lhs.lower_bound ();
2871 wide_int minv = valv & cst2v, maxv;
2872 bool we_know_nothing = false;
2873 if (minv != valv)
2875 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2876 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2877 if (minv == valv)
2879 // If we can't determine anything on this bound, fall
2880 // through and conservatively solve for the other end point.
2881 we_know_nothing = true;
2884 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2885 if (we_know_nothing)
2886 r.set_varying (type);
2887 else
2888 r = int_range<1> (type, minv, maxv);
2890 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2892 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2893 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2894 // VAL2 where
2895 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2896 // as maximum.
2897 // For signed comparison, if CST2 doesn't have most significant bit
2898 // set, handle it similarly. If CST2 has MSB set, the maximum is
2899 // the same and minimum is INT_MIN.
2900 valv = lhs.upper_bound ();
2901 minv = valv & cst2v;
2902 if (minv == valv)
2903 maxv = valv;
2904 else
2906 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2907 if (maxv == valv)
2909 // If we couldn't determine anything on either bound, return
2910 // undefined.
2911 if (we_know_nothing)
2912 r.set_undefined ();
2913 return;
2915 maxv -= 1;
2917 maxv |= ~cst2v;
2918 minv = sgnbit;
2919 int_range<1> upper_bits (type, minv, maxv);
2920 r.intersect (upper_bits);
2923 bool
2924 operator_bitwise_and::op1_range (irange &r, tree type,
2925 const irange &lhs,
2926 const irange &op2,
2927 relation_kind rel ATTRIBUTE_UNUSED) const
2929 if (types_compatible_p (type, boolean_type_node))
2930 return op_logical_and.op1_range (r, type, lhs, op2);
2932 r.set_undefined ();
2933 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
2935 int_range_max chunk (lhs.type (),
2936 lhs.lower_bound (i),
2937 lhs.upper_bound (i));
2938 int_range_max res;
2939 simple_op1_range_solver (res, type, chunk, op2);
2940 r.union_ (res);
2942 if (r.undefined_p ())
2943 set_nonzero_range_from_mask (r, type, lhs);
2944 return true;
2947 bool
2948 operator_bitwise_and::op2_range (irange &r, tree type,
2949 const irange &lhs,
2950 const irange &op1,
2951 relation_kind rel ATTRIBUTE_UNUSED) const
2953 return operator_bitwise_and::op1_range (r, type, lhs, op1);
2957 class operator_logical_or : public range_operator
2959 public:
2960 virtual bool fold_range (irange &r, tree type,
2961 const irange &lh,
2962 const irange &rh,
2963 relation_kind rel = VREL_NONE) const;
2964 virtual bool op1_range (irange &r, tree type,
2965 const irange &lhs,
2966 const irange &op2,
2967 relation_kind rel = VREL_NONE) const;
2968 virtual bool op2_range (irange &r, tree type,
2969 const irange &lhs,
2970 const irange &op1,
2971 relation_kind rel = VREL_NONE) const;
2972 } op_logical_or;
2974 bool
2975 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2976 const irange &lh,
2977 const irange &rh,
2978 relation_kind rel ATTRIBUTE_UNUSED) const
2980 if (empty_range_varying (r, type, lh, rh))
2981 return true;
2983 r = lh;
2984 r.union_ (rh);
2985 return true;
2988 bool
2989 operator_logical_or::op1_range (irange &r, tree type,
2990 const irange &lhs,
2991 const irange &op2 ATTRIBUTE_UNUSED,
2992 relation_kind rel ATTRIBUTE_UNUSED) const
2994 switch (get_bool_state (r, lhs, type))
2996 case BRS_FALSE:
2997 // A false result means both sides of the OR must be false.
2998 r = range_false (type);
2999 break;
3000 default:
3001 // Any other result means only one side has to be true, the
3002 // other side can be anything. so we can't be sure of any result
3003 // here.
3004 r = range_true_and_false (type);
3005 break;
3007 return true;
3010 bool
3011 operator_logical_or::op2_range (irange &r, tree type,
3012 const irange &lhs,
3013 const irange &op1,
3014 relation_kind rel ATTRIBUTE_UNUSED) const
3016 return operator_logical_or::op1_range (r, type, lhs, op1);
3020 class operator_bitwise_or : public range_operator
3022 public:
3023 virtual bool op1_range (irange &r, tree type,
3024 const irange &lhs,
3025 const irange &op2,
3026 relation_kind rel = VREL_NONE) const;
3027 virtual bool op2_range (irange &r, tree type,
3028 const irange &lhs,
3029 const irange &op1,
3030 relation_kind rel= VREL_NONE) const;
3031 virtual void wi_fold (irange &r, tree type,
3032 const wide_int &lh_lb,
3033 const wide_int &lh_ub,
3034 const wide_int &rh_lb,
3035 const wide_int &rh_ub) const;
3036 } op_bitwise_or;
3038 void
3039 operator_bitwise_or::wi_fold (irange &r, tree type,
3040 const wide_int &lh_lb,
3041 const wide_int &lh_ub,
3042 const wide_int &rh_lb,
3043 const wide_int &rh_ub) const
3045 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3046 return;
3048 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3049 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3050 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3051 maybe_nonzero_lh, mustbe_nonzero_lh);
3052 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3053 maybe_nonzero_rh, mustbe_nonzero_rh);
3054 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3055 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3056 signop sign = TYPE_SIGN (type);
3057 // If the input ranges contain only positive values we can
3058 // truncate the minimum of the result range to the maximum
3059 // of the input range minima.
3060 if (wi::ge_p (lh_lb, 0, sign)
3061 && wi::ge_p (rh_lb, 0, sign))
3063 new_lb = wi::max (new_lb, lh_lb, sign);
3064 new_lb = wi::max (new_lb, rh_lb, sign);
3066 // If either input range contains only negative values
3067 // we can truncate the minimum of the result range to the
3068 // respective minimum range.
3069 if (wi::lt_p (lh_ub, 0, sign))
3070 new_lb = wi::max (new_lb, lh_lb, sign);
3071 if (wi::lt_p (rh_ub, 0, sign))
3072 new_lb = wi::max (new_lb, rh_lb, sign);
3073 // If the limits got swapped around, return a conservative range.
3074 if (wi::gt_p (new_lb, new_ub, sign))
3076 // Make sure that nonzero|X is nonzero.
3077 if (wi::gt_p (lh_lb, 0, sign)
3078 || wi::gt_p (rh_lb, 0, sign)
3079 || wi::lt_p (lh_ub, 0, sign)
3080 || wi::lt_p (rh_ub, 0, sign))
3081 r.set_nonzero (type);
3082 else if (sign == SIGNED
3083 && wi_optimize_signed_bitwise_op (r, type,
3084 lh_lb, lh_ub,
3085 rh_lb, rh_ub))
3086 return;
3087 else
3088 r.set_varying (type);
3089 return;
3091 value_range_with_overflow (r, type, new_lb, new_ub);
3094 bool
3095 operator_bitwise_or::op1_range (irange &r, tree type,
3096 const irange &lhs,
3097 const irange &op2,
3098 relation_kind rel ATTRIBUTE_UNUSED) const
3100 // If this is really a logical wi_fold, call that.
3101 if (types_compatible_p (type, boolean_type_node))
3102 return op_logical_or.op1_range (r, type, lhs, op2);
3104 if (lhs.zero_p ())
3106 tree zero = build_zero_cst (type);
3107 r = int_range<1> (zero, zero);
3108 return true;
3110 r.set_varying (type);
3111 return true;
3114 bool
3115 operator_bitwise_or::op2_range (irange &r, tree type,
3116 const irange &lhs,
3117 const irange &op1,
3118 relation_kind rel ATTRIBUTE_UNUSED) const
3120 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3124 class operator_bitwise_xor : public range_operator
3126 public:
3127 virtual void wi_fold (irange &r, tree type,
3128 const wide_int &lh_lb,
3129 const wide_int &lh_ub,
3130 const wide_int &rh_lb,
3131 const wide_int &rh_ub) const;
3132 virtual bool op1_range (irange &r, tree type,
3133 const irange &lhs,
3134 const irange &op2,
3135 relation_kind rel = VREL_NONE) const;
3136 virtual bool op2_range (irange &r, tree type,
3137 const irange &lhs,
3138 const irange &op1,
3139 relation_kind rel = VREL_NONE) const;
3140 virtual bool op1_op2_relation_effect (irange &lhs_range,
3141 tree type,
3142 const irange &op1_range,
3143 const irange &op2_range,
3144 relation_kind rel) const;
3145 } op_bitwise_xor;
3147 void
3148 operator_bitwise_xor::wi_fold (irange &r, tree type,
3149 const wide_int &lh_lb,
3150 const wide_int &lh_ub,
3151 const wide_int &rh_lb,
3152 const wide_int &rh_ub) const
3154 signop sign = TYPE_SIGN (type);
3155 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3156 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3157 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3158 maybe_nonzero_lh, mustbe_nonzero_lh);
3159 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3160 maybe_nonzero_rh, mustbe_nonzero_rh);
3162 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3163 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3164 wide_int result_one_bits
3165 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3166 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3167 wide_int new_ub = ~result_zero_bits;
3168 wide_int new_lb = result_one_bits;
3170 // If the range has all positive or all negative values, the result
3171 // is better than VARYING.
3172 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3173 value_range_with_overflow (r, type, new_lb, new_ub);
3174 else if (sign == SIGNED
3175 && wi_optimize_signed_bitwise_op (r, type,
3176 lh_lb, lh_ub,
3177 rh_lb, rh_ub))
3178 ; /* Do nothing. */
3179 else
3180 r.set_varying (type);
3182 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3183 if (wi::lt_p (lh_ub, rh_lb, sign)
3184 || wi::lt_p (rh_ub, lh_lb, sign)
3185 || wi::ne_p (result_one_bits, 0))
3187 int_range<2> tmp;
3188 tmp.set_nonzero (type);
3189 r.intersect (tmp);
3193 bool
3194 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3195 tree type,
3196 const irange &,
3197 const irange &,
3198 relation_kind rel) const
3200 if (rel == VREL_NONE)
3201 return false;
3203 int_range<2> rel_range;
3205 switch (rel)
3207 case EQ_EXPR:
3208 rel_range.set_zero (type);
3209 break;
3210 case NE_EXPR:
3211 rel_range.set_nonzero (type);
3212 break;
3213 default:
3214 return false;
3217 lhs_range.intersect (rel_range);
3218 return true;
3221 bool
3222 operator_bitwise_xor::op1_range (irange &r, tree type,
3223 const irange &lhs,
3224 const irange &op2,
3225 relation_kind rel ATTRIBUTE_UNUSED) const
3227 if (lhs.undefined_p () || lhs.varying_p ())
3229 r = lhs;
3230 return true;
3232 if (types_compatible_p (type, boolean_type_node))
3234 switch (get_bool_state (r, lhs, type))
3236 case BRS_TRUE:
3237 if (op2.varying_p ())
3238 r.set_varying (type);
3239 else if (op2.zero_p ())
3240 r = range_true (type);
3241 else
3242 r = range_false (type);
3243 break;
3244 case BRS_FALSE:
3245 r = op2;
3246 break;
3247 default:
3248 break;
3250 return true;
3252 r.set_varying (type);
3253 return true;
3256 bool
3257 operator_bitwise_xor::op2_range (irange &r, tree type,
3258 const irange &lhs,
3259 const irange &op1,
3260 relation_kind rel ATTRIBUTE_UNUSED) const
3262 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3265 class operator_trunc_mod : public range_operator
3267 public:
3268 virtual void wi_fold (irange &r, tree type,
3269 const wide_int &lh_lb,
3270 const wide_int &lh_ub,
3271 const wide_int &rh_lb,
3272 const wide_int &rh_ub) const;
3273 virtual bool op1_range (irange &r, tree type,
3274 const irange &lhs,
3275 const irange &op2,
3276 relation_kind rel ATTRIBUTE_UNUSED) const;
3277 virtual bool op2_range (irange &r, tree type,
3278 const irange &lhs,
3279 const irange &op1,
3280 relation_kind rel ATTRIBUTE_UNUSED) const;
3281 } op_trunc_mod;
3283 void
3284 operator_trunc_mod::wi_fold (irange &r, tree type,
3285 const wide_int &lh_lb,
3286 const wide_int &lh_ub,
3287 const wide_int &rh_lb,
3288 const wide_int &rh_ub) const
3290 wide_int new_lb, new_ub, tmp;
3291 signop sign = TYPE_SIGN (type);
3292 unsigned prec = TYPE_PRECISION (type);
3294 // Mod 0 is undefined.
3295 if (wi_zero_p (type, rh_lb, rh_ub))
3297 r.set_undefined ();
3298 return;
3301 // Check for constant and try to fold.
3302 if (lh_lb == lh_ub && rh_lb == rh_ub)
3304 wi::overflow_type ov = wi::OVF_NONE;
3305 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3306 if (ov == wi::OVF_NONE)
3308 r = int_range<2> (type, tmp, tmp);
3309 return;
3313 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3314 new_ub = rh_ub - 1;
3315 if (sign == SIGNED)
3317 tmp = -1 - rh_lb;
3318 new_ub = wi::smax (new_ub, tmp);
3321 if (sign == UNSIGNED)
3322 new_lb = wi::zero (prec);
3323 else
3325 new_lb = -new_ub;
3326 tmp = lh_lb;
3327 if (wi::gts_p (tmp, 0))
3328 tmp = wi::zero (prec);
3329 new_lb = wi::smax (new_lb, tmp);
3331 tmp = lh_ub;
3332 if (sign == SIGNED && wi::neg_p (tmp))
3333 tmp = wi::zero (prec);
3334 new_ub = wi::min (new_ub, tmp, sign);
3336 value_range_with_overflow (r, type, new_lb, new_ub);
3339 bool
3340 operator_trunc_mod::op1_range (irange &r, tree type,
3341 const irange &lhs,
3342 const irange &,
3343 relation_kind rel ATTRIBUTE_UNUSED) const
3345 // PR 91029.
3346 signop sign = TYPE_SIGN (type);
3347 unsigned prec = TYPE_PRECISION (type);
3348 // (a % b) >= x && x > 0 , then a >= x.
3349 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3351 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3352 return true;
3354 // (a % b) <= x && x < 0 , then a <= x.
3355 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3357 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3358 return true;
3360 return false;
3363 bool
3364 operator_trunc_mod::op2_range (irange &r, tree type,
3365 const irange &lhs,
3366 const irange &,
3367 relation_kind rel ATTRIBUTE_UNUSED) const
3369 // PR 91029.
3370 signop sign = TYPE_SIGN (type);
3371 unsigned prec = TYPE_PRECISION (type);
3372 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3373 // or b > x for unsigned.
3374 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3376 if (sign == SIGNED)
3377 r = value_range (type, wi::neg (lhs.lower_bound ()),
3378 lhs.lower_bound (), VR_ANTI_RANGE);
3379 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3380 sign))
3381 r = value_range (type, lhs.lower_bound () + 1,
3382 wi::max_value (prec, sign));
3383 else
3384 return false;
3385 return true;
3387 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3388 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3390 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3391 r = value_range (type, lhs.upper_bound (),
3392 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3393 else
3394 return false;
3395 return true;
3397 return false;
3401 class operator_logical_not : public range_operator
3403 public:
3404 virtual bool fold_range (irange &r, tree type,
3405 const irange &lh,
3406 const irange &rh,
3407 relation_kind rel = VREL_NONE) const;
3408 virtual bool op1_range (irange &r, tree type,
3409 const irange &lhs,
3410 const irange &op2,
3411 relation_kind rel = VREL_NONE) const;
3412 } op_logical_not;
3414 // Folding a logical NOT, oddly enough, involves doing nothing on the
3415 // forward pass through. During the initial walk backwards, the
3416 // logical NOT reversed the desired outcome on the way back, so on the
3417 // way forward all we do is pass the range forward.
3419 // b_2 = x_1 < 20
3420 // b_3 = !b_2
3421 // if (b_3)
3422 // to determine the TRUE branch, walking backward
3423 // if (b_3) if ([1,1])
3424 // b_3 = !b_2 [1,1] = ![0,0]
3425 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3426 // which is the result we are looking for.. so.. pass it through.
3428 bool
3429 operator_logical_not::fold_range (irange &r, tree type,
3430 const irange &lh,
3431 const irange &rh ATTRIBUTE_UNUSED,
3432 relation_kind rel ATTRIBUTE_UNUSED) const
3434 if (empty_range_varying (r, type, lh, rh))
3435 return true;
3437 r = lh;
3438 if (!lh.varying_p () && !lh.undefined_p ())
3439 r.invert ();
3441 return true;
3444 bool
3445 operator_logical_not::op1_range (irange &r,
3446 tree type,
3447 const irange &lhs,
3448 const irange &op2,
3449 relation_kind rel ATTRIBUTE_UNUSED) const
3451 // Logical NOT is involutary...do it again.
3452 return fold_range (r, type, lhs, op2);
3456 class operator_bitwise_not : public range_operator
3458 public:
3459 virtual bool fold_range (irange &r, tree type,
3460 const irange &lh,
3461 const irange &rh,
3462 relation_kind rel = VREL_NONE) const;
3463 virtual bool op1_range (irange &r, tree type,
3464 const irange &lhs,
3465 const irange &op2,
3466 relation_kind rel = VREL_NONE) const;
3467 } op_bitwise_not;
3469 bool
3470 operator_bitwise_not::fold_range (irange &r, tree type,
3471 const irange &lh,
3472 const irange &rh,
3473 relation_kind rel ATTRIBUTE_UNUSED) const
3475 if (empty_range_varying (r, type, lh, rh))
3476 return true;
3478 if (types_compatible_p (type, boolean_type_node))
3479 return op_logical_not.fold_range (r, type, lh, rh);
3481 // ~X is simply -1 - X.
3482 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3483 wi::minus_one (TYPE_PRECISION (type)));
3484 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, minusone,
3485 lh);
3488 bool
3489 operator_bitwise_not::op1_range (irange &r, tree type,
3490 const irange &lhs,
3491 const irange &op2,
3492 relation_kind rel ATTRIBUTE_UNUSED) const
3494 if (types_compatible_p (type, boolean_type_node))
3495 return op_logical_not.op1_range (r, type, lhs, op2);
3497 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3498 return fold_range (r, type, lhs, op2);
3502 class operator_cst : public range_operator
3504 public:
3505 virtual bool fold_range (irange &r, tree type,
3506 const irange &op1,
3507 const irange &op2,
3508 relation_kind rel = VREL_NONE) const;
3509 } op_integer_cst;
3511 bool
3512 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3513 const irange &lh,
3514 const irange &rh ATTRIBUTE_UNUSED,
3515 relation_kind rel ATTRIBUTE_UNUSED) const
3517 r = lh;
3518 return true;
3522 class operator_identity : public range_operator
3524 public:
3525 virtual bool fold_range (irange &r, tree type,
3526 const irange &op1,
3527 const irange &op2,
3528 relation_kind rel = VREL_NONE) const;
3529 virtual bool op1_range (irange &r, tree type,
3530 const irange &lhs,
3531 const irange &op2,
3532 relation_kind rel = VREL_NONE) const;
3533 virtual enum tree_code lhs_op1_relation (const irange &lhs,
3534 const irange &op1,
3535 const irange &op2) const;
3536 } op_identity;
3538 // Determine if there is a relationship between LHS and OP1.
3540 enum tree_code
3541 operator_identity::lhs_op1_relation (const irange &lhs,
3542 const irange &op1 ATTRIBUTE_UNUSED,
3543 const irange &op2 ATTRIBUTE_UNUSED) const
3545 if (lhs.undefined_p ())
3546 return VREL_NONE;
3547 // Simply a copy, so they are equivalent.
3548 return EQ_EXPR;
3551 bool
3552 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3553 const irange &lh,
3554 const irange &rh ATTRIBUTE_UNUSED,
3555 relation_kind rel ATTRIBUTE_UNUSED) const
3557 r = lh;
3558 return true;
3561 bool
3562 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3563 const irange &lhs,
3564 const irange &op2 ATTRIBUTE_UNUSED,
3565 relation_kind rel ATTRIBUTE_UNUSED) const
3567 r = lhs;
3568 return true;
3572 class operator_unknown : public range_operator
3574 public:
3575 virtual bool fold_range (irange &r, tree type,
3576 const irange &op1,
3577 const irange &op2,
3578 relation_kind rel = VREL_NONE) const;
3579 } op_unknown;
3581 bool
3582 operator_unknown::fold_range (irange &r, tree type,
3583 const irange &lh ATTRIBUTE_UNUSED,
3584 const irange &rh ATTRIBUTE_UNUSED,
3585 relation_kind rel ATTRIBUTE_UNUSED) const
3587 r.set_varying (type);
3588 return true;
3592 class operator_abs : public range_operator
3594 public:
3595 virtual void wi_fold (irange &r, tree type,
3596 const wide_int &lh_lb,
3597 const wide_int &lh_ub,
3598 const wide_int &rh_lb,
3599 const wide_int &rh_ub) const;
3600 virtual bool op1_range (irange &r, tree type,
3601 const irange &lhs,
3602 const irange &op2,
3603 relation_kind rel ATTRIBUTE_UNUSED) const;
3604 } op_abs;
3606 void
3607 operator_abs::wi_fold (irange &r, tree type,
3608 const wide_int &lh_lb, const wide_int &lh_ub,
3609 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3610 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3612 wide_int min, max;
3613 signop sign = TYPE_SIGN (type);
3614 unsigned prec = TYPE_PRECISION (type);
3616 // Pass through LH for the easy cases.
3617 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3619 r = int_range<1> (type, lh_lb, lh_ub);
3620 return;
3623 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3624 // a useful range.
3625 wide_int min_value = wi::min_value (prec, sign);
3626 wide_int max_value = wi::max_value (prec, sign);
3627 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3629 r.set_varying (type);
3630 return;
3633 // ABS_EXPR may flip the range around, if the original range
3634 // included negative values.
3635 if (wi::eq_p (lh_lb, min_value))
3637 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3638 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3639 if (wi::eq_p (lh_ub, min_value))
3641 r = int_range<1> (type, min_value, min_value);
3642 return;
3644 min = max_value;
3646 else
3647 min = wi::abs (lh_lb);
3649 if (wi::eq_p (lh_ub, min_value))
3650 max = max_value;
3651 else
3652 max = wi::abs (lh_ub);
3654 // If the range contains zero then we know that the minimum value in the
3655 // range will be zero.
3656 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3658 if (wi::gt_p (min, max, sign))
3659 max = min;
3660 min = wi::zero (prec);
3662 else
3664 // If the range was reversed, swap MIN and MAX.
3665 if (wi::gt_p (min, max, sign))
3666 std::swap (min, max);
3669 // If the new range has its limits swapped around (MIN > MAX), then
3670 // the operation caused one of them to wrap around. The only thing
3671 // we know is that the result is positive.
3672 if (wi::gt_p (min, max, sign))
3674 min = wi::zero (prec);
3675 max = max_value;
3677 r = int_range<1> (type, min, max);
3680 bool
3681 operator_abs::op1_range (irange &r, tree type,
3682 const irange &lhs,
3683 const irange &op2,
3684 relation_kind rel ATTRIBUTE_UNUSED) const
3686 if (empty_range_varying (r, type, lhs, op2))
3687 return true;
3688 if (TYPE_UNSIGNED (type))
3690 r = lhs;
3691 return true;
3693 // Start with the positives because negatives are an impossible result.
3694 int_range_max positives = range_positives (type);
3695 positives.intersect (lhs);
3696 r = positives;
3697 // Then add the negative of each pair:
3698 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3699 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3700 r.union_ (int_range<1> (type,
3701 -positives.upper_bound (i),
3702 -positives.lower_bound (i)));
3703 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3704 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3705 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3706 wide_int lb = lhs.lower_bound ();
3707 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3708 r.union_ (int_range<2> (type, lb, lb));
3709 return true;
3713 class operator_absu : public range_operator
3715 public:
3716 virtual void wi_fold (irange &r, tree type,
3717 const wide_int &lh_lb, const wide_int &lh_ub,
3718 const wide_int &rh_lb, const wide_int &rh_ub) const;
3719 } op_absu;
3721 void
3722 operator_absu::wi_fold (irange &r, tree type,
3723 const wide_int &lh_lb, const wide_int &lh_ub,
3724 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3725 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3727 wide_int new_lb, new_ub;
3729 // Pass through VR0 the easy cases.
3730 if (wi::ges_p (lh_lb, 0))
3732 new_lb = lh_lb;
3733 new_ub = lh_ub;
3735 else
3737 new_lb = wi::abs (lh_lb);
3738 new_ub = wi::abs (lh_ub);
3740 // If the range contains zero then we know that the minimum
3741 // value in the range will be zero.
3742 if (wi::ges_p (lh_ub, 0))
3744 if (wi::gtu_p (new_lb, new_ub))
3745 new_ub = new_lb;
3746 new_lb = wi::zero (TYPE_PRECISION (type));
3748 else
3749 std::swap (new_lb, new_ub);
3752 gcc_checking_assert (TYPE_UNSIGNED (type));
3753 r = int_range<1> (type, new_lb, new_ub);
3757 class operator_negate : public range_operator
3759 public:
3760 virtual bool fold_range (irange &r, tree type,
3761 const irange &op1,
3762 const irange &op2,
3763 relation_kind rel = VREL_NONE) const;
3764 virtual bool op1_range (irange &r, tree type,
3765 const irange &lhs,
3766 const irange &op2,
3767 relation_kind rel = VREL_NONE) const;
3768 } op_negate;
3770 bool
3771 operator_negate::fold_range (irange &r, tree type,
3772 const irange &lh,
3773 const irange &rh,
3774 relation_kind rel ATTRIBUTE_UNUSED) const
3776 if (empty_range_varying (r, type, lh, rh))
3777 return true;
3778 // -X is simply 0 - X.
3779 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type,
3780 range_zero (type),
3781 lh);
3784 bool
3785 operator_negate::op1_range (irange &r, tree type,
3786 const irange &lhs,
3787 const irange &op2,
3788 relation_kind rel ATTRIBUTE_UNUSED) const
3790 // NEGATE is involutory.
3791 return fold_range (r, type, lhs, op2);
3795 class operator_addr_expr : public range_operator
3797 public:
3798 virtual bool fold_range (irange &r, tree type,
3799 const irange &op1,
3800 const irange &op2,
3801 relation_kind rel = VREL_NONE) const;
3802 virtual bool op1_range (irange &r, tree type,
3803 const irange &lhs,
3804 const irange &op2,
3805 relation_kind rel = VREL_NONE) const;
3806 } op_addr;
3808 bool
3809 operator_addr_expr::fold_range (irange &r, tree type,
3810 const irange &lh,
3811 const irange &rh,
3812 relation_kind rel ATTRIBUTE_UNUSED) const
3814 if (empty_range_varying (r, type, lh, rh))
3815 return true;
3817 // Return a non-null pointer of the LHS type (passed in op2).
3818 if (lh.zero_p ())
3819 r = range_zero (type);
3820 else if (!lh.contains_p (build_zero_cst (lh.type ())))
3821 r = range_nonzero (type);
3822 else
3823 r.set_varying (type);
3824 return true;
3827 bool
3828 operator_addr_expr::op1_range (irange &r, tree type,
3829 const irange &lhs,
3830 const irange &op2,
3831 relation_kind rel ATTRIBUTE_UNUSED) const
3833 return operator_addr_expr::fold_range (r, type, lhs, op2);
3837 class pointer_plus_operator : public range_operator
3839 public:
3840 virtual void wi_fold (irange &r, tree type,
3841 const wide_int &lh_lb,
3842 const wide_int &lh_ub,
3843 const wide_int &rh_lb,
3844 const wide_int &rh_ub) const;
3845 } op_pointer_plus;
3847 void
3848 pointer_plus_operator::wi_fold (irange &r, tree type,
3849 const wide_int &lh_lb,
3850 const wide_int &lh_ub,
3851 const wide_int &rh_lb,
3852 const wide_int &rh_ub) const
3854 // Check for [0,0] + const, and simply return the const.
3855 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3857 tree val = wide_int_to_tree (type, rh_lb);
3858 r.set (val, val);
3859 return;
3862 // For pointer types, we are really only interested in asserting
3863 // whether the expression evaluates to non-NULL.
3865 // With -fno-delete-null-pointer-checks we need to be more
3866 // conservative. As some object might reside at address 0,
3867 // then some offset could be added to it and the same offset
3868 // subtracted again and the result would be NULL.
3869 // E.g.
3870 // static int a[12]; where &a[0] is NULL and
3871 // ptr = &a[6];
3872 // ptr -= 6;
3873 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3874 // where the first range doesn't include zero and the second one
3875 // doesn't either. As the second operand is sizetype (unsigned),
3876 // consider all ranges where the MSB could be set as possible
3877 // subtractions where the result might be NULL.
3878 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3879 || !wi_includes_zero_p (type, rh_lb, rh_ub))
3880 && !TYPE_OVERFLOW_WRAPS (type)
3881 && (flag_delete_null_pointer_checks
3882 || !wi::sign_mask (rh_ub)))
3883 r = range_nonzero (type);
3884 else if (lh_lb == lh_ub && lh_lb == 0
3885 && rh_lb == rh_ub && rh_lb == 0)
3886 r = range_zero (type);
3887 else
3888 r.set_varying (type);
3892 class pointer_min_max_operator : public range_operator
3894 public:
3895 virtual void wi_fold (irange & r, tree type,
3896 const wide_int &lh_lb, const wide_int &lh_ub,
3897 const wide_int &rh_lb, const wide_int &rh_ub) const;
3898 } op_ptr_min_max;
3900 void
3901 pointer_min_max_operator::wi_fold (irange &r, tree type,
3902 const wide_int &lh_lb,
3903 const wide_int &lh_ub,
3904 const wide_int &rh_lb,
3905 const wide_int &rh_ub) const
3907 // For MIN/MAX expressions with pointers, we only care about
3908 // nullness. If both are non null, then the result is nonnull.
3909 // If both are null, then the result is null. Otherwise they
3910 // are varying.
3911 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3912 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3913 r = range_nonzero (type);
3914 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3915 r = range_zero (type);
3916 else
3917 r.set_varying (type);
3921 class pointer_and_operator : public range_operator
3923 public:
3924 virtual void wi_fold (irange &r, tree type,
3925 const wide_int &lh_lb, const wide_int &lh_ub,
3926 const wide_int &rh_lb, const wide_int &rh_ub) const;
3927 } op_pointer_and;
3929 void
3930 pointer_and_operator::wi_fold (irange &r, tree type,
3931 const wide_int &lh_lb,
3932 const wide_int &lh_ub,
3933 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3934 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3936 // For pointer types, we are really only interested in asserting
3937 // whether the expression evaluates to non-NULL.
3938 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
3939 r = range_zero (type);
3940 else
3941 r.set_varying (type);
3945 class pointer_or_operator : public range_operator
3947 public:
3948 virtual bool op1_range (irange &r, tree type,
3949 const irange &lhs,
3950 const irange &op2,
3951 relation_kind rel = VREL_NONE) const;
3952 virtual bool op2_range (irange &r, tree type,
3953 const irange &lhs,
3954 const irange &op1,
3955 relation_kind rel = VREL_NONE) const;
3956 virtual void wi_fold (irange &r, tree type,
3957 const wide_int &lh_lb, const wide_int &lh_ub,
3958 const wide_int &rh_lb, const wide_int &rh_ub) const;
3959 } op_pointer_or;
3961 bool
3962 pointer_or_operator::op1_range (irange &r, tree type,
3963 const irange &lhs,
3964 const irange &op2 ATTRIBUTE_UNUSED,
3965 relation_kind rel ATTRIBUTE_UNUSED) const
3967 if (lhs.zero_p ())
3969 tree zero = build_zero_cst (type);
3970 r = int_range<1> (zero, zero);
3971 return true;
3973 r.set_varying (type);
3974 return true;
3977 bool
3978 pointer_or_operator::op2_range (irange &r, tree type,
3979 const irange &lhs,
3980 const irange &op1,
3981 relation_kind rel ATTRIBUTE_UNUSED) const
3983 return pointer_or_operator::op1_range (r, type, lhs, op1);
3986 void
3987 pointer_or_operator::wi_fold (irange &r, tree type,
3988 const wide_int &lh_lb,
3989 const wide_int &lh_ub,
3990 const wide_int &rh_lb,
3991 const wide_int &rh_ub) const
3993 // For pointer types, we are really only interested in asserting
3994 // whether the expression evaluates to non-NULL.
3995 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3996 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3997 r = range_nonzero (type);
3998 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3999 r = range_zero (type);
4000 else
4001 r.set_varying (type);
4004 // Return a pointer to the range_operator instance, if there is one
4005 // associated with tree_code CODE.
4007 range_operator *
4008 range_op_table::operator[] (enum tree_code code)
4010 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4011 return m_range_tree[code];
4014 // Add OP to the handler table for CODE.
4016 void
4017 range_op_table::set (enum tree_code code, range_operator &op)
4019 gcc_checking_assert (m_range_tree[code] == NULL);
4020 m_range_tree[code] = &op;
4023 // Instantiate a range op table for integral operations.
4025 class integral_table : public range_op_table
4027 public:
4028 integral_table ();
4029 } integral_tree_table;
4031 integral_table::integral_table ()
4033 set (EQ_EXPR, op_equal);
4034 set (NE_EXPR, op_not_equal);
4035 set (LT_EXPR, op_lt);
4036 set (LE_EXPR, op_le);
4037 set (GT_EXPR, op_gt);
4038 set (GE_EXPR, op_ge);
4039 set (PLUS_EXPR, op_plus);
4040 set (MINUS_EXPR, op_minus);
4041 set (MIN_EXPR, op_min);
4042 set (MAX_EXPR, op_max);
4043 set (MULT_EXPR, op_mult);
4044 set (TRUNC_DIV_EXPR, op_trunc_div);
4045 set (FLOOR_DIV_EXPR, op_floor_div);
4046 set (ROUND_DIV_EXPR, op_round_div);
4047 set (CEIL_DIV_EXPR, op_ceil_div);
4048 set (EXACT_DIV_EXPR, op_exact_div);
4049 set (LSHIFT_EXPR, op_lshift);
4050 set (RSHIFT_EXPR, op_rshift);
4051 set (NOP_EXPR, op_convert);
4052 set (CONVERT_EXPR, op_convert);
4053 set (TRUTH_AND_EXPR, op_logical_and);
4054 set (BIT_AND_EXPR, op_bitwise_and);
4055 set (TRUTH_OR_EXPR, op_logical_or);
4056 set (BIT_IOR_EXPR, op_bitwise_or);
4057 set (BIT_XOR_EXPR, op_bitwise_xor);
4058 set (TRUNC_MOD_EXPR, op_trunc_mod);
4059 set (TRUTH_NOT_EXPR, op_logical_not);
4060 set (BIT_NOT_EXPR, op_bitwise_not);
4061 set (INTEGER_CST, op_integer_cst);
4062 set (SSA_NAME, op_identity);
4063 set (PAREN_EXPR, op_identity);
4064 set (OBJ_TYPE_REF, op_identity);
4065 set (IMAGPART_EXPR, op_unknown);
4066 set (REALPART_EXPR, op_unknown);
4067 set (POINTER_DIFF_EXPR, op_pointer_diff);
4068 set (ABS_EXPR, op_abs);
4069 set (ABSU_EXPR, op_absu);
4070 set (NEGATE_EXPR, op_negate);
4071 set (ADDR_EXPR, op_addr);
4074 // Instantiate a range op table for pointer operations.
4076 class pointer_table : public range_op_table
4078 public:
4079 pointer_table ();
4080 } pointer_tree_table;
4082 pointer_table::pointer_table ()
4084 set (BIT_AND_EXPR, op_pointer_and);
4085 set (BIT_IOR_EXPR, op_pointer_or);
4086 set (MIN_EXPR, op_ptr_min_max);
4087 set (MAX_EXPR, op_ptr_min_max);
4088 set (POINTER_PLUS_EXPR, op_pointer_plus);
4090 set (EQ_EXPR, op_equal);
4091 set (NE_EXPR, op_not_equal);
4092 set (LT_EXPR, op_lt);
4093 set (LE_EXPR, op_le);
4094 set (GT_EXPR, op_gt);
4095 set (GE_EXPR, op_ge);
4096 set (SSA_NAME, op_identity);
4097 set (INTEGER_CST, op_integer_cst);
4098 set (ADDR_EXPR, op_addr);
4099 set (NOP_EXPR, op_convert);
4100 set (CONVERT_EXPR, op_convert);
4102 set (BIT_NOT_EXPR, op_bitwise_not);
4103 set (BIT_XOR_EXPR, op_bitwise_xor);
4106 // The tables are hidden and accessed via a simple extern function.
4108 range_operator *
4109 range_op_handler (enum tree_code code, tree type)
4111 // First check if there is a pointer specialization.
4112 if (POINTER_TYPE_P (type))
4113 return pointer_tree_table[code];
4114 if (INTEGRAL_TYPE_P (type))
4115 return integral_tree_table[code];
4116 return NULL;
4119 // Cast the range in R to TYPE.
4121 void
4122 range_cast (irange &r, tree type)
4124 int_range_max tmp = r;
4125 range_operator *op = range_op_handler (CONVERT_EXPR, type);
4126 // Call op_convert, if it fails, the result is varying.
4127 if (!op->fold_range (r, type, tmp, int_range<1> (type)))
4128 r.set_varying (type);
4131 #if CHECKING_P
4132 #include "selftest.h"
4134 namespace selftest
4136 #define INT(N) build_int_cst (integer_type_node, (N))
4137 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4138 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4139 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4140 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4141 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4143 static void
4144 range_op_cast_tests ()
4146 int_range<1> r0, r1, r2, rold;
4147 r0.set_varying (integer_type_node);
4148 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4150 // If a range is in any way outside of the range for the converted
4151 // to range, default to the range for the new type.
4152 r0.set_varying (short_integer_type_node);
4153 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4154 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4155 if (TYPE_PRECISION (TREE_TYPE (maxint))
4156 > TYPE_PRECISION (short_integer_type_node))
4158 r1 = int_range<1> (integer_zero_node, maxint);
4159 range_cast (r1, short_integer_type_node);
4160 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4161 && r1.upper_bound() == wi::to_wide (maxshort));
4164 // (unsigned char)[-5,-1] => [251,255].
4165 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4166 range_cast (r0, unsigned_char_type_node);
4167 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4168 range_cast (r0, signed_char_type_node);
4169 ASSERT_TRUE (r0 == rold);
4171 // (signed char)[15, 150] => [-128,-106][15,127].
4172 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4173 range_cast (r0, signed_char_type_node);
4174 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4175 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4176 r1.union_ (r2);
4177 ASSERT_TRUE (r1 == r0);
4178 range_cast (r0, unsigned_char_type_node);
4179 ASSERT_TRUE (r0 == rold);
4181 // (unsigned char)[-5, 5] => [0,5][251,255].
4182 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4183 range_cast (r0, unsigned_char_type_node);
4184 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4185 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4186 r1.union_ (r2);
4187 ASSERT_TRUE (r0 == r1);
4188 range_cast (r0, signed_char_type_node);
4189 ASSERT_TRUE (r0 == rold);
4191 // (unsigned char)[-5,5] => [0,5][251,255].
4192 r0 = int_range<1> (INT (-5), INT (5));
4193 range_cast (r0, unsigned_char_type_node);
4194 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4195 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4196 ASSERT_TRUE (r0 == r1);
4198 // (unsigned char)[5U,1974U] => [0,255].
4199 r0 = int_range<1> (UINT (5), UINT (1974));
4200 range_cast (r0, unsigned_char_type_node);
4201 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4202 range_cast (r0, integer_type_node);
4203 // Going to a wider range should not sign extend.
4204 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4206 // (unsigned char)[-350,15] => [0,255].
4207 r0 = int_range<1> (INT (-350), INT (15));
4208 range_cast (r0, unsigned_char_type_node);
4209 ASSERT_TRUE (r0 == (int_range<1>
4210 (TYPE_MIN_VALUE (unsigned_char_type_node),
4211 TYPE_MAX_VALUE (unsigned_char_type_node))));
4213 // Casting [-120,20] from signed char to unsigned short.
4214 // => [0, 20][0xff88, 0xffff].
4215 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4216 range_cast (r0, short_unsigned_type_node);
4217 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4218 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4219 r1.union_ (r2);
4220 ASSERT_TRUE (r0 == r1);
4221 // A truncating cast back to signed char will work because [-120, 20]
4222 // is representable in signed char.
4223 range_cast (r0, signed_char_type_node);
4224 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4226 // unsigned char -> signed short
4227 // (signed short)[(unsigned char)25, (unsigned char)250]
4228 // => [(signed short)25, (signed short)250]
4229 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4230 range_cast (r0, short_integer_type_node);
4231 r1 = int_range<1> (INT16 (25), INT16 (250));
4232 ASSERT_TRUE (r0 == r1);
4233 range_cast (r0, unsigned_char_type_node);
4234 ASSERT_TRUE (r0 == rold);
4236 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4237 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4238 TYPE_MAX_VALUE (long_long_integer_type_node));
4239 range_cast (r0, short_unsigned_type_node);
4240 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4241 TYPE_MAX_VALUE (short_unsigned_type_node));
4242 ASSERT_TRUE (r0 == r1);
4244 // Casting NONZERO to a narrower type will wrap/overflow so
4245 // it's just the entire range for the narrower type.
4247 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4248 // is outside of the range of a smaller range, return the full
4249 // smaller range.
4250 if (TYPE_PRECISION (integer_type_node)
4251 > TYPE_PRECISION (short_integer_type_node))
4253 r0 = range_nonzero (integer_type_node);
4254 range_cast (r0, short_integer_type_node);
4255 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4256 TYPE_MAX_VALUE (short_integer_type_node));
4257 ASSERT_TRUE (r0 == r1);
4260 // Casting NONZERO from a narrower signed to a wider signed.
4262 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4263 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4264 r0 = range_nonzero (short_integer_type_node);
4265 range_cast (r0, integer_type_node);
4266 r1 = int_range<1> (INT (-32768), INT (-1));
4267 r2 = int_range<1> (INT (1), INT (32767));
4268 r1.union_ (r2);
4269 ASSERT_TRUE (r0 == r1);
4272 static void
4273 range_op_lshift_tests ()
4275 // Test that 0x808.... & 0x8.... still contains 0x8....
4276 // for a large set of numbers.
4278 int_range_max res;
4279 tree big_type = long_long_unsigned_type_node;
4280 // big_num = 0x808,0000,0000,0000
4281 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4282 build_int_cst (big_type, 0x808),
4283 build_int_cst (big_type, 48));
4284 op_bitwise_and.fold_range (res, big_type,
4285 int_range <1> (big_type),
4286 int_range <1> (big_num, big_num));
4287 // val = 0x8,0000,0000,0000
4288 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4289 build_int_cst (big_type, 0x8),
4290 build_int_cst (big_type, 48));
4291 ASSERT_TRUE (res.contains_p (val));
4294 if (TYPE_PRECISION (unsigned_type_node) > 31)
4296 // unsigned VARYING = op1 << 1 should be VARYING.
4297 int_range<2> lhs (unsigned_type_node);
4298 int_range<2> shift (INT (1), INT (1));
4299 int_range_max op1;
4300 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4301 ASSERT_TRUE (op1.varying_p ());
4303 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4304 int_range<2> zero (UINT (0), UINT (0));
4305 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4306 ASSERT_TRUE (op1.num_pairs () == 2);
4307 // Remove the [0,0] range.
4308 op1.intersect (zero);
4309 ASSERT_TRUE (op1.num_pairs () == 1);
4310 // op1 << 1 should be [0x8000,0x8000] << 1,
4311 // which should result in [0,0].
4312 int_range_max result;
4313 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4314 ASSERT_TRUE (result == zero);
4316 // signed VARYING = op1 << 1 should be VARYING.
4317 if (TYPE_PRECISION (integer_type_node) > 31)
4319 // unsigned VARYING = op1 << 1 hould be VARYING.
4320 int_range<2> lhs (integer_type_node);
4321 int_range<2> shift (INT (1), INT (1));
4322 int_range_max op1;
4323 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4324 ASSERT_TRUE (op1.varying_p ());
4326 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4327 int_range<2> zero (INT (0), INT (0));
4328 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4329 ASSERT_TRUE (op1.num_pairs () == 2);
4330 // Remove the [0,0] range.
4331 op1.intersect (zero);
4332 ASSERT_TRUE (op1.num_pairs () == 1);
4333 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4334 // which should result in [0,0].
4335 int_range_max result;
4336 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4337 ASSERT_TRUE (result == zero);
4341 static void
4342 range_op_rshift_tests ()
4344 // unsigned: [3, MAX] = OP1 >> 1
4346 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4347 TYPE_MAX_VALUE (unsigned_type_node));
4348 int_range_max one (build_one_cst (unsigned_type_node),
4349 build_one_cst (unsigned_type_node));
4350 int_range_max op1;
4351 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4352 ASSERT_FALSE (op1.contains_p (UINT (3)));
4355 // signed: [3, MAX] = OP1 >> 1
4357 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4358 int_range_max one (INT (1), INT (1));
4359 int_range_max op1;
4360 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4361 ASSERT_FALSE (op1.contains_p (INT (-2)));
4364 // This is impossible, so OP1 should be [].
4365 // signed: [MIN, MIN] = OP1 >> 1
4367 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4368 TYPE_MIN_VALUE (integer_type_node));
4369 int_range_max one (INT (1), INT (1));
4370 int_range_max op1;
4371 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4372 ASSERT_TRUE (op1.undefined_p ());
4375 // signed: ~[-1] = OP1 >> 31
4376 if (TYPE_PRECISION (integer_type_node) > 31)
4378 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4379 int_range_max shift (INT (31), INT (31));
4380 int_range_max op1;
4381 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4382 int_range_max negatives = range_negatives (integer_type_node);
4383 negatives.intersect (op1);
4384 ASSERT_TRUE (negatives.undefined_p ());
4388 static void
4389 range_op_bitwise_and_tests ()
4391 int_range_max res;
4392 tree min = vrp_val_min (integer_type_node);
4393 tree max = vrp_val_max (integer_type_node);
4394 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4395 build_one_cst (integer_type_node));
4396 int_range_max i1 (tiny, max);
4397 int_range_max i2 (build_int_cst (integer_type_node, 255),
4398 build_int_cst (integer_type_node, 255));
4400 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4401 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4402 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4404 // VARYING = OP1 & 255: OP1 is VARYING
4405 i1 = int_range<1> (integer_type_node);
4406 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4407 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4409 // (NONZERO | X) is nonzero.
4410 i1.set_nonzero (integer_type_node);
4411 i2.set_varying (integer_type_node);
4412 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4413 ASSERT_TRUE (res.nonzero_p ());
4415 // (NEGATIVE | X) is nonzero.
4416 i1 = int_range<1> (INT (-5), INT (-3));
4417 i2.set_varying (integer_type_node);
4418 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4419 ASSERT_FALSE (res.contains_p (INT (0)));
4422 static void
4423 range_relational_tests ()
4425 int_range<2> lhs (unsigned_char_type_node);
4426 int_range<2> op1 (UCHAR (8), UCHAR (10));
4427 int_range<2> op2 (UCHAR (20), UCHAR (20));
4429 // Never wrapping additions mean LHS > OP1.
4430 tree_code code = op_plus.lhs_op1_relation (lhs, op1, op2);
4431 ASSERT_TRUE (code == GT_EXPR);
4433 // Most wrapping additions mean nothing...
4434 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4435 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4436 code = op_plus.lhs_op1_relation (lhs, op1, op2);
4437 ASSERT_TRUE (code == VREL_NONE);
4439 // However, always wrapping additions mean LHS < OP1.
4440 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4441 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4442 code = op_plus.lhs_op1_relation (lhs, op1, op2);
4443 ASSERT_TRUE (code == LT_EXPR);
4446 void
4447 range_op_tests ()
4449 range_op_rshift_tests ();
4450 range_op_lshift_tests ();
4451 range_op_bitwise_and_tests ();
4452 range_op_cast_tests ();
4453 range_relational_tests ();
4456 } // namespace selftest
4458 #endif // CHECKING_P