Debian Codegraph @

gcc/rtl-ssa/changes.cc

raw
1 // RTL SSA routines for changing instructions -*- 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 #include "target.h"
33 #include "predict.h"
34 #include "memmodel.h" // Needed by emit-rtl.h
35 #include "emit-rtl.h"
36 #include "cfghooks.h"
37 #include "cfgrtl.h"
38 #include "sreal.h"
39
40 using namespace rtl_ssa;
41
42 // See the comment above the declaration.
43 void
44 insn_change::print (pretty_printer *pp) const
45 {
46 if (m_is_deletion)
47 {
48 pp_string (pp, "deletion of ");
49 pp_insn (pp, m_insn);
50 }
51 else
52 {
53 pp_string (pp, "change to ");
54 pp_insn (pp, m_insn);
55 pp_newline_and_indent (pp, 2);
56 pp_string (pp, "~~~~~~~");
57
58 pp_newline_and_indent (pp, 0);
59 pp_string (pp, "new cost: ");
60 pp_decimal_int (pp, new_cost);
61
62 pp_newline_and_indent (pp, 0);
63 pp_string (pp, "new uses:");
64 pp_newline_and_indent (pp, 2);
65 pp_accesses (pp, new_uses);
66 pp_indentation (pp) -= 2;
67
68 pp_newline_and_indent (pp, 0);
69 pp_string (pp, "new defs:");
70 pp_newline_and_indent (pp, 2);
71 pp_accesses (pp, new_defs);
72 pp_indentation (pp) -= 2;
73
74 pp_newline_and_indent (pp, 0);
75 pp_string (pp, "first insert-after candidate: ");
76 move_range.first->print_identifier_and_location (pp);
77
78 pp_newline_and_indent (pp, 0);
79 pp_string (pp, "last insert-after candidate: ");
80 move_range.last->print_identifier_and_location (pp);
81 }
82 }
83
84 // Return a copy of access_array ACCESSES, allocating it on the
85 // temporary obstack.
86 access_array
87 function_info::temp_access_array (access_array accesses)
88 {
89 if (accesses.empty ())
90 return accesses;
91
92 gcc_assert (obstack_object_size (&m_temp_obstack) == 0);
93 obstack_grow (&m_temp_obstack, accesses.begin (), accesses.size_bytes ());
94 return { static_cast<access_info **> (obstack_finish (&m_temp_obstack)),
95 accesses.size () };
96 }
97
98 // See the comment above the declaration.
99 bool
100 function_info::verify_insn_changes (array_slice<insn_change *const> changes)
101 {
102 HARD_REG_SET defined_hard_regs, clobbered_hard_regs;
103 CLEAR_HARD_REG_SET (defined_hard_regs);
104 CLEAR_HARD_REG_SET (clobbered_hard_regs);
105
106 insn_info *min_insn = m_first_insn;
107 for (insn_change *change : changes)
108 if (!change->is_deletion ())
109 {
110 // Make sure that the changes can be kept in their current order
111 // while honoring all of the move ranges.
112 min_insn = later_insn (min_insn, change->move_range.first);
113 while (min_insn != change->insn () && !can_insert_after (min_insn))
114 min_insn = min_insn->next_nondebug_insn ();
115 if (*min_insn > *change->move_range.last)
116 {
117 if (dump_file && (dump_flags & TDF_DETAILS))
118 fprintf (dump_file, "no viable insn position assignment\n");
119 return false;
120 }
121
122 // If recog introduced new clobbers of a register as part of
123 // the matching process, make sure that they don't conflict
124 // with any other new definitions or uses of the register.
125 // (We have already checked that they don't conflict with
126 // unchanging definitions and uses.)
127 for (use_info *use : change->new_uses)
128 {
129 unsigned int regno = use->regno ();
130 if (HARD_REGISTER_NUM_P (regno)
131 && TEST_HARD_REG_BIT (clobbered_hard_regs, regno))
132 {
133 if (dump_file && (dump_flags & TDF_DETAILS))
134 fprintf (dump_file, "register %d would be clobbered"
135 " while it is still live\n", regno);
136 return false;
137 }
138 }
139 for (def_info *def : change->new_defs)
140 {
141 unsigned int regno = def->regno ();
142 if (HARD_REGISTER_NUM_P (regno))
143 {
144 if (def->m_is_temp)
145 {
146 // This is a clobber introduced by recog.
147 gcc_checking_assert (is_a<clobber_info *> (def));
148 if (TEST_HARD_REG_BIT (defined_hard_regs, regno))
149 {
150 if (dump_file && (dump_flags & TDF_DETAILS))
151 fprintf (dump_file, "conflicting definitions of"
152 " register %d\n", regno);
153 return false;
154 }
155 SET_HARD_REG_BIT (clobbered_hard_regs, regno);
156 }
157 else if (is_a<set_info *> (def))
158 {
159 // REGNO now has a defined value.
160 SET_HARD_REG_BIT (defined_hard_regs, regno);
161 CLEAR_HARD_REG_BIT (clobbered_hard_regs, regno);
162 }
163 }
164 }
165 }
166 return true;
167 }
168
169 // See the comment above the declaration.
170 bool
171 rtl_ssa::changes_are_worthwhile (array_slice<insn_change *const> changes,
172 bool strict_p)
173 {
174 unsigned int old_cost = 0;
175 sreal weighted_old_cost = 0;
176 auto entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
177 {
178 undo_recog_changes undo (0);
179 for (insn_change *change : changes)
180 {
181 // Count zero for the old cost if the old instruction was a no-op
182 // move or had an unknown cost. This should reduce the chances of
183 // making an unprofitable change.
184 old_cost += change->old_cost ();
185 basic_block cfg_bb = change->bb ()->cfg_bb ();
186 if (optimize_bb_for_speed_p (cfg_bb))
187 weighted_old_cost += (cfg_bb->count.to_sreal_scale (entry_count)
188 * change->old_cost ());
189
190 }
191 }
192 unsigned int new_cost = 0;
193 sreal weighted_new_cost = 0;
194 for (insn_change *change : changes)
195 {
196 basic_block cfg_bb = change->bb ()->cfg_bb ();
197 bool for_speed = optimize_bb_for_speed_p (cfg_bb);
198 if (!change->is_deletion ()
199 && INSN_CODE (change->rtl ()) != NOOP_MOVE_INSN_CODE)
200 {
201 change->new_cost = insn_cost (change->rtl (), for_speed);
202 /* If the cost is unknown, replacement is not worthwhile. */
203 if (!change->new_cost)
204 {
205 if (dump_file && (dump_flags & TDF_DETAILS))
206 fprintf (dump_file,
207 "Reject replacement due to unknown insn cost.\n");
208 return false;
209 }
210 new_cost += change->new_cost;
211 if (for_speed)
212 weighted_new_cost += (cfg_bb->count.to_sreal_scale (entry_count)
213 * change->new_cost);
214 }
215 }
216 bool ok_p;
217 if (weighted_new_cost != weighted_old_cost)
218 ok_p = weighted_new_cost < weighted_old_cost;
219 else if (strict_p)
220 ok_p = new_cost < old_cost;
221 else
222 ok_p = new_cost <= old_cost;
223 if (dump_file && (dump_flags & TDF_DETAILS))
224 {
225 fprintf (dump_file, "original cost");
226 char sep = '=';
227 for (const insn_change *change : changes)
228 {
229 fprintf (dump_file, " %c %d", sep, change->old_cost ());
230 sep = '+';
231 }
232 if (weighted_old_cost != 0)
233 fprintf (dump_file, " (weighted: %f)", weighted_old_cost.to_double ());
234 fprintf (dump_file, ", replacement cost");
235 sep = '=';
236 for (const insn_change *change : changes)
237 if (!change->is_deletion ())
238 {
239 if (INSN_CODE (change->rtl ()) == NOOP_MOVE_INSN_CODE)
240 fprintf (dump_file, " %c nop", sep);
241 else
242 fprintf (dump_file, " %c %d", sep, change->new_cost);
243 sep = '+';
244 }
245 if (weighted_new_cost != 0)
246 fprintf (dump_file, " (weighted: %f)", weighted_new_cost.to_double ());
247 fprintf (dump_file, "; %s\n",
248 ok_p ? "keeping replacement" : "rejecting replacement");
249 }
250 if (!ok_p)
251 return false;
252
253 return true;
254 }
255
256 // SET has been deleted. Clean up all remaining uses. Such uses are
257 // either dead phis or now-redundant live-out uses.
258 void
259 function_info::process_uses_of_deleted_def (set_info *set)
260 {
261 if (!set->has_any_uses ())
262 return;
263
264 auto *use = *set->all_uses ().begin ();
265 do
266 {
267 auto *next_use = use->next_use ();
268 if (use->is_in_phi ())
269 {
270 // This call will not recurse.
271 process_uses_of_deleted_def (use->phi ());
272 delete_phi (use->phi ());
273 }
274 else
275 {
276 gcc_assert (use->is_live_out_use ());
277 remove_use (use);
278 }
279 use = next_use;
280 }
281 while (use);
282 gcc_assert (!set->has_any_uses ());
283 }
284
285 // Update the REG_NOTES of INSN, whose pattern has just been changed.
286 static void
287 update_notes (rtx_insn *insn)
288 {
289 for (rtx *note_ptr = &REG_NOTES (insn); *note_ptr; )
290 {
291 rtx note = *note_ptr;
292 bool keep_p = true;
293 switch (REG_NOTE_KIND (note))
294 {
295 case REG_EQUAL:
296 case REG_EQUIV:
297 case REG_NOALIAS:
298 keep_p = (single_set (insn) != nullptr);
299 break;
300
301 case REG_UNUSED:
302 case REG_DEAD:
303 // These notes are stale. We'll recompute REG_UNUSED notes
304 // after the update.
305 keep_p = false;
306 break;
307
308 default:
309 break;
310 }
311 if (keep_p)
312 note_ptr = &XEXP (*note_ptr, 1);
313 else
314 {
315 *note_ptr = XEXP (*note_ptr, 1);
316 free_EXPR_LIST_node (note);
317 }
318 }
319 }
320
321 // Pick a location for CHANGE's instruction and return the instruction
322 // after which it should be placed.
323 static insn_info *
324 choose_insn_placement (insn_change &change)
325 {
326 gcc_checking_assert (change.move_range);
327
328 insn_info *insn = change.insn ();
329 insn_info *first = change.move_range.first;
330 insn_info *last = change.move_range.last;
331
332 // Quick(ish) exit if there is only one possible choice.
333 if (first == last)
334 return first;
335 if (first == insn->prev_nondebug_insn () && last == insn)
336 return insn;
337
338 // For now just use the closest valid choice to the original instruction.
339 // If the register usage has changed significantly, it might instead be
340 // better to try to take register pressure into account.
341 insn_info *closest = change.move_range.clamp_insn_to_range (insn);
342 while (closest != insn && !can_insert_after (closest))
343 closest = closest->next_nondebug_insn ();
344 return closest;
345 }
346
347 // Record any changes related to CHANGE that need to be queued for later.
348 void
349 function_info::possibly_queue_changes (insn_change &change)
350 {
351 insn_info *insn = change.insn ();
352 rtx_insn *rtl = insn->rtl ();
353
354 // If the instruction could previously throw, we eventually need to call
355 // purge_dead_edges to check whether things have changed.
356 if (find_reg_note (rtl, REG_EH_REGION, nullptr))
357 bitmap_set_bit (m_need_to_purge_dead_edges, insn->bb ()->index ());
358
359 auto needs_pending_update = [&]()
360 {
361 // If an instruction became a no-op without the pass explicitly
362 // deleting it, queue the deletion for later. Removing the
363 // instruction on the fly would require an update to all instructions
364 // that use the result of the move, which would be a potential source
365 // of quadraticness. Also, definitions shouldn't disappear under
366 // the pass's feet.
367 if (INSN_CODE (rtl) == NOOP_MOVE_INSN_CODE)
368 return true;
369
370 // If any jumps got turned into unconditional jumps or nops, we need
371 // to update the CFG accordingly.
372 if (JUMP_P (rtl)
373 && (returnjump_p (rtl) || any_uncondjump_p (rtl))
374 && !single_succ_p (insn->bb ()->cfg_bb ()))
375 return true;
376
377 // If a previously conditional trap now always fires, execution
378 // terminates at that point.
379 rtx pattern = PATTERN (rtl);
380 if (GET_CODE (pattern) == TRAP_IF
381 && XEXP (pattern, 0) == const1_rtx)
382 return true;
383
384 return false;
385 };
386
387 if (needs_pending_update ()
388 && bitmap_set_bit (m_queued_insn_update_uids, insn->uid ()))
389 {
390 gcc_assert (!change.is_deletion ());
391 m_queued_insn_updates.safe_push (insn);
392 }
393 }
394
395 // Remove the instruction described by CHANGE from the underlying RTL
396 // and from the insn_info list.
397 static void
398 delete_insn (insn_change &change)
399 {
400 insn_info *insn = change.insn ();
401 rtx_insn *rtl = change.rtl ();
402 if (dump_file && (dump_flags & TDF_DETAILS))
403 fprintf (dump_file, "deleting insn %d\n", insn->uid ());
404 set_insn_deleted (rtl);
405 }
406
407 // Move the RTL instruction associated with CHANGE so that it comes
408 // immediately after AFTER.
409 static void
410 move_insn (insn_change &change, insn_info *after)
411 {
412 rtx_insn *rtl = change.rtl ();
413 rtx_insn *after_rtl = after->rtl ();
414 if (dump_file && (dump_flags & TDF_DETAILS))
415 fprintf (dump_file, "moving insn %d after insn %d\n",
416 INSN_UID (rtl), INSN_UID (after_rtl));
417
418 // At the moment we don't support moving instructions between EBBs,
419 // but this would be worth adding if it's useful.
420 insn_info *insn = change.insn ();
421
422 bb_info *bb = after->bb ();
423 basic_block cfg_bb = bb->cfg_bb ();
424
425 if (!insn->is_temporary ())
426 {
427 gcc_assert (after->ebb () == insn->ebb ());
428
429 if (insn->bb () != bb)
430 // Force DF to mark the old block as dirty.
431 df_insn_delete (rtl);
432 ::remove_insn (rtl);
433 }
434
435 ::add_insn_after (rtl, after_rtl, cfg_bb);
436 }
437
438 // The instruction associated with CHANGE is being changed in-place.
439 // Update the DF information for its new pattern.
440 static void
441 update_insn_in_place (insn_change &change)
442 {
443 insn_info *insn = change.insn ();
444 if (dump_file && (dump_flags & TDF_DETAILS))
445 fprintf (dump_file, "updating insn %d in-place\n", insn->uid ());
446 df_insn_rescan (change.rtl ());
447 }
448
449 // Finalize the new list of definitions and uses in CHANGE, removing
450 // any uses and definitions that are no longer needed, and converting
451 // pending clobbers into actual definitions.
452 //
453 // POS gives the final position of INSN, which hasn't yet been moved into
454 // place. Keep track of any newly-created set_infos being added with this
455 // change by adding them to NEW_SETS.
456 void
457 function_info::finalize_new_accesses (insn_change &change, insn_info *pos,
458 hash_set<def_info *> &new_sets)
459 {
460 insn_info *insn = change.insn ();
461
462 // Get a list of all the things that the instruction now references.
463 vec_rtx_properties properties;
464 properties.add_insn (insn->rtl (), true);
465
466 // Build up the new list of definitions.
467 for (rtx_obj_reference ref : properties.refs ())
468 if (ref.is_write ())
469 {
470 def_info *def = find_access (change.new_defs, ref.regno);
471 gcc_assert (def);
472
473 if (def->m_is_temp && is_a<set_info *> (def) && def->last_def ())
474 {
475 // For temporary sets being added with this change, we keep track of
476 // the corresponding permanent def using the last_def link.
477 //
478 // So if we have one of these, follow it to get the permanent def.
479 def = def->last_def ();
480 gcc_assert (!def->m_is_temp && !def->m_has_been_superceded);
481 }
482
483 if (def->m_is_temp)
484 {
485 if (is_a<clobber_info *> (def))
486 def = allocate<clobber_info> (change.insn (), ref.regno);
487 else if (is_a<set_info *> (def))
488 {
489 // Install the permanent set in the last_def link of the
490 // temporary def. This allows us to find the permanent def
491 // later in case we see a second write to the same resource.
492 def_info *perm_def = allocate<set_info> (change.insn (),
493 def->resource ());
494
495 // Keep track of the new set so we remember to add it to the
496 // def chain later.
497 if (new_sets.add (perm_def))
498 gcc_unreachable (); // We shouldn't see duplicates here.
499
500 def->set_last_def (perm_def);
501 def = perm_def;
502 }
503 else
504 gcc_unreachable ();
505 }
506 else if (!def->m_has_been_superceded)
507 {
508 // This is a second or subsequent definition.
509 // See function_info::record_def for a discussion of when
510 // this can happen.
511 def->record_reference (ref, false);
512 continue;
513 }
514 else
515 {
516 def->m_has_been_superceded = false;
517
518 // Clobbers can move around, so remove them from their current
519 // position and them back in their final position.
520 //
521 // At the moment, we don't allow sets to move relative to other
522 // definitions of the same resource, so we can leave those where
523 // they are. It might be useful to relax this in future.
524 // The main complication is that removing a set would potentially
525 // fuse two adjoining clobber_groups, and adding the set back
526 // would require the group to be split again.
527 if (is_a<clobber_info *> (def))
528 remove_def (def);
529 else if (ref.is_reg ())
530 def->set_mode (ref.mode);
531 def->set_insn (insn);
532 }
533 def->record_reference (ref, true);
534 m_temp_defs.safe_push (def);
535 }
536
537 // Also keep any explicitly-recorded call clobbers, which are deliberately
538 // excluded from the vec_rtx_properties. Calls shouldn't move, so we can
539 // keep the definitions in their current position.
540 //
541 // If the change describes a set of memory, but the pattern doesn't
542 // reference memory, keep the set anyway. This can happen if the
543 // old pattern was a parallel that contained a memory clobber, and if
544 // the new pattern was recognized without that clobber. Keeping the
545 // set avoids a linear-complexity update to the set's users.
546 //
547 // ??? We could queue an update so that these bogus clobbers are
548 // removed later.
549 for (def_info *def : change.new_defs)
550 if (def->m_has_been_superceded
551 && (def->is_call_clobber () || def->is_mem ()))
552 {
553 def->m_has_been_superceded = false;
554 def->set_insn (insn);
555 m_temp_defs.safe_push (def);
556 }
557
558 // Install the new list of definitions in CHANGE.
559 sort_accesses (m_temp_defs);
560 access_array accesses = temp_access_array (m_temp_defs);
561 change.new_defs = def_array (accesses);
562 m_temp_defs.truncate (0);
563
564 // Create temporary copies of use_infos that are already attached to
565 // other insns, which could happen if the uses come from unchanging
566 // insns or if they have been used by earlier changes. Doing this
567 // makes it easier to detect multiple reads below.
568 auto *unshared_uses_base = XOBNEWVEC (&m_temp_obstack, access_info *,
569 change.new_uses.size ());
570 unsigned int i = 0;
571 for (use_info *use : change.new_uses)
572 {
573 if (!use->m_has_been_superceded)
574 {
575 use = allocate_temp<use_info> (insn, use->resource (), use->def ());
576 use->m_has_been_superceded = true;
577 use->m_is_temp = true;
578 }
579 unshared_uses_base[i++] = use;
580 }
581 auto unshared_uses = use_array (unshared_uses_base, change.new_uses.size ());
582
583 // Add (possibly temporary) uses to m_temp_uses for each resource.
584 // If there are multiple references to the same resource, aggregate
585 // information in the modes and flags.
586 use_info *mem_use = nullptr;
587 for (rtx_obj_reference ref : properties.refs ())
588 if (ref.is_read ())
589 {
590 unsigned int regno = ref.regno;
591 machine_mode mode = ref.is_reg () ? ref.mode : BLKmode;
592 use_info *use = find_access (unshared_uses, ref.regno);
593 if (!use)
594 {
595 // For now, we only support inferring uses of mem.
596 gcc_assert (regno == MEM_REGNO);
597
598 if (mem_use)
599 {
600 mem_use->record_reference (ref, false);
601 continue;
602 }
603
604 resource_info resource { mode, regno };
605 auto def = find_def (resource, pos).prev_def (pos);
606 auto set = safe_dyn_cast <set_info *> (def);
607 gcc_assert (set);
608 mem_use = allocate<use_info> (insn, resource, set);
609 mem_use->record_reference (ref, true);
610 m_temp_uses.safe_push (mem_use);
611 continue;
612 }
613
614 if (use->m_has_been_superceded)
615 {
616 // This is the first reference to the resource.
617 bool is_temp = use->m_is_temp;
618 *use = use_info (insn, resource_info { mode, regno }, use->def ());
619 use->m_is_temp = is_temp;
620 use->record_reference (ref, true);
621 m_temp_uses.safe_push (use);
622 }
623 else
624 {
625 // Record the mode of the largest use. The choice is arbitrary if
626 // the instruction (unusually) references the same register in two
627 // different but equal-sized modes.
628 if (HARD_REGISTER_NUM_P (regno)
629 && partial_subreg_p (use->mode (), mode))
630 use->set_mode (mode);
631 use->record_reference (ref, false);
632 }
633 }
634
635 // Replace any temporary uses and definitions with real ones.
636 for (unsigned int i = 0; i < m_temp_uses.length (); ++i)
637 {
638 auto *use = as_a<use_info *> (m_temp_uses[i]);
639 if (use->m_is_temp)
640 {
641 m_temp_uses[i] = use = allocate<use_info> (*use);
642 use->m_is_temp = false;
643 set_info *def = use->def ();
644 if (!def || !def->m_is_temp)
645 continue;
646
647 if (auto phi = dyn_cast<phi_info *> (def))
648 {
649 // Handle cases in which the value was previously not used
650 // within the block.
651 gcc_assert (phi->is_degenerate ());
652 phi = create_degenerate_phi (phi->ebb (), phi->input_value (0));
653 use->set_def (phi);
654 }
655 else
656 {
657 // The temporary def may also be a set added with this change, in
658 // which case the permanent set is stored in the last_def link,
659 // and we need to update the use to refer to the permanent set.
660 gcc_assert (is_a<set_info *> (def));
661 auto perm_set = as_a<set_info *> (def->last_def ());
662 gcc_assert (!perm_set->is_temporary ());
663 use->set_def (perm_set);
664 }
665 }
666 }
667
668 // Install the new list of uses in CHANGE.
669 sort_accesses (m_temp_uses);
670 change.new_uses = use_array (temp_access_array (m_temp_uses));
671 m_temp_uses.truncate (0);
672
673 // Record the new instruction-wide properties.
674 insn->set_properties (properties);
675 }
676
677 // Copy information from CHANGE to its underlying insn_info, given that
678 // the insn_info has already been placed appropriately. NEW_SETS contains the
679 // new set_infos that are being added as part of this change (as opposed to
680 // being moved or repurposed from existing instructions).
681 void
682 function_info::apply_changes_to_insn (insn_change &change,
683 hash_set<def_info *> &new_sets)
684 {
685 insn_info *insn = change.insn ();
686 if (change.is_deletion ())
687 {
688 insn->set_accesses (nullptr, 0, 0);
689 return;
690 }
691
692 // Copy the cost.
693 insn->set_cost (change.new_cost);
694
695 // Add all clobbers and newly-created sets. Existing sets and call
696 // clobbers never move relative to other definitions, so are OK as-is.
697 for (def_info *def : change.new_defs)
698 if ((is_a<clobber_info *> (def) && !def->is_call_clobber ())
699 || (is_a<set_info *> (def) && new_sets.contains (def)))
700 add_def (def);
701
702 // Add all uses, now that their position is final.
703 for (use_info *use : change.new_uses)
704 add_use (use);
705
706 // Copy the uses and definitions.
707 unsigned int num_defs = change.new_defs.size ();
708 unsigned int num_uses = change.new_uses.size ();
709 if (num_defs + num_uses <= insn->num_defs () + insn->num_uses ())
710 insn->copy_accesses (change.new_defs, change.new_uses);
711 else
712 {
713 access_array_builder builder (&m_obstack);
714 builder.reserve (num_defs + num_uses);
715
716 for (def_info *def : change.new_defs)
717 builder.quick_push (def);
718 for (use_info *use : change.new_uses)
719 builder.quick_push (use);
720
721 insn->set_accesses (builder.finish ().begin (), num_defs, num_uses);
722 }
723
724 insn->m_is_temp = false;
725 }
726
727 // Add a temporary placeholder instruction after AFTER.
728 insn_info *
729 function_info::add_placeholder_after (insn_info *after)
730 {
731 insn_info *insn = allocate_temp<insn_info> (after->bb (), nullptr, -1);
732 add_insn_after (insn, after);
733 return insn;
734 }
735
736 // See the comment above the declaration.
737 void
738 function_info::change_insns (array_slice<insn_change *> changes)
739 {
740 auto watermark = temp_watermark ();
741
742 insn_info *min_insn = m_first_insn;
743 for (insn_change *change : changes)
744 {
745 // Tentatively mark all the old uses and definitions for deletion.
746 for (use_info *use : change->old_uses ())
747 {
748 use->m_has_been_superceded = true;
749 remove_use (use);
750 }
751 for (def_info *def : change->old_defs ())
752 def->m_has_been_superceded = true;
753
754 if (!change->is_deletion ())
755 {
756 // Remove any notes that are no longer relevant.
757 if (!change->insn ()->m_is_temp)
758 update_notes (change->rtl ());
759
760 // Make sure that the placement of this instruction would still
761 // leave room for previous instructions.
762 change->move_range = move_later_than (change->move_range, min_insn);
763 if (!canonicalize_move_range (change->move_range, change->insn ()))
764 // verify_insn_changes is supposed to make sure that this holds.
765 gcc_unreachable ();
766 min_insn = later_insn (min_insn, change->move_range.first);
767
768 if (change->insn ()->m_is_temp)
769 {
770 change->m_insn = allocate<insn_info> (change->insn ()->bb (),
771 change->rtl (),
772 change->insn_uid ());
773
774 // Set the flag again so subsequent logic is aware.
775 // It will be cleared later on.
776 change->m_insn->m_is_temp = true;
777 }
778 }
779 }
780
781 // Walk backwards through the changes, allocating specific positions
782 // to each one. Update the underlying RTL and its associated DF
783 // information.
784 insn_info *following_insn = nullptr;
785 auto_vec<insn_info *, 16> placeholders;
786 placeholders.safe_grow_cleared (changes.size ());
787 for (unsigned int i = changes.size (); i-- > 0;)
788 {
789 insn_change &change = *changes[i];
790 insn_info *placeholder = nullptr;
791 possibly_queue_changes (change);
792 if (change.is_deletion ())
793 delete_insn (change);
794 else
795 {
796 // Make sure that this instruction comes before later ones.
797 if (following_insn)
798 {
799 change.move_range = move_earlier_than (change.move_range,
800 following_insn);
801 if (!canonicalize_move_range (change.move_range,
802 change.insn ()))
803 // verify_insn_changes is supposed to make sure that this
804 // holds.
805 gcc_unreachable ();
806 }
807
808 // Decide which instruction INSN should go after.
809 insn_info *after = choose_insn_placement (change);
810
811 // If INSN is moving, insert a placeholder insn_info at the
812 // new location. We can't move INSN itself yet because it
813 // might still be referenced by earlier move ranges.
814 insn_info *insn = change.insn ();
815 if (after == insn || after == insn->prev_nondebug_insn ())
816 {
817 update_insn_in_place (change);
818 following_insn = insn;
819 }
820 else
821 {
822 move_insn (change, after);
823 placeholder = add_placeholder_after (after);
824 following_insn = placeholder;
825 }
826 }
827 placeholders[i] = placeholder;
828 }
829
830 // We need to keep track of newly-added sets as these need adding to
831 // the def chain later.
832 hash_set<def_info *> new_sets;
833
834 // Finalize the new list of accesses for each change. Don't install them yet,
835 // so that we still have access to the old lists below.
836 //
837 // Note that we do this forwards instead of in the backwards loop above so
838 // that any new defs being inserted are processed before new uses of those
839 // defs, so that the (initially) temporary uses referring to temporary defs
840 // can be easily updated to become permanent uses referring to permanent defs.
841 for (unsigned i = 0; i < changes.size (); i++)
842 {
843 insn_change &change = *changes[i];
844 insn_info *placeholder = placeholders[i];
845 if (!change.is_deletion ())
846 finalize_new_accesses (change,
847 placeholder ? placeholder : change.insn (),
848 new_sets);
849 }
850
851 // Remove all definitions that are no longer needed. After the above,
852 // the only uses of such definitions should be dead phis and now-redundant
853 // live-out uses.
854 //
855 // In particular, this means that consumers must handle debug
856 // instructions before removing a set.
857 for (insn_change *change : changes)
858 for (def_info *def : change->old_defs ())
859 if (def->m_has_been_superceded)
860 {
861 auto *set = dyn_cast<set_info *> (def);
862 if (set && set->has_any_uses ())
863 process_uses_of_deleted_def (set);
864 remove_def (def);
865 }
866
867 // Move the insn_infos to their new locations.
868 for (unsigned int i = 0; i < changes.size (); ++i)
869 {
870 insn_change &change = *changes[i];
871 insn_info *insn = change.insn ();
872 if (change.is_deletion ())
873 {
874 if (rtx_insn *rtl = insn->rtl ())
875 ::remove_insn (rtl); // Remove the underlying RTL insn.
876 remove_insn (insn);
877 }
878 else if (insn_info *placeholder = placeholders[i])
879 {
880 // Check if earlier movements turned a move into a no-op.
881 if (placeholder->prev_nondebug_insn () == insn
882 || placeholder->next_nondebug_insn () == insn)
883 {
884 remove_insn (placeholder);
885 placeholders[i] = nullptr;
886 }
887 else
888 {
889 insn_info *after = placeholder->prev_any_insn ();
890 if (!insn->is_temporary ())
891 remove_insn (insn);
892 replace_nondebug_insn (placeholder, insn);
893 insn->set_bb (after->bb ());
894 }
895 }
896 }
897
898 // Apply the changes to the underlying insn_infos.
899 for (insn_change *change : changes)
900 apply_changes_to_insn (*change, new_sets);
901
902 // Now that the insns and accesses are up to date, add any REG_UNUSED notes.
903 for (insn_change *change : changes)
904 if (!change->is_deletion ())
905 add_reg_unused_notes (change->insn ());
906 }
907
908 // See the comment above the declaration.
909 void
910 function_info::change_insn (insn_change &change)
911 {
912 insn_change *changes[] = { &change };
913 return change_insns (changes);
914 }
915
916 // Try to adjust CHANGE so that its pattern can include clobber rtx CLOBBER.
917 // Return true on success.
918 //
919 // ADD_REGNO_CLOBBER is a specialization of function_info::add_regno_clobber
920 // for a specific caller-provided predicate.
921 static bool
922 add_clobber (insn_change &change, add_regno_clobber_fn add_regno_clobber,
923 rtx clobber)
924 {
925 rtx pat = PATTERN (change.rtl ());
926 gcc_assert (GET_CODE (clobber) == CLOBBER);
927 rtx dest = XEXP (clobber, 0);
928 if (GET_CODE (dest) == SCRATCH)
929 {
930 if (reload_completed)
931 {
932 if (dump_file && (dump_flags & TDF_DETAILS))
933 {
934 // ??? Maybe we could try to do some RA here?
935 fprintf (dump_file, "instruction requires a scratch"
936 " after reload:\n");
937 print_rtl_single (dump_file, pat);
938 }
939 return false;
940 }
941 return true;
942 }
943
944 gcc_assert (REG_P (dest));
945 for (unsigned int regno = REGNO (dest); regno != END_REGNO (dest); ++regno)
946 if (!add_regno_clobber (change, regno))
947 {
948 if (dump_file && (dump_flags & TDF_DETAILS))
949 {
950 fprintf (dump_file, "cannot clobber live register %d in:\n",
951 regno);
952 print_rtl_single (dump_file, pat);
953 }
954 return false;
955 }
956 return true;
957 }
958
959 // See if PARALLEL pattern PAT clobbers any of the registers in ACCESSES.
960 // Return one such access if so, otherwise return null.
961 static access_info *
962 find_clobbered_access (access_array accesses, rtx pat)
963 {
964 rtx subpat;
965 for (int i = 0; i < XVECLEN (pat, 0); ++i)
966 if (GET_CODE (subpat = XVECEXP (pat, 0, i)) == CLOBBER)
967 {
968 rtx x = XEXP (subpat, 0);
969 if (REG_P (x))
970 for (auto *access : accesses)
971 if (access->regno () >= REGNO (x)
972 && access->regno () < END_REGNO (x))
973 return access;
974 }
975 return nullptr;
976 }
977
978 // Try to recognize the new form of the insn associated with CHANGE,
979 // adding any clobbers that are necessary to make the instruction match
980 // an .md pattern. Return true on success.
981 //
982 // ADD_REGNO_CLOBBER is a specialization of function_info::add_regno_clobber
983 // for a specific caller-provided predicate.
984 static bool
985 recog_level2 (insn_change &change, add_regno_clobber_fn add_regno_clobber)
986 {
987 insn_change_watermark insn_watermark;
988 rtx_insn *rtl = change.rtl ();
989 rtx pat = PATTERN (rtl);
990 int num_clobbers = 0;
991 int icode = -1;
992 bool asm_p = asm_noperands (pat) >= 0;
993 if (asm_p)
994 {
995 if (!check_asm_operands (pat))
996 {
997 if (dump_file && (dump_flags & TDF_DETAILS))
998 {
999 fprintf (dump_file, "failed to match this asm instruction:\n");
1000 print_rtl_single (dump_file, pat);
1001 }
1002 return false;
1003 }
1004 }
1005 else if (noop_move_p (rtl))
1006 {
1007 INSN_CODE (rtl) = NOOP_MOVE_INSN_CODE;
1008 if (dump_file && (dump_flags & TDF_DETAILS))
1009 {
1010 fprintf (dump_file, "instruction becomes a no-op:\n");
1011 print_rtl_single (dump_file, pat);
1012 }
1013 insn_watermark.keep ();
1014 return true;
1015 }
1016 else
1017 {
1018 icode = ::recog (pat, rtl, &num_clobbers);
1019 if (icode < 0)
1020 {
1021 if (dump_file && (dump_flags & TDF_DETAILS))
1022 {
1023 fprintf (dump_file, "failed to match this instruction:\n");
1024 print_rtl_single (dump_file, pat);
1025 }
1026 return false;
1027 }
1028 }
1029
1030 auto prev_new_defs = change.new_defs;
1031 auto prev_move_range = change.move_range;
1032 if (num_clobbers > 0)
1033 {
1034 // ??? It would be good to have a way of recycling the rtxes on failure,
1035 // but any attempt to cache old PARALLELs would at best be a half
1036 // measure, since add_clobbers would still generate fresh clobbers
1037 // each time. It would be better to have a more general recycling
1038 // mechanism that all rtx passes can use.
1039 rtvec newvec;
1040 int oldlen;
1041 if (GET_CODE (pat) == PARALLEL)
1042 {
1043 oldlen = XVECLEN (pat, 0);
1044 newvec = rtvec_alloc (num_clobbers + oldlen);
1045 for (int i = 0; i < oldlen; ++i)
1046 RTVEC_ELT (newvec, i) = XVECEXP (pat, 0, i);
1047 }
1048 else
1049 {
1050 oldlen = 1;
1051 newvec = rtvec_alloc (num_clobbers + oldlen);
1052 RTVEC_ELT (newvec, 0) = pat;
1053 }
1054 rtx newpat = gen_rtx_PARALLEL (VOIDmode, newvec);
1055 add_clobbers (newpat, icode);
1056 validate_change (rtl, &PATTERN (rtl), newpat, true);
1057 for (int i = 0; i < num_clobbers; ++i)
1058 if (!add_clobber (change, add_regno_clobber,
1059 XVECEXP (newpat, 0, oldlen + i)))
1060 {
1061 change.new_defs = prev_new_defs;
1062 change.move_range = prev_move_range;
1063 return false;
1064 }
1065
1066 pat = newpat;
1067 }
1068
1069 INSN_CODE (rtl) = icode;
1070 if (recog_data.insn == rtl)
1071 recog_data.insn = nullptr;
1072
1073 // See if the pattern contains any hard-coded clobbers of registers
1074 // that are also inputs to the instruction. The standard rtl semantics
1075 // treat such clobbers as earlyclobbers, since there is no way of proving
1076 // which clobbers conflict with the inputs and which don't.
1077 //
1078 // (Non-hard-coded clobbers are handled by constraint satisfaction instead.)
1079 rtx subpat;
1080 if (GET_CODE (pat) == PARALLEL)
1081 for (int i = 0; i < XVECLEN (pat, 0); ++i)
1082 if (GET_CODE (subpat = XVECEXP (pat, 0, i)) == CLOBBER
1083 && REG_P (XEXP (subpat, 0)))
1084 {
1085 // Stub out all operands, so that we can tell which registers
1086 // are hard-coded.
1087 extract_insn (rtl);
1088 for (int j = 0; j < recog_data.n_operands; ++j)
1089 *recog_data.operand_loc[j] = pc_rtx;
1090
1091 auto *use = find_clobbered_access (change.new_uses, pat);
1092
1093 // Restore the operands.
1094 for (int j = 0; j < recog_data.n_operands; ++j)
1095 *recog_data.operand_loc[j] = recog_data.operand[j];
1096
1097 if (use)
1098 {
1099 if (dump_file && (dump_flags & TDF_DETAILS))
1100 {
1101 fprintf (dump_file, "register %d is both clobbered"
1102 " and used as an input:\n", use->regno ());
1103 print_rtl_single (dump_file, pat);
1104 }
1105 return false;
1106 }
1107 }
1108
1109 // Per rtl.texi, registers that are modified using RTX_AUTOINC operations
1110 // cannot also appear outside an address.
1111 vec_rtx_properties properties;
1112 properties.add_pattern (pat);
1113 for (rtx_obj_reference def : properties.refs ())
1114 if (def.is_pre_post_modify ())
1115 for (rtx_obj_reference use : properties.refs ())
1116 if (def.regno == use.regno && !use.in_address ())
1117 {
1118 if (dump_file && (dump_flags & TDF_DETAILS))
1119 {
1120 fprintf (dump_file, "register %d is both auto-modified"
1121 " and used outside an address:\n", def.regno);
1122 print_rtl_single (dump_file, pat);
1123 }
1124 return false;
1125 }
1126
1127 // check_asm_operands checks the constraints after RA, so we don't
1128 // need to do it again.
1129 if (reload_completed && !asm_p)
1130 {
1131 extract_insn (rtl);
1132 if (!constrain_operands (1, get_preferred_alternatives (rtl)))
1133 {
1134 if (dump_file && (dump_flags & TDF_DETAILS))
1135 {
1136 if (asm_p)
1137 fprintf (dump_file, "asm does not match its constraints:\n");
1138 else if (const char *name = get_insn_name (icode))
1139 fprintf (dump_file, "instruction does not match the"
1140 " constraints for %s:\n", name);
1141 else
1142 fprintf (dump_file, "instruction does not match its"
1143 " constraints:\n");
1144 print_rtl_single (dump_file, pat);
1145 }
1146 change.new_defs = prev_new_defs;
1147 change.move_range = prev_move_range;
1148 return false;
1149 }
1150 }
1151
1152 if (dump_file && (dump_flags & TDF_DETAILS))
1153 {
1154 const char *name;
1155 if (!asm_p && (name = get_insn_name (icode)))
1156 fprintf (dump_file, "successfully matched this instruction "
1157 "to %s:\n", name);
1158 else
1159 fprintf (dump_file, "successfully matched this instruction:\n");
1160 print_rtl_single (dump_file, pat);
1161 }
1162
1163 insn_watermark.keep ();
1164 return true;
1165 }
1166
1167 // Try to recognize the new form of the insn associated with CHANGE,
1168 // adding and removing clobbers as necessary to make the instruction
1169 // match an .md pattern. Return true on success, otherwise leave
1170 // CHANGE as it was on entry.
1171 //
1172 // ADD_REGNO_CLOBBER is a specialization of function_info::add_regno_clobber
1173 // for a specific caller-provided predicate.
1174 bool
1175 rtl_ssa::recog_internal (insn_change &change,
1176 add_regno_clobber_fn add_regno_clobber)
1177 {
1178 // Accept all changes to debug instructions.
1179 insn_info *insn = change.insn ();
1180 if (insn->is_debug_insn ())
1181 return true;
1182
1183 rtx_insn *rtl = insn->rtl ();
1184 rtx pat = PATTERN (rtl);
1185 if (GET_CODE (pat) == PARALLEL && asm_noperands (pat) < 0)
1186 {
1187 // Try to remove trailing (clobber (scratch)) rtxes, since the new form
1188 // of the instruction might not need those scratches. recog will add
1189 // back any that are needed.
1190 int len = XVECLEN (pat, 0);
1191 int new_len = len;
1192 while (new_len > 0
1193 && GET_CODE (XVECEXP (pat, 0, new_len - 1)) == CLOBBER
1194 && GET_CODE (XEXP (XVECEXP (pat, 0, new_len - 1), 0)) == SCRATCH)
1195 new_len -= 1;
1196
1197 int old_num_changes = num_validated_changes ();
1198 validate_change_xveclen (rtl, &PATTERN (rtl), new_len, true);
1199 if (recog_level2 (change, add_regno_clobber))
1200 return true;
1201 cancel_changes (old_num_changes);
1202
1203 // Try to remove all trailing clobbers. For example, a pattern that
1204 // used to clobber the flags might no longer need to do so.
1205 int prev_len = new_len;
1206 while (new_len > 0
1207 && GET_CODE (XVECEXP (pat, 0, new_len - 1)) == CLOBBER)
1208 new_len -= 1;
1209 if (new_len != prev_len)
1210 {
1211 validate_change_xveclen (rtl, &PATTERN (rtl), new_len, true);
1212 if (recog_level2 (change, add_regno_clobber))
1213 return true;
1214 cancel_changes (old_num_changes);
1215 }
1216 return false;
1217 }
1218
1219 return recog_level2 (change, add_regno_clobber);
1220 }
1221
1222 // See the comment above the declaration.
1223 bool
1224 function_info::perform_pending_updates ()
1225 {
1226 bool changed_cfg = false;
1227 bool changed_jumps = false;
1228 for (insn_info *insn : m_queued_insn_updates)
1229 {
1230 rtx_insn *rtl = insn->rtl ();
1231 if (NOTE_P (rtl))
1232 // The insn was later optimized away, typically to a NOTE_INSN_DELETED.
1233 ;
1234 else if (JUMP_P (rtl))
1235 {
1236 if (INSN_CODE (rtl) == NOOP_MOVE_INSN_CODE)
1237 {
1238 ::delete_insn (rtl);
1239 bitmap_set_bit (m_need_to_purge_dead_edges,
1240 insn->bb ()->index ());
1241 }
1242 else if (returnjump_p (rtl) || any_uncondjump_p (rtl))
1243 {
1244 mark_jump_label (PATTERN (rtl), rtl, 0);
1245 update_cfg_for_uncondjump (rtl);
1246 changed_cfg = true;
1247 changed_jumps = true;
1248 }
1249 }
1250 else if (INSN_CODE (rtl) == NOOP_MOVE_INSN_CODE)
1251 ::delete_insn (rtl);
1252 else
1253 {
1254 rtx pattern = PATTERN (rtl);
1255 if (GET_CODE (pattern) == TRAP_IF
1256 && XEXP (pattern, 0) == const1_rtx)
1257 {
1258 remove_edge (split_block (BLOCK_FOR_INSN (rtl), rtl));
1259 emit_barrier_after_bb (BLOCK_FOR_INSN (rtl));
1260 changed_cfg = true;
1261 }
1262 }
1263 }
1264
1265 unsigned int index;
1266 bitmap_iterator bi;
1267 EXECUTE_IF_SET_IN_BITMAP (m_need_to_purge_dead_edges, 0, index, bi)
1268 if (purge_dead_edges (BASIC_BLOCK_FOR_FN (m_fn, index)))
1269 changed_cfg = true;
1270
1271 if (changed_jumps)
1272 // This uses its own timevar internally, so we don't need to push
1273 // one ourselves.
1274 rebuild_jump_labels (get_insns ());
1275
1276 bitmap_clear (m_need_to_purge_dead_edges);
1277 bitmap_clear (m_queued_insn_update_uids);
1278 m_queued_insn_updates.truncate (0);
1279
1280 if (changed_cfg)
1281 {
1282 free_dominance_info (CDI_DOMINATORS);
1283 free_dominance_info (CDI_POST_DOMINATORS);
1284 }
1285
1286 return changed_cfg;
1287 }
1288
1289 insn_info *
1290 function_info::create_insn (obstack_watermark &watermark,
1291 rtx_code insn_code,
1292 rtx pat)
1293 {
1294 rtx_insn *rti = nullptr;
1295
1296 // TODO: extend, move in to emit-rtl.cc.
1297 switch (insn_code)
1298 {
1299 case INSN:
1300 rti = make_insn_raw (pat);
1301 break;
1302 default:
1303 gcc_unreachable ();
1304 }
1305
1306 auto insn = change_alloc<insn_info> (watermark, nullptr, rti, INSN_UID (rti));
1307 insn->m_is_temp = true;
1308 return insn;
1309 }
1310
1311 // Print a description of CHANGE to PP.
1312 void
1313 rtl_ssa::pp_insn_change (pretty_printer *pp, const insn_change &change)
1314 {
1315 change.print (pp);
1316 }
1317
1318 // Print a description of CHANGE to FILE.
1319 void
1320 dump (FILE *file, const insn_change &change)
1321 {
1322 dump_using (file, pp_insn_change, change);
1323 }
1324
1325 // Debug interface to the dump routine above.
1326 void debug (const insn_change &x) { dump (stderr, x); }