Debian Codegraph @

gcc/rtl-ssa/accesses.cc

raw
1 // Implementation of access-related functions for RTL SSA -*- C++ -*-
2 // Copyright (C) 2020-2025 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.
10 //
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.
15 //
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/>.
19
20 #define INCLUDE_ALGORITHM
21 #define INCLUDE_FUNCTIONAL
22 #define INCLUDE_ARRAY
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "rtl.h"
28 #include "df.h"
29 #include "rtl-ssa.h"
30 #include "rtl-ssa/internals.h"
31 #include "rtl-ssa/internals.inl"
32
33 using namespace rtl_ssa;
34
35 // This clobber belongs to a clobber_group but m_group appears to be
36 // out of date. Update it and return the new (correct) value.
37 clobber_group *
38 clobber_info::recompute_group ()
39 {
40 using splay_tree = clobber_info::splay_tree;
41
42 // Splay this clobber to the root of the tree while searching for a node
43 // that has the correct group. The root always has the correct group,
44 // so the search always breaks early and does not install this clobber
45 // as the root.
46 clobber_info *cursor = m_parent;
47 auto find_group = [](clobber_info *node, unsigned int)
48 {
49 return node->m_group->has_been_superceded () ? nullptr : node->m_group;
50 };
51 clobber_group *group = splay_tree::splay_and_search (this, nullptr,
52 find_group);
53 gcc_checking_assert (m_parent);
54
55 // If the previous splay operation did anything, this clobber is now an
56 // ancestor of CURSOR, and all the nodes inbetween have a stale group.
57 // Since we have visited the nodes, we might as well update them too.
58 //
59 // If the previous splay operation did nothing, start the update from
60 // this clobber instead. In that case we change at most two clobbers:
61 // this clobber and possibly its parent.
62 if (cursor == m_parent)
63 cursor = this;
64
65 // Walk up the tree from CURSOR updating clobbers that need it.
66 // This walk always includes this clobber.
67 while (cursor->m_group != group)
68 {
69 cursor->m_group = group;
70 cursor = cursor->m_parent;
71 }
72
73 gcc_checking_assert (m_group == group);
74 return group;
75 }
76
77 // See the comment above the declaration.
78 void
79 resource_info::print_identifier (pretty_printer *pp) const
80 {
81 if (is_mem ())
82 pp_string (pp, "mem");
83 else
84 {
85 char tmp[3 * sizeof (regno) + 2];
86 snprintf (tmp, sizeof (tmp), "r%d", regno);
87 pp_string (pp, tmp);
88 }
89 }
90
91 // See the comment above the declaration.
92 void
93 resource_info::print_context (pretty_printer *pp) const
94 {
95 if (HARD_REGISTER_NUM_P (regno))
96 {
97 if (const char *name = reg_names[regno])
98 {
99 pp_space (pp);
100 pp_left_paren (pp);
101 pp_string (pp, name);
102 if (mode != E_BLKmode)
103 {
104 pp_colon (pp);
105 pp_string (pp, GET_MODE_NAME (mode));
106 }
107 pp_right_paren (pp);
108 }
109 }
110 else if (is_reg ())
111 {
112 pp_space (pp);
113 pp_left_paren (pp);
114 if (mode != E_BLKmode)
115 {
116 pp_string (pp, GET_MODE_NAME (mode));
117 pp_space (pp);
118 }
119 pp_string (pp, "pseudo");
120 pp_right_paren (pp);
121 }
122 }
123
124 // See the comment above the declaration.
125 void
126 resource_info::print (pretty_printer *pp) const
127 {
128 print_identifier (pp);
129 print_context (pp);
130 }
131
132 // Some properties can naturally be described using adjectives that attach
133 // to nouns like "use" or "definition". Print such adjectives to PP.
134 void
135 access_info::print_prefix_flags (pretty_printer *pp) const
136 {
137 if (m_is_temp)
138 pp_string (pp, "temporary ");
139 if (m_has_been_superceded)
140 pp_string (pp, "superceded ");
141 }
142
143 // Print properties not handled by print_prefix_flags to PP, putting
144 // each property on a new line indented by two extra spaces.
145 void
146 access_info::print_properties_on_new_lines (pretty_printer *pp) const
147 {
148 if (m_is_pre_post_modify)
149 {
150 pp_newline_and_indent (pp, 2);
151 pp_string (pp, "set by a pre/post-modify");
152 pp_indentation (pp) -= 2;
153 }
154 if (m_includes_address_uses)
155 {
156 pp_newline_and_indent (pp, 2);
157 pp_string (pp, "appears inside an address");
158 pp_indentation (pp) -= 2;
159 }
160 if (m_includes_read_writes)
161 {
162 pp_newline_and_indent (pp, 2);
163 pp_string (pp, "appears in a read/write context");
164 pp_indentation (pp) -= 2;
165 }
166 if (m_includes_subregs)
167 {
168 pp_newline_and_indent (pp, 2);
169 pp_string (pp, "appears inside a subreg");
170 pp_indentation (pp) -= 2;
171 }
172 }
173
174 // Return true if there are no known issues with the integrity of the
175 // link information.
176 inline bool
177 use_info::check_integrity ()
178 {
179 auto subsequence_id = [](use_info *use)
180 {
181 if (use->is_in_nondebug_insn ())
182 return 1;
183 if (use->is_in_debug_insn ())
184 return 2;
185 return 3;
186 };
187
188 use_info *prev = prev_use ();
189 use_info *next = next_use ();
190
191 if (prev && subsequence_id (prev) > subsequence_id (this))
192 return false;
193 if (next && subsequence_id (next) < subsequence_id (this))
194 return false;
195 if (m_is_last_nondebug_insn_use != calculate_is_last_nondebug_insn_use ())
196 return false;
197
198 if (!prev && last_use ()->next_use ())
199 return false;
200 if (!next)
201 if (use_info *use = last_nondebug_insn_use ())
202 if (!use->m_is_last_nondebug_insn_use)
203 return false;
204
205 return true;
206 }
207
208 // See the comment above the declaration.
209 void
210 use_info::print_location (pretty_printer *pp) const
211 {
212 if (is_in_phi ())
213 pp_access (pp, phi (), PP_ACCESS_INCLUDE_LOCATION);
214 else
215 insn ()->print_identifier_and_location (pp);
216 }
217
218 // See the comment above the declaration.
219 void
220 use_info::print_def (pretty_printer *pp) const
221 {
222 if (const set_info *set = def ())
223 pp_access (pp, set, 0);
224 else
225 {
226 pp_string (pp, "undefined ");
227 resource ().print (pp);
228 }
229 }
230
231 // See the comment above the declaration.
232 void
233 use_info::print (pretty_printer *pp, unsigned int flags) const
234 {
235 print_prefix_flags (pp);
236
237 const set_info *set = def ();
238 if (set && set->mode () != mode ())
239 {
240 pp_string (pp, GET_MODE_NAME (mode ()));
241 pp_space (pp);
242 }
243
244 pp_string (pp, "use of ");
245 print_def (pp);
246 if (flags & PP_ACCESS_INCLUDE_LOCATION)
247 {
248 pp_string (pp, " by ");
249 print_location (pp);
250 }
251 if (set && (flags & PP_ACCESS_INCLUDE_LINKS))
252 {
253 pp_newline_and_indent (pp, 2);
254 pp_string (pp, "defined in ");
255 set->insn ()->print_location (pp);
256 pp_indentation (pp) -= 2;
257 }
258 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
259 print_properties_on_new_lines (pp);
260 }
261
262 // See the comment above the declaration.
263 void
264 def_info::print_identifier (pretty_printer *pp) const
265 {
266 resource ().print_identifier (pp);
267 pp_colon (pp);
268 insn ()->print_identifier (pp);
269 resource ().print_context (pp);
270 }
271
272 // See the comment above the declaration.
273 void
274 def_info::print_location (pretty_printer *pp) const
275 {
276 insn ()->print_identifier_and_location (pp);
277 }
278
279 // See the comment above the declaration.
280 void
281 clobber_info::print (pretty_printer *pp, unsigned int flags) const
282 {
283 print_prefix_flags (pp);
284 if (is_call_clobber ())
285 pp_string (pp, "call ");
286 pp_string (pp, "clobber ");
287 print_identifier (pp);
288 if (flags & PP_ACCESS_INCLUDE_LOCATION)
289 {
290 pp_string (pp, " in ");
291 insn ()->print_location (pp);
292 }
293 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
294 print_properties_on_new_lines (pp);
295 }
296
297 // See the comment above the declaration.
298 void
299 set_info::print_uses_on_new_lines (pretty_printer *pp) const
300 {
301 for (const use_info *use : all_uses ())
302 {
303 pp_newline_and_indent (pp, 2);
304 if (use->is_live_out_use ())
305 {
306 pp_string (pp, "live out from ");
307 use->insn ()->print_location (pp);
308 }
309 else
310 {
311 pp_string (pp, "used by ");
312 use->print_location (pp);
313 }
314 pp_indentation (pp) -= 2;
315 }
316 if (m_use_tree)
317 {
318 pp_newline_and_indent (pp, 2);
319 pp_string (pp, "splay tree:");
320 pp_newline_and_indent (pp, 2);
321 auto print_use = [](pretty_printer *pp,
322 splay_tree_node<use_info *> *node)
323 {
324 pp_string (pp, "use by ");
325 node->value ()->print_location (pp);
326 };
327 m_use_tree.print (pp, m_use_tree.root (), print_use);
328 pp_indentation (pp) -= 4;
329 }
330 }
331
332 // See the comment above the declaration.
333 void
334 set_info::print (pretty_printer *pp, unsigned int flags) const
335 {
336 print_prefix_flags (pp);
337 pp_string (pp, "set ");
338 print_identifier (pp);
339 if (flags & PP_ACCESS_INCLUDE_LOCATION)
340 {
341 pp_string (pp, " in ");
342 insn ()->print_location (pp);
343 }
344 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
345 print_properties_on_new_lines (pp);
346 if (flags & PP_ACCESS_INCLUDE_LINKS)
347 print_uses_on_new_lines (pp);
348 }
349
350 // See the comment above the declaration.
351 void
352 phi_info::print (pretty_printer *pp, unsigned int flags) const
353 {
354 print_prefix_flags (pp);
355 pp_string (pp, "phi node ");
356 print_identifier (pp);
357 if (flags & PP_ACCESS_INCLUDE_LOCATION)
358 {
359 pp_string (pp, " in ");
360 insn ()->print_location (pp);
361 }
362
363 if (flags & PP_ACCESS_INCLUDE_PROPERTIES)
364 print_properties_on_new_lines (pp);
365
366 if (flags & PP_ACCESS_INCLUDE_LINKS)
367 {
368 basic_block cfg_bb = bb ()->cfg_bb ();
369 pp_newline_and_indent (pp, 2);
370 pp_string (pp, "inputs:");
371 unsigned int i = 0;
372 for (const use_info *input : inputs ())
373 {
374 basic_block pred_cfg_bb = EDGE_PRED (cfg_bb, i)->src;
375 pp_newline_and_indent (pp, 2);
376 pp_string (pp, "bb");
377 pp_decimal_int (pp, pred_cfg_bb->index);
378 pp_colon (pp);
379 pp_space (pp);
380 input->print_def (pp);
381 pp_indentation (pp) -= 2;
382 i += 1;
383 }
384 pp_indentation (pp) -= 2;
385
386 print_uses_on_new_lines (pp);
387 }
388 }
389
390 // See the comment above the declaration.
391 void
392 set_node::print (pretty_printer *pp) const
393 {
394 pp_access (pp, first_def ());
395 }
396
397 // See the comment above the declaration.
398 clobber_info *
399 clobber_group::prev_clobber (insn_info *insn) const
400 {
401 int comparison = lookup_clobber (insn);
402 if (comparison <= 0)
403 return dyn_cast<clobber_info *> (m_clobber_tree.root ()->prev_def ());
404 return m_clobber_tree.root ();
405 }
406
407 // See the comment above the declaration.
408 clobber_info *
409 clobber_group::next_clobber (insn_info *insn) const
410 {
411 int comparison = lookup_clobber (insn);
412 if (comparison >= 0)
413 return dyn_cast<clobber_info *> (m_clobber_tree.root ()->next_def ());
414 return m_clobber_tree.root ();
415 }
416
417 // See the comment above the declaration.
418 void
419 clobber_group::print (pretty_printer *pp) const
420 {
421 auto print_clobber = [](pretty_printer *pp, const def_info *clobber)
422 {
423 pp_access (pp, clobber);
424 };
425 pp_string (pp, "grouped clobber");
426 for (const def_info *clobber : clobbers ())
427 {
428 pp_newline_and_indent (pp, 2);
429 print_clobber (pp, clobber);
430 pp_indentation (pp) -= 2;
431 }
432 pp_newline_and_indent (pp, 2);
433 pp_string (pp, "splay tree");
434 pp_newline_and_indent (pp, 2);
435 m_clobber_tree.print (pp, print_clobber);
436 pp_indentation (pp) -= 4;
437 }
438
439 // A wrapper around rtl_ssa::lookup_clobber that ensures that the root
440 // of the splay tree always has the correct group.
441 int
442 clobber_group::lookup_clobber (insn_info *insn) const
443 {
444 auto &tree = const_cast<clobber_tree &> (m_clobber_tree);
445 int result = rtl_ssa::lookup_clobber (tree, insn);
446 tree->update_group (const_cast<clobber_group *> (this));
447 return result;
448 }
449
450 // See the comment above the declaration.
451 def_info *
452 def_lookup::prev_def (insn_info *insn) const
453 {
454 if (mux && comparison == 0)
455 if (auto *node = mux.dyn_cast<def_node *> ())
456 if (auto *group = dyn_cast<clobber_group *> (node))
457 if (clobber_info *clobber = group->prev_clobber (insn))
458 return clobber;
459
460 return last_def_of_prev_group ();
461 }
462
463 // See the comment above the declaration.
464 def_info *
465 def_lookup::next_def (insn_info *insn) const
466 {
467 if (mux && comparison == 0)
468 if (auto *node = mux.dyn_cast<def_node *> ())
469 if (auto *group = dyn_cast<clobber_group *> (node))
470 if (clobber_info *clobber = group->next_clobber (insn))
471 return clobber;
472
473 return first_def_of_next_group ();
474 }
475
476 // Return a clobber_group for CLOBBER, creating one if CLOBBER doesn't
477 // already belong to a group.
478 clobber_group *
479 function_info::need_clobber_group (clobber_info *clobber)
480 {
481 if (clobber->is_in_group ())
482 return clobber->group ();
483 return allocate<clobber_group> (clobber);
484 }
485
486 // Return a def_node for inserting DEF into the associated resource's
487 // splay tree. Use a clobber_group if DEF is a clobber and a set_node
488 // otherwise.
489 def_node *
490 function_info::need_def_node (def_info *def)
491 {
492 if (auto *clobber = dyn_cast<clobber_info *> (def))
493 return need_clobber_group (clobber);
494 return allocate<set_node> (as_a<set_info *> (def));
495 }
496
497 // LAST is the last thing to define LAST->resource (), and is where any
498 // splay tree root for LAST->resource () is stored. Require such a splay tree
499 // to exist, creating a new one if necessary. Return the root of the tree.
500 //
501 // The caller must call LAST->set_splay_root after it has finished with
502 // the splay tree.
503 def_splay_tree
504 function_info::need_def_splay_tree (def_info *last)
505 {
506 if (def_node *root = last->splay_root ())
507 return root;
508
509 // Use a left-spine rooted at the last node.
510 def_node *root = need_def_node (last);
511 def_node *parent = root;
512 while (def_info *prev = first_def (parent)->prev_def ())
513 {
514 def_node *node = need_def_node (prev);
515 def_splay_tree::insert_child (parent, 0, node);
516 parent = node;
517 }
518 return root;
519 }
520
521 // Search TREE for either:
522 //
523 // - a set_info at INSN or
524 // - a clobber_group whose range includes INSN
525 //
526 // If such a node exists, install it as the root of TREE and return 0.
527 // Otherwise arbitrarily choose between:
528 //
529 // (1) Installing the closest preceding node as the root and returning 1.
530 // (2) Installing the closest following node as the root and returning -1.
531 //
532 // Note that this routine should not be used to check whether INSN
533 // itself defines a resource; that can be checked more cheaply using
534 // find_access_index.
535 int
536 rtl_ssa::lookup_def (def_splay_tree &tree, insn_info *insn)
537 {
538 auto go_left = [&](def_node *node)
539 {
540 return *insn < *first_def (node)->insn ();
541 };
542 auto go_right = [&](def_node *node)
543 {
544 return *insn > *last_def (node)->insn ();
545 };
546 return tree.lookup (go_left, go_right);
547 }
548
549 // Search TREE for a clobber in INSN. If such a clobber exists, install
550 // it as the root of TREE and return 0. Otherwise arbitrarily choose between:
551 //
552 // (1) Installing the closest preceding clobber as the root and returning 1.
553 // (2) Installing the closest following clobber as the root and returning -1.
554 int
555 rtl_ssa::lookup_clobber (clobber_tree &tree, insn_info *insn)
556 {
557 auto compare = [&](clobber_info *clobber)
558 {
559 return insn->compare_with (clobber->insn ());
560 };
561 return tree.lookup (compare);
562 }
563
564 // Search for a definition of RESOURCE at INSN and return the result of
565 // the search as a def_lookup. See the comment above the class for more
566 // details.
567 def_lookup
568 function_info::find_def (resource_info resource, insn_info *insn)
569 {
570 def_info *first = m_defs[resource.regno + 1];
571 if (!first)
572 // There are no nodes. The comparison result is pretty meaningless
573 // in this case.
574 return { nullptr, -1 };
575
576 // See whether the first node matches.
577 auto first_result = clobber_group_or_single_def (first);
578 if (*insn <= *last_def (first_result)->insn ())
579 {
580 int comparison = (*insn >= *first->insn () ? 0 : -1);
581 return { first_result, comparison };
582 }
583
584 // See whether the last node matches.
585 def_info *last = first->last_def ();
586 auto last_result = clobber_group_or_single_def (last);
587 if (*insn >= *first_def (last_result)->insn ())
588 {
589 int comparison = (*insn <= *last->insn () ? 0 : 1);
590 return { last_result, comparison };
591 }
592
593 // Resort to using a splay tree to search for the result.
594 def_splay_tree tree = need_def_splay_tree (last);
595 int comparison = lookup_def (tree, insn);
596 last->set_splay_root (tree.root ());
597 return { tree.root (), comparison };
598 }
599
600 // Add DEF to the function's list of definitions of DEF->resource (),
601 // inserting DEF immediately before BEFORE. DEF is not currently in the list.
602 void
603 function_info::insert_def_before (def_info *def, def_info *before)
604 {
605 gcc_checking_assert (!def->has_def_links ()
606 && *before->insn () > *def->insn ());
607
608 def->copy_prev_from (before);
609 if (def_info *prev = def->prev_def ())
610 {
611 gcc_checking_assert (*prev->insn () < *def->insn ());
612 prev->set_next_def (def);
613 }
614 else
615 m_defs[def->regno () + 1] = def;
616
617 def->set_next_def (before);
618 before->set_prev_def (def);
619 }
620
621 // Add DEF to the function's list of definitions of DEF->resource (),
622 // inserting DEF immediately after AFTER. DEF is not currently in the list.
623 void
624 function_info::insert_def_after (def_info *def, def_info *after)
625 {
626 gcc_checking_assert (!def->has_def_links ()
627 && *after->insn () < *def->insn ());
628
629 def->copy_next_from (after);
630 if (def_info *next = def->next_def ())
631 {
632 gcc_checking_assert (*next->insn () > *def->insn ());
633 next->set_prev_def (def);
634 }
635 else
636 m_defs[def->regno () + 1]->set_last_def (def);
637
638 def->set_prev_def (after);
639 after->set_next_def (def);
640 }
641
642 // Remove DEF from the function's list of definitions of DEF->resource ().
643 void
644 function_info::remove_def_from_list (def_info *def)
645 {
646 def_info *prev = def->prev_def ();
647 def_info *next = def->next_def ();
648
649 if (next)
650 next->copy_prev_from (def);
651 else
652 m_defs[def->regno () + 1]->set_last_def (prev);
653
654 if (prev)
655 prev->copy_next_from (def);
656 else
657 m_defs[def->regno () + 1] = next;
658
659 def->clear_def_links ();
660 }
661
662 // Add CLOBBER to GROUP and insert it into the function's list of
663 // accesses to CLOBBER->resource (). CLOBBER is not currently part
664 // of an active group and is not currently in the list.
665 void
666 function_info::add_clobber (clobber_info *clobber, clobber_group *group)
667 {
668 // Search for either the previous or next clobber in the group.
669 // The result is less than zero if CLOBBER should come before NEIGHBOR
670 // or greater than zero if CLOBBER should come after NEIGHBOR.
671 int comparison = group->lookup_clobber (clobber->insn ());
672 gcc_checking_assert (comparison != 0);
673 clobber_info *neighbor = group->m_clobber_tree.root ();
674
675 // If CLOBBER comes before NEIGHBOR, insert CLOBBER to NEIGHBOR's left,
676 // otherwise insert CLOBBER to NEIGHBOR's right.
677 clobber_info::splay_tree::insert_child (neighbor, comparison > 0, clobber);
678 clobber->set_group (group);
679
680 // Insert the clobber into the function-wide list and update the
681 // bounds of the group.
682 if (comparison > 0)
683 {
684 insert_def_after (clobber, neighbor);
685 if (neighbor == group->last_clobber ())
686 group->set_last_clobber (clobber);
687 }
688 else
689 {
690 insert_def_before (clobber, neighbor);
691 if (neighbor == group->first_clobber ())
692 group->set_first_clobber (clobber);
693 }
694 }
695
696 // Remove CLOBBER from GROUP, given that GROUP contains other clobbers too.
697 // Also remove CLOBBER from the function's list of accesses to
698 // CLOBBER->resource ().
699 void
700 function_info::remove_clobber (clobber_info *clobber, clobber_group *group)
701 {
702 if (clobber == group->first_clobber ())
703 {
704 auto *new_first = as_a<clobber_info *> (clobber->next_def ());
705 group->set_first_clobber (new_first);
706 new_first->update_group (group);
707 }
708 else if (clobber == group->last_clobber ())
709 {
710 auto *new_last = as_a<clobber_info *> (clobber->prev_def ());
711 group->set_last_clobber (new_last);
712 new_last->update_group (group);
713 }
714
715 clobber_info *replacement = clobber_info::splay_tree::remove_node (clobber);
716 if (clobber == group->m_clobber_tree.root ())
717 {
718 group->m_clobber_tree = replacement;
719 replacement->update_group (group);
720 }
721 clobber->set_group (nullptr);
722
723 remove_def_from_list (clobber);
724 }
725
726 // Add CLOBBER immediately before the first clobber in GROUP, given that
727 // CLOBBER is not currently part of any group.
728 void
729 function_info::prepend_clobber_to_group (clobber_info *clobber,
730 clobber_group *group)
731 {
732 clobber_info *next = group->first_clobber ();
733 clobber_info::splay_tree::insert_child (next, 0, clobber);
734 group->set_first_clobber (clobber);
735 clobber->set_group (group);
736 }
737
738 // Add CLOBBER immediately after the last clobber in GROUP, given that
739 // CLOBBER is not currently part of any group.
740 void
741 function_info::append_clobber_to_group (clobber_info *clobber,
742 clobber_group *group)
743 {
744 clobber_info *prev = group->last_clobber ();
745 clobber_info::splay_tree::insert_child (prev, 1, clobber);
746 group->set_last_clobber (clobber);
747 clobber->set_group (group);
748 }
749
750 // Put CLOBBER1 and CLOBBER2 into the same clobber_group, given that
751 // CLOBBER1 occurs immediately before CLOBBER2 and that the two clobbers
752 // are not currently in the same group. LAST is the last definition of
753 // the associated resource, and is where any splay tree is stored.
754 void
755 function_info::merge_clobber_groups (clobber_info *clobber1,
756 clobber_info *clobber2,
757 def_info *last)
758 {
759 if (clobber1->is_in_group () && clobber2->is_in_group ())
760 {
761 clobber_group *group1 = clobber1->group ();
762 clobber_group *group2 = clobber2->group ();
763 gcc_checking_assert (clobber1 == group1->last_clobber ()
764 && clobber2 == group2->first_clobber ());
765
766 if (def_splay_tree tree = last->splay_root ())
767 {
768 // Remove GROUP2 from the splay tree.
769 int comparison = lookup_def (tree, clobber2->insn ());
770 gcc_checking_assert (comparison == 0);
771 tree.remove_root ();
772 last->set_splay_root (tree.root ());
773 }
774
775 // Splice the trees together.
776 group1->m_clobber_tree.splice_next_tree (group2->m_clobber_tree);
777
778 // Bring the two extremes of GROUP2 under GROUP1. Any other
779 // clobbers in the group are updated lazily on demand.
780 clobber2->set_group (group1);
781 group2->last_clobber ()->set_group (group1);
782 group1->set_last_clobber (group2->last_clobber ());
783
784 // Record that GROUP2 is no more.
785 group2->set_first_clobber (nullptr);
786 group2->set_last_clobber (nullptr);
787 group2->m_clobber_tree = nullptr;
788 }
789 else
790 {
791 // In this case there can be no active splay tree.
792 gcc_assert (!last->splay_root ());
793 if (clobber2->is_in_group ())
794 prepend_clobber_to_group (clobber1, clobber2->group ());
795 else
796 append_clobber_to_group (clobber2, need_clobber_group (clobber1));
797 }
798 }
799
800 // GROUP spans INSN, and INSN now sets the resource that GROUP clobbers.
801 // Split GROUP around INSN, to form two new groups. The first of the
802 // returned groups comes before INSN and the second comes after INSN.
803 //
804 // The caller is responsible for updating the def_splay_tree and chaining
805 // the defs together.
806 std::array<clobber_group *, 2>
807 function_info::split_clobber_group (clobber_group *group, insn_info *insn)
808 {
809 // Search for either the previous or next clobber in the group.
810 // The result is less than zero if CLOBBER should come before NEIGHBOR
811 // or greater than zero if CLOBBER should come after NEIGHBOR.
812 clobber_tree &tree1 = group->m_clobber_tree;
813 int comparison = lookup_clobber (tree1, insn);
814 gcc_checking_assert (comparison != 0);
815 clobber_info *neighbor = tree1.root ();
816
817 clobber_tree tree2;
818 clobber_info *prev;
819 clobber_info *next;
820 if (comparison > 0)
821 {
822 // NEIGHBOR is the last clobber in what will become the first group.
823 tree2 = tree1.split_after_root ();
824 prev = neighbor;
825 next = as_a<clobber_info *> (prev->next_def ());
826 }
827 else
828 {
829 // NEIGHBOR is the first clobber in what will become the second group.
830 tree2 = neighbor;
831 tree1 = tree2.split_before_root ();
832 next = neighbor;
833 prev = as_a<clobber_info *> (next->prev_def ());
834 }
835
836 // Create a new group for each side of the split. We need to invalidate
837 // the old group so that clobber_info::group can tell whether a lazy
838 // update is needed.
839 clobber_info *first_clobber = group->first_clobber ();
840 clobber_info *last_clobber = group->last_clobber ();
841 auto *group1 = allocate<clobber_group> (first_clobber, prev, tree1.root ());
842 auto *group2 = allocate<clobber_group> (next, last_clobber, tree2.root ());
843
844 // Invalidate the old group.
845 group->set_last_clobber (nullptr);
846
847 return { group1, group2 };
848 }
849
850 // Add DEF to the end of the function's list of definitions of
851 // DEF->resource (). There is known to be no associated splay tree yet.
852 void
853 function_info::append_def (def_info *def)
854 {
855 gcc_checking_assert (!def->has_def_links ());
856 def_info **head = &m_defs[def->regno () + 1];
857 def_info *first = *head;
858 if (!first)
859 {
860 // This is the only definition of the resource.
861 def->set_last_def (def);
862 *head = def;
863 return;
864 }
865
866 def_info *prev = first->last_def ();
867 gcc_checking_assert (!prev->splay_root ());
868
869 // Maintain the invariant that two clobbers must not appear in
870 // neighboring nodes of the splay tree.
871 auto *clobber = dyn_cast<clobber_info *> (def);
872 auto *prev_clobber = dyn_cast<clobber_info *> (prev);
873 if (clobber && prev_clobber)
874 append_clobber_to_group (clobber, need_clobber_group (prev_clobber));
875
876 prev->set_next_def (def);
877 def->set_prev_def (prev);
878 first->set_last_def (def);
879 }
880
881 // Add DEF to the function's list of definitions of DEF->resource ().
882 // Also insert it into the associated splay tree, if there is one.
883 // DEF is not currently part of the list and is not in the splay tree.
884 void
885 function_info::add_def (def_info *def)
886 {
887 gcc_checking_assert (!def->has_def_links ()
888 && !def->m_is_temp
889 && !def->m_has_been_superceded);
890 def_info **head = &m_defs[def->regno () + 1];
891 def_info *first = *head;
892 if (!first)
893 {
894 // This is the only definition of the resource.
895 def->set_last_def (def);
896 *head = def;
897 return;
898 }
899
900 def_info *last = first->last_def ();
901 insn_info *insn = def->insn ();
902
903 int comparison;
904 def_node *neighbor = nullptr;
905 def_info *prev = nullptr;
906 def_info *next = nullptr;
907 if (*insn > *last->insn ())
908 {
909 // This definition comes after all other definitions.
910 comparison = 1;
911 if (def_splay_tree tree = last->splay_root ())
912 {
913 tree.splay_max_node ();
914 last->set_splay_root (tree.root ());
915 neighbor = tree.root ();
916 }
917 prev = last;
918 }
919 else if (*insn < *first->insn ())
920 {
921 // This definition comes before all other definitions.
922 comparison = -1;
923 if (def_splay_tree tree = last->splay_root ())
924 {
925 tree.splay_min_node ();
926 last->set_splay_root (tree.root ());
927 neighbor = tree.root ();
928 }
929 next = first;
930 }
931 else
932 {
933 // Search the splay tree for an insertion point.
934 def_splay_tree tree = need_def_splay_tree (last);
935 comparison = lookup_def (tree, insn);
936 last->set_splay_root (tree.root ());
937 neighbor = tree.root ();
938
939 // Deal with cases in which we found an overlapping live range.
940 if (comparison == 0)
941 {
942 auto *group = as_a<clobber_group *> (tree.root ());
943 if (auto *clobber = dyn_cast<clobber_info *> (def))
944 {
945 add_clobber (clobber, group);
946 return;
947 }
948 auto new_groups = split_clobber_group (group, insn);
949
950 // Insert the two new groups immediately after GROUP.
951 def_splay_tree::insert_child (group, 1, new_groups[1]);
952 def_splay_tree::insert_child (group, 1, new_groups[0]);
953
954 // Remove GROUP.
955 tree.remove_root ();
956 last->set_splay_root (tree.root ());
957
958 prev = new_groups[0]->last_clobber ();
959 next = new_groups[1]->first_clobber ();
960
961 // DEF comes after the first group. (new_groups[1] and -1 would
962 // also work.)
963 neighbor = new_groups[0];
964 comparison = 1;
965 }
966 // COMPARISON is < 0 if DEF comes before NEIGHBOR or > 0 if DEF comes
967 // after NEIGHBOR.
968 else if (comparison < 0)
969 {
970 next = first_def (neighbor);
971 prev = next->prev_def ();
972 }
973 else
974 {
975 prev = last_def (neighbor);
976 next = prev->next_def ();
977 }
978 }
979
980 // See if we should merge CLOBBER with a neighboring clobber.
981 auto *clobber = dyn_cast<clobber_info *> (def);
982 auto *prev_clobber = safe_dyn_cast<clobber_info *> (prev);
983 auto *next_clobber = safe_dyn_cast<clobber_info *> (next);
984 // We shouldn't have consecutive clobber_groups.
985 gcc_checking_assert (!(clobber && prev_clobber && next_clobber));
986 if (clobber && prev_clobber)
987 append_clobber_to_group (clobber, need_clobber_group (prev_clobber));
988 else if (clobber && next_clobber)
989 prepend_clobber_to_group (clobber, need_clobber_group (next_clobber));
990 else if (neighbor)
991 {
992 // If DEF comes before NEIGHBOR, insert DEF to NEIGHBOR's left,
993 // otherwise insert DEF to NEIGHBOR's right.
994 def_node *node = need_def_node (def);
995 def_splay_tree::insert_child (neighbor, comparison >= 0, node);
996 }
997 if (prev)
998 insert_def_after (def, prev);
999 else
1000 insert_def_before (def, next);
1001 }
1002
1003 // Remove DEF from the function's list of definitions of DEF->resource ().
1004 // Also remove DEF from the associated splay tree, if there is one.
1005 void
1006 function_info::remove_def (def_info *def)
1007 {
1008 def_info **head = &m_defs[def->regno () + 1];
1009 def_info *first = *head;
1010 gcc_checking_assert (first);
1011 if (first->is_last_def ())
1012 {
1013 // DEF is the only definition of the resource.
1014 gcc_checking_assert (first == def);
1015 *head = nullptr;
1016 def->clear_def_links ();
1017 return;
1018 }
1019
1020 // If CLOBBER belongs to a clobber_group that contains other clobbers
1021 // too, then we need to update the clobber_group and the list, but any
1022 // splay tree that contains the clobber_group is unaffected.
1023 if (auto *clobber = dyn_cast<clobber_info *> (def))
1024 if (clobber->is_in_group ())
1025 {
1026 clobber_group *group = clobber->group ();
1027 if (group->first_clobber () != group->last_clobber ())
1028 {
1029 remove_clobber (clobber, group);
1030 return;
1031 }
1032 }
1033
1034 // If we've created a splay tree for this resource, remove the entry
1035 // for DEF.
1036 def_info *last = first->last_def ();
1037 if (def_splay_tree tree = last->splay_root ())
1038 {
1039 int comparison = lookup_def (tree, def->insn ());
1040 gcc_checking_assert (comparison == 0);
1041 tree.remove_root ();
1042 last->set_splay_root (tree.root ());
1043 }
1044
1045 // If the definition came between two clobbers, merge them into a single
1046 // group.
1047 auto *prev_clobber = safe_dyn_cast<clobber_info *> (def->prev_def ());
1048 auto *next_clobber = safe_dyn_cast<clobber_info *> (def->next_def ());
1049 if (prev_clobber && next_clobber)
1050 merge_clobber_groups (prev_clobber, next_clobber, last);
1051
1052 remove_def_from_list (def);
1053 }
1054
1055 // Require DEF to have a splay tree that contains all non-phi uses.
1056 void
1057 function_info::need_use_splay_tree (set_info *def)
1058 {
1059 if (!def->m_use_tree)
1060 for (use_info *use : def->all_insn_uses ())
1061 {
1062 auto *use_node = allocate<splay_tree_node<use_info *>> (use);
1063 def->m_use_tree.insert_max_node (use_node);
1064 }
1065 }
1066
1067 // Compare two instructions by their position in a use splay tree. Return >0
1068 // if INSN1 comes after INSN2, <0 if INSN1 comes before INSN2, or 0 if they are
1069 // the same instruction.
1070 static inline int
1071 compare_use_insns (insn_info *insn1, insn_info *insn2)
1072 {
1073 // Debug instructions go after nondebug instructions.
1074 int diff = insn1->is_debug_insn () - insn2->is_debug_insn ();
1075 if (diff != 0)
1076 return diff;
1077 return insn1->compare_with (insn2);
1078 }
1079
1080 // Search TREE for a use in INSN. If such a use exists, install it as
1081 // the root of TREE and return 0. Otherwise arbitrarily choose between:
1082 //
1083 // (1) Installing the closest preceding use as the root and returning 1.
1084 // (2) Installing the closest following use as the root and returning -1.
1085 int
1086 rtl_ssa::lookup_use (splay_tree<use_info *> &tree, insn_info *insn)
1087 {
1088 auto compare = [&](splay_tree_node<use_info *> *node)
1089 {
1090 return compare_use_insns (insn, node->value ()->insn ());
1091 };
1092 return tree.lookup (compare);
1093 }
1094
1095 // Add USE to USE->def ()'s list of uses. inserting USE immediately before
1096 // BEFORE. USE is not currently in the list.
1097 //
1098 // This routine should not be used for inserting phi uses.
1099 void
1100 function_info::insert_use_before (use_info *use, use_info *before)
1101 {
1102 gcc_checking_assert (!use->has_use_links () && use->is_in_any_insn ());
1103
1104 set_info *def = use->def ();
1105
1106 use->copy_prev_from (before);
1107 use->set_next_use (before);
1108
1109 if (use_info *prev = use->prev_use ())
1110 prev->set_next_use (use);
1111 else
1112 use->def ()->set_first_use (use);
1113
1114 before->set_prev_use (use);
1115 if (use->is_in_nondebug_insn () && before->is_in_debug_insn_or_phi ())
1116 def->last_use ()->set_last_nondebug_insn_use (use);
1117
1118 gcc_checking_assert (use->check_integrity () && before->check_integrity ());
1119 }
1120
1121 // Add USE to USE->def ()'s list of uses. inserting USE immediately after
1122 // AFTER. USE is not currently in the list.
1123 //
1124 // This routine should not be used for inserting phi uses.
1125 void
1126 function_info::insert_use_after (use_info *use, use_info *after)
1127 {
1128 set_info *def = use->def ();
1129 gcc_checking_assert (after->is_in_any_insn ()
1130 && !use->has_use_links ()
1131 && use->is_in_any_insn ());
1132
1133 use->set_prev_use (after);
1134 use->copy_next_from (after);
1135
1136 after->set_next_use (use);
1137
1138 if (use_info *next = use->next_use ())
1139 {
1140 // The last node doesn't change, but we might need to update its
1141 // last_nondebug_insn_use record.
1142 if (use->is_in_nondebug_insn () && next->is_in_debug_insn_or_phi ())
1143 def->last_use ()->set_last_nondebug_insn_use (use);
1144 next->set_prev_use (use);
1145 }
1146 else
1147 {
1148 // USE is now the last node.
1149 if (use->is_in_nondebug_insn ())
1150 use->set_last_nondebug_insn_use (use);
1151 def->first_use ()->set_last_use (use);
1152 }
1153
1154 gcc_checking_assert (use->check_integrity () && after->check_integrity ());
1155 }
1156
1157 // If USE has a known definition, add USE to that definition's list of uses.
1158 // Also update the associated splay tree, if any.
1159 void
1160 function_info::add_use (use_info *use)
1161 {
1162 gcc_checking_assert (!use->has_use_links ()
1163 && !use->m_is_temp
1164 && !use->m_has_been_superceded);
1165
1166 set_info *def = use->def ();
1167 if (!def)
1168 return;
1169
1170 use_info *first = def->first_use ();
1171 if (!first)
1172 {
1173 // This is the only use of the definition.
1174 use->set_last_use (use);
1175 if (use->is_in_nondebug_insn ())
1176 use->set_last_nondebug_insn_use (use);
1177
1178 def->set_first_use (use);
1179
1180 gcc_checking_assert (use->check_integrity ());
1181 return;
1182 }
1183
1184 if (use->is_in_phi ())
1185 {
1186 // Add USE at the end of the list, as the new first phi.
1187 use_info *last = first->last_use ();
1188
1189 use->set_prev_use (last);
1190 use->copy_next_from (last);
1191
1192 last->set_next_use (use);
1193 first->set_last_use (use);
1194
1195 gcc_checking_assert (use->check_integrity ());
1196 return;
1197 }
1198
1199 // If there is currently no splay tree for this definition, see if can
1200 // get away with a pure list-based update.
1201 insn_info *insn = use->insn ();
1202 auto quick_path = [&]()
1203 {
1204 // Check if USE should come before all current uses.
1205 if (first->is_in_phi () || compare_use_insns (insn, first->insn ()) < 0)
1206 {
1207 insert_use_before (use, first);
1208 return true;
1209 }
1210
1211 // Check if USE should come after all current uses in the same
1212 // subsequence (i.e. the list of nondebug insn uses or the list
1213 // of debug insn uses).
1214 use_info *last = first->last_use ();
1215 if (use->is_in_debug_insn ())
1216 {
1217 if (last->is_in_phi ())
1218 return false;
1219 }
1220 else
1221 last = last->last_nondebug_insn_use ();
1222
1223 if (compare_use_insns (insn, last->insn ()) > 0)
1224 {
1225 insert_use_after (use, last);
1226 return true;
1227 }
1228
1229 return false;
1230 };
1231 if (!def->m_use_tree && quick_path ())
1232 return;
1233
1234 // Search the splay tree for an insertion point. COMPARISON is less
1235 // than zero if USE should come before NEIGHBOR, or greater than zero
1236 // if USE should come after NEIGHBOR.
1237 need_use_splay_tree (def);
1238 int comparison = lookup_use (def->m_use_tree, insn);
1239 gcc_checking_assert (comparison != 0);
1240 use_info *neighbor = def->m_use_tree.root ()->value ();
1241
1242 // If USE comes before NEIGHBOR, insert USE to NEIGHBOR's left,
1243 // otherwise insert USE to NEIGHBOR's right.
1244 auto *use_node = allocate<splay_tree_node<use_info *>> (use);
1245 def->m_use_tree.insert_relative (comparison, use_node);
1246 if (comparison > 0)
1247 insert_use_after (use, neighbor);
1248 else
1249 insert_use_before (use, neighbor);
1250 }
1251
1252 void
1253 function_info::reparent_use (use_info *use, set_info *new_def)
1254 {
1255 remove_use (use);
1256 use->set_def (new_def);
1257 add_use (use);
1258 }
1259
1260 // If USE has a known definition, remove USE from that definition's list
1261 // of uses. Also remove if it from the associated splay tree, if any.
1262 void
1263 function_info::remove_use (use_info *use)
1264 {
1265 set_info *def = use->def ();
1266 if (!def)
1267 return;
1268
1269 // Remove USE from the splay tree.
1270 if (def->m_use_tree && use->is_in_any_insn ())
1271 {
1272 int comparison = lookup_use (def->m_use_tree, use->insn ());
1273 gcc_checking_assert (comparison == 0);
1274 def->m_use_tree.remove_root ();
1275 }
1276
1277 use_info *prev = use->prev_use ();
1278 use_info *next = use->next_use ();
1279
1280 use_info *first = def->first_use ();
1281 use_info *last = first->last_use ();
1282 if (last->last_nondebug_insn_use () == use)
1283 last->set_last_nondebug_insn_use (prev);
1284
1285 if (next)
1286 next->copy_prev_from (use);
1287 else
1288 first->set_last_use (prev);
1289
1290 if (prev)
1291 prev->copy_next_from (use);
1292 else
1293 def->set_first_use (next);
1294
1295 use->clear_use_links ();
1296 gcc_checking_assert ((!prev || prev->check_integrity ())
1297 && (!next || next->check_integrity ()));
1298 }
1299
1300 // Allocate a temporary clobber_info for register REGNO in insn INSN,
1301 // including it in the region of the obstack governed by WATERMARK.
1302 // Return a new def_array that contains OLD_DEFS and the new clobber.
1303 //
1304 // OLD_DEFS is known not to define REGNO.
1305 def_array
1306 function_info::insert_temp_clobber (obstack_watermark &watermark,
1307 insn_info *insn, unsigned int regno,
1308 def_array old_defs)
1309 {
1310 gcc_checking_assert (watermark == &m_temp_obstack);
1311 auto *clobber = allocate_temp<clobber_info> (insn, regno);
1312 clobber->m_is_temp = true;
1313 return insert_access (watermark, clobber, old_defs);
1314 }
1315
1316 // See the comment above the declaration.
1317 bool
1318 function_info::remains_available_at_insn (const set_info *set,
1319 insn_info *insn)
1320 {
1321 auto *ebb = set->ebb ();
1322 gcc_checking_assert (ebb == insn->ebb ());
1323
1324 def_info *next_def = set->next_def ();
1325 if (next_def && *next_def->insn () < *insn)
1326 return false;
1327
1328 if (HARD_REGISTER_NUM_P (set->regno ())
1329 && TEST_HARD_REG_BIT (m_clobbered_by_calls, set->regno ()))
1330 for (ebb_call_clobbers_info *call_group : ebb->call_clobbers ())
1331 {
1332 if (!call_group->clobbers (set->resource ()))
1333 continue;
1334
1335 insn_info *call_insn = next_call_clobbers (*call_group, insn);
1336 if (call_insn && *call_insn < *insn)
1337 return false;
1338 }
1339
1340 return true;
1341 }
1342
1343 // See the comment above the declaration.
1344 bool
1345 function_info::remains_available_on_exit (const set_info *set, bb_info *bb)
1346 {
1347 if (HARD_REGISTER_NUM_P (set->regno ())
1348 && TEST_HARD_REG_BIT (m_clobbered_by_calls, set->regno ()))
1349 {
1350 insn_info *search_insn = (set->bb () == bb
1351 ? set->insn ()
1352 : bb->head_insn ());
1353 for (ebb_call_clobbers_info *call_group : bb->ebb ()->call_clobbers ())
1354 {
1355 if (!call_group->clobbers (set->resource ()))
1356 continue;
1357
1358 insn_info *insn = next_call_clobbers (*call_group, search_insn);
1359 if (insn && insn->bb () == bb)
1360 return false;
1361 }
1362 }
1363
1364 return (set->is_last_def ()
1365 || *set->next_def ()->insn () > *bb->end_insn ());
1366 }
1367
1368 // A subroutine of make_uses_available. Try to make USE's definition
1369 // available at the head of BB. WILL_BE_DEBUG_USE is true if the
1370 // definition will be used only in debug instructions.
1371 //
1372 // On success:
1373 //
1374 // - If the use would have the same def () as USE, return USE.
1375 //
1376 // - If BB already has a degenerate phi for the same definition,
1377 // return a temporary use of that phi.
1378 //
1379 // - Otherwise, the use would need a new degenerate phi. Allocate a
1380 // temporary phi and return a temporary use of it.
1381 //
1382 // Return null on failure.
1383 use_info *
1384 function_info::make_use_available (use_info *use, bb_info *bb,
1385 bool will_be_debug_use)
1386 {
1387 set_info *def = use->def ();
1388 if (!def)
1389 return use;
1390
1391 if (is_single_dominating_def (def))
1392 return use;
1393
1394 if (def->ebb () == bb->ebb ())
1395 {
1396 if (remains_available_at_insn (def, bb->head_insn ()))
1397 return use;
1398 return nullptr;
1399 }
1400
1401 basic_block cfg_bb = bb->cfg_bb ();
1402 bb_info *use_bb = use->bb ();
1403 if (single_pred_p (cfg_bb)
1404 && single_pred (cfg_bb) == use_bb->cfg_bb ()
1405 && remains_available_on_exit (def, use_bb))
1406 {
1407 if (will_be_debug_use)
1408 return use;
1409
1410 resource_info resource = use->resource ();
1411 set_info *ultimate_def = look_through_degenerate_phi (def);
1412
1413 // See if there is already a (degenerate) phi for DEF.
1414 insn_info *phi_insn = bb->ebb ()->phi_insn ();
1415 phi_info *phi;
1416 def_lookup dl = find_def (resource, phi_insn);
1417 if (set_info *set = dl.matching_set ())
1418 {
1419 // There is an existing phi.
1420 phi = as_a<phi_info *> (set);
1421 gcc_checking_assert (phi->input_value (0) == ultimate_def);
1422 }
1423 else
1424 {
1425 // Create a temporary placeholder phi. This will become
1426 // permanent if the change is later committed.
1427 phi = allocate_temp<phi_info> (phi_insn, resource, 0);
1428 auto *input = allocate_temp<use_info> (phi, resource, ultimate_def);
1429 input->m_is_temp = true;
1430 phi->m_is_temp = true;
1431 phi->make_degenerate (input);
1432 if (def_info *prev = dl.prev_def (phi_insn))
1433 phi->set_prev_def (prev);
1434 if (def_info *next = dl.next_def (phi_insn))
1435 phi->set_next_def (next);
1436 }
1437
1438 // Create a temporary use of the phi at the head of the first
1439 // block, since we know for sure that it's available there.
1440 insn_info *use_insn = bb->ebb ()->first_bb ()->head_insn ();
1441 auto *new_use = allocate_temp<use_info> (use_insn, resource, phi);
1442 new_use->m_is_temp = true;
1443 return new_use;
1444 }
1445 return nullptr;
1446 }
1447
1448 // See the comment above the declaration.
1449 use_array
1450 function_info::make_uses_available (obstack_watermark &watermark,
1451 use_array uses, bb_info *bb,
1452 bool will_be_debug_uses)
1453 {
1454 unsigned int num_uses = uses.size ();
1455 if (num_uses == 0)
1456 return uses;
1457
1458 auto **new_uses = XOBNEWVEC (watermark, access_info *, num_uses);
1459 for (unsigned int i = 0; i < num_uses; ++i)
1460 {
1461 use_info *use = make_use_available (uses[i], bb, will_be_debug_uses);
1462 if (!use)
1463 return use_array (access_array::invalid ());
1464 new_uses[i] = use;
1465 }
1466 return use_array (new_uses, num_uses);
1467 }
1468
1469 set_info *
1470 function_info::create_set (obstack_watermark &watermark,
1471 insn_info *insn,
1472 resource_info resource)
1473 {
1474 auto set = change_alloc<set_info> (watermark, insn, resource);
1475 set->m_is_temp = true;
1476 return set;
1477 }
1478
1479 use_info *
1480 function_info::create_use (obstack_watermark &watermark,
1481 insn_info *insn,
1482 set_info *set)
1483 {
1484 auto use = change_alloc<use_info> (watermark, insn, set->resource (), set);
1485 use->m_is_temp = true;
1486 return use;
1487 }
1488
1489 // Return true if ACCESS1 can represent ACCESS2 and if ACCESS2 can
1490 // represent ACCESS1.
1491 static bool
1492 can_merge_accesses (access_info *access1, access_info *access2)
1493 {
1494 if (access1 == access2)
1495 return true;
1496
1497 auto *use1 = dyn_cast<use_info *> (access1);
1498 auto *use2 = dyn_cast<use_info *> (access2);
1499 return use1 && use2 && use1->def () == use2->def ();
1500 }
1501
1502 // See the comment above the declaration.
1503 access_array
1504 rtl_ssa::merge_access_arrays_base (obstack_watermark &watermark,
1505 access_array accesses1,
1506 access_array accesses2)
1507 {
1508 if (accesses1.empty ())
1509 return accesses2;
1510 if (accesses2.empty ())
1511 return accesses1;
1512
1513 auto i1 = accesses1.begin ();
1514 auto end1 = accesses1.end ();
1515 auto i2 = accesses2.begin ();
1516 auto end2 = accesses2.end ();
1517
1518 access_array_builder builder (watermark);
1519 builder.reserve (accesses1.size () + accesses2.size ());
1520
1521 while (i1 != end1 && i2 != end2)
1522 {
1523 access_info *access1 = *i1;
1524 access_info *access2 = *i2;
1525
1526 unsigned int regno1 = access1->regno ();
1527 unsigned int regno2 = access2->regno ();
1528 if (regno1 == regno2)
1529 {
1530 if (!can_merge_accesses (access1, access2))
1531 return access_array::invalid ();
1532
1533 builder.quick_push (access1);
1534 ++i1;
1535 ++i2;
1536 }
1537 else if (regno1 < regno2)
1538 {
1539 builder.quick_push (access1);
1540 ++i1;
1541 }
1542 else
1543 {
1544 builder.quick_push (access2);
1545 ++i2;
1546 }
1547 }
1548 for (; i1 != end1; ++i1)
1549 builder.quick_push (*i1);
1550 for (; i2 != end2; ++i2)
1551 builder.quick_push (*i2);
1552
1553 return builder.finish ();
1554 }
1555
1556 // See the comment above the declaration.
1557 access_array
1558 rtl_ssa::insert_access_base (obstack_watermark &watermark,
1559 access_info *access1, access_array accesses2)
1560 {
1561 access_array_builder builder (watermark);
1562 builder.reserve (1 + accesses2.size ());
1563
1564 unsigned int regno1 = access1->regno ();
1565 auto i2 = accesses2.begin ();
1566 auto end2 = accesses2.end ();
1567 while (i2 != end2)
1568 {
1569 access_info *access2 = *i2;
1570
1571 unsigned int regno2 = access2->regno ();
1572 if (regno1 == regno2)
1573 {
1574 if (!can_merge_accesses (access1, access2))
1575 return access_array::invalid ();
1576
1577 builder.quick_push (access1);
1578 access1 = nullptr;
1579 ++i2;
1580 break;
1581 }
1582 else if (regno1 < regno2)
1583 {
1584 builder.quick_push (access1);
1585 access1 = nullptr;
1586 break;
1587 }
1588 else
1589 {
1590 builder.quick_push (access2);
1591 ++i2;
1592 }
1593 }
1594 if (access1)
1595 builder.quick_push (access1);
1596 for (; i2 != end2; ++i2)
1597 builder.quick_push (*i2);
1598
1599 return builder.finish ();
1600 }
1601
1602 // See the comment above the declaration.
1603 use_array
1604 rtl_ssa::remove_uses_of_def (obstack_watermark &watermark, use_array uses,
1605 def_info *def)
1606 {
1607 access_array_builder uses_builder (watermark);
1608 uses_builder.reserve (uses.size ());
1609 for (use_info *use : uses)
1610 if (use->def () != def)
1611 uses_builder.quick_push (use);
1612 return use_array (uses_builder.finish ());
1613 }
1614
1615 // See the comment above the declaration.
1616 access_array
1617 rtl_ssa::remove_note_accesses_base (obstack_watermark &watermark,
1618 access_array accesses)
1619 {
1620 auto predicate = [](access_info *a) {
1621 return !a->only_occurs_in_notes ();
1622 };
1623
1624 for (access_info *access : accesses)
1625 if (access->only_occurs_in_notes ())
1626 return filter_accesses (watermark, accesses, predicate);
1627
1628 return accesses;
1629 }
1630
1631 // See the comment above the declaration.
1632 bool
1633 rtl_ssa::accesses_reference_same_resource (access_array accesses1,
1634 access_array accesses2)
1635 {
1636 auto i1 = accesses1.begin ();
1637 auto end1 = accesses1.end ();
1638 auto i2 = accesses2.begin ();
1639 auto end2 = accesses2.end ();
1640
1641 while (i1 != end1 && i2 != end2)
1642 {
1643 access_info *access1 = *i1;
1644 access_info *access2 = *i2;
1645
1646 unsigned int regno1 = access1->regno ();
1647 unsigned int regno2 = access2->regno ();
1648 if (regno1 == regno2)
1649 return true;
1650
1651 if (regno1 < regno2)
1652 ++i1;
1653 else
1654 ++i2;
1655 }
1656 return false;
1657 }
1658
1659 // See the comment above the declaration.
1660 bool
1661 rtl_ssa::insn_clobbers_resources (insn_info *insn, access_array accesses)
1662 {
1663 if (accesses_reference_same_resource (insn->defs (), accesses))
1664 return true;
1665
1666 if (insn->is_call () && accesses_include_hard_registers (accesses))
1667 {
1668 function_abi abi = insn_callee_abi (insn->rtl ());
1669 for (const access_info *access : accesses)
1670 {
1671 if (!HARD_REGISTER_NUM_P (access->regno ()))
1672 break;
1673 if (abi.clobbers_reg_p (access->mode (), access->regno ()))
1674 return true;
1675 }
1676 }
1677
1678 return false;
1679 }
1680
1681 // Print RESOURCE to PP.
1682 void
1683 rtl_ssa::pp_resource (pretty_printer *pp, resource_info resource)
1684 {
1685 resource.print (pp);
1686 }
1687
1688 // Print ACCESS to PP. FLAGS is a bitmask of PP_ACCESS_* flags.
1689 void
1690 rtl_ssa::pp_access (pretty_printer *pp, const access_info *access,
1691 unsigned int flags)
1692 {
1693 if (!access)
1694 pp_string (pp, "<null>");
1695 else if (auto *phi = dyn_cast<const phi_info *> (access))
1696 phi->print (pp, flags);
1697 else if (auto *set = dyn_cast<const set_info *> (access))
1698 set->print (pp, flags);
1699 else if (auto *clobber = dyn_cast<const clobber_info *> (access))
1700 clobber->print (pp, flags);
1701 else if (auto *use = dyn_cast<const use_info *> (access))
1702 use->print (pp, flags);
1703 else
1704 pp_string (pp, "??? Unknown access");
1705 }
1706
1707 // Print ACCESSES to PP. FLAGS is a bitmask of PP_ACCESS_* flags.
1708 void
1709 rtl_ssa::pp_accesses (pretty_printer *pp, access_array accesses,
1710 unsigned int flags)
1711 {
1712 if (accesses.empty ())
1713 pp_string (pp, "none");
1714 else
1715 {
1716 bool is_first = true;
1717 for (access_info *access : accesses)
1718 {
1719 if (is_first)
1720 is_first = false;
1721 else
1722 pp_newline_and_indent (pp, 0);
1723 pp_access (pp, access, flags);
1724 }
1725 }
1726 }
1727
1728 // Print NODE to PP.
1729 void
1730 rtl_ssa::pp_def_node (pretty_printer *pp, const def_node *node)
1731 {
1732 if (!node)
1733 pp_string (pp, "<null>");
1734 else if (auto *group = dyn_cast<const clobber_group *> (node))
1735 group->print (pp);
1736 else if (auto *set = dyn_cast<const set_node *> (node))
1737 set->print (pp);
1738 else
1739 pp_string (pp, "??? Unknown def node");
1740 }
1741
1742 // Print MUX to PP.
1743 void
1744 rtl_ssa::pp_def_mux (pretty_printer *pp, def_mux mux)
1745 {
1746 if (auto *node = mux.dyn_cast<def_node *> ())
1747 pp_def_node (pp, node);
1748 else
1749 pp_access (pp, mux.as_a<def_info *> ());
1750 }
1751
1752 // Print DL to PP.
1753 void
1754 rtl_ssa::pp_def_lookup (pretty_printer *pp, def_lookup dl)
1755 {
1756 pp_string (pp, "comparison result of ");
1757 pp_decimal_int (pp, dl.comparison);
1758 pp_string (pp, " for ");
1759 pp_newline_and_indent (pp, 0);
1760 pp_def_mux (pp, dl.mux);
1761 }
1762
1763 // Print TREE to PP.
1764 void
1765 rtl_ssa::pp_def_splay_tree (pretty_printer *pp, def_splay_tree tree)
1766 {
1767 tree.print (pp, pp_def_node);
1768 }
1769
1770 // Dump RESOURCE to FILE.
1771 void
1772 dump (FILE *file, resource_info resource)
1773 {
1774 dump_using (file, pp_resource, resource);
1775 }
1776
1777 // Dump ACCESS to FILE. FLAGS is a bitmask of PP_ACCESS_* flags.
1778 void
1779 dump (FILE *file, const access_info *access, unsigned int flags)
1780 {
1781 dump_using (file, pp_access, access, flags);
1782 }
1783
1784 // Dump ACCESSES to FILE. FLAGS is a bitmask of PP_ACCESS_* flags.
1785 void
1786 dump (FILE *file, access_array accesses, unsigned int flags)
1787 {
1788 dump_using (file, pp_accesses, accesses, flags);
1789 }
1790
1791 // Print NODE to FILE.
1792 void
1793 dump (FILE *file, const def_node *node)
1794 {
1795 dump_using (file, pp_def_node, node);
1796 }
1797
1798 // Print MUX to FILE.
1799 void
1800 dump (FILE *file, def_mux mux)
1801 {
1802 dump_using (file, pp_def_mux, mux);
1803 }
1804
1805 // Print RESULT to FILE.
1806 void
1807 dump (FILE *file, def_lookup result)
1808 {
1809 dump_using (file, pp_def_lookup, result);
1810 }
1811
1812 // Print TREE to FILE.
1813 void
1814 dump (FILE *file, def_splay_tree tree)
1815 {
1816 dump_using (file, pp_def_splay_tree, tree);
1817 }
1818
1819 // Debug interfaces to the dump routines above.
1820 void debug (const resource_info &x) { dump (stderr, x); }
1821 void debug (const access_info *x) { dump (stderr, x); }
1822 void debug (const access_array &x) { dump (stderr, x); }
1823 void debug (const def_node *x) { dump (stderr, x); }
1824 void debug (const def_mux &x) { dump (stderr, x); }
1825 void debug (const def_lookup &x) { dump (stderr, x); }
1826 void debug (const def_splay_tree &x) { dump (stderr, x); }