Require bitint575 for pr116003.c
[official-gcc.git] / gcc / rtl-ssa / accesses.cc
blob5cc05cb4be7f02f3294af002ddeae2caf7ca750f
1 // Implementation of access-related functions for RTL SSA -*- C++ -*-
2 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation; either version 3, or (at your option) any later
9 // version.
11 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 // for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #define INCLUDE_ALGORITHM
21 #define INCLUDE_FUNCTIONAL
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "df.h"
28 #include "rtl-ssa.h"
29 #include "rtl-ssa/internals.h"
30 #include "rtl-ssa/internals.inl"
32 using namespace rtl_ssa;
34 // This clobber belongs to a clobber_group but m_group appears to be
35 // out of date. Update it and return the new (correct) value.
36 clobber_group *
37 clobber_info::recompute_group ()
39 using splay_tree = clobber_info::splay_tree;
41 // Splay this clobber to the root of the tree while searching for a node
42 // that has the correct group. The root always has the correct group,
43 // so the search always breaks early and does not install this clobber
44 // as the root.
45 clobber_info *cursor = m_parent;
46 auto find_group = [](clobber_info *node, unsigned int)
48 return node->m_group->has_been_superceded () ? nullptr : node->m_group;
50 clobber_group *group = splay_tree::splay_and_search (this, nullptr,
51 find_group);
52 gcc_checking_assert (m_parent);
54 // If the previous splay operation did anything, this clobber is now an
55 // ancestor of CURSOR, and all the nodes inbetween have a stale group.
56 // Since we have visited the nodes, we might as well update them too.
58 // If the previous splay operation did nothing, start the update from
59 // this clobber instead. In that case we change at most two clobbers:
60 // this clobber and possibly its parent.
61 if (cursor == m_parent)
62 cursor = this;
64 // Walk up the tree from CURSOR updating clobbers that need it.
65 // This walk always includes this clobber.
66 while (cursor->m_group != group)
68 cursor->m_group = group;
69 cursor = cursor->m_parent;
72 gcc_checking_assert (m_group == group);
73 return group;
76 // See the comment above the declaration.
77 void
78 resource_info::print_identifier (pretty_printer *pp) const
80 if (is_mem ())
81 pp_string (pp, "mem");
82 else
84 char tmp[3 * sizeof (regno) + 2];
85 snprintf (tmp, sizeof (tmp), "r%d", regno);
86 pp_string (pp, tmp);
90 // See the comment above the declaration.
91 void
92 resource_info::print_context (pretty_printer *pp) const
94 if (HARD_REGISTER_NUM_P (regno))
96 if (const char *name = reg_names[regno])
98 pp_space (pp);
99 pp_left_paren (pp);
100 pp_string (pp, name);
101 if (mode != E_BLKmode)
103 pp_colon (pp);
104 pp_string (pp, GET_MODE_NAME (mode));
106 pp_right_paren (pp);
109 else if (is_reg ())
111 pp_space (pp);
112 pp_left_paren (pp);
113 if (mode != E_BLKmode)
115 pp_string (pp, GET_MODE_NAME (mode));
116 pp_space (pp);
118 pp_string (pp, "pseudo");
119 pp_right_paren (pp);
123 // See the comment above the declaration.
124 void
125 resource_info::print (pretty_printer *pp) const
127 print_identifier (pp);
128 print_context (pp);
131 // Some properties can naturally be described using adjectives that attach
132 // to nouns like "use" or "definition". Print such adjectives to PP.
133 void
134 access_info::print_prefix_flags (pretty_printer *pp) const
136 if (m_is_temp)
137 pp_string (pp, "temporary ");
138 if (m_has_been_superceded)
139 pp_string (pp, "superceded ");
142 // Print properties not handled by print_prefix_flags to PP, putting
143 // each property on a new line indented by two extra spaces.
144 void
145 access_info::print_properties_on_new_lines (pretty_printer *pp) const
147 if (m_is_pre_post_modify)
149 pp_newline_and_indent (pp, 2);
150 pp_string (pp, "set by a pre/post-modify");
151 pp_indentation (pp) -= 2;
153 if (m_includes_address_uses)
155 pp_newline_and_indent (pp, 2);
156 pp_string (pp, "appears inside an address");
157 pp_indentation (pp) -= 2;
159 if (m_includes_read_writes)
161 pp_newline_and_indent (pp, 2);
162 pp_string (pp, "appears in a read/write context");
163 pp_indentation (pp) -= 2;
165 if (m_includes_subregs)
167 pp_newline_and_indent (pp, 2);
168 pp_string (pp, "appears inside a subreg");
169 pp_indentation (pp) -= 2;
173 // Return true if there are no known issues with the integrity of the
174 // link information.
175 inline bool
176 use_info::check_integrity ()
178 auto subsequence_id = [](use_info *use)
180 if (use->is_in_nondebug_insn ())
181 return 1;
182 if (use->is_in_debug_insn ())
183 return 2;
184 return 3;
187 use_info *prev = prev_use ();
188 use_info *next = next_use ();
190 if (prev && subsequence_id (prev) > subsequence_id (this))
191 return false;
192 if (next && subsequence_id (next) < subsequence_id (this))
193 return false;
194 if (m_is_last_nondebug_insn_use != calculate_is_last_nondebug_insn_use ())
195 return false;
197 if (!prev && last_use ()->next_use ())
198 return false;
199 if (!next)
200 if (use_info *use = last_nondebug_insn_use ())
201 if (!use->m_is_last_nondebug_insn_use)
202 return false;
204 return true;
207 // See the comment above the declaration.
208 void
209 use_info::print_location (pretty_printer *pp) const
211 if (is_in_phi ())
212 pp_access (pp, phi (), PP_ACCESS_INCLUDE_LOCATION);
213 else
214 insn ()->print_identifier_and_location (pp);
217 // See the comment above the declaration.
218 void
219 use_info::print_def (pretty_printer *pp) const
221 if (const set_info *set = def ())
222 pp_access (pp, set, 0);
223 else
225 pp_string (pp, "undefined ");
226 resource ().print (pp);
230 // See the comment above the declaration.
231 void
232 use_info::print (pretty_printer *pp, unsigned int flags) const
234 print_prefix_flags (pp);
236 const set_info *set = def ();
237 if (set && set->mode () != mode ())
239 pp_string (pp, GET_MODE_NAME (mode ()));
240 pp_space (pp);
243 pp_string (pp, "use of ");
244 print_def (pp);
245 if (flags & PP_ACCESS_INCLUDE_LOCATION)
247 pp_string (pp, " by ");
248 print_location (pp);
250 if (set && (flags & PP_ACCESS_INCLUDE_LINKS))
252 pp_newline_and_indent (pp, 2);
253 pp_string (pp, "defined in ");
254 set->insn ()->print_location (pp);
255 pp_indentation (pp) -= 2;
257 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
258 print_properties_on_new_lines (pp);
261 // See the comment above the declaration.
262 void
263 def_info::print_identifier (pretty_printer *pp) const
265 resource ().print_identifier (pp);
266 pp_colon (pp);
267 insn ()->print_identifier (pp);
268 resource ().print_context (pp);
271 // See the comment above the declaration.
272 void
273 def_info::print_location (pretty_printer *pp) const
275 insn ()->print_identifier_and_location (pp);
278 // See the comment above the declaration.
279 void
280 clobber_info::print (pretty_printer *pp, unsigned int flags) const
282 print_prefix_flags (pp);
283 if (is_call_clobber ())
284 pp_string (pp, "call ");
285 pp_string (pp, "clobber ");
286 print_identifier (pp);
287 if (flags & PP_ACCESS_INCLUDE_LOCATION)
289 pp_string (pp, " in ");
290 insn ()->print_location (pp);
292 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
293 print_properties_on_new_lines (pp);
296 // See the comment above the declaration.
297 void
298 set_info::print_uses_on_new_lines (pretty_printer *pp) const
300 for (const use_info *use : all_uses ())
302 pp_newline_and_indent (pp, 2);
303 if (use->is_live_out_use ())
305 pp_string (pp, "live out from ");
306 use->insn ()->print_location (pp);
308 else
310 pp_string (pp, "used by ");
311 use->print_location (pp);
313 pp_indentation (pp) -= 2;
315 if (m_use_tree)
317 pp_newline_and_indent (pp, 2);
318 pp_string (pp, "splay tree:");
319 pp_newline_and_indent (pp, 2);
320 auto print_use = [](pretty_printer *pp,
321 splay_tree_node<use_info *> *node)
323 pp_string (pp, "use by ");
324 node->value ()->print_location (pp);
326 m_use_tree.print (pp, m_use_tree.root (), print_use);
327 pp_indentation (pp) -= 4;
331 // See the comment above the declaration.
332 void
333 set_info::print (pretty_printer *pp, unsigned int flags) const
335 print_prefix_flags (pp);
336 pp_string (pp, "set ");
337 print_identifier (pp);
338 if (flags & PP_ACCESS_INCLUDE_LOCATION)
340 pp_string (pp, " in ");
341 insn ()->print_location (pp);
343 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
344 print_properties_on_new_lines (pp);
345 if (flags & PP_ACCESS_INCLUDE_LINKS)
346 print_uses_on_new_lines (pp);
349 // See the comment above the declaration.
350 void
351 phi_info::print (pretty_printer *pp, unsigned int flags) const
353 print_prefix_flags (pp);
354 pp_string (pp, "phi node ");
355 print_identifier (pp);
356 if (flags & PP_ACCESS_INCLUDE_LOCATION)
358 pp_string (pp, " in ");
359 insn ()->print_location (pp);
362 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
363 print_properties_on_new_lines (pp);
365 if (flags & PP_ACCESS_INCLUDE_LINKS)
367 basic_block cfg_bb = bb ()->cfg_bb ();
368 pp_newline_and_indent (pp, 2);
369 pp_string (pp, "inputs:");
370 unsigned int i = 0;
371 for (const use_info *input : inputs ())
373 basic_block pred_cfg_bb = EDGE_PRED (cfg_bb, i)->src;
374 pp_newline_and_indent (pp, 2);
375 pp_string (pp, "bb");
376 pp_decimal_int (pp, pred_cfg_bb->index);
377 pp_colon (pp);
378 pp_space (pp);
379 input->print_def (pp);
380 pp_indentation (pp) -= 2;
381 i += 1;
383 pp_indentation (pp) -= 2;
385 print_uses_on_new_lines (pp);
389 // See the comment above the declaration.
390 void
391 set_node::print (pretty_printer *pp) const
393 pp_access (pp, first_def ());
396 // See the comment above the declaration.
397 clobber_info *
398 clobber_group::prev_clobber (insn_info *insn) const
400 auto &tree = const_cast<clobber_tree &> (m_clobber_tree);
401 int comparison = lookup_clobber (tree, insn);
402 if (comparison <= 0)
403 return dyn_cast<clobber_info *> (tree.root ()->prev_def ());
404 return tree.root ();
407 // See the comment above the declaration.
408 clobber_info *
409 clobber_group::next_clobber (insn_info *insn) const
411 auto &tree = const_cast<clobber_tree &> (m_clobber_tree);
412 int comparison = lookup_clobber (tree, insn);
413 if (comparison >= 0)
414 return dyn_cast<clobber_info *> (tree.root ()->next_def ());
415 return tree.root ();
418 // See the comment above the declaration.
419 void
420 clobber_group::print (pretty_printer *pp) const
422 auto print_clobber = [](pretty_printer *pp, const def_info *clobber)
424 pp_access (pp, clobber);
426 pp_string (pp, "grouped clobber");
427 for (const def_info *clobber : clobbers ())
429 pp_newline_and_indent (pp, 2);
430 print_clobber (pp, clobber);
431 pp_indentation (pp) -= 2;
433 pp_newline_and_indent (pp, 2);
434 pp_string (pp, "splay tree");
435 pp_newline_and_indent (pp, 2);
436 m_clobber_tree.print (pp, print_clobber);
437 pp_indentation (pp) -= 4;
440 // See the comment above the declaration.
441 def_info *
442 def_lookup::prev_def (insn_info *insn) const
444 if (mux && comparison == 0)
445 if (auto *node = mux.dyn_cast<def_node *> ())
446 if (auto *group = dyn_cast<clobber_group *> (node))
447 if (clobber_info *clobber = group->prev_clobber (insn))
448 return clobber;
450 return last_def_of_prev_group ();
453 // See the comment above the declaration.
454 def_info *
455 def_lookup::next_def (insn_info *insn) const
457 if (mux && comparison == 0)
458 if (auto *node = mux.dyn_cast<def_node *> ())
459 if (auto *group = dyn_cast<clobber_group *> (node))
460 if (clobber_info *clobber = group->next_clobber (insn))
461 return clobber;
463 return first_def_of_next_group ();
466 // Return a clobber_group for CLOBBER, creating one if CLOBBER doesn't
467 // already belong to a group.
468 clobber_group *
469 function_info::need_clobber_group (clobber_info *clobber)
471 if (clobber->is_in_group ())
472 return clobber->group ();
473 return allocate<clobber_group> (clobber);
476 // Return a def_node for inserting DEF into the associated resource's
477 // splay tree. Use a clobber_group if DEF is a clobber and a set_node
478 // otherwise.
479 def_node *
480 function_info::need_def_node (def_info *def)
482 if (auto *clobber = dyn_cast<clobber_info *> (def))
483 return need_clobber_group (clobber);
484 return allocate<set_node> (as_a<set_info *> (def));
487 // LAST is the last thing to define LAST->resource (), and is where any
488 // splay tree root for LAST->resource () is stored. Require such a splay tree
489 // to exist, creating a new one if necessary. Return the root of the tree.
491 // The caller must call LAST->set_splay_root after it has finished with
492 // the splay tree.
493 def_splay_tree
494 function_info::need_def_splay_tree (def_info *last)
496 if (def_node *root = last->splay_root ())
497 return root;
499 // Use a left-spine rooted at the last node.
500 def_node *root = need_def_node (last);
501 def_node *parent = root;
502 while (def_info *prev = first_def (parent)->prev_def ())
504 def_node *node = need_def_node (prev);
505 def_splay_tree::insert_child (parent, 0, node);
506 parent = node;
508 return root;
511 // Search TREE for either:
513 // - a set_info at INSN or
514 // - a clobber_group whose range includes INSN
516 // If such a node exists, install it as the root of TREE and return 0.
517 // Otherwise arbitrarily choose between:
519 // (1) Installing the closest preceding node as the root and returning 1.
520 // (2) Installing the closest following node as the root and returning -1.
522 // Note that this routine should not be used to check whether INSN
523 // itself defines a resource; that can be checked more cheaply using
524 // find_access_index.
526 rtl_ssa::lookup_def (def_splay_tree &tree, insn_info *insn)
528 auto go_left = [&](def_node *node)
530 return *insn < *first_def (node)->insn ();
532 auto go_right = [&](def_node *node)
534 return *insn > *last_def (node)->insn ();
536 return tree.lookup (go_left, go_right);
539 // Search TREE for a clobber in INSN. If such a clobber exists, install
540 // it as the root of TREE and return 0. Otherwise arbitrarily choose between:
542 // (1) Installing the closest preceding clobber as the root and returning 1.
543 // (2) Installing the closest following clobber as the root and returning -1.
545 rtl_ssa::lookup_clobber (clobber_tree &tree, insn_info *insn)
547 auto compare = [&](clobber_info *clobber)
549 return insn->compare_with (clobber->insn ());
551 return tree.lookup (compare);
554 // Search for a definition of RESOURCE at INSN and return the result of
555 // the search as a def_lookup. See the comment above the class for more
556 // details.
557 def_lookup
558 function_info::find_def (resource_info resource, insn_info *insn)
560 def_info *first = m_defs[resource.regno + 1];
561 if (!first)
562 // There are no nodes. The comparison result is pretty meaningless
563 // in this case.
564 return { nullptr, -1 };
566 // See whether the first node matches.
567 auto first_result = clobber_group_or_single_def (first);
568 if (*insn <= *last_def (first_result)->insn ())
570 int comparison = (*insn >= *first->insn () ? 0 : -1);
571 return { first_result, comparison };
574 // See whether the last node matches.
575 def_info *last = first->last_def ();
576 auto last_result = clobber_group_or_single_def (last);
577 if (*insn >= *first_def (last_result)->insn ())
579 int comparison = (*insn <= *last->insn () ? 0 : 1);
580 return { last_result, comparison };
583 // Resort to using a splay tree to search for the result.
584 def_splay_tree tree = need_def_splay_tree (last);
585 int comparison = lookup_def (tree, insn);
586 last->set_splay_root (tree.root ());
587 return { tree.root (), comparison };
590 // Add DEF to the function's list of definitions of DEF->resource (),
591 // inserting DEF immediately before BEFORE. DEF is not currently in the list.
592 void
593 function_info::insert_def_before (def_info *def, def_info *before)
595 gcc_checking_assert (!def->has_def_links ()
596 && *before->insn () > *def->insn ());
598 def->copy_prev_from (before);
599 if (def_info *prev = def->prev_def ())
601 gcc_checking_assert (*prev->insn () < *def->insn ());
602 prev->set_next_def (def);
604 else
605 m_defs[def->regno () + 1] = def;
607 def->set_next_def (before);
608 before->set_prev_def (def);
611 // Add DEF to the function's list of definitions of DEF->resource (),
612 // inserting DEF immediately after AFTER. DEF is not currently in the list.
613 void
614 function_info::insert_def_after (def_info *def, def_info *after)
616 gcc_checking_assert (!def->has_def_links ()
617 && *after->insn () < *def->insn ());
619 def->copy_next_from (after);
620 if (def_info *next = def->next_def ())
622 gcc_checking_assert (*next->insn () > *def->insn ());
623 next->set_prev_def (def);
625 else
626 m_defs[def->regno () + 1]->set_last_def (def);
628 def->set_prev_def (after);
629 after->set_next_def (def);
632 // Remove DEF from the function's list of definitions of DEF->resource ().
633 void
634 function_info::remove_def_from_list (def_info *def)
636 def_info *prev = def->prev_def ();
637 def_info *next = def->next_def ();
639 if (next)
640 next->copy_prev_from (def);
641 else
642 m_defs[def->regno () + 1]->set_last_def (prev);
644 if (prev)
645 prev->copy_next_from (def);
646 else
647 m_defs[def->regno () + 1] = next;
649 def->clear_def_links ();
652 // Add CLOBBER to GROUP and insert it into the function's list of
653 // accesses to CLOBBER->resource (). CLOBBER is not currently part
654 // of an active group and is not currently in the list.
655 void
656 function_info::add_clobber (clobber_info *clobber, clobber_group *group)
658 // Search for either the previous or next clobber in the group.
659 // The result is less than zero if CLOBBER should come before NEIGHBOR
660 // or greater than zero if CLOBBER should come after NEIGHBOR.
661 int comparison = lookup_clobber (group->m_clobber_tree, clobber->insn ());
662 gcc_checking_assert (comparison != 0);
663 clobber_info *neighbor = group->m_clobber_tree.root ();
665 // Since HEIGHBOR is now the root of the splay tree, its group needs
666 // to be up-to-date.
667 neighbor->update_group (group);
669 // If CLOBBER comes before NEIGHBOR, insert CLOBBER to NEIGHBOR's left,
670 // otherwise insert CLOBBER to NEIGHBOR's right.
671 clobber_info::splay_tree::insert_child (neighbor, comparison > 0, clobber);
672 clobber->set_group (group);
674 // Insert the clobber into the function-wide list and update the
675 // bounds of the group.
676 if (comparison > 0)
678 insert_def_after (clobber, neighbor);
679 if (neighbor == group->last_clobber ())
680 group->set_last_clobber (clobber);
682 else
684 insert_def_before (clobber, neighbor);
685 if (neighbor == group->first_clobber ())
686 group->set_first_clobber (clobber);
690 // Remove CLOBBER from GROUP, given that GROUP contains other clobbers too.
691 // Also remove CLOBBER from the function's list of accesses to
692 // CLOBBER->resource ().
693 void
694 function_info::remove_clobber (clobber_info *clobber, clobber_group *group)
696 if (clobber == group->first_clobber ())
698 auto *new_first = as_a<clobber_info *> (clobber->next_def ());
699 group->set_first_clobber (new_first);
700 new_first->update_group (group);
702 else if (clobber == group->last_clobber ())
704 auto *new_last = as_a<clobber_info *> (clobber->prev_def ());
705 group->set_last_clobber (new_last);
706 new_last->update_group (group);
709 clobber_info *replacement = clobber_info::splay_tree::remove_node (clobber);
710 if (clobber == group->m_clobber_tree.root ())
712 group->m_clobber_tree = replacement;
713 replacement->update_group (group);
715 clobber->set_group (nullptr);
717 remove_def_from_list (clobber);
720 // Add CLOBBER immediately before the first clobber in GROUP, given that
721 // CLOBBER is not currently part of any group.
722 void
723 function_info::prepend_clobber_to_group (clobber_info *clobber,
724 clobber_group *group)
726 clobber_info *next = group->first_clobber ();
727 clobber_info::splay_tree::insert_child (next, 0, clobber);
728 group->set_first_clobber (clobber);
729 clobber->set_group (group);
732 // Add CLOBBER immediately after the last clobber in GROUP, given that
733 // CLOBBER is not currently part of any group.
734 void
735 function_info::append_clobber_to_group (clobber_info *clobber,
736 clobber_group *group)
738 clobber_info *prev = group->last_clobber ();
739 clobber_info::splay_tree::insert_child (prev, 1, clobber);
740 group->set_last_clobber (clobber);
741 clobber->set_group (group);
744 // Put CLOBBER1 and CLOBBER2 into the same clobber_group, given that
745 // CLOBBER1 occurs immediately before CLOBBER2 and that the two clobbers
746 // are not currently in the same group. LAST is the last definition of
747 // the associated resource, and is where any splay tree is stored.
748 void
749 function_info::merge_clobber_groups (clobber_info *clobber1,
750 clobber_info *clobber2,
751 def_info *last)
753 if (clobber1->is_in_group () && clobber2->is_in_group ())
755 clobber_group *group1 = clobber1->group ();
756 clobber_group *group2 = clobber2->group ();
757 gcc_checking_assert (clobber1 == group1->last_clobber ()
758 && clobber2 == group2->first_clobber ());
760 if (def_splay_tree tree = last->splay_root ())
762 // Remove GROUP2 from the splay tree.
763 int comparison = lookup_def (tree, clobber2->insn ());
764 gcc_checking_assert (comparison == 0);
765 tree.remove_root ();
766 last->set_splay_root (tree.root ());
769 // Splice the trees together.
770 group1->m_clobber_tree.splice_next_tree (group2->m_clobber_tree);
772 // Bring the two extremes of GROUP2 under GROUP1. Any other
773 // clobbers in the group are updated lazily on demand.
774 clobber2->set_group (group1);
775 group2->last_clobber ()->set_group (group1);
776 group1->set_last_clobber (group2->last_clobber ());
778 // Record that GROUP2 is no more.
779 group2->set_first_clobber (nullptr);
780 group2->set_last_clobber (nullptr);
781 group2->m_clobber_tree = nullptr;
783 else
785 // In this case there can be no active splay tree.
786 gcc_assert (!last->splay_root ());
787 if (clobber2->is_in_group ())
788 prepend_clobber_to_group (clobber1, clobber2->group ());
789 else
790 append_clobber_to_group (clobber2, need_clobber_group (clobber1));
794 // GROUP spans INSN, and INSN now sets the resource that GROUP clobbers.
795 // Split GROUP around INSN, to form two new groups, and return the clobber
796 // that comes immediately before INSN.
798 // The resource that GROUP clobbers is known to have an associated
799 // splay tree. The caller must remove GROUP from the tree on return.
800 clobber_info *
801 function_info::split_clobber_group (clobber_group *group, insn_info *insn)
803 // Search for either the previous or next clobber in the group.
804 // The result is less than zero if CLOBBER should come before NEIGHBOR
805 // or greater than zero if CLOBBER should come after NEIGHBOR.
806 clobber_tree &tree1 = group->m_clobber_tree;
807 int comparison = lookup_clobber (tree1, insn);
808 gcc_checking_assert (comparison != 0);
809 clobber_info *neighbor = tree1.root ();
811 clobber_tree tree2;
812 clobber_info *prev;
813 clobber_info *next;
814 if (comparison > 0)
816 // NEIGHBOR is the last clobber in what will become the first group.
817 tree2 = tree1.split_after_root ();
818 prev = neighbor;
819 next = as_a<clobber_info *> (prev->next_def ());
821 else
823 // NEIGHBOR is the first clobber in what will become the second group.
824 tree2 = neighbor;
825 tree1 = tree2.split_before_root ();
826 next = neighbor;
827 prev = as_a<clobber_info *> (next->prev_def ());
830 // Create a new group for each side of the split. We need to invalidate
831 // the old group so that clobber_info::group can tell whether a lazy
832 // update is needed.
833 clobber_info *first_clobber = group->first_clobber ();
834 clobber_info *last_clobber = group->last_clobber ();
835 auto *group1 = allocate<clobber_group> (first_clobber, prev, tree1.root ());
836 auto *group2 = allocate<clobber_group> (next, last_clobber, tree2.root ());
838 // Insert GROUP2 into the splay tree as an immediate successor of GROUP1.
839 def_splay_tree::insert_child (group, 1, group2);
840 def_splay_tree::insert_child (group, 1, group1);
842 // Invalidate the old group.
843 group->set_last_clobber (nullptr);
845 return prev;
848 // Add DEF to the end of the function's list of definitions of
849 // DEF->resource (). There is known to be no associated splay tree yet.
850 void
851 function_info::append_def (def_info *def)
853 gcc_checking_assert (!def->has_def_links ());
854 def_info **head = &m_defs[def->regno () + 1];
855 def_info *first = *head;
856 if (!first)
858 // This is the only definition of the resource.
859 def->set_last_def (def);
860 *head = def;
861 return;
864 def_info *prev = first->last_def ();
865 gcc_checking_assert (!prev->splay_root ());
867 // Maintain the invariant that two clobbers must not appear in
868 // neighboring nodes of the splay tree.
869 auto *clobber = dyn_cast<clobber_info *> (def);
870 auto *prev_clobber = dyn_cast<clobber_info *> (prev);
871 if (clobber && prev_clobber)
872 append_clobber_to_group (clobber, need_clobber_group (prev_clobber));
874 prev->set_next_def (def);
875 def->set_prev_def (prev);
876 first->set_last_def (def);
879 // Add DEF to the function's list of definitions of DEF->resource ().
880 // Also insert it into the associated splay tree, if there is one.
881 // DEF is not currently part of the list and is not in the splay tree.
882 void
883 function_info::add_def (def_info *def)
885 gcc_checking_assert (!def->has_def_links ()
886 && !def->m_is_temp
887 && !def->m_has_been_superceded);
888 def_info **head = &m_defs[def->regno () + 1];
889 def_info *first = *head;
890 if (!first)
892 // This is the only definition of the resource.
893 def->set_last_def (def);
894 *head = def;
895 return;
898 def_info *last = first->last_def ();
899 insn_info *insn = def->insn ();
901 int comparison;
902 def_node *root = nullptr;
903 def_info *prev = nullptr;
904 def_info *next = nullptr;
905 if (*insn > *last->insn ())
907 // This definition comes after all other definitions.
908 comparison = 1;
909 if (def_splay_tree tree = last->splay_root ())
911 tree.splay_max_node ();
912 root = tree.root ();
913 last->set_splay_root (root);
915 prev = last;
917 else if (*insn < *first->insn ())
919 // This definition comes before all other definitions.
920 comparison = -1;
921 if (def_splay_tree tree = last->splay_root ())
923 tree.splay_min_node ();
924 root = tree.root ();
925 last->set_splay_root (root);
927 next = first;
929 else
931 // Search the splay tree for an insertion point.
932 def_splay_tree tree = need_def_splay_tree (last);
933 comparison = lookup_def (tree, insn);
934 root = tree.root ();
935 last->set_splay_root (root);
937 // Deal with cases in which we found an overlapping live range.
938 if (comparison == 0)
940 auto *group = as_a<clobber_group *> (tree.root ());
941 if (auto *clobber = dyn_cast<clobber_info *> (def))
943 add_clobber (clobber, group);
944 return;
946 prev = split_clobber_group (group, insn);
947 next = prev->next_def ();
948 tree.remove_root ();
949 last->set_splay_root (tree.root ());
951 // COMPARISON is < 0 if DEF comes before ROOT or > 0 if DEF comes
952 // after ROOT.
953 else if (comparison < 0)
955 next = first_def (root);
956 prev = next->prev_def ();
958 else
960 prev = last_def (root);
961 next = prev->next_def ();
965 // See if we should merge CLOBBER with a neighboring clobber.
966 auto *clobber = dyn_cast<clobber_info *> (def);
967 auto *prev_clobber = safe_dyn_cast<clobber_info *> (prev);
968 auto *next_clobber = safe_dyn_cast<clobber_info *> (next);
969 // We shouldn't have consecutive clobber_groups.
970 gcc_checking_assert (!(clobber && prev_clobber && next_clobber));
971 if (clobber && prev_clobber)
972 append_clobber_to_group (clobber, need_clobber_group (prev_clobber));
973 else if (clobber && next_clobber)
974 prepend_clobber_to_group (clobber, need_clobber_group (next_clobber));
975 else if (root)
977 // If DEF comes before ROOT, insert DEF to ROOT's left,
978 // otherwise insert DEF to ROOT's right.
979 def_node *node = need_def_node (def);
980 def_splay_tree::insert_child (root, comparison >= 0, node);
982 if (prev)
983 insert_def_after (def, prev);
984 else
985 insert_def_before (def, next);
988 // Remove DEF from the function's list of definitions of DEF->resource ().
989 // Also remove DEF from the associated splay tree, if there is one.
990 void
991 function_info::remove_def (def_info *def)
993 def_info **head = &m_defs[def->regno () + 1];
994 def_info *first = *head;
995 gcc_checking_assert (first);
996 if (first->is_last_def ())
998 // DEF is the only definition of the resource.
999 gcc_checking_assert (first == def);
1000 *head = nullptr;
1001 def->clear_def_links ();
1002 return;
1005 // If CLOBBER belongs to a clobber_group that contains other clobbers
1006 // too, then we need to update the clobber_group and the list, but any
1007 // splay tree that contains the clobber_group is unaffected.
1008 if (auto *clobber = dyn_cast<clobber_info *> (def))
1009 if (clobber->is_in_group ())
1011 clobber_group *group = clobber->group ();
1012 if (group->first_clobber () != group->last_clobber ())
1014 remove_clobber (clobber, group);
1015 return;
1019 // If we've created a splay tree for this resource, remove the entry
1020 // for DEF.
1021 def_info *last = first->last_def ();
1022 if (def_splay_tree tree = last->splay_root ())
1024 int comparison = lookup_def (tree, def->insn ());
1025 gcc_checking_assert (comparison == 0);
1026 tree.remove_root ();
1027 last->set_splay_root (tree.root ());
1030 // If the definition came between two clobbers, merge them into a single
1031 // group.
1032 auto *prev_clobber = safe_dyn_cast<clobber_info *> (def->prev_def ());
1033 auto *next_clobber = safe_dyn_cast<clobber_info *> (def->next_def ());
1034 if (prev_clobber && next_clobber)
1035 merge_clobber_groups (prev_clobber, next_clobber, last);
1037 remove_def_from_list (def);
1040 // Require DEF to have a splay tree that contains all non-phi uses.
1041 void
1042 function_info::need_use_splay_tree (set_info *def)
1044 if (!def->m_use_tree)
1045 for (use_info *use : def->all_insn_uses ())
1047 auto *use_node = allocate<splay_tree_node<use_info *>> (use);
1048 def->m_use_tree.insert_max_node (use_node);
1052 // Compare two instructions by their position in a use splay tree. Return >0
1053 // if INSN1 comes after INSN2, <0 if INSN1 comes before INSN2, or 0 if they are
1054 // the same instruction.
1055 static inline int
1056 compare_use_insns (insn_info *insn1, insn_info *insn2)
1058 // Debug instructions go after nondebug instructions.
1059 int diff = insn1->is_debug_insn () - insn2->is_debug_insn ();
1060 if (diff != 0)
1061 return diff;
1062 return insn1->compare_with (insn2);
1065 // Search TREE for a use in INSN. If such a use exists, install it as
1066 // the root of TREE and return 0. Otherwise arbitrarily choose between:
1068 // (1) Installing the closest preceding use as the root and returning 1.
1069 // (2) Installing the closest following use as the root and returning -1.
1071 rtl_ssa::lookup_use (splay_tree<use_info *> &tree, insn_info *insn)
1073 auto compare = [&](splay_tree_node<use_info *> *node)
1075 return compare_use_insns (insn, node->value ()->insn ());
1077 return tree.lookup (compare);
1080 // Add USE to USE->def ()'s list of uses. inserting USE immediately before
1081 // BEFORE. USE is not currently in the list.
1083 // This routine should not be used for inserting phi uses.
1084 void
1085 function_info::insert_use_before (use_info *use, use_info *before)
1087 gcc_checking_assert (!use->has_use_links () && use->is_in_any_insn ());
1089 set_info *def = use->def ();
1091 use->copy_prev_from (before);
1092 use->set_next_use (before);
1094 if (use_info *prev = use->prev_use ())
1095 prev->set_next_use (use);
1096 else
1097 use->def ()->set_first_use (use);
1099 before->set_prev_use (use);
1100 if (use->is_in_nondebug_insn () && before->is_in_debug_insn_or_phi ())
1101 def->last_use ()->set_last_nondebug_insn_use (use);
1103 gcc_checking_assert (use->check_integrity () && before->check_integrity ());
1106 // Add USE to USE->def ()'s list of uses. inserting USE immediately after
1107 // AFTER. USE is not currently in the list.
1109 // This routine should not be used for inserting phi uses.
1110 void
1111 function_info::insert_use_after (use_info *use, use_info *after)
1113 set_info *def = use->def ();
1114 gcc_checking_assert (after->is_in_any_insn ()
1115 && !use->has_use_links ()
1116 && use->is_in_any_insn ());
1118 use->set_prev_use (after);
1119 use->copy_next_from (after);
1121 after->set_next_use (use);
1123 if (use_info *next = use->next_use ())
1125 // The last node doesn't change, but we might need to update its
1126 // last_nondebug_insn_use record.
1127 if (use->is_in_nondebug_insn () && next->is_in_debug_insn_or_phi ())
1128 def->last_use ()->set_last_nondebug_insn_use (use);
1129 next->set_prev_use (use);
1131 else
1133 // USE is now the last node.
1134 if (use->is_in_nondebug_insn ())
1135 use->set_last_nondebug_insn_use (use);
1136 def->first_use ()->set_last_use (use);
1139 gcc_checking_assert (use->check_integrity () && after->check_integrity ());
1142 // If USE has a known definition, add USE to that definition's list of uses.
1143 // Also update the associated splay tree, if any.
1144 void
1145 function_info::add_use (use_info *use)
1147 gcc_checking_assert (!use->has_use_links ()
1148 && !use->m_is_temp
1149 && !use->m_has_been_superceded);
1151 set_info *def = use->def ();
1152 if (!def)
1153 return;
1155 use_info *first = def->first_use ();
1156 if (!first)
1158 // This is the only use of the definition.
1159 use->set_last_use (use);
1160 if (use->is_in_nondebug_insn ())
1161 use->set_last_nondebug_insn_use (use);
1163 def->set_first_use (use);
1165 gcc_checking_assert (use->check_integrity ());
1166 return;
1169 if (use->is_in_phi ())
1171 // Add USE at the end of the list, as the new first phi.
1172 use_info *last = first->last_use ();
1174 use->set_prev_use (last);
1175 use->copy_next_from (last);
1177 last->set_next_use (use);
1178 first->set_last_use (use);
1180 gcc_checking_assert (use->check_integrity ());
1181 return;
1184 // If there is currently no splay tree for this definition, see if can
1185 // get away with a pure list-based update.
1186 insn_info *insn = use->insn ();
1187 auto quick_path = [&]()
1189 // Check if USE should come before all current uses.
1190 if (first->is_in_phi () || compare_use_insns (insn, first->insn ()) < 0)
1192 insert_use_before (use, first);
1193 return true;
1196 // Check if USE should come after all current uses in the same
1197 // subsequence (i.e. the list of nondebug insn uses or the list
1198 // of debug insn uses).
1199 use_info *last = first->last_use ();
1200 if (use->is_in_debug_insn ())
1202 if (last->is_in_phi ())
1203 return false;
1205 else
1206 last = last->last_nondebug_insn_use ();
1208 if (compare_use_insns (insn, last->insn ()) > 0)
1210 insert_use_after (use, last);
1211 return true;
1214 return false;
1216 if (!def->m_use_tree && quick_path ())
1217 return;
1219 // Search the splay tree for an insertion point. COMPARISON is less
1220 // than zero if USE should come before NEIGHBOR, or greater than zero
1221 // if USE should come after NEIGHBOR.
1222 need_use_splay_tree (def);
1223 int comparison = lookup_use (def->m_use_tree, insn);
1224 gcc_checking_assert (comparison != 0);
1225 splay_tree_node<use_info *> *neighbor = def->m_use_tree.root ();
1227 // If USE comes before NEIGHBOR, insert USE to NEIGHBOR's left,
1228 // otherwise insert USE to NEIGHBOR's right.
1229 auto *use_node = allocate<splay_tree_node<use_info *>> (use);
1230 def->m_use_tree.insert_child (neighbor, comparison > 0, use_node);
1231 if (comparison > 0)
1232 insert_use_after (use, neighbor->value ());
1233 else
1234 insert_use_before (use, neighbor->value ());
1237 void
1238 function_info::reparent_use (use_info *use, set_info *new_def)
1240 remove_use (use);
1241 use->set_def (new_def);
1242 add_use (use);
1245 // If USE has a known definition, remove USE from that definition's list
1246 // of uses. Also remove if it from the associated splay tree, if any.
1247 void
1248 function_info::remove_use (use_info *use)
1250 set_info *def = use->def ();
1251 if (!def)
1252 return;
1254 // Remove USE from the splay tree.
1255 if (def->m_use_tree && use->is_in_any_insn ())
1257 int comparison = lookup_use (def->m_use_tree, use->insn ());
1258 gcc_checking_assert (comparison == 0);
1259 def->m_use_tree.remove_root ();
1262 use_info *prev = use->prev_use ();
1263 use_info *next = use->next_use ();
1265 use_info *first = def->first_use ();
1266 use_info *last = first->last_use ();
1267 if (last->last_nondebug_insn_use () == use)
1268 last->set_last_nondebug_insn_use (prev);
1270 if (next)
1271 next->copy_prev_from (use);
1272 else
1273 first->set_last_use (prev);
1275 if (prev)
1276 prev->copy_next_from (use);
1277 else
1278 def->set_first_use (next);
1280 use->clear_use_links ();
1281 gcc_checking_assert ((!prev || prev->check_integrity ())
1282 && (!next || next->check_integrity ()));
1285 // Allocate a temporary clobber_info for register REGNO in insn INSN,
1286 // including it in the region of the obstack governed by WATERMARK.
1287 // Return a new def_array that contains OLD_DEFS and the new clobber.
1289 // OLD_DEFS is known not to define REGNO.
1290 def_array
1291 function_info::insert_temp_clobber (obstack_watermark &watermark,
1292 insn_info *insn, unsigned int regno,
1293 def_array old_defs)
1295 gcc_checking_assert (watermark == &m_temp_obstack);
1296 auto *clobber = allocate_temp<clobber_info> (insn, regno);
1297 clobber->m_is_temp = true;
1298 return insert_access (watermark, clobber, old_defs);
1301 // See the comment above the declaration.
1302 bool
1303 function_info::remains_available_at_insn (const set_info *set,
1304 insn_info *insn)
1306 auto *ebb = set->ebb ();
1307 gcc_checking_assert (ebb == insn->ebb ());
1309 def_info *next_def = set->next_def ();
1310 if (next_def && *next_def->insn () < *insn)
1311 return false;
1313 if (HARD_REGISTER_NUM_P (set->regno ())
1314 && TEST_HARD_REG_BIT (m_clobbered_by_calls, set->regno ()))
1315 for (ebb_call_clobbers_info *call_group : ebb->call_clobbers ())
1317 if (!call_group->clobbers (set->resource ()))
1318 continue;
1320 insn_info *call_insn = next_call_clobbers (*call_group, insn);
1321 if (call_insn && *call_insn < *insn)
1322 return false;
1325 return true;
1328 // See the comment above the declaration.
1329 bool
1330 function_info::remains_available_on_exit (const set_info *set, bb_info *bb)
1332 if (HARD_REGISTER_NUM_P (set->regno ())
1333 && TEST_HARD_REG_BIT (m_clobbered_by_calls, set->regno ()))
1335 insn_info *search_insn = (set->bb () == bb
1336 ? set->insn ()
1337 : bb->head_insn ());
1338 for (ebb_call_clobbers_info *call_group : bb->ebb ()->call_clobbers ())
1340 if (!call_group->clobbers (set->resource ()))
1341 continue;
1343 insn_info *insn = next_call_clobbers (*call_group, search_insn);
1344 if (insn && insn->bb () == bb)
1345 return false;
1349 return (set->is_last_def ()
1350 || *set->next_def ()->insn () > *bb->end_insn ());
1353 // A subroutine of make_uses_available. Try to make USE's definition
1354 // available at the head of BB. WILL_BE_DEBUG_USE is true if the
1355 // definition will be used only in debug instructions.
1357 // On success:
1359 // - If the use would have the same def () as USE, return USE.
1361 // - If BB already has a degenerate phi for the same definition,
1362 // return a temporary use of that phi.
1364 // - Otherwise, the use would need a new degenerate phi. Allocate a
1365 // temporary phi and return a temporary use of it.
1367 // Return null on failure.
1368 use_info *
1369 function_info::make_use_available (use_info *use, bb_info *bb,
1370 bool will_be_debug_use)
1372 set_info *def = use->def ();
1373 if (!def)
1374 return use;
1376 if (is_single_dominating_def (def))
1377 return use;
1379 if (def->ebb () == bb->ebb ())
1381 if (remains_available_at_insn (def, bb->head_insn ()))
1382 return use;
1383 return nullptr;
1386 basic_block cfg_bb = bb->cfg_bb ();
1387 bb_info *use_bb = use->bb ();
1388 if (single_pred_p (cfg_bb)
1389 && single_pred (cfg_bb) == use_bb->cfg_bb ()
1390 && remains_available_on_exit (def, use_bb))
1392 if (will_be_debug_use)
1393 return use;
1395 resource_info resource = use->resource ();
1396 set_info *ultimate_def = look_through_degenerate_phi (def);
1398 // See if there is already a (degenerate) phi for DEF.
1399 insn_info *phi_insn = bb->ebb ()->phi_insn ();
1400 phi_info *phi;
1401 def_lookup dl = find_def (resource, phi_insn);
1402 if (set_info *set = dl.matching_set ())
1404 // There is an existing phi.
1405 phi = as_a<phi_info *> (set);
1406 gcc_checking_assert (phi->input_value (0) == ultimate_def);
1408 else
1410 // Create a temporary placeholder phi. This will become
1411 // permanent if the change is later committed.
1412 phi = allocate_temp<phi_info> (phi_insn, resource, 0);
1413 auto *input = allocate_temp<use_info> (phi, resource, ultimate_def);
1414 input->m_is_temp = true;
1415 phi->m_is_temp = true;
1416 phi->make_degenerate (input);
1417 if (def_info *prev = dl.prev_def (phi_insn))
1418 phi->set_prev_def (prev);
1419 if (def_info *next = dl.next_def (phi_insn))
1420 phi->set_next_def (next);
1423 // Create a temporary use of the phi at the head of the first
1424 // block, since we know for sure that it's available there.
1425 insn_info *use_insn = bb->ebb ()->first_bb ()->head_insn ();
1426 auto *new_use = allocate_temp<use_info> (use_insn, resource, phi);
1427 new_use->m_is_temp = true;
1428 return new_use;
1430 return nullptr;
1433 // See the comment above the declaration.
1434 use_array
1435 function_info::make_uses_available (obstack_watermark &watermark,
1436 use_array uses, bb_info *bb,
1437 bool will_be_debug_uses)
1439 unsigned int num_uses = uses.size ();
1440 if (num_uses == 0)
1441 return uses;
1443 auto **new_uses = XOBNEWVEC (watermark, access_info *, num_uses);
1444 for (unsigned int i = 0; i < num_uses; ++i)
1446 use_info *use = make_use_available (uses[i], bb, will_be_debug_uses);
1447 if (!use)
1448 return use_array (access_array::invalid ());
1449 new_uses[i] = use;
1451 return use_array (new_uses, num_uses);
1454 set_info *
1455 function_info::create_set (obstack_watermark &watermark,
1456 insn_info *insn,
1457 resource_info resource)
1459 auto set = change_alloc<set_info> (watermark, insn, resource);
1460 set->m_is_temp = true;
1461 return set;
1464 use_info *
1465 function_info::create_use (obstack_watermark &watermark,
1466 insn_info *insn,
1467 set_info *set)
1469 auto use = change_alloc<use_info> (watermark, insn, set->resource (), set);
1470 use->m_is_temp = true;
1471 return use;
1474 // Return true if ACCESS1 can represent ACCESS2 and if ACCESS2 can
1475 // represent ACCESS1.
1476 static bool
1477 can_merge_accesses (access_info *access1, access_info *access2)
1479 if (access1 == access2)
1480 return true;
1482 auto *use1 = dyn_cast<use_info *> (access1);
1483 auto *use2 = dyn_cast<use_info *> (access2);
1484 return use1 && use2 && use1->def () == use2->def ();
1487 // See the comment above the declaration.
1488 access_array
1489 rtl_ssa::merge_access_arrays_base (obstack_watermark &watermark,
1490 access_array accesses1,
1491 access_array accesses2)
1493 if (accesses1.empty ())
1494 return accesses2;
1495 if (accesses2.empty ())
1496 return accesses1;
1498 auto i1 = accesses1.begin ();
1499 auto end1 = accesses1.end ();
1500 auto i2 = accesses2.begin ();
1501 auto end2 = accesses2.end ();
1503 access_array_builder builder (watermark);
1504 builder.reserve (accesses1.size () + accesses2.size ());
1506 while (i1 != end1 && i2 != end2)
1508 access_info *access1 = *i1;
1509 access_info *access2 = *i2;
1511 unsigned int regno1 = access1->regno ();
1512 unsigned int regno2 = access2->regno ();
1513 if (regno1 == regno2)
1515 if (!can_merge_accesses (access1, access2))
1516 return access_array::invalid ();
1518 builder.quick_push (access1);
1519 ++i1;
1520 ++i2;
1522 else if (regno1 < regno2)
1524 builder.quick_push (access1);
1525 ++i1;
1527 else
1529 builder.quick_push (access2);
1530 ++i2;
1533 for (; i1 != end1; ++i1)
1534 builder.quick_push (*i1);
1535 for (; i2 != end2; ++i2)
1536 builder.quick_push (*i2);
1538 return builder.finish ();
1541 // See the comment above the declaration.
1542 access_array
1543 rtl_ssa::insert_access_base (obstack_watermark &watermark,
1544 access_info *access1, access_array accesses2)
1546 access_array_builder builder (watermark);
1547 builder.reserve (1 + accesses2.size ());
1549 unsigned int regno1 = access1->regno ();
1550 auto i2 = accesses2.begin ();
1551 auto end2 = accesses2.end ();
1552 while (i2 != end2)
1554 access_info *access2 = *i2;
1556 unsigned int regno2 = access2->regno ();
1557 if (regno1 == regno2)
1559 if (!can_merge_accesses (access1, access2))
1560 return access_array::invalid ();
1562 builder.quick_push (access1);
1563 access1 = nullptr;
1564 ++i2;
1565 break;
1567 else if (regno1 < regno2)
1569 builder.quick_push (access1);
1570 access1 = nullptr;
1571 break;
1573 else
1575 builder.quick_push (access2);
1576 ++i2;
1579 if (access1)
1580 builder.quick_push (access1);
1581 for (; i2 != end2; ++i2)
1582 builder.quick_push (*i2);
1584 return builder.finish ();
1587 // See the comment above the declaration.
1588 use_array
1589 rtl_ssa::remove_uses_of_def (obstack_watermark &watermark, use_array uses,
1590 def_info *def)
1592 access_array_builder uses_builder (watermark);
1593 uses_builder.reserve (uses.size ());
1594 for (use_info *use : uses)
1595 if (use->def () != def)
1596 uses_builder.quick_push (use);
1597 return use_array (uses_builder.finish ());
1600 // See the comment above the declaration.
1601 access_array
1602 rtl_ssa::remove_note_accesses_base (obstack_watermark &watermark,
1603 access_array accesses)
1605 auto predicate = [](access_info *a) {
1606 return !a->only_occurs_in_notes ();
1609 for (access_info *access : accesses)
1610 if (access->only_occurs_in_notes ())
1611 return filter_accesses (watermark, accesses, predicate);
1613 return accesses;
1616 // See the comment above the declaration.
1617 bool
1618 rtl_ssa::accesses_reference_same_resource (access_array accesses1,
1619 access_array accesses2)
1621 auto i1 = accesses1.begin ();
1622 auto end1 = accesses1.end ();
1623 auto i2 = accesses2.begin ();
1624 auto end2 = accesses2.end ();
1626 while (i1 != end1 && i2 != end2)
1628 access_info *access1 = *i1;
1629 access_info *access2 = *i2;
1631 unsigned int regno1 = access1->regno ();
1632 unsigned int regno2 = access2->regno ();
1633 if (regno1 == regno2)
1634 return true;
1636 if (regno1 < regno2)
1637 ++i1;
1638 else
1639 ++i2;
1641 return false;
1644 // See the comment above the declaration.
1645 bool
1646 rtl_ssa::insn_clobbers_resources (insn_info *insn, access_array accesses)
1648 if (accesses_reference_same_resource (insn->defs (), accesses))
1649 return true;
1651 if (insn->is_call () && accesses_include_hard_registers (accesses))
1653 function_abi abi = insn_callee_abi (insn->rtl ());
1654 for (const access_info *access : accesses)
1656 if (!HARD_REGISTER_NUM_P (access->regno ()))
1657 break;
1658 if (abi.clobbers_reg_p (access->mode (), access->regno ()))
1659 return true;
1663 return false;
1666 // Print RESOURCE to PP.
1667 void
1668 rtl_ssa::pp_resource (pretty_printer *pp, resource_info resource)
1670 resource.print (pp);
1673 // Print ACCESS to PP. FLAGS is a bitmask of PP_ACCESS_* flags.
1674 void
1675 rtl_ssa::pp_access (pretty_printer *pp, const access_info *access,
1676 unsigned int flags)
1678 if (!access)
1679 pp_string (pp, "<null>");
1680 else if (auto *phi = dyn_cast<const phi_info *> (access))
1681 phi->print (pp, flags);
1682 else if (auto *set = dyn_cast<const set_info *> (access))
1683 set->print (pp, flags);
1684 else if (auto *clobber = dyn_cast<const clobber_info *> (access))
1685 clobber->print (pp, flags);
1686 else if (auto *use = dyn_cast<const use_info *> (access))
1687 use->print (pp, flags);
1688 else
1689 pp_string (pp, "??? Unknown access");
1692 // Print ACCESSES to PP. FLAGS is a bitmask of PP_ACCESS_* flags.
1693 void
1694 rtl_ssa::pp_accesses (pretty_printer *pp, access_array accesses,
1695 unsigned int flags)
1697 if (accesses.empty ())
1698 pp_string (pp, "none");
1699 else
1701 bool is_first = true;
1702 for (access_info *access : accesses)
1704 if (is_first)
1705 is_first = false;
1706 else
1707 pp_newline_and_indent (pp, 0);
1708 pp_access (pp, access, flags);
1713 // Print NODE to PP.
1714 void
1715 rtl_ssa::pp_def_node (pretty_printer *pp, const def_node *node)
1717 if (!node)
1718 pp_string (pp, "<null>");
1719 else if (auto *group = dyn_cast<const clobber_group *> (node))
1720 group->print (pp);
1721 else if (auto *set = dyn_cast<const set_node *> (node))
1722 set->print (pp);
1723 else
1724 pp_string (pp, "??? Unknown def node");
1727 // Print MUX to PP.
1728 void
1729 rtl_ssa::pp_def_mux (pretty_printer *pp, def_mux mux)
1731 if (auto *node = mux.dyn_cast<def_node *> ())
1732 pp_def_node (pp, node);
1733 else
1734 pp_access (pp, mux.as_a<def_info *> ());
1737 // Print DL to PP.
1738 void
1739 rtl_ssa::pp_def_lookup (pretty_printer *pp, def_lookup dl)
1741 pp_string (pp, "comparison result of ");
1742 pp_decimal_int (pp, dl.comparison);
1743 pp_string (pp, " for ");
1744 pp_newline_and_indent (pp, 0);
1745 pp_def_mux (pp, dl.mux);
1748 // Dump RESOURCE to FILE.
1749 void
1750 dump (FILE *file, resource_info resource)
1752 dump_using (file, pp_resource, resource);
1755 // Dump ACCESS to FILE. FLAGS is a bitmask of PP_ACCESS_* flags.
1756 void
1757 dump (FILE *file, const access_info *access, unsigned int flags)
1759 dump_using (file, pp_access, access, flags);
1762 // Dump ACCESSES to FILE. FLAGS is a bitmask of PP_ACCESS_* flags.
1763 void
1764 dump (FILE *file, access_array accesses, unsigned int flags)
1766 dump_using (file, pp_accesses, accesses, flags);
1769 // Print NODE to FILE.
1770 void
1771 dump (FILE *file, const def_node *node)
1773 dump_using (file, pp_def_node, node);
1776 // Print MUX to FILE.
1777 void
1778 dump (FILE *file, def_mux mux)
1780 dump_using (file, pp_def_mux, mux);
1783 // Print RESULT to FILE.
1784 void
1785 dump (FILE *file, def_lookup result)
1787 dump_using (file, pp_def_lookup, result);
1790 // Debug interfaces to the dump routines above.
1791 void debug (const resource_info &x) { dump (stderr, x); }
1792 void debug (const access_info *x) { dump (stderr, x); }
1793 void debug (const access_array &x) { dump (stderr, x); }
1794 void debug (const def_node *x) { dump (stderr, x); }
1795 void debug (const def_mux &x) { dump (stderr, x); }
1796 void debug (const def_lookup &x) { dump (stderr, x); }