aix: Fix building fat library for AIX
[official-gcc.git] / gcc / gimple-range-op.cc
blob55dfbb23ce22c80e79626d63257a9f675f6f1e01
1 /* Code for GIMPLE range op related routines.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "insn-codes.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "optabs-tree.h"
32 #include "gimple-iterator.h"
33 #include "gimple-fold.h"
34 #include "wide-int.h"
35 #include "fold-const.h"
36 #include "case-cfn-macros.h"
37 #include "omp-general.h"
38 #include "cfgloop.h"
39 #include "tree-ssa-loop.h"
40 #include "tree-scalar-evolution.h"
41 #include "langhooks.h"
42 #include "vr-values.h"
43 #include "range.h"
44 #include "value-query.h"
45 #include "gimple-range.h"
46 #include "attr-fnspec.h"
47 #include "realmpfr.h"
49 // Given stmt S, fill VEC, up to VEC_SIZE elements, with relevant ssa-names
50 // on the statement. For efficiency, it is an error to not pass in enough
51 // elements for the vector. Return the number of ssa-names.
53 unsigned
54 gimple_range_ssa_names (tree *vec, unsigned vec_size, gimple *stmt)
56 tree ssa;
57 int count = 0;
59 gimple_range_op_handler handler (stmt);
60 if (handler)
62 gcc_checking_assert (vec_size >= 2);
63 if ((ssa = gimple_range_ssa_p (handler.operand1 ())))
64 vec[count++] = ssa;
65 if ((ssa = gimple_range_ssa_p (handler.operand2 ())))
66 vec[count++] = ssa;
68 else if (is_a<gassign *> (stmt)
69 && gimple_assign_rhs_code (stmt) == COND_EXPR)
71 gcc_checking_assert (vec_size >= 3);
72 gassign *st = as_a<gassign *> (stmt);
73 if ((ssa = gimple_range_ssa_p (gimple_assign_rhs1 (st))))
74 vec[count++] = ssa;
75 if ((ssa = gimple_range_ssa_p (gimple_assign_rhs2 (st))))
76 vec[count++] = ssa;
77 if ((ssa = gimple_range_ssa_p (gimple_assign_rhs3 (st))))
78 vec[count++] = ssa;
80 return count;
83 // Return the base of the RHS of an assignment.
85 static tree
86 gimple_range_base_of_assignment (const gimple *stmt)
88 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
89 tree op1 = gimple_assign_rhs1 (stmt);
90 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
91 return get_base_address (TREE_OPERAND (op1, 0));
92 return op1;
95 // If statement is supported by range-ops, set the CODE and return the TYPE.
97 static inline enum tree_code
98 get_code (gimple *s)
100 if (const gassign *ass = dyn_cast<const gassign *> (s))
101 return gimple_assign_rhs_code (ass);
102 if (const gcond *cond = dyn_cast<const gcond *> (s))
103 return gimple_cond_code (cond);
104 return ERROR_MARK;
107 // If statement S has a supported range_op handler return TRUE.
109 bool
110 gimple_range_op_handler::supported_p (gimple *s)
112 enum tree_code code = get_code (s);
113 if (range_op_handler (code))
114 return true;
115 if (is_a <gcall *> (s) && gimple_range_op_handler (s))
116 return true;
117 return false;
120 // Construct a handler object for statement S.
122 gimple_range_op_handler::gimple_range_op_handler (gimple *s)
124 range_op_handler oper (get_code (s));
125 m_stmt = s;
126 m_op1 = NULL_TREE;
127 m_op2 = NULL_TREE;
129 if (oper)
130 switch (gimple_code (m_stmt))
132 case GIMPLE_COND:
133 m_op1 = gimple_cond_lhs (m_stmt);
134 m_op2 = gimple_cond_rhs (m_stmt);
135 // Check that operands are supported types. One check is enough.
136 if (Value_Range::supports_type_p (TREE_TYPE (m_op1)))
137 m_operator = oper.range_op ();
138 gcc_checking_assert (m_operator);
139 return;
140 case GIMPLE_ASSIGN:
141 m_op1 = gimple_range_base_of_assignment (m_stmt);
142 if (m_op1 && TREE_CODE (m_op1) == MEM_REF)
144 // If the base address is an SSA_NAME, we return it
145 // here. This allows processing of the range of that
146 // name, while the rest of the expression is simply
147 // ignored. The code in range_ops will see the
148 // ADDR_EXPR and do the right thing.
149 tree ssa = TREE_OPERAND (m_op1, 0);
150 if (TREE_CODE (ssa) == SSA_NAME)
151 m_op1 = ssa;
153 if (gimple_num_ops (m_stmt) >= 3)
154 m_op2 = gimple_assign_rhs2 (m_stmt);
155 // Check that operands are supported types. One check is enough.
156 if ((m_op1 && !Value_Range::supports_type_p (TREE_TYPE (m_op1))))
157 return;
158 m_operator = oper.range_op ();
159 gcc_checking_assert (m_operator);
160 return;
161 default:
162 gcc_unreachable ();
163 return;
165 // If no range-op table entry handled this stmt, check for other supported
166 // statements.
167 if (is_a <gcall *> (m_stmt))
168 maybe_builtin_call ();
169 else
170 maybe_non_standard ();
171 gcc_checking_assert (m_operator);
174 // Calculate what we can determine of the range of this unary
175 // statement's operand if the lhs of the expression has the range
176 // LHS_RANGE. Return false if nothing can be determined.
178 bool
179 gimple_range_op_handler::calc_op1 (vrange &r, const vrange &lhs_range)
181 // Give up on empty ranges.
182 if (lhs_range.undefined_p ())
183 return false;
185 // Unary operations require the type of the first operand in the
186 // second range position.
187 tree type = TREE_TYPE (operand1 ());
188 Value_Range type_range (type);
189 type_range.set_varying (type);
190 return op1_range (r, type, lhs_range, type_range);
193 // Calculate what we can determine of the range of this statement's
194 // first operand if the lhs of the expression has the range LHS_RANGE
195 // and the second operand has the range OP2_RANGE. Return false if
196 // nothing can be determined.
198 bool
199 gimple_range_op_handler::calc_op1 (vrange &r, const vrange &lhs_range,
200 const vrange &op2_range, relation_trio k)
202 // Give up on empty ranges.
203 if (lhs_range.undefined_p ())
204 return false;
206 // Unary operation are allowed to pass a range in for second operand
207 // as there are often additional restrictions beyond the type which
208 // can be imposed. See operator_cast::op1_range().
209 tree type = TREE_TYPE (operand1 ());
210 // If op2 is undefined, solve as if it is varying.
211 if (op2_range.undefined_p ())
213 if (gimple_num_ops (m_stmt) < 3)
214 return false;
215 tree op2_type;
216 // This is sometimes invoked on single operand stmts.
217 if (operand2 ())
218 op2_type = TREE_TYPE (operand2 ());
219 else
220 op2_type = TREE_TYPE (operand1 ());
221 Value_Range trange (op2_type);
222 trange.set_varying (op2_type);
223 return op1_range (r, type, lhs_range, trange, k);
225 return op1_range (r, type, lhs_range, op2_range, k);
228 // Calculate what we can determine of the range of this statement's
229 // second operand if the lhs of the expression has the range LHS_RANGE
230 // and the first operand has the range OP1_RANGE. Return false if
231 // nothing can be determined.
233 bool
234 gimple_range_op_handler::calc_op2 (vrange &r, const vrange &lhs_range,
235 const vrange &op1_range, relation_trio k)
237 // Give up on empty ranges.
238 if (lhs_range.undefined_p ())
239 return false;
241 tree type = TREE_TYPE (operand2 ());
242 // If op1 is undefined, solve as if it is varying.
243 if (op1_range.undefined_p ())
245 tree op1_type = TREE_TYPE (operand1 ());
246 Value_Range trange (op1_type);
247 trange.set_varying (op1_type);
248 return op2_range (r, type, lhs_range, trange, k);
250 return op2_range (r, type, lhs_range, op1_range, k);
253 // --------------------------------------------------------------------
255 // Implement range operator for float CFN_BUILT_IN_CONSTANT_P.
256 class cfn_constant_float_p : public range_operator
258 public:
259 using range_operator::fold_range;
260 virtual bool fold_range (irange &r, tree type, const frange &lh,
261 const irange &, relation_trio) const
263 if (lh.singleton_p ())
265 wide_int one = wi::one (TYPE_PRECISION (type));
266 r.set (type, one, one);
267 return true;
269 if (cfun->after_inlining)
271 r.set_zero (type);
272 return true;
274 return false;
276 } op_cfn_constant_float_p;
278 // Implement range operator for integral CFN_BUILT_IN_CONSTANT_P.
279 class cfn_constant_p : public range_operator
281 public:
282 using range_operator::fold_range;
283 virtual bool fold_range (irange &r, tree type, const irange &lh,
284 const irange &, relation_trio) const
286 if (lh.singleton_p ())
288 wide_int one = wi::one (TYPE_PRECISION (type));
289 r.set (type, one, one);
290 return true;
292 if (cfun->after_inlining)
294 r.set_zero (type);
295 return true;
297 return false;
299 } op_cfn_constant_p;
301 // Implement range operator for integral/pointer functions returning
302 // the first argument.
303 class cfn_pass_through_arg1 : public range_operator
305 public:
306 using range_operator::fold_range;
307 using range_operator::op1_range;
308 virtual bool fold_range (irange &r, tree, const irange &lh,
309 const irange &, relation_trio) const
311 r = lh;
312 return true;
314 virtual bool fold_range (prange &r, tree, const prange &lh,
315 const prange &, relation_trio) const
317 r = lh;
318 return true;
320 virtual bool op1_range (irange &r, tree, const irange &lhs,
321 const irange &, relation_trio) const
323 r = lhs;
324 return true;
326 virtual bool op1_range (prange &r, tree, const prange &lhs,
327 const prange &, relation_trio) const
329 r = lhs;
330 return true;
332 virtual bool pointers_handled_p (range_op_dispatch_type type,
333 unsigned dispatch) const
335 switch (type)
337 case DISPATCH_FOLD_RANGE:
338 return dispatch == RO_PPP;
339 case DISPATCH_OP1_RANGE:
340 return dispatch == RO_PPP;
341 default:
342 return true;
345 } op_cfn_pass_through_arg1;
347 // Implement range operator for CFN_BUILT_IN_SIGNBIT.
348 class cfn_signbit : public range_operator
350 public:
351 using range_operator::fold_range;
352 using range_operator::op1_range;
353 virtual bool fold_range (irange &r, tree type, const frange &lh,
354 const irange &, relation_trio) const override
356 bool signbit;
357 if (lh.signbit_p (signbit))
359 if (signbit)
360 r.set_nonzero (type);
361 else
362 r.set_zero (type);
363 return true;
365 return false;
367 virtual bool op1_range (frange &r, tree type, const irange &lhs,
368 const frange &, relation_trio) const override
370 if (lhs.zero_p ())
372 r.set (type, dconst0, frange_val_max (type));
373 r.update_nan (false);
374 return true;
376 if (!lhs.contains_p (wi::zero (TYPE_PRECISION (lhs.type ()))))
378 r.set (type, frange_val_min (type), dconstm0);
379 r.update_nan (true);
380 return true;
382 return false;
384 } op_cfn_signbit;
386 // Implement range operator for CFN_BUILT_IN_COPYSIGN
387 class cfn_copysign : public range_operator
389 public:
390 using range_operator::fold_range;
391 virtual bool fold_range (frange &r, tree type, const frange &lh,
392 const frange &rh, relation_trio) const override
394 frange neg;
395 if (!range_op_handler (ABS_EXPR).fold_range (r, type, lh, frange (type)))
396 return false;
397 if (!range_op_handler (NEGATE_EXPR).fold_range (neg, type, r,
398 frange (type)))
399 return false;
401 bool signbit;
402 if (rh.signbit_p (signbit))
404 // If the sign is negative, flip the result from ABS,
405 // otherwise leave things positive.
406 if (signbit)
407 r = neg;
409 else
410 // If the sign is unknown, keep the positive and negative
411 // alternatives.
412 r.union_ (neg);
413 return true;
415 } op_cfn_copysign;
417 /* Compute FUNC (ARG) where FUNC is a mpfr function. If RES_LOW is non-NULL,
418 set it to low bound of possible range if the function is expected to have
419 ULPS precision and similarly if RES_HIGH is non-NULL, set it to high bound.
420 If the function returns false, the results weren't set. */
422 static bool
423 frange_mpfr_arg1 (REAL_VALUE_TYPE *res_low, REAL_VALUE_TYPE *res_high,
424 int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_rnd_t),
425 const REAL_VALUE_TYPE &arg, tree type, unsigned ulps)
427 if (ulps == ~0U || !real_isfinite (&arg))
428 return false;
429 machine_mode mode = TYPE_MODE (type);
430 const real_format *format = REAL_MODE_FORMAT (mode);
431 auto_mpfr m (format->p);
432 mpfr_from_real (m, &arg, MPFR_RNDN);
433 mpfr_clear_flags ();
434 bool inexact = func (m, m, MPFR_RNDN);
435 if (!mpfr_number_p (m) || mpfr_overflow_p () || mpfr_underflow_p ())
436 return false;
438 REAL_VALUE_TYPE value, result;
439 real_from_mpfr (&value, m, format, MPFR_RNDN);
440 if (!real_isfinite (&value))
441 return false;
442 if ((value.cl == rvc_zero) != (mpfr_zero_p (m) != 0))
443 inexact = true;
445 real_convert (&result, format, &value);
446 if (!real_isfinite (&result))
447 return false;
448 bool round_low = false;
449 bool round_high = false;
450 if (!ulps && flag_rounding_math)
451 ++ulps;
452 if (inexact || !real_identical (&result, &value))
454 if (MODE_COMPOSITE_P (mode))
455 round_low = round_high = true;
456 else
458 round_low = !real_less (&result, &value);
459 round_high = !real_less (&value, &result);
462 if (res_low)
464 *res_low = result;
465 for (unsigned int i = 0; i < ulps + round_low; ++i)
466 frange_nextafter (mode, *res_low, dconstninf);
468 if (res_high)
470 *res_high = result;
471 for (unsigned int i = 0; i < ulps + round_high; ++i)
472 frange_nextafter (mode, *res_high, dconstinf);
474 return true;
477 class cfn_sqrt : public range_operator
479 public:
480 using range_operator::fold_range;
481 using range_operator::op1_range;
482 virtual bool fold_range (frange &r, tree type,
483 const frange &lh, const frange &,
484 relation_trio) const final override
486 if (lh.undefined_p ())
487 return false;
488 if (lh.known_isnan () || real_less (&lh.upper_bound (), &dconstm0))
490 r.set_nan (type);
491 return true;
493 unsigned bulps
494 = targetm.libm_function_max_error (CFN_SQRT, TYPE_MODE (type), true);
495 if (bulps == ~0U)
496 r.set_varying (type);
497 else if (bulps == 0)
498 r.set (type, dconstm0, dconstinf);
499 else
501 REAL_VALUE_TYPE boundmin = dconstm0;
502 while (bulps--)
503 frange_nextafter (TYPE_MODE (type), boundmin, dconstninf);
504 r.set (type, boundmin, dconstinf);
506 if (!lh.maybe_isnan () && !real_less (&lh.lower_bound (), &dconst0))
507 r.clear_nan ();
509 unsigned ulps
510 = targetm.libm_function_max_error (CFN_SQRT, TYPE_MODE (type), false);
511 if (ulps == ~0U)
512 return true;
513 REAL_VALUE_TYPE lb = lh.lower_bound ();
514 REAL_VALUE_TYPE ub = lh.upper_bound ();
515 if (!frange_mpfr_arg1 (&lb, NULL, mpfr_sqrt, lb, type, ulps))
516 lb = dconstninf;
517 if (!frange_mpfr_arg1 (NULL, &ub, mpfr_sqrt, ub, type, ulps))
518 ub = dconstinf;
519 frange r2;
520 r2.set (type, lb, ub);
521 r2.flush_denormals_to_zero ();
522 r.intersect (r2);
523 return true;
525 virtual bool op1_range (frange &r, tree type,
526 const frange &lhs, const frange &,
527 relation_trio) const final override
529 if (lhs.undefined_p ())
530 return false;
532 // A known NAN means the input is [-INF,-0.) U +-NAN.
533 if (lhs.known_isnan ())
535 known_nan:
536 REAL_VALUE_TYPE ub = dconstm0;
537 frange_nextafter (TYPE_MODE (type), ub, dconstninf);
538 r.set (type, dconstninf, ub);
539 // No r.flush_denormals_to_zero (); here - it is a reverse op.
540 return true;
543 // Results outside of [-0.0, +Inf] are impossible.
544 unsigned bulps
545 = targetm.libm_function_max_error (CFN_SQRT, TYPE_MODE (type), true);
546 if (bulps != ~0U)
548 const REAL_VALUE_TYPE &ub = lhs.upper_bound ();
549 REAL_VALUE_TYPE m0 = dconstm0;
550 while (bulps--)
551 frange_nextafter (TYPE_MODE (type), m0, dconstninf);
552 if (real_less (&ub, &m0))
554 if (!lhs.maybe_isnan ())
555 r.set_undefined ();
556 else
557 // If lhs could be NAN and finite result is impossible,
558 // the range is like lhs.known_isnan () above.
559 goto known_nan;
560 return true;
564 if (!lhs.maybe_isnan ())
565 // If NAN is not valid result, the input cannot include either
566 // a NAN nor values smaller than -0.
567 r.set (type, dconstm0, dconstinf, nan_state (false, false));
568 else
569 r.set_varying (type);
571 unsigned ulps
572 = targetm.libm_function_max_error (CFN_SQRT, TYPE_MODE (type), false);
573 if (ulps == ~0U)
574 return true;
575 REAL_VALUE_TYPE lb = lhs.lower_bound ();
576 REAL_VALUE_TYPE ub = lhs.upper_bound ();
577 if (!lhs.maybe_isnan () && real_less (&dconst0, &lb))
579 for (unsigned i = 0; i < ulps; ++i)
580 frange_nextafter (TYPE_MODE (type), lb, dconstninf);
581 if (real_less (&dconst0, &lb))
583 REAL_VALUE_TYPE op = lb;
584 frange_arithmetic (MULT_EXPR, type, lb, op, op, dconstninf);
586 else
587 lb = dconstninf;
589 else
590 lb = dconstninf;
591 if (real_isfinite (&ub) && real_less (&dconst0, &ub))
593 for (unsigned i = 0; i < ulps; ++i)
594 frange_nextafter (TYPE_MODE (type), ub, dconstinf);
595 if (real_isfinite (&ub))
597 REAL_VALUE_TYPE op = ub;
598 frange_arithmetic (MULT_EXPR, type, ub, op, op, dconstinf);
600 else
601 ub = dconstinf;
603 else
604 ub = dconstinf;
605 frange r2;
606 r2.set (type, lb, ub);
607 r.intersect (r2);
608 return true;
610 } op_cfn_sqrt;
612 class cfn_sincos : public range_operator
614 public:
615 using range_operator::fold_range;
616 using range_operator::op1_range;
617 cfn_sincos (combined_fn cfn) { m_cfn = cfn; }
618 virtual bool fold_range (frange &r, tree type,
619 const frange &lh, const frange &,
620 relation_trio) const final override
622 if (lh.undefined_p ())
623 return false;
624 if (lh.known_isnan () || lh.known_isinf ())
626 r.set_nan (type);
627 return true;
629 unsigned bulps = targetm.libm_function_max_error (m_cfn, TYPE_MODE (type),
630 true);
631 if (bulps == ~0U)
632 r.set_varying (type);
633 else if (bulps == 0)
634 r.set (type, dconstm1, dconst1);
635 else
637 REAL_VALUE_TYPE boundmin, boundmax;
638 boundmax = dconst1;
639 while (bulps--)
640 frange_nextafter (TYPE_MODE (type), boundmax, dconstinf);
641 real_arithmetic (&boundmin, NEGATE_EXPR, &boundmax, NULL);
642 r.set (type, boundmin, boundmax);
644 if (!lh.maybe_isnan () && !lh.maybe_isinf ())
645 r.clear_nan ();
647 unsigned ulps
648 = targetm.libm_function_max_error (m_cfn, TYPE_MODE (type), false);
649 if (ulps == ~0U)
650 return true;
651 REAL_VALUE_TYPE lb = lh.lower_bound ();
652 REAL_VALUE_TYPE ub = lh.upper_bound ();
653 REAL_VALUE_TYPE diff;
654 real_arithmetic (&diff, MINUS_EXPR, &ub, &lb);
655 if (!real_isfinite (&diff))
656 return true;
657 REAL_VALUE_TYPE pi = dconst_pi ();
658 REAL_VALUE_TYPE pix2;
659 real_arithmetic (&pix2, PLUS_EXPR, &pi, &pi);
660 // We can only try to narrow the range further if ub-lb < 2*pi.
661 if (!real_less (&diff, &pix2))
662 return true;
663 REAL_VALUE_TYPE lb_lo, lb_hi, ub_lo, ub_hi;
664 REAL_VALUE_TYPE lb_deriv_lo, lb_deriv_hi, ub_deriv_lo, ub_deriv_hi;
665 if (!frange_mpfr_arg1 (&lb_lo, &lb_hi,
666 m_cfn == CFN_SIN ? mpfr_sin : mpfr_cos, lb,
667 type, ulps)
668 || !frange_mpfr_arg1 (&ub_lo, &ub_hi,
669 m_cfn == CFN_SIN ? mpfr_sin : mpfr_cos, ub,
670 type, ulps)
671 || !frange_mpfr_arg1 (&lb_deriv_lo, &lb_deriv_hi,
672 m_cfn == CFN_SIN ? mpfr_cos : mpfr_sin, lb,
673 type, 0)
674 || !frange_mpfr_arg1 (&ub_deriv_lo, &ub_deriv_hi,
675 m_cfn == CFN_SIN ? mpfr_cos : mpfr_sin, ub,
676 type, 0))
677 return true;
678 if (m_cfn == CFN_COS)
680 // Derivative of cos is -sin, so negate.
681 lb_deriv_lo.sign ^= 1;
682 lb_deriv_hi.sign ^= 1;
683 ub_deriv_lo.sign ^= 1;
684 ub_deriv_hi.sign ^= 1;
687 if (real_less (&lb_lo, &ub_lo))
688 lb = lb_lo;
689 else
690 lb = ub_lo;
691 if (real_less (&lb_hi, &ub_hi))
692 ub = ub_hi;
693 else
694 ub = lb_hi;
696 // The range between the function result on the boundaries may need
697 // to be extended to +1 (+Inf) or -1 (-Inf) or both depending on the
698 // derivative or length of the argument range (diff).
700 // First handle special case, where the derivative has different signs,
701 // so the bound must be roughly -1 or +1.
702 if (real_isneg (&lb_deriv_lo) != real_isneg (&lb_deriv_hi))
704 if (real_isneg (&lb_lo))
705 lb = dconstninf;
706 else
707 ub = dconstinf;
709 if (real_isneg (&ub_deriv_lo) != real_isneg (&ub_deriv_hi))
711 if (real_isneg (&ub_lo))
712 lb = dconstninf;
713 else
714 ub = dconstinf;
717 // If derivative at lower_bound and upper_bound have the same sign,
718 // the function grows or declines on the whole range if diff < pi, so
719 // [lb, ub] is correct, and if diff >= pi the result range must include
720 // both the minimum and maximum.
721 if (real_isneg (&lb_deriv_lo) == real_isneg (&ub_deriv_lo))
723 if (!real_less (&diff, &pi))
724 return true;
726 // If function declines at lower_bound and grows at upper_bound,
727 // the result range must include the minimum, so set lb to -Inf.
728 else if (real_isneg (&lb_deriv_lo))
729 lb = dconstninf;
730 // If function grows at lower_bound and declines at upper_bound,
731 // the result range must include the maximum, so set ub to +Inf.
732 else
733 ub = dconstinf;
734 frange r2;
735 r2.set (type, lb, ub);
736 r2.flush_denormals_to_zero ();
737 r.intersect (r2);
738 return true;
740 virtual bool op1_range (frange &r, tree type,
741 const frange &lhs, const frange &,
742 relation_trio) const final override
744 if (lhs.undefined_p ())
745 return false;
747 // A known NAN means the input is [-INF,-INF][+INF,+INF] U +-NAN,
748 // which we can't currently represent.
749 if (lhs.known_isnan ())
751 r.set_varying (type);
752 return true;
755 // Results outside of [-1.0, +1.0] are impossible.
756 unsigned bulps
757 = targetm.libm_function_max_error (m_cfn, TYPE_MODE (type), true);
758 if (bulps != ~0U)
760 const REAL_VALUE_TYPE &lb = lhs.lower_bound ();
761 const REAL_VALUE_TYPE &ub = lhs.upper_bound ();
762 REAL_VALUE_TYPE m1 = dconstm1;
763 REAL_VALUE_TYPE p1 = dconst1;
764 while (bulps--)
766 frange_nextafter (TYPE_MODE (type), m1, dconstninf);
767 frange_nextafter (TYPE_MODE (type), p1, dconstinf);
769 if (real_less (&ub, &m1) || real_less (&p1, &lb))
771 if (!lhs.maybe_isnan ())
772 r.set_undefined ();
773 else
774 /* If lhs could be NAN and finite result is impossible,
775 the range is like lhs.known_isnan () above,
776 [-INF,-INF][+INF,+INF] U +-NAN. */
777 r.set_varying (type);
778 return true;
782 if (!lhs.maybe_isnan ())
784 // If NAN is not valid result, the input cannot include either
785 // a NAN nor a +-INF.
786 REAL_VALUE_TYPE lb = real_min_representable (type);
787 REAL_VALUE_TYPE ub = real_max_representable (type);
788 r.set (type, lb, ub, nan_state (false, false));
790 else
791 r.set_varying (type);
792 return true;
794 private:
795 combined_fn m_cfn;
796 } op_cfn_sin (CFN_SIN), op_cfn_cos (CFN_COS);
798 // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER.
799 class cfn_toupper_tolower : public range_operator
801 public:
802 using range_operator::fold_range;
803 cfn_toupper_tolower (bool toupper) { m_toupper = toupper; }
804 virtual bool fold_range (irange &r, tree type, const irange &lh,
805 const irange &, relation_trio) const;
806 private:
807 bool get_letter_range (tree type, irange &lowers, irange &uppers) const;
808 bool m_toupper;
809 } op_cfn_toupper (true), op_cfn_tolower (false);
811 // Return TRUE if we recognize the target character set and return the
812 // range for lower case and upper case letters.
814 bool
815 cfn_toupper_tolower::get_letter_range (tree type, irange &lowers,
816 irange &uppers) const
818 // ASCII
819 int a = lang_hooks.to_target_charset ('a');
820 int z = lang_hooks.to_target_charset ('z');
821 int A = lang_hooks.to_target_charset ('A');
822 int Z = lang_hooks.to_target_charset ('Z');
824 if ((z - a == 25) && (Z - A == 25))
826 lowers = int_range<2> (type,
827 wi::shwi (a, TYPE_PRECISION (type)),
828 wi::shwi (z, TYPE_PRECISION (type)));
829 uppers = int_range<2> (type,
830 wi::shwi (A, TYPE_PRECISION (type)),
831 wi::shwi (Z, TYPE_PRECISION (type)));
832 return true;
834 // Unknown character set.
835 return false;
838 bool
839 cfn_toupper_tolower::fold_range (irange &r, tree type, const irange &lh,
840 const irange &, relation_trio) const
842 int_range<3> lowers;
843 int_range<3> uppers;
844 if (!get_letter_range (type, lowers, uppers))
845 return false;
847 r = lh;
848 if (m_toupper)
850 // Return the range passed in without any lower case characters,
851 // but including all the upper case ones.
852 lowers.invert ();
853 r.intersect (lowers);
854 r.union_ (uppers);
856 else
858 // Return the range passed in without any lower case characters,
859 // but including all the upper case ones.
860 uppers.invert ();
861 r.intersect (uppers);
862 r.union_ (lowers);
864 return true;
867 // Implement range operator for CFN_BUILT_IN_FFS.
868 class cfn_ffs : public range_operator
870 public:
871 using range_operator::fold_range;
872 virtual bool fold_range (irange &r, tree type, const irange &lh,
873 const irange &, relation_trio) const
875 if (lh.undefined_p ())
876 return false;
877 // __builtin_ffs* and __builtin_popcount* return [0, prec].
878 int prec = TYPE_PRECISION (lh.type ());
879 // If arg is non-zero, then ffs or popcount are non-zero.
880 int mini = range_includes_zero_p (lh) ? 0 : 1;
881 int maxi = prec;
883 // If some high bits are known to be zero, decrease the maximum.
884 int_range_max tmp = lh;
885 if (TYPE_SIGN (tmp.type ()) == SIGNED)
886 range_cast (tmp, unsigned_type_for (tmp.type ()));
887 wide_int max = tmp.upper_bound ();
888 maxi = wi::floor_log2 (max) + 1;
889 r.set (type,
890 wi::shwi (mini, TYPE_PRECISION (type)),
891 wi::shwi (maxi, TYPE_PRECISION (type)));
892 return true;
894 } op_cfn_ffs;
896 // Implement range operator for CFN_BUILT_IN_POPCOUNT.
897 class cfn_popcount : public cfn_ffs
899 public:
900 using range_operator::fold_range;
901 virtual bool fold_range (irange &r, tree type, const irange &lh,
902 const irange &rh, relation_trio rel) const
904 if (lh.undefined_p ())
905 return false;
906 unsigned prec = TYPE_PRECISION (type);
907 irange_bitmask bm = lh.get_bitmask ();
908 wide_int nz = bm.get_nonzero_bits ();
909 wide_int high = wi::shwi (wi::popcount (nz), prec);
910 // Calculating the popcount of a singleton is trivial.
911 if (lh.singleton_p ())
913 r.set (type, high, high);
914 return true;
916 if (cfn_ffs::fold_range (r, type, lh, rh, rel))
918 wide_int known_ones = ~bm.mask () & bm.value ();
919 wide_int low = wi::shwi (wi::popcount (known_ones), prec);
920 int_range<2> tmp (type, low, high);
921 r.intersect (tmp);
922 return true;
924 return false;
926 } op_cfn_popcount;
928 // Implement range operator for CFN_BUILT_IN_CLZ
929 class cfn_clz : public range_operator
931 public:
932 cfn_clz (bool internal) { m_gimple_call_internal_p = internal; }
933 using range_operator::fold_range;
934 virtual bool fold_range (irange &r, tree type, const irange &lh,
935 const irange &rh, relation_trio) const;
936 private:
937 bool m_gimple_call_internal_p;
938 } op_cfn_clz (false), op_cfn_clz_internal (true);
940 bool
941 cfn_clz::fold_range (irange &r, tree type, const irange &lh,
942 const irange &rh, relation_trio) const
944 // __builtin_c[lt]z* return [0, prec-1], except when the
945 // argument is 0, but that is undefined behavior.
947 // For __builtin_c[lt]z* consider argument of 0 always undefined
948 // behavior, for internal fns likewise, unless it has 2 arguments,
949 // then the second argument is the value at zero.
950 if (lh.undefined_p ())
951 return false;
952 int prec = TYPE_PRECISION (lh.type ());
953 int mini = 0;
954 int maxi = prec - 1;
955 if (m_gimple_call_internal_p)
957 // Only handle the single common value.
958 if (rh.lower_bound () == prec)
959 maxi = prec;
960 else
961 // Magic value to give up, unless we can prove arg is non-zero.
962 mini = -2;
965 // From clz of minimum we can compute result maximum.
966 if (wi::gt_p (lh.lower_bound (), 0, TYPE_SIGN (lh.type ())))
968 maxi = prec - 1 - wi::floor_log2 (lh.lower_bound ());
969 if (mini == -2)
970 mini = 0;
972 else if (!range_includes_zero_p (lh))
974 mini = 0;
975 maxi = prec - 1;
977 if (mini == -2)
978 return false;
979 // From clz of maximum we can compute result minimum.
980 wide_int max = lh.upper_bound ();
981 int newmini = prec - 1 - wi::floor_log2 (max);
982 if (max == 0)
984 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
985 // return [prec, prec], otherwise ignore the range.
986 if (maxi == prec)
987 mini = prec;
989 else
990 mini = newmini;
992 if (mini == -2)
993 return false;
994 r.set (type,
995 wi::shwi (mini, TYPE_PRECISION (type)),
996 wi::shwi (maxi, TYPE_PRECISION (type)));
997 return true;
1000 // Implement range operator for CFN_BUILT_IN_CTZ
1001 class cfn_ctz : public range_operator
1003 public:
1004 cfn_ctz (bool internal) { m_gimple_call_internal_p = internal; }
1005 using range_operator::fold_range;
1006 virtual bool fold_range (irange &r, tree type, const irange &lh,
1007 const irange &rh, relation_trio) const;
1008 private:
1009 bool m_gimple_call_internal_p;
1010 } op_cfn_ctz (false), op_cfn_ctz_internal (true);
1012 bool
1013 cfn_ctz::fold_range (irange &r, tree type, const irange &lh,
1014 const irange &rh, relation_trio) const
1016 if (lh.undefined_p ())
1017 return false;
1018 int prec = TYPE_PRECISION (lh.type ());
1019 int mini = 0;
1020 int maxi = prec - 1;
1022 if (m_gimple_call_internal_p)
1024 // Handle only the two common values.
1025 if (rh.lower_bound () == -1)
1026 mini = -1;
1027 else if (rh.lower_bound () == prec)
1028 maxi = prec;
1029 else
1030 // Magic value to give up, unless we can prove arg is non-zero.
1031 mini = -2;
1033 // If arg is non-zero, then use [0, prec - 1].
1034 if (!range_includes_zero_p (lh))
1036 mini = 0;
1037 maxi = prec - 1;
1039 // If some high bits are known to be zero, we can decrease
1040 // the maximum.
1041 wide_int max = lh.upper_bound ();
1042 if (max == 0)
1044 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1045 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1046 // Otherwise ignore the range.
1047 if (mini == -1)
1048 maxi = -1;
1049 else if (maxi == prec)
1050 mini = prec;
1052 // If value at zero is prec and 0 is in the range, we can't lower
1053 // the upper bound. We could create two separate ranges though,
1054 // [0,floor_log2(max)][prec,prec] though.
1055 else if (maxi != prec)
1056 maxi = wi::floor_log2 (max);
1058 if (mini == -2)
1059 return false;
1060 r.set (type,
1061 wi::shwi (mini, TYPE_PRECISION (type)),
1062 wi::shwi (maxi, TYPE_PRECISION (type)));
1063 return true;
1067 // Implement range operator for CFN_BUILT_IN_
1068 class cfn_clrsb : public range_operator
1070 public:
1071 using range_operator::fold_range;
1072 virtual bool fold_range (irange &r, tree type, const irange &lh,
1073 const irange &, relation_trio) const
1075 if (lh.undefined_p ())
1076 return false;
1077 int prec = TYPE_PRECISION (lh.type ());
1078 r.set (type,
1079 wi::zero (TYPE_PRECISION (type)),
1080 wi::shwi (prec - 1, TYPE_PRECISION (type)));
1081 return true;
1083 } op_cfn_clrsb;
1086 // Implement range operator for CFN_BUILT_IN_
1087 class cfn_ubsan : public range_operator
1089 public:
1090 cfn_ubsan (enum tree_code code) { m_code = code; }
1091 using range_operator::fold_range;
1092 virtual bool fold_range (irange &r, tree type, const irange &lh,
1093 const irange &rh, relation_trio rel) const
1095 bool saved_flag_wrapv = flag_wrapv;
1096 // Pretend the arithmetic is wrapping. If there is any overflow,
1097 // we'll complain, but will actually do wrapping operation.
1098 flag_wrapv = 1;
1099 bool result = range_op_handler (m_code).fold_range (r, type, lh, rh, rel);
1100 flag_wrapv = saved_flag_wrapv;
1102 // If for both arguments vrp_valueize returned non-NULL, this should
1103 // have been already folded and if not, it wasn't folded because of
1104 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
1105 if (result && r.singleton_p ())
1106 r.set_varying (type);
1107 return result;
1109 private:
1110 enum tree_code m_code;
1113 cfn_ubsan op_cfn_ubsan_add (PLUS_EXPR);
1114 cfn_ubsan op_cfn_ubsan_sub (MINUS_EXPR);
1115 cfn_ubsan op_cfn_ubsan_mul (MULT_EXPR);
1118 // Implement range operator for CFN_BUILT_IN_STRLEN
1119 class cfn_strlen : public range_operator
1121 public:
1122 using range_operator::fold_range;
1123 virtual bool fold_range (irange &r, tree type, const irange &,
1124 const irange &, relation_trio) const
1126 wide_int max = irange_val_max (ptrdiff_type_node);
1127 // To account for the terminating NULL, the maximum length
1128 // is one less than the maximum array size, which in turn
1129 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1130 // smaller than the former type).
1131 // FIXME: Use max_object_size() - 1 here.
1132 r.set (type, wi::zero (TYPE_PRECISION (type)), max - 2);
1133 return true;
1135 virtual bool pointers_handled_p (range_op_dispatch_type type,
1136 unsigned dispatch) const
1138 switch (type)
1140 case DISPATCH_FOLD_RANGE:
1141 return dispatch == RO_IPI;
1142 default:
1143 return true;
1146 } op_cfn_strlen;
1149 // Implement range operator for CFN_BUILT_IN_GOACC_DIM
1150 class cfn_goacc_dim : public range_operator
1152 public:
1153 cfn_goacc_dim (bool is_pos) { m_is_pos = is_pos; }
1154 using range_operator::fold_range;
1155 virtual bool fold_range (irange &r, tree type, const irange &lh,
1156 const irange &, relation_trio) const
1158 tree axis_tree;
1159 if (!lh.singleton_p (&axis_tree))
1160 return false;
1161 HOST_WIDE_INT axis = TREE_INT_CST_LOW (axis_tree);
1162 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1163 if (!size)
1164 // If it's dynamic, the backend might know a hardware limitation.
1165 size = targetm.goacc.dim_limit (axis);
1167 r.set (type,
1168 wi::shwi (m_is_pos ? 0 : 1, TYPE_PRECISION (type)),
1169 size
1170 ? wi::shwi (size - m_is_pos, TYPE_PRECISION (type))
1171 : irange_val_max (type));
1172 return true;
1174 private:
1175 bool m_is_pos;
1176 } op_cfn_goacc_dim_size (false), op_cfn_goacc_dim_pos (true);
1179 // Implement range operator for CFN_BUILT_IN_
1180 class cfn_parity : public range_operator
1182 public:
1183 using range_operator::fold_range;
1184 virtual bool fold_range (irange &r, tree type, const irange &,
1185 const irange &, relation_trio) const
1187 r = range_true_and_false (type);
1188 return true;
1190 } op_cfn_parity;
1192 // Set up a gimple_range_op_handler for any nonstandard function which can be
1193 // supported via range-ops.
1195 void
1196 gimple_range_op_handler::maybe_non_standard ()
1198 range_op_handler signed_op (OP_WIDEN_MULT_SIGNED);
1199 gcc_checking_assert (signed_op);
1200 range_op_handler unsigned_op (OP_WIDEN_MULT_UNSIGNED);
1201 gcc_checking_assert (unsigned_op);
1203 if (gimple_code (m_stmt) == GIMPLE_ASSIGN)
1204 switch (gimple_assign_rhs_code (m_stmt))
1206 case WIDEN_MULT_EXPR:
1208 m_op1 = gimple_assign_rhs1 (m_stmt);
1209 m_op2 = gimple_assign_rhs2 (m_stmt);
1210 tree ret = gimple_assign_lhs (m_stmt);
1211 bool signed1 = TYPE_SIGN (TREE_TYPE (m_op1)) == SIGNED;
1212 bool signed2 = TYPE_SIGN (TREE_TYPE (m_op2)) == SIGNED;
1213 bool signed_ret = TYPE_SIGN (TREE_TYPE (ret)) == SIGNED;
1215 /* Normally these operands should all have the same sign, but
1216 some passes and violate this by taking mismatched sign args. At
1217 the moment the only one that's possible is mismatch inputs and
1218 unsigned output. Once ranger supports signs for the operands we
1219 can properly fix it, for now only accept the case we can do
1220 correctly. */
1221 if ((signed1 ^ signed2) && signed_ret)
1222 return;
1224 if (signed2 && !signed1)
1225 std::swap (m_op1, m_op2);
1227 if (signed1 || signed2)
1228 m_operator = signed_op.range_op ();
1229 else
1230 m_operator = unsigned_op.range_op ();
1231 break;
1233 default:
1234 break;
1238 // Set up a gimple_range_op_handler for any built in function which can be
1239 // supported via range-ops.
1241 void
1242 gimple_range_op_handler::maybe_builtin_call ()
1244 gcc_checking_assert (is_a <gcall *> (m_stmt));
1246 gcall *call = as_a <gcall *> (m_stmt);
1247 combined_fn func = gimple_call_combined_fn (call);
1248 if (func == CFN_LAST)
1249 return;
1250 tree type = gimple_range_type (call);
1251 if (!type)
1252 return;
1253 if (!Value_Range::supports_type_p (type))
1254 return;
1256 switch (func)
1258 case CFN_BUILT_IN_CONSTANT_P:
1259 m_op1 = gimple_call_arg (call, 0);
1260 if (irange::supports_p (TREE_TYPE (m_op1)))
1261 m_operator = &op_cfn_constant_p;
1262 else if (frange::supports_p (TREE_TYPE (m_op1)))
1263 m_operator = &op_cfn_constant_float_p;
1264 break;
1266 CASE_FLT_FN (CFN_BUILT_IN_SIGNBIT):
1267 m_op1 = gimple_call_arg (call, 0);
1268 m_operator = &op_cfn_signbit;
1269 break;
1271 CASE_CFN_COPYSIGN_ALL:
1272 m_op1 = gimple_call_arg (call, 0);
1273 m_op2 = gimple_call_arg (call, 1);
1274 m_operator = &op_cfn_copysign;
1275 break;
1277 CASE_CFN_SQRT:
1278 CASE_CFN_SQRT_FN:
1279 m_op1 = gimple_call_arg (call, 0);
1280 m_operator = &op_cfn_sqrt;
1281 break;
1283 CASE_CFN_SIN:
1284 CASE_CFN_SIN_FN:
1285 m_op1 = gimple_call_arg (call, 0);
1286 m_operator = &op_cfn_sin;
1287 break;
1289 CASE_CFN_COS:
1290 CASE_CFN_COS_FN:
1291 m_op1 = gimple_call_arg (call, 0);
1292 m_operator = &op_cfn_cos;
1293 break;
1295 case CFN_BUILT_IN_TOUPPER:
1296 case CFN_BUILT_IN_TOLOWER:
1297 // Only proceed If the argument is compatible with the LHS.
1298 m_op1 = gimple_call_arg (call, 0);
1299 if (range_compatible_p (type, TREE_TYPE (m_op1)))
1300 m_operator = (func == CFN_BUILT_IN_TOLOWER) ? &op_cfn_tolower
1301 : &op_cfn_toupper;
1302 break;
1304 CASE_CFN_FFS:
1305 m_op1 = gimple_call_arg (call, 0);
1306 m_operator = &op_cfn_ffs;
1307 break;
1309 CASE_CFN_POPCOUNT:
1310 m_op1 = gimple_call_arg (call, 0);
1311 m_operator = &op_cfn_popcount;
1312 break;
1314 CASE_CFN_CLZ:
1315 m_op1 = gimple_call_arg (call, 0);
1316 if (gimple_call_internal_p (call)
1317 && gimple_call_num_args (call) == 2)
1319 m_op2 = gimple_call_arg (call, 1);
1320 m_operator = &op_cfn_clz_internal;
1322 else
1323 m_operator = &op_cfn_clz;
1324 break;
1326 CASE_CFN_CTZ:
1327 m_op1 = gimple_call_arg (call, 0);
1328 if (gimple_call_internal_p (call)
1329 && gimple_call_num_args (call) == 2)
1331 m_op2 = gimple_call_arg (call, 1);
1332 m_operator = &op_cfn_ctz_internal;
1334 else
1335 m_operator = &op_cfn_ctz;
1336 break;
1338 CASE_CFN_CLRSB:
1339 m_op1 = gimple_call_arg (call, 0);
1340 m_operator = &op_cfn_clrsb;
1341 break;
1343 case CFN_UBSAN_CHECK_ADD:
1344 m_op1 = gimple_call_arg (call, 0);
1345 m_op2 = gimple_call_arg (call, 1);
1346 m_operator = &op_cfn_ubsan_add;
1347 break;
1349 case CFN_UBSAN_CHECK_SUB:
1350 m_op1 = gimple_call_arg (call, 0);
1351 m_op2 = gimple_call_arg (call, 1);
1352 m_operator = &op_cfn_ubsan_sub;
1353 break;
1355 case CFN_UBSAN_CHECK_MUL:
1356 m_op1 = gimple_call_arg (call, 0);
1357 m_op2 = gimple_call_arg (call, 1);
1358 m_operator = &op_cfn_ubsan_mul;
1359 break;
1361 case CFN_BUILT_IN_STRLEN:
1363 tree lhs = gimple_call_lhs (call);
1364 if (lhs && ptrdiff_type_node && (TYPE_PRECISION (ptrdiff_type_node)
1365 == TYPE_PRECISION (TREE_TYPE (lhs))))
1367 m_op1 = gimple_call_arg (call, 0);
1368 m_operator = &op_cfn_strlen;
1370 break;
1373 // Optimizing these two internal functions helps the loop
1374 // optimizer eliminate outer comparisons. Size is [1,N]
1375 // and pos is [0,N-1].
1376 case CFN_GOACC_DIM_SIZE:
1377 // This call will ensure all the asserts are triggered.
1378 oacc_get_ifn_dim_arg (call);
1379 m_op1 = gimple_call_arg (call, 0);
1380 m_operator = &op_cfn_goacc_dim_size;
1381 break;
1383 case CFN_GOACC_DIM_POS:
1384 // This call will ensure all the asserts are triggered.
1385 oacc_get_ifn_dim_arg (call);
1386 m_op1 = gimple_call_arg (call, 0);
1387 m_operator = &op_cfn_goacc_dim_pos;
1388 break;
1390 CASE_CFN_PARITY:
1391 m_operator = &op_cfn_parity;
1392 break;
1394 default:
1396 unsigned arg;
1397 if (gimple_call_fnspec (call).returns_arg (&arg) && arg == 0)
1399 m_op1 = gimple_call_arg (call, 0);
1400 m_operator = &op_cfn_pass_through_arg1;
1402 break;