1 /* Code for range operators.
2 Copyright (C) 2017-2019 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-fold.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
49 // Return the upper limit for a type.
51 static inline wide_int
52 max_limit (const_tree type
)
54 return wi::max_value (TYPE_PRECISION (type
) , TYPE_SIGN (type
));
57 // Return the lower limit for a type.
59 static inline wide_int
60 min_limit (const_tree type
)
62 return wi::min_value (TYPE_PRECISION (type
) , TYPE_SIGN (type
));
65 // If the range of either op1 or op2 is undefined, set the result to
66 // undefined and return TRUE.
69 empty_range_check (value_range_base
&r
,
70 const value_range_base
&op1
,
71 const value_range_base
& op2
)
73 if (op1
.undefined_p () || op2
.undefined_p ())
82 // Return TRUE if shifting by OP is undefined behavior, and set R to
83 // the appropriate range.
86 undefined_shift_range_check (value_range_base
&r
, tree type
,
89 if (op
.undefined_p ())
91 r
= value_range_base ();
95 // Shifting by any values outside [0..prec-1], gets undefined
96 // behavior from the shift operation. We cannot even trust
97 // SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
98 // shifts, and the operation at the tree level may be widened.
99 if (wi::lt_p (op
.lower_bound (), 0, TYPE_SIGN (op
.type ()))
100 || wi::ge_p (op
.upper_bound (),
101 TYPE_PRECISION (type
), TYPE_SIGN (op
.type ())))
103 r
= value_range_base (type
);
109 // Return TRUE if 0 is within [WMIN, WMAX].
112 wi_includes_zero_p (tree type
, const wide_int
&wmin
, const wide_int
&wmax
)
114 signop sign
= TYPE_SIGN (type
);
115 return wi::le_p (wmin
, 0, sign
) && wi::ge_p (wmax
, 0, sign
);
118 // Return TRUE if [WMIN, WMAX] is the singleton 0.
121 wi_zero_p (tree type
, const wide_int
&wmin
, const wide_int
&wmax
)
123 unsigned prec
= TYPE_PRECISION (type
);
124 return wmin
== wmax
&& wi::eq_p (wmin
, wi::zero (prec
));
127 // Default wide_int fold operation returns [MIN, MAX].
130 range_operator::wi_fold (tree type
,
131 const wide_int
&lh_lb ATTRIBUTE_UNUSED
,
132 const wide_int
&lh_ub ATTRIBUTE_UNUSED
,
133 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
134 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
136 return value_range_base (type
);
139 // The default for fold is to break all ranges into sub-ranges and
140 // invoke the wi_fold method on each sub-range pair.
143 range_operator::fold_range (tree type
,
144 const value_range_base
&lh
,
145 const value_range_base
&rh
) const
148 if (empty_range_check (r
, lh
, rh
))
151 for (unsigned x
= 0; x
< lh
.num_pairs (); ++x
)
152 for (unsigned y
= 0; y
< rh
.num_pairs (); ++y
)
154 wide_int lh_lb
= lh
.lower_bound (x
);
155 wide_int lh_ub
= lh
.upper_bound (x
);
156 wide_int rh_lb
= rh
.lower_bound (y
);
157 wide_int rh_ub
= rh
.upper_bound (y
);
158 r
.union_ (wi_fold (type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
));
165 // The default for op1_range is to return false.
168 range_operator::op1_range (value_range_base
&r ATTRIBUTE_UNUSED
,
169 tree type ATTRIBUTE_UNUSED
,
170 const value_range_base
&lhs ATTRIBUTE_UNUSED
,
171 const value_range_base
&op2 ATTRIBUTE_UNUSED
) const
176 // The default for op2_range is to return false.
179 range_operator::op2_range (value_range_base
&r ATTRIBUTE_UNUSED
,
180 tree type ATTRIBUTE_UNUSED
,
181 const value_range_base
&lhs ATTRIBUTE_UNUSED
,
182 const value_range_base
&op1 ATTRIBUTE_UNUSED
) const
188 // Create and return a range from a pair of wide-ints that are known
189 // to have overflowed (or underflowed).
191 static value_range_base
192 value_range_from_overflowed_bounds (tree type
,
193 const wide_int
&wmin
,
194 const wide_int
&wmax
)
196 const signop sgn
= TYPE_SIGN (type
);
197 const unsigned int prec
= TYPE_PRECISION (type
);
199 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
200 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
205 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
208 if (wi::cmp (tmax
, tem
, sgn
) > 0)
211 // If the anti-range would cover nothing, drop to varying.
212 // Likewise if the anti-range bounds are outside of the types
214 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
215 return value_range_base (type
);
217 return value_range_base (VR_ANTI_RANGE
, type
, tmin
, tmax
);
220 // Create and return a range from a pair of wide-ints. MIN_OVF and
221 // MAX_OVF describe any overflow that might have occurred while
222 // calculating WMIN and WMAX respectively.
224 static value_range_base
225 value_range_with_overflow (tree type
,
226 const wide_int
&wmin
, const wide_int
&wmax
,
227 wi::overflow_type min_ovf
= wi::OVF_NONE
,
228 wi::overflow_type max_ovf
= wi::OVF_NONE
)
230 const signop sgn
= TYPE_SIGN (type
);
231 const unsigned int prec
= TYPE_PRECISION (type
);
232 const bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
234 // For one bit precision if max != min, then the range covers all
236 if (prec
== 1 && wi::ne_p (wmax
, wmin
))
237 return value_range_base (type
);
241 // If overflow wraps, truncate the values and adjust the range,
242 // kind, and bounds appropriately.
243 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
245 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
246 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
247 // If the limits are swapped, we wrapped around and cover
249 if (wi::gt_p (tmin
, tmax
, sgn
))
250 return value_range_base (type
);
252 // No overflow or both overflow or underflow. The range
253 // kind stays normal.
254 return value_range_base (type
, tmin
, tmax
);
257 if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
258 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
259 return value_range_from_overflowed_bounds (type
, wmin
, wmax
);
261 // Other underflow and/or overflow, drop to VR_VARYING.
262 return value_range_base (type
);
266 // If overflow does not wrap, saturate to [MIN, MAX].
267 wide_int new_lb
, new_ub
;
268 if (min_ovf
== wi::OVF_UNDERFLOW
)
269 new_lb
= wi::min_value (prec
, sgn
);
270 else if (min_ovf
== wi::OVF_OVERFLOW
)
271 new_lb
= wi::max_value (prec
, sgn
);
275 if (max_ovf
== wi::OVF_UNDERFLOW
)
276 new_ub
= wi::min_value (prec
, sgn
);
277 else if (max_ovf
== wi::OVF_OVERFLOW
)
278 new_ub
= wi::max_value (prec
, sgn
);
282 return value_range_base (type
, new_lb
, new_ub
);
286 // Create and return a range from a pair of wide-ints. Canonicalize
287 // the case where the bounds are swapped. In which case, we transform
288 // [10,5] into [MIN,5][10,MAX].
290 static inline value_range_base
291 create_possibly_reversed_range (tree type
,
292 const wide_int
&new_lb
, const wide_int
&new_ub
)
294 signop s
= TYPE_SIGN (type
);
295 // If the bounds are swapped, treat the result as if an overflow occured.
296 if (wi::gt_p (new_lb
, new_ub
, s
))
297 return value_range_from_overflowed_bounds (type
, new_lb
, new_ub
);
299 // Otherwise its just a normal range.
300 return value_range_base (type
, new_lb
, new_ub
);
303 // Return a value_range_base instance that is a boolean TRUE.
305 static inline value_range_base
306 range_true (tree type
)
308 unsigned prec
= TYPE_PRECISION (type
);
309 return value_range_base (type
, wi::one (prec
), wi::one (prec
));
312 // Return a value_range_base instance that is a boolean FALSE.
314 static inline value_range_base
315 range_false (tree type
)
317 unsigned prec
= TYPE_PRECISION (type
);
318 return value_range_base (type
, wi::zero (prec
), wi::zero (prec
));
321 // Return a value_range_base that covers both true and false.
323 static inline value_range_base
324 range_true_and_false (tree type
)
326 unsigned prec
= TYPE_PRECISION (type
);
327 return value_range_base (type
, wi::zero (prec
), wi::one (prec
));
330 enum bool_range_state
{ BRS_FALSE
, BRS_TRUE
, BRS_EMPTY
, BRS_FULL
};
332 // Return the summary information about boolean range LHS. Return an
333 // "interesting" range in R. For EMPTY or FULL, return the equivalent
334 // range for TYPE, for BRS_TRUE and BRS false, return the negation of
337 static bool_range_state
338 get_bool_state (value_range_base
&r
,
339 const value_range_base
&lhs
, tree val_type
)
341 // If there is no result, then this is unexecutable.
342 if (lhs
.undefined_p ())
348 // If the bounds aren't the same, then it's not a constant.
349 if (!wi::eq_p (lhs
.upper_bound (), lhs
.lower_bound ()))
351 r
.set_varying (val_type
);
362 class operator_equal
: public range_operator
365 virtual value_range_base
fold_range (tree type
,
366 const value_range_base
&op1
,
367 const value_range_base
&op2
) const;
368 virtual bool op1_range (value_range_base
&r
, tree type
,
369 const value_range_base
&lhs
,
370 const value_range_base
&val
) const;
371 virtual bool op2_range (value_range_base
&r
, tree type
,
372 const value_range_base
&lhs
,
373 const value_range_base
&val
) const;
377 operator_equal::fold_range (tree type
,
378 const value_range_base
&op1
,
379 const value_range_base
&op2
) const
382 if (empty_range_check (r
, op1
, op2
))
385 // We can be sure the values are always equal or not if both ranges
386 // consist of a single value, and then compare them.
387 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
388 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
390 if (wi::eq_p (op1
.lower_bound (), op2
.upper_bound()))
391 r
= range_true (type
);
393 r
= range_false (type
);
397 // If ranges do not intersect, we know the range is not equal,
398 // otherwise we don't know anything for sure.
399 r
= range_intersect (op1
, op2
);
400 if (r
.undefined_p ())
401 r
= range_false (type
);
403 r
= range_true_and_false (type
);
410 operator_equal::op1_range (value_range_base
&r
, tree type
,
411 const value_range_base
&lhs
,
412 const value_range_base
&op2
) const
414 switch (get_bool_state (r
, lhs
, type
))
417 // If the result is false, the only time we know anything is
418 // if OP2 is a constant.
419 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
420 r
= range_invert (op2
);
422 r
.set_varying (type
);
426 // If it's true, the result is the same as OP2.
437 operator_equal::op2_range (value_range_base
&r
, tree type
,
438 const value_range_base
&lhs
,
439 const value_range_base
&op1
) const
441 return operator_equal::op1_range (r
, type
, lhs
, op1
);
445 class operator_not_equal
: public range_operator
448 virtual value_range_base
fold_range (tree type
,
449 const value_range_base
&op1
,
450 const value_range_base
&op2
) const;
451 virtual bool op1_range (value_range_base
&r
, tree type
,
452 const value_range_base
&lhs
,
453 const value_range_base
&op2
) const;
454 virtual bool op2_range (value_range_base
&r
, tree type
,
455 const value_range_base
&lhs
,
456 const value_range_base
&op1
) const;
460 operator_not_equal::fold_range (tree type
,
461 const value_range_base
&op1
,
462 const value_range_base
&op2
) const
465 if (empty_range_check (r
, op1
, op2
))
468 // We can be sure the values are always equal or not if both ranges
469 // consist of a single value, and then compare them.
470 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
471 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
473 if (wi::ne_p (op1
.lower_bound (), op2
.upper_bound()))
474 r
= range_true (type
);
476 r
= range_false (type
);
480 // If ranges do not intersect, we know the range is not equal,
481 // otherwise we don't know anything for sure.
482 r
= range_intersect (op1
, op2
);
483 if (r
.undefined_p ())
484 r
= range_true (type
);
486 r
= range_true_and_false (type
);
493 operator_not_equal::op1_range (value_range_base
&r
, tree type
,
494 const value_range_base
&lhs
,
495 const value_range_base
&op2
) const
497 switch (get_bool_state (r
, lhs
, type
))
500 // If the result is true, the only time we know anything is if
501 // OP2 is a constant.
502 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
503 r
= range_invert (op2
);
505 r
.set_varying (type
);
509 // If its true, the result is the same as OP2.
521 operator_not_equal::op2_range (value_range_base
&r
, tree type
,
522 const value_range_base
&lhs
,
523 const value_range_base
&op1
) const
525 return operator_not_equal::op1_range (r
, type
, lhs
, op1
);
528 // (X < VAL) produces the range of [MIN, VAL - 1].
531 build_lt (value_range_base
&r
, tree type
, const wide_int
&val
)
533 wi::overflow_type ov
;
534 wide_int lim
= wi::sub (val
, 1, TYPE_SIGN (type
), &ov
);
536 // If val - 1 underflows, check if X < MIN, which is an empty range.
540 r
= value_range_base (type
, min_limit (type
), lim
);
543 // (X <= VAL) produces the range of [MIN, VAL].
546 build_le (value_range_base
&r
, tree type
, const wide_int
&val
)
548 r
= value_range_base (type
, min_limit (type
), val
);
551 // (X > VAL) produces the range of [VAL + 1, MAX].
554 build_gt (value_range_base
&r
, tree type
, const wide_int
&val
)
556 wi::overflow_type ov
;
557 wide_int lim
= wi::add (val
, 1, TYPE_SIGN (type
), &ov
);
558 // If val + 1 overflows, check is for X > MAX, which is an empty range.
562 r
= value_range_base (type
, lim
, max_limit (type
));
565 // (X >= val) produces the range of [VAL, MAX].
568 build_ge (value_range_base
&r
, tree type
, const wide_int
&val
)
570 r
= value_range_base (type
, val
, max_limit (type
));
574 class operator_lt
: public range_operator
577 virtual value_range_base
fold_range (tree type
,
578 const value_range_base
&op1
,
579 const value_range_base
&op2
) const;
580 virtual bool op1_range (value_range_base
&r
, tree type
,
581 const value_range_base
&lhs
,
582 const value_range_base
&op2
) const;
583 virtual bool op2_range (value_range_base
&r
, tree type
,
584 const value_range_base
&lhs
,
585 const value_range_base
&op1
) const;
589 operator_lt::fold_range (tree type
,
590 const value_range_base
&op1
,
591 const value_range_base
&op2
) const
594 if (empty_range_check (r
, op1
, op2
))
597 signop sign
= TYPE_SIGN (op1
.type ());
598 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
600 if (wi::lt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
601 r
= range_true (type
);
602 else if (!wi::lt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
603 r
= range_false (type
);
605 r
= range_true_and_false (type
);
610 operator_lt::op1_range (value_range_base
&r
, tree type
,
611 const value_range_base
&lhs
,
612 const value_range_base
&op2
) const
614 switch (get_bool_state (r
, lhs
, type
))
617 build_lt (r
, type
, op2
.upper_bound ());
621 build_ge (r
, type
, op2
.lower_bound ());
631 operator_lt::op2_range (value_range_base
&r
, tree type
,
632 const value_range_base
&lhs
,
633 const value_range_base
&op1
) const
635 switch (get_bool_state (r
, lhs
, type
))
638 build_le (r
, type
, op1
.upper_bound ());
642 build_gt (r
, type
, op1
.lower_bound ());
652 class operator_le
: public range_operator
655 virtual value_range_base
fold_range (tree type
,
656 const value_range_base
&op1
,
657 const value_range_base
&op2
) const;
658 virtual bool op1_range (value_range_base
&r
, tree type
,
659 const value_range_base
&lhs
,
660 const value_range_base
&op2
) const;
661 virtual bool op2_range (value_range_base
&r
, tree type
,
662 const value_range_base
&lhs
,
663 const value_range_base
&op1
) const;
667 operator_le::fold_range (tree type
,
668 const value_range_base
&op1
,
669 const value_range_base
&op2
) const
672 if (empty_range_check (r
, op1
, op2
))
675 signop sign
= TYPE_SIGN (op1
.type ());
676 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
678 if (wi::le_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
679 r
= range_true (type
);
680 else if (!wi::le_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
681 r
= range_false (type
);
683 r
= range_true_and_false (type
);
688 operator_le::op1_range (value_range_base
&r
, tree type
,
689 const value_range_base
&lhs
,
690 const value_range_base
&op2
) const
692 switch (get_bool_state (r
, lhs
, type
))
695 build_le (r
, type
, op2
.upper_bound ());
699 build_gt (r
, type
, op2
.lower_bound ());
709 operator_le::op2_range (value_range_base
&r
, tree type
,
710 const value_range_base
&lhs
,
711 const value_range_base
&op1
) const
713 switch (get_bool_state (r
, lhs
, type
))
716 build_lt (r
, type
, op1
.upper_bound ());
720 build_ge (r
, type
, op1
.lower_bound ());
730 class operator_gt
: public range_operator
733 virtual value_range_base
fold_range (tree type
,
734 const value_range_base
&op1
,
735 const value_range_base
&op2
) const;
736 virtual bool op1_range (value_range_base
&r
, tree type
,
737 const value_range_base
&lhs
,
738 const value_range_base
&op2
) const;
739 virtual bool op2_range (value_range_base
&r
, tree type
,
740 const value_range_base
&lhs
,
741 const value_range_base
&op1
) const;
745 operator_gt::fold_range (tree type
,
746 const value_range_base
&op1
,
747 const value_range_base
&op2
) const
750 if (empty_range_check (r
, op1
, op2
))
753 signop sign
= TYPE_SIGN (op1
.type ());
754 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
756 if (wi::gt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
757 r
= range_true (type
);
758 else if (!wi::gt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
759 r
= range_false (type
);
761 r
= range_true_and_false (type
);
766 operator_gt::op1_range (value_range_base
&r
, tree type
,
767 const value_range_base
&lhs
,
768 const value_range_base
&op2
) const
770 switch (get_bool_state (r
, lhs
, type
))
773 build_gt (r
, type
, op2
.lower_bound ());
777 build_le (r
, type
, op2
.upper_bound ());
787 operator_gt::op2_range (value_range_base
&r
, tree type
,
788 const value_range_base
&lhs
,
789 const value_range_base
&op1
) const
791 switch (get_bool_state (r
, lhs
, type
))
794 build_ge (r
, type
, op1
.lower_bound ());
798 build_lt (r
, type
, op1
.upper_bound ());
808 class operator_ge
: public range_operator
811 virtual value_range_base
fold_range (tree type
,
812 const value_range_base
&op1
,
813 const value_range_base
&op2
) const;
814 virtual bool op1_range (value_range_base
&r
, tree type
,
815 const value_range_base
&lhs
,
816 const value_range_base
&op2
) const;
817 virtual bool op2_range (value_range_base
&r
, tree type
,
818 const value_range_base
&lhs
,
819 const value_range_base
&op1
) const;
823 operator_ge::fold_range (tree type
,
824 const value_range_base
&op1
,
825 const value_range_base
&op2
) const
828 if (empty_range_check (r
, op1
, op2
))
831 signop sign
= TYPE_SIGN (op1
.type ());
832 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
834 if (wi::ge_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
835 r
= range_true (type
);
836 else if (!wi::ge_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
837 r
= range_false (type
);
839 r
= range_true_and_false (type
);
844 operator_ge::op1_range (value_range_base
&r
, tree type
,
845 const value_range_base
&lhs
,
846 const value_range_base
&op2
) const
848 switch (get_bool_state (r
, lhs
, type
))
851 build_ge (r
, type
, op2
.lower_bound ());
855 build_lt (r
, type
, op2
.upper_bound ());
865 operator_ge::op2_range (value_range_base
&r
, tree type
,
866 const value_range_base
&lhs
,
867 const value_range_base
&op1
) const
869 switch (get_bool_state (r
, lhs
, type
))
872 build_gt (r
, type
, op1
.lower_bound ());
876 build_le (r
, type
, op1
.upper_bound ());
886 class operator_plus
: public range_operator
889 virtual bool op1_range (value_range_base
&r
, tree type
,
890 const value_range_base
&lhs
,
891 const value_range_base
&op2
) const;
892 virtual bool op2_range (value_range_base
&r
, tree type
,
893 const value_range_base
&lhs
,
894 const value_range_base
&op1
) const;
895 virtual value_range_base
wi_fold (tree type
,
896 const wide_int
&lh_lb
,
897 const wide_int
&lh_ub
,
898 const wide_int
&rh_lb
,
899 const wide_int
&rh_ub
) const;
903 operator_plus::wi_fold (tree type
,
904 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
905 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
907 wi::overflow_type ov_lb
, ov_ub
;
908 signop s
= TYPE_SIGN (type
);
909 wide_int new_lb
= wi::add (lh_lb
, rh_lb
, s
, &ov_lb
);
910 wide_int new_ub
= wi::add (lh_ub
, rh_ub
, s
, &ov_ub
);
911 return value_range_with_overflow (type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
915 operator_plus::op1_range (value_range_base
&r
, tree type
,
916 const value_range_base
&lhs
,
917 const value_range_base
&op2
) const
919 r
= range_op_handler (MINUS_EXPR
, type
)->fold_range (type
, lhs
, op2
);
924 operator_plus::op2_range (value_range_base
&r
, tree type
,
925 const value_range_base
&lhs
,
926 const value_range_base
&op1
) const
928 r
= range_op_handler (MINUS_EXPR
, type
)->fold_range (type
, lhs
, op1
);
933 class operator_minus
: public range_operator
936 virtual bool op1_range (value_range_base
&r
, tree type
,
937 const value_range_base
&lhs
,
938 const value_range_base
&op2
) const;
939 virtual bool op2_range (value_range_base
&r
, tree type
,
940 const value_range_base
&lhs
,
941 const value_range_base
&op1
) const;
942 virtual value_range_base
wi_fold (tree type
,
943 const wide_int
&lh_lb
,
944 const wide_int
&lh_ub
,
945 const wide_int
&rh_lb
,
946 const wide_int
&rh_ub
) const;
950 operator_minus::wi_fold (tree type
,
951 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
952 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
954 wi::overflow_type ov_lb
, ov_ub
;
955 signop s
= TYPE_SIGN (type
);
956 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
957 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
958 return value_range_with_overflow (type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
962 operator_minus::op1_range (value_range_base
&r
, tree type
,
963 const value_range_base
&lhs
,
964 const value_range_base
&op2
) const
966 r
= range_op_handler (PLUS_EXPR
, type
)->fold_range (type
, lhs
, op2
);
971 operator_minus::op2_range (value_range_base
&r
, tree type
,
972 const value_range_base
&lhs
,
973 const value_range_base
&op1
) const
975 r
= fold_range (type
, op1
, lhs
);
980 class operator_min
: public range_operator
983 virtual value_range_base
wi_fold (tree type
,
984 const wide_int
&lh_lb
,
985 const wide_int
&lh_ub
,
986 const wide_int
&rh_lb
,
987 const wide_int
&rh_ub
) const;
991 operator_min::wi_fold (tree type
,
992 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
993 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
995 signop s
= TYPE_SIGN (type
);
996 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
997 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
998 return value_range_with_overflow (type
, new_lb
, new_ub
);
1002 class operator_max
: public range_operator
1005 virtual value_range_base
wi_fold (tree type
,
1006 const wide_int
&lh_lb
,
1007 const wide_int
&lh_ub
,
1008 const wide_int
&rh_lb
,
1009 const wide_int
&rh_ub
) const;
1013 operator_max::wi_fold (tree type
,
1014 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1015 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1017 signop s
= TYPE_SIGN (type
);
1018 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
1019 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
1020 return value_range_with_overflow (type
, new_lb
, new_ub
);
1024 class cross_product_operator
: public range_operator
1027 // Perform an operation between two wide-ints and place the result
1028 // in R. Return true if the operation overflowed.
1029 virtual bool wi_op_overflows (wide_int
&r
,
1032 const wide_int
&) const = 0;
1034 // Calculate the cross product of two sets of sub-ranges and return it.
1035 value_range_base
wi_cross_product (tree type
,
1036 const wide_int
&lh_lb
,
1037 const wide_int
&lh_ub
,
1038 const wide_int
&rh_lb
,
1039 const wide_int
&rh_ub
) const;
1042 // Calculate the cross product of two sets of ranges and return it.
1044 // Multiplications, divisions and shifts are a bit tricky to handle,
1045 // depending on the mix of signs we have in the two ranges, we need to
1046 // operate on different values to get the minimum and maximum values
1047 // for the new range. One approach is to figure out all the
1048 // variations of range combinations and do the operations.
1050 // However, this involves several calls to compare_values and it is
1051 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1052 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1053 // figure the smallest and largest values to form the new range.
1056 cross_product_operator::wi_cross_product (tree type
,
1057 const wide_int
&lh_lb
,
1058 const wide_int
&lh_ub
,
1059 const wide_int
&rh_lb
,
1060 const wide_int
&rh_ub
) const
1062 wide_int cp1
, cp2
, cp3
, cp4
;
1064 // Compute the 4 cross operations, bailing if we get an overflow we
1066 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
1067 return value_range_base (type
);
1068 if (wi::eq_p (lh_lb
, lh_ub
))
1070 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
1071 return value_range_base (type
);
1072 if (wi::eq_p (rh_lb
, rh_ub
))
1074 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
1075 return value_range_base (type
);
1076 if (wi::eq_p (lh_lb
, lh_ub
))
1078 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
1079 return value_range_base (type
);
1082 signop sign
= TYPE_SIGN (type
);
1083 if (wi::gt_p (cp1
, cp2
, sign
))
1084 std::swap (cp1
, cp2
);
1085 if (wi::gt_p (cp3
, cp4
, sign
))
1086 std::swap (cp3
, cp4
);
1088 // Choose min and max from the ordered pairs.
1089 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
1090 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
1091 return value_range_with_overflow (type
, res_lb
, res_ub
);
1095 class operator_mult
: public cross_product_operator
1098 virtual value_range_base
wi_fold (tree type
,
1099 const wide_int
&lh_lb
,
1100 const wide_int
&lh_ub
,
1101 const wide_int
&rh_lb
,
1102 const wide_int
&rh_ub
) const;
1103 virtual bool wi_op_overflows (wide_int
&res
,
1106 const wide_int
&w1
) const;
1110 operator_mult::wi_op_overflows (wide_int
&res
,
1113 const wide_int
&w1
) const
1115 wi::overflow_type overflow
= wi::OVF_NONE
;
1116 signop sign
= TYPE_SIGN (type
);
1117 res
= wi::mul (w0
, w1
, sign
, &overflow
);
1118 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1120 // For multiplication, the sign of the overflow is given
1121 // by the comparison of the signs of the operands.
1122 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
1123 res
= wi::max_value (w0
.get_precision (), sign
);
1125 res
= wi::min_value (w0
.get_precision (), sign
);
1132 operator_mult::wi_fold (tree type
,
1133 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1134 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1136 if (TYPE_OVERFLOW_UNDEFINED (type
))
1137 return wi_cross_product (type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1139 // Multiply the ranges when overflow wraps. This is basically fancy
1140 // code so we don't drop to varying with an unsigned
1143 // This test requires 2*prec bits if both operands are signed and
1144 // 2*prec + 2 bits if either is not. Therefore, extend the values
1145 // using the sign of the result to PREC2. From here on out,
1146 // everthing is just signed math no matter what the input types
1149 signop sign
= TYPE_SIGN (type
);
1150 unsigned prec
= TYPE_PRECISION (type
);
1151 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
1152 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
1153 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
1154 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
1155 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
1156 widest2_int size
= sizem1
+ 1;
1158 // Canonicalize the intervals.
1159 if (sign
== UNSIGNED
)
1161 if (wi::ltu_p (size
, min0
+ max0
))
1166 if (wi::ltu_p (size
, min1
+ max1
))
1173 // Sort the 4 products so that min is in prod0 and max is in
1175 widest2_int prod0
= min0
* min1
;
1176 widest2_int prod1
= min0
* max1
;
1177 widest2_int prod2
= max0
* min1
;
1178 widest2_int prod3
= max0
* max1
;
1180 // min0min1 > max0max1
1182 std::swap (prod0
, prod3
);
1184 // min0max1 > max0min1
1186 std::swap (prod1
, prod2
);
1189 std::swap (prod0
, prod1
);
1192 std::swap (prod2
, prod3
);
1195 prod2
= prod3
- prod0
;
1196 if (wi::geu_p (prod2
, sizem1
))
1197 // The range covers all values.
1198 return value_range_base (type
);
1200 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
1201 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
1202 return create_possibly_reversed_range (type
, new_lb
, new_ub
);
1206 class operator_div
: public cross_product_operator
1209 operator_div (enum tree_code c
) { code
= c
; }
1210 virtual value_range_base
wi_fold (tree type
,
1211 const wide_int
&lh_lb
,
1212 const wide_int
&lh_ub
,
1213 const wide_int
&rh_lb
,
1214 const wide_int
&rh_ub
) const;
1215 virtual bool wi_op_overflows (wide_int
&res
,
1218 const wide_int
&) const;
1220 enum tree_code code
;
1224 operator_div::wi_op_overflows (wide_int
&res
,
1227 const wide_int
&w1
) const
1232 wi::overflow_type overflow
= wi::OVF_NONE
;
1233 signop sign
= TYPE_SIGN (type
);
1237 case EXACT_DIV_EXPR
:
1238 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1239 // operator_exact_divide. No need to handle it here.
1242 case TRUNC_DIV_EXPR
:
1243 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
1245 case FLOOR_DIV_EXPR
:
1246 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
1248 case ROUND_DIV_EXPR
:
1249 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
1252 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
1258 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1260 // For division, the only case is -INF / -1 = +INF.
1261 res
= wi::max_value (w0
.get_precision (), sign
);
1268 operator_div::wi_fold (tree type
,
1269 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1270 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1272 // If we know we will divide by zero, return undefined.
1273 if (rh_lb
== 0 && rh_ub
== 0)
1274 return value_range_base ();
1276 const wide_int dividend_min
= lh_lb
;
1277 const wide_int dividend_max
= lh_ub
;
1278 const wide_int divisor_min
= rh_lb
;
1279 const wide_int divisor_max
= rh_ub
;
1280 signop sign
= TYPE_SIGN (type
);
1281 unsigned prec
= TYPE_PRECISION (type
);
1282 wide_int extra_min
, extra_max
;
1284 // If we know we won't divide by zero, just do the division.
1285 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
1286 return wi_cross_product (type
, dividend_min
, dividend_max
,
1287 divisor_min
, divisor_max
);
1289 // If flag_non_call_exceptions, we must not eliminate a division by zero.
1290 if (cfun
->can_throw_non_call_exceptions
)
1291 return value_range_base (type
);
1293 // If we're definitely dividing by zero, there's nothing to do.
1294 if (wi_zero_p (type
, divisor_min
, divisor_max
))
1295 return value_range_base ();
1297 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1298 // skip any division by zero.
1300 // First divide by the negative numbers, if any.
1302 if (wi::neg_p (divisor_min
, sign
))
1303 r
= wi_cross_product (type
, dividend_min
, dividend_max
,
1304 divisor_min
, wi::minus_one (prec
));
1305 // Then divide by the non-zero positive numbers, if any.
1306 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
1308 value_range_base tmp
;
1309 tmp
= wi_cross_product (type
, dividend_min
, dividend_max
,
1310 wi::one (prec
), divisor_max
);
1316 operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
1317 operator_div
op_floor_div(FLOOR_DIV_EXPR
);
1318 operator_div
op_round_div (ROUND_DIV_EXPR
);
1319 operator_div
op_ceil_div (CEIL_DIV_EXPR
);
1322 class operator_exact_divide
: public operator_div
1325 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR
) { }
1326 virtual bool op1_range (value_range_base
&r
, tree type
,
1327 const value_range_base
&lhs
,
1328 const value_range_base
&op2
) const;
1333 operator_exact_divide::op1_range (value_range_base
&r
, tree type
,
1334 const value_range_base
&lhs
,
1335 const value_range_base
&op2
) const
1338 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1339 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1340 // We wont bother trying to enumerate all the in between stuff :-P
1341 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1342 // the time however.
1343 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1344 if (op2
.singleton_p (&offset
)
1345 && !integer_zerop (offset
))
1347 r
= range_op_handler (MULT_EXPR
, type
)->fold_range (type
, lhs
, op2
);
1354 class operator_lshift
: public cross_product_operator
1357 virtual value_range_base
fold_range (tree type
,
1358 const value_range_base
&op1
,
1359 const value_range_base
&op2
) const;
1361 virtual value_range_base
wi_fold (tree type
,
1362 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1363 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
1364 virtual bool wi_op_overflows (wide_int
&res
,
1367 const wide_int
&) const;
1371 operator_lshift::fold_range (tree type
,
1372 const value_range_base
&op1
,
1373 const value_range_base
&op2
) const
1376 if (undefined_shift_range_check (r
, type
, op2
))
1379 // Transform left shifts by constants into multiplies.
1380 if (op2
.singleton_p ())
1382 unsigned shift
= op2
.lower_bound ().to_uhwi ();
1383 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
1384 value_range_base
mult (type
, tmp
, tmp
);
1386 // Force wrapping multiplication.
1387 bool saved_flag_wrapv
= flag_wrapv
;
1388 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
1390 flag_wrapv_pointer
= 1;
1391 r
= range_op_handler (MULT_EXPR
, type
)->fold_range (type
, op1
, mult
);
1392 flag_wrapv
= saved_flag_wrapv
;
1393 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
1397 // Otherwise, invoke the generic fold routine.
1398 return range_operator::fold_range (type
, op1
, op2
);
1402 operator_lshift::wi_fold (tree type
,
1403 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1404 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1406 signop sign
= TYPE_SIGN (type
);
1407 unsigned prec
= TYPE_PRECISION (type
);
1408 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
1409 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
1410 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1411 // overflow. However, for that to happen, rh.max needs to be zero,
1412 // which means rh is a singleton range of zero, which means it
1413 // should be handled by the lshift fold_range above.
1414 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
1415 wide_int complement
= ~(bound
- 1);
1416 wide_int low_bound
, high_bound
;
1417 bool in_bounds
= false;
1419 if (sign
== UNSIGNED
)
1422 high_bound
= complement
;
1423 if (wi::ltu_p (lh_ub
, low_bound
))
1425 // [5, 6] << [1, 2] == [10, 24].
1426 // We're shifting out only zeroes, the value increases
1430 else if (wi::ltu_p (high_bound
, lh_lb
))
1432 // [0xffffff00, 0xffffffff] << [1, 2]
1433 // == [0xfffffc00, 0xfffffffe].
1434 // We're shifting out only ones, the value decreases
1441 // [-1, 1] << [1, 2] == [-4, 4]
1442 low_bound
= complement
;
1444 if (wi::lts_p (lh_ub
, high_bound
)
1445 && wi::lts_p (low_bound
, lh_lb
))
1447 // For non-negative numbers, we're shifting out only zeroes,
1448 // the value increases monotonically. For negative numbers,
1449 // we're shifting out only ones, the value decreases
1456 return wi_cross_product (type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1458 return value_range_base (type
);
1462 operator_lshift::wi_op_overflows (wide_int
&res
,
1465 const wide_int
&w1
) const
1467 signop sign
= TYPE_SIGN (type
);
1470 // It's unclear from the C standard whether shifts can overflow.
1471 // The following code ignores overflow; perhaps a C standard
1472 // interpretation ruling is needed.
1473 res
= wi::rshift (w0
, -w1
, sign
);
1476 res
= wi::lshift (w0
, w1
);
1481 class operator_rshift
: public cross_product_operator
1484 virtual value_range_base
fold_range (tree type
,
1485 const value_range_base
&op1
,
1486 const value_range_base
&op2
) const;
1487 virtual value_range_base
wi_fold (tree type
,
1488 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1489 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
1490 virtual bool wi_op_overflows (wide_int
&res
,
1493 const wide_int
&w1
) const;
1497 operator_rshift::wi_op_overflows (wide_int
&res
,
1500 const wide_int
&w1
) const
1502 signop sign
= TYPE_SIGN (type
);
1504 res
= wi::lshift (w0
, -w1
);
1507 // It's unclear from the C standard whether shifts can overflow.
1508 // The following code ignores overflow; perhaps a C standard
1509 // interpretation ruling is needed.
1510 res
= wi::rshift (w0
, w1
, sign
);
1516 operator_rshift::fold_range (tree type
,
1517 const value_range_base
&op1
,
1518 const value_range_base
&op2
) const
1521 if (undefined_shift_range_check (r
, type
, op2
))
1524 // Otherwise, invoke the generic fold routine.
1525 return range_operator::fold_range (type
, op1
, op2
);
1529 operator_rshift::wi_fold (tree type
,
1530 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1531 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1533 return wi_cross_product (type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1537 class operator_cast
: public range_operator
1540 virtual value_range_base
fold_range (tree type
,
1541 const value_range_base
&op1
,
1542 const value_range_base
&op2
) const;
1543 virtual bool op1_range (value_range_base
&r
, tree type
,
1544 const value_range_base
&lhs
,
1545 const value_range_base
&op2
) const;
1550 operator_cast::fold_range (tree type ATTRIBUTE_UNUSED
,
1551 const value_range_base
&lh
,
1552 const value_range_base
&rh
) const
1555 if (empty_range_check (r
, lh
, rh
))
1558 tree inner
= lh
.type ();
1559 tree outer
= rh
.type ();
1560 gcc_checking_assert (rh
.varying_p ());
1561 gcc_checking_assert (types_compatible_p (outer
, type
));
1562 signop inner_sign
= TYPE_SIGN (inner
);
1563 signop outer_sign
= TYPE_SIGN (outer
);
1564 unsigned inner_prec
= TYPE_PRECISION (inner
);
1565 unsigned outer_prec
= TYPE_PRECISION (outer
);
1567 for (unsigned x
= 0; x
< lh
.num_pairs (); ++x
)
1569 wide_int lh_lb
= lh
.lower_bound (x
);
1570 wide_int lh_ub
= lh
.upper_bound (x
);
1572 // If the conversion is not truncating we can convert the min
1573 // and max values and canonicalize the resulting range.
1574 // Otherwise, we can do the conversion if the size of the range
1575 // is less than what the precision of the target type can
1577 if (outer_prec
>= inner_prec
1578 || wi::rshift (wi::sub (lh_ub
, lh_lb
),
1579 wi::uhwi (outer_prec
, inner_prec
),
1582 wide_int min
= wide_int::from (lh_lb
, outer_prec
, inner_sign
);
1583 wide_int max
= wide_int::from (lh_ub
, outer_prec
, inner_sign
);
1584 if (!wi::eq_p (min
, wi::min_value (outer_prec
, outer_sign
))
1585 || !wi::eq_p (max
, wi::max_value (outer_prec
, outer_sign
)))
1587 value_range_base tmp
;
1588 tmp
= create_possibly_reversed_range (type
, min
, max
);
1593 return value_range_base (type
);
1599 operator_cast::op1_range (value_range_base
&r
, tree type
,
1600 const value_range_base
&lhs
,
1601 const value_range_base
&op2
) const
1603 tree lhs_type
= lhs
.type ();
1604 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
1606 // If the precision of the LHS is smaller than the precision of the
1607 // RHS, then there would be truncation of the value on the RHS, and
1608 // so we can tell nothing about it.
1609 if (TYPE_PRECISION (lhs_type
) < TYPE_PRECISION (type
))
1611 // If we've been passed an actual value for the RHS rather than
1612 // the type, see if it fits the LHS, and if so, then we can allow
1615 r
= fold_range (lhs_type
, r
, value_range_base (lhs_type
));
1616 r
= fold_range (type
, r
, value_range_base (type
));
1619 // We know the value of the RHS fits in the LHS type, so
1620 // convert the LHS and remove any values that arent in OP2.
1622 r
= fold_range (type
, r
, value_range_base (type
));
1626 // Special case if the LHS is a boolean. A 0 means the RHS is
1627 // zero, and a 1 means the RHS is non-zero.
1628 if (TREE_CODE (lhs_type
) == BOOLEAN_TYPE
)
1630 // If the LHS is unknown, the result is whatever op2 already is.
1631 if (!lhs
.singleton_p ())
1636 // Boolean casts are weird in GCC. It's actually an implied
1637 // mask with 0x01, so all that is known is whether the
1638 // rightmost bit is 0 or 1, which implies the only value
1639 // *not* in the RHS is 0 or -1.
1640 unsigned prec
= TYPE_PRECISION (type
);
1642 r
= value_range_base (VR_ANTI_RANGE
, type
,
1643 wi::minus_one (prec
), wi::minus_one (prec
));
1645 r
= value_range_base (VR_ANTI_RANGE
, type
,
1646 wi::zero (prec
), wi::zero (prec
));
1647 // And intersect it with what we know about op2.
1651 // Otherwise we'll have to assume it's whatever we know about op2.
1656 // If the LHS precision is greater than the rhs precision, the LHS
1657 // range is restricted to the range of the RHS by this
1659 if (TYPE_PRECISION (lhs_type
) > TYPE_PRECISION (type
))
1661 // Cast the range of the RHS to the type of the LHS.
1662 value_range_base
op_type (type
);
1663 op_type
= fold_range (lhs_type
, op_type
, value_range_base (lhs_type
));
1665 // Intersect this with the LHS range will produce the RHS range.
1666 r
= range_intersect (lhs
, op_type
);
1671 // Cast the calculated range to the type of the RHS.
1672 r
= fold_range (type
, r
, value_range_base (type
));
1677 class operator_logical_and
: public range_operator
1680 virtual value_range_base
fold_range (tree type
,
1681 const value_range_base
&lh
,
1682 const value_range_base
&rh
) const;
1683 virtual bool op1_range (value_range_base
&r
, tree type
,
1684 const value_range_base
&lhs
,
1685 const value_range_base
&op2
) const;
1686 virtual bool op2_range (value_range_base
&r
, tree type
,
1687 const value_range_base
&lhs
,
1688 const value_range_base
&op1
) const;
1693 operator_logical_and::fold_range (tree type
,
1694 const value_range_base
&lh
,
1695 const value_range_base
&rh
) const
1698 if (empty_range_check (r
, lh
, rh
))
1701 // 0 && anything is 0.
1702 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
1703 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
1704 return range_false (type
);
1706 // To reach this point, there must be a logical 1 on each side, and
1707 // the only remaining question is whether there is a zero or not.
1708 if (lh
.contains_p (build_zero_cst (lh
.type ()))
1709 || rh
.contains_p (build_zero_cst (rh
.type ())))
1710 return range_true_and_false (type
);
1712 return range_true (type
);
1716 operator_logical_and::op1_range (value_range_base
&r
, tree type
,
1717 const value_range_base
&lhs
,
1718 const value_range_base
&op2 ATTRIBUTE_UNUSED
) const
1720 switch (get_bool_state (r
, lhs
, type
))
1723 // A true result means both sides of the AND must be true.
1724 r
= range_true (type
);
1727 // Any other result means only one side has to be false, the
1728 // other side can be anything. So we cannott be sure of any
1730 r
= range_true_and_false (type
);
1737 operator_logical_and::op2_range (value_range_base
&r
, tree type
,
1738 const value_range_base
&lhs
,
1739 const value_range_base
&op1
) const
1741 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
1745 class operator_bitwise_and
: public range_operator
1748 virtual bool op1_range (value_range_base
&r
, tree type
,
1749 const value_range_base
&lhs
,
1750 const value_range_base
&op2
) const;
1751 virtual bool op2_range (value_range_base
&r
, tree type
,
1752 const value_range_base
&lhs
,
1753 const value_range_base
&op1
) const;
1754 virtual value_range_base
wi_fold (tree type
,
1755 const wide_int
&lh_lb
,
1756 const wide_int
&lh_ub
,
1757 const wide_int
&rh_lb
,
1758 const wide_int
&rh_ub
) const;
1761 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
1762 // possible. Basically, see if we can optimize:
1766 // [LB op Z, UB op Z]
1768 // If the optimization was successful, accumulate the range in R and
1772 wi_optimize_and_or (value_range_base
&r
,
1773 enum tree_code code
,
1775 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1776 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
1778 // Calculate the singleton mask among the ranges, if any.
1779 wide_int lower_bound
, upper_bound
, mask
;
1780 if (wi::eq_p (rh_lb
, rh_ub
))
1783 lower_bound
= lh_lb
;
1784 upper_bound
= lh_ub
;
1786 else if (wi::eq_p (lh_lb
, lh_ub
))
1789 lower_bound
= rh_lb
;
1790 upper_bound
= rh_ub
;
1795 // If Z is a constant which (for op | its bitwise not) has n
1796 // consecutive least significant bits cleared followed by m 1
1797 // consecutive bits set immediately above it and either
1798 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
1800 // The least significant n bits of all the values in the range are
1801 // cleared or set, the m bits above it are preserved and any bits
1802 // above these are required to be the same for all values in the
1806 if (code
== BIT_IOR_EXPR
)
1808 if (wi::eq_p (w
, 0))
1809 n
= w
.get_precision ();
1813 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
1814 if (wi::eq_p (w
, 0))
1815 m
= w
.get_precision () - n
;
1817 m
= wi::ctz (w
) - n
;
1819 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
1820 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
1823 wide_int res_lb
, res_ub
;
1824 if (code
== BIT_AND_EXPR
)
1826 res_lb
= wi::bit_and (lower_bound
, mask
);
1827 res_ub
= wi::bit_and (upper_bound
, mask
);
1829 else if (code
== BIT_IOR_EXPR
)
1831 res_lb
= wi::bit_or (lower_bound
, mask
);
1832 res_ub
= wi::bit_or (upper_bound
, mask
);
1836 r
= value_range_with_overflow (type
, res_lb
, res_ub
);
1840 // For range [LB, UB] compute two wide_int bit masks.
1842 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
1843 // for all numbers in the range the bit is 0, otherwise it might be 0
1846 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
1847 // for all numbers in the range the bit is 1, otherwise it might be 0
1851 wi_set_zero_nonzero_bits (tree type
,
1852 const wide_int
&lb
, const wide_int
&ub
,
1853 wide_int
&maybe_nonzero
,
1854 wide_int
&mustbe_nonzero
)
1856 signop sign
= TYPE_SIGN (type
);
1858 if (wi::eq_p (lb
, ub
))
1859 maybe_nonzero
= mustbe_nonzero
= lb
;
1860 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
1862 wide_int xor_mask
= lb
^ ub
;
1863 maybe_nonzero
= lb
| ub
;
1864 mustbe_nonzero
= lb
& ub
;
1867 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1868 maybe_nonzero
.get_precision ());
1869 maybe_nonzero
= maybe_nonzero
| mask
;
1870 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
1875 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
1876 mustbe_nonzero
= wi::zero (lb
.get_precision ());
1881 operator_bitwise_and::wi_fold (tree type
,
1882 const wide_int
&lh_lb
,
1883 const wide_int
&lh_ub
,
1884 const wide_int
&rh_lb
,
1885 const wide_int
&rh_ub
) const
1888 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
1891 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
1892 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
1893 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
1894 maybe_nonzero_lh
, mustbe_nonzero_lh
);
1895 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
1896 maybe_nonzero_rh
, mustbe_nonzero_rh
);
1898 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
1899 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
1900 signop sign
= TYPE_SIGN (type
);
1901 unsigned prec
= TYPE_PRECISION (type
);
1902 // If both input ranges contain only negative values, we can
1903 // truncate the result range maximum to the minimum of the
1904 // input range maxima.
1905 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
1907 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
1908 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
1910 // If either input range contains only non-negative values
1911 // we can truncate the result range maximum to the respective
1912 // maximum of the input range.
1913 if (wi::ge_p (lh_lb
, 0, sign
))
1914 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
1915 if (wi::ge_p (rh_lb
, 0, sign
))
1916 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
1917 // PR68217: In case of signed & sign-bit-CST should
1918 // result in [-INF, 0] instead of [-INF, INF].
1919 if (wi::gt_p (new_lb
, new_ub
, sign
))
1921 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
1923 && ((wi::eq_p (lh_lb
, lh_ub
)
1924 && !wi::cmps (lh_lb
, sign_bit
))
1925 || (wi::eq_p (rh_lb
, rh_ub
)
1926 && !wi::cmps (rh_lb
, sign_bit
))))
1928 new_lb
= wi::min_value (prec
, sign
);
1929 new_ub
= wi::zero (prec
);
1932 // If the limits got swapped around, return varying.
1933 if (wi::gt_p (new_lb
, new_ub
,sign
))
1934 return value_range_base (type
);
1936 return value_range_with_overflow (type
, new_lb
, new_ub
);
1940 operator_bitwise_and::op1_range (value_range_base
&r
, tree type
,
1941 const value_range_base
&lhs
,
1942 const value_range_base
&op2
) const
1944 // If this is really a logical wi_fold, call that.
1945 if (types_compatible_p (type
, boolean_type_node
))
1946 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
1948 // For now do nothing with bitwise AND of value_range's.
1949 r
.set_varying (type
);
1954 operator_bitwise_and::op2_range (value_range_base
&r
, tree type
,
1955 const value_range_base
&lhs
,
1956 const value_range_base
&op1
) const
1958 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
1962 class operator_logical_or
: public range_operator
1965 virtual value_range_base
fold_range (tree type
,
1966 const value_range_base
&lh
,
1967 const value_range_base
&rh
) const;
1968 virtual bool op1_range (value_range_base
&r
, tree type
,
1969 const value_range_base
&lhs
,
1970 const value_range_base
&op2
) const;
1971 virtual bool op2_range (value_range_base
&r
, tree type
,
1972 const value_range_base
&lhs
,
1973 const value_range_base
&op1
) const;
1977 operator_logical_or::fold_range (tree type ATTRIBUTE_UNUSED
,
1978 const value_range_base
&lh
,
1979 const value_range_base
&rh
) const
1982 if (empty_range_check (r
, lh
, rh
))
1985 return range_union (lh
, rh
);
1989 operator_logical_or::op1_range (value_range_base
&r
, tree type
,
1990 const value_range_base
&lhs
,
1991 const value_range_base
&op2 ATTRIBUTE_UNUSED
) const
1993 switch (get_bool_state (r
, lhs
, type
))
1996 // A false result means both sides of the OR must be false.
1997 r
= range_false (type
);
2000 // Any other result means only one side has to be true, the
2001 // other side can be anything. so we can't be sure of any result
2003 r
= range_true_and_false (type
);
2010 operator_logical_or::op2_range (value_range_base
&r
, tree type
,
2011 const value_range_base
&lhs
,
2012 const value_range_base
&op1
) const
2014 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
2018 class operator_bitwise_or
: public range_operator
2021 virtual bool op1_range (value_range_base
&r
, tree type
,
2022 const value_range_base
&lhs
,
2023 const value_range_base
&op2
) const;
2024 virtual bool op2_range (value_range_base
&r
, tree type
,
2025 const value_range_base
&lhs
,
2026 const value_range_base
&op1
) const;
2027 virtual value_range_base
wi_fold (tree type
,
2028 const wide_int
&lh_lb
,
2029 const wide_int
&lh_ub
,
2030 const wide_int
&rh_lb
,
2031 const wide_int
&rh_ub
) const;
2035 operator_bitwise_or::wi_fold (tree type
,
2036 const wide_int
&lh_lb
,
2037 const wide_int
&lh_ub
,
2038 const wide_int
&rh_lb
,
2039 const wide_int
&rh_ub
) const
2042 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
2045 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
2046 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
2047 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
2048 maybe_nonzero_lh
, mustbe_nonzero_lh
);
2049 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
2050 maybe_nonzero_rh
, mustbe_nonzero_rh
);
2051 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
2052 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
2053 signop sign
= TYPE_SIGN (type
);
2054 // If the input ranges contain only positive values we can
2055 // truncate the minimum of the result range to the maximum
2056 // of the input range minima.
2057 if (wi::ge_p (lh_lb
, 0, sign
)
2058 && wi::ge_p (rh_lb
, 0, sign
))
2060 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
2061 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
2063 // If either input range contains only negative values
2064 // we can truncate the minimum of the result range to the
2065 // respective minimum range.
2066 if (wi::lt_p (lh_ub
, 0, sign
))
2067 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
2068 if (wi::lt_p (rh_ub
, 0, sign
))
2069 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
2070 // If the limits got swapped around, return varying.
2071 if (wi::gt_p (new_lb
, new_ub
,sign
))
2072 return value_range_base (type
);
2074 return value_range_with_overflow (type
, new_lb
, new_ub
);
2078 operator_bitwise_or::op1_range (value_range_base
&r
, tree type
,
2079 const value_range_base
&lhs
,
2080 const value_range_base
&op2
) const
2082 // If this is really a logical wi_fold, call that.
2083 if (types_compatible_p (type
, boolean_type_node
))
2084 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
2086 // For now do nothing with bitwise OR of value_range's.
2087 r
.set_varying (type
);
2092 operator_bitwise_or::op2_range (value_range_base
&r
, tree type
,
2093 const value_range_base
&lhs
,
2094 const value_range_base
&op1
) const
2096 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
2100 class operator_bitwise_xor
: public range_operator
2103 virtual value_range_base
wi_fold (tree type
,
2104 const wide_int
&lh_lb
,
2105 const wide_int
&lh_ub
,
2106 const wide_int
&rh_lb
,
2107 const wide_int
&rh_ub
) const;
2111 operator_bitwise_xor::wi_fold (tree type
,
2112 const wide_int
&lh_lb
,
2113 const wide_int
&lh_ub
,
2114 const wide_int
&rh_lb
,
2115 const wide_int
&rh_ub
) const
2117 signop sign
= TYPE_SIGN (type
);
2118 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
2119 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
2120 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
2121 maybe_nonzero_lh
, mustbe_nonzero_lh
);
2122 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
2123 maybe_nonzero_rh
, mustbe_nonzero_rh
);
2125 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
2126 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
2127 wide_int result_one_bits
2128 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
2129 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
2130 wide_int new_ub
= ~result_zero_bits
;
2131 wide_int new_lb
= result_one_bits
;
2133 // If the range has all positive or all negative values, the result
2134 // is better than VARYING.
2135 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
2136 return value_range_with_overflow (type
, new_lb
, new_ub
);
2138 return value_range_base (type
);
2142 class operator_trunc_mod
: public range_operator
2145 virtual value_range_base
wi_fold (tree type
,
2146 const wide_int
&lh_lb
,
2147 const wide_int
&lh_ub
,
2148 const wide_int
&rh_lb
,
2149 const wide_int
&rh_ub
) const;
2153 operator_trunc_mod::wi_fold (tree type
,
2154 const wide_int
&lh_lb
,
2155 const wide_int
&lh_ub
,
2156 const wide_int
&rh_lb
,
2157 const wide_int
&rh_ub
) const
2159 wide_int new_lb
, new_ub
, tmp
;
2160 signop sign
= TYPE_SIGN (type
);
2161 unsigned prec
= TYPE_PRECISION (type
);
2163 // Mod 0 is undefined. Return undefined.
2164 if (wi_zero_p (type
, rh_lb
, rh_ub
))
2165 return value_range_base ();
2167 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
2172 new_ub
= wi::smax (new_ub
, tmp
);
2175 if (sign
== UNSIGNED
)
2176 new_lb
= wi::zero (prec
);
2181 if (wi::gts_p (tmp
, 0))
2182 tmp
= wi::zero (prec
);
2183 new_lb
= wi::smax (new_lb
, tmp
);
2186 if (sign
== SIGNED
&& wi::neg_p (tmp
))
2187 tmp
= wi::zero (prec
);
2188 new_ub
= wi::min (new_ub
, tmp
, sign
);
2190 return value_range_with_overflow (type
, new_lb
, new_ub
);
2194 class operator_logical_not
: public range_operator
2197 virtual value_range_base
fold_range (tree type
,
2198 const value_range_base
&lh
,
2199 const value_range_base
&rh
) const;
2200 virtual bool op1_range (value_range_base
&r
, tree type
,
2201 const value_range_base
&lhs
,
2202 const value_range_base
&op2
) const;
2205 // Folding a logical NOT, oddly enough, involves doing nothing on the
2206 // forward pass through. During the initial walk backwards, the
2207 // logical NOT reversed the desired outcome on the way back, so on the
2208 // way forward all we do is pass the range forward.
2213 // to determine the TRUE branch, walking backward
2214 // if (b_3) if ([1,1])
2215 // b_3 = !b_2 [1,1] = ![0,0]
2216 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
2217 // which is the result we are looking for.. so.. pass it through.
2220 operator_logical_not::fold_range (tree type
,
2221 const value_range_base
&lh
,
2222 const value_range_base
&rh ATTRIBUTE_UNUSED
) const
2225 if (empty_range_check (r
, lh
, rh
))
2228 if (lh
.varying_p () || lh
.undefined_p ())
2231 r
= range_invert (lh
);
2232 gcc_checking_assert (lh
.type() == type
);
2237 operator_logical_not::op1_range (value_range_base
&r
,
2238 tree type ATTRIBUTE_UNUSED
,
2239 const value_range_base
&lhs
,
2240 const value_range_base
&op2 ATTRIBUTE_UNUSED
) const
2242 if (lhs
.varying_p () || lhs
.undefined_p ())
2245 r
= range_invert (lhs
);
2250 class operator_bitwise_not
: public range_operator
2253 virtual value_range_base
fold_range (tree type
,
2254 const value_range_base
&lh
,
2255 const value_range_base
&rh
) const;
2256 virtual bool op1_range (value_range_base
&r
, tree type
,
2257 const value_range_base
&lhs
,
2258 const value_range_base
&op2
) const;
2262 operator_bitwise_not::fold_range (tree type
,
2263 const value_range_base
&lh
,
2264 const value_range_base
&rh
) const
2267 if (empty_range_check (r
, lh
, rh
))
2270 // ~X is simply -1 - X.
2271 value_range_base
minusone (type
,
2272 wi::minus_one (TYPE_PRECISION (type
)),
2273 wi::minus_one (TYPE_PRECISION (type
)));
2274 r
= range_op_handler (MINUS_EXPR
, type
)->fold_range (type
, minusone
, lh
);
2279 operator_bitwise_not::op1_range (value_range_base
&r
, tree type
,
2280 const value_range_base
&lhs
,
2281 const value_range_base
&op2
) const
2283 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
2284 r
= fold_range (type
, lhs
, op2
);
2289 class operator_cst
: public range_operator
2292 virtual value_range_base
fold_range (tree type
,
2293 const value_range_base
&op1
,
2294 const value_range_base
&op2
) const;
2298 operator_cst::fold_range (tree type ATTRIBUTE_UNUSED
,
2299 const value_range_base
&lh
,
2300 const value_range_base
&rh ATTRIBUTE_UNUSED
) const
2306 class operator_identity
: public range_operator
2309 virtual value_range_base
fold_range (tree type
,
2310 const value_range_base
&op1
,
2311 const value_range_base
&op2
) const;
2312 virtual bool op1_range (value_range_base
&r
, tree type
,
2313 const value_range_base
&lhs
,
2314 const value_range_base
&op2
) const;
2318 operator_identity::fold_range (tree type ATTRIBUTE_UNUSED
,
2319 const value_range_base
&lh
,
2320 const value_range_base
&rh ATTRIBUTE_UNUSED
) const
2326 operator_identity::op1_range (value_range_base
&r
, tree type ATTRIBUTE_UNUSED
,
2327 const value_range_base
&lhs
,
2328 const value_range_base
&op2 ATTRIBUTE_UNUSED
) const
2335 class operator_abs
: public range_operator
2338 virtual value_range_base
wi_fold (tree type
,
2339 const wide_int
&lh_lb
,
2340 const wide_int
&lh_ub
,
2341 const wide_int
&rh_lb
,
2342 const wide_int
&rh_ub
) const;
2343 virtual bool op1_range (value_range_base
&r
, tree type
,
2344 const value_range_base
&lhs
,
2345 const value_range_base
&op2
) const;
2349 operator_abs::wi_fold (tree type
,
2350 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2351 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
2352 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
2355 signop sign
= TYPE_SIGN (type
);
2356 unsigned prec
= TYPE_PRECISION (type
);
2358 // Pass through LH for the easy cases.
2359 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
2360 return value_range_base (type
, lh_lb
, lh_ub
);
2362 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
2364 wide_int min_value
= wi::min_value (prec
, sign
);
2365 wide_int max_value
= wi::max_value (prec
, sign
);
2366 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
2367 return value_range_base (type
);
2369 // ABS_EXPR may flip the range around, if the original range
2370 // included negative values.
2371 if (wi::eq_p (lh_lb
, min_value
))
2374 min
= wi::abs (lh_lb
);
2375 if (wi::eq_p (lh_ub
, min_value
))
2378 max
= wi::abs (lh_ub
);
2380 // If the range contains zero then we know that the minimum value in the
2381 // range will be zero.
2382 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
2384 if (wi::gt_p (min
, max
, sign
))
2386 min
= wi::zero (prec
);
2390 // If the range was reversed, swap MIN and MAX.
2391 if (wi::gt_p (min
, max
, sign
))
2392 std::swap (min
, max
);
2395 // If the new range has its limits swapped around (MIN > MAX), then
2396 // the operation caused one of them to wrap around. The only thing
2397 // we know is that the result is positive.
2398 if (wi::gt_p (min
, max
, sign
))
2400 min
= wi::zero (prec
);
2403 return value_range_base (type
, min
, max
);
2407 operator_abs::op1_range (value_range_base
&r
, tree type
,
2408 const value_range_base
&lhs
,
2409 const value_range_base
&op2
) const
2411 if (empty_range_check (r
, lhs
, op2
))
2413 if (TYPE_UNSIGNED (type
))
2418 // Start with the positives because negatives are an impossible result.
2419 value_range_base positives
= range_positives (type
);
2420 positives
.intersect (lhs
);
2422 // Then add the negative of each pair:
2423 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
2424 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
2425 r
.union_ (value_range_base (type
,
2426 -positives
.upper_bound (i
),
2427 -positives
.lower_bound (i
)));
2432 class operator_absu
: public range_operator
2435 virtual value_range_base
wi_fold (tree type
,
2436 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2437 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
2441 operator_absu::wi_fold (tree type
,
2442 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2443 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
2444 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
2446 wide_int new_lb
, new_ub
;
2448 // Pass through VR0 the easy cases.
2449 if (wi::ges_p (lh_lb
, 0))
2456 new_lb
= wi::abs (lh_lb
);
2457 new_ub
= wi::abs (lh_ub
);
2459 // If the range contains zero then we know that the minimum
2460 // value in the range will be zero.
2461 if (wi::ges_p (lh_ub
, 0))
2463 if (wi::gtu_p (new_lb
, new_ub
))
2465 new_lb
= wi::zero (TYPE_PRECISION (type
));
2468 std::swap (new_lb
, new_ub
);
2471 gcc_checking_assert (TYPE_UNSIGNED (type
));
2472 return value_range_base (type
, new_lb
, new_ub
);
2476 class operator_negate
: public range_operator
2479 virtual value_range_base
fold_range (tree type
,
2480 const value_range_base
&op1
,
2481 const value_range_base
&op2
) const;
2482 virtual bool op1_range (value_range_base
&r
, tree type
,
2483 const value_range_base
&lhs
,
2484 const value_range_base
&op2
) const;
2488 operator_negate::fold_range (tree type
,
2489 const value_range_base
&lh
,
2490 const value_range_base
&rh
) const
2493 if (empty_range_check (r
, lh
, rh
))
2495 // -X is simply 0 - X.
2497 range_op_handler (MINUS_EXPR
, type
)->fold_range (type
,
2498 range_zero (type
), lh
);
2502 operator_negate::op1_range (value_range_base
&r
, tree type
,
2503 const value_range_base
&lhs
,
2504 const value_range_base
&op2
) const
2506 // NEGATE is involutory.
2507 r
= fold_range (type
, lhs
, op2
);
2512 class operator_addr_expr
: public range_operator
2515 virtual value_range_base
fold_range (tree type
,
2516 const value_range_base
&op1
,
2517 const value_range_base
&op2
) const;
2518 virtual bool op1_range (value_range_base
&r
, tree type
,
2519 const value_range_base
&lhs
,
2520 const value_range_base
&op2
) const;
2524 operator_addr_expr::fold_range (tree type
,
2525 const value_range_base
&lh
,
2526 const value_range_base
&rh
) const
2529 if (empty_range_check (r
, lh
, rh
))
2532 // Return a non-null pointer of the LHS type (passed in op2).
2534 return range_zero (type
);
2535 if (!lh
.contains_p (build_zero_cst (lh
.type ())))
2536 return range_nonzero (type
);
2537 return value_range_base (type
);
2541 operator_addr_expr::op1_range (value_range_base
&r
, tree type
,
2542 const value_range_base
&lhs
,
2543 const value_range_base
&op2
) const
2545 r
= operator_addr_expr::fold_range (type
, lhs
, op2
);
2550 class pointer_plus_operator
: public range_operator
2553 virtual value_range_base
wi_fold (tree type
,
2554 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2555 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
2559 pointer_plus_operator::wi_fold (tree type
,
2560 const wide_int
&lh_lb
,
2561 const wide_int
&lh_ub
,
2562 const wide_int
&rh_lb
,
2563 const wide_int
&rh_ub
) const
2565 // For pointer types, we are really only interested in asserting
2566 // whether the expression evaluates to non-NULL.
2568 // With -fno-delete-null-pointer-checks we need to be more
2569 // conservative. As some object might reside at address 0,
2570 // then some offset could be added to it and the same offset
2571 // subtracted again and the result would be NULL.
2573 // static int a[12]; where &a[0] is NULL and
2576 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
2577 // where the first range doesn't include zero and the second one
2578 // doesn't either. As the second operand is sizetype (unsigned),
2579 // consider all ranges where the MSB could be set as possible
2580 // subtractions where the result might be NULL.
2581 if ((!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
2582 || !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
2583 && !TYPE_OVERFLOW_WRAPS (type
)
2584 && (flag_delete_null_pointer_checks
2585 || !wi::sign_mask (rh_ub
)))
2586 return range_nonzero (type
);
2587 if (lh_lb
== lh_ub
&& lh_lb
== 0
2588 && rh_lb
== rh_ub
&& rh_lb
== 0)
2589 return range_zero (type
);
2590 return value_range_base (type
);
2594 class pointer_min_max_operator
: public range_operator
2597 virtual value_range_base
wi_fold (tree type
,
2598 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2599 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
2603 pointer_min_max_operator::wi_fold (tree type
,
2604 const wide_int
&lh_lb
,
2605 const wide_int
&lh_ub
,
2606 const wide_int
&rh_lb
,
2607 const wide_int
&rh_ub
) const
2609 // For MIN/MAX expressions with pointers, we only care about
2610 // nullness. If both are non null, then the result is nonnull.
2611 // If both are null, then the result is null. Otherwise they
2613 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
2614 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
2615 return range_nonzero (type
);
2616 if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
2617 return range_zero (type
);
2618 return value_range_base (type
);
2622 class pointer_and_operator
: public range_operator
2625 virtual value_range_base
wi_fold (tree type
,
2626 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2627 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
2631 pointer_and_operator::wi_fold (tree type
,
2632 const wide_int
&lh_lb
,
2633 const wide_int
&lh_ub
,
2634 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
2635 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
2637 // For pointer types, we are really only interested in asserting
2638 // whether the expression evaluates to non-NULL.
2639 if (wi_zero_p (type
, lh_lb
, lh_ub
) || wi_zero_p (type
, lh_lb
, lh_ub
))
2640 return range_zero (type
);
2642 return value_range_base (type
);
2646 class pointer_or_operator
: public range_operator
2649 virtual value_range_base
wi_fold (tree type
,
2650 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2651 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
2655 pointer_or_operator::wi_fold (tree type
,
2656 const wide_int
&lh_lb
,
2657 const wide_int
&lh_ub
,
2658 const wide_int
&rh_lb
,
2659 const wide_int
&rh_ub
) const
2661 // For pointer types, we are really only interested in asserting
2662 // whether the expression evaluates to non-NULL.
2663 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
2664 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
2665 return range_nonzero (type
);
2666 if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
2667 return range_zero (type
);
2668 return value_range_base (type
);
2671 // This implements the range operator tables as local objects in this file.
2673 class range_op_table
2676 inline range_operator
*operator[] (enum tree_code code
);
2678 void set (enum tree_code code
, range_operator
&op
);
2680 range_operator
*m_range_tree
[MAX_TREE_CODES
];
2683 // Return a pointer to the range_operator instance, if there is one
2684 // associated with tree_code CODE.
2687 range_op_table::operator[] (enum tree_code code
)
2689 gcc_checking_assert (code
> 0 && code
< MAX_TREE_CODES
);
2690 return m_range_tree
[code
];
2693 // Add OP to the handler table for CODE.
2696 range_op_table::set (enum tree_code code
, range_operator
&op
)
2698 gcc_checking_assert (m_range_tree
[code
] == NULL
);
2699 m_range_tree
[code
] = &op
;
2702 // Instantiate a range op table for integral operations.
2704 class integral_table
: public range_op_table
2708 } integral_tree_table
;
2710 integral_table::integral_table ()
2712 set (EQ_EXPR
, op_equal
);
2713 set (NE_EXPR
, op_not_equal
);
2714 set (LT_EXPR
, op_lt
);
2715 set (LE_EXPR
, op_le
);
2716 set (GT_EXPR
, op_gt
);
2717 set (GE_EXPR
, op_ge
);
2718 set (PLUS_EXPR
, op_plus
);
2719 set (MINUS_EXPR
, op_minus
);
2720 set (MIN_EXPR
, op_min
);
2721 set (MAX_EXPR
, op_max
);
2722 set (MULT_EXPR
, op_mult
);
2723 set (TRUNC_DIV_EXPR
, op_trunc_div
);
2724 set (FLOOR_DIV_EXPR
, op_floor_div
);
2725 set (ROUND_DIV_EXPR
, op_round_div
);
2726 set (CEIL_DIV_EXPR
, op_ceil_div
);
2727 set (EXACT_DIV_EXPR
, op_exact_div
);
2728 set (LSHIFT_EXPR
, op_lshift
);
2729 set (RSHIFT_EXPR
, op_rshift
);
2730 set (NOP_EXPR
, op_convert
);
2731 set (CONVERT_EXPR
, op_convert
);
2732 set (TRUTH_AND_EXPR
, op_logical_and
);
2733 set (BIT_AND_EXPR
, op_bitwise_and
);
2734 set (TRUTH_OR_EXPR
, op_logical_or
);
2735 set (BIT_IOR_EXPR
, op_bitwise_or
);
2736 set (BIT_XOR_EXPR
, op_bitwise_xor
);
2737 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
2738 set (TRUTH_NOT_EXPR
, op_logical_not
);
2739 set (BIT_NOT_EXPR
, op_bitwise_not
);
2740 set (INTEGER_CST
, op_integer_cst
);
2741 set (SSA_NAME
, op_identity
);
2742 set (PAREN_EXPR
, op_identity
);
2743 set (OBJ_TYPE_REF
, op_identity
);
2744 set (ABS_EXPR
, op_abs
);
2745 set (ABSU_EXPR
, op_absu
);
2746 set (NEGATE_EXPR
, op_negate
);
2747 set (ADDR_EXPR
, op_addr
);
2750 // Instantiate a range op table for pointer operations.
2752 class pointer_table
: public range_op_table
2756 } pointer_tree_table
;
2758 pointer_table::pointer_table ()
2760 set (BIT_AND_EXPR
, op_pointer_and
);
2761 set (BIT_IOR_EXPR
, op_pointer_or
);
2762 set (MIN_EXPR
, op_ptr_min_max
);
2763 set (MAX_EXPR
, op_ptr_min_max
);
2764 set (POINTER_PLUS_EXPR
, op_pointer_plus
);
2766 set (EQ_EXPR
, op_equal
);
2767 set (NE_EXPR
, op_not_equal
);
2768 set (LT_EXPR
, op_lt
);
2769 set (LE_EXPR
, op_le
);
2770 set (GT_EXPR
, op_gt
);
2771 set (GE_EXPR
, op_ge
);
2772 set (SSA_NAME
, op_identity
);
2773 set (ADDR_EXPR
, op_addr
);
2774 set (NOP_EXPR
, op_convert
);
2775 set (CONVERT_EXPR
, op_convert
);
2777 set (BIT_NOT_EXPR
, op_bitwise_not
);
2778 set (BIT_XOR_EXPR
, op_bitwise_xor
);
2781 // The tables are hidden and accessed via a simple extern function.
2784 range_op_handler (enum tree_code code
, tree type
)
2786 // First check if there is apointer specialization.
2787 if (POINTER_TYPE_P (type
))
2788 return pointer_tree_table
[code
];
2789 return integral_tree_table
[code
];
2792 // Cast the range in R to TYPE.
2795 range_cast (value_range_base
&r
, tree type
)
2797 range_operator
*op
= range_op_handler (CONVERT_EXPR
, type
);
2798 r
= op
->fold_range (type
, r
, value_range_base (type
));
2802 #include "selftest.h"
2803 #include "stor-layout.h"
2805 // Ideally this should go in namespace selftest, but range_tests
2806 // needs to be a friend of class value_range_base so it can access
2807 // value_range_base::m_max_pairs.
2809 #define INT(N) build_int_cst (integer_type_node, (N))
2810 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
2811 #define INT16(N) build_int_cst (short_integer_type_node, (N))
2812 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
2813 #define INT64(N) build_int_cstu (long_long_integer_type_node, (N))
2814 #define UINT64(N) build_int_cstu (long_long_unsigned_type_node, (N))
2815 #define UINT128(N) build_int_cstu (u128_type, (N))
2816 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
2817 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
2819 #define RANGE3(A,B,C,D,E,F) \
2820 ( i1 = value_range_base (INT (A), INT (B)), \
2821 i2 = value_range_base (INT (C), INT (D)), \
2822 i3 = value_range_base (INT (E), INT (F)), \
2827 // Run all of the selftests within this file.
2832 tree u128_type
= build_nonstandard_integer_type (128, /*unsigned=*/1);
2833 value_range_base i1
, i2
, i3
;
2834 value_range_base r0
, r1
, rold
;
2836 // Test that NOT(255) is [0..254] in 8-bit land.
2837 value_range_base
not_255 (VR_ANTI_RANGE
, UCHAR (255), UCHAR (255));
2838 ASSERT_TRUE (not_255
== value_range_base (UCHAR (0), UCHAR (254)));
2840 // Test that NOT(0) is [1..255] in 8-bit land.
2841 value_range_base not_zero
= range_nonzero (unsigned_char_type_node
);
2842 ASSERT_TRUE (not_zero
== value_range_base (UCHAR (1), UCHAR (255)));
2844 // Check that [0,127][0x..ffffff80,0x..ffffff]
2845 // => ~[128, 0x..ffffff7f].
2846 r0
= value_range_base (UINT128 (0), UINT128 (127));
2847 tree high
= build_minus_one_cst (u128_type
);
2848 // low = -1 - 127 => 0x..ffffff80.
2849 tree low
= fold_build2 (MINUS_EXPR
, u128_type
, high
, UINT128(127));
2850 r1
= value_range_base (low
, high
); // [0x..ffffff80, 0x..ffffffff]
2851 // r0 = [0,127][0x..ffffff80,0x..fffffff].
2853 // r1 = [128, 0x..ffffff7f].
2854 r1
= value_range_base (UINT128(128),
2855 fold_build2 (MINUS_EXPR
, u128_type
,
2856 build_minus_one_cst (u128_type
),
2859 ASSERT_TRUE (r0
== r1
);
2861 r0
.set_varying (integer_type_node
);
2862 tree minint
= wide_int_to_tree (integer_type_node
, r0
.lower_bound ());
2863 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
2865 r0
.set_varying (short_integer_type_node
);
2866 tree minshort
= wide_int_to_tree (short_integer_type_node
, r0
.lower_bound ());
2867 tree maxshort
= wide_int_to_tree (short_integer_type_node
, r0
.upper_bound ());
2869 r0
.set_varying (unsigned_type_node
);
2870 tree maxuint
= wide_int_to_tree (unsigned_type_node
, r0
.upper_bound ());
2872 // Check that ~[0,5] => [6,MAX] for unsigned int.
2873 r0
= value_range_base (UINT (0), UINT (5));
2875 ASSERT_TRUE (r0
== value_range_base (UINT(6), maxuint
));
2877 // Check that ~[10,MAX] => [0,9] for unsigned int.
2878 r0
= value_range_base (VR_RANGE
, UINT(10), maxuint
);
2880 ASSERT_TRUE (r0
== value_range_base (UINT (0), UINT (9)));
2882 // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers.
2883 r0
= value_range_base (VR_ANTI_RANGE
, UINT128 (0), UINT128 (5));
2884 r1
= value_range_base (UINT128(6), build_minus_one_cst (u128_type
));
2885 ASSERT_TRUE (r0
== r1
);
2887 // Check that [~5] is really [-MIN,4][6,MAX].
2888 r0
= value_range_base (VR_ANTI_RANGE
, INT (5), INT (5));
2889 r1
= value_range_base (minint
, INT (4));
2890 r1
.union_ (value_range_base (INT (6), maxint
));
2891 ASSERT_FALSE (r1
.undefined_p ());
2892 ASSERT_TRUE (r0
== r1
);
2894 r1
= value_range_base (INT (5), INT (5));
2896 value_range_base
r2 (r1
);
2897 ASSERT_TRUE (r1
== r2
);
2899 r1
= value_range_base (INT (5), INT (10));
2902 r1
= value_range_base (integer_type_node
,
2903 wi::to_wide (INT (5)), wi::to_wide (INT (10)));
2905 ASSERT_TRUE (r1
.contains_p (INT (7)));
2907 r1
= value_range_base (SCHAR (0), SCHAR (20));
2908 ASSERT_TRUE (r1
.contains_p (SCHAR(15)));
2909 ASSERT_FALSE (r1
.contains_p (SCHAR(300)));
2911 // If a range is in any way outside of the range for the converted
2912 // to range, default to the range for the new type.
2913 if (TYPE_PRECISION (TREE_TYPE (maxint
))
2914 > TYPE_PRECISION (short_integer_type_node
))
2916 r1
= value_range_base (integer_zero_node
, maxint
);
2917 range_cast (r1
, short_integer_type_node
);
2918 ASSERT_TRUE (r1
.lower_bound () == wi::to_wide (minshort
)
2919 && r1
.upper_bound() == wi::to_wide (maxshort
));
2922 // (unsigned char)[-5,-1] => [251,255].
2923 r0
= rold
= value_range_base (SCHAR (-5), SCHAR (-1));
2924 range_cast (r0
, unsigned_char_type_node
);
2925 ASSERT_TRUE (r0
== value_range_base (UCHAR (251), UCHAR (255)));
2926 range_cast (r0
, signed_char_type_node
);
2927 ASSERT_TRUE (r0
== rold
);
2929 // (signed char)[15, 150] => [-128,-106][15,127].
2930 r0
= rold
= value_range_base (UCHAR (15), UCHAR (150));
2931 range_cast (r0
, signed_char_type_node
);
2932 r1
= value_range_base (SCHAR (15), SCHAR (127));
2933 r2
= value_range_base (SCHAR (-128), SCHAR (-106));
2935 ASSERT_TRUE (r1
== r0
);
2936 range_cast (r0
, unsigned_char_type_node
);
2937 ASSERT_TRUE (r0
== rold
);
2939 // (unsigned char)[-5, 5] => [0,5][251,255].
2940 r0
= rold
= value_range_base (SCHAR (-5), SCHAR (5));
2941 range_cast (r0
, unsigned_char_type_node
);
2942 r1
= value_range_base (UCHAR (251), UCHAR (255));
2943 r2
= value_range_base (UCHAR (0), UCHAR (5));
2945 ASSERT_TRUE (r0
== r1
);
2946 range_cast (r0
, signed_char_type_node
);
2947 ASSERT_TRUE (r0
== rold
);
2949 // (unsigned char)[-5,5] => [0,5][251,255].
2950 r0
= value_range_base (INT (-5), INT (5));
2951 range_cast (r0
, unsigned_char_type_node
);
2952 r1
= value_range_base (UCHAR (0), UCHAR (5));
2953 r1
.union_ (value_range_base (UCHAR (251), UCHAR (255)));
2954 ASSERT_TRUE (r0
== r1
);
2956 // (unsigned char)[5U,1974U] => [0,255].
2957 r0
= value_range_base (UINT (5), UINT (1974));
2958 range_cast (r0
, unsigned_char_type_node
);
2959 ASSERT_TRUE (r0
== value_range_base (UCHAR (0), UCHAR (255)));
2960 range_cast (r0
, integer_type_node
);
2961 // Going to a wider range should not sign extend.
2962 ASSERT_TRUE (r0
== value_range_base (INT (0), INT (255)));
2964 // (unsigned char)[-350,15] => [0,255].
2965 r0
= value_range_base (INT (-350), INT (15));
2966 range_cast (r0
, unsigned_char_type_node
);
2967 ASSERT_TRUE (r0
== (value_range_base
2968 (TYPE_MIN_VALUE (unsigned_char_type_node
),
2969 TYPE_MAX_VALUE (unsigned_char_type_node
))));
2971 // Casting [-120,20] from signed char to unsigned short.
2972 // => [0, 20][0xff88, 0xffff].
2973 r0
= value_range_base (SCHAR (-120), SCHAR (20));
2974 range_cast (r0
, short_unsigned_type_node
);
2975 r1
= value_range_base (UINT16 (0), UINT16 (20));
2976 r2
= value_range_base (UINT16 (0xff88), UINT16 (0xffff));
2978 ASSERT_TRUE (r0
== r1
);
2979 // A truncating cast back to signed char will work because [-120, 20]
2980 // is representable in signed char.
2981 range_cast (r0
, signed_char_type_node
);
2982 ASSERT_TRUE (r0
== value_range_base (SCHAR (-120), SCHAR (20)));
2984 // unsigned char -> signed short
2985 // (signed short)[(unsigned char)25, (unsigned char)250]
2986 // => [(signed short)25, (signed short)250]
2987 r0
= rold
= value_range_base (UCHAR (25), UCHAR (250));
2988 range_cast (r0
, short_integer_type_node
);
2989 r1
= value_range_base (INT16 (25), INT16 (250));
2990 ASSERT_TRUE (r0
== r1
);
2991 range_cast (r0
, unsigned_char_type_node
);
2992 ASSERT_TRUE (r0
== rold
);
2994 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
2995 r0
= value_range_base (TYPE_MIN_VALUE (long_long_integer_type_node
),
2996 TYPE_MAX_VALUE (long_long_integer_type_node
));
2997 range_cast (r0
, short_unsigned_type_node
);
2998 r1
= value_range_base (TYPE_MIN_VALUE (short_unsigned_type_node
),
2999 TYPE_MAX_VALUE (short_unsigned_type_node
));
3000 ASSERT_TRUE (r0
== r1
);
3002 // NOT([10,20]) ==> [-MIN,9][21,MAX].
3003 r0
= r1
= value_range_base (INT (10), INT (20));
3004 r2
= value_range_base (minint
, INT(9));
3005 r2
.union_ (value_range_base (INT(21), maxint
));
3006 ASSERT_FALSE (r2
.undefined_p ());
3008 ASSERT_TRUE (r1
== r2
);
3009 // Test that NOT(NOT(x)) == x.
3011 ASSERT_TRUE (r0
== r2
);
3013 // Test that booleans and their inverse work as expected.
3014 r0
= range_zero (boolean_type_node
);
3015 ASSERT_TRUE (r0
== value_range_base (build_zero_cst (boolean_type_node
),
3016 build_zero_cst (boolean_type_node
)));
3018 ASSERT_TRUE (r0
== value_range_base (build_one_cst (boolean_type_node
),
3019 build_one_cst (boolean_type_node
)));
3021 // Casting NONZERO to a narrower type will wrap/overflow so
3022 // it's just the entire range for the narrower type.
3024 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
3025 // is outside of the range of a smaller range, return the full
3027 if (TYPE_PRECISION (integer_type_node
)
3028 > TYPE_PRECISION (short_integer_type_node
))
3030 r0
= range_nonzero (integer_type_node
);
3031 range_cast (r0
, short_integer_type_node
);
3032 r1
= value_range_base (TYPE_MIN_VALUE (short_integer_type_node
),
3033 TYPE_MAX_VALUE (short_integer_type_node
));
3034 ASSERT_TRUE (r0
== r1
);
3037 // Casting NONZERO from a narrower signed to a wider signed.
3039 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
3040 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
3041 r0
= range_nonzero (short_integer_type_node
);
3042 range_cast (r0
, integer_type_node
);
3043 r1
= value_range_base (INT (-32768), INT (-1));
3044 r2
= value_range_base (INT (1), INT (32767));
3046 ASSERT_TRUE (r0
== r1
);
3048 if (value_range_base::m_max_pairs
> 2)
3050 // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20].
3051 r0
= value_range_base (INT (10), INT (20));
3052 r1
= value_range_base (INT (5), INT (8));
3054 r1
= value_range_base (INT (1), INT (3));
3056 ASSERT_TRUE (r0
== RANGE3 (1, 3, 5, 8, 10, 20));
3058 // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20].
3059 r1
= value_range_base (INT (-5), INT (0));
3061 ASSERT_TRUE (r0
== RANGE3 (-5, 3, 5, 8, 10, 20));
3064 // [10,20] U [30,40] ==> [10,20][30,40].
3065 r0
= value_range_base (INT (10), INT (20));
3066 r1
= value_range_base (INT (30), INT (40));
3068 ASSERT_TRUE (r0
== range_union (value_range_base (INT (10), INT (20)),
3069 value_range_base (INT (30), INT (40))));
3070 if (value_range_base::m_max_pairs
> 2)
3072 // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60].
3073 r1
= value_range_base (INT (50), INT (60));
3075 ASSERT_TRUE (r0
== RANGE3 (10, 20, 30, 40, 50, 60));
3076 // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80].
3077 r1
= value_range_base (INT (70), INT (80));
3080 r2
= RANGE3 (10, 20, 30, 40, 50, 60);
3081 r2
.union_ (value_range_base (INT (70), INT (80)));
3082 ASSERT_TRUE (r0
== r2
);
3085 // Make sure NULL and non-NULL of pointer types work, and that
3086 // inverses of them are consistent.
3087 tree voidp
= build_pointer_type (void_type_node
);
3088 r0
= range_zero (voidp
);
3092 ASSERT_TRUE (r0
== r1
);
3094 if (value_range_base::m_max_pairs
> 2)
3096 // [10,20][30,40][50,60] U [6,35] => [6,40][50,60].
3097 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3098 r1
= value_range_base (INT (6), INT (35));
3100 ASSERT_TRUE (r0
== range_union (value_range_base (INT (6), INT (40)),
3101 value_range_base (INT (50), INT (60))));
3103 // [10,20][30,40][50,60] U [6,60] => [6,60].
3104 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3105 r1
= value_range_base (INT (6), INT (60));
3107 ASSERT_TRUE (r0
== value_range_base (INT (6), INT (60)));
3109 // [10,20][30,40][50,60] U [6,70] => [6,70].
3110 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3111 r1
= value_range_base (INT (6), INT (70));
3113 ASSERT_TRUE (r0
== value_range_base (INT (6), INT (70)));
3115 // [10,20][30,40][50,60] U [35,70] => [10,20][30,70].
3116 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3117 r1
= value_range_base (INT (35), INT (70));
3119 ASSERT_TRUE (r0
== range_union (value_range_base (INT (10), INT (20)),
3120 value_range_base (INT (30), INT (70))));
3123 // [10,20][30,40] U [25,70] => [10,70].
3124 r0
= range_union (value_range_base (INT (10), INT (20)),
3125 value_range_base (INT (30), INT (40)));
3126 r1
= value_range_base (INT (25), INT (70));
3128 ASSERT_TRUE (r0
== range_union (value_range_base (INT (10), INT (20)),
3129 value_range_base (INT (25), INT (70))));
3131 if (value_range_base::m_max_pairs
> 2)
3133 // [10,20][30,40][50,60] U [15,35] => [10,40][50,60].
3134 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3135 r1
= value_range_base (INT (15), INT (35));
3137 ASSERT_TRUE (r0
== range_union (value_range_base (INT (10), INT (40)),
3138 value_range_base (INT (50), INT (60))));
3141 // [10,20] U [15, 30] => [10, 30].
3142 r0
= value_range_base (INT (10), INT (20));
3143 r1
= value_range_base (INT (15), INT (30));
3145 ASSERT_TRUE (r0
== value_range_base (INT (10), INT (30)));
3147 // [10,20] U [25,25] => [10,20][25,25].
3148 r0
= value_range_base (INT (10), INT (20));
3149 r1
= value_range_base (INT (25), INT (25));
3151 ASSERT_TRUE (r0
== range_union (value_range_base (INT (10), INT (20)),
3152 value_range_base (INT (25), INT (25))));
3154 if (value_range_base::m_max_pairs
> 2)
3156 // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60].
3157 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3158 r1
= value_range_base (INT (35), INT (35));
3160 ASSERT_TRUE (r0
== RANGE3 (10, 20, 30, 40, 50, 60));
3163 // [15,40] U [] => [15,40].
3164 r0
= value_range_base (INT (15), INT (40));
3165 r1
.set_undefined ();
3167 ASSERT_TRUE (r0
== value_range_base (INT (15), INT (40)));
3169 // [10,20] U [10,10] => [10,20].
3170 r0
= value_range_base (INT (10), INT (20));
3171 r1
= value_range_base (INT (10), INT (10));
3173 ASSERT_TRUE (r0
== value_range_base (INT (10), INT (20)));
3175 // [10,20] U [9,9] => [9,20].
3176 r0
= value_range_base (INT (10), INT (20));
3177 r1
= value_range_base (INT (9), INT (9));
3179 ASSERT_TRUE (r0
== value_range_base (INT (9), INT (20)));
3181 if (value_range_base::m_max_pairs
> 2)
3183 // [10,10][12,12][20,100] ^ [15,200].
3184 r0
= RANGE3 (10, 10, 12, 12, 20, 100);
3185 r1
= value_range_base (INT (15), INT (200));
3187 ASSERT_TRUE (r0
== value_range_base (INT (20), INT (100)));
3189 // [10,20][30,40][50,60] ^ [15,25][38,51][55,70]
3190 // => [15,20][38,40][50,51][55,60]
3191 r0
= RANGE3 (10, 20, 30, 40, 50, 60);
3192 r1
= RANGE3 (15, 25, 38, 51, 55, 70);
3194 if (value_range_base::m_max_pairs
== 3)
3196 // When pairs==3, we don't have enough space, so
3197 // conservatively handle things. Thus, the ...[50,60].
3198 ASSERT_TRUE (r0
== RANGE3 (15, 20, 38, 40, 50, 60));
3202 r2
= RANGE3 (15, 20, 38, 40, 50, 51);
3203 r2
.union_ (value_range_base (INT (55), INT (60)));
3204 ASSERT_TRUE (r0
== r2
);
3207 // [15,20][30,40][50,60] ^ [15,35][40,90][100,200]
3208 // => [15,20][30,35][40,60]
3209 r0
= RANGE3 (15, 20, 30, 40, 50, 60);
3210 r1
= RANGE3 (15, 35, 40, 90, 100, 200);
3212 if (value_range_base::m_max_pairs
== 3)
3214 // When pairs==3, we don't have enough space, so
3215 // conservatively handle things.
3216 ASSERT_TRUE (r0
== RANGE3 (15, 20, 30, 35, 40, 60));
3220 r2
= RANGE3 (15, 20, 30, 35, 40, 40);
3221 r2
.union_ (value_range_base (INT (50), INT (60)));
3222 ASSERT_TRUE (r0
== r2
);
3225 // Test cases where a union inserts a sub-range inside a larger
3228 // [8,10][135,255] U [14,14] => [8,10][14,14][135,255]
3229 r0
= range_union (value_range_base (INT (8), INT (10)),
3230 value_range_base (INT (135), INT (255)));
3231 r1
= value_range_base (INT (14), INT (14));
3233 ASSERT_TRUE (r0
== RANGE3 (8, 10, 14, 14, 135, 255));
3236 // [10,20] ^ [15,30] => [15,20].
3237 r0
= value_range_base (INT (10), INT (20));
3238 r1
= value_range_base (INT (15), INT (30));
3240 ASSERT_TRUE (r0
== value_range_base (INT (15), INT (20)));
3242 // [10,20][30,40] ^ [40,50] => [40,40].
3243 r0
= range_union (value_range_base (INT (10), INT (20)),
3244 value_range_base (INT (30), INT (40)));
3245 r1
= value_range_base (INT (40), INT (50));
3247 ASSERT_TRUE (r0
== value_range_base (INT (40), INT (40)));
3249 // Test non-destructive intersection.
3250 r0
= rold
= value_range_base (INT (10), INT (20));
3251 ASSERT_FALSE (range_intersect (r0
, value_range_base (INT (15),
3252 INT (30))).undefined_p ());
3253 ASSERT_TRUE (r0
== rold
);
3255 // Test the internal sanity of wide_int's wrt HWIs.
3256 ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node
),
3257 TYPE_SIGN (boolean_type_node
))
3258 == wi::uhwi (1, TYPE_PRECISION (boolean_type_node
)));
3261 r0
= value_range_base (INT (0), INT (0));
3262 ASSERT_TRUE (r0
.zero_p ());
3264 // Test nonzero_p().
3265 r0
= value_range_base (INT (0), INT (0));
3267 ASSERT_TRUE (r0
.nonzero_p ());
3269 #endif // CHECKING_P