d: Add test for PR d/108167 to the testsuite [PR108167]
[official-gcc.git] / gcc / range-op.cc
blob5c67bce6d3aab81ad3186b902e09d6a96878d9bb
1 /* Code for range operators.
2 Copyright (C) 2017-2023 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"
49 #include "tree-ssa-ccp.h"
51 // Convert irange bitmasks into a VALUE MASK pair suitable for calling CCP.
53 static void
54 irange_to_masked_value (const irange &r, widest_int &value, widest_int &mask)
56 if (r.singleton_p ())
58 mask = 0;
59 value = widest_int::from (r.lower_bound (), TYPE_SIGN (r.type ()));
61 else
63 mask = widest_int::from (r.get_nonzero_bits (), TYPE_SIGN (r.type ()));
64 value = 0;
68 // Update the known bitmasks in R when applying the operation CODE to
69 // LH and RH.
71 static void
72 update_known_bitmask (irange &r, tree_code code,
73 const irange &lh, const irange &rh)
75 if (r.undefined_p () || lh.undefined_p () || rh.undefined_p ())
76 return;
78 widest_int value, mask, lh_mask, rh_mask, lh_value, rh_value;
79 tree type = r.type ();
80 signop sign = TYPE_SIGN (type);
81 int prec = TYPE_PRECISION (type);
82 signop lh_sign = TYPE_SIGN (lh.type ());
83 signop rh_sign = TYPE_SIGN (rh.type ());
84 int lh_prec = TYPE_PRECISION (lh.type ());
85 int rh_prec = TYPE_PRECISION (rh.type ());
87 irange_to_masked_value (lh, lh_value, lh_mask);
88 irange_to_masked_value (rh, rh_value, rh_mask);
89 bit_value_binop (code, sign, prec, &value, &mask,
90 lh_sign, lh_prec, lh_value, lh_mask,
91 rh_sign, rh_prec, rh_value, rh_mask);
92 r.set_nonzero_bits (value | mask);
95 // Return the upper limit for a type.
97 static inline wide_int
98 max_limit (const_tree type)
100 return wi::max_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
103 // Return the lower limit for a type.
105 static inline wide_int
106 min_limit (const_tree type)
108 return wi::min_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
111 // Return false if shifting by OP is undefined behavior. Otherwise, return
112 // true and the range it is to be shifted by. This allows trimming out of
113 // undefined ranges, leaving only valid ranges if there are any.
115 static inline bool
116 get_shift_range (irange &r, tree type, const irange &op)
118 if (op.undefined_p ())
119 return false;
121 // Build valid range and intersect it with the shift range.
122 r = value_range (build_int_cst_type (op.type (), 0),
123 build_int_cst_type (op.type (), TYPE_PRECISION (type) - 1));
124 r.intersect (op);
126 // If there are no valid ranges in the shift range, returned false.
127 if (r.undefined_p ())
128 return false;
129 return true;
132 // Return TRUE if 0 is within [WMIN, WMAX].
134 static inline bool
135 wi_includes_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
137 signop sign = TYPE_SIGN (type);
138 return wi::le_p (wmin, 0, sign) && wi::ge_p (wmax, 0, sign);
141 // Return TRUE if [WMIN, WMAX] is the singleton 0.
143 static inline bool
144 wi_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
146 unsigned prec = TYPE_PRECISION (type);
147 return wmin == wmax && wi::eq_p (wmin, wi::zero (prec));
150 // Default wide_int fold operation returns [MIN, MAX].
152 void
153 range_operator::wi_fold (irange &r, tree type,
154 const wide_int &lh_lb ATTRIBUTE_UNUSED,
155 const wide_int &lh_ub ATTRIBUTE_UNUSED,
156 const wide_int &rh_lb ATTRIBUTE_UNUSED,
157 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
159 gcc_checking_assert (r.supports_type_p (type));
160 r.set_varying (type);
163 // Call wi_fold when both op1 and op2 are equivalent. Further split small
164 // subranges into constants. This can provide better precision.
165 // For x + y, when x == y with a range of [0,4] instead of [0, 8] produce
166 // [0,0][2, 2][4,4][6, 6][8, 8]
167 // LIMIT is the maximum number of elements in range allowed before we
168 // do not processs them individually.
170 void
171 range_operator::wi_fold_in_parts_equiv (irange &r, tree type,
172 const wide_int &lh_lb,
173 const wide_int &lh_ub,
174 unsigned limit) const
176 int_range_max tmp;
177 widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
178 widest_int::from (lh_lb, TYPE_SIGN (type)));
179 // if there are 1 to 8 values in the LH range, split them up.
180 r.set_undefined ();
181 if (lh_range >= 0 && lh_range < limit)
183 for (unsigned x = 0; x <= lh_range; x++)
185 wide_int val = lh_lb + x;
186 wi_fold (tmp, type, val, val, val, val);
187 r.union_ (tmp);
190 // Otherwise just call wi_fold.
191 else
192 wi_fold (r, type, lh_lb, lh_ub, lh_lb, lh_ub);
195 // Call wi_fold, except further split small subranges into constants.
196 // This can provide better precision. For something 8 >> [0,1]
197 // Instead of [8, 16], we will produce [8,8][16,16]
199 void
200 range_operator::wi_fold_in_parts (irange &r, tree type,
201 const wide_int &lh_lb,
202 const wide_int &lh_ub,
203 const wide_int &rh_lb,
204 const wide_int &rh_ub) const
206 int_range_max tmp;
207 widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
208 widest_int::from (rh_lb, TYPE_SIGN (type)));
209 widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
210 widest_int::from (lh_lb, TYPE_SIGN (type)));
211 // If there are 2, 3, or 4 values in the RH range, do them separately.
212 // Call wi_fold_in_parts to check the RH side.
213 if (rh_range > 0 && rh_range < 4)
215 wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
216 if (rh_range > 1)
218 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
219 r.union_ (tmp);
220 if (rh_range == 3)
222 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
223 r.union_ (tmp);
226 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
227 r.union_ (tmp);
229 // Otherise check for 2, 3, or 4 values in the LH range and split them up.
230 // The RH side has been checked, so no recursion needed.
231 else if (lh_range > 0 && lh_range < 4)
233 wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
234 if (lh_range > 1)
236 wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
237 r.union_ (tmp);
238 if (lh_range == 3)
240 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
241 r.union_ (tmp);
244 wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
245 r.union_ (tmp);
247 // Otherwise just call wi_fold.
248 else
249 wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
252 // The default for fold is to break all ranges into sub-ranges and
253 // invoke the wi_fold method on each sub-range pair.
255 bool
256 range_operator::fold_range (irange &r, tree type,
257 const irange &lh,
258 const irange &rh,
259 relation_trio trio) const
261 gcc_checking_assert (r.supports_type_p (type));
262 if (empty_range_varying (r, type, lh, rh))
263 return true;
265 relation_kind rel = trio.op1_op2 ();
266 unsigned num_lh = lh.num_pairs ();
267 unsigned num_rh = rh.num_pairs ();
269 // If op1 and op2 are equivalences, then we don't need a complete cross
270 // product, just pairs of matching elements.
271 if (relation_equiv_p (rel) && lh == rh)
273 int_range_max tmp;
274 r.set_undefined ();
275 for (unsigned x = 0; x < num_lh; ++x)
277 // If the number of subranges is too high, limit subrange creation.
278 unsigned limit = (r.num_pairs () > 32) ? 0 : 8;
279 wide_int lh_lb = lh.lower_bound (x);
280 wide_int lh_ub = lh.upper_bound (x);
281 wi_fold_in_parts_equiv (tmp, type, lh_lb, lh_ub, limit);
282 r.union_ (tmp);
283 if (r.varying_p ())
284 break;
286 op1_op2_relation_effect (r, type, lh, rh, rel);
287 update_known_bitmask (r, m_code, lh, rh);
288 return true;
291 // If both ranges are single pairs, fold directly into the result range.
292 // If the number of subranges grows too high, produce a summary result as the
293 // loop becomes exponential with little benefit. See PR 103821.
294 if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
296 wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
297 rh.lower_bound (), rh.upper_bound ());
298 op1_op2_relation_effect (r, type, lh, rh, rel);
299 update_known_bitmask (r, m_code, lh, rh);
300 return true;
303 int_range_max tmp;
304 r.set_undefined ();
305 for (unsigned x = 0; x < num_lh; ++x)
306 for (unsigned y = 0; y < num_rh; ++y)
308 wide_int lh_lb = lh.lower_bound (x);
309 wide_int lh_ub = lh.upper_bound (x);
310 wide_int rh_lb = rh.lower_bound (y);
311 wide_int rh_ub = rh.upper_bound (y);
312 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
313 r.union_ (tmp);
314 if (r.varying_p ())
316 op1_op2_relation_effect (r, type, lh, rh, rel);
317 update_known_bitmask (r, m_code, lh, rh);
318 return true;
321 op1_op2_relation_effect (r, type, lh, rh, rel);
322 update_known_bitmask (r, m_code, lh, rh);
323 return true;
326 // The default for op1_range is to return false.
328 bool
329 range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
330 tree type ATTRIBUTE_UNUSED,
331 const irange &lhs ATTRIBUTE_UNUSED,
332 const irange &op2 ATTRIBUTE_UNUSED,
333 relation_trio) const
335 return false;
338 // The default for op2_range is to return false.
340 bool
341 range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
342 tree type ATTRIBUTE_UNUSED,
343 const irange &lhs ATTRIBUTE_UNUSED,
344 const irange &op1 ATTRIBUTE_UNUSED,
345 relation_trio) const
347 return false;
350 // The default relation routines return VREL_VARYING.
352 relation_kind
353 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
354 const irange &op1 ATTRIBUTE_UNUSED,
355 const irange &op2 ATTRIBUTE_UNUSED,
356 relation_kind rel ATTRIBUTE_UNUSED) const
358 return VREL_VARYING;
361 relation_kind
362 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
363 const irange &op1 ATTRIBUTE_UNUSED,
364 const irange &op2 ATTRIBUTE_UNUSED,
365 relation_kind rel ATTRIBUTE_UNUSED) const
367 return VREL_VARYING;
370 relation_kind
371 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
373 return VREL_VARYING;
376 // Default is no relation affects the LHS.
378 bool
379 range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
380 tree type ATTRIBUTE_UNUSED,
381 const irange &op1_range ATTRIBUTE_UNUSED,
382 const irange &op2_range ATTRIBUTE_UNUSED,
383 relation_kind rel ATTRIBUTE_UNUSED) const
385 return false;
388 // Create and return a range from a pair of wide-ints that are known
389 // to have overflowed (or underflowed).
391 static void
392 value_range_from_overflowed_bounds (irange &r, tree type,
393 const wide_int &wmin,
394 const wide_int &wmax)
396 const signop sgn = TYPE_SIGN (type);
397 const unsigned int prec = TYPE_PRECISION (type);
399 wide_int tmin = wide_int::from (wmin, prec, sgn);
400 wide_int tmax = wide_int::from (wmax, prec, sgn);
402 bool covers = false;
403 wide_int tem = tmin;
404 tmin = tmax + 1;
405 if (wi::cmp (tmin, tmax, sgn) < 0)
406 covers = true;
407 tmax = tem - 1;
408 if (wi::cmp (tmax, tem, sgn) > 0)
409 covers = true;
411 // If the anti-range would cover nothing, drop to varying.
412 // Likewise if the anti-range bounds are outside of the types
413 // values.
414 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
415 r.set_varying (type);
416 else
418 tree tree_min = wide_int_to_tree (type, tmin);
419 tree tree_max = wide_int_to_tree (type, tmax);
420 r.set (tree_min, tree_max, VR_ANTI_RANGE);
424 // Create and return a range from a pair of wide-ints. MIN_OVF and
425 // MAX_OVF describe any overflow that might have occurred while
426 // calculating WMIN and WMAX respectively.
428 static void
429 value_range_with_overflow (irange &r, tree type,
430 const wide_int &wmin, const wide_int &wmax,
431 wi::overflow_type min_ovf = wi::OVF_NONE,
432 wi::overflow_type max_ovf = wi::OVF_NONE)
434 const signop sgn = TYPE_SIGN (type);
435 const unsigned int prec = TYPE_PRECISION (type);
436 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
438 // For one bit precision if max != min, then the range covers all
439 // values.
440 if (prec == 1 && wi::ne_p (wmax, wmin))
442 r.set_varying (type);
443 return;
446 if (overflow_wraps)
448 // If overflow wraps, truncate the values and adjust the range,
449 // kind, and bounds appropriately.
450 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
452 wide_int tmin = wide_int::from (wmin, prec, sgn);
453 wide_int tmax = wide_int::from (wmax, prec, sgn);
454 // If the limits are swapped, we wrapped around and cover
455 // the entire range.
456 if (wi::gt_p (tmin, tmax, sgn))
457 r.set_varying (type);
458 else
459 // No overflow or both overflow or underflow. The range
460 // kind stays normal.
461 r.set (wide_int_to_tree (type, tmin),
462 wide_int_to_tree (type, tmax));
463 return;
466 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
467 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
468 value_range_from_overflowed_bounds (r, type, wmin, wmax);
469 else
470 // Other underflow and/or overflow, drop to VR_VARYING.
471 r.set_varying (type);
473 else
475 // If both bounds either underflowed or overflowed, then the result
476 // is undefined.
477 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
478 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
480 r.set_undefined ();
481 return;
484 // If overflow does not wrap, saturate to [MIN, MAX].
485 wide_int new_lb, new_ub;
486 if (min_ovf == wi::OVF_UNDERFLOW)
487 new_lb = wi::min_value (prec, sgn);
488 else if (min_ovf == wi::OVF_OVERFLOW)
489 new_lb = wi::max_value (prec, sgn);
490 else
491 new_lb = wmin;
493 if (max_ovf == wi::OVF_UNDERFLOW)
494 new_ub = wi::min_value (prec, sgn);
495 else if (max_ovf == wi::OVF_OVERFLOW)
496 new_ub = wi::max_value (prec, sgn);
497 else
498 new_ub = wmax;
500 r.set (wide_int_to_tree (type, new_lb),
501 wide_int_to_tree (type, new_ub));
505 // Create and return a range from a pair of wide-ints. Canonicalize
506 // the case where the bounds are swapped. In which case, we transform
507 // [10,5] into [MIN,5][10,MAX].
509 static inline void
510 create_possibly_reversed_range (irange &r, tree type,
511 const wide_int &new_lb, const wide_int &new_ub)
513 signop s = TYPE_SIGN (type);
514 // If the bounds are swapped, treat the result as if an overflow occured.
515 if (wi::gt_p (new_lb, new_ub, s))
516 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
517 else
518 // Otherwise it's just a normal range.
519 r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
522 // Return the summary information about boolean range LHS. If EMPTY/FULL,
523 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
525 bool_range_state
526 get_bool_state (vrange &r, const vrange &lhs, tree val_type)
528 // If there is no result, then this is unexecutable.
529 if (lhs.undefined_p ())
531 r.set_undefined ();
532 return BRS_EMPTY;
535 if (lhs.zero_p ())
536 return BRS_FALSE;
538 // For TRUE, we can't just test for [1,1] because Ada can have
539 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
540 if (lhs.contains_p (build_zero_cst (lhs.type ())))
542 r.set_varying (val_type);
543 return BRS_FULL;
546 return BRS_TRUE;
550 class operator_equal : public range_operator
552 using range_operator::fold_range;
553 using range_operator::op1_range;
554 using range_operator::op2_range;
555 public:
556 virtual bool fold_range (irange &r, tree type,
557 const irange &op1,
558 const irange &op2,
559 relation_trio = TRIO_VARYING) const;
560 virtual bool op1_range (irange &r, tree type,
561 const irange &lhs,
562 const irange &val,
563 relation_trio = TRIO_VARYING) const;
564 virtual bool op2_range (irange &r, tree type,
565 const irange &lhs,
566 const irange &val,
567 relation_trio = TRIO_VARYING) const;
568 virtual relation_kind op1_op2_relation (const irange &lhs) const;
569 } op_equal;
571 // Check if the LHS range indicates a relation between OP1 and OP2.
573 relation_kind
574 equal_op1_op2_relation (const irange &lhs)
576 if (lhs.undefined_p ())
577 return VREL_UNDEFINED;
579 // FALSE = op1 == op2 indicates NE_EXPR.
580 if (lhs.zero_p ())
581 return VREL_NE;
583 // TRUE = op1 == op2 indicates EQ_EXPR.
584 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
585 return VREL_EQ;
586 return VREL_VARYING;
589 relation_kind
590 operator_equal::op1_op2_relation (const irange &lhs) const
592 return equal_op1_op2_relation (lhs);
596 bool
597 operator_equal::fold_range (irange &r, tree type,
598 const irange &op1,
599 const irange &op2,
600 relation_trio rel) const
602 if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
603 return true;
605 // We can be sure the values are always equal or not if both ranges
606 // consist of a single value, and then compare them.
607 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
608 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
610 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
611 r = range_true (type);
612 else
613 r = range_false (type);
615 else
617 // If ranges do not intersect, we know the range is not equal,
618 // otherwise we don't know anything for sure.
619 int_range_max tmp = op1;
620 tmp.intersect (op2);
621 if (tmp.undefined_p ())
622 r = range_false (type);
623 else
624 r = range_true_and_false (type);
626 return true;
629 bool
630 operator_equal::op1_range (irange &r, tree type,
631 const irange &lhs,
632 const irange &op2,
633 relation_trio) const
635 switch (get_bool_state (r, lhs, type))
637 case BRS_TRUE:
638 // If it's true, the result is the same as OP2.
639 r = op2;
640 break;
642 case BRS_FALSE:
643 // If the result is false, the only time we know anything is
644 // if OP2 is a constant.
645 if (!op2.undefined_p ()
646 && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
648 r = op2;
649 r.invert ();
651 else
652 r.set_varying (type);
653 break;
655 default:
656 break;
658 return true;
661 bool
662 operator_equal::op2_range (irange &r, tree type,
663 const irange &lhs,
664 const irange &op1,
665 relation_trio rel) const
667 return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
670 class operator_not_equal : public range_operator
672 using range_operator::fold_range;
673 using range_operator::op1_range;
674 using range_operator::op2_range;
675 public:
676 virtual bool fold_range (irange &r, tree type,
677 const irange &op1,
678 const irange &op2,
679 relation_trio = TRIO_VARYING) const;
680 virtual bool op1_range (irange &r, tree type,
681 const irange &lhs,
682 const irange &op2,
683 relation_trio = TRIO_VARYING) const;
684 virtual bool op2_range (irange &r, tree type,
685 const irange &lhs,
686 const irange &op1,
687 relation_trio = TRIO_VARYING) const;
688 virtual relation_kind op1_op2_relation (const irange &lhs) const;
689 } op_not_equal;
691 // Check if the LHS range indicates a relation between OP1 and OP2.
693 relation_kind
694 not_equal_op1_op2_relation (const irange &lhs)
696 if (lhs.undefined_p ())
697 return VREL_UNDEFINED;
699 // FALSE = op1 != op2 indicates EQ_EXPR.
700 if (lhs.zero_p ())
701 return VREL_EQ;
703 // TRUE = op1 != op2 indicates NE_EXPR.
704 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
705 return VREL_NE;
706 return VREL_VARYING;
709 relation_kind
710 operator_not_equal::op1_op2_relation (const irange &lhs) const
712 return not_equal_op1_op2_relation (lhs);
715 bool
716 operator_not_equal::fold_range (irange &r, tree type,
717 const irange &op1,
718 const irange &op2,
719 relation_trio rel) const
721 if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
722 return true;
724 // We can be sure the values are always equal or not if both ranges
725 // consist of a single value, and then compare them.
726 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
727 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
729 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
730 r = range_true (type);
731 else
732 r = range_false (type);
734 else
736 // If ranges do not intersect, we know the range is not equal,
737 // otherwise we don't know anything for sure.
738 int_range_max tmp = op1;
739 tmp.intersect (op2);
740 if (tmp.undefined_p ())
741 r = range_true (type);
742 else
743 r = range_true_and_false (type);
745 return true;
748 bool
749 operator_not_equal::op1_range (irange &r, tree type,
750 const irange &lhs,
751 const irange &op2,
752 relation_trio) const
754 switch (get_bool_state (r, lhs, type))
756 case BRS_TRUE:
757 // If the result is true, the only time we know anything is if
758 // OP2 is a constant.
759 if (!op2.undefined_p ()
760 && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
762 r = op2;
763 r.invert ();
765 else
766 r.set_varying (type);
767 break;
769 case BRS_FALSE:
770 // If it's false, the result is the same as OP2.
771 r = op2;
772 break;
774 default:
775 break;
777 return true;
781 bool
782 operator_not_equal::op2_range (irange &r, tree type,
783 const irange &lhs,
784 const irange &op1,
785 relation_trio rel) const
787 return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
790 // (X < VAL) produces the range of [MIN, VAL - 1].
792 static void
793 build_lt (irange &r, tree type, const wide_int &val)
795 wi::overflow_type ov;
796 wide_int lim;
797 signop sgn = TYPE_SIGN (type);
799 // Signed 1 bit cannot represent 1 for subtraction.
800 if (sgn == SIGNED)
801 lim = wi::add (val, -1, sgn, &ov);
802 else
803 lim = wi::sub (val, 1, sgn, &ov);
805 // If val - 1 underflows, check if X < MIN, which is an empty range.
806 if (ov)
807 r.set_undefined ();
808 else
809 r = int_range<1> (type, min_limit (type), lim);
812 // (X <= VAL) produces the range of [MIN, VAL].
814 static void
815 build_le (irange &r, tree type, const wide_int &val)
817 r = int_range<1> (type, min_limit (type), val);
820 // (X > VAL) produces the range of [VAL + 1, MAX].
822 static void
823 build_gt (irange &r, tree type, const wide_int &val)
825 wi::overflow_type ov;
826 wide_int lim;
827 signop sgn = TYPE_SIGN (type);
829 // Signed 1 bit cannot represent 1 for addition.
830 if (sgn == SIGNED)
831 lim = wi::sub (val, -1, sgn, &ov);
832 else
833 lim = wi::add (val, 1, sgn, &ov);
834 // If val + 1 overflows, check is for X > MAX, which is an empty range.
835 if (ov)
836 r.set_undefined ();
837 else
838 r = int_range<1> (type, lim, max_limit (type));
841 // (X >= val) produces the range of [VAL, MAX].
843 static void
844 build_ge (irange &r, tree type, const wide_int &val)
846 r = int_range<1> (type, val, max_limit (type));
850 class operator_lt : public range_operator
852 using range_operator::fold_range;
853 using range_operator::op1_range;
854 using range_operator::op2_range;
855 public:
856 virtual bool fold_range (irange &r, tree type,
857 const irange &op1,
858 const irange &op2,
859 relation_trio = TRIO_VARYING) const;
860 virtual bool op1_range (irange &r, tree type,
861 const irange &lhs,
862 const irange &op2,
863 relation_trio = TRIO_VARYING) const;
864 virtual bool op2_range (irange &r, tree type,
865 const irange &lhs,
866 const irange &op1,
867 relation_trio = TRIO_VARYING) const;
868 virtual relation_kind op1_op2_relation (const irange &lhs) const;
869 } op_lt;
871 // Check if the LHS range indicates a relation between OP1 and OP2.
873 relation_kind
874 lt_op1_op2_relation (const irange &lhs)
876 if (lhs.undefined_p ())
877 return VREL_UNDEFINED;
879 // FALSE = op1 < op2 indicates GE_EXPR.
880 if (lhs.zero_p ())
881 return VREL_GE;
883 // TRUE = op1 < op2 indicates LT_EXPR.
884 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
885 return VREL_LT;
886 return VREL_VARYING;
889 relation_kind
890 operator_lt::op1_op2_relation (const irange &lhs) const
892 return lt_op1_op2_relation (lhs);
895 bool
896 operator_lt::fold_range (irange &r, tree type,
897 const irange &op1,
898 const irange &op2,
899 relation_trio rel) const
901 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
902 return true;
904 signop sign = TYPE_SIGN (op1.type ());
905 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
907 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
908 r = range_true (type);
909 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
910 r = range_false (type);
911 // Use nonzero bits to determine if < 0 is false.
912 else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
913 r = range_false (type);
914 else
915 r = range_true_and_false (type);
916 return true;
919 bool
920 operator_lt::op1_range (irange &r, tree type,
921 const irange &lhs,
922 const irange &op2,
923 relation_trio) const
925 if (op2.undefined_p ())
926 return false;
928 switch (get_bool_state (r, lhs, type))
930 case BRS_TRUE:
931 build_lt (r, type, op2.upper_bound ());
932 break;
934 case BRS_FALSE:
935 build_ge (r, type, op2.lower_bound ());
936 break;
938 default:
939 break;
941 return true;
944 bool
945 operator_lt::op2_range (irange &r, tree type,
946 const irange &lhs,
947 const irange &op1,
948 relation_trio) const
950 if (op1.undefined_p ())
951 return false;
953 switch (get_bool_state (r, lhs, type))
955 case BRS_TRUE:
956 build_gt (r, type, op1.lower_bound ());
957 break;
959 case BRS_FALSE:
960 build_le (r, type, op1.upper_bound ());
961 break;
963 default:
964 break;
966 return true;
970 class operator_le : 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_trio = TRIO_VARYING) const;
980 virtual bool op1_range (irange &r, tree type,
981 const irange &lhs,
982 const irange &op2,
983 relation_trio = TRIO_VARYING) const;
984 virtual bool op2_range (irange &r, tree type,
985 const irange &lhs,
986 const irange &op1,
987 relation_trio = TRIO_VARYING) const;
988 virtual relation_kind op1_op2_relation (const irange &lhs) const;
989 } op_le;
991 // Check if the LHS range indicates a relation between OP1 and OP2.
993 relation_kind
994 le_op1_op2_relation (const irange &lhs)
996 if (lhs.undefined_p ())
997 return VREL_UNDEFINED;
999 // FALSE = op1 <= op2 indicates GT_EXPR.
1000 if (lhs.zero_p ())
1001 return VREL_GT;
1003 // TRUE = op1 <= op2 indicates LE_EXPR.
1004 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1005 return VREL_LE;
1006 return VREL_VARYING;
1009 relation_kind
1010 operator_le::op1_op2_relation (const irange &lhs) const
1012 return le_op1_op2_relation (lhs);
1015 bool
1016 operator_le::fold_range (irange &r, tree type,
1017 const irange &op1,
1018 const irange &op2,
1019 relation_trio rel) const
1021 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1022 return true;
1024 signop sign = TYPE_SIGN (op1.type ());
1025 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1027 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
1028 r = range_true (type);
1029 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
1030 r = range_false (type);
1031 else
1032 r = range_true_and_false (type);
1033 return true;
1036 bool
1037 operator_le::op1_range (irange &r, tree type,
1038 const irange &lhs,
1039 const irange &op2,
1040 relation_trio) const
1042 if (op2.undefined_p ())
1043 return false;
1045 switch (get_bool_state (r, lhs, type))
1047 case BRS_TRUE:
1048 build_le (r, type, op2.upper_bound ());
1049 break;
1051 case BRS_FALSE:
1052 build_gt (r, type, op2.lower_bound ());
1053 break;
1055 default:
1056 break;
1058 return true;
1061 bool
1062 operator_le::op2_range (irange &r, tree type,
1063 const irange &lhs,
1064 const irange &op1,
1065 relation_trio) const
1067 if (op1.undefined_p ())
1068 return false;
1070 switch (get_bool_state (r, lhs, type))
1072 case BRS_TRUE:
1073 build_ge (r, type, op1.lower_bound ());
1074 break;
1076 case BRS_FALSE:
1077 build_lt (r, type, op1.upper_bound ());
1078 break;
1080 default:
1081 break;
1083 return true;
1087 class operator_gt : public range_operator
1089 using range_operator::fold_range;
1090 using range_operator::op1_range;
1091 using range_operator::op2_range;
1092 public:
1093 virtual bool fold_range (irange &r, tree type,
1094 const irange &op1,
1095 const irange &op2,
1096 relation_trio = TRIO_VARYING) const;
1097 virtual bool op1_range (irange &r, tree type,
1098 const irange &lhs,
1099 const irange &op2,
1100 relation_trio = TRIO_VARYING) const;
1101 virtual bool op2_range (irange &r, tree type,
1102 const irange &lhs,
1103 const irange &op1,
1104 relation_trio = TRIO_VARYING) const;
1105 virtual relation_kind op1_op2_relation (const irange &lhs) const;
1106 } op_gt;
1108 // Check if the LHS range indicates a relation between OP1 and OP2.
1110 relation_kind
1111 gt_op1_op2_relation (const irange &lhs)
1113 if (lhs.undefined_p ())
1114 return VREL_UNDEFINED;
1116 // FALSE = op1 > op2 indicates LE_EXPR.
1117 if (lhs.zero_p ())
1118 return VREL_LE;
1120 // TRUE = op1 > op2 indicates GT_EXPR.
1121 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1122 return VREL_GT;
1123 return VREL_VARYING;
1126 relation_kind
1127 operator_gt::op1_op2_relation (const irange &lhs) const
1129 return gt_op1_op2_relation (lhs);
1133 bool
1134 operator_gt::fold_range (irange &r, tree type,
1135 const irange &op1, const irange &op2,
1136 relation_trio rel) const
1138 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1139 return true;
1141 signop sign = TYPE_SIGN (op1.type ());
1142 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1144 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1145 r = range_true (type);
1146 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1147 r = range_false (type);
1148 else
1149 r = range_true_and_false (type);
1150 return true;
1153 bool
1154 operator_gt::op1_range (irange &r, tree type,
1155 const irange &lhs, const irange &op2,
1156 relation_trio) const
1158 if (op2.undefined_p ())
1159 return false;
1161 switch (get_bool_state (r, lhs, type))
1163 case BRS_TRUE:
1164 build_gt (r, type, op2.lower_bound ());
1165 break;
1167 case BRS_FALSE:
1168 build_le (r, type, op2.upper_bound ());
1169 break;
1171 default:
1172 break;
1174 return true;
1177 bool
1178 operator_gt::op2_range (irange &r, tree type,
1179 const irange &lhs,
1180 const irange &op1,
1181 relation_trio) const
1183 if (op1.undefined_p ())
1184 return false;
1186 switch (get_bool_state (r, lhs, type))
1188 case BRS_TRUE:
1189 build_lt (r, type, op1.upper_bound ());
1190 break;
1192 case BRS_FALSE:
1193 build_ge (r, type, op1.lower_bound ());
1194 break;
1196 default:
1197 break;
1199 return true;
1203 class operator_ge : public range_operator
1205 using range_operator::fold_range;
1206 using range_operator::op1_range;
1207 using range_operator::op2_range;
1208 public:
1209 virtual bool fold_range (irange &r, tree type,
1210 const irange &op1,
1211 const irange &op2,
1212 relation_trio = TRIO_VARYING) const;
1213 virtual bool op1_range (irange &r, tree type,
1214 const irange &lhs,
1215 const irange &op2,
1216 relation_trio = TRIO_VARYING) const;
1217 virtual bool op2_range (irange &r, tree type,
1218 const irange &lhs,
1219 const irange &op1,
1220 relation_trio = TRIO_VARYING) const;
1221 virtual relation_kind op1_op2_relation (const irange &lhs) const;
1222 } op_ge;
1224 // Check if the LHS range indicates a relation between OP1 and OP2.
1226 relation_kind
1227 ge_op1_op2_relation (const irange &lhs)
1229 if (lhs.undefined_p ())
1230 return VREL_UNDEFINED;
1232 // FALSE = op1 >= op2 indicates LT_EXPR.
1233 if (lhs.zero_p ())
1234 return VREL_LT;
1236 // TRUE = op1 >= op2 indicates GE_EXPR.
1237 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1238 return VREL_GE;
1239 return VREL_VARYING;
1242 relation_kind
1243 operator_ge::op1_op2_relation (const irange &lhs) const
1245 return ge_op1_op2_relation (lhs);
1248 bool
1249 operator_ge::fold_range (irange &r, tree type,
1250 const irange &op1,
1251 const irange &op2,
1252 relation_trio rel) const
1254 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1255 return true;
1257 signop sign = TYPE_SIGN (op1.type ());
1258 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1260 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1261 r = range_true (type);
1262 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1263 r = range_false (type);
1264 else
1265 r = range_true_and_false (type);
1266 return true;
1269 bool
1270 operator_ge::op1_range (irange &r, tree type,
1271 const irange &lhs,
1272 const irange &op2,
1273 relation_trio) const
1275 if (op2.undefined_p ())
1276 return false;
1278 switch (get_bool_state (r, lhs, type))
1280 case BRS_TRUE:
1281 build_ge (r, type, op2.lower_bound ());
1282 break;
1284 case BRS_FALSE:
1285 build_lt (r, type, op2.upper_bound ());
1286 break;
1288 default:
1289 break;
1291 return true;
1294 bool
1295 operator_ge::op2_range (irange &r, tree type,
1296 const irange &lhs,
1297 const irange &op1,
1298 relation_trio) const
1300 if (op1.undefined_p ())
1301 return false;
1303 switch (get_bool_state (r, lhs, type))
1305 case BRS_TRUE:
1306 build_le (r, type, op1.upper_bound ());
1307 break;
1309 case BRS_FALSE:
1310 build_gt (r, type, op1.lower_bound ());
1311 break;
1313 default:
1314 break;
1316 return true;
1320 class operator_plus : public range_operator
1322 using range_operator::op1_range;
1323 using range_operator::op2_range;
1324 using range_operator::lhs_op1_relation;
1325 using range_operator::lhs_op2_relation;
1326 public:
1327 virtual bool op1_range (irange &r, tree type,
1328 const irange &lhs,
1329 const irange &op2,
1330 relation_trio) const;
1331 virtual bool op2_range (irange &r, tree type,
1332 const irange &lhs,
1333 const irange &op1,
1334 relation_trio) const;
1335 virtual void wi_fold (irange &r, tree type,
1336 const wide_int &lh_lb,
1337 const wide_int &lh_ub,
1338 const wide_int &rh_lb,
1339 const wide_int &rh_ub) const;
1340 virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
1341 const irange &op2,
1342 relation_kind rel) const;
1343 virtual relation_kind lhs_op2_relation (const irange &lhs, const irange &op1,
1344 const irange &op2,
1345 relation_kind rel) const;
1346 } op_plus;
1348 // Check to see if the range of OP2 indicates anything about the relation
1349 // between LHS and OP1.
1351 relation_kind
1352 operator_plus::lhs_op1_relation (const irange &lhs,
1353 const irange &op1,
1354 const irange &op2,
1355 relation_kind) const
1357 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1358 return VREL_VARYING;
1360 tree type = lhs.type ();
1361 unsigned prec = TYPE_PRECISION (type);
1362 wi::overflow_type ovf1, ovf2;
1363 signop sign = TYPE_SIGN (type);
1365 // LHS = OP1 + 0 indicates LHS == OP1.
1366 if (op2.zero_p ())
1367 return VREL_EQ;
1369 if (TYPE_OVERFLOW_WRAPS (type))
1371 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1372 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1374 else
1375 ovf1 = ovf2 = wi::OVF_NONE;
1377 // Never wrapping additions.
1378 if (!ovf1 && !ovf2)
1380 // Positive op2 means lhs > op1.
1381 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1382 return VREL_GT;
1383 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1384 return VREL_GE;
1386 // Negative op2 means lhs < op1.
1387 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1388 return VREL_LT;
1389 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1390 return VREL_LE;
1392 // Always wrapping additions.
1393 else if (ovf1 && ovf1 == ovf2)
1395 // Positive op2 means lhs < op1.
1396 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1397 return VREL_LT;
1398 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1399 return VREL_LE;
1401 // Negative op2 means lhs > op1.
1402 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1403 return VREL_GT;
1404 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1405 return VREL_GE;
1408 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1409 if (!range_includes_zero_p (&op2))
1410 return VREL_NE;
1412 return VREL_VARYING;
1415 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1416 // operands.
1418 relation_kind
1419 operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1420 const irange &op2, relation_kind rel) const
1422 return lhs_op1_relation (lhs, op2, op1, rel);
1425 void
1426 operator_plus::wi_fold (irange &r, tree type,
1427 const wide_int &lh_lb, const wide_int &lh_ub,
1428 const wide_int &rh_lb, const wide_int &rh_ub) const
1430 wi::overflow_type ov_lb, ov_ub;
1431 signop s = TYPE_SIGN (type);
1432 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1433 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1434 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1437 // Given addition or subtraction, determine the possible NORMAL ranges and
1438 // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1439 // Return the relation that exists between the LHS and OP1 in order for the
1440 // NORMAL range to apply.
1441 // a return value of VREL_VARYING means no ranges were applicable.
1443 static relation_kind
1444 plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
1445 bool add_p)
1447 relation_kind kind = VREL_VARYING;
1448 // For now, only deal with constant adds. This could be extended to ranges
1449 // when someone is so motivated.
1450 if (!offset.singleton_p () || offset.zero_p ())
1451 return kind;
1453 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1454 wide_int off = offset.lower_bound ();
1455 if (wi::neg_p (off, SIGNED))
1457 add_p = !add_p;
1458 off = wi::neg (off);
1461 wi::overflow_type ov;
1462 tree type = offset.type ();
1463 unsigned prec = TYPE_PRECISION (type);
1464 wide_int ub;
1465 wide_int lb;
1466 // calculate the normal range and relation for the operation.
1467 if (add_p)
1469 // [ 0 , INF - OFF]
1470 lb = wi::zero (prec);
1471 ub = wi::sub (wi::to_wide (vrp_val_max (type)), off, UNSIGNED, &ov);
1472 kind = VREL_GT;
1474 else
1476 // [ OFF, INF ]
1477 lb = off;
1478 ub = wi::to_wide (vrp_val_max (type));
1479 kind = VREL_LT;
1481 int_range<2> normal_range (type, lb, ub);
1482 int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
1484 r_ov = ov_range;
1485 r_normal = normal_range;
1486 return kind;
1489 // Once op1 has been calculated by operator_plus or operator_minus, check
1490 // to see if the relation passed causes any part of the calculation to
1491 // be not possible. ie
1492 // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1493 // and that further refines a_2 to [0, 0].
1494 // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1495 // relation between LHS relatoin OP1 and ADD_P is true for PLUS, false for
1496 // MINUS. IF any adjustment can be made, R will reflect it.
1498 static void
1499 adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
1500 bool add_p)
1502 if (r.undefined_p ())
1503 return;
1504 tree type = r.type ();
1505 // Check for unsigned overflow and calculate the overflow part.
1506 signop s = TYPE_SIGN (type);
1507 if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
1508 return;
1510 // Only work with <, <=, >, >= relations.
1511 if (!relation_lt_le_gt_ge_p (rel))
1512 return;
1514 // Get the ranges for this offset.
1515 int_range_max normal, overflow;
1516 relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
1518 // VREL_VARYING means there are no adjustments.
1519 if (k == VREL_VARYING)
1520 return;
1522 // If the relations match use the normal range, otherwise use overflow range.
1523 if (relation_intersect (k, rel) == k)
1524 r.intersect (normal);
1525 else
1526 r.intersect (overflow);
1527 return;
1530 bool
1531 operator_plus::op1_range (irange &r, tree type,
1532 const irange &lhs,
1533 const irange &op2,
1534 relation_trio trio) const
1536 if (lhs.undefined_p ())
1537 return false;
1538 // Start with the default operation.
1539 range_op_handler minus (MINUS_EXPR, type);
1540 if (!minus)
1541 return false;
1542 bool res = minus.fold_range (r, type, lhs, op2);
1543 relation_kind rel = trio.lhs_op1 ();
1544 // Check for a relation refinement.
1545 if (res)
1546 adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
1547 return res;
1550 bool
1551 operator_plus::op2_range (irange &r, tree type,
1552 const irange &lhs,
1553 const irange &op1,
1554 relation_trio rel) const
1556 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1560 class operator_minus : public range_operator
1562 using range_operator::fold_range;
1563 using range_operator::op1_range;
1564 using range_operator::op2_range;
1565 public:
1566 virtual bool op1_range (irange &r, tree type,
1567 const irange &lhs,
1568 const irange &op2,
1569 relation_trio) const;
1570 virtual bool op2_range (irange &r, tree type,
1571 const irange &lhs,
1572 const irange &op1,
1573 relation_trio) const;
1574 virtual void wi_fold (irange &r, tree type,
1575 const wide_int &lh_lb,
1576 const wide_int &lh_ub,
1577 const wide_int &rh_lb,
1578 const wide_int &rh_ub) const;
1579 virtual relation_kind lhs_op1_relation (const irange &lhs,
1580 const irange &op1,
1581 const irange &op2,
1582 relation_kind rel) const;
1583 virtual bool op1_op2_relation_effect (irange &lhs_range,
1584 tree type,
1585 const irange &op1_range,
1586 const irange &op2_range,
1587 relation_kind rel) const;
1588 } op_minus;
1590 void
1591 operator_minus::wi_fold (irange &r, tree type,
1592 const wide_int &lh_lb, const wide_int &lh_ub,
1593 const wide_int &rh_lb, const wide_int &rh_ub) const
1595 wi::overflow_type ov_lb, ov_ub;
1596 signop s = TYPE_SIGN (type);
1597 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1598 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
1599 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1603 // Return the relation between LHS and OP1 based on the relation between
1604 // OP1 and OP2.
1606 relation_kind
1607 operator_minus::lhs_op1_relation (const irange &, const irange &op1,
1608 const irange &, relation_kind rel) const
1610 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
1611 switch (rel)
1613 case VREL_GT:
1614 case VREL_GE:
1615 return VREL_LE;
1616 default:
1617 break;
1619 return VREL_VARYING;
1622 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1623 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1624 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1626 static bool
1627 minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1628 const irange &op1_range ATTRIBUTE_UNUSED,
1629 const irange &op2_range ATTRIBUTE_UNUSED,
1630 relation_kind rel)
1632 if (rel == VREL_VARYING)
1633 return false;
1635 int_range<2> rel_range;
1636 unsigned prec = TYPE_PRECISION (type);
1637 signop sgn = TYPE_SIGN (type);
1639 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1640 if (rel == VREL_EQ)
1641 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
1642 else if (rel == VREL_NE)
1643 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1644 VR_ANTI_RANGE);
1645 else if (TYPE_OVERFLOW_WRAPS (type))
1647 switch (rel)
1649 // For wrapping signed values and unsigned, if op1 > op2 or
1650 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1651 case VREL_GT:
1652 case VREL_LT:
1653 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1654 VR_ANTI_RANGE);
1655 break;
1656 default:
1657 return false;
1660 else
1662 switch (rel)
1664 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1665 case VREL_GT:
1666 rel_range = int_range<2> (type, wi::one (prec),
1667 wi::max_value (prec, sgn));
1668 break;
1669 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1670 case VREL_GE:
1671 rel_range = int_range<2> (type, wi::zero (prec),
1672 wi::max_value (prec, sgn));
1673 break;
1674 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1675 case VREL_LT:
1676 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1677 wi::minus_one (prec));
1678 break;
1679 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1680 case VREL_LE:
1681 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1682 wi::zero (prec));
1683 break;
1684 default:
1685 return false;
1688 lhs_range.intersect (rel_range);
1689 return true;
1692 bool
1693 operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1694 const irange &op1_range,
1695 const irange &op2_range,
1696 relation_kind rel) const
1698 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1699 rel);
1702 bool
1703 operator_minus::op1_range (irange &r, tree type,
1704 const irange &lhs,
1705 const irange &op2,
1706 relation_trio trio) const
1708 if (lhs.undefined_p ())
1709 return false;
1710 // Start with the default operation.
1711 range_op_handler minus (PLUS_EXPR, type);
1712 if (!minus)
1713 return false;
1714 bool res = minus.fold_range (r, type, lhs, op2);
1715 relation_kind rel = trio.lhs_op1 ();
1716 if (res)
1717 adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
1718 return res;
1722 bool
1723 operator_minus::op2_range (irange &r, tree type,
1724 const irange &lhs,
1725 const irange &op1,
1726 relation_trio) const
1728 if (lhs.undefined_p ())
1729 return false;
1730 return fold_range (r, type, op1, lhs);
1734 class operator_pointer_diff : public range_operator
1736 virtual bool op1_op2_relation_effect (irange &lhs_range,
1737 tree type,
1738 const irange &op1_range,
1739 const irange &op2_range,
1740 relation_kind rel) const;
1741 } op_pointer_diff;
1743 bool
1744 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
1745 const irange &op1_range,
1746 const irange &op2_range,
1747 relation_kind rel) const
1749 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1750 rel);
1754 class operator_min : public range_operator
1756 public:
1757 virtual void wi_fold (irange &r, tree type,
1758 const wide_int &lh_lb,
1759 const wide_int &lh_ub,
1760 const wide_int &rh_lb,
1761 const wide_int &rh_ub) const;
1762 } op_min;
1764 void
1765 operator_min::wi_fold (irange &r, tree type,
1766 const wide_int &lh_lb, const wide_int &lh_ub,
1767 const wide_int &rh_lb, const wide_int &rh_ub) const
1769 signop s = TYPE_SIGN (type);
1770 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1771 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1772 value_range_with_overflow (r, type, new_lb, new_ub);
1776 class operator_max : public range_operator
1778 public:
1779 virtual void wi_fold (irange &r, tree type,
1780 const wide_int &lh_lb,
1781 const wide_int &lh_ub,
1782 const wide_int &rh_lb,
1783 const wide_int &rh_ub) const;
1784 } op_max;
1786 void
1787 operator_max::wi_fold (irange &r, tree type,
1788 const wide_int &lh_lb, const wide_int &lh_ub,
1789 const wide_int &rh_lb, const wide_int &rh_ub) const
1791 signop s = TYPE_SIGN (type);
1792 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1793 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1794 value_range_with_overflow (r, type, new_lb, new_ub);
1798 class cross_product_operator : public range_operator
1800 public:
1801 // Perform an operation between two wide-ints and place the result
1802 // in R. Return true if the operation overflowed.
1803 virtual bool wi_op_overflows (wide_int &r,
1804 tree type,
1805 const wide_int &,
1806 const wide_int &) const = 0;
1808 // Calculate the cross product of two sets of sub-ranges and return it.
1809 void wi_cross_product (irange &r, tree type,
1810 const wide_int &lh_lb,
1811 const wide_int &lh_ub,
1812 const wide_int &rh_lb,
1813 const wide_int &rh_ub) const;
1816 // Calculate the cross product of two sets of ranges and return it.
1818 // Multiplications, divisions and shifts are a bit tricky to handle,
1819 // depending on the mix of signs we have in the two ranges, we need to
1820 // operate on different values to get the minimum and maximum values
1821 // for the new range. One approach is to figure out all the
1822 // variations of range combinations and do the operations.
1824 // However, this involves several calls to compare_values and it is
1825 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1826 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1827 // figure the smallest and largest values to form the new range.
1829 void
1830 cross_product_operator::wi_cross_product (irange &r, tree type,
1831 const wide_int &lh_lb,
1832 const wide_int &lh_ub,
1833 const wide_int &rh_lb,
1834 const wide_int &rh_ub) const
1836 wide_int cp1, cp2, cp3, cp4;
1837 // Default to varying.
1838 r.set_varying (type);
1840 // Compute the 4 cross operations, bailing if we get an overflow we
1841 // can't handle.
1842 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1843 return;
1844 if (wi::eq_p (lh_lb, lh_ub))
1845 cp3 = cp1;
1846 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1847 return;
1848 if (wi::eq_p (rh_lb, rh_ub))
1849 cp2 = cp1;
1850 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1851 return;
1852 if (wi::eq_p (lh_lb, lh_ub))
1853 cp4 = cp2;
1854 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1855 return;
1857 // Order pairs.
1858 signop sign = TYPE_SIGN (type);
1859 if (wi::gt_p (cp1, cp2, sign))
1860 std::swap (cp1, cp2);
1861 if (wi::gt_p (cp3, cp4, sign))
1862 std::swap (cp3, cp4);
1864 // Choose min and max from the ordered pairs.
1865 wide_int res_lb = wi::min (cp1, cp3, sign);
1866 wide_int res_ub = wi::max (cp2, cp4, sign);
1867 value_range_with_overflow (r, type, res_lb, res_ub);
1871 class operator_mult : public cross_product_operator
1873 using range_operator::op1_range;
1874 using range_operator::op2_range;
1875 public:
1876 virtual void wi_fold (irange &r, tree type,
1877 const wide_int &lh_lb,
1878 const wide_int &lh_ub,
1879 const wide_int &rh_lb,
1880 const wide_int &rh_ub) const final override;
1881 virtual bool wi_op_overflows (wide_int &res, tree type,
1882 const wide_int &w0, const wide_int &w1)
1883 const final override;
1884 virtual bool op1_range (irange &r, tree type,
1885 const irange &lhs,
1886 const irange &op2,
1887 relation_trio) const final override;
1888 virtual bool op2_range (irange &r, tree type,
1889 const irange &lhs,
1890 const irange &op1,
1891 relation_trio) const final override;
1892 } op_mult;
1894 bool
1895 operator_mult::op1_range (irange &r, tree type,
1896 const irange &lhs, const irange &op2,
1897 relation_trio) const
1899 tree offset;
1900 if (lhs.undefined_p ())
1901 return false;
1903 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1904 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1905 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1906 if (TYPE_OVERFLOW_WRAPS (type))
1907 return false;
1909 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1910 return range_op_handler (TRUNC_DIV_EXPR, type).fold_range (r, type,
1911 lhs, op2);
1912 return false;
1915 bool
1916 operator_mult::op2_range (irange &r, tree type,
1917 const irange &lhs, const irange &op1,
1918 relation_trio rel) const
1920 return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1923 bool
1924 operator_mult::wi_op_overflows (wide_int &res, tree type,
1925 const wide_int &w0, const wide_int &w1) const
1927 wi::overflow_type overflow = wi::OVF_NONE;
1928 signop sign = TYPE_SIGN (type);
1929 res = wi::mul (w0, w1, sign, &overflow);
1930 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1932 // For multiplication, the sign of the overflow is given
1933 // by the comparison of the signs of the operands.
1934 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1935 res = wi::max_value (w0.get_precision (), sign);
1936 else
1937 res = wi::min_value (w0.get_precision (), sign);
1938 return false;
1940 return overflow;
1943 void
1944 operator_mult::wi_fold (irange &r, tree type,
1945 const wide_int &lh_lb, const wide_int &lh_ub,
1946 const wide_int &rh_lb, const wide_int &rh_ub) const
1948 if (TYPE_OVERFLOW_UNDEFINED (type))
1950 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1951 return;
1954 // Multiply the ranges when overflow wraps. This is basically fancy
1955 // code so we don't drop to varying with an unsigned
1956 // [-3,-1]*[-3,-1].
1958 // This test requires 2*prec bits if both operands are signed and
1959 // 2*prec + 2 bits if either is not. Therefore, extend the values
1960 // using the sign of the result to PREC2. From here on out,
1961 // everthing is just signed math no matter what the input types
1962 // were.
1964 signop sign = TYPE_SIGN (type);
1965 unsigned prec = TYPE_PRECISION (type);
1966 widest2_int min0 = widest2_int::from (lh_lb, sign);
1967 widest2_int max0 = widest2_int::from (lh_ub, sign);
1968 widest2_int min1 = widest2_int::from (rh_lb, sign);
1969 widest2_int max1 = widest2_int::from (rh_ub, sign);
1970 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1971 widest2_int size = sizem1 + 1;
1973 // Canonicalize the intervals.
1974 if (sign == UNSIGNED)
1976 if (wi::ltu_p (size, min0 + max0))
1978 min0 -= size;
1979 max0 -= size;
1981 if (wi::ltu_p (size, min1 + max1))
1983 min1 -= size;
1984 max1 -= size;
1988 // Sort the 4 products so that min is in prod0 and max is in
1989 // prod3.
1990 widest2_int prod0 = min0 * min1;
1991 widest2_int prod1 = min0 * max1;
1992 widest2_int prod2 = max0 * min1;
1993 widest2_int prod3 = max0 * max1;
1995 // min0min1 > max0max1
1996 if (prod0 > prod3)
1997 std::swap (prod0, prod3);
1999 // min0max1 > max0min1
2000 if (prod1 > prod2)
2001 std::swap (prod1, prod2);
2003 if (prod0 > prod1)
2004 std::swap (prod0, prod1);
2006 if (prod2 > prod3)
2007 std::swap (prod2, prod3);
2009 // diff = max - min
2010 prod2 = prod3 - prod0;
2011 if (wi::geu_p (prod2, sizem1))
2013 // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
2014 if (TYPE_UNSIGNED (type) && rh_lb == rh_ub
2015 && wi::exact_log2 (rh_lb) != -1 && prec > 1)
2017 r.set (type, rh_lb, wi::max_value (prec, sign));
2018 int_range<2> zero;
2019 zero.set_zero (type);
2020 r.union_ (zero);
2022 else
2023 // The range covers all values.
2024 r.set_varying (type);
2026 else
2028 wide_int new_lb = wide_int::from (prod0, prec, sign);
2029 wide_int new_ub = wide_int::from (prod3, prec, sign);
2030 create_possibly_reversed_range (r, type, new_lb, new_ub);
2035 class operator_div : public cross_product_operator
2037 public:
2038 virtual void wi_fold (irange &r, tree type,
2039 const wide_int &lh_lb,
2040 const wide_int &lh_ub,
2041 const wide_int &rh_lb,
2042 const wide_int &rh_ub) const final override;
2043 virtual bool wi_op_overflows (wide_int &res, tree type,
2044 const wide_int &, const wide_int &)
2045 const final override;
2048 bool
2049 operator_div::wi_op_overflows (wide_int &res, tree type,
2050 const wide_int &w0, const wide_int &w1) const
2052 if (w1 == 0)
2053 return true;
2055 wi::overflow_type overflow = wi::OVF_NONE;
2056 signop sign = TYPE_SIGN (type);
2058 switch (m_code)
2060 case EXACT_DIV_EXPR:
2061 case TRUNC_DIV_EXPR:
2062 res = wi::div_trunc (w0, w1, sign, &overflow);
2063 break;
2064 case FLOOR_DIV_EXPR:
2065 res = wi::div_floor (w0, w1, sign, &overflow);
2066 break;
2067 case ROUND_DIV_EXPR:
2068 res = wi::div_round (w0, w1, sign, &overflow);
2069 break;
2070 case CEIL_DIV_EXPR:
2071 res = wi::div_ceil (w0, w1, sign, &overflow);
2072 break;
2073 default:
2074 gcc_unreachable ();
2077 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2079 // For division, the only case is -INF / -1 = +INF.
2080 res = wi::max_value (w0.get_precision (), sign);
2081 return false;
2083 return overflow;
2086 void
2087 operator_div::wi_fold (irange &r, tree type,
2088 const wide_int &lh_lb, const wide_int &lh_ub,
2089 const wide_int &rh_lb, const wide_int &rh_ub) const
2091 const wide_int dividend_min = lh_lb;
2092 const wide_int dividend_max = lh_ub;
2093 const wide_int divisor_min = rh_lb;
2094 const wide_int divisor_max = rh_ub;
2095 signop sign = TYPE_SIGN (type);
2096 unsigned prec = TYPE_PRECISION (type);
2097 wide_int extra_min, extra_max;
2099 // If we know we won't divide by zero, just do the division.
2100 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
2102 wi_cross_product (r, type, dividend_min, dividend_max,
2103 divisor_min, divisor_max);
2104 return;
2107 // If we're definitely dividing by zero, there's nothing to do.
2108 if (wi_zero_p (type, divisor_min, divisor_max))
2110 r.set_undefined ();
2111 return;
2114 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2115 // skip any division by zero.
2117 // First divide by the negative numbers, if any.
2118 if (wi::neg_p (divisor_min, sign))
2119 wi_cross_product (r, type, dividend_min, dividend_max,
2120 divisor_min, wi::minus_one (prec));
2121 else
2122 r.set_undefined ();
2124 // Then divide by the non-zero positive numbers, if any.
2125 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
2127 int_range_max tmp;
2128 wi_cross_product (tmp, type, dividend_min, dividend_max,
2129 wi::one (prec), divisor_max);
2130 r.union_ (tmp);
2132 // We shouldn't still have undefined here.
2133 gcc_checking_assert (!r.undefined_p ());
2137 class operator_exact_divide : public operator_div
2139 using range_operator::op1_range;
2140 public:
2141 virtual bool op1_range (irange &r, tree type,
2142 const irange &lhs,
2143 const irange &op2,
2144 relation_trio) const;
2146 } op_exact_div;
2148 bool
2149 operator_exact_divide::op1_range (irange &r, tree type,
2150 const irange &lhs,
2151 const irange &op2,
2152 relation_trio) const
2154 if (lhs.undefined_p ())
2155 return false;
2156 tree offset;
2157 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2158 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2159 // We wont bother trying to enumerate all the in between stuff :-P
2160 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
2161 // the time however.
2162 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2163 if (op2.singleton_p (&offset)
2164 && !integer_zerop (offset))
2165 return range_op_handler (MULT_EXPR, type).fold_range (r, type, lhs, op2);
2166 return false;
2170 class operator_lshift : public cross_product_operator
2172 using range_operator::fold_range;
2173 using range_operator::op1_range;
2174 public:
2175 virtual bool op1_range (irange &r, tree type,
2176 const irange &lhs,
2177 const irange &op2,
2178 relation_trio rel = TRIO_VARYING) const;
2179 virtual bool fold_range (irange &r, tree type,
2180 const irange &op1,
2181 const irange &op2,
2182 relation_trio rel = TRIO_VARYING) const;
2184 virtual void wi_fold (irange &r, tree type,
2185 const wide_int &lh_lb, const wide_int &lh_ub,
2186 const wide_int &rh_lb, const wide_int &rh_ub) const;
2187 virtual bool wi_op_overflows (wide_int &res,
2188 tree type,
2189 const wide_int &,
2190 const wide_int &) const;
2191 } op_lshift;
2193 class operator_rshift : public cross_product_operator
2195 using range_operator::fold_range;
2196 using range_operator::op1_range;
2197 using range_operator::lhs_op1_relation;
2198 public:
2199 virtual bool fold_range (irange &r, tree type,
2200 const irange &op1,
2201 const irange &op2,
2202 relation_trio rel = TRIO_VARYING) const;
2203 virtual void wi_fold (irange &r, tree type,
2204 const wide_int &lh_lb,
2205 const wide_int &lh_ub,
2206 const wide_int &rh_lb,
2207 const wide_int &rh_ub) const;
2208 virtual bool wi_op_overflows (wide_int &res,
2209 tree type,
2210 const wide_int &w0,
2211 const wide_int &w1) const;
2212 virtual bool op1_range (irange &, tree type,
2213 const irange &lhs,
2214 const irange &op2,
2215 relation_trio rel = TRIO_VARYING) const;
2216 virtual relation_kind lhs_op1_relation (const irange &lhs,
2217 const irange &op1,
2218 const irange &op2,
2219 relation_kind rel) const;
2220 } op_rshift;
2223 relation_kind
2224 operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
2225 const irange &op1,
2226 const irange &op2,
2227 relation_kind) const
2229 // If both operands range are >= 0, then the LHS <= op1.
2230 if (!op1.undefined_p () && !op2.undefined_p ()
2231 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
2232 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
2233 return VREL_LE;
2234 return VREL_VARYING;
2237 bool
2238 operator_lshift::fold_range (irange &r, tree type,
2239 const irange &op1,
2240 const irange &op2,
2241 relation_trio rel) const
2243 int_range_max shift_range;
2244 if (!get_shift_range (shift_range, type, op2))
2246 if (op2.undefined_p ())
2247 r.set_undefined ();
2248 else
2249 r.set_zero (type);
2250 return true;
2253 // Transform left shifts by constants into multiplies.
2254 if (shift_range.singleton_p ())
2256 unsigned shift = shift_range.lower_bound ().to_uhwi ();
2257 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2258 int_range<1> mult (type, tmp, tmp);
2260 // Force wrapping multiplication.
2261 bool saved_flag_wrapv = flag_wrapv;
2262 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2263 flag_wrapv = 1;
2264 flag_wrapv_pointer = 1;
2265 bool b = op_mult.fold_range (r, type, op1, mult);
2266 flag_wrapv = saved_flag_wrapv;
2267 flag_wrapv_pointer = saved_flag_wrapv_pointer;
2268 return b;
2270 else
2271 // Otherwise, invoke the generic fold routine.
2272 return range_operator::fold_range (r, type, op1, shift_range, rel);
2275 void
2276 operator_lshift::wi_fold (irange &r, tree type,
2277 const wide_int &lh_lb, const wide_int &lh_ub,
2278 const wide_int &rh_lb, const wide_int &rh_ub) const
2280 signop sign = TYPE_SIGN (type);
2281 unsigned prec = TYPE_PRECISION (type);
2282 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2283 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2284 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2285 // overflow. However, for that to happen, rh.max needs to be zero,
2286 // which means rh is a singleton range of zero, which means we simply return
2287 // [lh_lb, lh_ub] as the range.
2288 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2290 r = int_range<2> (type, lh_lb, lh_ub);
2291 return;
2294 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2295 wide_int complement = ~(bound - 1);
2296 wide_int low_bound, high_bound;
2297 bool in_bounds = false;
2299 if (sign == UNSIGNED)
2301 low_bound = bound;
2302 high_bound = complement;
2303 if (wi::ltu_p (lh_ub, low_bound))
2305 // [5, 6] << [1, 2] == [10, 24].
2306 // We're shifting out only zeroes, the value increases
2307 // monotonically.
2308 in_bounds = true;
2310 else if (wi::ltu_p (high_bound, lh_lb))
2312 // [0xffffff00, 0xffffffff] << [1, 2]
2313 // == [0xfffffc00, 0xfffffffe].
2314 // We're shifting out only ones, the value decreases
2315 // monotonically.
2316 in_bounds = true;
2319 else
2321 // [-1, 1] << [1, 2] == [-4, 4]
2322 low_bound = complement;
2323 high_bound = bound;
2324 if (wi::lts_p (lh_ub, high_bound)
2325 && wi::lts_p (low_bound, lh_lb))
2327 // For non-negative numbers, we're shifting out only zeroes,
2328 // the value increases monotonically. For negative numbers,
2329 // we're shifting out only ones, the value decreases
2330 // monotonically.
2331 in_bounds = true;
2335 if (in_bounds)
2336 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2337 else
2338 r.set_varying (type);
2341 bool
2342 operator_lshift::wi_op_overflows (wide_int &res, tree type,
2343 const wide_int &w0, const wide_int &w1) const
2345 signop sign = TYPE_SIGN (type);
2346 if (wi::neg_p (w1))
2348 // It's unclear from the C standard whether shifts can overflow.
2349 // The following code ignores overflow; perhaps a C standard
2350 // interpretation ruling is needed.
2351 res = wi::rshift (w0, -w1, sign);
2353 else
2354 res = wi::lshift (w0, w1);
2355 return false;
2358 bool
2359 operator_lshift::op1_range (irange &r,
2360 tree type,
2361 const irange &lhs,
2362 const irange &op2,
2363 relation_trio) const
2365 if (lhs.undefined_p ())
2366 return false;
2367 tree shift_amount;
2369 if (!lhs.contains_p (build_zero_cst (type)))
2370 r.set_nonzero (type);
2371 else
2372 r.set_varying (type);
2374 if (op2.singleton_p (&shift_amount))
2376 wide_int shift = wi::to_wide (shift_amount);
2377 if (wi::lt_p (shift, 0, SIGNED))
2378 return false;
2379 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2380 TYPE_PRECISION (op2.type ())),
2381 UNSIGNED))
2382 return false;
2383 if (shift == 0)
2385 r.intersect (lhs);
2386 return true;
2389 // Work completely in unsigned mode to start.
2390 tree utype = type;
2391 int_range_max tmp_range;
2392 if (TYPE_SIGN (type) == SIGNED)
2394 int_range_max tmp = lhs;
2395 utype = unsigned_type_for (type);
2396 range_cast (tmp, utype);
2397 op_rshift.fold_range (tmp_range, utype, tmp, op2);
2399 else
2400 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2402 // Start with ranges which can produce the LHS by right shifting the
2403 // result by the shift amount.
2404 // ie [0x08, 0xF0] = op1 << 2 will start with
2405 // [00001000, 11110000] = op1 << 2
2406 // [0x02, 0x4C] aka [00000010, 00111100]
2408 // Then create a range from the LB with the least significant upper bit
2409 // set, to the upper bound with all the bits set.
2410 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2412 // Ideally we do this for each subrange, but just lump them all for now.
2413 unsigned low_bits = TYPE_PRECISION (utype)
2414 - TREE_INT_CST_LOW (shift_amount);
2415 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2416 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2417 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2418 int_range<2> fill_range (utype, new_lb, new_ub);
2419 tmp_range.union_ (fill_range);
2421 if (utype != type)
2422 range_cast (tmp_range, type);
2424 r.intersect (tmp_range);
2425 return true;
2428 return !r.varying_p ();
2431 bool
2432 operator_rshift::op1_range (irange &r,
2433 tree type,
2434 const irange &lhs,
2435 const irange &op2,
2436 relation_trio) const
2438 tree shift;
2439 if (lhs.undefined_p ())
2440 return false;
2441 if (op2.singleton_p (&shift))
2443 // Ignore nonsensical shifts.
2444 unsigned prec = TYPE_PRECISION (type);
2445 if (wi::ge_p (wi::to_wide (shift),
2446 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
2447 UNSIGNED))
2448 return false;
2449 if (wi::to_wide (shift) == 0)
2451 r = lhs;
2452 return true;
2455 // Folding the original operation may discard some impossible
2456 // ranges from the LHS.
2457 int_range_max lhs_refined;
2458 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2459 lhs_refined.intersect (lhs);
2460 if (lhs_refined.undefined_p ())
2462 r.set_undefined ();
2463 return true;
2465 int_range_max shift_range (shift, shift);
2466 int_range_max lb, ub;
2467 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2468 // LHS
2469 // 0000 0111 = OP1 >> 3
2471 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2472 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2473 // right hand side (0x07).
2474 tree mask = fold_build1 (BIT_NOT_EXPR, type,
2475 fold_build2 (LSHIFT_EXPR, type,
2476 build_minus_one_cst (type),
2477 shift));
2478 int_range_max mask_range (build_zero_cst (type), mask);
2479 op_plus.fold_range (ub, type, lb, mask_range);
2480 r = lb;
2481 r.union_ (ub);
2482 if (!lhs_refined.contains_p (build_zero_cst (type)))
2484 mask_range.invert ();
2485 r.intersect (mask_range);
2487 return true;
2489 return false;
2492 bool
2493 operator_rshift::wi_op_overflows (wide_int &res,
2494 tree type,
2495 const wide_int &w0,
2496 const wide_int &w1) const
2498 signop sign = TYPE_SIGN (type);
2499 if (wi::neg_p (w1))
2500 res = wi::lshift (w0, -w1);
2501 else
2503 // It's unclear from the C standard whether shifts can overflow.
2504 // The following code ignores overflow; perhaps a C standard
2505 // interpretation ruling is needed.
2506 res = wi::rshift (w0, w1, sign);
2508 return false;
2511 bool
2512 operator_rshift::fold_range (irange &r, tree type,
2513 const irange &op1,
2514 const irange &op2,
2515 relation_trio rel) const
2517 int_range_max shift;
2518 if (!get_shift_range (shift, type, op2))
2520 if (op2.undefined_p ())
2521 r.set_undefined ();
2522 else
2523 r.set_zero (type);
2524 return true;
2527 return range_operator::fold_range (r, type, op1, shift, rel);
2530 void
2531 operator_rshift::wi_fold (irange &r, tree type,
2532 const wide_int &lh_lb, const wide_int &lh_ub,
2533 const wide_int &rh_lb, const wide_int &rh_ub) const
2535 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2539 class operator_cast: public range_operator
2541 using range_operator::fold_range;
2542 using range_operator::op1_range;
2543 public:
2544 virtual bool fold_range (irange &r, tree type,
2545 const irange &op1,
2546 const irange &op2,
2547 relation_trio rel = TRIO_VARYING) const;
2548 virtual bool op1_range (irange &r, tree type,
2549 const irange &lhs,
2550 const irange &op2,
2551 relation_trio rel = TRIO_VARYING) const;
2552 virtual relation_kind lhs_op1_relation (const irange &lhs,
2553 const irange &op1,
2554 const irange &op2,
2555 relation_kind) const;
2556 private:
2557 bool truncating_cast_p (const irange &inner, const irange &outer) const;
2558 bool inside_domain_p (const wide_int &min, const wide_int &max,
2559 const irange &outer) const;
2560 void fold_pair (irange &r, unsigned index, const irange &inner,
2561 const irange &outer) const;
2564 // Add a partial equivalence between the LHS and op1 for casts.
2566 relation_kind
2567 operator_cast::lhs_op1_relation (const irange &lhs,
2568 const irange &op1,
2569 const irange &op2 ATTRIBUTE_UNUSED,
2570 relation_kind) const
2572 if (lhs.undefined_p () || op1.undefined_p ())
2573 return VREL_VARYING;
2574 unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
2575 unsigned op1_prec = TYPE_PRECISION (op1.type ());
2576 // If the result gets sign extended into a larger type check first if this
2577 // qualifies as a partial equivalence.
2578 if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
2580 // If the result is sign extended, and the LHS is larger than op1,
2581 // check if op1's range can be negative as the sign extention will
2582 // cause the upper bits to be 1 instead of 0, invalidating the PE.
2583 int_range<3> negs = range_negatives (op1.type ());
2584 negs.intersect (op1);
2585 if (!negs.undefined_p ())
2586 return VREL_VARYING;
2589 unsigned prec = MIN (lhs_prec, op1_prec);
2590 return bits_to_pe (prec);
2593 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2595 inline bool
2596 operator_cast::truncating_cast_p (const irange &inner,
2597 const irange &outer) const
2599 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2602 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2604 bool
2605 operator_cast::inside_domain_p (const wide_int &min,
2606 const wide_int &max,
2607 const irange &range) const
2609 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
2610 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
2611 signop domain_sign = TYPE_SIGN (range.type ());
2612 return (wi::le_p (min, domain_max, domain_sign)
2613 && wi::le_p (max, domain_max, domain_sign)
2614 && wi::ge_p (min, domain_min, domain_sign)
2615 && wi::ge_p (max, domain_min, domain_sign));
2619 // Helper for fold_range which work on a pair at a time.
2621 void
2622 operator_cast::fold_pair (irange &r, unsigned index,
2623 const irange &inner,
2624 const irange &outer) const
2626 tree inner_type = inner.type ();
2627 tree outer_type = outer.type ();
2628 signop inner_sign = TYPE_SIGN (inner_type);
2629 unsigned outer_prec = TYPE_PRECISION (outer_type);
2631 // check to see if casting from INNER to OUTER is a conversion that
2632 // fits in the resulting OUTER type.
2633 wide_int inner_lb = inner.lower_bound (index);
2634 wide_int inner_ub = inner.upper_bound (index);
2635 if (truncating_cast_p (inner, outer))
2637 // We may be able to accomodate a truncating cast if the
2638 // resulting range can be represented in the target type...
2639 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2640 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2641 inner_sign) != 0)
2643 r.set_varying (outer_type);
2644 return;
2647 // ...but we must still verify that the final range fits in the
2648 // domain. This catches -fstrict-enum restrictions where the domain
2649 // range is smaller than what fits in the underlying type.
2650 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2651 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2652 if (inside_domain_p (min, max, outer))
2653 create_possibly_reversed_range (r, outer_type, min, max);
2654 else
2655 r.set_varying (outer_type);
2659 bool
2660 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2661 const irange &inner,
2662 const irange &outer,
2663 relation_trio) const
2665 if (empty_range_varying (r, type, inner, outer))
2666 return true;
2668 gcc_checking_assert (outer.varying_p ());
2669 gcc_checking_assert (inner.num_pairs () > 0);
2671 // Avoid a temporary by folding the first pair directly into the result.
2672 fold_pair (r, 0, inner, outer);
2674 // Then process any additonal pairs by unioning with their results.
2675 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2677 int_range_max tmp;
2678 fold_pair (tmp, x, inner, outer);
2679 r.union_ (tmp);
2680 if (r.varying_p ())
2681 return true;
2684 // Update the nonzero mask. Truncating casts are problematic unless
2685 // the conversion fits in the resulting outer type.
2686 wide_int nz = inner.get_nonzero_bits ();
2687 if (truncating_cast_p (inner, outer)
2688 && wi::rshift (nz, wi::uhwi (TYPE_PRECISION (outer.type ()),
2689 TYPE_PRECISION (inner.type ())),
2690 TYPE_SIGN (inner.type ())) != 0)
2691 return true;
2692 nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
2693 r.set_nonzero_bits (nz);
2695 return true;
2698 bool
2699 operator_cast::op1_range (irange &r, tree type,
2700 const irange &lhs,
2701 const irange &op2,
2702 relation_trio) const
2704 if (lhs.undefined_p ())
2705 return false;
2706 tree lhs_type = lhs.type ();
2707 gcc_checking_assert (types_compatible_p (op2.type(), type));
2709 // If we are calculating a pointer, shortcut to what we really care about.
2710 if (POINTER_TYPE_P (type))
2712 // Conversion from other pointers or a constant (including 0/NULL)
2713 // are straightforward.
2714 if (POINTER_TYPE_P (lhs.type ())
2715 || (lhs.singleton_p ()
2716 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2718 r = lhs;
2719 range_cast (r, type);
2721 else
2723 // If the LHS is not a pointer nor a singleton, then it is
2724 // either VARYING or non-zero.
2725 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
2726 r.set_nonzero (type);
2727 else
2728 r.set_varying (type);
2730 r.intersect (op2);
2731 return true;
2734 if (truncating_cast_p (op2, lhs))
2736 if (lhs.varying_p ())
2737 r.set_varying (type);
2738 else
2740 // We want to insert the LHS as an unsigned value since it
2741 // would not trigger the signed bit of the larger type.
2742 int_range_max converted_lhs = lhs;
2743 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2744 range_cast (converted_lhs, type);
2745 // Start by building the positive signed outer range for the type.
2746 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2747 TYPE_PRECISION (type));
2748 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
2749 SIGNED));
2750 // For the signed part, we need to simply union the 2 ranges now.
2751 r.union_ (converted_lhs);
2753 // Create maximal negative number outside of LHS bits.
2754 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2755 TYPE_PRECISION (type));
2756 // Add this to the unsigned LHS range(s).
2757 int_range_max lim_range (type, lim, lim);
2758 int_range_max lhs_neg;
2759 range_op_handler (PLUS_EXPR, type).fold_range (lhs_neg, type,
2760 converted_lhs,
2761 lim_range);
2762 // lhs_neg now has all the negative versions of the LHS.
2763 // Now union in all the values from SIGNED MIN (0x80000) to
2764 // lim-1 in order to fill in all the ranges with the upper
2765 // bits set.
2767 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2768 // we don't need to create a range from min to lim-1
2769 // calculate neg range traps trying to create [lim, lim - 1].
2770 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2771 if (lim != min_val)
2773 int_range_max neg (type,
2774 wi::min_value (TYPE_PRECISION (type),
2775 SIGNED),
2776 lim - 1);
2777 lhs_neg.union_ (neg);
2779 // And finally, munge the signed and unsigned portions.
2780 r.union_ (lhs_neg);
2782 // And intersect with any known value passed in the extra operand.
2783 r.intersect (op2);
2784 return true;
2787 int_range_max tmp;
2788 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2789 tmp = lhs;
2790 else
2792 // The cast is not truncating, and the range is restricted to
2793 // the range of the RHS by this assignment.
2795 // Cast the range of the RHS to the type of the LHS.
2796 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2797 // Intersect this with the LHS range will produce the range,
2798 // which will be cast to the RHS type before returning.
2799 tmp.intersect (lhs);
2802 // Cast the calculated range to the type of the RHS.
2803 fold_range (r, type, tmp, int_range<1> (type));
2804 return true;
2808 class operator_logical_and : public range_operator
2810 using range_operator::fold_range;
2811 using range_operator::op1_range;
2812 using range_operator::op2_range;
2813 public:
2814 virtual bool fold_range (irange &r, tree type,
2815 const irange &lh,
2816 const irange &rh,
2817 relation_trio rel = TRIO_VARYING) const;
2818 virtual bool op1_range (irange &r, tree type,
2819 const irange &lhs,
2820 const irange &op2,
2821 relation_trio rel = TRIO_VARYING) const;
2822 virtual bool op2_range (irange &r, tree type,
2823 const irange &lhs,
2824 const irange &op1,
2825 relation_trio rel = TRIO_VARYING) const;
2826 } op_logical_and;
2829 bool
2830 operator_logical_and::fold_range (irange &r, tree type,
2831 const irange &lh,
2832 const irange &rh,
2833 relation_trio) const
2835 if (empty_range_varying (r, type, lh, rh))
2836 return true;
2838 // 0 && anything is 0.
2839 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
2840 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
2841 r = range_false (type);
2842 else if (lh.contains_p (build_zero_cst (lh.type ()))
2843 || rh.contains_p (build_zero_cst (rh.type ())))
2844 // To reach this point, there must be a logical 1 on each side, and
2845 // the only remaining question is whether there is a zero or not.
2846 r = range_true_and_false (type);
2847 else
2848 r = range_true (type);
2849 return true;
2852 bool
2853 operator_logical_and::op1_range (irange &r, tree type,
2854 const irange &lhs,
2855 const irange &op2 ATTRIBUTE_UNUSED,
2856 relation_trio) const
2858 switch (get_bool_state (r, lhs, type))
2860 case BRS_TRUE:
2861 // A true result means both sides of the AND must be true.
2862 r = range_true (type);
2863 break;
2864 default:
2865 // Any other result means only one side has to be false, the
2866 // other side can be anything. So we cannot be sure of any
2867 // result here.
2868 r = range_true_and_false (type);
2869 break;
2871 return true;
2874 bool
2875 operator_logical_and::op2_range (irange &r, tree type,
2876 const irange &lhs,
2877 const irange &op1,
2878 relation_trio) const
2880 return operator_logical_and::op1_range (r, type, lhs, op1);
2884 class operator_bitwise_and : public range_operator
2886 using range_operator::op1_range;
2887 using range_operator::op2_range;
2888 public:
2889 virtual bool op1_range (irange &r, tree type,
2890 const irange &lhs,
2891 const irange &op2,
2892 relation_trio rel = TRIO_VARYING) const;
2893 virtual bool op2_range (irange &r, tree type,
2894 const irange &lhs,
2895 const irange &op1,
2896 relation_trio rel = TRIO_VARYING) const;
2897 virtual void wi_fold (irange &r, tree type,
2898 const wide_int &lh_lb,
2899 const wide_int &lh_ub,
2900 const wide_int &rh_lb,
2901 const wide_int &rh_ub) const;
2902 virtual relation_kind lhs_op1_relation (const irange &lhs,
2903 const irange &op1,
2904 const irange &op2,
2905 relation_kind) const;
2906 private:
2907 void simple_op1_range_solver (irange &r, tree type,
2908 const irange &lhs,
2909 const irange &op2) const;
2910 } op_bitwise_and;
2913 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
2914 // by considering the number of leading redundant sign bit copies.
2915 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
2916 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
2917 static bool
2918 wi_optimize_signed_bitwise_op (irange &r, tree type,
2919 const wide_int &lh_lb, const wide_int &lh_ub,
2920 const wide_int &rh_lb, const wide_int &rh_ub)
2922 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
2923 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
2924 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
2925 if (new_clrsb == 0)
2926 return false;
2927 int type_prec = TYPE_PRECISION (type);
2928 int rprec = (type_prec - new_clrsb) - 1;
2929 value_range_with_overflow (r, type,
2930 wi::mask (rprec, true, type_prec),
2931 wi::mask (rprec, false, type_prec));
2932 return true;
2935 // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
2936 // the LHS and op1.
2938 relation_kind
2939 operator_bitwise_and::lhs_op1_relation (const irange &lhs,
2940 const irange &op1,
2941 const irange &op2,
2942 relation_kind) const
2944 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
2945 return VREL_VARYING;
2946 if (!op2.singleton_p ())
2947 return VREL_VARYING;
2948 // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
2949 int prec1 = TYPE_PRECISION (op1.type ());
2950 int prec2 = TYPE_PRECISION (op2.type ());
2951 int mask_prec = 0;
2952 wide_int mask = op2.lower_bound ();
2953 if (wi::eq_p (mask, wi::mask (8, false, prec2)))
2954 mask_prec = 8;
2955 else if (wi::eq_p (mask, wi::mask (16, false, prec2)))
2956 mask_prec = 16;
2957 else if (wi::eq_p (mask, wi::mask (32, false, prec2)))
2958 mask_prec = 32;
2959 else if (wi::eq_p (mask, wi::mask (64, false, prec2)))
2960 mask_prec = 64;
2961 return bits_to_pe (MIN (prec1, mask_prec));
2964 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2965 // possible. Basically, see if we can optimize:
2967 // [LB, UB] op Z
2968 // into:
2969 // [LB op Z, UB op Z]
2971 // If the optimization was successful, accumulate the range in R and
2972 // return TRUE.
2974 static bool
2975 wi_optimize_and_or (irange &r,
2976 enum tree_code code,
2977 tree type,
2978 const wide_int &lh_lb, const wide_int &lh_ub,
2979 const wide_int &rh_lb, const wide_int &rh_ub)
2981 // Calculate the singleton mask among the ranges, if any.
2982 wide_int lower_bound, upper_bound, mask;
2983 if (wi::eq_p (rh_lb, rh_ub))
2985 mask = rh_lb;
2986 lower_bound = lh_lb;
2987 upper_bound = lh_ub;
2989 else if (wi::eq_p (lh_lb, lh_ub))
2991 mask = lh_lb;
2992 lower_bound = rh_lb;
2993 upper_bound = rh_ub;
2995 else
2996 return false;
2998 // If Z is a constant which (for op | its bitwise not) has n
2999 // consecutive least significant bits cleared followed by m 1
3000 // consecutive bits set immediately above it and either
3001 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
3003 // The least significant n bits of all the values in the range are
3004 // cleared or set, the m bits above it are preserved and any bits
3005 // above these are required to be the same for all values in the
3006 // range.
3007 wide_int w = mask;
3008 int m = 0, n = 0;
3009 if (code == BIT_IOR_EXPR)
3010 w = ~w;
3011 if (wi::eq_p (w, 0))
3012 n = w.get_precision ();
3013 else
3015 n = wi::ctz (w);
3016 w = ~(w | wi::mask (n, false, w.get_precision ()));
3017 if (wi::eq_p (w, 0))
3018 m = w.get_precision () - n;
3019 else
3020 m = wi::ctz (w) - n;
3022 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
3023 if ((new_mask & lower_bound) != (new_mask & upper_bound))
3024 return false;
3026 wide_int res_lb, res_ub;
3027 if (code == BIT_AND_EXPR)
3029 res_lb = wi::bit_and (lower_bound, mask);
3030 res_ub = wi::bit_and (upper_bound, mask);
3032 else if (code == BIT_IOR_EXPR)
3034 res_lb = wi::bit_or (lower_bound, mask);
3035 res_ub = wi::bit_or (upper_bound, mask);
3037 else
3038 gcc_unreachable ();
3039 value_range_with_overflow (r, type, res_lb, res_ub);
3041 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
3042 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
3044 int_range<2> tmp;
3045 tmp.set_nonzero (type);
3046 r.intersect (tmp);
3048 return true;
3051 // For range [LB, UB] compute two wide_int bit masks.
3053 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3054 // for all numbers in the range the bit is 0, otherwise it might be 0
3055 // or 1.
3057 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3058 // for all numbers in the range the bit is 1, otherwise it might be 0
3059 // or 1.
3061 void
3062 wi_set_zero_nonzero_bits (tree type,
3063 const wide_int &lb, const wide_int &ub,
3064 wide_int &maybe_nonzero,
3065 wide_int &mustbe_nonzero)
3067 signop sign = TYPE_SIGN (type);
3069 if (wi::eq_p (lb, ub))
3070 maybe_nonzero = mustbe_nonzero = lb;
3071 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
3073 wide_int xor_mask = lb ^ ub;
3074 maybe_nonzero = lb | ub;
3075 mustbe_nonzero = lb & ub;
3076 if (xor_mask != 0)
3078 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
3079 maybe_nonzero.get_precision ());
3080 maybe_nonzero = maybe_nonzero | mask;
3081 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
3084 else
3086 maybe_nonzero = wi::minus_one (lb.get_precision ());
3087 mustbe_nonzero = wi::zero (lb.get_precision ());
3091 void
3092 operator_bitwise_and::wi_fold (irange &r, tree type,
3093 const wide_int &lh_lb,
3094 const wide_int &lh_ub,
3095 const wide_int &rh_lb,
3096 const wide_int &rh_ub) const
3098 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3099 return;
3101 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3102 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3103 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3104 maybe_nonzero_lh, mustbe_nonzero_lh);
3105 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3106 maybe_nonzero_rh, mustbe_nonzero_rh);
3108 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
3109 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
3110 signop sign = TYPE_SIGN (type);
3111 unsigned prec = TYPE_PRECISION (type);
3112 // If both input ranges contain only negative values, we can
3113 // truncate the result range maximum to the minimum of the
3114 // input range maxima.
3115 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
3117 new_ub = wi::min (new_ub, lh_ub, sign);
3118 new_ub = wi::min (new_ub, rh_ub, sign);
3120 // If either input range contains only non-negative values
3121 // we can truncate the result range maximum to the respective
3122 // maximum of the input range.
3123 if (wi::ge_p (lh_lb, 0, sign))
3124 new_ub = wi::min (new_ub, lh_ub, sign);
3125 if (wi::ge_p (rh_lb, 0, sign))
3126 new_ub = wi::min (new_ub, rh_ub, sign);
3127 // PR68217: In case of signed & sign-bit-CST should
3128 // result in [-INF, 0] instead of [-INF, INF].
3129 if (wi::gt_p (new_lb, new_ub, sign))
3131 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
3132 if (sign == SIGNED
3133 && ((wi::eq_p (lh_lb, lh_ub)
3134 && !wi::cmps (lh_lb, sign_bit))
3135 || (wi::eq_p (rh_lb, rh_ub)
3136 && !wi::cmps (rh_lb, sign_bit))))
3138 new_lb = wi::min_value (prec, sign);
3139 new_ub = wi::zero (prec);
3142 // If the limits got swapped around, return varying.
3143 if (wi::gt_p (new_lb, new_ub,sign))
3145 if (sign == SIGNED
3146 && wi_optimize_signed_bitwise_op (r, type,
3147 lh_lb, lh_ub,
3148 rh_lb, rh_ub))
3149 return;
3150 r.set_varying (type);
3152 else
3153 value_range_with_overflow (r, type, new_lb, new_ub);
3156 static void
3157 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
3159 if (!lhs.contains_p (build_zero_cst (type)))
3160 r = range_nonzero (type);
3161 else
3162 r.set_varying (type);
3165 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3166 (otherwise return VAL). VAL and MASK must be zero-extended for
3167 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3168 (to transform signed values into unsigned) and at the end xor
3169 SGNBIT back. */
3171 wide_int
3172 masked_increment (const wide_int &val_in, const wide_int &mask,
3173 const wide_int &sgnbit, unsigned int prec)
3175 wide_int bit = wi::one (prec), res;
3176 unsigned int i;
3178 wide_int val = val_in ^ sgnbit;
3179 for (i = 0; i < prec; i++, bit += bit)
3181 res = mask;
3182 if ((res & bit) == 0)
3183 continue;
3184 res = bit - 1;
3185 res = wi::bit_and_not (val + bit, res);
3186 res &= mask;
3187 if (wi::gtu_p (res, val))
3188 return res ^ sgnbit;
3190 return val ^ sgnbit;
3193 // This was shamelessly stolen from register_edge_assert_for_2 and
3194 // adjusted to work with iranges.
3196 void
3197 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
3198 const irange &lhs,
3199 const irange &op2) const
3201 if (!op2.singleton_p ())
3203 set_nonzero_range_from_mask (r, type, lhs);
3204 return;
3206 unsigned int nprec = TYPE_PRECISION (type);
3207 wide_int cst2v = op2.lower_bound ();
3208 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
3209 wide_int sgnbit;
3210 if (cst2n)
3211 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3212 else
3213 sgnbit = wi::zero (nprec);
3215 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3217 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3218 // maximum unsigned value is ~0. For signed comparison, if CST2
3219 // doesn't have the most significant bit set, handle it similarly. If
3220 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3221 wide_int valv = lhs.lower_bound ();
3222 wide_int minv = valv & cst2v, maxv;
3223 bool we_know_nothing = false;
3224 if (minv != valv)
3226 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3227 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3228 if (minv == valv)
3230 // If we can't determine anything on this bound, fall
3231 // through and conservatively solve for the other end point.
3232 we_know_nothing = true;
3235 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3236 if (we_know_nothing)
3237 r.set_varying (type);
3238 else
3239 r = int_range<1> (type, minv, maxv);
3241 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3243 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3244 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3245 // VAL2 where
3246 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3247 // as maximum.
3248 // For signed comparison, if CST2 doesn't have most significant bit
3249 // set, handle it similarly. If CST2 has MSB set, the maximum is
3250 // the same and minimum is INT_MIN.
3251 valv = lhs.upper_bound ();
3252 minv = valv & cst2v;
3253 if (minv == valv)
3254 maxv = valv;
3255 else
3257 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3258 if (maxv == valv)
3260 // If we couldn't determine anything on either bound, return
3261 // undefined.
3262 if (we_know_nothing)
3263 r.set_undefined ();
3264 return;
3266 maxv -= 1;
3268 maxv |= ~cst2v;
3269 minv = sgnbit;
3270 int_range<1> upper_bits (type, minv, maxv);
3271 r.intersect (upper_bits);
3274 bool
3275 operator_bitwise_and::op1_range (irange &r, tree type,
3276 const irange &lhs,
3277 const irange &op2,
3278 relation_trio) const
3280 if (lhs.undefined_p ())
3281 return false;
3282 if (types_compatible_p (type, boolean_type_node))
3283 return op_logical_and.op1_range (r, type, lhs, op2);
3285 r.set_undefined ();
3286 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
3288 int_range_max chunk (lhs.type (),
3289 lhs.lower_bound (i),
3290 lhs.upper_bound (i));
3291 int_range_max res;
3292 simple_op1_range_solver (res, type, chunk, op2);
3293 r.union_ (res);
3295 if (r.undefined_p ())
3296 set_nonzero_range_from_mask (r, type, lhs);
3298 // For 0 = op1 & MASK, op1 is ~MASK.
3299 if (lhs.zero_p () && op2.singleton_p ())
3301 wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
3302 int_range<2> tmp (type);
3303 tmp.set_nonzero_bits (nz);
3304 r.intersect (tmp);
3306 return true;
3309 bool
3310 operator_bitwise_and::op2_range (irange &r, tree type,
3311 const irange &lhs,
3312 const irange &op1,
3313 relation_trio) const
3315 return operator_bitwise_and::op1_range (r, type, lhs, op1);
3319 class operator_logical_or : public range_operator
3321 using range_operator::fold_range;
3322 using range_operator::op1_range;
3323 using range_operator::op2_range;
3324 public:
3325 virtual bool fold_range (irange &r, tree type,
3326 const irange &lh,
3327 const irange &rh,
3328 relation_trio rel = TRIO_VARYING) const;
3329 virtual bool op1_range (irange &r, tree type,
3330 const irange &lhs,
3331 const irange &op2,
3332 relation_trio rel = TRIO_VARYING) const;
3333 virtual bool op2_range (irange &r, tree type,
3334 const irange &lhs,
3335 const irange &op1,
3336 relation_trio rel = TRIO_VARYING) const;
3337 } op_logical_or;
3339 bool
3340 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3341 const irange &lh,
3342 const irange &rh,
3343 relation_trio) const
3345 if (empty_range_varying (r, type, lh, rh))
3346 return true;
3348 r = lh;
3349 r.union_ (rh);
3350 return true;
3353 bool
3354 operator_logical_or::op1_range (irange &r, tree type,
3355 const irange &lhs,
3356 const irange &op2 ATTRIBUTE_UNUSED,
3357 relation_trio) const
3359 switch (get_bool_state (r, lhs, type))
3361 case BRS_FALSE:
3362 // A false result means both sides of the OR must be false.
3363 r = range_false (type);
3364 break;
3365 default:
3366 // Any other result means only one side has to be true, the
3367 // other side can be anything. so we can't be sure of any result
3368 // here.
3369 r = range_true_and_false (type);
3370 break;
3372 return true;
3375 bool
3376 operator_logical_or::op2_range (irange &r, tree type,
3377 const irange &lhs,
3378 const irange &op1,
3379 relation_trio) const
3381 return operator_logical_or::op1_range (r, type, lhs, op1);
3385 class operator_bitwise_or : public range_operator
3387 using range_operator::op1_range;
3388 using range_operator::op2_range;
3389 public:
3390 virtual bool op1_range (irange &r, tree type,
3391 const irange &lhs,
3392 const irange &op2,
3393 relation_trio rel = TRIO_VARYING) const;
3394 virtual bool op2_range (irange &r, tree type,
3395 const irange &lhs,
3396 const irange &op1,
3397 relation_trio rel = TRIO_VARYING) const;
3398 virtual void wi_fold (irange &r, tree type,
3399 const wide_int &lh_lb,
3400 const wide_int &lh_ub,
3401 const wide_int &rh_lb,
3402 const wide_int &rh_ub) const;
3403 } op_bitwise_or;
3405 void
3406 operator_bitwise_or::wi_fold (irange &r, tree type,
3407 const wide_int &lh_lb,
3408 const wide_int &lh_ub,
3409 const wide_int &rh_lb,
3410 const wide_int &rh_ub) const
3412 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3413 return;
3415 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3416 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3417 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3418 maybe_nonzero_lh, mustbe_nonzero_lh);
3419 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3420 maybe_nonzero_rh, mustbe_nonzero_rh);
3421 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3422 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3423 signop sign = TYPE_SIGN (type);
3424 // If the input ranges contain only positive values we can
3425 // truncate the minimum of the result range to the maximum
3426 // of the input range minima.
3427 if (wi::ge_p (lh_lb, 0, sign)
3428 && wi::ge_p (rh_lb, 0, sign))
3430 new_lb = wi::max (new_lb, lh_lb, sign);
3431 new_lb = wi::max (new_lb, rh_lb, sign);
3433 // If either input range contains only negative values
3434 // we can truncate the minimum of the result range to the
3435 // respective minimum range.
3436 if (wi::lt_p (lh_ub, 0, sign))
3437 new_lb = wi::max (new_lb, lh_lb, sign);
3438 if (wi::lt_p (rh_ub, 0, sign))
3439 new_lb = wi::max (new_lb, rh_lb, sign);
3440 // If the limits got swapped around, return a conservative range.
3441 if (wi::gt_p (new_lb, new_ub, sign))
3443 // Make sure that nonzero|X is nonzero.
3444 if (wi::gt_p (lh_lb, 0, sign)
3445 || wi::gt_p (rh_lb, 0, sign)
3446 || wi::lt_p (lh_ub, 0, sign)
3447 || wi::lt_p (rh_ub, 0, sign))
3448 r.set_nonzero (type);
3449 else if (sign == SIGNED
3450 && wi_optimize_signed_bitwise_op (r, type,
3451 lh_lb, lh_ub,
3452 rh_lb, rh_ub))
3453 return;
3454 else
3455 r.set_varying (type);
3456 return;
3458 value_range_with_overflow (r, type, new_lb, new_ub);
3461 bool
3462 operator_bitwise_or::op1_range (irange &r, tree type,
3463 const irange &lhs,
3464 const irange &op2,
3465 relation_trio) const
3467 if (lhs.undefined_p ())
3468 return false;
3469 // If this is really a logical wi_fold, call that.
3470 if (types_compatible_p (type, boolean_type_node))
3471 return op_logical_or.op1_range (r, type, lhs, op2);
3473 if (lhs.zero_p ())
3475 tree zero = build_zero_cst (type);
3476 r = int_range<1> (zero, zero);
3477 return true;
3479 r.set_varying (type);
3480 return true;
3483 bool
3484 operator_bitwise_or::op2_range (irange &r, tree type,
3485 const irange &lhs,
3486 const irange &op1,
3487 relation_trio) const
3489 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3493 class operator_bitwise_xor : public range_operator
3495 using range_operator::op1_range;
3496 using range_operator::op2_range;
3497 public:
3498 virtual void wi_fold (irange &r, tree type,
3499 const wide_int &lh_lb,
3500 const wide_int &lh_ub,
3501 const wide_int &rh_lb,
3502 const wide_int &rh_ub) const;
3503 virtual bool op1_range (irange &r, tree type,
3504 const irange &lhs,
3505 const irange &op2,
3506 relation_trio rel = TRIO_VARYING) const;
3507 virtual bool op2_range (irange &r, tree type,
3508 const irange &lhs,
3509 const irange &op1,
3510 relation_trio rel = TRIO_VARYING) const;
3511 virtual bool op1_op2_relation_effect (irange &lhs_range,
3512 tree type,
3513 const irange &op1_range,
3514 const irange &op2_range,
3515 relation_kind rel) const;
3516 } op_bitwise_xor;
3518 void
3519 operator_bitwise_xor::wi_fold (irange &r, tree type,
3520 const wide_int &lh_lb,
3521 const wide_int &lh_ub,
3522 const wide_int &rh_lb,
3523 const wide_int &rh_ub) const
3525 signop sign = TYPE_SIGN (type);
3526 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3527 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3528 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3529 maybe_nonzero_lh, mustbe_nonzero_lh);
3530 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3531 maybe_nonzero_rh, mustbe_nonzero_rh);
3533 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3534 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3535 wide_int result_one_bits
3536 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3537 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3538 wide_int new_ub = ~result_zero_bits;
3539 wide_int new_lb = result_one_bits;
3541 // If the range has all positive or all negative values, the result
3542 // is better than VARYING.
3543 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
3544 value_range_with_overflow (r, type, new_lb, new_ub);
3545 else if (sign == SIGNED
3546 && wi_optimize_signed_bitwise_op (r, type,
3547 lh_lb, lh_ub,
3548 rh_lb, rh_ub))
3549 ; /* Do nothing. */
3550 else
3551 r.set_varying (type);
3553 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3554 if (wi::lt_p (lh_ub, rh_lb, sign)
3555 || wi::lt_p (rh_ub, lh_lb, sign)
3556 || wi::ne_p (result_one_bits, 0))
3558 int_range<2> tmp;
3559 tmp.set_nonzero (type);
3560 r.intersect (tmp);
3564 bool
3565 operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3566 tree type,
3567 const irange &,
3568 const irange &,
3569 relation_kind rel) const
3571 if (rel == VREL_VARYING)
3572 return false;
3574 int_range<2> rel_range;
3576 switch (rel)
3578 case VREL_EQ:
3579 rel_range.set_zero (type);
3580 break;
3581 case VREL_NE:
3582 rel_range.set_nonzero (type);
3583 break;
3584 default:
3585 return false;
3588 lhs_range.intersect (rel_range);
3589 return true;
3592 bool
3593 operator_bitwise_xor::op1_range (irange &r, tree type,
3594 const irange &lhs,
3595 const irange &op2,
3596 relation_trio) const
3598 if (lhs.undefined_p () || lhs.varying_p ())
3600 r = lhs;
3601 return true;
3603 if (types_compatible_p (type, boolean_type_node))
3605 switch (get_bool_state (r, lhs, type))
3607 case BRS_TRUE:
3608 if (op2.varying_p ())
3609 r.set_varying (type);
3610 else if (op2.zero_p ())
3611 r = range_true (type);
3612 // See get_bool_state for the rationale
3613 else if (op2.contains_p (build_zero_cst (op2.type ())))
3614 r = range_true_and_false (type);
3615 else
3616 r = range_false (type);
3617 break;
3618 case BRS_FALSE:
3619 r = op2;
3620 break;
3621 default:
3622 break;
3624 return true;
3626 r.set_varying (type);
3627 return true;
3630 bool
3631 operator_bitwise_xor::op2_range (irange &r, tree type,
3632 const irange &lhs,
3633 const irange &op1,
3634 relation_trio) const
3636 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3639 class operator_trunc_mod : public range_operator
3641 using range_operator::op1_range;
3642 using range_operator::op2_range;
3643 public:
3644 virtual void wi_fold (irange &r, tree type,
3645 const wide_int &lh_lb,
3646 const wide_int &lh_ub,
3647 const wide_int &rh_lb,
3648 const wide_int &rh_ub) const;
3649 virtual bool op1_range (irange &r, tree type,
3650 const irange &lhs,
3651 const irange &op2,
3652 relation_trio) const;
3653 virtual bool op2_range (irange &r, tree type,
3654 const irange &lhs,
3655 const irange &op1,
3656 relation_trio) const;
3657 } op_trunc_mod;
3659 void
3660 operator_trunc_mod::wi_fold (irange &r, tree type,
3661 const wide_int &lh_lb,
3662 const wide_int &lh_ub,
3663 const wide_int &rh_lb,
3664 const wide_int &rh_ub) const
3666 wide_int new_lb, new_ub, tmp;
3667 signop sign = TYPE_SIGN (type);
3668 unsigned prec = TYPE_PRECISION (type);
3670 // Mod 0 is undefined.
3671 if (wi_zero_p (type, rh_lb, rh_ub))
3673 r.set_undefined ();
3674 return;
3677 // Check for constant and try to fold.
3678 if (lh_lb == lh_ub && rh_lb == rh_ub)
3680 wi::overflow_type ov = wi::OVF_NONE;
3681 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3682 if (ov == wi::OVF_NONE)
3684 r = int_range<2> (type, tmp, tmp);
3685 return;
3689 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3690 new_ub = rh_ub - 1;
3691 if (sign == SIGNED)
3693 tmp = -1 - rh_lb;
3694 new_ub = wi::smax (new_ub, tmp);
3697 if (sign == UNSIGNED)
3698 new_lb = wi::zero (prec);
3699 else
3701 new_lb = -new_ub;
3702 tmp = lh_lb;
3703 if (wi::gts_p (tmp, 0))
3704 tmp = wi::zero (prec);
3705 new_lb = wi::smax (new_lb, tmp);
3707 tmp = lh_ub;
3708 if (sign == SIGNED && wi::neg_p (tmp))
3709 tmp = wi::zero (prec);
3710 new_ub = wi::min (new_ub, tmp, sign);
3712 value_range_with_overflow (r, type, new_lb, new_ub);
3715 bool
3716 operator_trunc_mod::op1_range (irange &r, tree type,
3717 const irange &lhs,
3718 const irange &,
3719 relation_trio) const
3721 if (lhs.undefined_p ())
3722 return false;
3723 // PR 91029.
3724 signop sign = TYPE_SIGN (type);
3725 unsigned prec = TYPE_PRECISION (type);
3726 // (a % b) >= x && x > 0 , then a >= x.
3727 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3729 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3730 return true;
3732 // (a % b) <= x && x < 0 , then a <= x.
3733 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3735 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3736 return true;
3738 return false;
3741 bool
3742 operator_trunc_mod::op2_range (irange &r, tree type,
3743 const irange &lhs,
3744 const irange &,
3745 relation_trio) const
3747 if (lhs.undefined_p ())
3748 return false;
3749 // PR 91029.
3750 signop sign = TYPE_SIGN (type);
3751 unsigned prec = TYPE_PRECISION (type);
3752 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3753 // or b > x for unsigned.
3754 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3756 if (sign == SIGNED)
3757 r = value_range (type, wi::neg (lhs.lower_bound ()),
3758 lhs.lower_bound (), VR_ANTI_RANGE);
3759 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3760 sign))
3761 r = value_range (type, lhs.lower_bound () + 1,
3762 wi::max_value (prec, sign));
3763 else
3764 return false;
3765 return true;
3767 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3768 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3770 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3771 r = value_range (type, lhs.upper_bound (),
3772 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3773 else
3774 return false;
3775 return true;
3777 return false;
3781 class operator_logical_not : public range_operator
3783 using range_operator::fold_range;
3784 using range_operator::op1_range;
3785 public:
3786 virtual bool fold_range (irange &r, tree type,
3787 const irange &lh,
3788 const irange &rh,
3789 relation_trio rel = TRIO_VARYING) const;
3790 virtual bool op1_range (irange &r, tree type,
3791 const irange &lhs,
3792 const irange &op2,
3793 relation_trio rel = TRIO_VARYING) const;
3794 } op_logical_not;
3796 // Folding a logical NOT, oddly enough, involves doing nothing on the
3797 // forward pass through. During the initial walk backwards, the
3798 // logical NOT reversed the desired outcome on the way back, so on the
3799 // way forward all we do is pass the range forward.
3801 // b_2 = x_1 < 20
3802 // b_3 = !b_2
3803 // if (b_3)
3804 // to determine the TRUE branch, walking backward
3805 // if (b_3) if ([1,1])
3806 // b_3 = !b_2 [1,1] = ![0,0]
3807 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3808 // which is the result we are looking for.. so.. pass it through.
3810 bool
3811 operator_logical_not::fold_range (irange &r, tree type,
3812 const irange &lh,
3813 const irange &rh ATTRIBUTE_UNUSED,
3814 relation_trio) const
3816 if (empty_range_varying (r, type, lh, rh))
3817 return true;
3819 r = lh;
3820 if (!lh.varying_p () && !lh.undefined_p ())
3821 r.invert ();
3823 return true;
3826 bool
3827 operator_logical_not::op1_range (irange &r,
3828 tree type,
3829 const irange &lhs,
3830 const irange &op2,
3831 relation_trio) const
3833 // Logical NOT is involutary...do it again.
3834 return fold_range (r, type, lhs, op2);
3838 class operator_bitwise_not : public range_operator
3840 using range_operator::fold_range;
3841 using range_operator::op1_range;
3842 public:
3843 virtual bool fold_range (irange &r, tree type,
3844 const irange &lh,
3845 const irange &rh,
3846 relation_trio rel = TRIO_VARYING) const;
3847 virtual bool op1_range (irange &r, tree type,
3848 const irange &lhs,
3849 const irange &op2,
3850 relation_trio rel = TRIO_VARYING) const;
3851 } op_bitwise_not;
3853 bool
3854 operator_bitwise_not::fold_range (irange &r, tree type,
3855 const irange &lh,
3856 const irange &rh,
3857 relation_trio) const
3859 if (empty_range_varying (r, type, lh, rh))
3860 return true;
3862 if (types_compatible_p (type, boolean_type_node))
3863 return op_logical_not.fold_range (r, type, lh, rh);
3865 // ~X is simply -1 - X.
3866 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
3867 wi::minus_one (TYPE_PRECISION (type)));
3868 return range_op_handler (MINUS_EXPR, type).fold_range (r, type, minusone, lh);
3871 bool
3872 operator_bitwise_not::op1_range (irange &r, tree type,
3873 const irange &lhs,
3874 const irange &op2,
3875 relation_trio) const
3877 if (lhs.undefined_p ())
3878 return false;
3879 if (types_compatible_p (type, boolean_type_node))
3880 return op_logical_not.op1_range (r, type, lhs, op2);
3882 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
3883 return fold_range (r, type, lhs, op2);
3887 class operator_cst : public range_operator
3889 using range_operator::fold_range;
3890 public:
3891 virtual bool fold_range (irange &r, tree type,
3892 const irange &op1,
3893 const irange &op2,
3894 relation_trio rel = TRIO_VARYING) const;
3895 } op_integer_cst;
3897 bool
3898 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3899 const irange &lh,
3900 const irange &rh ATTRIBUTE_UNUSED,
3901 relation_trio) const
3903 r = lh;
3904 return true;
3908 class operator_identity : public range_operator
3910 using range_operator::fold_range;
3911 using range_operator::op1_range;
3912 using range_operator::lhs_op1_relation;
3913 public:
3914 virtual bool fold_range (irange &r, tree type,
3915 const irange &op1,
3916 const irange &op2,
3917 relation_trio rel = TRIO_VARYING) const;
3918 virtual bool op1_range (irange &r, tree type,
3919 const irange &lhs,
3920 const irange &op2,
3921 relation_trio rel = TRIO_VARYING) const;
3922 virtual relation_kind lhs_op1_relation (const irange &lhs,
3923 const irange &op1,
3924 const irange &op2,
3925 relation_kind rel) const;
3928 // Determine if there is a relationship between LHS and OP1.
3930 relation_kind
3931 operator_identity::lhs_op1_relation (const irange &lhs,
3932 const irange &op1 ATTRIBUTE_UNUSED,
3933 const irange &op2 ATTRIBUTE_UNUSED,
3934 relation_kind) const
3936 if (lhs.undefined_p ())
3937 return VREL_VARYING;
3938 // Simply a copy, so they are equivalent.
3939 return VREL_EQ;
3942 bool
3943 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3944 const irange &lh,
3945 const irange &rh ATTRIBUTE_UNUSED,
3946 relation_trio) const
3948 r = lh;
3949 return true;
3952 bool
3953 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
3954 const irange &lhs,
3955 const irange &op2 ATTRIBUTE_UNUSED,
3956 relation_trio) const
3958 r = lhs;
3959 return true;
3963 class operator_unknown : public range_operator
3965 using range_operator::fold_range;
3966 public:
3967 virtual bool fold_range (irange &r, tree type,
3968 const irange &op1,
3969 const irange &op2,
3970 relation_trio rel = TRIO_VARYING) const;
3973 bool
3974 operator_unknown::fold_range (irange &r, tree type,
3975 const irange &lh ATTRIBUTE_UNUSED,
3976 const irange &rh ATTRIBUTE_UNUSED,
3977 relation_trio) const
3979 r.set_varying (type);
3980 return true;
3984 class operator_abs : public range_operator
3986 using range_operator::op1_range;
3987 public:
3988 virtual void wi_fold (irange &r, tree type,
3989 const wide_int &lh_lb,
3990 const wide_int &lh_ub,
3991 const wide_int &rh_lb,
3992 const wide_int &rh_ub) const;
3993 virtual bool op1_range (irange &r, tree type,
3994 const irange &lhs,
3995 const irange &op2,
3996 relation_trio) const;
3997 } op_abs;
3999 void
4000 operator_abs::wi_fold (irange &r, tree type,
4001 const wide_int &lh_lb, const wide_int &lh_ub,
4002 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4003 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4005 wide_int min, max;
4006 signop sign = TYPE_SIGN (type);
4007 unsigned prec = TYPE_PRECISION (type);
4009 // Pass through LH for the easy cases.
4010 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
4012 r = int_range<1> (type, lh_lb, lh_ub);
4013 return;
4016 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
4017 // a useful range.
4018 wide_int min_value = wi::min_value (prec, sign);
4019 wide_int max_value = wi::max_value (prec, sign);
4020 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
4022 r.set_varying (type);
4023 return;
4026 // ABS_EXPR may flip the range around, if the original range
4027 // included negative values.
4028 if (wi::eq_p (lh_lb, min_value))
4030 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
4031 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
4032 if (wi::eq_p (lh_ub, min_value))
4034 r = int_range<1> (type, min_value, min_value);
4035 return;
4037 min = max_value;
4039 else
4040 min = wi::abs (lh_lb);
4042 if (wi::eq_p (lh_ub, min_value))
4043 max = max_value;
4044 else
4045 max = wi::abs (lh_ub);
4047 // If the range contains zero then we know that the minimum value in the
4048 // range will be zero.
4049 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
4051 if (wi::gt_p (min, max, sign))
4052 max = min;
4053 min = wi::zero (prec);
4055 else
4057 // If the range was reversed, swap MIN and MAX.
4058 if (wi::gt_p (min, max, sign))
4059 std::swap (min, max);
4062 // If the new range has its limits swapped around (MIN > MAX), then
4063 // the operation caused one of them to wrap around. The only thing
4064 // we know is that the result is positive.
4065 if (wi::gt_p (min, max, sign))
4067 min = wi::zero (prec);
4068 max = max_value;
4070 r = int_range<1> (type, min, max);
4073 bool
4074 operator_abs::op1_range (irange &r, tree type,
4075 const irange &lhs,
4076 const irange &op2,
4077 relation_trio) const
4079 if (empty_range_varying (r, type, lhs, op2))
4080 return true;
4081 if (TYPE_UNSIGNED (type))
4083 r = lhs;
4084 return true;
4086 // Start with the positives because negatives are an impossible result.
4087 int_range_max positives = range_positives (type);
4088 positives.intersect (lhs);
4089 r = positives;
4090 // Then add the negative of each pair:
4091 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
4092 for (unsigned i = 0; i < positives.num_pairs (); ++i)
4093 r.union_ (int_range<1> (type,
4094 -positives.upper_bound (i),
4095 -positives.lower_bound (i)));
4096 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
4097 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
4098 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4099 wide_int lb = lhs.lower_bound ();
4100 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
4101 r.union_ (int_range<2> (type, lb, lb));
4102 return true;
4106 class operator_absu : public range_operator
4108 public:
4109 virtual void wi_fold (irange &r, tree type,
4110 const wide_int &lh_lb, const wide_int &lh_ub,
4111 const wide_int &rh_lb, const wide_int &rh_ub) const;
4112 } op_absu;
4114 void
4115 operator_absu::wi_fold (irange &r, tree type,
4116 const wide_int &lh_lb, const wide_int &lh_ub,
4117 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4118 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4120 wide_int new_lb, new_ub;
4122 // Pass through VR0 the easy cases.
4123 if (wi::ges_p (lh_lb, 0))
4125 new_lb = lh_lb;
4126 new_ub = lh_ub;
4128 else
4130 new_lb = wi::abs (lh_lb);
4131 new_ub = wi::abs (lh_ub);
4133 // If the range contains zero then we know that the minimum
4134 // value in the range will be zero.
4135 if (wi::ges_p (lh_ub, 0))
4137 if (wi::gtu_p (new_lb, new_ub))
4138 new_ub = new_lb;
4139 new_lb = wi::zero (TYPE_PRECISION (type));
4141 else
4142 std::swap (new_lb, new_ub);
4145 gcc_checking_assert (TYPE_UNSIGNED (type));
4146 r = int_range<1> (type, new_lb, new_ub);
4150 class operator_negate : public range_operator
4152 using range_operator::fold_range;
4153 using range_operator::op1_range;
4154 public:
4155 virtual bool fold_range (irange &r, tree type,
4156 const irange &op1,
4157 const irange &op2,
4158 relation_trio rel = TRIO_VARYING) const;
4159 virtual bool op1_range (irange &r, tree type,
4160 const irange &lhs,
4161 const irange &op2,
4162 relation_trio rel = TRIO_VARYING) const;
4163 } op_negate;
4165 bool
4166 operator_negate::fold_range (irange &r, tree type,
4167 const irange &lh,
4168 const irange &rh,
4169 relation_trio) const
4171 if (empty_range_varying (r, type, lh, rh))
4172 return true;
4173 // -X is simply 0 - X.
4174 return range_op_handler (MINUS_EXPR, type).fold_range (r, type,
4175 range_zero (type), lh);
4178 bool
4179 operator_negate::op1_range (irange &r, tree type,
4180 const irange &lhs,
4181 const irange &op2,
4182 relation_trio) const
4184 // NEGATE is involutory.
4185 return fold_range (r, type, lhs, op2);
4189 class operator_addr_expr : public range_operator
4191 using range_operator::fold_range;
4192 using range_operator::op1_range;
4193 public:
4194 virtual bool fold_range (irange &r, tree type,
4195 const irange &op1,
4196 const irange &op2,
4197 relation_trio rel = TRIO_VARYING) const;
4198 virtual bool op1_range (irange &r, tree type,
4199 const irange &lhs,
4200 const irange &op2,
4201 relation_trio rel = TRIO_VARYING) const;
4202 } op_addr;
4204 bool
4205 operator_addr_expr::fold_range (irange &r, tree type,
4206 const irange &lh,
4207 const irange &rh,
4208 relation_trio) const
4210 if (empty_range_varying (r, type, lh, rh))
4211 return true;
4213 // Return a non-null pointer of the LHS type (passed in op2).
4214 if (lh.zero_p ())
4215 r = range_zero (type);
4216 else if (!lh.contains_p (build_zero_cst (lh.type ())))
4217 r = range_nonzero (type);
4218 else
4219 r.set_varying (type);
4220 return true;
4223 bool
4224 operator_addr_expr::op1_range (irange &r, tree type,
4225 const irange &lhs,
4226 const irange &op2,
4227 relation_trio) const
4229 return operator_addr_expr::fold_range (r, type, lhs, op2);
4233 class pointer_plus_operator : public range_operator
4235 public:
4236 virtual void wi_fold (irange &r, tree type,
4237 const wide_int &lh_lb,
4238 const wide_int &lh_ub,
4239 const wide_int &rh_lb,
4240 const wide_int &rh_ub) const;
4241 virtual bool op2_range (irange &r, tree type,
4242 const irange &lhs,
4243 const irange &op1,
4244 relation_trio = TRIO_VARYING) const;
4245 } op_pointer_plus;
4247 void
4248 pointer_plus_operator::wi_fold (irange &r, tree type,
4249 const wide_int &lh_lb,
4250 const wide_int &lh_ub,
4251 const wide_int &rh_lb,
4252 const wide_int &rh_ub) const
4254 // Check for [0,0] + const, and simply return the const.
4255 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
4257 tree val = wide_int_to_tree (type, rh_lb);
4258 r.set (val, val);
4259 return;
4262 // For pointer types, we are really only interested in asserting
4263 // whether the expression evaluates to non-NULL.
4265 // With -fno-delete-null-pointer-checks we need to be more
4266 // conservative. As some object might reside at address 0,
4267 // then some offset could be added to it and the same offset
4268 // subtracted again and the result would be NULL.
4269 // E.g.
4270 // static int a[12]; where &a[0] is NULL and
4271 // ptr = &a[6];
4272 // ptr -= 6;
4273 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
4274 // where the first range doesn't include zero and the second one
4275 // doesn't either. As the second operand is sizetype (unsigned),
4276 // consider all ranges where the MSB could be set as possible
4277 // subtractions where the result might be NULL.
4278 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
4279 || !wi_includes_zero_p (type, rh_lb, rh_ub))
4280 && !TYPE_OVERFLOW_WRAPS (type)
4281 && (flag_delete_null_pointer_checks
4282 || !wi::sign_mask (rh_ub)))
4283 r = range_nonzero (type);
4284 else if (lh_lb == lh_ub && lh_lb == 0
4285 && rh_lb == rh_ub && rh_lb == 0)
4286 r = range_zero (type);
4287 else
4288 r.set_varying (type);
4291 bool
4292 pointer_plus_operator::op2_range (irange &r, tree type,
4293 const irange &lhs ATTRIBUTE_UNUSED,
4294 const irange &op1 ATTRIBUTE_UNUSED,
4295 relation_trio trio) const
4297 relation_kind rel = trio.lhs_op1 ();
4298 r.set_varying (type);
4300 // If the LHS and OP1 are equal, the op2 must be zero.
4301 if (rel == VREL_EQ)
4302 r.set_zero (type);
4303 // If the LHS and OP1 are not equal, the offset must be non-zero.
4304 else if (rel == VREL_NE)
4305 r.set_nonzero (type);
4306 else
4307 return false;
4308 return true;
4311 class pointer_min_max_operator : public range_operator
4313 public:
4314 virtual void wi_fold (irange & r, tree type,
4315 const wide_int &lh_lb, const wide_int &lh_ub,
4316 const wide_int &rh_lb, const wide_int &rh_ub) const;
4319 void
4320 pointer_min_max_operator::wi_fold (irange &r, tree type,
4321 const wide_int &lh_lb,
4322 const wide_int &lh_ub,
4323 const wide_int &rh_lb,
4324 const wide_int &rh_ub) const
4326 // For MIN/MAX expressions with pointers, we only care about
4327 // nullness. If both are non null, then the result is nonnull.
4328 // If both are null, then the result is null. Otherwise they
4329 // are varying.
4330 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4331 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4332 r = range_nonzero (type);
4333 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4334 r = range_zero (type);
4335 else
4336 r.set_varying (type);
4340 class pointer_and_operator : public range_operator
4342 public:
4343 virtual void wi_fold (irange &r, tree type,
4344 const wide_int &lh_lb, const wide_int &lh_ub,
4345 const wide_int &rh_lb, const wide_int &rh_ub) const;
4346 } op_pointer_and;
4348 void
4349 pointer_and_operator::wi_fold (irange &r, tree type,
4350 const wide_int &lh_lb,
4351 const wide_int &lh_ub,
4352 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4353 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4355 // For pointer types, we are really only interested in asserting
4356 // whether the expression evaluates to non-NULL.
4357 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
4358 r = range_zero (type);
4359 else
4360 r.set_varying (type);
4364 class pointer_or_operator : public range_operator
4366 using range_operator::op1_range;
4367 using range_operator::op2_range;
4368 public:
4369 virtual bool op1_range (irange &r, tree type,
4370 const irange &lhs,
4371 const irange &op2,
4372 relation_trio rel = TRIO_VARYING) const;
4373 virtual bool op2_range (irange &r, tree type,
4374 const irange &lhs,
4375 const irange &op1,
4376 relation_trio rel = TRIO_VARYING) const;
4377 virtual void wi_fold (irange &r, tree type,
4378 const wide_int &lh_lb, const wide_int &lh_ub,
4379 const wide_int &rh_lb, const wide_int &rh_ub) const;
4380 } op_pointer_or;
4382 bool
4383 pointer_or_operator::op1_range (irange &r, tree type,
4384 const irange &lhs,
4385 const irange &op2 ATTRIBUTE_UNUSED,
4386 relation_trio) const
4388 if (lhs.undefined_p ())
4389 return false;
4390 if (lhs.zero_p ())
4392 tree zero = build_zero_cst (type);
4393 r = int_range<1> (zero, zero);
4394 return true;
4396 r.set_varying (type);
4397 return true;
4400 bool
4401 pointer_or_operator::op2_range (irange &r, tree type,
4402 const irange &lhs,
4403 const irange &op1,
4404 relation_trio) const
4406 return pointer_or_operator::op1_range (r, type, lhs, op1);
4409 void
4410 pointer_or_operator::wi_fold (irange &r, tree type,
4411 const wide_int &lh_lb,
4412 const wide_int &lh_ub,
4413 const wide_int &rh_lb,
4414 const wide_int &rh_ub) const
4416 // For pointer types, we are really only interested in asserting
4417 // whether the expression evaluates to non-NULL.
4418 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
4419 && !wi_includes_zero_p (type, rh_lb, rh_ub))
4420 r = range_nonzero (type);
4421 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
4422 r = range_zero (type);
4423 else
4424 r.set_varying (type);
4427 // Return a pointer to the range_operator instance, if there is one
4428 // associated with tree_code CODE.
4430 range_operator *
4431 range_op_table::operator[] (enum tree_code code)
4433 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
4434 return m_range_tree[code];
4437 // Add OP to the handler table for CODE.
4439 void
4440 range_op_table::set (enum tree_code code, range_operator &op)
4442 gcc_checking_assert (m_range_tree[code] == NULL);
4443 m_range_tree[code] = &op;
4444 gcc_checking_assert (op.m_code == ERROR_MARK || op.m_code == code);
4445 op.m_code = code;
4448 // Shared operators that require separate instantiations because they
4449 // do not share a common tree code.
4450 static operator_cast op_nop, op_convert;
4451 static operator_identity op_ssa, op_paren, op_obj_type;
4452 static operator_unknown op_realpart, op_imagpart;
4453 static pointer_min_max_operator op_ptr_min, op_ptr_max;
4454 static operator_div op_trunc_div;
4455 static operator_div op_floor_div;
4456 static operator_div op_round_div;
4457 static operator_div op_ceil_div;
4459 // Instantiate a range op table for integral operations.
4461 class integral_table : public range_op_table
4463 public:
4464 integral_table ();
4465 } integral_tree_table;
4467 integral_table::integral_table ()
4469 set (EQ_EXPR, op_equal);
4470 set (NE_EXPR, op_not_equal);
4471 set (LT_EXPR, op_lt);
4472 set (LE_EXPR, op_le);
4473 set (GT_EXPR, op_gt);
4474 set (GE_EXPR, op_ge);
4475 set (PLUS_EXPR, op_plus);
4476 set (MINUS_EXPR, op_minus);
4477 set (MIN_EXPR, op_min);
4478 set (MAX_EXPR, op_max);
4479 set (MULT_EXPR, op_mult);
4480 set (TRUNC_DIV_EXPR, op_trunc_div);
4481 set (FLOOR_DIV_EXPR, op_floor_div);
4482 set (ROUND_DIV_EXPR, op_round_div);
4483 set (CEIL_DIV_EXPR, op_ceil_div);
4484 set (EXACT_DIV_EXPR, op_exact_div);
4485 set (LSHIFT_EXPR, op_lshift);
4486 set (RSHIFT_EXPR, op_rshift);
4487 set (NOP_EXPR, op_nop);
4488 set (CONVERT_EXPR, op_convert);
4489 set (TRUTH_AND_EXPR, op_logical_and);
4490 set (BIT_AND_EXPR, op_bitwise_and);
4491 set (TRUTH_OR_EXPR, op_logical_or);
4492 set (BIT_IOR_EXPR, op_bitwise_or);
4493 set (BIT_XOR_EXPR, op_bitwise_xor);
4494 set (TRUNC_MOD_EXPR, op_trunc_mod);
4495 set (TRUTH_NOT_EXPR, op_logical_not);
4496 set (BIT_NOT_EXPR, op_bitwise_not);
4497 set (INTEGER_CST, op_integer_cst);
4498 set (SSA_NAME, op_ssa);
4499 set (PAREN_EXPR, op_paren);
4500 set (OBJ_TYPE_REF, op_obj_type);
4501 set (IMAGPART_EXPR, op_imagpart);
4502 set (REALPART_EXPR, op_realpart);
4503 set (POINTER_DIFF_EXPR, op_pointer_diff);
4504 set (ABS_EXPR, op_abs);
4505 set (ABSU_EXPR, op_absu);
4506 set (NEGATE_EXPR, op_negate);
4507 set (ADDR_EXPR, op_addr);
4510 // Instantiate a range op table for pointer operations.
4512 class pointer_table : public range_op_table
4514 public:
4515 pointer_table ();
4516 } pointer_tree_table;
4518 pointer_table::pointer_table ()
4520 set (BIT_AND_EXPR, op_pointer_and);
4521 set (BIT_IOR_EXPR, op_pointer_or);
4522 set (MIN_EXPR, op_ptr_min);
4523 set (MAX_EXPR, op_ptr_max);
4524 set (POINTER_PLUS_EXPR, op_pointer_plus);
4526 set (EQ_EXPR, op_equal);
4527 set (NE_EXPR, op_not_equal);
4528 set (LT_EXPR, op_lt);
4529 set (LE_EXPR, op_le);
4530 set (GT_EXPR, op_gt);
4531 set (GE_EXPR, op_ge);
4532 set (SSA_NAME, op_ssa);
4533 set (INTEGER_CST, op_integer_cst);
4534 set (ADDR_EXPR, op_addr);
4535 set (NOP_EXPR, op_nop);
4536 set (CONVERT_EXPR, op_convert);
4538 set (BIT_NOT_EXPR, op_bitwise_not);
4539 set (BIT_XOR_EXPR, op_bitwise_xor);
4542 // The tables are hidden and accessed via a simple extern function.
4544 static inline range_operator *
4545 get_handler (enum tree_code code, tree type)
4547 // First check if there is a pointer specialization.
4548 if (POINTER_TYPE_P (type))
4549 return pointer_tree_table[code];
4550 if (INTEGRAL_TYPE_P (type))
4551 return integral_tree_table[code];
4552 return NULL;
4555 // Return the floating point operator for CODE or NULL if none available.
4557 static inline range_operator_float *
4558 get_float_handler (enum tree_code code, tree)
4560 return (*floating_tree_table)[code];
4563 void
4564 range_op_handler::set_op_handler (tree_code code, tree type)
4566 if (irange::supports_p (type))
4568 m_float = NULL;
4569 m_int = get_handler (code, type);
4570 m_valid = m_int != NULL;
4572 else if (frange::supports_p (type))
4574 m_int = NULL;
4575 m_float = get_float_handler (code, type);
4576 m_valid = m_float != NULL;
4578 else
4580 m_int = NULL;
4581 m_float = NULL;
4582 m_valid = false;
4586 range_op_handler::range_op_handler ()
4588 m_int = NULL;
4589 m_float = NULL;
4590 m_valid = false;
4593 range_op_handler::range_op_handler (tree_code code, tree type)
4595 set_op_handler (code, type);
4599 bool
4600 range_op_handler::fold_range (vrange &r, tree type,
4601 const vrange &lh,
4602 const vrange &rh,
4603 relation_trio rel) const
4605 gcc_checking_assert (m_valid);
4606 if (m_int)
4607 return m_int->fold_range (as_a <irange> (r), type,
4608 as_a <irange> (lh),
4609 as_a <irange> (rh), rel);
4611 if (is_a <irange> (r))
4613 if (is_a <irange> (rh))
4614 return m_float->fold_range (as_a <irange> (r), type,
4615 as_a <frange> (lh),
4616 as_a <irange> (rh), rel);
4617 else
4618 return m_float->fold_range (as_a <irange> (r), type,
4619 as_a <frange> (lh),
4620 as_a <frange> (rh), rel);
4622 return m_float->fold_range (as_a <frange> (r), type,
4623 as_a <frange> (lh),
4624 as_a <frange> (rh), rel);
4627 bool
4628 range_op_handler::op1_range (vrange &r, tree type,
4629 const vrange &lhs,
4630 const vrange &op2,
4631 relation_trio rel) const
4633 gcc_checking_assert (m_valid);
4635 if (lhs.undefined_p ())
4636 return false;
4637 if (m_int)
4638 return m_int->op1_range (as_a <irange> (r), type,
4639 as_a <irange> (lhs),
4640 as_a <irange> (op2), rel);
4642 if (is_a <irange> (lhs))
4643 return m_float->op1_range (as_a <frange> (r), type,
4644 as_a <irange> (lhs),
4645 as_a <frange> (op2), rel);
4646 return m_float->op1_range (as_a <frange> (r), type,
4647 as_a <frange> (lhs),
4648 as_a <frange> (op2), rel);
4651 bool
4652 range_op_handler::op2_range (vrange &r, tree type,
4653 const vrange &lhs,
4654 const vrange &op1,
4655 relation_trio rel) const
4657 gcc_checking_assert (m_valid);
4658 if (lhs.undefined_p ())
4659 return false;
4660 if (m_int)
4661 return m_int->op2_range (as_a <irange> (r), type,
4662 as_a <irange> (lhs),
4663 as_a <irange> (op1), rel);
4665 if (is_a <irange> (lhs))
4666 return m_float->op2_range (as_a <frange> (r), type,
4667 as_a <irange> (lhs),
4668 as_a <frange> (op1), rel);
4669 return m_float->op2_range (as_a <frange> (r), type,
4670 as_a <frange> (lhs),
4671 as_a <frange> (op1), rel);
4674 relation_kind
4675 range_op_handler::lhs_op1_relation (const vrange &lhs,
4676 const vrange &op1,
4677 const vrange &op2,
4678 relation_kind rel) const
4680 gcc_checking_assert (m_valid);
4681 if (m_int)
4682 return m_int->lhs_op1_relation (as_a <irange> (lhs),
4683 as_a <irange> (op1),
4684 as_a <irange> (op2), rel);
4686 if (is_a <irange> (lhs))
4687 return m_float->lhs_op1_relation (as_a <irange> (lhs),
4688 as_a <frange> (op1),
4689 as_a <frange> (op2), rel);
4690 return m_float->lhs_op1_relation (as_a <frange> (lhs),
4691 as_a <frange> (op1),
4692 as_a <frange> (op2), rel);
4695 relation_kind
4696 range_op_handler::lhs_op2_relation (const vrange &lhs,
4697 const vrange &op1,
4698 const vrange &op2,
4699 relation_kind rel) const
4701 gcc_checking_assert (m_valid);
4702 if (m_int)
4703 return m_int->lhs_op2_relation (as_a <irange> (lhs),
4704 as_a <irange> (op1),
4705 as_a <irange> (op2), rel);
4707 if (is_a <irange> (lhs))
4708 return m_float->lhs_op2_relation (as_a <irange> (lhs),
4709 as_a <frange> (op1),
4710 as_a <frange> (op2), rel);
4711 return m_float->lhs_op2_relation (as_a <frange> (lhs),
4712 as_a <frange> (op1),
4713 as_a <frange> (op2), rel);
4716 relation_kind
4717 range_op_handler::op1_op2_relation (const vrange &lhs) const
4719 gcc_checking_assert (m_valid);
4720 if (m_int)
4721 return m_int->op1_op2_relation (as_a <irange> (lhs));
4722 if (is_a <irange> (lhs))
4723 return m_float->op1_op2_relation (as_a <irange> (lhs));
4724 return m_float->op1_op2_relation (as_a <frange> (lhs));
4727 // Cast the range in R to TYPE.
4729 bool
4730 range_cast (vrange &r, tree type)
4732 Value_Range tmp (r);
4733 Value_Range varying (type);
4734 varying.set_varying (type);
4735 range_op_handler op (CONVERT_EXPR, type);
4736 // Call op_convert, if it fails, the result is varying.
4737 if (!op || !op.fold_range (r, type, tmp, varying))
4739 r.set_varying (type);
4740 return false;
4742 return true;
4745 #if CHECKING_P
4746 #include "selftest.h"
4748 namespace selftest
4750 #define INT(N) build_int_cst (integer_type_node, (N))
4751 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
4752 #define INT16(N) build_int_cst (short_integer_type_node, (N))
4753 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
4754 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
4755 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
4757 static void
4758 range_op_cast_tests ()
4760 int_range<1> r0, r1, r2, rold;
4761 r0.set_varying (integer_type_node);
4762 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
4764 // If a range is in any way outside of the range for the converted
4765 // to range, default to the range for the new type.
4766 r0.set_varying (short_integer_type_node);
4767 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
4768 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
4769 if (TYPE_PRECISION (TREE_TYPE (maxint))
4770 > TYPE_PRECISION (short_integer_type_node))
4772 r1 = int_range<1> (integer_zero_node, maxint);
4773 range_cast (r1, short_integer_type_node);
4774 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
4775 && r1.upper_bound() == wi::to_wide (maxshort));
4778 // (unsigned char)[-5,-1] => [251,255].
4779 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
4780 range_cast (r0, unsigned_char_type_node);
4781 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
4782 range_cast (r0, signed_char_type_node);
4783 ASSERT_TRUE (r0 == rold);
4785 // (signed char)[15, 150] => [-128,-106][15,127].
4786 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
4787 range_cast (r0, signed_char_type_node);
4788 r1 = int_range<1> (SCHAR (15), SCHAR (127));
4789 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
4790 r1.union_ (r2);
4791 ASSERT_TRUE (r1 == r0);
4792 range_cast (r0, unsigned_char_type_node);
4793 ASSERT_TRUE (r0 == rold);
4795 // (unsigned char)[-5, 5] => [0,5][251,255].
4796 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
4797 range_cast (r0, unsigned_char_type_node);
4798 r1 = int_range<1> (UCHAR (251), UCHAR (255));
4799 r2 = int_range<1> (UCHAR (0), UCHAR (5));
4800 r1.union_ (r2);
4801 ASSERT_TRUE (r0 == r1);
4802 range_cast (r0, signed_char_type_node);
4803 ASSERT_TRUE (r0 == rold);
4805 // (unsigned char)[-5,5] => [0,5][251,255].
4806 r0 = int_range<1> (INT (-5), INT (5));
4807 range_cast (r0, unsigned_char_type_node);
4808 r1 = int_range<1> (UCHAR (0), UCHAR (5));
4809 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
4810 ASSERT_TRUE (r0 == r1);
4812 // (unsigned char)[5U,1974U] => [0,255].
4813 r0 = int_range<1> (UINT (5), UINT (1974));
4814 range_cast (r0, unsigned_char_type_node);
4815 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
4816 range_cast (r0, integer_type_node);
4817 // Going to a wider range should not sign extend.
4818 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
4820 // (unsigned char)[-350,15] => [0,255].
4821 r0 = int_range<1> (INT (-350), INT (15));
4822 range_cast (r0, unsigned_char_type_node);
4823 ASSERT_TRUE (r0 == (int_range<1>
4824 (TYPE_MIN_VALUE (unsigned_char_type_node),
4825 TYPE_MAX_VALUE (unsigned_char_type_node))));
4827 // Casting [-120,20] from signed char to unsigned short.
4828 // => [0, 20][0xff88, 0xffff].
4829 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
4830 range_cast (r0, short_unsigned_type_node);
4831 r1 = int_range<1> (UINT16 (0), UINT16 (20));
4832 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
4833 r1.union_ (r2);
4834 ASSERT_TRUE (r0 == r1);
4835 // A truncating cast back to signed char will work because [-120, 20]
4836 // is representable in signed char.
4837 range_cast (r0, signed_char_type_node);
4838 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
4840 // unsigned char -> signed short
4841 // (signed short)[(unsigned char)25, (unsigned char)250]
4842 // => [(signed short)25, (signed short)250]
4843 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
4844 range_cast (r0, short_integer_type_node);
4845 r1 = int_range<1> (INT16 (25), INT16 (250));
4846 ASSERT_TRUE (r0 == r1);
4847 range_cast (r0, unsigned_char_type_node);
4848 ASSERT_TRUE (r0 == rold);
4850 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
4851 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
4852 TYPE_MAX_VALUE (long_long_integer_type_node));
4853 range_cast (r0, short_unsigned_type_node);
4854 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
4855 TYPE_MAX_VALUE (short_unsigned_type_node));
4856 ASSERT_TRUE (r0 == r1);
4858 // Casting NONZERO to a narrower type will wrap/overflow so
4859 // it's just the entire range for the narrower type.
4861 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4862 // is outside of the range of a smaller range, return the full
4863 // smaller range.
4864 if (TYPE_PRECISION (integer_type_node)
4865 > TYPE_PRECISION (short_integer_type_node))
4867 r0 = range_nonzero (integer_type_node);
4868 range_cast (r0, short_integer_type_node);
4869 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
4870 TYPE_MAX_VALUE (short_integer_type_node));
4871 ASSERT_TRUE (r0 == r1);
4874 // Casting NONZERO from a narrower signed to a wider signed.
4876 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4877 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4878 r0 = range_nonzero (short_integer_type_node);
4879 range_cast (r0, integer_type_node);
4880 r1 = int_range<1> (INT (-32768), INT (-1));
4881 r2 = int_range<1> (INT (1), INT (32767));
4882 r1.union_ (r2);
4883 ASSERT_TRUE (r0 == r1);
4886 static void
4887 range_op_lshift_tests ()
4889 // Test that 0x808.... & 0x8.... still contains 0x8....
4890 // for a large set of numbers.
4892 int_range_max res;
4893 tree big_type = long_long_unsigned_type_node;
4894 // big_num = 0x808,0000,0000,0000
4895 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
4896 build_int_cst (big_type, 0x808),
4897 build_int_cst (big_type, 48));
4898 op_bitwise_and.fold_range (res, big_type,
4899 int_range <1> (big_type),
4900 int_range <1> (big_num, big_num));
4901 // val = 0x8,0000,0000,0000
4902 tree val = fold_build2 (LSHIFT_EXPR, big_type,
4903 build_int_cst (big_type, 0x8),
4904 build_int_cst (big_type, 48));
4905 ASSERT_TRUE (res.contains_p (val));
4908 if (TYPE_PRECISION (unsigned_type_node) > 31)
4910 // unsigned VARYING = op1 << 1 should be VARYING.
4911 int_range<2> lhs (unsigned_type_node);
4912 int_range<2> shift (INT (1), INT (1));
4913 int_range_max op1;
4914 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4915 ASSERT_TRUE (op1.varying_p ());
4917 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4918 int_range<2> zero (UINT (0), UINT (0));
4919 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4920 ASSERT_TRUE (op1.num_pairs () == 2);
4921 // Remove the [0,0] range.
4922 op1.intersect (zero);
4923 ASSERT_TRUE (op1.num_pairs () == 1);
4924 // op1 << 1 should be [0x8000,0x8000] << 1,
4925 // which should result in [0,0].
4926 int_range_max result;
4927 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4928 ASSERT_TRUE (result == zero);
4930 // signed VARYING = op1 << 1 should be VARYING.
4931 if (TYPE_PRECISION (integer_type_node) > 31)
4933 // unsigned VARYING = op1 << 1 hould be VARYING.
4934 int_range<2> lhs (integer_type_node);
4935 int_range<2> shift (INT (1), INT (1));
4936 int_range_max op1;
4937 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4938 ASSERT_TRUE (op1.varying_p ());
4940 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4941 int_range<2> zero (INT (0), INT (0));
4942 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4943 ASSERT_TRUE (op1.num_pairs () == 2);
4944 // Remove the [0,0] range.
4945 op1.intersect (zero);
4946 ASSERT_TRUE (op1.num_pairs () == 1);
4947 // op1 << 1 shuould be [0x8000,0x8000] << 1,
4948 // which should result in [0,0].
4949 int_range_max result;
4950 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4951 ASSERT_TRUE (result == zero);
4955 static void
4956 range_op_rshift_tests ()
4958 // unsigned: [3, MAX] = OP1 >> 1
4960 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
4961 TYPE_MAX_VALUE (unsigned_type_node));
4962 int_range_max one (build_one_cst (unsigned_type_node),
4963 build_one_cst (unsigned_type_node));
4964 int_range_max op1;
4965 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4966 ASSERT_FALSE (op1.contains_p (UINT (3)));
4969 // signed: [3, MAX] = OP1 >> 1
4971 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
4972 int_range_max one (INT (1), INT (1));
4973 int_range_max op1;
4974 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4975 ASSERT_FALSE (op1.contains_p (INT (-2)));
4978 // This is impossible, so OP1 should be [].
4979 // signed: [MIN, MIN] = OP1 >> 1
4981 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
4982 TYPE_MIN_VALUE (integer_type_node));
4983 int_range_max one (INT (1), INT (1));
4984 int_range_max op1;
4985 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4986 ASSERT_TRUE (op1.undefined_p ());
4989 // signed: ~[-1] = OP1 >> 31
4990 if (TYPE_PRECISION (integer_type_node) > 31)
4992 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
4993 int_range_max shift (INT (31), INT (31));
4994 int_range_max op1;
4995 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4996 int_range_max negatives = range_negatives (integer_type_node);
4997 negatives.intersect (op1);
4998 ASSERT_TRUE (negatives.undefined_p ());
5002 static void
5003 range_op_bitwise_and_tests ()
5005 int_range_max res;
5006 tree min = vrp_val_min (integer_type_node);
5007 tree max = vrp_val_max (integer_type_node);
5008 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
5009 build_one_cst (integer_type_node));
5010 int_range_max i1 (tiny, max);
5011 int_range_max i2 (build_int_cst (integer_type_node, 255),
5012 build_int_cst (integer_type_node, 255));
5014 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
5015 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
5016 ASSERT_TRUE (res == int_range<1> (integer_type_node));
5018 // VARYING = OP1 & 255: OP1 is VARYING
5019 i1 = int_range<1> (integer_type_node);
5020 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
5021 ASSERT_TRUE (res == int_range<1> (integer_type_node));
5023 // For 0 = x & MASK, x is ~MASK.
5025 int_range<2> zero (integer_zero_node, integer_zero_node);
5026 int_range<2> mask = int_range<2> (INT (7), INT (7));
5027 op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
5028 wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
5029 ASSERT_TRUE (res.get_nonzero_bits () == inv);
5032 // (NONZERO | X) is nonzero.
5033 i1.set_nonzero (integer_type_node);
5034 i2.set_varying (integer_type_node);
5035 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
5036 ASSERT_TRUE (res.nonzero_p ());
5038 // (NEGATIVE | X) is nonzero.
5039 i1 = int_range<1> (INT (-5), INT (-3));
5040 i2.set_varying (integer_type_node);
5041 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
5042 ASSERT_FALSE (res.contains_p (INT (0)));
5045 static void
5046 range_relational_tests ()
5048 int_range<2> lhs (unsigned_char_type_node);
5049 int_range<2> op1 (UCHAR (8), UCHAR (10));
5050 int_range<2> op2 (UCHAR (20), UCHAR (20));
5052 // Never wrapping additions mean LHS > OP1.
5053 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5054 ASSERT_TRUE (code == VREL_GT);
5056 // Most wrapping additions mean nothing...
5057 op1 = int_range<2> (UCHAR (8), UCHAR (10));
5058 op2 = int_range<2> (UCHAR (0), UCHAR (255));
5059 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5060 ASSERT_TRUE (code == VREL_VARYING);
5062 // However, always wrapping additions mean LHS < OP1.
5063 op1 = int_range<2> (UCHAR (1), UCHAR (255));
5064 op2 = int_range<2> (UCHAR (255), UCHAR (255));
5065 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5066 ASSERT_TRUE (code == VREL_LT);
5069 void
5070 range_op_tests ()
5072 range_op_rshift_tests ();
5073 range_op_lshift_tests ();
5074 range_op_bitwise_and_tests ();
5075 range_op_cast_tests ();
5076 range_relational_tests ();
5078 extern void range_op_float_tests ();
5079 range_op_float_tests ();
5082 } // namespace selftest
5084 #endif // CHECKING_P