]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range-fold.cc
Add range-ops support for builtin functions.
[thirdparty/gcc.git] / gcc / gimple-range-fold.cc
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>.
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 // Construct a fur_source, and set the m_query field.
48
49 fur_source::fur_source (range_query *q)
50 {
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;
58 }
59
60 // Invoke range_of_expr on EXPR.
61
62 bool
63 fur_source::get_operand (vrange &r, tree expr)
64 {
65 return m_query->range_of_expr (r, expr);
66 }
67
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.
70
71 bool
72 fur_source::get_phi_operand (vrange &r, tree expr, edge e)
73 {
74 return m_query->range_on_edge (r, e, expr);
75 }
76
77 // Default is no relation.
78
79 relation_kind
80 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
81 tree op2 ATTRIBUTE_UNUSED)
82 {
83 return VREL_VARYING;
84 }
85
86 // Default registers nothing.
87
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)
93 {
94 }
95
96 // Default registers nothing.
97
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)
103 {
104 }
105
106 // This version of fur_source will pick a range up off an edge.
107
108 class fur_edge : public fur_source
109 {
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;
116 };
117
118 // Instantiate an edge based fur_source.
119
120 inline
121 fur_edge::fur_edge (edge e, range_query *q) : fur_source (q)
122 {
123 m_edge = e;
124 }
125
126 // Get the value of EXPR on edge m_edge.
127
128 bool
129 fur_edge::get_operand (vrange &r, tree expr)
130 {
131 return m_query->range_on_edge (r, m_edge, expr);
132 }
133
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.
136
137 bool
138 fur_edge::get_phi_operand (vrange &r, tree expr, edge e)
139 {
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);
143 }
144
145 // Instantiate a stmt based fur_source.
146
147 fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
148 {
149 m_stmt = s;
150 }
151
152 // Retreive range of EXPR as it occurs as a use on stmt M_STMT.
153
154 bool
155 fur_stmt::get_operand (vrange &r, tree expr)
156 {
157 return m_query->range_of_expr (r, expr, m_stmt);
158 }
159
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.
162
163 bool
164 fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
165 {
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);
169 }
170
171 // Return relation based from m_stmt.
172
173 relation_kind
174 fur_stmt::query_relation (tree op1, tree op2)
175 {
176 return m_query->query_relation (m_stmt, op1, op2);
177 }
178
179 // Instantiate a stmt based fur_source with a GORI object.
180
181
182 fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
183 : fur_stmt (s, q)
184 {
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 ();
190
191 }
192
193 // Register a relation on a stmt if there is an oracle.
194
195 void
196 fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
197 {
198 if (m_oracle)
199 m_oracle->register_stmt (s, k, op1, op2);
200 }
201
202 // Register a relation on an edge if there is an oracle.
203
204 void
205 fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
206 {
207 if (m_oracle)
208 m_oracle->register_edge (e, k, op1, op2);
209 }
210
211 // This version of fur_source will pick a range up from a list of ranges
212 // supplied by the caller.
213
214 class fur_list : public fur_source
215 {
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;
227 };
228
229 // One range supplied for unary operations.
230
231 fur_list::fur_list (vrange &r1) : fur_source (NULL)
232 {
233 m_list = m_local;
234 m_index = 0;
235 m_limit = 1;
236 m_local[0] = &r1;
237 }
238
239 // Two ranges supplied for binary operations.
240
241 fur_list::fur_list (vrange &r1, vrange &r2) : fur_source (NULL)
242 {
243 m_list = m_local;
244 m_index = 0;
245 m_limit = 2;
246 m_local[0] = &r1;
247 m_local[1] = &r2;
248 }
249
250 // Arbitrary number of ranges in a vector.
251
252 fur_list::fur_list (unsigned num, vrange **list) : fur_source (NULL)
253 {
254 m_list = list;
255 m_index = 0;
256 m_limit = num;
257 }
258
259 // Get the next operand from the vector, ensure types are compatible.
260
261 bool
262 fur_list::get_operand (vrange &r, tree expr)
263 {
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;
269 }
270
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)
274 {
275 return get_operand (r, expr);
276 }
277
278 // Fold stmt S into range R using R1 as the first operand.
279
280 bool
281 fold_range (vrange &r, gimple *s, vrange &r1)
282 {
283 fold_using_range f;
284 fur_list src (r1);
285 return f.fold_stmt (r, s, src);
286 }
287
288 // Fold stmt S into range R using R1 and R2 as the first two operands.
289
290 bool
291 fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2)
292 {
293 fold_using_range f;
294 fur_list src (r1, r2);
295 return f.fold_stmt (r, s, src);
296 }
297
298 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
299 // operands encountered.
300
301 bool
302 fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector)
303 {
304 fold_using_range f;
305 fur_list src (num_elements, vector);
306 return f.fold_stmt (r, s, src);
307 }
308
309 // Fold stmt S into range R using range query Q.
310
311 bool
312 fold_range (vrange &r, gimple *s, range_query *q)
313 {
314 fold_using_range f;
315 fur_stmt src (s, q);
316 return f.fold_stmt (r, s, src);
317 }
318
319 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
320
321 bool
322 fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
323 {
324 fold_using_range f;
325 fur_edge src (on_edge, q);
326 return f.fold_stmt (r, s, src);
327 }
328
329 // -------------------------------------------------------------------------
330
331 // Adjust the range for a pointer difference where the operands came
332 // from a memchr.
333 //
334 // This notices the following sequence:
335 //
336 // def = __builtin_memchr (arg, 0, sz)
337 // n = def - arg
338 //
339 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
340
341 static void
342 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
343 {
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;
349
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)))
362 {
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));
367 }
368 }
369
370 // Adjust the range for an IMAGPART_EXPR.
371
372 static void
373 adjust_imagpart_expr (vrange &res, const gimple *stmt)
374 {
375 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
376
377 if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
378 return;
379
380 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
381 if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
382 {
383 switch (gimple_call_internal_fn (def_stmt))
384 {
385 case IFN_ADD_OVERFLOW:
386 case IFN_SUB_OVERFLOW:
387 case IFN_MUL_OVERFLOW:
388 case IFN_ATOMIC_COMPARE_EXCHANGE:
389 {
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);
395 }
396 default:
397 break;
398 }
399 return;
400 }
401 if (is_gimple_assign (def_stmt)
402 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
403 {
404 tree cst = gimple_assign_rhs1 (def_stmt);
405 if (TREE_CODE (cst) == COMPLEX_CST)
406 {
407 int_range<2> imag (TREE_IMAGPART (cst), TREE_IMAGPART (cst));
408 res.intersect (imag);
409 }
410 }
411 }
412
413 // Adjust the range for a REALPART_EXPR.
414
415 static void
416 adjust_realpart_expr (vrange &res, const gimple *stmt)
417 {
418 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
419
420 if (TREE_CODE (name) != SSA_NAME)
421 return;
422
423 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
424 if (!SSA_NAME_DEF_STMT (name))
425 return;
426
427 if (is_gimple_assign (def_stmt)
428 && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
429 {
430 tree cst = gimple_assign_rhs1 (def_stmt);
431 if (TREE_CODE (cst) == COMPLEX_CST)
432 {
433 tree imag = TREE_REALPART (cst);
434 int_range<2> tmp (imag, imag);
435 res.intersect (tmp);
436 }
437 }
438 }
439
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.
443
444 static void
445 gimple_range_adjustment (vrange &res, const gimple *stmt)
446 {
447 switch (gimple_expr_code (stmt))
448 {
449 case POINTER_DIFF_EXPR:
450 adjust_pointer_diff_expr (as_a <irange> (res), stmt);
451 return;
452
453 case IMAGPART_EXPR:
454 adjust_imagpart_expr (res, stmt);
455 return;
456
457 case REALPART_EXPR:
458 adjust_realpart_expr (res, stmt);
459 return;
460
461 default:
462 break;
463 }
464 }
465
466 // Calculate a range for statement S and return it in R. If NAME is provided it
467 // represents the SSA_NAME on the LHS of the statement. It is only required
468 // if there is more than one lhs/output. If a range cannot
469 // be calculated, return false.
470
471 bool
472 fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
473 {
474 bool res = false;
475 // If name and S are specified, make sure it is an LHS of S.
476 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
477 name == gimple_get_lhs (s));
478
479 if (!name)
480 name = gimple_get_lhs (s);
481
482 // Process addresses.
483 if (gimple_code (s) == GIMPLE_ASSIGN
484 && gimple_assign_rhs_code (s) == ADDR_EXPR)
485 return range_of_address (as_a <irange> (r), s, src);
486
487 gimple_range_op_handler handler (s);
488 if (handler)
489 res = range_of_range_op (r, handler, src);
490 else if (is_a<gphi *>(s))
491 res = range_of_phi (r, as_a<gphi *> (s), src);
492 else if (is_a<gcall *>(s))
493 res = range_of_call (r, as_a<gcall *> (s), src);
494 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
495 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
496
497 if (!res)
498 {
499 // If no name specified or range is unsupported, bail.
500 if (!name || !gimple_range_ssa_p (name))
501 return false;
502 // We don't understand the stmt, so return the global range.
503 gimple_range_global (r, name);
504 return true;
505 }
506
507 if (r.undefined_p ())
508 return true;
509
510 // We sometimes get compatible types copied from operands, make sure
511 // the correct type is being returned.
512 if (name && TREE_TYPE (name) != r.type ())
513 {
514 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
515 range_cast (r, TREE_TYPE (name));
516 }
517 return true;
518 }
519
520 // Calculate a range for range_op statement S and return it in R. If any
521 // If a range cannot be calculated, return false.
522
523 bool
524 fold_using_range::range_of_range_op (vrange &r,
525 gimple_range_op_handler &handler,
526 fur_source &src)
527 {
528 gcc_checking_assert (handler);
529 gimple *s = handler.stmt ();
530 tree type = gimple_range_type (s);
531 if (!type)
532 return false;
533
534 tree lhs = handler.lhs ();
535 tree op1 = handler.operand1 ();
536 tree op2 = handler.operand2 ();
537 Value_Range range1 (TREE_TYPE (op1));
538 Value_Range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
539
540 if (src.get_operand (range1, op1))
541 {
542 if (!op2)
543 {
544 // Fold range, and register any dependency if available.
545 Value_Range r2 (type);
546 r2.set_varying (type);
547 if (!handler.fold_range (r, type, range1, r2))
548 r.set_varying (type);
549 if (lhs && gimple_range_ssa_p (op1))
550 {
551 if (src.gori ())
552 src.gori ()->register_dependency (lhs, op1);
553 relation_kind rel;
554 rel = handler.lhs_op1_relation (r, range1, range1);
555 if (rel != VREL_VARYING)
556 src.register_relation (s, rel, lhs, op1);
557 }
558 }
559 else if (src.get_operand (range2, op2))
560 {
561 relation_kind rel = src.query_relation (op1, op2);
562 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
563 {
564 fprintf (dump_file, " folding with relation ");
565 print_generic_expr (dump_file, op1, TDF_SLIM);
566 print_relation (dump_file, rel);
567 print_generic_expr (dump_file, op2, TDF_SLIM);
568 fputc ('\n', dump_file);
569 }
570 // Fold range, and register any dependency if available.
571 if (!handler.fold_range (r, type, range1, range2, rel))
572 r.set_varying (type);
573 if (irange::supports_p (type))
574 relation_fold_and_or (as_a <irange> (r), s, src);
575 if (lhs)
576 {
577 if (src.gori ())
578 {
579 src.gori ()->register_dependency (lhs, op1);
580 src.gori ()->register_dependency (lhs, op2);
581 }
582 if (gimple_range_ssa_p (op1))
583 {
584 rel = handler.lhs_op1_relation (r, range1, range2, rel);
585 if (rel != VREL_VARYING)
586 src.register_relation (s, rel, lhs, op1);
587 }
588 if (gimple_range_ssa_p (op2))
589 {
590 rel= handler.lhs_op2_relation (r, range1, range2, rel);
591 if (rel != VREL_VARYING)
592 src.register_relation (s, rel, lhs, op2);
593 }
594 }
595 // Check for an existing BB, as we maybe asked to fold an
596 // artificial statement not in the CFG.
597 else if (is_a<gcond *> (s) && gimple_bb (s))
598 {
599 basic_block bb = gimple_bb (s);
600 edge e0 = EDGE_SUCC (bb, 0);
601 edge e1 = EDGE_SUCC (bb, 1);
602
603 if (!single_pred_p (e0->dest))
604 e0 = NULL;
605 if (!single_pred_p (e1->dest))
606 e1 = NULL;
607 src.register_outgoing_edges (as_a<gcond *> (s),
608 as_a <irange> (r), e0, e1);
609 }
610 }
611 else
612 r.set_varying (type);
613 }
614 else
615 r.set_varying (type);
616 // Make certain range-op adjustments that aren't handled any other way.
617 gimple_range_adjustment (r, s);
618 return true;
619 }
620
621 // Calculate the range of an assignment containing an ADDR_EXPR.
622 // Return the range in R.
623 // If a range cannot be calculated, set it to VARYING and return true.
624
625 bool
626 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
627 {
628 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
629 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
630
631 bool strict_overflow_p;
632 tree expr = gimple_assign_rhs1 (stmt);
633 poly_int64 bitsize, bitpos;
634 tree offset;
635 machine_mode mode;
636 int unsignedp, reversep, volatilep;
637 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
638 &bitpos, &offset, &mode, &unsignedp,
639 &reversep, &volatilep);
640
641
642 if (base != NULL_TREE
643 && TREE_CODE (base) == MEM_REF
644 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
645 {
646 tree ssa = TREE_OPERAND (base, 0);
647 tree lhs = gimple_get_lhs (stmt);
648 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
649 src.gori ()->register_dependency (lhs, ssa);
650 src.get_operand (r, ssa);
651 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
652
653 poly_offset_int off = 0;
654 bool off_cst = false;
655 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
656 {
657 off = mem_ref_offset (base);
658 if (offset)
659 off += poly_offset_int::from (wi::to_poly_wide (offset),
660 SIGNED);
661 off <<= LOG2_BITS_PER_UNIT;
662 off += bitpos;
663 off_cst = true;
664 }
665 /* If &X->a is equal to X, the range of X is the result. */
666 if (off_cst && known_eq (off, 0))
667 return true;
668 else if (flag_delete_null_pointer_checks
669 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
670 {
671 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
672 allow going from non-NULL pointer to NULL. */
673 if (r.undefined_p () || !r.contains_p (build_zero_cst (r.type ())))
674 {
675 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
676 using POINTER_PLUS_EXPR if off_cst and just fall back to
677 this. */
678 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
679 return true;
680 }
681 }
682 /* If MEM_REF has a "positive" offset, consider it non-NULL
683 always, for -fdelete-null-pointer-checks also "negative"
684 ones. Punt for unknown offsets (e.g. variable ones). */
685 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
686 && off_cst
687 && known_ne (off, 0)
688 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
689 {
690 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
691 return true;
692 }
693 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
694 return true;
695 }
696
697 // Handle "= &a".
698 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
699 {
700 r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
701 return true;
702 }
703
704 // Otherwise return varying.
705 r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
706 return true;
707 }
708
709 // Calculate a range for phi statement S and return it in R.
710 // If a range cannot be calculated, return false.
711
712 bool
713 fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
714 {
715 tree phi_def = gimple_phi_result (phi);
716 tree type = gimple_range_type (phi);
717 Value_Range arg_range (type);
718 Value_Range equiv_range (type);
719 unsigned x;
720
721 if (!type)
722 return false;
723
724 // Track if all executable arguments are the same.
725 tree single_arg = NULL_TREE;
726 bool seen_arg = false;
727
728 // Start with an empty range, unioning in each argument's range.
729 r.set_undefined ();
730 for (x = 0; x < gimple_phi_num_args (phi); x++)
731 {
732 tree arg = gimple_phi_arg_def (phi, x);
733 // An argument that is the same as the def provides no new range.
734 if (arg == phi_def)
735 continue;
736
737 edge e = gimple_phi_arg_edge (phi, x);
738
739 // Get the range of the argument on its edge.
740 src.get_phi_operand (arg_range, arg, e);
741
742 if (!arg_range.undefined_p ())
743 {
744 // Register potential dependencies for stale value tracking.
745 // Likewise, if the incoming PHI argument is equivalent to this
746 // PHI definition, it provides no new info. Accumulate these ranges
747 // in case all arguments are equivalences.
748 if (src.query ()->query_relation (e, arg, phi_def, false) == VREL_EQ)
749 equiv_range.union_(arg_range);
750 else
751 r.union_ (arg_range);
752
753 if (gimple_range_ssa_p (arg) && src.gori ())
754 src.gori ()->register_dependency (phi_def, arg);
755
756 // Track if all arguments are the same.
757 if (!seen_arg)
758 {
759 seen_arg = true;
760 single_arg = arg;
761 }
762 else if (single_arg != arg)
763 single_arg = NULL_TREE;
764 }
765
766 // Once the value reaches varying, stop looking.
767 if (r.varying_p () && single_arg == NULL_TREE)
768 break;
769 }
770
771 // If all arguments were equivalences, use the equivalence ranges as no
772 // arguments were processed.
773 if (r.undefined_p () && !equiv_range.undefined_p ())
774 r = equiv_range;
775
776 // If the PHI boils down to a single effective argument, look at it.
777 if (single_arg)
778 {
779 // Symbolic arguments are equivalences.
780 if (gimple_range_ssa_p (single_arg))
781 src.register_relation (phi, VREL_EQ, phi_def, single_arg);
782 else if (src.get_operand (arg_range, single_arg)
783 && arg_range.singleton_p ())
784 {
785 // Numerical arguments that are a constant can be returned as
786 // the constant. This can help fold later cases where even this
787 // constant might have been UNDEFINED via an unreachable edge.
788 r = arg_range;
789 return true;
790 }
791 }
792
793 // If SCEV is available, query if this PHI has any knonwn values.
794 if (scev_initialized_p ()
795 && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
796 {
797 class loop *l = loop_containing_stmt (phi);
798 if (l && loop_outer (l))
799 {
800 Value_Range loop_range (type);
801 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
802 if (!loop_range.varying_p ())
803 {
804 if (dump_file && (dump_flags & TDF_DETAILS))
805 {
806 fprintf (dump_file, " Loops range found for ");
807 print_generic_expr (dump_file, phi_def, TDF_SLIM);
808 fprintf (dump_file, ": ");
809 loop_range.dump (dump_file);
810 fprintf (dump_file, " and calculated range :");
811 r.dump (dump_file);
812 fprintf (dump_file, "\n");
813 }
814 r.intersect (loop_range);
815 }
816 }
817 }
818
819 return true;
820 }
821
822 // Calculate a range for call statement S and return it in R.
823 // If a range cannot be calculated, return false.
824
825 bool
826 fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &src)
827 {
828 tree type = gimple_range_type (call);
829 if (!type)
830 return false;
831
832 tree lhs = gimple_call_lhs (call);
833 bool strict_overflow_p;
834
835 if (range_of_builtin_call (r, call, src))
836 ;
837 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
838 r.set_nonnegative (type);
839 else if (gimple_call_nonnull_result_p (call)
840 || gimple_call_nonnull_arg (call))
841 r.set_nonzero (type);
842 else
843 r.set_varying (type);
844
845 // If there is an LHS, intersect that with what is known.
846 if (lhs)
847 {
848 Value_Range def (TREE_TYPE (lhs));
849 gimple_range_global (def, lhs);
850 r.intersect (def);
851 }
852 return true;
853 }
854
855 // Return the range of a __builtin_ubsan* in CALL and set it in R.
856 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
857 // MULT_EXPR).
858
859 void
860 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
861 tree_code code, fur_source &src)
862 {
863 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
864 || code == MULT_EXPR);
865 tree type = gimple_range_type (call);
866 range_op_handler op (code, type);
867 gcc_checking_assert (op);
868 int_range_max ir0, ir1;
869 tree arg0 = gimple_call_arg (call, 0);
870 tree arg1 = gimple_call_arg (call, 1);
871 src.get_operand (ir0, arg0);
872 src.get_operand (ir1, arg1);
873 // Check for any relation between arg0 and arg1.
874 relation_kind relation = src.query_relation (arg0, arg1);
875
876 bool saved_flag_wrapv = flag_wrapv;
877 // Pretend the arithmetic is wrapping. If there is any overflow,
878 // we'll complain, but will actually do wrapping operation.
879 flag_wrapv = 1;
880 op.fold_range (r, type, ir0, ir1, relation);
881 flag_wrapv = saved_flag_wrapv;
882
883 // If for both arguments vrp_valueize returned non-NULL, this should
884 // have been already folded and if not, it wasn't folded because of
885 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
886 if (r.singleton_p ())
887 r.set_varying (type);
888 }
889
890 // Return TRUE if we recognize the target character set and return the
891 // range for lower case and upper case letters.
892
893 static bool
894 get_letter_range (tree type, irange &lowers, irange &uppers)
895 {
896 // ASCII
897 int a = lang_hooks.to_target_charset ('a');
898 int z = lang_hooks.to_target_charset ('z');
899 int A = lang_hooks.to_target_charset ('A');
900 int Z = lang_hooks.to_target_charset ('Z');
901
902 if ((z - a == 25) && (Z - A == 25))
903 {
904 lowers = int_range<2> (build_int_cst (type, a), build_int_cst (type, z));
905 uppers = int_range<2> (build_int_cst (type, A), build_int_cst (type, Z));
906 return true;
907 }
908 // Unknown character set.
909 return false;
910 }
911
912 // For a builtin in CALL, return a range in R if known and return
913 // TRUE. Otherwise return FALSE.
914
915 bool
916 fold_using_range::range_of_builtin_call (vrange &r, gcall *call,
917 fur_source &src)
918 {
919 combined_fn func = gimple_call_combined_fn (call);
920 if (func == CFN_LAST)
921 return false;
922
923 tree type = gimple_range_type (call);
924 gcc_checking_assert (type);
925
926 if (irange::supports_p (type))
927 return range_of_builtin_int_call (as_a <irange> (r), call, src);
928
929 return false;
930 }
931
932 bool
933 fold_using_range::range_of_builtin_int_call (irange &r, gcall *call,
934 fur_source &src)
935 {
936 combined_fn func = gimple_call_combined_fn (call);
937 if (func == CFN_LAST)
938 return false;
939
940 tree type = gimple_range_type (call);
941 tree arg;
942 int mini, maxi, zerov = 0, prec;
943 scalar_int_mode mode;
944
945 switch (func)
946 {
947 case CFN_BUILT_IN_SIGNBIT:
948 {
949 arg = gimple_call_arg (call, 0);
950 frange tmp;
951 if (src.get_operand (tmp, arg))
952 {
953 bool signbit;
954 if (tmp.signbit_p (signbit))
955 {
956 if (signbit)
957 r.set_nonzero (type);
958 else
959 r.set_zero (type);
960 return true;
961 }
962 return false;
963 }
964 break;
965 }
966
967 case CFN_BUILT_IN_TOUPPER:
968 {
969 arg = gimple_call_arg (call, 0);
970 // If the argument isn't compatible with the LHS, do nothing.
971 if (!range_compatible_p (type, TREE_TYPE (arg)))
972 return false;
973 if (!src.get_operand (r, arg))
974 return false;
975
976 int_range<3> lowers;
977 int_range<3> uppers;
978 if (!get_letter_range (type, lowers, uppers))
979 return false;
980
981 // Return the range passed in without any lower case characters,
982 // but including all the upper case ones.
983 lowers.invert ();
984 r.intersect (lowers);
985 r.union_ (uppers);
986 return true;
987 }
988
989 case CFN_BUILT_IN_TOLOWER:
990 {
991 arg = gimple_call_arg (call, 0);
992 // If the argument isn't compatible with the LHS, do nothing.
993 if (!range_compatible_p (type, TREE_TYPE (arg)))
994 return false;
995 if (!src.get_operand (r, arg))
996 return false;
997
998 int_range<3> lowers;
999 int_range<3> uppers;
1000 if (!get_letter_range (type, lowers, uppers))
1001 return false;
1002
1003 // Return the range passed in without any upper case characters,
1004 // but including all the lower case ones.
1005 uppers.invert ();
1006 r.intersect (uppers);
1007 r.union_ (lowers);
1008 return true;
1009 }
1010
1011 CASE_CFN_FFS:
1012 CASE_CFN_POPCOUNT:
1013 // __builtin_ffs* and __builtin_popcount* return [0, prec].
1014 arg = gimple_call_arg (call, 0);
1015 prec = TYPE_PRECISION (TREE_TYPE (arg));
1016 mini = 0;
1017 maxi = prec;
1018 src.get_operand (r, arg);
1019 // If arg is non-zero, then ffs or popcount are non-zero.
1020 if (!range_includes_zero_p (&r))
1021 mini = 1;
1022 // If some high bits are known to be zero, decrease the maximum.
1023 if (!r.undefined_p ())
1024 {
1025 if (TYPE_SIGN (r.type ()) == SIGNED)
1026 range_cast (r, unsigned_type_for (r.type ()));
1027 wide_int max = r.upper_bound ();
1028 maxi = wi::floor_log2 (max) + 1;
1029 }
1030 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1031 return true;
1032
1033 CASE_CFN_PARITY:
1034 r.set (build_zero_cst (type), build_one_cst (type));
1035 return true;
1036
1037 CASE_CFN_CLZ:
1038 // __builtin_c[lt]z* return [0, prec-1], except when the
1039 // argument is 0, but that is undefined behavior.
1040 //
1041 // For __builtin_c[lt]z* consider argument of 0 always undefined
1042 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
1043 arg = gimple_call_arg (call, 0);
1044 prec = TYPE_PRECISION (TREE_TYPE (arg));
1045 mini = 0;
1046 maxi = prec - 1;
1047 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1048 if (gimple_call_internal_p (call))
1049 {
1050 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1051 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1052 {
1053 // Only handle the single common value.
1054 if (zerov == prec)
1055 maxi = prec;
1056 else
1057 // Magic value to give up, unless we can prove arg is non-zero.
1058 mini = -2;
1059 }
1060 }
1061
1062 src.get_operand (r, arg);
1063 // From clz of minimum we can compute result maximum.
1064 if (!r.undefined_p ())
1065 {
1066 // From clz of minimum we can compute result maximum.
1067 if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
1068 {
1069 maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
1070 if (mini == -2)
1071 mini = 0;
1072 }
1073 else if (!range_includes_zero_p (&r))
1074 {
1075 mini = 0;
1076 maxi = prec - 1;
1077 }
1078 if (mini == -2)
1079 break;
1080 // From clz of maximum we can compute result minimum.
1081 wide_int max = r.upper_bound ();
1082 int newmini = prec - 1 - wi::floor_log2 (max);
1083 if (max == 0)
1084 {
1085 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
1086 // return [prec, prec], otherwise ignore the range.
1087 if (maxi == prec)
1088 mini = prec;
1089 }
1090 else
1091 mini = newmini;
1092 }
1093 if (mini == -2)
1094 break;
1095 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1096 return true;
1097
1098 CASE_CFN_CTZ:
1099 // __builtin_ctz* return [0, prec-1], except for when the
1100 // argument is 0, but that is undefined behavior.
1101 //
1102 // For __builtin_ctz* consider argument of 0 always undefined
1103 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
1104 arg = gimple_call_arg (call, 0);
1105 prec = TYPE_PRECISION (TREE_TYPE (arg));
1106 mini = 0;
1107 maxi = prec - 1;
1108 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1109 if (gimple_call_internal_p (call))
1110 {
1111 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1112 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1113 {
1114 // Handle only the two common values.
1115 if (zerov == -1)
1116 mini = -1;
1117 else if (zerov == prec)
1118 maxi = prec;
1119 else
1120 // Magic value to give up, unless we can prove arg is non-zero.
1121 mini = -2;
1122 }
1123 }
1124 src.get_operand (r, arg);
1125 if (!r.undefined_p ())
1126 {
1127 // If arg is non-zero, then use [0, prec - 1].
1128 if (!range_includes_zero_p (&r))
1129 {
1130 mini = 0;
1131 maxi = prec - 1;
1132 }
1133 // If some high bits are known to be zero, we can decrease
1134 // the maximum.
1135 wide_int max = r.upper_bound ();
1136 if (max == 0)
1137 {
1138 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1139 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1140 // Otherwise ignore the range.
1141 if (mini == -1)
1142 maxi = -1;
1143 else if (maxi == prec)
1144 mini = prec;
1145 }
1146 // If value at zero is prec and 0 is in the range, we can't lower
1147 // the upper bound. We could create two separate ranges though,
1148 // [0,floor_log2(max)][prec,prec] though.
1149 else if (maxi != prec)
1150 maxi = wi::floor_log2 (max);
1151 }
1152 if (mini == -2)
1153 break;
1154 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1155 return true;
1156
1157 CASE_CFN_CLRSB:
1158 arg = gimple_call_arg (call, 0);
1159 prec = TYPE_PRECISION (TREE_TYPE (arg));
1160 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
1161 return true;
1162 case CFN_UBSAN_CHECK_ADD:
1163 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
1164 return true;
1165 case CFN_UBSAN_CHECK_SUB:
1166 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
1167 return true;
1168 case CFN_UBSAN_CHECK_MUL:
1169 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
1170 return true;
1171
1172 case CFN_GOACC_DIM_SIZE:
1173 case CFN_GOACC_DIM_POS:
1174 // Optimizing these two internal functions helps the loop
1175 // optimizer eliminate outer comparisons. Size is [1,N]
1176 // and pos is [0,N-1].
1177 {
1178 bool is_pos = func == CFN_GOACC_DIM_POS;
1179 int axis = oacc_get_ifn_dim_arg (call);
1180 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1181 if (!size)
1182 // If it's dynamic, the backend might know a hardware limitation.
1183 size = targetm.goacc.dim_limit (axis);
1184
1185 r.set (build_int_cst (type, is_pos ? 0 : 1),
1186 size
1187 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1188 return true;
1189 }
1190
1191 case CFN_BUILT_IN_STRLEN:
1192 if (tree lhs = gimple_call_lhs (call))
1193 if (ptrdiff_type_node
1194 && (TYPE_PRECISION (ptrdiff_type_node)
1195 == TYPE_PRECISION (TREE_TYPE (lhs))))
1196 {
1197 tree type = TREE_TYPE (lhs);
1198 tree max = vrp_val_max (ptrdiff_type_node);
1199 wide_int wmax
1200 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1201 tree range_min = build_zero_cst (type);
1202 // To account for the terminating NULL, the maximum length
1203 // is one less than the maximum array size, which in turn
1204 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1205 // smaller than the former type).
1206 // FIXME: Use max_object_size() - 1 here.
1207 tree range_max = wide_int_to_tree (type, wmax - 2);
1208 r.set (range_min, range_max);
1209 return true;
1210 }
1211 break;
1212 default:
1213 break;
1214 }
1215 return false;
1216 }
1217
1218
1219 // Calculate a range for COND_EXPR statement S and return it in R.
1220 // If a range cannot be calculated, return false.
1221
1222 bool
1223 fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1224 {
1225 tree cond = gimple_assign_rhs1 (s);
1226 tree op1 = gimple_assign_rhs2 (s);
1227 tree op2 = gimple_assign_rhs3 (s);
1228
1229 tree type = gimple_range_type (s);
1230 if (!type)
1231 return false;
1232
1233 Value_Range range1 (TREE_TYPE (op1));
1234 Value_Range range2 (TREE_TYPE (op2));
1235 Value_Range cond_range (TREE_TYPE (cond));
1236 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1237 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1238 src.get_operand (cond_range, cond);
1239 src.get_operand (range1, op1);
1240 src.get_operand (range2, op2);
1241
1242 // Try to see if there is a dependence between the COND and either operand
1243 if (src.gori ())
1244 if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1245 if (dump_file && (dump_flags & TDF_DETAILS))
1246 {
1247 fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1248 range1.dump(dump_file);
1249 fprintf (dump_file, " and Range op2: ");
1250 range2.dump(dump_file);
1251 fprintf (dump_file, "\n");
1252 }
1253
1254 // If the condition is known, choose the appropriate expression.
1255 if (cond_range.singleton_p ())
1256 {
1257 // False, pick second operand.
1258 if (cond_range.zero_p ())
1259 r = range2;
1260 else
1261 r = range1;
1262 }
1263 else
1264 {
1265 r = range1;
1266 r.union_ (range2);
1267 }
1268 gcc_checking_assert (r.undefined_p ()
1269 || range_compatible_p (r.type (), type));
1270 return true;
1271 }
1272
1273 // Return the lower bound of R as a tree.
1274
1275 static inline tree
1276 tree_lower_bound (const vrange &r, tree type)
1277 {
1278 if (is_a <irange> (r))
1279 return wide_int_to_tree (type, as_a <irange> (r).lower_bound ());
1280 // ?? Handle floats when they contain endpoints.
1281 return NULL;
1282 }
1283
1284 // Return the upper bound of R as a tree.
1285
1286 static inline tree
1287 tree_upper_bound (const vrange &r, tree type)
1288 {
1289 if (is_a <irange> (r))
1290 return wide_int_to_tree (type, as_a <irange> (r).upper_bound ());
1291 // ?? Handle floats when they contain endpoints.
1292 return NULL;
1293 }
1294
1295 // If SCEV has any information about phi node NAME, return it as a range in R.
1296
1297 void
1298 fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1299 class loop *l, gphi *phi,
1300 fur_source &src)
1301 {
1302 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1303 tree min, max, type = TREE_TYPE (name);
1304 if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
1305 {
1306 if (!is_gimple_constant (min))
1307 {
1308 if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
1309 min = tree_lower_bound (r, type);
1310 else
1311 min = vrp_val_min (type);
1312 }
1313 if (!is_gimple_constant (max))
1314 {
1315 if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
1316 max = tree_upper_bound (r, type);
1317 else
1318 max = vrp_val_max (type);
1319 }
1320 if (min && max)
1321 {
1322 r.set (min, max);
1323 return;
1324 }
1325 }
1326 r.set_varying (type);
1327 }
1328
1329 // -----------------------------------------------------------------------
1330
1331 // Check if an && or || expression can be folded based on relations. ie
1332 // c_2 = a_6 > b_7
1333 // c_3 = a_6 < b_7
1334 // c_4 = c_2 && c_3
1335 // c_2 and c_3 can never be true at the same time,
1336 // Therefore c_4 can always resolve to false based purely on the relations.
1337
1338 void
1339 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1340 fur_source &src)
1341 {
1342 // No queries or already folded.
1343 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1344 return;
1345
1346 // Only care about AND and OR expressions.
1347 enum tree_code code = gimple_expr_code (s);
1348 bool is_and = false;
1349 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1350 is_and = true;
1351 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1352 return;
1353
1354 gimple_range_op_handler handler (s);
1355 tree lhs = handler.lhs ();
1356 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1357 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1358
1359 // Deal with || and && only when there is a full set of symbolics.
1360 if (!lhs || !ssa1 || !ssa2
1361 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1362 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1363 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1364 return;
1365
1366 // Now we know its a boolean AND or OR expression with boolean operands.
1367 // Ideally we search dependencies for common names, and see what pops out.
1368 // until then, simply try to resolve direct dependencies.
1369
1370 gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1371 gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1372
1373 gimple_range_op_handler handler1 (ssa1_stmt);
1374 gimple_range_op_handler handler2 (ssa2_stmt);
1375
1376 // If either handler is not present, no relation can be found.
1377 if (!handler1 || !handler2)
1378 return;
1379
1380 // Both stmts will need to have 2 ssa names in the stmt.
1381 tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1382 tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1383 tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1384 tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1385
1386 if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1387 return;
1388
1389 // Make sure they are the same dependencies, and detect the order of the
1390 // relationship.
1391 bool reverse_op2 = true;
1392 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1393 reverse_op2 = false;
1394 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1395 return;
1396
1397 int_range<2> bool_one (boolean_true_node, boolean_true_node);
1398
1399 relation_kind relation1 = handler1.op1_op2_relation (bool_one);
1400 relation_kind relation2 = handler2.op1_op2_relation (bool_one);
1401 if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1402 return;
1403
1404 if (reverse_op2)
1405 relation2 = relation_negate (relation2);
1406
1407 // x && y is false if the relation intersection of the true cases is NULL.
1408 if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1409 lhs_range = int_range<2> (boolean_false_node, boolean_false_node);
1410 // x || y is true if the union of the true cases is NO-RELATION..
1411 // ie, one or the other being true covers the full range of possibilties.
1412 else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1413 lhs_range = bool_one;
1414 else
1415 return;
1416
1417 range_cast (lhs_range, TREE_TYPE (lhs));
1418 if (dump_file && (dump_flags & TDF_DETAILS))
1419 {
1420 fprintf (dump_file, " Relation adjustment: ");
1421 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1422 fprintf (dump_file, " and ");
1423 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1424 fprintf (dump_file, " combine to produce ");
1425 lhs_range.dump (dump_file);
1426 fputc ('\n', dump_file);
1427 }
1428
1429 return;
1430 }
1431
1432 // Register any outgoing edge relations from a conditional branch.
1433
1434 void
1435 fur_source::register_outgoing_edges (gcond *s, irange &lhs_range, edge e0, edge e1)
1436 {
1437 int_range<2> e0_range, e1_range;
1438 tree name;
1439 basic_block bb = gimple_bb (s);
1440
1441 gimple_range_op_handler handler (s);
1442 if (!handler)
1443 return;
1444
1445 if (e0)
1446 {
1447 // If this edge is never taken, ignore it.
1448 gcond_edge_range (e0_range, e0);
1449 e0_range.intersect (lhs_range);
1450 if (e0_range.undefined_p ())
1451 e0 = NULL;
1452 }
1453
1454 if (e1)
1455 {
1456 // If this edge is never taken, ignore it.
1457 gcond_edge_range (e1_range, e1);
1458 e1_range.intersect (lhs_range);
1459 if (e1_range.undefined_p ())
1460 e1 = NULL;
1461 }
1462
1463 if (!e0 && !e1)
1464 return;
1465
1466 // First, register the gcond itself. This will catch statements like
1467 // if (a_2 < b_5)
1468 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1469 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1470 if (ssa1 && ssa2)
1471 {
1472 if (e0)
1473 {
1474 relation_kind relation = handler.op1_op2_relation (e0_range);
1475 if (relation != VREL_VARYING)
1476 register_relation (e0, relation, ssa1, ssa2);
1477 }
1478 if (e1)
1479 {
1480 relation_kind relation = handler.op1_op2_relation (e1_range);
1481 if (relation != VREL_VARYING)
1482 register_relation (e1, relation, ssa1, ssa2);
1483 }
1484 }
1485
1486 // Outgoing relations of GORI exports require a gori engine.
1487 if (!gori ())
1488 return;
1489
1490 // Now look for other relations in the exports. This will find stmts
1491 // leading to the condition such as:
1492 // c_2 = a_4 < b_7
1493 // if (c_2)
1494 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
1495 {
1496 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1497 continue;
1498 gimple *stmt = SSA_NAME_DEF_STMT (name);
1499 gimple_range_op_handler handler (stmt);
1500 if (!handler)
1501 continue;
1502 tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1503 tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1504 Value_Range r (TREE_TYPE (name));
1505 if (ssa1 && ssa2)
1506 {
1507 if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
1508 && r.singleton_p ())
1509 {
1510 relation_kind relation = handler.op1_op2_relation (r);
1511 if (relation != VREL_VARYING)
1512 register_relation (e0, relation, ssa1, ssa2);
1513 }
1514 if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
1515 && r.singleton_p ())
1516 {
1517 relation_kind relation = handler.op1_op2_relation (r);
1518 if (relation != VREL_VARYING)
1519 register_relation (e1, relation, ssa1, ssa2);
1520 }
1521 }
1522 }
1523 }