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)
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/>. */
24 #include "coretypes.h"
26 #include "insn-codes.h"
31 #include "tree-pass.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
41 #include "gimple-iterator.h"
42 #include "gimple-fold.h"
44 #include "gimple-walk.h"
47 #include "value-relation.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.
71 get_shift_range (irange &r, tree type, const irange &op)
73 if (op.undefined_p ())
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));
81 // If there are no valid ranges in the shift range, returned false.
87 // Return TRUE if 0 is within [WMIN, WMAX].
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.
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].
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]
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
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);
141 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
145 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
149 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
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);
159 wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
163 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
167 wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
170 // Otherwise just call wi_fold.
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.
179 range_operator::fold_range (irange &r, tree type,
182 relation_trio trio) const
184 gcc_checking_assert (r.supports_type_p (type));
185 if (empty_range_varying (r, type, lh, rh))
188 relation_kind rel = trio.op1_op2 ();
189 unsigned num_lh = lh.num_pairs ();
190 unsigned num_rh = rh.num_pairs ();
192 // If both ranges are single pairs, fold directly into the result range.
193 // If the number of subranges grows too high, produce a summary result as the
194 // loop becomes exponential with little benefit. See PR 103821.
195 if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
197 wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
198 rh.lower_bound (), rh.upper_bound ());
199 op1_op2_relation_effect (r, type, lh, rh, rel);
205 for (unsigned x = 0; x < num_lh; ++x)
206 for (unsigned y = 0; y < num_rh; ++y)
208 wide_int lh_lb = lh.lower_bound (x);
209 wide_int lh_ub = lh.upper_bound (x);
210 wide_int rh_lb = rh.lower_bound (y);
211 wide_int rh_ub = rh.upper_bound (y);
212 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
216 op1_op2_relation_effect (r, type, lh, rh, rel);
220 op1_op2_relation_effect (r, type, lh, rh, rel);
224 // The default for op1_range is to return false.
227 range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
228 tree type ATTRIBUTE_UNUSED,
229 const irange &lhs ATTRIBUTE_UNUSED,
230 const irange &op2 ATTRIBUTE_UNUSED,
236 // The default for op2_range is to return false.
239 range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
240 tree type ATTRIBUTE_UNUSED,
241 const irange &lhs ATTRIBUTE_UNUSED,
242 const irange &op1 ATTRIBUTE_UNUSED,
248 // The default relation routines return VREL_VARYING.
251 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
252 const irange &op1 ATTRIBUTE_UNUSED,
253 const irange &op2 ATTRIBUTE_UNUSED,
254 relation_kind rel ATTRIBUTE_UNUSED) const
260 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
261 const irange &op1 ATTRIBUTE_UNUSED,
262 const irange &op2 ATTRIBUTE_UNUSED,
263 relation_kind rel ATTRIBUTE_UNUSED) const
269 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
274 // Default is no relation affects the LHS.
277 range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
278 tree type ATTRIBUTE_UNUSED,
279 const irange &op1_range ATTRIBUTE_UNUSED,
280 const irange &op2_range ATTRIBUTE_UNUSED,
281 relation_kind rel ATTRIBUTE_UNUSED) const
286 // Create and return a range from a pair of wide-ints that are known
287 // to have overflowed (or underflowed).
290 value_range_from_overflowed_bounds (irange &r, tree type,
291 const wide_int &wmin,
292 const wide_int &wmax)
294 const signop sgn = TYPE_SIGN (type);
295 const unsigned int prec = TYPE_PRECISION (type);
297 wide_int tmin = wide_int::from (wmin, prec, sgn);
298 wide_int tmax = wide_int::from (wmax, prec, sgn);
303 if (wi::cmp (tmin, tmax, sgn) < 0)
306 if (wi::cmp (tmax, tem, sgn) > 0)
309 // If the anti-range would cover nothing, drop to varying.
310 // Likewise if the anti-range bounds are outside of the types
312 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
313 r.set_varying (type);
316 tree tree_min = wide_int_to_tree (type, tmin);
317 tree tree_max = wide_int_to_tree (type, tmax);
318 r.set (tree_min, tree_max, VR_ANTI_RANGE);
322 // Create and return a range from a pair of wide-ints. MIN_OVF and
323 // MAX_OVF describe any overflow that might have occurred while
324 // calculating WMIN and WMAX respectively.
327 value_range_with_overflow (irange &r, tree type,
328 const wide_int &wmin, const wide_int &wmax,
329 wi::overflow_type min_ovf = wi::OVF_NONE,
330 wi::overflow_type max_ovf = wi::OVF_NONE)
332 const signop sgn = TYPE_SIGN (type);
333 const unsigned int prec = TYPE_PRECISION (type);
334 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
336 // For one bit precision if max != min, then the range covers all
338 if (prec == 1 && wi::ne_p (wmax, wmin))
340 r.set_varying (type);
346 // If overflow wraps, truncate the values and adjust the range,
347 // kind, and bounds appropriately.
348 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
350 wide_int tmin = wide_int::from (wmin, prec, sgn);
351 wide_int tmax = wide_int::from (wmax, prec, sgn);
352 // If the limits are swapped, we wrapped around and cover
354 if (wi::gt_p (tmin, tmax, sgn))
355 r.set_varying (type);
357 // No overflow or both overflow or underflow. The range
358 // kind stays normal.
359 r.set (wide_int_to_tree (type, tmin),
360 wide_int_to_tree (type, tmax));
364 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
365 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
366 value_range_from_overflowed_bounds (r, type, wmin, wmax);
368 // Other underflow and/or overflow, drop to VR_VARYING.
369 r.set_varying (type);
373 // If both bounds either underflowed or overflowed, then the result
375 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
376 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
382 // If overflow does not wrap, saturate to [MIN, MAX].
383 wide_int new_lb, new_ub;
384 if (min_ovf == wi::OVF_UNDERFLOW)
385 new_lb = wi::min_value (prec, sgn);
386 else if (min_ovf == wi::OVF_OVERFLOW)
387 new_lb = wi::max_value (prec, sgn);
391 if (max_ovf == wi::OVF_UNDERFLOW)
392 new_ub = wi::min_value (prec, sgn);
393 else if (max_ovf == wi::OVF_OVERFLOW)
394 new_ub = wi::max_value (prec, sgn);
398 r.set (wide_int_to_tree (type, new_lb),
399 wide_int_to_tree (type, new_ub));
403 // Create and return a range from a pair of wide-ints. Canonicalize
404 // the case where the bounds are swapped. In which case, we transform
405 // [10,5] into [MIN,5][10,MAX].
408 create_possibly_reversed_range (irange &r, tree type,
409 const wide_int &new_lb, const wide_int &new_ub)
411 signop s = TYPE_SIGN (type);
412 // If the bounds are swapped, treat the result as if an overflow occured.
413 if (wi::gt_p (new_lb, new_ub, s))
414 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
416 // Otherwise it's just a normal range.
417 r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
420 // Return the summary information about boolean range LHS. If EMPTY/FULL,
421 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
424 get_bool_state (vrange &r, const vrange &lhs, tree val_type)
426 // If there is no result, then this is unexecutable.
427 if (lhs.undefined_p ())
436 // For TRUE, we can't just test for [1,1] because Ada can have
437 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
438 if (lhs.contains_p (build_zero_cst (lhs.type ())))
440 r.set_varying (val_type);
448 class operator_equal : public range_operator
450 using range_operator::fold_range;
451 using range_operator::op1_range;
452 using range_operator::op2_range;
454 virtual bool fold_range (irange &r, tree type,
457 relation_trio = TRIO_VARYING) const;
458 virtual bool op1_range (irange &r, tree type,
461 relation_trio = TRIO_VARYING) const;
462 virtual bool op2_range (irange &r, tree type,
465 relation_trio = TRIO_VARYING) const;
466 virtual relation_kind op1_op2_relation (const irange &lhs) const;
469 // Check if the LHS range indicates a relation between OP1 and OP2.
472 equal_op1_op2_relation (const irange &lhs)
474 if (lhs.undefined_p ())
475 return VREL_UNDEFINED;
477 // FALSE = op1 == op2 indicates NE_EXPR.
481 // TRUE = op1 == op2 indicates EQ_EXPR.
482 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
488 operator_equal::op1_op2_relation (const irange &lhs) const
490 return equal_op1_op2_relation (lhs);
495 operator_equal::fold_range (irange &r, tree type,
498 relation_trio rel) const
500 if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
503 // We can be sure the values are always equal or not if both ranges
504 // consist of a single value, and then compare them.
505 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
506 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
508 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
509 r = range_true (type);
511 r = range_false (type);
515 // If ranges do not intersect, we know the range is not equal,
516 // otherwise we don't know anything for sure.
517 int_range_max tmp = op1;
519 if (tmp.undefined_p ())
520 r = range_false (type);
522 r = range_true_and_false (type);
528 operator_equal::op1_range (irange &r, tree type,
533 switch (get_bool_state (r, lhs, type))
536 // If it's true, the result is the same as OP2.
541 // If the result is false, the only time we know anything is
542 // if OP2 is a constant.
543 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
549 r.set_varying (type);
559 operator_equal::op2_range (irange &r, tree type,
562 relation_trio rel) const
564 return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
567 class operator_not_equal : public range_operator
569 using range_operator::fold_range;
570 using range_operator::op1_range;
571 using range_operator::op2_range;
573 virtual bool fold_range (irange &r, tree type,
576 relation_trio = TRIO_VARYING) const;
577 virtual bool op1_range (irange &r, tree type,
580 relation_trio = TRIO_VARYING) const;
581 virtual bool op2_range (irange &r, tree type,
584 relation_trio = TRIO_VARYING) const;
585 virtual relation_kind op1_op2_relation (const irange &lhs) const;
588 // Check if the LHS range indicates a relation between OP1 and OP2.
591 not_equal_op1_op2_relation (const irange &lhs)
593 if (lhs.undefined_p ())
594 return VREL_UNDEFINED;
596 // FALSE = op1 != op2 indicates EQ_EXPR.
600 // TRUE = op1 != op2 indicates NE_EXPR.
601 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
607 operator_not_equal::op1_op2_relation (const irange &lhs) const
609 return not_equal_op1_op2_relation (lhs);
613 operator_not_equal::fold_range (irange &r, tree type,
616 relation_trio rel) const
618 if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
621 // We can be sure the values are always equal or not if both ranges
622 // consist of a single value, and then compare them.
623 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
624 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
626 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
627 r = range_true (type);
629 r = range_false (type);
633 // If ranges do not intersect, we know the range is not equal,
634 // otherwise we don't know anything for sure.
635 int_range_max tmp = op1;
637 if (tmp.undefined_p ())
638 r = range_true (type);
640 r = range_true_and_false (type);
646 operator_not_equal::op1_range (irange &r, tree type,
651 switch (get_bool_state (r, lhs, type))
654 // If the result is true, the only time we know anything is if
655 // OP2 is a constant.
656 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
662 r.set_varying (type);
666 // If it's false, the result is the same as OP2.
678 operator_not_equal::op2_range (irange &r, tree type,
681 relation_trio rel) const
683 return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
686 // (X < VAL) produces the range of [MIN, VAL - 1].
689 build_lt (irange &r, tree type, const wide_int &val)
691 wi::overflow_type ov;
693 signop sgn = TYPE_SIGN (type);
695 // Signed 1 bit cannot represent 1 for subtraction.
697 lim = wi::add (val, -1, sgn, &ov);
699 lim = wi::sub (val, 1, sgn, &ov);
701 // If val - 1 underflows, check if X < MIN, which is an empty range.
705 r = int_range<1> (type, min_limit (type), lim);
708 // (X <= VAL) produces the range of [MIN, VAL].
711 build_le (irange &r, tree type, const wide_int &val)
713 r = int_range<1> (type, min_limit (type), val);
716 // (X > VAL) produces the range of [VAL + 1, MAX].
719 build_gt (irange &r, tree type, const wide_int &val)
721 wi::overflow_type ov;
723 signop sgn = TYPE_SIGN (type);
725 // Signed 1 bit cannot represent 1 for addition.
727 lim = wi::sub (val, -1, sgn, &ov);
729 lim = wi::add (val, 1, sgn, &ov);
730 // If val + 1 overflows, check is for X > MAX, which is an empty range.
734 r = int_range<1> (type, lim, max_limit (type));
737 // (X >= val) produces the range of [VAL, MAX].
740 build_ge (irange &r, tree type, const wide_int &val)
742 r = int_range<1> (type, val, max_limit (type));
746 class operator_lt : public range_operator
748 using range_operator::fold_range;
749 using range_operator::op1_range;
750 using range_operator::op2_range;
752 virtual bool fold_range (irange &r, tree type,
755 relation_trio = TRIO_VARYING) const;
756 virtual bool op1_range (irange &r, tree type,
759 relation_trio = TRIO_VARYING) const;
760 virtual bool op2_range (irange &r, tree type,
763 relation_trio = TRIO_VARYING) const;
764 virtual relation_kind op1_op2_relation (const irange &lhs) const;
767 // Check if the LHS range indicates a relation between OP1 and OP2.
770 lt_op1_op2_relation (const irange &lhs)
772 if (lhs.undefined_p ())
773 return VREL_UNDEFINED;
775 // FALSE = op1 < op2 indicates GE_EXPR.
779 // TRUE = op1 < op2 indicates LT_EXPR.
780 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
786 operator_lt::op1_op2_relation (const irange &lhs) const
788 return lt_op1_op2_relation (lhs);
792 operator_lt::fold_range (irange &r, tree type,
795 relation_trio rel) const
797 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
800 signop sign = TYPE_SIGN (op1.type ());
801 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
803 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
804 r = range_true (type);
805 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
806 r = range_false (type);
807 // Use nonzero bits to determine if < 0 is false.
808 else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
809 r = range_false (type);
811 r = range_true_and_false (type);
816 operator_lt::op1_range (irange &r, tree type,
821 switch (get_bool_state (r, lhs, type))
824 build_lt (r, type, op2.upper_bound ());
828 build_ge (r, type, op2.lower_bound ());
838 operator_lt::op2_range (irange &r, tree type,
843 switch (get_bool_state (r, lhs, type))
846 build_gt (r, type, op1.lower_bound ());
850 build_le (r, type, op1.upper_bound ());
860 class operator_le : public range_operator
862 using range_operator::fold_range;
863 using range_operator::op1_range;
864 using range_operator::op2_range;
866 virtual bool fold_range (irange &r, tree type,
869 relation_trio = TRIO_VARYING) const;
870 virtual bool op1_range (irange &r, tree type,
873 relation_trio = TRIO_VARYING) const;
874 virtual bool op2_range (irange &r, tree type,
877 relation_trio = TRIO_VARYING) const;
878 virtual relation_kind op1_op2_relation (const irange &lhs) const;
881 // Check if the LHS range indicates a relation between OP1 and OP2.
884 le_op1_op2_relation (const irange &lhs)
886 if (lhs.undefined_p ())
887 return VREL_UNDEFINED;
889 // FALSE = op1 <= op2 indicates GT_EXPR.
893 // TRUE = op1 <= op2 indicates LE_EXPR.
894 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
900 operator_le::op1_op2_relation (const irange &lhs) const
902 return le_op1_op2_relation (lhs);
906 operator_le::fold_range (irange &r, tree type,
909 relation_trio rel) const
911 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
914 signop sign = TYPE_SIGN (op1.type ());
915 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
917 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
918 r = range_true (type);
919 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
920 r = range_false (type);
922 r = range_true_and_false (type);
927 operator_le::op1_range (irange &r, tree type,
932 switch (get_bool_state (r, lhs, type))
935 build_le (r, type, op2.upper_bound ());
939 build_gt (r, type, op2.lower_bound ());
949 operator_le::op2_range (irange &r, tree type,
954 switch (get_bool_state (r, lhs, type))
957 build_ge (r, type, op1.lower_bound ());
961 build_lt (r, type, op1.upper_bound ());
971 class operator_gt : public range_operator
973 using range_operator::fold_range;
974 using range_operator::op1_range;
975 using range_operator::op2_range;
977 virtual bool fold_range (irange &r, tree type,
980 relation_trio = TRIO_VARYING) const;
981 virtual bool op1_range (irange &r, tree type,
984 relation_trio = TRIO_VARYING) const;
985 virtual bool op2_range (irange &r, tree type,
988 relation_trio = TRIO_VARYING) const;
989 virtual relation_kind op1_op2_relation (const irange &lhs) const;
992 // Check if the LHS range indicates a relation between OP1 and OP2.
995 gt_op1_op2_relation (const irange &lhs)
997 if (lhs.undefined_p ())
998 return VREL_UNDEFINED;
1000 // FALSE = op1 > op2 indicates LE_EXPR.
1004 // TRUE = op1 > op2 indicates GT_EXPR.
1005 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1007 return VREL_VARYING;
1011 operator_gt::op1_op2_relation (const irange &lhs) const
1013 return gt_op1_op2_relation (lhs);
1018 operator_gt::fold_range (irange &r, tree type,
1019 const irange &op1, const irange &op2,
1020 relation_trio rel) const
1022 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1025 signop sign = TYPE_SIGN (op1.type ());
1026 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1028 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1029 r = range_true (type);
1030 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1031 r = range_false (type);
1033 r = range_true_and_false (type);
1038 operator_gt::op1_range (irange &r, tree type,
1039 const irange &lhs, const irange &op2,
1040 relation_trio) const
1042 switch (get_bool_state (r, lhs, type))
1045 build_gt (r, type, op2.lower_bound ());
1049 build_le (r, type, op2.upper_bound ());
1059 operator_gt::op2_range (irange &r, tree type,
1062 relation_trio) const
1064 switch (get_bool_state (r, lhs, type))
1067 build_lt (r, type, op1.upper_bound ());
1071 build_ge (r, type, op1.lower_bound ());
1081 class operator_ge : public range_operator
1083 using range_operator::fold_range;
1084 using range_operator::op1_range;
1085 using range_operator::op2_range;
1087 virtual bool fold_range (irange &r, tree type,
1090 relation_trio = TRIO_VARYING) const;
1091 virtual bool op1_range (irange &r, tree type,
1094 relation_trio = TRIO_VARYING) const;
1095 virtual bool op2_range (irange &r, tree type,
1098 relation_trio = TRIO_VARYING) const;
1099 virtual relation_kind op1_op2_relation (const irange &lhs) const;
1102 // Check if the LHS range indicates a relation between OP1 and OP2.
1105 ge_op1_op2_relation (const irange &lhs)
1107 if (lhs.undefined_p ())
1108 return VREL_UNDEFINED;
1110 // FALSE = op1 >= op2 indicates LT_EXPR.
1114 // TRUE = op1 >= op2 indicates GE_EXPR.
1115 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1117 return VREL_VARYING;
1121 operator_ge::op1_op2_relation (const irange &lhs) const
1123 return ge_op1_op2_relation (lhs);
1127 operator_ge::fold_range (irange &r, tree type,
1130 relation_trio rel) const
1132 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1135 signop sign = TYPE_SIGN (op1.type ());
1136 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1138 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1139 r = range_true (type);
1140 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1141 r = range_false (type);
1143 r = range_true_and_false (type);
1148 operator_ge::op1_range (irange &r, tree type,
1151 relation_trio) const
1153 switch (get_bool_state (r, lhs, type))
1156 build_ge (r, type, op2.lower_bound ());
1160 build_lt (r, type, op2.upper_bound ());
1170 operator_ge::op2_range (irange &r, tree type,
1173 relation_trio) const
1175 switch (get_bool_state (r, lhs, type))
1178 build_le (r, type, op1.upper_bound ());
1182 build_gt (r, type, op1.lower_bound ());
1192 class operator_plus : public range_operator
1194 using range_operator::op1_range;
1195 using range_operator::op2_range;
1196 using range_operator::lhs_op1_relation;
1197 using range_operator::lhs_op2_relation;
1199 virtual bool op1_range (irange &r, tree type,
1202 relation_trio) const;
1203 virtual bool op2_range (irange &r, tree type,
1206 relation_trio) const;
1207 virtual void wi_fold (irange &r, tree type,
1208 const wide_int &lh_lb,
1209 const wide_int &lh_ub,
1210 const wide_int &rh_lb,
1211 const wide_int &rh_ub) const;
1212 virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
1214 relation_kind rel) const;
1215 virtual relation_kind lhs_op2_relation (const irange &lhs, const irange &op1,
1217 relation_kind rel) const;
1220 // Check to see if the range of OP2 indicates anything about the relation
1221 // between LHS and OP1.
1224 operator_plus::lhs_op1_relation (const irange &lhs,
1227 relation_kind) const
1229 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1230 return VREL_VARYING;
1232 tree type = lhs.type ();
1233 unsigned prec = TYPE_PRECISION (type);
1234 wi::overflow_type ovf1, ovf2;
1235 signop sign = TYPE_SIGN (type);
1237 // LHS = OP1 + 0 indicates LHS == OP1.
1241 if (TYPE_OVERFLOW_WRAPS (type))
1243 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1244 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1247 ovf1 = ovf2 = wi::OVF_NONE;
1249 // Never wrapping additions.
1252 // Positive op2 means lhs > op1.
1253 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1255 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1258 // Negative op2 means lhs < op1.
1259 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1261 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1264 // Always wrapping additions.
1265 else if (ovf1 && ovf1 == ovf2)
1267 // Positive op2 means lhs < op1.
1268 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1270 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1273 // Negative op2 means lhs > op1.
1274 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1276 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1280 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1281 if (!range_includes_zero_p (&op2))
1284 return VREL_VARYING;
1287 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1291 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1292 const irange &op2, relation_kind rel) const
1294 return lhs_op1_relation (lhs, op2, op1, rel);
1298 operator_plus::wi_fold (irange &r, tree type,
1299 const wide_int &lh_lb, const wide_int &lh_ub,
1300 const wide_int &rh_lb, const wide_int &rh_ub) const
1302 wi::overflow_type ov_lb, ov_ub;
1303 signop s = TYPE_SIGN (type);
1304 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1305 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1306 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1309 // Given addition or subtraction, determine the possible NORMAL ranges and
1310 // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1311 // Return the relation that exists between the LHS and OP1 in order for the
1312 // NORMAL range to apply.
1313 // a return value of VREL_VARYING means no ranges were applicable.
1315 static relation_kind
1316 plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
1319 relation_kind kind = VREL_VARYING;
1320 // For now, only deal with constant adds. This could be extended to ranges
1321 // when someone is so motivated.
1322 if (!offset.singleton_p () || offset.zero_p ())
1325 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1326 wide_int off = offset.lower_bound ();
1327 if (wi::neg_p (off, SIGNED))
1330 off = wi::neg (off);
1333 wi::overflow_type ov;
1334 tree type = offset.type ();
1335 unsigned prec = TYPE_PRECISION (type);
1338 // calculate the normal range and relation for the operation.
1342 lb = wi::zero (prec);
1343 ub = wi::sub (wi::to_wide (vrp_val_max (type)), off, UNSIGNED, &ov);
1350 ub = wi::to_wide (vrp_val_max (type));
1353 int_range<2> normal_range (type, lb, ub);
1354 int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
1357 r_normal = normal_range;
1361 // Once op1 has been calculated by operator_plus or operator_minus, check
1362 // to see if the relation passed causes any part of the calculation to
1363 // be not possible. ie
1364 // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1365 // and that further refines a_2 to [0, 0].
1366 // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1367 // relation between LHS relatoin OP1 and ADD_P is true for PLUS, false for
1368 // MINUS. IF any adjustment can be made, R will reflect it.
1371 adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
1374 if (r.undefined_p ())
1376 tree type = r.type ();
1377 // Check for unsigned overflow and calculate the overflow part.
1378 signop s = TYPE_SIGN (type);
1379 if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
1382 // Only work with <, <=, >, >= relations.
1383 if (!relation_lt_le_gt_ge_p (rel))
1386 // Get the ranges for this offset.
1387 int_range_max normal, overflow;
1388 relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
1390 // VREL_VARYING means there are no adjustments.
1391 if (k == VREL_VARYING)
1394 // If the relations match use the normal range, otherwise use overflow range.
1395 if (relation_intersect (k, rel) == k)
1396 r.intersect (normal);
1398 r.intersect (overflow);
1403 operator_plus::op1_range (irange &r, tree type,
1406 relation_trio trio) const
1408 if (lhs.undefined_p ())
1410 // Start with the default operation.
1411 range_op_handler minus (MINUS_EXPR, type);
1414 bool res = minus.fold_range (r, type, lhs, op2);
1415 relation_kind rel = trio.lhs_op2 ();
1416 // Check for a relation refinement.
1418 adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
1423 operator_plus::op2_range (irange &r, tree type,
1426 relation_trio rel) const
1428 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1432 class operator_minus : public range_operator
1434 using range_operator::fold_range;
1435 using range_operator::op1_range;
1436 using range_operator::op2_range;
1438 virtual bool op1_range (irange &r, tree type,
1441 relation_trio) const;
1442 virtual bool op2_range (irange &r, tree type,
1445 relation_trio) const;
1446 virtual void wi_fold (irange &r, tree type,
1447 const wide_int &lh_lb,
1448 const wide_int &lh_ub,
1449 const wide_int &rh_lb,
1450 const wide_int &rh_ub) const;
1451 virtual relation_kind lhs_op1_relation (const irange &lhs,
1454 relation_kind rel) const;
1455 virtual bool op1_op2_relation_effect (irange &lhs_range,
1457 const irange &op1_range,
1458 const irange &op2_range,
1459 relation_kind rel) const;
1463 operator_minus::wi_fold (irange &r, tree type,
1464 const wide_int &lh_lb, const wide_int &lh_ub,
1465 const wide_int &rh_lb, const wide_int &rh_ub) const
1467 wi::overflow_type ov_lb, ov_ub;
1468 signop s = TYPE_SIGN (type);
1469 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1470 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1471 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1475 // Return the relation between LHS and OP1 based on the relation between
1479 operator_minus::lhs_op1_relation (const irange &, const irange &op1,
1480 const irange &, relation_kind rel) const
1482 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
1491 return VREL_VARYING;
1494 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1495 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1496 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1499 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1500 const irange &op1_range ATTRIBUTE_UNUSED,
1501 const irange &op2_range ATTRIBUTE_UNUSED,
1504 if (rel == VREL_VARYING)
1507 int_range<2> rel_range;
1508 unsigned prec = TYPE_PRECISION (type);
1509 signop sgn = TYPE_SIGN (type);
1511 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1513 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1514 else if (rel == VREL_NE)
1515 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1517 else if (TYPE_OVERFLOW_WRAPS (type))
1521 // For wrapping signed values and unsigned, if op1 > op2 or
1522 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1525 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1536 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1538 rel_range = int_range<2> (type, wi::one (prec),
1539 wi::max_value (prec, sgn));
1541 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1543 rel_range = int_range<2> (type, wi::zero (prec),
1544 wi::max_value (prec, sgn));
1546 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1548 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1549 wi::minus_one (prec));
1551 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1553 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1560 lhs_range.intersect (rel_range);
1565 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1566 const irange &op1_range,
1567 const irange &op2_range,
1568 relation_kind rel) const
1570 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1575 operator_minus::op1_range (irange &r, tree type,
1578 relation_trio trio) const
1580 if (lhs.undefined_p ())
1582 // Start with the default operation.
1583 range_op_handler minus (PLUS_EXPR, type);
1586 bool res = minus.fold_range (r, type, lhs, op2);
1587 relation_kind rel = trio.lhs_op2 ();
1589 adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
1595 operator_minus::op2_range (irange &r, tree type,
1598 relation_trio) const
1600 if (lhs.undefined_p ())
1602 return fold_range (r, type, op1, lhs);
1606 class operator_pointer_diff : public range_operator
1608 virtual bool op1_op2_relation_effect (irange &lhs_range,
1610 const irange &op1_range,
1611 const irange &op2_range,
1612 relation_kind rel) const;
1616 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1617 const irange &op1_range,
1618 const irange &op2_range,
1619 relation_kind rel) const
1621 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1626 class operator_min : public range_operator
1629 virtual void wi_fold (irange &r, tree type,
1630 const wide_int &lh_lb,
1631 const wide_int &lh_ub,
1632 const wide_int &rh_lb,
1633 const wide_int &rh_ub) const;
1637 operator_min::wi_fold (irange &r, tree type,
1638 const wide_int &lh_lb, const wide_int &lh_ub,
1639 const wide_int &rh_lb, const wide_int &rh_ub) const
1641 signop s = TYPE_SIGN (type);
1642 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1643 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1644 value_range_with_overflow (r, type, new_lb, new_ub);
1648 class operator_max : public range_operator
1651 virtual void wi_fold (irange &r, tree type,
1652 const wide_int &lh_lb,
1653 const wide_int &lh_ub,
1654 const wide_int &rh_lb,
1655 const wide_int &rh_ub) const;
1659 operator_max::wi_fold (irange &r, tree type,
1660 const wide_int &lh_lb, const wide_int &lh_ub,
1661 const wide_int &rh_lb, const wide_int &rh_ub) const
1663 signop s = TYPE_SIGN (type);
1664 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1665 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1666 value_range_with_overflow (r, type, new_lb, new_ub);
1670 class cross_product_operator : public range_operator
1673 // Perform an operation between two wide-ints and place the result
1674 // in R. Return true if the operation overflowed.
1675 virtual bool wi_op_overflows (wide_int &r,
1678 const wide_int &) const = 0;
1680 // Calculate the cross product of two sets of sub-ranges and return it.
1681 void wi_cross_product (irange &r, tree type,
1682 const wide_int &lh_lb,
1683 const wide_int &lh_ub,
1684 const wide_int &rh_lb,
1685 const wide_int &rh_ub) const;
1688 // Calculate the cross product of two sets of ranges and return it.
1690 // Multiplications, divisions and shifts are a bit tricky to handle,
1691 // depending on the mix of signs we have in the two ranges, we need to
1692 // operate on different values to get the minimum and maximum values
1693 // for the new range. One approach is to figure out all the
1694 // variations of range combinations and do the operations.
1696 // However, this involves several calls to compare_values and it is
1697 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1698 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1699 // figure the smallest and largest values to form the new range.
1702 cross_product_operator::wi_cross_product (irange &r, tree type,
1703 const wide_int &lh_lb,
1704 const wide_int &lh_ub,
1705 const wide_int &rh_lb,
1706 const wide_int &rh_ub) const
1708 wide_int cp1, cp2, cp3, cp4;
1709 // Default to varying.
1710 r.set_varying (type);
1712 // Compute the 4 cross operations, bailing if we get an overflow we
1714 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1716 if (wi::eq_p (lh_lb, lh_ub))
1718 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1720 if (wi::eq_p (rh_lb, rh_ub))
1722 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1724 if (wi::eq_p (lh_lb, lh_ub))
1726 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1730 signop sign = TYPE_SIGN (type);
1731 if (wi::gt_p (cp1, cp2, sign))
1732 std::swap (cp1, cp2);
1733 if (wi::gt_p (cp3, cp4, sign))
1734 std::swap (cp3, cp4);
1736 // Choose min and max from the ordered pairs.
1737 wide_int res_lb = wi::min (cp1, cp3, sign);
1738 wide_int res_ub = wi::max (cp2, cp4, sign);
1739 value_range_with_overflow (r, type, res_lb, res_ub);
1743 class operator_mult : public cross_product_operator
1745 using range_operator::fold_range;
1746 using range_operator::op1_range;
1747 using range_operator::op2_range;
1749 virtual bool fold_range (irange &r, tree type,
1750 const irange &lh, const irange &rh,
1751 relation_trio = TRIO_VARYING) const final override;
1752 virtual void wi_fold (irange &r, tree type,
1753 const wide_int &lh_lb,
1754 const wide_int &lh_ub,
1755 const wide_int &rh_lb,
1756 const wide_int &rh_ub) const final override;
1757 virtual bool wi_op_overflows (wide_int &res, tree type,
1758 const wide_int &w0, const wide_int &w1)
1759 const final override;
1760 virtual bool op1_range (irange &r, tree type,
1763 relation_trio) const final override;
1764 virtual bool op2_range (irange &r, tree type,
1767 relation_trio) const final override;
1771 operator_mult::fold_range (irange &r, tree type,
1772 const irange &lh, const irange &rh,
1773 relation_trio trio) const
1775 if (!cross_product_operator::fold_range (r, type, lh, rh, trio))
1778 if (lh.undefined_p ())
1782 if (rh.singleton_p (&t))
1784 wide_int w = wi::to_wide (t);
1785 int shift = wi::exact_log2 (w);
1788 wide_int nz = lh.get_nonzero_bits ();
1789 nz = wi::lshift (nz, shift);
1790 r.set_nonzero_bits (nz);
1797 operator_mult::op1_range (irange &r, tree type,
1798 const irange &lhs, const irange &op2,
1799 relation_trio) const
1802 if (lhs.undefined_p ())
1805 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1806 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1807 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1808 if (TYPE_OVERFLOW_WRAPS (type))
1811 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1812 return range_op_handler (TRUNC_DIV_EXPR, type).fold_range (r, type,
1818 operator_mult::op2_range (irange &r, tree type,
1819 const irange &lhs, const irange &op1,
1820 relation_trio rel) const
1822 return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1826 operator_mult::wi_op_overflows (wide_int &res, tree type,
1827 const wide_int &w0, const wide_int &w1) const
1829 wi::overflow_type overflow = wi::OVF_NONE;
1830 signop sign = TYPE_SIGN (type);
1831 res = wi::mul (w0, w1, sign, &overflow);
1832 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1834 // For multiplication, the sign of the overflow is given
1835 // by the comparison of the signs of the operands.
1836 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1837 res = wi::max_value (w0.get_precision (), sign);
1839 res = wi::min_value (w0.get_precision (), sign);
1846 operator_mult::wi_fold (irange &r, tree type,
1847 const wide_int &lh_lb, const wide_int &lh_ub,
1848 const wide_int &rh_lb, const wide_int &rh_ub) const
1850 if (TYPE_OVERFLOW_UNDEFINED (type))
1852 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1856 // Multiply the ranges when overflow wraps. This is basically fancy
1857 // code so we don't drop to varying with an unsigned
1860 // This test requires 2*prec bits if both operands are signed and
1861 // 2*prec + 2 bits if either is not. Therefore, extend the values
1862 // using the sign of the result to PREC2. From here on out,
1863 // everthing is just signed math no matter what the input types
1866 signop sign = TYPE_SIGN (type);
1867 unsigned prec = TYPE_PRECISION (type);
1868 widest2_int min0 = widest2_int::from (lh_lb, sign);
1869 widest2_int max0 = widest2_int::from (lh_ub, sign);
1870 widest2_int min1 = widest2_int::from (rh_lb, sign);
1871 widest2_int max1 = widest2_int::from (rh_ub, sign);
1872 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1873 widest2_int size = sizem1 + 1;
1875 // Canonicalize the intervals.
1876 if (sign == UNSIGNED)
1878 if (wi::ltu_p (size, min0 + max0))
1883 if (wi::ltu_p (size, min1 + max1))
1890 // Sort the 4 products so that min is in prod0 and max is in
1892 widest2_int prod0 = min0 * min1;
1893 widest2_int prod1 = min0 * max1;
1894 widest2_int prod2 = max0 * min1;
1895 widest2_int prod3 = max0 * max1;
1897 // min0min1 > max0max1
1899 std::swap (prod0, prod3);
1901 // min0max1 > max0min1
1903 std::swap (prod1, prod2);
1906 std::swap (prod0, prod1);
1909 std::swap (prod2, prod3);
1912 prod2 = prod3 - prod0;
1913 if (wi::geu_p (prod2, sizem1))
1914 // The range covers all values.
1915 r.set_varying (type);
1918 wide_int new_lb = wide_int::from (prod0, prec, sign);
1919 wide_int new_ub = wide_int::from (prod3, prec, sign);
1920 create_possibly_reversed_range (r, type, new_lb, new_ub);
1925 class operator_div : public cross_product_operator
1928 operator_div (enum tree_code c) { code = c; }
1929 virtual void wi_fold (irange &r, tree type,
1930 const wide_int &lh_lb,
1931 const wide_int &lh_ub,
1932 const wide_int &rh_lb,
1933 const wide_int &rh_ub) const final override;
1934 virtual bool wi_op_overflows (wide_int &res, tree type,
1935 const wide_int &, const wide_int &)
1936 const final override;
1937 virtual bool fold_range (irange &r, tree type,
1938 const irange &lh, const irange &rh,
1939 relation_trio trio) const final override;
1941 enum tree_code code;
1945 operator_div::fold_range (irange &r, tree type,
1946 const irange &lh, const irange &rh,
1947 relation_trio trio) const
1949 if (!cross_product_operator::fold_range (r, type, lh, rh, trio))
1952 if (lh.undefined_p ())
1956 if (code == TRUNC_DIV_EXPR
1957 && rh.singleton_p (&t)
1958 && !wi::neg_p (lh.lower_bound ()))
1960 wide_int wi = wi::to_wide (t);
1961 int shift = wi::exact_log2 (wi);
1964 wide_int nz = lh.get_nonzero_bits ();
1965 nz = wi::rshift (nz, shift, TYPE_SIGN (type));
1966 r.set_nonzero_bits (nz);
1973 operator_div::wi_op_overflows (wide_int &res, tree type,
1974 const wide_int &w0, const wide_int &w1) const
1979 wi::overflow_type overflow = wi::OVF_NONE;
1980 signop sign = TYPE_SIGN (type);
1984 case EXACT_DIV_EXPR:
1985 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1986 // operator_exact_divide. No need to handle it here.
1989 case TRUNC_DIV_EXPR:
1990 res = wi::div_trunc (w0, w1, sign, &overflow);
1992 case FLOOR_DIV_EXPR:
1993 res = wi::div_floor (w0, w1, sign, &overflow);
1995 case ROUND_DIV_EXPR:
1996 res = wi::div_round (w0, w1, sign, &overflow);
1999 res = wi::div_ceil (w0, w1, sign, &overflow);
2005 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2007 // For division, the only case is -INF / -1 = +INF.
2008 res = wi::max_value (w0.get_precision (), sign);
2015 operator_div::wi_fold (irange &r, tree type,
2016 const wide_int &lh_lb, const wide_int &lh_ub,
2017 const wide_int &rh_lb, const wide_int &rh_ub) const
2019 const wide_int dividend_min = lh_lb;
2020 const wide_int dividend_max = lh_ub;
2021 const wide_int divisor_min = rh_lb;
2022 const wide_int divisor_max = rh_ub;
2023 signop sign = TYPE_SIGN (type);
2024 unsigned prec = TYPE_PRECISION (type);
2025 wide_int extra_min, extra_max;
2027 // If we know we won't divide by zero, just do the division.
2028 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
2030 wi_cross_product (r, type, dividend_min, dividend_max,
2031 divisor_min, divisor_max);
2035 // If we're definitely dividing by zero, there's nothing to do.
2036 if (wi_zero_p (type, divisor_min, divisor_max))
2042 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2043 // skip any division by zero.
2045 // First divide by the negative numbers, if any.
2046 if (wi::neg_p (divisor_min, sign))
2047 wi_cross_product (r, type, dividend_min, dividend_max,
2048 divisor_min, wi::minus_one (prec));
2052 // Then divide by the non-zero positive numbers, if any.
2053 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
2056 wi_cross_product (tmp, type, dividend_min, dividend_max,
2057 wi::one (prec), divisor_max);
2060 // We shouldn't still have undefined here.
2061 gcc_checking_assert (!r.undefined_p ());
2064 operator_div op_trunc_div (TRUNC_DIV_EXPR);
2065 operator_div op_floor_div (FLOOR_DIV_EXPR);
2066 operator_div op_round_div (ROUND_DIV_EXPR);
2067 operator_div op_ceil_div (CEIL_DIV_EXPR);
2070 class operator_exact_divide : public operator_div
2072 using range_operator::op1_range;
2074 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
2075 virtual bool op1_range (irange &r, tree type,
2078 relation_trio) const;
2083 operator_exact_divide::op1_range (irange &r, tree type,
2086 relation_trio) const
2088 if (lhs.undefined_p ())
2091 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2092 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2093 // We wont bother trying to enumerate all the in between stuff :-P
2094 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
2095 // the time however.
2096 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2097 if (op2.singleton_p (&offset)
2098 && !integer_zerop (offset))
2099 return range_op_handler (MULT_EXPR, type).fold_range (r, type, lhs, op2);
2104 class operator_lshift : public cross_product_operator
2106 using range_operator::fold_range;
2107 using range_operator::op1_range;
2109 virtual bool op1_range (irange &r, tree type,
2112 relation_trio rel = TRIO_VARYING) const;
2113 virtual bool fold_range (irange &r, tree type,
2116 relation_trio rel = TRIO_VARYING) const;
2118 virtual void wi_fold (irange &r, tree type,
2119 const wide_int &lh_lb, const wide_int &lh_ub,
2120 const wide_int &rh_lb, const wide_int &rh_ub) const;
2121 virtual bool wi_op_overflows (wide_int &res,
2124 const wide_int &) const;
2127 class operator_rshift : public cross_product_operator
2129 using range_operator::fold_range;
2130 using range_operator::op1_range;
2131 using range_operator::lhs_op1_relation;
2133 virtual bool fold_range (irange &r, tree type,
2136 relation_trio rel = TRIO_VARYING) const;
2137 virtual void wi_fold (irange &r, tree type,
2138 const wide_int &lh_lb,
2139 const wide_int &lh_ub,
2140 const wide_int &rh_lb,
2141 const wide_int &rh_ub) const;
2142 virtual bool wi_op_overflows (wide_int &res,
2145 const wide_int &w1) const;
2146 virtual bool op1_range (irange &, tree type,
2149 relation_trio rel = TRIO_VARYING) const;
2150 virtual relation_kind lhs_op1_relation (const irange &lhs,
2153 relation_kind rel) const;
2158 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
2161 relation_kind) const
2163 // If both operands range are >= 0, then the LHS <= op1.
2164 if (!op1.undefined_p () && !op2.undefined_p ()
2165 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
2166 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
2168 return VREL_VARYING;
2172 operator_lshift::fold_range (irange &r, tree type,
2175 relation_trio rel) const
2177 int_range_max shift_range;
2178 if (!get_shift_range (shift_range, type, op2))
2180 if (op2.undefined_p ())
2183 r.set_varying (type);
2187 // Transform left shifts by constants into multiplies.
2188 if (shift_range.singleton_p ())
2190 unsigned shift = shift_range.lower_bound ().to_uhwi ();
2191 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2192 int_range<1> mult (type, tmp, tmp);
2194 // Force wrapping multiplication.
2195 bool saved_flag_wrapv = flag_wrapv;
2196 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2198 flag_wrapv_pointer = 1;
2199 bool b = op_mult.fold_range (r, type, op1, mult);
2200 flag_wrapv = saved_flag_wrapv;
2201 flag_wrapv_pointer = saved_flag_wrapv_pointer;
2205 // Otherwise, invoke the generic fold routine.
2206 return range_operator::fold_range (r, type, op1, shift_range, rel);
2210 operator_lshift::wi_fold (irange &r, tree type,
2211 const wide_int &lh_lb, const wide_int &lh_ub,
2212 const wide_int &rh_lb, const wide_int &rh_ub) const
2214 signop sign = TYPE_SIGN (type);
2215 unsigned prec = TYPE_PRECISION (type);
2216 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2217 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2218 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2219 // overflow. However, for that to happen, rh.max needs to be zero,
2220 // which means rh is a singleton range of zero, which means we simply return
2221 // [lh_lb, lh_ub] as the range.
2222 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2224 r = int_range<2> (type, lh_lb, lh_ub);
2228 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2229 wide_int complement = ~(bound - 1);
2230 wide_int low_bound, high_bound;
2231 bool in_bounds = false;
2233 if (sign == UNSIGNED)
2236 high_bound = complement;
2237 if (wi::ltu_p (lh_ub, low_bound))
2239 // [5, 6] << [1, 2] == [10, 24].
2240 // We're shifting out only zeroes, the value increases
2244 else if (wi::ltu_p (high_bound, lh_lb))
2246 // [0xffffff00, 0xffffffff] << [1, 2]
2247 // == [0xfffffc00, 0xfffffffe].
2248 // We're shifting out only ones, the value decreases
2255 // [-1, 1] << [1, 2] == [-4, 4]
2256 low_bound = complement;
2258 if (wi::lts_p (lh_ub, high_bound)
2259 && wi::lts_p (low_bound, lh_lb))
2261 // For non-negative numbers, we're shifting out only zeroes,
2262 // the value increases monotonically. For negative numbers,
2263 // we're shifting out only ones, the value decreases
2270 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2272 r.set_varying (type);
2276 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2277 const wide_int &w0, const wide_int &w1) const
2279 signop sign = TYPE_SIGN (type);
2282 // It's unclear from the C standard whether shifts can overflow.
2283 // The following code ignores overflow; perhaps a C standard
2284 // interpretation ruling is needed.
2285 res = wi::rshift (w0, -w1, sign);
2288 res = wi::lshift (w0, w1);
2293 operator_lshift::op1_range (irange &r,
2297 relation_trio) const
2299 if (lhs.undefined_p ())
2303 if (!lhs.contains_p (build_zero_cst (type)))
2304 r.set_nonzero (type);
2306 r.set_varying (type);
2308 if (op2.singleton_p (&shift_amount))
2310 wide_int shift = wi::to_wide (shift_amount);
2311 if (wi::lt_p (shift, 0, SIGNED))
2313 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2314 TYPE_PRECISION (op2.type ())),
2323 // Work completely in unsigned mode to start.
2325 int_range_max tmp_range;
2326 if (TYPE_SIGN (type) == SIGNED)
2328 int_range_max tmp = lhs;
2329 utype = unsigned_type_for (type);
2330 range_cast (tmp, utype);
2331 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2334 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2336 // Start with ranges which can produce the LHS by right shifting the
2337 // result by the shift amount.
2338 // ie [0x08, 0xF0] = op1 << 2 will start with
2339 // [00001000, 11110000] = op1 << 2
2340 // [0x02, 0x4C] aka [00000010, 00111100]
2342 // Then create a range from the LB with the least significant upper bit
2343 // set, to the upper bound with all the bits set.
2344 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2346 // Ideally we do this for each subrange, but just lump them all for now.
2347 unsigned low_bits = TYPE_PRECISION (utype)
2348 - TREE_INT_CST_LOW (shift_amount);
2349 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2350 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2351 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2352 int_range<2> fill_range (utype, new_lb, new_ub);
2353 tmp_range.union_ (fill_range);
2356 range_cast (tmp_range, type);
2358 r.intersect (tmp_range);
2362 return !r.varying_p ();
2366 operator_rshift::op1_range (irange &r,
2370 relation_trio) const
2373 if (lhs.undefined_p ())
2375 if (op2.singleton_p (&shift))
2377 // Ignore nonsensical shifts.
2378 unsigned prec = TYPE_PRECISION (type);
2379 if (wi::ge_p (wi::to_wide (shift),
2380 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2383 if (wi::to_wide (shift) == 0)
2389 // Folding the original operation may discard some impossible
2390 // ranges from the LHS.
2391 int_range_max lhs_refined;
2392 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2393 lhs_refined.intersect (lhs);
2394 if (lhs_refined.undefined_p ())
2399 int_range_max shift_range (shift, shift);
2400 int_range_max lb, ub;
2401 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2403 // 0000 0111 = OP1 >> 3
2405 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2406 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2407 // right hand side (0x07).
2408 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2409 fold_build2 (LSHIFT_EXPR, type,
2410 build_minus_one_cst (type),
2412 int_range_max mask_range (build_zero_cst (type), mask);
2413 op_plus.fold_range (ub, type, lb, mask_range);
2416 if (!lhs_refined.contains_p (build_zero_cst (type)))
2418 mask_range.invert ();
2419 r.intersect (mask_range);
2427 operator_rshift::wi_op_overflows (wide_int &res,
2430 const wide_int &w1) const
2432 signop sign = TYPE_SIGN (type);
2434 res = wi::lshift (w0, -w1);
2437 // It's unclear from the C standard whether shifts can overflow.
2438 // The following code ignores overflow; perhaps a C standard
2439 // interpretation ruling is needed.
2440 res = wi::rshift (w0, w1, sign);
2446 operator_rshift::fold_range (irange &r, tree type,
2449 relation_trio rel) const
2451 int_range_max shift;
2452 if (!get_shift_range (shift, type, op2))
2454 if (op2.undefined_p ())
2457 r.set_varying (type);
2461 return range_operator::fold_range (r, type, op1, shift, rel);
2465 operator_rshift::wi_fold (irange &r, tree type,
2466 const wide_int &lh_lb, const wide_int &lh_ub,
2467 const wide_int &rh_lb, const wide_int &rh_ub) const
2469 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2473 class operator_cast: public range_operator
2475 using range_operator::fold_range;
2476 using range_operator::op1_range;
2478 virtual bool fold_range (irange &r, tree type,
2481 relation_trio rel = TRIO_VARYING) const;
2482 virtual bool op1_range (irange &r, tree type,
2485 relation_trio rel = TRIO_VARYING) const;
2486 virtual relation_kind lhs_op1_relation (const irange &lhs,
2489 relation_kind) const;
2491 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2492 bool inside_domain_p (const wide_int &min, const wide_int &max,
2493 const irange &outer) const;
2494 void fold_pair (irange &r, unsigned index, const irange &inner,
2495 const irange &outer) const;
2498 // Add a partial equivalence between the LHS and op1 for casts.
2501 operator_cast::lhs_op1_relation (const irange &lhs,
2503 const irange &op2 ATTRIBUTE_UNUSED,
2504 relation_kind) const
2506 if (lhs.undefined_p () || op1.undefined_p ())
2507 return VREL_VARYING;
2508 unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
2509 unsigned op1_prec = TYPE_PRECISION (op1.type ());
2510 // If the result gets sign extended into a larger type check first if this
2511 // qualifies as a partial equivalence.
2512 if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
2514 // If the result is sign extended, and the LHS is larger than op1,
2515 // check if op1's range can be negative as the sign extention will
2516 // cause the upper bits to be 1 instead of 0, invalidating the PE.
2517 int_range<3> negs = range_negatives (op1.type ());
2518 negs.intersect (op1);
2519 if (!negs.undefined_p ())
2520 return VREL_VARYING;
2523 unsigned prec = MIN (lhs_prec, op1_prec);
2524 return bits_to_pe (prec);
2527 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2530 operator_cast::truncating_cast_p (const irange &inner,
2531 const irange &outer) const
2533 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2536 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2539 operator_cast::inside_domain_p (const wide_int &min,
2540 const wide_int &max,
2541 const irange &range) const
2543 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2544 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2545 signop domain_sign = TYPE_SIGN (range.type ());
2546 return (wi::le_p (min, domain_max, domain_sign)
2547 && wi::le_p (max, domain_max, domain_sign)
2548 && wi::ge_p (min, domain_min, domain_sign)
2549 && wi::ge_p (max, domain_min, domain_sign));
2553 // Helper for fold_range which work on a pair at a time.
2556 operator_cast::fold_pair (irange &r, unsigned index,
2557 const irange &inner,
2558 const irange &outer) const
2560 tree inner_type = inner.type ();
2561 tree outer_type = outer.type ();
2562 signop inner_sign = TYPE_SIGN (inner_type);
2563 unsigned outer_prec = TYPE_PRECISION (outer_type);
2565 // check to see if casting from INNER to OUTER is a conversion that
2566 // fits in the resulting OUTER type.
2567 wide_int inner_lb = inner.lower_bound (index);
2568 wide_int inner_ub = inner.upper_bound (index);
2569 if (truncating_cast_p (inner, outer))
2571 // We may be able to accomodate a truncating cast if the
2572 // resulting range can be represented in the target type...
2573 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2574 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2577 r.set_varying (outer_type);
2581 // ...but we must still verify that the final range fits in the
2582 // domain. This catches -fstrict-enum restrictions where the domain
2583 // range is smaller than what fits in the underlying type.
2584 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2585 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2586 if (inside_domain_p (min, max, outer))
2587 create_possibly_reversed_range (r, outer_type, min, max);
2589 r.set_varying (outer_type);
2594 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2595 const irange &inner,
2596 const irange &outer,
2597 relation_trio) const
2599 if (empty_range_varying (r, type, inner, outer))
2602 gcc_checking_assert (outer.varying_p ());
2603 gcc_checking_assert (inner.num_pairs () > 0);
2605 // Avoid a temporary by folding the first pair directly into the result.
2606 fold_pair (r, 0, inner, outer);
2608 // Then process any additonal pairs by unioning with their results.
2609 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2612 fold_pair (tmp, x, inner, outer);
2618 // Update the nonzero mask. Truncating casts are problematic unless
2619 // the conversion fits in the resulting outer type.
2620 wide_int nz = inner.get_nonzero_bits ();
2621 if (truncating_cast_p (inner, outer)
2622 && wi::rshift (nz, wi::uhwi (TYPE_PRECISION (outer.type ()),
2623 TYPE_PRECISION (inner.type ())),
2624 TYPE_SIGN (inner.type ())) != 0)
2626 nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
2627 r.set_nonzero_bits (nz);
2633 operator_cast::op1_range (irange &r, tree type,
2636 relation_trio) const
2638 if (lhs.undefined_p ())
2640 tree lhs_type = lhs.type ();
2641 gcc_checking_assert (types_compatible_p (op2.type(), type));
2643 // If we are calculating a pointer, shortcut to what we really care about.
2644 if (POINTER_TYPE_P (type))
2646 // Conversion from other pointers or a constant (including 0/NULL)
2647 // are straightforward.
2648 if (POINTER_TYPE_P (lhs.type ())
2649 || (lhs.singleton_p ()
2650 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2653 range_cast (r, type);
2657 // If the LHS is not a pointer nor a singleton, then it is
2658 // either VARYING or non-zero.
2659 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2660 r.set_nonzero (type);
2662 r.set_varying (type);
2668 if (truncating_cast_p (op2, lhs))
2670 if (lhs.varying_p ())
2671 r.set_varying (type);
2674 // We want to insert the LHS as an unsigned value since it
2675 // would not trigger the signed bit of the larger type.
2676 int_range_max converted_lhs = lhs;
2677 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2678 range_cast (converted_lhs, type);
2679 // Start by building the positive signed outer range for the type.
2680 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2681 TYPE_PRECISION (type));
2682 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2684 // For the signed part, we need to simply union the 2 ranges now.
2685 r.union_ (converted_lhs);
2687 // Create maximal negative number outside of LHS bits.
2688 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2689 TYPE_PRECISION (type));
2690 // Add this to the unsigned LHS range(s).
2691 int_range_max lim_range (type, lim, lim);
2692 int_range_max lhs_neg;
2693 range_op_handler (PLUS_EXPR, type).fold_range (lhs_neg, type,
2696 // lhs_neg now has all the negative versions of the LHS.
2697 // Now union in all the values from SIGNED MIN (0x80000) to
2698 // lim-1 in order to fill in all the ranges with the upper
2701 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2702 // we don't need to create a range from min to lim-1
2703 // calculate neg range traps trying to create [lim, lim - 1].
2704 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2707 int_range_max neg (type,
2708 wi::min_value (TYPE_PRECISION (type),
2711 lhs_neg.union_ (neg);
2713 // And finally, munge the signed and unsigned portions.
2716 // And intersect with any known value passed in the extra operand.
2722 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2726 // The cast is not truncating, and the range is restricted to
2727 // the range of the RHS by this assignment.
2729 // Cast the range of the RHS to the type of the LHS.
2730 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2731 // Intersect this with the LHS range will produce the range,
2732 // which will be cast to the RHS type before returning.
2733 tmp.intersect (lhs);
2736 // Cast the calculated range to the type of the RHS.
2737 fold_range (r, type, tmp, int_range<1> (type));
2742 class operator_logical_and : public range_operator
2744 using range_operator::fold_range;
2745 using range_operator::op1_range;
2746 using range_operator::op2_range;
2748 virtual bool fold_range (irange &r, tree type,
2751 relation_trio rel = TRIO_VARYING) const;
2752 virtual bool op1_range (irange &r, tree type,
2755 relation_trio rel = TRIO_VARYING) const;
2756 virtual bool op2_range (irange &r, tree type,
2759 relation_trio rel = TRIO_VARYING) const;
2764 operator_logical_and::fold_range (irange &r, tree type,
2767 relation_trio) const
2769 if (empty_range_varying (r, type, lh, rh))
2772 // 0 && anything is 0.
2773 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2774 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2775 r = range_false (type);
2776 else if (lh.contains_p (build_zero_cst (lh.type ()))
2777 || rh.contains_p (build_zero_cst (rh.type ())))
2778 // To reach this point, there must be a logical 1 on each side, and
2779 // the only remaining question is whether there is a zero or not.
2780 r = range_true_and_false (type);
2782 r = range_true (type);
2787 operator_logical_and::op1_range (irange &r, tree type,
2789 const irange &op2 ATTRIBUTE_UNUSED,
2790 relation_trio) const
2792 switch (get_bool_state (r, lhs, type))
2795 // A true result means both sides of the AND must be true.
2796 r = range_true (type);
2799 // Any other result means only one side has to be false, the
2800 // other side can be anything. So we cannot be sure of any
2802 r = range_true_and_false (type);
2809 operator_logical_and::op2_range (irange &r, tree type,
2812 relation_trio) const
2814 return operator_logical_and::op1_range (r, type, lhs, op1);
2818 class operator_bitwise_and : public range_operator
2820 using range_operator::fold_range;
2821 using range_operator::op1_range;
2822 using range_operator::op2_range;
2824 virtual bool fold_range (irange &r, tree type,
2827 relation_trio rel = TRIO_VARYING) const;
2828 virtual bool op1_range (irange &r, tree type,
2831 relation_trio rel = TRIO_VARYING) const;
2832 virtual bool op2_range (irange &r, tree type,
2835 relation_trio rel = TRIO_VARYING) const;
2836 virtual void wi_fold (irange &r, tree type,
2837 const wide_int &lh_lb,
2838 const wide_int &lh_ub,
2839 const wide_int &rh_lb,
2840 const wide_int &rh_ub) const;
2841 virtual relation_kind lhs_op1_relation (const irange &lhs,
2844 relation_kind) const;
2846 void simple_op1_range_solver (irange &r, tree type,
2848 const irange &op2) const;
2852 operator_bitwise_and::fold_range (irange &r, tree type,
2855 relation_trio) const
2857 if (range_operator::fold_range (r, type, lh, rh))
2859 if (!lh.undefined_p () && !rh.undefined_p ())
2860 r.set_nonzero_bits (wi::bit_and (lh.get_nonzero_bits (),
2861 rh.get_nonzero_bits ()));
2868 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2869 // by considering the number of leading redundant sign bit copies.
2870 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2871 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2873 wi_optimize_signed_bitwise_op (irange &r, tree type,
2874 const wide_int &lh_lb, const wide_int &lh_ub,
2875 const wide_int &rh_lb, const wide_int &rh_ub)
2877 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2878 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2879 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2882 int type_prec = TYPE_PRECISION (type);
2883 int rprec = (type_prec - new_clrsb) - 1;
2884 value_range_with_overflow (r, type,
2885 wi::mask (rprec, true, type_prec),
2886 wi::mask (rprec, false, type_prec));
2890 // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
2894 operator_bitwise_and::lhs_op1_relation (const irange &lhs,
2897 relation_kind) const
2899 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
2900 return VREL_VARYING;
2901 if (!op2.singleton_p ())
2902 return VREL_VARYING;
2903 // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
2904 int prec1 = TYPE_PRECISION (op1.type ());
2905 int prec2 = TYPE_PRECISION (op2.type ());
2907 wide_int mask = op2.lower_bound ();
2908 if (wi::eq_p (mask, wi::mask (8, false, prec2)))
2910 else if (wi::eq_p (mask, wi::mask (16, false, prec2)))
2912 else if (wi::eq_p (mask, wi::mask (32, false, prec2)))
2914 else if (wi::eq_p (mask, wi::mask (64, false, prec2)))
2916 return bits_to_pe (MIN (prec1, mask_prec));
2919 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2920 // possible. Basically, see if we can optimize:
2924 // [LB op Z, UB op Z]
2926 // If the optimization was successful, accumulate the range in R and
2930 wi_optimize_and_or (irange &r,
2931 enum tree_code code,
2933 const wide_int &lh_lb, const wide_int &lh_ub,
2934 const wide_int &rh_lb, const wide_int &rh_ub)
2936 // Calculate the singleton mask among the ranges, if any.
2937 wide_int lower_bound, upper_bound, mask;
2938 if (wi::eq_p (rh_lb, rh_ub))
2941 lower_bound = lh_lb;
2942 upper_bound = lh_ub;
2944 else if (wi::eq_p (lh_lb, lh_ub))
2947 lower_bound = rh_lb;
2948 upper_bound = rh_ub;
2953 // If Z is a constant which (for op | its bitwise not) has n
2954 // consecutive least significant bits cleared followed by m 1
2955 // consecutive bits set immediately above it and either
2956 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2958 // The least significant n bits of all the values in the range are
2959 // cleared or set, the m bits above it are preserved and any bits
2960 // above these are required to be the same for all values in the
2964 if (code == BIT_IOR_EXPR)
2966 if (wi::eq_p (w, 0))
2967 n = w.get_precision ();
2971 w = ~(w | wi::mask (n, false, w.get_precision ()));
2972 if (wi::eq_p (w, 0))
2973 m = w.get_precision () - n;
2975 m = wi::ctz (w) - n;
2977 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2978 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2981 wide_int res_lb, res_ub;
2982 if (code == BIT_AND_EXPR)
2984 res_lb = wi::bit_and (lower_bound, mask);
2985 res_ub = wi::bit_and (upper_bound, mask);
2987 else if (code == BIT_IOR_EXPR)
2989 res_lb = wi::bit_or (lower_bound, mask);
2990 res_ub = wi::bit_or (upper_bound, mask);
2994 value_range_with_overflow (r, type, res_lb, res_ub);
2996 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2997 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
3000 tmp.set_nonzero (type);
3006 // For range [LB, UB] compute two wide_int bit masks.
3008 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3009 // for all numbers in the range the bit is 0, otherwise it might be 0
3012 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3013 // for all numbers in the range the bit is 1, otherwise it might be 0
3017 wi_set_zero_nonzero_bits (tree type,
3018 const wide_int &lb, const wide_int &ub,
3019 wide_int &maybe_nonzero,
3020 wide_int &mustbe_nonzero)
3022 signop sign = TYPE_SIGN (type);
3024 if (wi::eq_p (lb, ub))
3025 maybe_nonzero = mustbe_nonzero = lb;
3026 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
3028 wide_int xor_mask = lb ^ ub;
3029 maybe_nonzero = lb | ub;
3030 mustbe_nonzero = lb & ub;
3033 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
3034 maybe_nonzero.get_precision ());
3035 maybe_nonzero = maybe_nonzero | mask;
3036 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
3041 maybe_nonzero = wi::minus_one (lb.get_precision ());
3042 mustbe_nonzero = wi::zero (lb.get_precision ());
3047 operator_bitwise_and::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
3053 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3056 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3057 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3058 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3059 maybe_nonzero_lh, mustbe_nonzero_lh);
3060 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3061 maybe_nonzero_rh, mustbe_nonzero_rh);
3063 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
3064 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
3065 signop sign = TYPE_SIGN (type);
3066 unsigned prec = TYPE_PRECISION (type);
3067 // If both input ranges contain only negative values, we can
3068 // truncate the result range maximum to the minimum of the
3069 // input range maxima.
3070 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
3072 new_ub = wi::min (new_ub, lh_ub, sign);
3073 new_ub = wi::min (new_ub, rh_ub, sign);
3075 // If either input range contains only non-negative values
3076 // we can truncate the result range maximum to the respective
3077 // maximum of the input range.
3078 if (wi::ge_p (lh_lb, 0, sign))
3079 new_ub = wi::min (new_ub, lh_ub, sign);
3080 if (wi::ge_p (rh_lb, 0, sign))
3081 new_ub = wi::min (new_ub, rh_ub, sign);
3082 // PR68217: In case of signed & sign-bit-CST should
3083 // result in [-INF, 0] instead of [-INF, INF].
3084 if (wi::gt_p (new_lb, new_ub, sign))
3086 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
3088 && ((wi::eq_p (lh_lb, lh_ub)
3089 && !wi::cmps (lh_lb, sign_bit))
3090 || (wi::eq_p (rh_lb, rh_ub)
3091 && !wi::cmps (rh_lb, sign_bit))))
3093 new_lb = wi::min_value (prec, sign);
3094 new_ub = wi::zero (prec);
3097 // If the limits got swapped around, return varying.
3098 if (wi::gt_p (new_lb, new_ub,sign))
3101 && wi_optimize_signed_bitwise_op (r, type,
3105 r.set_varying (type);
3108 value_range_with_overflow (r, type, new_lb, new_ub);
3112 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
3114 if (!lhs.contains_p (build_zero_cst (type)))
3115 r = range_nonzero (type);
3117 r.set_varying (type);
3120 // This was shamelessly stolen from register_edge_assert_for_2 and
3121 // adjusted to work with iranges.
3124 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
3126 const irange &op2) const
3128 if (!op2.singleton_p ())
3130 set_nonzero_range_from_mask (r, type, lhs);
3133 unsigned int nprec = TYPE_PRECISION (type);
3134 wide_int cst2v = op2.lower_bound ();
3135 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
3138 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3140 sgnbit = wi::zero (nprec);
3142 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3144 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3145 // maximum unsigned value is ~0. For signed comparison, if CST2
3146 // doesn't have the most significant bit set, handle it similarly. If
3147 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3148 wide_int valv = lhs.lower_bound ();
3149 wide_int minv = valv & cst2v, maxv;
3150 bool we_know_nothing = false;
3153 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3154 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3157 // If we can't determine anything on this bound, fall
3158 // through and conservatively solve for the other end point.
3159 we_know_nothing = true;
3162 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3163 if (we_know_nothing)
3164 r.set_varying (type);
3166 r = int_range<1> (type, minv, maxv);
3168 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3170 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3171 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3173 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3175 // For signed comparison, if CST2 doesn't have most significant bit
3176 // set, handle it similarly. If CST2 has MSB set, the maximum is
3177 // the same and minimum is INT_MIN.
3178 valv = lhs.upper_bound ();
3179 minv = valv & cst2v;
3184 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3187 // If we couldn't determine anything on either bound, return
3189 if (we_know_nothing)
3197 int_range<1> upper_bits (type, minv, maxv);
3198 r.intersect (upper_bits);
3202 operator_bitwise_and::op1_range (irange &r, tree type,
3205 relation_trio) const
3207 if (lhs.undefined_p ())
3209 if (types_compatible_p (type, boolean_type_node))
3210 return op_logical_and.op1_range (r, type, lhs, op2);
3213 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
3215 int_range_max chunk (lhs.type (),
3216 lhs.lower_bound (i),
3217 lhs.upper_bound (i));
3219 simple_op1_range_solver (res, type, chunk, op2);
3222 if (r.undefined_p ())
3223 set_nonzero_range_from_mask (r, type, lhs);
3225 // For 0 = op1 & MASK, op1 is ~MASK.
3226 if (lhs.zero_p () && op2.singleton_p ())
3228 wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
3229 int_range<2> tmp (type);
3230 tmp.set_nonzero_bits (nz);
3237 operator_bitwise_and::op2_range (irange &r, tree type,
3240 relation_trio) const
3242 return operator_bitwise_and::op1_range (r, type, lhs, op1);
3246 class operator_logical_or : public range_operator
3248 using range_operator::fold_range;
3249 using range_operator::op1_range;
3250 using range_operator::op2_range;
3252 virtual bool fold_range (irange &r, tree type,
3255 relation_trio rel = TRIO_VARYING) const;
3256 virtual bool op1_range (irange &r, tree type,
3259 relation_trio rel = TRIO_VARYING) const;
3260 virtual bool op2_range (irange &r, tree type,
3263 relation_trio rel = TRIO_VARYING) const;
3267 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3270 relation_trio) const
3272 if (empty_range_varying (r, type, lh, rh))
3281 operator_logical_or::op1_range (irange &r, tree type,
3283 const irange &op2 ATTRIBUTE_UNUSED,
3284 relation_trio) const
3286 switch (get_bool_state (r, lhs, type))
3289 // A false result means both sides of the OR must be false.
3290 r = range_false (type);
3293 // Any other result means only one side has to be true, the
3294 // other side can be anything. so we can't be sure of any result
3296 r = range_true_and_false (type);
3303 operator_logical_or::op2_range (irange &r, tree type,
3306 relation_trio) const
3308 return operator_logical_or::op1_range (r, type, lhs, op1);
3312 class operator_bitwise_or : public range_operator
3314 using range_operator::op1_range;
3315 using range_operator::op2_range;
3317 virtual bool op1_range (irange &r, tree type,
3320 relation_trio rel = TRIO_VARYING) const;
3321 virtual bool op2_range (irange &r, tree type,
3324 relation_trio rel = TRIO_VARYING) const;
3325 virtual void wi_fold (irange &r, tree type,
3326 const wide_int &lh_lb,
3327 const wide_int &lh_ub,
3328 const wide_int &rh_lb,
3329 const wide_int &rh_ub) const;
3333 operator_bitwise_or::wi_fold (irange &r, tree type,
3334 const wide_int &lh_lb,
3335 const wide_int &lh_ub,
3336 const wide_int &rh_lb,
3337 const wide_int &rh_ub) const
3339 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3342 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3343 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3344 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3345 maybe_nonzero_lh, mustbe_nonzero_lh);
3346 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3347 maybe_nonzero_rh, mustbe_nonzero_rh);
3348 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3349 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3350 signop sign = TYPE_SIGN (type);
3351 // If the input ranges contain only positive values we can
3352 // truncate the minimum of the result range to the maximum
3353 // of the input range minima.
3354 if (wi::ge_p (lh_lb, 0, sign)
3355 && wi::ge_p (rh_lb, 0, sign))
3357 new_lb = wi::max (new_lb, lh_lb, sign);
3358 new_lb = wi::max (new_lb, rh_lb, sign);
3360 // If either input range contains only negative values
3361 // we can truncate the minimum of the result range to the
3362 // respective minimum range.
3363 if (wi::lt_p (lh_ub, 0, sign))
3364 new_lb = wi::max (new_lb, lh_lb, sign);
3365 if (wi::lt_p (rh_ub, 0, sign))
3366 new_lb = wi::max (new_lb, rh_lb, sign);
3367 // If the limits got swapped around, return a conservative range.
3368 if (wi::gt_p (new_lb, new_ub, sign))
3370 // Make sure that nonzero|X is nonzero.
3371 if (wi::gt_p (lh_lb, 0, sign)
3372 || wi::gt_p (rh_lb, 0, sign)
3373 || wi::lt_p (lh_ub, 0, sign)
3374 || wi::lt_p (rh_ub, 0, sign))
3375 r.set_nonzero (type);
3376 else if (sign == SIGNED
3377 && wi_optimize_signed_bitwise_op (r, type,
3382 r.set_varying (type);
3385 value_range_with_overflow (r, type, new_lb, new_ub);
3389 operator_bitwise_or::op1_range (irange &r, tree type,
3392 relation_trio) const
3394 if (lhs.undefined_p ())
3396 // If this is really a logical wi_fold, call that.
3397 if (types_compatible_p (type, boolean_type_node))
3398 return op_logical_or.op1_range (r, type, lhs, op2);
3402 tree zero = build_zero_cst (type);
3403 r = int_range<1> (zero, zero);
3406 r.set_varying (type);
3411 operator_bitwise_or::op2_range (irange &r, tree type,
3414 relation_trio) const
3416 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3420 class operator_bitwise_xor : public range_operator
3422 using range_operator::op1_range;
3423 using range_operator::op2_range;
3425 virtual void wi_fold (irange &r, tree type,
3426 const wide_int &lh_lb,
3427 const wide_int &lh_ub,
3428 const wide_int &rh_lb,
3429 const wide_int &rh_ub) const;
3430 virtual bool op1_range (irange &r, tree type,
3433 relation_trio rel = TRIO_VARYING) const;
3434 virtual bool op2_range (irange &r, tree type,
3437 relation_trio rel = TRIO_VARYING) const;
3438 virtual bool op1_op2_relation_effect (irange &lhs_range,
3440 const irange &op1_range,
3441 const irange &op2_range,
3442 relation_kind rel) const;
3446 operator_bitwise_xor::wi_fold (irange &r, tree type,
3447 const wide_int &lh_lb,
3448 const wide_int &lh_ub,
3449 const wide_int &rh_lb,
3450 const wide_int &rh_ub) const
3452 signop sign = TYPE_SIGN (type);
3453 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3454 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3455 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3456 maybe_nonzero_lh, mustbe_nonzero_lh);
3457 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3458 maybe_nonzero_rh, mustbe_nonzero_rh);
3460 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3461 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3462 wide_int result_one_bits
3463 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3464 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3465 wide_int new_ub = ~result_zero_bits;
3466 wide_int new_lb = result_one_bits;
3468 // If the range has all positive or all negative values, the result
3469 // is better than VARYING.
3470 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3471 value_range_with_overflow (r, type, new_lb, new_ub);
3472 else if (sign == SIGNED
3473 && wi_optimize_signed_bitwise_op (r, type,
3478 r.set_varying (type);
3480 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3481 if (wi::lt_p (lh_ub, rh_lb, sign)
3482 || wi::lt_p (rh_ub, lh_lb, sign)
3483 || wi::ne_p (result_one_bits, 0))
3486 tmp.set_nonzero (type);
3492 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3496 relation_kind rel) const
3498 if (rel == VREL_VARYING)
3501 int_range<2> rel_range;
3506 rel_range.set_zero (type);
3509 rel_range.set_nonzero (type);
3515 lhs_range.intersect (rel_range);
3520 operator_bitwise_xor::op1_range (irange &r, tree type,
3523 relation_trio) const
3525 if (lhs.undefined_p () || lhs.varying_p ())
3530 if (types_compatible_p (type, boolean_type_node))
3532 switch (get_bool_state (r, lhs, type))
3535 if (op2.varying_p ())
3536 r.set_varying (type);
3537 else if (op2.zero_p ())
3538 r = range_true (type);
3540 r = range_false (type);
3550 r.set_varying (type);
3555 operator_bitwise_xor::op2_range (irange &r, tree type,
3558 relation_trio) const
3560 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3563 class operator_trunc_mod : public range_operator
3565 using range_operator::op1_range;
3566 using range_operator::op2_range;
3568 virtual void wi_fold (irange &r, tree type,
3569 const wide_int &lh_lb,
3570 const wide_int &lh_ub,
3571 const wide_int &rh_lb,
3572 const wide_int &rh_ub) const;
3573 virtual bool op1_range (irange &r, tree type,
3576 relation_trio) const;
3577 virtual bool op2_range (irange &r, tree type,
3580 relation_trio) const;
3584 operator_trunc_mod::wi_fold (irange &r, tree type,
3585 const wide_int &lh_lb,
3586 const wide_int &lh_ub,
3587 const wide_int &rh_lb,
3588 const wide_int &rh_ub) const
3590 wide_int new_lb, new_ub, tmp;
3591 signop sign = TYPE_SIGN (type);
3592 unsigned prec = TYPE_PRECISION (type);
3594 // Mod 0 is undefined.
3595 if (wi_zero_p (type, rh_lb, rh_ub))
3601 // Check for constant and try to fold.
3602 if (lh_lb == lh_ub && rh_lb == rh_ub)
3604 wi::overflow_type ov = wi::OVF_NONE;
3605 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3606 if (ov == wi::OVF_NONE)
3608 r = int_range<2> (type, tmp, tmp);
3613 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3618 new_ub = wi::smax (new_ub, tmp);
3621 if (sign == UNSIGNED)
3622 new_lb = wi::zero (prec);
3627 if (wi::gts_p (tmp, 0))
3628 tmp = wi::zero (prec);
3629 new_lb = wi::smax (new_lb, tmp);
3632 if (sign == SIGNED && wi::neg_p (tmp))
3633 tmp = wi::zero (prec);
3634 new_ub = wi::min (new_ub, tmp, sign);
3636 value_range_with_overflow (r, type, new_lb, new_ub);
3640 operator_trunc_mod::op1_range (irange &r, tree type,
3643 relation_trio) const
3645 if (lhs.undefined_p ())
3648 signop sign = TYPE_SIGN (type);
3649 unsigned prec = TYPE_PRECISION (type);
3650 // (a % b) >= x && x > 0 , then a >= x.
3651 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3653 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3656 // (a % b) <= x && x < 0 , then a <= x.
3657 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3659 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3666 operator_trunc_mod::op2_range (irange &r, tree type,
3669 relation_trio) const
3671 if (lhs.undefined_p ())
3674 signop sign = TYPE_SIGN (type);
3675 unsigned prec = TYPE_PRECISION (type);
3676 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3677 // or b > x for unsigned.
3678 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3681 r = value_range (type, wi::neg (lhs.lower_bound ()),
3682 lhs.lower_bound (), VR_ANTI_RANGE);
3683 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3685 r = value_range (type, lhs.lower_bound () + 1,
3686 wi::max_value (prec, sign));
3691 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3692 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3694 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3695 r = value_range (type, lhs.upper_bound (),
3696 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3705 class operator_logical_not : public range_operator
3707 using range_operator::fold_range;
3708 using range_operator::op1_range;
3710 virtual bool fold_range (irange &r, tree type,
3713 relation_trio rel = TRIO_VARYING) const;
3714 virtual bool op1_range (irange &r, tree type,
3717 relation_trio rel = TRIO_VARYING) const;
3720 // Folding a logical NOT, oddly enough, involves doing nothing on the
3721 // forward pass through. During the initial walk backwards, the
3722 // logical NOT reversed the desired outcome on the way back, so on the
3723 // way forward all we do is pass the range forward.
3728 // to determine the TRUE branch, walking backward
3729 // if (b_3) if ([1,1])
3730 // b_3 = !b_2 [1,1] = ![0,0]
3731 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3732 // which is the result we are looking for.. so.. pass it through.
3735 operator_logical_not::fold_range (irange &r, tree type,
3737 const irange &rh ATTRIBUTE_UNUSED,
3738 relation_trio) const
3740 if (empty_range_varying (r, type, lh, rh))
3744 if (!lh.varying_p () && !lh.undefined_p ())
3751 operator_logical_not::op1_range (irange &r,
3755 relation_trio) const
3757 // Logical NOT is involutary...do it again.
3758 return fold_range (r, type, lhs, op2);
3762 class operator_bitwise_not : public range_operator
3764 using range_operator::fold_range;
3765 using range_operator::op1_range;
3767 virtual bool fold_range (irange &r, tree type,
3770 relation_trio rel = TRIO_VARYING) const;
3771 virtual bool op1_range (irange &r, tree type,
3774 relation_trio rel = TRIO_VARYING) const;
3778 operator_bitwise_not::fold_range (irange &r, tree type,
3781 relation_trio) const
3783 if (empty_range_varying (r, type, lh, rh))
3786 if (types_compatible_p (type, boolean_type_node))
3787 return op_logical_not.fold_range (r, type, lh, rh);
3789 // ~X is simply -1 - X.
3790 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3791 wi::minus_one (TYPE_PRECISION (type)));
3792 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, minusone, lh);
3796 operator_bitwise_not::op1_range (irange &r, tree type,
3799 relation_trio) const
3801 if (lhs.undefined_p ())
3803 if (types_compatible_p (type, boolean_type_node))
3804 return op_logical_not.op1_range (r, type, lhs, op2);
3806 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3807 return fold_range (r, type, lhs, op2);
3811 class operator_cst : public range_operator
3813 using range_operator::fold_range;
3815 virtual bool fold_range (irange &r, tree type,
3818 relation_trio rel = TRIO_VARYING) const;
3822 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3824 const irange &rh ATTRIBUTE_UNUSED,
3825 relation_trio) const
3832 class operator_identity : public range_operator
3834 using range_operator::fold_range;
3835 using range_operator::op1_range;
3836 using range_operator::lhs_op1_relation;
3838 virtual bool fold_range (irange &r, tree type,
3841 relation_trio rel = TRIO_VARYING) const;
3842 virtual bool op1_range (irange &r, tree type,
3845 relation_trio rel = TRIO_VARYING) const;
3846 virtual relation_kind lhs_op1_relation (const irange &lhs,
3849 relation_kind rel) const;
3852 // Determine if there is a relationship between LHS and OP1.
3855 operator_identity::lhs_op1_relation (const irange &lhs,
3856 const irange &op1 ATTRIBUTE_UNUSED,
3857 const irange &op2 ATTRIBUTE_UNUSED,
3858 relation_kind) const
3860 if (lhs.undefined_p ())
3861 return VREL_VARYING;
3862 // Simply a copy, so they are equivalent.
3867 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3869 const irange &rh ATTRIBUTE_UNUSED,
3870 relation_trio) const
3877 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3879 const irange &op2 ATTRIBUTE_UNUSED,
3880 relation_trio) const
3887 class operator_unknown : public range_operator
3889 using range_operator::fold_range;
3891 virtual bool fold_range (irange &r, tree type,
3894 relation_trio rel = TRIO_VARYING) const;
3898 operator_unknown::fold_range (irange &r, tree type,
3899 const irange &lh ATTRIBUTE_UNUSED,
3900 const irange &rh ATTRIBUTE_UNUSED,
3901 relation_trio) const
3903 r.set_varying (type);
3908 class operator_abs : public range_operator
3910 using range_operator::op1_range;
3912 virtual void wi_fold (irange &r, tree type,
3913 const wide_int &lh_lb,
3914 const wide_int &lh_ub,
3915 const wide_int &rh_lb,
3916 const wide_int &rh_ub) const;
3917 virtual bool op1_range (irange &r, tree type,
3920 relation_trio) const;
3924 operator_abs::wi_fold (irange &r, tree type,
3925 const wide_int &lh_lb, const wide_int &lh_ub,
3926 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3927 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3930 signop sign = TYPE_SIGN (type);
3931 unsigned prec = TYPE_PRECISION (type);
3933 // Pass through LH for the easy cases.
3934 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3936 r = int_range<1> (type, lh_lb, lh_ub);
3940 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3942 wide_int min_value = wi::min_value (prec, sign);
3943 wide_int max_value = wi::max_value (prec, sign);
3944 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3946 r.set_varying (type);
3950 // ABS_EXPR may flip the range around, if the original range
3951 // included negative values.
3952 if (wi::eq_p (lh_lb, min_value))
3954 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3955 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3956 if (wi::eq_p (lh_ub, min_value))
3958 r = int_range<1> (type, min_value, min_value);
3964 min = wi::abs (lh_lb);
3966 if (wi::eq_p (lh_ub, min_value))
3969 max = wi::abs (lh_ub);
3971 // If the range contains zero then we know that the minimum value in the
3972 // range will be zero.
3973 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3975 if (wi::gt_p (min, max, sign))
3977 min = wi::zero (prec);
3981 // If the range was reversed, swap MIN and MAX.
3982 if (wi::gt_p (min, max, sign))
3983 std::swap (min, max);
3986 // If the new range has its limits swapped around (MIN > MAX), then
3987 // the operation caused one of them to wrap around. The only thing
3988 // we know is that the result is positive.
3989 if (wi::gt_p (min, max, sign))
3991 min = wi::zero (prec);
3994 r = int_range<1> (type, min, max);
3998 operator_abs::op1_range (irange &r, tree type,
4001 relation_trio) const
4003 if (empty_range_varying (r, type, lhs, op2))
4005 if (TYPE_UNSIGNED (type))
4010 // Start with the positives because negatives are an impossible result.
4011 int_range_max positives = range_positives (type);
4012 positives.intersect (lhs);
4014 // Then add the negative of each pair:
4015 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
4016 for (unsigned i = 0; i < positives.num_pairs (); ++i)
4017 r.union_ (int_range<1> (type,
4018 -positives.upper_bound (i),
4019 -positives.lower_bound (i)));
4020 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
4021 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
4022 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4023 wide_int lb = lhs.lower_bound ();
4024 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
4025 r.union_ (int_range<2> (type, lb, lb));
4030 class operator_absu : public range_operator
4033 virtual void wi_fold (irange &r, tree type,
4034 const wide_int &lh_lb, const wide_int &lh_ub,
4035 const wide_int &rh_lb, const wide_int &rh_ub) const;
4039 operator_absu::wi_fold (irange &r, tree type,
4040 const wide_int &lh_lb, const wide_int &lh_ub,
4041 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4042 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4044 wide_int new_lb, new_ub;
4046 // Pass through VR0 the easy cases.
4047 if (wi::ges_p (lh_lb, 0))
4054 new_lb = wi::abs (lh_lb);
4055 new_ub = wi::abs (lh_ub);
4057 // If the range contains zero then we know that the minimum
4058 // value in the range will be zero.
4059 if (wi::ges_p (lh_ub, 0))
4061 if (wi::gtu_p (new_lb, new_ub))
4063 new_lb = wi::zero (TYPE_PRECISION (type));
4066 std::swap (new_lb, new_ub);
4069 gcc_checking_assert (TYPE_UNSIGNED (type));
4070 r = int_range<1> (type, new_lb, new_ub);
4074 class operator_negate : public range_operator
4076 using range_operator::fold_range;
4077 using range_operator::op1_range;
4079 virtual bool fold_range (irange &r, tree type,
4082 relation_trio rel = TRIO_VARYING) const;
4083 virtual bool op1_range (irange &r, tree type,
4086 relation_trio rel = TRIO_VARYING) const;
4090 operator_negate::fold_range (irange &r, tree type,
4093 relation_trio) const
4095 if (empty_range_varying (r, type, lh, rh))
4097 // -X is simply 0 - X.
4098 return range_op_handler (MINUS_EXPR, type).fold_range (r, type,
4099 range_zero (type), lh);
4103 operator_negate::op1_range (irange &r, tree type,
4106 relation_trio) const
4108 // NEGATE is involutory.
4109 return fold_range (r, type, lhs, op2);
4113 class operator_addr_expr : public range_operator
4115 using range_operator::fold_range;
4116 using range_operator::op1_range;
4118 virtual bool fold_range (irange &r, tree type,
4121 relation_trio rel = TRIO_VARYING) const;
4122 virtual bool op1_range (irange &r, tree type,
4125 relation_trio rel = TRIO_VARYING) const;
4129 operator_addr_expr::fold_range (irange &r, tree type,
4132 relation_trio) const
4134 if (empty_range_varying (r, type, lh, rh))
4137 // Return a non-null pointer of the LHS type (passed in op2).
4139 r = range_zero (type);
4140 else if (!lh.contains_p (build_zero_cst (lh.type ())))
4141 r = range_nonzero (type);
4143 r.set_varying (type);
4148 operator_addr_expr::op1_range (irange &r, tree type,
4151 relation_trio) const
4153 return operator_addr_expr::fold_range (r, type, lhs, op2);
4157 class pointer_plus_operator : public range_operator
4160 virtual void wi_fold (irange &r, tree type,
4161 const wide_int &lh_lb,
4162 const wide_int &lh_ub,
4163 const wide_int &rh_lb,
4164 const wide_int &rh_ub) const;
4168 pointer_plus_operator::wi_fold (irange &r, tree type,
4169 const wide_int &lh_lb,
4170 const wide_int &lh_ub,
4171 const wide_int &rh_lb,
4172 const wide_int &rh_ub) const
4174 // Check for [0,0] + const, and simply return the const.
4175 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
4177 tree val = wide_int_to_tree (type, rh_lb);
4182 // For pointer types, we are really only interested in asserting
4183 // whether the expression evaluates to non-NULL.
4185 // With -fno-delete-null-pointer-checks we need to be more
4186 // conservative. As some object might reside at address 0,
4187 // then some offset could be added to it and the same offset
4188 // subtracted again and the result would be NULL.
4190 // static int a[12]; where &a[0] is NULL and
4193 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
4194 // where the first range doesn't include zero and the second one
4195 // doesn't either. As the second operand is sizetype (unsigned),
4196 // consider all ranges where the MSB could be set as possible
4197 // subtractions where the result might be NULL.
4198 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
4199 || !wi_includes_zero_p (type, rh_lb, rh_ub))
4200 && !TYPE_OVERFLOW_WRAPS (type)
4201 && (flag_delete_null_pointer_checks
4202 || !wi::sign_mask (rh_ub)))
4203 r = range_nonzero (type);
4204 else if (lh_lb == lh_ub && lh_lb == 0
4205 && rh_lb == rh_ub && rh_lb == 0)
4206 r = range_zero (type);
4208 r.set_varying (type);
4212 class pointer_min_max_operator : public range_operator
4215 virtual void wi_fold (irange & r, tree type,
4216 const wide_int &lh_lb, const wide_int &lh_ub,
4217 const wide_int &rh_lb, const wide_int &rh_ub) const;
4221 pointer_min_max_operator::wi_fold (irange &r, tree type,
4222 const wide_int &lh_lb,
4223 const wide_int &lh_ub,
4224 const wide_int &rh_lb,
4225 const wide_int &rh_ub) const
4227 // For MIN/MAX expressions with pointers, we only care about
4228 // nullness. If both are non null, then the result is nonnull.
4229 // If both are null, then the result is null. Otherwise they
4231 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4232 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4233 r = range_nonzero (type);
4234 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4235 r = range_zero (type);
4237 r.set_varying (type);
4241 class pointer_and_operator : public range_operator
4244 virtual void wi_fold (irange &r, tree type,
4245 const wide_int &lh_lb, const wide_int &lh_ub,
4246 const wide_int &rh_lb, const wide_int &rh_ub) const;
4250 pointer_and_operator::wi_fold (irange &r, tree type,
4251 const wide_int &lh_lb,
4252 const wide_int &lh_ub,
4253 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4254 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4256 // For pointer types, we are really only interested in asserting
4257 // whether the expression evaluates to non-NULL.
4258 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
4259 r = range_zero (type);
4261 r.set_varying (type);
4265 class pointer_or_operator : public range_operator
4267 using range_operator::op1_range;
4268 using range_operator::op2_range;
4270 virtual bool op1_range (irange &r, tree type,
4273 relation_trio rel = TRIO_VARYING) const;
4274 virtual bool op2_range (irange &r, tree type,
4277 relation_trio rel = TRIO_VARYING) const;
4278 virtual void wi_fold (irange &r, tree type,
4279 const wide_int &lh_lb, const wide_int &lh_ub,
4280 const wide_int &rh_lb, const wide_int &rh_ub) const;
4284 pointer_or_operator::op1_range (irange &r, tree type,
4286 const irange &op2 ATTRIBUTE_UNUSED,
4287 relation_trio) const
4289 if (lhs.undefined_p ())
4293 tree zero = build_zero_cst (type);
4294 r = int_range<1> (zero, zero);
4297 r.set_varying (type);
4302 pointer_or_operator::op2_range (irange &r, tree type,
4305 relation_trio) const
4307 return pointer_or_operator::op1_range (r, type, lhs, op1);
4311 pointer_or_operator::wi_fold (irange &r, tree type,
4312 const wide_int &lh_lb,
4313 const wide_int &lh_ub,
4314 const wide_int &rh_lb,
4315 const wide_int &rh_ub) const
4317 // For pointer types, we are really only interested in asserting
4318 // whether the expression evaluates to non-NULL.
4319 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4320 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4321 r = range_nonzero (type);
4322 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4323 r = range_zero (type);
4325 r.set_varying (type);
4328 // Return a pointer to the range_operator instance, if there is one
4329 // associated with tree_code CODE.
4332 range_op_table::operator[] (enum tree_code code)
4334 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4335 return m_range_tree[code];
4338 // Add OP to the handler table for CODE.
4341 range_op_table::set (enum tree_code code, range_operator &op)
4343 gcc_checking_assert (m_range_tree[code] == NULL);
4344 m_range_tree[code] = &op;
4347 // Instantiate a range op table for integral operations.
4349 class integral_table : public range_op_table
4353 } integral_tree_table;
4355 integral_table::integral_table ()
4357 set (EQ_EXPR, op_equal);
4358 set (NE_EXPR, op_not_equal);
4359 set (LT_EXPR, op_lt);
4360 set (LE_EXPR, op_le);
4361 set (GT_EXPR, op_gt);
4362 set (GE_EXPR, op_ge);
4363 set (PLUS_EXPR, op_plus);
4364 set (MINUS_EXPR, op_minus);
4365 set (MIN_EXPR, op_min);
4366 set (MAX_EXPR, op_max);
4367 set (MULT_EXPR, op_mult);
4368 set (TRUNC_DIV_EXPR, op_trunc_div);
4369 set (FLOOR_DIV_EXPR, op_floor_div);
4370 set (ROUND_DIV_EXPR, op_round_div);
4371 set (CEIL_DIV_EXPR, op_ceil_div);
4372 set (EXACT_DIV_EXPR, op_exact_div);
4373 set (LSHIFT_EXPR, op_lshift);
4374 set (RSHIFT_EXPR, op_rshift);
4375 set (NOP_EXPR, op_convert);
4376 set (CONVERT_EXPR, op_convert);
4377 set (TRUTH_AND_EXPR, op_logical_and);
4378 set (BIT_AND_EXPR, op_bitwise_and);
4379 set (TRUTH_OR_EXPR, op_logical_or);
4380 set (BIT_IOR_EXPR, op_bitwise_or);
4381 set (BIT_XOR_EXPR, op_bitwise_xor);
4382 set (TRUNC_MOD_EXPR, op_trunc_mod);
4383 set (TRUTH_NOT_EXPR, op_logical_not);
4384 set (BIT_NOT_EXPR, op_bitwise_not);
4385 set (INTEGER_CST, op_integer_cst);
4386 set (SSA_NAME, op_identity);
4387 set (PAREN_EXPR, op_identity);
4388 set (OBJ_TYPE_REF, op_identity);
4389 set (IMAGPART_EXPR, op_unknown);
4390 set (REALPART_EXPR, op_unknown);
4391 set (POINTER_DIFF_EXPR, op_pointer_diff);
4392 set (ABS_EXPR, op_abs);
4393 set (ABSU_EXPR, op_absu);
4394 set (NEGATE_EXPR, op_negate);
4395 set (ADDR_EXPR, op_addr);
4398 // Instantiate a range op table for pointer operations.
4400 class pointer_table : public range_op_table
4404 } pointer_tree_table;
4406 pointer_table::pointer_table ()
4408 set (BIT_AND_EXPR, op_pointer_and);
4409 set (BIT_IOR_EXPR, op_pointer_or);
4410 set (MIN_EXPR, op_ptr_min_max);
4411 set (MAX_EXPR, op_ptr_min_max);
4412 set (POINTER_PLUS_EXPR, op_pointer_plus);
4414 set (EQ_EXPR, op_equal);
4415 set (NE_EXPR, op_not_equal);
4416 set (LT_EXPR, op_lt);
4417 set (LE_EXPR, op_le);
4418 set (GT_EXPR, op_gt);
4419 set (GE_EXPR, op_ge);
4420 set (SSA_NAME, op_identity);
4421 set (INTEGER_CST, op_integer_cst);
4422 set (ADDR_EXPR, op_addr);
4423 set (NOP_EXPR, op_convert);
4424 set (CONVERT_EXPR, op_convert);
4426 set (BIT_NOT_EXPR, op_bitwise_not);
4427 set (BIT_XOR_EXPR, op_bitwise_xor);
4430 // The tables are hidden and accessed via a simple extern function.
4432 static inline range_operator *
4433 get_handler (enum tree_code code, tree type)
4435 // First check if there is a pointer specialization.
4436 if (POINTER_TYPE_P (type))
4437 return pointer_tree_table[code];
4438 if (INTEGRAL_TYPE_P (type))
4439 return integral_tree_table[code];
4443 // Return the floating point operator for CODE or NULL if none available.
4445 static inline range_operator_float *
4446 get_float_handler (enum tree_code code, tree)
4448 return (*floating_tree_table)[code];
4452 range_op_handler::set_op_handler (tree_code code, tree type)
4454 if (irange::supports_p (type))
4457 m_int = get_handler (code, type);
4458 m_valid = m_int != NULL;
4460 else if (frange::supports_p (type))
4463 m_float = get_float_handler (code, type);
4464 m_valid = m_float != NULL;
4474 range_op_handler::range_op_handler ()
4481 range_op_handler::range_op_handler (tree_code code, tree type)
4483 set_op_handler (code, type);
4488 range_op_handler::fold_range (vrange &r, tree type,
4491 relation_trio rel) const
4493 gcc_checking_assert (m_valid);
4495 return m_int->fold_range (as_a <irange> (r), type,
4497 as_a <irange> (rh), rel);
4499 if (is_a <irange> (r))
4501 if (is_a <irange> (rh))
4502 return m_float->fold_range (as_a <irange> (r), type,
4504 as_a <irange> (rh), rel);
4506 return m_float->fold_range (as_a <irange> (r), type,
4508 as_a <frange> (rh), rel);
4510 return m_float->fold_range (as_a <frange> (r), type,
4512 as_a <frange> (rh), rel);
4516 range_op_handler::op1_range (vrange &r, tree type,
4519 relation_trio rel) const
4521 gcc_checking_assert (m_valid);
4523 if (lhs.undefined_p ())
4526 return m_int->op1_range (as_a <irange> (r), type,
4527 as_a <irange> (lhs),
4528 as_a <irange> (op2), rel);
4530 if (is_a <irange> (lhs))
4531 return m_float->op1_range (as_a <frange> (r), type,
4532 as_a <irange> (lhs),
4533 as_a <frange> (op2), rel);
4534 return m_float->op1_range (as_a <frange> (r), type,
4535 as_a <frange> (lhs),
4536 as_a <frange> (op2), rel);
4540 range_op_handler::op2_range (vrange &r, tree type,
4543 relation_trio rel) const
4545 gcc_checking_assert (m_valid);
4546 if (lhs.undefined_p ())
4549 return m_int->op2_range (as_a <irange> (r), type,
4550 as_a <irange> (lhs),
4551 as_a <irange> (op1), rel);
4553 if (is_a <irange> (lhs))
4554 return m_float->op2_range (as_a <frange> (r), type,
4555 as_a <irange> (lhs),
4556 as_a <frange> (op1), rel);
4557 return m_float->op2_range (as_a <frange> (r), type,
4558 as_a <frange> (lhs),
4559 as_a <frange> (op1), rel);
4563 range_op_handler::lhs_op1_relation (const vrange &lhs,
4566 relation_kind rel) const
4568 gcc_checking_assert (m_valid);
4570 return m_int->lhs_op1_relation (as_a <irange> (lhs),
4571 as_a <irange> (op1),
4572 as_a <irange> (op2), rel);
4574 if (is_a <irange> (lhs))
4575 return m_float->lhs_op1_relation (as_a <irange> (lhs),
4576 as_a <frange> (op1),
4577 as_a <frange> (op2), rel);
4578 return m_float->lhs_op1_relation (as_a <frange> (lhs),
4579 as_a <frange> (op1),
4580 as_a <frange> (op2), rel);
4584 range_op_handler::lhs_op2_relation (const vrange &lhs,
4587 relation_kind rel) const
4589 gcc_checking_assert (m_valid);
4591 return m_int->lhs_op2_relation (as_a <irange> (lhs),
4592 as_a <irange> (op1),
4593 as_a <irange> (op2), rel);
4595 if (is_a <irange> (lhs))
4596 return m_float->lhs_op2_relation (as_a <irange> (lhs),
4597 as_a <frange> (op1),
4598 as_a <frange> (op2), rel);
4599 return m_float->lhs_op2_relation (as_a <frange> (lhs),
4600 as_a <frange> (op1),
4601 as_a <frange> (op2), rel);
4605 range_op_handler::op1_op2_relation (const vrange &lhs) const
4607 gcc_checking_assert (m_valid);
4609 return m_int->op1_op2_relation (as_a <irange> (lhs));
4610 if (is_a <irange> (lhs))
4611 return m_float->op1_op2_relation (as_a <irange> (lhs));
4612 return m_float->op1_op2_relation (as_a <frange> (lhs));
4615 // Cast the range in R to TYPE.
4618 range_cast (vrange &r, tree type)
4620 Value_Range tmp (r);
4621 Value_Range varying (type);
4622 varying.set_varying (type);
4623 range_op_handler op (CONVERT_EXPR, type);
4624 // Call op_convert, if it fails, the result is varying.
4625 if (!op || !op.fold_range (r, type, tmp, varying))
4627 r.set_varying (type);
4634 #include "selftest.h"
4638 #define INT(N) build_int_cst (integer_type_node, (N))
4639 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4640 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4641 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4642 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4643 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4646 range_op_cast_tests ()
4648 int_range<1> r0, r1, r2, rold;
4649 r0.set_varying (integer_type_node);
4650 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4652 // If a range is in any way outside of the range for the converted
4653 // to range, default to the range for the new type.
4654 r0.set_varying (short_integer_type_node);
4655 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4656 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4657 if (TYPE_PRECISION (TREE_TYPE (maxint))
4658 > TYPE_PRECISION (short_integer_type_node))
4660 r1 = int_range<1> (integer_zero_node, maxint);
4661 range_cast (r1, short_integer_type_node);
4662 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4663 && r1.upper_bound() == wi::to_wide (maxshort));
4666 // (unsigned char)[-5,-1] => [251,255].
4667 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4668 range_cast (r0, unsigned_char_type_node);
4669 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4670 range_cast (r0, signed_char_type_node);
4671 ASSERT_TRUE (r0 == rold);
4673 // (signed char)[15, 150] => [-128,-106][15,127].
4674 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4675 range_cast (r0, signed_char_type_node);
4676 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4677 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4679 ASSERT_TRUE (r1 == r0);
4680 range_cast (r0, unsigned_char_type_node);
4681 ASSERT_TRUE (r0 == rold);
4683 // (unsigned char)[-5, 5] => [0,5][251,255].
4684 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4685 range_cast (r0, unsigned_char_type_node);
4686 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4687 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4689 ASSERT_TRUE (r0 == r1);
4690 range_cast (r0, signed_char_type_node);
4691 ASSERT_TRUE (r0 == rold);
4693 // (unsigned char)[-5,5] => [0,5][251,255].
4694 r0 = int_range<1> (INT (-5), INT (5));
4695 range_cast (r0, unsigned_char_type_node);
4696 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4697 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4698 ASSERT_TRUE (r0 == r1);
4700 // (unsigned char)[5U,1974U] => [0,255].
4701 r0 = int_range<1> (UINT (5), UINT (1974));
4702 range_cast (r0, unsigned_char_type_node);
4703 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4704 range_cast (r0, integer_type_node);
4705 // Going to a wider range should not sign extend.
4706 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4708 // (unsigned char)[-350,15] => [0,255].
4709 r0 = int_range<1> (INT (-350), INT (15));
4710 range_cast (r0, unsigned_char_type_node);
4711 ASSERT_TRUE (r0 == (int_range<1>
4712 (TYPE_MIN_VALUE (unsigned_char_type_node),
4713 TYPE_MAX_VALUE (unsigned_char_type_node))));
4715 // Casting [-120,20] from signed char to unsigned short.
4716 // => [0, 20][0xff88, 0xffff].
4717 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4718 range_cast (r0, short_unsigned_type_node);
4719 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4720 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4722 ASSERT_TRUE (r0 == r1);
4723 // A truncating cast back to signed char will work because [-120, 20]
4724 // is representable in signed char.
4725 range_cast (r0, signed_char_type_node);
4726 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4728 // unsigned char -> signed short
4729 // (signed short)[(unsigned char)25, (unsigned char)250]
4730 // => [(signed short)25, (signed short)250]
4731 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4732 range_cast (r0, short_integer_type_node);
4733 r1 = int_range<1> (INT16 (25), INT16 (250));
4734 ASSERT_TRUE (r0 == r1);
4735 range_cast (r0, unsigned_char_type_node);
4736 ASSERT_TRUE (r0 == rold);
4738 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4739 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4740 TYPE_MAX_VALUE (long_long_integer_type_node));
4741 range_cast (r0, short_unsigned_type_node);
4742 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4743 TYPE_MAX_VALUE (short_unsigned_type_node));
4744 ASSERT_TRUE (r0 == r1);
4746 // Casting NONZERO to a narrower type will wrap/overflow so
4747 // it's just the entire range for the narrower type.
4749 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4750 // is outside of the range of a smaller range, return the full
4752 if (TYPE_PRECISION (integer_type_node)
4753 > TYPE_PRECISION (short_integer_type_node))
4755 r0 = range_nonzero (integer_type_node);
4756 range_cast (r0, short_integer_type_node);
4757 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4758 TYPE_MAX_VALUE (short_integer_type_node));
4759 ASSERT_TRUE (r0 == r1);
4762 // Casting NONZERO from a narrower signed to a wider signed.
4764 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4765 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4766 r0 = range_nonzero (short_integer_type_node);
4767 range_cast (r0, integer_type_node);
4768 r1 = int_range<1> (INT (-32768), INT (-1));
4769 r2 = int_range<1> (INT (1), INT (32767));
4771 ASSERT_TRUE (r0 == r1);
4775 range_op_lshift_tests ()
4777 // Test that 0x808.... & 0x8.... still contains 0x8....
4778 // for a large set of numbers.
4781 tree big_type = long_long_unsigned_type_node;
4782 // big_num = 0x808,0000,0000,0000
4783 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4784 build_int_cst (big_type, 0x808),
4785 build_int_cst (big_type, 48));
4786 op_bitwise_and.fold_range (res, big_type,
4787 int_range <1> (big_type),
4788 int_range <1> (big_num, big_num));
4789 // val = 0x8,0000,0000,0000
4790 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4791 build_int_cst (big_type, 0x8),
4792 build_int_cst (big_type, 48));
4793 ASSERT_TRUE (res.contains_p (val));
4796 if (TYPE_PRECISION (unsigned_type_node) > 31)
4798 // unsigned VARYING = op1 << 1 should be VARYING.
4799 int_range<2> lhs (unsigned_type_node);
4800 int_range<2> shift (INT (1), INT (1));
4802 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4803 ASSERT_TRUE (op1.varying_p ());
4805 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4806 int_range<2> zero (UINT (0), UINT (0));
4807 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4808 ASSERT_TRUE (op1.num_pairs () == 2);
4809 // Remove the [0,0] range.
4810 op1.intersect (zero);
4811 ASSERT_TRUE (op1.num_pairs () == 1);
4812 // op1 << 1 should be [0x8000,0x8000] << 1,
4813 // which should result in [0,0].
4814 int_range_max result;
4815 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4816 ASSERT_TRUE (result == zero);
4818 // signed VARYING = op1 << 1 should be VARYING.
4819 if (TYPE_PRECISION (integer_type_node) > 31)
4821 // unsigned VARYING = op1 << 1 hould be VARYING.
4822 int_range<2> lhs (integer_type_node);
4823 int_range<2> shift (INT (1), INT (1));
4825 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4826 ASSERT_TRUE (op1.varying_p ());
4828 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4829 int_range<2> zero (INT (0), INT (0));
4830 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4831 ASSERT_TRUE (op1.num_pairs () == 2);
4832 // Remove the [0,0] range.
4833 op1.intersect (zero);
4834 ASSERT_TRUE (op1.num_pairs () == 1);
4835 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4836 // which should result in [0,0].
4837 int_range_max result;
4838 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4839 ASSERT_TRUE (result == zero);
4844 range_op_rshift_tests ()
4846 // unsigned: [3, MAX] = OP1 >> 1
4848 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4849 TYPE_MAX_VALUE (unsigned_type_node));
4850 int_range_max one (build_one_cst (unsigned_type_node),
4851 build_one_cst (unsigned_type_node));
4853 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4854 ASSERT_FALSE (op1.contains_p (UINT (3)));
4857 // signed: [3, MAX] = OP1 >> 1
4859 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4860 int_range_max one (INT (1), INT (1));
4862 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4863 ASSERT_FALSE (op1.contains_p (INT (-2)));
4866 // This is impossible, so OP1 should be [].
4867 // signed: [MIN, MIN] = OP1 >> 1
4869 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4870 TYPE_MIN_VALUE (integer_type_node));
4871 int_range_max one (INT (1), INT (1));
4873 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4874 ASSERT_TRUE (op1.undefined_p ());
4877 // signed: ~[-1] = OP1 >> 31
4878 if (TYPE_PRECISION (integer_type_node) > 31)
4880 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4881 int_range_max shift (INT (31), INT (31));
4883 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4884 int_range_max negatives = range_negatives (integer_type_node);
4885 negatives.intersect (op1);
4886 ASSERT_TRUE (negatives.undefined_p ());
4891 range_op_bitwise_and_tests ()
4894 tree min = vrp_val_min (integer_type_node);
4895 tree max = vrp_val_max (integer_type_node);
4896 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4897 build_one_cst (integer_type_node));
4898 int_range_max i1 (tiny, max);
4899 int_range_max i2 (build_int_cst (integer_type_node, 255),
4900 build_int_cst (integer_type_node, 255));
4902 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4903 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4904 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4906 // VARYING = OP1 & 255: OP1 is VARYING
4907 i1 = int_range<1> (integer_type_node);
4908 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4909 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4911 // For 0 = x & MASK, x is ~MASK.
4913 int_range<2> zero (integer_zero_node, integer_zero_node);
4914 int_range<2> mask = int_range<2> (INT (7), INT (7));
4915 op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
4916 wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
4917 ASSERT_TRUE (res.get_nonzero_bits () == inv);
4920 // (NONZERO | X) is nonzero.
4921 i1.set_nonzero (integer_type_node);
4922 i2.set_varying (integer_type_node);
4923 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4924 ASSERT_TRUE (res.nonzero_p ());
4926 // (NEGATIVE | X) is nonzero.
4927 i1 = int_range<1> (INT (-5), INT (-3));
4928 i2.set_varying (integer_type_node);
4929 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4930 ASSERT_FALSE (res.contains_p (INT (0)));
4934 range_relational_tests ()
4936 int_range<2> lhs (unsigned_char_type_node);
4937 int_range<2> op1 (UCHAR (8), UCHAR (10));
4938 int_range<2> op2 (UCHAR (20), UCHAR (20));
4940 // Never wrapping additions mean LHS > OP1.
4941 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4942 ASSERT_TRUE (code == VREL_GT);
4944 // Most wrapping additions mean nothing...
4945 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4946 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4947 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4948 ASSERT_TRUE (code == VREL_VARYING);
4950 // However, always wrapping additions mean LHS < OP1.
4951 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4952 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4953 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4954 ASSERT_TRUE (code == VREL_LT);
4960 range_op_rshift_tests ();
4961 range_op_lshift_tests ();
4962 range_op_bitwise_and_tests ();
4963 range_op_cast_tests ();
4964 range_relational_tests ();
4966 extern void range_op_float_tests ();
4967 range_op_float_tests ();
4970 } // namespace selftest
4972 #endif // CHECKING_P