Fix ICE on view conversion between struct and integer
[official-gcc.git] / gcc / range-op.cc
blobe184129f9af7d87f632149f27401211166a77c60
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-iterator.h"
42 #include "gimple-fold.h"
43 #include "tree-eh.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 (r.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 (r.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_VARYING.
249 relation_kind
250 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
251 const irange &op1 ATTRIBUTE_UNUSED,
252 const irange &op2 ATTRIBUTE_UNUSED,
253 relation_kind rel ATTRIBUTE_UNUSED) const
255 return VREL_VARYING;
258 relation_kind
259 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
260 const irange &op1 ATTRIBUTE_UNUSED,
261 const irange &op2 ATTRIBUTE_UNUSED,
262 relation_kind rel ATTRIBUTE_UNUSED) const
264 return VREL_VARYING;
267 relation_kind
268 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
270 return VREL_VARYING;
273 // Default is no relation affects the LHS.
275 bool
276 range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
277 tree type ATTRIBUTE_UNUSED,
278 const irange &op1_range ATTRIBUTE_UNUSED,
279 const irange &op2_range ATTRIBUTE_UNUSED,
280 relation_kind rel ATTRIBUTE_UNUSED) const
282 return false;
285 // Create and return a range from a pair of wide-ints that are known
286 // to have overflowed (or underflowed).
288 static void
289 value_range_from_overflowed_bounds (irange &r, tree type,
290 const wide_int &wmin,
291 const wide_int &wmax)
293 const signop sgn = TYPE_SIGN (type);
294 const unsigned int prec = TYPE_PRECISION (type);
296 wide_int tmin = wide_int::from (wmin, prec, sgn);
297 wide_int tmax = wide_int::from (wmax, prec, sgn);
299 bool covers = false;
300 wide_int tem = tmin;
301 tmin = tmax + 1;
302 if (wi::cmp (tmin, tmax, sgn) < 0)
303 covers = true;
304 tmax = tem - 1;
305 if (wi::cmp (tmax, tem, sgn) > 0)
306 covers = true;
308 // If the anti-range would cover nothing, drop to varying.
309 // Likewise if the anti-range bounds are outside of the types
310 // values.
311 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
312 r.set_varying (type);
313 else
315 tree tree_min = wide_int_to_tree (type, tmin);
316 tree tree_max = wide_int_to_tree (type, tmax);
317 r.set (tree_min, tree_max, VR_ANTI_RANGE);
321 // Create and return a range from a pair of wide-ints. MIN_OVF and
322 // MAX_OVF describe any overflow that might have occurred while
323 // calculating WMIN and WMAX respectively.
325 static void
326 value_range_with_overflow (irange &r, tree type,
327 const wide_int &wmin, const wide_int &wmax,
328 wi::overflow_type min_ovf = wi::OVF_NONE,
329 wi::overflow_type max_ovf = wi::OVF_NONE)
331 const signop sgn = TYPE_SIGN (type);
332 const unsigned int prec = TYPE_PRECISION (type);
333 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
335 // For one bit precision if max != min, then the range covers all
336 // values.
337 if (prec == 1 && wi::ne_p (wmax, wmin))
339 r.set_varying (type);
340 return;
343 if (overflow_wraps)
345 // If overflow wraps, truncate the values and adjust the range,
346 // kind, and bounds appropriately.
347 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
349 wide_int tmin = wide_int::from (wmin, prec, sgn);
350 wide_int tmax = wide_int::from (wmax, prec, sgn);
351 // If the limits are swapped, we wrapped around and cover
352 // the entire range.
353 if (wi::gt_p (tmin, tmax, sgn))
354 r.set_varying (type);
355 else
356 // No overflow or both overflow or underflow. The range
357 // kind stays normal.
358 r.set (wide_int_to_tree (type, tmin),
359 wide_int_to_tree (type, tmax));
360 return;
363 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
364 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
365 value_range_from_overflowed_bounds (r, type, wmin, wmax);
366 else
367 // Other underflow and/or overflow, drop to VR_VARYING.
368 r.set_varying (type);
370 else
372 // If both bounds either underflowed or overflowed, then the result
373 // is undefined.
374 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
375 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
377 r.set_undefined ();
378 return;
381 // If overflow does not wrap, saturate to [MIN, MAX].
382 wide_int new_lb, new_ub;
383 if (min_ovf == wi::OVF_UNDERFLOW)
384 new_lb = wi::min_value (prec, sgn);
385 else if (min_ovf == wi::OVF_OVERFLOW)
386 new_lb = wi::max_value (prec, sgn);
387 else
388 new_lb = wmin;
390 if (max_ovf == wi::OVF_UNDERFLOW)
391 new_ub = wi::min_value (prec, sgn);
392 else if (max_ovf == wi::OVF_OVERFLOW)
393 new_ub = wi::max_value (prec, sgn);
394 else
395 new_ub = wmax;
397 r.set (wide_int_to_tree (type, new_lb),
398 wide_int_to_tree (type, new_ub));
402 // Create and return a range from a pair of wide-ints. Canonicalize
403 // the case where the bounds are swapped. In which case, we transform
404 // [10,5] into [MIN,5][10,MAX].
406 static inline void
407 create_possibly_reversed_range (irange &r, tree type,
408 const wide_int &new_lb, const wide_int &new_ub)
410 signop s = TYPE_SIGN (type);
411 // If the bounds are swapped, treat the result as if an overflow occured.
412 if (wi::gt_p (new_lb, new_ub, s))
413 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
414 else
415 // Otherwise it's just a normal range.
416 r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
419 // Return the summary information about boolean range LHS. If EMPTY/FULL,
420 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
422 bool_range_state
423 get_bool_state (vrange &r, const vrange &lhs, tree val_type)
425 // If there is no result, then this is unexecutable.
426 if (lhs.undefined_p ())
428 r.set_undefined ();
429 return BRS_EMPTY;
432 if (lhs.zero_p ())
433 return BRS_FALSE;
435 // For TRUE, we can't just test for [1,1] because Ada can have
436 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
437 if (lhs.contains_p (build_zero_cst (lhs.type ())))
439 r.set_varying (val_type);
440 return BRS_FULL;
443 return BRS_TRUE;
447 class operator_equal : public range_operator
449 using range_operator::fold_range;
450 using range_operator::op1_range;
451 using range_operator::op2_range;
452 public:
453 virtual bool fold_range (irange &r, tree type,
454 const irange &op1,
455 const irange &op2,
456 relation_kind rel = VREL_VARYING) const;
457 virtual bool op1_range (irange &r, tree type,
458 const irange &lhs,
459 const irange &val,
460 relation_kind rel = VREL_VARYING) const;
461 virtual bool op2_range (irange &r, tree type,
462 const irange &lhs,
463 const irange &val,
464 relation_kind rel = VREL_VARYING) const;
465 virtual relation_kind op1_op2_relation (const irange &lhs) const;
466 } op_equal;
468 // Check if the LHS range indicates a relation between OP1 and OP2.
470 relation_kind
471 equal_op1_op2_relation (const irange &lhs)
473 if (lhs.undefined_p ())
474 return VREL_UNDEFINED;
476 // FALSE = op1 == op2 indicates NE_EXPR.
477 if (lhs.zero_p ())
478 return VREL_NE;
480 // TRUE = op1 == op2 indicates EQ_EXPR.
481 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
482 return VREL_EQ;
483 return VREL_VARYING;
486 relation_kind
487 operator_equal::op1_op2_relation (const irange &lhs) const
489 return equal_op1_op2_relation (lhs);
493 bool
494 operator_equal::fold_range (irange &r, tree type,
495 const irange &op1,
496 const irange &op2,
497 relation_kind rel) const
499 if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
500 return true;
502 // We can be sure the values are always equal or not if both ranges
503 // consist of a single value, and then compare them.
504 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
505 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
507 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
508 r = range_true (type);
509 else
510 r = range_false (type);
512 else
514 // If ranges do not intersect, we know the range is not equal,
515 // otherwise we don't know anything for sure.
516 int_range_max tmp = op1;
517 tmp.intersect (op2);
518 if (tmp.undefined_p ())
519 r = range_false (type);
520 else
521 r = range_true_and_false (type);
523 return true;
526 bool
527 operator_equal::op1_range (irange &r, tree type,
528 const irange &lhs,
529 const irange &op2,
530 relation_kind rel ATTRIBUTE_UNUSED) const
532 switch (get_bool_state (r, lhs, type))
534 case BRS_FALSE:
535 // If the result is false, the only time we know anything is
536 // if OP2 is a constant.
537 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
539 r = op2;
540 r.invert ();
542 else
543 r.set_varying (type);
544 break;
546 case BRS_TRUE:
547 // If it's true, the result is the same as OP2.
548 r = op2;
549 break;
551 default:
552 break;
554 return true;
557 bool
558 operator_equal::op2_range (irange &r, tree type,
559 const irange &lhs,
560 const irange &op1,
561 relation_kind rel) const
563 return operator_equal::op1_range (r, type, lhs, op1, rel);
566 class operator_not_equal : public range_operator
568 using range_operator::fold_range;
569 using range_operator::op1_range;
570 using range_operator::op2_range;
571 public:
572 virtual bool fold_range (irange &r, tree type,
573 const irange &op1,
574 const irange &op2,
575 relation_kind rel = VREL_VARYING) const;
576 virtual bool op1_range (irange &r, tree type,
577 const irange &lhs,
578 const irange &op2,
579 relation_kind rel = VREL_VARYING) const;
580 virtual bool op2_range (irange &r, tree type,
581 const irange &lhs,
582 const irange &op1,
583 relation_kind rel = VREL_VARYING) const;
584 virtual relation_kind op1_op2_relation (const irange &lhs) const;
585 } op_not_equal;
587 // Check if the LHS range indicates a relation between OP1 and OP2.
589 relation_kind
590 not_equal_op1_op2_relation (const irange &lhs)
592 if (lhs.undefined_p ())
593 return VREL_UNDEFINED;
595 // FALSE = op1 != op2 indicates EQ_EXPR.
596 if (lhs.zero_p ())
597 return VREL_EQ;
599 // TRUE = op1 != op2 indicates NE_EXPR.
600 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
601 return VREL_NE;
602 return VREL_VARYING;
605 relation_kind
606 operator_not_equal::op1_op2_relation (const irange &lhs) const
608 return not_equal_op1_op2_relation (lhs);
611 bool
612 operator_not_equal::fold_range (irange &r, tree type,
613 const irange &op1,
614 const irange &op2,
615 relation_kind rel) const
617 if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
618 return true;
620 // We can be sure the values are always equal or not if both ranges
621 // consist of a single value, and then compare them.
622 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
623 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
625 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
626 r = range_true (type);
627 else
628 r = range_false (type);
630 else
632 // If ranges do not intersect, we know the range is not equal,
633 // otherwise we don't know anything for sure.
634 int_range_max tmp = op1;
635 tmp.intersect (op2);
636 if (tmp.undefined_p ())
637 r = range_true (type);
638 else
639 r = range_true_and_false (type);
641 return true;
644 bool
645 operator_not_equal::op1_range (irange &r, tree type,
646 const irange &lhs,
647 const irange &op2,
648 relation_kind rel ATTRIBUTE_UNUSED) const
650 switch (get_bool_state (r, lhs, type))
652 case BRS_TRUE:
653 // If the result is true, the only time we know anything is if
654 // OP2 is a constant.
655 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
657 r = op2;
658 r.invert ();
660 else
661 r.set_varying (type);
662 break;
664 case BRS_FALSE:
665 // If it's false, the result is the same as OP2.
666 r = op2;
667 break;
669 default:
670 break;
672 return true;
676 bool
677 operator_not_equal::op2_range (irange &r, tree type,
678 const irange &lhs,
679 const irange &op1,
680 relation_kind rel) const
682 return operator_not_equal::op1_range (r, type, lhs, op1, rel);
685 // (X < VAL) produces the range of [MIN, VAL - 1].
687 static void
688 build_lt (irange &r, tree type, const wide_int &val)
690 wi::overflow_type ov;
691 wide_int lim;
692 signop sgn = TYPE_SIGN (type);
694 // Signed 1 bit cannot represent 1 for subtraction.
695 if (sgn == SIGNED)
696 lim = wi::add (val, -1, sgn, &ov);
697 else
698 lim = wi::sub (val, 1, sgn, &ov);
700 // If val - 1 underflows, check if X < MIN, which is an empty range.
701 if (ov)
702 r.set_undefined ();
703 else
704 r = int_range<1> (type, min_limit (type), lim);
707 // (X <= VAL) produces the range of [MIN, VAL].
709 static void
710 build_le (irange &r, tree type, const wide_int &val)
712 r = int_range<1> (type, min_limit (type), val);
715 // (X > VAL) produces the range of [VAL + 1, MAX].
717 static void
718 build_gt (irange &r, tree type, const wide_int &val)
720 wi::overflow_type ov;
721 wide_int lim;
722 signop sgn = TYPE_SIGN (type);
724 // Signed 1 bit cannot represent 1 for addition.
725 if (sgn == SIGNED)
726 lim = wi::sub (val, -1, sgn, &ov);
727 else
728 lim = wi::add (val, 1, sgn, &ov);
729 // If val + 1 overflows, check is for X > MAX, which is an empty range.
730 if (ov)
731 r.set_undefined ();
732 else
733 r = int_range<1> (type, lim, max_limit (type));
736 // (X >= val) produces the range of [VAL, MAX].
738 static void
739 build_ge (irange &r, tree type, const wide_int &val)
741 r = int_range<1> (type, val, max_limit (type));
745 class operator_lt : public range_operator
747 using range_operator::fold_range;
748 using range_operator::op1_range;
749 using range_operator::op2_range;
750 public:
751 virtual bool fold_range (irange &r, tree type,
752 const irange &op1,
753 const irange &op2,
754 relation_kind rel = VREL_VARYING) const;
755 virtual bool op1_range (irange &r, tree type,
756 const irange &lhs,
757 const irange &op2,
758 relation_kind rel = VREL_VARYING) const;
759 virtual bool op2_range (irange &r, tree type,
760 const irange &lhs,
761 const irange &op1,
762 relation_kind rel = VREL_VARYING) const;
763 virtual relation_kind op1_op2_relation (const irange &lhs) const;
764 } op_lt;
766 // Check if the LHS range indicates a relation between OP1 and OP2.
768 relation_kind
769 lt_op1_op2_relation (const irange &lhs)
771 if (lhs.undefined_p ())
772 return VREL_UNDEFINED;
774 // FALSE = op1 < op2 indicates GE_EXPR.
775 if (lhs.zero_p ())
776 return VREL_GE;
778 // TRUE = op1 < op2 indicates LT_EXPR.
779 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
780 return VREL_LT;
781 return VREL_VARYING;
784 relation_kind
785 operator_lt::op1_op2_relation (const irange &lhs) const
787 return lt_op1_op2_relation (lhs);
790 bool
791 operator_lt::fold_range (irange &r, tree type,
792 const irange &op1,
793 const irange &op2,
794 relation_kind rel) const
796 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
797 return true;
799 signop sign = TYPE_SIGN (op1.type ());
800 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
802 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
803 r = range_true (type);
804 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
805 r = range_false (type);
806 // Use nonzero bits to determine if < 0 is false.
807 else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
808 r = range_false (type);
809 else
810 r = range_true_and_false (type);
811 return true;
814 bool
815 operator_lt::op1_range (irange &r, tree type,
816 const irange &lhs,
817 const irange &op2,
818 relation_kind rel ATTRIBUTE_UNUSED) const
820 switch (get_bool_state (r, lhs, type))
822 case BRS_TRUE:
823 build_lt (r, type, op2.upper_bound ());
824 break;
826 case BRS_FALSE:
827 build_ge (r, type, op2.lower_bound ());
828 break;
830 default:
831 break;
833 return true;
836 bool
837 operator_lt::op2_range (irange &r, tree type,
838 const irange &lhs,
839 const irange &op1,
840 relation_kind rel ATTRIBUTE_UNUSED) const
842 switch (get_bool_state (r, lhs, type))
844 case BRS_FALSE:
845 build_le (r, type, op1.upper_bound ());
846 break;
848 case BRS_TRUE:
849 build_gt (r, type, op1.lower_bound ());
850 break;
852 default:
853 break;
855 return true;
859 class operator_le : public range_operator
861 using range_operator::fold_range;
862 using range_operator::op1_range;
863 using range_operator::op2_range;
864 public:
865 virtual bool fold_range (irange &r, tree type,
866 const irange &op1,
867 const irange &op2,
868 relation_kind rel = VREL_VARYING) const;
869 virtual bool op1_range (irange &r, tree type,
870 const irange &lhs,
871 const irange &op2,
872 relation_kind rel = VREL_VARYING) const;
873 virtual bool op2_range (irange &r, tree type,
874 const irange &lhs,
875 const irange &op1,
876 relation_kind rel = VREL_VARYING) const;
877 virtual relation_kind op1_op2_relation (const irange &lhs) const;
878 } op_le;
880 // Check if the LHS range indicates a relation between OP1 and OP2.
882 relation_kind
883 le_op1_op2_relation (const irange &lhs)
885 if (lhs.undefined_p ())
886 return VREL_UNDEFINED;
888 // FALSE = op1 <= op2 indicates GT_EXPR.
889 if (lhs.zero_p ())
890 return VREL_GT;
892 // TRUE = op1 <= op2 indicates LE_EXPR.
893 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
894 return VREL_LE;
895 return VREL_VARYING;
898 relation_kind
899 operator_le::op1_op2_relation (const irange &lhs) const
901 return le_op1_op2_relation (lhs);
904 bool
905 operator_le::fold_range (irange &r, tree type,
906 const irange &op1,
907 const irange &op2,
908 relation_kind rel) const
910 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
911 return true;
913 signop sign = TYPE_SIGN (op1.type ());
914 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
916 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
917 r = range_true (type);
918 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
919 r = range_false (type);
920 else
921 r = range_true_and_false (type);
922 return true;
925 bool
926 operator_le::op1_range (irange &r, tree type,
927 const irange &lhs,
928 const irange &op2,
929 relation_kind rel ATTRIBUTE_UNUSED) const
931 switch (get_bool_state (r, lhs, type))
933 case BRS_TRUE:
934 build_le (r, type, op2.upper_bound ());
935 break;
937 case BRS_FALSE:
938 build_gt (r, type, op2.lower_bound ());
939 break;
941 default:
942 break;
944 return true;
947 bool
948 operator_le::op2_range (irange &r, tree type,
949 const irange &lhs,
950 const irange &op1,
951 relation_kind rel ATTRIBUTE_UNUSED) const
953 switch (get_bool_state (r, lhs, type))
955 case BRS_FALSE:
956 build_lt (r, type, op1.upper_bound ());
957 break;
959 case BRS_TRUE:
960 build_ge (r, type, op1.lower_bound ());
961 break;
963 default:
964 break;
966 return true;
970 class operator_gt : public range_operator
972 using range_operator::fold_range;
973 using range_operator::op1_range;
974 using range_operator::op2_range;
975 public:
976 virtual bool fold_range (irange &r, tree type,
977 const irange &op1,
978 const irange &op2,
979 relation_kind rel = VREL_VARYING) const;
980 virtual bool op1_range (irange &r, tree type,
981 const irange &lhs,
982 const irange &op2,
983 relation_kind rel = VREL_VARYING) const;
984 virtual bool op2_range (irange &r, tree type,
985 const irange &lhs,
986 const irange &op1,
987 relation_kind rel = VREL_VARYING) const;
988 virtual relation_kind op1_op2_relation (const irange &lhs) const;
989 } op_gt;
991 // Check if the LHS range indicates a relation between OP1 and OP2.
993 relation_kind
994 gt_op1_op2_relation (const irange &lhs)
996 if (lhs.undefined_p ())
997 return VREL_UNDEFINED;
999 // FALSE = op1 > op2 indicates LE_EXPR.
1000 if (lhs.zero_p ())
1001 return VREL_LE;
1003 // TRUE = op1 > op2 indicates GT_EXPR.
1004 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1005 return VREL_GT;
1006 return VREL_VARYING;
1009 relation_kind
1010 operator_gt::op1_op2_relation (const irange &lhs) const
1012 return gt_op1_op2_relation (lhs);
1016 bool
1017 operator_gt::fold_range (irange &r, tree type,
1018 const irange &op1, const irange &op2,
1019 relation_kind rel) const
1021 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1022 return true;
1024 signop sign = TYPE_SIGN (op1.type ());
1025 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1027 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1028 r = range_true (type);
1029 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1030 r = range_false (type);
1031 else
1032 r = range_true_and_false (type);
1033 return true;
1036 bool
1037 operator_gt::op1_range (irange &r, tree type,
1038 const irange &lhs, const irange &op2,
1039 relation_kind rel ATTRIBUTE_UNUSED) const
1041 switch (get_bool_state (r, lhs, type))
1043 case BRS_TRUE:
1044 build_gt (r, type, op2.lower_bound ());
1045 break;
1047 case BRS_FALSE:
1048 build_le (r, type, op2.upper_bound ());
1049 break;
1051 default:
1052 break;
1054 return true;
1057 bool
1058 operator_gt::op2_range (irange &r, tree type,
1059 const irange &lhs,
1060 const irange &op1,
1061 relation_kind rel ATTRIBUTE_UNUSED) const
1063 switch (get_bool_state (r, lhs, type))
1065 case BRS_FALSE:
1066 build_ge (r, type, op1.lower_bound ());
1067 break;
1069 case BRS_TRUE:
1070 build_lt (r, type, op1.upper_bound ());
1071 break;
1073 default:
1074 break;
1076 return true;
1080 class operator_ge : public range_operator
1082 using range_operator::fold_range;
1083 using range_operator::op1_range;
1084 using range_operator::op2_range;
1085 public:
1086 virtual bool fold_range (irange &r, tree type,
1087 const irange &op1,
1088 const irange &op2,
1089 relation_kind rel = VREL_VARYING) const;
1090 virtual bool op1_range (irange &r, tree type,
1091 const irange &lhs,
1092 const irange &op2,
1093 relation_kind rel = VREL_VARYING) const;
1094 virtual bool op2_range (irange &r, tree type,
1095 const irange &lhs,
1096 const irange &op1,
1097 relation_kind rel = VREL_VARYING) const;
1098 virtual relation_kind op1_op2_relation (const irange &lhs) const;
1099 } op_ge;
1101 // Check if the LHS range indicates a relation between OP1 and OP2.
1103 relation_kind
1104 ge_op1_op2_relation (const irange &lhs)
1106 if (lhs.undefined_p ())
1107 return VREL_UNDEFINED;
1109 // FALSE = op1 >= op2 indicates LT_EXPR.
1110 if (lhs.zero_p ())
1111 return VREL_LT;
1113 // TRUE = op1 >= op2 indicates GE_EXPR.
1114 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1115 return VREL_GE;
1116 return VREL_VARYING;
1119 relation_kind
1120 operator_ge::op1_op2_relation (const irange &lhs) const
1122 return ge_op1_op2_relation (lhs);
1125 bool
1126 operator_ge::fold_range (irange &r, tree type,
1127 const irange &op1,
1128 const irange &op2,
1129 relation_kind rel) const
1131 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1132 return true;
1134 signop sign = TYPE_SIGN (op1.type ());
1135 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1137 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1138 r = range_true (type);
1139 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1140 r = range_false (type);
1141 else
1142 r = range_true_and_false (type);
1143 return true;
1146 bool
1147 operator_ge::op1_range (irange &r, tree type,
1148 const irange &lhs,
1149 const irange &op2,
1150 relation_kind rel ATTRIBUTE_UNUSED) const
1152 switch (get_bool_state (r, lhs, type))
1154 case BRS_TRUE:
1155 build_ge (r, type, op2.lower_bound ());
1156 break;
1158 case BRS_FALSE:
1159 build_lt (r, type, op2.upper_bound ());
1160 break;
1162 default:
1163 break;
1165 return true;
1168 bool
1169 operator_ge::op2_range (irange &r, tree type,
1170 const irange &lhs,
1171 const irange &op1,
1172 relation_kind rel ATTRIBUTE_UNUSED) const
1174 switch (get_bool_state (r, lhs, type))
1176 case BRS_FALSE:
1177 build_gt (r, type, op1.lower_bound ());
1178 break;
1180 case BRS_TRUE:
1181 build_le (r, type, op1.upper_bound ());
1182 break;
1184 default:
1185 break;
1187 return true;
1191 class operator_plus : public range_operator
1193 using range_operator::op1_range;
1194 using range_operator::op2_range;
1195 using range_operator::lhs_op1_relation;
1196 using range_operator::lhs_op2_relation;
1197 public:
1198 virtual bool op1_range (irange &r, tree type,
1199 const irange &lhs,
1200 const irange &op2,
1201 relation_kind rel ATTRIBUTE_UNUSED) const;
1202 virtual bool op2_range (irange &r, tree type,
1203 const irange &lhs,
1204 const irange &op1,
1205 relation_kind rel ATTRIBUTE_UNUSED) const;
1206 virtual void wi_fold (irange &r, tree type,
1207 const wide_int &lh_lb,
1208 const wide_int &lh_ub,
1209 const wide_int &rh_lb,
1210 const wide_int &rh_ub) const;
1211 virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
1212 const irange &op2,
1213 relation_kind rel) const;
1214 virtual relation_kind lhs_op2_relation (const irange &lhs, const irange &op1,
1215 const irange &op2,
1216 relation_kind rel) const;
1217 } op_plus;
1219 // Check to see if the range of OP2 indicates anything about the relation
1220 // between LHS and OP1.
1222 relation_kind
1223 operator_plus::lhs_op1_relation (const irange &lhs,
1224 const irange &op1,
1225 const irange &op2,
1226 relation_kind) const
1228 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1229 return VREL_VARYING;
1231 tree type = lhs.type ();
1232 unsigned prec = TYPE_PRECISION (type);
1233 wi::overflow_type ovf1, ovf2;
1234 signop sign = TYPE_SIGN (type);
1236 // LHS = OP1 + 0 indicates LHS == OP1.
1237 if (op2.zero_p ())
1238 return VREL_EQ;
1240 if (TYPE_OVERFLOW_WRAPS (type))
1242 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1243 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1245 else
1246 ovf1 = ovf2 = wi::OVF_NONE;
1248 // Never wrapping additions.
1249 if (!ovf1 && !ovf2)
1251 // Positive op2 means lhs > op1.
1252 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1253 return VREL_GT;
1254 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1255 return VREL_GE;
1257 // Negative op2 means lhs < op1.
1258 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1259 return VREL_LT;
1260 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1261 return VREL_LE;
1263 // Always wrapping additions.
1264 else if (ovf1 && ovf1 == ovf2)
1266 // Positive op2 means lhs < op1.
1267 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1268 return VREL_LT;
1269 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1270 return VREL_LE;
1272 // Negative op2 means lhs > op1.
1273 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1274 return VREL_GT;
1275 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1276 return VREL_GE;
1279 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1280 if (!range_includes_zero_p (&op2))
1281 return VREL_NE;
1283 return VREL_VARYING;
1286 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1287 // operands.
1289 relation_kind
1290 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1291 const irange &op2, relation_kind rel) const
1293 return lhs_op1_relation (lhs, op2, op1, rel);
1296 void
1297 operator_plus::wi_fold (irange &r, tree type,
1298 const wide_int &lh_lb, const wide_int &lh_ub,
1299 const wide_int &rh_lb, const wide_int &rh_ub) const
1301 wi::overflow_type ov_lb, ov_ub;
1302 signop s = TYPE_SIGN (type);
1303 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1304 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1305 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1308 bool
1309 operator_plus::op1_range (irange &r, tree type,
1310 const irange &lhs,
1311 const irange &op2,
1312 relation_kind rel ATTRIBUTE_UNUSED) const
1314 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, lhs, op2);
1317 bool
1318 operator_plus::op2_range (irange &r, tree type,
1319 const irange &lhs,
1320 const irange &op1,
1321 relation_kind rel ATTRIBUTE_UNUSED) const
1323 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, lhs, op1);
1327 class operator_minus : public range_operator
1329 using range_operator::fold_range;
1330 using range_operator::op1_range;
1331 using range_operator::op2_range;
1332 public:
1333 virtual bool op1_range (irange &r, tree type,
1334 const irange &lhs,
1335 const irange &op2,
1336 relation_kind rel ATTRIBUTE_UNUSED) const;
1337 virtual bool op2_range (irange &r, tree type,
1338 const irange &lhs,
1339 const irange &op1,
1340 relation_kind rel ATTRIBUTE_UNUSED) const;
1341 virtual void wi_fold (irange &r, tree type,
1342 const wide_int &lh_lb,
1343 const wide_int &lh_ub,
1344 const wide_int &rh_lb,
1345 const wide_int &rh_ub) const;
1346 virtual relation_kind lhs_op1_relation (const irange &lhs,
1347 const irange &op1,
1348 const irange &op2,
1349 relation_kind rel) const;
1350 virtual bool op1_op2_relation_effect (irange &lhs_range,
1351 tree type,
1352 const irange &op1_range,
1353 const irange &op2_range,
1354 relation_kind rel) const;
1355 } op_minus;
1357 void
1358 operator_minus::wi_fold (irange &r, tree type,
1359 const wide_int &lh_lb, const wide_int &lh_ub,
1360 const wide_int &rh_lb, const wide_int &rh_ub) const
1362 wi::overflow_type ov_lb, ov_ub;
1363 signop s = TYPE_SIGN (type);
1364 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1365 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1366 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1370 // Return the relation between LHS and OP1 based on the relation between
1371 // OP1 and OP2.
1373 relation_kind
1374 operator_minus::lhs_op1_relation (const irange &, const irange &op1,
1375 const irange &, relation_kind rel) const
1377 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
1378 switch (rel)
1380 case VREL_GT:
1381 return VREL_LT;
1382 case VREL_GE:
1383 return VREL_LE;
1384 default:
1385 break;
1387 return VREL_VARYING;
1390 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1391 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1392 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1394 static bool
1395 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1396 const irange &op1_range ATTRIBUTE_UNUSED,
1397 const irange &op2_range ATTRIBUTE_UNUSED,
1398 relation_kind rel)
1400 if (rel == VREL_VARYING)
1401 return false;
1403 int_range<2> rel_range;
1404 unsigned prec = TYPE_PRECISION (type);
1405 signop sgn = TYPE_SIGN (type);
1407 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1408 if (rel == VREL_EQ)
1409 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1410 else if (rel == VREL_NE)
1411 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1412 VR_ANTI_RANGE);
1413 else if (TYPE_OVERFLOW_WRAPS (type))
1415 switch (rel)
1417 // For wrapping signed values and unsigned, if op1 > op2 or
1418 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1419 case VREL_GT:
1420 case VREL_LT:
1421 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1422 VR_ANTI_RANGE);
1423 break;
1424 default:
1425 return false;
1428 else
1430 switch (rel)
1432 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1433 case VREL_GT:
1434 rel_range = int_range<2> (type, wi::one (prec),
1435 wi::max_value (prec, sgn));
1436 break;
1437 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1438 case VREL_GE:
1439 rel_range = int_range<2> (type, wi::zero (prec),
1440 wi::max_value (prec, sgn));
1441 break;
1442 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1443 case VREL_LT:
1444 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1445 wi::minus_one (prec));
1446 break;
1447 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1448 case VREL_LE:
1449 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1450 wi::zero (prec));
1451 break;
1452 default:
1453 return false;
1456 lhs_range.intersect (rel_range);
1457 return true;
1460 bool
1461 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1462 const irange &op1_range,
1463 const irange &op2_range,
1464 relation_kind rel) const
1466 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1467 rel);
1470 bool
1471 operator_minus::op1_range (irange &r, tree type,
1472 const irange &lhs,
1473 const irange &op2,
1474 relation_kind rel ATTRIBUTE_UNUSED) const
1476 return range_op_handler (PLUS_EXPR, type).fold_range (r, type, lhs, op2);
1479 bool
1480 operator_minus::op2_range (irange &r, tree type,
1481 const irange &lhs,
1482 const irange &op1,
1483 relation_kind rel ATTRIBUTE_UNUSED) const
1485 return fold_range (r, type, op1, lhs);
1489 class operator_pointer_diff : public range_operator
1491 virtual bool op1_op2_relation_effect (irange &lhs_range,
1492 tree type,
1493 const irange &op1_range,
1494 const irange &op2_range,
1495 relation_kind rel) const;
1496 } op_pointer_diff;
1498 bool
1499 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1500 const irange &op1_range,
1501 const irange &op2_range,
1502 relation_kind rel) const
1504 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1505 rel);
1509 class operator_min : public range_operator
1511 public:
1512 virtual void wi_fold (irange &r, tree type,
1513 const wide_int &lh_lb,
1514 const wide_int &lh_ub,
1515 const wide_int &rh_lb,
1516 const wide_int &rh_ub) const;
1517 } op_min;
1519 void
1520 operator_min::wi_fold (irange &r, tree type,
1521 const wide_int &lh_lb, const wide_int &lh_ub,
1522 const wide_int &rh_lb, const wide_int &rh_ub) const
1524 signop s = TYPE_SIGN (type);
1525 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1526 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1527 value_range_with_overflow (r, type, new_lb, new_ub);
1531 class operator_max : public range_operator
1533 public:
1534 virtual void wi_fold (irange &r, tree type,
1535 const wide_int &lh_lb,
1536 const wide_int &lh_ub,
1537 const wide_int &rh_lb,
1538 const wide_int &rh_ub) const;
1539 } op_max;
1541 void
1542 operator_max::wi_fold (irange &r, tree type,
1543 const wide_int &lh_lb, const wide_int &lh_ub,
1544 const wide_int &rh_lb, const wide_int &rh_ub) const
1546 signop s = TYPE_SIGN (type);
1547 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1548 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1549 value_range_with_overflow (r, type, new_lb, new_ub);
1553 class cross_product_operator : public range_operator
1555 public:
1556 // Perform an operation between two wide-ints and place the result
1557 // in R. Return true if the operation overflowed.
1558 virtual bool wi_op_overflows (wide_int &r,
1559 tree type,
1560 const wide_int &,
1561 const wide_int &) const = 0;
1563 // Calculate the cross product of two sets of sub-ranges and return it.
1564 void wi_cross_product (irange &r, tree type,
1565 const wide_int &lh_lb,
1566 const wide_int &lh_ub,
1567 const wide_int &rh_lb,
1568 const wide_int &rh_ub) const;
1571 // Calculate the cross product of two sets of ranges and return it.
1573 // Multiplications, divisions and shifts are a bit tricky to handle,
1574 // depending on the mix of signs we have in the two ranges, we need to
1575 // operate on different values to get the minimum and maximum values
1576 // for the new range. One approach is to figure out all the
1577 // variations of range combinations and do the operations.
1579 // However, this involves several calls to compare_values and it is
1580 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1581 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1582 // figure the smallest and largest values to form the new range.
1584 void
1585 cross_product_operator::wi_cross_product (irange &r, tree type,
1586 const wide_int &lh_lb,
1587 const wide_int &lh_ub,
1588 const wide_int &rh_lb,
1589 const wide_int &rh_ub) const
1591 wide_int cp1, cp2, cp3, cp4;
1592 // Default to varying.
1593 r.set_varying (type);
1595 // Compute the 4 cross operations, bailing if we get an overflow we
1596 // can't handle.
1597 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1598 return;
1599 if (wi::eq_p (lh_lb, lh_ub))
1600 cp3 = cp1;
1601 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1602 return;
1603 if (wi::eq_p (rh_lb, rh_ub))
1604 cp2 = cp1;
1605 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1606 return;
1607 if (wi::eq_p (lh_lb, lh_ub))
1608 cp4 = cp2;
1609 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1610 return;
1612 // Order pairs.
1613 signop sign = TYPE_SIGN (type);
1614 if (wi::gt_p (cp1, cp2, sign))
1615 std::swap (cp1, cp2);
1616 if (wi::gt_p (cp3, cp4, sign))
1617 std::swap (cp3, cp4);
1619 // Choose min and max from the ordered pairs.
1620 wide_int res_lb = wi::min (cp1, cp3, sign);
1621 wide_int res_ub = wi::max (cp2, cp4, sign);
1622 value_range_with_overflow (r, type, res_lb, res_ub);
1626 class operator_mult : public cross_product_operator
1628 using range_operator::op1_range;
1629 using range_operator::op2_range;
1630 public:
1631 virtual void wi_fold (irange &r, tree type,
1632 const wide_int &lh_lb,
1633 const wide_int &lh_ub,
1634 const wide_int &rh_lb,
1635 const wide_int &rh_ub) const;
1636 virtual bool wi_op_overflows (wide_int &res, tree type,
1637 const wide_int &w0, const wide_int &w1) const;
1638 virtual bool op1_range (irange &r, tree type,
1639 const irange &lhs,
1640 const irange &op2,
1641 relation_kind rel ATTRIBUTE_UNUSED) const;
1642 virtual bool op2_range (irange &r, tree type,
1643 const irange &lhs,
1644 const irange &op1,
1645 relation_kind rel ATTRIBUTE_UNUSED) const;
1646 } op_mult;
1648 bool
1649 operator_mult::op1_range (irange &r, tree type,
1650 const irange &lhs, const irange &op2,
1651 relation_kind rel ATTRIBUTE_UNUSED) const
1653 tree offset;
1655 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1656 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1657 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1658 if (TYPE_OVERFLOW_WRAPS (type))
1659 return false;
1661 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1662 return range_op_handler (TRUNC_DIV_EXPR, type).fold_range (r, type,
1663 lhs, op2);
1664 return false;
1667 bool
1668 operator_mult::op2_range (irange &r, tree type,
1669 const irange &lhs, const irange &op1,
1670 relation_kind rel) const
1672 return operator_mult::op1_range (r, type, lhs, op1, rel);
1675 bool
1676 operator_mult::wi_op_overflows (wide_int &res, tree type,
1677 const wide_int &w0, const wide_int &w1) const
1679 wi::overflow_type overflow = wi::OVF_NONE;
1680 signop sign = TYPE_SIGN (type);
1681 res = wi::mul (w0, w1, sign, &overflow);
1682 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1684 // For multiplication, the sign of the overflow is given
1685 // by the comparison of the signs of the operands.
1686 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1687 res = wi::max_value (w0.get_precision (), sign);
1688 else
1689 res = wi::min_value (w0.get_precision (), sign);
1690 return false;
1692 return overflow;
1695 void
1696 operator_mult::wi_fold (irange &r, tree type,
1697 const wide_int &lh_lb, const wide_int &lh_ub,
1698 const wide_int &rh_lb, const wide_int &rh_ub) const
1700 if (TYPE_OVERFLOW_UNDEFINED (type))
1702 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1703 return;
1706 // Multiply the ranges when overflow wraps. This is basically fancy
1707 // code so we don't drop to varying with an unsigned
1708 // [-3,-1]*[-3,-1].
1710 // This test requires 2*prec bits if both operands are signed and
1711 // 2*prec + 2 bits if either is not. Therefore, extend the values
1712 // using the sign of the result to PREC2. From here on out,
1713 // everthing is just signed math no matter what the input types
1714 // were.
1716 signop sign = TYPE_SIGN (type);
1717 unsigned prec = TYPE_PRECISION (type);
1718 widest2_int min0 = widest2_int::from (lh_lb, sign);
1719 widest2_int max0 = widest2_int::from (lh_ub, sign);
1720 widest2_int min1 = widest2_int::from (rh_lb, sign);
1721 widest2_int max1 = widest2_int::from (rh_ub, sign);
1722 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1723 widest2_int size = sizem1 + 1;
1725 // Canonicalize the intervals.
1726 if (sign == UNSIGNED)
1728 if (wi::ltu_p (size, min0 + max0))
1730 min0 -= size;
1731 max0 -= size;
1733 if (wi::ltu_p (size, min1 + max1))
1735 min1 -= size;
1736 max1 -= size;
1740 // Sort the 4 products so that min is in prod0 and max is in
1741 // prod3.
1742 widest2_int prod0 = min0 * min1;
1743 widest2_int prod1 = min0 * max1;
1744 widest2_int prod2 = max0 * min1;
1745 widest2_int prod3 = max0 * max1;
1747 // min0min1 > max0max1
1748 if (prod0 > prod3)
1749 std::swap (prod0, prod3);
1751 // min0max1 > max0min1
1752 if (prod1 > prod2)
1753 std::swap (prod1, prod2);
1755 if (prod0 > prod1)
1756 std::swap (prod0, prod1);
1758 if (prod2 > prod3)
1759 std::swap (prod2, prod3);
1761 // diff = max - min
1762 prod2 = prod3 - prod0;
1763 if (wi::geu_p (prod2, sizem1))
1764 // The range covers all values.
1765 r.set_varying (type);
1766 else
1768 wide_int new_lb = wide_int::from (prod0, prec, sign);
1769 wide_int new_ub = wide_int::from (prod3, prec, sign);
1770 create_possibly_reversed_range (r, type, new_lb, new_ub);
1775 class operator_div : public cross_product_operator
1777 public:
1778 operator_div (enum tree_code c) { code = c; }
1779 virtual void wi_fold (irange &r, tree type,
1780 const wide_int &lh_lb,
1781 const wide_int &lh_ub,
1782 const wide_int &rh_lb,
1783 const wide_int &rh_ub) const;
1784 virtual bool wi_op_overflows (wide_int &res, tree type,
1785 const wide_int &, const wide_int &) const;
1786 private:
1787 enum tree_code code;
1790 bool
1791 operator_div::wi_op_overflows (wide_int &res, tree type,
1792 const wide_int &w0, const wide_int &w1) const
1794 if (w1 == 0)
1795 return true;
1797 wi::overflow_type overflow = wi::OVF_NONE;
1798 signop sign = TYPE_SIGN (type);
1800 switch (code)
1802 case EXACT_DIV_EXPR:
1803 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1804 // operator_exact_divide. No need to handle it here.
1805 gcc_unreachable ();
1806 break;
1807 case TRUNC_DIV_EXPR:
1808 res = wi::div_trunc (w0, w1, sign, &overflow);
1809 break;
1810 case FLOOR_DIV_EXPR:
1811 res = wi::div_floor (w0, w1, sign, &overflow);
1812 break;
1813 case ROUND_DIV_EXPR:
1814 res = wi::div_round (w0, w1, sign, &overflow);
1815 break;
1816 case CEIL_DIV_EXPR:
1817 res = wi::div_ceil (w0, w1, sign, &overflow);
1818 break;
1819 default:
1820 gcc_unreachable ();
1823 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1825 // For division, the only case is -INF / -1 = +INF.
1826 res = wi::max_value (w0.get_precision (), sign);
1827 return false;
1829 return overflow;
1832 void
1833 operator_div::wi_fold (irange &r, tree type,
1834 const wide_int &lh_lb, const wide_int &lh_ub,
1835 const wide_int &rh_lb, const wide_int &rh_ub) const
1837 const wide_int dividend_min = lh_lb;
1838 const wide_int dividend_max = lh_ub;
1839 const wide_int divisor_min = rh_lb;
1840 const wide_int divisor_max = rh_ub;
1841 signop sign = TYPE_SIGN (type);
1842 unsigned prec = TYPE_PRECISION (type);
1843 wide_int extra_min, extra_max;
1845 // If we know we won't divide by zero, just do the division.
1846 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1848 wi_cross_product (r, type, dividend_min, dividend_max,
1849 divisor_min, divisor_max);
1850 return;
1853 // If we're definitely dividing by zero, there's nothing to do.
1854 if (wi_zero_p (type, divisor_min, divisor_max))
1856 r.set_undefined ();
1857 return;
1860 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1861 // skip any division by zero.
1863 // First divide by the negative numbers, if any.
1864 if (wi::neg_p (divisor_min, sign))
1865 wi_cross_product (r, type, dividend_min, dividend_max,
1866 divisor_min, wi::minus_one (prec));
1867 else
1868 r.set_undefined ();
1870 // Then divide by the non-zero positive numbers, if any.
1871 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1873 int_range_max tmp;
1874 wi_cross_product (tmp, type, dividend_min, dividend_max,
1875 wi::one (prec), divisor_max);
1876 r.union_ (tmp);
1878 // We shouldn't still have undefined here.
1879 gcc_checking_assert (!r.undefined_p ());
1882 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1883 operator_div op_floor_div (FLOOR_DIV_EXPR);
1884 operator_div op_round_div (ROUND_DIV_EXPR);
1885 operator_div op_ceil_div (CEIL_DIV_EXPR);
1888 class operator_exact_divide : public operator_div
1890 using range_operator::op1_range;
1891 public:
1892 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1893 virtual bool op1_range (irange &r, tree type,
1894 const irange &lhs,
1895 const irange &op2,
1896 relation_kind rel ATTRIBUTE_UNUSED) const;
1898 } op_exact_div;
1900 bool
1901 operator_exact_divide::op1_range (irange &r, tree type,
1902 const irange &lhs,
1903 const irange &op2,
1904 relation_kind rel ATTRIBUTE_UNUSED) const
1906 tree offset;
1907 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1908 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1909 // We wont bother trying to enumerate all the in between stuff :-P
1910 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1911 // the time however.
1912 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1913 if (op2.singleton_p (&offset)
1914 && !integer_zerop (offset))
1915 return range_op_handler (MULT_EXPR, type).fold_range (r, type, lhs, op2);
1916 return false;
1920 class operator_lshift : public cross_product_operator
1922 using range_operator::fold_range;
1923 using range_operator::op1_range;
1924 public:
1925 virtual bool op1_range (irange &r, tree type,
1926 const irange &lhs,
1927 const irange &op2,
1928 relation_kind rel = VREL_VARYING) const;
1929 virtual bool fold_range (irange &r, tree type,
1930 const irange &op1,
1931 const irange &op2,
1932 relation_kind rel = VREL_VARYING) const;
1934 virtual void wi_fold (irange &r, tree type,
1935 const wide_int &lh_lb, const wide_int &lh_ub,
1936 const wide_int &rh_lb, const wide_int &rh_ub) const;
1937 virtual bool wi_op_overflows (wide_int &res,
1938 tree type,
1939 const wide_int &,
1940 const wide_int &) const;
1941 } op_lshift;
1943 class operator_rshift : public cross_product_operator
1945 using range_operator::fold_range;
1946 using range_operator::op1_range;
1947 using range_operator::lhs_op1_relation;
1948 public:
1949 virtual bool fold_range (irange &r, tree type,
1950 const irange &op1,
1951 const irange &op2,
1952 relation_kind rel = VREL_VARYING) const;
1953 virtual void wi_fold (irange &r, tree type,
1954 const wide_int &lh_lb,
1955 const wide_int &lh_ub,
1956 const wide_int &rh_lb,
1957 const wide_int &rh_ub) const;
1958 virtual bool wi_op_overflows (wide_int &res,
1959 tree type,
1960 const wide_int &w0,
1961 const wide_int &w1) const;
1962 virtual bool op1_range (irange &, tree type,
1963 const irange &lhs,
1964 const irange &op2,
1965 relation_kind rel = VREL_VARYING) const;
1966 virtual relation_kind lhs_op1_relation (const irange &lhs,
1967 const irange &op1,
1968 const irange &op2,
1969 relation_kind rel) const;
1970 } op_rshift;
1973 relation_kind
1974 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
1975 const irange &op1,
1976 const irange &op2,
1977 relation_kind) const
1979 // If both operands range are >= 0, then the LHS <= op1.
1980 if (!op1.undefined_p () && !op2.undefined_p ()
1981 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
1982 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
1983 return VREL_LE;
1984 return VREL_VARYING;
1987 bool
1988 operator_lshift::fold_range (irange &r, tree type,
1989 const irange &op1,
1990 const irange &op2,
1991 relation_kind rel) const
1993 int_range_max shift_range;
1994 if (!get_shift_range (shift_range, type, op2))
1996 if (op2.undefined_p ())
1997 r.set_undefined ();
1998 else
1999 r.set_varying (type);
2000 return true;
2003 // Transform left shifts by constants into multiplies.
2004 if (shift_range.singleton_p ())
2006 unsigned shift = shift_range.lower_bound ().to_uhwi ();
2007 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2008 int_range<1> mult (type, tmp, tmp);
2010 // Force wrapping multiplication.
2011 bool saved_flag_wrapv = flag_wrapv;
2012 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2013 flag_wrapv = 1;
2014 flag_wrapv_pointer = 1;
2015 bool b = op_mult.fold_range (r, type, op1, mult);
2016 flag_wrapv = saved_flag_wrapv;
2017 flag_wrapv_pointer = saved_flag_wrapv_pointer;
2018 return b;
2020 else
2021 // Otherwise, invoke the generic fold routine.
2022 return range_operator::fold_range (r, type, op1, shift_range, rel);
2025 void
2026 operator_lshift::wi_fold (irange &r, tree type,
2027 const wide_int &lh_lb, const wide_int &lh_ub,
2028 const wide_int &rh_lb, const wide_int &rh_ub) const
2030 signop sign = TYPE_SIGN (type);
2031 unsigned prec = TYPE_PRECISION (type);
2032 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2033 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2034 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2035 // overflow. However, for that to happen, rh.max needs to be zero,
2036 // which means rh is a singleton range of zero, which means we simply return
2037 // [lh_lb, lh_ub] as the range.
2038 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2040 r = int_range<2> (type, lh_lb, lh_ub);
2041 return;
2044 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2045 wide_int complement = ~(bound - 1);
2046 wide_int low_bound, high_bound;
2047 bool in_bounds = false;
2049 if (sign == UNSIGNED)
2051 low_bound = bound;
2052 high_bound = complement;
2053 if (wi::ltu_p (lh_ub, low_bound))
2055 // [5, 6] << [1, 2] == [10, 24].
2056 // We're shifting out only zeroes, the value increases
2057 // monotonically.
2058 in_bounds = true;
2060 else if (wi::ltu_p (high_bound, lh_lb))
2062 // [0xffffff00, 0xffffffff] << [1, 2]
2063 // == [0xfffffc00, 0xfffffffe].
2064 // We're shifting out only ones, the value decreases
2065 // monotonically.
2066 in_bounds = true;
2069 else
2071 // [-1, 1] << [1, 2] == [-4, 4]
2072 low_bound = complement;
2073 high_bound = bound;
2074 if (wi::lts_p (lh_ub, high_bound)
2075 && wi::lts_p (low_bound, lh_lb))
2077 // For non-negative numbers, we're shifting out only zeroes,
2078 // the value increases monotonically. For negative numbers,
2079 // we're shifting out only ones, the value decreases
2080 // monotonically.
2081 in_bounds = true;
2085 if (in_bounds)
2086 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2087 else
2088 r.set_varying (type);
2091 bool
2092 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2093 const wide_int &w0, const wide_int &w1) const
2095 signop sign = TYPE_SIGN (type);
2096 if (wi::neg_p (w1))
2098 // It's unclear from the C standard whether shifts can overflow.
2099 // The following code ignores overflow; perhaps a C standard
2100 // interpretation ruling is needed.
2101 res = wi::rshift (w0, -w1, sign);
2103 else
2104 res = wi::lshift (w0, w1);
2105 return false;
2108 bool
2109 operator_lshift::op1_range (irange &r,
2110 tree type,
2111 const irange &lhs,
2112 const irange &op2,
2113 relation_kind rel ATTRIBUTE_UNUSED) const
2115 tree shift_amount;
2117 if (!lhs.contains_p (build_zero_cst (type)))
2118 r.set_nonzero (type);
2119 else
2120 r.set_varying (type);
2122 if (op2.singleton_p (&shift_amount))
2124 wide_int shift = wi::to_wide (shift_amount);
2125 if (wi::lt_p (shift, 0, SIGNED))
2126 return false;
2127 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2128 TYPE_PRECISION (op2.type ())),
2129 UNSIGNED))
2130 return false;
2131 if (shift == 0)
2133 r.intersect (lhs);
2134 return true;
2137 // Work completely in unsigned mode to start.
2138 tree utype = type;
2139 int_range_max tmp_range;
2140 if (TYPE_SIGN (type) == SIGNED)
2142 int_range_max tmp = lhs;
2143 utype = unsigned_type_for (type);
2144 range_cast (tmp, utype);
2145 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2147 else
2148 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2150 // Start with ranges which can produce the LHS by right shifting the
2151 // result by the shift amount.
2152 // ie [0x08, 0xF0] = op1 << 2 will start with
2153 // [00001000, 11110000] = op1 << 2
2154 // [0x02, 0x4C] aka [00000010, 00111100]
2156 // Then create a range from the LB with the least significant upper bit
2157 // set, to the upper bound with all the bits set.
2158 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2160 // Ideally we do this for each subrange, but just lump them all for now.
2161 unsigned low_bits = TYPE_PRECISION (utype)
2162 - TREE_INT_CST_LOW (shift_amount);
2163 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2164 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2165 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2166 int_range<2> fill_range (utype, new_lb, new_ub);
2167 tmp_range.union_ (fill_range);
2169 if (utype != type)
2170 range_cast (tmp_range, type);
2172 r.intersect (tmp_range);
2173 return true;
2176 return !r.varying_p ();
2179 bool
2180 operator_rshift::op1_range (irange &r,
2181 tree type,
2182 const irange &lhs,
2183 const irange &op2,
2184 relation_kind rel ATTRIBUTE_UNUSED) const
2186 tree shift;
2187 if (op2.singleton_p (&shift))
2189 // Ignore nonsensical shifts.
2190 unsigned prec = TYPE_PRECISION (type);
2191 if (wi::ge_p (wi::to_wide (shift),
2192 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2193 UNSIGNED))
2194 return false;
2195 if (wi::to_wide (shift) == 0)
2197 r = lhs;
2198 return true;
2201 // Folding the original operation may discard some impossible
2202 // ranges from the LHS.
2203 int_range_max lhs_refined;
2204 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2205 lhs_refined.intersect (lhs);
2206 if (lhs_refined.undefined_p ())
2208 r.set_undefined ();
2209 return true;
2211 int_range_max shift_range (shift, shift);
2212 int_range_max lb, ub;
2213 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2214 // LHS
2215 // 0000 0111 = OP1 >> 3
2217 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2218 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2219 // right hand side (0x07).
2220 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2221 fold_build2 (LSHIFT_EXPR, type,
2222 build_minus_one_cst (type),
2223 shift));
2224 int_range_max mask_range (build_zero_cst (type), mask);
2225 op_plus.fold_range (ub, type, lb, mask_range);
2226 r = lb;
2227 r.union_ (ub);
2228 if (!lhs_refined.contains_p (build_zero_cst (type)))
2230 mask_range.invert ();
2231 r.intersect (mask_range);
2233 return true;
2235 return false;
2238 bool
2239 operator_rshift::wi_op_overflows (wide_int &res,
2240 tree type,
2241 const wide_int &w0,
2242 const wide_int &w1) const
2244 signop sign = TYPE_SIGN (type);
2245 if (wi::neg_p (w1))
2246 res = wi::lshift (w0, -w1);
2247 else
2249 // It's unclear from the C standard whether shifts can overflow.
2250 // The following code ignores overflow; perhaps a C standard
2251 // interpretation ruling is needed.
2252 res = wi::rshift (w0, w1, sign);
2254 return false;
2257 bool
2258 operator_rshift::fold_range (irange &r, tree type,
2259 const irange &op1,
2260 const irange &op2,
2261 relation_kind rel) const
2263 int_range_max shift;
2264 if (!get_shift_range (shift, type, op2))
2266 if (op2.undefined_p ())
2267 r.set_undefined ();
2268 else
2269 r.set_varying (type);
2270 return true;
2273 return range_operator::fold_range (r, type, op1, shift, rel);
2276 void
2277 operator_rshift::wi_fold (irange &r, tree type,
2278 const wide_int &lh_lb, const wide_int &lh_ub,
2279 const wide_int &rh_lb, const wide_int &rh_ub) const
2281 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2285 class operator_cast: public range_operator
2287 using range_operator::fold_range;
2288 using range_operator::op1_range;
2289 public:
2290 virtual bool fold_range (irange &r, tree type,
2291 const irange &op1,
2292 const irange &op2,
2293 relation_kind rel = VREL_VARYING) const;
2294 virtual bool op1_range (irange &r, tree type,
2295 const irange &lhs,
2296 const irange &op2,
2297 relation_kind rel = VREL_VARYING) const;
2298 private:
2299 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2300 bool inside_domain_p (const wide_int &min, const wide_int &max,
2301 const irange &outer) const;
2302 void fold_pair (irange &r, unsigned index, const irange &inner,
2303 const irange &outer) const;
2304 } op_convert;
2306 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2308 inline bool
2309 operator_cast::truncating_cast_p (const irange &inner,
2310 const irange &outer) const
2312 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2315 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2317 bool
2318 operator_cast::inside_domain_p (const wide_int &min,
2319 const wide_int &max,
2320 const irange &range) const
2322 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2323 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2324 signop domain_sign = TYPE_SIGN (range.type ());
2325 return (wi::le_p (min, domain_max, domain_sign)
2326 && wi::le_p (max, domain_max, domain_sign)
2327 && wi::ge_p (min, domain_min, domain_sign)
2328 && wi::ge_p (max, domain_min, domain_sign));
2332 // Helper for fold_range which work on a pair at a time.
2334 void
2335 operator_cast::fold_pair (irange &r, unsigned index,
2336 const irange &inner,
2337 const irange &outer) const
2339 tree inner_type = inner.type ();
2340 tree outer_type = outer.type ();
2341 signop inner_sign = TYPE_SIGN (inner_type);
2342 unsigned outer_prec = TYPE_PRECISION (outer_type);
2344 // check to see if casting from INNER to OUTER is a conversion that
2345 // fits in the resulting OUTER type.
2346 wide_int inner_lb = inner.lower_bound (index);
2347 wide_int inner_ub = inner.upper_bound (index);
2348 if (truncating_cast_p (inner, outer))
2350 // We may be able to accomodate a truncating cast if the
2351 // resulting range can be represented in the target type...
2352 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2353 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2354 inner_sign) != 0)
2356 r.set_varying (outer_type);
2357 return;
2360 // ...but we must still verify that the final range fits in the
2361 // domain. This catches -fstrict-enum restrictions where the domain
2362 // range is smaller than what fits in the underlying type.
2363 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2364 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2365 if (inside_domain_p (min, max, outer))
2366 create_possibly_reversed_range (r, outer_type, min, max);
2367 else
2368 r.set_varying (outer_type);
2372 bool
2373 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2374 const irange &inner,
2375 const irange &outer,
2376 relation_kind rel ATTRIBUTE_UNUSED) const
2378 if (empty_range_varying (r, type, inner, outer))
2379 return true;
2381 gcc_checking_assert (outer.varying_p ());
2382 gcc_checking_assert (inner.num_pairs () > 0);
2384 // Avoid a temporary by folding the first pair directly into the result.
2385 fold_pair (r, 0, inner, outer);
2387 // Then process any additonal pairs by unioning with their results.
2388 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2390 int_range_max tmp;
2391 fold_pair (tmp, x, inner, outer);
2392 r.union_ (tmp);
2393 if (r.varying_p ())
2394 return true;
2396 return true;
2399 bool
2400 operator_cast::op1_range (irange &r, tree type,
2401 const irange &lhs,
2402 const irange &op2,
2403 relation_kind rel ATTRIBUTE_UNUSED) const
2405 tree lhs_type = lhs.type ();
2406 gcc_checking_assert (types_compatible_p (op2.type(), type));
2408 // If we are calculating a pointer, shortcut to what we really care about.
2409 if (POINTER_TYPE_P (type))
2411 // Conversion from other pointers or a constant (including 0/NULL)
2412 // are straightforward.
2413 if (POINTER_TYPE_P (lhs.type ())
2414 || (lhs.singleton_p ()
2415 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2417 r = lhs;
2418 range_cast (r, type);
2420 else
2422 // If the LHS is not a pointer nor a singleton, then it is
2423 // either VARYING or non-zero.
2424 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2425 r.set_nonzero (type);
2426 else
2427 r.set_varying (type);
2429 r.intersect (op2);
2430 return true;
2433 if (truncating_cast_p (op2, lhs))
2435 if (lhs.varying_p ())
2436 r.set_varying (type);
2437 else
2439 // We want to insert the LHS as an unsigned value since it
2440 // would not trigger the signed bit of the larger type.
2441 int_range_max converted_lhs = lhs;
2442 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2443 range_cast (converted_lhs, type);
2444 // Start by building the positive signed outer range for the type.
2445 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2446 TYPE_PRECISION (type));
2447 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2448 SIGNED));
2449 // For the signed part, we need to simply union the 2 ranges now.
2450 r.union_ (converted_lhs);
2452 // Create maximal negative number outside of LHS bits.
2453 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2454 TYPE_PRECISION (type));
2455 // Add this to the unsigned LHS range(s).
2456 int_range_max lim_range (type, lim, lim);
2457 int_range_max lhs_neg;
2458 range_op_handler (PLUS_EXPR, type).fold_range (lhs_neg, type,
2459 converted_lhs,
2460 lim_range);
2461 // lhs_neg now has all the negative versions of the LHS.
2462 // Now union in all the values from SIGNED MIN (0x80000) to
2463 // lim-1 in order to fill in all the ranges with the upper
2464 // bits set.
2466 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2467 // we don't need to create a range from min to lim-1
2468 // calculate neg range traps trying to create [lim, lim - 1].
2469 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2470 if (lim != min_val)
2472 int_range_max neg (type,
2473 wi::min_value (TYPE_PRECISION (type),
2474 SIGNED),
2475 lim - 1);
2476 lhs_neg.union_ (neg);
2478 // And finally, munge the signed and unsigned portions.
2479 r.union_ (lhs_neg);
2481 // And intersect with any known value passed in the extra operand.
2482 r.intersect (op2);
2483 return true;
2486 int_range_max tmp;
2487 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2488 tmp = lhs;
2489 else
2491 // The cast is not truncating, and the range is restricted to
2492 // the range of the RHS by this assignment.
2494 // Cast the range of the RHS to the type of the LHS.
2495 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2496 // Intersect this with the LHS range will produce the range,
2497 // which will be cast to the RHS type before returning.
2498 tmp.intersect (lhs);
2501 // Cast the calculated range to the type of the RHS.
2502 fold_range (r, type, tmp, int_range<1> (type));
2503 return true;
2507 class operator_logical_and : public range_operator
2509 using range_operator::fold_range;
2510 using range_operator::op1_range;
2511 using range_operator::op2_range;
2512 public:
2513 virtual bool fold_range (irange &r, tree type,
2514 const irange &lh,
2515 const irange &rh,
2516 relation_kind rel = VREL_VARYING) const;
2517 virtual bool op1_range (irange &r, tree type,
2518 const irange &lhs,
2519 const irange &op2,
2520 relation_kind rel = VREL_VARYING) const;
2521 virtual bool op2_range (irange &r, tree type,
2522 const irange &lhs,
2523 const irange &op1,
2524 relation_kind rel = VREL_VARYING) const;
2525 } op_logical_and;
2528 bool
2529 operator_logical_and::fold_range (irange &r, tree type,
2530 const irange &lh,
2531 const irange &rh,
2532 relation_kind rel ATTRIBUTE_UNUSED) const
2534 if (empty_range_varying (r, type, lh, rh))
2535 return true;
2537 // 0 && anything is 0.
2538 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2539 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2540 r = range_false (type);
2541 else if (lh.contains_p (build_zero_cst (lh.type ()))
2542 || rh.contains_p (build_zero_cst (rh.type ())))
2543 // To reach this point, there must be a logical 1 on each side, and
2544 // the only remaining question is whether there is a zero or not.
2545 r = range_true_and_false (type);
2546 else
2547 r = range_true (type);
2548 return true;
2551 bool
2552 operator_logical_and::op1_range (irange &r, tree type,
2553 const irange &lhs,
2554 const irange &op2 ATTRIBUTE_UNUSED,
2555 relation_kind rel ATTRIBUTE_UNUSED) const
2557 switch (get_bool_state (r, lhs, type))
2559 case BRS_TRUE:
2560 // A true result means both sides of the AND must be true.
2561 r = range_true (type);
2562 break;
2563 default:
2564 // Any other result means only one side has to be false, the
2565 // other side can be anything. So we cannot be sure of any
2566 // result here.
2567 r = range_true_and_false (type);
2568 break;
2570 return true;
2573 bool
2574 operator_logical_and::op2_range (irange &r, tree type,
2575 const irange &lhs,
2576 const irange &op1,
2577 relation_kind rel ATTRIBUTE_UNUSED) const
2579 return operator_logical_and::op1_range (r, type, lhs, op1);
2583 class operator_bitwise_and : public range_operator
2585 using range_operator::fold_range;
2586 using range_operator::op1_range;
2587 using range_operator::op2_range;
2588 public:
2589 virtual bool fold_range (irange &r, tree type,
2590 const irange &lh,
2591 const irange &rh,
2592 relation_kind rel = VREL_VARYING) const;
2593 virtual bool op1_range (irange &r, tree type,
2594 const irange &lhs,
2595 const irange &op2,
2596 relation_kind rel = VREL_VARYING) const;
2597 virtual bool op2_range (irange &r, tree type,
2598 const irange &lhs,
2599 const irange &op1,
2600 relation_kind rel = VREL_VARYING) const;
2601 virtual void wi_fold (irange &r, tree type,
2602 const wide_int &lh_lb,
2603 const wide_int &lh_ub,
2604 const wide_int &rh_lb,
2605 const wide_int &rh_ub) const;
2606 private:
2607 void simple_op1_range_solver (irange &r, tree type,
2608 const irange &lhs,
2609 const irange &op2) const;
2610 } op_bitwise_and;
2612 bool
2613 operator_bitwise_and::fold_range (irange &r, tree type,
2614 const irange &lh,
2615 const irange &rh,
2616 relation_kind rel ATTRIBUTE_UNUSED) const
2618 if (range_operator::fold_range (r, type, lh, rh))
2620 if (!lh.undefined_p () && !rh.undefined_p ())
2621 r.set_nonzero_bits (wi::bit_and (lh.get_nonzero_bits (),
2622 rh.get_nonzero_bits ()));
2623 return true;
2625 return false;
2629 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2630 // by considering the number of leading redundant sign bit copies.
2631 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2632 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2633 static bool
2634 wi_optimize_signed_bitwise_op (irange &r, tree type,
2635 const wide_int &lh_lb, const wide_int &lh_ub,
2636 const wide_int &rh_lb, const wide_int &rh_ub)
2638 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2639 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2640 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2641 if (new_clrsb == 0)
2642 return false;
2643 int type_prec = TYPE_PRECISION (type);
2644 int rprec = (type_prec - new_clrsb) - 1;
2645 value_range_with_overflow (r, type,
2646 wi::mask (rprec, true, type_prec),
2647 wi::mask (rprec, false, type_prec));
2648 return true;
2652 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2653 // possible. Basically, see if we can optimize:
2655 // [LB, UB] op Z
2656 // into:
2657 // [LB op Z, UB op Z]
2659 // If the optimization was successful, accumulate the range in R and
2660 // return TRUE.
2662 static bool
2663 wi_optimize_and_or (irange &r,
2664 enum tree_code code,
2665 tree type,
2666 const wide_int &lh_lb, const wide_int &lh_ub,
2667 const wide_int &rh_lb, const wide_int &rh_ub)
2669 // Calculate the singleton mask among the ranges, if any.
2670 wide_int lower_bound, upper_bound, mask;
2671 if (wi::eq_p (rh_lb, rh_ub))
2673 mask = rh_lb;
2674 lower_bound = lh_lb;
2675 upper_bound = lh_ub;
2677 else if (wi::eq_p (lh_lb, lh_ub))
2679 mask = lh_lb;
2680 lower_bound = rh_lb;
2681 upper_bound = rh_ub;
2683 else
2684 return false;
2686 // If Z is a constant which (for op | its bitwise not) has n
2687 // consecutive least significant bits cleared followed by m 1
2688 // consecutive bits set immediately above it and either
2689 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2691 // The least significant n bits of all the values in the range are
2692 // cleared or set, the m bits above it are preserved and any bits
2693 // above these are required to be the same for all values in the
2694 // range.
2695 wide_int w = mask;
2696 int m = 0, n = 0;
2697 if (code == BIT_IOR_EXPR)
2698 w = ~w;
2699 if (wi::eq_p (w, 0))
2700 n = w.get_precision ();
2701 else
2703 n = wi::ctz (w);
2704 w = ~(w | wi::mask (n, false, w.get_precision ()));
2705 if (wi::eq_p (w, 0))
2706 m = w.get_precision () - n;
2707 else
2708 m = wi::ctz (w) - n;
2710 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2711 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2712 return false;
2714 wide_int res_lb, res_ub;
2715 if (code == BIT_AND_EXPR)
2717 res_lb = wi::bit_and (lower_bound, mask);
2718 res_ub = wi::bit_and (upper_bound, mask);
2720 else if (code == BIT_IOR_EXPR)
2722 res_lb = wi::bit_or (lower_bound, mask);
2723 res_ub = wi::bit_or (upper_bound, mask);
2725 else
2726 gcc_unreachable ();
2727 value_range_with_overflow (r, type, res_lb, res_ub);
2729 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2730 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2732 int_range<2> tmp;
2733 tmp.set_nonzero (type);
2734 r.intersect (tmp);
2736 return true;
2739 // For range [LB, UB] compute two wide_int bit masks.
2741 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2742 // for all numbers in the range the bit is 0, otherwise it might be 0
2743 // or 1.
2745 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2746 // for all numbers in the range the bit is 1, otherwise it might be 0
2747 // or 1.
2749 void
2750 wi_set_zero_nonzero_bits (tree type,
2751 const wide_int &lb, const wide_int &ub,
2752 wide_int &maybe_nonzero,
2753 wide_int &mustbe_nonzero)
2755 signop sign = TYPE_SIGN (type);
2757 if (wi::eq_p (lb, ub))
2758 maybe_nonzero = mustbe_nonzero = lb;
2759 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2761 wide_int xor_mask = lb ^ ub;
2762 maybe_nonzero = lb | ub;
2763 mustbe_nonzero = lb & ub;
2764 if (xor_mask != 0)
2766 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2767 maybe_nonzero.get_precision ());
2768 maybe_nonzero = maybe_nonzero | mask;
2769 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2772 else
2774 maybe_nonzero = wi::minus_one (lb.get_precision ());
2775 mustbe_nonzero = wi::zero (lb.get_precision ());
2779 void
2780 operator_bitwise_and::wi_fold (irange &r, tree type,
2781 const wide_int &lh_lb,
2782 const wide_int &lh_ub,
2783 const wide_int &rh_lb,
2784 const wide_int &rh_ub) const
2786 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2787 return;
2789 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2790 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2791 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2792 maybe_nonzero_lh, mustbe_nonzero_lh);
2793 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2794 maybe_nonzero_rh, mustbe_nonzero_rh);
2796 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2797 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2798 signop sign = TYPE_SIGN (type);
2799 unsigned prec = TYPE_PRECISION (type);
2800 // If both input ranges contain only negative values, we can
2801 // truncate the result range maximum to the minimum of the
2802 // input range maxima.
2803 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2805 new_ub = wi::min (new_ub, lh_ub, sign);
2806 new_ub = wi::min (new_ub, rh_ub, sign);
2808 // If either input range contains only non-negative values
2809 // we can truncate the result range maximum to the respective
2810 // maximum of the input range.
2811 if (wi::ge_p (lh_lb, 0, sign))
2812 new_ub = wi::min (new_ub, lh_ub, sign);
2813 if (wi::ge_p (rh_lb, 0, sign))
2814 new_ub = wi::min (new_ub, rh_ub, sign);
2815 // PR68217: In case of signed & sign-bit-CST should
2816 // result in [-INF, 0] instead of [-INF, INF].
2817 if (wi::gt_p (new_lb, new_ub, sign))
2819 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2820 if (sign == SIGNED
2821 && ((wi::eq_p (lh_lb, lh_ub)
2822 && !wi::cmps (lh_lb, sign_bit))
2823 || (wi::eq_p (rh_lb, rh_ub)
2824 && !wi::cmps (rh_lb, sign_bit))))
2826 new_lb = wi::min_value (prec, sign);
2827 new_ub = wi::zero (prec);
2830 // If the limits got swapped around, return varying.
2831 if (wi::gt_p (new_lb, new_ub,sign))
2833 if (sign == SIGNED
2834 && wi_optimize_signed_bitwise_op (r, type,
2835 lh_lb, lh_ub,
2836 rh_lb, rh_ub))
2837 return;
2838 r.set_varying (type);
2840 else
2841 value_range_with_overflow (r, type, new_lb, new_ub);
2844 static void
2845 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2847 if (!lhs.contains_p (build_zero_cst (type)))
2848 r = range_nonzero (type);
2849 else
2850 r.set_varying (type);
2853 // This was shamelessly stolen from register_edge_assert_for_2 and
2854 // adjusted to work with iranges.
2856 void
2857 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2858 const irange &lhs,
2859 const irange &op2) const
2861 if (!op2.singleton_p ())
2863 set_nonzero_range_from_mask (r, type, lhs);
2864 return;
2866 unsigned int nprec = TYPE_PRECISION (type);
2867 wide_int cst2v = op2.lower_bound ();
2868 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2869 wide_int sgnbit;
2870 if (cst2n)
2871 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2872 else
2873 sgnbit = wi::zero (nprec);
2875 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2877 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2878 // maximum unsigned value is ~0. For signed comparison, if CST2
2879 // doesn't have the most significant bit set, handle it similarly. If
2880 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2881 wide_int valv = lhs.lower_bound ();
2882 wide_int minv = valv & cst2v, maxv;
2883 bool we_know_nothing = false;
2884 if (minv != valv)
2886 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2887 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2888 if (minv == valv)
2890 // If we can't determine anything on this bound, fall
2891 // through and conservatively solve for the other end point.
2892 we_know_nothing = true;
2895 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2896 if (we_know_nothing)
2897 r.set_varying (type);
2898 else
2899 r = int_range<1> (type, minv, maxv);
2901 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2903 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2904 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2905 // VAL2 where
2906 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2907 // as maximum.
2908 // For signed comparison, if CST2 doesn't have most significant bit
2909 // set, handle it similarly. If CST2 has MSB set, the maximum is
2910 // the same and minimum is INT_MIN.
2911 valv = lhs.upper_bound ();
2912 minv = valv & cst2v;
2913 if (minv == valv)
2914 maxv = valv;
2915 else
2917 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2918 if (maxv == valv)
2920 // If we couldn't determine anything on either bound, return
2921 // undefined.
2922 if (we_know_nothing)
2923 r.set_undefined ();
2924 return;
2926 maxv -= 1;
2928 maxv |= ~cst2v;
2929 minv = sgnbit;
2930 int_range<1> upper_bits (type, minv, maxv);
2931 r.intersect (upper_bits);
2934 bool
2935 operator_bitwise_and::op1_range (irange &r, tree type,
2936 const irange &lhs,
2937 const irange &op2,
2938 relation_kind rel ATTRIBUTE_UNUSED) const
2940 if (types_compatible_p (type, boolean_type_node))
2941 return op_logical_and.op1_range (r, type, lhs, op2);
2943 r.set_undefined ();
2944 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
2946 int_range_max chunk (lhs.type (),
2947 lhs.lower_bound (i),
2948 lhs.upper_bound (i));
2949 int_range_max res;
2950 simple_op1_range_solver (res, type, chunk, op2);
2951 r.union_ (res);
2953 if (r.undefined_p ())
2954 set_nonzero_range_from_mask (r, type, lhs);
2955 return true;
2958 bool
2959 operator_bitwise_and::op2_range (irange &r, tree type,
2960 const irange &lhs,
2961 const irange &op1,
2962 relation_kind rel ATTRIBUTE_UNUSED) const
2964 return operator_bitwise_and::op1_range (r, type, lhs, op1);
2968 class operator_logical_or : public range_operator
2970 using range_operator::fold_range;
2971 using range_operator::op1_range;
2972 using range_operator::op2_range;
2973 public:
2974 virtual bool fold_range (irange &r, tree type,
2975 const irange &lh,
2976 const irange &rh,
2977 relation_kind rel = VREL_VARYING) const;
2978 virtual bool op1_range (irange &r, tree type,
2979 const irange &lhs,
2980 const irange &op2,
2981 relation_kind rel = VREL_VARYING) const;
2982 virtual bool op2_range (irange &r, tree type,
2983 const irange &lhs,
2984 const irange &op1,
2985 relation_kind rel = VREL_VARYING) const;
2986 } op_logical_or;
2988 bool
2989 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2990 const irange &lh,
2991 const irange &rh,
2992 relation_kind rel ATTRIBUTE_UNUSED) const
2994 if (empty_range_varying (r, type, lh, rh))
2995 return true;
2997 r = lh;
2998 r.union_ (rh);
2999 return true;
3002 bool
3003 operator_logical_or::op1_range (irange &r, tree type,
3004 const irange &lhs,
3005 const irange &op2 ATTRIBUTE_UNUSED,
3006 relation_kind rel ATTRIBUTE_UNUSED) const
3008 switch (get_bool_state (r, lhs, type))
3010 case BRS_FALSE:
3011 // A false result means both sides of the OR must be false.
3012 r = range_false (type);
3013 break;
3014 default:
3015 // Any other result means only one side has to be true, the
3016 // other side can be anything. so we can't be sure of any result
3017 // here.
3018 r = range_true_and_false (type);
3019 break;
3021 return true;
3024 bool
3025 operator_logical_or::op2_range (irange &r, tree type,
3026 const irange &lhs,
3027 const irange &op1,
3028 relation_kind rel ATTRIBUTE_UNUSED) const
3030 return operator_logical_or::op1_range (r, type, lhs, op1);
3034 class operator_bitwise_or : public range_operator
3036 using range_operator::op1_range;
3037 using range_operator::op2_range;
3038 public:
3039 virtual bool op1_range (irange &r, tree type,
3040 const irange &lhs,
3041 const irange &op2,
3042 relation_kind rel = VREL_VARYING) const;
3043 virtual bool op2_range (irange &r, tree type,
3044 const irange &lhs,
3045 const irange &op1,
3046 relation_kind rel= VREL_VARYING) const;
3047 virtual void wi_fold (irange &r, tree type,
3048 const wide_int &lh_lb,
3049 const wide_int &lh_ub,
3050 const wide_int &rh_lb,
3051 const wide_int &rh_ub) const;
3052 } op_bitwise_or;
3054 void
3055 operator_bitwise_or::wi_fold (irange &r, tree type,
3056 const wide_int &lh_lb,
3057 const wide_int &lh_ub,
3058 const wide_int &rh_lb,
3059 const wide_int &rh_ub) const
3061 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3062 return;
3064 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3065 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3066 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3067 maybe_nonzero_lh, mustbe_nonzero_lh);
3068 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3069 maybe_nonzero_rh, mustbe_nonzero_rh);
3070 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3071 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3072 signop sign = TYPE_SIGN (type);
3073 // If the input ranges contain only positive values we can
3074 // truncate the minimum of the result range to the maximum
3075 // of the input range minima.
3076 if (wi::ge_p (lh_lb, 0, sign)
3077 && wi::ge_p (rh_lb, 0, sign))
3079 new_lb = wi::max (new_lb, lh_lb, sign);
3080 new_lb = wi::max (new_lb, rh_lb, sign);
3082 // If either input range contains only negative values
3083 // we can truncate the minimum of the result range to the
3084 // respective minimum range.
3085 if (wi::lt_p (lh_ub, 0, sign))
3086 new_lb = wi::max (new_lb, lh_lb, sign);
3087 if (wi::lt_p (rh_ub, 0, sign))
3088 new_lb = wi::max (new_lb, rh_lb, sign);
3089 // If the limits got swapped around, return a conservative range.
3090 if (wi::gt_p (new_lb, new_ub, sign))
3092 // Make sure that nonzero|X is nonzero.
3093 if (wi::gt_p (lh_lb, 0, sign)
3094 || wi::gt_p (rh_lb, 0, sign)
3095 || wi::lt_p (lh_ub, 0, sign)
3096 || wi::lt_p (rh_ub, 0, sign))
3097 r.set_nonzero (type);
3098 else if (sign == SIGNED
3099 && wi_optimize_signed_bitwise_op (r, type,
3100 lh_lb, lh_ub,
3101 rh_lb, rh_ub))
3102 return;
3103 else
3104 r.set_varying (type);
3105 return;
3107 value_range_with_overflow (r, type, new_lb, new_ub);
3110 bool
3111 operator_bitwise_or::op1_range (irange &r, tree type,
3112 const irange &lhs,
3113 const irange &op2,
3114 relation_kind rel ATTRIBUTE_UNUSED) const
3116 // If this is really a logical wi_fold, call that.
3117 if (types_compatible_p (type, boolean_type_node))
3118 return op_logical_or.op1_range (r, type, lhs, op2);
3120 if (lhs.zero_p ())
3122 tree zero = build_zero_cst (type);
3123 r = int_range<1> (zero, zero);
3124 return true;
3126 r.set_varying (type);
3127 return true;
3130 bool
3131 operator_bitwise_or::op2_range (irange &r, tree type,
3132 const irange &lhs,
3133 const irange &op1,
3134 relation_kind rel ATTRIBUTE_UNUSED) const
3136 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3140 class operator_bitwise_xor : public range_operator
3142 using range_operator::op1_range;
3143 using range_operator::op2_range;
3144 public:
3145 virtual void wi_fold (irange &r, tree type,
3146 const wide_int &lh_lb,
3147 const wide_int &lh_ub,
3148 const wide_int &rh_lb,
3149 const wide_int &rh_ub) const;
3150 virtual bool op1_range (irange &r, tree type,
3151 const irange &lhs,
3152 const irange &op2,
3153 relation_kind rel = VREL_VARYING) const;
3154 virtual bool op2_range (irange &r, tree type,
3155 const irange &lhs,
3156 const irange &op1,
3157 relation_kind rel = VREL_VARYING) const;
3158 virtual bool op1_op2_relation_effect (irange &lhs_range,
3159 tree type,
3160 const irange &op1_range,
3161 const irange &op2_range,
3162 relation_kind rel) const;
3163 } op_bitwise_xor;
3165 void
3166 operator_bitwise_xor::wi_fold (irange &r, tree type,
3167 const wide_int &lh_lb,
3168 const wide_int &lh_ub,
3169 const wide_int &rh_lb,
3170 const wide_int &rh_ub) const
3172 signop sign = TYPE_SIGN (type);
3173 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3174 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3175 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3176 maybe_nonzero_lh, mustbe_nonzero_lh);
3177 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3178 maybe_nonzero_rh, mustbe_nonzero_rh);
3180 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3181 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3182 wide_int result_one_bits
3183 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3184 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3185 wide_int new_ub = ~result_zero_bits;
3186 wide_int new_lb = result_one_bits;
3188 // If the range has all positive or all negative values, the result
3189 // is better than VARYING.
3190 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3191 value_range_with_overflow (r, type, new_lb, new_ub);
3192 else if (sign == SIGNED
3193 && wi_optimize_signed_bitwise_op (r, type,
3194 lh_lb, lh_ub,
3195 rh_lb, rh_ub))
3196 ; /* Do nothing. */
3197 else
3198 r.set_varying (type);
3200 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3201 if (wi::lt_p (lh_ub, rh_lb, sign)
3202 || wi::lt_p (rh_ub, lh_lb, sign)
3203 || wi::ne_p (result_one_bits, 0))
3205 int_range<2> tmp;
3206 tmp.set_nonzero (type);
3207 r.intersect (tmp);
3211 bool
3212 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3213 tree type,
3214 const irange &,
3215 const irange &,
3216 relation_kind rel) const
3218 if (rel == VREL_VARYING)
3219 return false;
3221 int_range<2> rel_range;
3223 switch (rel)
3225 case VREL_EQ:
3226 rel_range.set_zero (type);
3227 break;
3228 case VREL_NE:
3229 rel_range.set_nonzero (type);
3230 break;
3231 default:
3232 return false;
3235 lhs_range.intersect (rel_range);
3236 return true;
3239 bool
3240 operator_bitwise_xor::op1_range (irange &r, tree type,
3241 const irange &lhs,
3242 const irange &op2,
3243 relation_kind rel ATTRIBUTE_UNUSED) const
3245 if (lhs.undefined_p () || lhs.varying_p ())
3247 r = lhs;
3248 return true;
3250 if (types_compatible_p (type, boolean_type_node))
3252 switch (get_bool_state (r, lhs, type))
3254 case BRS_TRUE:
3255 if (op2.varying_p ())
3256 r.set_varying (type);
3257 else if (op2.zero_p ())
3258 r = range_true (type);
3259 else
3260 r = range_false (type);
3261 break;
3262 case BRS_FALSE:
3263 r = op2;
3264 break;
3265 default:
3266 break;
3268 return true;
3270 r.set_varying (type);
3271 return true;
3274 bool
3275 operator_bitwise_xor::op2_range (irange &r, tree type,
3276 const irange &lhs,
3277 const irange &op1,
3278 relation_kind rel ATTRIBUTE_UNUSED) const
3280 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3283 class operator_trunc_mod : public range_operator
3285 using range_operator::op1_range;
3286 using range_operator::op2_range;
3287 public:
3288 virtual void wi_fold (irange &r, tree type,
3289 const wide_int &lh_lb,
3290 const wide_int &lh_ub,
3291 const wide_int &rh_lb,
3292 const wide_int &rh_ub) const;
3293 virtual bool op1_range (irange &r, tree type,
3294 const irange &lhs,
3295 const irange &op2,
3296 relation_kind rel ATTRIBUTE_UNUSED) const;
3297 virtual bool op2_range (irange &r, tree type,
3298 const irange &lhs,
3299 const irange &op1,
3300 relation_kind rel ATTRIBUTE_UNUSED) const;
3301 } op_trunc_mod;
3303 void
3304 operator_trunc_mod::wi_fold (irange &r, tree type,
3305 const wide_int &lh_lb,
3306 const wide_int &lh_ub,
3307 const wide_int &rh_lb,
3308 const wide_int &rh_ub) const
3310 wide_int new_lb, new_ub, tmp;
3311 signop sign = TYPE_SIGN (type);
3312 unsigned prec = TYPE_PRECISION (type);
3314 // Mod 0 is undefined.
3315 if (wi_zero_p (type, rh_lb, rh_ub))
3317 r.set_undefined ();
3318 return;
3321 // Check for constant and try to fold.
3322 if (lh_lb == lh_ub && rh_lb == rh_ub)
3324 wi::overflow_type ov = wi::OVF_NONE;
3325 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3326 if (ov == wi::OVF_NONE)
3328 r = int_range<2> (type, tmp, tmp);
3329 return;
3333 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3334 new_ub = rh_ub - 1;
3335 if (sign == SIGNED)
3337 tmp = -1 - rh_lb;
3338 new_ub = wi::smax (new_ub, tmp);
3341 if (sign == UNSIGNED)
3342 new_lb = wi::zero (prec);
3343 else
3345 new_lb = -new_ub;
3346 tmp = lh_lb;
3347 if (wi::gts_p (tmp, 0))
3348 tmp = wi::zero (prec);
3349 new_lb = wi::smax (new_lb, tmp);
3351 tmp = lh_ub;
3352 if (sign == SIGNED && wi::neg_p (tmp))
3353 tmp = wi::zero (prec);
3354 new_ub = wi::min (new_ub, tmp, sign);
3356 value_range_with_overflow (r, type, new_lb, new_ub);
3359 bool
3360 operator_trunc_mod::op1_range (irange &r, tree type,
3361 const irange &lhs,
3362 const irange &,
3363 relation_kind rel ATTRIBUTE_UNUSED) const
3365 // PR 91029.
3366 signop sign = TYPE_SIGN (type);
3367 unsigned prec = TYPE_PRECISION (type);
3368 // (a % b) >= x && x > 0 , then a >= x.
3369 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3371 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3372 return true;
3374 // (a % b) <= x && x < 0 , then a <= x.
3375 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3377 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3378 return true;
3380 return false;
3383 bool
3384 operator_trunc_mod::op2_range (irange &r, tree type,
3385 const irange &lhs,
3386 const irange &,
3387 relation_kind rel ATTRIBUTE_UNUSED) const
3389 // PR 91029.
3390 signop sign = TYPE_SIGN (type);
3391 unsigned prec = TYPE_PRECISION (type);
3392 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3393 // or b > x for unsigned.
3394 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3396 if (sign == SIGNED)
3397 r = value_range (type, wi::neg (lhs.lower_bound ()),
3398 lhs.lower_bound (), VR_ANTI_RANGE);
3399 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3400 sign))
3401 r = value_range (type, lhs.lower_bound () + 1,
3402 wi::max_value (prec, sign));
3403 else
3404 return false;
3405 return true;
3407 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3408 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3410 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3411 r = value_range (type, lhs.upper_bound (),
3412 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3413 else
3414 return false;
3415 return true;
3417 return false;
3421 class operator_logical_not : public range_operator
3423 using range_operator::fold_range;
3424 using range_operator::op1_range;
3425 public:
3426 virtual bool fold_range (irange &r, tree type,
3427 const irange &lh,
3428 const irange &rh,
3429 relation_kind rel = VREL_VARYING) const;
3430 virtual bool op1_range (irange &r, tree type,
3431 const irange &lhs,
3432 const irange &op2,
3433 relation_kind rel = VREL_VARYING) const;
3434 } op_logical_not;
3436 // Folding a logical NOT, oddly enough, involves doing nothing on the
3437 // forward pass through. During the initial walk backwards, the
3438 // logical NOT reversed the desired outcome on the way back, so on the
3439 // way forward all we do is pass the range forward.
3441 // b_2 = x_1 < 20
3442 // b_3 = !b_2
3443 // if (b_3)
3444 // to determine the TRUE branch, walking backward
3445 // if (b_3) if ([1,1])
3446 // b_3 = !b_2 [1,1] = ![0,0]
3447 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3448 // which is the result we are looking for.. so.. pass it through.
3450 bool
3451 operator_logical_not::fold_range (irange &r, tree type,
3452 const irange &lh,
3453 const irange &rh ATTRIBUTE_UNUSED,
3454 relation_kind rel ATTRIBUTE_UNUSED) const
3456 if (empty_range_varying (r, type, lh, rh))
3457 return true;
3459 r = lh;
3460 if (!lh.varying_p () && !lh.undefined_p ())
3461 r.invert ();
3463 return true;
3466 bool
3467 operator_logical_not::op1_range (irange &r,
3468 tree type,
3469 const irange &lhs,
3470 const irange &op2,
3471 relation_kind rel ATTRIBUTE_UNUSED) const
3473 // Logical NOT is involutary...do it again.
3474 return fold_range (r, type, lhs, op2);
3478 class operator_bitwise_not : public range_operator
3480 using range_operator::fold_range;
3481 using range_operator::op1_range;
3482 public:
3483 virtual bool fold_range (irange &r, tree type,
3484 const irange &lh,
3485 const irange &rh,
3486 relation_kind rel = VREL_VARYING) const;
3487 virtual bool op1_range (irange &r, tree type,
3488 const irange &lhs,
3489 const irange &op2,
3490 relation_kind rel = VREL_VARYING) const;
3491 } op_bitwise_not;
3493 bool
3494 operator_bitwise_not::fold_range (irange &r, tree type,
3495 const irange &lh,
3496 const irange &rh,
3497 relation_kind rel ATTRIBUTE_UNUSED) const
3499 if (empty_range_varying (r, type, lh, rh))
3500 return true;
3502 if (types_compatible_p (type, boolean_type_node))
3503 return op_logical_not.fold_range (r, type, lh, rh);
3505 // ~X is simply -1 - X.
3506 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3507 wi::minus_one (TYPE_PRECISION (type)));
3508 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, minusone, lh);
3511 bool
3512 operator_bitwise_not::op1_range (irange &r, tree type,
3513 const irange &lhs,
3514 const irange &op2,
3515 relation_kind rel ATTRIBUTE_UNUSED) const
3517 if (types_compatible_p (type, boolean_type_node))
3518 return op_logical_not.op1_range (r, type, lhs, op2);
3520 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3521 return fold_range (r, type, lhs, op2);
3525 class operator_cst : public range_operator
3527 using range_operator::fold_range;
3528 public:
3529 virtual bool fold_range (irange &r, tree type,
3530 const irange &op1,
3531 const irange &op2,
3532 relation_kind rel = VREL_VARYING) const;
3533 } op_integer_cst;
3535 bool
3536 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3537 const irange &lh,
3538 const irange &rh ATTRIBUTE_UNUSED,
3539 relation_kind rel ATTRIBUTE_UNUSED) const
3541 r = lh;
3542 return true;
3546 class operator_identity : public range_operator
3548 using range_operator::fold_range;
3549 using range_operator::op1_range;
3550 using range_operator::lhs_op1_relation;
3551 public:
3552 virtual bool fold_range (irange &r, tree type,
3553 const irange &op1,
3554 const irange &op2,
3555 relation_kind rel = VREL_VARYING) const;
3556 virtual bool op1_range (irange &r, tree type,
3557 const irange &lhs,
3558 const irange &op2,
3559 relation_kind rel = VREL_VARYING) const;
3560 virtual relation_kind lhs_op1_relation (const irange &lhs,
3561 const irange &op1,
3562 const irange &op2,
3563 relation_kind rel) const;
3564 } op_identity;
3566 // Determine if there is a relationship between LHS and OP1.
3568 relation_kind
3569 operator_identity::lhs_op1_relation (const irange &lhs,
3570 const irange &op1 ATTRIBUTE_UNUSED,
3571 const irange &op2 ATTRIBUTE_UNUSED,
3572 relation_kind) const
3574 if (lhs.undefined_p ())
3575 return VREL_VARYING;
3576 // Simply a copy, so they are equivalent.
3577 return VREL_EQ;
3580 bool
3581 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3582 const irange &lh,
3583 const irange &rh ATTRIBUTE_UNUSED,
3584 relation_kind rel ATTRIBUTE_UNUSED) const
3586 r = lh;
3587 return true;
3590 bool
3591 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3592 const irange &lhs,
3593 const irange &op2 ATTRIBUTE_UNUSED,
3594 relation_kind rel ATTRIBUTE_UNUSED) const
3596 r = lhs;
3597 return true;
3601 class operator_unknown : public range_operator
3603 using range_operator::fold_range;
3604 public:
3605 virtual bool fold_range (irange &r, tree type,
3606 const irange &op1,
3607 const irange &op2,
3608 relation_kind rel = VREL_VARYING) const;
3609 } op_unknown;
3611 bool
3612 operator_unknown::fold_range (irange &r, tree type,
3613 const irange &lh ATTRIBUTE_UNUSED,
3614 const irange &rh ATTRIBUTE_UNUSED,
3615 relation_kind rel ATTRIBUTE_UNUSED) const
3617 r.set_varying (type);
3618 return true;
3622 class operator_abs : public range_operator
3624 using range_operator::op1_range;
3625 public:
3626 virtual void wi_fold (irange &r, tree type,
3627 const wide_int &lh_lb,
3628 const wide_int &lh_ub,
3629 const wide_int &rh_lb,
3630 const wide_int &rh_ub) const;
3631 virtual bool op1_range (irange &r, tree type,
3632 const irange &lhs,
3633 const irange &op2,
3634 relation_kind rel ATTRIBUTE_UNUSED) const;
3635 } op_abs;
3637 void
3638 operator_abs::wi_fold (irange &r, tree type,
3639 const wide_int &lh_lb, const wide_int &lh_ub,
3640 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3641 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3643 wide_int min, max;
3644 signop sign = TYPE_SIGN (type);
3645 unsigned prec = TYPE_PRECISION (type);
3647 // Pass through LH for the easy cases.
3648 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3650 r = int_range<1> (type, lh_lb, lh_ub);
3651 return;
3654 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3655 // a useful range.
3656 wide_int min_value = wi::min_value (prec, sign);
3657 wide_int max_value = wi::max_value (prec, sign);
3658 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3660 r.set_varying (type);
3661 return;
3664 // ABS_EXPR may flip the range around, if the original range
3665 // included negative values.
3666 if (wi::eq_p (lh_lb, min_value))
3668 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3669 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3670 if (wi::eq_p (lh_ub, min_value))
3672 r = int_range<1> (type, min_value, min_value);
3673 return;
3675 min = max_value;
3677 else
3678 min = wi::abs (lh_lb);
3680 if (wi::eq_p (lh_ub, min_value))
3681 max = max_value;
3682 else
3683 max = wi::abs (lh_ub);
3685 // If the range contains zero then we know that the minimum value in the
3686 // range will be zero.
3687 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3689 if (wi::gt_p (min, max, sign))
3690 max = min;
3691 min = wi::zero (prec);
3693 else
3695 // If the range was reversed, swap MIN and MAX.
3696 if (wi::gt_p (min, max, sign))
3697 std::swap (min, max);
3700 // If the new range has its limits swapped around (MIN > MAX), then
3701 // the operation caused one of them to wrap around. The only thing
3702 // we know is that the result is positive.
3703 if (wi::gt_p (min, max, sign))
3705 min = wi::zero (prec);
3706 max = max_value;
3708 r = int_range<1> (type, min, max);
3711 bool
3712 operator_abs::op1_range (irange &r, tree type,
3713 const irange &lhs,
3714 const irange &op2,
3715 relation_kind rel ATTRIBUTE_UNUSED) const
3717 if (empty_range_varying (r, type, lhs, op2))
3718 return true;
3719 if (TYPE_UNSIGNED (type))
3721 r = lhs;
3722 return true;
3724 // Start with the positives because negatives are an impossible result.
3725 int_range_max positives = range_positives (type);
3726 positives.intersect (lhs);
3727 r = positives;
3728 // Then add the negative of each pair:
3729 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3730 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3731 r.union_ (int_range<1> (type,
3732 -positives.upper_bound (i),
3733 -positives.lower_bound (i)));
3734 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3735 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3736 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3737 wide_int lb = lhs.lower_bound ();
3738 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3739 r.union_ (int_range<2> (type, lb, lb));
3740 return true;
3744 class operator_absu : public range_operator
3746 public:
3747 virtual void wi_fold (irange &r, tree type,
3748 const wide_int &lh_lb, const wide_int &lh_ub,
3749 const wide_int &rh_lb, const wide_int &rh_ub) const;
3750 } op_absu;
3752 void
3753 operator_absu::wi_fold (irange &r, tree type,
3754 const wide_int &lh_lb, const wide_int &lh_ub,
3755 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3756 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3758 wide_int new_lb, new_ub;
3760 // Pass through VR0 the easy cases.
3761 if (wi::ges_p (lh_lb, 0))
3763 new_lb = lh_lb;
3764 new_ub = lh_ub;
3766 else
3768 new_lb = wi::abs (lh_lb);
3769 new_ub = wi::abs (lh_ub);
3771 // If the range contains zero then we know that the minimum
3772 // value in the range will be zero.
3773 if (wi::ges_p (lh_ub, 0))
3775 if (wi::gtu_p (new_lb, new_ub))
3776 new_ub = new_lb;
3777 new_lb = wi::zero (TYPE_PRECISION (type));
3779 else
3780 std::swap (new_lb, new_ub);
3783 gcc_checking_assert (TYPE_UNSIGNED (type));
3784 r = int_range<1> (type, new_lb, new_ub);
3788 class operator_negate : public range_operator
3790 using range_operator::fold_range;
3791 using range_operator::op1_range;
3792 public:
3793 virtual bool fold_range (irange &r, tree type,
3794 const irange &op1,
3795 const irange &op2,
3796 relation_kind rel = VREL_VARYING) const;
3797 virtual bool op1_range (irange &r, tree type,
3798 const irange &lhs,
3799 const irange &op2,
3800 relation_kind rel = VREL_VARYING) const;
3801 } op_negate;
3803 bool
3804 operator_negate::fold_range (irange &r, tree type,
3805 const irange &lh,
3806 const irange &rh,
3807 relation_kind rel ATTRIBUTE_UNUSED) const
3809 if (empty_range_varying (r, type, lh, rh))
3810 return true;
3811 // -X is simply 0 - X.
3812 return range_op_handler (MINUS_EXPR, type).fold_range (r, type,
3813 range_zero (type), lh);
3816 bool
3817 operator_negate::op1_range (irange &r, tree type,
3818 const irange &lhs,
3819 const irange &op2,
3820 relation_kind rel ATTRIBUTE_UNUSED) const
3822 // NEGATE is involutory.
3823 return fold_range (r, type, lhs, op2);
3827 class operator_addr_expr : public range_operator
3829 using range_operator::fold_range;
3830 using range_operator::op1_range;
3831 public:
3832 virtual bool fold_range (irange &r, tree type,
3833 const irange &op1,
3834 const irange &op2,
3835 relation_kind rel = VREL_VARYING) const;
3836 virtual bool op1_range (irange &r, tree type,
3837 const irange &lhs,
3838 const irange &op2,
3839 relation_kind rel = VREL_VARYING) const;
3840 } op_addr;
3842 bool
3843 operator_addr_expr::fold_range (irange &r, tree type,
3844 const irange &lh,
3845 const irange &rh,
3846 relation_kind rel ATTRIBUTE_UNUSED) const
3848 if (empty_range_varying (r, type, lh, rh))
3849 return true;
3851 // Return a non-null pointer of the LHS type (passed in op2).
3852 if (lh.zero_p ())
3853 r = range_zero (type);
3854 else if (!lh.contains_p (build_zero_cst (lh.type ())))
3855 r = range_nonzero (type);
3856 else
3857 r.set_varying (type);
3858 return true;
3861 bool
3862 operator_addr_expr::op1_range (irange &r, tree type,
3863 const irange &lhs,
3864 const irange &op2,
3865 relation_kind rel ATTRIBUTE_UNUSED) const
3867 return operator_addr_expr::fold_range (r, type, lhs, op2);
3871 class pointer_plus_operator : public range_operator
3873 public:
3874 virtual void wi_fold (irange &r, tree type,
3875 const wide_int &lh_lb,
3876 const wide_int &lh_ub,
3877 const wide_int &rh_lb,
3878 const wide_int &rh_ub) const;
3879 } op_pointer_plus;
3881 void
3882 pointer_plus_operator::wi_fold (irange &r, tree type,
3883 const wide_int &lh_lb,
3884 const wide_int &lh_ub,
3885 const wide_int &rh_lb,
3886 const wide_int &rh_ub) const
3888 // Check for [0,0] + const, and simply return the const.
3889 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3891 tree val = wide_int_to_tree (type, rh_lb);
3892 r.set (val, val);
3893 return;
3896 // For pointer types, we are really only interested in asserting
3897 // whether the expression evaluates to non-NULL.
3899 // With -fno-delete-null-pointer-checks we need to be more
3900 // conservative. As some object might reside at address 0,
3901 // then some offset could be added to it and the same offset
3902 // subtracted again and the result would be NULL.
3903 // E.g.
3904 // static int a[12]; where &a[0] is NULL and
3905 // ptr = &a[6];
3906 // ptr -= 6;
3907 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3908 // where the first range doesn't include zero and the second one
3909 // doesn't either. As the second operand is sizetype (unsigned),
3910 // consider all ranges where the MSB could be set as possible
3911 // subtractions where the result might be NULL.
3912 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3913 || !wi_includes_zero_p (type, rh_lb, rh_ub))
3914 && !TYPE_OVERFLOW_WRAPS (type)
3915 && (flag_delete_null_pointer_checks
3916 || !wi::sign_mask (rh_ub)))
3917 r = range_nonzero (type);
3918 else if (lh_lb == lh_ub && lh_lb == 0
3919 && rh_lb == rh_ub && rh_lb == 0)
3920 r = range_zero (type);
3921 else
3922 r.set_varying (type);
3926 class pointer_min_max_operator : public range_operator
3928 public:
3929 virtual void wi_fold (irange & r, tree type,
3930 const wide_int &lh_lb, const wide_int &lh_ub,
3931 const wide_int &rh_lb, const wide_int &rh_ub) const;
3932 } op_ptr_min_max;
3934 void
3935 pointer_min_max_operator::wi_fold (irange &r, tree type,
3936 const wide_int &lh_lb,
3937 const wide_int &lh_ub,
3938 const wide_int &rh_lb,
3939 const wide_int &rh_ub) const
3941 // For MIN/MAX expressions with pointers, we only care about
3942 // nullness. If both are non null, then the result is nonnull.
3943 // If both are null, then the result is null. Otherwise they
3944 // are varying.
3945 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3946 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3947 r = range_nonzero (type);
3948 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3949 r = range_zero (type);
3950 else
3951 r.set_varying (type);
3955 class pointer_and_operator : public range_operator
3957 public:
3958 virtual void wi_fold (irange &r, tree type,
3959 const wide_int &lh_lb, const wide_int &lh_ub,
3960 const wide_int &rh_lb, const wide_int &rh_ub) const;
3961 } op_pointer_and;
3963 void
3964 pointer_and_operator::wi_fold (irange &r, tree type,
3965 const wide_int &lh_lb,
3966 const wide_int &lh_ub,
3967 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3968 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3970 // For pointer types, we are really only interested in asserting
3971 // whether the expression evaluates to non-NULL.
3972 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
3973 r = range_zero (type);
3974 else
3975 r.set_varying (type);
3979 class pointer_or_operator : public range_operator
3981 using range_operator::op1_range;
3982 using range_operator::op2_range;
3983 public:
3984 virtual bool op1_range (irange &r, tree type,
3985 const irange &lhs,
3986 const irange &op2,
3987 relation_kind rel = VREL_VARYING) const;
3988 virtual bool op2_range (irange &r, tree type,
3989 const irange &lhs,
3990 const irange &op1,
3991 relation_kind rel = VREL_VARYING) const;
3992 virtual void wi_fold (irange &r, tree type,
3993 const wide_int &lh_lb, const wide_int &lh_ub,
3994 const wide_int &rh_lb, const wide_int &rh_ub) const;
3995 } op_pointer_or;
3997 bool
3998 pointer_or_operator::op1_range (irange &r, tree type,
3999 const irange &lhs,
4000 const irange &op2 ATTRIBUTE_UNUSED,
4001 relation_kind rel ATTRIBUTE_UNUSED) const
4003 if (lhs.zero_p ())
4005 tree zero = build_zero_cst (type);
4006 r = int_range<1> (zero, zero);
4007 return true;
4009 r.set_varying (type);
4010 return true;
4013 bool
4014 pointer_or_operator::op2_range (irange &r, tree type,
4015 const irange &lhs,
4016 const irange &op1,
4017 relation_kind rel ATTRIBUTE_UNUSED) const
4019 return pointer_or_operator::op1_range (r, type, lhs, op1);
4022 void
4023 pointer_or_operator::wi_fold (irange &r, tree type,
4024 const wide_int &lh_lb,
4025 const wide_int &lh_ub,
4026 const wide_int &rh_lb,
4027 const wide_int &rh_ub) const
4029 // For pointer types, we are really only interested in asserting
4030 // whether the expression evaluates to non-NULL.
4031 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4032 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4033 r = range_nonzero (type);
4034 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4035 r = range_zero (type);
4036 else
4037 r.set_varying (type);
4040 // Return a pointer to the range_operator instance, if there is one
4041 // associated with tree_code CODE.
4043 range_operator *
4044 range_op_table::operator[] (enum tree_code code)
4046 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4047 return m_range_tree[code];
4050 // Add OP to the handler table for CODE.
4052 void
4053 range_op_table::set (enum tree_code code, range_operator &op)
4055 gcc_checking_assert (m_range_tree[code] == NULL);
4056 m_range_tree[code] = &op;
4059 // Instantiate a range op table for integral operations.
4061 class integral_table : public range_op_table
4063 public:
4064 integral_table ();
4065 } integral_tree_table;
4067 integral_table::integral_table ()
4069 set (EQ_EXPR, op_equal);
4070 set (NE_EXPR, op_not_equal);
4071 set (LT_EXPR, op_lt);
4072 set (LE_EXPR, op_le);
4073 set (GT_EXPR, op_gt);
4074 set (GE_EXPR, op_ge);
4075 set (PLUS_EXPR, op_plus);
4076 set (MINUS_EXPR, op_minus);
4077 set (MIN_EXPR, op_min);
4078 set (MAX_EXPR, op_max);
4079 set (MULT_EXPR, op_mult);
4080 set (TRUNC_DIV_EXPR, op_trunc_div);
4081 set (FLOOR_DIV_EXPR, op_floor_div);
4082 set (ROUND_DIV_EXPR, op_round_div);
4083 set (CEIL_DIV_EXPR, op_ceil_div);
4084 set (EXACT_DIV_EXPR, op_exact_div);
4085 set (LSHIFT_EXPR, op_lshift);
4086 set (RSHIFT_EXPR, op_rshift);
4087 set (NOP_EXPR, op_convert);
4088 set (CONVERT_EXPR, op_convert);
4089 set (TRUTH_AND_EXPR, op_logical_and);
4090 set (BIT_AND_EXPR, op_bitwise_and);
4091 set (TRUTH_OR_EXPR, op_logical_or);
4092 set (BIT_IOR_EXPR, op_bitwise_or);
4093 set (BIT_XOR_EXPR, op_bitwise_xor);
4094 set (TRUNC_MOD_EXPR, op_trunc_mod);
4095 set (TRUTH_NOT_EXPR, op_logical_not);
4096 set (BIT_NOT_EXPR, op_bitwise_not);
4097 set (INTEGER_CST, op_integer_cst);
4098 set (SSA_NAME, op_identity);
4099 set (PAREN_EXPR, op_identity);
4100 set (OBJ_TYPE_REF, op_identity);
4101 set (IMAGPART_EXPR, op_unknown);
4102 set (REALPART_EXPR, op_unknown);
4103 set (POINTER_DIFF_EXPR, op_pointer_diff);
4104 set (ABS_EXPR, op_abs);
4105 set (ABSU_EXPR, op_absu);
4106 set (NEGATE_EXPR, op_negate);
4107 set (ADDR_EXPR, op_addr);
4110 // Instantiate a range op table for pointer operations.
4112 class pointer_table : public range_op_table
4114 public:
4115 pointer_table ();
4116 } pointer_tree_table;
4118 pointer_table::pointer_table ()
4120 set (BIT_AND_EXPR, op_pointer_and);
4121 set (BIT_IOR_EXPR, op_pointer_or);
4122 set (MIN_EXPR, op_ptr_min_max);
4123 set (MAX_EXPR, op_ptr_min_max);
4124 set (POINTER_PLUS_EXPR, op_pointer_plus);
4126 set (EQ_EXPR, op_equal);
4127 set (NE_EXPR, op_not_equal);
4128 set (LT_EXPR, op_lt);
4129 set (LE_EXPR, op_le);
4130 set (GT_EXPR, op_gt);
4131 set (GE_EXPR, op_ge);
4132 set (SSA_NAME, op_identity);
4133 set (INTEGER_CST, op_integer_cst);
4134 set (ADDR_EXPR, op_addr);
4135 set (NOP_EXPR, op_convert);
4136 set (CONVERT_EXPR, op_convert);
4138 set (BIT_NOT_EXPR, op_bitwise_not);
4139 set (BIT_XOR_EXPR, op_bitwise_xor);
4142 // The tables are hidden and accessed via a simple extern function.
4144 static inline range_operator *
4145 get_handler (enum tree_code code, tree type)
4147 // First check if there is a pointer specialization.
4148 if (POINTER_TYPE_P (type))
4149 return pointer_tree_table[code];
4150 if (INTEGRAL_TYPE_P (type))
4151 return integral_tree_table[code];
4152 return NULL;
4155 range_op_handler::range_op_handler (tree_code code, tree type)
4157 m_op = get_handler (code, type);
4160 range_op_handler::range_op_handler (const gimple *s)
4162 if (const gassign *ass = dyn_cast<const gassign *> (s))
4164 enum tree_code code = gimple_assign_rhs_code (ass);
4165 // The LHS of a comparison is always an int, so we must look at
4166 // the operands.
4167 if (TREE_CODE_CLASS (code) == tcc_comparison)
4168 m_op = get_handler (code, TREE_TYPE (gimple_assign_rhs1 (ass)));
4169 else
4170 m_op = get_handler (code, TREE_TYPE (gimple_assign_lhs (ass)));
4172 else if (const gcond *cond = dyn_cast<const gcond *> (s))
4173 m_op = get_handler (gimple_cond_code (cond),
4174 TREE_TYPE (gimple_cond_lhs (cond)));
4175 else
4176 m_op = NULL;
4179 bool
4180 range_op_handler::fold_range (vrange &r, tree type,
4181 const vrange &lh,
4182 const vrange &rh,
4183 relation_kind rel) const
4185 if (is_a <irange> (lh))
4186 return m_op->fold_range (as_a <irange> (r), type,
4187 as_a <irange> (lh),
4188 as_a <irange> (rh), rel);
4189 gcc_unreachable ();
4190 return false;
4193 bool
4194 range_op_handler::op1_range (vrange &r, tree type,
4195 const vrange &lhs,
4196 const vrange &op2,
4197 relation_kind rel) const
4199 if (is_a <irange> (r))
4200 return m_op->op1_range (as_a <irange> (r), type,
4201 as_a <irange> (lhs),
4202 as_a <irange> (op2), rel);
4203 gcc_unreachable ();
4204 return false;
4207 bool
4208 range_op_handler::op2_range (vrange &r, tree type,
4209 const vrange &lhs,
4210 const vrange &op1,
4211 relation_kind rel) const
4213 if (is_a <irange> (r))
4214 return m_op->op2_range (as_a <irange> (r), type,
4215 as_a <irange> (lhs),
4216 as_a <irange> (op1), rel);
4217 gcc_unreachable ();
4218 return false;
4221 relation_kind
4222 range_op_handler::lhs_op1_relation (const vrange &lhs,
4223 const vrange &op1,
4224 const vrange &op2,
4225 relation_kind rel) const
4227 if (is_a <irange> (op1))
4228 return m_op->lhs_op1_relation (as_a <irange> (lhs),
4229 as_a <irange> (op1), as_a <irange> (op2), rel);
4230 gcc_unreachable ();
4231 return VREL_VARYING;
4234 relation_kind
4235 range_op_handler::lhs_op2_relation (const vrange &lhs,
4236 const vrange &op1,
4237 const vrange &op2,
4238 relation_kind rel) const
4240 if (is_a <irange> (op1))
4241 return m_op->lhs_op2_relation (as_a <irange> (lhs),
4242 as_a <irange> (op1), as_a <irange> (op2), rel);
4243 gcc_unreachable ();
4244 return VREL_VARYING;
4247 relation_kind
4248 range_op_handler::op1_op2_relation (const vrange &lhs) const
4250 return m_op->op1_op2_relation (as_a <irange> (lhs));
4253 // Cast the range in R to TYPE.
4255 bool
4256 range_cast (vrange &r, tree type)
4258 Value_Range tmp (r);
4259 Value_Range varying (type);
4260 varying.set_varying (type);
4261 range_op_handler op (CONVERT_EXPR, type);
4262 // Call op_convert, if it fails, the result is varying.
4263 if (!op || !op.fold_range (r, type, tmp, varying))
4265 r.set_varying (type);
4266 return false;
4268 return true;
4271 #if CHECKING_P
4272 #include "selftest.h"
4274 namespace selftest
4276 #define INT(N) build_int_cst (integer_type_node, (N))
4277 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4278 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4279 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4280 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4281 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4283 static void
4284 range_op_cast_tests ()
4286 int_range<1> r0, r1, r2, rold;
4287 r0.set_varying (integer_type_node);
4288 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4290 // If a range is in any way outside of the range for the converted
4291 // to range, default to the range for the new type.
4292 r0.set_varying (short_integer_type_node);
4293 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4294 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4295 if (TYPE_PRECISION (TREE_TYPE (maxint))
4296 > TYPE_PRECISION (short_integer_type_node))
4298 r1 = int_range<1> (integer_zero_node, maxint);
4299 range_cast (r1, short_integer_type_node);
4300 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4301 && r1.upper_bound() == wi::to_wide (maxshort));
4304 // (unsigned char)[-5,-1] => [251,255].
4305 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4306 range_cast (r0, unsigned_char_type_node);
4307 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4308 range_cast (r0, signed_char_type_node);
4309 ASSERT_TRUE (r0 == rold);
4311 // (signed char)[15, 150] => [-128,-106][15,127].
4312 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4313 range_cast (r0, signed_char_type_node);
4314 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4315 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4316 r1.union_ (r2);
4317 ASSERT_TRUE (r1 == r0);
4318 range_cast (r0, unsigned_char_type_node);
4319 ASSERT_TRUE (r0 == rold);
4321 // (unsigned char)[-5, 5] => [0,5][251,255].
4322 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4323 range_cast (r0, unsigned_char_type_node);
4324 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4325 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4326 r1.union_ (r2);
4327 ASSERT_TRUE (r0 == r1);
4328 range_cast (r0, signed_char_type_node);
4329 ASSERT_TRUE (r0 == rold);
4331 // (unsigned char)[-5,5] => [0,5][251,255].
4332 r0 = int_range<1> (INT (-5), INT (5));
4333 range_cast (r0, unsigned_char_type_node);
4334 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4335 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4336 ASSERT_TRUE (r0 == r1);
4338 // (unsigned char)[5U,1974U] => [0,255].
4339 r0 = int_range<1> (UINT (5), UINT (1974));
4340 range_cast (r0, unsigned_char_type_node);
4341 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4342 range_cast (r0, integer_type_node);
4343 // Going to a wider range should not sign extend.
4344 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4346 // (unsigned char)[-350,15] => [0,255].
4347 r0 = int_range<1> (INT (-350), INT (15));
4348 range_cast (r0, unsigned_char_type_node);
4349 ASSERT_TRUE (r0 == (int_range<1>
4350 (TYPE_MIN_VALUE (unsigned_char_type_node),
4351 TYPE_MAX_VALUE (unsigned_char_type_node))));
4353 // Casting [-120,20] from signed char to unsigned short.
4354 // => [0, 20][0xff88, 0xffff].
4355 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4356 range_cast (r0, short_unsigned_type_node);
4357 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4358 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4359 r1.union_ (r2);
4360 ASSERT_TRUE (r0 == r1);
4361 // A truncating cast back to signed char will work because [-120, 20]
4362 // is representable in signed char.
4363 range_cast (r0, signed_char_type_node);
4364 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4366 // unsigned char -> signed short
4367 // (signed short)[(unsigned char)25, (unsigned char)250]
4368 // => [(signed short)25, (signed short)250]
4369 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4370 range_cast (r0, short_integer_type_node);
4371 r1 = int_range<1> (INT16 (25), INT16 (250));
4372 ASSERT_TRUE (r0 == r1);
4373 range_cast (r0, unsigned_char_type_node);
4374 ASSERT_TRUE (r0 == rold);
4376 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4377 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4378 TYPE_MAX_VALUE (long_long_integer_type_node));
4379 range_cast (r0, short_unsigned_type_node);
4380 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4381 TYPE_MAX_VALUE (short_unsigned_type_node));
4382 ASSERT_TRUE (r0 == r1);
4384 // Casting NONZERO to a narrower type will wrap/overflow so
4385 // it's just the entire range for the narrower type.
4387 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4388 // is outside of the range of a smaller range, return the full
4389 // smaller range.
4390 if (TYPE_PRECISION (integer_type_node)
4391 > TYPE_PRECISION (short_integer_type_node))
4393 r0 = range_nonzero (integer_type_node);
4394 range_cast (r0, short_integer_type_node);
4395 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4396 TYPE_MAX_VALUE (short_integer_type_node));
4397 ASSERT_TRUE (r0 == r1);
4400 // Casting NONZERO from a narrower signed to a wider signed.
4402 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4403 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4404 r0 = range_nonzero (short_integer_type_node);
4405 range_cast (r0, integer_type_node);
4406 r1 = int_range<1> (INT (-32768), INT (-1));
4407 r2 = int_range<1> (INT (1), INT (32767));
4408 r1.union_ (r2);
4409 ASSERT_TRUE (r0 == r1);
4412 static void
4413 range_op_lshift_tests ()
4415 // Test that 0x808.... & 0x8.... still contains 0x8....
4416 // for a large set of numbers.
4418 int_range_max res;
4419 tree big_type = long_long_unsigned_type_node;
4420 // big_num = 0x808,0000,0000,0000
4421 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4422 build_int_cst (big_type, 0x808),
4423 build_int_cst (big_type, 48));
4424 op_bitwise_and.fold_range (res, big_type,
4425 int_range <1> (big_type),
4426 int_range <1> (big_num, big_num));
4427 // val = 0x8,0000,0000,0000
4428 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4429 build_int_cst (big_type, 0x8),
4430 build_int_cst (big_type, 48));
4431 ASSERT_TRUE (res.contains_p (val));
4434 if (TYPE_PRECISION (unsigned_type_node) > 31)
4436 // unsigned VARYING = op1 << 1 should be VARYING.
4437 int_range<2> lhs (unsigned_type_node);
4438 int_range<2> shift (INT (1), INT (1));
4439 int_range_max op1;
4440 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4441 ASSERT_TRUE (op1.varying_p ());
4443 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4444 int_range<2> zero (UINT (0), UINT (0));
4445 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4446 ASSERT_TRUE (op1.num_pairs () == 2);
4447 // Remove the [0,0] range.
4448 op1.intersect (zero);
4449 ASSERT_TRUE (op1.num_pairs () == 1);
4450 // op1 << 1 should be [0x8000,0x8000] << 1,
4451 // which should result in [0,0].
4452 int_range_max result;
4453 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4454 ASSERT_TRUE (result == zero);
4456 // signed VARYING = op1 << 1 should be VARYING.
4457 if (TYPE_PRECISION (integer_type_node) > 31)
4459 // unsigned VARYING = op1 << 1 hould be VARYING.
4460 int_range<2> lhs (integer_type_node);
4461 int_range<2> shift (INT (1), INT (1));
4462 int_range_max op1;
4463 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4464 ASSERT_TRUE (op1.varying_p ());
4466 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4467 int_range<2> zero (INT (0), INT (0));
4468 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4469 ASSERT_TRUE (op1.num_pairs () == 2);
4470 // Remove the [0,0] range.
4471 op1.intersect (zero);
4472 ASSERT_TRUE (op1.num_pairs () == 1);
4473 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4474 // which should result in [0,0].
4475 int_range_max result;
4476 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4477 ASSERT_TRUE (result == zero);
4481 static void
4482 range_op_rshift_tests ()
4484 // unsigned: [3, MAX] = OP1 >> 1
4486 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4487 TYPE_MAX_VALUE (unsigned_type_node));
4488 int_range_max one (build_one_cst (unsigned_type_node),
4489 build_one_cst (unsigned_type_node));
4490 int_range_max op1;
4491 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4492 ASSERT_FALSE (op1.contains_p (UINT (3)));
4495 // signed: [3, MAX] = OP1 >> 1
4497 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4498 int_range_max one (INT (1), INT (1));
4499 int_range_max op1;
4500 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4501 ASSERT_FALSE (op1.contains_p (INT (-2)));
4504 // This is impossible, so OP1 should be [].
4505 // signed: [MIN, MIN] = OP1 >> 1
4507 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4508 TYPE_MIN_VALUE (integer_type_node));
4509 int_range_max one (INT (1), INT (1));
4510 int_range_max op1;
4511 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4512 ASSERT_TRUE (op1.undefined_p ());
4515 // signed: ~[-1] = OP1 >> 31
4516 if (TYPE_PRECISION (integer_type_node) > 31)
4518 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4519 int_range_max shift (INT (31), INT (31));
4520 int_range_max op1;
4521 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4522 int_range_max negatives = range_negatives (integer_type_node);
4523 negatives.intersect (op1);
4524 ASSERT_TRUE (negatives.undefined_p ());
4528 static void
4529 range_op_bitwise_and_tests ()
4531 int_range_max res;
4532 tree min = vrp_val_min (integer_type_node);
4533 tree max = vrp_val_max (integer_type_node);
4534 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4535 build_one_cst (integer_type_node));
4536 int_range_max i1 (tiny, max);
4537 int_range_max i2 (build_int_cst (integer_type_node, 255),
4538 build_int_cst (integer_type_node, 255));
4540 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4541 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4542 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4544 // VARYING = OP1 & 255: OP1 is VARYING
4545 i1 = int_range<1> (integer_type_node);
4546 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4547 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4549 // (NONZERO | X) is nonzero.
4550 i1.set_nonzero (integer_type_node);
4551 i2.set_varying (integer_type_node);
4552 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4553 ASSERT_TRUE (res.nonzero_p ());
4555 // (NEGATIVE | X) is nonzero.
4556 i1 = int_range<1> (INT (-5), INT (-3));
4557 i2.set_varying (integer_type_node);
4558 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4559 ASSERT_FALSE (res.contains_p (INT (0)));
4562 static void
4563 range_relational_tests ()
4565 int_range<2> lhs (unsigned_char_type_node);
4566 int_range<2> op1 (UCHAR (8), UCHAR (10));
4567 int_range<2> op2 (UCHAR (20), UCHAR (20));
4569 // Never wrapping additions mean LHS > OP1.
4570 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4571 ASSERT_TRUE (code == VREL_GT);
4573 // Most wrapping additions mean nothing...
4574 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4575 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4576 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4577 ASSERT_TRUE (code == VREL_VARYING);
4579 // However, always wrapping additions mean LHS < OP1.
4580 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4581 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4582 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4583 ASSERT_TRUE (code == VREL_LT);
4586 void
4587 range_op_tests ()
4589 range_op_rshift_tests ();
4590 range_op_lshift_tests ();
4591 range_op_bitwise_and_tests ();
4592 range_op_cast_tests ();
4593 range_relational_tests ();
4596 } // namespace selftest
4598 #endif // CHECKING_P