]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range-fold.cc
Split gimple-range into gimple-range-fold and gimple-range.
[thirdparty/gcc.git] / gcc / gimple-range-fold.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 "tree.h"
28 #include "gimple.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "optabs-tree.h"
32 #include "gimple-fold.h"
33 #include "wide-int.h"
34 #include "fold-const.h"
35 #include "case-cfn-macros.h"
36 #include "omp-general.h"
37 #include "cfgloop.h"
38 #include "tree-ssa-loop.h"
39 #include "tree-scalar-evolution.h"
40 #include "vr-values.h"
41 #include "range.h"
42 #include "value-query.h"
43 #include "range-op.h"
44 #include "gimple-range-fold.h"
45 #include "gimple-range-edge.h"
46 #include "gimple-range-gori.h"
47 // Construct a fur_source, and set the m_query field.
48
49 fur_source::fur_source (range_query *q)
50 {
51 if (q)
52 m_query = q;
53 else if (cfun)
54 m_query = get_range_query (cfun);
55 else
56 m_query = get_global_range_query ();
57 m_gori = NULL;
58 }
59
60 // Invoke range_of_expr on EXPR.
61
62 bool
63 fur_source::get_operand (irange &r, tree expr)
64 {
65 return m_query->range_of_expr (r, expr);
66 }
67
68 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
69 // range_query to get the range on the edge.
70
71 bool
72 fur_source::get_phi_operand (irange &r, tree expr, edge e)
73 {
74 return m_query->range_on_edge (r, e, expr);
75 }
76
77 // Default is no relation.
78
79 relation_kind
80 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
81 tree op2 ATTRIBUTE_UNUSED)
82 {
83 return VREL_NONE;
84 }
85
86 // Default registers nothing.
87
88 void
89 fur_source::register_relation (gimple *s ATTRIBUTE_UNUSED,
90 relation_kind k ATTRIBUTE_UNUSED,
91 tree op1 ATTRIBUTE_UNUSED,
92 tree op2 ATTRIBUTE_UNUSED)
93 {
94 }
95
96 // Default registers nothing.
97
98 void
99 fur_source::register_relation (edge e ATTRIBUTE_UNUSED,
100 relation_kind k ATTRIBUTE_UNUSED,
101 tree op1 ATTRIBUTE_UNUSED,
102 tree op2 ATTRIBUTE_UNUSED)
103 {
104 }
105
106 // This version of fur_source will pick a range up off an edge.
107
108 class fur_edge : public fur_source
109 {
110 public:
111 fur_edge (edge e, range_query *q = NULL);
112 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
113 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
114 private:
115 edge m_edge;
116 };
117
118 // Instantiate an edge based fur_source.
119
120 inline
121 fur_edge::fur_edge (edge e, range_query *q) : fur_source (q)
122 {
123 m_edge = e;
124 }
125
126 // Get the value of EXPR on edge m_edge.
127
128 bool
129 fur_edge::get_operand (irange &r, tree expr)
130 {
131 return m_query->range_on_edge (r, m_edge, expr);
132 }
133
134 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
135 // range_query to get the range on the edge.
136
137 bool
138 fur_edge::get_phi_operand (irange &r, tree expr, edge e)
139 {
140 // Edge to edge recalculations not supoprted yet, until we sort it out.
141 gcc_checking_assert (e == m_edge);
142 return m_query->range_on_edge (r, e, expr);
143 }
144
145 // Instantiate a stmt based fur_source.
146
147 fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
148 {
149 m_stmt = s;
150 }
151
152 // Retreive range of EXPR as it occurs as a use on stmt M_STMT.
153
154 bool
155 fur_stmt::get_operand (irange &r, tree expr)
156 {
157 return m_query->range_of_expr (r, expr, m_stmt);
158 }
159
160 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
161 // range_query to get the range on the edge.
162
163 bool
164 fur_stmt::get_phi_operand (irange &r, tree expr, edge e)
165 {
166 // Pick up the range of expr from edge E.
167 fur_edge e_src (e, m_query);
168 return e_src.get_operand (r, expr);
169 }
170
171 // Return relation based from m_stmt.
172
173 relation_kind
174 fur_stmt::query_relation (tree op1, tree op2)
175 {
176 return m_query->query_relation (m_stmt, op1, op2);
177 }
178
179 // Instantiate a stmt based fur_source with a GORI object.
180
181
182 fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
183 : fur_stmt (s, q)
184 {
185 gcc_checking_assert (gori);
186 m_gori = gori;
187 // Set relations if there is an oracle in the range_query.
188 // This will enable registering of relationships as they are discovered.
189 m_oracle = q->oracle ();
190
191 }
192
193 // Register a relation on a stmt if there is an oracle.
194
195 void
196 fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
197 {
198 if (m_oracle)
199 m_oracle->register_relation (s, k, op1, op2);
200 }
201
202 // Register a relation on an edge if there is an oracle.
203
204 void
205 fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
206 {
207 if (m_oracle)
208 m_oracle->register_relation (e, k, op1, op2);
209 }
210
211 // This version of fur_source will pick a range up from a list of ranges
212 // supplied by the caller.
213
214 class fur_list : public fur_source
215 {
216 public:
217 fur_list (irange &r1);
218 fur_list (irange &r1, irange &r2);
219 fur_list (unsigned num, irange *list);
220 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
221 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
222 private:
223 int_range_max m_local[2];
224 irange *m_list;
225 unsigned m_index;
226 unsigned m_limit;
227 };
228
229 // One range supplied for unary operations.
230
231 fur_list::fur_list (irange &r1) : fur_source (NULL)
232 {
233 m_list = m_local;
234 m_index = 0;
235 m_limit = 1;
236 m_local[0] = r1;
237 }
238
239 // Two ranges supplied for binary operations.
240
241 fur_list::fur_list (irange &r1, irange &r2) : fur_source (NULL)
242 {
243 m_list = m_local;
244 m_index = 0;
245 m_limit = 2;
246 m_local[0] = r1;
247 m_local[0] = r2;
248 }
249
250 // Arbitrary number of ranges in a vector.
251
252 fur_list::fur_list (unsigned num, irange *list) : fur_source (NULL)
253 {
254 m_list = list;
255 m_index = 0;
256 m_limit = num;
257 }
258
259 // Get the next operand from the vector, ensure types are compatible.
260
261 bool
262 fur_list::get_operand (irange &r, tree expr)
263 {
264 if (m_index >= m_limit)
265 return m_query->range_of_expr (r, expr);
266 r = m_list[m_index++];
267 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
268 return true;
269 }
270
271 // This will simply pick the next operand from the vector.
272 bool
273 fur_list::get_phi_operand (irange &r, tree expr, edge e ATTRIBUTE_UNUSED)
274 {
275 return get_operand (r, expr);
276 }
277
278 // Fold stmt S into range R using R1 as the first operand.
279
280 bool
281 fold_range (irange &r, gimple *s, irange &r1)
282 {
283 fold_using_range f;
284 fur_list src (r1);
285 return f.fold_stmt (r, s, src);
286 }
287
288 // Fold stmt S into range R using R1 and R2 as the first two operands.
289
290 bool
291 fold_range (irange &r, gimple *s, irange &r1, irange &r2)
292 {
293 fold_using_range f;
294 fur_list src (r1, r2);
295 return f.fold_stmt (r, s, src);
296 }
297
298 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
299 // operands encountered.
300
301 bool
302 fold_range (irange &r, gimple *s, unsigned num_elements, irange *vector)
303 {
304 fold_using_range f;
305 fur_list src (num_elements, vector);
306 return f.fold_stmt (r, s, src);
307 }
308
309 // Fold stmt S into range R using range query Q.
310
311 bool
312 fold_range (irange &r, gimple *s, range_query *q)
313 {
314 fold_using_range f;
315 fur_stmt src (s, q);
316 return f.fold_stmt (r, s, src);
317 }
318
319 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
320
321 bool
322 fold_range (irange &r, gimple *s, edge on_edge, range_query *q)
323 {
324 fold_using_range f;
325 fur_edge src (on_edge, q);
326 return f.fold_stmt (r, s, src);
327 }
328
329 // -------------------------------------------------------------------------
330
331 // Adjust the range for a pointer difference where the operands came
332 // from a memchr.
333 //
334 // This notices the following sequence:
335 //
336 // def = __builtin_memchr (arg, 0, sz)
337 // n = def - arg
338 //
339 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
340
341 static void
342 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
343 {
344 tree op0 = gimple_assign_rhs1 (diff_stmt);
345 tree op1 = gimple_assign_rhs2 (diff_stmt);
346 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
347 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
348 gimple *call;
349
350 if (TREE_CODE (op0) == SSA_NAME
351 && TREE_CODE (op1) == SSA_NAME
352 && (call = SSA_NAME_DEF_STMT (op0))
353 && is_gimple_call (call)
354 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
355 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
356 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
357 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
358 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
359 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
360 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
361 && integer_zerop (gimple_call_arg (call, 1)))
362 {
363 tree max = vrp_val_max (ptrdiff_type_node);
364 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
365 tree expr_type = gimple_expr_type (diff_stmt);
366 tree range_min = build_zero_cst (expr_type);
367 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
368 int_range<2> r (range_min, range_max);
369 res.intersect (r);
370 }
371 }
372
373 // This function looks for situations when walking the use/def chains
374 // may provide additonal contextual range information not exposed on
375 // this statement. Like knowing the IMAGPART return value from a
376 // builtin function is a boolean result.
377
378 // We should rework how we're called, as we have an op_unknown entry
379 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
380 // function gets called.
381
382 static void
383 gimple_range_adjustment (irange &res, const gimple *stmt)
384 {
385 switch (gimple_expr_code (stmt))
386 {
387 case POINTER_DIFF_EXPR:
388 adjust_pointer_diff_expr (res, stmt);
389 return;
390
391 case IMAGPART_EXPR:
392 {
393 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
394 if (TREE_CODE (name) == SSA_NAME)
395 {
396 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
397 if (def_stmt && is_gimple_call (def_stmt)
398 && gimple_call_internal_p (def_stmt))
399 {
400 switch (gimple_call_internal_fn (def_stmt))
401 {
402 case IFN_ADD_OVERFLOW:
403 case IFN_SUB_OVERFLOW:
404 case IFN_MUL_OVERFLOW:
405 case IFN_ATOMIC_COMPARE_EXCHANGE:
406 {
407 int_range<2> r;
408 r.set_varying (boolean_type_node);
409 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
410 range_cast (r, type);
411 res.intersect (r);
412 }
413 default:
414 break;
415 }
416 }
417 }
418 break;
419 }
420
421 default:
422 break;
423 }
424 }
425
426 // Return the base of the RHS of an assignment.
427
428 static tree
429 gimple_range_base_of_assignment (const gimple *stmt)
430 {
431 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
432 tree op1 = gimple_assign_rhs1 (stmt);
433 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
434 return get_base_address (TREE_OPERAND (op1, 0));
435 return op1;
436 }
437
438 // Return the first operand of this statement if it is a valid operand
439 // supported by ranges, otherwise return NULL_TREE. Special case is
440 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
441
442 tree
443 gimple_range_operand1 (const gimple *stmt)
444 {
445 gcc_checking_assert (gimple_range_handler (stmt));
446
447 switch (gimple_code (stmt))
448 {
449 case GIMPLE_COND:
450 return gimple_cond_lhs (stmt);
451 case GIMPLE_ASSIGN:
452 {
453 tree base = gimple_range_base_of_assignment (stmt);
454 if (base && TREE_CODE (base) == MEM_REF)
455 {
456 // If the base address is an SSA_NAME, we return it
457 // here. This allows processing of the range of that
458 // name, while the rest of the expression is simply
459 // ignored. The code in range_ops will see the
460 // ADDR_EXPR and do the right thing.
461 tree ssa = TREE_OPERAND (base, 0);
462 if (TREE_CODE (ssa) == SSA_NAME)
463 return ssa;
464 }
465 return base;
466 }
467 default:
468 break;
469 }
470 return NULL;
471 }
472
473 // Return the second operand of statement STMT, otherwise return NULL_TREE.
474
475 tree
476 gimple_range_operand2 (const gimple *stmt)
477 {
478 gcc_checking_assert (gimple_range_handler (stmt));
479
480 switch (gimple_code (stmt))
481 {
482 case GIMPLE_COND:
483 return gimple_cond_rhs (stmt);
484 case GIMPLE_ASSIGN:
485 if (gimple_num_ops (stmt) >= 3)
486 return gimple_assign_rhs2 (stmt);
487 default:
488 break;
489 }
490 return NULL_TREE;
491 }
492
493 // Calculate a range for statement S and return it in R. If NAME is provided it
494 // represents the SSA_NAME on the LHS of the statement. It is only required
495 // if there is more than one lhs/output. If a range cannot
496 // be calculated, return false.
497
498 bool
499 fold_using_range::fold_stmt (irange &r, gimple *s, fur_source &src, tree name)
500 {
501 bool res = false;
502 // If name and S are specified, make sure it is an LHS of S.
503 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
504 name == gimple_get_lhs (s));
505
506 if (!name)
507 name = gimple_get_lhs (s);
508
509 // Process addresses.
510 if (gimple_code (s) == GIMPLE_ASSIGN
511 && gimple_assign_rhs_code (s) == ADDR_EXPR)
512 return range_of_address (r, s, src);
513
514 if (gimple_range_handler (s))
515 res = range_of_range_op (r, s, src);
516 else if (is_a<gphi *>(s))
517 res = range_of_phi (r, as_a<gphi *> (s), src);
518 else if (is_a<gcall *>(s))
519 res = range_of_call (r, as_a<gcall *> (s), src);
520 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
521 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
522
523 if (!res)
524 {
525 // If no name is specified, try the expression kind.
526 if (!name)
527 {
528 tree t = gimple_expr_type (s);
529 if (!irange::supports_type_p (t))
530 return false;
531 r.set_varying (t);
532 return true;
533 }
534 if (!gimple_range_ssa_p (name))
535 return false;
536 // We don't understand the stmt, so return the global range.
537 r = gimple_range_global (name);
538 return true;
539 }
540
541 if (r.undefined_p ())
542 return true;
543
544 // We sometimes get compatible types copied from operands, make sure
545 // the correct type is being returned.
546 if (name && TREE_TYPE (name) != r.type ())
547 {
548 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
549 range_cast (r, TREE_TYPE (name));
550 }
551 return true;
552 }
553
554 // Calculate a range for range_op statement S and return it in R. If any
555 // If a range cannot be calculated, return false.
556
557 bool
558 fold_using_range::range_of_range_op (irange &r, gimple *s, fur_source &src)
559 {
560 int_range_max range1, range2;
561 tree type = gimple_expr_type (s);
562 range_operator *handler = gimple_range_handler (s);
563 gcc_checking_assert (handler);
564 gcc_checking_assert (irange::supports_type_p (type));
565
566 tree lhs = gimple_get_lhs (s);
567 tree op1 = gimple_range_operand1 (s);
568 tree op2 = gimple_range_operand2 (s);
569
570 if (src.get_operand (range1, op1))
571 {
572 if (!op2)
573 {
574 // Fold range, and register any dependency if available.
575 int_range<2> r2 (type);
576 handler->fold_range (r, type, range1, r2);
577 if (lhs && gimple_range_ssa_p (op1))
578 {
579 if (src.gori ())
580 src.gori ()->register_dependency (lhs, op1);
581 relation_kind rel;
582 rel = handler->lhs_op1_relation (r, range1, range1);
583 if (rel != VREL_NONE)
584 src.register_relation (s, rel, lhs, op1);
585 }
586 }
587 else if (src.get_operand (range2, op2))
588 {
589 relation_kind rel = src.query_relation (op1, op2);
590 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_NONE)
591 {
592 fprintf (dump_file, " folding with relation ");
593 print_relation (dump_file, rel);
594 fputc ('\n', dump_file);
595 }
596 // Fold range, and register any dependency if available.
597 handler->fold_range (r, type, range1, range2, rel);
598 relation_fold_and_or (r, s, src);
599 if (lhs)
600 {
601 if (src.gori ())
602 {
603 src.gori ()->register_dependency (lhs, op1);
604 src.gori ()->register_dependency (lhs, op2);
605 }
606 if (gimple_range_ssa_p (op1))
607 {
608 rel = handler->lhs_op1_relation (r, range1, range2);
609 if (rel != VREL_NONE)
610 src.register_relation (s, rel, lhs, op1);
611 }
612 if (gimple_range_ssa_p (op2))
613 {
614 rel= handler->lhs_op2_relation (r, range1, range2);
615 if (rel != VREL_NONE)
616 src.register_relation (s, rel, lhs, op2);
617 }
618 }
619 else if (is_a<gcond *> (s))
620 postfold_gcond_edges (as_a<gcond *> (s), src);
621 }
622 else
623 r.set_varying (type);
624 }
625 else
626 r.set_varying (type);
627 // Make certain range-op adjustments that aren't handled any other way.
628 gimple_range_adjustment (r, s);
629 return true;
630 }
631
632 // Calculate the range of an assignment containing an ADDR_EXPR.
633 // Return the range in R.
634 // If a range cannot be calculated, set it to VARYING and return true.
635
636 bool
637 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
638 {
639 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
640 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
641
642 bool strict_overflow_p;
643 tree expr = gimple_assign_rhs1 (stmt);
644 poly_int64 bitsize, bitpos;
645 tree offset;
646 machine_mode mode;
647 int unsignedp, reversep, volatilep;
648 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
649 &bitpos, &offset, &mode, &unsignedp,
650 &reversep, &volatilep);
651
652
653 if (base != NULL_TREE
654 && TREE_CODE (base) == MEM_REF
655 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
656 {
657 tree ssa = TREE_OPERAND (base, 0);
658 tree lhs = gimple_get_lhs (stmt);
659 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
660 src.gori ()->register_dependency (lhs, ssa);
661 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
662 src.get_operand (r, ssa);
663 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
664
665 poly_offset_int off = 0;
666 bool off_cst = false;
667 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
668 {
669 off = mem_ref_offset (base);
670 if (offset)
671 off += poly_offset_int::from (wi::to_poly_wide (offset),
672 SIGNED);
673 off <<= LOG2_BITS_PER_UNIT;
674 off += bitpos;
675 off_cst = true;
676 }
677 /* If &X->a is equal to X, the range of X is the result. */
678 if (off_cst && known_eq (off, 0))
679 return true;
680 else if (flag_delete_null_pointer_checks
681 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
682 {
683 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
684 allow going from non-NULL pointer to NULL. */
685 if(!range_includes_zero_p (&r))
686 return true;
687 }
688 /* If MEM_REF has a "positive" offset, consider it non-NULL
689 always, for -fdelete-null-pointer-checks also "negative"
690 ones. Punt for unknown offsets (e.g. variable ones). */
691 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
692 && off_cst
693 && known_ne (off, 0)
694 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
695 {
696 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
697 return true;
698 }
699 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
700 return true;
701 }
702
703 // Handle "= &a".
704 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
705 {
706 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
707 return true;
708 }
709
710 // Otherwise return varying.
711 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
712 return true;
713 }
714
715 // Calculate a range for phi statement S and return it in R.
716 // If a range cannot be calculated, return false.
717
718 bool
719 fold_using_range::range_of_phi (irange &r, gphi *phi, fur_source &src)
720 {
721 tree phi_def = gimple_phi_result (phi);
722 tree type = TREE_TYPE (phi_def);
723 int_range_max arg_range;
724 unsigned x;
725
726 if (!irange::supports_type_p (type))
727 return false;
728
729 // Start with an empty range, unioning in each argument's range.
730 r.set_undefined ();
731 for (x = 0; x < gimple_phi_num_args (phi); x++)
732 {
733 tree arg = gimple_phi_arg_def (phi, x);
734 edge e = gimple_phi_arg_edge (phi, x);
735
736 // Register potential dependencies for stale value tracking.
737 if (gimple_range_ssa_p (arg) && src.gori ())
738 src.gori ()->register_dependency (phi_def, arg);
739
740 // Get the range of the argument on its edge.
741 src.get_phi_operand (arg_range, arg, e);
742 // If we're recomputing the argument elsewhere, try to refine it.
743 r.union_ (arg_range);
744 // Once the value reaches varying, stop looking.
745 if (r.varying_p ())
746 break;
747 }
748
749 // If SCEV is available, query if this PHI has any knonwn values.
750 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
751 {
752 value_range loop_range;
753 class loop *l = loop_containing_stmt (phi);
754 if (l && loop_outer (l))
755 {
756 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
757 if (!loop_range.varying_p ())
758 {
759 if (dump_file && (dump_flags & TDF_DETAILS))
760 {
761 fprintf (dump_file, " Loops range found for ");
762 print_generic_expr (dump_file, phi_def, TDF_SLIM);
763 fprintf (dump_file, ": ");
764 loop_range.dump (dump_file);
765 fprintf (dump_file, " and calculated range :");
766 r.dump (dump_file);
767 fprintf (dump_file, "\n");
768 }
769 r.intersect (loop_range);
770 }
771 }
772 }
773
774 return true;
775 }
776
777 // Calculate a range for call statement S and return it in R.
778 // If a range cannot be calculated, return false.
779
780 bool
781 fold_using_range::range_of_call (irange &r, gcall *call, fur_source &src)
782 {
783 tree type = gimple_call_return_type (call);
784 tree lhs = gimple_call_lhs (call);
785 bool strict_overflow_p;
786
787 if (!irange::supports_type_p (type))
788 return false;
789
790 if (range_of_builtin_call (r, call, src))
791 ;
792 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
793 r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
794 else if (gimple_call_nonnull_result_p (call)
795 || gimple_call_nonnull_arg (call))
796 r = range_nonzero (type);
797 else
798 r.set_varying (type);
799
800 // If there is an LHS, intersect that with what is known.
801 if (lhs)
802 {
803 value_range def;
804 def = gimple_range_global (lhs);
805 r.intersect (def);
806 }
807 return true;
808 }
809
810 // Return the range of a __builtin_ubsan* in CALL and set it in R.
811 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
812 // MULT_EXPR).
813
814 void
815 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
816 tree_code code, fur_source &src)
817 {
818 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
819 || code == MULT_EXPR);
820 tree type = gimple_call_return_type (call);
821 range_operator *op = range_op_handler (code, type);
822 gcc_checking_assert (op);
823 int_range_max ir0, ir1;
824 tree arg0 = gimple_call_arg (call, 0);
825 tree arg1 = gimple_call_arg (call, 1);
826 src.get_operand (ir0, arg0);
827 src.get_operand (ir1, arg1);
828
829 bool saved_flag_wrapv = flag_wrapv;
830 // Pretend the arithmetic is wrapping. If there is any overflow,
831 // we'll complain, but will actually do wrapping operation.
832 flag_wrapv = 1;
833 op->fold_range (r, type, ir0, ir1);
834 flag_wrapv = saved_flag_wrapv;
835
836 // If for both arguments vrp_valueize returned non-NULL, this should
837 // have been already folded and if not, it wasn't folded because of
838 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
839 if (r.singleton_p ())
840 r.set_varying (type);
841 }
842
843 // For a builtin in CALL, return a range in R if known and return
844 // TRUE. Otherwise return FALSE.
845
846 bool
847 fold_using_range::range_of_builtin_call (irange &r, gcall *call,
848 fur_source &src)
849 {
850 combined_fn func = gimple_call_combined_fn (call);
851 if (func == CFN_LAST)
852 return false;
853
854 tree type = gimple_call_return_type (call);
855 tree arg;
856 int mini, maxi, zerov = 0, prec;
857 scalar_int_mode mode;
858
859 switch (func)
860 {
861 case CFN_BUILT_IN_CONSTANT_P:
862 if (cfun->after_inlining)
863 {
864 r.set_zero (type);
865 // r.equiv_clear ();
866 return true;
867 }
868 arg = gimple_call_arg (call, 0);
869 if (src.get_operand (r, arg) && r.singleton_p ())
870 {
871 r.set (build_one_cst (type), build_one_cst (type));
872 return true;
873 }
874 break;
875
876 CASE_CFN_FFS:
877 CASE_CFN_POPCOUNT:
878 // __builtin_ffs* and __builtin_popcount* return [0, prec].
879 arg = gimple_call_arg (call, 0);
880 prec = TYPE_PRECISION (TREE_TYPE (arg));
881 mini = 0;
882 maxi = prec;
883 src.get_operand (r, arg);
884 // If arg is non-zero, then ffs or popcount are non-zero.
885 if (!range_includes_zero_p (&r))
886 mini = 1;
887 // If some high bits are known to be zero, decrease the maximum.
888 if (!r.undefined_p ())
889 {
890 if (TYPE_SIGN (r.type ()) == SIGNED)
891 range_cast (r, unsigned_type_for (r.type ()));
892 wide_int max = r.upper_bound ();
893 maxi = wi::floor_log2 (max) + 1;
894 }
895 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
896 return true;
897
898 CASE_CFN_PARITY:
899 r.set (build_zero_cst (type), build_one_cst (type));
900 return true;
901
902 CASE_CFN_CLZ:
903 // __builtin_c[lt]z* return [0, prec-1], except when the
904 // argument is 0, but that is undefined behavior.
905 //
906 // For __builtin_c[lt]z* consider argument of 0 always undefined
907 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
908 arg = gimple_call_arg (call, 0);
909 prec = TYPE_PRECISION (TREE_TYPE (arg));
910 mini = 0;
911 maxi = prec - 1;
912 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
913 if (gimple_call_internal_p (call))
914 {
915 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
916 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
917 {
918 // Only handle the single common value.
919 if (zerov == prec)
920 maxi = prec;
921 else
922 // Magic value to give up, unless we can prove arg is non-zero.
923 mini = -2;
924 }
925 }
926
927 src.get_operand (r, arg);
928 // From clz of minimum we can compute result maximum.
929 if (!r.undefined_p ())
930 {
931 // From clz of minimum we can compute result maximum.
932 if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
933 {
934 maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
935 if (mini == -2)
936 mini = 0;
937 }
938 else if (!range_includes_zero_p (&r))
939 {
940 mini = 0;
941 maxi = prec - 1;
942 }
943 if (mini == -2)
944 break;
945 // From clz of maximum we can compute result minimum.
946 wide_int max = r.upper_bound ();
947 int newmini = prec - 1 - wi::floor_log2 (max);
948 if (max == 0)
949 {
950 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
951 // return [prec, prec], otherwise ignore the range.
952 if (maxi == prec)
953 mini = prec;
954 }
955 else
956 mini = newmini;
957 }
958 if (mini == -2)
959 break;
960 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
961 return true;
962
963 CASE_CFN_CTZ:
964 // __builtin_ctz* return [0, prec-1], except for when the
965 // argument is 0, but that is undefined behavior.
966 //
967 // For __builtin_ctz* consider argument of 0 always undefined
968 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
969 arg = gimple_call_arg (call, 0);
970 prec = TYPE_PRECISION (TREE_TYPE (arg));
971 mini = 0;
972 maxi = prec - 1;
973 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
974 if (gimple_call_internal_p (call))
975 {
976 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
977 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
978 {
979 // Handle only the two common values.
980 if (zerov == -1)
981 mini = -1;
982 else if (zerov == prec)
983 maxi = prec;
984 else
985 // Magic value to give up, unless we can prove arg is non-zero.
986 mini = -2;
987 }
988 }
989 src.get_operand (r, arg);
990 if (!r.undefined_p ())
991 {
992 // If arg is non-zero, then use [0, prec - 1].
993 if (!range_includes_zero_p (&r))
994 {
995 mini = 0;
996 maxi = prec - 1;
997 }
998 // If some high bits are known to be zero, we can decrease
999 // the maximum.
1000 wide_int max = r.upper_bound ();
1001 if (max == 0)
1002 {
1003 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1004 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1005 // Otherwise ignore the range.
1006 if (mini == -1)
1007 maxi = -1;
1008 else if (maxi == prec)
1009 mini = prec;
1010 }
1011 // If value at zero is prec and 0 is in the range, we can't lower
1012 // the upper bound. We could create two separate ranges though,
1013 // [0,floor_log2(max)][prec,prec] though.
1014 else if (maxi != prec)
1015 maxi = wi::floor_log2 (max);
1016 }
1017 if (mini == -2)
1018 break;
1019 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1020 return true;
1021
1022 CASE_CFN_CLRSB:
1023 arg = gimple_call_arg (call, 0);
1024 prec = TYPE_PRECISION (TREE_TYPE (arg));
1025 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
1026 return true;
1027 case CFN_UBSAN_CHECK_ADD:
1028 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
1029 return true;
1030 case CFN_UBSAN_CHECK_SUB:
1031 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
1032 return true;
1033 case CFN_UBSAN_CHECK_MUL:
1034 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
1035 return true;
1036
1037 case CFN_GOACC_DIM_SIZE:
1038 case CFN_GOACC_DIM_POS:
1039 // Optimizing these two internal functions helps the loop
1040 // optimizer eliminate outer comparisons. Size is [1,N]
1041 // and pos is [0,N-1].
1042 {
1043 bool is_pos = func == CFN_GOACC_DIM_POS;
1044 int axis = oacc_get_ifn_dim_arg (call);
1045 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1046 if (!size)
1047 // If it's dynamic, the backend might know a hardware limitation.
1048 size = targetm.goacc.dim_limit (axis);
1049
1050 r.set (build_int_cst (type, is_pos ? 0 : 1),
1051 size
1052 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1053 return true;
1054 }
1055
1056 case CFN_BUILT_IN_STRLEN:
1057 if (tree lhs = gimple_call_lhs (call))
1058 if (ptrdiff_type_node
1059 && (TYPE_PRECISION (ptrdiff_type_node)
1060 == TYPE_PRECISION (TREE_TYPE (lhs))))
1061 {
1062 tree type = TREE_TYPE (lhs);
1063 tree max = vrp_val_max (ptrdiff_type_node);
1064 wide_int wmax
1065 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1066 tree range_min = build_zero_cst (type);
1067 // To account for the terminating NULL, the maximum length
1068 // is one less than the maximum array size, which in turn
1069 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1070 // smaller than the former type).
1071 // FIXME: Use max_object_size() - 1 here.
1072 tree range_max = wide_int_to_tree (type, wmax - 2);
1073 r.set (range_min, range_max);
1074 return true;
1075 }
1076 break;
1077 default:
1078 break;
1079 }
1080 return false;
1081 }
1082
1083
1084 // Calculate a range for COND_EXPR statement S and return it in R.
1085 // If a range cannot be calculated, return false.
1086
1087 bool
1088 fold_using_range::range_of_cond_expr (irange &r, gassign *s, fur_source &src)
1089 {
1090 int_range_max cond_range, range1, range2;
1091 tree cond = gimple_assign_rhs1 (s);
1092 tree op1 = gimple_assign_rhs2 (s);
1093 tree op2 = gimple_assign_rhs3 (s);
1094
1095 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1096 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1),
1097 TREE_TYPE (op2)));
1098 if (!irange::supports_type_p (TREE_TYPE (op1)))
1099 return false;
1100
1101 src.get_operand (cond_range, cond);
1102 src.get_operand (range1, op1);
1103 src.get_operand (range2, op2);
1104
1105 // If the condition is known, choose the appropriate expression.
1106 if (cond_range.singleton_p ())
1107 {
1108 // False, pick second operand.
1109 if (cond_range.zero_p ())
1110 r = range2;
1111 else
1112 r = range1;
1113 }
1114 else
1115 {
1116 r = range1;
1117 r.union_ (range2);
1118 }
1119 return true;
1120 }
1121
1122 // If SCEV has any information about phi node NAME, return it as a range in R.
1123
1124 void
1125 fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
1126 class loop *l, gphi *phi,
1127 fur_source &src)
1128 {
1129 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1130 tree min, max, type = TREE_TYPE (name);
1131 if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
1132 {
1133 if (TREE_CODE (min) != INTEGER_CST)
1134 {
1135 if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
1136 min = wide_int_to_tree (type, r.lower_bound ());
1137 else
1138 min = vrp_val_min (type);
1139 }
1140 if (TREE_CODE (max) != INTEGER_CST)
1141 {
1142 if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
1143 max = wide_int_to_tree (type, r.upper_bound ());
1144 else
1145 max = vrp_val_max (type);
1146 }
1147 r.set (min, max);
1148 }
1149 else
1150 r.set_varying (type);
1151 }
1152
1153 // -----------------------------------------------------------------------
1154
1155 // Check if an && or || expression can be folded based on relations. ie
1156 // c_2 = a_6 > b_7
1157 // c_3 = a_6 < b_7
1158 // c_4 = c_2 && c_3
1159 // c_2 and c_3 can never be true at the same time,
1160 // Therefore c_4 can always resolve to false based purely on the relations.
1161
1162 void
1163 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1164 fur_source &src)
1165 {
1166 // No queries or already folded.
1167 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1168 return;
1169
1170 // Only care about AND and OR expressions.
1171 enum tree_code code = gimple_expr_code (s);
1172 bool is_and = false;
1173 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1174 is_and = true;
1175 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1176 return;
1177
1178 tree lhs = gimple_get_lhs (s);
1179 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1180 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1181
1182 // Deal with || and && only when there is a full set of symbolics.
1183 if (!lhs || !ssa1 || !ssa2
1184 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1185 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1186 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1187 return;
1188
1189 // Now we know its a boolean AND or OR expression with boolean operands.
1190 // Ideally we search dependencies for common names, and see what pops out.
1191 // until then, simply try to resolve direct dependencies.
1192
1193 // Both names will need to have 2 direct dependencies.
1194 tree ssa1_dep2 = src.gori ()->depend2 (ssa1);
1195 tree ssa2_dep2 = src.gori ()->depend2 (ssa2);
1196 if (!ssa1_dep2 || !ssa2_dep2)
1197 return;
1198
1199 tree ssa1_dep1 = src.gori ()->depend1 (ssa1);
1200 tree ssa2_dep1 = src.gori ()->depend1 (ssa2);
1201 // Make sure they are the same dependencies, and detect the order of the
1202 // relationship.
1203 bool reverse_op2 = true;
1204 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1205 reverse_op2 = false;
1206 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1207 return;
1208
1209 range_operator *handler1 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa1));
1210 range_operator *handler2 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa2));
1211
1212 int_range<2> bool_one (boolean_true_node, boolean_true_node);
1213
1214 relation_kind relation1 = handler1->op1_op2_relation (bool_one);
1215 relation_kind relation2 = handler2->op1_op2_relation (bool_one);
1216 if (relation1 == VREL_NONE || relation2 == VREL_NONE)
1217 return;
1218
1219 if (reverse_op2)
1220 relation2 = relation_negate (relation2);
1221
1222 // x && y is false if the relation intersection of the true cases is NULL.
1223 if (is_and && relation_intersect (relation1, relation2) == VREL_EMPTY)
1224 lhs_range = int_range<2> (boolean_false_node, boolean_false_node);
1225 // x || y is true if the union of the true cases is NO-RELATION..
1226 // ie, one or the other being true covers the full range of possibilties.
1227 else if (!is_and && relation_union (relation1, relation2) == VREL_NONE)
1228 lhs_range = bool_one;
1229 else
1230 return;
1231
1232 range_cast (lhs_range, TREE_TYPE (lhs));
1233 if (dump_file && (dump_flags & TDF_DETAILS))
1234 {
1235 fprintf (dump_file, " Relation adjustment: ");
1236 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1237 fprintf (dump_file, " and ");
1238 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1239 fprintf (dump_file, " combine to produce ");
1240 lhs_range.dump (dump_file);
1241 fputc ('\n', dump_file);
1242 }
1243
1244 return;
1245 }
1246
1247 // Register any outgoing edge relations from a conditional branch.
1248
1249 void
1250 fold_using_range::postfold_gcond_edges (gcond *s, fur_source &src)
1251 {
1252 int_range_max r;
1253 tree name;
1254 range_operator *handler;
1255 basic_block bb = gimple_bb (s);
1256
1257 edge e0 = EDGE_SUCC (bb, 0);
1258 if (!single_pred_p (e0->dest))
1259 e0 = NULL;
1260
1261 edge e1 = EDGE_SUCC (bb, 1);
1262 if (!single_pred_p (e1->dest))
1263 e1 = NULL;
1264
1265 // At least one edge needs to be single pred.
1266 if (!e0 && !e1)
1267 return;
1268
1269 // First, register the gcond itself. This will catch statements like
1270 // if (a_2 < b_5)
1271 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1272 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1273 if (ssa1 && ssa2)
1274 {
1275 handler = gimple_range_handler (s);
1276 gcc_checking_assert (handler);
1277 if (e0)
1278 {
1279 gcond_edge_range (r, e0);
1280 relation_kind relation = handler->op1_op2_relation (r);
1281 if (relation != VREL_NONE)
1282 src.register_relation (e0, relation, ssa1, ssa2);
1283 }
1284 if (e1)
1285 {
1286 gcond_edge_range (r, e1);
1287 relation_kind relation = handler->op1_op2_relation (r);
1288 if (relation != VREL_NONE)
1289 src.register_relation (e1, relation, ssa1, ssa2);
1290 }
1291 }
1292
1293 // Outgoing relations of GORI exports require a gori engine.
1294 if (!src.gori ())
1295 return;
1296
1297 range_query *q = src.query ();
1298 // Now look for other relations in the exports. This will find stmts
1299 // leading to the condition such as:
1300 // c_2 = a_4 < b_7
1301 // if (c_2)
1302
1303 FOR_EACH_GORI_EXPORT_NAME (*(src.gori ()), bb, name)
1304 {
1305 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1306 continue;
1307 gimple *stmt = SSA_NAME_DEF_STMT (name);
1308 handler = gimple_range_handler (stmt);
1309 if (!handler)
1310 continue;
1311 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
1312 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
1313 if (ssa1 && ssa2)
1314 {
1315 if (e0 && src.gori ()->outgoing_edge_range_p (r, e0, name, *q)
1316 && r.singleton_p ())
1317 {
1318 relation_kind relation = handler->op1_op2_relation (r);
1319 if (relation != VREL_NONE)
1320 src.register_relation (e0, relation, ssa1, ssa2);
1321 }
1322 if (e1 && src.gori ()->outgoing_edge_range_p (r, e1, name, *q)
1323 && r.singleton_p ())
1324 {
1325 relation_kind relation = handler->op1_op2_relation (r);
1326 if (relation != VREL_NONE)
1327 src.register_relation (e1, relation, ssa1, ssa2);
1328 }
1329 }
1330 }
1331 }