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_kind rel
) const
184 gcc_checking_assert (r
.supports_type_p (type
));
185 if (empty_range_varying (r
, type
, lh
, rh
))
188 unsigned num_lh
= lh
.num_pairs ();
189 unsigned num_rh
= rh
.num_pairs ();
191 // If both ranges are single pairs, fold directly into the result range.
192 // If the number of subranges grows too high, produce a summary result as the
193 // loop becomes exponential with little benefit. See PR 103821.
194 if ((num_lh
== 1 && num_rh
== 1) || num_lh
* num_rh
> 12)
196 wi_fold_in_parts (r
, type
, lh
.lower_bound (), lh
.upper_bound (),
197 rh
.lower_bound (), rh
.upper_bound ());
198 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
204 for (unsigned x
= 0; x
< num_lh
; ++x
)
205 for (unsigned y
= 0; y
< num_rh
; ++y
)
207 wide_int lh_lb
= lh
.lower_bound (x
);
208 wide_int lh_ub
= lh
.upper_bound (x
);
209 wide_int rh_lb
= rh
.lower_bound (y
);
210 wide_int rh_ub
= rh
.upper_bound (y
);
211 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
215 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
219 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
223 // The default for op1_range is to return false.
226 range_operator::op1_range (irange
&r ATTRIBUTE_UNUSED
,
227 tree type ATTRIBUTE_UNUSED
,
228 const irange
&lhs ATTRIBUTE_UNUSED
,
229 const irange
&op2 ATTRIBUTE_UNUSED
,
230 relation_kind rel ATTRIBUTE_UNUSED
) const
235 // The default for op2_range is to return false.
238 range_operator::op2_range (irange
&r ATTRIBUTE_UNUSED
,
239 tree type ATTRIBUTE_UNUSED
,
240 const irange
&lhs ATTRIBUTE_UNUSED
,
241 const irange
&op1 ATTRIBUTE_UNUSED
,
242 relation_kind rel ATTRIBUTE_UNUSED
) const
247 // The default relation routines return VREL_VARYING.
250 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
251 const irange
&op1 ATTRIBUTE_UNUSED
,
252 const irange
&op2 ATTRIBUTE_UNUSED
,
253 relation_kind rel ATTRIBUTE_UNUSED
) const
259 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
260 const irange
&op1 ATTRIBUTE_UNUSED
,
261 const irange
&op2 ATTRIBUTE_UNUSED
,
262 relation_kind rel ATTRIBUTE_UNUSED
) const
268 range_operator::op1_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
) const
273 // Default is no relation affects the LHS.
276 range_operator::op1_op2_relation_effect (irange
&lhs_range ATTRIBUTE_UNUSED
,
277 tree type ATTRIBUTE_UNUSED
,
278 const irange
&op1_range ATTRIBUTE_UNUSED
,
279 const irange
&op2_range ATTRIBUTE_UNUSED
,
280 relation_kind rel ATTRIBUTE_UNUSED
) const
285 // Create and return a range from a pair of wide-ints that are known
286 // to have overflowed (or underflowed).
289 value_range_from_overflowed_bounds (irange
&r
, tree type
,
290 const wide_int
&wmin
,
291 const wide_int
&wmax
)
293 const signop sgn
= TYPE_SIGN (type
);
294 const unsigned int prec
= TYPE_PRECISION (type
);
296 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
297 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
302 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
305 if (wi::cmp (tmax
, tem
, sgn
) > 0)
308 // If the anti-range would cover nothing, drop to varying.
309 // Likewise if the anti-range bounds are outside of the types
311 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
312 r
.set_varying (type
);
315 tree tree_min
= wide_int_to_tree (type
, tmin
);
316 tree tree_max
= wide_int_to_tree (type
, tmax
);
317 r
.set (tree_min
, tree_max
, VR_ANTI_RANGE
);
321 // Create and return a range from a pair of wide-ints. MIN_OVF and
322 // MAX_OVF describe any overflow that might have occurred while
323 // calculating WMIN and WMAX respectively.
326 value_range_with_overflow (irange
&r
, tree type
,
327 const wide_int
&wmin
, const wide_int
&wmax
,
328 wi::overflow_type min_ovf
= wi::OVF_NONE
,
329 wi::overflow_type max_ovf
= wi::OVF_NONE
)
331 const signop sgn
= TYPE_SIGN (type
);
332 const unsigned int prec
= TYPE_PRECISION (type
);
333 const bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
335 // For one bit precision if max != min, then the range covers all
337 if (prec
== 1 && wi::ne_p (wmax
, wmin
))
339 r
.set_varying (type
);
345 // If overflow wraps, truncate the values and adjust the range,
346 // kind, and bounds appropriately.
347 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
349 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
350 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
351 // If the limits are swapped, we wrapped around and cover
353 if (wi::gt_p (tmin
, tmax
, sgn
))
354 r
.set_varying (type
);
356 // No overflow or both overflow or underflow. The range
357 // kind stays normal.
358 r
.set (wide_int_to_tree (type
, tmin
),
359 wide_int_to_tree (type
, tmax
));
363 if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
364 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
365 value_range_from_overflowed_bounds (r
, type
, wmin
, wmax
);
367 // Other underflow and/or overflow, drop to VR_VARYING.
368 r
.set_varying (type
);
372 // If both bounds either underflowed or overflowed, then the result
374 if ((min_ovf
== wi::OVF_OVERFLOW
&& max_ovf
== wi::OVF_OVERFLOW
)
375 || (min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_UNDERFLOW
))
381 // If overflow does not wrap, saturate to [MIN, MAX].
382 wide_int new_lb
, new_ub
;
383 if (min_ovf
== wi::OVF_UNDERFLOW
)
384 new_lb
= wi::min_value (prec
, sgn
);
385 else if (min_ovf
== wi::OVF_OVERFLOW
)
386 new_lb
= wi::max_value (prec
, sgn
);
390 if (max_ovf
== wi::OVF_UNDERFLOW
)
391 new_ub
= wi::min_value (prec
, sgn
);
392 else if (max_ovf
== wi::OVF_OVERFLOW
)
393 new_ub
= wi::max_value (prec
, sgn
);
397 r
.set (wide_int_to_tree (type
, new_lb
),
398 wide_int_to_tree (type
, new_ub
));
402 // Create and return a range from a pair of wide-ints. Canonicalize
403 // the case where the bounds are swapped. In which case, we transform
404 // [10,5] into [MIN,5][10,MAX].
407 create_possibly_reversed_range (irange
&r
, tree type
,
408 const wide_int
&new_lb
, const wide_int
&new_ub
)
410 signop s
= TYPE_SIGN (type
);
411 // If the bounds are swapped, treat the result as if an overflow occured.
412 if (wi::gt_p (new_lb
, new_ub
, s
))
413 value_range_from_overflowed_bounds (r
, type
, new_lb
, new_ub
);
415 // Otherwise it's just a normal range.
416 r
.set (wide_int_to_tree (type
, new_lb
), wide_int_to_tree (type
, new_ub
));
419 // Return the summary information about boolean range LHS. If EMPTY/FULL,
420 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
423 get_bool_state (vrange
&r
, const vrange
&lhs
, tree val_type
)
425 // If there is no result, then this is unexecutable.
426 if (lhs
.undefined_p ())
435 // For TRUE, we can't just test for [1,1] because Ada can have
436 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
437 if (lhs
.contains_p (build_zero_cst (lhs
.type ())))
439 r
.set_varying (val_type
);
447 class operator_equal
: public range_operator
449 using range_operator::fold_range
;
450 using range_operator::op1_range
;
451 using range_operator::op2_range
;
453 virtual bool fold_range (irange
&r
, tree type
,
456 relation_kind rel
= VREL_VARYING
) const;
457 virtual bool op1_range (irange
&r
, tree type
,
460 relation_kind rel
= VREL_VARYING
) const;
461 virtual bool op2_range (irange
&r
, tree type
,
464 relation_kind rel
= VREL_VARYING
) const;
465 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
468 // Check if the LHS range indicates a relation between OP1 and OP2.
471 equal_op1_op2_relation (const irange
&lhs
)
473 if (lhs
.undefined_p ())
474 return VREL_UNDEFINED
;
476 // FALSE = op1 == op2 indicates NE_EXPR.
480 // TRUE = op1 == op2 indicates EQ_EXPR.
481 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
487 operator_equal::op1_op2_relation (const irange
&lhs
) const
489 return equal_op1_op2_relation (lhs
);
494 operator_equal::fold_range (irange
&r
, tree type
,
497 relation_kind rel
) const
499 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_EQ
))
502 // We can be sure the values are always equal or not if both ranges
503 // consist of a single value, and then compare them.
504 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
505 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
507 if (wi::eq_p (op1
.lower_bound (), op2
.upper_bound()))
508 r
= range_true (type
);
510 r
= range_false (type
);
514 // If ranges do not intersect, we know the range is not equal,
515 // otherwise we don't know anything for sure.
516 int_range_max tmp
= op1
;
518 if (tmp
.undefined_p ())
519 r
= range_false (type
);
521 r
= range_true_and_false (type
);
527 operator_equal::op1_range (irange
&r
, tree type
,
530 relation_kind rel ATTRIBUTE_UNUSED
) const
532 switch (get_bool_state (r
, lhs
, type
))
535 // If the result is false, the only time we know anything is
536 // if OP2 is a constant.
537 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
543 r
.set_varying (type
);
547 // If it's true, the result is the same as OP2.
558 operator_equal::op2_range (irange
&r
, tree type
,
561 relation_kind rel
) const
563 return operator_equal::op1_range (r
, type
, lhs
, op1
, rel
);
566 class operator_not_equal
: public range_operator
568 using range_operator::fold_range
;
569 using range_operator::op1_range
;
570 using range_operator::op2_range
;
572 virtual bool fold_range (irange
&r
, tree type
,
575 relation_kind rel
= VREL_VARYING
) const;
576 virtual bool op1_range (irange
&r
, tree type
,
579 relation_kind rel
= VREL_VARYING
) const;
580 virtual bool op2_range (irange
&r
, tree type
,
583 relation_kind rel
= VREL_VARYING
) const;
584 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
587 // Check if the LHS range indicates a relation between OP1 and OP2.
590 not_equal_op1_op2_relation (const irange
&lhs
)
592 if (lhs
.undefined_p ())
593 return VREL_UNDEFINED
;
595 // FALSE = op1 != op2 indicates EQ_EXPR.
599 // TRUE = op1 != op2 indicates NE_EXPR.
600 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
606 operator_not_equal::op1_op2_relation (const irange
&lhs
) const
608 return not_equal_op1_op2_relation (lhs
);
612 operator_not_equal::fold_range (irange
&r
, tree type
,
615 relation_kind rel
) const
617 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_NE
))
620 // We can be sure the values are always equal or not if both ranges
621 // consist of a single value, and then compare them.
622 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
623 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
625 if (wi::ne_p (op1
.lower_bound (), op2
.upper_bound()))
626 r
= range_true (type
);
628 r
= range_false (type
);
632 // If ranges do not intersect, we know the range is not equal,
633 // otherwise we don't know anything for sure.
634 int_range_max tmp
= op1
;
636 if (tmp
.undefined_p ())
637 r
= range_true (type
);
639 r
= range_true_and_false (type
);
645 operator_not_equal::op1_range (irange
&r
, tree type
,
648 relation_kind rel ATTRIBUTE_UNUSED
) const
650 switch (get_bool_state (r
, lhs
, type
))
653 // If the result is true, the only time we know anything is if
654 // OP2 is a constant.
655 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
661 r
.set_varying (type
);
665 // If it's false, the result is the same as OP2.
677 operator_not_equal::op2_range (irange
&r
, tree type
,
680 relation_kind rel
) const
682 return operator_not_equal::op1_range (r
, type
, lhs
, op1
, rel
);
685 // (X < VAL) produces the range of [MIN, VAL - 1].
688 build_lt (irange
&r
, tree type
, const wide_int
&val
)
690 wi::overflow_type ov
;
692 signop sgn
= TYPE_SIGN (type
);
694 // Signed 1 bit cannot represent 1 for subtraction.
696 lim
= wi::add (val
, -1, sgn
, &ov
);
698 lim
= wi::sub (val
, 1, sgn
, &ov
);
700 // If val - 1 underflows, check if X < MIN, which is an empty range.
704 r
= int_range
<1> (type
, min_limit (type
), lim
);
707 // (X <= VAL) produces the range of [MIN, VAL].
710 build_le (irange
&r
, tree type
, const wide_int
&val
)
712 r
= int_range
<1> (type
, min_limit (type
), val
);
715 // (X > VAL) produces the range of [VAL + 1, MAX].
718 build_gt (irange
&r
, tree type
, const wide_int
&val
)
720 wi::overflow_type ov
;
722 signop sgn
= TYPE_SIGN (type
);
724 // Signed 1 bit cannot represent 1 for addition.
726 lim
= wi::sub (val
, -1, sgn
, &ov
);
728 lim
= wi::add (val
, 1, sgn
, &ov
);
729 // If val + 1 overflows, check is for X > MAX, which is an empty range.
733 r
= int_range
<1> (type
, lim
, max_limit (type
));
736 // (X >= val) produces the range of [VAL, MAX].
739 build_ge (irange
&r
, tree type
, const wide_int
&val
)
741 r
= int_range
<1> (type
, val
, max_limit (type
));
745 class operator_lt
: public range_operator
747 using range_operator::fold_range
;
748 using range_operator::op1_range
;
749 using range_operator::op2_range
;
751 virtual bool fold_range (irange
&r
, tree type
,
754 relation_kind rel
= VREL_VARYING
) const;
755 virtual bool op1_range (irange
&r
, tree type
,
758 relation_kind rel
= VREL_VARYING
) const;
759 virtual bool op2_range (irange
&r
, tree type
,
762 relation_kind rel
= VREL_VARYING
) const;
763 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
766 // Check if the LHS range indicates a relation between OP1 and OP2.
769 lt_op1_op2_relation (const irange
&lhs
)
771 if (lhs
.undefined_p ())
772 return VREL_UNDEFINED
;
774 // FALSE = op1 < op2 indicates GE_EXPR.
778 // TRUE = op1 < op2 indicates LT_EXPR.
779 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
785 operator_lt::op1_op2_relation (const irange
&lhs
) const
787 return lt_op1_op2_relation (lhs
);
791 operator_lt::fold_range (irange
&r
, tree type
,
794 relation_kind rel
) const
796 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LT
))
799 signop sign
= TYPE_SIGN (op1
.type ());
800 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
802 if (wi::lt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
803 r
= range_true (type
);
804 else if (!wi::lt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
805 r
= range_false (type
);
806 // Use nonzero bits to determine if < 0 is false.
807 else if (op2
.zero_p () && !wi::neg_p (op1
.get_nonzero_bits (), sign
))
808 r
= range_false (type
);
810 r
= range_true_and_false (type
);
815 operator_lt::op1_range (irange
&r
, tree type
,
818 relation_kind rel ATTRIBUTE_UNUSED
) const
820 switch (get_bool_state (r
, lhs
, type
))
823 build_lt (r
, type
, op2
.upper_bound ());
827 build_ge (r
, type
, op2
.lower_bound ());
837 operator_lt::op2_range (irange
&r
, tree type
,
840 relation_kind rel ATTRIBUTE_UNUSED
) const
842 switch (get_bool_state (r
, lhs
, type
))
845 build_le (r
, type
, op1
.upper_bound ());
849 build_gt (r
, type
, op1
.lower_bound ());
859 class operator_le
: public range_operator
861 using range_operator::fold_range
;
862 using range_operator::op1_range
;
863 using range_operator::op2_range
;
865 virtual bool fold_range (irange
&r
, tree type
,
868 relation_kind rel
= VREL_VARYING
) const;
869 virtual bool op1_range (irange
&r
, tree type
,
872 relation_kind rel
= VREL_VARYING
) const;
873 virtual bool op2_range (irange
&r
, tree type
,
876 relation_kind rel
= VREL_VARYING
) const;
877 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
880 // Check if the LHS range indicates a relation between OP1 and OP2.
883 le_op1_op2_relation (const irange
&lhs
)
885 if (lhs
.undefined_p ())
886 return VREL_UNDEFINED
;
888 // FALSE = op1 <= op2 indicates GT_EXPR.
892 // TRUE = op1 <= op2 indicates LE_EXPR.
893 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
899 operator_le::op1_op2_relation (const irange
&lhs
) const
901 return le_op1_op2_relation (lhs
);
905 operator_le::fold_range (irange
&r
, tree type
,
908 relation_kind rel
) const
910 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LE
))
913 signop sign
= TYPE_SIGN (op1
.type ());
914 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
916 if (wi::le_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
917 r
= range_true (type
);
918 else if (!wi::le_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
919 r
= range_false (type
);
921 r
= range_true_and_false (type
);
926 operator_le::op1_range (irange
&r
, tree type
,
929 relation_kind rel ATTRIBUTE_UNUSED
) const
931 switch (get_bool_state (r
, lhs
, type
))
934 build_le (r
, type
, op2
.upper_bound ());
938 build_gt (r
, type
, op2
.lower_bound ());
948 operator_le::op2_range (irange
&r
, tree type
,
951 relation_kind rel ATTRIBUTE_UNUSED
) const
953 switch (get_bool_state (r
, lhs
, type
))
956 build_lt (r
, type
, op1
.upper_bound ());
960 build_ge (r
, type
, op1
.lower_bound ());
970 class operator_gt
: public range_operator
972 using range_operator::fold_range
;
973 using range_operator::op1_range
;
974 using range_operator::op2_range
;
976 virtual bool fold_range (irange
&r
, tree type
,
979 relation_kind rel
= VREL_VARYING
) const;
980 virtual bool op1_range (irange
&r
, tree type
,
983 relation_kind rel
= VREL_VARYING
) const;
984 virtual bool op2_range (irange
&r
, tree type
,
987 relation_kind rel
= VREL_VARYING
) const;
988 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
991 // Check if the LHS range indicates a relation between OP1 and OP2.
994 gt_op1_op2_relation (const irange
&lhs
)
996 if (lhs
.undefined_p ())
997 return VREL_UNDEFINED
;
999 // FALSE = op1 > op2 indicates LE_EXPR.
1003 // TRUE = op1 > op2 indicates GT_EXPR.
1004 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
1006 return VREL_VARYING
;
1010 operator_gt::op1_op2_relation (const irange
&lhs
) const
1012 return gt_op1_op2_relation (lhs
);
1017 operator_gt::fold_range (irange
&r
, tree type
,
1018 const irange
&op1
, const irange
&op2
,
1019 relation_kind rel
) const
1021 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GT
))
1024 signop sign
= TYPE_SIGN (op1
.type ());
1025 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1027 if (wi::gt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1028 r
= range_true (type
);
1029 else if (!wi::gt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1030 r
= range_false (type
);
1032 r
= range_true_and_false (type
);
1037 operator_gt::op1_range (irange
&r
, tree type
,
1038 const irange
&lhs
, const irange
&op2
,
1039 relation_kind rel ATTRIBUTE_UNUSED
) const
1041 switch (get_bool_state (r
, lhs
, type
))
1044 build_gt (r
, type
, op2
.lower_bound ());
1048 build_le (r
, type
, op2
.upper_bound ());
1058 operator_gt::op2_range (irange
&r
, tree type
,
1061 relation_kind rel ATTRIBUTE_UNUSED
) const
1063 switch (get_bool_state (r
, lhs
, type
))
1066 build_ge (r
, type
, op1
.lower_bound ());
1070 build_lt (r
, type
, op1
.upper_bound ());
1080 class operator_ge
: public range_operator
1082 using range_operator::fold_range
;
1083 using range_operator::op1_range
;
1084 using range_operator::op2_range
;
1086 virtual bool fold_range (irange
&r
, tree type
,
1089 relation_kind rel
= VREL_VARYING
) const;
1090 virtual bool op1_range (irange
&r
, tree type
,
1093 relation_kind rel
= VREL_VARYING
) const;
1094 virtual bool op2_range (irange
&r
, tree type
,
1097 relation_kind rel
= VREL_VARYING
) const;
1098 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
1101 // Check if the LHS range indicates a relation between OP1 and OP2.
1104 ge_op1_op2_relation (const irange
&lhs
)
1106 if (lhs
.undefined_p ())
1107 return VREL_UNDEFINED
;
1109 // FALSE = op1 >= op2 indicates LT_EXPR.
1113 // TRUE = op1 >= op2 indicates GE_EXPR.
1114 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
1116 return VREL_VARYING
;
1120 operator_ge::op1_op2_relation (const irange
&lhs
) const
1122 return ge_op1_op2_relation (lhs
);
1126 operator_ge::fold_range (irange
&r
, tree type
,
1129 relation_kind rel
) const
1131 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GE
))
1134 signop sign
= TYPE_SIGN (op1
.type ());
1135 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1137 if (wi::ge_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1138 r
= range_true (type
);
1139 else if (!wi::ge_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1140 r
= range_false (type
);
1142 r
= range_true_and_false (type
);
1147 operator_ge::op1_range (irange
&r
, tree type
,
1150 relation_kind rel ATTRIBUTE_UNUSED
) const
1152 switch (get_bool_state (r
, lhs
, type
))
1155 build_ge (r
, type
, op2
.lower_bound ());
1159 build_lt (r
, type
, op2
.upper_bound ());
1169 operator_ge::op2_range (irange
&r
, tree type
,
1172 relation_kind rel ATTRIBUTE_UNUSED
) const
1174 switch (get_bool_state (r
, lhs
, type
))
1177 build_gt (r
, type
, op1
.lower_bound ());
1181 build_le (r
, type
, op1
.upper_bound ());
1191 class operator_plus
: public range_operator
1193 using range_operator::op1_range
;
1194 using range_operator::op2_range
;
1195 using range_operator::lhs_op1_relation
;
1196 using range_operator::lhs_op2_relation
;
1198 virtual bool op1_range (irange
&r
, tree type
,
1201 relation_kind rel ATTRIBUTE_UNUSED
) const;
1202 virtual bool op2_range (irange
&r
, tree type
,
1205 relation_kind rel ATTRIBUTE_UNUSED
) const;
1206 virtual void wi_fold (irange
&r
, tree type
,
1207 const wide_int
&lh_lb
,
1208 const wide_int
&lh_ub
,
1209 const wide_int
&rh_lb
,
1210 const wide_int
&rh_ub
) const;
1211 virtual relation_kind
lhs_op1_relation (const irange
&lhs
, const irange
&op1
,
1213 relation_kind rel
) const;
1214 virtual relation_kind
lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1216 relation_kind rel
) const;
1219 // Check to see if the range of OP2 indicates anything about the relation
1220 // between LHS and OP1.
1223 operator_plus::lhs_op1_relation (const irange
&lhs
,
1226 relation_kind
) const
1228 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
1229 return VREL_VARYING
;
1231 tree type
= lhs
.type ();
1232 unsigned prec
= TYPE_PRECISION (type
);
1233 wi::overflow_type ovf1
, ovf2
;
1234 signop sign
= TYPE_SIGN (type
);
1236 // LHS = OP1 + 0 indicates LHS == OP1.
1240 if (TYPE_OVERFLOW_WRAPS (type
))
1242 wi::add (op1
.lower_bound (), op2
.lower_bound (), sign
, &ovf1
);
1243 wi::add (op1
.upper_bound (), op2
.upper_bound (), sign
, &ovf2
);
1246 ovf1
= ovf2
= wi::OVF_NONE
;
1248 // Never wrapping additions.
1251 // Positive op2 means lhs > op1.
1252 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1254 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1257 // Negative op2 means lhs < op1.
1258 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1260 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1263 // Always wrapping additions.
1264 else if (ovf1
&& ovf1
== ovf2
)
1266 // Positive op2 means lhs < op1.
1267 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1269 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1272 // Negative op2 means lhs > op1.
1273 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1275 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1279 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1280 if (!range_includes_zero_p (&op2
))
1283 return VREL_VARYING
;
1286 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1290 operator_plus::lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1291 const irange
&op2
, relation_kind rel
) const
1293 return lhs_op1_relation (lhs
, op2
, op1
, rel
);
1297 operator_plus::wi_fold (irange
&r
, tree type
,
1298 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1299 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1301 wi::overflow_type ov_lb
, ov_ub
;
1302 signop s
= TYPE_SIGN (type
);
1303 wide_int new_lb
= wi::add (lh_lb
, rh_lb
, s
, &ov_lb
);
1304 wide_int new_ub
= wi::add (lh_ub
, rh_ub
, s
, &ov_ub
);
1305 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1309 operator_plus::op1_range (irange
&r
, tree type
,
1312 relation_kind rel ATTRIBUTE_UNUSED
) const
1314 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
1318 operator_plus::op2_range (irange
&r
, tree type
,
1321 relation_kind rel ATTRIBUTE_UNUSED
) const
1323 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, lhs
, op1
);
1327 class operator_minus
: public range_operator
1329 using range_operator::fold_range
;
1330 using range_operator::op1_range
;
1331 using range_operator::op2_range
;
1333 virtual bool op1_range (irange
&r
, tree type
,
1336 relation_kind rel ATTRIBUTE_UNUSED
) const;
1337 virtual bool op2_range (irange
&r
, tree type
,
1340 relation_kind rel ATTRIBUTE_UNUSED
) const;
1341 virtual void wi_fold (irange
&r
, tree type
,
1342 const wide_int
&lh_lb
,
1343 const wide_int
&lh_ub
,
1344 const wide_int
&rh_lb
,
1345 const wide_int
&rh_ub
) const;
1346 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
1349 relation_kind rel
) const;
1350 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1352 const irange
&op1_range
,
1353 const irange
&op2_range
,
1354 relation_kind rel
) const;
1358 operator_minus::wi_fold (irange
&r
, tree type
,
1359 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1360 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1362 wi::overflow_type ov_lb
, ov_ub
;
1363 signop s
= TYPE_SIGN (type
);
1364 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
1365 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
1366 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1370 // Return the relation between LHS and OP1 based on the relation between
1374 operator_minus::lhs_op1_relation (const irange
&, const irange
&op1
,
1375 const irange
&, relation_kind rel
) const
1377 if (!op1
.undefined_p () && TYPE_SIGN (op1
.type ()) == UNSIGNED
)
1386 return VREL_VARYING
;
1389 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1390 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1391 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1394 minus_op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1395 const irange
&op1_range ATTRIBUTE_UNUSED
,
1396 const irange
&op2_range ATTRIBUTE_UNUSED
,
1399 if (rel
== VREL_VARYING
)
1402 int_range
<2> rel_range
;
1403 unsigned prec
= TYPE_PRECISION (type
);
1404 signop sgn
= TYPE_SIGN (type
);
1406 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1408 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
));
1409 else if (rel
== VREL_NE
)
1410 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1412 else if (TYPE_OVERFLOW_WRAPS (type
))
1416 // For wrapping signed values and unsigned, if op1 > op2 or
1417 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1420 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1431 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1433 rel_range
= int_range
<2> (type
, wi::one (prec
),
1434 wi::max_value (prec
, sgn
));
1436 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1438 rel_range
= int_range
<2> (type
, wi::zero (prec
),
1439 wi::max_value (prec
, sgn
));
1441 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1443 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1444 wi::minus_one (prec
));
1446 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1448 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1455 lhs_range
.intersect (rel_range
);
1460 operator_minus::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1461 const irange
&op1_range
,
1462 const irange
&op2_range
,
1463 relation_kind rel
) const
1465 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1470 operator_minus::op1_range (irange
&r
, tree type
,
1473 relation_kind rel ATTRIBUTE_UNUSED
) const
1475 return range_op_handler (PLUS_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
1479 operator_minus::op2_range (irange
&r
, tree type
,
1482 relation_kind rel ATTRIBUTE_UNUSED
) const
1484 return fold_range (r
, type
, op1
, lhs
);
1488 class operator_pointer_diff
: public range_operator
1490 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1492 const irange
&op1_range
,
1493 const irange
&op2_range
,
1494 relation_kind rel
) const;
1498 operator_pointer_diff::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1499 const irange
&op1_range
,
1500 const irange
&op2_range
,
1501 relation_kind rel
) const
1503 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1508 class operator_min
: public range_operator
1511 virtual void wi_fold (irange
&r
, tree type
,
1512 const wide_int
&lh_lb
,
1513 const wide_int
&lh_ub
,
1514 const wide_int
&rh_lb
,
1515 const wide_int
&rh_ub
) const;
1519 operator_min::wi_fold (irange
&r
, tree type
,
1520 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1521 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1523 signop s
= TYPE_SIGN (type
);
1524 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
1525 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
1526 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1530 class operator_max
: public range_operator
1533 virtual void wi_fold (irange
&r
, tree type
,
1534 const wide_int
&lh_lb
,
1535 const wide_int
&lh_ub
,
1536 const wide_int
&rh_lb
,
1537 const wide_int
&rh_ub
) const;
1541 operator_max::wi_fold (irange
&r
, tree type
,
1542 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1543 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1545 signop s
= TYPE_SIGN (type
);
1546 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
1547 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
1548 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1552 class cross_product_operator
: public range_operator
1555 // Perform an operation between two wide-ints and place the result
1556 // in R. Return true if the operation overflowed.
1557 virtual bool wi_op_overflows (wide_int
&r
,
1560 const wide_int
&) const = 0;
1562 // Calculate the cross product of two sets of sub-ranges and return it.
1563 void wi_cross_product (irange
&r
, tree type
,
1564 const wide_int
&lh_lb
,
1565 const wide_int
&lh_ub
,
1566 const wide_int
&rh_lb
,
1567 const wide_int
&rh_ub
) const;
1570 // Calculate the cross product of two sets of ranges and return it.
1572 // Multiplications, divisions and shifts are a bit tricky to handle,
1573 // depending on the mix of signs we have in the two ranges, we need to
1574 // operate on different values to get the minimum and maximum values
1575 // for the new range. One approach is to figure out all the
1576 // variations of range combinations and do the operations.
1578 // However, this involves several calls to compare_values and it is
1579 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1580 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1581 // figure the smallest and largest values to form the new range.
1584 cross_product_operator::wi_cross_product (irange
&r
, tree type
,
1585 const wide_int
&lh_lb
,
1586 const wide_int
&lh_ub
,
1587 const wide_int
&rh_lb
,
1588 const wide_int
&rh_ub
) const
1590 wide_int cp1
, cp2
, cp3
, cp4
;
1591 // Default to varying.
1592 r
.set_varying (type
);
1594 // Compute the 4 cross operations, bailing if we get an overflow we
1596 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
1598 if (wi::eq_p (lh_lb
, lh_ub
))
1600 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
1602 if (wi::eq_p (rh_lb
, rh_ub
))
1604 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
1606 if (wi::eq_p (lh_lb
, lh_ub
))
1608 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
1612 signop sign
= TYPE_SIGN (type
);
1613 if (wi::gt_p (cp1
, cp2
, sign
))
1614 std::swap (cp1
, cp2
);
1615 if (wi::gt_p (cp3
, cp4
, sign
))
1616 std::swap (cp3
, cp4
);
1618 // Choose min and max from the ordered pairs.
1619 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
1620 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
1621 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
1625 class operator_mult
: public cross_product_operator
1627 using range_operator::op1_range
;
1628 using range_operator::op2_range
;
1630 virtual void wi_fold (irange
&r
, tree type
,
1631 const wide_int
&lh_lb
,
1632 const wide_int
&lh_ub
,
1633 const wide_int
&rh_lb
,
1634 const wide_int
&rh_ub
) const;
1635 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1636 const wide_int
&w0
, const wide_int
&w1
) const;
1637 virtual bool op1_range (irange
&r
, tree type
,
1640 relation_kind rel ATTRIBUTE_UNUSED
) const;
1641 virtual bool op2_range (irange
&r
, tree type
,
1644 relation_kind rel ATTRIBUTE_UNUSED
) const;
1648 operator_mult::op1_range (irange
&r
, tree type
,
1649 const irange
&lhs
, const irange
&op2
,
1650 relation_kind rel ATTRIBUTE_UNUSED
) const
1654 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1655 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1656 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1657 if (TYPE_OVERFLOW_WRAPS (type
))
1660 if (op2
.singleton_p (&offset
) && !integer_zerop (offset
))
1661 return range_op_handler (TRUNC_DIV_EXPR
, type
).fold_range (r
, type
,
1667 operator_mult::op2_range (irange
&r
, tree type
,
1668 const irange
&lhs
, const irange
&op1
,
1669 relation_kind rel
) const
1671 return operator_mult::op1_range (r
, type
, lhs
, op1
, rel
);
1675 operator_mult::wi_op_overflows (wide_int
&res
, tree type
,
1676 const wide_int
&w0
, const wide_int
&w1
) const
1678 wi::overflow_type overflow
= wi::OVF_NONE
;
1679 signop sign
= TYPE_SIGN (type
);
1680 res
= wi::mul (w0
, w1
, sign
, &overflow
);
1681 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1683 // For multiplication, the sign of the overflow is given
1684 // by the comparison of the signs of the operands.
1685 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
1686 res
= wi::max_value (w0
.get_precision (), sign
);
1688 res
= wi::min_value (w0
.get_precision (), sign
);
1695 operator_mult::wi_fold (irange
&r
, tree type
,
1696 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1697 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1699 if (TYPE_OVERFLOW_UNDEFINED (type
))
1701 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1705 // Multiply the ranges when overflow wraps. This is basically fancy
1706 // code so we don't drop to varying with an unsigned
1709 // This test requires 2*prec bits if both operands are signed and
1710 // 2*prec + 2 bits if either is not. Therefore, extend the values
1711 // using the sign of the result to PREC2. From here on out,
1712 // everthing is just signed math no matter what the input types
1715 signop sign
= TYPE_SIGN (type
);
1716 unsigned prec
= TYPE_PRECISION (type
);
1717 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
1718 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
1719 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
1720 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
1721 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
1722 widest2_int size
= sizem1
+ 1;
1724 // Canonicalize the intervals.
1725 if (sign
== UNSIGNED
)
1727 if (wi::ltu_p (size
, min0
+ max0
))
1732 if (wi::ltu_p (size
, min1
+ max1
))
1739 // Sort the 4 products so that min is in prod0 and max is in
1741 widest2_int prod0
= min0
* min1
;
1742 widest2_int prod1
= min0
* max1
;
1743 widest2_int prod2
= max0
* min1
;
1744 widest2_int prod3
= max0
* max1
;
1746 // min0min1 > max0max1
1748 std::swap (prod0
, prod3
);
1750 // min0max1 > max0min1
1752 std::swap (prod1
, prod2
);
1755 std::swap (prod0
, prod1
);
1758 std::swap (prod2
, prod3
);
1761 prod2
= prod3
- prod0
;
1762 if (wi::geu_p (prod2
, sizem1
))
1763 // The range covers all values.
1764 r
.set_varying (type
);
1767 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
1768 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
1769 create_possibly_reversed_range (r
, type
, new_lb
, new_ub
);
1774 class operator_div
: public cross_product_operator
1777 operator_div (enum tree_code c
) { code
= c
; }
1778 virtual void wi_fold (irange
&r
, tree type
,
1779 const wide_int
&lh_lb
,
1780 const wide_int
&lh_ub
,
1781 const wide_int
&rh_lb
,
1782 const wide_int
&rh_ub
) const;
1783 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1784 const wide_int
&, const wide_int
&) const;
1786 enum tree_code code
;
1790 operator_div::wi_op_overflows (wide_int
&res
, tree type
,
1791 const wide_int
&w0
, const wide_int
&w1
) const
1796 wi::overflow_type overflow
= wi::OVF_NONE
;
1797 signop sign
= TYPE_SIGN (type
);
1801 case EXACT_DIV_EXPR
:
1802 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1803 // operator_exact_divide. No need to handle it here.
1806 case TRUNC_DIV_EXPR
:
1807 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
1809 case FLOOR_DIV_EXPR
:
1810 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
1812 case ROUND_DIV_EXPR
:
1813 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
1816 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
1822 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1824 // For division, the only case is -INF / -1 = +INF.
1825 res
= wi::max_value (w0
.get_precision (), sign
);
1832 operator_div::wi_fold (irange
&r
, tree type
,
1833 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1834 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1836 const wide_int dividend_min
= lh_lb
;
1837 const wide_int dividend_max
= lh_ub
;
1838 const wide_int divisor_min
= rh_lb
;
1839 const wide_int divisor_max
= rh_ub
;
1840 signop sign
= TYPE_SIGN (type
);
1841 unsigned prec
= TYPE_PRECISION (type
);
1842 wide_int extra_min
, extra_max
;
1844 // If we know we won't divide by zero, just do the division.
1845 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
1847 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1848 divisor_min
, divisor_max
);
1852 // If we're definitely dividing by zero, there's nothing to do.
1853 if (wi_zero_p (type
, divisor_min
, divisor_max
))
1859 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1860 // skip any division by zero.
1862 // First divide by the negative numbers, if any.
1863 if (wi::neg_p (divisor_min
, sign
))
1864 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1865 divisor_min
, wi::minus_one (prec
));
1869 // Then divide by the non-zero positive numbers, if any.
1870 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
1873 wi_cross_product (tmp
, type
, dividend_min
, dividend_max
,
1874 wi::one (prec
), divisor_max
);
1877 // We shouldn't still have undefined here.
1878 gcc_checking_assert (!r
.undefined_p ());
1881 operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
1882 operator_div
op_floor_div (FLOOR_DIV_EXPR
);
1883 operator_div
op_round_div (ROUND_DIV_EXPR
);
1884 operator_div
op_ceil_div (CEIL_DIV_EXPR
);
1887 class operator_exact_divide
: public operator_div
1889 using range_operator::op1_range
;
1891 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR
) { }
1892 virtual bool op1_range (irange
&r
, tree type
,
1895 relation_kind rel ATTRIBUTE_UNUSED
) const;
1900 operator_exact_divide::op1_range (irange
&r
, tree type
,
1903 relation_kind rel ATTRIBUTE_UNUSED
) const
1906 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1907 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1908 // We wont bother trying to enumerate all the in between stuff :-P
1909 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1910 // the time however.
1911 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1912 if (op2
.singleton_p (&offset
)
1913 && !integer_zerop (offset
))
1914 return range_op_handler (MULT_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
1919 class operator_lshift
: public cross_product_operator
1921 using range_operator::fold_range
;
1922 using range_operator::op1_range
;
1924 virtual bool op1_range (irange
&r
, tree type
,
1927 relation_kind rel
= VREL_VARYING
) const;
1928 virtual bool fold_range (irange
&r
, tree type
,
1931 relation_kind rel
= VREL_VARYING
) const;
1933 virtual void wi_fold (irange
&r
, tree type
,
1934 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1935 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
1936 virtual bool wi_op_overflows (wide_int
&res
,
1939 const wide_int
&) const;
1942 class operator_rshift
: public cross_product_operator
1944 using range_operator::fold_range
;
1945 using range_operator::op1_range
;
1946 using range_operator::lhs_op1_relation
;
1948 virtual bool fold_range (irange
&r
, tree type
,
1951 relation_kind rel
= VREL_VARYING
) const;
1952 virtual void wi_fold (irange
&r
, tree type
,
1953 const wide_int
&lh_lb
,
1954 const wide_int
&lh_ub
,
1955 const wide_int
&rh_lb
,
1956 const wide_int
&rh_ub
) const;
1957 virtual bool wi_op_overflows (wide_int
&res
,
1960 const wide_int
&w1
) const;
1961 virtual bool op1_range (irange
&, tree type
,
1964 relation_kind rel
= VREL_VARYING
) const;
1965 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
1968 relation_kind rel
) const;
1973 operator_rshift::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
1976 relation_kind
) const
1978 // If both operands range are >= 0, then the LHS <= op1.
1979 if (!op1
.undefined_p () && !op2
.undefined_p ()
1980 && wi::ge_p (op1
.lower_bound (), 0, TYPE_SIGN (op1
.type ()))
1981 && wi::ge_p (op2
.lower_bound (), 0, TYPE_SIGN (op2
.type ())))
1983 return VREL_VARYING
;
1987 operator_lshift::fold_range (irange
&r
, tree type
,
1990 relation_kind rel
) const
1992 int_range_max shift_range
;
1993 if (!get_shift_range (shift_range
, type
, op2
))
1995 if (op2
.undefined_p ())
1998 r
.set_varying (type
);
2002 // Transform left shifts by constants into multiplies.
2003 if (shift_range
.singleton_p ())
2005 unsigned shift
= shift_range
.lower_bound ().to_uhwi ();
2006 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
2007 int_range
<1> mult (type
, tmp
, tmp
);
2009 // Force wrapping multiplication.
2010 bool saved_flag_wrapv
= flag_wrapv
;
2011 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
2013 flag_wrapv_pointer
= 1;
2014 bool b
= op_mult
.fold_range (r
, type
, op1
, mult
);
2015 flag_wrapv
= saved_flag_wrapv
;
2016 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
2020 // Otherwise, invoke the generic fold routine.
2021 return range_operator::fold_range (r
, type
, op1
, shift_range
, rel
);
2025 operator_lshift::wi_fold (irange
&r
, tree type
,
2026 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2027 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2029 signop sign
= TYPE_SIGN (type
);
2030 unsigned prec
= TYPE_PRECISION (type
);
2031 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
2032 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
2033 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2034 // overflow. However, for that to happen, rh.max needs to be zero,
2035 // which means rh is a singleton range of zero, which means we simply return
2036 // [lh_lb, lh_ub] as the range.
2037 if (wi::eq_p (rh_ub
, rh_lb
) && wi::eq_p (rh_ub
, 0))
2039 r
= int_range
<2> (type
, lh_lb
, lh_ub
);
2043 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2044 wide_int complement
= ~(bound
- 1);
2045 wide_int low_bound
, high_bound
;
2046 bool in_bounds
= false;
2048 if (sign
== UNSIGNED
)
2051 high_bound
= complement
;
2052 if (wi::ltu_p (lh_ub
, low_bound
))
2054 // [5, 6] << [1, 2] == [10, 24].
2055 // We're shifting out only zeroes, the value increases
2059 else if (wi::ltu_p (high_bound
, lh_lb
))
2061 // [0xffffff00, 0xffffffff] << [1, 2]
2062 // == [0xfffffc00, 0xfffffffe].
2063 // We're shifting out only ones, the value decreases
2070 // [-1, 1] << [1, 2] == [-4, 4]
2071 low_bound
= complement
;
2073 if (wi::lts_p (lh_ub
, high_bound
)
2074 && wi::lts_p (low_bound
, lh_lb
))
2076 // For non-negative numbers, we're shifting out only zeroes,
2077 // the value increases monotonically. For negative numbers,
2078 // we're shifting out only ones, the value decreases
2085 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2087 r
.set_varying (type
);
2091 operator_lshift::wi_op_overflows (wide_int
&res
, tree type
,
2092 const wide_int
&w0
, const wide_int
&w1
) const
2094 signop sign
= TYPE_SIGN (type
);
2097 // It's unclear from the C standard whether shifts can overflow.
2098 // The following code ignores overflow; perhaps a C standard
2099 // interpretation ruling is needed.
2100 res
= wi::rshift (w0
, -w1
, sign
);
2103 res
= wi::lshift (w0
, w1
);
2108 operator_lshift::op1_range (irange
&r
,
2112 relation_kind rel ATTRIBUTE_UNUSED
) const
2116 if (!lhs
.contains_p (build_zero_cst (type
)))
2117 r
.set_nonzero (type
);
2119 r
.set_varying (type
);
2121 if (op2
.singleton_p (&shift_amount
))
2123 wide_int shift
= wi::to_wide (shift_amount
);
2124 if (wi::lt_p (shift
, 0, SIGNED
))
2126 if (wi::ge_p (shift
, wi::uhwi (TYPE_PRECISION (type
),
2127 TYPE_PRECISION (op2
.type ())),
2136 // Work completely in unsigned mode to start.
2138 int_range_max tmp_range
;
2139 if (TYPE_SIGN (type
) == SIGNED
)
2141 int_range_max tmp
= lhs
;
2142 utype
= unsigned_type_for (type
);
2143 range_cast (tmp
, utype
);
2144 op_rshift
.fold_range (tmp_range
, utype
, tmp
, op2
);
2147 op_rshift
.fold_range (tmp_range
, utype
, lhs
, op2
);
2149 // Start with ranges which can produce the LHS by right shifting the
2150 // result by the shift amount.
2151 // ie [0x08, 0xF0] = op1 << 2 will start with
2152 // [00001000, 11110000] = op1 << 2
2153 // [0x02, 0x4C] aka [00000010, 00111100]
2155 // Then create a range from the LB with the least significant upper bit
2156 // set, to the upper bound with all the bits set.
2157 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2159 // Ideally we do this for each subrange, but just lump them all for now.
2160 unsigned low_bits
= TYPE_PRECISION (utype
)
2161 - TREE_INT_CST_LOW (shift_amount
);
2162 wide_int up_mask
= wi::mask (low_bits
, true, TYPE_PRECISION (utype
));
2163 wide_int new_ub
= wi::bit_or (up_mask
, tmp_range
.upper_bound ());
2164 wide_int new_lb
= wi::set_bit (tmp_range
.lower_bound (), low_bits
);
2165 int_range
<2> fill_range (utype
, new_lb
, new_ub
);
2166 tmp_range
.union_ (fill_range
);
2169 range_cast (tmp_range
, type
);
2171 r
.intersect (tmp_range
);
2175 return !r
.varying_p ();
2179 operator_rshift::op1_range (irange
&r
,
2183 relation_kind rel ATTRIBUTE_UNUSED
) const
2186 if (op2
.singleton_p (&shift
))
2188 // Ignore nonsensical shifts.
2189 unsigned prec
= TYPE_PRECISION (type
);
2190 if (wi::ge_p (wi::to_wide (shift
),
2191 wi::uhwi (prec
, TYPE_PRECISION (TREE_TYPE (shift
))),
2194 if (wi::to_wide (shift
) == 0)
2200 // Folding the original operation may discard some impossible
2201 // ranges from the LHS.
2202 int_range_max lhs_refined
;
2203 op_rshift
.fold_range (lhs_refined
, type
, int_range
<1> (type
), op2
);
2204 lhs_refined
.intersect (lhs
);
2205 if (lhs_refined
.undefined_p ())
2210 int_range_max
shift_range (shift
, shift
);
2211 int_range_max lb
, ub
;
2212 op_lshift
.fold_range (lb
, type
, lhs_refined
, shift_range
);
2214 // 0000 0111 = OP1 >> 3
2216 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2217 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2218 // right hand side (0x07).
2219 tree mask
= fold_build1 (BIT_NOT_EXPR
, type
,
2220 fold_build2 (LSHIFT_EXPR
, type
,
2221 build_minus_one_cst (type
),
2223 int_range_max
mask_range (build_zero_cst (type
), mask
);
2224 op_plus
.fold_range (ub
, type
, lb
, mask_range
);
2227 if (!lhs_refined
.contains_p (build_zero_cst (type
)))
2229 mask_range
.invert ();
2230 r
.intersect (mask_range
);
2238 operator_rshift::wi_op_overflows (wide_int
&res
,
2241 const wide_int
&w1
) const
2243 signop sign
= TYPE_SIGN (type
);
2245 res
= wi::lshift (w0
, -w1
);
2248 // It's unclear from the C standard whether shifts can overflow.
2249 // The following code ignores overflow; perhaps a C standard
2250 // interpretation ruling is needed.
2251 res
= wi::rshift (w0
, w1
, sign
);
2257 operator_rshift::fold_range (irange
&r
, tree type
,
2260 relation_kind rel
) const
2262 int_range_max shift
;
2263 if (!get_shift_range (shift
, type
, op2
))
2265 if (op2
.undefined_p ())
2268 r
.set_varying (type
);
2272 return range_operator::fold_range (r
, type
, op1
, shift
, rel
);
2276 operator_rshift::wi_fold (irange
&r
, tree type
,
2277 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2278 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2280 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2284 class operator_cast
: public range_operator
2286 using range_operator::fold_range
;
2287 using range_operator::op1_range
;
2289 virtual bool fold_range (irange
&r
, tree type
,
2292 relation_kind rel
= VREL_VARYING
) const;
2293 virtual bool op1_range (irange
&r
, tree type
,
2296 relation_kind rel
= VREL_VARYING
) const;
2298 bool truncating_cast_p (const irange
&inner
, const irange
&outer
) const;
2299 bool inside_domain_p (const wide_int
&min
, const wide_int
&max
,
2300 const irange
&outer
) const;
2301 void fold_pair (irange
&r
, unsigned index
, const irange
&inner
,
2302 const irange
&outer
) const;
2305 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2308 operator_cast::truncating_cast_p (const irange
&inner
,
2309 const irange
&outer
) const
2311 return TYPE_PRECISION (outer
.type ()) < TYPE_PRECISION (inner
.type ());
2314 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2317 operator_cast::inside_domain_p (const wide_int
&min
,
2318 const wide_int
&max
,
2319 const irange
&range
) const
2321 wide_int domain_min
= wi::to_wide (vrp_val_min (range
.type ()));
2322 wide_int domain_max
= wi::to_wide (vrp_val_max (range
.type ()));
2323 signop domain_sign
= TYPE_SIGN (range
.type ());
2324 return (wi::le_p (min
, domain_max
, domain_sign
)
2325 && wi::le_p (max
, domain_max
, domain_sign
)
2326 && wi::ge_p (min
, domain_min
, domain_sign
)
2327 && wi::ge_p (max
, domain_min
, domain_sign
));
2331 // Helper for fold_range which work on a pair at a time.
2334 operator_cast::fold_pair (irange
&r
, unsigned index
,
2335 const irange
&inner
,
2336 const irange
&outer
) const
2338 tree inner_type
= inner
.type ();
2339 tree outer_type
= outer
.type ();
2340 signop inner_sign
= TYPE_SIGN (inner_type
);
2341 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2343 // check to see if casting from INNER to OUTER is a conversion that
2344 // fits in the resulting OUTER type.
2345 wide_int inner_lb
= inner
.lower_bound (index
);
2346 wide_int inner_ub
= inner
.upper_bound (index
);
2347 if (truncating_cast_p (inner
, outer
))
2349 // We may be able to accomodate a truncating cast if the
2350 // resulting range can be represented in the target type...
2351 if (wi::rshift (wi::sub (inner_ub
, inner_lb
),
2352 wi::uhwi (outer_prec
, TYPE_PRECISION (inner
.type ())),
2355 r
.set_varying (outer_type
);
2359 // ...but we must still verify that the final range fits in the
2360 // domain. This catches -fstrict-enum restrictions where the domain
2361 // range is smaller than what fits in the underlying type.
2362 wide_int min
= wide_int::from (inner_lb
, outer_prec
, inner_sign
);
2363 wide_int max
= wide_int::from (inner_ub
, outer_prec
, inner_sign
);
2364 if (inside_domain_p (min
, max
, outer
))
2365 create_possibly_reversed_range (r
, outer_type
, min
, max
);
2367 r
.set_varying (outer_type
);
2372 operator_cast::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2373 const irange
&inner
,
2374 const irange
&outer
,
2375 relation_kind rel ATTRIBUTE_UNUSED
) const
2377 if (empty_range_varying (r
, type
, inner
, outer
))
2380 gcc_checking_assert (outer
.varying_p ());
2381 gcc_checking_assert (inner
.num_pairs () > 0);
2383 // Avoid a temporary by folding the first pair directly into the result.
2384 fold_pair (r
, 0, inner
, outer
);
2386 // Then process any additonal pairs by unioning with their results.
2387 for (unsigned x
= 1; x
< inner
.num_pairs (); ++x
)
2390 fold_pair (tmp
, x
, inner
, outer
);
2399 operator_cast::op1_range (irange
&r
, tree type
,
2402 relation_kind rel ATTRIBUTE_UNUSED
) const
2404 tree lhs_type
= lhs
.type ();
2405 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
2407 // If we are calculating a pointer, shortcut to what we really care about.
2408 if (POINTER_TYPE_P (type
))
2410 // Conversion from other pointers or a constant (including 0/NULL)
2411 // are straightforward.
2412 if (POINTER_TYPE_P (lhs
.type ())
2413 || (lhs
.singleton_p ()
2414 && TYPE_PRECISION (lhs
.type ()) >= TYPE_PRECISION (type
)))
2417 range_cast (r
, type
);
2421 // If the LHS is not a pointer nor a singleton, then it is
2422 // either VARYING or non-zero.
2423 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
2424 r
.set_nonzero (type
);
2426 r
.set_varying (type
);
2432 if (truncating_cast_p (op2
, lhs
))
2434 if (lhs
.varying_p ())
2435 r
.set_varying (type
);
2438 // We want to insert the LHS as an unsigned value since it
2439 // would not trigger the signed bit of the larger type.
2440 int_range_max converted_lhs
= lhs
;
2441 range_cast (converted_lhs
, unsigned_type_for (lhs_type
));
2442 range_cast (converted_lhs
, type
);
2443 // Start by building the positive signed outer range for the type.
2444 wide_int lim
= wi::set_bit_in_zero (TYPE_PRECISION (lhs_type
),
2445 TYPE_PRECISION (type
));
2446 r
= int_range
<1> (type
, lim
, wi::max_value (TYPE_PRECISION (type
),
2448 // For the signed part, we need to simply union the 2 ranges now.
2449 r
.union_ (converted_lhs
);
2451 // Create maximal negative number outside of LHS bits.
2452 lim
= wi::mask (TYPE_PRECISION (lhs_type
), true,
2453 TYPE_PRECISION (type
));
2454 // Add this to the unsigned LHS range(s).
2455 int_range_max
lim_range (type
, lim
, lim
);
2456 int_range_max lhs_neg
;
2457 range_op_handler (PLUS_EXPR
, type
).fold_range (lhs_neg
, type
,
2460 // lhs_neg now has all the negative versions of the LHS.
2461 // Now union in all the values from SIGNED MIN (0x80000) to
2462 // lim-1 in order to fill in all the ranges with the upper
2465 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2466 // we don't need to create a range from min to lim-1
2467 // calculate neg range traps trying to create [lim, lim - 1].
2468 wide_int min_val
= wi::min_value (TYPE_PRECISION (type
), SIGNED
);
2471 int_range_max
neg (type
,
2472 wi::min_value (TYPE_PRECISION (type
),
2475 lhs_neg
.union_ (neg
);
2477 // And finally, munge the signed and unsigned portions.
2480 // And intersect with any known value passed in the extra operand.
2486 if (TYPE_PRECISION (lhs_type
) == TYPE_PRECISION (type
))
2490 // The cast is not truncating, and the range is restricted to
2491 // the range of the RHS by this assignment.
2493 // Cast the range of the RHS to the type of the LHS.
2494 fold_range (tmp
, lhs_type
, int_range
<1> (type
), int_range
<1> (lhs_type
));
2495 // Intersect this with the LHS range will produce the range,
2496 // which will be cast to the RHS type before returning.
2497 tmp
.intersect (lhs
);
2500 // Cast the calculated range to the type of the RHS.
2501 fold_range (r
, type
, tmp
, int_range
<1> (type
));
2506 class operator_logical_and
: public range_operator
2508 using range_operator::fold_range
;
2509 using range_operator::op1_range
;
2510 using range_operator::op2_range
;
2512 virtual bool fold_range (irange
&r
, tree type
,
2515 relation_kind rel
= VREL_VARYING
) const;
2516 virtual bool op1_range (irange
&r
, tree type
,
2519 relation_kind rel
= VREL_VARYING
) const;
2520 virtual bool op2_range (irange
&r
, tree type
,
2523 relation_kind rel
= VREL_VARYING
) const;
2528 operator_logical_and::fold_range (irange
&r
, tree type
,
2531 relation_kind rel ATTRIBUTE_UNUSED
) const
2533 if (empty_range_varying (r
, type
, lh
, rh
))
2536 // 0 && anything is 0.
2537 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
2538 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
2539 r
= range_false (type
);
2540 else if (lh
.contains_p (build_zero_cst (lh
.type ()))
2541 || rh
.contains_p (build_zero_cst (rh
.type ())))
2542 // To reach this point, there must be a logical 1 on each side, and
2543 // the only remaining question is whether there is a zero or not.
2544 r
= range_true_and_false (type
);
2546 r
= range_true (type
);
2551 operator_logical_and::op1_range (irange
&r
, tree type
,
2553 const irange
&op2 ATTRIBUTE_UNUSED
,
2554 relation_kind rel ATTRIBUTE_UNUSED
) const
2556 switch (get_bool_state (r
, lhs
, type
))
2559 // A true result means both sides of the AND must be true.
2560 r
= range_true (type
);
2563 // Any other result means only one side has to be false, the
2564 // other side can be anything. So we cannot be sure of any
2566 r
= range_true_and_false (type
);
2573 operator_logical_and::op2_range (irange
&r
, tree type
,
2576 relation_kind rel ATTRIBUTE_UNUSED
) const
2578 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
2582 class operator_bitwise_and
: public range_operator
2584 using range_operator::fold_range
;
2585 using range_operator::op1_range
;
2586 using range_operator::op2_range
;
2588 virtual bool fold_range (irange
&r
, tree type
,
2591 relation_kind rel
= VREL_VARYING
) const;
2592 virtual bool op1_range (irange
&r
, tree type
,
2595 relation_kind rel
= VREL_VARYING
) const;
2596 virtual bool op2_range (irange
&r
, tree type
,
2599 relation_kind rel
= VREL_VARYING
) const;
2600 virtual void wi_fold (irange
&r
, tree type
,
2601 const wide_int
&lh_lb
,
2602 const wide_int
&lh_ub
,
2603 const wide_int
&rh_lb
,
2604 const wide_int
&rh_ub
) const;
2606 void simple_op1_range_solver (irange
&r
, tree type
,
2608 const irange
&op2
) const;
2612 operator_bitwise_and::fold_range (irange
&r
, tree type
,
2615 relation_kind rel ATTRIBUTE_UNUSED
) const
2617 if (range_operator::fold_range (r
, type
, lh
, rh
))
2619 if (!lh
.undefined_p () && !rh
.undefined_p ())
2620 r
.set_nonzero_bits (wi::bit_and (lh
.get_nonzero_bits (),
2621 rh
.get_nonzero_bits ()));
2628 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2629 // by considering the number of leading redundant sign bit copies.
2630 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2631 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2633 wi_optimize_signed_bitwise_op (irange
&r
, tree type
,
2634 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2635 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2637 int lh_clrsb
= MIN (wi::clrsb (lh_lb
), wi::clrsb (lh_ub
));
2638 int rh_clrsb
= MIN (wi::clrsb (rh_lb
), wi::clrsb (rh_ub
));
2639 int new_clrsb
= MIN (lh_clrsb
, rh_clrsb
);
2642 int type_prec
= TYPE_PRECISION (type
);
2643 int rprec
= (type_prec
- new_clrsb
) - 1;
2644 value_range_with_overflow (r
, type
,
2645 wi::mask (rprec
, true, type_prec
),
2646 wi::mask (rprec
, false, type_prec
));
2651 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2652 // possible. Basically, see if we can optimize:
2656 // [LB op Z, UB op Z]
2658 // If the optimization was successful, accumulate the range in R and
2662 wi_optimize_and_or (irange
&r
,
2663 enum tree_code code
,
2665 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2666 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2668 // Calculate the singleton mask among the ranges, if any.
2669 wide_int lower_bound
, upper_bound
, mask
;
2670 if (wi::eq_p (rh_lb
, rh_ub
))
2673 lower_bound
= lh_lb
;
2674 upper_bound
= lh_ub
;
2676 else if (wi::eq_p (lh_lb
, lh_ub
))
2679 lower_bound
= rh_lb
;
2680 upper_bound
= rh_ub
;
2685 // If Z is a constant which (for op | its bitwise not) has n
2686 // consecutive least significant bits cleared followed by m 1
2687 // consecutive bits set immediately above it and either
2688 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2690 // The least significant n bits of all the values in the range are
2691 // cleared or set, the m bits above it are preserved and any bits
2692 // above these are required to be the same for all values in the
2696 if (code
== BIT_IOR_EXPR
)
2698 if (wi::eq_p (w
, 0))
2699 n
= w
.get_precision ();
2703 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2704 if (wi::eq_p (w
, 0))
2705 m
= w
.get_precision () - n
;
2707 m
= wi::ctz (w
) - n
;
2709 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2710 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
2713 wide_int res_lb
, res_ub
;
2714 if (code
== BIT_AND_EXPR
)
2716 res_lb
= wi::bit_and (lower_bound
, mask
);
2717 res_ub
= wi::bit_and (upper_bound
, mask
);
2719 else if (code
== BIT_IOR_EXPR
)
2721 res_lb
= wi::bit_or (lower_bound
, mask
);
2722 res_ub
= wi::bit_or (upper_bound
, mask
);
2726 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
2728 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2729 if (code
== BIT_IOR_EXPR
&& wi::ne_p (mask
, 0))
2732 tmp
.set_nonzero (type
);
2738 // For range [LB, UB] compute two wide_int bit masks.
2740 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2741 // for all numbers in the range the bit is 0, otherwise it might be 0
2744 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2745 // for all numbers in the range the bit is 1, otherwise it might be 0
2749 wi_set_zero_nonzero_bits (tree type
,
2750 const wide_int
&lb
, const wide_int
&ub
,
2751 wide_int
&maybe_nonzero
,
2752 wide_int
&mustbe_nonzero
)
2754 signop sign
= TYPE_SIGN (type
);
2756 if (wi::eq_p (lb
, ub
))
2757 maybe_nonzero
= mustbe_nonzero
= lb
;
2758 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
2760 wide_int xor_mask
= lb
^ ub
;
2761 maybe_nonzero
= lb
| ub
;
2762 mustbe_nonzero
= lb
& ub
;
2765 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2766 maybe_nonzero
.get_precision ());
2767 maybe_nonzero
= maybe_nonzero
| mask
;
2768 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
2773 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
2774 mustbe_nonzero
= wi::zero (lb
.get_precision ());
2779 operator_bitwise_and::wi_fold (irange
&r
, tree type
,
2780 const wide_int
&lh_lb
,
2781 const wide_int
&lh_ub
,
2782 const wide_int
&rh_lb
,
2783 const wide_int
&rh_ub
) const
2785 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
2788 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
2789 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
2790 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
2791 maybe_nonzero_lh
, mustbe_nonzero_lh
);
2792 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
2793 maybe_nonzero_rh
, mustbe_nonzero_rh
);
2795 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
2796 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
2797 signop sign
= TYPE_SIGN (type
);
2798 unsigned prec
= TYPE_PRECISION (type
);
2799 // If both input ranges contain only negative values, we can
2800 // truncate the result range maximum to the minimum of the
2801 // input range maxima.
2802 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
2804 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2805 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2807 // If either input range contains only non-negative values
2808 // we can truncate the result range maximum to the respective
2809 // maximum of the input range.
2810 if (wi::ge_p (lh_lb
, 0, sign
))
2811 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2812 if (wi::ge_p (rh_lb
, 0, sign
))
2813 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2814 // PR68217: In case of signed & sign-bit-CST should
2815 // result in [-INF, 0] instead of [-INF, INF].
2816 if (wi::gt_p (new_lb
, new_ub
, sign
))
2818 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
2820 && ((wi::eq_p (lh_lb
, lh_ub
)
2821 && !wi::cmps (lh_lb
, sign_bit
))
2822 || (wi::eq_p (rh_lb
, rh_ub
)
2823 && !wi::cmps (rh_lb
, sign_bit
))))
2825 new_lb
= wi::min_value (prec
, sign
);
2826 new_ub
= wi::zero (prec
);
2829 // If the limits got swapped around, return varying.
2830 if (wi::gt_p (new_lb
, new_ub
,sign
))
2833 && wi_optimize_signed_bitwise_op (r
, type
,
2837 r
.set_varying (type
);
2840 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2844 set_nonzero_range_from_mask (irange
&r
, tree type
, const irange
&lhs
)
2846 if (!lhs
.contains_p (build_zero_cst (type
)))
2847 r
= range_nonzero (type
);
2849 r
.set_varying (type
);
2852 // This was shamelessly stolen from register_edge_assert_for_2 and
2853 // adjusted to work with iranges.
2856 operator_bitwise_and::simple_op1_range_solver (irange
&r
, tree type
,
2858 const irange
&op2
) const
2860 if (!op2
.singleton_p ())
2862 set_nonzero_range_from_mask (r
, type
, lhs
);
2865 unsigned int nprec
= TYPE_PRECISION (type
);
2866 wide_int cst2v
= op2
.lower_bound ();
2867 bool cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (type
));
2870 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
2872 sgnbit
= wi::zero (nprec
);
2874 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2876 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2877 // maximum unsigned value is ~0. For signed comparison, if CST2
2878 // doesn't have the most significant bit set, handle it similarly. If
2879 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2880 wide_int valv
= lhs
.lower_bound ();
2881 wide_int minv
= valv
& cst2v
, maxv
;
2882 bool we_know_nothing
= false;
2885 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2886 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2889 // If we can't determine anything on this bound, fall
2890 // through and conservatively solve for the other end point.
2891 we_know_nothing
= true;
2894 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
2895 if (we_know_nothing
)
2896 r
.set_varying (type
);
2898 r
= int_range
<1> (type
, minv
, maxv
);
2900 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2902 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2903 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2905 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2907 // For signed comparison, if CST2 doesn't have most significant bit
2908 // set, handle it similarly. If CST2 has MSB set, the maximum is
2909 // the same and minimum is INT_MIN.
2910 valv
= lhs
.upper_bound ();
2911 minv
= valv
& cst2v
;
2916 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2919 // If we couldn't determine anything on either bound, return
2921 if (we_know_nothing
)
2929 int_range
<1> upper_bits (type
, minv
, maxv
);
2930 r
.intersect (upper_bits
);
2934 operator_bitwise_and::op1_range (irange
&r
, tree type
,
2937 relation_kind rel ATTRIBUTE_UNUSED
) const
2939 if (types_compatible_p (type
, boolean_type_node
))
2940 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
2943 for (unsigned i
= 0; i
< lhs
.num_pairs (); ++i
)
2945 int_range_max
chunk (lhs
.type (),
2946 lhs
.lower_bound (i
),
2947 lhs
.upper_bound (i
));
2949 simple_op1_range_solver (res
, type
, chunk
, op2
);
2952 if (r
.undefined_p ())
2953 set_nonzero_range_from_mask (r
, type
, lhs
);
2958 operator_bitwise_and::op2_range (irange
&r
, tree type
,
2961 relation_kind rel ATTRIBUTE_UNUSED
) const
2963 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
2967 class operator_logical_or
: public range_operator
2969 using range_operator::fold_range
;
2970 using range_operator::op1_range
;
2971 using range_operator::op2_range
;
2973 virtual bool fold_range (irange
&r
, tree type
,
2976 relation_kind rel
= VREL_VARYING
) const;
2977 virtual bool op1_range (irange
&r
, tree type
,
2980 relation_kind rel
= VREL_VARYING
) const;
2981 virtual bool op2_range (irange
&r
, tree type
,
2984 relation_kind rel
= VREL_VARYING
) const;
2988 operator_logical_or::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2991 relation_kind rel ATTRIBUTE_UNUSED
) const
2993 if (empty_range_varying (r
, type
, lh
, rh
))
3002 operator_logical_or::op1_range (irange
&r
, tree type
,
3004 const irange
&op2 ATTRIBUTE_UNUSED
,
3005 relation_kind rel ATTRIBUTE_UNUSED
) const
3007 switch (get_bool_state (r
, lhs
, type
))
3010 // A false result means both sides of the OR must be false.
3011 r
= range_false (type
);
3014 // Any other result means only one side has to be true, the
3015 // other side can be anything. so we can't be sure of any result
3017 r
= range_true_and_false (type
);
3024 operator_logical_or::op2_range (irange
&r
, tree type
,
3027 relation_kind rel ATTRIBUTE_UNUSED
) const
3029 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
3033 class operator_bitwise_or
: public range_operator
3035 using range_operator::op1_range
;
3036 using range_operator::op2_range
;
3038 virtual bool op1_range (irange
&r
, tree type
,
3041 relation_kind rel
= VREL_VARYING
) const;
3042 virtual bool op2_range (irange
&r
, tree type
,
3045 relation_kind rel
= VREL_VARYING
) const;
3046 virtual void wi_fold (irange
&r
, tree type
,
3047 const wide_int
&lh_lb
,
3048 const wide_int
&lh_ub
,
3049 const wide_int
&rh_lb
,
3050 const wide_int
&rh_ub
) const;
3054 operator_bitwise_or::wi_fold (irange
&r
, tree type
,
3055 const wide_int
&lh_lb
,
3056 const wide_int
&lh_ub
,
3057 const wide_int
&rh_lb
,
3058 const wide_int
&rh_ub
) const
3060 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3063 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3064 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3065 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3066 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3067 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3068 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3069 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
3070 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
3071 signop sign
= TYPE_SIGN (type
);
3072 // If the input ranges contain only positive values we can
3073 // truncate the minimum of the result range to the maximum
3074 // of the input range minima.
3075 if (wi::ge_p (lh_lb
, 0, sign
)
3076 && wi::ge_p (rh_lb
, 0, sign
))
3078 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3079 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3081 // If either input range contains only negative values
3082 // we can truncate the minimum of the result range to the
3083 // respective minimum range.
3084 if (wi::lt_p (lh_ub
, 0, sign
))
3085 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3086 if (wi::lt_p (rh_ub
, 0, sign
))
3087 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3088 // If the limits got swapped around, return a conservative range.
3089 if (wi::gt_p (new_lb
, new_ub
, sign
))
3091 // Make sure that nonzero|X is nonzero.
3092 if (wi::gt_p (lh_lb
, 0, sign
)
3093 || wi::gt_p (rh_lb
, 0, sign
)
3094 || wi::lt_p (lh_ub
, 0, sign
)
3095 || wi::lt_p (rh_ub
, 0, sign
))
3096 r
.set_nonzero (type
);
3097 else if (sign
== SIGNED
3098 && wi_optimize_signed_bitwise_op (r
, type
,
3103 r
.set_varying (type
);
3106 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3110 operator_bitwise_or::op1_range (irange
&r
, tree type
,
3113 relation_kind rel ATTRIBUTE_UNUSED
) const
3115 // If this is really a logical wi_fold, call that.
3116 if (types_compatible_p (type
, boolean_type_node
))
3117 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
3121 tree zero
= build_zero_cst (type
);
3122 r
= int_range
<1> (zero
, zero
);
3125 r
.set_varying (type
);
3130 operator_bitwise_or::op2_range (irange
&r
, tree type
,
3133 relation_kind rel ATTRIBUTE_UNUSED
) const
3135 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
3139 class operator_bitwise_xor
: public range_operator
3141 using range_operator::op1_range
;
3142 using range_operator::op2_range
;
3144 virtual void wi_fold (irange
&r
, tree type
,
3145 const wide_int
&lh_lb
,
3146 const wide_int
&lh_ub
,
3147 const wide_int
&rh_lb
,
3148 const wide_int
&rh_ub
) const;
3149 virtual bool op1_range (irange
&r
, tree type
,
3152 relation_kind rel
= VREL_VARYING
) const;
3153 virtual bool op2_range (irange
&r
, tree type
,
3156 relation_kind rel
= VREL_VARYING
) const;
3157 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
3159 const irange
&op1_range
,
3160 const irange
&op2_range
,
3161 relation_kind rel
) const;
3165 operator_bitwise_xor::wi_fold (irange
&r
, tree type
,
3166 const wide_int
&lh_lb
,
3167 const wide_int
&lh_ub
,
3168 const wide_int
&rh_lb
,
3169 const wide_int
&rh_ub
) const
3171 signop sign
= TYPE_SIGN (type
);
3172 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3173 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3174 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3175 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3176 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3177 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3179 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
3180 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
3181 wide_int result_one_bits
3182 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
3183 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
3184 wide_int new_ub
= ~result_zero_bits
;
3185 wide_int new_lb
= result_one_bits
;
3187 // If the range has all positive or all negative values, the result
3188 // is better than VARYING.
3189 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
3190 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3191 else if (sign
== SIGNED
3192 && wi_optimize_signed_bitwise_op (r
, type
,
3197 r
.set_varying (type
);
3199 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3200 if (wi::lt_p (lh_ub
, rh_lb
, sign
)
3201 || wi::lt_p (rh_ub
, lh_lb
, sign
)
3202 || wi::ne_p (result_one_bits
, 0))
3205 tmp
.set_nonzero (type
);
3211 operator_bitwise_xor::op1_op2_relation_effect (irange
&lhs_range
,
3215 relation_kind rel
) const
3217 if (rel
== VREL_VARYING
)
3220 int_range
<2> rel_range
;
3225 rel_range
.set_zero (type
);
3228 rel_range
.set_nonzero (type
);
3234 lhs_range
.intersect (rel_range
);
3239 operator_bitwise_xor::op1_range (irange
&r
, tree type
,
3242 relation_kind rel ATTRIBUTE_UNUSED
) const
3244 if (lhs
.undefined_p () || lhs
.varying_p ())
3249 if (types_compatible_p (type
, boolean_type_node
))
3251 switch (get_bool_state (r
, lhs
, type
))
3254 if (op2
.varying_p ())
3255 r
.set_varying (type
);
3256 else if (op2
.zero_p ())
3257 r
= range_true (type
);
3259 r
= range_false (type
);
3269 r
.set_varying (type
);
3274 operator_bitwise_xor::op2_range (irange
&r
, tree type
,
3277 relation_kind rel ATTRIBUTE_UNUSED
) const
3279 return operator_bitwise_xor::op1_range (r
, type
, lhs
, op1
);
3282 class operator_trunc_mod
: public range_operator
3284 using range_operator::op1_range
;
3285 using range_operator::op2_range
;
3287 virtual void wi_fold (irange
&r
, tree type
,
3288 const wide_int
&lh_lb
,
3289 const wide_int
&lh_ub
,
3290 const wide_int
&rh_lb
,
3291 const wide_int
&rh_ub
) const;
3292 virtual bool op1_range (irange
&r
, tree type
,
3295 relation_kind rel ATTRIBUTE_UNUSED
) const;
3296 virtual bool op2_range (irange
&r
, tree type
,
3299 relation_kind rel ATTRIBUTE_UNUSED
) const;
3303 operator_trunc_mod::wi_fold (irange
&r
, tree type
,
3304 const wide_int
&lh_lb
,
3305 const wide_int
&lh_ub
,
3306 const wide_int
&rh_lb
,
3307 const wide_int
&rh_ub
) const
3309 wide_int new_lb
, new_ub
, tmp
;
3310 signop sign
= TYPE_SIGN (type
);
3311 unsigned prec
= TYPE_PRECISION (type
);
3313 // Mod 0 is undefined.
3314 if (wi_zero_p (type
, rh_lb
, rh_ub
))
3320 // Check for constant and try to fold.
3321 if (lh_lb
== lh_ub
&& rh_lb
== rh_ub
)
3323 wi::overflow_type ov
= wi::OVF_NONE
;
3324 tmp
= wi::mod_trunc (lh_lb
, rh_lb
, sign
, &ov
);
3325 if (ov
== wi::OVF_NONE
)
3327 r
= int_range
<2> (type
, tmp
, tmp
);
3332 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3337 new_ub
= wi::smax (new_ub
, tmp
);
3340 if (sign
== UNSIGNED
)
3341 new_lb
= wi::zero (prec
);
3346 if (wi::gts_p (tmp
, 0))
3347 tmp
= wi::zero (prec
);
3348 new_lb
= wi::smax (new_lb
, tmp
);
3351 if (sign
== SIGNED
&& wi::neg_p (tmp
))
3352 tmp
= wi::zero (prec
);
3353 new_ub
= wi::min (new_ub
, tmp
, sign
);
3355 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3359 operator_trunc_mod::op1_range (irange
&r
, tree type
,
3362 relation_kind rel ATTRIBUTE_UNUSED
) const
3365 signop sign
= TYPE_SIGN (type
);
3366 unsigned prec
= TYPE_PRECISION (type
);
3367 // (a % b) >= x && x > 0 , then a >= x.
3368 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3370 r
= value_range (type
, lhs
.lower_bound (), wi::max_value (prec
, sign
));
3373 // (a % b) <= x && x < 0 , then a <= x.
3374 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3376 r
= value_range (type
, wi::min_value (prec
, sign
), lhs
.upper_bound ());
3383 operator_trunc_mod::op2_range (irange
&r
, tree type
,
3386 relation_kind rel ATTRIBUTE_UNUSED
) const
3389 signop sign
= TYPE_SIGN (type
);
3390 unsigned prec
= TYPE_PRECISION (type
);
3391 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3392 // or b > x for unsigned.
3393 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3396 r
= value_range (type
, wi::neg (lhs
.lower_bound ()),
3397 lhs
.lower_bound (), VR_ANTI_RANGE
);
3398 else if (wi::lt_p (lhs
.lower_bound (), wi::max_value (prec
, sign
),
3400 r
= value_range (type
, lhs
.lower_bound () + 1,
3401 wi::max_value (prec
, sign
));
3406 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3407 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3409 if (wi::gt_p (lhs
.upper_bound (), wi::min_value (prec
, sign
), sign
))
3410 r
= value_range (type
, lhs
.upper_bound (),
3411 wi::neg (lhs
.upper_bound ()), VR_ANTI_RANGE
);
3420 class operator_logical_not
: public range_operator
3422 using range_operator::fold_range
;
3423 using range_operator::op1_range
;
3425 virtual bool fold_range (irange
&r
, tree type
,
3428 relation_kind rel
= VREL_VARYING
) const;
3429 virtual bool op1_range (irange
&r
, tree type
,
3432 relation_kind rel
= VREL_VARYING
) const;
3435 // Folding a logical NOT, oddly enough, involves doing nothing on the
3436 // forward pass through. During the initial walk backwards, the
3437 // logical NOT reversed the desired outcome on the way back, so on the
3438 // way forward all we do is pass the range forward.
3443 // to determine the TRUE branch, walking backward
3444 // if (b_3) if ([1,1])
3445 // b_3 = !b_2 [1,1] = ![0,0]
3446 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3447 // which is the result we are looking for.. so.. pass it through.
3450 operator_logical_not::fold_range (irange
&r
, tree type
,
3452 const irange
&rh ATTRIBUTE_UNUSED
,
3453 relation_kind rel ATTRIBUTE_UNUSED
) const
3455 if (empty_range_varying (r
, type
, lh
, rh
))
3459 if (!lh
.varying_p () && !lh
.undefined_p ())
3466 operator_logical_not::op1_range (irange
&r
,
3470 relation_kind rel ATTRIBUTE_UNUSED
) const
3472 // Logical NOT is involutary...do it again.
3473 return fold_range (r
, type
, lhs
, op2
);
3477 class operator_bitwise_not
: public range_operator
3479 using range_operator::fold_range
;
3480 using range_operator::op1_range
;
3482 virtual bool fold_range (irange
&r
, tree type
,
3485 relation_kind rel
= VREL_VARYING
) const;
3486 virtual bool op1_range (irange
&r
, tree type
,
3489 relation_kind rel
= VREL_VARYING
) const;
3493 operator_bitwise_not::fold_range (irange
&r
, tree type
,
3496 relation_kind rel ATTRIBUTE_UNUSED
) const
3498 if (empty_range_varying (r
, type
, lh
, rh
))
3501 if (types_compatible_p (type
, boolean_type_node
))
3502 return op_logical_not
.fold_range (r
, type
, lh
, rh
);
3504 // ~X is simply -1 - X.
3505 int_range
<1> minusone (type
, wi::minus_one (TYPE_PRECISION (type
)),
3506 wi::minus_one (TYPE_PRECISION (type
)));
3507 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, minusone
, lh
);
3511 operator_bitwise_not::op1_range (irange
&r
, tree type
,
3514 relation_kind rel ATTRIBUTE_UNUSED
) const
3516 if (types_compatible_p (type
, boolean_type_node
))
3517 return op_logical_not
.op1_range (r
, type
, lhs
, op2
);
3519 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3520 return fold_range (r
, type
, lhs
, op2
);
3524 class operator_cst
: public range_operator
3526 using range_operator::fold_range
;
3528 virtual bool fold_range (irange
&r
, tree type
,
3531 relation_kind rel
= VREL_VARYING
) const;
3535 operator_cst::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3537 const irange
&rh ATTRIBUTE_UNUSED
,
3538 relation_kind rel ATTRIBUTE_UNUSED
) const
3545 class operator_identity
: public range_operator
3547 using range_operator::fold_range
;
3548 using range_operator::op1_range
;
3549 using range_operator::lhs_op1_relation
;
3551 virtual bool fold_range (irange
&r
, tree type
,
3554 relation_kind rel
= VREL_VARYING
) const;
3555 virtual bool op1_range (irange
&r
, tree type
,
3558 relation_kind rel
= VREL_VARYING
) const;
3559 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
3562 relation_kind rel
) const;
3565 // Determine if there is a relationship between LHS and OP1.
3568 operator_identity::lhs_op1_relation (const irange
&lhs
,
3569 const irange
&op1 ATTRIBUTE_UNUSED
,
3570 const irange
&op2 ATTRIBUTE_UNUSED
,
3571 relation_kind
) const
3573 if (lhs
.undefined_p ())
3574 return VREL_VARYING
;
3575 // Simply a copy, so they are equivalent.
3580 operator_identity::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3582 const irange
&rh ATTRIBUTE_UNUSED
,
3583 relation_kind rel ATTRIBUTE_UNUSED
) const
3590 operator_identity::op1_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3592 const irange
&op2 ATTRIBUTE_UNUSED
,
3593 relation_kind rel ATTRIBUTE_UNUSED
) const
3600 class operator_unknown
: public range_operator
3602 using range_operator::fold_range
;
3604 virtual bool fold_range (irange
&r
, tree type
,
3607 relation_kind rel
= VREL_VARYING
) const;
3611 operator_unknown::fold_range (irange
&r
, tree type
,
3612 const irange
&lh ATTRIBUTE_UNUSED
,
3613 const irange
&rh ATTRIBUTE_UNUSED
,
3614 relation_kind rel ATTRIBUTE_UNUSED
) const
3616 r
.set_varying (type
);
3621 class operator_abs
: public range_operator
3623 using range_operator::op1_range
;
3625 virtual void wi_fold (irange
&r
, tree type
,
3626 const wide_int
&lh_lb
,
3627 const wide_int
&lh_ub
,
3628 const wide_int
&rh_lb
,
3629 const wide_int
&rh_ub
) const;
3630 virtual bool op1_range (irange
&r
, tree type
,
3633 relation_kind rel ATTRIBUTE_UNUSED
) const;
3637 operator_abs::wi_fold (irange
&r
, tree type
,
3638 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3639 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3640 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3643 signop sign
= TYPE_SIGN (type
);
3644 unsigned prec
= TYPE_PRECISION (type
);
3646 // Pass through LH for the easy cases.
3647 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
3649 r
= int_range
<1> (type
, lh_lb
, lh_ub
);
3653 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3655 wide_int min_value
= wi::min_value (prec
, sign
);
3656 wide_int max_value
= wi::max_value (prec
, sign
);
3657 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
3659 r
.set_varying (type
);
3663 // ABS_EXPR may flip the range around, if the original range
3664 // included negative values.
3665 if (wi::eq_p (lh_lb
, min_value
))
3667 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3668 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3669 if (wi::eq_p (lh_ub
, min_value
))
3671 r
= int_range
<1> (type
, min_value
, min_value
);
3677 min
= wi::abs (lh_lb
);
3679 if (wi::eq_p (lh_ub
, min_value
))
3682 max
= wi::abs (lh_ub
);
3684 // If the range contains zero then we know that the minimum value in the
3685 // range will be zero.
3686 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
3688 if (wi::gt_p (min
, max
, sign
))
3690 min
= wi::zero (prec
);
3694 // If the range was reversed, swap MIN and MAX.
3695 if (wi::gt_p (min
, max
, sign
))
3696 std::swap (min
, max
);
3699 // If the new range has its limits swapped around (MIN > MAX), then
3700 // the operation caused one of them to wrap around. The only thing
3701 // we know is that the result is positive.
3702 if (wi::gt_p (min
, max
, sign
))
3704 min
= wi::zero (prec
);
3707 r
= int_range
<1> (type
, min
, max
);
3711 operator_abs::op1_range (irange
&r
, tree type
,
3714 relation_kind rel ATTRIBUTE_UNUSED
) const
3716 if (empty_range_varying (r
, type
, lhs
, op2
))
3718 if (TYPE_UNSIGNED (type
))
3723 // Start with the positives because negatives are an impossible result.
3724 int_range_max positives
= range_positives (type
);
3725 positives
.intersect (lhs
);
3727 // Then add the negative of each pair:
3728 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3729 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
3730 r
.union_ (int_range
<1> (type
,
3731 -positives
.upper_bound (i
),
3732 -positives
.lower_bound (i
)));
3733 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3734 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3735 wide_int min_value
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
3736 wide_int lb
= lhs
.lower_bound ();
3737 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lb
, min_value
))
3738 r
.union_ (int_range
<2> (type
, lb
, lb
));
3743 class operator_absu
: public range_operator
3746 virtual void wi_fold (irange
&r
, tree type
,
3747 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3748 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3752 operator_absu::wi_fold (irange
&r
, tree type
,
3753 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3754 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3755 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3757 wide_int new_lb
, new_ub
;
3759 // Pass through VR0 the easy cases.
3760 if (wi::ges_p (lh_lb
, 0))
3767 new_lb
= wi::abs (lh_lb
);
3768 new_ub
= wi::abs (lh_ub
);
3770 // If the range contains zero then we know that the minimum
3771 // value in the range will be zero.
3772 if (wi::ges_p (lh_ub
, 0))
3774 if (wi::gtu_p (new_lb
, new_ub
))
3776 new_lb
= wi::zero (TYPE_PRECISION (type
));
3779 std::swap (new_lb
, new_ub
);
3782 gcc_checking_assert (TYPE_UNSIGNED (type
));
3783 r
= int_range
<1> (type
, new_lb
, new_ub
);
3787 class operator_negate
: public range_operator
3789 using range_operator::fold_range
;
3790 using range_operator::op1_range
;
3792 virtual bool fold_range (irange
&r
, tree type
,
3795 relation_kind rel
= VREL_VARYING
) const;
3796 virtual bool op1_range (irange
&r
, tree type
,
3799 relation_kind rel
= VREL_VARYING
) const;
3803 operator_negate::fold_range (irange
&r
, tree type
,
3806 relation_kind rel ATTRIBUTE_UNUSED
) const
3808 if (empty_range_varying (r
, type
, lh
, rh
))
3810 // -X is simply 0 - X.
3811 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
,
3812 range_zero (type
), lh
);
3816 operator_negate::op1_range (irange
&r
, tree type
,
3819 relation_kind rel ATTRIBUTE_UNUSED
) const
3821 // NEGATE is involutory.
3822 return fold_range (r
, type
, lhs
, op2
);
3826 class operator_addr_expr
: public range_operator
3828 using range_operator::fold_range
;
3829 using range_operator::op1_range
;
3831 virtual bool fold_range (irange
&r
, tree type
,
3834 relation_kind rel
= VREL_VARYING
) const;
3835 virtual bool op1_range (irange
&r
, tree type
,
3838 relation_kind rel
= VREL_VARYING
) const;
3842 operator_addr_expr::fold_range (irange
&r
, tree type
,
3845 relation_kind rel ATTRIBUTE_UNUSED
) const
3847 if (empty_range_varying (r
, type
, lh
, rh
))
3850 // Return a non-null pointer of the LHS type (passed in op2).
3852 r
= range_zero (type
);
3853 else if (!lh
.contains_p (build_zero_cst (lh
.type ())))
3854 r
= range_nonzero (type
);
3856 r
.set_varying (type
);
3861 operator_addr_expr::op1_range (irange
&r
, tree type
,
3864 relation_kind rel ATTRIBUTE_UNUSED
) const
3866 return operator_addr_expr::fold_range (r
, type
, lhs
, op2
);
3870 class pointer_plus_operator
: public range_operator
3873 virtual void wi_fold (irange
&r
, tree type
,
3874 const wide_int
&lh_lb
,
3875 const wide_int
&lh_ub
,
3876 const wide_int
&rh_lb
,
3877 const wide_int
&rh_ub
) const;
3881 pointer_plus_operator::wi_fold (irange
&r
, tree type
,
3882 const wide_int
&lh_lb
,
3883 const wide_int
&lh_ub
,
3884 const wide_int
&rh_lb
,
3885 const wide_int
&rh_ub
) const
3887 // Check for [0,0] + const, and simply return the const.
3888 if (lh_lb
== 0 && lh_ub
== 0 && rh_lb
== rh_ub
)
3890 tree val
= wide_int_to_tree (type
, rh_lb
);
3895 // For pointer types, we are really only interested in asserting
3896 // whether the expression evaluates to non-NULL.
3898 // With -fno-delete-null-pointer-checks we need to be more
3899 // conservative. As some object might reside at address 0,
3900 // then some offset could be added to it and the same offset
3901 // subtracted again and the result would be NULL.
3903 // static int a[12]; where &a[0] is NULL and
3906 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3907 // where the first range doesn't include zero and the second one
3908 // doesn't either. As the second operand is sizetype (unsigned),
3909 // consider all ranges where the MSB could be set as possible
3910 // subtractions where the result might be NULL.
3911 if ((!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3912 || !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3913 && !TYPE_OVERFLOW_WRAPS (type
)
3914 && (flag_delete_null_pointer_checks
3915 || !wi::sign_mask (rh_ub
)))
3916 r
= range_nonzero (type
);
3917 else if (lh_lb
== lh_ub
&& lh_lb
== 0
3918 && rh_lb
== rh_ub
&& rh_lb
== 0)
3919 r
= range_zero (type
);
3921 r
.set_varying (type
);
3925 class pointer_min_max_operator
: public range_operator
3928 virtual void wi_fold (irange
& r
, tree type
,
3929 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3930 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3934 pointer_min_max_operator::wi_fold (irange
&r
, tree type
,
3935 const wide_int
&lh_lb
,
3936 const wide_int
&lh_ub
,
3937 const wide_int
&rh_lb
,
3938 const wide_int
&rh_ub
) const
3940 // For MIN/MAX expressions with pointers, we only care about
3941 // nullness. If both are non null, then the result is nonnull.
3942 // If both are null, then the result is null. Otherwise they
3944 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3945 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3946 r
= range_nonzero (type
);
3947 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
3948 r
= range_zero (type
);
3950 r
.set_varying (type
);
3954 class pointer_and_operator
: public range_operator
3957 virtual void wi_fold (irange
&r
, tree type
,
3958 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3959 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3963 pointer_and_operator::wi_fold (irange
&r
, tree type
,
3964 const wide_int
&lh_lb
,
3965 const wide_int
&lh_ub
,
3966 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3967 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3969 // For pointer types, we are really only interested in asserting
3970 // whether the expression evaluates to non-NULL.
3971 if (wi_zero_p (type
, lh_lb
, lh_ub
) || wi_zero_p (type
, lh_lb
, lh_ub
))
3972 r
= range_zero (type
);
3974 r
.set_varying (type
);
3978 class pointer_or_operator
: public range_operator
3980 using range_operator::op1_range
;
3981 using range_operator::op2_range
;
3983 virtual bool op1_range (irange
&r
, tree type
,
3986 relation_kind rel
= VREL_VARYING
) const;
3987 virtual bool op2_range (irange
&r
, tree type
,
3990 relation_kind rel
= VREL_VARYING
) const;
3991 virtual void wi_fold (irange
&r
, tree type
,
3992 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3993 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3997 pointer_or_operator::op1_range (irange
&r
, tree type
,
3999 const irange
&op2 ATTRIBUTE_UNUSED
,
4000 relation_kind rel ATTRIBUTE_UNUSED
) const
4004 tree zero
= build_zero_cst (type
);
4005 r
= int_range
<1> (zero
, zero
);
4008 r
.set_varying (type
);
4013 pointer_or_operator::op2_range (irange
&r
, tree type
,
4016 relation_kind rel ATTRIBUTE_UNUSED
) const
4018 return pointer_or_operator::op1_range (r
, type
, lhs
, op1
);
4022 pointer_or_operator::wi_fold (irange
&r
, tree type
,
4023 const wide_int
&lh_lb
,
4024 const wide_int
&lh_ub
,
4025 const wide_int
&rh_lb
,
4026 const wide_int
&rh_ub
) const
4028 // For pointer types, we are really only interested in asserting
4029 // whether the expression evaluates to non-NULL.
4030 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
4031 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
4032 r
= range_nonzero (type
);
4033 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
4034 r
= range_zero (type
);
4036 r
.set_varying (type
);
4039 // Return a pointer to the range_operator instance, if there is one
4040 // associated with tree_code CODE.
4043 range_op_table::operator[] (enum tree_code code
)
4045 gcc_checking_assert (code
> 0 && code
< MAX_TREE_CODES
);
4046 return m_range_tree
[code
];
4049 // Add OP to the handler table for CODE.
4052 range_op_table::set (enum tree_code code
, range_operator
&op
)
4054 gcc_checking_assert (m_range_tree
[code
] == NULL
);
4055 m_range_tree
[code
] = &op
;
4058 // Instantiate a range op table for integral operations.
4060 class integral_table
: public range_op_table
4064 } integral_tree_table
;
4066 integral_table::integral_table ()
4068 set (EQ_EXPR
, op_equal
);
4069 set (NE_EXPR
, op_not_equal
);
4070 set (LT_EXPR
, op_lt
);
4071 set (LE_EXPR
, op_le
);
4072 set (GT_EXPR
, op_gt
);
4073 set (GE_EXPR
, op_ge
);
4074 set (PLUS_EXPR
, op_plus
);
4075 set (MINUS_EXPR
, op_minus
);
4076 set (MIN_EXPR
, op_min
);
4077 set (MAX_EXPR
, op_max
);
4078 set (MULT_EXPR
, op_mult
);
4079 set (TRUNC_DIV_EXPR
, op_trunc_div
);
4080 set (FLOOR_DIV_EXPR
, op_floor_div
);
4081 set (ROUND_DIV_EXPR
, op_round_div
);
4082 set (CEIL_DIV_EXPR
, op_ceil_div
);
4083 set (EXACT_DIV_EXPR
, op_exact_div
);
4084 set (LSHIFT_EXPR
, op_lshift
);
4085 set (RSHIFT_EXPR
, op_rshift
);
4086 set (NOP_EXPR
, op_convert
);
4087 set (CONVERT_EXPR
, op_convert
);
4088 set (TRUTH_AND_EXPR
, op_logical_and
);
4089 set (BIT_AND_EXPR
, op_bitwise_and
);
4090 set (TRUTH_OR_EXPR
, op_logical_or
);
4091 set (BIT_IOR_EXPR
, op_bitwise_or
);
4092 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4093 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
4094 set (TRUTH_NOT_EXPR
, op_logical_not
);
4095 set (BIT_NOT_EXPR
, op_bitwise_not
);
4096 set (INTEGER_CST
, op_integer_cst
);
4097 set (SSA_NAME
, op_identity
);
4098 set (PAREN_EXPR
, op_identity
);
4099 set (OBJ_TYPE_REF
, op_identity
);
4100 set (IMAGPART_EXPR
, op_unknown
);
4101 set (REALPART_EXPR
, op_unknown
);
4102 set (POINTER_DIFF_EXPR
, op_pointer_diff
);
4103 set (ABS_EXPR
, op_abs
);
4104 set (ABSU_EXPR
, op_absu
);
4105 set (NEGATE_EXPR
, op_negate
);
4106 set (ADDR_EXPR
, op_addr
);
4109 // Instantiate a range op table for pointer operations.
4111 class pointer_table
: public range_op_table
4115 } pointer_tree_table
;
4117 pointer_table::pointer_table ()
4119 set (BIT_AND_EXPR
, op_pointer_and
);
4120 set (BIT_IOR_EXPR
, op_pointer_or
);
4121 set (MIN_EXPR
, op_ptr_min_max
);
4122 set (MAX_EXPR
, op_ptr_min_max
);
4123 set (POINTER_PLUS_EXPR
, op_pointer_plus
);
4125 set (EQ_EXPR
, op_equal
);
4126 set (NE_EXPR
, op_not_equal
);
4127 set (LT_EXPR
, op_lt
);
4128 set (LE_EXPR
, op_le
);
4129 set (GT_EXPR
, op_gt
);
4130 set (GE_EXPR
, op_ge
);
4131 set (SSA_NAME
, op_identity
);
4132 set (INTEGER_CST
, op_integer_cst
);
4133 set (ADDR_EXPR
, op_addr
);
4134 set (NOP_EXPR
, op_convert
);
4135 set (CONVERT_EXPR
, op_convert
);
4137 set (BIT_NOT_EXPR
, op_bitwise_not
);
4138 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4141 // The tables are hidden and accessed via a simple extern function.
4143 static inline range_operator
*
4144 get_handler (enum tree_code code
, tree type
)
4146 // First check if there is a pointer specialization.
4147 if (POINTER_TYPE_P (type
))
4148 return pointer_tree_table
[code
];
4149 if (INTEGRAL_TYPE_P (type
))
4150 return integral_tree_table
[code
];
4154 // Return the floating point operator for CODE or NULL if none available.
4156 static inline range_operator_float
*
4157 get_float_handler (enum tree_code code
, tree
)
4159 return (*floating_tree_table
)[code
];
4162 range_op_handler::range_op_handler (tree_code code
, tree type
)
4163 : m_code (code
), m_type (type
)
4167 range_op_handler::range_op_handler (const gimple
*s
)
4169 if (const gassign
*ass
= dyn_cast
<const gassign
*> (s
))
4171 m_code
= gimple_assign_rhs_code (ass
);
4172 // The LHS of a comparison is always an int, so we must look at
4174 if (TREE_CODE_CLASS (m_code
) == tcc_comparison
)
4175 m_type
= TREE_TYPE (gimple_assign_rhs1 (ass
));
4177 m_type
= TREE_TYPE (gimple_assign_lhs (ass
));
4179 else if (const gcond
*cond
= dyn_cast
<const gcond
*> (s
))
4181 m_code
= gimple_cond_code (cond
);
4182 m_type
= TREE_TYPE (gimple_cond_lhs (cond
));
4186 // A null type means there is no handler for this combination,
4187 // but the decision whether there is one or not, is delayed
4188 // until operator bool below is queried.
4194 // Return TRUE if there is a handler available for the current
4195 // combination of tree_code and type.
4197 range_op_handler::operator bool () const
4201 if (frange::supports_p (m_type
))
4202 return get_float_handler (m_code
, m_type
);
4203 return get_handler (m_code
, m_type
);
4207 range_op_handler::fold_range (vrange
&r
, tree type
,
4210 relation_kind rel
) const
4212 if (irange::supports_p (m_type
))
4214 range_operator
*op
= get_handler (m_code
, m_type
);
4215 return op
->fold_range (as_a
<irange
> (r
), type
,
4217 as_a
<irange
> (rh
), rel
);
4219 if (frange::supports_p (m_type
))
4221 range_operator_float
*op
= get_float_handler (m_code
, m_type
);
4222 if (is_a
<irange
> (r
))
4223 return op
->fold_range (as_a
<irange
> (r
), type
,
4225 as_a
<frange
> (rh
), rel
);
4226 return op
->fold_range (as_a
<frange
> (r
), type
,
4228 as_a
<frange
> (rh
), rel
);
4235 range_op_handler::op1_range (vrange
&r
, tree type
,
4238 relation_kind rel
) const
4240 if (irange::supports_p (m_type
))
4242 range_operator
*op
= get_handler (m_code
, m_type
);
4243 return op
->op1_range (as_a
<irange
> (r
), type
,
4244 as_a
<irange
> (lhs
),
4245 as_a
<irange
> (op2
), rel
);
4247 if (frange::supports_p (m_type
))
4249 range_operator_float
*op
= get_float_handler (m_code
, m_type
);
4250 if (is_a
<irange
> (lhs
))
4251 return op
->op1_range (as_a
<frange
> (r
), type
,
4252 as_a
<irange
> (lhs
),
4253 as_a
<frange
> (op2
), rel
);
4254 return op
->op1_range (as_a
<frange
> (r
), type
,
4255 as_a
<frange
> (lhs
),
4256 as_a
<frange
> (op2
), rel
);
4263 range_op_handler::op2_range (vrange
&r
, tree type
,
4266 relation_kind rel
) const
4268 if (irange::supports_p (m_type
))
4270 range_operator
*op
= get_handler (m_code
, m_type
);
4271 return op
->op2_range (as_a
<irange
> (r
), type
,
4272 as_a
<irange
> (lhs
),
4273 as_a
<irange
> (op1
), rel
);
4275 if (frange::supports_p (m_type
))
4277 range_operator_float
*op
= get_float_handler (m_code
, m_type
);
4278 if (is_a
<irange
> (lhs
))
4279 return op
->op2_range (as_a
<frange
> (r
), type
,
4280 as_a
<irange
> (lhs
),
4281 as_a
<frange
> (op1
), rel
);
4282 return op
->op2_range (as_a
<frange
> (r
), type
,
4283 as_a
<frange
> (lhs
),
4284 as_a
<frange
> (op1
), rel
);
4291 range_op_handler::lhs_op1_relation (const vrange
&lhs
,
4294 relation_kind rel
) const
4296 if (irange::supports_p (m_type
))
4298 range_operator
*op
= get_handler (m_code
, m_type
);
4299 return op
->lhs_op1_relation (as_a
<irange
> (lhs
),
4300 as_a
<irange
> (op1
),
4301 as_a
<irange
> (op2
), rel
);
4303 if (frange::supports_p (m_type
))
4305 range_operator_float
*op
= get_float_handler (m_code
, m_type
);
4306 if (is_a
<irange
> (lhs
))
4307 return op
->lhs_op1_relation (as_a
<irange
> (lhs
),
4308 as_a
<frange
> (op1
),
4309 as_a
<frange
> (op2
), rel
);
4310 return op
->lhs_op1_relation (as_a
<frange
> (lhs
),
4311 as_a
<frange
> (op1
),
4312 as_a
<frange
> (op2
), rel
);
4315 return VREL_VARYING
;
4319 range_op_handler::lhs_op2_relation (const vrange
&lhs
,
4322 relation_kind rel
) const
4324 if (irange::supports_p (m_type
))
4326 range_operator
*op
= get_handler (m_code
, m_type
);
4327 return op
->lhs_op2_relation (as_a
<irange
> (lhs
),
4328 as_a
<irange
> (op1
),
4329 as_a
<irange
> (op2
), rel
);
4331 if (frange::supports_p (m_type
))
4333 range_operator_float
*op
= get_float_handler (m_code
, m_type
);
4334 if (is_a
<irange
> (lhs
))
4335 return op
->lhs_op2_relation (as_a
<irange
> (lhs
),
4336 as_a
<frange
> (op1
),
4337 as_a
<frange
> (op2
), rel
);
4338 return op
->lhs_op2_relation (as_a
<frange
> (lhs
),
4339 as_a
<frange
> (op1
),
4340 as_a
<frange
> (op2
), rel
);
4343 return VREL_VARYING
;
4347 range_op_handler::op1_op2_relation (const vrange
&lhs
) const
4349 if (irange::supports_p (m_type
))
4351 range_operator
*op
= get_handler (m_code
, m_type
);
4352 return op
->op1_op2_relation (as_a
<irange
> (lhs
));
4354 if (frange::supports_p (m_type
))
4356 range_operator_float
*op
= get_float_handler (m_code
, m_type
);
4357 return op
->op1_op2_relation (as_a
<irange
> (lhs
));
4360 return VREL_VARYING
;
4363 // Cast the range in R to TYPE.
4366 range_cast (vrange
&r
, tree type
)
4368 Value_Range
tmp (r
);
4369 Value_Range
varying (type
);
4370 varying
.set_varying (type
);
4371 range_op_handler
op (CONVERT_EXPR
, type
);
4372 // Call op_convert, if it fails, the result is varying.
4373 if (!op
|| !op
.fold_range (r
, type
, tmp
, varying
))
4375 r
.set_varying (type
);
4382 #include "selftest.h"
4386 #define INT(N) build_int_cst (integer_type_node, (N))
4387 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4388 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4389 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4390 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4391 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4394 range_op_cast_tests ()
4396 int_range
<1> r0
, r1
, r2
, rold
;
4397 r0
.set_varying (integer_type_node
);
4398 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
4400 // If a range is in any way outside of the range for the converted
4401 // to range, default to the range for the new type.
4402 r0
.set_varying (short_integer_type_node
);
4403 tree minshort
= wide_int_to_tree (short_integer_type_node
, r0
.lower_bound ());
4404 tree maxshort
= wide_int_to_tree (short_integer_type_node
, r0
.upper_bound ());
4405 if (TYPE_PRECISION (TREE_TYPE (maxint
))
4406 > TYPE_PRECISION (short_integer_type_node
))
4408 r1
= int_range
<1> (integer_zero_node
, maxint
);
4409 range_cast (r1
, short_integer_type_node
);
4410 ASSERT_TRUE (r1
.lower_bound () == wi::to_wide (minshort
)
4411 && r1
.upper_bound() == wi::to_wide (maxshort
));
4414 // (unsigned char)[-5,-1] => [251,255].
4415 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (-1));
4416 range_cast (r0
, unsigned_char_type_node
);
4417 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (251), UCHAR (255)));
4418 range_cast (r0
, signed_char_type_node
);
4419 ASSERT_TRUE (r0
== rold
);
4421 // (signed char)[15, 150] => [-128,-106][15,127].
4422 r0
= rold
= int_range
<1> (UCHAR (15), UCHAR (150));
4423 range_cast (r0
, signed_char_type_node
);
4424 r1
= int_range
<1> (SCHAR (15), SCHAR (127));
4425 r2
= int_range
<1> (SCHAR (-128), SCHAR (-106));
4427 ASSERT_TRUE (r1
== r0
);
4428 range_cast (r0
, unsigned_char_type_node
);
4429 ASSERT_TRUE (r0
== rold
);
4431 // (unsigned char)[-5, 5] => [0,5][251,255].
4432 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (5));
4433 range_cast (r0
, unsigned_char_type_node
);
4434 r1
= int_range
<1> (UCHAR (251), UCHAR (255));
4435 r2
= int_range
<1> (UCHAR (0), UCHAR (5));
4437 ASSERT_TRUE (r0
== r1
);
4438 range_cast (r0
, signed_char_type_node
);
4439 ASSERT_TRUE (r0
== rold
);
4441 // (unsigned char)[-5,5] => [0,5][251,255].
4442 r0
= int_range
<1> (INT (-5), INT (5));
4443 range_cast (r0
, unsigned_char_type_node
);
4444 r1
= int_range
<1> (UCHAR (0), UCHAR (5));
4445 r1
.union_ (int_range
<1> (UCHAR (251), UCHAR (255)));
4446 ASSERT_TRUE (r0
== r1
);
4448 // (unsigned char)[5U,1974U] => [0,255].
4449 r0
= int_range
<1> (UINT (5), UINT (1974));
4450 range_cast (r0
, unsigned_char_type_node
);
4451 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (0), UCHAR (255)));
4452 range_cast (r0
, integer_type_node
);
4453 // Going to a wider range should not sign extend.
4454 ASSERT_TRUE (r0
== int_range
<1> (INT (0), INT (255)));
4456 // (unsigned char)[-350,15] => [0,255].
4457 r0
= int_range
<1> (INT (-350), INT (15));
4458 range_cast (r0
, unsigned_char_type_node
);
4459 ASSERT_TRUE (r0
== (int_range
<1>
4460 (TYPE_MIN_VALUE (unsigned_char_type_node
),
4461 TYPE_MAX_VALUE (unsigned_char_type_node
))));
4463 // Casting [-120,20] from signed char to unsigned short.
4464 // => [0, 20][0xff88, 0xffff].
4465 r0
= int_range
<1> (SCHAR (-120), SCHAR (20));
4466 range_cast (r0
, short_unsigned_type_node
);
4467 r1
= int_range
<1> (UINT16 (0), UINT16 (20));
4468 r2
= int_range
<1> (UINT16 (0xff88), UINT16 (0xffff));
4470 ASSERT_TRUE (r0
== r1
);
4471 // A truncating cast back to signed char will work because [-120, 20]
4472 // is representable in signed char.
4473 range_cast (r0
, signed_char_type_node
);
4474 ASSERT_TRUE (r0
== int_range
<1> (SCHAR (-120), SCHAR (20)));
4476 // unsigned char -> signed short
4477 // (signed short)[(unsigned char)25, (unsigned char)250]
4478 // => [(signed short)25, (signed short)250]
4479 r0
= rold
= int_range
<1> (UCHAR (25), UCHAR (250));
4480 range_cast (r0
, short_integer_type_node
);
4481 r1
= int_range
<1> (INT16 (25), INT16 (250));
4482 ASSERT_TRUE (r0
== r1
);
4483 range_cast (r0
, unsigned_char_type_node
);
4484 ASSERT_TRUE (r0
== rold
);
4486 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4487 r0
= int_range
<1> (TYPE_MIN_VALUE (long_long_integer_type_node
),
4488 TYPE_MAX_VALUE (long_long_integer_type_node
));
4489 range_cast (r0
, short_unsigned_type_node
);
4490 r1
= int_range
<1> (TYPE_MIN_VALUE (short_unsigned_type_node
),
4491 TYPE_MAX_VALUE (short_unsigned_type_node
));
4492 ASSERT_TRUE (r0
== r1
);
4494 // Casting NONZERO to a narrower type will wrap/overflow so
4495 // it's just the entire range for the narrower type.
4497 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4498 // is outside of the range of a smaller range, return the full
4500 if (TYPE_PRECISION (integer_type_node
)
4501 > TYPE_PRECISION (short_integer_type_node
))
4503 r0
= range_nonzero (integer_type_node
);
4504 range_cast (r0
, short_integer_type_node
);
4505 r1
= int_range
<1> (TYPE_MIN_VALUE (short_integer_type_node
),
4506 TYPE_MAX_VALUE (short_integer_type_node
));
4507 ASSERT_TRUE (r0
== r1
);
4510 // Casting NONZERO from a narrower signed to a wider signed.
4512 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4513 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4514 r0
= range_nonzero (short_integer_type_node
);
4515 range_cast (r0
, integer_type_node
);
4516 r1
= int_range
<1> (INT (-32768), INT (-1));
4517 r2
= int_range
<1> (INT (1), INT (32767));
4519 ASSERT_TRUE (r0
== r1
);
4523 range_op_lshift_tests ()
4525 // Test that 0x808.... & 0x8.... still contains 0x8....
4526 // for a large set of numbers.
4529 tree big_type
= long_long_unsigned_type_node
;
4530 // big_num = 0x808,0000,0000,0000
4531 tree big_num
= fold_build2 (LSHIFT_EXPR
, big_type
,
4532 build_int_cst (big_type
, 0x808),
4533 build_int_cst (big_type
, 48));
4534 op_bitwise_and
.fold_range (res
, big_type
,
4535 int_range
<1> (big_type
),
4536 int_range
<1> (big_num
, big_num
));
4537 // val = 0x8,0000,0000,0000
4538 tree val
= fold_build2 (LSHIFT_EXPR
, big_type
,
4539 build_int_cst (big_type
, 0x8),
4540 build_int_cst (big_type
, 48));
4541 ASSERT_TRUE (res
.contains_p (val
));
4544 if (TYPE_PRECISION (unsigned_type_node
) > 31)
4546 // unsigned VARYING = op1 << 1 should be VARYING.
4547 int_range
<2> lhs (unsigned_type_node
);
4548 int_range
<2> shift (INT (1), INT (1));
4550 op_lshift
.op1_range (op1
, unsigned_type_node
, lhs
, shift
);
4551 ASSERT_TRUE (op1
.varying_p ());
4553 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4554 int_range
<2> zero (UINT (0), UINT (0));
4555 op_lshift
.op1_range (op1
, unsigned_type_node
, zero
, shift
);
4556 ASSERT_TRUE (op1
.num_pairs () == 2);
4557 // Remove the [0,0] range.
4558 op1
.intersect (zero
);
4559 ASSERT_TRUE (op1
.num_pairs () == 1);
4560 // op1 << 1 should be [0x8000,0x8000] << 1,
4561 // which should result in [0,0].
4562 int_range_max result
;
4563 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4564 ASSERT_TRUE (result
== zero
);
4566 // signed VARYING = op1 << 1 should be VARYING.
4567 if (TYPE_PRECISION (integer_type_node
) > 31)
4569 // unsigned VARYING = op1 << 1 hould be VARYING.
4570 int_range
<2> lhs (integer_type_node
);
4571 int_range
<2> shift (INT (1), INT (1));
4573 op_lshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4574 ASSERT_TRUE (op1
.varying_p ());
4576 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4577 int_range
<2> zero (INT (0), INT (0));
4578 op_lshift
.op1_range (op1
, integer_type_node
, zero
, shift
);
4579 ASSERT_TRUE (op1
.num_pairs () == 2);
4580 // Remove the [0,0] range.
4581 op1
.intersect (zero
);
4582 ASSERT_TRUE (op1
.num_pairs () == 1);
4583 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4584 // which should result in [0,0].
4585 int_range_max result
;
4586 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4587 ASSERT_TRUE (result
== zero
);
4592 range_op_rshift_tests ()
4594 // unsigned: [3, MAX] = OP1 >> 1
4596 int_range_max
lhs (build_int_cst (unsigned_type_node
, 3),
4597 TYPE_MAX_VALUE (unsigned_type_node
));
4598 int_range_max
one (build_one_cst (unsigned_type_node
),
4599 build_one_cst (unsigned_type_node
));
4601 op_rshift
.op1_range (op1
, unsigned_type_node
, lhs
, one
);
4602 ASSERT_FALSE (op1
.contains_p (UINT (3)));
4605 // signed: [3, MAX] = OP1 >> 1
4607 int_range_max
lhs (INT (3), TYPE_MAX_VALUE (integer_type_node
));
4608 int_range_max
one (INT (1), INT (1));
4610 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4611 ASSERT_FALSE (op1
.contains_p (INT (-2)));
4614 // This is impossible, so OP1 should be [].
4615 // signed: [MIN, MIN] = OP1 >> 1
4617 int_range_max
lhs (TYPE_MIN_VALUE (integer_type_node
),
4618 TYPE_MIN_VALUE (integer_type_node
));
4619 int_range_max
one (INT (1), INT (1));
4621 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4622 ASSERT_TRUE (op1
.undefined_p ());
4625 // signed: ~[-1] = OP1 >> 31
4626 if (TYPE_PRECISION (integer_type_node
) > 31)
4628 int_range_max
lhs (INT (-1), INT (-1), VR_ANTI_RANGE
);
4629 int_range_max
shift (INT (31), INT (31));
4631 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4632 int_range_max negatives
= range_negatives (integer_type_node
);
4633 negatives
.intersect (op1
);
4634 ASSERT_TRUE (negatives
.undefined_p ());
4639 range_op_bitwise_and_tests ()
4642 tree min
= vrp_val_min (integer_type_node
);
4643 tree max
= vrp_val_max (integer_type_node
);
4644 tree tiny
= fold_build2 (PLUS_EXPR
, integer_type_node
, min
,
4645 build_one_cst (integer_type_node
));
4646 int_range_max
i1 (tiny
, max
);
4647 int_range_max
i2 (build_int_cst (integer_type_node
, 255),
4648 build_int_cst (integer_type_node
, 255));
4650 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4651 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4652 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4654 // VARYING = OP1 & 255: OP1 is VARYING
4655 i1
= int_range
<1> (integer_type_node
);
4656 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4657 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4659 // (NONZERO | X) is nonzero.
4660 i1
.set_nonzero (integer_type_node
);
4661 i2
.set_varying (integer_type_node
);
4662 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4663 ASSERT_TRUE (res
.nonzero_p ());
4665 // (NEGATIVE | X) is nonzero.
4666 i1
= int_range
<1> (INT (-5), INT (-3));
4667 i2
.set_varying (integer_type_node
);
4668 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4669 ASSERT_FALSE (res
.contains_p (INT (0)));
4673 range_relational_tests ()
4675 int_range
<2> lhs (unsigned_char_type_node
);
4676 int_range
<2> op1 (UCHAR (8), UCHAR (10));
4677 int_range
<2> op2 (UCHAR (20), UCHAR (20));
4679 // Never wrapping additions mean LHS > OP1.
4680 relation_kind code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4681 ASSERT_TRUE (code
== VREL_GT
);
4683 // Most wrapping additions mean nothing...
4684 op1
= int_range
<2> (UCHAR (8), UCHAR (10));
4685 op2
= int_range
<2> (UCHAR (0), UCHAR (255));
4686 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4687 ASSERT_TRUE (code
== VREL_VARYING
);
4689 // However, always wrapping additions mean LHS < OP1.
4690 op1
= int_range
<2> (UCHAR (1), UCHAR (255));
4691 op2
= int_range
<2> (UCHAR (255), UCHAR (255));
4692 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4693 ASSERT_TRUE (code
== VREL_LT
);
4699 range_op_rshift_tests ();
4700 range_op_lshift_tests ();
4701 range_op_bitwise_and_tests ();
4702 range_op_cast_tests ();
4703 range_relational_tests ();
4706 } // namespace selftest
4708 #endif // CHECKING_P