Daily bump.
[gcc.git] / gcc / range-op.cc
1 /* Code for range operators.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "insn-codes.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
36 #include "flags.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "cfganal.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "tree-cfg.h"
46 #include "wide-int.h"
47 #include "range-op.h"
48
49 // Return the upper limit for a type.
50
51 static inline wide_int
52 max_limit (const_tree type)
53 {
54 return wi::max_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
55 }
56
57 // Return the lower limit for a type.
58
59 static inline wide_int
60 min_limit (const_tree type)
61 {
62 return wi::min_value (TYPE_PRECISION (type) , TYPE_SIGN (type));
63 }
64
65 // If the range of either op1 or op2 is undefined, set the result to
66 // varying and return TRUE. If the caller truely cares about a result,
67 // they should pass in a varying if it has an undefined that it wants
68 // treated as a varying.
69
70 inline bool
71 empty_range_varying (irange &r, tree type,
72 const irange &op1, const irange & op2)
73 {
74 if (op1.undefined_p () || op2.undefined_p ())
75 {
76 r.set_varying (type);
77 return true;
78 }
79 else
80 return false;
81 }
82
83 // Return false if shifting by OP is undefined behavior. Otherwise, return
84 // true and the range it is to be shifted by. This allows trimming out of
85 // undefined ranges, leaving only valid ranges if there are any.
86
87 static inline bool
88 get_shift_range (irange &r, tree type, const irange &op)
89 {
90 if (op.undefined_p ())
91 return false;
92
93 // Build valid range and intersect it with the shift range.
94 r = value_range (build_int_cst_type (op.type (), 0),
95 build_int_cst_type (op.type (), TYPE_PRECISION (type) - 1));
96 r.intersect (op);
97
98 // If there are no valid ranges in the shift range, returned false.
99 if (r.undefined_p ())
100 return false;
101 return true;
102 }
103
104 // Return TRUE if 0 is within [WMIN, WMAX].
105
106 static inline bool
107 wi_includes_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
108 {
109 signop sign = TYPE_SIGN (type);
110 return wi::le_p (wmin, 0, sign) && wi::ge_p (wmax, 0, sign);
111 }
112
113 // Return TRUE if [WMIN, WMAX] is the singleton 0.
114
115 static inline bool
116 wi_zero_p (tree type, const wide_int &wmin, const wide_int &wmax)
117 {
118 unsigned prec = TYPE_PRECISION (type);
119 return wmin == wmax && wi::eq_p (wmin, wi::zero (prec));
120 }
121
122 // Default wide_int fold operation returns [MIN, MAX].
123
124 void
125 range_operator::wi_fold (irange &r, tree type,
126 const wide_int &lh_lb ATTRIBUTE_UNUSED,
127 const wide_int &lh_ub ATTRIBUTE_UNUSED,
128 const wide_int &rh_lb ATTRIBUTE_UNUSED,
129 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
130 {
131 gcc_checking_assert (irange::supports_type_p (type));
132 r.set_varying (type);
133 }
134
135 // The default for fold is to break all ranges into sub-ranges and
136 // invoke the wi_fold method on each sub-range pair.
137
138 bool
139 range_operator::fold_range (irange &r, tree type,
140 const irange &lh,
141 const irange &rh) const
142 {
143 gcc_checking_assert (irange::supports_type_p (type));
144 if (empty_range_varying (r, type, lh, rh))
145 return true;
146
147 unsigned num_lh = lh.num_pairs ();
148 unsigned num_rh = rh.num_pairs ();
149
150 // If both ranges are single pairs, fold directly into the result range.
151 if (num_lh == 1 && num_rh == 1)
152 {
153 wi_fold (r, type, lh.lower_bound (0), lh.upper_bound (0),
154 rh.lower_bound (0), rh.upper_bound (0));
155 return true;
156 }
157
158 int_range_max tmp;
159 r.set_undefined ();
160 for (unsigned x = 0; x < num_lh; ++x)
161 for (unsigned y = 0; y < num_rh; ++y)
162 {
163 wide_int lh_lb = lh.lower_bound (x);
164 wide_int lh_ub = lh.upper_bound (x);
165 wide_int rh_lb = rh.lower_bound (y);
166 wide_int rh_ub = rh.upper_bound (y);
167 wi_fold (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
168 r.union_ (tmp);
169 if (r.varying_p ())
170 return true;
171 }
172 return true;
173 }
174
175 // The default for op1_range is to return false.
176
177 bool
178 range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
179 tree type ATTRIBUTE_UNUSED,
180 const irange &lhs ATTRIBUTE_UNUSED,
181 const irange &op2 ATTRIBUTE_UNUSED) const
182 {
183 return false;
184 }
185
186 // The default for op2_range is to return false.
187
188 bool
189 range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
190 tree type ATTRIBUTE_UNUSED,
191 const irange &lhs ATTRIBUTE_UNUSED,
192 const irange &op1 ATTRIBUTE_UNUSED) const
193 {
194 return false;
195 }
196
197
198 // Create and return a range from a pair of wide-ints that are known
199 // to have overflowed (or underflowed).
200
201 static void
202 value_range_from_overflowed_bounds (irange &r, tree type,
203 const wide_int &wmin,
204 const wide_int &wmax)
205 {
206 const signop sgn = TYPE_SIGN (type);
207 const unsigned int prec = TYPE_PRECISION (type);
208
209 wide_int tmin = wide_int::from (wmin, prec, sgn);
210 wide_int tmax = wide_int::from (wmax, prec, sgn);
211
212 bool covers = false;
213 wide_int tem = tmin;
214 tmin = tmax + 1;
215 if (wi::cmp (tmin, tmax, sgn) < 0)
216 covers = true;
217 tmax = tem - 1;
218 if (wi::cmp (tmax, tem, sgn) > 0)
219 covers = true;
220
221 // If the anti-range would cover nothing, drop to varying.
222 // Likewise if the anti-range bounds are outside of the types
223 // values.
224 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
225 r.set_varying (type);
226 else
227 {
228 tree tree_min = wide_int_to_tree (type, tmin);
229 tree tree_max = wide_int_to_tree (type, tmax);
230 r.set (tree_min, tree_max, VR_ANTI_RANGE);
231 }
232 }
233
234 // Create and return a range from a pair of wide-ints. MIN_OVF and
235 // MAX_OVF describe any overflow that might have occurred while
236 // calculating WMIN and WMAX respectively.
237
238 static void
239 value_range_with_overflow (irange &r, tree type,
240 const wide_int &wmin, const wide_int &wmax,
241 wi::overflow_type min_ovf = wi::OVF_NONE,
242 wi::overflow_type max_ovf = wi::OVF_NONE)
243 {
244 const signop sgn = TYPE_SIGN (type);
245 const unsigned int prec = TYPE_PRECISION (type);
246 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
247
248 // For one bit precision if max != min, then the range covers all
249 // values.
250 if (prec == 1 && wi::ne_p (wmax, wmin))
251 {
252 r.set_varying (type);
253 return;
254 }
255
256 if (overflow_wraps)
257 {
258 // If overflow wraps, truncate the values and adjust the range,
259 // kind, and bounds appropriately.
260 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
261 {
262 wide_int tmin = wide_int::from (wmin, prec, sgn);
263 wide_int tmax = wide_int::from (wmax, prec, sgn);
264 // If the limits are swapped, we wrapped around and cover
265 // the entire range.
266 if (wi::gt_p (tmin, tmax, sgn))
267 r.set_varying (type);
268 else
269 // No overflow or both overflow or underflow. The range
270 // kind stays normal.
271 r.set (wide_int_to_tree (type, tmin),
272 wide_int_to_tree (type, tmax));
273 return;
274 }
275
276 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
277 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
278 value_range_from_overflowed_bounds (r, type, wmin, wmax);
279 else
280 // Other underflow and/or overflow, drop to VR_VARYING.
281 r.set_varying (type);
282 }
283 else
284 {
285 // If both bounds either underflowed or overflowed, then the result
286 // is undefined.
287 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
288 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
289 {
290 r.set_undefined ();
291 return;
292 }
293
294 // If overflow does not wrap, saturate to [MIN, MAX].
295 wide_int new_lb, new_ub;
296 if (min_ovf == wi::OVF_UNDERFLOW)
297 new_lb = wi::min_value (prec, sgn);
298 else if (min_ovf == wi::OVF_OVERFLOW)
299 new_lb = wi::max_value (prec, sgn);
300 else
301 new_lb = wmin;
302
303 if (max_ovf == wi::OVF_UNDERFLOW)
304 new_ub = wi::min_value (prec, sgn);
305 else if (max_ovf == wi::OVF_OVERFLOW)
306 new_ub = wi::max_value (prec, sgn);
307 else
308 new_ub = wmax;
309
310 r.set (wide_int_to_tree (type, new_lb),
311 wide_int_to_tree (type, new_ub));
312 }
313 }
314
315 // Create and return a range from a pair of wide-ints. Canonicalize
316 // the case where the bounds are swapped. In which case, we transform
317 // [10,5] into [MIN,5][10,MAX].
318
319 static inline void
320 create_possibly_reversed_range (irange &r, tree type,
321 const wide_int &new_lb, const wide_int &new_ub)
322 {
323 signop s = TYPE_SIGN (type);
324 // If the bounds are swapped, treat the result as if an overflow occured.
325 if (wi::gt_p (new_lb, new_ub, s))
326 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
327 else
328 // Otherwise it's just a normal range.
329 r.set (wide_int_to_tree (type, new_lb), wide_int_to_tree (type, new_ub));
330 }
331
332 // Return an irange instance that is a boolean TRUE.
333
334 static inline int_range<1>
335 range_true (tree type)
336 {
337 unsigned prec = TYPE_PRECISION (type);
338 return int_range<1> (type, wi::one (prec), wi::one (prec));
339 }
340
341 // Return an irange instance that is a boolean FALSE.
342
343 static inline int_range<1>
344 range_false (tree type)
345 {
346 unsigned prec = TYPE_PRECISION (type);
347 return int_range<1> (type, wi::zero (prec), wi::zero (prec));
348 }
349
350 // Return an irange that covers both true and false.
351
352 static inline int_range<1>
353 range_true_and_false (tree type)
354 {
355 unsigned prec = TYPE_PRECISION (type);
356 return int_range<1> (type, wi::zero (prec), wi::one (prec));
357 }
358
359 enum bool_range_state { BRS_FALSE, BRS_TRUE, BRS_EMPTY, BRS_FULL };
360
361 // Return the summary information about boolean range LHS. Return an
362 // "interesting" range in R. For EMPTY or FULL, return the equivalent
363 // range for TYPE, for BRS_TRUE and BRS false, return the negation of
364 // the bool range.
365
366 static bool_range_state
367 get_bool_state (irange &r, const irange &lhs, tree val_type)
368 {
369 // If there is no result, then this is unexecutable.
370 if (lhs.undefined_p ())
371 {
372 r.set_undefined ();
373 return BRS_EMPTY;
374 }
375
376 if (lhs.zero_p ())
377 return BRS_FALSE;
378
379 // For TRUE, we can't just test for [1,1] because Ada can have
380 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
381 if (lhs.contains_p (build_zero_cst (lhs.type ())))
382 {
383 r.set_varying (val_type);
384 return BRS_FULL;
385 }
386 return BRS_TRUE;
387 }
388
389
390 class operator_equal : public range_operator
391 {
392 public:
393 virtual bool fold_range (irange &r, tree type,
394 const irange &op1,
395 const irange &op2) const;
396 virtual bool op1_range (irange &r, tree type,
397 const irange &lhs,
398 const irange &val) const;
399 virtual bool op2_range (irange &r, tree type,
400 const irange &lhs,
401 const irange &val) const;
402 } op_equal;
403
404 bool
405 operator_equal::fold_range (irange &r, tree type,
406 const irange &op1,
407 const irange &op2) const
408 {
409 if (empty_range_varying (r, type, op1, op2))
410 return true;
411
412 // We can be sure the values are always equal or not if both ranges
413 // consist of a single value, and then compare them.
414 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
415 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
416 {
417 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
418 r = range_true (type);
419 else
420 r = range_false (type);
421 }
422 else
423 {
424 // If ranges do not intersect, we know the range is not equal,
425 // otherwise we don't know anything for sure.
426 int_range_max tmp = op1;
427 tmp.intersect (op2);
428 if (tmp.undefined_p ())
429 r = range_false (type);
430 else
431 r = range_true_and_false (type);
432 }
433 return true;
434 }
435
436 bool
437 operator_equal::op1_range (irange &r, tree type,
438 const irange &lhs,
439 const irange &op2) const
440 {
441 switch (get_bool_state (r, lhs, type))
442 {
443 case BRS_FALSE:
444 // If the result is false, the only time we know anything is
445 // if OP2 is a constant.
446 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
447 {
448 r = op2;
449 r.invert ();
450 }
451 else
452 r.set_varying (type);
453 break;
454
455 case BRS_TRUE:
456 // If it's true, the result is the same as OP2.
457 r = op2;
458 break;
459
460 default:
461 break;
462 }
463 return true;
464 }
465
466 bool
467 operator_equal::op2_range (irange &r, tree type,
468 const irange &lhs,
469 const irange &op1) const
470 {
471 return operator_equal::op1_range (r, type, lhs, op1);
472 }
473
474
475 class operator_not_equal : public range_operator
476 {
477 public:
478 virtual bool fold_range (irange &r, tree type,
479 const irange &op1,
480 const irange &op2) const;
481 virtual bool op1_range (irange &r, tree type,
482 const irange &lhs,
483 const irange &op2) const;
484 virtual bool op2_range (irange &r, tree type,
485 const irange &lhs,
486 const irange &op1) const;
487 } op_not_equal;
488
489 bool
490 operator_not_equal::fold_range (irange &r, tree type,
491 const irange &op1,
492 const irange &op2) const
493 {
494 if (empty_range_varying (r, type, op1, op2))
495 return true;
496
497 // We can be sure the values are always equal or not if both ranges
498 // consist of a single value, and then compare them.
499 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
500 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
501 {
502 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
503 r = range_true (type);
504 else
505 r = range_false (type);
506 }
507 else
508 {
509 // If ranges do not intersect, we know the range is not equal,
510 // otherwise we don't know anything for sure.
511 int_range_max tmp = op1;
512 tmp.intersect (op2);
513 if (tmp.undefined_p ())
514 r = range_true (type);
515 else
516 r = range_true_and_false (type);
517 }
518 return true;
519 }
520
521 bool
522 operator_not_equal::op1_range (irange &r, tree type,
523 const irange &lhs,
524 const irange &op2) const
525 {
526 switch (get_bool_state (r, lhs, type))
527 {
528 case BRS_TRUE:
529 // If the result is true, the only time we know anything is if
530 // OP2 is a constant.
531 if (wi::eq_p (op2.lower_bound(), op2.upper_bound()))
532 {
533 r = op2;
534 r.invert ();
535 }
536 else
537 r.set_varying (type);
538 break;
539
540 case BRS_FALSE:
541 // If its true, the result is the same as OP2.
542 r = op2;
543 break;
544
545 default:
546 break;
547 }
548 return true;
549 }
550
551
552 bool
553 operator_not_equal::op2_range (irange &r, tree type,
554 const irange &lhs,
555 const irange &op1) const
556 {
557 return operator_not_equal::op1_range (r, type, lhs, op1);
558 }
559
560 // (X < VAL) produces the range of [MIN, VAL - 1].
561
562 static void
563 build_lt (irange &r, tree type, const wide_int &val)
564 {
565 wi::overflow_type ov;
566 wide_int lim = wi::sub (val, 1, TYPE_SIGN (type), &ov);
567
568 // If val - 1 underflows, check if X < MIN, which is an empty range.
569 if (ov)
570 r.set_undefined ();
571 else
572 r = int_range<1> (type, min_limit (type), lim);
573 }
574
575 // (X <= VAL) produces the range of [MIN, VAL].
576
577 static void
578 build_le (irange &r, tree type, const wide_int &val)
579 {
580 r = int_range<1> (type, min_limit (type), val);
581 }
582
583 // (X > VAL) produces the range of [VAL + 1, MAX].
584
585 static void
586 build_gt (irange &r, tree type, const wide_int &val)
587 {
588 wi::overflow_type ov;
589 wide_int lim = wi::add (val, 1, TYPE_SIGN (type), &ov);
590 // If val + 1 overflows, check is for X > MAX, which is an empty range.
591 if (ov)
592 r.set_undefined ();
593 else
594 r = int_range<1> (type, lim, max_limit (type));
595 }
596
597 // (X >= val) produces the range of [VAL, MAX].
598
599 static void
600 build_ge (irange &r, tree type, const wide_int &val)
601 {
602 r = int_range<1> (type, val, max_limit (type));
603 }
604
605
606 class operator_lt : public range_operator
607 {
608 public:
609 virtual bool fold_range (irange &r, tree type,
610 const irange &op1,
611 const irange &op2) const;
612 virtual bool op1_range (irange &r, tree type,
613 const irange &lhs,
614 const irange &op2) const;
615 virtual bool op2_range (irange &r, tree type,
616 const irange &lhs,
617 const irange &op1) const;
618 } op_lt;
619
620 bool
621 operator_lt::fold_range (irange &r, tree type,
622 const irange &op1,
623 const irange &op2) const
624 {
625 if (empty_range_varying (r, type, op1, op2))
626 return true;
627
628 signop sign = TYPE_SIGN (op1.type ());
629 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
630
631 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
632 r = range_true (type);
633 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
634 r = range_false (type);
635 else
636 r = range_true_and_false (type);
637 return true;
638 }
639
640 bool
641 operator_lt::op1_range (irange &r, tree type,
642 const irange &lhs,
643 const irange &op2) const
644 {
645 switch (get_bool_state (r, lhs, type))
646 {
647 case BRS_TRUE:
648 build_lt (r, type, op2.upper_bound ());
649 break;
650
651 case BRS_FALSE:
652 build_ge (r, type, op2.lower_bound ());
653 break;
654
655 default:
656 break;
657 }
658 return true;
659 }
660
661 bool
662 operator_lt::op2_range (irange &r, tree type,
663 const irange &lhs,
664 const irange &op1) const
665 {
666 switch (get_bool_state (r, lhs, type))
667 {
668 case BRS_FALSE:
669 build_le (r, type, op1.upper_bound ());
670 break;
671
672 case BRS_TRUE:
673 build_gt (r, type, op1.lower_bound ());
674 break;
675
676 default:
677 break;
678 }
679 return true;
680 }
681
682
683 class operator_le : public range_operator
684 {
685 public:
686 virtual bool fold_range (irange &r, tree type,
687 const irange &op1,
688 const irange &op2) const;
689 virtual bool op1_range (irange &r, tree type,
690 const irange &lhs,
691 const irange &op2) const;
692 virtual bool op2_range (irange &r, tree type,
693 const irange &lhs,
694 const irange &op1) const;
695 } op_le;
696
697 bool
698 operator_le::fold_range (irange &r, tree type,
699 const irange &op1,
700 const irange &op2) const
701 {
702 if (empty_range_varying (r, type, op1, op2))
703 return true;
704
705 signop sign = TYPE_SIGN (op1.type ());
706 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
707
708 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
709 r = range_true (type);
710 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
711 r = range_false (type);
712 else
713 r = range_true_and_false (type);
714 return true;
715 }
716
717 bool
718 operator_le::op1_range (irange &r, tree type,
719 const irange &lhs,
720 const irange &op2) const
721 {
722 switch (get_bool_state (r, lhs, type))
723 {
724 case BRS_TRUE:
725 build_le (r, type, op2.upper_bound ());
726 break;
727
728 case BRS_FALSE:
729 build_gt (r, type, op2.lower_bound ());
730 break;
731
732 default:
733 break;
734 }
735 return true;
736 }
737
738 bool
739 operator_le::op2_range (irange &r, tree type,
740 const irange &lhs,
741 const irange &op1) const
742 {
743 switch (get_bool_state (r, lhs, type))
744 {
745 case BRS_FALSE:
746 build_lt (r, type, op1.upper_bound ());
747 break;
748
749 case BRS_TRUE:
750 build_ge (r, type, op1.lower_bound ());
751 break;
752
753 default:
754 break;
755 }
756 return true;
757 }
758
759
760 class operator_gt : public range_operator
761 {
762 public:
763 virtual bool fold_range (irange &r, tree type,
764 const irange &op1,
765 const irange &op2) const;
766 virtual bool op1_range (irange &r, tree type,
767 const irange &lhs,
768 const irange &op2) const;
769 virtual bool op2_range (irange &r, tree type,
770 const irange &lhs,
771 const irange &op1) const;
772 } op_gt;
773
774 bool
775 operator_gt::fold_range (irange &r, tree type,
776 const irange &op1, const irange &op2) const
777 {
778 if (empty_range_varying (r, type, op1, op2))
779 return true;
780
781 signop sign = TYPE_SIGN (op1.type ());
782 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
783
784 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
785 r = range_true (type);
786 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
787 r = range_false (type);
788 else
789 r = range_true_and_false (type);
790 return true;
791 }
792
793 bool
794 operator_gt::op1_range (irange &r, tree type,
795 const irange &lhs, const irange &op2) const
796 {
797 switch (get_bool_state (r, lhs, type))
798 {
799 case BRS_TRUE:
800 build_gt (r, type, op2.lower_bound ());
801 break;
802
803 case BRS_FALSE:
804 build_le (r, type, op2.upper_bound ());
805 break;
806
807 default:
808 break;
809 }
810 return true;
811 }
812
813 bool
814 operator_gt::op2_range (irange &r, tree type,
815 const irange &lhs,
816 const irange &op1) const
817 {
818 switch (get_bool_state (r, lhs, type))
819 {
820 case BRS_FALSE:
821 build_ge (r, type, op1.lower_bound ());
822 break;
823
824 case BRS_TRUE:
825 build_lt (r, type, op1.upper_bound ());
826 break;
827
828 default:
829 break;
830 }
831 return true;
832 }
833
834
835 class operator_ge : public range_operator
836 {
837 public:
838 virtual bool fold_range (irange &r, tree type,
839 const irange &op1,
840 const irange &op2) const;
841 virtual bool op1_range (irange &r, tree type,
842 const irange &lhs,
843 const irange &op2) const;
844 virtual bool op2_range (irange &r, tree type,
845 const irange &lhs,
846 const irange &op1) const;
847 } op_ge;
848
849 bool
850 operator_ge::fold_range (irange &r, tree type,
851 const irange &op1,
852 const irange &op2) const
853 {
854 if (empty_range_varying (r, type, op1, op2))
855 return true;
856
857 signop sign = TYPE_SIGN (op1.type ());
858 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
859
860 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
861 r = range_true (type);
862 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
863 r = range_false (type);
864 else
865 r = range_true_and_false (type);
866 return true;
867 }
868
869 bool
870 operator_ge::op1_range (irange &r, tree type,
871 const irange &lhs,
872 const irange &op2) const
873 {
874 switch (get_bool_state (r, lhs, type))
875 {
876 case BRS_TRUE:
877 build_ge (r, type, op2.lower_bound ());
878 break;
879
880 case BRS_FALSE:
881 build_lt (r, type, op2.upper_bound ());
882 break;
883
884 default:
885 break;
886 }
887 return true;
888 }
889
890 bool
891 operator_ge::op2_range (irange &r, tree type,
892 const irange &lhs,
893 const irange &op1) const
894 {
895 switch (get_bool_state (r, lhs, type))
896 {
897 case BRS_FALSE:
898 build_gt (r, type, op1.lower_bound ());
899 break;
900
901 case BRS_TRUE:
902 build_le (r, type, op1.upper_bound ());
903 break;
904
905 default:
906 break;
907 }
908 return true;
909 }
910
911
912 class operator_plus : public range_operator
913 {
914 public:
915 virtual bool op1_range (irange &r, tree type,
916 const irange &lhs,
917 const irange &op2) const;
918 virtual bool op2_range (irange &r, tree type,
919 const irange &lhs,
920 const irange &op1) const;
921 virtual void wi_fold (irange &r, tree type,
922 const wide_int &lh_lb,
923 const wide_int &lh_ub,
924 const wide_int &rh_lb,
925 const wide_int &rh_ub) const;
926 } op_plus;
927
928 void
929 operator_plus::wi_fold (irange &r, tree type,
930 const wide_int &lh_lb, const wide_int &lh_ub,
931 const wide_int &rh_lb, const wide_int &rh_ub) const
932 {
933 wi::overflow_type ov_lb, ov_ub;
934 signop s = TYPE_SIGN (type);
935 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
936 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
937 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
938 }
939
940 bool
941 operator_plus::op1_range (irange &r, tree type,
942 const irange &lhs,
943 const irange &op2) const
944 {
945 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op2);
946 }
947
948 bool
949 operator_plus::op2_range (irange &r, tree type,
950 const irange &lhs,
951 const irange &op1) const
952 {
953 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, lhs, op1);
954 }
955
956
957 class operator_minus : public range_operator
958 {
959 public:
960 virtual bool op1_range (irange &r, tree type,
961 const irange &lhs,
962 const irange &op2) const;
963 virtual bool op2_range (irange &r, tree type,
964 const irange &lhs,
965 const irange &op1) const;
966 virtual void wi_fold (irange &r, tree type,
967 const wide_int &lh_lb,
968 const wide_int &lh_ub,
969 const wide_int &rh_lb,
970 const wide_int &rh_ub) const;
971 } op_minus;
972
973 void
974 operator_minus::wi_fold (irange &r, tree type,
975 const wide_int &lh_lb, const wide_int &lh_ub,
976 const wide_int &rh_lb, const wide_int &rh_ub) const
977 {
978 wi::overflow_type ov_lb, ov_ub;
979 signop s = TYPE_SIGN (type);
980 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
981 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
982 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
983 }
984
985 bool
986 operator_minus::op1_range (irange &r, tree type,
987 const irange &lhs,
988 const irange &op2) const
989 {
990 return range_op_handler (PLUS_EXPR, type)->fold_range (r, type, lhs, op2);
991 }
992
993 bool
994 operator_minus::op2_range (irange &r, tree type,
995 const irange &lhs,
996 const irange &op1) const
997 {
998 return fold_range (r, type, op1, lhs);
999 }
1000
1001
1002 class operator_min : public range_operator
1003 {
1004 public:
1005 virtual void wi_fold (irange &r, tree type,
1006 const wide_int &lh_lb,
1007 const wide_int &lh_ub,
1008 const wide_int &rh_lb,
1009 const wide_int &rh_ub) const;
1010 } op_min;
1011
1012 void
1013 operator_min::wi_fold (irange &r, tree type,
1014 const wide_int &lh_lb, const wide_int &lh_ub,
1015 const wide_int &rh_lb, const wide_int &rh_ub) const
1016 {
1017 signop s = TYPE_SIGN (type);
1018 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1019 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
1020 value_range_with_overflow (r, type, new_lb, new_ub);
1021 }
1022
1023
1024 class operator_max : public range_operator
1025 {
1026 public:
1027 virtual void wi_fold (irange &r, tree type,
1028 const wide_int &lh_lb,
1029 const wide_int &lh_ub,
1030 const wide_int &rh_lb,
1031 const wide_int &rh_ub) const;
1032 } op_max;
1033
1034 void
1035 operator_max::wi_fold (irange &r, tree type,
1036 const wide_int &lh_lb, const wide_int &lh_ub,
1037 const wide_int &rh_lb, const wide_int &rh_ub) const
1038 {
1039 signop s = TYPE_SIGN (type);
1040 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1041 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
1042 value_range_with_overflow (r, type, new_lb, new_ub);
1043 }
1044
1045
1046 class cross_product_operator : public range_operator
1047 {
1048 public:
1049 // Perform an operation between two wide-ints and place the result
1050 // in R. Return true if the operation overflowed.
1051 virtual bool wi_op_overflows (wide_int &r,
1052 tree type,
1053 const wide_int &,
1054 const wide_int &) const = 0;
1055
1056 // Calculate the cross product of two sets of sub-ranges and return it.
1057 void wi_cross_product (irange &r, tree type,
1058 const wide_int &lh_lb,
1059 const wide_int &lh_ub,
1060 const wide_int &rh_lb,
1061 const wide_int &rh_ub) const;
1062 };
1063
1064 // Calculate the cross product of two sets of ranges and return it.
1065 //
1066 // Multiplications, divisions and shifts are a bit tricky to handle,
1067 // depending on the mix of signs we have in the two ranges, we need to
1068 // operate on different values to get the minimum and maximum values
1069 // for the new range. One approach is to figure out all the
1070 // variations of range combinations and do the operations.
1071 //
1072 // However, this involves several calls to compare_values and it is
1073 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1074 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1075 // figure the smallest and largest values to form the new range.
1076
1077 void
1078 cross_product_operator::wi_cross_product (irange &r, tree type,
1079 const wide_int &lh_lb,
1080 const wide_int &lh_ub,
1081 const wide_int &rh_lb,
1082 const wide_int &rh_ub) const
1083 {
1084 wide_int cp1, cp2, cp3, cp4;
1085 // Default to varying.
1086 r.set_varying (type);
1087
1088 // Compute the 4 cross operations, bailing if we get an overflow we
1089 // can't handle.
1090 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
1091 return;
1092 if (wi::eq_p (lh_lb, lh_ub))
1093 cp3 = cp1;
1094 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
1095 return;
1096 if (wi::eq_p (rh_lb, rh_ub))
1097 cp2 = cp1;
1098 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
1099 return;
1100 if (wi::eq_p (lh_lb, lh_ub))
1101 cp4 = cp2;
1102 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
1103 return;
1104
1105 // Order pairs.
1106 signop sign = TYPE_SIGN (type);
1107 if (wi::gt_p (cp1, cp2, sign))
1108 std::swap (cp1, cp2);
1109 if (wi::gt_p (cp3, cp4, sign))
1110 std::swap (cp3, cp4);
1111
1112 // Choose min and max from the ordered pairs.
1113 wide_int res_lb = wi::min (cp1, cp3, sign);
1114 wide_int res_ub = wi::max (cp2, cp4, sign);
1115 value_range_with_overflow (r, type, res_lb, res_ub);
1116 }
1117
1118
1119 class operator_mult : public cross_product_operator
1120 {
1121 public:
1122 virtual void wi_fold (irange &r, tree type,
1123 const wide_int &lh_lb,
1124 const wide_int &lh_ub,
1125 const wide_int &rh_lb,
1126 const wide_int &rh_ub) const;
1127 virtual bool wi_op_overflows (wide_int &res, tree type,
1128 const wide_int &w0, const wide_int &w1) const;
1129 virtual bool op1_range (irange &r, tree type,
1130 const irange &lhs,
1131 const irange &op2) const;
1132 virtual bool op2_range (irange &r, tree type,
1133 const irange &lhs,
1134 const irange &op1) const;
1135 } op_mult;
1136
1137 bool
1138 operator_mult::op1_range (irange &r, tree type,
1139 const irange &lhs, const irange &op2) const
1140 {
1141 tree offset;
1142
1143 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
1144 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
1145 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
1146 if (TYPE_OVERFLOW_WRAPS (type))
1147 return false;
1148
1149 if (op2.singleton_p (&offset) && !integer_zerop (offset))
1150 return range_op_handler (TRUNC_DIV_EXPR, type)->fold_range (r, type,
1151 lhs, op2);
1152 return false;
1153 }
1154
1155 bool
1156 operator_mult::op2_range (irange &r, tree type,
1157 const irange &lhs, const irange &op1) const
1158 {
1159 return operator_mult::op1_range (r, type, lhs, op1);
1160 }
1161
1162 bool
1163 operator_mult::wi_op_overflows (wide_int &res, tree type,
1164 const wide_int &w0, const wide_int &w1) const
1165 {
1166 wi::overflow_type overflow = wi::OVF_NONE;
1167 signop sign = TYPE_SIGN (type);
1168 res = wi::mul (w0, w1, sign, &overflow);
1169 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1170 {
1171 // For multiplication, the sign of the overflow is given
1172 // by the comparison of the signs of the operands.
1173 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
1174 res = wi::max_value (w0.get_precision (), sign);
1175 else
1176 res = wi::min_value (w0.get_precision (), sign);
1177 return false;
1178 }
1179 return overflow;
1180 }
1181
1182 void
1183 operator_mult::wi_fold (irange &r, tree type,
1184 const wide_int &lh_lb, const wide_int &lh_ub,
1185 const wide_int &rh_lb, const wide_int &rh_ub) const
1186 {
1187 if (TYPE_OVERFLOW_UNDEFINED (type))
1188 {
1189 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1190 return;
1191 }
1192
1193 // Multiply the ranges when overflow wraps. This is basically fancy
1194 // code so we don't drop to varying with an unsigned
1195 // [-3,-1]*[-3,-1].
1196 //
1197 // This test requires 2*prec bits if both operands are signed and
1198 // 2*prec + 2 bits if either is not. Therefore, extend the values
1199 // using the sign of the result to PREC2. From here on out,
1200 // everthing is just signed math no matter what the input types
1201 // were.
1202
1203 signop sign = TYPE_SIGN (type);
1204 unsigned prec = TYPE_PRECISION (type);
1205 widest2_int min0 = widest2_int::from (lh_lb, sign);
1206 widest2_int max0 = widest2_int::from (lh_ub, sign);
1207 widest2_int min1 = widest2_int::from (rh_lb, sign);
1208 widest2_int max1 = widest2_int::from (rh_ub, sign);
1209 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
1210 widest2_int size = sizem1 + 1;
1211
1212 // Canonicalize the intervals.
1213 if (sign == UNSIGNED)
1214 {
1215 if (wi::ltu_p (size, min0 + max0))
1216 {
1217 min0 -= size;
1218 max0 -= size;
1219 }
1220 if (wi::ltu_p (size, min1 + max1))
1221 {
1222 min1 -= size;
1223 max1 -= size;
1224 }
1225 }
1226
1227 // Sort the 4 products so that min is in prod0 and max is in
1228 // prod3.
1229 widest2_int prod0 = min0 * min1;
1230 widest2_int prod1 = min0 * max1;
1231 widest2_int prod2 = max0 * min1;
1232 widest2_int prod3 = max0 * max1;
1233
1234 // min0min1 > max0max1
1235 if (prod0 > prod3)
1236 std::swap (prod0, prod3);
1237
1238 // min0max1 > max0min1
1239 if (prod1 > prod2)
1240 std::swap (prod1, prod2);
1241
1242 if (prod0 > prod1)
1243 std::swap (prod0, prod1);
1244
1245 if (prod2 > prod3)
1246 std::swap (prod2, prod3);
1247
1248 // diff = max - min
1249 prod2 = prod3 - prod0;
1250 if (wi::geu_p (prod2, sizem1))
1251 // The range covers all values.
1252 r.set_varying (type);
1253 else
1254 {
1255 wide_int new_lb = wide_int::from (prod0, prec, sign);
1256 wide_int new_ub = wide_int::from (prod3, prec, sign);
1257 create_possibly_reversed_range (r, type, new_lb, new_ub);
1258 }
1259 }
1260
1261
1262 class operator_div : public cross_product_operator
1263 {
1264 public:
1265 operator_div (enum tree_code c) { code = c; }
1266 virtual void wi_fold (irange &r, tree type,
1267 const wide_int &lh_lb,
1268 const wide_int &lh_ub,
1269 const wide_int &rh_lb,
1270 const wide_int &rh_ub) const;
1271 virtual bool wi_op_overflows (wide_int &res, tree type,
1272 const wide_int &, const wide_int &) const;
1273 private:
1274 enum tree_code code;
1275 };
1276
1277 bool
1278 operator_div::wi_op_overflows (wide_int &res, tree type,
1279 const wide_int &w0, const wide_int &w1) const
1280 {
1281 if (w1 == 0)
1282 return true;
1283
1284 wi::overflow_type overflow = wi::OVF_NONE;
1285 signop sign = TYPE_SIGN (type);
1286
1287 switch (code)
1288 {
1289 case EXACT_DIV_EXPR:
1290 // EXACT_DIV_EXPR is implemented as TRUNC_DIV_EXPR in
1291 // operator_exact_divide. No need to handle it here.
1292 gcc_unreachable ();
1293 break;
1294 case TRUNC_DIV_EXPR:
1295 res = wi::div_trunc (w0, w1, sign, &overflow);
1296 break;
1297 case FLOOR_DIV_EXPR:
1298 res = wi::div_floor (w0, w1, sign, &overflow);
1299 break;
1300 case ROUND_DIV_EXPR:
1301 res = wi::div_round (w0, w1, sign, &overflow);
1302 break;
1303 case CEIL_DIV_EXPR:
1304 res = wi::div_ceil (w0, w1, sign, &overflow);
1305 break;
1306 default:
1307 gcc_unreachable ();
1308 }
1309
1310 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
1311 {
1312 // For division, the only case is -INF / -1 = +INF.
1313 res = wi::max_value (w0.get_precision (), sign);
1314 return false;
1315 }
1316 return overflow;
1317 }
1318
1319 void
1320 operator_div::wi_fold (irange &r, tree type,
1321 const wide_int &lh_lb, const wide_int &lh_ub,
1322 const wide_int &rh_lb, const wide_int &rh_ub) const
1323 {
1324 // If we know we will divide by zero...
1325 if (rh_lb == 0 && rh_ub == 0)
1326 {
1327 r.set_varying (type);
1328 return;
1329 }
1330
1331 const wide_int dividend_min = lh_lb;
1332 const wide_int dividend_max = lh_ub;
1333 const wide_int divisor_min = rh_lb;
1334 const wide_int divisor_max = rh_ub;
1335 signop sign = TYPE_SIGN (type);
1336 unsigned prec = TYPE_PRECISION (type);
1337 wide_int extra_min, extra_max;
1338
1339 // If we know we won't divide by zero, just do the division.
1340 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
1341 {
1342 wi_cross_product (r, type, dividend_min, dividend_max,
1343 divisor_min, divisor_max);
1344 return;
1345 }
1346
1347 // If flag_non_call_exceptions, we must not eliminate a division by zero.
1348 if (cfun->can_throw_non_call_exceptions)
1349 {
1350 r.set_varying (type);
1351 return;
1352 }
1353
1354 // If we're definitely dividing by zero, there's nothing to do.
1355 if (wi_zero_p (type, divisor_min, divisor_max))
1356 {
1357 r.set_varying (type);
1358 return;
1359 }
1360
1361 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
1362 // skip any division by zero.
1363
1364 // First divide by the negative numbers, if any.
1365 if (wi::neg_p (divisor_min, sign))
1366 wi_cross_product (r, type, dividend_min, dividend_max,
1367 divisor_min, wi::minus_one (prec));
1368 else
1369 r.set_undefined ();
1370
1371 // Then divide by the non-zero positive numbers, if any.
1372 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
1373 {
1374 int_range_max tmp;
1375 wi_cross_product (tmp, type, dividend_min, dividend_max,
1376 wi::one (prec), divisor_max);
1377 r.union_ (tmp);
1378 }
1379 // We shouldn't still have undefined here.
1380 gcc_checking_assert (!r.undefined_p ());
1381 }
1382
1383 operator_div op_trunc_div (TRUNC_DIV_EXPR);
1384 operator_div op_floor_div (FLOOR_DIV_EXPR);
1385 operator_div op_round_div (ROUND_DIV_EXPR);
1386 operator_div op_ceil_div (CEIL_DIV_EXPR);
1387
1388
1389 class operator_exact_divide : public operator_div
1390 {
1391 public:
1392 operator_exact_divide () : operator_div (TRUNC_DIV_EXPR) { }
1393 virtual bool op1_range (irange &r, tree type,
1394 const irange &lhs,
1395 const irange &op2) const;
1396
1397 } op_exact_div;
1398
1399 bool
1400 operator_exact_divide::op1_range (irange &r, tree type,
1401 const irange &lhs,
1402 const irange &op2) const
1403 {
1404 tree offset;
1405 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
1406 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
1407 // We wont bother trying to enumerate all the in between stuff :-P
1408 // TRUE accuraacy is [6,6][9,9][12,12]. This is unlikely to matter most of
1409 // the time however.
1410 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
1411 if (op2.singleton_p (&offset)
1412 && !integer_zerop (offset))
1413 return range_op_handler (MULT_EXPR, type)->fold_range (r, type, lhs, op2);
1414 return false;
1415 }
1416
1417
1418 class operator_lshift : public cross_product_operator
1419 {
1420 public:
1421 virtual bool op1_range (irange &r, tree type,
1422 const irange &lhs,
1423 const irange &op2) const;
1424 virtual bool fold_range (irange &r, tree type,
1425 const irange &op1,
1426 const irange &op2) const;
1427
1428 virtual void wi_fold (irange &r, tree type,
1429 const wide_int &lh_lb, const wide_int &lh_ub,
1430 const wide_int &rh_lb, const wide_int &rh_ub) const;
1431 virtual bool wi_op_overflows (wide_int &res,
1432 tree type,
1433 const wide_int &,
1434 const wide_int &) const;
1435 } op_lshift;
1436
1437 class operator_rshift : public cross_product_operator
1438 {
1439 public:
1440 virtual bool fold_range (irange &r, tree type,
1441 const irange &op1,
1442 const irange &op2) const;
1443 virtual void wi_fold (irange &r, tree type,
1444 const wide_int &lh_lb,
1445 const wide_int &lh_ub,
1446 const wide_int &rh_lb,
1447 const wide_int &rh_ub) const;
1448 virtual bool wi_op_overflows (wide_int &res,
1449 tree type,
1450 const wide_int &w0,
1451 const wide_int &w1) const;
1452 virtual bool op1_range (irange &, tree type,
1453 const irange &lhs,
1454 const irange &op2) const;
1455 } op_rshift;
1456
1457
1458 bool
1459 operator_lshift::fold_range (irange &r, tree type,
1460 const irange &op1,
1461 const irange &op2) const
1462 {
1463 int_range_max shift_range;
1464 if (!get_shift_range (shift_range, type, op2))
1465 {
1466 if (op2.undefined_p ())
1467 r.set_undefined ();
1468 else
1469 r.set_varying (type);
1470 return true;
1471 }
1472
1473 // Transform left shifts by constants into multiplies.
1474 if (shift_range.singleton_p ())
1475 {
1476 unsigned shift = shift_range.lower_bound ().to_uhwi ();
1477 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
1478 int_range<1> mult (type, tmp, tmp);
1479
1480 // Force wrapping multiplication.
1481 bool saved_flag_wrapv = flag_wrapv;
1482 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
1483 flag_wrapv = 1;
1484 flag_wrapv_pointer = 1;
1485 bool b = op_mult.fold_range (r, type, op1, mult);
1486 flag_wrapv = saved_flag_wrapv;
1487 flag_wrapv_pointer = saved_flag_wrapv_pointer;
1488 return b;
1489 }
1490 else
1491 // Otherwise, invoke the generic fold routine.
1492 return range_operator::fold_range (r, type, op1, shift_range);
1493 }
1494
1495 void
1496 operator_lshift::wi_fold (irange &r, tree type,
1497 const wide_int &lh_lb, const wide_int &lh_ub,
1498 const wide_int &rh_lb, const wide_int &rh_ub) const
1499 {
1500 signop sign = TYPE_SIGN (type);
1501 unsigned prec = TYPE_PRECISION (type);
1502 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
1503 int bound_shift = overflow_pos - rh_ub.to_shwi ();
1504 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
1505 // overflow. However, for that to happen, rh.max needs to be zero,
1506 // which means rh is a singleton range of zero, which means it
1507 // should be handled by the lshift fold_range above.
1508 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
1509 wide_int complement = ~(bound - 1);
1510 wide_int low_bound, high_bound;
1511 bool in_bounds = false;
1512
1513 if (sign == UNSIGNED)
1514 {
1515 low_bound = bound;
1516 high_bound = complement;
1517 if (wi::ltu_p (lh_ub, low_bound))
1518 {
1519 // [5, 6] << [1, 2] == [10, 24].
1520 // We're shifting out only zeroes, the value increases
1521 // monotonically.
1522 in_bounds = true;
1523 }
1524 else if (wi::ltu_p (high_bound, lh_lb))
1525 {
1526 // [0xffffff00, 0xffffffff] << [1, 2]
1527 // == [0xfffffc00, 0xfffffffe].
1528 // We're shifting out only ones, the value decreases
1529 // monotonically.
1530 in_bounds = true;
1531 }
1532 }
1533 else
1534 {
1535 // [-1, 1] << [1, 2] == [-4, 4]
1536 low_bound = complement;
1537 high_bound = bound;
1538 if (wi::lts_p (lh_ub, high_bound)
1539 && wi::lts_p (low_bound, lh_lb))
1540 {
1541 // For non-negative numbers, we're shifting out only zeroes,
1542 // the value increases monotonically. For negative numbers,
1543 // we're shifting out only ones, the value decreases
1544 // monotonically.
1545 in_bounds = true;
1546 }
1547 }
1548
1549 if (in_bounds)
1550 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1551 else
1552 r.set_varying (type);
1553 }
1554
1555 bool
1556 operator_lshift::wi_op_overflows (wide_int &res, tree type,
1557 const wide_int &w0, const wide_int &w1) const
1558 {
1559 signop sign = TYPE_SIGN (type);
1560 if (wi::neg_p (w1))
1561 {
1562 // It's unclear from the C standard whether shifts can overflow.
1563 // The following code ignores overflow; perhaps a C standard
1564 // interpretation ruling is needed.
1565 res = wi::rshift (w0, -w1, sign);
1566 }
1567 else
1568 res = wi::lshift (w0, w1);
1569 return false;
1570 }
1571
1572 bool
1573 operator_lshift::op1_range (irange &r,
1574 tree type,
1575 const irange &lhs,
1576 const irange &op2) const
1577 {
1578 tree shift_amount;
1579 if (op2.singleton_p (&shift_amount))
1580 {
1581 wide_int shift = wi::to_wide (shift_amount);
1582 if (wi::lt_p (shift, 0, SIGNED))
1583 return false;
1584 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
1585 TYPE_PRECISION (op2.type ())),
1586 UNSIGNED))
1587 return false;
1588 if (shift == 0)
1589 {
1590 r = lhs;
1591 return true;
1592 }
1593
1594 // Work completely in unsigned mode to start.
1595 tree utype = type;
1596 if (TYPE_SIGN (type) == SIGNED)
1597 {
1598 int_range_max tmp = lhs;
1599 utype = unsigned_type_for (type);
1600 range_cast (tmp, utype);
1601 op_rshift.fold_range (r, utype, tmp, op2);
1602 }
1603 else
1604 op_rshift.fold_range (r, utype, lhs, op2);
1605
1606 // Start with ranges which can produce the LHS by right shifting the
1607 // result by the shift amount.
1608 // ie [0x08, 0xF0] = op1 << 2 will start with
1609 // [00001000, 11110000] = op1 << 2
1610 // [0x02, 0x4C] aka [00000010, 00111100]
1611
1612 // Then create a range from the LB with the least significant upper bit
1613 // set, to the upper bound with all the bits set.
1614 // This would be [0x42, 0xFC] aka [01000010, 11111100].
1615
1616 // Ideally we do this for each subrange, but just lump them all for now.
1617 unsigned low_bits = TYPE_PRECISION (utype)
1618 - TREE_INT_CST_LOW (shift_amount);
1619 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
1620 wide_int new_ub = wi::bit_or (up_mask, r.upper_bound ());
1621 wide_int new_lb = wi::set_bit (r.lower_bound (), low_bits);
1622 int_range<2> fill_range (utype, new_lb, new_ub);
1623 r.union_ (fill_range);
1624
1625 if (utype != type)
1626 range_cast (r, type);
1627 return true;
1628 }
1629 return false;
1630 }
1631
1632 bool
1633 operator_rshift::op1_range (irange &r,
1634 tree type,
1635 const irange &lhs,
1636 const irange &op2) const
1637 {
1638 tree shift;
1639 if (op2.singleton_p (&shift))
1640 {
1641 // Ignore nonsensical shifts.
1642 unsigned prec = TYPE_PRECISION (type);
1643 if (wi::ge_p (wi::to_wide (shift),
1644 wi::uhwi (prec, TYPE_PRECISION (TREE_TYPE (shift))),
1645 UNSIGNED))
1646 return false;
1647 if (wi::to_wide (shift) == 0)
1648 {
1649 r = lhs;
1650 return true;
1651 }
1652
1653 // Folding the original operation may discard some impossible
1654 // ranges from the LHS.
1655 int_range_max lhs_refined;
1656 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
1657 lhs_refined.intersect (lhs);
1658 if (lhs_refined.undefined_p ())
1659 {
1660 r.set_undefined ();
1661 return true;
1662 }
1663 int_range_max shift_range (shift, shift);
1664 int_range_max lb, ub;
1665 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
1666 // LHS
1667 // 0000 0111 = OP1 >> 3
1668 //
1669 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
1670 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
1671 // right hand side (0x07).
1672 tree mask = fold_build1 (BIT_NOT_EXPR, type,
1673 fold_build2 (LSHIFT_EXPR, type,
1674 build_minus_one_cst (type),
1675 shift));
1676 int_range_max mask_range (build_zero_cst (type), mask);
1677 op_plus.fold_range (ub, type, lb, mask_range);
1678 r = lb;
1679 r.union_ (ub);
1680 if (!lhs_refined.contains_p (build_zero_cst (type)))
1681 {
1682 mask_range.invert ();
1683 r.intersect (mask_range);
1684 }
1685 return true;
1686 }
1687 return false;
1688 }
1689
1690 bool
1691 operator_rshift::wi_op_overflows (wide_int &res,
1692 tree type,
1693 const wide_int &w0,
1694 const wide_int &w1) const
1695 {
1696 signop sign = TYPE_SIGN (type);
1697 if (wi::neg_p (w1))
1698 res = wi::lshift (w0, -w1);
1699 else
1700 {
1701 // It's unclear from the C standard whether shifts can overflow.
1702 // The following code ignores overflow; perhaps a C standard
1703 // interpretation ruling is needed.
1704 res = wi::rshift (w0, w1, sign);
1705 }
1706 return false;
1707 }
1708
1709 bool
1710 operator_rshift::fold_range (irange &r, tree type,
1711 const irange &op1,
1712 const irange &op2) const
1713 {
1714 int_range_max shift;
1715 if (!get_shift_range (shift, type, op2))
1716 {
1717 if (op2.undefined_p ())
1718 r.set_undefined ();
1719 else
1720 r.set_varying (type);
1721 return true;
1722 }
1723
1724 return range_operator::fold_range (r, type, op1, shift);
1725 }
1726
1727 void
1728 operator_rshift::wi_fold (irange &r, tree type,
1729 const wide_int &lh_lb, const wide_int &lh_ub,
1730 const wide_int &rh_lb, const wide_int &rh_ub) const
1731 {
1732 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
1733 }
1734
1735
1736 class operator_cast: public range_operator
1737 {
1738 public:
1739 virtual bool fold_range (irange &r, tree type,
1740 const irange &op1,
1741 const irange &op2) const;
1742 virtual bool op1_range (irange &r, tree type,
1743 const irange &lhs,
1744 const irange &op2) const;
1745 private:
1746 bool truncating_cast_p (const irange &inner, const irange &outer) const;
1747 bool inside_domain_p (const wide_int &min, const wide_int &max,
1748 const irange &outer) const;
1749 void fold_pair (irange &r, unsigned index, const irange &inner,
1750 const irange &outer) const;
1751 } op_convert;
1752
1753 // Return TRUE if casting from INNER to OUTER is a truncating cast.
1754
1755 inline bool
1756 operator_cast::truncating_cast_p (const irange &inner,
1757 const irange &outer) const
1758 {
1759 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
1760 }
1761
1762 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
1763
1764 bool
1765 operator_cast::inside_domain_p (const wide_int &min,
1766 const wide_int &max,
1767 const irange &range) const
1768 {
1769 wide_int domain_min = wi::to_wide (vrp_val_min (range.type ()));
1770 wide_int domain_max = wi::to_wide (vrp_val_max (range.type ()));
1771 signop domain_sign = TYPE_SIGN (range.type ());
1772 return (wi::le_p (min, domain_max, domain_sign)
1773 && wi::le_p (max, domain_max, domain_sign)
1774 && wi::ge_p (min, domain_min, domain_sign)
1775 && wi::ge_p (max, domain_min, domain_sign));
1776 }
1777
1778
1779 // Helper for fold_range which work on a pair at a time.
1780
1781 void
1782 operator_cast::fold_pair (irange &r, unsigned index,
1783 const irange &inner,
1784 const irange &outer) const
1785 {
1786 tree inner_type = inner.type ();
1787 tree outer_type = outer.type ();
1788 signop inner_sign = TYPE_SIGN (inner_type);
1789 unsigned outer_prec = TYPE_PRECISION (outer_type);
1790
1791 // check to see if casting from INNER to OUTER is a conversion that
1792 // fits in the resulting OUTER type.
1793 wide_int inner_lb = inner.lower_bound (index);
1794 wide_int inner_ub = inner.upper_bound (index);
1795 if (truncating_cast_p (inner, outer))
1796 {
1797 // We may be able to accomodate a truncating cast if the
1798 // resulting range can be represented in the target type...
1799 if (wi::rshift (wi::sub (inner_ub, inner_lb),
1800 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
1801 inner_sign) != 0)
1802 {
1803 r.set_varying (outer_type);
1804 return;
1805 }
1806 }
1807 // ...but we must still verify that the final range fits in the
1808 // domain. This catches -fstrict-enum restrictions where the domain
1809 // range is smaller than what fits in the underlying type.
1810 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
1811 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
1812 if (inside_domain_p (min, max, outer))
1813 create_possibly_reversed_range (r, outer_type, min, max);
1814 else
1815 r.set_varying (outer_type);
1816 }
1817
1818
1819 bool
1820 operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
1821 const irange &inner,
1822 const irange &outer) const
1823 {
1824 if (empty_range_varying (r, type, inner, outer))
1825 return true;
1826
1827 gcc_checking_assert (outer.varying_p ());
1828 gcc_checking_assert (inner.num_pairs () > 0);
1829
1830 // Avoid a temporary by folding the first pair directly into the result.
1831 fold_pair (r, 0, inner, outer);
1832
1833 // Then process any additonal pairs by unioning with their results.
1834 for (unsigned x = 1; x < inner.num_pairs (); ++x)
1835 {
1836 int_range_max tmp;
1837 fold_pair (tmp, x, inner, outer);
1838 r.union_ (tmp);
1839 if (r.varying_p ())
1840 return true;
1841 }
1842 return true;
1843 }
1844
1845 bool
1846 operator_cast::op1_range (irange &r, tree type,
1847 const irange &lhs,
1848 const irange &op2) const
1849 {
1850 tree lhs_type = lhs.type ();
1851 gcc_checking_assert (types_compatible_p (op2.type(), type));
1852
1853 // If we are calculating a pointer, shortcut to what we really care about.
1854 if (POINTER_TYPE_P (type))
1855 {
1856 // Conversion from other pointers or a constant (including 0/NULL)
1857 // are straightforward.
1858 if (POINTER_TYPE_P (lhs.type ())
1859 || (lhs.singleton_p ()
1860 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
1861 {
1862 r = lhs;
1863 range_cast (r, type);
1864 }
1865 else
1866 {
1867 // If the LHS is not a pointer nor a singleton, then it is
1868 // either VARYING or non-zero.
1869 if (!lhs.contains_p (build_zero_cst (lhs.type ())))
1870 r.set_nonzero (type);
1871 else
1872 r.set_varying (type);
1873 }
1874 r.intersect (op2);
1875 return true;
1876 }
1877
1878 if (truncating_cast_p (op2, lhs))
1879 {
1880 if (lhs.varying_p ())
1881 r.set_varying (type);
1882 else
1883 {
1884 // We want to insert the LHS as an unsigned value since it
1885 // would not trigger the signed bit of the larger type.
1886 int_range_max converted_lhs = lhs;
1887 range_cast (converted_lhs, unsigned_type_for (lhs_type));
1888 range_cast (converted_lhs, type);
1889 // Start by building the positive signed outer range for the type.
1890 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
1891 TYPE_PRECISION (type));
1892 r = int_range<1> (type, lim, wi::max_value (TYPE_PRECISION (type),
1893 SIGNED));
1894 // For the signed part, we need to simply union the 2 ranges now.
1895 r.union_ (converted_lhs);
1896
1897 // Create maximal negative number outside of LHS bits.
1898 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
1899 TYPE_PRECISION (type));
1900 // Add this to the unsigned LHS range(s).
1901 int_range_max lim_range (type, lim, lim);
1902 int_range_max lhs_neg;
1903 range_op_handler (PLUS_EXPR, type)->fold_range (lhs_neg,
1904 type,
1905 converted_lhs,
1906 lim_range);
1907 // lhs_neg now has all the negative versions of the LHS.
1908 // Now union in all the values from SIGNED MIN (0x80000) to
1909 // lim-1 in order to fill in all the ranges with the upper
1910 // bits set.
1911
1912 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
1913 // we don't need to create a range from min to lim-1
1914 // calculate neg range traps trying to create [lim, lim - 1].
1915 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
1916 if (lim != min_val)
1917 {
1918 int_range_max neg (type,
1919 wi::min_value (TYPE_PRECISION (type),
1920 SIGNED),
1921 lim - 1);
1922 lhs_neg.union_ (neg);
1923 }
1924 // And finally, munge the signed and unsigned portions.
1925 r.union_ (lhs_neg);
1926 }
1927 // And intersect with any known value passed in the extra operand.
1928 r.intersect (op2);
1929 return true;
1930 }
1931
1932 int_range_max tmp;
1933 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
1934 tmp = lhs;
1935 else
1936 {
1937 // The cast is not truncating, and the range is restricted to
1938 // the range of the RHS by this assignment.
1939 //
1940 // Cast the range of the RHS to the type of the LHS.
1941 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
1942 // Intersect this with the LHS range will produce the range,
1943 // which will be cast to the RHS type before returning.
1944 tmp.intersect (lhs);
1945 }
1946
1947 // Cast the calculated range to the type of the RHS.
1948 fold_range (r, type, tmp, int_range<1> (type));
1949 return true;
1950 }
1951
1952
1953 class operator_logical_and : public range_operator
1954 {
1955 public:
1956 virtual bool fold_range (irange &r, tree type,
1957 const irange &lh,
1958 const irange &rh) const;
1959 virtual bool op1_range (irange &r, tree type,
1960 const irange &lhs,
1961 const irange &op2) const;
1962 virtual bool op2_range (irange &r, tree type,
1963 const irange &lhs,
1964 const irange &op1) const;
1965 } op_logical_and;
1966
1967
1968 bool
1969 operator_logical_and::fold_range (irange &r, tree type,
1970 const irange &lh,
1971 const irange &rh) const
1972 {
1973 if (empty_range_varying (r, type, lh, rh))
1974 return true;
1975
1976 // 0 && anything is 0.
1977 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
1978 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
1979 r = range_false (type);
1980 else if (lh.contains_p (build_zero_cst (lh.type ()))
1981 || rh.contains_p (build_zero_cst (rh.type ())))
1982 // To reach this point, there must be a logical 1 on each side, and
1983 // the only remaining question is whether there is a zero or not.
1984 r = range_true_and_false (type);
1985 else
1986 r = range_true (type);
1987 return true;
1988 }
1989
1990 bool
1991 operator_logical_and::op1_range (irange &r, tree type,
1992 const irange &lhs,
1993 const irange &op2 ATTRIBUTE_UNUSED) const
1994 {
1995 switch (get_bool_state (r, lhs, type))
1996 {
1997 case BRS_TRUE:
1998 // A true result means both sides of the AND must be true.
1999 r = range_true (type);
2000 break;
2001 default:
2002 // Any other result means only one side has to be false, the
2003 // other side can be anything. So we cannott be sure of any
2004 // result here.
2005 r = range_true_and_false (type);
2006 break;
2007 }
2008 return true;
2009 }
2010
2011 bool
2012 operator_logical_and::op2_range (irange &r, tree type,
2013 const irange &lhs,
2014 const irange &op1) const
2015 {
2016 return operator_logical_and::op1_range (r, type, lhs, op1);
2017 }
2018
2019
2020 class operator_bitwise_and : public range_operator
2021 {
2022 public:
2023 virtual bool fold_range (irange &r, tree type,
2024 const irange &lh,
2025 const irange &rh) const;
2026 virtual bool op1_range (irange &r, tree type,
2027 const irange &lhs,
2028 const irange &op2) const;
2029 virtual bool op2_range (irange &r, tree type,
2030 const irange &lhs,
2031 const irange &op1) const;
2032 virtual void wi_fold (irange &r, tree type,
2033 const wide_int &lh_lb,
2034 const wide_int &lh_ub,
2035 const wide_int &rh_lb,
2036 const wide_int &rh_ub) const;
2037 private:
2038 void simple_op1_range_solver (irange &r, tree type,
2039 const irange &lhs,
2040 const irange &op2) const;
2041 void remove_impossible_ranges (irange &r, const irange &rh) const;
2042 } op_bitwise_and;
2043
2044 static bool
2045 unsigned_singleton_p (const irange &op)
2046 {
2047 tree mask;
2048 if (op.singleton_p (&mask))
2049 {
2050 wide_int x = wi::to_wide (mask);
2051 return wi::ge_p (x, 0, TYPE_SIGN (op.type ()));
2052 }
2053 return false;
2054 }
2055
2056 // Remove any ranges from R that are known to be impossible when an
2057 // range is ANDed with MASK.
2058
2059 void
2060 operator_bitwise_and::remove_impossible_ranges (irange &r,
2061 const irange &rmask) const
2062 {
2063 if (r.undefined_p () || !unsigned_singleton_p (rmask))
2064 return;
2065
2066 wide_int mask = rmask.lower_bound ();
2067 tree type = r.type ();
2068 int prec = TYPE_PRECISION (type);
2069 int leading_zeros = wi::clz (mask);
2070 int_range_max impossible_ranges;
2071
2072 /* We know that starting at the most significant bit, any 0 in the
2073 mask means the resulting range cannot contain a 1 in that same
2074 position. This means the following ranges are impossible:
2075
2076 x & 0b1001 1010
2077 IMPOSSIBLE RANGES
2078 01xx xxxx [0100 0000, 0111 1111]
2079 001x xxxx [0010 0000, 0011 1111]
2080 0000 01xx [0000 0100, 0000 0111]
2081 0000 0001 [0000 0001, 0000 0001]
2082 */
2083 wide_int one = wi::one (prec);
2084 for (int i = 0; i < prec - leading_zeros - 1; ++i)
2085 if (wi::bit_and (mask, wi::lshift (one, wi::uhwi (i, prec))) == 0)
2086 {
2087 tree lb = fold_build2 (LSHIFT_EXPR, type,
2088 build_one_cst (type),
2089 build_int_cst (type, i));
2090 tree ub_left = fold_build1 (BIT_NOT_EXPR, type,
2091 fold_build2 (LSHIFT_EXPR, type,
2092 build_minus_one_cst (type),
2093 build_int_cst (type, i)));
2094 tree ub_right = fold_build2 (LSHIFT_EXPR, type,
2095 build_one_cst (type),
2096 build_int_cst (type, i));
2097 tree ub = fold_build2 (BIT_IOR_EXPR, type, ub_left, ub_right);
2098 impossible_ranges.union_ (int_range<1> (lb, ub));
2099 }
2100 if (!impossible_ranges.undefined_p ())
2101 {
2102 impossible_ranges.invert ();
2103 r.intersect (impossible_ranges);
2104 }
2105 }
2106
2107 bool
2108 operator_bitwise_and::fold_range (irange &r, tree type,
2109 const irange &lh,
2110 const irange &rh) const
2111 {
2112 if (range_operator::fold_range (r, type, lh, rh))
2113 {
2114 // FIXME: This is temporarily disabled because, though it
2115 // generates better ranges, it's noticeably slower for evrp.
2116 // remove_impossible_ranges (r, rh);
2117 return true;
2118 }
2119 return false;
2120 }
2121
2122
2123 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
2124 // possible. Basically, see if we can optimize:
2125 //
2126 // [LB, UB] op Z
2127 // into:
2128 // [LB op Z, UB op Z]
2129 //
2130 // If the optimization was successful, accumulate the range in R and
2131 // return TRUE.
2132
2133 static bool
2134 wi_optimize_and_or (irange &r,
2135 enum tree_code code,
2136 tree type,
2137 const wide_int &lh_lb, const wide_int &lh_ub,
2138 const wide_int &rh_lb, const wide_int &rh_ub)
2139 {
2140 // Calculate the singleton mask among the ranges, if any.
2141 wide_int lower_bound, upper_bound, mask;
2142 if (wi::eq_p (rh_lb, rh_ub))
2143 {
2144 mask = rh_lb;
2145 lower_bound = lh_lb;
2146 upper_bound = lh_ub;
2147 }
2148 else if (wi::eq_p (lh_lb, lh_ub))
2149 {
2150 mask = lh_lb;
2151 lower_bound = rh_lb;
2152 upper_bound = rh_ub;
2153 }
2154 else
2155 return false;
2156
2157 // If Z is a constant which (for op | its bitwise not) has n
2158 // consecutive least significant bits cleared followed by m 1
2159 // consecutive bits set immediately above it and either
2160 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2161 //
2162 // The least significant n bits of all the values in the range are
2163 // cleared or set, the m bits above it are preserved and any bits
2164 // above these are required to be the same for all values in the
2165 // range.
2166 wide_int w = mask;
2167 int m = 0, n = 0;
2168 if (code == BIT_IOR_EXPR)
2169 w = ~w;
2170 if (wi::eq_p (w, 0))
2171 n = w.get_precision ();
2172 else
2173 {
2174 n = wi::ctz (w);
2175 w = ~(w | wi::mask (n, false, w.get_precision ()));
2176 if (wi::eq_p (w, 0))
2177 m = w.get_precision () - n;
2178 else
2179 m = wi::ctz (w) - n;
2180 }
2181 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
2182 if ((new_mask & lower_bound) != (new_mask & upper_bound))
2183 return false;
2184
2185 wide_int res_lb, res_ub;
2186 if (code == BIT_AND_EXPR)
2187 {
2188 res_lb = wi::bit_and (lower_bound, mask);
2189 res_ub = wi::bit_and (upper_bound, mask);
2190 }
2191 else if (code == BIT_IOR_EXPR)
2192 {
2193 res_lb = wi::bit_or (lower_bound, mask);
2194 res_ub = wi::bit_or (upper_bound, mask);
2195 }
2196 else
2197 gcc_unreachable ();
2198 value_range_with_overflow (r, type, res_lb, res_ub);
2199
2200 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
2201 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
2202 {
2203 int_range<2> tmp;
2204 tmp.set_nonzero (type);
2205 r.intersect (tmp);
2206 }
2207 return true;
2208 }
2209
2210 // For range [LB, UB] compute two wide_int bit masks.
2211 //
2212 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
2213 // for all numbers in the range the bit is 0, otherwise it might be 0
2214 // or 1.
2215 //
2216 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
2217 // for all numbers in the range the bit is 1, otherwise it might be 0
2218 // or 1.
2219
2220 void
2221 wi_set_zero_nonzero_bits (tree type,
2222 const wide_int &lb, const wide_int &ub,
2223 wide_int &maybe_nonzero,
2224 wide_int &mustbe_nonzero)
2225 {
2226 signop sign = TYPE_SIGN (type);
2227
2228 if (wi::eq_p (lb, ub))
2229 maybe_nonzero = mustbe_nonzero = lb;
2230 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
2231 {
2232 wide_int xor_mask = lb ^ ub;
2233 maybe_nonzero = lb | ub;
2234 mustbe_nonzero = lb & ub;
2235 if (xor_mask != 0)
2236 {
2237 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2238 maybe_nonzero.get_precision ());
2239 maybe_nonzero = maybe_nonzero | mask;
2240 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
2241 }
2242 }
2243 else
2244 {
2245 maybe_nonzero = wi::minus_one (lb.get_precision ());
2246 mustbe_nonzero = wi::zero (lb.get_precision ());
2247 }
2248 }
2249
2250 void
2251 operator_bitwise_and::wi_fold (irange &r, tree type,
2252 const wide_int &lh_lb,
2253 const wide_int &lh_ub,
2254 const wide_int &rh_lb,
2255 const wide_int &rh_ub) const
2256 {
2257 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2258 return;
2259
2260 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2261 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2262 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2263 maybe_nonzero_lh, mustbe_nonzero_lh);
2264 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2265 maybe_nonzero_rh, mustbe_nonzero_rh);
2266
2267 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
2268 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
2269 signop sign = TYPE_SIGN (type);
2270 unsigned prec = TYPE_PRECISION (type);
2271 // If both input ranges contain only negative values, we can
2272 // truncate the result range maximum to the minimum of the
2273 // input range maxima.
2274 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
2275 {
2276 new_ub = wi::min (new_ub, lh_ub, sign);
2277 new_ub = wi::min (new_ub, rh_ub, sign);
2278 }
2279 // If either input range contains only non-negative values
2280 // we can truncate the result range maximum to the respective
2281 // maximum of the input range.
2282 if (wi::ge_p (lh_lb, 0, sign))
2283 new_ub = wi::min (new_ub, lh_ub, sign);
2284 if (wi::ge_p (rh_lb, 0, sign))
2285 new_ub = wi::min (new_ub, rh_ub, sign);
2286 // PR68217: In case of signed & sign-bit-CST should
2287 // result in [-INF, 0] instead of [-INF, INF].
2288 if (wi::gt_p (new_lb, new_ub, sign))
2289 {
2290 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
2291 if (sign == SIGNED
2292 && ((wi::eq_p (lh_lb, lh_ub)
2293 && !wi::cmps (lh_lb, sign_bit))
2294 || (wi::eq_p (rh_lb, rh_ub)
2295 && !wi::cmps (rh_lb, sign_bit))))
2296 {
2297 new_lb = wi::min_value (prec, sign);
2298 new_ub = wi::zero (prec);
2299 }
2300 }
2301 // If the limits got swapped around, return varying.
2302 if (wi::gt_p (new_lb, new_ub,sign))
2303 r.set_varying (type);
2304 else
2305 value_range_with_overflow (r, type, new_lb, new_ub);
2306 }
2307
2308 static void
2309 set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
2310 {
2311 if (!lhs.contains_p (build_zero_cst (type)))
2312 r = range_nonzero (type);
2313 else
2314 r.set_varying (type);
2315 }
2316
2317 // This was shamelessly stolen from register_edge_assert_for_2 and
2318 // adjusted to work with iranges.
2319
2320 void
2321 operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
2322 const irange &lhs,
2323 const irange &op2) const
2324 {
2325 if (!op2.singleton_p ())
2326 {
2327 set_nonzero_range_from_mask (r, type, lhs);
2328 return;
2329 }
2330 unsigned int nprec = TYPE_PRECISION (type);
2331 wide_int cst2v = op2.lower_bound ();
2332 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
2333 wide_int sgnbit;
2334 if (cst2n)
2335 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2336 else
2337 sgnbit = wi::zero (nprec);
2338
2339 // Solve [lhs.lower_bound (), +INF] = x & MASK.
2340 //
2341 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
2342 // maximum unsigned value is ~0. For signed comparison, if CST2
2343 // doesn't have the most significant bit set, handle it similarly. If
2344 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
2345 wide_int valv = lhs.lower_bound ();
2346 wide_int minv = valv & cst2v, maxv;
2347 bool we_know_nothing = false;
2348 if (minv != valv)
2349 {
2350 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
2351 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2352 if (minv == valv)
2353 {
2354 // If we can't determine anything on this bound, fall
2355 // through and conservatively solve for the other end point.
2356 we_know_nothing = true;
2357 }
2358 }
2359 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2360 if (we_know_nothing)
2361 r.set_varying (type);
2362 else
2363 r = int_range<1> (type, minv, maxv);
2364
2365 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
2366 //
2367 // Minimum unsigned value for <= is 0 and maximum unsigned value is
2368 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
2369 // VAL2 where
2370 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2371 // as maximum.
2372 // For signed comparison, if CST2 doesn't have most significant bit
2373 // set, handle it similarly. If CST2 has MSB set, the maximum is
2374 // the same and minimum is INT_MIN.
2375 valv = lhs.upper_bound ();
2376 minv = valv & cst2v;
2377 if (minv == valv)
2378 maxv = valv;
2379 else
2380 {
2381 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2382 if (maxv == valv)
2383 {
2384 // If we couldn't determine anything on either bound, return
2385 // undefined.
2386 if (we_know_nothing)
2387 r.set_undefined ();
2388 return;
2389 }
2390 maxv -= 1;
2391 }
2392 maxv |= ~cst2v;
2393 minv = sgnbit;
2394 int_range<1> upper_bits (type, minv, maxv);
2395 r.intersect (upper_bits);
2396 }
2397
2398 bool
2399 operator_bitwise_and::op1_range (irange &r, tree type,
2400 const irange &lhs,
2401 const irange &op2) const
2402 {
2403 if (types_compatible_p (type, boolean_type_node))
2404 return op_logical_and.op1_range (r, type, lhs, op2);
2405
2406 r.set_undefined ();
2407 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
2408 {
2409 int_range_max chunk (lhs.type (),
2410 lhs.lower_bound (i),
2411 lhs.upper_bound (i));
2412 int_range_max res;
2413 simple_op1_range_solver (res, type, chunk, op2);
2414 r.union_ (res);
2415 }
2416 if (r.undefined_p ())
2417 set_nonzero_range_from_mask (r, type, lhs);
2418 return true;
2419 }
2420
2421 bool
2422 operator_bitwise_and::op2_range (irange &r, tree type,
2423 const irange &lhs,
2424 const irange &op1) const
2425 {
2426 return operator_bitwise_and::op1_range (r, type, lhs, op1);
2427 }
2428
2429
2430 class operator_logical_or : public range_operator
2431 {
2432 public:
2433 virtual bool fold_range (irange &r, tree type,
2434 const irange &lh,
2435 const irange &rh) const;
2436 virtual bool op1_range (irange &r, tree type,
2437 const irange &lhs,
2438 const irange &op2) const;
2439 virtual bool op2_range (irange &r, tree type,
2440 const irange &lhs,
2441 const irange &op1) const;
2442 } op_logical_or;
2443
2444 bool
2445 operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2446 const irange &lh,
2447 const irange &rh) const
2448 {
2449 if (empty_range_varying (r, type, lh, rh))
2450 return true;
2451
2452 r = lh;
2453 r.union_ (rh);
2454 return true;
2455 }
2456
2457 bool
2458 operator_logical_or::op1_range (irange &r, tree type,
2459 const irange &lhs,
2460 const irange &op2 ATTRIBUTE_UNUSED) const
2461 {
2462 switch (get_bool_state (r, lhs, type))
2463 {
2464 case BRS_FALSE:
2465 // A false result means both sides of the OR must be false.
2466 r = range_false (type);
2467 break;
2468 default:
2469 // Any other result means only one side has to be true, the
2470 // other side can be anything. so we can't be sure of any result
2471 // here.
2472 r = range_true_and_false (type);
2473 break;
2474 }
2475 return true;
2476 }
2477
2478 bool
2479 operator_logical_or::op2_range (irange &r, tree type,
2480 const irange &lhs,
2481 const irange &op1) const
2482 {
2483 return operator_logical_or::op1_range (r, type, lhs, op1);
2484 }
2485
2486
2487 class operator_bitwise_or : public range_operator
2488 {
2489 public:
2490 virtual bool op1_range (irange &r, tree type,
2491 const irange &lhs,
2492 const irange &op2) const;
2493 virtual bool op2_range (irange &r, tree type,
2494 const irange &lhs,
2495 const irange &op1) const;
2496 virtual void wi_fold (irange &r, tree type,
2497 const wide_int &lh_lb,
2498 const wide_int &lh_ub,
2499 const wide_int &rh_lb,
2500 const wide_int &rh_ub) const;
2501 } op_bitwise_or;
2502
2503 void
2504 operator_bitwise_or::wi_fold (irange &r, tree type,
2505 const wide_int &lh_lb,
2506 const wide_int &lh_ub,
2507 const wide_int &rh_lb,
2508 const wide_int &rh_ub) const
2509 {
2510 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
2511 return;
2512
2513 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2514 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2515 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2516 maybe_nonzero_lh, mustbe_nonzero_lh);
2517 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2518 maybe_nonzero_rh, mustbe_nonzero_rh);
2519 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
2520 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
2521 signop sign = TYPE_SIGN (type);
2522 // If the input ranges contain only positive values we can
2523 // truncate the minimum of the result range to the maximum
2524 // of the input range minima.
2525 if (wi::ge_p (lh_lb, 0, sign)
2526 && wi::ge_p (rh_lb, 0, sign))
2527 {
2528 new_lb = wi::max (new_lb, lh_lb, sign);
2529 new_lb = wi::max (new_lb, rh_lb, sign);
2530 }
2531 // If either input range contains only negative values
2532 // we can truncate the minimum of the result range to the
2533 // respective minimum range.
2534 if (wi::lt_p (lh_ub, 0, sign))
2535 new_lb = wi::max (new_lb, lh_lb, sign);
2536 if (wi::lt_p (rh_ub, 0, sign))
2537 new_lb = wi::max (new_lb, rh_lb, sign);
2538 // If the limits got swapped around, return varying.
2539 if (wi::gt_p (new_lb, new_ub,sign))
2540 r.set_varying (type);
2541 else
2542 value_range_with_overflow (r, type, new_lb, new_ub);
2543 }
2544
2545 bool
2546 operator_bitwise_or::op1_range (irange &r, tree type,
2547 const irange &lhs,
2548 const irange &op2) const
2549 {
2550 // If this is really a logical wi_fold, call that.
2551 if (types_compatible_p (type, boolean_type_node))
2552 return op_logical_or.op1_range (r, type, lhs, op2);
2553
2554 if (lhs.zero_p ())
2555 {
2556 tree zero = build_zero_cst (type);
2557 r = int_range<1> (zero, zero);
2558 return true;
2559 }
2560 r.set_varying (type);
2561 return true;
2562 }
2563
2564 bool
2565 operator_bitwise_or::op2_range (irange &r, tree type,
2566 const irange &lhs,
2567 const irange &op1) const
2568 {
2569 return operator_bitwise_or::op1_range (r, type, lhs, op1);
2570 }
2571
2572
2573 class operator_bitwise_xor : public range_operator
2574 {
2575 public:
2576 virtual void wi_fold (irange &r, tree type,
2577 const wide_int &lh_lb,
2578 const wide_int &lh_ub,
2579 const wide_int &rh_lb,
2580 const wide_int &rh_ub) const;
2581 virtual bool op1_range (irange &r, tree type,
2582 const irange &lhs,
2583 const irange &op2) const;
2584 virtual bool op2_range (irange &r, tree type,
2585 const irange &lhs,
2586 const irange &op1) const;
2587 } op_bitwise_xor;
2588
2589 void
2590 operator_bitwise_xor::wi_fold (irange &r, tree type,
2591 const wide_int &lh_lb,
2592 const wide_int &lh_ub,
2593 const wide_int &rh_lb,
2594 const wide_int &rh_ub) const
2595 {
2596 signop sign = TYPE_SIGN (type);
2597 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
2598 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
2599 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
2600 maybe_nonzero_lh, mustbe_nonzero_lh);
2601 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
2602 maybe_nonzero_rh, mustbe_nonzero_rh);
2603
2604 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
2605 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
2606 wide_int result_one_bits
2607 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
2608 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
2609 wide_int new_ub = ~result_zero_bits;
2610 wide_int new_lb = result_one_bits;
2611
2612 // If the range has all positive or all negative values, the result
2613 // is better than VARYING.
2614 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
2615 value_range_with_overflow (r, type, new_lb, new_ub);
2616 else
2617 r.set_varying (type);
2618 }
2619
2620 bool
2621 operator_bitwise_xor::op1_range (irange &r, tree type,
2622 const irange &lhs,
2623 const irange &op2) const
2624 {
2625 if (lhs.undefined_p () || lhs.varying_p ())
2626 {
2627 r = lhs;
2628 return true;
2629 }
2630 if (types_compatible_p (type, boolean_type_node))
2631 {
2632 switch (get_bool_state (r, lhs, type))
2633 {
2634 case BRS_TRUE:
2635 if (op2.varying_p ())
2636 r.set_varying (type);
2637 else if (op2.zero_p ())
2638 r = range_true (type);
2639 else
2640 r = range_false (type);
2641 break;
2642 case BRS_FALSE:
2643 r = op2;
2644 break;
2645 default:
2646 gcc_unreachable ();
2647 }
2648 return true;
2649 }
2650 r.set_varying (type);
2651 return true;
2652 }
2653
2654 bool
2655 operator_bitwise_xor::op2_range (irange &r, tree type,
2656 const irange &lhs,
2657 const irange &op1) const
2658 {
2659 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
2660 }
2661
2662 class operator_trunc_mod : public range_operator
2663 {
2664 public:
2665 virtual void wi_fold (irange &r, tree type,
2666 const wide_int &lh_lb,
2667 const wide_int &lh_ub,
2668 const wide_int &rh_lb,
2669 const wide_int &rh_ub) const;
2670 virtual bool op1_range (irange &r, tree type,
2671 const irange &lhs,
2672 const irange &op2) const;
2673 virtual bool op2_range (irange &r, tree type,
2674 const irange &lhs,
2675 const irange &op1) const;
2676 } op_trunc_mod;
2677
2678 void
2679 operator_trunc_mod::wi_fold (irange &r, tree type,
2680 const wide_int &lh_lb,
2681 const wide_int &lh_ub,
2682 const wide_int &rh_lb,
2683 const wide_int &rh_ub) const
2684 {
2685 wide_int new_lb, new_ub, tmp;
2686 signop sign = TYPE_SIGN (type);
2687 unsigned prec = TYPE_PRECISION (type);
2688
2689 // Mod 0 is undefined.
2690 if (wi_zero_p (type, rh_lb, rh_ub))
2691 {
2692 r.set_varying (type);
2693 return;
2694 }
2695
2696 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
2697 new_ub = rh_ub - 1;
2698 if (sign == SIGNED)
2699 {
2700 tmp = -1 - rh_lb;
2701 new_ub = wi::smax (new_ub, tmp);
2702 }
2703
2704 if (sign == UNSIGNED)
2705 new_lb = wi::zero (prec);
2706 else
2707 {
2708 new_lb = -new_ub;
2709 tmp = lh_lb;
2710 if (wi::gts_p (tmp, 0))
2711 tmp = wi::zero (prec);
2712 new_lb = wi::smax (new_lb, tmp);
2713 }
2714 tmp = lh_ub;
2715 if (sign == SIGNED && wi::neg_p (tmp))
2716 tmp = wi::zero (prec);
2717 new_ub = wi::min (new_ub, tmp, sign);
2718
2719 value_range_with_overflow (r, type, new_lb, new_ub);
2720 }
2721
2722 bool
2723 operator_trunc_mod::op1_range (irange &r, tree type,
2724 const irange &lhs,
2725 const irange &) const
2726 {
2727 // PR 91029.
2728 signop sign = TYPE_SIGN (type);
2729 unsigned prec = TYPE_PRECISION (type);
2730 // (a % b) >= x && x > 0 , then a >= x.
2731 if (wi::gt_p (lhs.lower_bound (), 0, sign))
2732 {
2733 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
2734 return true;
2735 }
2736 // (a % b) <= x && x < 0 , then a <= x.
2737 if (wi::lt_p (lhs.upper_bound (), 0, sign))
2738 {
2739 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
2740 return true;
2741 }
2742 return false;
2743 }
2744
2745 bool
2746 operator_trunc_mod::op2_range (irange &r, tree type,
2747 const irange &lhs,
2748 const irange &) const
2749 {
2750 // PR 91029.
2751 signop sign = TYPE_SIGN (type);
2752 unsigned prec = TYPE_PRECISION (type);
2753 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
2754 // or b > x for unsigned.
2755 if (wi::gt_p (lhs.lower_bound (), 0, sign))
2756 {
2757 if (sign == SIGNED)
2758 r = value_range (type, wi::neg (lhs.lower_bound ()),
2759 lhs.lower_bound (), VR_ANTI_RANGE);
2760 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
2761 sign))
2762 r = value_range (type, lhs.lower_bound () + 1,
2763 wi::max_value (prec, sign));
2764 else
2765 return false;
2766 return true;
2767 }
2768 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
2769 if (wi::lt_p (lhs.upper_bound (), 0, sign))
2770 {
2771 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
2772 r = value_range (type, lhs.upper_bound (),
2773 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
2774 else
2775 return false;
2776 return true;
2777 }
2778 return false;
2779 }
2780
2781
2782 class operator_logical_not : public range_operator
2783 {
2784 public:
2785 virtual bool fold_range (irange &r, tree type,
2786 const irange &lh,
2787 const irange &rh) const;
2788 virtual bool op1_range (irange &r, tree type,
2789 const irange &lhs,
2790 const irange &op2) const;
2791 } op_logical_not;
2792
2793 // Folding a logical NOT, oddly enough, involves doing nothing on the
2794 // forward pass through. During the initial walk backwards, the
2795 // logical NOT reversed the desired outcome on the way back, so on the
2796 // way forward all we do is pass the range forward.
2797 //
2798 // b_2 = x_1 < 20
2799 // b_3 = !b_2
2800 // if (b_3)
2801 // to determine the TRUE branch, walking backward
2802 // if (b_3) if ([1,1])
2803 // b_3 = !b_2 [1,1] = ![0,0]
2804 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
2805 // which is the result we are looking for.. so.. pass it through.
2806
2807 bool
2808 operator_logical_not::fold_range (irange &r, tree type,
2809 const irange &lh,
2810 const irange &rh ATTRIBUTE_UNUSED) const
2811 {
2812 if (empty_range_varying (r, type, lh, rh))
2813 return true;
2814
2815 r = lh;
2816 if (!lh.varying_p () && !lh.undefined_p ())
2817 r.invert ();
2818
2819 return true;
2820 }
2821
2822 bool
2823 operator_logical_not::op1_range (irange &r,
2824 tree type,
2825 const irange &lhs,
2826 const irange &op2) const
2827 {
2828 // Logical NOT is involutary...do it again.
2829 return fold_range (r, type, lhs, op2);
2830 }
2831
2832
2833 class operator_bitwise_not : public range_operator
2834 {
2835 public:
2836 virtual bool fold_range (irange &r, tree type,
2837 const irange &lh,
2838 const irange &rh) const;
2839 virtual bool op1_range (irange &r, tree type,
2840 const irange &lhs,
2841 const irange &op2) const;
2842 } op_bitwise_not;
2843
2844 bool
2845 operator_bitwise_not::fold_range (irange &r, tree type,
2846 const irange &lh,
2847 const irange &rh) const
2848 {
2849 if (empty_range_varying (r, type, lh, rh))
2850 return true;
2851
2852 if (types_compatible_p (type, boolean_type_node))
2853 return op_logical_not.fold_range (r, type, lh, rh);
2854
2855 // ~X is simply -1 - X.
2856 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
2857 wi::minus_one (TYPE_PRECISION (type)));
2858 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type, minusone,
2859 lh);
2860 }
2861
2862 bool
2863 operator_bitwise_not::op1_range (irange &r, tree type,
2864 const irange &lhs,
2865 const irange &op2) const
2866 {
2867 if (types_compatible_p (type, boolean_type_node))
2868 return op_logical_not.op1_range (r, type, lhs, op2);
2869
2870 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
2871 return fold_range (r, type, lhs, op2);
2872 }
2873
2874
2875 class operator_cst : public range_operator
2876 {
2877 public:
2878 virtual bool fold_range (irange &r, tree type,
2879 const irange &op1,
2880 const irange &op2) const;
2881 } op_integer_cst;
2882
2883 bool
2884 operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2885 const irange &lh,
2886 const irange &rh ATTRIBUTE_UNUSED) const
2887 {
2888 r = lh;
2889 return true;
2890 }
2891
2892
2893 class operator_identity : public range_operator
2894 {
2895 public:
2896 virtual bool fold_range (irange &r, tree type,
2897 const irange &op1,
2898 const irange &op2) const;
2899 virtual bool op1_range (irange &r, tree type,
2900 const irange &lhs,
2901 const irange &op2) const;
2902 } op_identity;
2903
2904 bool
2905 operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2906 const irange &lh,
2907 const irange &rh ATTRIBUTE_UNUSED) const
2908 {
2909 r = lh;
2910 return true;
2911 }
2912
2913 bool
2914 operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
2915 const irange &lhs,
2916 const irange &op2 ATTRIBUTE_UNUSED) const
2917 {
2918 r = lhs;
2919 return true;
2920 }
2921
2922
2923 class operator_unknown : public range_operator
2924 {
2925 public:
2926 virtual bool fold_range (irange &r, tree type,
2927 const irange &op1,
2928 const irange &op2) const;
2929 } op_unknown;
2930
2931 bool
2932 operator_unknown::fold_range (irange &r, tree type,
2933 const irange &lh ATTRIBUTE_UNUSED,
2934 const irange &rh ATTRIBUTE_UNUSED) const
2935 {
2936 r.set_varying (type);
2937 return true;
2938 }
2939
2940
2941 class operator_abs : public range_operator
2942 {
2943 public:
2944 virtual void wi_fold (irange &r, tree type,
2945 const wide_int &lh_lb,
2946 const wide_int &lh_ub,
2947 const wide_int &rh_lb,
2948 const wide_int &rh_ub) const;
2949 virtual bool op1_range (irange &r, tree type,
2950 const irange &lhs,
2951 const irange &op2) const;
2952 } op_abs;
2953
2954 void
2955 operator_abs::wi_fold (irange &r, tree type,
2956 const wide_int &lh_lb, const wide_int &lh_ub,
2957 const wide_int &rh_lb ATTRIBUTE_UNUSED,
2958 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
2959 {
2960 wide_int min, max;
2961 signop sign = TYPE_SIGN (type);
2962 unsigned prec = TYPE_PRECISION (type);
2963
2964 // Pass through LH for the easy cases.
2965 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
2966 {
2967 r = int_range<1> (type, lh_lb, lh_ub);
2968 return;
2969 }
2970
2971 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
2972 // a useful range.
2973 wide_int min_value = wi::min_value (prec, sign);
2974 wide_int max_value = wi::max_value (prec, sign);
2975 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
2976 {
2977 r.set_varying (type);
2978 return;
2979 }
2980
2981 // ABS_EXPR may flip the range around, if the original range
2982 // included negative values.
2983 if (wi::eq_p (lh_lb, min_value))
2984 {
2985 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
2986 // returned [-MIN,-MIN] so this preserves that behaviour. PR37078
2987 if (wi::eq_p (lh_ub, min_value))
2988 {
2989 r = int_range<1> (type, min_value, min_value);
2990 return;
2991 }
2992 min = max_value;
2993 }
2994 else
2995 min = wi::abs (lh_lb);
2996
2997 if (wi::eq_p (lh_ub, min_value))
2998 max = max_value;
2999 else
3000 max = wi::abs (lh_ub);
3001
3002 // If the range contains zero then we know that the minimum value in the
3003 // range will be zero.
3004 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
3005 {
3006 if (wi::gt_p (min, max, sign))
3007 max = min;
3008 min = wi::zero (prec);
3009 }
3010 else
3011 {
3012 // If the range was reversed, swap MIN and MAX.
3013 if (wi::gt_p (min, max, sign))
3014 std::swap (min, max);
3015 }
3016
3017 // If the new range has its limits swapped around (MIN > MAX), then
3018 // the operation caused one of them to wrap around. The only thing
3019 // we know is that the result is positive.
3020 if (wi::gt_p (min, max, sign))
3021 {
3022 min = wi::zero (prec);
3023 max = max_value;
3024 }
3025 r = int_range<1> (type, min, max);
3026 }
3027
3028 bool
3029 operator_abs::op1_range (irange &r, tree type,
3030 const irange &lhs,
3031 const irange &op2) const
3032 {
3033 if (empty_range_varying (r, type, lhs, op2))
3034 return true;
3035 if (TYPE_UNSIGNED (type))
3036 {
3037 r = lhs;
3038 return true;
3039 }
3040 // Start with the positives because negatives are an impossible result.
3041 int_range_max positives = range_positives (type);
3042 positives.intersect (lhs);
3043 r = positives;
3044 // Then add the negative of each pair:
3045 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
3046 for (unsigned i = 0; i < positives.num_pairs (); ++i)
3047 r.union_ (int_range<1> (type,
3048 -positives.upper_bound (i),
3049 -positives.lower_bound (i)));
3050 return true;
3051 }
3052
3053
3054 class operator_absu : public range_operator
3055 {
3056 public:
3057 virtual void wi_fold (irange &r, tree type,
3058 const wide_int &lh_lb, const wide_int &lh_ub,
3059 const wide_int &rh_lb, const wide_int &rh_ub) const;
3060 } op_absu;
3061
3062 void
3063 operator_absu::wi_fold (irange &r, tree type,
3064 const wide_int &lh_lb, const wide_int &lh_ub,
3065 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3066 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3067 {
3068 wide_int new_lb, new_ub;
3069
3070 // Pass through VR0 the easy cases.
3071 if (wi::ges_p (lh_lb, 0))
3072 {
3073 new_lb = lh_lb;
3074 new_ub = lh_ub;
3075 }
3076 else
3077 {
3078 new_lb = wi::abs (lh_lb);
3079 new_ub = wi::abs (lh_ub);
3080
3081 // If the range contains zero then we know that the minimum
3082 // value in the range will be zero.
3083 if (wi::ges_p (lh_ub, 0))
3084 {
3085 if (wi::gtu_p (new_lb, new_ub))
3086 new_ub = new_lb;
3087 new_lb = wi::zero (TYPE_PRECISION (type));
3088 }
3089 else
3090 std::swap (new_lb, new_ub);
3091 }
3092
3093 gcc_checking_assert (TYPE_UNSIGNED (type));
3094 r = int_range<1> (type, new_lb, new_ub);
3095 }
3096
3097
3098 class operator_negate : public range_operator
3099 {
3100 public:
3101 virtual bool fold_range (irange &r, tree type,
3102 const irange &op1,
3103 const irange &op2) const;
3104 virtual bool op1_range (irange &r, tree type,
3105 const irange &lhs,
3106 const irange &op2) const;
3107 } op_negate;
3108
3109 bool
3110 operator_negate::fold_range (irange &r, tree type,
3111 const irange &lh,
3112 const irange &rh) const
3113 {
3114 if (empty_range_varying (r, type, lh, rh))
3115 return true;
3116 // -X is simply 0 - X.
3117 return range_op_handler (MINUS_EXPR, type)->fold_range (r, type,
3118 range_zero (type),
3119 lh);
3120 }
3121
3122 bool
3123 operator_negate::op1_range (irange &r, tree type,
3124 const irange &lhs,
3125 const irange &op2) const
3126 {
3127 // NEGATE is involutory.
3128 return fold_range (r, type, lhs, op2);
3129 }
3130
3131
3132 class operator_addr_expr : public range_operator
3133 {
3134 public:
3135 virtual bool fold_range (irange &r, tree type,
3136 const irange &op1,
3137 const irange &op2) const;
3138 virtual bool op1_range (irange &r, tree type,
3139 const irange &lhs,
3140 const irange &op2) const;
3141 } op_addr;
3142
3143 bool
3144 operator_addr_expr::fold_range (irange &r, tree type,
3145 const irange &lh,
3146 const irange &rh) const
3147 {
3148 if (empty_range_varying (r, type, lh, rh))
3149 return true;
3150
3151 // Return a non-null pointer of the LHS type (passed in op2).
3152 if (lh.zero_p ())
3153 r = range_zero (type);
3154 else if (!lh.contains_p (build_zero_cst (lh.type ())))
3155 r = range_nonzero (type);
3156 else
3157 r.set_varying (type);
3158 return true;
3159 }
3160
3161 bool
3162 operator_addr_expr::op1_range (irange &r, tree type,
3163 const irange &lhs,
3164 const irange &op2) const
3165 {
3166 return operator_addr_expr::fold_range (r, type, lhs, op2);
3167 }
3168
3169
3170 class pointer_plus_operator : public range_operator
3171 {
3172 public:
3173 virtual void wi_fold (irange &r, tree type,
3174 const wide_int &lh_lb,
3175 const wide_int &lh_ub,
3176 const wide_int &rh_lb,
3177 const wide_int &rh_ub) const;
3178 } op_pointer_plus;
3179
3180 void
3181 pointer_plus_operator::wi_fold (irange &r, tree type,
3182 const wide_int &lh_lb,
3183 const wide_int &lh_ub,
3184 const wide_int &rh_lb,
3185 const wide_int &rh_ub) const
3186 {
3187 // Check for [0,0] + const, and simply return the const.
3188 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
3189 {
3190 tree val = wide_int_to_tree (type, rh_lb);
3191 r.set (val, val);
3192 return;
3193 }
3194
3195 // For pointer types, we are really only interested in asserting
3196 // whether the expression evaluates to non-NULL.
3197 //
3198 // With -fno-delete-null-pointer-checks we need to be more
3199 // conservative. As some object might reside at address 0,
3200 // then some offset could be added to it and the same offset
3201 // subtracted again and the result would be NULL.
3202 // E.g.
3203 // static int a[12]; where &a[0] is NULL and
3204 // ptr = &a[6];
3205 // ptr -= 6;
3206 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
3207 // where the first range doesn't include zero and the second one
3208 // doesn't either. As the second operand is sizetype (unsigned),
3209 // consider all ranges where the MSB could be set as possible
3210 // subtractions where the result might be NULL.
3211 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
3212 || !wi_includes_zero_p (type, rh_lb, rh_ub))
3213 && !TYPE_OVERFLOW_WRAPS (type)
3214 && (flag_delete_null_pointer_checks
3215 || !wi::sign_mask (rh_ub)))
3216 r = range_nonzero (type);
3217 else if (lh_lb == lh_ub && lh_lb == 0
3218 && rh_lb == rh_ub && rh_lb == 0)
3219 r = range_zero (type);
3220 else
3221 r.set_varying (type);
3222 }
3223
3224
3225 class pointer_min_max_operator : public range_operator
3226 {
3227 public:
3228 virtual void wi_fold (irange & r, tree type,
3229 const wide_int &lh_lb, const wide_int &lh_ub,
3230 const wide_int &rh_lb, const wide_int &rh_ub) const;
3231 } op_ptr_min_max;
3232
3233 void
3234 pointer_min_max_operator::wi_fold (irange &r, tree type,
3235 const wide_int &lh_lb,
3236 const wide_int &lh_ub,
3237 const wide_int &rh_lb,
3238 const wide_int &rh_ub) const
3239 {
3240 // For MIN/MAX expressions with pointers, we only care about
3241 // nullness. If both are non null, then the result is nonnull.
3242 // If both are null, then the result is null. Otherwise they
3243 // are varying.
3244 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3245 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3246 r = range_nonzero (type);
3247 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3248 r = range_zero (type);
3249 else
3250 r.set_varying (type);
3251 }
3252
3253
3254 class pointer_and_operator : public range_operator
3255 {
3256 public:
3257 virtual void wi_fold (irange &r, tree type,
3258 const wide_int &lh_lb, const wide_int &lh_ub,
3259 const wide_int &rh_lb, const wide_int &rh_ub) const;
3260 } op_pointer_and;
3261
3262 void
3263 pointer_and_operator::wi_fold (irange &r, tree type,
3264 const wide_int &lh_lb,
3265 const wide_int &lh_ub,
3266 const wide_int &rh_lb ATTRIBUTE_UNUSED,
3267 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
3268 {
3269 // For pointer types, we are really only interested in asserting
3270 // whether the expression evaluates to non-NULL.
3271 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
3272 r = range_zero (type);
3273 else
3274 r.set_varying (type);
3275 }
3276
3277
3278 class pointer_or_operator : public range_operator
3279 {
3280 public:
3281 virtual bool op1_range (irange &r, tree type,
3282 const irange &lhs,
3283 const irange &op2) const;
3284 virtual bool op2_range (irange &r, tree type,
3285 const irange &lhs,
3286 const irange &op1) const;
3287 virtual void wi_fold (irange &r, tree type,
3288 const wide_int &lh_lb, const wide_int &lh_ub,
3289 const wide_int &rh_lb, const wide_int &rh_ub) const;
3290 } op_pointer_or;
3291
3292 bool
3293 pointer_or_operator::op1_range (irange &r, tree type,
3294 const irange &lhs,
3295 const irange &op2 ATTRIBUTE_UNUSED) const
3296 {
3297 if (lhs.zero_p ())
3298 {
3299 tree zero = build_zero_cst (type);
3300 r = int_range<1> (zero, zero);
3301 return true;
3302 }
3303 r.set_varying (type);
3304 return true;
3305 }
3306
3307 bool
3308 pointer_or_operator::op2_range (irange &r, tree type,
3309 const irange &lhs,
3310 const irange &op1) const
3311 {
3312 return pointer_or_operator::op1_range (r, type, lhs, op1);
3313 }
3314
3315 void
3316 pointer_or_operator::wi_fold (irange &r, tree type,
3317 const wide_int &lh_lb,
3318 const wide_int &lh_ub,
3319 const wide_int &rh_lb,
3320 const wide_int &rh_ub) const
3321 {
3322 // For pointer types, we are really only interested in asserting
3323 // whether the expression evaluates to non-NULL.
3324 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
3325 && !wi_includes_zero_p (type, rh_lb, rh_ub))
3326 r = range_nonzero (type);
3327 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
3328 r = range_zero (type);
3329 else
3330 r.set_varying (type);
3331 }
3332 \f
3333 // This implements the range operator tables as local objects in this file.
3334
3335 class range_op_table
3336 {
3337 public:
3338 inline range_operator *operator[] (enum tree_code code);
3339 protected:
3340 void set (enum tree_code code, range_operator &op);
3341 private:
3342 range_operator *m_range_tree[MAX_TREE_CODES];
3343 };
3344
3345 // Return a pointer to the range_operator instance, if there is one
3346 // associated with tree_code CODE.
3347
3348 range_operator *
3349 range_op_table::operator[] (enum tree_code code)
3350 {
3351 gcc_checking_assert (code > 0 && code < MAX_TREE_CODES);
3352 return m_range_tree[code];
3353 }
3354
3355 // Add OP to the handler table for CODE.
3356
3357 void
3358 range_op_table::set (enum tree_code code, range_operator &op)
3359 {
3360 gcc_checking_assert (m_range_tree[code] == NULL);
3361 m_range_tree[code] = &op;
3362 }
3363
3364 // Instantiate a range op table for integral operations.
3365
3366 class integral_table : public range_op_table
3367 {
3368 public:
3369 integral_table ();
3370 } integral_tree_table;
3371
3372 integral_table::integral_table ()
3373 {
3374 set (EQ_EXPR, op_equal);
3375 set (NE_EXPR, op_not_equal);
3376 set (LT_EXPR, op_lt);
3377 set (LE_EXPR, op_le);
3378 set (GT_EXPR, op_gt);
3379 set (GE_EXPR, op_ge);
3380 set (PLUS_EXPR, op_plus);
3381 set (MINUS_EXPR, op_minus);
3382 set (MIN_EXPR, op_min);
3383 set (MAX_EXPR, op_max);
3384 set (MULT_EXPR, op_mult);
3385 set (TRUNC_DIV_EXPR, op_trunc_div);
3386 set (FLOOR_DIV_EXPR, op_floor_div);
3387 set (ROUND_DIV_EXPR, op_round_div);
3388 set (CEIL_DIV_EXPR, op_ceil_div);
3389 set (EXACT_DIV_EXPR, op_exact_div);
3390 set (LSHIFT_EXPR, op_lshift);
3391 set (RSHIFT_EXPR, op_rshift);
3392 set (NOP_EXPR, op_convert);
3393 set (CONVERT_EXPR, op_convert);
3394 set (TRUTH_AND_EXPR, op_logical_and);
3395 set (BIT_AND_EXPR, op_bitwise_and);
3396 set (TRUTH_OR_EXPR, op_logical_or);
3397 set (BIT_IOR_EXPR, op_bitwise_or);
3398 set (BIT_XOR_EXPR, op_bitwise_xor);
3399 set (TRUNC_MOD_EXPR, op_trunc_mod);
3400 set (TRUTH_NOT_EXPR, op_logical_not);
3401 set (BIT_NOT_EXPR, op_bitwise_not);
3402 set (INTEGER_CST, op_integer_cst);
3403 set (SSA_NAME, op_identity);
3404 set (PAREN_EXPR, op_identity);
3405 set (OBJ_TYPE_REF, op_identity);
3406 set (IMAGPART_EXPR, op_unknown);
3407 set (POINTER_DIFF_EXPR, op_unknown);
3408 set (ABS_EXPR, op_abs);
3409 set (ABSU_EXPR, op_absu);
3410 set (NEGATE_EXPR, op_negate);
3411 set (ADDR_EXPR, op_addr);
3412 }
3413
3414 // Instantiate a range op table for pointer operations.
3415
3416 class pointer_table : public range_op_table
3417 {
3418 public:
3419 pointer_table ();
3420 } pointer_tree_table;
3421
3422 pointer_table::pointer_table ()
3423 {
3424 set (BIT_AND_EXPR, op_pointer_and);
3425 set (BIT_IOR_EXPR, op_pointer_or);
3426 set (MIN_EXPR, op_ptr_min_max);
3427 set (MAX_EXPR, op_ptr_min_max);
3428 set (POINTER_PLUS_EXPR, op_pointer_plus);
3429
3430 set (EQ_EXPR, op_equal);
3431 set (NE_EXPR, op_not_equal);
3432 set (LT_EXPR, op_lt);
3433 set (LE_EXPR, op_le);
3434 set (GT_EXPR, op_gt);
3435 set (GE_EXPR, op_ge);
3436 set (SSA_NAME, op_identity);
3437 set (INTEGER_CST, op_integer_cst);
3438 set (ADDR_EXPR, op_addr);
3439 set (NOP_EXPR, op_convert);
3440 set (CONVERT_EXPR, op_convert);
3441
3442 set (BIT_NOT_EXPR, op_bitwise_not);
3443 set (BIT_XOR_EXPR, op_bitwise_xor);
3444 }
3445
3446 // The tables are hidden and accessed via a simple extern function.
3447
3448 range_operator *
3449 range_op_handler (enum tree_code code, tree type)
3450 {
3451 // First check if there is a pointer specialization.
3452 if (POINTER_TYPE_P (type))
3453 return pointer_tree_table[code];
3454 if (INTEGRAL_TYPE_P (type))
3455 return integral_tree_table[code];
3456 return NULL;
3457 }
3458
3459 // Cast the range in R to TYPE.
3460
3461 void
3462 range_cast (irange &r, tree type)
3463 {
3464 int_range_max tmp = r;
3465 range_operator *op = range_op_handler (CONVERT_EXPR, type);
3466 // Call op_convert, if it fails, the result is varying.
3467 if (!op->fold_range (r, type, tmp, int_range<1> (type)))
3468 r.set_varying (type);
3469 }
3470
3471 #if CHECKING_P
3472 #include "selftest.h"
3473
3474 namespace selftest
3475 {
3476 #define INT(N) build_int_cst (integer_type_node, (N))
3477 #define UINT(N) build_int_cstu (unsigned_type_node, (N))
3478 #define INT16(N) build_int_cst (short_integer_type_node, (N))
3479 #define UINT16(N) build_int_cstu (short_unsigned_type_node, (N))
3480 #define SCHAR(N) build_int_cst (signed_char_type_node, (N))
3481 #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N))
3482
3483 static void
3484 range_op_cast_tests ()
3485 {
3486 int_range<1> r0, r1, r2, rold;
3487 r0.set_varying (integer_type_node);
3488 tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ());
3489
3490 // If a range is in any way outside of the range for the converted
3491 // to range, default to the range for the new type.
3492 r0.set_varying (short_integer_type_node);
3493 tree minshort = wide_int_to_tree (short_integer_type_node, r0.lower_bound ());
3494 tree maxshort = wide_int_to_tree (short_integer_type_node, r0.upper_bound ());
3495 if (TYPE_PRECISION (TREE_TYPE (maxint))
3496 > TYPE_PRECISION (short_integer_type_node))
3497 {
3498 r1 = int_range<1> (integer_zero_node, maxint);
3499 range_cast (r1, short_integer_type_node);
3500 ASSERT_TRUE (r1.lower_bound () == wi::to_wide (minshort)
3501 && r1.upper_bound() == wi::to_wide (maxshort));
3502 }
3503
3504 // (unsigned char)[-5,-1] => [251,255].
3505 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (-1));
3506 range_cast (r0, unsigned_char_type_node);
3507 ASSERT_TRUE (r0 == int_range<1> (UCHAR (251), UCHAR (255)));
3508 range_cast (r0, signed_char_type_node);
3509 ASSERT_TRUE (r0 == rold);
3510
3511 // (signed char)[15, 150] => [-128,-106][15,127].
3512 r0 = rold = int_range<1> (UCHAR (15), UCHAR (150));
3513 range_cast (r0, signed_char_type_node);
3514 r1 = int_range<1> (SCHAR (15), SCHAR (127));
3515 r2 = int_range<1> (SCHAR (-128), SCHAR (-106));
3516 r1.union_ (r2);
3517 ASSERT_TRUE (r1 == r0);
3518 range_cast (r0, unsigned_char_type_node);
3519 ASSERT_TRUE (r0 == rold);
3520
3521 // (unsigned char)[-5, 5] => [0,5][251,255].
3522 r0 = rold = int_range<1> (SCHAR (-5), SCHAR (5));
3523 range_cast (r0, unsigned_char_type_node);
3524 r1 = int_range<1> (UCHAR (251), UCHAR (255));
3525 r2 = int_range<1> (UCHAR (0), UCHAR (5));
3526 r1.union_ (r2);
3527 ASSERT_TRUE (r0 == r1);
3528 range_cast (r0, signed_char_type_node);
3529 ASSERT_TRUE (r0 == rold);
3530
3531 // (unsigned char)[-5,5] => [0,5][251,255].
3532 r0 = int_range<1> (INT (-5), INT (5));
3533 range_cast (r0, unsigned_char_type_node);
3534 r1 = int_range<1> (UCHAR (0), UCHAR (5));
3535 r1.union_ (int_range<1> (UCHAR (251), UCHAR (255)));
3536 ASSERT_TRUE (r0 == r1);
3537
3538 // (unsigned char)[5U,1974U] => [0,255].
3539 r0 = int_range<1> (UINT (5), UINT (1974));
3540 range_cast (r0, unsigned_char_type_node);
3541 ASSERT_TRUE (r0 == int_range<1> (UCHAR (0), UCHAR (255)));
3542 range_cast (r0, integer_type_node);
3543 // Going to a wider range should not sign extend.
3544 ASSERT_TRUE (r0 == int_range<1> (INT (0), INT (255)));
3545
3546 // (unsigned char)[-350,15] => [0,255].
3547 r0 = int_range<1> (INT (-350), INT (15));
3548 range_cast (r0, unsigned_char_type_node);
3549 ASSERT_TRUE (r0 == (int_range<1>
3550 (TYPE_MIN_VALUE (unsigned_char_type_node),
3551 TYPE_MAX_VALUE (unsigned_char_type_node))));
3552
3553 // Casting [-120,20] from signed char to unsigned short.
3554 // => [0, 20][0xff88, 0xffff].
3555 r0 = int_range<1> (SCHAR (-120), SCHAR (20));
3556 range_cast (r0, short_unsigned_type_node);
3557 r1 = int_range<1> (UINT16 (0), UINT16 (20));
3558 r2 = int_range<1> (UINT16 (0xff88), UINT16 (0xffff));
3559 r1.union_ (r2);
3560 ASSERT_TRUE (r0 == r1);
3561 // A truncating cast back to signed char will work because [-120, 20]
3562 // is representable in signed char.
3563 range_cast (r0, signed_char_type_node);
3564 ASSERT_TRUE (r0 == int_range<1> (SCHAR (-120), SCHAR (20)));
3565
3566 // unsigned char -> signed short
3567 // (signed short)[(unsigned char)25, (unsigned char)250]
3568 // => [(signed short)25, (signed short)250]
3569 r0 = rold = int_range<1> (UCHAR (25), UCHAR (250));
3570 range_cast (r0, short_integer_type_node);
3571 r1 = int_range<1> (INT16 (25), INT16 (250));
3572 ASSERT_TRUE (r0 == r1);
3573 range_cast (r0, unsigned_char_type_node);
3574 ASSERT_TRUE (r0 == rold);
3575
3576 // Test casting a wider signed [-MIN,MAX] to a nar`rower unsigned.
3577 r0 = int_range<1> (TYPE_MIN_VALUE (long_long_integer_type_node),
3578 TYPE_MAX_VALUE (long_long_integer_type_node));
3579 range_cast (r0, short_unsigned_type_node);
3580 r1 = int_range<1> (TYPE_MIN_VALUE (short_unsigned_type_node),
3581 TYPE_MAX_VALUE (short_unsigned_type_node));
3582 ASSERT_TRUE (r0 == r1);
3583
3584 // Casting NONZERO to a narrower type will wrap/overflow so
3585 // it's just the entire range for the narrower type.
3586 //
3587 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
3588 // is outside of the range of a smaller range, return the full
3589 // smaller range.
3590 if (TYPE_PRECISION (integer_type_node)
3591 > TYPE_PRECISION (short_integer_type_node))
3592 {
3593 r0 = range_nonzero (integer_type_node);
3594 range_cast (r0, short_integer_type_node);
3595 r1 = int_range<1> (TYPE_MIN_VALUE (short_integer_type_node),
3596 TYPE_MAX_VALUE (short_integer_type_node));
3597 ASSERT_TRUE (r0 == r1);
3598 }
3599
3600 // Casting NONZERO from a narrower signed to a wider signed.
3601 //
3602 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
3603 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
3604 r0 = range_nonzero (short_integer_type_node);
3605 range_cast (r0, integer_type_node);
3606 r1 = int_range<1> (INT (-32768), INT (-1));
3607 r2 = int_range<1> (INT (1), INT (32767));
3608 r1.union_ (r2);
3609 ASSERT_TRUE (r0 == r1);
3610 }
3611
3612 static void
3613 range_op_lshift_tests ()
3614 {
3615 // Test that 0x808.... & 0x8.... still contains 0x8....
3616 // for a large set of numbers.
3617 {
3618 int_range_max res;
3619 tree big_type = long_long_unsigned_type_node;
3620 // big_num = 0x808,0000,0000,0000
3621 tree big_num = fold_build2 (LSHIFT_EXPR, big_type,
3622 build_int_cst (big_type, 0x808),
3623 build_int_cst (big_type, 48));
3624 op_bitwise_and.fold_range (res, big_type,
3625 int_range <1> (big_type),
3626 int_range <1> (big_num, big_num));
3627 // val = 0x8,0000,0000,0000
3628 tree val = fold_build2 (LSHIFT_EXPR, big_type,
3629 build_int_cst (big_type, 0x8),
3630 build_int_cst (big_type, 48));
3631 ASSERT_TRUE (res.contains_p (val));
3632 }
3633
3634 if (TYPE_PRECISION (unsigned_type_node) > 31)
3635 {
3636 // unsigned VARYING = op1 << 1 should be VARYING.
3637 int_range<2> lhs (unsigned_type_node);
3638 int_range<2> shift (INT (1), INT (1));
3639 int_range_max op1;
3640 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
3641 ASSERT_TRUE (op1.varying_p ());
3642
3643 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
3644 int_range<2> zero (UINT (0), UINT (0));
3645 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
3646 ASSERT_TRUE (op1.num_pairs () == 2);
3647 // Remove the [0,0] range.
3648 op1.intersect (zero);
3649 ASSERT_TRUE (op1.num_pairs () == 1);
3650 // op1 << 1 should be [0x8000,0x8000] << 1,
3651 // which should result in [0,0].
3652 int_range_max result;
3653 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
3654 ASSERT_TRUE (result == zero);
3655 }
3656 // signed VARYING = op1 << 1 should be VARYING.
3657 if (TYPE_PRECISION (integer_type_node) > 31)
3658 {
3659 // unsigned VARYING = op1 << 1 hould be VARYING.
3660 int_range<2> lhs (integer_type_node);
3661 int_range<2> shift (INT (1), INT (1));
3662 int_range_max op1;
3663 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
3664 ASSERT_TRUE (op1.varying_p ());
3665
3666 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
3667 int_range<2> zero (INT (0), INT (0));
3668 op_lshift.op1_range (op1, integer_type_node, zero, shift);
3669 ASSERT_TRUE (op1.num_pairs () == 2);
3670 // Remove the [0,0] range.
3671 op1.intersect (zero);
3672 ASSERT_TRUE (op1.num_pairs () == 1);
3673 // op1 << 1 shuould be [0x8000,0x8000] << 1,
3674 // which should result in [0,0].
3675 int_range_max result;
3676 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
3677 ASSERT_TRUE (result == zero);
3678 }
3679 }
3680
3681 static void
3682 range_op_rshift_tests ()
3683 {
3684 // unsigned: [3, MAX] = OP1 >> 1
3685 {
3686 int_range_max lhs (build_int_cst (unsigned_type_node, 3),
3687 TYPE_MAX_VALUE (unsigned_type_node));
3688 int_range_max one (build_one_cst (unsigned_type_node),
3689 build_one_cst (unsigned_type_node));
3690 int_range_max op1;
3691 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
3692 ASSERT_FALSE (op1.contains_p (UINT (3)));
3693 }
3694
3695 // signed: [3, MAX] = OP1 >> 1
3696 {
3697 int_range_max lhs (INT (3), TYPE_MAX_VALUE (integer_type_node));
3698 int_range_max one (INT (1), INT (1));
3699 int_range_max op1;
3700 op_rshift.op1_range (op1, integer_type_node, lhs, one);
3701 ASSERT_FALSE (op1.contains_p (INT (-2)));
3702 }
3703
3704 // This is impossible, so OP1 should be [].
3705 // signed: [MIN, MIN] = OP1 >> 1
3706 {
3707 int_range_max lhs (TYPE_MIN_VALUE (integer_type_node),
3708 TYPE_MIN_VALUE (integer_type_node));
3709 int_range_max one (INT (1), INT (1));
3710 int_range_max op1;
3711 op_rshift.op1_range (op1, integer_type_node, lhs, one);
3712 ASSERT_TRUE (op1.undefined_p ());
3713 }
3714
3715 // signed: ~[-1] = OP1 >> 31
3716 if (TYPE_PRECISION (integer_type_node) > 31)
3717 {
3718 int_range_max lhs (INT (-1), INT (-1), VR_ANTI_RANGE);
3719 int_range_max shift (INT (31), INT (31));
3720 int_range_max op1;
3721 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
3722 int_range_max negatives = range_negatives (integer_type_node);
3723 negatives.intersect (op1);
3724 ASSERT_TRUE (negatives.undefined_p ());
3725 }
3726 }
3727
3728 static void
3729 range_op_bitwise_and_tests ()
3730 {
3731 int_range_max res;
3732 tree min = vrp_val_min (integer_type_node);
3733 tree max = vrp_val_max (integer_type_node);
3734 tree tiny = fold_build2 (PLUS_EXPR, integer_type_node, min,
3735 build_one_cst (integer_type_node));
3736 int_range_max i1 (tiny, max);
3737 int_range_max i2 (build_int_cst (integer_type_node, 255),
3738 build_int_cst (integer_type_node, 255));
3739
3740 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
3741 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
3742 ASSERT_TRUE (res == int_range<1> (integer_type_node));
3743
3744 // VARYING = OP1 & 255: OP1 is VARYING
3745 i1 = int_range<1> (integer_type_node);
3746 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
3747 ASSERT_TRUE (res == int_range<1> (integer_type_node));
3748 }
3749
3750 void
3751 range_op_tests ()
3752 {
3753 range_op_rshift_tests ();
3754 range_op_lshift_tests ();
3755 range_op_bitwise_and_tests ();
3756 range_op_cast_tests ();
3757 }
3758
3759 } // namespace selftest
3760
3761 #endif // CHECKING_P