1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2020 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"
51 // Adjust the range for a pointer difference where the operands came
54 // This notices the following sequence:
56 // def = __builtin_memchr (arg, 0, sz)
59 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
62 adjust_pointer_diff_expr (irange
&res
, const gimple
*diff_stmt
)
64 tree op0
= gimple_assign_rhs1 (diff_stmt
);
65 tree op1
= gimple_assign_rhs2 (diff_stmt
);
66 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
67 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
70 if (TREE_CODE (op0
) == SSA_NAME
71 && TREE_CODE (op1
) == SSA_NAME
72 && (call
= SSA_NAME_DEF_STMT (op0
))
73 && is_gimple_call (call
)
74 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
75 && TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
76 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
77 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
78 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
79 && gimple_call_builtin_p (call
, BUILT_IN_MEMCHR
)
80 && vrp_operand_equal_p (op1
, gimple_call_arg (call
, 0))
81 && integer_zerop (gimple_call_arg (call
, 1)))
83 tree max
= vrp_val_max (ptrdiff_type_node
);
84 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
85 tree expr_type
= gimple_expr_type (diff_stmt
);
86 tree range_min
= build_zero_cst (expr_type
);
87 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
88 int_range
<2> r (range_min
, range_max
);
93 // This function looks for situations when walking the use/def chains
94 // may provide additonal contextual range information not exposed on
95 // this statement. Like knowing the IMAGPART return value from a
96 // builtin function is a boolean result.
98 // We should rework how we're called, as we have an op_unknown entry
99 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
100 // function gets called.
103 gimple_range_adjustment (irange
&res
, const gimple
*stmt
)
105 switch (gimple_expr_code (stmt
))
107 case POINTER_DIFF_EXPR
:
108 adjust_pointer_diff_expr (res
, stmt
);
113 tree name
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
114 if (TREE_CODE (name
) == SSA_NAME
)
116 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
117 if (def_stmt
&& is_gimple_call (def_stmt
)
118 && gimple_call_internal_p (def_stmt
))
120 switch (gimple_call_internal_fn (def_stmt
))
122 case IFN_ADD_OVERFLOW
:
123 case IFN_SUB_OVERFLOW
:
124 case IFN_MUL_OVERFLOW
:
125 case IFN_ATOMIC_COMPARE_EXCHANGE
:
128 r
.set_varying (boolean_type_node
);
129 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
130 range_cast (r
, type
);
146 // Return a range in R for the tree EXPR. Return true if a range is
150 get_tree_range (irange
&r
, tree expr
)
156 type
= TREE_TYPE (expr
);
158 // Return false if the type isn't suported.
159 if (!irange::supports_type_p (type
))
162 switch (TREE_CODE (expr
))
169 r
= gimple_range_global (expr
);
174 // Handle &var which can show up in phi arguments.
176 if (tree_single_nonzero_warnv_p (expr
, &ov
))
178 r
= range_nonzero (type
);
187 r
.set_varying (type
);
191 // Fold this unary statement using R1 as operand1's range, returning
192 // the result in RES. Return false if the operation fails.
195 gimple_range_fold (irange
&res
, const gimple
*stmt
, const irange
&r1
)
197 gcc_checking_assert (gimple_range_handler (stmt
));
199 tree type
= gimple_expr_type (stmt
);
200 // Unary SSA operations require the LHS type as the second range.
201 int_range
<2> r2 (type
);
203 return gimple_range_fold (res
, stmt
, r1
, r2
);
206 // Fold this binary statement using R1 and R2 as the operands ranges,
207 // returning the result in RES. Return false if the operation fails.
210 gimple_range_fold (irange
&res
, const gimple
*stmt
,
211 const irange
&r1
, const irange
&r2
)
213 gcc_checking_assert (gimple_range_handler (stmt
));
215 gimple_range_handler (stmt
)->fold_range (res
, gimple_expr_type (stmt
),
218 // If there are any gimple lookups, do those now.
219 gimple_range_adjustment (res
, stmt
);
223 // Return the base of the RHS of an assignment.
226 gimple_range_base_of_assignment (const gimple
*stmt
)
228 gcc_checking_assert (gimple_code (stmt
) == GIMPLE_ASSIGN
);
229 tree op1
= gimple_assign_rhs1 (stmt
);
230 if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
231 return get_base_address (TREE_OPERAND (op1
, 0));
235 // Return the first operand of this statement if it is a valid operand
236 // supported by ranges, otherwise return NULL_TREE. Special case is
237 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
240 gimple_range_operand1 (const gimple
*stmt
)
242 gcc_checking_assert (gimple_range_handler (stmt
));
244 switch (gimple_code (stmt
))
247 return gimple_cond_lhs (stmt
);
250 tree base
= gimple_range_base_of_assignment (stmt
);
251 if (base
&& TREE_CODE (base
) == MEM_REF
)
253 // If the base address is an SSA_NAME, we return it
254 // here. This allows processing of the range of that
255 // name, while the rest of the expression is simply
256 // ignored. The code in range_ops will see the
257 // ADDR_EXPR and do the right thing.
258 tree ssa
= TREE_OPERAND (base
, 0);
259 if (TREE_CODE (ssa
) == SSA_NAME
)
270 // Return the second operand of statement STMT, otherwise return NULL_TREE.
273 gimple_range_operand2 (const gimple
*stmt
)
275 gcc_checking_assert (gimple_range_handler (stmt
));
277 switch (gimple_code (stmt
))
280 return gimple_cond_rhs (stmt
);
282 if (gimple_num_ops (stmt
) >= 3)
283 return gimple_assign_rhs2 (stmt
);
290 // Calculate what we can determine of the range of this unary
291 // statement's operand if the lhs of the expression has the range
292 // LHS_RANGE. Return false if nothing can be determined.
295 gimple_range_calc_op1 (irange
&r
, const gimple
*stmt
, const irange
&lhs_range
)
297 gcc_checking_assert (gimple_num_ops (stmt
) < 3);
299 // An empty range is viral.
300 tree type
= TREE_TYPE (gimple_range_operand1 (stmt
));
301 if (lhs_range
.undefined_p ())
306 // Unary operations require the type of the first operand in the
307 // second range position.
308 int_range
<2> type_range (type
);
309 return gimple_range_handler (stmt
)->op1_range (r
, type
, lhs_range
,
313 // Calculate what we can determine of the range of this statement's
314 // first operand if the lhs of the expression has the range LHS_RANGE
315 // and the second operand has the range OP2_RANGE. Return false if
316 // nothing can be determined.
319 gimple_range_calc_op1 (irange
&r
, const gimple
*stmt
,
320 const irange
&lhs_range
, const irange
&op2_range
)
322 // Unary operation are allowed to pass a range in for second operand
323 // as there are often additional restrictions beyond the type which
324 // can be imposed. See operator_cast::op1_range().
325 tree type
= TREE_TYPE (gimple_range_operand1 (stmt
));
326 // An empty range is viral.
327 if (op2_range
.undefined_p () || lhs_range
.undefined_p ())
332 return gimple_range_handler (stmt
)->op1_range (r
, type
, lhs_range
,
336 // Calculate what we can determine of the range of this statement's
337 // second operand if the lhs of the expression has the range LHS_RANGE
338 // and the first operand has the range OP1_RANGE. Return false if
339 // nothing can be determined.
342 gimple_range_calc_op2 (irange
&r
, const gimple
*stmt
,
343 const irange
&lhs_range
, const irange
&op1_range
)
345 tree type
= TREE_TYPE (gimple_range_operand2 (stmt
));
346 // An empty range is viral.
347 if (op1_range
.undefined_p () || lhs_range
.undefined_p ())
352 return gimple_range_handler (stmt
)->op2_range (r
, type
, lhs_range
,
356 // Calculate a range for statement S and return it in R. If NAME is provided it
357 // represents the SSA_NAME on the LHS of the statement. It is only required
358 // if there is more than one lhs/output. If a range cannot
359 // be calculated, return false.
362 gimple_ranger::calc_stmt (irange
&r
, gimple
*s
, tree name
)
365 // If name is specified, make sure it is an LHS of S.
366 gcc_checking_assert (name
? SSA_NAME_DEF_STMT (name
) == s
: true);
368 if (gimple_range_handler (s
))
369 res
= range_of_range_op (r
, s
);
370 else if (is_a
<gphi
*>(s
))
371 res
= range_of_phi (r
, as_a
<gphi
*> (s
));
372 else if (is_a
<gcall
*>(s
))
373 res
= range_of_call (r
, as_a
<gcall
*> (s
));
374 else if (is_a
<gassign
*> (s
) && gimple_assign_rhs_code (s
) == COND_EXPR
)
375 res
= range_of_cond_expr (r
, as_a
<gassign
*> (s
));
378 // If no name is specified, try the expression kind.
381 tree t
= gimple_expr_type (s
);
382 if (!irange::supports_type_p (t
))
387 // We don't understand the stmt, so return the global range.
388 r
= gimple_range_global (name
);
393 if (r
.undefined_p ())
395 if (name
&& TREE_TYPE (name
) != r
.type ())
396 range_cast (r
, TREE_TYPE (name
));
402 // Calculate a range for range_op statement S and return it in R. If any
403 // If a range cannot be calculated, return false.
406 gimple_ranger::range_of_range_op (irange
&r
, gimple
*s
)
408 int_range_max range1
, range2
;
409 tree type
= gimple_expr_type (s
);
410 gcc_checking_assert (irange::supports_type_p (type
));
412 tree op1
= gimple_range_operand1 (s
);
413 tree op2
= gimple_range_operand2 (s
);
415 if (range_of_non_trivial_assignment (r
, s
))
418 if (range_of_expr (range1
, op1
, s
))
421 return gimple_range_fold (r
, s
, range1
);
423 if (range_of_expr (range2
, op2
, s
))
424 return gimple_range_fold (r
, s
, range1
, range2
);
426 r
.set_varying (type
);
430 // Calculate the range of a non-trivial assignment. That is, is one
431 // inolving arithmetic on an SSA name (for example, an ADDR_EXPR).
432 // Return the range in R.
434 // If a range cannot be calculated, return false.
437 gimple_ranger::range_of_non_trivial_assignment (irange
&r
, gimple
*stmt
)
439 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
442 tree base
= gimple_range_base_of_assignment (stmt
);
443 if (base
&& TREE_CODE (base
) == MEM_REF
444 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
446 int_range_max range1
;
447 tree ssa
= TREE_OPERAND (base
, 0);
448 if (range_of_expr (range1
, ssa
, stmt
))
450 tree type
= TREE_TYPE (ssa
);
451 range_operator
*op
= range_op_handler (POINTER_PLUS_EXPR
, type
);
452 int_range
<2> offset (TREE_OPERAND (base
, 1), TREE_OPERAND (base
, 1));
453 op
->fold_range (r
, type
, range1
, offset
);
460 // Calculate a range for phi statement S and return it in R.
461 // If a range cannot be calculated, return false.
464 gimple_ranger::range_of_phi (irange
&r
, gphi
*phi
)
466 tree phi_def
= gimple_phi_result (phi
);
467 tree type
= TREE_TYPE (phi_def
);
468 int_range_max arg_range
;
471 if (!irange::supports_type_p (type
))
474 // Start with an empty range, unioning in each argument's range.
476 for (x
= 0; x
< gimple_phi_num_args (phi
); x
++)
478 tree arg
= gimple_phi_arg_def (phi
, x
);
479 edge e
= gimple_phi_arg_edge (phi
, x
);
481 range_on_edge (arg_range
, e
, arg
);
482 r
.union_ (arg_range
);
483 // Once the value reaches varying, stop looking.
488 // If SCEV is available, query if this PHI has any knonwn values.
489 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def
)))
491 value_range loop_range
;
492 class loop
*l
= loop_containing_stmt (phi
);
495 range_of_ssa_name_with_loop_info (loop_range
, phi_def
, l
, phi
);
496 if (!loop_range
.varying_p ())
498 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
500 fprintf (dump_file
, " Loops range found for ");
501 print_generic_expr (dump_file
, phi_def
, TDF_SLIM
);
502 fprintf (dump_file
, ": ");
503 loop_range
.dump (dump_file
);
504 fprintf (dump_file
, " and calculated range :");
506 fprintf (dump_file
, "\n");
508 r
.intersect (loop_range
);
516 // Calculate a range for call statement S and return it in R.
517 // If a range cannot be calculated, return false.
520 gimple_ranger::range_of_call (irange
&r
, gcall
*call
)
522 tree type
= gimple_call_return_type (call
);
523 tree lhs
= gimple_call_lhs (call
);
524 bool strict_overflow_p
;
526 if (!irange::supports_type_p (type
))
529 if (range_of_builtin_call (r
, call
))
531 else if (gimple_stmt_nonnegative_warnv_p (call
, &strict_overflow_p
))
532 r
.set (build_int_cst (type
, 0), TYPE_MAX_VALUE (type
));
533 else if (gimple_call_nonnull_result_p (call
)
534 || gimple_call_nonnull_arg (call
))
535 r
= range_nonzero (type
);
537 r
.set_varying (type
);
539 // If there is an LHS, intersect that with what is known.
543 def
= gimple_range_global (lhs
);
551 gimple_ranger::range_of_builtin_ubsan_call (irange
&r
, gcall
*call
,
554 gcc_checking_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
555 || code
== MULT_EXPR
);
556 tree type
= gimple_call_return_type (call
);
557 range_operator
*op
= range_op_handler (code
, type
);
558 gcc_checking_assert (op
);
559 int_range_max ir0
, ir1
;
560 tree arg0
= gimple_call_arg (call
, 0);
561 tree arg1
= gimple_call_arg (call
, 1);
562 gcc_assert (range_of_expr (ir0
, arg0
, call
));
563 gcc_assert (range_of_expr (ir1
, arg1
, call
));
565 bool saved_flag_wrapv
= flag_wrapv
;
566 // Pretend the arithmetic is wrapping. If there is any overflow,
567 // we'll complain, but will actually do wrapping operation.
569 op
->fold_range (r
, type
, ir0
, ir1
);
570 flag_wrapv
= saved_flag_wrapv
;
572 // If for both arguments vrp_valueize returned non-NULL, this should
573 // have been already folded and if not, it wasn't folded because of
574 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
575 if (r
.singleton_p ())
576 r
.set_varying (type
);
581 gimple_ranger::range_of_builtin_call (irange
&r
, gcall
*call
)
583 combined_fn func
= gimple_call_combined_fn (call
);
584 if (func
== CFN_LAST
)
587 tree type
= gimple_call_return_type (call
);
589 int mini
, maxi
, zerov
, prec
;
590 scalar_int_mode mode
;
594 case CFN_BUILT_IN_CONSTANT_P
:
595 if (cfun
->after_inlining
)
601 arg
= gimple_call_arg (call
, 0);
602 if (range_of_expr (r
, arg
, call
) && r
.singleton_p ())
604 r
.set (build_one_cst (type
), build_one_cst (type
));
611 // __builtin_ffs* and __builtin_popcount* return [0, prec].
612 arg
= gimple_call_arg (call
, 0);
613 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
616 gcc_assert (range_of_expr (r
, arg
, call
));
617 // If arg is non-zero, then ffs or popcount are non-zero.
618 if (!range_includes_zero_p (&r
))
620 // If some high bits are known to be zero, decrease the maximum.
621 if (!r
.undefined_p ())
623 wide_int max
= r
.upper_bound ();
624 maxi
= wi::floor_log2 (max
) + 1;
626 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
630 r
.set (build_zero_cst (type
), build_one_cst (type
));
634 // __builtin_c[lt]z* return [0, prec-1], except when the
635 // argument is 0, but that is undefined behavior.
637 // On many targets where the CLZ RTL or optab value is defined
638 // for 0, the value is prec, so include that in the range by
640 arg
= gimple_call_arg (call
, 0);
641 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
644 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
645 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
646 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
)
647 // Only handle the single common value.
649 // Magic value to give up, unless we can prove arg is non-zero.
652 gcc_assert (range_of_expr (r
, arg
, call
));
653 // From clz of minimum we can compute result maximum.
656 maxi
= prec
- 1 - wi::floor_log2 (r
.lower_bound ());
660 else if (!range_includes_zero_p (&r
))
667 // From clz of maximum we can compute result minimum.
670 mini
= prec
- 1 - wi::floor_log2 (r
.upper_bound ());
676 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
680 // __builtin_ctz* return [0, prec-1], except for when the
681 // argument is 0, but that is undefined behavior.
683 // If there is a ctz optab for this mode and
684 // CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
685 // otherwise just assume 0 won't be seen.
686 arg
= gimple_call_arg (call
, 0);
687 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
690 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
691 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
692 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
))
694 // Handle only the two common values.
697 else if (zerov
== prec
)
700 // Magic value to give up, unless we can prove arg is non-zero.
703 gcc_assert (range_of_expr (r
, arg
, call
));
704 if (!r
.undefined_p ())
706 if (r
.lower_bound () != 0)
711 // If some high bits are known to be zero, we can decrease
713 wide_int max
= r
.upper_bound ();
716 maxi
= wi::floor_log2 (max
);
720 r
.set (build_int_cst (type
, mini
), build_int_cst (type
, maxi
));
724 arg
= gimple_call_arg (call
, 0);
725 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
726 r
.set (build_int_cst (type
, 0), build_int_cst (type
, prec
- 1));
728 case CFN_UBSAN_CHECK_ADD
:
729 range_of_builtin_ubsan_call (r
, call
, PLUS_EXPR
);
731 case CFN_UBSAN_CHECK_SUB
:
732 range_of_builtin_ubsan_call (r
, call
, MINUS_EXPR
);
734 case CFN_UBSAN_CHECK_MUL
:
735 range_of_builtin_ubsan_call (r
, call
, MULT_EXPR
);
738 case CFN_GOACC_DIM_SIZE
:
739 case CFN_GOACC_DIM_POS
:
740 // Optimizing these two internal functions helps the loop
741 // optimizer eliminate outer comparisons. Size is [1,N]
742 // and pos is [0,N-1].
744 bool is_pos
= func
== CFN_GOACC_DIM_POS
;
745 int axis
= oacc_get_ifn_dim_arg (call
);
746 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
748 // If it's dynamic, the backend might know a hardware limitation.
749 size
= targetm
.goacc
.dim_limit (axis
);
751 r
.set (build_int_cst (type
, is_pos
? 0 : 1),
753 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
757 case CFN_BUILT_IN_STRLEN
:
758 if (tree lhs
= gimple_call_lhs (call
))
759 if (ptrdiff_type_node
760 && (TYPE_PRECISION (ptrdiff_type_node
)
761 == TYPE_PRECISION (TREE_TYPE (lhs
))))
763 tree type
= TREE_TYPE (lhs
);
764 tree max
= vrp_val_max (ptrdiff_type_node
);
766 = wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
767 tree range_min
= build_zero_cst (type
);
768 // To account for the terminating NULL, the maximum length
769 // is one less than the maximum array size, which in turn
770 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
771 // smaller than the former type).
772 // FIXME: Use max_object_size() - 1 here.
773 tree range_max
= wide_int_to_tree (type
, wmax
- 2);
774 r
.set (range_min
, range_max
);
786 // Calculate a range for COND_EXPR statement S and return it in R.
787 // If a range cannot be calculated, return false.
790 gimple_ranger::range_of_cond_expr (irange
&r
, gassign
*s
)
792 int_range_max cond_range
, range1
, range2
;
793 tree cond
= gimple_assign_rhs1 (s
);
794 tree op1
= gimple_assign_rhs2 (s
);
795 tree op2
= gimple_assign_rhs3 (s
);
797 gcc_checking_assert (gimple_assign_rhs_code (s
) == COND_EXPR
);
798 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1
),
800 if (!irange::supports_type_p (TREE_TYPE (op1
)))
803 gcc_assert (range_of_expr (cond_range
, cond
, s
));
804 gcc_assert (range_of_expr (range1
, op1
, s
));
805 gcc_assert (range_of_expr (range2
, op2
, s
));
807 // If the condition is known, choose the appropriate expression.
808 if (cond_range
.singleton_p ())
810 // False, pick second operand.
811 if (cond_range
.zero_p ())
825 gimple_ranger::range_of_expr (irange
&r
, tree expr
, gimple
*stmt
)
827 if (!gimple_range_ssa_p (expr
))
828 return get_tree_range (r
, expr
);
830 // If there is no statement, just get the global value.
833 if (!m_cache
.m_globals
.get_global_range (r
, expr
))
834 r
= gimple_range_global (expr
);
838 basic_block bb
= gimple_bb (stmt
);
839 gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
841 // If name is defined in this block, try to get an range from S.
842 if (def_stmt
&& gimple_bb (def_stmt
) == bb
)
843 gcc_assert (range_of_stmt (r
, def_stmt
, expr
));
845 // Otherwise OP comes from outside this block, use range on entry.
846 range_on_entry (r
, bb
, expr
);
848 // No range yet, see if there is a dereference in the block.
849 // We don't care if it's between the def and a use within a block
850 // because the entire block must be executed anyway.
851 // FIXME:?? For non-call exceptions we could have a statement throw
852 // which causes an early block exit.
853 // in which case we may need to walk from S back to the def/top of block
854 // to make sure the deref happens between S and there before claiming
855 // there is a deref. Punt for now.
856 if (!cfun
->can_throw_non_call_exceptions
&& r
.varying_p () &&
857 m_cache
.m_non_null
.non_null_deref_p (expr
, bb
))
858 r
= range_nonzero (TREE_TYPE (expr
));
863 // Return the range of NAME on entry to block BB in R.
866 gimple_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
868 int_range_max entry_range
;
869 gcc_checking_assert (gimple_range_ssa_p (name
));
871 // Start with any known range
872 gcc_assert (range_of_stmt (r
, SSA_NAME_DEF_STMT (name
), name
));
874 // Now see if there is any on_entry value which may refine it.
875 if (m_cache
.block_range (entry_range
, bb
, name
))
876 r
.intersect (entry_range
);
879 // Calculate the range for NAME at the end of block BB and return it in R.
880 // Return false if no range can be calculated.
883 gimple_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
885 // on-exit from the exit block?
886 gcc_checking_assert (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
888 gimple
*s
= last_stmt (bb
);
889 // If there is no statement in the block and this isn't the entry
890 // block, go get the range_on_entry for this block. For the entry
891 // block, a NULL stmt will return the global value for NAME.
892 if (!s
&& bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
893 range_on_entry (r
, bb
, name
);
895 gcc_assert (range_of_expr (r
, name
, s
));
896 gcc_checking_assert (r
.undefined_p ()
897 || types_compatible_p (r
.type(), TREE_TYPE (name
)));
900 // Calculate a range for NAME on edge E and return it in R.
903 gimple_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
905 int_range_max edge_range
;
906 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name
)));
908 // PHI arguments can be constants, catch these here.
909 if (!gimple_range_ssa_p (name
))
911 gcc_assert (range_of_expr (r
, name
));
915 range_on_exit (r
, e
->src
, name
);
916 gcc_checking_assert (r
.undefined_p ()
917 || types_compatible_p (r
.type(), TREE_TYPE (name
)));
919 // Check to see if NAME is defined on edge e.
920 if (m_cache
.outgoing_edge_range_p (edge_range
, e
, name
))
921 r
.intersect (edge_range
);
926 // Calculate a range for statement S and return it in R. If NAME is
927 // provided it represents the SSA_NAME on the LHS of the statement.
928 // It is only required if there is more than one lhs/output. Check
929 // the global cache for NAME first to see if the evaluation can be
930 // avoided. If a range cannot be calculated, return false.
933 gimple_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
935 // If no name, simply call the base routine.
937 name
= gimple_get_lhs (s
);
940 return calc_stmt (r
, s
, NULL_TREE
);
942 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
&&
943 irange::supports_type_p (TREE_TYPE (name
)));
945 // If this STMT has already been processed, return that value.
946 if (m_cache
.m_globals
.get_global_range (r
, name
))
948 // Avoid infinite recursion by initializing global cache
949 int_range_max tmp
= gimple_range_global (name
);
950 m_cache
.m_globals
.set_global_range (name
, tmp
);
952 gcc_assert (calc_stmt (r
, s
, name
));
954 if (is_a
<gphi
*> (s
))
956 m_cache
.m_globals
.set_global_range (name
, r
);
960 // This routine will export whatever global ranges are known to GCC
961 // SSA_RANGE_NAME_INFO fields.
964 gimple_ranger::export_global_ranges ()
970 fprintf (dump_file
, "Exported global range table\n");
971 fprintf (dump_file
, "===========================\n");
974 for ( x
= 1; x
< num_ssa_names
; x
++)
976 tree name
= ssa_name (x
);
977 if (name
&& !SSA_NAME_IN_FREE_LIST (name
)
978 && gimple_range_ssa_p (name
)
979 && m_cache
.m_globals
.get_global_range (r
, name
)
982 // Make sure the new range is a subset of the old range.
983 int_range_max old_range
;
984 old_range
= gimple_range_global (name
);
985 old_range
.intersect (r
);
986 /* Disable this while we fix tree-ssa/pr61743-2.c. */
987 //gcc_checking_assert (old_range == r);
989 // WTF? Can't write non-null pointer ranges?? stupid set_range_info!
990 if (!POINTER_TYPE_P (TREE_TYPE (name
)) && !r
.undefined_p ())
993 set_range_info (name
, vr
);
996 print_generic_expr (dump_file
, name
, TDF_SLIM
);
997 fprintf (dump_file
, " --> ");
999 fprintf (dump_file
, "\n");
1000 fprintf (dump_file
, " irange : ");
1002 fprintf (dump_file
, "\n");
1009 // Print the known table values to file F.
1012 gimple_ranger::dump (FILE *f
)
1016 FOR_EACH_BB_FN (bb
, cfun
)
1021 int_range_max range
;
1022 fprintf (f
, "\n=========== BB %d ============\n", bb
->index
);
1023 m_cache
.m_on_entry
.dump (f
, bb
);
1025 dump_bb (f
, bb
, 4, TDF_NONE
);
1027 // Now find any globals defined in this block.
1028 for (x
= 1; x
< num_ssa_names
; x
++)
1030 tree name
= ssa_name (x
);
1031 if (gimple_range_ssa_p (name
) && SSA_NAME_DEF_STMT (name
) &&
1032 gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
&&
1033 m_cache
.m_globals
.get_global_range (range
, name
))
1035 if (!range
.varying_p ())
1037 print_generic_expr (f
, name
, TDF_SLIM
);
1046 // And now outgoing edges, if they define anything.
1047 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1049 for (x
= 1; x
< num_ssa_names
; x
++)
1051 tree name
= gimple_range_ssa_p (ssa_name (x
));
1052 if (name
&& m_cache
.outgoing_edge_range_p (range
, e
, name
))
1054 gimple
*s
= SSA_NAME_DEF_STMT (name
);
1055 // Only print the range if this is the def block, or
1056 // the on entry cache for either end of the edge is
1058 if ((s
&& bb
== gimple_bb (s
)) ||
1059 m_cache
.block_range (range
, bb
, name
, false) ||
1060 m_cache
.block_range (range
, e
->dest
, name
, false))
1062 range_on_edge (range
, e
, name
);
1063 if (!range
.varying_p ())
1065 fprintf (f
, "%d->%d ", e
->src
->index
,
1068 if (e
->flags
& EDGE_TRUE_VALUE
)
1069 fprintf (f
, " (T)%c", c
);
1070 else if (e
->flags
& EDGE_FALSE_VALUE
)
1071 fprintf (f
, " (F)%c", c
);
1074 print_generic_expr (f
, name
, TDF_SLIM
);
1075 fprintf(f
, " : \t");
1085 m_cache
.m_globals
.dump (dump_file
);
1088 if (dump_flags
& TDF_DETAILS
)
1090 fprintf (f
, "\nDUMPING GORI MAP\n");
1096 // If SCEV has any information about phi node NAME, return it as a range in R.
1099 gimple_ranger::range_of_ssa_name_with_loop_info (irange
&r
, tree name
,
1100 class loop
*l
, gphi
*phi
)
1102 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
1103 tree min
, max
, type
= TREE_TYPE (name
);
1104 if (bounds_of_var_in_loop (&min
, &max
, this, l
, phi
, name
))
1106 // ?? We could do better here. Since MIN/MAX can only be an
1107 // SSA, SSA +- INTEGER_CST, or INTEGER_CST, we could easily call
1108 // the ranger and solve anything not an integer.
1109 if (TREE_CODE (min
) != INTEGER_CST
)
1110 min
= vrp_val_min (type
);
1111 if (TREE_CODE (max
) != INTEGER_CST
)
1112 max
= vrp_val_max (type
);
1116 r
.set_varying (type
);
1119 // --------------------------------------------------------------------------
1120 // trace_ranger implementation.
1123 trace_ranger::trace_ranger ()
1129 // If dumping, return true and print the prefix for the next output line.
1132 trace_ranger::dumping (unsigned counter
, bool trailing
)
1134 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1136 // Print counter index as well as INDENT spaces.
1138 fprintf (dump_file
, " %-7u ", counter
);
1140 fprintf (dump_file
, " ");
1142 for (x
= 0; x
< indent
; x
++)
1143 fputc (' ', dump_file
);
1149 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
1150 // returning RESULT.
1153 trace_ranger::trailer (unsigned counter
, const char *caller
, bool result
,
1154 tree name
, const irange
&r
)
1156 if (dumping (counter
, true))
1159 fputs(result
? "TRUE : " : "FALSE : ", dump_file
);
1160 fprintf (dump_file
, "(%u) ", counter
);
1161 fputs (caller
, dump_file
);
1162 fputs (" (",dump_file
);
1164 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1165 fputs (") ",dump_file
);
1169 fputc('\n', dump_file
);
1172 fputc('\n', dump_file
);
1173 // Marks the end of a request.
1175 fputc('\n', dump_file
);
1180 // Tracing version of range_on_edge. Call it with printing wrappers.
1183 trace_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
1185 unsigned idx
= ++trace_count
;
1188 fprintf (dump_file
, "range_on_edge (");
1189 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1190 fprintf (dump_file
, ") on edge %d->%d\n", e
->src
->index
, e
->dest
->index
);
1194 bool res
= gimple_ranger::range_on_edge (r
, e
, name
);
1195 trailer (idx
, "range_on_edge", true, name
, r
);
1199 // Tracing version of range_on_entry. Call it with printing wrappers.
1202 trace_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
1204 unsigned idx
= ++trace_count
;
1207 fprintf (dump_file
, "range_on_entry (");
1208 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1209 fprintf (dump_file
, ") to BB %d\n", bb
->index
);
1213 gimple_ranger::range_on_entry (r
, bb
, name
);
1215 trailer (idx
, "range_on_entry", true, name
, r
);
1218 // Tracing version of range_on_exit. Call it with printing wrappers.
1221 trace_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
1223 unsigned idx
= ++trace_count
;
1226 fprintf (dump_file
, "range_on_exit (");
1227 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1228 fprintf (dump_file
, ") from BB %d\n", bb
->index
);
1232 gimple_ranger::range_on_exit (r
, bb
, name
);
1234 trailer (idx
, "range_on_exit", true, name
, r
);
1237 // Tracing version of range_of_stmt. Call it with printing wrappers.
1240 trace_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
1243 unsigned idx
= ++trace_count
;
1246 fprintf (dump_file
, "range_of_stmt (");
1248 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1249 fputs (") at stmt ", dump_file
);
1250 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
1254 res
= gimple_ranger::range_of_stmt (r
, s
, name
);
1256 return trailer (idx
, "range_of_stmt", res
, name
, r
);
1259 // Tracing version of range_of_expr. Call it with printing wrappers.
1262 trace_ranger::range_of_expr (irange
&r
, tree name
, gimple
*s
)
1265 unsigned idx
= ++trace_count
;
1268 fprintf (dump_file
, "range_of_expr(");
1269 print_generic_expr (dump_file
, name
, TDF_SLIM
);
1270 fputs (")", dump_file
);
1273 fputs (" at stmt ", dump_file
);
1274 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
1277 fputs ("\n", dump_file
);
1281 res
= gimple_ranger::range_of_expr (r
, name
, s
);
1283 return trailer (idx
, "range_of_expr", res
, name
, r
);