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