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