1 /* Code for range operators.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "insn-codes.h"
31 #include "tree-pass.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
41 #include "gimple-iterator.h"
42 #include "gimple-fold.h"
44 #include "gimple-walk.h"
47 #include "value-relation.h"
50 // Return the upper limit for a type.
52 static inline wide_int
53 max_limit (const_tree type
)
55 return wi::max_value (TYPE_PRECISION (type
) , TYPE_SIGN (type
));
58 // Return the lower limit for a type.
60 static inline wide_int
61 min_limit (const_tree type
)
63 return wi::min_value (TYPE_PRECISION (type
) , TYPE_SIGN (type
));
66 // Return false if shifting by OP is undefined behavior. Otherwise, return
67 // true and the range it is to be shifted by. This allows trimming out of
68 // undefined ranges, leaving only valid ranges if there are any.
71 get_shift_range (irange
&r
, tree type
, const irange
&op
)
73 if (op
.undefined_p ())
76 // Build valid range and intersect it with the shift range.
77 r
= value_range (build_int_cst_type (op
.type (), 0),
78 build_int_cst_type (op
.type (), TYPE_PRECISION (type
) - 1));
81 // If there are no valid ranges in the shift range, returned false.
87 // Return TRUE if 0 is within [WMIN, WMAX].
90 wi_includes_zero_p (tree type
, const wide_int
&wmin
, const wide_int
&wmax
)
92 signop sign
= TYPE_SIGN (type
);
93 return wi::le_p (wmin
, 0, sign
) && wi::ge_p (wmax
, 0, sign
);
96 // Return TRUE if [WMIN, WMAX] is the singleton 0.
99 wi_zero_p (tree type
, const wide_int
&wmin
, const wide_int
&wmax
)
101 unsigned prec
= TYPE_PRECISION (type
);
102 return wmin
== wmax
&& wi::eq_p (wmin
, wi::zero (prec
));
105 // Default wide_int fold operation returns [MIN, MAX].
108 range_operator::wi_fold (irange
&r
, tree type
,
109 const wide_int
&lh_lb ATTRIBUTE_UNUSED
,
110 const wide_int
&lh_ub ATTRIBUTE_UNUSED
,
111 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
112 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
114 gcc_checking_assert (r
.supports_type_p (type
));
115 r
.set_varying (type
);
118 // Call wi_fold, except further split small subranges into constants.
119 // This can provide better precision. For something 8 >> [0,1]
120 // Instead of [8, 16], we will produce [8,8][16,16]
123 range_operator::wi_fold_in_parts (irange
&r
, tree type
,
124 const wide_int
&lh_lb
,
125 const wide_int
&lh_ub
,
126 const wide_int
&rh_lb
,
127 const wide_int
&rh_ub
) const
130 widest_int rh_range
= wi::sub (widest_int::from (rh_ub
, TYPE_SIGN (type
)),
131 widest_int::from (rh_lb
, TYPE_SIGN (type
)));
132 widest_int lh_range
= wi::sub (widest_int::from (lh_ub
, TYPE_SIGN (type
)),
133 widest_int::from (lh_lb
, TYPE_SIGN (type
)));
134 // If there are 2, 3, or 4 values in the RH range, do them separately.
135 // Call wi_fold_in_parts to check the RH side.
136 if (rh_range
> 0 && rh_range
< 4)
138 wi_fold_in_parts (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_lb
);
141 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 1, rh_lb
+ 1);
145 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 2, rh_lb
+ 2);
149 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_ub
, rh_ub
);
152 // Otherise check for 2, 3, or 4 values in the LH range and split them up.
153 // The RH side has been checked, so no recursion needed.
154 else if (lh_range
> 0 && lh_range
< 4)
156 wi_fold (r
, type
, lh_lb
, lh_lb
, rh_lb
, rh_ub
);
159 wi_fold (tmp
, type
, lh_lb
+ 1, lh_lb
+ 1, rh_lb
, rh_ub
);
163 wi_fold (tmp
, type
, lh_lb
+ 2, lh_lb
+ 2, rh_lb
, rh_ub
);
167 wi_fold (tmp
, type
, lh_ub
, lh_ub
, rh_lb
, rh_ub
);
170 // Otherwise just call wi_fold.
172 wi_fold (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
175 // The default for fold is to break all ranges into sub-ranges and
176 // invoke the wi_fold method on each sub-range pair.
179 range_operator::fold_range (irange
&r
, tree type
,
182 relation_kind rel
) const
184 gcc_checking_assert (r
.supports_type_p (type
));
185 if (empty_range_varying (r
, type
, lh
, rh
))
188 unsigned num_lh
= lh
.num_pairs ();
189 unsigned num_rh
= rh
.num_pairs ();
191 // If both ranges are single pairs, fold directly into the result range.
192 // If the number of subranges grows too high, produce a summary result as the
193 // loop becomes exponential with little benefit. See PR 103821.
194 if ((num_lh
== 1 && num_rh
== 1) || num_lh
* num_rh
> 12)
196 wi_fold_in_parts (r
, type
, lh
.lower_bound (), lh
.upper_bound (),
197 rh
.lower_bound (), rh
.upper_bound ());
198 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
204 for (unsigned x
= 0; x
< num_lh
; ++x
)
205 for (unsigned y
= 0; y
< num_rh
; ++y
)
207 wide_int lh_lb
= lh
.lower_bound (x
);
208 wide_int lh_ub
= lh
.upper_bound (x
);
209 wide_int rh_lb
= rh
.lower_bound (y
);
210 wide_int rh_ub
= rh
.upper_bound (y
);
211 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
215 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
219 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
223 // The default for op1_range is to return false.
226 range_operator::op1_range (irange
&r ATTRIBUTE_UNUSED
,
227 tree type ATTRIBUTE_UNUSED
,
228 const irange
&lhs ATTRIBUTE_UNUSED
,
229 const irange
&op2 ATTRIBUTE_UNUSED
,
230 relation_kind rel ATTRIBUTE_UNUSED
) const
235 // The default for op2_range is to return false.
238 range_operator::op2_range (irange
&r ATTRIBUTE_UNUSED
,
239 tree type ATTRIBUTE_UNUSED
,
240 const irange
&lhs ATTRIBUTE_UNUSED
,
241 const irange
&op1 ATTRIBUTE_UNUSED
,
242 relation_kind rel ATTRIBUTE_UNUSED
) const
247 // The default relation routines return VREL_VARYING.
250 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
251 const irange
&op1 ATTRIBUTE_UNUSED
,
252 const irange
&op2 ATTRIBUTE_UNUSED
,
253 relation_kind rel ATTRIBUTE_UNUSED
) const
259 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
260 const irange
&op1 ATTRIBUTE_UNUSED
,
261 const irange
&op2 ATTRIBUTE_UNUSED
,
262 relation_kind rel ATTRIBUTE_UNUSED
) const
268 range_operator::op1_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
) const
273 // Default is no relation affects the LHS.
276 range_operator::op1_op2_relation_effect (irange
&lhs_range ATTRIBUTE_UNUSED
,
277 tree type ATTRIBUTE_UNUSED
,
278 const irange
&op1_range ATTRIBUTE_UNUSED
,
279 const irange
&op2_range ATTRIBUTE_UNUSED
,
280 relation_kind rel ATTRIBUTE_UNUSED
) const
285 // Create and return a range from a pair of wide-ints that are known
286 // to have overflowed (or underflowed).
289 value_range_from_overflowed_bounds (irange
&r
, tree type
,
290 const wide_int
&wmin
,
291 const wide_int
&wmax
)
293 const signop sgn
= TYPE_SIGN (type
);
294 const unsigned int prec
= TYPE_PRECISION (type
);
296 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
297 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
302 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
305 if (wi::cmp (tmax
, tem
, sgn
) > 0)
308 // If the anti-range would cover nothing, drop to varying.
309 // Likewise if the anti-range bounds are outside of the types
311 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
312 r
.set_varying (type
);
315 tree tree_min
= wide_int_to_tree (type
, tmin
);
316 tree tree_max
= wide_int_to_tree (type
, tmax
);
317 r
.set (tree_min
, tree_max
, VR_ANTI_RANGE
);
321 // Create and return a range from a pair of wide-ints. MIN_OVF and
322 // MAX_OVF describe any overflow that might have occurred while
323 // calculating WMIN and WMAX respectively.
326 value_range_with_overflow (irange
&r
, tree type
,
327 const wide_int
&wmin
, const wide_int
&wmax
,
328 wi::overflow_type min_ovf
= wi::OVF_NONE
,
329 wi::overflow_type max_ovf
= wi::OVF_NONE
)
331 const signop sgn
= TYPE_SIGN (type
);
332 const unsigned int prec
= TYPE_PRECISION (type
);
333 const bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
335 // For one bit precision if max != min, then the range covers all
337 if (prec
== 1 && wi::ne_p (wmax
, wmin
))
339 r
.set_varying (type
);
345 // If overflow wraps, truncate the values and adjust the range,
346 // kind, and bounds appropriately.
347 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
349 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
350 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
351 // If the limits are swapped, we wrapped around and cover
353 if (wi::gt_p (tmin
, tmax
, sgn
))
354 r
.set_varying (type
);
356 // No overflow or both overflow or underflow. The range
357 // kind stays normal.
358 r
.set (wide_int_to_tree (type
, tmin
),
359 wide_int_to_tree (type
, tmax
));
363 if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
364 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
365 value_range_from_overflowed_bounds (r
, type
, wmin
, wmax
);
367 // Other underflow and/or overflow, drop to VR_VARYING.
368 r
.set_varying (type
);
372 // If both bounds either underflowed or overflowed, then the result
374 if ((min_ovf
== wi::OVF_OVERFLOW
&& max_ovf
== wi::OVF_OVERFLOW
)
375 || (min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_UNDERFLOW
))
381 // If overflow does not wrap, saturate to [MIN, MAX].
382 wide_int new_lb
, new_ub
;
383 if (min_ovf
== wi::OVF_UNDERFLOW
)
384 new_lb
= wi::min_value (prec
, sgn
);
385 else if (min_ovf
== wi::OVF_OVERFLOW
)
386 new_lb
= wi::max_value (prec
, sgn
);
390 if (max_ovf
== wi::OVF_UNDERFLOW
)
391 new_ub
= wi::min_value (prec
, sgn
);
392 else if (max_ovf
== wi::OVF_OVERFLOW
)
393 new_ub
= wi::max_value (prec
, sgn
);
397 r
.set (wide_int_to_tree (type
, new_lb
),
398 wide_int_to_tree (type
, new_ub
));
402 // Create and return a range from a pair of wide-ints. Canonicalize
403 // the case where the bounds are swapped. In which case, we transform
404 // [10,5] into [MIN,5][10,MAX].
407 create_possibly_reversed_range (irange
&r
, tree type
,
408 const wide_int
&new_lb
, const wide_int
&new_ub
)
410 signop s
= TYPE_SIGN (type
);
411 // If the bounds are swapped, treat the result as if an overflow occured.
412 if (wi::gt_p (new_lb
, new_ub
, s
))
413 value_range_from_overflowed_bounds (r
, type
, new_lb
, new_ub
);
415 // Otherwise it's just a normal range.
416 r
.set (wide_int_to_tree (type
, new_lb
), wide_int_to_tree (type
, new_ub
));
419 // Return the summary information about boolean range LHS. If EMPTY/FULL,
420 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
423 get_bool_state (vrange
&r
, const vrange
&lhs
, tree val_type
)
425 // If there is no result, then this is unexecutable.
426 if (lhs
.undefined_p ())
435 // For TRUE, we can't just test for [1,1] because Ada can have
436 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
437 if (lhs
.contains_p (build_zero_cst (lhs
.type ())))
439 r
.set_varying (val_type
);
447 class operator_equal
: public range_operator
449 using range_operator::fold_range
;
450 using range_operator::op1_range
;
451 using range_operator::op2_range
;
453 virtual bool fold_range (irange
&r
, tree type
,
456 relation_kind rel
= VREL_VARYING
) const;
457 virtual bool op1_range (irange
&r
, tree type
,
460 relation_kind rel
= VREL_VARYING
) const;
461 virtual bool op2_range (irange
&r
, tree type
,
464 relation_kind rel
= VREL_VARYING
) const;
465 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
468 // Check if the LHS range indicates a relation between OP1 and OP2.
471 equal_op1_op2_relation (const irange
&lhs
)
473 if (lhs
.undefined_p ())
474 return VREL_UNDEFINED
;
476 // FALSE = op1 == op2 indicates NE_EXPR.
480 // TRUE = op1 == op2 indicates EQ_EXPR.
481 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
487 operator_equal::op1_op2_relation (const irange
&lhs
) const
489 return equal_op1_op2_relation (lhs
);
494 operator_equal::fold_range (irange
&r
, tree type
,
497 relation_kind rel
) const
499 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_EQ
))
502 // We can be sure the values are always equal or not if both ranges
503 // consist of a single value, and then compare them.
504 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
505 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
507 if (wi::eq_p (op1
.lower_bound (), op2
.upper_bound()))
508 r
= range_true (type
);
510 r
= range_false (type
);
514 // If ranges do not intersect, we know the range is not equal,
515 // otherwise we don't know anything for sure.
516 int_range_max tmp
= op1
;
518 if (tmp
.undefined_p ())
519 r
= range_false (type
);
521 r
= range_true_and_false (type
);
527 operator_equal::op1_range (irange
&r
, tree type
,
530 relation_kind rel ATTRIBUTE_UNUSED
) const
532 switch (get_bool_state (r
, lhs
, type
))
535 // If the result is false, the only time we know anything is
536 // if OP2 is a constant.
537 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
543 r
.set_varying (type
);
547 // If it's true, the result is the same as OP2.
558 operator_equal::op2_range (irange
&r
, tree type
,
561 relation_kind rel
) const
563 return operator_equal::op1_range (r
, type
, lhs
, op1
, rel
);
566 class operator_not_equal
: public range_operator
568 using range_operator::fold_range
;
569 using range_operator::op1_range
;
570 using range_operator::op2_range
;
572 virtual bool fold_range (irange
&r
, tree type
,
575 relation_kind rel
= VREL_VARYING
) const;
576 virtual bool op1_range (irange
&r
, tree type
,
579 relation_kind rel
= VREL_VARYING
) const;
580 virtual bool op2_range (irange
&r
, tree type
,
583 relation_kind rel
= VREL_VARYING
) const;
584 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
587 // Check if the LHS range indicates a relation between OP1 and OP2.
590 not_equal_op1_op2_relation (const irange
&lhs
)
592 if (lhs
.undefined_p ())
593 return VREL_UNDEFINED
;
595 // FALSE = op1 != op2 indicates EQ_EXPR.
599 // TRUE = op1 != op2 indicates NE_EXPR.
600 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
606 operator_not_equal::op1_op2_relation (const irange
&lhs
) const
608 return not_equal_op1_op2_relation (lhs
);
612 operator_not_equal::fold_range (irange
&r
, tree type
,
615 relation_kind rel
) const
617 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_NE
))
620 // We can be sure the values are always equal or not if both ranges
621 // consist of a single value, and then compare them.
622 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
623 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
625 if (wi::ne_p (op1
.lower_bound (), op2
.upper_bound()))
626 r
= range_true (type
);
628 r
= range_false (type
);
632 // If ranges do not intersect, we know the range is not equal,
633 // otherwise we don't know anything for sure.
634 int_range_max tmp
= op1
;
636 if (tmp
.undefined_p ())
637 r
= range_true (type
);
639 r
= range_true_and_false (type
);
645 operator_not_equal::op1_range (irange
&r
, tree type
,
648 relation_kind rel ATTRIBUTE_UNUSED
) const
650 switch (get_bool_state (r
, lhs
, type
))
653 // If the result is true, the only time we know anything is if
654 // OP2 is a constant.
655 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
661 r
.set_varying (type
);
665 // If it's false, the result is the same as OP2.
677 operator_not_equal::op2_range (irange
&r
, tree type
,
680 relation_kind rel
) const
682 return operator_not_equal::op1_range (r
, type
, lhs
, op1
, rel
);
685 // (X < VAL) produces the range of [MIN, VAL - 1].
688 build_lt (irange
&r
, tree type
, const wide_int
&val
)
690 wi::overflow_type ov
;
692 signop sgn
= TYPE_SIGN (type
);
694 // Signed 1 bit cannot represent 1 for subtraction.
696 lim
= wi::add (val
, -1, sgn
, &ov
);
698 lim
= wi::sub (val
, 1, sgn
, &ov
);
700 // If val - 1 underflows, check if X < MIN, which is an empty range.
704 r
= int_range
<1> (type
, min_limit (type
), lim
);
707 // (X <= VAL) produces the range of [MIN, VAL].
710 build_le (irange
&r
, tree type
, const wide_int
&val
)
712 r
= int_range
<1> (type
, min_limit (type
), val
);
715 // (X > VAL) produces the range of [VAL + 1, MAX].
718 build_gt (irange
&r
, tree type
, const wide_int
&val
)
720 wi::overflow_type ov
;
722 signop sgn
= TYPE_SIGN (type
);
724 // Signed 1 bit cannot represent 1 for addition.
726 lim
= wi::sub (val
, -1, sgn
, &ov
);
728 lim
= wi::add (val
, 1, sgn
, &ov
);
729 // If val + 1 overflows, check is for X > MAX, which is an empty range.
733 r
= int_range
<1> (type
, lim
, max_limit (type
));
736 // (X >= val) produces the range of [VAL, MAX].
739 build_ge (irange
&r
, tree type
, const wide_int
&val
)
741 r
= int_range
<1> (type
, val
, max_limit (type
));
745 class operator_lt
: public range_operator
747 using range_operator::fold_range
;
748 using range_operator::op1_range
;
749 using range_operator::op2_range
;
751 virtual bool fold_range (irange
&r
, tree type
,
754 relation_kind rel
= VREL_VARYING
) const;
755 virtual bool op1_range (irange
&r
, tree type
,
758 relation_kind rel
= VREL_VARYING
) const;
759 virtual bool op2_range (irange
&r
, tree type
,
762 relation_kind rel
= VREL_VARYING
) const;
763 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
766 // Check if the LHS range indicates a relation between OP1 and OP2.
769 lt_op1_op2_relation (const irange
&lhs
)
771 if (lhs
.undefined_p ())
772 return VREL_UNDEFINED
;
774 // FALSE = op1 < op2 indicates GE_EXPR.
778 // TRUE = op1 < op2 indicates LT_EXPR.
779 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
785 operator_lt::op1_op2_relation (const irange
&lhs
) const
787 return lt_op1_op2_relation (lhs
);
791 operator_lt::fold_range (irange
&r
, tree type
,
794 relation_kind rel
) const
796 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LT
))
799 signop sign
= TYPE_SIGN (op1
.type ());
800 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
802 if (wi::lt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
803 r
= range_true (type
);
804 else if (!wi::lt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
805 r
= range_false (type
);
806 // Use nonzero bits to determine if < 0 is false.
807 else if (op2
.zero_p () && !wi::neg_p (op1
.get_nonzero_bits (), sign
))
808 r
= range_false (type
);
810 r
= range_true_and_false (type
);
815 operator_lt::op1_range (irange
&r
, tree type
,
818 relation_kind rel ATTRIBUTE_UNUSED
) const
820 switch (get_bool_state (r
, lhs
, type
))
823 build_lt (r
, type
, op2
.upper_bound ());
827 build_ge (r
, type
, op2
.lower_bound ());
837 operator_lt::op2_range (irange
&r
, tree type
,
840 relation_kind rel ATTRIBUTE_UNUSED
) const
842 switch (get_bool_state (r
, lhs
, type
))
845 build_le (r
, type
, op1
.upper_bound ());
849 build_gt (r
, type
, op1
.lower_bound ());
859 class operator_le
: public range_operator
861 using range_operator::fold_range
;
862 using range_operator::op1_range
;
863 using range_operator::op2_range
;
865 virtual bool fold_range (irange
&r
, tree type
,
868 relation_kind rel
= VREL_VARYING
) const;
869 virtual bool op1_range (irange
&r
, tree type
,
872 relation_kind rel
= VREL_VARYING
) const;
873 virtual bool op2_range (irange
&r
, tree type
,
876 relation_kind rel
= VREL_VARYING
) const;
877 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
880 // Check if the LHS range indicates a relation between OP1 and OP2.
883 le_op1_op2_relation (const irange
&lhs
)
885 if (lhs
.undefined_p ())
886 return VREL_UNDEFINED
;
888 // FALSE = op1 <= op2 indicates GT_EXPR.
892 // TRUE = op1 <= op2 indicates LE_EXPR.
893 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
899 operator_le::op1_op2_relation (const irange
&lhs
) const
901 return le_op1_op2_relation (lhs
);
905 operator_le::fold_range (irange
&r
, tree type
,
908 relation_kind rel
) const
910 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LE
))
913 signop sign
= TYPE_SIGN (op1
.type ());
914 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
916 if (wi::le_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
917 r
= range_true (type
);
918 else if (!wi::le_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
919 r
= range_false (type
);
921 r
= range_true_and_false (type
);
926 operator_le::op1_range (irange
&r
, tree type
,
929 relation_kind rel ATTRIBUTE_UNUSED
) const
931 switch (get_bool_state (r
, lhs
, type
))
934 build_le (r
, type
, op2
.upper_bound ());
938 build_gt (r
, type
, op2
.lower_bound ());
948 operator_le::op2_range (irange
&r
, tree type
,
951 relation_kind rel ATTRIBUTE_UNUSED
) const
953 switch (get_bool_state (r
, lhs
, type
))
956 build_lt (r
, type
, op1
.upper_bound ());
960 build_ge (r
, type
, op1
.lower_bound ());
970 class operator_gt
: public range_operator
972 using range_operator::fold_range
;
973 using range_operator::op1_range
;
974 using range_operator::op2_range
;
976 virtual bool fold_range (irange
&r
, tree type
,
979 relation_kind rel
= VREL_VARYING
) const;
980 virtual bool op1_range (irange
&r
, tree type
,
983 relation_kind rel
= VREL_VARYING
) const;
984 virtual bool op2_range (irange
&r
, tree type
,
987 relation_kind rel
= VREL_VARYING
) const;
988 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
991 // Check if the LHS range indicates a relation between OP1 and OP2.
994 gt_op1_op2_relation (const irange
&lhs
)
996 if (lhs
.undefined_p ())
997 return VREL_UNDEFINED
;
999 // FALSE = op1 > op2 indicates LE_EXPR.
1003 // TRUE = op1 > op2 indicates GT_EXPR.
1004 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
1006 return VREL_VARYING
;
1010 operator_gt::op1_op2_relation (const irange
&lhs
) const
1012 return gt_op1_op2_relation (lhs
);
1017 operator_gt::fold_range (irange
&r
, tree type
,
1018 const irange
&op1
, const irange
&op2
,
1019 relation_kind rel
) const
1021 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GT
))
1024 signop sign
= TYPE_SIGN (op1
.type ());
1025 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1027 if (wi::gt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1028 r
= range_true (type
);
1029 else if (!wi::gt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1030 r
= range_false (type
);
1032 r
= range_true_and_false (type
);
1037 operator_gt::op1_range (irange
&r
, tree type
,
1038 const irange
&lhs
, const irange
&op2
,
1039 relation_kind rel ATTRIBUTE_UNUSED
) const
1041 switch (get_bool_state (r
, lhs
, type
))
1044 build_gt (r
, type
, op2
.lower_bound ());
1048 build_le (r
, type
, op2
.upper_bound ());
1058 operator_gt::op2_range (irange
&r
, tree type
,
1061 relation_kind rel ATTRIBUTE_UNUSED
) const
1063 switch (get_bool_state (r
, lhs
, type
))
1066 build_ge (r
, type
, op1
.lower_bound ());
1070 build_lt (r
, type
, op1
.upper_bound ());
1080 class operator_ge
: public range_operator
1082 using range_operator::fold_range
;
1083 using range_operator::op1_range
;
1084 using range_operator::op2_range
;
1086 virtual bool fold_range (irange
&r
, tree type
,
1089 relation_kind rel
= VREL_VARYING
) const;
1090 virtual bool op1_range (irange
&r
, tree type
,
1093 relation_kind rel
= VREL_VARYING
) const;
1094 virtual bool op2_range (irange
&r
, tree type
,
1097 relation_kind rel
= VREL_VARYING
) const;
1098 virtual relation_kind
op1_op2_relation (const irange
&lhs
) const;
1101 // Check if the LHS range indicates a relation between OP1 and OP2.
1104 ge_op1_op2_relation (const irange
&lhs
)
1106 if (lhs
.undefined_p ())
1107 return VREL_UNDEFINED
;
1109 // FALSE = op1 >= op2 indicates LT_EXPR.
1113 // TRUE = op1 >= op2 indicates GE_EXPR.
1114 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
1116 return VREL_VARYING
;
1120 operator_ge::op1_op2_relation (const irange
&lhs
) const
1122 return ge_op1_op2_relation (lhs
);
1126 operator_ge::fold_range (irange
&r
, tree type
,
1129 relation_kind rel
) const
1131 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GE
))
1134 signop sign
= TYPE_SIGN (op1
.type ());
1135 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1137 if (wi::ge_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1138 r
= range_true (type
);
1139 else if (!wi::ge_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1140 r
= range_false (type
);
1142 r
= range_true_and_false (type
);
1147 operator_ge::op1_range (irange
&r
, tree type
,
1150 relation_kind rel ATTRIBUTE_UNUSED
) const
1152 switch (get_bool_state (r
, lhs
, type
))
1155 build_ge (r
, type
, op2
.lower_bound ());
1159 build_lt (r
, type
, op2
.upper_bound ());
1169 operator_ge::op2_range (irange
&r
, tree type
,
1172 relation_kind rel ATTRIBUTE_UNUSED
) const
1174 switch (get_bool_state (r
, lhs
, type
))
1177 build_gt (r
, type
, op1
.lower_bound ());
1181 build_le (r
, type
, op1
.upper_bound ());
1191 class operator_plus
: public range_operator
1193 using range_operator::op1_range
;
1194 using range_operator::op2_range
;
1195 using range_operator::lhs_op1_relation
;
1196 using range_operator::lhs_op2_relation
;
1198 virtual bool op1_range (irange
&r
, tree type
,
1201 relation_kind rel ATTRIBUTE_UNUSED
) const;
1202 virtual bool op2_range (irange
&r
, tree type
,
1205 relation_kind rel ATTRIBUTE_UNUSED
) const;
1206 virtual void wi_fold (irange
&r
, tree type
,
1207 const wide_int
&lh_lb
,
1208 const wide_int
&lh_ub
,
1209 const wide_int
&rh_lb
,
1210 const wide_int
&rh_ub
) const;
1211 virtual relation_kind
lhs_op1_relation (const irange
&lhs
, const irange
&op1
,
1213 relation_kind rel
) const;
1214 virtual relation_kind
lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1216 relation_kind rel
) const;
1219 // Check to see if the range of OP2 indicates anything about the relation
1220 // between LHS and OP1.
1223 operator_plus::lhs_op1_relation (const irange
&lhs
,
1226 relation_kind
) const
1228 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
1229 return VREL_VARYING
;
1231 tree type
= lhs
.type ();
1232 unsigned prec
= TYPE_PRECISION (type
);
1233 wi::overflow_type ovf1
, ovf2
;
1234 signop sign
= TYPE_SIGN (type
);
1236 // LHS = OP1 + 0 indicates LHS == OP1.
1240 if (TYPE_OVERFLOW_WRAPS (type
))
1242 wi::add (op1
.lower_bound (), op2
.lower_bound (), sign
, &ovf1
);
1243 wi::add (op1
.upper_bound (), op2
.upper_bound (), sign
, &ovf2
);
1246 ovf1
= ovf2
= wi::OVF_NONE
;
1248 // Never wrapping additions.
1251 // Positive op2 means lhs > op1.
1252 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1254 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1257 // Negative op2 means lhs < op1.
1258 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1260 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1263 // Always wrapping additions.
1264 else if (ovf1
&& ovf1
== ovf2
)
1266 // Positive op2 means lhs < op1.
1267 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1269 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1272 // Negative op2 means lhs > op1.
1273 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1275 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1279 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1280 if (!range_includes_zero_p (&op2
))
1283 return VREL_VARYING
;
1286 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1290 operator_plus::lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1291 const irange
&op2
, relation_kind rel
) const
1293 return lhs_op1_relation (lhs
, op2
, op1
, rel
);
1297 operator_plus::wi_fold (irange
&r
, tree type
,
1298 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1299 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1301 wi::overflow_type ov_lb
, ov_ub
;
1302 signop s
= TYPE_SIGN (type
);
1303 wide_int new_lb
= wi::add (lh_lb
, rh_lb
, s
, &ov_lb
);
1304 wide_int new_ub
= wi::add (lh_ub
, rh_ub
, s
, &ov_ub
);
1305 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1309 operator_plus::op1_range (irange
&r
, tree type
,
1312 relation_kind rel ATTRIBUTE_UNUSED
) const
1314 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
1318 operator_plus::op2_range (irange
&r
, tree type
,
1321 relation_kind rel ATTRIBUTE_UNUSED
) const
1323 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, lhs
, op1
);
1327 class operator_minus
: public range_operator
1329 using range_operator::fold_range
;
1330 using range_operator::op1_range
;
1331 using range_operator::op2_range
;
1333 virtual bool op1_range (irange
&r
, tree type
,
1336 relation_kind rel ATTRIBUTE_UNUSED
) const;
1337 virtual bool op2_range (irange
&r
, tree type
,
1340 relation_kind rel ATTRIBUTE_UNUSED
) const;
1341 virtual void wi_fold (irange
&r
, tree type
,
1342 const wide_int
&lh_lb
,
1343 const wide_int
&lh_ub
,
1344 const wide_int
&rh_lb
,
1345 const wide_int
&rh_ub
) const;
1346 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
1349 relation_kind rel
) const;
1350 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1352 const irange
&op1_range
,
1353 const irange
&op2_range
,
1354 relation_kind rel
) const;
1358 operator_minus::wi_fold (irange
&r
, tree type
,
1359 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1360 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1362 wi::overflow_type ov_lb
, ov_ub
;
1363 signop s
= TYPE_SIGN (type
);
1364 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
1365 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
1366 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1370 // Return the relation between LHS and OP1 based on the relation between
1374 operator_minus::lhs_op1_relation (const irange
&, const irange
&op1
,
1375 const irange
&, relation_kind rel
) const
1377 if (!op1
.undefined_p () && TYPE_SIGN (op1
.type ()) == UNSIGNED
)
1387 return VREL_VARYING
;
1390 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1391 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1392 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1395 minus_op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1396 const irange
&op1_range ATTRIBUTE_UNUSED
,
1397 const irange
&op2_range ATTRIBUTE_UNUSED
,
1400 if (rel
== VREL_VARYING
)
1403 int_range
<2> rel_range
;
1404 unsigned prec
= TYPE_PRECISION (type
);
1405 signop sgn
= TYPE_SIGN (type
);
1407 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1409 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
));
1410 else if (rel
== VREL_NE
)
1411 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1413 else if (TYPE_OVERFLOW_WRAPS (type
))
1417 // For wrapping signed values and unsigned, if op1 > op2 or
1418 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1421 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1432 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1434 rel_range
= int_range
<2> (type
, wi::one (prec
),
1435 wi::max_value (prec
, sgn
));
1437 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1439 rel_range
= int_range
<2> (type
, wi::zero (prec
),
1440 wi::max_value (prec
, sgn
));
1442 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1444 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1445 wi::minus_one (prec
));
1447 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1449 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1456 lhs_range
.intersect (rel_range
);
1461 operator_minus::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1462 const irange
&op1_range
,
1463 const irange
&op2_range
,
1464 relation_kind rel
) const
1466 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1471 operator_minus::op1_range (irange
&r
, tree type
,
1474 relation_kind rel ATTRIBUTE_UNUSED
) const
1476 return range_op_handler (PLUS_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
1480 operator_minus::op2_range (irange
&r
, tree type
,
1483 relation_kind rel ATTRIBUTE_UNUSED
) const
1485 return fold_range (r
, type
, op1
, lhs
);
1489 class operator_pointer_diff
: public range_operator
1491 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1493 const irange
&op1_range
,
1494 const irange
&op2_range
,
1495 relation_kind rel
) const;
1499 operator_pointer_diff::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1500 const irange
&op1_range
,
1501 const irange
&op2_range
,
1502 relation_kind rel
) const
1504 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1509 class operator_min
: public range_operator
1512 virtual void wi_fold (irange
&r
, tree type
,
1513 const wide_int
&lh_lb
,
1514 const wide_int
&lh_ub
,
1515 const wide_int
&rh_lb
,
1516 const wide_int
&rh_ub
) const;
1520 operator_min::wi_fold (irange
&r
, tree type
,
1521 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1522 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1524 signop s
= TYPE_SIGN (type
);
1525 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
1526 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
1527 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1531 class operator_max
: public range_operator
1534 virtual void wi_fold (irange
&r
, tree type
,
1535 const wide_int
&lh_lb
,
1536 const wide_int
&lh_ub
,
1537 const wide_int
&rh_lb
,
1538 const wide_int
&rh_ub
) const;
1542 operator_max::wi_fold (irange
&r
, tree type
,
1543 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1544 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1546 signop s
= TYPE_SIGN (type
);
1547 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
1548 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
1549 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1553 class cross_product_operator
: public range_operator
1556 // Perform an operation between two wide-ints and place the result
1557 // in R. Return true if the operation overflowed.
1558 virtual bool wi_op_overflows (wide_int
&r
,
1561 const wide_int
&) const = 0;
1563 // Calculate the cross product of two sets of sub-ranges and return it.
1564 void wi_cross_product (irange
&r
, tree type
,
1565 const wide_int
&lh_lb
,
1566 const wide_int
&lh_ub
,
1567 const wide_int
&rh_lb
,
1568 const wide_int
&rh_ub
) const;
1571 // Calculate the cross product of two sets of ranges and return it.
1573 // Multiplications, divisions and shifts are a bit tricky to handle,
1574 // depending on the mix of signs we have in the two ranges, we need to
1575 // operate on different values to get the minimum and maximum values
1576 // for the new range. One approach is to figure out all the
1577 // variations of range combinations and do the operations.
1579 // However, this involves several calls to compare_values and it is
1580 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1581 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1582 // figure the smallest and largest values to form the new range.
1585 cross_product_operator::wi_cross_product (irange
&r
, tree type
,
1586 const wide_int
&lh_lb
,
1587 const wide_int
&lh_ub
,
1588 const wide_int
&rh_lb
,
1589 const wide_int
&rh_ub
) const
1591 wide_int cp1
, cp2
, cp3
, cp4
;
1592 // Default to varying.
1593 r
.set_varying (type
);
1595 // Compute the 4 cross operations, bailing if we get an overflow we
1597 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
1599 if (wi::eq_p (lh_lb
, lh_ub
))
1601 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
1603 if (wi::eq_p (rh_lb
, rh_ub
))
1605 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
1607 if (wi::eq_p (lh_lb
, lh_ub
))
1609 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
1613 signop sign
= TYPE_SIGN (type
);
1614 if (wi::gt_p (cp1
, cp2
, sign
))
1615 std::swap (cp1
, cp2
);
1616 if (wi::gt_p (cp3
, cp4
, sign
))
1617 std::swap (cp3
, cp4
);
1619 // Choose min and max from the ordered pairs.
1620 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
1621 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
1622 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
1626 class operator_mult
: public cross_product_operator
1628 using range_operator::op1_range
;
1629 using range_operator::op2_range
;
1631 virtual void wi_fold (irange
&r
, tree type
,
1632 const wide_int
&lh_lb
,
1633 const wide_int
&lh_ub
,
1634 const wide_int
&rh_lb
,
1635 const wide_int
&rh_ub
) const;
1636 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1637 const wide_int
&w0
, const wide_int
&w1
) const;
1638 virtual bool op1_range (irange
&r
, tree type
,
1641 relation_kind rel ATTRIBUTE_UNUSED
) const;
1642 virtual bool op2_range (irange
&r
, tree type
,
1645 relation_kind rel ATTRIBUTE_UNUSED
) const;
1649 operator_mult::op1_range (irange
&r
, tree type
,
1650 const irange
&lhs
, const irange
&op2
,
1651 relation_kind rel ATTRIBUTE_UNUSED
) const
1655 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1656 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1657 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1658 if (TYPE_OVERFLOW_WRAPS (type
))
1661 if (op2
.singleton_p (&offset
) && !integer_zerop (offset
))
1662 return range_op_handler (TRUNC_DIV_EXPR
, type
).fold_range (r
, type
,
1668 operator_mult::op2_range (irange
&r
, tree type
,
1669 const irange
&lhs
, const irange
&op1
,
1670 relation_kind rel
) const
1672 return operator_mult::op1_range (r
, type
, lhs
, op1
, rel
);
1676 operator_mult::wi_op_overflows (wide_int
&res
, tree type
,
1677 const wide_int
&w0
, const wide_int
&w1
) const
1679 wi::overflow_type overflow
= wi::OVF_NONE
;
1680 signop sign
= TYPE_SIGN (type
);
1681 res
= wi::mul (w0
, w1
, sign
, &overflow
);
1682 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1684 // For multiplication, the sign of the overflow is given
1685 // by the comparison of the signs of the operands.
1686 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
1687 res
= wi::max_value (w0
.get_precision (), sign
);
1689 res
= wi::min_value (w0
.get_precision (), sign
);
1696 operator_mult::wi_fold (irange
&r
, tree type
,
1697 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1698 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1700 if (TYPE_OVERFLOW_UNDEFINED (type
))
1702 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1706 // Multiply the ranges when overflow wraps. This is basically fancy
1707 // code so we don't drop to varying with an unsigned
1710 // This test requires 2*prec bits if both operands are signed and
1711 // 2*prec + 2 bits if either is not. Therefore, extend the values
1712 // using the sign of the result to PREC2. From here on out,
1713 // everthing is just signed math no matter what the input types
1716 signop sign
= TYPE_SIGN (type
);
1717 unsigned prec
= TYPE_PRECISION (type
);
1718 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
1719 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
1720 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
1721 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
1722 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
1723 widest2_int size
= sizem1
+ 1;
1725 // Canonicalize the intervals.
1726 if (sign
== UNSIGNED
)
1728 if (wi::ltu_p (size
, min0
+ max0
))
1733 if (wi::ltu_p (size
, min1
+ max1
))
1740 // Sort the 4 products so that min is in prod0 and max is in
1742 widest2_int prod0
= min0
* min1
;
1743 widest2_int prod1
= min0
* max1
;
1744 widest2_int prod2
= max0
* min1
;
1745 widest2_int prod3
= max0
* max1
;
1747 // min0min1 > max0max1
1749 std::swap (prod0
, prod3
);
1751 // min0max1 > max0min1
1753 std::swap (prod1
, prod2
);
1756 std::swap (prod0
, prod1
);
1759 std::swap (prod2
, prod3
);
1762 prod2
= prod3
- prod0
;
1763 if (wi::geu_p (prod2
, sizem1
))
1764 // The range covers all values.
1765 r
.set_varying (type
);
1768 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
1769 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
1770 create_possibly_reversed_range (r
, type
, new_lb
, new_ub
);
1775 class operator_div
: public cross_product_operator
1778 operator_div (enum tree_code c
) { code
= c
; }
1779 virtual void wi_fold (irange
&r
, tree type
,
1780 const wide_int
&lh_lb
,
1781 const wide_int
&lh_ub
,
1782 const wide_int
&rh_lb
,
1783 const wide_int
&rh_ub
) const;
1784 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1785 const wide_int
&, const wide_int
&) const;
1787 enum tree_code code
;
1791 operator_div::wi_op_overflows (wide_int
&res
, tree type
,
1792 const wide_int
&w0
, const wide_int
&w1
) const
1797 wi::overflow_type overflow
= wi::OVF_NONE
;
1798 signop sign
= TYPE_SIGN (type
);
1802 case EXACT_DIV_EXPR
:
1803 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1804 // operator_exact_divide. No need to handle it here.
1807 case TRUNC_DIV_EXPR
:
1808 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
1810 case FLOOR_DIV_EXPR
:
1811 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
1813 case ROUND_DIV_EXPR
:
1814 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
1817 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
1823 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1825 // For division, the only case is -INF / -1 = +INF.
1826 res
= wi::max_value (w0
.get_precision (), sign
);
1833 operator_div::wi_fold (irange
&r
, tree type
,
1834 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1835 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1837 const wide_int dividend_min
= lh_lb
;
1838 const wide_int dividend_max
= lh_ub
;
1839 const wide_int divisor_min
= rh_lb
;
1840 const wide_int divisor_max
= rh_ub
;
1841 signop sign
= TYPE_SIGN (type
);
1842 unsigned prec
= TYPE_PRECISION (type
);
1843 wide_int extra_min
, extra_max
;
1845 // If we know we won't divide by zero, just do the division.
1846 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
1848 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1849 divisor_min
, divisor_max
);
1853 // If we're definitely dividing by zero, there's nothing to do.
1854 if (wi_zero_p (type
, divisor_min
, divisor_max
))
1860 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1861 // skip any division by zero.
1863 // First divide by the negative numbers, if any.
1864 if (wi::neg_p (divisor_min
, sign
))
1865 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1866 divisor_min
, wi::minus_one (prec
));
1870 // Then divide by the non-zero positive numbers, if any.
1871 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
1874 wi_cross_product (tmp
, type
, dividend_min
, dividend_max
,
1875 wi::one (prec
), divisor_max
);
1878 // We shouldn't still have undefined here.
1879 gcc_checking_assert (!r
.undefined_p ());
1882 operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
1883 operator_div
op_floor_div (FLOOR_DIV_EXPR
);
1884 operator_div
op_round_div (ROUND_DIV_EXPR
);
1885 operator_div
op_ceil_div (CEIL_DIV_EXPR
);
1888 class operator_exact_divide
: public operator_div
1890 using range_operator::op1_range
;
1892 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR
) { }
1893 virtual bool op1_range (irange
&r
, tree type
,
1896 relation_kind rel ATTRIBUTE_UNUSED
) const;
1901 operator_exact_divide::op1_range (irange
&r
, tree type
,
1904 relation_kind rel ATTRIBUTE_UNUSED
) const
1907 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1908 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1909 // We wont bother trying to enumerate all the in between stuff :-P
1910 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1911 // the time however.
1912 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1913 if (op2
.singleton_p (&offset
)
1914 && !integer_zerop (offset
))
1915 return range_op_handler (MULT_EXPR
, type
).fold_range (r
, type
, lhs
, op2
);
1920 class operator_lshift
: public cross_product_operator
1922 using range_operator::fold_range
;
1923 using range_operator::op1_range
;
1925 virtual bool op1_range (irange
&r
, tree type
,
1928 relation_kind rel
= VREL_VARYING
) const;
1929 virtual bool fold_range (irange
&r
, tree type
,
1932 relation_kind rel
= VREL_VARYING
) const;
1934 virtual void wi_fold (irange
&r
, tree type
,
1935 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1936 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
1937 virtual bool wi_op_overflows (wide_int
&res
,
1940 const wide_int
&) const;
1943 class operator_rshift
: public cross_product_operator
1945 using range_operator::fold_range
;
1946 using range_operator::op1_range
;
1947 using range_operator::lhs_op1_relation
;
1949 virtual bool fold_range (irange
&r
, tree type
,
1952 relation_kind rel
= VREL_VARYING
) const;
1953 virtual void wi_fold (irange
&r
, tree type
,
1954 const wide_int
&lh_lb
,
1955 const wide_int
&lh_ub
,
1956 const wide_int
&rh_lb
,
1957 const wide_int
&rh_ub
) const;
1958 virtual bool wi_op_overflows (wide_int
&res
,
1961 const wide_int
&w1
) const;
1962 virtual bool op1_range (irange
&, tree type
,
1965 relation_kind rel
= VREL_VARYING
) const;
1966 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
1969 relation_kind rel
) const;
1974 operator_rshift::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
1977 relation_kind
) const
1979 // If both operands range are >= 0, then the LHS <= op1.
1980 if (!op1
.undefined_p () && !op2
.undefined_p ()
1981 && wi::ge_p (op1
.lower_bound (), 0, TYPE_SIGN (op1
.type ()))
1982 && wi::ge_p (op2
.lower_bound (), 0, TYPE_SIGN (op2
.type ())))
1984 return VREL_VARYING
;
1988 operator_lshift::fold_range (irange
&r
, tree type
,
1991 relation_kind rel
) const
1993 int_range_max shift_range
;
1994 if (!get_shift_range (shift_range
, type
, op2
))
1996 if (op2
.undefined_p ())
1999 r
.set_varying (type
);
2003 // Transform left shifts by constants into multiplies.
2004 if (shift_range
.singleton_p ())
2006 unsigned shift
= shift_range
.lower_bound ().to_uhwi ();
2007 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
2008 int_range
<1> mult (type
, tmp
, tmp
);
2010 // Force wrapping multiplication.
2011 bool saved_flag_wrapv
= flag_wrapv
;
2012 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
2014 flag_wrapv_pointer
= 1;
2015 bool b
= op_mult
.fold_range (r
, type
, op1
, mult
);
2016 flag_wrapv
= saved_flag_wrapv
;
2017 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
2021 // Otherwise, invoke the generic fold routine.
2022 return range_operator::fold_range (r
, type
, op1
, shift_range
, rel
);
2026 operator_lshift::wi_fold (irange
&r
, tree type
,
2027 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2028 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2030 signop sign
= TYPE_SIGN (type
);
2031 unsigned prec
= TYPE_PRECISION (type
);
2032 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
2033 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
2034 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2035 // overflow. However, for that to happen, rh.max needs to be zero,
2036 // which means rh is a singleton range of zero, which means we simply return
2037 // [lh_lb, lh_ub] as the range.
2038 if (wi::eq_p (rh_ub
, rh_lb
) && wi::eq_p (rh_ub
, 0))
2040 r
= int_range
<2> (type
, lh_lb
, lh_ub
);
2044 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2045 wide_int complement
= ~(bound
- 1);
2046 wide_int low_bound
, high_bound
;
2047 bool in_bounds
= false;
2049 if (sign
== UNSIGNED
)
2052 high_bound
= complement
;
2053 if (wi::ltu_p (lh_ub
, low_bound
))
2055 // [5, 6] << [1, 2] == [10, 24].
2056 // We're shifting out only zeroes, the value increases
2060 else if (wi::ltu_p (high_bound
, lh_lb
))
2062 // [0xffffff00, 0xffffffff] << [1, 2]
2063 // == [0xfffffc00, 0xfffffffe].
2064 // We're shifting out only ones, the value decreases
2071 // [-1, 1] << [1, 2] == [-4, 4]
2072 low_bound
= complement
;
2074 if (wi::lts_p (lh_ub
, high_bound
)
2075 && wi::lts_p (low_bound
, lh_lb
))
2077 // For non-negative numbers, we're shifting out only zeroes,
2078 // the value increases monotonically. For negative numbers,
2079 // we're shifting out only ones, the value decreases
2086 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2088 r
.set_varying (type
);
2092 operator_lshift::wi_op_overflows (wide_int
&res
, tree type
,
2093 const wide_int
&w0
, const wide_int
&w1
) const
2095 signop sign
= TYPE_SIGN (type
);
2098 // It's unclear from the C standard whether shifts can overflow.
2099 // The following code ignores overflow; perhaps a C standard
2100 // interpretation ruling is needed.
2101 res
= wi::rshift (w0
, -w1
, sign
);
2104 res
= wi::lshift (w0
, w1
);
2109 operator_lshift::op1_range (irange
&r
,
2113 relation_kind rel ATTRIBUTE_UNUSED
) const
2117 if (!lhs
.contains_p (build_zero_cst (type
)))
2118 r
.set_nonzero (type
);
2120 r
.set_varying (type
);
2122 if (op2
.singleton_p (&shift_amount
))
2124 wide_int shift
= wi::to_wide (shift_amount
);
2125 if (wi::lt_p (shift
, 0, SIGNED
))
2127 if (wi::ge_p (shift
, wi::uhwi (TYPE_PRECISION (type
),
2128 TYPE_PRECISION (op2
.type ())),
2137 // Work completely in unsigned mode to start.
2139 int_range_max tmp_range
;
2140 if (TYPE_SIGN (type
) == SIGNED
)
2142 int_range_max tmp
= lhs
;
2143 utype
= unsigned_type_for (type
);
2144 range_cast (tmp
, utype
);
2145 op_rshift
.fold_range (tmp_range
, utype
, tmp
, op2
);
2148 op_rshift
.fold_range (tmp_range
, utype
, lhs
, op2
);
2150 // Start with ranges which can produce the LHS by right shifting the
2151 // result by the shift amount.
2152 // ie [0x08, 0xF0] = op1 << 2 will start with
2153 // [00001000, 11110000] = op1 << 2
2154 // [0x02, 0x4C] aka [00000010, 00111100]
2156 // Then create a range from the LB with the least significant upper bit
2157 // set, to the upper bound with all the bits set.
2158 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2160 // Ideally we do this for each subrange, but just lump them all for now.
2161 unsigned low_bits
= TYPE_PRECISION (utype
)
2162 - TREE_INT_CST_LOW (shift_amount
);
2163 wide_int up_mask
= wi::mask (low_bits
, true, TYPE_PRECISION (utype
));
2164 wide_int new_ub
= wi::bit_or (up_mask
, tmp_range
.upper_bound ());
2165 wide_int new_lb
= wi::set_bit (tmp_range
.lower_bound (), low_bits
);
2166 int_range
<2> fill_range (utype
, new_lb
, new_ub
);
2167 tmp_range
.union_ (fill_range
);
2170 range_cast (tmp_range
, type
);
2172 r
.intersect (tmp_range
);
2176 return !r
.varying_p ();
2180 operator_rshift::op1_range (irange
&r
,
2184 relation_kind rel ATTRIBUTE_UNUSED
) const
2187 if (op2
.singleton_p (&shift
))
2189 // Ignore nonsensical shifts.
2190 unsigned prec
= TYPE_PRECISION (type
);
2191 if (wi::ge_p (wi::to_wide (shift
),
2192 wi::uhwi (prec
, TYPE_PRECISION (TREE_TYPE (shift
))),
2195 if (wi::to_wide (shift
) == 0)
2201 // Folding the original operation may discard some impossible
2202 // ranges from the LHS.
2203 int_range_max lhs_refined
;
2204 op_rshift
.fold_range (lhs_refined
, type
, int_range
<1> (type
), op2
);
2205 lhs_refined
.intersect (lhs
);
2206 if (lhs_refined
.undefined_p ())
2211 int_range_max
shift_range (shift
, shift
);
2212 int_range_max lb
, ub
;
2213 op_lshift
.fold_range (lb
, type
, lhs_refined
, shift_range
);
2215 // 0000 0111 = OP1 >> 3
2217 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2218 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2219 // right hand side (0x07).
2220 tree mask
= fold_build1 (BIT_NOT_EXPR
, type
,
2221 fold_build2 (LSHIFT_EXPR
, type
,
2222 build_minus_one_cst (type
),
2224 int_range_max
mask_range (build_zero_cst (type
), mask
);
2225 op_plus
.fold_range (ub
, type
, lb
, mask_range
);
2228 if (!lhs_refined
.contains_p (build_zero_cst (type
)))
2230 mask_range
.invert ();
2231 r
.intersect (mask_range
);
2239 operator_rshift::wi_op_overflows (wide_int
&res
,
2242 const wide_int
&w1
) const
2244 signop sign
= TYPE_SIGN (type
);
2246 res
= wi::lshift (w0
, -w1
);
2249 // It's unclear from the C standard whether shifts can overflow.
2250 // The following code ignores overflow; perhaps a C standard
2251 // interpretation ruling is needed.
2252 res
= wi::rshift (w0
, w1
, sign
);
2258 operator_rshift::fold_range (irange
&r
, tree type
,
2261 relation_kind rel
) const
2263 int_range_max shift
;
2264 if (!get_shift_range (shift
, type
, op2
))
2266 if (op2
.undefined_p ())
2269 r
.set_varying (type
);
2273 return range_operator::fold_range (r
, type
, op1
, shift
, rel
);
2277 operator_rshift::wi_fold (irange
&r
, tree type
,
2278 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2279 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2281 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2285 class operator_cast
: public range_operator
2287 using range_operator::fold_range
;
2288 using range_operator::op1_range
;
2290 virtual bool fold_range (irange
&r
, tree type
,
2293 relation_kind rel
= VREL_VARYING
) const;
2294 virtual bool op1_range (irange
&r
, tree type
,
2297 relation_kind rel
= VREL_VARYING
) const;
2299 bool truncating_cast_p (const irange
&inner
, const irange
&outer
) const;
2300 bool inside_domain_p (const wide_int
&min
, const wide_int
&max
,
2301 const irange
&outer
) const;
2302 void fold_pair (irange
&r
, unsigned index
, const irange
&inner
,
2303 const irange
&outer
) const;
2306 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2309 operator_cast::truncating_cast_p (const irange
&inner
,
2310 const irange
&outer
) const
2312 return TYPE_PRECISION (outer
.type ()) < TYPE_PRECISION (inner
.type ());
2315 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2318 operator_cast::inside_domain_p (const wide_int
&min
,
2319 const wide_int
&max
,
2320 const irange
&range
) const
2322 wide_int domain_min
= wi::to_wide (vrp_val_min (range
.type ()));
2323 wide_int domain_max
= wi::to_wide (vrp_val_max (range
.type ()));
2324 signop domain_sign
= TYPE_SIGN (range
.type ());
2325 return (wi::le_p (min
, domain_max
, domain_sign
)
2326 && wi::le_p (max
, domain_max
, domain_sign
)
2327 && wi::ge_p (min
, domain_min
, domain_sign
)
2328 && wi::ge_p (max
, domain_min
, domain_sign
));
2332 // Helper for fold_range which work on a pair at a time.
2335 operator_cast::fold_pair (irange
&r
, unsigned index
,
2336 const irange
&inner
,
2337 const irange
&outer
) const
2339 tree inner_type
= inner
.type ();
2340 tree outer_type
= outer
.type ();
2341 signop inner_sign
= TYPE_SIGN (inner_type
);
2342 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2344 // check to see if casting from INNER to OUTER is a conversion that
2345 // fits in the resulting OUTER type.
2346 wide_int inner_lb
= inner
.lower_bound (index
);
2347 wide_int inner_ub
= inner
.upper_bound (index
);
2348 if (truncating_cast_p (inner
, outer
))
2350 // We may be able to accomodate a truncating cast if the
2351 // resulting range can be represented in the target type...
2352 if (wi::rshift (wi::sub (inner_ub
, inner_lb
),
2353 wi::uhwi (outer_prec
, TYPE_PRECISION (inner
.type ())),
2356 r
.set_varying (outer_type
);
2360 // ...but we must still verify that the final range fits in the
2361 // domain. This catches -fstrict-enum restrictions where the domain
2362 // range is smaller than what fits in the underlying type.
2363 wide_int min
= wide_int::from (inner_lb
, outer_prec
, inner_sign
);
2364 wide_int max
= wide_int::from (inner_ub
, outer_prec
, inner_sign
);
2365 if (inside_domain_p (min
, max
, outer
))
2366 create_possibly_reversed_range (r
, outer_type
, min
, max
);
2368 r
.set_varying (outer_type
);
2373 operator_cast::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2374 const irange
&inner
,
2375 const irange
&outer
,
2376 relation_kind rel ATTRIBUTE_UNUSED
) const
2378 if (empty_range_varying (r
, type
, inner
, outer
))
2381 gcc_checking_assert (outer
.varying_p ());
2382 gcc_checking_assert (inner
.num_pairs () > 0);
2384 // Avoid a temporary by folding the first pair directly into the result.
2385 fold_pair (r
, 0, inner
, outer
);
2387 // Then process any additonal pairs by unioning with their results.
2388 for (unsigned x
= 1; x
< inner
.num_pairs (); ++x
)
2391 fold_pair (tmp
, x
, inner
, outer
);
2400 operator_cast::op1_range (irange
&r
, tree type
,
2403 relation_kind rel ATTRIBUTE_UNUSED
) const
2405 tree lhs_type
= lhs
.type ();
2406 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
2408 // If we are calculating a pointer, shortcut to what we really care about.
2409 if (POINTER_TYPE_P (type
))
2411 // Conversion from other pointers or a constant (including 0/NULL)
2412 // are straightforward.
2413 if (POINTER_TYPE_P (lhs
.type ())
2414 || (lhs
.singleton_p ()
2415 && TYPE_PRECISION (lhs
.type ()) >= TYPE_PRECISION (type
)))
2418 range_cast (r
, type
);
2422 // If the LHS is not a pointer nor a singleton, then it is
2423 // either VARYING or non-zero.
2424 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
2425 r
.set_nonzero (type
);
2427 r
.set_varying (type
);
2433 if (truncating_cast_p (op2
, lhs
))
2435 if (lhs
.varying_p ())
2436 r
.set_varying (type
);
2439 // We want to insert the LHS as an unsigned value since it
2440 // would not trigger the signed bit of the larger type.
2441 int_range_max converted_lhs
= lhs
;
2442 range_cast (converted_lhs
, unsigned_type_for (lhs_type
));
2443 range_cast (converted_lhs
, type
);
2444 // Start by building the positive signed outer range for the type.
2445 wide_int lim
= wi::set_bit_in_zero (TYPE_PRECISION (lhs_type
),
2446 TYPE_PRECISION (type
));
2447 r
= int_range
<1> (type
, lim
, wi::max_value (TYPE_PRECISION (type
),
2449 // For the signed part, we need to simply union the 2 ranges now.
2450 r
.union_ (converted_lhs
);
2452 // Create maximal negative number outside of LHS bits.
2453 lim
= wi::mask (TYPE_PRECISION (lhs_type
), true,
2454 TYPE_PRECISION (type
));
2455 // Add this to the unsigned LHS range(s).
2456 int_range_max
lim_range (type
, lim
, lim
);
2457 int_range_max lhs_neg
;
2458 range_op_handler (PLUS_EXPR
, type
).fold_range (lhs_neg
, type
,
2461 // lhs_neg now has all the negative versions of the LHS.
2462 // Now union in all the values from SIGNED MIN (0x80000) to
2463 // lim-1 in order to fill in all the ranges with the upper
2466 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2467 // we don't need to create a range from min to lim-1
2468 // calculate neg range traps trying to create [lim, lim - 1].
2469 wide_int min_val
= wi::min_value (TYPE_PRECISION (type
), SIGNED
);
2472 int_range_max
neg (type
,
2473 wi::min_value (TYPE_PRECISION (type
),
2476 lhs_neg
.union_ (neg
);
2478 // And finally, munge the signed and unsigned portions.
2481 // And intersect with any known value passed in the extra operand.
2487 if (TYPE_PRECISION (lhs_type
) == TYPE_PRECISION (type
))
2491 // The cast is not truncating, and the range is restricted to
2492 // the range of the RHS by this assignment.
2494 // Cast the range of the RHS to the type of the LHS.
2495 fold_range (tmp
, lhs_type
, int_range
<1> (type
), int_range
<1> (lhs_type
));
2496 // Intersect this with the LHS range will produce the range,
2497 // which will be cast to the RHS type before returning.
2498 tmp
.intersect (lhs
);
2501 // Cast the calculated range to the type of the RHS.
2502 fold_range (r
, type
, tmp
, int_range
<1> (type
));
2507 class operator_logical_and
: public range_operator
2509 using range_operator::fold_range
;
2510 using range_operator::op1_range
;
2511 using range_operator::op2_range
;
2513 virtual bool fold_range (irange
&r
, tree type
,
2516 relation_kind rel
= VREL_VARYING
) const;
2517 virtual bool op1_range (irange
&r
, tree type
,
2520 relation_kind rel
= VREL_VARYING
) const;
2521 virtual bool op2_range (irange
&r
, tree type
,
2524 relation_kind rel
= VREL_VARYING
) const;
2529 operator_logical_and::fold_range (irange
&r
, tree type
,
2532 relation_kind rel ATTRIBUTE_UNUSED
) const
2534 if (empty_range_varying (r
, type
, lh
, rh
))
2537 // 0 && anything is 0.
2538 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
2539 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
2540 r
= range_false (type
);
2541 else if (lh
.contains_p (build_zero_cst (lh
.type ()))
2542 || rh
.contains_p (build_zero_cst (rh
.type ())))
2543 // To reach this point, there must be a logical 1 on each side, and
2544 // the only remaining question is whether there is a zero or not.
2545 r
= range_true_and_false (type
);
2547 r
= range_true (type
);
2552 operator_logical_and::op1_range (irange
&r
, tree type
,
2554 const irange
&op2 ATTRIBUTE_UNUSED
,
2555 relation_kind rel ATTRIBUTE_UNUSED
) const
2557 switch (get_bool_state (r
, lhs
, type
))
2560 // A true result means both sides of the AND must be true.
2561 r
= range_true (type
);
2564 // Any other result means only one side has to be false, the
2565 // other side can be anything. So we cannot be sure of any
2567 r
= range_true_and_false (type
);
2574 operator_logical_and::op2_range (irange
&r
, tree type
,
2577 relation_kind rel ATTRIBUTE_UNUSED
) const
2579 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
2583 class operator_bitwise_and
: public range_operator
2585 using range_operator::fold_range
;
2586 using range_operator::op1_range
;
2587 using range_operator::op2_range
;
2589 virtual bool fold_range (irange
&r
, tree type
,
2592 relation_kind rel
= VREL_VARYING
) const;
2593 virtual bool op1_range (irange
&r
, tree type
,
2596 relation_kind rel
= VREL_VARYING
) const;
2597 virtual bool op2_range (irange
&r
, tree type
,
2600 relation_kind rel
= VREL_VARYING
) const;
2601 virtual void wi_fold (irange
&r
, tree type
,
2602 const wide_int
&lh_lb
,
2603 const wide_int
&lh_ub
,
2604 const wide_int
&rh_lb
,
2605 const wide_int
&rh_ub
) const;
2607 void simple_op1_range_solver (irange
&r
, tree type
,
2609 const irange
&op2
) const;
2613 operator_bitwise_and::fold_range (irange
&r
, tree type
,
2616 relation_kind rel ATTRIBUTE_UNUSED
) const
2618 if (range_operator::fold_range (r
, type
, lh
, rh
))
2620 if (!lh
.undefined_p () && !rh
.undefined_p ())
2621 r
.set_nonzero_bits (wi::bit_and (lh
.get_nonzero_bits (),
2622 rh
.get_nonzero_bits ()));
2629 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2630 // by considering the number of leading redundant sign bit copies.
2631 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2632 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2634 wi_optimize_signed_bitwise_op (irange
&r
, tree type
,
2635 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2636 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2638 int lh_clrsb
= MIN (wi::clrsb (lh_lb
), wi::clrsb (lh_ub
));
2639 int rh_clrsb
= MIN (wi::clrsb (rh_lb
), wi::clrsb (rh_ub
));
2640 int new_clrsb
= MIN (lh_clrsb
, rh_clrsb
);
2643 int type_prec
= TYPE_PRECISION (type
);
2644 int rprec
= (type_prec
- new_clrsb
) - 1;
2645 value_range_with_overflow (r
, type
,
2646 wi::mask (rprec
, true, type_prec
),
2647 wi::mask (rprec
, false, type_prec
));
2652 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2653 // possible. Basically, see if we can optimize:
2657 // [LB op Z, UB op Z]
2659 // If the optimization was successful, accumulate the range in R and
2663 wi_optimize_and_or (irange
&r
,
2664 enum tree_code code
,
2666 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2667 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2669 // Calculate the singleton mask among the ranges, if any.
2670 wide_int lower_bound
, upper_bound
, mask
;
2671 if (wi::eq_p (rh_lb
, rh_ub
))
2674 lower_bound
= lh_lb
;
2675 upper_bound
= lh_ub
;
2677 else if (wi::eq_p (lh_lb
, lh_ub
))
2680 lower_bound
= rh_lb
;
2681 upper_bound
= rh_ub
;
2686 // If Z is a constant which (for op | its bitwise not) has n
2687 // consecutive least significant bits cleared followed by m 1
2688 // consecutive bits set immediately above it and either
2689 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2691 // The least significant n bits of all the values in the range are
2692 // cleared or set, the m bits above it are preserved and any bits
2693 // above these are required to be the same for all values in the
2697 if (code
== BIT_IOR_EXPR
)
2699 if (wi::eq_p (w
, 0))
2700 n
= w
.get_precision ();
2704 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2705 if (wi::eq_p (w
, 0))
2706 m
= w
.get_precision () - n
;
2708 m
= wi::ctz (w
) - n
;
2710 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2711 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
2714 wide_int res_lb
, res_ub
;
2715 if (code
== BIT_AND_EXPR
)
2717 res_lb
= wi::bit_and (lower_bound
, mask
);
2718 res_ub
= wi::bit_and (upper_bound
, mask
);
2720 else if (code
== BIT_IOR_EXPR
)
2722 res_lb
= wi::bit_or (lower_bound
, mask
);
2723 res_ub
= wi::bit_or (upper_bound
, mask
);
2727 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
2729 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2730 if (code
== BIT_IOR_EXPR
&& wi::ne_p (mask
, 0))
2733 tmp
.set_nonzero (type
);
2739 // For range [LB, UB] compute two wide_int bit masks.
2741 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2742 // for all numbers in the range the bit is 0, otherwise it might be 0
2745 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2746 // for all numbers in the range the bit is 1, otherwise it might be 0
2750 wi_set_zero_nonzero_bits (tree type
,
2751 const wide_int
&lb
, const wide_int
&ub
,
2752 wide_int
&maybe_nonzero
,
2753 wide_int
&mustbe_nonzero
)
2755 signop sign
= TYPE_SIGN (type
);
2757 if (wi::eq_p (lb
, ub
))
2758 maybe_nonzero
= mustbe_nonzero
= lb
;
2759 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
2761 wide_int xor_mask
= lb
^ ub
;
2762 maybe_nonzero
= lb
| ub
;
2763 mustbe_nonzero
= lb
& ub
;
2766 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2767 maybe_nonzero
.get_precision ());
2768 maybe_nonzero
= maybe_nonzero
| mask
;
2769 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
2774 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
2775 mustbe_nonzero
= wi::zero (lb
.get_precision ());
2780 operator_bitwise_and::wi_fold (irange
&r
, tree type
,
2781 const wide_int
&lh_lb
,
2782 const wide_int
&lh_ub
,
2783 const wide_int
&rh_lb
,
2784 const wide_int
&rh_ub
) const
2786 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
2789 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
2790 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
2791 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
2792 maybe_nonzero_lh
, mustbe_nonzero_lh
);
2793 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
2794 maybe_nonzero_rh
, mustbe_nonzero_rh
);
2796 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
2797 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
2798 signop sign
= TYPE_SIGN (type
);
2799 unsigned prec
= TYPE_PRECISION (type
);
2800 // If both input ranges contain only negative values, we can
2801 // truncate the result range maximum to the minimum of the
2802 // input range maxima.
2803 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
2805 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2806 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2808 // If either input range contains only non-negative values
2809 // we can truncate the result range maximum to the respective
2810 // maximum of the input range.
2811 if (wi::ge_p (lh_lb
, 0, sign
))
2812 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2813 if (wi::ge_p (rh_lb
, 0, sign
))
2814 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2815 // PR68217: In case of signed & sign-bit-CST should
2816 // result in [-INF, 0] instead of [-INF, INF].
2817 if (wi::gt_p (new_lb
, new_ub
, sign
))
2819 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
2821 && ((wi::eq_p (lh_lb
, lh_ub
)
2822 && !wi::cmps (lh_lb
, sign_bit
))
2823 || (wi::eq_p (rh_lb
, rh_ub
)
2824 && !wi::cmps (rh_lb
, sign_bit
))))
2826 new_lb
= wi::min_value (prec
, sign
);
2827 new_ub
= wi::zero (prec
);
2830 // If the limits got swapped around, return varying.
2831 if (wi::gt_p (new_lb
, new_ub
,sign
))
2834 && wi_optimize_signed_bitwise_op (r
, type
,
2838 r
.set_varying (type
);
2841 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2845 set_nonzero_range_from_mask (irange
&r
, tree type
, const irange
&lhs
)
2847 if (!lhs
.contains_p (build_zero_cst (type
)))
2848 r
= range_nonzero (type
);
2850 r
.set_varying (type
);
2853 // This was shamelessly stolen from register_edge_assert_for_2 and
2854 // adjusted to work with iranges.
2857 operator_bitwise_and::simple_op1_range_solver (irange
&r
, tree type
,
2859 const irange
&op2
) const
2861 if (!op2
.singleton_p ())
2863 set_nonzero_range_from_mask (r
, type
, lhs
);
2866 unsigned int nprec
= TYPE_PRECISION (type
);
2867 wide_int cst2v
= op2
.lower_bound ();
2868 bool cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (type
));
2871 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
2873 sgnbit
= wi::zero (nprec
);
2875 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2877 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2878 // maximum unsigned value is ~0. For signed comparison, if CST2
2879 // doesn't have the most significant bit set, handle it similarly. If
2880 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2881 wide_int valv
= lhs
.lower_bound ();
2882 wide_int minv
= valv
& cst2v
, maxv
;
2883 bool we_know_nothing
= false;
2886 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2887 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2890 // If we can't determine anything on this bound, fall
2891 // through and conservatively solve for the other end point.
2892 we_know_nothing
= true;
2895 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
2896 if (we_know_nothing
)
2897 r
.set_varying (type
);
2899 r
= int_range
<1> (type
, minv
, maxv
);
2901 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2903 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2904 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2906 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2908 // For signed comparison, if CST2 doesn't have most significant bit
2909 // set, handle it similarly. If CST2 has MSB set, the maximum is
2910 // the same and minimum is INT_MIN.
2911 valv
= lhs
.upper_bound ();
2912 minv
= valv
& cst2v
;
2917 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2920 // If we couldn't determine anything on either bound, return
2922 if (we_know_nothing
)
2930 int_range
<1> upper_bits (type
, minv
, maxv
);
2931 r
.intersect (upper_bits
);
2935 operator_bitwise_and::op1_range (irange
&r
, tree type
,
2938 relation_kind rel ATTRIBUTE_UNUSED
) const
2940 if (types_compatible_p (type
, boolean_type_node
))
2941 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
2944 for (unsigned i
= 0; i
< lhs
.num_pairs (); ++i
)
2946 int_range_max
chunk (lhs
.type (),
2947 lhs
.lower_bound (i
),
2948 lhs
.upper_bound (i
));
2950 simple_op1_range_solver (res
, type
, chunk
, op2
);
2953 if (r
.undefined_p ())
2954 set_nonzero_range_from_mask (r
, type
, lhs
);
2959 operator_bitwise_and::op2_range (irange
&r
, tree type
,
2962 relation_kind rel ATTRIBUTE_UNUSED
) const
2964 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
2968 class operator_logical_or
: public range_operator
2970 using range_operator::fold_range
;
2971 using range_operator::op1_range
;
2972 using range_operator::op2_range
;
2974 virtual bool fold_range (irange
&r
, tree type
,
2977 relation_kind rel
= VREL_VARYING
) const;
2978 virtual bool op1_range (irange
&r
, tree type
,
2981 relation_kind rel
= VREL_VARYING
) const;
2982 virtual bool op2_range (irange
&r
, tree type
,
2985 relation_kind rel
= VREL_VARYING
) const;
2989 operator_logical_or::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2992 relation_kind rel ATTRIBUTE_UNUSED
) const
2994 if (empty_range_varying (r
, type
, lh
, rh
))
3003 operator_logical_or::op1_range (irange
&r
, tree type
,
3005 const irange
&op2 ATTRIBUTE_UNUSED
,
3006 relation_kind rel ATTRIBUTE_UNUSED
) const
3008 switch (get_bool_state (r
, lhs
, type
))
3011 // A false result means both sides of the OR must be false.
3012 r
= range_false (type
);
3015 // Any other result means only one side has to be true, the
3016 // other side can be anything. so we can't be sure of any result
3018 r
= range_true_and_false (type
);
3025 operator_logical_or::op2_range (irange
&r
, tree type
,
3028 relation_kind rel ATTRIBUTE_UNUSED
) const
3030 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
3034 class operator_bitwise_or
: public range_operator
3036 using range_operator::op1_range
;
3037 using range_operator::op2_range
;
3039 virtual bool op1_range (irange
&r
, tree type
,
3042 relation_kind rel
= VREL_VARYING
) const;
3043 virtual bool op2_range (irange
&r
, tree type
,
3046 relation_kind rel
= VREL_VARYING
) const;
3047 virtual void wi_fold (irange
&r
, tree type
,
3048 const wide_int
&lh_lb
,
3049 const wide_int
&lh_ub
,
3050 const wide_int
&rh_lb
,
3051 const wide_int
&rh_ub
) const;
3055 operator_bitwise_or::wi_fold (irange
&r
, tree type
,
3056 const wide_int
&lh_lb
,
3057 const wide_int
&lh_ub
,
3058 const wide_int
&rh_lb
,
3059 const wide_int
&rh_ub
) const
3061 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3064 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3065 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3066 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3067 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3068 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3069 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3070 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
3071 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
3072 signop sign
= TYPE_SIGN (type
);
3073 // If the input ranges contain only positive values we can
3074 // truncate the minimum of the result range to the maximum
3075 // of the input range minima.
3076 if (wi::ge_p (lh_lb
, 0, sign
)
3077 && wi::ge_p (rh_lb
, 0, sign
))
3079 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3080 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3082 // If either input range contains only negative values
3083 // we can truncate the minimum of the result range to the
3084 // respective minimum range.
3085 if (wi::lt_p (lh_ub
, 0, sign
))
3086 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3087 if (wi::lt_p (rh_ub
, 0, sign
))
3088 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3089 // If the limits got swapped around, return a conservative range.
3090 if (wi::gt_p (new_lb
, new_ub
, sign
))
3092 // Make sure that nonzero|X is nonzero.
3093 if (wi::gt_p (lh_lb
, 0, sign
)
3094 || wi::gt_p (rh_lb
, 0, sign
)
3095 || wi::lt_p (lh_ub
, 0, sign
)
3096 || wi::lt_p (rh_ub
, 0, sign
))
3097 r
.set_nonzero (type
);
3098 else if (sign
== SIGNED
3099 && wi_optimize_signed_bitwise_op (r
, type
,
3104 r
.set_varying (type
);
3107 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3111 operator_bitwise_or::op1_range (irange
&r
, tree type
,
3114 relation_kind rel ATTRIBUTE_UNUSED
) const
3116 // If this is really a logical wi_fold, call that.
3117 if (types_compatible_p (type
, boolean_type_node
))
3118 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
3122 tree zero
= build_zero_cst (type
);
3123 r
= int_range
<1> (zero
, zero
);
3126 r
.set_varying (type
);
3131 operator_bitwise_or::op2_range (irange
&r
, tree type
,
3134 relation_kind rel ATTRIBUTE_UNUSED
) const
3136 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
3140 class operator_bitwise_xor
: public range_operator
3142 using range_operator::op1_range
;
3143 using range_operator::op2_range
;
3145 virtual void wi_fold (irange
&r
, tree type
,
3146 const wide_int
&lh_lb
,
3147 const wide_int
&lh_ub
,
3148 const wide_int
&rh_lb
,
3149 const wide_int
&rh_ub
) const;
3150 virtual bool op1_range (irange
&r
, tree type
,
3153 relation_kind rel
= VREL_VARYING
) const;
3154 virtual bool op2_range (irange
&r
, tree type
,
3157 relation_kind rel
= VREL_VARYING
) const;
3158 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
3160 const irange
&op1_range
,
3161 const irange
&op2_range
,
3162 relation_kind rel
) const;
3166 operator_bitwise_xor::wi_fold (irange
&r
, tree type
,
3167 const wide_int
&lh_lb
,
3168 const wide_int
&lh_ub
,
3169 const wide_int
&rh_lb
,
3170 const wide_int
&rh_ub
) const
3172 signop sign
= TYPE_SIGN (type
);
3173 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3174 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3175 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3176 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3177 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3178 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3180 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
3181 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
3182 wide_int result_one_bits
3183 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
3184 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
3185 wide_int new_ub
= ~result_zero_bits
;
3186 wide_int new_lb
= result_one_bits
;
3188 // If the range has all positive or all negative values, the result
3189 // is better than VARYING.
3190 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
3191 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3192 else if (sign
== SIGNED
3193 && wi_optimize_signed_bitwise_op (r
, type
,
3198 r
.set_varying (type
);
3200 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3201 if (wi::lt_p (lh_ub
, rh_lb
, sign
)
3202 || wi::lt_p (rh_ub
, lh_lb
, sign
)
3203 || wi::ne_p (result_one_bits
, 0))
3206 tmp
.set_nonzero (type
);
3212 operator_bitwise_xor::op1_op2_relation_effect (irange
&lhs_range
,
3216 relation_kind rel
) const
3218 if (rel
== VREL_VARYING
)
3221 int_range
<2> rel_range
;
3226 rel_range
.set_zero (type
);
3229 rel_range
.set_nonzero (type
);
3235 lhs_range
.intersect (rel_range
);
3240 operator_bitwise_xor::op1_range (irange
&r
, tree type
,
3243 relation_kind rel ATTRIBUTE_UNUSED
) const
3245 if (lhs
.undefined_p () || lhs
.varying_p ())
3250 if (types_compatible_p (type
, boolean_type_node
))
3252 switch (get_bool_state (r
, lhs
, type
))
3255 if (op2
.varying_p ())
3256 r
.set_varying (type
);
3257 else if (op2
.zero_p ())
3258 r
= range_true (type
);
3260 r
= range_false (type
);
3270 r
.set_varying (type
);
3275 operator_bitwise_xor::op2_range (irange
&r
, tree type
,
3278 relation_kind rel ATTRIBUTE_UNUSED
) const
3280 return operator_bitwise_xor::op1_range (r
, type
, lhs
, op1
);
3283 class operator_trunc_mod
: public range_operator
3285 using range_operator::op1_range
;
3286 using range_operator::op2_range
;
3288 virtual void wi_fold (irange
&r
, tree type
,
3289 const wide_int
&lh_lb
,
3290 const wide_int
&lh_ub
,
3291 const wide_int
&rh_lb
,
3292 const wide_int
&rh_ub
) const;
3293 virtual bool op1_range (irange
&r
, tree type
,
3296 relation_kind rel ATTRIBUTE_UNUSED
) const;
3297 virtual bool op2_range (irange
&r
, tree type
,
3300 relation_kind rel ATTRIBUTE_UNUSED
) const;
3304 operator_trunc_mod::wi_fold (irange
&r
, tree type
,
3305 const wide_int
&lh_lb
,
3306 const wide_int
&lh_ub
,
3307 const wide_int
&rh_lb
,
3308 const wide_int
&rh_ub
) const
3310 wide_int new_lb
, new_ub
, tmp
;
3311 signop sign
= TYPE_SIGN (type
);
3312 unsigned prec
= TYPE_PRECISION (type
);
3314 // Mod 0 is undefined.
3315 if (wi_zero_p (type
, rh_lb
, rh_ub
))
3321 // Check for constant and try to fold.
3322 if (lh_lb
== lh_ub
&& rh_lb
== rh_ub
)
3324 wi::overflow_type ov
= wi::OVF_NONE
;
3325 tmp
= wi::mod_trunc (lh_lb
, rh_lb
, sign
, &ov
);
3326 if (ov
== wi::OVF_NONE
)
3328 r
= int_range
<2> (type
, tmp
, tmp
);
3333 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3338 new_ub
= wi::smax (new_ub
, tmp
);
3341 if (sign
== UNSIGNED
)
3342 new_lb
= wi::zero (prec
);
3347 if (wi::gts_p (tmp
, 0))
3348 tmp
= wi::zero (prec
);
3349 new_lb
= wi::smax (new_lb
, tmp
);
3352 if (sign
== SIGNED
&& wi::neg_p (tmp
))
3353 tmp
= wi::zero (prec
);
3354 new_ub
= wi::min (new_ub
, tmp
, sign
);
3356 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3360 operator_trunc_mod::op1_range (irange
&r
, tree type
,
3363 relation_kind rel ATTRIBUTE_UNUSED
) const
3366 signop sign
= TYPE_SIGN (type
);
3367 unsigned prec
= TYPE_PRECISION (type
);
3368 // (a % b) >= x && x > 0 , then a >= x.
3369 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3371 r
= value_range (type
, lhs
.lower_bound (), wi::max_value (prec
, sign
));
3374 // (a % b) <= x && x < 0 , then a <= x.
3375 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3377 r
= value_range (type
, wi::min_value (prec
, sign
), lhs
.upper_bound ());
3384 operator_trunc_mod::op2_range (irange
&r
, tree type
,
3387 relation_kind rel ATTRIBUTE_UNUSED
) const
3390 signop sign
= TYPE_SIGN (type
);
3391 unsigned prec
= TYPE_PRECISION (type
);
3392 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3393 // or b > x for unsigned.
3394 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3397 r
= value_range (type
, wi::neg (lhs
.lower_bound ()),
3398 lhs
.lower_bound (), VR_ANTI_RANGE
);
3399 else if (wi::lt_p (lhs
.lower_bound (), wi::max_value (prec
, sign
),
3401 r
= value_range (type
, lhs
.lower_bound () + 1,
3402 wi::max_value (prec
, sign
));
3407 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3408 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3410 if (wi::gt_p (lhs
.upper_bound (), wi::min_value (prec
, sign
), sign
))
3411 r
= value_range (type
, lhs
.upper_bound (),
3412 wi::neg (lhs
.upper_bound ()), VR_ANTI_RANGE
);
3421 class operator_logical_not
: public range_operator
3423 using range_operator::fold_range
;
3424 using range_operator::op1_range
;
3426 virtual bool fold_range (irange
&r
, tree type
,
3429 relation_kind rel
= VREL_VARYING
) const;
3430 virtual bool op1_range (irange
&r
, tree type
,
3433 relation_kind rel
= VREL_VARYING
) const;
3436 // Folding a logical NOT, oddly enough, involves doing nothing on the
3437 // forward pass through. During the initial walk backwards, the
3438 // logical NOT reversed the desired outcome on the way back, so on the
3439 // way forward all we do is pass the range forward.
3444 // to determine the TRUE branch, walking backward
3445 // if (b_3) if ([1,1])
3446 // b_3 = !b_2 [1,1] = ![0,0]
3447 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3448 // which is the result we are looking for.. so.. pass it through.
3451 operator_logical_not::fold_range (irange
&r
, tree type
,
3453 const irange
&rh ATTRIBUTE_UNUSED
,
3454 relation_kind rel ATTRIBUTE_UNUSED
) const
3456 if (empty_range_varying (r
, type
, lh
, rh
))
3460 if (!lh
.varying_p () && !lh
.undefined_p ())
3467 operator_logical_not::op1_range (irange
&r
,
3471 relation_kind rel ATTRIBUTE_UNUSED
) const
3473 // Logical NOT is involutary...do it again.
3474 return fold_range (r
, type
, lhs
, op2
);
3478 class operator_bitwise_not
: public range_operator
3480 using range_operator::fold_range
;
3481 using range_operator::op1_range
;
3483 virtual bool fold_range (irange
&r
, tree type
,
3486 relation_kind rel
= VREL_VARYING
) const;
3487 virtual bool op1_range (irange
&r
, tree type
,
3490 relation_kind rel
= VREL_VARYING
) const;
3494 operator_bitwise_not::fold_range (irange
&r
, tree type
,
3497 relation_kind rel ATTRIBUTE_UNUSED
) const
3499 if (empty_range_varying (r
, type
, lh
, rh
))
3502 if (types_compatible_p (type
, boolean_type_node
))
3503 return op_logical_not
.fold_range (r
, type
, lh
, rh
);
3505 // ~X is simply -1 - X.
3506 int_range
<1> minusone (type
, wi::minus_one (TYPE_PRECISION (type
)),
3507 wi::minus_one (TYPE_PRECISION (type
)));
3508 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
, minusone
, lh
);
3512 operator_bitwise_not::op1_range (irange
&r
, tree type
,
3515 relation_kind rel ATTRIBUTE_UNUSED
) const
3517 if (types_compatible_p (type
, boolean_type_node
))
3518 return op_logical_not
.op1_range (r
, type
, lhs
, op2
);
3520 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3521 return fold_range (r
, type
, lhs
, op2
);
3525 class operator_cst
: public range_operator
3527 using range_operator::fold_range
;
3529 virtual bool fold_range (irange
&r
, tree type
,
3532 relation_kind rel
= VREL_VARYING
) const;
3536 operator_cst::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3538 const irange
&rh ATTRIBUTE_UNUSED
,
3539 relation_kind rel ATTRIBUTE_UNUSED
) const
3546 class operator_identity
: public range_operator
3548 using range_operator::fold_range
;
3549 using range_operator::op1_range
;
3550 using range_operator::lhs_op1_relation
;
3552 virtual bool fold_range (irange
&r
, tree type
,
3555 relation_kind rel
= VREL_VARYING
) const;
3556 virtual bool op1_range (irange
&r
, tree type
,
3559 relation_kind rel
= VREL_VARYING
) const;
3560 virtual relation_kind
lhs_op1_relation (const irange
&lhs
,
3563 relation_kind rel
) const;
3566 // Determine if there is a relationship between LHS and OP1.
3569 operator_identity::lhs_op1_relation (const irange
&lhs
,
3570 const irange
&op1 ATTRIBUTE_UNUSED
,
3571 const irange
&op2 ATTRIBUTE_UNUSED
,
3572 relation_kind
) const
3574 if (lhs
.undefined_p ())
3575 return VREL_VARYING
;
3576 // Simply a copy, so they are equivalent.
3581 operator_identity::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3583 const irange
&rh ATTRIBUTE_UNUSED
,
3584 relation_kind rel ATTRIBUTE_UNUSED
) const
3591 operator_identity::op1_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3593 const irange
&op2 ATTRIBUTE_UNUSED
,
3594 relation_kind rel ATTRIBUTE_UNUSED
) const
3601 class operator_unknown
: public range_operator
3603 using range_operator::fold_range
;
3605 virtual bool fold_range (irange
&r
, tree type
,
3608 relation_kind rel
= VREL_VARYING
) const;
3612 operator_unknown::fold_range (irange
&r
, tree type
,
3613 const irange
&lh ATTRIBUTE_UNUSED
,
3614 const irange
&rh ATTRIBUTE_UNUSED
,
3615 relation_kind rel ATTRIBUTE_UNUSED
) const
3617 r
.set_varying (type
);
3622 class operator_abs
: public range_operator
3624 using range_operator::op1_range
;
3626 virtual void wi_fold (irange
&r
, tree type
,
3627 const wide_int
&lh_lb
,
3628 const wide_int
&lh_ub
,
3629 const wide_int
&rh_lb
,
3630 const wide_int
&rh_ub
) const;
3631 virtual bool op1_range (irange
&r
, tree type
,
3634 relation_kind rel ATTRIBUTE_UNUSED
) const;
3638 operator_abs::wi_fold (irange
&r
, tree type
,
3639 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3640 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3641 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3644 signop sign
= TYPE_SIGN (type
);
3645 unsigned prec
= TYPE_PRECISION (type
);
3647 // Pass through LH for the easy cases.
3648 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
3650 r
= int_range
<1> (type
, lh_lb
, lh_ub
);
3654 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3656 wide_int min_value
= wi::min_value (prec
, sign
);
3657 wide_int max_value
= wi::max_value (prec
, sign
);
3658 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
3660 r
.set_varying (type
);
3664 // ABS_EXPR may flip the range around, if the original range
3665 // included negative values.
3666 if (wi::eq_p (lh_lb
, min_value
))
3668 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3669 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3670 if (wi::eq_p (lh_ub
, min_value
))
3672 r
= int_range
<1> (type
, min_value
, min_value
);
3678 min
= wi::abs (lh_lb
);
3680 if (wi::eq_p (lh_ub
, min_value
))
3683 max
= wi::abs (lh_ub
);
3685 // If the range contains zero then we know that the minimum value in the
3686 // range will be zero.
3687 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
3689 if (wi::gt_p (min
, max
, sign
))
3691 min
= wi::zero (prec
);
3695 // If the range was reversed, swap MIN and MAX.
3696 if (wi::gt_p (min
, max
, sign
))
3697 std::swap (min
, max
);
3700 // If the new range has its limits swapped around (MIN > MAX), then
3701 // the operation caused one of them to wrap around. The only thing
3702 // we know is that the result is positive.
3703 if (wi::gt_p (min
, max
, sign
))
3705 min
= wi::zero (prec
);
3708 r
= int_range
<1> (type
, min
, max
);
3712 operator_abs::op1_range (irange
&r
, tree type
,
3715 relation_kind rel ATTRIBUTE_UNUSED
) const
3717 if (empty_range_varying (r
, type
, lhs
, op2
))
3719 if (TYPE_UNSIGNED (type
))
3724 // Start with the positives because negatives are an impossible result.
3725 int_range_max positives
= range_positives (type
);
3726 positives
.intersect (lhs
);
3728 // Then add the negative of each pair:
3729 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3730 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
3731 r
.union_ (int_range
<1> (type
,
3732 -positives
.upper_bound (i
),
3733 -positives
.lower_bound (i
)));
3734 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3735 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3736 wide_int min_value
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
3737 wide_int lb
= lhs
.lower_bound ();
3738 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lb
, min_value
))
3739 r
.union_ (int_range
<2> (type
, lb
, lb
));
3744 class operator_absu
: public range_operator
3747 virtual void wi_fold (irange
&r
, tree type
,
3748 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3749 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3753 operator_absu::wi_fold (irange
&r
, tree type
,
3754 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3755 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3756 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3758 wide_int new_lb
, new_ub
;
3760 // Pass through VR0 the easy cases.
3761 if (wi::ges_p (lh_lb
, 0))
3768 new_lb
= wi::abs (lh_lb
);
3769 new_ub
= wi::abs (lh_ub
);
3771 // If the range contains zero then we know that the minimum
3772 // value in the range will be zero.
3773 if (wi::ges_p (lh_ub
, 0))
3775 if (wi::gtu_p (new_lb
, new_ub
))
3777 new_lb
= wi::zero (TYPE_PRECISION (type
));
3780 std::swap (new_lb
, new_ub
);
3783 gcc_checking_assert (TYPE_UNSIGNED (type
));
3784 r
= int_range
<1> (type
, new_lb
, new_ub
);
3788 class operator_negate
: public range_operator
3790 using range_operator::fold_range
;
3791 using range_operator::op1_range
;
3793 virtual bool fold_range (irange
&r
, tree type
,
3796 relation_kind rel
= VREL_VARYING
) const;
3797 virtual bool op1_range (irange
&r
, tree type
,
3800 relation_kind rel
= VREL_VARYING
) const;
3804 operator_negate::fold_range (irange
&r
, tree type
,
3807 relation_kind rel ATTRIBUTE_UNUSED
) const
3809 if (empty_range_varying (r
, type
, lh
, rh
))
3811 // -X is simply 0 - X.
3812 return range_op_handler (MINUS_EXPR
, type
).fold_range (r
, type
,
3813 range_zero (type
), lh
);
3817 operator_negate::op1_range (irange
&r
, tree type
,
3820 relation_kind rel ATTRIBUTE_UNUSED
) const
3822 // NEGATE is involutory.
3823 return fold_range (r
, type
, lhs
, op2
);
3827 class operator_addr_expr
: public range_operator
3829 using range_operator::fold_range
;
3830 using range_operator::op1_range
;
3832 virtual bool fold_range (irange
&r
, tree type
,
3835 relation_kind rel
= VREL_VARYING
) const;
3836 virtual bool op1_range (irange
&r
, tree type
,
3839 relation_kind rel
= VREL_VARYING
) const;
3843 operator_addr_expr::fold_range (irange
&r
, tree type
,
3846 relation_kind rel ATTRIBUTE_UNUSED
) const
3848 if (empty_range_varying (r
, type
, lh
, rh
))
3851 // Return a non-null pointer of the LHS type (passed in op2).
3853 r
= range_zero (type
);
3854 else if (!lh
.contains_p (build_zero_cst (lh
.type ())))
3855 r
= range_nonzero (type
);
3857 r
.set_varying (type
);
3862 operator_addr_expr::op1_range (irange
&r
, tree type
,
3865 relation_kind rel ATTRIBUTE_UNUSED
) const
3867 return operator_addr_expr::fold_range (r
, type
, lhs
, op2
);
3871 class pointer_plus_operator
: public range_operator
3874 virtual void wi_fold (irange
&r
, tree type
,
3875 const wide_int
&lh_lb
,
3876 const wide_int
&lh_ub
,
3877 const wide_int
&rh_lb
,
3878 const wide_int
&rh_ub
) const;
3882 pointer_plus_operator::wi_fold (irange
&r
, tree type
,
3883 const wide_int
&lh_lb
,
3884 const wide_int
&lh_ub
,
3885 const wide_int
&rh_lb
,
3886 const wide_int
&rh_ub
) const
3888 // Check for [0,0] + const, and simply return the const.
3889 if (lh_lb
== 0 && lh_ub
== 0 && rh_lb
== rh_ub
)
3891 tree val
= wide_int_to_tree (type
, rh_lb
);
3896 // For pointer types, we are really only interested in asserting
3897 // whether the expression evaluates to non-NULL.
3899 // With -fno-delete-null-pointer-checks we need to be more
3900 // conservative. As some object might reside at address 0,
3901 // then some offset could be added to it and the same offset
3902 // subtracted again and the result would be NULL.
3904 // static int a[12]; where &a[0] is NULL and
3907 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3908 // where the first range doesn't include zero and the second one
3909 // doesn't either. As the second operand is sizetype (unsigned),
3910 // consider all ranges where the MSB could be set as possible
3911 // subtractions where the result might be NULL.
3912 if ((!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3913 || !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3914 && !TYPE_OVERFLOW_WRAPS (type
)
3915 && (flag_delete_null_pointer_checks
3916 || !wi::sign_mask (rh_ub
)))
3917 r
= range_nonzero (type
);
3918 else if (lh_lb
== lh_ub
&& lh_lb
== 0
3919 && rh_lb
== rh_ub
&& rh_lb
== 0)
3920 r
= range_zero (type
);
3922 r
.set_varying (type
);
3926 class pointer_min_max_operator
: public range_operator
3929 virtual void wi_fold (irange
& r
, tree type
,
3930 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3931 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3935 pointer_min_max_operator::wi_fold (irange
&r
, tree type
,
3936 const wide_int
&lh_lb
,
3937 const wide_int
&lh_ub
,
3938 const wide_int
&rh_lb
,
3939 const wide_int
&rh_ub
) const
3941 // For MIN/MAX expressions with pointers, we only care about
3942 // nullness. If both are non null, then the result is nonnull.
3943 // If both are null, then the result is null. Otherwise they
3945 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3946 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3947 r
= range_nonzero (type
);
3948 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
3949 r
= range_zero (type
);
3951 r
.set_varying (type
);
3955 class pointer_and_operator
: public range_operator
3958 virtual void wi_fold (irange
&r
, tree type
,
3959 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3960 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3964 pointer_and_operator::wi_fold (irange
&r
, tree type
,
3965 const wide_int
&lh_lb
,
3966 const wide_int
&lh_ub
,
3967 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3968 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3970 // For pointer types, we are really only interested in asserting
3971 // whether the expression evaluates to non-NULL.
3972 if (wi_zero_p (type
, lh_lb
, lh_ub
) || wi_zero_p (type
, lh_lb
, lh_ub
))
3973 r
= range_zero (type
);
3975 r
.set_varying (type
);
3979 class pointer_or_operator
: public range_operator
3981 using range_operator::op1_range
;
3982 using range_operator::op2_range
;
3984 virtual bool op1_range (irange
&r
, tree type
,
3987 relation_kind rel
= VREL_VARYING
) const;
3988 virtual bool op2_range (irange
&r
, tree type
,
3991 relation_kind rel
= VREL_VARYING
) const;
3992 virtual void wi_fold (irange
&r
, tree type
,
3993 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3994 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3998 pointer_or_operator::op1_range (irange
&r
, tree type
,
4000 const irange
&op2 ATTRIBUTE_UNUSED
,
4001 relation_kind rel ATTRIBUTE_UNUSED
) const
4005 tree zero
= build_zero_cst (type
);
4006 r
= int_range
<1> (zero
, zero
);
4009 r
.set_varying (type
);
4014 pointer_or_operator::op2_range (irange
&r
, tree type
,
4017 relation_kind rel ATTRIBUTE_UNUSED
) const
4019 return pointer_or_operator::op1_range (r
, type
, lhs
, op1
);
4023 pointer_or_operator::wi_fold (irange
&r
, tree type
,
4024 const wide_int
&lh_lb
,
4025 const wide_int
&lh_ub
,
4026 const wide_int
&rh_lb
,
4027 const wide_int
&rh_ub
) const
4029 // For pointer types, we are really only interested in asserting
4030 // whether the expression evaluates to non-NULL.
4031 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
4032 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
4033 r
= range_nonzero (type
);
4034 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
4035 r
= range_zero (type
);
4037 r
.set_varying (type
);
4040 // Return a pointer to the range_operator instance, if there is one
4041 // associated with tree_code CODE.
4044 range_op_table::operator[] (enum tree_code code
)
4046 gcc_checking_assert (code
> 0 && code
< MAX_TREE_CODES
);
4047 return m_range_tree
[code
];
4050 // Add OP to the handler table for CODE.
4053 range_op_table::set (enum tree_code code
, range_operator
&op
)
4055 gcc_checking_assert (m_range_tree
[code
] == NULL
);
4056 m_range_tree
[code
] = &op
;
4059 // Instantiate a range op table for integral operations.
4061 class integral_table
: public range_op_table
4065 } integral_tree_table
;
4067 integral_table::integral_table ()
4069 set (EQ_EXPR
, op_equal
);
4070 set (NE_EXPR
, op_not_equal
);
4071 set (LT_EXPR
, op_lt
);
4072 set (LE_EXPR
, op_le
);
4073 set (GT_EXPR
, op_gt
);
4074 set (GE_EXPR
, op_ge
);
4075 set (PLUS_EXPR
, op_plus
);
4076 set (MINUS_EXPR
, op_minus
);
4077 set (MIN_EXPR
, op_min
);
4078 set (MAX_EXPR
, op_max
);
4079 set (MULT_EXPR
, op_mult
);
4080 set (TRUNC_DIV_EXPR
, op_trunc_div
);
4081 set (FLOOR_DIV_EXPR
, op_floor_div
);
4082 set (ROUND_DIV_EXPR
, op_round_div
);
4083 set (CEIL_DIV_EXPR
, op_ceil_div
);
4084 set (EXACT_DIV_EXPR
, op_exact_div
);
4085 set (LSHIFT_EXPR
, op_lshift
);
4086 set (RSHIFT_EXPR
, op_rshift
);
4087 set (NOP_EXPR
, op_convert
);
4088 set (CONVERT_EXPR
, op_convert
);
4089 set (TRUTH_AND_EXPR
, op_logical_and
);
4090 set (BIT_AND_EXPR
, op_bitwise_and
);
4091 set (TRUTH_OR_EXPR
, op_logical_or
);
4092 set (BIT_IOR_EXPR
, op_bitwise_or
);
4093 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4094 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
4095 set (TRUTH_NOT_EXPR
, op_logical_not
);
4096 set (BIT_NOT_EXPR
, op_bitwise_not
);
4097 set (INTEGER_CST
, op_integer_cst
);
4098 set (SSA_NAME
, op_identity
);
4099 set (PAREN_EXPR
, op_identity
);
4100 set (OBJ_TYPE_REF
, op_identity
);
4101 set (IMAGPART_EXPR
, op_unknown
);
4102 set (REALPART_EXPR
, op_unknown
);
4103 set (POINTER_DIFF_EXPR
, op_pointer_diff
);
4104 set (ABS_EXPR
, op_abs
);
4105 set (ABSU_EXPR
, op_absu
);
4106 set (NEGATE_EXPR
, op_negate
);
4107 set (ADDR_EXPR
, op_addr
);
4110 // Instantiate a range op table for pointer operations.
4112 class pointer_table
: public range_op_table
4116 } pointer_tree_table
;
4118 pointer_table::pointer_table ()
4120 set (BIT_AND_EXPR
, op_pointer_and
);
4121 set (BIT_IOR_EXPR
, op_pointer_or
);
4122 set (MIN_EXPR
, op_ptr_min_max
);
4123 set (MAX_EXPR
, op_ptr_min_max
);
4124 set (POINTER_PLUS_EXPR
, op_pointer_plus
);
4126 set (EQ_EXPR
, op_equal
);
4127 set (NE_EXPR
, op_not_equal
);
4128 set (LT_EXPR
, op_lt
);
4129 set (LE_EXPR
, op_le
);
4130 set (GT_EXPR
, op_gt
);
4131 set (GE_EXPR
, op_ge
);
4132 set (SSA_NAME
, op_identity
);
4133 set (INTEGER_CST
, op_integer_cst
);
4134 set (ADDR_EXPR
, op_addr
);
4135 set (NOP_EXPR
, op_convert
);
4136 set (CONVERT_EXPR
, op_convert
);
4138 set (BIT_NOT_EXPR
, op_bitwise_not
);
4139 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4142 // The tables are hidden and accessed via a simple extern function.
4144 static inline range_operator
*
4145 get_handler (enum tree_code code
, tree type
)
4147 // First check if there is a pointer specialization.
4148 if (POINTER_TYPE_P (type
))
4149 return pointer_tree_table
[code
];
4150 if (INTEGRAL_TYPE_P (type
))
4151 return integral_tree_table
[code
];
4155 range_op_handler::range_op_handler (tree_code code
, tree type
)
4157 m_op
= get_handler (code
, type
);
4160 range_op_handler::range_op_handler (const gimple
*s
)
4162 if (const gassign
*ass
= dyn_cast
<const gassign
*> (s
))
4164 enum tree_code code
= gimple_assign_rhs_code (ass
);
4165 // The LHS of a comparison is always an int, so we must look at
4167 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4168 m_op
= get_handler (code
, TREE_TYPE (gimple_assign_rhs1 (ass
)));
4170 m_op
= get_handler (code
, TREE_TYPE (gimple_assign_lhs (ass
)));
4172 else if (const gcond
*cond
= dyn_cast
<const gcond
*> (s
))
4173 m_op
= get_handler (gimple_cond_code (cond
),
4174 TREE_TYPE (gimple_cond_lhs (cond
)));
4180 range_op_handler::fold_range (vrange
&r
, tree type
,
4183 relation_kind rel
) const
4185 if (is_a
<irange
> (lh
))
4186 return m_op
->fold_range (as_a
<irange
> (r
), type
,
4188 as_a
<irange
> (rh
), rel
);
4194 range_op_handler::op1_range (vrange
&r
, tree type
,
4197 relation_kind rel
) const
4199 if (is_a
<irange
> (r
))
4200 return m_op
->op1_range (as_a
<irange
> (r
), type
,
4201 as_a
<irange
> (lhs
),
4202 as_a
<irange
> (op2
), rel
);
4208 range_op_handler::op2_range (vrange
&r
, tree type
,
4211 relation_kind rel
) const
4213 if (is_a
<irange
> (r
))
4214 return m_op
->op2_range (as_a
<irange
> (r
), type
,
4215 as_a
<irange
> (lhs
),
4216 as_a
<irange
> (op1
), rel
);
4222 range_op_handler::lhs_op1_relation (const vrange
&lhs
,
4225 relation_kind rel
) const
4227 if (is_a
<irange
> (op1
))
4228 return m_op
->lhs_op1_relation (as_a
<irange
> (lhs
),
4229 as_a
<irange
> (op1
), as_a
<irange
> (op2
), rel
);
4231 return VREL_VARYING
;
4235 range_op_handler::lhs_op2_relation (const vrange
&lhs
,
4238 relation_kind rel
) const
4240 if (is_a
<irange
> (op1
))
4241 return m_op
->lhs_op2_relation (as_a
<irange
> (lhs
),
4242 as_a
<irange
> (op1
), as_a
<irange
> (op2
), rel
);
4244 return VREL_VARYING
;
4248 range_op_handler::op1_op2_relation (const vrange
&lhs
) const
4250 return m_op
->op1_op2_relation (as_a
<irange
> (lhs
));
4253 // Cast the range in R to TYPE.
4256 range_cast (vrange
&r
, tree type
)
4258 Value_Range
tmp (r
);
4259 Value_Range
varying (type
);
4260 varying
.set_varying (type
);
4261 range_op_handler
op (CONVERT_EXPR
, type
);
4262 // Call op_convert, if it fails, the result is varying.
4263 if (!op
|| !op
.fold_range (r
, type
, tmp
, varying
))
4265 r
.set_varying (type
);
4272 #include "selftest.h"
4276 #define INT(N) build_int_cst (integer_type_node, (N))
4277 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4278 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4279 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4280 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4281 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4284 range_op_cast_tests ()
4286 int_range
<1> r0
, r1
, r2
, rold
;
4287 r0
.set_varying (integer_type_node
);
4288 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
4290 // If a range is in any way outside of the range for the converted
4291 // to range, default to the range for the new type.
4292 r0
.set_varying (short_integer_type_node
);
4293 tree minshort
= wide_int_to_tree (short_integer_type_node
, r0
.lower_bound ());
4294 tree maxshort
= wide_int_to_tree (short_integer_type_node
, r0
.upper_bound ());
4295 if (TYPE_PRECISION (TREE_TYPE (maxint
))
4296 > TYPE_PRECISION (short_integer_type_node
))
4298 r1
= int_range
<1> (integer_zero_node
, maxint
);
4299 range_cast (r1
, short_integer_type_node
);
4300 ASSERT_TRUE (r1
.lower_bound () == wi::to_wide (minshort
)
4301 && r1
.upper_bound() == wi::to_wide (maxshort
));
4304 // (unsigned char)[-5,-1] => [251,255].
4305 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (-1));
4306 range_cast (r0
, unsigned_char_type_node
);
4307 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (251), UCHAR (255)));
4308 range_cast (r0
, signed_char_type_node
);
4309 ASSERT_TRUE (r0
== rold
);
4311 // (signed char)[15, 150] => [-128,-106][15,127].
4312 r0
= rold
= int_range
<1> (UCHAR (15), UCHAR (150));
4313 range_cast (r0
, signed_char_type_node
);
4314 r1
= int_range
<1> (SCHAR (15), SCHAR (127));
4315 r2
= int_range
<1> (SCHAR (-128), SCHAR (-106));
4317 ASSERT_TRUE (r1
== r0
);
4318 range_cast (r0
, unsigned_char_type_node
);
4319 ASSERT_TRUE (r0
== rold
);
4321 // (unsigned char)[-5, 5] => [0,5][251,255].
4322 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (5));
4323 range_cast (r0
, unsigned_char_type_node
);
4324 r1
= int_range
<1> (UCHAR (251), UCHAR (255));
4325 r2
= int_range
<1> (UCHAR (0), UCHAR (5));
4327 ASSERT_TRUE (r0
== r1
);
4328 range_cast (r0
, signed_char_type_node
);
4329 ASSERT_TRUE (r0
== rold
);
4331 // (unsigned char)[-5,5] => [0,5][251,255].
4332 r0
= int_range
<1> (INT (-5), INT (5));
4333 range_cast (r0
, unsigned_char_type_node
);
4334 r1
= int_range
<1> (UCHAR (0), UCHAR (5));
4335 r1
.union_ (int_range
<1> (UCHAR (251), UCHAR (255)));
4336 ASSERT_TRUE (r0
== r1
);
4338 // (unsigned char)[5U,1974U] => [0,255].
4339 r0
= int_range
<1> (UINT (5), UINT (1974));
4340 range_cast (r0
, unsigned_char_type_node
);
4341 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (0), UCHAR (255)));
4342 range_cast (r0
, integer_type_node
);
4343 // Going to a wider range should not sign extend.
4344 ASSERT_TRUE (r0
== int_range
<1> (INT (0), INT (255)));
4346 // (unsigned char)[-350,15] => [0,255].
4347 r0
= int_range
<1> (INT (-350), INT (15));
4348 range_cast (r0
, unsigned_char_type_node
);
4349 ASSERT_TRUE (r0
== (int_range
<1>
4350 (TYPE_MIN_VALUE (unsigned_char_type_node
),
4351 TYPE_MAX_VALUE (unsigned_char_type_node
))));
4353 // Casting [-120,20] from signed char to unsigned short.
4354 // => [0, 20][0xff88, 0xffff].
4355 r0
= int_range
<1> (SCHAR (-120), SCHAR (20));
4356 range_cast (r0
, short_unsigned_type_node
);
4357 r1
= int_range
<1> (UINT16 (0), UINT16 (20));
4358 r2
= int_range
<1> (UINT16 (0xff88), UINT16 (0xffff));
4360 ASSERT_TRUE (r0
== r1
);
4361 // A truncating cast back to signed char will work because [-120, 20]
4362 // is representable in signed char.
4363 range_cast (r0
, signed_char_type_node
);
4364 ASSERT_TRUE (r0
== int_range
<1> (SCHAR (-120), SCHAR (20)));
4366 // unsigned char -> signed short
4367 // (signed short)[(unsigned char)25, (unsigned char)250]
4368 // => [(signed short)25, (signed short)250]
4369 r0
= rold
= int_range
<1> (UCHAR (25), UCHAR (250));
4370 range_cast (r0
, short_integer_type_node
);
4371 r1
= int_range
<1> (INT16 (25), INT16 (250));
4372 ASSERT_TRUE (r0
== r1
);
4373 range_cast (r0
, unsigned_char_type_node
);
4374 ASSERT_TRUE (r0
== rold
);
4376 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4377 r0
= int_range
<1> (TYPE_MIN_VALUE (long_long_integer_type_node
),
4378 TYPE_MAX_VALUE (long_long_integer_type_node
));
4379 range_cast (r0
, short_unsigned_type_node
);
4380 r1
= int_range
<1> (TYPE_MIN_VALUE (short_unsigned_type_node
),
4381 TYPE_MAX_VALUE (short_unsigned_type_node
));
4382 ASSERT_TRUE (r0
== r1
);
4384 // Casting NONZERO to a narrower type will wrap/overflow so
4385 // it's just the entire range for the narrower type.
4387 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4388 // is outside of the range of a smaller range, return the full
4390 if (TYPE_PRECISION (integer_type_node
)
4391 > TYPE_PRECISION (short_integer_type_node
))
4393 r0
= range_nonzero (integer_type_node
);
4394 range_cast (r0
, short_integer_type_node
);
4395 r1
= int_range
<1> (TYPE_MIN_VALUE (short_integer_type_node
),
4396 TYPE_MAX_VALUE (short_integer_type_node
));
4397 ASSERT_TRUE (r0
== r1
);
4400 // Casting NONZERO from a narrower signed to a wider signed.
4402 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4403 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4404 r0
= range_nonzero (short_integer_type_node
);
4405 range_cast (r0
, integer_type_node
);
4406 r1
= int_range
<1> (INT (-32768), INT (-1));
4407 r2
= int_range
<1> (INT (1), INT (32767));
4409 ASSERT_TRUE (r0
== r1
);
4413 range_op_lshift_tests ()
4415 // Test that 0x808.... & 0x8.... still contains 0x8....
4416 // for a large set of numbers.
4419 tree big_type
= long_long_unsigned_type_node
;
4420 // big_num = 0x808,0000,0000,0000
4421 tree big_num
= fold_build2 (LSHIFT_EXPR
, big_type
,
4422 build_int_cst (big_type
, 0x808),
4423 build_int_cst (big_type
, 48));
4424 op_bitwise_and
.fold_range (res
, big_type
,
4425 int_range
<1> (big_type
),
4426 int_range
<1> (big_num
, big_num
));
4427 // val = 0x8,0000,0000,0000
4428 tree val
= fold_build2 (LSHIFT_EXPR
, big_type
,
4429 build_int_cst (big_type
, 0x8),
4430 build_int_cst (big_type
, 48));
4431 ASSERT_TRUE (res
.contains_p (val
));
4434 if (TYPE_PRECISION (unsigned_type_node
) > 31)
4436 // unsigned VARYING = op1 << 1 should be VARYING.
4437 int_range
<2> lhs (unsigned_type_node
);
4438 int_range
<2> shift (INT (1), INT (1));
4440 op_lshift
.op1_range (op1
, unsigned_type_node
, lhs
, shift
);
4441 ASSERT_TRUE (op1
.varying_p ());
4443 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4444 int_range
<2> zero (UINT (0), UINT (0));
4445 op_lshift
.op1_range (op1
, unsigned_type_node
, zero
, shift
);
4446 ASSERT_TRUE (op1
.num_pairs () == 2);
4447 // Remove the [0,0] range.
4448 op1
.intersect (zero
);
4449 ASSERT_TRUE (op1
.num_pairs () == 1);
4450 // op1 << 1 should be [0x8000,0x8000] << 1,
4451 // which should result in [0,0].
4452 int_range_max result
;
4453 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4454 ASSERT_TRUE (result
== zero
);
4456 // signed VARYING = op1 << 1 should be VARYING.
4457 if (TYPE_PRECISION (integer_type_node
) > 31)
4459 // unsigned VARYING = op1 << 1 hould be VARYING.
4460 int_range
<2> lhs (integer_type_node
);
4461 int_range
<2> shift (INT (1), INT (1));
4463 op_lshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4464 ASSERT_TRUE (op1
.varying_p ());
4466 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4467 int_range
<2> zero (INT (0), INT (0));
4468 op_lshift
.op1_range (op1
, integer_type_node
, zero
, shift
);
4469 ASSERT_TRUE (op1
.num_pairs () == 2);
4470 // Remove the [0,0] range.
4471 op1
.intersect (zero
);
4472 ASSERT_TRUE (op1
.num_pairs () == 1);
4473 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4474 // which should result in [0,0].
4475 int_range_max result
;
4476 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4477 ASSERT_TRUE (result
== zero
);
4482 range_op_rshift_tests ()
4484 // unsigned: [3, MAX] = OP1 >> 1
4486 int_range_max
lhs (build_int_cst (unsigned_type_node
, 3),
4487 TYPE_MAX_VALUE (unsigned_type_node
));
4488 int_range_max
one (build_one_cst (unsigned_type_node
),
4489 build_one_cst (unsigned_type_node
));
4491 op_rshift
.op1_range (op1
, unsigned_type_node
, lhs
, one
);
4492 ASSERT_FALSE (op1
.contains_p (UINT (3)));
4495 // signed: [3, MAX] = OP1 >> 1
4497 int_range_max
lhs (INT (3), TYPE_MAX_VALUE (integer_type_node
));
4498 int_range_max
one (INT (1), INT (1));
4500 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4501 ASSERT_FALSE (op1
.contains_p (INT (-2)));
4504 // This is impossible, so OP1 should be [].
4505 // signed: [MIN, MIN] = OP1 >> 1
4507 int_range_max
lhs (TYPE_MIN_VALUE (integer_type_node
),
4508 TYPE_MIN_VALUE (integer_type_node
));
4509 int_range_max
one (INT (1), INT (1));
4511 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4512 ASSERT_TRUE (op1
.undefined_p ());
4515 // signed: ~[-1] = OP1 >> 31
4516 if (TYPE_PRECISION (integer_type_node
) > 31)
4518 int_range_max
lhs (INT (-1), INT (-1), VR_ANTI_RANGE
);
4519 int_range_max
shift (INT (31), INT (31));
4521 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4522 int_range_max negatives
= range_negatives (integer_type_node
);
4523 negatives
.intersect (op1
);
4524 ASSERT_TRUE (negatives
.undefined_p ());
4529 range_op_bitwise_and_tests ()
4532 tree min
= vrp_val_min (integer_type_node
);
4533 tree max
= vrp_val_max (integer_type_node
);
4534 tree tiny
= fold_build2 (PLUS_EXPR
, integer_type_node
, min
,
4535 build_one_cst (integer_type_node
));
4536 int_range_max
i1 (tiny
, max
);
4537 int_range_max
i2 (build_int_cst (integer_type_node
, 255),
4538 build_int_cst (integer_type_node
, 255));
4540 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4541 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4542 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4544 // VARYING = OP1 & 255: OP1 is VARYING
4545 i1
= int_range
<1> (integer_type_node
);
4546 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4547 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4549 // (NONZERO | X) is nonzero.
4550 i1
.set_nonzero (integer_type_node
);
4551 i2
.set_varying (integer_type_node
);
4552 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4553 ASSERT_TRUE (res
.nonzero_p ());
4555 // (NEGATIVE | X) is nonzero.
4556 i1
= int_range
<1> (INT (-5), INT (-3));
4557 i2
.set_varying (integer_type_node
);
4558 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4559 ASSERT_FALSE (res
.contains_p (INT (0)));
4563 range_relational_tests ()
4565 int_range
<2> lhs (unsigned_char_type_node
);
4566 int_range
<2> op1 (UCHAR (8), UCHAR (10));
4567 int_range
<2> op2 (UCHAR (20), UCHAR (20));
4569 // Never wrapping additions mean LHS > OP1.
4570 relation_kind code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4571 ASSERT_TRUE (code
== VREL_GT
);
4573 // Most wrapping additions mean nothing...
4574 op1
= int_range
<2> (UCHAR (8), UCHAR (10));
4575 op2
= int_range
<2> (UCHAR (0), UCHAR (255));
4576 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4577 ASSERT_TRUE (code
== VREL_VARYING
);
4579 // However, always wrapping additions mean LHS < OP1.
4580 op1
= int_range
<2> (UCHAR (1), UCHAR (255));
4581 op2
= int_range
<2> (UCHAR (255), UCHAR (255));
4582 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4583 ASSERT_TRUE (code
== VREL_LT
);
4589 range_op_rshift_tests ();
4590 range_op_lshift_tests ();
4591 range_op_bitwise_and_tests ();
4592 range_op_cast_tests ();
4593 range_relational_tests ();
4596 } // namespace selftest
4598 #endif // CHECKING_P