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
);
1308 // Given addition or subtraction, determine the possible NORMAL ranges and
1309 // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1310 // Return the relation that exists between the LHS and OP1 in order for the
1311 // NORMAL range to apply.
1312 // a return value of VREL_VARYING means no ranges were applicable.
1314 static relation_kind
1315 plus_minus_ranges (irange
&r_ov
, irange
&r_normal
, const irange
&offset
,
1318 relation_kind kind
= VREL_VARYING
;
1319 // For now, only deal with constant adds. This could be extended to ranges
1320 // when someone is so motivated.
1321 if (!offset
.singleton_p () || offset
.zero_p ())
1324 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1325 wide_int off
= offset
.lower_bound ();
1326 if (wi::neg_p (off
, SIGNED
))
1329 off
= wi::neg (off
);
1332 wi::overflow_type ov
;
1333 tree type
= offset
.type ();
1334 unsigned prec
= TYPE_PRECISION (type
);
1337 // calculate the normal range and relation for the operation.
1341 lb
= wi::zero (prec
);
1342 ub
= wi::sub (wi::to_wide (vrp_val_max (type
)), off
, UNSIGNED
, &ov
);
1349 ub
= wi::to_wide (vrp_val_max (type
));
1352 int_range
<2> normal_range (type
, lb
, ub
);
1353 int_range
<2> ov_range (type
, lb
, ub
, VR_ANTI_RANGE
);
1356 r_normal
= normal_range
;
1360 // Once op1 has been calculated by operator_plus or operator_minus, check
1361 // to see if the relation passed causes any part of the calculation to
1362 // be not possible. ie
1363 // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1364 // and that further refines a_2 to [0, 0].
1365 // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1366 // relation between LHS relatoin OP1 and ADD_P is true for PLUS, false for
1367 // MINUS. IF any adjustment can be made, R will reflect it.
1370 adjust_op1_for_overflow (irange
&r
, const irange
&op2
, relation_kind rel
,
1373 if (r
.undefined_p ())
1375 tree type
= r
.type ();
1376 // Check for unsigned overflow and calculate the overflow part.
1377 signop s
= TYPE_SIGN (type
);
1378 if (!TYPE_OVERFLOW_WRAPS (type
) || s
== SIGNED
)
1381 // Only work with <, <=, >, >= relations.
1382 if (!relation_lt_le_gt_ge_p (rel
))
1385 // Get the ranges for this offset.
1386 int_range_max normal
, overflow
;
1387 relation_kind k
= plus_minus_ranges (overflow
, normal
, op2
, add_p
);
1389 // VREL_VARYING means there are no adjustments.
1390 if (k
== VREL_VARYING
)
1393 // If the relations match use the normal range, otherwise use overflow range.
1394 if (relation_intersect (k
, rel
) == k
)
1395 r
.intersect (normal
);
1397 r
.intersect (overflow
);
1402 operator_plus::op1_range (irange
&r
, tree type
,
1405 relation_kind rel
) const
1407 if (lhs
.undefined_p ())
1409 // Start with the default operation.
1410 range_op_handler
minus (MINUS_EXPR
, type
);
1413 bool res
= minus
.fold_range (r
, type
, lhs
, op2
);
1414 // Check for a relation refinement.
1416 adjust_op1_for_overflow (r
, op2
, rel
, true /* PLUS_EXPR */);
1421 operator_plus::op2_range (irange
&r
, tree type
,
1424 relation_kind rel
) const
1426 return op1_range (r
, type
, lhs
, op1
, rel
);
1430 class operator_minus
: public range_operator
1432 using range_operator::fold_range
;
1433 using range_operator::op1_range
;
1434 using range_operator::op2_range
;
1436 virtual bool op1_range (irange
&r
, tree type
,
1439 relation_kind rel ATTRIBUTE_UNUSED
) const;
1440 virtual bool op2_range (irange
&r
, tree type
,
1443 relation_kind rel ATTRIBUTE_UNUSED
) const;
1444 virtual void wi_fold (irange
&r
, tree type
,
1445 const wide_int
&lh_lb
,
1446 const wide_int
&lh_ub
,
1447 const wide_int
&rh_lb
,
1448 const wide_int
&rh_ub
) const;
1449 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
1452 relation_kind rel
) const;
1453 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1455 const irange
&op1_range
,
1456 const irange
&op2_range
,
1457 relation_kind rel
) const;
1461 operator_minus::wi_fold (irange
&r
, tree type
,
1462 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1463 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1465 wi::overflow_type ov_lb
, ov_ub
;
1466 signop s
= TYPE_SIGN (type
);
1467 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
1468 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
1469 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1473 // Return the relation between LHS and OP1 based on the relation between
1477 operator_minus::lhs_op1_relation (const irange
&, const irange
&op1
,
1478 const irange
&, relation_kind rel
) const
1480 if (!op1
.undefined_p () && TYPE_SIGN (op1
.type ()) == UNSIGNED
)
1489 return VREL_VARYING
;
1492 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1493 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1494 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1497 minus_op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1498 const irange
&op1_range ATTRIBUTE_UNUSED
,
1499 const irange
&op2_range ATTRIBUTE_UNUSED
,
1502 if (rel
== VREL_VARYING
)
1505 int_range
<2> rel_range
;
1506 unsigned prec
= TYPE_PRECISION (type
);
1507 signop sgn
= TYPE_SIGN (type
);
1509 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1511 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
));
1512 else if (rel
== VREL_NE
)
1513 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1515 else if (TYPE_OVERFLOW_WRAPS (type
))
1519 // For wrapping signed values and unsigned, if op1 > op2 or
1520 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1523 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1534 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1536 rel_range
= int_range
<2> (type
, wi::one (prec
),
1537 wi::max_value (prec
, sgn
));
1539 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1541 rel_range
= int_range
<2> (type
, wi::zero (prec
),
1542 wi::max_value (prec
, sgn
));
1544 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1546 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1547 wi::minus_one (prec
));
1549 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1551 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1558 lhs_range
.intersect (rel_range
);
1563 operator_minus::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1564 const irange
&op1_range
,
1565 const irange
&op2_range
,
1566 relation_kind rel
) const
1568 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1573 operator_minus::op1_range (irange
&r
, tree type
,
1576 relation_kind rel ATTRIBUTE_UNUSED
) const
1578 if (lhs
.undefined_p ())
1580 // Start with the default operation.
1581 range_op_handler
minus (PLUS_EXPR
, type
);
1584 bool res
= minus
.fold_range (r
, type
, lhs
, op2
);
1586 adjust_op1_for_overflow (r
, op2
, rel
, false /* PLUS_EXPR */);
1592 operator_minus::op2_range (irange
&r
, tree type
,
1595 relation_kind rel ATTRIBUTE_UNUSED
) const
1597 if (lhs
.undefined_p ())
1599 return fold_range (r
, type
, op1
, lhs
);
1603 class operator_pointer_diff
: public range_operator
1605 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1607 const irange
&op1_range
,
1608 const irange
&op2_range
,
1609 relation_kind rel
) const;
1613 operator_pointer_diff::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1614 const irange
&op1_range
,
1615 const irange
&op2_range
,
1616 relation_kind rel
) const
1618 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1623 class operator_min
: public range_operator
1626 virtual void wi_fold (irange
&r
, tree type
,
1627 const wide_int
&lh_lb
,
1628 const wide_int
&lh_ub
,
1629 const wide_int
&rh_lb
,
1630 const wide_int
&rh_ub
) const;
1634 operator_min::wi_fold (irange
&r
, tree type
,
1635 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1636 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1638 signop s
= TYPE_SIGN (type
);
1639 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
1640 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
1641 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1645 class operator_max
: public range_operator
1648 virtual void wi_fold (irange
&r
, tree type
,
1649 const wide_int
&lh_lb
,
1650 const wide_int
&lh_ub
,
1651 const wide_int
&rh_lb
,
1652 const wide_int
&rh_ub
) const;
1656 operator_max::wi_fold (irange
&r
, tree type
,
1657 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1658 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1660 signop s
= TYPE_SIGN (type
);
1661 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
1662 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
1663 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1667 class cross_product_operator
: public range_operator
1670 // Perform an operation between two wide-ints and place the result
1671 // in R. Return true if the operation overflowed.
1672 virtual bool wi_op_overflows (wide_int
&r
,
1675 const wide_int
&) const = 0;
1677 // Calculate the cross product of two sets of sub-ranges and return it.
1678 void wi_cross_product (irange
&r
, tree type
,
1679 const wide_int
&lh_lb
,
1680 const wide_int
&lh_ub
,
1681 const wide_int
&rh_lb
,
1682 const wide_int
&rh_ub
) const;
1685 // Calculate the cross product of two sets of ranges and return it.
1687 // Multiplications, divisions and shifts are a bit tricky to handle,
1688 // depending on the mix of signs we have in the two ranges, we need to
1689 // operate on different values to get the minimum and maximum values
1690 // for the new range. One approach is to figure out all the
1691 // variations of range combinations and do the operations.
1693 // However, this involves several calls to compare_values and it is
1694 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1695 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1696 // figure the smallest and largest values to form the new range.
1699 cross_product_operator::wi_cross_product (irange
&r
, tree type
,
1700 const wide_int
&lh_lb
,
1701 const wide_int
&lh_ub
,
1702 const wide_int
&rh_lb
,
1703 const wide_int
&rh_ub
) const
1705 wide_int cp1
, cp2
, cp3
, cp4
;
1706 // Default to varying.
1707 r
.set_varying (type
);
1709 // Compute the 4 cross operations, bailing if we get an overflow we
1711 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
1713 if (wi::eq_p (lh_lb
, lh_ub
))
1715 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
1717 if (wi::eq_p (rh_lb
, rh_ub
))
1719 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
1721 if (wi::eq_p (lh_lb
, lh_ub
))
1723 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
1727 signop sign
= TYPE_SIGN (type
);
1728 if (wi::gt_p (cp1
, cp2
, sign
))
1729 std::swap (cp1
, cp2
);
1730 if (wi::gt_p (cp3
, cp4
, sign
))
1731 std::swap (cp3
, cp4
);
1733 // Choose min and max from the ordered pairs.
1734 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
1735 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
1736 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
1740 class operator_mult
: public cross_product_operator
1742 using range_operator::op1_range
;
1743 using range_operator::op2_range
;
1745 virtual void wi_fold (irange
&r
, tree type
,
1746 const wide_int
&lh_lb
,
1747 const wide_int
&lh_ub
,
1748 const wide_int
&rh_lb
,
1749 const wide_int
&rh_ub
) const;
1750 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1751 const wide_int
&w0
, const wide_int
&w1
) const;
1752 virtual bool op1_range (irange
&r
, tree type
,
1755 relation_kind rel ATTRIBUTE_UNUSED
) const;
1756 virtual bool op2_range (irange
&r
, tree type
,
1759 relation_kind rel ATTRIBUTE_UNUSED
) const;
1763 operator_mult::op1_range (irange
&r
, tree type
,
1764 const irange
&lhs
, const irange
&op2
,
1765 relation_kind rel ATTRIBUTE_UNUSED
) const
1768 if (lhs
.undefined_p ())
1771 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1772 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1773 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1774 if (TYPE_OVERFLOW_WRAPS (type
))
1777 if (op2
.singleton_p (&offset
) && !integer_zerop (offset
))
1778 return range_op_handler (TRUNC_DIV_EXPR
, type
).fold_range (r
, type
,
1784 operator_mult::op2_range (irange
&r
, tree type
,
1785 const irange
&lhs
, const irange
&op1
,
1786 relation_kind rel
) const
1788 return operator_mult::op1_range (r
, type
, lhs
, op1
, rel
);
1792 operator_mult::wi_op_overflows (wide_int
&res
, tree type
,
1793 const wide_int
&w0
, const wide_int
&w1
) const
1795 wi::overflow_type overflow
= wi::OVF_NONE
;
1796 signop sign
= TYPE_SIGN (type
);
1797 res
= wi::mul (w0
, w1
, sign
, &overflow
);
1798 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1800 // For multiplication, the sign of the overflow is given
1801 // by the comparison of the signs of the operands.
1802 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
1803 res
= wi::max_value (w0
.get_precision (), sign
);
1805 res
= wi::min_value (w0
.get_precision (), sign
);
1812 operator_mult::wi_fold (irange
&r
, tree type
,
1813 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1814 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1816 if (TYPE_OVERFLOW_UNDEFINED (type
))
1818 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1822 // Multiply the ranges when overflow wraps. This is basically fancy
1823 // code so we don't drop to varying with an unsigned
1826 // This test requires 2*prec bits if both operands are signed and
1827 // 2*prec + 2 bits if either is not. Therefore, extend the values
1828 // using the sign of the result to PREC2. From here on out,
1829 // everthing is just signed math no matter what the input types
1832 signop sign
= TYPE_SIGN (type
);
1833 unsigned prec
= TYPE_PRECISION (type
);
1834 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
1835 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
1836 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
1837 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
1838 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
1839 widest2_int size
= sizem1
+ 1;
1841 // Canonicalize the intervals.
1842 if (sign
== UNSIGNED
)
1844 if (wi::ltu_p (size
, min0
+ max0
))
1849 if (wi::ltu_p (size
, min1
+ max1
))
1856 // Sort the 4 products so that min is in prod0 and max is in
1858 widest2_int prod0
= min0
* min1
;
1859 widest2_int prod1
= min0
* max1
;
1860 widest2_int prod2
= max0
* min1
;
1861 widest2_int prod3
= max0
* max1
;
1863 // min0min1 > max0max1
1865 std::swap (prod0
, prod3
);
1867 // min0max1 > max0min1
1869 std::swap (prod1
, prod2
);
1872 std::swap (prod0
, prod1
);
1875 std::swap (prod2
, prod3
);
1878 prod2
= prod3
- prod0
;
1879 if (wi::geu_p (prod2
, sizem1
))
1880 // The range covers all values.
1881 r
.set_varying (type
);
1884 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
1885 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
1886 create_possibly_reversed_range (r
, type
, new_lb
, new_ub
);
1891 class operator_div
: public cross_product_operator
1894 operator_div (enum tree_code c
) { code
= c
; }
1895 virtual void wi_fold (irange
&r
, tree type
,
1896 const wide_int
&lh_lb
,
1897 const wide_int
&lh_ub
,
1898 const wide_int
&rh_lb
,
1899 const wide_int
&rh_ub
) const;
1900 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1901 const wide_int
&, const wide_int
&) const;
1903 enum tree_code code
;
1907 operator_div::wi_op_overflows (wide_int
&res
, tree type
,
1908 const wide_int
&w0
, const wide_int
&w1
) const
1913 wi::overflow_type overflow
= wi::OVF_NONE
;
1914 signop sign
= TYPE_SIGN (type
);
1918 case EXACT_DIV_EXPR
:
1919 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1920 // operator_exact_divide. No need to handle it here.
1923 case TRUNC_DIV_EXPR
:
1924 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
1926 case FLOOR_DIV_EXPR
:
1927 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
1929 case ROUND_DIV_EXPR
:
1930 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
1933 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
1939 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1941 // For division, the only case is -INF / -1 = +INF.
1942 res
= wi::max_value (w0
.get_precision (), sign
);
1949 operator_div::wi_fold (irange
&r
, tree type
,
1950 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1951 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1953 const wide_int dividend_min
= lh_lb
;
1954 const wide_int dividend_max
= lh_ub
;
1955 const wide_int divisor_min
= rh_lb
;
1956 const wide_int divisor_max
= rh_ub
;
1957 signop sign
= TYPE_SIGN (type
);
1958 unsigned prec
= TYPE_PRECISION (type
);
1959 wide_int extra_min
, extra_max
;
1961 // If we know we won't divide by zero, just do the division.
1962 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
1964 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1965 divisor_min
, divisor_max
);
1969 // If we're definitely dividing by zero, there's nothing to do.
1970 if (wi_zero_p (type
, divisor_min
, divisor_max
))
1976 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1977 // skip any division by zero.
1979 // First divide by the negative numbers, if any.
1980 if (wi::neg_p (divisor_min
, sign
))
1981 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1982 divisor_min
, wi::minus_one (prec
));
1986 // Then divide by the non-zero positive numbers, if any.
1987 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
1990 wi_cross_product (tmp
, type
, dividend_min
, dividend_max
,
1991 wi::one (prec
), divisor_max
);
1994 // We shouldn't still have undefined here.
1995 gcc_checking_assert (!r
.undefined_p ());
1998 operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
1999 operator_div
op_floor_div (FLOOR_DIV_EXPR
);
2000 operator_div
op_round_div (ROUND_DIV_EXPR
);
2001 operator_div
op_ceil_div (CEIL_DIV_EXPR
);
2004 class operator_exact_divide
: public operator_div
2006 using range_operator::op1_range
;
2008 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR
) { }
2009 virtual bool op1_range (irange
&r
, tree type
,
2012 relation_kind rel ATTRIBUTE_UNUSED
) const;
2017 operator_exact_divide::op1_range (irange
&r
, tree type
,
2020 relation_kind rel ATTRIBUTE_UNUSED
) const
2022 if (lhs
.undefined_p ())
2025 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2026 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2027 // We wont bother trying to enumerate all the in between stuff :-P
2028 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
2029 // the time however.
2030 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2031 if (op2
.singleton_p (&offset
)
2032 && !integer_zerop (offset
))
2033 return range_op_handler (MULT_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
2038 class operator_lshift
: public cross_product_operator
2040 using range_operator::fold_range
;
2041 using range_operator::op1_range
;
2043 virtual bool op1_range (irange
&r
, tree type
,
2046 relation_kind rel
= VREL_VARYING
) const;
2047 virtual bool fold_range (irange
&r
, tree type
,
2050 relation_kind rel
= VREL_VARYING
) const;
2052 virtual void wi_fold (irange
&r
, tree type
,
2053 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2054 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
2055 virtual bool wi_op_overflows (wide_int
&res
,
2058 const wide_int
&) const;
2061 class operator_rshift
: public cross_product_operator
2063 using range_operator::fold_range
;
2064 using range_operator::op1_range
;
2065 using range_operator::lhs_op1_relation
;
2067 virtual bool fold_range (irange
&r
, tree type
,
2070 relation_kind rel
= VREL_VARYING
) const;
2071 virtual void wi_fold (irange
&r
, tree type
,
2072 const wide_int
&lh_lb
,
2073 const wide_int
&lh_ub
,
2074 const wide_int
&rh_lb
,
2075 const wide_int
&rh_ub
) const;
2076 virtual bool wi_op_overflows (wide_int
&res
,
2079 const wide_int
&w1
) const;
2080 virtual bool op1_range (irange
&, tree type
,
2083 relation_kind rel
= VREL_VARYING
) const;
2084 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
2087 relation_kind rel
) const;
2092 operator_rshift::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
2095 relation_kind
) const
2097 // If both operands range are >= 0, then the LHS <= op1.
2098 if (!op1
.undefined_p () && !op2
.undefined_p ()
2099 && wi::ge_p (op1
.lower_bound (), 0, TYPE_SIGN (op1
.type ()))
2100 && wi::ge_p (op2
.lower_bound (), 0, TYPE_SIGN (op2
.type ())))
2102 return VREL_VARYING
;
2106 operator_lshift::fold_range (irange
&r
, tree type
,
2109 relation_kind rel
) const
2111 int_range_max shift_range
;
2112 if (!get_shift_range (shift_range
, type
, op2
))
2114 if (op2
.undefined_p ())
2117 r
.set_varying (type
);
2121 // Transform left shifts by constants into multiplies.
2122 if (shift_range
.singleton_p ())
2124 unsigned shift
= shift_range
.lower_bound ().to_uhwi ();
2125 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
2126 int_range
<1> mult (type
, tmp
, tmp
);
2128 // Force wrapping multiplication.
2129 bool saved_flag_wrapv
= flag_wrapv
;
2130 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
2132 flag_wrapv_pointer
= 1;
2133 bool b
= op_mult
.fold_range (r
, type
, op1
, mult
);
2134 flag_wrapv
= saved_flag_wrapv
;
2135 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
2139 // Otherwise, invoke the generic fold routine.
2140 return range_operator::fold_range (r
, type
, op1
, shift_range
, rel
);
2144 operator_lshift::wi_fold (irange
&r
, tree type
,
2145 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2146 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2148 signop sign
= TYPE_SIGN (type
);
2149 unsigned prec
= TYPE_PRECISION (type
);
2150 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
2151 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
2152 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2153 // overflow. However, for that to happen, rh.max needs to be zero,
2154 // which means rh is a singleton range of zero, which means we simply return
2155 // [lh_lb, lh_ub] as the range.
2156 if (wi::eq_p (rh_ub
, rh_lb
) && wi::eq_p (rh_ub
, 0))
2158 r
= int_range
<2> (type
, lh_lb
, lh_ub
);
2162 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2163 wide_int complement
= ~(bound
- 1);
2164 wide_int low_bound
, high_bound
;
2165 bool in_bounds
= false;
2167 if (sign
== UNSIGNED
)
2170 high_bound
= complement
;
2171 if (wi::ltu_p (lh_ub
, low_bound
))
2173 // [5, 6] << [1, 2] == [10, 24].
2174 // We're shifting out only zeroes, the value increases
2178 else if (wi::ltu_p (high_bound
, lh_lb
))
2180 // [0xffffff00, 0xffffffff] << [1, 2]
2181 // == [0xfffffc00, 0xfffffffe].
2182 // We're shifting out only ones, the value decreases
2189 // [-1, 1] << [1, 2] == [-4, 4]
2190 low_bound
= complement
;
2192 if (wi::lts_p (lh_ub
, high_bound
)
2193 && wi::lts_p (low_bound
, lh_lb
))
2195 // For non-negative numbers, we're shifting out only zeroes,
2196 // the value increases monotonically. For negative numbers,
2197 // we're shifting out only ones, the value decreases
2204 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2206 r
.set_varying (type
);
2210 operator_lshift::wi_op_overflows (wide_int
&res
, tree type
,
2211 const wide_int
&w0
, const wide_int
&w1
) const
2213 signop sign
= TYPE_SIGN (type
);
2216 // It's unclear from the C standard whether shifts can overflow.
2217 // The following code ignores overflow; perhaps a C standard
2218 // interpretation ruling is needed.
2219 res
= wi::rshift (w0
, -w1
, sign
);
2222 res
= wi::lshift (w0
, w1
);
2227 operator_lshift::op1_range (irange
&r
,
2231 relation_kind rel ATTRIBUTE_UNUSED
) const
2233 if (lhs
.undefined_p ())
2237 if (!lhs
.contains_p (build_zero_cst (type
)))
2238 r
.set_nonzero (type
);
2240 r
.set_varying (type
);
2242 if (op2
.singleton_p (&shift_amount
))
2244 wide_int shift
= wi::to_wide (shift_amount
);
2245 if (wi::lt_p (shift
, 0, SIGNED
))
2247 if (wi::ge_p (shift
, wi::uhwi (TYPE_PRECISION (type
),
2248 TYPE_PRECISION (op2
.type ())),
2257 // Work completely in unsigned mode to start.
2259 int_range_max tmp_range
;
2260 if (TYPE_SIGN (type
) == SIGNED
)
2262 int_range_max tmp
= lhs
;
2263 utype
= unsigned_type_for (type
);
2264 range_cast (tmp
, utype
);
2265 op_rshift
.fold_range (tmp_range
, utype
, tmp
, op2
);
2268 op_rshift
.fold_range (tmp_range
, utype
, lhs
, op2
);
2270 // Start with ranges which can produce the LHS by right shifting the
2271 // result by the shift amount.
2272 // ie [0x08, 0xF0] = op1 << 2 will start with
2273 // [00001000, 11110000] = op1 << 2
2274 // [0x02, 0x4C] aka [00000010, 00111100]
2276 // Then create a range from the LB with the least significant upper bit
2277 // set, to the upper bound with all the bits set.
2278 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2280 // Ideally we do this for each subrange, but just lump them all for now.
2281 unsigned low_bits
= TYPE_PRECISION (utype
)
2282 - TREE_INT_CST_LOW (shift_amount
);
2283 wide_int up_mask
= wi::mask (low_bits
, true, TYPE_PRECISION (utype
));
2284 wide_int new_ub
= wi::bit_or (up_mask
, tmp_range
.upper_bound ());
2285 wide_int new_lb
= wi::set_bit (tmp_range
.lower_bound (), low_bits
);
2286 int_range
<2> fill_range (utype
, new_lb
, new_ub
);
2287 tmp_range
.union_ (fill_range
);
2290 range_cast (tmp_range
, type
);
2292 r
.intersect (tmp_range
);
2296 return !r
.varying_p ();
2300 operator_rshift::op1_range (irange
&r
,
2304 relation_kind rel ATTRIBUTE_UNUSED
) const
2307 if (lhs
.undefined_p ())
2309 if (op2
.singleton_p (&shift
))
2311 // Ignore nonsensical shifts.
2312 unsigned prec
= TYPE_PRECISION (type
);
2313 if (wi::ge_p (wi::to_wide (shift
),
2314 wi::uhwi (prec
, TYPE_PRECISION (TREE_TYPE (shift
))),
2317 if (wi::to_wide (shift
) == 0)
2323 // Folding the original operation may discard some impossible
2324 // ranges from the LHS.
2325 int_range_max lhs_refined
;
2326 op_rshift
.fold_range (lhs_refined
, type
, int_range
<1> (type
), op2
);
2327 lhs_refined
.intersect (lhs
);
2328 if (lhs_refined
.undefined_p ())
2333 int_range_max
shift_range (shift
, shift
);
2334 int_range_max lb
, ub
;
2335 op_lshift
.fold_range (lb
, type
, lhs_refined
, shift_range
);
2337 // 0000 0111 = OP1 >> 3
2339 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2340 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2341 // right hand side (0x07).
2342 tree mask
= fold_build1 (BIT_NOT_EXPR
, type
,
2343 fold_build2 (LSHIFT_EXPR
, type
,
2344 build_minus_one_cst (type
),
2346 int_range_max
mask_range (build_zero_cst (type
), mask
);
2347 op_plus
.fold_range (ub
, type
, lb
, mask_range
);
2350 if (!lhs_refined
.contains_p (build_zero_cst (type
)))
2352 mask_range
.invert ();
2353 r
.intersect (mask_range
);
2361 operator_rshift::wi_op_overflows (wide_int
&res
,
2364 const wide_int
&w1
) const
2366 signop sign
= TYPE_SIGN (type
);
2368 res
= wi::lshift (w0
, -w1
);
2371 // It's unclear from the C standard whether shifts can overflow.
2372 // The following code ignores overflow; perhaps a C standard
2373 // interpretation ruling is needed.
2374 res
= wi::rshift (w0
, w1
, sign
);
2380 operator_rshift::fold_range (irange
&r
, tree type
,
2383 relation_kind rel
) const
2385 int_range_max shift
;
2386 if (!get_shift_range (shift
, type
, op2
))
2388 if (op2
.undefined_p ())
2391 r
.set_varying (type
);
2395 return range_operator::fold_range (r
, type
, op1
, shift
, rel
);
2399 operator_rshift::wi_fold (irange
&r
, tree type
,
2400 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2401 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2403 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2407 class operator_cast
: public range_operator
2409 using range_operator::fold_range
;
2410 using range_operator::op1_range
;
2412 virtual bool fold_range (irange
&r
, tree type
,
2415 relation_kind rel
= VREL_VARYING
) const;
2416 virtual bool op1_range (irange
&r
, tree type
,
2419 relation_kind rel
= VREL_VARYING
) const;
2421 bool truncating_cast_p (const irange
&inner
, const irange
&outer
) const;
2422 bool inside_domain_p (const wide_int
&min
, const wide_int
&max
,
2423 const irange
&outer
) const;
2424 void fold_pair (irange
&r
, unsigned index
, const irange
&inner
,
2425 const irange
&outer
) const;
2428 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2431 operator_cast::truncating_cast_p (const irange
&inner
,
2432 const irange
&outer
) const
2434 return TYPE_PRECISION (outer
.type ()) < TYPE_PRECISION (inner
.type ());
2437 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2440 operator_cast::inside_domain_p (const wide_int
&min
,
2441 const wide_int
&max
,
2442 const irange
&range
) const
2444 wide_int domain_min
= wi::to_wide (vrp_val_min (range
.type ()));
2445 wide_int domain_max
= wi::to_wide (vrp_val_max (range
.type ()));
2446 signop domain_sign
= TYPE_SIGN (range
.type ());
2447 return (wi::le_p (min
, domain_max
, domain_sign
)
2448 && wi::le_p (max
, domain_max
, domain_sign
)
2449 && wi::ge_p (min
, domain_min
, domain_sign
)
2450 && wi::ge_p (max
, domain_min
, domain_sign
));
2454 // Helper for fold_range which work on a pair at a time.
2457 operator_cast::fold_pair (irange
&r
, unsigned index
,
2458 const irange
&inner
,
2459 const irange
&outer
) const
2461 tree inner_type
= inner
.type ();
2462 tree outer_type
= outer
.type ();
2463 signop inner_sign
= TYPE_SIGN (inner_type
);
2464 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2466 // check to see if casting from INNER to OUTER is a conversion that
2467 // fits in the resulting OUTER type.
2468 wide_int inner_lb
= inner
.lower_bound (index
);
2469 wide_int inner_ub
= inner
.upper_bound (index
);
2470 if (truncating_cast_p (inner
, outer
))
2472 // We may be able to accomodate a truncating cast if the
2473 // resulting range can be represented in the target type...
2474 if (wi::rshift (wi::sub (inner_ub
, inner_lb
),
2475 wi::uhwi (outer_prec
, TYPE_PRECISION (inner
.type ())),
2478 r
.set_varying (outer_type
);
2482 // ...but we must still verify that the final range fits in the
2483 // domain. This catches -fstrict-enum restrictions where the domain
2484 // range is smaller than what fits in the underlying type.
2485 wide_int min
= wide_int::from (inner_lb
, outer_prec
, inner_sign
);
2486 wide_int max
= wide_int::from (inner_ub
, outer_prec
, inner_sign
);
2487 if (inside_domain_p (min
, max
, outer
))
2488 create_possibly_reversed_range (r
, outer_type
, min
, max
);
2490 r
.set_varying (outer_type
);
2495 operator_cast::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2496 const irange
&inner
,
2497 const irange
&outer
,
2498 relation_kind rel ATTRIBUTE_UNUSED
) const
2500 if (empty_range_varying (r
, type
, inner
, outer
))
2503 gcc_checking_assert (outer
.varying_p ());
2504 gcc_checking_assert (inner
.num_pairs () > 0);
2506 // Avoid a temporary by folding the first pair directly into the result.
2507 fold_pair (r
, 0, inner
, outer
);
2509 // Then process any additonal pairs by unioning with their results.
2510 for (unsigned x
= 1; x
< inner
.num_pairs (); ++x
)
2513 fold_pair (tmp
, x
, inner
, outer
);
2519 // Update the nonzero mask. Truncating casts are problematic unless
2520 // the conversion fits in the resulting outer type.
2521 wide_int nz
= inner
.get_nonzero_bits ();
2522 if (truncating_cast_p (inner
, outer
)
2523 && wi::rshift (nz
, wi::uhwi (TYPE_PRECISION (outer
.type ()),
2524 TYPE_PRECISION (inner
.type ())),
2525 TYPE_SIGN (inner
.type ())) != 0)
2527 nz
= wide_int::from (nz
, TYPE_PRECISION (type
), TYPE_SIGN (inner
.type ()));
2528 r
.set_nonzero_bits (nz
);
2534 operator_cast::op1_range (irange
&r
, tree type
,
2537 relation_kind rel ATTRIBUTE_UNUSED
) const
2539 if (lhs
.undefined_p ())
2541 tree lhs_type
= lhs
.type ();
2542 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
2544 // If we are calculating a pointer, shortcut to what we really care about.
2545 if (POINTER_TYPE_P (type
))
2547 // Conversion from other pointers or a constant (including 0/NULL)
2548 // are straightforward.
2549 if (POINTER_TYPE_P (lhs
.type ())
2550 || (lhs
.singleton_p ()
2551 && TYPE_PRECISION (lhs
.type ()) >= TYPE_PRECISION (type
)))
2554 range_cast (r
, type
);
2558 // If the LHS is not a pointer nor a singleton, then it is
2559 // either VARYING or non-zero.
2560 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
2561 r
.set_nonzero (type
);
2563 r
.set_varying (type
);
2569 if (truncating_cast_p (op2
, lhs
))
2571 if (lhs
.varying_p ())
2572 r
.set_varying (type
);
2575 // We want to insert the LHS as an unsigned value since it
2576 // would not trigger the signed bit of the larger type.
2577 int_range_max converted_lhs
= lhs
;
2578 range_cast (converted_lhs
, unsigned_type_for (lhs_type
));
2579 range_cast (converted_lhs
, type
);
2580 // Start by building the positive signed outer range for the type.
2581 wide_int lim
= wi::set_bit_in_zero (TYPE_PRECISION (lhs_type
),
2582 TYPE_PRECISION (type
));
2583 r
= int_range
<1> (type
, lim
, wi::max_value (TYPE_PRECISION (type
),
2585 // For the signed part, we need to simply union the 2 ranges now.
2586 r
.union_ (converted_lhs
);
2588 // Create maximal negative number outside of LHS bits.
2589 lim
= wi::mask (TYPE_PRECISION (lhs_type
), true,
2590 TYPE_PRECISION (type
));
2591 // Add this to the unsigned LHS range(s).
2592 int_range_max
lim_range (type
, lim
, lim
);
2593 int_range_max lhs_neg
;
2594 range_op_handler (PLUS_EXPR
, type
).fold_range (lhs_neg
, type
,
2597 // lhs_neg now has all the negative versions of the LHS.
2598 // Now union in all the values from SIGNED MIN (0x80000) to
2599 // lim-1 in order to fill in all the ranges with the upper
2602 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2603 // we don't need to create a range from min to lim-1
2604 // calculate neg range traps trying to create [lim, lim - 1].
2605 wide_int min_val
= wi::min_value (TYPE_PRECISION (type
), SIGNED
);
2608 int_range_max
neg (type
,
2609 wi::min_value (TYPE_PRECISION (type
),
2612 lhs_neg
.union_ (neg
);
2614 // And finally, munge the signed and unsigned portions.
2617 // And intersect with any known value passed in the extra operand.
2623 if (TYPE_PRECISION (lhs_type
) == TYPE_PRECISION (type
))
2627 // The cast is not truncating, and the range is restricted to
2628 // the range of the RHS by this assignment.
2630 // Cast the range of the RHS to the type of the LHS.
2631 fold_range (tmp
, lhs_type
, int_range
<1> (type
), int_range
<1> (lhs_type
));
2632 // Intersect this with the LHS range will produce the range,
2633 // which will be cast to the RHS type before returning.
2634 tmp
.intersect (lhs
);
2637 // Cast the calculated range to the type of the RHS.
2638 fold_range (r
, type
, tmp
, int_range
<1> (type
));
2643 class operator_logical_and
: public range_operator
2645 using range_operator::fold_range
;
2646 using range_operator::op1_range
;
2647 using range_operator::op2_range
;
2649 virtual bool fold_range (irange
&r
, tree type
,
2652 relation_kind rel
= VREL_VARYING
) const;
2653 virtual bool op1_range (irange
&r
, tree type
,
2656 relation_kind rel
= VREL_VARYING
) const;
2657 virtual bool op2_range (irange
&r
, tree type
,
2660 relation_kind rel
= VREL_VARYING
) const;
2665 operator_logical_and::fold_range (irange
&r
, tree type
,
2668 relation_kind rel ATTRIBUTE_UNUSED
) const
2670 if (empty_range_varying (r
, type
, lh
, rh
))
2673 // 0 && anything is 0.
2674 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
2675 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
2676 r
= range_false (type
);
2677 else if (lh
.contains_p (build_zero_cst (lh
.type ()))
2678 || rh
.contains_p (build_zero_cst (rh
.type ())))
2679 // To reach this point, there must be a logical 1 on each side, and
2680 // the only remaining question is whether there is a zero or not.
2681 r
= range_true_and_false (type
);
2683 r
= range_true (type
);
2688 operator_logical_and::op1_range (irange
&r
, tree type
,
2690 const irange
&op2 ATTRIBUTE_UNUSED
,
2691 relation_kind rel ATTRIBUTE_UNUSED
) const
2693 switch (get_bool_state (r
, lhs
, type
))
2696 // A true result means both sides of the AND must be true.
2697 r
= range_true (type
);
2700 // Any other result means only one side has to be false, the
2701 // other side can be anything. So we cannot be sure of any
2703 r
= range_true_and_false (type
);
2710 operator_logical_and::op2_range (irange
&r
, tree type
,
2713 relation_kind rel ATTRIBUTE_UNUSED
) const
2715 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
2719 class operator_bitwise_and
: public range_operator
2721 using range_operator::fold_range
;
2722 using range_operator::op1_range
;
2723 using range_operator::op2_range
;
2725 virtual bool fold_range (irange
&r
, tree type
,
2728 relation_kind rel
= VREL_VARYING
) const;
2729 virtual bool op1_range (irange
&r
, tree type
,
2732 relation_kind rel
= VREL_VARYING
) const;
2733 virtual bool op2_range (irange
&r
, tree type
,
2736 relation_kind rel
= VREL_VARYING
) const;
2737 virtual void wi_fold (irange
&r
, tree type
,
2738 const wide_int
&lh_lb
,
2739 const wide_int
&lh_ub
,
2740 const wide_int
&rh_lb
,
2741 const wide_int
&rh_ub
) const;
2743 void simple_op1_range_solver (irange
&r
, tree type
,
2745 const irange
&op2
) const;
2749 operator_bitwise_and::fold_range (irange
&r
, tree type
,
2752 relation_kind rel ATTRIBUTE_UNUSED
) const
2754 if (range_operator::fold_range (r
, type
, lh
, rh
))
2756 if (!lh
.undefined_p () && !rh
.undefined_p ())
2757 r
.set_nonzero_bits (wi::bit_and (lh
.get_nonzero_bits (),
2758 rh
.get_nonzero_bits ()));
2765 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2766 // by considering the number of leading redundant sign bit copies.
2767 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2768 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2770 wi_optimize_signed_bitwise_op (irange
&r
, tree type
,
2771 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2772 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2774 int lh_clrsb
= MIN (wi::clrsb (lh_lb
), wi::clrsb (lh_ub
));
2775 int rh_clrsb
= MIN (wi::clrsb (rh_lb
), wi::clrsb (rh_ub
));
2776 int new_clrsb
= MIN (lh_clrsb
, rh_clrsb
);
2779 int type_prec
= TYPE_PRECISION (type
);
2780 int rprec
= (type_prec
- new_clrsb
) - 1;
2781 value_range_with_overflow (r
, type
,
2782 wi::mask (rprec
, true, type_prec
),
2783 wi::mask (rprec
, false, type_prec
));
2788 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2789 // possible. Basically, see if we can optimize:
2793 // [LB op Z, UB op Z]
2795 // If the optimization was successful, accumulate the range in R and
2799 wi_optimize_and_or (irange
&r
,
2800 enum tree_code code
,
2802 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2803 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2805 // Calculate the singleton mask among the ranges, if any.
2806 wide_int lower_bound
, upper_bound
, mask
;
2807 if (wi::eq_p (rh_lb
, rh_ub
))
2810 lower_bound
= lh_lb
;
2811 upper_bound
= lh_ub
;
2813 else if (wi::eq_p (lh_lb
, lh_ub
))
2816 lower_bound
= rh_lb
;
2817 upper_bound
= rh_ub
;
2822 // If Z is a constant which (for op | its bitwise not) has n
2823 // consecutive least significant bits cleared followed by m 1
2824 // consecutive bits set immediately above it and either
2825 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2827 // The least significant n bits of all the values in the range are
2828 // cleared or set, the m bits above it are preserved and any bits
2829 // above these are required to be the same for all values in the
2833 if (code
== BIT_IOR_EXPR
)
2835 if (wi::eq_p (w
, 0))
2836 n
= w
.get_precision ();
2840 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2841 if (wi::eq_p (w
, 0))
2842 m
= w
.get_precision () - n
;
2844 m
= wi::ctz (w
) - n
;
2846 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2847 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
2850 wide_int res_lb
, res_ub
;
2851 if (code
== BIT_AND_EXPR
)
2853 res_lb
= wi::bit_and (lower_bound
, mask
);
2854 res_ub
= wi::bit_and (upper_bound
, mask
);
2856 else if (code
== BIT_IOR_EXPR
)
2858 res_lb
= wi::bit_or (lower_bound
, mask
);
2859 res_ub
= wi::bit_or (upper_bound
, mask
);
2863 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
2865 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2866 if (code
== BIT_IOR_EXPR
&& wi::ne_p (mask
, 0))
2869 tmp
.set_nonzero (type
);
2875 // For range [LB, UB] compute two wide_int bit masks.
2877 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2878 // for all numbers in the range the bit is 0, otherwise it might be 0
2881 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2882 // for all numbers in the range the bit is 1, otherwise it might be 0
2886 wi_set_zero_nonzero_bits (tree type
,
2887 const wide_int
&lb
, const wide_int
&ub
,
2888 wide_int
&maybe_nonzero
,
2889 wide_int
&mustbe_nonzero
)
2891 signop sign
= TYPE_SIGN (type
);
2893 if (wi::eq_p (lb
, ub
))
2894 maybe_nonzero
= mustbe_nonzero
= lb
;
2895 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
2897 wide_int xor_mask
= lb
^ ub
;
2898 maybe_nonzero
= lb
| ub
;
2899 mustbe_nonzero
= lb
& ub
;
2902 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2903 maybe_nonzero
.get_precision ());
2904 maybe_nonzero
= maybe_nonzero
| mask
;
2905 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
2910 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
2911 mustbe_nonzero
= wi::zero (lb
.get_precision ());
2916 operator_bitwise_and::wi_fold (irange
&r
, tree type
,
2917 const wide_int
&lh_lb
,
2918 const wide_int
&lh_ub
,
2919 const wide_int
&rh_lb
,
2920 const wide_int
&rh_ub
) const
2922 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
2925 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
2926 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
2927 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
2928 maybe_nonzero_lh
, mustbe_nonzero_lh
);
2929 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
2930 maybe_nonzero_rh
, mustbe_nonzero_rh
);
2932 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
2933 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
2934 signop sign
= TYPE_SIGN (type
);
2935 unsigned prec
= TYPE_PRECISION (type
);
2936 // If both input ranges contain only negative values, we can
2937 // truncate the result range maximum to the minimum of the
2938 // input range maxima.
2939 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
2941 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2942 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2944 // If either input range contains only non-negative values
2945 // we can truncate the result range maximum to the respective
2946 // maximum of the input range.
2947 if (wi::ge_p (lh_lb
, 0, sign
))
2948 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2949 if (wi::ge_p (rh_lb
, 0, sign
))
2950 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2951 // PR68217: In case of signed & sign-bit-CST should
2952 // result in [-INF, 0] instead of [-INF, INF].
2953 if (wi::gt_p (new_lb
, new_ub
, sign
))
2955 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
2957 && ((wi::eq_p (lh_lb
, lh_ub
)
2958 && !wi::cmps (lh_lb
, sign_bit
))
2959 || (wi::eq_p (rh_lb
, rh_ub
)
2960 && !wi::cmps (rh_lb
, sign_bit
))))
2962 new_lb
= wi::min_value (prec
, sign
);
2963 new_ub
= wi::zero (prec
);
2966 // If the limits got swapped around, return varying.
2967 if (wi::gt_p (new_lb
, new_ub
,sign
))
2970 && wi_optimize_signed_bitwise_op (r
, type
,
2974 r
.set_varying (type
);
2977 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2981 set_nonzero_range_from_mask (irange
&r
, tree type
, const irange
&lhs
)
2983 if (!lhs
.contains_p (build_zero_cst (type
)))
2984 r
= range_nonzero (type
);
2986 r
.set_varying (type
);
2989 // This was shamelessly stolen from register_edge_assert_for_2 and
2990 // adjusted to work with iranges.
2993 operator_bitwise_and::simple_op1_range_solver (irange
&r
, tree type
,
2995 const irange
&op2
) const
2997 if (!op2
.singleton_p ())
2999 set_nonzero_range_from_mask (r
, type
, lhs
);
3002 unsigned int nprec
= TYPE_PRECISION (type
);
3003 wide_int cst2v
= op2
.lower_bound ();
3004 bool cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (type
));
3007 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3009 sgnbit
= wi::zero (nprec
);
3011 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3013 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3014 // maximum unsigned value is ~0. For signed comparison, if CST2
3015 // doesn't have the most significant bit set, handle it similarly. If
3016 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3017 wide_int valv
= lhs
.lower_bound ();
3018 wide_int minv
= valv
& cst2v
, maxv
;
3019 bool we_know_nothing
= false;
3022 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3023 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3026 // If we can't determine anything on this bound, fall
3027 // through and conservatively solve for the other end point.
3028 we_know_nothing
= true;
3031 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3032 if (we_know_nothing
)
3033 r
.set_varying (type
);
3035 r
= int_range
<1> (type
, minv
, maxv
);
3037 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3039 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3040 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3042 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3044 // For signed comparison, if CST2 doesn't have most significant bit
3045 // set, handle it similarly. If CST2 has MSB set, the maximum is
3046 // the same and minimum is INT_MIN.
3047 valv
= lhs
.upper_bound ();
3048 minv
= valv
& cst2v
;
3053 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3056 // If we couldn't determine anything on either bound, return
3058 if (we_know_nothing
)
3066 int_range
<1> upper_bits (type
, minv
, maxv
);
3067 r
.intersect (upper_bits
);
3071 operator_bitwise_and::op1_range (irange
&r
, tree type
,
3074 relation_kind rel ATTRIBUTE_UNUSED
) const
3076 if (lhs
.undefined_p ())
3078 if (types_compatible_p (type
, boolean_type_node
))
3079 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
3082 for (unsigned i
= 0; i
< lhs
.num_pairs (); ++i
)
3084 int_range_max
chunk (lhs
.type (),
3085 lhs
.lower_bound (i
),
3086 lhs
.upper_bound (i
));
3088 simple_op1_range_solver (res
, type
, chunk
, op2
);
3091 if (r
.undefined_p ())
3092 set_nonzero_range_from_mask (r
, type
, lhs
);
3094 // For 0 = op1 & MASK, op1 is ~MASK.
3095 if (lhs
.zero_p () && op2
.singleton_p ())
3097 wide_int nz
= wi::bit_not (op2
.get_nonzero_bits ());
3098 int_range
<2> tmp (type
);
3099 tmp
.set_nonzero_bits (nz
);
3106 operator_bitwise_and::op2_range (irange
&r
, tree type
,
3109 relation_kind rel ATTRIBUTE_UNUSED
) const
3111 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
3115 class operator_logical_or
: public range_operator
3117 using range_operator::fold_range
;
3118 using range_operator::op1_range
;
3119 using range_operator::op2_range
;
3121 virtual bool fold_range (irange
&r
, tree type
,
3124 relation_kind rel
= VREL_VARYING
) const;
3125 virtual bool op1_range (irange
&r
, tree type
,
3128 relation_kind rel
= VREL_VARYING
) const;
3129 virtual bool op2_range (irange
&r
, tree type
,
3132 relation_kind rel
= VREL_VARYING
) const;
3136 operator_logical_or::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3139 relation_kind rel ATTRIBUTE_UNUSED
) const
3141 if (empty_range_varying (r
, type
, lh
, rh
))
3150 operator_logical_or::op1_range (irange
&r
, tree type
,
3152 const irange
&op2 ATTRIBUTE_UNUSED
,
3153 relation_kind rel ATTRIBUTE_UNUSED
) const
3155 switch (get_bool_state (r
, lhs
, type
))
3158 // A false result means both sides of the OR must be false.
3159 r
= range_false (type
);
3162 // Any other result means only one side has to be true, the
3163 // other side can be anything. so we can't be sure of any result
3165 r
= range_true_and_false (type
);
3172 operator_logical_or::op2_range (irange
&r
, tree type
,
3175 relation_kind rel ATTRIBUTE_UNUSED
) const
3177 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
3181 class operator_bitwise_or
: public range_operator
3183 using range_operator::op1_range
;
3184 using range_operator::op2_range
;
3186 virtual bool op1_range (irange
&r
, tree type
,
3189 relation_kind rel
= VREL_VARYING
) const;
3190 virtual bool op2_range (irange
&r
, tree type
,
3193 relation_kind rel
= VREL_VARYING
) const;
3194 virtual void wi_fold (irange
&r
, tree type
,
3195 const wide_int
&lh_lb
,
3196 const wide_int
&lh_ub
,
3197 const wide_int
&rh_lb
,
3198 const wide_int
&rh_ub
) const;
3202 operator_bitwise_or::wi_fold (irange
&r
, tree type
,
3203 const wide_int
&lh_lb
,
3204 const wide_int
&lh_ub
,
3205 const wide_int
&rh_lb
,
3206 const wide_int
&rh_ub
) const
3208 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3211 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3212 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3213 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3214 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3215 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3216 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3217 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
3218 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
3219 signop sign
= TYPE_SIGN (type
);
3220 // If the input ranges contain only positive values we can
3221 // truncate the minimum of the result range to the maximum
3222 // of the input range minima.
3223 if (wi::ge_p (lh_lb
, 0, sign
)
3224 && wi::ge_p (rh_lb
, 0, sign
))
3226 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3227 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3229 // If either input range contains only negative values
3230 // we can truncate the minimum of the result range to the
3231 // respective minimum range.
3232 if (wi::lt_p (lh_ub
, 0, sign
))
3233 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3234 if (wi::lt_p (rh_ub
, 0, sign
))
3235 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3236 // If the limits got swapped around, return a conservative range.
3237 if (wi::gt_p (new_lb
, new_ub
, sign
))
3239 // Make sure that nonzero|X is nonzero.
3240 if (wi::gt_p (lh_lb
, 0, sign
)
3241 || wi::gt_p (rh_lb
, 0, sign
)
3242 || wi::lt_p (lh_ub
, 0, sign
)
3243 || wi::lt_p (rh_ub
, 0, sign
))
3244 r
.set_nonzero (type
);
3245 else if (sign
== SIGNED
3246 && wi_optimize_signed_bitwise_op (r
, type
,
3251 r
.set_varying (type
);
3254 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3258 operator_bitwise_or::op1_range (irange
&r
, tree type
,
3261 relation_kind rel ATTRIBUTE_UNUSED
) const
3263 if (lhs
.undefined_p ())
3265 // If this is really a logical wi_fold, call that.
3266 if (types_compatible_p (type
, boolean_type_node
))
3267 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
3271 tree zero
= build_zero_cst (type
);
3272 r
= int_range
<1> (zero
, zero
);
3275 r
.set_varying (type
);
3280 operator_bitwise_or::op2_range (irange
&r
, tree type
,
3283 relation_kind rel ATTRIBUTE_UNUSED
) const
3285 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
3289 class operator_bitwise_xor
: public range_operator
3291 using range_operator::op1_range
;
3292 using range_operator::op2_range
;
3294 virtual void wi_fold (irange
&r
, tree type
,
3295 const wide_int
&lh_lb
,
3296 const wide_int
&lh_ub
,
3297 const wide_int
&rh_lb
,
3298 const wide_int
&rh_ub
) const;
3299 virtual bool op1_range (irange
&r
, tree type
,
3302 relation_kind rel
= VREL_VARYING
) const;
3303 virtual bool op2_range (irange
&r
, tree type
,
3306 relation_kind rel
= VREL_VARYING
) const;
3307 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
3309 const irange
&op1_range
,
3310 const irange
&op2_range
,
3311 relation_kind rel
) const;
3315 operator_bitwise_xor::wi_fold (irange
&r
, tree type
,
3316 const wide_int
&lh_lb
,
3317 const wide_int
&lh_ub
,
3318 const wide_int
&rh_lb
,
3319 const wide_int
&rh_ub
) const
3321 signop sign
= TYPE_SIGN (type
);
3322 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3323 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3324 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3325 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3326 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3327 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3329 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
3330 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
3331 wide_int result_one_bits
3332 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
3333 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
3334 wide_int new_ub
= ~result_zero_bits
;
3335 wide_int new_lb
= result_one_bits
;
3337 // If the range has all positive or all negative values, the result
3338 // is better than VARYING.
3339 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
3340 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3341 else if (sign
== SIGNED
3342 && wi_optimize_signed_bitwise_op (r
, type
,
3347 r
.set_varying (type
);
3349 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3350 if (wi::lt_p (lh_ub
, rh_lb
, sign
)
3351 || wi::lt_p (rh_ub
, lh_lb
, sign
)
3352 || wi::ne_p (result_one_bits
, 0))
3355 tmp
.set_nonzero (type
);
3361 operator_bitwise_xor::op1_op2_relation_effect (irange
&lhs_range
,
3365 relation_kind rel
) const
3367 if (rel
== VREL_VARYING
)
3370 int_range
<2> rel_range
;
3375 rel_range
.set_zero (type
);
3378 rel_range
.set_nonzero (type
);
3384 lhs_range
.intersect (rel_range
);
3389 operator_bitwise_xor::op1_range (irange
&r
, tree type
,
3392 relation_kind rel ATTRIBUTE_UNUSED
) const
3394 if (lhs
.undefined_p () || lhs
.varying_p ())
3399 if (types_compatible_p (type
, boolean_type_node
))
3401 switch (get_bool_state (r
, lhs
, type
))
3404 if (op2
.varying_p ())
3405 r
.set_varying (type
);
3406 else if (op2
.zero_p ())
3407 r
= range_true (type
);
3409 r
= range_false (type
);
3419 r
.set_varying (type
);
3424 operator_bitwise_xor::op2_range (irange
&r
, tree type
,
3427 relation_kind rel ATTRIBUTE_UNUSED
) const
3429 return operator_bitwise_xor::op1_range (r
, type
, lhs
, op1
);
3432 class operator_trunc_mod
: public range_operator
3434 using range_operator::op1_range
;
3435 using range_operator::op2_range
;
3437 virtual void wi_fold (irange
&r
, tree type
,
3438 const wide_int
&lh_lb
,
3439 const wide_int
&lh_ub
,
3440 const wide_int
&rh_lb
,
3441 const wide_int
&rh_ub
) const;
3442 virtual bool op1_range (irange
&r
, tree type
,
3445 relation_kind rel ATTRIBUTE_UNUSED
) const;
3446 virtual bool op2_range (irange
&r
, tree type
,
3449 relation_kind rel ATTRIBUTE_UNUSED
) const;
3453 operator_trunc_mod::wi_fold (irange
&r
, tree type
,
3454 const wide_int
&lh_lb
,
3455 const wide_int
&lh_ub
,
3456 const wide_int
&rh_lb
,
3457 const wide_int
&rh_ub
) const
3459 wide_int new_lb
, new_ub
, tmp
;
3460 signop sign
= TYPE_SIGN (type
);
3461 unsigned prec
= TYPE_PRECISION (type
);
3463 // Mod 0 is undefined.
3464 if (wi_zero_p (type
, rh_lb
, rh_ub
))
3470 // Check for constant and try to fold.
3471 if (lh_lb
== lh_ub
&& rh_lb
== rh_ub
)
3473 wi::overflow_type ov
= wi::OVF_NONE
;
3474 tmp
= wi::mod_trunc (lh_lb
, rh_lb
, sign
, &ov
);
3475 if (ov
== wi::OVF_NONE
)
3477 r
= int_range
<2> (type
, tmp
, tmp
);
3482 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3487 new_ub
= wi::smax (new_ub
, tmp
);
3490 if (sign
== UNSIGNED
)
3491 new_lb
= wi::zero (prec
);
3496 if (wi::gts_p (tmp
, 0))
3497 tmp
= wi::zero (prec
);
3498 new_lb
= wi::smax (new_lb
, tmp
);
3501 if (sign
== SIGNED
&& wi::neg_p (tmp
))
3502 tmp
= wi::zero (prec
);
3503 new_ub
= wi::min (new_ub
, tmp
, sign
);
3505 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3509 operator_trunc_mod::op1_range (irange
&r
, tree type
,
3512 relation_kind rel ATTRIBUTE_UNUSED
) const
3514 if (lhs
.undefined_p ())
3517 signop sign
= TYPE_SIGN (type
);
3518 unsigned prec
= TYPE_PRECISION (type
);
3519 // (a % b) >= x && x > 0 , then a >= x.
3520 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3522 r
= value_range (type
, lhs
.lower_bound (), wi::max_value (prec
, sign
));
3525 // (a % b) <= x && x < 0 , then a <= x.
3526 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3528 r
= value_range (type
, wi::min_value (prec
, sign
), lhs
.upper_bound ());
3535 operator_trunc_mod::op2_range (irange
&r
, tree type
,
3538 relation_kind rel ATTRIBUTE_UNUSED
) const
3540 if (lhs
.undefined_p ())
3543 signop sign
= TYPE_SIGN (type
);
3544 unsigned prec
= TYPE_PRECISION (type
);
3545 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3546 // or b > x for unsigned.
3547 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3550 r
= value_range (type
, wi::neg (lhs
.lower_bound ()),
3551 lhs
.lower_bound (), VR_ANTI_RANGE
);
3552 else if (wi::lt_p (lhs
.lower_bound (), wi::max_value (prec
, sign
),
3554 r
= value_range (type
, lhs
.lower_bound () + 1,
3555 wi::max_value (prec
, sign
));
3560 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3561 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3563 if (wi::gt_p (lhs
.upper_bound (), wi::min_value (prec
, sign
), sign
))
3564 r
= value_range (type
, lhs
.upper_bound (),
3565 wi::neg (lhs
.upper_bound ()), VR_ANTI_RANGE
);
3574 class operator_logical_not
: public range_operator
3576 using range_operator::fold_range
;
3577 using range_operator::op1_range
;
3579 virtual bool fold_range (irange
&r
, tree type
,
3582 relation_kind rel
= VREL_VARYING
) const;
3583 virtual bool op1_range (irange
&r
, tree type
,
3586 relation_kind rel
= VREL_VARYING
) const;
3589 // Folding a logical NOT, oddly enough, involves doing nothing on the
3590 // forward pass through. During the initial walk backwards, the
3591 // logical NOT reversed the desired outcome on the way back, so on the
3592 // way forward all we do is pass the range forward.
3597 // to determine the TRUE branch, walking backward
3598 // if (b_3) if ([1,1])
3599 // b_3 = !b_2 [1,1] = ![0,0]
3600 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3601 // which is the result we are looking for.. so.. pass it through.
3604 operator_logical_not::fold_range (irange
&r
, tree type
,
3606 const irange
&rh ATTRIBUTE_UNUSED
,
3607 relation_kind rel ATTRIBUTE_UNUSED
) const
3609 if (empty_range_varying (r
, type
, lh
, rh
))
3613 if (!lh
.varying_p () && !lh
.undefined_p ())
3620 operator_logical_not::op1_range (irange
&r
,
3624 relation_kind rel ATTRIBUTE_UNUSED
) const
3626 // Logical NOT is involutary...do it again.
3627 return fold_range (r
, type
, lhs
, op2
);
3631 class operator_bitwise_not
: public range_operator
3633 using range_operator::fold_range
;
3634 using range_operator::op1_range
;
3636 virtual bool fold_range (irange
&r
, tree type
,
3639 relation_kind rel
= VREL_VARYING
) const;
3640 virtual bool op1_range (irange
&r
, tree type
,
3643 relation_kind rel
= VREL_VARYING
) const;
3647 operator_bitwise_not::fold_range (irange
&r
, tree type
,
3650 relation_kind rel ATTRIBUTE_UNUSED
) const
3652 if (empty_range_varying (r
, type
, lh
, rh
))
3655 if (types_compatible_p (type
, boolean_type_node
))
3656 return op_logical_not
.fold_range (r
, type
, lh
, rh
);
3658 // ~X is simply -1 - X.
3659 int_range
<1> minusone (type
, wi::minus_one (TYPE_PRECISION (type
)),
3660 wi::minus_one (TYPE_PRECISION (type
)));
3661 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, minusone
, lh
);
3665 operator_bitwise_not::op1_range (irange
&r
, tree type
,
3668 relation_kind rel ATTRIBUTE_UNUSED
) const
3670 if (lhs
.undefined_p ())
3672 if (types_compatible_p (type
, boolean_type_node
))
3673 return op_logical_not
.op1_range (r
, type
, lhs
, op2
);
3675 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3676 return fold_range (r
, type
, lhs
, op2
);
3680 class operator_cst
: public range_operator
3682 using range_operator::fold_range
;
3684 virtual bool fold_range (irange
&r
, tree type
,
3687 relation_kind rel
= VREL_VARYING
) const;
3691 operator_cst::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3693 const irange
&rh ATTRIBUTE_UNUSED
,
3694 relation_kind rel ATTRIBUTE_UNUSED
) const
3701 class operator_identity
: public range_operator
3703 using range_operator::fold_range
;
3704 using range_operator::op1_range
;
3705 using range_operator::lhs_op1_relation
;
3707 virtual bool fold_range (irange
&r
, tree type
,
3710 relation_kind rel
= VREL_VARYING
) const;
3711 virtual bool op1_range (irange
&r
, tree type
,
3714 relation_kind rel
= VREL_VARYING
) const;
3715 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
3718 relation_kind rel
) const;
3721 // Determine if there is a relationship between LHS and OP1.
3724 operator_identity::lhs_op1_relation (const irange
&lhs
,
3725 const irange
&op1 ATTRIBUTE_UNUSED
,
3726 const irange
&op2 ATTRIBUTE_UNUSED
,
3727 relation_kind
) const
3729 if (lhs
.undefined_p ())
3730 return VREL_VARYING
;
3731 // Simply a copy, so they are equivalent.
3736 operator_identity::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3738 const irange
&rh ATTRIBUTE_UNUSED
,
3739 relation_kind rel ATTRIBUTE_UNUSED
) const
3746 operator_identity::op1_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3748 const irange
&op2 ATTRIBUTE_UNUSED
,
3749 relation_kind rel ATTRIBUTE_UNUSED
) const
3756 class operator_unknown
: public range_operator
3758 using range_operator::fold_range
;
3760 virtual bool fold_range (irange
&r
, tree type
,
3763 relation_kind rel
= VREL_VARYING
) const;
3767 operator_unknown::fold_range (irange
&r
, tree type
,
3768 const irange
&lh ATTRIBUTE_UNUSED
,
3769 const irange
&rh ATTRIBUTE_UNUSED
,
3770 relation_kind rel ATTRIBUTE_UNUSED
) const
3772 r
.set_varying (type
);
3777 class operator_abs
: public range_operator
3779 using range_operator::op1_range
;
3781 virtual void wi_fold (irange
&r
, tree type
,
3782 const wide_int
&lh_lb
,
3783 const wide_int
&lh_ub
,
3784 const wide_int
&rh_lb
,
3785 const wide_int
&rh_ub
) const;
3786 virtual bool op1_range (irange
&r
, tree type
,
3789 relation_kind rel ATTRIBUTE_UNUSED
) const;
3793 operator_abs::wi_fold (irange
&r
, tree type
,
3794 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3795 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3796 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3799 signop sign
= TYPE_SIGN (type
);
3800 unsigned prec
= TYPE_PRECISION (type
);
3802 // Pass through LH for the easy cases.
3803 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
3805 r
= int_range
<1> (type
, lh_lb
, lh_ub
);
3809 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3811 wide_int min_value
= wi::min_value (prec
, sign
);
3812 wide_int max_value
= wi::max_value (prec
, sign
);
3813 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
3815 r
.set_varying (type
);
3819 // ABS_EXPR may flip the range around, if the original range
3820 // included negative values.
3821 if (wi::eq_p (lh_lb
, min_value
))
3823 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3824 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3825 if (wi::eq_p (lh_ub
, min_value
))
3827 r
= int_range
<1> (type
, min_value
, min_value
);
3833 min
= wi::abs (lh_lb
);
3835 if (wi::eq_p (lh_ub
, min_value
))
3838 max
= wi::abs (lh_ub
);
3840 // If the range contains zero then we know that the minimum value in the
3841 // range will be zero.
3842 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
3844 if (wi::gt_p (min
, max
, sign
))
3846 min
= wi::zero (prec
);
3850 // If the range was reversed, swap MIN and MAX.
3851 if (wi::gt_p (min
, max
, sign
))
3852 std::swap (min
, max
);
3855 // If the new range has its limits swapped around (MIN > MAX), then
3856 // the operation caused one of them to wrap around. The only thing
3857 // we know is that the result is positive.
3858 if (wi::gt_p (min
, max
, sign
))
3860 min
= wi::zero (prec
);
3863 r
= int_range
<1> (type
, min
, max
);
3867 operator_abs::op1_range (irange
&r
, tree type
,
3870 relation_kind rel ATTRIBUTE_UNUSED
) const
3872 if (empty_range_varying (r
, type
, lhs
, op2
))
3874 if (TYPE_UNSIGNED (type
))
3879 // Start with the positives because negatives are an impossible result.
3880 int_range_max positives
= range_positives (type
);
3881 positives
.intersect (lhs
);
3883 // Then add the negative of each pair:
3884 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3885 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
3886 r
.union_ (int_range
<1> (type
,
3887 -positives
.upper_bound (i
),
3888 -positives
.lower_bound (i
)));
3889 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3890 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3891 wide_int min_value
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
3892 wide_int lb
= lhs
.lower_bound ();
3893 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lb
, min_value
))
3894 r
.union_ (int_range
<2> (type
, lb
, lb
));
3899 class operator_absu
: public range_operator
3902 virtual void wi_fold (irange
&r
, tree type
,
3903 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3904 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3908 operator_absu::wi_fold (irange
&r
, tree type
,
3909 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3910 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3911 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3913 wide_int new_lb
, new_ub
;
3915 // Pass through VR0 the easy cases.
3916 if (wi::ges_p (lh_lb
, 0))
3923 new_lb
= wi::abs (lh_lb
);
3924 new_ub
= wi::abs (lh_ub
);
3926 // If the range contains zero then we know that the minimum
3927 // value in the range will be zero.
3928 if (wi::ges_p (lh_ub
, 0))
3930 if (wi::gtu_p (new_lb
, new_ub
))
3932 new_lb
= wi::zero (TYPE_PRECISION (type
));
3935 std::swap (new_lb
, new_ub
);
3938 gcc_checking_assert (TYPE_UNSIGNED (type
));
3939 r
= int_range
<1> (type
, new_lb
, new_ub
);
3943 class operator_negate
: public range_operator
3945 using range_operator::fold_range
;
3946 using range_operator::op1_range
;
3948 virtual bool fold_range (irange
&r
, tree type
,
3951 relation_kind rel
= VREL_VARYING
) const;
3952 virtual bool op1_range (irange
&r
, tree type
,
3955 relation_kind rel
= VREL_VARYING
) const;
3959 operator_negate::fold_range (irange
&r
, tree type
,
3962 relation_kind rel ATTRIBUTE_UNUSED
) const
3964 if (empty_range_varying (r
, type
, lh
, rh
))
3966 // -X is simply 0 - X.
3967 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
,
3968 range_zero (type
), lh
);
3972 operator_negate::op1_range (irange
&r
, tree type
,
3975 relation_kind rel ATTRIBUTE_UNUSED
) const
3977 // NEGATE is involutory.
3978 return fold_range (r
, type
, lhs
, op2
);
3982 class operator_addr_expr
: public range_operator
3984 using range_operator::fold_range
;
3985 using range_operator::op1_range
;
3987 virtual bool fold_range (irange
&r
, tree type
,
3990 relation_kind rel
= VREL_VARYING
) const;
3991 virtual bool op1_range (irange
&r
, tree type
,
3994 relation_kind rel
= VREL_VARYING
) const;
3998 operator_addr_expr::fold_range (irange
&r
, tree type
,
4001 relation_kind rel ATTRIBUTE_UNUSED
) const
4003 if (empty_range_varying (r
, type
, lh
, rh
))
4006 // Return a non-null pointer of the LHS type (passed in op2).
4008 r
= range_zero (type
);
4009 else if (!lh
.contains_p (build_zero_cst (lh
.type ())))
4010 r
= range_nonzero (type
);
4012 r
.set_varying (type
);
4017 operator_addr_expr::op1_range (irange
&r
, tree type
,
4020 relation_kind rel ATTRIBUTE_UNUSED
) const
4022 return operator_addr_expr::fold_range (r
, type
, lhs
, op2
);
4026 class pointer_plus_operator
: public range_operator
4029 virtual void wi_fold (irange
&r
, tree type
,
4030 const wide_int
&lh_lb
,
4031 const wide_int
&lh_ub
,
4032 const wide_int
&rh_lb
,
4033 const wide_int
&rh_ub
) const;
4037 pointer_plus_operator::wi_fold (irange
&r
, tree type
,
4038 const wide_int
&lh_lb
,
4039 const wide_int
&lh_ub
,
4040 const wide_int
&rh_lb
,
4041 const wide_int
&rh_ub
) const
4043 // Check for [0,0] + const, and simply return the const.
4044 if (lh_lb
== 0 && lh_ub
== 0 && rh_lb
== rh_ub
)
4046 tree val
= wide_int_to_tree (type
, rh_lb
);
4051 // For pointer types, we are really only interested in asserting
4052 // whether the expression evaluates to non-NULL.
4054 // With -fno-delete-null-pointer-checks we need to be more
4055 // conservative. As some object might reside at address 0,
4056 // then some offset could be added to it and the same offset
4057 // subtracted again and the result would be NULL.
4059 // static int a[12]; where &a[0] is NULL and
4062 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
4063 // where the first range doesn't include zero and the second one
4064 // doesn't either. As the second operand is sizetype (unsigned),
4065 // consider all ranges where the MSB could be set as possible
4066 // subtractions where the result might be NULL.
4067 if ((!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
4068 || !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
4069 && !TYPE_OVERFLOW_WRAPS (type
)
4070 && (flag_delete_null_pointer_checks
4071 || !wi::sign_mask (rh_ub
)))
4072 r
= range_nonzero (type
);
4073 else if (lh_lb
== lh_ub
&& lh_lb
== 0
4074 && rh_lb
== rh_ub
&& rh_lb
== 0)
4075 r
= range_zero (type
);
4077 r
.set_varying (type
);
4081 class pointer_min_max_operator
: public range_operator
4084 virtual void wi_fold (irange
& r
, tree type
,
4085 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4086 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
4090 pointer_min_max_operator::wi_fold (irange
&r
, tree type
,
4091 const wide_int
&lh_lb
,
4092 const wide_int
&lh_ub
,
4093 const wide_int
&rh_lb
,
4094 const wide_int
&rh_ub
) const
4096 // For MIN/MAX expressions with pointers, we only care about
4097 // nullness. If both are non null, then the result is nonnull.
4098 // If both are null, then the result is null. Otherwise they
4100 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
4101 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
4102 r
= range_nonzero (type
);
4103 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
4104 r
= range_zero (type
);
4106 r
.set_varying (type
);
4110 class pointer_and_operator
: public range_operator
4113 virtual void wi_fold (irange
&r
, tree type
,
4114 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4115 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
4119 pointer_and_operator::wi_fold (irange
&r
, tree type
,
4120 const wide_int
&lh_lb
,
4121 const wide_int
&lh_ub
,
4122 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
4123 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
4125 // For pointer types, we are really only interested in asserting
4126 // whether the expression evaluates to non-NULL.
4127 if (wi_zero_p (type
, lh_lb
, lh_ub
) || wi_zero_p (type
, lh_lb
, lh_ub
))
4128 r
= range_zero (type
);
4130 r
.set_varying (type
);
4134 class pointer_or_operator
: public range_operator
4136 using range_operator::op1_range
;
4137 using range_operator::op2_range
;
4139 virtual bool op1_range (irange
&r
, tree type
,
4142 relation_kind rel
= VREL_VARYING
) const;
4143 virtual bool op2_range (irange
&r
, tree type
,
4146 relation_kind rel
= VREL_VARYING
) const;
4147 virtual void wi_fold (irange
&r
, tree type
,
4148 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4149 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
4153 pointer_or_operator::op1_range (irange
&r
, tree type
,
4155 const irange
&op2 ATTRIBUTE_UNUSED
,
4156 relation_kind rel ATTRIBUTE_UNUSED
) const
4158 if (lhs
.undefined_p ())
4162 tree zero
= build_zero_cst (type
);
4163 r
= int_range
<1> (zero
, zero
);
4166 r
.set_varying (type
);
4171 pointer_or_operator::op2_range (irange
&r
, tree type
,
4174 relation_kind rel ATTRIBUTE_UNUSED
) const
4176 return pointer_or_operator::op1_range (r
, type
, lhs
, op1
);
4180 pointer_or_operator::wi_fold (irange
&r
, tree type
,
4181 const wide_int
&lh_lb
,
4182 const wide_int
&lh_ub
,
4183 const wide_int
&rh_lb
,
4184 const wide_int
&rh_ub
) const
4186 // For pointer types, we are really only interested in asserting
4187 // whether the expression evaluates to non-NULL.
4188 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
4189 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
4190 r
= range_nonzero (type
);
4191 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
4192 r
= range_zero (type
);
4194 r
.set_varying (type
);
4197 // Return a pointer to the range_operator instance, if there is one
4198 // associated with tree_code CODE.
4201 range_op_table::operator[] (enum tree_code code
)
4203 gcc_checking_assert (code
> 0 && code
< MAX_TREE_CODES
);
4204 return m_range_tree
[code
];
4207 // Add OP to the handler table for CODE.
4210 range_op_table::set (enum tree_code code
, range_operator
&op
)
4212 gcc_checking_assert (m_range_tree
[code
] == NULL
);
4213 m_range_tree
[code
] = &op
;
4216 // Instantiate a range op table for integral operations.
4218 class integral_table
: public range_op_table
4222 } integral_tree_table
;
4224 integral_table::integral_table ()
4226 set (EQ_EXPR
, op_equal
);
4227 set (NE_EXPR
, op_not_equal
);
4228 set (LT_EXPR
, op_lt
);
4229 set (LE_EXPR
, op_le
);
4230 set (GT_EXPR
, op_gt
);
4231 set (GE_EXPR
, op_ge
);
4232 set (PLUS_EXPR
, op_plus
);
4233 set (MINUS_EXPR
, op_minus
);
4234 set (MIN_EXPR
, op_min
);
4235 set (MAX_EXPR
, op_max
);
4236 set (MULT_EXPR
, op_mult
);
4237 set (TRUNC_DIV_EXPR
, op_trunc_div
);
4238 set (FLOOR_DIV_EXPR
, op_floor_div
);
4239 set (ROUND_DIV_EXPR
, op_round_div
);
4240 set (CEIL_DIV_EXPR
, op_ceil_div
);
4241 set (EXACT_DIV_EXPR
, op_exact_div
);
4242 set (LSHIFT_EXPR
, op_lshift
);
4243 set (RSHIFT_EXPR
, op_rshift
);
4244 set (NOP_EXPR
, op_convert
);
4245 set (CONVERT_EXPR
, op_convert
);
4246 set (TRUTH_AND_EXPR
, op_logical_and
);
4247 set (BIT_AND_EXPR
, op_bitwise_and
);
4248 set (TRUTH_OR_EXPR
, op_logical_or
);
4249 set (BIT_IOR_EXPR
, op_bitwise_or
);
4250 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4251 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
4252 set (TRUTH_NOT_EXPR
, op_logical_not
);
4253 set (BIT_NOT_EXPR
, op_bitwise_not
);
4254 set (INTEGER_CST
, op_integer_cst
);
4255 set (SSA_NAME
, op_identity
);
4256 set (PAREN_EXPR
, op_identity
);
4257 set (OBJ_TYPE_REF
, op_identity
);
4258 set (IMAGPART_EXPR
, op_unknown
);
4259 set (REALPART_EXPR
, op_unknown
);
4260 set (POINTER_DIFF_EXPR
, op_pointer_diff
);
4261 set (ABS_EXPR
, op_abs
);
4262 set (ABSU_EXPR
, op_absu
);
4263 set (NEGATE_EXPR
, op_negate
);
4264 set (ADDR_EXPR
, op_addr
);
4267 // Instantiate a range op table for pointer operations.
4269 class pointer_table
: public range_op_table
4273 } pointer_tree_table
;
4275 pointer_table::pointer_table ()
4277 set (BIT_AND_EXPR
, op_pointer_and
);
4278 set (BIT_IOR_EXPR
, op_pointer_or
);
4279 set (MIN_EXPR
, op_ptr_min_max
);
4280 set (MAX_EXPR
, op_ptr_min_max
);
4281 set (POINTER_PLUS_EXPR
, op_pointer_plus
);
4283 set (EQ_EXPR
, op_equal
);
4284 set (NE_EXPR
, op_not_equal
);
4285 set (LT_EXPR
, op_lt
);
4286 set (LE_EXPR
, op_le
);
4287 set (GT_EXPR
, op_gt
);
4288 set (GE_EXPR
, op_ge
);
4289 set (SSA_NAME
, op_identity
);
4290 set (INTEGER_CST
, op_integer_cst
);
4291 set (ADDR_EXPR
, op_addr
);
4292 set (NOP_EXPR
, op_convert
);
4293 set (CONVERT_EXPR
, op_convert
);
4295 set (BIT_NOT_EXPR
, op_bitwise_not
);
4296 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4299 // The tables are hidden and accessed via a simple extern function.
4301 static inline range_operator
*
4302 get_handler (enum tree_code code
, tree type
)
4304 // First check if there is a pointer specialization.
4305 if (POINTER_TYPE_P (type
))
4306 return pointer_tree_table
[code
];
4307 if (INTEGRAL_TYPE_P (type
))
4308 return integral_tree_table
[code
];
4312 // Return the floating point operator for CODE or NULL if none available.
4314 static inline range_operator_float
*
4315 get_float_handler (enum tree_code code
, tree
)
4317 return (*floating_tree_table
)[code
];
4321 range_op_handler::set_op_handler (tree_code code
, tree type
)
4323 if (irange::supports_p (type
))
4326 m_int
= get_handler (code
, type
);
4327 m_valid
= m_int
!= NULL
;
4329 else if (frange::supports_p (type
))
4332 m_float
= get_float_handler (code
, type
);
4333 m_valid
= m_float
!= NULL
;
4343 range_op_handler::range_op_handler ()
4350 range_op_handler::range_op_handler (tree_code code
, tree type
)
4352 set_op_handler (code
, type
);
4357 range_op_handler::fold_range (vrange
&r
, tree type
,
4360 relation_kind rel
) const
4362 gcc_checking_assert (m_valid
);
4364 return m_int
->fold_range (as_a
<irange
> (r
), type
,
4366 as_a
<irange
> (rh
), rel
);
4368 if (is_a
<irange
> (r
))
4370 if (is_a
<irange
> (rh
))
4371 return m_float
->fold_range (as_a
<irange
> (r
), type
,
4373 as_a
<irange
> (rh
), rel
);
4375 return m_float
->fold_range (as_a
<irange
> (r
), type
,
4377 as_a
<frange
> (rh
), rel
);
4379 return m_float
->fold_range (as_a
<frange
> (r
), type
,
4381 as_a
<frange
> (rh
), rel
);
4385 range_op_handler::op1_range (vrange
&r
, tree type
,
4388 relation_kind rel
) const
4390 gcc_checking_assert (m_valid
);
4392 if (lhs
.undefined_p ())
4395 return m_int
->op1_range (as_a
<irange
> (r
), type
,
4396 as_a
<irange
> (lhs
),
4397 as_a
<irange
> (op2
), rel
);
4399 if (is_a
<irange
> (lhs
))
4400 return m_float
->op1_range (as_a
<frange
> (r
), type
,
4401 as_a
<irange
> (lhs
),
4402 as_a
<frange
> (op2
), rel
);
4403 return m_float
->op1_range (as_a
<frange
> (r
), type
,
4404 as_a
<frange
> (lhs
),
4405 as_a
<frange
> (op2
), rel
);
4409 range_op_handler::op2_range (vrange
&r
, tree type
,
4412 relation_kind rel
) const
4414 gcc_checking_assert (m_valid
);
4415 if (lhs
.undefined_p ())
4418 return m_int
->op2_range (as_a
<irange
> (r
), type
,
4419 as_a
<irange
> (lhs
),
4420 as_a
<irange
> (op1
), rel
);
4422 if (is_a
<irange
> (lhs
))
4423 return m_float
->op2_range (as_a
<frange
> (r
), type
,
4424 as_a
<irange
> (lhs
),
4425 as_a
<frange
> (op1
), rel
);
4426 return m_float
->op2_range (as_a
<frange
> (r
), type
,
4427 as_a
<frange
> (lhs
),
4428 as_a
<frange
> (op1
), rel
);
4432 range_op_handler::lhs_op1_relation (const vrange
&lhs
,
4435 relation_kind rel
) const
4437 gcc_checking_assert (m_valid
);
4439 return m_int
->lhs_op1_relation (as_a
<irange
> (lhs
),
4440 as_a
<irange
> (op1
),
4441 as_a
<irange
> (op2
), rel
);
4443 if (is_a
<irange
> (lhs
))
4444 return m_float
->lhs_op1_relation (as_a
<irange
> (lhs
),
4445 as_a
<frange
> (op1
),
4446 as_a
<frange
> (op2
), rel
);
4447 return m_float
->lhs_op1_relation (as_a
<frange
> (lhs
),
4448 as_a
<frange
> (op1
),
4449 as_a
<frange
> (op2
), rel
);
4453 range_op_handler::lhs_op2_relation (const vrange
&lhs
,
4456 relation_kind rel
) const
4458 gcc_checking_assert (m_valid
);
4460 return m_int
->lhs_op2_relation (as_a
<irange
> (lhs
),
4461 as_a
<irange
> (op1
),
4462 as_a
<irange
> (op2
), rel
);
4464 if (is_a
<irange
> (lhs
))
4465 return m_float
->lhs_op2_relation (as_a
<irange
> (lhs
),
4466 as_a
<frange
> (op1
),
4467 as_a
<frange
> (op2
), rel
);
4468 return m_float
->lhs_op2_relation (as_a
<frange
> (lhs
),
4469 as_a
<frange
> (op1
),
4470 as_a
<frange
> (op2
), rel
);
4474 range_op_handler::op1_op2_relation (const vrange
&lhs
) const
4476 gcc_checking_assert (m_valid
);
4478 return m_int
->op1_op2_relation (as_a
<irange
> (lhs
));
4479 return m_float
->op1_op2_relation (as_a
<irange
> (lhs
));
4482 // Cast the range in R to TYPE.
4485 range_cast (vrange
&r
, tree type
)
4487 Value_Range
tmp (r
);
4488 Value_Range
varying (type
);
4489 varying
.set_varying (type
);
4490 range_op_handler
op (CONVERT_EXPR
, type
);
4491 // Call op_convert, if it fails, the result is varying.
4492 if (!op
|| !op
.fold_range (r
, type
, tmp
, varying
))
4494 r
.set_varying (type
);
4501 #include "selftest.h"
4505 #define INT(N) build_int_cst (integer_type_node, (N))
4506 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4507 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4508 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4509 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4510 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4513 range_op_cast_tests ()
4515 int_range
<1> r0
, r1
, r2
, rold
;
4516 r0
.set_varying (integer_type_node
);
4517 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
4519 // If a range is in any way outside of the range for the converted
4520 // to range, default to the range for the new type.
4521 r0
.set_varying (short_integer_type_node
);
4522 tree minshort
= wide_int_to_tree (short_integer_type_node
, r0
.lower_bound ());
4523 tree maxshort
= wide_int_to_tree (short_integer_type_node
, r0
.upper_bound ());
4524 if (TYPE_PRECISION (TREE_TYPE (maxint
))
4525 > TYPE_PRECISION (short_integer_type_node
))
4527 r1
= int_range
<1> (integer_zero_node
, maxint
);
4528 range_cast (r1
, short_integer_type_node
);
4529 ASSERT_TRUE (r1
.lower_bound () == wi::to_wide (minshort
)
4530 && r1
.upper_bound() == wi::to_wide (maxshort
));
4533 // (unsigned char)[-5,-1] => [251,255].
4534 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (-1));
4535 range_cast (r0
, unsigned_char_type_node
);
4536 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (251), UCHAR (255)));
4537 range_cast (r0
, signed_char_type_node
);
4538 ASSERT_TRUE (r0
== rold
);
4540 // (signed char)[15, 150] => [-128,-106][15,127].
4541 r0
= rold
= int_range
<1> (UCHAR (15), UCHAR (150));
4542 range_cast (r0
, signed_char_type_node
);
4543 r1
= int_range
<1> (SCHAR (15), SCHAR (127));
4544 r2
= int_range
<1> (SCHAR (-128), SCHAR (-106));
4546 ASSERT_TRUE (r1
== r0
);
4547 range_cast (r0
, unsigned_char_type_node
);
4548 ASSERT_TRUE (r0
== rold
);
4550 // (unsigned char)[-5, 5] => [0,5][251,255].
4551 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (5));
4552 range_cast (r0
, unsigned_char_type_node
);
4553 r1
= int_range
<1> (UCHAR (251), UCHAR (255));
4554 r2
= int_range
<1> (UCHAR (0), UCHAR (5));
4556 ASSERT_TRUE (r0
== r1
);
4557 range_cast (r0
, signed_char_type_node
);
4558 ASSERT_TRUE (r0
== rold
);
4560 // (unsigned char)[-5,5] => [0,5][251,255].
4561 r0
= int_range
<1> (INT (-5), INT (5));
4562 range_cast (r0
, unsigned_char_type_node
);
4563 r1
= int_range
<1> (UCHAR (0), UCHAR (5));
4564 r1
.union_ (int_range
<1> (UCHAR (251), UCHAR (255)));
4565 ASSERT_TRUE (r0
== r1
);
4567 // (unsigned char)[5U,1974U] => [0,255].
4568 r0
= int_range
<1> (UINT (5), UINT (1974));
4569 range_cast (r0
, unsigned_char_type_node
);
4570 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (0), UCHAR (255)));
4571 range_cast (r0
, integer_type_node
);
4572 // Going to a wider range should not sign extend.
4573 ASSERT_TRUE (r0
== int_range
<1> (INT (0), INT (255)));
4575 // (unsigned char)[-350,15] => [0,255].
4576 r0
= int_range
<1> (INT (-350), INT (15));
4577 range_cast (r0
, unsigned_char_type_node
);
4578 ASSERT_TRUE (r0
== (int_range
<1>
4579 (TYPE_MIN_VALUE (unsigned_char_type_node
),
4580 TYPE_MAX_VALUE (unsigned_char_type_node
))));
4582 // Casting [-120,20] from signed char to unsigned short.
4583 // => [0, 20][0xff88, 0xffff].
4584 r0
= int_range
<1> (SCHAR (-120), SCHAR (20));
4585 range_cast (r0
, short_unsigned_type_node
);
4586 r1
= int_range
<1> (UINT16 (0), UINT16 (20));
4587 r2
= int_range
<1> (UINT16 (0xff88), UINT16 (0xffff));
4589 ASSERT_TRUE (r0
== r1
);
4590 // A truncating cast back to signed char will work because [-120, 20]
4591 // is representable in signed char.
4592 range_cast (r0
, signed_char_type_node
);
4593 ASSERT_TRUE (r0
== int_range
<1> (SCHAR (-120), SCHAR (20)));
4595 // unsigned char -> signed short
4596 // (signed short)[(unsigned char)25, (unsigned char)250]
4597 // => [(signed short)25, (signed short)250]
4598 r0
= rold
= int_range
<1> (UCHAR (25), UCHAR (250));
4599 range_cast (r0
, short_integer_type_node
);
4600 r1
= int_range
<1> (INT16 (25), INT16 (250));
4601 ASSERT_TRUE (r0
== r1
);
4602 range_cast (r0
, unsigned_char_type_node
);
4603 ASSERT_TRUE (r0
== rold
);
4605 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4606 r0
= int_range
<1> (TYPE_MIN_VALUE (long_long_integer_type_node
),
4607 TYPE_MAX_VALUE (long_long_integer_type_node
));
4608 range_cast (r0
, short_unsigned_type_node
);
4609 r1
= int_range
<1> (TYPE_MIN_VALUE (short_unsigned_type_node
),
4610 TYPE_MAX_VALUE (short_unsigned_type_node
));
4611 ASSERT_TRUE (r0
== r1
);
4613 // Casting NONZERO to a narrower type will wrap/overflow so
4614 // it's just the entire range for the narrower type.
4616 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4617 // is outside of the range of a smaller range, return the full
4619 if (TYPE_PRECISION (integer_type_node
)
4620 > TYPE_PRECISION (short_integer_type_node
))
4622 r0
= range_nonzero (integer_type_node
);
4623 range_cast (r0
, short_integer_type_node
);
4624 r1
= int_range
<1> (TYPE_MIN_VALUE (short_integer_type_node
),
4625 TYPE_MAX_VALUE (short_integer_type_node
));
4626 ASSERT_TRUE (r0
== r1
);
4629 // Casting NONZERO from a narrower signed to a wider signed.
4631 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4632 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4633 r0
= range_nonzero (short_integer_type_node
);
4634 range_cast (r0
, integer_type_node
);
4635 r1
= int_range
<1> (INT (-32768), INT (-1));
4636 r2
= int_range
<1> (INT (1), INT (32767));
4638 ASSERT_TRUE (r0
== r1
);
4642 range_op_lshift_tests ()
4644 // Test that 0x808.... & 0x8.... still contains 0x8....
4645 // for a large set of numbers.
4648 tree big_type
= long_long_unsigned_type_node
;
4649 // big_num = 0x808,0000,0000,0000
4650 tree big_num
= fold_build2 (LSHIFT_EXPR
, big_type
,
4651 build_int_cst (big_type
, 0x808),
4652 build_int_cst (big_type
, 48));
4653 op_bitwise_and
.fold_range (res
, big_type
,
4654 int_range
<1> (big_type
),
4655 int_range
<1> (big_num
, big_num
));
4656 // val = 0x8,0000,0000,0000
4657 tree val
= fold_build2 (LSHIFT_EXPR
, big_type
,
4658 build_int_cst (big_type
, 0x8),
4659 build_int_cst (big_type
, 48));
4660 ASSERT_TRUE (res
.contains_p (val
));
4663 if (TYPE_PRECISION (unsigned_type_node
) > 31)
4665 // unsigned VARYING = op1 << 1 should be VARYING.
4666 int_range
<2> lhs (unsigned_type_node
);
4667 int_range
<2> shift (INT (1), INT (1));
4669 op_lshift
.op1_range (op1
, unsigned_type_node
, lhs
, shift
);
4670 ASSERT_TRUE (op1
.varying_p ());
4672 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4673 int_range
<2> zero (UINT (0), UINT (0));
4674 op_lshift
.op1_range (op1
, unsigned_type_node
, zero
, shift
);
4675 ASSERT_TRUE (op1
.num_pairs () == 2);
4676 // Remove the [0,0] range.
4677 op1
.intersect (zero
);
4678 ASSERT_TRUE (op1
.num_pairs () == 1);
4679 // op1 << 1 should be [0x8000,0x8000] << 1,
4680 // which should result in [0,0].
4681 int_range_max result
;
4682 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4683 ASSERT_TRUE (result
== zero
);
4685 // signed VARYING = op1 << 1 should be VARYING.
4686 if (TYPE_PRECISION (integer_type_node
) > 31)
4688 // unsigned VARYING = op1 << 1 hould be VARYING.
4689 int_range
<2> lhs (integer_type_node
);
4690 int_range
<2> shift (INT (1), INT (1));
4692 op_lshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4693 ASSERT_TRUE (op1
.varying_p ());
4695 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4696 int_range
<2> zero (INT (0), INT (0));
4697 op_lshift
.op1_range (op1
, integer_type_node
, zero
, shift
);
4698 ASSERT_TRUE (op1
.num_pairs () == 2);
4699 // Remove the [0,0] range.
4700 op1
.intersect (zero
);
4701 ASSERT_TRUE (op1
.num_pairs () == 1);
4702 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4703 // which should result in [0,0].
4704 int_range_max result
;
4705 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4706 ASSERT_TRUE (result
== zero
);
4711 range_op_rshift_tests ()
4713 // unsigned: [3, MAX] = OP1 >> 1
4715 int_range_max
lhs (build_int_cst (unsigned_type_node
, 3),
4716 TYPE_MAX_VALUE (unsigned_type_node
));
4717 int_range_max
one (build_one_cst (unsigned_type_node
),
4718 build_one_cst (unsigned_type_node
));
4720 op_rshift
.op1_range (op1
, unsigned_type_node
, lhs
, one
);
4721 ASSERT_FALSE (op1
.contains_p (UINT (3)));
4724 // signed: [3, MAX] = OP1 >> 1
4726 int_range_max
lhs (INT (3), TYPE_MAX_VALUE (integer_type_node
));
4727 int_range_max
one (INT (1), INT (1));
4729 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4730 ASSERT_FALSE (op1
.contains_p (INT (-2)));
4733 // This is impossible, so OP1 should be [].
4734 // signed: [MIN, MIN] = OP1 >> 1
4736 int_range_max
lhs (TYPE_MIN_VALUE (integer_type_node
),
4737 TYPE_MIN_VALUE (integer_type_node
));
4738 int_range_max
one (INT (1), INT (1));
4740 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4741 ASSERT_TRUE (op1
.undefined_p ());
4744 // signed: ~[-1] = OP1 >> 31
4745 if (TYPE_PRECISION (integer_type_node
) > 31)
4747 int_range_max
lhs (INT (-1), INT (-1), VR_ANTI_RANGE
);
4748 int_range_max
shift (INT (31), INT (31));
4750 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4751 int_range_max negatives
= range_negatives (integer_type_node
);
4752 negatives
.intersect (op1
);
4753 ASSERT_TRUE (negatives
.undefined_p ());
4758 range_op_bitwise_and_tests ()
4761 tree min
= vrp_val_min (integer_type_node
);
4762 tree max
= vrp_val_max (integer_type_node
);
4763 tree tiny
= fold_build2 (PLUS_EXPR
, integer_type_node
, min
,
4764 build_one_cst (integer_type_node
));
4765 int_range_max
i1 (tiny
, max
);
4766 int_range_max
i2 (build_int_cst (integer_type_node
, 255),
4767 build_int_cst (integer_type_node
, 255));
4769 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4770 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4771 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4773 // VARYING = OP1 & 255: OP1 is VARYING
4774 i1
= int_range
<1> (integer_type_node
);
4775 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4776 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4778 // For 0 = x & MASK, x is ~MASK.
4780 int_range
<2> zero (integer_zero_node
, integer_zero_node
);
4781 int_range
<2> mask
= int_range
<2> (INT (7), INT (7));
4782 op_bitwise_and
.op1_range (res
, integer_type_node
, zero
, mask
);
4783 wide_int inv
= wi::shwi (~7U, TYPE_PRECISION (integer_type_node
));
4784 ASSERT_TRUE (res
.get_nonzero_bits () == inv
);
4787 // (NONZERO | X) is nonzero.
4788 i1
.set_nonzero (integer_type_node
);
4789 i2
.set_varying (integer_type_node
);
4790 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4791 ASSERT_TRUE (res
.nonzero_p ());
4793 // (NEGATIVE | X) is nonzero.
4794 i1
= int_range
<1> (INT (-5), INT (-3));
4795 i2
.set_varying (integer_type_node
);
4796 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4797 ASSERT_FALSE (res
.contains_p (INT (0)));
4801 range_relational_tests ()
4803 int_range
<2> lhs (unsigned_char_type_node
);
4804 int_range
<2> op1 (UCHAR (8), UCHAR (10));
4805 int_range
<2> op2 (UCHAR (20), UCHAR (20));
4807 // Never wrapping additions mean LHS > OP1.
4808 relation_kind code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4809 ASSERT_TRUE (code
== VREL_GT
);
4811 // Most wrapping additions mean nothing...
4812 op1
= int_range
<2> (UCHAR (8), UCHAR (10));
4813 op2
= int_range
<2> (UCHAR (0), UCHAR (255));
4814 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4815 ASSERT_TRUE (code
== VREL_VARYING
);
4817 // However, always wrapping additions mean LHS < OP1.
4818 op1
= int_range
<2> (UCHAR (1), UCHAR (255));
4819 op2
= int_range
<2> (UCHAR (255), UCHAR (255));
4820 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4821 ASSERT_TRUE (code
== VREL_LT
);
4827 range_op_rshift_tests ();
4828 range_op_lshift_tests ();
4829 range_op_bitwise_and_tests ();
4830 range_op_cast_tests ();
4831 range_relational_tests ();
4834 } // namespace selftest
4836 #endif // CHECKING_P