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