RISC-V: Make stack_save_restore tests more robust
[official-gcc.git] / gcc / gimple-range-fold.cc
blob8ebff7f598043e96d02f07a3e7253d81c35f980a
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 // Do not use the vector for non-ssa-names, or if it has been emptied.
266 if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
267 return m_query->range_of_expr (r, expr);
268 r = *m_list[m_index++];
269 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
270 return true;
273 // This will simply pick the next operand from the vector.
274 bool
275 fur_list::get_phi_operand (vrange &r, tree expr, edge e ATTRIBUTE_UNUSED)
277 return get_operand (r, expr);
280 // Fold stmt S into range R using R1 as the first operand.
282 bool
283 fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
285 fold_using_range f;
286 fur_list src (r1, q);
287 return f.fold_stmt (r, s, src);
290 // Fold stmt S into range R using R1 and R2 as the first two operands.
292 bool
293 fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2, range_query *q)
295 fold_using_range f;
296 fur_list src (r1, r2, q);
297 return f.fold_stmt (r, s, src);
300 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
301 // operands encountered.
303 bool
304 fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector,
305 range_query *q)
307 fold_using_range f;
308 fur_list src (num_elements, vector, q);
309 return f.fold_stmt (r, s, src);
312 // Fold stmt S into range R using range query Q.
314 bool
315 fold_range (vrange &r, gimple *s, range_query *q)
317 fold_using_range f;
318 fur_stmt src (s, q);
319 return f.fold_stmt (r, s, src);
322 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
324 bool
325 fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
327 fold_using_range f;
328 fur_edge src (on_edge, q);
329 return f.fold_stmt (r, s, src);
332 // Provide a fur_source which can be used to determine any relations on
333 // a statement. It manages the callback from fold_using_ranges to determine
334 // a relation_trio for a statement.
336 class fur_relation : public fur_stmt
338 public:
339 fur_relation (gimple *s, range_query *q = NULL);
340 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
341 tree op2);
342 virtual void register_relation (edge e, relation_kind k, tree op1,
343 tree op2);
344 relation_trio trio() const;
345 private:
346 relation_kind def_op1, def_op2, op1_op2;
349 fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
351 def_op1 = def_op2 = op1_op2 = VREL_VARYING;
354 // Construct a trio from what is known.
356 relation_trio
357 fur_relation::trio () const
359 return relation_trio (def_op1, def_op2, op1_op2);
362 // Don't support edges, but avoid a compiler warning by providing the routine.
364 void
365 fur_relation::register_relation (edge, relation_kind, tree, tree)
369 // Register relation K between OP1 and OP2 on STMT.
371 void
372 fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
373 tree op2)
375 tree lhs = gimple_get_lhs (stmt);
376 tree a1 = NULL_TREE;
377 tree a2 = NULL_TREE;
378 switch (gimple_code (stmt))
380 case GIMPLE_COND:
381 a1 = gimple_cond_lhs (stmt);
382 a2 = gimple_cond_rhs (stmt);
383 break;
384 case GIMPLE_ASSIGN:
385 a1 = gimple_assign_rhs1 (stmt);
386 if (gimple_num_ops (stmt) >= 3)
387 a2 = gimple_assign_rhs2 (stmt);
388 break;
389 default:
390 break;
392 // STMT is of the form LHS = A1 op A2, now map the relation to these
393 // operands, if possible.
394 if (op1 == lhs)
396 if (op2 == a1)
397 def_op1 = k;
398 else if (op2 == a2)
399 def_op2 = k;
401 else if (op2 == lhs)
403 if (op1 == a1)
404 def_op1 = relation_swap (k);
405 else if (op1 == a2)
406 def_op2 = relation_swap (k);
408 else
410 if (op1 == a1 && op2 == a2)
411 op1_op2 = k;
412 else if (op2 == a1 && op1 == a2)
413 op1_op2 = relation_swap (k);
417 // Return the relation trio for stmt S using query Q.
419 relation_trio
420 fold_relations (gimple *s, range_query *q)
422 fold_using_range f;
423 fur_relation src (s, q);
424 tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
425 if (lhs)
427 Value_Range vr(TREE_TYPE (lhs));
428 if (f.fold_stmt (vr, s, src))
429 return src.trio ();
431 return TRIO_VARYING;
434 // -------------------------------------------------------------------------
436 // Adjust the range for a pointer difference where the operands came
437 // from a memchr.
439 // This notices the following sequence:
441 // def = __builtin_memchr (arg, 0, sz)
442 // n = def - arg
444 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
446 static void
447 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
449 tree op0 = gimple_assign_rhs1 (diff_stmt);
450 tree op1 = gimple_assign_rhs2 (diff_stmt);
451 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
452 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
453 gimple *call;
455 if (TREE_CODE (op0) == SSA_NAME
456 && TREE_CODE (op1) == SSA_NAME
457 && (call = SSA_NAME_DEF_STMT (op0))
458 && is_gimple_call (call)
459 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
460 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
461 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
462 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
463 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
464 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
465 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
466 && integer_zerop (gimple_call_arg (call, 1)))
468 wide_int maxm1 = irange_val_max (ptrdiff_type_node) - 1;
469 res.intersect (int_range<2> (ptrdiff_type_node,
470 wi::zero (TYPE_PRECISION (ptrdiff_type_node)),
471 maxm1));
475 // Adjust the range for an IMAGPART_EXPR.
477 static void
478 adjust_imagpart_expr (vrange &res, const gimple *stmt)
480 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
482 if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
483 return;
485 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
486 if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
488 switch (gimple_call_internal_fn (def_stmt))
490 case IFN_ADD_OVERFLOW:
491 case IFN_SUB_OVERFLOW:
492 case IFN_MUL_OVERFLOW:
493 case IFN_UADDC:
494 case IFN_USUBC:
495 case IFN_ATOMIC_COMPARE_EXCHANGE:
497 int_range<2> r;
498 r.set_varying (boolean_type_node);
499 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
500 range_cast (r, type);
501 res.intersect (r);
503 default:
504 break;
506 return;
508 if (is_gimple_assign (def_stmt)
509 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
511 tree cst = gimple_assign_rhs1 (def_stmt);
512 if (TREE_CODE (cst) == COMPLEX_CST
513 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
515 wide_int w = wi::to_wide (TREE_IMAGPART (cst));
516 int_range<1> imag (TREE_TYPE (TREE_IMAGPART (cst)), w, w);
517 res.intersect (imag);
522 // Adjust the range for a REALPART_EXPR.
524 static void
525 adjust_realpart_expr (vrange &res, const gimple *stmt)
527 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
529 if (TREE_CODE (name) != SSA_NAME)
530 return;
532 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
533 if (!SSA_NAME_DEF_STMT (name))
534 return;
536 if (is_gimple_assign (def_stmt)
537 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
539 tree cst = gimple_assign_rhs1 (def_stmt);
540 if (TREE_CODE (cst) == COMPLEX_CST
541 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
543 wide_int imag = wi::to_wide (TREE_REALPART (cst));
544 int_range<2> tmp (TREE_TYPE (TREE_REALPART (cst)), imag, imag);
545 res.intersect (tmp);
550 // This function looks for situations when walking the use/def chains
551 // may provide additional contextual range information not exposed on
552 // this statement.
554 static void
555 gimple_range_adjustment (vrange &res, const gimple *stmt)
557 switch (gimple_expr_code (stmt))
559 case POINTER_DIFF_EXPR:
560 adjust_pointer_diff_expr (as_a <irange> (res), stmt);
561 return;
563 case IMAGPART_EXPR:
564 adjust_imagpart_expr (res, stmt);
565 return;
567 case REALPART_EXPR:
568 adjust_realpart_expr (res, stmt);
569 return;
571 default:
572 break;
576 // Calculate a range for statement S and return it in R. If NAME is provided it
577 // represents the SSA_NAME on the LHS of the statement. It is only required
578 // if there is more than one lhs/output. If a range cannot
579 // be calculated, return false.
581 bool
582 fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
584 bool res = false;
585 // If name and S are specified, make sure it is an LHS of S.
586 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
587 name == gimple_get_lhs (s));
589 if (!name)
590 name = gimple_get_lhs (s);
592 // Process addresses.
593 if (gimple_code (s) == GIMPLE_ASSIGN
594 && gimple_assign_rhs_code (s) == ADDR_EXPR)
595 return range_of_address (as_a <irange> (r), s, src);
597 gimple_range_op_handler handler (s);
598 if (handler)
599 res = range_of_range_op (r, handler, src);
600 else if (is_a<gphi *>(s))
601 res = range_of_phi (r, as_a<gphi *> (s), src);
602 else if (is_a<gcall *>(s))
603 res = range_of_call (r, as_a<gcall *> (s), src);
604 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
605 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
607 // If the result is varying, check for basic nonnegativeness.
608 // Specifically this helps for now with strict enum in cases like
609 // g++.dg/warn/pr33738.C.
610 bool so_p;
611 if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
612 && gimple_stmt_nonnegative_warnv_p (s, &so_p))
613 r.set_nonnegative (r.type ());
615 if (!res)
617 // If no name specified or range is unsupported, bail.
618 if (!name || !gimple_range_ssa_p (name))
619 return false;
620 // We don't understand the stmt, so return the global range.
621 gimple_range_global (r, name);
622 return true;
625 if (r.undefined_p ())
626 return true;
628 // We sometimes get compatible types copied from operands, make sure
629 // the correct type is being returned.
630 if (name && TREE_TYPE (name) != r.type ())
632 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
633 range_cast (r, TREE_TYPE (name));
635 return true;
638 // Calculate a range for range_op statement S and return it in R. If any
639 // If a range cannot be calculated, return false.
641 bool
642 fold_using_range::range_of_range_op (vrange &r,
643 gimple_range_op_handler &handler,
644 fur_source &src)
646 gcc_checking_assert (handler);
647 gimple *s = handler.stmt ();
648 tree type = gimple_range_type (s);
649 if (!type)
650 return false;
652 tree lhs = handler.lhs ();
653 tree op1 = handler.operand1 ();
654 tree op2 = handler.operand2 ();
656 // Certain types of builtin functions may have no arguments.
657 if (!op1)
659 Value_Range r1 (type);
660 if (!handler.fold_range (r, type, r1, r1))
661 r.set_varying (type);
662 return true;
665 Value_Range range1 (TREE_TYPE (op1));
666 Value_Range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
668 if (src.get_operand (range1, op1))
670 if (!op2)
672 // Fold range, and register any dependency if available.
673 Value_Range r2 (type);
674 r2.set_varying (type);
675 if (!handler.fold_range (r, type, range1, r2))
676 r.set_varying (type);
677 if (lhs && gimple_range_ssa_p (op1))
679 if (src.gori ())
680 src.gori ()->register_dependency (lhs, op1);
681 relation_kind rel;
682 rel = handler.lhs_op1_relation (r, range1, range1);
683 if (rel != VREL_VARYING)
684 src.register_relation (s, rel, lhs, op1);
687 else if (src.get_operand (range2, op2))
689 relation_kind rel = src.query_relation (op1, op2);
690 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
692 fprintf (dump_file, " folding with relation ");
693 print_generic_expr (dump_file, op1, TDF_SLIM);
694 print_relation (dump_file, rel);
695 print_generic_expr (dump_file, op2, TDF_SLIM);
696 fputc ('\n', dump_file);
698 // Fold range, and register any dependency if available.
699 if (!handler.fold_range (r, type, range1, range2,
700 relation_trio::op1_op2 (rel)))
701 r.set_varying (type);
702 if (irange::supports_p (type))
703 relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
704 if (lhs)
706 if (src.gori ())
708 src.gori ()->register_dependency (lhs, op1);
709 src.gori ()->register_dependency (lhs, op2);
711 if (gimple_range_ssa_p (op1))
713 rel = handler.lhs_op1_relation (r, range1, range2, rel);
714 if (rel != VREL_VARYING)
715 src.register_relation (s, rel, lhs, op1);
717 if (gimple_range_ssa_p (op2))
719 rel = handler.lhs_op2_relation (r, range1, range2, rel);
720 if (rel != VREL_VARYING)
721 src.register_relation (s, rel, lhs, op2);
724 // Check for an existing BB, as we maybe asked to fold an
725 // artificial statement not in the CFG.
726 else if (is_a<gcond *> (s) && gimple_bb (s))
728 basic_block bb = gimple_bb (s);
729 edge e0 = EDGE_SUCC (bb, 0);
730 edge e1 = EDGE_SUCC (bb, 1);
732 if (!single_pred_p (e0->dest))
733 e0 = NULL;
734 if (!single_pred_p (e1->dest))
735 e1 = NULL;
736 src.register_outgoing_edges (as_a<gcond *> (s),
737 as_a <irange> (r), e0, e1);
740 else
741 r.set_varying (type);
743 else
744 r.set_varying (type);
745 // Make certain range-op adjustments that aren't handled any other way.
746 gimple_range_adjustment (r, s);
747 return true;
750 // Calculate the range of an assignment containing an ADDR_EXPR.
751 // Return the range in R.
752 // If a range cannot be calculated, set it to VARYING and return true.
754 bool
755 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
757 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
758 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
760 bool strict_overflow_p;
761 tree expr = gimple_assign_rhs1 (stmt);
762 poly_int64 bitsize, bitpos;
763 tree offset;
764 machine_mode mode;
765 int unsignedp, reversep, volatilep;
766 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
767 &bitpos, &offset, &mode, &unsignedp,
768 &reversep, &volatilep);
771 if (base != NULL_TREE
772 && TREE_CODE (base) == MEM_REF
773 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
775 tree ssa = TREE_OPERAND (base, 0);
776 tree lhs = gimple_get_lhs (stmt);
777 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
778 src.gori ()->register_dependency (lhs, ssa);
779 src.get_operand (r, ssa);
780 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
782 poly_offset_int off = 0;
783 bool off_cst = false;
784 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
786 off = mem_ref_offset (base);
787 if (offset)
788 off += poly_offset_int::from (wi::to_poly_wide (offset),
789 SIGNED);
790 off <<= LOG2_BITS_PER_UNIT;
791 off += bitpos;
792 off_cst = true;
794 /* If &X->a is equal to X, the range of X is the result. */
795 if (off_cst && known_eq (off, 0))
796 return true;
797 else if (flag_delete_null_pointer_checks
798 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
800 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
801 allow going from non-NULL pointer to NULL. */
802 if (r.undefined_p ()
803 || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
805 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
806 using POINTER_PLUS_EXPR if off_cst and just fall back to
807 this. */
808 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
809 return true;
812 /* If MEM_REF has a "positive" offset, consider it non-NULL
813 always, for -fdelete-null-pointer-checks also "negative"
814 ones. Punt for unknown offsets (e.g. variable ones). */
815 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
816 && off_cst
817 && known_ne (off, 0)
818 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
820 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
821 return true;
823 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
824 return true;
827 // Handle "= &a".
828 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
830 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
831 return true;
834 // Otherwise return varying.
835 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
836 return true;
839 // Calculate a range for phi statement S and return it in R.
840 // If a range cannot be calculated, return false.
842 bool
843 fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
845 tree phi_def = gimple_phi_result (phi);
846 tree type = gimple_range_type (phi);
847 Value_Range arg_range (type);
848 Value_Range equiv_range (type);
849 unsigned x;
851 if (!type)
852 return false;
854 // Track if all executable arguments are the same.
855 tree single_arg = NULL_TREE;
856 bool seen_arg = false;
858 // Start with an empty range, unioning in each argument's range.
859 r.set_undefined ();
860 for (x = 0; x < gimple_phi_num_args (phi); x++)
862 tree arg = gimple_phi_arg_def (phi, x);
863 // An argument that is the same as the def provides no new range.
864 if (arg == phi_def)
865 continue;
867 edge e = gimple_phi_arg_edge (phi, x);
869 // Get the range of the argument on its edge.
870 src.get_phi_operand (arg_range, arg, e);
872 if (!arg_range.undefined_p ())
874 // Register potential dependencies for stale value tracking.
875 // Likewise, if the incoming PHI argument is equivalent to this
876 // PHI definition, it provides no new info. Accumulate these ranges
877 // in case all arguments are equivalences.
878 if (src.query ()->query_relation (e, arg, phi_def, false) == VREL_EQ)
879 equiv_range.union_(arg_range);
880 else
881 r.union_ (arg_range);
883 if (gimple_range_ssa_p (arg) && src.gori ())
884 src.gori ()->register_dependency (phi_def, arg);
887 // Track if all arguments are the same.
888 if (!seen_arg)
890 seen_arg = true;
891 single_arg = arg;
893 else if (single_arg != arg)
894 single_arg = NULL_TREE;
896 // Once the value reaches varying, stop looking.
897 if (r.varying_p () && single_arg == NULL_TREE)
898 break;
901 // If all arguments were equivalences, use the equivalence ranges as no
902 // arguments were processed.
903 if (r.undefined_p () && !equiv_range.undefined_p ())
904 r = equiv_range;
906 // If the PHI boils down to a single effective argument, look at it.
907 if (single_arg)
909 // Symbolic arguments can be equivalences.
910 if (gimple_range_ssa_p (single_arg))
912 // Only allow the equivalence if the PHI definition does not
913 // dominate any incoming edge for SINGLE_ARG.
914 // See PR 108139 and 109462.
915 basic_block bb = gimple_bb (phi);
916 if (!dom_info_available_p (CDI_DOMINATORS))
917 single_arg = NULL;
918 else
919 for (x = 0; x < gimple_phi_num_args (phi); x++)
920 if (gimple_phi_arg_def (phi, x) == single_arg
921 && dominated_by_p (CDI_DOMINATORS,
922 gimple_phi_arg_edge (phi, x)->src,
923 bb))
925 single_arg = NULL;
926 break;
928 if (single_arg)
929 src.register_relation (phi, VREL_EQ, phi_def, single_arg);
931 else if (src.get_operand (arg_range, single_arg)
932 && arg_range.singleton_p ())
934 // Numerical arguments that are a constant can be returned as
935 // the constant. This can help fold later cases where even this
936 // constant might have been UNDEFINED via an unreachable edge.
937 r = arg_range;
938 return true;
942 bool loop_info_p = false;
943 // If SCEV is available, query if this PHI has any known values.
944 if (scev_initialized_p ()
945 && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
947 class loop *l = loop_containing_stmt (phi);
948 if (l && loop_outer (l))
950 Value_Range loop_range (type);
951 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
952 if (!loop_range.varying_p ())
954 if (dump_file && (dump_flags & TDF_DETAILS))
956 fprintf (dump_file, "Loops range found for ");
957 print_generic_expr (dump_file, phi_def, TDF_SLIM);
958 fprintf (dump_file, ": ");
959 loop_range.dump (dump_file);
960 fprintf (dump_file, " and calculated range :");
961 r.dump (dump_file);
962 fprintf (dump_file, "\n");
964 r.intersect (loop_range);
965 loop_info_p = true;
970 if (!loop_info_p && phi_analysis_available_p ()
971 && irange::supports_p (TREE_TYPE (phi_def)))
973 phi_group *g = (phi_analysis())[phi_def];
974 if (g && !(g->range ().varying_p ()))
976 if (dump_file && (dump_flags & TDF_DETAILS))
978 fprintf (dump_file, "PHI GROUP query for ");
979 print_generic_expr (dump_file, phi_def, TDF_SLIM);
980 fprintf (dump_file, " found : ");
981 g->range ().dump (dump_file);
982 fprintf (dump_file, " and adjusted original range from :");
983 r.dump (dump_file);
985 r.intersect (g->range ());
986 if (dump_file && (dump_flags & TDF_DETAILS))
988 fprintf (dump_file, " to :");
989 r.dump (dump_file);
990 fprintf (dump_file, "\n");
995 return true;
998 // Calculate a range for call statement S and return it in R.
999 // If a range cannot be calculated, return false.
1001 bool
1002 fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1004 tree type = gimple_range_type (call);
1005 if (!type)
1006 return false;
1008 tree lhs = gimple_call_lhs (call);
1009 bool strict_overflow_p;
1011 if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1012 r.set_nonnegative (type);
1013 else if (gimple_call_nonnull_result_p (call)
1014 || gimple_call_nonnull_arg (call))
1015 r.set_nonzero (type);
1016 else
1017 r.set_varying (type);
1019 // If there is an LHS, intersect that with what is known.
1020 if (lhs)
1022 Value_Range def (TREE_TYPE (lhs));
1023 gimple_range_global (def, lhs);
1024 r.intersect (def);
1026 return true;
1029 // Calculate a range for COND_EXPR statement S and return it in R.
1030 // If a range cannot be calculated, return false.
1032 bool
1033 fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1035 tree cond = gimple_assign_rhs1 (s);
1036 tree op1 = gimple_assign_rhs2 (s);
1037 tree op2 = gimple_assign_rhs3 (s);
1039 tree type = gimple_range_type (s);
1040 if (!type)
1041 return false;
1043 Value_Range range1 (TREE_TYPE (op1));
1044 Value_Range range2 (TREE_TYPE (op2));
1045 Value_Range cond_range (TREE_TYPE (cond));
1046 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1047 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1048 src.get_operand (cond_range, cond);
1049 src.get_operand (range1, op1);
1050 src.get_operand (range2, op2);
1052 // Try to see if there is a dependence between the COND and either operand
1053 if (src.gori ())
1054 if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1055 if (dump_file && (dump_flags & TDF_DETAILS))
1057 fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1058 range1.dump(dump_file);
1059 fprintf (dump_file, " and Range op2: ");
1060 range2.dump(dump_file);
1061 fprintf (dump_file, "\n");
1064 // If the condition is known, choose the appropriate expression.
1065 if (cond_range.singleton_p ())
1067 // False, pick second operand.
1068 if (cond_range.zero_p ())
1069 r = range2;
1070 else
1071 r = range1;
1073 else
1075 r = range1;
1076 r.union_ (range2);
1078 gcc_checking_assert (r.undefined_p ()
1079 || range_compatible_p (r.type (), type));
1080 return true;
1083 // If SCEV has any information about phi node NAME, return it as a range in R.
1085 void
1086 fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1087 class loop *l, gphi *phi,
1088 fur_source &src)
1090 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1091 if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1092 r.set_varying (TREE_TYPE (name));
1095 // -----------------------------------------------------------------------
1097 // Check if an && or || expression can be folded based on relations. ie
1098 // c_2 = a_6 > b_7
1099 // c_3 = a_6 < b_7
1100 // c_4 = c_2 && c_3
1101 // c_2 and c_3 can never be true at the same time,
1102 // Therefore c_4 can always resolve to false based purely on the relations.
1104 void
1105 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1106 fur_source &src, vrange &op1,
1107 vrange &op2)
1109 // No queries or already folded.
1110 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1111 return;
1113 // Only care about AND and OR expressions.
1114 enum tree_code code = gimple_expr_code (s);
1115 bool is_and = false;
1116 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1117 is_and = true;
1118 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1119 return;
1121 gimple_range_op_handler handler (s);
1122 tree lhs = handler.lhs ();
1123 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1124 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1126 // Deal with || and && only when there is a full set of symbolics.
1127 if (!lhs || !ssa1 || !ssa2
1128 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1129 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1130 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1131 return;
1133 // Now we know its a boolean AND or OR expression with boolean operands.
1134 // Ideally we search dependencies for common names, and see what pops out.
1135 // until then, simply try to resolve direct dependencies.
1137 gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1138 gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1140 gimple_range_op_handler handler1 (ssa1_stmt);
1141 gimple_range_op_handler handler2 (ssa2_stmt);
1143 // If either handler is not present, no relation can be found.
1144 if (!handler1 || !handler2)
1145 return;
1147 // Both stmts will need to have 2 ssa names in the stmt.
1148 tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1149 tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1150 tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1151 tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1153 if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1154 return;
1156 if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1157 return;
1159 // Make sure they are the same dependencies, and detect the order of the
1160 // relationship.
1161 bool reverse_op2 = true;
1162 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1163 reverse_op2 = false;
1164 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1165 return;
1167 int_range<2> bool_one = range_true ();
1168 relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1169 relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1170 if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1171 return;
1173 if (reverse_op2)
1174 relation2 = relation_negate (relation2);
1176 // x && y is false if the relation intersection of the true cases is NULL.
1177 if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1178 lhs_range = range_false (boolean_type_node);
1179 // x || y is true if the union of the true cases is NO-RELATION..
1180 // ie, one or the other being true covers the full range of possibilities.
1181 else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1182 lhs_range = bool_one;
1183 else
1184 return;
1186 range_cast (lhs_range, TREE_TYPE (lhs));
1187 if (dump_file && (dump_flags & TDF_DETAILS))
1189 fprintf (dump_file, " Relation adjustment: ");
1190 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1191 fprintf (dump_file, " and ");
1192 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1193 fprintf (dump_file, " combine to produce ");
1194 lhs_range.dump (dump_file);
1195 fputc ('\n', dump_file);
1198 return;
1201 // Register any outgoing edge relations from a conditional branch.
1203 void
1204 fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1205 edge e0, edge e1)
1207 int_range<2> e0_range, e1_range;
1208 tree name;
1209 basic_block bb = gimple_bb (s);
1211 gimple_range_op_handler handler (s);
1212 if (!handler)
1213 return;
1215 if (e0)
1217 // If this edge is never taken, ignore it.
1218 gcond_edge_range (e0_range, e0);
1219 e0_range.intersect (lhs_range);
1220 if (e0_range.undefined_p ())
1221 e0 = NULL;
1224 if (e1)
1226 // If this edge is never taken, ignore it.
1227 gcond_edge_range (e1_range, e1);
1228 e1_range.intersect (lhs_range);
1229 if (e1_range.undefined_p ())
1230 e1 = NULL;
1233 if (!e0 && !e1)
1234 return;
1236 // First, register the gcond itself. This will catch statements like
1237 // if (a_2 < b_5)
1238 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1239 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1240 Value_Range r1,r2;
1241 if (ssa1 && ssa2)
1243 r1.set_varying (TREE_TYPE (ssa1));
1244 r2.set_varying (TREE_TYPE (ssa2));
1245 if (e0)
1247 relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1248 if (relation != VREL_VARYING)
1249 register_relation (e0, relation, ssa1, ssa2);
1251 if (e1)
1253 relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1254 if (relation != VREL_VARYING)
1255 register_relation (e1, relation, ssa1, ssa2);
1259 // Outgoing relations of GORI exports require a gori engine.
1260 if (!gori ())
1261 return;
1263 // Now look for other relations in the exports. This will find stmts
1264 // leading to the condition such as:
1265 // c_2 = a_4 < b_7
1266 // if (c_2)
1267 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
1269 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1270 continue;
1271 gimple *stmt = SSA_NAME_DEF_STMT (name);
1272 gimple_range_op_handler handler (stmt);
1273 if (!handler)
1274 continue;
1275 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1276 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1277 Value_Range r (TREE_TYPE (name));
1278 if (ssa1 && ssa2)
1280 r1.set_varying (TREE_TYPE (ssa1));
1281 r2.set_varying (TREE_TYPE (ssa2));
1282 if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
1283 && r.singleton_p ())
1285 relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1286 if (relation != VREL_VARYING)
1287 register_relation (e0, relation, ssa1, ssa2);
1289 if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
1290 && r.singleton_p ())
1292 relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1293 if (relation != VREL_VARYING)
1294 register_relation (e1, relation, ssa1, ssa2);