libstdc++: Remove std::__unicode::__null_sentinel
[official-gcc.git] / gcc / gimple-range-fold.cc
blob0cc531999631645528b79675455a41e59db9b17c
1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "insn-codes.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "optabs-tree.h"
32 #include "gimple-iterator.h"
33 #include "gimple-fold.h"
34 #include "wide-int.h"
35 #include "fold-const.h"
36 #include "case-cfn-macros.h"
37 #include "omp-general.h"
38 #include "cfgloop.h"
39 #include "tree-ssa-loop.h"
40 #include "tree-scalar-evolution.h"
41 #include "langhooks.h"
42 #include "vr-values.h"
43 #include "range.h"
44 #include "value-query.h"
45 #include "gimple-range-op.h"
46 #include "gimple-range.h"
47 #include "cgraph.h"
48 #include "alloc-pool.h"
49 #include "symbol-summary.h"
50 #include "ipa-utils.h"
51 #include "ipa-prop.h"
52 // Construct a fur_source, and set the m_query field.
54 fur_source::fur_source (range_query *q)
56 if (q)
57 m_query = q;
58 else
59 m_query = get_range_query (cfun);
60 m_gori = NULL;
63 // Invoke range_of_expr on EXPR.
65 bool
66 fur_source::get_operand (vrange &r, tree expr)
68 return m_query->range_of_expr (r, expr);
71 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
72 // range_query to get the range on the edge.
74 bool
75 fur_source::get_phi_operand (vrange &r, tree expr, edge e)
77 return m_query->range_on_edge (r, e, expr);
80 // Default is no relation.
82 relation_kind
83 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
84 tree op2 ATTRIBUTE_UNUSED)
86 return VREL_VARYING;
89 // Default registers nothing.
91 void
92 fur_source::register_relation (gimple *s ATTRIBUTE_UNUSED,
93 relation_kind k ATTRIBUTE_UNUSED,
94 tree op1 ATTRIBUTE_UNUSED,
95 tree op2 ATTRIBUTE_UNUSED)
99 // Default registers nothing.
101 void
102 fur_source::register_relation (edge e ATTRIBUTE_UNUSED,
103 relation_kind k ATTRIBUTE_UNUSED,
104 tree op1 ATTRIBUTE_UNUSED,
105 tree op2 ATTRIBUTE_UNUSED)
109 // This version of fur_source will pick a range up off an edge.
111 class fur_edge : public fur_source
113 public:
114 fur_edge (edge e, range_query *q = NULL);
115 virtual bool get_operand (vrange &r, tree expr) override;
116 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
117 private:
118 edge m_edge;
121 // Instantiate an edge based fur_source.
123 inline
124 fur_edge::fur_edge (edge e, range_query *q) : fur_source (q)
126 m_edge = e;
129 // Get the value of EXPR on edge m_edge.
131 bool
132 fur_edge::get_operand (vrange &r, tree expr)
134 return m_query->range_on_edge (r, m_edge, expr);
137 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
138 // range_query to get the range on the edge.
140 bool
141 fur_edge::get_phi_operand (vrange &r, tree expr, edge e)
143 // Edge to edge recalculations not supported yet, until we sort it out.
144 gcc_checking_assert (e == m_edge);
145 return m_query->range_on_edge (r, e, expr);
148 // Instantiate a stmt based fur_source.
150 fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
152 m_stmt = s;
155 // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
157 bool
158 fur_stmt::get_operand (vrange &r, tree expr)
160 return m_query->range_of_expr (r, expr, m_stmt);
163 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
164 // range_query to get the range on the edge.
166 bool
167 fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
169 // Pick up the range of expr from edge E.
170 fur_edge e_src (e, m_query);
171 return e_src.get_operand (r, expr);
174 // Return relation based from m_stmt.
176 relation_kind
177 fur_stmt::query_relation (tree op1, tree op2)
179 return m_query->query_relation (m_stmt, op1, op2);
182 // Instantiate a stmt based fur_source with a GORI object.
185 fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
186 : fur_stmt (s, q)
188 gcc_checking_assert (gori);
189 m_gori = gori;
190 // Set relations if there is an oracle in the range_query.
191 // This will enable registering of relationships as they are discovered.
192 m_oracle = q->oracle ();
196 // Register a relation on a stmt if there is an oracle.
198 void
199 fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
201 if (m_oracle)
202 m_oracle->register_stmt (s, k, op1, op2);
205 // Register a relation on an edge if there is an oracle.
207 void
208 fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
210 if (m_oracle)
211 m_oracle->register_edge (e, k, op1, op2);
214 // This version of fur_source will pick a range up from a list of ranges
215 // supplied by the caller.
217 class fur_list : public fur_source
219 public:
220 fur_list (vrange &r1, range_query *q = NULL);
221 fur_list (vrange &r1, vrange &r2, range_query *q = NULL);
222 fur_list (unsigned num, vrange **list, range_query *q = NULL);
223 virtual bool get_operand (vrange &r, tree expr) override;
224 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
225 private:
226 vrange *m_local[2];
227 vrange **m_list;
228 unsigned m_index;
229 unsigned m_limit;
232 // One range supplied for unary operations.
234 fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
236 m_list = m_local;
237 m_index = 0;
238 m_limit = 1;
239 m_local[0] = &r1;
242 // Two ranges supplied for binary operations.
244 fur_list::fur_list (vrange &r1, vrange &r2, range_query *q) : fur_source (q)
246 m_list = m_local;
247 m_index = 0;
248 m_limit = 2;
249 m_local[0] = &r1;
250 m_local[1] = &r2;
253 // Arbitrary number of ranges in a vector.
255 fur_list::fur_list (unsigned num, vrange **list, range_query *q)
256 : fur_source (q)
258 m_list = list;
259 m_index = 0;
260 m_limit = num;
263 // Get the next operand from the vector, ensure types are compatible.
265 bool
266 fur_list::get_operand (vrange &r, tree expr)
268 // Do not use the vector for non-ssa-names, or if it has been emptied.
269 if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
270 return m_query->range_of_expr (r, expr);
271 r = *m_list[m_index++];
272 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
273 return true;
276 // This will simply pick the next operand from the vector.
277 bool
278 fur_list::get_phi_operand (vrange &r, tree expr, edge e ATTRIBUTE_UNUSED)
280 return get_operand (r, expr);
283 // Fold stmt S into range R using R1 as the first operand.
285 bool
286 fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
288 fold_using_range f;
289 fur_list src (r1, q);
290 return f.fold_stmt (r, s, src);
293 // Fold stmt S into range R using R1 and R2 as the first two operands.
295 bool
296 fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2, range_query *q)
298 fold_using_range f;
299 fur_list src (r1, r2, q);
300 return f.fold_stmt (r, s, src);
303 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
304 // operands encountered.
306 bool
307 fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector,
308 range_query *q)
310 fold_using_range f;
311 fur_list src (num_elements, vector, q);
312 return f.fold_stmt (r, s, src);
315 // Fold stmt S into range R using range query Q.
317 bool
318 fold_range (vrange &r, gimple *s, range_query *q)
320 fold_using_range f;
321 fur_stmt src (s, q);
322 return f.fold_stmt (r, s, src);
325 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
327 bool
328 fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
330 fold_using_range f;
331 fur_edge src (on_edge, q);
332 return f.fold_stmt (r, s, src);
335 // Provide a fur_source which can be used to determine any relations on
336 // a statement. It manages the callback from fold_using_ranges to determine
337 // a relation_trio for a statement.
339 class fur_relation : public fur_stmt
341 public:
342 fur_relation (gimple *s, range_query *q = NULL);
343 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
344 tree op2);
345 virtual void register_relation (edge e, relation_kind k, tree op1,
346 tree op2);
347 relation_trio trio() const;
348 private:
349 relation_kind def_op1, def_op2, op1_op2;
352 fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
354 def_op1 = def_op2 = op1_op2 = VREL_VARYING;
357 // Construct a trio from what is known.
359 relation_trio
360 fur_relation::trio () const
362 return relation_trio (def_op1, def_op2, op1_op2);
365 // Don't support edges, but avoid a compiler warning by providing the routine.
367 void
368 fur_relation::register_relation (edge, relation_kind, tree, tree)
372 // Register relation K between OP1 and OP2 on STMT.
374 void
375 fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
376 tree op2)
378 tree lhs = gimple_get_lhs (stmt);
379 tree a1 = NULL_TREE;
380 tree a2 = NULL_TREE;
381 switch (gimple_code (stmt))
383 case GIMPLE_COND:
384 a1 = gimple_cond_lhs (stmt);
385 a2 = gimple_cond_rhs (stmt);
386 break;
387 case GIMPLE_ASSIGN:
388 a1 = gimple_assign_rhs1 (stmt);
389 if (gimple_num_ops (stmt) >= 3)
390 a2 = gimple_assign_rhs2 (stmt);
391 break;
392 default:
393 break;
395 // STMT is of the form LHS = A1 op A2, now map the relation to these
396 // operands, if possible.
397 if (op1 == lhs)
399 if (op2 == a1)
400 def_op1 = k;
401 else if (op2 == a2)
402 def_op2 = k;
404 else if (op2 == lhs)
406 if (op1 == a1)
407 def_op1 = relation_swap (k);
408 else if (op1 == a2)
409 def_op2 = relation_swap (k);
411 else
413 if (op1 == a1 && op2 == a2)
414 op1_op2 = k;
415 else if (op2 == a1 && op1 == a2)
416 op1_op2 = relation_swap (k);
420 // Return the relation trio for stmt S using query Q.
422 relation_trio
423 fold_relations (gimple *s, range_query *q)
425 fold_using_range f;
426 fur_relation src (s, q);
427 tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
428 if (lhs)
430 Value_Range vr(TREE_TYPE (lhs));
431 if (f.fold_stmt (vr, s, src))
432 return src.trio ();
434 return TRIO_VARYING;
437 // -------------------------------------------------------------------------
439 // Adjust the range for a pointer difference where the operands came
440 // from a memchr.
442 // This notices the following sequence:
444 // def = __builtin_memchr (arg, 0, sz)
445 // n = def - arg
447 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
449 static void
450 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
452 tree op0 = gimple_assign_rhs1 (diff_stmt);
453 tree op1 = gimple_assign_rhs2 (diff_stmt);
454 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
455 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
456 gimple *call;
458 if (TREE_CODE (op0) == SSA_NAME
459 && TREE_CODE (op1) == SSA_NAME
460 && (call = SSA_NAME_DEF_STMT (op0))
461 && is_gimple_call (call)
462 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
463 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
464 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
465 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
466 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
467 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
468 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
469 && integer_zerop (gimple_call_arg (call, 1)))
471 wide_int maxm1 = irange_val_max (ptrdiff_type_node) - 1;
472 res.intersect (int_range<2> (ptrdiff_type_node,
473 wi::zero (TYPE_PRECISION (ptrdiff_type_node)),
474 maxm1));
478 // Adjust the range for an IMAGPART_EXPR.
480 static void
481 adjust_imagpart_expr (vrange &res, const gimple *stmt)
483 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
485 if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
486 return;
488 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
489 if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
491 switch (gimple_call_internal_fn (def_stmt))
493 case IFN_ADD_OVERFLOW:
494 case IFN_SUB_OVERFLOW:
495 case IFN_MUL_OVERFLOW:
496 case IFN_UADDC:
497 case IFN_USUBC:
498 case IFN_ATOMIC_COMPARE_EXCHANGE:
500 int_range<2> r;
501 r.set_varying (boolean_type_node);
502 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
503 range_cast (r, type);
504 res.intersect (r);
506 default:
507 break;
509 return;
511 if (is_gimple_assign (def_stmt)
512 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
514 tree cst = gimple_assign_rhs1 (def_stmt);
515 if (TREE_CODE (cst) == COMPLEX_CST
516 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
518 wide_int w = wi::to_wide (TREE_IMAGPART (cst));
519 int_range<1> imag (TREE_TYPE (TREE_IMAGPART (cst)), w, w);
520 res.intersect (imag);
525 // Adjust the range for a REALPART_EXPR.
527 static void
528 adjust_realpart_expr (vrange &res, const gimple *stmt)
530 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
532 if (TREE_CODE (name) != SSA_NAME)
533 return;
535 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
536 if (!SSA_NAME_DEF_STMT (name))
537 return;
539 if (is_gimple_assign (def_stmt)
540 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
542 tree cst = gimple_assign_rhs1 (def_stmt);
543 if (TREE_CODE (cst) == COMPLEX_CST
544 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
546 wide_int imag = wi::to_wide (TREE_REALPART (cst));
547 int_range<2> tmp (TREE_TYPE (TREE_REALPART (cst)), imag, imag);
548 res.intersect (tmp);
553 // This function looks for situations when walking the use/def chains
554 // may provide additional contextual range information not exposed on
555 // this statement.
557 static void
558 gimple_range_adjustment (vrange &res, const gimple *stmt)
560 switch (gimple_expr_code (stmt))
562 case POINTER_DIFF_EXPR:
563 adjust_pointer_diff_expr (as_a <irange> (res), stmt);
564 return;
566 case IMAGPART_EXPR:
567 adjust_imagpart_expr (res, stmt);
568 return;
570 case REALPART_EXPR:
571 adjust_realpart_expr (res, stmt);
572 return;
574 default:
575 break;
579 // Calculate a range for statement S and return it in R. If NAME is provided it
580 // represents the SSA_NAME on the LHS of the statement. It is only required
581 // if there is more than one lhs/output. If a range cannot
582 // be calculated, return false.
584 bool
585 fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
587 bool res = false;
588 // If name and S are specified, make sure it is an LHS of S.
589 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
590 name == gimple_get_lhs (s));
592 if (!name)
593 name = gimple_get_lhs (s);
595 // Process addresses.
596 if (gimple_code (s) == GIMPLE_ASSIGN
597 && gimple_assign_rhs_code (s) == ADDR_EXPR)
598 return range_of_address (as_a <irange> (r), s, src);
600 gimple_range_op_handler handler (s);
601 if (handler)
602 res = range_of_range_op (r, handler, src);
603 else if (is_a<gphi *>(s))
604 res = range_of_phi (r, as_a<gphi *> (s), src);
605 else if (is_a<gcall *>(s))
606 res = range_of_call (r, as_a<gcall *> (s), src);
607 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
608 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
610 // If the result is varying, check for basic nonnegativeness.
611 // Specifically this helps for now with strict enum in cases like
612 // g++.dg/warn/pr33738.C.
613 bool so_p;
614 if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
615 && gimple_stmt_nonnegative_warnv_p (s, &so_p))
616 r.set_nonnegative (r.type ());
618 if (!res)
620 // If no name specified or range is unsupported, bail.
621 if (!name || !gimple_range_ssa_p (name))
622 return false;
623 // We don't understand the stmt, so return the global range.
624 gimple_range_global (r, name);
625 return true;
628 if (r.undefined_p ())
629 return true;
631 // We sometimes get compatible types copied from operands, make sure
632 // the correct type is being returned.
633 if (name && TREE_TYPE (name) != r.type ())
635 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
636 range_cast (r, TREE_TYPE (name));
638 return true;
641 // Calculate a range for range_op statement S and return it in R. If any
642 // If a range cannot be calculated, return false.
644 bool
645 fold_using_range::range_of_range_op (vrange &r,
646 gimple_range_op_handler &handler,
647 fur_source &src)
649 gcc_checking_assert (handler);
650 gimple *s = handler.stmt ();
651 tree type = gimple_range_type (s);
652 if (!type)
653 return false;
655 tree lhs = handler.lhs ();
656 tree op1 = handler.operand1 ();
657 tree op2 = handler.operand2 ();
659 // Certain types of builtin functions may have no arguments.
660 if (!op1)
662 Value_Range r1 (type);
663 if (!handler.fold_range (r, type, r1, r1))
664 r.set_varying (type);
665 return true;
668 Value_Range range1 (TREE_TYPE (op1));
669 Value_Range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
671 if (src.get_operand (range1, op1))
673 if (!op2)
675 // Fold range, and register any dependency if available.
676 Value_Range r2 (type);
677 r2.set_varying (type);
678 if (!handler.fold_range (r, type, range1, r2))
679 r.set_varying (type);
680 if (lhs && gimple_range_ssa_p (op1))
682 if (src.gori ())
683 src.gori ()->register_dependency (lhs, op1);
684 relation_kind rel;
685 rel = handler.lhs_op1_relation (r, range1, range1);
686 if (rel != VREL_VARYING)
687 src.register_relation (s, rel, lhs, op1);
690 else if (src.get_operand (range2, op2))
692 relation_kind rel = src.query_relation (op1, op2);
693 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
695 fprintf (dump_file, " folding with relation ");
696 print_generic_expr (dump_file, op1, TDF_SLIM);
697 print_relation (dump_file, rel);
698 print_generic_expr (dump_file, op2, TDF_SLIM);
699 fputc ('\n', dump_file);
701 // Fold range, and register any dependency if available.
702 if (!handler.fold_range (r, type, range1, range2,
703 relation_trio::op1_op2 (rel)))
704 r.set_varying (type);
705 if (irange::supports_p (type))
706 relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
707 if (lhs)
709 if (src.gori ())
711 src.gori ()->register_dependency (lhs, op1);
712 src.gori ()->register_dependency (lhs, op2);
714 if (gimple_range_ssa_p (op1))
716 rel = handler.lhs_op1_relation (r, range1, range2, rel);
717 if (rel != VREL_VARYING)
718 src.register_relation (s, rel, lhs, op1);
720 if (gimple_range_ssa_p (op2))
722 rel = handler.lhs_op2_relation (r, range1, range2, rel);
723 if (rel != VREL_VARYING)
724 src.register_relation (s, rel, lhs, op2);
727 // Check for an existing BB, as we maybe asked to fold an
728 // artificial statement not in the CFG.
729 else if (is_a<gcond *> (s) && gimple_bb (s))
731 basic_block bb = gimple_bb (s);
732 edge e0 = EDGE_SUCC (bb, 0);
733 edge e1 = EDGE_SUCC (bb, 1);
735 if (!single_pred_p (e0->dest))
736 e0 = NULL;
737 if (!single_pred_p (e1->dest))
738 e1 = NULL;
739 src.register_outgoing_edges (as_a<gcond *> (s),
740 as_a <irange> (r), e0, e1);
743 else
744 r.set_varying (type);
746 else
747 r.set_varying (type);
748 // Make certain range-op adjustments that aren't handled any other way.
749 gimple_range_adjustment (r, s);
750 return true;
753 // Calculate the range of an assignment containing an ADDR_EXPR.
754 // Return the range in R.
755 // If a range cannot be calculated, set it to VARYING and return true.
757 bool
758 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
760 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
761 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
763 bool strict_overflow_p;
764 tree expr = gimple_assign_rhs1 (stmt);
765 poly_int64 bitsize, bitpos;
766 tree offset;
767 machine_mode mode;
768 int unsignedp, reversep, volatilep;
769 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
770 &bitpos, &offset, &mode, &unsignedp,
771 &reversep, &volatilep);
774 if (base != NULL_TREE
775 && TREE_CODE (base) == MEM_REF
776 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
778 tree ssa = TREE_OPERAND (base, 0);
779 tree lhs = gimple_get_lhs (stmt);
780 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
781 src.gori ()->register_dependency (lhs, ssa);
782 src.get_operand (r, ssa);
783 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
785 poly_offset_int off = 0;
786 bool off_cst = false;
787 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
789 off = mem_ref_offset (base);
790 if (offset)
791 off += poly_offset_int::from (wi::to_poly_wide (offset),
792 SIGNED);
793 off <<= LOG2_BITS_PER_UNIT;
794 off += bitpos;
795 off_cst = true;
797 /* If &X->a is equal to X, the range of X is the result. */
798 if (off_cst && known_eq (off, 0))
799 return true;
800 else if (flag_delete_null_pointer_checks
801 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
803 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
804 allow going from non-NULL pointer to NULL. */
805 if (r.undefined_p ()
806 || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
808 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
809 using POINTER_PLUS_EXPR if off_cst and just fall back to
810 this. */
811 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
812 return true;
815 /* If MEM_REF has a "positive" offset, consider it non-NULL
816 always, for -fdelete-null-pointer-checks also "negative"
817 ones. Punt for unknown offsets (e.g. variable ones). */
818 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
819 && off_cst
820 && known_ne (off, 0)
821 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
823 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
824 return true;
826 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
827 return true;
830 // Handle "= &a".
831 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
833 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
834 return true;
837 // Otherwise return varying.
838 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
839 return true;
842 // Calculate a range for phi statement S and return it in R.
843 // If a range cannot be calculated, return false.
845 bool
846 fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
848 tree phi_def = gimple_phi_result (phi);
849 tree type = gimple_range_type (phi);
850 Value_Range arg_range (type);
851 Value_Range equiv_range (type);
852 unsigned x;
854 if (!type)
855 return false;
857 // Track if all executable arguments are the same.
858 tree single_arg = NULL_TREE;
859 bool seen_arg = false;
861 // Start with an empty range, unioning in each argument's range.
862 r.set_undefined ();
863 for (x = 0; x < gimple_phi_num_args (phi); x++)
865 tree arg = gimple_phi_arg_def (phi, x);
866 // An argument that is the same as the def provides no new range.
867 if (arg == phi_def)
868 continue;
870 edge e = gimple_phi_arg_edge (phi, x);
872 // Get the range of the argument on its edge.
873 src.get_phi_operand (arg_range, arg, e);
875 if (!arg_range.undefined_p ())
877 // Register potential dependencies for stale value tracking.
878 // Likewise, if the incoming PHI argument is equivalent to this
879 // PHI definition, it provides no new info. Accumulate these ranges
880 // in case all arguments are equivalences.
881 if (src.query ()->query_relation (e, arg, phi_def, false) == VREL_EQ)
882 equiv_range.union_(arg_range);
883 else
884 r.union_ (arg_range);
886 if (gimple_range_ssa_p (arg) && src.gori ())
887 src.gori ()->register_dependency (phi_def, arg);
890 // Track if all arguments are the same.
891 if (!seen_arg)
893 seen_arg = true;
894 single_arg = arg;
896 else if (single_arg != arg)
897 single_arg = NULL_TREE;
899 // Once the value reaches varying, stop looking.
900 if (r.varying_p () && single_arg == NULL_TREE)
901 break;
904 // If all arguments were equivalences, use the equivalence ranges as no
905 // arguments were processed.
906 if (r.undefined_p () && !equiv_range.undefined_p ())
907 r = equiv_range;
909 // If the PHI boils down to a single effective argument, look at it.
910 if (single_arg)
912 // Symbolic arguments can be equivalences.
913 if (gimple_range_ssa_p (single_arg))
915 // Only allow the equivalence if the PHI definition does not
916 // dominate any incoming edge for SINGLE_ARG.
917 // See PR 108139 and 109462.
918 basic_block bb = gimple_bb (phi);
919 if (!dom_info_available_p (CDI_DOMINATORS))
920 single_arg = NULL;
921 else
922 for (x = 0; x < gimple_phi_num_args (phi); x++)
923 if (gimple_phi_arg_def (phi, x) == single_arg
924 && dominated_by_p (CDI_DOMINATORS,
925 gimple_phi_arg_edge (phi, x)->src,
926 bb))
928 single_arg = NULL;
929 break;
931 if (single_arg)
932 src.register_relation (phi, VREL_EQ, phi_def, single_arg);
934 else if (src.get_operand (arg_range, single_arg)
935 && arg_range.singleton_p ())
937 // Numerical arguments that are a constant can be returned as
938 // the constant. This can help fold later cases where even this
939 // constant might have been UNDEFINED via an unreachable edge.
940 r = arg_range;
941 return true;
945 // If PHI analysis is available, see if there is an iniital range.
946 if (phi_analysis_available_p ()
947 && irange::supports_p (TREE_TYPE (phi_def)))
949 phi_group *g = (phi_analysis())[phi_def];
950 if (g && !(g->range ().varying_p ()))
952 if (dump_file && (dump_flags & TDF_DETAILS))
954 fprintf (dump_file, "PHI GROUP query for ");
955 print_generic_expr (dump_file, phi_def, TDF_SLIM);
956 fprintf (dump_file, " found : ");
957 g->range ().dump (dump_file);
958 fprintf (dump_file, " and adjusted original range from :");
959 r.dump (dump_file);
961 r.intersect (g->range ());
962 if (dump_file && (dump_flags & TDF_DETAILS))
964 fprintf (dump_file, " to :");
965 r.dump (dump_file);
966 fprintf (dump_file, "\n");
971 // If SCEV is available, query if this PHI has any known values.
972 if (scev_initialized_p ()
973 && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
975 class loop *l = loop_containing_stmt (phi);
976 if (l && loop_outer (l))
978 Value_Range loop_range (type);
979 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
980 if (!loop_range.varying_p ())
982 if (dump_file && (dump_flags & TDF_DETAILS))
984 fprintf (dump_file, "Loops range found for ");
985 print_generic_expr (dump_file, phi_def, TDF_SLIM);
986 fprintf (dump_file, ": ");
987 loop_range.dump (dump_file);
988 fprintf (dump_file, " and calculated range :");
989 r.dump (dump_file);
990 fprintf (dump_file, "\n");
992 r.intersect (loop_range);
997 return true;
1000 // Calculate a range for call statement S and return it in R.
1001 // If a range cannot be calculated, return false.
1003 bool
1004 fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1006 tree type = gimple_range_type (call);
1007 if (!type)
1008 return false;
1010 tree lhs = gimple_call_lhs (call);
1011 bool strict_overflow_p;
1013 if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1014 r.set_nonnegative (type);
1015 else if (gimple_call_nonnull_result_p (call)
1016 || gimple_call_nonnull_arg (call))
1017 r.set_nonzero (type);
1018 else
1019 r.set_varying (type);
1021 tree callee = gimple_call_fndecl (call);
1022 if (callee
1023 && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1025 Value_Range val;
1026 if (ipa_return_value_range (val, callee))
1028 r.intersect (val);
1029 if (dump_file && (dump_flags & TDF_DETAILS))
1031 fprintf (dump_file, "Using return value range of ");
1032 print_generic_expr (dump_file, callee, TDF_SLIM);
1033 fprintf (dump_file, ": ");
1034 val.dump (dump_file);
1035 fprintf (dump_file, "\n");
1040 // If there is an LHS, intersect that with what is known.
1041 if (lhs)
1043 Value_Range def (TREE_TYPE (lhs));
1044 gimple_range_global (def, lhs);
1045 r.intersect (def);
1047 return true;
1050 // Calculate a range for COND_EXPR statement S and return it in R.
1051 // If a range cannot be calculated, return false.
1053 bool
1054 fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1056 tree cond = gimple_assign_rhs1 (s);
1057 tree op1 = gimple_assign_rhs2 (s);
1058 tree op2 = gimple_assign_rhs3 (s);
1060 tree type = gimple_range_type (s);
1061 if (!type)
1062 return false;
1064 Value_Range range1 (TREE_TYPE (op1));
1065 Value_Range range2 (TREE_TYPE (op2));
1066 Value_Range cond_range (TREE_TYPE (cond));
1067 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1068 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1069 src.get_operand (cond_range, cond);
1070 src.get_operand (range1, op1);
1071 src.get_operand (range2, op2);
1073 // Try to see if there is a dependence between the COND and either operand
1074 if (src.gori ())
1075 if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1076 if (dump_file && (dump_flags & TDF_DETAILS))
1078 fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1079 range1.dump(dump_file);
1080 fprintf (dump_file, " and Range op2: ");
1081 range2.dump(dump_file);
1082 fprintf (dump_file, "\n");
1085 // If the condition is known, choose the appropriate expression.
1086 if (cond_range.singleton_p ())
1088 // False, pick second operand.
1089 if (cond_range.zero_p ())
1090 r = range2;
1091 else
1092 r = range1;
1094 else
1096 r = range1;
1097 r.union_ (range2);
1099 gcc_checking_assert (r.undefined_p ()
1100 || range_compatible_p (r.type (), type));
1101 return true;
1104 // If SCEV has any information about phi node NAME, return it as a range in R.
1106 void
1107 fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1108 class loop *l, gphi *phi,
1109 fur_source &src)
1111 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1112 if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1113 r.set_varying (TREE_TYPE (name));
1116 // -----------------------------------------------------------------------
1118 // Check if an && or || expression can be folded based on relations. ie
1119 // c_2 = a_6 > b_7
1120 // c_3 = a_6 < b_7
1121 // c_4 = c_2 && c_3
1122 // c_2 and c_3 can never be true at the same time,
1123 // Therefore c_4 can always resolve to false based purely on the relations.
1125 void
1126 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1127 fur_source &src, vrange &op1,
1128 vrange &op2)
1130 // No queries or already folded.
1131 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1132 return;
1134 // Only care about AND and OR expressions.
1135 enum tree_code code = gimple_expr_code (s);
1136 bool is_and = false;
1137 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1138 is_and = true;
1139 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1140 return;
1142 gimple_range_op_handler handler (s);
1143 tree lhs = handler.lhs ();
1144 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1145 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1147 // Deal with || and && only when there is a full set of symbolics.
1148 if (!lhs || !ssa1 || !ssa2
1149 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1150 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1151 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1152 return;
1154 // Now we know its a boolean AND or OR expression with boolean operands.
1155 // Ideally we search dependencies for common names, and see what pops out.
1156 // until then, simply try to resolve direct dependencies.
1158 gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1159 gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1161 gimple_range_op_handler handler1 (ssa1_stmt);
1162 gimple_range_op_handler handler2 (ssa2_stmt);
1164 // If either handler is not present, no relation can be found.
1165 if (!handler1 || !handler2)
1166 return;
1168 // Both stmts will need to have 2 ssa names in the stmt.
1169 tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1170 tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1171 tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1172 tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1174 if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1175 return;
1177 if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1178 return;
1180 // Make sure they are the same dependencies, and detect the order of the
1181 // relationship.
1182 bool reverse_op2 = true;
1183 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1184 reverse_op2 = false;
1185 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1186 return;
1188 int_range<2> bool_one = range_true ();
1189 relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1190 relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1191 if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1192 return;
1194 if (reverse_op2)
1195 relation2 = relation_negate (relation2);
1197 // x && y is false if the relation intersection of the true cases is NULL.
1198 if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1199 lhs_range = range_false (boolean_type_node);
1200 // x || y is true if the union of the true cases is NO-RELATION..
1201 // ie, one or the other being true covers the full range of possibilities.
1202 else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1203 lhs_range = bool_one;
1204 else
1205 return;
1207 range_cast (lhs_range, TREE_TYPE (lhs));
1208 if (dump_file && (dump_flags & TDF_DETAILS))
1210 fprintf (dump_file, " Relation adjustment: ");
1211 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1212 fprintf (dump_file, " and ");
1213 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1214 fprintf (dump_file, " combine to produce ");
1215 lhs_range.dump (dump_file);
1216 fputc ('\n', dump_file);
1219 return;
1222 // Register any outgoing edge relations from a conditional branch.
1224 void
1225 fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1226 edge e0, edge e1)
1228 int_range<2> e0_range, e1_range;
1229 tree name;
1230 basic_block bb = gimple_bb (s);
1232 gimple_range_op_handler handler (s);
1233 if (!handler)
1234 return;
1236 if (e0)
1238 // If this edge is never taken, ignore it.
1239 gcond_edge_range (e0_range, e0);
1240 e0_range.intersect (lhs_range);
1241 if (e0_range.undefined_p ())
1242 e0 = NULL;
1245 if (e1)
1247 // If this edge is never taken, ignore it.
1248 gcond_edge_range (e1_range, e1);
1249 e1_range.intersect (lhs_range);
1250 if (e1_range.undefined_p ())
1251 e1 = NULL;
1254 if (!e0 && !e1)
1255 return;
1257 // First, register the gcond itself. This will catch statements like
1258 // if (a_2 < b_5)
1259 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1260 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1261 Value_Range r1,r2;
1262 if (ssa1 && ssa2)
1264 r1.set_varying (TREE_TYPE (ssa1));
1265 r2.set_varying (TREE_TYPE (ssa2));
1266 if (e0)
1268 relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1269 if (relation != VREL_VARYING)
1270 register_relation (e0, relation, ssa1, ssa2);
1272 if (e1)
1274 relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1275 if (relation != VREL_VARYING)
1276 register_relation (e1, relation, ssa1, ssa2);
1280 // Outgoing relations of GORI exports require a gori engine.
1281 if (!gori ())
1282 return;
1284 // Now look for other relations in the exports. This will find stmts
1285 // leading to the condition such as:
1286 // c_2 = a_4 < b_7
1287 // if (c_2)
1288 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
1290 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1291 continue;
1292 gimple *stmt = SSA_NAME_DEF_STMT (name);
1293 gimple_range_op_handler handler (stmt);
1294 if (!handler)
1295 continue;
1296 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1297 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1298 Value_Range r (TREE_TYPE (name));
1299 if (ssa1 && ssa2)
1301 r1.set_varying (TREE_TYPE (ssa1));
1302 r2.set_varying (TREE_TYPE (ssa2));
1303 if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
1304 && r.singleton_p ())
1306 relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1307 if (relation != VREL_VARYING)
1308 register_relation (e0, relation, ssa1, ssa2);
1310 if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
1311 && r.singleton_p ())
1313 relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1314 if (relation != VREL_VARYING)
1315 register_relation (e1, relation, ssa1, ssa2);