Line data Source code
1 : /* Floating point range operators.
2 : Copyright (C) 2022-2026 Free Software Foundation, Inc.
3 : Contributed by Aldy Hernandez <aldyh@redhat.com>.
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 5650159 : range_operator::fold_range (frange &r, tree type,
54 : const frange &op1, const frange &op2,
55 : relation_trio trio) const
56 : {
57 5650159 : if (empty_range_varying (r, type, op1, op2))
58 6771 : return true;
59 5643388 : if (op1.known_isnan () || op2.known_isnan ())
60 : {
61 6766 : r.set_nan (type);
62 6766 : return true;
63 : }
64 :
65 5636622 : relation_kind rel = trio.op1_op2 ();
66 5636622 : r.set_undefined ();
67 5636622 : frange tmp;
68 11231996 : if (relation_equiv_p (rel) && op1 == op2)
69 : {
70 : // The operands are known equal (x op x). Fold each sub-range against
71 : // itself, the diagonal of the cross product, so a relation-aware rv_fold
72 : // like operator_mult's is_square stays valid.
73 : //
74 : // For x * x with x in [-3, -2] U [2, 3] the diagonal is
75 : //
76 : // [-3, -2] * [-3, -2] = [4, 9]
77 : // [ 2, 3] * [ 2, 3] = [4, 9]
78 : //
79 : // giving the exact [4, 9]. The full cross product would form:
80 : //
81 : // [-3, -2] * [-3, -2] = [4, 9]
82 : // [-3, -2] * [ 2, 3] = [-9, -4]
83 : // [ 2, 3] * [-3, -2] = [-9, -4]
84 : // [ 2, 3] * [ 2, 3] = [4, 9]
85 : //
86 : // whose union [-9, -4] U [4, 9] contains negative products that x * x
87 : // can never produce: the off-diagonal terms pair a value from one
88 : // sub-range with a value from the other, which x (a single value) can
89 : // never do.
90 83176 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
91 : {
92 41928 : rv_fold (tmp, type,
93 : op1.lower_bound (i), op1.upper_bound (i),
94 : op1.lower_bound (i), op1.upper_bound (i), rel);
95 41928 : r.union_ (tmp);
96 : }
97 : }
98 : else
99 : // Otherwise do the straight cross product.
100 11306737 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
101 11537031 : for (unsigned j = 0; j < op2.num_pairs (); ++j)
102 : {
103 5825668 : rv_fold (tmp, type,
104 : op1.lower_bound (i), op1.upper_bound (i),
105 : op2.lower_bound (j), op2.upper_bound (j), rel);
106 5825668 : r.union_ (tmp);
107 : }
108 :
109 5636622 : if (r.known_isnan ())
110 : return true;
111 9367800 : if (op1.maybe_isnan () || op2.maybe_isnan ())
112 3964111 : r.update_nan ();
113 :
114 : // If the result has overflowed and flag_trapping_math, folding this
115 : // operation could elide an overflow or division by zero exception.
116 : // Avoid returning a singleton +-INF, to keep the propagators (DOM
117 : // and substitute_and_fold_engine) from folding. See PR107608.
118 5635522 : if (flag_trapping_math
119 26520804 : && MODE_HAS_INFINITIES (TYPE_MODE (type))
120 10857245 : && r.known_isinf () && !op1.known_isinf () && !op2.known_isinf ())
121 : {
122 1610 : REAL_VALUE_TYPE inf = r.lower_bound ();
123 1610 : if (real_isneg (&inf))
124 : {
125 96 : REAL_VALUE_TYPE min = real_min_representable (type);
126 96 : r.set (type, inf, min);
127 : }
128 : else
129 : {
130 1514 : REAL_VALUE_TYPE max = real_max_representable (type);
131 1514 : r.set (type, max, inf);
132 : }
133 : }
134 :
135 5635522 : r.flush_denormals_to_zero ();
136 :
137 5635522 : return true;
138 : }
139 :
140 : // For a given operation, fold two sets of ranges into [lb, ub].
141 : // MAYBE_NAN is set to TRUE if, in addition to any result in LB or
142 : // UB, the final range has the possibility of a NAN.
143 : void
144 515133 : range_operator::rv_fold (frange &r, tree type,
145 : const REAL_VALUE_TYPE &,
146 : const REAL_VALUE_TYPE &,
147 : const REAL_VALUE_TYPE &,
148 : const REAL_VALUE_TYPE &, relation_kind) const
149 : {
150 515133 : r.set (type, dconstninf, dconstinf, nan_state (true));
151 515133 : }
152 :
153 : bool
154 100053 : range_operator::fold_range (irange &r ATTRIBUTE_UNUSED,
155 : tree type ATTRIBUTE_UNUSED,
156 : const frange &lh ATTRIBUTE_UNUSED,
157 : const irange &rh ATTRIBUTE_UNUSED,
158 : relation_trio) const
159 : {
160 100053 : return false;
161 : }
162 :
163 : bool
164 0 : range_operator::fold_range (irange &r ATTRIBUTE_UNUSED,
165 : tree type ATTRIBUTE_UNUSED,
166 : const frange &lh ATTRIBUTE_UNUSED,
167 : const frange &rh ATTRIBUTE_UNUSED,
168 : relation_trio) const
169 : {
170 0 : return false;
171 : }
172 :
173 : bool
174 0 : range_operator::fold_range (frange &r ATTRIBUTE_UNUSED,
175 : tree type ATTRIBUTE_UNUSED,
176 : const irange &lh ATTRIBUTE_UNUSED,
177 : const irange &rh ATTRIBUTE_UNUSED,
178 : relation_trio) const
179 : {
180 0 : return false;
181 : }
182 :
183 : bool
184 5319 : range_operator::op1_range (frange &r ATTRIBUTE_UNUSED,
185 : tree type ATTRIBUTE_UNUSED,
186 : const frange &lhs ATTRIBUTE_UNUSED,
187 : const frange &op2 ATTRIBUTE_UNUSED,
188 : relation_trio) const
189 : {
190 5319 : return false;
191 : }
192 :
193 : bool
194 5619 : range_operator::op1_range (frange &r ATTRIBUTE_UNUSED,
195 : tree type ATTRIBUTE_UNUSED,
196 : const irange &lhs ATTRIBUTE_UNUSED,
197 : const frange &op2 ATTRIBUTE_UNUSED,
198 : relation_trio) const
199 : {
200 5619 : return false;
201 : }
202 :
203 : bool
204 2431 : range_operator::op2_range (frange &r ATTRIBUTE_UNUSED,
205 : tree type ATTRIBUTE_UNUSED,
206 : const frange &lhs ATTRIBUTE_UNUSED,
207 : const frange &op1 ATTRIBUTE_UNUSED,
208 : relation_trio) const
209 : {
210 2431 : return false;
211 : }
212 :
213 : bool
214 0 : range_operator::op2_range (frange &r ATTRIBUTE_UNUSED,
215 : tree type ATTRIBUTE_UNUSED,
216 : const irange &lhs ATTRIBUTE_UNUSED,
217 : const frange &op1 ATTRIBUTE_UNUSED,
218 : relation_trio) const
219 : {
220 0 : return false;
221 : }
222 :
223 : relation_kind
224 5614463 : range_operator::lhs_op1_relation (const frange &lhs ATTRIBUTE_UNUSED,
225 : const frange &op1 ATTRIBUTE_UNUSED,
226 : const frange &op2 ATTRIBUTE_UNUSED,
227 : relation_kind) const
228 : {
229 5614463 : return VREL_VARYING;
230 : }
231 :
232 : relation_kind
233 1072742 : range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
234 : const frange &op1 ATTRIBUTE_UNUSED,
235 : const frange &op2 ATTRIBUTE_UNUSED,
236 : relation_kind) const
237 : {
238 1072742 : return VREL_VARYING;
239 : }
240 :
241 : relation_kind
242 182860 : range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
243 : const frange &op1 ATTRIBUTE_UNUSED,
244 : const frange &op2 ATTRIBUTE_UNUSED,
245 : relation_kind) const
246 : {
247 182860 : return VREL_VARYING;
248 : }
249 :
250 : relation_kind
251 3342952 : range_operator::lhs_op2_relation (const frange &lhs ATTRIBUTE_UNUSED,
252 : const frange &op1 ATTRIBUTE_UNUSED,
253 : const frange &op2 ATTRIBUTE_UNUSED,
254 : relation_kind) const
255 : {
256 3342952 : return VREL_VARYING;
257 : }
258 :
259 : relation_kind
260 379096 : range_operator::op1_op2_relation (const irange &,
261 : const frange &,
262 : const frange &) const
263 : {
264 379096 : return VREL_VARYING;
265 : }
266 :
267 :
268 : relation_kind
269 722258 : range_operator::op1_op2_relation (const frange &,
270 : const frange &,
271 : const frange &) const
272 : {
273 722258 : return VREL_VARYING;
274 : }
275 :
276 : // Return TRUE if OP1 and OP2 may be a NAN.
277 :
278 : static inline bool
279 3696619 : maybe_isnan (const frange &op1, const frange &op2)
280 : {
281 6465682 : return op1.maybe_isnan () || op2.maybe_isnan ();
282 : }
283 :
284 : // Floating point version of relop_early_resolve that takes NANs into
285 : // account.
286 : //
287 : // For relation opcodes, first try to see if the supplied relation
288 : // forces a true or false result, and return that.
289 : // Then check for undefined operands. If none of this applies,
290 : // return false.
291 : //
292 : // TRIO are the relations between operands as they appear in the IL.
293 : // MY_REL is the relation that corresponds to the operator being
294 : // folded. For example, when attempting to fold x_3 == y_5, MY_REL is
295 : // VREL_EQ, and if the statement is dominated by x_3 > y_5, then
296 : // TRIO.op1_op2() is VREL_GT.
297 :
298 : static inline bool
299 2145493 : frelop_early_resolve (irange &r, tree type,
300 : const frange &op1, const frange &op2,
301 : relation_trio trio, relation_kind my_rel)
302 : {
303 2145493 : relation_kind rel = trio.op1_op2 ();
304 :
305 : // If known relation is a complete subset of this relation, always
306 : // return true. However, avoid doing this when NAN is a possibility
307 : // as we'll incorrectly fold conditions:
308 : //
309 : // if (x_3 >= y_5)
310 : // ;
311 : // else
312 : // ;; With NANs the relation here is basically VREL_UNLT, so we
313 : // ;; can't fold the following:
314 : // if (x_3 < y_5)
315 2145493 : if (!maybe_isnan (op1, op2) && relation_union (rel, my_rel) == my_rel)
316 : {
317 1895 : r = range_true (type);
318 1895 : return true;
319 : }
320 :
321 : // If known relation has no subset of this relation, always false.
322 2143598 : if (relation_intersect (rel, my_rel) == VREL_UNDEFINED)
323 : {
324 999 : r = range_false (type);
325 999 : return true;
326 : }
327 :
328 : // If either operand is undefined, return VARYING.
329 2142599 : if (empty_range_varying (r, type, op1, op2))
330 3844 : return true;
331 :
332 : return false;
333 : }
334 :
335 : // Set VALUE to its next real value, or INF if the operation overflows.
336 :
337 : void
338 14301675 : frange_nextafter (enum machine_mode mode,
339 : REAL_VALUE_TYPE &value,
340 : const REAL_VALUE_TYPE &inf)
341 : {
342 100111725 : if (MODE_COMPOSITE_P (mode)
343 0 : && (real_isdenormal (&value, mode) || real_iszero (&value)))
344 : {
345 : // IBM extended denormals only have DFmode precision.
346 0 : REAL_VALUE_TYPE tmp, tmp2;
347 0 : real_convert (&tmp2, DFmode, &value);
348 0 : real_nextafter (&tmp, REAL_MODE_FORMAT (DFmode), &tmp2, &inf);
349 0 : real_convert (&value, mode, &tmp);
350 : }
351 : else
352 : {
353 14301675 : REAL_VALUE_TYPE tmp;
354 14301675 : real_nextafter (&tmp, REAL_MODE_FORMAT (mode), &value, &inf);
355 14301675 : value = tmp;
356 : }
357 14301675 : }
358 :
359 : // Like real_arithmetic, but round the result to INF if the operation
360 : // produced inexact results.
361 : //
362 : // ?? There is still one problematic case, i387. With
363 : // -fexcess-precision=standard we perform most SF/DFmode arithmetic in
364 : // XFmode (long_double_type_node), so that case is OK. But without
365 : // -mfpmath=sse, all the SF/DFmode computations are in XFmode
366 : // precision (64-bit mantissa) and only occasionally rounded to
367 : // SF/DFmode (when storing into memory from the 387 stack). Maybe
368 : // this is ok as well though it is just occasionally more precise. ??
369 :
370 : void
371 16650948 : frange_arithmetic (enum tree_code code, tree type,
372 : REAL_VALUE_TYPE &result,
373 : const REAL_VALUE_TYPE &op1,
374 : const REAL_VALUE_TYPE &op2,
375 : const REAL_VALUE_TYPE &inf)
376 : {
377 16650948 : REAL_VALUE_TYPE value;
378 16650948 : enum machine_mode mode = TYPE_MODE (type);
379 116556636 : bool mode_composite = MODE_COMPOSITE_P (mode);
380 :
381 16650948 : bool inexact = real_arithmetic (&value, code, &op1, &op2);
382 16650948 : real_convert (&result, mode, &value);
383 :
384 : /* When rounding towards negative infinity, x + (-x) and
385 : x - x is -0 rather than +0 real_arithmetic computes.
386 : So, when we are looking for lower bound (inf is negative),
387 : use -0 rather than +0. */
388 16650948 : if (flag_rounding_math
389 17208 : && (code == PLUS_EXPR || code == MINUS_EXPR)
390 5188 : && !inexact
391 5188 : && real_iszero (&result)
392 156 : && !real_isneg (&result)
393 16651104 : && real_isneg (&inf))
394 : {
395 94 : REAL_VALUE_TYPE op2a = op2;
396 94 : if (code == PLUS_EXPR)
397 82 : op2a.sign ^= 1;
398 94 : if (real_isneg (&op1) == real_isneg (&op2a) && real_equal (&op1, &op2a))
399 42 : result.sign = 1;
400 : }
401 :
402 : // Be extra careful if there may be discrepancies between the
403 : // compile and runtime results.
404 16650948 : bool round = false;
405 16650948 : if (mode_composite)
406 : round = true;
407 : else
408 : {
409 16650948 : bool low = real_isneg (&inf);
410 16650948 : round = (low ? !real_less (&result, &value)
411 8325542 : : !real_less (&value, &result));
412 16650948 : if (real_isinf (&result, !low)
413 1784910 : && !real_isinf (&value)
414 17086305 : && !flag_rounding_math)
415 : {
416 : // Use just [+INF, +INF] rather than [MAX, +INF]
417 : // even if value is larger than MAX and rounds to
418 : // nearest to +INF. Similarly just [-INF, -INF]
419 : // rather than [-INF, +MAX] even if value is smaller
420 : // than -MAX and rounds to nearest to -INF.
421 : // Unless INEXACT is true, in that case we need some
422 : // extra buffer.
423 433456 : if (!inexact)
424 : round = false;
425 : else
426 : {
427 57394 : REAL_VALUE_TYPE tmp = result, tmp2;
428 57394 : frange_nextafter (mode, tmp, inf);
429 : // TMP is at this point the maximum representable
430 : // number.
431 57394 : real_arithmetic (&tmp2, MINUS_EXPR, &value, &tmp);
432 57394 : if (real_isneg (&tmp2) != low
433 57394 : && (REAL_EXP (&tmp2) - REAL_EXP (&tmp)
434 57394 : >= 2 - REAL_MODE_FORMAT (mode)->p))
435 57393 : round = false;
436 : }
437 : }
438 : }
439 16274886 : if (round && (inexact || !real_identical (&result, &value)))
440 : {
441 1593962 : if (mode_composite
442 1593962 : && (real_isdenormal (&result, mode) || real_iszero (&result)))
443 : {
444 : // IBM extended denormals only have DFmode precision.
445 0 : REAL_VALUE_TYPE tmp, tmp2;
446 0 : real_convert (&tmp2, DFmode, &value);
447 0 : real_nextafter (&tmp, REAL_MODE_FORMAT (DFmode), &tmp2, &inf);
448 0 : real_convert (&result, mode, &tmp);
449 : }
450 : else
451 1593962 : frange_nextafter (mode, result, inf);
452 : }
453 16650948 : if (mode_composite)
454 0 : switch (code)
455 : {
456 0 : case PLUS_EXPR:
457 0 : case MINUS_EXPR:
458 : // ibm-ldouble-format documents 1ulp for + and -.
459 0 : frange_nextafter (mode, result, inf);
460 0 : break;
461 0 : case MULT_EXPR:
462 : // ibm-ldouble-format documents 2ulps for *.
463 0 : frange_nextafter (mode, result, inf);
464 0 : frange_nextafter (mode, result, inf);
465 0 : break;
466 0 : case RDIV_EXPR:
467 : // ibm-ldouble-format documents 3ulps for /.
468 0 : frange_nextafter (mode, result, inf);
469 0 : frange_nextafter (mode, result, inf);
470 0 : frange_nextafter (mode, result, inf);
471 0 : break;
472 : default:
473 : break;
474 : }
475 16650948 : }
476 :
477 : // Crop R to [-INF, MAX] where MAX is the maximum representable number
478 : // for TYPE.
479 :
480 : static inline void
481 196398 : frange_drop_inf (frange &r, tree type)
482 : {
483 196398 : REAL_VALUE_TYPE max = real_max_representable (type);
484 196398 : frange tmp (type, r.lower_bound (), max);
485 196398 : r.intersect (tmp);
486 196398 : }
487 :
488 : // Crop R to [MIN, +INF] where MIN is the minimum representable number
489 : // for TYPE.
490 :
491 : static inline void
492 101526 : frange_drop_ninf (frange &r, tree type)
493 : {
494 101526 : REAL_VALUE_TYPE min = real_min_representable (type);
495 101526 : frange tmp (type, min, r.upper_bound ());
496 101526 : r.intersect (tmp);
497 101526 : }
498 :
499 : // Crop R to [MIN, MAX] where MAX is the maximum representable number
500 : // for TYPE and MIN the minimum representable number for TYPE.
501 :
502 : static inline void
503 563896 : frange_drop_infs (frange &r, tree type)
504 : {
505 563896 : REAL_VALUE_TYPE max = real_max_representable (type);
506 563896 : REAL_VALUE_TYPE min = real_min_representable (type);
507 563896 : frange tmp (type, min, max);
508 563896 : r.intersect (tmp);
509 563896 : }
510 :
511 : // If zero is in R, make sure both -0.0 and +0.0 are in the range.
512 :
513 : static inline void
514 1008411 : frange_add_zeros (frange &r, tree type)
515 : {
516 1008411 : if (r.undefined_p () || r.known_isnan ())
517 : return;
518 :
519 1008411 : if (HONOR_SIGNED_ZEROS (type) && r.contains_zero_p ())
520 : {
521 713154 : frange zero;
522 713154 : zero.set_zero (type);
523 713154 : r.union_ (zero);
524 713154 : }
525 : }
526 :
527 : // Build a range that is <= VAL and store it in R. Return TRUE if
528 : // further changes may be needed for R, or FALSE if R is in its final
529 : // form.
530 :
531 : static bool
532 274960 : build_le (frange &r, tree type, const frange &val)
533 : {
534 274960 : gcc_checking_assert (!val.known_isnan ());
535 :
536 274960 : REAL_VALUE_TYPE ninf = frange_val_min (type);
537 274960 : r.set (type, ninf, val.upper_bound ());
538 :
539 : // Add both zeros if there's the possibility of zero equality.
540 274960 : frange_add_zeros (r, type);
541 :
542 274960 : return true;
543 : }
544 :
545 : // Build a range that is < VAL and store it in R. Return TRUE if
546 : // further changes may be needed for R, or FALSE if R is in its final
547 : // form.
548 :
549 : static bool
550 327343 : build_lt (frange &r, tree type, const frange &val)
551 : {
552 327343 : gcc_checking_assert (!val.known_isnan ());
553 :
554 : // < -INF is outside the range.
555 327343 : if (real_isinf (&val.upper_bound (), 1))
556 : {
557 1074 : if (HONOR_NANS (type))
558 1074 : r.set_nan (type);
559 : else
560 0 : r.set_undefined ();
561 : return false;
562 : }
563 :
564 326269 : REAL_VALUE_TYPE ninf = frange_val_min (type);
565 326269 : REAL_VALUE_TYPE prev = val.upper_bound ();
566 326269 : machine_mode mode = TYPE_MODE (type);
567 : // Default to the conservatively correct closed ranges for
568 : // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
569 : // !HONOR_INFINITIES, nextafter will yield -INF, but frange::set()
570 : // will crop the range appropriately.
571 2283883 : if (!MODE_COMPOSITE_P (mode))
572 326269 : frange_nextafter (mode, prev, ninf);
573 326269 : r.set (type, ninf, prev);
574 326269 : return true;
575 : }
576 :
577 : // Build a range that is >= VAL and store it in R. Return TRUE if
578 : // further changes may be needed for R, or FALSE if R is in its final
579 : // form.
580 :
581 : static bool
582 409797 : build_ge (frange &r, tree type, const frange &val)
583 : {
584 409797 : gcc_checking_assert (!val.known_isnan ());
585 :
586 409797 : REAL_VALUE_TYPE inf = frange_val_max (type);
587 409797 : r.set (type, val.lower_bound (), inf);
588 :
589 : // Add both zeros if there's the possibility of zero equality.
590 409797 : frange_add_zeros (r, type);
591 :
592 409797 : return true;
593 : }
594 :
595 : // Build a range that is > VAL and store it in R. Return TRUE if
596 : // further changes may be needed for R, or FALSE if R is in its final
597 : // form.
598 :
599 : static bool
600 210553 : build_gt (frange &r, tree type, const frange &val)
601 : {
602 210553 : gcc_checking_assert (!val.known_isnan ());
603 :
604 : // > +INF is outside the range.
605 210553 : if (real_isinf (&val.lower_bound (), 0))
606 : {
607 1766 : if (HONOR_NANS (type))
608 1766 : r.set_nan (type);
609 : else
610 0 : r.set_undefined ();
611 : return false;
612 : }
613 :
614 208787 : REAL_VALUE_TYPE inf = frange_val_max (type);
615 208787 : REAL_VALUE_TYPE next = val.lower_bound ();
616 208787 : machine_mode mode = TYPE_MODE (type);
617 : // Default to the conservatively correct closed ranges for
618 : // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
619 : // !HONOR_INFINITIES, nextafter will yield +INF, but frange::set()
620 : // will crop the range appropriately.
621 1461509 : if (!MODE_COMPOSITE_P (mode))
622 208787 : frange_nextafter (mode, next, inf);
623 208787 : r.set (type, next, inf);
624 208787 : return true;
625 : }
626 :
627 :
628 : bool
629 74788 : operator_identity::fold_range (frange &r, tree, const frange &op1,
630 : const frange &, relation_trio) const
631 : {
632 74788 : r = op1;
633 74788 : return true;
634 : }
635 :
636 : bool
637 20338 : operator_identity::op1_range (frange &r, tree, const frange &lhs,
638 : const frange &, relation_trio) const
639 : {
640 20338 : r = lhs;
641 20338 : return true;
642 : }
643 :
644 : bool
645 8160 : operator_cst::fold_range (frange &r, tree, const frange &op1,
646 : const frange &, relation_trio) const
647 : {
648 8160 : r = op1;
649 8160 : return true;
650 : }
651 :
652 : bool
653 32324 : operator_equal::op2_range (frange &r, tree type,
654 : const irange &lhs, const frange &op1,
655 : relation_trio rel) const
656 : {
657 32324 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
658 : }
659 :
660 : bool
661 193541 : operator_equal::fold_range (irange &r, tree type,
662 : const frange &op1, const frange &op2,
663 : relation_trio rel) const
664 : {
665 193541 : if (frelop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
666 : return true;
667 :
668 190478 : if (op1.known_isnan () || op2.known_isnan ())
669 478 : r = range_false (type);
670 : // We can be sure the values are always equal or not if both ranges
671 : // consist of a single value, and then compare them.
672 190000 : else if (op1.singleton_p () && op2.singleton_p ())
673 : {
674 1374 : if (op1 == op2)
675 255 : r = range_true (type);
676 : // If one operand is -0.0 and other 0.0, they are still equal.
677 1119 : else if (real_iszero (&op1.lower_bound ())
678 1119 : && real_iszero (&op2.lower_bound ()))
679 29 : r = range_true (type);
680 : else
681 1090 : r = range_false (type);
682 : }
683 188626 : else if (real_iszero (&op1.lower_bound ())
684 6266 : && real_iszero (&op1.upper_bound ())
685 673 : && real_iszero (&op2.lower_bound ())
686 484 : && real_iszero (&op2.upper_bound ())
687 189110 : && !maybe_isnan (op1, op2))
688 : // [-0.0, 0.0] == [-0.0, 0.0] or similar.
689 329 : r = range_true (type);
690 : else
691 : {
692 : // If ranges do not intersect, we know the range is not equal,
693 : // otherwise we don't know anything for sure.
694 188297 : frange tmp = op1;
695 188297 : tmp.intersect (op2);
696 188297 : if (tmp.undefined_p ())
697 : {
698 : // If one range contains -0.0 and another +0.0, we don't know
699 : // anything either, because -0.0 == 0.0.
700 1698 : if (op1.contains_zero_p () && op2.contains_zero_p ())
701 13 : r = range_true_and_false (type);
702 : else
703 1685 : r = range_false (type);
704 : }
705 : else
706 186599 : r = range_true_and_false (type);
707 188297 : }
708 : return true;
709 : }
710 :
711 : bool
712 155486 : operator_equal::op1_range (frange &r, tree type,
713 : const irange &lhs,
714 : const frange &op2,
715 : relation_trio trio) const
716 : {
717 155486 : relation_kind rel = trio.op1_op2 ();
718 155486 : switch (get_bool_state (r, lhs, type))
719 : {
720 53168 : case BRS_TRUE:
721 : // The TRUE side of x == NAN is unreachable.
722 53168 : if (op2.known_isnan ())
723 3 : r.set_undefined ();
724 : else
725 : {
726 : // If it's true, the result is the same as OP2.
727 53165 : r = op2;
728 : // Add both zeros if there's the possibility of zero equality.
729 53165 : frange_add_zeros (r, type);
730 : // The TRUE side of op1 == op2 implies op1 is !NAN.
731 53165 : r.clear_nan ();
732 : }
733 : break;
734 :
735 99892 : case BRS_FALSE:
736 : // The FALSE side of op1 == op1 implies op1 is a NAN.
737 99892 : if (rel == VREL_EQ)
738 2961 : r.set_nan (type);
739 : // On the FALSE side of x == NAN, we know nothing about x.
740 96931 : else if (op2.known_isnan ())
741 5 : r.set_varying (type);
742 : // If the result is false, the only time we know anything is
743 : // if OP2 is a constant.
744 96926 : else if (op2.singleton_p ()
745 105353 : || (!op2.maybe_isnan () && op2.zero_p ()))
746 : {
747 62115 : REAL_VALUE_TYPE tmp = op2.lower_bound ();
748 62115 : r.set (type, tmp, tmp, VR_ANTI_RANGE);
749 : }
750 : else
751 34811 : r.set_varying (type);
752 : break;
753 :
754 : default:
755 : break;
756 : }
757 155486 : return true;
758 : }
759 :
760 : // Check if the LHS range indicates a relation between OP1 and OP2.
761 :
762 : relation_kind
763 155999 : operator_equal::op1_op2_relation (const irange &lhs, const frange &,
764 : const frange &) const
765 : {
766 155999 : if (lhs.undefined_p ())
767 : return VREL_UNDEFINED;
768 :
769 : // FALSE = op1 == op2 indicates NE_EXPR.
770 155999 : if (lhs.zero_p ())
771 : return VREL_NE;
772 :
773 : // TRUE = op1 == op2 indicates EQ_EXPR.
774 86564 : if (!lhs.contains_zero_p ())
775 83742 : return VREL_EQ;
776 : return VREL_VARYING;
777 : }
778 :
779 : bool
780 861075 : operator_not_equal::fold_range (irange &r, tree type,
781 : const frange &op1, const frange &op2,
782 : relation_trio trio) const
783 : {
784 861075 : relation_kind rel = trio.op1_op2 ();
785 :
786 : // VREL_NE & NE_EXPR is always true, even with NANs.
787 861075 : if (rel == VREL_NE)
788 : {
789 26 : r = range_true (type);
790 26 : return true;
791 : }
792 861049 : if (rel == VREL_EQ && maybe_isnan (op1, op2))
793 : {
794 : // Avoid frelop_early_resolve() below as it could fold to FALSE
795 : // without regards to NANs. This would be incorrect if trying
796 : // to fold x_5 != x_5 without prior knowledge of NANs.
797 : // Still, if either operand is undefined, return VARYING.
798 24137 : if (empty_range_varying (r, type, op1, op2))
799 5 : return true;
800 : }
801 836912 : else if (frelop_early_resolve (r, type, op1, op2, trio, VREL_NE))
802 : return true;
803 :
804 : // x != NAN is always TRUE.
805 859638 : if (op1.known_isnan () || op2.known_isnan ())
806 1296 : r = range_true (type);
807 : // We can be sure the values are always equal or not if both ranges
808 : // consist of a single value, and then compare them.
809 858342 : else if (op1.singleton_p () && op2.singleton_p ())
810 : {
811 1594 : if (op1 == op2)
812 1082 : r = range_false (type);
813 : // If one operand is -0.0 and other 0.0, they are still equal.
814 512 : else if (real_iszero (&op1.lower_bound ())
815 512 : && real_iszero (&op2.lower_bound ()))
816 1 : r = range_false (type);
817 : else
818 511 : r = range_true (type);
819 : }
820 856748 : else if (real_iszero (&op1.lower_bound ())
821 7012 : && real_iszero (&op1.upper_bound ())
822 471 : && real_iszero (&op2.lower_bound ())
823 212 : && real_iszero (&op2.upper_bound ())
824 856960 : && !maybe_isnan (op1, op2))
825 : // [-0.0, 0.0] != [-0.0, 0.0] or similar.
826 112 : r = range_false (type);
827 : else
828 : {
829 : // If ranges do not intersect, we know the range is not equal,
830 : // otherwise we don't know anything for sure.
831 856636 : frange tmp = op1;
832 856636 : tmp.intersect (op2);
833 856636 : if (tmp.undefined_p ())
834 : {
835 : // If one range contains -0.0 and another +0.0, we don't know
836 : // anything either, because -0.0 == 0.0.
837 856 : if (op1.contains_zero_p () && op2.contains_zero_p ())
838 2 : r = range_true_and_false (type);
839 : else
840 854 : r = range_true (type);
841 : }
842 : else
843 855780 : r = range_true_and_false (type);
844 856636 : }
845 : return true;
846 : }
847 :
848 : bool
849 325763 : operator_not_equal::op1_range (frange &r, tree type,
850 : const irange &lhs,
851 : const frange &op2,
852 : relation_trio trio) const
853 : {
854 325763 : relation_kind rel = trio.op1_op2 ();
855 325763 : switch (get_bool_state (r, lhs, type))
856 : {
857 52497 : case BRS_TRUE:
858 : // If the result is true, the only time we know anything is if
859 : // OP2 is a constant.
860 52497 : if (op2.singleton_p ())
861 : {
862 : // This is correct even if op1 is NAN, because the following
863 : // range would be ~[tmp, tmp] with the NAN property set to
864 : // maybe (VARYING).
865 23810 : REAL_VALUE_TYPE tmp = op2.lower_bound ();
866 23810 : r.set (type, tmp, tmp, VR_ANTI_RANGE);
867 : }
868 : // The TRUE side of op1 != op1 implies op1 is NAN.
869 28687 : else if (rel == VREL_EQ)
870 5776 : r.set_nan (type);
871 : else
872 22911 : r.set_varying (type);
873 : break;
874 :
875 270658 : case BRS_FALSE:
876 : // The FALSE side of x != NAN is impossible.
877 270658 : if (op2.known_isnan ())
878 182 : r.set_undefined ();
879 : else
880 : {
881 : // If it's false, the result is the same as OP2.
882 270476 : r = op2;
883 : // Add both zeros if there's the possibility of zero equality.
884 270476 : frange_add_zeros (r, type);
885 : // The FALSE side of op1 != op2 implies op1 is !NAN.
886 270476 : r.clear_nan ();
887 : }
888 : break;
889 :
890 : default:
891 : break;
892 : }
893 325763 : return true;
894 : }
895 :
896 : bool
897 209901 : operator_not_equal::op2_range (frange &r, tree type,
898 : const irange &lhs,
899 : const frange &op1,
900 : relation_trio trio) const
901 : {
902 209901 : return op1_range (r, type, lhs, op1, trio);
903 : }
904 :
905 : // Check if the LHS range indicates a relation between OP1 and OP2.
906 :
907 : relation_kind
908 696988 : operator_not_equal::op1_op2_relation (const irange &lhs, const frange &,
909 : const frange &) const
910 : {
911 696988 : if (lhs.undefined_p ())
912 : return VREL_UNDEFINED;
913 :
914 : // FALSE = op1 != op2 indicates EQ_EXPR.
915 696988 : if (lhs.zero_p ())
916 : return VREL_EQ;
917 :
918 : // TRUE = op1 != op2 indicates NE_EXPR.
919 125256 : if (!lhs.contains_zero_p ())
920 122331 : return VREL_NE;
921 : return VREL_VARYING;
922 : }
923 :
924 : bool
925 283628 : operator_lt::fold_range (irange &r, tree type,
926 : const frange &op1, const frange &op2,
927 : relation_trio trio) const
928 : {
929 283628 : if (frelop_early_resolve (r, type, op1, op2, trio, VREL_LT))
930 : return true;
931 :
932 283053 : if (op1.known_isnan ()
933 282579 : || op2.known_isnan ()
934 565370 : || !real_less (&op1.lower_bound (), &op2.upper_bound ()))
935 3137 : r = range_false (type);
936 279916 : else if (!maybe_isnan (op1, op2)
937 279916 : && real_less (&op1.upper_bound (), &op2.lower_bound ()))
938 1417 : r = range_true (type);
939 : else
940 278499 : r = range_true_and_false (type);
941 : return true;
942 : }
943 :
944 : bool
945 466656 : operator_lt::op1_range (frange &r,
946 : tree type,
947 : const irange &lhs,
948 : const frange &op2,
949 : relation_trio) const
950 : {
951 466656 : switch (get_bool_state (r, lhs, type))
952 : {
953 163283 : case BRS_TRUE:
954 : // The TRUE side of x < NAN is unreachable.
955 163283 : if (op2.known_isnan ())
956 68 : r.set_undefined ();
957 163215 : else if (op2.undefined_p ())
958 : return false;
959 163215 : else if (build_lt (r, type, op2))
960 : {
961 163211 : r.clear_nan ();
962 : // x < y implies x is not +INF.
963 163211 : frange_drop_inf (r, type);
964 : }
965 : break;
966 :
967 302464 : case BRS_FALSE:
968 : // On the FALSE side of x < NAN, we know nothing about x.
969 572362 : if (op2.maybe_isnan ())
970 32566 : r.set_varying (type);
971 : else
972 269898 : build_ge (r, type, op2);
973 : break;
974 :
975 : default:
976 : break;
977 : }
978 : return true;
979 : }
980 :
981 : bool
982 72348 : operator_lt::op2_range (frange &r,
983 : tree type,
984 : const irange &lhs,
985 : const frange &op1,
986 : relation_trio) const
987 : {
988 72348 : switch (get_bool_state (r, lhs, type))
989 : {
990 28971 : case BRS_TRUE:
991 : // The TRUE side of NAN < x is unreachable.
992 28971 : if (op1.known_isnan ())
993 22 : r.set_undefined ();
994 28949 : else if (op1.undefined_p ())
995 : return false;
996 28949 : else if (build_gt (r, type, op1))
997 : {
998 28949 : r.clear_nan ();
999 : // x < y implies y is not -INF.
1000 28949 : frange_drop_ninf (r, type);
1001 : }
1002 : break;
1003 :
1004 43167 : case BRS_FALSE:
1005 : // On the FALSE side of NAN < x, we know nothing about x.
1006 47623 : if (op1.maybe_isnan ())
1007 38711 : r.set_varying (type);
1008 : else
1009 4456 : build_le (r, type, op1);
1010 : break;
1011 :
1012 : default:
1013 : break;
1014 : }
1015 : return true;
1016 : }
1017 :
1018 :
1019 : // Check if the LHS range indicates a relation between OP1 and OP2.
1020 :
1021 : relation_kind
1022 279050 : operator_lt::op1_op2_relation (const irange &lhs, const frange &,
1023 : const frange &) const
1024 : {
1025 279050 : if (lhs.undefined_p ())
1026 : return VREL_UNDEFINED;
1027 :
1028 : // FALSE = op1 < op2 indicates GE_EXPR.
1029 279050 : if (lhs.zero_p ())
1030 : return VREL_GE;
1031 :
1032 : // TRUE = op1 < op2 indicates LT_EXPR.
1033 128410 : if (!lhs.contains_zero_p ())
1034 128020 : return VREL_LT;
1035 : return VREL_VARYING;
1036 : }
1037 :
1038 : bool
1039 273117 : operator_le::fold_range (irange &r, tree type,
1040 : const frange &op1, const frange &op2,
1041 : relation_trio rel) const
1042 : {
1043 273117 : if (frelop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1044 : return true;
1045 :
1046 272570 : if (op1.known_isnan ()
1047 271552 : || op2.known_isnan ()
1048 544088 : || !real_compare (LE_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
1049 2933 : r = range_false (type);
1050 269637 : else if (!maybe_isnan (op1, op2)
1051 269637 : && real_compare (LE_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
1052 935 : r = range_true (type);
1053 : else
1054 268702 : r = range_true_and_false (type);
1055 : return true;
1056 : }
1057 :
1058 : bool
1059 103213 : operator_le::op1_range (frange &r,
1060 : tree type,
1061 : const irange &lhs,
1062 : const frange &op2,
1063 : relation_trio) const
1064 : {
1065 103213 : switch (get_bool_state (r, lhs, type))
1066 : {
1067 73300 : case BRS_TRUE:
1068 : // The TRUE side of x <= NAN is unreachable.
1069 73300 : if (op2.known_isnan ())
1070 0 : r.set_undefined ();
1071 73300 : else if (op2.undefined_p ())
1072 : return false;
1073 73300 : else if (build_le (r, type, op2))
1074 73300 : r.clear_nan ();
1075 : break;
1076 :
1077 29712 : case BRS_FALSE:
1078 : // On the FALSE side of x <= NAN, we know nothing about x.
1079 54763 : if (op2.maybe_isnan ())
1080 4661 : r.set_varying (type);
1081 : else
1082 25051 : build_gt (r, type, op2);
1083 : break;
1084 :
1085 : default:
1086 : break;
1087 : }
1088 : return true;
1089 : }
1090 :
1091 : bool
1092 19167 : operator_le::op2_range (frange &r,
1093 : tree type,
1094 : const irange &lhs,
1095 : const frange &op1,
1096 : relation_trio) const
1097 : {
1098 19167 : switch (get_bool_state (r, lhs, type))
1099 : {
1100 7731 : case BRS_TRUE:
1101 : // The TRUE side of NAN <= x is unreachable.
1102 7731 : if (op1.known_isnan ())
1103 0 : r.set_undefined ();
1104 7731 : else if (op1.undefined_p ())
1105 : return false;
1106 7731 : else if (build_ge (r, type, op1))
1107 7731 : r.clear_nan ();
1108 : break;
1109 :
1110 11298 : case BRS_FALSE:
1111 : // On the FALSE side of NAN <= x, we know nothing about x.
1112 12065 : if (op1.maybe_isnan ())
1113 10531 : r.set_varying (type);
1114 767 : else if (op1.undefined_p ())
1115 : return false;
1116 : else
1117 767 : build_lt (r, type, op1);
1118 : break;
1119 :
1120 : default:
1121 : break;
1122 : }
1123 : return true;
1124 : }
1125 :
1126 : // Check if the LHS range indicates a relation between OP1 and OP2.
1127 :
1128 : relation_kind
1129 73398 : operator_le::op1_op2_relation (const irange &lhs, const frange &,
1130 : const frange &) const
1131 : {
1132 73398 : if (lhs.undefined_p ())
1133 : return VREL_UNDEFINED;
1134 :
1135 : // FALSE = op1 <= op2 indicates GT_EXPR.
1136 73398 : if (lhs.zero_p ())
1137 : return VREL_GT;
1138 :
1139 : // TRUE = op1 <= op2 indicates LE_EXPR.
1140 40393 : if (!lhs.contains_zero_p ())
1141 40113 : return VREL_LE;
1142 : return VREL_VARYING;
1143 : }
1144 :
1145 : bool
1146 289368 : operator_gt::fold_range (irange &r, tree type,
1147 : const frange &op1, const frange &op2,
1148 : relation_trio trio) const
1149 : {
1150 289368 : if (frelop_early_resolve (r, type, op1, op2, trio, VREL_GT))
1151 : return true;
1152 :
1153 288578 : if (op1.known_isnan ()
1154 288042 : || op2.known_isnan ()
1155 576298 : || !real_compare (GT_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
1156 4622 : r = range_false (type);
1157 283956 : else if (!maybe_isnan (op1, op2)
1158 283956 : && real_compare (GT_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
1159 1127 : r = range_true (type);
1160 : else
1161 282829 : r = range_true_and_false (type);
1162 : return true;
1163 : }
1164 :
1165 : bool
1166 183665 : operator_gt::op1_range (frange &r,
1167 : tree type,
1168 : const irange &lhs,
1169 : const frange &op2,
1170 : relation_trio) const
1171 : {
1172 183665 : switch (get_bool_state (r, lhs, type))
1173 : {
1174 72605 : case BRS_TRUE:
1175 : // The TRUE side of x > NAN is unreachable.
1176 72605 : if (op2.known_isnan ())
1177 28 : r.set_undefined ();
1178 72577 : else if (op2.undefined_p ())
1179 : return false;
1180 72577 : else if (build_gt (r, type, op2))
1181 : {
1182 72577 : r.clear_nan ();
1183 : // x > y implies x is not -INF.
1184 72577 : frange_drop_ninf (r, type);
1185 : }
1186 : break;
1187 :
1188 110486 : case BRS_FALSE:
1189 : // On the FALSE side of x > NAN, we know nothing about x.
1190 189837 : if (op2.maybe_isnan ())
1191 31135 : r.set_varying (type);
1192 79351 : else if (op2.undefined_p ())
1193 : return false;
1194 : else
1195 79351 : build_le (r, type, op2);
1196 : break;
1197 :
1198 : default:
1199 : break;
1200 : }
1201 : return true;
1202 : }
1203 :
1204 : bool
1205 83491 : operator_gt::op2_range (frange &r,
1206 : tree type,
1207 : const irange &lhs,
1208 : const frange &op1,
1209 : relation_trio) const
1210 : {
1211 83491 : switch (get_bool_state (r, lhs, type))
1212 : {
1213 33209 : case BRS_TRUE:
1214 : // The TRUE side of NAN > x is unreachable.
1215 33209 : if (op1.known_isnan ())
1216 22 : r.set_undefined ();
1217 33187 : else if (op1.undefined_p ())
1218 : return false;
1219 33187 : else if (build_lt (r, type, op1))
1220 : {
1221 33187 : r.clear_nan ();
1222 : // x > y implies y is not +INF.
1223 33187 : frange_drop_inf (r, type);
1224 : }
1225 : break;
1226 :
1227 50050 : case BRS_FALSE:
1228 : // On The FALSE side of NAN > x, we know nothing about x.
1229 54299 : if (op1.maybe_isnan ())
1230 45801 : r.set_varying (type);
1231 4249 : else if (op1.undefined_p ())
1232 : return false;
1233 : else
1234 4249 : build_ge (r, type, op1);
1235 : break;
1236 :
1237 : default:
1238 : break;
1239 : }
1240 : return true;
1241 : }
1242 :
1243 : // Check if the LHS range indicates a relation between OP1 and OP2.
1244 :
1245 : relation_kind
1246 296945 : operator_gt::op1_op2_relation (const irange &lhs, const frange &,
1247 : const frange &) const
1248 : {
1249 296945 : if (lhs.undefined_p ())
1250 : return VREL_UNDEFINED;
1251 :
1252 : // FALSE = op1 > op2 indicates LE_EXPR.
1253 296945 : if (lhs.zero_p ())
1254 : return VREL_LE;
1255 :
1256 : // TRUE = op1 > op2 indicates GT_EXPR.
1257 133342 : if (!lhs.contains_zero_p ())
1258 132842 : return VREL_GT;
1259 : return VREL_VARYING;
1260 : }
1261 :
1262 : bool
1263 268927 : operator_ge::fold_range (irange &r, tree type,
1264 : const frange &op1, const frange &op2,
1265 : relation_trio rel) const
1266 : {
1267 268927 : if (frelop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1268 : return true;
1269 :
1270 268570 : if (op1.known_isnan ()
1271 267768 : || op2.known_isnan ()
1272 536288 : || !real_compare (GE_EXPR, &op1.upper_bound (), &op2.lower_bound ()))
1273 1560 : r = range_false (type);
1274 267010 : else if (!maybe_isnan (op1, op2)
1275 267010 : && real_compare (GE_EXPR, &op1.lower_bound (), &op2.upper_bound ()))
1276 3134 : r = range_true (type);
1277 : else
1278 263876 : r = range_true_and_false (type);
1279 : return true;
1280 : }
1281 :
1282 : bool
1283 116155 : operator_ge::op1_range (frange &r,
1284 : tree type,
1285 : const irange &lhs,
1286 : const frange &op2,
1287 : relation_trio) const
1288 : {
1289 116155 : switch (get_bool_state (r, lhs, type))
1290 : {
1291 66016 : case BRS_TRUE:
1292 : // The TRUE side of x >= NAN is unreachable.
1293 66016 : if (op2.known_isnan ())
1294 0 : r.set_undefined ();
1295 66016 : else if (op2.undefined_p ())
1296 : return false;
1297 66016 : else if (build_ge (r, type, op2))
1298 66016 : r.clear_nan ();
1299 : break;
1300 :
1301 49977 : case BRS_FALSE:
1302 : // On the FALSE side of x >= NAN, we know nothing about x.
1303 92793 : if (op2.maybe_isnan ())
1304 7161 : r.set_varying (type);
1305 42816 : else if (op2.undefined_p ())
1306 : return false;
1307 : else
1308 42816 : build_lt (r, type, op2);
1309 : break;
1310 :
1311 : default:
1312 : break;
1313 : }
1314 : return true;
1315 : }
1316 :
1317 : bool
1318 18050 : operator_ge::op2_range (frange &r, tree type,
1319 : const irange &lhs,
1320 : const frange &op1,
1321 : relation_trio) const
1322 : {
1323 18050 : switch (get_bool_state (r, lhs, type))
1324 : {
1325 9509 : case BRS_TRUE:
1326 : // The TRUE side of NAN >= x is unreachable.
1327 9509 : if (op1.known_isnan ())
1328 0 : r.set_undefined ();
1329 9509 : else if (op1.undefined_p ())
1330 : return false;
1331 9509 : else if (build_le (r, type, op1))
1332 9509 : r.clear_nan ();
1333 : break;
1334 :
1335 8439 : case BRS_FALSE:
1336 : // On the FALSE side of NAN >= x, we know nothing about x.
1337 9220 : if (op1.maybe_isnan ())
1338 7658 : r.set_varying (type);
1339 781 : else if (op1.undefined_p ())
1340 : return false;
1341 : else
1342 781 : build_gt (r, type, op1);
1343 : break;
1344 :
1345 : default:
1346 : break;
1347 : }
1348 : return true;
1349 : }
1350 :
1351 : // Check if the LHS range indicates a relation between OP1 and OP2.
1352 :
1353 : relation_kind
1354 73855 : operator_ge::op1_op2_relation (const irange &lhs, const frange &,
1355 : const frange &) const
1356 : {
1357 73855 : if (lhs.undefined_p ())
1358 : return VREL_UNDEFINED;
1359 :
1360 : // FALSE = op1 >= op2 indicates LT_EXPR.
1361 73855 : if (lhs.zero_p ())
1362 : return VREL_LT;
1363 :
1364 : // TRUE = op1 >= op2 indicates GE_EXPR.
1365 42217 : if (!lhs.contains_zero_p ())
1366 42000 : return VREL_GE;
1367 : return VREL_VARYING;
1368 : }
1369 :
1370 : // UNORDERED_EXPR comparison.
1371 :
1372 : class foperator_unordered : public range_operator
1373 : {
1374 : using range_operator::fold_range;
1375 : using range_operator::op1_range;
1376 : using range_operator::op2_range;
1377 : public:
1378 : bool fold_range (irange &r, tree type,
1379 : const frange &op1, const frange &op2,
1380 : relation_trio = TRIO_VARYING) const final override;
1381 : bool op1_range (frange &r, tree type,
1382 : const irange &lhs, const frange &op2,
1383 : relation_trio = TRIO_VARYING) const final override;
1384 64782 : bool op2_range (frange &r, tree type,
1385 : const irange &lhs, const frange &op1,
1386 : relation_trio rel = TRIO_VARYING) const final override
1387 : {
1388 64782 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1389 : }
1390 : };
1391 : static const foperator_unordered fop_unordered;
1392 :
1393 : bool
1394 60400 : foperator_unordered::fold_range (irange &r, tree type,
1395 : const frange &op1, const frange &op2,
1396 : relation_trio) const
1397 : {
1398 : // UNORDERED is TRUE if either operand is a NAN.
1399 60400 : if (op1.known_isnan () || op2.known_isnan ())
1400 217 : r = range_true (type);
1401 : // UNORDERED is FALSE if neither operand is a NAN.
1402 63171 : else if (!op1.maybe_isnan () && !op2.maybe_isnan ())
1403 1498 : r = range_false (type);
1404 : else
1405 58685 : r = range_true_and_false (type);
1406 60400 : return true;
1407 : }
1408 :
1409 : bool
1410 122720 : foperator_unordered::op1_range (frange &r, tree type,
1411 : const irange &lhs,
1412 : const frange &op2,
1413 : relation_trio trio) const
1414 : {
1415 122720 : relation_kind rel = trio.op1_op2 ();
1416 122720 : switch (get_bool_state (r, lhs, type))
1417 : {
1418 48226 : case BRS_TRUE:
1419 : // Since at least one operand must be NAN, if one of them is
1420 : // not, the other must be.
1421 48226 : if (rel == VREL_EQ || !op2.maybe_isnan ())
1422 14741 : r.set_nan (type);
1423 : else
1424 33485 : r.set_varying (type);
1425 : break;
1426 :
1427 66037 : case BRS_FALSE:
1428 : // A false UNORDERED means both operands are !NAN, so it's
1429 : // impossible for op2 to be a NAN.
1430 66037 : if (op2.known_isnan ())
1431 16 : r.set_undefined ();
1432 : else
1433 : {
1434 66021 : r.set_varying (type);
1435 66021 : r.clear_nan ();
1436 : }
1437 : break;
1438 :
1439 : default:
1440 : break;
1441 : }
1442 122720 : return true;
1443 : }
1444 :
1445 : // ORDERED_EXPR comparison.
1446 :
1447 : class foperator_ordered : public range_operator
1448 : {
1449 : using range_operator::fold_range;
1450 : using range_operator::op1_range;
1451 : using range_operator::op2_range;
1452 : public:
1453 : bool fold_range (irange &r, tree type,
1454 : const frange &op1, const frange &op2,
1455 : relation_trio = TRIO_VARYING) const final override;
1456 : bool op1_range (frange &r, tree type,
1457 : const irange &lhs, const frange &op2,
1458 : relation_trio = TRIO_VARYING) const final override;
1459 31905 : bool op2_range (frange &r, tree type,
1460 : const irange &lhs, const frange &op1,
1461 : relation_trio rel = TRIO_VARYING) const final override
1462 : {
1463 31905 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1464 : }
1465 : };
1466 : static const foperator_ordered fop_ordered;
1467 :
1468 : bool
1469 33505 : foperator_ordered::fold_range (irange &r, tree type,
1470 : const frange &op1, const frange &op2,
1471 : relation_trio) const
1472 : {
1473 33505 : if (op1.known_isnan () || op2.known_isnan ())
1474 360 : r = range_false (type);
1475 36405 : else if (!op1.maybe_isnan () && !op2.maybe_isnan ())
1476 1623 : r = range_true (type);
1477 : else
1478 31522 : r = range_true_and_false (type);
1479 33505 : return true;
1480 : }
1481 :
1482 : bool
1483 73968 : foperator_ordered::op1_range (frange &r, tree type,
1484 : const irange &lhs,
1485 : const frange &op2,
1486 : relation_trio trio) const
1487 : {
1488 73968 : relation_kind rel = trio.op1_op2 ();
1489 73968 : switch (get_bool_state (r, lhs, type))
1490 : {
1491 63455 : case BRS_TRUE:
1492 : // The TRUE side of ORDERED means both operands are !NAN, so
1493 : // it's impossible for op2 to be a NAN.
1494 63455 : if (op2.known_isnan ())
1495 10 : r.set_undefined ();
1496 : else
1497 : {
1498 63445 : r.set_varying (type);
1499 63445 : r.clear_nan ();
1500 : }
1501 : break;
1502 :
1503 5847 : case BRS_FALSE:
1504 : // The FALSE side of op1 ORDERED op1 implies op1 is NAN.
1505 5847 : if (rel == VREL_EQ)
1506 2942 : r.set_nan (type);
1507 : else
1508 2905 : r.set_varying (type);
1509 : break;
1510 :
1511 : default:
1512 : break;
1513 : }
1514 73968 : return true;
1515 : }
1516 :
1517 : bool
1518 244586 : operator_negate::fold_range (frange &r, tree type,
1519 : const frange &op1, const frange &op2,
1520 : relation_trio) const
1521 : {
1522 244586 : if (empty_range_varying (r, type, op1, op2))
1523 406 : return true;
1524 244180 : if (op1.known_isnan ())
1525 : {
1526 457 : bool sign;
1527 457 : if (op1.nan_signbit_p (sign))
1528 38 : r.set_nan (type, !sign);
1529 : else
1530 419 : r.set_nan (type);
1531 : return true;
1532 : }
1533 :
1534 243723 : r.set_undefined ();
1535 732776 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
1536 : {
1537 245330 : REAL_VALUE_TYPE lb = op1.lower_bound (i);
1538 245330 : REAL_VALUE_TYPE ub = op1.upper_bound (i);
1539 245330 : lb = real_value_negate (&lb);
1540 245330 : ub = real_value_negate (&ub);
1541 245330 : frange tmp (type, ub, lb);
1542 245330 : r.union_ (tmp);
1543 245330 : }
1544 438178 : if (op1.maybe_isnan ())
1545 : {
1546 49272 : bool sign;
1547 49272 : if (op1.nan_signbit_p (sign))
1548 2207 : r.update_nan (!sign);
1549 : else
1550 47065 : r.update_nan ();
1551 : }
1552 : else
1553 194451 : r.clear_nan ();
1554 : return true;
1555 : }
1556 :
1557 : bool
1558 3990 : operator_negate::op1_range (frange &r, tree type,
1559 : const frange &lhs, const frange &op2,
1560 : relation_trio rel) const
1561 : {
1562 3990 : return fold_range (r, type, lhs, op2, rel);
1563 : }
1564 :
1565 : bool
1566 597400 : operator_abs::fold_range (frange &r, tree type,
1567 : const frange &op1, const frange &op2,
1568 : relation_trio) const
1569 : {
1570 597400 : if (empty_range_varying (r, type, op1, op2))
1571 130 : return true;
1572 597270 : if (op1.known_isnan ())
1573 : {
1574 738 : r.set_nan (type, /*sign=*/false);
1575 738 : return true;
1576 : }
1577 :
1578 596532 : const REAL_VALUE_TYPE lh_lb = op1.lower_bound ();
1579 : // Handle the easy case where everything is positive.
1580 596532 : if (real_compare (GE_EXPR, &lh_lb, &dconst0)
1581 160418 : && !real_iszero (&lh_lb, /*sign=*/true)
1582 754558 : && !op1.maybe_isnan (/*sign=*/true))
1583 : {
1584 157417 : r = op1;
1585 157417 : return true;
1586 : }
1587 :
1588 : // Take the absolute value of each sub-range and union the results.
1589 439115 : r.set_undefined ();
1590 1326728 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
1591 : {
1592 448498 : const REAL_VALUE_TYPE lb = op1.lower_bound (i);
1593 448498 : const REAL_VALUE_TYPE ub = op1.upper_bound (i);
1594 448498 : REAL_VALUE_TYPE min = real_value_abs (&lb);
1595 448498 : REAL_VALUE_TYPE max = real_value_abs (&ub);
1596 : // If the range contains zero then we know that the minimum value in the
1597 : // range will be zero.
1598 448498 : if (real_compare (LE_EXPR, &lb, &dconst0)
1599 448498 : && real_compare (GE_EXPR, &ub, &dconst0))
1600 : {
1601 430317 : if (real_compare (GT_EXPR, &min, &max))
1602 6714 : max = min;
1603 430317 : min = dconst0;
1604 : }
1605 : else
1606 : {
1607 : // If the range was reversed, swap MIN and MAX.
1608 18181 : if (real_compare (GT_EXPR, &min, &max))
1609 8643 : std::swap (min, max);
1610 : }
1611 448498 : frange tmp (type, min, max);
1612 448498 : r.union_ (tmp);
1613 448498 : }
1614 466391 : if (op1.maybe_isnan ())
1615 413934 : r.update_nan (/*sign=*/false);
1616 : else
1617 25181 : r.clear_nan ();
1618 : return true;
1619 : }
1620 :
1621 : bool
1622 346064 : operator_abs::op1_range (frange &r, tree type,
1623 : const frange &lhs, const frange &op2,
1624 : relation_trio) const
1625 : {
1626 346064 : if (empty_range_varying (r, type, lhs, op2))
1627 0 : return true;
1628 346064 : if (lhs.known_isnan ())
1629 : {
1630 1518 : r.set_nan (type);
1631 1518 : return true;
1632 : }
1633 :
1634 : // Start with the positives because negatives are an impossible result.
1635 344546 : frange positives (type, dconst0, frange_val_max (type));
1636 344546 : positives.update_nan (/*sign=*/false);
1637 344546 : positives.intersect (lhs);
1638 344546 : r = positives;
1639 : // Add -NAN if relevant.
1640 523321 : if (r.maybe_isnan ())
1641 : {
1642 165711 : frange neg_nan;
1643 165711 : neg_nan.set_nan (type, true);
1644 165711 : r.union_ (neg_nan);
1645 165711 : }
1646 344546 : if (r.known_isnan () || r.undefined_p ())
1647 : return true;
1648 : // Then add the negative of each pair:
1649 : // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
1650 344478 : frange negatives (type, real_value_negate (&positives.upper_bound ()),
1651 344478 : real_value_negate (&positives.lower_bound ()));
1652 344478 : negatives.clear_nan ();
1653 344478 : r.union_ (negatives);
1654 344478 : return true;
1655 344478 : }
1656 :
1657 : class foperator_unordered_lt : public range_operator
1658 : {
1659 : using range_operator::fold_range;
1660 : using range_operator::op1_range;
1661 : using range_operator::op2_range;
1662 : public:
1663 13250 : bool fold_range (irange &r, tree type,
1664 : const frange &op1, const frange &op2,
1665 : relation_trio trio = TRIO_VARYING) const final override
1666 : {
1667 13250 : if (op1.known_isnan () || op2.known_isnan ())
1668 : {
1669 33 : r = range_true (type);
1670 33 : return true;
1671 : }
1672 13217 : frange op1_no_nan = op1;
1673 13217 : frange op2_no_nan = op2;
1674 16442 : if (op1.maybe_isnan ())
1675 9975 : op1_no_nan.clear_nan ();
1676 21447 : if (op2.maybe_isnan ())
1677 4987 : op2_no_nan.clear_nan ();
1678 13217 : if (!range_op_handler (LT_EXPR).fold_range (r, type, op1_no_nan,
1679 : op2_no_nan, trio))
1680 : return false;
1681 : // The result is the same as the ordered version when the
1682 : // comparison is true or when the operands cannot be NANs.
1683 13217 : if (!maybe_isnan (op1, op2) || r == range_true (type))
1684 : return true;
1685 : else
1686 : {
1687 10035 : r = range_true_and_false (type);
1688 10035 : return true;
1689 : }
1690 : }
1691 : bool op1_range (frange &r, tree type,
1692 : const irange &lhs,
1693 : const frange &op2,
1694 : relation_trio trio) const final override;
1695 : bool op2_range (frange &r, tree type,
1696 : const irange &lhs,
1697 : const frange &op1,
1698 : relation_trio trio) const final override;
1699 : };
1700 : static const foperator_unordered_lt fop_unordered_lt;
1701 :
1702 : bool
1703 5182 : foperator_unordered_lt::op1_range (frange &r, tree type,
1704 : const irange &lhs,
1705 : const frange &op2,
1706 : relation_trio) const
1707 : {
1708 5182 : switch (get_bool_state (r, lhs, type))
1709 : {
1710 2099 : case BRS_TRUE:
1711 3876 : if (op2.maybe_isnan ())
1712 322 : r.set_varying (type);
1713 1777 : else if (op2.undefined_p ())
1714 : return false;
1715 : else
1716 1777 : build_lt (r, type, op2);
1717 : break;
1718 :
1719 1533 : case BRS_FALSE:
1720 : // A false UNORDERED_LT means both operands are !NAN, so it's
1721 : // impossible for op2 to be a NAN.
1722 1533 : if (op2.known_isnan ())
1723 0 : r.set_undefined ();
1724 1533 : else if (op2.undefined_p ())
1725 : return false;
1726 1533 : else if (build_ge (r, type, op2))
1727 1533 : r.clear_nan ();
1728 : break;
1729 :
1730 : default:
1731 : break;
1732 : }
1733 : return true;
1734 : }
1735 :
1736 : bool
1737 3160 : foperator_unordered_lt::op2_range (frange &r, tree type,
1738 : const irange &lhs,
1739 : const frange &op1,
1740 : relation_trio) const
1741 : {
1742 3160 : switch (get_bool_state (r, lhs, type))
1743 : {
1744 1498 : case BRS_TRUE:
1745 2164 : if (op1.maybe_isnan ())
1746 832 : r.set_varying (type);
1747 666 : else if (op1.undefined_p ())
1748 : return false;
1749 : else
1750 666 : build_gt (r, type, op1);
1751 : break;
1752 :
1753 112 : case BRS_FALSE:
1754 : // A false UNORDERED_LT means both operands are !NAN, so it's
1755 : // impossible for op1 to be a NAN.
1756 112 : if (op1.known_isnan ())
1757 0 : r.set_undefined ();
1758 112 : else if (op1.undefined_p ())
1759 : return false;
1760 112 : else if (build_le (r, type, op1))
1761 112 : r.clear_nan ();
1762 : break;
1763 :
1764 : default:
1765 : break;
1766 : }
1767 : return true;
1768 : }
1769 :
1770 : class foperator_unordered_le : public range_operator
1771 : {
1772 : using range_operator::fold_range;
1773 : using range_operator::op1_range;
1774 : using range_operator::op2_range;
1775 : public:
1776 180450 : bool fold_range (irange &r, tree type,
1777 : const frange &op1, const frange &op2,
1778 : relation_trio trio = TRIO_VARYING) const final override
1779 : {
1780 180450 : if (op1.known_isnan () || op2.known_isnan ())
1781 : {
1782 94 : r = range_true (type);
1783 94 : return true;
1784 : }
1785 180356 : frange op1_no_nan = op1;
1786 180356 : frange op2_no_nan = op2;
1787 205710 : if (op1.maybe_isnan ())
1788 154978 : op1_no_nan.clear_nan ();
1789 353807 : if (op2.maybe_isnan ())
1790 6899 : op2_no_nan.clear_nan ();
1791 180356 : if (!range_op_handler (LE_EXPR).fold_range (r, type, op1_no_nan,
1792 : op2_no_nan, trio))
1793 : return false;
1794 : // The result is the same as the ordered version when the
1795 : // comparison is true or when the operands cannot be NANs.
1796 180356 : if (!maybe_isnan (op1, op2) || r == range_true (type))
1797 : return true;
1798 : else
1799 : {
1800 156290 : r = range_true_and_false (type);
1801 156290 : return true;
1802 : }
1803 : }
1804 : bool op1_range (frange &r, tree type,
1805 : const irange &lhs, const frange &op2,
1806 : relation_trio = TRIO_VARYING) const final override;
1807 : bool op2_range (frange &r, tree type,
1808 : const irange &lhs, const frange &op1,
1809 : relation_trio = TRIO_VARYING) const final override;
1810 : };
1811 : static const foperator_unordered_le fop_unordered_le;
1812 :
1813 : bool
1814 172163 : foperator_unordered_le::op1_range (frange &r, tree type,
1815 : const irange &lhs, const frange &op2,
1816 : relation_trio) const
1817 : {
1818 172163 : switch (get_bool_state (r, lhs, type))
1819 : {
1820 96646 : case BRS_TRUE:
1821 189285 : if (op2.maybe_isnan ())
1822 4007 : r.set_varying (type);
1823 92639 : else if (op2.undefined_p ())
1824 : return false;
1825 : else
1826 92639 : build_le (r, type, op2);
1827 : break;
1828 :
1829 74029 : case BRS_FALSE:
1830 : // A false UNORDERED_LE means both operands are !NAN, so it's
1831 : // impossible for op2 to be a NAN.
1832 74029 : if (op2.known_isnan ())
1833 0 : r.set_undefined ();
1834 74029 : else if (build_gt (r, type, op2))
1835 74027 : r.clear_nan ();
1836 : break;
1837 :
1838 : default:
1839 : break;
1840 : }
1841 : return true;
1842 : }
1843 :
1844 : bool
1845 11535 : foperator_unordered_le::op2_range (frange &r,
1846 : tree type,
1847 : const irange &lhs,
1848 : const frange &op1,
1849 : relation_trio) const
1850 : {
1851 11535 : switch (get_bool_state (r, lhs, type))
1852 : {
1853 6997 : case BRS_TRUE:
1854 10436 : if (op1.maybe_isnan ())
1855 3558 : r.set_varying (type);
1856 3439 : else if (op1.undefined_p ())
1857 : return false;
1858 : else
1859 3439 : build_ge (r, type, op1);
1860 : break;
1861 :
1862 3050 : case BRS_FALSE:
1863 : // A false UNORDERED_LE means both operands are !NAN, so it's
1864 : // impossible for op1 to be a NAN.
1865 3050 : if (op1.known_isnan ())
1866 0 : r.set_undefined ();
1867 3050 : else if (op1.undefined_p ())
1868 : return false;
1869 3050 : else if (build_lt (r, type, op1))
1870 3050 : r.clear_nan ();
1871 : break;
1872 :
1873 : default:
1874 : break;
1875 : }
1876 : return true;
1877 : }
1878 :
1879 : class foperator_unordered_gt : public range_operator
1880 : {
1881 : using range_operator::fold_range;
1882 : using range_operator::op1_range;
1883 : using range_operator::op2_range;
1884 : public:
1885 54363 : bool fold_range (irange &r, tree type,
1886 : const frange &op1, const frange &op2,
1887 : relation_trio trio = TRIO_VARYING) const final override
1888 : {
1889 54363 : if (op1.known_isnan () || op2.known_isnan ())
1890 : {
1891 152 : r = range_true (type);
1892 152 : return true;
1893 : }
1894 54211 : frange op1_no_nan = op1;
1895 54211 : frange op2_no_nan = op2;
1896 58503 : if (op1.maybe_isnan ())
1897 49900 : op1_no_nan.clear_nan ();
1898 103366 : if (op2.maybe_isnan ())
1899 5056 : op2_no_nan.clear_nan ();
1900 54211 : if (!range_op_handler (GT_EXPR).fold_range (r, type, op1_no_nan,
1901 : op2_no_nan, trio))
1902 : return false;
1903 : // The result is the same as the ordered version when the
1904 : // comparison is true or when the operands cannot be NANs.
1905 54211 : if (!maybe_isnan (op1, op2) || r == range_true (type))
1906 : return true;
1907 : else
1908 : {
1909 49974 : r = range_true_and_false (type);
1910 49974 : return true;
1911 : }
1912 : }
1913 : bool op1_range (frange &r, tree type,
1914 : const irange &lhs, const frange &op2,
1915 : relation_trio = TRIO_VARYING) const final override;
1916 : bool op2_range (frange &r, tree type,
1917 : const irange &lhs, const frange &op1,
1918 : relation_trio = TRIO_VARYING) const final override;
1919 : };
1920 : static const foperator_unordered_gt fop_unordered_gt;
1921 :
1922 : bool
1923 18810 : foperator_unordered_gt::op1_range (frange &r,
1924 : tree type,
1925 : const irange &lhs,
1926 : const frange &op2,
1927 : relation_trio) const
1928 : {
1929 18810 : switch (get_bool_state (r, lhs, type))
1930 : {
1931 4678 : case BRS_TRUE:
1932 9128 : if (op2.maybe_isnan ())
1933 228 : r.set_varying (type);
1934 4450 : else if (op2.undefined_p ())
1935 : return false;
1936 : else
1937 4450 : build_gt (r, type, op2);
1938 : break;
1939 :
1940 12616 : case BRS_FALSE:
1941 : // A false UNORDERED_GT means both operands are !NAN, so it's
1942 : // impossible for op2 to be a NAN.
1943 12616 : if (op2.known_isnan ())
1944 0 : r.set_undefined ();
1945 12616 : else if (op2.undefined_p ())
1946 : return false;
1947 12616 : else if (build_le (r, type, op2))
1948 12616 : r.clear_nan ();
1949 : break;
1950 :
1951 : default:
1952 : break;
1953 : }
1954 : return true;
1955 : }
1956 :
1957 : bool
1958 3081 : foperator_unordered_gt::op2_range (frange &r,
1959 : tree type,
1960 : const irange &lhs,
1961 : const frange &op1,
1962 : relation_trio) const
1963 : {
1964 3081 : switch (get_bool_state (r, lhs, type))
1965 : {
1966 1187 : case BRS_TRUE:
1967 1679 : if (op1.maybe_isnan ())
1968 695 : r.set_varying (type);
1969 492 : else if (op1.undefined_p ())
1970 : return false;
1971 : else
1972 492 : build_lt (r, type, op1);
1973 : break;
1974 :
1975 378 : case BRS_FALSE:
1976 : // A false UNORDERED_GT means both operands are !NAN, so it's
1977 : // impossible for op1 to be a NAN.
1978 378 : if (op1.known_isnan ())
1979 0 : r.set_undefined ();
1980 378 : else if (op1.undefined_p ())
1981 : return false;
1982 378 : else if (build_ge (r, type, op1))
1983 378 : r.clear_nan ();
1984 : break;
1985 :
1986 : default:
1987 : break;
1988 : }
1989 : return true;
1990 : }
1991 :
1992 : class foperator_unordered_ge : public range_operator
1993 : {
1994 : using range_operator::fold_range;
1995 : using range_operator::op1_range;
1996 : using range_operator::op2_range;
1997 : public:
1998 172995 : bool fold_range (irange &r, tree type,
1999 : const frange &op1, const frange &op2,
2000 : relation_trio trio = TRIO_VARYING) const final override
2001 : {
2002 172995 : if (op1.known_isnan () || op2.known_isnan ())
2003 : {
2004 52 : r = range_true (type);
2005 52 : return true;
2006 : }
2007 172943 : frange op1_no_nan = op1;
2008 172943 : frange op2_no_nan = op2;
2009 186266 : if (op1.maybe_isnan ())
2010 159580 : op1_no_nan.clear_nan ();
2011 338279 : if (op2.maybe_isnan ())
2012 7601 : op2_no_nan.clear_nan ();
2013 172943 : if (!range_op_handler (GE_EXPR).fold_range (r, type, op1_no_nan,
2014 : op2_no_nan, trio))
2015 : return false;
2016 : // The result is the same as the ordered version when the
2017 : // comparison is true or when the operands cannot be NANs.
2018 172943 : if (!maybe_isnan (op1, op2) || r == range_true (type))
2019 : return true;
2020 : else
2021 : {
2022 158870 : r = range_true_and_false (type);
2023 158870 : return true;
2024 : }
2025 : }
2026 : bool op1_range (frange &r, tree type,
2027 : const irange &lhs, const frange &op2,
2028 : relation_trio = TRIO_VARYING) const final override;
2029 : bool op2_range (frange &r, tree type,
2030 : const irange &lhs, const frange &op1,
2031 : relation_trio = TRIO_VARYING) const final override;
2032 : };
2033 : static const foperator_unordered_ge fop_unordered_ge;
2034 :
2035 : bool
2036 143965 : foperator_unordered_ge::op1_range (frange &r,
2037 : tree type,
2038 : const irange &lhs,
2039 : const frange &op2,
2040 : relation_trio) const
2041 : {
2042 143965 : switch (get_bool_state (r, lhs, type))
2043 : {
2044 60376 : case BRS_TRUE:
2045 116929 : if (op2.maybe_isnan ())
2046 3823 : r.set_varying (type);
2047 56553 : else if (op2.undefined_p ())
2048 : return false;
2049 : else
2050 56553 : build_ge (r, type, op2);
2051 : break;
2052 :
2053 82039 : case BRS_FALSE:
2054 : // A false UNORDERED_GE means both operands are !NAN, so it's
2055 : // impossible for op2 to be a NAN.
2056 82039 : if (op2.known_isnan ())
2057 0 : r.set_undefined ();
2058 82039 : else if (op2.undefined_p ())
2059 : return false;
2060 82039 : else if (build_lt (r, type, op2))
2061 82035 : r.clear_nan ();
2062 : break;
2063 :
2064 : default:
2065 : break;
2066 : }
2067 : return true;
2068 : }
2069 :
2070 : bool
2071 12073 : foperator_unordered_ge::op2_range (frange &r, tree type,
2072 : const irange &lhs,
2073 : const frange &op1,
2074 : relation_trio) const
2075 : {
2076 12073 : switch (get_bool_state (r, lhs, type))
2077 : {
2078 6593 : case BRS_TRUE:
2079 9570 : if (op1.maybe_isnan ())
2080 3616 : r.set_varying (type);
2081 2977 : else if (op1.undefined_p ())
2082 : return false;
2083 : else
2084 2977 : build_le (r, type, op1);
2085 : break;
2086 :
2087 4050 : case BRS_FALSE:
2088 : // A false UNORDERED_GE means both operands are !NAN, so it's
2089 : // impossible for op1 to be a NAN.
2090 4050 : if (op1.known_isnan ())
2091 0 : r.set_undefined ();
2092 4050 : else if (op1.undefined_p ())
2093 : return false;
2094 4050 : else if (build_gt (r, type, op1))
2095 4050 : r.clear_nan ();
2096 : break;
2097 :
2098 : default:
2099 : break;
2100 : }
2101 : return true;
2102 : }
2103 :
2104 : class foperator_unordered_equal : public range_operator
2105 : {
2106 : using range_operator::fold_range;
2107 : using range_operator::op1_range;
2108 : using range_operator::op2_range;
2109 : public:
2110 4022 : bool fold_range (irange &r, tree type,
2111 : const frange &op1, const frange &op2,
2112 : relation_trio trio = TRIO_VARYING) const final override
2113 : {
2114 4022 : if (op1.known_isnan () || op2.known_isnan ())
2115 : {
2116 0 : r = range_true (type);
2117 0 : return true;
2118 : }
2119 4022 : frange op1_no_nan = op1;
2120 4022 : frange op2_no_nan = op2;
2121 4438 : if (op1.maybe_isnan ())
2122 3606 : op1_no_nan.clear_nan ();
2123 4539 : if (op2.maybe_isnan ())
2124 3505 : op2_no_nan.clear_nan ();
2125 4022 : if (!range_op_handler (EQ_EXPR).fold_range (r, type, op1_no_nan,
2126 : op2_no_nan, trio))
2127 : return false;
2128 : // The result is the same as the ordered version when the
2129 : // comparison is true or when the operands cannot be NANs.
2130 4022 : if (!maybe_isnan (op1, op2) || r == range_true (type))
2131 : return true;
2132 : else
2133 : {
2134 3605 : r = range_true_and_false (type);
2135 3605 : return true;
2136 : }
2137 : }
2138 : bool op1_range (frange &r, tree type,
2139 : const irange &lhs, const frange &op2,
2140 : relation_trio = TRIO_VARYING) const final override;
2141 1636 : bool op2_range (frange &r, tree type,
2142 : const irange &lhs, const frange &op1,
2143 : relation_trio rel = TRIO_VARYING) const final override
2144 : {
2145 1636 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
2146 : }
2147 : };
2148 : static const foperator_unordered_equal fop_unordered_equal;
2149 :
2150 : bool
2151 3275 : foperator_unordered_equal::op1_range (frange &r, tree type,
2152 : const irange &lhs,
2153 : const frange &op2,
2154 : relation_trio) const
2155 : {
2156 3275 : switch (get_bool_state (r, lhs, type))
2157 : {
2158 3 : case BRS_TRUE:
2159 : // If it's true, the result is the same as OP2 plus a NAN.
2160 3 : r = op2;
2161 : // Add both zeros if there's the possibility of zero equality.
2162 3 : frange_add_zeros (r, type);
2163 : // Add the possibility of a NAN.
2164 3 : r.update_nan ();
2165 3 : break;
2166 :
2167 0 : case BRS_FALSE:
2168 : // A false UNORDERED_EQ means both operands are !NAN, so it's
2169 : // impossible for op2 to be a NAN.
2170 0 : if (op2.known_isnan ())
2171 0 : r.set_undefined ();
2172 : else
2173 : {
2174 : // The false side indicates !NAN and not equal. We can at least
2175 : // represent !NAN.
2176 0 : r.set_varying (type);
2177 0 : r.clear_nan ();
2178 : }
2179 : break;
2180 :
2181 : default:
2182 : break;
2183 : }
2184 3275 : return true;
2185 : }
2186 :
2187 : class foperator_ltgt : public range_operator
2188 : {
2189 : using range_operator::fold_range;
2190 : using range_operator::op1_range;
2191 : using range_operator::op2_range;
2192 : public:
2193 365 : bool fold_range (irange &r, tree type,
2194 : const frange &op1, const frange &op2,
2195 : relation_trio trio = TRIO_VARYING) const final override
2196 : {
2197 365 : if (op1.known_isnan () || op2.known_isnan ())
2198 : {
2199 0 : r = range_false (type);
2200 0 : return true;
2201 : }
2202 365 : frange op1_no_nan = op1;
2203 365 : frange op2_no_nan = op2;
2204 365 : if (op1.maybe_isnan ())
2205 354 : op1_no_nan.clear_nan ();
2206 365 : if (op2.maybe_isnan ())
2207 354 : op2_no_nan.clear_nan ();
2208 365 : if (!range_op_handler (NE_EXPR).fold_range (r, type, op1_no_nan,
2209 : op2_no_nan, trio))
2210 : return false;
2211 : // The result is the same as the ordered version when the
2212 : // comparison is true or when the operands cannot be NANs.
2213 365 : if (!maybe_isnan (op1, op2) || r == range_false (type))
2214 : return true;
2215 : else
2216 : {
2217 354 : r = range_true_and_false (type);
2218 354 : return true;
2219 : }
2220 : }
2221 : bool op1_range (frange &r, tree type,
2222 : const irange &lhs, const frange &op2,
2223 : relation_trio = TRIO_VARYING) const final override;
2224 22 : bool op2_range (frange &r, tree type,
2225 : const irange &lhs, const frange &op1,
2226 : relation_trio rel = TRIO_VARYING) const final override
2227 : {
2228 22 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
2229 : }
2230 : };
2231 : static const foperator_ltgt fop_ltgt;
2232 :
2233 : bool
2234 34 : foperator_ltgt::op1_range (frange &r, tree type,
2235 : const irange &lhs,
2236 : const frange &op2,
2237 : relation_trio) const
2238 : {
2239 34 : switch (get_bool_state (r, lhs, type))
2240 : {
2241 0 : case BRS_TRUE:
2242 : // A true LTGT means both operands are !NAN, so it's
2243 : // impossible for op2 to be a NAN.
2244 0 : if (op2.known_isnan ())
2245 0 : r.set_undefined ();
2246 : else
2247 : {
2248 : // The true side indicates !NAN and not equal. We can at least
2249 : // represent !NAN.
2250 0 : r.set_varying (type);
2251 0 : r.clear_nan ();
2252 : }
2253 : break;
2254 :
2255 10 : case BRS_FALSE:
2256 : // If it's false, the result is the same as OP2 plus a NAN.
2257 10 : r = op2;
2258 : // Add both zeros if there's the possibility of zero equality.
2259 10 : frange_add_zeros (r, type);
2260 : // Add the possibility of a NAN.
2261 10 : r.update_nan ();
2262 10 : break;
2263 :
2264 : default:
2265 : break;
2266 : }
2267 34 : return true;
2268 : }
2269 :
2270 : // Final tweaks for float binary op op1_range/op2_range.
2271 : // Return TRUE if the operation is performed and a valid range is available.
2272 :
2273 : static bool
2274 998484 : float_binary_op_range_finish (bool ret, frange &r, tree type,
2275 : const frange &lhs, bool div_op2 = false)
2276 : {
2277 998484 : if (!ret)
2278 : return false;
2279 :
2280 : // If we get a known NAN from reverse op, it means either that
2281 : // the other operand was known NAN (in that case we know nothing),
2282 : // or the reverse operation introduced a known NAN.
2283 : // Say for lhs = op1 * op2 if lhs is [-0, +0] and op2 is too,
2284 : // 0 / 0 is known NAN. Just punt in that case.
2285 : // If NANs aren't honored, we get for 0 / 0 UNDEFINED, so punt as well.
2286 : // Or if lhs is a known NAN, we also don't know anything.
2287 998484 : if (r.known_isnan () || lhs.known_isnan () || r.undefined_p ())
2288 : {
2289 5511 : r.set_varying (type);
2290 5511 : return true;
2291 : }
2292 :
2293 : // If lhs isn't NAN, then neither operand could be NAN,
2294 : // even if the reverse operation does introduce a maybe_nan.
2295 1778710 : if (!lhs.maybe_isnan ())
2296 : {
2297 785737 : r.clear_nan ();
2298 44356 : if (div_op2
2299 785737 : ? !lhs.contains_zero_p ()
2300 1331314 : : !(real_isinf (&lhs.lower_bound ())
2301 589933 : || real_isinf (&lhs.upper_bound ())))
2302 : // For reverse + or - or * or op1 of /, if result is finite, then
2303 : // r must be finite too, as X + INF or X - INF or X * INF or
2304 : // INF / X is always +-INF or NAN. For op2 of /, if result is
2305 : // non-zero and not NAN, r must be finite, as X / INF is always
2306 : // 0 or NAN.
2307 563896 : frange_drop_infs (r, type);
2308 : }
2309 : // If lhs is a maybe or known NAN, the operand could be
2310 : // NAN.
2311 : else
2312 207236 : r.update_nan ();
2313 : return true;
2314 : }
2315 :
2316 : // True if [lb, ub] is [+-0, +-0].
2317 : static bool
2318 5993918 : zero_p (const REAL_VALUE_TYPE &lb, const REAL_VALUE_TYPE &ub)
2319 : {
2320 5993918 : return real_iszero (&lb) && real_iszero (&ub);
2321 : }
2322 :
2323 : // True if +0 or -0 is in [lb, ub] range.
2324 : static bool
2325 4999286 : contains_zero_p (const REAL_VALUE_TYPE &lb, const REAL_VALUE_TYPE &ub)
2326 : {
2327 4999286 : return (real_compare (LE_EXPR, &lb, &dconst0)
2328 4999286 : && real_compare (GE_EXPR, &ub, &dconst0));
2329 : }
2330 :
2331 : // True if [lb, ub] is [-INF, -INF] or [+INF, +INF].
2332 : static bool
2333 3496824 : singleton_inf_p (const REAL_VALUE_TYPE &lb, const REAL_VALUE_TYPE &ub)
2334 : {
2335 3496824 : return real_isinf (&lb) && real_isinf (&ub, real_isneg (&lb));
2336 : }
2337 :
2338 : // Return -1 if binary op result must have sign bit set,
2339 : // 1 if binary op result must have sign bit clear,
2340 : // 0 otherwise.
2341 : // Sign bit of binary op result is exclusive or of the
2342 : // operand's sign bits.
2343 : static int
2344 1563234 : signbit_known_p (const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub,
2345 : const REAL_VALUE_TYPE &rh_lb, const REAL_VALUE_TYPE &rh_ub)
2346 : {
2347 1563234 : if (real_isneg (&lh_lb) == real_isneg (&lh_ub)
2348 1563234 : && real_isneg (&rh_lb) == real_isneg (&rh_ub))
2349 : {
2350 200170 : if (real_isneg (&lh_lb) == real_isneg (&rh_ub))
2351 : return 1;
2352 : else
2353 37501 : return -1;
2354 : }
2355 : return 0;
2356 : }
2357 :
2358 : // Set [lb, ub] to [-0, -0], [-0, +0] or [+0, +0] depending on
2359 : // signbit_known.
2360 : static void
2361 7193 : zero_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, int signbit_known)
2362 : {
2363 7193 : ub = lb = dconst0;
2364 7193 : if (signbit_known <= 0)
2365 6951 : lb = dconstm0;
2366 6951 : if (signbit_known < 0)
2367 66 : ub = lb;
2368 7193 : }
2369 :
2370 : // Set [lb, ub] to [-INF, -INF], [-INF, +INF] or [+INF, +INF] depending on
2371 : // signbit_known.
2372 : static void
2373 4626 : inf_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, int signbit_known)
2374 : {
2375 4626 : if (signbit_known > 0)
2376 1025 : ub = lb = dconstinf;
2377 3601 : else if (signbit_known < 0)
2378 157 : ub = lb = dconstninf;
2379 : else
2380 : {
2381 3444 : lb = dconstninf;
2382 3444 : ub = dconstinf;
2383 : }
2384 4626 : }
2385 :
2386 : // Set [lb, ub] to [-INF, -0], [-INF, +INF] or [+0, +INF] depending on
2387 : // signbit_known.
2388 : static void
2389 1102844 : zero_to_inf_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, int signbit_known)
2390 : {
2391 1102844 : if (signbit_known > 0)
2392 : {
2393 18481 : lb = dconst0;
2394 18481 : ub = dconstinf;
2395 : }
2396 1084363 : else if (signbit_known < 0)
2397 : {
2398 6666 : lb = dconstninf;
2399 6666 : ub = dconstm0;
2400 : }
2401 : else
2402 : {
2403 1077697 : lb = dconstninf;
2404 1077697 : ub = dconstinf;
2405 : }
2406 1102844 : }
2407 :
2408 : bool
2409 196762 : operator_plus::op1_range (frange &r, tree type, const frange &lhs,
2410 : const frange &op2, relation_trio) const
2411 : {
2412 196762 : if (lhs.undefined_p ())
2413 : return false;
2414 196762 : range_op_handler minus (MINUS_EXPR);
2415 196762 : if (!minus)
2416 : return false;
2417 196762 : frange wlhs = lhs;
2418 196762 : wlhs.widen (type);
2419 196762 : return float_binary_op_range_finish (minus.fold_range (r, type, wlhs, op2),
2420 : r, type, wlhs);
2421 196762 : }
2422 :
2423 : bool
2424 83611 : operator_plus::op2_range (frange &r, tree type,
2425 : const frange &lhs, const frange &op1,
2426 : relation_trio) const
2427 : {
2428 83611 : return op1_range (r, type, lhs, op1);
2429 : }
2430 :
2431 : void
2432 2175452 : operator_plus::rv_fold (frange &r, tree type,
2433 : const REAL_VALUE_TYPE &lh_lb,
2434 : const REAL_VALUE_TYPE &lh_ub,
2435 : const REAL_VALUE_TYPE &rh_lb,
2436 : const REAL_VALUE_TYPE &rh_ub,
2437 : relation_kind) const
2438 : {
2439 2175452 : REAL_VALUE_TYPE lb, ub;
2440 2175452 : bool maybe_nan = false;
2441 :
2442 2175452 : frange_arithmetic (PLUS_EXPR, type, lb, lh_lb, rh_lb, dconstninf);
2443 2175452 : frange_arithmetic (PLUS_EXPR, type, ub, lh_ub, rh_ub, dconstinf);
2444 :
2445 : // [-INF] + [+INF] = NAN
2446 2175452 : if (real_isinf (&lh_lb, true) && real_isinf (&rh_ub, false))
2447 : maybe_nan = true;
2448 : // [+INF] + [-INF] = NAN
2449 820818 : else if (real_isinf (&lh_ub, false) && real_isinf (&rh_lb, true))
2450 : maybe_nan = true;
2451 :
2452 : // Handle possible NANs by saturating to the appropriate INF if only
2453 : // one end is a NAN. If both ends are a NAN, just return a NAN.
2454 2175452 : bool lb_nan = real_isnan (&lb);
2455 2175452 : bool ub_nan = real_isnan (&ub);
2456 2175452 : if (lb_nan && ub_nan)
2457 : {
2458 0 : r.set_nan (type);
2459 0 : return;
2460 : }
2461 2175452 : if (lb_nan)
2462 360 : lb = dconstninf;
2463 2175092 : else if (ub_nan)
2464 0 : ub = dconstinf;
2465 2175452 : r.set (type, lb, ub, nan_state (maybe_nan));
2466 : }
2467 :
2468 :
2469 : bool
2470 103756 : operator_minus::op1_range (frange &r, tree type,
2471 : const frange &lhs, const frange &op2,
2472 : relation_trio) const
2473 : {
2474 103756 : if (lhs.undefined_p ())
2475 : return false;
2476 103756 : frange wlhs = lhs;
2477 103756 : wlhs.widen (type);
2478 103756 : return float_binary_op_range_finish (
2479 207512 : range_op_handler (PLUS_EXPR).fold_range (r, type, wlhs, op2),
2480 : r, type, wlhs);
2481 103756 : }
2482 :
2483 : bool
2484 142446 : operator_minus::op2_range (frange &r, tree type,
2485 : const frange &lhs, const frange &op1,
2486 : relation_trio) const
2487 : {
2488 142446 : if (lhs.undefined_p ())
2489 : return false;
2490 142446 : frange wlhs = lhs;
2491 142446 : wlhs.widen (type);
2492 142446 : return float_binary_op_range_finish (fold_range (r, type, op1, wlhs),
2493 : r, type, wlhs);
2494 142446 : }
2495 :
2496 : void
2497 1015852 : operator_minus::rv_fold (frange &r, tree type,
2498 : const REAL_VALUE_TYPE &lh_lb,
2499 : const REAL_VALUE_TYPE &lh_ub,
2500 : const REAL_VALUE_TYPE &rh_lb,
2501 : const REAL_VALUE_TYPE &rh_ub,
2502 : relation_kind) const
2503 : {
2504 1015852 : REAL_VALUE_TYPE lb, ub;
2505 1015852 : bool maybe_nan = false;
2506 :
2507 1015852 : frange_arithmetic (MINUS_EXPR, type, lb, lh_lb, rh_ub, dconstninf);
2508 1015852 : frange_arithmetic (MINUS_EXPR, type, ub, lh_ub, rh_lb, dconstinf);
2509 :
2510 : // [+INF] - [+INF] = NAN
2511 1015852 : if (real_isinf (&lh_ub, false) && real_isinf (&rh_ub, false))
2512 : maybe_nan = true;
2513 : // [-INF] - [-INF] = NAN
2514 715423 : else if (real_isinf (&lh_lb, true) && real_isinf (&rh_lb, true))
2515 : maybe_nan = true;
2516 :
2517 : // Handle possible NANs by saturating to the appropriate INF if only
2518 : // one end is a NAN. If both ends are a NAN, just return a NAN.
2519 1015852 : bool lb_nan = real_isnan (&lb);
2520 1015852 : bool ub_nan = real_isnan (&ub);
2521 1015852 : if (lb_nan && ub_nan)
2522 : {
2523 826 : r.set_nan (type);
2524 826 : return;
2525 : }
2526 1015026 : if (lb_nan)
2527 1443 : lb = dconstninf;
2528 1013583 : else if (ub_nan)
2529 1738 : ub = dconstinf;
2530 1015026 : r.set (type, lb, ub, nan_state (maybe_nan));
2531 : }
2532 :
2533 :
2534 : // Given CP[0] to CP[3] floating point values rounded to -INF,
2535 : // set LB to the smallest of them (treating -0 as smaller to +0).
2536 : // Given CP[4] to CP[7] floating point values rounded to +INF,
2537 : // set UB to the largest of them (treating -0 as smaller to +0).
2538 :
2539 : static void
2540 1302910 : find_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub,
2541 : const REAL_VALUE_TYPE (&cp)[8])
2542 : {
2543 1302910 : lb = cp[0];
2544 1302910 : ub = cp[4];
2545 5211640 : for (int i = 1; i < 4; ++i)
2546 : {
2547 3908730 : if (real_less (&cp[i], &lb)
2548 3908730 : || (real_iszero (&lb) && real_isnegzero (&cp[i])))
2549 442636 : lb = cp[i];
2550 3908730 : if (real_less (&ub, &cp[i + 4])
2551 3908730 : || (real_isnegzero (&ub) && real_iszero (&cp[i + 4])))
2552 1202168 : ub = cp[i + 4];
2553 : }
2554 1302910 : }
2555 :
2556 :
2557 : bool
2558 394997 : operator_mult::op1_range (frange &r, tree type,
2559 : const frange &lhs, const frange &op2,
2560 : relation_trio) const
2561 : {
2562 394997 : if (lhs.undefined_p ())
2563 : return false;
2564 394997 : range_op_handler rdiv (RDIV_EXPR);
2565 394997 : if (!rdiv)
2566 : return false;
2567 394997 : frange wlhs = lhs;
2568 394997 : wlhs.widen (type);
2569 394997 : bool ret = rdiv.fold_range (r, type, wlhs, op2);
2570 394997 : if (ret == false)
2571 : return false;
2572 394997 : if (wlhs.known_isnan () || op2.known_isnan () || op2.undefined_p ())
2573 16 : return float_binary_op_range_finish (ret, r, type, wlhs);
2574 394981 : const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound ();
2575 394981 : const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound ();
2576 394981 : const REAL_VALUE_TYPE &op2_lb = op2.lower_bound ();
2577 394981 : const REAL_VALUE_TYPE &op2_ub = op2.upper_bound ();
2578 731273 : if ((contains_zero_p (lhs_lb, lhs_ub) && contains_zero_p (op2_lb, op2_ub))
2579 562594 : || ((real_isinf (&lhs_lb) || real_isinf (&lhs_ub))
2580 47675 : && (real_isinf (&op2_lb) || real_isinf (&op2_ub))))
2581 : {
2582 : // If both lhs and op2 could be zeros or both could be infinities,
2583 : // we don't know anything about op1 except maybe for the sign
2584 : // and perhaps if it can be NAN or not.
2585 177713 : REAL_VALUE_TYPE lb, ub;
2586 177713 : int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op2_lb, op2_ub);
2587 177713 : zero_to_inf_range (lb, ub, signbit_known);
2588 177713 : r.set (type, lb, ub);
2589 : }
2590 : // Otherwise, if op2 is a singleton INF and lhs doesn't include INF,
2591 : // or if lhs must be zero and op2 doesn't include zero, it would be
2592 : // UNDEFINED, while rdiv.fold_range computes a zero or singleton INF
2593 : // range. Those are supersets of UNDEFINED, so let's keep that way.
2594 394981 : return float_binary_op_range_finish (ret, r, type, wlhs);
2595 : }
2596 :
2597 : bool
2598 95444 : operator_mult::op2_range (frange &r, tree type,
2599 : const frange &lhs, const frange &op1,
2600 : relation_trio) const
2601 : {
2602 95444 : return op1_range (r, type, lhs, op1);
2603 : }
2604 :
2605 : void
2606 1301207 : operator_mult::rv_fold (frange &r, tree type,
2607 : const REAL_VALUE_TYPE &lh_lb,
2608 : const REAL_VALUE_TYPE &lh_ub,
2609 : const REAL_VALUE_TYPE &rh_lb,
2610 : const REAL_VALUE_TYPE &rh_ub,
2611 : relation_kind kind) const
2612 : {
2613 1301207 : bool is_square
2614 : = (kind == VREL_EQ
2615 38845 : && real_equal (&lh_lb, &rh_lb)
2616 38842 : && real_equal (&lh_ub, &rh_ub)
2617 38842 : && real_isneg (&lh_lb) == real_isneg (&rh_lb)
2618 1340049 : && real_isneg (&lh_ub) == real_isneg (&rh_ub));
2619 1262365 : REAL_VALUE_TYPE lb, ub;
2620 2116704 : bool maybe_nan = false;
2621 : // x * x never produces a new NAN and we only multiply the same
2622 : // values, so the 0 * INF problematic cases never appear there.
2623 1262365 : if (!is_square)
2624 : {
2625 : // [+-0, +-0] * [+INF,+INF] (or [-INF,-INF] or swapped is a known NAN.
2626 1286019 : if ((zero_p (lh_lb, lh_ub) && singleton_inf_p (rh_lb, rh_ub))
2627 1285989 : || (zero_p (rh_lb, rh_ub) && singleton_inf_p (lh_lb, lh_ub)))
2628 : {
2629 85 : r.set_nan (type);
2630 446868 : return;
2631 : }
2632 :
2633 : // Otherwise, if one range includes zero and the other ends with +-INF,
2634 : // it is a maybe NAN.
2635 1262280 : if ((contains_zero_p (lh_lb, lh_ub)
2636 1042792 : && (real_isinf (&rh_lb) || real_isinf (&rh_ub)))
2637 1909678 : || (contains_zero_p (rh_lb, rh_ub)
2638 237422 : && (real_isinf (&lh_lb) || real_isinf (&lh_ub))))
2639 : {
2640 446783 : maybe_nan = true;
2641 :
2642 446783 : int signbit_known = signbit_known_p (lh_lb, lh_ub, rh_lb, rh_ub);
2643 :
2644 : // If one of the ranges that includes INF is singleton
2645 : // and the other range includes zero, the resulting
2646 : // range is INF and NAN, because the 0 * INF boundary
2647 : // case will be NAN, but already nextafter (0, 1) * INF
2648 : // is INF.
2649 446783 : if (singleton_inf_p (lh_lb, lh_ub)
2650 446783 : || singleton_inf_p (rh_lb, rh_ub))
2651 : {
2652 1312 : inf_range (lb, ub, signbit_known);
2653 1312 : r.set (type, lb, ub, nan_state (true));
2654 1312 : return;
2655 : }
2656 :
2657 : // If one of the multiplicands must be zero, the resulting
2658 : // range is +-0 and NAN.
2659 445471 : if (zero_p (lh_lb, lh_ub) || zero_p (rh_lb, rh_ub))
2660 : {
2661 6364 : zero_range (lb, ub, signbit_known);
2662 6364 : r.set (type, lb, ub, nan_state (true));
2663 6364 : return;
2664 : }
2665 :
2666 : // Otherwise one of the multiplicands could be
2667 : // [0.0, nextafter (0.0, 1.0)] and the [DBL_MAX, INF]
2668 : // or similarly with different signs. 0.0 * DBL_MAX
2669 : // is still 0.0, nextafter (0.0, 1.0) * INF is still INF,
2670 : // so if the signs are always the same or always different,
2671 : // result is [+0.0, +INF] or [-INF, -0.0], otherwise VARYING.
2672 439107 : zero_to_inf_range (lb, ub, signbit_known);
2673 439107 : r.set (type, lb, ub, nan_state (true));
2674 439107 : return;
2675 : }
2676 : }
2677 :
2678 854339 : REAL_VALUE_TYPE cp[8];
2679 : // Do a cross-product. At this point none of the multiplications
2680 : // should produce a NAN.
2681 854339 : frange_arithmetic (MULT_EXPR, type, cp[0], lh_lb, rh_lb, dconstninf);
2682 854339 : frange_arithmetic (MULT_EXPR, type, cp[4], lh_lb, rh_lb, dconstinf);
2683 854339 : if (is_square)
2684 : {
2685 : // For x * x we can just do max (lh_lb * lh_lb, lh_ub * lh_ub)
2686 : // as maximum and -0.0 as minimum if 0.0 is in the range,
2687 : // otherwise min (lh_lb * lh_lb, lh_ub * lh_ub).
2688 : // -0.0 rather than 0.0 because VREL_EQ doesn't prove that
2689 : // x and y are bitwise equal, just that they compare equal.
2690 38842 : if (contains_zero_p (lh_lb, lh_ub))
2691 : {
2692 35171 : if (real_isneg (&lh_lb) == real_isneg (&lh_ub))
2693 1514 : cp[1] = dconst0;
2694 : else
2695 33657 : cp[1] = dconstm0;
2696 : }
2697 : else
2698 3671 : cp[1] = cp[0];
2699 38842 : cp[2] = cp[0];
2700 38842 : cp[5] = cp[4];
2701 38842 : cp[6] = cp[4];
2702 : }
2703 : else
2704 : {
2705 815497 : frange_arithmetic (MULT_EXPR, type, cp[1], lh_lb, rh_ub, dconstninf);
2706 815497 : frange_arithmetic (MULT_EXPR, type, cp[5], lh_lb, rh_ub, dconstinf);
2707 815497 : frange_arithmetic (MULT_EXPR, type, cp[2], lh_ub, rh_lb, dconstninf);
2708 815497 : frange_arithmetic (MULT_EXPR, type, cp[6], lh_ub, rh_lb, dconstinf);
2709 : }
2710 854339 : frange_arithmetic (MULT_EXPR, type, cp[3], lh_ub, rh_ub, dconstninf);
2711 854339 : frange_arithmetic (MULT_EXPR, type, cp[7], lh_ub, rh_ub, dconstinf);
2712 :
2713 854339 : find_range (lb, ub, cp);
2714 :
2715 854339 : gcc_checking_assert (!real_isnan (&lb));
2716 854339 : gcc_checking_assert (!real_isnan (&ub));
2717 854339 : r.set (type, lb, ub, nan_state (maybe_nan));
2718 : }
2719 :
2720 :
2721 : class foperator_div : public range_operator
2722 : {
2723 : using range_operator::op1_range;
2724 : using range_operator::op2_range;
2725 : public:
2726 34228 : virtual bool op1_range (frange &r, tree type,
2727 : const frange &lhs,
2728 : const frange &op2,
2729 : relation_trio = TRIO_VARYING) const final override
2730 : {
2731 34228 : if (lhs.undefined_p ())
2732 : return false;
2733 34228 : frange wlhs = lhs;
2734 34228 : wlhs.widen (type);
2735 34228 : bool ret = range_op_handler (MULT_EXPR).fold_range (r, type, wlhs, op2);
2736 34228 : if (!ret)
2737 : return ret;
2738 34228 : if (wlhs.known_isnan () || op2.known_isnan () || op2.undefined_p ())
2739 338 : return float_binary_op_range_finish (ret, r, type, wlhs);
2740 33890 : const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound ();
2741 33890 : const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound ();
2742 33890 : const REAL_VALUE_TYPE &op2_lb = op2.lower_bound ();
2743 33890 : const REAL_VALUE_TYPE &op2_ub = op2.upper_bound ();
2744 33890 : if ((contains_zero_p (lhs_lb, lhs_ub)
2745 25542 : && (real_isinf (&op2_lb) || real_isinf (&op2_ub)))
2746 41269 : || ((contains_zero_p (op2_lb, op2_ub))
2747 8515 : && (real_isinf (&lhs_lb) || real_isinf (&lhs_ub))))
2748 : {
2749 : // If both lhs could be zero and op2 infinity or vice versa,
2750 : // we don't know anything about op1 except maybe for the sign
2751 : // and perhaps if it can be NAN or not.
2752 26065 : REAL_VALUE_TYPE lb, ub;
2753 26065 : int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op2_lb, op2_ub);
2754 26065 : zero_to_inf_range (lb, ub, signbit_known);
2755 26065 : r.set (type, lb, ub);
2756 : }
2757 33890 : return float_binary_op_range_finish (ret, r, type, wlhs);
2758 : }
2759 64228 : virtual bool op2_range (frange &r, tree type,
2760 : const frange &lhs,
2761 : const frange &op1,
2762 : relation_trio = TRIO_VARYING) const final override
2763 : {
2764 64228 : if (lhs.undefined_p ())
2765 : return false;
2766 64228 : frange wlhs = lhs;
2767 64228 : wlhs.widen (type);
2768 64228 : bool ret = fold_range (r, type, op1, wlhs);
2769 64228 : if (!ret)
2770 : return ret;
2771 64228 : if (wlhs.known_isnan () || op1.known_isnan () || op1.undefined_p ())
2772 18 : return float_binary_op_range_finish (ret, r, type, wlhs, true);
2773 64210 : const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound ();
2774 64210 : const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound ();
2775 64210 : const REAL_VALUE_TYPE &op1_lb = op1.lower_bound ();
2776 64210 : const REAL_VALUE_TYPE &op1_ub = op1.upper_bound ();
2777 117063 : if ((contains_zero_p (lhs_lb, lhs_ub) && contains_zero_p (op1_lb, op1_ub))
2778 68097 : || ((real_isinf (&lhs_lb) || real_isinf (&lhs_ub))
2779 7591 : && (real_isinf (&op1_lb) || real_isinf (&op1_ub))))
2780 : {
2781 : // If both lhs and op1 could be zeros or both could be infinities,
2782 : // we don't know anything about op2 except maybe for the sign
2783 : // and perhaps if it can be NAN or not.
2784 53358 : REAL_VALUE_TYPE lb, ub;
2785 53358 : int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op1_lb, op1_ub);
2786 53358 : zero_to_inf_range (lb, ub, signbit_known);
2787 53358 : r.set (type, lb, ub);
2788 : }
2789 64210 : return float_binary_op_range_finish (ret, r, type, wlhs, true);
2790 : }
2791 : private:
2792 859952 : void rv_fold (frange &r, tree type,
2793 : const REAL_VALUE_TYPE &lh_lb,
2794 : const REAL_VALUE_TYPE &lh_ub,
2795 : const REAL_VALUE_TYPE &rh_lb,
2796 : const REAL_VALUE_TYPE &rh_ub,
2797 : relation_kind) const final override
2798 : {
2799 : // +-0.0 / +-0.0 or +-INF / +-INF is a known NAN.
2800 861402 : if ((zero_p (lh_lb, lh_ub) && zero_p (rh_lb, rh_ub))
2801 860771 : || (singleton_inf_p (lh_lb, lh_ub) && singleton_inf_p (rh_lb, rh_ub)))
2802 : {
2803 637 : r.set_nan (type);
2804 411381 : return;
2805 : }
2806 :
2807 859315 : REAL_VALUE_TYPE lb, ub;
2808 859315 : bool maybe_nan = false;
2809 : // If +-0.0 is in both ranges, it is a maybe NAN.
2810 859315 : if (contains_zero_p (lh_lb, lh_ub) && contains_zero_p (rh_lb, rh_ub))
2811 : maybe_nan = true;
2812 : // If +-INF is in both ranges, it is a maybe NAN.
2813 942821 : else if ((real_isinf (&lh_lb) || real_isinf (&lh_ub))
2814 594256 : && (real_isinf (&rh_lb) || real_isinf (&rh_ub)))
2815 : maybe_nan = true;
2816 :
2817 859315 : int signbit_known = signbit_known_p (lh_lb, lh_ub, rh_lb, rh_ub);
2818 :
2819 : // If dividend must be zero, the range is just +-0
2820 : // (including if the divisor is +-INF).
2821 : // If divisor must be +-INF, the range is just +-0
2822 : // (including if the dividend is zero).
2823 859315 : if (zero_p (lh_lb, lh_ub) || singleton_inf_p (rh_lb, rh_ub))
2824 : {
2825 829 : zero_range (lb, ub, signbit_known);
2826 829 : r.set (type, lb, ub, nan_state (maybe_nan));
2827 829 : return;
2828 : }
2829 :
2830 : // If divisor must be zero, the range is just +-INF
2831 : // (including if the dividend is +-INF).
2832 : // If dividend must be +-INF, the range is just +-INF
2833 : // (including if the dividend is zero).
2834 858486 : if (zero_p (rh_lb, rh_ub) || singleton_inf_p (lh_lb, lh_ub))
2835 : {
2836 3314 : inf_range (lb, ub, signbit_known);
2837 3314 : r.set (type, lb, ub, nan_state (maybe_nan));
2838 3314 : return;
2839 : }
2840 :
2841 : // Otherwise if both operands may be zero, divisor could be
2842 : // nextafter(0.0, +-1.0) and dividend +-0.0
2843 : // in which case result is going to INF or vice versa and
2844 : // result +0.0. So, all we can say for that case is if the
2845 : // signs of divisor and dividend are always the same we have
2846 : // [+0.0, +INF], if they are always different we have
2847 : // [-INF, -0.0]. If they vary, VARYING.
2848 : // If both may be +-INF, divisor could be INF and dividend FLT_MAX,
2849 : // in which case result is going to INF or vice versa and
2850 : // result +0.0. So, all we can say for that case is if the
2851 : // signs of divisor and dividend are always the same we have
2852 : // [+0.0, +INF], if they are always different we have
2853 : // [-INF, -0.0]. If they vary, VARYING.
2854 855172 : if (maybe_nan)
2855 : {
2856 406601 : zero_to_inf_range (lb, ub, signbit_known);
2857 406601 : r.set (type, lb, ub, nan_state (maybe_nan));
2858 406601 : return;
2859 : }
2860 :
2861 448571 : REAL_VALUE_TYPE cp[8];
2862 : // Do a cross-division. At this point none of the divisions should
2863 : // produce a NAN.
2864 448571 : frange_arithmetic (RDIV_EXPR, type, cp[0], lh_lb, rh_lb, dconstninf);
2865 448571 : frange_arithmetic (RDIV_EXPR, type, cp[1], lh_lb, rh_ub, dconstninf);
2866 448571 : frange_arithmetic (RDIV_EXPR, type, cp[2], lh_ub, rh_lb, dconstninf);
2867 448571 : frange_arithmetic (RDIV_EXPR, type, cp[3], lh_ub, rh_ub, dconstninf);
2868 448571 : frange_arithmetic (RDIV_EXPR, type, cp[4], lh_lb, rh_lb, dconstinf);
2869 448571 : frange_arithmetic (RDIV_EXPR, type, cp[5], lh_lb, rh_ub, dconstinf);
2870 448571 : frange_arithmetic (RDIV_EXPR, type, cp[6], lh_ub, rh_lb, dconstinf);
2871 448571 : frange_arithmetic (RDIV_EXPR, type, cp[7], lh_ub, rh_ub, dconstinf);
2872 :
2873 448571 : find_range (lb, ub, cp);
2874 :
2875 : // If divisor may be zero (but is not known to be only zero),
2876 : // and dividend can't be zero, the range can go up to -INF or +INF
2877 : // depending on the signs.
2878 448571 : if (contains_zero_p (rh_lb, rh_ub))
2879 : {
2880 44914 : if (signbit_known <= 0)
2881 40174 : real_inf (&lb, true);
2882 40174 : if (signbit_known >= 0)
2883 44894 : real_inf (&ub, false);
2884 : }
2885 :
2886 448571 : gcc_checking_assert (!real_isnan (&lb));
2887 448571 : gcc_checking_assert (!real_isnan (&ub));
2888 448571 : r.set (type, lb, ub, nan_state (maybe_nan));
2889 : }
2890 : };
2891 : static const foperator_div fop_div;
2892 :
2893 : bool
2894 948825 : operator_cast::fold_range (frange &r, tree type, const frange &op1,
2895 : const frange &, relation_trio) const
2896 : {
2897 948825 : enum machine_mode mode = TYPE_MODE (type);
2898 6641775 : bool mode_composite = MODE_COMPOSITE_P (mode);
2899 :
2900 948825 : if (empty_range_varying (r, type, op1, op1))
2901 1096 : return true;
2902 3790916 : if (!MODE_HAS_NANS (mode) && op1.maybe_isnan ())
2903 : {
2904 0 : r.set_varying (type);
2905 0 : return true;
2906 : }
2907 947729 : if (op1.known_isnan ())
2908 : {
2909 2766 : r.set_nan (type);
2910 2766 : return true;
2911 : }
2912 :
2913 944963 : r.set_undefined ();
2914 2862204 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
2915 : {
2916 972278 : REAL_VALUE_TYPE lb, ub;
2917 972278 : const REAL_VALUE_TYPE &lh_lb = op1.lower_bound (i);
2918 972278 : const REAL_VALUE_TYPE &lh_ub = op1.upper_bound (i);
2919 972278 : real_convert (&lb, mode, &lh_lb);
2920 972278 : real_convert (&ub, mode, &lh_ub);
2921 :
2922 972278 : if (flag_rounding_math)
2923 : {
2924 42602 : if (real_less (&lh_lb, &lb))
2925 : {
2926 3436 : if (mode_composite
2927 3436 : && (real_isdenormal (&lb, mode) || real_iszero (&lb)))
2928 : {
2929 : // IBM extended denormals only have DFmode precision.
2930 0 : REAL_VALUE_TYPE tmp, tmp2;
2931 0 : real_convert (&tmp2, DFmode, &lh_lb);
2932 0 : real_nextafter (&tmp, REAL_MODE_FORMAT (DFmode), &tmp2,
2933 : &dconstninf);
2934 0 : real_convert (&lb, mode, &tmp);
2935 : }
2936 : else
2937 3436 : frange_nextafter (mode, lb, dconstninf);
2938 : }
2939 42602 : if (real_less (&ub, &lh_ub))
2940 : {
2941 1550 : if (mode_composite
2942 1550 : && (real_isdenormal (&ub, mode) || real_iszero (&ub)))
2943 : {
2944 : // IBM extended denormals only have DFmode precision.
2945 0 : REAL_VALUE_TYPE tmp, tmp2;
2946 0 : real_convert (&tmp2, DFmode, &lh_ub);
2947 0 : real_nextafter (&tmp, REAL_MODE_FORMAT (DFmode), &tmp2,
2948 : &dconstinf);
2949 0 : real_convert (&ub, mode, &tmp);
2950 : }
2951 : else
2952 1550 : frange_nextafter (mode, ub, dconstinf);
2953 : }
2954 : }
2955 :
2956 972278 : frange tmp;
2957 972278 : tmp.set (type, lb, ub, op1.get_nan_state ());
2958 972278 : r.union_ (tmp);
2959 972278 : }
2960 :
2961 944963 : if (flag_trapping_math
2962 4281826 : && MODE_HAS_INFINITIES (TYPE_MODE (type))
2963 834216 : && r.known_isinf ()
2964 945047 : && !op1.known_isinf ())
2965 : {
2966 1 : REAL_VALUE_TYPE inf = r.lower_bound ();
2967 1 : if (real_isneg (&inf))
2968 : {
2969 0 : REAL_VALUE_TYPE min = real_min_representable (type);
2970 0 : r.set (type, inf, min);
2971 : }
2972 : else
2973 : {
2974 1 : REAL_VALUE_TYPE max = real_max_representable (type);
2975 1 : r.set (type, max, inf);
2976 : }
2977 : }
2978 :
2979 944963 : r.flush_denormals_to_zero ();
2980 944963 : return true;
2981 : }
2982 :
2983 : // Implement fold for a cast from float to another float.
2984 : bool
2985 62067 : operator_cast::op1_range (frange &r, tree type, const frange &lhs,
2986 : const frange &op2, relation_trio) const
2987 : {
2988 62067 : if (lhs.undefined_p ())
2989 : return false;
2990 62067 : tree lhs_type = lhs.type ();
2991 62067 : enum machine_mode mode = TYPE_MODE (type);
2992 62067 : enum machine_mode lhs_mode = TYPE_MODE (lhs_type);
2993 62067 : frange wlhs;
2994 62067 : bool rm;
2995 62067 : if (REAL_MODE_FORMAT (mode)->ieee_bits
2996 56495 : && REAL_MODE_FORMAT (lhs_mode)->ieee_bits
2997 50949 : && (REAL_MODE_FORMAT (lhs_mode)->ieee_bits
2998 : >= REAL_MODE_FORMAT (mode)->ieee_bits)
2999 83878 : && pow2p_hwi (REAL_MODE_FORMAT (mode)->ieee_bits))
3000 : {
3001 : /* If the cast is widening from IEEE exchange mode to
3002 : wider exchange mode or extended mode, no need to extend
3003 : the range on reverse operation. */
3004 21811 : rm = false;
3005 21811 : wlhs = lhs;
3006 : }
3007 : else
3008 : {
3009 40256 : rm = true;
3010 40256 : wlhs = lhs;
3011 40256 : wlhs.widen (lhs_type);
3012 : }
3013 62067 : auto save_flag_rounding_math = flag_rounding_math;
3014 62067 : flag_rounding_math = rm;
3015 62067 : bool ret = float_binary_op_range_finish (fold_range (r, type, wlhs, op2),
3016 : r, type, lhs);
3017 62067 : flag_rounding_math = save_flag_rounding_math;
3018 62067 : return ret;
3019 62067 : }
3020 :
3021 : // Implement fold for a cast from float to an int.
3022 : bool
3023 218708 : operator_cast::fold_range (irange &r, tree type, const frange &op1,
3024 : const irange &, relation_trio) const
3025 : {
3026 218708 : if (empty_range_varying (r, type, op1, op1))
3027 824 : return true;
3028 217884 : if (op1.maybe_isnan () || op1.maybe_isinf ())
3029 : {
3030 186698 : r.set_varying (type);
3031 186698 : return true;
3032 : }
3033 31186 : REAL_VALUE_TYPE l, u;
3034 31186 : l = real_value_from_int_cst (NULL_TREE, TYPE_MIN_VALUE (type));
3035 31186 : u = real_value_from_int_cst (NULL_TREE, TYPE_MAX_VALUE (type));
3036 :
3037 31186 : r.set_undefined ();
3038 73233 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
3039 : {
3040 31899 : REAL_VALUE_TYPE lb, ub;
3041 31899 : real_trunc (&lb, VOIDmode, &op1.lower_bound (i));
3042 31899 : real_trunc (&ub, VOIDmode, &op1.upper_bound (i));
3043 31899 : if (real_less (&lb, &l))
3044 : {
3045 11903 : r.set_varying (type);
3046 21038 : return true;
3047 : }
3048 19996 : if (real_less (&u, &ub))
3049 : {
3050 9135 : r.set_varying (type);
3051 9135 : return true;
3052 : }
3053 10861 : bool fail = false;
3054 10861 : wide_int wlb = real_to_integer (&lb, &fail, TYPE_PRECISION (type));
3055 10861 : wide_int wub = real_to_integer (&ub, &fail, TYPE_PRECISION (type));
3056 10861 : if (fail)
3057 : {
3058 0 : r.set_varying (type);
3059 0 : return true;
3060 : }
3061 10861 : int_range<2> tmp (type, wlb, wub);
3062 10861 : r.union_ (tmp);
3063 10861 : }
3064 : return true;
3065 : }
3066 :
3067 : // Implement op1_range for a cast from float to an int.
3068 : bool
3069 10231 : operator_cast::op1_range (frange &r, tree type, const irange &lhs,
3070 : const frange &, relation_trio) const
3071 : {
3072 10231 : if (lhs.undefined_p ())
3073 : return false;
3074 10231 : REAL_VALUE_TYPE lb, lbo, ub, ubo;
3075 10231 : wide_int lhs_lb = lhs.lower_bound ();
3076 10231 : wide_int lhs_ub = lhs.upper_bound ();
3077 10231 : tree lhs_type = lhs.type ();
3078 10231 : enum machine_mode mode = TYPE_MODE (type);
3079 10231 : real_from_integer (&lbo, VOIDmode, lhs_lb, TYPE_SIGN (lhs_type));
3080 10231 : real_from_integer (&ubo, VOIDmode, lhs_ub, TYPE_SIGN (lhs_type));
3081 10231 : real_convert (&lb, mode, &lbo);
3082 10231 : real_convert (&ub, mode, &ubo);
3083 10231 : if (real_identical (&lb, &lbo))
3084 : {
3085 : /* If low bound is exactly representable in type,
3086 : use nextafter (lb - 1., +inf). */
3087 9997 : real_arithmetic (&lb, PLUS_EXPR, &lbo, &dconstm1);
3088 9997 : real_convert (&lb, mode, &lb);
3089 9997 : if (!real_identical (&lb, &lbo))
3090 8551 : frange_nextafter (mode, lb, dconstinf);
3091 9997 : if (real_identical (&lb, &lbo))
3092 1448 : frange_nextafter (mode, lb, dconstninf);
3093 : }
3094 234 : else if (real_less (&lbo, &lb))
3095 9 : frange_nextafter (mode, lb, dconstninf);
3096 10231 : if (real_identical (&ub, &ubo))
3097 : {
3098 : /* If upper bound is exactly representable in type,
3099 : use nextafter (ub + 1., -inf). */
3100 6436 : real_arithmetic (&ub, PLUS_EXPR, &ubo, &dconst1);
3101 6436 : real_convert (&ub, mode, &ub);
3102 6436 : if (!real_identical (&ub, &ubo))
3103 6282 : frange_nextafter (mode, ub, dconstninf);
3104 6436 : if (real_identical (&ub, &ubo))
3105 154 : frange_nextafter (mode, ub, dconstinf);
3106 : }
3107 3795 : else if (real_less (&ub, &ubo))
3108 0 : frange_nextafter (mode, ub, dconstinf);
3109 10231 : r.set (type, lb, ub, nan_state (false));
3110 10231 : return true;
3111 10231 : }
3112 :
3113 : // Implement fold for a cast from int to a float.
3114 : bool
3115 878743 : operator_cast::fold_range (frange &r, tree type, const irange &op1,
3116 : const frange &, relation_trio) const
3117 : {
3118 878743 : if (empty_range_varying (r, type, op1, op1))
3119 1491 : return true;
3120 877252 : tree op1_type = op1.type ();
3121 877252 : r.set_undefined ();
3122 2704205 : for (unsigned i = 0; i < op1.num_pairs (); ++i)
3123 : {
3124 949701 : REAL_VALUE_TYPE lb, ub;
3125 949701 : wide_int op1_lb = op1.lower_bound (i);
3126 949701 : wide_int op1_ub = op1.upper_bound (i);
3127 949701 : enum machine_mode mode = flag_rounding_math ? VOIDmode : TYPE_MODE (type);
3128 949701 : real_from_integer (&lb, mode, op1_lb, TYPE_SIGN (op1_type));
3129 949701 : real_from_integer (&ub, mode, op1_ub, TYPE_SIGN (op1_type));
3130 949701 : if (flag_rounding_math)
3131 : {
3132 1390 : REAL_VALUE_TYPE lbo = lb, ubo = ub;
3133 1390 : mode = TYPE_MODE (type);
3134 1390 : real_convert (&lb, mode, &lb);
3135 1390 : real_convert (&ub, mode, &ub);
3136 1390 : if (real_less (&lbo, &lb))
3137 349 : frange_nextafter (mode, lb, dconstninf);
3138 1390 : if (real_less (&ub, &ubo))
3139 186 : frange_nextafter (mode, ub, dconstinf);
3140 : }
3141 949701 : frange tmp;
3142 949701 : tmp.set (type, lb, ub, nan_state (false));
3143 949701 : r.union_ (tmp);
3144 949731 : }
3145 877252 : if (r.undefined_p ())
3146 0 : r.set_varying (type);
3147 : return true;
3148 : }
3149 :
3150 : // Implement op1_range for a cast from int to a float.
3151 : bool
3152 263489 : operator_cast::op1_range (irange &r, tree type, const frange &lhs,
3153 : const irange &, relation_trio) const
3154 : {
3155 263489 : if (lhs.undefined_p ())
3156 : return false;
3157 263489 : if (lhs.known_isnan ())
3158 : {
3159 0 : r.set_varying (type);
3160 0 : return true;
3161 : }
3162 263489 : REAL_VALUE_TYPE lb = lhs.lower_bound ();
3163 263489 : REAL_VALUE_TYPE ub = lhs.upper_bound ();
3164 263489 : enum machine_mode mode = TYPE_MODE (lhs.type ());
3165 263489 : frange_nextafter (mode, lb, dconstninf);
3166 263489 : frange_nextafter (mode, ub, dconstinf);
3167 263489 : if (flag_rounding_math)
3168 : {
3169 37 : real_floor (&lb, mode, &lb);
3170 37 : real_ceil (&ub, mode, &ub);
3171 : }
3172 : else
3173 : {
3174 263452 : real_trunc (&lb, mode, &lb);
3175 263452 : real_trunc (&ub, mode, &ub);
3176 : }
3177 263489 : REAL_VALUE_TYPE l, u;
3178 263489 : wide_int wlb, wub;
3179 263489 : l = real_value_from_int_cst (NULL_TREE, TYPE_MIN_VALUE (type));
3180 263489 : if (real_less (&lb, &l))
3181 31591 : wlb = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3182 : else
3183 : {
3184 231898 : bool fail = false;
3185 231898 : wlb = real_to_integer (&lb, &fail, TYPE_PRECISION (type));
3186 231898 : if (fail)
3187 0 : wlb = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3188 : }
3189 263489 : u = real_value_from_int_cst (NULL_TREE, TYPE_MAX_VALUE (type));
3190 263489 : if (real_less (&u, &ub))
3191 50829 : wub = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3192 : else
3193 : {
3194 212660 : bool fail = false;
3195 212660 : wub = real_to_integer (&ub, &fail, TYPE_PRECISION (type));
3196 212660 : if (fail)
3197 0 : wub = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
3198 : }
3199 263489 : r.set (type, wlb, wub);
3200 263489 : return true;
3201 263489 : }
3202 :
3203 : // Initialize any float operators to the primary table
3204 :
3205 : void
3206 296010 : range_op_table::initialize_float_ops ()
3207 : {
3208 296010 : set (UNLE_EXPR, fop_unordered_le);
3209 296010 : set (UNLT_EXPR, fop_unordered_lt);
3210 296010 : set (UNGE_EXPR, fop_unordered_ge);
3211 296010 : set (UNGT_EXPR, fop_unordered_gt);
3212 296010 : set (UNEQ_EXPR, fop_unordered_equal);
3213 296010 : set (ORDERED_EXPR, fop_ordered);
3214 296010 : set (UNORDERED_EXPR, fop_unordered);
3215 296010 : set (LTGT_EXPR, fop_ltgt);
3216 296010 : set (RDIV_EXPR, fop_div);
3217 296010 : }
3218 :
3219 : #if CHECKING_P
3220 : #include "selftest.h"
3221 :
3222 : namespace selftest
3223 : {
3224 :
3225 : // Build an frange from string endpoints.
3226 :
3227 : static inline frange
3228 84 : frange_float (const char *lb, const char *ub, tree type = float_type_node)
3229 : {
3230 84 : REAL_VALUE_TYPE min, max;
3231 84 : gcc_assert (real_from_string (&min, lb) == 0);
3232 84 : gcc_assert (real_from_string (&max, ub) == 0);
3233 84 : return frange (type, min, max);
3234 : }
3235 :
3236 : void
3237 4 : range_op_float_tests ()
3238 : {
3239 4 : frange r, r0, r1;
3240 4 : frange trange (float_type_node);
3241 :
3242 : // negate([-5, +10]) => [-10, 5]
3243 4 : r0 = frange_float ("-5", "10");
3244 4 : range_op_handler (NEGATE_EXPR).fold_range (r, float_type_node, r0, trange);
3245 4 : ASSERT_EQ (r, frange_float ("-10", "5"));
3246 :
3247 : // negate([0, 1] -NAN) => [-1, -0] +NAN
3248 4 : r0 = frange_float ("0", "1");
3249 4 : r0.update_nan (true);
3250 4 : range_op_handler (NEGATE_EXPR).fold_range (r, float_type_node, r0, trange);
3251 4 : r1 = frange_float ("-1", "-0");
3252 4 : r1.update_nan (false);
3253 4 : ASSERT_EQ (r, r1);
3254 :
3255 : // [-INF,+INF] + [-INF,+INF] could be a NAN.
3256 4 : range_op_handler plus (PLUS_EXPR);
3257 4 : r0.set_varying (float_type_node);
3258 4 : r1.set_varying (float_type_node);
3259 4 : r0.clear_nan ();
3260 4 : r1.clear_nan ();
3261 4 : plus.fold_range (r, float_type_node, r0, r1);
3262 4 : if (HONOR_NANS (float_type_node))
3263 8 : ASSERT_TRUE (r.maybe_isnan ());
3264 :
3265 : // r.widen widens each sub-range and keeps the gap between
3266 : // them.
3267 4 : r0 = frange_float ("1.0", "2.0");
3268 4 : r1 = frange_float ("10.0", "11.0");
3269 4 : r0.union_ (r1);
3270 4 : r0.clear_nan ();
3271 4 : ASSERT_EQ (r0.num_pairs (), 2);
3272 4 : r = r0;
3273 4 : r.widen (float_type_node);
3274 4 : ASSERT_EQ (r.num_pairs (), 2);
3275 4 : REAL_VALUE_TYPE five;
3276 4 : real_from_string (&five, "5.0");
3277 4 : ASSERT_FALSE (r.contains_p (five));
3278 :
3279 : // op1_range for "op1 == op2" where op2 = [-1.0,-0.0][1.0,1.0] holds -0.0 but
3280 : // not +0.0 must still admit +0.0 for op1, since -0.0 == +0.0.
3281 4 : if (HONOR_SIGNED_ZEROS (float_type_node))
3282 : {
3283 4 : r0 = frange_float ("-1.0", "-0.0");
3284 4 : r1 = frange_float ("1.0", "1.0");
3285 4 : r0.union_ (r1);
3286 4 : r0.clear_nan ();
3287 4 : ASSERT_FALSE (r0.contains_p (dconst0));
3288 4 : ASSERT_TRUE (r0.contains_p (dconstm0));
3289 4 : int_range<2> bool_true = range_true ();
3290 4 : range_op_handler (EQ_EXPR).op1_range (r, float_type_node, bool_true, r0);
3291 4 : ASSERT_TRUE (r.contains_p (dconst0));
3292 4 : }
3293 :
3294 : // negate([1, 2] U [10, 11]) => [-11, -10] U [-2, -1], keeping the gap.
3295 4 : r0 = frange_float ("1.0", "2.0");
3296 4 : r1 = frange_float ("10.0", "11.0");
3297 4 : r0.union_ (r1);
3298 4 : r0.clear_nan ();
3299 4 : range_op_handler (NEGATE_EXPR).fold_range (r, float_type_node, r0, trange);
3300 4 : ASSERT_EQ (r.num_pairs (), 2);
3301 :
3302 : // abs([-6, -5] U [1, 2]) => [1, 2] U [5, 6], keeping the gap rather than
3303 : // collapsing to [0, 6].
3304 4 : r0 = frange_float ("-6.0", "-5.0");
3305 4 : r1 = frange_float ("1.0", "2.0");
3306 4 : r0.union_ (r1);
3307 4 : r0.clear_nan ();
3308 4 : range_op_handler (ABS_EXPR).fold_range (r, float_type_node, r0, trange);
3309 4 : ASSERT_EQ (r.num_pairs (), 2);
3310 :
3311 : // (float)([1, 4] U [6, 10]) keeps the gap.
3312 4 : unsigned iprec = TYPE_PRECISION (integer_type_node);
3313 4 : int_range<2> i0 (integer_type_node,
3314 4 : wi::shwi (1, iprec), wi::shwi (4, iprec));
3315 4 : int_range<2> i1 (integer_type_node,
3316 4 : wi::shwi (6, iprec), wi::shwi (10, iprec));
3317 4 : i0.union_ (i1);
3318 4 : range_op_handler (FLOAT_EXPR).fold_range (r, float_type_node, i0, trange);
3319 4 : ASSERT_EQ (r.num_pairs (), 2);
3320 :
3321 : // Casting the double range [1,2] U [10,11] to float stays two pieces rather
3322 : // than collapsing to the hull [1, 11].
3323 4 : r0 = frange_float ("1.0", "2.0", double_type_node);
3324 4 : r1 = frange_float ("10.0", "11.0", double_type_node);
3325 4 : r0.union_ (r1);
3326 4 : r0.clear_nan ();
3327 4 : range_op_handler (CONVERT_EXPR).fold_range (r, float_type_node, r0, r0);
3328 4 : ASSERT_EQ (r.num_pairs (), 2);
3329 :
3330 : // Cast conversion of (int)([1.0,2.0] U [10.0,11.0]) stays two pieces.
3331 4 : r0 = frange_float ("1.0", "2.0");
3332 4 : r1 = frange_float ("10.0", "11.0");
3333 4 : r0.union_ (r1);
3334 4 : r0.clear_nan ();
3335 4 : int_range<2> ir, ir_op2;
3336 4 : range_op_handler (FIX_TRUNC_EXPR).fold_range (ir, integer_type_node,
3337 : r0, ir_op2);
3338 4 : ASSERT_EQ (ir.num_pairs (), 2);
3339 :
3340 : // ([1, 2] U [10, 11]) + 0 stays two pieces.
3341 4 : r0 = frange_float ("1.0", "2.0");
3342 4 : r1 = frange_float ("10.0", "11.0");
3343 4 : r0.union_ (r1);
3344 4 : r0.clear_nan ();
3345 4 : r1 = frange_float ("0.0", "0.0");
3346 4 : r1.clear_nan ();
3347 4 : range_op_handler (PLUS_EXPR).fold_range (r, float_type_node, r0, r1);
3348 4 : ASSERT_EQ (r.num_pairs (), 2);
3349 :
3350 : // x * x (VREL_EQ) with a two-piece x: the diagonal fold gives the exact
3351 : // [4, 9], not the full cross product's [-9, -4] U [4, 9], nor the hull's
3352 : // looser [0, 9].
3353 4 : r0 = frange_float ("-3.0", "-2.0");
3354 4 : r1 = frange_float ("2.0", "3.0");
3355 4 : r0.union_ (r1);
3356 4 : r0.clear_nan ();
3357 4 : range_op_handler (MULT_EXPR).fold_range (r, float_type_node, r0, r0,
3358 : relation_trio (VREL_VARYING,
3359 : VREL_VARYING,
3360 4 : VREL_EQ));
3361 4 : REAL_VALUE_TYPE val;
3362 4 : real_from_string (&val, "5.0");
3363 4 : ASSERT_TRUE (r.contains_p (val)); // the result is [4, 9]
3364 4 : real_from_string (&val, "-5.0");
3365 4 : ASSERT_FALSE (r.contains_p (val)); // not the cross product's negatives
3366 4 : real_from_string (&val, "3.0");
3367 4 : ASSERT_FALSE (r.contains_p (val)); // tighter than the hull [0, 9]
3368 4 : }
3369 :
3370 : } // namespace selftest
3371 :
3372 : #endif // CHECKING_P
|