Debian Codegraph @

gcc/gimple-range-fold.cc

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