c++: new-expression is potentially constant in C++20
[official-gcc.git] / gcc / gimple-range-fold.cc
blobc1a801668c44e1aceb2c23d784bc49a419183ce3
1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2022 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 "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 supoprted 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 // Retreive 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);
218 fur_list (vrange &r1, vrange &r2);
219 fur_list (unsigned num, vrange **list);
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) : fur_source (NULL)
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) : fur_source (NULL)
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) : fur_source (NULL)
254 m_list = list;
255 m_index = 0;
256 m_limit = num;
259 // Get the next operand from the vector, ensure types are compatible.
261 bool
262 fur_list::get_operand (vrange &r, tree expr)
264 if (m_index >= m_limit)
265 return m_query->range_of_expr (r, expr);
266 r = *m_list[m_index++];
267 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
268 return true;
271 // This will simply pick the next operand from the vector.
272 bool
273 fur_list::get_phi_operand (vrange &r, tree expr, edge e ATTRIBUTE_UNUSED)
275 return get_operand (r, expr);
278 // Fold stmt S into range R using R1 as the first operand.
280 bool
281 fold_range (vrange &r, gimple *s, vrange &r1)
283 fold_using_range f;
284 fur_list src (r1);
285 return f.fold_stmt (r, s, src);
288 // Fold stmt S into range R using R1 and R2 as the first two operands.
290 bool
291 fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2)
293 fold_using_range f;
294 fur_list src (r1, r2);
295 return f.fold_stmt (r, s, src);
298 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
299 // operands encountered.
301 bool
302 fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector)
304 fold_using_range f;
305 fur_list src (num_elements, vector);
306 return f.fold_stmt (r, s, src);
309 // Fold stmt S into range R using range query Q.
311 bool
312 fold_range (vrange &r, gimple *s, range_query *q)
314 fold_using_range f;
315 fur_stmt src (s, q);
316 return f.fold_stmt (r, s, src);
319 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
321 bool
322 fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
324 fold_using_range f;
325 fur_edge src (on_edge, q);
326 return f.fold_stmt (r, s, src);
329 // -------------------------------------------------------------------------
331 // Adjust the range for a pointer difference where the operands came
332 // from a memchr.
334 // This notices the following sequence:
336 // def = __builtin_memchr (arg, 0, sz)
337 // n = def - arg
339 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
341 static void
342 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
344 tree op0 = gimple_assign_rhs1 (diff_stmt);
345 tree op1 = gimple_assign_rhs2 (diff_stmt);
346 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
347 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
348 gimple *call;
350 if (TREE_CODE (op0) == SSA_NAME
351 && TREE_CODE (op1) == SSA_NAME
352 && (call = SSA_NAME_DEF_STMT (op0))
353 && is_gimple_call (call)
354 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
355 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
356 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
357 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
358 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
359 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
360 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
361 && integer_zerop (gimple_call_arg (call, 1)))
363 tree max = vrp_val_max (ptrdiff_type_node);
364 unsigned prec = TYPE_PRECISION (TREE_TYPE (max));
365 wide_int wmaxm1 = wi::to_wide (max, prec) - 1;
366 res.intersect (int_range<2> (TREE_TYPE (max), wi::zero (prec), wmaxm1));
370 // Adjust the range for an IMAGPART_EXPR.
372 static void
373 adjust_imagpart_expr (vrange &res, const gimple *stmt)
375 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
377 if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
378 return;
380 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
381 if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
383 switch (gimple_call_internal_fn (def_stmt))
385 case IFN_ADD_OVERFLOW:
386 case IFN_SUB_OVERFLOW:
387 case IFN_MUL_OVERFLOW:
388 case IFN_ATOMIC_COMPARE_EXCHANGE:
390 int_range<2> r;
391 r.set_varying (boolean_type_node);
392 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
393 range_cast (r, type);
394 res.intersect (r);
396 default:
397 break;
399 return;
401 if (is_gimple_assign (def_stmt)
402 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
404 tree cst = gimple_assign_rhs1 (def_stmt);
405 if (TREE_CODE (cst) == COMPLEX_CST)
407 int_range<2> imag (TREE_IMAGPART (cst), TREE_IMAGPART (cst));
408 res.intersect (imag);
413 // Adjust the range for a REALPART_EXPR.
415 static void
416 adjust_realpart_expr (vrange &res, const gimple *stmt)
418 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
420 if (TREE_CODE (name) != SSA_NAME)
421 return;
423 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
424 if (!SSA_NAME_DEF_STMT (name))
425 return;
427 if (is_gimple_assign (def_stmt)
428 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
430 tree cst = gimple_assign_rhs1 (def_stmt);
431 if (TREE_CODE (cst) == COMPLEX_CST)
433 tree imag = TREE_REALPART (cst);
434 int_range<2> tmp (imag, imag);
435 res.intersect (tmp);
440 // This function looks for situations when walking the use/def chains
441 // may provide additonal contextual range information not exposed on
442 // this statement.
444 static void
445 gimple_range_adjustment (vrange &res, const gimple *stmt)
447 switch (gimple_expr_code (stmt))
449 case POINTER_DIFF_EXPR:
450 adjust_pointer_diff_expr (as_a <irange> (res), stmt);
451 return;
453 case IMAGPART_EXPR:
454 adjust_imagpart_expr (res, stmt);
455 return;
457 case REALPART_EXPR:
458 adjust_realpart_expr (res, stmt);
459 return;
461 default:
462 break;
466 // Return the base of the RHS of an assignment.
468 static tree
469 gimple_range_base_of_assignment (const gimple *stmt)
471 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
472 tree op1 = gimple_assign_rhs1 (stmt);
473 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
474 return get_base_address (TREE_OPERAND (op1, 0));
475 return op1;
478 // Return the first operand of this statement if it is a valid operand
479 // supported by ranges, otherwise return NULL_TREE. Special case is
480 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
482 tree
483 gimple_range_operand1 (const gimple *stmt)
485 gcc_checking_assert (range_op_handler (stmt));
487 switch (gimple_code (stmt))
489 case GIMPLE_COND:
490 return gimple_cond_lhs (stmt);
491 case GIMPLE_ASSIGN:
493 tree base = gimple_range_base_of_assignment (stmt);
494 if (base && TREE_CODE (base) == MEM_REF)
496 // If the base address is an SSA_NAME, we return it
497 // here. This allows processing of the range of that
498 // name, while the rest of the expression is simply
499 // ignored. The code in range_ops will see the
500 // ADDR_EXPR and do the right thing.
501 tree ssa = TREE_OPERAND (base, 0);
502 if (TREE_CODE (ssa) == SSA_NAME)
503 return ssa;
505 return base;
507 default:
508 break;
510 return NULL;
513 // Return the second operand of statement STMT, otherwise return NULL_TREE.
515 tree
516 gimple_range_operand2 (const gimple *stmt)
518 gcc_checking_assert (range_op_handler (stmt));
520 switch (gimple_code (stmt))
522 case GIMPLE_COND:
523 return gimple_cond_rhs (stmt);
524 case GIMPLE_ASSIGN:
525 if (gimple_num_ops (stmt) >= 3)
526 return gimple_assign_rhs2 (stmt);
527 default:
528 break;
530 return NULL_TREE;
533 // Calculate a range for statement S and return it in R. If NAME is provided it
534 // represents the SSA_NAME on the LHS of the statement. It is only required
535 // if there is more than one lhs/output. If a range cannot
536 // be calculated, return false.
538 bool
539 fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
541 bool res = false;
542 // If name and S are specified, make sure it is an LHS of S.
543 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
544 name == gimple_get_lhs (s));
546 if (!name)
547 name = gimple_get_lhs (s);
549 // Process addresses.
550 if (gimple_code (s) == GIMPLE_ASSIGN
551 && gimple_assign_rhs_code (s) == ADDR_EXPR)
552 return range_of_address (as_a <irange> (r), s, src);
554 if (range_op_handler (s))
555 res = range_of_range_op (r, s, src);
556 else if (is_a<gphi *>(s))
557 res = range_of_phi (r, as_a<gphi *> (s), src);
558 else if (is_a<gcall *>(s))
559 res = range_of_call (r, as_a<gcall *> (s), src);
560 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
561 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
563 if (!res)
565 // If no name specified or range is unsupported, bail.
566 if (!name || !gimple_range_ssa_p (name))
567 return false;
568 // We don't understand the stmt, so return the global range.
569 gimple_range_global (r, name);
570 return true;
573 if (r.undefined_p ())
574 return true;
576 // We sometimes get compatible types copied from operands, make sure
577 // the correct type is being returned.
578 if (name && TREE_TYPE (name) != r.type ())
580 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
581 range_cast (r, TREE_TYPE (name));
583 return true;
586 // Calculate a range for range_op statement S and return it in R. If any
587 // If a range cannot be calculated, return false.
589 bool
590 fold_using_range::range_of_range_op (vrange &r, gimple *s, fur_source &src)
592 tree type = gimple_range_type (s);
593 if (!type)
594 return false;
595 range_op_handler handler (s);
596 gcc_checking_assert (handler);
598 tree lhs = gimple_get_lhs (s);
599 tree op1 = gimple_range_operand1 (s);
600 tree op2 = gimple_range_operand2 (s);
601 Value_Range range1 (TREE_TYPE (op1));
602 Value_Range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
604 if (src.get_operand (range1, op1))
606 if (!op2)
608 // Fold range, and register any dependency if available.
609 Value_Range r2 (type);
610 r2.set_varying (type);
611 handler.fold_range (r, type, range1, r2);
612 if (lhs && gimple_range_ssa_p (op1))
614 if (src.gori ())
615 src.gori ()->register_dependency (lhs, op1);
616 relation_kind rel;
617 rel = handler.lhs_op1_relation (r, range1, range1);
618 if (rel != VREL_VARYING)
619 src.register_relation (s, rel, lhs, op1);
622 else if (src.get_operand (range2, op2))
624 relation_kind rel = src.query_relation (op1, op2);
625 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
627 fprintf (dump_file, " folding with relation ");
628 print_generic_expr (dump_file, op1, TDF_SLIM);
629 print_relation (dump_file, rel);
630 print_generic_expr (dump_file, op2, TDF_SLIM);
631 fputc ('\n', dump_file);
633 // Fold range, and register any dependency if available.
634 handler.fold_range (r, type, range1, range2, rel);
635 if (irange::supports_type_p (type))
636 relation_fold_and_or (as_a <irange> (r), s, src);
637 if (lhs)
639 if (src.gori ())
641 src.gori ()->register_dependency (lhs, op1);
642 src.gori ()->register_dependency (lhs, op2);
644 if (gimple_range_ssa_p (op1))
646 rel = handler.lhs_op1_relation (r, range1, range2, rel);
647 if (rel != VREL_VARYING)
648 src.register_relation (s, rel, lhs, op1);
650 if (gimple_range_ssa_p (op2))
652 rel= handler.lhs_op2_relation (r, range1, range2, rel);
653 if (rel != VREL_VARYING)
654 src.register_relation (s, rel, lhs, op2);
657 // Check for an existing BB, as we maybe asked to fold an
658 // artificial statement not in the CFG.
659 else if (is_a<gcond *> (s) && gimple_bb (s))
661 basic_block bb = gimple_bb (s);
662 edge e0 = EDGE_SUCC (bb, 0);
663 edge e1 = EDGE_SUCC (bb, 1);
665 if (!single_pred_p (e0->dest))
666 e0 = NULL;
667 if (!single_pred_p (e1->dest))
668 e1 = NULL;
669 src.register_outgoing_edges (as_a<gcond *> (s),
670 as_a <irange> (r), e0, e1);
673 else
674 r.set_varying (type);
676 else
677 r.set_varying (type);
678 // Make certain range-op adjustments that aren't handled any other way.
679 gimple_range_adjustment (r, s);
680 return true;
683 // Calculate the range of an assignment containing an ADDR_EXPR.
684 // Return the range in R.
685 // If a range cannot be calculated, set it to VARYING and return true.
687 bool
688 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
690 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
691 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
693 bool strict_overflow_p;
694 tree expr = gimple_assign_rhs1 (stmt);
695 poly_int64 bitsize, bitpos;
696 tree offset;
697 machine_mode mode;
698 int unsignedp, reversep, volatilep;
699 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
700 &bitpos, &offset, &mode, &unsignedp,
701 &reversep, &volatilep);
704 if (base != NULL_TREE
705 && TREE_CODE (base) == MEM_REF
706 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
708 tree ssa = TREE_OPERAND (base, 0);
709 tree lhs = gimple_get_lhs (stmt);
710 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
711 src.gori ()->register_dependency (lhs, ssa);
712 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
713 src.get_operand (r, ssa);
714 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
716 poly_offset_int off = 0;
717 bool off_cst = false;
718 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
720 off = mem_ref_offset (base);
721 if (offset)
722 off += poly_offset_int::from (wi::to_poly_wide (offset),
723 SIGNED);
724 off <<= LOG2_BITS_PER_UNIT;
725 off += bitpos;
726 off_cst = true;
728 /* If &X->a is equal to X, the range of X is the result. */
729 if (off_cst && known_eq (off, 0))
730 return true;
731 else if (flag_delete_null_pointer_checks
732 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
734 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
735 allow going from non-NULL pointer to NULL. */
736 if (r.undefined_p () || !r.contains_p (build_zero_cst (r.type ())))
738 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
739 using POINTER_PLUS_EXPR if off_cst and just fall back to
740 this. */
741 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
742 return true;
745 /* If MEM_REF has a "positive" offset, consider it non-NULL
746 always, for -fdelete-null-pointer-checks also "negative"
747 ones. Punt for unknown offsets (e.g. variable ones). */
748 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
749 && off_cst
750 && known_ne (off, 0)
751 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
753 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
754 return true;
756 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
757 return true;
760 // Handle "= &a".
761 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
763 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
764 return true;
767 // Otherwise return varying.
768 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
769 return true;
772 // Calculate a range for phi statement S and return it in R.
773 // If a range cannot be calculated, return false.
775 bool
776 fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
778 tree phi_def = gimple_phi_result (phi);
779 tree type = gimple_range_type (phi);
780 Value_Range arg_range (type);
781 Value_Range equiv_range (type);
782 unsigned x;
784 if (!type)
785 return false;
787 // Track if all executable arguments are the same.
788 tree single_arg = NULL_TREE;
789 bool seen_arg = false;
791 // Start with an empty range, unioning in each argument's range.
792 r.set_undefined ();
793 for (x = 0; x < gimple_phi_num_args (phi); x++)
795 tree arg = gimple_phi_arg_def (phi, x);
796 // An argument that is the same as the def provides no new range.
797 if (arg == phi_def)
798 continue;
800 edge e = gimple_phi_arg_edge (phi, x);
802 // Get the range of the argument on its edge.
803 src.get_phi_operand (arg_range, arg, e);
805 if (!arg_range.undefined_p ())
807 // Register potential dependencies for stale value tracking.
808 // Likewise, if the incoming PHI argument is equivalent to this
809 // PHI definition, it provides no new info. Accumulate these ranges
810 // in case all arguments are equivalences.
811 if (src.query ()->query_relation (e, arg, phi_def, false) == VREL_EQ)
812 equiv_range.union_(arg_range);
813 else
814 r.union_ (arg_range);
816 if (gimple_range_ssa_p (arg) && src.gori ())
817 src.gori ()->register_dependency (phi_def, arg);
819 // Track if all arguments are the same.
820 if (!seen_arg)
822 seen_arg = true;
823 single_arg = arg;
825 else if (single_arg != arg)
826 single_arg = NULL_TREE;
829 // Once the value reaches varying, stop looking.
830 if (r.varying_p () && single_arg == NULL_TREE)
831 break;
834 // If all arguments were equivalences, use the equivalence ranges as no
835 // arguments were processed.
836 if (r.undefined_p () && !equiv_range.undefined_p ())
837 r = equiv_range;
839 // If the PHI boils down to a single effective argument, look at it.
840 if (single_arg)
842 // Symbolic arguments are equivalences.
843 if (gimple_range_ssa_p (single_arg))
844 src.register_relation (phi, VREL_EQ, phi_def, single_arg);
845 else if (src.get_operand (arg_range, single_arg)
846 && arg_range.singleton_p ())
848 // Numerical arguments that are a constant can be returned as
849 // the constant. This can help fold later cases where even this
850 // constant might have been UNDEFINED via an unreachable edge.
851 r = arg_range;
852 return true;
856 // If SCEV is available, query if this PHI has any knonwn values.
857 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
859 value_range loop_range;
860 class loop *l = loop_containing_stmt (phi);
861 if (l && loop_outer (l))
863 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
864 if (!loop_range.varying_p ())
866 if (dump_file && (dump_flags & TDF_DETAILS))
868 fprintf (dump_file, " Loops range found for ");
869 print_generic_expr (dump_file, phi_def, TDF_SLIM);
870 fprintf (dump_file, ": ");
871 loop_range.dump (dump_file);
872 fprintf (dump_file, " and calculated range :");
873 r.dump (dump_file);
874 fprintf (dump_file, "\n");
876 r.intersect (loop_range);
881 return true;
884 // Calculate a range for call statement S and return it in R.
885 // If a range cannot be calculated, return false.
887 bool
888 fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &src)
890 tree type = gimple_range_type (call);
891 if (!type)
892 return false;
894 tree lhs = gimple_call_lhs (call);
895 bool strict_overflow_p;
897 if (range_of_builtin_call (r, call, src))
899 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
900 r.set_nonnegative (type);
901 else if (gimple_call_nonnull_result_p (call)
902 || gimple_call_nonnull_arg (call))
903 r.set_nonzero (type);
904 else
905 r.set_varying (type);
907 // If there is an LHS, intersect that with what is known.
908 if (lhs)
910 Value_Range def (TREE_TYPE (lhs));
911 gimple_range_global (def, lhs);
912 r.intersect (def);
914 return true;
917 // Return the range of a __builtin_ubsan* in CALL and set it in R.
918 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
919 // MULT_EXPR).
921 void
922 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
923 tree_code code, fur_source &src)
925 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
926 || code == MULT_EXPR);
927 tree type = gimple_range_type (call);
928 range_op_handler op (code, type);
929 gcc_checking_assert (op);
930 int_range_max ir0, ir1;
931 tree arg0 = gimple_call_arg (call, 0);
932 tree arg1 = gimple_call_arg (call, 1);
933 src.get_operand (ir0, arg0);
934 src.get_operand (ir1, arg1);
935 // Check for any relation between arg0 and arg1.
936 relation_kind relation = src.query_relation (arg0, arg1);
938 bool saved_flag_wrapv = flag_wrapv;
939 // Pretend the arithmetic is wrapping. If there is any overflow,
940 // we'll complain, but will actually do wrapping operation.
941 flag_wrapv = 1;
942 op.fold_range (r, type, ir0, ir1, relation);
943 flag_wrapv = saved_flag_wrapv;
945 // If for both arguments vrp_valueize returned non-NULL, this should
946 // have been already folded and if not, it wasn't folded because of
947 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
948 if (r.singleton_p ())
949 r.set_varying (type);
952 // Return TRUE if we recognize the target character set and return the
953 // range for lower case and upper case letters.
955 static bool
956 get_letter_range (tree type, irange &lowers, irange &uppers)
958 // ASCII
959 int a = lang_hooks.to_target_charset ('a');
960 int z = lang_hooks.to_target_charset ('z');
961 int A = lang_hooks.to_target_charset ('A');
962 int Z = lang_hooks.to_target_charset ('Z');
964 if ((z - a == 25) && (Z - A == 25))
966 lowers = int_range<2> (build_int_cst (type, a), build_int_cst (type, z));
967 uppers = int_range<2> (build_int_cst (type, A), build_int_cst (type, Z));
968 return true;
970 // Unknown character set.
971 return false;
974 // For a builtin in CALL, return a range in R if known and return
975 // TRUE. Otherwise return FALSE.
977 bool
978 fold_using_range::range_of_builtin_call (vrange &r, gcall *call,
979 fur_source &src)
981 combined_fn func = gimple_call_combined_fn (call);
982 if (func == CFN_LAST)
983 return false;
985 tree type = gimple_range_type (call);
986 gcc_checking_assert (type);
988 if (irange::supports_type_p (type))
989 return range_of_builtin_int_call (as_a <irange> (r), call, src);
991 return false;
994 bool
995 fold_using_range::range_of_builtin_int_call (irange &r, gcall *call,
996 fur_source &src)
998 combined_fn func = gimple_call_combined_fn (call);
999 if (func == CFN_LAST)
1000 return false;
1002 tree type = gimple_range_type (call);
1003 tree arg;
1004 int mini, maxi, zerov = 0, prec;
1005 scalar_int_mode mode;
1007 switch (func)
1009 case CFN_BUILT_IN_CONSTANT_P:
1010 arg = gimple_call_arg (call, 0);
1011 if (src.get_operand (r, arg) && r.singleton_p ())
1013 r.set (build_one_cst (type), build_one_cst (type));
1014 return true;
1016 if (cfun->after_inlining)
1018 r.set_zero (type);
1019 // r.equiv_clear ();
1020 return true;
1022 break;
1024 case CFN_BUILT_IN_TOUPPER:
1026 arg = gimple_call_arg (call, 0);
1027 // If the argument isn't compatible with the LHS, do nothing.
1028 if (!range_compatible_p (type, TREE_TYPE (arg)))
1029 return false;
1030 if (!src.get_operand (r, arg))
1031 return false;
1033 int_range<3> lowers;
1034 int_range<3> uppers;
1035 if (!get_letter_range (type, lowers, uppers))
1036 return false;
1038 // Return the range passed in without any lower case characters,
1039 // but including all the upper case ones.
1040 lowers.invert ();
1041 r.intersect (lowers);
1042 r.union_ (uppers);
1043 return true;
1046 case CFN_BUILT_IN_TOLOWER:
1048 arg = gimple_call_arg (call, 0);
1049 // If the argument isn't compatible with the LHS, do nothing.
1050 if (!range_compatible_p (type, TREE_TYPE (arg)))
1051 return false;
1052 if (!src.get_operand (r, arg))
1053 return false;
1055 int_range<3> lowers;
1056 int_range<3> uppers;
1057 if (!get_letter_range (type, lowers, uppers))
1058 return false;
1060 // Return the range passed in without any upper case characters,
1061 // but including all the lower case ones.
1062 uppers.invert ();
1063 r.intersect (uppers);
1064 r.union_ (lowers);
1065 return true;
1068 CASE_CFN_FFS:
1069 CASE_CFN_POPCOUNT:
1070 // __builtin_ffs* and __builtin_popcount* return [0, prec].
1071 arg = gimple_call_arg (call, 0);
1072 prec = TYPE_PRECISION (TREE_TYPE (arg));
1073 mini = 0;
1074 maxi = prec;
1075 src.get_operand (r, arg);
1076 // If arg is non-zero, then ffs or popcount are non-zero.
1077 if (!range_includes_zero_p (&r))
1078 mini = 1;
1079 // If some high bits are known to be zero, decrease the maximum.
1080 if (!r.undefined_p ())
1082 if (TYPE_SIGN (r.type ()) == SIGNED)
1083 range_cast (r, unsigned_type_for (r.type ()));
1084 wide_int max = r.upper_bound ();
1085 maxi = wi::floor_log2 (max) + 1;
1087 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1088 return true;
1090 CASE_CFN_PARITY:
1091 r.set (build_zero_cst (type), build_one_cst (type));
1092 return true;
1094 CASE_CFN_CLZ:
1095 // __builtin_c[lt]z* return [0, prec-1], except when the
1096 // argument is 0, but that is undefined behavior.
1098 // For __builtin_c[lt]z* consider argument of 0 always undefined
1099 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
1100 arg = gimple_call_arg (call, 0);
1101 prec = TYPE_PRECISION (TREE_TYPE (arg));
1102 mini = 0;
1103 maxi = prec - 1;
1104 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1105 if (gimple_call_internal_p (call))
1107 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1108 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1110 // Only handle the single common value.
1111 if (zerov == prec)
1112 maxi = prec;
1113 else
1114 // Magic value to give up, unless we can prove arg is non-zero.
1115 mini = -2;
1119 src.get_operand (r, arg);
1120 // From clz of minimum we can compute result maximum.
1121 if (!r.undefined_p ())
1123 // From clz of minimum we can compute result maximum.
1124 if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
1126 maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
1127 if (mini == -2)
1128 mini = 0;
1130 else if (!range_includes_zero_p (&r))
1132 mini = 0;
1133 maxi = prec - 1;
1135 if (mini == -2)
1136 break;
1137 // From clz of maximum we can compute result minimum.
1138 wide_int max = r.upper_bound ();
1139 int newmini = prec - 1 - wi::floor_log2 (max);
1140 if (max == 0)
1142 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
1143 // return [prec, prec], otherwise ignore the range.
1144 if (maxi == prec)
1145 mini = prec;
1147 else
1148 mini = newmini;
1150 if (mini == -2)
1151 break;
1152 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1153 return true;
1155 CASE_CFN_CTZ:
1156 // __builtin_ctz* return [0, prec-1], except for when the
1157 // argument is 0, but that is undefined behavior.
1159 // For __builtin_ctz* consider argument of 0 always undefined
1160 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
1161 arg = gimple_call_arg (call, 0);
1162 prec = TYPE_PRECISION (TREE_TYPE (arg));
1163 mini = 0;
1164 maxi = prec - 1;
1165 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1166 if (gimple_call_internal_p (call))
1168 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1169 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1171 // Handle only the two common values.
1172 if (zerov == -1)
1173 mini = -1;
1174 else if (zerov == prec)
1175 maxi = prec;
1176 else
1177 // Magic value to give up, unless we can prove arg is non-zero.
1178 mini = -2;
1181 src.get_operand (r, arg);
1182 if (!r.undefined_p ())
1184 // If arg is non-zero, then use [0, prec - 1].
1185 if (!range_includes_zero_p (&r))
1187 mini = 0;
1188 maxi = prec - 1;
1190 // If some high bits are known to be zero, we can decrease
1191 // the maximum.
1192 wide_int max = r.upper_bound ();
1193 if (max == 0)
1195 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1196 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1197 // Otherwise ignore the range.
1198 if (mini == -1)
1199 maxi = -1;
1200 else if (maxi == prec)
1201 mini = prec;
1203 // If value at zero is prec and 0 is in the range, we can't lower
1204 // the upper bound. We could create two separate ranges though,
1205 // [0,floor_log2(max)][prec,prec] though.
1206 else if (maxi != prec)
1207 maxi = wi::floor_log2 (max);
1209 if (mini == -2)
1210 break;
1211 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1212 return true;
1214 CASE_CFN_CLRSB:
1215 arg = gimple_call_arg (call, 0);
1216 prec = TYPE_PRECISION (TREE_TYPE (arg));
1217 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
1218 return true;
1219 case CFN_UBSAN_CHECK_ADD:
1220 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
1221 return true;
1222 case CFN_UBSAN_CHECK_SUB:
1223 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
1224 return true;
1225 case CFN_UBSAN_CHECK_MUL:
1226 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
1227 return true;
1229 case CFN_GOACC_DIM_SIZE:
1230 case CFN_GOACC_DIM_POS:
1231 // Optimizing these two internal functions helps the loop
1232 // optimizer eliminate outer comparisons. Size is [1,N]
1233 // and pos is [0,N-1].
1235 bool is_pos = func == CFN_GOACC_DIM_POS;
1236 int axis = oacc_get_ifn_dim_arg (call);
1237 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1238 if (!size)
1239 // If it's dynamic, the backend might know a hardware limitation.
1240 size = targetm.goacc.dim_limit (axis);
1242 r.set (build_int_cst (type, is_pos ? 0 : 1),
1243 size
1244 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1245 return true;
1248 case CFN_BUILT_IN_STRLEN:
1249 if (tree lhs = gimple_call_lhs (call))
1250 if (ptrdiff_type_node
1251 && (TYPE_PRECISION (ptrdiff_type_node)
1252 == TYPE_PRECISION (TREE_TYPE (lhs))))
1254 tree type = TREE_TYPE (lhs);
1255 tree max = vrp_val_max (ptrdiff_type_node);
1256 wide_int wmax
1257 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1258 tree range_min = build_zero_cst (type);
1259 // To account for the terminating NULL, the maximum length
1260 // is one less than the maximum array size, which in turn
1261 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1262 // smaller than the former type).
1263 // FIXME: Use max_object_size() - 1 here.
1264 tree range_max = wide_int_to_tree (type, wmax - 2);
1265 r.set (range_min, range_max);
1266 return true;
1268 break;
1269 default:
1270 break;
1272 return false;
1276 // Calculate a range for COND_EXPR statement S and return it in R.
1277 // If a range cannot be calculated, return false.
1279 bool
1280 fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1282 tree cond = gimple_assign_rhs1 (s);
1283 tree op1 = gimple_assign_rhs2 (s);
1284 tree op2 = gimple_assign_rhs3 (s);
1286 tree type = gimple_range_type (s);
1287 if (!type)
1288 return false;
1290 Value_Range range1 (TREE_TYPE (op1));
1291 Value_Range range2 (TREE_TYPE (op2));
1292 Value_Range cond_range (TREE_TYPE (cond));
1293 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1294 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1295 src.get_operand (cond_range, cond);
1296 src.get_operand (range1, op1);
1297 src.get_operand (range2, op2);
1299 // Try to see if there is a dependence between the COND and either operand
1300 if (src.gori ())
1301 if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1302 if (dump_file && (dump_flags & TDF_DETAILS))
1304 fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1305 range1.dump(dump_file);
1306 fprintf (dump_file, " and Range op2: ");
1307 range2.dump(dump_file);
1308 fprintf (dump_file, "\n");
1311 // If the condition is known, choose the appropriate expression.
1312 if (cond_range.singleton_p ())
1314 // False, pick second operand.
1315 if (cond_range.zero_p ())
1316 r = range2;
1317 else
1318 r = range1;
1320 else
1322 r = range1;
1323 r.union_ (range2);
1325 gcc_checking_assert (r.undefined_p ()
1326 || range_compatible_p (r.type (), type));
1327 return true;
1330 // If SCEV has any information about phi node NAME, return it as a range in R.
1332 void
1333 fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
1334 class loop *l, gphi *phi,
1335 fur_source &src)
1337 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1338 tree min, max, type = TREE_TYPE (name);
1339 if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
1341 if (TREE_CODE (min) != INTEGER_CST)
1343 if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
1344 min = wide_int_to_tree (type, r.lower_bound ());
1345 else
1346 min = vrp_val_min (type);
1348 if (TREE_CODE (max) != INTEGER_CST)
1350 if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
1351 max = wide_int_to_tree (type, r.upper_bound ());
1352 else
1353 max = vrp_val_max (type);
1355 r.set (min, max);
1357 else
1358 r.set_varying (type);
1361 // -----------------------------------------------------------------------
1363 // Check if an && or || expression can be folded based on relations. ie
1364 // c_2 = a_6 > b_7
1365 // c_3 = a_6 < b_7
1366 // c_4 = c_2 && c_3
1367 // c_2 and c_3 can never be true at the same time,
1368 // Therefore c_4 can always resolve to false based purely on the relations.
1370 void
1371 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1372 fur_source &src)
1374 // No queries or already folded.
1375 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1376 return;
1378 // Only care about AND and OR expressions.
1379 enum tree_code code = gimple_expr_code (s);
1380 bool is_and = false;
1381 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1382 is_and = true;
1383 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1384 return;
1386 tree lhs = gimple_get_lhs (s);
1387 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1388 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1390 // Deal with || and && only when there is a full set of symbolics.
1391 if (!lhs || !ssa1 || !ssa2
1392 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1393 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1394 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1395 return;
1397 // Now we know its a boolean AND or OR expression with boolean operands.
1398 // Ideally we search dependencies for common names, and see what pops out.
1399 // until then, simply try to resolve direct dependencies.
1401 // Both names will need to have 2 direct dependencies.
1402 tree ssa1_dep2 = src.gori ()->depend2 (ssa1);
1403 tree ssa2_dep2 = src.gori ()->depend2 (ssa2);
1404 if (!ssa1_dep2 || !ssa2_dep2)
1405 return;
1407 tree ssa1_dep1 = src.gori ()->depend1 (ssa1);
1408 tree ssa2_dep1 = src.gori ()->depend1 (ssa2);
1409 // Make sure they are the same dependencies, and detect the order of the
1410 // relationship.
1411 bool reverse_op2 = true;
1412 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1413 reverse_op2 = false;
1414 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1415 return;
1417 range_op_handler handler1 (SSA_NAME_DEF_STMT (ssa1));
1418 range_op_handler handler2 (SSA_NAME_DEF_STMT (ssa2));
1420 // If either handler is not present, no relation is found.
1421 if (!handler1 || !handler2)
1422 return;
1424 int_range<2> bool_one (boolean_true_node, boolean_true_node);
1426 relation_kind relation1 = handler1.op1_op2_relation (bool_one);
1427 relation_kind relation2 = handler2.op1_op2_relation (bool_one);
1428 if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1429 return;
1431 if (reverse_op2)
1432 relation2 = relation_negate (relation2);
1434 // x && y is false if the relation intersection of the true cases is NULL.
1435 if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1436 lhs_range = int_range<2> (boolean_false_node, boolean_false_node);
1437 // x || y is true if the union of the true cases is NO-RELATION..
1438 // ie, one or the other being true covers the full range of possibilties.
1439 else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1440 lhs_range = bool_one;
1441 else
1442 return;
1444 range_cast (lhs_range, TREE_TYPE (lhs));
1445 if (dump_file && (dump_flags & TDF_DETAILS))
1447 fprintf (dump_file, " Relation adjustment: ");
1448 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1449 fprintf (dump_file, " and ");
1450 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1451 fprintf (dump_file, " combine to produce ");
1452 lhs_range.dump (dump_file);
1453 fputc ('\n', dump_file);
1456 return;
1459 // Register any outgoing edge relations from a conditional branch.
1461 void
1462 fur_source::register_outgoing_edges (gcond *s, irange &lhs_range, edge e0, edge e1)
1464 int_range<2> e0_range, e1_range;
1465 tree name;
1466 basic_block bb = gimple_bb (s);
1468 if (e0)
1470 // If this edge is never taken, ignore it.
1471 gcond_edge_range (e0_range, e0);
1472 e0_range.intersect (lhs_range);
1473 if (e0_range.undefined_p ())
1474 e0 = NULL;
1478 if (e1)
1480 // If this edge is never taken, ignore it.
1481 gcond_edge_range (e1_range, e1);
1482 e1_range.intersect (lhs_range);
1483 if (e1_range.undefined_p ())
1484 e1 = NULL;
1487 if (!e0 && !e1)
1488 return;
1490 // First, register the gcond itself. This will catch statements like
1491 // if (a_2 < b_5)
1492 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1493 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1494 if (ssa1 && ssa2)
1496 range_op_handler handler (s);
1497 gcc_checking_assert (handler);
1498 if (e0)
1500 relation_kind relation = handler.op1_op2_relation (e0_range);
1501 if (relation != VREL_VARYING)
1502 register_relation (e0, relation, ssa1, ssa2);
1504 if (e1)
1506 relation_kind relation = handler.op1_op2_relation (e1_range);
1507 if (relation != VREL_VARYING)
1508 register_relation (e1, relation, ssa1, ssa2);
1512 // Outgoing relations of GORI exports require a gori engine.
1513 if (!gori ())
1514 return;
1516 // Now look for other relations in the exports. This will find stmts
1517 // leading to the condition such as:
1518 // c_2 = a_4 < b_7
1519 // if (c_2)
1520 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
1522 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1523 continue;
1524 gimple *stmt = SSA_NAME_DEF_STMT (name);
1525 range_op_handler handler (stmt);
1526 if (!handler)
1527 continue;
1528 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
1529 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
1530 Value_Range r (TREE_TYPE (name));
1531 if (ssa1 && ssa2)
1533 if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
1534 && r.singleton_p ())
1536 relation_kind relation = handler.op1_op2_relation (r);
1537 if (relation != VREL_VARYING)
1538 register_relation (e0, relation, ssa1, ssa2);
1540 if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
1541 && r.singleton_p ())
1543 relation_kind relation = handler.op1_op2_relation (r);
1544 if (relation != VREL_VARYING)
1545 register_relation (e1, relation, ssa1, ssa2);