mklog: add subject line skeleton
[official-gcc.git] / gcc / gimple-range.cc
blob624cfb12562a9528c34d53ae395264d908704459
1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
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"
50 // Evaluate expression EXPR using the source information the class was
51 // instantiated with. Place the result in R, and return TRUE. If a range
52 // cannot be calculated, return FALSE.
54 bool
55 fur_source::get_operand (irange &r, tree expr)
57 return get_range_query (cfun)->range_of_expr (r, expr);
60 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
61 // range_query to get the range on the edge.
63 bool
64 fur_source::get_phi_operand (irange &r, tree expr, edge e)
66 return get_range_query (cfun)->range_on_edge (r, e, expr);
69 // Default is to not register any dependencies from fold_using_range.
71 void
72 fur_source::register_dependency (tree lhs ATTRIBUTE_UNUSED,
73 tree rhs ATTRIBUTE_UNUSED)
77 // Default object is the current range query.
79 range_query *
80 fur_source::query ()
82 return get_range_query (cfun);
85 // This version of fur_source will pick a range up off an edge.
87 class fur_edge : public fur_source
89 public:
90 fur_edge (edge e, range_query *q = NULL);
91 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
92 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
93 virtual range_query *query () OVERRIDE;
94 private:
95 range_query *m_query;
96 edge m_edge;
99 // Instantiate an edge based fur_source.
101 inline
102 fur_edge::fur_edge (edge e, range_query *q)
104 m_edge = e;
105 if (q)
106 m_query = q;
107 else
108 m_query = get_range_query (cfun);
111 // Get the value of EXPR on edge m_edge.
113 bool
114 fur_edge::get_operand (irange &r, tree expr)
116 return m_query->range_on_edge (r, m_edge, expr);
119 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
120 // range_query to get the range on the edge.
122 bool
123 fur_edge::get_phi_operand (irange &r, tree expr, edge e)
125 // edge to edge recalculations not supoprted yet, until we sort it out.
126 gcc_checking_assert (e == m_edge);
127 return m_query->range_on_edge (r, e, expr);
130 // Return the current range_query object.
132 range_query *
133 fur_edge::query ()
135 return m_query;
138 // Instantiate a stmt based fur_source.
140 fur_stmt::fur_stmt (gimple *s, range_query *q)
142 m_stmt = s;
143 if (q)
144 m_query = q;
145 else
146 m_query = get_global_range_query ();
149 // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
151 bool
152 fur_stmt::get_operand (irange &r, tree expr)
154 return m_query->range_of_expr (r, expr, m_stmt);
157 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
158 // range_query to get the range on the edge.
160 bool
161 fur_stmt::get_phi_operand (irange &r, tree expr, edge e)
163 // Pick up the range of expr from edge E.
164 fur_edge e_src (e, m_query);
165 return e_src.get_operand (r, expr);
168 // Return the current range_query object.
170 range_query *
171 fur_stmt::query ()
173 return m_query;
176 // This version of fur_source will pick a range from a stmt, and also register
177 // dependencies via a gori_compute object. This is mostly an internal API.
179 class fur_depend : public fur_stmt
181 public:
182 fur_depend (gimple *s, gori_compute *gori, range_query *q = NULL);
183 virtual void register_dependency (tree lhs, tree rhs) OVERRIDE;
184 private:
185 gori_compute *m_gori;
188 // Instantiate a stmt based fur_source with a GORI object
190 inline
191 fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
192 : fur_stmt (s, q)
194 gcc_checking_assert (gori);
195 m_gori = gori;
198 // find and add any dependnecy between LHS and RHS
200 void
201 fur_depend::register_dependency (tree lhs, tree rhs)
203 m_gori->register_dependency (lhs, rhs);
206 // This version of fur_source will pick a range up from a list of ranges
207 // supplied by the caller.
209 class fur_list : public fur_source
211 public:
212 fur_list (irange &r1);
213 fur_list (irange &r1, irange &r2);
214 fur_list (unsigned num, irange *list);
215 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
216 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
217 private:
218 int_range_max m_local[2];
219 irange *m_list;
220 unsigned m_index;
221 unsigned m_limit;
224 // One range supplied for unary operations.
226 fur_list::fur_list (irange &r1)
228 m_list = m_local;
229 m_index = 0;
230 m_limit = 1;
231 m_local[0] = r1;
234 // Two ranges supplied for binary operations.
236 fur_list::fur_list (irange &r1, irange &r2)
238 m_list = m_local;
239 m_index = 0;
240 m_limit = 2;
241 m_local[0] = r1;
242 m_local[0] = r2;
245 // Arbitrary number of ranges in a vector.
247 fur_list::fur_list (unsigned num, irange *list)
249 m_list = list;
250 m_index = 0;
251 m_limit = num;
254 // Get the next operand from the vector, ensure types are compatible.
256 bool
257 fur_list::get_operand (irange &r, tree expr)
259 if (m_index >= m_limit)
260 return get_range_query (cfun)->range_of_expr (r, expr);
261 r = m_list[m_index++];
262 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
263 return true;
266 // This will simply pick the next operand from the vector.
267 bool
268 fur_list::get_phi_operand (irange &r, tree expr, edge e ATTRIBUTE_UNUSED)
270 return get_operand (r, expr);
273 // Fold stmt S into range R using R1 as the first operand.
275 bool
276 fold_range (irange &r, gimple *s, irange &r1)
278 fold_using_range f;
279 fur_list src (r1);
280 return f.fold_stmt (r, s, src);
283 // Fold stmt S into range R using R1 and R2 as the first two operands.
285 bool
286 fold_range (irange &r, gimple *s, irange &r1, irange &r2)
288 fold_using_range f;
289 fur_list src (r1, r2);
290 return f.fold_stmt (r, s, src);
294 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
295 // operands encountered.
297 bool
298 fold_range (irange &r, gimple *s, unsigned num_elements, irange *vector)
300 fold_using_range f;
301 fur_list src (num_elements, vector);
302 return f.fold_stmt (r, s, src);
305 // Fold stmt S into range R using range query Q.
307 bool
308 fold_range (irange &r, gimple *s, range_query *q)
310 fold_using_range f;
311 fur_stmt src (s, q);
312 return f.fold_stmt (r, s, src);
315 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
317 bool
318 fold_range (irange &r, gimple *s, edge on_edge, range_query *q)
320 fold_using_range f;
321 fur_edge src (on_edge, q);
322 return f.fold_stmt (r, s, src);
325 // -------------------------------------------------------------------------
327 // Adjust the range for a pointer difference where the operands came
328 // from a memchr.
330 // This notices the following sequence:
332 // def = __builtin_memchr (arg, 0, sz)
333 // n = def - arg
335 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
337 static void
338 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
340 tree op0 = gimple_assign_rhs1 (diff_stmt);
341 tree op1 = gimple_assign_rhs2 (diff_stmt);
342 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
343 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
344 gimple *call;
346 if (TREE_CODE (op0) == SSA_NAME
347 && TREE_CODE (op1) == SSA_NAME
348 && (call = SSA_NAME_DEF_STMT (op0))
349 && is_gimple_call (call)
350 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
351 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
352 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
353 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
354 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
355 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
356 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
357 && integer_zerop (gimple_call_arg (call, 1)))
359 tree max = vrp_val_max (ptrdiff_type_node);
360 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
361 tree expr_type = gimple_expr_type (diff_stmt);
362 tree range_min = build_zero_cst (expr_type);
363 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
364 int_range<2> r (range_min, range_max);
365 res.intersect (r);
369 // This function looks for situations when walking the use/def chains
370 // may provide additonal contextual range information not exposed on
371 // this statement. Like knowing the IMAGPART return value from a
372 // builtin function is a boolean result.
374 // We should rework how we're called, as we have an op_unknown entry
375 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
376 // function gets called.
378 static void
379 gimple_range_adjustment (irange &res, const gimple *stmt)
381 switch (gimple_expr_code (stmt))
383 case POINTER_DIFF_EXPR:
384 adjust_pointer_diff_expr (res, stmt);
385 return;
387 case IMAGPART_EXPR:
389 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
390 if (TREE_CODE (name) == SSA_NAME)
392 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
393 if (def_stmt && is_gimple_call (def_stmt)
394 && gimple_call_internal_p (def_stmt))
396 switch (gimple_call_internal_fn (def_stmt))
398 case IFN_ADD_OVERFLOW:
399 case IFN_SUB_OVERFLOW:
400 case IFN_MUL_OVERFLOW:
401 case IFN_ATOMIC_COMPARE_EXCHANGE:
403 int_range<2> r;
404 r.set_varying (boolean_type_node);
405 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
406 range_cast (r, type);
407 res.intersect (r);
409 default:
410 break;
414 break;
417 default:
418 break;
422 // Return the base of the RHS of an assignment.
424 static tree
425 gimple_range_base_of_assignment (const gimple *stmt)
427 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
428 tree op1 = gimple_assign_rhs1 (stmt);
429 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
430 return get_base_address (TREE_OPERAND (op1, 0));
431 return op1;
434 // Return the first operand of this statement if it is a valid operand
435 // supported by ranges, otherwise return NULL_TREE. Special case is
436 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
438 tree
439 gimple_range_operand1 (const gimple *stmt)
441 gcc_checking_assert (gimple_range_handler (stmt));
443 switch (gimple_code (stmt))
445 case GIMPLE_COND:
446 return gimple_cond_lhs (stmt);
447 case GIMPLE_ASSIGN:
449 tree base = gimple_range_base_of_assignment (stmt);
450 if (base && TREE_CODE (base) == MEM_REF)
452 // If the base address is an SSA_NAME, we return it
453 // here. This allows processing of the range of that
454 // name, while the rest of the expression is simply
455 // ignored. The code in range_ops will see the
456 // ADDR_EXPR and do the right thing.
457 tree ssa = TREE_OPERAND (base, 0);
458 if (TREE_CODE (ssa) == SSA_NAME)
459 return ssa;
461 return base;
463 default:
464 break;
466 return NULL;
469 // Return the second operand of statement STMT, otherwise return NULL_TREE.
471 tree
472 gimple_range_operand2 (const gimple *stmt)
474 gcc_checking_assert (gimple_range_handler (stmt));
476 switch (gimple_code (stmt))
478 case GIMPLE_COND:
479 return gimple_cond_rhs (stmt);
480 case GIMPLE_ASSIGN:
481 if (gimple_num_ops (stmt) >= 3)
482 return gimple_assign_rhs2 (stmt);
483 default:
484 break;
486 return NULL_TREE;
489 // Calculate what we can determine of the range of this unary
490 // statement's operand if the lhs of the expression has the range
491 // LHS_RANGE. Return false if nothing can be determined.
493 bool
494 gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range)
496 gcc_checking_assert (gimple_num_ops (stmt) < 3);
498 // An empty range is viral.
499 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
500 if (lhs_range.undefined_p ())
502 r.set_undefined ();
503 return true;
505 // Unary operations require the type of the first operand in the
506 // second range position.
507 int_range<2> type_range (type);
508 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
509 type_range);
512 // Calculate what we can determine of the range of this statement's
513 // first operand if the lhs of the expression has the range LHS_RANGE
514 // and the second operand has the range OP2_RANGE. Return false if
515 // nothing can be determined.
517 bool
518 gimple_range_calc_op1 (irange &r, const gimple *stmt,
519 const irange &lhs_range, const irange &op2_range)
521 // Unary operation are allowed to pass a range in for second operand
522 // as there are often additional restrictions beyond the type which
523 // can be imposed. See operator_cast::op1_range().
524 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
525 // An empty range is viral.
526 if (op2_range.undefined_p () || lhs_range.undefined_p ())
528 r.set_undefined ();
529 return true;
531 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
532 op2_range);
535 // Calculate what we can determine of the range of this statement's
536 // second operand if the lhs of the expression has the range LHS_RANGE
537 // and the first operand has the range OP1_RANGE. Return false if
538 // nothing can be determined.
540 bool
541 gimple_range_calc_op2 (irange &r, const gimple *stmt,
542 const irange &lhs_range, const irange &op1_range)
544 tree type = TREE_TYPE (gimple_range_operand2 (stmt));
545 // An empty range is viral.
546 if (op1_range.undefined_p () || lhs_range.undefined_p ())
548 r.set_undefined ();
549 return true;
551 return gimple_range_handler (stmt)->op2_range (r, type, lhs_range,
552 op1_range);
555 // Calculate a range for statement S and return it in R. If NAME is provided it
556 // represents the SSA_NAME on the LHS of the statement. It is only required
557 // if there is more than one lhs/output. If a range cannot
558 // be calculated, return false.
560 bool
561 fold_using_range::fold_stmt (irange &r, gimple *s, fur_source &src, tree name)
563 bool res = false;
564 // If name and S are specified, make sure it is an LHS of S.
565 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
566 name == gimple_get_lhs (s));
568 if (!name)
569 name = gimple_get_lhs (s);
571 // Process addresses.
572 if (gimple_code (s) == GIMPLE_ASSIGN
573 && gimple_assign_rhs_code (s) == ADDR_EXPR)
574 return range_of_address (r, s, src);
576 if (gimple_range_handler (s))
577 res = range_of_range_op (r, s, src);
578 else if (is_a<gphi *>(s))
579 res = range_of_phi (r, as_a<gphi *> (s), src);
580 else if (is_a<gcall *>(s))
581 res = range_of_call (r, as_a<gcall *> (s), src);
582 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
583 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
585 if (!res)
587 // If no name is specified, try the expression kind.
588 if (!name)
590 tree t = gimple_expr_type (s);
591 if (!irange::supports_type_p (t))
592 return false;
593 r.set_varying (t);
594 return true;
596 if (!gimple_range_ssa_p (name))
597 return false;
598 // We don't understand the stmt, so return the global range.
599 r = gimple_range_global (name);
600 return true;
603 if (r.undefined_p ())
604 return true;
606 // We sometimes get compatible types copied from operands, make sure
607 // the correct type is being returned.
608 if (name && TREE_TYPE (name) != r.type ())
610 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
611 range_cast (r, TREE_TYPE (name));
613 return true;
616 // Calculate a range for range_op statement S and return it in R. If any
617 // If a range cannot be calculated, return false.
619 bool
620 fold_using_range::range_of_range_op (irange &r, gimple *s, fur_source &src)
622 int_range_max range1, range2;
623 tree type = gimple_expr_type (s);
624 range_operator *handler = gimple_range_handler (s);
625 gcc_checking_assert (handler);
626 gcc_checking_assert (irange::supports_type_p (type));
628 tree lhs = gimple_get_lhs (s);
629 tree op1 = gimple_range_operand1 (s);
630 tree op2 = gimple_range_operand2 (s);
632 if (src.get_operand (range1, op1))
634 if (!op2)
636 // Fold range, and register any dependency if available.
637 int_range<2> r2 (type);
638 handler->fold_range (r, type, range1, r2);
639 if (lhs)
640 src.register_dependency (lhs, op1);
642 else if (src.get_operand (range2, op2))
644 // Fold range, and register any dependency if available.
645 handler->fold_range (r, type, range1, range2);
646 if (lhs)
648 src.register_dependency (lhs, op1);
649 src.register_dependency (lhs, op2);
652 else
653 r.set_varying (type);
655 else
656 r.set_varying (type);
657 // Make certain range-op adjustments that aren't handled any other way.
658 gimple_range_adjustment (r, s);
659 return true;
662 // Calculate the range of an assignment containing an ADDR_EXPR.
663 // Return the range in R.
664 // If a range cannot be calculated, set it to VARYING and return true.
666 bool
667 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
669 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
670 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
672 bool strict_overflow_p;
673 tree expr = gimple_assign_rhs1 (stmt);
674 poly_int64 bitsize, bitpos;
675 tree offset;
676 machine_mode mode;
677 int unsignedp, reversep, volatilep;
678 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
679 &bitpos, &offset, &mode, &unsignedp,
680 &reversep, &volatilep);
683 if (base != NULL_TREE
684 && TREE_CODE (base) == MEM_REF
685 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
687 tree ssa = TREE_OPERAND (base, 0);
688 tree lhs = gimple_get_lhs (stmt);
689 if (lhs && gimple_range_ssa_p (ssa))
690 src.register_dependency (lhs, ssa);
691 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
692 src.get_operand (r, ssa);
693 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
695 poly_offset_int off = 0;
696 bool off_cst = false;
697 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
699 off = mem_ref_offset (base);
700 if (offset)
701 off += poly_offset_int::from (wi::to_poly_wide (offset),
702 SIGNED);
703 off <<= LOG2_BITS_PER_UNIT;
704 off += bitpos;
705 off_cst = true;
707 /* If &X->a is equal to X, the range of X is the result. */
708 if (off_cst && known_eq (off, 0))
709 return true;
710 else if (flag_delete_null_pointer_checks
711 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
713 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
714 allow going from non-NULL pointer to NULL. */
715 if(!range_includes_zero_p (&r))
716 return true;
718 /* If MEM_REF has a "positive" offset, consider it non-NULL
719 always, for -fdelete-null-pointer-checks also "negative"
720 ones. Punt for unknown offsets (e.g. variable ones). */
721 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
722 && off_cst
723 && known_ne (off, 0)
724 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
726 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
727 return true;
729 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
730 return true;
733 // Handle "= &a".
734 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
736 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
737 return true;
740 // Otherwise return varying.
741 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
742 return true;
745 // Calculate a range for phi statement S and return it in R.
746 // If a range cannot be calculated, return false.
748 bool
749 fold_using_range::range_of_phi (irange &r, gphi *phi, fur_source &src)
751 tree phi_def = gimple_phi_result (phi);
752 tree type = TREE_TYPE (phi_def);
753 int_range_max arg_range;
754 unsigned x;
756 if (!irange::supports_type_p (type))
757 return false;
759 // Start with an empty range, unioning in each argument's range.
760 r.set_undefined ();
761 for (x = 0; x < gimple_phi_num_args (phi); x++)
763 tree arg = gimple_phi_arg_def (phi, x);
764 edge e = gimple_phi_arg_edge (phi, x);
766 // Register potential dependencies for stale value tracking.
767 if (gimple_range_ssa_p (arg))
768 src.register_dependency (phi_def, arg);
770 // Get the range of the argument on its edge.
771 src.get_phi_operand (arg_range, arg, e);
772 // If we're recomputing the argument elsewhere, try to refine it.
773 r.union_ (arg_range);
774 // Once the value reaches varying, stop looking.
775 if (r.varying_p ())
776 break;
779 // If SCEV is available, query if this PHI has any knonwn values.
780 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
782 value_range loop_range;
783 class loop *l = loop_containing_stmt (phi);
784 if (l && loop_outer (l))
786 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
787 if (!loop_range.varying_p ())
789 if (dump_file && (dump_flags & TDF_DETAILS))
791 fprintf (dump_file, " Loops range found for ");
792 print_generic_expr (dump_file, phi_def, TDF_SLIM);
793 fprintf (dump_file, ": ");
794 loop_range.dump (dump_file);
795 fprintf (dump_file, " and calculated range :");
796 r.dump (dump_file);
797 fprintf (dump_file, "\n");
799 r.intersect (loop_range);
804 return true;
807 // Calculate a range for call statement S and return it in R.
808 // If a range cannot be calculated, return false.
810 bool
811 fold_using_range::range_of_call (irange &r, gcall *call, fur_source &src)
813 tree type = gimple_call_return_type (call);
814 tree lhs = gimple_call_lhs (call);
815 bool strict_overflow_p;
817 if (!irange::supports_type_p (type))
818 return false;
820 if (range_of_builtin_call (r, call, src))
822 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
823 r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
824 else if (gimple_call_nonnull_result_p (call)
825 || gimple_call_nonnull_arg (call))
826 r = range_nonzero (type);
827 else
828 r.set_varying (type);
830 // If there is an LHS, intersect that with what is known.
831 if (lhs)
833 value_range def;
834 def = gimple_range_global (lhs);
835 r.intersect (def);
837 return true;
840 // Return the range of a __builtin_ubsan* in CALL and set it in R.
841 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
842 // MULT_EXPR).
844 void
845 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
846 tree_code code, fur_source &src)
848 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
849 || code == MULT_EXPR);
850 tree type = gimple_call_return_type (call);
851 range_operator *op = range_op_handler (code, type);
852 gcc_checking_assert (op);
853 int_range_max ir0, ir1;
854 tree arg0 = gimple_call_arg (call, 0);
855 tree arg1 = gimple_call_arg (call, 1);
856 src.get_operand (ir0, arg0);
857 src.get_operand (ir1, arg1);
859 bool saved_flag_wrapv = flag_wrapv;
860 // Pretend the arithmetic is wrapping. If there is any overflow,
861 // we'll complain, but will actually do wrapping operation.
862 flag_wrapv = 1;
863 op->fold_range (r, type, ir0, ir1);
864 flag_wrapv = saved_flag_wrapv;
866 // If for both arguments vrp_valueize returned non-NULL, this should
867 // have been already folded and if not, it wasn't folded because of
868 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
869 if (r.singleton_p ())
870 r.set_varying (type);
873 // For a builtin in CALL, return a range in R if known and return
874 // TRUE. Otherwise return FALSE.
876 bool
877 fold_using_range::range_of_builtin_call (irange &r, gcall *call,
878 fur_source &src)
880 combined_fn func = gimple_call_combined_fn (call);
881 if (func == CFN_LAST)
882 return false;
884 tree type = gimple_call_return_type (call);
885 tree arg;
886 int mini, maxi, zerov = 0, prec;
887 scalar_int_mode mode;
889 switch (func)
891 case CFN_BUILT_IN_CONSTANT_P:
892 if (cfun->after_inlining)
894 r.set_zero (type);
895 // r.equiv_clear ();
896 return true;
898 arg = gimple_call_arg (call, 0);
899 if (src.get_operand (r, arg) && r.singleton_p ())
901 r.set (build_one_cst (type), build_one_cst (type));
902 return true;
904 break;
906 CASE_CFN_FFS:
907 CASE_CFN_POPCOUNT:
908 // __builtin_ffs* and __builtin_popcount* return [0, prec].
909 arg = gimple_call_arg (call, 0);
910 prec = TYPE_PRECISION (TREE_TYPE (arg));
911 mini = 0;
912 maxi = prec;
913 src.get_operand (r, arg);
914 // If arg is non-zero, then ffs or popcount are non-zero.
915 if (!range_includes_zero_p (&r))
916 mini = 1;
917 // If some high bits are known to be zero, decrease the maximum.
918 if (!r.undefined_p ())
920 if (TYPE_SIGN (r.type ()) == SIGNED)
921 range_cast (r, unsigned_type_for (r.type ()));
922 wide_int max = r.upper_bound ();
923 maxi = wi::floor_log2 (max) + 1;
925 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
926 return true;
928 CASE_CFN_PARITY:
929 r.set (build_zero_cst (type), build_one_cst (type));
930 return true;
932 CASE_CFN_CLZ:
933 // __builtin_c[lt]z* return [0, prec-1], except when the
934 // argument is 0, but that is undefined behavior.
936 // For __builtin_c[lt]z* consider argument of 0 always undefined
937 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
938 arg = gimple_call_arg (call, 0);
939 prec = TYPE_PRECISION (TREE_TYPE (arg));
940 mini = 0;
941 maxi = prec - 1;
942 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
943 if (gimple_call_internal_p (call))
945 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
946 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
948 // Only handle the single common value.
949 if (zerov == prec)
950 maxi = prec;
951 else
952 // Magic value to give up, unless we can prove arg is non-zero.
953 mini = -2;
957 src.get_operand (r, arg);
958 // From clz of minimum we can compute result maximum.
959 if (!r.undefined_p ())
961 // From clz of minimum we can compute result maximum.
962 if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
964 maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
965 if (mini == -2)
966 mini = 0;
968 else if (!range_includes_zero_p (&r))
970 mini = 0;
971 maxi = prec - 1;
973 if (mini == -2)
974 break;
975 // From clz of maximum we can compute result minimum.
976 wide_int max = r.upper_bound ();
977 int newmini = prec - 1 - wi::floor_log2 (max);
978 if (max == 0)
980 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
981 // return [prec, prec], otherwise ignore the range.
982 if (maxi == prec)
983 mini = prec;
985 else
986 mini = newmini;
988 if (mini == -2)
989 break;
990 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
991 return true;
993 CASE_CFN_CTZ:
994 // __builtin_ctz* return [0, prec-1], except for when the
995 // argument is 0, but that is undefined behavior.
997 // For __builtin_ctz* consider argument of 0 always undefined
998 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
999 arg = gimple_call_arg (call, 0);
1000 prec = TYPE_PRECISION (TREE_TYPE (arg));
1001 mini = 0;
1002 maxi = prec - 1;
1003 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1004 if (gimple_call_internal_p (call))
1006 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1007 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1009 // Handle only the two common values.
1010 if (zerov == -1)
1011 mini = -1;
1012 else if (zerov == prec)
1013 maxi = prec;
1014 else
1015 // Magic value to give up, unless we can prove arg is non-zero.
1016 mini = -2;
1019 src.get_operand (r, arg);
1020 if (!r.undefined_p ())
1022 // If arg is non-zero, then use [0, prec - 1].
1023 if (!range_includes_zero_p (&r))
1025 mini = 0;
1026 maxi = prec - 1;
1028 // If some high bits are known to be zero, we can decrease
1029 // the maximum.
1030 wide_int max = r.upper_bound ();
1031 if (max == 0)
1033 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1034 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1035 // Otherwise ignore the range.
1036 if (mini == -1)
1037 maxi = -1;
1038 else if (maxi == prec)
1039 mini = prec;
1041 // If value at zero is prec and 0 is in the range, we can't lower
1042 // the upper bound. We could create two separate ranges though,
1043 // [0,floor_log2(max)][prec,prec] though.
1044 else if (maxi != prec)
1045 maxi = wi::floor_log2 (max);
1047 if (mini == -2)
1048 break;
1049 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1050 return true;
1052 CASE_CFN_CLRSB:
1053 arg = gimple_call_arg (call, 0);
1054 prec = TYPE_PRECISION (TREE_TYPE (arg));
1055 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
1056 return true;
1057 case CFN_UBSAN_CHECK_ADD:
1058 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
1059 return true;
1060 case CFN_UBSAN_CHECK_SUB:
1061 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
1062 return true;
1063 case CFN_UBSAN_CHECK_MUL:
1064 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
1065 return true;
1067 case CFN_GOACC_DIM_SIZE:
1068 case CFN_GOACC_DIM_POS:
1069 // Optimizing these two internal functions helps the loop
1070 // optimizer eliminate outer comparisons. Size is [1,N]
1071 // and pos is [0,N-1].
1073 bool is_pos = func == CFN_GOACC_DIM_POS;
1074 int axis = oacc_get_ifn_dim_arg (call);
1075 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1076 if (!size)
1077 // If it's dynamic, the backend might know a hardware limitation.
1078 size = targetm.goacc.dim_limit (axis);
1080 r.set (build_int_cst (type, is_pos ? 0 : 1),
1081 size
1082 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1083 return true;
1086 case CFN_BUILT_IN_STRLEN:
1087 if (tree lhs = gimple_call_lhs (call))
1088 if (ptrdiff_type_node
1089 && (TYPE_PRECISION (ptrdiff_type_node)
1090 == TYPE_PRECISION (TREE_TYPE (lhs))))
1092 tree type = TREE_TYPE (lhs);
1093 tree max = vrp_val_max (ptrdiff_type_node);
1094 wide_int wmax
1095 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1096 tree range_min = build_zero_cst (type);
1097 // To account for the terminating NULL, the maximum length
1098 // is one less than the maximum array size, which in turn
1099 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1100 // smaller than the former type).
1101 // FIXME: Use max_object_size() - 1 here.
1102 tree range_max = wide_int_to_tree (type, wmax - 2);
1103 r.set (range_min, range_max);
1104 return true;
1106 break;
1107 default:
1108 break;
1110 return false;
1114 // Calculate a range for COND_EXPR statement S and return it in R.
1115 // If a range cannot be calculated, return false.
1117 bool
1118 fold_using_range::range_of_cond_expr (irange &r, gassign *s, fur_source &src)
1120 int_range_max cond_range, range1, range2;
1121 tree cond = gimple_assign_rhs1 (s);
1122 tree op1 = gimple_assign_rhs2 (s);
1123 tree op2 = gimple_assign_rhs3 (s);
1125 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1126 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1),
1127 TREE_TYPE (op2)));
1128 if (!irange::supports_type_p (TREE_TYPE (op1)))
1129 return false;
1131 src.get_operand (cond_range, cond);
1132 src.get_operand (range1, op1);
1133 src.get_operand (range2, op2);
1135 // If the condition is known, choose the appropriate expression.
1136 if (cond_range.singleton_p ())
1138 // False, pick second operand.
1139 if (cond_range.zero_p ())
1140 r = range2;
1141 else
1142 r = range1;
1144 else
1146 r = range1;
1147 r.union_ (range2);
1149 return true;
1152 bool
1153 gimple_ranger::range_of_expr (irange &r, tree expr, gimple *stmt)
1155 if (!gimple_range_ssa_p (expr))
1156 return get_tree_range (r, expr, stmt);
1158 // If there is no statement, just get the global value.
1159 if (!stmt)
1161 if (!m_cache.get_global_range (r, expr))
1162 r = gimple_range_global (expr);
1163 return true;
1166 // For a debug stmt, pick the best value currently available, do not
1167 // trigger new value calculations. PR 100781.
1168 if (is_gimple_debug (stmt))
1170 bool state = m_cache.enable_new_values (false);
1171 m_cache.range_of_expr (r, expr, stmt);
1172 m_cache.enable_new_values (state);
1173 return true;
1175 basic_block bb = gimple_bb (stmt);
1176 gimple *def_stmt = SSA_NAME_DEF_STMT (expr);
1178 // If name is defined in this block, try to get an range from S.
1179 if (def_stmt && gimple_bb (def_stmt) == bb)
1181 range_of_stmt (r, def_stmt, expr);
1182 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
1183 m_cache.m_non_null.non_null_deref_p (expr, bb))
1184 r = range_nonzero (TREE_TYPE (expr));
1186 else
1187 // Otherwise OP comes from outside this block, use range on entry.
1188 range_on_entry (r, bb, expr);
1190 return true;
1193 // Return the range of NAME on entry to block BB in R.
1195 void
1196 gimple_ranger::range_on_entry (irange &r, basic_block bb, tree name)
1198 int_range_max entry_range;
1199 gcc_checking_assert (gimple_range_ssa_p (name));
1201 // Start with any known range
1202 range_of_stmt (r, SSA_NAME_DEF_STMT (name), name);
1204 // Now see if there is any on_entry value which may refine it.
1205 if (m_cache.block_range (entry_range, bb, name))
1206 r.intersect (entry_range);
1208 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
1209 m_cache.m_non_null.non_null_deref_p (name, bb))
1210 r = range_nonzero (TREE_TYPE (name));
1213 // Calculate the range for NAME at the end of block BB and return it in R.
1214 // Return false if no range can be calculated.
1216 void
1217 gimple_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1219 // on-exit from the exit block?
1220 gcc_checking_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1221 gcc_checking_assert (gimple_range_ssa_p (name));
1223 gimple *s = SSA_NAME_DEF_STMT (name);
1224 basic_block def_bb = gimple_bb (s);
1225 // If this is not the definition block, get the range on the last stmt in
1226 // the block... if there is one.
1227 if (def_bb != bb)
1228 s = last_stmt (bb);
1229 // If there is no statement provided, get the range_on_entry for this block.
1230 if (s)
1231 range_of_expr (r, name, s);
1232 else
1233 range_on_entry (r, bb, name);
1234 gcc_checking_assert (r.undefined_p ()
1235 || range_compatible_p (r.type (), TREE_TYPE (name)));
1238 // Calculate a range for NAME on edge E and return it in R.
1240 bool
1241 gimple_ranger::range_on_edge (irange &r, edge e, tree name)
1243 int_range_max edge_range;
1244 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name)));
1246 // PHI arguments can be constants, catch these here.
1247 if (!gimple_range_ssa_p (name))
1248 return range_of_expr (r, name);
1250 range_on_exit (r, e->src, name);
1251 gcc_checking_assert (r.undefined_p ()
1252 || range_compatible_p (r.type(), TREE_TYPE (name)));
1254 // Check to see if NAME is defined on edge e.
1255 if (m_cache.range_on_edge (edge_range, e, name))
1256 r.intersect (edge_range);
1258 return true;
1261 // fold_range wrapper for range_of_stmt to use as an internal client.
1263 bool
1264 gimple_ranger::fold_range_internal (irange &r, gimple *s, tree name)
1266 fold_using_range f;
1267 fur_depend src (s, &(gori ()), this);
1268 return f.fold_stmt (r, s, src, name);
1271 // Calculate a range for statement S and return it in R. If NAME is
1272 // provided it represents the SSA_NAME on the LHS of the statement.
1273 // It is only required if there is more than one lhs/output. Check
1274 // the global cache for NAME first to see if the evaluation can be
1275 // avoided. If a range cannot be calculated, return false and UNDEFINED.
1277 bool
1278 gimple_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1280 r.set_undefined ();
1282 if (!name)
1283 name = gimple_get_lhs (s);
1285 // If no name, simply call the base routine.
1286 if (!name)
1287 return fold_range_internal (r, s, NULL_TREE);
1289 if (!gimple_range_ssa_p (name))
1290 return false;
1292 // Check if the stmt has already been processed, and is not stale.
1293 if (m_cache.get_non_stale_global_range (r, name))
1294 return true;
1296 // Otherwise calculate a new value.
1297 int_range_max tmp;
1298 fold_range_internal (tmp, s, name);
1300 // Combine the new value with the old value. This is required because
1301 // the way value propagation works, when the IL changes on the fly we
1302 // can sometimes get different results. See PR 97741.
1303 r.intersect (tmp);
1304 m_cache.set_global_range (name, r);
1306 return true;
1309 // This routine will export whatever global ranges are known to GCC
1310 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
1312 void
1313 gimple_ranger::export_global_ranges ()
1315 unsigned x;
1316 int_range_max r;
1317 if (dump_file)
1319 fprintf (dump_file, "Exported global range table\n");
1320 fprintf (dump_file, "===========================\n");
1323 for ( x = 1; x < num_ssa_names; x++)
1325 tree name = ssa_name (x);
1326 if (name && !SSA_NAME_IN_FREE_LIST (name)
1327 && gimple_range_ssa_p (name)
1328 && m_cache.get_global_range (r, name)
1329 && !r.varying_p())
1331 bool updated = update_global_range (r, name);
1333 if (updated && dump_file)
1335 value_range vr = r;
1336 print_generic_expr (dump_file, name , TDF_SLIM);
1337 fprintf (dump_file, " --> ");
1338 vr.dump (dump_file);
1339 fprintf (dump_file, "\n");
1340 int_range_max same = vr;
1341 if (same != r)
1343 fprintf (dump_file, " irange : ");
1344 r.dump (dump_file);
1345 fprintf (dump_file, "\n");
1352 // Print the known table values to file F.
1354 void
1355 gimple_ranger::dump_bb (FILE *f, basic_block bb)
1357 unsigned x;
1358 edge_iterator ei;
1359 edge e;
1360 int_range_max range;
1361 fprintf (f, "\n=========== BB %d ============\n", bb->index);
1362 m_cache.dump_bb (f, bb);
1364 ::dump_bb (f, bb, 4, TDF_NONE);
1366 // Now find any globals defined in this block.
1367 for (x = 1; x < num_ssa_names; x++)
1369 tree name = ssa_name (x);
1370 if (gimple_range_ssa_p (name) && SSA_NAME_DEF_STMT (name) &&
1371 gimple_bb (SSA_NAME_DEF_STMT (name)) == bb &&
1372 m_cache.get_global_range (range, name))
1374 if (!range.varying_p ())
1376 print_generic_expr (f, name, TDF_SLIM);
1377 fprintf (f, " : ");
1378 range.dump (f);
1379 fprintf (f, "\n");
1385 // And now outgoing edges, if they define anything.
1386 FOR_EACH_EDGE (e, ei, bb->succs)
1388 for (x = 1; x < num_ssa_names; x++)
1390 tree name = gimple_range_ssa_p (ssa_name (x));
1391 if (name && gori ().has_edge_range_p (name, e)
1392 && m_cache.range_on_edge (range, e, name))
1394 gimple *s = SSA_NAME_DEF_STMT (name);
1395 // Only print the range if this is the def block, or
1396 // the on entry cache for either end of the edge is
1397 // set.
1398 if ((s && bb == gimple_bb (s)) ||
1399 m_cache.block_range (range, bb, name, false) ||
1400 m_cache.block_range (range, e->dest, name, false))
1402 range_on_edge (range, e, name);
1403 if (!range.varying_p ())
1405 fprintf (f, "%d->%d ", e->src->index,
1406 e->dest->index);
1407 char c = ' ';
1408 if (e->flags & EDGE_TRUE_VALUE)
1409 fprintf (f, " (T)%c", c);
1410 else if (e->flags & EDGE_FALSE_VALUE)
1411 fprintf (f, " (F)%c", c);
1412 else
1413 fprintf (f, " ");
1414 print_generic_expr (f, name, TDF_SLIM);
1415 fprintf(f, " : \t");
1416 range.dump(f);
1417 fprintf (f, "\n");
1425 // Print the known table values to file F.
1427 void
1428 gimple_ranger::dump (FILE *f)
1430 basic_block bb;
1432 FOR_EACH_BB_FN (bb, cfun)
1433 dump_bb (f, bb);
1435 m_cache.dump (f);
1438 // If SCEV has any information about phi node NAME, return it as a range in R.
1440 void
1441 fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
1442 class loop *l, gphi *phi,
1443 fur_source &src)
1445 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1446 tree min, max, type = TREE_TYPE (name);
1447 if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
1449 if (TREE_CODE (min) != INTEGER_CST)
1451 if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
1452 min = wide_int_to_tree (type, r.lower_bound ());
1453 else
1454 min = vrp_val_min (type);
1456 if (TREE_CODE (max) != INTEGER_CST)
1458 if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
1459 max = wide_int_to_tree (type, r.upper_bound ());
1460 else
1461 max = vrp_val_max (type);
1463 r.set (min, max);
1465 else
1466 r.set_varying (type);
1469 // --------------------------------------------------------------------------
1470 // trace_ranger implementation.
1473 trace_ranger::trace_ranger ()
1475 indent = 0;
1476 trace_count = 0;
1479 // If dumping, return true and print the prefix for the next output line.
1481 bool
1482 trace_ranger::dumping (unsigned counter, bool trailing)
1484 if (dump_file && (dump_flags & TDF_DETAILS))
1486 // Print counter index as well as INDENT spaces.
1487 if (!trailing)
1488 fprintf (dump_file, " %-7u ", counter);
1489 else
1490 fprintf (dump_file, " ");
1491 unsigned x;
1492 for (x = 0; x< indent; x++)
1493 fputc (' ', dump_file);
1494 return true;
1496 return false;
1499 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
1500 // returning RESULT.
1502 bool
1503 trace_ranger::trailer (unsigned counter, const char *caller, bool result,
1504 tree name, const irange &r)
1506 if (dumping (counter, true))
1508 indent -= bump;
1509 fputs(result ? "TRUE : " : "FALSE : ", dump_file);
1510 fprintf (dump_file, "(%u) ", counter);
1511 fputs (caller, dump_file);
1512 fputs (" (",dump_file);
1513 if (name)
1514 print_generic_expr (dump_file, name, TDF_SLIM);
1515 fputs (") ",dump_file);
1516 if (result)
1518 r.dump (dump_file);
1519 fputc('\n', dump_file);
1521 else
1522 fputc('\n', dump_file);
1523 // Marks the end of a request.
1524 if (indent == 0)
1525 fputc('\n', dump_file);
1527 return result;
1530 // Tracing version of range_on_edge. Call it with printing wrappers.
1532 bool
1533 trace_ranger::range_on_edge (irange &r, edge e, tree name)
1535 unsigned idx = ++trace_count;
1536 if (dumping (idx))
1538 fprintf (dump_file, "range_on_edge (");
1539 print_generic_expr (dump_file, name, TDF_SLIM);
1540 fprintf (dump_file, ") on edge %d->%d\n", e->src->index, e->dest->index);
1541 indent += bump;
1544 bool res = gimple_ranger::range_on_edge (r, e, name);
1545 trailer (idx, "range_on_edge", true, name, r);
1546 return res;
1549 // Tracing version of range_on_entry. Call it with printing wrappers.
1551 void
1552 trace_ranger::range_on_entry (irange &r, basic_block bb, tree name)
1554 unsigned idx = ++trace_count;
1555 if (dumping (idx))
1557 fprintf (dump_file, "range_on_entry (");
1558 print_generic_expr (dump_file, name, TDF_SLIM);
1559 fprintf (dump_file, ") to BB %d\n", bb->index);
1560 indent += bump;
1563 gimple_ranger::range_on_entry (r, bb, name);
1565 trailer (idx, "range_on_entry", true, name, r);
1568 // Tracing version of range_on_exit. Call it with printing wrappers.
1570 void
1571 trace_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1573 unsigned idx = ++trace_count;
1574 if (dumping (idx))
1576 fprintf (dump_file, "range_on_exit (");
1577 print_generic_expr (dump_file, name, TDF_SLIM);
1578 fprintf (dump_file, ") from BB %d\n", bb->index);
1579 indent += bump;
1582 gimple_ranger::range_on_exit (r, bb, name);
1584 trailer (idx, "range_on_exit", true, name, r);
1587 // Tracing version of range_of_stmt. Call it with printing wrappers.
1589 bool
1590 trace_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1592 bool res;
1593 unsigned idx = ++trace_count;
1594 if (dumping (idx))
1596 fprintf (dump_file, "range_of_stmt (");
1597 if (name)
1598 print_generic_expr (dump_file, name, TDF_SLIM);
1599 fputs (") at stmt ", dump_file);
1600 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1601 indent += bump;
1604 res = gimple_ranger::range_of_stmt (r, s, name);
1606 return trailer (idx, "range_of_stmt", res, name, r);
1609 // Tracing version of range_of_expr. Call it with printing wrappers.
1611 bool
1612 trace_ranger::range_of_expr (irange &r, tree name, gimple *s)
1614 bool res;
1615 unsigned idx = ++trace_count;
1616 if (dumping (idx))
1618 fprintf (dump_file, "range_of_expr(");
1619 print_generic_expr (dump_file, name, TDF_SLIM);
1620 fputs (")", dump_file);
1621 if (s)
1623 fputs (" at stmt ", dump_file);
1624 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1626 else
1627 fputs ("\n", dump_file);
1628 indent += bump;
1631 res = gimple_ranger::range_of_expr (r, name, s);
1633 return trailer (idx, "range_of_expr", res, name, r);
1636 gimple_ranger *
1637 enable_ranger (struct function *fun)
1639 gimple_ranger *r;
1641 if (param_evrp_mode & EVRP_MODE_TRACE)
1642 r = new trace_ranger;
1643 else
1644 r = new gimple_ranger;
1646 fun->x_range_query = r;
1648 return r;
1651 void
1652 disable_ranger (struct function *fun)
1654 delete fun->x_range_query;
1656 fun->x_range_query = &global_ranges;
1659 // =========================================
1660 // Debugging helpers.
1661 // =========================================
1663 // Query all statements in the IL to precalculate computable ranges in RANGER.
1665 static DEBUG_FUNCTION void
1666 debug_seed_ranger (gimple_ranger &ranger)
1668 // Recalculate SCEV to make sure the dump lists everything.
1669 if (scev_initialized_p ())
1671 scev_finalize ();
1672 scev_initialize ();
1675 basic_block bb;
1676 int_range_max r;
1677 gimple_stmt_iterator gsi;
1678 FOR_EACH_BB_FN (bb, cfun)
1679 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1681 gimple *stmt = gsi_stmt (gsi);
1683 if (is_gimple_debug (stmt))
1684 continue;
1686 ranger.range_of_stmt (r, stmt);
1690 // Dump all that ranger knows for the current function.
1692 DEBUG_FUNCTION void
1693 dump_ranger (FILE *out)
1695 gimple_ranger ranger;
1696 debug_seed_ranger (ranger);
1697 ranger.dump (out);
1700 DEBUG_FUNCTION void
1701 debug_ranger ()
1703 dump_ranger (stderr);
1706 // Dump all that ranger knows on a path of BBs.
1708 // Note that the blocks are in reverse order, thus the exit block is
1709 // path[0].
1711 DEBUG_FUNCTION void
1712 dump_ranger (FILE *dump_file, const vec<basic_block> &path)
1714 if (path.length () == 0)
1716 fprintf (dump_file, "empty\n");
1717 return;
1720 gimple_ranger ranger;
1721 debug_seed_ranger (ranger);
1723 unsigned i = path.length ();
1726 i--;
1727 ranger.dump_bb (dump_file, path[i]);
1729 while (i > 0);
1732 DEBUG_FUNCTION void
1733 debug_ranger (const vec<basic_block> &path)
1735 dump_ranger (stderr, path);
1738 #include "gimple-range-tests.cc"