compiler: don't use sink as parameter in method expression thunk
[official-gcc.git] / gcc / range-op.cc
blob5150c6021b838432e3069aaa58cbccd567220d29
1 /* Code for range operators.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
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-iterator.h"
42 #include "gimple-fold.h"
43 #include "tree-eh.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 // Return false if shifting by OP is undefined behavior. Otherwise, return
67 // true and the range it is to be shifted by. This allows trimming out of
68 // undefined ranges, leaving only valid ranges if there are any.
70 static inline bool
71 get_shift_range (irange &r, tree type, const irange &op)
73 if (op.undefined_p ())
74 return false;
76 // Build valid range and intersect it with the shift range.
77 r = value_range (build_int_cst_type (op.type (), 0),
78 build_int_cst_type (op.type (), TYPE_PRECISION (type) - 1));
79 r.intersect (op);
81 // If there are no valid ranges in the shift range, returned false.
82 if (r.undefined_p ())
83 return false;
84 return true;
87 // Return TRUE if 0 is within [WMIN, WMAX].
89 static inline bool
90 wi_includes_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
92 signop sign = TYPE_SIGN (type);
93 return wi::le_p (wmin, 0, sign) && wi::ge_p (wmax, 0, sign);
96 // Return TRUE if [WMIN, WMAX] is the singleton 0.
98 static inline bool
99 wi_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
101 unsigned prec = TYPE_PRECISION (type);
102 return wmin == wmax && wi::eq_p (wmin, wi::zero (prec));
105 // Default wide_int fold operation returns [MIN, MAX].
107 void
108 range_operator::wi_fold (irange &r, tree type,
109 const wide_int &lh_lb ATTRIBUTE_UNUSED,
110 const wide_int &lh_ub ATTRIBUTE_UNUSED,
111 const wide_int &rh_lb ATTRIBUTE_UNUSED,
112 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
114 gcc_checking_assert (r.supports_type_p (type));
115 r.set_varying (type);
118 // Call wi_fold, except further split small subranges into constants.
119 // This can provide better precision. For something 8 >> [0,1]
120 // Instead of [8, 16], we will produce [8,8][16,16]
122 void
123 range_operator::wi_fold_in_parts (irange &r, tree type,
124 const wide_int &lh_lb,
125 const wide_int &lh_ub,
126 const wide_int &rh_lb,
127 const wide_int &rh_ub) const
129 int_range_max tmp;
130 widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
131 widest_int::from (rh_lb, TYPE_SIGN (type)));
132 widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
133 widest_int::from (lh_lb, TYPE_SIGN (type)));
134 // If there are 2, 3, or 4 values in the RH range, do them separately.
135 // Call wi_fold_in_parts to check the RH side.
136 if (rh_range > 0 && rh_range < 4)
138 wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
139 if (rh_range > 1)
141 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
142 r.union_ (tmp);
143 if (rh_range == 3)
145 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
146 r.union_ (tmp);
149 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
150 r.union_ (tmp);
152 // Otherise check for 2, 3, or 4 values in the LH range and split them up.
153 // The RH side has been checked, so no recursion needed.
154 else if (lh_range > 0 && lh_range < 4)
156 wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
157 if (lh_range > 1)
159 wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
160 r.union_ (tmp);
161 if (lh_range == 3)
163 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
164 r.union_ (tmp);
167 wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
168 r.union_ (tmp);
170 // Otherwise just call wi_fold.
171 else
172 wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
175 // The default for fold is to break all ranges into sub-ranges and
176 // invoke the wi_fold method on each sub-range pair.
178 bool
179 range_operator::fold_range (irange &r, tree type,
180 const irange &lh,
181 const irange &rh,
182 relation_kind rel) const
184 gcc_checking_assert (r.supports_type_p (type));
185 if (empty_range_varying (r, type, lh, rh))
186 return true;
188 unsigned num_lh = lh.num_pairs ();
189 unsigned num_rh = rh.num_pairs ();
191 // If both ranges are single pairs, fold directly into the result range.
192 // If the number of subranges grows too high, produce a summary result as the
193 // loop becomes exponential with little benefit. See PR 103821.
194 if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
196 wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
197 rh.lower_bound (), rh.upper_bound ());
198 op1_op2_relation_effect (r, type, lh, rh, rel);
199 return true;
202 int_range_max tmp;
203 r.set_undefined ();
204 for (unsigned x = 0; x < num_lh; ++x)
205 for (unsigned y = 0; y < num_rh; ++y)
207 wide_int lh_lb = lh.lower_bound (x);
208 wide_int lh_ub = lh.upper_bound (x);
209 wide_int rh_lb = rh.lower_bound (y);
210 wide_int rh_ub = rh.upper_bound (y);
211 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
212 r.union_ (tmp);
213 if (r.varying_p ())
215 op1_op2_relation_effect (r, type, lh, rh, rel);
216 return true;
219 op1_op2_relation_effect (r, type, lh, rh, rel);
220 return true;
223 // The default for op1_range is to return false.
225 bool
226 range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
227 tree type ATTRIBUTE_UNUSED,
228 const irange &lhs ATTRIBUTE_UNUSED,
229 const irange &op2 ATTRIBUTE_UNUSED,
230 relation_kind rel ATTRIBUTE_UNUSED) const
232 return false;
235 // The default for op2_range is to return false.
237 bool
238 range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
239 tree type ATTRIBUTE_UNUSED,
240 const irange &lhs ATTRIBUTE_UNUSED,
241 const irange &op1 ATTRIBUTE_UNUSED,
242 relation_kind rel ATTRIBUTE_UNUSED) const
244 return false;
247 // The default relation routines return VREL_VARYING.
249 relation_kind
250 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
251 const irange &op1 ATTRIBUTE_UNUSED,
252 const irange &op2 ATTRIBUTE_UNUSED,
253 relation_kind rel ATTRIBUTE_UNUSED) const
255 return VREL_VARYING;
258 relation_kind
259 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
260 const irange &op1 ATTRIBUTE_UNUSED,
261 const irange &op2 ATTRIBUTE_UNUSED,
262 relation_kind rel ATTRIBUTE_UNUSED) const
264 return VREL_VARYING;
267 relation_kind
268 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
270 return VREL_VARYING;
273 // Default is no relation affects the LHS.
275 bool
276 range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
277 tree type ATTRIBUTE_UNUSED,
278 const irange &op1_range ATTRIBUTE_UNUSED,
279 const irange &op2_range ATTRIBUTE_UNUSED,
280 relation_kind rel ATTRIBUTE_UNUSED) const
282 return false;
285 // Create and return a range from a pair of wide-ints that are known
286 // to have overflowed (or underflowed).
288 static void
289 value_range_from_overflowed_bounds (irange &r, tree type,
290 const wide_int &wmin,
291 const wide_int &wmax)
293 const signop sgn = TYPE_SIGN (type);
294 const unsigned int prec = TYPE_PRECISION (type);
296 wide_int tmin = wide_int::from (wmin, prec, sgn);
297 wide_int tmax = wide_int::from (wmax, prec, sgn);
299 bool covers = false;
300 wide_int tem = tmin;
301 tmin = tmax + 1;
302 if (wi::cmp (tmin, tmax, sgn) < 0)
303 covers = true;
304 tmax = tem - 1;
305 if (wi::cmp (tmax, tem, sgn) > 0)
306 covers = true;
308 // If the anti-range would cover nothing, drop to varying.
309 // Likewise if the anti-range bounds are outside of the types
310 // values.
311 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
312 r.set_varying (type);
313 else
315 tree tree_min = wide_int_to_tree (type, tmin);
316 tree tree_max = wide_int_to_tree (type, tmax);
317 r.set (tree_min, tree_max, VR_ANTI_RANGE);
321 // Create and return a range from a pair of wide-ints. MIN_OVF and
322 // MAX_OVF describe any overflow that might have occurred while
323 // calculating WMIN and WMAX respectively.
325 static void
326 value_range_with_overflow (irange &r, tree type,
327 const wide_int &wmin, const wide_int &wmax,
328 wi::overflow_type min_ovf = wi::OVF_NONE,
329 wi::overflow_type max_ovf = wi::OVF_NONE)
331 const signop sgn = TYPE_SIGN (type);
332 const unsigned int prec = TYPE_PRECISION (type);
333 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
335 // For one bit precision if max != min, then the range covers all
336 // values.
337 if (prec == 1 && wi::ne_p (wmax, wmin))
339 r.set_varying (type);
340 return;
343 if (overflow_wraps)
345 // If overflow wraps, truncate the values and adjust the range,
346 // kind, and bounds appropriately.
347 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
349 wide_int tmin = wide_int::from (wmin, prec, sgn);
350 wide_int tmax = wide_int::from (wmax, prec, sgn);
351 // If the limits are swapped, we wrapped around and cover
352 // the entire range.
353 if (wi::gt_p (tmin, tmax, sgn))
354 r.set_varying (type);
355 else
356 // No overflow or both overflow or underflow. The range
357 // kind stays normal.
358 r.set (wide_int_to_tree (type, tmin),
359 wide_int_to_tree (type, tmax));
360 return;
363 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
364 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
365 value_range_from_overflowed_bounds (r, type, wmin, wmax);
366 else
367 // Other underflow and/or overflow, drop to VR_VARYING.
368 r.set_varying (type);
370 else
372 // If both bounds either underflowed or overflowed, then the result
373 // is undefined.
374 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
375 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
377 r.set_undefined ();
378 return;
381 // If overflow does not wrap, saturate to [MIN, MAX].
382 wide_int new_lb, new_ub;
383 if (min_ovf == wi::OVF_UNDERFLOW)
384 new_lb = wi::min_value (prec, sgn);
385 else if (min_ovf == wi::OVF_OVERFLOW)
386 new_lb = wi::max_value (prec, sgn);
387 else
388 new_lb = wmin;
390 if (max_ovf == wi::OVF_UNDERFLOW)
391 new_ub = wi::min_value (prec, sgn);
392 else if (max_ovf == wi::OVF_OVERFLOW)
393 new_ub = wi::max_value (prec, sgn);
394 else
395 new_ub = wmax;
397 r.set (wide_int_to_tree (type, new_lb),
398 wide_int_to_tree (type, new_ub));
402 // Create and return a range from a pair of wide-ints. Canonicalize
403 // the case where the bounds are swapped. In which case, we transform
404 // [10,5] into [MIN,5][10,MAX].
406 static inline void
407 create_possibly_reversed_range (irange &r, tree type,
408 const wide_int &new_lb, const wide_int &new_ub)
410 signop s = TYPE_SIGN (type);
411 // If the bounds are swapped, treat the result as if an overflow occured.
412 if (wi::gt_p (new_lb, new_ub, s))
413 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
414 else
415 // Otherwise it's just a normal range.
416 r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
419 // Return the summary information about boolean range LHS. If EMPTY/FULL,
420 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
422 bool_range_state
423 get_bool_state (vrange &r, const vrange &lhs, tree val_type)
425 // If there is no result, then this is unexecutable.
426 if (lhs.undefined_p ())
428 r.set_undefined ();
429 return BRS_EMPTY;
432 if (lhs.zero_p ())
433 return BRS_FALSE;
435 // For TRUE, we can't just test for [1,1] because Ada can have
436 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
437 if (lhs.contains_p (build_zero_cst (lhs.type ())))
439 r.set_varying (val_type);
440 return BRS_FULL;
443 return BRS_TRUE;
447 class operator_equal : public range_operator
449 using range_operator::fold_range;
450 using range_operator::op1_range;
451 using range_operator::op2_range;
452 public:
453 virtual bool fold_range (irange &r, tree type,
454 const irange &op1,
455 const irange &op2,
456 relation_kind rel = VREL_VARYING) const;
457 virtual bool op1_range (irange &r, tree type,
458 const irange &lhs,
459 const irange &val,
460 relation_kind rel = VREL_VARYING) const;
461 virtual bool op2_range (irange &r, tree type,
462 const irange &lhs,
463 const irange &val,
464 relation_kind rel = VREL_VARYING) const;
465 virtual relation_kind op1_op2_relation (const irange &lhs) const;
466 } op_equal;
468 // Check if the LHS range indicates a relation between OP1 and OP2.
470 relation_kind
471 equal_op1_op2_relation (const irange &lhs)
473 if (lhs.undefined_p ())
474 return VREL_UNDEFINED;
476 // FALSE = op1 == op2 indicates NE_EXPR.
477 if (lhs.zero_p ())
478 return VREL_NE;
480 // TRUE = op1 == op2 indicates EQ_EXPR.
481 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
482 return VREL_EQ;
483 return VREL_VARYING;
486 relation_kind
487 operator_equal::op1_op2_relation (const irange &lhs) const
489 return equal_op1_op2_relation (lhs);
493 bool
494 operator_equal::fold_range (irange &r, tree type,
495 const irange &op1,
496 const irange &op2,
497 relation_kind rel) const
499 if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
500 return true;
502 // We can be sure the values are always equal or not if both ranges
503 // consist of a single value, and then compare them.
504 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
505 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
507 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
508 r = range_true (type);
509 else
510 r = range_false (type);
512 else
514 // If ranges do not intersect, we know the range is not equal,
515 // otherwise we don't know anything for sure.
516 int_range_max tmp = op1;
517 tmp.intersect (op2);
518 if (tmp.undefined_p ())
519 r = range_false (type);
520 else
521 r = range_true_and_false (type);
523 return true;
526 bool
527 operator_equal::op1_range (irange &r, tree type,
528 const irange &lhs,
529 const irange &op2,
530 relation_kind rel ATTRIBUTE_UNUSED) const
532 switch (get_bool_state (r, lhs, type))
534 case BRS_FALSE:
535 // If the result is false, the only time we know anything is
536 // if OP2 is a constant.
537 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
539 r = op2;
540 r.invert ();
542 else
543 r.set_varying (type);
544 break;
546 case BRS_TRUE:
547 // If it's true, the result is the same as OP2.
548 r = op2;
549 break;
551 default:
552 break;
554 return true;
557 bool
558 operator_equal::op2_range (irange &r, tree type,
559 const irange &lhs,
560 const irange &op1,
561 relation_kind rel) const
563 return operator_equal::op1_range (r, type, lhs, op1, rel);
566 class operator_not_equal : public range_operator
568 using range_operator::fold_range;
569 using range_operator::op1_range;
570 using range_operator::op2_range;
571 public:
572 virtual bool fold_range (irange &r, tree type,
573 const irange &op1,
574 const irange &op2,
575 relation_kind rel = VREL_VARYING) const;
576 virtual bool op1_range (irange &r, tree type,
577 const irange &lhs,
578 const irange &op2,
579 relation_kind rel = VREL_VARYING) const;
580 virtual bool op2_range (irange &r, tree type,
581 const irange &lhs,
582 const irange &op1,
583 relation_kind rel = VREL_VARYING) const;
584 virtual relation_kind op1_op2_relation (const irange &lhs) const;
585 } op_not_equal;
587 // Check if the LHS range indicates a relation between OP1 and OP2.
589 relation_kind
590 not_equal_op1_op2_relation (const irange &lhs)
592 if (lhs.undefined_p ())
593 return VREL_UNDEFINED;
595 // FALSE = op1 != op2 indicates EQ_EXPR.
596 if (lhs.zero_p ())
597 return VREL_EQ;
599 // TRUE = op1 != op2 indicates NE_EXPR.
600 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
601 return VREL_NE;
602 return VREL_VARYING;
605 relation_kind
606 operator_not_equal::op1_op2_relation (const irange &lhs) const
608 return not_equal_op1_op2_relation (lhs);
611 bool
612 operator_not_equal::fold_range (irange &r, tree type,
613 const irange &op1,
614 const irange &op2,
615 relation_kind rel) const
617 if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
618 return true;
620 // We can be sure the values are always equal or not if both ranges
621 // consist of a single value, and then compare them.
622 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
623 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
625 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
626 r = range_true (type);
627 else
628 r = range_false (type);
630 else
632 // If ranges do not intersect, we know the range is not equal,
633 // otherwise we don't know anything for sure.
634 int_range_max tmp = op1;
635 tmp.intersect (op2);
636 if (tmp.undefined_p ())
637 r = range_true (type);
638 else
639 r = range_true_and_false (type);
641 return true;
644 bool
645 operator_not_equal::op1_range (irange &r, tree type,
646 const irange &lhs,
647 const irange &op2,
648 relation_kind rel ATTRIBUTE_UNUSED) const
650 switch (get_bool_state (r, lhs, type))
652 case BRS_TRUE:
653 // If the result is true, the only time we know anything is if
654 // OP2 is a constant.
655 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
657 r = op2;
658 r.invert ();
660 else
661 r.set_varying (type);
662 break;
664 case BRS_FALSE:
665 // If it's false, the result is the same as OP2.
666 r = op2;
667 break;
669 default:
670 break;
672 return true;
676 bool
677 operator_not_equal::op2_range (irange &r, tree type,
678 const irange &lhs,
679 const irange &op1,
680 relation_kind rel) const
682 return operator_not_equal::op1_range (r, type, lhs, op1, rel);
685 // (X < VAL) produces the range of [MIN, VAL - 1].
687 static void
688 build_lt (irange &r, tree type, const wide_int &val)
690 wi::overflow_type ov;
691 wide_int lim;
692 signop sgn = TYPE_SIGN (type);
694 // Signed 1 bit cannot represent 1 for subtraction.
695 if (sgn == SIGNED)
696 lim = wi::add (val, -1, sgn, &ov);
697 else
698 lim = wi::sub (val, 1, sgn, &ov);
700 // If val - 1 underflows, check if X < MIN, which is an empty range.
701 if (ov)
702 r.set_undefined ();
703 else
704 r = int_range<1> (type, min_limit (type), lim);
707 // (X <= VAL) produces the range of [MIN, VAL].
709 static void
710 build_le (irange &r, tree type, const wide_int &val)
712 r = int_range<1> (type, min_limit (type), val);
715 // (X > VAL) produces the range of [VAL + 1, MAX].
717 static void
718 build_gt (irange &r, tree type, const wide_int &val)
720 wi::overflow_type ov;
721 wide_int lim;
722 signop sgn = TYPE_SIGN (type);
724 // Signed 1 bit cannot represent 1 for addition.
725 if (sgn == SIGNED)
726 lim = wi::sub (val, -1, sgn, &ov);
727 else
728 lim = wi::add (val, 1, sgn, &ov);
729 // If val + 1 overflows, check is for X > MAX, which is an empty range.
730 if (ov)
731 r.set_undefined ();
732 else
733 r = int_range<1> (type, lim, max_limit (type));
736 // (X >= val) produces the range of [VAL, MAX].
738 static void
739 build_ge (irange &r, tree type, const wide_int &val)
741 r = int_range<1> (type, val, max_limit (type));
745 class operator_lt : public range_operator
747 using range_operator::fold_range;
748 using range_operator::op1_range;
749 using range_operator::op2_range;
750 public:
751 virtual bool fold_range (irange &r, tree type,
752 const irange &op1,
753 const irange &op2,
754 relation_kind rel = VREL_VARYING) const;
755 virtual bool op1_range (irange &r, tree type,
756 const irange &lhs,
757 const irange &op2,
758 relation_kind rel = VREL_VARYING) const;
759 virtual bool op2_range (irange &r, tree type,
760 const irange &lhs,
761 const irange &op1,
762 relation_kind rel = VREL_VARYING) const;
763 virtual relation_kind op1_op2_relation (const irange &lhs) const;
764 } op_lt;
766 // Check if the LHS range indicates a relation between OP1 and OP2.
768 relation_kind
769 lt_op1_op2_relation (const irange &lhs)
771 if (lhs.undefined_p ())
772 return VREL_UNDEFINED;
774 // FALSE = op1 < op2 indicates GE_EXPR.
775 if (lhs.zero_p ())
776 return VREL_GE;
778 // TRUE = op1 < op2 indicates LT_EXPR.
779 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
780 return VREL_LT;
781 return VREL_VARYING;
784 relation_kind
785 operator_lt::op1_op2_relation (const irange &lhs) const
787 return lt_op1_op2_relation (lhs);
790 bool
791 operator_lt::fold_range (irange &r, tree type,
792 const irange &op1,
793 const irange &op2,
794 relation_kind rel) const
796 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
797 return true;
799 signop sign = TYPE_SIGN (op1.type ());
800 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
802 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
803 r = range_true (type);
804 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
805 r = range_false (type);
806 else
807 r = range_true_and_false (type);
808 return true;
811 bool
812 operator_lt::op1_range (irange &r, tree type,
813 const irange &lhs,
814 const irange &op2,
815 relation_kind rel ATTRIBUTE_UNUSED) const
817 switch (get_bool_state (r, lhs, type))
819 case BRS_TRUE:
820 build_lt (r, type, op2.upper_bound ());
821 break;
823 case BRS_FALSE:
824 build_ge (r, type, op2.lower_bound ());
825 break;
827 default:
828 break;
830 return true;
833 bool
834 operator_lt::op2_range (irange &r, tree type,
835 const irange &lhs,
836 const irange &op1,
837 relation_kind rel ATTRIBUTE_UNUSED) const
839 switch (get_bool_state (r, lhs, type))
841 case BRS_FALSE:
842 build_le (r, type, op1.upper_bound ());
843 break;
845 case BRS_TRUE:
846 build_gt (r, type, op1.lower_bound ());
847 break;
849 default:
850 break;
852 return true;
856 class operator_le : public range_operator
858 using range_operator::fold_range;
859 using range_operator::op1_range;
860 using range_operator::op2_range;
861 public:
862 virtual bool fold_range (irange &r, tree type,
863 const irange &op1,
864 const irange &op2,
865 relation_kind rel = VREL_VARYING) const;
866 virtual bool op1_range (irange &r, tree type,
867 const irange &lhs,
868 const irange &op2,
869 relation_kind rel = VREL_VARYING) const;
870 virtual bool op2_range (irange &r, tree type,
871 const irange &lhs,
872 const irange &op1,
873 relation_kind rel = VREL_VARYING) const;
874 virtual relation_kind op1_op2_relation (const irange &lhs) const;
875 } op_le;
877 // Check if the LHS range indicates a relation between OP1 and OP2.
879 relation_kind
880 le_op1_op2_relation (const irange &lhs)
882 if (lhs.undefined_p ())
883 return VREL_UNDEFINED;
885 // FALSE = op1 <= op2 indicates GT_EXPR.
886 if (lhs.zero_p ())
887 return VREL_GT;
889 // TRUE = op1 <= op2 indicates LE_EXPR.
890 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
891 return VREL_LE;
892 return VREL_VARYING;
895 relation_kind
896 operator_le::op1_op2_relation (const irange &lhs) const
898 return le_op1_op2_relation (lhs);
901 bool
902 operator_le::fold_range (irange &r, tree type,
903 const irange &op1,
904 const irange &op2,
905 relation_kind rel) const
907 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
908 return true;
910 signop sign = TYPE_SIGN (op1.type ());
911 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
913 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
914 r = range_true (type);
915 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
916 r = range_false (type);
917 else
918 r = range_true_and_false (type);
919 return true;
922 bool
923 operator_le::op1_range (irange &r, tree type,
924 const irange &lhs,
925 const irange &op2,
926 relation_kind rel ATTRIBUTE_UNUSED) const
928 switch (get_bool_state (r, lhs, type))
930 case BRS_TRUE:
931 build_le (r, type, op2.upper_bound ());
932 break;
934 case BRS_FALSE:
935 build_gt (r, type, op2.lower_bound ());
936 break;
938 default:
939 break;
941 return true;
944 bool
945 operator_le::op2_range (irange &r, tree type,
946 const irange &lhs,
947 const irange &op1,
948 relation_kind rel ATTRIBUTE_UNUSED) const
950 switch (get_bool_state (r, lhs, type))
952 case BRS_FALSE:
953 build_lt (r, type, op1.upper_bound ());
954 break;
956 case BRS_TRUE:
957 build_ge (r, type, op1.lower_bound ());
958 break;
960 default:
961 break;
963 return true;
967 class operator_gt : public range_operator
969 using range_operator::fold_range;
970 using range_operator::op1_range;
971 using range_operator::op2_range;
972 public:
973 virtual bool fold_range (irange &r, tree type,
974 const irange &op1,
975 const irange &op2,
976 relation_kind rel = VREL_VARYING) const;
977 virtual bool op1_range (irange &r, tree type,
978 const irange &lhs,
979 const irange &op2,
980 relation_kind rel = VREL_VARYING) const;
981 virtual bool op2_range (irange &r, tree type,
982 const irange &lhs,
983 const irange &op1,
984 relation_kind rel = VREL_VARYING) const;
985 virtual relation_kind op1_op2_relation (const irange &lhs) const;
986 } op_gt;
988 // Check if the LHS range indicates a relation between OP1 and OP2.
990 relation_kind
991 gt_op1_op2_relation (const irange &lhs)
993 if (lhs.undefined_p ())
994 return VREL_UNDEFINED;
996 // FALSE = op1 > op2 indicates LE_EXPR.
997 if (lhs.zero_p ())
998 return VREL_LE;
1000 // TRUE = op1 > op2 indicates GT_EXPR.
1001 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1002 return VREL_GT;
1003 return VREL_VARYING;
1006 relation_kind
1007 operator_gt::op1_op2_relation (const irange &lhs) const
1009 return gt_op1_op2_relation (lhs);
1013 bool
1014 operator_gt::fold_range (irange &r, tree type,
1015 const irange &op1, const irange &op2,
1016 relation_kind rel) const
1018 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1019 return true;
1021 signop sign = TYPE_SIGN (op1.type ());
1022 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1024 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1025 r = range_true (type);
1026 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1027 r = range_false (type);
1028 else
1029 r = range_true_and_false (type);
1030 return true;
1033 bool
1034 operator_gt::op1_range (irange &r, tree type,
1035 const irange &lhs, const irange &op2,
1036 relation_kind rel ATTRIBUTE_UNUSED) const
1038 switch (get_bool_state (r, lhs, type))
1040 case BRS_TRUE:
1041 build_gt (r, type, op2.lower_bound ());
1042 break;
1044 case BRS_FALSE:
1045 build_le (r, type, op2.upper_bound ());
1046 break;
1048 default:
1049 break;
1051 return true;
1054 bool
1055 operator_gt::op2_range (irange &r, tree type,
1056 const irange &lhs,
1057 const irange &op1,
1058 relation_kind rel ATTRIBUTE_UNUSED) const
1060 switch (get_bool_state (r, lhs, type))
1062 case BRS_FALSE:
1063 build_ge (r, type, op1.lower_bound ());
1064 break;
1066 case BRS_TRUE:
1067 build_lt (r, type, op1.upper_bound ());
1068 break;
1070 default:
1071 break;
1073 return true;
1077 class operator_ge : public range_operator
1079 using range_operator::fold_range;
1080 using range_operator::op1_range;
1081 using range_operator::op2_range;
1082 public:
1083 virtual bool fold_range (irange &r, tree type,
1084 const irange &op1,
1085 const irange &op2,
1086 relation_kind rel = VREL_VARYING) const;
1087 virtual bool op1_range (irange &r, tree type,
1088 const irange &lhs,
1089 const irange &op2,
1090 relation_kind rel = VREL_VARYING) const;
1091 virtual bool op2_range (irange &r, tree type,
1092 const irange &lhs,
1093 const irange &op1,
1094 relation_kind rel = VREL_VARYING) const;
1095 virtual relation_kind op1_op2_relation (const irange &lhs) const;
1096 } op_ge;
1098 // Check if the LHS range indicates a relation between OP1 and OP2.
1100 relation_kind
1101 ge_op1_op2_relation (const irange &lhs)
1103 if (lhs.undefined_p ())
1104 return VREL_UNDEFINED;
1106 // FALSE = op1 >= op2 indicates LT_EXPR.
1107 if (lhs.zero_p ())
1108 return VREL_LT;
1110 // TRUE = op1 >= op2 indicates GE_EXPR.
1111 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1112 return VREL_GE;
1113 return VREL_VARYING;
1116 relation_kind
1117 operator_ge::op1_op2_relation (const irange &lhs) const
1119 return ge_op1_op2_relation (lhs);
1122 bool
1123 operator_ge::fold_range (irange &r, tree type,
1124 const irange &op1,
1125 const irange &op2,
1126 relation_kind rel) const
1128 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1129 return true;
1131 signop sign = TYPE_SIGN (op1.type ());
1132 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1134 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1135 r = range_true (type);
1136 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1137 r = range_false (type);
1138 else
1139 r = range_true_and_false (type);
1140 return true;
1143 bool
1144 operator_ge::op1_range (irange &r, tree type,
1145 const irange &lhs,
1146 const irange &op2,
1147 relation_kind rel ATTRIBUTE_UNUSED) const
1149 switch (get_bool_state (r, lhs, type))
1151 case BRS_TRUE:
1152 build_ge (r, type, op2.lower_bound ());
1153 break;
1155 case BRS_FALSE:
1156 build_lt (r, type, op2.upper_bound ());
1157 break;
1159 default:
1160 break;
1162 return true;
1165 bool
1166 operator_ge::op2_range (irange &r, tree type,
1167 const irange &lhs,
1168 const irange &op1,
1169 relation_kind rel ATTRIBUTE_UNUSED) const
1171 switch (get_bool_state (r, lhs, type))
1173 case BRS_FALSE:
1174 build_gt (r, type, op1.lower_bound ());
1175 break;
1177 case BRS_TRUE:
1178 build_le (r, type, op1.upper_bound ());
1179 break;
1181 default:
1182 break;
1184 return true;
1188 class operator_plus : public range_operator
1190 using range_operator::op1_range;
1191 using range_operator::op2_range;
1192 using range_operator::lhs_op1_relation;
1193 using range_operator::lhs_op2_relation;
1194 public:
1195 virtual bool op1_range (irange &r, tree type,
1196 const irange &lhs,
1197 const irange &op2,
1198 relation_kind rel ATTRIBUTE_UNUSED) const;
1199 virtual bool op2_range (irange &r, tree type,
1200 const irange &lhs,
1201 const irange &op1,
1202 relation_kind rel ATTRIBUTE_UNUSED) const;
1203 virtual void wi_fold (irange &r, tree type,
1204 const wide_int &lh_lb,
1205 const wide_int &lh_ub,
1206 const wide_int &rh_lb,
1207 const wide_int &rh_ub) const;
1208 virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
1209 const irange &op2,
1210 relation_kind rel) const;
1211 virtual relation_kind lhs_op2_relation (const irange &lhs, const irange &op1,
1212 const irange &op2,
1213 relation_kind rel) const;
1214 } op_plus;
1216 // Check to see if the range of OP2 indicates anything about the relation
1217 // between LHS and OP1.
1219 relation_kind
1220 operator_plus::lhs_op1_relation (const irange &lhs,
1221 const irange &op1,
1222 const irange &op2,
1223 relation_kind) const
1225 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1226 return VREL_VARYING;
1228 tree type = lhs.type ();
1229 unsigned prec = TYPE_PRECISION (type);
1230 wi::overflow_type ovf1, ovf2;
1231 signop sign = TYPE_SIGN (type);
1233 // LHS = OP1 + 0 indicates LHS == OP1.
1234 if (op2.zero_p ())
1235 return VREL_EQ;
1237 if (TYPE_OVERFLOW_WRAPS (type))
1239 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1240 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1242 else
1243 ovf1 = ovf2 = wi::OVF_NONE;
1245 // Never wrapping additions.
1246 if (!ovf1 && !ovf2)
1248 // Positive op2 means lhs > op1.
1249 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1250 return VREL_GT;
1251 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1252 return VREL_GE;
1254 // Negative op2 means lhs < op1.
1255 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1256 return VREL_LT;
1257 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1258 return VREL_LE;
1260 // Always wrapping additions.
1261 else if (ovf1 && ovf1 == ovf2)
1263 // Positive op2 means lhs < op1.
1264 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1265 return VREL_LT;
1266 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1267 return VREL_LE;
1269 // Negative op2 means lhs > op1.
1270 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1271 return VREL_GT;
1272 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1273 return VREL_GE;
1276 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1277 if (!range_includes_zero_p (&op2))
1278 return VREL_NE;
1280 return VREL_VARYING;
1283 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1284 // operands.
1286 relation_kind
1287 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1288 const irange &op2, relation_kind rel) const
1290 return lhs_op1_relation (lhs, op2, op1, rel);
1293 void
1294 operator_plus::wi_fold (irange &r, tree type,
1295 const wide_int &lh_lb, const wide_int &lh_ub,
1296 const wide_int &rh_lb, const wide_int &rh_ub) const
1298 wi::overflow_type ov_lb, ov_ub;
1299 signop s = TYPE_SIGN (type);
1300 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1301 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1302 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1305 bool
1306 operator_plus::op1_range (irange &r, tree type,
1307 const irange &lhs,
1308 const irange &op2,
1309 relation_kind rel ATTRIBUTE_UNUSED) const
1311 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, lhs, op2);
1314 bool
1315 operator_plus::op2_range (irange &r, tree type,
1316 const irange &lhs,
1317 const irange &op1,
1318 relation_kind rel ATTRIBUTE_UNUSED) const
1320 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, lhs, op1);
1324 class operator_minus : public range_operator
1326 using range_operator::fold_range;
1327 using range_operator::op1_range;
1328 using range_operator::op2_range;
1329 public:
1330 virtual bool op1_range (irange &r, tree type,
1331 const irange &lhs,
1332 const irange &op2,
1333 relation_kind rel ATTRIBUTE_UNUSED) const;
1334 virtual bool op2_range (irange &r, tree type,
1335 const irange &lhs,
1336 const irange &op1,
1337 relation_kind rel ATTRIBUTE_UNUSED) const;
1338 virtual void wi_fold (irange &r, tree type,
1339 const wide_int &lh_lb,
1340 const wide_int &lh_ub,
1341 const wide_int &rh_lb,
1342 const wide_int &rh_ub) const;
1343 virtual relation_kind lhs_op1_relation (const irange &lhs,
1344 const irange &op1,
1345 const irange &op2,
1346 relation_kind rel) const;
1347 virtual bool op1_op2_relation_effect (irange &lhs_range,
1348 tree type,
1349 const irange &op1_range,
1350 const irange &op2_range,
1351 relation_kind rel) const;
1352 } op_minus;
1354 void
1355 operator_minus::wi_fold (irange &r, tree type,
1356 const wide_int &lh_lb, const wide_int &lh_ub,
1357 const wide_int &rh_lb, const wide_int &rh_ub) const
1359 wi::overflow_type ov_lb, ov_ub;
1360 signop s = TYPE_SIGN (type);
1361 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1362 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1363 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1367 // Return the relation between LHS and OP1 based on the relation between
1368 // OP1 and OP2.
1370 relation_kind
1371 operator_minus::lhs_op1_relation (const irange &, const irange &op1,
1372 const irange &, relation_kind rel) const
1374 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
1375 switch (rel)
1377 case VREL_GT:
1378 return VREL_LT;
1379 case VREL_GE:
1380 return VREL_LE;
1381 default:
1382 break;
1384 return VREL_VARYING;
1387 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1388 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1389 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1391 static bool
1392 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1393 const irange &op1_range ATTRIBUTE_UNUSED,
1394 const irange &op2_range ATTRIBUTE_UNUSED,
1395 relation_kind rel)
1397 if (rel == VREL_VARYING)
1398 return false;
1400 int_range<2> rel_range;
1401 unsigned prec = TYPE_PRECISION (type);
1402 signop sgn = TYPE_SIGN (type);
1404 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1405 if (rel == VREL_EQ)
1406 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1407 else if (rel == VREL_NE)
1408 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1409 VR_ANTI_RANGE);
1410 else if (TYPE_OVERFLOW_WRAPS (type))
1412 switch (rel)
1414 // For wrapping signed values and unsigned, if op1 > op2 or
1415 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1416 case VREL_GT:
1417 case VREL_LT:
1418 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1419 VR_ANTI_RANGE);
1420 break;
1421 default:
1422 return false;
1425 else
1427 switch (rel)
1429 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1430 case VREL_GT:
1431 rel_range = int_range<2> (type, wi::one (prec),
1432 wi::max_value (prec, sgn));
1433 break;
1434 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1435 case VREL_GE:
1436 rel_range = int_range<2> (type, wi::zero (prec),
1437 wi::max_value (prec, sgn));
1438 break;
1439 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1440 case VREL_LT:
1441 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1442 wi::minus_one (prec));
1443 break;
1444 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1445 case VREL_LE:
1446 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1447 wi::zero (prec));
1448 break;
1449 default:
1450 return false;
1453 lhs_range.intersect (rel_range);
1454 return true;
1457 bool
1458 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1459 const irange &op1_range,
1460 const irange &op2_range,
1461 relation_kind rel) const
1463 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1464 rel);
1467 bool
1468 operator_minus::op1_range (irange &r, tree type,
1469 const irange &lhs,
1470 const irange &op2,
1471 relation_kind rel ATTRIBUTE_UNUSED) const
1473 return range_op_handler (PLUS_EXPR, type).fold_range (r, type, lhs, op2);
1476 bool
1477 operator_minus::op2_range (irange &r, tree type,
1478 const irange &lhs,
1479 const irange &op1,
1480 relation_kind rel ATTRIBUTE_UNUSED) const
1482 return fold_range (r, type, op1, lhs);
1486 class operator_pointer_diff : public range_operator
1488 virtual bool op1_op2_relation_effect (irange &lhs_range,
1489 tree type,
1490 const irange &op1_range,
1491 const irange &op2_range,
1492 relation_kind rel) const;
1493 } op_pointer_diff;
1495 bool
1496 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1497 const irange &op1_range,
1498 const irange &op2_range,
1499 relation_kind rel) const
1501 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1502 rel);
1506 class operator_min : public range_operator
1508 public:
1509 virtual void wi_fold (irange &r, tree type,
1510 const wide_int &lh_lb,
1511 const wide_int &lh_ub,
1512 const wide_int &rh_lb,
1513 const wide_int &rh_ub) const;
1514 } op_min;
1516 void
1517 operator_min::wi_fold (irange &r, tree type,
1518 const wide_int &lh_lb, const wide_int &lh_ub,
1519 const wide_int &rh_lb, const wide_int &rh_ub) const
1521 signop s = TYPE_SIGN (type);
1522 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1523 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1524 value_range_with_overflow (r, type, new_lb, new_ub);
1528 class operator_max : public range_operator
1530 public:
1531 virtual void wi_fold (irange &r, tree type,
1532 const wide_int &lh_lb,
1533 const wide_int &lh_ub,
1534 const wide_int &rh_lb,
1535 const wide_int &rh_ub) const;
1536 } op_max;
1538 void
1539 operator_max::wi_fold (irange &r, tree type,
1540 const wide_int &lh_lb, const wide_int &lh_ub,
1541 const wide_int &rh_lb, const wide_int &rh_ub) const
1543 signop s = TYPE_SIGN (type);
1544 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1545 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1546 value_range_with_overflow (r, type, new_lb, new_ub);
1550 class cross_product_operator : public range_operator
1552 public:
1553 // Perform an operation between two wide-ints and place the result
1554 // in R. Return true if the operation overflowed.
1555 virtual bool wi_op_overflows (wide_int &r,
1556 tree type,
1557 const wide_int &,
1558 const wide_int &) const = 0;
1560 // Calculate the cross product of two sets of sub-ranges and return it.
1561 void wi_cross_product (irange &r, tree type,
1562 const wide_int &lh_lb,
1563 const wide_int &lh_ub,
1564 const wide_int &rh_lb,
1565 const wide_int &rh_ub) const;
1568 // Calculate the cross product of two sets of ranges and return it.
1570 // Multiplications, divisions and shifts are a bit tricky to handle,
1571 // depending on the mix of signs we have in the two ranges, we need to
1572 // operate on different values to get the minimum and maximum values
1573 // for the new range. One approach is to figure out all the
1574 // variations of range combinations and do the operations.
1576 // However, this involves several calls to compare_values and it is
1577 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1578 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1579 // figure the smallest and largest values to form the new range.
1581 void
1582 cross_product_operator::wi_cross_product (irange &r, tree type,
1583 const wide_int &lh_lb,
1584 const wide_int &lh_ub,
1585 const wide_int &rh_lb,
1586 const wide_int &rh_ub) const
1588 wide_int cp1, cp2, cp3, cp4;
1589 // Default to varying.
1590 r.set_varying (type);
1592 // Compute the 4 cross operations, bailing if we get an overflow we
1593 // can't handle.
1594 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1595 return;
1596 if (wi::eq_p (lh_lb, lh_ub))
1597 cp3 = cp1;
1598 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1599 return;
1600 if (wi::eq_p (rh_lb, rh_ub))
1601 cp2 = cp1;
1602 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1603 return;
1604 if (wi::eq_p (lh_lb, lh_ub))
1605 cp4 = cp2;
1606 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1607 return;
1609 // Order pairs.
1610 signop sign = TYPE_SIGN (type);
1611 if (wi::gt_p (cp1, cp2, sign))
1612 std::swap (cp1, cp2);
1613 if (wi::gt_p (cp3, cp4, sign))
1614 std::swap (cp3, cp4);
1616 // Choose min and max from the ordered pairs.
1617 wide_int res_lb = wi::min (cp1, cp3, sign);
1618 wide_int res_ub = wi::max (cp2, cp4, sign);
1619 value_range_with_overflow (r, type, res_lb, res_ub);
1623 class operator_mult : public cross_product_operator
1625 using range_operator::op1_range;
1626 using range_operator::op2_range;
1627 public:
1628 virtual void wi_fold (irange &r, tree type,
1629 const wide_int &lh_lb,
1630 const wide_int &lh_ub,
1631 const wide_int &rh_lb,
1632 const wide_int &rh_ub) const;
1633 virtual bool wi_op_overflows (wide_int &res, tree type,
1634 const wide_int &w0, const wide_int &w1) const;
1635 virtual bool op1_range (irange &r, tree type,
1636 const irange &lhs,
1637 const irange &op2,
1638 relation_kind rel ATTRIBUTE_UNUSED) const;
1639 virtual bool op2_range (irange &r, tree type,
1640 const irange &lhs,
1641 const irange &op1,
1642 relation_kind rel ATTRIBUTE_UNUSED) const;
1643 } op_mult;
1645 bool
1646 operator_mult::op1_range (irange &r, tree type,
1647 const irange &lhs, const irange &op2,
1648 relation_kind rel ATTRIBUTE_UNUSED) const
1650 tree offset;
1652 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1653 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1654 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1655 if (TYPE_OVERFLOW_WRAPS (type))
1656 return false;
1658 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1659 return range_op_handler (TRUNC_DIV_EXPR, type).fold_range (r, type,
1660 lhs, op2);
1661 return false;
1664 bool
1665 operator_mult::op2_range (irange &r, tree type,
1666 const irange &lhs, const irange &op1,
1667 relation_kind rel) const
1669 return operator_mult::op1_range (r, type, lhs, op1, rel);
1672 bool
1673 operator_mult::wi_op_overflows (wide_int &res, tree type,
1674 const wide_int &w0, const wide_int &w1) const
1676 wi::overflow_type overflow = wi::OVF_NONE;
1677 signop sign = TYPE_SIGN (type);
1678 res = wi::mul (w0, w1, sign, &overflow);
1679 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1681 // For multiplication, the sign of the overflow is given
1682 // by the comparison of the signs of the operands.
1683 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1684 res = wi::max_value (w0.get_precision (), sign);
1685 else
1686 res = wi::min_value (w0.get_precision (), sign);
1687 return false;
1689 return overflow;
1692 void
1693 operator_mult::wi_fold (irange &r, tree type,
1694 const wide_int &lh_lb, const wide_int &lh_ub,
1695 const wide_int &rh_lb, const wide_int &rh_ub) const
1697 if (TYPE_OVERFLOW_UNDEFINED (type))
1699 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1700 return;
1703 // Multiply the ranges when overflow wraps. This is basically fancy
1704 // code so we don't drop to varying with an unsigned
1705 // [-3,-1]*[-3,-1].
1707 // This test requires 2*prec bits if both operands are signed and
1708 // 2*prec + 2 bits if either is not. Therefore, extend the values
1709 // using the sign of the result to PREC2. From here on out,
1710 // everthing is just signed math no matter what the input types
1711 // were.
1713 signop sign = TYPE_SIGN (type);
1714 unsigned prec = TYPE_PRECISION (type);
1715 widest2_int min0 = widest2_int::from (lh_lb, sign);
1716 widest2_int max0 = widest2_int::from (lh_ub, sign);
1717 widest2_int min1 = widest2_int::from (rh_lb, sign);
1718 widest2_int max1 = widest2_int::from (rh_ub, sign);
1719 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1720 widest2_int size = sizem1 + 1;
1722 // Canonicalize the intervals.
1723 if (sign == UNSIGNED)
1725 if (wi::ltu_p (size, min0 + max0))
1727 min0 -= size;
1728 max0 -= size;
1730 if (wi::ltu_p (size, min1 + max1))
1732 min1 -= size;
1733 max1 -= size;
1737 // Sort the 4 products so that min is in prod0 and max is in
1738 // prod3.
1739 widest2_int prod0 = min0 * min1;
1740 widest2_int prod1 = min0 * max1;
1741 widest2_int prod2 = max0 * min1;
1742 widest2_int prod3 = max0 * max1;
1744 // min0min1 > max0max1
1745 if (prod0 > prod3)
1746 std::swap (prod0, prod3);
1748 // min0max1 > max0min1
1749 if (prod1 > prod2)
1750 std::swap (prod1, prod2);
1752 if (prod0 > prod1)
1753 std::swap (prod0, prod1);
1755 if (prod2 > prod3)
1756 std::swap (prod2, prod3);
1758 // diff = max - min
1759 prod2 = prod3 - prod0;
1760 if (wi::geu_p (prod2, sizem1))
1761 // The range covers all values.
1762 r.set_varying (type);
1763 else
1765 wide_int new_lb = wide_int::from (prod0, prec, sign);
1766 wide_int new_ub = wide_int::from (prod3, prec, sign);
1767 create_possibly_reversed_range (r, type, new_lb, new_ub);
1772 class operator_div : public cross_product_operator
1774 public:
1775 operator_div (enum tree_code c) { code = c; }
1776 virtual void wi_fold (irange &r, tree type,
1777 const wide_int &lh_lb,
1778 const wide_int &lh_ub,
1779 const wide_int &rh_lb,
1780 const wide_int &rh_ub) const;
1781 virtual bool wi_op_overflows (wide_int &res, tree type,
1782 const wide_int &, const wide_int &) const;
1783 private:
1784 enum tree_code code;
1787 bool
1788 operator_div::wi_op_overflows (wide_int &res, tree type,
1789 const wide_int &w0, const wide_int &w1) const
1791 if (w1 == 0)
1792 return true;
1794 wi::overflow_type overflow = wi::OVF_NONE;
1795 signop sign = TYPE_SIGN (type);
1797 switch (code)
1799 case EXACT_DIV_EXPR:
1800 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1801 // operator_exact_divide. No need to handle it here.
1802 gcc_unreachable ();
1803 break;
1804 case TRUNC_DIV_EXPR:
1805 res = wi::div_trunc (w0, w1, sign, &overflow);
1806 break;
1807 case FLOOR_DIV_EXPR:
1808 res = wi::div_floor (w0, w1, sign, &overflow);
1809 break;
1810 case ROUND_DIV_EXPR:
1811 res = wi::div_round (w0, w1, sign, &overflow);
1812 break;
1813 case CEIL_DIV_EXPR:
1814 res = wi::div_ceil (w0, w1, sign, &overflow);
1815 break;
1816 default:
1817 gcc_unreachable ();
1820 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1822 // For division, the only case is -INF / -1 = +INF.
1823 res = wi::max_value (w0.get_precision (), sign);
1824 return false;
1826 return overflow;
1829 void
1830 operator_div::wi_fold (irange &r, tree type,
1831 const wide_int &lh_lb, const wide_int &lh_ub,
1832 const wide_int &rh_lb, const wide_int &rh_ub) const
1834 const wide_int dividend_min = lh_lb;
1835 const wide_int dividend_max = lh_ub;
1836 const wide_int divisor_min = rh_lb;
1837 const wide_int divisor_max = rh_ub;
1838 signop sign = TYPE_SIGN (type);
1839 unsigned prec = TYPE_PRECISION (type);
1840 wide_int extra_min, extra_max;
1842 // If we know we won't divide by zero, just do the division.
1843 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1845 wi_cross_product (r, type, dividend_min, dividend_max,
1846 divisor_min, divisor_max);
1847 return;
1850 // If we're definitely dividing by zero, there's nothing to do.
1851 if (wi_zero_p (type, divisor_min, divisor_max))
1853 r.set_undefined ();
1854 return;
1857 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1858 // skip any division by zero.
1860 // First divide by the negative numbers, if any.
1861 if (wi::neg_p (divisor_min, sign))
1862 wi_cross_product (r, type, dividend_min, dividend_max,
1863 divisor_min, wi::minus_one (prec));
1864 else
1865 r.set_undefined ();
1867 // Then divide by the non-zero positive numbers, if any.
1868 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1870 int_range_max tmp;
1871 wi_cross_product (tmp, type, dividend_min, dividend_max,
1872 wi::one (prec), divisor_max);
1873 r.union_ (tmp);
1875 // We shouldn't still have undefined here.
1876 gcc_checking_assert (!r.undefined_p ());
1879 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1880 operator_div op_floor_div (FLOOR_DIV_EXPR);
1881 operator_div op_round_div (ROUND_DIV_EXPR);
1882 operator_div op_ceil_div (CEIL_DIV_EXPR);
1885 class operator_exact_divide : public operator_div
1887 using range_operator::op1_range;
1888 public:
1889 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1890 virtual bool op1_range (irange &r, tree type,
1891 const irange &lhs,
1892 const irange &op2,
1893 relation_kind rel ATTRIBUTE_UNUSED) const;
1895 } op_exact_div;
1897 bool
1898 operator_exact_divide::op1_range (irange &r, tree type,
1899 const irange &lhs,
1900 const irange &op2,
1901 relation_kind rel ATTRIBUTE_UNUSED) const
1903 tree offset;
1904 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1905 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1906 // We wont bother trying to enumerate all the in between stuff :-P
1907 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1908 // the time however.
1909 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1910 if (op2.singleton_p (&offset)
1911 && !integer_zerop (offset))
1912 return range_op_handler (MULT_EXPR, type).fold_range (r, type, lhs, op2);
1913 return false;
1917 class operator_lshift : public cross_product_operator
1919 using range_operator::fold_range;
1920 using range_operator::op1_range;
1921 public:
1922 virtual bool op1_range (irange &r, tree type,
1923 const irange &lhs,
1924 const irange &op2,
1925 relation_kind rel = VREL_VARYING) const;
1926 virtual bool fold_range (irange &r, tree type,
1927 const irange &op1,
1928 const irange &op2,
1929 relation_kind rel = VREL_VARYING) const;
1931 virtual void wi_fold (irange &r, tree type,
1932 const wide_int &lh_lb, const wide_int &lh_ub,
1933 const wide_int &rh_lb, const wide_int &rh_ub) const;
1934 virtual bool wi_op_overflows (wide_int &res,
1935 tree type,
1936 const wide_int &,
1937 const wide_int &) const;
1938 } op_lshift;
1940 class operator_rshift : public cross_product_operator
1942 using range_operator::fold_range;
1943 using range_operator::op1_range;
1944 using range_operator::lhs_op1_relation;
1945 public:
1946 virtual bool fold_range (irange &r, tree type,
1947 const irange &op1,
1948 const irange &op2,
1949 relation_kind rel = VREL_VARYING) const;
1950 virtual void wi_fold (irange &r, tree type,
1951 const wide_int &lh_lb,
1952 const wide_int &lh_ub,
1953 const wide_int &rh_lb,
1954 const wide_int &rh_ub) const;
1955 virtual bool wi_op_overflows (wide_int &res,
1956 tree type,
1957 const wide_int &w0,
1958 const wide_int &w1) const;
1959 virtual bool op1_range (irange &, tree type,
1960 const irange &lhs,
1961 const irange &op2,
1962 relation_kind rel = VREL_VARYING) const;
1963 virtual relation_kind lhs_op1_relation (const irange &lhs,
1964 const irange &op1,
1965 const irange &op2,
1966 relation_kind rel) const;
1967 } op_rshift;
1970 relation_kind
1971 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
1972 const irange &op1,
1973 const irange &op2,
1974 relation_kind) const
1976 // If both operands range are >= 0, then the LHS <= op1.
1977 if (!op1.undefined_p () && !op2.undefined_p ()
1978 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
1979 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
1980 return VREL_LE;
1981 return VREL_VARYING;
1984 bool
1985 operator_lshift::fold_range (irange &r, tree type,
1986 const irange &op1,
1987 const irange &op2,
1988 relation_kind rel) const
1990 int_range_max shift_range;
1991 if (!get_shift_range (shift_range, type, op2))
1993 if (op2.undefined_p ())
1994 r.set_undefined ();
1995 else
1996 r.set_varying (type);
1997 return true;
2000 // Transform left shifts by constants into multiplies.
2001 if (shift_range.singleton_p ())
2003 unsigned shift = shift_range.lower_bound ().to_uhwi ();
2004 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2005 int_range<1> mult (type, tmp, tmp);
2007 // Force wrapping multiplication.
2008 bool saved_flag_wrapv = flag_wrapv;
2009 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2010 flag_wrapv = 1;
2011 flag_wrapv_pointer = 1;
2012 bool b = op_mult.fold_range (r, type, op1, mult);
2013 flag_wrapv = saved_flag_wrapv;
2014 flag_wrapv_pointer = saved_flag_wrapv_pointer;
2015 return b;
2017 else
2018 // Otherwise, invoke the generic fold routine.
2019 return range_operator::fold_range (r, type, op1, shift_range, rel);
2022 void
2023 operator_lshift::wi_fold (irange &r, tree type,
2024 const wide_int &lh_lb, const wide_int &lh_ub,
2025 const wide_int &rh_lb, const wide_int &rh_ub) const
2027 signop sign = TYPE_SIGN (type);
2028 unsigned prec = TYPE_PRECISION (type);
2029 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2030 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2031 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2032 // overflow. However, for that to happen, rh.max needs to be zero,
2033 // which means rh is a singleton range of zero, which means we simply return
2034 // [lh_lb, lh_ub] as the range.
2035 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2037 r = int_range<2> (type, lh_lb, lh_ub);
2038 return;
2041 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2042 wide_int complement = ~(bound - 1);
2043 wide_int low_bound, high_bound;
2044 bool in_bounds = false;
2046 if (sign == UNSIGNED)
2048 low_bound = bound;
2049 high_bound = complement;
2050 if (wi::ltu_p (lh_ub, low_bound))
2052 // [5, 6] << [1, 2] == [10, 24].
2053 // We're shifting out only zeroes, the value increases
2054 // monotonically.
2055 in_bounds = true;
2057 else if (wi::ltu_p (high_bound, lh_lb))
2059 // [0xffffff00, 0xffffffff] << [1, 2]
2060 // == [0xfffffc00, 0xfffffffe].
2061 // We're shifting out only ones, the value decreases
2062 // monotonically.
2063 in_bounds = true;
2066 else
2068 // [-1, 1] << [1, 2] == [-4, 4]
2069 low_bound = complement;
2070 high_bound = bound;
2071 if (wi::lts_p (lh_ub, high_bound)
2072 && wi::lts_p (low_bound, lh_lb))
2074 // For non-negative numbers, we're shifting out only zeroes,
2075 // the value increases monotonically. For negative numbers,
2076 // we're shifting out only ones, the value decreases
2077 // monotonically.
2078 in_bounds = true;
2082 if (in_bounds)
2083 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2084 else
2085 r.set_varying (type);
2088 bool
2089 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2090 const wide_int &w0, const wide_int &w1) const
2092 signop sign = TYPE_SIGN (type);
2093 if (wi::neg_p (w1))
2095 // It's unclear from the C standard whether shifts can overflow.
2096 // The following code ignores overflow; perhaps a C standard
2097 // interpretation ruling is needed.
2098 res = wi::rshift (w0, -w1, sign);
2100 else
2101 res = wi::lshift (w0, w1);
2102 return false;
2105 bool
2106 operator_lshift::op1_range (irange &r,
2107 tree type,
2108 const irange &lhs,
2109 const irange &op2,
2110 relation_kind rel ATTRIBUTE_UNUSED) const
2112 tree shift_amount;
2114 if (!lhs.contains_p (build_zero_cst (type)))
2115 r.set_nonzero (type);
2116 else
2117 r.set_varying (type);
2119 if (op2.singleton_p (&shift_amount))
2121 wide_int shift = wi::to_wide (shift_amount);
2122 if (wi::lt_p (shift, 0, SIGNED))
2123 return false;
2124 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2125 TYPE_PRECISION (op2.type ())),
2126 UNSIGNED))
2127 return false;
2128 if (shift == 0)
2130 r.intersect (lhs);
2131 return true;
2134 // Work completely in unsigned mode to start.
2135 tree utype = type;
2136 int_range_max tmp_range;
2137 if (TYPE_SIGN (type) == SIGNED)
2139 int_range_max tmp = lhs;
2140 utype = unsigned_type_for (type);
2141 range_cast (tmp, utype);
2142 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2144 else
2145 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2147 // Start with ranges which can produce the LHS by right shifting the
2148 // result by the shift amount.
2149 // ie [0x08, 0xF0] = op1 << 2 will start with
2150 // [00001000, 11110000] = op1 << 2
2151 // [0x02, 0x4C] aka [00000010, 00111100]
2153 // Then create a range from the LB with the least significant upper bit
2154 // set, to the upper bound with all the bits set.
2155 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2157 // Ideally we do this for each subrange, but just lump them all for now.
2158 unsigned low_bits = TYPE_PRECISION (utype)
2159 - TREE_INT_CST_LOW (shift_amount);
2160 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2161 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2162 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2163 int_range<2> fill_range (utype, new_lb, new_ub);
2164 tmp_range.union_ (fill_range);
2166 if (utype != type)
2167 range_cast (tmp_range, type);
2169 r.intersect (tmp_range);
2170 return true;
2173 return !r.varying_p ();
2176 bool
2177 operator_rshift::op1_range (irange &r,
2178 tree type,
2179 const irange &lhs,
2180 const irange &op2,
2181 relation_kind rel ATTRIBUTE_UNUSED) const
2183 tree shift;
2184 if (op2.singleton_p (&shift))
2186 // Ignore nonsensical shifts.
2187 unsigned prec = TYPE_PRECISION (type);
2188 if (wi::ge_p (wi::to_wide (shift),
2189 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2190 UNSIGNED))
2191 return false;
2192 if (wi::to_wide (shift) == 0)
2194 r = lhs;
2195 return true;
2198 // Folding the original operation may discard some impossible
2199 // ranges from the LHS.
2200 int_range_max lhs_refined;
2201 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2202 lhs_refined.intersect (lhs);
2203 if (lhs_refined.undefined_p ())
2205 r.set_undefined ();
2206 return true;
2208 int_range_max shift_range (shift, shift);
2209 int_range_max lb, ub;
2210 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2211 // LHS
2212 // 0000 0111 = OP1 >> 3
2214 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2215 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2216 // right hand side (0x07).
2217 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2218 fold_build2 (LSHIFT_EXPR, type,
2219 build_minus_one_cst (type),
2220 shift));
2221 int_range_max mask_range (build_zero_cst (type), mask);
2222 op_plus.fold_range (ub, type, lb, mask_range);
2223 r = lb;
2224 r.union_ (ub);
2225 if (!lhs_refined.contains_p (build_zero_cst (type)))
2227 mask_range.invert ();
2228 r.intersect (mask_range);
2230 return true;
2232 return false;
2235 bool
2236 operator_rshift::wi_op_overflows (wide_int &res,
2237 tree type,
2238 const wide_int &w0,
2239 const wide_int &w1) const
2241 signop sign = TYPE_SIGN (type);
2242 if (wi::neg_p (w1))
2243 res = wi::lshift (w0, -w1);
2244 else
2246 // It's unclear from the C standard whether shifts can overflow.
2247 // The following code ignores overflow; perhaps a C standard
2248 // interpretation ruling is needed.
2249 res = wi::rshift (w0, w1, sign);
2251 return false;
2254 bool
2255 operator_rshift::fold_range (irange &r, tree type,
2256 const irange &op1,
2257 const irange &op2,
2258 relation_kind rel) const
2260 int_range_max shift;
2261 if (!get_shift_range (shift, type, op2))
2263 if (op2.undefined_p ())
2264 r.set_undefined ();
2265 else
2266 r.set_varying (type);
2267 return true;
2270 return range_operator::fold_range (r, type, op1, shift, rel);
2273 void
2274 operator_rshift::wi_fold (irange &r, tree type,
2275 const wide_int &lh_lb, const wide_int &lh_ub,
2276 const wide_int &rh_lb, const wide_int &rh_ub) const
2278 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2282 class operator_cast: public range_operator
2284 using range_operator::fold_range;
2285 using range_operator::op1_range;
2286 public:
2287 virtual bool fold_range (irange &r, tree type,
2288 const irange &op1,
2289 const irange &op2,
2290 relation_kind rel = VREL_VARYING) const;
2291 virtual bool op1_range (irange &r, tree type,
2292 const irange &lhs,
2293 const irange &op2,
2294 relation_kind rel = VREL_VARYING) const;
2295 private:
2296 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2297 bool inside_domain_p (const wide_int &min, const wide_int &max,
2298 const irange &outer) const;
2299 void fold_pair (irange &r, unsigned index, const irange &inner,
2300 const irange &outer) const;
2301 } op_convert;
2303 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2305 inline bool
2306 operator_cast::truncating_cast_p (const irange &inner,
2307 const irange &outer) const
2309 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2312 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2314 bool
2315 operator_cast::inside_domain_p (const wide_int &min,
2316 const wide_int &max,
2317 const irange &range) const
2319 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2320 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2321 signop domain_sign = TYPE_SIGN (range.type ());
2322 return (wi::le_p (min, domain_max, domain_sign)
2323 && wi::le_p (max, domain_max, domain_sign)
2324 && wi::ge_p (min, domain_min, domain_sign)
2325 && wi::ge_p (max, domain_min, domain_sign));
2329 // Helper for fold_range which work on a pair at a time.
2331 void
2332 operator_cast::fold_pair (irange &r, unsigned index,
2333 const irange &inner,
2334 const irange &outer) const
2336 tree inner_type = inner.type ();
2337 tree outer_type = outer.type ();
2338 signop inner_sign = TYPE_SIGN (inner_type);
2339 unsigned outer_prec = TYPE_PRECISION (outer_type);
2341 // check to see if casting from INNER to OUTER is a conversion that
2342 // fits in the resulting OUTER type.
2343 wide_int inner_lb = inner.lower_bound (index);
2344 wide_int inner_ub = inner.upper_bound (index);
2345 if (truncating_cast_p (inner, outer))
2347 // We may be able to accomodate a truncating cast if the
2348 // resulting range can be represented in the target type...
2349 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2350 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2351 inner_sign) != 0)
2353 r.set_varying (outer_type);
2354 return;
2357 // ...but we must still verify that the final range fits in the
2358 // domain. This catches -fstrict-enum restrictions where the domain
2359 // range is smaller than what fits in the underlying type.
2360 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2361 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2362 if (inside_domain_p (min, max, outer))
2363 create_possibly_reversed_range (r, outer_type, min, max);
2364 else
2365 r.set_varying (outer_type);
2369 bool
2370 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2371 const irange &inner,
2372 const irange &outer,
2373 relation_kind rel ATTRIBUTE_UNUSED) const
2375 if (empty_range_varying (r, type, inner, outer))
2376 return true;
2378 gcc_checking_assert (outer.varying_p ());
2379 gcc_checking_assert (inner.num_pairs () > 0);
2381 // Avoid a temporary by folding the first pair directly into the result.
2382 fold_pair (r, 0, inner, outer);
2384 // Then process any additonal pairs by unioning with their results.
2385 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2387 int_range_max tmp;
2388 fold_pair (tmp, x, inner, outer);
2389 r.union_ (tmp);
2390 if (r.varying_p ())
2391 return true;
2393 return true;
2396 bool
2397 operator_cast::op1_range (irange &r, tree type,
2398 const irange &lhs,
2399 const irange &op2,
2400 relation_kind rel ATTRIBUTE_UNUSED) const
2402 tree lhs_type = lhs.type ();
2403 gcc_checking_assert (types_compatible_p (op2.type(), type));
2405 // If we are calculating a pointer, shortcut to what we really care about.
2406 if (POINTER_TYPE_P (type))
2408 // Conversion from other pointers or a constant (including 0/NULL)
2409 // are straightforward.
2410 if (POINTER_TYPE_P (lhs.type ())
2411 || (lhs.singleton_p ()
2412 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2414 r = lhs;
2415 range_cast (r, type);
2417 else
2419 // If the LHS is not a pointer nor a singleton, then it is
2420 // either VARYING or non-zero.
2421 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2422 r.set_nonzero (type);
2423 else
2424 r.set_varying (type);
2426 r.intersect (op2);
2427 return true;
2430 if (truncating_cast_p (op2, lhs))
2432 if (lhs.varying_p ())
2433 r.set_varying (type);
2434 else
2436 // We want to insert the LHS as an unsigned value since it
2437 // would not trigger the signed bit of the larger type.
2438 int_range_max converted_lhs = lhs;
2439 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2440 range_cast (converted_lhs, type);
2441 // Start by building the positive signed outer range for the type.
2442 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2443 TYPE_PRECISION (type));
2444 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2445 SIGNED));
2446 // For the signed part, we need to simply union the 2 ranges now.
2447 r.union_ (converted_lhs);
2449 // Create maximal negative number outside of LHS bits.
2450 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2451 TYPE_PRECISION (type));
2452 // Add this to the unsigned LHS range(s).
2453 int_range_max lim_range (type, lim, lim);
2454 int_range_max lhs_neg;
2455 range_op_handler (PLUS_EXPR, type).fold_range (lhs_neg, type,
2456 converted_lhs,
2457 lim_range);
2458 // lhs_neg now has all the negative versions of the LHS.
2459 // Now union in all the values from SIGNED MIN (0x80000) to
2460 // lim-1 in order to fill in all the ranges with the upper
2461 // bits set.
2463 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2464 // we don't need to create a range from min to lim-1
2465 // calculate neg range traps trying to create [lim, lim - 1].
2466 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2467 if (lim != min_val)
2469 int_range_max neg (type,
2470 wi::min_value (TYPE_PRECISION (type),
2471 SIGNED),
2472 lim - 1);
2473 lhs_neg.union_ (neg);
2475 // And finally, munge the signed and unsigned portions.
2476 r.union_ (lhs_neg);
2478 // And intersect with any known value passed in the extra operand.
2479 r.intersect (op2);
2480 return true;
2483 int_range_max tmp;
2484 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2485 tmp = lhs;
2486 else
2488 // The cast is not truncating, and the range is restricted to
2489 // the range of the RHS by this assignment.
2491 // Cast the range of the RHS to the type of the LHS.
2492 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2493 // Intersect this with the LHS range will produce the range,
2494 // which will be cast to the RHS type before returning.
2495 tmp.intersect (lhs);
2498 // Cast the calculated range to the type of the RHS.
2499 fold_range (r, type, tmp, int_range<1> (type));
2500 return true;
2504 class operator_logical_and : public range_operator
2506 using range_operator::fold_range;
2507 using range_operator::op1_range;
2508 using range_operator::op2_range;
2509 public:
2510 virtual bool fold_range (irange &r, tree type,
2511 const irange &lh,
2512 const irange &rh,
2513 relation_kind rel = VREL_VARYING) const;
2514 virtual bool op1_range (irange &r, tree type,
2515 const irange &lhs,
2516 const irange &op2,
2517 relation_kind rel = VREL_VARYING) const;
2518 virtual bool op2_range (irange &r, tree type,
2519 const irange &lhs,
2520 const irange &op1,
2521 relation_kind rel = VREL_VARYING) const;
2522 } op_logical_and;
2525 bool
2526 operator_logical_and::fold_range (irange &r, tree type,
2527 const irange &lh,
2528 const irange &rh,
2529 relation_kind rel ATTRIBUTE_UNUSED) const
2531 if (empty_range_varying (r, type, lh, rh))
2532 return true;
2534 // 0 && anything is 0.
2535 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2536 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2537 r = range_false (type);
2538 else if (lh.contains_p (build_zero_cst (lh.type ()))
2539 || rh.contains_p (build_zero_cst (rh.type ())))
2540 // To reach this point, there must be a logical 1 on each side, and
2541 // the only remaining question is whether there is a zero or not.
2542 r = range_true_and_false (type);
2543 else
2544 r = range_true (type);
2545 return true;
2548 bool
2549 operator_logical_and::op1_range (irange &r, tree type,
2550 const irange &lhs,
2551 const irange &op2 ATTRIBUTE_UNUSED,
2552 relation_kind rel ATTRIBUTE_UNUSED) const
2554 switch (get_bool_state (r, lhs, type))
2556 case BRS_TRUE:
2557 // A true result means both sides of the AND must be true.
2558 r = range_true (type);
2559 break;
2560 default:
2561 // Any other result means only one side has to be false, the
2562 // other side can be anything. So we cannot be sure of any
2563 // result here.
2564 r = range_true_and_false (type);
2565 break;
2567 return true;
2570 bool
2571 operator_logical_and::op2_range (irange &r, tree type,
2572 const irange &lhs,
2573 const irange &op1,
2574 relation_kind rel ATTRIBUTE_UNUSED) const
2576 return operator_logical_and::op1_range (r, type, lhs, op1);
2580 class operator_bitwise_and : public range_operator
2582 using range_operator::fold_range;
2583 using range_operator::op1_range;
2584 using range_operator::op2_range;
2585 public:
2586 virtual bool fold_range (irange &r, tree type,
2587 const irange &lh,
2588 const irange &rh,
2589 relation_kind rel = VREL_VARYING) const;
2590 virtual bool op1_range (irange &r, tree type,
2591 const irange &lhs,
2592 const irange &op2,
2593 relation_kind rel = VREL_VARYING) const;
2594 virtual bool op2_range (irange &r, tree type,
2595 const irange &lhs,
2596 const irange &op1,
2597 relation_kind rel = VREL_VARYING) const;
2598 virtual void wi_fold (irange &r, tree type,
2599 const wide_int &lh_lb,
2600 const wide_int &lh_ub,
2601 const wide_int &rh_lb,
2602 const wide_int &rh_ub) const;
2603 private:
2604 void simple_op1_range_solver (irange &r, tree type,
2605 const irange &lhs,
2606 const irange &op2) const;
2607 void remove_impossible_ranges (irange &r, const irange &rh) const;
2608 } op_bitwise_and;
2610 static bool
2611 unsigned_singleton_p (const irange &op)
2613 tree mask;
2614 if (op.singleton_p (&mask))
2616 wide_int x = wi::to_wide (mask);
2617 return wi::ge_p (x, 0, TYPE_SIGN (op.type ()));
2619 return false;
2622 // Remove any ranges from R that are known to be impossible when an
2623 // range is ANDed with MASK.
2625 void
2626 operator_bitwise_and::remove_impossible_ranges (irange &r,
2627 const irange &rmask) const
2629 if (r.undefined_p () || !unsigned_singleton_p (rmask))
2630 return;
2632 wide_int mask = rmask.lower_bound ();
2633 tree type = r.type ();
2634 int prec = TYPE_PRECISION (type);
2635 int leading_zeros = wi::clz (mask);
2636 int_range_max impossible_ranges;
2638 /* We know that starting at the most significant bit, any 0 in the
2639 mask means the resulting range cannot contain a 1 in that same
2640 position. This means the following ranges are impossible:
2642 x & 0b1001 1010
2643 IMPOSSIBLE RANGES
2644 01xx xxxx [0100 0000, 0111 1111]
2645 001x xxxx [0010 0000, 0011 1111]
2646 0000 01xx [0000 0100, 0000 0111]
2647 0000 0001 [0000 0001, 0000 0001]
2649 wide_int one = wi::one (prec);
2650 for (int i = 0; i < prec - leading_zeros - 1; ++i)
2651 if (wi::bit_and (mask, wi::lshift (one, wi::uhwi (i, prec))) == 0)
2653 tree lb = fold_build2 (LSHIFT_EXPR, type,
2654 build_one_cst (type),
2655 build_int_cst (type, i));
2656 tree ub_left = fold_build1 (BIT_NOT_EXPR, type,
2657 fold_build2 (LSHIFT_EXPR, type,
2658 build_minus_one_cst (type),
2659 build_int_cst (type, i)));
2660 tree ub_right = fold_build2 (LSHIFT_EXPR, type,
2661 build_one_cst (type),
2662 build_int_cst (type, i));
2663 tree ub = fold_build2 (BIT_IOR_EXPR, type, ub_left, ub_right);
2664 impossible_ranges.union_ (int_range<1> (lb, ub));
2666 if (!impossible_ranges.undefined_p ())
2668 impossible_ranges.invert ();
2669 r.intersect (impossible_ranges);
2673 bool
2674 operator_bitwise_and::fold_range (irange &r, tree type,
2675 const irange &lh,
2676 const irange &rh,
2677 relation_kind rel ATTRIBUTE_UNUSED) const
2679 if (range_operator::fold_range (r, type, lh, rh))
2681 // FIXME: This is temporarily disabled because, though it
2682 // generates better ranges, it's noticeably slower for evrp.
2683 // remove_impossible_ranges (r, rh);
2684 return true;
2686 return false;
2690 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2691 // by considering the number of leading redundant sign bit copies.
2692 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2693 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2694 static bool
2695 wi_optimize_signed_bitwise_op (irange &r, tree type,
2696 const wide_int &lh_lb, const wide_int &lh_ub,
2697 const wide_int &rh_lb, const wide_int &rh_ub)
2699 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2700 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2701 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2702 if (new_clrsb == 0)
2703 return false;
2704 int type_prec = TYPE_PRECISION (type);
2705 int rprec = (type_prec - new_clrsb) - 1;
2706 value_range_with_overflow (r, type,
2707 wi::mask (rprec, true, type_prec),
2708 wi::mask (rprec, false, type_prec));
2709 return true;
2713 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2714 // possible. Basically, see if we can optimize:
2716 // [LB, UB] op Z
2717 // into:
2718 // [LB op Z, UB op Z]
2720 // If the optimization was successful, accumulate the range in R and
2721 // return TRUE.
2723 static bool
2724 wi_optimize_and_or (irange &r,
2725 enum tree_code code,
2726 tree type,
2727 const wide_int &lh_lb, const wide_int &lh_ub,
2728 const wide_int &rh_lb, const wide_int &rh_ub)
2730 // Calculate the singleton mask among the ranges, if any.
2731 wide_int lower_bound, upper_bound, mask;
2732 if (wi::eq_p (rh_lb, rh_ub))
2734 mask = rh_lb;
2735 lower_bound = lh_lb;
2736 upper_bound = lh_ub;
2738 else if (wi::eq_p (lh_lb, lh_ub))
2740 mask = lh_lb;
2741 lower_bound = rh_lb;
2742 upper_bound = rh_ub;
2744 else
2745 return false;
2747 // If Z is a constant which (for op | its bitwise not) has n
2748 // consecutive least significant bits cleared followed by m 1
2749 // consecutive bits set immediately above it and either
2750 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2752 // The least significant n bits of all the values in the range are
2753 // cleared or set, the m bits above it are preserved and any bits
2754 // above these are required to be the same for all values in the
2755 // range.
2756 wide_int w = mask;
2757 int m = 0, n = 0;
2758 if (code == BIT_IOR_EXPR)
2759 w = ~w;
2760 if (wi::eq_p (w, 0))
2761 n = w.get_precision ();
2762 else
2764 n = wi::ctz (w);
2765 w = ~(w | wi::mask (n, false, w.get_precision ()));
2766 if (wi::eq_p (w, 0))
2767 m = w.get_precision () - n;
2768 else
2769 m = wi::ctz (w) - n;
2771 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2772 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2773 return false;
2775 wide_int res_lb, res_ub;
2776 if (code == BIT_AND_EXPR)
2778 res_lb = wi::bit_and (lower_bound, mask);
2779 res_ub = wi::bit_and (upper_bound, mask);
2781 else if (code == BIT_IOR_EXPR)
2783 res_lb = wi::bit_or (lower_bound, mask);
2784 res_ub = wi::bit_or (upper_bound, mask);
2786 else
2787 gcc_unreachable ();
2788 value_range_with_overflow (r, type, res_lb, res_ub);
2790 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2791 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2793 int_range<2> tmp;
2794 tmp.set_nonzero (type);
2795 r.intersect (tmp);
2797 return true;
2800 // For range [LB, UB] compute two wide_int bit masks.
2802 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2803 // for all numbers in the range the bit is 0, otherwise it might be 0
2804 // or 1.
2806 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2807 // for all numbers in the range the bit is 1, otherwise it might be 0
2808 // or 1.
2810 void
2811 wi_set_zero_nonzero_bits (tree type,
2812 const wide_int &lb, const wide_int &ub,
2813 wide_int &maybe_nonzero,
2814 wide_int &mustbe_nonzero)
2816 signop sign = TYPE_SIGN (type);
2818 if (wi::eq_p (lb, ub))
2819 maybe_nonzero = mustbe_nonzero = lb;
2820 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2822 wide_int xor_mask = lb ^ ub;
2823 maybe_nonzero = lb | ub;
2824 mustbe_nonzero = lb & ub;
2825 if (xor_mask != 0)
2827 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2828 maybe_nonzero.get_precision ());
2829 maybe_nonzero = maybe_nonzero | mask;
2830 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2833 else
2835 maybe_nonzero = wi::minus_one (lb.get_precision ());
2836 mustbe_nonzero = wi::zero (lb.get_precision ());
2840 void
2841 operator_bitwise_and::wi_fold (irange &r, tree type,
2842 const wide_int &lh_lb,
2843 const wide_int &lh_ub,
2844 const wide_int &rh_lb,
2845 const wide_int &rh_ub) const
2847 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2848 return;
2850 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2851 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2852 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2853 maybe_nonzero_lh, mustbe_nonzero_lh);
2854 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2855 maybe_nonzero_rh, mustbe_nonzero_rh);
2857 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2858 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2859 signop sign = TYPE_SIGN (type);
2860 unsigned prec = TYPE_PRECISION (type);
2861 // If both input ranges contain only negative values, we can
2862 // truncate the result range maximum to the minimum of the
2863 // input range maxima.
2864 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2866 new_ub = wi::min (new_ub, lh_ub, sign);
2867 new_ub = wi::min (new_ub, rh_ub, sign);
2869 // If either input range contains only non-negative values
2870 // we can truncate the result range maximum to the respective
2871 // maximum of the input range.
2872 if (wi::ge_p (lh_lb, 0, sign))
2873 new_ub = wi::min (new_ub, lh_ub, sign);
2874 if (wi::ge_p (rh_lb, 0, sign))
2875 new_ub = wi::min (new_ub, rh_ub, sign);
2876 // PR68217: In case of signed & sign-bit-CST should
2877 // result in [-INF, 0] instead of [-INF, INF].
2878 if (wi::gt_p (new_lb, new_ub, sign))
2880 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2881 if (sign == SIGNED
2882 && ((wi::eq_p (lh_lb, lh_ub)
2883 && !wi::cmps (lh_lb, sign_bit))
2884 || (wi::eq_p (rh_lb, rh_ub)
2885 && !wi::cmps (rh_lb, sign_bit))))
2887 new_lb = wi::min_value (prec, sign);
2888 new_ub = wi::zero (prec);
2891 // If the limits got swapped around, return varying.
2892 if (wi::gt_p (new_lb, new_ub,sign))
2894 if (sign == SIGNED
2895 && wi_optimize_signed_bitwise_op (r, type,
2896 lh_lb, lh_ub,
2897 rh_lb, rh_ub))
2898 return;
2899 r.set_varying (type);
2901 else
2902 value_range_with_overflow (r, type, new_lb, new_ub);
2905 static void
2906 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2908 if (!lhs.contains_p (build_zero_cst (type)))
2909 r = range_nonzero (type);
2910 else
2911 r.set_varying (type);
2914 // This was shamelessly stolen from register_edge_assert_for_2 and
2915 // adjusted to work with iranges.
2917 void
2918 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2919 const irange &lhs,
2920 const irange &op2) const
2922 if (!op2.singleton_p ())
2924 set_nonzero_range_from_mask (r, type, lhs);
2925 return;
2927 unsigned int nprec = TYPE_PRECISION (type);
2928 wide_int cst2v = op2.lower_bound ();
2929 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2930 wide_int sgnbit;
2931 if (cst2n)
2932 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2933 else
2934 sgnbit = wi::zero (nprec);
2936 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2938 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2939 // maximum unsigned value is ~0. For signed comparison, if CST2
2940 // doesn't have the most significant bit set, handle it similarly. If
2941 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2942 wide_int valv = lhs.lower_bound ();
2943 wide_int minv = valv & cst2v, maxv;
2944 bool we_know_nothing = false;
2945 if (minv != valv)
2947 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2948 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2949 if (minv == valv)
2951 // If we can't determine anything on this bound, fall
2952 // through and conservatively solve for the other end point.
2953 we_know_nothing = true;
2956 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2957 if (we_know_nothing)
2958 r.set_varying (type);
2959 else
2960 r = int_range<1> (type, minv, maxv);
2962 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2964 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2965 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2966 // VAL2 where
2967 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2968 // as maximum.
2969 // For signed comparison, if CST2 doesn't have most significant bit
2970 // set, handle it similarly. If CST2 has MSB set, the maximum is
2971 // the same and minimum is INT_MIN.
2972 valv = lhs.upper_bound ();
2973 minv = valv & cst2v;
2974 if (minv == valv)
2975 maxv = valv;
2976 else
2978 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2979 if (maxv == valv)
2981 // If we couldn't determine anything on either bound, return
2982 // undefined.
2983 if (we_know_nothing)
2984 r.set_undefined ();
2985 return;
2987 maxv -= 1;
2989 maxv |= ~cst2v;
2990 minv = sgnbit;
2991 int_range<1> upper_bits (type, minv, maxv);
2992 r.intersect (upper_bits);
2995 bool
2996 operator_bitwise_and::op1_range (irange &r, tree type,
2997 const irange &lhs,
2998 const irange &op2,
2999 relation_kind rel ATTRIBUTE_UNUSED) const
3001 if (types_compatible_p (type, boolean_type_node))
3002 return op_logical_and.op1_range (r, type, lhs, op2);
3004 r.set_undefined ();
3005 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
3007 int_range_max chunk (lhs.type (),
3008 lhs.lower_bound (i),
3009 lhs.upper_bound (i));
3010 int_range_max res;
3011 simple_op1_range_solver (res, type, chunk, op2);
3012 r.union_ (res);
3014 if (r.undefined_p ())
3015 set_nonzero_range_from_mask (r, type, lhs);
3016 return true;
3019 bool
3020 operator_bitwise_and::op2_range (irange &r, tree type,
3021 const irange &lhs,
3022 const irange &op1,
3023 relation_kind rel ATTRIBUTE_UNUSED) const
3025 return operator_bitwise_and::op1_range (r, type, lhs, op1);
3029 class operator_logical_or : public range_operator
3031 using range_operator::fold_range;
3032 using range_operator::op1_range;
3033 using range_operator::op2_range;
3034 public:
3035 virtual bool fold_range (irange &r, tree type,
3036 const irange &lh,
3037 const irange &rh,
3038 relation_kind rel = VREL_VARYING) const;
3039 virtual bool op1_range (irange &r, tree type,
3040 const irange &lhs,
3041 const irange &op2,
3042 relation_kind rel = VREL_VARYING) const;
3043 virtual bool op2_range (irange &r, tree type,
3044 const irange &lhs,
3045 const irange &op1,
3046 relation_kind rel = VREL_VARYING) const;
3047 } op_logical_or;
3049 bool
3050 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3051 const irange &lh,
3052 const irange &rh,
3053 relation_kind rel ATTRIBUTE_UNUSED) const
3055 if (empty_range_varying (r, type, lh, rh))
3056 return true;
3058 r = lh;
3059 r.union_ (rh);
3060 return true;
3063 bool
3064 operator_logical_or::op1_range (irange &r, tree type,
3065 const irange &lhs,
3066 const irange &op2 ATTRIBUTE_UNUSED,
3067 relation_kind rel ATTRIBUTE_UNUSED) const
3069 switch (get_bool_state (r, lhs, type))
3071 case BRS_FALSE:
3072 // A false result means both sides of the OR must be false.
3073 r = range_false (type);
3074 break;
3075 default:
3076 // Any other result means only one side has to be true, the
3077 // other side can be anything. so we can't be sure of any result
3078 // here.
3079 r = range_true_and_false (type);
3080 break;
3082 return true;
3085 bool
3086 operator_logical_or::op2_range (irange &r, tree type,
3087 const irange &lhs,
3088 const irange &op1,
3089 relation_kind rel ATTRIBUTE_UNUSED) const
3091 return operator_logical_or::op1_range (r, type, lhs, op1);
3095 class operator_bitwise_or : public range_operator
3097 using range_operator::op1_range;
3098 using range_operator::op2_range;
3099 public:
3100 virtual bool op1_range (irange &r, tree type,
3101 const irange &lhs,
3102 const irange &op2,
3103 relation_kind rel = VREL_VARYING) const;
3104 virtual bool op2_range (irange &r, tree type,
3105 const irange &lhs,
3106 const irange &op1,
3107 relation_kind rel= VREL_VARYING) const;
3108 virtual void wi_fold (irange &r, tree type,
3109 const wide_int &lh_lb,
3110 const wide_int &lh_ub,
3111 const wide_int &rh_lb,
3112 const wide_int &rh_ub) const;
3113 } op_bitwise_or;
3115 void
3116 operator_bitwise_or::wi_fold (irange &r, tree type,
3117 const wide_int &lh_lb,
3118 const wide_int &lh_ub,
3119 const wide_int &rh_lb,
3120 const wide_int &rh_ub) const
3122 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3123 return;
3125 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3126 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3127 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3128 maybe_nonzero_lh, mustbe_nonzero_lh);
3129 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3130 maybe_nonzero_rh, mustbe_nonzero_rh);
3131 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3132 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3133 signop sign = TYPE_SIGN (type);
3134 // If the input ranges contain only positive values we can
3135 // truncate the minimum of the result range to the maximum
3136 // of the input range minima.
3137 if (wi::ge_p (lh_lb, 0, sign)
3138 && wi::ge_p (rh_lb, 0, sign))
3140 new_lb = wi::max (new_lb, lh_lb, sign);
3141 new_lb = wi::max (new_lb, rh_lb, sign);
3143 // If either input range contains only negative values
3144 // we can truncate the minimum of the result range to the
3145 // respective minimum range.
3146 if (wi::lt_p (lh_ub, 0, sign))
3147 new_lb = wi::max (new_lb, lh_lb, sign);
3148 if (wi::lt_p (rh_ub, 0, sign))
3149 new_lb = wi::max (new_lb, rh_lb, sign);
3150 // If the limits got swapped around, return a conservative range.
3151 if (wi::gt_p (new_lb, new_ub, sign))
3153 // Make sure that nonzero|X is nonzero.
3154 if (wi::gt_p (lh_lb, 0, sign)
3155 || wi::gt_p (rh_lb, 0, sign)
3156 || wi::lt_p (lh_ub, 0, sign)
3157 || wi::lt_p (rh_ub, 0, sign))
3158 r.set_nonzero (type);
3159 else if (sign == SIGNED
3160 && wi_optimize_signed_bitwise_op (r, type,
3161 lh_lb, lh_ub,
3162 rh_lb, rh_ub))
3163 return;
3164 else
3165 r.set_varying (type);
3166 return;
3168 value_range_with_overflow (r, type, new_lb, new_ub);
3171 bool
3172 operator_bitwise_or::op1_range (irange &r, tree type,
3173 const irange &lhs,
3174 const irange &op2,
3175 relation_kind rel ATTRIBUTE_UNUSED) const
3177 // If this is really a logical wi_fold, call that.
3178 if (types_compatible_p (type, boolean_type_node))
3179 return op_logical_or.op1_range (r, type, lhs, op2);
3181 if (lhs.zero_p ())
3183 tree zero = build_zero_cst (type);
3184 r = int_range<1> (zero, zero);
3185 return true;
3187 r.set_varying (type);
3188 return true;
3191 bool
3192 operator_bitwise_or::op2_range (irange &r, tree type,
3193 const irange &lhs,
3194 const irange &op1,
3195 relation_kind rel ATTRIBUTE_UNUSED) const
3197 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3201 class operator_bitwise_xor : public range_operator
3203 using range_operator::op1_range;
3204 using range_operator::op2_range;
3205 public:
3206 virtual void wi_fold (irange &r, tree type,
3207 const wide_int &lh_lb,
3208 const wide_int &lh_ub,
3209 const wide_int &rh_lb,
3210 const wide_int &rh_ub) const;
3211 virtual bool op1_range (irange &r, tree type,
3212 const irange &lhs,
3213 const irange &op2,
3214 relation_kind rel = VREL_VARYING) const;
3215 virtual bool op2_range (irange &r, tree type,
3216 const irange &lhs,
3217 const irange &op1,
3218 relation_kind rel = VREL_VARYING) const;
3219 virtual bool op1_op2_relation_effect (irange &lhs_range,
3220 tree type,
3221 const irange &op1_range,
3222 const irange &op2_range,
3223 relation_kind rel) const;
3224 } op_bitwise_xor;
3226 void
3227 operator_bitwise_xor::wi_fold (irange &r, tree type,
3228 const wide_int &lh_lb,
3229 const wide_int &lh_ub,
3230 const wide_int &rh_lb,
3231 const wide_int &rh_ub) const
3233 signop sign = TYPE_SIGN (type);
3234 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3235 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3236 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3237 maybe_nonzero_lh, mustbe_nonzero_lh);
3238 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3239 maybe_nonzero_rh, mustbe_nonzero_rh);
3241 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3242 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3243 wide_int result_one_bits
3244 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3245 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3246 wide_int new_ub = ~result_zero_bits;
3247 wide_int new_lb = result_one_bits;
3249 // If the range has all positive or all negative values, the result
3250 // is better than VARYING.
3251 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3252 value_range_with_overflow (r, type, new_lb, new_ub);
3253 else if (sign == SIGNED
3254 && wi_optimize_signed_bitwise_op (r, type,
3255 lh_lb, lh_ub,
3256 rh_lb, rh_ub))
3257 ; /* Do nothing. */
3258 else
3259 r.set_varying (type);
3261 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3262 if (wi::lt_p (lh_ub, rh_lb, sign)
3263 || wi::lt_p (rh_ub, lh_lb, sign)
3264 || wi::ne_p (result_one_bits, 0))
3266 int_range<2> tmp;
3267 tmp.set_nonzero (type);
3268 r.intersect (tmp);
3272 bool
3273 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3274 tree type,
3275 const irange &,
3276 const irange &,
3277 relation_kind rel) const
3279 if (rel == VREL_VARYING)
3280 return false;
3282 int_range<2> rel_range;
3284 switch (rel)
3286 case VREL_EQ:
3287 rel_range.set_zero (type);
3288 break;
3289 case VREL_NE:
3290 rel_range.set_nonzero (type);
3291 break;
3292 default:
3293 return false;
3296 lhs_range.intersect (rel_range);
3297 return true;
3300 bool
3301 operator_bitwise_xor::op1_range (irange &r, tree type,
3302 const irange &lhs,
3303 const irange &op2,
3304 relation_kind rel ATTRIBUTE_UNUSED) const
3306 if (lhs.undefined_p () || lhs.varying_p ())
3308 r = lhs;
3309 return true;
3311 if (types_compatible_p (type, boolean_type_node))
3313 switch (get_bool_state (r, lhs, type))
3315 case BRS_TRUE:
3316 if (op2.varying_p ())
3317 r.set_varying (type);
3318 else if (op2.zero_p ())
3319 r = range_true (type);
3320 else
3321 r = range_false (type);
3322 break;
3323 case BRS_FALSE:
3324 r = op2;
3325 break;
3326 default:
3327 break;
3329 return true;
3331 r.set_varying (type);
3332 return true;
3335 bool
3336 operator_bitwise_xor::op2_range (irange &r, tree type,
3337 const irange &lhs,
3338 const irange &op1,
3339 relation_kind rel ATTRIBUTE_UNUSED) const
3341 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3344 class operator_trunc_mod : public range_operator
3346 using range_operator::op1_range;
3347 using range_operator::op2_range;
3348 public:
3349 virtual void wi_fold (irange &r, tree type,
3350 const wide_int &lh_lb,
3351 const wide_int &lh_ub,
3352 const wide_int &rh_lb,
3353 const wide_int &rh_ub) const;
3354 virtual bool op1_range (irange &r, tree type,
3355 const irange &lhs,
3356 const irange &op2,
3357 relation_kind rel ATTRIBUTE_UNUSED) const;
3358 virtual bool op2_range (irange &r, tree type,
3359 const irange &lhs,
3360 const irange &op1,
3361 relation_kind rel ATTRIBUTE_UNUSED) const;
3362 } op_trunc_mod;
3364 void
3365 operator_trunc_mod::wi_fold (irange &r, tree type,
3366 const wide_int &lh_lb,
3367 const wide_int &lh_ub,
3368 const wide_int &rh_lb,
3369 const wide_int &rh_ub) const
3371 wide_int new_lb, new_ub, tmp;
3372 signop sign = TYPE_SIGN (type);
3373 unsigned prec = TYPE_PRECISION (type);
3375 // Mod 0 is undefined.
3376 if (wi_zero_p (type, rh_lb, rh_ub))
3378 r.set_undefined ();
3379 return;
3382 // Check for constant and try to fold.
3383 if (lh_lb == lh_ub && rh_lb == rh_ub)
3385 wi::overflow_type ov = wi::OVF_NONE;
3386 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3387 if (ov == wi::OVF_NONE)
3389 r = int_range<2> (type, tmp, tmp);
3390 return;
3394 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3395 new_ub = rh_ub - 1;
3396 if (sign == SIGNED)
3398 tmp = -1 - rh_lb;
3399 new_ub = wi::smax (new_ub, tmp);
3402 if (sign == UNSIGNED)
3403 new_lb = wi::zero (prec);
3404 else
3406 new_lb = -new_ub;
3407 tmp = lh_lb;
3408 if (wi::gts_p (tmp, 0))
3409 tmp = wi::zero (prec);
3410 new_lb = wi::smax (new_lb, tmp);
3412 tmp = lh_ub;
3413 if (sign == SIGNED && wi::neg_p (tmp))
3414 tmp = wi::zero (prec);
3415 new_ub = wi::min (new_ub, tmp, sign);
3417 value_range_with_overflow (r, type, new_lb, new_ub);
3420 bool
3421 operator_trunc_mod::op1_range (irange &r, tree type,
3422 const irange &lhs,
3423 const irange &,
3424 relation_kind rel ATTRIBUTE_UNUSED) const
3426 // PR 91029.
3427 signop sign = TYPE_SIGN (type);
3428 unsigned prec = TYPE_PRECISION (type);
3429 // (a % b) >= x && x > 0 , then a >= x.
3430 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3432 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3433 return true;
3435 // (a % b) <= x && x < 0 , then a <= x.
3436 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3438 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3439 return true;
3441 return false;
3444 bool
3445 operator_trunc_mod::op2_range (irange &r, tree type,
3446 const irange &lhs,
3447 const irange &,
3448 relation_kind rel ATTRIBUTE_UNUSED) const
3450 // PR 91029.
3451 signop sign = TYPE_SIGN (type);
3452 unsigned prec = TYPE_PRECISION (type);
3453 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3454 // or b > x for unsigned.
3455 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3457 if (sign == SIGNED)
3458 r = value_range (type, wi::neg (lhs.lower_bound ()),
3459 lhs.lower_bound (), VR_ANTI_RANGE);
3460 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3461 sign))
3462 r = value_range (type, lhs.lower_bound () + 1,
3463 wi::max_value (prec, sign));
3464 else
3465 return false;
3466 return true;
3468 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3469 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3471 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3472 r = value_range (type, lhs.upper_bound (),
3473 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3474 else
3475 return false;
3476 return true;
3478 return false;
3482 class operator_logical_not : public range_operator
3484 using range_operator::fold_range;
3485 using range_operator::op1_range;
3486 public:
3487 virtual bool fold_range (irange &r, tree type,
3488 const irange &lh,
3489 const irange &rh,
3490 relation_kind rel = VREL_VARYING) const;
3491 virtual bool op1_range (irange &r, tree type,
3492 const irange &lhs,
3493 const irange &op2,
3494 relation_kind rel = VREL_VARYING) const;
3495 } op_logical_not;
3497 // Folding a logical NOT, oddly enough, involves doing nothing on the
3498 // forward pass through. During the initial walk backwards, the
3499 // logical NOT reversed the desired outcome on the way back, so on the
3500 // way forward all we do is pass the range forward.
3502 // b_2 = x_1 < 20
3503 // b_3 = !b_2
3504 // if (b_3)
3505 // to determine the TRUE branch, walking backward
3506 // if (b_3) if ([1,1])
3507 // b_3 = !b_2 [1,1] = ![0,0]
3508 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3509 // which is the result we are looking for.. so.. pass it through.
3511 bool
3512 operator_logical_not::fold_range (irange &r, tree type,
3513 const irange &lh,
3514 const irange &rh ATTRIBUTE_UNUSED,
3515 relation_kind rel ATTRIBUTE_UNUSED) const
3517 if (empty_range_varying (r, type, lh, rh))
3518 return true;
3520 r = lh;
3521 if (!lh.varying_p () && !lh.undefined_p ())
3522 r.invert ();
3524 return true;
3527 bool
3528 operator_logical_not::op1_range (irange &r,
3529 tree type,
3530 const irange &lhs,
3531 const irange &op2,
3532 relation_kind rel ATTRIBUTE_UNUSED) const
3534 // Logical NOT is involutary...do it again.
3535 return fold_range (r, type, lhs, op2);
3539 class operator_bitwise_not : public range_operator
3541 using range_operator::fold_range;
3542 using range_operator::op1_range;
3543 public:
3544 virtual bool fold_range (irange &r, tree type,
3545 const irange &lh,
3546 const irange &rh,
3547 relation_kind rel = VREL_VARYING) const;
3548 virtual bool op1_range (irange &r, tree type,
3549 const irange &lhs,
3550 const irange &op2,
3551 relation_kind rel = VREL_VARYING) const;
3552 } op_bitwise_not;
3554 bool
3555 operator_bitwise_not::fold_range (irange &r, tree type,
3556 const irange &lh,
3557 const irange &rh,
3558 relation_kind rel ATTRIBUTE_UNUSED) const
3560 if (empty_range_varying (r, type, lh, rh))
3561 return true;
3563 if (types_compatible_p (type, boolean_type_node))
3564 return op_logical_not.fold_range (r, type, lh, rh);
3566 // ~X is simply -1 - X.
3567 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3568 wi::minus_one (TYPE_PRECISION (type)));
3569 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, minusone, lh);
3572 bool
3573 operator_bitwise_not::op1_range (irange &r, tree type,
3574 const irange &lhs,
3575 const irange &op2,
3576 relation_kind rel ATTRIBUTE_UNUSED) const
3578 if (types_compatible_p (type, boolean_type_node))
3579 return op_logical_not.op1_range (r, type, lhs, op2);
3581 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3582 return fold_range (r, type, lhs, op2);
3586 class operator_cst : public range_operator
3588 using range_operator::fold_range;
3589 public:
3590 virtual bool fold_range (irange &r, tree type,
3591 const irange &op1,
3592 const irange &op2,
3593 relation_kind rel = VREL_VARYING) const;
3594 } op_integer_cst;
3596 bool
3597 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3598 const irange &lh,
3599 const irange &rh ATTRIBUTE_UNUSED,
3600 relation_kind rel ATTRIBUTE_UNUSED) const
3602 r = lh;
3603 return true;
3607 class operator_identity : public range_operator
3609 using range_operator::fold_range;
3610 using range_operator::op1_range;
3611 using range_operator::lhs_op1_relation;
3612 public:
3613 virtual bool fold_range (irange &r, tree type,
3614 const irange &op1,
3615 const irange &op2,
3616 relation_kind rel = VREL_VARYING) const;
3617 virtual bool op1_range (irange &r, tree type,
3618 const irange &lhs,
3619 const irange &op2,
3620 relation_kind rel = VREL_VARYING) const;
3621 virtual relation_kind lhs_op1_relation (const irange &lhs,
3622 const irange &op1,
3623 const irange &op2,
3624 relation_kind rel) const;
3625 } op_identity;
3627 // Determine if there is a relationship between LHS and OP1.
3629 relation_kind
3630 operator_identity::lhs_op1_relation (const irange &lhs,
3631 const irange &op1 ATTRIBUTE_UNUSED,
3632 const irange &op2 ATTRIBUTE_UNUSED,
3633 relation_kind) const
3635 if (lhs.undefined_p ())
3636 return VREL_VARYING;
3637 // Simply a copy, so they are equivalent.
3638 return VREL_EQ;
3641 bool
3642 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3643 const irange &lh,
3644 const irange &rh ATTRIBUTE_UNUSED,
3645 relation_kind rel ATTRIBUTE_UNUSED) const
3647 r = lh;
3648 return true;
3651 bool
3652 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3653 const irange &lhs,
3654 const irange &op2 ATTRIBUTE_UNUSED,
3655 relation_kind rel ATTRIBUTE_UNUSED) const
3657 r = lhs;
3658 return true;
3662 class operator_unknown : public range_operator
3664 using range_operator::fold_range;
3665 public:
3666 virtual bool fold_range (irange &r, tree type,
3667 const irange &op1,
3668 const irange &op2,
3669 relation_kind rel = VREL_VARYING) const;
3670 } op_unknown;
3672 bool
3673 operator_unknown::fold_range (irange &r, tree type,
3674 const irange &lh ATTRIBUTE_UNUSED,
3675 const irange &rh ATTRIBUTE_UNUSED,
3676 relation_kind rel ATTRIBUTE_UNUSED) const
3678 r.set_varying (type);
3679 return true;
3683 class operator_abs : public range_operator
3685 using range_operator::op1_range;
3686 public:
3687 virtual void wi_fold (irange &r, tree type,
3688 const wide_int &lh_lb,
3689 const wide_int &lh_ub,
3690 const wide_int &rh_lb,
3691 const wide_int &rh_ub) const;
3692 virtual bool op1_range (irange &r, tree type,
3693 const irange &lhs,
3694 const irange &op2,
3695 relation_kind rel ATTRIBUTE_UNUSED) const;
3696 } op_abs;
3698 void
3699 operator_abs::wi_fold (irange &r, tree type,
3700 const wide_int &lh_lb, const wide_int &lh_ub,
3701 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3702 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3704 wide_int min, max;
3705 signop sign = TYPE_SIGN (type);
3706 unsigned prec = TYPE_PRECISION (type);
3708 // Pass through LH for the easy cases.
3709 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3711 r = int_range<1> (type, lh_lb, lh_ub);
3712 return;
3715 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3716 // a useful range.
3717 wide_int min_value = wi::min_value (prec, sign);
3718 wide_int max_value = wi::max_value (prec, sign);
3719 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3721 r.set_varying (type);
3722 return;
3725 // ABS_EXPR may flip the range around, if the original range
3726 // included negative values.
3727 if (wi::eq_p (lh_lb, min_value))
3729 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3730 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3731 if (wi::eq_p (lh_ub, min_value))
3733 r = int_range<1> (type, min_value, min_value);
3734 return;
3736 min = max_value;
3738 else
3739 min = wi::abs (lh_lb);
3741 if (wi::eq_p (lh_ub, min_value))
3742 max = max_value;
3743 else
3744 max = wi::abs (lh_ub);
3746 // If the range contains zero then we know that the minimum value in the
3747 // range will be zero.
3748 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3750 if (wi::gt_p (min, max, sign))
3751 max = min;
3752 min = wi::zero (prec);
3754 else
3756 // If the range was reversed, swap MIN and MAX.
3757 if (wi::gt_p (min, max, sign))
3758 std::swap (min, max);
3761 // If the new range has its limits swapped around (MIN > MAX), then
3762 // the operation caused one of them to wrap around. The only thing
3763 // we know is that the result is positive.
3764 if (wi::gt_p (min, max, sign))
3766 min = wi::zero (prec);
3767 max = max_value;
3769 r = int_range<1> (type, min, max);
3772 bool
3773 operator_abs::op1_range (irange &r, tree type,
3774 const irange &lhs,
3775 const irange &op2,
3776 relation_kind rel ATTRIBUTE_UNUSED) const
3778 if (empty_range_varying (r, type, lhs, op2))
3779 return true;
3780 if (TYPE_UNSIGNED (type))
3782 r = lhs;
3783 return true;
3785 // Start with the positives because negatives are an impossible result.
3786 int_range_max positives = range_positives (type);
3787 positives.intersect (lhs);
3788 r = positives;
3789 // Then add the negative of each pair:
3790 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3791 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3792 r.union_ (int_range<1> (type,
3793 -positives.upper_bound (i),
3794 -positives.lower_bound (i)));
3795 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3796 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3797 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3798 wide_int lb = lhs.lower_bound ();
3799 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3800 r.union_ (int_range<2> (type, lb, lb));
3801 return true;
3805 class operator_absu : public range_operator
3807 public:
3808 virtual void wi_fold (irange &r, tree type,
3809 const wide_int &lh_lb, const wide_int &lh_ub,
3810 const wide_int &rh_lb, const wide_int &rh_ub) const;
3811 } op_absu;
3813 void
3814 operator_absu::wi_fold (irange &r, tree type,
3815 const wide_int &lh_lb, const wide_int &lh_ub,
3816 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3817 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3819 wide_int new_lb, new_ub;
3821 // Pass through VR0 the easy cases.
3822 if (wi::ges_p (lh_lb, 0))
3824 new_lb = lh_lb;
3825 new_ub = lh_ub;
3827 else
3829 new_lb = wi::abs (lh_lb);
3830 new_ub = wi::abs (lh_ub);
3832 // If the range contains zero then we know that the minimum
3833 // value in the range will be zero.
3834 if (wi::ges_p (lh_ub, 0))
3836 if (wi::gtu_p (new_lb, new_ub))
3837 new_ub = new_lb;
3838 new_lb = wi::zero (TYPE_PRECISION (type));
3840 else
3841 std::swap (new_lb, new_ub);
3844 gcc_checking_assert (TYPE_UNSIGNED (type));
3845 r = int_range<1> (type, new_lb, new_ub);
3849 class operator_negate : public range_operator
3851 using range_operator::fold_range;
3852 using range_operator::op1_range;
3853 public:
3854 virtual bool fold_range (irange &r, tree type,
3855 const irange &op1,
3856 const irange &op2,
3857 relation_kind rel = VREL_VARYING) const;
3858 virtual bool op1_range (irange &r, tree type,
3859 const irange &lhs,
3860 const irange &op2,
3861 relation_kind rel = VREL_VARYING) const;
3862 } op_negate;
3864 bool
3865 operator_negate::fold_range (irange &r, tree type,
3866 const irange &lh,
3867 const irange &rh,
3868 relation_kind rel ATTRIBUTE_UNUSED) const
3870 if (empty_range_varying (r, type, lh, rh))
3871 return true;
3872 // -X is simply 0 - X.
3873 return range_op_handler (MINUS_EXPR, type).fold_range (r, type,
3874 range_zero (type), lh);
3877 bool
3878 operator_negate::op1_range (irange &r, tree type,
3879 const irange &lhs,
3880 const irange &op2,
3881 relation_kind rel ATTRIBUTE_UNUSED) const
3883 // NEGATE is involutory.
3884 return fold_range (r, type, lhs, op2);
3888 class operator_addr_expr : public range_operator
3890 using range_operator::fold_range;
3891 using range_operator::op1_range;
3892 public:
3893 virtual bool fold_range (irange &r, tree type,
3894 const irange &op1,
3895 const irange &op2,
3896 relation_kind rel = VREL_VARYING) const;
3897 virtual bool op1_range (irange &r, tree type,
3898 const irange &lhs,
3899 const irange &op2,
3900 relation_kind rel = VREL_VARYING) const;
3901 } op_addr;
3903 bool
3904 operator_addr_expr::fold_range (irange &r, tree type,
3905 const irange &lh,
3906 const irange &rh,
3907 relation_kind rel ATTRIBUTE_UNUSED) const
3909 if (empty_range_varying (r, type, lh, rh))
3910 return true;
3912 // Return a non-null pointer of the LHS type (passed in op2).
3913 if (lh.zero_p ())
3914 r = range_zero (type);
3915 else if (!lh.contains_p (build_zero_cst (lh.type ())))
3916 r = range_nonzero (type);
3917 else
3918 r.set_varying (type);
3919 return true;
3922 bool
3923 operator_addr_expr::op1_range (irange &r, tree type,
3924 const irange &lhs,
3925 const irange &op2,
3926 relation_kind rel ATTRIBUTE_UNUSED) const
3928 return operator_addr_expr::fold_range (r, type, lhs, op2);
3932 class pointer_plus_operator : public range_operator
3934 public:
3935 virtual void wi_fold (irange &r, tree type,
3936 const wide_int &lh_lb,
3937 const wide_int &lh_ub,
3938 const wide_int &rh_lb,
3939 const wide_int &rh_ub) const;
3940 } op_pointer_plus;
3942 void
3943 pointer_plus_operator::wi_fold (irange &r, tree type,
3944 const wide_int &lh_lb,
3945 const wide_int &lh_ub,
3946 const wide_int &rh_lb,
3947 const wide_int &rh_ub) const
3949 // Check for [0,0] + const, and simply return the const.
3950 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3952 tree val = wide_int_to_tree (type, rh_lb);
3953 r.set (val, val);
3954 return;
3957 // For pointer types, we are really only interested in asserting
3958 // whether the expression evaluates to non-NULL.
3960 // With -fno-delete-null-pointer-checks we need to be more
3961 // conservative. As some object might reside at address 0,
3962 // then some offset could be added to it and the same offset
3963 // subtracted again and the result would be NULL.
3964 // E.g.
3965 // static int a[12]; where &a[0] is NULL and
3966 // ptr = &a[6];
3967 // ptr -= 6;
3968 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3969 // where the first range doesn't include zero and the second one
3970 // doesn't either. As the second operand is sizetype (unsigned),
3971 // consider all ranges where the MSB could be set as possible
3972 // subtractions where the result might be NULL.
3973 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3974 || !wi_includes_zero_p (type, rh_lb, rh_ub))
3975 && !TYPE_OVERFLOW_WRAPS (type)
3976 && (flag_delete_null_pointer_checks
3977 || !wi::sign_mask (rh_ub)))
3978 r = range_nonzero (type);
3979 else if (lh_lb == lh_ub && lh_lb == 0
3980 && rh_lb == rh_ub && rh_lb == 0)
3981 r = range_zero (type);
3982 else
3983 r.set_varying (type);
3987 class pointer_min_max_operator : public range_operator
3989 public:
3990 virtual void wi_fold (irange & r, tree type,
3991 const wide_int &lh_lb, const wide_int &lh_ub,
3992 const wide_int &rh_lb, const wide_int &rh_ub) const;
3993 } op_ptr_min_max;
3995 void
3996 pointer_min_max_operator::wi_fold (irange &r, tree type,
3997 const wide_int &lh_lb,
3998 const wide_int &lh_ub,
3999 const wide_int &rh_lb,
4000 const wide_int &rh_ub) const
4002 // For MIN/MAX expressions with pointers, we only care about
4003 // nullness. If both are non null, then the result is nonnull.
4004 // If both are null, then the result is null. Otherwise they
4005 // are varying.
4006 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4007 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4008 r = range_nonzero (type);
4009 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4010 r = range_zero (type);
4011 else
4012 r.set_varying (type);
4016 class pointer_and_operator : public range_operator
4018 public:
4019 virtual void wi_fold (irange &r, tree type,
4020 const wide_int &lh_lb, const wide_int &lh_ub,
4021 const wide_int &rh_lb, const wide_int &rh_ub) const;
4022 } op_pointer_and;
4024 void
4025 pointer_and_operator::wi_fold (irange &r, tree type,
4026 const wide_int &lh_lb,
4027 const wide_int &lh_ub,
4028 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4029 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4031 // For pointer types, we are really only interested in asserting
4032 // whether the expression evaluates to non-NULL.
4033 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
4034 r = range_zero (type);
4035 else
4036 r.set_varying (type);
4040 class pointer_or_operator : public range_operator
4042 using range_operator::op1_range;
4043 using range_operator::op2_range;
4044 public:
4045 virtual bool op1_range (irange &r, tree type,
4046 const irange &lhs,
4047 const irange &op2,
4048 relation_kind rel = VREL_VARYING) const;
4049 virtual bool op2_range (irange &r, tree type,
4050 const irange &lhs,
4051 const irange &op1,
4052 relation_kind rel = VREL_VARYING) const;
4053 virtual void wi_fold (irange &r, tree type,
4054 const wide_int &lh_lb, const wide_int &lh_ub,
4055 const wide_int &rh_lb, const wide_int &rh_ub) const;
4056 } op_pointer_or;
4058 bool
4059 pointer_or_operator::op1_range (irange &r, tree type,
4060 const irange &lhs,
4061 const irange &op2 ATTRIBUTE_UNUSED,
4062 relation_kind rel ATTRIBUTE_UNUSED) const
4064 if (lhs.zero_p ())
4066 tree zero = build_zero_cst (type);
4067 r = int_range<1> (zero, zero);
4068 return true;
4070 r.set_varying (type);
4071 return true;
4074 bool
4075 pointer_or_operator::op2_range (irange &r, tree type,
4076 const irange &lhs,
4077 const irange &op1,
4078 relation_kind rel ATTRIBUTE_UNUSED) const
4080 return pointer_or_operator::op1_range (r, type, lhs, op1);
4083 void
4084 pointer_or_operator::wi_fold (irange &r, tree type,
4085 const wide_int &lh_lb,
4086 const wide_int &lh_ub,
4087 const wide_int &rh_lb,
4088 const wide_int &rh_ub) const
4090 // For pointer types, we are really only interested in asserting
4091 // whether the expression evaluates to non-NULL.
4092 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4093 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4094 r = range_nonzero (type);
4095 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4096 r = range_zero (type);
4097 else
4098 r.set_varying (type);
4101 // Return a pointer to the range_operator instance, if there is one
4102 // associated with tree_code CODE.
4104 range_operator *
4105 range_op_table::operator[] (enum tree_code code)
4107 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4108 return m_range_tree[code];
4111 // Add OP to the handler table for CODE.
4113 void
4114 range_op_table::set (enum tree_code code, range_operator &op)
4116 gcc_checking_assert (m_range_tree[code] == NULL);
4117 m_range_tree[code] = &op;
4120 // Instantiate a range op table for integral operations.
4122 class integral_table : public range_op_table
4124 public:
4125 integral_table ();
4126 } integral_tree_table;
4128 integral_table::integral_table ()
4130 set (EQ_EXPR, op_equal);
4131 set (NE_EXPR, op_not_equal);
4132 set (LT_EXPR, op_lt);
4133 set (LE_EXPR, op_le);
4134 set (GT_EXPR, op_gt);
4135 set (GE_EXPR, op_ge);
4136 set (PLUS_EXPR, op_plus);
4137 set (MINUS_EXPR, op_minus);
4138 set (MIN_EXPR, op_min);
4139 set (MAX_EXPR, op_max);
4140 set (MULT_EXPR, op_mult);
4141 set (TRUNC_DIV_EXPR, op_trunc_div);
4142 set (FLOOR_DIV_EXPR, op_floor_div);
4143 set (ROUND_DIV_EXPR, op_round_div);
4144 set (CEIL_DIV_EXPR, op_ceil_div);
4145 set (EXACT_DIV_EXPR, op_exact_div);
4146 set (LSHIFT_EXPR, op_lshift);
4147 set (RSHIFT_EXPR, op_rshift);
4148 set (NOP_EXPR, op_convert);
4149 set (CONVERT_EXPR, op_convert);
4150 set (TRUTH_AND_EXPR, op_logical_and);
4151 set (BIT_AND_EXPR, op_bitwise_and);
4152 set (TRUTH_OR_EXPR, op_logical_or);
4153 set (BIT_IOR_EXPR, op_bitwise_or);
4154 set (BIT_XOR_EXPR, op_bitwise_xor);
4155 set (TRUNC_MOD_EXPR, op_trunc_mod);
4156 set (TRUTH_NOT_EXPR, op_logical_not);
4157 set (BIT_NOT_EXPR, op_bitwise_not);
4158 set (INTEGER_CST, op_integer_cst);
4159 set (SSA_NAME, op_identity);
4160 set (PAREN_EXPR, op_identity);
4161 set (OBJ_TYPE_REF, op_identity);
4162 set (IMAGPART_EXPR, op_unknown);
4163 set (REALPART_EXPR, op_unknown);
4164 set (POINTER_DIFF_EXPR, op_pointer_diff);
4165 set (ABS_EXPR, op_abs);
4166 set (ABSU_EXPR, op_absu);
4167 set (NEGATE_EXPR, op_negate);
4168 set (ADDR_EXPR, op_addr);
4171 // Instantiate a range op table for pointer operations.
4173 class pointer_table : public range_op_table
4175 public:
4176 pointer_table ();
4177 } pointer_tree_table;
4179 pointer_table::pointer_table ()
4181 set (BIT_AND_EXPR, op_pointer_and);
4182 set (BIT_IOR_EXPR, op_pointer_or);
4183 set (MIN_EXPR, op_ptr_min_max);
4184 set (MAX_EXPR, op_ptr_min_max);
4185 set (POINTER_PLUS_EXPR, op_pointer_plus);
4187 set (EQ_EXPR, op_equal);
4188 set (NE_EXPR, op_not_equal);
4189 set (LT_EXPR, op_lt);
4190 set (LE_EXPR, op_le);
4191 set (GT_EXPR, op_gt);
4192 set (GE_EXPR, op_ge);
4193 set (SSA_NAME, op_identity);
4194 set (INTEGER_CST, op_integer_cst);
4195 set (ADDR_EXPR, op_addr);
4196 set (NOP_EXPR, op_convert);
4197 set (CONVERT_EXPR, op_convert);
4199 set (BIT_NOT_EXPR, op_bitwise_not);
4200 set (BIT_XOR_EXPR, op_bitwise_xor);
4203 // The tables are hidden and accessed via a simple extern function.
4205 static inline range_operator *
4206 get_handler (enum tree_code code, tree type)
4208 // First check if there is a pointer specialization.
4209 if (POINTER_TYPE_P (type))
4210 return pointer_tree_table[code];
4211 if (INTEGRAL_TYPE_P (type))
4212 return integral_tree_table[code];
4213 return NULL;
4216 range_op_handler::range_op_handler (tree_code code, tree type)
4218 m_op = get_handler (code, type);
4221 range_op_handler::range_op_handler (const gimple *s)
4223 if (const gassign *ass = dyn_cast<const gassign *> (s))
4225 enum tree_code code = gimple_assign_rhs_code (ass);
4226 // The LHS of a comparison is always an int, so we must look at
4227 // the operands.
4228 if (TREE_CODE_CLASS (code) == tcc_comparison)
4229 m_op = get_handler (code, TREE_TYPE (gimple_assign_rhs1 (ass)));
4230 else
4231 m_op = get_handler (code, TREE_TYPE (gimple_assign_lhs (ass)));
4233 else if (const gcond *cond = dyn_cast<const gcond *> (s))
4234 m_op = get_handler (gimple_cond_code (cond),
4235 TREE_TYPE (gimple_cond_lhs (cond)));
4236 else
4237 m_op = NULL;
4240 bool
4241 range_op_handler::fold_range (vrange &r, tree type,
4242 const vrange &lh,
4243 const vrange &rh,
4244 relation_kind rel) const
4246 if (is_a <irange> (lh))
4247 return m_op->fold_range (as_a <irange> (r), type,
4248 as_a <irange> (lh),
4249 as_a <irange> (rh), rel);
4250 gcc_unreachable ();
4251 return false;
4254 bool
4255 range_op_handler::op1_range (vrange &r, tree type,
4256 const vrange &lhs,
4257 const vrange &op2,
4258 relation_kind rel) const
4260 if (is_a <irange> (r))
4261 return m_op->op1_range (as_a <irange> (r), type,
4262 as_a <irange> (lhs),
4263 as_a <irange> (op2), rel);
4264 gcc_unreachable ();
4265 return false;
4268 bool
4269 range_op_handler::op2_range (vrange &r, tree type,
4270 const vrange &lhs,
4271 const vrange &op1,
4272 relation_kind rel) const
4274 if (is_a <irange> (r))
4275 return m_op->op2_range (as_a <irange> (r), type,
4276 as_a <irange> (lhs),
4277 as_a <irange> (op1), rel);
4278 gcc_unreachable ();
4279 return false;
4282 relation_kind
4283 range_op_handler::lhs_op1_relation (const vrange &lhs,
4284 const vrange &op1,
4285 const vrange &op2,
4286 relation_kind rel) const
4288 if (is_a <irange> (op1))
4289 return m_op->lhs_op1_relation (as_a <irange> (lhs),
4290 as_a <irange> (op1), as_a <irange> (op2), rel);
4291 gcc_unreachable ();
4292 return VREL_VARYING;
4295 relation_kind
4296 range_op_handler::lhs_op2_relation (const vrange &lhs,
4297 const vrange &op1,
4298 const vrange &op2,
4299 relation_kind rel) const
4301 if (is_a <irange> (op1))
4302 return m_op->lhs_op2_relation (as_a <irange> (lhs),
4303 as_a <irange> (op1), as_a <irange> (op2), rel);
4304 gcc_unreachable ();
4305 return VREL_VARYING;
4308 relation_kind
4309 range_op_handler::op1_op2_relation (const vrange &lhs) const
4311 return m_op->op1_op2_relation (as_a <irange> (lhs));
4314 // Cast the range in R to TYPE.
4316 bool
4317 range_cast (vrange &r, tree type)
4319 Value_Range tmp (r);
4320 Value_Range varying (type);
4321 varying.set_varying (type);
4322 range_op_handler op (CONVERT_EXPR, type);
4323 // Call op_convert, if it fails, the result is varying.
4324 if (!op || !op.fold_range (r, type, tmp, varying))
4326 r.set_varying (type);
4327 return false;
4329 return true;
4332 #if CHECKING_P
4333 #include "selftest.h"
4335 namespace selftest
4337 #define INT(N) build_int_cst (integer_type_node, (N))
4338 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4339 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4340 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4341 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4342 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4344 static void
4345 range_op_cast_tests ()
4347 int_range<1> r0, r1, r2, rold;
4348 r0.set_varying (integer_type_node);
4349 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4351 // If a range is in any way outside of the range for the converted
4352 // to range, default to the range for the new type.
4353 r0.set_varying (short_integer_type_node);
4354 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4355 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4356 if (TYPE_PRECISION (TREE_TYPE (maxint))
4357 > TYPE_PRECISION (short_integer_type_node))
4359 r1 = int_range<1> (integer_zero_node, maxint);
4360 range_cast (r1, short_integer_type_node);
4361 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4362 && r1.upper_bound() == wi::to_wide (maxshort));
4365 // (unsigned char)[-5,-1] => [251,255].
4366 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4367 range_cast (r0, unsigned_char_type_node);
4368 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4369 range_cast (r0, signed_char_type_node);
4370 ASSERT_TRUE (r0 == rold);
4372 // (signed char)[15, 150] => [-128,-106][15,127].
4373 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4374 range_cast (r0, signed_char_type_node);
4375 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4376 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4377 r1.union_ (r2);
4378 ASSERT_TRUE (r1 == r0);
4379 range_cast (r0, unsigned_char_type_node);
4380 ASSERT_TRUE (r0 == rold);
4382 // (unsigned char)[-5, 5] => [0,5][251,255].
4383 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4384 range_cast (r0, unsigned_char_type_node);
4385 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4386 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4387 r1.union_ (r2);
4388 ASSERT_TRUE (r0 == r1);
4389 range_cast (r0, signed_char_type_node);
4390 ASSERT_TRUE (r0 == rold);
4392 // (unsigned char)[-5,5] => [0,5][251,255].
4393 r0 = int_range<1> (INT (-5), INT (5));
4394 range_cast (r0, unsigned_char_type_node);
4395 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4396 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4397 ASSERT_TRUE (r0 == r1);
4399 // (unsigned char)[5U,1974U] => [0,255].
4400 r0 = int_range<1> (UINT (5), UINT (1974));
4401 range_cast (r0, unsigned_char_type_node);
4402 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4403 range_cast (r0, integer_type_node);
4404 // Going to a wider range should not sign extend.
4405 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4407 // (unsigned char)[-350,15] => [0,255].
4408 r0 = int_range<1> (INT (-350), INT (15));
4409 range_cast (r0, unsigned_char_type_node);
4410 ASSERT_TRUE (r0 == (int_range<1>
4411 (TYPE_MIN_VALUE (unsigned_char_type_node),
4412 TYPE_MAX_VALUE (unsigned_char_type_node))));
4414 // Casting [-120,20] from signed char to unsigned short.
4415 // => [0, 20][0xff88, 0xffff].
4416 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4417 range_cast (r0, short_unsigned_type_node);
4418 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4419 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4420 r1.union_ (r2);
4421 ASSERT_TRUE (r0 == r1);
4422 // A truncating cast back to signed char will work because [-120, 20]
4423 // is representable in signed char.
4424 range_cast (r0, signed_char_type_node);
4425 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4427 // unsigned char -> signed short
4428 // (signed short)[(unsigned char)25, (unsigned char)250]
4429 // => [(signed short)25, (signed short)250]
4430 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4431 range_cast (r0, short_integer_type_node);
4432 r1 = int_range<1> (INT16 (25), INT16 (250));
4433 ASSERT_TRUE (r0 == r1);
4434 range_cast (r0, unsigned_char_type_node);
4435 ASSERT_TRUE (r0 == rold);
4437 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4438 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4439 TYPE_MAX_VALUE (long_long_integer_type_node));
4440 range_cast (r0, short_unsigned_type_node);
4441 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4442 TYPE_MAX_VALUE (short_unsigned_type_node));
4443 ASSERT_TRUE (r0 == r1);
4445 // Casting NONZERO to a narrower type will wrap/overflow so
4446 // it's just the entire range for the narrower type.
4448 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4449 // is outside of the range of a smaller range, return the full
4450 // smaller range.
4451 if (TYPE_PRECISION (integer_type_node)
4452 > TYPE_PRECISION (short_integer_type_node))
4454 r0 = range_nonzero (integer_type_node);
4455 range_cast (r0, short_integer_type_node);
4456 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4457 TYPE_MAX_VALUE (short_integer_type_node));
4458 ASSERT_TRUE (r0 == r1);
4461 // Casting NONZERO from a narrower signed to a wider signed.
4463 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4464 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4465 r0 = range_nonzero (short_integer_type_node);
4466 range_cast (r0, integer_type_node);
4467 r1 = int_range<1> (INT (-32768), INT (-1));
4468 r2 = int_range<1> (INT (1), INT (32767));
4469 r1.union_ (r2);
4470 ASSERT_TRUE (r0 == r1);
4473 static void
4474 range_op_lshift_tests ()
4476 // Test that 0x808.... & 0x8.... still contains 0x8....
4477 // for a large set of numbers.
4479 int_range_max res;
4480 tree big_type = long_long_unsigned_type_node;
4481 // big_num = 0x808,0000,0000,0000
4482 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4483 build_int_cst (big_type, 0x808),
4484 build_int_cst (big_type, 48));
4485 op_bitwise_and.fold_range (res, big_type,
4486 int_range <1> (big_type),
4487 int_range <1> (big_num, big_num));
4488 // val = 0x8,0000,0000,0000
4489 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4490 build_int_cst (big_type, 0x8),
4491 build_int_cst (big_type, 48));
4492 ASSERT_TRUE (res.contains_p (val));
4495 if (TYPE_PRECISION (unsigned_type_node) > 31)
4497 // unsigned VARYING = op1 << 1 should be VARYING.
4498 int_range<2> lhs (unsigned_type_node);
4499 int_range<2> shift (INT (1), INT (1));
4500 int_range_max op1;
4501 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4502 ASSERT_TRUE (op1.varying_p ());
4504 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4505 int_range<2> zero (UINT (0), UINT (0));
4506 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4507 ASSERT_TRUE (op1.num_pairs () == 2);
4508 // Remove the [0,0] range.
4509 op1.intersect (zero);
4510 ASSERT_TRUE (op1.num_pairs () == 1);
4511 // op1 << 1 should be [0x8000,0x8000] << 1,
4512 // which should result in [0,0].
4513 int_range_max result;
4514 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4515 ASSERT_TRUE (result == zero);
4517 // signed VARYING = op1 << 1 should be VARYING.
4518 if (TYPE_PRECISION (integer_type_node) > 31)
4520 // unsigned VARYING = op1 << 1 hould be VARYING.
4521 int_range<2> lhs (integer_type_node);
4522 int_range<2> shift (INT (1), INT (1));
4523 int_range_max op1;
4524 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4525 ASSERT_TRUE (op1.varying_p ());
4527 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4528 int_range<2> zero (INT (0), INT (0));
4529 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4530 ASSERT_TRUE (op1.num_pairs () == 2);
4531 // Remove the [0,0] range.
4532 op1.intersect (zero);
4533 ASSERT_TRUE (op1.num_pairs () == 1);
4534 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4535 // which should result in [0,0].
4536 int_range_max result;
4537 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4538 ASSERT_TRUE (result == zero);
4542 static void
4543 range_op_rshift_tests ()
4545 // unsigned: [3, MAX] = OP1 >> 1
4547 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4548 TYPE_MAX_VALUE (unsigned_type_node));
4549 int_range_max one (build_one_cst (unsigned_type_node),
4550 build_one_cst (unsigned_type_node));
4551 int_range_max op1;
4552 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4553 ASSERT_FALSE (op1.contains_p (UINT (3)));
4556 // signed: [3, MAX] = OP1 >> 1
4558 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4559 int_range_max one (INT (1), INT (1));
4560 int_range_max op1;
4561 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4562 ASSERT_FALSE (op1.contains_p (INT (-2)));
4565 // This is impossible, so OP1 should be [].
4566 // signed: [MIN, MIN] = OP1 >> 1
4568 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4569 TYPE_MIN_VALUE (integer_type_node));
4570 int_range_max one (INT (1), INT (1));
4571 int_range_max op1;
4572 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4573 ASSERT_TRUE (op1.undefined_p ());
4576 // signed: ~[-1] = OP1 >> 31
4577 if (TYPE_PRECISION (integer_type_node) > 31)
4579 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4580 int_range_max shift (INT (31), INT (31));
4581 int_range_max op1;
4582 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4583 int_range_max negatives = range_negatives (integer_type_node);
4584 negatives.intersect (op1);
4585 ASSERT_TRUE (negatives.undefined_p ());
4589 static void
4590 range_op_bitwise_and_tests ()
4592 int_range_max res;
4593 tree min = vrp_val_min (integer_type_node);
4594 tree max = vrp_val_max (integer_type_node);
4595 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4596 build_one_cst (integer_type_node));
4597 int_range_max i1 (tiny, max);
4598 int_range_max i2 (build_int_cst (integer_type_node, 255),
4599 build_int_cst (integer_type_node, 255));
4601 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4602 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4603 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4605 // VARYING = OP1 & 255: OP1 is VARYING
4606 i1 = int_range<1> (integer_type_node);
4607 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4608 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4610 // (NONZERO | X) is nonzero.
4611 i1.set_nonzero (integer_type_node);
4612 i2.set_varying (integer_type_node);
4613 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4614 ASSERT_TRUE (res.nonzero_p ());
4616 // (NEGATIVE | X) is nonzero.
4617 i1 = int_range<1> (INT (-5), INT (-3));
4618 i2.set_varying (integer_type_node);
4619 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4620 ASSERT_FALSE (res.contains_p (INT (0)));
4623 static void
4624 range_relational_tests ()
4626 int_range<2> lhs (unsigned_char_type_node);
4627 int_range<2> op1 (UCHAR (8), UCHAR (10));
4628 int_range<2> op2 (UCHAR (20), UCHAR (20));
4630 // Never wrapping additions mean LHS > OP1.
4631 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4632 ASSERT_TRUE (code == VREL_GT);
4634 // Most wrapping additions mean nothing...
4635 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4636 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4637 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4638 ASSERT_TRUE (code == VREL_VARYING);
4640 // However, always wrapping additions mean LHS < OP1.
4641 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4642 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4643 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4644 ASSERT_TRUE (code == VREL_LT);
4647 void
4648 range_op_tests ()
4650 range_op_rshift_tests ();
4651 range_op_lshift_tests ();
4652 range_op_bitwise_and_tests ();
4653 range_op_cast_tests ();
4654 range_relational_tests ();
4657 } // namespace selftest
4659 #endif // CHECKING_P