1 /* Floating point range operators.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-iterator.h"
41 #include "gimple-fold.h"
43 #include "gimple-walk.h"
46 #include "value-relation.h"
48 #include "range-op-mixed.h"
50 // Default definitions for floating point operators.
53 range_operator::fold_range (frange
&r
, tree type
,
54 const frange
&op1
, const frange
&op2
,
55 relation_trio trio
) const
57 if (empty_range_varying (r
, type
, op1
, op2
))
59 if (op1
.known_isnan () || op2
.known_isnan ())
65 REAL_VALUE_TYPE lb
, ub
;
67 rv_fold (lb
, ub
, maybe_nan
, type
,
68 op1
.lower_bound (), op1
.upper_bound (),
69 op2
.lower_bound (), op2
.upper_bound (), trio
.op1_op2 ());
71 // Handle possible NANs by saturating to the appropriate INF if only
72 // one end is a NAN. If both ends are a NAN, just return a NAN.
73 bool lb_nan
= real_isnan (&lb
);
74 bool ub_nan
= real_isnan (&ub
);
87 if (lb_nan
|| ub_nan
|| maybe_nan
89 || op2
.maybe_isnan ())
90 // Keep the default NAN (with a varying sign) set by the setter.
95 // If the result has overflowed and flag_trapping_math, folding this
96 // operation could elide an overflow or division by zero exception.
97 // Avoid returning a singleton +-INF, to keep the propagators (DOM
98 // and substitute_and_fold_engine) from folding. See PR107608.
99 if (flag_trapping_math
100 && MODE_HAS_INFINITIES (TYPE_MODE (type
))
101 && r
.known_isinf () && !op1
.known_isinf () && !op2
.known_isinf ())
103 REAL_VALUE_TYPE inf
= r
.lower_bound ();
104 if (real_isneg (&inf
))
106 REAL_VALUE_TYPE min
= real_min_representable (type
);
107 r
.set (type
, inf
, min
);
111 REAL_VALUE_TYPE max
= real_max_representable (type
);
112 r
.set (type
, max
, inf
);
116 r
.flush_denormals_to_zero ();
121 // For a given operation, fold two sets of ranges into [lb, ub].
122 // MAYBE_NAN is set to TRUE if, in addition to any result in LB or
123 // UB, the final range has the possibility of a NAN.
125 range_operator::rv_fold (REAL_VALUE_TYPE
&lb
,
128 tree type ATTRIBUTE_UNUSED
,
129 const REAL_VALUE_TYPE
&lh_lb ATTRIBUTE_UNUSED
,
130 const REAL_VALUE_TYPE
&lh_ub ATTRIBUTE_UNUSED
,
131 const REAL_VALUE_TYPE
&rh_lb ATTRIBUTE_UNUSED
,
132 const REAL_VALUE_TYPE
&rh_ub ATTRIBUTE_UNUSED
,
141 range_operator::fold_range (irange
&r ATTRIBUTE_UNUSED
,
142 tree type ATTRIBUTE_UNUSED
,
143 const frange
&lh ATTRIBUTE_UNUSED
,
144 const irange
&rh ATTRIBUTE_UNUSED
,
151 range_operator::fold_range (irange
&r ATTRIBUTE_UNUSED
,
152 tree type ATTRIBUTE_UNUSED
,
153 const frange
&lh ATTRIBUTE_UNUSED
,
154 const frange
&rh ATTRIBUTE_UNUSED
,
161 range_operator::fold_range (frange
&r ATTRIBUTE_UNUSED
,
162 tree type ATTRIBUTE_UNUSED
,
163 const irange
&lh ATTRIBUTE_UNUSED
,
164 const irange
&rh ATTRIBUTE_UNUSED
,
171 range_operator::op1_range (frange
&r ATTRIBUTE_UNUSED
,
172 tree type ATTRIBUTE_UNUSED
,
173 const frange
&lhs ATTRIBUTE_UNUSED
,
174 const frange
&op2 ATTRIBUTE_UNUSED
,
181 range_operator::op1_range (frange
&r ATTRIBUTE_UNUSED
,
182 tree type ATTRIBUTE_UNUSED
,
183 const irange
&lhs ATTRIBUTE_UNUSED
,
184 const frange
&op2 ATTRIBUTE_UNUSED
,
191 range_operator::op2_range (frange
&r ATTRIBUTE_UNUSED
,
192 tree type ATTRIBUTE_UNUSED
,
193 const frange
&lhs ATTRIBUTE_UNUSED
,
194 const frange
&op1 ATTRIBUTE_UNUSED
,
201 range_operator::op2_range (frange
&r ATTRIBUTE_UNUSED
,
202 tree type ATTRIBUTE_UNUSED
,
203 const irange
&lhs ATTRIBUTE_UNUSED
,
204 const frange
&op1 ATTRIBUTE_UNUSED
,
211 range_operator::lhs_op1_relation (const frange
&lhs ATTRIBUTE_UNUSED
,
212 const frange
&op1 ATTRIBUTE_UNUSED
,
213 const frange
&op2 ATTRIBUTE_UNUSED
,
220 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
221 const frange
&op1 ATTRIBUTE_UNUSED
,
222 const frange
&op2 ATTRIBUTE_UNUSED
,
229 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
230 const frange
&op1 ATTRIBUTE_UNUSED
,
231 const frange
&op2 ATTRIBUTE_UNUSED
,
238 range_operator::lhs_op2_relation (const frange
&lhs ATTRIBUTE_UNUSED
,
239 const frange
&op1 ATTRIBUTE_UNUSED
,
240 const frange
&op2 ATTRIBUTE_UNUSED
,
247 range_operator::op1_op2_relation (const irange
&,
249 const frange
&) const
256 range_operator::op1_op2_relation (const frange
&,
258 const frange
&) const
263 // Return TRUE if OP1 and OP2 may be a NAN.
266 maybe_isnan (const frange
&op1
, const frange
&op2
)
268 return op1
.maybe_isnan () || op2
.maybe_isnan ();
271 // Floating point version of relop_early_resolve that takes NANs into
274 // For relation opcodes, first try to see if the supplied relation
275 // forces a true or false result, and return that.
276 // Then check for undefined operands. If none of this applies,
279 // TRIO are the relations between operands as they appear in the IL.
280 // MY_REL is the relation that corresponds to the operator being
281 // folded. For example, when attempting to fold x_3 == y_5, MY_REL is
282 // VREL_EQ, and if the statement is dominated by x_3 > y_5, then
283 // TRIO.op1_op2() is VREL_GT.
285 // Relations in a floating point world are a bit tricky, as TRIO
286 // behaves as the corresponding unordered variant if either operand
287 // could be a NAN. For example, when resolving "if (x_5 == x_5)", the
288 // relation is VREL_EQ, but it behaves like VREL_UNEQ if NANs are a
289 // possibility. Similarly, the false edge of "if (x >= y)" has a
290 // relation of VREL_LT, but behaves as VREL_UNLT unless we can prove
291 // neither operand can be NAN.
293 // ?? It is unclear whether providing unordered VREL relations would
294 // simplify things, as we'd have to add more entries to various
295 // tables, tweak all the op1_op2_relation() entries, etc.
298 frelop_early_resolve (irange
&r
, tree type
,
299 const frange
&op1
, const frange
&op2
,
300 relation_trio trio
, relation_kind my_rel
)
302 relation_kind rel
= trio
.op1_op2 ();
304 if (maybe_isnan (op1
, op2
))
306 // There's not much we can do for VREL_EQ if NAN is a
307 // possibility. It is up to the caller to deal with these
310 return empty_range_varying (r
, type
, op1
, op2
);
312 // If known relation is a complete subset of this relation, always
313 // return true. However, avoid doing this when NAN is a possibility
314 // as we'll incorrectly fold conditions:
319 // ;; With NANs the relation here is basically VREL_UNLT, so we
320 // ;; can't fold the following:
322 else if (relation_union (rel
, my_rel
) == my_rel
)
324 r
= range_true (type
);
328 // If known relation has no subset of this relation, always false.
329 if (relation_intersect (rel
, my_rel
) == VREL_UNDEFINED
)
331 r
= range_false (type
);
335 // If either operand is undefined, return VARYING.
336 if (empty_range_varying (r
, type
, op1
, op2
))
342 // Set VALUE to its next real value, or INF if the operation overflows.
345 frange_nextafter (enum machine_mode mode
,
346 REAL_VALUE_TYPE
&value
,
347 const REAL_VALUE_TYPE
&inf
)
349 if (MODE_COMPOSITE_P (mode
)
350 && (real_isdenormal (&value
, mode
) || real_iszero (&value
)))
352 // IBM extended denormals only have DFmode precision.
353 REAL_VALUE_TYPE tmp
, tmp2
;
354 real_convert (&tmp2
, DFmode
, &value
);
355 real_nextafter (&tmp
, REAL_MODE_FORMAT (DFmode
), &tmp2
, &inf
);
356 real_convert (&value
, mode
, &tmp
);
361 real_nextafter (&tmp
, REAL_MODE_FORMAT (mode
), &value
, &inf
);
366 // Like real_arithmetic, but round the result to INF if the operation
367 // produced inexact results.
369 // ?? There is still one problematic case, i387. With
370 // -fexcess-precision=standard we perform most SF/DFmode arithmetic in
371 // XFmode (long_double_type_node), so that case is OK. But without
372 // -mfpmath=sse, all the SF/DFmode computations are in XFmode
373 // precision (64-bit mantissa) and only occasionally rounded to
374 // SF/DFmode (when storing into memory from the 387 stack). Maybe
375 // this is ok as well though it is just occasionally more precise. ??
378 frange_arithmetic (enum tree_code code
, tree type
,
379 REAL_VALUE_TYPE
&result
,
380 const REAL_VALUE_TYPE
&op1
,
381 const REAL_VALUE_TYPE
&op2
,
382 const REAL_VALUE_TYPE
&inf
)
384 REAL_VALUE_TYPE value
;
385 enum machine_mode mode
= TYPE_MODE (type
);
386 bool mode_composite
= MODE_COMPOSITE_P (mode
);
388 bool inexact
= real_arithmetic (&value
, code
, &op1
, &op2
);
389 real_convert (&result
, mode
, &value
);
391 /* When rounding towards negative infinity, x + (-x) and
392 x - x is -0 rather than +0 real_arithmetic computes.
393 So, when we are looking for lower bound (inf is negative),
394 use -0 rather than +0. */
395 if (flag_rounding_math
396 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
398 && real_iszero (&result
)
399 && !real_isneg (&result
)
400 && real_isneg (&inf
))
402 REAL_VALUE_TYPE op2a
= op2
;
403 if (code
== PLUS_EXPR
)
405 if (real_isneg (&op1
) == real_isneg (&op2a
) && real_equal (&op1
, &op2a
))
409 // Be extra careful if there may be discrepancies between the
410 // compile and runtime results.
416 bool low
= real_isneg (&inf
);
417 round
= (low
? !real_less (&result
, &value
)
418 : !real_less (&value
, &result
));
419 if (real_isinf (&result
, !low
)
420 && !real_isinf (&value
)
421 && !flag_rounding_math
)
423 // Use just [+INF, +INF] rather than [MAX, +INF]
424 // even if value is larger than MAX and rounds to
425 // nearest to +INF. Similarly just [-INF, -INF]
426 // rather than [-INF, +MAX] even if value is smaller
427 // than -MAX and rounds to nearest to -INF.
428 // Unless INEXACT is true, in that case we need some
434 REAL_VALUE_TYPE tmp
= result
, tmp2
;
435 frange_nextafter (mode
, tmp
, inf
);
436 // TMP is at this point the maximum representable
438 real_arithmetic (&tmp2
, MINUS_EXPR
, &value
, &tmp
);
439 if (real_isneg (&tmp2
) != low
440 && (REAL_EXP (&tmp2
) - REAL_EXP (&tmp
)
441 >= 2 - REAL_MODE_FORMAT (mode
)->p
))
446 if (round
&& (inexact
|| !real_identical (&result
, &value
)))
449 && (real_isdenormal (&result
, mode
) || real_iszero (&result
)))
451 // IBM extended denormals only have DFmode precision.
452 REAL_VALUE_TYPE tmp
, tmp2
;
453 real_convert (&tmp2
, DFmode
, &value
);
454 real_nextafter (&tmp
, REAL_MODE_FORMAT (DFmode
), &tmp2
, &inf
);
455 real_convert (&result
, mode
, &tmp
);
458 frange_nextafter (mode
, result
, inf
);
465 // ibm-ldouble-format documents 1ulp for + and -.
466 frange_nextafter (mode
, result
, inf
);
469 // ibm-ldouble-format documents 2ulps for *.
470 frange_nextafter (mode
, result
, inf
);
471 frange_nextafter (mode
, result
, inf
);
474 // ibm-ldouble-format documents 3ulps for /.
475 frange_nextafter (mode
, result
, inf
);
476 frange_nextafter (mode
, result
, inf
);
477 frange_nextafter (mode
, result
, inf
);
484 // Crop R to [-INF, MAX] where MAX is the maximum representable number
488 frange_drop_inf (frange
&r
, tree type
)
490 REAL_VALUE_TYPE max
= real_max_representable (type
);
491 frange
tmp (type
, r
.lower_bound (), max
);
495 // Crop R to [MIN, +INF] where MIN is the minimum representable number
499 frange_drop_ninf (frange
&r
, tree type
)
501 REAL_VALUE_TYPE min
= real_min_representable (type
);
502 frange
tmp (type
, min
, r
.upper_bound ());
506 // Crop R to [MIN, MAX] where MAX is the maximum representable number
507 // for TYPE and MIN the minimum representable number for TYPE.
510 frange_drop_infs (frange
&r
, tree type
)
512 REAL_VALUE_TYPE max
= real_max_representable (type
);
513 REAL_VALUE_TYPE min
= real_min_representable (type
);
514 frange
tmp (type
, min
, max
);
518 // If zero is in R, make sure both -0.0 and +0.0 are in the range.
521 frange_add_zeros (frange
&r
, tree type
)
523 if (r
.undefined_p () || r
.known_isnan ())
526 if (HONOR_SIGNED_ZEROS (type
)
527 && (real_iszero (&r
.lower_bound ()) || real_iszero (&r
.upper_bound ())))
530 zero
.set_zero (type
);
535 // Build a range that is <= VAL and store it in R. Return TRUE if
536 // further changes may be needed for R, or FALSE if R is in its final
540 build_le (frange
&r
, tree type
, const frange
&val
)
542 gcc_checking_assert (!val
.known_isnan ());
544 REAL_VALUE_TYPE ninf
= frange_val_min (type
);
545 r
.set (type
, ninf
, val
.upper_bound ());
547 // Add both zeros if there's the possibility of zero equality.
548 frange_add_zeros (r
, type
);
553 // Build a range that is < VAL and store it in R. Return TRUE if
554 // further changes may be needed for R, or FALSE if R is in its final
558 build_lt (frange
&r
, tree type
, const frange
&val
)
560 gcc_checking_assert (!val
.known_isnan ());
562 // < -INF is outside the range.
563 if (real_isinf (&val
.upper_bound (), 1))
565 if (HONOR_NANS (type
))
572 REAL_VALUE_TYPE ninf
= frange_val_min (type
);
573 REAL_VALUE_TYPE prev
= val
.upper_bound ();
574 machine_mode mode
= TYPE_MODE (type
);
575 // Default to the conservatively correct closed ranges for
576 // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
577 // !HONOR_INFINITIES, nextafter will yield -INF, but frange::set()
578 // will crop the range appropriately.
579 if (!MODE_COMPOSITE_P (mode
))
580 frange_nextafter (mode
, prev
, ninf
);
581 r
.set (type
, ninf
, prev
);
585 // Build a range that is >= VAL and store it in R. Return TRUE if
586 // further changes may be needed for R, or FALSE if R is in its final
590 build_ge (frange
&r
, tree type
, const frange
&val
)
592 gcc_checking_assert (!val
.known_isnan ());
594 REAL_VALUE_TYPE inf
= frange_val_max (type
);
595 r
.set (type
, val
.lower_bound (), inf
);
597 // Add both zeros if there's the possibility of zero equality.
598 frange_add_zeros (r
, type
);
603 // Build a range that is > VAL and store it in R. Return TRUE if
604 // further changes may be needed for R, or FALSE if R is in its final
608 build_gt (frange
&r
, tree type
, const frange
&val
)
610 gcc_checking_assert (!val
.known_isnan ());
612 // > +INF is outside the range.
613 if (real_isinf (&val
.lower_bound (), 0))
615 if (HONOR_NANS (type
))
622 REAL_VALUE_TYPE inf
= frange_val_max (type
);
623 REAL_VALUE_TYPE next
= val
.lower_bound ();
624 machine_mode mode
= TYPE_MODE (type
);
625 // Default to the conservatively correct closed ranges for
626 // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
627 // !HONOR_INFINITIES, nextafter will yield +INF, but frange::set()
628 // will crop the range appropriately.
629 if (!MODE_COMPOSITE_P (mode
))
630 frange_nextafter (mode
, next
, inf
);
631 r
.set (type
, next
, inf
);
637 operator_identity::fold_range (frange
&r
, tree
, const frange
&op1
,
638 const frange
&, relation_trio
) const
645 operator_identity::op1_range (frange
&r
, tree
, const frange
&lhs
,
646 const frange
&, relation_trio
) const
653 operator_cst::fold_range (frange
&r
, tree
, const frange
&op1
,
654 const frange
&, relation_trio
) const
661 operator_equal::op2_range (frange
&r
, tree type
,
662 const irange
&lhs
, const frange
&op1
,
663 relation_trio rel
) const
665 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
669 operator_equal::fold_range (irange
&r
, tree type
,
670 const frange
&op1
, const frange
&op2
,
671 relation_trio rel
) const
673 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_EQ
))
676 if (op1
.known_isnan () || op2
.known_isnan ())
677 r
= range_false (type
);
678 // We can be sure the values are always equal or not if both ranges
679 // consist of a single value, and then compare them.
680 else if (op1
.singleton_p () && op2
.singleton_p ())
683 r
= range_true (type
);
684 // If one operand is -0.0 and other 0.0, they are still equal.
685 else if (real_iszero (&op1
.lower_bound ())
686 && real_iszero (&op2
.lower_bound ()))
687 r
= range_true (type
);
689 r
= range_false (type
);
691 else if (real_iszero (&op1
.lower_bound ())
692 && real_iszero (&op1
.upper_bound ())
693 && real_iszero (&op2
.lower_bound ())
694 && real_iszero (&op2
.upper_bound ())
695 && !maybe_isnan (op1
, op2
))
696 // [-0.0, 0.0] == [-0.0, 0.0] or similar.
697 r
= range_true (type
);
700 // If ranges do not intersect, we know the range is not equal,
701 // otherwise we don't know anything for sure.
704 if (tmp
.undefined_p ())
706 // If one range is [whatever, -0.0] and another
707 // [0.0, whatever2], we don't know anything either,
708 // because -0.0 == 0.0.
709 if ((real_iszero (&op1
.upper_bound ())
710 && real_iszero (&op2
.lower_bound ()))
711 || (real_iszero (&op1
.lower_bound ())
712 && real_iszero (&op2
.upper_bound ())))
713 r
= range_true_and_false (type
);
715 r
= range_false (type
);
718 r
= range_true_and_false (type
);
724 operator_equal::op1_range (frange
&r
, tree type
,
727 relation_trio trio
) const
729 relation_kind rel
= trio
.op1_op2 ();
730 switch (get_bool_state (r
, lhs
, type
))
733 // The TRUE side of x == NAN is unreachable.
734 if (op2
.known_isnan ())
738 // If it's true, the result is the same as OP2.
740 // Add both zeros if there's the possibility of zero equality.
741 frange_add_zeros (r
, type
);
742 // The TRUE side of op1 == op2 implies op1 is !NAN.
748 // The FALSE side of op1 == op1 implies op1 is a NAN.
751 // On the FALSE side of x == NAN, we know nothing about x.
752 else if (op2
.known_isnan ())
753 r
.set_varying (type
);
754 // If the result is false, the only time we know anything is
755 // if OP2 is a constant.
756 else if (op2
.singleton_p ()
757 || (!op2
.maybe_isnan () && op2
.zero_p ()))
759 REAL_VALUE_TYPE tmp
= op2
.lower_bound ();
760 r
.set (type
, tmp
, tmp
, VR_ANTI_RANGE
);
763 r
.set_varying (type
);
772 // Check if the LHS range indicates a relation between OP1 and OP2.
775 operator_equal::op1_op2_relation (const irange
&lhs
, const frange
&,
776 const frange
&) const
778 if (lhs
.undefined_p ())
779 return VREL_UNDEFINED
;
781 // FALSE = op1 == op2 indicates NE_EXPR.
785 // TRUE = op1 == op2 indicates EQ_EXPR.
786 if (!contains_zero_p (lhs
))
792 operator_not_equal::fold_range (irange
&r
, tree type
,
793 const frange
&op1
, const frange
&op2
,
794 relation_trio trio
) const
796 relation_kind rel
= trio
.op1_op2 ();
798 // VREL_NE & NE_EXPR is always true, even with NANs.
801 r
= range_true (type
);
804 if (frelop_early_resolve (r
, type
, op1
, op2
, trio
, VREL_NE
))
807 // x != NAN is always TRUE.
808 if (op1
.known_isnan () || op2
.known_isnan ())
809 r
= range_true (type
);
810 // We can be sure the values are always equal or not if both ranges
811 // consist of a single value, and then compare them.
812 else if (op1
.singleton_p () && op2
.singleton_p ())
815 r
= range_false (type
);
816 // If one operand is -0.0 and other 0.0, they are still equal.
817 else if (real_iszero (&op1
.lower_bound ())
818 && real_iszero (&op2
.lower_bound ()))
819 r
= range_false (type
);
821 r
= range_true (type
);
823 else if (real_iszero (&op1
.lower_bound ())
824 && real_iszero (&op1
.upper_bound ())
825 && real_iszero (&op2
.lower_bound ())
826 && real_iszero (&op2
.upper_bound ())
827 && !maybe_isnan (op1
, op2
))
828 // [-0.0, 0.0] != [-0.0, 0.0] or similar.
829 r
= range_false (type
);
832 // If ranges do not intersect, we know the range is not equal,
833 // otherwise we don't know anything for sure.
836 if (tmp
.undefined_p ())
838 // If one range is [whatever, -0.0] and another
839 // [0.0, whatever2], we don't know anything either,
840 // because -0.0 == 0.0.
841 if ((real_iszero (&op1
.upper_bound ())
842 && real_iszero (&op2
.lower_bound ()))
843 || (real_iszero (&op1
.lower_bound ())
844 && real_iszero (&op2
.upper_bound ())))
845 r
= range_true_and_false (type
);
847 r
= range_true (type
);
850 r
= range_true_and_false (type
);
856 operator_not_equal::op1_range (frange
&r
, tree type
,
859 relation_trio trio
) const
861 relation_kind rel
= trio
.op1_op2 ();
862 switch (get_bool_state (r
, lhs
, type
))
865 // If the result is true, the only time we know anything is if
866 // OP2 is a constant.
867 if (op2
.singleton_p ())
869 // This is correct even if op1 is NAN, because the following
870 // range would be ~[tmp, tmp] with the NAN property set to
872 REAL_VALUE_TYPE tmp
= op2
.lower_bound ();
873 r
.set (type
, tmp
, tmp
, VR_ANTI_RANGE
);
875 // The TRUE side of op1 != op1 implies op1 is NAN.
876 else if (rel
== VREL_EQ
)
879 r
.set_varying (type
);
883 // The FALSE side of x != NAN is impossible.
884 if (op2
.known_isnan ())
888 // If it's false, the result is the same as OP2.
890 // Add both zeros if there's the possibility of zero equality.
891 frange_add_zeros (r
, type
);
892 // The FALSE side of op1 != op2 implies op1 is !NAN.
904 operator_not_equal::op2_range (frange
&r
, tree type
,
907 relation_trio trio
) const
909 return op1_range (r
, type
, lhs
, op1
, trio
);
912 // Check if the LHS range indicates a relation between OP1 and OP2.
915 operator_not_equal::op1_op2_relation (const irange
&lhs
, const frange
&,
916 const frange
&) const
918 if (lhs
.undefined_p ())
919 return VREL_UNDEFINED
;
921 // FALSE = op1 != op2 indicates EQ_EXPR.
925 // TRUE = op1 != op2 indicates NE_EXPR.
926 if (!contains_zero_p (lhs
))
932 operator_lt::fold_range (irange
&r
, tree type
,
933 const frange
&op1
, const frange
&op2
,
934 relation_trio trio
) const
936 relation_kind rel
= trio
.op1_op2 ();
938 // VREL_EQ & LT_EXPR is impossible, even with NANs.
941 r
= range_false (type
);
944 if (frelop_early_resolve (r
, type
, op1
, op2
, trio
, VREL_LT
))
947 if (op1
.known_isnan ()
948 || op2
.known_isnan ()
949 || !real_less (&op1
.lower_bound (), &op2
.upper_bound ()))
950 r
= range_false (type
);
951 else if (!maybe_isnan (op1
, op2
)
952 && real_less (&op1
.upper_bound (), &op2
.lower_bound ()))
953 r
= range_true (type
);
955 r
= range_true_and_false (type
);
960 operator_lt::op1_range (frange
&r
,
966 switch (get_bool_state (r
, lhs
, type
))
969 // The TRUE side of x < NAN is unreachable.
970 if (op2
.known_isnan ())
972 else if (op2
.undefined_p ())
974 else if (build_lt (r
, type
, op2
))
977 // x < y implies x is not +INF.
978 frange_drop_inf (r
, type
);
983 // On the FALSE side of x < NAN, we know nothing about x.
984 if (op2
.maybe_isnan ())
985 r
.set_varying (type
);
987 build_ge (r
, type
, op2
);
997 operator_lt::op2_range (frange
&r
,
1001 relation_trio
) const
1003 switch (get_bool_state (r
, lhs
, type
))
1006 // The TRUE side of NAN < x is unreachable.
1007 if (op1
.known_isnan ())
1009 else if (op1
.undefined_p ())
1011 else if (build_gt (r
, type
, op1
))
1014 // x < y implies y is not -INF.
1015 frange_drop_ninf (r
, type
);
1020 // On the FALSE side of NAN < x, we know nothing about x.
1021 if (op1
.maybe_isnan ())
1022 r
.set_varying (type
);
1024 build_le (r
, type
, op1
);
1034 // Check if the LHS range indicates a relation between OP1 and OP2.
1037 operator_lt::op1_op2_relation (const irange
&lhs
, const frange
&,
1038 const frange
&) const
1040 if (lhs
.undefined_p ())
1041 return VREL_UNDEFINED
;
1043 // FALSE = op1 < op2 indicates GE_EXPR.
1047 // TRUE = op1 < op2 indicates LT_EXPR.
1048 if (!contains_zero_p (lhs
))
1050 return VREL_VARYING
;
1054 operator_le::fold_range (irange
&r
, tree type
,
1055 const frange
&op1
, const frange
&op2
,
1056 relation_trio rel
) const
1058 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LE
))
1061 if (op1
.known_isnan ()
1062 || op2
.known_isnan ()
1063 || !real_compare (LE_EXPR
, &op1
.lower_bound (), &op2
.upper_bound ()))
1064 r
= range_false (type
);
1065 else if (!maybe_isnan (op1
, op2
)
1066 && real_compare (LE_EXPR
, &op1
.upper_bound (), &op2
.lower_bound ()))
1067 r
= range_true (type
);
1069 r
= range_true_and_false (type
);
1074 operator_le::op1_range (frange
&r
,
1078 relation_trio
) const
1080 switch (get_bool_state (r
, lhs
, type
))
1083 // The TRUE side of x <= NAN is unreachable.
1084 if (op2
.known_isnan ())
1086 else if (op2
.undefined_p ())
1088 else if (build_le (r
, type
, op2
))
1093 // On the FALSE side of x <= NAN, we know nothing about x.
1094 if (op2
.maybe_isnan ())
1095 r
.set_varying (type
);
1097 build_gt (r
, type
, op2
);
1107 operator_le::op2_range (frange
&r
,
1111 relation_trio
) const
1113 switch (get_bool_state (r
, lhs
, type
))
1116 // The TRUE side of NAN <= x is unreachable.
1117 if (op1
.known_isnan ())
1119 else if (op1
.undefined_p ())
1121 else if (build_ge (r
, type
, op1
))
1126 // On the FALSE side of NAN <= x, we know nothing about x.
1127 if (op1
.maybe_isnan ())
1128 r
.set_varying (type
);
1129 else if (op1
.undefined_p ())
1132 build_lt (r
, type
, op1
);
1141 // Check if the LHS range indicates a relation between OP1 and OP2.
1144 operator_le::op1_op2_relation (const irange
&lhs
, const frange
&,
1145 const frange
&) const
1147 if (lhs
.undefined_p ())
1148 return VREL_UNDEFINED
;
1150 // FALSE = op1 <= op2 indicates GT_EXPR.
1154 // TRUE = op1 <= op2 indicates LE_EXPR.
1155 if (!contains_zero_p (lhs
))
1157 return VREL_VARYING
;
1161 operator_gt::fold_range (irange
&r
, tree type
,
1162 const frange
&op1
, const frange
&op2
,
1163 relation_trio trio
) const
1165 relation_kind rel
= trio
.op1_op2 ();
1167 // VREL_EQ & GT_EXPR is impossible, even with NANs.
1170 r
= range_false (type
);
1173 if (frelop_early_resolve (r
, type
, op1
, op2
, trio
, VREL_GT
))
1176 if (op1
.known_isnan ()
1177 || op2
.known_isnan ()
1178 || !real_compare (GT_EXPR
, &op1
.upper_bound (), &op2
.lower_bound ()))
1179 r
= range_false (type
);
1180 else if (!maybe_isnan (op1
, op2
)
1181 && real_compare (GT_EXPR
, &op1
.lower_bound (), &op2
.upper_bound ()))
1182 r
= range_true (type
);
1184 r
= range_true_and_false (type
);
1189 operator_gt::op1_range (frange
&r
,
1193 relation_trio
) const
1195 switch (get_bool_state (r
, lhs
, type
))
1198 // The TRUE side of x > NAN is unreachable.
1199 if (op2
.known_isnan ())
1201 else if (op2
.undefined_p ())
1203 else if (build_gt (r
, type
, op2
))
1206 // x > y implies x is not -INF.
1207 frange_drop_ninf (r
, type
);
1212 // On the FALSE side of x > NAN, we know nothing about x.
1213 if (op2
.maybe_isnan ())
1214 r
.set_varying (type
);
1215 else if (op2
.undefined_p ())
1218 build_le (r
, type
, op2
);
1228 operator_gt::op2_range (frange
&r
,
1232 relation_trio
) const
1234 switch (get_bool_state (r
, lhs
, type
))
1237 // The TRUE side of NAN > x is unreachable.
1238 if (op1
.known_isnan ())
1240 else if (op1
.undefined_p ())
1242 else if (build_lt (r
, type
, op1
))
1245 // x > y implies y is not +INF.
1246 frange_drop_inf (r
, type
);
1251 // On The FALSE side of NAN > x, we know nothing about x.
1252 if (op1
.maybe_isnan ())
1253 r
.set_varying (type
);
1254 else if (op1
.undefined_p ())
1257 build_ge (r
, type
, op1
);
1266 // Check if the LHS range indicates a relation between OP1 and OP2.
1269 operator_gt::op1_op2_relation (const irange
&lhs
, const frange
&,
1270 const frange
&) const
1272 if (lhs
.undefined_p ())
1273 return VREL_UNDEFINED
;
1275 // FALSE = op1 > op2 indicates LE_EXPR.
1279 // TRUE = op1 > op2 indicates GT_EXPR.
1280 if (!contains_zero_p (lhs
))
1282 return VREL_VARYING
;
1286 operator_ge::fold_range (irange
&r
, tree type
,
1287 const frange
&op1
, const frange
&op2
,
1288 relation_trio rel
) const
1290 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GE
))
1293 if (op1
.known_isnan ()
1294 || op2
.known_isnan ()
1295 || !real_compare (GE_EXPR
, &op1
.upper_bound (), &op2
.lower_bound ()))
1296 r
= range_false (type
);
1297 else if (!maybe_isnan (op1
, op2
)
1298 && real_compare (GE_EXPR
, &op1
.lower_bound (), &op2
.upper_bound ()))
1299 r
= range_true (type
);
1301 r
= range_true_and_false (type
);
1306 operator_ge::op1_range (frange
&r
,
1310 relation_trio
) const
1312 switch (get_bool_state (r
, lhs
, type
))
1315 // The TRUE side of x >= NAN is unreachable.
1316 if (op2
.known_isnan ())
1318 else if (op2
.undefined_p ())
1320 else if (build_ge (r
, type
, op2
))
1325 // On the FALSE side of x >= NAN, we know nothing about x.
1326 if (op2
.maybe_isnan ())
1327 r
.set_varying (type
);
1328 else if (op2
.undefined_p ())
1331 build_lt (r
, type
, op2
);
1341 operator_ge::op2_range (frange
&r
, tree type
,
1344 relation_trio
) const
1346 switch (get_bool_state (r
, lhs
, type
))
1349 // The TRUE side of NAN >= x is unreachable.
1350 if (op1
.known_isnan ())
1352 else if (op1
.undefined_p ())
1354 else if (build_le (r
, type
, op1
))
1359 // On the FALSE side of NAN >= x, we know nothing about x.
1360 if (op1
.maybe_isnan ())
1361 r
.set_varying (type
);
1362 else if (op1
.undefined_p ())
1365 build_gt (r
, type
, op1
);
1374 // Check if the LHS range indicates a relation between OP1 and OP2.
1377 operator_ge::op1_op2_relation (const irange
&lhs
, const frange
&,
1378 const frange
&) const
1380 if (lhs
.undefined_p ())
1381 return VREL_UNDEFINED
;
1383 // FALSE = op1 >= op2 indicates LT_EXPR.
1387 // TRUE = op1 >= op2 indicates GE_EXPR.
1388 if (!contains_zero_p (lhs
))
1390 return VREL_VARYING
;
1393 // UNORDERED_EXPR comparison.
1395 class foperator_unordered
: public range_operator
1397 using range_operator::fold_range
;
1398 using range_operator::op1_range
;
1399 using range_operator::op2_range
;
1401 bool fold_range (irange
&r
, tree type
,
1402 const frange
&op1
, const frange
&op2
,
1403 relation_trio
= TRIO_VARYING
) const final override
;
1404 bool op1_range (frange
&r
, tree type
,
1405 const irange
&lhs
, const frange
&op2
,
1406 relation_trio
= TRIO_VARYING
) const final override
;
1407 bool op2_range (frange
&r
, tree type
,
1408 const irange
&lhs
, const frange
&op1
,
1409 relation_trio rel
= TRIO_VARYING
) const final override
1411 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1416 foperator_unordered::fold_range (irange
&r
, tree type
,
1417 const frange
&op1
, const frange
&op2
,
1418 relation_trio
) const
1420 // UNORDERED is TRUE if either operand is a NAN.
1421 if (op1
.known_isnan () || op2
.known_isnan ())
1422 r
= range_true (type
);
1423 // UNORDERED is FALSE if neither operand is a NAN.
1424 else if (!op1
.maybe_isnan () && !op2
.maybe_isnan ())
1425 r
= range_false (type
);
1427 r
= range_true_and_false (type
);
1432 foperator_unordered::op1_range (frange
&r
, tree type
,
1435 relation_trio trio
) const
1437 relation_kind rel
= trio
.op1_op2 ();
1438 switch (get_bool_state (r
, lhs
, type
))
1441 // Since at least one operand must be NAN, if one of them is
1442 // not, the other must be.
1443 if (rel
== VREL_EQ
|| !op2
.maybe_isnan ())
1446 r
.set_varying (type
);
1450 // A false UNORDERED means both operands are !NAN, so it's
1451 // impossible for op2 to be a NAN.
1452 if (op2
.known_isnan ())
1456 r
.set_varying (type
);
1467 // ORDERED_EXPR comparison.
1469 class foperator_ordered
: public range_operator
1471 using range_operator::fold_range
;
1472 using range_operator::op1_range
;
1473 using range_operator::op2_range
;
1475 bool fold_range (irange
&r
, tree type
,
1476 const frange
&op1
, const frange
&op2
,
1477 relation_trio
= TRIO_VARYING
) const final override
;
1478 bool op1_range (frange
&r
, tree type
,
1479 const irange
&lhs
, const frange
&op2
,
1480 relation_trio
= TRIO_VARYING
) const final override
;
1481 bool op2_range (frange
&r
, tree type
,
1482 const irange
&lhs
, const frange
&op1
,
1483 relation_trio rel
= TRIO_VARYING
) const final override
1485 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1490 foperator_ordered::fold_range (irange
&r
, tree type
,
1491 const frange
&op1
, const frange
&op2
,
1492 relation_trio
) const
1494 if (op1
.known_isnan () || op2
.known_isnan ())
1495 r
= range_false (type
);
1496 else if (!op1
.maybe_isnan () && !op2
.maybe_isnan ())
1497 r
= range_true (type
);
1499 r
= range_true_and_false (type
);
1504 foperator_ordered::op1_range (frange
&r
, tree type
,
1507 relation_trio trio
) const
1509 relation_kind rel
= trio
.op1_op2 ();
1510 switch (get_bool_state (r
, lhs
, type
))
1513 // The TRUE side of ORDERED means both operands are !NAN, so
1514 // it's impossible for op2 to be a NAN.
1515 if (op2
.known_isnan ())
1519 r
.set_varying (type
);
1525 // The FALSE side of op1 ORDERED op1 implies op1 is NAN.
1529 r
.set_varying (type
);
1539 operator_negate::fold_range (frange
&r
, tree type
,
1540 const frange
&op1
, const frange
&op2
,
1541 relation_trio
) const
1543 if (empty_range_varying (r
, type
, op1
, op2
))
1545 if (op1
.known_isnan ())
1548 if (op1
.nan_signbit_p (sign
))
1549 r
.set_nan (type
, !sign
);
1555 REAL_VALUE_TYPE lh_lb
= op1
.lower_bound ();
1556 REAL_VALUE_TYPE lh_ub
= op1
.upper_bound ();
1557 lh_lb
= real_value_negate (&lh_lb
);
1558 lh_ub
= real_value_negate (&lh_ub
);
1559 r
.set (type
, lh_ub
, lh_lb
);
1560 if (op1
.maybe_isnan ())
1563 if (op1
.nan_signbit_p (sign
))
1564 r
.update_nan (!sign
);
1574 operator_negate::op1_range (frange
&r
, tree type
,
1575 const frange
&lhs
, const frange
&op2
,
1576 relation_trio rel
) const
1578 return fold_range (r
, type
, lhs
, op2
, rel
);
1582 operator_abs::fold_range (frange
&r
, tree type
,
1583 const frange
&op1
, const frange
&op2
,
1584 relation_trio
) const
1586 if (empty_range_varying (r
, type
, op1
, op2
))
1588 if (op1
.known_isnan ())
1590 r
.set_nan (type
, /*sign=*/false);
1594 const REAL_VALUE_TYPE lh_lb
= op1
.lower_bound ();
1595 const REAL_VALUE_TYPE lh_ub
= op1
.upper_bound ();
1596 // Handle the easy case where everything is positive.
1597 if (real_compare (GE_EXPR
, &lh_lb
, &dconst0
)
1598 && !real_iszero (&lh_lb
, /*sign=*/true)
1599 && !op1
.maybe_isnan (/*sign=*/true))
1605 REAL_VALUE_TYPE min
= real_value_abs (&lh_lb
);
1606 REAL_VALUE_TYPE max
= real_value_abs (&lh_ub
);
1607 // If the range contains zero then we know that the minimum value in the
1608 // range will be zero.
1609 if (real_compare (LE_EXPR
, &lh_lb
, &dconst0
)
1610 && real_compare (GE_EXPR
, &lh_ub
, &dconst0
))
1612 if (real_compare (GT_EXPR
, &min
, &max
))
1618 // If the range was reversed, swap MIN and MAX.
1619 if (real_compare (GT_EXPR
, &min
, &max
))
1620 std::swap (min
, max
);
1623 r
.set (type
, min
, max
);
1624 if (op1
.maybe_isnan ())
1625 r
.update_nan (/*sign=*/false);
1632 operator_abs::op1_range (frange
&r
, tree type
,
1633 const frange
&lhs
, const frange
&op2
,
1634 relation_trio
) const
1636 if (empty_range_varying (r
, type
, lhs
, op2
))
1638 if (lhs
.known_isnan ())
1644 // Start with the positives because negatives are an impossible result.
1645 frange
positives (type
, dconst0
, frange_val_max (type
));
1646 positives
.update_nan (/*sign=*/false);
1647 positives
.intersect (lhs
);
1649 // Add -NAN if relevant.
1650 if (r
.maybe_isnan ())
1653 neg_nan
.set_nan (type
, true);
1656 if (r
.known_isnan () || r
.undefined_p ())
1658 // Then add the negative of each pair:
1659 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
1660 frange
negatives (type
, real_value_negate (&positives
.upper_bound ()),
1661 real_value_negate (&positives
.lower_bound ()));
1662 negatives
.clear_nan ();
1663 r
.union_ (negatives
);
1667 class foperator_unordered_lt
: public range_operator
1669 using range_operator::fold_range
;
1670 using range_operator::op1_range
;
1671 using range_operator::op2_range
;
1673 bool fold_range (irange
&r
, tree type
,
1674 const frange
&op1
, const frange
&op2
,
1675 relation_trio trio
= TRIO_VARYING
) const final override
1677 relation_kind rel
= trio
.op1_op2 ();
1679 if (op1
.known_isnan () || op2
.known_isnan ()
1682 r
= range_true (type
);
1685 frange op1_no_nan
= op1
;
1686 frange op2_no_nan
= op2
;
1687 if (op1
.maybe_isnan ())
1688 op1_no_nan
.clear_nan ();
1689 if (op2
.maybe_isnan ())
1690 op2_no_nan
.clear_nan ();
1691 if (!range_op_handler (LT_EXPR
).fold_range (r
, type
, op1_no_nan
,
1694 // The result is the same as the ordered version when the
1695 // comparison is true or when the operands cannot be NANs.
1696 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1700 r
= range_true_and_false (type
);
1704 bool op1_range (frange
&r
, tree type
,
1707 relation_trio trio
) const final override
;
1708 bool op2_range (frange
&r
, tree type
,
1711 relation_trio trio
) const final override
;
1715 foperator_unordered_lt::op1_range (frange
&r
, tree type
,
1718 relation_trio
) const
1720 switch (get_bool_state (r
, lhs
, type
))
1723 if (op2
.maybe_isnan ())
1724 r
.set_varying (type
);
1725 else if (op2
.undefined_p ())
1728 build_lt (r
, type
, op2
);
1732 // A false UNORDERED_LT means both operands are !NAN, so it's
1733 // impossible for op2 to be a NAN.
1734 if (op2
.known_isnan ())
1736 else if (op2
.undefined_p ())
1738 else if (build_ge (r
, type
, op2
))
1749 foperator_unordered_lt::op2_range (frange
&r
, tree type
,
1752 relation_trio
) const
1754 switch (get_bool_state (r
, lhs
, type
))
1757 if (op1
.maybe_isnan ())
1758 r
.set_varying (type
);
1759 else if (op1
.undefined_p ())
1762 build_gt (r
, type
, op1
);
1766 // A false UNORDERED_LT means both operands are !NAN, so it's
1767 // impossible for op1 to be a NAN.
1768 if (op1
.known_isnan ())
1770 else if (op1
.undefined_p ())
1772 else if (build_le (r
, type
, op1
))
1782 class foperator_unordered_le
: public range_operator
1784 using range_operator::fold_range
;
1785 using range_operator::op1_range
;
1786 using range_operator::op2_range
;
1788 bool fold_range (irange
&r
, tree type
,
1789 const frange
&op1
, const frange
&op2
,
1790 relation_trio trio
= TRIO_VARYING
) const final override
1792 relation_kind rel
= trio
.op1_op2 ();
1794 if (op1
.known_isnan () || op2
.known_isnan ()
1797 r
= range_true (type
);
1800 frange op1_no_nan
= op1
;
1801 frange op2_no_nan
= op2
;
1802 if (op1
.maybe_isnan ())
1803 op1_no_nan
.clear_nan ();
1804 if (op2
.maybe_isnan ())
1805 op2_no_nan
.clear_nan ();
1806 if (!range_op_handler (LE_EXPR
).fold_range (r
, type
, op1_no_nan
,
1809 // The result is the same as the ordered version when the
1810 // comparison is true or when the operands cannot be NANs.
1811 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1815 r
= range_true_and_false (type
);
1819 bool op1_range (frange
&r
, tree type
,
1820 const irange
&lhs
, const frange
&op2
,
1821 relation_trio
= TRIO_VARYING
) const final override
;
1822 bool op2_range (frange
&r
, tree type
,
1823 const irange
&lhs
, const frange
&op1
,
1824 relation_trio
= TRIO_VARYING
) const final override
;
1828 foperator_unordered_le::op1_range (frange
&r
, tree type
,
1829 const irange
&lhs
, const frange
&op2
,
1830 relation_trio
) const
1832 switch (get_bool_state (r
, lhs
, type
))
1835 if (op2
.maybe_isnan ())
1836 r
.set_varying (type
);
1837 else if (op2
.undefined_p ())
1840 build_le (r
, type
, op2
);
1844 // A false UNORDERED_LE means both operands are !NAN, so it's
1845 // impossible for op2 to be a NAN.
1846 if (op2
.known_isnan ())
1848 else if (build_gt (r
, type
, op2
))
1859 foperator_unordered_le::op2_range (frange
&r
,
1863 relation_trio
) const
1865 switch (get_bool_state (r
, lhs
, type
))
1868 if (op1
.maybe_isnan ())
1869 r
.set_varying (type
);
1870 else if (op1
.undefined_p ())
1873 build_ge (r
, type
, op1
);
1877 // A false UNORDERED_LE means both operands are !NAN, so it's
1878 // impossible for op1 to be a NAN.
1879 if (op1
.known_isnan ())
1881 else if (op1
.undefined_p ())
1883 else if (build_lt (r
, type
, op1
))
1893 class foperator_unordered_gt
: public range_operator
1895 using range_operator::fold_range
;
1896 using range_operator::op1_range
;
1897 using range_operator::op2_range
;
1899 bool fold_range (irange
&r
, tree type
,
1900 const frange
&op1
, const frange
&op2
,
1901 relation_trio trio
= TRIO_VARYING
) const final override
1903 relation_kind rel
= trio
.op1_op2 ();
1905 if (op1
.known_isnan () || op2
.known_isnan ()
1908 r
= range_true (type
);
1911 frange op1_no_nan
= op1
;
1912 frange op2_no_nan
= op2
;
1913 if (op1
.maybe_isnan ())
1914 op1_no_nan
.clear_nan ();
1915 if (op2
.maybe_isnan ())
1916 op2_no_nan
.clear_nan ();
1917 if (!range_op_handler (GT_EXPR
).fold_range (r
, type
, op1_no_nan
,
1920 // The result is the same as the ordered version when the
1921 // comparison is true or when the operands cannot be NANs.
1922 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1926 r
= range_true_and_false (type
);
1930 bool op1_range (frange
&r
, tree type
,
1931 const irange
&lhs
, const frange
&op2
,
1932 relation_trio
= TRIO_VARYING
) const final override
;
1933 bool op2_range (frange
&r
, tree type
,
1934 const irange
&lhs
, const frange
&op1
,
1935 relation_trio
= TRIO_VARYING
) const final override
;
1939 foperator_unordered_gt::op1_range (frange
&r
,
1943 relation_trio
) const
1945 switch (get_bool_state (r
, lhs
, type
))
1948 if (op2
.maybe_isnan ())
1949 r
.set_varying (type
);
1950 else if (op2
.undefined_p ())
1953 build_gt (r
, type
, op2
);
1957 // A false UNORDERED_GT means both operands are !NAN, so it's
1958 // impossible for op2 to be a NAN.
1959 if (op2
.known_isnan ())
1961 else if (op2
.undefined_p ())
1963 else if (build_le (r
, type
, op2
))
1974 foperator_unordered_gt::op2_range (frange
&r
,
1978 relation_trio
) const
1980 switch (get_bool_state (r
, lhs
, type
))
1983 if (op1
.maybe_isnan ())
1984 r
.set_varying (type
);
1985 else if (op1
.undefined_p ())
1988 build_lt (r
, type
, op1
);
1992 // A false UNORDERED_GT means both operands are !NAN, so it's
1993 // impossible for op1 to be a NAN.
1994 if (op1
.known_isnan ())
1996 else if (op1
.undefined_p ())
1998 else if (build_ge (r
, type
, op1
))
2008 class foperator_unordered_ge
: public range_operator
2010 using range_operator::fold_range
;
2011 using range_operator::op1_range
;
2012 using range_operator::op2_range
;
2014 bool fold_range (irange
&r
, tree type
,
2015 const frange
&op1
, const frange
&op2
,
2016 relation_trio trio
= TRIO_VARYING
) const final override
2018 relation_kind rel
= trio
.op1_op2 ();
2020 if (op1
.known_isnan () || op2
.known_isnan ()
2023 r
= range_true (type
);
2026 frange op1_no_nan
= op1
;
2027 frange op2_no_nan
= op2
;
2028 if (op1
.maybe_isnan ())
2029 op1_no_nan
.clear_nan ();
2030 if (op2
.maybe_isnan ())
2031 op2_no_nan
.clear_nan ();
2032 if (!range_op_handler (GE_EXPR
).fold_range (r
, type
, op1_no_nan
,
2035 // The result is the same as the ordered version when the
2036 // comparison is true or when the operands cannot be NANs.
2037 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
2041 r
= range_true_and_false (type
);
2045 bool op1_range (frange
&r
, tree type
,
2046 const irange
&lhs
, const frange
&op2
,
2047 relation_trio
= TRIO_VARYING
) const final override
;
2048 bool op2_range (frange
&r
, tree type
,
2049 const irange
&lhs
, const frange
&op1
,
2050 relation_trio
= TRIO_VARYING
) const final override
;
2054 foperator_unordered_ge::op1_range (frange
&r
,
2058 relation_trio
) const
2060 switch (get_bool_state (r
, lhs
, type
))
2063 if (op2
.maybe_isnan ())
2064 r
.set_varying (type
);
2065 else if (op2
.undefined_p ())
2068 build_ge (r
, type
, op2
);
2072 // A false UNORDERED_GE means both operands are !NAN, so it's
2073 // impossible for op2 to be a NAN.
2074 if (op2
.known_isnan ())
2076 else if (op2
.undefined_p ())
2078 else if (build_lt (r
, type
, op2
))
2089 foperator_unordered_ge::op2_range (frange
&r
, tree type
,
2092 relation_trio
) const
2094 switch (get_bool_state (r
, lhs
, type
))
2097 if (op1
.maybe_isnan ())
2098 r
.set_varying (type
);
2099 else if (op1
.undefined_p ())
2102 build_le (r
, type
, op1
);
2106 // A false UNORDERED_GE means both operands are !NAN, so it's
2107 // impossible for op1 to be a NAN.
2108 if (op1
.known_isnan ())
2110 else if (op1
.undefined_p ())
2112 else if (build_gt (r
, type
, op1
))
2122 class foperator_unordered_equal
: public range_operator
2124 using range_operator::fold_range
;
2125 using range_operator::op1_range
;
2126 using range_operator::op2_range
;
2128 bool fold_range (irange
&r
, tree type
,
2129 const frange
&op1
, const frange
&op2
,
2130 relation_trio trio
= TRIO_VARYING
) const final override
2132 relation_kind rel
= trio
.op1_op2 ();
2134 if (op1
.known_isnan () || op2
.known_isnan ()
2137 r
= range_true (type
);
2140 frange op1_no_nan
= op1
;
2141 frange op2_no_nan
= op2
;
2142 if (op1
.maybe_isnan ())
2143 op1_no_nan
.clear_nan ();
2144 if (op2
.maybe_isnan ())
2145 op2_no_nan
.clear_nan ();
2146 if (!range_op_handler (EQ_EXPR
).fold_range (r
, type
, op1_no_nan
,
2149 // The result is the same as the ordered version when the
2150 // comparison is true or when the operands cannot be NANs.
2151 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
2155 r
= range_true_and_false (type
);
2159 bool op1_range (frange
&r
, tree type
,
2160 const irange
&lhs
, const frange
&op2
,
2161 relation_trio
= TRIO_VARYING
) const final override
;
2162 bool op2_range (frange
&r
, tree type
,
2163 const irange
&lhs
, const frange
&op1
,
2164 relation_trio rel
= TRIO_VARYING
) const final override
2166 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
2168 } fop_unordered_equal
;
2171 foperator_unordered_equal::op1_range (frange
&r
, tree type
,
2174 relation_trio
) const
2176 switch (get_bool_state (r
, lhs
, type
))
2179 // If it's true, the result is the same as OP2 plus a NAN.
2181 // Add both zeros if there's the possibility of zero equality.
2182 frange_add_zeros (r
, type
);
2183 // Add the possibility of a NAN.
2188 // A false UNORDERED_EQ means both operands are !NAN, so it's
2189 // impossible for op2 to be a NAN.
2190 if (op2
.known_isnan ())
2194 // The false side indicates !NAN and not equal. We can at least
2196 r
.set_varying (type
);
2207 class foperator_ltgt
: public range_operator
2209 using range_operator::fold_range
;
2210 using range_operator::op1_range
;
2211 using range_operator::op2_range
;
2213 bool fold_range (irange
&r
, tree type
,
2214 const frange
&op1
, const frange
&op2
,
2215 relation_trio trio
= TRIO_VARYING
) const final override
2217 if (op1
.known_isnan () || op2
.known_isnan ())
2219 r
= range_false (type
);
2222 frange op1_no_nan
= op1
;
2223 frange op2_no_nan
= op2
;
2224 if (op1
.maybe_isnan ())
2225 op1_no_nan
.clear_nan ();
2226 if (op2
.maybe_isnan ())
2227 op2_no_nan
.clear_nan ();
2228 if (!range_op_handler (NE_EXPR
).fold_range (r
, type
, op1_no_nan
,
2231 // The result is the same as the ordered version when the
2232 // comparison is true or when the operands cannot be NANs.
2233 if (!maybe_isnan (op1
, op2
) || r
== range_false (type
))
2237 r
= range_true_and_false (type
);
2241 bool op1_range (frange
&r
, tree type
,
2242 const irange
&lhs
, const frange
&op2
,
2243 relation_trio
= TRIO_VARYING
) const final override
;
2244 bool op2_range (frange
&r
, tree type
,
2245 const irange
&lhs
, const frange
&op1
,
2246 relation_trio rel
= TRIO_VARYING
) const final override
2248 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
2253 foperator_ltgt::op1_range (frange
&r
, tree type
,
2256 relation_trio
) const
2258 switch (get_bool_state (r
, lhs
, type
))
2261 // A true LTGT means both operands are !NAN, so it's
2262 // impossible for op2 to be a NAN.
2263 if (op2
.known_isnan ())
2267 // The true side indicates !NAN and not equal. We can at least
2269 r
.set_varying (type
);
2275 // If it's false, the result is the same as OP2 plus a NAN.
2277 // Add both zeros if there's the possibility of zero equality.
2278 frange_add_zeros (r
, type
);
2279 // Add the possibility of a NAN.
2289 // Final tweaks for float binary op op1_range/op2_range.
2290 // Return TRUE if the operation is performed and a valid range is available.
2293 float_binary_op_range_finish (bool ret
, frange
&r
, tree type
,
2294 const frange
&lhs
, bool div_op2
= false)
2299 // If we get a known NAN from reverse op, it means either that
2300 // the other operand was known NAN (in that case we know nothing),
2301 // or the reverse operation introduced a known NAN.
2302 // Say for lhs = op1 * op2 if lhs is [-0, +0] and op2 is too,
2303 // 0 / 0 is known NAN. Just punt in that case.
2304 // If NANs aren't honored, we get for 0 / 0 UNDEFINED, so punt as well.
2305 // Or if lhs is a known NAN, we also don't know anything.
2306 if (r
.known_isnan () || lhs
.known_isnan () || r
.undefined_p ())
2308 r
.set_varying (type
);
2312 // If lhs isn't NAN, then neither operand could be NAN,
2313 // even if the reverse operation does introduce a maybe_nan.
2314 if (!lhs
.maybe_isnan ())
2318 ? !(real_compare (LE_EXPR
, &lhs
.lower_bound (), &dconst0
)
2319 && real_compare (GE_EXPR
, &lhs
.upper_bound (), &dconst0
))
2320 : !(real_isinf (&lhs
.lower_bound ())
2321 || real_isinf (&lhs
.upper_bound ())))
2322 // For reverse + or - or * or op1 of /, if result is finite, then
2323 // r must be finite too, as X + INF or X - INF or X * INF or
2324 // INF / X is always +-INF or NAN. For op2 of /, if result is
2325 // non-zero and not NAN, r must be finite, as X / INF is always
2327 frange_drop_infs (r
, type
);
2329 // If lhs is a maybe or known NAN, the operand could be
2336 // True if [lb, ub] is [+-0, +-0].
2338 zero_p (const REAL_VALUE_TYPE
&lb
, const REAL_VALUE_TYPE
&ub
)
2340 return real_iszero (&lb
) && real_iszero (&ub
);
2343 // True if +0 or -0 is in [lb, ub] range.
2345 contains_zero_p (const REAL_VALUE_TYPE
&lb
, const REAL_VALUE_TYPE
&ub
)
2347 return (real_compare (LE_EXPR
, &lb
, &dconst0
)
2348 && real_compare (GE_EXPR
, &ub
, &dconst0
));
2351 // True if [lb, ub] is [-INF, -INF] or [+INF, +INF].
2353 singleton_inf_p (const REAL_VALUE_TYPE
&lb
, const REAL_VALUE_TYPE
&ub
)
2355 return real_isinf (&lb
) && real_isinf (&ub
, real_isneg (&lb
));
2358 // Return -1 if binary op result must have sign bit set,
2359 // 1 if binary op result must have sign bit clear,
2361 // Sign bit of binary op result is exclusive or of the
2362 // operand's sign bits.
2364 signbit_known_p (const REAL_VALUE_TYPE
&lh_lb
, const REAL_VALUE_TYPE
&lh_ub
,
2365 const REAL_VALUE_TYPE
&rh_lb
, const REAL_VALUE_TYPE
&rh_ub
)
2367 if (real_isneg (&lh_lb
) == real_isneg (&lh_ub
)
2368 && real_isneg (&rh_lb
) == real_isneg (&rh_ub
))
2370 if (real_isneg (&lh_lb
) == real_isneg (&rh_ub
))
2378 // Set [lb, ub] to [-0, -0], [-0, +0] or [+0, +0] depending on
2381 zero_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, int signbit_known
)
2384 if (signbit_known
<= 0)
2386 if (signbit_known
< 0)
2390 // Set [lb, ub] to [-INF, -INF], [-INF, +INF] or [+INF, +INF] depending on
2393 inf_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, int signbit_known
)
2395 if (signbit_known
> 0)
2396 ub
= lb
= dconstinf
;
2397 else if (signbit_known
< 0)
2398 ub
= lb
= dconstninf
;
2406 // Set [lb, ub] to [-INF, -0], [-INF, +INF] or [+0, +INF] depending on
2409 zero_to_inf_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, int signbit_known
)
2411 if (signbit_known
> 0)
2416 else if (signbit_known
< 0)
2428 /* Extend the LHS range by 1ulp in each direction. For op1_range
2429 or op2_range of binary operations just computing the inverse
2430 operation on ranges isn't sufficient. Consider e.g.
2431 [1., 1.] = op1 + [1., 1.]. op1's range is not [0., 0.], but
2432 [-0x1.0p-54, 0x1.0p-53] (when not -frounding-math), any value for
2433 which adding 1. to it results in 1. after rounding to nearest.
2434 So, for op1_range/op2_range extend the lhs range by 1ulp (or 0.5ulp)
2435 in each direction. See PR109008 for more details. */
2438 float_widen_lhs_range (tree type
, const frange
&lhs
)
2441 if (lhs
.known_isnan ())
2443 REAL_VALUE_TYPE lb
= lhs
.lower_bound ();
2444 REAL_VALUE_TYPE ub
= lhs
.upper_bound ();
2445 if (real_isfinite (&lb
))
2447 frange_nextafter (TYPE_MODE (type
), lb
, dconstninf
);
2448 if (real_isinf (&lb
))
2450 /* For -DBL_MAX, instead of -Inf use
2451 nexttoward (-DBL_MAX, -LDBL_MAX) in a hypothetical
2452 wider type with the same mantissa precision but larger
2453 exponent range; it is outside of range of double values,
2454 but makes it clear it is just one ulp larger rather than
2455 infinite amount larger. */
2457 SET_REAL_EXP (&lb
, FLOAT_MODE_FORMAT (TYPE_MODE (type
))->emax
+ 1);
2459 if (!flag_rounding_math
&& !MODE_COMPOSITE_P (TYPE_MODE (type
)))
2461 /* If not -frounding-math nor IBM double double, actually widen
2462 just by 0.5ulp rather than 1ulp. */
2463 REAL_VALUE_TYPE tem
;
2464 real_arithmetic (&tem
, PLUS_EXPR
, &lhs
.lower_bound (), &lb
);
2465 real_arithmetic (&lb
, RDIV_EXPR
, &tem
, &dconst2
);
2468 if (real_isfinite (&ub
))
2470 frange_nextafter (TYPE_MODE (type
), ub
, dconstinf
);
2471 if (real_isinf (&ub
))
2473 /* For DBL_MAX similarly. */
2475 SET_REAL_EXP (&ub
, FLOAT_MODE_FORMAT (TYPE_MODE (type
))->emax
+ 1);
2477 if (!flag_rounding_math
&& !MODE_COMPOSITE_P (TYPE_MODE (type
)))
2479 /* If not -frounding-math nor IBM double double, actually widen
2480 just by 0.5ulp rather than 1ulp. */
2481 REAL_VALUE_TYPE tem
;
2482 real_arithmetic (&tem
, PLUS_EXPR
, &lhs
.upper_bound (), &ub
);
2483 real_arithmetic (&ub
, RDIV_EXPR
, &tem
, &dconst2
);
2486 /* Temporarily disable -ffinite-math-only, so that frange::set doesn't
2487 reduce the range back to real_min_representable (type) as lower bound
2488 or real_max_representable (type) as upper bound. */
2489 bool save_flag_finite_math_only
= flag_finite_math_only
;
2490 flag_finite_math_only
= false;
2491 ret
.set (type
, lb
, ub
, lhs
.get_nan_state ());
2492 flag_finite_math_only
= save_flag_finite_math_only
;
2497 operator_plus::op1_range (frange
&r
, tree type
, const frange
&lhs
,
2498 const frange
&op2
, relation_trio
) const
2500 if (lhs
.undefined_p ())
2502 range_op_handler
minus (MINUS_EXPR
);
2505 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2506 return float_binary_op_range_finish (minus
.fold_range (r
, type
, wlhs
, op2
),
2511 operator_plus::op2_range (frange
&r
, tree type
,
2512 const frange
&lhs
, const frange
&op1
,
2513 relation_trio
) const
2515 return op1_range (r
, type
, lhs
, op1
);
2519 operator_plus::rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2520 bool &maybe_nan
, tree type
,
2521 const REAL_VALUE_TYPE
&lh_lb
,
2522 const REAL_VALUE_TYPE
&lh_ub
,
2523 const REAL_VALUE_TYPE
&rh_lb
,
2524 const REAL_VALUE_TYPE
&rh_ub
,
2525 relation_kind
) const
2527 frange_arithmetic (PLUS_EXPR
, type
, lb
, lh_lb
, rh_lb
, dconstninf
);
2528 frange_arithmetic (PLUS_EXPR
, type
, ub
, lh_ub
, rh_ub
, dconstinf
);
2530 // [-INF] + [+INF] = NAN
2531 if (real_isinf (&lh_lb
, true) && real_isinf (&rh_ub
, false))
2533 // [+INF] + [-INF] = NAN
2534 else if (real_isinf (&lh_ub
, false) && real_isinf (&rh_lb
, true))
2542 operator_minus::op1_range (frange
&r
, tree type
,
2543 const frange
&lhs
, const frange
&op2
,
2544 relation_trio
) const
2546 if (lhs
.undefined_p ())
2548 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2549 return float_binary_op_range_finish (
2550 range_op_handler (PLUS_EXPR
).fold_range (r
, type
, wlhs
, op2
),
2555 operator_minus::op2_range (frange
&r
, tree type
,
2556 const frange
&lhs
, const frange
&op1
,
2557 relation_trio
) const
2559 if (lhs
.undefined_p ())
2561 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2562 return float_binary_op_range_finish (fold_range (r
, type
, op1
, wlhs
),
2567 operator_minus::rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2568 bool &maybe_nan
, tree type
,
2569 const REAL_VALUE_TYPE
&lh_lb
,
2570 const REAL_VALUE_TYPE
&lh_ub
,
2571 const REAL_VALUE_TYPE
&rh_lb
,
2572 const REAL_VALUE_TYPE
&rh_ub
,
2573 relation_kind
) const
2575 frange_arithmetic (MINUS_EXPR
, type
, lb
, lh_lb
, rh_ub
, dconstninf
);
2576 frange_arithmetic (MINUS_EXPR
, type
, ub
, lh_ub
, rh_lb
, dconstinf
);
2578 // [+INF] - [+INF] = NAN
2579 if (real_isinf (&lh_ub
, false) && real_isinf (&rh_ub
, false))
2581 // [-INF] - [-INF] = NAN
2582 else if (real_isinf (&lh_lb
, true) && real_isinf (&rh_lb
, true))
2589 // Given CP[0] to CP[3] floating point values rounded to -INF,
2590 // set LB to the smallest of them (treating -0 as smaller to +0).
2591 // Given CP[4] to CP[7] floating point values rounded to +INF,
2592 // set UB to the largest of them (treating -0 as smaller to +0).
2595 find_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2596 const REAL_VALUE_TYPE (&cp
)[8])
2600 for (int i
= 1; i
< 4; ++i
)
2602 if (real_less (&cp
[i
], &lb
)
2603 || (real_iszero (&lb
) && real_isnegzero (&cp
[i
])))
2605 if (real_less (&ub
, &cp
[i
+ 4])
2606 || (real_isnegzero (&ub
) && real_iszero (&cp
[i
+ 4])))
2613 operator_mult::op1_range (frange
&r
, tree type
,
2614 const frange
&lhs
, const frange
&op2
,
2615 relation_trio
) const
2617 if (lhs
.undefined_p ())
2619 range_op_handler
rdiv (RDIV_EXPR
);
2622 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2623 bool ret
= rdiv
.fold_range (r
, type
, wlhs
, op2
);
2626 if (wlhs
.known_isnan () || op2
.known_isnan () || op2
.undefined_p ())
2627 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2628 const REAL_VALUE_TYPE
&lhs_lb
= wlhs
.lower_bound ();
2629 const REAL_VALUE_TYPE
&lhs_ub
= wlhs
.upper_bound ();
2630 const REAL_VALUE_TYPE
&op2_lb
= op2
.lower_bound ();
2631 const REAL_VALUE_TYPE
&op2_ub
= op2
.upper_bound ();
2632 if ((contains_zero_p (lhs_lb
, lhs_ub
) && contains_zero_p (op2_lb
, op2_ub
))
2633 || ((real_isinf (&lhs_lb
) || real_isinf (&lhs_ub
))
2634 && (real_isinf (&op2_lb
) || real_isinf (&op2_ub
))))
2636 // If both lhs and op2 could be zeros or both could be infinities,
2637 // we don't know anything about op1 except maybe for the sign
2638 // and perhaps if it can be NAN or not.
2639 REAL_VALUE_TYPE lb
, ub
;
2640 int signbit_known
= signbit_known_p (lhs_lb
, lhs_ub
, op2_lb
, op2_ub
);
2641 zero_to_inf_range (lb
, ub
, signbit_known
);
2642 r
.set (type
, lb
, ub
);
2644 // Otherwise, if op2 is a singleton INF and lhs doesn't include INF,
2645 // or if lhs must be zero and op2 doesn't include zero, it would be
2646 // UNDEFINED, while rdiv.fold_range computes a zero or singleton INF
2647 // range. Those are supersets of UNDEFINED, so let's keep that way.
2648 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2652 operator_mult::op2_range (frange
&r
, tree type
,
2653 const frange
&lhs
, const frange
&op1
,
2654 relation_trio
) const
2656 return op1_range (r
, type
, lhs
, op1
);
2660 operator_mult::rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2661 bool &maybe_nan
, tree type
,
2662 const REAL_VALUE_TYPE
&lh_lb
,
2663 const REAL_VALUE_TYPE
&lh_ub
,
2664 const REAL_VALUE_TYPE
&rh_lb
,
2665 const REAL_VALUE_TYPE
&rh_ub
,
2666 relation_kind kind
) const
2670 && real_equal (&lh_lb
, &rh_lb
)
2671 && real_equal (&lh_ub
, &rh_ub
)
2672 && real_isneg (&lh_lb
) == real_isneg (&rh_lb
)
2673 && real_isneg (&lh_ub
) == real_isneg (&rh_ub
));
2676 // x * x never produces a new NAN and we only multiply the same
2677 // values, so the 0 * INF problematic cases never appear there.
2680 // [+-0, +-0] * [+INF,+INF] (or [-INF,-INF] or swapped is a known NAN.
2681 if ((zero_p (lh_lb
, lh_ub
) && singleton_inf_p (rh_lb
, rh_ub
))
2682 || (zero_p (rh_lb
, rh_ub
) && singleton_inf_p (lh_lb
, lh_ub
)))
2684 real_nan (&lb
, "", 0, TYPE_MODE (type
));
2690 // Otherwise, if one range includes zero and the other ends with +-INF,
2691 // it is a maybe NAN.
2692 if ((contains_zero_p (lh_lb
, lh_ub
)
2693 && (real_isinf (&rh_lb
) || real_isinf (&rh_ub
)))
2694 || (contains_zero_p (rh_lb
, rh_ub
)
2695 && (real_isinf (&lh_lb
) || real_isinf (&lh_ub
))))
2699 int signbit_known
= signbit_known_p (lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2701 // If one of the ranges that includes INF is singleton
2702 // and the other range includes zero, the resulting
2703 // range is INF and NAN, because the 0 * INF boundary
2704 // case will be NAN, but already nextafter (0, 1) * INF
2706 if (singleton_inf_p (lh_lb
, lh_ub
)
2707 || singleton_inf_p (rh_lb
, rh_ub
))
2708 return inf_range (lb
, ub
, signbit_known
);
2710 // If one of the multiplicands must be zero, the resulting
2711 // range is +-0 and NAN.
2712 if (zero_p (lh_lb
, lh_ub
) || zero_p (rh_lb
, rh_ub
))
2713 return zero_range (lb
, ub
, signbit_known
);
2715 // Otherwise one of the multiplicands could be
2716 // [0.0, nextafter (0.0, 1.0)] and the [DBL_MAX, INF]
2717 // or similarly with different signs. 0.0 * DBL_MAX
2718 // is still 0.0, nextafter (0.0, 1.0) * INF is still INF,
2719 // so if the signs are always the same or always different,
2720 // result is [+0.0, +INF] or [-INF, -0.0], otherwise VARYING.
2721 return zero_to_inf_range (lb
, ub
, signbit_known
);
2725 REAL_VALUE_TYPE cp
[8];
2726 // Do a cross-product. At this point none of the multiplications
2727 // should produce a NAN.
2728 frange_arithmetic (MULT_EXPR
, type
, cp
[0], lh_lb
, rh_lb
, dconstninf
);
2729 frange_arithmetic (MULT_EXPR
, type
, cp
[4], lh_lb
, rh_lb
, dconstinf
);
2732 // For x * x we can just do max (lh_lb * lh_lb, lh_ub * lh_ub)
2733 // as maximum and -0.0 as minimum if 0.0 is in the range,
2734 // otherwise min (lh_lb * lh_lb, lh_ub * lh_ub).
2735 // -0.0 rather than 0.0 because VREL_EQ doesn't prove that
2736 // x and y are bitwise equal, just that they compare equal.
2737 if (contains_zero_p (lh_lb
, lh_ub
))
2739 if (real_isneg (&lh_lb
) == real_isneg (&lh_ub
))
2752 frange_arithmetic (MULT_EXPR
, type
, cp
[1], lh_lb
, rh_ub
, dconstninf
);
2753 frange_arithmetic (MULT_EXPR
, type
, cp
[5], lh_lb
, rh_ub
, dconstinf
);
2754 frange_arithmetic (MULT_EXPR
, type
, cp
[2], lh_ub
, rh_lb
, dconstninf
);
2755 frange_arithmetic (MULT_EXPR
, type
, cp
[6], lh_ub
, rh_lb
, dconstinf
);
2757 frange_arithmetic (MULT_EXPR
, type
, cp
[3], lh_ub
, rh_ub
, dconstninf
);
2758 frange_arithmetic (MULT_EXPR
, type
, cp
[7], lh_ub
, rh_ub
, dconstinf
);
2760 find_range (lb
, ub
, cp
);
2764 class foperator_div
: public range_operator
2766 using range_operator::op1_range
;
2767 using range_operator::op2_range
;
2769 virtual bool op1_range (frange
&r
, tree type
,
2772 relation_trio
= TRIO_VARYING
) const final override
2774 if (lhs
.undefined_p ())
2776 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2777 bool ret
= range_op_handler (MULT_EXPR
).fold_range (r
, type
, wlhs
, op2
);
2780 if (wlhs
.known_isnan () || op2
.known_isnan () || op2
.undefined_p ())
2781 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2782 const REAL_VALUE_TYPE
&lhs_lb
= wlhs
.lower_bound ();
2783 const REAL_VALUE_TYPE
&lhs_ub
= wlhs
.upper_bound ();
2784 const REAL_VALUE_TYPE
&op2_lb
= op2
.lower_bound ();
2785 const REAL_VALUE_TYPE
&op2_ub
= op2
.upper_bound ();
2786 if ((contains_zero_p (lhs_lb
, lhs_ub
)
2787 && (real_isinf (&op2_lb
) || real_isinf (&op2_ub
)))
2788 || ((contains_zero_p (op2_lb
, op2_ub
))
2789 && (real_isinf (&lhs_lb
) || real_isinf (&lhs_ub
))))
2791 // If both lhs could be zero and op2 infinity or vice versa,
2792 // we don't know anything about op1 except maybe for the sign
2793 // and perhaps if it can be NAN or not.
2794 REAL_VALUE_TYPE lb
, ub
;
2795 int signbit_known
= signbit_known_p (lhs_lb
, lhs_ub
, op2_lb
, op2_ub
);
2796 zero_to_inf_range (lb
, ub
, signbit_known
);
2797 r
.set (type
, lb
, ub
);
2799 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2801 virtual bool op2_range (frange
&r
, tree type
,
2804 relation_trio
= TRIO_VARYING
) const final override
2806 if (lhs
.undefined_p ())
2808 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2809 bool ret
= fold_range (r
, type
, op1
, wlhs
);
2812 if (wlhs
.known_isnan () || op1
.known_isnan () || op1
.undefined_p ())
2813 return float_binary_op_range_finish (ret
, r
, type
, wlhs
, true);
2814 const REAL_VALUE_TYPE
&lhs_lb
= wlhs
.lower_bound ();
2815 const REAL_VALUE_TYPE
&lhs_ub
= wlhs
.upper_bound ();
2816 const REAL_VALUE_TYPE
&op1_lb
= op1
.lower_bound ();
2817 const REAL_VALUE_TYPE
&op1_ub
= op1
.upper_bound ();
2818 if ((contains_zero_p (lhs_lb
, lhs_ub
) && contains_zero_p (op1_lb
, op1_ub
))
2819 || ((real_isinf (&lhs_lb
) || real_isinf (&lhs_ub
))
2820 && (real_isinf (&op1_lb
) || real_isinf (&op1_ub
))))
2822 // If both lhs and op1 could be zeros or both could be infinities,
2823 // we don't know anything about op2 except maybe for the sign
2824 // and perhaps if it can be NAN or not.
2825 REAL_VALUE_TYPE lb
, ub
;
2826 int signbit_known
= signbit_known_p (lhs_lb
, lhs_ub
, op1_lb
, op1_ub
);
2827 zero_to_inf_range (lb
, ub
, signbit_known
);
2828 r
.set (type
, lb
, ub
);
2830 return float_binary_op_range_finish (ret
, r
, type
, wlhs
, true);
2833 void rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, bool &maybe_nan
,
2835 const REAL_VALUE_TYPE
&lh_lb
,
2836 const REAL_VALUE_TYPE
&lh_ub
,
2837 const REAL_VALUE_TYPE
&rh_lb
,
2838 const REAL_VALUE_TYPE
&rh_ub
,
2839 relation_kind
) const final override
2841 // +-0.0 / +-0.0 or +-INF / +-INF is a known NAN.
2842 if ((zero_p (lh_lb
, lh_ub
) && zero_p (rh_lb
, rh_ub
))
2843 || (singleton_inf_p (lh_lb
, lh_ub
) && singleton_inf_p (rh_lb
, rh_ub
)))
2845 real_nan (&lb
, "", 0, TYPE_MODE (type
));
2851 // If +-0.0 is in both ranges, it is a maybe NAN.
2852 if (contains_zero_p (lh_lb
, lh_ub
) && contains_zero_p (rh_lb
, rh_ub
))
2854 // If +-INF is in both ranges, it is a maybe NAN.
2855 else if ((real_isinf (&lh_lb
) || real_isinf (&lh_ub
))
2856 && (real_isinf (&rh_lb
) || real_isinf (&rh_ub
)))
2861 int signbit_known
= signbit_known_p (lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2863 // If dividend must be zero, the range is just +-0
2864 // (including if the divisor is +-INF).
2865 // If divisor must be +-INF, the range is just +-0
2866 // (including if the dividend is zero).
2867 if (zero_p (lh_lb
, lh_ub
) || singleton_inf_p (rh_lb
, rh_ub
))
2868 return zero_range (lb
, ub
, signbit_known
);
2870 // If divisor must be zero, the range is just +-INF
2871 // (including if the dividend is +-INF).
2872 // If dividend must be +-INF, the range is just +-INF
2873 // (including if the dividend is zero).
2874 if (zero_p (rh_lb
, rh_ub
) || singleton_inf_p (lh_lb
, lh_ub
))
2875 return inf_range (lb
, ub
, signbit_known
);
2877 // Otherwise if both operands may be zero, divisor could be
2878 // nextafter(0.0, +-1.0) and dividend +-0.0
2879 // in which case result is going to INF or vice versa and
2880 // result +0.0. So, all we can say for that case is if the
2881 // signs of divisor and dividend are always the same we have
2882 // [+0.0, +INF], if they are always different we have
2883 // [-INF, -0.0]. If they vary, VARYING.
2884 // If both may be +-INF, divisor could be INF and dividend FLT_MAX,
2885 // in which case result is going to INF or vice versa and
2886 // result +0.0. So, all we can say for that case is if the
2887 // signs of divisor and dividend are always the same we have
2888 // [+0.0, +INF], if they are always different we have
2889 // [-INF, -0.0]. If they vary, VARYING.
2891 return zero_to_inf_range (lb
, ub
, signbit_known
);
2893 REAL_VALUE_TYPE cp
[8];
2894 // Do a cross-division. At this point none of the divisions should
2896 frange_arithmetic (RDIV_EXPR
, type
, cp
[0], lh_lb
, rh_lb
, dconstninf
);
2897 frange_arithmetic (RDIV_EXPR
, type
, cp
[1], lh_lb
, rh_ub
, dconstninf
);
2898 frange_arithmetic (RDIV_EXPR
, type
, cp
[2], lh_ub
, rh_lb
, dconstninf
);
2899 frange_arithmetic (RDIV_EXPR
, type
, cp
[3], lh_ub
, rh_ub
, dconstninf
);
2900 frange_arithmetic (RDIV_EXPR
, type
, cp
[4], lh_lb
, rh_lb
, dconstinf
);
2901 frange_arithmetic (RDIV_EXPR
, type
, cp
[5], lh_lb
, rh_ub
, dconstinf
);
2902 frange_arithmetic (RDIV_EXPR
, type
, cp
[6], lh_ub
, rh_lb
, dconstinf
);
2903 frange_arithmetic (RDIV_EXPR
, type
, cp
[7], lh_ub
, rh_ub
, dconstinf
);
2905 find_range (lb
, ub
, cp
);
2907 // If divisor may be zero (but is not known to be only zero),
2908 // and dividend can't be zero, the range can go up to -INF or +INF
2909 // depending on the signs.
2910 if (contains_zero_p (rh_lb
, rh_ub
))
2912 if (signbit_known
<= 0)
2913 real_inf (&lb
, true);
2914 if (signbit_known
>= 0)
2915 real_inf (&ub
, false);
2921 // Initialize any float operators to the primary table
2924 range_op_table::initialize_float_ops ()
2926 set (UNLE_EXPR
, fop_unordered_le
);
2927 set (UNLT_EXPR
, fop_unordered_lt
);
2928 set (UNGE_EXPR
, fop_unordered_ge
);
2929 set (UNGT_EXPR
, fop_unordered_gt
);
2930 set (UNEQ_EXPR
, fop_unordered_equal
);
2931 set (ORDERED_EXPR
, fop_ordered
);
2932 set (UNORDERED_EXPR
, fop_unordered
);
2933 set (LTGT_EXPR
, fop_ltgt
);
2934 set (RDIV_EXPR
, fop_div
);
2938 #include "selftest.h"
2943 // Build an frange from string endpoints.
2945 static inline frange
2946 frange_float (const char *lb
, const char *ub
, tree type
= float_type_node
)
2948 REAL_VALUE_TYPE min
, max
;
2949 gcc_assert (real_from_string (&min
, lb
) == 0);
2950 gcc_assert (real_from_string (&max
, ub
) == 0);
2951 return frange (type
, min
, max
);
2955 range_op_float_tests ()
2958 frange
trange (float_type_node
);
2960 // negate([-5, +10]) => [-10, 5]
2961 r0
= frange_float ("-5", "10");
2962 range_op_handler (NEGATE_EXPR
).fold_range (r
, float_type_node
, r0
, trange
);
2963 ASSERT_EQ (r
, frange_float ("-10", "5"));
2965 // negate([0, 1] -NAN) => [-1, -0] +NAN
2966 r0
= frange_float ("0", "1");
2967 r0
.update_nan (true);
2968 range_op_handler (NEGATE_EXPR
).fold_range (r
, float_type_node
, r0
, trange
);
2969 r1
= frange_float ("-1", "-0");
2970 r1
.update_nan (false);
2973 // [-INF,+INF] + [-INF,+INF] could be a NAN.
2974 range_op_handler
plus (PLUS_EXPR
);
2975 r0
.set_varying (float_type_node
);
2976 r1
.set_varying (float_type_node
);
2979 plus
.fold_range (r
, float_type_node
, r0
, r1
);
2980 if (HONOR_NANS (float_type_node
))
2981 ASSERT_TRUE (r
.maybe_isnan ());
2984 } // namespace selftest
2986 #endif // CHECKING_P