c++: only cache constexpr calls that are constant exprs
[official-gcc.git] / gcc / gimple-range-fold.cc
blobd07246008f0f9ea39cf739367e4ac3ef80e7f801
1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2023 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 // Construct a fur_source, and set the m_query field.
49 fur_source::fur_source (range_query *q)
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;
60 // Invoke range_of_expr on EXPR.
62 bool
63 fur_source::get_operand (vrange &r, tree expr)
65 return m_query->range_of_expr (r, expr);
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.
71 bool
72 fur_source::get_phi_operand (vrange &r, tree expr, edge e)
74 return m_query->range_on_edge (r, e, expr);
77 // Default is no relation.
79 relation_kind
80 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
81 tree op2 ATTRIBUTE_UNUSED)
83 return VREL_VARYING;
86 // Default registers nothing.
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)
96 // Default registers nothing.
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)
106 // This version of fur_source will pick a range up off an edge.
108 class fur_edge : public fur_source
110 public:
111 fur_edge (edge e, range_query *q = NULL);
112 virtual bool get_operand (vrange &r, tree expr) override;
113 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
114 private:
115 edge m_edge;
118 // Instantiate an edge based fur_source.
120 inline
121 fur_edge::fur_edge (edge e, range_query *q) : fur_source (q)
123 m_edge = e;
126 // Get the value of EXPR on edge m_edge.
128 bool
129 fur_edge::get_operand (vrange &r, tree expr)
131 return m_query->range_on_edge (r, m_edge, expr);
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.
137 bool
138 fur_edge::get_phi_operand (vrange &r, tree expr, edge e)
140 // Edge to edge recalculations not supported yet, until we sort it out.
141 gcc_checking_assert (e == m_edge);
142 return m_query->range_on_edge (r, e, expr);
145 // Instantiate a stmt based fur_source.
147 fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
149 m_stmt = s;
152 // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
154 bool
155 fur_stmt::get_operand (vrange &r, tree expr)
157 return m_query->range_of_expr (r, expr, m_stmt);
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.
163 bool
164 fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
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);
171 // Return relation based from m_stmt.
173 relation_kind
174 fur_stmt::query_relation (tree op1, tree op2)
176 return m_query->query_relation (m_stmt, op1, op2);
179 // Instantiate a stmt based fur_source with a GORI object.
182 fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
183 : fur_stmt (s, q)
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 ();
193 // Register a relation on a stmt if there is an oracle.
195 void
196 fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
198 if (m_oracle)
199 m_oracle->register_stmt (s, k, op1, op2);
202 // Register a relation on an edge if there is an oracle.
204 void
205 fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
207 if (m_oracle)
208 m_oracle->register_edge (e, k, op1, op2);
211 // This version of fur_source will pick a range up from a list of ranges
212 // supplied by the caller.
214 class fur_list : public fur_source
216 public:
217 fur_list (vrange &r1, range_query *q = NULL);
218 fur_list (vrange &r1, vrange &r2, range_query *q = NULL);
219 fur_list (unsigned num, vrange **list, range_query *q = NULL);
220 virtual bool get_operand (vrange &r, tree expr) override;
221 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
222 private:
223 vrange *m_local[2];
224 vrange **m_list;
225 unsigned m_index;
226 unsigned m_limit;
229 // One range supplied for unary operations.
231 fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
233 m_list = m_local;
234 m_index = 0;
235 m_limit = 1;
236 m_local[0] = &r1;
239 // Two ranges supplied for binary operations.
241 fur_list::fur_list (vrange &r1, vrange &r2, range_query *q) : fur_source (q)
243 m_list = m_local;
244 m_index = 0;
245 m_limit = 2;
246 m_local[0] = &r1;
247 m_local[1] = &r2;
250 // Arbitrary number of ranges in a vector.
252 fur_list::fur_list (unsigned num, vrange **list, range_query *q)
253 : fur_source (q)
255 m_list = list;
256 m_index = 0;
257 m_limit = num;
260 // Get the next operand from the vector, ensure types are compatible.
262 bool
263 fur_list::get_operand (vrange &r, tree expr)
265 if (m_index >= m_limit)
266 return m_query->range_of_expr (r, expr);
267 r = *m_list[m_index++];
268 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
269 return true;
272 // This will simply pick the next operand from the vector.
273 bool
274 fur_list::get_phi_operand (vrange &r, tree expr, edge e ATTRIBUTE_UNUSED)
276 return get_operand (r, expr);
279 // Fold stmt S into range R using R1 as the first operand.
281 bool
282 fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
284 fold_using_range f;
285 fur_list src (r1, q);
286 return f.fold_stmt (r, s, src);
289 // Fold stmt S into range R using R1 and R2 as the first two operands.
291 bool
292 fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2, range_query *q)
294 fold_using_range f;
295 fur_list src (r1, r2, q);
296 return f.fold_stmt (r, s, src);
299 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
300 // operands encountered.
302 bool
303 fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector,
304 range_query *q)
306 fold_using_range f;
307 fur_list src (num_elements, vector, q);
308 return f.fold_stmt (r, s, src);
311 // Fold stmt S into range R using range query Q.
313 bool
314 fold_range (vrange &r, gimple *s, range_query *q)
316 fold_using_range f;
317 fur_stmt src (s, q);
318 return f.fold_stmt (r, s, src);
321 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
323 bool
324 fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
326 fold_using_range f;
327 fur_edge src (on_edge, q);
328 return f.fold_stmt (r, s, src);
331 // Provide a fur_source which can be used to determine any relations on
332 // a statement. It manages the callback from fold_using_ranges to determine
333 // a relation_trio for a statement.
335 class fur_relation : public fur_stmt
337 public:
338 fur_relation (gimple *s, range_query *q = NULL);
339 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
340 tree op2);
341 virtual void register_relation (edge e, relation_kind k, tree op1,
342 tree op2);
343 relation_trio trio() const;
344 private:
345 relation_kind def_op1, def_op2, op1_op2;
348 fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
350 def_op1 = def_op2 = op1_op2 = VREL_VARYING;
353 // Construct a trio from what is known.
355 relation_trio
356 fur_relation::trio () const
358 return relation_trio (def_op1, def_op2, op1_op2);
361 // Don't support edges, but avoid a compiler warning by providing the routine.
363 void
364 fur_relation::register_relation (edge, relation_kind, tree, tree)
368 // Register relation K between OP1 and OP2 on STMT.
370 void
371 fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
372 tree op2)
374 tree lhs = gimple_get_lhs (stmt);
375 tree a1 = NULL_TREE;
376 tree a2 = NULL_TREE;
377 switch (gimple_code (stmt))
379 case GIMPLE_COND:
380 a1 = gimple_cond_lhs (stmt);
381 a2 = gimple_cond_rhs (stmt);
382 break;
383 case GIMPLE_ASSIGN:
384 a1 = gimple_assign_rhs1 (stmt);
385 if (gimple_num_ops (stmt) >= 3)
386 a2 = gimple_assign_rhs2 (stmt);
387 break;
388 default:
389 break;
391 // STMT is of the form LHS = A1 op A2, now map the relation to these
392 // operands, if possible.
393 if (op1 == lhs)
395 if (op2 == a1)
396 def_op1 = k;
397 else if (op2 == a2)
398 def_op2 = k;
400 else if (op2 == lhs)
402 if (op1 == a1)
403 def_op1 = relation_swap (k);
404 else if (op1 == a2)
405 def_op2 = relation_swap (k);
407 else
409 if (op1 == a1 && op2 == a2)
410 op1_op2 = k;
411 else if (op2 == a1 && op1 == a2)
412 op1_op2 = relation_swap (k);
416 // Return the relation trio for stmt S using query Q.
418 relation_trio
419 fold_relations (gimple *s, range_query *q)
421 fold_using_range f;
422 fur_relation src (s, q);
423 tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
424 if (lhs)
426 Value_Range vr(TREE_TYPE (lhs));
427 if (f.fold_stmt (vr, s, src))
428 return src.trio ();
430 return TRIO_VARYING;
433 // -------------------------------------------------------------------------
435 // Adjust the range for a pointer difference where the operands came
436 // from a memchr.
438 // This notices the following sequence:
440 // def = __builtin_memchr (arg, 0, sz)
441 // n = def - arg
443 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
445 static void
446 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
448 tree op0 = gimple_assign_rhs1 (diff_stmt);
449 tree op1 = gimple_assign_rhs2 (diff_stmt);
450 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
451 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
452 gimple *call;
454 if (TREE_CODE (op0) == SSA_NAME
455 && TREE_CODE (op1) == SSA_NAME
456 && (call = SSA_NAME_DEF_STMT (op0))
457 && is_gimple_call (call)
458 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
459 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
460 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
461 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
462 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
463 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
464 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
465 && integer_zerop (gimple_call_arg (call, 1)))
467 wide_int maxm1 = irange_val_max (ptrdiff_type_node) - 1;
468 res.intersect (int_range<2> (ptrdiff_type_node,
469 wi::zero (TYPE_PRECISION (ptrdiff_type_node)),
470 maxm1));
474 // Adjust the range for an IMAGPART_EXPR.
476 static void
477 adjust_imagpart_expr (vrange &res, const gimple *stmt)
479 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
481 if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
482 return;
484 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
485 if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
487 switch (gimple_call_internal_fn (def_stmt))
489 case IFN_ADD_OVERFLOW:
490 case IFN_SUB_OVERFLOW:
491 case IFN_MUL_OVERFLOW:
492 case IFN_UADDC:
493 case IFN_USUBC:
494 case IFN_ATOMIC_COMPARE_EXCHANGE:
496 int_range<2> r;
497 r.set_varying (boolean_type_node);
498 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
499 range_cast (r, type);
500 res.intersect (r);
502 default:
503 break;
505 return;
507 if (is_gimple_assign (def_stmt)
508 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
510 tree cst = gimple_assign_rhs1 (def_stmt);
511 if (TREE_CODE (cst) == COMPLEX_CST
512 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
514 wide_int w = wi::to_wide (TREE_IMAGPART (cst));
515 int_range<1> imag (TREE_TYPE (TREE_IMAGPART (cst)), w, w);
516 res.intersect (imag);
521 // Adjust the range for a REALPART_EXPR.
523 static void
524 adjust_realpart_expr (vrange &res, const gimple *stmt)
526 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
528 if (TREE_CODE (name) != SSA_NAME)
529 return;
531 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
532 if (!SSA_NAME_DEF_STMT (name))
533 return;
535 if (is_gimple_assign (def_stmt)
536 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
538 tree cst = gimple_assign_rhs1 (def_stmt);
539 if (TREE_CODE (cst) == COMPLEX_CST
540 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
542 wide_int imag = wi::to_wide (TREE_REALPART (cst));
543 int_range<2> tmp (TREE_TYPE (TREE_REALPART (cst)), imag, imag);
544 res.intersect (tmp);
549 // This function looks for situations when walking the use/def chains
550 // may provide additional contextual range information not exposed on
551 // this statement.
553 static void
554 gimple_range_adjustment (vrange &res, const gimple *stmt)
556 switch (gimple_expr_code (stmt))
558 case POINTER_DIFF_EXPR:
559 adjust_pointer_diff_expr (as_a <irange> (res), stmt);
560 return;
562 case IMAGPART_EXPR:
563 adjust_imagpart_expr (res, stmt);
564 return;
566 case REALPART_EXPR:
567 adjust_realpart_expr (res, stmt);
568 return;
570 default:
571 break;
575 // Calculate a range for statement S and return it in R. If NAME is provided it
576 // represents the SSA_NAME on the LHS of the statement. It is only required
577 // if there is more than one lhs/output. If a range cannot
578 // be calculated, return false.
580 bool
581 fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
583 bool res = false;
584 // If name and S are specified, make sure it is an LHS of S.
585 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
586 name == gimple_get_lhs (s));
588 if (!name)
589 name = gimple_get_lhs (s);
591 // Process addresses.
592 if (gimple_code (s) == GIMPLE_ASSIGN
593 && gimple_assign_rhs_code (s) == ADDR_EXPR)
594 return range_of_address (as_a <irange> (r), s, src);
596 gimple_range_op_handler handler (s);
597 if (handler)
598 res = range_of_range_op (r, handler, src);
599 else if (is_a<gphi *>(s))
600 res = range_of_phi (r, as_a<gphi *> (s), src);
601 else if (is_a<gcall *>(s))
602 res = range_of_call (r, as_a<gcall *> (s), src);
603 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
604 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
606 // If the result is varying, check for basic nonnegativeness.
607 // Specifically this helps for now with strict enum in cases like
608 // g++.dg/warn/pr33738.C.
609 bool so_p;
610 if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
611 && gimple_stmt_nonnegative_warnv_p (s, &so_p))
612 r.set_nonnegative (r.type ());
614 if (!res)
616 // If no name specified or range is unsupported, bail.
617 if (!name || !gimple_range_ssa_p (name))
618 return false;
619 // We don't understand the stmt, so return the global range.
620 gimple_range_global (r, name);
621 return true;
624 if (r.undefined_p ())
625 return true;
627 // We sometimes get compatible types copied from operands, make sure
628 // the correct type is being returned.
629 if (name && TREE_TYPE (name) != r.type ())
631 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
632 range_cast (r, TREE_TYPE (name));
634 return true;
637 // Calculate a range for range_op statement S and return it in R. If any
638 // If a range cannot be calculated, return false.
640 bool
641 fold_using_range::range_of_range_op (vrange &r,
642 gimple_range_op_handler &handler,
643 fur_source &src)
645 gcc_checking_assert (handler);
646 gimple *s = handler.stmt ();
647 tree type = gimple_range_type (s);
648 if (!type)
649 return false;
651 tree lhs = handler.lhs ();
652 tree op1 = handler.operand1 ();
653 tree op2 = handler.operand2 ();
655 // Certain types of builtin functions may have no arguments.
656 if (!op1)
658 Value_Range r1 (type);
659 if (!handler.fold_range (r, type, r1, r1))
660 r.set_varying (type);
661 return true;
664 Value_Range range1 (TREE_TYPE (op1));
665 Value_Range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
667 if (src.get_operand (range1, op1))
669 if (!op2)
671 // Fold range, and register any dependency if available.
672 Value_Range r2 (type);
673 r2.set_varying (type);
674 if (!handler.fold_range (r, type, range1, r2))
675 r.set_varying (type);
676 if (lhs && gimple_range_ssa_p (op1))
678 if (src.gori ())
679 src.gori ()->register_dependency (lhs, op1);
680 relation_kind rel;
681 rel = handler.lhs_op1_relation (r, range1, range1);
682 if (rel != VREL_VARYING)
683 src.register_relation (s, rel, lhs, op1);
686 else if (src.get_operand (range2, op2))
688 relation_kind rel = src.query_relation (op1, op2);
689 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
691 fprintf (dump_file, " folding with relation ");
692 print_generic_expr (dump_file, op1, TDF_SLIM);
693 print_relation (dump_file, rel);
694 print_generic_expr (dump_file, op2, TDF_SLIM);
695 fputc ('\n', dump_file);
697 // Fold range, and register any dependency if available.
698 if (!handler.fold_range (r, type, range1, range2,
699 relation_trio::op1_op2 (rel)))
700 r.set_varying (type);
701 if (irange::supports_p (type))
702 relation_fold_and_or (as_a <irange> (r), s, src);
703 if (lhs)
705 if (src.gori ())
707 src.gori ()->register_dependency (lhs, op1);
708 src.gori ()->register_dependency (lhs, op2);
710 if (gimple_range_ssa_p (op1))
712 rel = handler.lhs_op1_relation (r, range1, range2, rel);
713 if (rel != VREL_VARYING)
714 src.register_relation (s, rel, lhs, op1);
716 if (gimple_range_ssa_p (op2))
718 rel = handler.lhs_op2_relation (r, range1, range2, rel);
719 if (rel != VREL_VARYING)
720 src.register_relation (s, rel, lhs, op2);
723 // Check for an existing BB, as we maybe asked to fold an
724 // artificial statement not in the CFG.
725 else if (is_a<gcond *> (s) && gimple_bb (s))
727 basic_block bb = gimple_bb (s);
728 edge e0 = EDGE_SUCC (bb, 0);
729 edge e1 = EDGE_SUCC (bb, 1);
731 if (!single_pred_p (e0->dest))
732 e0 = NULL;
733 if (!single_pred_p (e1->dest))
734 e1 = NULL;
735 src.register_outgoing_edges (as_a<gcond *> (s),
736 as_a <irange> (r), e0, e1);
739 else
740 r.set_varying (type);
742 else
743 r.set_varying (type);
744 // Make certain range-op adjustments that aren't handled any other way.
745 gimple_range_adjustment (r, s);
746 return true;
749 // Calculate the range of an assignment containing an ADDR_EXPR.
750 // Return the range in R.
751 // If a range cannot be calculated, set it to VARYING and return true.
753 bool
754 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
756 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
757 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
759 bool strict_overflow_p;
760 tree expr = gimple_assign_rhs1 (stmt);
761 poly_int64 bitsize, bitpos;
762 tree offset;
763 machine_mode mode;
764 int unsignedp, reversep, volatilep;
765 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
766 &bitpos, &offset, &mode, &unsignedp,
767 &reversep, &volatilep);
770 if (base != NULL_TREE
771 && TREE_CODE (base) == MEM_REF
772 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
774 tree ssa = TREE_OPERAND (base, 0);
775 tree lhs = gimple_get_lhs (stmt);
776 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
777 src.gori ()->register_dependency (lhs, ssa);
778 src.get_operand (r, ssa);
779 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
781 poly_offset_int off = 0;
782 bool off_cst = false;
783 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
785 off = mem_ref_offset (base);
786 if (offset)
787 off += poly_offset_int::from (wi::to_poly_wide (offset),
788 SIGNED);
789 off <<= LOG2_BITS_PER_UNIT;
790 off += bitpos;
791 off_cst = true;
793 /* If &X->a is equal to X, the range of X is the result. */
794 if (off_cst && known_eq (off, 0))
795 return true;
796 else if (flag_delete_null_pointer_checks
797 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
799 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
800 allow going from non-NULL pointer to NULL. */
801 if (r.undefined_p ()
802 || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
804 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
805 using POINTER_PLUS_EXPR if off_cst and just fall back to
806 this. */
807 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
808 return true;
811 /* If MEM_REF has a "positive" offset, consider it non-NULL
812 always, for -fdelete-null-pointer-checks also "negative"
813 ones. Punt for unknown offsets (e.g. variable ones). */
814 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
815 && off_cst
816 && known_ne (off, 0)
817 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
819 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
820 return true;
822 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
823 return true;
826 // Handle "= &a".
827 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
829 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
830 return true;
833 // Otherwise return varying.
834 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
835 return true;
838 // Calculate a range for phi statement S and return it in R.
839 // If a range cannot be calculated, return false.
841 bool
842 fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
844 tree phi_def = gimple_phi_result (phi);
845 tree type = gimple_range_type (phi);
846 Value_Range arg_range (type);
847 Value_Range equiv_range (type);
848 unsigned x;
850 if (!type)
851 return false;
853 // Track if all executable arguments are the same.
854 tree single_arg = NULL_TREE;
855 bool seen_arg = false;
857 // Start with an empty range, unioning in each argument's range.
858 r.set_undefined ();
859 for (x = 0; x < gimple_phi_num_args (phi); x++)
861 tree arg = gimple_phi_arg_def (phi, x);
862 // An argument that is the same as the def provides no new range.
863 if (arg == phi_def)
864 continue;
866 edge e = gimple_phi_arg_edge (phi, x);
868 // Get the range of the argument on its edge.
869 src.get_phi_operand (arg_range, arg, e);
871 if (!arg_range.undefined_p ())
873 // Register potential dependencies for stale value tracking.
874 // Likewise, if the incoming PHI argument is equivalent to this
875 // PHI definition, it provides no new info. Accumulate these ranges
876 // in case all arguments are equivalences.
877 if (src.query ()->query_relation (e, arg, phi_def, false) == VREL_EQ)
878 equiv_range.union_(arg_range);
879 else
880 r.union_ (arg_range);
882 if (gimple_range_ssa_p (arg) && src.gori ())
883 src.gori ()->register_dependency (phi_def, arg);
886 // Track if all arguments are the same.
887 if (!seen_arg)
889 seen_arg = true;
890 single_arg = arg;
892 else if (single_arg != arg)
893 single_arg = NULL_TREE;
895 // Once the value reaches varying, stop looking.
896 if (r.varying_p () && single_arg == NULL_TREE)
897 break;
900 // If all arguments were equivalences, use the equivalence ranges as no
901 // arguments were processed.
902 if (r.undefined_p () && !equiv_range.undefined_p ())
903 r = equiv_range;
905 // If the PHI boils down to a single effective argument, look at it.
906 if (single_arg)
908 // Symbolic arguments can be equivalences.
909 if (gimple_range_ssa_p (single_arg))
911 // Only allow the equivalence if the PHI definition does not
912 // dominate any incoming edge for SINGLE_ARG.
913 // See PR 108139 and 109462.
914 basic_block bb = gimple_bb (phi);
915 if (!dom_info_available_p (CDI_DOMINATORS))
916 single_arg = NULL;
917 else
918 for (x = 0; x < gimple_phi_num_args (phi); x++)
919 if (gimple_phi_arg_def (phi, x) == single_arg
920 && dominated_by_p (CDI_DOMINATORS,
921 gimple_phi_arg_edge (phi, x)->src,
922 bb))
924 single_arg = NULL;
925 break;
927 if (single_arg)
928 src.register_relation (phi, VREL_EQ, phi_def, single_arg);
930 else if (src.get_operand (arg_range, single_arg)
931 && arg_range.singleton_p ())
933 // Numerical arguments that are a constant can be returned as
934 // the constant. This can help fold later cases where even this
935 // constant might have been UNDEFINED via an unreachable edge.
936 r = arg_range;
937 return true;
941 bool loop_info_p = false;
942 // If SCEV is available, query if this PHI has any known values.
943 if (scev_initialized_p ()
944 && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
946 class loop *l = loop_containing_stmt (phi);
947 if (l && loop_outer (l))
949 Value_Range loop_range (type);
950 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
951 if (!loop_range.varying_p ())
953 if (dump_file && (dump_flags & TDF_DETAILS))
955 fprintf (dump_file, " Loops range found for ");
956 print_generic_expr (dump_file, phi_def, TDF_SLIM);
957 fprintf (dump_file, ": ");
958 loop_range.dump (dump_file);
959 fprintf (dump_file, " and calculated range :");
960 r.dump (dump_file);
961 fprintf (dump_file, "\n");
963 r.intersect (loop_range);
964 loop_info_p = true;
969 if (!loop_info_p && phi_analysis_available_p ()
970 && irange::supports_p (TREE_TYPE (phi_def)))
972 phi_group *g = (phi_analysis())[phi_def];
973 if (g && !(g->range ().varying_p ()))
975 if (dump_file && (dump_flags & TDF_DETAILS))
977 fprintf (dump_file, " PHI group range found for ");
978 print_generic_expr (dump_file, phi_def, TDF_SLIM);
979 fprintf (dump_file, ": ");
980 g->range ().dump (dump_file);
981 fprintf (dump_file, " and adjusted original range from :");
982 r.dump (dump_file);
984 r.intersect (g->range ());
985 if (dump_file && (dump_flags & TDF_DETAILS))
987 fprintf (dump_file, " to :");
988 r.dump (dump_file);
989 fprintf (dump_file, "\n");
994 return true;
997 // Calculate a range for call statement S and return it in R.
998 // If a range cannot be calculated, return false.
1000 bool
1001 fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1003 tree type = gimple_range_type (call);
1004 if (!type)
1005 return false;
1007 tree lhs = gimple_call_lhs (call);
1008 bool strict_overflow_p;
1010 if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1011 r.set_nonnegative (type);
1012 else if (gimple_call_nonnull_result_p (call)
1013 || gimple_call_nonnull_arg (call))
1014 r.set_nonzero (type);
1015 else
1016 r.set_varying (type);
1018 // If there is an LHS, intersect that with what is known.
1019 if (lhs)
1021 Value_Range def (TREE_TYPE (lhs));
1022 gimple_range_global (def, lhs);
1023 r.intersect (def);
1025 return true;
1028 // Calculate a range for COND_EXPR statement S and return it in R.
1029 // If a range cannot be calculated, return false.
1031 bool
1032 fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1034 tree cond = gimple_assign_rhs1 (s);
1035 tree op1 = gimple_assign_rhs2 (s);
1036 tree op2 = gimple_assign_rhs3 (s);
1038 tree type = gimple_range_type (s);
1039 if (!type)
1040 return false;
1042 Value_Range range1 (TREE_TYPE (op1));
1043 Value_Range range2 (TREE_TYPE (op2));
1044 Value_Range cond_range (TREE_TYPE (cond));
1045 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1046 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1047 src.get_operand (cond_range, cond);
1048 src.get_operand (range1, op1);
1049 src.get_operand (range2, op2);
1051 // Try to see if there is a dependence between the COND and either operand
1052 if (src.gori ())
1053 if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1054 if (dump_file && (dump_flags & TDF_DETAILS))
1056 fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1057 range1.dump(dump_file);
1058 fprintf (dump_file, " and Range op2: ");
1059 range2.dump(dump_file);
1060 fprintf (dump_file, "\n");
1063 // If the condition is known, choose the appropriate expression.
1064 if (cond_range.singleton_p ())
1066 // False, pick second operand.
1067 if (cond_range.zero_p ())
1068 r = range2;
1069 else
1070 r = range1;
1072 else
1074 r = range1;
1075 r.union_ (range2);
1077 gcc_checking_assert (r.undefined_p ()
1078 || range_compatible_p (r.type (), type));
1079 return true;
1082 // If SCEV has any information about phi node NAME, return it as a range in R.
1084 void
1085 fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1086 class loop *l, gphi *phi,
1087 fur_source &src)
1089 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1090 if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1091 r.set_varying (TREE_TYPE (name));
1094 // -----------------------------------------------------------------------
1096 // Check if an && or || expression can be folded based on relations. ie
1097 // c_2 = a_6 > b_7
1098 // c_3 = a_6 < b_7
1099 // c_4 = c_2 && c_3
1100 // c_2 and c_3 can never be true at the same time,
1101 // Therefore c_4 can always resolve to false based purely on the relations.
1103 void
1104 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1105 fur_source &src)
1107 // No queries or already folded.
1108 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1109 return;
1111 // Only care about AND and OR expressions.
1112 enum tree_code code = gimple_expr_code (s);
1113 bool is_and = false;
1114 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1115 is_and = true;
1116 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1117 return;
1119 gimple_range_op_handler handler (s);
1120 tree lhs = handler.lhs ();
1121 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1122 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1124 // Deal with || and && only when there is a full set of symbolics.
1125 if (!lhs || !ssa1 || !ssa2
1126 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1127 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1128 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1129 return;
1131 // Now we know its a boolean AND or OR expression with boolean operands.
1132 // Ideally we search dependencies for common names, and see what pops out.
1133 // until then, simply try to resolve direct dependencies.
1135 gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1136 gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1138 gimple_range_op_handler handler1 (ssa1_stmt);
1139 gimple_range_op_handler handler2 (ssa2_stmt);
1141 // If either handler is not present, no relation can be found.
1142 if (!handler1 || !handler2)
1143 return;
1145 // Both stmts will need to have 2 ssa names in the stmt.
1146 tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1147 tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1148 tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1149 tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1151 if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1152 return;
1154 if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1155 return;
1157 // Make sure they are the same dependencies, and detect the order of the
1158 // relationship.
1159 bool reverse_op2 = true;
1160 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1161 reverse_op2 = false;
1162 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1163 return;
1165 int_range<2> bool_one = range_true ();
1167 relation_kind relation1 = handler1.op1_op2_relation (bool_one);
1168 relation_kind relation2 = handler2.op1_op2_relation (bool_one);
1169 if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1170 return;
1172 if (reverse_op2)
1173 relation2 = relation_negate (relation2);
1175 // x && y is false if the relation intersection of the true cases is NULL.
1176 if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1177 lhs_range = range_false (boolean_type_node);
1178 // x || y is true if the union of the true cases is NO-RELATION..
1179 // ie, one or the other being true covers the full range of possibilities.
1180 else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1181 lhs_range = bool_one;
1182 else
1183 return;
1185 range_cast (lhs_range, TREE_TYPE (lhs));
1186 if (dump_file && (dump_flags & TDF_DETAILS))
1188 fprintf (dump_file, " Relation adjustment: ");
1189 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1190 fprintf (dump_file, " and ");
1191 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1192 fprintf (dump_file, " combine to produce ");
1193 lhs_range.dump (dump_file);
1194 fputc ('\n', dump_file);
1197 return;
1200 // Register any outgoing edge relations from a conditional branch.
1202 void
1203 fur_source::register_outgoing_edges (gcond *s, irange &lhs_range, edge e0, edge e1)
1205 int_range<2> e0_range, e1_range;
1206 tree name;
1207 basic_block bb = gimple_bb (s);
1209 gimple_range_op_handler handler (s);
1210 if (!handler)
1211 return;
1213 if (e0)
1215 // If this edge is never taken, ignore it.
1216 gcond_edge_range (e0_range, e0);
1217 e0_range.intersect (lhs_range);
1218 if (e0_range.undefined_p ())
1219 e0 = NULL;
1222 if (e1)
1224 // If this edge is never taken, ignore it.
1225 gcond_edge_range (e1_range, e1);
1226 e1_range.intersect (lhs_range);
1227 if (e1_range.undefined_p ())
1228 e1 = NULL;
1231 if (!e0 && !e1)
1232 return;
1234 // First, register the gcond itself. This will catch statements like
1235 // if (a_2 < b_5)
1236 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1237 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1238 if (ssa1 && ssa2)
1240 if (e0)
1242 relation_kind relation = handler.op1_op2_relation (e0_range);
1243 if (relation != VREL_VARYING)
1244 register_relation (e0, relation, ssa1, ssa2);
1246 if (e1)
1248 relation_kind relation = handler.op1_op2_relation (e1_range);
1249 if (relation != VREL_VARYING)
1250 register_relation (e1, relation, ssa1, ssa2);
1254 // Outgoing relations of GORI exports require a gori engine.
1255 if (!gori ())
1256 return;
1258 // Now look for other relations in the exports. This will find stmts
1259 // leading to the condition such as:
1260 // c_2 = a_4 < b_7
1261 // if (c_2)
1262 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
1264 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1265 continue;
1266 gimple *stmt = SSA_NAME_DEF_STMT (name);
1267 gimple_range_op_handler handler (stmt);
1268 if (!handler)
1269 continue;
1270 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1271 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1272 Value_Range r (TREE_TYPE (name));
1273 if (ssa1 && ssa2)
1275 if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
1276 && r.singleton_p ())
1278 relation_kind relation = handler.op1_op2_relation (r);
1279 if (relation != VREL_VARYING)
1280 register_relation (e0, relation, ssa1, ssa2);
1282 if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
1283 && r.singleton_p ())
1285 relation_kind relation = handler.op1_op2_relation (r);
1286 if (relation != VREL_VARYING)
1287 register_relation (e1, relation, ssa1, ssa2);