]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range-fold.cc
Add relation between op1 & op2 to lhs_opN_relation API.
[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 "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 (irange &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 (irange &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_NONE;
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 (irange &r, tree expr) OVERRIDE;
113 virtual bool get_phi_operand (irange &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 (irange &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 (irange &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 (irange &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 (irange &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 (irange &r1);
218 fur_list (irange &r1, irange &r2);
219 fur_list (unsigned num, irange *list);
220 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
221 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
222 private:
223 int_range_max m_local[2];
224 irange *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 (irange &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 (irange &r1, irange &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, irange *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 (irange &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 (irange &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 (irange &r, gimple *s, irange &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 (irange &r, gimple *s, irange &r1, irange &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 (irange &r, gimple *s, unsigned num_elements, irange *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 (irange &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 (irange &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 (irange &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 (irange &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 (irange &res, const gimple *stmt)
446 {
447 switch (gimple_expr_code (stmt))
448 {
449 case POINTER_DIFF_EXPR:
450 adjust_pointer_diff_expr (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 // Return the base of the RHS of an assignment.
467
468 static tree
469 gimple_range_base_of_assignment (const gimple *stmt)
470 {
471 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
472 tree op1 = gimple_assign_rhs1 (stmt);
473 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
474 return get_base_address (TREE_OPERAND (op1, 0));
475 return op1;
476 }
477
478 // Return the first operand of this statement if it is a valid operand
479 // supported by ranges, otherwise return NULL_TREE. Special case is
480 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
481
482 tree
483 gimple_range_operand1 (const gimple *stmt)
484 {
485 gcc_checking_assert (gimple_range_handler (stmt));
486
487 switch (gimple_code (stmt))
488 {
489 case GIMPLE_COND:
490 return gimple_cond_lhs (stmt);
491 case GIMPLE_ASSIGN:
492 {
493 tree base = gimple_range_base_of_assignment (stmt);
494 if (base && TREE_CODE (base) == MEM_REF)
495 {
496 // If the base address is an SSA_NAME, we return it
497 // here. This allows processing of the range of that
498 // name, while the rest of the expression is simply
499 // ignored. The code in range_ops will see the
500 // ADDR_EXPR and do the right thing.
501 tree ssa = TREE_OPERAND (base, 0);
502 if (TREE_CODE (ssa) == SSA_NAME)
503 return ssa;
504 }
505 return base;
506 }
507 default:
508 break;
509 }
510 return NULL;
511 }
512
513 // Return the second operand of statement STMT, otherwise return NULL_TREE.
514
515 tree
516 gimple_range_operand2 (const gimple *stmt)
517 {
518 gcc_checking_assert (gimple_range_handler (stmt));
519
520 switch (gimple_code (stmt))
521 {
522 case GIMPLE_COND:
523 return gimple_cond_rhs (stmt);
524 case GIMPLE_ASSIGN:
525 if (gimple_num_ops (stmt) >= 3)
526 return gimple_assign_rhs2 (stmt);
527 default:
528 break;
529 }
530 return NULL_TREE;
531 }
532
533 // Calculate a range for statement S and return it in R. If NAME is provided it
534 // represents the SSA_NAME on the LHS of the statement. It is only required
535 // if there is more than one lhs/output. If a range cannot
536 // be calculated, return false.
537
538 bool
539 fold_using_range::fold_stmt (irange &r, gimple *s, fur_source &src, tree name)
540 {
541 bool res = false;
542 // If name and S are specified, make sure it is an LHS of S.
543 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
544 name == gimple_get_lhs (s));
545
546 if (!name)
547 name = gimple_get_lhs (s);
548
549 // Process addresses.
550 if (gimple_code (s) == GIMPLE_ASSIGN
551 && gimple_assign_rhs_code (s) == ADDR_EXPR)
552 return range_of_address (r, s, src);
553
554 if (gimple_range_handler (s))
555 res = range_of_range_op (r, s, src);
556 else if (is_a<gphi *>(s))
557 res = range_of_phi (r, as_a<gphi *> (s), src);
558 else if (is_a<gcall *>(s))
559 res = range_of_call (r, as_a<gcall *> (s), src);
560 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
561 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
562
563 if (!res)
564 {
565 // If no name specified or range is unsupported, bail.
566 if (!name || !gimple_range_ssa_p (name))
567 return false;
568 // We don't understand the stmt, so return the global range.
569 r = gimple_range_global (name);
570 return true;
571 }
572
573 if (r.undefined_p ())
574 return true;
575
576 // We sometimes get compatible types copied from operands, make sure
577 // the correct type is being returned.
578 if (name && TREE_TYPE (name) != r.type ())
579 {
580 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
581 range_cast (r, TREE_TYPE (name));
582 }
583 return true;
584 }
585
586 // Calculate a range for range_op statement S and return it in R. If any
587 // If a range cannot be calculated, return false.
588
589 bool
590 fold_using_range::range_of_range_op (irange &r, gimple *s, fur_source &src)
591 {
592 int_range_max range1, range2;
593 tree type = gimple_range_type (s);
594 if (!type)
595 return false;
596 range_operator *handler = gimple_range_handler (s);
597 gcc_checking_assert (handler);
598
599 tree lhs = gimple_get_lhs (s);
600 tree op1 = gimple_range_operand1 (s);
601 tree op2 = gimple_range_operand2 (s);
602
603 if (src.get_operand (range1, op1))
604 {
605 if (!op2)
606 {
607 // Fold range, and register any dependency if available.
608 int_range<2> r2 (type);
609 handler->fold_range (r, type, range1, r2);
610 if (lhs && gimple_range_ssa_p (op1))
611 {
612 if (src.gori ())
613 src.gori ()->register_dependency (lhs, op1);
614 relation_kind rel;
615 rel = handler->lhs_op1_relation (r, range1, range1);
616 if (rel != VREL_NONE)
617 src.register_relation (s, rel, lhs, op1);
618 }
619 }
620 else if (src.get_operand (range2, op2))
621 {
622 relation_kind rel = src.query_relation (op1, op2);
623 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_NONE)
624 {
625 fprintf (dump_file, " folding with relation ");
626 print_generic_expr (dump_file, op1, TDF_SLIM);
627 print_relation (dump_file, rel);
628 print_generic_expr (dump_file, op2, TDF_SLIM);
629 fputc ('\n', dump_file);
630 }
631 // Fold range, and register any dependency if available.
632 handler->fold_range (r, type, range1, range2, rel);
633 relation_fold_and_or (r, s, src);
634 if (lhs)
635 {
636 if (src.gori ())
637 {
638 src.gori ()->register_dependency (lhs, op1);
639 src.gori ()->register_dependency (lhs, op2);
640 }
641 if (gimple_range_ssa_p (op1))
642 {
643 rel = handler->lhs_op1_relation (r, range1, range2, rel);
644 if (rel != VREL_NONE)
645 src.register_relation (s, rel, lhs, op1);
646 }
647 if (gimple_range_ssa_p (op2))
648 {
649 rel= handler->lhs_op2_relation (r, range1, range2, rel);
650 if (rel != VREL_NONE)
651 src.register_relation (s, rel, lhs, op2);
652 }
653 }
654 // Check for an existing BB, as we maybe asked to fold an
655 // artificial statement not in the CFG.
656 else if (is_a<gcond *> (s) && gimple_bb (s))
657 {
658 basic_block bb = gimple_bb (s);
659 edge e0 = EDGE_SUCC (bb, 0);
660 edge e1 = EDGE_SUCC (bb, 1);
661
662 if (!single_pred_p (e0->dest))
663 e0 = NULL;
664 if (!single_pred_p (e1->dest))
665 e1 = NULL;
666 src.register_outgoing_edges (as_a<gcond *> (s), r, e0, e1);
667 }
668 }
669 else
670 r.set_varying (type);
671 }
672 else
673 r.set_varying (type);
674 // Make certain range-op adjustments that aren't handled any other way.
675 gimple_range_adjustment (r, s);
676 return true;
677 }
678
679 // Calculate the range of an assignment containing an ADDR_EXPR.
680 // Return the range in R.
681 // If a range cannot be calculated, set it to VARYING and return true.
682
683 bool
684 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
685 {
686 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
687 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
688
689 bool strict_overflow_p;
690 tree expr = gimple_assign_rhs1 (stmt);
691 poly_int64 bitsize, bitpos;
692 tree offset;
693 machine_mode mode;
694 int unsignedp, reversep, volatilep;
695 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
696 &bitpos, &offset, &mode, &unsignedp,
697 &reversep, &volatilep);
698
699
700 if (base != NULL_TREE
701 && TREE_CODE (base) == MEM_REF
702 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
703 {
704 tree ssa = TREE_OPERAND (base, 0);
705 tree lhs = gimple_get_lhs (stmt);
706 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
707 src.gori ()->register_dependency (lhs, ssa);
708 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
709 src.get_operand (r, ssa);
710 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
711
712 poly_offset_int off = 0;
713 bool off_cst = false;
714 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
715 {
716 off = mem_ref_offset (base);
717 if (offset)
718 off += poly_offset_int::from (wi::to_poly_wide (offset),
719 SIGNED);
720 off <<= LOG2_BITS_PER_UNIT;
721 off += bitpos;
722 off_cst = true;
723 }
724 /* If &X->a is equal to X, the range of X is the result. */
725 if (off_cst && known_eq (off, 0))
726 return true;
727 else if (flag_delete_null_pointer_checks
728 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
729 {
730 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
731 allow going from non-NULL pointer to NULL. */
732 if (!range_includes_zero_p (&r))
733 {
734 /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
735 using POINTER_PLUS_EXPR if off_cst and just fall back to
736 this. */
737 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
738 return true;
739 }
740 }
741 /* If MEM_REF has a "positive" offset, consider it non-NULL
742 always, for -fdelete-null-pointer-checks also "negative"
743 ones. Punt for unknown offsets (e.g. variable ones). */
744 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
745 && off_cst
746 && known_ne (off, 0)
747 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
748 {
749 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
750 return true;
751 }
752 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
753 return true;
754 }
755
756 // Handle "= &a".
757 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
758 {
759 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
760 return true;
761 }
762
763 // Otherwise return varying.
764 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
765 return true;
766 }
767
768 // Calculate a range for phi statement S and return it in R.
769 // If a range cannot be calculated, return false.
770
771 bool
772 fold_using_range::range_of_phi (irange &r, gphi *phi, fur_source &src)
773 {
774 tree phi_def = gimple_phi_result (phi);
775 tree type = gimple_range_type (phi);
776 int_range_max arg_range;
777 int_range_max equiv_range;
778 unsigned x;
779
780 if (!type)
781 return false;
782
783 // Track if all executable arguments are the same.
784 tree single_arg = NULL_TREE;
785 bool seen_arg = false;
786
787 // Start with an empty range, unioning in each argument's range.
788 r.set_undefined ();
789 for (x = 0; x < gimple_phi_num_args (phi); x++)
790 {
791 tree arg = gimple_phi_arg_def (phi, x);
792 // An argument that is the same as the def provides no new range.
793 if (arg == phi_def)
794 continue;
795
796 edge e = gimple_phi_arg_edge (phi, x);
797
798 // Get the range of the argument on its edge.
799 src.get_phi_operand (arg_range, arg, e);
800
801 if (!arg_range.undefined_p ())
802 {
803 // Register potential dependencies for stale value tracking.
804 // Likewise, if the incoming PHI argument is equivalent to this
805 // PHI definition, it provides no new info. Accumulate these ranges
806 // in case all arguments are equivalences.
807 if (src.query ()->query_relation (e, arg, phi_def, false) == EQ_EXPR)
808 equiv_range.union_(arg_range);
809 else
810 r.union_ (arg_range);
811
812 if (gimple_range_ssa_p (arg) && src.gori ())
813 src.gori ()->register_dependency (phi_def, arg);
814
815 // Track if all arguments are the same.
816 if (!seen_arg)
817 {
818 seen_arg = true;
819 single_arg = arg;
820 }
821 else if (single_arg != arg)
822 single_arg = NULL_TREE;
823 }
824
825 // Once the value reaches varying, stop looking.
826 if (r.varying_p () && single_arg == NULL_TREE)
827 break;
828 }
829
830 // If all arguments were equivalences, use the equivalence ranges as no
831 // arguments were processed.
832 if (r.undefined_p () && !equiv_range.undefined_p ())
833 r = equiv_range;
834
835 // If the PHI boils down to a single effective argument, look at it.
836 if (single_arg)
837 {
838 // Symbolic arguments are equivalences.
839 if (gimple_range_ssa_p (single_arg))
840 src.register_relation (phi, EQ_EXPR, phi_def, single_arg);
841 else if (src.get_operand (arg_range, single_arg)
842 && arg_range.singleton_p ())
843 {
844 // Numerical arguments that are a constant can be returned as
845 // the constant. This can help fold later cases where even this
846 // constant might have been UNDEFINED via an unreachable edge.
847 r = arg_range;
848 return true;
849 }
850 }
851
852 // If SCEV is available, query if this PHI has any knonwn values.
853 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
854 {
855 value_range loop_range;
856 class loop *l = loop_containing_stmt (phi);
857 if (l && loop_outer (l))
858 {
859 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
860 if (!loop_range.varying_p ())
861 {
862 if (dump_file && (dump_flags & TDF_DETAILS))
863 {
864 fprintf (dump_file, " Loops range found for ");
865 print_generic_expr (dump_file, phi_def, TDF_SLIM);
866 fprintf (dump_file, ": ");
867 loop_range.dump (dump_file);
868 fprintf (dump_file, " and calculated range :");
869 r.dump (dump_file);
870 fprintf (dump_file, "\n");
871 }
872 r.intersect (loop_range);
873 }
874 }
875 }
876
877 return true;
878 }
879
880 // Calculate a range for call statement S and return it in R.
881 // If a range cannot be calculated, return false.
882
883 bool
884 fold_using_range::range_of_call (irange &r, gcall *call, fur_source &src)
885 {
886 tree type = gimple_range_type (call);
887 if (!type)
888 return false;
889
890 tree lhs = gimple_call_lhs (call);
891 bool strict_overflow_p;
892
893 if (range_of_builtin_call (r, call, src))
894 ;
895 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
896 r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
897 else if (gimple_call_nonnull_result_p (call)
898 || gimple_call_nonnull_arg (call))
899 r = range_nonzero (type);
900 else
901 r.set_varying (type);
902
903 // If there is an LHS, intersect that with what is known.
904 if (lhs)
905 {
906 value_range def;
907 def = gimple_range_global (lhs);
908 r.intersect (def);
909 }
910 return true;
911 }
912
913 // Return the range of a __builtin_ubsan* in CALL and set it in R.
914 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
915 // MULT_EXPR).
916
917 void
918 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
919 tree_code code, fur_source &src)
920 {
921 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
922 || code == MULT_EXPR);
923 tree type = gimple_range_type (call);
924 range_operator *op = range_op_handler (code, type);
925 gcc_checking_assert (op);
926 int_range_max ir0, ir1;
927 tree arg0 = gimple_call_arg (call, 0);
928 tree arg1 = gimple_call_arg (call, 1);
929 src.get_operand (ir0, arg0);
930 src.get_operand (ir1, arg1);
931 // Check for any relation between arg0 and arg1.
932 relation_kind relation = src.query_relation (arg0, arg1);
933
934 bool saved_flag_wrapv = flag_wrapv;
935 // Pretend the arithmetic is wrapping. If there is any overflow,
936 // we'll complain, but will actually do wrapping operation.
937 flag_wrapv = 1;
938 op->fold_range (r, type, ir0, ir1, relation);
939 flag_wrapv = saved_flag_wrapv;
940
941 // If for both arguments vrp_valueize returned non-NULL, this should
942 // have been already folded and if not, it wasn't folded because of
943 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
944 if (r.singleton_p ())
945 r.set_varying (type);
946 }
947
948 // Return TRUE if we recognize the target character set and return the
949 // range for lower case and upper case letters.
950
951 static bool
952 get_letter_range (tree type, irange &lowers, irange &uppers)
953 {
954 // ASCII
955 int a = lang_hooks.to_target_charset ('a');
956 int z = lang_hooks.to_target_charset ('z');
957 int A = lang_hooks.to_target_charset ('A');
958 int Z = lang_hooks.to_target_charset ('Z');
959
960 if ((z - a == 25) && (Z - A == 25))
961 {
962 lowers = int_range<2> (build_int_cst (type, a), build_int_cst (type, z));
963 uppers = int_range<2> (build_int_cst (type, A), build_int_cst (type, Z));
964 return true;
965 }
966 // Unknown character set.
967 return false;
968 }
969
970 // For a builtin in CALL, return a range in R if known and return
971 // TRUE. Otherwise return FALSE.
972
973 bool
974 fold_using_range::range_of_builtin_call (irange &r, gcall *call,
975 fur_source &src)
976 {
977 combined_fn func = gimple_call_combined_fn (call);
978 if (func == CFN_LAST)
979 return false;
980
981 tree type = gimple_range_type (call);
982 tree arg;
983 int mini, maxi, zerov = 0, prec;
984 scalar_int_mode mode;
985
986 switch (func)
987 {
988 case CFN_BUILT_IN_CONSTANT_P:
989 arg = gimple_call_arg (call, 0);
990 if (src.get_operand (r, arg) && r.singleton_p ())
991 {
992 r.set (build_one_cst (type), build_one_cst (type));
993 return true;
994 }
995 if (cfun->after_inlining)
996 {
997 r.set_zero (type);
998 // r.equiv_clear ();
999 return true;
1000 }
1001 break;
1002
1003 case CFN_BUILT_IN_TOUPPER:
1004 {
1005 arg = gimple_call_arg (call, 0);
1006 // If the argument isn't compatible with the LHS, do nothing.
1007 if (!range_compatible_p (type, TREE_TYPE (arg)))
1008 return false;
1009 if (!src.get_operand (r, arg))
1010 return false;
1011
1012 int_range<3> lowers;
1013 int_range<3> uppers;
1014 if (!get_letter_range (type, lowers, uppers))
1015 return false;
1016
1017 // Return the range passed in without any lower case characters,
1018 // but including all the upper case ones.
1019 lowers.invert ();
1020 r.intersect (lowers);
1021 r.union_ (uppers);
1022 return true;
1023 }
1024
1025 case CFN_BUILT_IN_TOLOWER:
1026 {
1027 arg = gimple_call_arg (call, 0);
1028 // If the argument isn't compatible with the LHS, do nothing.
1029 if (!range_compatible_p (type, TREE_TYPE (arg)))
1030 return false;
1031 if (!src.get_operand (r, arg))
1032 return false;
1033
1034 int_range<3> lowers;
1035 int_range<3> uppers;
1036 if (!get_letter_range (type, lowers, uppers))
1037 return false;
1038
1039 // Return the range passed in without any upper case characters,
1040 // but including all the lower case ones.
1041 uppers.invert ();
1042 r.intersect (uppers);
1043 r.union_ (lowers);
1044 return true;
1045 }
1046
1047 CASE_CFN_FFS:
1048 CASE_CFN_POPCOUNT:
1049 // __builtin_ffs* and __builtin_popcount* return [0, prec].
1050 arg = gimple_call_arg (call, 0);
1051 prec = TYPE_PRECISION (TREE_TYPE (arg));
1052 mini = 0;
1053 maxi = prec;
1054 src.get_operand (r, arg);
1055 // If arg is non-zero, then ffs or popcount are non-zero.
1056 if (!range_includes_zero_p (&r))
1057 mini = 1;
1058 // If some high bits are known to be zero, decrease the maximum.
1059 if (!r.undefined_p ())
1060 {
1061 if (TYPE_SIGN (r.type ()) == SIGNED)
1062 range_cast (r, unsigned_type_for (r.type ()));
1063 wide_int max = r.upper_bound ();
1064 maxi = wi::floor_log2 (max) + 1;
1065 }
1066 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1067 return true;
1068
1069 CASE_CFN_PARITY:
1070 r.set (build_zero_cst (type), build_one_cst (type));
1071 return true;
1072
1073 CASE_CFN_CLZ:
1074 // __builtin_c[lt]z* return [0, prec-1], except when the
1075 // argument is 0, but that is undefined behavior.
1076 //
1077 // For __builtin_c[lt]z* consider argument of 0 always undefined
1078 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
1079 arg = gimple_call_arg (call, 0);
1080 prec = TYPE_PRECISION (TREE_TYPE (arg));
1081 mini = 0;
1082 maxi = prec - 1;
1083 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1084 if (gimple_call_internal_p (call))
1085 {
1086 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1087 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1088 {
1089 // Only handle the single common value.
1090 if (zerov == prec)
1091 maxi = prec;
1092 else
1093 // Magic value to give up, unless we can prove arg is non-zero.
1094 mini = -2;
1095 }
1096 }
1097
1098 src.get_operand (r, arg);
1099 // From clz of minimum we can compute result maximum.
1100 if (!r.undefined_p ())
1101 {
1102 // From clz of minimum we can compute result maximum.
1103 if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
1104 {
1105 maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
1106 if (mini == -2)
1107 mini = 0;
1108 }
1109 else if (!range_includes_zero_p (&r))
1110 {
1111 mini = 0;
1112 maxi = prec - 1;
1113 }
1114 if (mini == -2)
1115 break;
1116 // From clz of maximum we can compute result minimum.
1117 wide_int max = r.upper_bound ();
1118 int newmini = prec - 1 - wi::floor_log2 (max);
1119 if (max == 0)
1120 {
1121 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
1122 // return [prec, prec], otherwise ignore the range.
1123 if (maxi == prec)
1124 mini = prec;
1125 }
1126 else
1127 mini = newmini;
1128 }
1129 if (mini == -2)
1130 break;
1131 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1132 return true;
1133
1134 CASE_CFN_CTZ:
1135 // __builtin_ctz* return [0, prec-1], except for when the
1136 // argument is 0, but that is undefined behavior.
1137 //
1138 // For __builtin_ctz* consider argument of 0 always undefined
1139 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
1140 arg = gimple_call_arg (call, 0);
1141 prec = TYPE_PRECISION (TREE_TYPE (arg));
1142 mini = 0;
1143 maxi = prec - 1;
1144 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1145 if (gimple_call_internal_p (call))
1146 {
1147 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1148 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1149 {
1150 // Handle only the two common values.
1151 if (zerov == -1)
1152 mini = -1;
1153 else if (zerov == prec)
1154 maxi = prec;
1155 else
1156 // Magic value to give up, unless we can prove arg is non-zero.
1157 mini = -2;
1158 }
1159 }
1160 src.get_operand (r, arg);
1161 if (!r.undefined_p ())
1162 {
1163 // If arg is non-zero, then use [0, prec - 1].
1164 if (!range_includes_zero_p (&r))
1165 {
1166 mini = 0;
1167 maxi = prec - 1;
1168 }
1169 // If some high bits are known to be zero, we can decrease
1170 // the maximum.
1171 wide_int max = r.upper_bound ();
1172 if (max == 0)
1173 {
1174 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1175 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1176 // Otherwise ignore the range.
1177 if (mini == -1)
1178 maxi = -1;
1179 else if (maxi == prec)
1180 mini = prec;
1181 }
1182 // If value at zero is prec and 0 is in the range, we can't lower
1183 // the upper bound. We could create two separate ranges though,
1184 // [0,floor_log2(max)][prec,prec] though.
1185 else if (maxi != prec)
1186 maxi = wi::floor_log2 (max);
1187 }
1188 if (mini == -2)
1189 break;
1190 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1191 return true;
1192
1193 CASE_CFN_CLRSB:
1194 arg = gimple_call_arg (call, 0);
1195 prec = TYPE_PRECISION (TREE_TYPE (arg));
1196 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
1197 return true;
1198 case CFN_UBSAN_CHECK_ADD:
1199 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
1200 return true;
1201 case CFN_UBSAN_CHECK_SUB:
1202 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
1203 return true;
1204 case CFN_UBSAN_CHECK_MUL:
1205 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
1206 return true;
1207
1208 case CFN_GOACC_DIM_SIZE:
1209 case CFN_GOACC_DIM_POS:
1210 // Optimizing these two internal functions helps the loop
1211 // optimizer eliminate outer comparisons. Size is [1,N]
1212 // and pos is [0,N-1].
1213 {
1214 bool is_pos = func == CFN_GOACC_DIM_POS;
1215 int axis = oacc_get_ifn_dim_arg (call);
1216 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1217 if (!size)
1218 // If it's dynamic, the backend might know a hardware limitation.
1219 size = targetm.goacc.dim_limit (axis);
1220
1221 r.set (build_int_cst (type, is_pos ? 0 : 1),
1222 size
1223 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1224 return true;
1225 }
1226
1227 case CFN_BUILT_IN_STRLEN:
1228 if (tree lhs = gimple_call_lhs (call))
1229 if (ptrdiff_type_node
1230 && (TYPE_PRECISION (ptrdiff_type_node)
1231 == TYPE_PRECISION (TREE_TYPE (lhs))))
1232 {
1233 tree type = TREE_TYPE (lhs);
1234 tree max = vrp_val_max (ptrdiff_type_node);
1235 wide_int wmax
1236 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1237 tree range_min = build_zero_cst (type);
1238 // To account for the terminating NULL, the maximum length
1239 // is one less than the maximum array size, which in turn
1240 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1241 // smaller than the former type).
1242 // FIXME: Use max_object_size() - 1 here.
1243 tree range_max = wide_int_to_tree (type, wmax - 2);
1244 r.set (range_min, range_max);
1245 return true;
1246 }
1247 break;
1248 default:
1249 break;
1250 }
1251 return false;
1252 }
1253
1254
1255 // Calculate a range for COND_EXPR statement S and return it in R.
1256 // If a range cannot be calculated, return false.
1257
1258 bool
1259 fold_using_range::range_of_cond_expr (irange &r, gassign *s, fur_source &src)
1260 {
1261 int_range_max cond_range, range1, range2;
1262 tree cond = gimple_assign_rhs1 (s);
1263 tree op1 = gimple_assign_rhs2 (s);
1264 tree op2 = gimple_assign_rhs3 (s);
1265
1266 tree type = gimple_range_type (s);
1267 if (!type)
1268 return false;
1269
1270 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1271 gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1272 src.get_operand (cond_range, cond);
1273 src.get_operand (range1, op1);
1274 src.get_operand (range2, op2);
1275
1276 // Try to see if there is a dependence between the COND and either operand
1277 if (src.gori ())
1278 if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1279 if (dump_file && (dump_flags & TDF_DETAILS))
1280 {
1281 fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1282 range1.dump(dump_file);
1283 fprintf (dump_file, " and Range op2: ");
1284 range2.dump(dump_file);
1285 fprintf (dump_file, "\n");
1286 }
1287
1288 // If the condition is known, choose the appropriate expression.
1289 if (cond_range.singleton_p ())
1290 {
1291 // False, pick second operand.
1292 if (cond_range.zero_p ())
1293 r = range2;
1294 else
1295 r = range1;
1296 }
1297 else
1298 {
1299 r = range1;
1300 r.union_ (range2);
1301 }
1302 gcc_checking_assert (r.undefined_p ()
1303 || range_compatible_p (r.type (), type));
1304 return true;
1305 }
1306
1307 // If SCEV has any information about phi node NAME, return it as a range in R.
1308
1309 void
1310 fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
1311 class loop *l, gphi *phi,
1312 fur_source &src)
1313 {
1314 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1315 tree min, max, type = TREE_TYPE (name);
1316 if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
1317 {
1318 if (TREE_CODE (min) != INTEGER_CST)
1319 {
1320 if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
1321 min = wide_int_to_tree (type, r.lower_bound ());
1322 else
1323 min = vrp_val_min (type);
1324 }
1325 if (TREE_CODE (max) != INTEGER_CST)
1326 {
1327 if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
1328 max = wide_int_to_tree (type, r.upper_bound ());
1329 else
1330 max = vrp_val_max (type);
1331 }
1332 r.set (min, max);
1333 }
1334 else
1335 r.set_varying (type);
1336 }
1337
1338 // -----------------------------------------------------------------------
1339
1340 // Check if an && or || expression can be folded based on relations. ie
1341 // c_2 = a_6 > b_7
1342 // c_3 = a_6 < b_7
1343 // c_4 = c_2 && c_3
1344 // c_2 and c_3 can never be true at the same time,
1345 // Therefore c_4 can always resolve to false based purely on the relations.
1346
1347 void
1348 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1349 fur_source &src)
1350 {
1351 // No queries or already folded.
1352 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1353 return;
1354
1355 // Only care about AND and OR expressions.
1356 enum tree_code code = gimple_expr_code (s);
1357 bool is_and = false;
1358 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1359 is_and = true;
1360 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1361 return;
1362
1363 tree lhs = gimple_get_lhs (s);
1364 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1365 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1366
1367 // Deal with || and && only when there is a full set of symbolics.
1368 if (!lhs || !ssa1 || !ssa2
1369 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1370 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1371 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1372 return;
1373
1374 // Now we know its a boolean AND or OR expression with boolean operands.
1375 // Ideally we search dependencies for common names, and see what pops out.
1376 // until then, simply try to resolve direct dependencies.
1377
1378 // Both names will need to have 2 direct dependencies.
1379 tree ssa1_dep2 = src.gori ()->depend2 (ssa1);
1380 tree ssa2_dep2 = src.gori ()->depend2 (ssa2);
1381 if (!ssa1_dep2 || !ssa2_dep2)
1382 return;
1383
1384 tree ssa1_dep1 = src.gori ()->depend1 (ssa1);
1385 tree ssa2_dep1 = src.gori ()->depend1 (ssa2);
1386 // Make sure they are the same dependencies, and detect the order of the
1387 // relationship.
1388 bool reverse_op2 = true;
1389 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1390 reverse_op2 = false;
1391 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1392 return;
1393
1394 range_operator *handler1 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa1));
1395 range_operator *handler2 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa2));
1396
1397 // If either handler is not present, no relation is found.
1398 if (!handler1 || !handler2)
1399 return;
1400
1401 int_range<2> bool_one (boolean_true_node, boolean_true_node);
1402
1403 relation_kind relation1 = handler1->op1_op2_relation (bool_one);
1404 relation_kind relation2 = handler2->op1_op2_relation (bool_one);
1405 if (relation1 == VREL_NONE || relation2 == VREL_NONE)
1406 return;
1407
1408 if (reverse_op2)
1409 relation2 = relation_negate (relation2);
1410
1411 // x && y is false if the relation intersection of the true cases is NULL.
1412 if (is_and && relation_intersect (relation1, relation2) == VREL_EMPTY)
1413 lhs_range = int_range<2> (boolean_false_node, boolean_false_node);
1414 // x || y is true if the union of the true cases is NO-RELATION..
1415 // ie, one or the other being true covers the full range of possibilties.
1416 else if (!is_and && relation_union (relation1, relation2) == VREL_NONE)
1417 lhs_range = bool_one;
1418 else
1419 return;
1420
1421 range_cast (lhs_range, TREE_TYPE (lhs));
1422 if (dump_file && (dump_flags & TDF_DETAILS))
1423 {
1424 fprintf (dump_file, " Relation adjustment: ");
1425 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1426 fprintf (dump_file, " and ");
1427 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1428 fprintf (dump_file, " combine to produce ");
1429 lhs_range.dump (dump_file);
1430 fputc ('\n', dump_file);
1431 }
1432
1433 return;
1434 }
1435
1436 // Register any outgoing edge relations from a conditional branch.
1437
1438 void
1439 fur_source::register_outgoing_edges (gcond *s, irange &lhs_range, edge e0, edge e1)
1440 {
1441 int_range_max r;
1442 int_range<2> e0_range, e1_range;
1443 tree name;
1444 range_operator *handler;
1445 basic_block bb = gimple_bb (s);
1446
1447 if (e0)
1448 {
1449 // If this edge is never taken, ignore it.
1450 gcond_edge_range (e0_range, e0);
1451 e0_range.intersect (lhs_range);
1452 if (e0_range.undefined_p ())
1453 e0 = NULL;
1454 }
1455
1456
1457 if (e1)
1458 {
1459 // If this edge is never taken, ignore it.
1460 gcond_edge_range (e1_range, e1);
1461 e1_range.intersect (lhs_range);
1462 if (e1_range.undefined_p ())
1463 e1 = NULL;
1464 }
1465
1466 if (!e0 && !e1)
1467 return;
1468
1469 // First, register the gcond itself. This will catch statements like
1470 // if (a_2 < b_5)
1471 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1472 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1473 if (ssa1 && ssa2)
1474 {
1475 handler = gimple_range_handler (s);
1476 gcc_checking_assert (handler);
1477 if (e0)
1478 {
1479 relation_kind relation = handler->op1_op2_relation (e0_range);
1480 if (relation != VREL_NONE)
1481 register_relation (e0, relation, ssa1, ssa2);
1482 }
1483 if (e1)
1484 {
1485 relation_kind relation = handler->op1_op2_relation (e1_range);
1486 if (relation != VREL_NONE)
1487 register_relation (e1, relation, ssa1, ssa2);
1488 }
1489 }
1490
1491 // Outgoing relations of GORI exports require a gori engine.
1492 if (!gori ())
1493 return;
1494
1495 // Now look for other relations in the exports. This will find stmts
1496 // leading to the condition such as:
1497 // c_2 = a_4 < b_7
1498 // if (c_2)
1499 FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
1500 {
1501 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1502 continue;
1503 gimple *stmt = SSA_NAME_DEF_STMT (name);
1504 handler = gimple_range_handler (stmt);
1505 if (!handler)
1506 continue;
1507 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
1508 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
1509 if (ssa1 && ssa2)
1510 {
1511 if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
1512 && r.singleton_p ())
1513 {
1514 relation_kind relation = handler->op1_op2_relation (r);
1515 if (relation != VREL_NONE)
1516 register_relation (e0, relation, ssa1, ssa2);
1517 }
1518 if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
1519 && r.singleton_p ())
1520 {
1521 relation_kind relation = handler->op1_op2_relation (r);
1522 if (relation != VREL_NONE)
1523 register_relation (e1, relation, ssa1, ssa2);
1524 }
1525 }
1526 }
1527 }