Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / gcc / rtl-ssa / blocks.cc
blob1f9969d78d8812f87e58b4168bc97ab35be051fc
1 // Implementation of basic-block-related functions for RTL SSA -*- C++ -*-
2 // Copyright (C) 2020-2023 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)
52 last_access.safe_grow_cleared (num_regs + 1);
54 bitmap_clear (potential_phi_regs);
56 // These arrays shouldn't need to be initialized, since we'll always
57 // write to an entry before reading from it. But poison the contents
58 // when checking, just to make sure we don't accidentally use an
59 // uninitialized value.
60 bb_phis.quick_grow (num_bb_indices);
61 bb_mem_live_out.quick_grow (num_bb_indices);
62 bb_to_rpo.quick_grow (num_bb_indices);
63 if (flag_checking)
65 // Can't do this for bb_phis because it has a constructor.
66 memset (bb_mem_live_out.address (), 0xaf,
67 num_bb_indices * sizeof (bb_mem_live_out[0]));
68 memset (bb_to_rpo.address (), 0xaf,
69 num_bb_indices * sizeof (bb_to_rpo[0]));
72 // Start off with an empty set of phi nodes for each block.
73 for (bb_phi_info &info : bb_phis)
74 bitmap_initialize (&info.regs, &bitmap_default_obstack);
77 function_info::build_info::~build_info ()
79 for (bb_phi_info &info : bb_phis)
80 bitmap_release (&info.regs);
83 // A dom_walker for populating the basic blocks.
84 class function_info::bb_walker : public dom_walker
86 public:
87 bb_walker (function_info *, build_info &);
88 edge before_dom_children (basic_block) final override;
89 void after_dom_children (basic_block) final override;
91 private:
92 // Information about the function we're building.
93 function_info *m_function;
94 build_info &m_bi;
96 // We should treat the exit block as being the last child of this one.
97 // See the comment in the constructor for more information.
98 basic_block m_exit_block_dominator;
101 // Prepare to walk the blocks in FUNCTION using BI.
102 function_info::bb_walker::bb_walker (function_info *function, build_info &bi)
103 : dom_walker (CDI_DOMINATORS, ALL_BLOCKS, bi.bb_to_rpo.address ()),
104 m_function (function),
105 m_bi (bi),
106 m_exit_block_dominator (nullptr)
108 // ??? There is no dominance information associated with the exit block,
109 // so work out its immediate dominator using predecessor blocks. We then
110 // walk the exit block just before popping its immediate dominator.
111 edge e;
112 edge_iterator ei;
113 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn)->preds)
114 if (m_exit_block_dominator)
115 m_exit_block_dominator
116 = nearest_common_dominator (CDI_DOMINATORS,
117 m_exit_block_dominator, e->src);
118 else
119 m_exit_block_dominator = e->src;
121 // If the exit block is unreachable, process it last.
122 if (!m_exit_block_dominator)
123 m_exit_block_dominator = ENTRY_BLOCK_PTR_FOR_FN (m_function->m_fn);
126 edge
127 function_info::bb_walker::before_dom_children (basic_block bb)
129 m_function->start_block (m_bi, m_function->bb (bb));
130 return nullptr;
133 void
134 function_info::bb_walker::after_dom_children (basic_block bb)
136 // See the comment in the constructor for details.
137 if (bb == m_exit_block_dominator)
139 before_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
140 after_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
142 m_function->end_block (m_bi, m_function->bb (bb));
145 // See the comment above the declaration.
146 void
147 bb_info::print_identifier (pretty_printer *pp) const
149 char tmp[3 * sizeof (index ()) + 3];
150 snprintf (tmp, sizeof (tmp), "bb%d", index ());
151 pp_string (pp, tmp);
152 if (ebb_info *ebb = this->ebb ())
154 pp_space (pp);
155 pp_left_bracket (pp);
156 ebb->print_identifier (pp);
157 pp_right_bracket (pp);
161 // See the comment above the declaration.
162 void
163 bb_info::print_full (pretty_printer *pp) const
165 pp_string (pp, "basic block ");
166 print_identifier (pp);
167 pp_colon (pp);
169 auto print_insn = [pp](const char *header, const insn_info *insn)
171 pp_newline_and_indent (pp, 2);
172 pp_string (pp, header);
173 pp_newline_and_indent (pp, 2);
174 if (insn)
175 pp_insn (pp, insn);
176 else
177 pp_string (pp, "<uninitialized>");
178 pp_indentation (pp) -= 4;
181 print_insn ("head:", head_insn ());
183 pp_newline (pp);
184 pp_newline_and_indent (pp, 2);
185 pp_string (pp, "contents:");
186 if (!head_insn ())
188 pp_newline_and_indent (pp, 2);
189 pp_string (pp, "<uninitialized>");
190 pp_indentation (pp) -= 2;
192 else if (auto insns = real_insns ())
194 bool is_first = true;
195 for (const insn_info *insn : insns)
197 if (is_first)
198 is_first = false;
199 else
200 pp_newline (pp);
201 pp_newline_and_indent (pp, 2);
202 pp_insn (pp, insn);
203 pp_indentation (pp) -= 2;
206 else
208 pp_newline_and_indent (pp, 2);
209 pp_string (pp, "none");
210 pp_indentation (pp) -= 2;
212 pp_indentation (pp) -= 2;
214 pp_newline (pp);
215 print_insn ("end:", end_insn ());
218 // See the comment above the declaration.
219 void
220 ebb_call_clobbers_info::print_summary (pretty_printer *pp) const
222 pp_string (pp, "call clobbers for ABI ");
223 if (m_abi)
224 pp_decimal_int (pp, m_abi->id ());
225 else
226 pp_string (pp, "<null>");
229 // See the comment above the declaration.
230 void
231 ebb_call_clobbers_info::print_full (pretty_printer *pp) const
233 print_summary (pp);
234 pp_colon (pp);
235 pp_newline_and_indent (pp, 2);
236 auto print_node = [](pretty_printer *pp,
237 const insn_call_clobbers_note *note)
239 if (insn_info *insn = note->insn ())
240 insn->print_identifier_and_location (pp);
241 else
242 pp_string (pp, "<null>");
244 print (pp, root (), print_node);
245 pp_indentation (pp) -= 2;
248 // See the comment above the declaration.
249 void
250 ebb_info::print_identifier (pretty_printer *pp) const
252 // first_bb is populated by the constructor and so should always
253 // be nonnull.
254 auto index = first_bb ()->index ();
255 char tmp[3 * sizeof (index) + 4];
256 snprintf (tmp, sizeof (tmp), "ebb%d", index);
257 pp_string (pp, tmp);
260 // See the comment above the declaration.
261 void
262 ebb_info::print_full (pretty_printer *pp) const
264 pp_string (pp, "extended basic block ");
265 print_identifier (pp);
266 pp_colon (pp);
268 pp_newline_and_indent (pp, 2);
269 if (insn_info *phi_insn = this->phi_insn ())
271 phi_insn->print_identifier_and_location (pp);
272 pp_colon (pp);
273 if (auto phis = this->phis ())
275 bool is_first = true;
276 for (const phi_info *phi : phis)
278 if (is_first)
279 is_first = false;
280 else
281 pp_newline (pp);
282 pp_newline_and_indent (pp, 2);
283 pp_access (pp, phi, PP_ACCESS_SETTER);
284 pp_indentation (pp) -= 2;
287 else
289 pp_newline_and_indent (pp, 2);
290 pp_string (pp, "no phi nodes");
291 pp_indentation (pp) -= 2;
294 else
295 pp_string (pp, "no phi insn");
296 pp_indentation (pp) -= 2;
298 for (const bb_info *bb : bbs ())
300 pp_newline (pp);
301 pp_newline_and_indent (pp, 2);
302 pp_bb (pp, bb);
303 pp_indentation (pp) -= 2;
306 for (ebb_call_clobbers_info *ecc : call_clobbers ())
308 pp_newline (pp);
309 pp_newline_and_indent (pp, 2);
310 pp_ebb_call_clobbers (pp, ecc);
311 pp_indentation (pp) -= 2;
315 // Add a dummy use to mark that DEF is live out of BB's EBB at the end of BB.
316 void
317 function_info::add_live_out_use (bb_info *bb, set_info *def)
319 // There is nothing to do if DEF is an artificial definition at the end
320 // of BB. In that case the definitino is rooted at the end of the block
321 // and we wouldn't gain anything by inserting a use immediately after it.
322 // If we did want to insert a use, we'd need to associate it with a new
323 // instruction that comes after bb->end_insn ().
324 if (def->insn () == bb->end_insn ())
325 return;
327 // If the end of the block already has an artificial use, that use
328 // acts to make DEF live at the appropriate point.
329 use_info *use = def->last_nondebug_insn_use ();
330 if (use && use->insn () == bb->end_insn ())
331 return;
333 // Currently there is no need to maintain a backward link from the end
334 // instruction to the list of live-out uses. Such a list would be
335 // expensive to update if it was represented using the usual insn_info
336 // access arrays.
337 use = allocate<use_info> (bb->end_insn (), def->resource (), def);
338 use->set_is_live_out_use (true);
339 add_use (use);
342 // Return true if all nondebug uses of DEF are live-out uses.
343 static bool
344 all_uses_are_live_out_uses (set_info *def)
346 for (use_info *use : def->all_uses ())
347 if (!use->is_in_debug_insn () && !use->is_live_out_use ())
348 return false;
349 return true;
352 // SET, if nonnull, is a definition of something that is live out from BB.
353 // Return the live-out value itself.
354 set_info *
355 function_info::live_out_value (bb_info *bb, set_info *set)
357 // Degenerate phis only exist to provide a definition for uses in the
358 // same EBB. The live-out value is the same as the live-in value.
359 if (auto *phi = safe_dyn_cast<phi_info *> (set))
360 if (phi->is_degenerate ())
362 set = phi->input_value (0);
364 // Remove the phi if it turned out to be useless. This is
365 // mainly useful for memory, because we don't know ahead of time
366 // whether a block will use memory or not.
367 if (bb == bb->ebb ()->last_bb () && all_uses_are_live_out_uses (phi))
368 replace_phi (phi, set);
371 return set;
374 // Add PHI to EBB and enter it into the function's hash table.
375 void
376 function_info::append_phi (ebb_info *ebb, phi_info *phi)
378 phi_info *first_phi = ebb->first_phi ();
379 if (first_phi)
380 first_phi->set_prev_phi (phi);
381 phi->set_next_phi (first_phi);
382 ebb->set_first_phi (phi);
383 add_def (phi);
386 // Remove PHI from its current position in the SSA graph.
387 void
388 function_info::remove_phi (phi_info *phi)
390 phi_info *next = phi->next_phi ();
391 phi_info *prev = phi->prev_phi ();
393 if (next)
394 next->set_prev_phi (prev);
396 if (prev)
397 prev->set_next_phi (next);
398 else
399 phi->ebb ()->set_first_phi (next);
401 remove_def (phi);
402 phi->clear_phi_links ();
405 // Remove PHI from the SSA graph and free its memory.
406 void
407 function_info::delete_phi (phi_info *phi)
409 gcc_assert (!phi->has_any_uses ());
411 // Remove the inputs to the phi.
412 for (use_info *input : phi->inputs ())
413 remove_use (input);
415 remove_phi (phi);
417 phi->set_next_phi (m_free_phis);
418 m_free_phis = phi;
421 // If possible, remove PHI and replace all uses with NEW_VALUE.
422 void
423 function_info::replace_phi (phi_info *phi, set_info *new_value)
425 auto update_use = [&](use_info *use)
427 remove_use (use);
428 use->set_def (new_value);
429 add_use (use);
432 if (new_value)
433 for (use_info *use : phi->nondebug_insn_uses ())
434 if (!use->is_live_out_use ())
436 // We need to keep the phi around for its local uses.
437 // Turn it into a degenerate phi, if it isn't already.
438 use_info *use = phi->input_use (0);
439 if (use->def () != new_value)
440 update_use (use);
442 if (phi->is_degenerate ())
443 return;
445 phi->make_degenerate (use);
447 // Redirect all phi users to NEW_VALUE.
448 while (use_info *phi_use = phi->last_phi_use ())
449 update_use (phi_use);
451 return;
454 // Replace the uses. We can discard uses that only existed for the
455 // sake of marking live-out values, since the resource is now transparent
456 // in the phi's EBB.
457 while (use_info *use = phi->last_use ())
458 if (use->is_live_out_use ())
459 remove_use (use);
460 else
461 update_use (use);
463 delete_phi (phi);
466 // Create and return a phi node for EBB. RESOURCE is the resource that
467 // the phi node sets (and thus that all the inputs set too). NUM_INPUTS
468 // is the number of inputs, which is 1 for a degenerate phi. INPUTS[I]
469 // is a set_info that gives the value of input I, or null if the value
470 // is either unknown or uninitialized. If NUM_INPUTS > 1, this array
471 // is allocated on the main obstack and can be reused for the use array.
473 // Add the created phi node to its basic block and enter it into the
474 // function's hash table.
475 phi_info *
476 function_info::create_phi (ebb_info *ebb, resource_info resource,
477 access_info **inputs, unsigned int num_inputs)
479 phi_info *phi = m_free_phis;
480 if (phi)
482 m_free_phis = phi->next_phi ();
483 *phi = phi_info (ebb->phi_insn (), resource, phi->uid ());
485 else
487 phi = allocate<phi_info> (ebb->phi_insn (), resource, m_next_phi_uid);
488 m_next_phi_uid += 1;
491 // Convert the array of set_infos into an array of use_infos. Also work
492 // out what mode the phi should have.
493 machine_mode new_mode = resource.mode;
494 for (unsigned int i = 0; i < num_inputs; ++i)
496 auto *input = safe_as_a<set_info *> (inputs[i]);
497 auto *use = allocate<use_info> (phi, resource, input);
498 add_use (use);
499 inputs[i] = use;
500 if (input)
501 new_mode = combine_modes (new_mode, input->mode ());
504 phi->set_inputs (use_array (inputs, num_inputs));
505 phi->set_mode (new_mode);
507 append_phi (ebb, phi);
509 return phi;
512 // Create and return a degenerate phi for EBB whose input comes from DEF.
513 // This is used in cases where DEF is known to be available on entry to
514 // EBB but was not previously used within it. If DEF is for a register,
515 // there are two cases:
517 // (1) DEF was already live on entry to EBB but was previously transparent
518 // within it.
520 // (2) DEF was not previously live on entry to EBB and is being made live
521 // by this update.
523 // At the moment, this function only handles the case in which EBB has a
524 // single predecessor block and DEF is defined in that block's EBB.
525 phi_info *
526 function_info::create_degenerate_phi (ebb_info *ebb, set_info *def)
528 access_info *input = def;
529 phi_info *phi = create_phi (ebb, def->resource (), &input, 1);
530 if (def->is_reg ())
532 unsigned int regno = def->regno ();
534 // Find the single predecessor mentioned above.
535 basic_block pred_cfg_bb = single_pred (ebb->first_bb ()->cfg_bb ());
536 bb_info *pred_bb = this->bb (pred_cfg_bb);
538 if (!bitmap_set_bit (DF_LR_IN (ebb->first_bb ()->cfg_bb ()), regno))
540 // The register was not previously live on entry to EBB and
541 // might not have been live on exit from PRED_BB either.
542 if (bitmap_set_bit (DF_LR_OUT (pred_cfg_bb), regno))
543 add_live_out_use (pred_bb, def);
545 else
547 // The register was previously live in to EBB. Add live-out uses
548 // at the appropriate points.
549 insn_info *next_insn = nullptr;
550 if (def_info *next_def = phi->next_def ())
551 next_insn = next_def->insn ();
552 for (bb_info *bb : ebb->bbs ())
554 if ((next_insn && *next_insn <= *bb->end_insn ())
555 || !bitmap_bit_p (DF_LR_OUT (bb->cfg_bb ()), regno))
556 break;
557 add_live_out_use (bb, def);
561 return phi;
564 // Create a bb_info for CFG_BB, given that no such structure currently exists.
565 bb_info *
566 function_info::create_bb_info (basic_block cfg_bb)
568 bb_info *bb = allocate<bb_info> (cfg_bb);
569 gcc_checking_assert (!m_bbs[cfg_bb->index]);
570 m_bbs[cfg_bb->index] = bb;
571 return bb;
574 // Add BB to the end of the list of blocks.
575 void
576 function_info::append_bb (bb_info *bb)
578 if (m_last_bb)
579 m_last_bb->set_next_bb (bb);
580 else
581 m_first_bb = bb;
582 bb->set_prev_bb (m_last_bb);
583 m_last_bb = bb;
586 // Calculate BI.potential_phi_regs and BI.potential_phi_regs_for_debug.
587 void
588 function_info::calculate_potential_phi_regs (build_info &bi)
590 auto *lr_info = DF_LR_BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
591 bool is_debug = MAY_HAVE_DEBUG_INSNS;
592 for (unsigned int regno = 0; regno < m_num_regs; ++regno)
593 if (regno >= DF_REG_SIZE (DF)
594 // Exclude registers that have a single definition that dominates
595 // all uses. If the definition does not dominate all uses,
596 // the register will be exposed upwards to the entry block but
597 // will not be defined by the entry block.
598 || DF_REG_DEF_COUNT (regno) > 1
599 || (!bitmap_bit_p (&lr_info->def, regno)
600 && bitmap_bit_p (&lr_info->out, regno)))
602 bitmap_set_bit (bi.potential_phi_regs, regno);
603 if (is_debug)
604 bitmap_set_bit (bi.potential_phi_regs_for_debug, regno);
608 // Called while building SSA form using BI. Decide where phi nodes
609 // should be placed for each register and initialize BI.bb_phis accordingly.
610 void
611 function_info::place_phis (build_info &bi)
613 unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
615 // Calculate dominance frontiers.
616 auto_vec<bitmap_head> frontiers;
617 frontiers.safe_grow (num_bb_indices);
618 for (unsigned int i = 0; i < num_bb_indices; ++i)
619 bitmap_initialize (&frontiers[i], &bitmap_default_obstack);
620 compute_dominance_frontiers (frontiers.address ());
622 // In extreme cases, the number of live-in registers can be much
623 // greater than the number of phi nodes needed in a block (see PR98863).
624 // Try to reduce the number of operations involving live-in sets by using
625 // PENDING as a staging area: registers in PENDING need phi nodes if
626 // they are live on entry to the corresponding block, but do not need
627 // phi nodes otherwise.
628 auto_vec<bitmap_head> unfiltered;
629 unfiltered.safe_grow (num_bb_indices);
630 for (unsigned int i = 0; i < num_bb_indices; ++i)
631 bitmap_initialize (&unfiltered[i], &bitmap_default_obstack);
633 // If block B1 defines R and if B2 is in the dominance frontier of B1,
634 // queue a possible phi node for R in B2.
635 auto_bitmap worklist;
636 for (unsigned int b1 = 0; b1 < num_bb_indices; ++b1)
638 // Only access DF information for blocks that are known to exist.
639 if (bitmap_empty_p (&frontiers[b1]))
640 continue;
642 bitmap b1_def = &DF_LR_BB_INFO (BASIC_BLOCK_FOR_FN (m_fn, b1))->def;
643 bitmap_iterator bmi;
644 unsigned int b2;
645 EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
646 if (bitmap_ior_into (&unfiltered[b2], b1_def)
647 && !bitmap_empty_p (&frontiers[b2]))
648 // Propagate the (potential) new phi node definitions in B2.
649 bitmap_set_bit (worklist, b2);
652 while (!bitmap_empty_p (worklist))
654 unsigned int b1 = bitmap_first_set_bit (worklist);
655 bitmap_clear_bit (worklist, b1);
657 // Restrict the phi nodes to registers that are live on entry to
658 // the block.
659 bitmap b1_in = DF_LR_IN (BASIC_BLOCK_FOR_FN (m_fn, b1));
660 bitmap b1_phis = &bi.bb_phis[b1].regs;
661 if (!bitmap_ior_and_into (b1_phis, &unfiltered[b1], b1_in))
662 continue;
664 // If block B1 has a phi node for R and if B2 is in the dominance
665 // frontier of B1, queue a possible phi node for R in B2.
666 bitmap_iterator bmi;
667 unsigned int b2;
668 EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
669 if (bitmap_ior_into (&unfiltered[b2], b1_phis)
670 && !bitmap_empty_p (&frontiers[b2]))
671 bitmap_set_bit (worklist, b2);
674 basic_block cfg_bb;
675 FOR_ALL_BB_FN (cfg_bb, m_fn)
677 // Calculate the set of phi nodes for blocks that don't have any
678 // dominance frontiers. We only need to do this once per block.
679 unsigned int i = cfg_bb->index;
680 bb_phi_info &phis = bi.bb_phis[i];
681 if (bitmap_empty_p (&frontiers[i]))
682 bitmap_and (&phis.regs, &unfiltered[i], DF_LR_IN (cfg_bb));
684 // Create an array that contains all phi inputs for this block.
685 // See the comment above the member variables for more information.
686 phis.num_phis = bitmap_count_bits (&phis.regs);
687 phis.num_preds = EDGE_COUNT (cfg_bb->preds);
688 unsigned int num_inputs = phis.num_phis * phis.num_preds;
689 if (num_inputs != 0)
691 phis.inputs = XOBNEWVEC (&m_temp_obstack, set_info *, num_inputs);
692 memset (phis.inputs, 0, num_inputs * sizeof (phis.inputs[0]));
696 // Free the temporary bitmaps.
697 for (unsigned int i = 0; i < num_bb_indices; ++i)
699 bitmap_release (&frontiers[i]);
700 bitmap_release (&unfiltered[i]);
704 // Called while building SSA form using BI, with BI.current_bb being
705 // the entry block.
707 // Create the entry block instructions and their definitions. The only
708 // useful instruction is the end instruction, which carries definitions
709 // for the values that are live on entry to the function. However, it
710 // seems simpler to create a head instruction too, rather than force all
711 // users of the block information to treat the entry block as a special case.
712 void
713 function_info::add_entry_block_defs (build_info &bi)
715 bb_info *bb = bi.current_bb;
716 basic_block cfg_bb = bi.current_bb->cfg_bb ();
717 auto *lr_info = DF_LR_BB_INFO (cfg_bb);
719 bb->set_head_insn (append_artificial_insn (bb));
720 insn_info *insn = append_artificial_insn (bb);
721 bb->set_end_insn (insn);
723 start_insn_accesses ();
725 // Using LR to derive the liveness information means that we create an
726 // entry block definition for upwards exposed registers. These registers
727 // are sometimes genuinely uninitialized. However, some targets also
728 // create a pseudo PIC base register and only initialize it later.
729 // Handling that case correctly seems more important than optimizing
730 // uninitialized uses.
731 unsigned int regno;
732 bitmap_iterator in_bi;
733 EXECUTE_IF_SET_IN_BITMAP (&lr_info->out, 0, regno, in_bi)
735 auto *set = allocate<set_info> (insn, full_register (regno));
736 append_def (set);
737 m_temp_defs.safe_push (set);
738 bi.record_reg_def (set);
741 // Create a definition that reflects the state of memory on entry to
742 // the function.
743 auto *set = allocate<set_info> (insn, memory);
744 append_def (set);
745 m_temp_defs.safe_push (set);
746 bi.record_mem_def (set);
748 finish_insn_accesses (insn);
751 // Lazily calculate the value of BI.ebb_live_in_for_debug for BI.current_ebb.
752 void
753 function_info::calculate_ebb_live_in_for_debug (build_info &bi)
755 gcc_checking_assert (bitmap_empty_p (bi.tmp_ebb_live_in_for_debug));
756 bi.ebb_live_in_for_debug = bi.tmp_ebb_live_in_for_debug;
757 bitmap_and (bi.ebb_live_in_for_debug, bi.potential_phi_regs_for_debug,
758 DF_LR_IN (bi.current_ebb->first_bb ()->cfg_bb ()));
759 bitmap_tree_view (bi.ebb_live_in_for_debug);
762 // Called while building SSA form using BI. Create phi nodes for the
763 // current EBB.
764 void
765 function_info::add_phi_nodes (build_info &bi)
767 ebb_info *ebb = bi.current_ebb;
768 basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
770 // Create the register phis for this EBB.
771 bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
772 unsigned int num_preds = phis.num_preds;
773 unsigned int regno;
774 bitmap_iterator in_bi;
775 EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, in_bi)
777 gcc_checking_assert (bitmap_bit_p (bi.potential_phi_regs, regno));
779 // Create an array of phi inputs, to be filled in later.
780 auto *inputs = XOBNEWVEC (&m_obstack, access_info *, num_preds);
781 memset (inputs, 0, sizeof (access_info *) * num_preds);
783 // Later code works out the correct mode of the phi. Use BLKmode
784 // as a placeholder for now.
785 phi_info *phi = create_phi (ebb, { E_BLKmode, regno },
786 inputs, num_preds);
787 bi.record_reg_def (phi);
790 bitmap_copy (bi.ebb_def_regs, &phis.regs);
792 // Collect the live-in memory definitions and record whether they're
793 // all the same.
794 m_temp_defs.reserve (num_preds);
795 set_info *mem_value = nullptr;
796 bool mem_phi_is_degenerate = true;
797 edge e;
798 edge_iterator ei;
799 FOR_EACH_EDGE (e, ei, cfg_bb->preds)
801 bb_info *pred_bb = this->bb (e->src);
802 if (pred_bb && pred_bb->head_insn ())
804 mem_value = bi.bb_mem_live_out[pred_bb->index ()];
805 m_temp_defs.quick_push (mem_value);
806 if (mem_value != m_temp_defs[0])
807 mem_phi_is_degenerate = false;
809 else
811 m_temp_defs.quick_push (nullptr);
812 mem_phi_is_degenerate = false;
816 // Create a phi for memory, on the assumption that something in the
817 // EBB will need it.
818 if (mem_phi_is_degenerate)
820 access_info *input[] = { mem_value };
821 mem_value = create_phi (ebb, memory, input, 1);
823 else
825 obstack_grow (&m_obstack, m_temp_defs.address (),
826 num_preds * sizeof (access_info *));
827 auto *inputs = static_cast<access_info **> (obstack_finish (&m_obstack));
828 mem_value = create_phi (ebb, memory, inputs, num_preds);
830 bi.record_mem_def (mem_value);
831 m_temp_defs.truncate (0);
834 // Called while building SSA form using BI.
836 // If FLAGS is DF_REF_AT_TOP, create the head insn for BI.current_bb
837 // and populate its uses and definitions. If FLAGS is 0, do the same
838 // for the end insn.
839 void
840 function_info::add_artificial_accesses (build_info &bi, df_ref_flags flags)
842 bb_info *bb = bi.current_bb;
843 basic_block cfg_bb = bb->cfg_bb ();
844 auto *lr_info = DF_LR_BB_INFO (cfg_bb);
845 df_ref ref;
847 insn_info *insn;
848 if (flags == DF_REF_AT_TOP)
850 if (cfg_bb->index == EXIT_BLOCK)
851 insn = append_artificial_insn (bb);
852 else
853 insn = append_artificial_insn (bb, bb_note (cfg_bb));
854 bb->set_head_insn (insn);
856 else
858 insn = append_artificial_insn (bb);
859 bb->set_end_insn (insn);
862 start_insn_accesses ();
864 FOR_EACH_ARTIFICIAL_USE (ref, cfg_bb->index)
865 if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
867 unsigned int regno = DF_REF_REGNO (ref);
868 machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
870 // A definition must be available.
871 gcc_checking_assert (bitmap_bit_p (&lr_info->in, regno)
872 || (flags != DF_REF_AT_TOP
873 && bitmap_bit_p (&lr_info->def, regno)));
874 m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, regno }));
877 // Track the return value of memory by adding an artificial use of
878 // memory at the end of the exit block.
879 if (flags == 0 && cfg_bb->index == EXIT_BLOCK)
881 auto *use = allocate<use_info> (insn, memory, bi.current_mem_value ());
882 add_use (use);
883 m_temp_uses.safe_push (use);
886 FOR_EACH_ARTIFICIAL_DEF (ref, cfg_bb->index)
887 if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
889 unsigned int regno = DF_REF_REGNO (ref);
890 machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
891 resource_info resource { mode, regno };
893 // We rely on the def set being correct.
894 gcc_checking_assert (bitmap_bit_p (&lr_info->def, regno));
896 // If the value isn't used later in the block and isn't live
897 // on exit, we could instead represent the definition as a
898 // clobber_info. However, that case should be relatively
899 // rare and set_info is any case more compact than clobber_info.
900 set_info *def = allocate<set_info> (insn, resource);
901 append_def (def);
902 m_temp_defs.safe_push (def);
903 bi.record_reg_def (def);
906 // Model the effect of a memory clobber on an incoming edge by adding
907 // a fake definition of memory at the start of the block. We don't need
908 // to add a use of the phi node because memory is implicitly always live.
909 if (flags == DF_REF_AT_TOP && has_abnormal_call_or_eh_pred_edge_p (cfg_bb))
911 set_info *def = allocate<set_info> (insn, memory);
912 append_def (def);
913 m_temp_defs.safe_push (def);
914 bi.record_mem_def (def);
917 finish_insn_accesses (insn);
920 // Called while building SSA form using BI. Create insn_infos for all
921 // relevant instructions in BI.current_bb.
922 void
923 function_info::add_block_contents (build_info &bi)
925 basic_block cfg_bb = bi.current_bb->cfg_bb ();
926 rtx_insn *insn;
927 FOR_BB_INSNS (cfg_bb, insn)
928 if (INSN_P (insn))
929 add_insn_to_block (bi, insn);
932 // Called while building SSA form using BI. Record live-out register values
933 // in the phi inputs of successor blocks and create live-out uses where
934 // appropriate. Record the live-out memory value in BI.bb_mem_live_out.
935 void
936 function_info::record_block_live_out (build_info &bi)
938 bb_info *bb = bi.current_bb;
939 ebb_info *ebb = bi.current_ebb;
940 basic_block cfg_bb = bb->cfg_bb ();
942 // Record the live-out register values in the phi inputs of
943 // successor blocks.
944 edge e;
945 edge_iterator ei;
946 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
948 bb_phi_info &phis = bi.bb_phis[e->dest->index];
949 unsigned int input_i = e->dest_idx * phis.num_phis;
950 unsigned int regno;
951 bitmap_iterator out_bi;
952 EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, out_bi)
954 phis.inputs[input_i]
955 = live_out_value (bb, bi.current_reg_value (regno));
956 input_i += 1;
960 // Add the set of registers that were defined in this BB to the set
961 // of potentially-live registers defined in the EBB.
962 bitmap_ior_into (bi.ebb_def_regs, &DF_LR_BB_INFO (cfg_bb)->def);
964 // Iterate through the registers in LIVE_OUT and see whether we need
965 // to add a live-out use for them.
966 auto record_live_out_regs = [&](bitmap live_out)
968 unsigned int regno;
969 bitmap_iterator out_bi;
970 EXECUTE_IF_AND_IN_BITMAP (bi.ebb_def_regs, live_out, 0, regno, out_bi)
972 set_info *value = live_out_value (bb, bi.current_reg_value (regno));
973 if (value && value->ebb () == ebb)
974 add_live_out_use (bb, value);
978 if (bb == ebb->last_bb ())
979 // All live-out registers might need live-out uses.
980 record_live_out_regs (DF_LR_OUT (cfg_bb));
981 else
982 // Registers might need live-out uses if they are live on entry
983 // to a successor block in a different EBB.
984 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
986 bb_info *dest_bb = this->bb (e->dest);
987 if (dest_bb->ebb () != ebb || dest_bb == ebb->first_bb ())
988 record_live_out_regs (DF_LR_IN (e->dest));
991 // Record the live-out memory value.
992 bi.bb_mem_live_out[cfg_bb->index]
993 = live_out_value (bb, bi.current_mem_value ());
996 // Add BB and its contents to the SSA information.
997 void
998 function_info::start_block (build_info &bi, bb_info *bb)
1000 ebb_info *ebb = bb->ebb ();
1002 // We (need to) add all blocks from one EBB before moving on to the next.
1003 bi.current_bb = bb;
1004 if (bb == ebb->first_bb ())
1005 bi.current_ebb = ebb;
1006 else
1007 gcc_assert (bi.current_ebb == ebb);
1009 // Record the start of this block's definitions in the definitions stack.
1010 bi.old_def_stack_limit.safe_push (bi.def_stack.length ());
1012 // Add the block itself.
1013 append_bb (bb);
1015 // If the block starts an EBB, create the phi insn. This insn should exist
1016 // for all EBBs, even if they don't (yet) need phis.
1017 if (bb == ebb->first_bb ())
1018 ebb->set_phi_insn (append_artificial_insn (bb));
1020 if (bb->index () == ENTRY_BLOCK)
1022 add_entry_block_defs (bi);
1023 record_block_live_out (bi);
1024 return;
1027 if (EDGE_COUNT (bb->cfg_bb ()->preds) == 0)
1029 // Leave unreachable blocks empty, since there is no useful
1030 // liveness information for them, and anything they do will
1031 // be wasted work. In a cleaned-up cfg, the only unreachable
1032 // block we should see is the exit block of a noreturn function.
1033 bb->set_head_insn (append_artificial_insn (bb));
1034 bb->set_end_insn (append_artificial_insn (bb));
1035 return;
1038 // If the block starts an EBB, create the phi nodes.
1039 if (bb == ebb->first_bb ())
1040 add_phi_nodes (bi);
1042 // Process the contents of the block.
1043 add_artificial_accesses (bi, DF_REF_AT_TOP);
1044 if (bb->index () != EXIT_BLOCK)
1045 add_block_contents (bi);
1046 add_artificial_accesses (bi, df_ref_flags ());
1047 record_block_live_out (bi);
1049 // If we needed to calculate a live-in set for debug purposes,
1050 // reset it to null at the end of the EBB. Convert the underlying
1051 // bitmap to an empty list view, ready for the next calculation.
1052 if (bi.ebb_live_in_for_debug && bb == ebb->last_bb ())
1054 bitmap_clear (bi.tmp_ebb_live_in_for_debug);
1055 bitmap_list_view (bi.tmp_ebb_live_in_for_debug);
1056 bi.ebb_live_in_for_debug = nullptr;
1060 // Finish adding BB and the blocks that it dominates to the SSA information.
1061 void
1062 function_info::end_block (build_info &bi, bb_info *bb)
1064 // Restore the register last_access information to the state it was
1065 // in before we started processing BB.
1066 unsigned int old_limit = bi.old_def_stack_limit.pop ();
1067 while (bi.def_stack.length () > old_limit)
1069 // We pushed a definition in BB if it was the first dominating
1070 // definition (and so the previous entry was null). In other
1071 // cases we pushed the previous dominating definition.
1072 def_info *def = bi.def_stack.pop ();
1073 unsigned int regno = def->regno ();
1074 if (def->bb () == bb)
1075 def = nullptr;
1076 bi.last_access[regno + 1] = def;
1080 // Finish setting up the phi nodes for each block, now that we've added
1081 // the contents of all blocks.
1082 void
1083 function_info::populate_phi_inputs (build_info &bi)
1085 auto_vec<phi_info *, 32> sorted_phis;
1086 for (ebb_info *ebb : ebbs ())
1088 if (!ebb->first_phi ())
1089 continue;
1091 // Get a sorted array of EBB's phi nodes.
1092 basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
1093 bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
1094 sorted_phis.truncate (0);
1095 for (phi_info *phi : ebb->phis ())
1096 sorted_phis.safe_push (phi);
1097 std::sort (sorted_phis.address (),
1098 sorted_phis.address () + sorted_phis.length (),
1099 compare_access_infos);
1101 // Set the inputs of the non-degenerate register phis. All inputs
1102 // for one edge come before all inputs for the next edge.
1103 set_info **inputs = phis.inputs;
1104 unsigned int phi_i = 0;
1105 bitmap_iterator bmi;
1106 unsigned int regno;
1107 EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, bmi)
1109 // Skip intervening degenerate phis.
1110 while (sorted_phis[phi_i]->regno () < regno)
1111 phi_i += 1;
1112 phi_info *phi = sorted_phis[phi_i];
1113 gcc_assert (phi->regno () == regno);
1114 for (unsigned int input_i = 0; input_i < phis.num_preds; ++input_i)
1115 if (set_info *input = inputs[input_i * phis.num_phis])
1117 use_info *use = phi->input_use (input_i);
1118 gcc_assert (!use->def ());
1119 use->set_def (input);
1120 add_use (use);
1122 phi_i += 1;
1123 inputs += 1;
1126 // Fill in the backedge inputs to any memory phi.
1127 phi_info *mem_phi = sorted_phis.last ();
1128 if (mem_phi->is_mem () && !mem_phi->is_degenerate ())
1130 edge e;
1131 edge_iterator ei;
1132 FOR_EACH_EDGE (e, ei, cfg_bb->preds)
1134 use_info *use = mem_phi->input_use (e->dest_idx);
1135 if (!use->def ())
1137 use->set_def (bi.bb_mem_live_out[e->src->index]);
1138 add_use (use);
1145 // Return true if it would be better to continue an EBB across NEW_EDGE
1146 // rather than across OLD_EDGE, given that both edges are viable candidates.
1147 // This is not a total ordering.
1148 static bool
1149 better_ebb_edge_p (edge new_edge, edge old_edge)
1151 // Prefer the likeliest edge.
1152 if (new_edge->probability.initialized_p ()
1153 && old_edge->probability.initialized_p ()
1154 && !(old_edge->probability == new_edge->probability))
1155 return old_edge->probability < new_edge->probability;
1157 // If both edges are equally likely, prefer a fallthru edge.
1158 if (new_edge->flags & EDGE_FALLTHRU)
1159 return true;
1160 if (old_edge->flags & EDGE_FALLTHRU)
1161 return false;
1163 // Otherwise just stick with OLD_EDGE.
1164 return false;
1167 // Pick and return the next basic block in an EBB that currently ends with BB.
1168 // Return null if the EBB must end with BB.
1169 static basic_block
1170 choose_next_block_in_ebb (basic_block bb)
1172 // Although there's nothing in principle wrong with having an EBB that
1173 // starts with the entry block and includes later blocks, there's not
1174 // really much point either. Keeping the entry block separate means
1175 // that uses of arguments consistently occur through phi nodes, rather
1176 // than the arguments sometimes appearing to come from an EBB-local
1177 // definition instead.
1178 if (bb->index == ENTRY_BLOCK)
1179 return nullptr;
1181 bool optimize_for_speed_p = optimize_bb_for_speed_p (bb);
1182 edge best_edge = nullptr;
1183 edge e;
1184 edge_iterator ei;
1185 FOR_EACH_EDGE (e, ei, bb->succs)
1186 if (!(e->flags & EDGE_COMPLEX)
1187 && e->dest->index != EXIT_BLOCK
1188 && single_pred_p (e->dest)
1189 && optimize_for_speed_p == optimize_bb_for_speed_p (e->dest)
1190 && (!best_edge || better_ebb_edge_p (e, best_edge)))
1191 best_edge = e;
1193 return best_edge ? best_edge->dest : nullptr;
1196 // Partition the function into extended basic blocks. Create the
1197 // associated ebb_infos and bb_infos, but don't add the bb_infos
1198 // to the function list yet.
1199 void
1200 function_info::create_ebbs (build_info &bi)
1202 // Compute the starting reverse postorder. We tweak this later to try
1203 // to get better EBB assignments.
1204 auto *postorder = new int[n_basic_blocks_for_fn (m_fn)];
1205 unsigned int postorder_num
1206 = pre_and_rev_post_order_compute (nullptr, postorder, true);
1207 gcc_assert (int (postorder_num) <= n_basic_blocks_for_fn (m_fn));
1209 // Iterate over the blocks in reverse postorder. In cases where
1210 // multiple possible orders exist, prefer orders that chain blocks
1211 // together into EBBs. If multiple possible EBBs exist, try to pick
1212 // the ones that are most likely to be profitable.
1213 auto_vec<bb_info *, 16> bbs;
1214 unsigned int next_bb_index = 0;
1215 for (unsigned int i = 0; i < postorder_num; ++i)
1216 if (!m_bbs[postorder[i]])
1218 // Choose and create the blocks that should form the next EBB.
1219 basic_block cfg_bb = BASIC_BLOCK_FOR_FN (m_fn, postorder[i]);
1222 // Record the chosen block order in a new RPO.
1223 bi.bb_to_rpo[cfg_bb->index] = next_bb_index++;
1224 bbs.safe_push (create_bb_info (cfg_bb));
1225 cfg_bb = choose_next_block_in_ebb (cfg_bb);
1227 while (cfg_bb);
1229 // Create the EBB itself.
1230 auto *ebb = allocate<ebb_info> (bbs[0], bbs.last ());
1231 for (bb_info *bb : bbs)
1232 bb->set_ebb (ebb);
1233 bbs.truncate (0);
1236 delete[] postorder;
1239 // Partition the function's blocks into EBBs and build SSA form for all
1240 // EBBs in the function.
1241 void
1242 function_info::process_all_blocks ()
1244 auto temps = temp_watermark ();
1245 unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
1247 build_info bi (m_num_regs, num_bb_indices);
1249 calculate_potential_phi_regs (bi);
1250 create_ebbs (bi);
1251 place_phis (bi);
1252 bb_walker (this, bi).walk (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
1253 populate_phi_inputs (bi);
1255 if (flag_checking)
1257 // The definition stack should be empty and all register definitions
1258 // should be back in their original undefined state.
1259 gcc_assert (bi.def_stack.is_empty ()
1260 && bi.old_def_stack_limit.is_empty ());
1261 for (unsigned int regno = 0; regno < m_num_regs; ++regno)
1262 gcc_assert (!bi.last_access[regno + 1]);
1266 // Print a description of CALL_CLOBBERS to PP.
1267 void
1268 rtl_ssa::pp_ebb_call_clobbers (pretty_printer *pp,
1269 const ebb_call_clobbers_info *call_clobbers)
1271 if (!call_clobbers)
1272 pp_string (pp, "<null>");
1273 else
1274 call_clobbers->print_full (pp);
1277 // Print a description of BB to PP.
1278 void
1279 rtl_ssa::pp_bb (pretty_printer *pp, const bb_info *bb)
1281 if (!bb)
1282 pp_string (pp, "<null>");
1283 else
1284 bb->print_full (pp);
1287 // Print a description of EBB to PP
1288 void
1289 rtl_ssa::pp_ebb (pretty_printer *pp, const ebb_info *ebb)
1291 if (!ebb)
1292 pp_string (pp, "<null>");
1293 else
1294 ebb->print_full (pp);
1297 // Print a description of CALL_CLOBBERS to FILE.
1298 void
1299 dump (FILE *file, const ebb_call_clobbers_info *call_clobbers)
1301 dump_using (file, pp_ebb_call_clobbers, call_clobbers);
1304 // Print a description of BB to FILE.
1305 void
1306 dump (FILE *file, const bb_info *bb)
1308 dump_using (file, pp_bb, bb);
1311 // Print a description of EBB to FILE.
1312 void
1313 dump (FILE *file, const ebb_info *ebb)
1315 dump_using (file, pp_ebb, ebb);
1318 // Debug interfaces to the dump routines above.
1319 void debug (const ebb_call_clobbers_info *x) { dump (stderr, x); }
1320 void debug (const bb_info *x) { dump (stderr, x); }
1321 void debug (const ebb_info *x) { dump (stderr, x); }