1/* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "backend.h"
26#include "insn-codes.h"
27#include "tree.h"
28#include "gimple.h"
29#include "ssa.h"
30#include "gimple-pretty-print.h"
31#include "optabs-tree.h"
32#include "gimple-iterator.h"
33#include "gimple-fold.h"
34#include "wide-int.h"
35#include "fold-const.h"
36#include "case-cfn-macros.h"
37#include "omp-general.h"
38#include "cfgloop.h"
39#include "tree-ssa-loop.h"
40#include "tree-scalar-evolution.h"
41#include "langhooks.h"
42#include "vr-values.h"
43#include "range.h"
44#include "value-query.h"
45#include "gimple-range-op.h"
46#include "gimple-range.h"
47#include "cgraph.h"
48#include "alloc-pool.h"
49#include "symbol-summary.h"
50#include "ipa-utils.h"
51#include "sreal.h"
52#include "ipa-cp.h"
53#include "ipa-prop.h"
54#include "rtl.h"
55// Construct a fur_source, and set the m_query field.
56
57fur_source::fur_source (range_query *q)
58{
59 if (q)
60 m_query = q;
61 else
62 m_query = get_range_query (cfun);
63 m_depend_p = false;
64}
65
66// Invoke range_of_expr on EXPR.
67
68bool
69fur_source::get_operand (vrange &r, tree expr)
70{
71 return m_query->range_of_expr (r, expr);
72}
73
74// Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
75// range_query to get the range on the edge.
76
77bool
78fur_source::get_phi_operand (vrange &r, tree expr, edge e)
79{
80 return m_query->range_on_edge (r, e, expr);
81}
82
83// Default is no relation.
84
85relation_kind
86fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
87 tree op2 ATTRIBUTE_UNUSED)
88{
89 return VREL_VARYING;
90}
91
92// Default registers nothing.
93
94void
95fur_source::register_relation (gimple *s ATTRIBUTE_UNUSED,
96 relation_kind k ATTRIBUTE_UNUSED,
97 tree op1 ATTRIBUTE_UNUSED,
98 tree op2 ATTRIBUTE_UNUSED)
99{
100}
101
102// Default registers nothing.
103
104void
105fur_source::register_relation (edge e ATTRIBUTE_UNUSED,
106 relation_kind k ATTRIBUTE_UNUSED,
107 tree op1 ATTRIBUTE_UNUSED,
108 tree op2 ATTRIBUTE_UNUSED)
109{
110}
111
112// Get the value of EXPR on edge m_edge.
113
114bool
115fur_edge::get_operand (vrange &r, tree expr)
116{
117 return m_query->range_on_edge (r, m_edge, expr);
118}
119
120// Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
121// range_query to get the range on the edge.
122
123bool
124fur_edge::get_phi_operand (vrange &r, tree expr, edge e)
125{
126 // Edge to edge recalculations not supported yet, until we sort it out.
127 gcc_checking_assert (e == m_edge);
128 return m_query->range_on_edge (r, e, expr);
129}
130
131// Instantiate a stmt based fur_source.
132
133fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
134{
135 m_stmt = s;
136}
137
138// Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
139
140bool
141fur_stmt::get_operand (vrange &r, tree expr)
142{
143 return m_query->range_of_expr (r, expr, m_stmt);
144}
145
146// Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
147// range_query to get the range on the edge.
148
149bool
150fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
151{
152 // Pick up the range of expr from edge E.
153 fur_edge e_src (e, m_query);
154 return e_src.get_operand (r, expr);
155}
156
157// Return relation based from m_stmt.
158
159relation_kind
160fur_stmt::query_relation (tree op1, tree op2)
161{
162 return m_query->relation ().query (s: m_stmt, ssa1: op1, ssa2: op2);
163}
164
165// Instantiate a stmt based fur_source with a GORI object.
166
167
168fur_depend::fur_depend (gimple *s, range_query *q)
169 : fur_stmt (s, q)
170{
171 m_depend_p = true;
172}
173
174// Register a relation on a stmt if there is an oracle.
175
176void
177fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
178{
179 m_query->relation ().record (s, k, op1, op2);
180}
181
182// Register a relation on an edge if there is an oracle.
183
184void
185fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
186{
187 m_query->relation ().record (e, k, op1, op2);
188}
189
190// This version of fur_source will pick a range up from a list of ranges
191// supplied by the caller.
192
193class fur_list : public fur_source
194{
195public:
196 fur_list (vrange &r1, range_query *q = NULL);
197 fur_list (vrange &r1, vrange &r2, range_query *q = NULL);
198 fur_list (unsigned num, vrange **list, range_query *q = NULL);
199 virtual bool get_operand (vrange &r, tree expr) override;
200 virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
201private:
202 vrange *m_local[2];
203 vrange **m_list;
204 unsigned m_index;
205 unsigned m_limit;
206};
207
208// One range supplied for unary operations.
209
210fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
211{
212 m_list = m_local;
213 m_index = 0;
214 m_limit = 1;
215 m_local[0] = &r1;
216}
217
218// Two ranges supplied for binary operations.
219
220fur_list::fur_list (vrange &r1, vrange &r2, range_query *q) : fur_source (q)
221{
222 m_list = m_local;
223 m_index = 0;
224 m_limit = 2;
225 m_local[0] = &r1;
226 m_local[1] = &r2;
227}
228
229// Arbitrary number of ranges in a vector.
230
231fur_list::fur_list (unsigned num, vrange **list, range_query *q)
232 : fur_source (q)
233{
234 m_list = list;
235 m_index = 0;
236 m_limit = num;
237}
238
239// Get the next operand from the vector, ensure types are compatible.
240
241bool
242fur_list::get_operand (vrange &r, tree expr)
243{
244 // Do not use the vector for non-ssa-names, or if it has been emptied.
245 if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
246 return m_query->range_of_expr (r, expr);
247 r = *m_list[m_index++];
248 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
249 return true;
250}
251
252// This will simply pick the next operand from the vector.
253bool
254fur_list::get_phi_operand (vrange &r, tree expr, edge e ATTRIBUTE_UNUSED)
255{
256 return get_operand (r, expr);
257}
258
259// Fold stmt S into range R using R1 as the first operand.
260
261bool
262fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
263{
264 fold_using_range f;
265 fur_list src (r1, q);
266 return f.fold_stmt (r, s, src);
267}
268
269// Fold stmt S into range R using R1 and R2 as the first two operands.
270
271bool
272fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2, range_query *q)
273{
274 fold_using_range f;
275 fur_list src (r1, r2, q);
276 return f.fold_stmt (r, s, src);
277}
278
279// Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
280// operands encountered.
281
282bool
283fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector,
284 range_query *q)
285{
286 fold_using_range f;
287 fur_list src (num_elements, vector, q);
288 return f.fold_stmt (r, s, src);
289}
290
291// Fold stmt S into range R using range query Q.
292
293bool
294fold_range (vrange &r, gimple *s, range_query *q)
295{
296 fold_using_range f;
297 fur_stmt src (s, q);
298 return f.fold_stmt (r, s, src);
299}
300
301// Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
302
303bool
304fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
305{
306 fold_using_range f;
307 fur_edge src (on_edge, q);
308 return f.fold_stmt (r, s, src);
309}
310
311// Calculate op1 on statetemt S with LHS into range R using range query Q
312// to resolve any other operands.
313
314bool
315op1_range (vrange &r, gimple *s, const vrange &lhs, range_query *q)
316{
317 gimple_range_op_handler handler (s);
318 if (!handler)
319 return false;
320
321 fur_stmt src (s, q);
322
323 tree op2_expr = handler.operand2 ();
324 if (!op2_expr)
325 return handler.calc_op1 (r, lhs_range: lhs);
326
327 value_range op2 (TREE_TYPE (op2_expr));
328 if (!src.get_operand (r&: op2, expr: op2_expr))
329 return false;
330
331 return handler.calc_op1 (r, lhs_range: lhs, op2_range: op2);
332}
333
334// Calculate op1 on statetemt S into range R using range query Q.
335// LHS is set to VARYING in this case.
336
337bool
338op1_range (vrange &r, gimple *s, range_query *q)
339{
340 tree lhs_type = gimple_range_type (s);
341 if (!lhs_type)
342 return false;
343 value_range lhs_range;
344 lhs_range.set_varying (lhs_type);
345 return op1_range (r, s, lhs: lhs_range, q);
346}
347
348// Calculate op2 on statetemt S with LHS into range R using range query Q
349// to resolve any other operands.
350
351bool
352op2_range (vrange &r, gimple *s, const vrange &lhs, range_query *q)
353{
354
355 gimple_range_op_handler handler (s);
356 if (!handler)
357 return false;
358
359 fur_stmt src (s, q);
360
361 value_range op1 (TREE_TYPE (handler.operand1 ()));
362 if (!src.get_operand (r&: op1, expr: handler.operand1 ()))
363 return false;
364
365 return handler.calc_op2 (r, lhs_range: lhs, op1_range: op1);
366}
367
368// Calculate op2 on statetemt S into range R using range query Q.
369// LHS is set to VARYING in this case.
370
371bool
372op2_range (vrange &r, gimple *s, range_query *q)
373{
374 tree lhs_type = gimple_range_type (s);
375 if (!lhs_type)
376 return false;
377 value_range lhs_range;
378 lhs_range.set_varying (lhs_type);
379 return op2_range (r, s, lhs: lhs_range, q);
380}
381
382// Provide a fur_source which can be used to determine any relations on
383// a statement. It manages the callback from fold_using_ranges to determine
384// a relation_trio for a statement.
385
386class fur_relation : public fur_stmt
387{
388public:
389 fur_relation (gimple *s, range_query *q = NULL);
390 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
391 tree op2);
392 virtual void register_relation (edge e, relation_kind k, tree op1,
393 tree op2);
394 relation_trio trio() const;
395private:
396 relation_kind def_op1, def_op2, op1_op2;
397};
398
399fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
400{
401 def_op1 = def_op2 = op1_op2 = VREL_VARYING;
402}
403
404// Construct a trio from what is known.
405
406relation_trio
407fur_relation::trio () const
408{
409 return relation_trio (def_op1, def_op2, op1_op2);
410}
411
412// Don't support edges, but avoid a compiler warning by providing the routine.
413
414void
415fur_relation::register_relation (edge, relation_kind, tree, tree)
416{
417}
418
419// Register relation K between OP1 and OP2 on STMT.
420
421void
422fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
423 tree op2)
424{
425 tree lhs = gimple_get_lhs (stmt);
426 tree a1 = NULL_TREE;
427 tree a2 = NULL_TREE;
428 switch (gimple_code (g: stmt))
429 {
430 case GIMPLE_COND:
431 a1 = gimple_cond_lhs (gs: stmt);
432 a2 = gimple_cond_rhs (gs: stmt);
433 break;
434 case GIMPLE_ASSIGN:
435 a1 = gimple_assign_rhs1 (gs: stmt);
436 if (gimple_num_ops (gs: stmt) >= 3)
437 a2 = gimple_assign_rhs2 (gs: stmt);
438 break;
439 default:
440 break;
441 }
442 // STMT is of the form LHS = A1 op A2, now map the relation to these
443 // operands, if possible.
444 if (op1 == lhs)
445 {
446 if (op2 == a1)
447 def_op1 = k;
448 else if (op2 == a2)
449 def_op2 = k;
450 }
451 else if (op2 == lhs)
452 {
453 if (op1 == a1)
454 def_op1 = relation_swap (r: k);
455 else if (op1 == a2)
456 def_op2 = relation_swap (r: k);
457 }
458 else
459 {
460 if (op1 == a1 && op2 == a2)
461 op1_op2 = k;
462 else if (op2 == a1 && op1 == a2)
463 op1_op2 = relation_swap (r: k);
464 }
465}
466
467// Return the relation trio for stmt S using query Q.
468
469relation_trio
470fold_relations (gimple *s, range_query *q)
471{
472 fold_using_range f;
473 fur_relation src (s, q);
474 tree lhs = gimple_range_ssa_p (exp: gimple_get_lhs (s));
475 if (lhs)
476 {
477 value_range vr(TREE_TYPE (lhs));
478 if (f.fold_stmt (r&: vr, s, src))
479 return src.trio ();
480 }
481 return TRIO_VARYING;
482}
483
484// -------------------------------------------------------------------------
485
486// Adjust the range for a pointer difference where the operands came
487// from a memchr.
488//
489// This notices the following sequence:
490//
491// def = __builtin_memchr (arg, 0, sz)
492// n = def - arg
493//
494// The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
495
496static void
497adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
498{
499 tree op0 = gimple_assign_rhs1 (gs: diff_stmt);
500 tree op1 = gimple_assign_rhs2 (gs: diff_stmt);
501 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
502 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
503 gimple *call;
504
505 if (TREE_CODE (op0) == SSA_NAME
506 && TREE_CODE (op1) == SSA_NAME
507 && (call = SSA_NAME_DEF_STMT (op0))
508 && is_gimple_call (gs: call)
509 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
510 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
511 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
512 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
513 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
514 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
515 && vrp_operand_equal_p (op1, gimple_call_arg (gs: call, index: 0))
516 && integer_zerop (gimple_call_arg (gs: call, index: 1)))
517 {
518 wide_int maxm1 = irange_val_max (ptrdiff_type_node) - 1;
519 res.intersect (int_range<2> (ptrdiff_type_node,
520 wi::zero (TYPE_PRECISION (ptrdiff_type_node)),
521 maxm1));
522 }
523}
524
525// Adjust the range for an IMAGPART_EXPR.
526
527static void
528adjust_imagpart_expr (vrange &res, const gimple *stmt)
529{
530 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
531
532 if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
533 return;
534
535 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
536 if (is_gimple_call (gs: def_stmt) && gimple_call_internal_p (gs: def_stmt))
537 {
538 switch (gimple_call_internal_fn (gs: def_stmt))
539 {
540 case IFN_ADD_OVERFLOW:
541 case IFN_SUB_OVERFLOW:
542 case IFN_MUL_OVERFLOW:
543 case IFN_UADDC:
544 case IFN_USUBC:
545 case IFN_ATOMIC_COMPARE_EXCHANGE:
546 {
547 int_range<2> r;
548 r.set_varying (boolean_type_node);
549 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
550 range_cast (r, type);
551 res.intersect (r);
552 }
553 default:
554 break;
555 }
556 return;
557 }
558 if (is_gimple_assign (gs: def_stmt)
559 && gimple_assign_rhs_code (gs: def_stmt) == COMPLEX_CST)
560 {
561 tree cst = gimple_assign_rhs1 (gs: def_stmt);
562 if (TREE_CODE (cst) == COMPLEX_CST
563 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
564 {
565 wide_int w = wi::to_wide (TREE_IMAGPART (cst));
566 int_range<1> imag (TREE_TYPE (TREE_IMAGPART (cst)), w, w);
567 res.intersect (imag);
568 }
569 }
570}
571
572// Adjust the range for a REALPART_EXPR.
573
574static void
575adjust_realpart_expr (vrange &res, const gimple *stmt)
576{
577 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
578
579 if (TREE_CODE (name) != SSA_NAME)
580 return;
581
582 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
583 if (!SSA_NAME_DEF_STMT (name))
584 return;
585
586 if (is_gimple_assign (gs: def_stmt)
587 && gimple_assign_rhs_code (gs: def_stmt) == COMPLEX_CST)
588 {
589 tree cst = gimple_assign_rhs1 (gs: def_stmt);
590 if (TREE_CODE (cst) == COMPLEX_CST
591 && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
592 {
593 wide_int imag = wi::to_wide (TREE_REALPART (cst));
594 int_range<2> tmp (TREE_TYPE (TREE_REALPART (cst)), imag, imag);
595 res.intersect (tmp);
596 }
597 }
598}
599
600// This function looks for situations when walking the use/def chains
601// may provide additional contextual range information not exposed on
602// this statement.
603
604static void
605gimple_range_adjustment (vrange &res, const gimple *stmt)
606{
607 switch (gimple_expr_code (stmt))
608 {
609 case POINTER_DIFF_EXPR:
610 adjust_pointer_diff_expr (res&: as_a <irange> (v&: res), diff_stmt: stmt);
611 return;
612
613 case IMAGPART_EXPR:
614 adjust_imagpart_expr (res, stmt);
615 return;
616
617 case REALPART_EXPR:
618 adjust_realpart_expr (res, stmt);
619 return;
620
621 default:
622 break;
623 }
624}
625
626// Calculate a range for statement S and return it in R. If NAME is provided it
627// represents the SSA_NAME on the LHS of the statement. It is only required
628// if there is more than one lhs/output. If a range cannot
629// be calculated, return false.
630
631bool
632fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
633{
634 bool res = false;
635 // If name and S are specified, make sure it is an LHS of S.
636 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
637 name == gimple_get_lhs (s));
638
639 if (!name)
640 name = gimple_get_lhs (s);
641
642 // Process addresses.
643 if (gimple_code (g: s) == GIMPLE_ASSIGN
644 && gimple_assign_rhs_code (gs: s) == ADDR_EXPR)
645 return range_of_address (r&: as_a <prange> (v&: r), s, src);
646
647 gimple_range_op_handler handler (s);
648 if (handler)
649 res = range_of_range_op (r, handler, src);
650 else if (is_a<gphi *>(p: s))
651 res = range_of_phi (r, phi: as_a<gphi *> (p: s), src);
652 else if (is_a<gcall *>(p: s))
653 res = range_of_call (r, call: as_a<gcall *> (p: s), src);
654 else if (is_a<gassign *> (p: s) && gimple_assign_rhs_code (gs: s) == COND_EXPR)
655 res = range_of_cond_expr (r, cond: as_a<gassign *> (p: s), src);
656
657 // If the result is varying, check for basic nonnegativeness.
658 // Specifically this helps for now with strict enum in cases like
659 // g++.dg/warn/pr33738.C.
660 bool so_p;
661 if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
662 && gimple_stmt_nonnegative_warnv_p (s, &so_p))
663 r.set_nonnegative (r.type ());
664
665 if (!res)
666 {
667 // If no name specified or range is unsupported, bail.
668 if (!name || !gimple_range_ssa_p (exp: name))
669 return false;
670 // We don't understand the stmt, so return the global range.
671 gimple_range_global (v&: r, name);
672 return true;
673 }
674
675 if (r.undefined_p ())
676 return true;
677
678 // We sometimes get compatible types copied from operands, make sure
679 // the correct type is being returned.
680 if (name && TREE_TYPE (name) != r.type ())
681 {
682 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
683 range_cast (r, TREE_TYPE (name));
684 }
685 return true;
686}
687
688// Calculate a range for range_op statement S and return it in R. If any
689// If a range cannot be calculated, return false.
690
691bool
692fold_using_range::range_of_range_op (vrange &r,
693 gimple_range_op_handler &handler,
694 fur_source &src)
695{
696 gcc_checking_assert (handler);
697 gimple *s = handler.stmt ();
698 tree type = gimple_range_type (s);
699 if (!type)
700 return false;
701
702 tree lhs = handler.lhs ();
703 tree op1 = handler.operand1 ();
704 tree op2 = handler.operand2 ();
705
706 // Certain types of builtin functions may have no arguments.
707 if (!op1)
708 {
709 value_range r1 (type);
710 if (!handler.fold_range (r, type, lh: r1, rh: r1))
711 r.set_varying (type);
712 return true;
713 }
714
715 value_range range1 (TREE_TYPE (op1));
716 value_range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
717
718 if (src.get_operand (r&: range1, expr: op1))
719 {
720 if (!op2)
721 {
722 // Fold range, and register any dependency if available.
723 value_range r2 (type);
724 r2.set_varying (type);
725 if (!handler.fold_range (r, type, lh: range1, rh: r2))
726 r.set_varying (type);
727 if (lhs && gimple_range_ssa_p (exp: op1))
728 {
729 if (src.gori_ssa ())
730 src.gori_ssa ()->register_dependency (name: lhs, ssa1: op1);
731 relation_kind rel;
732 rel = handler.lhs_op1_relation (lhs: r, op1: range1, op2: range1);
733 if (rel != VREL_VARYING)
734 src.register_relation (s, k: rel, op1: lhs, op2: op1);
735 }
736 }
737 else if (src.get_operand (r&: range2, expr: op2))
738 {
739 relation_kind rel = src.query_relation (op1, op2);
740 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
741 {
742 fprintf (stream: dump_file, format: " folding with relation ");
743 print_generic_expr (dump_file, op1, TDF_SLIM);
744 print_relation (f: dump_file, rel);
745 print_generic_expr (dump_file, op2, TDF_SLIM);
746 fputc (c: '\n', stream: dump_file);
747 }
748 // Fold range, and register any dependency if available.
749 if (!handler.fold_range (r, type, lh: range1, rh: range2,
750 relation_trio::op1_op2 (k: rel)))
751 r.set_varying (type);
752 if (irange::supports_p (type))
753 relation_fold_and_or (lhs_range&: as_a <irange> (v&: r), s, src, op1&: range1, op2&: range2);
754 if (lhs)
755 {
756 if (src.gori_ssa ())
757 {
758 src.gori_ssa ()->register_dependency (name: lhs, ssa1: op1);
759 src.gori_ssa ()->register_dependency (name: lhs, ssa1: op2);
760 }
761 if (gimple_range_ssa_p (exp: op1))
762 {
763 relation_kind rel2 = handler.lhs_op1_relation (lhs: r, op1: range1,
764 op2: range2, rel);
765 if (rel2 != VREL_VARYING)
766 src.register_relation (s, k: rel2, op1: lhs, op2: op1);
767 }
768 if (gimple_range_ssa_p (exp: op2))
769 {
770 relation_kind rel2 = handler.lhs_op2_relation (lhs: r, op1: range1,
771 op2: range2, rel);
772 if (rel2 != VREL_VARYING)
773 src.register_relation (s, k: rel2, op1: lhs, op2);
774 }
775 }
776 // Check for an existing BB, as we maybe asked to fold an
777 // artificial statement not in the CFG.
778 else if (is_a<gcond *> (p: s) && gimple_bb (g: s))
779 {
780 basic_block bb = gimple_bb (g: s);
781 edge e0 = EDGE_SUCC (bb, 0);
782 /* During RTL expansion one of the edges can be removed
783 if expansion proves the jump is unconditional. */
784 edge e1 = single_succ_p (bb) ? NULL : EDGE_SUCC (bb, 1);
785
786 gcc_checking_assert (e1 || currently_expanding_to_rtl);
787 if (!single_pred_p (bb: e0->dest))
788 e0 = NULL;
789 if (e1 && !single_pred_p (bb: e1->dest))
790 e1 = NULL;
791 src.register_outgoing_edges (as_a<gcond *> (p: s),
792 lhs_range&: as_a <irange> (v&: r), e0, e1);
793 }
794 }
795 else
796 r.set_varying (type);
797 }
798 else
799 r.set_varying (type);
800 // Make certain range-op adjustments that aren't handled any other way.
801 gimple_range_adjustment (res&: r, stmt: s);
802 return true;
803}
804
805// Calculate the range of an assignment containing an ADDR_EXPR.
806// Return the range in R.
807// If a range cannot be calculated, set it to VARYING and return true.
808
809bool
810fold_using_range::range_of_address (prange &r, gimple *stmt, fur_source &src)
811{
812 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
813 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
814
815 bool strict_overflow_p;
816 tree expr = gimple_assign_rhs1 (gs: stmt);
817 poly_int64 bitsize, bitpos;
818 tree offset;
819 machine_mode mode;
820 int unsignedp, reversep, volatilep;
821 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
822 &bitpos, &offset, &mode, &unsignedp,
823 &reversep, &volatilep);
824
825
826 if (base != NULL_TREE
827 && TREE_CODE (base) == MEM_REF
828 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
829 {
830 tree ssa = TREE_OPERAND (base, 0);
831 tree lhs = gimple_get_lhs (stmt);
832 if (lhs && gimple_range_ssa_p (exp: ssa) && src.gori_ssa ())
833 src.gori_ssa ()->register_dependency (name: lhs, ssa1: ssa);
834 src.get_operand (r, expr: ssa);
835 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
836
837 poly_offset_int off = 0;
838 bool off_cst = false;
839 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
840 {
841 off = mem_ref_offset (base);
842 if (offset)
843 off += poly_offset_int::from (a: wi::to_poly_wide (t: offset),
844 sgn: SIGNED);
845 off <<= LOG2_BITS_PER_UNIT;
846 off += bitpos;
847 off_cst = true;
848 }
849 /* If &X->a is equal to X, the range of X is the result. */
850 if (off_cst && known_eq (off, 0))
851 return true;
852 else if (flag_delete_null_pointer_checks
853 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
854 {
855 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
856 allow going from non-NULL pointer to NULL. */
857 if (r.undefined_p ()
858 || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
859 {
860 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
861 using POINTER_PLUS_EXPR if off_cst and just fall back to
862 this. */
863 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
864 return true;
865 }
866 }
867 /* If MEM_REF has a "positive" offset, consider it non-NULL
868 always, for -fdelete-null-pointer-checks also "negative"
869 ones. Punt for unknown offsets (e.g. variable ones). */
870 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
871 && off_cst
872 && known_ne (off, 0)
873 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
874 {
875 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
876 return true;
877 }
878 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
879 return true;
880 }
881
882 // Handle "= &a".
883 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
884 {
885 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
886 return true;
887 }
888
889 // Otherwise return varying.
890 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
891 return true;
892}
893
894// Calculate a range for phi statement S and return it in R.
895// If a range cannot be calculated, return false.
896
897bool
898fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
899{
900 tree phi_def = gimple_phi_result (gs: phi);
901 tree type = gimple_range_type (s: phi);
902 value_range arg_range (type);
903 value_range equiv_range (type);
904 unsigned x;
905
906 if (!type)
907 return false;
908
909 // Track if all executable arguments are the same.
910 tree single_arg = NULL_TREE;
911 bool seen_arg = false;
912
913 relation_oracle *oracle = &(src.query()->relation ());
914 // Start with an empty range, unioning in each argument's range.
915 r.set_undefined ();
916 for (x = 0; x < gimple_phi_num_args (gs: phi); x++)
917 {
918 tree arg = gimple_phi_arg_def (gs: phi, index: x);
919 // An argument that is the same as the def provides no new range.
920 if (arg == phi_def)
921 continue;
922
923 edge e = gimple_phi_arg_edge (phi, i: x);
924
925 // Get the range of the argument on its edge.
926 src.get_phi_operand (r&: arg_range, expr: arg, e);
927
928 if (!arg_range.undefined_p ())
929 {
930 // Register potential dependencies for stale value tracking.
931 // Likewise, if the incoming PHI argument is equivalent to this
932 // PHI definition, it provides no new info. Accumulate these ranges
933 // in case all arguments are equivalences.
934 if (oracle->query (e, ssa1: arg, ssa2: phi_def) == VREL_EQ)
935 equiv_range.union_(r: arg_range);
936 else
937 r.union_ (arg_range);
938
939 if (gimple_range_ssa_p (exp: arg) && src.gori_ssa ())
940 src.gori_ssa ()->register_dependency (name: phi_def, ssa1: arg);
941 }
942
943 // Track if all arguments are the same.
944 if (!seen_arg)
945 {
946 seen_arg = true;
947 single_arg = arg;
948 }
949 else if (single_arg != arg)
950 single_arg = NULL_TREE;
951
952 // Once the value reaches varying, stop looking.
953 if (r.varying_p () && single_arg == NULL_TREE)
954 break;
955 }
956
957 // If all arguments were equivalences, use the equivalence ranges as no
958 // arguments were processed.
959 if (r.undefined_p () && !equiv_range.undefined_p ())
960 r = equiv_range;
961
962 // If the PHI boils down to a single effective argument, look at it.
963 if (single_arg)
964 {
965 // Symbolic arguments can be equivalences.
966 if (gimple_range_ssa_p (exp: single_arg))
967 {
968 // Only allow the equivalence if the PHI definition does not
969 // dominate any incoming edge for SINGLE_ARG.
970 // See PR 108139 and 109462.
971 basic_block bb = gimple_bb (g: phi);
972 if (!dom_info_available_p (CDI_DOMINATORS))
973 single_arg = NULL;
974 else
975 for (x = 0; x < gimple_phi_num_args (gs: phi); x++)
976 if (gimple_phi_arg_def (gs: phi, index: x) == single_arg
977 && dominated_by_p (CDI_DOMINATORS,
978 gimple_phi_arg_edge (phi, i: x)->src,
979 bb))
980 {
981 single_arg = NULL;
982 break;
983 }
984 if (single_arg)
985 src.register_relation (s: phi, k: VREL_EQ, op1: phi_def, op2: single_arg);
986 }
987 else if (src.get_operand (r&: arg_range, expr: single_arg)
988 && arg_range.singleton_p ())
989 {
990 // Numerical arguments that are a constant can be returned as
991 // the constant. This can help fold later cases where even this
992 // constant might have been UNDEFINED via an unreachable edge.
993 r = arg_range;
994 return true;
995 }
996 }
997
998 // If PHI analysis is available, see if there is an iniital range.
999 if (phi_analysis_available_p ()
1000 && irange::supports_p (TREE_TYPE (phi_def)))
1001 {
1002 phi_group *g = (phi_analysis())[phi_def];
1003 if (g && !(g->range ().varying_p ()))
1004 {
1005 if (dump_file && (dump_flags & TDF_DETAILS))
1006 {
1007 fprintf (stream: dump_file, format: "PHI GROUP query for ");
1008 print_generic_expr (dump_file, phi_def, TDF_SLIM);
1009 fprintf (stream: dump_file, format: " found : ");
1010 g->range ().dump (dump_file);
1011 fprintf (stream: dump_file, format: " and adjusted original range from :");
1012 r.dump (dump_file);
1013 }
1014 r.intersect (g->range ());
1015 if (dump_file && (dump_flags & TDF_DETAILS))
1016 {
1017 fprintf (stream: dump_file, format: " to :");
1018 r.dump (dump_file);
1019 fprintf (stream: dump_file, format: "\n");
1020 }
1021 }
1022 }
1023
1024 // If SCEV is available, query if this PHI has any known values.
1025 if (scev_initialized_p ()
1026 && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
1027 {
1028 class loop *l = loop_containing_stmt (stmt: phi);
1029 if (l && loop_outer (loop: l))
1030 {
1031 value_range loop_range (type);
1032 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
1033 if (!loop_range.varying_p ())
1034 {
1035 if (dump_file && (dump_flags & TDF_DETAILS))
1036 {
1037 fprintf (stream: dump_file, format: "Loops range found for ");
1038 print_generic_expr (dump_file, phi_def, TDF_SLIM);
1039 fprintf (stream: dump_file, format: ": ");
1040 loop_range.dump (dump_file);
1041 fprintf (stream: dump_file, format: " and calculated range :");
1042 r.dump (dump_file);
1043 fprintf (stream: dump_file, format: "\n");
1044 }
1045 r.intersect (loop_range);
1046 }
1047 }
1048 }
1049
1050 return true;
1051}
1052
1053// Calculate a range for call statement S and return it in R.
1054// If a range cannot be calculated, return false.
1055
1056bool
1057fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1058{
1059 tree type = gimple_range_type (s: call);
1060 if (!type)
1061 return false;
1062
1063 tree lhs = gimple_call_lhs (gs: call);
1064 bool strict_overflow_p;
1065
1066 if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1067 r.set_nonnegative (type);
1068 else if (gimple_call_nonnull_result_p (call)
1069 || gimple_call_nonnull_arg (call))
1070 r.set_nonzero (type);
1071 else
1072 r.set_varying (type);
1073
1074 tree callee = gimple_call_fndecl (gs: call);
1075 if (callee
1076 && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1077 {
1078 value_range val;
1079 if (ipa_return_value_range (range&: val, decl: callee))
1080 {
1081 r.intersect (val);
1082 if (dump_file && (dump_flags & TDF_DETAILS))
1083 {
1084 fprintf (stream: dump_file, format: "Using return value range of ");
1085 print_generic_expr (dump_file, callee, TDF_SLIM);
1086 fprintf (stream: dump_file, format: ": ");
1087 val.dump (dump_file);
1088 fprintf (stream: dump_file, format: "\n");
1089 }
1090 }
1091 }
1092
1093 // If there is an LHS, intersect that with what is known.
1094 if (gimple_range_ssa_p (exp: lhs))
1095 {
1096 value_range def (TREE_TYPE (lhs));
1097 gimple_range_global (v&: def, name: lhs);
1098 r.intersect (def);
1099 }
1100 return true;
1101}
1102
1103// Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1104// to further resolve R1 and R2 if there are any dependencies between
1105// OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1106// as the origination source location for operands..
1107// Effectively, use COND an the edge condition and solve for OP1 on the true
1108// edge and OP2 on the false edge.
1109
1110bool
1111fold_using_range::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1112 tree op1, tree op2, fur_source &src)
1113{
1114 if (!src.gori () || !src.gori_ssa ())
1115 return false;
1116
1117 tree ssa1 = gimple_range_ssa_p (exp: op1);
1118 tree ssa2 = gimple_range_ssa_p (exp: op2);
1119 if (!ssa1 && !ssa2)
1120 return false;
1121 if (TREE_CODE (cond) != SSA_NAME)
1122 return false;
1123 gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1124 if (!cond_def
1125 || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1126 return false;
1127 tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1128 if (!value_range::supports_type_p (type)
1129 || !range_compatible_p (type1: type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1130 return false;
1131 range_op_handler hand (gimple_assign_rhs_code (gs: cond_def));
1132 if (!hand)
1133 return false;
1134
1135 tree c1 = gimple_range_ssa_p (exp: gimple_assign_rhs1 (gs: cond_def));
1136 tree c2 = gimple_range_ssa_p (exp: gimple_assign_rhs2 (gs: cond_def));
1137
1138 // Only solve if there is one SSA name in the condition.
1139 if ((!c1 && !c2) || (c1 && c2))
1140 return false;
1141
1142 // Pick up the current values of each part of the condition.
1143 tree rhs1 = gimple_assign_rhs1 (gs: cond_def);
1144 tree rhs2 = gimple_assign_rhs2 (gs: cond_def);
1145 value_range cl (TREE_TYPE (rhs1));
1146 value_range cr (TREE_TYPE (rhs2));
1147 src.get_operand (r&: cl, expr: rhs1);
1148 src.get_operand (r&: cr, expr: rhs2);
1149
1150 tree cond_name = c1 ? c1 : c2;
1151 gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1152
1153 // Evaluate the value of COND_NAME on the true and false edges, using either
1154 // the op1 or op2 routines based on its location.
1155 value_range cond_true (type), cond_false (type);
1156 if (c1)
1157 {
1158 if (!hand.op1_range (r&: cond_false, type, lhs: range_false (), op2: cr))
1159 return false;
1160 if (!hand.op1_range (r&: cond_true, type, lhs: range_true (), op2: cr))
1161 return false;
1162 cond_false.intersect (r: cl);
1163 cond_true.intersect (r: cl);
1164 }
1165 else
1166 {
1167 if (!hand.op2_range (r&: cond_false, type, lhs: range_false (), op1: cl))
1168 return false;
1169 if (!hand.op2_range (r&: cond_true, type, lhs: range_true (), op1: cl))
1170 return false;
1171 cond_false.intersect (r: cr);
1172 cond_true.intersect (r: cr);
1173 }
1174
1175 // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1176 if (ssa1 && src.gori_ssa()->in_chain_p (name: ssa1, def: cond_name))
1177 {
1178 value_range tmp1 (TREE_TYPE (ssa1));
1179 if (src.gori ()->compute_operand_range (tmp1, def_stmt, cond_true,
1180 ssa1, src))
1181 r1.intersect (tmp1);
1182 }
1183 if (ssa2 && src.gori_ssa ()->in_chain_p (name: ssa2, def: cond_name))
1184 {
1185 value_range tmp2 (TREE_TYPE (ssa2));
1186 if (src.gori ()->compute_operand_range (tmp2, def_stmt, cond_false,
1187 ssa2, src))
1188 r2.intersect (tmp2);
1189 }
1190 return true;
1191}
1192
1193// Calculate a range for COND_EXPR statement S and return it in R.
1194// If a range cannot be calculated, return false.
1195
1196bool
1197fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1198{
1199 tree cond = gimple_assign_rhs1 (gs: s);
1200 tree op1 = gimple_assign_rhs2 (gs: s);
1201 tree op2 = gimple_assign_rhs3 (gs: s);
1202
1203 tree type = gimple_range_type (s);
1204 if (!type)
1205 return false;
1206
1207 value_range range1 (TREE_TYPE (op1));
1208 value_range range2 (TREE_TYPE (op2));
1209 value_range cond_range (TREE_TYPE (cond));
1210 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1211 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1212 src.get_operand (r&: cond_range, expr: cond);
1213 src.get_operand (r&: range1, expr: op1);
1214 src.get_operand (r&: range2, expr: op2);
1215
1216 // Try to see if there is a dependence between the COND and either operand
1217 if (condexpr_adjust (r1&: range1, r2&: range2, s, cond, op1, op2, src))
1218 if (dump_file && (dump_flags & TDF_DETAILS))
1219 {
1220 fprintf (stream: dump_file, format: "Possible COND_EXPR adjustment. Range op1 : ");
1221 range1.dump(dump_file);
1222 fprintf (stream: dump_file, format: " and Range op2: ");
1223 range2.dump(dump_file);
1224 fprintf (stream: dump_file, format: "\n");
1225 }
1226
1227 // If the condition is known, choose the appropriate expression.
1228 if (cond_range.singleton_p ())
1229 {
1230 // False, pick second operand.
1231 if (cond_range.zero_p ())
1232 r = range2;
1233 else
1234 r = range1;
1235 }
1236 else
1237 {
1238 r = range1;
1239 r.union_ (range2);
1240 }
1241 gcc_checking_assert (r.undefined_p ()
1242 || range_compatible_p (r.type (), type));
1243 return true;
1244}
1245
1246// If SCEV has any information about phi node NAME, return it as a range in R.
1247
1248void
1249fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1250 class loop *l, gphi *phi,
1251 fur_source &src)
1252{
1253 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1254 // SCEV currently invokes get_range_query () for values. If the query
1255 // being passed in is not the same SCEV will use, do not invoke SCEV.
1256 // This can be remove if/when SCEV uses a passed in range-query.
1257 if (src.query () != get_range_query (cfun))
1258 {
1259 r.set_varying (TREE_TYPE (name));
1260 // Report the msmatch if SRC is not the global query. The cache
1261 // uses a global query and would provide numerous false positives.
1262 if (dump_file && (dump_flags & TDF_DETAILS)
1263 && src.query () != get_global_range_query ())
1264 fprintf (stream: dump_file,
1265 format: "fold_using-range:: SCEV not invoked due to mismatched queries\n");
1266 }
1267 else if (!range_of_var_in_loop (r, var: name, l, phi, src.query ()))
1268 r.set_varying (TREE_TYPE (name));
1269}
1270
1271// -----------------------------------------------------------------------
1272
1273// Check if an && or || expression can be folded based on relations. ie
1274// c_2 = a_6 > b_7
1275// c_3 = a_6 < b_7
1276// c_4 = c_2 && c_3
1277// c_2 and c_3 can never be true at the same time,
1278// Therefore c_4 can always resolve to false based purely on the relations.
1279
1280void
1281fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1282 fur_source &src, vrange &op1,
1283 vrange &op2)
1284{
1285 // No queries or already folded.
1286 if (!src.gori () || lhs_range.singleton_p ())
1287 return;
1288
1289 // Only care about AND and OR expressions.
1290 enum tree_code code = gimple_expr_code (stmt: s);
1291 bool is_and = false;
1292 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1293 is_and = true;
1294 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1295 return;
1296
1297 gimple_range_op_handler handler (s);
1298 tree lhs = handler.lhs ();
1299 tree ssa1 = gimple_range_ssa_p (exp: handler.operand1 ());
1300 tree ssa2 = gimple_range_ssa_p (exp: handler.operand2 ());
1301
1302 // Deal with || and && only when there is a full set of symbolics.
1303 if (!lhs || !ssa1 || !ssa2
1304 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1305 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1306 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1307 return;
1308
1309 // Now we know its a boolean AND or OR expression with boolean operands.
1310 // Ideally we search dependencies for common names, and see what pops out.
1311 // until then, simply try to resolve direct dependencies.
1312
1313 gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1314 gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1315
1316 gimple_range_op_handler handler1 (ssa1_stmt);
1317 gimple_range_op_handler handler2 (ssa2_stmt);
1318
1319 // If either handler is not present, no relation can be found.
1320 if (!handler1 || !handler2)
1321 return;
1322
1323 // Both stmts will need to have 2 ssa names in the stmt.
1324 tree ssa1_dep1 = gimple_range_ssa_p (exp: handler1.operand1 ());
1325 tree ssa1_dep2 = gimple_range_ssa_p (exp: handler1.operand2 ());
1326 tree ssa2_dep1 = gimple_range_ssa_p (exp: handler2.operand1 ());
1327 tree ssa2_dep2 = gimple_range_ssa_p (exp: handler2.operand2 ());
1328
1329 if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1330 return;
1331
1332 if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1333 return;
1334
1335 // Make sure they are the same dependencies, and detect the order of the
1336 // relationship.
1337 bool reverse_op2 = true;
1338 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1339 reverse_op2 = false;
1340 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1341 return;
1342
1343 int_range<2> bool_one = range_true ();
1344 relation_kind relation1 = handler1.op1_op2_relation (lhs: bool_one, op1, op2);
1345 relation_kind relation2 = handler2.op1_op2_relation (lhs: bool_one, op1, op2);
1346 if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1347 return;
1348
1349 if (reverse_op2)
1350 relation2 = relation_negate (r: relation2);
1351
1352 // x && y is false if the relation intersection of the true cases is NULL.
1353 if (is_and && relation_intersect (r1: relation1, r2: relation2) == VREL_UNDEFINED)
1354 lhs_range = range_false (boolean_type_node);
1355 // x || y is true if the union of the true cases is NO-RELATION..
1356 // ie, one or the other being true covers the full range of possibilities.
1357 else if (!is_and && relation_union (r1: relation1, r2: relation2) == VREL_VARYING)
1358 lhs_range = bool_one;
1359 else
1360 return;
1361
1362 range_cast (r&: lhs_range, TREE_TYPE (lhs));
1363 if (dump_file && (dump_flags & TDF_DETAILS))
1364 {
1365 fprintf (stream: dump_file, format: " Relation adjustment: ");
1366 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1367 fprintf (stream: dump_file, format: " and ");
1368 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1369 fprintf (stream: dump_file, format: " combine to produce ");
1370 lhs_range.dump (dump_file);
1371 fputc (c: '\n', stream: dump_file);
1372 }
1373
1374 return;
1375}
1376
1377// Register any outgoing edge relations from a conditional branch.
1378
1379void
1380fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1381 edge e0, edge e1)
1382{
1383 int_range<2> e0_range, e1_range;
1384 tree name;
1385 basic_block bb = gimple_bb (g: s);
1386
1387 gimple_range_op_handler handler (s);
1388 if (!handler)
1389 return;
1390
1391 if (e0)
1392 {
1393 // If this edge is never taken, ignore it.
1394 gcond_edge_range (r&: e0_range, e: e0);
1395 e0_range.intersect (lhs_range);
1396 if (e0_range.undefined_p ())
1397 e0 = NULL;
1398 }
1399
1400 if (e1)
1401 {
1402 // If this edge is never taken, ignore it.
1403 gcond_edge_range (r&: e1_range, e: e1);
1404 e1_range.intersect (lhs_range);
1405 if (e1_range.undefined_p ())
1406 e1 = NULL;
1407 }
1408
1409 if (!e0 && !e1)
1410 return;
1411
1412 // First, register the gcond itself. This will catch statements like
1413 // if (a_2 < b_5)
1414 tree ssa1 = gimple_range_ssa_p (exp: handler.operand1 ());
1415 tree ssa2 = gimple_range_ssa_p (exp: handler.operand2 ());
1416 value_range r1,r2;
1417 if (ssa1 && ssa2)
1418 {
1419 r1.set_varying (TREE_TYPE (ssa1));
1420 r2.set_varying (TREE_TYPE (ssa2));
1421 if (e0)
1422 {
1423 relation_kind relation = handler.op1_op2_relation (lhs: e0_range, op1: r1, op2: r2);
1424 if (relation != VREL_VARYING)
1425 register_relation (e: e0, k: relation, op1: ssa1, op2: ssa2);
1426 }
1427 if (e1)
1428 {
1429 relation_kind relation = handler.op1_op2_relation (lhs: e1_range, op1: r1, op2: r2);
1430 if (relation != VREL_VARYING)
1431 register_relation (e: e1, k: relation, op1: ssa1, op2: ssa2);
1432 }
1433 }
1434
1435 // Outgoing relations of GORI exports require a gori engine.
1436 if (!gori_ssa ())
1437 return;
1438
1439 // Now look for other relations in the exports. This will find stmts
1440 // leading to the condition such as:
1441 // c_2 = a_4 < b_7
1442 // if (c_2)
1443 FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, name)
1444 {
1445 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1446 continue;
1447 gimple *stmt = SSA_NAME_DEF_STMT (name);
1448 gimple_range_op_handler handler (stmt);
1449 if (!handler)
1450 continue;
1451 tree ssa1 = gimple_range_ssa_p (exp: handler.operand1 ());
1452 tree ssa2 = gimple_range_ssa_p (exp: handler.operand2 ());
1453 value_range r (TREE_TYPE (name));
1454 if (ssa1 && ssa2)
1455 {
1456 r1.set_varying (TREE_TYPE (ssa1));
1457 r2.set_varying (TREE_TYPE (ssa2));
1458 if (e0 && gori ()->edge_range_p (r, e0, name, *m_query)
1459 && r.singleton_p ())
1460 {
1461 relation_kind relation = handler.op1_op2_relation (lhs: r, op1: r1, op2: r2);
1462 if (relation != VREL_VARYING)
1463 register_relation (e: e0, k: relation, op1: ssa1, op2: ssa2);
1464 }
1465 if (e1 && gori ()->edge_range_p (r, e1, name, *m_query)
1466 && r.singleton_p ())
1467 {
1468 relation_kind relation = handler.op1_op2_relation (lhs: r, op1: r1, op2: r2);
1469 if (relation != VREL_VARYING)
1470 register_relation (e: e1, k: relation, op1: ssa1, op2: ssa2);
1471 }
1472 }
1473 }
1474}
1475

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of gcc/gimple-range-fold.cc