Debian Codegraph @

gcc/range-op-float.cc

raw
1 /* Floating point range operators.
2 Copyright (C) 2022-2025 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <[email protected]>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-iterator.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "wide-int.h"
46 #include "value-relation.h"
47 #include "range-op.h"
48 #include "range-op-mixed.h"
49
50 // Default definitions for floating point operators.
51
52 bool
53 range_operator::fold_range (frange &r, tree type,
54 const frange &op1, const frange &op2,
55 relation_trio trio) const
56 {
57 if (empty_range_varying (r, type, op1, op2))
58 return true;
59 if (op1.known_isnan () || op2.known_isnan ())
60 {
61 r.set_nan (type);
62 return true;
63 }
64
65 rv_fold (r, type,
66 op1.lower_bound (), op1.upper_bound (),
67 op2.lower_bound (), op2.upper_bound (), trio.op1_op2 ());
68
69 if (r.known_isnan ())
70 return true;
71 if (op1.maybe_isnan () || op2.maybe_isnan ())
72 r.update_nan ();
73
74 // If the result has overflowed and flag_trapping_math, folding this
75 // operation could elide an overflow or division by zero exception.
76 // Avoid returning a singleton +-INF, to keep the propagators (DOM
77 // and substitute_and_fold_engine) from folding. See PR107608.
78 if (flag_trapping_math
79 && MODE_HAS_INFINITIES (TYPE_MODE (type))
80 && r.known_isinf () && !op1.known_isinf () && !op2.known_isinf ())
81 {
82 REAL_VALUE_TYPE inf = r.lower_bound ();
83 if (real_isneg (&inf))
84 {
85 REAL_VALUE_TYPE min = real_min_representable (type);
86 r.set (type, inf, min);
87 }
88 else
89 {
90 REAL_VALUE_TYPE max = real_max_representable (type);
91 r.set (type, max, inf);
92 }
93 }
94
95 r.flush_denormals_to_zero ();
96
97 return true;
98 }
99
100 // For a given operation, fold two sets of ranges into [lb, ub].
101 // MAYBE_NAN is set to TRUE if, in addition to any result in LB or
102 // UB, the final range has the possibility of a NAN.
103 void
104 range_operator::rv_fold (frange &r, tree type,
105 const REAL_VALUE_TYPE &,
106 const REAL_VALUE_TYPE &,
107 const REAL_VALUE_TYPE &,
108 const REAL_VALUE_TYPE &, relation_kind) const
109 {
110 r.set (type, dconstninf, dconstinf, nan_state (true));
111 }
112
113 bool
114 range_operator::fold_range (irange &r ATTRIBUTE_UNUSED,
115 tree type ATTRIBUTE_UNUSED,
116 const frange &lh ATTRIBUTE_UNUSED,
117 const irange &rh ATTRIBUTE_UNUSED,
118 relation_trio) const
119 {
120 return false;
121 }
122
123 bool
124 range_operator::fold_range (irange &r ATTRIBUTE_UNUSED,
125 tree type ATTRIBUTE_UNUSED,
126 const frange &lh ATTRIBUTE_UNUSED,
127 const frange &rh ATTRIBUTE_UNUSED,
128 relation_trio) const
129 {
130 return false;
131 }
132
133 bool
134 range_operator::fold_range (frange &r ATTRIBUTE_UNUSED,
135 tree type ATTRIBUTE_UNUSED,
136 const irange &lh ATTRIBUTE_UNUSED,
137 const irange &rh ATTRIBUTE_UNUSED,
138 relation_trio) const
139 {
140 return false;
141 }
142
143 bool
144 range_operator::op1_range (frange &r ATTRIBUTE_UNUSED,
145 tree type ATTRIBUTE_UNUSED,
146 const frange &lhs ATTRIBUTE_UNUSED,
147 const frange &op2 ATTRIBUTE_UNUSED,
148 relation_trio) const
149 {
150 return false;
151 }
152
153 bool
154 range_operator::op1_range (frange &r ATTRIBUTE_UNUSED,
155 tree type ATTRIBUTE_UNUSED,
156 const irange &lhs ATTRIBUTE_UNUSED,
157 const frange &op2 ATTRIBUTE_UNUSED,
158 relation_trio) const
159 {
160 return false;
161 }
162
163 bool
164 range_operator::op2_range (frange &r ATTRIBUTE_UNUSED,
165 tree type ATTRIBUTE_UNUSED,
166 const frange &lhs ATTRIBUTE_UNUSED,
167 const frange &op1 ATTRIBUTE_UNUSED,
168 relation_trio) const
169 {
170 return false;
171 }
172
173 bool
174 range_operator::op2_range (frange &r ATTRIBUTE_UNUSED,
175 tree type ATTRIBUTE_UNUSED,
176 const irange &lhs ATTRIBUTE_UNUSED,
177 const frange &op1 ATTRIBUTE_UNUSED,
178 relation_trio) const
179 {
180 return false;
181 }
182
183 relation_kind
184 range_operator::lhs_op1_relation (const frange &lhs ATTRIBUTE_UNUSED,
185 const frange &op1 ATTRIBUTE_UNUSED,
186 const frange &op2 ATTRIBUTE_UNUSED,
187 relation_kind) const
188 {
189 return VREL_VARYING;
190 }
191
192 relation_kind
193 range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
194 const frange &op1 ATTRIBUTE_UNUSED,
195 const frange &op2 ATTRIBUTE_UNUSED,
196 relation_kind) const
197 {
198 return VREL_VARYING;
199 }
200
201 relation_kind
202 range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
203 const frange &op1 ATTRIBUTE_UNUSED,
204 const frange &op2 ATTRIBUTE_UNUSED,
205 relation_kind) const
206 {
207 return VREL_VARYING;
208 }
209
210 relation_kind
211 range_operator::lhs_op2_relation (const frange &lhs ATTRIBUTE_UNUSED,
212 const frange &op1 ATTRIBUTE_UNUSED,
213 const frange &op2 ATTRIBUTE_UNUSED,
214 relation_kind) const
215 {
216 return VREL_VARYING;
217 }
218
219 relation_kind
220 range_operator::op1_op2_relation (const irange &,
221 const frange &,
222 const frange &) const
223 {
224 return VREL_VARYING;
225 }
226
227
228 relation_kind
229 range_operator::op1_op2_relation (const frange &,
230 const frange &,
231 const frange &) const
232 {
233 return VREL_VARYING;
234 }
235
236 // Return TRUE if OP1 and OP2 may be a NAN.
237
238 static inline bool
239 maybe_isnan (const frange &op1, const frange &op2)
240 {
241 return op1.maybe_isnan () || op2.maybe_isnan ();
242 }
243
244 // Floating point version of relop_early_resolve that takes NANs into
245 // account.
246 //
247 // For relation opcodes, first try to see if the supplied relation
248 // forces a true or false result, and return that.
249 // Then check for undefined operands. If none of this applies,
250 // return false.
251 //
252 // TRIO are the relations between operands as they appear in the IL.
253 // MY_REL is the relation that corresponds to the operator being
254 // folded. For example, when attempting to fold x_3 == y_5, MY_REL is
255 // VREL_EQ, and if the statement is dominated by x_3 > y_5, then
256 // TRIO.op1_op2() is VREL_GT.
257
258 static inline bool
259 frelop_early_resolve (irange &r, tree type,
260 const frange &op1, const frange &op2,
261 relation_trio trio, relation_kind my_rel)
262 {
263 relation_kind rel = trio.op1_op2 ();
264
265 // If known relation is a complete subset of this relation, always
266 // return true. However, avoid doing this when NAN is a possibility
267 // as we'll incorrectly fold conditions:
268 //
269 // if (x_3 >= y_5)
270 // ;
271 // else
272 // ;; With NANs the relation here is basically VREL_UNLT, so we
273 // ;; can't fold the following:
274 // if (x_3 < y_5)
275 if (!maybe_isnan (op1, op2) && relation_union (rel, my_rel) == my_rel)
276 {
277 r = range_true (type);
278 return true;
279 }
280
281 // If known relation has no subset of this relation, always false.
282 if (relation_intersect (rel, my_rel) == VREL_UNDEFINED)
283 {
284 r = range_false (type);
285 return true;
286 }
287
288 // If either operand is undefined, return VARYING.
289 if (empty_range_varying (r, type, op1, op2))
290 return true;
291
292 return false;
293 }
294
295 // Set VALUE to its next real value, or INF if the operation overflows.
296
297 void
298 frange_nextafter (enum machine_mode mode,
299 REAL_VALUE_TYPE &value,
300 const REAL_VALUE_TYPE &inf)
301 {
302 if (MODE_COMPOSITE_P (mode)
303 && (real_isdenormal (&value, mode) || real_iszero (&value)))
304 {
305 // IBM extended denormals only have DFmode precision.
306 REAL_VALUE_TYPE tmp, tmp2;
307 real_convert (&tmp2, DFmode, &value);
308 real_nextafter (&tmp, REAL_MODE_FORMAT (DFmode), &tmp2, &inf);
309 real_convert (&value, mode, &tmp);
310 }
311 else
312 {
313 REAL_VALUE_TYPE tmp;
314 real_nextafter (&tmp, REAL_MODE_FORMAT (mode), &value, &inf);
315 value = tmp;
316 }
317 }
318
319 // Like real_arithmetic, but round the result to INF if the operation
320 // produced inexact results.
321 //
322 // ?? There is still one problematic case, i387. With
323 // -fexcess-precision=standard we perform most SF/DFmode arithmetic in
324 // XFmode (long_double_type_node), so that case is OK. But without
325 // -mfpmath=sse, all the SF/DFmode computations are in XFmode
326 // precision (64-bit mantissa) and only occasionally rounded to
327 // SF/DFmode (when storing into memory from the 387 stack). Maybe
328 // this is ok as well though it is just occasionally more precise. ??
329
330 void
331 frange_arithmetic (enum tree_code code, tree type,
332 REAL_VALUE_TYPE &result,
333 const REAL_VALUE_TYPE &op1,
334 const REAL_VALUE_TYPE &op2,
335 const REAL_VALUE_TYPE &inf)
336 {
337 REAL_VALUE_TYPE value;
338 enum machine_mode mode = TYPE_MODE (type);
339 bool mode_composite = MODE_COMPOSITE_P (mode);
340
341 bool inexact = real_arithmetic (&value, code, &op1, &op2);
342 real_convert (&result, mode, &value);
343
344 /* When rounding towards negative infinity, x + (-x) and
345 x - x is -0 rather than +0 real_arithmetic computes.
346 So, when we are looking for lower bound (inf is negative),
347 use -0 rather than +0. */
348 if (flag_rounding_math
349 && (code == PLUS_EXPR || code == MINUS_EXPR)
350 && !inexact
351 && real_iszero (&result)
352 && !real_isneg (&result)
353 && real_isneg (&inf))
354 {
355 REAL_VALUE_TYPE op2a = op2;
356 if (code == PLUS_EXPR)
357 op2a.sign ^= 1;
358 if (real_isneg (&op1) == real_isneg (&op2a) && real_equal (&op1, &op2a))
359 result.sign = 1;
360 }
361
362 // Be extra careful if there may be discrepancies between the
363 // compile and runtime results.
364 bool round = false;
365 if (mode_composite)
366 round = true;
367 else
368 {
369 bool low = real_isneg (&inf);
370 round = (low ? !real_less (&result, &value)
371 : !real_less (&value, &result));
372 if (real_isinf (&result, !low)
373 && !real_isinf (&value)
374 && !flag_rounding_math)
375 {
376 // Use just [+INF, +INF] rather than [MAX, +INF]
377 // even if value is larger than MAX and rounds to
378 // nearest to +INF. Similarly just [-INF, -INF]
379 // rather than [-INF, +MAX] even if value is smaller
380 // than -MAX and rounds to nearest to -INF.
381 // Unless INEXACT is true, in that case we need some
382 // extra buffer.
383 if (!inexact)
384 round = false;
385 else
386 {
387 REAL_VALUE_TYPE tmp = result, tmp2;
388 frange_nextafter (mode, tmp, inf);
389 // TMP is at this point the maximum representable
390 // number.
391 real_arithmetic (&tmp2, MINUS_EXPR, &value, &tmp);
392 if (real_isneg (&tmp2) != low
393 && (REAL_EXP (&tmp2) - REAL_EXP (&tmp)
394 >= 2 - REAL_MODE_FORMAT (mode)->p))
395 round = false;
396 }
397 }
398 }
399 if (round && (inexact || !real_identical (&result, &value)))
400 {
401 if (mode_composite
402 && (real_isdenormal (&result, mode) || real_iszero (&result)))
403 {
404 // IBM extended denormals only have DFmode precision.
405 REAL_VALUE_TYPE tmp, tmp2;
406 real_convert (&tmp2, DFmode, &value);
407 real_nextafter (&tmp, REAL_MODE_FORMAT (DFmode), &tmp2, &inf);
408 real_convert (&result, mode, &tmp);
409 }
410 else
411 frange_nextafter (mode, result, inf);
412 }
413 if (mode_composite)
414 switch (code)
415 {
416 case PLUS_EXPR:
417 case MINUS_EXPR:
418 // ibm-ldouble-format documents 1ulp for + and -.
419 frange_nextafter (mode, result, inf);
420 break;
421 case MULT_EXPR:
422 // ibm-ldouble-format documents 2ulps for *.
423 frange_nextafter (mode, result, inf);
424 frange_nextafter (mode, result, inf);
425 break;
426 case RDIV_EXPR:
427 // ibm-ldouble-format documents 3ulps for /.
428 frange_nextafter (mode, result, inf);
429 frange_nextafter (mode, result, inf);
430 frange_nextafter (mode, result, inf);
431 break;
432 default:
433 break;
434 }
435 }
436
437 // Crop R to [-INF, MAX] where MAX is the maximum representable number
438 // for TYPE.
439
440 static inline void
441 frange_drop_inf (frange &r, tree type)
442 {
443 REAL_VALUE_TYPE max = real_max_representable (type);
444 frange tmp (type, r.lower_bound (), max);
445 r.intersect (tmp);
446 }
447
448 // Crop R to [MIN, +INF] where MIN is the minimum representable number
449 // for TYPE.
450
451 static inline void
452 frange_drop_ninf (frange &r, tree type)
453 {
454 REAL_VALUE_TYPE min = real_min_representable (type);
455 frange tmp (type, min, r.upper_bound ());
456 r.intersect (tmp);
457 }
458
459 // Crop R to [MIN, MAX] where MAX is the maximum representable number
460 // for TYPE and MIN the minimum representable number for TYPE.
461
462 static inline void
463 frange_drop_infs (frange &r, tree type)
464 {
465 REAL_VALUE_TYPE max = real_max_representable (type);
466 REAL_VALUE_TYPE min = real_min_representable (type);
467 frange tmp (type, min, max);
468 r.intersect (tmp);
469 }
470
471 // If zero is in R, make sure both -0.0 and +0.0 are in the range.
472
473 static inline void
474 frange_add_zeros (frange &r, tree type)
475 {
476 if (r.undefined_p () || r.known_isnan ())
477 return;
478
479 if (HONOR_SIGNED_ZEROS (type)
480 && (real_iszero (&r.lower_bound ()) || real_iszero (&r.upper_bound ())))
481 {
482 frange zero;
483 zero.set_zero (type);
484 r.union_ (zero);
485 }
486 }
487
488 // Build a range that is <= VAL and store it in R. Return TRUE if
489 // further changes may be needed for R, or FALSE if R is in its final
490 // form.
491
492 static bool
493 build_le (frange &r, tree type, const frange &val)
494 {
495 gcc_checking_assert (!val.known_isnan ());
496
497 REAL_VALUE_TYPE ninf = frange_val_min (type);
498 r.set (type, ninf, val.upper_bound ());
499
500 // Add both zeros if there's the possibility of zero equality.
501 frange_add_zeros (r, type);
502
503 return true;
504 }
505
506 // Build a range that is < VAL and store it in R. Return TRUE if
507 // further changes may be needed for R, or FALSE if R is in its final
508 // form.
509
510 static bool
511 build_lt (frange &r, tree type, const frange &val)
512 {
513 gcc_checking_assert (!val.known_isnan ());
514
515 // < -INF is outside the range.
516 if (real_isinf (&val.upper_bound (), 1))
517 {
518 if (HONOR_NANS (type))
519 r.set_nan (type);
520 else
521 r.set_undefined ();
522 return false;
523 }
524
525 REAL_VALUE_TYPE ninf = frange_val_min (type);
526 REAL_VALUE_TYPE prev = val.upper_bound ();
527 machine_mode mode = TYPE_MODE (type);
528 // Default to the conservatively correct closed ranges for
529 // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
530 // !HONOR_INFINITIES, nextafter will yield -INF, but frange::set()
531 // will crop the range appropriately.
532 if (!MODE_COMPOSITE_P (mode))
533 frange_nextafter (mode, prev, ninf);
534 r.set (type, ninf, prev);
535 return true;
536 }
537
538 // Build a range that is >= VAL and store it in R. Return TRUE if
539 // further changes may be needed for R, or FALSE if R is in its final
540 // form.
541
542 static bool
543 build_ge (frange &r, tree type, const frange &val)
544 {
545 gcc_checking_assert (!val.known_isnan ());
546
547 REAL_VALUE_TYPE inf = frange_val_max (type);
548 r.set (type, val.lower_bound (), inf);
549
550 // Add both zeros if there's the possibility of zero equality.
551 frange_add_zeros (r, type);
552
553 return true;
554 }
555
556 // Build a range that is > VAL and store it in R. Return TRUE if
557 // further changes may be needed for R, or FALSE if R is in its final
558 // form.
559
560 static bool
561 build_gt (frange &r, tree type, const frange &val)
562 {
563 gcc_checking_assert (!val.known_isnan ());
564
565 // > +INF is outside the range.
566 if (real_isinf (&val.lower_bound (), 0))
567 {
568 if (HONOR_NANS (type))
569 r.set_nan (type);
570 else
571 r.set_undefined ();
572 return false;
573 }
574
575 REAL_VALUE_TYPE inf = frange_val_max (type);
576 REAL_VALUE_TYPE next = val.lower_bound ();
577 machine_mode mode = TYPE_MODE (type);
578 // Default to the conservatively correct closed ranges for
579 // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
580 // !HONOR_INFINITIES, nextafter will yield +INF, but frange::set()
581 // will crop the range appropriately.
582 if (!MODE_COMPOSITE_P (mode))
583 frange_nextafter (mode, next, inf);
584 r.set (type, next, inf);
585 return true;
586 }
587
588
589 bool
590 operator_identity::fold_range (frange &r, tree, const frange &op1,
591 const frange &, relation_trio) const
592 {
593 r = op1;
594 return true;
595 }
596
597 bool
598 operator_identity::op1_range (frange &r, tree, const frange &lhs,
599 const frange &, relation_trio) const
600 {
601 r = lhs;
602 return true;
603 }
604
605 bool
606 operator_cst::fold_range (frange &r, tree, const frange &op1,
607 const frange &, relation_trio) const
608 {
609 r = op1;
610 return true;
611 }
612
613 bool
614 operator_equal::op2_range (frange &r, tree type,
615 const irange &lhs, const frange &op1,
616 relation_trio rel) const
617 {
618 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
619 }
620
621 bool
622 operator_equal::fold_range (irange &r, tree type,
623 const frange &op1, const frange &op2,
624 relation_trio rel) const
625 {
626 if (frelop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
627 return true;
628
629 if (op1.known_isnan () || op2.known_isnan ())
630 r = range_false (type);
631 // We can be sure the values are always equal or not if both ranges
632 // consist of a single value, and then compare them.
633 else if (op1.singleton_p () && op2.singleton_p ())
634 {
635 if (op1 == op2)
636 r = range_true (type);
637 // If one operand is -0.0 and other 0.0, they are still equal.
638 else if (real_iszero (&op1.lower_bound ())
639 && real_iszero (&op2.lower_bound ()))
640 r = range_true (type);
641 else
642 r = range_false (type);
643 }
644 else if (real_iszero (&op1.lower_bound ())
645 && real_iszero (&op1.upper_bound ())
646 && real_iszero (&op2.lower_bound ())
647 && real_iszero (&op2.upper_bound ())
648 && !maybe_isnan (op1, op2))
649 // [-0.0, 0.0] == [-0.0, 0.0] or similar.
650 r = range_true (type);
651 else
652 {
653 // If ranges do not intersect, we know the range is not equal,
654 // otherwise we don't know anything for sure.
655 frange tmp = op1;
656 tmp.intersect (op2);
657 if (tmp.undefined_p ())
658 {
659 // If one range is [whatever, -0.0] and another
660 // [0.0, whatever2], we don't know anything either,
661 // because -0.0 == 0.0.
662 if ((real_iszero (&op1.upper_bound ())
663 && real_iszero (&op2.lower_bound ()))
664 || (real_iszero (&op1.lower_bound ())
665 && real_iszero (&op2.upper_bound ())))
666 r = range_true_and_false (type);
667 else
668 r = range_false (type);
669 }
670 else
671 r = range_true_and_false (type);
672 }
673 return true;
674 }
675
676 bool
677 operator_equal::op1_range (frange &r, tree type,
678 const irange &lhs,
679 const frange &op2,
680 relation_trio trio) const
681 {
682 relation_kind rel = trio.op1_op2 ();
683 switch (get_bool_state (r, lhs, type))
684 {
685 case BRS_TRUE:
686 // The TRUE side of x == NAN is unreachable.
687 if (op2.known_isnan ())
688 r.set_undefined ();
689 else
690 {
691 // If it's true, the result is the same as OP2.
692 r = op2;
693 // Add both zeros if there's the possibility of zero equality.
694 frange_add_zeros (r, type);
695 // The TRUE side of op1 == op2 implies op1 is !NAN.
696 r.clear_nan ();
697 }
698 break;
699
700 case BRS_FALSE:
701 // The FALSE side of op1 == op1 implies op1 is a NAN.
702 if (rel == VREL_EQ)
703 r.set_nan (type);
704 // On the FALSE side of x == NAN, we know nothing about x.
705 else if (op2.known_isnan ())
706 r.set_varying (type);
707 // If the result is false, the only time we know anything is
708 // if OP2 is a constant.
709 else if (op2.singleton_p ()
710 || (!op2.maybe_isnan () && op2.zero_p ()))
711 {
712 REAL_VALUE_TYPE tmp = op2.lower_bound ();
713 r.set (type, tmp, tmp, VR_ANTI_RANGE);
714 }
715 else
716 r.set_varying (type);
717 break;
718
719 default:
720 break;
721 }
722 return true;
723 }
724
725 // Check if the LHS range indicates a relation between OP1 and OP2.
726
727 relation_kind
728 operator_equal::op1_op2_relation (const irange &lhs, const frange &,
729 const frange &) const
730 {
731 if (lhs.undefined_p ())
732 return VREL_UNDEFINED;
733
734 // FALSE = op1 == op2 indicates NE_EXPR.
735 if (lhs.zero_p ())
736 return VREL_NE;
737
738 // TRUE = op1 == op2 indicates EQ_EXPR.
739 if (!contains_zero_p (lhs))
740 return VREL_EQ;
741 return VREL_VARYING;
742 }
743
744 bool
745 operator_not_equal::fold_range (irange &r, tree type,
746 const frange &op1, const frange &op2,
747 relation_trio trio) const
748 {
749 relation_kind rel = trio.op1_op2 ();
750
751 // VREL_NE & NE_EXPR is always true, even with NANs.
752 if (rel == VREL_NE)
753 {
754 r = range_true (type);
755 return true;
756 }
757 if (rel == VREL_EQ && maybe_isnan (op1, op2))
758 {
759 // Avoid frelop_early_resolve() below as it could fold to FALSE
760 // without regards to NANs. This would be incorrect if trying
761 // to fold x_5 != x_5 without prior knowledge of NANs.
762 // Still, if either operand is undefined, return VARYING.
763 if (empty_range_varying (r, type, op1, op2))
764 return true;
765 }
766 else if (frelop_early_resolve (r, type, op1, op2, trio, VREL_NE))
767 return true;
768
769 // x != NAN is always TRUE.
770 if (op1.known_isnan () || op2.known_isnan ())
771 r = range_true (type);
772 // We can be sure the values are always equal or not if both ranges
773 // consist of a single value, and then compare them.
774 else if (op1.singleton_p () && op2.singleton_p ())
775 {
776 if (op1 == op2)
777 r = range_false (type);
778 // If one operand is -0.0 and other 0.0, they are still equal.
779 else if (real_iszero (&op1.lower_bound ())
780 && real_iszero (&op2.lower_bound ()))
781 r = range_false (type);
782 else
783 r = range_true (type);
784 }
785 else if (real_iszero (&op1.lower_bound ())
786 && real_iszero (&op1.upper_bound ())
787 && real_iszero (&op2.lower_bound ())
788 && real_iszero (&op2.upper_bound ())
789 && !maybe_isnan (op1, op2))
790 // [-0.0, 0.0] != [-0.0, 0.0] or similar.
791 r = range_false (type);
792 else
793 {
794 // If ranges do not intersect, we know the range is not equal,
795 // otherwise we don't know anything for sure.
796 frange tmp = op1;
797 tmp.intersect (op2);
798 if (tmp.undefined_p ())
799 {
800 // If one range is [whatever, -0.0] and another
801 // [0.0, whatever2], we don't know anything either,
802 // because -0.0 == 0.0.
803 if ((real_iszero (&op1.upper_bound ())
804 && real_iszero (&op2.lower_bound ()))
805 || (real_iszero (&op1.lower_bound ())
806 && real_iszero (&op2.upper_bound ())))
807 r = range_true_and_false (type);
808 else
809 r = range_true (type);
810 }
811 else
812 r = range_true_and_false (type);
813 }
814 return true;
815 }
816
817 bool
818 operator_not_equal::op1_range (frange &r, tree type,
819 const irange &lhs,
820 const frange &op2,
821 relation_trio trio) const
822 {
823 relation_kind rel = trio.op1_op2 ();
824 switch (get_bool_state (r, lhs, type))
825 {
826 case BRS_TRUE:
827 // If the result is true, the only time we know anything is if
828 // OP2 is a constant.
829 if (op2.singleton_p ())
830 {
831 // This is correct even if op1 is NAN, because the following
832 // range would be ~[tmp, tmp] with the NAN property set to
833 // maybe (VARYING).
834 REAL_VALUE_TYPE tmp = op2.lower_bound ();
835 r.set (type, tmp, tmp, VR_ANTI_RANGE);
836 }
837 // The TRUE side of op1 != op1 implies op1 is NAN.
838 else if (rel == VREL_EQ)
839 r.set_nan (type);
840 else
841 r.set_varying (type);
842 break;
843
844 case BRS_FALSE:
845 // The FALSE side of x != NAN is impossible.
846 if (op2.known_isnan ())
847 r.set_undefined ();
848 else
849 {
850 // If it's false, the result is the same as OP2.
851 r = op2;
852 // Add both zeros if there's the possibility of zero equality.
853 frange_add_zeros (r, type);
854 // The FALSE side of op1 != op2 implies op1 is !NAN.
855 r.clear_nan ();
856 }
857 break;
858
859 default:
860 break;
861 }
862 return true;
863 }
864
865 bool
866 operator_not_equal::op2_range (frange &r, tree type,
867 const irange &lhs,
868 const frange &op1,
869 relation_trio trio) const
870 {
871 return op1_range (r, type, lhs, op1, trio);
872 }
873
874 // Check if the LHS range indicates a relation between OP1 and OP2.
875
876 relation_kind
877 operator_not_equal::op1_op2_relation (const irange &lhs, const frange &,
878 const frange &) const
879 {
880 if (lhs.undefined_p ())
881 return VREL_UNDEFINED;
882
883 // FALSE = op1 != op2 indicates EQ_EXPR.
884 if (lhs.zero_p ())
885 return VREL_EQ;
886
887 // TRUE = op1 != op2 indicates NE_EXPR.
888 if (!contains_zero_p (lhs))
889 return VREL_NE;
890 return VREL_VARYING;
891 }
892
893 bool
894 operator_lt::fold_range (irange &r, tree type,
895 const frange &op1, const frange &op2,
896 relation_trio trio) const
897 {
898 if (frelop_early_resolve (r, type, op1, op2, trio, VREL_LT))
899 return true;
900
901 if (op1.known_isnan ()
902 || op2.known_isnan ()
903 || !real_less (&op1.lower_bound (), &op2.upper_bound ()))
904 r = range_false (type);
905 else if (!maybe_isnan (op1, op2)
906 && real_less (&op1.upper_bound (), &op2.lower_bound ()))
907 r = range_true (type);
908 else
909 r = range_true_and_false (type);
910 return true;
911 }
912
913 bool
914 operator_lt::op1_range (frange &r,
915 tree type,
916 const irange &lhs,
917 const frange &op2,
918 relation_trio) const
919 {
920 switch (get_bool_state (r, lhs, type))
921 {
922 case BRS_TRUE:
923 // The TRUE side of x < NAN is unreachable.
924 if (op2.known_isnan ())
925 r.set_undefined ();
926 else if (op2.undefined_p ())
927 return false;
928 else if (build_lt (r, type, op2))
929 {
930 r.clear_nan ();
931 // x < y implies x is not +INF.
932 frange_drop_inf (r, type);
933 }
934 break;
935
936 case BRS_FALSE:
937 // On the FALSE side of x < NAN, we know nothing about x.
938 if (op2.maybe_isnan ())
939 r.set_varying (type);
940 else
941 build_ge (r, type, op2);
942 break;
943
944 default:
945 break;
946 }
947 return true;
948 }
949
950 bool
951 operator_lt::op2_range (frange &r,
952 tree type,
953 const irange &lhs,
954 const frange &op1,
955 relation_trio) const
956 {
957 switch (get_bool_state (r, lhs, type))
958 {
959 case BRS_TRUE:
960 // The TRUE side of NAN < x is unreachable.
961 if (op1.known_isnan ())
962 r.set_undefined ();
963 else if (op1.undefined_p ())
964 return false;
965 else if (build_gt (r, type, op1))
966 {
967 r.clear_nan ();
968 // x < y implies y is not -INF.
969 frange_drop_ninf (r, type);
970 }
971 break;
972
973 case BRS_FALSE:
974 // On the FALSE side of NAN < x, we know nothing about x.
975 if (op1.maybe_isnan ())
976 r.set_varying (type);
977 else
978 build_le (r, type, op1);
979 break;
980
981 default:
982 break;
983 }
984 return true;
985 }
986
987
988 // Check if the LHS range indicates a relation between OP1 and OP2.
989
990 relation_kind
991 operator_lt::op1_op2_relation (const irange &lhs, const frange &,
992 const frange &) const
993 {
994 if (lhs.undefined_p ())
995 return VREL_UNDEFINED;
996
997 // FALSE = op1 < op2 indicates GE_EXPR.
998 if (lhs.zero_p ())
999 return VREL_GE;
1000
1001 // TRUE = op1 < op2 indicates LT_EXPR.
1002 if (!contains_zero_p (lhs))
1003 return VREL_LT;
1004 return VREL_VARYING;
1005 }
1006
1007 bool
1008 operator_le::fold_range (irange &r, tree type,
1009 const frange &op1, const frange &op2,
1010 relation_trio rel) const
1011 {
1012 if (frelop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1013 return true;
1014
1015 if (op1.known_isnan ()
1016 || op2.known_isnan ()
1017 || !real_compare (LE_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
1018 r = range_false (type);
1019 else if (!maybe_isnan (op1, op2)
1020 && real_compare (LE_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
1021 r = range_true (type);
1022 else
1023 r = range_true_and_false (type);
1024 return true;
1025 }
1026
1027 bool
1028 operator_le::op1_range (frange &r,
1029 tree type,
1030 const irange &lhs,
1031 const frange &op2,
1032 relation_trio) const
1033 {
1034 switch (get_bool_state (r, lhs, type))
1035 {
1036 case BRS_TRUE:
1037 // The TRUE side of x <= NAN is unreachable.
1038 if (op2.known_isnan ())
1039 r.set_undefined ();
1040 else if (op2.undefined_p ())
1041 return false;
1042 else if (build_le (r, type, op2))
1043 r.clear_nan ();
1044 break;
1045
1046 case BRS_FALSE:
1047 // On the FALSE side of x <= NAN, we know nothing about x.
1048 if (op2.maybe_isnan ())
1049 r.set_varying (type);
1050 else
1051 build_gt (r, type, op2);
1052 break;
1053
1054 default:
1055 break;
1056 }
1057 return true;
1058 }
1059
1060 bool
1061 operator_le::op2_range (frange &r,
1062 tree type,
1063 const irange &lhs,
1064 const frange &op1,
1065 relation_trio) const
1066 {
1067 switch (get_bool_state (r, lhs, type))
1068 {
1069 case BRS_TRUE:
1070 // The TRUE side of NAN <= x is unreachable.
1071 if (op1.known_isnan ())
1072 r.set_undefined ();
1073 else if (op1.undefined_p ())
1074 return false;
1075 else if (build_ge (r, type, op1))
1076 r.clear_nan ();
1077 break;
1078
1079 case BRS_FALSE:
1080 // On the FALSE side of NAN <= x, we know nothing about x.
1081 if (op1.maybe_isnan ())
1082 r.set_varying (type);
1083 else if (op1.undefined_p ())
1084 return false;
1085 else
1086 build_lt (r, type, op1);
1087 break;
1088
1089 default:
1090 break;
1091 }
1092 return true;
1093 }
1094
1095 // Check if the LHS range indicates a relation between OP1 and OP2.
1096
1097 relation_kind
1098 operator_le::op1_op2_relation (const irange &lhs, const frange &,
1099 const frange &) const
1100 {
1101 if (lhs.undefined_p ())
1102 return VREL_UNDEFINED;
1103
1104 // FALSE = op1 <= op2 indicates GT_EXPR.
1105 if (lhs.zero_p ())
1106 return VREL_GT;
1107
1108 // TRUE = op1 <= op2 indicates LE_EXPR.
1109 if (!contains_zero_p (lhs))
1110 return VREL_LE;
1111 return VREL_VARYING;
1112 }
1113
1114 bool
1115 operator_gt::fold_range (irange &r, tree type,
1116 const frange &op1, const frange &op2,
1117 relation_trio trio) const
1118 {
1119 if (frelop_early_resolve (r, type, op1, op2, trio, VREL_GT))
1120 return true;
1121
1122 if (op1.known_isnan ()
1123 || op2.known_isnan ()
1124 || !real_compare (GT_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
1125 r = range_false (type);
1126 else if (!maybe_isnan (op1, op2)
1127 && real_compare (GT_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
1128 r = range_true (type);
1129 else
1130 r = range_true_and_false (type);
1131 return true;
1132 }
1133
1134 bool
1135 operator_gt::op1_range (frange &r,
1136 tree type,
1137 const irange &lhs,
1138 const frange &op2,
1139 relation_trio) const
1140 {
1141 switch (get_bool_state (r, lhs, type))
1142 {
1143 case BRS_TRUE:
1144 // The TRUE side of x > NAN is unreachable.
1145 if (op2.known_isnan ())
1146 r.set_undefined ();
1147 else if (op2.undefined_p ())
1148 return false;
1149 else if (build_gt (r, type, op2))
1150 {
1151 r.clear_nan ();
1152 // x > y implies x is not -INF.
1153 frange_drop_ninf (r, type);
1154 }
1155 break;
1156
1157 case BRS_FALSE:
1158 // On the FALSE side of x > NAN, we know nothing about x.
1159 if (op2.maybe_isnan ())
1160 r.set_varying (type);
1161 else if (op2.undefined_p ())
1162 return false;
1163 else
1164 build_le (r, type, op2);
1165 break;
1166
1167 default:
1168 break;
1169 }
1170 return true;
1171 }
1172
1173 bool
1174 operator_gt::op2_range (frange &r,
1175 tree type,
1176 const irange &lhs,
1177 const frange &op1,
1178 relation_trio) const
1179 {
1180 switch (get_bool_state (r, lhs, type))
1181 {
1182 case BRS_TRUE:
1183 // The TRUE side of NAN > x is unreachable.
1184 if (op1.known_isnan ())
1185 r.set_undefined ();
1186 else if (op1.undefined_p ())
1187 return false;
1188 else if (build_lt (r, type, op1))
1189 {
1190 r.clear_nan ();
1191 // x > y implies y is not +INF.
1192 frange_drop_inf (r, type);
1193 }
1194 break;
1195
1196 case BRS_FALSE:
1197 // On The FALSE side of NAN > x, we know nothing about x.
1198 if (op1.maybe_isnan ())
1199 r.set_varying (type);
1200 else if (op1.undefined_p ())
1201 return false;
1202 else
1203 build_ge (r, type, op1);
1204 break;
1205
1206 default:
1207 break;
1208 }
1209 return true;
1210 }
1211
1212 // Check if the LHS range indicates a relation between OP1 and OP2.
1213
1214 relation_kind
1215 operator_gt::op1_op2_relation (const irange &lhs, const frange &,
1216 const frange &) const
1217 {
1218 if (lhs.undefined_p ())
1219 return VREL_UNDEFINED;
1220
1221 // FALSE = op1 > op2 indicates LE_EXPR.
1222 if (lhs.zero_p ())
1223 return VREL_LE;
1224
1225 // TRUE = op1 > op2 indicates GT_EXPR.
1226 if (!contains_zero_p (lhs))
1227 return VREL_GT;
1228 return VREL_VARYING;
1229 }
1230
1231 bool
1232 operator_ge::fold_range (irange &r, tree type,
1233 const frange &op1, const frange &op2,
1234 relation_trio rel) const
1235 {
1236 if (frelop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1237 return true;
1238
1239 if (op1.known_isnan ()
1240 || op2.known_isnan ()
1241 || !real_compare (GE_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
1242 r = range_false (type);
1243 else if (!maybe_isnan (op1, op2)
1244 && real_compare (GE_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
1245 r = range_true (type);
1246 else
1247 r = range_true_and_false (type);
1248 return true;
1249 }
1250
1251 bool
1252 operator_ge::op1_range (frange &r,
1253 tree type,
1254 const irange &lhs,
1255 const frange &op2,
1256 relation_trio) const
1257 {
1258 switch (get_bool_state (r, lhs, type))
1259 {
1260 case BRS_TRUE:
1261 // The TRUE side of x >= NAN is unreachable.
1262 if (op2.known_isnan ())
1263 r.set_undefined ();
1264 else if (op2.undefined_p ())
1265 return false;
1266 else if (build_ge (r, type, op2))
1267 r.clear_nan ();
1268 break;
1269
1270 case BRS_FALSE:
1271 // On the FALSE side of x >= NAN, we know nothing about x.
1272 if (op2.maybe_isnan ())
1273 r.set_varying (type);
1274 else if (op2.undefined_p ())
1275 return false;
1276 else
1277 build_lt (r, type, op2);
1278 break;
1279
1280 default:
1281 break;
1282 }
1283 return true;
1284 }
1285
1286 bool
1287 operator_ge::op2_range (frange &r, tree type,
1288 const irange &lhs,
1289 const frange &op1,
1290 relation_trio) const
1291 {
1292 switch (get_bool_state (r, lhs, type))
1293 {
1294 case BRS_TRUE:
1295 // The TRUE side of NAN >= x is unreachable.
1296 if (op1.known_isnan ())
1297 r.set_undefined ();
1298 else if (op1.undefined_p ())
1299 return false;
1300 else if (build_le (r, type, op1))
1301 r.clear_nan ();
1302 break;
1303
1304 case BRS_FALSE:
1305 // On the FALSE side of NAN >= x, we know nothing about x.
1306 if (op1.maybe_isnan ())
1307 r.set_varying (type);
1308 else if (op1.undefined_p ())
1309 return false;
1310 else
1311 build_gt (r, type, op1);
1312 break;
1313
1314 default:
1315 break;
1316 }
1317 return true;
1318 }
1319
1320 // Check if the LHS range indicates a relation between OP1 and OP2.
1321
1322 relation_kind
1323 operator_ge::op1_op2_relation (const irange &lhs, const frange &,
1324 const frange &) const
1325 {
1326 if (lhs.undefined_p ())
1327 return VREL_UNDEFINED;
1328
1329 // FALSE = op1 >= op2 indicates LT_EXPR.
1330 if (lhs.zero_p ())
1331 return VREL_LT;
1332
1333 // TRUE = op1 >= op2 indicates GE_EXPR.
1334 if (!contains_zero_p (lhs))
1335 return VREL_GE;
1336 return VREL_VARYING;
1337 }
1338
1339 // UNORDERED_EXPR comparison.
1340
1341 class foperator_unordered : public range_operator
1342 {
1343 using range_operator::fold_range;
1344 using range_operator::op1_range;
1345 using range_operator::op2_range;
1346 public:
1347 bool fold_range (irange &r, tree type,
1348 const frange &op1, const frange &op2,
1349 relation_trio = TRIO_VARYING) const final override;
1350 bool op1_range (frange &r, tree type,
1351 const irange &lhs, const frange &op2,
1352 relation_trio = TRIO_VARYING) const final override;
1353 bool op2_range (frange &r, tree type,
1354 const irange &lhs, const frange &op1,
1355 relation_trio rel = TRIO_VARYING) const final override
1356 {
1357 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1358 }
1359 } fop_unordered;
1360
1361 bool
1362 foperator_unordered::fold_range (irange &r, tree type,
1363 const frange &op1, const frange &op2,
1364 relation_trio) const
1365 {
1366 // UNORDERED is TRUE if either operand is a NAN.
1367 if (op1.known_isnan () || op2.known_isnan ())
1368 r = range_true (type);
1369 // UNORDERED is FALSE if neither operand is a NAN.
1370 else if (!op1.maybe_isnan () && !op2.maybe_isnan ())
1371 r = range_false (type);
1372 else
1373 r = range_true_and_false (type);
1374 return true;
1375 }
1376
1377 bool
1378 foperator_unordered::op1_range (frange &r, tree type,
1379 const irange &lhs,
1380 const frange &op2,
1381 relation_trio trio) const
1382 {
1383 relation_kind rel = trio.op1_op2 ();
1384 switch (get_bool_state (r, lhs, type))
1385 {
1386 case BRS_TRUE:
1387 // Since at least one operand must be NAN, if one of them is
1388 // not, the other must be.
1389 if (rel == VREL_EQ || !op2.maybe_isnan ())
1390 r.set_nan (type);
1391 else
1392 r.set_varying (type);
1393 break;
1394
1395 case BRS_FALSE:
1396 // A false UNORDERED means both operands are !NAN, so it's
1397 // impossible for op2 to be a NAN.
1398 if (op2.known_isnan ())
1399 r.set_undefined ();
1400 else
1401 {
1402 r.set_varying (type);
1403 r.clear_nan ();
1404 }
1405 break;
1406
1407 default:
1408 break;
1409 }
1410 return true;
1411 }
1412
1413 // ORDERED_EXPR comparison.
1414
1415 class foperator_ordered : public range_operator
1416 {
1417 using range_operator::fold_range;
1418 using range_operator::op1_range;
1419 using range_operator::op2_range;
1420 public:
1421 bool fold_range (irange &r, tree type,
1422 const frange &op1, const frange &op2,
1423 relation_trio = TRIO_VARYING) const final override;
1424 bool op1_range (frange &r, tree type,
1425 const irange &lhs, const frange &op2,
1426 relation_trio = TRIO_VARYING) const final override;
1427 bool op2_range (frange &r, tree type,
1428 const irange &lhs, const frange &op1,
1429 relation_trio rel = TRIO_VARYING) const final override
1430 {
1431 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1432 }
1433 } fop_ordered;
1434
1435 bool
1436 foperator_ordered::fold_range (irange &r, tree type,
1437 const frange &op1, const frange &op2,
1438 relation_trio) const
1439 {
1440 if (op1.known_isnan () || op2.known_isnan ())
1441 r = range_false (type);
1442 else if (!op1.maybe_isnan () && !op2.maybe_isnan ())
1443 r = range_true (type);
1444 else
1445 r = range_true_and_false (type);
1446 return true;
1447 }
1448
1449 bool
1450 foperator_ordered::op1_range (frange &r, tree type,
1451 const irange &lhs,
1452 const frange &op2,
1453 relation_trio trio) const
1454 {
1455 relation_kind rel = trio.op1_op2 ();
1456 switch (get_bool_state (r, lhs, type))
1457 {
1458 case BRS_TRUE:
1459 // The TRUE side of ORDERED means both operands are !NAN, so
1460 // it's impossible for op2 to be a NAN.
1461 if (op2.known_isnan ())
1462 r.set_undefined ();
1463 else
1464 {
1465 r.set_varying (type);
1466 r.clear_nan ();
1467 }
1468 break;
1469
1470 case BRS_FALSE:
1471 // The FALSE side of op1 ORDERED op1 implies op1 is NAN.
1472 if (rel == VREL_EQ)
1473 r.set_nan (type);
1474 else
1475 r.set_varying (type);
1476 break;
1477
1478 default:
1479 break;
1480 }
1481 return true;
1482 }
1483
1484 bool
1485 operator_negate::fold_range (frange &r, tree type,
1486 const frange &op1, const frange &op2,
1487 relation_trio) const
1488 {
1489 if (empty_range_varying (r, type, op1, op2))
1490 return true;
1491 if (op1.known_isnan ())
1492 {
1493 bool sign;
1494 if (op1.nan_signbit_p (sign))
1495 r.set_nan (type, !sign);
1496 else
1497 r.set_nan (type);
1498 return true;
1499 }
1500
1501 REAL_VALUE_TYPE lh_lb = op1.lower_bound ();
1502 REAL_VALUE_TYPE lh_ub = op1.upper_bound ();
1503 lh_lb = real_value_negate (&lh_lb);
1504 lh_ub = real_value_negate (&lh_ub);
1505 r.set (type, lh_ub, lh_lb);
1506 if (op1.maybe_isnan ())
1507 {
1508 bool sign;
1509 if (op1.nan_signbit_p (sign))
1510 r.update_nan (!sign);
1511 else
1512 r.update_nan ();
1513 }
1514 else
1515 r.clear_nan ();
1516 return true;
1517 }
1518
1519 bool
1520 operator_negate::op1_range (frange &r, tree type,
1521 const frange &lhs, const frange &op2,
1522 relation_trio rel) const
1523 {
1524 return fold_range (r, type, lhs, op2, rel);
1525 }
1526
1527 bool
1528 operator_abs::fold_range (frange &r, tree type,
1529 const frange &op1, const frange &op2,
1530 relation_trio) const
1531 {
1532 if (empty_range_varying (r, type, op1, op2))
1533 return true;
1534 if (op1.known_isnan ())
1535 {
1536 r.set_nan (type, /*sign=*/false);
1537 return true;
1538 }
1539
1540 const REAL_VALUE_TYPE lh_lb = op1.lower_bound ();
1541 const REAL_VALUE_TYPE lh_ub = op1.upper_bound ();
1542 // Handle the easy case where everything is positive.
1543 if (real_compare (GE_EXPR, &lh_lb, &dconst0)
1544 && !real_iszero (&lh_lb, /*sign=*/true)
1545 && !op1.maybe_isnan (/*sign=*/true))
1546 {
1547 r = op1;
1548 return true;
1549 }
1550
1551 REAL_VALUE_TYPE min = real_value_abs (&lh_lb);
1552 REAL_VALUE_TYPE max = real_value_abs (&lh_ub);
1553 // If the range contains zero then we know that the minimum value in the
1554 // range will be zero.
1555 if (real_compare (LE_EXPR, &lh_lb, &dconst0)
1556 && real_compare (GE_EXPR, &lh_ub, &dconst0))
1557 {
1558 if (real_compare (GT_EXPR, &min, &max))
1559 max = min;
1560 min = dconst0;
1561 }
1562 else
1563 {
1564 // If the range was reversed, swap MIN and MAX.
1565 if (real_compare (GT_EXPR, &min, &max))
1566 std::swap (min, max);
1567 }
1568
1569 r.set (type, min, max);
1570 if (op1.maybe_isnan ())
1571 r.update_nan (/*sign=*/false);
1572 else
1573 r.clear_nan ();
1574 return true;
1575 }
1576
1577 bool
1578 operator_abs::op1_range (frange &r, tree type,
1579 const frange &lhs, const frange &op2,
1580 relation_trio) const
1581 {
1582 if (empty_range_varying (r, type, lhs, op2))
1583 return true;
1584 if (lhs.known_isnan ())
1585 {
1586 r.set_nan (type);
1587 return true;
1588 }
1589
1590 // Start with the positives because negatives are an impossible result.
1591 frange positives (type, dconst0, frange_val_max (type));
1592 positives.update_nan (/*sign=*/false);
1593 positives.intersect (lhs);
1594 r = positives;
1595 // Add -NAN if relevant.
1596 if (r.maybe_isnan ())
1597 {
1598 frange neg_nan;
1599 neg_nan.set_nan (type, true);
1600 r.union_ (neg_nan);
1601 }
1602 if (r.known_isnan () || r.undefined_p ())
1603 return true;
1604 // Then add the negative of each pair:
1605 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
1606 frange negatives (type, real_value_negate (&positives.upper_bound ()),
1607 real_value_negate (&positives.lower_bound ()));
1608 negatives.clear_nan ();
1609 r.union_ (negatives);
1610 return true;
1611 }
1612
1613 class foperator_unordered_lt : public range_operator
1614 {
1615 using range_operator::fold_range;
1616 using range_operator::op1_range;
1617 using range_operator::op2_range;
1618 public:
1619 bool fold_range (irange &r, tree type,
1620 const frange &op1, const frange &op2,
1621 relation_trio trio = TRIO_VARYING) const final override
1622 {
1623 if (op1.known_isnan () || op2.known_isnan ())
1624 {
1625 r = range_true (type);
1626 return true;
1627 }
1628 frange op1_no_nan = op1;
1629 frange op2_no_nan = op2;
1630 if (op1.maybe_isnan ())
1631 op1_no_nan.clear_nan ();
1632 if (op2.maybe_isnan ())
1633 op2_no_nan.clear_nan ();
1634 if (!range_op_handler (LT_EXPR).fold_range (r, type, op1_no_nan,
1635 op2_no_nan, trio))
1636 return false;
1637 // The result is the same as the ordered version when the
1638 // comparison is true or when the operands cannot be NANs.
1639 if (!maybe_isnan (op1, op2) || r == range_true (type))
1640 return true;
1641 else
1642 {
1643 r = range_true_and_false (type);
1644 return true;
1645 }
1646 }
1647 bool op1_range (frange &r, tree type,
1648 const irange &lhs,
1649 const frange &op2,
1650 relation_trio trio) const final override;
1651 bool op2_range (frange &r, tree type,
1652 const irange &lhs,
1653 const frange &op1,
1654 relation_trio trio) const final override;
1655 } fop_unordered_lt;
1656
1657 bool
1658 foperator_unordered_lt::op1_range (frange &r, tree type,
1659 const irange &lhs,
1660 const frange &op2,
1661 relation_trio) const
1662 {
1663 switch (get_bool_state (r, lhs, type))
1664 {
1665 case BRS_TRUE:
1666 if (op2.maybe_isnan ())
1667 r.set_varying (type);
1668 else if (op2.undefined_p ())
1669 return false;
1670 else
1671 build_lt (r, type, op2);
1672 break;
1673
1674 case BRS_FALSE:
1675 // A false UNORDERED_LT means both operands are !NAN, so it's
1676 // impossible for op2 to be a NAN.
1677 if (op2.known_isnan ())
1678 r.set_undefined ();
1679 else if (op2.undefined_p ())
1680 return false;
1681 else if (build_ge (r, type, op2))
1682 r.clear_nan ();
1683 break;
1684
1685 default:
1686 break;
1687 }
1688 return true;
1689 }
1690
1691 bool
1692 foperator_unordered_lt::op2_range (frange &r, tree type,
1693 const irange &lhs,
1694 const frange &op1,
1695 relation_trio) const
1696 {
1697 switch (get_bool_state (r, lhs, type))
1698 {
1699 case BRS_TRUE:
1700 if (op1.maybe_isnan ())
1701 r.set_varying (type);
1702 else if (op1.undefined_p ())
1703 return false;
1704 else
1705 build_gt (r, type, op1);
1706 break;
1707
1708 case BRS_FALSE:
1709 // A false UNORDERED_LT means both operands are !NAN, so it's
1710 // impossible for op1 to be a NAN.
1711 if (op1.known_isnan ())
1712 r.set_undefined ();
1713 else if (op1.undefined_p ())
1714 return false;
1715 else if (build_le (r, type, op1))
1716 r.clear_nan ();
1717 break;
1718
1719 default:
1720 break;
1721 }
1722 return true;
1723 }
1724
1725 class foperator_unordered_le : public range_operator
1726 {
1727 using range_operator::fold_range;
1728 using range_operator::op1_range;
1729 using range_operator::op2_range;
1730 public:
1731 bool fold_range (irange &r, tree type,
1732 const frange &op1, const frange &op2,
1733 relation_trio trio = TRIO_VARYING) const final override
1734 {
1735 if (op1.known_isnan () || op2.known_isnan ())
1736 {
1737 r = range_true (type);
1738 return true;
1739 }
1740 frange op1_no_nan = op1;
1741 frange op2_no_nan = op2;
1742 if (op1.maybe_isnan ())
1743 op1_no_nan.clear_nan ();
1744 if (op2.maybe_isnan ())
1745 op2_no_nan.clear_nan ();
1746 if (!range_op_handler (LE_EXPR).fold_range (r, type, op1_no_nan,
1747 op2_no_nan, trio))
1748 return false;
1749 // The result is the same as the ordered version when the
1750 // comparison is true or when the operands cannot be NANs.
1751 if (!maybe_isnan (op1, op2) || r == range_true (type))
1752 return true;
1753 else
1754 {
1755 r = range_true_and_false (type);
1756 return true;
1757 }
1758 }
1759 bool op1_range (frange &r, tree type,
1760 const irange &lhs, const frange &op2,
1761 relation_trio = TRIO_VARYING) const final override;
1762 bool op2_range (frange &r, tree type,
1763 const irange &lhs, const frange &op1,
1764 relation_trio = TRIO_VARYING) const final override;
1765 } fop_unordered_le;
1766
1767 bool
1768 foperator_unordered_le::op1_range (frange &r, tree type,
1769 const irange &lhs, const frange &op2,
1770 relation_trio) const
1771 {
1772 switch (get_bool_state (r, lhs, type))
1773 {
1774 case BRS_TRUE:
1775 if (op2.maybe_isnan ())
1776 r.set_varying (type);
1777 else if (op2.undefined_p ())
1778 return false;
1779 else
1780 build_le (r, type, op2);
1781 break;
1782
1783 case BRS_FALSE:
1784 // A false UNORDERED_LE means both operands are !NAN, so it's
1785 // impossible for op2 to be a NAN.
1786 if (op2.known_isnan ())
1787 r.set_undefined ();
1788 else if (build_gt (r, type, op2))
1789 r.clear_nan ();
1790 break;
1791
1792 default:
1793 break;
1794 }
1795 return true;
1796 }
1797
1798 bool
1799 foperator_unordered_le::op2_range (frange &r,
1800 tree type,
1801 const irange &lhs,
1802 const frange &op1,
1803 relation_trio) const
1804 {
1805 switch (get_bool_state (r, lhs, type))
1806 {
1807 case BRS_TRUE:
1808 if (op1.maybe_isnan ())
1809 r.set_varying (type);
1810 else if (op1.undefined_p ())
1811 return false;
1812 else
1813 build_ge (r, type, op1);
1814 break;
1815
1816 case BRS_FALSE:
1817 // A false UNORDERED_LE means both operands are !NAN, so it's
1818 // impossible for op1 to be a NAN.
1819 if (op1.known_isnan ())
1820 r.set_undefined ();
1821 else if (op1.undefined_p ())
1822 return false;
1823 else if (build_lt (r, type, op1))
1824 r.clear_nan ();
1825 break;
1826
1827 default:
1828 break;
1829 }
1830 return true;
1831 }
1832
1833 class foperator_unordered_gt : public range_operator
1834 {
1835 using range_operator::fold_range;
1836 using range_operator::op1_range;
1837 using range_operator::op2_range;
1838 public:
1839 bool fold_range (irange &r, tree type,
1840 const frange &op1, const frange &op2,
1841 relation_trio trio = TRIO_VARYING) const final override
1842 {
1843 if (op1.known_isnan () || op2.known_isnan ())
1844 {
1845 r = range_true (type);
1846 return true;
1847 }
1848 frange op1_no_nan = op1;
1849 frange op2_no_nan = op2;
1850 if (op1.maybe_isnan ())
1851 op1_no_nan.clear_nan ();
1852 if (op2.maybe_isnan ())
1853 op2_no_nan.clear_nan ();
1854 if (!range_op_handler (GT_EXPR).fold_range (r, type, op1_no_nan,
1855 op2_no_nan, trio))
1856 return false;
1857 // The result is the same as the ordered version when the
1858 // comparison is true or when the operands cannot be NANs.
1859 if (!maybe_isnan (op1, op2) || r == range_true (type))
1860 return true;
1861 else
1862 {
1863 r = range_true_and_false (type);
1864 return true;
1865 }
1866 }
1867 bool op1_range (frange &r, tree type,
1868 const irange &lhs, const frange &op2,
1869 relation_trio = TRIO_VARYING) const final override;
1870 bool op2_range (frange &r, tree type,
1871 const irange &lhs, const frange &op1,
1872 relation_trio = TRIO_VARYING) const final override;
1873 } fop_unordered_gt;
1874
1875 bool
1876 foperator_unordered_gt::op1_range (frange &r,
1877 tree type,
1878 const irange &lhs,
1879 const frange &op2,
1880 relation_trio) const
1881 {
1882 switch (get_bool_state (r, lhs, type))
1883 {
1884 case BRS_TRUE:
1885 if (op2.maybe_isnan ())
1886 r.set_varying (type);
1887 else if (op2.undefined_p ())
1888 return false;
1889 else
1890 build_gt (r, type, op2);
1891 break;
1892
1893 case BRS_FALSE:
1894 // A false UNORDERED_GT means both operands are !NAN, so it's
1895 // impossible for op2 to be a NAN.
1896 if (op2.known_isnan ())
1897 r.set_undefined ();
1898 else if (op2.undefined_p ())
1899 return false;
1900 else if (build_le (r, type, op2))
1901 r.clear_nan ();
1902 break;
1903
1904 default:
1905 break;
1906 }
1907 return true;
1908 }
1909
1910 bool
1911 foperator_unordered_gt::op2_range (frange &r,
1912 tree type,
1913 const irange &lhs,
1914 const frange &op1,
1915 relation_trio) const
1916 {
1917 switch (get_bool_state (r, lhs, type))
1918 {
1919 case BRS_TRUE:
1920 if (op1.maybe_isnan ())
1921 r.set_varying (type);
1922 else if (op1.undefined_p ())
1923 return false;
1924 else
1925 build_lt (r, type, op1);
1926 break;
1927
1928 case BRS_FALSE:
1929 // A false UNORDERED_GT means both operands are !NAN, so it's
1930 // impossible for op1 to be a NAN.
1931 if (op1.known_isnan ())
1932 r.set_undefined ();
1933 else if (op1.undefined_p ())
1934 return false;
1935 else if (build_ge (r, type, op1))
1936 r.clear_nan ();
1937 break;
1938
1939 default:
1940 break;
1941 }
1942 return true;
1943 }
1944
1945 class foperator_unordered_ge : public range_operator
1946 {
1947 using range_operator::fold_range;
1948 using range_operator::op1_range;
1949 using range_operator::op2_range;
1950 public:
1951 bool fold_range (irange &r, tree type,
1952 const frange &op1, const frange &op2,
1953 relation_trio trio = TRIO_VARYING) const final override
1954 {
1955 if (op1.known_isnan () || op2.known_isnan ())
1956 {
1957 r = range_true (type);
1958 return true;
1959 }
1960 frange op1_no_nan = op1;
1961 frange op2_no_nan = op2;
1962 if (op1.maybe_isnan ())
1963 op1_no_nan.clear_nan ();
1964 if (op2.maybe_isnan ())
1965 op2_no_nan.clear_nan ();
1966 if (!range_op_handler (GE_EXPR).fold_range (r, type, op1_no_nan,
1967 op2_no_nan, trio))
1968 return false;
1969 // The result is the same as the ordered version when the
1970 // comparison is true or when the operands cannot be NANs.
1971 if (!maybe_isnan (op1, op2) || r == range_true (type))
1972 return true;
1973 else
1974 {
1975 r = range_true_and_false (type);
1976 return true;
1977 }
1978 }
1979 bool op1_range (frange &r, tree type,
1980 const irange &lhs, const frange &op2,
1981 relation_trio = TRIO_VARYING) const final override;
1982 bool op2_range (frange &r, tree type,
1983 const irange &lhs, const frange &op1,
1984 relation_trio = TRIO_VARYING) const final override;
1985 } fop_unordered_ge;
1986
1987 bool
1988 foperator_unordered_ge::op1_range (frange &r,
1989 tree type,
1990 const irange &lhs,
1991 const frange &op2,
1992 relation_trio) const
1993 {
1994 switch (get_bool_state (r, lhs, type))
1995 {
1996 case BRS_TRUE:
1997 if (op2.maybe_isnan ())
1998 r.set_varying (type);
1999 else if (op2.undefined_p ())
2000 return false;
2001 else
2002 build_ge (r, type, op2);
2003 break;
2004
2005 case BRS_FALSE:
2006 // A false UNORDERED_GE means both operands are !NAN, so it's
2007 // impossible for op2 to be a NAN.
2008 if (op2.known_isnan ())
2009 r.set_undefined ();
2010 else if (op2.undefined_p ())
2011 return false;
2012 else if (build_lt (r, type, op2))
2013 r.clear_nan ();
2014 break;
2015
2016 default:
2017 break;
2018 }
2019 return true;
2020 }
2021
2022 bool
2023 foperator_unordered_ge::op2_range (frange &r, tree type,
2024 const irange &lhs,
2025 const frange &op1,
2026 relation_trio) const
2027 {
2028 switch (get_bool_state (r, lhs, type))
2029 {
2030 case BRS_TRUE:
2031 if (op1.maybe_isnan ())
2032 r.set_varying (type);
2033 else if (op1.undefined_p ())
2034 return false;
2035 else
2036 build_le (r, type, op1);
2037 break;
2038
2039 case BRS_FALSE:
2040 // A false UNORDERED_GE means both operands are !NAN, so it's
2041 // impossible for op1 to be a NAN.
2042 if (op1.known_isnan ())
2043 r.set_undefined ();
2044 else if (op1.undefined_p ())
2045 return false;
2046 else if (build_gt (r, type, op1))
2047 r.clear_nan ();
2048 break;
2049
2050 default:
2051 break;
2052 }
2053 return true;
2054 }
2055
2056 class foperator_unordered_equal : public range_operator
2057 {
2058 using range_operator::fold_range;
2059 using range_operator::op1_range;
2060 using range_operator::op2_range;
2061 public:
2062 bool fold_range (irange &r, tree type,
2063 const frange &op1, const frange &op2,
2064 relation_trio trio = TRIO_VARYING) const final override
2065 {
2066 if (op1.known_isnan () || op2.known_isnan ())
2067 {
2068 r = range_true (type);
2069 return true;
2070 }
2071 frange op1_no_nan = op1;
2072 frange op2_no_nan = op2;
2073 if (op1.maybe_isnan ())
2074 op1_no_nan.clear_nan ();
2075 if (op2.maybe_isnan ())
2076 op2_no_nan.clear_nan ();
2077 if (!range_op_handler (EQ_EXPR).fold_range (r, type, op1_no_nan,
2078 op2_no_nan, trio))
2079 return false;
2080 // The result is the same as the ordered version when the
2081 // comparison is true or when the operands cannot be NANs.
2082 if (!maybe_isnan (op1, op2) || r == range_true (type))
2083 return true;
2084 else
2085 {
2086 r = range_true_and_false (type);
2087 return true;
2088 }
2089 }
2090 bool op1_range (frange &r, tree type,
2091 const irange &lhs, const frange &op2,
2092 relation_trio = TRIO_VARYING) const final override;
2093 bool op2_range (frange &r, tree type,
2094 const irange &lhs, const frange &op1,
2095 relation_trio rel = TRIO_VARYING) const final override
2096 {
2097 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
2098 }
2099 } fop_unordered_equal;
2100
2101 bool
2102 foperator_unordered_equal::op1_range (frange &r, tree type,
2103 const irange &lhs,
2104 const frange &op2,
2105 relation_trio) const
2106 {
2107 switch (get_bool_state (r, lhs, type))
2108 {
2109 case BRS_TRUE:
2110 // If it's true, the result is the same as OP2 plus a NAN.
2111 r = op2;
2112 // Add both zeros if there's the possibility of zero equality.
2113 frange_add_zeros (r, type);
2114 // Add the possibility of a NAN.
2115 r.update_nan ();
2116 break;
2117
2118 case BRS_FALSE:
2119 // A false UNORDERED_EQ means both operands are !NAN, so it's
2120 // impossible for op2 to be a NAN.
2121 if (op2.known_isnan ())
2122 r.set_undefined ();
2123 else
2124 {
2125 // The false side indicates !NAN and not equal. We can at least
2126 // represent !NAN.
2127 r.set_varying (type);
2128 r.clear_nan ();
2129 }
2130 break;
2131
2132 default:
2133 break;
2134 }
2135 return true;
2136 }
2137
2138 class foperator_ltgt : public range_operator
2139 {
2140 using range_operator::fold_range;
2141 using range_operator::op1_range;
2142 using range_operator::op2_range;
2143 public:
2144 bool fold_range (irange &r, tree type,
2145 const frange &op1, const frange &op2,
2146 relation_trio trio = TRIO_VARYING) const final override
2147 {
2148 if (op1.known_isnan () || op2.known_isnan ())
2149 {
2150 r = range_false (type);
2151 return true;
2152 }
2153 frange op1_no_nan = op1;
2154 frange op2_no_nan = op2;
2155 if (op1.maybe_isnan ())
2156 op1_no_nan.clear_nan ();
2157 if (op2.maybe_isnan ())
2158 op2_no_nan.clear_nan ();
2159 if (!range_op_handler (NE_EXPR).fold_range (r, type, op1_no_nan,
2160 op2_no_nan, trio))
2161 return false;
2162 // The result is the same as the ordered version when the
2163 // comparison is true or when the operands cannot be NANs.
2164 if (!maybe_isnan (op1, op2) || r == range_false (type))
2165 return true;
2166 else
2167 {
2168 r = range_true_and_false (type);
2169 return true;
2170 }
2171 }
2172 bool op1_range (frange &r, tree type,
2173 const irange &lhs, const frange &op2,
2174 relation_trio = TRIO_VARYING) const final override;
2175 bool op2_range (frange &r, tree type,
2176 const irange &lhs, const frange &op1,
2177 relation_trio rel = TRIO_VARYING) const final override
2178 {
2179 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
2180 }
2181 } fop_ltgt;
2182
2183 bool
2184 foperator_ltgt::op1_range (frange &r, tree type,
2185 const irange &lhs,
2186 const frange &op2,
2187 relation_trio) const
2188 {
2189 switch (get_bool_state (r, lhs, type))
2190 {
2191 case BRS_TRUE:
2192 // A true LTGT means both operands are !NAN, so it's
2193 // impossible for op2 to be a NAN.
2194 if (op2.known_isnan ())
2195 r.set_undefined ();
2196 else
2197 {
2198 // The true side indicates !NAN and not equal. We can at least
2199 // represent !NAN.
2200 r.set_varying (type);
2201 r.clear_nan ();
2202 }
2203 break;
2204
2205 case BRS_FALSE:
2206 // If it's false, the result is the same as OP2 plus a NAN.
2207 r = op2;
2208 // Add both zeros if there's the possibility of zero equality.
2209 frange_add_zeros (r, type);
2210 // Add the possibility of a NAN.
2211 r.update_nan ();
2212 break;
2213
2214 default:
2215 break;
2216 }
2217 return true;
2218 }
2219
2220 // Final tweaks for float binary op op1_range/op2_range.
2221 // Return TRUE if the operation is performed and a valid range is available.
2222
2223 static bool
2224 float_binary_op_range_finish (bool ret, frange &r, tree type,
2225 const frange &lhs, bool div_op2 = false)
2226 {
2227 if (!ret)
2228 return false;
2229
2230 // If we get a known NAN from reverse op, it means either that
2231 // the other operand was known NAN (in that case we know nothing),
2232 // or the reverse operation introduced a known NAN.
2233 // Say for lhs = op1 * op2 if lhs is [-0, +0] and op2 is too,
2234 // 0 / 0 is known NAN. Just punt in that case.
2235 // If NANs aren't honored, we get for 0 / 0 UNDEFINED, so punt as well.
2236 // Or if lhs is a known NAN, we also don't know anything.
2237 if (r.known_isnan () || lhs.known_isnan () || r.undefined_p ())
2238 {
2239 r.set_varying (type);
2240 return true;
2241 }
2242
2243 // If lhs isn't NAN, then neither operand could be NAN,
2244 // even if the reverse operation does introduce a maybe_nan.
2245 if (!lhs.maybe_isnan ())
2246 {
2247 r.clear_nan ();
2248 if (div_op2
2249 ? !(real_compare (LE_EXPR, &lhs.lower_bound (), &dconst0)
2250 && real_compare (GE_EXPR, &lhs.upper_bound (), &dconst0))
2251 : !(real_isinf (&lhs.lower_bound ())
2252 || real_isinf (&lhs.upper_bound ())))
2253 // For reverse + or - or * or op1 of /, if result is finite, then
2254 // r must be finite too, as X + INF or X - INF or X * INF or
2255 // INF / X is always +-INF or NAN. For op2 of /, if result is
2256 // non-zero and not NAN, r must be finite, as X / INF is always
2257 // 0 or NAN.
2258 frange_drop_infs (r, type);
2259 }
2260 // If lhs is a maybe or known NAN, the operand could be
2261 // NAN.
2262 else
2263 r.update_nan ();
2264 return true;
2265 }
2266
2267 // True if [lb, ub] is [+-0, +-0].
2268 static bool
2269 zero_p (const REAL_VALUE_TYPE &lb, const REAL_VALUE_TYPE &ub)
2270 {
2271 return real_iszero (&lb) && real_iszero (&ub);
2272 }
2273
2274 // True if +0 or -0 is in [lb, ub] range.
2275 static bool
2276 contains_zero_p (const REAL_VALUE_TYPE &lb, const REAL_VALUE_TYPE &ub)
2277 {
2278 return (real_compare (LE_EXPR, &lb, &dconst0)
2279 && real_compare (GE_EXPR, &ub, &dconst0));
2280 }
2281
2282 // True if [lb, ub] is [-INF, -INF] or [+INF, +INF].
2283 static bool
2284 singleton_inf_p (const REAL_VALUE_TYPE &lb, const REAL_VALUE_TYPE &ub)
2285 {
2286 return real_isinf (&lb) && real_isinf (&ub, real_isneg (&lb));
2287 }
2288
2289 // Return -1 if binary op result must have sign bit set,
2290 // 1 if binary op result must have sign bit clear,
2291 // 0 otherwise.
2292 // Sign bit of binary op result is exclusive or of the
2293 // operand's sign bits.
2294 static int
2295 signbit_known_p (const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub,
2296 const REAL_VALUE_TYPE &rh_lb, const REAL_VALUE_TYPE &rh_ub)
2297 {
2298 if (real_isneg (&lh_lb) == real_isneg (&lh_ub)
2299 && real_isneg (&rh_lb) == real_isneg (&rh_ub))
2300 {
2301 if (real_isneg (&lh_lb) == real_isneg (&rh_ub))
2302 return 1;
2303 else
2304 return -1;
2305 }
2306 return 0;
2307 }
2308
2309 // Set [lb, ub] to [-0, -0], [-0, +0] or [+0, +0] depending on
2310 // signbit_known.
2311 static void
2312 zero_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, int signbit_known)
2313 {
2314 ub = lb = dconst0;
2315 if (signbit_known <= 0)
2316 lb = dconstm0;
2317 if (signbit_known < 0)
2318 ub = lb;
2319 }
2320
2321 // Set [lb, ub] to [-INF, -INF], [-INF, +INF] or [+INF, +INF] depending on
2322 // signbit_known.
2323 static void
2324 inf_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, int signbit_known)
2325 {
2326 if (signbit_known > 0)
2327 ub = lb = dconstinf;
2328 else if (signbit_known < 0)
2329 ub = lb = dconstninf;
2330 else
2331 {
2332 lb = dconstninf;
2333 ub = dconstinf;
2334 }
2335 }
2336
2337 // Set [lb, ub] to [-INF, -0], [-INF, +INF] or [+0, +INF] depending on
2338 // signbit_known.
2339 static void
2340 zero_to_inf_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, int signbit_known)
2341 {
2342 if (signbit_known > 0)
2343 {
2344 lb = dconst0;
2345 ub = dconstinf;
2346 }
2347 else if (signbit_known < 0)
2348 {
2349 lb = dconstninf;
2350 ub = dconstm0;
2351 }
2352 else
2353 {
2354 lb = dconstninf;
2355 ub = dconstinf;
2356 }
2357 }
2358
2359 /* Extend the LHS range by 1ulp in each direction. For op1_range
2360 or op2_range of binary operations just computing the inverse
2361 operation on ranges isn't sufficient. Consider e.g.
2362 [1., 1.] = op1 + [1., 1.]. op1's range is not [0., 0.], but
2363 [-0x1.0p-54, 0x1.0p-53] (when not -frounding-math), any value for
2364 which adding 1. to it results in 1. after rounding to nearest.
2365 So, for op1_range/op2_range extend the lhs range by 1ulp (or 0.5ulp)
2366 in each direction. See PR109008 for more details. */
2367
2368 static frange
2369 float_widen_lhs_range (tree type, const frange &lhs)
2370 {
2371 frange ret = lhs;
2372 if (lhs.known_isnan ())
2373 return ret;
2374 REAL_VALUE_TYPE lb = lhs.lower_bound ();
2375 REAL_VALUE_TYPE ub = lhs.upper_bound ();
2376 if (real_isfinite (&lb))
2377 {
2378 frange_nextafter (TYPE_MODE (type), lb, dconstninf);
2379 if (real_isinf (&lb))
2380 {
2381 /* For -DBL_MAX, instead of -Inf use
2382 nexttoward (-DBL_MAX, -LDBL_MAX) in a hypothetical
2383 wider type with the same mantissa precision but larger
2384 exponent range; it is outside of range of double values,
2385 but makes it clear it is just one ulp larger rather than
2386 infinite amount larger. */
2387 lb = dconstm1;
2388 SET_REAL_EXP (&lb, FLOAT_MODE_FORMAT (TYPE_MODE (type))->emax + 1);
2389 }
2390 if (!flag_rounding_math && !MODE_COMPOSITE_P (TYPE_MODE (type)))
2391 {
2392 /* If not -frounding-math nor IBM double double, actually widen
2393 just by 0.5ulp rather than 1ulp. */
2394 REAL_VALUE_TYPE tem;
2395 real_arithmetic (&tem, PLUS_EXPR, &lhs.lower_bound (), &lb);
2396 real_arithmetic (&lb, RDIV_EXPR, &tem, &dconst2);
2397 }
2398 }
2399 if (real_isfinite (&ub))
2400 {
2401 frange_nextafter (TYPE_MODE (type), ub, dconstinf);
2402 if (real_isinf (&ub))
2403 {
2404 /* For DBL_MAX similarly. */
2405 ub = dconst1;
2406 SET_REAL_EXP (&ub, FLOAT_MODE_FORMAT (TYPE_MODE (type))->emax + 1);
2407 }
2408 if (!flag_rounding_math && !MODE_COMPOSITE_P (TYPE_MODE (type)))
2409 {
2410 /* If not -frounding-math nor IBM double double, actually widen
2411 just by 0.5ulp rather than 1ulp. */
2412 REAL_VALUE_TYPE tem;
2413 real_arithmetic (&tem, PLUS_EXPR, &lhs.upper_bound (), &ub);
2414 real_arithmetic (&ub, RDIV_EXPR, &tem, &dconst2);
2415 }
2416 }
2417 /* Temporarily disable -ffinite-math-only, so that frange::set doesn't
2418 reduce the range back to real_min_representable (type) as lower bound
2419 or real_max_representable (type) as upper bound. */
2420 bool save_flag_finite_math_only = flag_finite_math_only;
2421 flag_finite_math_only = false;
2422 ret.set (type, lb, ub, lhs.get_nan_state ());
2423 flag_finite_math_only = save_flag_finite_math_only;
2424 return ret;
2425 }
2426
2427 bool
2428 operator_plus::op1_range (frange &r, tree type, const frange &lhs,
2429 const frange &op2, relation_trio) const
2430 {
2431 if (lhs.undefined_p ())
2432 return false;
2433 range_op_handler minus (MINUS_EXPR);
2434 if (!minus)
2435 return false;
2436 frange wlhs = float_widen_lhs_range (type, lhs);
2437 return float_binary_op_range_finish (minus.fold_range (r, type, wlhs, op2),
2438 r, type, wlhs);
2439 }
2440
2441 bool
2442 operator_plus::op2_range (frange &r, tree type,
2443 const frange &lhs, const frange &op1,
2444 relation_trio) const
2445 {
2446 return op1_range (r, type, lhs, op1);
2447 }
2448
2449 void
2450 operator_plus::rv_fold (frange &r, tree type,
2451 const REAL_VALUE_TYPE &lh_lb,
2452 const REAL_VALUE_TYPE &lh_ub,
2453 const REAL_VALUE_TYPE &rh_lb,
2454 const REAL_VALUE_TYPE &rh_ub,
2455 relation_kind) const
2456 {
2457 REAL_VALUE_TYPE lb, ub;
2458 bool maybe_nan = false;
2459
2460 frange_arithmetic (PLUS_EXPR, type, lb, lh_lb, rh_lb, dconstninf);
2461 frange_arithmetic (PLUS_EXPR, type, ub, lh_ub, rh_ub, dconstinf);
2462
2463 // [-INF] + [+INF] = NAN
2464 if (real_isinf (&lh_lb, true) && real_isinf (&rh_ub, false))
2465 maybe_nan = true;
2466 // [+INF] + [-INF] = NAN
2467 else if (real_isinf (&lh_ub, false) && real_isinf (&rh_lb, true))
2468 maybe_nan = true;
2469
2470 // Handle possible NANs by saturating to the appropriate INF if only
2471 // one end is a NAN. If both ends are a NAN, just return a NAN.
2472 bool lb_nan = real_isnan (&lb);
2473 bool ub_nan = real_isnan (&ub);
2474 if (lb_nan && ub_nan)
2475 {
2476 r.set_nan (type);
2477 return;
2478 }
2479 if (lb_nan)
2480 lb = dconstninf;
2481 else if (ub_nan)
2482 ub = dconstinf;
2483 r.set (type, lb, ub, nan_state (maybe_nan));
2484 }
2485
2486
2487 bool
2488 operator_minus::op1_range (frange &r, tree type,
2489 const frange &lhs, const frange &op2,
2490 relation_trio) const
2491 {
2492 if (lhs.undefined_p ())
2493 return false;
2494 frange wlhs = float_widen_lhs_range (type, lhs);
2495 return float_binary_op_range_finish (
2496 range_op_handler (PLUS_EXPR).fold_range (r, type, wlhs, op2),
2497 r, type, wlhs);
2498 }
2499
2500 bool
2501 operator_minus::op2_range (frange &r, tree type,
2502 const frange &lhs, const frange &op1,
2503 relation_trio) const
2504 {
2505 if (lhs.undefined_p ())
2506 return false;
2507 frange wlhs = float_widen_lhs_range (type, lhs);
2508 return float_binary_op_range_finish (fold_range (r, type, op1, wlhs),
2509 r, type, wlhs);
2510 }
2511
2512 void
2513 operator_minus::rv_fold (frange &r, tree type,
2514 const REAL_VALUE_TYPE &lh_lb,
2515 const REAL_VALUE_TYPE &lh_ub,
2516 const REAL_VALUE_TYPE &rh_lb,
2517 const REAL_VALUE_TYPE &rh_ub,
2518 relation_kind) const
2519 {
2520 REAL_VALUE_TYPE lb, ub;
2521 bool maybe_nan = false;
2522
2523 frange_arithmetic (MINUS_EXPR, type, lb, lh_lb, rh_ub, dconstninf);
2524 frange_arithmetic (MINUS_EXPR, type, ub, lh_ub, rh_lb, dconstinf);
2525
2526 // [+INF] - [+INF] = NAN
2527 if (real_isinf (&lh_ub, false) && real_isinf (&rh_ub, false))
2528 maybe_nan = true;
2529 // [-INF] - [-INF] = NAN
2530 else if (real_isinf (&lh_lb, true) && real_isinf (&rh_lb, true))
2531 maybe_nan = true;
2532
2533 // Handle possible NANs by saturating to the appropriate INF if only
2534 // one end is a NAN. If both ends are a NAN, just return a NAN.
2535 bool lb_nan = real_isnan (&lb);
2536 bool ub_nan = real_isnan (&ub);
2537 if (lb_nan && ub_nan)
2538 {
2539 r.set_nan (type);
2540 return;
2541 }
2542 if (lb_nan)
2543 lb = dconstninf;
2544 else if (ub_nan)
2545 ub = dconstinf;
2546 r.set (type, lb, ub, nan_state (maybe_nan));
2547 }
2548
2549
2550 // Given CP[0] to CP[3] floating point values rounded to -INF,
2551 // set LB to the smallest of them (treating -0 as smaller to +0).
2552 // Given CP[4] to CP[7] floating point values rounded to +INF,
2553 // set UB to the largest of them (treating -0 as smaller to +0).
2554
2555 static void
2556 find_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub,
2557 const REAL_VALUE_TYPE (&cp)[8])
2558 {
2559 lb = cp[0];
2560 ub = cp[4];
2561 for (int i = 1; i < 4; ++i)
2562 {
2563 if (real_less (&cp[i], &lb)
2564 || (real_iszero (&lb) && real_isnegzero (&cp[i])))
2565 lb = cp[i];
2566 if (real_less (&ub, &cp[i + 4])
2567 || (real_isnegzero (&ub) && real_iszero (&cp[i + 4])))
2568 ub = cp[i + 4];
2569 }
2570 }
2571
2572
2573 bool
2574 operator_mult::op1_range (frange &r, tree type,
2575 const frange &lhs, const frange &op2,
2576 relation_trio) const
2577 {
2578 if (lhs.undefined_p ())
2579 return false;
2580 range_op_handler rdiv (RDIV_EXPR);
2581 if (!rdiv)
2582 return false;
2583 frange wlhs = float_widen_lhs_range (type, lhs);
2584 bool ret = rdiv.fold_range (r, type, wlhs, op2);
2585 if (ret == false)
2586 return false;
2587 if (wlhs.known_isnan () || op2.known_isnan () || op2.undefined_p ())
2588 return float_binary_op_range_finish (ret, r, type, wlhs);
2589 const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound ();
2590 const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound ();
2591 const REAL_VALUE_TYPE &op2_lb = op2.lower_bound ();
2592 const REAL_VALUE_TYPE &op2_ub = op2.upper_bound ();
2593 if ((contains_zero_p (lhs_lb, lhs_ub) && contains_zero_p (op2_lb, op2_ub))
2594 || ((real_isinf (&lhs_lb) || real_isinf (&lhs_ub))
2595 && (real_isinf (&op2_lb) || real_isinf (&op2_ub))))
2596 {
2597 // If both lhs and op2 could be zeros or both could be infinities,
2598 // we don't know anything about op1 except maybe for the sign
2599 // and perhaps if it can be NAN or not.
2600 REAL_VALUE_TYPE lb, ub;
2601 int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op2_lb, op2_ub);
2602 zero_to_inf_range (lb, ub, signbit_known);
2603 r.set (type, lb, ub);
2604 }
2605 // Otherwise, if op2 is a singleton INF and lhs doesn't include INF,
2606 // or if lhs must be zero and op2 doesn't include zero, it would be
2607 // UNDEFINED, while rdiv.fold_range computes a zero or singleton INF
2608 // range. Those are supersets of UNDEFINED, so let's keep that way.
2609 return float_binary_op_range_finish (ret, r, type, wlhs);
2610 }
2611
2612 bool
2613 operator_mult::op2_range (frange &r, tree type,
2614 const frange &lhs, const frange &op1,
2615 relation_trio) const
2616 {
2617 return op1_range (r, type, lhs, op1);
2618 }
2619
2620 void
2621 operator_mult::rv_fold (frange &r, tree type,
2622 const REAL_VALUE_TYPE &lh_lb,
2623 const REAL_VALUE_TYPE &lh_ub,
2624 const REAL_VALUE_TYPE &rh_lb,
2625 const REAL_VALUE_TYPE &rh_ub,
2626 relation_kind kind) const
2627 {
2628 bool is_square
2629 = (kind == VREL_EQ
2630 && real_equal (&lh_lb, &rh_lb)
2631 && real_equal (&lh_ub, &rh_ub)
2632 && real_isneg (&lh_lb) == real_isneg (&rh_lb)
2633 && real_isneg (&lh_ub) == real_isneg (&rh_ub));
2634 REAL_VALUE_TYPE lb, ub;
2635 bool maybe_nan = false;
2636 // x * x never produces a new NAN and we only multiply the same
2637 // values, so the 0 * INF problematic cases never appear there.
2638 if (!is_square)
2639 {
2640 // [+-0, +-0] * [+INF,+INF] (or [-INF,-INF] or swapped is a known NAN.
2641 if ((zero_p (lh_lb, lh_ub) && singleton_inf_p (rh_lb, rh_ub))
2642 || (zero_p (rh_lb, rh_ub) && singleton_inf_p (lh_lb, lh_ub)))
2643 {
2644 r.set_nan (type);
2645 return;
2646 }
2647
2648 // Otherwise, if one range includes zero and the other ends with +-INF,
2649 // it is a maybe NAN.
2650 if ((contains_zero_p (lh_lb, lh_ub)
2651 && (real_isinf (&rh_lb) || real_isinf (&rh_ub)))
2652 || (contains_zero_p (rh_lb, rh_ub)
2653 && (real_isinf (&lh_lb) || real_isinf (&lh_ub))))
2654 {
2655 maybe_nan = true;
2656
2657 int signbit_known = signbit_known_p (lh_lb, lh_ub, rh_lb, rh_ub);
2658
2659 // If one of the ranges that includes INF is singleton
2660 // and the other range includes zero, the resulting
2661 // range is INF and NAN, because the 0 * INF boundary
2662 // case will be NAN, but already nextafter (0, 1) * INF
2663 // is INF.
2664 if (singleton_inf_p (lh_lb, lh_ub)
2665 || singleton_inf_p (rh_lb, rh_ub))
2666 {
2667 inf_range (lb, ub, signbit_known);
2668 r.set (type, lb, ub, nan_state (true));
2669 return;
2670 }
2671
2672 // If one of the multiplicands must be zero, the resulting
2673 // range is +-0 and NAN.
2674 if (zero_p (lh_lb, lh_ub) || zero_p (rh_lb, rh_ub))
2675 {
2676 zero_range (lb, ub, signbit_known);
2677 r.set (type, lb, ub, nan_state (true));
2678 return;
2679 }
2680
2681 // Otherwise one of the multiplicands could be
2682 // [0.0, nextafter (0.0, 1.0)] and the [DBL_MAX, INF]
2683 // or similarly with different signs. 0.0 * DBL_MAX
2684 // is still 0.0, nextafter (0.0, 1.0) * INF is still INF,
2685 // so if the signs are always the same or always different,
2686 // result is [+0.0, +INF] or [-INF, -0.0], otherwise VARYING.
2687 zero_to_inf_range (lb, ub, signbit_known);
2688 r.set (type, lb, ub, nan_state (true));
2689 return;
2690 }
2691 }
2692
2693 REAL_VALUE_TYPE cp[8];
2694 // Do a cross-product. At this point none of the multiplications
2695 // should produce a NAN.
2696 frange_arithmetic (MULT_EXPR, type, cp[0], lh_lb, rh_lb, dconstninf);
2697 frange_arithmetic (MULT_EXPR, type, cp[4], lh_lb, rh_lb, dconstinf);
2698 if (is_square)
2699 {
2700 // For x * x we can just do max (lh_lb * lh_lb, lh_ub * lh_ub)
2701 // as maximum and -0.0 as minimum if 0.0 is in the range,
2702 // otherwise min (lh_lb * lh_lb, lh_ub * lh_ub).
2703 // -0.0 rather than 0.0 because VREL_EQ doesn't prove that
2704 // x and y are bitwise equal, just that they compare equal.
2705 if (contains_zero_p (lh_lb, lh_ub))
2706 {
2707 if (real_isneg (&lh_lb) == real_isneg (&lh_ub))
2708 cp[1] = dconst0;
2709 else
2710 cp[1] = dconstm0;
2711 }
2712 else
2713 cp[1] = cp[0];
2714 cp[2] = cp[0];
2715 cp[5] = cp[4];
2716 cp[6] = cp[4];
2717 }
2718 else
2719 {
2720 frange_arithmetic (MULT_EXPR, type, cp[1], lh_lb, rh_ub, dconstninf);
2721 frange_arithmetic (MULT_EXPR, type, cp[5], lh_lb, rh_ub, dconstinf);
2722 frange_arithmetic (MULT_EXPR, type, cp[2], lh_ub, rh_lb, dconstninf);
2723 frange_arithmetic (MULT_EXPR, type, cp[6], lh_ub, rh_lb, dconstinf);
2724 }
2725 frange_arithmetic (MULT_EXPR, type, cp[3], lh_ub, rh_ub, dconstninf);
2726 frange_arithmetic (MULT_EXPR, type, cp[7], lh_ub, rh_ub, dconstinf);
2727
2728 find_range (lb, ub, cp);
2729
2730 gcc_checking_assert (!real_isnan (&lb));
2731 gcc_checking_assert (!real_isnan (&ub));
2732 r.set (type, lb, ub, nan_state (maybe_nan));
2733 }
2734
2735
2736 class foperator_div : public range_operator
2737 {
2738 using range_operator::op1_range;
2739 using range_operator::op2_range;
2740 public:
2741 virtual bool op1_range (frange &r, tree type,
2742 const frange &lhs,
2743 const frange &op2,
2744 relation_trio = TRIO_VARYING) const final override
2745 {
2746 if (lhs.undefined_p ())
2747 return false;
2748 frange wlhs = float_widen_lhs_range (type, lhs);
2749 bool ret = range_op_handler (MULT_EXPR).fold_range (r, type, wlhs, op2);
2750 if (!ret)
2751 return ret;
2752 if (wlhs.known_isnan () || op2.known_isnan () || op2.undefined_p ())
2753 return float_binary_op_range_finish (ret, r, type, wlhs);
2754 const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound ();
2755 const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound ();
2756 const REAL_VALUE_TYPE &op2_lb = op2.lower_bound ();
2757 const REAL_VALUE_TYPE &op2_ub = op2.upper_bound ();
2758 if ((contains_zero_p (lhs_lb, lhs_ub)
2759 && (real_isinf (&op2_lb) || real_isinf (&op2_ub)))
2760 || ((contains_zero_p (op2_lb, op2_ub))
2761 && (real_isinf (&lhs_lb) || real_isinf (&lhs_ub))))
2762 {
2763 // If both lhs could be zero and op2 infinity or vice versa,
2764 // we don't know anything about op1 except maybe for the sign
2765 // and perhaps if it can be NAN or not.
2766 REAL_VALUE_TYPE lb, ub;
2767 int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op2_lb, op2_ub);
2768 zero_to_inf_range (lb, ub, signbit_known);
2769 r.set (type, lb, ub);
2770 }
2771 return float_binary_op_range_finish (ret, r, type, wlhs);
2772 }
2773 virtual bool op2_range (frange &r, tree type,
2774 const frange &lhs,
2775 const frange &op1,
2776 relation_trio = TRIO_VARYING) const final override
2777 {
2778 if (lhs.undefined_p ())
2779 return false;
2780 frange wlhs = float_widen_lhs_range (type, lhs);
2781 bool ret = fold_range (r, type, op1, wlhs);
2782 if (!ret)
2783 return ret;
2784 if (wlhs.known_isnan () || op1.known_isnan () || op1.undefined_p ())
2785 return float_binary_op_range_finish (ret, r, type, wlhs, true);
2786 const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound ();
2787 const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound ();
2788 const REAL_VALUE_TYPE &op1_lb = op1.lower_bound ();
2789 const REAL_VALUE_TYPE &op1_ub = op1.upper_bound ();
2790 if ((contains_zero_p (lhs_lb, lhs_ub) && contains_zero_p (op1_lb, op1_ub))
2791 || ((real_isinf (&lhs_lb) || real_isinf (&lhs_ub))
2792 && (real_isinf (&op1_lb) || real_isinf (&op1_ub))))
2793 {
2794 // If both lhs and op1 could be zeros or both could be infinities,
2795 // we don't know anything about op2 except maybe for the sign
2796 // and perhaps if it can be NAN or not.
2797 REAL_VALUE_TYPE lb, ub;
2798 int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op1_lb, op1_ub);
2799 zero_to_inf_range (lb, ub, signbit_known);
2800 r.set (type, lb, ub);
2801 }
2802 return float_binary_op_range_finish (ret, r, type, wlhs, true);
2803 }
2804 private:
2805 void rv_fold (frange &r, tree type,
2806 const REAL_VALUE_TYPE &lh_lb,
2807 const REAL_VALUE_TYPE &lh_ub,
2808 const REAL_VALUE_TYPE &rh_lb,
2809 const REAL_VALUE_TYPE &rh_ub,
2810 relation_kind) const final override
2811 {
2812 // +-0.0 / +-0.0 or +-INF / +-INF is a known NAN.
2813 if ((zero_p (lh_lb, lh_ub) && zero_p (rh_lb, rh_ub))
2814 || (singleton_inf_p (lh_lb, lh_ub) && singleton_inf_p (rh_lb, rh_ub)))
2815 {
2816 r.set_nan (type);
2817 return;
2818 }
2819
2820 REAL_VALUE_TYPE lb, ub;
2821 bool maybe_nan = false;
2822 // If +-0.0 is in both ranges, it is a maybe NAN.
2823 if (contains_zero_p (lh_lb, lh_ub) && contains_zero_p (rh_lb, rh_ub))
2824 maybe_nan = true;
2825 // If +-INF is in both ranges, it is a maybe NAN.
2826 else if ((real_isinf (&lh_lb) || real_isinf (&lh_ub))
2827 && (real_isinf (&rh_lb) || real_isinf (&rh_ub)))
2828 maybe_nan = true;
2829
2830 int signbit_known = signbit_known_p (lh_lb, lh_ub, rh_lb, rh_ub);
2831
2832 // If dividend must be zero, the range is just +-0
2833 // (including if the divisor is +-INF).
2834 // If divisor must be +-INF, the range is just +-0
2835 // (including if the dividend is zero).
2836 if (zero_p (lh_lb, lh_ub) || singleton_inf_p (rh_lb, rh_ub))
2837 {
2838 zero_range (lb, ub, signbit_known);
2839 r.set (type, lb, ub, nan_state (maybe_nan));
2840 return;
2841 }
2842
2843 // If divisor must be zero, the range is just +-INF
2844 // (including if the dividend is +-INF).
2845 // If dividend must be +-INF, the range is just +-INF
2846 // (including if the dividend is zero).
2847 if (zero_p (rh_lb, rh_ub) || singleton_inf_p (lh_lb, lh_ub))
2848 {
2849 inf_range (lb, ub, signbit_known);
2850 r.set (type, lb, ub, nan_state (maybe_nan));
2851 return;
2852 }
2853
2854 // Otherwise if both operands may be zero, divisor could be
2855 // nextafter(0.0, +-1.0) and dividend +-0.0
2856 // in which case result is going to INF or vice versa and
2857 // result +0.0. So, all we can say for that case is if the
2858 // signs of divisor and dividend are always the same we have
2859 // [+0.0, +INF], if they are always different we have
2860 // [-INF, -0.0]. If they vary, VARYING.
2861 // If both may be +-INF, divisor could be INF and dividend FLT_MAX,
2862 // in which case result is going to INF or vice versa and
2863 // result +0.0. So, all we can say for that case is if the
2864 // signs of divisor and dividend are always the same we have
2865 // [+0.0, +INF], if they are always different we have
2866 // [-INF, -0.0]. If they vary, VARYING.
2867 if (maybe_nan)
2868 {
2869 zero_to_inf_range (lb, ub, signbit_known);
2870 r.set (type, lb, ub, nan_state (maybe_nan));
2871 return;
2872 }
2873
2874 REAL_VALUE_TYPE cp[8];
2875 // Do a cross-division. At this point none of the divisions should
2876 // produce a NAN.
2877 frange_arithmetic (RDIV_EXPR, type, cp[0], lh_lb, rh_lb, dconstninf);
2878 frange_arithmetic (RDIV_EXPR, type, cp[1], lh_lb, rh_ub, dconstninf);
2879 frange_arithmetic (RDIV_EXPR, type, cp[2], lh_ub, rh_lb, dconstninf);
2880 frange_arithmetic (RDIV_EXPR, type, cp[3], lh_ub, rh_ub, dconstninf);
2881 frange_arithmetic (RDIV_EXPR, type, cp[4], lh_lb, rh_lb, dconstinf);
2882 frange_arithmetic (RDIV_EXPR, type, cp[5], lh_lb, rh_ub, dconstinf);
2883 frange_arithmetic (RDIV_EXPR, type, cp[6], lh_ub, rh_lb, dconstinf);
2884 frange_arithmetic (RDIV_EXPR, type, cp[7], lh_ub, rh_ub, dconstinf);
2885
2886 find_range (lb, ub, cp);
2887
2888 // If divisor may be zero (but is not known to be only zero),
2889 // and dividend can't be zero, the range can go up to -INF or +INF
2890 // depending on the signs.
2891 if (contains_zero_p (rh_lb, rh_ub))
2892 {
2893 if (signbit_known <= 0)
2894 real_inf (&lb, true);
2895 if (signbit_known >= 0)
2896 real_inf (&ub, false);
2897 }
2898
2899 gcc_checking_assert (!real_isnan (&lb));
2900 gcc_checking_assert (!real_isnan (&ub));
2901 r.set (type, lb, ub, nan_state (maybe_nan));
2902 }
2903 } fop_div;
2904
2905
2906 // Initialize any float operators to the primary table
2907
2908 void
2909 range_op_table::initialize_float_ops ()
2910 {
2911 set (UNLE_EXPR, fop_unordered_le);
2912 set (UNLT_EXPR, fop_unordered_lt);
2913 set (UNGE_EXPR, fop_unordered_ge);
2914 set (UNGT_EXPR, fop_unordered_gt);
2915 set (UNEQ_EXPR, fop_unordered_equal);
2916 set (ORDERED_EXPR, fop_ordered);
2917 set (UNORDERED_EXPR, fop_unordered);
2918 set (LTGT_EXPR, fop_ltgt);
2919 set (RDIV_EXPR, fop_div);
2920 }
2921
2922 #if CHECKING_P
2923 #include "selftest.h"
2924
2925 namespace selftest
2926 {
2927
2928 // Build an frange from string endpoints.
2929
2930 static inline frange
2931 frange_float (const char *lb, const char *ub, tree type = float_type_node)
2932 {
2933 REAL_VALUE_TYPE min, max;
2934 gcc_assert (real_from_string (&min, lb) == 0);
2935 gcc_assert (real_from_string (&max, ub) == 0);
2936 return frange (type, min, max);
2937 }
2938
2939 void
2940 range_op_float_tests ()
2941 {
2942 frange r, r0, r1;
2943 frange trange (float_type_node);
2944
2945 // negate([-5, +10]) => [-10, 5]
2946 r0 = frange_float ("-5", "10");
2947 range_op_handler (NEGATE_EXPR).fold_range (r, float_type_node, r0, trange);
2948 ASSERT_EQ (r, frange_float ("-10", "5"));
2949
2950 // negate([0, 1] -NAN) => [-1, -0] +NAN
2951 r0 = frange_float ("0", "1");
2952 r0.update_nan (true);
2953 range_op_handler (NEGATE_EXPR).fold_range (r, float_type_node, r0, trange);
2954 r1 = frange_float ("-1", "-0");
2955 r1.update_nan (false);
2956 ASSERT_EQ (r, r1);
2957
2958 // [-INF,+INF] + [-INF,+INF] could be a NAN.
2959 range_op_handler plus (PLUS_EXPR);
2960 r0.set_varying (float_type_node);
2961 r1.set_varying (float_type_node);
2962 r0.clear_nan ();
2963 r1.clear_nan ();
2964 plus.fold_range (r, float_type_node, r0, r1);
2965 if (HONOR_NANS (float_type_node))
2966 ASSERT_TRUE (r.maybe_isnan ());
2967 }
2968
2969 } // namespace selftest
2970
2971 #endif // CHECKING_P