Daily bump.
[official-gcc.git] / gcc / gimple-range-path.cc
blob15ef72fd4925a464c6c3f177c491711831a16005
1 /* Basic block path solver.
2 Copyright (C) 2021 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "cfganal.h"
28 #include "value-range.h"
29 #include "gimple-range.h"
30 #include "tree-pretty-print.h"
31 #include "gimple-range-path.h"
32 #include "ssa.h"
33 #include "tree-cfg.h"
34 #include "gimple-iterator.h"
36 // Internal construct to help facilitate debugging of solver.
37 #define DEBUG_SOLVER (dump_file && (param_threader_debug == THREADER_DEBUG_ALL))
39 path_range_query::path_range_query (bool resolve, gimple_ranger *ranger)
40 : m_cache (new ssa_global_cache),
41 m_has_cache_entry (BITMAP_ALLOC (NULL)),
42 m_resolve (resolve),
43 m_alloced_ranger (!ranger)
45 if (m_alloced_ranger)
46 m_ranger = new gimple_ranger;
47 else
48 m_ranger = ranger;
50 m_oracle = new path_oracle (m_ranger->oracle ());
53 path_range_query::~path_range_query ()
55 delete m_oracle;
56 if (m_alloced_ranger)
57 delete m_ranger;
58 BITMAP_FREE (m_has_cache_entry);
59 delete m_cache;
62 // Return TRUE if NAME is in the import bitmap.
64 bool
65 path_range_query::import_p (tree name)
67 return (TREE_CODE (name) == SSA_NAME
68 && bitmap_bit_p (m_imports, SSA_NAME_VERSION (name)));
71 // Mark cache entry for NAME as unused.
73 void
74 path_range_query::clear_cache (tree name)
76 unsigned v = SSA_NAME_VERSION (name);
77 bitmap_clear_bit (m_has_cache_entry, v);
80 // If NAME has a cache entry, return it in R, and return TRUE.
82 inline bool
83 path_range_query::get_cache (irange &r, tree name)
85 if (!gimple_range_ssa_p (name))
86 return get_global_range_query ()->range_of_expr (r, name);
88 unsigned v = SSA_NAME_VERSION (name);
89 if (bitmap_bit_p (m_has_cache_entry, v))
90 return m_cache->get_global_range (r, name);
92 return false;
95 // Set the cache entry for NAME to R.
97 void
98 path_range_query::set_cache (const irange &r, tree name)
100 unsigned v = SSA_NAME_VERSION (name);
101 bitmap_set_bit (m_has_cache_entry, v);
102 m_cache->set_global_range (name, r);
105 void
106 path_range_query::dump (FILE *dump_file)
108 push_dump_file save (dump_file, dump_flags & ~TDF_DETAILS);
110 if (m_path.is_empty ())
111 return;
113 unsigned i;
114 bitmap_iterator bi;
116 dump_ranger (dump_file, m_path);
118 fprintf (dump_file, "Imports:\n");
119 EXECUTE_IF_SET_IN_BITMAP (m_imports, 0, i, bi)
121 tree name = ssa_name (i);
122 print_generic_expr (dump_file, name, TDF_SLIM);
123 fprintf (dump_file, "\n");
126 m_cache->dump (dump_file);
129 void
130 path_range_query::debug ()
132 dump (stderr);
135 // Return TRUE if NAME is defined outside the current path.
137 bool
138 path_range_query::defined_outside_path (tree name)
140 gimple *def = SSA_NAME_DEF_STMT (name);
141 basic_block bb = gimple_bb (def);
143 return !bb || !m_path.contains (bb);
146 // Return the range of NAME on entry to the path.
148 void
149 path_range_query::range_on_path_entry (irange &r, tree name)
151 gcc_checking_assert (defined_outside_path (name));
152 basic_block entry = entry_bb ();
154 // Prefer to use range_of_expr if we have a statement to look at,
155 // since it has better caching than range_on_edge.
156 gimple *last = last_stmt (entry);
157 if (last)
159 if (m_ranger->range_of_expr (r, name, last))
160 return;
161 gcc_unreachable ();
164 // If we have no statement, look at all the incoming ranges to the
165 // block. This can happen when we're querying a block with only an
166 // outgoing edge (no statement but the fall through edge), but for
167 // which we can determine a range on entry to the block.
168 int_range_max tmp;
169 bool changed = false;
170 r.set_undefined ();
171 for (unsigned i = 0; i < EDGE_COUNT (entry->preds); ++i)
173 edge e = EDGE_PRED (entry, i);
174 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
175 && m_ranger->range_on_edge (tmp, e, name))
177 r.union_ (tmp);
178 changed = true;
182 // Make sure we don't return UNDEFINED by mistake.
183 if (!changed)
184 r.set_varying (TREE_TYPE (name));
187 // Return the range of NAME at the end of the path being analyzed.
189 bool
190 path_range_query::internal_range_of_expr (irange &r, tree name, gimple *stmt)
192 if (!irange::supports_type_p (TREE_TYPE (name)))
193 return false;
195 if (get_cache (r, name))
196 return true;
198 if (m_resolve && defined_outside_path (name))
200 range_on_path_entry (r, name);
201 set_cache (r, name);
202 return true;
205 if (stmt
206 && range_defined_in_block (r, name, gimple_bb (stmt)))
208 if (TREE_CODE (name) == SSA_NAME)
209 r.intersect (gimple_range_global (name));
211 set_cache (r, name);
212 return true;
215 r = gimple_range_global (name);
216 return true;
219 bool
220 path_range_query::range_of_expr (irange &r, tree name, gimple *stmt)
222 if (internal_range_of_expr (r, name, stmt))
224 if (r.undefined_p ())
225 m_undefined_path = true;
227 return true;
229 return false;
232 bool
233 path_range_query::unreachable_path_p ()
235 return m_undefined_path;
238 // Initialize the current path to PATH. The current block is set to
239 // the entry block to the path.
241 // Note that the blocks are in reverse order, so the exit block is
242 // path[0].
244 void
245 path_range_query::set_path (const vec<basic_block> &path)
247 gcc_checking_assert (path.length () > 1);
248 m_path = path.copy ();
249 m_pos = m_path.length () - 1;
250 bitmap_clear (m_has_cache_entry);
253 bool
254 path_range_query::ssa_defined_in_bb (tree name, basic_block bb)
256 return (TREE_CODE (name) == SSA_NAME
257 && SSA_NAME_DEF_STMT (name)
258 && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb);
261 // Return the range of the result of PHI in R.
263 // Since PHIs are calculated in parallel at the beginning of the
264 // block, we must be careful to never save anything to the cache here.
265 // It is the caller's responsibility to adjust the cache. Also,
266 // calculating the PHI's range must not trigger additional lookups.
268 void
269 path_range_query::ssa_range_in_phi (irange &r, gphi *phi)
271 tree name = gimple_phi_result (phi);
272 basic_block bb = gimple_bb (phi);
273 unsigned nargs = gimple_phi_num_args (phi);
275 if (at_entry ())
277 if (m_resolve && m_ranger->range_of_expr (r, name, phi))
278 return;
280 // Try to fold the phi exclusively with global or cached values.
281 // This will get things like PHI <5(99), 6(88)>. We do this by
282 // calling range_of_expr with no context.
283 int_range_max arg_range;
284 r.set_undefined ();
285 for (size_t i = 0; i < nargs; ++i)
287 tree arg = gimple_phi_arg_def (phi, i);
288 if (range_of_expr (arg_range, arg, /*stmt=*/NULL))
289 r.union_ (arg_range);
290 else
292 r.set_varying (TREE_TYPE (name));
293 return;
296 return;
299 basic_block prev = prev_bb ();
300 edge e_in = find_edge (prev, bb);
302 for (size_t i = 0; i < nargs; ++i)
303 if (e_in == gimple_phi_arg_edge (phi, i))
305 tree arg = gimple_phi_arg_def (phi, i);
306 // Avoid using the cache for ARGs defined in this block, as
307 // that could create an ordering problem.
308 if (ssa_defined_in_bb (arg, bb) || !get_cache (r, arg))
310 if (m_resolve)
312 int_range_max tmp;
313 // Using both the range on entry to the path, and the
314 // range on this edge yields significantly better
315 // results.
316 if (defined_outside_path (arg))
317 range_on_path_entry (r, arg);
318 else
319 r.set_varying (TREE_TYPE (name));
320 m_ranger->range_on_edge (tmp, e_in, arg);
321 r.intersect (tmp);
322 return;
324 r.set_varying (TREE_TYPE (name));
326 return;
328 gcc_unreachable ();
331 // If NAME is defined in BB, set R to the range of NAME, and return
332 // TRUE. Otherwise, return FALSE.
334 bool
335 path_range_query::range_defined_in_block (irange &r, tree name, basic_block bb)
337 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
338 basic_block def_bb = gimple_bb (def_stmt);
340 if (def_bb != bb)
341 return false;
343 if (get_cache (r, name))
344 return true;
346 if (gimple_code (def_stmt) == GIMPLE_PHI)
347 ssa_range_in_phi (r, as_a<gphi *> (def_stmt));
348 else
350 if (name)
351 get_path_oracle ()->killing_def (name);
353 if (!range_of_stmt (r, def_stmt, name))
354 r.set_varying (TREE_TYPE (name));
357 if (bb)
358 m_non_null.adjust_range (r, name, bb);
360 if (DEBUG_SOLVER && (bb || !r.varying_p ()))
362 fprintf (dump_file, "range_defined_in_block (BB%d) for ", bb ? bb->index : -1);
363 print_generic_expr (dump_file, name, TDF_SLIM);
364 fprintf (dump_file, " is ");
365 r.dump (dump_file);
366 fprintf (dump_file, "\n");
369 return true;
372 // Compute ranges defined in the PHIs in this block.
374 void
375 path_range_query::compute_ranges_in_phis (basic_block bb)
377 int_range_max r;
378 auto_bitmap phi_set;
380 // PHIs must be resolved simultaneously on entry to the block
381 // because any dependencies must be satistifed with values on entry.
382 // Thus, we calculate all PHIs first, and then update the cache at
383 // the end.
385 for (auto iter = gsi_start_phis (bb); !gsi_end_p (iter); gsi_next (&iter))
387 gphi *phi = iter.phi ();
388 tree name = gimple_phi_result (phi);
390 if (import_p (name) && range_defined_in_block (r, name, bb))
392 unsigned v = SSA_NAME_VERSION (name);
393 set_cache (r, name);
394 bitmap_set_bit (phi_set, v);
395 // Pretend we don't have a cache entry for this name until
396 // we're done with all PHIs.
397 bitmap_clear_bit (m_has_cache_entry, v);
400 bitmap_ior_into (m_has_cache_entry, phi_set);
403 // Compute ranges defined in the current block, or exported to the
404 // next block.
406 void
407 path_range_query::compute_ranges_in_block (basic_block bb)
409 bitmap_iterator bi;
410 int_range_max r, cached_range;
411 unsigned i;
413 if (m_resolve && !at_entry ())
414 compute_phi_relations (bb, prev_bb ());
416 // Force recalculation of any names in the cache that are defined in
417 // this block. This can happen on interdependent SSA/phis in loops.
418 EXECUTE_IF_SET_IN_BITMAP (m_imports, 0, i, bi)
420 tree name = ssa_name (i);
421 if (ssa_defined_in_bb (name, bb))
422 clear_cache (name);
425 // Solve imports defined in this block, starting with the PHIs...
426 compute_ranges_in_phis (bb);
427 // ...and then the rest of the imports.
428 EXECUTE_IF_SET_IN_BITMAP (m_imports, 0, i, bi)
430 tree name = ssa_name (i);
432 if (gimple_code (SSA_NAME_DEF_STMT (name)) != GIMPLE_PHI
433 && range_defined_in_block (r, name, bb))
434 set_cache (r, name);
437 if (at_exit ())
438 return;
440 // Solve imports that are exported to the next block.
441 basic_block next = next_bb ();
442 edge e = find_edge (bb, next);
443 EXECUTE_IF_SET_IN_BITMAP (m_imports, 0, i, bi)
445 tree name = ssa_name (i);
446 gori_compute &g = m_ranger->gori ();
447 bitmap exports = g.exports (bb);
449 if (bitmap_bit_p (exports, i))
451 if (g.outgoing_edge_range_p (r, e, name, *this))
453 if (get_cache (cached_range, name))
454 r.intersect (cached_range);
456 set_cache (r, name);
457 if (DEBUG_SOLVER)
459 fprintf (dump_file, "outgoing_edge_range_p for ");
460 print_generic_expr (dump_file, name, TDF_SLIM);
461 fprintf (dump_file, " on edge %d->%d ",
462 e->src->index, e->dest->index);
463 fprintf (dump_file, "is ");
464 r.dump (dump_file);
465 fprintf (dump_file, "\n");
471 if (m_resolve)
472 compute_outgoing_relations (bb, next);
475 // Adjust all pointer imports in BB with non-null information.
477 void
478 path_range_query::adjust_for_non_null_uses (basic_block bb)
480 int_range_max r;
481 bitmap_iterator bi;
482 unsigned i;
484 EXECUTE_IF_SET_IN_BITMAP (m_imports, 0, i, bi)
486 tree name = ssa_name (i);
488 if (!POINTER_TYPE_P (TREE_TYPE (name)))
489 continue;
491 if (get_cache (r, name))
493 if (r.nonzero_p ())
494 continue;
496 else
497 r.set_varying (TREE_TYPE (name));
499 if (m_non_null.adjust_range (r, name, bb))
500 set_cache (r, name);
504 // If NAME is a supported SSA_NAME, add it the bitmap in IMPORTS.
506 bool
507 path_range_query::add_to_imports (tree name, bitmap imports)
509 if (TREE_CODE (name) == SSA_NAME
510 && irange::supports_type_p (TREE_TYPE (name)))
511 return bitmap_set_bit (imports, SSA_NAME_VERSION (name));
512 return false;
515 // Compute the imports to the path ending in EXIT. These are
516 // essentially the SSA names used to calculate the final conditional
517 // along the path.
519 // They are hints for the solver. Adding more elements doesn't slow
520 // us down, because we don't solve anything that doesn't appear in the
521 // path. On the other hand, not having enough imports will limit what
522 // we can solve.
524 void
525 path_range_query::compute_imports (bitmap imports, basic_block exit)
527 // Start with the imports from the exit block...
528 gori_compute &gori = m_ranger->gori ();
529 bitmap r_imports = gori.imports (exit);
530 bitmap_copy (imports, r_imports);
532 auto_vec<tree> worklist (bitmap_count_bits (imports));
533 bitmap_iterator bi;
534 unsigned i;
535 EXECUTE_IF_SET_IN_BITMAP (imports, 0, i, bi)
537 tree name = ssa_name (i);
538 worklist.quick_push (name);
541 // ...and add any operands used to define these imports.
542 while (!worklist.is_empty ())
544 tree name = worklist.pop ();
545 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
547 if (is_gimple_assign (def_stmt))
549 add_to_imports (gimple_assign_rhs1 (def_stmt), imports);
550 tree rhs = gimple_assign_rhs2 (def_stmt);
551 if (rhs && add_to_imports (rhs, imports))
552 worklist.safe_push (rhs);
553 rhs = gimple_assign_rhs3 (def_stmt);
554 if (rhs && add_to_imports (rhs, imports))
555 worklist.safe_push (rhs);
557 else if (gphi *phi = dyn_cast <gphi *> (def_stmt))
559 for (size_t i = 0; i < gimple_phi_num_args (phi); ++i)
561 edge e = gimple_phi_arg_edge (phi, i);
562 tree arg = gimple_phi_arg (phi, i)->def;
564 if (TREE_CODE (arg) == SSA_NAME
565 && m_path.contains (e->src)
566 && bitmap_set_bit (imports, SSA_NAME_VERSION (arg)))
567 worklist.safe_push (arg);
571 // Exported booleans along the path, may help conditionals.
572 if (m_resolve)
573 for (i = 0; i < m_path.length (); ++i)
575 basic_block bb = m_path[i];
576 tree name;
577 FOR_EACH_GORI_EXPORT_NAME (gori, bb, name)
578 if (TREE_CODE (TREE_TYPE (name)) == BOOLEAN_TYPE)
579 bitmap_set_bit (imports, SSA_NAME_VERSION (name));
583 // Compute the ranges for IMPORTS along PATH.
585 // IMPORTS are the set of SSA names, any of which could potentially
586 // change the value of the final conditional in PATH. Default to the
587 // imports of the last block in the path if none is given.
589 void
590 path_range_query::compute_ranges (const vec<basic_block> &path,
591 const bitmap_head *imports)
593 if (DEBUG_SOLVER)
594 fprintf (dump_file, "\n==============================================\n");
596 set_path (path);
597 m_undefined_path = false;
599 if (imports)
600 bitmap_copy (m_imports, imports);
601 else
602 compute_imports (m_imports, exit_bb ());
604 if (m_resolve)
605 get_path_oracle ()->reset_path ();
607 if (DEBUG_SOLVER)
609 fprintf (dump_file, "path_range_query: compute_ranges for path: ");
610 for (unsigned i = path.length (); i > 0; --i)
612 basic_block bb = path[i - 1];
613 fprintf (dump_file, "%d", bb->index);
614 if (i > 1)
615 fprintf (dump_file, "->");
617 fprintf (dump_file, "\n");
620 while (1)
622 basic_block bb = curr_bb ();
624 compute_ranges_in_block (bb);
625 adjust_for_non_null_uses (bb);
627 if (at_exit ())
628 break;
630 move_next ();
633 if (DEBUG_SOLVER)
635 get_path_oracle ()->dump (dump_file);
636 dump (dump_file);
640 // Convenience function to compute ranges along a path consisting of
641 // E->SRC and E->DEST.
643 void
644 path_range_query::compute_ranges (edge e)
646 auto_vec<basic_block> bbs (2);
647 bbs.quick_push (e->dest);
648 bbs.quick_push (e->src);
649 compute_ranges (bbs);
652 // A folding aid used to register and query relations along a path.
653 // When queried, it returns relations as they would appear on exit to
654 // the path.
656 // Relations are registered on entry so the path_oracle knows which
657 // block to query the root oracle at when a relation lies outside the
658 // path. However, when queried we return the relation on exit to the
659 // path, since the root_oracle ignores the registered.
661 class jt_fur_source : public fur_depend
663 public:
664 jt_fur_source (gimple *s, path_range_query *, gori_compute *,
665 const vec<basic_block> &);
666 relation_kind query_relation (tree op1, tree op2) override;
667 void register_relation (gimple *, relation_kind, tree op1, tree op2) override;
668 void register_relation (edge, relation_kind, tree op1, tree op2) override;
669 private:
670 basic_block m_entry;
673 jt_fur_source::jt_fur_source (gimple *s,
674 path_range_query *query,
675 gori_compute *gori,
676 const vec<basic_block> &path)
677 : fur_depend (s, gori, query)
679 gcc_checking_assert (!path.is_empty ());
681 m_entry = path[path.length () - 1];
683 if (dom_info_available_p (CDI_DOMINATORS))
684 m_oracle = query->oracle ();
685 else
686 m_oracle = NULL;
689 // Ignore statement and register relation on entry to path.
691 void
692 jt_fur_source::register_relation (gimple *, relation_kind k, tree op1, tree op2)
694 if (m_oracle)
695 m_oracle->register_relation (m_entry, k, op1, op2);
698 // Ignore edge and register relation on entry to path.
700 void
701 jt_fur_source::register_relation (edge, relation_kind k, tree op1, tree op2)
703 if (m_oracle)
704 m_oracle->register_relation (m_entry, k, op1, op2);
707 relation_kind
708 jt_fur_source::query_relation (tree op1, tree op2)
710 if (!m_oracle)
711 return VREL_NONE;
713 if (TREE_CODE (op1) != SSA_NAME || TREE_CODE (op2) != SSA_NAME)
714 return VREL_NONE;
716 return m_oracle->query_relation (m_entry, op1, op2);
719 // Return the range of STMT at the end of the path being analyzed.
721 bool
722 path_range_query::range_of_stmt (irange &r, gimple *stmt, tree)
724 tree type = gimple_range_type (stmt);
726 if (!irange::supports_type_p (type))
727 return false;
729 // If resolving unknowns, fold the statement making use of any
730 // relations along the path.
731 if (m_resolve)
733 fold_using_range f;
734 jt_fur_source src (stmt, this, &m_ranger->gori (), m_path);
735 if (!f.fold_stmt (r, stmt, src))
736 r.set_varying (type);
738 // Otherwise, fold without relations.
739 else if (!fold_range (r, stmt, this))
740 r.set_varying (type);
742 return true;
745 void
746 path_range_query::maybe_register_phi_relation (gphi *phi, tree arg)
748 basic_block bb = gimple_bb (phi);
749 tree result = gimple_phi_result (phi);
751 // Avoid recording the equivalence if the arg is defined in this
752 // block, as that could create an ordering problem.
753 if (ssa_defined_in_bb (arg, bb))
754 return;
756 if (dump_file && (dump_flags & TDF_DETAILS))
757 fprintf (dump_file, " from bb%d:", bb->index);
759 get_path_oracle ()->killing_def (result);
760 m_oracle->register_relation (entry_bb (), EQ_EXPR, arg, result);
763 // Compute relations for each PHI in BB. For example:
765 // x_5 = PHI<y_9(5),...>
767 // If the path flows through BB5, we can register that x_5 == y_9.
769 void
770 path_range_query::compute_phi_relations (basic_block bb, basic_block prev)
772 if (prev == NULL)
773 return;
775 edge e_in = find_edge (prev, bb);
777 for (gphi_iterator iter = gsi_start_phis (bb); !gsi_end_p (iter);
778 gsi_next (&iter))
780 gphi *phi = iter.phi ();
781 tree result = gimple_phi_result (phi);
782 unsigned nargs = gimple_phi_num_args (phi);
784 if (!import_p (result))
785 continue;
787 for (size_t i = 0; i < nargs; ++i)
788 if (e_in == gimple_phi_arg_edge (phi, i))
790 tree arg = gimple_phi_arg_def (phi, i);
792 if (gimple_range_ssa_p (arg))
793 maybe_register_phi_relation (phi, arg);
794 break;
799 // Compute outgoing relations from BB to NEXT.
801 void
802 path_range_query::compute_outgoing_relations (basic_block bb, basic_block next)
804 gimple *stmt = last_stmt (bb);
806 if (stmt
807 && gimple_code (stmt) == GIMPLE_COND
808 && (import_p (gimple_cond_lhs (stmt))
809 || import_p (gimple_cond_rhs (stmt))))
811 int_range<2> r;
812 gcond *cond = as_a<gcond *> (stmt);
813 edge e0 = EDGE_SUCC (bb, 0);
814 edge e1 = EDGE_SUCC (bb, 1);
816 if (e0->dest == next)
817 gcond_edge_range (r, e0);
818 else if (e1->dest == next)
819 gcond_edge_range (r, e1);
820 else
821 gcc_unreachable ();
823 jt_fur_source src (NULL, this, &m_ranger->gori (), m_path);
824 src.register_outgoing_edges (cond, r, e0, e1);