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 "vr-values.h"
42 #include "value-query.h"
44 #include "gimple-range-fold.h"
45 #include "gimple-range-edge.h"
46 #include "gimple-range-gori.h"
47 // Construct a fur_source, and set the m_query field.
49 fur_source::fur_source (range_query
*q
)
54 m_query
= get_range_query (cfun
);
56 m_query
= get_global_range_query ();
60 // Invoke range_of_expr on EXPR.
63 fur_source::get_operand (irange
&r
, tree expr
)
65 return m_query
->range_of_expr (r
, expr
);
68 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
69 // range_query to get the range on the edge.
72 fur_source::get_phi_operand (irange
&r
, tree expr
, edge e
)
74 return m_query
->range_on_edge (r
, e
, expr
);
77 // Default is no relation.
80 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED
,
81 tree op2 ATTRIBUTE_UNUSED
)
86 // Default registers nothing.
89 fur_source::register_relation (gimple
*s ATTRIBUTE_UNUSED
,
90 relation_kind k ATTRIBUTE_UNUSED
,
91 tree op1 ATTRIBUTE_UNUSED
,
92 tree op2 ATTRIBUTE_UNUSED
)
96 // Default registers nothing.
99 fur_source::register_relation (edge e ATTRIBUTE_UNUSED
,
100 relation_kind k ATTRIBUTE_UNUSED
,
101 tree op1 ATTRIBUTE_UNUSED
,
102 tree op2 ATTRIBUTE_UNUSED
)
106 // This version of fur_source will pick a range up off an edge.
108 class fur_edge
: public fur_source
111 fur_edge (edge e
, range_query
*q
= NULL
);
112 virtual bool get_operand (irange
&r
, tree expr
) OVERRIDE
;
113 virtual bool get_phi_operand (irange
&r
, tree expr
, edge e
) OVERRIDE
;
118 // Instantiate an edge based fur_source.
121 fur_edge::fur_edge (edge e
, range_query
*q
) : fur_source (q
)
126 // Get the value of EXPR on edge m_edge.
129 fur_edge::get_operand (irange
&r
, tree expr
)
131 return m_query
->range_on_edge (r
, m_edge
, expr
);
134 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
135 // range_query to get the range on the edge.
138 fur_edge::get_phi_operand (irange
&r
, tree expr
, edge e
)
140 // Edge to edge recalculations not supoprted yet, until we sort it out.
141 gcc_checking_assert (e
== m_edge
);
142 return m_query
->range_on_edge (r
, e
, expr
);
145 // Instantiate a stmt based fur_source.
147 fur_stmt::fur_stmt (gimple
*s
, range_query
*q
) : fur_source (q
)
152 // Retreive range of EXPR as it occurs as a use on stmt M_STMT.
155 fur_stmt::get_operand (irange
&r
, tree expr
)
157 return m_query
->range_of_expr (r
, expr
, m_stmt
);
160 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
161 // range_query to get the range on the edge.
164 fur_stmt::get_phi_operand (irange
&r
, tree expr
, edge e
)
166 // Pick up the range of expr from edge E.
167 fur_edge
e_src (e
, m_query
);
168 return e_src
.get_operand (r
, expr
);
171 // Return relation based from m_stmt.
174 fur_stmt::query_relation (tree op1
, tree op2
)
176 return m_query
->query_relation (m_stmt
, op1
, op2
);
179 // Instantiate a stmt based fur_source with a GORI object.
182 fur_depend::fur_depend (gimple
*s
, gori_compute
*gori
, range_query
*q
)
185 gcc_checking_assert (gori
);
187 // Set relations if there is an oracle in the range_query.
188 // This will enable registering of relationships as they are discovered.
189 m_oracle
= q
->oracle ();
193 // Register a relation on a stmt if there is an oracle.
196 fur_depend::register_relation (gimple
*s
, relation_kind k
, tree op1
, tree op2
)
199 m_oracle
->register_relation (s
, k
, op1
, op2
);
202 // Register a relation on an edge if there is an oracle.
205 fur_depend::register_relation (edge e
, relation_kind k
, tree op1
, tree op2
)
208 m_oracle
->register_relation (e
, k
, op1
, op2
);
211 // This version of fur_source will pick a range up from a list of ranges
212 // supplied by the caller.
214 class fur_list
: public fur_source
217 fur_list (irange
&r1
);
218 fur_list (irange
&r1
, irange
&r2
);
219 fur_list (unsigned num
, irange
*list
);
220 virtual bool get_operand (irange
&r
, tree expr
) OVERRIDE
;
221 virtual bool get_phi_operand (irange
&r
, tree expr
, edge e
) OVERRIDE
;
223 int_range_max m_local
[2];
229 // One range supplied for unary operations.
231 fur_list::fur_list (irange
&r1
) : fur_source (NULL
)
239 // Two ranges supplied for binary operations.
241 fur_list::fur_list (irange
&r1
, irange
&r2
) : fur_source (NULL
)
250 // Arbitrary number of ranges in a vector.
252 fur_list::fur_list (unsigned num
, irange
*list
) : fur_source (NULL
)
259 // Get the next operand from the vector, ensure types are compatible.
262 fur_list::get_operand (irange
&r
, tree expr
)
264 if (m_index
>= m_limit
)
265 return m_query
->range_of_expr (r
, expr
);
266 r
= m_list
[m_index
++];
267 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr
), r
.type ()));
271 // This will simply pick the next operand from the vector.
273 fur_list::get_phi_operand (irange
&r
, tree expr
, edge e ATTRIBUTE_UNUSED
)
275 return get_operand (r
, expr
);
278 // Fold stmt S into range R using R1 as the first operand.
281 fold_range (irange
&r
, gimple
*s
, irange
&r1
)
285 return f
.fold_stmt (r
, s
, src
);
288 // Fold stmt S into range R using R1 and R2 as the first two operands.
291 fold_range (irange
&r
, gimple
*s
, irange
&r1
, irange
&r2
)
294 fur_list
src (r1
, r2
);
295 return f
.fold_stmt (r
, s
, src
);
298 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
299 // operands encountered.
302 fold_range (irange
&r
, gimple
*s
, unsigned num_elements
, irange
*vector
)
305 fur_list
src (num_elements
, vector
);
306 return f
.fold_stmt (r
, s
, src
);
309 // Fold stmt S into range R using range query Q.
312 fold_range (irange
&r
, gimple
*s
, range_query
*q
)
316 return f
.fold_stmt (r
, s
, src
);
319 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
322 fold_range (irange
&r
, gimple
*s
, edge on_edge
, range_query
*q
)
325 fur_edge
src (on_edge
, q
);
326 return f
.fold_stmt (r
, s
, src
);
329 // -------------------------------------------------------------------------
331 // Adjust the range for a pointer difference where the operands came
334 // This notices the following sequence:
336 // def = __builtin_memchr (arg, 0, sz)
339 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
342 adjust_pointer_diff_expr (irange
&res
, const gimple
*diff_stmt
)
344 tree op0
= gimple_assign_rhs1 (diff_stmt
);
345 tree op1
= gimple_assign_rhs2 (diff_stmt
);
346 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
347 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
350 if (TREE_CODE (op0
) == SSA_NAME
351 && TREE_CODE (op1
) == SSA_NAME
352 && (call
= SSA_NAME_DEF_STMT (op0
))
353 && is_gimple_call (call
)
354 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
355 && TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
356 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
357 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
358 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
359 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
360 && vrp_operand_equal_p (op1
, gimple_call_arg (call
, 0))
361 && integer_zerop (gimple_call_arg (call
, 1)))
363 tree max
= vrp_val_max (ptrdiff_type_node
);
364 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
365 tree expr_type
= gimple_expr_type (diff_stmt
);
366 tree range_min
= build_zero_cst (expr_type
);
367 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
368 int_range
<2> r (range_min
, range_max
);
373 // This function looks for situations when walking the use/def chains
374 // may provide additonal contextual range information not exposed on
375 // this statement. Like knowing the IMAGPART return value from a
376 // builtin function is a boolean result.
378 // We should rework how we're called, as we have an op_unknown entry
379 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
380 // function gets called.
383 gimple_range_adjustment (irange
&res
, const gimple
*stmt
)
385 switch (gimple_expr_code (stmt
))
387 case POINTER_DIFF_EXPR
:
388 adjust_pointer_diff_expr (res
, stmt
);
393 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
394 if (TREE_CODE (name
) == SSA_NAME
)
396 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
397 if (def_stmt
&& is_gimple_call (def_stmt
)
398 && gimple_call_internal_p (def_stmt
))
400 switch (gimple_call_internal_fn (def_stmt
))
402 case IFN_ADD_OVERFLOW
:
403 case IFN_SUB_OVERFLOW
:
404 case IFN_MUL_OVERFLOW
:
405 case IFN_ATOMIC_COMPARE_EXCHANGE
:
408 r
.set_varying (boolean_type_node
);
409 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
410 range_cast (r
, type
);
426 // Return the base of the RHS of an assignment.
429 gimple_range_base_of_assignment (const gimple
*stmt
)
431 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
432 tree op1
= gimple_assign_rhs1 (stmt
);
433 if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
434 return get_base_address (TREE_OPERAND (op1
, 0));
438 // Return the first operand of this statement if it is a valid operand
439 // supported by ranges, otherwise return NULL_TREE. Special case is
440 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
443 gimple_range_operand1 (const gimple
*stmt
)
445 gcc_checking_assert (gimple_range_handler (stmt
));
447 switch (gimple_code (stmt
))
450 return gimple_cond_lhs (stmt
);
453 tree base
= gimple_range_base_of_assignment (stmt
);
454 if (base
&& TREE_CODE (base
) == MEM_REF
)
456 // If the base address is an SSA_NAME, we return it
457 // here. This allows processing of the range of that
458 // name, while the rest of the expression is simply
459 // ignored. The code in range_ops will see the
460 // ADDR_EXPR and do the right thing.
461 tree ssa
= TREE_OPERAND (base
, 0);
462 if (TREE_CODE (ssa
) == SSA_NAME
)
473 // Return the second operand of statement STMT, otherwise return NULL_TREE.
476 gimple_range_operand2 (const gimple
*stmt
)
478 gcc_checking_assert (gimple_range_handler (stmt
));
480 switch (gimple_code (stmt
))
483 return gimple_cond_rhs (stmt
);
485 if (gimple_num_ops (stmt
) >= 3)
486 return gimple_assign_rhs2 (stmt
);
493 // Calculate a range for statement S and return it in R. If NAME is provided it
494 // represents the SSA_NAME on the LHS of the statement. It is only required
495 // if there is more than one lhs/output. If a range cannot
496 // be calculated, return false.
499 fold_using_range::fold_stmt (irange
&r
, gimple
*s
, fur_source
&src
, tree name
)
502 // If name and S are specified, make sure it is an LHS of S.
503 gcc_checking_assert (!name
|| !gimple_get_lhs (s
) ||
504 name
== gimple_get_lhs (s
));
507 name
= gimple_get_lhs (s
);
509 // Process addresses.
510 if (gimple_code (s
) == GIMPLE_ASSIGN
511 && gimple_assign_rhs_code (s
) == ADDR_EXPR
)
512 return range_of_address (r
, s
, src
);
514 if (gimple_range_handler (s
))
515 res
= range_of_range_op (r
, s
, src
);
516 else if (is_a
<gphi
*>(s
))
517 res
= range_of_phi (r
, as_a
<gphi
*> (s
), src
);
518 else if (is_a
<gcall
*>(s
))
519 res
= range_of_call (r
, as_a
<gcall
*> (s
), src
);
520 else if (is_a
<gassign
*> (s
) && gimple_assign_rhs_code (s
) == COND_EXPR
)
521 res
= range_of_cond_expr (r
, as_a
<gassign
*> (s
), src
);
525 // If no name is specified, try the expression kind.
528 tree t
= gimple_expr_type (s
);
529 if (!irange::supports_type_p (t
))
534 if (!gimple_range_ssa_p (name
))
536 // We don't understand the stmt, so return the global range.
537 r
= gimple_range_global (name
);
541 if (r
.undefined_p ())
544 // We sometimes get compatible types copied from operands, make sure
545 // the correct type is being returned.
546 if (name
&& TREE_TYPE (name
) != r
.type ())
548 gcc_checking_assert (range_compatible_p (r
.type (), TREE_TYPE (name
)));
549 range_cast (r
, TREE_TYPE (name
));
554 // Calculate a range for range_op statement S and return it in R. If any
555 // If a range cannot be calculated, return false.
558 fold_using_range::range_of_range_op (irange
&r
, gimple
*s
, fur_source
&src
)
560 int_range_max range1
, range2
;
561 tree type
= gimple_expr_type (s
);
562 range_operator
*handler
= gimple_range_handler (s
);
563 gcc_checking_assert (handler
);
564 gcc_checking_assert (irange::supports_type_p (type
));
566 tree lhs
= gimple_get_lhs (s
);
567 tree op1
= gimple_range_operand1 (s
);
568 tree op2
= gimple_range_operand2 (s
);
570 if (src
.get_operand (range1
, op1
))
574 // Fold range, and register any dependency if available.
575 int_range
<2> r2 (type
);
576 handler
->fold_range (r
, type
, range1
, r2
);
577 if (lhs
&& gimple_range_ssa_p (op1
))
580 src
.gori ()->register_dependency (lhs
, op1
);
582 rel
= handler
->lhs_op1_relation (r
, range1
, range1
);
583 if (rel
!= VREL_NONE
)
584 src
.register_relation (s
, rel
, lhs
, op1
);
587 else if (src
.get_operand (range2
, op2
))
589 relation_kind rel
= src
.query_relation (op1
, op2
);
590 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && rel
!= VREL_NONE
)
592 fprintf (dump_file
, " folding with relation ");
593 print_relation (dump_file
, rel
);
594 fputc ('\n', dump_file
);
596 // Fold range, and register any dependency if available.
597 handler
->fold_range (r
, type
, range1
, range2
, rel
);
598 relation_fold_and_or (r
, s
, src
);
603 src
.gori ()->register_dependency (lhs
, op1
);
604 src
.gori ()->register_dependency (lhs
, op2
);
606 if (gimple_range_ssa_p (op1
))
608 rel
= handler
->lhs_op1_relation (r
, range1
, range2
);
609 if (rel
!= VREL_NONE
)
610 src
.register_relation (s
, rel
, lhs
, op1
);
612 if (gimple_range_ssa_p (op2
))
614 rel
= handler
->lhs_op2_relation (r
, range1
, range2
);
615 if (rel
!= VREL_NONE
)
616 src
.register_relation (s
, rel
, lhs
, op2
);
619 else if (is_a
<gcond
*> (s
))
620 postfold_gcond_edges (as_a
<gcond
*> (s
), src
);
623 r
.set_varying (type
);
626 r
.set_varying (type
);
627 // Make certain range-op adjustments that aren't handled any other way.
628 gimple_range_adjustment (r
, s
);
632 // Calculate the range of an assignment containing an ADDR_EXPR.
633 // Return the range in R.
634 // If a range cannot be calculated, set it to VARYING and return true.
637 fold_using_range::range_of_address (irange
&r
, gimple
*stmt
, fur_source
&src
)
639 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
640 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
);
642 bool strict_overflow_p
;
643 tree expr
= gimple_assign_rhs1 (stmt
);
644 poly_int64 bitsize
, bitpos
;
647 int unsignedp
, reversep
, volatilep
;
648 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
649 &bitpos
, &offset
, &mode
, &unsignedp
,
650 &reversep
, &volatilep
);
653 if (base
!= NULL_TREE
654 && TREE_CODE (base
) == MEM_REF
655 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
657 tree ssa
= TREE_OPERAND (base
, 0);
658 tree lhs
= gimple_get_lhs (stmt
);
659 if (lhs
&& gimple_range_ssa_p (ssa
) && src
.gori ())
660 src
.gori ()->register_dependency (lhs
, ssa
);
661 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa
)));
662 src
.get_operand (r
, ssa
);
663 range_cast (r
, TREE_TYPE (gimple_assign_rhs1 (stmt
)));
665 poly_offset_int off
= 0;
666 bool off_cst
= false;
667 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
669 off
= mem_ref_offset (base
);
671 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
673 off
<<= LOG2_BITS_PER_UNIT
;
677 /* If &X->a is equal to X, the range of X is the result. */
678 if (off_cst
&& known_eq (off
, 0))
680 else if (flag_delete_null_pointer_checks
681 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
)))
683 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
684 allow going from non-NULL pointer to NULL. */
685 if(!range_includes_zero_p (&r
))
688 /* If MEM_REF has a "positive" offset, consider it non-NULL
689 always, for -fdelete-null-pointer-checks also "negative"
690 ones. Punt for unknown offsets (e.g. variable ones). */
691 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
694 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
696 r
= range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
699 r
= int_range
<2> (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
704 if (tree_single_nonzero_warnv_p (expr
, &strict_overflow_p
))
706 r
= range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
710 // Otherwise return varying.
711 r
= int_range
<2> (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
715 // Calculate a range for phi statement S and return it in R.
716 // If a range cannot be calculated, return false.
719 fold_using_range::range_of_phi (irange
&r
, gphi
*phi
, fur_source
&src
)
721 tree phi_def
= gimple_phi_result (phi
);
722 tree type
= TREE_TYPE (phi_def
);
723 int_range_max arg_range
;
726 if (!irange::supports_type_p (type
))
729 // Start with an empty range, unioning in each argument's range.
731 for (x
= 0; x
< gimple_phi_num_args (phi
); x
++)
733 tree arg
= gimple_phi_arg_def (phi
, x
);
734 edge e
= gimple_phi_arg_edge (phi
, x
);
736 // Register potential dependencies for stale value tracking.
737 if (gimple_range_ssa_p (arg
) && src
.gori ())
738 src
.gori ()->register_dependency (phi_def
, arg
);
740 // Get the range of the argument on its edge.
741 src
.get_phi_operand (arg_range
, arg
, e
);
742 // If we're recomputing the argument elsewhere, try to refine it.
743 r
.union_ (arg_range
);
744 // Once the value reaches varying, stop looking.
749 // If SCEV is available, query if this PHI has any knonwn values.
750 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def
)))
752 value_range loop_range
;
753 class loop
*l
= loop_containing_stmt (phi
);
754 if (l
&& loop_outer (l
))
756 range_of_ssa_name_with_loop_info (loop_range
, phi_def
, l
, phi
, src
);
757 if (!loop_range
.varying_p ())
759 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
761 fprintf (dump_file
, " Loops range found for ");
762 print_generic_expr (dump_file
, phi_def
, TDF_SLIM
);
763 fprintf (dump_file
, ": ");
764 loop_range
.dump (dump_file
);
765 fprintf (dump_file
, " and calculated range :");
767 fprintf (dump_file
, "\n");
769 r
.intersect (loop_range
);
777 // Calculate a range for call statement S and return it in R.
778 // If a range cannot be calculated, return false.
781 fold_using_range::range_of_call (irange
&r
, gcall
*call
, fur_source
&src
)
783 tree type
= gimple_call_return_type (call
);
784 tree lhs
= gimple_call_lhs (call
);
785 bool strict_overflow_p
;
787 if (!irange::supports_type_p (type
))
790 if (range_of_builtin_call (r
, call
, src
))
792 else if (gimple_stmt_nonnegative_warnv_p (call
, &strict_overflow_p
))
793 r
.set (build_int_cst (type
, 0), TYPE_MAX_VALUE (type
));
794 else if (gimple_call_nonnull_result_p (call
)
795 || gimple_call_nonnull_arg (call
))
796 r
= range_nonzero (type
);
798 r
.set_varying (type
);
800 // If there is an LHS, intersect that with what is known.
804 def
= gimple_range_global (lhs
);
810 // Return the range of a __builtin_ubsan* in CALL and set it in R.
811 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
815 fold_using_range::range_of_builtin_ubsan_call (irange
&r
, gcall
*call
,
816 tree_code code
, fur_source
&src
)
818 gcc_checking_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
819 || code
== MULT_EXPR
);
820 tree type
= gimple_call_return_type (call
);
821 range_operator
*op
= range_op_handler (code
, type
);
822 gcc_checking_assert (op
);
823 int_range_max ir0
, ir1
;
824 tree arg0
= gimple_call_arg (call
, 0);
825 tree arg1
= gimple_call_arg (call
, 1);
826 src
.get_operand (ir0
, arg0
);
827 src
.get_operand (ir1
, arg1
);
829 bool saved_flag_wrapv
= flag_wrapv
;
830 // Pretend the arithmetic is wrapping. If there is any overflow,
831 // we'll complain, but will actually do wrapping operation.
833 op
->fold_range (r
, type
, ir0
, ir1
);
834 flag_wrapv
= saved_flag_wrapv
;
836 // If for both arguments vrp_valueize returned non-NULL, this should
837 // have been already folded and if not, it wasn't folded because of
838 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
839 if (r
.singleton_p ())
840 r
.set_varying (type
);
843 // For a builtin in CALL, return a range in R if known and return
844 // TRUE. Otherwise return FALSE.
847 fold_using_range::range_of_builtin_call (irange
&r
, gcall
*call
,
850 combined_fn func
= gimple_call_combined_fn (call
);
851 if (func
== CFN_LAST
)
854 tree type
= gimple_call_return_type (call
);
856 int mini
, maxi
, zerov
= 0, prec
;
857 scalar_int_mode mode
;
861 case CFN_BUILT_IN_CONSTANT_P
:
862 if (cfun
->after_inlining
)
868 arg
= gimple_call_arg (call
, 0);
869 if (src
.get_operand (r
, arg
) && r
.singleton_p ())
871 r
.set (build_one_cst (type
), build_one_cst (type
));
878 // __builtin_ffs* and __builtin_popcount* return [0, prec].
879 arg
= gimple_call_arg (call
, 0);
880 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
883 src
.get_operand (r
, arg
);
884 // If arg is non-zero, then ffs or popcount are non-zero.
885 if (!range_includes_zero_p (&r
))
887 // If some high bits are known to be zero, decrease the maximum.
888 if (!r
.undefined_p ())
890 if (TYPE_SIGN (r
.type ()) == SIGNED
)
891 range_cast (r
, unsigned_type_for (r
.type ()));
892 wide_int max
= r
.upper_bound ();
893 maxi
= wi::floor_log2 (max
) + 1;
895 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
899 r
.set (build_zero_cst (type
), build_one_cst (type
));
903 // __builtin_c[lt]z* return [0, prec-1], except when the
904 // argument is 0, but that is undefined behavior.
906 // For __builtin_c[lt]z* consider argument of 0 always undefined
907 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
908 arg
= gimple_call_arg (call
, 0);
909 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
912 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
913 if (gimple_call_internal_p (call
))
915 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
916 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
918 // Only handle the single common value.
922 // Magic value to give up, unless we can prove arg is non-zero.
927 src
.get_operand (r
, arg
);
928 // From clz of minimum we can compute result maximum.
929 if (!r
.undefined_p ())
931 // From clz of minimum we can compute result maximum.
932 if (wi::gt_p (r
.lower_bound (), 0, TYPE_SIGN (r
.type ())))
934 maxi
= prec
- 1 - wi::floor_log2 (r
.lower_bound ());
938 else if (!range_includes_zero_p (&r
))
945 // From clz of maximum we can compute result minimum.
946 wide_int max
= r
.upper_bound ();
947 int newmini
= prec
- 1 - wi::floor_log2 (max
);
950 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
951 // return [prec, prec], otherwise ignore the range.
960 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
964 // __builtin_ctz* return [0, prec-1], except for when the
965 // argument is 0, but that is undefined behavior.
967 // For __builtin_ctz* consider argument of 0 always undefined
968 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
969 arg
= gimple_call_arg (call
, 0);
970 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
973 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
974 if (gimple_call_internal_p (call
))
976 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
977 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
979 // Handle only the two common values.
982 else if (zerov
== prec
)
985 // Magic value to give up, unless we can prove arg is non-zero.
989 src
.get_operand (r
, arg
);
990 if (!r
.undefined_p ())
992 // If arg is non-zero, then use [0, prec - 1].
993 if (!range_includes_zero_p (&r
))
998 // If some high bits are known to be zero, we can decrease
1000 wide_int max
= r
.upper_bound ();
1003 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1004 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1005 // Otherwise ignore the range.
1008 else if (maxi
== prec
)
1011 // If value at zero is prec and 0 is in the range, we can't lower
1012 // the upper bound. We could create two separate ranges though,
1013 // [0,floor_log2(max)][prec,prec] though.
1014 else if (maxi
!= prec
)
1015 maxi
= wi::floor_log2 (max
);
1019 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
1023 arg
= gimple_call_arg (call
, 0);
1024 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1025 r
.set (build_int_cst (type
, 0), build_int_cst (type
, prec
- 1));
1027 case CFN_UBSAN_CHECK_ADD
:
1028 range_of_builtin_ubsan_call (r
, call
, PLUS_EXPR
, src
);
1030 case CFN_UBSAN_CHECK_SUB
:
1031 range_of_builtin_ubsan_call (r
, call
, MINUS_EXPR
, src
);
1033 case CFN_UBSAN_CHECK_MUL
:
1034 range_of_builtin_ubsan_call (r
, call
, MULT_EXPR
, src
);
1037 case CFN_GOACC_DIM_SIZE
:
1038 case CFN_GOACC_DIM_POS
:
1039 // Optimizing these two internal functions helps the loop
1040 // optimizer eliminate outer comparisons. Size is [1,N]
1041 // and pos is [0,N-1].
1043 bool is_pos
= func
== CFN_GOACC_DIM_POS
;
1044 int axis
= oacc_get_ifn_dim_arg (call
);
1045 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1047 // If it's dynamic, the backend might know a hardware limitation.
1048 size
= targetm
.goacc
.dim_limit (axis
);
1050 r
.set (build_int_cst (type
, is_pos
? 0 : 1),
1052 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1056 case CFN_BUILT_IN_STRLEN
:
1057 if (tree lhs
= gimple_call_lhs (call
))
1058 if (ptrdiff_type_node
1059 && (TYPE_PRECISION (ptrdiff_type_node
)
1060 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1062 tree type
= TREE_TYPE (lhs
);
1063 tree max
= vrp_val_max (ptrdiff_type_node
);
1065 = wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1066 tree range_min
= build_zero_cst (type
);
1067 // To account for the terminating NULL, the maximum length
1068 // is one less than the maximum array size, which in turn
1069 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1070 // smaller than the former type).
1071 // FIXME: Use max_object_size() - 1 here.
1072 tree range_max
= wide_int_to_tree (type
, wmax
- 2);
1073 r
.set (range_min
, range_max
);
1084 // Calculate a range for COND_EXPR statement S and return it in R.
1085 // If a range cannot be calculated, return false.
1088 fold_using_range::range_of_cond_expr (irange
&r
, gassign
*s
, fur_source
&src
)
1090 int_range_max cond_range
, range1
, range2
;
1091 tree cond
= gimple_assign_rhs1 (s
);
1092 tree op1
= gimple_assign_rhs2 (s
);
1093 tree op2
= gimple_assign_rhs3 (s
);
1095 gcc_checking_assert (gimple_assign_rhs_code (s
) == COND_EXPR
);
1096 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1
),
1098 if (!irange::supports_type_p (TREE_TYPE (op1
)))
1101 src
.get_operand (cond_range
, cond
);
1102 src
.get_operand (range1
, op1
);
1103 src
.get_operand (range2
, op2
);
1105 // If the condition is known, choose the appropriate expression.
1106 if (cond_range
.singleton_p ())
1108 // False, pick second operand.
1109 if (cond_range
.zero_p ())
1122 // If SCEV has any information about phi node NAME, return it as a range in R.
1125 fold_using_range::range_of_ssa_name_with_loop_info (irange
&r
, tree name
,
1126 class loop
*l
, gphi
*phi
,
1129 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
1130 tree min
, max
, type
= TREE_TYPE (name
);
1131 if (bounds_of_var_in_loop (&min
, &max
, src
.query (), l
, phi
, name
))
1133 if (TREE_CODE (min
) != INTEGER_CST
)
1135 if (src
.query ()->range_of_expr (r
, min
, phi
) && !r
.undefined_p ())
1136 min
= wide_int_to_tree (type
, r
.lower_bound ());
1138 min
= vrp_val_min (type
);
1140 if (TREE_CODE (max
) != INTEGER_CST
)
1142 if (src
.query ()->range_of_expr (r
, max
, phi
) && !r
.undefined_p ())
1143 max
= wide_int_to_tree (type
, r
.upper_bound ());
1145 max
= vrp_val_max (type
);
1150 r
.set_varying (type
);
1153 // -----------------------------------------------------------------------
1155 // Check if an && or || expression can be folded based on relations. ie
1159 // c_2 and c_3 can never be true at the same time,
1160 // Therefore c_4 can always resolve to false based purely on the relations.
1163 fold_using_range::relation_fold_and_or (irange
& lhs_range
, gimple
*s
,
1166 // No queries or already folded.
1167 if (!src
.gori () || !src
.query ()->oracle () || lhs_range
.singleton_p ())
1170 // Only care about AND and OR expressions.
1171 enum tree_code code
= gimple_expr_code (s
);
1172 bool is_and
= false;
1173 if (code
== BIT_AND_EXPR
|| code
== TRUTH_AND_EXPR
)
1175 else if (code
!= BIT_IOR_EXPR
&& code
!= TRUTH_OR_EXPR
)
1178 tree lhs
= gimple_get_lhs (s
);
1179 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (s
));
1180 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (s
));
1182 // Deal with || and && only when there is a full set of symbolics.
1183 if (!lhs
|| !ssa1
|| !ssa2
1184 || (TREE_CODE (TREE_TYPE (lhs
)) != BOOLEAN_TYPE
)
1185 || (TREE_CODE (TREE_TYPE (ssa1
)) != BOOLEAN_TYPE
)
1186 || (TREE_CODE (TREE_TYPE (ssa2
)) != BOOLEAN_TYPE
))
1189 // Now we know its a boolean AND or OR expression with boolean operands.
1190 // Ideally we search dependencies for common names, and see what pops out.
1191 // until then, simply try to resolve direct dependencies.
1193 // Both names will need to have 2 direct dependencies.
1194 tree ssa1_dep2
= src
.gori ()->depend2 (ssa1
);
1195 tree ssa2_dep2
= src
.gori ()->depend2 (ssa2
);
1196 if (!ssa1_dep2
|| !ssa2_dep2
)
1199 tree ssa1_dep1
= src
.gori ()->depend1 (ssa1
);
1200 tree ssa2_dep1
= src
.gori ()->depend1 (ssa2
);
1201 // Make sure they are the same dependencies, and detect the order of the
1203 bool reverse_op2
= true;
1204 if (ssa1_dep1
== ssa2_dep1
&& ssa1_dep2
== ssa2_dep2
)
1205 reverse_op2
= false;
1206 else if (ssa1_dep1
!= ssa2_dep2
|| ssa1_dep2
!= ssa2_dep1
)
1209 range_operator
*handler1
= gimple_range_handler (SSA_NAME_DEF_STMT (ssa1
));
1210 range_operator
*handler2
= gimple_range_handler (SSA_NAME_DEF_STMT (ssa2
));
1212 int_range
<2> bool_one (boolean_true_node
, boolean_true_node
);
1214 relation_kind relation1
= handler1
->op1_op2_relation (bool_one
);
1215 relation_kind relation2
= handler2
->op1_op2_relation (bool_one
);
1216 if (relation1
== VREL_NONE
|| relation2
== VREL_NONE
)
1220 relation2
= relation_negate (relation2
);
1222 // x && y is false if the relation intersection of the true cases is NULL.
1223 if (is_and
&& relation_intersect (relation1
, relation2
) == VREL_EMPTY
)
1224 lhs_range
= int_range
<2> (boolean_false_node
, boolean_false_node
);
1225 // x || y is true if the union of the true cases is NO-RELATION..
1226 // ie, one or the other being true covers the full range of possibilties.
1227 else if (!is_and
&& relation_union (relation1
, relation2
) == VREL_NONE
)
1228 lhs_range
= bool_one
;
1232 range_cast (lhs_range
, TREE_TYPE (lhs
));
1233 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1235 fprintf (dump_file
, " Relation adjustment: ");
1236 print_generic_expr (dump_file
, ssa1
, TDF_SLIM
);
1237 fprintf (dump_file
, " and ");
1238 print_generic_expr (dump_file
, ssa2
, TDF_SLIM
);
1239 fprintf (dump_file
, " combine to produce ");
1240 lhs_range
.dump (dump_file
);
1241 fputc ('\n', dump_file
);
1247 // Register any outgoing edge relations from a conditional branch.
1250 fold_using_range::postfold_gcond_edges (gcond
*s
, fur_source
&src
)
1254 range_operator
*handler
;
1255 basic_block bb
= gimple_bb (s
);
1257 edge e0
= EDGE_SUCC (bb
, 0);
1258 if (!single_pred_p (e0
->dest
))
1261 edge e1
= EDGE_SUCC (bb
, 1);
1262 if (!single_pred_p (e1
->dest
))
1265 // At least one edge needs to be single pred.
1269 // First, register the gcond itself. This will catch statements like
1271 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (s
));
1272 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (s
));
1275 handler
= gimple_range_handler (s
);
1276 gcc_checking_assert (handler
);
1279 gcond_edge_range (r
, e0
);
1280 relation_kind relation
= handler
->op1_op2_relation (r
);
1281 if (relation
!= VREL_NONE
)
1282 src
.register_relation (e0
, relation
, ssa1
, ssa2
);
1286 gcond_edge_range (r
, e1
);
1287 relation_kind relation
= handler
->op1_op2_relation (r
);
1288 if (relation
!= VREL_NONE
)
1289 src
.register_relation (e1
, relation
, ssa1
, ssa2
);
1293 // Outgoing relations of GORI exports require a gori engine.
1297 range_query
*q
= src
.query ();
1298 // Now look for other relations in the exports. This will find stmts
1299 // leading to the condition such as:
1303 FOR_EACH_GORI_EXPORT_NAME (*(src
.gori ()), bb
, name
)
1305 if (TREE_CODE (TREE_TYPE (name
)) != BOOLEAN_TYPE
)
1307 gimple
*stmt
= SSA_NAME_DEF_STMT (name
);
1308 handler
= gimple_range_handler (stmt
);
1311 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (stmt
));
1312 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (stmt
));
1315 if (e0
&& src
.gori ()->outgoing_edge_range_p (r
, e0
, name
, *q
)
1316 && r
.singleton_p ())
1318 relation_kind relation
= handler
->op1_op2_relation (r
);
1319 if (relation
!= VREL_NONE
)
1320 src
.register_relation (e0
, relation
, ssa1
, ssa2
);
1322 if (e1
&& src
.gori ()->outgoing_edge_range_p (r
, e1
, name
, *q
)
1323 && r
.singleton_p ())
1325 relation_kind relation
= handler
->op1_op2_relation (r
);
1326 if (relation
!= VREL_NONE
)
1327 src
.register_relation (e1
, relation
, ssa1
, ssa2
);