1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "insn-codes.h"
30 #include "gimple-pretty-print.h"
31 #include "optabs-tree.h"
32 #include "gimple-iterator.h"
33 #include "gimple-fold.h"
35 #include "fold-const.h"
36 #include "case-cfn-macros.h"
37 #include "omp-general.h"
39 #include "tree-ssa-loop.h"
40 #include "tree-scalar-evolution.h"
41 #include "langhooks.h"
42 #include "vr-values.h"
44 #include "value-query.h"
45 #include "gimple-range-op.h"
46 #include "gimple-range.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
);
58 // Invoke range_of_expr on EXPR.
61 fur_source::get_operand (vrange
&r
, tree expr
)
63 return m_query
->range_of_expr (r
, expr
);
66 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
67 // range_query to get the range on the edge.
70 fur_source::get_phi_operand (vrange
&r
, tree expr
, edge e
)
72 return m_query
->range_on_edge (r
, e
, expr
);
75 // Default is no relation.
78 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED
,
79 tree op2 ATTRIBUTE_UNUSED
)
84 // Default registers nothing.
87 fur_source::register_relation (gimple
*s ATTRIBUTE_UNUSED
,
88 relation_kind k ATTRIBUTE_UNUSED
,
89 tree op1 ATTRIBUTE_UNUSED
,
90 tree op2 ATTRIBUTE_UNUSED
)
94 // Default registers nothing.
97 fur_source::register_relation (edge e ATTRIBUTE_UNUSED
,
98 relation_kind k ATTRIBUTE_UNUSED
,
99 tree op1 ATTRIBUTE_UNUSED
,
100 tree op2 ATTRIBUTE_UNUSED
)
104 // This version of fur_source will pick a range up off an edge.
106 class fur_edge
: public fur_source
109 fur_edge (edge e
, range_query
*q
= NULL
);
110 virtual bool get_operand (vrange
&r
, tree expr
) override
;
111 virtual bool get_phi_operand (vrange
&r
, tree expr
, edge e
) override
;
116 // Instantiate an edge based fur_source.
119 fur_edge::fur_edge (edge e
, range_query
*q
) : fur_source (q
)
124 // Get the value of EXPR on edge m_edge.
127 fur_edge::get_operand (vrange
&r
, tree expr
)
129 return m_query
->range_on_edge (r
, m_edge
, expr
);
132 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
133 // range_query to get the range on the edge.
136 fur_edge::get_phi_operand (vrange
&r
, tree expr
, edge e
)
138 // Edge to edge recalculations not supported yet, until we sort it out.
139 gcc_checking_assert (e
== m_edge
);
140 return m_query
->range_on_edge (r
, e
, expr
);
143 // Instantiate a stmt based fur_source.
145 fur_stmt::fur_stmt (gimple
*s
, range_query
*q
) : fur_source (q
)
150 // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
153 fur_stmt::get_operand (vrange
&r
, tree expr
)
155 return m_query
->range_of_expr (r
, expr
, m_stmt
);
158 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
159 // range_query to get the range on the edge.
162 fur_stmt::get_phi_operand (vrange
&r
, tree expr
, edge e
)
164 // Pick up the range of expr from edge E.
165 fur_edge
e_src (e
, m_query
);
166 return e_src
.get_operand (r
, expr
);
169 // Return relation based from m_stmt.
172 fur_stmt::query_relation (tree op1
, tree op2
)
174 return m_query
->query_relation (m_stmt
, op1
, op2
);
177 // Instantiate a stmt based fur_source with a GORI object.
180 fur_depend::fur_depend (gimple
*s
, gori_compute
*gori
, range_query
*q
)
183 gcc_checking_assert (gori
);
185 // Set relations if there is an oracle in the range_query.
186 // This will enable registering of relationships as they are discovered.
187 m_oracle
= q
->oracle ();
191 // Register a relation on a stmt if there is an oracle.
194 fur_depend::register_relation (gimple
*s
, relation_kind k
, tree op1
, tree op2
)
197 m_oracle
->register_stmt (s
, k
, op1
, op2
);
200 // Register a relation on an edge if there is an oracle.
203 fur_depend::register_relation (edge e
, relation_kind k
, tree op1
, tree op2
)
206 m_oracle
->register_edge (e
, k
, op1
, op2
);
209 // This version of fur_source will pick a range up from a list of ranges
210 // supplied by the caller.
212 class fur_list
: public fur_source
215 fur_list (vrange
&r1
, range_query
*q
= NULL
);
216 fur_list (vrange
&r1
, vrange
&r2
, range_query
*q
= NULL
);
217 fur_list (unsigned num
, vrange
**list
, range_query
*q
= NULL
);
218 virtual bool get_operand (vrange
&r
, tree expr
) override
;
219 virtual bool get_phi_operand (vrange
&r
, tree expr
, edge e
) override
;
227 // One range supplied for unary operations.
229 fur_list::fur_list (vrange
&r1
, range_query
*q
) : fur_source (q
)
237 // Two ranges supplied for binary operations.
239 fur_list::fur_list (vrange
&r1
, vrange
&r2
, range_query
*q
) : fur_source (q
)
248 // Arbitrary number of ranges in a vector.
250 fur_list::fur_list (unsigned num
, vrange
**list
, range_query
*q
)
258 // Get the next operand from the vector, ensure types are compatible.
261 fur_list::get_operand (vrange
&r
, tree expr
)
263 // Do not use the vector for non-ssa-names, or if it has been emptied.
264 if (TREE_CODE (expr
) != SSA_NAME
|| 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 (vrange
&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 (vrange
&r
, gimple
*s
, vrange
&r1
, range_query
*q
)
284 fur_list
src (r1
, q
);
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 (vrange
&r
, gimple
*s
, vrange
&r1
, vrange
&r2
, range_query
*q
)
294 fur_list
src (r1
, r2
, q
);
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 (vrange
&r
, gimple
*s
, unsigned num_elements
, vrange
**vector
,
306 fur_list
src (num_elements
, vector
, q
);
307 return f
.fold_stmt (r
, s
, src
);
310 // Fold stmt S into range R using range query Q.
313 fold_range (vrange
&r
, gimple
*s
, range_query
*q
)
317 return f
.fold_stmt (r
, s
, src
);
320 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
323 fold_range (vrange
&r
, gimple
*s
, edge on_edge
, range_query
*q
)
326 fur_edge
src (on_edge
, q
);
327 return f
.fold_stmt (r
, s
, src
);
330 // Provide a fur_source which can be used to determine any relations on
331 // a statement. It manages the callback from fold_using_ranges to determine
332 // a relation_trio for a statement.
334 class fur_relation
: public fur_stmt
337 fur_relation (gimple
*s
, range_query
*q
= NULL
);
338 virtual void register_relation (gimple
*stmt
, relation_kind k
, tree op1
,
340 virtual void register_relation (edge e
, relation_kind k
, tree op1
,
342 relation_trio
trio() const;
344 relation_kind def_op1
, def_op2
, op1_op2
;
347 fur_relation::fur_relation (gimple
*s
, range_query
*q
) : fur_stmt (s
, q
)
349 def_op1
= def_op2
= op1_op2
= VREL_VARYING
;
352 // Construct a trio from what is known.
355 fur_relation::trio () const
357 return relation_trio (def_op1
, def_op2
, op1_op2
);
360 // Don't support edges, but avoid a compiler warning by providing the routine.
363 fur_relation::register_relation (edge
, relation_kind
, tree
, tree
)
367 // Register relation K between OP1 and OP2 on STMT.
370 fur_relation::register_relation (gimple
*stmt
, relation_kind k
, tree op1
,
373 tree lhs
= gimple_get_lhs (stmt
);
376 switch (gimple_code (stmt
))
379 a1
= gimple_cond_lhs (stmt
);
380 a2
= gimple_cond_rhs (stmt
);
383 a1
= gimple_assign_rhs1 (stmt
);
384 if (gimple_num_ops (stmt
) >= 3)
385 a2
= gimple_assign_rhs2 (stmt
);
390 // STMT is of the form LHS = A1 op A2, now map the relation to these
391 // operands, if possible.
402 def_op1
= relation_swap (k
);
404 def_op2
= relation_swap (k
);
408 if (op1
== a1
&& op2
== a2
)
410 else if (op2
== a1
&& op1
== a2
)
411 op1_op2
= relation_swap (k
);
415 // Return the relation trio for stmt S using query Q.
418 fold_relations (gimple
*s
, range_query
*q
)
421 fur_relation
src (s
, q
);
422 tree lhs
= gimple_range_ssa_p (gimple_get_lhs (s
));
425 Value_Range
vr(TREE_TYPE (lhs
));
426 if (f
.fold_stmt (vr
, s
, src
))
432 // -------------------------------------------------------------------------
434 // Adjust the range for a pointer difference where the operands came
437 // This notices the following sequence:
439 // def = __builtin_memchr (arg, 0, sz)
442 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
445 adjust_pointer_diff_expr (irange
&res
, const gimple
*diff_stmt
)
447 tree op0
= gimple_assign_rhs1 (diff_stmt
);
448 tree op1
= gimple_assign_rhs2 (diff_stmt
);
449 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
450 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
453 if (TREE_CODE (op0
) == SSA_NAME
454 && TREE_CODE (op1
) == SSA_NAME
455 && (call
= SSA_NAME_DEF_STMT (op0
))
456 && is_gimple_call (call
)
457 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
458 && TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
459 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
460 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
461 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
462 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
463 && vrp_operand_equal_p (op1
, gimple_call_arg (call
, 0))
464 && integer_zerop (gimple_call_arg (call
, 1)))
466 wide_int maxm1
= irange_val_max (ptrdiff_type_node
) - 1;
467 res
.intersect (int_range
<2> (ptrdiff_type_node
,
468 wi::zero (TYPE_PRECISION (ptrdiff_type_node
)),
473 // Adjust the range for an IMAGPART_EXPR.
476 adjust_imagpart_expr (vrange
&res
, const gimple
*stmt
)
478 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
480 if (TREE_CODE (name
) != SSA_NAME
|| !SSA_NAME_DEF_STMT (name
))
483 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
484 if (is_gimple_call (def_stmt
) && gimple_call_internal_p (def_stmt
))
486 switch (gimple_call_internal_fn (def_stmt
))
488 case IFN_ADD_OVERFLOW
:
489 case IFN_SUB_OVERFLOW
:
490 case IFN_MUL_OVERFLOW
:
493 case IFN_ATOMIC_COMPARE_EXCHANGE
:
496 r
.set_varying (boolean_type_node
);
497 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
498 range_cast (r
, type
);
506 if (is_gimple_assign (def_stmt
)
507 && gimple_assign_rhs_code (def_stmt
) == COMPLEX_CST
)
509 tree cst
= gimple_assign_rhs1 (def_stmt
);
510 if (TREE_CODE (cst
) == COMPLEX_CST
511 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst
))) == INTEGER_TYPE
)
513 wide_int w
= wi::to_wide (TREE_IMAGPART (cst
));
514 int_range
<1> imag (TREE_TYPE (TREE_IMAGPART (cst
)), w
, w
);
515 res
.intersect (imag
);
520 // Adjust the range for a REALPART_EXPR.
523 adjust_realpart_expr (vrange
&res
, const gimple
*stmt
)
525 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
527 if (TREE_CODE (name
) != SSA_NAME
)
530 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
531 if (!SSA_NAME_DEF_STMT (name
))
534 if (is_gimple_assign (def_stmt
)
535 && gimple_assign_rhs_code (def_stmt
) == COMPLEX_CST
)
537 tree cst
= gimple_assign_rhs1 (def_stmt
);
538 if (TREE_CODE (cst
) == COMPLEX_CST
539 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst
))) == INTEGER_TYPE
)
541 wide_int imag
= wi::to_wide (TREE_REALPART (cst
));
542 int_range
<2> tmp (TREE_TYPE (TREE_REALPART (cst
)), imag
, imag
);
548 // This function looks for situations when walking the use/def chains
549 // may provide additional contextual range information not exposed on
553 gimple_range_adjustment (vrange
&res
, const gimple
*stmt
)
555 switch (gimple_expr_code (stmt
))
557 case POINTER_DIFF_EXPR
:
558 adjust_pointer_diff_expr (as_a
<irange
> (res
), stmt
);
562 adjust_imagpart_expr (res
, stmt
);
566 adjust_realpart_expr (res
, stmt
);
574 // Calculate a range for statement S and return it in R. If NAME is provided it
575 // represents the SSA_NAME on the LHS of the statement. It is only required
576 // if there is more than one lhs/output. If a range cannot
577 // be calculated, return false.
580 fold_using_range::fold_stmt (vrange
&r
, gimple
*s
, fur_source
&src
, tree name
)
583 // If name and S are specified, make sure it is an LHS of S.
584 gcc_checking_assert (!name
|| !gimple_get_lhs (s
) ||
585 name
== gimple_get_lhs (s
));
588 name
= gimple_get_lhs (s
);
590 // Process addresses.
591 if (gimple_code (s
) == GIMPLE_ASSIGN
592 && gimple_assign_rhs_code (s
) == ADDR_EXPR
)
593 return range_of_address (as_a
<irange
> (r
), s
, src
);
595 gimple_range_op_handler
handler (s
);
597 res
= range_of_range_op (r
, handler
, src
);
598 else if (is_a
<gphi
*>(s
))
599 res
= range_of_phi (r
, as_a
<gphi
*> (s
), src
);
600 else if (is_a
<gcall
*>(s
))
601 res
= range_of_call (r
, as_a
<gcall
*> (s
), src
);
602 else if (is_a
<gassign
*> (s
) && gimple_assign_rhs_code (s
) == COND_EXPR
)
603 res
= range_of_cond_expr (r
, as_a
<gassign
*> (s
), src
);
605 // If the result is varying, check for basic nonnegativeness.
606 // Specifically this helps for now with strict enum in cases like
607 // g++.dg/warn/pr33738.C.
609 if (res
&& r
.varying_p () && INTEGRAL_TYPE_P (r
.type ())
610 && gimple_stmt_nonnegative_warnv_p (s
, &so_p
))
611 r
.set_nonnegative (r
.type ());
615 // If no name specified or range is unsupported, bail.
616 if (!name
|| !gimple_range_ssa_p (name
))
618 // We don't understand the stmt, so return the global range.
619 gimple_range_global (r
, name
);
623 if (r
.undefined_p ())
626 // We sometimes get compatible types copied from operands, make sure
627 // the correct type is being returned.
628 if (name
&& TREE_TYPE (name
) != r
.type ())
630 gcc_checking_assert (range_compatible_p (r
.type (), TREE_TYPE (name
)));
631 range_cast (r
, TREE_TYPE (name
));
636 // Calculate a range for range_op statement S and return it in R. If any
637 // If a range cannot be calculated, return false.
640 fold_using_range::range_of_range_op (vrange
&r
,
641 gimple_range_op_handler
&handler
,
644 gcc_checking_assert (handler
);
645 gimple
*s
= handler
.stmt ();
646 tree type
= gimple_range_type (s
);
650 tree lhs
= handler
.lhs ();
651 tree op1
= handler
.operand1 ();
652 tree op2
= handler
.operand2 ();
654 // Certain types of builtin functions may have no arguments.
657 Value_Range
r1 (type
);
658 if (!handler
.fold_range (r
, type
, r1
, r1
))
659 r
.set_varying (type
);
663 Value_Range
range1 (TREE_TYPE (op1
));
664 Value_Range
range2 (op2
? TREE_TYPE (op2
) : TREE_TYPE (op1
));
666 if (src
.get_operand (range1
, op1
))
670 // Fold range, and register any dependency if available.
671 Value_Range
r2 (type
);
672 r2
.set_varying (type
);
673 if (!handler
.fold_range (r
, type
, range1
, r2
))
674 r
.set_varying (type
);
675 if (lhs
&& gimple_range_ssa_p (op1
))
678 src
.gori ()->register_dependency (lhs
, op1
);
680 rel
= handler
.lhs_op1_relation (r
, range1
, range1
);
681 if (rel
!= VREL_VARYING
)
682 src
.register_relation (s
, rel
, lhs
, op1
);
685 else if (src
.get_operand (range2
, op2
))
687 relation_kind rel
= src
.query_relation (op1
, op2
);
688 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && rel
!= VREL_VARYING
)
690 fprintf (dump_file
, " folding with relation ");
691 print_generic_expr (dump_file
, op1
, TDF_SLIM
);
692 print_relation (dump_file
, rel
);
693 print_generic_expr (dump_file
, op2
, TDF_SLIM
);
694 fputc ('\n', dump_file
);
696 // Fold range, and register any dependency if available.
697 if (!handler
.fold_range (r
, type
, range1
, range2
,
698 relation_trio::op1_op2 (rel
)))
699 r
.set_varying (type
);
700 if (irange::supports_p (type
))
701 relation_fold_and_or (as_a
<irange
> (r
), s
, src
, range1
, range2
);
706 src
.gori ()->register_dependency (lhs
, op1
);
707 src
.gori ()->register_dependency (lhs
, op2
);
709 if (gimple_range_ssa_p (op1
))
711 rel
= handler
.lhs_op1_relation (r
, range1
, range2
, rel
);
712 if (rel
!= VREL_VARYING
)
713 src
.register_relation (s
, rel
, lhs
, op1
);
715 if (gimple_range_ssa_p (op2
))
717 rel
= handler
.lhs_op2_relation (r
, range1
, range2
, rel
);
718 if (rel
!= VREL_VARYING
)
719 src
.register_relation (s
, rel
, lhs
, op2
);
722 // Check for an existing BB, as we maybe asked to fold an
723 // artificial statement not in the CFG.
724 else if (is_a
<gcond
*> (s
) && gimple_bb (s
))
726 basic_block bb
= gimple_bb (s
);
727 edge e0
= EDGE_SUCC (bb
, 0);
728 edge e1
= EDGE_SUCC (bb
, 1);
730 if (!single_pred_p (e0
->dest
))
732 if (!single_pred_p (e1
->dest
))
734 src
.register_outgoing_edges (as_a
<gcond
*> (s
),
735 as_a
<irange
> (r
), e0
, e1
);
739 r
.set_varying (type
);
742 r
.set_varying (type
);
743 // Make certain range-op adjustments that aren't handled any other way.
744 gimple_range_adjustment (r
, s
);
748 // Calculate the range of an assignment containing an ADDR_EXPR.
749 // Return the range in R.
750 // If a range cannot be calculated, set it to VARYING and return true.
753 fold_using_range::range_of_address (irange
&r
, gimple
*stmt
, fur_source
&src
)
755 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
756 gcc_checking_assert (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
);
758 bool strict_overflow_p
;
759 tree expr
= gimple_assign_rhs1 (stmt
);
760 poly_int64 bitsize
, bitpos
;
763 int unsignedp
, reversep
, volatilep
;
764 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
765 &bitpos
, &offset
, &mode
, &unsignedp
,
766 &reversep
, &volatilep
);
769 if (base
!= NULL_TREE
770 && TREE_CODE (base
) == MEM_REF
771 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
773 tree ssa
= TREE_OPERAND (base
, 0);
774 tree lhs
= gimple_get_lhs (stmt
);
775 if (lhs
&& gimple_range_ssa_p (ssa
) && src
.gori ())
776 src
.gori ()->register_dependency (lhs
, ssa
);
777 src
.get_operand (r
, ssa
);
778 range_cast (r
, TREE_TYPE (gimple_assign_rhs1 (stmt
)));
780 poly_offset_int off
= 0;
781 bool off_cst
= false;
782 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
784 off
= mem_ref_offset (base
);
786 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
788 off
<<= LOG2_BITS_PER_UNIT
;
792 /* If &X->a is equal to X, the range of X is the result. */
793 if (off_cst
&& known_eq (off
, 0))
795 else if (flag_delete_null_pointer_checks
796 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
)))
798 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
799 allow going from non-NULL pointer to NULL. */
801 || !r
.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr
)))))
803 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
804 using POINTER_PLUS_EXPR if off_cst and just fall back to
806 r
.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
810 /* If MEM_REF has a "positive" offset, consider it non-NULL
811 always, for -fdelete-null-pointer-checks also "negative"
812 ones. Punt for unknown offsets (e.g. variable ones). */
813 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
816 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
818 r
.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
821 r
.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
826 if (tree_single_nonzero_warnv_p (expr
, &strict_overflow_p
))
828 r
.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
832 // Otherwise return varying.
833 r
.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt
)));
837 // Calculate a range for phi statement S and return it in R.
838 // If a range cannot be calculated, return false.
841 fold_using_range::range_of_phi (vrange
&r
, gphi
*phi
, fur_source
&src
)
843 tree phi_def
= gimple_phi_result (phi
);
844 tree type
= gimple_range_type (phi
);
845 Value_Range
arg_range (type
);
846 Value_Range
equiv_range (type
);
852 // Track if all executable arguments are the same.
853 tree single_arg
= NULL_TREE
;
854 bool seen_arg
= false;
856 // Start with an empty range, unioning in each argument's range.
858 for (x
= 0; x
< gimple_phi_num_args (phi
); x
++)
860 tree arg
= gimple_phi_arg_def (phi
, x
);
861 // An argument that is the same as the def provides no new range.
865 edge e
= gimple_phi_arg_edge (phi
, x
);
867 // Get the range of the argument on its edge.
868 src
.get_phi_operand (arg_range
, arg
, e
);
870 if (!arg_range
.undefined_p ())
872 // Register potential dependencies for stale value tracking.
873 // Likewise, if the incoming PHI argument is equivalent to this
874 // PHI definition, it provides no new info. Accumulate these ranges
875 // in case all arguments are equivalences.
876 if (src
.query ()->query_relation (e
, arg
, phi_def
, false) == VREL_EQ
)
877 equiv_range
.union_(arg_range
);
879 r
.union_ (arg_range
);
881 if (gimple_range_ssa_p (arg
) && src
.gori ())
882 src
.gori ()->register_dependency (phi_def
, arg
);
885 // Track if all arguments are the same.
891 else if (single_arg
!= arg
)
892 single_arg
= NULL_TREE
;
894 // Once the value reaches varying, stop looking.
895 if (r
.varying_p () && single_arg
== NULL_TREE
)
899 // If all arguments were equivalences, use the equivalence ranges as no
900 // arguments were processed.
901 if (r
.undefined_p () && !equiv_range
.undefined_p ())
904 // If the PHI boils down to a single effective argument, look at it.
907 // Symbolic arguments can be equivalences.
908 if (gimple_range_ssa_p (single_arg
))
910 // Only allow the equivalence if the PHI definition does not
911 // dominate any incoming edge for SINGLE_ARG.
912 // See PR 108139 and 109462.
913 basic_block bb
= gimple_bb (phi
);
914 if (!dom_info_available_p (CDI_DOMINATORS
))
917 for (x
= 0; x
< gimple_phi_num_args (phi
); x
++)
918 if (gimple_phi_arg_def (phi
, x
) == single_arg
919 && dominated_by_p (CDI_DOMINATORS
,
920 gimple_phi_arg_edge (phi
, x
)->src
,
927 src
.register_relation (phi
, VREL_EQ
, phi_def
, single_arg
);
929 else if (src
.get_operand (arg_range
, single_arg
)
930 && arg_range
.singleton_p ())
932 // Numerical arguments that are a constant can be returned as
933 // the constant. This can help fold later cases where even this
934 // constant might have been UNDEFINED via an unreachable edge.
940 // If PHI analysis is available, see if there is an iniital range.
941 if (phi_analysis_available_p ()
942 && irange::supports_p (TREE_TYPE (phi_def
)))
944 phi_group
*g
= (phi_analysis())[phi_def
];
945 if (g
&& !(g
->range ().varying_p ()))
947 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
949 fprintf (dump_file
, "PHI GROUP query for ");
950 print_generic_expr (dump_file
, phi_def
, TDF_SLIM
);
951 fprintf (dump_file
, " found : ");
952 g
->range ().dump (dump_file
);
953 fprintf (dump_file
, " and adjusted original range from :");
956 r
.intersect (g
->range ());
957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
959 fprintf (dump_file
, " to :");
961 fprintf (dump_file
, "\n");
966 // If SCEV is available, query if this PHI has any known values.
967 if (scev_initialized_p ()
968 && !POINTER_TYPE_P (TREE_TYPE (phi_def
)))
970 class loop
*l
= loop_containing_stmt (phi
);
971 if (l
&& loop_outer (l
))
973 Value_Range
loop_range (type
);
974 range_of_ssa_name_with_loop_info (loop_range
, phi_def
, l
, phi
, src
);
975 if (!loop_range
.varying_p ())
977 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
979 fprintf (dump_file
, "Loops range found for ");
980 print_generic_expr (dump_file
, phi_def
, TDF_SLIM
);
981 fprintf (dump_file
, ": ");
982 loop_range
.dump (dump_file
);
983 fprintf (dump_file
, " and calculated range :");
985 fprintf (dump_file
, "\n");
987 r
.intersect (loop_range
);
995 // Calculate a range for call statement S and return it in R.
996 // If a range cannot be calculated, return false.
999 fold_using_range::range_of_call (vrange
&r
, gcall
*call
, fur_source
&)
1001 tree type
= gimple_range_type (call
);
1005 tree lhs
= gimple_call_lhs (call
);
1006 bool strict_overflow_p
;
1008 if (gimple_stmt_nonnegative_warnv_p (call
, &strict_overflow_p
))
1009 r
.set_nonnegative (type
);
1010 else if (gimple_call_nonnull_result_p (call
)
1011 || gimple_call_nonnull_arg (call
))
1012 r
.set_nonzero (type
);
1014 r
.set_varying (type
);
1016 // If there is an LHS, intersect that with what is known.
1019 Value_Range
def (TREE_TYPE (lhs
));
1020 gimple_range_global (def
, lhs
);
1026 // Calculate a range for COND_EXPR statement S and return it in R.
1027 // If a range cannot be calculated, return false.
1030 fold_using_range::range_of_cond_expr (vrange
&r
, gassign
*s
, fur_source
&src
)
1032 tree cond
= gimple_assign_rhs1 (s
);
1033 tree op1
= gimple_assign_rhs2 (s
);
1034 tree op2
= gimple_assign_rhs3 (s
);
1036 tree type
= gimple_range_type (s
);
1040 Value_Range
range1 (TREE_TYPE (op1
));
1041 Value_Range
range2 (TREE_TYPE (op2
));
1042 Value_Range
cond_range (TREE_TYPE (cond
));
1043 gcc_checking_assert (gimple_assign_rhs_code (s
) == COND_EXPR
);
1044 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1
), TREE_TYPE (op2
)));
1045 src
.get_operand (cond_range
, cond
);
1046 src
.get_operand (range1
, op1
);
1047 src
.get_operand (range2
, op2
);
1049 // Try to see if there is a dependence between the COND and either operand
1051 if (src
.gori ()->condexpr_adjust (range1
, range2
, s
, cond
, op1
, op2
, src
))
1052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1054 fprintf (dump_file
, "Possible COND_EXPR adjustment. Range op1 : ");
1055 range1
.dump(dump_file
);
1056 fprintf (dump_file
, " and Range op2: ");
1057 range2
.dump(dump_file
);
1058 fprintf (dump_file
, "\n");
1061 // If the condition is known, choose the appropriate expression.
1062 if (cond_range
.singleton_p ())
1064 // False, pick second operand.
1065 if (cond_range
.zero_p ())
1075 gcc_checking_assert (r
.undefined_p ()
1076 || range_compatible_p (r
.type (), type
));
1080 // If SCEV has any information about phi node NAME, return it as a range in R.
1083 fold_using_range::range_of_ssa_name_with_loop_info (vrange
&r
, tree name
,
1084 class loop
*l
, gphi
*phi
,
1087 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
1088 if (!range_of_var_in_loop (r
, name
, l
, phi
, src
.query ()))
1089 r
.set_varying (TREE_TYPE (name
));
1092 // -----------------------------------------------------------------------
1094 // Check if an && or || expression can be folded based on relations. ie
1098 // c_2 and c_3 can never be true at the same time,
1099 // Therefore c_4 can always resolve to false based purely on the relations.
1102 fold_using_range::relation_fold_and_or (irange
& lhs_range
, gimple
*s
,
1103 fur_source
&src
, vrange
&op1
,
1106 // No queries or already folded.
1107 if (!src
.gori () || !src
.query ()->oracle () || lhs_range
.singleton_p ())
1110 // Only care about AND and OR expressions.
1111 enum tree_code code
= gimple_expr_code (s
);
1112 bool is_and
= false;
1113 if (code
== BIT_AND_EXPR
|| code
== TRUTH_AND_EXPR
)
1115 else if (code
!= BIT_IOR_EXPR
&& code
!= TRUTH_OR_EXPR
)
1118 gimple_range_op_handler
handler (s
);
1119 tree lhs
= handler
.lhs ();
1120 tree ssa1
= gimple_range_ssa_p (handler
.operand1 ());
1121 tree ssa2
= gimple_range_ssa_p (handler
.operand2 ());
1123 // Deal with || and && only when there is a full set of symbolics.
1124 if (!lhs
|| !ssa1
|| !ssa2
1125 || (TREE_CODE (TREE_TYPE (lhs
)) != BOOLEAN_TYPE
)
1126 || (TREE_CODE (TREE_TYPE (ssa1
)) != BOOLEAN_TYPE
)
1127 || (TREE_CODE (TREE_TYPE (ssa2
)) != BOOLEAN_TYPE
))
1130 // Now we know its a boolean AND or OR expression with boolean operands.
1131 // Ideally we search dependencies for common names, and see what pops out.
1132 // until then, simply try to resolve direct dependencies.
1134 gimple
*ssa1_stmt
= SSA_NAME_DEF_STMT (ssa1
);
1135 gimple
*ssa2_stmt
= SSA_NAME_DEF_STMT (ssa2
);
1137 gimple_range_op_handler
handler1 (ssa1_stmt
);
1138 gimple_range_op_handler
handler2 (ssa2_stmt
);
1140 // If either handler is not present, no relation can be found.
1141 if (!handler1
|| !handler2
)
1144 // Both stmts will need to have 2 ssa names in the stmt.
1145 tree ssa1_dep1
= gimple_range_ssa_p (handler1
.operand1 ());
1146 tree ssa1_dep2
= gimple_range_ssa_p (handler1
.operand2 ());
1147 tree ssa2_dep1
= gimple_range_ssa_p (handler2
.operand1 ());
1148 tree ssa2_dep2
= gimple_range_ssa_p (handler2
.operand2 ());
1150 if (!ssa1_dep1
|| !ssa1_dep2
|| !ssa2_dep1
|| !ssa2_dep2
)
1153 if (HONOR_NANS (TREE_TYPE (ssa1_dep1
)))
1156 // Make sure they are the same dependencies, and detect the order of the
1158 bool reverse_op2
= true;
1159 if (ssa1_dep1
== ssa2_dep1
&& ssa1_dep2
== ssa2_dep2
)
1160 reverse_op2
= false;
1161 else if (ssa1_dep1
!= ssa2_dep2
|| ssa1_dep2
!= ssa2_dep1
)
1164 int_range
<2> bool_one
= range_true ();
1165 relation_kind relation1
= handler1
.op1_op2_relation (bool_one
, op1
, op2
);
1166 relation_kind relation2
= handler2
.op1_op2_relation (bool_one
, op1
, op2
);
1167 if (relation1
== VREL_VARYING
|| relation2
== VREL_VARYING
)
1171 relation2
= relation_negate (relation2
);
1173 // x && y is false if the relation intersection of the true cases is NULL.
1174 if (is_and
&& relation_intersect (relation1
, relation2
) == VREL_UNDEFINED
)
1175 lhs_range
= range_false (boolean_type_node
);
1176 // x || y is true if the union of the true cases is NO-RELATION..
1177 // ie, one or the other being true covers the full range of possibilities.
1178 else if (!is_and
&& relation_union (relation1
, relation2
) == VREL_VARYING
)
1179 lhs_range
= bool_one
;
1183 range_cast (lhs_range
, TREE_TYPE (lhs
));
1184 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1186 fprintf (dump_file
, " Relation adjustment: ");
1187 print_generic_expr (dump_file
, ssa1
, TDF_SLIM
);
1188 fprintf (dump_file
, " and ");
1189 print_generic_expr (dump_file
, ssa2
, TDF_SLIM
);
1190 fprintf (dump_file
, " combine to produce ");
1191 lhs_range
.dump (dump_file
);
1192 fputc ('\n', dump_file
);
1198 // Register any outgoing edge relations from a conditional branch.
1201 fur_source::register_outgoing_edges (gcond
*s
, irange
&lhs_range
,
1204 int_range
<2> e0_range
, e1_range
;
1206 basic_block bb
= gimple_bb (s
);
1208 gimple_range_op_handler
handler (s
);
1214 // If this edge is never taken, ignore it.
1215 gcond_edge_range (e0_range
, e0
);
1216 e0_range
.intersect (lhs_range
);
1217 if (e0_range
.undefined_p ())
1223 // If this edge is never taken, ignore it.
1224 gcond_edge_range (e1_range
, e1
);
1225 e1_range
.intersect (lhs_range
);
1226 if (e1_range
.undefined_p ())
1233 // First, register the gcond itself. This will catch statements like
1235 tree ssa1
= gimple_range_ssa_p (handler
.operand1 ());
1236 tree ssa2
= gimple_range_ssa_p (handler
.operand2 ());
1240 r1
.set_varying (TREE_TYPE (ssa1
));
1241 r2
.set_varying (TREE_TYPE (ssa2
));
1244 relation_kind relation
= handler
.op1_op2_relation (e0_range
, r1
, r2
);
1245 if (relation
!= VREL_VARYING
)
1246 register_relation (e0
, relation
, ssa1
, ssa2
);
1250 relation_kind relation
= handler
.op1_op2_relation (e1_range
, r1
, r2
);
1251 if (relation
!= VREL_VARYING
)
1252 register_relation (e1
, relation
, ssa1
, ssa2
);
1256 // Outgoing relations of GORI exports require a gori engine.
1260 // Now look for other relations in the exports. This will find stmts
1261 // leading to the condition such as:
1264 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb
, name
)
1266 if (TREE_CODE (TREE_TYPE (name
)) != BOOLEAN_TYPE
)
1268 gimple
*stmt
= SSA_NAME_DEF_STMT (name
);
1269 gimple_range_op_handler
handler (stmt
);
1272 tree ssa1
= gimple_range_ssa_p (handler
.operand1 ());
1273 tree ssa2
= gimple_range_ssa_p (handler
.operand2 ());
1274 Value_Range
r (TREE_TYPE (name
));
1277 r1
.set_varying (TREE_TYPE (ssa1
));
1278 r2
.set_varying (TREE_TYPE (ssa2
));
1279 if (e0
&& gori ()->outgoing_edge_range_p (r
, e0
, name
, *m_query
)
1280 && r
.singleton_p ())
1282 relation_kind relation
= handler
.op1_op2_relation (r
, r1
, r2
);
1283 if (relation
!= VREL_VARYING
)
1284 register_relation (e0
, relation
, ssa1
, ssa2
);
1286 if (e1
&& gori ()->outgoing_edge_range_p (r
, e1
, name
, *m_query
)
1287 && r
.singleton_p ())
1289 relation_kind relation
= handler
.op1_op2_relation (r
, r1
, r2
);
1290 if (relation
!= VREL_VARYING
)
1291 register_relation (e1
, relation
, ssa1
, ssa2
);