Fix typo in t-dimode
[official-gcc.git] / gcc / range-op.cc
blobbbf2924f8157645e34ab80098d5ba8437eb1256e
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)
11 any later version.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "insn-codes.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
36 #include "flags.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "cfganal.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "tree-cfg.h"
46 #include "wide-int.h"
47 #include "value-relation.h"
48 #include "range-op.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.
71 inline bool
72 empty_range_varying (irange &r, tree type,
73 const irange &op1, const irange & op2)
75 if (op1.undefined_p () || op2.undefined_p ())
77 r.set_varying (type);
78 return true;
80 else
81 return false;
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.
88 static inline bool
89 get_shift_range (irange &r, tree type, const irange &op)
91 if (op.undefined_p ())
92 return false;
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));
97 r.intersect (op);
99 // If there are no valid ranges in the shift range, returned false.
100 if (r.undefined_p ())
101 return false;
102 return true;
105 // Return TRUE if 0 is within [WMIN, WMAX].
107 static inline bool
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.
116 static inline bool
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].
125 void
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]
140 void
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;
148 int_range_max tmp;
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);
161 r.union_ (tmp);
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);
165 r.union_ (tmp);
168 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
169 r.union_ (tmp);
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);
180 r.union_ (tmp);
181 if (wi::eq_p (lh_range, 3))
183 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
184 r.union_ (tmp);
187 wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
188 r.union_ (tmp);
190 // Otherwise just call wi_fold.
191 else
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.
198 bool
199 range_operator::fold_range (irange &r, tree type,
200 const irange &lh,
201 const irange &rh,
202 relation_kind rel) const
204 gcc_checking_assert (irange::supports_type_p (type));
205 if (empty_range_varying (r, type, lh, rh))
206 return true;
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);
217 return true;
220 int_range_max tmp;
221 r.set_undefined ();
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);
230 r.union_ (tmp);
231 if (r.varying_p ())
233 op1_op2_relation_effect (r, type, lh, rh, rel);
234 return true;
237 op1_op2_relation_effect (r, type, lh, rh, rel);
238 return true;
241 // The default for op1_range is to return false.
243 bool
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
250 return false;
253 // The default for op2_range is to return false.
255 bool
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
262 return false;
265 // The default relation routines return VREL_NONE.
267 enum tree_code
268 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
269 const irange &op1 ATTRIBUTE_UNUSED,
270 const irange &op2 ATTRIBUTE_UNUSED) const
272 return VREL_NONE;
275 enum tree_code
276 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
277 const irange &op1 ATTRIBUTE_UNUSED,
278 const irange &op2 ATTRIBUTE_UNUSED) const
280 return VREL_NONE;
283 enum tree_code
284 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
286 return VREL_NONE;
289 // Default is no relation affects the LHS.
291 bool
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
298 return false;
301 // Create and return a range from a pair of wide-ints that are known
302 // to have overflowed (or underflowed).
304 static void
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);
315 bool covers = false;
316 wide_int tem = tmin;
317 tmin = tmax + 1;
318 if (wi::cmp (tmin, tmax, sgn) < 0)
319 covers = true;
320 tmax = tem - 1;
321 if (wi::cmp (tmax, tem, sgn) > 0)
322 covers = true;
324 // If the anti-range would cover nothing, drop to varying.
325 // Likewise if the anti-range bounds are outside of the types
326 // values.
327 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
328 r.set_varying (type);
329 else
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.
341 static void
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
352 // values.
353 if (prec == 1 && wi::ne_p (wmax, wmin))
355 r.set_varying (type);
356 return;
359 if (overflow_wraps)
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
368 // the entire range.
369 if (wi::gt_p (tmin, tmax, sgn))
370 r.set_varying (type);
371 else
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));
376 return;
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);
382 else
383 // Other underflow and/or overflow, drop to VR_VARYING.
384 r.set_varying (type);
386 else
388 // If both bounds either underflowed or overflowed, then the result
389 // is undefined.
390 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
391 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
393 r.set_undefined ();
394 return;
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);
403 else
404 new_lb = wmin;
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);
410 else
411 new_ub = wmax;
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].
422 static inline void
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);
430 else
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 ())
473 r.set_undefined ();
474 return BRS_EMPTY;
477 if (lhs.zero_p ())
478 return BRS_FALSE;
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);
485 return BRS_FULL;
488 return BRS_TRUE;
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,
494 // return false.
496 static inline bool
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);
505 return true;
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);
512 return true;
515 // If either operand is undefined, return VARYING.
516 if (empty_range_varying (r, type, op1, op2))
517 return true;
519 return false;
523 class operator_equal : public range_operator
525 public:
526 virtual bool fold_range (irange &r, tree type,
527 const irange &op1,
528 const irange &op2,
529 relation_kind rel = VREL_NONE) const;
530 virtual bool op1_range (irange &r, tree type,
531 const irange &lhs,
532 const irange &val,
533 relation_kind rel = VREL_NONE) const;
534 virtual bool op2_range (irange &r, tree type,
535 const irange &lhs,
536 const irange &val,
537 relation_kind rel = VREL_NONE) const;
538 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
539 } op_equal;
541 // Check if the LHS range indicates a relation between OP1 and OP2.
543 enum tree_code
544 operator_equal::op1_op2_relation (const irange &lhs) const
546 if (lhs.undefined_p ())
547 return VREL_EMPTY;
549 // FALSE = op1 == op2 indicates NE_EXPR.
550 if (lhs.zero_p ())
551 return NE_EXPR;
553 // TRUE = op1 == op2 indicates EQ_EXPR.
554 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
555 return EQ_EXPR;
556 return VREL_NONE;
560 bool
561 operator_equal::fold_range (irange &r, tree type,
562 const irange &op1,
563 const irange &op2,
564 relation_kind rel) const
566 if (relop_early_resolve (r, type, op1, op2, rel, EQ_EXPR))
567 return true;
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);
576 else
577 r = range_false (type);
579 else
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;
584 tmp.intersect (op2);
585 if (tmp.undefined_p ())
586 r = range_false (type);
587 else
588 r = range_true_and_false (type);
590 return true;
593 bool
594 operator_equal::op1_range (irange &r, tree type,
595 const irange &lhs,
596 const irange &op2,
597 relation_kind rel ATTRIBUTE_UNUSED) const
599 switch (get_bool_state (r, lhs, type))
601 case BRS_FALSE:
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()))
606 r = op2;
607 r.invert ();
609 else
610 r.set_varying (type);
611 break;
613 case BRS_TRUE:
614 // If it's true, the result is the same as OP2.
615 r = op2;
616 break;
618 default:
619 break;
621 return true;
624 bool
625 operator_equal::op2_range (irange &r, tree type,
626 const irange &lhs,
627 const irange &op1,
628 relation_kind rel) const
630 return operator_equal::op1_range (r, type, lhs, op1, rel);
633 class operator_not_equal : public range_operator
635 public:
636 virtual bool fold_range (irange &r, tree type,
637 const irange &op1,
638 const irange &op2,
639 relation_kind rel = VREL_NONE) const;
640 virtual bool op1_range (irange &r, tree type,
641 const irange &lhs,
642 const irange &op2,
643 relation_kind rel = VREL_NONE) const;
644 virtual bool op2_range (irange &r, tree type,
645 const irange &lhs,
646 const irange &op1,
647 relation_kind rel = VREL_NONE) const;
648 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
649 } op_not_equal;
651 // Check if the LHS range indicates a relation between OP1 and OP2.
653 enum tree_code
654 operator_not_equal::op1_op2_relation (const irange &lhs) const
656 if (lhs.undefined_p ())
657 return VREL_EMPTY;
659 // FALSE = op1 != op2 indicates EQ_EXPR.
660 if (lhs.zero_p ())
661 return EQ_EXPR;
663 // TRUE = op1 != op2 indicates NE_EXPR.
664 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
665 return NE_EXPR;
666 return VREL_NONE;
669 bool
670 operator_not_equal::fold_range (irange &r, tree type,
671 const irange &op1,
672 const irange &op2,
673 relation_kind rel) const
675 if (relop_early_resolve (r, type, op1, op2, rel, NE_EXPR))
676 return true;
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);
685 else
686 r = range_false (type);
688 else
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;
693 tmp.intersect (op2);
694 if (tmp.undefined_p ())
695 r = range_true (type);
696 else
697 r = range_true_and_false (type);
699 return true;
702 bool
703 operator_not_equal::op1_range (irange &r, tree type,
704 const irange &lhs,
705 const irange &op2,
706 relation_kind rel ATTRIBUTE_UNUSED) const
708 switch (get_bool_state (r, lhs, type))
710 case BRS_TRUE:
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()))
715 r = op2;
716 r.invert ();
718 else
719 r.set_varying (type);
720 break;
722 case BRS_FALSE:
723 // If it's false, the result is the same as OP2.
724 r = op2;
725 break;
727 default:
728 break;
730 return true;
734 bool
735 operator_not_equal::op2_range (irange &r, tree type,
736 const irange &lhs,
737 const irange &op1,
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].
745 static void
746 build_lt (irange &r, tree type, const wide_int &val)
748 wi::overflow_type ov;
749 wide_int lim;
750 signop sgn = TYPE_SIGN (type);
752 // Signed 1 bit cannot represent 1 for subtraction.
753 if (sgn == SIGNED)
754 lim = wi::add (val, -1, sgn, &ov);
755 else
756 lim = wi::sub (val, 1, sgn, &ov);
758 // If val - 1 underflows, check if X < MIN, which is an empty range.
759 if (ov)
760 r.set_undefined ();
761 else
762 r = int_range<1> (type, min_limit (type), lim);
765 // (X <= VAL) produces the range of [MIN, VAL].
767 static void
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].
775 static void
776 build_gt (irange &r, tree type, const wide_int &val)
778 wi::overflow_type ov;
779 wide_int lim;
780 signop sgn = TYPE_SIGN (type);
782 // Signed 1 bit cannot represent 1 for addition.
783 if (sgn == SIGNED)
784 lim = wi::sub (val, -1, sgn, &ov);
785 else
786 lim = wi::add (val, 1, sgn, &ov);
787 // If val + 1 overflows, check is for X > MAX, which is an empty range.
788 if (ov)
789 r.set_undefined ();
790 else
791 r = int_range<1> (type, lim, max_limit (type));
794 // (X >= val) produces the range of [VAL, MAX].
796 static void
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
805 public:
806 virtual bool fold_range (irange &r, tree type,
807 const irange &op1,
808 const irange &op2,
809 relation_kind rel = VREL_NONE) const;
810 virtual bool op1_range (irange &r, tree type,
811 const irange &lhs,
812 const irange &op2,
813 relation_kind rel = VREL_NONE) const;
814 virtual bool op2_range (irange &r, tree type,
815 const irange &lhs,
816 const irange &op1,
817 relation_kind rel = VREL_NONE) const;
818 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
819 } op_lt;
821 // Check if the LHS range indicates a relation between OP1 and OP2.
823 enum tree_code
824 operator_lt::op1_op2_relation (const irange &lhs) const
826 if (lhs.undefined_p ())
827 return VREL_EMPTY;
829 // FALSE = op1 < op2 indicates GE_EXPR.
830 if (lhs.zero_p ())
831 return GE_EXPR;
833 // TRUE = op1 < op2 indicates LT_EXPR.
834 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
835 return LT_EXPR;
836 return VREL_NONE;
839 bool
840 operator_lt::fold_range (irange &r, tree type,
841 const irange &op1,
842 const irange &op2,
843 relation_kind rel) const
845 if (relop_early_resolve (r, type, op1, op2, rel, LT_EXPR))
846 return true;
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);
855 else
856 r = range_true_and_false (type);
857 return true;
860 bool
861 operator_lt::op1_range (irange &r, tree type,
862 const irange &lhs,
863 const irange &op2,
864 relation_kind rel ATTRIBUTE_UNUSED) const
866 switch (get_bool_state (r, lhs, type))
868 case BRS_TRUE:
869 build_lt (r, type, op2.upper_bound ());
870 break;
872 case BRS_FALSE:
873 build_ge (r, type, op2.lower_bound ());
874 break;
876 default:
877 break;
879 return true;
882 bool
883 operator_lt::op2_range (irange &r, tree type,
884 const irange &lhs,
885 const irange &op1,
886 relation_kind rel ATTRIBUTE_UNUSED) const
888 switch (get_bool_state (r, lhs, type))
890 case BRS_FALSE:
891 build_le (r, type, op1.upper_bound ());
892 break;
894 case BRS_TRUE:
895 build_gt (r, type, op1.lower_bound ());
896 break;
898 default:
899 break;
901 return true;
905 class operator_le : public range_operator
907 public:
908 virtual bool fold_range (irange &r, tree type,
909 const irange &op1,
910 const irange &op2,
911 relation_kind rel = VREL_NONE) const;
912 virtual bool op1_range (irange &r, tree type,
913 const irange &lhs,
914 const irange &op2,
915 relation_kind rel = VREL_NONE) const;
916 virtual bool op2_range (irange &r, tree type,
917 const irange &lhs,
918 const irange &op1,
919 relation_kind rel = VREL_NONE) const;
920 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
921 } op_le;
923 // Check if the LHS range indicates a relation between OP1 and OP2.
925 enum tree_code
926 operator_le::op1_op2_relation (const irange &lhs) const
928 if (lhs.undefined_p ())
929 return VREL_EMPTY;
931 // FALSE = op1 <= op2 indicates GT_EXPR.
932 if (lhs.zero_p ())
933 return GT_EXPR;
935 // TRUE = op1 <= op2 indicates LE_EXPR.
936 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
937 return LE_EXPR;
938 return VREL_NONE;
941 bool
942 operator_le::fold_range (irange &r, tree type,
943 const irange &op1,
944 const irange &op2,
945 relation_kind rel) const
947 if (relop_early_resolve (r, type, op1, op2, rel, LE_EXPR))
948 return true;
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);
957 else
958 r = range_true_and_false (type);
959 return true;
962 bool
963 operator_le::op1_range (irange &r, tree type,
964 const irange &lhs,
965 const irange &op2,
966 relation_kind rel ATTRIBUTE_UNUSED) const
968 switch (get_bool_state (r, lhs, type))
970 case BRS_TRUE:
971 build_le (r, type, op2.upper_bound ());
972 break;
974 case BRS_FALSE:
975 build_gt (r, type, op2.lower_bound ());
976 break;
978 default:
979 break;
981 return true;
984 bool
985 operator_le::op2_range (irange &r, tree type,
986 const irange &lhs,
987 const irange &op1,
988 relation_kind rel ATTRIBUTE_UNUSED) const
990 switch (get_bool_state (r, lhs, type))
992 case BRS_FALSE:
993 build_lt (r, type, op1.upper_bound ());
994 break;
996 case BRS_TRUE:
997 build_ge (r, type, op1.lower_bound ());
998 break;
1000 default:
1001 break;
1003 return true;
1007 class operator_gt : public range_operator
1009 public:
1010 virtual bool fold_range (irange &r, tree type,
1011 const irange &op1,
1012 const irange &op2,
1013 relation_kind rel = VREL_NONE) const;
1014 virtual bool op1_range (irange &r, tree type,
1015 const irange &lhs,
1016 const irange &op2,
1017 relation_kind rel = VREL_NONE) const;
1018 virtual bool op2_range (irange &r, tree type,
1019 const irange &lhs,
1020 const irange &op1,
1021 relation_kind rel = VREL_NONE) const;
1022 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
1023 } op_gt;
1025 // Check if the LHS range indicates a relation between OP1 and OP2.
1027 enum tree_code
1028 operator_gt::op1_op2_relation (const irange &lhs) const
1030 if (lhs.undefined_p ())
1031 return VREL_EMPTY;
1033 // FALSE = op1 > op2 indicates LE_EXPR.
1034 if (lhs.zero_p ())
1035 return LE_EXPR;
1037 // TRUE = op1 > op2 indicates GT_EXPR.
1038 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1039 return GT_EXPR;
1040 return VREL_NONE;
1044 bool
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))
1050 return true;
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);
1059 else
1060 r = range_true_and_false (type);
1061 return true;
1064 bool
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))
1071 case BRS_TRUE:
1072 build_gt (r, type, op2.lower_bound ());
1073 break;
1075 case BRS_FALSE:
1076 build_le (r, type, op2.upper_bound ());
1077 break;
1079 default:
1080 break;
1082 return true;
1085 bool
1086 operator_gt::op2_range (irange &r, tree type,
1087 const irange &lhs,
1088 const irange &op1,
1089 relation_kind rel ATTRIBUTE_UNUSED) const
1091 switch (get_bool_state (r, lhs, type))
1093 case BRS_FALSE:
1094 build_ge (r, type, op1.lower_bound ());
1095 break;
1097 case BRS_TRUE:
1098 build_lt (r, type, op1.upper_bound ());
1099 break;
1101 default:
1102 break;
1104 return true;
1108 class operator_ge : public range_operator
1110 public:
1111 virtual bool fold_range (irange &r, tree type,
1112 const irange &op1,
1113 const irange &op2,
1114 relation_kind rel = VREL_NONE) const;
1115 virtual bool op1_range (irange &r, tree type,
1116 const irange &lhs,
1117 const irange &op2,
1118 relation_kind rel = VREL_NONE) const;
1119 virtual bool op2_range (irange &r, tree type,
1120 const irange &lhs,
1121 const irange &op1,
1122 relation_kind rel = VREL_NONE) const;
1123 virtual enum tree_code op1_op2_relation (const irange &lhs) const;
1124 } op_ge;
1126 // Check if the LHS range indicates a relation between OP1 and OP2.
1128 enum tree_code
1129 operator_ge::op1_op2_relation (const irange &lhs) const
1131 if (lhs.undefined_p ())
1132 return VREL_EMPTY;
1134 // FALSE = op1 >= op2 indicates LT_EXPR.
1135 if (lhs.zero_p ())
1136 return LT_EXPR;
1138 // TRUE = op1 >= op2 indicates GE_EXPR.
1139 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1140 return GE_EXPR;
1141 return VREL_NONE;
1144 bool
1145 operator_ge::fold_range (irange &r, tree type,
1146 const irange &op1,
1147 const irange &op2,
1148 relation_kind rel) const
1150 if (relop_early_resolve (r, type, op1, op2, rel, GE_EXPR))
1151 return true;
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);
1160 else
1161 r = range_true_and_false (type);
1162 return true;
1165 bool
1166 operator_ge::op1_range (irange &r, tree type,
1167 const irange &lhs,
1168 const irange &op2,
1169 relation_kind rel ATTRIBUTE_UNUSED) const
1171 switch (get_bool_state (r, lhs, type))
1173 case BRS_TRUE:
1174 build_ge (r, type, op2.lower_bound ());
1175 break;
1177 case BRS_FALSE:
1178 build_lt (r, type, op2.upper_bound ());
1179 break;
1181 default:
1182 break;
1184 return true;
1187 bool
1188 operator_ge::op2_range (irange &r, tree type,
1189 const irange &lhs,
1190 const irange &op1,
1191 relation_kind rel ATTRIBUTE_UNUSED) const
1193 switch (get_bool_state (r, lhs, type))
1195 case BRS_FALSE:
1196 build_gt (r, type, op1.lower_bound ());
1197 break;
1199 case BRS_TRUE:
1200 build_le (r, type, op1.upper_bound ());
1201 break;
1203 default:
1204 break;
1206 return true;
1210 class operator_plus : public range_operator
1212 public:
1213 virtual bool op1_range (irange &r, tree type,
1214 const irange &lhs,
1215 const irange &op2,
1216 relation_kind rel ATTRIBUTE_UNUSED) const;
1217 virtual bool op2_range (irange &r, tree type,
1218 const irange &lhs,
1219 const irange &op1,
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;
1230 } op_plus;
1232 // Check to see if the range of OP2 indicates anything about the relation
1233 // between LHS and OP1.
1235 enum tree_code
1236 operator_plus::lhs_op1_relation (const irange &lhs,
1237 const irange &op1,
1238 const irange &op2) const
1240 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1241 return VREL_NONE;
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.
1249 if (op2.zero_p ())
1250 return EQ_EXPR;
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);
1257 else
1258 ovf1 = ovf2 = wi::OVF_NONE;
1260 // Never wrapping additions.
1261 if (!ovf1 && !ovf2)
1263 // Positive op2 means lhs > op1.
1264 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1265 return GT_EXPR;
1266 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1267 return GE_EXPR;
1269 // Negative op2 means lhs < op1.
1270 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1271 return LT_EXPR;
1272 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1273 return LE_EXPR;
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))
1280 return LT_EXPR;
1281 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1282 return LE_EXPR;
1284 // Negative op2 means lhs > op1.
1285 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1286 return GT_EXPR;
1287 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1288 return GE_EXPR;
1291 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1292 if (!range_includes_zero_p (&op2))
1293 return NE_EXPR;
1295 return VREL_NONE;
1298 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1299 // operands.
1301 enum tree_code
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);
1308 void
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);
1320 bool
1321 operator_plus::op1_range (irange &r, tree type,
1322 const irange &lhs,
1323 const irange &op2,
1324 relation_kind rel ATTRIBUTE_UNUSED) const
1326 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op2);
1329 bool
1330 operator_plus::op2_range (irange &r, tree type,
1331 const irange &lhs,
1332 const irange &op1,
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
1341 public:
1342 virtual bool op1_range (irange &r, tree type,
1343 const irange &lhs,
1344 const irange &op2,
1345 relation_kind rel ATTRIBUTE_UNUSED) const;
1346 virtual bool op2_range (irange &r, tree type,
1347 const irange &lhs,
1348 const irange &op1,
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,
1356 tree type,
1357 const irange &op1_range,
1358 const irange &op2_range,
1359 relation_kind rel) const;
1360 } op_minus;
1362 void
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.
1378 static bool
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,
1382 relation_kind rel)
1384 if (rel == VREL_NONE)
1385 return false;
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.
1392 if (rel == EQ_EXPR)
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),
1396 VR_ANTI_RANGE);
1397 else if (TYPE_OVERFLOW_WRAPS (type))
1399 switch (rel)
1401 // For wrapping signed values and unsigned, if op1 > op2 or
1402 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1403 case GT_EXPR:
1404 case LT_EXPR:
1405 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1406 VR_ANTI_RANGE);
1407 break;
1408 default:
1409 return false;
1412 else
1414 switch (rel)
1416 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1417 case GT_EXPR:
1418 rel_range = int_range<2> (type, wi::one (prec),
1419 wi::max_value (prec, sgn));
1420 break;
1421 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1422 case GE_EXPR:
1423 rel_range = int_range<2> (type, wi::zero (prec),
1424 wi::max_value (prec, sgn));
1425 break;
1426 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1427 case LT_EXPR:
1428 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1429 wi::minus_one (prec));
1430 break;
1431 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1432 case LE_EXPR:
1433 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1434 wi::zero (prec));
1435 break;
1436 default:
1437 return false;
1440 lhs_range.intersect (rel_range);
1441 return true;
1444 bool
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,
1451 rel);
1454 bool
1455 operator_minus::op1_range (irange &r, tree type,
1456 const irange &lhs,
1457 const irange &op2,
1458 relation_kind rel ATTRIBUTE_UNUSED) const
1460 return range_op_handler (PLUS_EXPR, type)->fold_range (r, type, lhs, op2);
1463 bool
1464 operator_minus::op2_range (irange &r, tree type,
1465 const irange &lhs,
1466 const irange &op1,
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,
1476 tree type,
1477 const irange &op1_range,
1478 const irange &op2_range,
1479 relation_kind rel) const;
1480 } op_pointer_diff;
1482 bool
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,
1489 rel);
1493 class operator_min : public range_operator
1495 public:
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;
1501 } op_min;
1503 void
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
1517 public:
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;
1523 } op_max;
1525 void
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
1539 public:
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,
1543 tree type,
1544 const wide_int &,
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.
1568 void
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
1580 // can't handle.
1581 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1582 return;
1583 if (wi::eq_p (lh_lb, lh_ub))
1584 cp3 = cp1;
1585 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1586 return;
1587 if (wi::eq_p (rh_lb, rh_ub))
1588 cp2 = cp1;
1589 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1590 return;
1591 if (wi::eq_p (lh_lb, lh_ub))
1592 cp4 = cp2;
1593 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1594 return;
1596 // Order pairs.
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
1612 public:
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,
1621 const irange &lhs,
1622 const irange &op2,
1623 relation_kind rel ATTRIBUTE_UNUSED) const;
1624 virtual bool op2_range (irange &r, tree type,
1625 const irange &lhs,
1626 const irange &op1,
1627 relation_kind rel ATTRIBUTE_UNUSED) const;
1628 } op_mult;
1630 bool
1631 operator_mult::op1_range (irange &r, tree type,
1632 const irange &lhs, const irange &op2,
1633 relation_kind rel ATTRIBUTE_UNUSED) const
1635 tree offset;
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))
1641 return false;
1643 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1644 return range_op_handler (TRUNC_DIV_EXPR, type)->fold_range (r, type,
1645 lhs, op2);
1646 return false;
1649 bool
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);
1657 bool
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);
1670 else
1671 res = wi::min_value (w0.get_precision (), sign);
1672 return false;
1674 return overflow;
1677 void
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);
1685 return;
1688 // Multiply the ranges when overflow wraps. This is basically fancy
1689 // code so we don't drop to varying with an unsigned
1690 // [-3,-1]*[-3,-1].
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
1696 // were.
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))
1712 min0 -= size;
1713 max0 -= size;
1715 if (wi::ltu_p (size, min1 + max1))
1717 min1 -= size;
1718 max1 -= size;
1722 // Sort the 4 products so that min is in prod0 and max is in
1723 // prod3.
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
1730 if (prod0 > prod3)
1731 std::swap (prod0, prod3);
1733 // min0max1 > max0min1
1734 if (prod1 > prod2)
1735 std::swap (prod1, prod2);
1737 if (prod0 > prod1)
1738 std::swap (prod0, prod1);
1740 if (prod2 > prod3)
1741 std::swap (prod2, prod3);
1743 // diff = max - min
1744 prod2 = prod3 - prod0;
1745 if (wi::geu_p (prod2, sizem1))
1746 // The range covers all values.
1747 r.set_varying (type);
1748 else
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
1759 public:
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;
1768 private:
1769 enum tree_code code;
1772 bool
1773 operator_div::wi_op_overflows (wide_int &res, tree type,
1774 const wide_int &w0, const wide_int &w1) const
1776 if (w1 == 0)
1777 return true;
1779 wi::overflow_type overflow = wi::OVF_NONE;
1780 signop sign = TYPE_SIGN (type);
1782 switch (code)
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.
1787 gcc_unreachable ();
1788 break;
1789 case TRUNC_DIV_EXPR:
1790 res = wi::div_trunc (w0, w1, sign, &overflow);
1791 break;
1792 case FLOOR_DIV_EXPR:
1793 res = wi::div_floor (w0, w1, sign, &overflow);
1794 break;
1795 case ROUND_DIV_EXPR:
1796 res = wi::div_round (w0, w1, sign, &overflow);
1797 break;
1798 case CEIL_DIV_EXPR:
1799 res = wi::div_ceil (w0, w1, sign, &overflow);
1800 break;
1801 default:
1802 gcc_unreachable ();
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);
1809 return false;
1811 return overflow;
1814 void
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);
1832 return;
1835 // If flag_non_call_exceptions, we must not eliminate a division by zero.
1836 if (cfun->can_throw_non_call_exceptions)
1838 r.set_varying (type);
1839 return;
1842 // If we're definitely dividing by zero, there's nothing to do.
1843 if (wi_zero_p (type, divisor_min, divisor_max))
1845 r.set_undefined ();
1846 return;
1849 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1850 // skip any division by zero.
1852 // First divide by the negative numbers, if any.
1853 if (wi::neg_p (divisor_min, sign))
1854 wi_cross_product (r, type, dividend_min, dividend_max,
1855 divisor_min, wi::minus_one (prec));
1856 else
1857 r.set_undefined ();
1859 // Then divide by the non-zero positive numbers, if any.
1860 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1862 int_range_max tmp;
1863 wi_cross_product (tmp, type, dividend_min, dividend_max,
1864 wi::one (prec), divisor_max);
1865 r.union_ (tmp);
1867 // We shouldn't still have undefined here.
1868 gcc_checking_assert (!r.undefined_p ());
1871 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1872 operator_div op_floor_div (FLOOR_DIV_EXPR);
1873 operator_div op_round_div (ROUND_DIV_EXPR);
1874 operator_div op_ceil_div (CEIL_DIV_EXPR);
1877 class operator_exact_divide : public operator_div
1879 public:
1880 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1881 virtual bool op1_range (irange &r, tree type,
1882 const irange &lhs,
1883 const irange &op2,
1884 relation_kind rel ATTRIBUTE_UNUSED) const;
1886 } op_exact_div;
1888 bool
1889 operator_exact_divide::op1_range (irange &r, tree type,
1890 const irange &lhs,
1891 const irange &op2,
1892 relation_kind rel ATTRIBUTE_UNUSED) const
1894 tree offset;
1895 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1896 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1897 // We wont bother trying to enumerate all the in between stuff :-P
1898 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1899 // the time however.
1900 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1901 if (op2.singleton_p (&offset)
1902 && !integer_zerop (offset))
1903 return range_op_handler (MULT_EXPR, type)->fold_range (r, type, lhs, op2);
1904 return false;
1908 class operator_lshift : public cross_product_operator
1910 public:
1911 virtual bool op1_range (irange &r, tree type,
1912 const irange &lhs,
1913 const irange &op2,
1914 relation_kind rel = VREL_NONE) const;
1915 virtual bool fold_range (irange &r, tree type,
1916 const irange &op1,
1917 const irange &op2,
1918 relation_kind rel = VREL_NONE) const;
1920 virtual void wi_fold (irange &r, tree type,
1921 const wide_int &lh_lb, const wide_int &lh_ub,
1922 const wide_int &rh_lb, const wide_int &rh_ub) const;
1923 virtual bool wi_op_overflows (wide_int &res,
1924 tree type,
1925 const wide_int &,
1926 const wide_int &) const;
1927 } op_lshift;
1929 class operator_rshift : public cross_product_operator
1931 public:
1932 virtual bool fold_range (irange &r, tree type,
1933 const irange &op1,
1934 const irange &op2,
1935 relation_kind rel = VREL_NONE) const;
1936 virtual void wi_fold (irange &r, tree type,
1937 const wide_int &lh_lb,
1938 const wide_int &lh_ub,
1939 const wide_int &rh_lb,
1940 const wide_int &rh_ub) const;
1941 virtual bool wi_op_overflows (wide_int &res,
1942 tree type,
1943 const wide_int &w0,
1944 const wide_int &w1) const;
1945 virtual bool op1_range (irange &, tree type,
1946 const irange &lhs,
1947 const irange &op2,
1948 relation_kind rel = VREL_NONE) const;
1949 } op_rshift;
1952 bool
1953 operator_lshift::fold_range (irange &r, tree type,
1954 const irange &op1,
1955 const irange &op2,
1956 relation_kind rel) const
1958 int_range_max shift_range;
1959 if (!get_shift_range (shift_range, type, op2))
1961 if (op2.undefined_p ())
1962 r.set_undefined ();
1963 else
1964 r.set_varying (type);
1965 return true;
1968 // Transform left shifts by constants into multiplies.
1969 if (shift_range.singleton_p ())
1971 unsigned shift = shift_range.lower_bound ().to_uhwi ();
1972 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
1973 int_range<1> mult (type, tmp, tmp);
1975 // Force wrapping multiplication.
1976 bool saved_flag_wrapv = flag_wrapv;
1977 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
1978 flag_wrapv = 1;
1979 flag_wrapv_pointer = 1;
1980 bool b = op_mult.fold_range (r, type, op1, mult);
1981 flag_wrapv = saved_flag_wrapv;
1982 flag_wrapv_pointer = saved_flag_wrapv_pointer;
1983 return b;
1985 else
1986 // Otherwise, invoke the generic fold routine.
1987 return range_operator::fold_range (r, type, op1, shift_range, rel);
1990 void
1991 operator_lshift::wi_fold (irange &r, tree type,
1992 const wide_int &lh_lb, const wide_int &lh_ub,
1993 const wide_int &rh_lb, const wide_int &rh_ub) const
1995 signop sign = TYPE_SIGN (type);
1996 unsigned prec = TYPE_PRECISION (type);
1997 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
1998 int bound_shift = overflow_pos - rh_ub.to_shwi ();
1999 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2000 // overflow. However, for that to happen, rh.max needs to be zero,
2001 // which means rh is a singleton range of zero, which means we simply return
2002 // [lh_lb, lh_ub] as the range.
2003 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2005 r = int_range<2> (type, lh_lb, lh_ub);
2006 return;
2009 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2010 wide_int complement = ~(bound - 1);
2011 wide_int low_bound, high_bound;
2012 bool in_bounds = false;
2014 if (sign == UNSIGNED)
2016 low_bound = bound;
2017 high_bound = complement;
2018 if (wi::ltu_p (lh_ub, low_bound))
2020 // [5, 6] << [1, 2] == [10, 24].
2021 // We're shifting out only zeroes, the value increases
2022 // monotonically.
2023 in_bounds = true;
2025 else if (wi::ltu_p (high_bound, lh_lb))
2027 // [0xffffff00, 0xffffffff] << [1, 2]
2028 // == [0xfffffc00, 0xfffffffe].
2029 // We're shifting out only ones, the value decreases
2030 // monotonically.
2031 in_bounds = true;
2034 else
2036 // [-1, 1] << [1, 2] == [-4, 4]
2037 low_bound = complement;
2038 high_bound = bound;
2039 if (wi::lts_p (lh_ub, high_bound)
2040 && wi::lts_p (low_bound, lh_lb))
2042 // For non-negative numbers, we're shifting out only zeroes,
2043 // the value increases monotonically. For negative numbers,
2044 // we're shifting out only ones, the value decreases
2045 // monotonically.
2046 in_bounds = true;
2050 if (in_bounds)
2051 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2052 else
2053 r.set_varying (type);
2056 bool
2057 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2058 const wide_int &w0, const wide_int &w1) const
2060 signop sign = TYPE_SIGN (type);
2061 if (wi::neg_p (w1))
2063 // It's unclear from the C standard whether shifts can overflow.
2064 // The following code ignores overflow; perhaps a C standard
2065 // interpretation ruling is needed.
2066 res = wi::rshift (w0, -w1, sign);
2068 else
2069 res = wi::lshift (w0, w1);
2070 return false;
2073 bool
2074 operator_lshift::op1_range (irange &r,
2075 tree type,
2076 const irange &lhs,
2077 const irange &op2,
2078 relation_kind rel ATTRIBUTE_UNUSED) const
2080 tree shift_amount;
2082 if (!lhs.contains_p (build_zero_cst (type)))
2083 r.set_nonzero (type);
2084 else
2085 r.set_varying (type);
2087 if (op2.singleton_p (&shift_amount))
2089 wide_int shift = wi::to_wide (shift_amount);
2090 if (wi::lt_p (shift, 0, SIGNED))
2091 return false;
2092 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2093 TYPE_PRECISION (op2.type ())),
2094 UNSIGNED))
2095 return false;
2096 if (shift == 0)
2098 r.intersect (lhs);
2099 return true;
2102 // Work completely in unsigned mode to start.
2103 tree utype = type;
2104 int_range_max tmp_range;
2105 if (TYPE_SIGN (type) == SIGNED)
2107 int_range_max tmp = lhs;
2108 utype = unsigned_type_for (type);
2109 range_cast (tmp, utype);
2110 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2112 else
2113 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2115 // Start with ranges which can produce the LHS by right shifting the
2116 // result by the shift amount.
2117 // ie [0x08, 0xF0] = op1 << 2 will start with
2118 // [00001000, 11110000] = op1 << 2
2119 // [0x02, 0x4C] aka [00000010, 00111100]
2121 // Then create a range from the LB with the least significant upper bit
2122 // set, to the upper bound with all the bits set.
2123 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2125 // Ideally we do this for each subrange, but just lump them all for now.
2126 unsigned low_bits = TYPE_PRECISION (utype)
2127 - TREE_INT_CST_LOW (shift_amount);
2128 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2129 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2130 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2131 int_range<2> fill_range (utype, new_lb, new_ub);
2132 tmp_range.union_ (fill_range);
2134 if (utype != type)
2135 range_cast (tmp_range, type);
2137 r.intersect (tmp_range);
2138 return true;
2141 return !r.varying_p ();
2144 bool
2145 operator_rshift::op1_range (irange &r,
2146 tree type,
2147 const irange &lhs,
2148 const irange &op2,
2149 relation_kind rel ATTRIBUTE_UNUSED) const
2151 tree shift;
2152 if (op2.singleton_p (&shift))
2154 // Ignore nonsensical shifts.
2155 unsigned prec = TYPE_PRECISION (type);
2156 if (wi::ge_p (wi::to_wide (shift),
2157 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2158 UNSIGNED))
2159 return false;
2160 if (wi::to_wide (shift) == 0)
2162 r = lhs;
2163 return true;
2166 // Folding the original operation may discard some impossible
2167 // ranges from the LHS.
2168 int_range_max lhs_refined;
2169 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2170 lhs_refined.intersect (lhs);
2171 if (lhs_refined.undefined_p ())
2173 r.set_undefined ();
2174 return true;
2176 int_range_max shift_range (shift, shift);
2177 int_range_max lb, ub;
2178 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2179 // LHS
2180 // 0000 0111 = OP1 >> 3
2182 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2183 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2184 // right hand side (0x07).
2185 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2186 fold_build2 (LSHIFT_EXPR, type,
2187 build_minus_one_cst (type),
2188 shift));
2189 int_range_max mask_range (build_zero_cst (type), mask);
2190 op_plus.fold_range (ub, type, lb, mask_range);
2191 r = lb;
2192 r.union_ (ub);
2193 if (!lhs_refined.contains_p (build_zero_cst (type)))
2195 mask_range.invert ();
2196 r.intersect (mask_range);
2198 return true;
2200 return false;
2203 bool
2204 operator_rshift::wi_op_overflows (wide_int &res,
2205 tree type,
2206 const wide_int &w0,
2207 const wide_int &w1) const
2209 signop sign = TYPE_SIGN (type);
2210 if (wi::neg_p (w1))
2211 res = wi::lshift (w0, -w1);
2212 else
2214 // It's unclear from the C standard whether shifts can overflow.
2215 // The following code ignores overflow; perhaps a C standard
2216 // interpretation ruling is needed.
2217 res = wi::rshift (w0, w1, sign);
2219 return false;
2222 bool
2223 operator_rshift::fold_range (irange &r, tree type,
2224 const irange &op1,
2225 const irange &op2,
2226 relation_kind rel) const
2228 int_range_max shift;
2229 if (!get_shift_range (shift, type, op2))
2231 if (op2.undefined_p ())
2232 r.set_undefined ();
2233 else
2234 r.set_varying (type);
2235 return true;
2238 return range_operator::fold_range (r, type, op1, shift, rel);
2241 void
2242 operator_rshift::wi_fold (irange &r, tree type,
2243 const wide_int &lh_lb, const wide_int &lh_ub,
2244 const wide_int &rh_lb, const wide_int &rh_ub) const
2246 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2250 class operator_cast: public range_operator
2252 public:
2253 virtual bool fold_range (irange &r, tree type,
2254 const irange &op1,
2255 const irange &op2,
2256 relation_kind rel = VREL_NONE) const;
2257 virtual bool op1_range (irange &r, tree type,
2258 const irange &lhs,
2259 const irange &op2,
2260 relation_kind rel = VREL_NONE) const;
2261 private:
2262 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2263 bool inside_domain_p (const wide_int &min, const wide_int &max,
2264 const irange &outer) const;
2265 void fold_pair (irange &r, unsigned index, const irange &inner,
2266 const irange &outer) const;
2267 } op_convert;
2269 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2271 inline bool
2272 operator_cast::truncating_cast_p (const irange &inner,
2273 const irange &outer) const
2275 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2278 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2280 bool
2281 operator_cast::inside_domain_p (const wide_int &min,
2282 const wide_int &max,
2283 const irange &range) const
2285 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2286 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2287 signop domain_sign = TYPE_SIGN (range.type ());
2288 return (wi::le_p (min, domain_max, domain_sign)
2289 && wi::le_p (max, domain_max, domain_sign)
2290 && wi::ge_p (min, domain_min, domain_sign)
2291 && wi::ge_p (max, domain_min, domain_sign));
2295 // Helper for fold_range which work on a pair at a time.
2297 void
2298 operator_cast::fold_pair (irange &r, unsigned index,
2299 const irange &inner,
2300 const irange &outer) const
2302 tree inner_type = inner.type ();
2303 tree outer_type = outer.type ();
2304 signop inner_sign = TYPE_SIGN (inner_type);
2305 unsigned outer_prec = TYPE_PRECISION (outer_type);
2307 // check to see if casting from INNER to OUTER is a conversion that
2308 // fits in the resulting OUTER type.
2309 wide_int inner_lb = inner.lower_bound (index);
2310 wide_int inner_ub = inner.upper_bound (index);
2311 if (truncating_cast_p (inner, outer))
2313 // We may be able to accomodate a truncating cast if the
2314 // resulting range can be represented in the target type...
2315 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2316 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2317 inner_sign) != 0)
2319 r.set_varying (outer_type);
2320 return;
2323 // ...but we must still verify that the final range fits in the
2324 // domain. This catches -fstrict-enum restrictions where the domain
2325 // range is smaller than what fits in the underlying type.
2326 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2327 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2328 if (inside_domain_p (min, max, outer))
2329 create_possibly_reversed_range (r, outer_type, min, max);
2330 else
2331 r.set_varying (outer_type);
2335 bool
2336 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2337 const irange &inner,
2338 const irange &outer,
2339 relation_kind rel ATTRIBUTE_UNUSED) const
2341 if (empty_range_varying (r, type, inner, outer))
2342 return true;
2344 gcc_checking_assert (outer.varying_p ());
2345 gcc_checking_assert (inner.num_pairs () > 0);
2347 // Avoid a temporary by folding the first pair directly into the result.
2348 fold_pair (r, 0, inner, outer);
2350 // Then process any additonal pairs by unioning with their results.
2351 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2353 int_range_max tmp;
2354 fold_pair (tmp, x, inner, outer);
2355 r.union_ (tmp);
2356 if (r.varying_p ())
2357 return true;
2359 return true;
2362 bool
2363 operator_cast::op1_range (irange &r, tree type,
2364 const irange &lhs,
2365 const irange &op2,
2366 relation_kind rel ATTRIBUTE_UNUSED) const
2368 tree lhs_type = lhs.type ();
2369 gcc_checking_assert (types_compatible_p (op2.type(), type));
2371 // If we are calculating a pointer, shortcut to what we really care about.
2372 if (POINTER_TYPE_P (type))
2374 // Conversion from other pointers or a constant (including 0/NULL)
2375 // are straightforward.
2376 if (POINTER_TYPE_P (lhs.type ())
2377 || (lhs.singleton_p ()
2378 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2380 r = lhs;
2381 range_cast (r, type);
2383 else
2385 // If the LHS is not a pointer nor a singleton, then it is
2386 // either VARYING or non-zero.
2387 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2388 r.set_nonzero (type);
2389 else
2390 r.set_varying (type);
2392 r.intersect (op2);
2393 return true;
2396 if (truncating_cast_p (op2, lhs))
2398 if (lhs.varying_p ())
2399 r.set_varying (type);
2400 else
2402 // We want to insert the LHS as an unsigned value since it
2403 // would not trigger the signed bit of the larger type.
2404 int_range_max converted_lhs = lhs;
2405 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2406 range_cast (converted_lhs, type);
2407 // Start by building the positive signed outer range for the type.
2408 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2409 TYPE_PRECISION (type));
2410 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2411 SIGNED));
2412 // For the signed part, we need to simply union the 2 ranges now.
2413 r.union_ (converted_lhs);
2415 // Create maximal negative number outside of LHS bits.
2416 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2417 TYPE_PRECISION (type));
2418 // Add this to the unsigned LHS range(s).
2419 int_range_max lim_range (type, lim, lim);
2420 int_range_max lhs_neg;
2421 range_op_handler (PLUS_EXPR, type)->fold_range (lhs_neg,
2422 type,
2423 converted_lhs,
2424 lim_range);
2425 // lhs_neg now has all the negative versions of the LHS.
2426 // Now union in all the values from SIGNED MIN (0x80000) to
2427 // lim-1 in order to fill in all the ranges with the upper
2428 // bits set.
2430 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2431 // we don't need to create a range from min to lim-1
2432 // calculate neg range traps trying to create [lim, lim - 1].
2433 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2434 if (lim != min_val)
2436 int_range_max neg (type,
2437 wi::min_value (TYPE_PRECISION (type),
2438 SIGNED),
2439 lim - 1);
2440 lhs_neg.union_ (neg);
2442 // And finally, munge the signed and unsigned portions.
2443 r.union_ (lhs_neg);
2445 // And intersect with any known value passed in the extra operand.
2446 r.intersect (op2);
2447 return true;
2450 int_range_max tmp;
2451 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2452 tmp = lhs;
2453 else
2455 // The cast is not truncating, and the range is restricted to
2456 // the range of the RHS by this assignment.
2458 // Cast the range of the RHS to the type of the LHS.
2459 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2460 // Intersect this with the LHS range will produce the range,
2461 // which will be cast to the RHS type before returning.
2462 tmp.intersect (lhs);
2465 // Cast the calculated range to the type of the RHS.
2466 fold_range (r, type, tmp, int_range<1> (type));
2467 return true;
2471 class operator_logical_and : public range_operator
2473 public:
2474 virtual bool fold_range (irange &r, tree type,
2475 const irange &lh,
2476 const irange &rh,
2477 relation_kind rel = VREL_NONE) const;
2478 virtual bool op1_range (irange &r, tree type,
2479 const irange &lhs,
2480 const irange &op2,
2481 relation_kind rel = VREL_NONE) const;
2482 virtual bool op2_range (irange &r, tree type,
2483 const irange &lhs,
2484 const irange &op1,
2485 relation_kind rel = VREL_NONE) const;
2486 } op_logical_and;
2489 bool
2490 operator_logical_and::fold_range (irange &r, tree type,
2491 const irange &lh,
2492 const irange &rh,
2493 relation_kind rel ATTRIBUTE_UNUSED) const
2495 if (empty_range_varying (r, type, lh, rh))
2496 return true;
2498 // 0 && anything is 0.
2499 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2500 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2501 r = range_false (type);
2502 else if (lh.contains_p (build_zero_cst (lh.type ()))
2503 || rh.contains_p (build_zero_cst (rh.type ())))
2504 // To reach this point, there must be a logical 1 on each side, and
2505 // the only remaining question is whether there is a zero or not.
2506 r = range_true_and_false (type);
2507 else
2508 r = range_true (type);
2509 return true;
2512 bool
2513 operator_logical_and::op1_range (irange &r, tree type,
2514 const irange &lhs,
2515 const irange &op2 ATTRIBUTE_UNUSED,
2516 relation_kind rel ATTRIBUTE_UNUSED) const
2518 switch (get_bool_state (r, lhs, type))
2520 case BRS_TRUE:
2521 // A true result means both sides of the AND must be true.
2522 r = range_true (type);
2523 break;
2524 default:
2525 // Any other result means only one side has to be false, the
2526 // other side can be anything. So we cannott be sure of any
2527 // result here.
2528 r = range_true_and_false (type);
2529 break;
2531 return true;
2534 bool
2535 operator_logical_and::op2_range (irange &r, tree type,
2536 const irange &lhs,
2537 const irange &op1,
2538 relation_kind rel ATTRIBUTE_UNUSED) const
2540 return operator_logical_and::op1_range (r, type, lhs, op1);
2544 class operator_bitwise_and : public range_operator
2546 public:
2547 virtual bool fold_range (irange &r, tree type,
2548 const irange &lh,
2549 const irange &rh,
2550 relation_kind rel = VREL_NONE) const;
2551 virtual bool op1_range (irange &r, tree type,
2552 const irange &lhs,
2553 const irange &op2,
2554 relation_kind rel = VREL_NONE) const;
2555 virtual bool op2_range (irange &r, tree type,
2556 const irange &lhs,
2557 const irange &op1,
2558 relation_kind rel = VREL_NONE) const;
2559 virtual void wi_fold (irange &r, tree type,
2560 const wide_int &lh_lb,
2561 const wide_int &lh_ub,
2562 const wide_int &rh_lb,
2563 const wide_int &rh_ub) const;
2564 private:
2565 void simple_op1_range_solver (irange &r, tree type,
2566 const irange &lhs,
2567 const irange &op2) const;
2568 void remove_impossible_ranges (irange &r, const irange &rh) const;
2569 } op_bitwise_and;
2571 static bool
2572 unsigned_singleton_p (const irange &op)
2574 tree mask;
2575 if (op.singleton_p (&mask))
2577 wide_int x = wi::to_wide (mask);
2578 return wi::ge_p (x, 0, TYPE_SIGN (op.type ()));
2580 return false;
2583 // Remove any ranges from R that are known to be impossible when an
2584 // range is ANDed with MASK.
2586 void
2587 operator_bitwise_and::remove_impossible_ranges (irange &r,
2588 const irange &rmask) const
2590 if (r.undefined_p () || !unsigned_singleton_p (rmask))
2591 return;
2593 wide_int mask = rmask.lower_bound ();
2594 tree type = r.type ();
2595 int prec = TYPE_PRECISION (type);
2596 int leading_zeros = wi::clz (mask);
2597 int_range_max impossible_ranges;
2599 /* We know that starting at the most significant bit, any 0 in the
2600 mask means the resulting range cannot contain a 1 in that same
2601 position. This means the following ranges are impossible:
2603 x & 0b1001 1010
2604 IMPOSSIBLE RANGES
2605 01xx xxxx [0100 0000, 0111 1111]
2606 001x xxxx [0010 0000, 0011 1111]
2607 0000 01xx [0000 0100, 0000 0111]
2608 0000 0001 [0000 0001, 0000 0001]
2610 wide_int one = wi::one (prec);
2611 for (int i = 0; i < prec - leading_zeros - 1; ++i)
2612 if (wi::bit_and (mask, wi::lshift (one, wi::uhwi (i, prec))) == 0)
2614 tree lb = fold_build2 (LSHIFT_EXPR, type,
2615 build_one_cst (type),
2616 build_int_cst (type, i));
2617 tree ub_left = fold_build1 (BIT_NOT_EXPR, type,
2618 fold_build2 (LSHIFT_EXPR, type,
2619 build_minus_one_cst (type),
2620 build_int_cst (type, i)));
2621 tree ub_right = fold_build2 (LSHIFT_EXPR, type,
2622 build_one_cst (type),
2623 build_int_cst (type, i));
2624 tree ub = fold_build2 (BIT_IOR_EXPR, type, ub_left, ub_right);
2625 impossible_ranges.union_ (int_range<1> (lb, ub));
2627 if (!impossible_ranges.undefined_p ())
2629 impossible_ranges.invert ();
2630 r.intersect (impossible_ranges);
2634 bool
2635 operator_bitwise_and::fold_range (irange &r, tree type,
2636 const irange &lh,
2637 const irange &rh,
2638 relation_kind rel ATTRIBUTE_UNUSED) const
2640 if (range_operator::fold_range (r, type, lh, rh))
2642 // FIXME: This is temporarily disabled because, though it
2643 // generates better ranges, it's noticeably slower for evrp.
2644 // remove_impossible_ranges (r, rh);
2645 return true;
2647 return false;
2651 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2652 // possible. Basically, see if we can optimize:
2654 // [LB, UB] op Z
2655 // into:
2656 // [LB op Z, UB op Z]
2658 // If the optimization was successful, accumulate the range in R and
2659 // return TRUE.
2661 static bool
2662 wi_optimize_and_or (irange &r,
2663 enum tree_code code,
2664 tree type,
2665 const wide_int &lh_lb, const wide_int &lh_ub,
2666 const wide_int &rh_lb, const wide_int &rh_ub)
2668 // Calculate the singleton mask among the ranges, if any.
2669 wide_int lower_bound, upper_bound, mask;
2670 if (wi::eq_p (rh_lb, rh_ub))
2672 mask = rh_lb;
2673 lower_bound = lh_lb;
2674 upper_bound = lh_ub;
2676 else if (wi::eq_p (lh_lb, lh_ub))
2678 mask = lh_lb;
2679 lower_bound = rh_lb;
2680 upper_bound = rh_ub;
2682 else
2683 return false;
2685 // If Z is a constant which (for op | its bitwise not) has n
2686 // consecutive least significant bits cleared followed by m 1
2687 // consecutive bits set immediately above it and either
2688 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2690 // The least significant n bits of all the values in the range are
2691 // cleared or set, the m bits above it are preserved and any bits
2692 // above these are required to be the same for all values in the
2693 // range.
2694 wide_int w = mask;
2695 int m = 0, n = 0;
2696 if (code == BIT_IOR_EXPR)
2697 w = ~w;
2698 if (wi::eq_p (w, 0))
2699 n = w.get_precision ();
2700 else
2702 n = wi::ctz (w);
2703 w = ~(w | wi::mask (n, false, w.get_precision ()));
2704 if (wi::eq_p (w, 0))
2705 m = w.get_precision () - n;
2706 else
2707 m = wi::ctz (w) - n;
2709 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2710 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2711 return false;
2713 wide_int res_lb, res_ub;
2714 if (code == BIT_AND_EXPR)
2716 res_lb = wi::bit_and (lower_bound, mask);
2717 res_ub = wi::bit_and (upper_bound, mask);
2719 else if (code == BIT_IOR_EXPR)
2721 res_lb = wi::bit_or (lower_bound, mask);
2722 res_ub = wi::bit_or (upper_bound, mask);
2724 else
2725 gcc_unreachable ();
2726 value_range_with_overflow (r, type, res_lb, res_ub);
2728 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2729 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2731 int_range<2> tmp;
2732 tmp.set_nonzero (type);
2733 r.intersect (tmp);
2735 return true;
2738 // For range [LB, UB] compute two wide_int bit masks.
2740 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2741 // for all numbers in the range the bit is 0, otherwise it might be 0
2742 // or 1.
2744 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2745 // for all numbers in the range the bit is 1, otherwise it might be 0
2746 // or 1.
2748 void
2749 wi_set_zero_nonzero_bits (tree type,
2750 const wide_int &lb, const wide_int &ub,
2751 wide_int &maybe_nonzero,
2752 wide_int &mustbe_nonzero)
2754 signop sign = TYPE_SIGN (type);
2756 if (wi::eq_p (lb, ub))
2757 maybe_nonzero = mustbe_nonzero = lb;
2758 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2760 wide_int xor_mask = lb ^ ub;
2761 maybe_nonzero = lb | ub;
2762 mustbe_nonzero = lb & ub;
2763 if (xor_mask != 0)
2765 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2766 maybe_nonzero.get_precision ());
2767 maybe_nonzero = maybe_nonzero | mask;
2768 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2771 else
2773 maybe_nonzero = wi::minus_one (lb.get_precision ());
2774 mustbe_nonzero = wi::zero (lb.get_precision ());
2778 void
2779 operator_bitwise_and::wi_fold (irange &r, tree type,
2780 const wide_int &lh_lb,
2781 const wide_int &lh_ub,
2782 const wide_int &rh_lb,
2783 const wide_int &rh_ub) const
2785 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2786 return;
2788 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2789 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2790 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2791 maybe_nonzero_lh, mustbe_nonzero_lh);
2792 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2793 maybe_nonzero_rh, mustbe_nonzero_rh);
2795 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2796 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2797 signop sign = TYPE_SIGN (type);
2798 unsigned prec = TYPE_PRECISION (type);
2799 // If both input ranges contain only negative values, we can
2800 // truncate the result range maximum to the minimum of the
2801 // input range maxima.
2802 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2804 new_ub = wi::min (new_ub, lh_ub, sign);
2805 new_ub = wi::min (new_ub, rh_ub, sign);
2807 // If either input range contains only non-negative values
2808 // we can truncate the result range maximum to the respective
2809 // maximum of the input range.
2810 if (wi::ge_p (lh_lb, 0, sign))
2811 new_ub = wi::min (new_ub, lh_ub, sign);
2812 if (wi::ge_p (rh_lb, 0, sign))
2813 new_ub = wi::min (new_ub, rh_ub, sign);
2814 // PR68217: In case of signed & sign-bit-CST should
2815 // result in [-INF, 0] instead of [-INF, INF].
2816 if (wi::gt_p (new_lb, new_ub, sign))
2818 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2819 if (sign == SIGNED
2820 && ((wi::eq_p (lh_lb, lh_ub)
2821 && !wi::cmps (lh_lb, sign_bit))
2822 || (wi::eq_p (rh_lb, rh_ub)
2823 && !wi::cmps (rh_lb, sign_bit))))
2825 new_lb = wi::min_value (prec, sign);
2826 new_ub = wi::zero (prec);
2829 // If the limits got swapped around, return varying.
2830 if (wi::gt_p (new_lb, new_ub,sign))
2831 r.set_varying (type);
2832 else
2833 value_range_with_overflow (r, type, new_lb, new_ub);
2836 static void
2837 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2839 if (!lhs.contains_p (build_zero_cst (type)))
2840 r = range_nonzero (type);
2841 else
2842 r.set_varying (type);
2845 // This was shamelessly stolen from register_edge_assert_for_2 and
2846 // adjusted to work with iranges.
2848 void
2849 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2850 const irange &lhs,
2851 const irange &op2) const
2853 if (!op2.singleton_p ())
2855 set_nonzero_range_from_mask (r, type, lhs);
2856 return;
2858 unsigned int nprec = TYPE_PRECISION (type);
2859 wide_int cst2v = op2.lower_bound ();
2860 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2861 wide_int sgnbit;
2862 if (cst2n)
2863 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2864 else
2865 sgnbit = wi::zero (nprec);
2867 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2869 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2870 // maximum unsigned value is ~0. For signed comparison, if CST2
2871 // doesn't have the most significant bit set, handle it similarly. If
2872 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2873 wide_int valv = lhs.lower_bound ();
2874 wide_int minv = valv & cst2v, maxv;
2875 bool we_know_nothing = false;
2876 if (minv != valv)
2878 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2879 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2880 if (minv == valv)
2882 // If we can't determine anything on this bound, fall
2883 // through and conservatively solve for the other end point.
2884 we_know_nothing = true;
2887 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2888 if (we_know_nothing)
2889 r.set_varying (type);
2890 else
2891 r = int_range<1> (type, minv, maxv);
2893 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2895 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2896 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2897 // VAL2 where
2898 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2899 // as maximum.
2900 // For signed comparison, if CST2 doesn't have most significant bit
2901 // set, handle it similarly. If CST2 has MSB set, the maximum is
2902 // the same and minimum is INT_MIN.
2903 valv = lhs.upper_bound ();
2904 minv = valv & cst2v;
2905 if (minv == valv)
2906 maxv = valv;
2907 else
2909 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2910 if (maxv == valv)
2912 // If we couldn't determine anything on either bound, return
2913 // undefined.
2914 if (we_know_nothing)
2915 r.set_undefined ();
2916 return;
2918 maxv -= 1;
2920 maxv |= ~cst2v;
2921 minv = sgnbit;
2922 int_range<1> upper_bits (type, minv, maxv);
2923 r.intersect (upper_bits);
2926 bool
2927 operator_bitwise_and::op1_range (irange &r, tree type,
2928 const irange &lhs,
2929 const irange &op2,
2930 relation_kind rel ATTRIBUTE_UNUSED) const
2932 if (types_compatible_p (type, boolean_type_node))
2933 return op_logical_and.op1_range (r, type, lhs, op2);
2935 r.set_undefined ();
2936 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
2938 int_range_max chunk (lhs.type (),
2939 lhs.lower_bound (i),
2940 lhs.upper_bound (i));
2941 int_range_max res;
2942 simple_op1_range_solver (res, type, chunk, op2);
2943 r.union_ (res);
2945 if (r.undefined_p ())
2946 set_nonzero_range_from_mask (r, type, lhs);
2947 return true;
2950 bool
2951 operator_bitwise_and::op2_range (irange &r, tree type,
2952 const irange &lhs,
2953 const irange &op1,
2954 relation_kind rel ATTRIBUTE_UNUSED) const
2956 return operator_bitwise_and::op1_range (r, type, lhs, op1);
2960 class operator_logical_or : public range_operator
2962 public:
2963 virtual bool fold_range (irange &r, tree type,
2964 const irange &lh,
2965 const irange &rh,
2966 relation_kind rel = VREL_NONE) const;
2967 virtual bool op1_range (irange &r, tree type,
2968 const irange &lhs,
2969 const irange &op2,
2970 relation_kind rel = VREL_NONE) const;
2971 virtual bool op2_range (irange &r, tree type,
2972 const irange &lhs,
2973 const irange &op1,
2974 relation_kind rel = VREL_NONE) const;
2975 } op_logical_or;
2977 bool
2978 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2979 const irange &lh,
2980 const irange &rh,
2981 relation_kind rel ATTRIBUTE_UNUSED) const
2983 if (empty_range_varying (r, type, lh, rh))
2984 return true;
2986 r = lh;
2987 r.union_ (rh);
2988 return true;
2991 bool
2992 operator_logical_or::op1_range (irange &r, tree type,
2993 const irange &lhs,
2994 const irange &op2 ATTRIBUTE_UNUSED,
2995 relation_kind rel ATTRIBUTE_UNUSED) const
2997 switch (get_bool_state (r, lhs, type))
2999 case BRS_FALSE:
3000 // A false result means both sides of the OR must be false.
3001 r = range_false (type);
3002 break;
3003 default:
3004 // Any other result means only one side has to be true, the
3005 // other side can be anything. so we can't be sure of any result
3006 // here.
3007 r = range_true_and_false (type);
3008 break;
3010 return true;
3013 bool
3014 operator_logical_or::op2_range (irange &r, tree type,
3015 const irange &lhs,
3016 const irange &op1,
3017 relation_kind rel ATTRIBUTE_UNUSED) const
3019 return operator_logical_or::op1_range (r, type, lhs, op1);
3023 class operator_bitwise_or : public range_operator
3025 public:
3026 virtual bool op1_range (irange &r, tree type,
3027 const irange &lhs,
3028 const irange &op2,
3029 relation_kind rel = VREL_NONE) const;
3030 virtual bool op2_range (irange &r, tree type,
3031 const irange &lhs,
3032 const irange &op1,
3033 relation_kind rel= VREL_NONE) const;
3034 virtual void wi_fold (irange &r, tree type,
3035 const wide_int &lh_lb,
3036 const wide_int &lh_ub,
3037 const wide_int &rh_lb,
3038 const wide_int &rh_ub) const;
3039 } op_bitwise_or;
3041 void
3042 operator_bitwise_or::wi_fold (irange &r, tree type,
3043 const wide_int &lh_lb,
3044 const wide_int &lh_ub,
3045 const wide_int &rh_lb,
3046 const wide_int &rh_ub) const
3048 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3049 return;
3051 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3052 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3053 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3054 maybe_nonzero_lh, mustbe_nonzero_lh);
3055 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3056 maybe_nonzero_rh, mustbe_nonzero_rh);
3057 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3058 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3059 signop sign = TYPE_SIGN (type);
3060 // If the input ranges contain only positive values we can
3061 // truncate the minimum of the result range to the maximum
3062 // of the input range minima.
3063 if (wi::ge_p (lh_lb, 0, sign)
3064 && wi::ge_p (rh_lb, 0, sign))
3066 new_lb = wi::max (new_lb, lh_lb, sign);
3067 new_lb = wi::max (new_lb, rh_lb, sign);
3069 // If either input range contains only negative values
3070 // we can truncate the minimum of the result range to the
3071 // respective minimum range.
3072 if (wi::lt_p (lh_ub, 0, sign))
3073 new_lb = wi::max (new_lb, lh_lb, sign);
3074 if (wi::lt_p (rh_ub, 0, sign))
3075 new_lb = wi::max (new_lb, rh_lb, sign);
3076 // If the limits got swapped around, return a conservative range.
3077 if (wi::gt_p (new_lb, new_ub, sign))
3079 // Make sure that nonzero|X is nonzero.
3080 if (wi::gt_p (lh_lb, 0, sign)
3081 || wi::gt_p (rh_lb, 0, sign)
3082 || wi::lt_p (lh_ub, 0, sign)
3083 || wi::lt_p (rh_ub, 0, sign))
3084 r.set_nonzero (type);
3085 else
3086 r.set_varying (type);
3087 return;
3089 value_range_with_overflow (r, type, new_lb, new_ub);
3092 bool
3093 operator_bitwise_or::op1_range (irange &r, tree type,
3094 const irange &lhs,
3095 const irange &op2,
3096 relation_kind rel ATTRIBUTE_UNUSED) const
3098 // If this is really a logical wi_fold, call that.
3099 if (types_compatible_p (type, boolean_type_node))
3100 return op_logical_or.op1_range (r, type, lhs, op2);
3102 if (lhs.zero_p ())
3104 tree zero = build_zero_cst (type);
3105 r = int_range<1> (zero, zero);
3106 return true;
3108 r.set_varying (type);
3109 return true;
3112 bool
3113 operator_bitwise_or::op2_range (irange &r, tree type,
3114 const irange &lhs,
3115 const irange &op1,
3116 relation_kind rel ATTRIBUTE_UNUSED) const
3118 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3122 class operator_bitwise_xor : public range_operator
3124 public:
3125 virtual void wi_fold (irange &r, tree type,
3126 const wide_int &lh_lb,
3127 const wide_int &lh_ub,
3128 const wide_int &rh_lb,
3129 const wide_int &rh_ub) const;
3130 virtual bool op1_range (irange &r, tree type,
3131 const irange &lhs,
3132 const irange &op2,
3133 relation_kind rel = VREL_NONE) const;
3134 virtual bool op2_range (irange &r, tree type,
3135 const irange &lhs,
3136 const irange &op1,
3137 relation_kind rel = VREL_NONE) const;
3138 virtual bool op1_op2_relation_effect (irange &lhs_range,
3139 tree type,
3140 const irange &op1_range,
3141 const irange &op2_range,
3142 relation_kind rel) const;
3143 } op_bitwise_xor;
3145 void
3146 operator_bitwise_xor::wi_fold (irange &r, tree type,
3147 const wide_int &lh_lb,
3148 const wide_int &lh_ub,
3149 const wide_int &rh_lb,
3150 const wide_int &rh_ub) const
3152 signop sign = TYPE_SIGN (type);
3153 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3154 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3155 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3156 maybe_nonzero_lh, mustbe_nonzero_lh);
3157 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3158 maybe_nonzero_rh, mustbe_nonzero_rh);
3160 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3161 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3162 wide_int result_one_bits
3163 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3164 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3165 wide_int new_ub = ~result_zero_bits;
3166 wide_int new_lb = result_one_bits;
3168 // If the range has all positive or all negative values, the result
3169 // is better than VARYING.
3170 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3171 value_range_with_overflow (r, type, new_lb, new_ub);
3172 else
3173 r.set_varying (type);
3176 bool
3177 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3178 tree type,
3179 const irange &,
3180 const irange &,
3181 relation_kind rel) const
3183 if (rel == VREL_NONE)
3184 return false;
3186 int_range<2> rel_range;
3188 switch (rel)
3190 case EQ_EXPR:
3191 rel_range.set_zero (type);
3192 break;
3193 case NE_EXPR:
3194 rel_range.set_nonzero (type);
3195 break;
3196 default:
3197 return false;
3200 lhs_range.intersect (rel_range);
3201 return true;
3204 bool
3205 operator_bitwise_xor::op1_range (irange &r, tree type,
3206 const irange &lhs,
3207 const irange &op2,
3208 relation_kind rel ATTRIBUTE_UNUSED) const
3210 if (lhs.undefined_p () || lhs.varying_p ())
3212 r = lhs;
3213 return true;
3215 if (types_compatible_p (type, boolean_type_node))
3217 switch (get_bool_state (r, lhs, type))
3219 case BRS_TRUE:
3220 if (op2.varying_p ())
3221 r.set_varying (type);
3222 else if (op2.zero_p ())
3223 r = range_true (type);
3224 else
3225 r = range_false (type);
3226 break;
3227 case BRS_FALSE:
3228 r = op2;
3229 break;
3230 default:
3231 break;
3233 return true;
3235 r.set_varying (type);
3236 return true;
3239 bool
3240 operator_bitwise_xor::op2_range (irange &r, tree type,
3241 const irange &lhs,
3242 const irange &op1,
3243 relation_kind rel ATTRIBUTE_UNUSED) const
3245 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3248 class operator_trunc_mod : public range_operator
3250 public:
3251 virtual void wi_fold (irange &r, tree type,
3252 const wide_int &lh_lb,
3253 const wide_int &lh_ub,
3254 const wide_int &rh_lb,
3255 const wide_int &rh_ub) const;
3256 virtual bool op1_range (irange &r, tree type,
3257 const irange &lhs,
3258 const irange &op2,
3259 relation_kind rel ATTRIBUTE_UNUSED) const;
3260 virtual bool op2_range (irange &r, tree type,
3261 const irange &lhs,
3262 const irange &op1,
3263 relation_kind rel ATTRIBUTE_UNUSED) const;
3264 } op_trunc_mod;
3266 void
3267 operator_trunc_mod::wi_fold (irange &r, tree type,
3268 const wide_int &lh_lb,
3269 const wide_int &lh_ub,
3270 const wide_int &rh_lb,
3271 const wide_int &rh_ub) const
3273 wide_int new_lb, new_ub, tmp;
3274 signop sign = TYPE_SIGN (type);
3275 unsigned prec = TYPE_PRECISION (type);
3277 // Mod 0 is undefined.
3278 if (wi_zero_p (type, rh_lb, rh_ub))
3280 r.set_undefined ();
3281 return;
3284 // Check for constant and try to fold.
3285 if (lh_lb == lh_ub && rh_lb == rh_ub)
3287 wi::overflow_type ov = wi::OVF_NONE;
3288 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3289 if (ov == wi::OVF_NONE)
3291 r = int_range<2> (type, tmp, tmp);
3292 return;
3296 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3297 new_ub = rh_ub - 1;
3298 if (sign == SIGNED)
3300 tmp = -1 - rh_lb;
3301 new_ub = wi::smax (new_ub, tmp);
3304 if (sign == UNSIGNED)
3305 new_lb = wi::zero (prec);
3306 else
3308 new_lb = -new_ub;
3309 tmp = lh_lb;
3310 if (wi::gts_p (tmp, 0))
3311 tmp = wi::zero (prec);
3312 new_lb = wi::smax (new_lb, tmp);
3314 tmp = lh_ub;
3315 if (sign == SIGNED && wi::neg_p (tmp))
3316 tmp = wi::zero (prec);
3317 new_ub = wi::min (new_ub, tmp, sign);
3319 value_range_with_overflow (r, type, new_lb, new_ub);
3322 bool
3323 operator_trunc_mod::op1_range (irange &r, tree type,
3324 const irange &lhs,
3325 const irange &,
3326 relation_kind rel ATTRIBUTE_UNUSED) const
3328 // PR 91029.
3329 signop sign = TYPE_SIGN (type);
3330 unsigned prec = TYPE_PRECISION (type);
3331 // (a % b) >= x && x > 0 , then a >= x.
3332 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3334 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3335 return true;
3337 // (a % b) <= x && x < 0 , then a <= x.
3338 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3340 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3341 return true;
3343 return false;
3346 bool
3347 operator_trunc_mod::op2_range (irange &r, tree type,
3348 const irange &lhs,
3349 const irange &,
3350 relation_kind rel ATTRIBUTE_UNUSED) const
3352 // PR 91029.
3353 signop sign = TYPE_SIGN (type);
3354 unsigned prec = TYPE_PRECISION (type);
3355 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3356 // or b > x for unsigned.
3357 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3359 if (sign == SIGNED)
3360 r = value_range (type, wi::neg (lhs.lower_bound ()),
3361 lhs.lower_bound (), VR_ANTI_RANGE);
3362 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3363 sign))
3364 r = value_range (type, lhs.lower_bound () + 1,
3365 wi::max_value (prec, sign));
3366 else
3367 return false;
3368 return true;
3370 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3371 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3373 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3374 r = value_range (type, lhs.upper_bound (),
3375 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3376 else
3377 return false;
3378 return true;
3380 return false;
3384 class operator_logical_not : public range_operator
3386 public:
3387 virtual bool fold_range (irange &r, tree type,
3388 const irange &lh,
3389 const irange &rh,
3390 relation_kind rel = VREL_NONE) const;
3391 virtual bool op1_range (irange &r, tree type,
3392 const irange &lhs,
3393 const irange &op2,
3394 relation_kind rel = VREL_NONE) const;
3395 } op_logical_not;
3397 // Folding a logical NOT, oddly enough, involves doing nothing on the
3398 // forward pass through. During the initial walk backwards, the
3399 // logical NOT reversed the desired outcome on the way back, so on the
3400 // way forward all we do is pass the range forward.
3402 // b_2 = x_1 < 20
3403 // b_3 = !b_2
3404 // if (b_3)
3405 // to determine the TRUE branch, walking backward
3406 // if (b_3) if ([1,1])
3407 // b_3 = !b_2 [1,1] = ![0,0]
3408 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3409 // which is the result we are looking for.. so.. pass it through.
3411 bool
3412 operator_logical_not::fold_range (irange &r, tree type,
3413 const irange &lh,
3414 const irange &rh ATTRIBUTE_UNUSED,
3415 relation_kind rel ATTRIBUTE_UNUSED) const
3417 if (empty_range_varying (r, type, lh, rh))
3418 return true;
3420 r = lh;
3421 if (!lh.varying_p () && !lh.undefined_p ())
3422 r.invert ();
3424 return true;
3427 bool
3428 operator_logical_not::op1_range (irange &r,
3429 tree type,
3430 const irange &lhs,
3431 const irange &op2,
3432 relation_kind rel ATTRIBUTE_UNUSED) const
3434 // Logical NOT is involutary...do it again.
3435 return fold_range (r, type, lhs, op2);
3439 class operator_bitwise_not : public range_operator
3441 public:
3442 virtual bool fold_range (irange &r, tree type,
3443 const irange &lh,
3444 const irange &rh,
3445 relation_kind rel = VREL_NONE) const;
3446 virtual bool op1_range (irange &r, tree type,
3447 const irange &lhs,
3448 const irange &op2,
3449 relation_kind rel = VREL_NONE) const;
3450 } op_bitwise_not;
3452 bool
3453 operator_bitwise_not::fold_range (irange &r, tree type,
3454 const irange &lh,
3455 const irange &rh,
3456 relation_kind rel ATTRIBUTE_UNUSED) const
3458 if (empty_range_varying (r, type, lh, rh))
3459 return true;
3461 if (types_compatible_p (type, boolean_type_node))
3462 return op_logical_not.fold_range (r, type, lh, rh);
3464 // ~X is simply -1 - X.
3465 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3466 wi::minus_one (TYPE_PRECISION (type)));
3467 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, minusone,
3468 lh);
3471 bool
3472 operator_bitwise_not::op1_range (irange &r, tree type,
3473 const irange &lhs,
3474 const irange &op2,
3475 relation_kind rel ATTRIBUTE_UNUSED) const
3477 if (types_compatible_p (type, boolean_type_node))
3478 return op_logical_not.op1_range (r, type, lhs, op2);
3480 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3481 return fold_range (r, type, lhs, op2);
3485 class operator_cst : public range_operator
3487 public:
3488 virtual bool fold_range (irange &r, tree type,
3489 const irange &op1,
3490 const irange &op2,
3491 relation_kind rel = VREL_NONE) const;
3492 } op_integer_cst;
3494 bool
3495 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3496 const irange &lh,
3497 const irange &rh ATTRIBUTE_UNUSED,
3498 relation_kind rel ATTRIBUTE_UNUSED) const
3500 r = lh;
3501 return true;
3505 class operator_identity : public range_operator
3507 public:
3508 virtual bool fold_range (irange &r, tree type,
3509 const irange &op1,
3510 const irange &op2,
3511 relation_kind rel = VREL_NONE) const;
3512 virtual bool op1_range (irange &r, tree type,
3513 const irange &lhs,
3514 const irange &op2,
3515 relation_kind rel = VREL_NONE) const;
3516 virtual enum tree_code lhs_op1_relation (const irange &lhs,
3517 const irange &op1,
3518 const irange &op2) const;
3519 } op_identity;
3521 // Determine if there is a relationship between LHS and OP1.
3523 enum tree_code
3524 operator_identity::lhs_op1_relation (const irange &lhs,
3525 const irange &op1 ATTRIBUTE_UNUSED,
3526 const irange &op2 ATTRIBUTE_UNUSED) const
3528 if (lhs.undefined_p ())
3529 return VREL_NONE;
3530 // Simply a copy, so they are equivalent.
3531 return EQ_EXPR;
3534 bool
3535 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3536 const irange &lh,
3537 const irange &rh ATTRIBUTE_UNUSED,
3538 relation_kind rel ATTRIBUTE_UNUSED) const
3540 r = lh;
3541 return true;
3544 bool
3545 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3546 const irange &lhs,
3547 const irange &op2 ATTRIBUTE_UNUSED,
3548 relation_kind rel ATTRIBUTE_UNUSED) const
3550 r = lhs;
3551 return true;
3555 class operator_unknown : public range_operator
3557 public:
3558 virtual bool fold_range (irange &r, tree type,
3559 const irange &op1,
3560 const irange &op2,
3561 relation_kind rel = VREL_NONE) const;
3562 } op_unknown;
3564 bool
3565 operator_unknown::fold_range (irange &r, tree type,
3566 const irange &lh ATTRIBUTE_UNUSED,
3567 const irange &rh ATTRIBUTE_UNUSED,
3568 relation_kind rel ATTRIBUTE_UNUSED) const
3570 r.set_varying (type);
3571 return true;
3575 class operator_abs : public range_operator
3577 public:
3578 virtual void wi_fold (irange &r, tree type,
3579 const wide_int &lh_lb,
3580 const wide_int &lh_ub,
3581 const wide_int &rh_lb,
3582 const wide_int &rh_ub) const;
3583 virtual bool op1_range (irange &r, tree type,
3584 const irange &lhs,
3585 const irange &op2,
3586 relation_kind rel ATTRIBUTE_UNUSED) const;
3587 } op_abs;
3589 void
3590 operator_abs::wi_fold (irange &r, tree type,
3591 const wide_int &lh_lb, const wide_int &lh_ub,
3592 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3593 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3595 wide_int min, max;
3596 signop sign = TYPE_SIGN (type);
3597 unsigned prec = TYPE_PRECISION (type);
3599 // Pass through LH for the easy cases.
3600 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3602 r = int_range<1> (type, lh_lb, lh_ub);
3603 return;
3606 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3607 // a useful range.
3608 wide_int min_value = wi::min_value (prec, sign);
3609 wide_int max_value = wi::max_value (prec, sign);
3610 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3612 r.set_varying (type);
3613 return;
3616 // ABS_EXPR may flip the range around, if the original range
3617 // included negative values.
3618 if (wi::eq_p (lh_lb, min_value))
3620 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3621 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3622 if (wi::eq_p (lh_ub, min_value))
3624 r = int_range<1> (type, min_value, min_value);
3625 return;
3627 min = max_value;
3629 else
3630 min = wi::abs (lh_lb);
3632 if (wi::eq_p (lh_ub, min_value))
3633 max = max_value;
3634 else
3635 max = wi::abs (lh_ub);
3637 // If the range contains zero then we know that the minimum value in the
3638 // range will be zero.
3639 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3641 if (wi::gt_p (min, max, sign))
3642 max = min;
3643 min = wi::zero (prec);
3645 else
3647 // If the range was reversed, swap MIN and MAX.
3648 if (wi::gt_p (min, max, sign))
3649 std::swap (min, max);
3652 // If the new range has its limits swapped around (MIN > MAX), then
3653 // the operation caused one of them to wrap around. The only thing
3654 // we know is that the result is positive.
3655 if (wi::gt_p (min, max, sign))
3657 min = wi::zero (prec);
3658 max = max_value;
3660 r = int_range<1> (type, min, max);
3663 bool
3664 operator_abs::op1_range (irange &r, tree type,
3665 const irange &lhs,
3666 const irange &op2,
3667 relation_kind rel ATTRIBUTE_UNUSED) const
3669 if (empty_range_varying (r, type, lhs, op2))
3670 return true;
3671 if (TYPE_UNSIGNED (type))
3673 r = lhs;
3674 return true;
3676 // Start with the positives because negatives are an impossible result.
3677 int_range_max positives = range_positives (type);
3678 positives.intersect (lhs);
3679 r = positives;
3680 // Then add the negative of each pair:
3681 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3682 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3683 r.union_ (int_range<1> (type,
3684 -positives.upper_bound (i),
3685 -positives.lower_bound (i)));
3686 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3687 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3688 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3689 wide_int lb = lhs.lower_bound ();
3690 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3691 r.union_ (int_range<2> (type, lb, lb));
3692 return true;
3696 class operator_absu : public range_operator
3698 public:
3699 virtual void wi_fold (irange &r, tree type,
3700 const wide_int &lh_lb, const wide_int &lh_ub,
3701 const wide_int &rh_lb, const wide_int &rh_ub) const;
3702 } op_absu;
3704 void
3705 operator_absu::wi_fold (irange &r, tree type,
3706 const wide_int &lh_lb, const wide_int &lh_ub,
3707 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3708 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3710 wide_int new_lb, new_ub;
3712 // Pass through VR0 the easy cases.
3713 if (wi::ges_p (lh_lb, 0))
3715 new_lb = lh_lb;
3716 new_ub = lh_ub;
3718 else
3720 new_lb = wi::abs (lh_lb);
3721 new_ub = wi::abs (lh_ub);
3723 // If the range contains zero then we know that the minimum
3724 // value in the range will be zero.
3725 if (wi::ges_p (lh_ub, 0))
3727 if (wi::gtu_p (new_lb, new_ub))
3728 new_ub = new_lb;
3729 new_lb = wi::zero (TYPE_PRECISION (type));
3731 else
3732 std::swap (new_lb, new_ub);
3735 gcc_checking_assert (TYPE_UNSIGNED (type));
3736 r = int_range<1> (type, new_lb, new_ub);
3740 class operator_negate : public range_operator
3742 public:
3743 virtual bool fold_range (irange &r, tree type,
3744 const irange &op1,
3745 const irange &op2,
3746 relation_kind rel = VREL_NONE) const;
3747 virtual bool op1_range (irange &r, tree type,
3748 const irange &lhs,
3749 const irange &op2,
3750 relation_kind rel = VREL_NONE) const;
3751 } op_negate;
3753 bool
3754 operator_negate::fold_range (irange &r, tree type,
3755 const irange &lh,
3756 const irange &rh,
3757 relation_kind rel ATTRIBUTE_UNUSED) const
3759 if (empty_range_varying (r, type, lh, rh))
3760 return true;
3761 // -X is simply 0 - X.
3762 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type,
3763 range_zero (type),
3764 lh);
3767 bool
3768 operator_negate::op1_range (irange &r, tree type,
3769 const irange &lhs,
3770 const irange &op2,
3771 relation_kind rel ATTRIBUTE_UNUSED) const
3773 // NEGATE is involutory.
3774 return fold_range (r, type, lhs, op2);
3778 class operator_addr_expr : public range_operator
3780 public:
3781 virtual bool fold_range (irange &r, tree type,
3782 const irange &op1,
3783 const irange &op2,
3784 relation_kind rel = VREL_NONE) const;
3785 virtual bool op1_range (irange &r, tree type,
3786 const irange &lhs,
3787 const irange &op2,
3788 relation_kind rel = VREL_NONE) const;
3789 } op_addr;
3791 bool
3792 operator_addr_expr::fold_range (irange &r, tree type,
3793 const irange &lh,
3794 const irange &rh,
3795 relation_kind rel ATTRIBUTE_UNUSED) const
3797 if (empty_range_varying (r, type, lh, rh))
3798 return true;
3800 // Return a non-null pointer of the LHS type (passed in op2).
3801 if (lh.zero_p ())
3802 r = range_zero (type);
3803 else if (!lh.contains_p (build_zero_cst (lh.type ())))
3804 r = range_nonzero (type);
3805 else
3806 r.set_varying (type);
3807 return true;
3810 bool
3811 operator_addr_expr::op1_range (irange &r, tree type,
3812 const irange &lhs,
3813 const irange &op2,
3814 relation_kind rel ATTRIBUTE_UNUSED) const
3816 return operator_addr_expr::fold_range (r, type, lhs, op2);
3820 class pointer_plus_operator : public range_operator
3822 public:
3823 virtual void wi_fold (irange &r, tree type,
3824 const wide_int &lh_lb,
3825 const wide_int &lh_ub,
3826 const wide_int &rh_lb,
3827 const wide_int &rh_ub) const;
3828 } op_pointer_plus;
3830 void
3831 pointer_plus_operator::wi_fold (irange &r, tree type,
3832 const wide_int &lh_lb,
3833 const wide_int &lh_ub,
3834 const wide_int &rh_lb,
3835 const wide_int &rh_ub) const
3837 // Check for [0,0] + const, and simply return the const.
3838 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3840 tree val = wide_int_to_tree (type, rh_lb);
3841 r.set (val, val);
3842 return;
3845 // For pointer types, we are really only interested in asserting
3846 // whether the expression evaluates to non-NULL.
3848 // With -fno-delete-null-pointer-checks we need to be more
3849 // conservative. As some object might reside at address 0,
3850 // then some offset could be added to it and the same offset
3851 // subtracted again and the result would be NULL.
3852 // E.g.
3853 // static int a[12]; where &a[0] is NULL and
3854 // ptr = &a[6];
3855 // ptr -= 6;
3856 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3857 // where the first range doesn't include zero and the second one
3858 // doesn't either. As the second operand is sizetype (unsigned),
3859 // consider all ranges where the MSB could be set as possible
3860 // subtractions where the result might be NULL.
3861 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3862 || !wi_includes_zero_p (type, rh_lb, rh_ub))
3863 && !TYPE_OVERFLOW_WRAPS (type)
3864 && (flag_delete_null_pointer_checks
3865 || !wi::sign_mask (rh_ub)))
3866 r = range_nonzero (type);
3867 else if (lh_lb == lh_ub && lh_lb == 0
3868 && rh_lb == rh_ub && rh_lb == 0)
3869 r = range_zero (type);
3870 else
3871 r.set_varying (type);
3875 class pointer_min_max_operator : public range_operator
3877 public:
3878 virtual void wi_fold (irange & r, tree type,
3879 const wide_int &lh_lb, const wide_int &lh_ub,
3880 const wide_int &rh_lb, const wide_int &rh_ub) const;
3881 } op_ptr_min_max;
3883 void
3884 pointer_min_max_operator::wi_fold (irange &r, tree type,
3885 const wide_int &lh_lb,
3886 const wide_int &lh_ub,
3887 const wide_int &rh_lb,
3888 const wide_int &rh_ub) const
3890 // For MIN/MAX expressions with pointers, we only care about
3891 // nullness. If both are non null, then the result is nonnull.
3892 // If both are null, then the result is null. Otherwise they
3893 // are varying.
3894 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3895 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3896 r = range_nonzero (type);
3897 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3898 r = range_zero (type);
3899 else
3900 r.set_varying (type);
3904 class pointer_and_operator : public range_operator
3906 public:
3907 virtual void wi_fold (irange &r, tree type,
3908 const wide_int &lh_lb, const wide_int &lh_ub,
3909 const wide_int &rh_lb, const wide_int &rh_ub) const;
3910 } op_pointer_and;
3912 void
3913 pointer_and_operator::wi_fold (irange &r, tree type,
3914 const wide_int &lh_lb,
3915 const wide_int &lh_ub,
3916 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3917 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3919 // For pointer types, we are really only interested in asserting
3920 // whether the expression evaluates to non-NULL.
3921 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
3922 r = range_zero (type);
3923 else
3924 r.set_varying (type);
3928 class pointer_or_operator : public range_operator
3930 public:
3931 virtual bool op1_range (irange &r, tree type,
3932 const irange &lhs,
3933 const irange &op2,
3934 relation_kind rel = VREL_NONE) const;
3935 virtual bool op2_range (irange &r, tree type,
3936 const irange &lhs,
3937 const irange &op1,
3938 relation_kind rel = VREL_NONE) const;
3939 virtual void wi_fold (irange &r, tree type,
3940 const wide_int &lh_lb, const wide_int &lh_ub,
3941 const wide_int &rh_lb, const wide_int &rh_ub) const;
3942 } op_pointer_or;
3944 bool
3945 pointer_or_operator::op1_range (irange &r, tree type,
3946 const irange &lhs,
3947 const irange &op2 ATTRIBUTE_UNUSED,
3948 relation_kind rel ATTRIBUTE_UNUSED) const
3950 if (lhs.zero_p ())
3952 tree zero = build_zero_cst (type);
3953 r = int_range<1> (zero, zero);
3954 return true;
3956 r.set_varying (type);
3957 return true;
3960 bool
3961 pointer_or_operator::op2_range (irange &r, tree type,
3962 const irange &lhs,
3963 const irange &op1,
3964 relation_kind rel ATTRIBUTE_UNUSED) const
3966 return pointer_or_operator::op1_range (r, type, lhs, op1);
3969 void
3970 pointer_or_operator::wi_fold (irange &r, tree type,
3971 const wide_int &lh_lb,
3972 const wide_int &lh_ub,
3973 const wide_int &rh_lb,
3974 const wide_int &rh_ub) const
3976 // For pointer types, we are really only interested in asserting
3977 // whether the expression evaluates to non-NULL.
3978 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3979 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3980 r = range_nonzero (type);
3981 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3982 r = range_zero (type);
3983 else
3984 r.set_varying (type);
3987 // This implements the range operator tables as local objects in this file.
3989 class range_op_table
3991 public:
3992 inline range_operator *operator[] (enum tree_code code);
3993 protected:
3994 void set (enum tree_code code, range_operator &op);
3995 private:
3996 range_operator *m_range_tree[MAX_TREE_CODES];
3999 // Return a pointer to the range_operator instance, if there is one
4000 // associated with tree_code CODE.
4002 range_operator *
4003 range_op_table::operator[] (enum tree_code code)
4005 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4006 return m_range_tree[code];
4009 // Add OP to the handler table for CODE.
4011 void
4012 range_op_table::set (enum tree_code code, range_operator &op)
4014 gcc_checking_assert (m_range_tree[code] == NULL);
4015 m_range_tree[code] = &op;
4018 // Instantiate a range op table for integral operations.
4020 class integral_table : public range_op_table
4022 public:
4023 integral_table ();
4024 } integral_tree_table;
4026 integral_table::integral_table ()
4028 set (EQ_EXPR, op_equal);
4029 set (NE_EXPR, op_not_equal);
4030 set (LT_EXPR, op_lt);
4031 set (LE_EXPR, op_le);
4032 set (GT_EXPR, op_gt);
4033 set (GE_EXPR, op_ge);
4034 set (PLUS_EXPR, op_plus);
4035 set (MINUS_EXPR, op_minus);
4036 set (MIN_EXPR, op_min);
4037 set (MAX_EXPR, op_max);
4038 set (MULT_EXPR, op_mult);
4039 set (TRUNC_DIV_EXPR, op_trunc_div);
4040 set (FLOOR_DIV_EXPR, op_floor_div);
4041 set (ROUND_DIV_EXPR, op_round_div);
4042 set (CEIL_DIV_EXPR, op_ceil_div);
4043 set (EXACT_DIV_EXPR, op_exact_div);
4044 set (LSHIFT_EXPR, op_lshift);
4045 set (RSHIFT_EXPR, op_rshift);
4046 set (NOP_EXPR, op_convert);
4047 set (CONVERT_EXPR, op_convert);
4048 set (TRUTH_AND_EXPR, op_logical_and);
4049 set (BIT_AND_EXPR, op_bitwise_and);
4050 set (TRUTH_OR_EXPR, op_logical_or);
4051 set (BIT_IOR_EXPR, op_bitwise_or);
4052 set (BIT_XOR_EXPR, op_bitwise_xor);
4053 set (TRUNC_MOD_EXPR, op_trunc_mod);
4054 set (TRUTH_NOT_EXPR, op_logical_not);
4055 set (BIT_NOT_EXPR, op_bitwise_not);
4056 set (INTEGER_CST, op_integer_cst);
4057 set (SSA_NAME, op_identity);
4058 set (PAREN_EXPR, op_identity);
4059 set (OBJ_TYPE_REF, op_identity);
4060 set (IMAGPART_EXPR, op_unknown);
4061 set (REALPART_EXPR, op_unknown);
4062 set (POINTER_DIFF_EXPR, op_pointer_diff);
4063 set (ABS_EXPR, op_abs);
4064 set (ABSU_EXPR, op_absu);
4065 set (NEGATE_EXPR, op_negate);
4066 set (ADDR_EXPR, op_addr);
4069 // Instantiate a range op table for pointer operations.
4071 class pointer_table : public range_op_table
4073 public:
4074 pointer_table ();
4075 } pointer_tree_table;
4077 pointer_table::pointer_table ()
4079 set (BIT_AND_EXPR, op_pointer_and);
4080 set (BIT_IOR_EXPR, op_pointer_or);
4081 set (MIN_EXPR, op_ptr_min_max);
4082 set (MAX_EXPR, op_ptr_min_max);
4083 set (POINTER_PLUS_EXPR, op_pointer_plus);
4085 set (EQ_EXPR, op_equal);
4086 set (NE_EXPR, op_not_equal);
4087 set (LT_EXPR, op_lt);
4088 set (LE_EXPR, op_le);
4089 set (GT_EXPR, op_gt);
4090 set (GE_EXPR, op_ge);
4091 set (SSA_NAME, op_identity);
4092 set (INTEGER_CST, op_integer_cst);
4093 set (ADDR_EXPR, op_addr);
4094 set (NOP_EXPR, op_convert);
4095 set (CONVERT_EXPR, op_convert);
4097 set (BIT_NOT_EXPR, op_bitwise_not);
4098 set (BIT_XOR_EXPR, op_bitwise_xor);
4101 // The tables are hidden and accessed via a simple extern function.
4103 range_operator *
4104 range_op_handler (enum tree_code code, tree type)
4106 // First check if there is a pointer specialization.
4107 if (POINTER_TYPE_P (type))
4108 return pointer_tree_table[code];
4109 if (INTEGRAL_TYPE_P (type))
4110 return integral_tree_table[code];
4111 return NULL;
4114 // Cast the range in R to TYPE.
4116 void
4117 range_cast (irange &r, tree type)
4119 int_range_max tmp = r;
4120 range_operator *op = range_op_handler (CONVERT_EXPR, type);
4121 // Call op_convert, if it fails, the result is varying.
4122 if (!op->fold_range (r, type, tmp, int_range<1> (type)))
4123 r.set_varying (type);
4126 #if CHECKING_P
4127 #include "selftest.h"
4129 namespace selftest
4131 #define INT(N) build_int_cst (integer_type_node, (N))
4132 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4133 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4134 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4135 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4136 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4138 static void
4139 range_op_cast_tests ()
4141 int_range<1> r0, r1, r2, rold;
4142 r0.set_varying (integer_type_node);
4143 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4145 // If a range is in any way outside of the range for the converted
4146 // to range, default to the range for the new type.
4147 r0.set_varying (short_integer_type_node);
4148 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4149 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4150 if (TYPE_PRECISION (TREE_TYPE (maxint))
4151 > TYPE_PRECISION (short_integer_type_node))
4153 r1 = int_range<1> (integer_zero_node, maxint);
4154 range_cast (r1, short_integer_type_node);
4155 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4156 && r1.upper_bound() == wi::to_wide (maxshort));
4159 // (unsigned char)[-5,-1] => [251,255].
4160 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4161 range_cast (r0, unsigned_char_type_node);
4162 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4163 range_cast (r0, signed_char_type_node);
4164 ASSERT_TRUE (r0 == rold);
4166 // (signed char)[15, 150] => [-128,-106][15,127].
4167 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4168 range_cast (r0, signed_char_type_node);
4169 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4170 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4171 r1.union_ (r2);
4172 ASSERT_TRUE (r1 == r0);
4173 range_cast (r0, unsigned_char_type_node);
4174 ASSERT_TRUE (r0 == rold);
4176 // (unsigned char)[-5, 5] => [0,5][251,255].
4177 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4178 range_cast (r0, unsigned_char_type_node);
4179 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4180 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4181 r1.union_ (r2);
4182 ASSERT_TRUE (r0 == r1);
4183 range_cast (r0, signed_char_type_node);
4184 ASSERT_TRUE (r0 == rold);
4186 // (unsigned char)[-5,5] => [0,5][251,255].
4187 r0 = int_range<1> (INT (-5), INT (5));
4188 range_cast (r0, unsigned_char_type_node);
4189 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4190 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4191 ASSERT_TRUE (r0 == r1);
4193 // (unsigned char)[5U,1974U] => [0,255].
4194 r0 = int_range<1> (UINT (5), UINT (1974));
4195 range_cast (r0, unsigned_char_type_node);
4196 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4197 range_cast (r0, integer_type_node);
4198 // Going to a wider range should not sign extend.
4199 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4201 // (unsigned char)[-350,15] => [0,255].
4202 r0 = int_range<1> (INT (-350), INT (15));
4203 range_cast (r0, unsigned_char_type_node);
4204 ASSERT_TRUE (r0 == (int_range<1>
4205 (TYPE_MIN_VALUE (unsigned_char_type_node),
4206 TYPE_MAX_VALUE (unsigned_char_type_node))));
4208 // Casting [-120,20] from signed char to unsigned short.
4209 // => [0, 20][0xff88, 0xffff].
4210 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4211 range_cast (r0, short_unsigned_type_node);
4212 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4213 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4214 r1.union_ (r2);
4215 ASSERT_TRUE (r0 == r1);
4216 // A truncating cast back to signed char will work because [-120, 20]
4217 // is representable in signed char.
4218 range_cast (r0, signed_char_type_node);
4219 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4221 // unsigned char -> signed short
4222 // (signed short)[(unsigned char)25, (unsigned char)250]
4223 // => [(signed short)25, (signed short)250]
4224 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4225 range_cast (r0, short_integer_type_node);
4226 r1 = int_range<1> (INT16 (25), INT16 (250));
4227 ASSERT_TRUE (r0 == r1);
4228 range_cast (r0, unsigned_char_type_node);
4229 ASSERT_TRUE (r0 == rold);
4231 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4232 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4233 TYPE_MAX_VALUE (long_long_integer_type_node));
4234 range_cast (r0, short_unsigned_type_node);
4235 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4236 TYPE_MAX_VALUE (short_unsigned_type_node));
4237 ASSERT_TRUE (r0 == r1);
4239 // Casting NONZERO to a narrower type will wrap/overflow so
4240 // it's just the entire range for the narrower type.
4242 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4243 // is outside of the range of a smaller range, return the full
4244 // smaller range.
4245 if (TYPE_PRECISION (integer_type_node)
4246 > TYPE_PRECISION (short_integer_type_node))
4248 r0 = range_nonzero (integer_type_node);
4249 range_cast (r0, short_integer_type_node);
4250 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4251 TYPE_MAX_VALUE (short_integer_type_node));
4252 ASSERT_TRUE (r0 == r1);
4255 // Casting NONZERO from a narrower signed to a wider signed.
4257 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4258 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4259 r0 = range_nonzero (short_integer_type_node);
4260 range_cast (r0, integer_type_node);
4261 r1 = int_range<1> (INT (-32768), INT (-1));
4262 r2 = int_range<1> (INT (1), INT (32767));
4263 r1.union_ (r2);
4264 ASSERT_TRUE (r0 == r1);
4267 static void
4268 range_op_lshift_tests ()
4270 // Test that 0x808.... & 0x8.... still contains 0x8....
4271 // for a large set of numbers.
4273 int_range_max res;
4274 tree big_type = long_long_unsigned_type_node;
4275 // big_num = 0x808,0000,0000,0000
4276 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4277 build_int_cst (big_type, 0x808),
4278 build_int_cst (big_type, 48));
4279 op_bitwise_and.fold_range (res, big_type,
4280 int_range <1> (big_type),
4281 int_range <1> (big_num, big_num));
4282 // val = 0x8,0000,0000,0000
4283 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4284 build_int_cst (big_type, 0x8),
4285 build_int_cst (big_type, 48));
4286 ASSERT_TRUE (res.contains_p (val));
4289 if (TYPE_PRECISION (unsigned_type_node) > 31)
4291 // unsigned VARYING = op1 << 1 should be VARYING.
4292 int_range<2> lhs (unsigned_type_node);
4293 int_range<2> shift (INT (1), INT (1));
4294 int_range_max op1;
4295 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4296 ASSERT_TRUE (op1.varying_p ());
4298 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4299 int_range<2> zero (UINT (0), UINT (0));
4300 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4301 ASSERT_TRUE (op1.num_pairs () == 2);
4302 // Remove the [0,0] range.
4303 op1.intersect (zero);
4304 ASSERT_TRUE (op1.num_pairs () == 1);
4305 // op1 << 1 should be [0x8000,0x8000] << 1,
4306 // which should result in [0,0].
4307 int_range_max result;
4308 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4309 ASSERT_TRUE (result == zero);
4311 // signed VARYING = op1 << 1 should be VARYING.
4312 if (TYPE_PRECISION (integer_type_node) > 31)
4314 // unsigned VARYING = op1 << 1 hould be VARYING.
4315 int_range<2> lhs (integer_type_node);
4316 int_range<2> shift (INT (1), INT (1));
4317 int_range_max op1;
4318 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4319 ASSERT_TRUE (op1.varying_p ());
4321 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4322 int_range<2> zero (INT (0), INT (0));
4323 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4324 ASSERT_TRUE (op1.num_pairs () == 2);
4325 // Remove the [0,0] range.
4326 op1.intersect (zero);
4327 ASSERT_TRUE (op1.num_pairs () == 1);
4328 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4329 // which should result in [0,0].
4330 int_range_max result;
4331 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4332 ASSERT_TRUE (result == zero);
4336 static void
4337 range_op_rshift_tests ()
4339 // unsigned: [3, MAX] = OP1 >> 1
4341 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4342 TYPE_MAX_VALUE (unsigned_type_node));
4343 int_range_max one (build_one_cst (unsigned_type_node),
4344 build_one_cst (unsigned_type_node));
4345 int_range_max op1;
4346 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4347 ASSERT_FALSE (op1.contains_p (UINT (3)));
4350 // signed: [3, MAX] = OP1 >> 1
4352 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4353 int_range_max one (INT (1), INT (1));
4354 int_range_max op1;
4355 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4356 ASSERT_FALSE (op1.contains_p (INT (-2)));
4359 // This is impossible, so OP1 should be [].
4360 // signed: [MIN, MIN] = OP1 >> 1
4362 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4363 TYPE_MIN_VALUE (integer_type_node));
4364 int_range_max one (INT (1), INT (1));
4365 int_range_max op1;
4366 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4367 ASSERT_TRUE (op1.undefined_p ());
4370 // signed: ~[-1] = OP1 >> 31
4371 if (TYPE_PRECISION (integer_type_node) > 31)
4373 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4374 int_range_max shift (INT (31), INT (31));
4375 int_range_max op1;
4376 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4377 int_range_max negatives = range_negatives (integer_type_node);
4378 negatives.intersect (op1);
4379 ASSERT_TRUE (negatives.undefined_p ());
4383 static void
4384 range_op_bitwise_and_tests ()
4386 int_range_max res;
4387 tree min = vrp_val_min (integer_type_node);
4388 tree max = vrp_val_max (integer_type_node);
4389 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4390 build_one_cst (integer_type_node));
4391 int_range_max i1 (tiny, max);
4392 int_range_max i2 (build_int_cst (integer_type_node, 255),
4393 build_int_cst (integer_type_node, 255));
4395 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4396 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4397 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4399 // VARYING = OP1 & 255: OP1 is VARYING
4400 i1 = int_range<1> (integer_type_node);
4401 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4402 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4404 // (NONZERO | X) is nonzero.
4405 i1.set_nonzero (integer_type_node);
4406 i2.set_varying (integer_type_node);
4407 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4408 ASSERT_TRUE (res.nonzero_p ());
4410 // (NEGATIVE | X) is nonzero.
4411 i1 = int_range<1> (INT (-5), INT (-3));
4412 i2.set_varying (integer_type_node);
4413 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4414 ASSERT_FALSE (res.contains_p (INT (0)));
4417 static void
4418 range_relational_tests ()
4420 int_range<2> lhs (unsigned_char_type_node);
4421 int_range<2> op1 (UCHAR (8), UCHAR (10));
4422 int_range<2> op2 (UCHAR (20), UCHAR (20));
4424 // Never wrapping additions mean LHS > OP1.
4425 tree_code code = op_plus.lhs_op1_relation (lhs, op1, op2);
4426 ASSERT_TRUE (code == GT_EXPR);
4428 // Most wrapping additions mean nothing...
4429 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4430 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4431 code = op_plus.lhs_op1_relation (lhs, op1, op2);
4432 ASSERT_TRUE (code == VREL_NONE);
4434 // However, always wrapping additions mean LHS < OP1.
4435 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4436 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4437 code = op_plus.lhs_op1_relation (lhs, op1, op2);
4438 ASSERT_TRUE (code == LT_EXPR);
4441 void
4442 range_op_tests ()
4444 range_op_rshift_tests ();
4445 range_op_lshift_tests ();
4446 range_op_bitwise_and_tests ();
4447 range_op_cast_tests ();
4448 range_relational_tests ();
4451 } // namespace selftest
4453 #endif // CHECKING_P