xtensa: constantsynth: Add new 3-insns synthesis pattern
[official-gcc.git] / gcc / range-op.cc
blob806edf1012ea2764ba89715689d54734c10a5993
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 // Use nonzero bits to determine if < 0 is false.
807 else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
808 r = range_false (type);
809 else
810 r = range_true_and_false (type);
811 return true;
814 bool
815 operator_lt::op1_range (irange &r, tree type,
816 const irange &lhs,
817 const irange &op2,
818 relation_kind rel ATTRIBUTE_UNUSED) const
820 switch (get_bool_state (r, lhs, type))
822 case BRS_TRUE:
823 build_lt (r, type, op2.upper_bound ());
824 break;
826 case BRS_FALSE:
827 build_ge (r, type, op2.lower_bound ());
828 break;
830 default:
831 break;
833 return true;
836 bool
837 operator_lt::op2_range (irange &r, tree type,
838 const irange &lhs,
839 const irange &op1,
840 relation_kind rel ATTRIBUTE_UNUSED) const
842 switch (get_bool_state (r, lhs, type))
844 case BRS_FALSE:
845 build_le (r, type, op1.upper_bound ());
846 break;
848 case BRS_TRUE:
849 build_gt (r, type, op1.lower_bound ());
850 break;
852 default:
853 break;
855 return true;
859 class operator_le : public range_operator
861 using range_operator::fold_range;
862 using range_operator::op1_range;
863 using range_operator::op2_range;
864 public:
865 virtual bool fold_range (irange &r, tree type,
866 const irange &op1,
867 const irange &op2,
868 relation_kind rel = VREL_VARYING) const;
869 virtual bool op1_range (irange &r, tree type,
870 const irange &lhs,
871 const irange &op2,
872 relation_kind rel = VREL_VARYING) const;
873 virtual bool op2_range (irange &r, tree type,
874 const irange &lhs,
875 const irange &op1,
876 relation_kind rel = VREL_VARYING) const;
877 virtual relation_kind op1_op2_relation (const irange &lhs) const;
878 } op_le;
880 // Check if the LHS range indicates a relation between OP1 and OP2.
882 relation_kind
883 le_op1_op2_relation (const irange &lhs)
885 if (lhs.undefined_p ())
886 return VREL_UNDEFINED;
888 // FALSE = op1 <= op2 indicates GT_EXPR.
889 if (lhs.zero_p ())
890 return VREL_GT;
892 // TRUE = op1 <= op2 indicates LE_EXPR.
893 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
894 return VREL_LE;
895 return VREL_VARYING;
898 relation_kind
899 operator_le::op1_op2_relation (const irange &lhs) const
901 return le_op1_op2_relation (lhs);
904 bool
905 operator_le::fold_range (irange &r, tree type,
906 const irange &op1,
907 const irange &op2,
908 relation_kind rel) const
910 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
911 return true;
913 signop sign = TYPE_SIGN (op1.type ());
914 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
916 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
917 r = range_true (type);
918 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
919 r = range_false (type);
920 else
921 r = range_true_and_false (type);
922 return true;
925 bool
926 operator_le::op1_range (irange &r, tree type,
927 const irange &lhs,
928 const irange &op2,
929 relation_kind rel ATTRIBUTE_UNUSED) const
931 switch (get_bool_state (r, lhs, type))
933 case BRS_TRUE:
934 build_le (r, type, op2.upper_bound ());
935 break;
937 case BRS_FALSE:
938 build_gt (r, type, op2.lower_bound ());
939 break;
941 default:
942 break;
944 return true;
947 bool
948 operator_le::op2_range (irange &r, tree type,
949 const irange &lhs,
950 const irange &op1,
951 relation_kind rel ATTRIBUTE_UNUSED) const
953 switch (get_bool_state (r, lhs, type))
955 case BRS_FALSE:
956 build_lt (r, type, op1.upper_bound ());
957 break;
959 case BRS_TRUE:
960 build_ge (r, type, op1.lower_bound ());
961 break;
963 default:
964 break;
966 return true;
970 class operator_gt : public range_operator
972 using range_operator::fold_range;
973 using range_operator::op1_range;
974 using range_operator::op2_range;
975 public:
976 virtual bool fold_range (irange &r, tree type,
977 const irange &op1,
978 const irange &op2,
979 relation_kind rel = VREL_VARYING) const;
980 virtual bool op1_range (irange &r, tree type,
981 const irange &lhs,
982 const irange &op2,
983 relation_kind rel = VREL_VARYING) const;
984 virtual bool op2_range (irange &r, tree type,
985 const irange &lhs,
986 const irange &op1,
987 relation_kind rel = VREL_VARYING) const;
988 virtual relation_kind op1_op2_relation (const irange &lhs) const;
989 } op_gt;
991 // Check if the LHS range indicates a relation between OP1 and OP2.
993 relation_kind
994 gt_op1_op2_relation (const irange &lhs)
996 if (lhs.undefined_p ())
997 return VREL_UNDEFINED;
999 // FALSE = op1 > op2 indicates LE_EXPR.
1000 if (lhs.zero_p ())
1001 return VREL_LE;
1003 // TRUE = op1 > op2 indicates GT_EXPR.
1004 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1005 return VREL_GT;
1006 return VREL_VARYING;
1009 relation_kind
1010 operator_gt::op1_op2_relation (const irange &lhs) const
1012 return gt_op1_op2_relation (lhs);
1016 bool
1017 operator_gt::fold_range (irange &r, tree type,
1018 const irange &op1, const irange &op2,
1019 relation_kind rel) const
1021 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1022 return true;
1024 signop sign = TYPE_SIGN (op1.type ());
1025 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1027 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1028 r = range_true (type);
1029 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1030 r = range_false (type);
1031 else
1032 r = range_true_and_false (type);
1033 return true;
1036 bool
1037 operator_gt::op1_range (irange &r, tree type,
1038 const irange &lhs, const irange &op2,
1039 relation_kind rel ATTRIBUTE_UNUSED) const
1041 switch (get_bool_state (r, lhs, type))
1043 case BRS_TRUE:
1044 build_gt (r, type, op2.lower_bound ());
1045 break;
1047 case BRS_FALSE:
1048 build_le (r, type, op2.upper_bound ());
1049 break;
1051 default:
1052 break;
1054 return true;
1057 bool
1058 operator_gt::op2_range (irange &r, tree type,
1059 const irange &lhs,
1060 const irange &op1,
1061 relation_kind rel ATTRIBUTE_UNUSED) const
1063 switch (get_bool_state (r, lhs, type))
1065 case BRS_FALSE:
1066 build_ge (r, type, op1.lower_bound ());
1067 break;
1069 case BRS_TRUE:
1070 build_lt (r, type, op1.upper_bound ());
1071 break;
1073 default:
1074 break;
1076 return true;
1080 class operator_ge : public range_operator
1082 using range_operator::fold_range;
1083 using range_operator::op1_range;
1084 using range_operator::op2_range;
1085 public:
1086 virtual bool fold_range (irange &r, tree type,
1087 const irange &op1,
1088 const irange &op2,
1089 relation_kind rel = VREL_VARYING) const;
1090 virtual bool op1_range (irange &r, tree type,
1091 const irange &lhs,
1092 const irange &op2,
1093 relation_kind rel = VREL_VARYING) const;
1094 virtual bool op2_range (irange &r, tree type,
1095 const irange &lhs,
1096 const irange &op1,
1097 relation_kind rel = VREL_VARYING) const;
1098 virtual relation_kind op1_op2_relation (const irange &lhs) const;
1099 } op_ge;
1101 // Check if the LHS range indicates a relation between OP1 and OP2.
1103 relation_kind
1104 ge_op1_op2_relation (const irange &lhs)
1106 if (lhs.undefined_p ())
1107 return VREL_UNDEFINED;
1109 // FALSE = op1 >= op2 indicates LT_EXPR.
1110 if (lhs.zero_p ())
1111 return VREL_LT;
1113 // TRUE = op1 >= op2 indicates GE_EXPR.
1114 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1115 return VREL_GE;
1116 return VREL_VARYING;
1119 relation_kind
1120 operator_ge::op1_op2_relation (const irange &lhs) const
1122 return ge_op1_op2_relation (lhs);
1125 bool
1126 operator_ge::fold_range (irange &r, tree type,
1127 const irange &op1,
1128 const irange &op2,
1129 relation_kind rel) const
1131 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1132 return true;
1134 signop sign = TYPE_SIGN (op1.type ());
1135 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1137 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1138 r = range_true (type);
1139 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1140 r = range_false (type);
1141 else
1142 r = range_true_and_false (type);
1143 return true;
1146 bool
1147 operator_ge::op1_range (irange &r, tree type,
1148 const irange &lhs,
1149 const irange &op2,
1150 relation_kind rel ATTRIBUTE_UNUSED) const
1152 switch (get_bool_state (r, lhs, type))
1154 case BRS_TRUE:
1155 build_ge (r, type, op2.lower_bound ());
1156 break;
1158 case BRS_FALSE:
1159 build_lt (r, type, op2.upper_bound ());
1160 break;
1162 default:
1163 break;
1165 return true;
1168 bool
1169 operator_ge::op2_range (irange &r, tree type,
1170 const irange &lhs,
1171 const irange &op1,
1172 relation_kind rel ATTRIBUTE_UNUSED) const
1174 switch (get_bool_state (r, lhs, type))
1176 case BRS_FALSE:
1177 build_gt (r, type, op1.lower_bound ());
1178 break;
1180 case BRS_TRUE:
1181 build_le (r, type, op1.upper_bound ());
1182 break;
1184 default:
1185 break;
1187 return true;
1191 class operator_plus : public range_operator
1193 using range_operator::op1_range;
1194 using range_operator::op2_range;
1195 using range_operator::lhs_op1_relation;
1196 using range_operator::lhs_op2_relation;
1197 public:
1198 virtual bool op1_range (irange &r, tree type,
1199 const irange &lhs,
1200 const irange &op2,
1201 relation_kind rel ATTRIBUTE_UNUSED) const;
1202 virtual bool op2_range (irange &r, tree type,
1203 const irange &lhs,
1204 const irange &op1,
1205 relation_kind rel ATTRIBUTE_UNUSED) const;
1206 virtual void wi_fold (irange &r, tree type,
1207 const wide_int &lh_lb,
1208 const wide_int &lh_ub,
1209 const wide_int &rh_lb,
1210 const wide_int &rh_ub) const;
1211 virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
1212 const irange &op2,
1213 relation_kind rel) const;
1214 virtual relation_kind lhs_op2_relation (const irange &lhs, const irange &op1,
1215 const irange &op2,
1216 relation_kind rel) const;
1217 } op_plus;
1219 // Check to see if the range of OP2 indicates anything about the relation
1220 // between LHS and OP1.
1222 relation_kind
1223 operator_plus::lhs_op1_relation (const irange &lhs,
1224 const irange &op1,
1225 const irange &op2,
1226 relation_kind) const
1228 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1229 return VREL_VARYING;
1231 tree type = lhs.type ();
1232 unsigned prec = TYPE_PRECISION (type);
1233 wi::overflow_type ovf1, ovf2;
1234 signop sign = TYPE_SIGN (type);
1236 // LHS = OP1 + 0 indicates LHS == OP1.
1237 if (op2.zero_p ())
1238 return VREL_EQ;
1240 if (TYPE_OVERFLOW_WRAPS (type))
1242 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1243 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1245 else
1246 ovf1 = ovf2 = wi::OVF_NONE;
1248 // Never wrapping additions.
1249 if (!ovf1 && !ovf2)
1251 // Positive op2 means lhs > op1.
1252 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1253 return VREL_GT;
1254 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1255 return VREL_GE;
1257 // Negative op2 means lhs < op1.
1258 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1259 return VREL_LT;
1260 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1261 return VREL_LE;
1263 // Always wrapping additions.
1264 else if (ovf1 && ovf1 == ovf2)
1266 // Positive op2 means lhs < op1.
1267 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1268 return VREL_LT;
1269 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1270 return VREL_LE;
1272 // Negative op2 means lhs > op1.
1273 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1274 return VREL_GT;
1275 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1276 return VREL_GE;
1279 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1280 if (!range_includes_zero_p (&op2))
1281 return VREL_NE;
1283 return VREL_VARYING;
1286 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1287 // operands.
1289 relation_kind
1290 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1291 const irange &op2, relation_kind rel) const
1293 return lhs_op1_relation (lhs, op2, op1, rel);
1296 void
1297 operator_plus::wi_fold (irange &r, tree type,
1298 const wide_int &lh_lb, const wide_int &lh_ub,
1299 const wide_int &rh_lb, const wide_int &rh_ub) const
1301 wi::overflow_type ov_lb, ov_ub;
1302 signop s = TYPE_SIGN (type);
1303 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1304 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1305 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1308 bool
1309 operator_plus::op1_range (irange &r, tree type,
1310 const irange &lhs,
1311 const irange &op2,
1312 relation_kind rel ATTRIBUTE_UNUSED) const
1314 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, lhs, op2);
1317 bool
1318 operator_plus::op2_range (irange &r, tree type,
1319 const irange &lhs,
1320 const irange &op1,
1321 relation_kind rel ATTRIBUTE_UNUSED) const
1323 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, lhs, op1);
1327 class operator_minus : public range_operator
1329 using range_operator::fold_range;
1330 using range_operator::op1_range;
1331 using range_operator::op2_range;
1332 public:
1333 virtual bool op1_range (irange &r, tree type,
1334 const irange &lhs,
1335 const irange &op2,
1336 relation_kind rel ATTRIBUTE_UNUSED) const;
1337 virtual bool op2_range (irange &r, tree type,
1338 const irange &lhs,
1339 const irange &op1,
1340 relation_kind rel ATTRIBUTE_UNUSED) const;
1341 virtual void wi_fold (irange &r, tree type,
1342 const wide_int &lh_lb,
1343 const wide_int &lh_ub,
1344 const wide_int &rh_lb,
1345 const wide_int &rh_ub) const;
1346 virtual relation_kind lhs_op1_relation (const irange &lhs,
1347 const irange &op1,
1348 const irange &op2,
1349 relation_kind rel) const;
1350 virtual bool op1_op2_relation_effect (irange &lhs_range,
1351 tree type,
1352 const irange &op1_range,
1353 const irange &op2_range,
1354 relation_kind rel) const;
1355 } op_minus;
1357 void
1358 operator_minus::wi_fold (irange &r, tree type,
1359 const wide_int &lh_lb, const wide_int &lh_ub,
1360 const wide_int &rh_lb, const wide_int &rh_ub) const
1362 wi::overflow_type ov_lb, ov_ub;
1363 signop s = TYPE_SIGN (type);
1364 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1365 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1366 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1370 // Return the relation between LHS and OP1 based on the relation between
1371 // OP1 and OP2.
1373 relation_kind
1374 operator_minus::lhs_op1_relation (const irange &, const irange &op1,
1375 const irange &, relation_kind rel) const
1377 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
1378 switch (rel)
1380 case VREL_GT:
1381 case VREL_GE:
1382 return VREL_LE;
1383 default:
1384 break;
1386 return VREL_VARYING;
1389 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1390 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1391 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1393 static bool
1394 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1395 const irange &op1_range ATTRIBUTE_UNUSED,
1396 const irange &op2_range ATTRIBUTE_UNUSED,
1397 relation_kind rel)
1399 if (rel == VREL_VARYING)
1400 return false;
1402 int_range<2> rel_range;
1403 unsigned prec = TYPE_PRECISION (type);
1404 signop sgn = TYPE_SIGN (type);
1406 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1407 if (rel == VREL_EQ)
1408 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1409 else if (rel == VREL_NE)
1410 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1411 VR_ANTI_RANGE);
1412 else if (TYPE_OVERFLOW_WRAPS (type))
1414 switch (rel)
1416 // For wrapping signed values and unsigned, if op1 > op2 or
1417 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1418 case VREL_GT:
1419 case VREL_LT:
1420 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1421 VR_ANTI_RANGE);
1422 break;
1423 default:
1424 return false;
1427 else
1429 switch (rel)
1431 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1432 case VREL_GT:
1433 rel_range = int_range<2> (type, wi::one (prec),
1434 wi::max_value (prec, sgn));
1435 break;
1436 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1437 case VREL_GE:
1438 rel_range = int_range<2> (type, wi::zero (prec),
1439 wi::max_value (prec, sgn));
1440 break;
1441 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1442 case VREL_LT:
1443 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1444 wi::minus_one (prec));
1445 break;
1446 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1447 case VREL_LE:
1448 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1449 wi::zero (prec));
1450 break;
1451 default:
1452 return false;
1455 lhs_range.intersect (rel_range);
1456 return true;
1459 bool
1460 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1461 const irange &op1_range,
1462 const irange &op2_range,
1463 relation_kind rel) const
1465 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1466 rel);
1469 bool
1470 operator_minus::op1_range (irange &r, tree type,
1471 const irange &lhs,
1472 const irange &op2,
1473 relation_kind rel ATTRIBUTE_UNUSED) const
1475 return range_op_handler (PLUS_EXPR, type).fold_range (r, type, lhs, op2);
1478 bool
1479 operator_minus::op2_range (irange &r, tree type,
1480 const irange &lhs,
1481 const irange &op1,
1482 relation_kind rel ATTRIBUTE_UNUSED) const
1484 return fold_range (r, type, op1, lhs);
1488 class operator_pointer_diff : public range_operator
1490 virtual bool op1_op2_relation_effect (irange &lhs_range,
1491 tree type,
1492 const irange &op1_range,
1493 const irange &op2_range,
1494 relation_kind rel) const;
1495 } op_pointer_diff;
1497 bool
1498 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1499 const irange &op1_range,
1500 const irange &op2_range,
1501 relation_kind rel) const
1503 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1504 rel);
1508 class operator_min : public range_operator
1510 public:
1511 virtual void wi_fold (irange &r, tree type,
1512 const wide_int &lh_lb,
1513 const wide_int &lh_ub,
1514 const wide_int &rh_lb,
1515 const wide_int &rh_ub) const;
1516 } op_min;
1518 void
1519 operator_min::wi_fold (irange &r, tree type,
1520 const wide_int &lh_lb, const wide_int &lh_ub,
1521 const wide_int &rh_lb, const wide_int &rh_ub) const
1523 signop s = TYPE_SIGN (type);
1524 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1525 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1526 value_range_with_overflow (r, type, new_lb, new_ub);
1530 class operator_max : public range_operator
1532 public:
1533 virtual void wi_fold (irange &r, tree type,
1534 const wide_int &lh_lb,
1535 const wide_int &lh_ub,
1536 const wide_int &rh_lb,
1537 const wide_int &rh_ub) const;
1538 } op_max;
1540 void
1541 operator_max::wi_fold (irange &r, tree type,
1542 const wide_int &lh_lb, const wide_int &lh_ub,
1543 const wide_int &rh_lb, const wide_int &rh_ub) const
1545 signop s = TYPE_SIGN (type);
1546 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1547 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1548 value_range_with_overflow (r, type, new_lb, new_ub);
1552 class cross_product_operator : public range_operator
1554 public:
1555 // Perform an operation between two wide-ints and place the result
1556 // in R. Return true if the operation overflowed.
1557 virtual bool wi_op_overflows (wide_int &r,
1558 tree type,
1559 const wide_int &,
1560 const wide_int &) const = 0;
1562 // Calculate the cross product of two sets of sub-ranges and return it.
1563 void wi_cross_product (irange &r, tree type,
1564 const wide_int &lh_lb,
1565 const wide_int &lh_ub,
1566 const wide_int &rh_lb,
1567 const wide_int &rh_ub) const;
1570 // Calculate the cross product of two sets of ranges and return it.
1572 // Multiplications, divisions and shifts are a bit tricky to handle,
1573 // depending on the mix of signs we have in the two ranges, we need to
1574 // operate on different values to get the minimum and maximum values
1575 // for the new range. One approach is to figure out all the
1576 // variations of range combinations and do the operations.
1578 // However, this involves several calls to compare_values and it is
1579 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1580 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1581 // figure the smallest and largest values to form the new range.
1583 void
1584 cross_product_operator::wi_cross_product (irange &r, tree type,
1585 const wide_int &lh_lb,
1586 const wide_int &lh_ub,
1587 const wide_int &rh_lb,
1588 const wide_int &rh_ub) const
1590 wide_int cp1, cp2, cp3, cp4;
1591 // Default to varying.
1592 r.set_varying (type);
1594 // Compute the 4 cross operations, bailing if we get an overflow we
1595 // can't handle.
1596 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1597 return;
1598 if (wi::eq_p (lh_lb, lh_ub))
1599 cp3 = cp1;
1600 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1601 return;
1602 if (wi::eq_p (rh_lb, rh_ub))
1603 cp2 = cp1;
1604 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1605 return;
1606 if (wi::eq_p (lh_lb, lh_ub))
1607 cp4 = cp2;
1608 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1609 return;
1611 // Order pairs.
1612 signop sign = TYPE_SIGN (type);
1613 if (wi::gt_p (cp1, cp2, sign))
1614 std::swap (cp1, cp2);
1615 if (wi::gt_p (cp3, cp4, sign))
1616 std::swap (cp3, cp4);
1618 // Choose min and max from the ordered pairs.
1619 wide_int res_lb = wi::min (cp1, cp3, sign);
1620 wide_int res_ub = wi::max (cp2, cp4, sign);
1621 value_range_with_overflow (r, type, res_lb, res_ub);
1625 class operator_mult : public cross_product_operator
1627 using range_operator::op1_range;
1628 using range_operator::op2_range;
1629 public:
1630 virtual void wi_fold (irange &r, tree type,
1631 const wide_int &lh_lb,
1632 const wide_int &lh_ub,
1633 const wide_int &rh_lb,
1634 const wide_int &rh_ub) const;
1635 virtual bool wi_op_overflows (wide_int &res, tree type,
1636 const wide_int &w0, const wide_int &w1) const;
1637 virtual bool op1_range (irange &r, tree type,
1638 const irange &lhs,
1639 const irange &op2,
1640 relation_kind rel ATTRIBUTE_UNUSED) const;
1641 virtual bool op2_range (irange &r, tree type,
1642 const irange &lhs,
1643 const irange &op1,
1644 relation_kind rel ATTRIBUTE_UNUSED) const;
1645 } op_mult;
1647 bool
1648 operator_mult::op1_range (irange &r, tree type,
1649 const irange &lhs, const irange &op2,
1650 relation_kind rel ATTRIBUTE_UNUSED) const
1652 tree offset;
1654 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1655 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1656 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1657 if (TYPE_OVERFLOW_WRAPS (type))
1658 return false;
1660 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1661 return range_op_handler (TRUNC_DIV_EXPR, type).fold_range (r, type,
1662 lhs, op2);
1663 return false;
1666 bool
1667 operator_mult::op2_range (irange &r, tree type,
1668 const irange &lhs, const irange &op1,
1669 relation_kind rel) const
1671 return operator_mult::op1_range (r, type, lhs, op1, rel);
1674 bool
1675 operator_mult::wi_op_overflows (wide_int &res, tree type,
1676 const wide_int &w0, const wide_int &w1) const
1678 wi::overflow_type overflow = wi::OVF_NONE;
1679 signop sign = TYPE_SIGN (type);
1680 res = wi::mul (w0, w1, sign, &overflow);
1681 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1683 // For multiplication, the sign of the overflow is given
1684 // by the comparison of the signs of the operands.
1685 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1686 res = wi::max_value (w0.get_precision (), sign);
1687 else
1688 res = wi::min_value (w0.get_precision (), sign);
1689 return false;
1691 return overflow;
1694 void
1695 operator_mult::wi_fold (irange &r, tree type,
1696 const wide_int &lh_lb, const wide_int &lh_ub,
1697 const wide_int &rh_lb, const wide_int &rh_ub) const
1699 if (TYPE_OVERFLOW_UNDEFINED (type))
1701 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1702 return;
1705 // Multiply the ranges when overflow wraps. This is basically fancy
1706 // code so we don't drop to varying with an unsigned
1707 // [-3,-1]*[-3,-1].
1709 // This test requires 2*prec bits if both operands are signed and
1710 // 2*prec + 2 bits if either is not. Therefore, extend the values
1711 // using the sign of the result to PREC2. From here on out,
1712 // everthing is just signed math no matter what the input types
1713 // were.
1715 signop sign = TYPE_SIGN (type);
1716 unsigned prec = TYPE_PRECISION (type);
1717 widest2_int min0 = widest2_int::from (lh_lb, sign);
1718 widest2_int max0 = widest2_int::from (lh_ub, sign);
1719 widest2_int min1 = widest2_int::from (rh_lb, sign);
1720 widest2_int max1 = widest2_int::from (rh_ub, sign);
1721 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1722 widest2_int size = sizem1 + 1;
1724 // Canonicalize the intervals.
1725 if (sign == UNSIGNED)
1727 if (wi::ltu_p (size, min0 + max0))
1729 min0 -= size;
1730 max0 -= size;
1732 if (wi::ltu_p (size, min1 + max1))
1734 min1 -= size;
1735 max1 -= size;
1739 // Sort the 4 products so that min is in prod0 and max is in
1740 // prod3.
1741 widest2_int prod0 = min0 * min1;
1742 widest2_int prod1 = min0 * max1;
1743 widest2_int prod2 = max0 * min1;
1744 widest2_int prod3 = max0 * max1;
1746 // min0min1 > max0max1
1747 if (prod0 > prod3)
1748 std::swap (prod0, prod3);
1750 // min0max1 > max0min1
1751 if (prod1 > prod2)
1752 std::swap (prod1, prod2);
1754 if (prod0 > prod1)
1755 std::swap (prod0, prod1);
1757 if (prod2 > prod3)
1758 std::swap (prod2, prod3);
1760 // diff = max - min
1761 prod2 = prod3 - prod0;
1762 if (wi::geu_p (prod2, sizem1))
1763 // The range covers all values.
1764 r.set_varying (type);
1765 else
1767 wide_int new_lb = wide_int::from (prod0, prec, sign);
1768 wide_int new_ub = wide_int::from (prod3, prec, sign);
1769 create_possibly_reversed_range (r, type, new_lb, new_ub);
1774 class operator_div : public cross_product_operator
1776 public:
1777 operator_div (enum tree_code c) { code = c; }
1778 virtual void wi_fold (irange &r, tree type,
1779 const wide_int &lh_lb,
1780 const wide_int &lh_ub,
1781 const wide_int &rh_lb,
1782 const wide_int &rh_ub) const;
1783 virtual bool wi_op_overflows (wide_int &res, tree type,
1784 const wide_int &, const wide_int &) const;
1785 private:
1786 enum tree_code code;
1789 bool
1790 operator_div::wi_op_overflows (wide_int &res, tree type,
1791 const wide_int &w0, const wide_int &w1) const
1793 if (w1 == 0)
1794 return true;
1796 wi::overflow_type overflow = wi::OVF_NONE;
1797 signop sign = TYPE_SIGN (type);
1799 switch (code)
1801 case EXACT_DIV_EXPR:
1802 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1803 // operator_exact_divide. No need to handle it here.
1804 gcc_unreachable ();
1805 break;
1806 case TRUNC_DIV_EXPR:
1807 res = wi::div_trunc (w0, w1, sign, &overflow);
1808 break;
1809 case FLOOR_DIV_EXPR:
1810 res = wi::div_floor (w0, w1, sign, &overflow);
1811 break;
1812 case ROUND_DIV_EXPR:
1813 res = wi::div_round (w0, w1, sign, &overflow);
1814 break;
1815 case CEIL_DIV_EXPR:
1816 res = wi::div_ceil (w0, w1, sign, &overflow);
1817 break;
1818 default:
1819 gcc_unreachable ();
1822 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1824 // For division, the only case is -INF / -1 = +INF.
1825 res = wi::max_value (w0.get_precision (), sign);
1826 return false;
1828 return overflow;
1831 void
1832 operator_div::wi_fold (irange &r, tree type,
1833 const wide_int &lh_lb, const wide_int &lh_ub,
1834 const wide_int &rh_lb, const wide_int &rh_ub) const
1836 const wide_int dividend_min = lh_lb;
1837 const wide_int dividend_max = lh_ub;
1838 const wide_int divisor_min = rh_lb;
1839 const wide_int divisor_max = rh_ub;
1840 signop sign = TYPE_SIGN (type);
1841 unsigned prec = TYPE_PRECISION (type);
1842 wide_int extra_min, extra_max;
1844 // If we know we won't divide by zero, just do the division.
1845 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1847 wi_cross_product (r, type, dividend_min, dividend_max,
1848 divisor_min, divisor_max);
1849 return;
1852 // If we're definitely dividing by zero, there's nothing to do.
1853 if (wi_zero_p (type, divisor_min, divisor_max))
1855 r.set_undefined ();
1856 return;
1859 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1860 // skip any division by zero.
1862 // First divide by the negative numbers, if any.
1863 if (wi::neg_p (divisor_min, sign))
1864 wi_cross_product (r, type, dividend_min, dividend_max,
1865 divisor_min, wi::minus_one (prec));
1866 else
1867 r.set_undefined ();
1869 // Then divide by the non-zero positive numbers, if any.
1870 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1872 int_range_max tmp;
1873 wi_cross_product (tmp, type, dividend_min, dividend_max,
1874 wi::one (prec), divisor_max);
1875 r.union_ (tmp);
1877 // We shouldn't still have undefined here.
1878 gcc_checking_assert (!r.undefined_p ());
1881 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1882 operator_div op_floor_div (FLOOR_DIV_EXPR);
1883 operator_div op_round_div (ROUND_DIV_EXPR);
1884 operator_div op_ceil_div (CEIL_DIV_EXPR);
1887 class operator_exact_divide : public operator_div
1889 using range_operator::op1_range;
1890 public:
1891 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1892 virtual bool op1_range (irange &r, tree type,
1893 const irange &lhs,
1894 const irange &op2,
1895 relation_kind rel ATTRIBUTE_UNUSED) const;
1897 } op_exact_div;
1899 bool
1900 operator_exact_divide::op1_range (irange &r, tree type,
1901 const irange &lhs,
1902 const irange &op2,
1903 relation_kind rel ATTRIBUTE_UNUSED) const
1905 tree offset;
1906 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1907 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1908 // We wont bother trying to enumerate all the in between stuff :-P
1909 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1910 // the time however.
1911 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1912 if (op2.singleton_p (&offset)
1913 && !integer_zerop (offset))
1914 return range_op_handler (MULT_EXPR, type).fold_range (r, type, lhs, op2);
1915 return false;
1919 class operator_lshift : public cross_product_operator
1921 using range_operator::fold_range;
1922 using range_operator::op1_range;
1923 public:
1924 virtual bool op1_range (irange &r, tree type,
1925 const irange &lhs,
1926 const irange &op2,
1927 relation_kind rel = VREL_VARYING) const;
1928 virtual bool fold_range (irange &r, tree type,
1929 const irange &op1,
1930 const irange &op2,
1931 relation_kind rel = VREL_VARYING) const;
1933 virtual void wi_fold (irange &r, tree type,
1934 const wide_int &lh_lb, const wide_int &lh_ub,
1935 const wide_int &rh_lb, const wide_int &rh_ub) const;
1936 virtual bool wi_op_overflows (wide_int &res,
1937 tree type,
1938 const wide_int &,
1939 const wide_int &) const;
1940 } op_lshift;
1942 class operator_rshift : public cross_product_operator
1944 using range_operator::fold_range;
1945 using range_operator::op1_range;
1946 using range_operator::lhs_op1_relation;
1947 public:
1948 virtual bool fold_range (irange &r, tree type,
1949 const irange &op1,
1950 const irange &op2,
1951 relation_kind rel = VREL_VARYING) const;
1952 virtual void wi_fold (irange &r, tree type,
1953 const wide_int &lh_lb,
1954 const wide_int &lh_ub,
1955 const wide_int &rh_lb,
1956 const wide_int &rh_ub) const;
1957 virtual bool wi_op_overflows (wide_int &res,
1958 tree type,
1959 const wide_int &w0,
1960 const wide_int &w1) const;
1961 virtual bool op1_range (irange &, tree type,
1962 const irange &lhs,
1963 const irange &op2,
1964 relation_kind rel = VREL_VARYING) const;
1965 virtual relation_kind lhs_op1_relation (const irange &lhs,
1966 const irange &op1,
1967 const irange &op2,
1968 relation_kind rel) const;
1969 } op_rshift;
1972 relation_kind
1973 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
1974 const irange &op1,
1975 const irange &op2,
1976 relation_kind) const
1978 // If both operands range are >= 0, then the LHS <= op1.
1979 if (!op1.undefined_p () && !op2.undefined_p ()
1980 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
1981 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
1982 return VREL_LE;
1983 return VREL_VARYING;
1986 bool
1987 operator_lshift::fold_range (irange &r, tree type,
1988 const irange &op1,
1989 const irange &op2,
1990 relation_kind rel) const
1992 int_range_max shift_range;
1993 if (!get_shift_range (shift_range, type, op2))
1995 if (op2.undefined_p ())
1996 r.set_undefined ();
1997 else
1998 r.set_varying (type);
1999 return true;
2002 // Transform left shifts by constants into multiplies.
2003 if (shift_range.singleton_p ())
2005 unsigned shift = shift_range.lower_bound ().to_uhwi ();
2006 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2007 int_range<1> mult (type, tmp, tmp);
2009 // Force wrapping multiplication.
2010 bool saved_flag_wrapv = flag_wrapv;
2011 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2012 flag_wrapv = 1;
2013 flag_wrapv_pointer = 1;
2014 bool b = op_mult.fold_range (r, type, op1, mult);
2015 flag_wrapv = saved_flag_wrapv;
2016 flag_wrapv_pointer = saved_flag_wrapv_pointer;
2017 return b;
2019 else
2020 // Otherwise, invoke the generic fold routine.
2021 return range_operator::fold_range (r, type, op1, shift_range, rel);
2024 void
2025 operator_lshift::wi_fold (irange &r, tree type,
2026 const wide_int &lh_lb, const wide_int &lh_ub,
2027 const wide_int &rh_lb, const wide_int &rh_ub) const
2029 signop sign = TYPE_SIGN (type);
2030 unsigned prec = TYPE_PRECISION (type);
2031 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2032 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2033 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2034 // overflow. However, for that to happen, rh.max needs to be zero,
2035 // which means rh is a singleton range of zero, which means we simply return
2036 // [lh_lb, lh_ub] as the range.
2037 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2039 r = int_range<2> (type, lh_lb, lh_ub);
2040 return;
2043 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2044 wide_int complement = ~(bound - 1);
2045 wide_int low_bound, high_bound;
2046 bool in_bounds = false;
2048 if (sign == UNSIGNED)
2050 low_bound = bound;
2051 high_bound = complement;
2052 if (wi::ltu_p (lh_ub, low_bound))
2054 // [5, 6] << [1, 2] == [10, 24].
2055 // We're shifting out only zeroes, the value increases
2056 // monotonically.
2057 in_bounds = true;
2059 else if (wi::ltu_p (high_bound, lh_lb))
2061 // [0xffffff00, 0xffffffff] << [1, 2]
2062 // == [0xfffffc00, 0xfffffffe].
2063 // We're shifting out only ones, the value decreases
2064 // monotonically.
2065 in_bounds = true;
2068 else
2070 // [-1, 1] << [1, 2] == [-4, 4]
2071 low_bound = complement;
2072 high_bound = bound;
2073 if (wi::lts_p (lh_ub, high_bound)
2074 && wi::lts_p (low_bound, lh_lb))
2076 // For non-negative numbers, we're shifting out only zeroes,
2077 // the value increases monotonically. For negative numbers,
2078 // we're shifting out only ones, the value decreases
2079 // monotonically.
2080 in_bounds = true;
2084 if (in_bounds)
2085 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2086 else
2087 r.set_varying (type);
2090 bool
2091 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2092 const wide_int &w0, const wide_int &w1) const
2094 signop sign = TYPE_SIGN (type);
2095 if (wi::neg_p (w1))
2097 // It's unclear from the C standard whether shifts can overflow.
2098 // The following code ignores overflow; perhaps a C standard
2099 // interpretation ruling is needed.
2100 res = wi::rshift (w0, -w1, sign);
2102 else
2103 res = wi::lshift (w0, w1);
2104 return false;
2107 bool
2108 operator_lshift::op1_range (irange &r,
2109 tree type,
2110 const irange &lhs,
2111 const irange &op2,
2112 relation_kind rel ATTRIBUTE_UNUSED) const
2114 tree shift_amount;
2116 if (!lhs.contains_p (build_zero_cst (type)))
2117 r.set_nonzero (type);
2118 else
2119 r.set_varying (type);
2121 if (op2.singleton_p (&shift_amount))
2123 wide_int shift = wi::to_wide (shift_amount);
2124 if (wi::lt_p (shift, 0, SIGNED))
2125 return false;
2126 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2127 TYPE_PRECISION (op2.type ())),
2128 UNSIGNED))
2129 return false;
2130 if (shift == 0)
2132 r.intersect (lhs);
2133 return true;
2136 // Work completely in unsigned mode to start.
2137 tree utype = type;
2138 int_range_max tmp_range;
2139 if (TYPE_SIGN (type) == SIGNED)
2141 int_range_max tmp = lhs;
2142 utype = unsigned_type_for (type);
2143 range_cast (tmp, utype);
2144 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2146 else
2147 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2149 // Start with ranges which can produce the LHS by right shifting the
2150 // result by the shift amount.
2151 // ie [0x08, 0xF0] = op1 << 2 will start with
2152 // [00001000, 11110000] = op1 << 2
2153 // [0x02, 0x4C] aka [00000010, 00111100]
2155 // Then create a range from the LB with the least significant upper bit
2156 // set, to the upper bound with all the bits set.
2157 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2159 // Ideally we do this for each subrange, but just lump them all for now.
2160 unsigned low_bits = TYPE_PRECISION (utype)
2161 - TREE_INT_CST_LOW (shift_amount);
2162 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2163 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2164 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2165 int_range<2> fill_range (utype, new_lb, new_ub);
2166 tmp_range.union_ (fill_range);
2168 if (utype != type)
2169 range_cast (tmp_range, type);
2171 r.intersect (tmp_range);
2172 return true;
2175 return !r.varying_p ();
2178 bool
2179 operator_rshift::op1_range (irange &r,
2180 tree type,
2181 const irange &lhs,
2182 const irange &op2,
2183 relation_kind rel ATTRIBUTE_UNUSED) const
2185 tree shift;
2186 if (op2.singleton_p (&shift))
2188 // Ignore nonsensical shifts.
2189 unsigned prec = TYPE_PRECISION (type);
2190 if (wi::ge_p (wi::to_wide (shift),
2191 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2192 UNSIGNED))
2193 return false;
2194 if (wi::to_wide (shift) == 0)
2196 r = lhs;
2197 return true;
2200 // Folding the original operation may discard some impossible
2201 // ranges from the LHS.
2202 int_range_max lhs_refined;
2203 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2204 lhs_refined.intersect (lhs);
2205 if (lhs_refined.undefined_p ())
2207 r.set_undefined ();
2208 return true;
2210 int_range_max shift_range (shift, shift);
2211 int_range_max lb, ub;
2212 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2213 // LHS
2214 // 0000 0111 = OP1 >> 3
2216 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2217 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2218 // right hand side (0x07).
2219 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2220 fold_build2 (LSHIFT_EXPR, type,
2221 build_minus_one_cst (type),
2222 shift));
2223 int_range_max mask_range (build_zero_cst (type), mask);
2224 op_plus.fold_range (ub, type, lb, mask_range);
2225 r = lb;
2226 r.union_ (ub);
2227 if (!lhs_refined.contains_p (build_zero_cst (type)))
2229 mask_range.invert ();
2230 r.intersect (mask_range);
2232 return true;
2234 return false;
2237 bool
2238 operator_rshift::wi_op_overflows (wide_int &res,
2239 tree type,
2240 const wide_int &w0,
2241 const wide_int &w1) const
2243 signop sign = TYPE_SIGN (type);
2244 if (wi::neg_p (w1))
2245 res = wi::lshift (w0, -w1);
2246 else
2248 // It's unclear from the C standard whether shifts can overflow.
2249 // The following code ignores overflow; perhaps a C standard
2250 // interpretation ruling is needed.
2251 res = wi::rshift (w0, w1, sign);
2253 return false;
2256 bool
2257 operator_rshift::fold_range (irange &r, tree type,
2258 const irange &op1,
2259 const irange &op2,
2260 relation_kind rel) const
2262 int_range_max shift;
2263 if (!get_shift_range (shift, type, op2))
2265 if (op2.undefined_p ())
2266 r.set_undefined ();
2267 else
2268 r.set_varying (type);
2269 return true;
2272 return range_operator::fold_range (r, type, op1, shift, rel);
2275 void
2276 operator_rshift::wi_fold (irange &r, tree type,
2277 const wide_int &lh_lb, const wide_int &lh_ub,
2278 const wide_int &rh_lb, const wide_int &rh_ub) const
2280 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2284 class operator_cast: public range_operator
2286 using range_operator::fold_range;
2287 using range_operator::op1_range;
2288 public:
2289 virtual bool fold_range (irange &r, tree type,
2290 const irange &op1,
2291 const irange &op2,
2292 relation_kind rel = VREL_VARYING) const;
2293 virtual bool op1_range (irange &r, tree type,
2294 const irange &lhs,
2295 const irange &op2,
2296 relation_kind rel = VREL_VARYING) const;
2297 private:
2298 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2299 bool inside_domain_p (const wide_int &min, const wide_int &max,
2300 const irange &outer) const;
2301 void fold_pair (irange &r, unsigned index, const irange &inner,
2302 const irange &outer) const;
2303 } op_convert;
2305 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2307 inline bool
2308 operator_cast::truncating_cast_p (const irange &inner,
2309 const irange &outer) const
2311 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2314 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2316 bool
2317 operator_cast::inside_domain_p (const wide_int &min,
2318 const wide_int &max,
2319 const irange &range) const
2321 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2322 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2323 signop domain_sign = TYPE_SIGN (range.type ());
2324 return (wi::le_p (min, domain_max, domain_sign)
2325 && wi::le_p (max, domain_max, domain_sign)
2326 && wi::ge_p (min, domain_min, domain_sign)
2327 && wi::ge_p (max, domain_min, domain_sign));
2331 // Helper for fold_range which work on a pair at a time.
2333 void
2334 operator_cast::fold_pair (irange &r, unsigned index,
2335 const irange &inner,
2336 const irange &outer) const
2338 tree inner_type = inner.type ();
2339 tree outer_type = outer.type ();
2340 signop inner_sign = TYPE_SIGN (inner_type);
2341 unsigned outer_prec = TYPE_PRECISION (outer_type);
2343 // check to see if casting from INNER to OUTER is a conversion that
2344 // fits in the resulting OUTER type.
2345 wide_int inner_lb = inner.lower_bound (index);
2346 wide_int inner_ub = inner.upper_bound (index);
2347 if (truncating_cast_p (inner, outer))
2349 // We may be able to accomodate a truncating cast if the
2350 // resulting range can be represented in the target type...
2351 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2352 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2353 inner_sign) != 0)
2355 r.set_varying (outer_type);
2356 return;
2359 // ...but we must still verify that the final range fits in the
2360 // domain. This catches -fstrict-enum restrictions where the domain
2361 // range is smaller than what fits in the underlying type.
2362 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2363 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2364 if (inside_domain_p (min, max, outer))
2365 create_possibly_reversed_range (r, outer_type, min, max);
2366 else
2367 r.set_varying (outer_type);
2371 bool
2372 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2373 const irange &inner,
2374 const irange &outer,
2375 relation_kind rel ATTRIBUTE_UNUSED) const
2377 if (empty_range_varying (r, type, inner, outer))
2378 return true;
2380 gcc_checking_assert (outer.varying_p ());
2381 gcc_checking_assert (inner.num_pairs () > 0);
2383 // Avoid a temporary by folding the first pair directly into the result.
2384 fold_pair (r, 0, inner, outer);
2386 // Then process any additonal pairs by unioning with their results.
2387 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2389 int_range_max tmp;
2390 fold_pair (tmp, x, inner, outer);
2391 r.union_ (tmp);
2392 if (r.varying_p ())
2393 return true;
2395 return true;
2398 bool
2399 operator_cast::op1_range (irange &r, tree type,
2400 const irange &lhs,
2401 const irange &op2,
2402 relation_kind rel ATTRIBUTE_UNUSED) const
2404 tree lhs_type = lhs.type ();
2405 gcc_checking_assert (types_compatible_p (op2.type(), type));
2407 // If we are calculating a pointer, shortcut to what we really care about.
2408 if (POINTER_TYPE_P (type))
2410 // Conversion from other pointers or a constant (including 0/NULL)
2411 // are straightforward.
2412 if (POINTER_TYPE_P (lhs.type ())
2413 || (lhs.singleton_p ()
2414 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2416 r = lhs;
2417 range_cast (r, type);
2419 else
2421 // If the LHS is not a pointer nor a singleton, then it is
2422 // either VARYING or non-zero.
2423 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2424 r.set_nonzero (type);
2425 else
2426 r.set_varying (type);
2428 r.intersect (op2);
2429 return true;
2432 if (truncating_cast_p (op2, lhs))
2434 if (lhs.varying_p ())
2435 r.set_varying (type);
2436 else
2438 // We want to insert the LHS as an unsigned value since it
2439 // would not trigger the signed bit of the larger type.
2440 int_range_max converted_lhs = lhs;
2441 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2442 range_cast (converted_lhs, type);
2443 // Start by building the positive signed outer range for the type.
2444 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2445 TYPE_PRECISION (type));
2446 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2447 SIGNED));
2448 // For the signed part, we need to simply union the 2 ranges now.
2449 r.union_ (converted_lhs);
2451 // Create maximal negative number outside of LHS bits.
2452 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2453 TYPE_PRECISION (type));
2454 // Add this to the unsigned LHS range(s).
2455 int_range_max lim_range (type, lim, lim);
2456 int_range_max lhs_neg;
2457 range_op_handler (PLUS_EXPR, type).fold_range (lhs_neg, type,
2458 converted_lhs,
2459 lim_range);
2460 // lhs_neg now has all the negative versions of the LHS.
2461 // Now union in all the values from SIGNED MIN (0x80000) to
2462 // lim-1 in order to fill in all the ranges with the upper
2463 // bits set.
2465 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2466 // we don't need to create a range from min to lim-1
2467 // calculate neg range traps trying to create [lim, lim - 1].
2468 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2469 if (lim != min_val)
2471 int_range_max neg (type,
2472 wi::min_value (TYPE_PRECISION (type),
2473 SIGNED),
2474 lim - 1);
2475 lhs_neg.union_ (neg);
2477 // And finally, munge the signed and unsigned portions.
2478 r.union_ (lhs_neg);
2480 // And intersect with any known value passed in the extra operand.
2481 r.intersect (op2);
2482 return true;
2485 int_range_max tmp;
2486 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2487 tmp = lhs;
2488 else
2490 // The cast is not truncating, and the range is restricted to
2491 // the range of the RHS by this assignment.
2493 // Cast the range of the RHS to the type of the LHS.
2494 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2495 // Intersect this with the LHS range will produce the range,
2496 // which will be cast to the RHS type before returning.
2497 tmp.intersect (lhs);
2500 // Cast the calculated range to the type of the RHS.
2501 fold_range (r, type, tmp, int_range<1> (type));
2502 return true;
2506 class operator_logical_and : public range_operator
2508 using range_operator::fold_range;
2509 using range_operator::op1_range;
2510 using range_operator::op2_range;
2511 public:
2512 virtual bool fold_range (irange &r, tree type,
2513 const irange &lh,
2514 const irange &rh,
2515 relation_kind rel = VREL_VARYING) const;
2516 virtual bool op1_range (irange &r, tree type,
2517 const irange &lhs,
2518 const irange &op2,
2519 relation_kind rel = VREL_VARYING) const;
2520 virtual bool op2_range (irange &r, tree type,
2521 const irange &lhs,
2522 const irange &op1,
2523 relation_kind rel = VREL_VARYING) const;
2524 } op_logical_and;
2527 bool
2528 operator_logical_and::fold_range (irange &r, tree type,
2529 const irange &lh,
2530 const irange &rh,
2531 relation_kind rel ATTRIBUTE_UNUSED) const
2533 if (empty_range_varying (r, type, lh, rh))
2534 return true;
2536 // 0 && anything is 0.
2537 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2538 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2539 r = range_false (type);
2540 else if (lh.contains_p (build_zero_cst (lh.type ()))
2541 || rh.contains_p (build_zero_cst (rh.type ())))
2542 // To reach this point, there must be a logical 1 on each side, and
2543 // the only remaining question is whether there is a zero or not.
2544 r = range_true_and_false (type);
2545 else
2546 r = range_true (type);
2547 return true;
2550 bool
2551 operator_logical_and::op1_range (irange &r, tree type,
2552 const irange &lhs,
2553 const irange &op2 ATTRIBUTE_UNUSED,
2554 relation_kind rel ATTRIBUTE_UNUSED) const
2556 switch (get_bool_state (r, lhs, type))
2558 case BRS_TRUE:
2559 // A true result means both sides of the AND must be true.
2560 r = range_true (type);
2561 break;
2562 default:
2563 // Any other result means only one side has to be false, the
2564 // other side can be anything. So we cannot be sure of any
2565 // result here.
2566 r = range_true_and_false (type);
2567 break;
2569 return true;
2572 bool
2573 operator_logical_and::op2_range (irange &r, tree type,
2574 const irange &lhs,
2575 const irange &op1,
2576 relation_kind rel ATTRIBUTE_UNUSED) const
2578 return operator_logical_and::op1_range (r, type, lhs, op1);
2582 class operator_bitwise_and : public range_operator
2584 using range_operator::fold_range;
2585 using range_operator::op1_range;
2586 using range_operator::op2_range;
2587 public:
2588 virtual bool fold_range (irange &r, tree type,
2589 const irange &lh,
2590 const irange &rh,
2591 relation_kind rel = VREL_VARYING) const;
2592 virtual bool op1_range (irange &r, tree type,
2593 const irange &lhs,
2594 const irange &op2,
2595 relation_kind rel = VREL_VARYING) const;
2596 virtual bool op2_range (irange &r, tree type,
2597 const irange &lhs,
2598 const irange &op1,
2599 relation_kind rel = VREL_VARYING) const;
2600 virtual void wi_fold (irange &r, tree type,
2601 const wide_int &lh_lb,
2602 const wide_int &lh_ub,
2603 const wide_int &rh_lb,
2604 const wide_int &rh_ub) const;
2605 private:
2606 void simple_op1_range_solver (irange &r, tree type,
2607 const irange &lhs,
2608 const irange &op2) const;
2609 } op_bitwise_and;
2611 bool
2612 operator_bitwise_and::fold_range (irange &r, tree type,
2613 const irange &lh,
2614 const irange &rh,
2615 relation_kind rel ATTRIBUTE_UNUSED) const
2617 if (range_operator::fold_range (r, type, lh, rh))
2619 if (!lh.undefined_p () && !rh.undefined_p ())
2620 r.set_nonzero_bits (wi::bit_and (lh.get_nonzero_bits (),
2621 rh.get_nonzero_bits ()));
2622 return true;
2624 return false;
2628 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2629 // by considering the number of leading redundant sign bit copies.
2630 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2631 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2632 static bool
2633 wi_optimize_signed_bitwise_op (irange &r, tree type,
2634 const wide_int &lh_lb, const wide_int &lh_ub,
2635 const wide_int &rh_lb, const wide_int &rh_ub)
2637 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2638 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2639 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2640 if (new_clrsb == 0)
2641 return false;
2642 int type_prec = TYPE_PRECISION (type);
2643 int rprec = (type_prec - new_clrsb) - 1;
2644 value_range_with_overflow (r, type,
2645 wi::mask (rprec, true, type_prec),
2646 wi::mask (rprec, false, type_prec));
2647 return true;
2651 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2652 // possible. Basically, see if we can optimize:
2654 // [LB, UB] op Z
2655 // into:
2656 // [LB op Z, UB op Z]
2658 // If the optimization was successful, accumulate the range in R and
2659 // return TRUE.
2661 static bool
2662 wi_optimize_and_or (irange &r,
2663 enum tree_code code,
2664 tree type,
2665 const wide_int &lh_lb, const wide_int &lh_ub,
2666 const wide_int &rh_lb, const wide_int &rh_ub)
2668 // Calculate the singleton mask among the ranges, if any.
2669 wide_int lower_bound, upper_bound, mask;
2670 if (wi::eq_p (rh_lb, rh_ub))
2672 mask = rh_lb;
2673 lower_bound = lh_lb;
2674 upper_bound = lh_ub;
2676 else if (wi::eq_p (lh_lb, lh_ub))
2678 mask = lh_lb;
2679 lower_bound = rh_lb;
2680 upper_bound = rh_ub;
2682 else
2683 return false;
2685 // If Z is a constant which (for op | its bitwise not) has n
2686 // consecutive least significant bits cleared followed by m 1
2687 // consecutive bits set immediately above it and either
2688 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2690 // The least significant n bits of all the values in the range are
2691 // cleared or set, the m bits above it are preserved and any bits
2692 // above these are required to be the same for all values in the
2693 // range.
2694 wide_int w = mask;
2695 int m = 0, n = 0;
2696 if (code == BIT_IOR_EXPR)
2697 w = ~w;
2698 if (wi::eq_p (w, 0))
2699 n = w.get_precision ();
2700 else
2702 n = wi::ctz (w);
2703 w = ~(w | wi::mask (n, false, w.get_precision ()));
2704 if (wi::eq_p (w, 0))
2705 m = w.get_precision () - n;
2706 else
2707 m = wi::ctz (w) - n;
2709 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2710 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2711 return false;
2713 wide_int res_lb, res_ub;
2714 if (code == BIT_AND_EXPR)
2716 res_lb = wi::bit_and (lower_bound, mask);
2717 res_ub = wi::bit_and (upper_bound, mask);
2719 else if (code == BIT_IOR_EXPR)
2721 res_lb = wi::bit_or (lower_bound, mask);
2722 res_ub = wi::bit_or (upper_bound, mask);
2724 else
2725 gcc_unreachable ();
2726 value_range_with_overflow (r, type, res_lb, res_ub);
2728 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2729 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2731 int_range<2> tmp;
2732 tmp.set_nonzero (type);
2733 r.intersect (tmp);
2735 return true;
2738 // For range [LB, UB] compute two wide_int bit masks.
2740 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2741 // for all numbers in the range the bit is 0, otherwise it might be 0
2742 // or 1.
2744 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2745 // for all numbers in the range the bit is 1, otherwise it might be 0
2746 // or 1.
2748 void
2749 wi_set_zero_nonzero_bits (tree type,
2750 const wide_int &lb, const wide_int &ub,
2751 wide_int &maybe_nonzero,
2752 wide_int &mustbe_nonzero)
2754 signop sign = TYPE_SIGN (type);
2756 if (wi::eq_p (lb, ub))
2757 maybe_nonzero = mustbe_nonzero = lb;
2758 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2760 wide_int xor_mask = lb ^ ub;
2761 maybe_nonzero = lb | ub;
2762 mustbe_nonzero = lb & ub;
2763 if (xor_mask != 0)
2765 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2766 maybe_nonzero.get_precision ());
2767 maybe_nonzero = maybe_nonzero | mask;
2768 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2771 else
2773 maybe_nonzero = wi::minus_one (lb.get_precision ());
2774 mustbe_nonzero = wi::zero (lb.get_precision ());
2778 void
2779 operator_bitwise_and::wi_fold (irange &r, tree type,
2780 const wide_int &lh_lb,
2781 const wide_int &lh_ub,
2782 const wide_int &rh_lb,
2783 const wide_int &rh_ub) const
2785 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2786 return;
2788 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2789 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2790 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2791 maybe_nonzero_lh, mustbe_nonzero_lh);
2792 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2793 maybe_nonzero_rh, mustbe_nonzero_rh);
2795 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2796 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2797 signop sign = TYPE_SIGN (type);
2798 unsigned prec = TYPE_PRECISION (type);
2799 // If both input ranges contain only negative values, we can
2800 // truncate the result range maximum to the minimum of the
2801 // input range maxima.
2802 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2804 new_ub = wi::min (new_ub, lh_ub, sign);
2805 new_ub = wi::min (new_ub, rh_ub, sign);
2807 // If either input range contains only non-negative values
2808 // we can truncate the result range maximum to the respective
2809 // maximum of the input range.
2810 if (wi::ge_p (lh_lb, 0, sign))
2811 new_ub = wi::min (new_ub, lh_ub, sign);
2812 if (wi::ge_p (rh_lb, 0, sign))
2813 new_ub = wi::min (new_ub, rh_ub, sign);
2814 // PR68217: In case of signed & sign-bit-CST should
2815 // result in [-INF, 0] instead of [-INF, INF].
2816 if (wi::gt_p (new_lb, new_ub, sign))
2818 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2819 if (sign == SIGNED
2820 && ((wi::eq_p (lh_lb, lh_ub)
2821 && !wi::cmps (lh_lb, sign_bit))
2822 || (wi::eq_p (rh_lb, rh_ub)
2823 && !wi::cmps (rh_lb, sign_bit))))
2825 new_lb = wi::min_value (prec, sign);
2826 new_ub = wi::zero (prec);
2829 // If the limits got swapped around, return varying.
2830 if (wi::gt_p (new_lb, new_ub,sign))
2832 if (sign == SIGNED
2833 && wi_optimize_signed_bitwise_op (r, type,
2834 lh_lb, lh_ub,
2835 rh_lb, rh_ub))
2836 return;
2837 r.set_varying (type);
2839 else
2840 value_range_with_overflow (r, type, new_lb, new_ub);
2843 static void
2844 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2846 if (!lhs.contains_p (build_zero_cst (type)))
2847 r = range_nonzero (type);
2848 else
2849 r.set_varying (type);
2852 // This was shamelessly stolen from register_edge_assert_for_2 and
2853 // adjusted to work with iranges.
2855 void
2856 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2857 const irange &lhs,
2858 const irange &op2) const
2860 if (!op2.singleton_p ())
2862 set_nonzero_range_from_mask (r, type, lhs);
2863 return;
2865 unsigned int nprec = TYPE_PRECISION (type);
2866 wide_int cst2v = op2.lower_bound ();
2867 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2868 wide_int sgnbit;
2869 if (cst2n)
2870 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2871 else
2872 sgnbit = wi::zero (nprec);
2874 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2876 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2877 // maximum unsigned value is ~0. For signed comparison, if CST2
2878 // doesn't have the most significant bit set, handle it similarly. If
2879 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2880 wide_int valv = lhs.lower_bound ();
2881 wide_int minv = valv & cst2v, maxv;
2882 bool we_know_nothing = false;
2883 if (minv != valv)
2885 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2886 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2887 if (minv == valv)
2889 // If we can't determine anything on this bound, fall
2890 // through and conservatively solve for the other end point.
2891 we_know_nothing = true;
2894 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2895 if (we_know_nothing)
2896 r.set_varying (type);
2897 else
2898 r = int_range<1> (type, minv, maxv);
2900 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2902 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2903 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2904 // VAL2 where
2905 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2906 // as maximum.
2907 // For signed comparison, if CST2 doesn't have most significant bit
2908 // set, handle it similarly. If CST2 has MSB set, the maximum is
2909 // the same and minimum is INT_MIN.
2910 valv = lhs.upper_bound ();
2911 minv = valv & cst2v;
2912 if (minv == valv)
2913 maxv = valv;
2914 else
2916 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2917 if (maxv == valv)
2919 // If we couldn't determine anything on either bound, return
2920 // undefined.
2921 if (we_know_nothing)
2922 r.set_undefined ();
2923 return;
2925 maxv -= 1;
2927 maxv |= ~cst2v;
2928 minv = sgnbit;
2929 int_range<1> upper_bits (type, minv, maxv);
2930 r.intersect (upper_bits);
2933 bool
2934 operator_bitwise_and::op1_range (irange &r, tree type,
2935 const irange &lhs,
2936 const irange &op2,
2937 relation_kind rel ATTRIBUTE_UNUSED) const
2939 if (types_compatible_p (type, boolean_type_node))
2940 return op_logical_and.op1_range (r, type, lhs, op2);
2942 r.set_undefined ();
2943 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
2945 int_range_max chunk (lhs.type (),
2946 lhs.lower_bound (i),
2947 lhs.upper_bound (i));
2948 int_range_max res;
2949 simple_op1_range_solver (res, type, chunk, op2);
2950 r.union_ (res);
2952 if (r.undefined_p ())
2953 set_nonzero_range_from_mask (r, type, lhs);
2954 return true;
2957 bool
2958 operator_bitwise_and::op2_range (irange &r, tree type,
2959 const irange &lhs,
2960 const irange &op1,
2961 relation_kind rel ATTRIBUTE_UNUSED) const
2963 return operator_bitwise_and::op1_range (r, type, lhs, op1);
2967 class operator_logical_or : public range_operator
2969 using range_operator::fold_range;
2970 using range_operator::op1_range;
2971 using range_operator::op2_range;
2972 public:
2973 virtual bool fold_range (irange &r, tree type,
2974 const irange &lh,
2975 const irange &rh,
2976 relation_kind rel = VREL_VARYING) const;
2977 virtual bool op1_range (irange &r, tree type,
2978 const irange &lhs,
2979 const irange &op2,
2980 relation_kind rel = VREL_VARYING) const;
2981 virtual bool op2_range (irange &r, tree type,
2982 const irange &lhs,
2983 const irange &op1,
2984 relation_kind rel = VREL_VARYING) const;
2985 } op_logical_or;
2987 bool
2988 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2989 const irange &lh,
2990 const irange &rh,
2991 relation_kind rel ATTRIBUTE_UNUSED) const
2993 if (empty_range_varying (r, type, lh, rh))
2994 return true;
2996 r = lh;
2997 r.union_ (rh);
2998 return true;
3001 bool
3002 operator_logical_or::op1_range (irange &r, tree type,
3003 const irange &lhs,
3004 const irange &op2 ATTRIBUTE_UNUSED,
3005 relation_kind rel ATTRIBUTE_UNUSED) const
3007 switch (get_bool_state (r, lhs, type))
3009 case BRS_FALSE:
3010 // A false result means both sides of the OR must be false.
3011 r = range_false (type);
3012 break;
3013 default:
3014 // Any other result means only one side has to be true, the
3015 // other side can be anything. so we can't be sure of any result
3016 // here.
3017 r = range_true_and_false (type);
3018 break;
3020 return true;
3023 bool
3024 operator_logical_or::op2_range (irange &r, tree type,
3025 const irange &lhs,
3026 const irange &op1,
3027 relation_kind rel ATTRIBUTE_UNUSED) const
3029 return operator_logical_or::op1_range (r, type, lhs, op1);
3033 class operator_bitwise_or : public range_operator
3035 using range_operator::op1_range;
3036 using range_operator::op2_range;
3037 public:
3038 virtual bool op1_range (irange &r, tree type,
3039 const irange &lhs,
3040 const irange &op2,
3041 relation_kind rel = VREL_VARYING) const;
3042 virtual bool op2_range (irange &r, tree type,
3043 const irange &lhs,
3044 const irange &op1,
3045 relation_kind rel= VREL_VARYING) const;
3046 virtual void wi_fold (irange &r, tree type,
3047 const wide_int &lh_lb,
3048 const wide_int &lh_ub,
3049 const wide_int &rh_lb,
3050 const wide_int &rh_ub) const;
3051 } op_bitwise_or;
3053 void
3054 operator_bitwise_or::wi_fold (irange &r, tree type,
3055 const wide_int &lh_lb,
3056 const wide_int &lh_ub,
3057 const wide_int &rh_lb,
3058 const wide_int &rh_ub) const
3060 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3061 return;
3063 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3064 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3065 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3066 maybe_nonzero_lh, mustbe_nonzero_lh);
3067 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3068 maybe_nonzero_rh, mustbe_nonzero_rh);
3069 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3070 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3071 signop sign = TYPE_SIGN (type);
3072 // If the input ranges contain only positive values we can
3073 // truncate the minimum of the result range to the maximum
3074 // of the input range minima.
3075 if (wi::ge_p (lh_lb, 0, sign)
3076 && wi::ge_p (rh_lb, 0, sign))
3078 new_lb = wi::max (new_lb, lh_lb, sign);
3079 new_lb = wi::max (new_lb, rh_lb, sign);
3081 // If either input range contains only negative values
3082 // we can truncate the minimum of the result range to the
3083 // respective minimum range.
3084 if (wi::lt_p (lh_ub, 0, sign))
3085 new_lb = wi::max (new_lb, lh_lb, sign);
3086 if (wi::lt_p (rh_ub, 0, sign))
3087 new_lb = wi::max (new_lb, rh_lb, sign);
3088 // If the limits got swapped around, return a conservative range.
3089 if (wi::gt_p (new_lb, new_ub, sign))
3091 // Make sure that nonzero|X is nonzero.
3092 if (wi::gt_p (lh_lb, 0, sign)
3093 || wi::gt_p (rh_lb, 0, sign)
3094 || wi::lt_p (lh_ub, 0, sign)
3095 || wi::lt_p (rh_ub, 0, sign))
3096 r.set_nonzero (type);
3097 else if (sign == SIGNED
3098 && wi_optimize_signed_bitwise_op (r, type,
3099 lh_lb, lh_ub,
3100 rh_lb, rh_ub))
3101 return;
3102 else
3103 r.set_varying (type);
3104 return;
3106 value_range_with_overflow (r, type, new_lb, new_ub);
3109 bool
3110 operator_bitwise_or::op1_range (irange &r, tree type,
3111 const irange &lhs,
3112 const irange &op2,
3113 relation_kind rel ATTRIBUTE_UNUSED) const
3115 // If this is really a logical wi_fold, call that.
3116 if (types_compatible_p (type, boolean_type_node))
3117 return op_logical_or.op1_range (r, type, lhs, op2);
3119 if (lhs.zero_p ())
3121 tree zero = build_zero_cst (type);
3122 r = int_range<1> (zero, zero);
3123 return true;
3125 r.set_varying (type);
3126 return true;
3129 bool
3130 operator_bitwise_or::op2_range (irange &r, tree type,
3131 const irange &lhs,
3132 const irange &op1,
3133 relation_kind rel ATTRIBUTE_UNUSED) const
3135 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3139 class operator_bitwise_xor : public range_operator
3141 using range_operator::op1_range;
3142 using range_operator::op2_range;
3143 public:
3144 virtual void wi_fold (irange &r, tree type,
3145 const wide_int &lh_lb,
3146 const wide_int &lh_ub,
3147 const wide_int &rh_lb,
3148 const wide_int &rh_ub) const;
3149 virtual bool op1_range (irange &r, tree type,
3150 const irange &lhs,
3151 const irange &op2,
3152 relation_kind rel = VREL_VARYING) const;
3153 virtual bool op2_range (irange &r, tree type,
3154 const irange &lhs,
3155 const irange &op1,
3156 relation_kind rel = VREL_VARYING) const;
3157 virtual bool op1_op2_relation_effect (irange &lhs_range,
3158 tree type,
3159 const irange &op1_range,
3160 const irange &op2_range,
3161 relation_kind rel) const;
3162 } op_bitwise_xor;
3164 void
3165 operator_bitwise_xor::wi_fold (irange &r, tree type,
3166 const wide_int &lh_lb,
3167 const wide_int &lh_ub,
3168 const wide_int &rh_lb,
3169 const wide_int &rh_ub) const
3171 signop sign = TYPE_SIGN (type);
3172 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3173 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3174 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3175 maybe_nonzero_lh, mustbe_nonzero_lh);
3176 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3177 maybe_nonzero_rh, mustbe_nonzero_rh);
3179 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3180 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3181 wide_int result_one_bits
3182 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3183 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3184 wide_int new_ub = ~result_zero_bits;
3185 wide_int new_lb = result_one_bits;
3187 // If the range has all positive or all negative values, the result
3188 // is better than VARYING.
3189 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3190 value_range_with_overflow (r, type, new_lb, new_ub);
3191 else if (sign == SIGNED
3192 && wi_optimize_signed_bitwise_op (r, type,
3193 lh_lb, lh_ub,
3194 rh_lb, rh_ub))
3195 ; /* Do nothing. */
3196 else
3197 r.set_varying (type);
3199 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3200 if (wi::lt_p (lh_ub, rh_lb, sign)
3201 || wi::lt_p (rh_ub, lh_lb, sign)
3202 || wi::ne_p (result_one_bits, 0))
3204 int_range<2> tmp;
3205 tmp.set_nonzero (type);
3206 r.intersect (tmp);
3210 bool
3211 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3212 tree type,
3213 const irange &,
3214 const irange &,
3215 relation_kind rel) const
3217 if (rel == VREL_VARYING)
3218 return false;
3220 int_range<2> rel_range;
3222 switch (rel)
3224 case VREL_EQ:
3225 rel_range.set_zero (type);
3226 break;
3227 case VREL_NE:
3228 rel_range.set_nonzero (type);
3229 break;
3230 default:
3231 return false;
3234 lhs_range.intersect (rel_range);
3235 return true;
3238 bool
3239 operator_bitwise_xor::op1_range (irange &r, tree type,
3240 const irange &lhs,
3241 const irange &op2,
3242 relation_kind rel ATTRIBUTE_UNUSED) const
3244 if (lhs.undefined_p () || lhs.varying_p ())
3246 r = lhs;
3247 return true;
3249 if (types_compatible_p (type, boolean_type_node))
3251 switch (get_bool_state (r, lhs, type))
3253 case BRS_TRUE:
3254 if (op2.varying_p ())
3255 r.set_varying (type);
3256 else if (op2.zero_p ())
3257 r = range_true (type);
3258 else
3259 r = range_false (type);
3260 break;
3261 case BRS_FALSE:
3262 r = op2;
3263 break;
3264 default:
3265 break;
3267 return true;
3269 r.set_varying (type);
3270 return true;
3273 bool
3274 operator_bitwise_xor::op2_range (irange &r, tree type,
3275 const irange &lhs,
3276 const irange &op1,
3277 relation_kind rel ATTRIBUTE_UNUSED) const
3279 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3282 class operator_trunc_mod : public range_operator
3284 using range_operator::op1_range;
3285 using range_operator::op2_range;
3286 public:
3287 virtual void wi_fold (irange &r, tree type,
3288 const wide_int &lh_lb,
3289 const wide_int &lh_ub,
3290 const wide_int &rh_lb,
3291 const wide_int &rh_ub) const;
3292 virtual bool op1_range (irange &r, tree type,
3293 const irange &lhs,
3294 const irange &op2,
3295 relation_kind rel ATTRIBUTE_UNUSED) const;
3296 virtual bool op2_range (irange &r, tree type,
3297 const irange &lhs,
3298 const irange &op1,
3299 relation_kind rel ATTRIBUTE_UNUSED) const;
3300 } op_trunc_mod;
3302 void
3303 operator_trunc_mod::wi_fold (irange &r, tree type,
3304 const wide_int &lh_lb,
3305 const wide_int &lh_ub,
3306 const wide_int &rh_lb,
3307 const wide_int &rh_ub) const
3309 wide_int new_lb, new_ub, tmp;
3310 signop sign = TYPE_SIGN (type);
3311 unsigned prec = TYPE_PRECISION (type);
3313 // Mod 0 is undefined.
3314 if (wi_zero_p (type, rh_lb, rh_ub))
3316 r.set_undefined ();
3317 return;
3320 // Check for constant and try to fold.
3321 if (lh_lb == lh_ub && rh_lb == rh_ub)
3323 wi::overflow_type ov = wi::OVF_NONE;
3324 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3325 if (ov == wi::OVF_NONE)
3327 r = int_range<2> (type, tmp, tmp);
3328 return;
3332 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3333 new_ub = rh_ub - 1;
3334 if (sign == SIGNED)
3336 tmp = -1 - rh_lb;
3337 new_ub = wi::smax (new_ub, tmp);
3340 if (sign == UNSIGNED)
3341 new_lb = wi::zero (prec);
3342 else
3344 new_lb = -new_ub;
3345 tmp = lh_lb;
3346 if (wi::gts_p (tmp, 0))
3347 tmp = wi::zero (prec);
3348 new_lb = wi::smax (new_lb, tmp);
3350 tmp = lh_ub;
3351 if (sign == SIGNED && wi::neg_p (tmp))
3352 tmp = wi::zero (prec);
3353 new_ub = wi::min (new_ub, tmp, sign);
3355 value_range_with_overflow (r, type, new_lb, new_ub);
3358 bool
3359 operator_trunc_mod::op1_range (irange &r, tree type,
3360 const irange &lhs,
3361 const irange &,
3362 relation_kind rel ATTRIBUTE_UNUSED) const
3364 // PR 91029.
3365 signop sign = TYPE_SIGN (type);
3366 unsigned prec = TYPE_PRECISION (type);
3367 // (a % b) >= x && x > 0 , then a >= x.
3368 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3370 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3371 return true;
3373 // (a % b) <= x && x < 0 , then a <= x.
3374 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3376 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3377 return true;
3379 return false;
3382 bool
3383 operator_trunc_mod::op2_range (irange &r, tree type,
3384 const irange &lhs,
3385 const irange &,
3386 relation_kind rel ATTRIBUTE_UNUSED) const
3388 // PR 91029.
3389 signop sign = TYPE_SIGN (type);
3390 unsigned prec = TYPE_PRECISION (type);
3391 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3392 // or b > x for unsigned.
3393 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3395 if (sign == SIGNED)
3396 r = value_range (type, wi::neg (lhs.lower_bound ()),
3397 lhs.lower_bound (), VR_ANTI_RANGE);
3398 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3399 sign))
3400 r = value_range (type, lhs.lower_bound () + 1,
3401 wi::max_value (prec, sign));
3402 else
3403 return false;
3404 return true;
3406 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3407 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3409 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3410 r = value_range (type, lhs.upper_bound (),
3411 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3412 else
3413 return false;
3414 return true;
3416 return false;
3420 class operator_logical_not : public range_operator
3422 using range_operator::fold_range;
3423 using range_operator::op1_range;
3424 public:
3425 virtual bool fold_range (irange &r, tree type,
3426 const irange &lh,
3427 const irange &rh,
3428 relation_kind rel = VREL_VARYING) const;
3429 virtual bool op1_range (irange &r, tree type,
3430 const irange &lhs,
3431 const irange &op2,
3432 relation_kind rel = VREL_VARYING) const;
3433 } op_logical_not;
3435 // Folding a logical NOT, oddly enough, involves doing nothing on the
3436 // forward pass through. During the initial walk backwards, the
3437 // logical NOT reversed the desired outcome on the way back, so on the
3438 // way forward all we do is pass the range forward.
3440 // b_2 = x_1 < 20
3441 // b_3 = !b_2
3442 // if (b_3)
3443 // to determine the TRUE branch, walking backward
3444 // if (b_3) if ([1,1])
3445 // b_3 = !b_2 [1,1] = ![0,0]
3446 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3447 // which is the result we are looking for.. so.. pass it through.
3449 bool
3450 operator_logical_not::fold_range (irange &r, tree type,
3451 const irange &lh,
3452 const irange &rh ATTRIBUTE_UNUSED,
3453 relation_kind rel ATTRIBUTE_UNUSED) const
3455 if (empty_range_varying (r, type, lh, rh))
3456 return true;
3458 r = lh;
3459 if (!lh.varying_p () && !lh.undefined_p ())
3460 r.invert ();
3462 return true;
3465 bool
3466 operator_logical_not::op1_range (irange &r,
3467 tree type,
3468 const irange &lhs,
3469 const irange &op2,
3470 relation_kind rel ATTRIBUTE_UNUSED) const
3472 // Logical NOT is involutary...do it again.
3473 return fold_range (r, type, lhs, op2);
3477 class operator_bitwise_not : public range_operator
3479 using range_operator::fold_range;
3480 using range_operator::op1_range;
3481 public:
3482 virtual bool fold_range (irange &r, tree type,
3483 const irange &lh,
3484 const irange &rh,
3485 relation_kind rel = VREL_VARYING) const;
3486 virtual bool op1_range (irange &r, tree type,
3487 const irange &lhs,
3488 const irange &op2,
3489 relation_kind rel = VREL_VARYING) const;
3490 } op_bitwise_not;
3492 bool
3493 operator_bitwise_not::fold_range (irange &r, tree type,
3494 const irange &lh,
3495 const irange &rh,
3496 relation_kind rel ATTRIBUTE_UNUSED) const
3498 if (empty_range_varying (r, type, lh, rh))
3499 return true;
3501 if (types_compatible_p (type, boolean_type_node))
3502 return op_logical_not.fold_range (r, type, lh, rh);
3504 // ~X is simply -1 - X.
3505 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3506 wi::minus_one (TYPE_PRECISION (type)));
3507 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, minusone, lh);
3510 bool
3511 operator_bitwise_not::op1_range (irange &r, tree type,
3512 const irange &lhs,
3513 const irange &op2,
3514 relation_kind rel ATTRIBUTE_UNUSED) const
3516 if (types_compatible_p (type, boolean_type_node))
3517 return op_logical_not.op1_range (r, type, lhs, op2);
3519 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3520 return fold_range (r, type, lhs, op2);
3524 class operator_cst : public range_operator
3526 using range_operator::fold_range;
3527 public:
3528 virtual bool fold_range (irange &r, tree type,
3529 const irange &op1,
3530 const irange &op2,
3531 relation_kind rel = VREL_VARYING) const;
3532 } op_integer_cst;
3534 bool
3535 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3536 const irange &lh,
3537 const irange &rh ATTRIBUTE_UNUSED,
3538 relation_kind rel ATTRIBUTE_UNUSED) const
3540 r = lh;
3541 return true;
3545 class operator_identity : public range_operator
3547 using range_operator::fold_range;
3548 using range_operator::op1_range;
3549 using range_operator::lhs_op1_relation;
3550 public:
3551 virtual bool fold_range (irange &r, tree type,
3552 const irange &op1,
3553 const irange &op2,
3554 relation_kind rel = VREL_VARYING) const;
3555 virtual bool op1_range (irange &r, tree type,
3556 const irange &lhs,
3557 const irange &op2,
3558 relation_kind rel = VREL_VARYING) const;
3559 virtual relation_kind lhs_op1_relation (const irange &lhs,
3560 const irange &op1,
3561 const irange &op2,
3562 relation_kind rel) const;
3563 } op_identity;
3565 // Determine if there is a relationship between LHS and OP1.
3567 relation_kind
3568 operator_identity::lhs_op1_relation (const irange &lhs,
3569 const irange &op1 ATTRIBUTE_UNUSED,
3570 const irange &op2 ATTRIBUTE_UNUSED,
3571 relation_kind) const
3573 if (lhs.undefined_p ())
3574 return VREL_VARYING;
3575 // Simply a copy, so they are equivalent.
3576 return VREL_EQ;
3579 bool
3580 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3581 const irange &lh,
3582 const irange &rh ATTRIBUTE_UNUSED,
3583 relation_kind rel ATTRIBUTE_UNUSED) const
3585 r = lh;
3586 return true;
3589 bool
3590 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3591 const irange &lhs,
3592 const irange &op2 ATTRIBUTE_UNUSED,
3593 relation_kind rel ATTRIBUTE_UNUSED) const
3595 r = lhs;
3596 return true;
3600 class operator_unknown : public range_operator
3602 using range_operator::fold_range;
3603 public:
3604 virtual bool fold_range (irange &r, tree type,
3605 const irange &op1,
3606 const irange &op2,
3607 relation_kind rel = VREL_VARYING) const;
3608 } op_unknown;
3610 bool
3611 operator_unknown::fold_range (irange &r, tree type,
3612 const irange &lh ATTRIBUTE_UNUSED,
3613 const irange &rh ATTRIBUTE_UNUSED,
3614 relation_kind rel ATTRIBUTE_UNUSED) const
3616 r.set_varying (type);
3617 return true;
3621 class operator_abs : public range_operator
3623 using range_operator::op1_range;
3624 public:
3625 virtual void wi_fold (irange &r, tree type,
3626 const wide_int &lh_lb,
3627 const wide_int &lh_ub,
3628 const wide_int &rh_lb,
3629 const wide_int &rh_ub) const;
3630 virtual bool op1_range (irange &r, tree type,
3631 const irange &lhs,
3632 const irange &op2,
3633 relation_kind rel ATTRIBUTE_UNUSED) const;
3634 } op_abs;
3636 void
3637 operator_abs::wi_fold (irange &r, tree type,
3638 const wide_int &lh_lb, const wide_int &lh_ub,
3639 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3640 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3642 wide_int min, max;
3643 signop sign = TYPE_SIGN (type);
3644 unsigned prec = TYPE_PRECISION (type);
3646 // Pass through LH for the easy cases.
3647 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3649 r = int_range<1> (type, lh_lb, lh_ub);
3650 return;
3653 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3654 // a useful range.
3655 wide_int min_value = wi::min_value (prec, sign);
3656 wide_int max_value = wi::max_value (prec, sign);
3657 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3659 r.set_varying (type);
3660 return;
3663 // ABS_EXPR may flip the range around, if the original range
3664 // included negative values.
3665 if (wi::eq_p (lh_lb, min_value))
3667 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3668 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3669 if (wi::eq_p (lh_ub, min_value))
3671 r = int_range<1> (type, min_value, min_value);
3672 return;
3674 min = max_value;
3676 else
3677 min = wi::abs (lh_lb);
3679 if (wi::eq_p (lh_ub, min_value))
3680 max = max_value;
3681 else
3682 max = wi::abs (lh_ub);
3684 // If the range contains zero then we know that the minimum value in the
3685 // range will be zero.
3686 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3688 if (wi::gt_p (min, max, sign))
3689 max = min;
3690 min = wi::zero (prec);
3692 else
3694 // If the range was reversed, swap MIN and MAX.
3695 if (wi::gt_p (min, max, sign))
3696 std::swap (min, max);
3699 // If the new range has its limits swapped around (MIN > MAX), then
3700 // the operation caused one of them to wrap around. The only thing
3701 // we know is that the result is positive.
3702 if (wi::gt_p (min, max, sign))
3704 min = wi::zero (prec);
3705 max = max_value;
3707 r = int_range<1> (type, min, max);
3710 bool
3711 operator_abs::op1_range (irange &r, tree type,
3712 const irange &lhs,
3713 const irange &op2,
3714 relation_kind rel ATTRIBUTE_UNUSED) const
3716 if (empty_range_varying (r, type, lhs, op2))
3717 return true;
3718 if (TYPE_UNSIGNED (type))
3720 r = lhs;
3721 return true;
3723 // Start with the positives because negatives are an impossible result.
3724 int_range_max positives = range_positives (type);
3725 positives.intersect (lhs);
3726 r = positives;
3727 // Then add the negative of each pair:
3728 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3729 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3730 r.union_ (int_range<1> (type,
3731 -positives.upper_bound (i),
3732 -positives.lower_bound (i)));
3733 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3734 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3735 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3736 wide_int lb = lhs.lower_bound ();
3737 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3738 r.union_ (int_range<2> (type, lb, lb));
3739 return true;
3743 class operator_absu : public range_operator
3745 public:
3746 virtual void wi_fold (irange &r, tree type,
3747 const wide_int &lh_lb, const wide_int &lh_ub,
3748 const wide_int &rh_lb, const wide_int &rh_ub) const;
3749 } op_absu;
3751 void
3752 operator_absu::wi_fold (irange &r, tree type,
3753 const wide_int &lh_lb, const wide_int &lh_ub,
3754 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3755 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3757 wide_int new_lb, new_ub;
3759 // Pass through VR0 the easy cases.
3760 if (wi::ges_p (lh_lb, 0))
3762 new_lb = lh_lb;
3763 new_ub = lh_ub;
3765 else
3767 new_lb = wi::abs (lh_lb);
3768 new_ub = wi::abs (lh_ub);
3770 // If the range contains zero then we know that the minimum
3771 // value in the range will be zero.
3772 if (wi::ges_p (lh_ub, 0))
3774 if (wi::gtu_p (new_lb, new_ub))
3775 new_ub = new_lb;
3776 new_lb = wi::zero (TYPE_PRECISION (type));
3778 else
3779 std::swap (new_lb, new_ub);
3782 gcc_checking_assert (TYPE_UNSIGNED (type));
3783 r = int_range<1> (type, new_lb, new_ub);
3787 class operator_negate : public range_operator
3789 using range_operator::fold_range;
3790 using range_operator::op1_range;
3791 public:
3792 virtual bool fold_range (irange &r, tree type,
3793 const irange &op1,
3794 const irange &op2,
3795 relation_kind rel = VREL_VARYING) const;
3796 virtual bool op1_range (irange &r, tree type,
3797 const irange &lhs,
3798 const irange &op2,
3799 relation_kind rel = VREL_VARYING) const;
3800 } op_negate;
3802 bool
3803 operator_negate::fold_range (irange &r, tree type,
3804 const irange &lh,
3805 const irange &rh,
3806 relation_kind rel ATTRIBUTE_UNUSED) const
3808 if (empty_range_varying (r, type, lh, rh))
3809 return true;
3810 // -X is simply 0 - X.
3811 return range_op_handler (MINUS_EXPR, type).fold_range (r, type,
3812 range_zero (type), lh);
3815 bool
3816 operator_negate::op1_range (irange &r, tree type,
3817 const irange &lhs,
3818 const irange &op2,
3819 relation_kind rel ATTRIBUTE_UNUSED) const
3821 // NEGATE is involutory.
3822 return fold_range (r, type, lhs, op2);
3826 class operator_addr_expr : public range_operator
3828 using range_operator::fold_range;
3829 using range_operator::op1_range;
3830 public:
3831 virtual bool fold_range (irange &r, tree type,
3832 const irange &op1,
3833 const irange &op2,
3834 relation_kind rel = VREL_VARYING) const;
3835 virtual bool op1_range (irange &r, tree type,
3836 const irange &lhs,
3837 const irange &op2,
3838 relation_kind rel = VREL_VARYING) const;
3839 } op_addr;
3841 bool
3842 operator_addr_expr::fold_range (irange &r, tree type,
3843 const irange &lh,
3844 const irange &rh,
3845 relation_kind rel ATTRIBUTE_UNUSED) const
3847 if (empty_range_varying (r, type, lh, rh))
3848 return true;
3850 // Return a non-null pointer of the LHS type (passed in op2).
3851 if (lh.zero_p ())
3852 r = range_zero (type);
3853 else if (!lh.contains_p (build_zero_cst (lh.type ())))
3854 r = range_nonzero (type);
3855 else
3856 r.set_varying (type);
3857 return true;
3860 bool
3861 operator_addr_expr::op1_range (irange &r, tree type,
3862 const irange &lhs,
3863 const irange &op2,
3864 relation_kind rel ATTRIBUTE_UNUSED) const
3866 return operator_addr_expr::fold_range (r, type, lhs, op2);
3870 class pointer_plus_operator : public range_operator
3872 public:
3873 virtual void wi_fold (irange &r, tree type,
3874 const wide_int &lh_lb,
3875 const wide_int &lh_ub,
3876 const wide_int &rh_lb,
3877 const wide_int &rh_ub) const;
3878 } op_pointer_plus;
3880 void
3881 pointer_plus_operator::wi_fold (irange &r, tree type,
3882 const wide_int &lh_lb,
3883 const wide_int &lh_ub,
3884 const wide_int &rh_lb,
3885 const wide_int &rh_ub) const
3887 // Check for [0,0] + const, and simply return the const.
3888 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3890 tree val = wide_int_to_tree (type, rh_lb);
3891 r.set (val, val);
3892 return;
3895 // For pointer types, we are really only interested in asserting
3896 // whether the expression evaluates to non-NULL.
3898 // With -fno-delete-null-pointer-checks we need to be more
3899 // conservative. As some object might reside at address 0,
3900 // then some offset could be added to it and the same offset
3901 // subtracted again and the result would be NULL.
3902 // E.g.
3903 // static int a[12]; where &a[0] is NULL and
3904 // ptr = &a[6];
3905 // ptr -= 6;
3906 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3907 // where the first range doesn't include zero and the second one
3908 // doesn't either. As the second operand is sizetype (unsigned),
3909 // consider all ranges where the MSB could be set as possible
3910 // subtractions where the result might be NULL.
3911 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3912 || !wi_includes_zero_p (type, rh_lb, rh_ub))
3913 && !TYPE_OVERFLOW_WRAPS (type)
3914 && (flag_delete_null_pointer_checks
3915 || !wi::sign_mask (rh_ub)))
3916 r = range_nonzero (type);
3917 else if (lh_lb == lh_ub && lh_lb == 0
3918 && rh_lb == rh_ub && rh_lb == 0)
3919 r = range_zero (type);
3920 else
3921 r.set_varying (type);
3925 class pointer_min_max_operator : public range_operator
3927 public:
3928 virtual void wi_fold (irange & r, tree type,
3929 const wide_int &lh_lb, const wide_int &lh_ub,
3930 const wide_int &rh_lb, const wide_int &rh_ub) const;
3931 } op_ptr_min_max;
3933 void
3934 pointer_min_max_operator::wi_fold (irange &r, tree type,
3935 const wide_int &lh_lb,
3936 const wide_int &lh_ub,
3937 const wide_int &rh_lb,
3938 const wide_int &rh_ub) const
3940 // For MIN/MAX expressions with pointers, we only care about
3941 // nullness. If both are non null, then the result is nonnull.
3942 // If both are null, then the result is null. Otherwise they
3943 // are varying.
3944 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3945 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3946 r = range_nonzero (type);
3947 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3948 r = range_zero (type);
3949 else
3950 r.set_varying (type);
3954 class pointer_and_operator : public range_operator
3956 public:
3957 virtual void wi_fold (irange &r, tree type,
3958 const wide_int &lh_lb, const wide_int &lh_ub,
3959 const wide_int &rh_lb, const wide_int &rh_ub) const;
3960 } op_pointer_and;
3962 void
3963 pointer_and_operator::wi_fold (irange &r, tree type,
3964 const wide_int &lh_lb,
3965 const wide_int &lh_ub,
3966 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3967 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3969 // For pointer types, we are really only interested in asserting
3970 // whether the expression evaluates to non-NULL.
3971 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
3972 r = range_zero (type);
3973 else
3974 r.set_varying (type);
3978 class pointer_or_operator : public range_operator
3980 using range_operator::op1_range;
3981 using range_operator::op2_range;
3982 public:
3983 virtual bool op1_range (irange &r, tree type,
3984 const irange &lhs,
3985 const irange &op2,
3986 relation_kind rel = VREL_VARYING) const;
3987 virtual bool op2_range (irange &r, tree type,
3988 const irange &lhs,
3989 const irange &op1,
3990 relation_kind rel = VREL_VARYING) const;
3991 virtual void wi_fold (irange &r, tree type,
3992 const wide_int &lh_lb, const wide_int &lh_ub,
3993 const wide_int &rh_lb, const wide_int &rh_ub) const;
3994 } op_pointer_or;
3996 bool
3997 pointer_or_operator::op1_range (irange &r, tree type,
3998 const irange &lhs,
3999 const irange &op2 ATTRIBUTE_UNUSED,
4000 relation_kind rel ATTRIBUTE_UNUSED) const
4002 if (lhs.zero_p ())
4004 tree zero = build_zero_cst (type);
4005 r = int_range<1> (zero, zero);
4006 return true;
4008 r.set_varying (type);
4009 return true;
4012 bool
4013 pointer_or_operator::op2_range (irange &r, tree type,
4014 const irange &lhs,
4015 const irange &op1,
4016 relation_kind rel ATTRIBUTE_UNUSED) const
4018 return pointer_or_operator::op1_range (r, type, lhs, op1);
4021 void
4022 pointer_or_operator::wi_fold (irange &r, tree type,
4023 const wide_int &lh_lb,
4024 const wide_int &lh_ub,
4025 const wide_int &rh_lb,
4026 const wide_int &rh_ub) const
4028 // For pointer types, we are really only interested in asserting
4029 // whether the expression evaluates to non-NULL.
4030 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4031 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4032 r = range_nonzero (type);
4033 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4034 r = range_zero (type);
4035 else
4036 r.set_varying (type);
4039 // Return a pointer to the range_operator instance, if there is one
4040 // associated with tree_code CODE.
4042 range_operator *
4043 range_op_table::operator[] (enum tree_code code)
4045 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4046 return m_range_tree[code];
4049 // Add OP to the handler table for CODE.
4051 void
4052 range_op_table::set (enum tree_code code, range_operator &op)
4054 gcc_checking_assert (m_range_tree[code] == NULL);
4055 m_range_tree[code] = &op;
4058 // Instantiate a range op table for integral operations.
4060 class integral_table : public range_op_table
4062 public:
4063 integral_table ();
4064 } integral_tree_table;
4066 integral_table::integral_table ()
4068 set (EQ_EXPR, op_equal);
4069 set (NE_EXPR, op_not_equal);
4070 set (LT_EXPR, op_lt);
4071 set (LE_EXPR, op_le);
4072 set (GT_EXPR, op_gt);
4073 set (GE_EXPR, op_ge);
4074 set (PLUS_EXPR, op_plus);
4075 set (MINUS_EXPR, op_minus);
4076 set (MIN_EXPR, op_min);
4077 set (MAX_EXPR, op_max);
4078 set (MULT_EXPR, op_mult);
4079 set (TRUNC_DIV_EXPR, op_trunc_div);
4080 set (FLOOR_DIV_EXPR, op_floor_div);
4081 set (ROUND_DIV_EXPR, op_round_div);
4082 set (CEIL_DIV_EXPR, op_ceil_div);
4083 set (EXACT_DIV_EXPR, op_exact_div);
4084 set (LSHIFT_EXPR, op_lshift);
4085 set (RSHIFT_EXPR, op_rshift);
4086 set (NOP_EXPR, op_convert);
4087 set (CONVERT_EXPR, op_convert);
4088 set (TRUTH_AND_EXPR, op_logical_and);
4089 set (BIT_AND_EXPR, op_bitwise_and);
4090 set (TRUTH_OR_EXPR, op_logical_or);
4091 set (BIT_IOR_EXPR, op_bitwise_or);
4092 set (BIT_XOR_EXPR, op_bitwise_xor);
4093 set (TRUNC_MOD_EXPR, op_trunc_mod);
4094 set (TRUTH_NOT_EXPR, op_logical_not);
4095 set (BIT_NOT_EXPR, op_bitwise_not);
4096 set (INTEGER_CST, op_integer_cst);
4097 set (SSA_NAME, op_identity);
4098 set (PAREN_EXPR, op_identity);
4099 set (OBJ_TYPE_REF, op_identity);
4100 set (IMAGPART_EXPR, op_unknown);
4101 set (REALPART_EXPR, op_unknown);
4102 set (POINTER_DIFF_EXPR, op_pointer_diff);
4103 set (ABS_EXPR, op_abs);
4104 set (ABSU_EXPR, op_absu);
4105 set (NEGATE_EXPR, op_negate);
4106 set (ADDR_EXPR, op_addr);
4109 // Instantiate a range op table for pointer operations.
4111 class pointer_table : public range_op_table
4113 public:
4114 pointer_table ();
4115 } pointer_tree_table;
4117 pointer_table::pointer_table ()
4119 set (BIT_AND_EXPR, op_pointer_and);
4120 set (BIT_IOR_EXPR, op_pointer_or);
4121 set (MIN_EXPR, op_ptr_min_max);
4122 set (MAX_EXPR, op_ptr_min_max);
4123 set (POINTER_PLUS_EXPR, op_pointer_plus);
4125 set (EQ_EXPR, op_equal);
4126 set (NE_EXPR, op_not_equal);
4127 set (LT_EXPR, op_lt);
4128 set (LE_EXPR, op_le);
4129 set (GT_EXPR, op_gt);
4130 set (GE_EXPR, op_ge);
4131 set (SSA_NAME, op_identity);
4132 set (INTEGER_CST, op_integer_cst);
4133 set (ADDR_EXPR, op_addr);
4134 set (NOP_EXPR, op_convert);
4135 set (CONVERT_EXPR, op_convert);
4137 set (BIT_NOT_EXPR, op_bitwise_not);
4138 set (BIT_XOR_EXPR, op_bitwise_xor);
4141 // The tables are hidden and accessed via a simple extern function.
4143 static inline range_operator *
4144 get_handler (enum tree_code code, tree type)
4146 // First check if there is a pointer specialization.
4147 if (POINTER_TYPE_P (type))
4148 return pointer_tree_table[code];
4149 if (INTEGRAL_TYPE_P (type))
4150 return integral_tree_table[code];
4151 return NULL;
4154 // Return the floating point operator for CODE or NULL if none available.
4156 static inline range_operator_float *
4157 get_float_handler (enum tree_code code, tree)
4159 return (*floating_tree_table)[code];
4162 range_op_handler::range_op_handler (tree_code code, tree type)
4163 : m_code (code), m_type (type)
4167 range_op_handler::range_op_handler (const gimple *s)
4169 if (const gassign *ass = dyn_cast<const gassign *> (s))
4171 m_code = gimple_assign_rhs_code (ass);
4172 // The LHS of a comparison is always an int, so we must look at
4173 // the operands.
4174 if (TREE_CODE_CLASS (m_code) == tcc_comparison)
4175 m_type = TREE_TYPE (gimple_assign_rhs1 (ass));
4176 else
4177 m_type = TREE_TYPE (gimple_assign_lhs (ass));
4179 else if (const gcond *cond = dyn_cast<const gcond *> (s))
4181 m_code = gimple_cond_code (cond);
4182 m_type = TREE_TYPE (gimple_cond_lhs (cond));
4184 else
4186 // A null type means there is no handler for this combination,
4187 // but the decision whether there is one or not, is delayed
4188 // until operator bool below is queried.
4189 m_code = NOP_EXPR;
4190 m_type = nullptr;
4194 // Return TRUE if there is a handler available for the current
4195 // combination of tree_code and type.
4197 range_op_handler::operator bool () const
4199 if (!m_type)
4200 return false;
4201 if (frange::supports_p (m_type))
4202 return get_float_handler (m_code, m_type);
4203 return get_handler (m_code, m_type);
4206 bool
4207 range_op_handler::fold_range (vrange &r, tree type,
4208 const vrange &lh,
4209 const vrange &rh,
4210 relation_kind rel) const
4212 if (irange::supports_p (m_type))
4214 range_operator *op = get_handler (m_code, m_type);
4215 return op->fold_range (as_a <irange> (r), type,
4216 as_a <irange> (lh),
4217 as_a <irange> (rh), rel);
4219 if (frange::supports_p (m_type))
4221 range_operator_float *op = get_float_handler (m_code, m_type);
4222 if (is_a <irange> (r))
4223 return op->fold_range (as_a <irange> (r), type,
4224 as_a <frange> (lh),
4225 as_a <frange> (rh), rel);
4226 return op->fold_range (as_a <frange> (r), type,
4227 as_a <frange> (lh),
4228 as_a <frange> (rh), rel);
4230 gcc_unreachable ();
4231 return false;
4234 bool
4235 range_op_handler::op1_range (vrange &r, tree type,
4236 const vrange &lhs,
4237 const vrange &op2,
4238 relation_kind rel) const
4240 if (irange::supports_p (m_type))
4242 range_operator *op = get_handler (m_code, m_type);
4243 return op->op1_range (as_a <irange> (r), type,
4244 as_a <irange> (lhs),
4245 as_a <irange> (op2), rel);
4247 if (frange::supports_p (m_type))
4249 range_operator_float *op = get_float_handler (m_code, m_type);
4250 if (is_a <irange> (lhs))
4251 return op->op1_range (as_a <frange> (r), type,
4252 as_a <irange> (lhs),
4253 as_a <frange> (op2), rel);
4254 return op->op1_range (as_a <frange> (r), type,
4255 as_a <frange> (lhs),
4256 as_a <frange> (op2), rel);
4258 gcc_unreachable ();
4259 return false;
4262 bool
4263 range_op_handler::op2_range (vrange &r, tree type,
4264 const vrange &lhs,
4265 const vrange &op1,
4266 relation_kind rel) const
4268 if (irange::supports_p (m_type))
4270 range_operator *op = get_handler (m_code, m_type);
4271 return op->op2_range (as_a <irange> (r), type,
4272 as_a <irange> (lhs),
4273 as_a <irange> (op1), rel);
4275 if (frange::supports_p (m_type))
4277 range_operator_float *op = get_float_handler (m_code, m_type);
4278 if (is_a <irange> (lhs))
4279 return op->op2_range (as_a <frange> (r), type,
4280 as_a <irange> (lhs),
4281 as_a <frange> (op1), rel);
4282 return op->op2_range (as_a <frange> (r), type,
4283 as_a <frange> (lhs),
4284 as_a <frange> (op1), rel);
4286 gcc_unreachable ();
4287 return false;
4290 relation_kind
4291 range_op_handler::lhs_op1_relation (const vrange &lhs,
4292 const vrange &op1,
4293 const vrange &op2,
4294 relation_kind rel) const
4296 if (irange::supports_p (m_type))
4298 range_operator *op = get_handler (m_code, m_type);
4299 return op->lhs_op1_relation (as_a <irange> (lhs),
4300 as_a <irange> (op1),
4301 as_a <irange> (op2), rel);
4303 if (frange::supports_p (m_type))
4305 range_operator_float *op = get_float_handler (m_code, m_type);
4306 if (is_a <irange> (lhs))
4307 return op->lhs_op1_relation (as_a <irange> (lhs),
4308 as_a <frange> (op1),
4309 as_a <frange> (op2), rel);
4310 return op->lhs_op1_relation (as_a <frange> (lhs),
4311 as_a <frange> (op1),
4312 as_a <frange> (op2), rel);
4314 gcc_unreachable ();
4315 return VREL_VARYING;
4318 relation_kind
4319 range_op_handler::lhs_op2_relation (const vrange &lhs,
4320 const vrange &op1,
4321 const vrange &op2,
4322 relation_kind rel) const
4324 if (irange::supports_p (m_type))
4326 range_operator *op = get_handler (m_code, m_type);
4327 return op->lhs_op2_relation (as_a <irange> (lhs),
4328 as_a <irange> (op1),
4329 as_a <irange> (op2), rel);
4331 if (frange::supports_p (m_type))
4333 range_operator_float *op = get_float_handler (m_code, m_type);
4334 if (is_a <irange> (lhs))
4335 return op->lhs_op2_relation (as_a <irange> (lhs),
4336 as_a <frange> (op1),
4337 as_a <frange> (op2), rel);
4338 return op->lhs_op2_relation (as_a <frange> (lhs),
4339 as_a <frange> (op1),
4340 as_a <frange> (op2), rel);
4342 gcc_unreachable ();
4343 return VREL_VARYING;
4346 relation_kind
4347 range_op_handler::op1_op2_relation (const vrange &lhs) const
4349 if (irange::supports_p (m_type))
4351 range_operator *op = get_handler (m_code, m_type);
4352 return op->op1_op2_relation (as_a <irange> (lhs));
4354 if (frange::supports_p (m_type))
4356 range_operator_float *op = get_float_handler (m_code, m_type);
4357 return op->op1_op2_relation (as_a <irange> (lhs));
4359 gcc_unreachable ();
4360 return VREL_VARYING;
4363 // Cast the range in R to TYPE.
4365 bool
4366 range_cast (vrange &r, tree type)
4368 Value_Range tmp (r);
4369 Value_Range varying (type);
4370 varying.set_varying (type);
4371 range_op_handler op (CONVERT_EXPR, type);
4372 // Call op_convert, if it fails, the result is varying.
4373 if (!op || !op.fold_range (r, type, tmp, varying))
4375 r.set_varying (type);
4376 return false;
4378 return true;
4381 #if CHECKING_P
4382 #include "selftest.h"
4384 namespace selftest
4386 #define INT(N) build_int_cst (integer_type_node, (N))
4387 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4388 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4389 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4390 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4391 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4393 static void
4394 range_op_cast_tests ()
4396 int_range<1> r0, r1, r2, rold;
4397 r0.set_varying (integer_type_node);
4398 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4400 // If a range is in any way outside of the range for the converted
4401 // to range, default to the range for the new type.
4402 r0.set_varying (short_integer_type_node);
4403 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4404 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4405 if (TYPE_PRECISION (TREE_TYPE (maxint))
4406 > TYPE_PRECISION (short_integer_type_node))
4408 r1 = int_range<1> (integer_zero_node, maxint);
4409 range_cast (r1, short_integer_type_node);
4410 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4411 && r1.upper_bound() == wi::to_wide (maxshort));
4414 // (unsigned char)[-5,-1] => [251,255].
4415 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4416 range_cast (r0, unsigned_char_type_node);
4417 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4418 range_cast (r0, signed_char_type_node);
4419 ASSERT_TRUE (r0 == rold);
4421 // (signed char)[15, 150] => [-128,-106][15,127].
4422 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4423 range_cast (r0, signed_char_type_node);
4424 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4425 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4426 r1.union_ (r2);
4427 ASSERT_TRUE (r1 == r0);
4428 range_cast (r0, unsigned_char_type_node);
4429 ASSERT_TRUE (r0 == rold);
4431 // (unsigned char)[-5, 5] => [0,5][251,255].
4432 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4433 range_cast (r0, unsigned_char_type_node);
4434 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4435 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4436 r1.union_ (r2);
4437 ASSERT_TRUE (r0 == r1);
4438 range_cast (r0, signed_char_type_node);
4439 ASSERT_TRUE (r0 == rold);
4441 // (unsigned char)[-5,5] => [0,5][251,255].
4442 r0 = int_range<1> (INT (-5), INT (5));
4443 range_cast (r0, unsigned_char_type_node);
4444 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4445 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4446 ASSERT_TRUE (r0 == r1);
4448 // (unsigned char)[5U,1974U] => [0,255].
4449 r0 = int_range<1> (UINT (5), UINT (1974));
4450 range_cast (r0, unsigned_char_type_node);
4451 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4452 range_cast (r0, integer_type_node);
4453 // Going to a wider range should not sign extend.
4454 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4456 // (unsigned char)[-350,15] => [0,255].
4457 r0 = int_range<1> (INT (-350), INT (15));
4458 range_cast (r0, unsigned_char_type_node);
4459 ASSERT_TRUE (r0 == (int_range<1>
4460 (TYPE_MIN_VALUE (unsigned_char_type_node),
4461 TYPE_MAX_VALUE (unsigned_char_type_node))));
4463 // Casting [-120,20] from signed char to unsigned short.
4464 // => [0, 20][0xff88, 0xffff].
4465 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4466 range_cast (r0, short_unsigned_type_node);
4467 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4468 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4469 r1.union_ (r2);
4470 ASSERT_TRUE (r0 == r1);
4471 // A truncating cast back to signed char will work because [-120, 20]
4472 // is representable in signed char.
4473 range_cast (r0, signed_char_type_node);
4474 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4476 // unsigned char -> signed short
4477 // (signed short)[(unsigned char)25, (unsigned char)250]
4478 // => [(signed short)25, (signed short)250]
4479 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4480 range_cast (r0, short_integer_type_node);
4481 r1 = int_range<1> (INT16 (25), INT16 (250));
4482 ASSERT_TRUE (r0 == r1);
4483 range_cast (r0, unsigned_char_type_node);
4484 ASSERT_TRUE (r0 == rold);
4486 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4487 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4488 TYPE_MAX_VALUE (long_long_integer_type_node));
4489 range_cast (r0, short_unsigned_type_node);
4490 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4491 TYPE_MAX_VALUE (short_unsigned_type_node));
4492 ASSERT_TRUE (r0 == r1);
4494 // Casting NONZERO to a narrower type will wrap/overflow so
4495 // it's just the entire range for the narrower type.
4497 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4498 // is outside of the range of a smaller range, return the full
4499 // smaller range.
4500 if (TYPE_PRECISION (integer_type_node)
4501 > TYPE_PRECISION (short_integer_type_node))
4503 r0 = range_nonzero (integer_type_node);
4504 range_cast (r0, short_integer_type_node);
4505 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4506 TYPE_MAX_VALUE (short_integer_type_node));
4507 ASSERT_TRUE (r0 == r1);
4510 // Casting NONZERO from a narrower signed to a wider signed.
4512 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4513 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4514 r0 = range_nonzero (short_integer_type_node);
4515 range_cast (r0, integer_type_node);
4516 r1 = int_range<1> (INT (-32768), INT (-1));
4517 r2 = int_range<1> (INT (1), INT (32767));
4518 r1.union_ (r2);
4519 ASSERT_TRUE (r0 == r1);
4522 static void
4523 range_op_lshift_tests ()
4525 // Test that 0x808.... & 0x8.... still contains 0x8....
4526 // for a large set of numbers.
4528 int_range_max res;
4529 tree big_type = long_long_unsigned_type_node;
4530 // big_num = 0x808,0000,0000,0000
4531 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4532 build_int_cst (big_type, 0x808),
4533 build_int_cst (big_type, 48));
4534 op_bitwise_and.fold_range (res, big_type,
4535 int_range <1> (big_type),
4536 int_range <1> (big_num, big_num));
4537 // val = 0x8,0000,0000,0000
4538 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4539 build_int_cst (big_type, 0x8),
4540 build_int_cst (big_type, 48));
4541 ASSERT_TRUE (res.contains_p (val));
4544 if (TYPE_PRECISION (unsigned_type_node) > 31)
4546 // unsigned VARYING = op1 << 1 should be VARYING.
4547 int_range<2> lhs (unsigned_type_node);
4548 int_range<2> shift (INT (1), INT (1));
4549 int_range_max op1;
4550 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4551 ASSERT_TRUE (op1.varying_p ());
4553 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4554 int_range<2> zero (UINT (0), UINT (0));
4555 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4556 ASSERT_TRUE (op1.num_pairs () == 2);
4557 // Remove the [0,0] range.
4558 op1.intersect (zero);
4559 ASSERT_TRUE (op1.num_pairs () == 1);
4560 // op1 << 1 should be [0x8000,0x8000] << 1,
4561 // which should result in [0,0].
4562 int_range_max result;
4563 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4564 ASSERT_TRUE (result == zero);
4566 // signed VARYING = op1 << 1 should be VARYING.
4567 if (TYPE_PRECISION (integer_type_node) > 31)
4569 // unsigned VARYING = op1 << 1 hould be VARYING.
4570 int_range<2> lhs (integer_type_node);
4571 int_range<2> shift (INT (1), INT (1));
4572 int_range_max op1;
4573 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4574 ASSERT_TRUE (op1.varying_p ());
4576 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4577 int_range<2> zero (INT (0), INT (0));
4578 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4579 ASSERT_TRUE (op1.num_pairs () == 2);
4580 // Remove the [0,0] range.
4581 op1.intersect (zero);
4582 ASSERT_TRUE (op1.num_pairs () == 1);
4583 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4584 // which should result in [0,0].
4585 int_range_max result;
4586 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4587 ASSERT_TRUE (result == zero);
4591 static void
4592 range_op_rshift_tests ()
4594 // unsigned: [3, MAX] = OP1 >> 1
4596 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4597 TYPE_MAX_VALUE (unsigned_type_node));
4598 int_range_max one (build_one_cst (unsigned_type_node),
4599 build_one_cst (unsigned_type_node));
4600 int_range_max op1;
4601 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4602 ASSERT_FALSE (op1.contains_p (UINT (3)));
4605 // signed: [3, MAX] = OP1 >> 1
4607 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4608 int_range_max one (INT (1), INT (1));
4609 int_range_max op1;
4610 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4611 ASSERT_FALSE (op1.contains_p (INT (-2)));
4614 // This is impossible, so OP1 should be [].
4615 // signed: [MIN, MIN] = OP1 >> 1
4617 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4618 TYPE_MIN_VALUE (integer_type_node));
4619 int_range_max one (INT (1), INT (1));
4620 int_range_max op1;
4621 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4622 ASSERT_TRUE (op1.undefined_p ());
4625 // signed: ~[-1] = OP1 >> 31
4626 if (TYPE_PRECISION (integer_type_node) > 31)
4628 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4629 int_range_max shift (INT (31), INT (31));
4630 int_range_max op1;
4631 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4632 int_range_max negatives = range_negatives (integer_type_node);
4633 negatives.intersect (op1);
4634 ASSERT_TRUE (negatives.undefined_p ());
4638 static void
4639 range_op_bitwise_and_tests ()
4641 int_range_max res;
4642 tree min = vrp_val_min (integer_type_node);
4643 tree max = vrp_val_max (integer_type_node);
4644 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4645 build_one_cst (integer_type_node));
4646 int_range_max i1 (tiny, max);
4647 int_range_max i2 (build_int_cst (integer_type_node, 255),
4648 build_int_cst (integer_type_node, 255));
4650 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4651 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4652 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4654 // VARYING = OP1 & 255: OP1 is VARYING
4655 i1 = int_range<1> (integer_type_node);
4656 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4657 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4659 // (NONZERO | X) is nonzero.
4660 i1.set_nonzero (integer_type_node);
4661 i2.set_varying (integer_type_node);
4662 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4663 ASSERT_TRUE (res.nonzero_p ());
4665 // (NEGATIVE | X) is nonzero.
4666 i1 = int_range<1> (INT (-5), INT (-3));
4667 i2.set_varying (integer_type_node);
4668 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4669 ASSERT_FALSE (res.contains_p (INT (0)));
4672 static void
4673 range_relational_tests ()
4675 int_range<2> lhs (unsigned_char_type_node);
4676 int_range<2> op1 (UCHAR (8), UCHAR (10));
4677 int_range<2> op2 (UCHAR (20), UCHAR (20));
4679 // Never wrapping additions mean LHS > OP1.
4680 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4681 ASSERT_TRUE (code == VREL_GT);
4683 // Most wrapping additions mean nothing...
4684 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4685 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4686 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4687 ASSERT_TRUE (code == VREL_VARYING);
4689 // However, always wrapping additions mean LHS < OP1.
4690 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4691 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4692 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4693 ASSERT_TRUE (code == VREL_LT);
4696 void
4697 range_op_tests ()
4699 range_op_rshift_tests ();
4700 range_op_lshift_tests ();
4701 range_op_bitwise_and_tests ();
4702 range_op_cast_tests ();
4703 range_relational_tests ();
4706 } // namespace selftest
4708 #endif // CHECKING_P