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)
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/>. */
24 #include "coretypes.h"
26 #include "insn-codes.h"
30 #include "gimple-pretty-print.h"
31 #include "optabs-tree.h"
32 #include "gimple-iterator.h"
33 #include "gimple-fold.h"
35 #include "fold-const.h"
36 #include "case-cfn-macros.h"
37 #include "omp-general.h"
39 #include "tree-ssa-loop.h"
40 #include "tree-scalar-evolution.h"
41 #include "langhooks.h"
42 #include "vr-values.h"
44 #include "value-query.h"
45 #include "gimple-range.h"
46 #include "attr-fnspec.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.
54 gimple_range_ssa_names (tree
*vec
, unsigned vec_size
, gimple
*stmt
)
59 gimple_range_op_handler
handler (stmt
);
62 gcc_checking_assert (vec_size
>= 2);
63 if ((ssa
= gimple_range_ssa_p (handler
.operand1 ())))
65 if ((ssa
= gimple_range_ssa_p (handler
.operand2 ())))
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
))))
75 if ((ssa
= gimple_range_ssa_p (gimple_assign_rhs2 (st
))))
77 if ((ssa
= gimple_range_ssa_p (gimple_assign_rhs3 (st
))))
83 // Return the base of the RHS of an assignment.
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));
95 // If statement is supported by range-ops, set the CODE and return the TYPE.
97 static inline enum tree_code
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
);
107 // If statement S has a supported range_op handler return TRUE.
110 gimple_range_op_handler::supported_p (gimple
*s
)
112 enum tree_code code
= get_code (s
);
113 if (range_op_handler (code
))
115 if (is_a
<gcall
*> (s
) && gimple_range_op_handler (s
))
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
));
130 switch (gimple_code (m_stmt
))
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
);
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
)
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
))))
158 m_operator
= oper
.range_op ();
159 gcc_checking_assert (m_operator
);
165 // If no range-op table entry handled this stmt, check for other supported
167 if (is_a
<gcall
*> (m_stmt
))
168 maybe_builtin_call ();
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.
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 ())
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.
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 ())
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)
216 // This is sometimes invoked on single operand stmts.
218 op2_type
= TREE_TYPE (operand2 ());
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.
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 ())
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
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
);
269 if (cfun
->after_inlining
)
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
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
);
292 if (cfun
->after_inlining
)
301 // Implement range operator for integral/pointer functions returning
302 // the first argument.
303 class cfn_pass_through_arg1
: public range_operator
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
314 virtual bool fold_range (prange
&r
, tree
, const prange
&lh
,
315 const prange
&, relation_trio
) const
320 virtual bool op1_range (irange
&r
, tree
, const irange
&lhs
,
321 const irange
&, relation_trio
) const
326 virtual bool op1_range (prange
&r
, tree
, const prange
&lhs
,
327 const prange
&, relation_trio
) const
332 virtual bool pointers_handled_p (range_op_dispatch_type type
,
333 unsigned dispatch
) const
337 case DISPATCH_FOLD_RANGE
:
338 return dispatch
== RO_PPP
;
339 case DISPATCH_OP1_RANGE
:
340 return dispatch
== RO_PPP
;
345 } op_cfn_pass_through_arg1
;
347 // Implement range operator for CFN_BUILT_IN_SIGNBIT.
348 class cfn_signbit
: public range_operator
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
357 if (lh
.signbit_p (signbit
))
360 r
.set_nonzero (type
);
367 virtual bool op1_range (frange
&r
, tree type
, const irange
&lhs
,
368 const frange
&, relation_trio
) const override
372 r
.set (type
, dconst0
, frange_val_max (type
));
373 r
.update_nan (false);
376 if (!lhs
.contains_p (wi::zero (TYPE_PRECISION (lhs
.type ()))))
378 r
.set (type
, frange_val_min (type
), dconstm0
);
386 // Implement range operator for CFN_BUILT_IN_COPYSIGN
387 class cfn_copysign
: public range_operator
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
395 if (!range_op_handler (ABS_EXPR
).fold_range (r
, type
, lh
, frange (type
)))
397 if (!range_op_handler (NEGATE_EXPR
).fold_range (neg
, type
, r
,
402 if (rh
.signbit_p (signbit
))
404 // If the sign is negative, flip the result from ABS,
405 // otherwise leave things positive.
410 // If the sign is unknown, keep the positive and negative
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. */
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
))
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
);
434 bool inexact
= func (m
, m
, MPFR_RNDN
);
435 if (!mpfr_number_p (m
) || mpfr_overflow_p () || mpfr_underflow_p ())
438 REAL_VALUE_TYPE value
, result
;
439 real_from_mpfr (&value
, m
, format
, MPFR_RNDN
);
440 if (!real_isfinite (&value
))
442 if ((value
.cl
== rvc_zero
) != (mpfr_zero_p (m
) != 0))
445 real_convert (&result
, format
, &value
);
446 if (!real_isfinite (&result
))
448 bool round_low
= false;
449 bool round_high
= false;
450 if (!ulps
&& flag_rounding_math
)
452 if (inexact
|| !real_identical (&result
, &value
))
454 if (MODE_COMPOSITE_P (mode
))
455 round_low
= round_high
= true;
458 round_low
= !real_less (&result
, &value
);
459 round_high
= !real_less (&value
, &result
);
465 for (unsigned int i
= 0; i
< ulps
+ round_low
; ++i
)
466 frange_nextafter (mode
, *res_low
, dconstninf
);
471 for (unsigned int i
= 0; i
< ulps
+ round_high
; ++i
)
472 frange_nextafter (mode
, *res_high
, dconstinf
);
477 class cfn_sqrt
: public range_operator
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 ())
488 if (lh
.known_isnan () || real_less (&lh
.upper_bound (), &dconstm0
))
494 = targetm
.libm_function_max_error (CFN_SQRT
, TYPE_MODE (type
), true);
496 r
.set_varying (type
);
498 r
.set (type
, dconstm0
, dconstinf
);
501 REAL_VALUE_TYPE boundmin
= dconstm0
;
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
))
510 = targetm
.libm_function_max_error (CFN_SQRT
, TYPE_MODE (type
), false);
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
))
517 if (!frange_mpfr_arg1 (NULL
, &ub
, mpfr_sqrt
, ub
, type
, ulps
))
520 r2
.set (type
, lb
, ub
);
521 r2
.flush_denormals_to_zero ();
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 ())
532 // A known NAN means the input is [-INF,-0.) U +-NAN.
533 if (lhs
.known_isnan ())
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.
543 // Results outside of [-0.0, +Inf] are impossible.
545 = targetm
.libm_function_max_error (CFN_SQRT
, TYPE_MODE (type
), true);
548 const REAL_VALUE_TYPE
&ub
= lhs
.upper_bound ();
549 REAL_VALUE_TYPE m0
= dconstm0
;
551 frange_nextafter (TYPE_MODE (type
), m0
, dconstninf
);
552 if (real_less (&ub
, &m0
))
554 if (!lhs
.maybe_isnan ())
557 // If lhs could be NAN and finite result is impossible,
558 // the range is like lhs.known_isnan () above.
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));
569 r
.set_varying (type
);
572 = targetm
.libm_function_max_error (CFN_SQRT
, TYPE_MODE (type
), false);
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
);
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
);
606 r2
.set (type
, lb
, ub
);
612 class cfn_sincos
: public range_operator
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 ())
624 if (lh
.known_isnan () || lh
.known_isinf ())
629 unsigned bulps
= targetm
.libm_function_max_error (m_cfn
, TYPE_MODE (type
),
632 r
.set_varying (type
);
634 r
.set (type
, dconstm1
, dconst1
);
637 REAL_VALUE_TYPE boundmin
, boundmax
;
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 ())
648 = targetm
.libm_function_max_error (m_cfn
, TYPE_MODE (type
), false);
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
))
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
))
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
,
668 || !frange_mpfr_arg1 (&ub_lo
, &ub_hi
,
669 m_cfn
== CFN_SIN
? mpfr_sin
: mpfr_cos
, ub
,
671 || !frange_mpfr_arg1 (&lb_deriv_lo
, &lb_deriv_hi
,
672 m_cfn
== CFN_SIN
? mpfr_cos
: mpfr_sin
, lb
,
674 || !frange_mpfr_arg1 (&ub_deriv_lo
, &ub_deriv_hi
,
675 m_cfn
== CFN_SIN
? mpfr_cos
: mpfr_sin
, ub
,
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
))
691 if (real_less (&lb_hi
, &ub_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
))
709 if (real_isneg (&ub_deriv_lo
) != real_isneg (&ub_deriv_hi
))
711 if (real_isneg (&ub_lo
))
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
))
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
))
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.
735 r2
.set (type
, lb
, ub
);
736 r2
.flush_denormals_to_zero ();
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 ())
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
);
755 // Results outside of [-1.0, +1.0] are impossible.
757 = targetm
.libm_function_max_error (m_cfn
, TYPE_MODE (type
), true);
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
;
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 ())
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
);
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));
791 r
.set_varying (type
);
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
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;
807 bool get_letter_range (tree type
, irange
&lowers
, irange
&uppers
) const;
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.
815 cfn_toupper_tolower::get_letter_range (tree type
, irange
&lowers
,
816 irange
&uppers
) const
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
)));
834 // Unknown character set.
839 cfn_toupper_tolower::fold_range (irange
&r
, tree type
, const irange
&lh
,
840 const irange
&, relation_trio
) const
844 if (!get_letter_range (type
, lowers
, uppers
))
850 // Return the range passed in without any lower case characters,
851 // but including all the upper case ones.
853 r
.intersect (lowers
);
858 // Return the range passed in without any lower case characters,
859 // but including all the upper case ones.
861 r
.intersect (uppers
);
867 // Implement range operator for CFN_BUILT_IN_FFS.
868 class cfn_ffs
: public range_operator
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 ())
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;
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;
890 wi::shwi (mini
, TYPE_PRECISION (type
)),
891 wi::shwi (maxi
, TYPE_PRECISION (type
)));
896 // Implement range operator for CFN_BUILT_IN_POPCOUNT.
897 class cfn_popcount
: public cfn_ffs
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 ())
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
);
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
);
928 // Implement range operator for CFN_BUILT_IN_CLZ
929 class cfn_clz
: public range_operator
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;
937 bool m_gimple_call_internal_p
;
938 } op_cfn_clz (false), op_cfn_clz_internal (true);
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 ())
952 int prec
= TYPE_PRECISION (lh
.type ());
955 if (m_gimple_call_internal_p
)
957 // Only handle the single common value.
958 if (rh
.lower_bound () == prec
)
961 // Magic value to give up, unless we can prove arg is non-zero.
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 ());
972 else if (!range_includes_zero_p (lh
))
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
);
984 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
985 // return [prec, prec], otherwise ignore the range.
995 wi::shwi (mini
, TYPE_PRECISION (type
)),
996 wi::shwi (maxi
, TYPE_PRECISION (type
)));
1000 // Implement range operator for CFN_BUILT_IN_CTZ
1001 class cfn_ctz
: public range_operator
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;
1009 bool m_gimple_call_internal_p
;
1010 } op_cfn_ctz (false), op_cfn_ctz_internal (true);
1013 cfn_ctz::fold_range (irange
&r
, tree type
, const irange
&lh
,
1014 const irange
&rh
, relation_trio
) const
1016 if (lh
.undefined_p ())
1018 int prec
= TYPE_PRECISION (lh
.type ());
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)
1027 else if (rh
.lower_bound () == prec
)
1030 // Magic value to give up, unless we can prove arg is non-zero.
1033 // If arg is non-zero, then use [0, prec - 1].
1034 if (!range_includes_zero_p (lh
))
1039 // If some high bits are known to be zero, we can decrease
1041 wide_int max
= lh
.upper_bound ();
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.
1049 else if (maxi
== 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
);
1061 wi::shwi (mini
, TYPE_PRECISION (type
)),
1062 wi::shwi (maxi
, TYPE_PRECISION (type
)));
1067 // Implement range operator for CFN_BUILT_IN_
1068 class cfn_clrsb
: public range_operator
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 ())
1077 int prec
= TYPE_PRECISION (lh
.type ());
1079 wi::zero (TYPE_PRECISION (type
)),
1080 wi::shwi (prec
- 1, TYPE_PRECISION (type
)));
1086 // Implement range operator for CFN_BUILT_IN_
1087 class cfn_ubsan
: public range_operator
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.
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
);
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
1122 using range_operator::fold_range
;
1123 virtual bool fold_range (irange
&r
, tree type
, const prange
&,
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);
1135 virtual bool pointers_handled_p (range_op_dispatch_type type
,
1136 unsigned dispatch
) const
1140 case DISPATCH_FOLD_RANGE
:
1141 return dispatch
== RO_IPI
;
1149 // Implement range operator for CFN_BUILT_IN_GOACC_DIM
1150 class cfn_goacc_dim
: public range_operator
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
1159 if (!lh
.singleton_p (&axis_tree
))
1161 HOST_WIDE_INT axis
= TREE_INT_CST_LOW (axis_tree
);
1162 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1164 // If it's dynamic, the backend might know a hardware limitation.
1165 size
= targetm
.goacc
.dim_limit (axis
);
1168 wi::shwi (m_is_pos
? 0 : 1, TYPE_PRECISION (type
)),
1170 ? wi::shwi (size
- m_is_pos
, TYPE_PRECISION (type
))
1171 : irange_val_max (type
));
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
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
);
1192 // Set up a gimple_range_op_handler for any nonstandard function which can be
1193 // supported via range-ops.
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
1221 if ((signed1
^ signed2
) && signed_ret
)
1224 if (signed2
&& !signed1
)
1225 std::swap (m_op1
, m_op2
);
1227 if (signed1
|| signed2
)
1228 m_operator
= signed_op
.range_op ();
1230 m_operator
= unsigned_op
.range_op ();
1238 // Set up a gimple_range_op_handler for any built in function which can be
1239 // supported via range-ops.
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
)
1250 tree type
= gimple_range_type (call
);
1253 if (!Value_Range::supports_type_p (type
))
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
;
1266 CASE_FLT_FN (CFN_BUILT_IN_SIGNBIT
):
1267 m_op1
= gimple_call_arg (call
, 0);
1268 m_operator
= &op_cfn_signbit
;
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
;
1279 m_op1
= gimple_call_arg (call
, 0);
1280 m_operator
= &op_cfn_sqrt
;
1285 m_op1
= gimple_call_arg (call
, 0);
1286 m_operator
= &op_cfn_sin
;
1291 m_op1
= gimple_call_arg (call
, 0);
1292 m_operator
= &op_cfn_cos
;
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
1305 m_op1
= gimple_call_arg (call
, 0);
1306 m_operator
= &op_cfn_ffs
;
1310 m_op1
= gimple_call_arg (call
, 0);
1311 m_operator
= &op_cfn_popcount
;
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
;
1323 m_operator
= &op_cfn_clz
;
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
;
1335 m_operator
= &op_cfn_ctz
;
1339 m_op1
= gimple_call_arg (call
, 0);
1340 m_operator
= &op_cfn_clrsb
;
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
;
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
;
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
;
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
;
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
;
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
;
1391 m_operator
= &op_cfn_parity
;
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
;