Line data Source code
1 : /* Code for range operators.
2 : Copyright (C) 2017-2026 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-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"
51 :
52 : bool
53 0 : range_operator::fold_range (prange &, tree, const prange &, const prange &,
54 : relation_trio) const
55 : {
56 0 : return false;
57 : }
58 :
59 : bool
60 0 : range_operator::fold_range (prange &, tree, const prange &, const irange &,
61 : relation_trio) const
62 : {
63 0 : return false;
64 : }
65 :
66 : bool
67 0 : range_operator::fold_range (irange &, tree, const prange &, const prange &,
68 : relation_trio) const
69 : {
70 0 : return false;
71 : }
72 :
73 : bool
74 0 : range_operator::fold_range (prange &, tree, const irange &, const prange &,
75 : relation_trio) const
76 : {
77 0 : return false;
78 : }
79 :
80 : bool
81 369 : range_operator::fold_range (irange &, tree, const prange &, const irange &,
82 : relation_trio) const
83 : {
84 369 : return false;
85 : }
86 :
87 : bool
88 0 : range_operator::op1_op2_relation_effect (prange &, tree,
89 : const prange &,
90 : const prange &,
91 : relation_kind) const
92 : {
93 0 : return false;
94 : }
95 :
96 : bool
97 0 : range_operator::op1_op2_relation_effect (prange &, tree,
98 : const prange &,
99 : const irange &,
100 : relation_kind) const
101 : {
102 0 : return false;
103 : }
104 :
105 : bool
106 0 : range_operator::op1_op2_relation_effect (irange &, tree,
107 : const prange &,
108 : const prange &,
109 : relation_kind) const
110 : {
111 0 : return false;
112 : }
113 :
114 : bool
115 0 : range_operator::op1_op2_relation_effect (prange &, tree,
116 : const irange &,
117 : const prange &,
118 : relation_kind) const
119 : {
120 0 : return false;
121 : }
122 :
123 : bool
124 0 : range_operator::op1_op2_relation_effect (irange &, tree,
125 : const prange &,
126 : const irange &,
127 : relation_kind) const
128 : {
129 0 : return false;
130 : }
131 :
132 : bool
133 86 : range_operator::op1_range (prange &, tree,
134 : const prange &lhs ATTRIBUTE_UNUSED,
135 : const prange &op2 ATTRIBUTE_UNUSED,
136 : relation_trio) const
137 : {
138 86 : return false;
139 : }
140 :
141 : bool
142 1758681 : range_operator::op1_range (prange &, tree,
143 : const irange &lhs ATTRIBUTE_UNUSED,
144 : const prange &op2 ATTRIBUTE_UNUSED,
145 : relation_trio) const
146 : {
147 1758681 : return false;
148 : }
149 :
150 : bool
151 123616 : range_operator::op1_range (prange &, tree,
152 : const prange &lhs ATTRIBUTE_UNUSED,
153 : const irange &op2 ATTRIBUTE_UNUSED,
154 : relation_trio) const
155 : {
156 123616 : return false;
157 : }
158 :
159 : bool
160 0 : range_operator::op1_range (irange &, tree,
161 : const prange &lhs ATTRIBUTE_UNUSED,
162 : const irange &op2 ATTRIBUTE_UNUSED,
163 : relation_trio) const
164 : {
165 0 : return false;
166 : }
167 :
168 : bool
169 2063420 : range_operator::op2_range (prange &, tree,
170 : const irange &lhs ATTRIBUTE_UNUSED,
171 : const prange &op1 ATTRIBUTE_UNUSED,
172 : relation_trio) const
173 : {
174 2063420 : return false;
175 : }
176 :
177 : bool
178 0 : range_operator::op2_range (irange &, tree,
179 : const prange &lhs ATTRIBUTE_UNUSED,
180 : const prange &op1 ATTRIBUTE_UNUSED,
181 : relation_trio) const
182 : {
183 0 : return false;
184 : }
185 :
186 : relation_kind
187 0 : range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
188 : const prange &op1 ATTRIBUTE_UNUSED,
189 : const prange &op2 ATTRIBUTE_UNUSED) const
190 : {
191 0 : return VREL_VARYING;
192 : }
193 :
194 : relation_kind
195 0 : 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
199 : {
200 0 : return VREL_VARYING;
201 : }
202 :
203 : relation_kind
204 4102559 : 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
208 : {
209 4102559 : return VREL_VARYING;
210 : }
211 :
212 : relation_kind
213 78215 : 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
217 : {
218 78215 : return VREL_VARYING;
219 : }
220 :
221 : relation_kind
222 0 : range_operator::lhs_op1_relation (const prange &lhs ATTRIBUTE_UNUSED,
223 : const prange &op1 ATTRIBUTE_UNUSED,
224 : const irange &op2 ATTRIBUTE_UNUSED,
225 : relation_kind rel ATTRIBUTE_UNUSED) const
226 : {
227 0 : return VREL_VARYING;
228 : }
229 :
230 : void
231 0 : range_operator::update_bitmask (irange &,
232 : const prange &,
233 : const prange &) const
234 : {
235 0 : }
236 :
237 : // Return the upper limit for a type.
238 :
239 : static inline wide_int
240 827615 : max_limit (const_tree type)
241 : {
242 827615 : return wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
243 : }
244 :
245 : // Return the lower limit for a type.
246 :
247 : static inline wide_int
248 848547 : min_limit (const_tree type)
249 : {
250 848547 : return wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
251 : }
252 :
253 : // Build a range that is < VAL and store it in R.
254 :
255 : static void
256 517623 : build_lt (prange &r, tree type, const prange &val)
257 : {
258 517623 : wi::overflow_type ov;
259 517623 : wide_int lim = wi::sub (val.upper_bound (), 1, UNSIGNED, &ov);
260 :
261 : // If val - 1 underflows, check if X < MIN, which is an empty range.
262 517623 : if (ov)
263 0 : r.set_undefined ();
264 : else
265 517623 : r.set (type, min_limit (type), lim);
266 517623 : }
267 :
268 : // Build a range that is <= VAL and store it in R.
269 :
270 : static void
271 330924 : build_le (prange &r, tree type, const prange &val)
272 : {
273 330924 : r.set (type, min_limit (type), val.upper_bound ());
274 330924 : }
275 :
276 : // Build a range that is > VAL and store it in R.
277 :
278 : static void
279 417951 : build_gt (prange &r, tree type, const prange &val)
280 : {
281 417951 : wi::overflow_type ov;
282 417951 : wide_int lim = wi::add (val.lower_bound (), 1, UNSIGNED, &ov);
283 :
284 : // If val + 1 overflows, check is for X > MAX, which is an empty range.
285 417951 : if (ov)
286 0 : r.set_undefined ();
287 : else
288 417951 : r.set (type, lim, max_limit (type));
289 :
290 417951 : }
291 :
292 : // Build a range that is >= VAL and store it in R.
293 :
294 : static void
295 409664 : build_ge (prange &r, tree type, const prange &val)
296 : {
297 409664 : r.set (type, val.lower_bound (), max_limit (type));
298 409664 : }
299 :
300 : class pointer_plus_operator : public range_operator
301 : {
302 : using range_operator::update_bitmask;
303 : using range_operator::fold_range;
304 : using range_operator::op2_range;
305 : using range_operator::lhs_op1_relation;
306 : public:
307 : virtual bool fold_range (prange &r, tree type,
308 : const prange &op1,
309 : const irange &op2,
310 : relation_trio) const final override;
311 : virtual bool op2_range (irange &r, tree type,
312 : const prange &lhs,
313 : const prange &op1,
314 : relation_trio = TRIO_VARYING) const final override;
315 : virtual relation_kind lhs_op1_relation (const prange &lhs,
316 : const prange &op1,
317 : const irange &op2,
318 : relation_kind) const final override;
319 : void update_bitmask (prange &r, const prange &lh, const irange &rh) const
320 : { update_known_bitmask (r, POINTER_PLUS_EXPR, lh, rh); }
321 : };
322 : static const pointer_plus_operator op_pointer_plus;
323 :
324 : bool
325 9047341 : pointer_plus_operator::fold_range (prange &r, tree type,
326 : const prange &op1,
327 : const irange &op2,
328 : relation_trio) const
329 : {
330 9047341 : if (empty_range_varying (r, type, op1, op2))
331 6806 : return true;
332 :
333 9040535 : const wide_int lh_lb = op1.lower_bound ();
334 9040535 : const wide_int lh_ub = op1.upper_bound ();
335 9040535 : const wide_int rh_lb = op2.lower_bound ();
336 9040535 : const wide_int rh_ub = op2.upper_bound ();
337 :
338 : // Check for [0,0] + const, and simply return the const.
339 9048426 : if (lh_lb == 0 && lh_ub == 0 && rh_lb == rh_ub)
340 : {
341 559 : r.set (type, rh_lb, rh_lb);
342 559 : return true;
343 : }
344 :
345 : // For pointer types, we are really only interested in asserting
346 : // whether the expression evaluates to non-NULL.
347 : //
348 : // With -fno-delete-null-pointer-checks we need to be more
349 : // conservative. As some object might reside at address 0,
350 : // then some offset could be added to it and the same offset
351 : // subtracted again and the result would be NULL.
352 : // E.g.
353 : // static int a[12]; where &a[0] is NULL and
354 : // ptr = &a[6];
355 : // ptr -= 6;
356 : // ptr will be NULL here, even when there is POINTER_PLUS_EXPR
357 : // where the first range doesn't include zero and the second one
358 : // doesn't either. As the second operand is sizetype (unsigned),
359 : // consider all ranges where the MSB could be set as possible
360 : // subtractions where the result might be NULL.
361 9039976 : if ((!wi_includes_zero_p (type, lh_lb, lh_ub)
362 5349961 : || !wi_includes_zero_p (type, rh_lb, rh_ub))
363 5900846 : && !TYPE_OVERFLOW_WRAPS (type)
364 14939992 : && (flag_delete_null_pointer_checks
365 1165 : || !wi::sign_mask (rh_ub)))
366 5899654 : r.set_nonzero (type);
367 3146893 : else if (lh_lb == lh_ub && lh_lb == 0
368 3146893 : && rh_lb == rh_ub && rh_lb == 0)
369 0 : r.set_zero (type);
370 : else
371 3140322 : r.set_varying (type);
372 :
373 : // If op1 refers to an object, op1 + 0 will also refer to the object.
374 9039976 : if (rh_lb == rh_ub && rh_lb == 0)
375 22764 : r.set_addr (op1);
376 :
377 9039976 : update_known_bitmask (r, POINTER_PLUS_EXPR, op1, op2);
378 9039976 : return true;
379 9040535 : }
380 :
381 : bool
382 132357 : pointer_plus_operator::op2_range (irange &r, tree type,
383 : const prange &lhs,
384 : const prange &op1,
385 : relation_trio trio) const
386 : {
387 132357 : relation_kind rel = trio.lhs_op1 ();
388 132357 : r.set_varying (type);
389 :
390 : // If the LHS and OP1 are equal, the op2 must be zero.
391 132357 : if (rel == VREL_EQ || lhs.addr_same_known_p (op1))
392 46 : r.set_zero (type);
393 : // If the LHS and OP1 are not equal, the offset must be non-zero.
394 : // AWM: Check if aliasing may mean we can't do the not_equal check.
395 132311 : else if (rel == VREL_NE || lhs.addr_inverted_p (op1))
396 55 : r.set_nonzero (type);
397 : else
398 : return false;
399 : return true;
400 : }
401 :
402 : // Return the relation between the LHS and OP1 based on the value of the
403 : // operand being added. Pointer_plus is define to have a size_type for
404 : // operand 2 which can be interpreted as negative, so always used SIGNED.
405 : // Any overflow is considered UB and thus ignored.
406 :
407 : relation_kind
408 7836054 : pointer_plus_operator::lhs_op1_relation (const prange &lhs,
409 : const prange &op1,
410 : const irange &op2,
411 : relation_kind) const
412 : {
413 7836054 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
414 : return VREL_VARYING;
415 :
416 7829602 : unsigned prec = TYPE_PRECISION (op2.type ());
417 :
418 : // LHS = OP1 + 0 indicates LHS == OP1.
419 7829602 : if (op2.zero_p ())
420 : return VREL_EQ;
421 :
422 7808968 : tree val;
423 : // Only deal with singletons for now.
424 7808968 : if (TYPE_OVERFLOW_UNDEFINED (lhs.type ()) && op2.singleton_p (&val))
425 : {
426 : // Always interpret VALUE as a signed value. Positive will increase
427 : // the pointer value, and negative will decrease the poiinter value.
428 : // It cannot be zero or the earlier zero_p () condition will catch it.
429 3863226 : wide_int value = wi::to_wide (val);
430 :
431 : // Positive op2 means lhs > op1.
432 3863226 : if (wi::gt_p (value, wi::zero (prec), SIGNED))
433 : return VREL_GT;
434 :
435 : // Negative op2 means lhs < op1.
436 438560 : if (wi::lt_p (value, wi::zero (prec), SIGNED))
437 : return VREL_LT;
438 3863226 : }
439 :
440 : // If op2 does not contain 0, then LHS and OP1 can never be equal.
441 3945742 : if (!range_includes_zero_p (op2))
442 543804 : return VREL_NE;
443 :
444 : return VREL_VARYING;
445 : }
446 :
447 : bool
448 0 : operator_bitwise_or::fold_range (prange &r, tree type,
449 : const prange &op1,
450 : const prange &op2,
451 : relation_trio) const
452 : {
453 : // For pointer types, we are really only interested in asserting
454 : // whether the expression evaluates to non-NULL.
455 0 : if (!range_includes_zero_p (op1) || !range_includes_zero_p (op2))
456 0 : r.set_nonzero (type);
457 0 : else if (op1.zero_p () && op2.zero_p ())
458 0 : r.set_zero (type);
459 : else
460 0 : r.set_varying (type);
461 :
462 0 : update_known_bitmask (r, BIT_IOR_EXPR, op1, op2);
463 0 : return true;
464 : }
465 :
466 :
467 : class operator_pointer_diff : public range_operator
468 : {
469 : using range_operator::fold_range;
470 : using range_operator::update_bitmask;
471 : using range_operator::op1_op2_relation_effect;
472 : using range_operator::op1_op2_relation;
473 : virtual bool fold_range (irange &r, tree type,
474 : const prange &op1,
475 : const prange &op2,
476 : relation_trio trio) const final override;
477 : virtual bool op1_op2_relation_effect (irange &lhs_range,
478 : tree type,
479 : const prange &op1_range,
480 : const prange &op2_range,
481 : relation_kind rel) const final override;
482 : virtual relation_kind op1_op2_relation (const irange &lhs,
483 : const prange &rhs1,
484 : const prange &rhs2) const final override;
485 2841308 : void update_bitmask (irange &r,
486 : const prange &lh, const prange &rh) const final override
487 0 : { update_known_bitmask (r, POINTER_DIFF_EXPR, lh, rh); }
488 : };
489 : static const operator_pointer_diff op_pointer_diff;
490 :
491 : bool
492 2841308 : operator_pointer_diff::fold_range (irange &r, tree type,
493 : const prange &op1,
494 : const prange &op2,
495 : relation_trio trio) const
496 : {
497 2841308 : gcc_checking_assert (r.supports_type_p (type));
498 :
499 2841308 : r.set_varying (type);
500 2841308 : relation_kind rel = trio.op1_op2 ();
501 2841308 : op1_op2_relation_effect (r, type, op1, op2, rel);
502 : // if op1 and op2 point to the same object, the diff is 0.
503 2841308 : if (op1.addr_same_known_p (op2))
504 756 : r.set_zero (type);
505 2841308 : update_bitmask (r, op1, op2);
506 2841308 : return true;
507 : }
508 :
509 : bool
510 2841308 : operator_pointer_diff::op1_op2_relation_effect (irange &lhs_range, tree type,
511 : const prange &op1_range,
512 : const prange &op2_range,
513 : relation_kind rel) const
514 : {
515 2841308 : int_range<2> op1, op2, tmp;
516 2841308 : range_op_handler cast (CONVERT_EXPR);
517 :
518 2841308 : if (!cast.fold_range (op1, type, op1_range, tmp)
519 2841308 : || !cast.fold_range (op2, type, op2_range, tmp))
520 : return false;
521 :
522 2841308 : return minus_op1_op2_relation_effect (lhs_range, type, op1, op2, rel);
523 2841308 : }
524 :
525 :
526 : // Check if the LHS range indicates a relation between OP1 and OP2.
527 : // OP1 - OP2 == 0 implies OP1 == OP2 even with wrapping arithmetic.
528 : // For signed types, when both OP1 and OP2 are both positive, infer
529 : // a relationship if there were it is overflow free.
530 :
531 : relation_kind
532 5007977 : operator_pointer_diff::op1_op2_relation (const irange &lhs, const prange &rhs1,
533 : const prange &) const
534 : {
535 5007977 : if (lhs.undefined_p ())
536 : return VREL_UNDEFINED;
537 :
538 5007977 : if (lhs.zero_p ())
539 : return VREL_EQ;
540 :
541 4789376 : bool contains_z = lhs.contains_zero_p ();
542 : // When a - b does not overflow and signed,
543 : // we can figure out the relationship between
544 : // a and b if the result is non-negative or non-positive.
545 4789376 : if (!rhs1.undefined_p ()
546 4789376 : && TYPE_OVERFLOW_UNDEFINED (rhs1.type ()))
547 : {
548 4789371 : if (lhs.nonnegative_p ())
549 2952918 : return contains_z ? VREL_GE : VREL_GT;
550 1836453 : if (lhs.nonpositive_p ())
551 465872 : return contains_z ? VREL_LE : VREL_LT;
552 : }
553 :
554 1370586 : if (!contains_z)
555 667761 : return VREL_NE;
556 :
557 : return VREL_VARYING;
558 : }
559 :
560 : bool
561 2160507 : operator_identity::fold_range (prange &r, tree type ATTRIBUTE_UNUSED,
562 : const prange &lh ATTRIBUTE_UNUSED,
563 : const prange &rh ATTRIBUTE_UNUSED,
564 : relation_trio) const
565 : {
566 2160507 : r = lh;
567 2160507 : return true;
568 : }
569 :
570 : relation_kind
571 2003131 : operator_identity::lhs_op1_relation (const prange &lhs,
572 : const prange &op1 ATTRIBUTE_UNUSED,
573 : const prange &op2 ATTRIBUTE_UNUSED,
574 : relation_kind) const
575 : {
576 2003131 : if (lhs.undefined_p ())
577 666 : return VREL_VARYING;
578 : // Simply a copy, so they are equivalent.
579 : return VREL_EQ;
580 : }
581 :
582 : bool
583 42020 : operator_identity::op1_range (prange &r, tree type ATTRIBUTE_UNUSED,
584 : const prange &lhs,
585 : const prange &op2 ATTRIBUTE_UNUSED,
586 : relation_trio) const
587 : {
588 42020 : r = lhs;
589 42020 : return true;
590 : }
591 :
592 : bool
593 78389 : operator_cst::fold_range (prange &r, tree type ATTRIBUTE_UNUSED,
594 : const prange &lh,
595 : const prange & ATTRIBUTE_UNUSED,
596 : relation_trio) const
597 : {
598 78389 : r = lh;
599 78389 : return true;
600 : }
601 :
602 : // Cast between pointers.
603 :
604 : bool
605 16477994 : operator_cast::fold_range (prange &r, tree type,
606 : const prange &inner,
607 : const prange &outer,
608 : relation_trio) const
609 : {
610 16477994 : if (empty_range_varying (r, type, inner, outer))
611 429 : return true;
612 :
613 16477565 : r.set (type, inner.lower_bound (), inner.upper_bound ());
614 :
615 : // The resulting pointer still points to the same object.
616 16477565 : r.set_addr (inner);
617 :
618 16477565 : r.update_bitmask (inner.get_bitmask ());
619 16477565 : return true;
620 : }
621 :
622 : // Cast a pointer to an integer.
623 :
624 : bool
625 11555189 : operator_cast::fold_range (irange &r, tree type,
626 : const prange &inner,
627 : const irange &outer,
628 : relation_trio) const
629 : {
630 11555189 : if (empty_range_varying (r, type, inner, outer))
631 5684007 : return true;
632 :
633 : // Represent INNER as an integer of the same size, and then cast it
634 : // to the resulting integer type.
635 5871182 : tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (inner.type ()));
636 5871182 : r.set (pointer_uint_type, inner.lower_bound (), inner.upper_bound ());
637 5871182 : r.update_bitmask (inner.get_bitmask ());
638 5871182 : range_cast (r, type);
639 5871182 : return true;
640 : }
641 :
642 : // Cast an integer to a pointer.
643 :
644 : bool
645 1917844 : operator_cast::fold_range (prange &r, tree type,
646 : const irange &inner,
647 : const prange &outer,
648 : relation_trio) const
649 : {
650 1917844 : if (empty_range_varying (r, type, inner, outer))
651 1761 : return true;
652 :
653 : // Cast INNER to an integer of the same size as the pointer we want,
654 : // and then copy the bounds to the resulting pointer range.
655 1916083 : int_range<2> tmp = inner;
656 1916083 : tree pointer_uint_type = make_unsigned_type (TYPE_PRECISION (type));
657 1916083 : range_cast (tmp, pointer_uint_type);
658 : // Casts may cause ranges to become UNDEFINED based on bitmasks.
659 1916083 : if (tmp.undefined_p ())
660 0 : r.set_varying (type);
661 : else
662 : {
663 1916083 : r.set (type, tmp.lower_bound (), tmp.upper_bound ());
664 1916083 : r.update_bitmask (tmp.get_bitmask ());
665 : }
666 1916083 : return true;
667 1916083 : }
668 :
669 : bool
670 69 : operator_cast::op1_range (prange &r, tree type,
671 : const prange &lhs,
672 : const prange &op2,
673 : relation_trio trio) const
674 : {
675 69 : if (lhs.undefined_p ())
676 : return false;
677 69 : gcc_checking_assert (types_compatible_p (op2.type(), type));
678 :
679 : // Conversion from other pointers or a constant (including 0/NULL)
680 : // are straightforward.
681 69 : if (POINTER_TYPE_P (lhs.type ())
682 69 : || (lhs.singleton_p ()
683 0 : && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
684 69 : fold_range (r, type, lhs, op2, trio);
685 : else
686 : {
687 : // If the LHS is not a pointer nor a singleton, then it is
688 : // either VARYING or non-zero.
689 0 : if (!lhs.undefined_p () && !range_includes_zero_p (lhs))
690 0 : r.set_nonzero (type);
691 : else
692 0 : r.set_varying (type);
693 : }
694 69 : r.intersect (op2);
695 69 : return true;
696 : }
697 :
698 : bool
699 147285 : operator_cast::op1_range (irange &r, tree type,
700 : const prange &lhs,
701 : const irange &op2,
702 : relation_trio trio) const
703 : {
704 147285 : if (lhs.undefined_p ())
705 : return false;
706 147285 : gcc_checking_assert (types_compatible_p (op2.type(), type));
707 :
708 : // Conversion from other pointers or a constant (including 0/NULL)
709 : // are straightforward.
710 147285 : if (POINTER_TYPE_P (lhs.type ())
711 147285 : || (lhs.singleton_p ()
712 0 : && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
713 147285 : fold_range (r, type, lhs, op2, trio);
714 : else
715 : {
716 : // If the LHS is not a pointer nor a singleton, then it is
717 : // either VARYING or non-zero.
718 0 : if (!lhs.undefined_p () && !range_includes_zero_p (lhs))
719 0 : r.set_nonzero (type);
720 : else
721 0 : r.set_varying (type);
722 : }
723 147285 : r.intersect (op2);
724 147285 : return true;
725 : }
726 :
727 : bool
728 264320 : operator_cast::op1_range (prange &r, tree type,
729 : const irange &lhs,
730 : const prange &op2,
731 : relation_trio trio) const
732 : {
733 264320 : if (lhs.undefined_p ())
734 : return false;
735 264320 : gcc_checking_assert (types_compatible_p (op2.type(), type));
736 :
737 : // Conversion from other pointers or a constant (including 0/NULL)
738 : // are straightforward.
739 528640 : if (POINTER_TYPE_P (lhs.type ())
740 528640 : || (lhs.singleton_p ()
741 1250 : && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
742 1244 : fold_range (r, type, lhs, op2, trio);
743 : else
744 : {
745 : // If the LHS is not a pointer nor a singleton, then it is
746 : // either VARYING or non-zero.
747 263076 : if (!lhs.undefined_p () && !range_includes_zero_p (lhs))
748 148932 : r.set_nonzero (type);
749 : else
750 114144 : r.set_varying (type);
751 : }
752 264320 : r.intersect (op2);
753 264320 : return true;
754 : }
755 :
756 : relation_kind
757 137495 : operator_cast::lhs_op1_relation (const prange &lhs,
758 : const prange &op1,
759 : const prange &op2 ATTRIBUTE_UNUSED,
760 : relation_kind) const
761 : {
762 137495 : if (lhs.undefined_p () || op1.undefined_p ())
763 : return VREL_VARYING;
764 137495 : unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
765 137495 : unsigned op1_prec = TYPE_PRECISION (op1.type ());
766 : // If the result gets sign extended into a larger type check first if this
767 : // qualifies as a partial equivalence.
768 137495 : if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
769 : {
770 : // If the result is sign extended, and the LHS is larger than op1,
771 : // check if op1's range can be negative as the sign extension will
772 : // cause the upper bits to be 1 instead of 0, invalidating the PE.
773 0 : int_range<3> negs = range_negatives (op1.type ());
774 0 : negs.intersect (op1);
775 0 : if (!negs.undefined_p ())
776 0 : return VREL_VARYING;
777 0 : }
778 :
779 : // If the pointer precisions are the same, check for equality and
780 : // inequality in the points to fields.
781 137495 : if (lhs_prec == op1_prec)
782 : {
783 137495 : if (lhs.addr_same_known_p (op1))
784 : return VREL_EQ;
785 137345 : if (lhs.addr_inverted_p (op1))
786 : return VREL_NE;
787 : }
788 137345 : unsigned prec = MIN (lhs_prec, op1_prec);
789 137345 : return bits_to_pe (prec);
790 : }
791 :
792 : relation_kind
793 1441618 : operator_cast::lhs_op1_relation (const prange &lhs,
794 : const irange &op1,
795 : const irange &op2 ATTRIBUTE_UNUSED,
796 : relation_kind) const
797 : {
798 1441618 : if (lhs.undefined_p () || op1.undefined_p ())
799 : return VREL_VARYING;
800 1439861 : unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
801 1439861 : unsigned op1_prec = TYPE_PRECISION (op1.type ());
802 : // If the result gets sign extended into a larger type check first if this
803 : // qualifies as a partial equivalence.
804 1439861 : if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
805 : {
806 : // If the result is sign extended, and the LHS is larger than op1,
807 : // check if op1's range can be negative as the sign extension will
808 : // cause the upper bits to be 1 instead of 0, invalidating the PE.
809 458 : int_range<3> negs = range_negatives (op1.type ());
810 458 : negs.intersect (op1);
811 458 : if (!negs.undefined_p ())
812 418 : return VREL_VARYING;
813 458 : }
814 :
815 1439443 : unsigned prec = MIN (lhs_prec, op1_prec);
816 1439443 : return bits_to_pe (prec);
817 : }
818 :
819 : relation_kind
820 2348133 : operator_cast::lhs_op1_relation (const irange &lhs,
821 : const prange &op1,
822 : const prange &op2 ATTRIBUTE_UNUSED,
823 : relation_kind) const
824 : {
825 2348133 : if (lhs.undefined_p () || op1.undefined_p ())
826 : return VREL_VARYING;
827 2347134 : unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
828 2347134 : unsigned op1_prec = TYPE_PRECISION (op1.type ());
829 : // If the result gets sign extended into a larger type check first if this
830 : // qualifies as a partial equivalence.
831 2347134 : if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
832 : {
833 : // If the result is sign extended, and the LHS is larger than op1,
834 : // check if op1's range can be negative as the sign extension will
835 : // cause the upper bits to be 1 instead of 0, invalidating the PE.
836 0 : int_range<3> negs = range_negatives (op1.type ());
837 0 : negs.intersect (op1);
838 0 : if (!negs.undefined_p ())
839 0 : return VREL_VARYING;
840 0 : }
841 :
842 2347134 : unsigned prec = MIN (lhs_prec, op1_prec);
843 2347134 : return bits_to_pe (prec);
844 : }
845 :
846 : bool
847 1155 : operator_min::fold_range (prange &r, tree type,
848 : const prange &op1,
849 : const prange &op2,
850 : relation_trio) const
851 : {
852 : // For MIN/MAX expressions with pointers, we only care about
853 : // nullness. If both are non null, then the result is nonnull.
854 : // If both are null, then the result is null. Otherwise they
855 : // are varying.
856 1155 : if (!range_includes_zero_p (op1)
857 1155 : && !range_includes_zero_p (op2))
858 148 : r.set_nonzero (type);
859 1007 : else if (op1.zero_p () && op2.zero_p ())
860 0 : r.set_zero (type);
861 : else
862 1007 : r.set_varying (type);
863 :
864 1155 : update_known_bitmask (r, MIN_EXPR, op1, op2);
865 1155 : return true;
866 : }
867 :
868 : bool
869 1140 : operator_max::fold_range (prange &r, tree type,
870 : const prange &op1,
871 : const prange &op2,
872 : relation_trio) const
873 : {
874 : // For MIN/MAX expressions with pointers, we only care about
875 : // nullness. If both are non null, then the result is nonnull.
876 : // If both are null, then the result is null. Otherwise they
877 : // are varying.
878 1140 : if (!range_includes_zero_p (op1)
879 1140 : && !range_includes_zero_p (op2))
880 114 : r.set_nonzero (type);
881 1026 : else if (op1.zero_p () && op2.zero_p ())
882 0 : r.set_zero (type);
883 : else
884 1026 : r.set_varying (type);
885 :
886 1140 : update_known_bitmask (r, MAX_EXPR, op1, op2);
887 1140 : return true;
888 : }
889 :
890 : bool
891 26172 : operator_addr_expr::op1_range (prange &r, tree type,
892 : const prange &lhs,
893 : const prange &op2,
894 : relation_trio) const
895 : {
896 26172 : if (empty_range_varying (r, type, lhs, op2))
897 0 : return true;
898 :
899 : // Return a non-null pointer of the LHS type (passed in op2), but only
900 : // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
901 : // See PR 111009.
902 26172 : if (!lhs.undefined_p ()
903 26172 : && !range_includes_zero_p (lhs)
904 22719 : && TYPE_OVERFLOW_UNDEFINED (type))
905 22703 : r.set_nonzero (type);
906 : else
907 3469 : r.set_varying (type);
908 : return true;
909 : }
910 :
911 : bool
912 879 : operator_bitwise_and::fold_range (prange &r, tree type,
913 : const prange &op1,
914 : const prange &op2 ATTRIBUTE_UNUSED,
915 : relation_trio) const
916 : {
917 : // For pointer types, we are really only interested in asserting
918 : // whether the expression evaluates to non-NULL.
919 879 : if (op1.zero_p () || op2.zero_p ())
920 0 : r.set_zero (type);
921 : else
922 879 : r.set_varying (type);
923 :
924 879 : update_known_bitmask (r, BIT_AND_EXPR, op1, op2);
925 879 : return true;
926 : }
927 :
928 : bool
929 6648150 : operator_equal::fold_range (irange &r, tree type,
930 : const prange &op1,
931 : const prange &op2,
932 : relation_trio rel) const
933 : {
934 6648150 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
935 : return true;
936 :
937 : // We can be sure the values are always equal or not if both ranges
938 : // consist of a single value, and then compare them.
939 6625779 : bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
940 6625779 : bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
941 : // Check for points to equality and inequality first.
942 6625779 : if (op1.addr_same_known_p (op2))
943 13096 : r = range_true (type);
944 6612683 : else if (op1.addr_inverted_p (op2))
945 9021 : r = range_false (type);
946 6603662 : else if (op1_const && op2_const)
947 : {
948 31980 : if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
949 31811 : r = range_true (type);
950 : else
951 169 : r = range_false (type);
952 : }
953 : else
954 : {
955 : // If ranges do not intersect, we know the range is not equal,
956 : // otherwise we don't know anything for sure.
957 6571682 : prange tmp = op1;
958 6571682 : tmp.intersect (op2);
959 6571682 : if (tmp.undefined_p ())
960 177941 : r = range_false (type);
961 : // Check if a constant cannot satisfy the bitmask requirements.
962 11222643 : else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
963 0 : r = range_false (type);
964 6402127 : else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
965 0 : r = range_false (type);
966 : else
967 6393741 : r = range_true_and_false (type);
968 6571682 : }
969 :
970 : //update_known_bitmask (r, EQ_EXPR, op1, op2);
971 : return true;
972 : }
973 :
974 : bool
975 4302975 : operator_equal::op1_range (prange &r, tree type,
976 : const irange &lhs,
977 : const prange &op2,
978 : relation_trio) const
979 : {
980 4302975 : switch (get_bool_state (r, lhs, type))
981 : {
982 1195165 : case BRS_TRUE:
983 : // If it's true, the result is the same as OP2.
984 1195165 : r = op2;
985 1195165 : break;
986 :
987 3107810 : case BRS_FALSE:
988 : // If the result is false, the only time we know anything is
989 : // if OP2 is a constant, or refers to a known object.
990 3107810 : if (!op2.undefined_p ()
991 9323430 : && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
992 : {
993 1054403 : r = op2;
994 1054403 : if (!r.invert ())
995 : return false;
996 : }
997 : else
998 : {
999 2053407 : r.set_varying (type);
1000 : // OP2 refers to OBJ, so OP1 does not refer to OBJ.
1001 2053407 : if (tree obj = op2.addr_known ())
1002 557065 : r.set_addr (obj, false);
1003 : }
1004 : break;
1005 :
1006 : default:
1007 : break;
1008 : }
1009 : return true;
1010 : }
1011 :
1012 : bool
1013 1407028 : operator_equal::op2_range (prange &r, tree type,
1014 : const irange &lhs,
1015 : const prange &op1,
1016 : relation_trio rel) const
1017 : {
1018 1407028 : return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1019 : }
1020 :
1021 : relation_kind
1022 5378009 : operator_equal::op1_op2_relation (const irange &lhs, const prange &,
1023 : const prange &) const
1024 : {
1025 5378009 : if (lhs.undefined_p ())
1026 : return VREL_UNDEFINED;
1027 :
1028 : // FALSE = op1 == op2 indicates NE_EXPR.
1029 5378009 : if (lhs.zero_p ())
1030 : return VREL_NE;
1031 :
1032 : // TRUE = op1 == op2 indicates EQ_EXPR.
1033 2440275 : if (!range_includes_zero_p (lhs))
1034 2439718 : return VREL_EQ;
1035 : return VREL_VARYING;
1036 : }
1037 :
1038 : bool
1039 8154879 : operator_not_equal::fold_range (irange &r, tree type,
1040 : const prange &op1,
1041 : const prange &op2,
1042 : relation_trio rel) const
1043 : {
1044 8154879 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
1045 : return true;
1046 :
1047 : // We can be sure the values are always equal or not if both ranges
1048 : // consist of a single value, and then compare them.
1049 8133610 : bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
1050 8133610 : bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
1051 : // Check for points to equality and inequality first.
1052 8133610 : if (op1.addr_inverted_p (op2))
1053 1244 : r = range_true (type);
1054 8132366 : else if (op1.addr_same_known_p (op2))
1055 2598 : r = range_false (type);
1056 8129768 : else if (op1_const && op2_const)
1057 : {
1058 61854 : if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
1059 182 : r = range_true (type);
1060 : else
1061 61672 : r = range_false (type);
1062 : }
1063 : else
1064 : {
1065 : // If ranges do not intersect, we know the range is not equal,
1066 : // otherwise we don't know anything for sure.
1067 8067914 : prange tmp = op1;
1068 8067914 : tmp.intersect (op2);
1069 8067914 : if (tmp.undefined_p ())
1070 197270 : r = range_true (type);
1071 : // Check if a constant cannot satisfy the bitmask requirements.
1072 16880662 : else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
1073 0 : r = range_true (type);
1074 7874350 : else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
1075 0 : r = range_true (type);
1076 : else
1077 7870644 : r = range_true_and_false (type);
1078 8067914 : }
1079 :
1080 : //update_known_bitmask (r, NE_EXPR, op1, op2);
1081 : return true;
1082 : }
1083 :
1084 : bool
1085 5329226 : operator_not_equal::op1_range (prange &r, tree type,
1086 : const irange &lhs,
1087 : const prange &op2,
1088 : relation_trio) const
1089 : {
1090 5329226 : switch (get_bool_state (r, lhs, type))
1091 : {
1092 4242650 : case BRS_TRUE:
1093 : // If the result is true, the only time we know anything is if
1094 : // OP2 is a constant, or refers to a known object.
1095 4242650 : if (!op2.undefined_p ()
1096 12727950 : && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1097 : {
1098 1692095 : r = op2;
1099 1692095 : if (!r.invert ())
1100 : return false;
1101 : }
1102 : else
1103 : {
1104 2550555 : r.set_varying (type);
1105 : // Check if op1 is known not to refer to op2 points to object.
1106 2550555 : if (tree obj = op2.addr_known ())
1107 487990 : r.set_addr (obj, false);
1108 : }
1109 : break;
1110 :
1111 1086576 : case BRS_FALSE:
1112 : // If it's false, the result is the same as OP2.
1113 1086576 : r = op2;
1114 1086576 : break;
1115 :
1116 : default:
1117 : break;
1118 : }
1119 : return true;
1120 : }
1121 :
1122 :
1123 : bool
1124 1601488 : operator_not_equal::op2_range (prange &r, tree type,
1125 : const irange &lhs,
1126 : const prange &op1,
1127 : relation_trio rel) const
1128 : {
1129 1601488 : return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1130 : }
1131 :
1132 : relation_kind
1133 6021310 : operator_not_equal::op1_op2_relation (const irange &lhs, const prange &,
1134 : const prange &) const
1135 : {
1136 6021310 : if (lhs.undefined_p ())
1137 : return VREL_UNDEFINED;
1138 :
1139 : // FALSE = op1 != op2 indicates EQ_EXPR.
1140 6021310 : if (lhs.zero_p ())
1141 : return VREL_EQ;
1142 :
1143 : // TRUE = op1 != op2 indicates NE_EXPR.
1144 4029849 : if (!range_includes_zero_p (lhs))
1145 4027953 : return VREL_NE;
1146 : return VREL_VARYING;
1147 : }
1148 :
1149 : bool
1150 396370 : operator_lt::fold_range (irange &r, tree type,
1151 : const prange &op1,
1152 : const prange &op2,
1153 : relation_trio rel) const
1154 : {
1155 396370 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
1156 : return true;
1157 :
1158 393988 : signop sign = TYPE_SIGN (op1.type ());
1159 393988 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1160 :
1161 393988 : if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
1162 0 : r = range_true (type);
1163 393988 : else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
1164 28 : r = range_false (type);
1165 : // Use nonzero bits to determine if < 0 is false.
1166 393960 : else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
1167 0 : r = range_false (type);
1168 : else
1169 393960 : r = range_true_and_false (type);
1170 :
1171 : //update_known_bitmask (r, LT_EXPR, op1, op2);
1172 : return true;
1173 : }
1174 :
1175 : bool
1176 311652 : operator_lt::op1_range (prange &r, tree type,
1177 : const irange &lhs,
1178 : const prange &op2,
1179 : relation_trio) const
1180 : {
1181 311652 : if (op2.undefined_p ())
1182 : return false;
1183 :
1184 311652 : switch (get_bool_state (r, lhs, type))
1185 : {
1186 258372 : case BRS_TRUE:
1187 258372 : build_lt (r, type, op2);
1188 258372 : break;
1189 :
1190 53280 : case BRS_FALSE:
1191 53280 : build_ge (r, type, op2);
1192 53280 : break;
1193 :
1194 : default:
1195 : break;
1196 : }
1197 : return true;
1198 : }
1199 :
1200 : bool
1201 215916 : operator_lt::op2_range (prange &r, tree type,
1202 : const irange &lhs,
1203 : const prange &op1,
1204 : relation_trio) const
1205 : {
1206 215916 : if (op1.undefined_p ())
1207 : return false;
1208 :
1209 215916 : switch (get_bool_state (r, lhs, type))
1210 : {
1211 146813 : case BRS_TRUE:
1212 146813 : build_gt (r, type, op1);
1213 146813 : break;
1214 :
1215 69103 : case BRS_FALSE:
1216 69103 : build_le (r, type, op1);
1217 69103 : break;
1218 :
1219 : default:
1220 : break;
1221 : }
1222 : return true;
1223 : }
1224 :
1225 : relation_kind
1226 885597 : operator_lt::op1_op2_relation (const irange &lhs, const prange &,
1227 : const prange &) const
1228 : {
1229 885597 : if (lhs.undefined_p ())
1230 : return VREL_UNDEFINED;
1231 :
1232 : // FALSE = op1 < op2 indicates GE_EXPR.
1233 885597 : if (lhs.zero_p ())
1234 : return VREL_GE;
1235 :
1236 : // TRUE = op1 < op2 indicates LT_EXPR.
1237 634668 : if (!range_includes_zero_p (lhs))
1238 634614 : return VREL_LT;
1239 : return VREL_VARYING;
1240 : }
1241 :
1242 : bool
1243 125094 : operator_le::fold_range (irange &r, tree type,
1244 : const prange &op1,
1245 : const prange &op2,
1246 : relation_trio rel) const
1247 : {
1248 125094 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1249 : return true;
1250 :
1251 124698 : signop sign = TYPE_SIGN (op1.type ());
1252 124698 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1253 :
1254 124698 : if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
1255 40 : r = range_true (type);
1256 124658 : else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
1257 12 : r = range_false (type);
1258 : else
1259 124646 : r = range_true_and_false (type);
1260 :
1261 : //update_known_bitmask (r, LE_EXPR, op1, op2);
1262 : return true;
1263 : }
1264 :
1265 : bool
1266 97189 : operator_le::op1_range (prange &r, tree type,
1267 : const irange &lhs,
1268 : const prange &op2,
1269 : relation_trio) const
1270 : {
1271 97189 : if (op2.undefined_p ())
1272 : return false;
1273 :
1274 97189 : switch (get_bool_state (r, lhs, type))
1275 : {
1276 73335 : case BRS_TRUE:
1277 73335 : build_le (r, type, op2);
1278 73335 : break;
1279 :
1280 23854 : case BRS_FALSE:
1281 23854 : build_gt (r, type, op2);
1282 23854 : break;
1283 :
1284 : default:
1285 : break;
1286 : }
1287 : return true;
1288 : }
1289 :
1290 : bool
1291 133509 : operator_le::op2_range (prange &r, tree type,
1292 : const irange &lhs,
1293 : const prange &op1,
1294 : relation_trio) const
1295 : {
1296 133509 : if (op1.undefined_p ())
1297 : return false;
1298 :
1299 133509 : switch (get_bool_state (r, lhs, type))
1300 : {
1301 109057 : case BRS_TRUE:
1302 109057 : build_ge (r, type, op1);
1303 109057 : break;
1304 :
1305 24452 : case BRS_FALSE:
1306 24452 : build_lt (r, type, op1);
1307 24452 : break;
1308 :
1309 : default:
1310 : break;
1311 : }
1312 : return true;
1313 : }
1314 :
1315 : relation_kind
1316 383813 : operator_le::op1_op2_relation (const irange &lhs, const prange &,
1317 : const prange &) const
1318 : {
1319 383813 : if (lhs.undefined_p ())
1320 : return VREL_UNDEFINED;
1321 :
1322 : // FALSE = op1 <= op2 indicates GT_EXPR.
1323 383813 : if (lhs.zero_p ())
1324 : return VREL_GT;
1325 :
1326 : // TRUE = op1 <= op2 indicates LE_EXPR.
1327 281757 : if (!range_includes_zero_p (lhs))
1328 281471 : return VREL_LE;
1329 : return VREL_VARYING;
1330 : }
1331 :
1332 : bool
1333 416350 : operator_gt::fold_range (irange &r, tree type,
1334 : const prange &op1, const prange &op2,
1335 : relation_trio rel) const
1336 : {
1337 416350 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1338 : return true;
1339 :
1340 413824 : signop sign = TYPE_SIGN (op1.type ());
1341 413824 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1342 :
1343 413824 : if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1344 2 : r = range_true (type);
1345 413822 : else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1346 9 : r = range_false (type);
1347 : else
1348 413813 : r = range_true_and_false (type);
1349 :
1350 : //update_known_bitmask (r, GT_EXPR, op1, op2);
1351 : return true;
1352 : }
1353 :
1354 : bool
1355 337792 : operator_gt::op1_range (prange &r, tree type,
1356 : const irange &lhs, const prange &op2,
1357 : relation_trio) const
1358 : {
1359 337792 : if (op2.undefined_p ())
1360 : return false;
1361 :
1362 337792 : switch (get_bool_state (r, lhs, type))
1363 : {
1364 218027 : case BRS_TRUE:
1365 218027 : build_gt (r, type, op2);
1366 218027 : break;
1367 :
1368 119765 : case BRS_FALSE:
1369 119765 : build_le (r, type, op2);
1370 119765 : break;
1371 :
1372 : default:
1373 : break;
1374 : }
1375 : return true;
1376 : }
1377 :
1378 : bool
1379 282120 : operator_gt::op2_range (prange &r, tree type,
1380 : const irange &lhs,
1381 : const prange &op1,
1382 : relation_trio) const
1383 : {
1384 282120 : if (op1.undefined_p ())
1385 : return false;
1386 :
1387 282120 : switch (get_bool_state (r, lhs, type))
1388 : {
1389 207463 : case BRS_TRUE:
1390 207463 : build_lt (r, type, op1);
1391 207463 : break;
1392 :
1393 74657 : case BRS_FALSE:
1394 74657 : build_ge (r, type, op1);
1395 74657 : break;
1396 :
1397 : default:
1398 : break;
1399 : }
1400 : return true;
1401 : }
1402 :
1403 : relation_kind
1404 1014671 : operator_gt::op1_op2_relation (const irange &lhs, const prange &,
1405 : const prange &) const
1406 : {
1407 1014671 : if (lhs.undefined_p ())
1408 : return VREL_UNDEFINED;
1409 :
1410 : // FALSE = op1 > op2 indicates LE_EXPR.
1411 1014671 : if (lhs.zero_p ())
1412 : return VREL_LE;
1413 :
1414 : // TRUE = op1 > op2 indicates GT_EXPR.
1415 681988 : if (!range_includes_zero_p (lhs))
1416 681982 : return VREL_GT;
1417 : return VREL_VARYING;
1418 : }
1419 :
1420 : bool
1421 197953 : operator_ge::fold_range (irange &r, tree type,
1422 : const prange &op1,
1423 : const prange &op2,
1424 : relation_trio rel) const
1425 : {
1426 197953 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1427 : return true;
1428 :
1429 197591 : signop sign = TYPE_SIGN (op1.type ());
1430 197591 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1431 :
1432 197591 : if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1433 64 : r = range_true (type);
1434 197527 : else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1435 0 : r = range_false (type);
1436 : else
1437 197527 : r = range_true_and_false (type);
1438 :
1439 : //update_known_bitmask (r, GE_EXPR, op1, op2);
1440 : return true;
1441 : }
1442 :
1443 : bool
1444 200006 : operator_ge::op1_range (prange &r, tree type,
1445 : const irange &lhs,
1446 : const prange &op2,
1447 : relation_trio) const
1448 : {
1449 200006 : if (op2.undefined_p ())
1450 : return false;
1451 :
1452 200006 : switch (get_bool_state (r, lhs, type))
1453 : {
1454 172670 : case BRS_TRUE:
1455 172670 : build_ge (r, type, op2);
1456 172670 : break;
1457 :
1458 27336 : case BRS_FALSE:
1459 27336 : build_lt (r, type, op2);
1460 27336 : break;
1461 :
1462 : default:
1463 : break;
1464 : }
1465 : return true;
1466 : }
1467 :
1468 : bool
1469 97978 : operator_ge::op2_range (prange &r, tree type,
1470 : const irange &lhs,
1471 : const prange &op1,
1472 : relation_trio) const
1473 : {
1474 97978 : if (op1.undefined_p ())
1475 : return false;
1476 :
1477 97978 : switch (get_bool_state (r, lhs, type))
1478 : {
1479 68721 : case BRS_TRUE:
1480 68721 : build_le (r, type, op1);
1481 68721 : break;
1482 :
1483 29257 : case BRS_FALSE:
1484 29257 : build_gt (r, type, op1);
1485 29257 : break;
1486 :
1487 : default:
1488 : break;
1489 : }
1490 : return true;
1491 : }
1492 :
1493 : relation_kind
1494 510309 : operator_ge::op1_op2_relation (const irange &lhs, const prange &,
1495 : const prange &) const
1496 : {
1497 510309 : if (lhs.undefined_p ())
1498 : return VREL_UNDEFINED;
1499 :
1500 : // FALSE = op1 >= op2 indicates LT_EXPR.
1501 510309 : if (lhs.zero_p ())
1502 : return VREL_LT;
1503 :
1504 : // TRUE = op1 >= op2 indicates GE_EXPR.
1505 381094 : if (!range_includes_zero_p (lhs))
1506 381094 : return VREL_GE;
1507 : return VREL_VARYING;
1508 : }
1509 :
1510 : // Initialize any pointer operators to the primary table
1511 :
1512 : void
1513 301808 : range_op_table::initialize_pointer_ops ()
1514 : {
1515 301808 : set (POINTER_PLUS_EXPR, op_pointer_plus);
1516 301808 : set (POINTER_DIFF_EXPR, op_pointer_diff);
1517 301808 : }
|