]>
gcc.gnu.org Git - gcc.git/blob - gcc/gimple-range-op.cc
d7c6dfa933d1f63192953807815778b38956f3df
1 /* Code for GIMPLE range op related routines.
2 Copyright (C) 2019-2022 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"
47 // Given stmt S, fill VEC, up to VEC_SIZE elements, with relevant ssa-names
48 // on the statement. For efficiency, it is an error to not pass in enough
49 // elements for the vector. Return the number of ssa-names.
52 gimple_range_ssa_names (tree
*vec
, unsigned vec_size
, gimple
*stmt
)
57 gimple_range_op_handler
handler (stmt
);
60 gcc_checking_assert (vec_size
>= 2);
61 if ((ssa
= gimple_range_ssa_p (handler
.operand1 ())))
63 if ((ssa
= gimple_range_ssa_p (handler
.operand2 ())))
66 else if (is_a
<gassign
*> (stmt
)
67 && gimple_assign_rhs_code (stmt
) == COND_EXPR
)
69 gcc_checking_assert (vec_size
>= 3);
70 gassign
*st
= as_a
<gassign
*> (stmt
);
71 if ((ssa
= gimple_range_ssa_p (gimple_assign_rhs1 (st
))))
73 if ((ssa
= gimple_range_ssa_p (gimple_assign_rhs2 (st
))))
75 if ((ssa
= gimple_range_ssa_p (gimple_assign_rhs3 (st
))))
81 // Return the base of the RHS of an assignment.
84 gimple_range_base_of_assignment (const gimple
*stmt
)
86 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
87 tree op1
= gimple_assign_rhs1 (stmt
);
88 if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
89 return get_base_address (TREE_OPERAND (op1
, 0));
93 // If statement is supported by range-ops, set the CODE and return the TYPE.
96 get_code_and_type (gimple
*s
, enum tree_code
&code
)
98 tree type
= NULL_TREE
;
101 if (const gassign
*ass
= dyn_cast
<const gassign
*> (s
))
103 code
= gimple_assign_rhs_code (ass
);
104 // The LHS of a comparison is always an int, so we must look at
106 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
107 type
= TREE_TYPE (gimple_assign_rhs1 (ass
));
109 type
= TREE_TYPE (gimple_assign_lhs (ass
));
111 else if (const gcond
*cond
= dyn_cast
<const gcond
*> (s
))
113 code
= gimple_cond_code (cond
);
114 type
= TREE_TYPE (gimple_cond_lhs (cond
));
119 // If statement S has a supported range_op handler return TRUE.
122 gimple_range_op_handler::supported_p (gimple
*s
)
125 tree type
= get_code_and_type (s
, code
);
126 if (type
&& range_op_handler (code
, type
))
128 if (is_a
<gcall
*> (s
) && gimple_range_op_handler (s
))
133 // Construct a handler object for statement S.
135 gimple_range_op_handler::gimple_range_op_handler (gimple
*s
)
138 tree type
= get_code_and_type (s
, code
);
143 set_op_handler (code
, type
);
146 switch (gimple_code (m_stmt
))
149 m_op1
= gimple_cond_lhs (m_stmt
);
150 m_op2
= gimple_cond_rhs (m_stmt
);
153 m_op1
= gimple_range_base_of_assignment (m_stmt
);
154 if (m_op1
&& TREE_CODE (m_op1
) == MEM_REF
)
156 // If the base address is an SSA_NAME, we return it
157 // here. This allows processing of the range of that
158 // name, while the rest of the expression is simply
159 // ignored. The code in range_ops will see the
160 // ADDR_EXPR and do the right thing.
161 tree ssa
= TREE_OPERAND (m_op1
, 0);
162 if (TREE_CODE (ssa
) == SSA_NAME
)
165 if (gimple_num_ops (m_stmt
) >= 3)
166 m_op2
= gimple_assign_rhs2 (m_stmt
);
172 // If no range-op table entry handled this stmt, check for other supported
174 if (is_a
<gcall
*> (m_stmt
))
175 maybe_builtin_call ();
178 // Calculate what we can determine of the range of this unary
179 // statement's operand if the lhs of the expression has the range
180 // LHS_RANGE. Return false if nothing can be determined.
183 gimple_range_op_handler::calc_op1 (vrange
&r
, const vrange
&lhs_range
)
185 gcc_checking_assert (gimple_num_ops (m_stmt
) < 3);
186 // Give up on empty ranges.
187 if (lhs_range
.undefined_p ())
190 // Unary operations require the type of the first operand in the
191 // second range position.
192 tree type
= TREE_TYPE (operand1 ());
193 Value_Range
type_range (type
);
194 type_range
.set_varying (type
);
195 return op1_range (r
, type
, lhs_range
, type_range
);
198 // Calculate what we can determine of the range of this statement's
199 // first operand if the lhs of the expression has the range LHS_RANGE
200 // and the second operand has the range OP2_RANGE. Return false if
201 // nothing can be determined.
204 gimple_range_op_handler::calc_op1 (vrange
&r
, const vrange
&lhs_range
,
205 const vrange
&op2_range
)
207 // Give up on empty ranges.
208 if (lhs_range
.undefined_p ())
211 // Unary operation are allowed to pass a range in for second operand
212 // as there are often additional restrictions beyond the type which
213 // can be imposed. See operator_cast::op1_range().
214 tree type
= TREE_TYPE (operand1 ());
215 // If op2 is undefined, solve as if it is varying.
216 if (op2_range
.undefined_p ())
218 if (gimple_num_ops (m_stmt
) < 3)
221 // This is sometimes invoked on single operand stmts.
223 op2_type
= TREE_TYPE (operand2 ());
225 op2_type
= TREE_TYPE (operand1 ());
226 Value_Range
trange (op2_type
);
227 trange
.set_varying (op2_type
);
228 return op1_range (r
, type
, lhs_range
, trange
);
230 return op1_range (r
, type
, lhs_range
, op2_range
);
233 // Calculate what we can determine of the range of this statement's
234 // second operand if the lhs of the expression has the range LHS_RANGE
235 // and the first operand has the range OP1_RANGE. Return false if
236 // nothing can be determined.
239 gimple_range_op_handler::calc_op2 (vrange
&r
, const vrange
&lhs_range
,
240 const vrange
&op1_range
)
242 // Give up on empty ranges.
243 if (lhs_range
.undefined_p ())
246 tree type
= TREE_TYPE (operand2 ());
247 // If op1 is undefined, solve as if it is varying.
248 if (op1_range
.undefined_p ())
250 tree op1_type
= TREE_TYPE (operand1 ());
251 Value_Range
trange (op1_type
);
252 trange
.set_varying (op1_type
);
253 return op2_range (r
, type
, lhs_range
, trange
);
255 return op2_range (r
, type
, lhs_range
, op1_range
);
258 // --------------------------------------------------------------------
260 // Implement range operator for float CFN_BUILT_IN_CONSTANT_P.
261 class cfn_constant_float_p
: public range_operator_float
264 using range_operator_float::fold_range
;
265 virtual bool fold_range (irange
&r
, tree type
, const frange
&lh
,
266 const irange
&, relation_kind
) const
268 if (lh
.singleton_p ())
270 r
.set (build_one_cst (type
), build_one_cst (type
));
273 if (cfun
->after_inlining
)
280 } op_cfn_constant_float_p
;
282 // Implement range operator for integral CFN_BUILT_IN_CONSTANT_P.
283 class cfn_constant_p
: public range_operator
286 using range_operator::fold_range
;
287 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
288 const irange
&, relation_kind
) const
290 if (lh
.singleton_p ())
292 r
.set (build_one_cst (type
), build_one_cst (type
));
295 if (cfun
->after_inlining
)
304 // Implement range operator for CFN_BUILT_IN_SIGNBIT.
305 class cfn_signbit
: public range_operator_float
308 using range_operator_float::fold_range
;
309 virtual bool fold_range (irange
&r
, tree type
, const frange
&lh
,
310 const irange
&, relation_kind
) const
313 if (lh
.signbit_p (signbit
))
316 r
.set_nonzero (type
);
325 // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER.
326 class cfn_toupper_tolower
: public range_operator
329 using range_operator::fold_range
;
330 cfn_toupper_tolower (bool toupper
) { m_toupper
= toupper
; }
331 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
332 const irange
&, relation_kind
) const;
334 bool get_letter_range (tree type
, irange
&lowers
, irange
&uppers
) const;
336 } op_cfn_toupper (true), op_cfn_tolower (false);
338 // Return TRUE if we recognize the target character set and return the
339 // range for lower case and upper case letters.
342 cfn_toupper_tolower::get_letter_range (tree type
, irange
&lowers
,
343 irange
&uppers
) const
346 int a
= lang_hooks
.to_target_charset ('a');
347 int z
= lang_hooks
.to_target_charset ('z');
348 int A
= lang_hooks
.to_target_charset ('A');
349 int Z
= lang_hooks
.to_target_charset ('Z');
351 if ((z
- a
== 25) && (Z
- A
== 25))
353 lowers
= int_range
<2> (build_int_cst (type
, a
), build_int_cst (type
, z
));
354 uppers
= int_range
<2> (build_int_cst (type
, A
), build_int_cst (type
, Z
));
357 // Unknown character set.
362 cfn_toupper_tolower::fold_range (irange
&r
, tree type
, const irange
&lh
,
363 const irange
&, relation_kind
) const
367 if (!get_letter_range (type
, lowers
, uppers
))
373 // Return the range passed in without any lower case characters,
374 // but including all the upper case ones.
376 r
.intersect (lowers
);
381 // Return the range passed in without any lower case characters,
382 // but including all the upper case ones.
384 r
.intersect (uppers
);
390 // Implement range operator for CFN_BUILT_IN_FFS and CFN_BUILT_IN_POPCOUNT.
391 class cfn_popcount
: public range_operator
394 using range_operator::fold_range
;
395 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
396 const irange
&, relation_kind
) const
398 if (lh
.undefined_p ())
400 // __builtin_ffs* and __builtin_popcount* return [0, prec].
401 int prec
= TYPE_PRECISION (lh
.type ());
402 // If arg is non-zero, then ffs or popcount are non-zero.
403 int mini
= range_includes_zero_p (&lh
) ? 0 : 1;
406 // If some high bits are known to be zero, decrease the maximum.
407 int_range_max tmp
= lh
;
408 if (TYPE_SIGN (tmp
.type ()) == SIGNED
)
409 range_cast (tmp
, unsigned_type_for (tmp
.type ()));
410 wide_int max
= tmp
.upper_bound ();
411 maxi
= wi::floor_log2 (max
) + 1;
412 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
417 // Implement range operator for CFN_BUILT_IN_CLZ
418 class cfn_clz
: public range_operator
421 cfn_clz (bool internal
) { m_gimple_call_internal_p
= internal
; }
422 using range_operator::fold_range
;
423 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
424 const irange
&, relation_kind
) const;
426 bool m_gimple_call_internal_p
;
427 } op_cfn_clz (false), op_cfn_clz_internal (true);
430 cfn_clz::fold_range (irange
&r
, tree type
, const irange
&lh
,
431 const irange
&, relation_kind
) const
433 // __builtin_c[lt]z* return [0, prec-1], except when the
434 // argument is 0, but that is undefined behavior.
436 // For __builtin_c[lt]z* consider argument of 0 always undefined
437 // behavior, for internal fns depending on C?Z_DEFINED_ALUE_AT_ZERO.
438 if (lh
.undefined_p ())
440 int prec
= TYPE_PRECISION (lh
.type ());
444 scalar_int_mode mode
= SCALAR_INT_TYPE_MODE (lh
.type ());
445 if (m_gimple_call_internal_p
)
447 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
448 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
450 // Only handle the single common value.
454 // Magic value to give up, unless we can prove arg is non-zero.
459 // From clz of minimum we can compute result maximum.
460 if (wi::gt_p (lh
.lower_bound (), 0, TYPE_SIGN (lh
.type ())))
462 maxi
= prec
- 1 - wi::floor_log2 (lh
.lower_bound ());
466 else if (!range_includes_zero_p (&lh
))
473 // From clz of maximum we can compute result minimum.
474 wide_int max
= lh
.upper_bound ();
475 int newmini
= prec
- 1 - wi::floor_log2 (max
);
478 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
479 // return [prec, prec], otherwise ignore the range.
488 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
492 // Implement range operator for CFN_BUILT_IN_CTZ
493 class cfn_ctz
: public range_operator
496 cfn_ctz (bool internal
) { m_gimple_call_internal_p
= internal
; }
497 using range_operator::fold_range
;
498 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
499 const irange
&, relation_kind
) const;
501 bool m_gimple_call_internal_p
;
502 } op_cfn_ctz (false), op_cfn_ctz_internal (true);
505 cfn_ctz::fold_range (irange
&r
, tree type
, const irange
&lh
,
506 const irange
&, relation_kind
) const
508 if (lh
.undefined_p ())
510 int prec
= TYPE_PRECISION (lh
.type ());
514 scalar_int_mode mode
= SCALAR_INT_TYPE_MODE (lh
.type ());
516 if (m_gimple_call_internal_p
)
518 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
519 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
521 // Handle only the two common values.
524 else if (zerov
== prec
)
527 // Magic value to give up, unless we can prove arg is non-zero.
531 // If arg is non-zero, then use [0, prec - 1].
532 if (!range_includes_zero_p (&lh
))
537 // If some high bits are known to be zero, we can decrease
539 wide_int max
= lh
.upper_bound ();
542 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
543 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
544 // Otherwise ignore the range.
547 else if (maxi
== prec
)
550 // If value at zero is prec and 0 is in the range, we can't lower
551 // the upper bound. We could create two separate ranges though,
552 // [0,floor_log2(max)][prec,prec] though.
553 else if (maxi
!= prec
)
554 maxi
= wi::floor_log2 (max
);
558 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
563 // Implement range operator for CFN_BUILT_IN_
564 class cfn_clrsb
: public range_operator
567 using range_operator::fold_range
;
568 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
569 const irange
&, relation_kind
) const
571 if (lh
.undefined_p ())
573 int prec
= TYPE_PRECISION (lh
.type ());
574 r
.set (build_int_cst (type
, 0), build_int_cst (type
, prec
- 1));
580 // Implement range operator for CFN_BUILT_IN_
581 class cfn_ubsan
: public range_operator
584 cfn_ubsan (enum tree_code code
) { m_code
= code
; }
585 using range_operator::fold_range
;
586 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
587 const irange
&rh
, relation_kind rel
) const
589 range_op_handler
handler (m_code
, type
);
590 gcc_checking_assert (handler
);
592 bool saved_flag_wrapv
= flag_wrapv
;
593 // Pretend the arithmetic is wrapping. If there is any overflow,
594 // we'll complain, but will actually do wrapping operation.
596 bool result
= handler
.fold_range (r
, type
, lh
, rh
, rel
);
597 flag_wrapv
= saved_flag_wrapv
;
599 // If for both arguments vrp_valueize returned non-NULL, this should
600 // have been already folded and if not, it wasn't folded because of
601 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
602 if (result
&& r
.singleton_p ())
603 r
.set_varying (type
);
607 enum tree_code m_code
;
610 cfn_ubsan
op_cfn_ubsan_add (PLUS_EXPR
);
611 cfn_ubsan
op_cfn_ubsan_sub (MINUS_EXPR
);
612 cfn_ubsan
op_cfn_ubsan_mul (MULT_EXPR
);
615 // Implement range operator for CFN_BUILT_IN_STRLEN
616 class cfn_strlen
: public range_operator
619 using range_operator::fold_range
;
620 virtual bool fold_range (irange
&r
, tree type
, const irange
&,
621 const irange
&, relation_kind
) const
623 tree max
= vrp_val_max (ptrdiff_type_node
);
625 = wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
626 tree range_min
= build_zero_cst (type
);
627 // To account for the terminating NULL, the maximum length
628 // is one less than the maximum array size, which in turn
629 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
630 // smaller than the former type).
631 // FIXME: Use max_object_size() - 1 here.
632 tree range_max
= wide_int_to_tree (type
, wmax
- 2);
633 r
.set (range_min
, range_max
);
639 // Implement range operator for CFN_BUILT_IN_GOACC_DIM
640 class cfn_goacc_dim
: public range_operator
643 cfn_goacc_dim (bool is_pos
) { m_is_pos
= is_pos
; }
644 using range_operator::fold_range
;
645 virtual bool fold_range (irange
&r
, tree type
, const irange
&lh
,
646 const irange
&, relation_kind
) const
649 if (!lh
.singleton_p (&axis_tree
))
651 HOST_WIDE_INT axis
= TREE_INT_CST_LOW (axis_tree
);
652 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
654 // If it's dynamic, the backend might know a hardware limitation.
655 size
= targetm
.goacc
.dim_limit (axis
);
657 r
.set (build_int_cst (type
, m_is_pos
? 0 : 1),
659 ? build_int_cst (type
, size
- m_is_pos
) : vrp_val_max (type
));
664 } op_cfn_goacc_dim_size (false), op_cfn_goacc_dim_pos (true);
667 // Implement range operator for CFN_BUILT_IN_
668 class cfn_parity
: public range_operator
671 using range_operator::fold_range
;
672 virtual bool fold_range (irange
&r
, tree type
, const irange
&,
673 const irange
&, relation_kind
) const
675 r
.set (build_zero_cst (type
), build_one_cst (type
));
680 // Set up a gimple_range_op_handler for any built in function which can be
681 // supported via range-ops.
684 gimple_range_op_handler::maybe_builtin_call ()
686 gcc_checking_assert (is_a
<gcall
*> (m_stmt
));
688 gcall
*call
= as_a
<gcall
*> (m_stmt
);
689 combined_fn func
= gimple_call_combined_fn (call
);
690 if (func
== CFN_LAST
)
692 tree type
= gimple_range_type (call
);
693 gcc_checking_assert (type
);
694 if (!Value_Range::supports_type_p (type
))
699 case CFN_BUILT_IN_CONSTANT_P
:
700 m_op1
= gimple_call_arg (call
, 0);
702 if (irange::supports_p (TREE_TYPE (m_op1
)))
703 m_int
= &op_cfn_constant_p
;
704 else if (frange::supports_p (TREE_TYPE (m_op1
)))
705 m_float
= &op_cfn_constant_float_p
;
710 case CFN_BUILT_IN_SIGNBIT
:
711 m_op1
= gimple_call_arg (call
, 0);
712 m_float
= &op_cfn_signbit
;
716 case CFN_BUILT_IN_TOUPPER
:
717 case CFN_BUILT_IN_TOLOWER
:
718 // Only proceed If the argument is compatible with the LHS.
719 m_op1
= gimple_call_arg (call
, 0);
720 if (range_compatible_p (type
, TREE_TYPE (m_op1
)))
723 m_int
= (func
== CFN_BUILT_IN_TOLOWER
) ? &op_cfn_tolower
730 m_op1
= gimple_call_arg (call
, 0);
731 m_int
= &op_cfn_popcount
;
736 m_op1
= gimple_call_arg (call
, 0);
738 if (gimple_call_internal_p (call
))
739 m_int
= &op_cfn_clz_internal
;
745 m_op1
= gimple_call_arg (call
, 0);
747 if (gimple_call_internal_p (call
))
748 m_int
= &op_cfn_ctz_internal
;
754 m_op1
= gimple_call_arg (call
, 0);
756 m_int
= &op_cfn_clrsb
;
759 case CFN_UBSAN_CHECK_ADD
:
760 m_op1
= gimple_call_arg (call
, 0);
761 m_op2
= gimple_call_arg (call
, 1);
763 m_int
= &op_cfn_ubsan_add
;
766 case CFN_UBSAN_CHECK_SUB
:
767 m_op1
= gimple_call_arg (call
, 0);
768 m_op2
= gimple_call_arg (call
, 1);
770 m_int
= &op_cfn_ubsan_sub
;
773 case CFN_UBSAN_CHECK_MUL
:
774 m_op1
= gimple_call_arg (call
, 0);
775 m_op2
= gimple_call_arg (call
, 1);
777 m_int
= &op_cfn_ubsan_mul
;
780 case CFN_BUILT_IN_STRLEN
:
782 tree lhs
= gimple_call_lhs (call
);
783 if (lhs
&& ptrdiff_type_node
&& (TYPE_PRECISION (ptrdiff_type_node
)
784 == TYPE_PRECISION (TREE_TYPE (lhs
))))
786 m_op1
= gimple_call_arg (call
, 0);
788 m_int
= &op_cfn_strlen
;
793 // Optimizing these two internal functions helps the loop
794 // optimizer eliminate outer comparisons. Size is [1,N]
795 // and pos is [0,N-1].
796 case CFN_GOACC_DIM_SIZE
:
797 // This call will ensure all the asserts are triggered.
798 oacc_get_ifn_dim_arg (call
);
799 m_op1
= gimple_call_arg (call
, 0);
801 m_int
= &op_cfn_goacc_dim_size
;
804 case CFN_GOACC_DIM_POS
:
805 // This call will ensure all the asserts are triggered.
806 oacc_get_ifn_dim_arg (call
);
807 m_op1
= gimple_call_arg (call
, 0);
809 m_int
= &op_cfn_goacc_dim_pos
;
814 m_int
= &op_cfn_parity
;
This page took 0.071289 seconds and 4 git commands to generate.