compiler: only build thunk struct type when it is needed
[official-gcc.git] / gcc / range-op.cc
blobdf0735cb42a837577859ef322ac8920c539e0d5a
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 // Given addition or subtraction, determine the possible NORMAL ranges and
1309 // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1310 // Return the relation that exists between the LHS and OP1 in order for the
1311 // NORMAL range to apply.
1312 // a return value of VREL_VARYING means no ranges were applicable.
1314 static relation_kind
1315 plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
1316 bool add_p)
1318 relation_kind kind = VREL_VARYING;
1319 // For now, only deal with constant adds. This could be extended to ranges
1320 // when someone is so motivated.
1321 if (!offset.singleton_p () || offset.zero_p ())
1322 return kind;
1324 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1325 wide_int off = offset.lower_bound ();
1326 if (wi::neg_p (off, SIGNED))
1328 add_p = !add_p;
1329 off = wi::neg (off);
1332 wi::overflow_type ov;
1333 tree type = offset.type ();
1334 unsigned prec = TYPE_PRECISION (type);
1335 wide_int ub;
1336 wide_int lb;
1337 // calculate the normal range and relation for the operation.
1338 if (add_p)
1340 // [ 0 , INF - OFF]
1341 lb = wi::zero (prec);
1342 ub = wi::sub (wi::to_wide (vrp_val_max (type)), off, UNSIGNED, &ov);
1343 kind = VREL_GT;
1345 else
1347 // [ OFF, INF ]
1348 lb = off;
1349 ub = wi::to_wide (vrp_val_max (type));
1350 kind = VREL_LT;
1352 int_range<2> normal_range (type, lb, ub);
1353 int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
1355 r_ov = ov_range;
1356 r_normal = normal_range;
1357 return kind;
1360 // Once op1 has been calculated by operator_plus or operator_minus, check
1361 // to see if the relation passed causes any part of the calculation to
1362 // be not possible. ie
1363 // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1364 // and that further refines a_2 to [0, 0].
1365 // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1366 // relation between LHS relatoin OP1 and ADD_P is true for PLUS, false for
1367 // MINUS. IF any adjustment can be made, R will reflect it.
1369 static void
1370 adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
1371 bool add_p)
1373 if (r.undefined_p ())
1374 return;
1375 tree type = r.type ();
1376 // Check for unsigned overflow and calculate the overflow part.
1377 signop s = TYPE_SIGN (type);
1378 if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
1379 return;
1381 // Only work with <, <=, >, >= relations.
1382 if (!relation_lt_le_gt_ge_p (rel))
1383 return;
1385 // Get the ranges for this offset.
1386 int_range_max normal, overflow;
1387 relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
1389 // VREL_VARYING means there are no adjustments.
1390 if (k == VREL_VARYING)
1391 return;
1393 // If the relations match use the normal range, otherwise use overflow range.
1394 if (relation_intersect (k, rel) == k)
1395 r.intersect (normal);
1396 else
1397 r.intersect (overflow);
1398 return;
1401 bool
1402 operator_plus::op1_range (irange &r, tree type,
1403 const irange &lhs,
1404 const irange &op2,
1405 relation_kind rel) const
1407 if (lhs.undefined_p ())
1408 return false;
1409 // Start with the default operation.
1410 range_op_handler minus (MINUS_EXPR, type);
1411 if (!minus)
1412 return false;
1413 bool res = minus.fold_range (r, type, lhs, op2);
1414 // Check for a relation refinement.
1415 if (res)
1416 adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
1417 return res;
1420 bool
1421 operator_plus::op2_range (irange &r, tree type,
1422 const irange &lhs,
1423 const irange &op1,
1424 relation_kind rel) const
1426 return op1_range (r, type, lhs, op1, rel);
1430 class operator_minus : public range_operator
1432 using range_operator::fold_range;
1433 using range_operator::op1_range;
1434 using range_operator::op2_range;
1435 public:
1436 virtual bool op1_range (irange &r, tree type,
1437 const irange &lhs,
1438 const irange &op2,
1439 relation_kind rel ATTRIBUTE_UNUSED) const;
1440 virtual bool op2_range (irange &r, tree type,
1441 const irange &lhs,
1442 const irange &op1,
1443 relation_kind rel ATTRIBUTE_UNUSED) const;
1444 virtual void wi_fold (irange &r, tree type,
1445 const wide_int &lh_lb,
1446 const wide_int &lh_ub,
1447 const wide_int &rh_lb,
1448 const wide_int &rh_ub) const;
1449 virtual relation_kind lhs_op1_relation (const irange &lhs,
1450 const irange &op1,
1451 const irange &op2,
1452 relation_kind rel) const;
1453 virtual bool op1_op2_relation_effect (irange &lhs_range,
1454 tree type,
1455 const irange &op1_range,
1456 const irange &op2_range,
1457 relation_kind rel) const;
1458 } op_minus;
1460 void
1461 operator_minus::wi_fold (irange &r, tree type,
1462 const wide_int &lh_lb, const wide_int &lh_ub,
1463 const wide_int &rh_lb, const wide_int &rh_ub) const
1465 wi::overflow_type ov_lb, ov_ub;
1466 signop s = TYPE_SIGN (type);
1467 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1468 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1469 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1473 // Return the relation between LHS and OP1 based on the relation between
1474 // OP1 and OP2.
1476 relation_kind
1477 operator_minus::lhs_op1_relation (const irange &, const irange &op1,
1478 const irange &, relation_kind rel) const
1480 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
1481 switch (rel)
1483 case VREL_GT:
1484 case VREL_GE:
1485 return VREL_LE;
1486 default:
1487 break;
1489 return VREL_VARYING;
1492 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1493 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1494 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1496 static bool
1497 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1498 const irange &op1_range ATTRIBUTE_UNUSED,
1499 const irange &op2_range ATTRIBUTE_UNUSED,
1500 relation_kind rel)
1502 if (rel == VREL_VARYING)
1503 return false;
1505 int_range<2> rel_range;
1506 unsigned prec = TYPE_PRECISION (type);
1507 signop sgn = TYPE_SIGN (type);
1509 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1510 if (rel == VREL_EQ)
1511 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1512 else if (rel == VREL_NE)
1513 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1514 VR_ANTI_RANGE);
1515 else if (TYPE_OVERFLOW_WRAPS (type))
1517 switch (rel)
1519 // For wrapping signed values and unsigned, if op1 > op2 or
1520 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1521 case VREL_GT:
1522 case VREL_LT:
1523 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1524 VR_ANTI_RANGE);
1525 break;
1526 default:
1527 return false;
1530 else
1532 switch (rel)
1534 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1535 case VREL_GT:
1536 rel_range = int_range<2> (type, wi::one (prec),
1537 wi::max_value (prec, sgn));
1538 break;
1539 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1540 case VREL_GE:
1541 rel_range = int_range<2> (type, wi::zero (prec),
1542 wi::max_value (prec, sgn));
1543 break;
1544 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1545 case VREL_LT:
1546 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1547 wi::minus_one (prec));
1548 break;
1549 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1550 case VREL_LE:
1551 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1552 wi::zero (prec));
1553 break;
1554 default:
1555 return false;
1558 lhs_range.intersect (rel_range);
1559 return true;
1562 bool
1563 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1564 const irange &op1_range,
1565 const irange &op2_range,
1566 relation_kind rel) const
1568 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1569 rel);
1572 bool
1573 operator_minus::op1_range (irange &r, tree type,
1574 const irange &lhs,
1575 const irange &op2,
1576 relation_kind rel ATTRIBUTE_UNUSED) const
1578 if (lhs.undefined_p ())
1579 return false;
1580 // Start with the default operation.
1581 range_op_handler minus (PLUS_EXPR, type);
1582 if (!minus)
1583 return false;
1584 bool res = minus.fold_range (r, type, lhs, op2);
1585 if (res)
1586 adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
1587 return res;
1591 bool
1592 operator_minus::op2_range (irange &r, tree type,
1593 const irange &lhs,
1594 const irange &op1,
1595 relation_kind rel ATTRIBUTE_UNUSED) const
1597 if (lhs.undefined_p ())
1598 return false;
1599 return fold_range (r, type, op1, lhs);
1603 class operator_pointer_diff : public range_operator
1605 virtual bool op1_op2_relation_effect (irange &lhs_range,
1606 tree type,
1607 const irange &op1_range,
1608 const irange &op2_range,
1609 relation_kind rel) const;
1610 } op_pointer_diff;
1612 bool
1613 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1614 const irange &op1_range,
1615 const irange &op2_range,
1616 relation_kind rel) const
1618 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1619 rel);
1623 class operator_min : public range_operator
1625 public:
1626 virtual void wi_fold (irange &r, tree type,
1627 const wide_int &lh_lb,
1628 const wide_int &lh_ub,
1629 const wide_int &rh_lb,
1630 const wide_int &rh_ub) const;
1631 } op_min;
1633 void
1634 operator_min::wi_fold (irange &r, tree type,
1635 const wide_int &lh_lb, const wide_int &lh_ub,
1636 const wide_int &rh_lb, const wide_int &rh_ub) const
1638 signop s = TYPE_SIGN (type);
1639 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1640 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1641 value_range_with_overflow (r, type, new_lb, new_ub);
1645 class operator_max : public range_operator
1647 public:
1648 virtual void wi_fold (irange &r, tree type,
1649 const wide_int &lh_lb,
1650 const wide_int &lh_ub,
1651 const wide_int &rh_lb,
1652 const wide_int &rh_ub) const;
1653 } op_max;
1655 void
1656 operator_max::wi_fold (irange &r, tree type,
1657 const wide_int &lh_lb, const wide_int &lh_ub,
1658 const wide_int &rh_lb, const wide_int &rh_ub) const
1660 signop s = TYPE_SIGN (type);
1661 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1662 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1663 value_range_with_overflow (r, type, new_lb, new_ub);
1667 class cross_product_operator : public range_operator
1669 public:
1670 // Perform an operation between two wide-ints and place the result
1671 // in R. Return true if the operation overflowed.
1672 virtual bool wi_op_overflows (wide_int &r,
1673 tree type,
1674 const wide_int &,
1675 const wide_int &) const = 0;
1677 // Calculate the cross product of two sets of sub-ranges and return it.
1678 void wi_cross_product (irange &r, tree type,
1679 const wide_int &lh_lb,
1680 const wide_int &lh_ub,
1681 const wide_int &rh_lb,
1682 const wide_int &rh_ub) const;
1685 // Calculate the cross product of two sets of ranges and return it.
1687 // Multiplications, divisions and shifts are a bit tricky to handle,
1688 // depending on the mix of signs we have in the two ranges, we need to
1689 // operate on different values to get the minimum and maximum values
1690 // for the new range. One approach is to figure out all the
1691 // variations of range combinations and do the operations.
1693 // However, this involves several calls to compare_values and it is
1694 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1695 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1696 // figure the smallest and largest values to form the new range.
1698 void
1699 cross_product_operator::wi_cross_product (irange &r, tree type,
1700 const wide_int &lh_lb,
1701 const wide_int &lh_ub,
1702 const wide_int &rh_lb,
1703 const wide_int &rh_ub) const
1705 wide_int cp1, cp2, cp3, cp4;
1706 // Default to varying.
1707 r.set_varying (type);
1709 // Compute the 4 cross operations, bailing if we get an overflow we
1710 // can't handle.
1711 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1712 return;
1713 if (wi::eq_p (lh_lb, lh_ub))
1714 cp3 = cp1;
1715 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1716 return;
1717 if (wi::eq_p (rh_lb, rh_ub))
1718 cp2 = cp1;
1719 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1720 return;
1721 if (wi::eq_p (lh_lb, lh_ub))
1722 cp4 = cp2;
1723 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1724 return;
1726 // Order pairs.
1727 signop sign = TYPE_SIGN (type);
1728 if (wi::gt_p (cp1, cp2, sign))
1729 std::swap (cp1, cp2);
1730 if (wi::gt_p (cp3, cp4, sign))
1731 std::swap (cp3, cp4);
1733 // Choose min and max from the ordered pairs.
1734 wide_int res_lb = wi::min (cp1, cp3, sign);
1735 wide_int res_ub = wi::max (cp2, cp4, sign);
1736 value_range_with_overflow (r, type, res_lb, res_ub);
1740 class operator_mult : public cross_product_operator
1742 using range_operator::op1_range;
1743 using range_operator::op2_range;
1744 public:
1745 virtual void wi_fold (irange &r, tree type,
1746 const wide_int &lh_lb,
1747 const wide_int &lh_ub,
1748 const wide_int &rh_lb,
1749 const wide_int &rh_ub) const;
1750 virtual bool wi_op_overflows (wide_int &res, tree type,
1751 const wide_int &w0, const wide_int &w1) const;
1752 virtual bool op1_range (irange &r, tree type,
1753 const irange &lhs,
1754 const irange &op2,
1755 relation_kind rel ATTRIBUTE_UNUSED) const;
1756 virtual bool op2_range (irange &r, tree type,
1757 const irange &lhs,
1758 const irange &op1,
1759 relation_kind rel ATTRIBUTE_UNUSED) const;
1760 } op_mult;
1762 bool
1763 operator_mult::op1_range (irange &r, tree type,
1764 const irange &lhs, const irange &op2,
1765 relation_kind rel ATTRIBUTE_UNUSED) const
1767 tree offset;
1768 if (lhs.undefined_p ())
1769 return false;
1771 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1772 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1773 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1774 if (TYPE_OVERFLOW_WRAPS (type))
1775 return false;
1777 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1778 return range_op_handler (TRUNC_DIV_EXPR, type).fold_range (r, type,
1779 lhs, op2);
1780 return false;
1783 bool
1784 operator_mult::op2_range (irange &r, tree type,
1785 const irange &lhs, const irange &op1,
1786 relation_kind rel) const
1788 return operator_mult::op1_range (r, type, lhs, op1, rel);
1791 bool
1792 operator_mult::wi_op_overflows (wide_int &res, tree type,
1793 const wide_int &w0, const wide_int &w1) const
1795 wi::overflow_type overflow = wi::OVF_NONE;
1796 signop sign = TYPE_SIGN (type);
1797 res = wi::mul (w0, w1, sign, &overflow);
1798 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1800 // For multiplication, the sign of the overflow is given
1801 // by the comparison of the signs of the operands.
1802 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1803 res = wi::max_value (w0.get_precision (), sign);
1804 else
1805 res = wi::min_value (w0.get_precision (), sign);
1806 return false;
1808 return overflow;
1811 void
1812 operator_mult::wi_fold (irange &r, tree type,
1813 const wide_int &lh_lb, const wide_int &lh_ub,
1814 const wide_int &rh_lb, const wide_int &rh_ub) const
1816 if (TYPE_OVERFLOW_UNDEFINED (type))
1818 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1819 return;
1822 // Multiply the ranges when overflow wraps. This is basically fancy
1823 // code so we don't drop to varying with an unsigned
1824 // [-3,-1]*[-3,-1].
1826 // This test requires 2*prec bits if both operands are signed and
1827 // 2*prec + 2 bits if either is not. Therefore, extend the values
1828 // using the sign of the result to PREC2. From here on out,
1829 // everthing is just signed math no matter what the input types
1830 // were.
1832 signop sign = TYPE_SIGN (type);
1833 unsigned prec = TYPE_PRECISION (type);
1834 widest2_int min0 = widest2_int::from (lh_lb, sign);
1835 widest2_int max0 = widest2_int::from (lh_ub, sign);
1836 widest2_int min1 = widest2_int::from (rh_lb, sign);
1837 widest2_int max1 = widest2_int::from (rh_ub, sign);
1838 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1839 widest2_int size = sizem1 + 1;
1841 // Canonicalize the intervals.
1842 if (sign == UNSIGNED)
1844 if (wi::ltu_p (size, min0 + max0))
1846 min0 -= size;
1847 max0 -= size;
1849 if (wi::ltu_p (size, min1 + max1))
1851 min1 -= size;
1852 max1 -= size;
1856 // Sort the 4 products so that min is in prod0 and max is in
1857 // prod3.
1858 widest2_int prod0 = min0 * min1;
1859 widest2_int prod1 = min0 * max1;
1860 widest2_int prod2 = max0 * min1;
1861 widest2_int prod3 = max0 * max1;
1863 // min0min1 > max0max1
1864 if (prod0 > prod3)
1865 std::swap (prod0, prod3);
1867 // min0max1 > max0min1
1868 if (prod1 > prod2)
1869 std::swap (prod1, prod2);
1871 if (prod0 > prod1)
1872 std::swap (prod0, prod1);
1874 if (prod2 > prod3)
1875 std::swap (prod2, prod3);
1877 // diff = max - min
1878 prod2 = prod3 - prod0;
1879 if (wi::geu_p (prod2, sizem1))
1880 // The range covers all values.
1881 r.set_varying (type);
1882 else
1884 wide_int new_lb = wide_int::from (prod0, prec, sign);
1885 wide_int new_ub = wide_int::from (prod3, prec, sign);
1886 create_possibly_reversed_range (r, type, new_lb, new_ub);
1891 class operator_div : public cross_product_operator
1893 public:
1894 operator_div (enum tree_code c) { code = c; }
1895 virtual void wi_fold (irange &r, tree type,
1896 const wide_int &lh_lb,
1897 const wide_int &lh_ub,
1898 const wide_int &rh_lb,
1899 const wide_int &rh_ub) const;
1900 virtual bool wi_op_overflows (wide_int &res, tree type,
1901 const wide_int &, const wide_int &) const;
1902 private:
1903 enum tree_code code;
1906 bool
1907 operator_div::wi_op_overflows (wide_int &res, tree type,
1908 const wide_int &w0, const wide_int &w1) const
1910 if (w1 == 0)
1911 return true;
1913 wi::overflow_type overflow = wi::OVF_NONE;
1914 signop sign = TYPE_SIGN (type);
1916 switch (code)
1918 case EXACT_DIV_EXPR:
1919 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1920 // operator_exact_divide. No need to handle it here.
1921 gcc_unreachable ();
1922 break;
1923 case TRUNC_DIV_EXPR:
1924 res = wi::div_trunc (w0, w1, sign, &overflow);
1925 break;
1926 case FLOOR_DIV_EXPR:
1927 res = wi::div_floor (w0, w1, sign, &overflow);
1928 break;
1929 case ROUND_DIV_EXPR:
1930 res = wi::div_round (w0, w1, sign, &overflow);
1931 break;
1932 case CEIL_DIV_EXPR:
1933 res = wi::div_ceil (w0, w1, sign, &overflow);
1934 break;
1935 default:
1936 gcc_unreachable ();
1939 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1941 // For division, the only case is -INF / -1 = +INF.
1942 res = wi::max_value (w0.get_precision (), sign);
1943 return false;
1945 return overflow;
1948 void
1949 operator_div::wi_fold (irange &r, tree type,
1950 const wide_int &lh_lb, const wide_int &lh_ub,
1951 const wide_int &rh_lb, const wide_int &rh_ub) const
1953 const wide_int dividend_min = lh_lb;
1954 const wide_int dividend_max = lh_ub;
1955 const wide_int divisor_min = rh_lb;
1956 const wide_int divisor_max = rh_ub;
1957 signop sign = TYPE_SIGN (type);
1958 unsigned prec = TYPE_PRECISION (type);
1959 wide_int extra_min, extra_max;
1961 // If we know we won't divide by zero, just do the division.
1962 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1964 wi_cross_product (r, type, dividend_min, dividend_max,
1965 divisor_min, divisor_max);
1966 return;
1969 // If we're definitely dividing by zero, there's nothing to do.
1970 if (wi_zero_p (type, divisor_min, divisor_max))
1972 r.set_undefined ();
1973 return;
1976 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1977 // skip any division by zero.
1979 // First divide by the negative numbers, if any.
1980 if (wi::neg_p (divisor_min, sign))
1981 wi_cross_product (r, type, dividend_min, dividend_max,
1982 divisor_min, wi::minus_one (prec));
1983 else
1984 r.set_undefined ();
1986 // Then divide by the non-zero positive numbers, if any.
1987 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1989 int_range_max tmp;
1990 wi_cross_product (tmp, type, dividend_min, dividend_max,
1991 wi::one (prec), divisor_max);
1992 r.union_ (tmp);
1994 // We shouldn't still have undefined here.
1995 gcc_checking_assert (!r.undefined_p ());
1998 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1999 operator_div op_floor_div (FLOOR_DIV_EXPR);
2000 operator_div op_round_div (ROUND_DIV_EXPR);
2001 operator_div op_ceil_div (CEIL_DIV_EXPR);
2004 class operator_exact_divide : public operator_div
2006 using range_operator::op1_range;
2007 public:
2008 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
2009 virtual bool op1_range (irange &r, tree type,
2010 const irange &lhs,
2011 const irange &op2,
2012 relation_kind rel ATTRIBUTE_UNUSED) const;
2014 } op_exact_div;
2016 bool
2017 operator_exact_divide::op1_range (irange &r, tree type,
2018 const irange &lhs,
2019 const irange &op2,
2020 relation_kind rel ATTRIBUTE_UNUSED) const
2022 if (lhs.undefined_p ())
2023 return false;
2024 tree offset;
2025 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2026 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2027 // We wont bother trying to enumerate all the in between stuff :-P
2028 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
2029 // the time however.
2030 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2031 if (op2.singleton_p (&offset)
2032 && !integer_zerop (offset))
2033 return range_op_handler (MULT_EXPR, type).fold_range (r, type, lhs, op2);
2034 return false;
2038 class operator_lshift : public cross_product_operator
2040 using range_operator::fold_range;
2041 using range_operator::op1_range;
2042 public:
2043 virtual bool op1_range (irange &r, tree type,
2044 const irange &lhs,
2045 const irange &op2,
2046 relation_kind rel = VREL_VARYING) const;
2047 virtual bool fold_range (irange &r, tree type,
2048 const irange &op1,
2049 const irange &op2,
2050 relation_kind rel = VREL_VARYING) const;
2052 virtual void wi_fold (irange &r, tree type,
2053 const wide_int &lh_lb, const wide_int &lh_ub,
2054 const wide_int &rh_lb, const wide_int &rh_ub) const;
2055 virtual bool wi_op_overflows (wide_int &res,
2056 tree type,
2057 const wide_int &,
2058 const wide_int &) const;
2059 } op_lshift;
2061 class operator_rshift : public cross_product_operator
2063 using range_operator::fold_range;
2064 using range_operator::op1_range;
2065 using range_operator::lhs_op1_relation;
2066 public:
2067 virtual bool fold_range (irange &r, tree type,
2068 const irange &op1,
2069 const irange &op2,
2070 relation_kind rel = VREL_VARYING) const;
2071 virtual void wi_fold (irange &r, tree type,
2072 const wide_int &lh_lb,
2073 const wide_int &lh_ub,
2074 const wide_int &rh_lb,
2075 const wide_int &rh_ub) const;
2076 virtual bool wi_op_overflows (wide_int &res,
2077 tree type,
2078 const wide_int &w0,
2079 const wide_int &w1) const;
2080 virtual bool op1_range (irange &, tree type,
2081 const irange &lhs,
2082 const irange &op2,
2083 relation_kind rel = VREL_VARYING) const;
2084 virtual relation_kind lhs_op1_relation (const irange &lhs,
2085 const irange &op1,
2086 const irange &op2,
2087 relation_kind rel) const;
2088 } op_rshift;
2091 relation_kind
2092 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
2093 const irange &op1,
2094 const irange &op2,
2095 relation_kind) const
2097 // If both operands range are >= 0, then the LHS <= op1.
2098 if (!op1.undefined_p () && !op2.undefined_p ()
2099 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
2100 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
2101 return VREL_LE;
2102 return VREL_VARYING;
2105 bool
2106 operator_lshift::fold_range (irange &r, tree type,
2107 const irange &op1,
2108 const irange &op2,
2109 relation_kind rel) const
2111 int_range_max shift_range;
2112 if (!get_shift_range (shift_range, type, op2))
2114 if (op2.undefined_p ())
2115 r.set_undefined ();
2116 else
2117 r.set_varying (type);
2118 return true;
2121 // Transform left shifts by constants into multiplies.
2122 if (shift_range.singleton_p ())
2124 unsigned shift = shift_range.lower_bound ().to_uhwi ();
2125 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2126 int_range<1> mult (type, tmp, tmp);
2128 // Force wrapping multiplication.
2129 bool saved_flag_wrapv = flag_wrapv;
2130 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2131 flag_wrapv = 1;
2132 flag_wrapv_pointer = 1;
2133 bool b = op_mult.fold_range (r, type, op1, mult);
2134 flag_wrapv = saved_flag_wrapv;
2135 flag_wrapv_pointer = saved_flag_wrapv_pointer;
2136 return b;
2138 else
2139 // Otherwise, invoke the generic fold routine.
2140 return range_operator::fold_range (r, type, op1, shift_range, rel);
2143 void
2144 operator_lshift::wi_fold (irange &r, tree type,
2145 const wide_int &lh_lb, const wide_int &lh_ub,
2146 const wide_int &rh_lb, const wide_int &rh_ub) const
2148 signop sign = TYPE_SIGN (type);
2149 unsigned prec = TYPE_PRECISION (type);
2150 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2151 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2152 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2153 // overflow. However, for that to happen, rh.max needs to be zero,
2154 // which means rh is a singleton range of zero, which means we simply return
2155 // [lh_lb, lh_ub] as the range.
2156 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2158 r = int_range<2> (type, lh_lb, lh_ub);
2159 return;
2162 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2163 wide_int complement = ~(bound - 1);
2164 wide_int low_bound, high_bound;
2165 bool in_bounds = false;
2167 if (sign == UNSIGNED)
2169 low_bound = bound;
2170 high_bound = complement;
2171 if (wi::ltu_p (lh_ub, low_bound))
2173 // [5, 6] << [1, 2] == [10, 24].
2174 // We're shifting out only zeroes, the value increases
2175 // monotonically.
2176 in_bounds = true;
2178 else if (wi::ltu_p (high_bound, lh_lb))
2180 // [0xffffff00, 0xffffffff] << [1, 2]
2181 // == [0xfffffc00, 0xfffffffe].
2182 // We're shifting out only ones, the value decreases
2183 // monotonically.
2184 in_bounds = true;
2187 else
2189 // [-1, 1] << [1, 2] == [-4, 4]
2190 low_bound = complement;
2191 high_bound = bound;
2192 if (wi::lts_p (lh_ub, high_bound)
2193 && wi::lts_p (low_bound, lh_lb))
2195 // For non-negative numbers, we're shifting out only zeroes,
2196 // the value increases monotonically. For negative numbers,
2197 // we're shifting out only ones, the value decreases
2198 // monotonically.
2199 in_bounds = true;
2203 if (in_bounds)
2204 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2205 else
2206 r.set_varying (type);
2209 bool
2210 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2211 const wide_int &w0, const wide_int &w1) const
2213 signop sign = TYPE_SIGN (type);
2214 if (wi::neg_p (w1))
2216 // It's unclear from the C standard whether shifts can overflow.
2217 // The following code ignores overflow; perhaps a C standard
2218 // interpretation ruling is needed.
2219 res = wi::rshift (w0, -w1, sign);
2221 else
2222 res = wi::lshift (w0, w1);
2223 return false;
2226 bool
2227 operator_lshift::op1_range (irange &r,
2228 tree type,
2229 const irange &lhs,
2230 const irange &op2,
2231 relation_kind rel ATTRIBUTE_UNUSED) const
2233 if (lhs.undefined_p ())
2234 return false;
2235 tree shift_amount;
2237 if (!lhs.contains_p (build_zero_cst (type)))
2238 r.set_nonzero (type);
2239 else
2240 r.set_varying (type);
2242 if (op2.singleton_p (&shift_amount))
2244 wide_int shift = wi::to_wide (shift_amount);
2245 if (wi::lt_p (shift, 0, SIGNED))
2246 return false;
2247 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2248 TYPE_PRECISION (op2.type ())),
2249 UNSIGNED))
2250 return false;
2251 if (shift == 0)
2253 r.intersect (lhs);
2254 return true;
2257 // Work completely in unsigned mode to start.
2258 tree utype = type;
2259 int_range_max tmp_range;
2260 if (TYPE_SIGN (type) == SIGNED)
2262 int_range_max tmp = lhs;
2263 utype = unsigned_type_for (type);
2264 range_cast (tmp, utype);
2265 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2267 else
2268 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2270 // Start with ranges which can produce the LHS by right shifting the
2271 // result by the shift amount.
2272 // ie [0x08, 0xF0] = op1 << 2 will start with
2273 // [00001000, 11110000] = op1 << 2
2274 // [0x02, 0x4C] aka [00000010, 00111100]
2276 // Then create a range from the LB with the least significant upper bit
2277 // set, to the upper bound with all the bits set.
2278 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2280 // Ideally we do this for each subrange, but just lump them all for now.
2281 unsigned low_bits = TYPE_PRECISION (utype)
2282 - TREE_INT_CST_LOW (shift_amount);
2283 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2284 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2285 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2286 int_range<2> fill_range (utype, new_lb, new_ub);
2287 tmp_range.union_ (fill_range);
2289 if (utype != type)
2290 range_cast (tmp_range, type);
2292 r.intersect (tmp_range);
2293 return true;
2296 return !r.varying_p ();
2299 bool
2300 operator_rshift::op1_range (irange &r,
2301 tree type,
2302 const irange &lhs,
2303 const irange &op2,
2304 relation_kind rel ATTRIBUTE_UNUSED) const
2306 tree shift;
2307 if (lhs.undefined_p ())
2308 return false;
2309 if (op2.singleton_p (&shift))
2311 // Ignore nonsensical shifts.
2312 unsigned prec = TYPE_PRECISION (type);
2313 if (wi::ge_p (wi::to_wide (shift),
2314 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2315 UNSIGNED))
2316 return false;
2317 if (wi::to_wide (shift) == 0)
2319 r = lhs;
2320 return true;
2323 // Folding the original operation may discard some impossible
2324 // ranges from the LHS.
2325 int_range_max lhs_refined;
2326 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2327 lhs_refined.intersect (lhs);
2328 if (lhs_refined.undefined_p ())
2330 r.set_undefined ();
2331 return true;
2333 int_range_max shift_range (shift, shift);
2334 int_range_max lb, ub;
2335 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2336 // LHS
2337 // 0000 0111 = OP1 >> 3
2339 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2340 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2341 // right hand side (0x07).
2342 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2343 fold_build2 (LSHIFT_EXPR, type,
2344 build_minus_one_cst (type),
2345 shift));
2346 int_range_max mask_range (build_zero_cst (type), mask);
2347 op_plus.fold_range (ub, type, lb, mask_range);
2348 r = lb;
2349 r.union_ (ub);
2350 if (!lhs_refined.contains_p (build_zero_cst (type)))
2352 mask_range.invert ();
2353 r.intersect (mask_range);
2355 return true;
2357 return false;
2360 bool
2361 operator_rshift::wi_op_overflows (wide_int &res,
2362 tree type,
2363 const wide_int &w0,
2364 const wide_int &w1) const
2366 signop sign = TYPE_SIGN (type);
2367 if (wi::neg_p (w1))
2368 res = wi::lshift (w0, -w1);
2369 else
2371 // It's unclear from the C standard whether shifts can overflow.
2372 // The following code ignores overflow; perhaps a C standard
2373 // interpretation ruling is needed.
2374 res = wi::rshift (w0, w1, sign);
2376 return false;
2379 bool
2380 operator_rshift::fold_range (irange &r, tree type,
2381 const irange &op1,
2382 const irange &op2,
2383 relation_kind rel) const
2385 int_range_max shift;
2386 if (!get_shift_range (shift, type, op2))
2388 if (op2.undefined_p ())
2389 r.set_undefined ();
2390 else
2391 r.set_varying (type);
2392 return true;
2395 return range_operator::fold_range (r, type, op1, shift, rel);
2398 void
2399 operator_rshift::wi_fold (irange &r, tree type,
2400 const wide_int &lh_lb, const wide_int &lh_ub,
2401 const wide_int &rh_lb, const wide_int &rh_ub) const
2403 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2407 class operator_cast: public range_operator
2409 using range_operator::fold_range;
2410 using range_operator::op1_range;
2411 public:
2412 virtual bool fold_range (irange &r, tree type,
2413 const irange &op1,
2414 const irange &op2,
2415 relation_kind rel = VREL_VARYING) const;
2416 virtual bool op1_range (irange &r, tree type,
2417 const irange &lhs,
2418 const irange &op2,
2419 relation_kind rel = VREL_VARYING) const;
2420 private:
2421 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2422 bool inside_domain_p (const wide_int &min, const wide_int &max,
2423 const irange &outer) const;
2424 void fold_pair (irange &r, unsigned index, const irange &inner,
2425 const irange &outer) const;
2426 } op_convert;
2428 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2430 inline bool
2431 operator_cast::truncating_cast_p (const irange &inner,
2432 const irange &outer) const
2434 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2437 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2439 bool
2440 operator_cast::inside_domain_p (const wide_int &min,
2441 const wide_int &max,
2442 const irange &range) const
2444 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2445 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2446 signop domain_sign = TYPE_SIGN (range.type ());
2447 return (wi::le_p (min, domain_max, domain_sign)
2448 && wi::le_p (max, domain_max, domain_sign)
2449 && wi::ge_p (min, domain_min, domain_sign)
2450 && wi::ge_p (max, domain_min, domain_sign));
2454 // Helper for fold_range which work on a pair at a time.
2456 void
2457 operator_cast::fold_pair (irange &r, unsigned index,
2458 const irange &inner,
2459 const irange &outer) const
2461 tree inner_type = inner.type ();
2462 tree outer_type = outer.type ();
2463 signop inner_sign = TYPE_SIGN (inner_type);
2464 unsigned outer_prec = TYPE_PRECISION (outer_type);
2466 // check to see if casting from INNER to OUTER is a conversion that
2467 // fits in the resulting OUTER type.
2468 wide_int inner_lb = inner.lower_bound (index);
2469 wide_int inner_ub = inner.upper_bound (index);
2470 if (truncating_cast_p (inner, outer))
2472 // We may be able to accomodate a truncating cast if the
2473 // resulting range can be represented in the target type...
2474 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2475 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2476 inner_sign) != 0)
2478 r.set_varying (outer_type);
2479 return;
2482 // ...but we must still verify that the final range fits in the
2483 // domain. This catches -fstrict-enum restrictions where the domain
2484 // range is smaller than what fits in the underlying type.
2485 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2486 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2487 if (inside_domain_p (min, max, outer))
2488 create_possibly_reversed_range (r, outer_type, min, max);
2489 else
2490 r.set_varying (outer_type);
2494 bool
2495 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2496 const irange &inner,
2497 const irange &outer,
2498 relation_kind rel ATTRIBUTE_UNUSED) const
2500 if (empty_range_varying (r, type, inner, outer))
2501 return true;
2503 gcc_checking_assert (outer.varying_p ());
2504 gcc_checking_assert (inner.num_pairs () > 0);
2506 // Avoid a temporary by folding the first pair directly into the result.
2507 fold_pair (r, 0, inner, outer);
2509 // Then process any additonal pairs by unioning with their results.
2510 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2512 int_range_max tmp;
2513 fold_pair (tmp, x, inner, outer);
2514 r.union_ (tmp);
2515 if (r.varying_p ())
2516 return true;
2519 // Update the nonzero mask. Truncating casts are problematic unless
2520 // the conversion fits in the resulting outer type.
2521 wide_int nz = inner.get_nonzero_bits ();
2522 if (truncating_cast_p (inner, outer)
2523 && wi::rshift (nz, wi::uhwi (TYPE_PRECISION (outer.type ()),
2524 TYPE_PRECISION (inner.type ())),
2525 TYPE_SIGN (inner.type ())) != 0)
2526 return true;
2527 nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
2528 r.set_nonzero_bits (nz);
2530 return true;
2533 bool
2534 operator_cast::op1_range (irange &r, tree type,
2535 const irange &lhs,
2536 const irange &op2,
2537 relation_kind rel ATTRIBUTE_UNUSED) const
2539 if (lhs.undefined_p ())
2540 return false;
2541 tree lhs_type = lhs.type ();
2542 gcc_checking_assert (types_compatible_p (op2.type(), type));
2544 // If we are calculating a pointer, shortcut to what we really care about.
2545 if (POINTER_TYPE_P (type))
2547 // Conversion from other pointers or a constant (including 0/NULL)
2548 // are straightforward.
2549 if (POINTER_TYPE_P (lhs.type ())
2550 || (lhs.singleton_p ()
2551 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2553 r = lhs;
2554 range_cast (r, type);
2556 else
2558 // If the LHS is not a pointer nor a singleton, then it is
2559 // either VARYING or non-zero.
2560 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2561 r.set_nonzero (type);
2562 else
2563 r.set_varying (type);
2565 r.intersect (op2);
2566 return true;
2569 if (truncating_cast_p (op2, lhs))
2571 if (lhs.varying_p ())
2572 r.set_varying (type);
2573 else
2575 // We want to insert the LHS as an unsigned value since it
2576 // would not trigger the signed bit of the larger type.
2577 int_range_max converted_lhs = lhs;
2578 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2579 range_cast (converted_lhs, type);
2580 // Start by building the positive signed outer range for the type.
2581 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2582 TYPE_PRECISION (type));
2583 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2584 SIGNED));
2585 // For the signed part, we need to simply union the 2 ranges now.
2586 r.union_ (converted_lhs);
2588 // Create maximal negative number outside of LHS bits.
2589 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2590 TYPE_PRECISION (type));
2591 // Add this to the unsigned LHS range(s).
2592 int_range_max lim_range (type, lim, lim);
2593 int_range_max lhs_neg;
2594 range_op_handler (PLUS_EXPR, type).fold_range (lhs_neg, type,
2595 converted_lhs,
2596 lim_range);
2597 // lhs_neg now has all the negative versions of the LHS.
2598 // Now union in all the values from SIGNED MIN (0x80000) to
2599 // lim-1 in order to fill in all the ranges with the upper
2600 // bits set.
2602 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2603 // we don't need to create a range from min to lim-1
2604 // calculate neg range traps trying to create [lim, lim - 1].
2605 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2606 if (lim != min_val)
2608 int_range_max neg (type,
2609 wi::min_value (TYPE_PRECISION (type),
2610 SIGNED),
2611 lim - 1);
2612 lhs_neg.union_ (neg);
2614 // And finally, munge the signed and unsigned portions.
2615 r.union_ (lhs_neg);
2617 // And intersect with any known value passed in the extra operand.
2618 r.intersect (op2);
2619 return true;
2622 int_range_max tmp;
2623 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2624 tmp = lhs;
2625 else
2627 // The cast is not truncating, and the range is restricted to
2628 // the range of the RHS by this assignment.
2630 // Cast the range of the RHS to the type of the LHS.
2631 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2632 // Intersect this with the LHS range will produce the range,
2633 // which will be cast to the RHS type before returning.
2634 tmp.intersect (lhs);
2637 // Cast the calculated range to the type of the RHS.
2638 fold_range (r, type, tmp, int_range<1> (type));
2639 return true;
2643 class operator_logical_and : public range_operator
2645 using range_operator::fold_range;
2646 using range_operator::op1_range;
2647 using range_operator::op2_range;
2648 public:
2649 virtual bool fold_range (irange &r, tree type,
2650 const irange &lh,
2651 const irange &rh,
2652 relation_kind rel = VREL_VARYING) const;
2653 virtual bool op1_range (irange &r, tree type,
2654 const irange &lhs,
2655 const irange &op2,
2656 relation_kind rel = VREL_VARYING) const;
2657 virtual bool op2_range (irange &r, tree type,
2658 const irange &lhs,
2659 const irange &op1,
2660 relation_kind rel = VREL_VARYING) const;
2661 } op_logical_and;
2664 bool
2665 operator_logical_and::fold_range (irange &r, tree type,
2666 const irange &lh,
2667 const irange &rh,
2668 relation_kind rel ATTRIBUTE_UNUSED) const
2670 if (empty_range_varying (r, type, lh, rh))
2671 return true;
2673 // 0 && anything is 0.
2674 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2675 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2676 r = range_false (type);
2677 else if (lh.contains_p (build_zero_cst (lh.type ()))
2678 || rh.contains_p (build_zero_cst (rh.type ())))
2679 // To reach this point, there must be a logical 1 on each side, and
2680 // the only remaining question is whether there is a zero or not.
2681 r = range_true_and_false (type);
2682 else
2683 r = range_true (type);
2684 return true;
2687 bool
2688 operator_logical_and::op1_range (irange &r, tree type,
2689 const irange &lhs,
2690 const irange &op2 ATTRIBUTE_UNUSED,
2691 relation_kind rel ATTRIBUTE_UNUSED) const
2693 switch (get_bool_state (r, lhs, type))
2695 case BRS_TRUE:
2696 // A true result means both sides of the AND must be true.
2697 r = range_true (type);
2698 break;
2699 default:
2700 // Any other result means only one side has to be false, the
2701 // other side can be anything. So we cannot be sure of any
2702 // result here.
2703 r = range_true_and_false (type);
2704 break;
2706 return true;
2709 bool
2710 operator_logical_and::op2_range (irange &r, tree type,
2711 const irange &lhs,
2712 const irange &op1,
2713 relation_kind rel ATTRIBUTE_UNUSED) const
2715 return operator_logical_and::op1_range (r, type, lhs, op1);
2719 class operator_bitwise_and : public range_operator
2721 using range_operator::fold_range;
2722 using range_operator::op1_range;
2723 using range_operator::op2_range;
2724 public:
2725 virtual bool fold_range (irange &r, tree type,
2726 const irange &lh,
2727 const irange &rh,
2728 relation_kind rel = VREL_VARYING) const;
2729 virtual bool op1_range (irange &r, tree type,
2730 const irange &lhs,
2731 const irange &op2,
2732 relation_kind rel = VREL_VARYING) const;
2733 virtual bool op2_range (irange &r, tree type,
2734 const irange &lhs,
2735 const irange &op1,
2736 relation_kind rel = VREL_VARYING) const;
2737 virtual void wi_fold (irange &r, tree type,
2738 const wide_int &lh_lb,
2739 const wide_int &lh_ub,
2740 const wide_int &rh_lb,
2741 const wide_int &rh_ub) const;
2742 private:
2743 void simple_op1_range_solver (irange &r, tree type,
2744 const irange &lhs,
2745 const irange &op2) const;
2746 } op_bitwise_and;
2748 bool
2749 operator_bitwise_and::fold_range (irange &r, tree type,
2750 const irange &lh,
2751 const irange &rh,
2752 relation_kind rel ATTRIBUTE_UNUSED) const
2754 if (range_operator::fold_range (r, type, lh, rh))
2756 if (!lh.undefined_p () && !rh.undefined_p ())
2757 r.set_nonzero_bits (wi::bit_and (lh.get_nonzero_bits (),
2758 rh.get_nonzero_bits ()));
2759 return true;
2761 return false;
2765 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2766 // by considering the number of leading redundant sign bit copies.
2767 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2768 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2769 static bool
2770 wi_optimize_signed_bitwise_op (irange &r, tree type,
2771 const wide_int &lh_lb, const wide_int &lh_ub,
2772 const wide_int &rh_lb, const wide_int &rh_ub)
2774 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2775 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2776 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2777 if (new_clrsb == 0)
2778 return false;
2779 int type_prec = TYPE_PRECISION (type);
2780 int rprec = (type_prec - new_clrsb) - 1;
2781 value_range_with_overflow (r, type,
2782 wi::mask (rprec, true, type_prec),
2783 wi::mask (rprec, false, type_prec));
2784 return true;
2788 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2789 // possible. Basically, see if we can optimize:
2791 // [LB, UB] op Z
2792 // into:
2793 // [LB op Z, UB op Z]
2795 // If the optimization was successful, accumulate the range in R and
2796 // return TRUE.
2798 static bool
2799 wi_optimize_and_or (irange &r,
2800 enum tree_code code,
2801 tree type,
2802 const wide_int &lh_lb, const wide_int &lh_ub,
2803 const wide_int &rh_lb, const wide_int &rh_ub)
2805 // Calculate the singleton mask among the ranges, if any.
2806 wide_int lower_bound, upper_bound, mask;
2807 if (wi::eq_p (rh_lb, rh_ub))
2809 mask = rh_lb;
2810 lower_bound = lh_lb;
2811 upper_bound = lh_ub;
2813 else if (wi::eq_p (lh_lb, lh_ub))
2815 mask = lh_lb;
2816 lower_bound = rh_lb;
2817 upper_bound = rh_ub;
2819 else
2820 return false;
2822 // If Z is a constant which (for op | its bitwise not) has n
2823 // consecutive least significant bits cleared followed by m 1
2824 // consecutive bits set immediately above it and either
2825 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2827 // The least significant n bits of all the values in the range are
2828 // cleared or set, the m bits above it are preserved and any bits
2829 // above these are required to be the same for all values in the
2830 // range.
2831 wide_int w = mask;
2832 int m = 0, n = 0;
2833 if (code == BIT_IOR_EXPR)
2834 w = ~w;
2835 if (wi::eq_p (w, 0))
2836 n = w.get_precision ();
2837 else
2839 n = wi::ctz (w);
2840 w = ~(w | wi::mask (n, false, w.get_precision ()));
2841 if (wi::eq_p (w, 0))
2842 m = w.get_precision () - n;
2843 else
2844 m = wi::ctz (w) - n;
2846 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2847 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2848 return false;
2850 wide_int res_lb, res_ub;
2851 if (code == BIT_AND_EXPR)
2853 res_lb = wi::bit_and (lower_bound, mask);
2854 res_ub = wi::bit_and (upper_bound, mask);
2856 else if (code == BIT_IOR_EXPR)
2858 res_lb = wi::bit_or (lower_bound, mask);
2859 res_ub = wi::bit_or (upper_bound, mask);
2861 else
2862 gcc_unreachable ();
2863 value_range_with_overflow (r, type, res_lb, res_ub);
2865 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2866 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2868 int_range<2> tmp;
2869 tmp.set_nonzero (type);
2870 r.intersect (tmp);
2872 return true;
2875 // For range [LB, UB] compute two wide_int bit masks.
2877 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2878 // for all numbers in the range the bit is 0, otherwise it might be 0
2879 // or 1.
2881 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2882 // for all numbers in the range the bit is 1, otherwise it might be 0
2883 // or 1.
2885 void
2886 wi_set_zero_nonzero_bits (tree type,
2887 const wide_int &lb, const wide_int &ub,
2888 wide_int &maybe_nonzero,
2889 wide_int &mustbe_nonzero)
2891 signop sign = TYPE_SIGN (type);
2893 if (wi::eq_p (lb, ub))
2894 maybe_nonzero = mustbe_nonzero = lb;
2895 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2897 wide_int xor_mask = lb ^ ub;
2898 maybe_nonzero = lb | ub;
2899 mustbe_nonzero = lb & ub;
2900 if (xor_mask != 0)
2902 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2903 maybe_nonzero.get_precision ());
2904 maybe_nonzero = maybe_nonzero | mask;
2905 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2908 else
2910 maybe_nonzero = wi::minus_one (lb.get_precision ());
2911 mustbe_nonzero = wi::zero (lb.get_precision ());
2915 void
2916 operator_bitwise_and::wi_fold (irange &r, tree type,
2917 const wide_int &lh_lb,
2918 const wide_int &lh_ub,
2919 const wide_int &rh_lb,
2920 const wide_int &rh_ub) const
2922 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2923 return;
2925 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2926 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2927 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2928 maybe_nonzero_lh, mustbe_nonzero_lh);
2929 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2930 maybe_nonzero_rh, mustbe_nonzero_rh);
2932 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2933 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2934 signop sign = TYPE_SIGN (type);
2935 unsigned prec = TYPE_PRECISION (type);
2936 // If both input ranges contain only negative values, we can
2937 // truncate the result range maximum to the minimum of the
2938 // input range maxima.
2939 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2941 new_ub = wi::min (new_ub, lh_ub, sign);
2942 new_ub = wi::min (new_ub, rh_ub, sign);
2944 // If either input range contains only non-negative values
2945 // we can truncate the result range maximum to the respective
2946 // maximum of the input range.
2947 if (wi::ge_p (lh_lb, 0, sign))
2948 new_ub = wi::min (new_ub, lh_ub, sign);
2949 if (wi::ge_p (rh_lb, 0, sign))
2950 new_ub = wi::min (new_ub, rh_ub, sign);
2951 // PR68217: In case of signed & sign-bit-CST should
2952 // result in [-INF, 0] instead of [-INF, INF].
2953 if (wi::gt_p (new_lb, new_ub, sign))
2955 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2956 if (sign == SIGNED
2957 && ((wi::eq_p (lh_lb, lh_ub)
2958 && !wi::cmps (lh_lb, sign_bit))
2959 || (wi::eq_p (rh_lb, rh_ub)
2960 && !wi::cmps (rh_lb, sign_bit))))
2962 new_lb = wi::min_value (prec, sign);
2963 new_ub = wi::zero (prec);
2966 // If the limits got swapped around, return varying.
2967 if (wi::gt_p (new_lb, new_ub,sign))
2969 if (sign == SIGNED
2970 && wi_optimize_signed_bitwise_op (r, type,
2971 lh_lb, lh_ub,
2972 rh_lb, rh_ub))
2973 return;
2974 r.set_varying (type);
2976 else
2977 value_range_with_overflow (r, type, new_lb, new_ub);
2980 static void
2981 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2983 if (!lhs.contains_p (build_zero_cst (type)))
2984 r = range_nonzero (type);
2985 else
2986 r.set_varying (type);
2989 // This was shamelessly stolen from register_edge_assert_for_2 and
2990 // adjusted to work with iranges.
2992 void
2993 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2994 const irange &lhs,
2995 const irange &op2) const
2997 if (!op2.singleton_p ())
2999 set_nonzero_range_from_mask (r, type, lhs);
3000 return;
3002 unsigned int nprec = TYPE_PRECISION (type);
3003 wide_int cst2v = op2.lower_bound ();
3004 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
3005 wide_int sgnbit;
3006 if (cst2n)
3007 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3008 else
3009 sgnbit = wi::zero (nprec);
3011 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3013 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3014 // maximum unsigned value is ~0. For signed comparison, if CST2
3015 // doesn't have the most significant bit set, handle it similarly. If
3016 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3017 wide_int valv = lhs.lower_bound ();
3018 wide_int minv = valv & cst2v, maxv;
3019 bool we_know_nothing = false;
3020 if (minv != valv)
3022 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3023 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3024 if (minv == valv)
3026 // If we can't determine anything on this bound, fall
3027 // through and conservatively solve for the other end point.
3028 we_know_nothing = true;
3031 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3032 if (we_know_nothing)
3033 r.set_varying (type);
3034 else
3035 r = int_range<1> (type, minv, maxv);
3037 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3039 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3040 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3041 // VAL2 where
3042 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3043 // as maximum.
3044 // For signed comparison, if CST2 doesn't have most significant bit
3045 // set, handle it similarly. If CST2 has MSB set, the maximum is
3046 // the same and minimum is INT_MIN.
3047 valv = lhs.upper_bound ();
3048 minv = valv & cst2v;
3049 if (minv == valv)
3050 maxv = valv;
3051 else
3053 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3054 if (maxv == valv)
3056 // If we couldn't determine anything on either bound, return
3057 // undefined.
3058 if (we_know_nothing)
3059 r.set_undefined ();
3060 return;
3062 maxv -= 1;
3064 maxv |= ~cst2v;
3065 minv = sgnbit;
3066 int_range<1> upper_bits (type, minv, maxv);
3067 r.intersect (upper_bits);
3070 bool
3071 operator_bitwise_and::op1_range (irange &r, tree type,
3072 const irange &lhs,
3073 const irange &op2,
3074 relation_kind rel ATTRIBUTE_UNUSED) const
3076 if (lhs.undefined_p ())
3077 return false;
3078 if (types_compatible_p (type, boolean_type_node))
3079 return op_logical_and.op1_range (r, type, lhs, op2);
3081 r.set_undefined ();
3082 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
3084 int_range_max chunk (lhs.type (),
3085 lhs.lower_bound (i),
3086 lhs.upper_bound (i));
3087 int_range_max res;
3088 simple_op1_range_solver (res, type, chunk, op2);
3089 r.union_ (res);
3091 if (r.undefined_p ())
3092 set_nonzero_range_from_mask (r, type, lhs);
3094 // For 0 = op1 & MASK, op1 is ~MASK.
3095 if (lhs.zero_p () && op2.singleton_p ())
3097 wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
3098 int_range<2> tmp (type);
3099 tmp.set_nonzero_bits (nz);
3100 r.intersect (tmp);
3102 return true;
3105 bool
3106 operator_bitwise_and::op2_range (irange &r, tree type,
3107 const irange &lhs,
3108 const irange &op1,
3109 relation_kind rel ATTRIBUTE_UNUSED) const
3111 return operator_bitwise_and::op1_range (r, type, lhs, op1);
3115 class operator_logical_or : public range_operator
3117 using range_operator::fold_range;
3118 using range_operator::op1_range;
3119 using range_operator::op2_range;
3120 public:
3121 virtual bool fold_range (irange &r, tree type,
3122 const irange &lh,
3123 const irange &rh,
3124 relation_kind rel = VREL_VARYING) const;
3125 virtual bool op1_range (irange &r, tree type,
3126 const irange &lhs,
3127 const irange &op2,
3128 relation_kind rel = VREL_VARYING) const;
3129 virtual bool op2_range (irange &r, tree type,
3130 const irange &lhs,
3131 const irange &op1,
3132 relation_kind rel = VREL_VARYING) const;
3133 } op_logical_or;
3135 bool
3136 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3137 const irange &lh,
3138 const irange &rh,
3139 relation_kind rel ATTRIBUTE_UNUSED) const
3141 if (empty_range_varying (r, type, lh, rh))
3142 return true;
3144 r = lh;
3145 r.union_ (rh);
3146 return true;
3149 bool
3150 operator_logical_or::op1_range (irange &r, tree type,
3151 const irange &lhs,
3152 const irange &op2 ATTRIBUTE_UNUSED,
3153 relation_kind rel ATTRIBUTE_UNUSED) const
3155 switch (get_bool_state (r, lhs, type))
3157 case BRS_FALSE:
3158 // A false result means both sides of the OR must be false.
3159 r = range_false (type);
3160 break;
3161 default:
3162 // Any other result means only one side has to be true, the
3163 // other side can be anything. so we can't be sure of any result
3164 // here.
3165 r = range_true_and_false (type);
3166 break;
3168 return true;
3171 bool
3172 operator_logical_or::op2_range (irange &r, tree type,
3173 const irange &lhs,
3174 const irange &op1,
3175 relation_kind rel ATTRIBUTE_UNUSED) const
3177 return operator_logical_or::op1_range (r, type, lhs, op1);
3181 class operator_bitwise_or : public range_operator
3183 using range_operator::op1_range;
3184 using range_operator::op2_range;
3185 public:
3186 virtual bool op1_range (irange &r, tree type,
3187 const irange &lhs,
3188 const irange &op2,
3189 relation_kind rel = VREL_VARYING) const;
3190 virtual bool op2_range (irange &r, tree type,
3191 const irange &lhs,
3192 const irange &op1,
3193 relation_kind rel= VREL_VARYING) const;
3194 virtual void wi_fold (irange &r, tree type,
3195 const wide_int &lh_lb,
3196 const wide_int &lh_ub,
3197 const wide_int &rh_lb,
3198 const wide_int &rh_ub) const;
3199 } op_bitwise_or;
3201 void
3202 operator_bitwise_or::wi_fold (irange &r, tree type,
3203 const wide_int &lh_lb,
3204 const wide_int &lh_ub,
3205 const wide_int &rh_lb,
3206 const wide_int &rh_ub) const
3208 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3209 return;
3211 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3212 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3213 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3214 maybe_nonzero_lh, mustbe_nonzero_lh);
3215 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3216 maybe_nonzero_rh, mustbe_nonzero_rh);
3217 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3218 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3219 signop sign = TYPE_SIGN (type);
3220 // If the input ranges contain only positive values we can
3221 // truncate the minimum of the result range to the maximum
3222 // of the input range minima.
3223 if (wi::ge_p (lh_lb, 0, sign)
3224 && wi::ge_p (rh_lb, 0, sign))
3226 new_lb = wi::max (new_lb, lh_lb, sign);
3227 new_lb = wi::max (new_lb, rh_lb, sign);
3229 // If either input range contains only negative values
3230 // we can truncate the minimum of the result range to the
3231 // respective minimum range.
3232 if (wi::lt_p (lh_ub, 0, sign))
3233 new_lb = wi::max (new_lb, lh_lb, sign);
3234 if (wi::lt_p (rh_ub, 0, sign))
3235 new_lb = wi::max (new_lb, rh_lb, sign);
3236 // If the limits got swapped around, return a conservative range.
3237 if (wi::gt_p (new_lb, new_ub, sign))
3239 // Make sure that nonzero|X is nonzero.
3240 if (wi::gt_p (lh_lb, 0, sign)
3241 || wi::gt_p (rh_lb, 0, sign)
3242 || wi::lt_p (lh_ub, 0, sign)
3243 || wi::lt_p (rh_ub, 0, sign))
3244 r.set_nonzero (type);
3245 else if (sign == SIGNED
3246 && wi_optimize_signed_bitwise_op (r, type,
3247 lh_lb, lh_ub,
3248 rh_lb, rh_ub))
3249 return;
3250 else
3251 r.set_varying (type);
3252 return;
3254 value_range_with_overflow (r, type, new_lb, new_ub);
3257 bool
3258 operator_bitwise_or::op1_range (irange &r, tree type,
3259 const irange &lhs,
3260 const irange &op2,
3261 relation_kind rel ATTRIBUTE_UNUSED) const
3263 if (lhs.undefined_p ())
3264 return false;
3265 // If this is really a logical wi_fold, call that.
3266 if (types_compatible_p (type, boolean_type_node))
3267 return op_logical_or.op1_range (r, type, lhs, op2);
3269 if (lhs.zero_p ())
3271 tree zero = build_zero_cst (type);
3272 r = int_range<1> (zero, zero);
3273 return true;
3275 r.set_varying (type);
3276 return true;
3279 bool
3280 operator_bitwise_or::op2_range (irange &r, tree type,
3281 const irange &lhs,
3282 const irange &op1,
3283 relation_kind rel ATTRIBUTE_UNUSED) const
3285 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3289 class operator_bitwise_xor : public range_operator
3291 using range_operator::op1_range;
3292 using range_operator::op2_range;
3293 public:
3294 virtual void wi_fold (irange &r, tree type,
3295 const wide_int &lh_lb,
3296 const wide_int &lh_ub,
3297 const wide_int &rh_lb,
3298 const wide_int &rh_ub) const;
3299 virtual bool op1_range (irange &r, tree type,
3300 const irange &lhs,
3301 const irange &op2,
3302 relation_kind rel = VREL_VARYING) const;
3303 virtual bool op2_range (irange &r, tree type,
3304 const irange &lhs,
3305 const irange &op1,
3306 relation_kind rel = VREL_VARYING) const;
3307 virtual bool op1_op2_relation_effect (irange &lhs_range,
3308 tree type,
3309 const irange &op1_range,
3310 const irange &op2_range,
3311 relation_kind rel) const;
3312 } op_bitwise_xor;
3314 void
3315 operator_bitwise_xor::wi_fold (irange &r, tree type,
3316 const wide_int &lh_lb,
3317 const wide_int &lh_ub,
3318 const wide_int &rh_lb,
3319 const wide_int &rh_ub) const
3321 signop sign = TYPE_SIGN (type);
3322 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3323 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3324 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3325 maybe_nonzero_lh, mustbe_nonzero_lh);
3326 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3327 maybe_nonzero_rh, mustbe_nonzero_rh);
3329 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3330 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3331 wide_int result_one_bits
3332 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3333 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3334 wide_int new_ub = ~result_zero_bits;
3335 wide_int new_lb = result_one_bits;
3337 // If the range has all positive or all negative values, the result
3338 // is better than VARYING.
3339 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3340 value_range_with_overflow (r, type, new_lb, new_ub);
3341 else if (sign == SIGNED
3342 && wi_optimize_signed_bitwise_op (r, type,
3343 lh_lb, lh_ub,
3344 rh_lb, rh_ub))
3345 ; /* Do nothing. */
3346 else
3347 r.set_varying (type);
3349 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3350 if (wi::lt_p (lh_ub, rh_lb, sign)
3351 || wi::lt_p (rh_ub, lh_lb, sign)
3352 || wi::ne_p (result_one_bits, 0))
3354 int_range<2> tmp;
3355 tmp.set_nonzero (type);
3356 r.intersect (tmp);
3360 bool
3361 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3362 tree type,
3363 const irange &,
3364 const irange &,
3365 relation_kind rel) const
3367 if (rel == VREL_VARYING)
3368 return false;
3370 int_range<2> rel_range;
3372 switch (rel)
3374 case VREL_EQ:
3375 rel_range.set_zero (type);
3376 break;
3377 case VREL_NE:
3378 rel_range.set_nonzero (type);
3379 break;
3380 default:
3381 return false;
3384 lhs_range.intersect (rel_range);
3385 return true;
3388 bool
3389 operator_bitwise_xor::op1_range (irange &r, tree type,
3390 const irange &lhs,
3391 const irange &op2,
3392 relation_kind rel ATTRIBUTE_UNUSED) const
3394 if (lhs.undefined_p () || lhs.varying_p ())
3396 r = lhs;
3397 return true;
3399 if (types_compatible_p (type, boolean_type_node))
3401 switch (get_bool_state (r, lhs, type))
3403 case BRS_TRUE:
3404 if (op2.varying_p ())
3405 r.set_varying (type);
3406 else if (op2.zero_p ())
3407 r = range_true (type);
3408 else
3409 r = range_false (type);
3410 break;
3411 case BRS_FALSE:
3412 r = op2;
3413 break;
3414 default:
3415 break;
3417 return true;
3419 r.set_varying (type);
3420 return true;
3423 bool
3424 operator_bitwise_xor::op2_range (irange &r, tree type,
3425 const irange &lhs,
3426 const irange &op1,
3427 relation_kind rel ATTRIBUTE_UNUSED) const
3429 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3432 class operator_trunc_mod : public range_operator
3434 using range_operator::op1_range;
3435 using range_operator::op2_range;
3436 public:
3437 virtual void wi_fold (irange &r, tree type,
3438 const wide_int &lh_lb,
3439 const wide_int &lh_ub,
3440 const wide_int &rh_lb,
3441 const wide_int &rh_ub) const;
3442 virtual bool op1_range (irange &r, tree type,
3443 const irange &lhs,
3444 const irange &op2,
3445 relation_kind rel ATTRIBUTE_UNUSED) const;
3446 virtual bool op2_range (irange &r, tree type,
3447 const irange &lhs,
3448 const irange &op1,
3449 relation_kind rel ATTRIBUTE_UNUSED) const;
3450 } op_trunc_mod;
3452 void
3453 operator_trunc_mod::wi_fold (irange &r, tree type,
3454 const wide_int &lh_lb,
3455 const wide_int &lh_ub,
3456 const wide_int &rh_lb,
3457 const wide_int &rh_ub) const
3459 wide_int new_lb, new_ub, tmp;
3460 signop sign = TYPE_SIGN (type);
3461 unsigned prec = TYPE_PRECISION (type);
3463 // Mod 0 is undefined.
3464 if (wi_zero_p (type, rh_lb, rh_ub))
3466 r.set_undefined ();
3467 return;
3470 // Check for constant and try to fold.
3471 if (lh_lb == lh_ub && rh_lb == rh_ub)
3473 wi::overflow_type ov = wi::OVF_NONE;
3474 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3475 if (ov == wi::OVF_NONE)
3477 r = int_range<2> (type, tmp, tmp);
3478 return;
3482 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3483 new_ub = rh_ub - 1;
3484 if (sign == SIGNED)
3486 tmp = -1 - rh_lb;
3487 new_ub = wi::smax (new_ub, tmp);
3490 if (sign == UNSIGNED)
3491 new_lb = wi::zero (prec);
3492 else
3494 new_lb = -new_ub;
3495 tmp = lh_lb;
3496 if (wi::gts_p (tmp, 0))
3497 tmp = wi::zero (prec);
3498 new_lb = wi::smax (new_lb, tmp);
3500 tmp = lh_ub;
3501 if (sign == SIGNED && wi::neg_p (tmp))
3502 tmp = wi::zero (prec);
3503 new_ub = wi::min (new_ub, tmp, sign);
3505 value_range_with_overflow (r, type, new_lb, new_ub);
3508 bool
3509 operator_trunc_mod::op1_range (irange &r, tree type,
3510 const irange &lhs,
3511 const irange &,
3512 relation_kind rel ATTRIBUTE_UNUSED) const
3514 if (lhs.undefined_p ())
3515 return false;
3516 // PR 91029.
3517 signop sign = TYPE_SIGN (type);
3518 unsigned prec = TYPE_PRECISION (type);
3519 // (a % b) >= x && x > 0 , then a >= x.
3520 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3522 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3523 return true;
3525 // (a % b) <= x && x < 0 , then a <= x.
3526 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3528 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3529 return true;
3531 return false;
3534 bool
3535 operator_trunc_mod::op2_range (irange &r, tree type,
3536 const irange &lhs,
3537 const irange &,
3538 relation_kind rel ATTRIBUTE_UNUSED) const
3540 if (lhs.undefined_p ())
3541 return false;
3542 // PR 91029.
3543 signop sign = TYPE_SIGN (type);
3544 unsigned prec = TYPE_PRECISION (type);
3545 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3546 // or b > x for unsigned.
3547 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3549 if (sign == SIGNED)
3550 r = value_range (type, wi::neg (lhs.lower_bound ()),
3551 lhs.lower_bound (), VR_ANTI_RANGE);
3552 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3553 sign))
3554 r = value_range (type, lhs.lower_bound () + 1,
3555 wi::max_value (prec, sign));
3556 else
3557 return false;
3558 return true;
3560 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3561 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3563 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3564 r = value_range (type, lhs.upper_bound (),
3565 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3566 else
3567 return false;
3568 return true;
3570 return false;
3574 class operator_logical_not : public range_operator
3576 using range_operator::fold_range;
3577 using range_operator::op1_range;
3578 public:
3579 virtual bool fold_range (irange &r, tree type,
3580 const irange &lh,
3581 const irange &rh,
3582 relation_kind rel = VREL_VARYING) const;
3583 virtual bool op1_range (irange &r, tree type,
3584 const irange &lhs,
3585 const irange &op2,
3586 relation_kind rel = VREL_VARYING) const;
3587 } op_logical_not;
3589 // Folding a logical NOT, oddly enough, involves doing nothing on the
3590 // forward pass through. During the initial walk backwards, the
3591 // logical NOT reversed the desired outcome on the way back, so on the
3592 // way forward all we do is pass the range forward.
3594 // b_2 = x_1 < 20
3595 // b_3 = !b_2
3596 // if (b_3)
3597 // to determine the TRUE branch, walking backward
3598 // if (b_3) if ([1,1])
3599 // b_3 = !b_2 [1,1] = ![0,0]
3600 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3601 // which is the result we are looking for.. so.. pass it through.
3603 bool
3604 operator_logical_not::fold_range (irange &r, tree type,
3605 const irange &lh,
3606 const irange &rh ATTRIBUTE_UNUSED,
3607 relation_kind rel ATTRIBUTE_UNUSED) const
3609 if (empty_range_varying (r, type, lh, rh))
3610 return true;
3612 r = lh;
3613 if (!lh.varying_p () && !lh.undefined_p ())
3614 r.invert ();
3616 return true;
3619 bool
3620 operator_logical_not::op1_range (irange &r,
3621 tree type,
3622 const irange &lhs,
3623 const irange &op2,
3624 relation_kind rel ATTRIBUTE_UNUSED) const
3626 // Logical NOT is involutary...do it again.
3627 return fold_range (r, type, lhs, op2);
3631 class operator_bitwise_not : public range_operator
3633 using range_operator::fold_range;
3634 using range_operator::op1_range;
3635 public:
3636 virtual bool fold_range (irange &r, tree type,
3637 const irange &lh,
3638 const irange &rh,
3639 relation_kind rel = VREL_VARYING) const;
3640 virtual bool op1_range (irange &r, tree type,
3641 const irange &lhs,
3642 const irange &op2,
3643 relation_kind rel = VREL_VARYING) const;
3644 } op_bitwise_not;
3646 bool
3647 operator_bitwise_not::fold_range (irange &r, tree type,
3648 const irange &lh,
3649 const irange &rh,
3650 relation_kind rel ATTRIBUTE_UNUSED) const
3652 if (empty_range_varying (r, type, lh, rh))
3653 return true;
3655 if (types_compatible_p (type, boolean_type_node))
3656 return op_logical_not.fold_range (r, type, lh, rh);
3658 // ~X is simply -1 - X.
3659 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3660 wi::minus_one (TYPE_PRECISION (type)));
3661 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, minusone, lh);
3664 bool
3665 operator_bitwise_not::op1_range (irange &r, tree type,
3666 const irange &lhs,
3667 const irange &op2,
3668 relation_kind rel ATTRIBUTE_UNUSED) const
3670 if (lhs.undefined_p ())
3671 return false;
3672 if (types_compatible_p (type, boolean_type_node))
3673 return op_logical_not.op1_range (r, type, lhs, op2);
3675 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3676 return fold_range (r, type, lhs, op2);
3680 class operator_cst : public range_operator
3682 using range_operator::fold_range;
3683 public:
3684 virtual bool fold_range (irange &r, tree type,
3685 const irange &op1,
3686 const irange &op2,
3687 relation_kind rel = VREL_VARYING) const;
3688 } op_integer_cst;
3690 bool
3691 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3692 const irange &lh,
3693 const irange &rh ATTRIBUTE_UNUSED,
3694 relation_kind rel ATTRIBUTE_UNUSED) const
3696 r = lh;
3697 return true;
3701 class operator_identity : public range_operator
3703 using range_operator::fold_range;
3704 using range_operator::op1_range;
3705 using range_operator::lhs_op1_relation;
3706 public:
3707 virtual bool fold_range (irange &r, tree type,
3708 const irange &op1,
3709 const irange &op2,
3710 relation_kind rel = VREL_VARYING) const;
3711 virtual bool op1_range (irange &r, tree type,
3712 const irange &lhs,
3713 const irange &op2,
3714 relation_kind rel = VREL_VARYING) const;
3715 virtual relation_kind lhs_op1_relation (const irange &lhs,
3716 const irange &op1,
3717 const irange &op2,
3718 relation_kind rel) const;
3719 } op_identity;
3721 // Determine if there is a relationship between LHS and OP1.
3723 relation_kind
3724 operator_identity::lhs_op1_relation (const irange &lhs,
3725 const irange &op1 ATTRIBUTE_UNUSED,
3726 const irange &op2 ATTRIBUTE_UNUSED,
3727 relation_kind) const
3729 if (lhs.undefined_p ())
3730 return VREL_VARYING;
3731 // Simply a copy, so they are equivalent.
3732 return VREL_EQ;
3735 bool
3736 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3737 const irange &lh,
3738 const irange &rh ATTRIBUTE_UNUSED,
3739 relation_kind rel ATTRIBUTE_UNUSED) const
3741 r = lh;
3742 return true;
3745 bool
3746 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3747 const irange &lhs,
3748 const irange &op2 ATTRIBUTE_UNUSED,
3749 relation_kind rel ATTRIBUTE_UNUSED) const
3751 r = lhs;
3752 return true;
3756 class operator_unknown : public range_operator
3758 using range_operator::fold_range;
3759 public:
3760 virtual bool fold_range (irange &r, tree type,
3761 const irange &op1,
3762 const irange &op2,
3763 relation_kind rel = VREL_VARYING) const;
3764 } op_unknown;
3766 bool
3767 operator_unknown::fold_range (irange &r, tree type,
3768 const irange &lh ATTRIBUTE_UNUSED,
3769 const irange &rh ATTRIBUTE_UNUSED,
3770 relation_kind rel ATTRIBUTE_UNUSED) const
3772 r.set_varying (type);
3773 return true;
3777 class operator_abs : public range_operator
3779 using range_operator::op1_range;
3780 public:
3781 virtual void wi_fold (irange &r, tree type,
3782 const wide_int &lh_lb,
3783 const wide_int &lh_ub,
3784 const wide_int &rh_lb,
3785 const wide_int &rh_ub) const;
3786 virtual bool op1_range (irange &r, tree type,
3787 const irange &lhs,
3788 const irange &op2,
3789 relation_kind rel ATTRIBUTE_UNUSED) const;
3790 } op_abs;
3792 void
3793 operator_abs::wi_fold (irange &r, tree type,
3794 const wide_int &lh_lb, const wide_int &lh_ub,
3795 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3796 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3798 wide_int min, max;
3799 signop sign = TYPE_SIGN (type);
3800 unsigned prec = TYPE_PRECISION (type);
3802 // Pass through LH for the easy cases.
3803 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
3805 r = int_range<1> (type, lh_lb, lh_ub);
3806 return;
3809 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
3810 // a useful range.
3811 wide_int min_value = wi::min_value (prec, sign);
3812 wide_int max_value = wi::max_value (prec, sign);
3813 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
3815 r.set_varying (type);
3816 return;
3819 // ABS_EXPR may flip the range around, if the original range
3820 // included negative values.
3821 if (wi::eq_p (lh_lb, min_value))
3823 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
3824 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
3825 if (wi::eq_p (lh_ub, min_value))
3827 r = int_range<1> (type, min_value, min_value);
3828 return;
3830 min = max_value;
3832 else
3833 min = wi::abs (lh_lb);
3835 if (wi::eq_p (lh_ub, min_value))
3836 max = max_value;
3837 else
3838 max = wi::abs (lh_ub);
3840 // If the range contains zero then we know that the minimum value in the
3841 // range will be zero.
3842 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3844 if (wi::gt_p (min, max, sign))
3845 max = min;
3846 min = wi::zero (prec);
3848 else
3850 // If the range was reversed, swap MIN and MAX.
3851 if (wi::gt_p (min, max, sign))
3852 std::swap (min, max);
3855 // If the new range has its limits swapped around (MIN > MAX), then
3856 // the operation caused one of them to wrap around. The only thing
3857 // we know is that the result is positive.
3858 if (wi::gt_p (min, max, sign))
3860 min = wi::zero (prec);
3861 max = max_value;
3863 r = int_range<1> (type, min, max);
3866 bool
3867 operator_abs::op1_range (irange &r, tree type,
3868 const irange &lhs,
3869 const irange &op2,
3870 relation_kind rel ATTRIBUTE_UNUSED) const
3872 if (empty_range_varying (r, type, lhs, op2))
3873 return true;
3874 if (TYPE_UNSIGNED (type))
3876 r = lhs;
3877 return true;
3879 // Start with the positives because negatives are an impossible result.
3880 int_range_max positives = range_positives (type);
3881 positives.intersect (lhs);
3882 r = positives;
3883 // Then add the negative of each pair:
3884 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3885 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3886 r.union_ (int_range<1> (type,
3887 -positives.upper_bound (i),
3888 -positives.lower_bound (i)));
3889 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
3890 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
3891 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3892 wide_int lb = lhs.lower_bound ();
3893 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
3894 r.union_ (int_range<2> (type, lb, lb));
3895 return true;
3899 class operator_absu : public range_operator
3901 public:
3902 virtual void wi_fold (irange &r, tree type,
3903 const wide_int &lh_lb, const wide_int &lh_ub,
3904 const wide_int &rh_lb, const wide_int &rh_ub) const;
3905 } op_absu;
3907 void
3908 operator_absu::wi_fold (irange &r, tree type,
3909 const wide_int &lh_lb, const wide_int &lh_ub,
3910 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3911 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3913 wide_int new_lb, new_ub;
3915 // Pass through VR0 the easy cases.
3916 if (wi::ges_p (lh_lb, 0))
3918 new_lb = lh_lb;
3919 new_ub = lh_ub;
3921 else
3923 new_lb = wi::abs (lh_lb);
3924 new_ub = wi::abs (lh_ub);
3926 // If the range contains zero then we know that the minimum
3927 // value in the range will be zero.
3928 if (wi::ges_p (lh_ub, 0))
3930 if (wi::gtu_p (new_lb, new_ub))
3931 new_ub = new_lb;
3932 new_lb = wi::zero (TYPE_PRECISION (type));
3934 else
3935 std::swap (new_lb, new_ub);
3938 gcc_checking_assert (TYPE_UNSIGNED (type));
3939 r = int_range<1> (type, new_lb, new_ub);
3943 class operator_negate : public range_operator
3945 using range_operator::fold_range;
3946 using range_operator::op1_range;
3947 public:
3948 virtual bool fold_range (irange &r, tree type,
3949 const irange &op1,
3950 const irange &op2,
3951 relation_kind rel = VREL_VARYING) const;
3952 virtual bool op1_range (irange &r, tree type,
3953 const irange &lhs,
3954 const irange &op2,
3955 relation_kind rel = VREL_VARYING) const;
3956 } op_negate;
3958 bool
3959 operator_negate::fold_range (irange &r, tree type,
3960 const irange &lh,
3961 const irange &rh,
3962 relation_kind rel ATTRIBUTE_UNUSED) const
3964 if (empty_range_varying (r, type, lh, rh))
3965 return true;
3966 // -X is simply 0 - X.
3967 return range_op_handler (MINUS_EXPR, type).fold_range (r, type,
3968 range_zero (type), lh);
3971 bool
3972 operator_negate::op1_range (irange &r, tree type,
3973 const irange &lhs,
3974 const irange &op2,
3975 relation_kind rel ATTRIBUTE_UNUSED) const
3977 // NEGATE is involutory.
3978 return fold_range (r, type, lhs, op2);
3982 class operator_addr_expr : public range_operator
3984 using range_operator::fold_range;
3985 using range_operator::op1_range;
3986 public:
3987 virtual bool fold_range (irange &r, tree type,
3988 const irange &op1,
3989 const irange &op2,
3990 relation_kind rel = VREL_VARYING) const;
3991 virtual bool op1_range (irange &r, tree type,
3992 const irange &lhs,
3993 const irange &op2,
3994 relation_kind rel = VREL_VARYING) const;
3995 } op_addr;
3997 bool
3998 operator_addr_expr::fold_range (irange &r, tree type,
3999 const irange &lh,
4000 const irange &rh,
4001 relation_kind rel ATTRIBUTE_UNUSED) const
4003 if (empty_range_varying (r, type, lh, rh))
4004 return true;
4006 // Return a non-null pointer of the LHS type (passed in op2).
4007 if (lh.zero_p ())
4008 r = range_zero (type);
4009 else if (!lh.contains_p (build_zero_cst (lh.type ())))
4010 r = range_nonzero (type);
4011 else
4012 r.set_varying (type);
4013 return true;
4016 bool
4017 operator_addr_expr::op1_range (irange &r, tree type,
4018 const irange &lhs,
4019 const irange &op2,
4020 relation_kind rel ATTRIBUTE_UNUSED) const
4022 return operator_addr_expr::fold_range (r, type, lhs, op2);
4026 class pointer_plus_operator : public range_operator
4028 public:
4029 virtual void wi_fold (irange &r, tree type,
4030 const wide_int &lh_lb,
4031 const wide_int &lh_ub,
4032 const wide_int &rh_lb,
4033 const wide_int &rh_ub) const;
4034 } op_pointer_plus;
4036 void
4037 pointer_plus_operator::wi_fold (irange &r, tree type,
4038 const wide_int &lh_lb,
4039 const wide_int &lh_ub,
4040 const wide_int &rh_lb,
4041 const wide_int &rh_ub) const
4043 // Check for [0,0] + const, and simply return the const.
4044 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
4046 tree val = wide_int_to_tree (type, rh_lb);
4047 r.set (val, val);
4048 return;
4051 // For pointer types, we are really only interested in asserting
4052 // whether the expression evaluates to non-NULL.
4054 // With -fno-delete-null-pointer-checks we need to be more
4055 // conservative. As some object might reside at address 0,
4056 // then some offset could be added to it and the same offset
4057 // subtracted again and the result would be NULL.
4058 // E.g.
4059 // static int a[12]; where &a[0] is NULL and
4060 // ptr = &a[6];
4061 // ptr -= 6;
4062 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
4063 // where the first range doesn't include zero and the second one
4064 // doesn't either. As the second operand is sizetype (unsigned),
4065 // consider all ranges where the MSB could be set as possible
4066 // subtractions where the result might be NULL.
4067 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
4068 || !wi_includes_zero_p (type, rh_lb, rh_ub))
4069 && !TYPE_OVERFLOW_WRAPS (type)
4070 && (flag_delete_null_pointer_checks
4071 || !wi::sign_mask (rh_ub)))
4072 r = range_nonzero (type);
4073 else if (lh_lb == lh_ub && lh_lb == 0
4074 && rh_lb == rh_ub && rh_lb == 0)
4075 r = range_zero (type);
4076 else
4077 r.set_varying (type);
4081 class pointer_min_max_operator : public range_operator
4083 public:
4084 virtual void wi_fold (irange & r, tree type,
4085 const wide_int &lh_lb, const wide_int &lh_ub,
4086 const wide_int &rh_lb, const wide_int &rh_ub) const;
4087 } op_ptr_min_max;
4089 void
4090 pointer_min_max_operator::wi_fold (irange &r, tree type,
4091 const wide_int &lh_lb,
4092 const wide_int &lh_ub,
4093 const wide_int &rh_lb,
4094 const wide_int &rh_ub) const
4096 // For MIN/MAX expressions with pointers, we only care about
4097 // nullness. If both are non null, then the result is nonnull.
4098 // If both are null, then the result is null. Otherwise they
4099 // are varying.
4100 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4101 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4102 r = range_nonzero (type);
4103 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4104 r = range_zero (type);
4105 else
4106 r.set_varying (type);
4110 class pointer_and_operator : public range_operator
4112 public:
4113 virtual void wi_fold (irange &r, tree type,
4114 const wide_int &lh_lb, const wide_int &lh_ub,
4115 const wide_int &rh_lb, const wide_int &rh_ub) const;
4116 } op_pointer_and;
4118 void
4119 pointer_and_operator::wi_fold (irange &r, tree type,
4120 const wide_int &lh_lb,
4121 const wide_int &lh_ub,
4122 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4123 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4125 // For pointer types, we are really only interested in asserting
4126 // whether the expression evaluates to non-NULL.
4127 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
4128 r = range_zero (type);
4129 else
4130 r.set_varying (type);
4134 class pointer_or_operator : public range_operator
4136 using range_operator::op1_range;
4137 using range_operator::op2_range;
4138 public:
4139 virtual bool op1_range (irange &r, tree type,
4140 const irange &lhs,
4141 const irange &op2,
4142 relation_kind rel = VREL_VARYING) const;
4143 virtual bool op2_range (irange &r, tree type,
4144 const irange &lhs,
4145 const irange &op1,
4146 relation_kind rel = VREL_VARYING) const;
4147 virtual void wi_fold (irange &r, tree type,
4148 const wide_int &lh_lb, const wide_int &lh_ub,
4149 const wide_int &rh_lb, const wide_int &rh_ub) const;
4150 } op_pointer_or;
4152 bool
4153 pointer_or_operator::op1_range (irange &r, tree type,
4154 const irange &lhs,
4155 const irange &op2 ATTRIBUTE_UNUSED,
4156 relation_kind rel ATTRIBUTE_UNUSED) const
4158 if (lhs.undefined_p ())
4159 return false;
4160 if (lhs.zero_p ())
4162 tree zero = build_zero_cst (type);
4163 r = int_range<1> (zero, zero);
4164 return true;
4166 r.set_varying (type);
4167 return true;
4170 bool
4171 pointer_or_operator::op2_range (irange &r, tree type,
4172 const irange &lhs,
4173 const irange &op1,
4174 relation_kind rel ATTRIBUTE_UNUSED) const
4176 return pointer_or_operator::op1_range (r, type, lhs, op1);
4179 void
4180 pointer_or_operator::wi_fold (irange &r, tree type,
4181 const wide_int &lh_lb,
4182 const wide_int &lh_ub,
4183 const wide_int &rh_lb,
4184 const wide_int &rh_ub) const
4186 // For pointer types, we are really only interested in asserting
4187 // whether the expression evaluates to non-NULL.
4188 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4189 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4190 r = range_nonzero (type);
4191 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4192 r = range_zero (type);
4193 else
4194 r.set_varying (type);
4197 // Return a pointer to the range_operator instance, if there is one
4198 // associated with tree_code CODE.
4200 range_operator *
4201 range_op_table::operator[] (enum tree_code code)
4203 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4204 return m_range_tree[code];
4207 // Add OP to the handler table for CODE.
4209 void
4210 range_op_table::set (enum tree_code code, range_operator &op)
4212 gcc_checking_assert (m_range_tree[code] == NULL);
4213 m_range_tree[code] = &op;
4216 // Instantiate a range op table for integral operations.
4218 class integral_table : public range_op_table
4220 public:
4221 integral_table ();
4222 } integral_tree_table;
4224 integral_table::integral_table ()
4226 set (EQ_EXPR, op_equal);
4227 set (NE_EXPR, op_not_equal);
4228 set (LT_EXPR, op_lt);
4229 set (LE_EXPR, op_le);
4230 set (GT_EXPR, op_gt);
4231 set (GE_EXPR, op_ge);
4232 set (PLUS_EXPR, op_plus);
4233 set (MINUS_EXPR, op_minus);
4234 set (MIN_EXPR, op_min);
4235 set (MAX_EXPR, op_max);
4236 set (MULT_EXPR, op_mult);
4237 set (TRUNC_DIV_EXPR, op_trunc_div);
4238 set (FLOOR_DIV_EXPR, op_floor_div);
4239 set (ROUND_DIV_EXPR, op_round_div);
4240 set (CEIL_DIV_EXPR, op_ceil_div);
4241 set (EXACT_DIV_EXPR, op_exact_div);
4242 set (LSHIFT_EXPR, op_lshift);
4243 set (RSHIFT_EXPR, op_rshift);
4244 set (NOP_EXPR, op_convert);
4245 set (CONVERT_EXPR, op_convert);
4246 set (TRUTH_AND_EXPR, op_logical_and);
4247 set (BIT_AND_EXPR, op_bitwise_and);
4248 set (TRUTH_OR_EXPR, op_logical_or);
4249 set (BIT_IOR_EXPR, op_bitwise_or);
4250 set (BIT_XOR_EXPR, op_bitwise_xor);
4251 set (TRUNC_MOD_EXPR, op_trunc_mod);
4252 set (TRUTH_NOT_EXPR, op_logical_not);
4253 set (BIT_NOT_EXPR, op_bitwise_not);
4254 set (INTEGER_CST, op_integer_cst);
4255 set (SSA_NAME, op_identity);
4256 set (PAREN_EXPR, op_identity);
4257 set (OBJ_TYPE_REF, op_identity);
4258 set (IMAGPART_EXPR, op_unknown);
4259 set (REALPART_EXPR, op_unknown);
4260 set (POINTER_DIFF_EXPR, op_pointer_diff);
4261 set (ABS_EXPR, op_abs);
4262 set (ABSU_EXPR, op_absu);
4263 set (NEGATE_EXPR, op_negate);
4264 set (ADDR_EXPR, op_addr);
4267 // Instantiate a range op table for pointer operations.
4269 class pointer_table : public range_op_table
4271 public:
4272 pointer_table ();
4273 } pointer_tree_table;
4275 pointer_table::pointer_table ()
4277 set (BIT_AND_EXPR, op_pointer_and);
4278 set (BIT_IOR_EXPR, op_pointer_or);
4279 set (MIN_EXPR, op_ptr_min_max);
4280 set (MAX_EXPR, op_ptr_min_max);
4281 set (POINTER_PLUS_EXPR, op_pointer_plus);
4283 set (EQ_EXPR, op_equal);
4284 set (NE_EXPR, op_not_equal);
4285 set (LT_EXPR, op_lt);
4286 set (LE_EXPR, op_le);
4287 set (GT_EXPR, op_gt);
4288 set (GE_EXPR, op_ge);
4289 set (SSA_NAME, op_identity);
4290 set (INTEGER_CST, op_integer_cst);
4291 set (ADDR_EXPR, op_addr);
4292 set (NOP_EXPR, op_convert);
4293 set (CONVERT_EXPR, op_convert);
4295 set (BIT_NOT_EXPR, op_bitwise_not);
4296 set (BIT_XOR_EXPR, op_bitwise_xor);
4299 // The tables are hidden and accessed via a simple extern function.
4301 static inline range_operator *
4302 get_handler (enum tree_code code, tree type)
4304 // First check if there is a pointer specialization.
4305 if (POINTER_TYPE_P (type))
4306 return pointer_tree_table[code];
4307 if (INTEGRAL_TYPE_P (type))
4308 return integral_tree_table[code];
4309 return NULL;
4312 // Return the floating point operator for CODE or NULL if none available.
4314 static inline range_operator_float *
4315 get_float_handler (enum tree_code code, tree)
4317 return (*floating_tree_table)[code];
4320 void
4321 range_op_handler::set_op_handler (tree_code code, tree type)
4323 if (irange::supports_p (type))
4325 m_float = NULL;
4326 m_int = get_handler (code, type);
4327 m_valid = m_int != NULL;
4329 else if (frange::supports_p (type))
4331 m_int = NULL;
4332 m_float = get_float_handler (code, type);
4333 m_valid = m_float != NULL;
4335 else
4337 m_int = NULL;
4338 m_float = NULL;
4339 m_valid = false;
4343 range_op_handler::range_op_handler ()
4345 m_int = NULL;
4346 m_float = NULL;
4347 m_valid = false;
4350 range_op_handler::range_op_handler (tree_code code, tree type)
4352 set_op_handler (code, type);
4356 bool
4357 range_op_handler::fold_range (vrange &r, tree type,
4358 const vrange &lh,
4359 const vrange &rh,
4360 relation_kind rel) const
4362 gcc_checking_assert (m_valid);
4363 if (m_int)
4364 return m_int->fold_range (as_a <irange> (r), type,
4365 as_a <irange> (lh),
4366 as_a <irange> (rh), rel);
4368 if (is_a <irange> (r))
4370 if (is_a <irange> (rh))
4371 return m_float->fold_range (as_a <irange> (r), type,
4372 as_a <frange> (lh),
4373 as_a <irange> (rh), rel);
4374 else
4375 return m_float->fold_range (as_a <irange> (r), type,
4376 as_a <frange> (lh),
4377 as_a <frange> (rh), rel);
4379 return m_float->fold_range (as_a <frange> (r), type,
4380 as_a <frange> (lh),
4381 as_a <frange> (rh), rel);
4384 bool
4385 range_op_handler::op1_range (vrange &r, tree type,
4386 const vrange &lhs,
4387 const vrange &op2,
4388 relation_kind rel) const
4390 gcc_checking_assert (m_valid);
4392 if (lhs.undefined_p ())
4393 return false;
4394 if (m_int)
4395 return m_int->op1_range (as_a <irange> (r), type,
4396 as_a <irange> (lhs),
4397 as_a <irange> (op2), rel);
4399 if (is_a <irange> (lhs))
4400 return m_float->op1_range (as_a <frange> (r), type,
4401 as_a <irange> (lhs),
4402 as_a <frange> (op2), rel);
4403 return m_float->op1_range (as_a <frange> (r), type,
4404 as_a <frange> (lhs),
4405 as_a <frange> (op2), rel);
4408 bool
4409 range_op_handler::op2_range (vrange &r, tree type,
4410 const vrange &lhs,
4411 const vrange &op1,
4412 relation_kind rel) const
4414 gcc_checking_assert (m_valid);
4415 if (lhs.undefined_p ())
4416 return false;
4417 if (m_int)
4418 return m_int->op2_range (as_a <irange> (r), type,
4419 as_a <irange> (lhs),
4420 as_a <irange> (op1), rel);
4422 if (is_a <irange> (lhs))
4423 return m_float->op2_range (as_a <frange> (r), type,
4424 as_a <irange> (lhs),
4425 as_a <frange> (op1), rel);
4426 return m_float->op2_range (as_a <frange> (r), type,
4427 as_a <frange> (lhs),
4428 as_a <frange> (op1), rel);
4431 relation_kind
4432 range_op_handler::lhs_op1_relation (const vrange &lhs,
4433 const vrange &op1,
4434 const vrange &op2,
4435 relation_kind rel) const
4437 gcc_checking_assert (m_valid);
4438 if (m_int)
4439 return m_int->lhs_op1_relation (as_a <irange> (lhs),
4440 as_a <irange> (op1),
4441 as_a <irange> (op2), rel);
4443 if (is_a <irange> (lhs))
4444 return m_float->lhs_op1_relation (as_a <irange> (lhs),
4445 as_a <frange> (op1),
4446 as_a <frange> (op2), rel);
4447 return m_float->lhs_op1_relation (as_a <frange> (lhs),
4448 as_a <frange> (op1),
4449 as_a <frange> (op2), rel);
4452 relation_kind
4453 range_op_handler::lhs_op2_relation (const vrange &lhs,
4454 const vrange &op1,
4455 const vrange &op2,
4456 relation_kind rel) const
4458 gcc_checking_assert (m_valid);
4459 if (m_int)
4460 return m_int->lhs_op2_relation (as_a <irange> (lhs),
4461 as_a <irange> (op1),
4462 as_a <irange> (op2), rel);
4464 if (is_a <irange> (lhs))
4465 return m_float->lhs_op2_relation (as_a <irange> (lhs),
4466 as_a <frange> (op1),
4467 as_a <frange> (op2), rel);
4468 return m_float->lhs_op2_relation (as_a <frange> (lhs),
4469 as_a <frange> (op1),
4470 as_a <frange> (op2), rel);
4473 relation_kind
4474 range_op_handler::op1_op2_relation (const vrange &lhs) const
4476 gcc_checking_assert (m_valid);
4477 if (m_int)
4478 return m_int->op1_op2_relation (as_a <irange> (lhs));
4479 return m_float->op1_op2_relation (as_a <irange> (lhs));
4482 // Cast the range in R to TYPE.
4484 bool
4485 range_cast (vrange &r, tree type)
4487 Value_Range tmp (r);
4488 Value_Range varying (type);
4489 varying.set_varying (type);
4490 range_op_handler op (CONVERT_EXPR, type);
4491 // Call op_convert, if it fails, the result is varying.
4492 if (!op || !op.fold_range (r, type, tmp, varying))
4494 r.set_varying (type);
4495 return false;
4497 return true;
4500 #if CHECKING_P
4501 #include "selftest.h"
4503 namespace selftest
4505 #define INT(N) build_int_cst (integer_type_node, (N))
4506 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4507 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4508 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4509 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4510 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4512 static void
4513 range_op_cast_tests ()
4515 int_range<1> r0, r1, r2, rold;
4516 r0.set_varying (integer_type_node);
4517 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4519 // If a range is in any way outside of the range for the converted
4520 // to range, default to the range for the new type.
4521 r0.set_varying (short_integer_type_node);
4522 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4523 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4524 if (TYPE_PRECISION (TREE_TYPE (maxint))
4525 > TYPE_PRECISION (short_integer_type_node))
4527 r1 = int_range<1> (integer_zero_node, maxint);
4528 range_cast (r1, short_integer_type_node);
4529 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4530 && r1.upper_bound() == wi::to_wide (maxshort));
4533 // (unsigned char)[-5,-1] => [251,255].
4534 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4535 range_cast (r0, unsigned_char_type_node);
4536 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4537 range_cast (r0, signed_char_type_node);
4538 ASSERT_TRUE (r0 == rold);
4540 // (signed char)[15, 150] => [-128,-106][15,127].
4541 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4542 range_cast (r0, signed_char_type_node);
4543 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4544 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4545 r1.union_ (r2);
4546 ASSERT_TRUE (r1 == r0);
4547 range_cast (r0, unsigned_char_type_node);
4548 ASSERT_TRUE (r0 == rold);
4550 // (unsigned char)[-5, 5] => [0,5][251,255].
4551 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4552 range_cast (r0, unsigned_char_type_node);
4553 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4554 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4555 r1.union_ (r2);
4556 ASSERT_TRUE (r0 == r1);
4557 range_cast (r0, signed_char_type_node);
4558 ASSERT_TRUE (r0 == rold);
4560 // (unsigned char)[-5,5] => [0,5][251,255].
4561 r0 = int_range<1> (INT (-5), INT (5));
4562 range_cast (r0, unsigned_char_type_node);
4563 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4564 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4565 ASSERT_TRUE (r0 == r1);
4567 // (unsigned char)[5U,1974U] => [0,255].
4568 r0 = int_range<1> (UINT (5), UINT (1974));
4569 range_cast (r0, unsigned_char_type_node);
4570 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4571 range_cast (r0, integer_type_node);
4572 // Going to a wider range should not sign extend.
4573 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4575 // (unsigned char)[-350,15] => [0,255].
4576 r0 = int_range<1> (INT (-350), INT (15));
4577 range_cast (r0, unsigned_char_type_node);
4578 ASSERT_TRUE (r0 == (int_range<1>
4579 (TYPE_MIN_VALUE (unsigned_char_type_node),
4580 TYPE_MAX_VALUE (unsigned_char_type_node))));
4582 // Casting [-120,20] from signed char to unsigned short.
4583 // => [0, 20][0xff88, 0xffff].
4584 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4585 range_cast (r0, short_unsigned_type_node);
4586 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4587 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4588 r1.union_ (r2);
4589 ASSERT_TRUE (r0 == r1);
4590 // A truncating cast back to signed char will work because [-120, 20]
4591 // is representable in signed char.
4592 range_cast (r0, signed_char_type_node);
4593 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4595 // unsigned char -> signed short
4596 // (signed short)[(unsigned char)25, (unsigned char)250]
4597 // => [(signed short)25, (signed short)250]
4598 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4599 range_cast (r0, short_integer_type_node);
4600 r1 = int_range<1> (INT16 (25), INT16 (250));
4601 ASSERT_TRUE (r0 == r1);
4602 range_cast (r0, unsigned_char_type_node);
4603 ASSERT_TRUE (r0 == rold);
4605 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4606 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4607 TYPE_MAX_VALUE (long_long_integer_type_node));
4608 range_cast (r0, short_unsigned_type_node);
4609 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4610 TYPE_MAX_VALUE (short_unsigned_type_node));
4611 ASSERT_TRUE (r0 == r1);
4613 // Casting NONZERO to a narrower type will wrap/overflow so
4614 // it's just the entire range for the narrower type.
4616 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4617 // is outside of the range of a smaller range, return the full
4618 // smaller range.
4619 if (TYPE_PRECISION (integer_type_node)
4620 > TYPE_PRECISION (short_integer_type_node))
4622 r0 = range_nonzero (integer_type_node);
4623 range_cast (r0, short_integer_type_node);
4624 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4625 TYPE_MAX_VALUE (short_integer_type_node));
4626 ASSERT_TRUE (r0 == r1);
4629 // Casting NONZERO from a narrower signed to a wider signed.
4631 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4632 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4633 r0 = range_nonzero (short_integer_type_node);
4634 range_cast (r0, integer_type_node);
4635 r1 = int_range<1> (INT (-32768), INT (-1));
4636 r2 = int_range<1> (INT (1), INT (32767));
4637 r1.union_ (r2);
4638 ASSERT_TRUE (r0 == r1);
4641 static void
4642 range_op_lshift_tests ()
4644 // Test that 0x808.... & 0x8.... still contains 0x8....
4645 // for a large set of numbers.
4647 int_range_max res;
4648 tree big_type = long_long_unsigned_type_node;
4649 // big_num = 0x808,0000,0000,0000
4650 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4651 build_int_cst (big_type, 0x808),
4652 build_int_cst (big_type, 48));
4653 op_bitwise_and.fold_range (res, big_type,
4654 int_range <1> (big_type),
4655 int_range <1> (big_num, big_num));
4656 // val = 0x8,0000,0000,0000
4657 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4658 build_int_cst (big_type, 0x8),
4659 build_int_cst (big_type, 48));
4660 ASSERT_TRUE (res.contains_p (val));
4663 if (TYPE_PRECISION (unsigned_type_node) > 31)
4665 // unsigned VARYING = op1 << 1 should be VARYING.
4666 int_range<2> lhs (unsigned_type_node);
4667 int_range<2> shift (INT (1), INT (1));
4668 int_range_max op1;
4669 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4670 ASSERT_TRUE (op1.varying_p ());
4672 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4673 int_range<2> zero (UINT (0), UINT (0));
4674 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4675 ASSERT_TRUE (op1.num_pairs () == 2);
4676 // Remove the [0,0] range.
4677 op1.intersect (zero);
4678 ASSERT_TRUE (op1.num_pairs () == 1);
4679 // op1 << 1 should be [0x8000,0x8000] << 1,
4680 // which should result in [0,0].
4681 int_range_max result;
4682 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4683 ASSERT_TRUE (result == zero);
4685 // signed VARYING = op1 << 1 should be VARYING.
4686 if (TYPE_PRECISION (integer_type_node) > 31)
4688 // unsigned VARYING = op1 << 1 hould be VARYING.
4689 int_range<2> lhs (integer_type_node);
4690 int_range<2> shift (INT (1), INT (1));
4691 int_range_max op1;
4692 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4693 ASSERT_TRUE (op1.varying_p ());
4695 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4696 int_range<2> zero (INT (0), INT (0));
4697 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4698 ASSERT_TRUE (op1.num_pairs () == 2);
4699 // Remove the [0,0] range.
4700 op1.intersect (zero);
4701 ASSERT_TRUE (op1.num_pairs () == 1);
4702 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4703 // which should result in [0,0].
4704 int_range_max result;
4705 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4706 ASSERT_TRUE (result == zero);
4710 static void
4711 range_op_rshift_tests ()
4713 // unsigned: [3, MAX] = OP1 >> 1
4715 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4716 TYPE_MAX_VALUE (unsigned_type_node));
4717 int_range_max one (build_one_cst (unsigned_type_node),
4718 build_one_cst (unsigned_type_node));
4719 int_range_max op1;
4720 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4721 ASSERT_FALSE (op1.contains_p (UINT (3)));
4724 // signed: [3, MAX] = OP1 >> 1
4726 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4727 int_range_max one (INT (1), INT (1));
4728 int_range_max op1;
4729 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4730 ASSERT_FALSE (op1.contains_p (INT (-2)));
4733 // This is impossible, so OP1 should be [].
4734 // signed: [MIN, MIN] = OP1 >> 1
4736 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4737 TYPE_MIN_VALUE (integer_type_node));
4738 int_range_max one (INT (1), INT (1));
4739 int_range_max op1;
4740 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4741 ASSERT_TRUE (op1.undefined_p ());
4744 // signed: ~[-1] = OP1 >> 31
4745 if (TYPE_PRECISION (integer_type_node) > 31)
4747 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4748 int_range_max shift (INT (31), INT (31));
4749 int_range_max op1;
4750 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4751 int_range_max negatives = range_negatives (integer_type_node);
4752 negatives.intersect (op1);
4753 ASSERT_TRUE (negatives.undefined_p ());
4757 static void
4758 range_op_bitwise_and_tests ()
4760 int_range_max res;
4761 tree min = vrp_val_min (integer_type_node);
4762 tree max = vrp_val_max (integer_type_node);
4763 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
4764 build_one_cst (integer_type_node));
4765 int_range_max i1 (tiny, max);
4766 int_range_max i2 (build_int_cst (integer_type_node, 255),
4767 build_int_cst (integer_type_node, 255));
4769 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4770 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4771 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4773 // VARYING = OP1 & 255: OP1 is VARYING
4774 i1 = int_range<1> (integer_type_node);
4775 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4776 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4778 // For 0 = x & MASK, x is ~MASK.
4780 int_range<2> zero (integer_zero_node, integer_zero_node);
4781 int_range<2> mask = int_range<2> (INT (7), INT (7));
4782 op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
4783 wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
4784 ASSERT_TRUE (res.get_nonzero_bits () == inv);
4787 // (NONZERO | X) is nonzero.
4788 i1.set_nonzero (integer_type_node);
4789 i2.set_varying (integer_type_node);
4790 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4791 ASSERT_TRUE (res.nonzero_p ());
4793 // (NEGATIVE | X) is nonzero.
4794 i1 = int_range<1> (INT (-5), INT (-3));
4795 i2.set_varying (integer_type_node);
4796 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4797 ASSERT_FALSE (res.contains_p (INT (0)));
4800 static void
4801 range_relational_tests ()
4803 int_range<2> lhs (unsigned_char_type_node);
4804 int_range<2> op1 (UCHAR (8), UCHAR (10));
4805 int_range<2> op2 (UCHAR (20), UCHAR (20));
4807 // Never wrapping additions mean LHS > OP1.
4808 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4809 ASSERT_TRUE (code == VREL_GT);
4811 // Most wrapping additions mean nothing...
4812 op1 = int_range<2> (UCHAR (8), UCHAR (10));
4813 op2 = int_range<2> (UCHAR (0), UCHAR (255));
4814 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4815 ASSERT_TRUE (code == VREL_VARYING);
4817 // However, always wrapping additions mean LHS < OP1.
4818 op1 = int_range<2> (UCHAR (1), UCHAR (255));
4819 op2 = int_range<2> (UCHAR (255), UCHAR (255));
4820 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4821 ASSERT_TRUE (code == VREL_LT);
4824 void
4825 range_op_tests ()
4827 range_op_rshift_tests ();
4828 range_op_lshift_tests ();
4829 range_op_bitwise_and_tests ();
4830 range_op_cast_tests ();
4831 range_relational_tests ();
4834 } // namespace selftest
4836 #endif // CHECKING_P