1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2021 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-fold.h"
34 #include "fold-const.h"
35 #include "case-cfn-macros.h"
36 #include "omp-general.h"
38 #include "tree-ssa-loop.h"
39 #include "tree-scalar-evolution.h"
40 #include "langhooks.h"
41 #include "vr-values.h"
43 #include "value-query.h"
45 #include "gimple-range.h"
46 // Construct a fur_source, and set the m_query field.
48 fur_source::fur_source (range_query
*q
)
53 m_query
= get_range_query (cfun
);
55 m_query
= get_global_range_query ();
59 // Invoke range_of_expr on EXPR.
62 fur_source::get_operand (irange
&r
, tree expr
)
64 return m_query
->range_of_expr (r
, expr
);
67 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
68 // range_query to get the range on the edge.
71 fur_source::get_phi_operand (irange
&r
, tree expr
, edge e
)
73 return m_query
->range_on_edge (r
, e
, expr
);
76 // Default is no relation.
79 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED
,
80 tree op2 ATTRIBUTE_UNUSED
)
85 // Default registers nothing.
88 fur_source::register_relation (gimple
*s ATTRIBUTE_UNUSED
,
89 relation_kind k ATTRIBUTE_UNUSED
,
90 tree op1 ATTRIBUTE_UNUSED
,
91 tree op2 ATTRIBUTE_UNUSED
)
95 // Default registers nothing.
98 fur_source::register_relation (edge e ATTRIBUTE_UNUSED
,
99 relation_kind k ATTRIBUTE_UNUSED
,
100 tree op1 ATTRIBUTE_UNUSED
,
101 tree op2 ATTRIBUTE_UNUSED
)
105 // This version of fur_source will pick a range up off an edge.
107 class fur_edge
: public fur_source
110 fur_edge (edge e
, range_query
*q
= NULL
);
111 virtual bool get_operand (irange
&r
, tree expr
) OVERRIDE
;
112 virtual bool get_phi_operand (irange
&r
, tree expr
, edge e
) OVERRIDE
;
117 // Instantiate an edge based fur_source.
120 fur_edge::fur_edge (edge e
, range_query
*q
) : fur_source (q
)
125 // Get the value of EXPR on edge m_edge.
128 fur_edge::get_operand (irange
&r
, tree expr
)
130 return m_query
->range_on_edge (r
, m_edge
, expr
);
133 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
134 // range_query to get the range on the edge.
137 fur_edge::get_phi_operand (irange
&r
, tree expr
, edge e
)
139 // Edge to edge recalculations not supoprted yet, until we sort it out.
140 gcc_checking_assert (e
== m_edge
);
141 return m_query
->range_on_edge (r
, e
, expr
);
144 // Instantiate a stmt based fur_source.
146 fur_stmt::fur_stmt (gimple
*s
, range_query
*q
) : fur_source (q
)
151 // Retreive range of EXPR as it occurs as a use on stmt M_STMT.
154 fur_stmt::get_operand (irange
&r
, tree expr
)
156 return m_query
->range_of_expr (r
, expr
, m_stmt
);
159 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
160 // range_query to get the range on the edge.
163 fur_stmt::get_phi_operand (irange
&r
, tree expr
, edge e
)
165 // Pick up the range of expr from edge E.
166 fur_edge
e_src (e
, m_query
);
167 return e_src
.get_operand (r
, expr
);
170 // Return relation based from m_stmt.
173 fur_stmt::query_relation (tree op1
, tree op2
)
175 return m_query
->query_relation (m_stmt
, op1
, op2
);
178 // Instantiate a stmt based fur_source with a GORI object.
181 fur_depend::fur_depend (gimple
*s
, gori_compute
*gori
, range_query
*q
)
184 gcc_checking_assert (gori
);
186 // Set relations if there is an oracle in the range_query.
187 // This will enable registering of relationships as they are discovered.
188 m_oracle
= q
->oracle ();
192 // Register a relation on a stmt if there is an oracle.
195 fur_depend::register_relation (gimple
*s
, relation_kind k
, tree op1
, tree op2
)
198 m_oracle
->register_stmt (s
, k
, op1
, op2
);
201 // Register a relation on an edge if there is an oracle.
204 fur_depend::register_relation (edge e
, relation_kind k
, tree op1
, tree op2
)
207 m_oracle
->register_edge (e
, k
, op1
, op2
);
210 // This version of fur_source will pick a range up from a list of ranges
211 // supplied by the caller.
213 class fur_list
: public fur_source
216 fur_list (irange
&r1
);
217 fur_list (irange
&r1
, irange
&r2
);
218 fur_list (unsigned num
, irange
*list
);
219 virtual bool get_operand (irange
&r
, tree expr
) OVERRIDE
;
220 virtual bool get_phi_operand (irange
&r
, tree expr
, edge e
) OVERRIDE
;
222 int_range_max m_local
[2];
228 // One range supplied for unary operations.
230 fur_list::fur_list (irange
&r1
) : fur_source (NULL
)
238 // Two ranges supplied for binary operations.
240 fur_list::fur_list (irange
&r1
, irange
&r2
) : fur_source (NULL
)
249 // Arbitrary number of ranges in a vector.
251 fur_list::fur_list (unsigned num
, irange
*list
) : fur_source (NULL
)
258 // Get the next operand from the vector, ensure types are compatible.
261 fur_list::get_operand (irange
&r
, tree expr
)
263 if (m_index
>= m_limit
)
264 return m_query
->range_of_expr (r
, expr
);
265 r
= m_list
[m_index
++];
266 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr
), r
.type ()));
270 // This will simply pick the next operand from the vector.
272 fur_list::get_phi_operand (irange
&r
, tree expr
, edge e ATTRIBUTE_UNUSED
)
274 return get_operand (r
, expr
);
277 // Fold stmt S into range R using R1 as the first operand.
280 fold_range (irange
&r
, gimple
*s
, irange
&r1
)
284 return f
.fold_stmt (r
, s
, src
);
287 // Fold stmt S into range R using R1 and R2 as the first two operands.
290 fold_range (irange
&r
, gimple
*s
, irange
&r1
, irange
&r2
)
293 fur_list
src (r1
, r2
);
294 return f
.fold_stmt (r
, s
, src
);
297 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
298 // operands encountered.
301 fold_range (irange
&r
, gimple
*s
, unsigned num_elements
, irange
*vector
)
304 fur_list
src (num_elements
, vector
);
305 return f
.fold_stmt (r
, s
, src
);
308 // Fold stmt S into range R using range query Q.
311 fold_range (irange
&r
, gimple
*s
, range_query
*q
)
315 return f
.fold_stmt (r
, s
, src
);
318 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
321 fold_range (irange
&r
, gimple
*s
, edge on_edge
, range_query
*q
)
324 fur_edge
src (on_edge
, q
);
325 return f
.fold_stmt (r
, s
, src
);
328 // -------------------------------------------------------------------------
330 // Adjust the range for a pointer difference where the operands came
333 // This notices the following sequence:
335 // def = __builtin_memchr (arg, 0, sz)
338 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
341 adjust_pointer_diff_expr (irange
&res
, const gimple
*diff_stmt
)
343 tree op0
= gimple_assign_rhs1 (diff_stmt
);
344 tree op1
= gimple_assign_rhs2 (diff_stmt
);
345 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
346 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
349 if (TREE_CODE (op0
) == SSA_NAME
350 && TREE_CODE (op1
) == SSA_NAME
351 && (call
= SSA_NAME_DEF_STMT (op0
))
352 && is_gimple_call (call
)
353 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
354 && TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
355 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
356 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
357 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
358 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
359 && vrp_operand_equal_p (op1
, gimple_call_arg (call
, 0))
360 && integer_zerop (gimple_call_arg (call
, 1)))
362 tree max
= vrp_val_max (ptrdiff_type_node
);
363 unsigned prec
= TYPE_PRECISION (TREE_TYPE (max
));
364 wide_int wmaxm1
= wi::to_wide (max
, prec
) - 1;
365 res
.intersect (wi::zero (prec
), wmaxm1
);
369 // Adjust the range for an IMAGPART_EXPR.
372 adjust_imagpart_expr (irange
&res
, const gimple
*stmt
)
374 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
376 if (TREE_CODE (name
) != SSA_NAME
|| !SSA_NAME_DEF_STMT (name
))
379 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
380 if (is_gimple_call (def_stmt
) && gimple_call_internal_p (def_stmt
))
382 switch (gimple_call_internal_fn (def_stmt
))
384 case IFN_ADD_OVERFLOW
:
385 case IFN_SUB_OVERFLOW
:
386 case IFN_MUL_OVERFLOW
:
387 case IFN_ATOMIC_COMPARE_EXCHANGE
:
390 r
.set_varying (boolean_type_node
);
391 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
392 range_cast (r
, type
);
400 if (is_gimple_assign (def_stmt
))
402 tree cst
= gimple_assign_rhs1 (def_stmt
);
403 if (TREE_CODE (cst
) == COMPLEX_CST
)
405 wide_int imag
= wi::to_wide (TREE_IMAGPART (cst
));
406 res
.intersect (imag
, imag
);
411 // Adjust the range for a REALPART_EXPR.
414 adjust_realpart_expr (irange
&res
, const gimple
*stmt
)
416 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
418 if (TREE_CODE (name
) != SSA_NAME
)
421 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
422 if (!SSA_NAME_DEF_STMT (name
))
425 if (is_gimple_assign (def_stmt
))
427 tree cst
= gimple_assign_rhs1 (def_stmt
);
428 if (TREE_CODE (cst
) == COMPLEX_CST
)
430 tree imag
= TREE_REALPART (cst
);
431 int_range
<2> tmp (imag
, imag
);
437 // This function looks for situations when walking the use/def chains
438 // may provide additonal contextual range information not exposed on
442 gimple_range_adjustment (irange
&res
, const gimple
*stmt
)
444 switch (gimple_expr_code (stmt
))
446 case POINTER_DIFF_EXPR
:
447 adjust_pointer_diff_expr (res
, stmt
);
451 adjust_imagpart_expr (res
, stmt
);
455 adjust_realpart_expr (res
, stmt
);
463 // Return the base of the RHS of an assignment.
466 gimple_range_base_of_assignment (const gimple
*stmt
)
468 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
469 tree op1
= gimple_assign_rhs1 (stmt
);
470 if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
471 return get_base_address (TREE_OPERAND (op1
, 0));
475 // Return the first operand of this statement if it is a valid operand
476 // supported by ranges, otherwise return NULL_TREE. Special case is
477 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
480 gimple_range_operand1 (const gimple
*stmt
)
482 gcc_checking_assert (gimple_range_handler (stmt
));
484 switch (gimple_code (stmt
))
487 return gimple_cond_lhs (stmt
);
490 tree base
= gimple_range_base_of_assignment (stmt
);
491 if (base
&& TREE_CODE (base
) == MEM_REF
)
493 // If the base address is an SSA_NAME, we return it
494 // here. This allows processing of the range of that
495 // name, while the rest of the expression is simply
496 // ignored. The code in range_ops will see the
497 // ADDR_EXPR and do the right thing.
498 tree ssa
= TREE_OPERAND (base
, 0);
499 if (TREE_CODE (ssa
) == SSA_NAME
)
510 // Return the second operand of statement STMT, otherwise return NULL_TREE.
513 gimple_range_operand2 (const gimple
*stmt
)
515 gcc_checking_assert (gimple_range_handler (stmt
));
517 switch (gimple_code (stmt
))
520 return gimple_cond_rhs (stmt
);
522 if (gimple_num_ops (stmt
) >= 3)
523 return gimple_assign_rhs2 (stmt
);
530 // Calculate a range for statement S and return it in R. If NAME is provided it
531 // represents the SSA_NAME on the LHS of the statement. It is only required
532 // if there is more than one lhs/output. If a range cannot
533 // be calculated, return false.
536 fold_using_range::fold_stmt (irange
&r
, gimple
*s
, fur_source
&src
, tree name
)
539 // If name and S are specified, make sure it is an LHS of S.
540 gcc_checking_assert (!name
|| !gimple_get_lhs (s
) ||
541 name
== gimple_get_lhs (s
));
544 name
= gimple_get_lhs (s
);
546 // Process addresses.
547 if (gimple_code (s
) == GIMPLE_ASSIGN
548 && gimple_assign_rhs_code (s
) == ADDR_EXPR
)
549 return range_of_address (r
, s
, src
);
551 if (gimple_range_handler (s
))
552 res
= range_of_range_op (r
, s
, src
);
553 else if (is_a
<gphi
*>(s
))
554 res
= range_of_phi (r
, as_a
<gphi
*> (s
), src
);
555 else if (is_a
<gcall
*>(s
))
556 res
= range_of_call (r
, as_a
<gcall
*> (s
), src
);
557 else if (is_a
<gassign
*> (s
) && gimple_assign_rhs_code (s
) == COND_EXPR
)
558 res
= range_of_cond_expr (r
, as_a
<gassign
*> (s
), src
);
562 // If no name specified or range is unsupported, bail.
563 if (!name
|| !gimple_range_ssa_p (name
))
565 // We don't understand the stmt, so return the global range.
566 r
= gimple_range_global (name
);
570 if (r
.undefined_p ())
573 // We sometimes get compatible types copied from operands, make sure
574 // the correct type is being returned.
575 if (name
&& TREE_TYPE (name
) != r
.type ())
577 gcc_checking_assert (range_compatible_p (r
.type (), TREE_TYPE (name
)));
578 range_cast (r
, TREE_TYPE (name
));
583 // Calculate a range for range_op statement S and return it in R. If any
584 // If a range cannot be calculated, return false.
587 fold_using_range::range_of_range_op (irange
&r
, gimple
*s
, fur_source
&src
)
589 int_range_max range1
, range2
;
590 tree type
= gimple_range_type (s
);
593 range_operator
*handler
= gimple_range_handler (s
);
594 gcc_checking_assert (handler
);
596 tree lhs
= gimple_get_lhs (s
);
597 tree op1
= gimple_range_operand1 (s
);
598 tree op2
= gimple_range_operand2 (s
);
600 if (src
.get_operand (range1
, op1
))
604 // Fold range, and register any dependency if available.
605 int_range
<2> r2 (type
);
606 handler
->fold_range (r
, type
, range1
, r2
);
607 if (lhs
&& gimple_range_ssa_p (op1
))
610 src
.gori ()->register_dependency (lhs
, op1
);
612 rel
= handler
->lhs_op1_relation (r
, range1
, range1
);
613 if (rel
!= VREL_NONE
)
614 src
.register_relation (s
, rel
, lhs
, op1
);
617 else if (src
.get_operand (range2
, op2
))
619 relation_kind rel
= src
.query_relation (op1
, op2
);
620 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && rel
!= VREL_NONE
)
622 fprintf (dump_file
, " folding with relation ");
623 print_relation (dump_file
, rel
);
624 fputc ('\n', dump_file
);
626 // Fold range, and register any dependency if available.
627 handler
->fold_range (r
, type
, range1
, range2
, rel
);
628 relation_fold_and_or (r
, s
, src
);
633 src
.gori ()->register_dependency (lhs
, op1
);
634 src
.gori ()->register_dependency (lhs
, op2
);
636 if (gimple_range_ssa_p (op1
))
638 rel
= handler
->lhs_op1_relation (r
, range1
, range2
);
639 if (rel
!= VREL_NONE
)
640 src
.register_relation (s
, rel
, lhs
, op1
);
642 if (gimple_range_ssa_p (op2
))
644 rel
= handler
->lhs_op2_relation (r
, range1
, range2
);
645 if (rel
!= VREL_NONE
)
646 src
.register_relation (s
, rel
, lhs
, op2
);
649 // Check for an existing BB, as we maybe asked to fold an
650 // artificial statement not in the CFG.
651 else if (is_a
<gcond
*> (s
) && gimple_bb (s
))
653 basic_block bb
= gimple_bb (s
);
654 edge e0
= EDGE_SUCC (bb
, 0);
655 edge e1
= EDGE_SUCC (bb
, 1);
657 if (!single_pred_p (e0
->dest
))
659 if (!single_pred_p (e1
->dest
))
661 src
.register_outgoing_edges (as_a
<gcond
*> (s
), r
, e0
, e1
);
665 r
.set_varying (type
);
668 r
.set_varying (type
);
669 // Make certain range-op adjustments that aren't handled any other way.
670 gimple_range_adjustment (r
, s
);
674 // Calculate the range of an assignment containing an ADDR_EXPR.
675 // Return the range in R.
676 // If a range cannot be calculated, set it to VARYING and return true.
679 fold_using_range::range_of_address (irange
&r
, gimple
*stmt
, fur_source
&src
)
681 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
682 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
);
684 bool strict_overflow_p
;
685 tree expr
= gimple_assign_rhs1 (stmt
);
686 poly_int64 bitsize
, bitpos
;
689 int unsignedp
, reversep
, volatilep
;
690 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
691 &bitpos
, &offset
, &mode
, &unsignedp
,
692 &reversep
, &volatilep
);
695 if (base
!= NULL_TREE
696 && TREE_CODE (base
) == MEM_REF
697 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
699 tree ssa
= TREE_OPERAND (base
, 0);
700 tree lhs
= gimple_get_lhs (stmt
);
701 if (lhs
&& gimple_range_ssa_p (ssa
) && src
.gori ())
702 src
.gori ()->register_dependency (lhs
, ssa
);
703 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa
)));
704 src
.get_operand (r
, ssa
);
705 range_cast (r
, TREE_TYPE (gimple_assign_rhs1 (stmt
)));
707 poly_offset_int off
= 0;
708 bool off_cst
= false;
709 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
711 off
= mem_ref_offset (base
);
713 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
715 off
<<= LOG2_BITS_PER_UNIT
;
719 /* If &X->a is equal to X, the range of X is the result. */
720 if (off_cst
&& known_eq (off
, 0))
722 else if (flag_delete_null_pointer_checks
723 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
)))
725 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
726 allow going from non-NULL pointer to NULL. */
727 if(!range_includes_zero_p (&r
))
730 /* If MEM_REF has a "positive" offset, consider it non-NULL
731 always, for -fdelete-null-pointer-checks also "negative"
732 ones. Punt for unknown offsets (e.g. variable ones). */
733 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
736 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
738 r
= range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
741 r
= int_range
<2> (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
746 if (tree_single_nonzero_warnv_p (expr
, &strict_overflow_p
))
748 r
= range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
752 // Otherwise return varying.
753 r
= int_range
<2> (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
757 // Calculate a range for phi statement S and return it in R.
758 // If a range cannot be calculated, return false.
761 fold_using_range::range_of_phi (irange
&r
, gphi
*phi
, fur_source
&src
)
763 tree phi_def
= gimple_phi_result (phi
);
764 tree type
= gimple_range_type (phi
);
765 int_range_max arg_range
;
771 // Track if all executable arguments are the same.
772 tree single_arg
= NULL_TREE
;
773 bool seen_arg
= false;
775 // Start with an empty range, unioning in each argument's range.
777 for (x
= 0; x
< gimple_phi_num_args (phi
); x
++)
779 tree arg
= gimple_phi_arg_def (phi
, x
);
780 edge e
= gimple_phi_arg_edge (phi
, x
);
782 // Get the range of the argument on its edge.
783 src
.get_phi_operand (arg_range
, arg
, e
);
785 if (!arg_range
.undefined_p ())
787 // Register potential dependencies for stale value tracking.
788 r
.union_ (arg_range
);
789 if (gimple_range_ssa_p (arg
) && src
.gori ())
790 src
.gori ()->register_dependency (phi_def
, arg
);
792 // Track if all arguments are the same.
798 else if (single_arg
!= arg
)
799 single_arg
= NULL_TREE
;
802 // Once the value reaches varying, stop looking.
803 if (r
.varying_p () && single_arg
== NULL_TREE
)
807 // If the PHI boils down to a single effective argument, look at it.
810 // Symbolic arguments are equivalences.
811 if (gimple_range_ssa_p (single_arg
))
812 src
.register_relation (phi
, EQ_EXPR
, phi_def
, single_arg
);
813 else if (src
.get_operand (arg_range
, single_arg
)
814 && arg_range
.singleton_p ())
816 // Numerical arguments that are a constant can be returned as
817 // the constant. This can help fold later cases where even this
818 // constant might have been UNDEFINED via an unreachable edge.
824 // If SCEV is available, query if this PHI has any knonwn values.
825 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def
)))
827 value_range loop_range
;
828 class loop
*l
= loop_containing_stmt (phi
);
829 if (l
&& loop_outer (l
))
831 range_of_ssa_name_with_loop_info (loop_range
, phi_def
, l
, phi
, src
);
832 if (!loop_range
.varying_p ())
834 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
836 fprintf (dump_file
, " Loops range found for ");
837 print_generic_expr (dump_file
, phi_def
, TDF_SLIM
);
838 fprintf (dump_file
, ": ");
839 loop_range
.dump (dump_file
);
840 fprintf (dump_file
, " and calculated range :");
842 fprintf (dump_file
, "\n");
844 r
.intersect (loop_range
);
852 // Calculate a range for call statement S and return it in R.
853 // If a range cannot be calculated, return false.
856 fold_using_range::range_of_call (irange
&r
, gcall
*call
, fur_source
&src
)
858 tree type
= gimple_range_type (call
);
862 tree lhs
= gimple_call_lhs (call
);
863 bool strict_overflow_p
;
865 if (range_of_builtin_call (r
, call
, src
))
867 else if (gimple_stmt_nonnegative_warnv_p (call
, &strict_overflow_p
))
868 r
.set (build_int_cst (type
, 0), TYPE_MAX_VALUE (type
));
869 else if (gimple_call_nonnull_result_p (call
)
870 || gimple_call_nonnull_arg (call
))
871 r
= range_nonzero (type
);
873 r
.set_varying (type
);
875 // If there is an LHS, intersect that with what is known.
879 def
= gimple_range_global (lhs
);
885 // Return the range of a __builtin_ubsan* in CALL and set it in R.
886 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
890 fold_using_range::range_of_builtin_ubsan_call (irange
&r
, gcall
*call
,
891 tree_code code
, fur_source
&src
)
893 gcc_checking_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
894 || code
== MULT_EXPR
);
895 tree type
= gimple_range_type (call
);
896 range_operator
*op
= range_op_handler (code
, type
);
897 gcc_checking_assert (op
);
898 int_range_max ir0
, ir1
;
899 tree arg0
= gimple_call_arg (call
, 0);
900 tree arg1
= gimple_call_arg (call
, 1);
901 src
.get_operand (ir0
, arg0
);
902 src
.get_operand (ir1
, arg1
);
903 // Check for any relation between arg0 and arg1.
904 relation_kind relation
= src
.query_relation (arg0
, arg1
);
906 bool saved_flag_wrapv
= flag_wrapv
;
907 // Pretend the arithmetic is wrapping. If there is any overflow,
908 // we'll complain, but will actually do wrapping operation.
910 op
->fold_range (r
, type
, ir0
, ir1
, relation
);
911 flag_wrapv
= saved_flag_wrapv
;
913 // If for both arguments vrp_valueize returned non-NULL, this should
914 // have been already folded and if not, it wasn't folded because of
915 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
916 if (r
.singleton_p ())
917 r
.set_varying (type
);
920 // Return TRUE if we recognize the target character set and return the
921 // range for lower case and upper case letters.
924 get_letter_range (tree type
, irange
&lowers
, irange
&uppers
)
927 int a
= lang_hooks
.to_target_charset ('a');
928 int z
= lang_hooks
.to_target_charset ('z');
929 int A
= lang_hooks
.to_target_charset ('A');
930 int Z
= lang_hooks
.to_target_charset ('Z');
932 if ((z
- a
== 25) && (Z
- A
== 25))
934 lowers
= int_range
<2> (build_int_cst (type
, a
), build_int_cst (type
, z
));
935 uppers
= int_range
<2> (build_int_cst (type
, A
), build_int_cst (type
, Z
));
938 // Unknown character set.
942 // For a builtin in CALL, return a range in R if known and return
943 // TRUE. Otherwise return FALSE.
946 fold_using_range::range_of_builtin_call (irange
&r
, gcall
*call
,
949 combined_fn func
= gimple_call_combined_fn (call
);
950 if (func
== CFN_LAST
)
953 tree type
= gimple_range_type (call
);
955 int mini
, maxi
, zerov
= 0, prec
;
956 scalar_int_mode mode
;
960 case CFN_BUILT_IN_CONSTANT_P
:
961 if (cfun
->after_inlining
)
967 arg
= gimple_call_arg (call
, 0);
968 if (src
.get_operand (r
, arg
) && r
.singleton_p ())
970 r
.set (build_one_cst (type
), build_one_cst (type
));
975 case CFN_BUILT_IN_TOUPPER
:
977 arg
= gimple_call_arg (call
, 0);
978 // If the argument isn't compatible with the LHS, do nothing.
979 if (!range_compatible_p (type
, TREE_TYPE (arg
)))
981 if (!src
.get_operand (r
, arg
))
986 if (!get_letter_range (type
, lowers
, uppers
))
989 // Return the range passed in without any lower case characters,
990 // but including all the upper case ones.
992 r
.intersect (lowers
);
997 case CFN_BUILT_IN_TOLOWER
:
999 arg
= gimple_call_arg (call
, 0);
1000 // If the argument isn't compatible with the LHS, do nothing.
1001 if (!range_compatible_p (type
, TREE_TYPE (arg
)))
1003 if (!src
.get_operand (r
, arg
))
1006 int_range
<3> lowers
;
1007 int_range
<3> uppers
;
1008 if (!get_letter_range (type
, lowers
, uppers
))
1011 // Return the range passed in without any upper case characters,
1012 // but including all the lower case ones.
1014 r
.intersect (uppers
);
1021 // __builtin_ffs* and __builtin_popcount* return [0, prec].
1022 arg
= gimple_call_arg (call
, 0);
1023 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1026 src
.get_operand (r
, arg
);
1027 // If arg is non-zero, then ffs or popcount are non-zero.
1028 if (!range_includes_zero_p (&r
))
1030 // If some high bits are known to be zero, decrease the maximum.
1031 if (!r
.undefined_p ())
1033 if (TYPE_SIGN (r
.type ()) == SIGNED
)
1034 range_cast (r
, unsigned_type_for (r
.type ()));
1035 wide_int max
= r
.upper_bound ();
1036 maxi
= wi::floor_log2 (max
) + 1;
1038 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
1042 r
.set (build_zero_cst (type
), build_one_cst (type
));
1046 // __builtin_c[lt]z* return [0, prec-1], except when the
1047 // argument is 0, but that is undefined behavior.
1049 // For __builtin_c[lt]z* consider argument of 0 always undefined
1050 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
1051 arg
= gimple_call_arg (call
, 0);
1052 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1055 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1056 if (gimple_call_internal_p (call
))
1058 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
1059 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
1061 // Only handle the single common value.
1065 // Magic value to give up, unless we can prove arg is non-zero.
1070 src
.get_operand (r
, arg
);
1071 // From clz of minimum we can compute result maximum.
1072 if (!r
.undefined_p ())
1074 // From clz of minimum we can compute result maximum.
1075 if (wi::gt_p (r
.lower_bound (), 0, TYPE_SIGN (r
.type ())))
1077 maxi
= prec
- 1 - wi::floor_log2 (r
.lower_bound ());
1081 else if (!range_includes_zero_p (&r
))
1088 // From clz of maximum we can compute result minimum.
1089 wide_int max
= r
.upper_bound ();
1090 int newmini
= prec
- 1 - wi::floor_log2 (max
);
1093 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
1094 // return [prec, prec], otherwise ignore the range.
1103 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
1107 // __builtin_ctz* return [0, prec-1], except for when the
1108 // argument is 0, but that is undefined behavior.
1110 // For __builtin_ctz* consider argument of 0 always undefined
1111 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
1112 arg
= gimple_call_arg (call
, 0);
1113 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1116 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1117 if (gimple_call_internal_p (call
))
1119 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
1120 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
1122 // Handle only the two common values.
1125 else if (zerov
== prec
)
1128 // Magic value to give up, unless we can prove arg is non-zero.
1132 src
.get_operand (r
, arg
);
1133 if (!r
.undefined_p ())
1135 // If arg is non-zero, then use [0, prec - 1].
1136 if (!range_includes_zero_p (&r
))
1141 // If some high bits are known to be zero, we can decrease
1143 wide_int max
= r
.upper_bound ();
1146 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1147 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1148 // Otherwise ignore the range.
1151 else if (maxi
== prec
)
1154 // If value at zero is prec and 0 is in the range, we can't lower
1155 // the upper bound. We could create two separate ranges though,
1156 // [0,floor_log2(max)][prec,prec] though.
1157 else if (maxi
!= prec
)
1158 maxi
= wi::floor_log2 (max
);
1162 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
1166 arg
= gimple_call_arg (call
, 0);
1167 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1168 r
.set (build_int_cst (type
, 0), build_int_cst (type
, prec
- 1));
1170 case CFN_UBSAN_CHECK_ADD
:
1171 range_of_builtin_ubsan_call (r
, call
, PLUS_EXPR
, src
);
1173 case CFN_UBSAN_CHECK_SUB
:
1174 range_of_builtin_ubsan_call (r
, call
, MINUS_EXPR
, src
);
1176 case CFN_UBSAN_CHECK_MUL
:
1177 range_of_builtin_ubsan_call (r
, call
, MULT_EXPR
, src
);
1180 case CFN_GOACC_DIM_SIZE
:
1181 case CFN_GOACC_DIM_POS
:
1182 // Optimizing these two internal functions helps the loop
1183 // optimizer eliminate outer comparisons. Size is [1,N]
1184 // and pos is [0,N-1].
1186 bool is_pos
= func
== CFN_GOACC_DIM_POS
;
1187 int axis
= oacc_get_ifn_dim_arg (call
);
1188 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1190 // If it's dynamic, the backend might know a hardware limitation.
1191 size
= targetm
.goacc
.dim_limit (axis
);
1193 r
.set (build_int_cst (type
, is_pos
? 0 : 1),
1195 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1199 case CFN_BUILT_IN_STRLEN
:
1200 if (tree lhs
= gimple_call_lhs (call
))
1201 if (ptrdiff_type_node
1202 && (TYPE_PRECISION (ptrdiff_type_node
)
1203 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1205 tree type
= TREE_TYPE (lhs
);
1206 tree max
= vrp_val_max (ptrdiff_type_node
);
1208 = wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1209 tree range_min
= build_zero_cst (type
);
1210 // To account for the terminating NULL, the maximum length
1211 // is one less than the maximum array size, which in turn
1212 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1213 // smaller than the former type).
1214 // FIXME: Use max_object_size() - 1 here.
1215 tree range_max
= wide_int_to_tree (type
, wmax
- 2);
1216 r
.set (range_min
, range_max
);
1227 // Calculate a range for COND_EXPR statement S and return it in R.
1228 // If a range cannot be calculated, return false.
1231 fold_using_range::range_of_cond_expr (irange
&r
, gassign
*s
, fur_source
&src
)
1233 int_range_max cond_range
, range1
, range2
;
1234 tree cond
= gimple_assign_rhs1 (s
);
1235 tree op1
= gimple_assign_rhs2 (s
);
1236 tree op2
= gimple_assign_rhs3 (s
);
1238 tree type
= gimple_range_type (s
);
1242 gcc_checking_assert (gimple_assign_rhs_code (s
) == COND_EXPR
);
1243 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1
), TREE_TYPE (op2
)));
1244 src
.get_operand (cond_range
, cond
);
1245 src
.get_operand (range1
, op1
);
1246 src
.get_operand (range2
, op2
);
1248 // If the condition is known, choose the appropriate expression.
1249 if (cond_range
.singleton_p ())
1251 // False, pick second operand.
1252 if (cond_range
.zero_p ())
1262 gcc_checking_assert (r
.undefined_p ()
1263 || range_compatible_p (r
.type (), type
));
1267 // If SCEV has any information about phi node NAME, return it as a range in R.
1270 fold_using_range::range_of_ssa_name_with_loop_info (irange
&r
, tree name
,
1271 class loop
*l
, gphi
*phi
,
1274 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
1275 tree min
, max
, type
= TREE_TYPE (name
);
1276 if (bounds_of_var_in_loop (&min
, &max
, src
.query (), l
, phi
, name
))
1278 if (TREE_CODE (min
) != INTEGER_CST
)
1280 if (src
.query ()->range_of_expr (r
, min
, phi
) && !r
.undefined_p ())
1281 min
= wide_int_to_tree (type
, r
.lower_bound ());
1283 min
= vrp_val_min (type
);
1285 if (TREE_CODE (max
) != INTEGER_CST
)
1287 if (src
.query ()->range_of_expr (r
, max
, phi
) && !r
.undefined_p ())
1288 max
= wide_int_to_tree (type
, r
.upper_bound ());
1290 max
= vrp_val_max (type
);
1295 r
.set_varying (type
);
1298 // -----------------------------------------------------------------------
1300 // Check if an && or || expression can be folded based on relations. ie
1304 // c_2 and c_3 can never be true at the same time,
1305 // Therefore c_4 can always resolve to false based purely on the relations.
1308 fold_using_range::relation_fold_and_or (irange
& lhs_range
, gimple
*s
,
1311 // No queries or already folded.
1312 if (!src
.gori () || !src
.query ()->oracle () || lhs_range
.singleton_p ())
1315 // Only care about AND and OR expressions.
1316 enum tree_code code
= gimple_expr_code (s
);
1317 bool is_and
= false;
1318 if (code
== BIT_AND_EXPR
|| code
== TRUTH_AND_EXPR
)
1320 else if (code
!= BIT_IOR_EXPR
&& code
!= TRUTH_OR_EXPR
)
1323 tree lhs
= gimple_get_lhs (s
);
1324 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (s
));
1325 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (s
));
1327 // Deal with || and && only when there is a full set of symbolics.
1328 if (!lhs
|| !ssa1
|| !ssa2
1329 || (TREE_CODE (TREE_TYPE (lhs
)) != BOOLEAN_TYPE
)
1330 || (TREE_CODE (TREE_TYPE (ssa1
)) != BOOLEAN_TYPE
)
1331 || (TREE_CODE (TREE_TYPE (ssa2
)) != BOOLEAN_TYPE
))
1334 // Now we know its a boolean AND or OR expression with boolean operands.
1335 // Ideally we search dependencies for common names, and see what pops out.
1336 // until then, simply try to resolve direct dependencies.
1338 // Both names will need to have 2 direct dependencies.
1339 tree ssa1_dep2
= src
.gori ()->depend2 (ssa1
);
1340 tree ssa2_dep2
= src
.gori ()->depend2 (ssa2
);
1341 if (!ssa1_dep2
|| !ssa2_dep2
)
1344 tree ssa1_dep1
= src
.gori ()->depend1 (ssa1
);
1345 tree ssa2_dep1
= src
.gori ()->depend1 (ssa2
);
1346 // Make sure they are the same dependencies, and detect the order of the
1348 bool reverse_op2
= true;
1349 if (ssa1_dep1
== ssa2_dep1
&& ssa1_dep2
== ssa2_dep2
)
1350 reverse_op2
= false;
1351 else if (ssa1_dep1
!= ssa2_dep2
|| ssa1_dep2
!= ssa2_dep1
)
1354 range_operator
*handler1
= gimple_range_handler (SSA_NAME_DEF_STMT (ssa1
));
1355 range_operator
*handler2
= gimple_range_handler (SSA_NAME_DEF_STMT (ssa2
));
1357 // If either handler is not present, no relation is found.
1358 if (!handler1
|| !handler2
)
1361 int_range
<2> bool_one (boolean_true_node
, boolean_true_node
);
1363 relation_kind relation1
= handler1
->op1_op2_relation (bool_one
);
1364 relation_kind relation2
= handler2
->op1_op2_relation (bool_one
);
1365 if (relation1
== VREL_NONE
|| relation2
== VREL_NONE
)
1369 relation2
= relation_negate (relation2
);
1371 // x && y is false if the relation intersection of the true cases is NULL.
1372 if (is_and
&& relation_intersect (relation1
, relation2
) == VREL_EMPTY
)
1373 lhs_range
= int_range
<2> (boolean_false_node
, boolean_false_node
);
1374 // x || y is true if the union of the true cases is NO-RELATION..
1375 // ie, one or the other being true covers the full range of possibilties.
1376 else if (!is_and
&& relation_union (relation1
, relation2
) == VREL_NONE
)
1377 lhs_range
= bool_one
;
1381 range_cast (lhs_range
, TREE_TYPE (lhs
));
1382 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1384 fprintf (dump_file
, " Relation adjustment: ");
1385 print_generic_expr (dump_file
, ssa1
, TDF_SLIM
);
1386 fprintf (dump_file
, " and ");
1387 print_generic_expr (dump_file
, ssa2
, TDF_SLIM
);
1388 fprintf (dump_file
, " combine to produce ");
1389 lhs_range
.dump (dump_file
);
1390 fputc ('\n', dump_file
);
1396 // Register any outgoing edge relations from a conditional branch.
1399 fur_source::register_outgoing_edges (gcond
*s
, irange
&lhs_range
, edge e0
, edge e1
)
1402 int_range
<2> e0_range
, e1_range
;
1404 range_operator
*handler
;
1405 basic_block bb
= gimple_bb (s
);
1409 // If this edge is never taken, ignore it.
1410 gcond_edge_range (e0_range
, e0
);
1411 e0_range
.intersect (lhs_range
);
1412 if (e0_range
.undefined_p ())
1419 // If this edge is never taken, ignore it.
1420 gcond_edge_range (e1_range
, e1
);
1421 e1_range
.intersect (lhs_range
);
1422 if (e1_range
.undefined_p ())
1429 // First, register the gcond itself. This will catch statements like
1431 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (s
));
1432 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (s
));
1435 handler
= gimple_range_handler (s
);
1436 gcc_checking_assert (handler
);
1439 relation_kind relation
= handler
->op1_op2_relation (e0_range
);
1440 if (relation
!= VREL_NONE
)
1441 register_relation (e0
, relation
, ssa1
, ssa2
);
1445 relation_kind relation
= handler
->op1_op2_relation (e1_range
);
1446 if (relation
!= VREL_NONE
)
1447 register_relation (e1
, relation
, ssa1
, ssa2
);
1451 // Outgoing relations of GORI exports require a gori engine.
1455 // Now look for other relations in the exports. This will find stmts
1456 // leading to the condition such as:
1459 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb
, name
)
1461 if (TREE_CODE (TREE_TYPE (name
)) != BOOLEAN_TYPE
)
1463 gimple
*stmt
= SSA_NAME_DEF_STMT (name
);
1464 handler
= gimple_range_handler (stmt
);
1467 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (stmt
));
1468 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (stmt
));
1471 if (e0
&& gori ()->outgoing_edge_range_p (r
, e0
, name
, *m_query
)
1472 && r
.singleton_p ())
1474 relation_kind relation
= handler
->op1_op2_relation (r
);
1475 if (relation
!= VREL_NONE
)
1476 register_relation (e0
, relation
, ssa1
, ssa2
);
1478 if (e1
&& gori ()->outgoing_edge_range_p (r
, e1
, name
, *m_query
)
1479 && r
.singleton_p ())
1481 relation_kind relation
= handler
->op1_op2_relation (r
);
1482 if (relation
!= VREL_NONE
)
1483 register_relation (e1
, relation
, ssa1
, ssa2
);