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"
31 #include "gimple-pretty-print.h"
32 #include "gimple-iterator.h"
33 #include "optabs-tree.h"
34 #include "gimple-fold.h"
36 #include "fold-const.h"
39 #include "fold-const.h"
40 #include "case-cfn-macros.h"
41 #include "omp-general.h"
43 #include "tree-ssa-loop.h"
44 #include "tree-scalar-evolution.h"
46 #include "alloc-pool.h"
47 #include "vr-values.h"
48 #include "gimple-range.h"
50 // Construct a fur_source, and set the m_query field.
52 fur_source::fur_source (range_query
*q
)
57 m_query
= get_range_query (cfun
);
59 m_query
= get_global_range_query ();
63 // Invoke range_of_expr on EXPR.
66 fur_source::get_operand (irange
&r
, tree expr
)
68 return m_query
->range_of_expr (r
, expr
);
71 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
72 // range_query to get the range on the edge.
75 fur_source::get_phi_operand (irange
&r
, tree expr
, edge e
)
77 return m_query
->range_on_edge (r
, e
, expr
);
80 // Default is no relation.
83 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED
,
84 tree op2 ATTRIBUTE_UNUSED
)
89 // Default registers nothing.
92 fur_source::register_relation (gimple
*s ATTRIBUTE_UNUSED
,
93 relation_kind k ATTRIBUTE_UNUSED
,
94 tree op1 ATTRIBUTE_UNUSED
,
95 tree op2 ATTRIBUTE_UNUSED
)
99 // Default registers nothing.
102 fur_source::register_relation (edge e ATTRIBUTE_UNUSED
,
103 relation_kind k ATTRIBUTE_UNUSED
,
104 tree op1 ATTRIBUTE_UNUSED
,
105 tree op2 ATTRIBUTE_UNUSED
)
109 // This version of fur_source will pick a range up off an edge.
111 class fur_edge
: public fur_source
114 fur_edge (edge e
, range_query
*q
= NULL
);
115 virtual bool get_operand (irange
&r
, tree expr
) OVERRIDE
;
116 virtual bool get_phi_operand (irange
&r
, tree expr
, edge e
) OVERRIDE
;
121 // Instantiate an edge based fur_source.
124 fur_edge::fur_edge (edge e
, range_query
*q
) : fur_source (q
)
129 // Get the value of EXPR on edge m_edge.
132 fur_edge::get_operand (irange
&r
, tree expr
)
134 return m_query
->range_on_edge (r
, m_edge
, expr
);
137 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
138 // range_query to get the range on the edge.
141 fur_edge::get_phi_operand (irange
&r
, tree expr
, edge e
)
143 // Edge to edge recalculations not supoprted yet, until we sort it out.
144 gcc_checking_assert (e
== m_edge
);
145 return m_query
->range_on_edge (r
, e
, expr
);
148 // Instantiate a stmt based fur_source.
150 fur_stmt::fur_stmt (gimple
*s
, range_query
*q
) : fur_source (q
)
155 // Retreive range of EXPR as it occurs as a use on stmt M_STMT.
158 fur_stmt::get_operand (irange
&r
, tree expr
)
160 return m_query
->range_of_expr (r
, expr
, m_stmt
);
163 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
164 // range_query to get the range on the edge.
167 fur_stmt::get_phi_operand (irange
&r
, tree expr
, edge e
)
169 // Pick up the range of expr from edge E.
170 fur_edge
e_src (e
, m_query
);
171 return e_src
.get_operand (r
, expr
);
174 // Return relation based from m_stmt.
177 fur_stmt::query_relation (tree op1
, tree op2
)
179 return m_query
->query_relation (m_stmt
, op1
, op2
);
182 // This version of fur_source will pick a range from a stmt, and also register
183 // dependencies via a gori_compute object. This is mostly an internal API.
185 class fur_depend
: public fur_stmt
188 fur_depend (gimple
*s
, gori_compute
*gori
, range_query
*q
= NULL
);
189 virtual void register_relation (gimple
*stmt
, relation_kind k
, tree op1
,
191 virtual void register_relation (edge e
, relation_kind k
, tree op1
,
194 relation_oracle
*m_oracle
;
197 // Instantiate a stmt based fur_source with a GORI object.
200 fur_depend::fur_depend (gimple
*s
, gori_compute
*gori
, range_query
*q
)
203 gcc_checking_assert (gori
);
205 // Set relations if there is an oracle in the range_query.
206 // This will enable registering of relationships as they are discovered.
207 m_oracle
= q
->oracle ();
211 // Register a relation on a stmt if there is an oracle.
214 fur_depend::register_relation (gimple
*s
, relation_kind k
, tree op1
, tree op2
)
217 m_oracle
->register_relation (s
, k
, op1
, op2
);
220 // Register a relation on an edge if there is an oracle.
223 fur_depend::register_relation (edge e
, relation_kind k
, tree op1
, tree op2
)
226 m_oracle
->register_relation (e
, k
, op1
, op2
);
229 // This version of fur_source will pick a range up from a list of ranges
230 // supplied by the caller.
232 class fur_list
: public fur_source
235 fur_list (irange
&r1
);
236 fur_list (irange
&r1
, irange
&r2
);
237 fur_list (unsigned num
, irange
*list
);
238 virtual bool get_operand (irange
&r
, tree expr
) OVERRIDE
;
239 virtual bool get_phi_operand (irange
&r
, tree expr
, edge e
) OVERRIDE
;
241 int_range_max m_local
[2];
247 // One range supplied for unary operations.
249 fur_list::fur_list (irange
&r1
) : fur_source (NULL
)
257 // Two ranges supplied for binary operations.
259 fur_list::fur_list (irange
&r1
, irange
&r2
) : fur_source (NULL
)
268 // Arbitrary number of ranges in a vector.
270 fur_list::fur_list (unsigned num
, irange
*list
) : fur_source (NULL
)
277 // Get the next operand from the vector, ensure types are compatible.
280 fur_list::get_operand (irange
&r
, tree expr
)
282 if (m_index
>= m_limit
)
283 return m_query
->range_of_expr (r
, expr
);
284 r
= m_list
[m_index
++];
285 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr
), r
.type ()));
289 // This will simply pick the next operand from the vector.
291 fur_list::get_phi_operand (irange
&r
, tree expr
, edge e ATTRIBUTE_UNUSED
)
293 return get_operand (r
, expr
);
296 // Fold stmt S into range R using R1 as the first operand.
299 fold_range (irange
&r
, gimple
*s
, irange
&r1
)
303 return f
.fold_stmt (r
, s
, src
);
306 // Fold stmt S into range R using R1 and R2 as the first two operands.
309 fold_range (irange
&r
, gimple
*s
, irange
&r1
, irange
&r2
)
312 fur_list
src (r1
, r2
);
313 return f
.fold_stmt (r
, s
, src
);
316 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
317 // operands encountered.
320 fold_range (irange
&r
, gimple
*s
, unsigned num_elements
, irange
*vector
)
323 fur_list
src (num_elements
, vector
);
324 return f
.fold_stmt (r
, s
, src
);
327 // Fold stmt S into range R using range query Q.
330 fold_range (irange
&r
, gimple
*s
, range_query
*q
)
334 return f
.fold_stmt (r
, s
, src
);
337 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
340 fold_range (irange
&r
, gimple
*s
, edge on_edge
, range_query
*q
)
343 fur_edge
src (on_edge
, q
);
344 return f
.fold_stmt (r
, s
, src
);
347 // -------------------------------------------------------------------------
349 // Adjust the range for a pointer difference where the operands came
352 // This notices the following sequence:
354 // def = __builtin_memchr (arg, 0, sz)
357 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
360 adjust_pointer_diff_expr (irange
&res
, const gimple
*diff_stmt
)
362 tree op0
= gimple_assign_rhs1 (diff_stmt
);
363 tree op1
= gimple_assign_rhs2 (diff_stmt
);
364 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
365 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
368 if (TREE_CODE (op0
) == SSA_NAME
369 && TREE_CODE (op1
) == SSA_NAME
370 && (call
= SSA_NAME_DEF_STMT (op0
))
371 && is_gimple_call (call
)
372 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
373 && TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
374 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
375 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
376 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
377 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
378 && vrp_operand_equal_p (op1
, gimple_call_arg (call
, 0))
379 && integer_zerop (gimple_call_arg (call
, 1)))
381 tree max
= vrp_val_max (ptrdiff_type_node
);
382 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
383 tree expr_type
= gimple_expr_type (diff_stmt
);
384 tree range_min
= build_zero_cst (expr_type
);
385 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
386 int_range
<2> r (range_min
, range_max
);
391 // This function looks for situations when walking the use/def chains
392 // may provide additonal contextual range information not exposed on
393 // this statement. Like knowing the IMAGPART return value from a
394 // builtin function is a boolean result.
396 // We should rework how we're called, as we have an op_unknown entry
397 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
398 // function gets called.
401 gimple_range_adjustment (irange
&res
, const gimple
*stmt
)
403 switch (gimple_expr_code (stmt
))
405 case POINTER_DIFF_EXPR
:
406 adjust_pointer_diff_expr (res
, stmt
);
411 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
412 if (TREE_CODE (name
) == SSA_NAME
)
414 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
415 if (def_stmt
&& is_gimple_call (def_stmt
)
416 && gimple_call_internal_p (def_stmt
))
418 switch (gimple_call_internal_fn (def_stmt
))
420 case IFN_ADD_OVERFLOW
:
421 case IFN_SUB_OVERFLOW
:
422 case IFN_MUL_OVERFLOW
:
423 case IFN_ATOMIC_COMPARE_EXCHANGE
:
426 r
.set_varying (boolean_type_node
);
427 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
428 range_cast (r
, type
);
444 // Return the base of the RHS of an assignment.
447 gimple_range_base_of_assignment (const gimple
*stmt
)
449 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
450 tree op1
= gimple_assign_rhs1 (stmt
);
451 if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
452 return get_base_address (TREE_OPERAND (op1
, 0));
456 // Return the first operand of this statement if it is a valid operand
457 // supported by ranges, otherwise return NULL_TREE. Special case is
458 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
461 gimple_range_operand1 (const gimple
*stmt
)
463 gcc_checking_assert (gimple_range_handler (stmt
));
465 switch (gimple_code (stmt
))
468 return gimple_cond_lhs (stmt
);
471 tree base
= gimple_range_base_of_assignment (stmt
);
472 if (base
&& TREE_CODE (base
) == MEM_REF
)
474 // If the base address is an SSA_NAME, we return it
475 // here. This allows processing of the range of that
476 // name, while the rest of the expression is simply
477 // ignored. The code in range_ops will see the
478 // ADDR_EXPR and do the right thing.
479 tree ssa
= TREE_OPERAND (base
, 0);
480 if (TREE_CODE (ssa
) == SSA_NAME
)
491 // Return the second operand of statement STMT, otherwise return NULL_TREE.
494 gimple_range_operand2 (const gimple
*stmt
)
496 gcc_checking_assert (gimple_range_handler (stmt
));
498 switch (gimple_code (stmt
))
501 return gimple_cond_rhs (stmt
);
503 if (gimple_num_ops (stmt
) >= 3)
504 return gimple_assign_rhs2 (stmt
);
511 // Calculate what we can determine of the range of this unary
512 // statement's operand if the lhs of the expression has the range
513 // LHS_RANGE. Return false if nothing can be determined.
516 gimple_range_calc_op1 (irange
&r
, const gimple
*stmt
, const irange
&lhs_range
)
518 gcc_checking_assert (gimple_num_ops (stmt
) < 3);
520 // An empty range is viral.
521 tree type
= TREE_TYPE (gimple_range_operand1 (stmt
));
522 if (lhs_range
.undefined_p ())
527 // Unary operations require the type of the first operand in the
528 // second range position.
529 int_range
<2> type_range (type
);
530 return gimple_range_handler (stmt
)->op1_range (r
, type
, lhs_range
,
534 // Calculate what we can determine of the range of this statement's
535 // first operand if the lhs of the expression has the range LHS_RANGE
536 // and the second operand has the range OP2_RANGE. Return false if
537 // nothing can be determined.
540 gimple_range_calc_op1 (irange
&r
, const gimple
*stmt
,
541 const irange
&lhs_range
, const irange
&op2_range
)
543 // Unary operation are allowed to pass a range in for second operand
544 // as there are often additional restrictions beyond the type which
545 // can be imposed. See operator_cast::op1_range().
546 tree type
= TREE_TYPE (gimple_range_operand1 (stmt
));
547 // An empty range is viral.
548 if (op2_range
.undefined_p () || lhs_range
.undefined_p ())
553 return gimple_range_handler (stmt
)->op1_range (r
, type
, lhs_range
,
557 // Calculate what we can determine of the range of this statement's
558 // second operand if the lhs of the expression has the range LHS_RANGE
559 // and the first operand has the range OP1_RANGE. Return false if
560 // nothing can be determined.
563 gimple_range_calc_op2 (irange
&r
, const gimple
*stmt
,
564 const irange
&lhs_range
, const irange
&op1_range
)
566 tree type
= TREE_TYPE (gimple_range_operand2 (stmt
));
567 // An empty range is viral.
568 if (op1_range
.undefined_p () || lhs_range
.undefined_p ())
573 return gimple_range_handler (stmt
)->op2_range (r
, type
, lhs_range
,
577 // Calculate a range for statement S and return it in R. If NAME is provided it
578 // represents the SSA_NAME on the LHS of the statement. It is only required
579 // if there is more than one lhs/output. If a range cannot
580 // be calculated, return false.
583 fold_using_range::fold_stmt (irange
&r
, gimple
*s
, fur_source
&src
, tree name
)
586 // If name and S are specified, make sure it is an LHS of S.
587 gcc_checking_assert (!name
|| !gimple_get_lhs (s
) ||
588 name
== gimple_get_lhs (s
));
591 name
= gimple_get_lhs (s
);
593 // Process addresses.
594 if (gimple_code (s
) == GIMPLE_ASSIGN
595 && gimple_assign_rhs_code (s
) == ADDR_EXPR
)
596 return range_of_address (r
, s
, src
);
598 if (gimple_range_handler (s
))
599 res
= range_of_range_op (r
, s
, src
);
600 else if (is_a
<gphi
*>(s
))
601 res
= range_of_phi (r
, as_a
<gphi
*> (s
), src
);
602 else if (is_a
<gcall
*>(s
))
603 res
= range_of_call (r
, as_a
<gcall
*> (s
), src
);
604 else if (is_a
<gassign
*> (s
) && gimple_assign_rhs_code (s
) == COND_EXPR
)
605 res
= range_of_cond_expr (r
, as_a
<gassign
*> (s
), src
);
609 // If no name is specified, try the expression kind.
612 tree t
= gimple_expr_type (s
);
613 if (!irange::supports_type_p (t
))
618 if (!gimple_range_ssa_p (name
))
620 // We don't understand the stmt, so return the global range.
621 r
= gimple_range_global (name
);
625 if (r
.undefined_p ())
628 // We sometimes get compatible types copied from operands, make sure
629 // the correct type is being returned.
630 if (name
&& TREE_TYPE (name
) != r
.type ())
632 gcc_checking_assert (range_compatible_p (r
.type (), TREE_TYPE (name
)));
633 range_cast (r
, TREE_TYPE (name
));
638 // Calculate a range for range_op statement S and return it in R. If any
639 // If a range cannot be calculated, return false.
642 fold_using_range::range_of_range_op (irange
&r
, gimple
*s
, fur_source
&src
)
644 int_range_max range1
, range2
;
645 tree type
= gimple_expr_type (s
);
646 range_operator
*handler
= gimple_range_handler (s
);
647 gcc_checking_assert (handler
);
648 gcc_checking_assert (irange::supports_type_p (type
));
650 tree lhs
= gimple_get_lhs (s
);
651 tree op1
= gimple_range_operand1 (s
);
652 tree op2
= gimple_range_operand2 (s
);
654 if (src
.get_operand (range1
, op1
))
658 // Fold range, and register any dependency if available.
659 int_range
<2> r2 (type
);
660 handler
->fold_range (r
, type
, range1
, r2
);
661 if (lhs
&& gimple_range_ssa_p (op1
))
664 src
.gori ()->register_dependency (lhs
, op1
);
666 rel
= handler
->lhs_op1_relation (r
, range1
, range1
);
667 if (rel
!= VREL_NONE
)
668 src
.register_relation (s
, rel
, lhs
, op1
);
671 else if (src
.get_operand (range2
, op2
))
673 relation_kind rel
= src
.query_relation (op1
, op2
);
674 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && rel
!= VREL_NONE
)
676 fprintf (dump_file
, " folding with relation ");
677 print_relation (dump_file
, rel
);
678 fputc ('\n', dump_file
);
680 // Fold range, and register any dependency if available.
681 handler
->fold_range (r
, type
, range1
, range2
, rel
);
682 relation_fold_and_or (r
, s
, src
);
687 src
.gori ()->register_dependency (lhs
, op1
);
688 src
.gori ()->register_dependency (lhs
, op2
);
690 if (gimple_range_ssa_p (op1
))
692 rel
= handler
->lhs_op1_relation (r
, range1
, range2
);
693 if (rel
!= VREL_NONE
)
694 src
.register_relation (s
, rel
, lhs
, op1
);
696 if (gimple_range_ssa_p (op2
))
698 rel
= handler
->lhs_op2_relation (r
, range1
, range2
);
699 if (rel
!= VREL_NONE
)
700 src
.register_relation (s
, rel
, lhs
, op2
);
703 else if (is_a
<gcond
*> (s
))
704 postfold_gcond_edges (as_a
<gcond
*> (s
), src
);
707 r
.set_varying (type
);
710 r
.set_varying (type
);
711 // Make certain range-op adjustments that aren't handled any other way.
712 gimple_range_adjustment (r
, s
);
716 // Calculate the range of an assignment containing an ADDR_EXPR.
717 // Return the range in R.
718 // If a range cannot be calculated, set it to VARYING and return true.
721 fold_using_range::range_of_address (irange
&r
, gimple
*stmt
, fur_source
&src
)
723 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
724 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
);
726 bool strict_overflow_p
;
727 tree expr
= gimple_assign_rhs1 (stmt
);
728 poly_int64 bitsize
, bitpos
;
731 int unsignedp
, reversep
, volatilep
;
732 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
733 &bitpos
, &offset
, &mode
, &unsignedp
,
734 &reversep
, &volatilep
);
737 if (base
!= NULL_TREE
738 && TREE_CODE (base
) == MEM_REF
739 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
741 tree ssa
= TREE_OPERAND (base
, 0);
742 tree lhs
= gimple_get_lhs (stmt
);
743 if (lhs
&& gimple_range_ssa_p (ssa
) && src
.gori ())
744 src
.gori ()->register_dependency (lhs
, ssa
);
745 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa
)));
746 src
.get_operand (r
, ssa
);
747 range_cast (r
, TREE_TYPE (gimple_assign_rhs1 (stmt
)));
749 poly_offset_int off
= 0;
750 bool off_cst
= false;
751 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
753 off
= mem_ref_offset (base
);
755 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
757 off
<<= LOG2_BITS_PER_UNIT
;
761 /* If &X->a is equal to X, the range of X is the result. */
762 if (off_cst
&& known_eq (off
, 0))
764 else if (flag_delete_null_pointer_checks
765 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
)))
767 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
768 allow going from non-NULL pointer to NULL. */
769 if(!range_includes_zero_p (&r
))
772 /* If MEM_REF has a "positive" offset, consider it non-NULL
773 always, for -fdelete-null-pointer-checks also "negative"
774 ones. Punt for unknown offsets (e.g. variable ones). */
775 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
778 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
780 r
= range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
783 r
= int_range
<2> (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
788 if (tree_single_nonzero_warnv_p (expr
, &strict_overflow_p
))
790 r
= range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
794 // Otherwise return varying.
795 r
= int_range
<2> (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
799 // Calculate a range for phi statement S and return it in R.
800 // If a range cannot be calculated, return false.
803 fold_using_range::range_of_phi (irange
&r
, gphi
*phi
, fur_source
&src
)
805 tree phi_def
= gimple_phi_result (phi
);
806 tree type
= TREE_TYPE (phi_def
);
807 int_range_max arg_range
;
810 if (!irange::supports_type_p (type
))
813 // Start with an empty range, unioning in each argument's range.
815 for (x
= 0; x
< gimple_phi_num_args (phi
); x
++)
817 tree arg
= gimple_phi_arg_def (phi
, x
);
818 edge e
= gimple_phi_arg_edge (phi
, x
);
820 // Register potential dependencies for stale value tracking.
821 if (gimple_range_ssa_p (arg
) && src
.gori ())
822 src
.gori ()->register_dependency (phi_def
, arg
);
824 // Get the range of the argument on its edge.
825 src
.get_phi_operand (arg_range
, arg
, e
);
826 // If we're recomputing the argument elsewhere, try to refine it.
827 r
.union_ (arg_range
);
828 // Once the value reaches varying, stop looking.
833 // If SCEV is available, query if this PHI has any knonwn values.
834 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def
)))
836 value_range loop_range
;
837 class loop
*l
= loop_containing_stmt (phi
);
838 if (l
&& loop_outer (l
))
840 range_of_ssa_name_with_loop_info (loop_range
, phi_def
, l
, phi
, src
);
841 if (!loop_range
.varying_p ())
843 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
845 fprintf (dump_file
, " Loops range found for ");
846 print_generic_expr (dump_file
, phi_def
, TDF_SLIM
);
847 fprintf (dump_file
, ": ");
848 loop_range
.dump (dump_file
);
849 fprintf (dump_file
, " and calculated range :");
851 fprintf (dump_file
, "\n");
853 r
.intersect (loop_range
);
861 // Calculate a range for call statement S and return it in R.
862 // If a range cannot be calculated, return false.
865 fold_using_range::range_of_call (irange
&r
, gcall
*call
, fur_source
&src
)
867 tree type
= gimple_call_return_type (call
);
868 tree lhs
= gimple_call_lhs (call
);
869 bool strict_overflow_p
;
871 if (!irange::supports_type_p (type
))
874 if (range_of_builtin_call (r
, call
, src
))
876 else if (gimple_stmt_nonnegative_warnv_p (call
, &strict_overflow_p
))
877 r
.set (build_int_cst (type
, 0), TYPE_MAX_VALUE (type
));
878 else if (gimple_call_nonnull_result_p (call
)
879 || gimple_call_nonnull_arg (call
))
880 r
= range_nonzero (type
);
882 r
.set_varying (type
);
884 // If there is an LHS, intersect that with what is known.
888 def
= gimple_range_global (lhs
);
894 // Return the range of a __builtin_ubsan* in CALL and set it in R.
895 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
899 fold_using_range::range_of_builtin_ubsan_call (irange
&r
, gcall
*call
,
900 tree_code code
, fur_source
&src
)
902 gcc_checking_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
903 || code
== MULT_EXPR
);
904 tree type
= gimple_call_return_type (call
);
905 range_operator
*op
= range_op_handler (code
, type
);
906 gcc_checking_assert (op
);
907 int_range_max ir0
, ir1
;
908 tree arg0
= gimple_call_arg (call
, 0);
909 tree arg1
= gimple_call_arg (call
, 1);
910 src
.get_operand (ir0
, arg0
);
911 src
.get_operand (ir1
, arg1
);
913 bool saved_flag_wrapv
= flag_wrapv
;
914 // Pretend the arithmetic is wrapping. If there is any overflow,
915 // we'll complain, but will actually do wrapping operation.
917 op
->fold_range (r
, type
, ir0
, ir1
);
918 flag_wrapv
= saved_flag_wrapv
;
920 // If for both arguments vrp_valueize returned non-NULL, this should
921 // have been already folded and if not, it wasn't folded because of
922 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
923 if (r
.singleton_p ())
924 r
.set_varying (type
);
927 // For a builtin in CALL, return a range in R if known and return
928 // TRUE. Otherwise return FALSE.
931 fold_using_range::range_of_builtin_call (irange
&r
, gcall
*call
,
934 combined_fn func
= gimple_call_combined_fn (call
);
935 if (func
== CFN_LAST
)
938 tree type
= gimple_call_return_type (call
);
940 int mini
, maxi
, zerov
= 0, prec
;
941 scalar_int_mode mode
;
945 case CFN_BUILT_IN_CONSTANT_P
:
946 if (cfun
->after_inlining
)
952 arg
= gimple_call_arg (call
, 0);
953 if (src
.get_operand (r
, arg
) && r
.singleton_p ())
955 r
.set (build_one_cst (type
), build_one_cst (type
));
962 // __builtin_ffs* and __builtin_popcount* return [0, prec].
963 arg
= gimple_call_arg (call
, 0);
964 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
967 src
.get_operand (r
, arg
);
968 // If arg is non-zero, then ffs or popcount are non-zero.
969 if (!range_includes_zero_p (&r
))
971 // If some high bits are known to be zero, decrease the maximum.
972 if (!r
.undefined_p ())
974 if (TYPE_SIGN (r
.type ()) == SIGNED
)
975 range_cast (r
, unsigned_type_for (r
.type ()));
976 wide_int max
= r
.upper_bound ();
977 maxi
= wi::floor_log2 (max
) + 1;
979 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
983 r
.set (build_zero_cst (type
), build_one_cst (type
));
987 // __builtin_c[lt]z* return [0, prec-1], except when the
988 // argument is 0, but that is undefined behavior.
990 // For __builtin_c[lt]z* consider argument of 0 always undefined
991 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
992 arg
= gimple_call_arg (call
, 0);
993 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
996 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
997 if (gimple_call_internal_p (call
))
999 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
1000 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
1002 // Only handle the single common value.
1006 // Magic value to give up, unless we can prove arg is non-zero.
1011 src
.get_operand (r
, arg
);
1012 // From clz of minimum we can compute result maximum.
1013 if (!r
.undefined_p ())
1015 // From clz of minimum we can compute result maximum.
1016 if (wi::gt_p (r
.lower_bound (), 0, TYPE_SIGN (r
.type ())))
1018 maxi
= prec
- 1 - wi::floor_log2 (r
.lower_bound ());
1022 else if (!range_includes_zero_p (&r
))
1029 // From clz of maximum we can compute result minimum.
1030 wide_int max
= r
.upper_bound ();
1031 int newmini
= prec
- 1 - wi::floor_log2 (max
);
1034 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
1035 // return [prec, prec], otherwise ignore the range.
1044 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
1048 // __builtin_ctz* return [0, prec-1], except for when the
1049 // argument is 0, but that is undefined behavior.
1051 // For __builtin_ctz* consider argument of 0 always undefined
1052 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
1053 arg
= gimple_call_arg (call
, 0);
1054 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1057 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1058 if (gimple_call_internal_p (call
))
1060 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
1061 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
) == 2)
1063 // Handle only the two common values.
1066 else if (zerov
== prec
)
1069 // Magic value to give up, unless we can prove arg is non-zero.
1073 src
.get_operand (r
, arg
);
1074 if (!r
.undefined_p ())
1076 // If arg is non-zero, then use [0, prec - 1].
1077 if (!range_includes_zero_p (&r
))
1082 // If some high bits are known to be zero, we can decrease
1084 wide_int max
= r
.upper_bound ();
1087 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1088 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1089 // Otherwise ignore the range.
1092 else if (maxi
== prec
)
1095 // If value at zero is prec and 0 is in the range, we can't lower
1096 // the upper bound. We could create two separate ranges though,
1097 // [0,floor_log2(max)][prec,prec] though.
1098 else if (maxi
!= prec
)
1099 maxi
= wi::floor_log2 (max
);
1103 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
1107 arg
= gimple_call_arg (call
, 0);
1108 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1109 r
.set (build_int_cst (type
, 0), build_int_cst (type
, prec
- 1));
1111 case CFN_UBSAN_CHECK_ADD
:
1112 range_of_builtin_ubsan_call (r
, call
, PLUS_EXPR
, src
);
1114 case CFN_UBSAN_CHECK_SUB
:
1115 range_of_builtin_ubsan_call (r
, call
, MINUS_EXPR
, src
);
1117 case CFN_UBSAN_CHECK_MUL
:
1118 range_of_builtin_ubsan_call (r
, call
, MULT_EXPR
, src
);
1121 case CFN_GOACC_DIM_SIZE
:
1122 case CFN_GOACC_DIM_POS
:
1123 // Optimizing these two internal functions helps the loop
1124 // optimizer eliminate outer comparisons. Size is [1,N]
1125 // and pos is [0,N-1].
1127 bool is_pos
= func
== CFN_GOACC_DIM_POS
;
1128 int axis
= oacc_get_ifn_dim_arg (call
);
1129 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1131 // If it's dynamic, the backend might know a hardware limitation.
1132 size
= targetm
.goacc
.dim_limit (axis
);
1134 r
.set (build_int_cst (type
, is_pos
? 0 : 1),
1136 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1140 case CFN_BUILT_IN_STRLEN
:
1141 if (tree lhs
= gimple_call_lhs (call
))
1142 if (ptrdiff_type_node
1143 && (TYPE_PRECISION (ptrdiff_type_node
)
1144 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1146 tree type
= TREE_TYPE (lhs
);
1147 tree max
= vrp_val_max (ptrdiff_type_node
);
1149 = wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1150 tree range_min
= build_zero_cst (type
);
1151 // To account for the terminating NULL, the maximum length
1152 // is one less than the maximum array size, which in turn
1153 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1154 // smaller than the former type).
1155 // FIXME: Use max_object_size() - 1 here.
1156 tree range_max
= wide_int_to_tree (type
, wmax
- 2);
1157 r
.set (range_min
, range_max
);
1168 // Calculate a range for COND_EXPR statement S and return it in R.
1169 // If a range cannot be calculated, return false.
1172 fold_using_range::range_of_cond_expr (irange
&r
, gassign
*s
, fur_source
&src
)
1174 int_range_max cond_range
, range1
, range2
;
1175 tree cond
= gimple_assign_rhs1 (s
);
1176 tree op1
= gimple_assign_rhs2 (s
);
1177 tree op2
= gimple_assign_rhs3 (s
);
1179 gcc_checking_assert (gimple_assign_rhs_code (s
) == COND_EXPR
);
1180 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1
),
1182 if (!irange::supports_type_p (TREE_TYPE (op1
)))
1185 src
.get_operand (cond_range
, cond
);
1186 src
.get_operand (range1
, op1
);
1187 src
.get_operand (range2
, op2
);
1189 // If the condition is known, choose the appropriate expression.
1190 if (cond_range
.singleton_p ())
1192 // False, pick second operand.
1193 if (cond_range
.zero_p ())
1206 gimple_ranger::gimple_ranger ()
1208 // If the cache has a relation oracle, use it.
1209 m_oracle
= m_cache
.oracle ();
1213 gimple_ranger::range_of_expr (irange
&r
, tree expr
, gimple
*stmt
)
1215 if (!gimple_range_ssa_p (expr
))
1216 return get_tree_range (r
, expr
, stmt
);
1218 // If there is no statement, just get the global value.
1221 if (!m_cache
.get_global_range (r
, expr
))
1222 r
= gimple_range_global (expr
);
1226 // For a debug stmt, pick the best value currently available, do not
1227 // trigger new value calculations. PR 100781.
1228 if (is_gimple_debug (stmt
))
1230 m_cache
.range_of_expr (r
, expr
, stmt
);
1233 basic_block bb
= gimple_bb (stmt
);
1234 gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
1236 // If name is defined in this block, try to get an range from S.
1237 if (def_stmt
&& gimple_bb (def_stmt
) == bb
)
1239 range_of_stmt (r
, def_stmt
, expr
);
1240 if (!cfun
->can_throw_non_call_exceptions
&& r
.varying_p () &&
1241 m_cache
.m_non_null
.non_null_deref_p (expr
, bb
))
1242 r
= range_nonzero (TREE_TYPE (expr
));
1245 // Otherwise OP comes from outside this block, use range on entry.
1246 range_on_entry (r
, bb
, expr
);
1251 // Return the range of NAME on entry to block BB in R.
1254 gimple_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
1256 int_range_max entry_range
;
1257 gcc_checking_assert (gimple_range_ssa_p (name
));
1259 // Start with any known range
1260 range_of_stmt (r
, SSA_NAME_DEF_STMT (name
), name
);
1262 // Now see if there is any on_entry value which may refine it.
1263 if (m_cache
.block_range (entry_range
, bb
, name
))
1264 r
.intersect (entry_range
);
1266 if (!cfun
->can_throw_non_call_exceptions
&& r
.varying_p () &&
1267 m_cache
.m_non_null
.non_null_deref_p (name
, bb
))
1268 r
= range_nonzero (TREE_TYPE (name
));
1271 // Calculate the range for NAME at the end of block BB and return it in R.
1272 // Return false if no range can be calculated.
1275 gimple_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
1277 // on-exit from the exit block?
1278 gcc_checking_assert (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
1279 gcc_checking_assert (gimple_range_ssa_p (name
));
1281 gimple
*s
= SSA_NAME_DEF_STMT (name
);
1282 basic_block def_bb
= gimple_bb (s
);
1283 // If this is not the definition block, get the range on the last stmt in
1284 // the block... if there is one.
1287 // If there is no statement provided, get the range_on_entry for this block.
1289 range_of_expr (r
, name
, s
);
1291 range_on_entry (r
, bb
, name
);
1292 gcc_checking_assert (r
.undefined_p ()
1293 || range_compatible_p (r
.type (), TREE_TYPE (name
)));
1296 // Calculate a range for NAME on edge E and return it in R.
1299 gimple_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
1301 int_range_max edge_range
;
1302 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name
)));
1304 // PHI arguments can be constants, catch these here.
1305 if (!gimple_range_ssa_p (name
))
1306 return range_of_expr (r
, name
);
1308 range_on_exit (r
, e
->src
, name
);
1309 gcc_checking_assert (r
.undefined_p ()
1310 || range_compatible_p (r
.type(), TREE_TYPE (name
)));
1312 // Check to see if NAME is defined on edge e.
1313 if (m_cache
.range_on_edge (edge_range
, e
, name
))
1314 r
.intersect (edge_range
);
1319 // fold_range wrapper for range_of_stmt to use as an internal client.
1322 gimple_ranger::fold_range_internal (irange
&r
, gimple
*s
, tree name
)
1325 fur_depend
src (s
, &(gori ()), this);
1326 return f
.fold_stmt (r
, s
, src
, name
);
1329 // Calculate a range for statement S and return it in R. If NAME is
1330 // provided it represents the SSA_NAME on the LHS of the statement.
1331 // It is only required if there is more than one lhs/output. Check
1332 // the global cache for NAME first to see if the evaluation can be
1333 // avoided. If a range cannot be calculated, return false and UNDEFINED.
1336 gimple_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
1341 name
= gimple_get_lhs (s
);
1343 // If no name, simply call the base routine.
1345 return fold_range_internal (r
, s
, NULL_TREE
);
1347 if (!gimple_range_ssa_p (name
))
1350 // Check if the stmt has already been processed, and is not stale.
1351 if (m_cache
.get_non_stale_global_range (r
, name
))
1354 // Otherwise calculate a new value.
1356 fold_range_internal (tmp
, s
, name
);
1358 // Combine the new value with the old value. This is required because
1359 // the way value propagation works, when the IL changes on the fly we
1360 // can sometimes get different results. See PR 97741.
1362 m_cache
.set_global_range (name
, r
);
1367 // This routine will export whatever global ranges are known to GCC
1368 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
1371 gimple_ranger::export_global_ranges ()
1377 fprintf (dump_file
, "Exported global range table\n");
1378 fprintf (dump_file
, "===========================\n");
1381 for ( x
= 1; x
< num_ssa_names
; x
++)
1383 tree name
= ssa_name (x
);
1384 if (name
&& !SSA_NAME_IN_FREE_LIST (name
)
1385 && gimple_range_ssa_p (name
)
1386 && m_cache
.get_global_range (r
, name
)
1389 bool updated
= update_global_range (r
, name
);
1391 if (updated
&& dump_file
)
1394 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1395 fprintf (dump_file
, " --> ");
1396 vr
.dump (dump_file
);
1397 fprintf (dump_file
, "\n");
1398 int_range_max same
= vr
;
1401 fprintf (dump_file
, " irange : ");
1403 fprintf (dump_file
, "\n");
1410 // Print the known table values to file F.
1413 gimple_ranger::dump_bb (FILE *f
, basic_block bb
)
1418 int_range_max range
;
1419 fprintf (f
, "\n=========== BB %d ============\n", bb
->index
);
1420 m_cache
.dump_bb (f
, bb
);
1422 ::dump_bb (f
, bb
, 4, TDF_NONE
);
1424 // Now find any globals defined in this block.
1425 for (x
= 1; x
< num_ssa_names
; x
++)
1427 tree name
= ssa_name (x
);
1428 if (gimple_range_ssa_p (name
) && SSA_NAME_DEF_STMT (name
) &&
1429 gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
&&
1430 m_cache
.get_global_range (range
, name
))
1432 if (!range
.varying_p ())
1434 print_generic_expr (f
, name
, TDF_SLIM
);
1443 // And now outgoing edges, if they define anything.
1444 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1446 for (x
= 1; x
< num_ssa_names
; x
++)
1448 tree name
= gimple_range_ssa_p (ssa_name (x
));
1449 if (name
&& gori ().has_edge_range_p (name
, e
)
1450 && m_cache
.range_on_edge (range
, e
, name
))
1452 gimple
*s
= SSA_NAME_DEF_STMT (name
);
1453 // Only print the range if this is the def block, or
1454 // the on entry cache for either end of the edge is
1456 if ((s
&& bb
== gimple_bb (s
)) ||
1457 m_cache
.block_range (range
, bb
, name
, false) ||
1458 m_cache
.block_range (range
, e
->dest
, name
, false))
1460 m_cache
.range_on_edge (range
, e
, name
);
1461 if (!range
.varying_p ())
1463 fprintf (f
, "%d->%d ", e
->src
->index
,
1466 if (e
->flags
& EDGE_TRUE_VALUE
)
1467 fprintf (f
, " (T)%c", c
);
1468 else if (e
->flags
& EDGE_FALSE_VALUE
)
1469 fprintf (f
, " (F)%c", c
);
1472 print_generic_expr (f
, name
, TDF_SLIM
);
1473 fprintf(f
, " : \t");
1483 // Print the known table values to file F.
1486 gimple_ranger::dump (FILE *f
)
1490 FOR_EACH_BB_FN (bb
, cfun
)
1496 // If SCEV has any information about phi node NAME, return it as a range in R.
1499 fold_using_range::range_of_ssa_name_with_loop_info (irange
&r
, tree name
,
1500 class loop
*l
, gphi
*phi
,
1503 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
1504 tree min
, max
, type
= TREE_TYPE (name
);
1505 if (bounds_of_var_in_loop (&min
, &max
, src
.query (), l
, phi
, name
))
1507 if (TREE_CODE (min
) != INTEGER_CST
)
1509 if (src
.query ()->range_of_expr (r
, min
, phi
) && !r
.undefined_p ())
1510 min
= wide_int_to_tree (type
, r
.lower_bound ());
1512 min
= vrp_val_min (type
);
1514 if (TREE_CODE (max
) != INTEGER_CST
)
1516 if (src
.query ()->range_of_expr (r
, max
, phi
) && !r
.undefined_p ())
1517 max
= wide_int_to_tree (type
, r
.upper_bound ());
1519 max
= vrp_val_max (type
);
1524 r
.set_varying (type
);
1527 // -----------------------------------------------------------------------
1529 // Check if an && or || expression can be folded based on relations. ie
1533 // c_2 and c_3 can never be true at the same time,
1534 // Therefore c_4 can always resolve to false based purely on the relations.
1537 fold_using_range::relation_fold_and_or (irange
& lhs_range
, gimple
*s
,
1540 // No queries or already folded.
1541 if (!src
.gori () || !src
.query ()->oracle () || lhs_range
.singleton_p ())
1544 // Only care about AND and OR expressions.
1545 enum tree_code code
= gimple_expr_code (s
);
1546 bool is_and
= false;
1547 if (code
== BIT_AND_EXPR
|| code
== TRUTH_AND_EXPR
)
1549 else if (code
!= BIT_IOR_EXPR
&& code
!= TRUTH_OR_EXPR
)
1552 tree lhs
= gimple_get_lhs (s
);
1553 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (s
));
1554 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (s
));
1556 // Deal with || and && only when there is a full set of symbolics.
1557 if (!lhs
|| !ssa1
|| !ssa2
1558 || (TREE_CODE (TREE_TYPE (lhs
)) != BOOLEAN_TYPE
)
1559 || (TREE_CODE (TREE_TYPE (ssa1
)) != BOOLEAN_TYPE
)
1560 || (TREE_CODE (TREE_TYPE (ssa2
)) != BOOLEAN_TYPE
))
1563 // Now we know its a boolean AND or OR expression with boolean operands.
1564 // Ideally we search dependencies for common names, and see what pops out.
1565 // until then, simply try to resolve direct dependencies.
1567 // Both names will need to have 2 direct dependencies.
1568 tree ssa1_dep2
= src
.gori ()->depend2 (ssa1
);
1569 tree ssa2_dep2
= src
.gori ()->depend2 (ssa2
);
1570 if (!ssa1_dep2
|| !ssa2_dep2
)
1573 tree ssa1_dep1
= src
.gori ()->depend1 (ssa1
);
1574 tree ssa2_dep1
= src
.gori ()->depend1 (ssa2
);
1575 // Make sure they are the same dependencies, and detect the order of the
1577 bool reverse_op2
= true;
1578 if (ssa1_dep1
== ssa2_dep1
&& ssa1_dep2
== ssa2_dep2
)
1579 reverse_op2
= false;
1580 else if (ssa1_dep1
!= ssa2_dep2
|| ssa1_dep2
!= ssa2_dep1
)
1583 range_operator
*handler1
= gimple_range_handler (SSA_NAME_DEF_STMT (ssa1
));
1584 range_operator
*handler2
= gimple_range_handler (SSA_NAME_DEF_STMT (ssa2
));
1586 int_range
<2> bool_one (boolean_true_node
, boolean_true_node
);
1588 relation_kind relation1
= handler1
->op1_op2_relation (bool_one
);
1589 relation_kind relation2
= handler2
->op1_op2_relation (bool_one
);
1590 if (relation1
== VREL_NONE
|| relation2
== VREL_NONE
)
1594 relation2
= relation_negate (relation2
);
1596 // x && y is false if the relation intersection of the true cases is NULL.
1597 if (is_and
&& relation_intersect (relation1
, relation2
) == VREL_EMPTY
)
1598 lhs_range
= int_range
<2> (boolean_false_node
, boolean_false_node
);
1599 // x || y is true if the union of the true cases is NO-RELATION..
1600 // ie, one or the other being true covers the full range of possibilties.
1601 else if (!is_and
&& relation_union (relation1
, relation2
) == VREL_NONE
)
1602 lhs_range
= bool_one
;
1606 range_cast (lhs_range
, TREE_TYPE (lhs
));
1607 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1609 fprintf (dump_file
, " Relation adjustment: ");
1610 print_generic_expr (dump_file
, ssa1
, TDF_SLIM
);
1611 fprintf (dump_file
, " and ");
1612 print_generic_expr (dump_file
, ssa2
, TDF_SLIM
);
1613 fprintf (dump_file
, " combine to produce ");
1614 lhs_range
.dump (dump_file
);
1615 fputc ('\n', dump_file
);
1621 // Register any outgoing edge relations from a conditional branch.
1624 fold_using_range::postfold_gcond_edges (gcond
*s
, fur_source
&src
)
1628 range_operator
*handler
;
1629 basic_block bb
= gimple_bb (s
);
1631 edge e0
= EDGE_SUCC (bb
, 0);
1632 if (!single_pred_p (e0
->dest
))
1635 edge e1
= EDGE_SUCC (bb
, 1);
1636 if (!single_pred_p (e1
->dest
))
1639 // At least one edge needs to be single pred.
1643 // First, register the gcond itself. This will catch statements like
1645 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (s
));
1646 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (s
));
1649 handler
= gimple_range_handler (s
);
1650 gcc_checking_assert (handler
);
1653 gcond_edge_range (r
, e0
);
1654 relation_kind relation
= handler
->op1_op2_relation (r
);
1655 if (relation
!= VREL_NONE
)
1656 src
.register_relation (e0
, relation
, ssa1
, ssa2
);
1660 gcond_edge_range (r
, e1
);
1661 relation_kind relation
= handler
->op1_op2_relation (r
);
1662 if (relation
!= VREL_NONE
)
1663 src
.register_relation (e1
, relation
, ssa1
, ssa2
);
1667 // Outgoing relations of GORI exports require a gori engine.
1671 range_query
*q
= src
.query ();
1672 // Now look for other relations in the exports. This will find stmts
1673 // leading to the condition such as:
1677 FOR_EACH_GORI_EXPORT_NAME (*(src
.gori ()), bb
, name
)
1679 if (TREE_CODE (TREE_TYPE (name
)) != BOOLEAN_TYPE
)
1681 gimple
*stmt
= SSA_NAME_DEF_STMT (name
);
1682 handler
= gimple_range_handler (stmt
);
1685 tree ssa1
= gimple_range_ssa_p (gimple_range_operand1 (stmt
));
1686 tree ssa2
= gimple_range_ssa_p (gimple_range_operand2 (stmt
));
1689 if (e0
&& src
.gori ()->outgoing_edge_range_p (r
, e0
, name
, *q
)
1690 && r
.singleton_p ())
1692 relation_kind relation
= handler
->op1_op2_relation (r
);
1693 if (relation
!= VREL_NONE
)
1694 src
.register_relation (e0
, relation
, ssa1
, ssa2
);
1696 if (e1
&& src
.gori ()->outgoing_edge_range_p (r
, e1
, name
, *q
)
1697 && r
.singleton_p ())
1699 relation_kind relation
= handler
->op1_op2_relation (r
);
1700 if (relation
!= VREL_NONE
)
1701 src
.register_relation (e1
, relation
, ssa1
, ssa2
);
1706 // --------------------------------------------------------------------------
1707 // trace_ranger implementation.
1710 trace_ranger::trace_ranger ()
1716 // If dumping, return true and print the prefix for the next output line.
1719 trace_ranger::dumping (unsigned counter
, bool trailing
)
1721 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1723 // Print counter index as well as INDENT spaces.
1725 fprintf (dump_file
, " %-7u ", counter
);
1727 fprintf (dump_file
, " ");
1729 for (x
= 0; x
< indent
; x
++)
1730 fputc (' ', dump_file
);
1736 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
1737 // returning RESULT.
1740 trace_ranger::trailer (unsigned counter
, const char *caller
, bool result
,
1741 tree name
, const irange
&r
)
1743 if (dumping (counter
, true))
1746 fputs(result
? "TRUE : " : "FALSE : ", dump_file
);
1747 fprintf (dump_file
, "(%u) ", counter
);
1748 fputs (caller
, dump_file
);
1749 fputs (" (",dump_file
);
1751 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1752 fputs (") ",dump_file
);
1756 fputc('\n', dump_file
);
1759 fputc('\n', dump_file
);
1760 // Marks the end of a request.
1762 fputc('\n', dump_file
);
1767 // Tracing version of range_on_edge. Call it with printing wrappers.
1770 trace_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
1772 unsigned idx
= ++trace_count
;
1775 fprintf (dump_file
, "range_on_edge (");
1776 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1777 fprintf (dump_file
, ") on edge %d->%d\n", e
->src
->index
, e
->dest
->index
);
1781 bool res
= gimple_ranger::range_on_edge (r
, e
, name
);
1782 trailer (idx
, "range_on_edge", true, name
, r
);
1786 // Tracing version of range_on_entry. Call it with printing wrappers.
1789 trace_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
1791 unsigned idx
= ++trace_count
;
1794 fprintf (dump_file
, "range_on_entry (");
1795 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1796 fprintf (dump_file
, ") to BB %d\n", bb
->index
);
1800 gimple_ranger::range_on_entry (r
, bb
, name
);
1802 trailer (idx
, "range_on_entry", true, name
, r
);
1805 // Tracing version of range_on_exit. Call it with printing wrappers.
1808 trace_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
1810 unsigned idx
= ++trace_count
;
1813 fprintf (dump_file
, "range_on_exit (");
1814 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1815 fprintf (dump_file
, ") from BB %d\n", bb
->index
);
1819 gimple_ranger::range_on_exit (r
, bb
, name
);
1821 trailer (idx
, "range_on_exit", true, name
, r
);
1824 // Tracing version of range_of_stmt. Call it with printing wrappers.
1827 trace_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
1830 unsigned idx
= ++trace_count
;
1833 fprintf (dump_file
, "range_of_stmt (");
1835 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1836 fputs (") at stmt ", dump_file
);
1837 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
1841 res
= gimple_ranger::range_of_stmt (r
, s
, name
);
1843 return trailer (idx
, "range_of_stmt", res
, name
, r
);
1846 // Tracing version of range_of_expr. Call it with printing wrappers.
1849 trace_ranger::range_of_expr (irange
&r
, tree name
, gimple
*s
)
1852 unsigned idx
= ++trace_count
;
1855 fprintf (dump_file
, "range_of_expr(");
1856 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1857 fputs (")", dump_file
);
1860 fputs (" at stmt ", dump_file
);
1861 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
1864 fputs ("\n", dump_file
);
1868 res
= gimple_ranger::range_of_expr (r
, name
, s
);
1870 return trailer (idx
, "range_of_expr", res
, name
, r
);
1874 enable_ranger (struct function
*fun
)
1878 if (param_evrp_mode
& EVRP_MODE_TRACE
)
1879 r
= new trace_ranger
;
1881 r
= new gimple_ranger
;
1883 fun
->x_range_query
= r
;
1889 disable_ranger (struct function
*fun
)
1891 delete fun
->x_range_query
;
1893 fun
->x_range_query
= &global_ranges
;
1896 // =========================================
1897 // Debugging helpers.
1898 // =========================================
1900 // Query all statements in the IL to precalculate computable ranges in RANGER.
1902 static DEBUG_FUNCTION
void
1903 debug_seed_ranger (gimple_ranger
&ranger
)
1905 // Recalculate SCEV to make sure the dump lists everything.
1906 if (scev_initialized_p ())
1914 gimple_stmt_iterator gsi
;
1915 FOR_EACH_BB_FN (bb
, cfun
)
1916 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1918 gimple
*stmt
= gsi_stmt (gsi
);
1920 if (is_gimple_debug (stmt
))
1923 ranger
.range_of_stmt (r
, stmt
);
1927 // Dump all that ranger knows for the current function.
1930 dump_ranger (FILE *out
)
1932 gimple_ranger ranger
;
1933 debug_seed_ranger (ranger
);
1940 dump_ranger (stderr
);
1943 // Dump all that ranger knows on a path of BBs.
1945 // Note that the blocks are in reverse order, thus the exit block is
1949 dump_ranger (FILE *dump_file
, const vec
<basic_block
> &path
)
1951 if (path
.length () == 0)
1953 fprintf (dump_file
, "empty\n");
1957 gimple_ranger ranger
;
1958 debug_seed_ranger (ranger
);
1960 unsigned i
= path
.length ();
1964 ranger
.dump_bb (dump_file
, path
[i
]);
1970 debug_ranger (const vec
<basic_block
> &path
)
1972 dump_ranger (stderr
, path
);
1975 #include "gimple-range-tests.cc"