[RS6000] dg-do !compile and scan-assembler
[official-gcc.git] / gcc / gimple-range.cc
blobcf979845acffad6c6e71d187e18ce1ade34d1b1e
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)
11 any later version.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "insn-codes.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "ssa.h"
31 #include "gimple-pretty-print.h"
32 #include "gimple-iterator.h"
33 #include "optabs-tree.h"
34 #include "gimple-fold.h"
35 #include "tree-cfg.h"
36 #include "fold-const.h"
37 #include "tree-cfg.h"
38 #include "wide-int.h"
39 #include "fold-const.h"
40 #include "case-cfn-macros.h"
41 #include "omp-general.h"
42 #include "cfgloop.h"
43 #include "tree-ssa-loop.h"
44 #include "tree-scalar-evolution.h"
45 #include "dbgcnt.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
52 // from a memchr.
54 // This notices the following sequence:
56 // def = __builtin_memchr (arg, 0, sz)
57 // n = def - arg
59 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
61 static void
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));
68 gimple *call;
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);
89 res.intersect (r);
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.
102 static void
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);
109 return;
111 case IMAGPART_EXPR:
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:
127 int_range<2> r;
128 r.set_varying (boolean_type_node);
129 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
130 range_cast (r, type);
131 res.intersect (r);
133 default:
134 break;
138 break;
141 default:
142 break;
146 // Return a range in R for the tree EXPR. Return true if a range is
147 // representable, and UNDEFINED/false if not.
149 bool
150 get_tree_range (irange &r, tree expr)
152 tree type;
153 if (TYPE_P (expr))
154 type = expr;
155 else
156 type = TREE_TYPE (expr);
158 // Return false if the type isn't suported.
159 if (!irange::supports_type_p (type))
161 r.set_undefined ();
162 return false;
165 switch (TREE_CODE (expr))
167 case INTEGER_CST:
168 r.set (expr, expr);
169 return true;
171 case SSA_NAME:
172 r = gimple_range_global (expr);
173 return true;
175 case ADDR_EXPR:
177 // Handle &var which can show up in phi arguments.
178 bool ov;
179 if (tree_single_nonzero_warnv_p (expr, &ov))
181 r = range_nonzero (type);
182 return true;
184 break;
187 default:
188 break;
190 r.set_varying (type);
191 return true;
194 // Fold this unary statement using R1 as operand1's range, returning
195 // the result in RES. Return false if the operation fails.
197 bool
198 gimple_range_fold (irange &res, const gimple *stmt, const irange &r1)
200 gcc_checking_assert (gimple_range_handler (stmt));
202 tree type = gimple_expr_type (stmt);
203 // Unary SSA operations require the LHS type as the second range.
204 int_range<2> r2 (type);
206 return gimple_range_fold (res, stmt, r1, r2);
209 // Fold this binary statement using R1 and R2 as the operands ranges,
210 // returning the result in RES. Return false if the operation fails.
212 bool
213 gimple_range_fold (irange &res, const gimple *stmt,
214 const irange &r1, const irange &r2)
216 gcc_checking_assert (gimple_range_handler (stmt));
218 gimple_range_handler (stmt)->fold_range (res, gimple_expr_type (stmt),
219 r1, r2);
221 // If there are any gimple lookups, do those now.
222 gimple_range_adjustment (res, stmt);
223 return true;
226 // Return the base of the RHS of an assignment.
228 tree
229 gimple_range_base_of_assignment (const gimple *stmt)
231 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
232 tree op1 = gimple_assign_rhs1 (stmt);
233 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
234 return get_base_address (TREE_OPERAND (op1, 0));
235 return op1;
238 // Return the first operand of this statement if it is a valid operand
239 // supported by ranges, otherwise return NULL_TREE. Special case is
240 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
242 tree
243 gimple_range_operand1 (const gimple *stmt)
245 gcc_checking_assert (gimple_range_handler (stmt));
247 switch (gimple_code (stmt))
249 case GIMPLE_COND:
250 return gimple_cond_lhs (stmt);
251 case GIMPLE_ASSIGN:
253 tree base = gimple_range_base_of_assignment (stmt);
254 if (base && TREE_CODE (base) == MEM_REF)
256 // If the base address is an SSA_NAME, we return it
257 // here. This allows processing of the range of that
258 // name, while the rest of the expression is simply
259 // ignored. The code in range_ops will see the
260 // ADDR_EXPR and do the right thing.
261 tree ssa = TREE_OPERAND (base, 0);
262 if (TREE_CODE (ssa) == SSA_NAME)
263 return ssa;
265 return base;
267 default:
268 break;
270 return NULL;
273 // Return the second operand of statement STMT, otherwise return NULL_TREE.
275 tree
276 gimple_range_operand2 (const gimple *stmt)
278 gcc_checking_assert (gimple_range_handler (stmt));
280 switch (gimple_code (stmt))
282 case GIMPLE_COND:
283 return gimple_cond_rhs (stmt);
284 case GIMPLE_ASSIGN:
285 if (gimple_num_ops (stmt) >= 3)
286 return gimple_assign_rhs2 (stmt);
287 default:
288 break;
290 return NULL_TREE;
293 // Calculate what we can determine of the range of this unary
294 // statement's operand if the lhs of the expression has the range
295 // LHS_RANGE. Return false if nothing can be determined.
297 bool
298 gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range)
300 gcc_checking_assert (gimple_num_ops (stmt) < 3);
302 // An empty range is viral.
303 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
304 if (lhs_range.undefined_p ())
306 r.set_undefined ();
307 return true;
309 // Unary operations require the type of the first operand in the
310 // second range position.
311 int_range<2> type_range (type);
312 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
313 type_range);
316 // Calculate what we can determine of the range of this statement's
317 // first operand if the lhs of the expression has the range LHS_RANGE
318 // and the second operand has the range OP2_RANGE. Return false if
319 // nothing can be determined.
321 bool
322 gimple_range_calc_op1 (irange &r, const gimple *stmt,
323 const irange &lhs_range, const irange &op2_range)
325 // Unary operation are allowed to pass a range in for second operand
326 // as there are often additional restrictions beyond the type which
327 // can be imposed. See operator_cast::op1_range().
328 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
329 // An empty range is viral.
330 if (op2_range.undefined_p () || lhs_range.undefined_p ())
332 r.set_undefined ();
333 return true;
335 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
336 op2_range);
339 // Calculate what we can determine of the range of this statement's
340 // second operand if the lhs of the expression has the range LHS_RANGE
341 // and the first operand has the range OP1_RANGE. Return false if
342 // nothing can be determined.
344 bool
345 gimple_range_calc_op2 (irange &r, const gimple *stmt,
346 const irange &lhs_range, const irange &op1_range)
348 tree type = TREE_TYPE (gimple_range_operand2 (stmt));
349 // An empty range is viral.
350 if (op1_range.undefined_p () || lhs_range.undefined_p ())
352 r.set_undefined ();
353 return true;
355 return gimple_range_handler (stmt)->op2_range (r, type, lhs_range,
356 op1_range);
359 // Calculate a range for statement S and return it in R. If NAME is provided it
360 // represents the SSA_NAME on the LHS of the statement. It is only required
361 // if there is more than one lhs/output. If a range cannot
362 // be calculated, return false.
364 bool
365 gimple_ranger::calc_stmt (irange &r, gimple *s, tree name)
367 bool res = false;
368 // If name is specified, make sure it is an LHS of S.
369 gcc_checking_assert (name ? SSA_NAME_DEF_STMT (name) == s : true);
371 if (gimple_range_handler (s))
372 res = range_of_range_op (r, s);
373 else if (is_a<gphi *>(s))
374 res = range_of_phi (r, as_a<gphi *> (s));
375 else if (is_a<gcall *>(s))
376 res = range_of_call (r, as_a<gcall *> (s));
377 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
378 res = range_of_cond_expr (r, as_a<gassign *> (s));
380 if (!res)
382 // If no name is specified, try the expression kind.
383 if (!name)
385 tree t = gimple_expr_type (s);
386 if (!irange::supports_type_p (t))
387 return false;
388 r.set_varying (t);
389 return true;
391 if (!gimple_range_ssa_p (name))
392 return false;
393 // We don't understand the stmt, so return the global range.
394 r = gimple_range_global (name);
395 return true;
398 if (r.undefined_p ())
399 return true;
401 // We sometimes get compatible types copied from operands, make sure
402 // the correct type is being returned.
403 if (name && TREE_TYPE (name) != r.type ())
405 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
406 range_cast (r, TREE_TYPE (name));
408 return true;
411 // Calculate a range for range_op statement S and return it in R. If any
412 // If a range cannot be calculated, return false.
414 bool
415 gimple_ranger::range_of_range_op (irange &r, gimple *s)
417 int_range_max range1, range2;
418 tree type = gimple_expr_type (s);
419 gcc_checking_assert (irange::supports_type_p (type));
421 tree op1 = gimple_range_operand1 (s);
422 tree op2 = gimple_range_operand2 (s);
424 if (range_of_non_trivial_assignment (r, s))
425 return true;
427 if (range_of_expr (range1, op1, s))
429 if (!op2)
430 return gimple_range_fold (r, s, range1);
432 if (range_of_expr (range2, op2, s))
433 return gimple_range_fold (r, s, range1, range2);
435 r.set_varying (type);
436 return true;
439 // Calculate the range of a non-trivial assignment. That is, is one
440 // inolving arithmetic on an SSA name (for example, an ADDR_EXPR).
441 // Return the range in R.
443 // If a range cannot be calculated, return false.
445 bool
446 gimple_ranger::range_of_non_trivial_assignment (irange &r, gimple *stmt)
448 if (gimple_code (stmt) != GIMPLE_ASSIGN)
449 return false;
451 tree base = gimple_range_base_of_assignment (stmt);
452 if (base)
454 if (TREE_CODE (base) == MEM_REF)
456 if (TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
458 int_range_max range1;
459 tree ssa = TREE_OPERAND (base, 0);
460 if (range_of_expr (range1, ssa, stmt))
462 tree type = TREE_TYPE (ssa);
463 range_operator *op = range_op_handler (POINTER_PLUS_EXPR,
464 type);
465 int_range<2> offset (TREE_OPERAND (base, 1),
466 TREE_OPERAND (base, 1));
467 op->fold_range (r, type, range1, offset);
468 return true;
471 return false;
473 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
475 // Handle "= &a" and return non-zero.
476 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
477 return true;
480 return false;
483 // Calculate a range for phi statement S and return it in R.
484 // If a range cannot be calculated, return false.
486 bool
487 gimple_ranger::range_of_phi (irange &r, gphi *phi)
489 tree phi_def = gimple_phi_result (phi);
490 tree type = TREE_TYPE (phi_def);
491 int_range_max arg_range;
492 unsigned x;
494 if (!irange::supports_type_p (type))
495 return false;
497 // Start with an empty range, unioning in each argument's range.
498 r.set_undefined ();
499 for (x = 0; x < gimple_phi_num_args (phi); x++)
501 tree arg = gimple_phi_arg_def (phi, x);
502 edge e = gimple_phi_arg_edge (phi, x);
504 range_on_edge (arg_range, e, arg);
505 r.union_ (arg_range);
506 // Once the value reaches varying, stop looking.
507 if (r.varying_p ())
508 break;
511 // If SCEV is available, query if this PHI has any knonwn values.
512 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
514 value_range loop_range;
515 class loop *l = loop_containing_stmt (phi);
516 if (l && loop_outer (l))
518 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi);
519 if (!loop_range.varying_p ())
521 if (dump_file && (dump_flags & TDF_DETAILS))
523 fprintf (dump_file, " Loops range found for ");
524 print_generic_expr (dump_file, phi_def, TDF_SLIM);
525 fprintf (dump_file, ": ");
526 loop_range.dump (dump_file);
527 fprintf (dump_file, " and calculated range :");
528 r.dump (dump_file);
529 fprintf (dump_file, "\n");
531 r.intersect (loop_range);
536 return true;
539 // Calculate a range for call statement S and return it in R.
540 // If a range cannot be calculated, return false.
542 bool
543 gimple_ranger::range_of_call (irange &r, gcall *call)
545 tree type = gimple_call_return_type (call);
546 tree lhs = gimple_call_lhs (call);
547 bool strict_overflow_p;
549 if (!irange::supports_type_p (type))
550 return false;
552 if (range_of_builtin_call (r, call))
554 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
555 r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
556 else if (gimple_call_nonnull_result_p (call)
557 || gimple_call_nonnull_arg (call))
558 r = range_nonzero (type);
559 else
560 r.set_varying (type);
562 // If there is an LHS, intersect that with what is known.
563 if (lhs)
565 value_range def;
566 def = gimple_range_global (lhs);
567 r.intersect (def);
569 return true;
572 // Return the range of a __builtin_ubsan* in CALL and set it in R.
573 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
574 // MULT_EXPR).
576 static void
577 range_of_builtin_ubsan_call (range_query &query, irange &r, gcall *call,
578 tree_code code)
580 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
581 || code == MULT_EXPR);
582 tree type = gimple_call_return_type (call);
583 range_operator *op = range_op_handler (code, type);
584 gcc_checking_assert (op);
585 int_range_max ir0, ir1;
586 tree arg0 = gimple_call_arg (call, 0);
587 tree arg1 = gimple_call_arg (call, 1);
588 query.range_of_expr (ir0, arg0, call);
589 query.range_of_expr (ir1, arg1, call);
591 bool saved_flag_wrapv = flag_wrapv;
592 // Pretend the arithmetic is wrapping. If there is any overflow,
593 // we'll complain, but will actually do wrapping operation.
594 flag_wrapv = 1;
595 op->fold_range (r, type, ir0, ir1);
596 flag_wrapv = saved_flag_wrapv;
598 // If for both arguments vrp_valueize returned non-NULL, this should
599 // have been already folded and if not, it wasn't folded because of
600 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
601 if (r.singleton_p ())
602 r.set_varying (type);
605 // For a builtin in CALL, return a range in R if known and return
606 // TRUE. Otherwise return FALSE.
608 bool
609 range_of_builtin_call (range_query &query, irange &r, gcall *call)
611 combined_fn func = gimple_call_combined_fn (call);
612 if (func == CFN_LAST)
613 return false;
615 tree type = gimple_call_return_type (call);
616 tree arg;
617 int mini, maxi, zerov = 0, prec;
618 scalar_int_mode mode;
620 switch (func)
622 case CFN_BUILT_IN_CONSTANT_P:
623 if (cfun->after_inlining)
625 r.set_zero (type);
626 // r.equiv_clear ();
627 return true;
629 arg = gimple_call_arg (call, 0);
630 if (query.range_of_expr (r, arg, call) && r.singleton_p ())
632 r.set (build_one_cst (type), build_one_cst (type));
633 return true;
635 break;
637 CASE_CFN_FFS:
638 CASE_CFN_POPCOUNT:
639 // __builtin_ffs* and __builtin_popcount* return [0, prec].
640 arg = gimple_call_arg (call, 0);
641 prec = TYPE_PRECISION (TREE_TYPE (arg));
642 mini = 0;
643 maxi = prec;
644 query.range_of_expr (r, arg, call);
645 // If arg is non-zero, then ffs or popcount are non-zero.
646 if (!range_includes_zero_p (&r))
647 mini = 1;
648 // If some high bits are known to be zero, decrease the maximum.
649 if (!r.undefined_p ())
651 if (TYPE_SIGN (r.type ()) == SIGNED)
652 range_cast (r, unsigned_type_for (r.type ()));
653 wide_int max = r.upper_bound ();
654 maxi = wi::floor_log2 (max) + 1;
656 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
657 return true;
659 CASE_CFN_PARITY:
660 r.set (build_zero_cst (type), build_one_cst (type));
661 return true;
663 CASE_CFN_CLZ:
664 // __builtin_c[lt]z* return [0, prec-1], except when the
665 // argument is 0, but that is undefined behavior.
667 // For __builtin_c[lt]z* consider argument of 0 always undefined
668 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
669 arg = gimple_call_arg (call, 0);
670 prec = TYPE_PRECISION (TREE_TYPE (arg));
671 mini = 0;
672 maxi = prec - 1;
673 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
674 if (gimple_call_internal_p (call))
676 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
677 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
679 // Only handle the single common value.
680 if (zerov == prec)
681 maxi = prec;
682 else
683 // Magic value to give up, unless we can prove arg is non-zero.
684 mini = -2;
688 query.range_of_expr (r, arg, call);
689 // From clz of minimum we can compute result maximum.
690 if (r.constant_p ())
692 int newmaxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
693 // Argument is unsigned, so do nothing if it is [0, ...] range.
694 if (newmaxi != prec)
696 mini = 0;
697 maxi = newmaxi;
700 else if (!range_includes_zero_p (&r))
702 maxi = prec - 1;
703 mini = 0;
705 if (mini == -2)
706 break;
707 // From clz of maximum we can compute result minimum.
708 if (r.constant_p ())
710 int newmini = prec - 1 - wi::floor_log2 (r.upper_bound ());
711 if (newmini == prec)
713 // Argument range is [0, 0]. If CLZ_DEFINED_VALUE_AT_ZERO
714 // is 2 with VALUE of prec, return [prec, prec], otherwise
715 // ignore the range.
716 if (maxi == prec)
717 mini = prec;
719 else
720 mini = newmini;
722 if (mini == -2)
723 break;
724 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
725 return true;
727 CASE_CFN_CTZ:
728 // __builtin_ctz* return [0, prec-1], except for when the
729 // argument is 0, but that is undefined behavior.
731 // For __builtin_ctz* consider argument of 0 always undefined
732 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
733 arg = gimple_call_arg (call, 0);
734 prec = TYPE_PRECISION (TREE_TYPE (arg));
735 mini = 0;
736 maxi = prec - 1;
737 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
738 if (gimple_call_internal_p (call))
740 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
741 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
743 // Handle only the two common values.
744 if (zerov == -1)
745 mini = -1;
746 else if (zerov == prec)
747 maxi = prec;
748 else
749 // Magic value to give up, unless we can prove arg is non-zero.
750 mini = -2;
753 query.range_of_expr (r, arg, call);
754 if (!r.undefined_p ())
756 if (r.lower_bound () != 0)
758 mini = 0;
759 maxi = prec - 1;
761 // If some high bits are known to be zero, we can decrease
762 // the maximum.
763 wide_int max = r.upper_bound ();
764 if (max == 0)
766 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
767 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
768 // Otherwise ignore the range.
769 if (mini == -1)
770 maxi = -1;
771 else if (maxi == prec)
772 mini = prec;
774 // If value at zero is prec and 0 is in the range, we can't lower
775 // the upper bound. We could create two separate ranges though,
776 // [0,floor_log2(max)][prec,prec] though.
777 else if (maxi != prec)
778 maxi = wi::floor_log2 (max);
780 if (mini == -2)
781 break;
782 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
783 return true;
785 CASE_CFN_CLRSB:
786 arg = gimple_call_arg (call, 0);
787 prec = TYPE_PRECISION (TREE_TYPE (arg));
788 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
789 return true;
790 case CFN_UBSAN_CHECK_ADD:
791 range_of_builtin_ubsan_call (query, r, call, PLUS_EXPR);
792 return true;
793 case CFN_UBSAN_CHECK_SUB:
794 range_of_builtin_ubsan_call (query, r, call, MINUS_EXPR);
795 return true;
796 case CFN_UBSAN_CHECK_MUL:
797 range_of_builtin_ubsan_call (query, r, call, MULT_EXPR);
798 return true;
800 case CFN_GOACC_DIM_SIZE:
801 case CFN_GOACC_DIM_POS:
802 // Optimizing these two internal functions helps the loop
803 // optimizer eliminate outer comparisons. Size is [1,N]
804 // and pos is [0,N-1].
806 bool is_pos = func == CFN_GOACC_DIM_POS;
807 int axis = oacc_get_ifn_dim_arg (call);
808 int size = oacc_get_fn_dim_size (current_function_decl, axis);
809 if (!size)
810 // If it's dynamic, the backend might know a hardware limitation.
811 size = targetm.goacc.dim_limit (axis);
813 r.set (build_int_cst (type, is_pos ? 0 : 1),
814 size
815 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
816 return true;
819 case CFN_BUILT_IN_STRLEN:
820 if (tree lhs = gimple_call_lhs (call))
821 if (ptrdiff_type_node
822 && (TYPE_PRECISION (ptrdiff_type_node)
823 == TYPE_PRECISION (TREE_TYPE (lhs))))
825 tree type = TREE_TYPE (lhs);
826 tree max = vrp_val_max (ptrdiff_type_node);
827 wide_int wmax
828 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
829 tree range_min = build_zero_cst (type);
830 // To account for the terminating NULL, the maximum length
831 // is one less than the maximum array size, which in turn
832 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
833 // smaller than the former type).
834 // FIXME: Use max_object_size() - 1 here.
835 tree range_max = wide_int_to_tree (type, wmax - 2);
836 r.set (range_min, range_max);
837 return true;
839 break;
840 default:
841 break;
843 return false;
847 bool
848 gimple_ranger::range_of_builtin_call (irange &r, gcall *call)
850 return ::range_of_builtin_call (*this, r, call);
853 // Calculate a range for COND_EXPR statement S and return it in R.
854 // If a range cannot be calculated, return false.
856 bool
857 gimple_ranger::range_of_cond_expr (irange &r, gassign *s)
859 int_range_max cond_range, range1, range2;
860 tree cond = gimple_assign_rhs1 (s);
861 tree op1 = gimple_assign_rhs2 (s);
862 tree op2 = gimple_assign_rhs3 (s);
864 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
865 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1),
866 TREE_TYPE (op2)));
867 if (!irange::supports_type_p (TREE_TYPE (op1)))
868 return false;
870 range_of_expr (cond_range, cond, s);
871 range_of_expr (range1, op1, s);
872 range_of_expr (range2, op2, s);
874 // If the condition is known, choose the appropriate expression.
875 if (cond_range.singleton_p ())
877 // False, pick second operand.
878 if (cond_range.zero_p ())
879 r = range2;
880 else
881 r = range1;
883 else
885 r = range1;
886 r.union_ (range2);
888 return true;
891 bool
892 gimple_ranger::range_of_expr (irange &r, tree expr, gimple *stmt)
894 if (!gimple_range_ssa_p (expr))
895 return get_tree_range (r, expr);
897 // If there is no statement, just get the global value.
898 if (!stmt)
900 if (!m_cache.m_globals.get_global_range (r, expr))
901 r = gimple_range_global (expr);
902 return true;
905 basic_block bb = gimple_bb (stmt);
906 gimple *def_stmt = SSA_NAME_DEF_STMT (expr);
908 // If name is defined in this block, try to get an range from S.
909 if (def_stmt && gimple_bb (def_stmt) == bb)
910 range_of_stmt (r, def_stmt, expr);
911 else
912 // Otherwise OP comes from outside this block, use range on entry.
913 range_on_entry (r, bb, expr);
915 // No range yet, see if there is a dereference in the block.
916 // We don't care if it's between the def and a use within a block
917 // because the entire block must be executed anyway.
918 // FIXME:?? For non-call exceptions we could have a statement throw
919 // which causes an early block exit.
920 // in which case we may need to walk from S back to the def/top of block
921 // to make sure the deref happens between S and there before claiming
922 // there is a deref. Punt for now.
923 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
924 m_cache.m_non_null.non_null_deref_p (expr, bb))
925 r = range_nonzero (TREE_TYPE (expr));
927 return true;
930 // Return the range of NAME on entry to block BB in R.
932 void
933 gimple_ranger::range_on_entry (irange &r, basic_block bb, tree name)
935 int_range_max entry_range;
936 gcc_checking_assert (gimple_range_ssa_p (name));
938 // Start with any known range
939 range_of_stmt (r, SSA_NAME_DEF_STMT (name), name);
941 // Now see if there is any on_entry value which may refine it.
942 if (m_cache.block_range (entry_range, bb, name))
943 r.intersect (entry_range);
946 // Calculate the range for NAME at the end of block BB and return it in R.
947 // Return false if no range can be calculated.
949 void
950 gimple_ranger::range_on_exit (irange &r, basic_block bb, tree name)
952 // on-exit from the exit block?
953 gcc_checking_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
954 gcc_checking_assert (gimple_range_ssa_p (name));
956 gimple *s = last_stmt (bb);
957 // If there is no statement in the block and this isn't the entry
958 // block, go get the range_on_entry for this block. For the entry
959 // block, a NULL stmt will return the global value for NAME.
960 if (!s && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
961 range_on_entry (r, bb, name);
962 else
963 range_of_expr (r, name, s);
964 gcc_checking_assert (r.undefined_p ()
965 || range_compatible_p (r.type (), TREE_TYPE (name)));
968 // Calculate a range for NAME on edge E and return it in R.
970 bool
971 gimple_ranger::range_on_edge (irange &r, edge e, tree name)
973 int_range_max edge_range;
974 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name)));
976 // PHI arguments can be constants, catch these here.
977 if (!gimple_range_ssa_p (name))
978 return range_of_expr (r, name);
980 range_on_exit (r, e->src, name);
981 gcc_checking_assert (r.undefined_p ()
982 || range_compatible_p (r.type(), TREE_TYPE (name)));
984 // Check to see if NAME is defined on edge e.
985 if (m_cache.outgoing_edge_range_p (edge_range, e, name))
986 r.intersect (edge_range);
988 return true;
991 // Calculate a range for statement S and return it in R. If NAME is
992 // provided it represents the SSA_NAME on the LHS of the statement.
993 // It is only required if there is more than one lhs/output. Check
994 // the global cache for NAME first to see if the evaluation can be
995 // avoided. If a range cannot be calculated, return false and UNDEFINED.
997 bool
998 gimple_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1000 r.set_undefined ();
1002 if (!name)
1003 name = gimple_get_lhs (s);
1005 // If no name, simply call the base routine.
1006 if (!name)
1007 return calc_stmt (r, s, NULL_TREE);
1009 if (!gimple_range_ssa_p (name))
1010 return false;
1012 // If this STMT has already been processed, return that value.
1013 if (m_cache.m_globals.get_global_range (r, name))
1014 return true;
1016 // Avoid infinite recursion by initializing global cache
1017 int_range_max tmp = gimple_range_global (name);
1018 m_cache.m_globals.set_global_range (name, tmp);
1020 calc_stmt (r, s, name);
1022 if (is_a<gphi *> (s))
1023 r.intersect (tmp);
1024 m_cache.m_globals.set_global_range (name, r);
1025 return true;
1028 // This routine will export whatever global ranges are known to GCC
1029 // SSA_RANGE_NAME_INFO fields.
1031 void
1032 gimple_ranger::export_global_ranges ()
1034 unsigned x;
1035 int_range_max r;
1036 if (dump_file)
1038 fprintf (dump_file, "Exported global range table\n");
1039 fprintf (dump_file, "===========================\n");
1042 for ( x = 1; x < num_ssa_names; x++)
1044 tree name = ssa_name (x);
1045 if (name && !SSA_NAME_IN_FREE_LIST (name)
1046 && gimple_range_ssa_p (name)
1047 && m_cache.m_globals.get_global_range (r, name)
1048 && !r.varying_p())
1050 // Make sure the new range is a subset of the old range.
1051 int_range_max old_range;
1052 old_range = gimple_range_global (name);
1053 old_range.intersect (r);
1054 /* Disable this while we fix tree-ssa/pr61743-2.c. */
1055 //gcc_checking_assert (old_range == r);
1057 // WTF? Can't write non-null pointer ranges?? stupid set_range_info!
1058 if (!POINTER_TYPE_P (TREE_TYPE (name)) && !r.undefined_p ())
1060 value_range vr = r;
1061 set_range_info (name, vr);
1062 if (dump_file)
1064 print_generic_expr (dump_file, name , TDF_SLIM);
1065 fprintf (dump_file, " --> ");
1066 vr.dump (dump_file);
1067 fprintf (dump_file, "\n");
1068 fprintf (dump_file, " irange : ");
1069 r.dump (dump_file);
1070 fprintf (dump_file, "\n");
1077 // Print the known table values to file F.
1079 void
1080 gimple_ranger::dump (FILE *f)
1082 basic_block bb;
1084 FOR_EACH_BB_FN (bb, cfun)
1086 unsigned x;
1087 edge_iterator ei;
1088 edge e;
1089 int_range_max range;
1090 fprintf (f, "\n=========== BB %d ============\n", bb->index);
1091 m_cache.m_on_entry.dump (f, bb);
1093 dump_bb (f, bb, 4, TDF_NONE);
1095 // Now find any globals defined in this block.
1096 for (x = 1; x < num_ssa_names; x++)
1098 tree name = ssa_name (x);
1099 if (gimple_range_ssa_p (name) && SSA_NAME_DEF_STMT (name) &&
1100 gimple_bb (SSA_NAME_DEF_STMT (name)) == bb &&
1101 m_cache.m_globals.get_global_range (range, name))
1103 if (!range.varying_p ())
1105 print_generic_expr (f, name, TDF_SLIM);
1106 fprintf (f, " : ");
1107 range.dump (f);
1108 fprintf (f, "\n");
1114 // And now outgoing edges, if they define anything.
1115 FOR_EACH_EDGE (e, ei, bb->succs)
1117 for (x = 1; x < num_ssa_names; x++)
1119 tree name = gimple_range_ssa_p (ssa_name (x));
1120 if (name && m_cache.outgoing_edge_range_p (range, e, name))
1122 gimple *s = SSA_NAME_DEF_STMT (name);
1123 // Only print the range if this is the def block, or
1124 // the on entry cache for either end of the edge is
1125 // set.
1126 if ((s && bb == gimple_bb (s)) ||
1127 m_cache.block_range (range, bb, name, false) ||
1128 m_cache.block_range (range, e->dest, name, false))
1130 range_on_edge (range, e, name);
1131 if (!range.varying_p ())
1133 fprintf (f, "%d->%d ", e->src->index,
1134 e->dest->index);
1135 char c = ' ';
1136 if (e->flags & EDGE_TRUE_VALUE)
1137 fprintf (f, " (T)%c", c);
1138 else if (e->flags & EDGE_FALSE_VALUE)
1139 fprintf (f, " (F)%c", c);
1140 else
1141 fprintf (f, " ");
1142 print_generic_expr (f, name, TDF_SLIM);
1143 fprintf(f, " : \t");
1144 range.dump(f);
1145 fprintf (f, "\n");
1153 m_cache.m_globals.dump (dump_file);
1154 fprintf (f, "\n");
1156 if (dump_flags & TDF_DETAILS)
1158 fprintf (f, "\nDUMPING GORI MAP\n");
1159 m_cache.dump (f);
1160 fprintf (f, "\n");
1164 // If SCEV has any information about phi node NAME, return it as a range in R.
1166 void
1167 gimple_ranger::range_of_ssa_name_with_loop_info (irange &r, tree name,
1168 class loop *l, gphi *phi)
1170 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1171 tree min, max, type = TREE_TYPE (name);
1172 if (bounds_of_var_in_loop (&min, &max, this, l, phi, name))
1174 // ?? We could do better here. Since MIN/MAX can only be an
1175 // SSA, SSA +- INTEGER_CST, or INTEGER_CST, we could easily call
1176 // the ranger and solve anything not an integer.
1177 if (TREE_CODE (min) != INTEGER_CST)
1178 min = vrp_val_min (type);
1179 if (TREE_CODE (max) != INTEGER_CST)
1180 max = vrp_val_max (type);
1181 r.set (min, max);
1183 else
1184 r.set_varying (type);
1187 // --------------------------------------------------------------------------
1188 // trace_ranger implementation.
1191 trace_ranger::trace_ranger ()
1193 indent = 0;
1194 trace_count = 0;
1197 // If dumping, return true and print the prefix for the next output line.
1199 bool
1200 trace_ranger::dumping (unsigned counter, bool trailing)
1202 if (dump_file && (dump_flags & TDF_DETAILS))
1204 // Print counter index as well as INDENT spaces.
1205 if (!trailing)
1206 fprintf (dump_file, " %-7u ", counter);
1207 else
1208 fprintf (dump_file, " ");
1209 unsigned x;
1210 for (x = 0; x< indent; x++)
1211 fputc (' ', dump_file);
1212 return true;
1214 return false;
1217 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
1218 // returning RESULT.
1220 bool
1221 trace_ranger::trailer (unsigned counter, const char *caller, bool result,
1222 tree name, const irange &r)
1224 if (dumping (counter, true))
1226 indent -= bump;
1227 fputs(result ? "TRUE : " : "FALSE : ", dump_file);
1228 fprintf (dump_file, "(%u) ", counter);
1229 fputs (caller, dump_file);
1230 fputs (" (",dump_file);
1231 if (name)
1232 print_generic_expr (dump_file, name, TDF_SLIM);
1233 fputs (") ",dump_file);
1234 if (result)
1236 r.dump (dump_file);
1237 fputc('\n', dump_file);
1239 else
1240 fputc('\n', dump_file);
1241 // Marks the end of a request.
1242 if (indent == 0)
1243 fputc('\n', dump_file);
1245 return result;
1248 // Tracing version of range_on_edge. Call it with printing wrappers.
1250 bool
1251 trace_ranger::range_on_edge (irange &r, edge e, tree name)
1253 unsigned idx = ++trace_count;
1254 if (dumping (idx))
1256 fprintf (dump_file, "range_on_edge (");
1257 print_generic_expr (dump_file, name, TDF_SLIM);
1258 fprintf (dump_file, ") on edge %d->%d\n", e->src->index, e->dest->index);
1259 indent += bump;
1262 bool res = gimple_ranger::range_on_edge (r, e, name);
1263 trailer (idx, "range_on_edge", true, name, r);
1264 return res;
1267 // Tracing version of range_on_entry. Call it with printing wrappers.
1269 void
1270 trace_ranger::range_on_entry (irange &r, basic_block bb, tree name)
1272 unsigned idx = ++trace_count;
1273 if (dumping (idx))
1275 fprintf (dump_file, "range_on_entry (");
1276 print_generic_expr (dump_file, name, TDF_SLIM);
1277 fprintf (dump_file, ") to BB %d\n", bb->index);
1278 indent += bump;
1281 gimple_ranger::range_on_entry (r, bb, name);
1283 trailer (idx, "range_on_entry", true, name, r);
1286 // Tracing version of range_on_exit. Call it with printing wrappers.
1288 void
1289 trace_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1291 unsigned idx = ++trace_count;
1292 if (dumping (idx))
1294 fprintf (dump_file, "range_on_exit (");
1295 print_generic_expr (dump_file, name, TDF_SLIM);
1296 fprintf (dump_file, ") from BB %d\n", bb->index);
1297 indent += bump;
1300 gimple_ranger::range_on_exit (r, bb, name);
1302 trailer (idx, "range_on_exit", true, name, r);
1305 // Tracing version of range_of_stmt. Call it with printing wrappers.
1307 bool
1308 trace_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1310 bool res;
1311 unsigned idx = ++trace_count;
1312 if (dumping (idx))
1314 fprintf (dump_file, "range_of_stmt (");
1315 if (name)
1316 print_generic_expr (dump_file, name, TDF_SLIM);
1317 fputs (") at stmt ", dump_file);
1318 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1319 indent += bump;
1322 res = gimple_ranger::range_of_stmt (r, s, name);
1324 return trailer (idx, "range_of_stmt", res, name, r);
1327 // Tracing version of range_of_expr. Call it with printing wrappers.
1329 bool
1330 trace_ranger::range_of_expr (irange &r, tree name, gimple *s)
1332 bool res;
1333 unsigned idx = ++trace_count;
1334 if (dumping (idx))
1336 fprintf (dump_file, "range_of_expr(");
1337 print_generic_expr (dump_file, name, TDF_SLIM);
1338 fputs (")", dump_file);
1339 if (s)
1341 fputs (" at stmt ", dump_file);
1342 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1344 else
1345 fputs ("\n", dump_file);
1346 indent += bump;
1349 res = gimple_ranger::range_of_expr (r, name, s);
1351 return trailer (idx, "range_of_expr", res, name, r);