libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / range-op-ptr.cc
blob24e206c00cddd9382837b4af8d7243812dec5a9b
1 /* Code for range operators.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "insn-codes.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
36 #include "flags.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "calls.h"
40 #include "cfganal.h"
41 #include "gimple-iterator.h"
42 #include "gimple-fold.h"
43 #include "tree-eh.h"
44 #include "gimple-walk.h"
45 #include "tree-cfg.h"
46 #include "wide-int.h"
47 #include "value-relation.h"
48 #include "range-op.h"
49 #include "tree-ssa-ccp.h"
50 #include "range-op-mixed.h"
52 bool
53 range_operator::fold_range (prange &, tree, const prange &, const prange &,
54 relation_trio) const
56 return false;
59 bool
60 range_operator::fold_range (prange &, tree, const prange &, const irange &,
61 relation_trio) const
63 return false;
66 bool
67 range_operator::fold_range (irange &, tree, const prange &, const prange &,
68 relation_trio) const
70 return false;
73 bool
74 range_operator::fold_range (prange &, tree, const irange &, const prange &,
75 relation_trio) const
77 return false;
80 bool
81 range_operator::fold_range (irange &, tree, const prange &, const irange &,
82 relation_trio) const
84 return false;
87 bool
88 range_operator::op1_op2_relation_effect (prange &, tree,
89 const prange &,
90 const prange &,
91 relation_kind) const
93 return false;
96 bool
97 range_operator::op1_op2_relation_effect (prange &, tree,
98 const prange &,
99 const irange &,
100 relation_kind) const
102 return false;
105 bool
106 range_operator::op1_op2_relation_effect (irange &, tree,
107 const prange &,
108 const prange &,
109 relation_kind) const
111 return false;
114 bool
115 range_operator::op1_op2_relation_effect (prange &, tree,
116 const irange &,
117 const prange &,
118 relation_kind) const
120 return false;
123 bool
124 range_operator::op1_op2_relation_effect (irange &, tree,
125 const prange &,
126 const irange &,
127 relation_kind) const
129 return false;
132 bool
133 range_operator::op1_range (prange &, tree,
134 const prange &lhs ATTRIBUTE_UNUSED,
135 const prange &op2 ATTRIBUTE_UNUSED,
136 relation_trio) const
138 return false;
141 bool
142 range_operator::op1_range (prange &, tree,
143 const irange &lhs ATTRIBUTE_UNUSED,
144 const prange &op2 ATTRIBUTE_UNUSED,
145 relation_trio) const
147 return false;
150 bool
151 range_operator::op1_range (prange &, tree,
152 const prange &lhs ATTRIBUTE_UNUSED,
153 const irange &op2 ATTRIBUTE_UNUSED,
154 relation_trio) const
156 return false;
159 bool
160 range_operator::op1_range (irange &, tree,
161 const prange &lhs ATTRIBUTE_UNUSED,
162 const irange &op2 ATTRIBUTE_UNUSED,
163 relation_trio) const
165 return false;
168 bool
169 range_operator::op2_range (prange &, tree,
170 const irange &lhs ATTRIBUTE_UNUSED,
171 const prange &op1 ATTRIBUTE_UNUSED,
172 relation_trio) const
174 return false;
177 bool
178 range_operator::op2_range (irange &, tree,
179 const prange &lhs ATTRIBUTE_UNUSED,
180 const prange &op1 ATTRIBUTE_UNUSED,
181 relation_trio) const
183 return false;
186 relation_kind
187 range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
188 const prange &op1 ATTRIBUTE_UNUSED,
189 const prange &op2 ATTRIBUTE_UNUSED) const
191 return VREL_VARYING;
194 relation_kind
195 range_operator::lhs_op1_relation (const prange &lhs ATTRIBUTE_UNUSED,
196 const irange &op1 ATTRIBUTE_UNUSED,
197 const irange &op2 ATTRIBUTE_UNUSED,
198 relation_kind rel ATTRIBUTE_UNUSED) const
200 return VREL_VARYING;
203 relation_kind
204 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
205 const prange &op1 ATTRIBUTE_UNUSED,
206 const prange &op2 ATTRIBUTE_UNUSED,
207 relation_kind rel ATTRIBUTE_UNUSED) const
209 return VREL_VARYING;
212 relation_kind
213 range_operator::lhs_op1_relation (const prange &lhs ATTRIBUTE_UNUSED,
214 const prange &op1 ATTRIBUTE_UNUSED,
215 const prange &op2 ATTRIBUTE_UNUSED,
216 relation_kind rel ATTRIBUTE_UNUSED) const
218 return VREL_VARYING;
221 void
222 range_operator::update_bitmask (irange &,
223 const prange &,
224 const prange &) const
228 // Return the upper limit for a type.
230 static inline wide_int
231 max_limit (const_tree type)
233 return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
236 // Return the lower limit for a type.
238 static inline wide_int
239 min_limit (const_tree type)
241 return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
244 // Build a range that is < VAL and store it in R.
246 static void
247 build_lt (prange &r, tree type, const prange &val)
249 wi::overflow_type ov;
250 wide_int lim = wi::sub (val.upper_bound (), 1, UNSIGNED, &ov);
252 // If val - 1 underflows, check if X < MIN, which is an empty range.
253 if (ov)
254 r.set_undefined ();
255 else
256 r.set (type, min_limit (type), lim);
259 // Build a range that is <= VAL and store it in R.
261 static void
262 build_le (prange &r, tree type, const prange &val)
264 r.set (type, min_limit (type), val.upper_bound ());
267 // Build a range that is > VAL and store it in R.
269 static void
270 build_gt (prange &r, tree type, const prange &val)
272 wi::overflow_type ov;
273 wide_int lim = wi::add (val.lower_bound (), 1, UNSIGNED, &ov);
275 // If val + 1 overflows, check is for X > MAX, which is an empty range.
276 if (ov)
277 r.set_undefined ();
278 else
279 r.set (type, lim, max_limit (type));
283 // Build a range that is >= VAL and store it in R.
285 static void
286 build_ge (prange &r, tree type, const prange &val)
288 r.set (type, val.lower_bound (), max_limit (type));
291 class pointer_plus_operator : public range_operator
293 using range_operator::update_bitmask;
294 using range_operator::fold_range;
295 using range_operator::op2_range;
296 public:
297 virtual bool fold_range (prange &r, tree type,
298 const prange &op1,
299 const irange &op2,
300 relation_trio) const final override;
301 virtual bool op2_range (irange &r, tree type,
302 const prange &lhs,
303 const prange &op1,
304 relation_trio = TRIO_VARYING) const final override;
305 virtual void wi_fold (irange &r, tree type,
306 const wide_int &lh_lb,
307 const wide_int &lh_ub,
308 const wide_int &rh_lb,
309 const wide_int &rh_ub) const;
310 virtual bool op2_range (irange &r, tree type,
311 const irange &lhs,
312 const irange &op1,
313 relation_trio = TRIO_VARYING) const;
314 void update_bitmask (irange &r, const irange &lh, const irange &rh) const
315 { update_known_bitmask (r, POINTER_PLUS_EXPR, lh, rh); }
316 } op_pointer_plus;
318 bool
319 pointer_plus_operator::fold_range (prange &r, tree type,
320 const prange &op1,
321 const irange &op2,
322 relation_trio) const
324 if (empty_range_varying (r, type, op1, op2))
325 return true;
327 const wide_int lh_lb = op1.lower_bound ();
328 const wide_int lh_ub = op1.upper_bound ();
329 const wide_int rh_lb = op2.lower_bound ();
330 const wide_int rh_ub = op2.upper_bound ();
332 // Check for [0,0] + const, and simply return the const.
333 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
335 r.set (type, rh_lb, rh_lb);
336 return true;
339 // For pointer types, we are really only interested in asserting
340 // whether the expression evaluates to non-NULL.
342 // With -fno-delete-null-pointer-checks we need to be more
343 // conservative. As some object might reside at address 0,
344 // then some offset could be added to it and the same offset
345 // subtracted again and the result would be NULL.
346 // E.g.
347 // static int a[12]; where &a[0] is NULL and
348 // ptr = &a[6];
349 // ptr -= 6;
350 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
351 // where the first range doesn't include zero and the second one
352 // doesn't either. As the second operand is sizetype (unsigned),
353 // consider all ranges where the MSB could be set as possible
354 // subtractions where the result might be NULL.
355 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
356 || !wi_includes_zero_p (type, rh_lb, rh_ub))
357 && !TYPE_OVERFLOW_WRAPS (type)
358 && (flag_delete_null_pointer_checks
359 || !wi::sign_mask (rh_ub)))
360 r.set_nonzero (type);
361 else if (lh_lb == lh_ub && lh_lb == 0
362 && rh_lb == rh_ub && rh_lb == 0)
363 r.set_zero (type);
364 else
365 r.set_varying (type);
367 update_known_bitmask (r, POINTER_PLUS_EXPR, op1, op2);
368 return true;
371 bool
372 pointer_plus_operator::op2_range (irange &r, tree type,
373 const prange &lhs ATTRIBUTE_UNUSED,
374 const prange &op1 ATTRIBUTE_UNUSED,
375 relation_trio trio) const
377 relation_kind rel = trio.lhs_op1 ();
378 r.set_varying (type);
380 // If the LHS and OP1 are equal, the op2 must be zero.
381 if (rel == VREL_EQ)
382 r.set_zero (type);
383 // If the LHS and OP1 are not equal, the offset must be non-zero.
384 else if (rel == VREL_NE)
385 r.set_nonzero (type);
386 else
387 return false;
388 return true;
391 void
392 pointer_plus_operator::wi_fold (irange &r, tree type,
393 const wide_int &lh_lb,
394 const wide_int &lh_ub,
395 const wide_int &rh_lb,
396 const wide_int &rh_ub) const
398 // Check for [0,0] + const, and simply return the const.
399 if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
401 r.set (type, rh_lb, rh_lb);
402 return;
405 // For pointer types, we are really only interested in asserting
406 // whether the expression evaluates to non-NULL.
408 // With -fno-delete-null-pointer-checks we need to be more
409 // conservative. As some object might reside at address 0,
410 // then some offset could be added to it and the same offset
411 // subtracted again and the result would be NULL.
412 // E.g.
413 // static int a[12]; where &a[0] is NULL and
414 // ptr = &a[6];
415 // ptr -= 6;
416 // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
417 // where the first range doesn't include zero and the second one
418 // doesn't either. As the second operand is sizetype (unsigned),
419 // consider all ranges where the MSB could be set as possible
420 // subtractions where the result might be NULL.
421 if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
422 || !wi_includes_zero_p (type, rh_lb, rh_ub))
423 && !TYPE_OVERFLOW_WRAPS (type)
424 && (flag_delete_null_pointer_checks
425 || !wi::sign_mask (rh_ub)))
426 r.set_nonzero (type);
427 else if (lh_lb == lh_ub && lh_lb == 0
428 && rh_lb == rh_ub && rh_lb == 0)
429 r.set_zero (type);
430 else
431 r.set_varying (type);
434 bool
435 pointer_plus_operator::op2_range (irange &r, tree type,
436 const irange &lhs ATTRIBUTE_UNUSED,
437 const irange &op1 ATTRIBUTE_UNUSED,
438 relation_trio trio) const
440 relation_kind rel = trio.lhs_op1 ();
441 r.set_varying (type);
443 // If the LHS and OP1 are equal, the op2 must be zero.
444 if (rel == VREL_EQ)
445 r.set_zero (type);
446 // If the LHS and OP1 are not equal, the offset must be non-zero.
447 else if (rel == VREL_NE)
448 r.set_nonzero (type);
449 else
450 return false;
451 return true;
454 class pointer_min_max_operator : public range_operator
456 public:
457 virtual void wi_fold (irange & r, tree type,
458 const wide_int &lh_lb, const wide_int &lh_ub,
459 const wide_int &rh_lb, const wide_int &rh_ub) const;
460 } op_ptr_min_max;
462 void
463 pointer_min_max_operator::wi_fold (irange &r, tree type,
464 const wide_int &lh_lb,
465 const wide_int &lh_ub,
466 const wide_int &rh_lb,
467 const wide_int &rh_ub) const
469 // For MIN/MAX expressions with pointers, we only care about
470 // nullness. If both are non null, then the result is nonnull.
471 // If both are null, then the result is null. Otherwise they
472 // are varying.
473 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
474 && !wi_includes_zero_p (type, rh_lb, rh_ub))
475 r.set_nonzero (type);
476 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
477 r.set_zero (type);
478 else
479 r.set_varying (type);
482 class pointer_and_operator : public range_operator
484 public:
485 virtual void wi_fold (irange &r, tree type,
486 const wide_int &lh_lb, const wide_int &lh_ub,
487 const wide_int &rh_lb, const wide_int &rh_ub) const;
488 } op_pointer_and;
490 void
491 pointer_and_operator::wi_fold (irange &r, tree type,
492 const wide_int &lh_lb,
493 const wide_int &lh_ub,
494 const wide_int &rh_lb ATTRIBUTE_UNUSED,
495 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
497 // For pointer types, we are really only interested in asserting
498 // whether the expression evaluates to non-NULL.
499 if (wi_zero_p (type, lh_lb, lh_ub) || wi_zero_p (type, lh_lb, lh_ub))
500 r.set_zero (type);
501 else
502 r.set_varying (type);
506 class pointer_or_operator : public range_operator
508 public:
509 using range_operator::op1_range;
510 using range_operator::op2_range;
511 virtual bool op1_range (irange &r, tree type,
512 const irange &lhs,
513 const irange &op2,
514 relation_trio rel = TRIO_VARYING) const;
515 virtual bool op2_range (irange &r, tree type,
516 const irange &lhs,
517 const irange &op1,
518 relation_trio rel = TRIO_VARYING) const;
519 virtual void wi_fold (irange &r, tree type,
520 const wide_int &lh_lb, const wide_int &lh_ub,
521 const wide_int &rh_lb, const wide_int &rh_ub) const;
522 } op_pointer_or;
524 bool
525 pointer_or_operator::op1_range (irange &r, tree type,
526 const irange &lhs,
527 const irange &op2 ATTRIBUTE_UNUSED,
528 relation_trio) const
530 if (lhs.undefined_p ())
531 return false;
532 if (lhs.zero_p ())
534 r.set_zero (type);
535 return true;
537 r.set_varying (type);
538 return true;
541 bool
542 pointer_or_operator::op2_range (irange &r, tree type,
543 const irange &lhs,
544 const irange &op1,
545 relation_trio) const
547 return pointer_or_operator::op1_range (r, type, lhs, op1);
550 void
551 pointer_or_operator::wi_fold (irange &r, tree type,
552 const wide_int &lh_lb,
553 const wide_int &lh_ub,
554 const wide_int &rh_lb,
555 const wide_int &rh_ub) const
557 // For pointer types, we are really only interested in asserting
558 // whether the expression evaluates to non-NULL.
559 if (!wi_includes_zero_p (type, lh_lb, lh_ub)
560 && !wi_includes_zero_p (type, rh_lb, rh_ub))
561 r.set_nonzero (type);
562 else if (wi_zero_p (type, lh_lb, lh_ub) && wi_zero_p (type, rh_lb, rh_ub))
563 r.set_zero (type);
564 else
565 r.set_varying (type);
568 class operator_pointer_diff : public range_operator
570 using range_operator::update_bitmask;
571 using range_operator::op1_op2_relation_effect;
572 virtual bool op1_op2_relation_effect (irange &lhs_range,
573 tree type,
574 const irange &op1_range,
575 const irange &op2_range,
576 relation_kind rel) const;
577 virtual bool op1_op2_relation_effect (irange &lhs_range,
578 tree type,
579 const prange &op1_range,
580 const prange &op2_range,
581 relation_kind rel) const final override;
582 void update_bitmask (irange &r, const irange &lh, const irange &rh) const
583 { update_known_bitmask (r, POINTER_DIFF_EXPR, lh, rh); }
584 void update_bitmask (irange &r,
585 const prange &lh, const prange &rh) const final override
586 { update_known_bitmask (r, POINTER_DIFF_EXPR, lh, rh); }
587 } op_pointer_diff;
589 bool
590 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
591 const prange &op1_range,
592 const prange &op2_range,
593 relation_kind rel) const
595 int_range<2> op1, op2, tmp;
596 range_op_handler cast (CONVERT_EXPR);
598 if (!cast.fold_range (op1, type, op1_range, tmp)
599 || !cast.fold_range (op2, type, op2_range, tmp))
600 return false;
602 return minus_op1_op2_relation_effect (lhs_range, type, op1, op2, rel);
605 bool
606 operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
607 const irange &op1_range,
608 const irange &op2_range,
609 relation_kind rel) const
611 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
612 rel);
615 bool
616 operator_identity::fold_range (prange &r, tree type ATTRIBUTE_UNUSED,
617 const prange &lh ATTRIBUTE_UNUSED,
618 const prange &rh ATTRIBUTE_UNUSED,
619 relation_trio) const
621 r = lh;
622 return true;
625 relation_kind
626 operator_identity::lhs_op1_relation (const prange &lhs,
627 const prange &op1 ATTRIBUTE_UNUSED,
628 const prange &op2 ATTRIBUTE_UNUSED,
629 relation_kind) const
631 if (lhs.undefined_p ())
632 return VREL_VARYING;
633 // Simply a copy, so they are equivalent.
634 return VREL_EQ;
637 bool
638 operator_identity::op1_range (prange &r, tree type ATTRIBUTE_UNUSED,
639 const prange &lhs,
640 const prange &op2 ATTRIBUTE_UNUSED,
641 relation_trio) const
643 r = lhs;
644 return true;
647 bool
648 operator_cst::fold_range (prange &r, tree type ATTRIBUTE_UNUSED,
649 const prange &lh,
650 const prange & ATTRIBUTE_UNUSED,
651 relation_trio) const
653 r = lh;
654 return true;
657 // Cast between pointers.
659 bool
660 operator_cast::fold_range (prange &r, tree type,
661 const prange &inner,
662 const prange &outer,
663 relation_trio) const
665 if (empty_range_varying (r, type, inner, outer))
666 return true;
668 r.set (type, inner.lower_bound (), inner.upper_bound ());
669 r.update_bitmask (inner.get_bitmask ());
670 return true;
673 // Cast a pointer to an integer.
675 bool
676 operator_cast::fold_range (irange &r, tree type,
677 const prange &inner,
678 const irange &outer,
679 relation_trio) const
681 if (empty_range_varying (r, type, inner, outer))
682 return true;
684 // Represent INNER as an integer of the same size, and then cast it
685 // to the resulting integer type.
686 tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (inner.type ()));
687 r.set (pointer_uint_type, inner.lower_bound (), inner.upper_bound ());
688 r.update_bitmask (inner.get_bitmask ());
689 range_cast (r, type);
690 return true;
693 // Cast an integer to a pointer.
695 bool
696 operator_cast::fold_range (prange &r, tree type,
697 const irange &inner,
698 const prange &outer,
699 relation_trio) const
701 if (empty_range_varying (r, type, inner, outer))
702 return true;
704 // Cast INNER to an integer of the same size as the pointer we want,
705 // and then copy the bounds to the resulting pointer range.
706 int_range<2> tmp = inner;
707 tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (type));
708 range_cast (tmp, pointer_uint_type);
709 r.set (type, tmp.lower_bound (), tmp.upper_bound ());
710 r.update_bitmask (tmp.get_bitmask ());
711 return true;
714 bool
715 operator_cast::op1_range (prange &r, tree type,
716 const prange &lhs,
717 const prange &op2,
718 relation_trio trio) const
720 if (lhs.undefined_p ())
721 return false;
722 gcc_checking_assert (types_compatible_p (op2.type(), type));
724 // Conversion from other pointers or a constant (including 0/NULL)
725 // are straightforward.
726 if (POINTER_TYPE_P (lhs.type ())
727 || (lhs.singleton_p ()
728 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
729 fold_range (r, type, lhs, op2, trio);
730 else
732 // If the LHS is not a pointer nor a singleton, then it is
733 // either VARYING or non-zero.
734 if (!lhs.undefined_p () && !range_includes_zero_p (lhs))
735 r.set_nonzero (type);
736 else
737 r.set_varying (type);
739 r.intersect (op2);
740 return true;
743 bool
744 operator_cast::op1_range (irange &r, tree type,
745 const prange &lhs,
746 const irange &op2,
747 relation_trio trio) const
749 if (lhs.undefined_p ())
750 return false;
751 gcc_checking_assert (types_compatible_p (op2.type(), type));
753 // Conversion from other pointers or a constant (including 0/NULL)
754 // are straightforward.
755 if (POINTER_TYPE_P (lhs.type ())
756 || (lhs.singleton_p ()
757 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
758 fold_range (r, type, lhs, op2, trio);
759 else
761 // If the LHS is not a pointer nor a singleton, then it is
762 // either VARYING or non-zero.
763 if (!lhs.undefined_p () && !range_includes_zero_p (lhs))
764 r.set_nonzero (type);
765 else
766 r.set_varying (type);
768 r.intersect (op2);
769 return true;
772 bool
773 operator_cast::op1_range (prange &r, tree type,
774 const irange &lhs,
775 const prange &op2,
776 relation_trio trio) const
778 if (lhs.undefined_p ())
779 return false;
780 gcc_checking_assert (types_compatible_p (op2.type(), type));
782 // Conversion from other pointers or a constant (including 0/NULL)
783 // are straightforward.
784 if (POINTER_TYPE_P (lhs.type ())
785 || (lhs.singleton_p ()
786 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
787 fold_range (r, type, lhs, op2, trio);
788 else
790 // If the LHS is not a pointer nor a singleton, then it is
791 // either VARYING or non-zero.
792 if (!lhs.undefined_p () && !range_includes_zero_p (lhs))
793 r.set_nonzero (type);
794 else
795 r.set_varying (type);
797 r.intersect (op2);
798 return true;
801 relation_kind
802 operator_cast::lhs_op1_relation (const prange &lhs,
803 const prange &op1,
804 const prange &op2 ATTRIBUTE_UNUSED,
805 relation_kind) const
807 if (lhs.undefined_p () || op1.undefined_p ())
808 return VREL_VARYING;
809 unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
810 unsigned op1_prec = TYPE_PRECISION (op1.type ());
811 // If the result gets sign extended into a larger type check first if this
812 // qualifies as a partial equivalence.
813 if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
815 // If the result is sign extended, and the LHS is larger than op1,
816 // check if op1's range can be negative as the sign extension will
817 // cause the upper bits to be 1 instead of 0, invalidating the PE.
818 int_range<3> negs = range_negatives (op1.type ());
819 negs.intersect (op1);
820 if (!negs.undefined_p ())
821 return VREL_VARYING;
824 unsigned prec = MIN (lhs_prec, op1_prec);
825 return bits_to_pe (prec);
828 relation_kind
829 operator_cast::lhs_op1_relation (const prange &lhs,
830 const irange &op1,
831 const irange &op2 ATTRIBUTE_UNUSED,
832 relation_kind) const
834 if (lhs.undefined_p () || op1.undefined_p ())
835 return VREL_VARYING;
836 unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
837 unsigned op1_prec = TYPE_PRECISION (op1.type ());
838 // If the result gets sign extended into a larger type check first if this
839 // qualifies as a partial equivalence.
840 if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
842 // If the result is sign extended, and the LHS is larger than op1,
843 // check if op1's range can be negative as the sign extension will
844 // cause the upper bits to be 1 instead of 0, invalidating the PE.
845 int_range<3> negs = range_negatives (op1.type ());
846 negs.intersect (op1);
847 if (!negs.undefined_p ())
848 return VREL_VARYING;
851 unsigned prec = MIN (lhs_prec, op1_prec);
852 return bits_to_pe (prec);
855 relation_kind
856 operator_cast::lhs_op1_relation (const irange &lhs,
857 const prange &op1,
858 const prange &op2 ATTRIBUTE_UNUSED,
859 relation_kind) const
861 if (lhs.undefined_p () || op1.undefined_p ())
862 return VREL_VARYING;
863 unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
864 unsigned op1_prec = TYPE_PRECISION (op1.type ());
865 // If the result gets sign extended into a larger type check first if this
866 // qualifies as a partial equivalence.
867 if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
869 // If the result is sign extended, and the LHS is larger than op1,
870 // check if op1's range can be negative as the sign extension will
871 // cause the upper bits to be 1 instead of 0, invalidating the PE.
872 int_range<3> negs = range_negatives (op1.type ());
873 negs.intersect (op1);
874 if (!negs.undefined_p ())
875 return VREL_VARYING;
878 unsigned prec = MIN (lhs_prec, op1_prec);
879 return bits_to_pe (prec);
882 bool
883 operator_min::fold_range (prange &r, tree type,
884 const prange &op1,
885 const prange &op2,
886 relation_trio) const
888 // For MIN/MAX expressions with pointers, we only care about
889 // nullness. If both are non null, then the result is nonnull.
890 // If both are null, then the result is null. Otherwise they
891 // are varying.
892 if (!range_includes_zero_p (op1)
893 && !range_includes_zero_p (op2))
894 r.set_nonzero (type);
895 else if (op1.zero_p () && op2.zero_p ())
896 r.set_zero (type);
897 else
898 r.set_varying (type);
900 update_known_bitmask (r, MIN_EXPR, op1, op2);
901 return true;
904 bool
905 operator_max::fold_range (prange &r, tree type,
906 const prange &op1,
907 const prange &op2,
908 relation_trio) const
910 // For MIN/MAX expressions with pointers, we only care about
911 // nullness. If both are non null, then the result is nonnull.
912 // If both are null, then the result is null. Otherwise they
913 // are varying.
914 if (!range_includes_zero_p (op1)
915 && !range_includes_zero_p (op2))
916 r.set_nonzero (type);
917 else if (op1.zero_p () && op2.zero_p ())
918 r.set_zero (type);
919 else
920 r.set_varying (type);
922 update_known_bitmask (r, MAX_EXPR, op1, op2);
923 return true;
926 bool
927 operator_addr_expr::op1_range (prange &r, tree type,
928 const prange &lhs,
929 const prange &op2,
930 relation_trio) const
932 if (empty_range_varying (r, type, lhs, op2))
933 return true;
935 // Return a non-null pointer of the LHS type (passed in op2), but only
936 // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
937 // See PR 111009.
938 if (!lhs.undefined_p ()
939 && !range_includes_zero_p (lhs)
940 && TYPE_OVERFLOW_UNDEFINED (type))
941 r.set_nonzero (type);
942 else
943 r.set_varying (type);
944 return true;
947 bool
948 operator_bitwise_and::fold_range (prange &r, tree type,
949 const prange &op1,
950 const prange &op2 ATTRIBUTE_UNUSED,
951 relation_trio) const
953 // For pointer types, we are really only interested in asserting
954 // whether the expression evaluates to non-NULL.
955 if (op1.zero_p () || op2.zero_p ())
956 r.set_zero (type);
957 else
958 r.set_varying (type);
960 update_known_bitmask (r, BIT_AND_EXPR, op1, op2);
961 return true;
964 bool
965 operator_equal::fold_range (irange &r, tree type,
966 const prange &op1,
967 const prange &op2,
968 relation_trio rel) const
970 if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
971 return true;
973 // We can be sure the values are always equal or not if both ranges
974 // consist of a single value, and then compare them.
975 bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
976 bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
977 if (op1_const && op2_const)
979 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
980 r = range_true (type);
981 else
982 r = range_false (type);
984 else
986 // If ranges do not intersect, we know the range is not equal,
987 // otherwise we don't know anything for sure.
988 prange tmp = op1;
989 tmp.intersect (op2);
990 if (tmp.undefined_p ())
991 r = range_false (type);
992 // Check if a constant cannot satisfy the bitmask requirements.
993 else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
994 r = range_false (type);
995 else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
996 r = range_false (type);
997 else
998 r = range_true_and_false (type);
1001 //update_known_bitmask (r, EQ_EXPR, op1, op2);
1002 return true;
1005 bool
1006 operator_equal::op1_range (prange &r, tree type,
1007 const irange &lhs,
1008 const prange &op2,
1009 relation_trio) const
1011 switch (get_bool_state (r, lhs, type))
1013 case BRS_TRUE:
1014 // If it's true, the result is the same as OP2.
1015 r = op2;
1016 break;
1018 case BRS_FALSE:
1019 // If the result is false, the only time we know anything is
1020 // if OP2 is a constant.
1021 if (!op2.undefined_p ()
1022 && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1024 r = op2;
1025 r.invert ();
1027 else
1028 r.set_varying (type);
1029 break;
1031 default:
1032 break;
1034 return true;
1037 bool
1038 operator_equal::op2_range (prange &r, tree type,
1039 const irange &lhs,
1040 const prange &op1,
1041 relation_trio rel) const
1043 return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1046 relation_kind
1047 operator_equal::op1_op2_relation (const irange &lhs, const prange &,
1048 const prange &) const
1050 if (lhs.undefined_p ())
1051 return VREL_UNDEFINED;
1053 // FALSE = op1 == op2 indicates NE_EXPR.
1054 if (lhs.zero_p ())
1055 return VREL_NE;
1057 // TRUE = op1 == op2 indicates EQ_EXPR.
1058 if (!range_includes_zero_p (lhs))
1059 return VREL_EQ;
1060 return VREL_VARYING;
1063 bool
1064 operator_not_equal::fold_range (irange &r, tree type,
1065 const prange &op1,
1066 const prange &op2,
1067 relation_trio rel) const
1069 if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
1070 return true;
1072 // We can be sure the values are always equal or not if both ranges
1073 // consist of a single value, and then compare them.
1074 bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
1075 bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
1076 if (op1_const && op2_const)
1078 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
1079 r = range_true (type);
1080 else
1081 r = range_false (type);
1083 else
1085 // If ranges do not intersect, we know the range is not equal,
1086 // otherwise we don't know anything for sure.
1087 prange tmp = op1;
1088 tmp.intersect (op2);
1089 if (tmp.undefined_p ())
1090 r = range_true (type);
1091 // Check if a constant cannot satisfy the bitmask requirements.
1092 else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
1093 r = range_true (type);
1094 else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
1095 r = range_true (type);
1096 else
1097 r = range_true_and_false (type);
1100 //update_known_bitmask (r, NE_EXPR, op1, op2);
1101 return true;
1104 bool
1105 operator_not_equal::op1_range (prange &r, tree type,
1106 const irange &lhs,
1107 const prange &op2,
1108 relation_trio) const
1110 switch (get_bool_state (r, lhs, type))
1112 case BRS_TRUE:
1113 // If the result is true, the only time we know anything is if
1114 // OP2 is a constant.
1115 if (!op2.undefined_p ()
1116 && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1118 r = op2;
1119 r.invert ();
1121 else
1122 r.set_varying (type);
1123 break;
1125 case BRS_FALSE:
1126 // If it's false, the result is the same as OP2.
1127 r = op2;
1128 break;
1130 default:
1131 break;
1133 return true;
1137 bool
1138 operator_not_equal::op2_range (prange &r, tree type,
1139 const irange &lhs,
1140 const prange &op1,
1141 relation_trio rel) const
1143 return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1146 relation_kind
1147 operator_not_equal::op1_op2_relation (const irange &lhs, const prange &,
1148 const prange &) const
1150 if (lhs.undefined_p ())
1151 return VREL_UNDEFINED;
1153 // FALSE = op1 != op2 indicates EQ_EXPR.
1154 if (lhs.zero_p ())
1155 return VREL_EQ;
1157 // TRUE = op1 != op2 indicates NE_EXPR.
1158 if (!range_includes_zero_p (lhs))
1159 return VREL_NE;
1160 return VREL_VARYING;
1163 bool
1164 operator_lt::fold_range (irange &r, tree type,
1165 const prange &op1,
1166 const prange &op2,
1167 relation_trio rel) const
1169 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
1170 return true;
1172 signop sign = TYPE_SIGN (op1.type ());
1173 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1175 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
1176 r = range_true (type);
1177 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
1178 r = range_false (type);
1179 // Use nonzero bits to determine if < 0 is false.
1180 else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
1181 r = range_false (type);
1182 else
1183 r = range_true_and_false (type);
1185 //update_known_bitmask (r, LT_EXPR, op1, op2);
1186 return true;
1189 bool
1190 operator_lt::op1_range (prange &r, tree type,
1191 const irange &lhs,
1192 const prange &op2,
1193 relation_trio) const
1195 if (op2.undefined_p ())
1196 return false;
1198 switch (get_bool_state (r, lhs, type))
1200 case BRS_TRUE:
1201 build_lt (r, type, op2);
1202 break;
1204 case BRS_FALSE:
1205 build_ge (r, type, op2);
1206 break;
1208 default:
1209 break;
1211 return true;
1214 bool
1215 operator_lt::op2_range (prange &r, tree type,
1216 const irange &lhs,
1217 const prange &op1,
1218 relation_trio) const
1220 if (op1.undefined_p ())
1221 return false;
1223 switch (get_bool_state (r, lhs, type))
1225 case BRS_TRUE:
1226 build_gt (r, type, op1);
1227 break;
1229 case BRS_FALSE:
1230 build_le (r, type, op1);
1231 break;
1233 default:
1234 break;
1236 return true;
1239 relation_kind
1240 operator_lt::op1_op2_relation (const irange &lhs, const prange &,
1241 const prange &) const
1243 if (lhs.undefined_p ())
1244 return VREL_UNDEFINED;
1246 // FALSE = op1 < op2 indicates GE_EXPR.
1247 if (lhs.zero_p ())
1248 return VREL_GE;
1250 // TRUE = op1 < op2 indicates LT_EXPR.
1251 if (!range_includes_zero_p (lhs))
1252 return VREL_LT;
1253 return VREL_VARYING;
1256 bool
1257 operator_le::fold_range (irange &r, tree type,
1258 const prange &op1,
1259 const prange &op2,
1260 relation_trio rel) const
1262 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1263 return true;
1265 signop sign = TYPE_SIGN (op1.type ());
1266 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1268 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
1269 r = range_true (type);
1270 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
1271 r = range_false (type);
1272 else
1273 r = range_true_and_false (type);
1275 //update_known_bitmask (r, LE_EXPR, op1, op2);
1276 return true;
1279 bool
1280 operator_le::op1_range (prange &r, tree type,
1281 const irange &lhs,
1282 const prange &op2,
1283 relation_trio) const
1285 if (op2.undefined_p ())
1286 return false;
1288 switch (get_bool_state (r, lhs, type))
1290 case BRS_TRUE:
1291 build_le (r, type, op2);
1292 break;
1294 case BRS_FALSE:
1295 build_gt (r, type, op2);
1296 break;
1298 default:
1299 break;
1301 return true;
1304 bool
1305 operator_le::op2_range (prange &r, tree type,
1306 const irange &lhs,
1307 const prange &op1,
1308 relation_trio) const
1310 if (op1.undefined_p ())
1311 return false;
1313 switch (get_bool_state (r, lhs, type))
1315 case BRS_TRUE:
1316 build_ge (r, type, op1);
1317 break;
1319 case BRS_FALSE:
1320 build_lt (r, type, op1);
1321 break;
1323 default:
1324 break;
1326 return true;
1329 relation_kind
1330 operator_le::op1_op2_relation (const irange &lhs, const prange &,
1331 const prange &) const
1333 if (lhs.undefined_p ())
1334 return VREL_UNDEFINED;
1336 // FALSE = op1 <= op2 indicates GT_EXPR.
1337 if (lhs.zero_p ())
1338 return VREL_GT;
1340 // TRUE = op1 <= op2 indicates LE_EXPR.
1341 if (!range_includes_zero_p (lhs))
1342 return VREL_LE;
1343 return VREL_VARYING;
1346 bool
1347 operator_gt::fold_range (irange &r, tree type,
1348 const prange &op1, const prange &op2,
1349 relation_trio rel) const
1351 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1352 return true;
1354 signop sign = TYPE_SIGN (op1.type ());
1355 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1357 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1358 r = range_true (type);
1359 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1360 r = range_false (type);
1361 else
1362 r = range_true_and_false (type);
1364 //update_known_bitmask (r, GT_EXPR, op1, op2);
1365 return true;
1368 bool
1369 operator_gt::op1_range (prange &r, tree type,
1370 const irange &lhs, const prange &op2,
1371 relation_trio) const
1373 if (op2.undefined_p ())
1374 return false;
1376 switch (get_bool_state (r, lhs, type))
1378 case BRS_TRUE:
1379 build_gt (r, type, op2);
1380 break;
1382 case BRS_FALSE:
1383 build_le (r, type, op2);
1384 break;
1386 default:
1387 break;
1389 return true;
1392 bool
1393 operator_gt::op2_range (prange &r, tree type,
1394 const irange &lhs,
1395 const prange &op1,
1396 relation_trio) const
1398 if (op1.undefined_p ())
1399 return false;
1401 switch (get_bool_state (r, lhs, type))
1403 case BRS_TRUE:
1404 build_lt (r, type, op1);
1405 break;
1407 case BRS_FALSE:
1408 build_ge (r, type, op1);
1409 break;
1411 default:
1412 break;
1414 return true;
1417 relation_kind
1418 operator_gt::op1_op2_relation (const irange &lhs, const prange &,
1419 const prange &) const
1421 if (lhs.undefined_p ())
1422 return VREL_UNDEFINED;
1424 // FALSE = op1 > op2 indicates LE_EXPR.
1425 if (lhs.zero_p ())
1426 return VREL_LE;
1428 // TRUE = op1 > op2 indicates GT_EXPR.
1429 if (!range_includes_zero_p (lhs))
1430 return VREL_GT;
1431 return VREL_VARYING;
1434 bool
1435 operator_ge::fold_range (irange &r, tree type,
1436 const prange &op1,
1437 const prange &op2,
1438 relation_trio rel) const
1440 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1441 return true;
1443 signop sign = TYPE_SIGN (op1.type ());
1444 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1446 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1447 r = range_true (type);
1448 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1449 r = range_false (type);
1450 else
1451 r = range_true_and_false (type);
1453 //update_known_bitmask (r, GE_EXPR, op1, op2);
1454 return true;
1457 bool
1458 operator_ge::op1_range (prange &r, tree type,
1459 const irange &lhs,
1460 const prange &op2,
1461 relation_trio) const
1463 if (op2.undefined_p ())
1464 return false;
1466 switch (get_bool_state (r, lhs, type))
1468 case BRS_TRUE:
1469 build_ge (r, type, op2);
1470 break;
1472 case BRS_FALSE:
1473 build_lt (r, type, op2);
1474 break;
1476 default:
1477 break;
1479 return true;
1482 bool
1483 operator_ge::op2_range (prange &r, tree type,
1484 const irange &lhs,
1485 const prange &op1,
1486 relation_trio) const
1488 if (op1.undefined_p ())
1489 return false;
1491 switch (get_bool_state (r, lhs, type))
1493 case BRS_TRUE:
1494 build_le (r, type, op1);
1495 break;
1497 case BRS_FALSE:
1498 build_gt (r, type, op1);
1499 break;
1501 default:
1502 break;
1504 return true;
1507 relation_kind
1508 operator_ge::op1_op2_relation (const irange &lhs, const prange &,
1509 const prange &) const
1511 if (lhs.undefined_p ())
1512 return VREL_UNDEFINED;
1514 // FALSE = op1 >= op2 indicates LT_EXPR.
1515 if (lhs.zero_p ())
1516 return VREL_LT;
1518 // TRUE = op1 >= op2 indicates GE_EXPR.
1519 if (!range_includes_zero_p (lhs))
1520 return VREL_GE;
1521 return VREL_VARYING;
1524 // Initialize any pointer operators to the primary table
1526 void
1527 range_op_table::initialize_pointer_ops ()
1529 set (POINTER_PLUS_EXPR, op_pointer_plus);
1530 set (POINTER_DIFF_EXPR, op_pointer_diff);