1 /* Code for range operators.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "insn-codes.h"
31 #include "tree-pass.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
41 #include "gimple-fold.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
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 // If the range of either op1 or op2 is undefined, set the result to
67 // varying and return TRUE. If the caller truely cares about a result,
68 // they should pass in a varying if it has an undefined that it wants
69 // treated as a varying.
72 empty_range_varying (irange
&r
, tree type
,
73 const irange
&op1
, const irange
& op2
)
75 if (op1
.undefined_p () || op2
.undefined_p ())
84 // Return false if shifting by OP is undefined behavior. Otherwise, return
85 // true and the range it is to be shifted by. This allows trimming out of
86 // undefined ranges, leaving only valid ranges if there are any.
89 get_shift_range (irange
&r
, tree type
, const irange
&op
)
91 if (op
.undefined_p ())
94 // Build valid range and intersect it with the shift range.
95 r
= value_range (build_int_cst_type (op
.type (), 0),
96 build_int_cst_type (op
.type (), TYPE_PRECISION (type
) - 1));
99 // If there are no valid ranges in the shift range, returned false.
100 if (r
.undefined_p ())
105 // Return TRUE if 0 is within [WMIN, WMAX].
108 wi_includes_zero_p (tree type
, const wide_int
&wmin
, const wide_int
&wmax
)
110 signop sign
= TYPE_SIGN (type
);
111 return wi::le_p (wmin
, 0, sign
) && wi::ge_p (wmax
, 0, sign
);
114 // Return TRUE if [WMIN, WMAX] is the singleton 0.
117 wi_zero_p (tree type
, const wide_int
&wmin
, const wide_int
&wmax
)
119 unsigned prec
= TYPE_PRECISION (type
);
120 return wmin
== wmax
&& wi::eq_p (wmin
, wi::zero (prec
));
123 // Default wide_int fold operation returns [MIN, MAX].
126 range_operator::wi_fold (irange
&r
, tree type
,
127 const wide_int
&lh_lb ATTRIBUTE_UNUSED
,
128 const wide_int
&lh_ub ATTRIBUTE_UNUSED
,
129 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
130 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
132 gcc_checking_assert (irange::supports_type_p (type
));
133 r
.set_varying (type
);
136 // Call wi_fold, except further split small subranges into constants.
137 // This can provide better precision. For something 8 >> [0,1]
138 // Instead of [8, 16], we will produce [8,8][16,16]
141 range_operator::wi_fold_in_parts (irange
&r
, tree type
,
142 const wide_int
&lh_lb
,
143 const wide_int
&lh_ub
,
144 const wide_int
&rh_lb
,
145 const wide_int
&rh_ub
) const
147 wi::overflow_type ov_rh
, ov_lh
;
149 wide_int rh_range
= wi::sub (rh_ub
, rh_lb
, TYPE_SIGN (type
), &ov_rh
);
150 wide_int lh_range
= wi::sub (lh_ub
, lh_lb
, TYPE_SIGN (type
), &ov_lh
);
151 signop sign
= TYPE_SIGN (type
);;
152 // If there are 2, 3, or 4 values in the RH range, do them separately.
153 // Call wi_fold_in_parts to check the RH side.
154 if (wi::gt_p (rh_range
, 0, sign
) && wi::lt_p (rh_range
, 4, sign
)
155 && ov_rh
== wi::OVF_NONE
)
157 wi_fold_in_parts (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_lb
);
158 if (wi::gt_p (rh_range
, 1, sign
))
160 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 1, rh_lb
+ 1);
162 if (wi::eq_p (rh_range
, 3))
164 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 2, rh_lb
+ 2);
168 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_ub
, rh_ub
);
171 // Otherise check for 2, 3, or 4 values in the LH range and split them up.
172 // The RH side has been checked, so no recursion needed.
173 else if (wi::gt_p (lh_range
, 0, sign
) && wi::lt_p (lh_range
, 4, sign
)
174 && ov_lh
== wi::OVF_NONE
)
176 wi_fold (r
, type
, lh_lb
, lh_lb
, rh_lb
, rh_ub
);
177 if (wi::gt_p (lh_range
, 1, sign
))
179 wi_fold (tmp
, type
, lh_lb
+ 1, lh_lb
+ 1, rh_lb
, rh_ub
);
181 if (wi::eq_p (lh_range
, 3))
183 wi_fold (tmp
, type
, lh_lb
+ 2, lh_lb
+ 2, rh_lb
, rh_ub
);
187 wi_fold (tmp
, type
, lh_ub
, lh_ub
, rh_lb
, rh_ub
);
190 // Otherwise just call wi_fold.
192 wi_fold (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
195 // The default for fold is to break all ranges into sub-ranges and
196 // invoke the wi_fold method on each sub-range pair.
199 range_operator::fold_range (irange
&r
, tree type
,
202 relation_kind rel
) const
204 gcc_checking_assert (irange::supports_type_p (type
));
205 if (empty_range_varying (r
, type
, lh
, rh
))
208 unsigned num_lh
= lh
.num_pairs ();
209 unsigned num_rh
= rh
.num_pairs ();
211 // If both ranges are single pairs, fold directly into the result range.
212 if (num_lh
== 1 && num_rh
== 1)
214 wi_fold_in_parts (r
, type
, lh
.lower_bound (0), lh
.upper_bound (0),
215 rh
.lower_bound (0), rh
.upper_bound (0));
216 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
222 for (unsigned x
= 0; x
< num_lh
; ++x
)
223 for (unsigned y
= 0; y
< num_rh
; ++y
)
225 wide_int lh_lb
= lh
.lower_bound (x
);
226 wide_int lh_ub
= lh
.upper_bound (x
);
227 wide_int rh_lb
= rh
.lower_bound (y
);
228 wide_int rh_ub
= rh
.upper_bound (y
);
229 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
233 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
237 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
241 // The default for op1_range is to return false.
244 range_operator::op1_range (irange
&r ATTRIBUTE_UNUSED
,
245 tree type ATTRIBUTE_UNUSED
,
246 const irange
&lhs ATTRIBUTE_UNUSED
,
247 const irange
&op2 ATTRIBUTE_UNUSED
,
248 relation_kind rel ATTRIBUTE_UNUSED
) const
253 // The default for op2_range is to return false.
256 range_operator::op2_range (irange
&r ATTRIBUTE_UNUSED
,
257 tree type ATTRIBUTE_UNUSED
,
258 const irange
&lhs ATTRIBUTE_UNUSED
,
259 const irange
&op1 ATTRIBUTE_UNUSED
,
260 relation_kind rel ATTRIBUTE_UNUSED
) const
265 // The default relation routines return VREL_NONE.
268 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
269 const irange
&op1 ATTRIBUTE_UNUSED
,
270 const irange
&op2 ATTRIBUTE_UNUSED
) const
276 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
277 const irange
&op1 ATTRIBUTE_UNUSED
,
278 const irange
&op2 ATTRIBUTE_UNUSED
) const
284 range_operator::op1_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
) const
289 // Default is no relation affects the LHS.
292 range_operator::op1_op2_relation_effect (irange
&lhs_range ATTRIBUTE_UNUSED
,
293 tree type ATTRIBUTE_UNUSED
,
294 const irange
&op1_range ATTRIBUTE_UNUSED
,
295 const irange
&op2_range ATTRIBUTE_UNUSED
,
296 relation_kind rel ATTRIBUTE_UNUSED
) const
301 // Create and return a range from a pair of wide-ints that are known
302 // to have overflowed (or underflowed).
305 value_range_from_overflowed_bounds (irange
&r
, tree type
,
306 const wide_int
&wmin
,
307 const wide_int
&wmax
)
309 const signop sgn
= TYPE_SIGN (type
);
310 const unsigned int prec
= TYPE_PRECISION (type
);
312 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
313 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
318 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
321 if (wi::cmp (tmax
, tem
, sgn
) > 0)
324 // If the anti-range would cover nothing, drop to varying.
325 // Likewise if the anti-range bounds are outside of the types
327 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
328 r
.set_varying (type
);
331 tree tree_min
= wide_int_to_tree (type
, tmin
);
332 tree tree_max
= wide_int_to_tree (type
, tmax
);
333 r
.set (tree_min
, tree_max
, VR_ANTI_RANGE
);
337 // Create and return a range from a pair of wide-ints. MIN_OVF and
338 // MAX_OVF describe any overflow that might have occurred while
339 // calculating WMIN and WMAX respectively.
342 value_range_with_overflow (irange
&r
, tree type
,
343 const wide_int
&wmin
, const wide_int
&wmax
,
344 wi::overflow_type min_ovf
= wi::OVF_NONE
,
345 wi::overflow_type max_ovf
= wi::OVF_NONE
)
347 const signop sgn
= TYPE_SIGN (type
);
348 const unsigned int prec
= TYPE_PRECISION (type
);
349 const bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
351 // For one bit precision if max != min, then the range covers all
353 if (prec
== 1 && wi::ne_p (wmax
, wmin
))
355 r
.set_varying (type
);
361 // If overflow wraps, truncate the values and adjust the range,
362 // kind, and bounds appropriately.
363 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
365 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
366 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
367 // If the limits are swapped, we wrapped around and cover
369 if (wi::gt_p (tmin
, tmax
, sgn
))
370 r
.set_varying (type
);
372 // No overflow or both overflow or underflow. The range
373 // kind stays normal.
374 r
.set (wide_int_to_tree (type
, tmin
),
375 wide_int_to_tree (type
, tmax
));
379 if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
380 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
381 value_range_from_overflowed_bounds (r
, type
, wmin
, wmax
);
383 // Other underflow and/or overflow, drop to VR_VARYING.
384 r
.set_varying (type
);
388 // If both bounds either underflowed or overflowed, then the result
390 if ((min_ovf
== wi::OVF_OVERFLOW
&& max_ovf
== wi::OVF_OVERFLOW
)
391 || (min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_UNDERFLOW
))
397 // If overflow does not wrap, saturate to [MIN, MAX].
398 wide_int new_lb
, new_ub
;
399 if (min_ovf
== wi::OVF_UNDERFLOW
)
400 new_lb
= wi::min_value (prec
, sgn
);
401 else if (min_ovf
== wi::OVF_OVERFLOW
)
402 new_lb
= wi::max_value (prec
, sgn
);
406 if (max_ovf
== wi::OVF_UNDERFLOW
)
407 new_ub
= wi::min_value (prec
, sgn
);
408 else if (max_ovf
== wi::OVF_OVERFLOW
)
409 new_ub
= wi::max_value (prec
, sgn
);
413 r
.set (wide_int_to_tree (type
, new_lb
),
414 wide_int_to_tree (type
, new_ub
));
418 // Create and return a range from a pair of wide-ints. Canonicalize
419 // the case where the bounds are swapped. In which case, we transform
420 // [10,5] into [MIN,5][10,MAX].
423 create_possibly_reversed_range (irange
&r
, tree type
,
424 const wide_int
&new_lb
, const wide_int
&new_ub
)
426 signop s
= TYPE_SIGN (type
);
427 // If the bounds are swapped, treat the result as if an overflow occured.
428 if (wi::gt_p (new_lb
, new_ub
, s
))
429 value_range_from_overflowed_bounds (r
, type
, new_lb
, new_ub
);
431 // Otherwise it's just a normal range.
432 r
.set (wide_int_to_tree (type
, new_lb
), wide_int_to_tree (type
, new_ub
));
435 // Return an irange instance that is a boolean TRUE.
437 static inline int_range
<1>
438 range_true (tree type
)
440 unsigned prec
= TYPE_PRECISION (type
);
441 return int_range
<1> (type
, wi::one (prec
), wi::one (prec
));
444 // Return an irange instance that is a boolean FALSE.
446 static inline int_range
<1>
447 range_false (tree type
)
449 unsigned prec
= TYPE_PRECISION (type
);
450 return int_range
<1> (type
, wi::zero (prec
), wi::zero (prec
));
453 // Return an irange that covers both true and false.
455 static inline int_range
<1>
456 range_true_and_false (tree type
)
458 unsigned prec
= TYPE_PRECISION (type
);
459 return int_range
<1> (type
, wi::zero (prec
), wi::one (prec
));
462 enum bool_range_state
{ BRS_FALSE
, BRS_TRUE
, BRS_EMPTY
, BRS_FULL
};
464 // Return the summary information about boolean range LHS. If EMPTY/FULL,
465 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
467 static bool_range_state
468 get_bool_state (irange
&r
, const irange
&lhs
, tree val_type
)
470 // If there is no result, then this is unexecutable.
471 if (lhs
.undefined_p ())
480 // For TRUE, we can't just test for [1,1] because Ada can have
481 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
482 if (lhs
.contains_p (build_zero_cst (lhs
.type ())))
484 r
.set_varying (val_type
);
491 // For relation opcodes, first try to see if the supplied relation
492 // forces a true or false result, and return that.
493 // Then check for undefined operands. If none of this applies,
497 relop_early_resolve (irange
&r
, tree type
, const irange
&op1
,
498 const irange
&op2
, relation_kind rel
,
499 relation_kind my_rel
)
501 // If known relation is a complete subset of this relation, always true.
502 if (relation_union (rel
, my_rel
) == my_rel
)
504 r
= range_true (type
);
508 // If known relation has no subset of this relation, always false.
509 if (relation_intersect (rel
, my_rel
) == VREL_EMPTY
)
511 r
= range_false (type
);
515 // If either operand is undefined, return VARYING.
516 if (empty_range_varying (r
, type
, op1
, op2
))
523 class operator_equal
: public range_operator
526 virtual bool fold_range (irange
&r
, tree type
,
529 relation_kind rel
= VREL_NONE
) const;
530 virtual bool op1_range (irange
&r
, tree type
,
533 relation_kind rel
= VREL_NONE
) const;
534 virtual bool op2_range (irange
&r
, tree type
,
537 relation_kind rel
= VREL_NONE
) const;
538 virtual enum tree_code
op1_op2_relation (const irange
&lhs
) const;
541 // Check if the LHS range indicates a relation between OP1 and OP2.
544 operator_equal::op1_op2_relation (const irange
&lhs
) const
546 if (lhs
.undefined_p ())
549 // FALSE = op1 == op2 indicates NE_EXPR.
553 // TRUE = op1 == op2 indicates EQ_EXPR.
554 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
561 operator_equal::fold_range (irange
&r
, tree type
,
564 relation_kind rel
) const
566 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, EQ_EXPR
))
569 // We can be sure the values are always equal or not if both ranges
570 // consist of a single value, and then compare them.
571 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
572 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
574 if (wi::eq_p (op1
.lower_bound (), op2
.upper_bound()))
575 r
= range_true (type
);
577 r
= range_false (type
);
581 // If ranges do not intersect, we know the range is not equal,
582 // otherwise we don't know anything for sure.
583 int_range_max tmp
= op1
;
585 if (tmp
.undefined_p ())
586 r
= range_false (type
);
588 r
= range_true_and_false (type
);
594 operator_equal::op1_range (irange
&r
, tree type
,
597 relation_kind rel ATTRIBUTE_UNUSED
) const
599 switch (get_bool_state (r
, lhs
, type
))
602 // If the result is false, the only time we know anything is
603 // if OP2 is a constant.
604 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
610 r
.set_varying (type
);
614 // If it's true, the result is the same as OP2.
625 operator_equal::op2_range (irange
&r
, tree type
,
628 relation_kind rel
) const
630 return operator_equal::op1_range (r
, type
, lhs
, op1
, rel
);
633 class operator_not_equal
: public range_operator
636 virtual bool fold_range (irange
&r
, tree type
,
639 relation_kind rel
= VREL_NONE
) const;
640 virtual bool op1_range (irange
&r
, tree type
,
643 relation_kind rel
= VREL_NONE
) const;
644 virtual bool op2_range (irange
&r
, tree type
,
647 relation_kind rel
= VREL_NONE
) const;
648 virtual enum tree_code
op1_op2_relation (const irange
&lhs
) const;
651 // Check if the LHS range indicates a relation between OP1 and OP2.
654 operator_not_equal::op1_op2_relation (const irange
&lhs
) const
656 if (lhs
.undefined_p ())
659 // FALSE = op1 != op2 indicates EQ_EXPR.
663 // TRUE = op1 != op2 indicates NE_EXPR.
664 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
670 operator_not_equal::fold_range (irange
&r
, tree type
,
673 relation_kind rel
) const
675 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, NE_EXPR
))
678 // We can be sure the values are always equal or not if both ranges
679 // consist of a single value, and then compare them.
680 if (wi::eq_p (op1
.lower_bound (), op1
.upper_bound ())
681 && wi::eq_p (op2
.lower_bound (), op2
.upper_bound ()))
683 if (wi::ne_p (op1
.lower_bound (), op2
.upper_bound()))
684 r
= range_true (type
);
686 r
= range_false (type
);
690 // If ranges do not intersect, we know the range is not equal,
691 // otherwise we don't know anything for sure.
692 int_range_max tmp
= op1
;
694 if (tmp
.undefined_p ())
695 r
= range_true (type
);
697 r
= range_true_and_false (type
);
703 operator_not_equal::op1_range (irange
&r
, tree type
,
706 relation_kind rel ATTRIBUTE_UNUSED
) const
708 switch (get_bool_state (r
, lhs
, type
))
711 // If the result is true, the only time we know anything is if
712 // OP2 is a constant.
713 if (wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
719 r
.set_varying (type
);
723 // If it's false, the result is the same as OP2.
735 operator_not_equal::op2_range (irange
&r
, tree type
,
738 relation_kind rel
) const
740 return operator_not_equal::op1_range (r
, type
, lhs
, op1
, rel
);
743 // (X < VAL) produces the range of [MIN, VAL - 1].
746 build_lt (irange
&r
, tree type
, const wide_int
&val
)
748 wi::overflow_type ov
;
750 signop sgn
= TYPE_SIGN (type
);
752 // Signed 1 bit cannot represent 1 for subtraction.
754 lim
= wi::add (val
, -1, sgn
, &ov
);
756 lim
= wi::sub (val
, 1, sgn
, &ov
);
758 // If val - 1 underflows, check if X < MIN, which is an empty range.
762 r
= int_range
<1> (type
, min_limit (type
), lim
);
765 // (X <= VAL) produces the range of [MIN, VAL].
768 build_le (irange
&r
, tree type
, const wide_int
&val
)
770 r
= int_range
<1> (type
, min_limit (type
), val
);
773 // (X > VAL) produces the range of [VAL + 1, MAX].
776 build_gt (irange
&r
, tree type
, const wide_int
&val
)
778 wi::overflow_type ov
;
780 signop sgn
= TYPE_SIGN (type
);
782 // Signed 1 bit cannot represent 1 for addition.
784 lim
= wi::sub (val
, -1, sgn
, &ov
);
786 lim
= wi::add (val
, 1, sgn
, &ov
);
787 // If val + 1 overflows, check is for X > MAX, which is an empty range.
791 r
= int_range
<1> (type
, lim
, max_limit (type
));
794 // (X >= val) produces the range of [VAL, MAX].
797 build_ge (irange
&r
, tree type
, const wide_int
&val
)
799 r
= int_range
<1> (type
, val
, max_limit (type
));
803 class operator_lt
: public range_operator
806 virtual bool fold_range (irange
&r
, tree type
,
809 relation_kind rel
= VREL_NONE
) const;
810 virtual bool op1_range (irange
&r
, tree type
,
813 relation_kind rel
= VREL_NONE
) const;
814 virtual bool op2_range (irange
&r
, tree type
,
817 relation_kind rel
= VREL_NONE
) const;
818 virtual enum tree_code
op1_op2_relation (const irange
&lhs
) const;
821 // Check if the LHS range indicates a relation between OP1 and OP2.
824 operator_lt::op1_op2_relation (const irange
&lhs
) const
826 if (lhs
.undefined_p ())
829 // FALSE = op1 < op2 indicates GE_EXPR.
833 // TRUE = op1 < op2 indicates LT_EXPR.
834 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
840 operator_lt::fold_range (irange
&r
, tree type
,
843 relation_kind rel
) const
845 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, LT_EXPR
))
848 signop sign
= TYPE_SIGN (op1
.type ());
849 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
851 if (wi::lt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
852 r
= range_true (type
);
853 else if (!wi::lt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
854 r
= range_false (type
);
856 r
= range_true_and_false (type
);
861 operator_lt::op1_range (irange
&r
, tree type
,
864 relation_kind rel ATTRIBUTE_UNUSED
) const
866 switch (get_bool_state (r
, lhs
, type
))
869 build_lt (r
, type
, op2
.upper_bound ());
873 build_ge (r
, type
, op2
.lower_bound ());
883 operator_lt::op2_range (irange
&r
, tree type
,
886 relation_kind rel ATTRIBUTE_UNUSED
) const
888 switch (get_bool_state (r
, lhs
, type
))
891 build_le (r
, type
, op1
.upper_bound ());
895 build_gt (r
, type
, op1
.lower_bound ());
905 class operator_le
: public range_operator
908 virtual bool fold_range (irange
&r
, tree type
,
911 relation_kind rel
= VREL_NONE
) const;
912 virtual bool op1_range (irange
&r
, tree type
,
915 relation_kind rel
= VREL_NONE
) const;
916 virtual bool op2_range (irange
&r
, tree type
,
919 relation_kind rel
= VREL_NONE
) const;
920 virtual enum tree_code
op1_op2_relation (const irange
&lhs
) const;
923 // Check if the LHS range indicates a relation between OP1 and OP2.
926 operator_le::op1_op2_relation (const irange
&lhs
) const
928 if (lhs
.undefined_p ())
931 // FALSE = op1 <= op2 indicates GT_EXPR.
935 // TRUE = op1 <= op2 indicates LE_EXPR.
936 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
942 operator_le::fold_range (irange
&r
, tree type
,
945 relation_kind rel
) const
947 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, LE_EXPR
))
950 signop sign
= TYPE_SIGN (op1
.type ());
951 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
953 if (wi::le_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
954 r
= range_true (type
);
955 else if (!wi::le_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
956 r
= range_false (type
);
958 r
= range_true_and_false (type
);
963 operator_le::op1_range (irange
&r
, tree type
,
966 relation_kind rel ATTRIBUTE_UNUSED
) const
968 switch (get_bool_state (r
, lhs
, type
))
971 build_le (r
, type
, op2
.upper_bound ());
975 build_gt (r
, type
, op2
.lower_bound ());
985 operator_le::op2_range (irange
&r
, tree type
,
988 relation_kind rel ATTRIBUTE_UNUSED
) const
990 switch (get_bool_state (r
, lhs
, type
))
993 build_lt (r
, type
, op1
.upper_bound ());
997 build_ge (r
, type
, op1
.lower_bound ());
1007 class operator_gt
: public range_operator
1010 virtual bool fold_range (irange
&r
, tree type
,
1013 relation_kind rel
= VREL_NONE
) const;
1014 virtual bool op1_range (irange
&r
, tree type
,
1017 relation_kind rel
= VREL_NONE
) const;
1018 virtual bool op2_range (irange
&r
, tree type
,
1021 relation_kind rel
= VREL_NONE
) const;
1022 virtual enum tree_code
op1_op2_relation (const irange
&lhs
) const;
1025 // Check if the LHS range indicates a relation between OP1 and OP2.
1028 operator_gt::op1_op2_relation (const irange
&lhs
) const
1030 if (lhs
.undefined_p ())
1033 // FALSE = op1 > op2 indicates LE_EXPR.
1037 // TRUE = op1 > op2 indicates GT_EXPR.
1038 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
1045 operator_gt::fold_range (irange
&r
, tree type
,
1046 const irange
&op1
, const irange
&op2
,
1047 relation_kind rel
) const
1049 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, GT_EXPR
))
1052 signop sign
= TYPE_SIGN (op1
.type ());
1053 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1055 if (wi::gt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1056 r
= range_true (type
);
1057 else if (!wi::gt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1058 r
= range_false (type
);
1060 r
= range_true_and_false (type
);
1065 operator_gt::op1_range (irange
&r
, tree type
,
1066 const irange
&lhs
, const irange
&op2
,
1067 relation_kind rel ATTRIBUTE_UNUSED
) const
1069 switch (get_bool_state (r
, lhs
, type
))
1072 build_gt (r
, type
, op2
.lower_bound ());
1076 build_le (r
, type
, op2
.upper_bound ());
1086 operator_gt::op2_range (irange
&r
, tree type
,
1089 relation_kind rel ATTRIBUTE_UNUSED
) const
1091 switch (get_bool_state (r
, lhs
, type
))
1094 build_ge (r
, type
, op1
.lower_bound ());
1098 build_lt (r
, type
, op1
.upper_bound ());
1108 class operator_ge
: public range_operator
1111 virtual bool fold_range (irange
&r
, tree type
,
1114 relation_kind rel
= VREL_NONE
) const;
1115 virtual bool op1_range (irange
&r
, tree type
,
1118 relation_kind rel
= VREL_NONE
) const;
1119 virtual bool op2_range (irange
&r
, tree type
,
1122 relation_kind rel
= VREL_NONE
) const;
1123 virtual enum tree_code
op1_op2_relation (const irange
&lhs
) const;
1126 // Check if the LHS range indicates a relation between OP1 and OP2.
1129 operator_ge::op1_op2_relation (const irange
&lhs
) const
1131 if (lhs
.undefined_p ())
1134 // FALSE = op1 >= op2 indicates LT_EXPR.
1138 // TRUE = op1 >= op2 indicates GE_EXPR.
1139 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
1145 operator_ge::fold_range (irange
&r
, tree type
,
1148 relation_kind rel
) const
1150 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, GE_EXPR
))
1153 signop sign
= TYPE_SIGN (op1
.type ());
1154 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1156 if (wi::ge_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1157 r
= range_true (type
);
1158 else if (!wi::ge_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1159 r
= range_false (type
);
1161 r
= range_true_and_false (type
);
1166 operator_ge::op1_range (irange
&r
, tree type
,
1169 relation_kind rel ATTRIBUTE_UNUSED
) const
1171 switch (get_bool_state (r
, lhs
, type
))
1174 build_ge (r
, type
, op2
.lower_bound ());
1178 build_lt (r
, type
, op2
.upper_bound ());
1188 operator_ge::op2_range (irange
&r
, tree type
,
1191 relation_kind rel ATTRIBUTE_UNUSED
) const
1193 switch (get_bool_state (r
, lhs
, type
))
1196 build_gt (r
, type
, op1
.lower_bound ());
1200 build_le (r
, type
, op1
.upper_bound ());
1210 class operator_plus
: public range_operator
1213 virtual bool op1_range (irange
&r
, tree type
,
1216 relation_kind rel ATTRIBUTE_UNUSED
) const;
1217 virtual bool op2_range (irange
&r
, tree type
,
1220 relation_kind rel ATTRIBUTE_UNUSED
) const;
1221 virtual void wi_fold (irange
&r
, tree type
,
1222 const wide_int
&lh_lb
,
1223 const wide_int
&lh_ub
,
1224 const wide_int
&rh_lb
,
1225 const wide_int
&rh_ub
) const;
1226 virtual enum tree_code
lhs_op1_relation (const irange
&lhs
, const irange
&op1
,
1227 const irange
&op2
) const;
1228 virtual enum tree_code
lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1229 const irange
&op2
) const;
1232 // Check to see if the range of OP2 indicates anything about the relation
1233 // between LHS and OP1.
1236 operator_plus::lhs_op1_relation (const irange
&lhs
,
1238 const irange
&op2
) const
1240 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
1243 tree type
= lhs
.type ();
1244 unsigned prec
= TYPE_PRECISION (type
);
1245 wi::overflow_type ovf1
, ovf2
;
1246 signop sign
= TYPE_SIGN (type
);
1248 // LHS = OP1 + 0 indicates LHS == OP1.
1252 if (TYPE_OVERFLOW_WRAPS (type
))
1254 wi::add (op1
.lower_bound (), op2
.lower_bound (), sign
, &ovf1
);
1255 wi::add (op1
.upper_bound (), op2
.upper_bound (), sign
, &ovf2
);
1258 ovf1
= ovf2
= wi::OVF_NONE
;
1260 // Never wrapping additions.
1263 // Positive op2 means lhs > op1.
1264 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1266 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1269 // Negative op2 means lhs < op1.
1270 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1272 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1275 // Always wrapping additions.
1276 else if (ovf1
&& ovf1
== ovf2
)
1278 // Positive op2 means lhs < op1.
1279 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1281 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1284 // Negative op2 means lhs > op1.
1285 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1287 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1291 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1292 if (!range_includes_zero_p (&op2
))
1298 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1302 operator_plus::lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1303 const irange
&op2
) const
1305 return lhs_op1_relation (lhs
, op2
, op1
);
1309 operator_plus::wi_fold (irange
&r
, tree type
,
1310 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1311 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1313 wi::overflow_type ov_lb
, ov_ub
;
1314 signop s
= TYPE_SIGN (type
);
1315 wide_int new_lb
= wi::add (lh_lb
, rh_lb
, s
, &ov_lb
);
1316 wide_int new_ub
= wi::add (lh_ub
, rh_ub
, s
, &ov_ub
);
1317 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1321 operator_plus::op1_range (irange
&r
, tree type
,
1324 relation_kind rel ATTRIBUTE_UNUSED
) const
1326 return range_op_handler (MINUS_EXPR
, type
)->fold_range (r
, type
, lhs
, op2
);
1330 operator_plus::op2_range (irange
&r
, tree type
,
1333 relation_kind rel ATTRIBUTE_UNUSED
) const
1335 return range_op_handler (MINUS_EXPR
, type
)->fold_range (r
, type
, lhs
, op1
);
1339 class operator_minus
: public range_operator
1342 virtual bool op1_range (irange
&r
, tree type
,
1345 relation_kind rel ATTRIBUTE_UNUSED
) const;
1346 virtual bool op2_range (irange
&r
, tree type
,
1349 relation_kind rel ATTRIBUTE_UNUSED
) const;
1350 virtual void wi_fold (irange
&r
, tree type
,
1351 const wide_int
&lh_lb
,
1352 const wide_int
&lh_ub
,
1353 const wide_int
&rh_lb
,
1354 const wide_int
&rh_ub
) const;
1355 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1357 const irange
&op1_range
,
1358 const irange
&op2_range
,
1359 relation_kind rel
) const;
1363 operator_minus::wi_fold (irange
&r
, tree type
,
1364 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1365 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1367 wi::overflow_type ov_lb
, ov_ub
;
1368 signop s
= TYPE_SIGN (type
);
1369 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
1370 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
1371 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1374 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1375 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1376 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1379 minus_op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1380 const irange
&op1_range ATTRIBUTE_UNUSED
,
1381 const irange
&op2_range ATTRIBUTE_UNUSED
,
1384 if (rel
== VREL_NONE
)
1387 int_range
<2> rel_range
;
1388 unsigned prec
= TYPE_PRECISION (type
);
1389 signop sgn
= TYPE_SIGN (type
);
1391 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1393 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
));
1394 else if (rel
== NE_EXPR
)
1395 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1397 else if (TYPE_OVERFLOW_WRAPS (type
))
1401 // For wrapping signed values and unsigned, if op1 > op2 or
1402 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1405 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1416 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1418 rel_range
= int_range
<2> (type
, wi::one (prec
),
1419 wi::max_value (prec
, sgn
));
1421 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1423 rel_range
= int_range
<2> (type
, wi::zero (prec
),
1424 wi::max_value (prec
, sgn
));
1426 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1428 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1429 wi::minus_one (prec
));
1431 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1433 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1440 lhs_range
.intersect (rel_range
);
1445 operator_minus::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1446 const irange
&op1_range
,
1447 const irange
&op2_range
,
1448 relation_kind rel
) const
1450 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1455 operator_minus::op1_range (irange
&r
, tree type
,
1458 relation_kind rel ATTRIBUTE_UNUSED
) const
1460 return range_op_handler (PLUS_EXPR
, type
)->fold_range (r
, type
, lhs
, op2
);
1464 operator_minus::op2_range (irange
&r
, tree type
,
1467 relation_kind rel ATTRIBUTE_UNUSED
) const
1469 return fold_range (r
, type
, op1
, lhs
);
1473 class operator_pointer_diff
: public range_operator
1475 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
1477 const irange
&op1_range
,
1478 const irange
&op2_range
,
1479 relation_kind rel
) const;
1483 operator_pointer_diff::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1484 const irange
&op1_range
,
1485 const irange
&op2_range
,
1486 relation_kind rel
) const
1488 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1493 class operator_min
: public range_operator
1496 virtual void wi_fold (irange
&r
, tree type
,
1497 const wide_int
&lh_lb
,
1498 const wide_int
&lh_ub
,
1499 const wide_int
&rh_lb
,
1500 const wide_int
&rh_ub
) const;
1504 operator_min::wi_fold (irange
&r
, tree type
,
1505 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1506 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1508 signop s
= TYPE_SIGN (type
);
1509 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
1510 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
1511 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1515 class operator_max
: public range_operator
1518 virtual void wi_fold (irange
&r
, tree type
,
1519 const wide_int
&lh_lb
,
1520 const wide_int
&lh_ub
,
1521 const wide_int
&rh_lb
,
1522 const wide_int
&rh_ub
) const;
1526 operator_max::wi_fold (irange
&r
, tree type
,
1527 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1528 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1530 signop s
= TYPE_SIGN (type
);
1531 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
1532 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
1533 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
1537 class cross_product_operator
: public range_operator
1540 // Perform an operation between two wide-ints and place the result
1541 // in R. Return true if the operation overflowed.
1542 virtual bool wi_op_overflows (wide_int
&r
,
1545 const wide_int
&) const = 0;
1547 // Calculate the cross product of two sets of sub-ranges and return it.
1548 void wi_cross_product (irange
&r
, tree type
,
1549 const wide_int
&lh_lb
,
1550 const wide_int
&lh_ub
,
1551 const wide_int
&rh_lb
,
1552 const wide_int
&rh_ub
) const;
1555 // Calculate the cross product of two sets of ranges and return it.
1557 // Multiplications, divisions and shifts are a bit tricky to handle,
1558 // depending on the mix of signs we have in the two ranges, we need to
1559 // operate on different values to get the minimum and maximum values
1560 // for the new range. One approach is to figure out all the
1561 // variations of range combinations and do the operations.
1563 // However, this involves several calls to compare_values and it is
1564 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1565 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1566 // figure the smallest and largest values to form the new range.
1569 cross_product_operator::wi_cross_product (irange
&r
, tree type
,
1570 const wide_int
&lh_lb
,
1571 const wide_int
&lh_ub
,
1572 const wide_int
&rh_lb
,
1573 const wide_int
&rh_ub
) const
1575 wide_int cp1
, cp2
, cp3
, cp4
;
1576 // Default to varying.
1577 r
.set_varying (type
);
1579 // Compute the 4 cross operations, bailing if we get an overflow we
1581 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
1583 if (wi::eq_p (lh_lb
, lh_ub
))
1585 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
1587 if (wi::eq_p (rh_lb
, rh_ub
))
1589 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
1591 if (wi::eq_p (lh_lb
, lh_ub
))
1593 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
1597 signop sign
= TYPE_SIGN (type
);
1598 if (wi::gt_p (cp1
, cp2
, sign
))
1599 std::swap (cp1
, cp2
);
1600 if (wi::gt_p (cp3
, cp4
, sign
))
1601 std::swap (cp3
, cp4
);
1603 // Choose min and max from the ordered pairs.
1604 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
1605 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
1606 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
1610 class operator_mult
: public cross_product_operator
1613 virtual void wi_fold (irange
&r
, tree type
,
1614 const wide_int
&lh_lb
,
1615 const wide_int
&lh_ub
,
1616 const wide_int
&rh_lb
,
1617 const wide_int
&rh_ub
) const;
1618 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1619 const wide_int
&w0
, const wide_int
&w1
) const;
1620 virtual bool op1_range (irange
&r
, tree type
,
1623 relation_kind rel ATTRIBUTE_UNUSED
) const;
1624 virtual bool op2_range (irange
&r
, tree type
,
1627 relation_kind rel ATTRIBUTE_UNUSED
) const;
1631 operator_mult::op1_range (irange
&r
, tree type
,
1632 const irange
&lhs
, const irange
&op2
,
1633 relation_kind rel ATTRIBUTE_UNUSED
) const
1637 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1638 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1639 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1640 if (TYPE_OVERFLOW_WRAPS (type
))
1643 if (op2
.singleton_p (&offset
) && !integer_zerop (offset
))
1644 return range_op_handler (TRUNC_DIV_EXPR
, type
)->fold_range (r
, type
,
1650 operator_mult::op2_range (irange
&r
, tree type
,
1651 const irange
&lhs
, const irange
&op1
,
1652 relation_kind rel
) const
1654 return operator_mult::op1_range (r
, type
, lhs
, op1
, rel
);
1658 operator_mult::wi_op_overflows (wide_int
&res
, tree type
,
1659 const wide_int
&w0
, const wide_int
&w1
) const
1661 wi::overflow_type overflow
= wi::OVF_NONE
;
1662 signop sign
= TYPE_SIGN (type
);
1663 res
= wi::mul (w0
, w1
, sign
, &overflow
);
1664 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1666 // For multiplication, the sign of the overflow is given
1667 // by the comparison of the signs of the operands.
1668 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
1669 res
= wi::max_value (w0
.get_precision (), sign
);
1671 res
= wi::min_value (w0
.get_precision (), sign
);
1678 operator_mult::wi_fold (irange
&r
, tree type
,
1679 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1680 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1682 if (TYPE_OVERFLOW_UNDEFINED (type
))
1684 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
1688 // Multiply the ranges when overflow wraps. This is basically fancy
1689 // code so we don't drop to varying with an unsigned
1692 // This test requires 2*prec bits if both operands are signed and
1693 // 2*prec + 2 bits if either is not. Therefore, extend the values
1694 // using the sign of the result to PREC2. From here on out,
1695 // everthing is just signed math no matter what the input types
1698 signop sign
= TYPE_SIGN (type
);
1699 unsigned prec
= TYPE_PRECISION (type
);
1700 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
1701 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
1702 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
1703 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
1704 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
1705 widest2_int size
= sizem1
+ 1;
1707 // Canonicalize the intervals.
1708 if (sign
== UNSIGNED
)
1710 if (wi::ltu_p (size
, min0
+ max0
))
1715 if (wi::ltu_p (size
, min1
+ max1
))
1722 // Sort the 4 products so that min is in prod0 and max is in
1724 widest2_int prod0
= min0
* min1
;
1725 widest2_int prod1
= min0
* max1
;
1726 widest2_int prod2
= max0
* min1
;
1727 widest2_int prod3
= max0
* max1
;
1729 // min0min1 > max0max1
1731 std::swap (prod0
, prod3
);
1733 // min0max1 > max0min1
1735 std::swap (prod1
, prod2
);
1738 std::swap (prod0
, prod1
);
1741 std::swap (prod2
, prod3
);
1744 prod2
= prod3
- prod0
;
1745 if (wi::geu_p (prod2
, sizem1
))
1746 // The range covers all values.
1747 r
.set_varying (type
);
1750 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
1751 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
1752 create_possibly_reversed_range (r
, type
, new_lb
, new_ub
);
1757 class operator_div
: public cross_product_operator
1760 operator_div (enum tree_code c
) { code
= c
; }
1761 virtual void wi_fold (irange
&r
, tree type
,
1762 const wide_int
&lh_lb
,
1763 const wide_int
&lh_ub
,
1764 const wide_int
&rh_lb
,
1765 const wide_int
&rh_ub
) const;
1766 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
1767 const wide_int
&, const wide_int
&) const;
1769 enum tree_code code
;
1773 operator_div::wi_op_overflows (wide_int
&res
, tree type
,
1774 const wide_int
&w0
, const wide_int
&w1
) const
1779 wi::overflow_type overflow
= wi::OVF_NONE
;
1780 signop sign
= TYPE_SIGN (type
);
1784 case EXACT_DIV_EXPR
:
1785 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1786 // operator_exact_divide. No need to handle it here.
1789 case TRUNC_DIV_EXPR
:
1790 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
1792 case FLOOR_DIV_EXPR
:
1793 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
1795 case ROUND_DIV_EXPR
:
1796 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
1799 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
1805 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
1807 // For division, the only case is -INF / -1 = +INF.
1808 res
= wi::max_value (w0
.get_precision (), sign
);
1815 operator_div::wi_fold (irange
&r
, tree type
,
1816 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1817 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1819 const wide_int dividend_min
= lh_lb
;
1820 const wide_int dividend_max
= lh_ub
;
1821 const wide_int divisor_min
= rh_lb
;
1822 const wide_int divisor_max
= rh_ub
;
1823 signop sign
= TYPE_SIGN (type
);
1824 unsigned prec
= TYPE_PRECISION (type
);
1825 wide_int extra_min
, extra_max
;
1827 // If we know we won't divide by zero, just do the division.
1828 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
1830 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1831 divisor_min
, divisor_max
);
1835 // If we're definitely dividing by zero, there's nothing to do.
1836 if (wi_zero_p (type
, divisor_min
, divisor_max
))
1842 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1843 // skip any division by zero.
1845 // First divide by the negative numbers, if any.
1846 if (wi::neg_p (divisor_min
, sign
))
1847 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
1848 divisor_min
, wi::minus_one (prec
));
1852 // Then divide by the non-zero positive numbers, if any.
1853 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
1856 wi_cross_product (tmp
, type
, dividend_min
, dividend_max
,
1857 wi::one (prec
), divisor_max
);
1860 // We shouldn't still have undefined here.
1861 gcc_checking_assert (!r
.undefined_p ());
1864 operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
1865 operator_div
op_floor_div (FLOOR_DIV_EXPR
);
1866 operator_div
op_round_div (ROUND_DIV_EXPR
);
1867 operator_div
op_ceil_div (CEIL_DIV_EXPR
);
1870 class operator_exact_divide
: public operator_div
1873 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR
) { }
1874 virtual bool op1_range (irange
&r
, tree type
,
1877 relation_kind rel ATTRIBUTE_UNUSED
) const;
1882 operator_exact_divide::op1_range (irange
&r
, tree type
,
1885 relation_kind rel ATTRIBUTE_UNUSED
) const
1888 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1889 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1890 // We wont bother trying to enumerate all the in between stuff :-P
1891 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1892 // the time however.
1893 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1894 if (op2
.singleton_p (&offset
)
1895 && !integer_zerop (offset
))
1896 return range_op_handler (MULT_EXPR
, type
)->fold_range (r
, type
, lhs
, op2
);
1901 class operator_lshift
: public cross_product_operator
1904 virtual bool op1_range (irange
&r
, tree type
,
1907 relation_kind rel
= VREL_NONE
) const;
1908 virtual bool fold_range (irange
&r
, tree type
,
1911 relation_kind rel
= VREL_NONE
) const;
1913 virtual void wi_fold (irange
&r
, tree type
,
1914 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1915 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
1916 virtual bool wi_op_overflows (wide_int
&res
,
1919 const wide_int
&) const;
1922 class operator_rshift
: public cross_product_operator
1925 virtual bool fold_range (irange
&r
, tree type
,
1928 relation_kind rel
= VREL_NONE
) const;
1929 virtual void wi_fold (irange
&r
, tree type
,
1930 const wide_int
&lh_lb
,
1931 const wide_int
&lh_ub
,
1932 const wide_int
&rh_lb
,
1933 const wide_int
&rh_ub
) const;
1934 virtual bool wi_op_overflows (wide_int
&res
,
1937 const wide_int
&w1
) const;
1938 virtual bool op1_range (irange
&, tree type
,
1941 relation_kind rel
= VREL_NONE
) const;
1946 operator_lshift::fold_range (irange
&r
, tree type
,
1949 relation_kind rel
) const
1951 int_range_max shift_range
;
1952 if (!get_shift_range (shift_range
, type
, op2
))
1954 if (op2
.undefined_p ())
1957 r
.set_varying (type
);
1961 // Transform left shifts by constants into multiplies.
1962 if (shift_range
.singleton_p ())
1964 unsigned shift
= shift_range
.lower_bound ().to_uhwi ();
1965 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
1966 int_range
<1> mult (type
, tmp
, tmp
);
1968 // Force wrapping multiplication.
1969 bool saved_flag_wrapv
= flag_wrapv
;
1970 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
1972 flag_wrapv_pointer
= 1;
1973 bool b
= op_mult
.fold_range (r
, type
, op1
, mult
);
1974 flag_wrapv
= saved_flag_wrapv
;
1975 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
1979 // Otherwise, invoke the generic fold routine.
1980 return range_operator::fold_range (r
, type
, op1
, shift_range
, rel
);
1984 operator_lshift::wi_fold (irange
&r
, tree type
,
1985 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1986 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1988 signop sign
= TYPE_SIGN (type
);
1989 unsigned prec
= TYPE_PRECISION (type
);
1990 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
1991 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
1992 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1993 // overflow. However, for that to happen, rh.max needs to be zero,
1994 // which means rh is a singleton range of zero, which means we simply return
1995 // [lh_lb, lh_ub] as the range.
1996 if (wi::eq_p (rh_ub
, rh_lb
) && wi::eq_p (rh_ub
, 0))
1998 r
= int_range
<2> (type
, lh_lb
, lh_ub
);
2002 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2003 wide_int complement
= ~(bound
- 1);
2004 wide_int low_bound
, high_bound
;
2005 bool in_bounds
= false;
2007 if (sign
== UNSIGNED
)
2010 high_bound
= complement
;
2011 if (wi::ltu_p (lh_ub
, low_bound
))
2013 // [5, 6] << [1, 2] == [10, 24].
2014 // We're shifting out only zeroes, the value increases
2018 else if (wi::ltu_p (high_bound
, lh_lb
))
2020 // [0xffffff00, 0xffffffff] << [1, 2]
2021 // == [0xfffffc00, 0xfffffffe].
2022 // We're shifting out only ones, the value decreases
2029 // [-1, 1] << [1, 2] == [-4, 4]
2030 low_bound
= complement
;
2032 if (wi::lts_p (lh_ub
, high_bound
)
2033 && wi::lts_p (low_bound
, lh_lb
))
2035 // For non-negative numbers, we're shifting out only zeroes,
2036 // the value increases monotonically. For negative numbers,
2037 // we're shifting out only ones, the value decreases
2044 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2046 r
.set_varying (type
);
2050 operator_lshift::wi_op_overflows (wide_int
&res
, tree type
,
2051 const wide_int
&w0
, const wide_int
&w1
) const
2053 signop sign
= TYPE_SIGN (type
);
2056 // It's unclear from the C standard whether shifts can overflow.
2057 // The following code ignores overflow; perhaps a C standard
2058 // interpretation ruling is needed.
2059 res
= wi::rshift (w0
, -w1
, sign
);
2062 res
= wi::lshift (w0
, w1
);
2067 operator_lshift::op1_range (irange
&r
,
2071 relation_kind rel ATTRIBUTE_UNUSED
) const
2075 if (!lhs
.contains_p (build_zero_cst (type
)))
2076 r
.set_nonzero (type
);
2078 r
.set_varying (type
);
2080 if (op2
.singleton_p (&shift_amount
))
2082 wide_int shift
= wi::to_wide (shift_amount
);
2083 if (wi::lt_p (shift
, 0, SIGNED
))
2085 if (wi::ge_p (shift
, wi::uhwi (TYPE_PRECISION (type
),
2086 TYPE_PRECISION (op2
.type ())),
2095 // Work completely in unsigned mode to start.
2097 int_range_max tmp_range
;
2098 if (TYPE_SIGN (type
) == SIGNED
)
2100 int_range_max tmp
= lhs
;
2101 utype
= unsigned_type_for (type
);
2102 range_cast (tmp
, utype
);
2103 op_rshift
.fold_range (tmp_range
, utype
, tmp
, op2
);
2106 op_rshift
.fold_range (tmp_range
, utype
, lhs
, op2
);
2108 // Start with ranges which can produce the LHS by right shifting the
2109 // result by the shift amount.
2110 // ie [0x08, 0xF0] = op1 << 2 will start with
2111 // [00001000, 11110000] = op1 << 2
2112 // [0x02, 0x4C] aka [00000010, 00111100]
2114 // Then create a range from the LB with the least significant upper bit
2115 // set, to the upper bound with all the bits set.
2116 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2118 // Ideally we do this for each subrange, but just lump them all for now.
2119 unsigned low_bits
= TYPE_PRECISION (utype
)
2120 - TREE_INT_CST_LOW (shift_amount
);
2121 wide_int up_mask
= wi::mask (low_bits
, true, TYPE_PRECISION (utype
));
2122 wide_int new_ub
= wi::bit_or (up_mask
, tmp_range
.upper_bound ());
2123 wide_int new_lb
= wi::set_bit (tmp_range
.lower_bound (), low_bits
);
2124 int_range
<2> fill_range (utype
, new_lb
, new_ub
);
2125 tmp_range
.union_ (fill_range
);
2128 range_cast (tmp_range
, type
);
2130 r
.intersect (tmp_range
);
2134 return !r
.varying_p ();
2138 operator_rshift::op1_range (irange
&r
,
2142 relation_kind rel ATTRIBUTE_UNUSED
) const
2145 if (op2
.singleton_p (&shift
))
2147 // Ignore nonsensical shifts.
2148 unsigned prec
= TYPE_PRECISION (type
);
2149 if (wi::ge_p (wi::to_wide (shift
),
2150 wi::uhwi (prec
, TYPE_PRECISION (TREE_TYPE (shift
))),
2153 if (wi::to_wide (shift
) == 0)
2159 // Folding the original operation may discard some impossible
2160 // ranges from the LHS.
2161 int_range_max lhs_refined
;
2162 op_rshift
.fold_range (lhs_refined
, type
, int_range
<1> (type
), op2
);
2163 lhs_refined
.intersect (lhs
);
2164 if (lhs_refined
.undefined_p ())
2169 int_range_max
shift_range (shift
, shift
);
2170 int_range_max lb
, ub
;
2171 op_lshift
.fold_range (lb
, type
, lhs_refined
, shift_range
);
2173 // 0000 0111 = OP1 >> 3
2175 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2176 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2177 // right hand side (0x07).
2178 tree mask
= fold_build1 (BIT_NOT_EXPR
, type
,
2179 fold_build2 (LSHIFT_EXPR
, type
,
2180 build_minus_one_cst (type
),
2182 int_range_max
mask_range (build_zero_cst (type
), mask
);
2183 op_plus
.fold_range (ub
, type
, lb
, mask_range
);
2186 if (!lhs_refined
.contains_p (build_zero_cst (type
)))
2188 mask_range
.invert ();
2189 r
.intersect (mask_range
);
2197 operator_rshift::wi_op_overflows (wide_int
&res
,
2200 const wide_int
&w1
) const
2202 signop sign
= TYPE_SIGN (type
);
2204 res
= wi::lshift (w0
, -w1
);
2207 // It's unclear from the C standard whether shifts can overflow.
2208 // The following code ignores overflow; perhaps a C standard
2209 // interpretation ruling is needed.
2210 res
= wi::rshift (w0
, w1
, sign
);
2216 operator_rshift::fold_range (irange
&r
, tree type
,
2219 relation_kind rel
) const
2221 int_range_max shift
;
2222 if (!get_shift_range (shift
, type
, op2
))
2224 if (op2
.undefined_p ())
2227 r
.set_varying (type
);
2231 return range_operator::fold_range (r
, type
, op1
, shift
, rel
);
2235 operator_rshift::wi_fold (irange
&r
, tree type
,
2236 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2237 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2239 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2243 class operator_cast
: public range_operator
2246 virtual bool fold_range (irange
&r
, tree type
,
2249 relation_kind rel
= VREL_NONE
) const;
2250 virtual bool op1_range (irange
&r
, tree type
,
2253 relation_kind rel
= VREL_NONE
) const;
2255 bool truncating_cast_p (const irange
&inner
, const irange
&outer
) const;
2256 bool inside_domain_p (const wide_int
&min
, const wide_int
&max
,
2257 const irange
&outer
) const;
2258 void fold_pair (irange
&r
, unsigned index
, const irange
&inner
,
2259 const irange
&outer
) const;
2262 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2265 operator_cast::truncating_cast_p (const irange
&inner
,
2266 const irange
&outer
) const
2268 return TYPE_PRECISION (outer
.type ()) < TYPE_PRECISION (inner
.type ());
2271 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2274 operator_cast::inside_domain_p (const wide_int
&min
,
2275 const wide_int
&max
,
2276 const irange
&range
) const
2278 wide_int domain_min
= wi::to_wide (vrp_val_min (range
.type ()));
2279 wide_int domain_max
= wi::to_wide (vrp_val_max (range
.type ()));
2280 signop domain_sign
= TYPE_SIGN (range
.type ());
2281 return (wi::le_p (min
, domain_max
, domain_sign
)
2282 && wi::le_p (max
, domain_max
, domain_sign
)
2283 && wi::ge_p (min
, domain_min
, domain_sign
)
2284 && wi::ge_p (max
, domain_min
, domain_sign
));
2288 // Helper for fold_range which work on a pair at a time.
2291 operator_cast::fold_pair (irange
&r
, unsigned index
,
2292 const irange
&inner
,
2293 const irange
&outer
) const
2295 tree inner_type
= inner
.type ();
2296 tree outer_type
= outer
.type ();
2297 signop inner_sign
= TYPE_SIGN (inner_type
);
2298 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2300 // check to see if casting from INNER to OUTER is a conversion that
2301 // fits in the resulting OUTER type.
2302 wide_int inner_lb
= inner
.lower_bound (index
);
2303 wide_int inner_ub
= inner
.upper_bound (index
);
2304 if (truncating_cast_p (inner
, outer
))
2306 // We may be able to accomodate a truncating cast if the
2307 // resulting range can be represented in the target type...
2308 if (wi::rshift (wi::sub (inner_ub
, inner_lb
),
2309 wi::uhwi (outer_prec
, TYPE_PRECISION (inner
.type ())),
2312 r
.set_varying (outer_type
);
2316 // ...but we must still verify that the final range fits in the
2317 // domain. This catches -fstrict-enum restrictions where the domain
2318 // range is smaller than what fits in the underlying type.
2319 wide_int min
= wide_int::from (inner_lb
, outer_prec
, inner_sign
);
2320 wide_int max
= wide_int::from (inner_ub
, outer_prec
, inner_sign
);
2321 if (inside_domain_p (min
, max
, outer
))
2322 create_possibly_reversed_range (r
, outer_type
, min
, max
);
2324 r
.set_varying (outer_type
);
2329 operator_cast::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2330 const irange
&inner
,
2331 const irange
&outer
,
2332 relation_kind rel ATTRIBUTE_UNUSED
) const
2334 if (empty_range_varying (r
, type
, inner
, outer
))
2337 gcc_checking_assert (outer
.varying_p ());
2338 gcc_checking_assert (inner
.num_pairs () > 0);
2340 // Avoid a temporary by folding the first pair directly into the result.
2341 fold_pair (r
, 0, inner
, outer
);
2343 // Then process any additonal pairs by unioning with their results.
2344 for (unsigned x
= 1; x
< inner
.num_pairs (); ++x
)
2347 fold_pair (tmp
, x
, inner
, outer
);
2356 operator_cast::op1_range (irange
&r
, tree type
,
2359 relation_kind rel ATTRIBUTE_UNUSED
) const
2361 tree lhs_type
= lhs
.type ();
2362 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
2364 // If we are calculating a pointer, shortcut to what we really care about.
2365 if (POINTER_TYPE_P (type
))
2367 // Conversion from other pointers or a constant (including 0/NULL)
2368 // are straightforward.
2369 if (POINTER_TYPE_P (lhs
.type ())
2370 || (lhs
.singleton_p ()
2371 && TYPE_PRECISION (lhs
.type ()) >= TYPE_PRECISION (type
)))
2374 range_cast (r
, type
);
2378 // If the LHS is not a pointer nor a singleton, then it is
2379 // either VARYING or non-zero.
2380 if (!lhs
.contains_p (build_zero_cst (lhs
.type ())))
2381 r
.set_nonzero (type
);
2383 r
.set_varying (type
);
2389 if (truncating_cast_p (op2
, lhs
))
2391 if (lhs
.varying_p ())
2392 r
.set_varying (type
);
2395 // We want to insert the LHS as an unsigned value since it
2396 // would not trigger the signed bit of the larger type.
2397 int_range_max converted_lhs
= lhs
;
2398 range_cast (converted_lhs
, unsigned_type_for (lhs_type
));
2399 range_cast (converted_lhs
, type
);
2400 // Start by building the positive signed outer range for the type.
2401 wide_int lim
= wi::set_bit_in_zero (TYPE_PRECISION (lhs_type
),
2402 TYPE_PRECISION (type
));
2403 r
= int_range
<1> (type
, lim
, wi::max_value (TYPE_PRECISION (type
),
2405 // For the signed part, we need to simply union the 2 ranges now.
2406 r
.union_ (converted_lhs
);
2408 // Create maximal negative number outside of LHS bits.
2409 lim
= wi::mask (TYPE_PRECISION (lhs_type
), true,
2410 TYPE_PRECISION (type
));
2411 // Add this to the unsigned LHS range(s).
2412 int_range_max
lim_range (type
, lim
, lim
);
2413 int_range_max lhs_neg
;
2414 range_op_handler (PLUS_EXPR
, type
)->fold_range (lhs_neg
,
2418 // lhs_neg now has all the negative versions of the LHS.
2419 // Now union in all the values from SIGNED MIN (0x80000) to
2420 // lim-1 in order to fill in all the ranges with the upper
2423 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2424 // we don't need to create a range from min to lim-1
2425 // calculate neg range traps trying to create [lim, lim - 1].
2426 wide_int min_val
= wi::min_value (TYPE_PRECISION (type
), SIGNED
);
2429 int_range_max
neg (type
,
2430 wi::min_value (TYPE_PRECISION (type
),
2433 lhs_neg
.union_ (neg
);
2435 // And finally, munge the signed and unsigned portions.
2438 // And intersect with any known value passed in the extra operand.
2444 if (TYPE_PRECISION (lhs_type
) == TYPE_PRECISION (type
))
2448 // The cast is not truncating, and the range is restricted to
2449 // the range of the RHS by this assignment.
2451 // Cast the range of the RHS to the type of the LHS.
2452 fold_range (tmp
, lhs_type
, int_range
<1> (type
), int_range
<1> (lhs_type
));
2453 // Intersect this with the LHS range will produce the range,
2454 // which will be cast to the RHS type before returning.
2455 tmp
.intersect (lhs
);
2458 // Cast the calculated range to the type of the RHS.
2459 fold_range (r
, type
, tmp
, int_range
<1> (type
));
2464 class operator_logical_and
: public range_operator
2467 virtual bool fold_range (irange
&r
, tree type
,
2470 relation_kind rel
= VREL_NONE
) const;
2471 virtual bool op1_range (irange
&r
, tree type
,
2474 relation_kind rel
= VREL_NONE
) const;
2475 virtual bool op2_range (irange
&r
, tree type
,
2478 relation_kind rel
= VREL_NONE
) const;
2483 operator_logical_and::fold_range (irange
&r
, tree type
,
2486 relation_kind rel ATTRIBUTE_UNUSED
) const
2488 if (empty_range_varying (r
, type
, lh
, rh
))
2491 // 0 && anything is 0.
2492 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
2493 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
2494 r
= range_false (type
);
2495 else if (lh
.contains_p (build_zero_cst (lh
.type ()))
2496 || rh
.contains_p (build_zero_cst (rh
.type ())))
2497 // To reach this point, there must be a logical 1 on each side, and
2498 // the only remaining question is whether there is a zero or not.
2499 r
= range_true_and_false (type
);
2501 r
= range_true (type
);
2506 operator_logical_and::op1_range (irange
&r
, tree type
,
2508 const irange
&op2 ATTRIBUTE_UNUSED
,
2509 relation_kind rel ATTRIBUTE_UNUSED
) const
2511 switch (get_bool_state (r
, lhs
, type
))
2514 // A true result means both sides of the AND must be true.
2515 r
= range_true (type
);
2518 // Any other result means only one side has to be false, the
2519 // other side can be anything. So we cannott be sure of any
2521 r
= range_true_and_false (type
);
2528 operator_logical_and::op2_range (irange
&r
, tree type
,
2531 relation_kind rel ATTRIBUTE_UNUSED
) const
2533 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
2537 class operator_bitwise_and
: public range_operator
2540 virtual bool fold_range (irange
&r
, tree type
,
2543 relation_kind rel
= VREL_NONE
) const;
2544 virtual bool op1_range (irange
&r
, tree type
,
2547 relation_kind rel
= VREL_NONE
) const;
2548 virtual bool op2_range (irange
&r
, tree type
,
2551 relation_kind rel
= VREL_NONE
) const;
2552 virtual void wi_fold (irange
&r
, tree type
,
2553 const wide_int
&lh_lb
,
2554 const wide_int
&lh_ub
,
2555 const wide_int
&rh_lb
,
2556 const wide_int
&rh_ub
) const;
2558 void simple_op1_range_solver (irange
&r
, tree type
,
2560 const irange
&op2
) const;
2561 void remove_impossible_ranges (irange
&r
, const irange
&rh
) const;
2565 unsigned_singleton_p (const irange
&op
)
2568 if (op
.singleton_p (&mask
))
2570 wide_int x
= wi::to_wide (mask
);
2571 return wi::ge_p (x
, 0, TYPE_SIGN (op
.type ()));
2576 // Remove any ranges from R that are known to be impossible when an
2577 // range is ANDed with MASK.
2580 operator_bitwise_and::remove_impossible_ranges (irange
&r
,
2581 const irange
&rmask
) const
2583 if (r
.undefined_p () || !unsigned_singleton_p (rmask
))
2586 wide_int mask
= rmask
.lower_bound ();
2587 tree type
= r
.type ();
2588 int prec
= TYPE_PRECISION (type
);
2589 int leading_zeros
= wi::clz (mask
);
2590 int_range_max impossible_ranges
;
2592 /* We know that starting at the most significant bit, any 0 in the
2593 mask means the resulting range cannot contain a 1 in that same
2594 position. This means the following ranges are impossible:
2598 01xx xxxx [0100 0000, 0111 1111]
2599 001x xxxx [0010 0000, 0011 1111]
2600 0000 01xx [0000 0100, 0000 0111]
2601 0000 0001 [0000 0001, 0000 0001]
2603 wide_int one
= wi::one (prec
);
2604 for (int i
= 0; i
< prec
- leading_zeros
- 1; ++i
)
2605 if (wi::bit_and (mask
, wi::lshift (one
, wi::uhwi (i
, prec
))) == 0)
2607 tree lb
= fold_build2 (LSHIFT_EXPR
, type
,
2608 build_one_cst (type
),
2609 build_int_cst (type
, i
));
2610 tree ub_left
= fold_build1 (BIT_NOT_EXPR
, type
,
2611 fold_build2 (LSHIFT_EXPR
, type
,
2612 build_minus_one_cst (type
),
2613 build_int_cst (type
, i
)));
2614 tree ub_right
= fold_build2 (LSHIFT_EXPR
, type
,
2615 build_one_cst (type
),
2616 build_int_cst (type
, i
));
2617 tree ub
= fold_build2 (BIT_IOR_EXPR
, type
, ub_left
, ub_right
);
2618 impossible_ranges
.union_ (int_range
<1> (lb
, ub
));
2620 if (!impossible_ranges
.undefined_p ())
2622 impossible_ranges
.invert ();
2623 r
.intersect (impossible_ranges
);
2628 operator_bitwise_and::fold_range (irange
&r
, tree type
,
2631 relation_kind rel ATTRIBUTE_UNUSED
) const
2633 if (range_operator::fold_range (r
, type
, lh
, rh
))
2635 // FIXME: This is temporarily disabled because, though it
2636 // generates better ranges, it's noticeably slower for evrp.
2637 // remove_impossible_ranges (r, rh);
2644 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2645 // possible. Basically, see if we can optimize:
2649 // [LB op Z, UB op Z]
2651 // If the optimization was successful, accumulate the range in R and
2655 wi_optimize_and_or (irange
&r
,
2656 enum tree_code code
,
2658 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2659 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
2661 // Calculate the singleton mask among the ranges, if any.
2662 wide_int lower_bound
, upper_bound
, mask
;
2663 if (wi::eq_p (rh_lb
, rh_ub
))
2666 lower_bound
= lh_lb
;
2667 upper_bound
= lh_ub
;
2669 else if (wi::eq_p (lh_lb
, lh_ub
))
2672 lower_bound
= rh_lb
;
2673 upper_bound
= rh_ub
;
2678 // If Z is a constant which (for op | its bitwise not) has n
2679 // consecutive least significant bits cleared followed by m 1
2680 // consecutive bits set immediately above it and either
2681 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2683 // The least significant n bits of all the values in the range are
2684 // cleared or set, the m bits above it are preserved and any bits
2685 // above these are required to be the same for all values in the
2689 if (code
== BIT_IOR_EXPR
)
2691 if (wi::eq_p (w
, 0))
2692 n
= w
.get_precision ();
2696 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2697 if (wi::eq_p (w
, 0))
2698 m
= w
.get_precision () - n
;
2700 m
= wi::ctz (w
) - n
;
2702 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2703 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
2706 wide_int res_lb
, res_ub
;
2707 if (code
== BIT_AND_EXPR
)
2709 res_lb
= wi::bit_and (lower_bound
, mask
);
2710 res_ub
= wi::bit_and (upper_bound
, mask
);
2712 else if (code
== BIT_IOR_EXPR
)
2714 res_lb
= wi::bit_or (lower_bound
, mask
);
2715 res_ub
= wi::bit_or (upper_bound
, mask
);
2719 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
2721 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2722 if (code
== BIT_IOR_EXPR
&& wi::ne_p (mask
, 0))
2725 tmp
.set_nonzero (type
);
2731 // For range [LB, UB] compute two wide_int bit masks.
2733 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2734 // for all numbers in the range the bit is 0, otherwise it might be 0
2737 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2738 // for all numbers in the range the bit is 1, otherwise it might be 0
2742 wi_set_zero_nonzero_bits (tree type
,
2743 const wide_int
&lb
, const wide_int
&ub
,
2744 wide_int
&maybe_nonzero
,
2745 wide_int
&mustbe_nonzero
)
2747 signop sign
= TYPE_SIGN (type
);
2749 if (wi::eq_p (lb
, ub
))
2750 maybe_nonzero
= mustbe_nonzero
= lb
;
2751 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
2753 wide_int xor_mask
= lb
^ ub
;
2754 maybe_nonzero
= lb
| ub
;
2755 mustbe_nonzero
= lb
& ub
;
2758 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2759 maybe_nonzero
.get_precision ());
2760 maybe_nonzero
= maybe_nonzero
| mask
;
2761 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
2766 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
2767 mustbe_nonzero
= wi::zero (lb
.get_precision ());
2772 operator_bitwise_and::wi_fold (irange
&r
, tree type
,
2773 const wide_int
&lh_lb
,
2774 const wide_int
&lh_ub
,
2775 const wide_int
&rh_lb
,
2776 const wide_int
&rh_ub
) const
2778 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
2781 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
2782 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
2783 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
2784 maybe_nonzero_lh
, mustbe_nonzero_lh
);
2785 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
2786 maybe_nonzero_rh
, mustbe_nonzero_rh
);
2788 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
2789 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
2790 signop sign
= TYPE_SIGN (type
);
2791 unsigned prec
= TYPE_PRECISION (type
);
2792 // If both input ranges contain only negative values, we can
2793 // truncate the result range maximum to the minimum of the
2794 // input range maxima.
2795 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
2797 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2798 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2800 // If either input range contains only non-negative values
2801 // we can truncate the result range maximum to the respective
2802 // maximum of the input range.
2803 if (wi::ge_p (lh_lb
, 0, sign
))
2804 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
2805 if (wi::ge_p (rh_lb
, 0, sign
))
2806 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
2807 // PR68217: In case of signed & sign-bit-CST should
2808 // result in [-INF, 0] instead of [-INF, INF].
2809 if (wi::gt_p (new_lb
, new_ub
, sign
))
2811 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
2813 && ((wi::eq_p (lh_lb
, lh_ub
)
2814 && !wi::cmps (lh_lb
, sign_bit
))
2815 || (wi::eq_p (rh_lb
, rh_ub
)
2816 && !wi::cmps (rh_lb
, sign_bit
))))
2818 new_lb
= wi::min_value (prec
, sign
);
2819 new_ub
= wi::zero (prec
);
2822 // If the limits got swapped around, return varying.
2823 if (wi::gt_p (new_lb
, new_ub
,sign
))
2824 r
.set_varying (type
);
2826 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2830 set_nonzero_range_from_mask (irange
&r
, tree type
, const irange
&lhs
)
2832 if (!lhs
.contains_p (build_zero_cst (type
)))
2833 r
= range_nonzero (type
);
2835 r
.set_varying (type
);
2838 // This was shamelessly stolen from register_edge_assert_for_2 and
2839 // adjusted to work with iranges.
2842 operator_bitwise_and::simple_op1_range_solver (irange
&r
, tree type
,
2844 const irange
&op2
) const
2846 if (!op2
.singleton_p ())
2848 set_nonzero_range_from_mask (r
, type
, lhs
);
2851 unsigned int nprec
= TYPE_PRECISION (type
);
2852 wide_int cst2v
= op2
.lower_bound ();
2853 bool cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (type
));
2856 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
2858 sgnbit
= wi::zero (nprec
);
2860 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2862 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2863 // maximum unsigned value is ~0. For signed comparison, if CST2
2864 // doesn't have the most significant bit set, handle it similarly. If
2865 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2866 wide_int valv
= lhs
.lower_bound ();
2867 wide_int minv
= valv
& cst2v
, maxv
;
2868 bool we_know_nothing
= false;
2871 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2872 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2875 // If we can't determine anything on this bound, fall
2876 // through and conservatively solve for the other end point.
2877 we_know_nothing
= true;
2880 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
2881 if (we_know_nothing
)
2882 r
.set_varying (type
);
2884 r
= int_range
<1> (type
, minv
, maxv
);
2886 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2888 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2889 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2891 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2893 // For signed comparison, if CST2 doesn't have most significant bit
2894 // set, handle it similarly. If CST2 has MSB set, the maximum is
2895 // the same and minimum is INT_MIN.
2896 valv
= lhs
.upper_bound ();
2897 minv
= valv
& cst2v
;
2902 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
2905 // If we couldn't determine anything on either bound, return
2907 if (we_know_nothing
)
2915 int_range
<1> upper_bits (type
, minv
, maxv
);
2916 r
.intersect (upper_bits
);
2920 operator_bitwise_and::op1_range (irange
&r
, tree type
,
2923 relation_kind rel ATTRIBUTE_UNUSED
) const
2925 if (types_compatible_p (type
, boolean_type_node
))
2926 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
2929 for (unsigned i
= 0; i
< lhs
.num_pairs (); ++i
)
2931 int_range_max
chunk (lhs
.type (),
2932 lhs
.lower_bound (i
),
2933 lhs
.upper_bound (i
));
2935 simple_op1_range_solver (res
, type
, chunk
, op2
);
2938 if (r
.undefined_p ())
2939 set_nonzero_range_from_mask (r
, type
, lhs
);
2944 operator_bitwise_and::op2_range (irange
&r
, tree type
,
2947 relation_kind rel ATTRIBUTE_UNUSED
) const
2949 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
2953 class operator_logical_or
: public range_operator
2956 virtual bool fold_range (irange
&r
, tree type
,
2959 relation_kind rel
= VREL_NONE
) const;
2960 virtual bool op1_range (irange
&r
, tree type
,
2963 relation_kind rel
= VREL_NONE
) const;
2964 virtual bool op2_range (irange
&r
, tree type
,
2967 relation_kind rel
= VREL_NONE
) const;
2971 operator_logical_or::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2974 relation_kind rel ATTRIBUTE_UNUSED
) const
2976 if (empty_range_varying (r
, type
, lh
, rh
))
2985 operator_logical_or::op1_range (irange
&r
, tree type
,
2987 const irange
&op2 ATTRIBUTE_UNUSED
,
2988 relation_kind rel ATTRIBUTE_UNUSED
) const
2990 switch (get_bool_state (r
, lhs
, type
))
2993 // A false result means both sides of the OR must be false.
2994 r
= range_false (type
);
2997 // Any other result means only one side has to be true, the
2998 // other side can be anything. so we can't be sure of any result
3000 r
= range_true_and_false (type
);
3007 operator_logical_or::op2_range (irange
&r
, tree type
,
3010 relation_kind rel ATTRIBUTE_UNUSED
) const
3012 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
3016 class operator_bitwise_or
: public range_operator
3019 virtual bool op1_range (irange
&r
, tree type
,
3022 relation_kind rel
= VREL_NONE
) const;
3023 virtual bool op2_range (irange
&r
, tree type
,
3026 relation_kind rel
= VREL_NONE
) const;
3027 virtual void wi_fold (irange
&r
, tree type
,
3028 const wide_int
&lh_lb
,
3029 const wide_int
&lh_ub
,
3030 const wide_int
&rh_lb
,
3031 const wide_int
&rh_ub
) const;
3035 operator_bitwise_or::wi_fold (irange
&r
, tree type
,
3036 const wide_int
&lh_lb
,
3037 const wide_int
&lh_ub
,
3038 const wide_int
&rh_lb
,
3039 const wide_int
&rh_ub
) const
3041 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3044 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3045 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3046 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3047 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3048 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3049 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3050 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
3051 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
3052 signop sign
= TYPE_SIGN (type
);
3053 // If the input ranges contain only positive values we can
3054 // truncate the minimum of the result range to the maximum
3055 // of the input range minima.
3056 if (wi::ge_p (lh_lb
, 0, sign
)
3057 && wi::ge_p (rh_lb
, 0, sign
))
3059 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3060 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3062 // If either input range contains only negative values
3063 // we can truncate the minimum of the result range to the
3064 // respective minimum range.
3065 if (wi::lt_p (lh_ub
, 0, sign
))
3066 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3067 if (wi::lt_p (rh_ub
, 0, sign
))
3068 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3069 // If the limits got swapped around, return a conservative range.
3070 if (wi::gt_p (new_lb
, new_ub
, sign
))
3072 // Make sure that nonzero|X is nonzero.
3073 if (wi::gt_p (lh_lb
, 0, sign
)
3074 || wi::gt_p (rh_lb
, 0, sign
)
3075 || wi::lt_p (lh_ub
, 0, sign
)
3076 || wi::lt_p (rh_ub
, 0, sign
))
3077 r
.set_nonzero (type
);
3079 r
.set_varying (type
);
3082 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3086 operator_bitwise_or::op1_range (irange
&r
, tree type
,
3089 relation_kind rel ATTRIBUTE_UNUSED
) const
3091 // If this is really a logical wi_fold, call that.
3092 if (types_compatible_p (type
, boolean_type_node
))
3093 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
3097 tree zero
= build_zero_cst (type
);
3098 r
= int_range
<1> (zero
, zero
);
3101 r
.set_varying (type
);
3106 operator_bitwise_or::op2_range (irange
&r
, tree type
,
3109 relation_kind rel ATTRIBUTE_UNUSED
) const
3111 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
3115 class operator_bitwise_xor
: public range_operator
3118 virtual void wi_fold (irange
&r
, tree type
,
3119 const wide_int
&lh_lb
,
3120 const wide_int
&lh_ub
,
3121 const wide_int
&rh_lb
,
3122 const wide_int
&rh_ub
) const;
3123 virtual bool op1_range (irange
&r
, tree type
,
3126 relation_kind rel
= VREL_NONE
) const;
3127 virtual bool op2_range (irange
&r
, tree type
,
3130 relation_kind rel
= VREL_NONE
) const;
3131 virtual bool op1_op2_relation_effect (irange
&lhs_range
,
3133 const irange
&op1_range
,
3134 const irange
&op2_range
,
3135 relation_kind rel
) const;
3139 operator_bitwise_xor::wi_fold (irange
&r
, tree type
,
3140 const wide_int
&lh_lb
,
3141 const wide_int
&lh_ub
,
3142 const wide_int
&rh_lb
,
3143 const wide_int
&rh_ub
) const
3145 signop sign
= TYPE_SIGN (type
);
3146 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3147 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3148 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3149 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3150 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3151 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3153 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
3154 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
3155 wide_int result_one_bits
3156 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
3157 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
3158 wide_int new_ub
= ~result_zero_bits
;
3159 wide_int new_lb
= result_one_bits
;
3161 // If the range has all positive or all negative values, the result
3162 // is better than VARYING.
3163 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
3164 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3166 r
.set_varying (type
);
3170 operator_bitwise_xor::op1_op2_relation_effect (irange
&lhs_range
,
3174 relation_kind rel
) const
3176 if (rel
== VREL_NONE
)
3179 int_range
<2> rel_range
;
3184 rel_range
.set_zero (type
);
3187 rel_range
.set_nonzero (type
);
3193 lhs_range
.intersect (rel_range
);
3198 operator_bitwise_xor::op1_range (irange
&r
, tree type
,
3201 relation_kind rel ATTRIBUTE_UNUSED
) const
3203 if (lhs
.undefined_p () || lhs
.varying_p ())
3208 if (types_compatible_p (type
, boolean_type_node
))
3210 switch (get_bool_state (r
, lhs
, type
))
3213 if (op2
.varying_p ())
3214 r
.set_varying (type
);
3215 else if (op2
.zero_p ())
3216 r
= range_true (type
);
3218 r
= range_false (type
);
3228 r
.set_varying (type
);
3233 operator_bitwise_xor::op2_range (irange
&r
, tree type
,
3236 relation_kind rel ATTRIBUTE_UNUSED
) const
3238 return operator_bitwise_xor::op1_range (r
, type
, lhs
, op1
);
3241 class operator_trunc_mod
: public range_operator
3244 virtual void wi_fold (irange
&r
, tree type
,
3245 const wide_int
&lh_lb
,
3246 const wide_int
&lh_ub
,
3247 const wide_int
&rh_lb
,
3248 const wide_int
&rh_ub
) const;
3249 virtual bool op1_range (irange
&r
, tree type
,
3252 relation_kind rel ATTRIBUTE_UNUSED
) const;
3253 virtual bool op2_range (irange
&r
, tree type
,
3256 relation_kind rel ATTRIBUTE_UNUSED
) const;
3260 operator_trunc_mod::wi_fold (irange
&r
, tree type
,
3261 const wide_int
&lh_lb
,
3262 const wide_int
&lh_ub
,
3263 const wide_int
&rh_lb
,
3264 const wide_int
&rh_ub
) const
3266 wide_int new_lb
, new_ub
, tmp
;
3267 signop sign
= TYPE_SIGN (type
);
3268 unsigned prec
= TYPE_PRECISION (type
);
3270 // Mod 0 is undefined.
3271 if (wi_zero_p (type
, rh_lb
, rh_ub
))
3277 // Check for constant and try to fold.
3278 if (lh_lb
== lh_ub
&& rh_lb
== rh_ub
)
3280 wi::overflow_type ov
= wi::OVF_NONE
;
3281 tmp
= wi::mod_trunc (lh_lb
, rh_lb
, sign
, &ov
);
3282 if (ov
== wi::OVF_NONE
)
3284 r
= int_range
<2> (type
, tmp
, tmp
);
3289 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3294 new_ub
= wi::smax (new_ub
, tmp
);
3297 if (sign
== UNSIGNED
)
3298 new_lb
= wi::zero (prec
);
3303 if (wi::gts_p (tmp
, 0))
3304 tmp
= wi::zero (prec
);
3305 new_lb
= wi::smax (new_lb
, tmp
);
3308 if (sign
== SIGNED
&& wi::neg_p (tmp
))
3309 tmp
= wi::zero (prec
);
3310 new_ub
= wi::min (new_ub
, tmp
, sign
);
3312 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3316 operator_trunc_mod::op1_range (irange
&r
, tree type
,
3319 relation_kind rel ATTRIBUTE_UNUSED
) const
3322 signop sign
= TYPE_SIGN (type
);
3323 unsigned prec
= TYPE_PRECISION (type
);
3324 // (a % b) >= x && x > 0 , then a >= x.
3325 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3327 r
= value_range (type
, lhs
.lower_bound (), wi::max_value (prec
, sign
));
3330 // (a % b) <= x && x < 0 , then a <= x.
3331 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3333 r
= value_range (type
, wi::min_value (prec
, sign
), lhs
.upper_bound ());
3340 operator_trunc_mod::op2_range (irange
&r
, tree type
,
3343 relation_kind rel ATTRIBUTE_UNUSED
) const
3346 signop sign
= TYPE_SIGN (type
);
3347 unsigned prec
= TYPE_PRECISION (type
);
3348 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3349 // or b > x for unsigned.
3350 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3353 r
= value_range (type
, wi::neg (lhs
.lower_bound ()),
3354 lhs
.lower_bound (), VR_ANTI_RANGE
);
3355 else if (wi::lt_p (lhs
.lower_bound (), wi::max_value (prec
, sign
),
3357 r
= value_range (type
, lhs
.lower_bound () + 1,
3358 wi::max_value (prec
, sign
));
3363 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3364 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3366 if (wi::gt_p (lhs
.upper_bound (), wi::min_value (prec
, sign
), sign
))
3367 r
= value_range (type
, lhs
.upper_bound (),
3368 wi::neg (lhs
.upper_bound ()), VR_ANTI_RANGE
);
3377 class operator_logical_not
: public range_operator
3380 virtual bool fold_range (irange
&r
, tree type
,
3383 relation_kind rel
= VREL_NONE
) const;
3384 virtual bool op1_range (irange
&r
, tree type
,
3387 relation_kind rel
= VREL_NONE
) const;
3390 // Folding a logical NOT, oddly enough, involves doing nothing on the
3391 // forward pass through. During the initial walk backwards, the
3392 // logical NOT reversed the desired outcome on the way back, so on the
3393 // way forward all we do is pass the range forward.
3398 // to determine the TRUE branch, walking backward
3399 // if (b_3) if ([1,1])
3400 // b_3 = !b_2 [1,1] = ![0,0]
3401 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3402 // which is the result we are looking for.. so.. pass it through.
3405 operator_logical_not::fold_range (irange
&r
, tree type
,
3407 const irange
&rh ATTRIBUTE_UNUSED
,
3408 relation_kind rel ATTRIBUTE_UNUSED
) const
3410 if (empty_range_varying (r
, type
, lh
, rh
))
3414 if (!lh
.varying_p () && !lh
.undefined_p ())
3421 operator_logical_not::op1_range (irange
&r
,
3425 relation_kind rel ATTRIBUTE_UNUSED
) const
3427 // Logical NOT is involutary...do it again.
3428 return fold_range (r
, type
, lhs
, op2
);
3432 class operator_bitwise_not
: public range_operator
3435 virtual bool fold_range (irange
&r
, tree type
,
3438 relation_kind rel
= VREL_NONE
) const;
3439 virtual bool op1_range (irange
&r
, tree type
,
3442 relation_kind rel
= VREL_NONE
) const;
3446 operator_bitwise_not::fold_range (irange
&r
, tree type
,
3449 relation_kind rel ATTRIBUTE_UNUSED
) const
3451 if (empty_range_varying (r
, type
, lh
, rh
))
3454 if (types_compatible_p (type
, boolean_type_node
))
3455 return op_logical_not
.fold_range (r
, type
, lh
, rh
);
3457 // ~X is simply -1 - X.
3458 int_range
<1> minusone (type
, wi::minus_one (TYPE_PRECISION (type
)),
3459 wi::minus_one (TYPE_PRECISION (type
)));
3460 return range_op_handler (MINUS_EXPR
, type
)->fold_range (r
, type
, minusone
,
3465 operator_bitwise_not::op1_range (irange
&r
, tree type
,
3468 relation_kind rel ATTRIBUTE_UNUSED
) const
3470 if (types_compatible_p (type
, boolean_type_node
))
3471 return op_logical_not
.op1_range (r
, type
, lhs
, op2
);
3473 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3474 return fold_range (r
, type
, lhs
, op2
);
3478 class operator_cst
: public range_operator
3481 virtual bool fold_range (irange
&r
, tree type
,
3484 relation_kind rel
= VREL_NONE
) const;
3488 operator_cst::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3490 const irange
&rh ATTRIBUTE_UNUSED
,
3491 relation_kind rel ATTRIBUTE_UNUSED
) const
3498 class operator_identity
: public range_operator
3501 virtual bool fold_range (irange
&r
, tree type
,
3504 relation_kind rel
= VREL_NONE
) const;
3505 virtual bool op1_range (irange
&r
, tree type
,
3508 relation_kind rel
= VREL_NONE
) const;
3509 virtual enum tree_code
lhs_op1_relation (const irange
&lhs
,
3511 const irange
&op2
) const;
3514 // Determine if there is a relationship between LHS and OP1.
3517 operator_identity::lhs_op1_relation (const irange
&lhs
,
3518 const irange
&op1 ATTRIBUTE_UNUSED
,
3519 const irange
&op2 ATTRIBUTE_UNUSED
) const
3521 if (lhs
.undefined_p ())
3523 // Simply a copy, so they are equivalent.
3528 operator_identity::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3530 const irange
&rh ATTRIBUTE_UNUSED
,
3531 relation_kind rel ATTRIBUTE_UNUSED
) const
3538 operator_identity::op1_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3540 const irange
&op2 ATTRIBUTE_UNUSED
,
3541 relation_kind rel ATTRIBUTE_UNUSED
) const
3548 class operator_unknown
: public range_operator
3551 virtual bool fold_range (irange
&r
, tree type
,
3554 relation_kind rel
= VREL_NONE
) const;
3558 operator_unknown::fold_range (irange
&r
, tree type
,
3559 const irange
&lh ATTRIBUTE_UNUSED
,
3560 const irange
&rh ATTRIBUTE_UNUSED
,
3561 relation_kind rel ATTRIBUTE_UNUSED
) const
3563 r
.set_varying (type
);
3568 class operator_abs
: public range_operator
3571 virtual void wi_fold (irange
&r
, tree type
,
3572 const wide_int
&lh_lb
,
3573 const wide_int
&lh_ub
,
3574 const wide_int
&rh_lb
,
3575 const wide_int
&rh_ub
) const;
3576 virtual bool op1_range (irange
&r
, tree type
,
3579 relation_kind rel ATTRIBUTE_UNUSED
) const;
3583 operator_abs::wi_fold (irange
&r
, tree type
,
3584 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3585 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3586 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3589 signop sign
= TYPE_SIGN (type
);
3590 unsigned prec
= TYPE_PRECISION (type
);
3592 // Pass through LH for the easy cases.
3593 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
3595 r
= int_range
<1> (type
, lh_lb
, lh_ub
);
3599 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3601 wide_int min_value
= wi::min_value (prec
, sign
);
3602 wide_int max_value
= wi::max_value (prec
, sign
);
3603 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
3605 r
.set_varying (type
);
3609 // ABS_EXPR may flip the range around, if the original range
3610 // included negative values.
3611 if (wi::eq_p (lh_lb
, min_value
))
3613 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3614 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3615 if (wi::eq_p (lh_ub
, min_value
))
3617 r
= int_range
<1> (type
, min_value
, min_value
);
3623 min
= wi::abs (lh_lb
);
3625 if (wi::eq_p (lh_ub
, min_value
))
3628 max
= wi::abs (lh_ub
);
3630 // If the range contains zero then we know that the minimum value in the
3631 // range will be zero.
3632 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
3634 if (wi::gt_p (min
, max
, sign
))
3636 min
= wi::zero (prec
);
3640 // If the range was reversed, swap MIN and MAX.
3641 if (wi::gt_p (min
, max
, sign
))
3642 std::swap (min
, max
);
3645 // If the new range has its limits swapped around (MIN > MAX), then
3646 // the operation caused one of them to wrap around. The only thing
3647 // we know is that the result is positive.
3648 if (wi::gt_p (min
, max
, sign
))
3650 min
= wi::zero (prec
);
3653 r
= int_range
<1> (type
, min
, max
);
3657 operator_abs::op1_range (irange
&r
, tree type
,
3660 relation_kind rel ATTRIBUTE_UNUSED
) const
3662 if (empty_range_varying (r
, type
, lhs
, op2
))
3664 if (TYPE_UNSIGNED (type
))
3669 // Start with the positives because negatives are an impossible result.
3670 int_range_max positives
= range_positives (type
);
3671 positives
.intersect (lhs
);
3673 // Then add the negative of each pair:
3674 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3675 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
3676 r
.union_ (int_range
<1> (type
,
3677 -positives
.upper_bound (i
),
3678 -positives
.lower_bound (i
)));
3679 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3680 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3681 wide_int min_value
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
3682 wide_int lb
= lhs
.lower_bound ();
3683 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lb
, min_value
))
3684 r
.union_ (int_range
<2> (type
, lb
, lb
));
3689 class operator_absu
: public range_operator
3692 virtual void wi_fold (irange
&r
, tree type
,
3693 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3694 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3698 operator_absu::wi_fold (irange
&r
, tree type
,
3699 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3700 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3701 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3703 wide_int new_lb
, new_ub
;
3705 // Pass through VR0 the easy cases.
3706 if (wi::ges_p (lh_lb
, 0))
3713 new_lb
= wi::abs (lh_lb
);
3714 new_ub
= wi::abs (lh_ub
);
3716 // If the range contains zero then we know that the minimum
3717 // value in the range will be zero.
3718 if (wi::ges_p (lh_ub
, 0))
3720 if (wi::gtu_p (new_lb
, new_ub
))
3722 new_lb
= wi::zero (TYPE_PRECISION (type
));
3725 std::swap (new_lb
, new_ub
);
3728 gcc_checking_assert (TYPE_UNSIGNED (type
));
3729 r
= int_range
<1> (type
, new_lb
, new_ub
);
3733 class operator_negate
: public range_operator
3736 virtual bool fold_range (irange
&r
, tree type
,
3739 relation_kind rel
= VREL_NONE
) const;
3740 virtual bool op1_range (irange
&r
, tree type
,
3743 relation_kind rel
= VREL_NONE
) const;
3747 operator_negate::fold_range (irange
&r
, tree type
,
3750 relation_kind rel ATTRIBUTE_UNUSED
) const
3752 if (empty_range_varying (r
, type
, lh
, rh
))
3754 // -X is simply 0 - X.
3755 return range_op_handler (MINUS_EXPR
, type
)->fold_range (r
, type
,
3761 operator_negate::op1_range (irange
&r
, tree type
,
3764 relation_kind rel ATTRIBUTE_UNUSED
) const
3766 // NEGATE is involutory.
3767 return fold_range (r
, type
, lhs
, op2
);
3771 class operator_addr_expr
: public range_operator
3774 virtual bool fold_range (irange
&r
, tree type
,
3777 relation_kind rel
= VREL_NONE
) const;
3778 virtual bool op1_range (irange
&r
, tree type
,
3781 relation_kind rel
= VREL_NONE
) const;
3785 operator_addr_expr::fold_range (irange
&r
, tree type
,
3788 relation_kind rel ATTRIBUTE_UNUSED
) const
3790 if (empty_range_varying (r
, type
, lh
, rh
))
3793 // Return a non-null pointer of the LHS type (passed in op2).
3795 r
= range_zero (type
);
3796 else if (!lh
.contains_p (build_zero_cst (lh
.type ())))
3797 r
= range_nonzero (type
);
3799 r
.set_varying (type
);
3804 operator_addr_expr::op1_range (irange
&r
, tree type
,
3807 relation_kind rel ATTRIBUTE_UNUSED
) const
3809 return operator_addr_expr::fold_range (r
, type
, lhs
, op2
);
3813 class pointer_plus_operator
: public range_operator
3816 virtual void wi_fold (irange
&r
, tree type
,
3817 const wide_int
&lh_lb
,
3818 const wide_int
&lh_ub
,
3819 const wide_int
&rh_lb
,
3820 const wide_int
&rh_ub
) const;
3824 pointer_plus_operator::wi_fold (irange
&r
, tree type
,
3825 const wide_int
&lh_lb
,
3826 const wide_int
&lh_ub
,
3827 const wide_int
&rh_lb
,
3828 const wide_int
&rh_ub
) const
3830 // Check for [0,0] + const, and simply return the const.
3831 if (lh_lb
== 0 && lh_ub
== 0 && rh_lb
== rh_ub
)
3833 tree val
= wide_int_to_tree (type
, rh_lb
);
3838 // For pointer types, we are really only interested in asserting
3839 // whether the expression evaluates to non-NULL.
3841 // With -fno-delete-null-pointer-checks we need to be more
3842 // conservative. As some object might reside at address 0,
3843 // then some offset could be added to it and the same offset
3844 // subtracted again and the result would be NULL.
3846 // static int a[12]; where &a[0] is NULL and
3849 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3850 // where the first range doesn't include zero and the second one
3851 // doesn't either. As the second operand is sizetype (unsigned),
3852 // consider all ranges where the MSB could be set as possible
3853 // subtractions where the result might be NULL.
3854 if ((!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3855 || !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3856 && !TYPE_OVERFLOW_WRAPS (type
)
3857 && (flag_delete_null_pointer_checks
3858 || !wi::sign_mask (rh_ub
)))
3859 r
= range_nonzero (type
);
3860 else if (lh_lb
== lh_ub
&& lh_lb
== 0
3861 && rh_lb
== rh_ub
&& rh_lb
== 0)
3862 r
= range_zero (type
);
3864 r
.set_varying (type
);
3868 class pointer_min_max_operator
: public range_operator
3871 virtual void wi_fold (irange
& r
, tree type
,
3872 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3873 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3877 pointer_min_max_operator::wi_fold (irange
&r
, tree type
,
3878 const wide_int
&lh_lb
,
3879 const wide_int
&lh_ub
,
3880 const wide_int
&rh_lb
,
3881 const wide_int
&rh_ub
) const
3883 // For MIN/MAX expressions with pointers, we only care about
3884 // nullness. If both are non null, then the result is nonnull.
3885 // If both are null, then the result is null. Otherwise they
3887 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3888 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3889 r
= range_nonzero (type
);
3890 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
3891 r
= range_zero (type
);
3893 r
.set_varying (type
);
3897 class pointer_and_operator
: public range_operator
3900 virtual void wi_fold (irange
&r
, tree type
,
3901 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3902 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3906 pointer_and_operator::wi_fold (irange
&r
, tree type
,
3907 const wide_int
&lh_lb
,
3908 const wide_int
&lh_ub
,
3909 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
3910 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
3912 // For pointer types, we are really only interested in asserting
3913 // whether the expression evaluates to non-NULL.
3914 if (wi_zero_p (type
, lh_lb
, lh_ub
) || wi_zero_p (type
, lh_lb
, lh_ub
))
3915 r
= range_zero (type
);
3917 r
.set_varying (type
);
3921 class pointer_or_operator
: public range_operator
3924 virtual bool op1_range (irange
&r
, tree type
,
3927 relation_kind rel
= VREL_NONE
) const;
3928 virtual bool op2_range (irange
&r
, tree type
,
3931 relation_kind rel
= VREL_NONE
) const;
3932 virtual void wi_fold (irange
&r
, tree type
,
3933 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3934 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
3938 pointer_or_operator::op1_range (irange
&r
, tree type
,
3940 const irange
&op2 ATTRIBUTE_UNUSED
,
3941 relation_kind rel ATTRIBUTE_UNUSED
) const
3945 tree zero
= build_zero_cst (type
);
3946 r
= int_range
<1> (zero
, zero
);
3949 r
.set_varying (type
);
3954 pointer_or_operator::op2_range (irange
&r
, tree type
,
3957 relation_kind rel ATTRIBUTE_UNUSED
) const
3959 return pointer_or_operator::op1_range (r
, type
, lhs
, op1
);
3963 pointer_or_operator::wi_fold (irange
&r
, tree type
,
3964 const wide_int
&lh_lb
,
3965 const wide_int
&lh_ub
,
3966 const wide_int
&rh_lb
,
3967 const wide_int
&rh_ub
) const
3969 // For pointer types, we are really only interested in asserting
3970 // whether the expression evaluates to non-NULL.
3971 if (!wi_includes_zero_p (type
, lh_lb
, lh_ub
)
3972 && !wi_includes_zero_p (type
, rh_lb
, rh_ub
))
3973 r
= range_nonzero (type
);
3974 else if (wi_zero_p (type
, lh_lb
, lh_ub
) && wi_zero_p (type
, rh_lb
, rh_ub
))
3975 r
= range_zero (type
);
3977 r
.set_varying (type
);
3980 // This implements the range operator tables as local objects in this file.
3982 class range_op_table
3985 inline range_operator
*operator[] (enum tree_code code
);
3987 void set (enum tree_code code
, range_operator
&op
);
3989 range_operator
*m_range_tree
[MAX_TREE_CODES
];
3992 // Return a pointer to the range_operator instance, if there is one
3993 // associated with tree_code CODE.
3996 range_op_table::operator[] (enum tree_code code
)
3998 gcc_checking_assert (code
> 0 && code
< MAX_TREE_CODES
);
3999 return m_range_tree
[code
];
4002 // Add OP to the handler table for CODE.
4005 range_op_table::set (enum tree_code code
, range_operator
&op
)
4007 gcc_checking_assert (m_range_tree
[code
] == NULL
);
4008 m_range_tree
[code
] = &op
;
4011 // Instantiate a range op table for integral operations.
4013 class integral_table
: public range_op_table
4017 } integral_tree_table
;
4019 integral_table::integral_table ()
4021 set (EQ_EXPR
, op_equal
);
4022 set (NE_EXPR
, op_not_equal
);
4023 set (LT_EXPR
, op_lt
);
4024 set (LE_EXPR
, op_le
);
4025 set (GT_EXPR
, op_gt
);
4026 set (GE_EXPR
, op_ge
);
4027 set (PLUS_EXPR
, op_plus
);
4028 set (MINUS_EXPR
, op_minus
);
4029 set (MIN_EXPR
, op_min
);
4030 set (MAX_EXPR
, op_max
);
4031 set (MULT_EXPR
, op_mult
);
4032 set (TRUNC_DIV_EXPR
, op_trunc_div
);
4033 set (FLOOR_DIV_EXPR
, op_floor_div
);
4034 set (ROUND_DIV_EXPR
, op_round_div
);
4035 set (CEIL_DIV_EXPR
, op_ceil_div
);
4036 set (EXACT_DIV_EXPR
, op_exact_div
);
4037 set (LSHIFT_EXPR
, op_lshift
);
4038 set (RSHIFT_EXPR
, op_rshift
);
4039 set (NOP_EXPR
, op_convert
);
4040 set (CONVERT_EXPR
, op_convert
);
4041 set (TRUTH_AND_EXPR
, op_logical_and
);
4042 set (BIT_AND_EXPR
, op_bitwise_and
);
4043 set (TRUTH_OR_EXPR
, op_logical_or
);
4044 set (BIT_IOR_EXPR
, op_bitwise_or
);
4045 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4046 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
4047 set (TRUTH_NOT_EXPR
, op_logical_not
);
4048 set (BIT_NOT_EXPR
, op_bitwise_not
);
4049 set (INTEGER_CST
, op_integer_cst
);
4050 set (SSA_NAME
, op_identity
);
4051 set (PAREN_EXPR
, op_identity
);
4052 set (OBJ_TYPE_REF
, op_identity
);
4053 set (IMAGPART_EXPR
, op_unknown
);
4054 set (REALPART_EXPR
, op_unknown
);
4055 set (POINTER_DIFF_EXPR
, op_pointer_diff
);
4056 set (ABS_EXPR
, op_abs
);
4057 set (ABSU_EXPR
, op_absu
);
4058 set (NEGATE_EXPR
, op_negate
);
4059 set (ADDR_EXPR
, op_addr
);
4062 // Instantiate a range op table for pointer operations.
4064 class pointer_table
: public range_op_table
4068 } pointer_tree_table
;
4070 pointer_table::pointer_table ()
4072 set (BIT_AND_EXPR
, op_pointer_and
);
4073 set (BIT_IOR_EXPR
, op_pointer_or
);
4074 set (MIN_EXPR
, op_ptr_min_max
);
4075 set (MAX_EXPR
, op_ptr_min_max
);
4076 set (POINTER_PLUS_EXPR
, op_pointer_plus
);
4078 set (EQ_EXPR
, op_equal
);
4079 set (NE_EXPR
, op_not_equal
);
4080 set (LT_EXPR
, op_lt
);
4081 set (LE_EXPR
, op_le
);
4082 set (GT_EXPR
, op_gt
);
4083 set (GE_EXPR
, op_ge
);
4084 set (SSA_NAME
, op_identity
);
4085 set (INTEGER_CST
, op_integer_cst
);
4086 set (ADDR_EXPR
, op_addr
);
4087 set (NOP_EXPR
, op_convert
);
4088 set (CONVERT_EXPR
, op_convert
);
4090 set (BIT_NOT_EXPR
, op_bitwise_not
);
4091 set (BIT_XOR_EXPR
, op_bitwise_xor
);
4094 // The tables are hidden and accessed via a simple extern function.
4097 range_op_handler (enum tree_code code
, tree type
)
4099 // First check if there is a pointer specialization.
4100 if (POINTER_TYPE_P (type
))
4101 return pointer_tree_table
[code
];
4102 if (INTEGRAL_TYPE_P (type
))
4103 return integral_tree_table
[code
];
4107 // Cast the range in R to TYPE.
4110 range_cast (irange
&r
, tree type
)
4112 int_range_max tmp
= r
;
4113 range_operator
*op
= range_op_handler (CONVERT_EXPR
, type
);
4114 // Call op_convert, if it fails, the result is varying.
4115 if (!op
->fold_range (r
, type
, tmp
, int_range
<1> (type
)))
4116 r
.set_varying (type
);
4120 #include "selftest.h"
4124 #define INT(N) build_int_cst (integer_type_node, (N))
4125 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4126 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4127 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4128 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4129 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4132 range_op_cast_tests ()
4134 int_range
<1> r0
, r1
, r2
, rold
;
4135 r0
.set_varying (integer_type_node
);
4136 tree maxint
= wide_int_to_tree (integer_type_node
, r0
.upper_bound ());
4138 // If a range is in any way outside of the range for the converted
4139 // to range, default to the range for the new type.
4140 r0
.set_varying (short_integer_type_node
);
4141 tree minshort
= wide_int_to_tree (short_integer_type_node
, r0
.lower_bound ());
4142 tree maxshort
= wide_int_to_tree (short_integer_type_node
, r0
.upper_bound ());
4143 if (TYPE_PRECISION (TREE_TYPE (maxint
))
4144 > TYPE_PRECISION (short_integer_type_node
))
4146 r1
= int_range
<1> (integer_zero_node
, maxint
);
4147 range_cast (r1
, short_integer_type_node
);
4148 ASSERT_TRUE (r1
.lower_bound () == wi::to_wide (minshort
)
4149 && r1
.upper_bound() == wi::to_wide (maxshort
));
4152 // (unsigned char)[-5,-1] => [251,255].
4153 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (-1));
4154 range_cast (r0
, unsigned_char_type_node
);
4155 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (251), UCHAR (255)));
4156 range_cast (r0
, signed_char_type_node
);
4157 ASSERT_TRUE (r0
== rold
);
4159 // (signed char)[15, 150] => [-128,-106][15,127].
4160 r0
= rold
= int_range
<1> (UCHAR (15), UCHAR (150));
4161 range_cast (r0
, signed_char_type_node
);
4162 r1
= int_range
<1> (SCHAR (15), SCHAR (127));
4163 r2
= int_range
<1> (SCHAR (-128), SCHAR (-106));
4165 ASSERT_TRUE (r1
== r0
);
4166 range_cast (r0
, unsigned_char_type_node
);
4167 ASSERT_TRUE (r0
== rold
);
4169 // (unsigned char)[-5, 5] => [0,5][251,255].
4170 r0
= rold
= int_range
<1> (SCHAR (-5), SCHAR (5));
4171 range_cast (r0
, unsigned_char_type_node
);
4172 r1
= int_range
<1> (UCHAR (251), UCHAR (255));
4173 r2
= int_range
<1> (UCHAR (0), UCHAR (5));
4175 ASSERT_TRUE (r0
== r1
);
4176 range_cast (r0
, signed_char_type_node
);
4177 ASSERT_TRUE (r0
== rold
);
4179 // (unsigned char)[-5,5] => [0,5][251,255].
4180 r0
= int_range
<1> (INT (-5), INT (5));
4181 range_cast (r0
, unsigned_char_type_node
);
4182 r1
= int_range
<1> (UCHAR (0), UCHAR (5));
4183 r1
.union_ (int_range
<1> (UCHAR (251), UCHAR (255)));
4184 ASSERT_TRUE (r0
== r1
);
4186 // (unsigned char)[5U,1974U] => [0,255].
4187 r0
= int_range
<1> (UINT (5), UINT (1974));
4188 range_cast (r0
, unsigned_char_type_node
);
4189 ASSERT_TRUE (r0
== int_range
<1> (UCHAR (0), UCHAR (255)));
4190 range_cast (r0
, integer_type_node
);
4191 // Going to a wider range should not sign extend.
4192 ASSERT_TRUE (r0
== int_range
<1> (INT (0), INT (255)));
4194 // (unsigned char)[-350,15] => [0,255].
4195 r0
= int_range
<1> (INT (-350), INT (15));
4196 range_cast (r0
, unsigned_char_type_node
);
4197 ASSERT_TRUE (r0
== (int_range
<1>
4198 (TYPE_MIN_VALUE (unsigned_char_type_node
),
4199 TYPE_MAX_VALUE (unsigned_char_type_node
))));
4201 // Casting [-120,20] from signed char to unsigned short.
4202 // => [0, 20][0xff88, 0xffff].
4203 r0
= int_range
<1> (SCHAR (-120), SCHAR (20));
4204 range_cast (r0
, short_unsigned_type_node
);
4205 r1
= int_range
<1> (UINT16 (0), UINT16 (20));
4206 r2
= int_range
<1> (UINT16 (0xff88), UINT16 (0xffff));
4208 ASSERT_TRUE (r0
== r1
);
4209 // A truncating cast back to signed char will work because [-120, 20]
4210 // is representable in signed char.
4211 range_cast (r0
, signed_char_type_node
);
4212 ASSERT_TRUE (r0
== int_range
<1> (SCHAR (-120), SCHAR (20)));
4214 // unsigned char -> signed short
4215 // (signed short)[(unsigned char)25, (unsigned char)250]
4216 // => [(signed short)25, (signed short)250]
4217 r0
= rold
= int_range
<1> (UCHAR (25), UCHAR (250));
4218 range_cast (r0
, short_integer_type_node
);
4219 r1
= int_range
<1> (INT16 (25), INT16 (250));
4220 ASSERT_TRUE (r0
== r1
);
4221 range_cast (r0
, unsigned_char_type_node
);
4222 ASSERT_TRUE (r0
== rold
);
4224 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4225 r0
= int_range
<1> (TYPE_MIN_VALUE (long_long_integer_type_node
),
4226 TYPE_MAX_VALUE (long_long_integer_type_node
));
4227 range_cast (r0
, short_unsigned_type_node
);
4228 r1
= int_range
<1> (TYPE_MIN_VALUE (short_unsigned_type_node
),
4229 TYPE_MAX_VALUE (short_unsigned_type_node
));
4230 ASSERT_TRUE (r0
== r1
);
4232 // Casting NONZERO to a narrower type will wrap/overflow so
4233 // it's just the entire range for the narrower type.
4235 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4236 // is outside of the range of a smaller range, return the full
4238 if (TYPE_PRECISION (integer_type_node
)
4239 > TYPE_PRECISION (short_integer_type_node
))
4241 r0
= range_nonzero (integer_type_node
);
4242 range_cast (r0
, short_integer_type_node
);
4243 r1
= int_range
<1> (TYPE_MIN_VALUE (short_integer_type_node
),
4244 TYPE_MAX_VALUE (short_integer_type_node
));
4245 ASSERT_TRUE (r0
== r1
);
4248 // Casting NONZERO from a narrower signed to a wider signed.
4250 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4251 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4252 r0
= range_nonzero (short_integer_type_node
);
4253 range_cast (r0
, integer_type_node
);
4254 r1
= int_range
<1> (INT (-32768), INT (-1));
4255 r2
= int_range
<1> (INT (1), INT (32767));
4257 ASSERT_TRUE (r0
== r1
);
4261 range_op_lshift_tests ()
4263 // Test that 0x808.... & 0x8.... still contains 0x8....
4264 // for a large set of numbers.
4267 tree big_type
= long_long_unsigned_type_node
;
4268 // big_num = 0x808,0000,0000,0000
4269 tree big_num
= fold_build2 (LSHIFT_EXPR
, big_type
,
4270 build_int_cst (big_type
, 0x808),
4271 build_int_cst (big_type
, 48));
4272 op_bitwise_and
.fold_range (res
, big_type
,
4273 int_range
<1> (big_type
),
4274 int_range
<1> (big_num
, big_num
));
4275 // val = 0x8,0000,0000,0000
4276 tree val
= fold_build2 (LSHIFT_EXPR
, big_type
,
4277 build_int_cst (big_type
, 0x8),
4278 build_int_cst (big_type
, 48));
4279 ASSERT_TRUE (res
.contains_p (val
));
4282 if (TYPE_PRECISION (unsigned_type_node
) > 31)
4284 // unsigned VARYING = op1 << 1 should be VARYING.
4285 int_range
<2> lhs (unsigned_type_node
);
4286 int_range
<2> shift (INT (1), INT (1));
4288 op_lshift
.op1_range (op1
, unsigned_type_node
, lhs
, shift
);
4289 ASSERT_TRUE (op1
.varying_p ());
4291 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4292 int_range
<2> zero (UINT (0), UINT (0));
4293 op_lshift
.op1_range (op1
, unsigned_type_node
, zero
, shift
);
4294 ASSERT_TRUE (op1
.num_pairs () == 2);
4295 // Remove the [0,0] range.
4296 op1
.intersect (zero
);
4297 ASSERT_TRUE (op1
.num_pairs () == 1);
4298 // op1 << 1 should be [0x8000,0x8000] << 1,
4299 // which should result in [0,0].
4300 int_range_max result
;
4301 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4302 ASSERT_TRUE (result
== zero
);
4304 // signed VARYING = op1 << 1 should be VARYING.
4305 if (TYPE_PRECISION (integer_type_node
) > 31)
4307 // unsigned VARYING = op1 << 1 hould be VARYING.
4308 int_range
<2> lhs (integer_type_node
);
4309 int_range
<2> shift (INT (1), INT (1));
4311 op_lshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4312 ASSERT_TRUE (op1
.varying_p ());
4314 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4315 int_range
<2> zero (INT (0), INT (0));
4316 op_lshift
.op1_range (op1
, integer_type_node
, zero
, shift
);
4317 ASSERT_TRUE (op1
.num_pairs () == 2);
4318 // Remove the [0,0] range.
4319 op1
.intersect (zero
);
4320 ASSERT_TRUE (op1
.num_pairs () == 1);
4321 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4322 // which should result in [0,0].
4323 int_range_max result
;
4324 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4325 ASSERT_TRUE (result
== zero
);
4330 range_op_rshift_tests ()
4332 // unsigned: [3, MAX] = OP1 >> 1
4334 int_range_max
lhs (build_int_cst (unsigned_type_node
, 3),
4335 TYPE_MAX_VALUE (unsigned_type_node
));
4336 int_range_max
one (build_one_cst (unsigned_type_node
),
4337 build_one_cst (unsigned_type_node
));
4339 op_rshift
.op1_range (op1
, unsigned_type_node
, lhs
, one
);
4340 ASSERT_FALSE (op1
.contains_p (UINT (3)));
4343 // signed: [3, MAX] = OP1 >> 1
4345 int_range_max
lhs (INT (3), TYPE_MAX_VALUE (integer_type_node
));
4346 int_range_max
one (INT (1), INT (1));
4348 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4349 ASSERT_FALSE (op1
.contains_p (INT (-2)));
4352 // This is impossible, so OP1 should be [].
4353 // signed: [MIN, MIN] = OP1 >> 1
4355 int_range_max
lhs (TYPE_MIN_VALUE (integer_type_node
),
4356 TYPE_MIN_VALUE (integer_type_node
));
4357 int_range_max
one (INT (1), INT (1));
4359 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4360 ASSERT_TRUE (op1
.undefined_p ());
4363 // signed: ~[-1] = OP1 >> 31
4364 if (TYPE_PRECISION (integer_type_node
) > 31)
4366 int_range_max
lhs (INT (-1), INT (-1), VR_ANTI_RANGE
);
4367 int_range_max
shift (INT (31), INT (31));
4369 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4370 int_range_max negatives
= range_negatives (integer_type_node
);
4371 negatives
.intersect (op1
);
4372 ASSERT_TRUE (negatives
.undefined_p ());
4377 range_op_bitwise_and_tests ()
4380 tree min
= vrp_val_min (integer_type_node
);
4381 tree max
= vrp_val_max (integer_type_node
);
4382 tree tiny
= fold_build2 (PLUS_EXPR
, integer_type_node
, min
,
4383 build_one_cst (integer_type_node
));
4384 int_range_max
i1 (tiny
, max
);
4385 int_range_max
i2 (build_int_cst (integer_type_node
, 255),
4386 build_int_cst (integer_type_node
, 255));
4388 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4389 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4390 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4392 // VARYING = OP1 & 255: OP1 is VARYING
4393 i1
= int_range
<1> (integer_type_node
);
4394 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4395 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4397 // (NONZERO | X) is nonzero.
4398 i1
.set_nonzero (integer_type_node
);
4399 i2
.set_varying (integer_type_node
);
4400 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4401 ASSERT_TRUE (res
.nonzero_p ());
4403 // (NEGATIVE | X) is nonzero.
4404 i1
= int_range
<1> (INT (-5), INT (-3));
4405 i2
.set_varying (integer_type_node
);
4406 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4407 ASSERT_FALSE (res
.contains_p (INT (0)));
4411 range_relational_tests ()
4413 int_range
<2> lhs (unsigned_char_type_node
);
4414 int_range
<2> op1 (UCHAR (8), UCHAR (10));
4415 int_range
<2> op2 (UCHAR (20), UCHAR (20));
4417 // Never wrapping additions mean LHS > OP1.
4418 tree_code code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
);
4419 ASSERT_TRUE (code
== GT_EXPR
);
4421 // Most wrapping additions mean nothing...
4422 op1
= int_range
<2> (UCHAR (8), UCHAR (10));
4423 op2
= int_range
<2> (UCHAR (0), UCHAR (255));
4424 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
);
4425 ASSERT_TRUE (code
== VREL_NONE
);
4427 // However, always wrapping additions mean LHS < OP1.
4428 op1
= int_range
<2> (UCHAR (1), UCHAR (255));
4429 op2
= int_range
<2> (UCHAR (255), UCHAR (255));
4430 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
);
4431 ASSERT_TRUE (code
== LT_EXPR
);
4437 range_op_rshift_tests ();
4438 range_op_lshift_tests ();
4439 range_op_bitwise_and_tests ();
4440 range_op_cast_tests ();
4441 range_relational_tests ();
4444 } // namespace selftest
4446 #endif // CHECKING_P