hppa: Fix REG+D address support before reload
[official-gcc.git] / gcc / rtl-ssa / blocks.cc
blobcf4224e61ecf7dc91e3741993aa9732603a8d6d9
1 // Implementation of basic-block-related functions for RTL SSA -*- C++ -*-
2 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation; either version 3, or (at your option) any later
9 // version.
11 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 // for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #define INCLUDE_ALGORITHM
21 #define INCLUDE_FUNCTIONAL
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "df.h"
28 #include "rtl-ssa.h"
29 #include "rtl-ssa/internals.h"
30 #include "rtl-ssa/internals.inl"
31 #include "cfganal.h"
32 #include "cfgrtl.h"
33 #include "predict.h"
34 #include "domwalk.h"
36 using namespace rtl_ssa;
38 // Prepare to build information for a function in which all register numbers
39 // are less than NUM_REGS and all basic block indices are less than
40 // NUM_BB_INDICES
41 function_info::build_info::build_info (unsigned int num_regs,
42 unsigned int num_bb_indices)
43 : current_bb (nullptr),
44 current_ebb (nullptr),
45 last_access (num_regs + 1),
46 ebb_live_in_for_debug (nullptr),
47 potential_phi_regs (num_regs),
48 bb_phis (num_bb_indices),
49 bb_mem_live_out (num_bb_indices),
50 bb_to_rpo (num_bb_indices),
51 exit_block_dominator (nullptr)
53 last_access.safe_grow_cleared (num_regs + 1);
55 bitmap_clear (potential_phi_regs);
57 // These arrays shouldn't need to be initialized, since we'll always
58 // write to an entry before reading from it. But poison the contents
59 // when checking, just to make sure we don't accidentally use an
60 // uninitialized value.
61 bb_phis.quick_grow_cleared (num_bb_indices);
62 bb_mem_live_out.quick_grow (num_bb_indices);
63 bb_to_rpo.quick_grow (num_bb_indices);
64 if (flag_checking)
66 // Can't do this for bb_phis because it has a constructor.
67 memset (bb_mem_live_out.address (), 0xaf,
68 num_bb_indices * sizeof (bb_mem_live_out[0]));
69 memset (bb_to_rpo.address (), 0xaf,
70 num_bb_indices * sizeof (bb_to_rpo[0]));
73 // Start off with an empty set of phi nodes for each block.
74 for (bb_phi_info &info : bb_phis)
75 bitmap_initialize (&info.regs, &bitmap_default_obstack);
78 function_info::build_info::~build_info ()
80 for (bb_phi_info &info : bb_phis)
81 bitmap_release (&info.regs);
84 // A dom_walker for populating the basic blocks.
85 class function_info::bb_walker : public dom_walker
87 public:
88 bb_walker (function_info *, build_info &);
89 edge before_dom_children (basic_block) final override;
90 void after_dom_children (basic_block) final override;
92 private:
93 // Information about the function we're building.
94 function_info *m_function;
95 build_info &m_bi;
97 // We should treat the exit block as being the last child of this one.
98 // See the comment in the constructor for more information.
99 basic_block m_exit_block_dominator;
102 // Prepare to walk the blocks in FUNCTION using BI.
103 function_info::bb_walker::bb_walker (function_info *function, build_info &bi)
104 : dom_walker (CDI_DOMINATORS, ALL_BLOCKS, bi.bb_to_rpo.address ()),
105 m_function (function),
106 m_bi (bi),
107 m_exit_block_dominator (bi.exit_block_dominator)
109 // If the exit block is unreachable, process it last.
110 if (!m_exit_block_dominator)
111 m_exit_block_dominator = ENTRY_BLOCK_PTR_FOR_FN (m_function->m_fn);
114 edge
115 function_info::bb_walker::before_dom_children (basic_block bb)
117 m_function->start_block (m_bi, m_function->bb (bb));
118 return nullptr;
121 void
122 function_info::bb_walker::after_dom_children (basic_block bb)
124 // See the comment in the constructor for details.
125 if (bb == m_exit_block_dominator)
127 before_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
128 after_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
130 m_function->end_block (m_bi, m_function->bb (bb));
133 // See the comment above the declaration.
134 void
135 bb_info::print_identifier (pretty_printer *pp) const
137 char tmp[3 * sizeof (index ()) + 3];
138 snprintf (tmp, sizeof (tmp), "bb%d", index ());
139 pp_string (pp, tmp);
140 if (ebb_info *ebb = this->ebb ())
142 pp_space (pp);
143 pp_left_bracket (pp);
144 ebb->print_identifier (pp);
145 pp_right_bracket (pp);
149 // See the comment above the declaration.
150 void
151 bb_info::print_full (pretty_printer *pp) const
153 pp_string (pp, "basic block ");
154 print_identifier (pp);
155 pp_colon (pp);
157 auto print_insn = [pp](const char *header, const insn_info *insn)
159 pp_newline_and_indent (pp, 2);
160 pp_string (pp, header);
161 pp_newline_and_indent (pp, 2);
162 if (insn)
163 pp_insn (pp, insn);
164 else
165 pp_string (pp, "<uninitialized>");
166 pp_indentation (pp) -= 4;
169 print_insn ("head:", head_insn ());
171 pp_newline (pp);
172 pp_newline_and_indent (pp, 2);
173 pp_string (pp, "contents:");
174 if (!head_insn ())
176 pp_newline_and_indent (pp, 2);
177 pp_string (pp, "<uninitialized>");
178 pp_indentation (pp) -= 2;
180 else if (auto insns = real_insns ())
182 bool is_first = true;
183 for (const insn_info *insn : insns)
185 if (is_first)
186 is_first = false;
187 else
188 pp_newline (pp);
189 pp_newline_and_indent (pp, 2);
190 pp_insn (pp, insn);
191 pp_indentation (pp) -= 2;
194 else
196 pp_newline_and_indent (pp, 2);
197 pp_string (pp, "none");
198 pp_indentation (pp) -= 2;
200 pp_indentation (pp) -= 2;
202 pp_newline (pp);
203 print_insn ("end:", end_insn ());
206 // See the comment above the declaration.
207 void
208 ebb_call_clobbers_info::print_summary (pretty_printer *pp) const
210 pp_string (pp, "call clobbers for ABI ");
211 if (m_abi)
212 pp_decimal_int (pp, m_abi->id ());
213 else
214 pp_string (pp, "<null>");
217 // See the comment above the declaration.
218 void
219 ebb_call_clobbers_info::print_full (pretty_printer *pp) const
221 print_summary (pp);
222 pp_colon (pp);
223 pp_newline_and_indent (pp, 2);
224 auto print_node = [](pretty_printer *pp,
225 const insn_call_clobbers_note *note)
227 if (insn_info *insn = note->insn ())
228 insn->print_identifier_and_location (pp);
229 else
230 pp_string (pp, "<null>");
232 print (pp, root (), print_node);
233 pp_indentation (pp) -= 2;
236 // See the comment above the declaration.
237 void
238 ebb_info::print_identifier (pretty_printer *pp) const
240 // first_bb is populated by the constructor and so should always
241 // be nonnull.
242 auto index = first_bb ()->index ();
243 char tmp[3 * sizeof (index) + 4];
244 snprintf (tmp, sizeof (tmp), "ebb%d", index);
245 pp_string (pp, tmp);
248 // See the comment above the declaration.
249 void
250 ebb_info::print_full (pretty_printer *pp) const
252 pp_string (pp, "extended basic block ");
253 print_identifier (pp);
254 pp_colon (pp);
256 pp_newline_and_indent (pp, 2);
257 if (insn_info *phi_insn = this->phi_insn ())
259 phi_insn->print_identifier_and_location (pp);
260 pp_colon (pp);
261 if (auto phis = this->phis ())
263 bool is_first = true;
264 for (const phi_info *phi : phis)
266 if (is_first)
267 is_first = false;
268 else
269 pp_newline (pp);
270 pp_newline_and_indent (pp, 2);
271 pp_access (pp, phi, PP_ACCESS_SETTER);
272 pp_indentation (pp) -= 2;
275 else
277 pp_newline_and_indent (pp, 2);
278 pp_string (pp, "no phi nodes");
279 pp_indentation (pp) -= 2;
282 else
283 pp_string (pp, "no phi insn");
284 pp_indentation (pp) -= 2;
286 for (const bb_info *bb : bbs ())
288 pp_newline (pp);
289 pp_newline_and_indent (pp, 2);
290 pp_bb (pp, bb);
291 pp_indentation (pp) -= 2;
294 for (ebb_call_clobbers_info *ecc : call_clobbers ())
296 pp_newline (pp);
297 pp_newline_and_indent (pp, 2);
298 pp_ebb_call_clobbers (pp, ecc);
299 pp_indentation (pp) -= 2;
303 // Add a dummy use to mark that DEF is live out of BB's EBB at the end of BB.
304 void
305 function_info::add_live_out_use (bb_info *bb, set_info *def)
307 // There is nothing to do if DEF is an artificial definition at the end
308 // of BB. In that case the definitino is rooted at the end of the block
309 // and we wouldn't gain anything by inserting a use immediately after it.
310 // If we did want to insert a use, we'd need to associate it with a new
311 // instruction that comes after bb->end_insn ().
312 if (def->insn () == bb->end_insn ())
313 return;
315 // If the end of the block already has an artificial use, that use
316 // acts to make DEF live at the appropriate point.
317 use_info *use = def->last_nondebug_insn_use ();
318 if (use && use->insn () == bb->end_insn ())
319 return;
321 // Currently there is no need to maintain a backward link from the end
322 // instruction to the list of live-out uses. Such a list would be
323 // expensive to update if it was represented using the usual insn_info
324 // access arrays.
325 use = allocate<use_info> (bb->end_insn (), def->resource (), def);
326 use->set_is_live_out_use (true);
327 add_use (use);
330 // Return true if all nondebug uses of DEF are live-out uses.
331 static bool
332 all_uses_are_live_out_uses (set_info *def)
334 for (use_info *use : def->all_uses ())
335 if (!use->is_in_debug_insn () && !use->is_live_out_use ())
336 return false;
337 return true;
340 // SET, if nonnull, is a definition of something that is live out from BB.
341 // Return the live-out value itself.
342 set_info *
343 function_info::live_out_value (bb_info *bb, set_info *set)
345 // Degenerate phis only exist to provide a definition for uses in the
346 // same EBB. The live-out value is the same as the live-in value.
347 if (auto *phi = safe_dyn_cast<phi_info *> (set))
348 if (phi->is_degenerate ())
350 set = phi->input_value (0);
352 // Remove the phi if it turned out to be useless. This is
353 // mainly useful for memory, because we don't know ahead of time
354 // whether a block will use memory or not.
355 if (bb == bb->ebb ()->last_bb () && all_uses_are_live_out_uses (phi))
356 replace_phi (phi, set);
359 return set;
362 // Add PHI to EBB and enter it into the function's hash table.
363 void
364 function_info::append_phi (ebb_info *ebb, phi_info *phi)
366 phi_info *first_phi = ebb->first_phi ();
367 if (first_phi)
368 first_phi->set_prev_phi (phi);
369 phi->set_next_phi (first_phi);
370 ebb->set_first_phi (phi);
371 add_def (phi);
374 // Remove PHI from its current position in the SSA graph.
375 void
376 function_info::remove_phi (phi_info *phi)
378 phi_info *next = phi->next_phi ();
379 phi_info *prev = phi->prev_phi ();
381 if (next)
382 next->set_prev_phi (prev);
384 if (prev)
385 prev->set_next_phi (next);
386 else
387 phi->ebb ()->set_first_phi (next);
389 remove_def (phi);
390 phi->clear_phi_links ();
393 // Remove PHI from the SSA graph and free its memory.
394 void
395 function_info::delete_phi (phi_info *phi)
397 gcc_assert (!phi->has_any_uses ());
399 // Remove the inputs to the phi.
400 for (use_info *input : phi->inputs ())
401 remove_use (input);
403 remove_phi (phi);
405 phi->set_next_phi (m_free_phis);
406 m_free_phis = phi;
409 // If possible, remove PHI and replace all uses with NEW_VALUE.
410 void
411 function_info::replace_phi (phi_info *phi, set_info *new_value)
413 auto update_use = [&](use_info *use)
415 remove_use (use);
416 use->set_def (new_value);
417 add_use (use);
420 if (new_value)
421 for (use_info *use : phi->nondebug_insn_uses ())
422 if (!use->is_live_out_use ())
424 // We need to keep the phi around for its local uses.
425 // Turn it into a degenerate phi, if it isn't already.
426 use_info *use = phi->input_use (0);
427 if (use->def () != new_value)
428 update_use (use);
430 if (phi->is_degenerate ())
431 return;
433 phi->make_degenerate (use);
435 // Redirect all phi users to NEW_VALUE.
436 while (use_info *phi_use = phi->last_phi_use ())
437 update_use (phi_use);
439 return;
442 // Replace the uses. We can discard uses that only existed for the
443 // sake of marking live-out values, since the resource is now transparent
444 // in the phi's EBB.
445 while (use_info *use = phi->last_use ())
446 if (use->is_live_out_use ())
447 remove_use (use);
448 else
449 update_use (use);
451 delete_phi (phi);
454 // Create and return a phi node for EBB. RESOURCE is the resource that
455 // the phi node sets (and thus that all the inputs set too). NUM_INPUTS
456 // is the number of inputs, which is 1 for a degenerate phi. INPUTS[I]
457 // is a set_info that gives the value of input I, or null if the value
458 // is either unknown or uninitialized. If NUM_INPUTS > 1, this array
459 // is allocated on the main obstack and can be reused for the use array.
461 // Add the created phi node to its basic block and enter it into the
462 // function's hash table.
463 phi_info *
464 function_info::create_phi (ebb_info *ebb, resource_info resource,
465 access_info **inputs, unsigned int num_inputs)
467 phi_info *phi = m_free_phis;
468 if (phi)
470 m_free_phis = phi->next_phi ();
471 *phi = phi_info (ebb->phi_insn (), resource, phi->uid ());
473 else
475 phi = allocate<phi_info> (ebb->phi_insn (), resource, m_next_phi_uid);
476 m_next_phi_uid += 1;
479 // Convert the array of set_infos into an array of use_infos. Also work
480 // out what mode the phi should have.
481 machine_mode new_mode = resource.mode;
482 for (unsigned int i = 0; i < num_inputs; ++i)
484 auto *input = safe_as_a<set_info *> (inputs[i]);
485 auto *use = allocate<use_info> (phi, resource, input);
486 add_use (use);
487 inputs[i] = use;
488 if (input)
489 new_mode = combine_modes (new_mode, input->mode ());
492 phi->set_inputs (use_array (inputs, num_inputs));
493 phi->set_mode (new_mode);
495 append_phi (ebb, phi);
497 return phi;
500 // Create and return a degenerate phi for EBB whose input comes from DEF.
501 // This is used in cases where DEF is known to be available on entry to
502 // EBB but was not previously used within it. If DEF is for a register,
503 // there are two cases:
505 // (1) DEF was already live on entry to EBB but was previously transparent
506 // within it.
508 // (2) DEF was not previously live on entry to EBB and is being made live
509 // by this update.
511 // At the moment, this function only handles the case in which EBB has a
512 // single predecessor block and DEF is defined in that block's EBB.
513 phi_info *
514 function_info::create_degenerate_phi (ebb_info *ebb, set_info *def)
516 // Allow the function to be called twice in succession for the same def.
517 def_lookup dl = find_def (def->resource (), ebb->phi_insn ());
518 if (set_info *set = dl.matching_set ())
519 return as_a<phi_info *> (set);
521 access_info *input = def;
522 phi_info *phi = create_phi (ebb, def->resource (), &input, 1);
523 if (def->is_reg ())
525 unsigned int regno = def->regno ();
527 // Find the single predecessor mentioned above.
528 basic_block pred_cfg_bb = single_pred (ebb->first_bb ()->cfg_bb ());
529 bb_info *pred_bb = this->bb (pred_cfg_bb);
531 if (!bitmap_set_bit (DF_LR_IN (ebb->first_bb ()->cfg_bb ()), regno))
533 // The register was not previously live on entry to EBB and
534 // might not have been live on exit from PRED_BB either.
535 if (bitmap_set_bit (DF_LR_OUT (pred_cfg_bb), regno))
536 add_live_out_use (pred_bb, def);
538 else
540 // The register was previously live in to EBB. Add live-out uses
541 // at the appropriate points.
542 insn_info *next_insn = nullptr;
543 if (def_info *next_def = phi->next_def ())
544 next_insn = next_def->insn ();
545 for (bb_info *bb : ebb->bbs ())
547 if ((next_insn && *next_insn <= *bb->end_insn ())
548 || !bitmap_bit_p (DF_LR_OUT (bb->cfg_bb ()), regno))
549 break;
550 add_live_out_use (bb, def);
554 return phi;
557 // Create a bb_info for CFG_BB, given that no such structure currently exists.
558 bb_info *
559 function_info::create_bb_info (basic_block cfg_bb)
561 bb_info *bb = allocate<bb_info> (cfg_bb);
562 gcc_checking_assert (!m_bbs[cfg_bb->index]);
563 m_bbs[cfg_bb->index] = bb;
564 return bb;
567 // Add BB to the end of the list of blocks.
568 void
569 function_info::append_bb (bb_info *bb)
571 if (m_last_bb)
572 m_last_bb->set_next_bb (bb);
573 else
574 m_first_bb = bb;
575 bb->set_prev_bb (m_last_bb);
576 m_last_bb = bb;
579 // Calculate BI.potential_phi_regs and BI.potential_phi_regs_for_debug.
580 void
581 function_info::calculate_potential_phi_regs (build_info &bi)
583 auto *lr_info = DF_LR_BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
584 bool is_debug = MAY_HAVE_DEBUG_INSNS;
585 for (unsigned int regno = 0; regno < m_num_regs; ++regno)
586 if (regno >= DF_REG_SIZE (DF)
587 // Exclude registers that have a single definition that dominates
588 // all uses. If the definition does not dominate all uses,
589 // the register will be exposed upwards to the entry block but
590 // will not be defined by the entry block.
591 || DF_REG_DEF_COUNT (regno) > 1
592 || (!bitmap_bit_p (&lr_info->def, regno)
593 && bitmap_bit_p (&lr_info->out, regno)))
595 bitmap_set_bit (bi.potential_phi_regs, regno);
596 if (is_debug)
597 bitmap_set_bit (bi.potential_phi_regs_for_debug, regno);
601 // Called while building SSA form using BI. Decide where phi nodes
602 // should be placed for each register and initialize BI.bb_phis accordingly.
603 void
604 function_info::place_phis (build_info &bi)
606 unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
608 // Calculate dominance frontiers.
609 auto_vec<bitmap_head> frontiers;
610 frontiers.safe_grow_cleared (num_bb_indices);
611 for (unsigned int i = 0; i < num_bb_indices; ++i)
612 bitmap_initialize (&frontiers[i], &bitmap_default_obstack);
613 compute_dominance_frontiers (frontiers.address ());
615 // The normal dominance information doesn't calculate dominators for
616 // the exit block, so we don't get dominance frontiers for them either.
617 // Calculate them by hand.
618 for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
620 basic_block bb = e->src;
621 while (bb != bi.exit_block_dominator)
623 bitmap_set_bit (&frontiers[bb->index], EXIT_BLOCK);
624 bb = get_immediate_dominator (CDI_DOMINATORS, bb);
628 // In extreme cases, the number of live-in registers can be much
629 // greater than the number of phi nodes needed in a block (see PR98863).
630 // Try to reduce the number of operations involving live-in sets by using
631 // PENDING as a staging area: registers in PENDING need phi nodes if
632 // they are live on entry to the corresponding block, but do not need
633 // phi nodes otherwise.
634 auto_vec<bitmap_head> unfiltered;
635 unfiltered.safe_grow_cleared (num_bb_indices);
636 for (unsigned int i = 0; i < num_bb_indices; ++i)
637 bitmap_initialize (&unfiltered[i], &bitmap_default_obstack);
639 // If block B1 defines R and if B2 is in the dominance frontier of B1,
640 // queue a possible phi node for R in B2.
641 auto_bitmap worklist;
642 for (unsigned int b1 = 0; b1 < num_bb_indices; ++b1)
644 // Only access DF information for blocks that are known to exist.
645 if (bitmap_empty_p (&frontiers[b1]))
646 continue;
648 // Defs in B1 that are possibly in LR_IN in the dominance frontier
649 // blocks.
650 auto_bitmap b1_def;
651 bitmap_and (b1_def, &DF_LR_BB_INFO (BASIC_BLOCK_FOR_FN (m_fn, b1))->def,
652 DF_LR_OUT (BASIC_BLOCK_FOR_FN (m_fn, b1)));
654 bitmap_iterator bmi;
655 unsigned int b2;
656 EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
657 if (bitmap_ior_into (&unfiltered[b2], b1_def)
658 && !bitmap_empty_p (&frontiers[b2]))
659 // Propagate the (potential) new phi node definitions in B2.
660 bitmap_set_bit (worklist, b2);
663 while (!bitmap_empty_p (worklist))
665 unsigned int b1 = bitmap_first_set_bit (worklist);
666 bitmap_clear_bit (worklist, b1);
668 // Restrict the phi nodes to registers that are live on entry to
669 // the block.
670 bitmap b1_in = DF_LR_IN (BASIC_BLOCK_FOR_FN (m_fn, b1));
671 bitmap b1_phis = &bi.bb_phis[b1].regs;
672 if (!bitmap_ior_and_into (b1_phis, &unfiltered[b1], b1_in))
673 continue;
675 // If block B1 has a phi node for R and if B2 is in the dominance
676 // frontier of B1, queue a possible phi node for R in B2.
677 bitmap_iterator bmi;
678 unsigned int b2;
679 EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
680 if (bitmap_ior_into (&unfiltered[b2], b1_phis)
681 && !bitmap_empty_p (&frontiers[b2]))
682 bitmap_set_bit (worklist, b2);
685 basic_block cfg_bb;
686 FOR_ALL_BB_FN (cfg_bb, m_fn)
688 // Calculate the set of phi nodes for blocks that don't have any
689 // dominance frontiers. We only need to do this once per block.
690 unsigned int i = cfg_bb->index;
691 bb_phi_info &phis = bi.bb_phis[i];
692 if (bitmap_empty_p (&frontiers[i]))
693 bitmap_and (&phis.regs, &unfiltered[i], DF_LR_IN (cfg_bb));
695 // Create an array that contains all phi inputs for this block.
696 // See the comment above the member variables for more information.
697 phis.num_phis = bitmap_count_bits (&phis.regs);
698 phis.num_preds = EDGE_COUNT (cfg_bb->preds);
699 unsigned int num_inputs = phis.num_phis * phis.num_preds;
700 if (num_inputs != 0)
702 phis.inputs = XOBNEWVEC (&m_temp_obstack, set_info *, num_inputs);
703 memset (phis.inputs, 0, num_inputs * sizeof (phis.inputs[0]));
707 // Free the temporary bitmaps.
708 for (unsigned int i = 0; i < num_bb_indices; ++i)
710 bitmap_release (&frontiers[i]);
711 bitmap_release (&unfiltered[i]);
715 // Called while building SSA form using BI, with BI.current_bb being
716 // the entry block.
718 // Create the entry block instructions and their definitions. The only
719 // useful instruction is the end instruction, which carries definitions
720 // for the values that are live on entry to the function. However, it
721 // seems simpler to create a head instruction too, rather than force all
722 // users of the block information to treat the entry block as a special case.
723 void
724 function_info::add_entry_block_defs (build_info &bi)
726 bb_info *bb = bi.current_bb;
727 basic_block cfg_bb = bi.current_bb->cfg_bb ();
728 auto *lr_info = DF_LR_BB_INFO (cfg_bb);
730 bb->set_head_insn (append_artificial_insn (bb));
731 insn_info *insn = append_artificial_insn (bb);
732 bb->set_end_insn (insn);
734 start_insn_accesses ();
736 // Using LR to derive the liveness information means that we create an
737 // entry block definition for upwards exposed registers. These registers
738 // are sometimes genuinely uninitialized. However, some targets also
739 // create a pseudo PIC base register and only initialize it later.
740 // Handling that case correctly seems more important than optimizing
741 // uninitialized uses.
742 unsigned int regno;
743 bitmap_iterator in_bi;
744 EXECUTE_IF_SET_IN_BITMAP (&lr_info->out, 0, regno, in_bi)
746 auto *set = allocate<set_info> (insn, full_register (regno));
747 append_def (set);
748 m_temp_defs.safe_push (set);
749 bi.record_reg_def (set);
752 // Create a definition that reflects the state of memory on entry to
753 // the function.
754 auto *set = allocate<set_info> (insn, memory);
755 append_def (set);
756 m_temp_defs.safe_push (set);
757 bi.record_mem_def (set);
759 finish_insn_accesses (insn);
762 // Lazily calculate the value of BI.ebb_live_in_for_debug for BI.current_ebb.
763 void
764 function_info::calculate_ebb_live_in_for_debug (build_info &bi)
766 gcc_checking_assert (bitmap_empty_p (bi.tmp_ebb_live_in_for_debug));
767 bi.ebb_live_in_for_debug = bi.tmp_ebb_live_in_for_debug;
768 bitmap_and (bi.ebb_live_in_for_debug, bi.potential_phi_regs_for_debug,
769 DF_LR_IN (bi.current_ebb->first_bb ()->cfg_bb ()));
770 bitmap_tree_view (bi.ebb_live_in_for_debug);
773 // Called while building SSA form using BI. Create phi nodes for the
774 // current EBB.
775 void
776 function_info::add_phi_nodes (build_info &bi)
778 ebb_info *ebb = bi.current_ebb;
779 basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
781 // Create the register phis for this EBB.
782 bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
783 unsigned int num_preds = phis.num_preds;
784 unsigned int regno;
785 bitmap_iterator in_bi;
786 EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, in_bi)
788 gcc_checking_assert (bitmap_bit_p (bi.potential_phi_regs, regno));
790 // Create an array of phi inputs, to be filled in later.
791 auto *inputs = XOBNEWVEC (&m_obstack, access_info *, num_preds);
792 memset (inputs, 0, sizeof (access_info *) * num_preds);
794 // Later code works out the correct mode of the phi. Use BLKmode
795 // as a placeholder for now.
796 phi_info *phi = create_phi (ebb, { E_BLKmode, regno },
797 inputs, num_preds);
798 bi.record_reg_def (phi);
801 bitmap_copy (bi.ebb_def_regs, &phis.regs);
803 // Collect the live-in memory definitions and record whether they're
804 // all the same.
805 m_temp_defs.reserve (num_preds);
806 set_info *mem_value = nullptr;
807 bool mem_phi_is_degenerate = true;
808 edge e;
809 edge_iterator ei;
810 FOR_EACH_EDGE (e, ei, cfg_bb->preds)
812 bb_info *pred_bb = this->bb (e->src);
813 if (pred_bb && pred_bb->head_insn ())
815 mem_value = bi.bb_mem_live_out[pred_bb->index ()];
816 m_temp_defs.quick_push (mem_value);
817 if (mem_value != m_temp_defs[0])
818 mem_phi_is_degenerate = false;
820 else
822 m_temp_defs.quick_push (nullptr);
823 mem_phi_is_degenerate = false;
827 // Create a phi for memory, on the assumption that something in the
828 // EBB will need it.
829 if (mem_phi_is_degenerate)
831 access_info *input[] = { mem_value };
832 mem_value = create_phi (ebb, memory, input, 1);
834 else
836 obstack_grow (&m_obstack, m_temp_defs.address (),
837 num_preds * sizeof (access_info *));
838 auto *inputs = static_cast<access_info **> (obstack_finish (&m_obstack));
839 mem_value = create_phi (ebb, memory, inputs, num_preds);
841 bi.record_mem_def (mem_value);
842 m_temp_defs.truncate (0);
845 // Called while building SSA form using BI.
847 // If FLAGS is DF_REF_AT_TOP, create the head insn for BI.current_bb
848 // and populate its uses and definitions. If FLAGS is 0, do the same
849 // for the end insn.
850 void
851 function_info::add_artificial_accesses (build_info &bi, df_ref_flags flags)
853 bb_info *bb = bi.current_bb;
854 basic_block cfg_bb = bb->cfg_bb ();
855 auto *lr_info = DF_LR_BB_INFO (cfg_bb);
856 df_ref ref;
858 insn_info *insn;
859 if (flags == DF_REF_AT_TOP)
861 if (cfg_bb->index == EXIT_BLOCK)
862 insn = append_artificial_insn (bb);
863 else
864 insn = append_artificial_insn (bb, bb_note (cfg_bb));
865 bb->set_head_insn (insn);
867 else
869 insn = append_artificial_insn (bb);
870 bb->set_end_insn (insn);
873 start_insn_accesses ();
875 HARD_REG_SET added_regs = {};
876 FOR_EACH_ARTIFICIAL_USE (ref, cfg_bb->index)
877 if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
879 unsigned int regno = DF_REF_REGNO (ref);
880 machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
881 if (HARD_REGISTER_NUM_P (regno))
882 SET_HARD_REG_BIT (added_regs, regno);
884 // A definition must be available.
885 gcc_checking_assert (bitmap_bit_p (&lr_info->in, regno)
886 || (flags != DF_REF_AT_TOP
887 && bitmap_bit_p (&lr_info->def, regno)));
888 m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, regno }));
891 // Ensure that global registers and memory are live at the end of any
892 // block that has no successors, such as the exit block and non-local gotos.
893 // Global registers have to be singled out because they are not part of
894 // the DF artifical use list (they are instead treated as used within
895 // every block).
896 if (flags == 0 && EDGE_COUNT (cfg_bb->succs) == 0)
898 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
899 if (global_regs[i] && !TEST_HARD_REG_BIT (added_regs, i))
901 auto mode = reg_raw_mode[i];
902 m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, i }));
905 auto *use = allocate<use_info> (insn, memory, bi.current_mem_value ());
906 add_use (use);
907 m_temp_uses.safe_push (use);
910 FOR_EACH_ARTIFICIAL_DEF (ref, cfg_bb->index)
911 if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
913 unsigned int regno = DF_REF_REGNO (ref);
914 machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
915 resource_info resource { mode, regno };
917 // We rely on the def set being correct.
918 gcc_checking_assert (bitmap_bit_p (&lr_info->def, regno));
920 // If the value isn't used later in the block and isn't live
921 // on exit, we could instead represent the definition as a
922 // clobber_info. However, that case should be relatively
923 // rare and set_info is any case more compact than clobber_info.
924 set_info *def = allocate<set_info> (insn, resource);
925 append_def (def);
926 m_temp_defs.safe_push (def);
927 bi.record_reg_def (def);
930 // Model the effect of a memory clobber on an incoming edge by adding
931 // a fake definition of memory at the start of the block. We don't need
932 // to add a use of the phi node because memory is implicitly always live.
933 if (flags == DF_REF_AT_TOP && has_abnormal_call_or_eh_pred_edge_p (cfg_bb))
935 set_info *def = allocate<set_info> (insn, memory);
936 append_def (def);
937 m_temp_defs.safe_push (def);
938 bi.record_mem_def (def);
941 finish_insn_accesses (insn);
944 // Called while building SSA form using BI. Create insn_infos for all
945 // relevant instructions in BI.current_bb.
946 void
947 function_info::add_block_contents (build_info &bi)
949 basic_block cfg_bb = bi.current_bb->cfg_bb ();
950 rtx_insn *insn;
951 FOR_BB_INSNS (cfg_bb, insn)
952 if (INSN_P (insn))
953 add_insn_to_block (bi, insn);
956 // Called while building SSA form using BI. Record live-out register values
957 // in the phi inputs of successor blocks and create live-out uses where
958 // appropriate. Record the live-out memory value in BI.bb_mem_live_out.
959 void
960 function_info::record_block_live_out (build_info &bi)
962 bb_info *bb = bi.current_bb;
963 ebb_info *ebb = bi.current_ebb;
964 basic_block cfg_bb = bb->cfg_bb ();
966 // Record the live-out register values in the phi inputs of
967 // successor blocks.
968 edge e;
969 edge_iterator ei;
970 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
972 bb_phi_info &phis = bi.bb_phis[e->dest->index];
973 unsigned int input_i = e->dest_idx * phis.num_phis;
974 unsigned int regno;
975 bitmap_iterator out_bi;
976 EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, out_bi)
978 phis.inputs[input_i]
979 = live_out_value (bb, bi.current_reg_value (regno));
980 input_i += 1;
984 // Add the set of registers that were defined in this BB to the set
985 // of potentially-live registers defined in the EBB.
986 bitmap_ior_into (bi.ebb_def_regs, &DF_LR_BB_INFO (cfg_bb)->def);
988 // Iterate through the registers in LIVE_OUT and see whether we need
989 // to add a live-out use for them.
990 auto record_live_out_regs = [&](bitmap live_out)
992 unsigned int regno;
993 bitmap_iterator out_bi;
994 EXECUTE_IF_AND_IN_BITMAP (bi.ebb_def_regs, live_out, 0, regno, out_bi)
996 set_info *value = live_out_value (bb, bi.current_reg_value (regno));
997 if (value && value->ebb () == ebb)
998 add_live_out_use (bb, value);
1002 if (bb == ebb->last_bb ())
1003 // All live-out registers might need live-out uses.
1004 record_live_out_regs (DF_LR_OUT (cfg_bb));
1005 else
1006 // Registers might need live-out uses if they are live on entry
1007 // to a successor block in a different EBB.
1008 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
1010 bb_info *dest_bb = this->bb (e->dest);
1011 if (dest_bb->ebb () != ebb || dest_bb == ebb->first_bb ())
1012 record_live_out_regs (DF_LR_IN (e->dest));
1015 // Record the live-out memory value.
1016 bi.bb_mem_live_out[cfg_bb->index]
1017 = live_out_value (bb, bi.current_mem_value ());
1020 // Add BB and its contents to the SSA information.
1021 void
1022 function_info::start_block (build_info &bi, bb_info *bb)
1024 ebb_info *ebb = bb->ebb ();
1026 // We (need to) add all blocks from one EBB before moving on to the next.
1027 bi.current_bb = bb;
1028 if (bb == ebb->first_bb ())
1029 bi.current_ebb = ebb;
1030 else
1031 gcc_assert (bi.current_ebb == ebb);
1033 // Record the start of this block's definitions in the definitions stack.
1034 bi.old_def_stack_limit.safe_push (bi.def_stack.length ());
1036 // Add the block itself.
1037 append_bb (bb);
1039 // If the block starts an EBB, create the phi insn. This insn should exist
1040 // for all EBBs, even if they don't (yet) need phis.
1041 if (bb == ebb->first_bb ())
1042 ebb->set_phi_insn (append_artificial_insn (bb));
1044 if (bb->index () == ENTRY_BLOCK)
1046 add_entry_block_defs (bi);
1047 record_block_live_out (bi);
1048 return;
1051 if (EDGE_COUNT (bb->cfg_bb ()->preds) == 0)
1053 // Leave unreachable blocks empty, since there is no useful
1054 // liveness information for them, and anything they do will
1055 // be wasted work. In a cleaned-up cfg, the only unreachable
1056 // block we should see is the exit block of a noreturn function.
1057 bb->set_head_insn (append_artificial_insn (bb));
1058 bb->set_end_insn (append_artificial_insn (bb));
1059 return;
1062 // If the block starts an EBB, create the phi nodes.
1063 if (bb == ebb->first_bb ())
1064 add_phi_nodes (bi);
1066 // Process the contents of the block.
1067 add_artificial_accesses (bi, DF_REF_AT_TOP);
1068 if (bb->index () != EXIT_BLOCK)
1069 add_block_contents (bi);
1070 add_artificial_accesses (bi, df_ref_flags ());
1071 record_block_live_out (bi);
1073 // If we needed to calculate a live-in set for debug purposes,
1074 // reset it to null at the end of the EBB. Convert the underlying
1075 // bitmap to an empty list view, ready for the next calculation.
1076 if (bi.ebb_live_in_for_debug && bb == ebb->last_bb ())
1078 bitmap_clear (bi.tmp_ebb_live_in_for_debug);
1079 bitmap_list_view (bi.tmp_ebb_live_in_for_debug);
1080 bi.ebb_live_in_for_debug = nullptr;
1084 // Finish adding BB and the blocks that it dominates to the SSA information.
1085 void
1086 function_info::end_block (build_info &bi, bb_info *bb)
1088 // Restore the register last_access information to the state it was
1089 // in before we started processing BB.
1090 unsigned int old_limit = bi.old_def_stack_limit.pop ();
1091 while (bi.def_stack.length () > old_limit)
1093 // We pushed a definition in BB if it was the first dominating
1094 // definition (and so the previous entry was null). In other
1095 // cases we pushed the previous dominating definition.
1096 def_info *def = bi.def_stack.pop ();
1097 unsigned int regno = def->regno ();
1098 if (def->bb () == bb)
1099 def = nullptr;
1100 bi.last_access[regno + 1] = def;
1104 // Finish setting up the phi nodes for each block, now that we've added
1105 // the contents of all blocks.
1106 void
1107 function_info::populate_phi_inputs (build_info &bi)
1109 auto_vec<phi_info *, 32> sorted_phis;
1110 for (ebb_info *ebb : ebbs ())
1112 if (!ebb->first_phi ())
1113 continue;
1115 // Get a sorted array of EBB's phi nodes.
1116 basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
1117 bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
1118 sorted_phis.truncate (0);
1119 for (phi_info *phi : ebb->phis ())
1120 sorted_phis.safe_push (phi);
1121 std::sort (sorted_phis.address (),
1122 sorted_phis.address () + sorted_phis.length (),
1123 compare_access_infos);
1125 // Set the inputs of the non-degenerate register phis. All inputs
1126 // for one edge come before all inputs for the next edge.
1127 set_info **inputs = phis.inputs;
1128 unsigned int phi_i = 0;
1129 bitmap_iterator bmi;
1130 unsigned int regno;
1131 EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, bmi)
1133 // Skip intervening degenerate phis.
1134 while (sorted_phis[phi_i]->regno () < regno)
1135 phi_i += 1;
1136 phi_info *phi = sorted_phis[phi_i];
1137 gcc_assert (phi->regno () == regno);
1138 for (unsigned int input_i = 0; input_i < phis.num_preds; ++input_i)
1139 if (set_info *input = inputs[input_i * phis.num_phis])
1141 use_info *use = phi->input_use (input_i);
1142 gcc_assert (!use->def ());
1143 use->set_def (input);
1144 add_use (use);
1146 phi_i += 1;
1147 inputs += 1;
1150 // Fill in the backedge inputs to any memory phi.
1151 phi_info *mem_phi = sorted_phis.last ();
1152 if (mem_phi->is_mem () && !mem_phi->is_degenerate ())
1154 edge e;
1155 edge_iterator ei;
1156 FOR_EACH_EDGE (e, ei, cfg_bb->preds)
1158 use_info *use = mem_phi->input_use (e->dest_idx);
1159 if (!use->def ())
1161 use->set_def (bi.bb_mem_live_out[e->src->index]);
1162 add_use (use);
1169 // Return true if it would be better to continue an EBB across NEW_EDGE
1170 // rather than across OLD_EDGE, given that both edges are viable candidates.
1171 // This is not a total ordering.
1172 static bool
1173 better_ebb_edge_p (edge new_edge, edge old_edge)
1175 // Prefer the likeliest edge.
1176 if (new_edge->probability.initialized_p ()
1177 && old_edge->probability.initialized_p ()
1178 && !(old_edge->probability == new_edge->probability))
1179 return old_edge->probability < new_edge->probability;
1181 // If both edges are equally likely, prefer a fallthru edge.
1182 if (new_edge->flags & EDGE_FALLTHRU)
1183 return true;
1184 if (old_edge->flags & EDGE_FALLTHRU)
1185 return false;
1187 // Otherwise just stick with OLD_EDGE.
1188 return false;
1191 // Pick and return the next basic block in an EBB that currently ends with BB.
1192 // Return null if the EBB must end with BB.
1193 static basic_block
1194 choose_next_block_in_ebb (basic_block bb)
1196 // Although there's nothing in principle wrong with having an EBB that
1197 // starts with the entry block and includes later blocks, there's not
1198 // really much point either. Keeping the entry block separate means
1199 // that uses of arguments consistently occur through phi nodes, rather
1200 // than the arguments sometimes appearing to come from an EBB-local
1201 // definition instead.
1202 if (bb->index == ENTRY_BLOCK)
1203 return nullptr;
1205 bool optimize_for_speed_p = optimize_bb_for_speed_p (bb);
1206 edge best_edge = nullptr;
1207 edge e;
1208 edge_iterator ei;
1209 FOR_EACH_EDGE (e, ei, bb->succs)
1210 if (!(e->flags & EDGE_COMPLEX)
1211 && e->dest->index != EXIT_BLOCK
1212 && single_pred_p (e->dest)
1213 && optimize_for_speed_p == optimize_bb_for_speed_p (e->dest)
1214 && (!best_edge || better_ebb_edge_p (e, best_edge)))
1215 best_edge = e;
1217 return best_edge ? best_edge->dest : nullptr;
1220 // Partition the function into extended basic blocks. Create the
1221 // associated ebb_infos and bb_infos, but don't add the bb_infos
1222 // to the function list yet.
1223 void
1224 function_info::create_ebbs (build_info &bi)
1226 // Compute the starting reverse postorder. We tweak this later to try
1227 // to get better EBB assignments.
1228 auto *postorder = new int[n_basic_blocks_for_fn (m_fn)];
1229 unsigned int postorder_num
1230 = pre_and_rev_post_order_compute (nullptr, postorder, true);
1231 gcc_assert (int (postorder_num) <= n_basic_blocks_for_fn (m_fn));
1233 // Iterate over the blocks in reverse postorder. In cases where
1234 // multiple possible orders exist, prefer orders that chain blocks
1235 // together into EBBs. If multiple possible EBBs exist, try to pick
1236 // the ones that are most likely to be profitable.
1237 auto_vec<bb_info *, 16> bbs;
1238 unsigned int next_bb_index = 0;
1239 for (unsigned int i = 0; i < postorder_num; ++i)
1240 if (!m_bbs[postorder[i]])
1242 // Choose and create the blocks that should form the next EBB.
1243 basic_block cfg_bb = BASIC_BLOCK_FOR_FN (m_fn, postorder[i]);
1246 // Record the chosen block order in a new RPO.
1247 bi.bb_to_rpo[cfg_bb->index] = next_bb_index++;
1248 bbs.safe_push (create_bb_info (cfg_bb));
1249 cfg_bb = choose_next_block_in_ebb (cfg_bb);
1251 while (cfg_bb);
1253 // Create the EBB itself.
1254 auto *ebb = allocate<ebb_info> (bbs[0], bbs.last ());
1255 for (bb_info *bb : bbs)
1256 bb->set_ebb (ebb);
1257 bbs.truncate (0);
1260 delete[] postorder;
1263 // Partition the function's blocks into EBBs and build SSA form for all
1264 // EBBs in the function.
1265 void
1266 function_info::process_all_blocks ()
1268 auto temps = temp_watermark ();
1269 unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
1271 build_info bi (m_num_regs, num_bb_indices);
1273 // ??? There is no dominance information associated with the exit block,
1274 // so work out its immediate dominator using predecessor blocks.
1275 for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
1276 if (bi.exit_block_dominator)
1277 bi.exit_block_dominator
1278 = nearest_common_dominator (CDI_DOMINATORS,
1279 bi.exit_block_dominator, e->src);
1280 else
1281 bi.exit_block_dominator = e->src;
1283 calculate_potential_phi_regs (bi);
1284 create_ebbs (bi);
1285 place_phis (bi);
1286 bb_walker (this, bi).walk (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
1287 populate_phi_inputs (bi);
1289 if (flag_checking)
1291 // The definition stack should be empty and all register definitions
1292 // should be back in their original undefined state.
1293 gcc_assert (bi.def_stack.is_empty ()
1294 && bi.old_def_stack_limit.is_empty ());
1295 for (unsigned int regno = 0; regno < m_num_regs; ++regno)
1296 gcc_assert (!bi.last_access[regno + 1]);
1300 // Print a description of CALL_CLOBBERS to PP.
1301 void
1302 rtl_ssa::pp_ebb_call_clobbers (pretty_printer *pp,
1303 const ebb_call_clobbers_info *call_clobbers)
1305 if (!call_clobbers)
1306 pp_string (pp, "<null>");
1307 else
1308 call_clobbers->print_full (pp);
1311 // Print a description of BB to PP.
1312 void
1313 rtl_ssa::pp_bb (pretty_printer *pp, const bb_info *bb)
1315 if (!bb)
1316 pp_string (pp, "<null>");
1317 else
1318 bb->print_full (pp);
1321 // Print a description of EBB to PP
1322 void
1323 rtl_ssa::pp_ebb (pretty_printer *pp, const ebb_info *ebb)
1325 if (!ebb)
1326 pp_string (pp, "<null>");
1327 else
1328 ebb->print_full (pp);
1331 // Print a description of CALL_CLOBBERS to FILE.
1332 void
1333 dump (FILE *file, const ebb_call_clobbers_info *call_clobbers)
1335 dump_using (file, pp_ebb_call_clobbers, call_clobbers);
1338 // Print a description of BB to FILE.
1339 void
1340 dump (FILE *file, const bb_info *bb)
1342 dump_using (file, pp_bb, bb);
1345 // Print a description of EBB to FILE.
1346 void
1347 dump (FILE *file, const ebb_info *ebb)
1349 dump_using (file, pp_ebb, ebb);
1352 // Debug interfaces to the dump routines above.
1353 void debug (const ebb_call_clobbers_info *x) { dump (stderr, x); }
1354 void debug (const bb_info *x) { dump (stderr, x); }
1355 void debug (const ebb_info *x) { dump (stderr, x); }