]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range.cc
Dump should be read only. Do not trigger new lookups.
[thirdparty/gcc.git] / gcc / gimple-range.cc
1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2021 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 "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "ssa.h"
31 #include "gimple-pretty-print.h"
32 #include "gimple-iterator.h"
33 #include "optabs-tree.h"
34 #include "gimple-fold.h"
35 #include "tree-cfg.h"
36 #include "fold-const.h"
37 #include "tree-cfg.h"
38 #include "wide-int.h"
39 #include "fold-const.h"
40 #include "case-cfn-macros.h"
41 #include "omp-general.h"
42 #include "cfgloop.h"
43 #include "tree-ssa-loop.h"
44 #include "tree-scalar-evolution.h"
45 #include "dbgcnt.h"
46 #include "alloc-pool.h"
47 #include "vr-values.h"
48 #include "gimple-range.h"
49
50 // Construct a fur_source, and set the m_query field.
51
52 fur_source::fur_source (range_query *q)
53 {
54 if (q)
55 m_query = q;
56 else if (cfun)
57 m_query = get_range_query (cfun);
58 else
59 m_query = get_global_range_query ();
60 m_gori = NULL;
61 }
62
63 // Invoke range_of_expr on EXPR.
64
65 bool
66 fur_source::get_operand (irange &r, tree expr)
67 {
68 return m_query->range_of_expr (r, expr);
69 }
70
71 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
72 // range_query to get the range on the edge.
73
74 bool
75 fur_source::get_phi_operand (irange &r, tree expr, edge e)
76 {
77 return m_query->range_on_edge (r, e, expr);
78 }
79
80 // Default is no relation.
81
82 relation_kind
83 fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
84 tree op2 ATTRIBUTE_UNUSED)
85 {
86 return VREL_NONE;
87 }
88
89 // Default registers nothing.
90
91 void
92 fur_source::register_relation (gimple *s ATTRIBUTE_UNUSED,
93 relation_kind k ATTRIBUTE_UNUSED,
94 tree op1 ATTRIBUTE_UNUSED,
95 tree op2 ATTRIBUTE_UNUSED)
96 {
97 }
98
99 // Default registers nothing.
100
101 void
102 fur_source::register_relation (edge e ATTRIBUTE_UNUSED,
103 relation_kind k ATTRIBUTE_UNUSED,
104 tree op1 ATTRIBUTE_UNUSED,
105 tree op2 ATTRIBUTE_UNUSED)
106 {
107 }
108
109 // This version of fur_source will pick a range up off an edge.
110
111 class fur_edge : public fur_source
112 {
113 public:
114 fur_edge (edge e, range_query *q = NULL);
115 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
116 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
117 private:
118 edge m_edge;
119 };
120
121 // Instantiate an edge based fur_source.
122
123 inline
124 fur_edge::fur_edge (edge e, range_query *q) : fur_source (q)
125 {
126 m_edge = e;
127 }
128
129 // Get the value of EXPR on edge m_edge.
130
131 bool
132 fur_edge::get_operand (irange &r, tree expr)
133 {
134 return m_query->range_on_edge (r, m_edge, expr);
135 }
136
137 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
138 // range_query to get the range on the edge.
139
140 bool
141 fur_edge::get_phi_operand (irange &r, tree expr, edge e)
142 {
143 // Edge to edge recalculations not supoprted yet, until we sort it out.
144 gcc_checking_assert (e == m_edge);
145 return m_query->range_on_edge (r, e, expr);
146 }
147
148 // Instantiate a stmt based fur_source.
149
150 fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
151 {
152 m_stmt = s;
153 }
154
155 // Retreive range of EXPR as it occurs as a use on stmt M_STMT.
156
157 bool
158 fur_stmt::get_operand (irange &r, tree expr)
159 {
160 return m_query->range_of_expr (r, expr, m_stmt);
161 }
162
163 // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
164 // range_query to get the range on the edge.
165
166 bool
167 fur_stmt::get_phi_operand (irange &r, tree expr, edge e)
168 {
169 // Pick up the range of expr from edge E.
170 fur_edge e_src (e, m_query);
171 return e_src.get_operand (r, expr);
172 }
173
174 // Return relation based from m_stmt.
175
176 relation_kind
177 fur_stmt::query_relation (tree op1, tree op2)
178 {
179 return m_query->query_relation (m_stmt, op1, op2);
180 }
181
182 // This version of fur_source will pick a range from a stmt, and also register
183 // dependencies via a gori_compute object. This is mostly an internal API.
184
185 class fur_depend : public fur_stmt
186 {
187 public:
188 fur_depend (gimple *s, gori_compute *gori, range_query *q = NULL);
189 virtual void register_relation (gimple *stmt, relation_kind k, tree op1,
190 tree op2) OVERRIDE;
191 virtual void register_relation (edge e, relation_kind k, tree op1,
192 tree op2) OVERRIDE;
193 private:
194 relation_oracle *m_oracle;
195 };
196
197 // Instantiate a stmt based fur_source with a GORI object.
198
199 inline
200 fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
201 : fur_stmt (s, q)
202 {
203 gcc_checking_assert (gori);
204 m_gori = gori;
205 // Set relations if there is an oracle in the range_query.
206 // This will enable registering of relationships as they are discovered.
207 m_oracle = q->oracle ();
208
209 }
210
211 // Register a relation on a stmt if there is an oracle.
212
213 void
214 fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
215 {
216 if (m_oracle)
217 m_oracle->register_relation (s, k, op1, op2);
218 }
219
220 // Register a relation on an edge if there is an oracle.
221
222 void
223 fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
224 {
225 if (m_oracle)
226 m_oracle->register_relation (e, k, op1, op2);
227 }
228
229 // This version of fur_source will pick a range up from a list of ranges
230 // supplied by the caller.
231
232 class fur_list : public fur_source
233 {
234 public:
235 fur_list (irange &r1);
236 fur_list (irange &r1, irange &r2);
237 fur_list (unsigned num, irange *list);
238 virtual bool get_operand (irange &r, tree expr) OVERRIDE;
239 virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
240 private:
241 int_range_max m_local[2];
242 irange *m_list;
243 unsigned m_index;
244 unsigned m_limit;
245 };
246
247 // One range supplied for unary operations.
248
249 fur_list::fur_list (irange &r1) : fur_source (NULL)
250 {
251 m_list = m_local;
252 m_index = 0;
253 m_limit = 1;
254 m_local[0] = r1;
255 }
256
257 // Two ranges supplied for binary operations.
258
259 fur_list::fur_list (irange &r1, irange &r2) : fur_source (NULL)
260 {
261 m_list = m_local;
262 m_index = 0;
263 m_limit = 2;
264 m_local[0] = r1;
265 m_local[0] = r2;
266 }
267
268 // Arbitrary number of ranges in a vector.
269
270 fur_list::fur_list (unsigned num, irange *list) : fur_source (NULL)
271 {
272 m_list = list;
273 m_index = 0;
274 m_limit = num;
275 }
276
277 // Get the next operand from the vector, ensure types are compatible.
278
279 bool
280 fur_list::get_operand (irange &r, tree expr)
281 {
282 if (m_index >= m_limit)
283 return m_query->range_of_expr (r, expr);
284 r = m_list[m_index++];
285 gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
286 return true;
287 }
288
289 // This will simply pick the next operand from the vector.
290 bool
291 fur_list::get_phi_operand (irange &r, tree expr, edge e ATTRIBUTE_UNUSED)
292 {
293 return get_operand (r, expr);
294 }
295
296 // Fold stmt S into range R using R1 as the first operand.
297
298 bool
299 fold_range (irange &r, gimple *s, irange &r1)
300 {
301 fold_using_range f;
302 fur_list src (r1);
303 return f.fold_stmt (r, s, src);
304 }
305
306 // Fold stmt S into range R using R1 and R2 as the first two operands.
307
308 bool
309 fold_range (irange &r, gimple *s, irange &r1, irange &r2)
310 {
311 fold_using_range f;
312 fur_list src (r1, r2);
313 return f.fold_stmt (r, s, src);
314 }
315
316 // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
317 // operands encountered.
318
319 bool
320 fold_range (irange &r, gimple *s, unsigned num_elements, irange *vector)
321 {
322 fold_using_range f;
323 fur_list src (num_elements, vector);
324 return f.fold_stmt (r, s, src);
325 }
326
327 // Fold stmt S into range R using range query Q.
328
329 bool
330 fold_range (irange &r, gimple *s, range_query *q)
331 {
332 fold_using_range f;
333 fur_stmt src (s, q);
334 return f.fold_stmt (r, s, src);
335 }
336
337 // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
338
339 bool
340 fold_range (irange &r, gimple *s, edge on_edge, range_query *q)
341 {
342 fold_using_range f;
343 fur_edge src (on_edge, q);
344 return f.fold_stmt (r, s, src);
345 }
346
347 // -------------------------------------------------------------------------
348
349 // Adjust the range for a pointer difference where the operands came
350 // from a memchr.
351 //
352 // This notices the following sequence:
353 //
354 // def = __builtin_memchr (arg, 0, sz)
355 // n = def - arg
356 //
357 // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
358
359 static void
360 adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
361 {
362 tree op0 = gimple_assign_rhs1 (diff_stmt);
363 tree op1 = gimple_assign_rhs2 (diff_stmt);
364 tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
365 tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
366 gimple *call;
367
368 if (TREE_CODE (op0) == SSA_NAME
369 && TREE_CODE (op1) == SSA_NAME
370 && (call = SSA_NAME_DEF_STMT (op0))
371 && is_gimple_call (call)
372 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
373 && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
374 && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
375 && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
376 && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
377 && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
378 && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
379 && integer_zerop (gimple_call_arg (call, 1)))
380 {
381 tree max = vrp_val_max (ptrdiff_type_node);
382 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
383 tree expr_type = gimple_expr_type (diff_stmt);
384 tree range_min = build_zero_cst (expr_type);
385 tree range_max = wide_int_to_tree (expr_type, wmax - 1);
386 int_range<2> r (range_min, range_max);
387 res.intersect (r);
388 }
389 }
390
391 // This function looks for situations when walking the use/def chains
392 // may provide additonal contextual range information not exposed on
393 // this statement. Like knowing the IMAGPART return value from a
394 // builtin function is a boolean result.
395
396 // We should rework how we're called, as we have an op_unknown entry
397 // for IMAGPART_EXPR and POINTER_DIFF_EXPR in range-ops just so this
398 // function gets called.
399
400 static void
401 gimple_range_adjustment (irange &res, const gimple *stmt)
402 {
403 switch (gimple_expr_code (stmt))
404 {
405 case POINTER_DIFF_EXPR:
406 adjust_pointer_diff_expr (res, stmt);
407 return;
408
409 case IMAGPART_EXPR:
410 {
411 tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
412 if (TREE_CODE (name) == SSA_NAME)
413 {
414 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
415 if (def_stmt && is_gimple_call (def_stmt)
416 && gimple_call_internal_p (def_stmt))
417 {
418 switch (gimple_call_internal_fn (def_stmt))
419 {
420 case IFN_ADD_OVERFLOW:
421 case IFN_SUB_OVERFLOW:
422 case IFN_MUL_OVERFLOW:
423 case IFN_ATOMIC_COMPARE_EXCHANGE:
424 {
425 int_range<2> r;
426 r.set_varying (boolean_type_node);
427 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
428 range_cast (r, type);
429 res.intersect (r);
430 }
431 default:
432 break;
433 }
434 }
435 }
436 break;
437 }
438
439 default:
440 break;
441 }
442 }
443
444 // Return the base of the RHS of an assignment.
445
446 static tree
447 gimple_range_base_of_assignment (const gimple *stmt)
448 {
449 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
450 tree op1 = gimple_assign_rhs1 (stmt);
451 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
452 return get_base_address (TREE_OPERAND (op1, 0));
453 return op1;
454 }
455
456 // Return the first operand of this statement if it is a valid operand
457 // supported by ranges, otherwise return NULL_TREE. Special case is
458 // &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
459
460 tree
461 gimple_range_operand1 (const gimple *stmt)
462 {
463 gcc_checking_assert (gimple_range_handler (stmt));
464
465 switch (gimple_code (stmt))
466 {
467 case GIMPLE_COND:
468 return gimple_cond_lhs (stmt);
469 case GIMPLE_ASSIGN:
470 {
471 tree base = gimple_range_base_of_assignment (stmt);
472 if (base && TREE_CODE (base) == MEM_REF)
473 {
474 // If the base address is an SSA_NAME, we return it
475 // here. This allows processing of the range of that
476 // name, while the rest of the expression is simply
477 // ignored. The code in range_ops will see the
478 // ADDR_EXPR and do the right thing.
479 tree ssa = TREE_OPERAND (base, 0);
480 if (TREE_CODE (ssa) == SSA_NAME)
481 return ssa;
482 }
483 return base;
484 }
485 default:
486 break;
487 }
488 return NULL;
489 }
490
491 // Return the second operand of statement STMT, otherwise return NULL_TREE.
492
493 tree
494 gimple_range_operand2 (const gimple *stmt)
495 {
496 gcc_checking_assert (gimple_range_handler (stmt));
497
498 switch (gimple_code (stmt))
499 {
500 case GIMPLE_COND:
501 return gimple_cond_rhs (stmt);
502 case GIMPLE_ASSIGN:
503 if (gimple_num_ops (stmt) >= 3)
504 return gimple_assign_rhs2 (stmt);
505 default:
506 break;
507 }
508 return NULL_TREE;
509 }
510
511 // Calculate what we can determine of the range of this unary
512 // statement's operand if the lhs of the expression has the range
513 // LHS_RANGE. Return false if nothing can be determined.
514
515 bool
516 gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range)
517 {
518 gcc_checking_assert (gimple_num_ops (stmt) < 3);
519
520 // An empty range is viral.
521 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
522 if (lhs_range.undefined_p ())
523 {
524 r.set_undefined ();
525 return true;
526 }
527 // Unary operations require the type of the first operand in the
528 // second range position.
529 int_range<2> type_range (type);
530 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
531 type_range);
532 }
533
534 // Calculate what we can determine of the range of this statement's
535 // first operand if the lhs of the expression has the range LHS_RANGE
536 // and the second operand has the range OP2_RANGE. Return false if
537 // nothing can be determined.
538
539 bool
540 gimple_range_calc_op1 (irange &r, const gimple *stmt,
541 const irange &lhs_range, const irange &op2_range)
542 {
543 // Unary operation are allowed to pass a range in for second operand
544 // as there are often additional restrictions beyond the type which
545 // can be imposed. See operator_cast::op1_range().
546 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
547 // An empty range is viral.
548 if (op2_range.undefined_p () || lhs_range.undefined_p ())
549 {
550 r.set_undefined ();
551 return true;
552 }
553 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
554 op2_range);
555 }
556
557 // Calculate what we can determine of the range of this statement's
558 // second operand if the lhs of the expression has the range LHS_RANGE
559 // and the first operand has the range OP1_RANGE. Return false if
560 // nothing can be determined.
561
562 bool
563 gimple_range_calc_op2 (irange &r, const gimple *stmt,
564 const irange &lhs_range, const irange &op1_range)
565 {
566 tree type = TREE_TYPE (gimple_range_operand2 (stmt));
567 // An empty range is viral.
568 if (op1_range.undefined_p () || lhs_range.undefined_p ())
569 {
570 r.set_undefined ();
571 return true;
572 }
573 return gimple_range_handler (stmt)->op2_range (r, type, lhs_range,
574 op1_range);
575 }
576
577 // Calculate a range for statement S and return it in R. If NAME is provided it
578 // represents the SSA_NAME on the LHS of the statement. It is only required
579 // if there is more than one lhs/output. If a range cannot
580 // be calculated, return false.
581
582 bool
583 fold_using_range::fold_stmt (irange &r, gimple *s, fur_source &src, tree name)
584 {
585 bool res = false;
586 // If name and S are specified, make sure it is an LHS of S.
587 gcc_checking_assert (!name || !gimple_get_lhs (s) ||
588 name == gimple_get_lhs (s));
589
590 if (!name)
591 name = gimple_get_lhs (s);
592
593 // Process addresses.
594 if (gimple_code (s) == GIMPLE_ASSIGN
595 && gimple_assign_rhs_code (s) == ADDR_EXPR)
596 return range_of_address (r, s, src);
597
598 if (gimple_range_handler (s))
599 res = range_of_range_op (r, s, src);
600 else if (is_a<gphi *>(s))
601 res = range_of_phi (r, as_a<gphi *> (s), src);
602 else if (is_a<gcall *>(s))
603 res = range_of_call (r, as_a<gcall *> (s), src);
604 else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
605 res = range_of_cond_expr (r, as_a<gassign *> (s), src);
606
607 if (!res)
608 {
609 // If no name is specified, try the expression kind.
610 if (!name)
611 {
612 tree t = gimple_expr_type (s);
613 if (!irange::supports_type_p (t))
614 return false;
615 r.set_varying (t);
616 return true;
617 }
618 if (!gimple_range_ssa_p (name))
619 return false;
620 // We don't understand the stmt, so return the global range.
621 r = gimple_range_global (name);
622 return true;
623 }
624
625 if (r.undefined_p ())
626 return true;
627
628 // We sometimes get compatible types copied from operands, make sure
629 // the correct type is being returned.
630 if (name && TREE_TYPE (name) != r.type ())
631 {
632 gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
633 range_cast (r, TREE_TYPE (name));
634 }
635 return true;
636 }
637
638 // Calculate a range for range_op statement S and return it in R. If any
639 // If a range cannot be calculated, return false.
640
641 bool
642 fold_using_range::range_of_range_op (irange &r, gimple *s, fur_source &src)
643 {
644 int_range_max range1, range2;
645 tree type = gimple_expr_type (s);
646 range_operator *handler = gimple_range_handler (s);
647 gcc_checking_assert (handler);
648 gcc_checking_assert (irange::supports_type_p (type));
649
650 tree lhs = gimple_get_lhs (s);
651 tree op1 = gimple_range_operand1 (s);
652 tree op2 = gimple_range_operand2 (s);
653
654 if (src.get_operand (range1, op1))
655 {
656 if (!op2)
657 {
658 // Fold range, and register any dependency if available.
659 int_range<2> r2 (type);
660 handler->fold_range (r, type, range1, r2);
661 if (lhs && gimple_range_ssa_p (op1))
662 {
663 if (src.gori ())
664 src.gori ()->register_dependency (lhs, op1);
665 relation_kind rel;
666 rel = handler->lhs_op1_relation (r, range1, range1);
667 if (rel != VREL_NONE)
668 src.register_relation (s, rel, lhs, op1);
669 }
670 }
671 else if (src.get_operand (range2, op2))
672 {
673 relation_kind rel = src.query_relation (op1, op2);
674 if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_NONE)
675 {
676 fprintf (dump_file, " folding with relation ");
677 print_relation (dump_file, rel);
678 fputc ('\n', dump_file);
679 }
680 // Fold range, and register any dependency if available.
681 handler->fold_range (r, type, range1, range2, rel);
682 relation_fold_and_or (r, s, src);
683 if (lhs)
684 {
685 if (src.gori ())
686 {
687 src.gori ()->register_dependency (lhs, op1);
688 src.gori ()->register_dependency (lhs, op2);
689 }
690 if (gimple_range_ssa_p (op1))
691 {
692 rel = handler->lhs_op1_relation (r, range1, range2);
693 if (rel != VREL_NONE)
694 src.register_relation (s, rel, lhs, op1);
695 }
696 if (gimple_range_ssa_p (op2))
697 {
698 rel= handler->lhs_op2_relation (r, range1, range2);
699 if (rel != VREL_NONE)
700 src.register_relation (s, rel, lhs, op2);
701 }
702 }
703 else if (is_a<gcond *> (s))
704 postfold_gcond_edges (as_a<gcond *> (s), src);
705 }
706 else
707 r.set_varying (type);
708 }
709 else
710 r.set_varying (type);
711 // Make certain range-op adjustments that aren't handled any other way.
712 gimple_range_adjustment (r, s);
713 return true;
714 }
715
716 // Calculate the range of an assignment containing an ADDR_EXPR.
717 // Return the range in R.
718 // If a range cannot be calculated, set it to VARYING and return true.
719
720 bool
721 fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
722 {
723 gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
724 gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
725
726 bool strict_overflow_p;
727 tree expr = gimple_assign_rhs1 (stmt);
728 poly_int64 bitsize, bitpos;
729 tree offset;
730 machine_mode mode;
731 int unsignedp, reversep, volatilep;
732 tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
733 &bitpos, &offset, &mode, &unsignedp,
734 &reversep, &volatilep);
735
736
737 if (base != NULL_TREE
738 && TREE_CODE (base) == MEM_REF
739 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
740 {
741 tree ssa = TREE_OPERAND (base, 0);
742 tree lhs = gimple_get_lhs (stmt);
743 if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
744 src.gori ()->register_dependency (lhs, ssa);
745 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
746 src.get_operand (r, ssa);
747 range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
748
749 poly_offset_int off = 0;
750 bool off_cst = false;
751 if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
752 {
753 off = mem_ref_offset (base);
754 if (offset)
755 off += poly_offset_int::from (wi::to_poly_wide (offset),
756 SIGNED);
757 off <<= LOG2_BITS_PER_UNIT;
758 off += bitpos;
759 off_cst = true;
760 }
761 /* If &X->a is equal to X, the range of X is the result. */
762 if (off_cst && known_eq (off, 0))
763 return true;
764 else if (flag_delete_null_pointer_checks
765 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
766 {
767 /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
768 allow going from non-NULL pointer to NULL. */
769 if(!range_includes_zero_p (&r))
770 return true;
771 }
772 /* If MEM_REF has a "positive" offset, consider it non-NULL
773 always, for -fdelete-null-pointer-checks also "negative"
774 ones. Punt for unknown offsets (e.g. variable ones). */
775 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
776 && off_cst
777 && known_ne (off, 0)
778 && (flag_delete_null_pointer_checks || known_gt (off, 0)))
779 {
780 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
781 return true;
782 }
783 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
784 return true;
785 }
786
787 // Handle "= &a".
788 if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
789 {
790 r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
791 return true;
792 }
793
794 // Otherwise return varying.
795 r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
796 return true;
797 }
798
799 // Calculate a range for phi statement S and return it in R.
800 // If a range cannot be calculated, return false.
801
802 bool
803 fold_using_range::range_of_phi (irange &r, gphi *phi, fur_source &src)
804 {
805 tree phi_def = gimple_phi_result (phi);
806 tree type = TREE_TYPE (phi_def);
807 int_range_max arg_range;
808 unsigned x;
809
810 if (!irange::supports_type_p (type))
811 return false;
812
813 // Start with an empty range, unioning in each argument's range.
814 r.set_undefined ();
815 for (x = 0; x < gimple_phi_num_args (phi); x++)
816 {
817 tree arg = gimple_phi_arg_def (phi, x);
818 edge e = gimple_phi_arg_edge (phi, x);
819
820 // Register potential dependencies for stale value tracking.
821 if (gimple_range_ssa_p (arg) && src.gori ())
822 src.gori ()->register_dependency (phi_def, arg);
823
824 // Get the range of the argument on its edge.
825 src.get_phi_operand (arg_range, arg, e);
826 // If we're recomputing the argument elsewhere, try to refine it.
827 r.union_ (arg_range);
828 // Once the value reaches varying, stop looking.
829 if (r.varying_p ())
830 break;
831 }
832
833 // If SCEV is available, query if this PHI has any knonwn values.
834 if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
835 {
836 value_range loop_range;
837 class loop *l = loop_containing_stmt (phi);
838 if (l && loop_outer (l))
839 {
840 range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
841 if (!loop_range.varying_p ())
842 {
843 if (dump_file && (dump_flags & TDF_DETAILS))
844 {
845 fprintf (dump_file, " Loops range found for ");
846 print_generic_expr (dump_file, phi_def, TDF_SLIM);
847 fprintf (dump_file, ": ");
848 loop_range.dump (dump_file);
849 fprintf (dump_file, " and calculated range :");
850 r.dump (dump_file);
851 fprintf (dump_file, "\n");
852 }
853 r.intersect (loop_range);
854 }
855 }
856 }
857
858 return true;
859 }
860
861 // Calculate a range for call statement S and return it in R.
862 // If a range cannot be calculated, return false.
863
864 bool
865 fold_using_range::range_of_call (irange &r, gcall *call, fur_source &src)
866 {
867 tree type = gimple_call_return_type (call);
868 tree lhs = gimple_call_lhs (call);
869 bool strict_overflow_p;
870
871 if (!irange::supports_type_p (type))
872 return false;
873
874 if (range_of_builtin_call (r, call, src))
875 ;
876 else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
877 r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
878 else if (gimple_call_nonnull_result_p (call)
879 || gimple_call_nonnull_arg (call))
880 r = range_nonzero (type);
881 else
882 r.set_varying (type);
883
884 // If there is an LHS, intersect that with what is known.
885 if (lhs)
886 {
887 value_range def;
888 def = gimple_range_global (lhs);
889 r.intersect (def);
890 }
891 return true;
892 }
893
894 // Return the range of a __builtin_ubsan* in CALL and set it in R.
895 // CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
896 // MULT_EXPR).
897
898 void
899 fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
900 tree_code code, fur_source &src)
901 {
902 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
903 || code == MULT_EXPR);
904 tree type = gimple_call_return_type (call);
905 range_operator *op = range_op_handler (code, type);
906 gcc_checking_assert (op);
907 int_range_max ir0, ir1;
908 tree arg0 = gimple_call_arg (call, 0);
909 tree arg1 = gimple_call_arg (call, 1);
910 src.get_operand (ir0, arg0);
911 src.get_operand (ir1, arg1);
912
913 bool saved_flag_wrapv = flag_wrapv;
914 // Pretend the arithmetic is wrapping. If there is any overflow,
915 // we'll complain, but will actually do wrapping operation.
916 flag_wrapv = 1;
917 op->fold_range (r, type, ir0, ir1);
918 flag_wrapv = saved_flag_wrapv;
919
920 // If for both arguments vrp_valueize returned non-NULL, this should
921 // have been already folded and if not, it wasn't folded because of
922 // overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
923 if (r.singleton_p ())
924 r.set_varying (type);
925 }
926
927 // For a builtin in CALL, return a range in R if known and return
928 // TRUE. Otherwise return FALSE.
929
930 bool
931 fold_using_range::range_of_builtin_call (irange &r, gcall *call,
932 fur_source &src)
933 {
934 combined_fn func = gimple_call_combined_fn (call);
935 if (func == CFN_LAST)
936 return false;
937
938 tree type = gimple_call_return_type (call);
939 tree arg;
940 int mini, maxi, zerov = 0, prec;
941 scalar_int_mode mode;
942
943 switch (func)
944 {
945 case CFN_BUILT_IN_CONSTANT_P:
946 if (cfun->after_inlining)
947 {
948 r.set_zero (type);
949 // r.equiv_clear ();
950 return true;
951 }
952 arg = gimple_call_arg (call, 0);
953 if (src.get_operand (r, arg) && r.singleton_p ())
954 {
955 r.set (build_one_cst (type), build_one_cst (type));
956 return true;
957 }
958 break;
959
960 CASE_CFN_FFS:
961 CASE_CFN_POPCOUNT:
962 // __builtin_ffs* and __builtin_popcount* return [0, prec].
963 arg = gimple_call_arg (call, 0);
964 prec = TYPE_PRECISION (TREE_TYPE (arg));
965 mini = 0;
966 maxi = prec;
967 src.get_operand (r, arg);
968 // If arg is non-zero, then ffs or popcount are non-zero.
969 if (!range_includes_zero_p (&r))
970 mini = 1;
971 // If some high bits are known to be zero, decrease the maximum.
972 if (!r.undefined_p ())
973 {
974 if (TYPE_SIGN (r.type ()) == SIGNED)
975 range_cast (r, unsigned_type_for (r.type ()));
976 wide_int max = r.upper_bound ();
977 maxi = wi::floor_log2 (max) + 1;
978 }
979 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
980 return true;
981
982 CASE_CFN_PARITY:
983 r.set (build_zero_cst (type), build_one_cst (type));
984 return true;
985
986 CASE_CFN_CLZ:
987 // __builtin_c[lt]z* return [0, prec-1], except when the
988 // argument is 0, but that is undefined behavior.
989 //
990 // For __builtin_c[lt]z* consider argument of 0 always undefined
991 // behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
992 arg = gimple_call_arg (call, 0);
993 prec = TYPE_PRECISION (TREE_TYPE (arg));
994 mini = 0;
995 maxi = prec - 1;
996 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
997 if (gimple_call_internal_p (call))
998 {
999 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
1000 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1001 {
1002 // Only handle the single common value.
1003 if (zerov == prec)
1004 maxi = prec;
1005 else
1006 // Magic value to give up, unless we can prove arg is non-zero.
1007 mini = -2;
1008 }
1009 }
1010
1011 src.get_operand (r, arg);
1012 // From clz of minimum we can compute result maximum.
1013 if (!r.undefined_p ())
1014 {
1015 // From clz of minimum we can compute result maximum.
1016 if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
1017 {
1018 maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
1019 if (mini == -2)
1020 mini = 0;
1021 }
1022 else if (!range_includes_zero_p (&r))
1023 {
1024 mini = 0;
1025 maxi = prec - 1;
1026 }
1027 if (mini == -2)
1028 break;
1029 // From clz of maximum we can compute result minimum.
1030 wide_int max = r.upper_bound ();
1031 int newmini = prec - 1 - wi::floor_log2 (max);
1032 if (max == 0)
1033 {
1034 // If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
1035 // return [prec, prec], otherwise ignore the range.
1036 if (maxi == prec)
1037 mini = prec;
1038 }
1039 else
1040 mini = newmini;
1041 }
1042 if (mini == -2)
1043 break;
1044 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1045 return true;
1046
1047 CASE_CFN_CTZ:
1048 // __builtin_ctz* return [0, prec-1], except for when the
1049 // argument is 0, but that is undefined behavior.
1050 //
1051 // For __builtin_ctz* consider argument of 0 always undefined
1052 // behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
1053 arg = gimple_call_arg (call, 0);
1054 prec = TYPE_PRECISION (TREE_TYPE (arg));
1055 mini = 0;
1056 maxi = prec - 1;
1057 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
1058 if (gimple_call_internal_p (call))
1059 {
1060 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
1061 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
1062 {
1063 // Handle only the two common values.
1064 if (zerov == -1)
1065 mini = -1;
1066 else if (zerov == prec)
1067 maxi = prec;
1068 else
1069 // Magic value to give up, unless we can prove arg is non-zero.
1070 mini = -2;
1071 }
1072 }
1073 src.get_operand (r, arg);
1074 if (!r.undefined_p ())
1075 {
1076 // If arg is non-zero, then use [0, prec - 1].
1077 if (!range_includes_zero_p (&r))
1078 {
1079 mini = 0;
1080 maxi = prec - 1;
1081 }
1082 // If some high bits are known to be zero, we can decrease
1083 // the maximum.
1084 wide_int max = r.upper_bound ();
1085 if (max == 0)
1086 {
1087 // Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
1088 // is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
1089 // Otherwise ignore the range.
1090 if (mini == -1)
1091 maxi = -1;
1092 else if (maxi == prec)
1093 mini = prec;
1094 }
1095 // If value at zero is prec and 0 is in the range, we can't lower
1096 // the upper bound. We could create two separate ranges though,
1097 // [0,floor_log2(max)][prec,prec] though.
1098 else if (maxi != prec)
1099 maxi = wi::floor_log2 (max);
1100 }
1101 if (mini == -2)
1102 break;
1103 r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
1104 return true;
1105
1106 CASE_CFN_CLRSB:
1107 arg = gimple_call_arg (call, 0);
1108 prec = TYPE_PRECISION (TREE_TYPE (arg));
1109 r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
1110 return true;
1111 case CFN_UBSAN_CHECK_ADD:
1112 range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
1113 return true;
1114 case CFN_UBSAN_CHECK_SUB:
1115 range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
1116 return true;
1117 case CFN_UBSAN_CHECK_MUL:
1118 range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
1119 return true;
1120
1121 case CFN_GOACC_DIM_SIZE:
1122 case CFN_GOACC_DIM_POS:
1123 // Optimizing these two internal functions helps the loop
1124 // optimizer eliminate outer comparisons. Size is [1,N]
1125 // and pos is [0,N-1].
1126 {
1127 bool is_pos = func == CFN_GOACC_DIM_POS;
1128 int axis = oacc_get_ifn_dim_arg (call);
1129 int size = oacc_get_fn_dim_size (current_function_decl, axis);
1130 if (!size)
1131 // If it's dynamic, the backend might know a hardware limitation.
1132 size = targetm.goacc.dim_limit (axis);
1133
1134 r.set (build_int_cst (type, is_pos ? 0 : 1),
1135 size
1136 ? build_int_cst (type, size - is_pos) : vrp_val_max (type));
1137 return true;
1138 }
1139
1140 case CFN_BUILT_IN_STRLEN:
1141 if (tree lhs = gimple_call_lhs (call))
1142 if (ptrdiff_type_node
1143 && (TYPE_PRECISION (ptrdiff_type_node)
1144 == TYPE_PRECISION (TREE_TYPE (lhs))))
1145 {
1146 tree type = TREE_TYPE (lhs);
1147 tree max = vrp_val_max (ptrdiff_type_node);
1148 wide_int wmax
1149 = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
1150 tree range_min = build_zero_cst (type);
1151 // To account for the terminating NULL, the maximum length
1152 // is one less than the maximum array size, which in turn
1153 // is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1154 // smaller than the former type).
1155 // FIXME: Use max_object_size() - 1 here.
1156 tree range_max = wide_int_to_tree (type, wmax - 2);
1157 r.set (range_min, range_max);
1158 return true;
1159 }
1160 break;
1161 default:
1162 break;
1163 }
1164 return false;
1165 }
1166
1167
1168 // Calculate a range for COND_EXPR statement S and return it in R.
1169 // If a range cannot be calculated, return false.
1170
1171 bool
1172 fold_using_range::range_of_cond_expr (irange &r, gassign *s, fur_source &src)
1173 {
1174 int_range_max cond_range, range1, range2;
1175 tree cond = gimple_assign_rhs1 (s);
1176 tree op1 = gimple_assign_rhs2 (s);
1177 tree op2 = gimple_assign_rhs3 (s);
1178
1179 gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1180 gcc_checking_assert (useless_type_conversion_p (TREE_TYPE (op1),
1181 TREE_TYPE (op2)));
1182 if (!irange::supports_type_p (TREE_TYPE (op1)))
1183 return false;
1184
1185 src.get_operand (cond_range, cond);
1186 src.get_operand (range1, op1);
1187 src.get_operand (range2, op2);
1188
1189 // If the condition is known, choose the appropriate expression.
1190 if (cond_range.singleton_p ())
1191 {
1192 // False, pick second operand.
1193 if (cond_range.zero_p ())
1194 r = range2;
1195 else
1196 r = range1;
1197 }
1198 else
1199 {
1200 r = range1;
1201 r.union_ (range2);
1202 }
1203 return true;
1204 }
1205
1206 gimple_ranger::gimple_ranger ()
1207 {
1208 // If the cache has a relation oracle, use it.
1209 m_oracle = m_cache.oracle ();
1210 }
1211
1212 bool
1213 gimple_ranger::range_of_expr (irange &r, tree expr, gimple *stmt)
1214 {
1215 if (!gimple_range_ssa_p (expr))
1216 return get_tree_range (r, expr, stmt);
1217
1218 // If there is no statement, just get the global value.
1219 if (!stmt)
1220 {
1221 if (!m_cache.get_global_range (r, expr))
1222 r = gimple_range_global (expr);
1223 return true;
1224 }
1225
1226 // For a debug stmt, pick the best value currently available, do not
1227 // trigger new value calculations. PR 100781.
1228 if (is_gimple_debug (stmt))
1229 {
1230 m_cache.range_of_expr (r, expr, stmt);
1231 return true;
1232 }
1233 basic_block bb = gimple_bb (stmt);
1234 gimple *def_stmt = SSA_NAME_DEF_STMT (expr);
1235
1236 // If name is defined in this block, try to get an range from S.
1237 if (def_stmt && gimple_bb (def_stmt) == bb)
1238 {
1239 range_of_stmt (r, def_stmt, expr);
1240 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
1241 m_cache.m_non_null.non_null_deref_p (expr, bb))
1242 r = range_nonzero (TREE_TYPE (expr));
1243 }
1244 else
1245 // Otherwise OP comes from outside this block, use range on entry.
1246 range_on_entry (r, bb, expr);
1247
1248 return true;
1249 }
1250
1251 // Return the range of NAME on entry to block BB in R.
1252
1253 void
1254 gimple_ranger::range_on_entry (irange &r, basic_block bb, tree name)
1255 {
1256 int_range_max entry_range;
1257 gcc_checking_assert (gimple_range_ssa_p (name));
1258
1259 // Start with any known range
1260 range_of_stmt (r, SSA_NAME_DEF_STMT (name), name);
1261
1262 // Now see if there is any on_entry value which may refine it.
1263 if (m_cache.block_range (entry_range, bb, name))
1264 r.intersect (entry_range);
1265
1266 if (!cfun->can_throw_non_call_exceptions && r.varying_p () &&
1267 m_cache.m_non_null.non_null_deref_p (name, bb))
1268 r = range_nonzero (TREE_TYPE (name));
1269 }
1270
1271 // Calculate the range for NAME at the end of block BB and return it in R.
1272 // Return false if no range can be calculated.
1273
1274 void
1275 gimple_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1276 {
1277 // on-exit from the exit block?
1278 gcc_checking_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1279 gcc_checking_assert (gimple_range_ssa_p (name));
1280
1281 gimple *s = SSA_NAME_DEF_STMT (name);
1282 basic_block def_bb = gimple_bb (s);
1283 // If this is not the definition block, get the range on the last stmt in
1284 // the block... if there is one.
1285 if (def_bb != bb)
1286 s = last_stmt (bb);
1287 // If there is no statement provided, get the range_on_entry for this block.
1288 if (s)
1289 range_of_expr (r, name, s);
1290 else
1291 range_on_entry (r, bb, name);
1292 gcc_checking_assert (r.undefined_p ()
1293 || range_compatible_p (r.type (), TREE_TYPE (name)));
1294 }
1295
1296 // Calculate a range for NAME on edge E and return it in R.
1297
1298 bool
1299 gimple_ranger::range_on_edge (irange &r, edge e, tree name)
1300 {
1301 int_range_max edge_range;
1302 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name)));
1303
1304 // PHI arguments can be constants, catch these here.
1305 if (!gimple_range_ssa_p (name))
1306 return range_of_expr (r, name);
1307
1308 range_on_exit (r, e->src, name);
1309 gcc_checking_assert (r.undefined_p ()
1310 || range_compatible_p (r.type(), TREE_TYPE (name)));
1311
1312 // Check to see if NAME is defined on edge e.
1313 if (m_cache.range_on_edge (edge_range, e, name))
1314 r.intersect (edge_range);
1315
1316 return true;
1317 }
1318
1319 // fold_range wrapper for range_of_stmt to use as an internal client.
1320
1321 bool
1322 gimple_ranger::fold_range_internal (irange &r, gimple *s, tree name)
1323 {
1324 fold_using_range f;
1325 fur_depend src (s, &(gori ()), this);
1326 return f.fold_stmt (r, s, src, name);
1327 }
1328
1329 // Calculate a range for statement S and return it in R. If NAME is
1330 // provided it represents the SSA_NAME on the LHS of the statement.
1331 // It is only required if there is more than one lhs/output. Check
1332 // the global cache for NAME first to see if the evaluation can be
1333 // avoided. If a range cannot be calculated, return false and UNDEFINED.
1334
1335 bool
1336 gimple_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1337 {
1338 r.set_undefined ();
1339
1340 if (!name)
1341 name = gimple_get_lhs (s);
1342
1343 // If no name, simply call the base routine.
1344 if (!name)
1345 return fold_range_internal (r, s, NULL_TREE);
1346
1347 if (!gimple_range_ssa_p (name))
1348 return false;
1349
1350 // Check if the stmt has already been processed, and is not stale.
1351 if (m_cache.get_non_stale_global_range (r, name))
1352 return true;
1353
1354 // Otherwise calculate a new value.
1355 int_range_max tmp;
1356 fold_range_internal (tmp, s, name);
1357
1358 // Combine the new value with the old value. This is required because
1359 // the way value propagation works, when the IL changes on the fly we
1360 // can sometimes get different results. See PR 97741.
1361 r.intersect (tmp);
1362 m_cache.set_global_range (name, r);
1363
1364 return true;
1365 }
1366
1367 // This routine will export whatever global ranges are known to GCC
1368 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
1369
1370 void
1371 gimple_ranger::export_global_ranges ()
1372 {
1373 unsigned x;
1374 int_range_max r;
1375 if (dump_file)
1376 {
1377 fprintf (dump_file, "Exported global range table\n");
1378 fprintf (dump_file, "===========================\n");
1379 }
1380
1381 for ( x = 1; x < num_ssa_names; x++)
1382 {
1383 tree name = ssa_name (x);
1384 if (name && !SSA_NAME_IN_FREE_LIST (name)
1385 && gimple_range_ssa_p (name)
1386 && m_cache.get_global_range (r, name)
1387 && !r.varying_p())
1388 {
1389 bool updated = update_global_range (r, name);
1390
1391 if (updated && dump_file)
1392 {
1393 value_range vr = r;
1394 print_generic_expr (dump_file, name , TDF_SLIM);
1395 fprintf (dump_file, " --> ");
1396 vr.dump (dump_file);
1397 fprintf (dump_file, "\n");
1398 int_range_max same = vr;
1399 if (same != r)
1400 {
1401 fprintf (dump_file, " irange : ");
1402 r.dump (dump_file);
1403 fprintf (dump_file, "\n");
1404 }
1405 }
1406 }
1407 }
1408 }
1409
1410 // Print the known table values to file F.
1411
1412 void
1413 gimple_ranger::dump_bb (FILE *f, basic_block bb)
1414 {
1415 unsigned x;
1416 edge_iterator ei;
1417 edge e;
1418 int_range_max range;
1419 fprintf (f, "\n=========== BB %d ============\n", bb->index);
1420 m_cache.dump_bb (f, bb);
1421
1422 ::dump_bb (f, bb, 4, TDF_NONE);
1423
1424 // Now find any globals defined in this block.
1425 for (x = 1; x < num_ssa_names; x++)
1426 {
1427 tree name = ssa_name (x);
1428 if (gimple_range_ssa_p (name) && SSA_NAME_DEF_STMT (name) &&
1429 gimple_bb (SSA_NAME_DEF_STMT (name)) == bb &&
1430 m_cache.get_global_range (range, name))
1431 {
1432 if (!range.varying_p ())
1433 {
1434 print_generic_expr (f, name, TDF_SLIM);
1435 fprintf (f, " : ");
1436 range.dump (f);
1437 fprintf (f, "\n");
1438 }
1439
1440 }
1441 }
1442
1443 // And now outgoing edges, if they define anything.
1444 FOR_EACH_EDGE (e, ei, bb->succs)
1445 {
1446 for (x = 1; x < num_ssa_names; x++)
1447 {
1448 tree name = gimple_range_ssa_p (ssa_name (x));
1449 if (name && gori ().has_edge_range_p (name, e)
1450 && m_cache.range_on_edge (range, e, name))
1451 {
1452 gimple *s = SSA_NAME_DEF_STMT (name);
1453 // Only print the range if this is the def block, or
1454 // the on entry cache for either end of the edge is
1455 // set.
1456 if ((s && bb == gimple_bb (s)) ||
1457 m_cache.block_range (range, bb, name, false) ||
1458 m_cache.block_range (range, e->dest, name, false))
1459 {
1460 m_cache.range_on_edge (range, e, name);
1461 if (!range.varying_p ())
1462 {
1463 fprintf (f, "%d->%d ", e->src->index,
1464 e->dest->index);
1465 char c = ' ';
1466 if (e->flags & EDGE_TRUE_VALUE)
1467 fprintf (f, " (T)%c", c);
1468 else if (e->flags & EDGE_FALSE_VALUE)
1469 fprintf (f, " (F)%c", c);
1470 else
1471 fprintf (f, " ");
1472 print_generic_expr (f, name, TDF_SLIM);
1473 fprintf(f, " : \t");
1474 range.dump(f);
1475 fprintf (f, "\n");
1476 }
1477 }
1478 }
1479 }
1480 }
1481 }
1482
1483 // Print the known table values to file F.
1484
1485 void
1486 gimple_ranger::dump (FILE *f)
1487 {
1488 basic_block bb;
1489
1490 FOR_EACH_BB_FN (bb, cfun)
1491 dump_bb (f, bb);
1492
1493 m_cache.dump (f);
1494 }
1495
1496 // If SCEV has any information about phi node NAME, return it as a range in R.
1497
1498 void
1499 fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
1500 class loop *l, gphi *phi,
1501 fur_source &src)
1502 {
1503 gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1504 tree min, max, type = TREE_TYPE (name);
1505 if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
1506 {
1507 if (TREE_CODE (min) != INTEGER_CST)
1508 {
1509 if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
1510 min = wide_int_to_tree (type, r.lower_bound ());
1511 else
1512 min = vrp_val_min (type);
1513 }
1514 if (TREE_CODE (max) != INTEGER_CST)
1515 {
1516 if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
1517 max = wide_int_to_tree (type, r.upper_bound ());
1518 else
1519 max = vrp_val_max (type);
1520 }
1521 r.set (min, max);
1522 }
1523 else
1524 r.set_varying (type);
1525 }
1526
1527 // -----------------------------------------------------------------------
1528
1529 // Check if an && or || expression can be folded based on relations. ie
1530 // c_2 = a_6 > b_7
1531 // c_3 = a_6 < b_7
1532 // c_4 = c_2 && c_3
1533 // c_2 and c_3 can never be true at the same time,
1534 // Therefore c_4 can always resolve to false based purely on the relations.
1535
1536 void
1537 fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1538 fur_source &src)
1539 {
1540 // No queries or already folded.
1541 if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
1542 return;
1543
1544 // Only care about AND and OR expressions.
1545 enum tree_code code = gimple_expr_code (s);
1546 bool is_and = false;
1547 if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1548 is_and = true;
1549 else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1550 return;
1551
1552 tree lhs = gimple_get_lhs (s);
1553 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1554 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1555
1556 // Deal with || and && only when there is a full set of symbolics.
1557 if (!lhs || !ssa1 || !ssa2
1558 || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1559 || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1560 || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1561 return;
1562
1563 // Now we know its a boolean AND or OR expression with boolean operands.
1564 // Ideally we search dependencies for common names, and see what pops out.
1565 // until then, simply try to resolve direct dependencies.
1566
1567 // Both names will need to have 2 direct dependencies.
1568 tree ssa1_dep2 = src.gori ()->depend2 (ssa1);
1569 tree ssa2_dep2 = src.gori ()->depend2 (ssa2);
1570 if (!ssa1_dep2 || !ssa2_dep2)
1571 return;
1572
1573 tree ssa1_dep1 = src.gori ()->depend1 (ssa1);
1574 tree ssa2_dep1 = src.gori ()->depend1 (ssa2);
1575 // Make sure they are the same dependencies, and detect the order of the
1576 // relationship.
1577 bool reverse_op2 = true;
1578 if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1579 reverse_op2 = false;
1580 else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1581 return;
1582
1583 range_operator *handler1 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa1));
1584 range_operator *handler2 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa2));
1585
1586 int_range<2> bool_one (boolean_true_node, boolean_true_node);
1587
1588 relation_kind relation1 = handler1->op1_op2_relation (bool_one);
1589 relation_kind relation2 = handler2->op1_op2_relation (bool_one);
1590 if (relation1 == VREL_NONE || relation2 == VREL_NONE)
1591 return;
1592
1593 if (reverse_op2)
1594 relation2 = relation_negate (relation2);
1595
1596 // x && y is false if the relation intersection of the true cases is NULL.
1597 if (is_and && relation_intersect (relation1, relation2) == VREL_EMPTY)
1598 lhs_range = int_range<2> (boolean_false_node, boolean_false_node);
1599 // x || y is true if the union of the true cases is NO-RELATION..
1600 // ie, one or the other being true covers the full range of possibilties.
1601 else if (!is_and && relation_union (relation1, relation2) == VREL_NONE)
1602 lhs_range = bool_one;
1603 else
1604 return;
1605
1606 range_cast (lhs_range, TREE_TYPE (lhs));
1607 if (dump_file && (dump_flags & TDF_DETAILS))
1608 {
1609 fprintf (dump_file, " Relation adjustment: ");
1610 print_generic_expr (dump_file, ssa1, TDF_SLIM);
1611 fprintf (dump_file, " and ");
1612 print_generic_expr (dump_file, ssa2, TDF_SLIM);
1613 fprintf (dump_file, " combine to produce ");
1614 lhs_range.dump (dump_file);
1615 fputc ('\n', dump_file);
1616 }
1617
1618 return;
1619 }
1620
1621 // Register any outgoing edge relations from a conditional branch.
1622
1623 void
1624 fold_using_range::postfold_gcond_edges (gcond *s, fur_source &src)
1625 {
1626 int_range_max r;
1627 tree name;
1628 range_operator *handler;
1629 basic_block bb = gimple_bb (s);
1630
1631 edge e0 = EDGE_SUCC (bb, 0);
1632 if (!single_pred_p (e0->dest))
1633 e0 = NULL;
1634
1635 edge e1 = EDGE_SUCC (bb, 1);
1636 if (!single_pred_p (e1->dest))
1637 e1 = NULL;
1638
1639 // At least one edge needs to be single pred.
1640 if (!e0 && !e1)
1641 return;
1642
1643 // First, register the gcond itself. This will catch statements like
1644 // if (a_2 < b_5)
1645 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
1646 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
1647 if (ssa1 && ssa2)
1648 {
1649 handler = gimple_range_handler (s);
1650 gcc_checking_assert (handler);
1651 if (e0)
1652 {
1653 gcond_edge_range (r, e0);
1654 relation_kind relation = handler->op1_op2_relation (r);
1655 if (relation != VREL_NONE)
1656 src.register_relation (e0, relation, ssa1, ssa2);
1657 }
1658 if (e1)
1659 {
1660 gcond_edge_range (r, e1);
1661 relation_kind relation = handler->op1_op2_relation (r);
1662 if (relation != VREL_NONE)
1663 src.register_relation (e1, relation, ssa1, ssa2);
1664 }
1665 }
1666
1667 // Outgoing relations of GORI exports require a gori engine.
1668 if (!src.gori ())
1669 return;
1670
1671 range_query *q = src.query ();
1672 // Now look for other relations in the exports. This will find stmts
1673 // leading to the condition such as:
1674 // c_2 = a_4 < b_7
1675 // if (c_2)
1676
1677 FOR_EACH_GORI_EXPORT_NAME (*(src.gori ()), bb, name)
1678 {
1679 if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1680 continue;
1681 gimple *stmt = SSA_NAME_DEF_STMT (name);
1682 handler = gimple_range_handler (stmt);
1683 if (!handler)
1684 continue;
1685 tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
1686 tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
1687 if (ssa1 && ssa2)
1688 {
1689 if (e0 && src.gori ()->outgoing_edge_range_p (r, e0, name, *q)
1690 && r.singleton_p ())
1691 {
1692 relation_kind relation = handler->op1_op2_relation (r);
1693 if (relation != VREL_NONE)
1694 src.register_relation (e0, relation, ssa1, ssa2);
1695 }
1696 if (e1 && src.gori ()->outgoing_edge_range_p (r, e1, name, *q)
1697 && r.singleton_p ())
1698 {
1699 relation_kind relation = handler->op1_op2_relation (r);
1700 if (relation != VREL_NONE)
1701 src.register_relation (e1, relation, ssa1, ssa2);
1702 }
1703 }
1704 }
1705 }
1706 // --------------------------------------------------------------------------
1707 // trace_ranger implementation.
1708
1709
1710 trace_ranger::trace_ranger ()
1711 {
1712 indent = 0;
1713 trace_count = 0;
1714 }
1715
1716 // If dumping, return true and print the prefix for the next output line.
1717
1718 bool
1719 trace_ranger::dumping (unsigned counter, bool trailing)
1720 {
1721 if (dump_file && (dump_flags & TDF_DETAILS))
1722 {
1723 // Print counter index as well as INDENT spaces.
1724 if (!trailing)
1725 fprintf (dump_file, " %-7u ", counter);
1726 else
1727 fprintf (dump_file, " ");
1728 unsigned x;
1729 for (x = 0; x< indent; x++)
1730 fputc (' ', dump_file);
1731 return true;
1732 }
1733 return false;
1734 }
1735
1736 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
1737 // returning RESULT.
1738
1739 bool
1740 trace_ranger::trailer (unsigned counter, const char *caller, bool result,
1741 tree name, const irange &r)
1742 {
1743 if (dumping (counter, true))
1744 {
1745 indent -= bump;
1746 fputs(result ? "TRUE : " : "FALSE : ", dump_file);
1747 fprintf (dump_file, "(%u) ", counter);
1748 fputs (caller, dump_file);
1749 fputs (" (",dump_file);
1750 if (name)
1751 print_generic_expr (dump_file, name, TDF_SLIM);
1752 fputs (") ",dump_file);
1753 if (result)
1754 {
1755 r.dump (dump_file);
1756 fputc('\n', dump_file);
1757 }
1758 else
1759 fputc('\n', dump_file);
1760 // Marks the end of a request.
1761 if (indent == 0)
1762 fputc('\n', dump_file);
1763 }
1764 return result;
1765 }
1766
1767 // Tracing version of range_on_edge. Call it with printing wrappers.
1768
1769 bool
1770 trace_ranger::range_on_edge (irange &r, edge e, tree name)
1771 {
1772 unsigned idx = ++trace_count;
1773 if (dumping (idx))
1774 {
1775 fprintf (dump_file, "range_on_edge (");
1776 print_generic_expr (dump_file, name, TDF_SLIM);
1777 fprintf (dump_file, ") on edge %d->%d\n", e->src->index, e->dest->index);
1778 indent += bump;
1779 }
1780
1781 bool res = gimple_ranger::range_on_edge (r, e, name);
1782 trailer (idx, "range_on_edge", true, name, r);
1783 return res;
1784 }
1785
1786 // Tracing version of range_on_entry. Call it with printing wrappers.
1787
1788 void
1789 trace_ranger::range_on_entry (irange &r, basic_block bb, tree name)
1790 {
1791 unsigned idx = ++trace_count;
1792 if (dumping (idx))
1793 {
1794 fprintf (dump_file, "range_on_entry (");
1795 print_generic_expr (dump_file, name, TDF_SLIM);
1796 fprintf (dump_file, ") to BB %d\n", bb->index);
1797 indent += bump;
1798 }
1799
1800 gimple_ranger::range_on_entry (r, bb, name);
1801
1802 trailer (idx, "range_on_entry", true, name, r);
1803 }
1804
1805 // Tracing version of range_on_exit. Call it with printing wrappers.
1806
1807 void
1808 trace_ranger::range_on_exit (irange &r, basic_block bb, tree name)
1809 {
1810 unsigned idx = ++trace_count;
1811 if (dumping (idx))
1812 {
1813 fprintf (dump_file, "range_on_exit (");
1814 print_generic_expr (dump_file, name, TDF_SLIM);
1815 fprintf (dump_file, ") from BB %d\n", bb->index);
1816 indent += bump;
1817 }
1818
1819 gimple_ranger::range_on_exit (r, bb, name);
1820
1821 trailer (idx, "range_on_exit", true, name, r);
1822 }
1823
1824 // Tracing version of range_of_stmt. Call it with printing wrappers.
1825
1826 bool
1827 trace_ranger::range_of_stmt (irange &r, gimple *s, tree name)
1828 {
1829 bool res;
1830 unsigned idx = ++trace_count;
1831 if (dumping (idx))
1832 {
1833 fprintf (dump_file, "range_of_stmt (");
1834 if (name)
1835 print_generic_expr (dump_file, name, TDF_SLIM);
1836 fputs (") at stmt ", dump_file);
1837 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1838 indent += bump;
1839 }
1840
1841 res = gimple_ranger::range_of_stmt (r, s, name);
1842
1843 return trailer (idx, "range_of_stmt", res, name, r);
1844 }
1845
1846 // Tracing version of range_of_expr. Call it with printing wrappers.
1847
1848 bool
1849 trace_ranger::range_of_expr (irange &r, tree name, gimple *s)
1850 {
1851 bool res;
1852 unsigned idx = ++trace_count;
1853 if (dumping (idx))
1854 {
1855 fprintf (dump_file, "range_of_expr(");
1856 print_generic_expr (dump_file, name, TDF_SLIM);
1857 fputs (")", dump_file);
1858 if (s)
1859 {
1860 fputs (" at stmt ", dump_file);
1861 print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
1862 }
1863 else
1864 fputs ("\n", dump_file);
1865 indent += bump;
1866 }
1867
1868 res = gimple_ranger::range_of_expr (r, name, s);
1869
1870 return trailer (idx, "range_of_expr", res, name, r);
1871 }
1872
1873 gimple_ranger *
1874 enable_ranger (struct function *fun)
1875 {
1876 gimple_ranger *r;
1877
1878 if (param_evrp_mode & EVRP_MODE_TRACE)
1879 r = new trace_ranger;
1880 else
1881 r = new gimple_ranger;
1882
1883 fun->x_range_query = r;
1884
1885 return r;
1886 }
1887
1888 void
1889 disable_ranger (struct function *fun)
1890 {
1891 delete fun->x_range_query;
1892
1893 fun->x_range_query = &global_ranges;
1894 }
1895
1896 // =========================================
1897 // Debugging helpers.
1898 // =========================================
1899
1900 // Query all statements in the IL to precalculate computable ranges in RANGER.
1901
1902 static DEBUG_FUNCTION void
1903 debug_seed_ranger (gimple_ranger &ranger)
1904 {
1905 // Recalculate SCEV to make sure the dump lists everything.
1906 if (scev_initialized_p ())
1907 {
1908 scev_finalize ();
1909 scev_initialize ();
1910 }
1911
1912 basic_block bb;
1913 int_range_max r;
1914 gimple_stmt_iterator gsi;
1915 FOR_EACH_BB_FN (bb, cfun)
1916 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1917 {
1918 gimple *stmt = gsi_stmt (gsi);
1919
1920 if (is_gimple_debug (stmt))
1921 continue;
1922
1923 ranger.range_of_stmt (r, stmt);
1924 }
1925 }
1926
1927 // Dump all that ranger knows for the current function.
1928
1929 DEBUG_FUNCTION void
1930 dump_ranger (FILE *out)
1931 {
1932 gimple_ranger ranger;
1933 debug_seed_ranger (ranger);
1934 ranger.dump (out);
1935 }
1936
1937 DEBUG_FUNCTION void
1938 debug_ranger ()
1939 {
1940 dump_ranger (stderr);
1941 }
1942
1943 // Dump all that ranger knows on a path of BBs.
1944 //
1945 // Note that the blocks are in reverse order, thus the exit block is
1946 // path[0].
1947
1948 DEBUG_FUNCTION void
1949 dump_ranger (FILE *dump_file, const vec<basic_block> &path)
1950 {
1951 if (path.length () == 0)
1952 {
1953 fprintf (dump_file, "empty\n");
1954 return;
1955 }
1956
1957 gimple_ranger ranger;
1958 debug_seed_ranger (ranger);
1959
1960 unsigned i = path.length ();
1961 do
1962 {
1963 i--;
1964 ranger.dump_bb (dump_file, path[i]);
1965 }
1966 while (i > 0);
1967 }
1968
1969 DEBUG_FUNCTION void
1970 debug_ranger (const vec<basic_block> &path)
1971 {
1972 dump_ranger (stderr, path);
1973 }
1974
1975 #include "gimple-range-tests.cc"