]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range.cc
Move global range code to value-query.cc.
[thirdparty/gcc.git] / gcc / gimple-range.cc
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>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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"
49
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 calcluated, return FALSE.
53
54 bool
55 fur_source::get_operand (irange &r, tree expr)
56 {
57 if (!gimple_range_ssa_p (expr))
58 return get_tree_range (r, expr);
59
60 // If no query engine is present, simply get the global value.
61 if (!m_query)
62 {
63 r = gimple_range_global (expr);
64 return true;
65 }
66
67 // First look for a stmt.
68 if (m_stmt)
69 return m_query->range_of_expr (r, expr, m_stmt);
70
71 // Finally must be on an edge.
72 return m_query->range_on_edge (r, m_edge, expr);
73 }
74
75
76 // Adjust the range for a pointer difference where the operands came
77 // from a memchr.
78 //
79 // This notices the following sequence:
80 //
81 // def = __builtin_memchr (arg, 0, sz)
82 // n = def - arg
83 //
84 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
85
86 static void
87 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
88 {
89 tree op0 = gimple_assign_rhs1 (diff_stmt);
90 tree op1 = gimple_assign_rhs2 (diff_stmt);
91 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
92 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
93 gimple *call;
94
95 if (TREE_CODE (op0) == SSA_NAME
96 && TREE_CODE (op1) == SSA_NAME
97 && (call = SSA_NAME_DEF_STMT (op0))
98 && is_gimple_call (call)
99 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
100 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
101 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
102 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
103 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
104 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
105 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
106 && integer_zerop (gimple_call_arg (call, 1)))
107 {
108 tree max = vrp_val_max (ptrdiff_type_node);
109 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
110 tree expr_type = gimple_expr_type (diff_stmt);
111 tree range_min = build_zero_cst (expr_type);
112 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
113 int_range<2> r (range_min, range_max);
114 res.intersect (r);
115 }
116 }
117
118 // This function looks for situations when walking the use/def chains
119 // may provide additonal contextual range information not exposed on
120 // this statement. Like knowing the IMAGPART return value from a
121 // builtin function is a boolean result.
122
123 // We should rework how we're called, as we have an op_unknown entry
124 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
125 // function gets called.
126
127 static void
128 gimple_range_adjustment (irange &res, const gimple *stmt)
129 {
130 switch (gimple_expr_code (stmt))
131 {
132 case POINTER_DIFF_EXPR:
133 adjust_pointer_diff_expr (res, stmt);
134 return;
135
136 case IMAGPART_EXPR:
137 {
138 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
139 if (TREE_CODE (name) == SSA_NAME)
140 {
141 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
142 if (def_stmt && is_gimple_call (def_stmt)
143 && gimple_call_internal_p (def_stmt))
144 {
145 switch (gimple_call_internal_fn (def_stmt))
146 {
147 case IFN_ADD_OVERFLOW:
148 case IFN_SUB_OVERFLOW:
149 case IFN_MUL_OVERFLOW:
150 case IFN_ATOMIC_COMPARE_EXCHANGE:
151 {
152 int_range<2> r;
153 r.set_varying (boolean_type_node);
154 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
155 range_cast (r, type);
156 res.intersect (r);
157 }
158 default:
159 break;
160 }
161 }
162 }
163 break;
164 }
165
166 default:
167 break;
168 }
169 }
170
171 // Return a range in R for the tree EXPR. Return true if a range is
172 // representable, and UNDEFINED/false if not.
173
174 bool
175 get_tree_range (irange &r, tree expr)
176 {
177 tree type;
178 if (TYPE_P (expr))
179 type = expr;
180 else
181 type = TREE_TYPE (expr);
182
183 // Return false if the type isn't suported.
184 if (!irange::supports_type_p (type))
185 {
186 r.set_undefined ();
187 return false;
188 }
189
190 switch (TREE_CODE (expr))
191 {
192 case INTEGER_CST:
193 if (TREE_OVERFLOW_P (expr))
194 expr = drop_tree_overflow (expr);
195 r.set (expr, expr);
196 return true;
197
198 case SSA_NAME:
199 r = gimple_range_global (expr);
200 return true;
201
202 case ADDR_EXPR:
203 {
204 // Handle &var which can show up in phi arguments.
205 bool ov;
206 if (tree_single_nonzero_warnv_p (expr, &ov))
207 {
208 r = range_nonzero (type);
209 return true;
210 }
211 break;
212 }
213
214 default:
215 break;
216 }
217 r.set_varying (type);
218 return true;
219 }
220
221 // Return the base of the RHS of an assignment.
222
223 static tree
224 gimple_range_base_of_assignment (const gimple *stmt)
225 {
226 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
227 tree op1 = gimple_assign_rhs1 (stmt);
228 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
229 return get_base_address (TREE_OPERAND (op1, 0));
230 return op1;
231 }
232
233 // Return the first operand of this statement if it is a valid operand
234 // supported by ranges, otherwise return NULL_TREE. Special case is
235 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
236
237 tree
238 gimple_range_operand1 (const gimple *stmt)
239 {
240 gcc_checking_assert (gimple_range_handler (stmt));
241
242 switch (gimple_code (stmt))
243 {
244 case GIMPLE_COND:
245 return gimple_cond_lhs (stmt);
246 case GIMPLE_ASSIGN:
247 {
248 tree base = gimple_range_base_of_assignment (stmt);
249 if (base && TREE_CODE (base) == MEM_REF)
250 {
251 // If the base address is an SSA_NAME, we return it
252 // here. This allows processing of the range of that
253 // name, while the rest of the expression is simply
254 // ignored. The code in range_ops will see the
255 // ADDR_EXPR and do the right thing.
256 tree ssa = TREE_OPERAND (base, 0);
257 if (TREE_CODE (ssa) == SSA_NAME)
258 return ssa;
259 }
260 return base;
261 }
262 default:
263 break;
264 }
265 return NULL;
266 }
267
268 // Return the second operand of statement STMT, otherwise return NULL_TREE.
269
270 tree
271 gimple_range_operand2 (const gimple *stmt)
272 {
273 gcc_checking_assert (gimple_range_handler (stmt));
274
275 switch (gimple_code (stmt))
276 {
277 case GIMPLE_COND:
278 return gimple_cond_rhs (stmt);
279 case GIMPLE_ASSIGN:
280 if (gimple_num_ops (stmt) >= 3)
281 return gimple_assign_rhs2 (stmt);
282 default:
283 break;
284 }
285 return NULL_TREE;
286 }
287
288 // Calculate what we can determine of the range of this unary
289 // statement's operand if the lhs of the expression has the range
290 // LHS_RANGE. Return false if nothing can be determined.
291
292 bool
293 gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range)
294 {
295 gcc_checking_assert (gimple_num_ops (stmt) < 3);
296
297 // An empty range is viral.
298 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
299 if (lhs_range.undefined_p ())
300 {
301 r.set_undefined ();
302 return true;
303 }
304 // Unary operations require the type of the first operand in the
305 // second range position.
306 int_range<2> type_range (type);
307 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
308 type_range);
309 }
310
311 // Calculate what we can determine of the range of this statement's
312 // first operand if the lhs of the expression has the range LHS_RANGE
313 // and the second operand has the range OP2_RANGE. Return false if
314 // nothing can be determined.
315
316 bool
317 gimple_range_calc_op1 (irange &r, const gimple *stmt,
318 const irange &lhs_range, const irange &op2_range)
319 {
320 // Unary operation are allowed to pass a range in for second operand
321 // as there are often additional restrictions beyond the type which
322 // can be imposed. See operator_cast::op1_range().
323 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
324 // An empty range is viral.
325 if (op2_range.undefined_p () || lhs_range.undefined_p ())
326 {
327 r.set_undefined ();
328 return true;
329 }
330 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
331 op2_range);
332 }
333
334 // Calculate what we can determine of the range of this statement's
335 // second operand if the lhs of the expression has the range LHS_RANGE
336 // and the first operand has the range OP1_RANGE. Return false if
337 // nothing can be determined.
338
339 bool
340 gimple_range_calc_op2 (irange &r, const gimple *stmt,
341 const irange &lhs_range, const irange &op1_range)
342 {
343 tree type = TREE_TYPE (gimple_range_operand2 (stmt));
344 // An empty range is viral.
345 if (op1_range.undefined_p () || lhs_range.undefined_p ())
346 {
347 r.set_undefined ();
348 return true;
349 }
350 return gimple_range_handler (stmt)->op2_range (r, type, lhs_range,
351 op1_range);
352 }
353
354 // Calculate a range for statement S and return it in R. If NAME is provided it
355 // represents the SSA_NAME on the LHS of the statement. It is only required
356 // if there is more than one lhs/output. If a range cannot
357 // be calculated, return false.
358
359 bool
360 fold_using_range::fold_stmt (irange &r, gimple *s, fur_source &src, tree name)
361 {
362 bool res = false;
363 // If name and S are specified, make sure it is an LHS of S.
364 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
365 name == gimple_get_lhs (s));
366
367 if (!name)
368 name = gimple_get_lhs (s);
369
370 // Process addresses.
371 if (gimple_code (s) == GIMPLE_ASSIGN
372 && gimple_assign_rhs_code (s) == ADDR_EXPR)
373 return range_of_address (r, s, src);
374
375 if (gimple_range_handler (s))
376 res = range_of_range_op (r, s, src);
377 else if (is_a<gphi *>(s))
378 res = range_of_phi (r, as_a<gphi *> (s), src);
379 else if (is_a<gcall *>(s))
380 res = range_of_call (r, as_a<gcall *> (s), src);
381 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
382 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
383
384 if (!res)
385 {
386 // If no name is specified, try the expression kind.
387 if (!name)
388 {
389 tree t = gimple_expr_type (s);
390 if (!irange::supports_type_p (t))
391 return false;
392 r.set_varying (t);
393 return true;
394 }
395 if (!gimple_range_ssa_p (name))
396 return false;
397 // We don't understand the stmt, so return the global range.
398 r = gimple_range_global (name);
399 return true;
400 }
401
402 if (r.undefined_p ())
403 return true;
404
405 // We sometimes get compatible types copied from operands, make sure
406 // the correct type is being returned.
407 if (name && TREE_TYPE (name) != r.type ())
408 {
409 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
410 range_cast (r, TREE_TYPE (name));
411 }
412 return true;
413 }
414
415 // Calculate a range for range_op statement S and return it in R. If any
416 // If a range cannot be calculated, return false.
417
418 bool
419 fold_using_range::range_of_range_op (irange &r, gimple *s, fur_source &src)
420 {
421 int_range_max range1, range2;
422 tree type = gimple_expr_type (s);
423 range_operator *handler = gimple_range_handler (s);
424 gcc_checking_assert (handler);
425 gcc_checking_assert (irange::supports_type_p (type));
426
427 tree lhs = gimple_get_lhs (s);
428 tree op1 = gimple_range_operand1 (s);
429 tree op2 = gimple_range_operand2 (s);
430
431 if (src.get_operand (range1, op1))
432 {
433 if (!op2)
434 {
435 // Fold range, and register any dependency if available.
436 int_range<2> r2 (type);
437 handler->fold_range (r, type, range1, r2);
438 if (lhs && src.m_gori)
439 src.m_gori->register_dependency (lhs, op1);
440 }
441 else if (src.get_operand (range2, op2))
442 {
443 // Fold range, and register any dependency if available.
444 handler->fold_range (r, type, range1, range2);
445 if (lhs && src.m_gori)
446 {
447 src.m_gori->register_dependency (lhs, op1);
448 src.m_gori->register_dependency (lhs, op2);
449 }
450 }
451 else
452 r.set_varying (type);
453 }
454 else
455 r.set_varying (type);
456 // Make certain range-op adjustments that aren't handled any other way.
457 gimple_range_adjustment (r, s);
458 return true;
459 }
460
461 // Calculate the range of an assignment containing an ADDR_EXPR.
462 // Return the range in R.
463 // If a range cannot be calculated, set it to VARYING and return true.
464
465 bool
466 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
467 {
468 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
469 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
470
471 bool strict_overflow_p;
472 tree expr = gimple_assign_rhs1 (stmt);
473 poly_int64 bitsize, bitpos;
474 tree offset;
475 machine_mode mode;
476 int unsignedp, reversep, volatilep;
477 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
478 &bitpos, &offset, &mode, &unsignedp,
479 &reversep, &volatilep);
480
481
482 if (base != NULL_TREE
483 && TREE_CODE (base) == MEM_REF
484 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
485 {
486 tree ssa = TREE_OPERAND (base, 0);
487 tree lhs = gimple_get_lhs (stmt);
488 if (src.m_gori && lhs && gimple_range_ssa_p (ssa))
489 src.m_gori->register_dependency (lhs, ssa);
490 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
491 src.get_operand (r, ssa);
492 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
493
494 poly_offset_int off = 0;
495 bool off_cst = false;
496 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
497 {
498 off = mem_ref_offset (base);
499 if (offset)
500 off += poly_offset_int::from (wi::to_poly_wide (offset),
501 SIGNED);
502 off <<= LOG2_BITS_PER_UNIT;
503 off += bitpos;
504 off_cst = true;
505 }
506 /* If &X->a is equal to X, the range of X is the result. */
507 if (off_cst && known_eq (off, 0))
508 return true;
509 else if (flag_delete_null_pointer_checks
510 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
511 {
512 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
513 allow going from non-NULL pointer to NULL. */
514 if(!range_includes_zero_p (&r))
515 return true;
516 }
517 /* If MEM_REF has a "positive" offset, consider it non-NULL
518 always, for -fdelete-null-pointer-checks also "negative"
519 ones. Punt for unknown offsets (e.g. variable ones). */
520 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
521 && off_cst
522 && known_ne (off, 0)
523 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
524 {
525 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
526 return true;
527 }
528 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
529 return true;
530 }
531
532 // Handle "= &a".
533 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
534 {
535 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
536 return true;
537 }
538
539 // Otherwise return varying.
540 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
541 return true;
542 }
543
544 // Calculate a range for phi statement S and return it in R.
545 // If a range cannot be calculated, return false.
546
547 bool
548 fold_using_range::range_of_phi (irange &r, gphi *phi, fur_source &src)
549 {
550 tree phi_def = gimple_phi_result (phi);
551 tree type = TREE_TYPE (phi_def);
552 int_range_max arg_range;
553 unsigned x;
554
555 if (!irange::supports_type_p (type))
556 return false;
557
558 // Start with an empty range, unioning in each argument's range.
559 r.set_undefined ();
560 for (x = 0; x < gimple_phi_num_args (phi); x++)
561 {
562 tree arg = gimple_phi_arg_def (phi, x);
563 edge e = gimple_phi_arg_edge (phi, x);
564
565 // Register potential dependencies for stale value tracking.
566 if (src.m_gori && gimple_range_ssa_p (arg))
567 src.m_gori->register_dependency (phi_def, arg);
568
569 // Get the range of the argument on its edge.
570 fur_source e_src (src.m_query, e);
571 e_src.get_operand (arg_range, arg);
572 // If we're recomputing the argument elsewhere, try to refine it.
573 if (src.m_stmt != phi)
574 {
575 int_range_max tmp;
576 e_src.get_operand (tmp, arg);
577 arg_range.intersect (tmp);
578 }
579 r.union_ (arg_range);
580 // Once the value reaches varying, stop looking.
581 if (r.varying_p ())
582 break;
583 }
584
585 // If SCEV is available, query if this PHI has any knonwn values.
586 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
587 {
588 value_range loop_range;
589 class loop *l = loop_containing_stmt (phi);
590 if (l && loop_outer (l))
591 {
592 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
593 if (!loop_range.varying_p ())
594 {
595 if (dump_file && (dump_flags & TDF_DETAILS))
596 {
597 fprintf (dump_file, " Loops range found for ");
598 print_generic_expr (dump_file, phi_def, TDF_SLIM);
599 fprintf (dump_file, ": ");
600 loop_range.dump (dump_file);
601 fprintf (dump_file, " and calculated range :");
602 r.dump (dump_file);
603 fprintf (dump_file, "\n");
604 }
605 r.intersect (loop_range);
606 }
607 }
608 }
609
610 return true;
611 }
612
613 // Calculate a range for call statement S and return it in R.
614 // If a range cannot be calculated, return false.
615
616 bool
617 fold_using_range::range_of_call (irange &r, gcall *call, fur_source &src)
618 {
619 tree type = gimple_call_return_type (call);
620 tree lhs = gimple_call_lhs (call);
621 bool strict_overflow_p;
622
623 if (!irange::supports_type_p (type))
624 return false;
625
626 if (range_of_builtin_call (r, call, src))
627 ;
628 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
629 r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
630 else if (gimple_call_nonnull_result_p (call)
631 || gimple_call_nonnull_arg (call))
632 r = range_nonzero (type);
633 else
634 r.set_varying (type);
635
636 // If there is an LHS, intersect that with what is known.
637 if (lhs)
638 {
639 value_range def;
640 def = gimple_range_global (lhs);
641 r.intersect (def);
642 }
643 return true;
644 }
645
646 // Return the range of a __builtin_ubsan* in CALL and set it in R.
647 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
648 // MULT_EXPR).
649
650 void
651 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
652 tree_code code, fur_source &src)
653 {
654 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
655 || code == MULT_EXPR);
656 tree type = gimple_call_return_type (call);
657 range_operator *op = range_op_handler (code, type);
658 gcc_checking_assert (op);
659 int_range_max ir0, ir1;
660 tree arg0 = gimple_call_arg (call, 0);
661 tree arg1 = gimple_call_arg (call, 1);
662 src.get_operand (ir0, arg0);
663 src.get_operand (ir1, arg1);
664
665 bool saved_flag_wrapv = flag_wrapv;
666 // Pretend the arithmetic is wrapping. If there is any overflow,
667 // we'll complain, but will actually do wrapping operation.
668 flag_wrapv = 1;
669 op->fold_range (r, type, ir0, ir1);
670 flag_wrapv = saved_flag_wrapv;
671
672 // If for both arguments vrp_valueize returned non-NULL, this should
673 // have been already folded and if not, it wasn't folded because of
674 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
675 if (r.singleton_p ())
676 r.set_varying (type);
677 }
678
679 // For a builtin in CALL, return a range in R if known and return
680 // TRUE. Otherwise return FALSE.
681
682 bool
683 fold_using_range::range_of_builtin_call (irange &r, gcall *call,
684 fur_source &src)
685 {
686 combined_fn func = gimple_call_combined_fn (call);
687 if (func == CFN_LAST)
688 return false;
689
690 tree type = gimple_call_return_type (call);
691 tree arg;
692 int mini, maxi, zerov = 0, prec;
693 scalar_int_mode mode;
694
695 switch (func)
696 {
697 case CFN_BUILT_IN_CONSTANT_P:
698 if (cfun->after_inlining)
699 {
700 r.set_zero (type);
701 // r.equiv_clear ();
702 return true;
703 }
704 arg = gimple_call_arg (call, 0);
705 if (src.get_operand (r, arg) && r.singleton_p ())
706 {
707 r.set (build_one_cst (type), build_one_cst (type));
708 return true;
709 }
710 break;
711
712 CASE_CFN_FFS:
713 CASE_CFN_POPCOUNT:
714 // __builtin_ffs* and __builtin_popcount* return [0, prec].
715 arg = gimple_call_arg (call, 0);
716 prec = TYPE_PRECISION (TREE_TYPE (arg));
717 mini = 0;
718 maxi = prec;
719 src.get_operand (r, arg);
720 // If arg is non-zero, then ffs or popcount are non-zero.
721 if (!range_includes_zero_p (&r))
722 mini = 1;
723 // If some high bits are known to be zero, decrease the maximum.
724 if (!r.undefined_p ())
725 {
726 if (TYPE_SIGN (r.type ()) == SIGNED)
727 range_cast (r, unsigned_type_for (r.type ()));
728 wide_int max = r.upper_bound ();
729 maxi = wi::floor_log2 (max) + 1;
730 }
731 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
732 return true;
733
734 CASE_CFN_PARITY:
735 r.set (build_zero_cst (type), build_one_cst (type));
736 return true;
737
738 CASE_CFN_CLZ:
739 // __builtin_c[lt]z* return [0, prec-1], except when the
740 // argument is 0, but that is undefined behavior.
741 //
742 // For __builtin_c[lt]z* consider argument of 0 always undefined
743 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
744 arg = gimple_call_arg (call, 0);
745 prec = TYPE_PRECISION (TREE_TYPE (arg));
746 mini = 0;
747 maxi = prec - 1;
748 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
749 if (gimple_call_internal_p (call))
750 {
751 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
752 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
753 {
754 // Only handle the single common value.
755 if (zerov == prec)
756 maxi = prec;
757 else
758 // Magic value to give up, unless we can prove arg is non-zero.
759 mini = -2;
760 }
761 }
762
763 src.get_operand (r, arg);
764 // From clz of minimum we can compute result maximum.
765 if (r.constant_p () && !r.varying_p ())
766 {
767 int newmaxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
768 // Argument is unsigned, so do nothing if it is [0, ...] range.
769 if (newmaxi != prec)
770 {
771 mini = 0;
772 maxi = newmaxi;
773 }
774 }
775 else if (!range_includes_zero_p (&r))
776 {
777 maxi = prec - 1;
778 mini = 0;
779 }
780 if (mini == -2)
781 break;
782 // From clz of maximum we can compute result minimum.
783 if (r.constant_p ())
784 {
785 int newmini = prec - 1 - wi::floor_log2 (r.upper_bound ());
786 if (newmini == prec)
787 {
788 // Argument range is [0, 0]. If CLZ_DEFINED_VALUE_AT_ZERO
789 // is 2 with VALUE of prec, return [prec, prec], otherwise
790 // ignore the range.
791 if (maxi == prec)
792 mini = prec;
793 }
794 else
795 mini = newmini;
796 }
797 if (mini == -2)
798 break;
799 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
800 return true;
801
802 CASE_CFN_CTZ:
803 // __builtin_ctz* return [0, prec-1], except for when the
804 // argument is 0, but that is undefined behavior.
805 //
806 // For __builtin_ctz* consider argument of 0 always undefined
807 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
808 arg = gimple_call_arg (call, 0);
809 prec = TYPE_PRECISION (TREE_TYPE (arg));
810 mini = 0;
811 maxi = prec - 1;
812 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
813 if (gimple_call_internal_p (call))
814 {
815 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
816 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
817 {
818 // Handle only the two common values.
819 if (zerov == -1)
820 mini = -1;
821 else if (zerov == prec)
822 maxi = prec;
823 else
824 // Magic value to give up, unless we can prove arg is non-zero.
825 mini = -2;
826 }
827 }
828 src.get_operand (r, arg);
829 if (!r.undefined_p ())
830 {
831 if (r.lower_bound () != 0)
832 {
833 mini = 0;
834 maxi = prec - 1;
835 }
836 // If some high bits are known to be zero, we can decrease
837 // the maximum.
838 wide_int max = r.upper_bound ();
839 if (max == 0)
840 {
841 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
842 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
843 // Otherwise ignore the range.
844 if (mini == -1)
845 maxi = -1;
846 else if (maxi == prec)
847 mini = prec;
848 }
849 // If value at zero is prec and 0 is in the range, we can't lower
850 // the upper bound. We could create two separate ranges though,
851 // [0,floor_log2(max)][prec,prec] though.
852 else if (maxi != prec)
853 maxi = wi::floor_log2 (max);
854 }
855 if (mini == -2)
856 break;
857 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
858 return true;
859
860 CASE_CFN_CLRSB:
861 arg = gimple_call_arg (call, 0);
862 prec = TYPE_PRECISION (TREE_TYPE (arg));
863 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
864 return true;
865 case CFN_UBSAN_CHECK_ADD:
866 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
867 return true;
868 case CFN_UBSAN_CHECK_SUB:
869 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
870 return true;
871 case CFN_UBSAN_CHECK_MUL:
872 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
873 return true;
874
875 case CFN_GOACC_DIM_SIZE:
876 case CFN_GOACC_DIM_POS:
877 // Optimizing these two internal functions helps the loop
878 // optimizer eliminate outer comparisons. Size is [1,N]
879 // and pos is [0,N-1].
880 {
881 bool is_pos = func == CFN_GOACC_DIM_POS;
882 int axis = oacc_get_ifn_dim_arg (call);
883 int size = oacc_get_fn_dim_size (current_function_decl, axis);
884 if (!size)
885 // If it's dynamic, the backend might know a hardware limitation.
886 size = targetm.goacc.dim_limit (axis);
887
888 r.set (build_int_cst (type, is_pos ? 0 : 1),
889 size
890 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
891 return true;
892 }
893
894 case CFN_BUILT_IN_STRLEN:
895 if (tree lhs = gimple_call_lhs (call))
896 if (ptrdiff_type_node
897 && (TYPE_PRECISION (ptrdiff_type_node)
898 == TYPE_PRECISION (TREE_TYPE (lhs))))
899 {
900 tree type = TREE_TYPE (lhs);
901 tree max = vrp_val_max (ptrdiff_type_node);
902 wide_int wmax
903 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
904 tree range_min = build_zero_cst (type);
905 // To account for the terminating NULL, the maximum length
906 // is one less than the maximum array size, which in turn
907 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
908 // smaller than the former type).
909 // FIXME: Use max_object_size() - 1 here.
910 tree range_max = wide_int_to_tree (type, wmax - 2);
911 r.set (range_min, range_max);
912 return true;
913 }
914 break;
915 default:
916 break;
917 }
918 return false;
919 }
920
921
922 // Calculate a range for COND_EXPR statement S and return it in R.
923 // If a range cannot be calculated, return false.
924
925 bool
926 fold_using_range::range_of_cond_expr (irange &r, gassign *s, fur_source &src)
927 {
928 int_range_max cond_range, range1, range2;
929 tree cond = gimple_assign_rhs1 (s);
930 tree op1 = gimple_assign_rhs2 (s);
931 tree op2 = gimple_assign_rhs3 (s);
932
933 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
934 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1),
935 TREE_TYPE (op2)));
936 if (!irange::supports_type_p (TREE_TYPE (op1)))
937 return false;
938
939 src.get_operand (cond_range, cond);
940 src.get_operand (range1, op1);
941 src.get_operand (range2, op2);
942
943 // If the condition is known, choose the appropriate expression.
944 if (cond_range.singleton_p ())
945 {
946 // False, pick second operand.
947 if (cond_range.zero_p ())
948 r = range2;
949 else
950 r = range1;
951 }
952 else
953 {
954 r = range1;
955 r.union_ (range2);
956 }
957 return true;
958 }
959
960 bool
961 gimple_ranger::range_of_expr (irange &r, tree expr, gimple *stmt)
962 {
963 if (!gimple_range_ssa_p (expr))
964 return get_tree_range (r, expr);
965
966 // If there is no statement, just get the global value.
967 if (!stmt)
968 {
969 if (!m_cache.get_global_range (r, expr))
970 r = gimple_range_global (expr);
971 return true;
972 }
973
974 basic_block bb = gimple_bb (stmt);
975 gimple *def_stmt = SSA_NAME_DEF_STMT (expr);
976
977 // If name is defined in this block, try to get an range from S.
978 if (def_stmt && gimple_bb (def_stmt) == bb)
979 {
980 range_of_stmt (r, def_stmt, expr);
981 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
982 m_cache.m_non_null.non_null_deref_p (expr, bb))
983 r = range_nonzero (TREE_TYPE (expr));
984 }
985 else
986 // Otherwise OP comes from outside this block, use range on entry.
987 range_on_entry (r, bb, expr);
988
989 return true;
990 }
991
992 // Return the range of NAME on entry to block BB in R.
993
994 void
995 gimple_ranger::range_on_entry (irange &r, basic_block bb, tree name)
996 {
997 int_range_max entry_range;
998 gcc_checking_assert (gimple_range_ssa_p (name));
999
1000 // Start with any known range
1001 range_of_stmt (r, SSA_NAME_DEF_STMT (name), name);
1002
1003 // Now see if there is any on_entry value which may refine it.
1004 if (m_cache.block_range (entry_range, bb, name))
1005 r.intersect (entry_range);
1006
1007 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
1008 m_cache.m_non_null.non_null_deref_p (name, bb))
1009 r = range_nonzero (TREE_TYPE (name));
1010 }
1011
1012 // Calculate the range for NAME at the end of block BB and return it in R.
1013 // Return false if no range can be calculated.
1014
1015 void
1016 gimple_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1017 {
1018 // on-exit from the exit block?
1019 gcc_checking_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1020 gcc_checking_assert (gimple_range_ssa_p (name));
1021
1022 gimple *s = SSA_NAME_DEF_STMT (name);
1023 basic_block def_bb = gimple_bb (s);
1024 // If this is not the definition block, get the range on the last stmt in
1025 // the block... if there is one.
1026 if (def_bb != bb)
1027 s = last_stmt (bb);
1028 // If there is no statement provided, get the range_on_entry for this block.
1029 if (s)
1030 range_of_expr (r, name, s);
1031 else
1032 range_on_entry (r, bb, name);
1033 gcc_checking_assert (r.undefined_p ()
1034 || range_compatible_p (r.type (), TREE_TYPE (name)));
1035 }
1036
1037 // Calculate a range for NAME on edge E and return it in R.
1038
1039 bool
1040 gimple_ranger::range_on_edge (irange &r, edge e, tree name)
1041 {
1042 int_range_max edge_range;
1043 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name)));
1044
1045 // PHI arguments can be constants, catch these here.
1046 if (!gimple_range_ssa_p (name))
1047 return range_of_expr (r, name);
1048
1049 range_on_exit (r, e->src, name);
1050 gcc_checking_assert (r.undefined_p ()
1051 || range_compatible_p (r.type(), TREE_TYPE (name)));
1052
1053 // Check to see if NAME is defined on edge e.
1054 if (m_cache.outgoing_edge_range_p (edge_range, e, name))
1055 r.intersect (edge_range);
1056
1057 return true;
1058 }
1059
1060 // fold_range wrapper for range_of_stmt to use as an internal client.
1061
1062 bool
1063 gimple_ranger::fold_range_internal (irange &r, gimple *s, tree name)
1064 {
1065 fold_using_range f;
1066 fur_source src (this, &m_cache, NULL, s);
1067 return f.fold_stmt (r, s, src, name);
1068 }
1069
1070 // Calculate a range for statement S and return it in R. If NAME is
1071 // provided it represents the SSA_NAME on the LHS of the statement.
1072 // It is only required if there is more than one lhs/output. Check
1073 // the global cache for NAME first to see if the evaluation can be
1074 // avoided. If a range cannot be calculated, return false and UNDEFINED.
1075
1076 bool
1077 gimple_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1078 {
1079 r.set_undefined ();
1080
1081 if (!name)
1082 name = gimple_get_lhs (s);
1083
1084 // If no name, simply call the base routine.
1085 if (!name)
1086 return fold_range_internal (r, s, NULL_TREE);
1087
1088 if (!gimple_range_ssa_p (name))
1089 return false;
1090
1091 // Check if the stmt has already been processed, and is not stale.
1092 if (m_cache.get_non_stale_global_range (r, name))
1093 return true;
1094
1095 // Otherwise calculate a new value.
1096 int_range_max tmp;
1097 fold_range_internal (tmp, s, name);
1098
1099 // Combine the new value with the old value. This is required because
1100 // the way value propagation works, when the IL changes on the fly we
1101 // can sometimes get different results. See PR 97741.
1102 r.intersect (tmp);
1103 m_cache.set_global_range (name, r);
1104
1105 return true;
1106 }
1107
1108 // This routine will export whatever global ranges are known to GCC
1109 // SSA_RANGE_NAME_INFO fields.
1110
1111 void
1112 gimple_ranger::export_global_ranges ()
1113 {
1114 unsigned x;
1115 int_range_max r;
1116 if (dump_file)
1117 {
1118 fprintf (dump_file, "Exported global range table\n");
1119 fprintf (dump_file, "===========================\n");
1120 }
1121
1122 for ( x = 1; x < num_ssa_names; x++)
1123 {
1124 tree name = ssa_name (x);
1125 if (name && !SSA_NAME_IN_FREE_LIST (name)
1126 && gimple_range_ssa_p (name)
1127 && m_cache.get_global_range (r, name)
1128 && !r.varying_p())
1129 {
1130 // Make sure the new range is a subset of the old range.
1131 int_range_max old_range;
1132 old_range = gimple_range_global (name);
1133 old_range.intersect (r);
1134 /* Disable this while we fix tree-ssa/pr61743-2.c. */
1135 //gcc_checking_assert (old_range == r);
1136
1137 // WTF? Can't write non-null pointer ranges?? stupid set_range_info!
1138 if (!POINTER_TYPE_P (TREE_TYPE (name)) && !r.undefined_p ())
1139 {
1140 value_range vr = r;
1141 set_range_info (name, vr);
1142 if (dump_file)
1143 {
1144 print_generic_expr (dump_file, name , TDF_SLIM);
1145 fprintf (dump_file, " --> ");
1146 vr.dump (dump_file);
1147 fprintf (dump_file, "\n");
1148 fprintf (dump_file, " irange : ");
1149 r.dump (dump_file);
1150 fprintf (dump_file, "\n");
1151 }
1152 }
1153 }
1154 }
1155 }
1156
1157 // Print the known table values to file F.
1158
1159 void
1160 gimple_ranger::dump_bb (FILE *f, basic_block bb)
1161 {
1162 unsigned x;
1163 edge_iterator ei;
1164 edge e;
1165 int_range_max range;
1166 fprintf (f, "\n=========== BB %d ============\n", bb->index);
1167 m_cache.dump (f, bb);
1168
1169 ::dump_bb (f, bb, 4, TDF_NONE);
1170
1171 // Now find any globals defined in this block.
1172 for (x = 1; x < num_ssa_names; x++)
1173 {
1174 tree name = ssa_name (x);
1175 if (gimple_range_ssa_p (name) && SSA_NAME_DEF_STMT (name) &&
1176 gimple_bb (SSA_NAME_DEF_STMT (name)) == bb &&
1177 m_cache.get_global_range (range, name))
1178 {
1179 if (!range.varying_p ())
1180 {
1181 print_generic_expr (f, name, TDF_SLIM);
1182 fprintf (f, " : ");
1183 range.dump (f);
1184 fprintf (f, "\n");
1185 }
1186
1187 }
1188 }
1189
1190 // And now outgoing edges, if they define anything.
1191 FOR_EACH_EDGE (e, ei, bb->succs)
1192 {
1193 for (x = 1; x < num_ssa_names; x++)
1194 {
1195 tree name = gimple_range_ssa_p (ssa_name (x));
1196 if (name && m_cache.outgoing_edge_range_p (range, e, name))
1197 {
1198 gimple *s = SSA_NAME_DEF_STMT (name);
1199 // Only print the range if this is the def block, or
1200 // the on entry cache for either end of the edge is
1201 // set.
1202 if ((s && bb == gimple_bb (s)) ||
1203 m_cache.block_range (range, bb, name, false) ||
1204 m_cache.block_range (range, e->dest, name, false))
1205 {
1206 range_on_edge (range, e, name);
1207 if (!range.varying_p ())
1208 {
1209 fprintf (f, "%d->%d ", e->src->index,
1210 e->dest->index);
1211 char c = ' ';
1212 if (e->flags & EDGE_TRUE_VALUE)
1213 fprintf (f, " (T)%c", c);
1214 else if (e->flags & EDGE_FALSE_VALUE)
1215 fprintf (f, " (F)%c", c);
1216 else
1217 fprintf (f, " ");
1218 print_generic_expr (f, name, TDF_SLIM);
1219 fprintf(f, " : \t");
1220 range.dump(f);
1221 fprintf (f, "\n");
1222 }
1223 }
1224 }
1225 }
1226 }
1227 }
1228
1229 // Print the known table values to file F.
1230
1231 void
1232 gimple_ranger::dump (FILE *f)
1233 {
1234 basic_block bb;
1235
1236 FOR_EACH_BB_FN (bb, cfun)
1237 dump_bb (f, bb);
1238
1239 m_cache.dump (f, false);
1240 }
1241
1242 // If SCEV has any information about phi node NAME, return it as a range in R.
1243
1244 void
1245 fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
1246 class loop *l, gphi *phi,
1247 fur_source &src)
1248 {
1249 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1250 tree min, max, type = TREE_TYPE (name);
1251 if (bounds_of_var_in_loop (&min, &max, src.m_query, l, phi, name))
1252 {
1253 if (TREE_CODE (min) != INTEGER_CST)
1254 {
1255 if (src.m_query
1256 && src.m_query->range_of_expr (r, min, phi)
1257 && !r.undefined_p ())
1258 min = wide_int_to_tree (type, r.lower_bound ());
1259 else
1260 min = vrp_val_min (type);
1261 }
1262 if (TREE_CODE (max) != INTEGER_CST)
1263 {
1264 if (src.m_query
1265 && src.m_query->range_of_expr (r, max, phi)
1266 && !r.undefined_p ())
1267 max = wide_int_to_tree (type, r.upper_bound ());
1268 else
1269 max = vrp_val_max (type);
1270 }
1271 r.set (min, max);
1272 }
1273 else
1274 r.set_varying (type);
1275 }
1276
1277 // --------------------------------------------------------------------------
1278 // trace_ranger implementation.
1279
1280
1281 trace_ranger::trace_ranger ()
1282 {
1283 indent = 0;
1284 trace_count = 0;
1285 }
1286
1287 // If dumping, return true and print the prefix for the next output line.
1288
1289 bool
1290 trace_ranger::dumping (unsigned counter, bool trailing)
1291 {
1292 if (dump_file && (dump_flags & TDF_DETAILS))
1293 {
1294 // Print counter index as well as INDENT spaces.
1295 if (!trailing)
1296 fprintf (dump_file, " %-7u ", counter);
1297 else
1298 fprintf (dump_file, " ");
1299 unsigned x;
1300 for (x = 0; x< indent; x++)
1301 fputc (' ', dump_file);
1302 return true;
1303 }
1304 return false;
1305 }
1306
1307 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
1308 // returning RESULT.
1309
1310 bool
1311 trace_ranger::trailer (unsigned counter, const char *caller, bool result,
1312 tree name, const irange &r)
1313 {
1314 if (dumping (counter, true))
1315 {
1316 indent -= bump;
1317 fputs(result ? "TRUE : " : "FALSE : ", dump_file);
1318 fprintf (dump_file, "(%u) ", counter);
1319 fputs (caller, dump_file);
1320 fputs (" (",dump_file);
1321 if (name)
1322 print_generic_expr (dump_file, name, TDF_SLIM);
1323 fputs (") ",dump_file);
1324 if (result)
1325 {
1326 r.dump (dump_file);
1327 fputc('\n', dump_file);
1328 }
1329 else
1330 fputc('\n', dump_file);
1331 // Marks the end of a request.
1332 if (indent == 0)
1333 fputc('\n', dump_file);
1334 }
1335 return result;
1336 }
1337
1338 // Tracing version of range_on_edge. Call it with printing wrappers.
1339
1340 bool
1341 trace_ranger::range_on_edge (irange &r, edge e, tree name)
1342 {
1343 unsigned idx = ++trace_count;
1344 if (dumping (idx))
1345 {
1346 fprintf (dump_file, "range_on_edge (");
1347 print_generic_expr (dump_file, name, TDF_SLIM);
1348 fprintf (dump_file, ") on edge %d->%d\n", e->src->index, e->dest->index);
1349 indent += bump;
1350 }
1351
1352 bool res = gimple_ranger::range_on_edge (r, e, name);
1353 trailer (idx, "range_on_edge", true, name, r);
1354 return res;
1355 }
1356
1357 // Tracing version of range_on_entry. Call it with printing wrappers.
1358
1359 void
1360 trace_ranger::range_on_entry (irange &r, basic_block bb, tree name)
1361 {
1362 unsigned idx = ++trace_count;
1363 if (dumping (idx))
1364 {
1365 fprintf (dump_file, "range_on_entry (");
1366 print_generic_expr (dump_file, name, TDF_SLIM);
1367 fprintf (dump_file, ") to BB %d\n", bb->index);
1368 indent += bump;
1369 }
1370
1371 gimple_ranger::range_on_entry (r, bb, name);
1372
1373 trailer (idx, "range_on_entry", true, name, r);
1374 }
1375
1376 // Tracing version of range_on_exit. Call it with printing wrappers.
1377
1378 void
1379 trace_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1380 {
1381 unsigned idx = ++trace_count;
1382 if (dumping (idx))
1383 {
1384 fprintf (dump_file, "range_on_exit (");
1385 print_generic_expr (dump_file, name, TDF_SLIM);
1386 fprintf (dump_file, ") from BB %d\n", bb->index);
1387 indent += bump;
1388 }
1389
1390 gimple_ranger::range_on_exit (r, bb, name);
1391
1392 trailer (idx, "range_on_exit", true, name, r);
1393 }
1394
1395 // Tracing version of range_of_stmt. Call it with printing wrappers.
1396
1397 bool
1398 trace_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1399 {
1400 bool res;
1401 unsigned idx = ++trace_count;
1402 if (dumping (idx))
1403 {
1404 fprintf (dump_file, "range_of_stmt (");
1405 if (name)
1406 print_generic_expr (dump_file, name, TDF_SLIM);
1407 fputs (") at stmt ", dump_file);
1408 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1409 indent += bump;
1410 }
1411
1412 res = gimple_ranger::range_of_stmt (r, s, name);
1413
1414 return trailer (idx, "range_of_stmt", res, name, r);
1415 }
1416
1417 // Tracing version of range_of_expr. Call it with printing wrappers.
1418
1419 bool
1420 trace_ranger::range_of_expr (irange &r, tree name, gimple *s)
1421 {
1422 bool res;
1423 unsigned idx = ++trace_count;
1424 if (dumping (idx))
1425 {
1426 fprintf (dump_file, "range_of_expr(");
1427 print_generic_expr (dump_file, name, TDF_SLIM);
1428 fputs (")", dump_file);
1429 if (s)
1430 {
1431 fputs (" at stmt ", dump_file);
1432 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1433 }
1434 else
1435 fputs ("\n", dump_file);
1436 indent += bump;
1437 }
1438
1439 res = gimple_ranger::range_of_expr (r, name, s);
1440
1441 return trailer (idx, "range_of_expr", res, name, r);
1442 }
1443
1444 gimple_ranger *
1445 enable_ranger (struct function *fun)
1446 {
1447 gimple_ranger *r;
1448
1449 if (param_evrp_mode & EVRP_MODE_TRACE)
1450 r = new trace_ranger;
1451 else
1452 r = new gimple_ranger;
1453
1454 fun->x_range_query = r;
1455
1456 return r;
1457 }
1458
1459 void
1460 disable_ranger (struct function *fun)
1461 {
1462 delete fun->x_range_query;
1463
1464 fun->x_range_query = &global_ranges;
1465 }