Branch data Line data Source code
1 : : // Implementation of basic-block-related functions for RTL SSA -*- C++ -*-
2 : : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 : : //
4 : : // This file is part of GCC.
5 : : //
6 : : // GCC is free software; you can redistribute it and/or modify it under
7 : : // the terms of the GNU General Public License as published by the Free
8 : : // Software Foundation; either version 3, or (at your option) any later
9 : : // version.
10 : : //
11 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : // for more details.
15 : : //
16 : : // You should have received a copy of the GNU General Public License
17 : : // along with GCC; see the file COPYING3. If not see
18 : : // <http://www.gnu.org/licenses/>.
19 : :
20 : : #define INCLUDE_ALGORITHM
21 : : #define INCLUDE_FUNCTIONAL
22 : : #define INCLUDE_ARRAY
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "backend.h"
27 : : #include "rtl.h"
28 : : #include "df.h"
29 : : #include "rtl-ssa.h"
30 : : #include "rtl-ssa/internals.h"
31 : : #include "rtl-ssa/internals.inl"
32 : : #include "cfganal.h"
33 : : #include "cfgrtl.h"
34 : : #include "predict.h"
35 : : #include "domwalk.h"
36 : :
37 : : using namespace rtl_ssa;
38 : :
39 : : // Prepare to build information for a function in which all register numbers
40 : : // are less than NUM_REGS and all basic block indices are less than
41 : : // NUM_BB_INDICES
42 : 3966710 : function_info::build_info::build_info (unsigned int num_regs,
43 : 3966710 : unsigned int num_bb_indices)
44 : 3966710 : : current_bb (nullptr),
45 : 3966710 : current_ebb (nullptr),
46 : 3966710 : last_access (num_regs + 1),
47 : 3966710 : ebb_live_in_for_debug (nullptr),
48 : 3966710 : potential_phi_regs (num_regs),
49 : 3966710 : bb_phis (num_bb_indices),
50 : 3966710 : bb_mem_live_out (num_bb_indices),
51 : 3966710 : bb_to_rpo (num_bb_indices),
52 : 7933420 : exit_block_dominator (nullptr)
53 : : {
54 : 3966710 : last_access.safe_grow_cleared (num_regs + 1);
55 : :
56 : 3966710 : bitmap_clear (potential_phi_regs);
57 : :
58 : : // These arrays shouldn't need to be initialized, since we'll always
59 : : // write to an entry before reading from it. But poison the contents
60 : : // when checking, just to make sure we don't accidentally use an
61 : : // uninitialized value.
62 : 3966710 : bb_phis.quick_grow_cleared (num_bb_indices);
63 : 3966710 : bb_mem_live_out.quick_grow (num_bb_indices);
64 : 3966710 : bb_to_rpo.quick_grow (num_bb_indices);
65 : 3966710 : if (flag_checking)
66 : : {
67 : : // Can't do this for bb_phis because it has a constructor.
68 : 7933292 : memset (bb_mem_live_out.address (), 0xaf,
69 : 3966646 : num_bb_indices * sizeof (bb_mem_live_out[0]));
70 : 7933292 : memset (bb_to_rpo.address (), 0xaf,
71 : : num_bb_indices * sizeof (bb_to_rpo[0]));
72 : : }
73 : :
74 : : // Start off with an empty set of phi nodes for each block.
75 : 62634378 : for (bb_phi_info &info : bb_phis)
76 : 50734248 : bitmap_initialize (&info.regs, &bitmap_default_obstack);
77 : 3966710 : }
78 : :
79 : 3966710 : function_info::build_info::~build_info ()
80 : : {
81 : 62634378 : for (bb_phi_info &info : bb_phis)
82 : 50734248 : bitmap_release (&info.regs);
83 : 3966710 : }
84 : :
85 : : // A dom_walker for populating the basic blocks.
86 : 7933420 : class function_info::bb_walker : public dom_walker
87 : : {
88 : : public:
89 : : bb_walker (function_info *, build_info &);
90 : : edge before_dom_children (basic_block) final override;
91 : : void after_dom_children (basic_block) final override;
92 : :
93 : : private:
94 : : // Information about the function we're building.
95 : : function_info *m_function;
96 : : build_info &m_bi;
97 : :
98 : : // We should treat the exit block as being the last child of this one.
99 : : // See the comment in the constructor for more information.
100 : : basic_block m_exit_block_dominator;
101 : : };
102 : :
103 : : // Prepare to walk the blocks in FUNCTION using BI.
104 : 3966710 : function_info::bb_walker::bb_walker (function_info *function, build_info &bi)
105 : : : dom_walker (CDI_DOMINATORS, ALL_BLOCKS, bi.bb_to_rpo.address ()),
106 : 3966710 : m_function (function),
107 : 3966710 : m_bi (bi),
108 : 7933420 : m_exit_block_dominator (bi.exit_block_dominator)
109 : : {
110 : : // If the exit block is unreachable, process it last.
111 : 3966710 : if (!m_exit_block_dominator)
112 : 107940 : m_exit_block_dominator = ENTRY_BLOCK_PTR_FOR_FN (m_function->m_fn);
113 : 3966710 : }
114 : :
115 : : edge
116 : 49344938 : function_info::bb_walker::before_dom_children (basic_block bb)
117 : : {
118 : 49344938 : m_function->start_block (m_bi, m_function->bb (bb));
119 : 49344938 : return nullptr;
120 : : }
121 : :
122 : : void
123 : 49344938 : function_info::bb_walker::after_dom_children (basic_block bb)
124 : : {
125 : : // See the comment in the constructor for details.
126 : 49344938 : if (bb == m_exit_block_dominator)
127 : : {
128 : 3966710 : before_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
129 : 3966710 : after_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
130 : : }
131 : 49344938 : m_function->end_block (m_bi, m_function->bb (bb));
132 : 49344938 : }
133 : :
134 : : // See the comment above the declaration.
135 : : void
136 : 0 : bb_info::print_identifier (pretty_printer *pp) const
137 : : {
138 : 0 : char tmp[3 * sizeof (index ()) + 3];
139 : 0 : snprintf (tmp, sizeof (tmp), "bb%d", index ());
140 : 0 : pp_string (pp, tmp);
141 : 0 : if (ebb_info *ebb = this->ebb ())
142 : : {
143 : 0 : pp_space (pp);
144 : 0 : pp_left_bracket (pp);
145 : 0 : ebb->print_identifier (pp);
146 : 0 : pp_right_bracket (pp);
147 : : }
148 : 0 : }
149 : :
150 : : // See the comment above the declaration.
151 : : void
152 : 0 : bb_info::print_full (pretty_printer *pp) const
153 : : {
154 : 0 : pp_string (pp, "basic block ");
155 : 0 : print_identifier (pp);
156 : 0 : pp_colon (pp);
157 : :
158 : 0 : auto print_insn = [pp](const char *header, const insn_info *insn)
159 : : {
160 : 0 : pp_newline_and_indent (pp, 2);
161 : 0 : pp_string (pp, header);
162 : 0 : pp_newline_and_indent (pp, 2);
163 : 0 : if (insn)
164 : 0 : pp_insn (pp, insn);
165 : : else
166 : 0 : pp_string (pp, "<uninitialized>");
167 : 0 : pp_indentation (pp) -= 4;
168 : 0 : };
169 : :
170 : 0 : print_insn ("head:", head_insn ());
171 : :
172 : 0 : pp_newline (pp);
173 : 0 : pp_newline_and_indent (pp, 2);
174 : 0 : pp_string (pp, "contents:");
175 : 0 : if (!head_insn ())
176 : : {
177 : 0 : pp_newline_and_indent (pp, 2);
178 : 0 : pp_string (pp, "<uninitialized>");
179 : 0 : pp_indentation (pp) -= 2;
180 : : }
181 : 0 : else if (auto insns = real_insns ())
182 : : {
183 : : bool is_first = true;
184 : 0 : for (const insn_info *insn : insns)
185 : : {
186 : 0 : if (is_first)
187 : : is_first = false;
188 : : else
189 : 0 : pp_newline (pp);
190 : 0 : pp_newline_and_indent (pp, 2);
191 : 0 : pp_insn (pp, insn);
192 : 0 : pp_indentation (pp) -= 2;
193 : : }
194 : : }
195 : : else
196 : : {
197 : 0 : pp_newline_and_indent (pp, 2);
198 : 0 : pp_string (pp, "none");
199 : 0 : pp_indentation (pp) -= 2;
200 : : }
201 : 0 : pp_indentation (pp) -= 2;
202 : :
203 : 0 : pp_newline (pp);
204 : 0 : print_insn ("end:", end_insn ());
205 : 0 : }
206 : :
207 : : // See the comment above the declaration.
208 : : void
209 : 0 : ebb_call_clobbers_info::print_summary (pretty_printer *pp) const
210 : : {
211 : 0 : pp_string (pp, "call clobbers for ABI ");
212 : 0 : if (m_abi)
213 : 0 : pp_decimal_int (pp, m_abi->id ());
214 : : else
215 : 0 : pp_string (pp, "<null>");
216 : 0 : }
217 : :
218 : : // See the comment above the declaration.
219 : : void
220 : 0 : ebb_call_clobbers_info::print_full (pretty_printer *pp) const
221 : : {
222 : 0 : print_summary (pp);
223 : 0 : pp_colon (pp);
224 : 0 : pp_newline_and_indent (pp, 2);
225 : 0 : auto print_node = [](pretty_printer *pp,
226 : : const insn_call_clobbers_note *note)
227 : : {
228 : 0 : if (insn_info *insn = note->insn ())
229 : 0 : insn->print_identifier_and_location (pp);
230 : : else
231 : 0 : pp_string (pp, "<null>");
232 : 0 : };
233 : 0 : print (pp, root (), print_node);
234 : 0 : pp_indentation (pp) -= 2;
235 : 0 : }
236 : :
237 : : // See the comment above the declaration.
238 : : void
239 : 0 : ebb_info::print_identifier (pretty_printer *pp) const
240 : : {
241 : : // first_bb is populated by the constructor and so should always
242 : : // be nonnull.
243 : 0 : auto index = first_bb ()->index ();
244 : 0 : char tmp[3 * sizeof (index) + 4];
245 : 0 : snprintf (tmp, sizeof (tmp), "ebb%d", index);
246 : 0 : pp_string (pp, tmp);
247 : 0 : }
248 : :
249 : : // See the comment above the declaration.
250 : : void
251 : 0 : ebb_info::print_full (pretty_printer *pp) const
252 : : {
253 : 0 : pp_string (pp, "extended basic block ");
254 : 0 : print_identifier (pp);
255 : 0 : pp_colon (pp);
256 : :
257 : 0 : pp_newline_and_indent (pp, 2);
258 : 0 : if (insn_info *phi_insn = this->phi_insn ())
259 : : {
260 : 0 : phi_insn->print_identifier_and_location (pp);
261 : 0 : pp_colon (pp);
262 : 0 : if (auto phis = this->phis ())
263 : : {
264 : : bool is_first = true;
265 : 0 : for (const phi_info *phi : phis)
266 : : {
267 : 0 : if (is_first)
268 : : is_first = false;
269 : : else
270 : 0 : pp_newline (pp);
271 : 0 : pp_newline_and_indent (pp, 2);
272 : 0 : pp_access (pp, phi, PP_ACCESS_SETTER);
273 : 0 : pp_indentation (pp) -= 2;
274 : : }
275 : : }
276 : : else
277 : : {
278 : 0 : pp_newline_and_indent (pp, 2);
279 : 0 : pp_string (pp, "no phi nodes");
280 : 0 : pp_indentation (pp) -= 2;
281 : : }
282 : : }
283 : : else
284 : 0 : pp_string (pp, "no phi insn");
285 : 0 : pp_indentation (pp) -= 2;
286 : :
287 : 0 : for (const bb_info *bb : bbs ())
288 : : {
289 : 0 : pp_newline (pp);
290 : 0 : pp_newline_and_indent (pp, 2);
291 : 0 : pp_bb (pp, bb);
292 : 0 : pp_indentation (pp) -= 2;
293 : : }
294 : :
295 : 0 : for (ebb_call_clobbers_info *ecc : call_clobbers ())
296 : : {
297 : 0 : pp_newline (pp);
298 : 0 : pp_newline_and_indent (pp, 2);
299 : 0 : pp_ebb_call_clobbers (pp, ecc);
300 : 0 : pp_indentation (pp) -= 2;
301 : : }
302 : 0 : }
303 : :
304 : : // Add a dummy use to mark that DEF is live out of BB's EBB at the end of BB.
305 : : void
306 : 79817812 : function_info::add_live_out_use (bb_info *bb, set_info *def)
307 : : {
308 : : // There is nothing to do if DEF is an artificial definition at the end
309 : : // of BB. In that case the definitino is rooted at the end of the block
310 : : // and we wouldn't gain anything by inserting a use immediately after it.
311 : : // If we did want to insert a use, we'd need to associate it with a new
312 : : // instruction that comes after bb->end_insn ().
313 : 79817812 : if (def->insn () == bb->end_insn ())
314 : : return;
315 : :
316 : : // If the end of the block already has an artificial use, that use
317 : : // acts to make DEF live at the appropriate point.
318 : 60770843 : if (find_use (def, bb->end_insn ()).matching_use ())
319 : : return;
320 : :
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 : 51239614 : auto *use = allocate<use_info> (bb->end_insn (), def->resource (), def);
326 : 51239614 : use->set_is_live_out_use (true);
327 : 51239614 : add_use (use);
328 : : }
329 : :
330 : : // Return true if all nondebug uses of DEF are live-out uses.
331 : : static bool
332 : 10760009 : all_uses_are_live_out_uses (set_info *def)
333 : : {
334 : 10780376 : for (use_info *use : def->all_uses ())
335 : 13540904 : if (!use->is_in_debug_insn () && !use->is_live_out_use ())
336 : 10760009 : return false;
337 : : return true;
338 : : }
339 : :
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 : 159323138 : function_info::live_out_value (bb_info *bb, set_info *set)
344 : : {
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 : 159323138 : if (auto *phi = safe_dyn_cast<phi_info *> (set))
348 : 42068830 : if (phi->is_degenerate ())
349 : : {
350 : 16955966 : set = phi->input_value (0);
351 : :
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 : 16955966 : if (bb == bb->ebb ()->last_bb () && all_uses_are_live_out_uses (phi))
356 : 4009924 : replace_phi (phi, set);
357 : : }
358 : :
359 : 159323138 : return set;
360 : : }
361 : :
362 : : // Make USE's definition available at USE, if it isn't already. Assume that
363 : : // the caller has properly used make_use_available to check that this is
364 : : // possible.
365 : : void
366 : 15241472 : function_info::commit_make_use_available (use_info *use)
367 : : {
368 : : // We only need to handle single dominating definitions here.
369 : : // Other cases are handled by degenerate phis, with create_degenerate_phi
370 : : // creating any necessary live-out uses.
371 : 15241472 : set_info *def = use->def ();
372 : 15241472 : if (def
373 : 15241472 : && use->is_reg ()
374 : 12643715 : && is_single_dominating_def (def)
375 : 26147373 : && use->ebb () != def->ebb ())
376 : : {
377 : : // If USE's EBB has DEF's EBB as its single predecessor, it's enough
378 : : // to add a live-out use to the former's predecessor block. Otherwise,
379 : : // conservatively add a live-out use at the end of DEF's block, so that
380 : : // DEF cannot move further down. Doing a minimal yet accurate update
381 : : // would be an O(n.log(n)) operation in the worst case.
382 : 4768719 : auto ebb_cfg_bb = def->ebb ()->first_bb ()->cfg_bb ();
383 : 4768719 : if (single_pred_p (ebb_cfg_bb))
384 : : {
385 : 3385726 : bb_info *pred_bb = this->bb (single_pred (ebb_cfg_bb));
386 : 3385726 : if (pred_bb->ebb () == def->ebb ())
387 : : {
388 : 0 : add_live_out_use (pred_bb, def);
389 : 0 : return;
390 : : }
391 : : }
392 : 4768719 : add_live_out_use (def->bb (), def);
393 : 4768719 : return;
394 : : }
395 : : }
396 : :
397 : : // Add PHI to EBB and enter it into the function's hash table.
398 : : void
399 : 61007752 : function_info::append_phi (ebb_info *ebb, phi_info *phi)
400 : : {
401 : 61007752 : phi_info *first_phi = ebb->first_phi ();
402 : 61007752 : if (first_phi)
403 : 34501284 : first_phi->set_prev_phi (phi);
404 : 61007752 : phi->set_next_phi (first_phi);
405 : 61007752 : ebb->set_first_phi (phi);
406 : 61007752 : add_def (phi);
407 : 61007752 : }
408 : :
409 : : // Remove PHI from its current position in the SSA graph.
410 : : void
411 : 7143098 : function_info::remove_phi (phi_info *phi)
412 : : {
413 : 7143098 : phi_info *next = phi->next_phi ();
414 : 7143098 : phi_info *prev = phi->prev_phi ();
415 : :
416 : 7143098 : if (next)
417 : 946178 : next->set_prev_phi (prev);
418 : :
419 : 7143098 : if (prev)
420 : 3614508 : prev->set_next_phi (next);
421 : : else
422 : 3528590 : phi->ebb ()->set_first_phi (next);
423 : :
424 : 7143098 : remove_def (phi);
425 : 7143098 : phi->clear_phi_links ();
426 : 7143098 : }
427 : :
428 : : // Remove PHI from the SSA graph and free its memory.
429 : : void
430 : 7143098 : function_info::delete_phi (phi_info *phi)
431 : : {
432 : 7143098 : gcc_assert (!phi->has_any_uses ());
433 : :
434 : : // Remove the inputs to the phi.
435 : 22102098 : for (use_info *input : phi->inputs ())
436 : 7815902 : remove_use (input);
437 : :
438 : 7143098 : remove_phi (phi);
439 : :
440 : 7143098 : phi->set_next_phi (m_free_phis);
441 : 7143098 : m_free_phis = phi;
442 : 7143098 : }
443 : :
444 : : // If possible, remove PHI and replace all uses with NEW_VALUE.
445 : : void
446 : 39961952 : function_info::replace_phi (phi_info *phi, set_info *new_value)
447 : : {
448 : 42056269 : auto update_use = [&](use_info *use)
449 : : {
450 : 2094317 : remove_use (use);
451 : 2094317 : use->set_def (new_value);
452 : 2094317 : add_use (use);
453 : 42056269 : };
454 : :
455 : 39961952 : if (new_value)
456 : 79921828 : for (use_info *use : phi->nondebug_insn_uses ())
457 : 32842691 : if (!use->is_live_out_use ())
458 : : {
459 : : // We need to keep the phi around for its local uses.
460 : : // Turn it into a degenerate phi, if it isn't already.
461 : 32842689 : use_info *single_use = nullptr;
462 : 99565546 : for (auto *use : phi->inputs ())
463 : 33880168 : if (!single_use)
464 : : single_use = use;
465 : 1037479 : else if (use->def () == new_value)
466 : : {
467 : 863778 : remove_use (single_use);
468 : 863778 : single_use = use;
469 : : }
470 : : else
471 : 173701 : remove_use (use);
472 : :
473 : 32842689 : if (single_use->def () != new_value)
474 : 0 : update_use (single_use);
475 : :
476 : 32842689 : if (phi->is_degenerate ())
477 : 32842689 : return;
478 : :
479 : 835063 : phi->make_degenerate (single_use);
480 : :
481 : : // Redirect all phi users to NEW_VALUE.
482 : 35194632 : while (use_info *phi_use = phi->last_phi_use ())
483 : 1516880 : update_use (phi_use);
484 : :
485 : : return;
486 : : }
487 : :
488 : : // Replace the uses. We can discard uses that only existed for the
489 : : // sake of marking live-out values, since the resource is now transparent
490 : : // in the phi's EBB.
491 : 7698597 : while (use_info *use = phi->last_use ())
492 : 579334 : if (use->is_live_out_use ())
493 : 1897 : remove_use (use);
494 : : else
495 : 577437 : update_use (use);
496 : :
497 : 7119263 : delete_phi (phi);
498 : : }
499 : :
500 : : // Create and return a phi node for EBB. RESOURCE is the resource that
501 : : // the phi node sets (and thus that all the inputs set too). NUM_INPUTS
502 : : // is the number of inputs, which is 1 for a degenerate phi. INPUTS[I]
503 : : // is a set_info that gives the value of input I, or null if the value
504 : : // is either unknown or uninitialized. If NUM_INPUTS > 1, this array
505 : : // is allocated on the main obstack and can be reused for the use array.
506 : : //
507 : : // Add the created phi node to its basic block and enter it into the
508 : : // function's hash table.
509 : : phi_info *
510 : 61007752 : function_info::create_phi (ebb_info *ebb, resource_info resource,
511 : : access_info **inputs, unsigned int num_inputs)
512 : : {
513 : 61007752 : phi_info *phi = m_free_phis;
514 : 61007752 : if (phi)
515 : : {
516 : 4014441 : m_free_phis = phi->next_phi ();
517 : 4014441 : *phi = phi_info (ebb->phi_insn (), resource, phi->uid ());
518 : : }
519 : : else
520 : : {
521 : 56993311 : phi = allocate<phi_info> (ebb->phi_insn (), resource, m_next_phi_uid);
522 : 56993311 : m_next_phi_uid += 1;
523 : : }
524 : :
525 : : // Convert the array of set_infos into an array of use_infos. Also work
526 : : // out what mode the phi should have.
527 : 61007752 : machine_mode new_mode = resource.mode;
528 : 160106456 : for (unsigned int i = 0; i < num_inputs; ++i)
529 : : {
530 : 99098704 : auto *input = safe_as_a<set_info *> (inputs[i]);
531 : 99098704 : auto *use = allocate<use_info> (phi, resource, input);
532 : 99098704 : add_use (use);
533 : 99098704 : inputs[i] = use;
534 : 99098704 : if (input)
535 : 61422382 : new_mode = combine_modes (new_mode, input->mode ());
536 : : }
537 : :
538 : 61007752 : phi->set_inputs (use_array (inputs, num_inputs));
539 : 61007752 : phi->set_mode (new_mode);
540 : :
541 : 61007752 : append_phi (ebb, phi);
542 : :
543 : 61007752 : return phi;
544 : : }
545 : :
546 : : // Create and return a degenerate phi for EBB whose input comes from DEF.
547 : : // This is used in cases where DEF is known to be available on entry to
548 : : // EBB but was not previously used within it. If DEF is for a register,
549 : : // there are two cases:
550 : : //
551 : : // (1) DEF was already live on entry to EBB but was previously transparent
552 : : // within it.
553 : : //
554 : : // (2) DEF was not previously live on entry to EBB and is being made live
555 : : // by this update.
556 : : //
557 : : // At the moment, this function only handles the case in which EBB has a
558 : : // single predecessor block and DEF is defined in that block's EBB.
559 : : phi_info *
560 : 12886 : function_info::create_degenerate_phi (ebb_info *ebb, set_info *def)
561 : : {
562 : : // Allow the function to be called twice in succession for the same def.
563 : 12886 : def_lookup dl = find_def (def->resource (), ebb->phi_insn ());
564 : 12886 : if (set_info *set = dl.matching_set ())
565 : 598 : return as_a<phi_info *> (set);
566 : :
567 : 12288 : access_info *input = def;
568 : 12288 : phi_info *phi = create_phi (ebb, def->resource (), &input, 1);
569 : 12288 : if (def->is_reg ())
570 : : {
571 : 10998 : unsigned int regno = def->regno ();
572 : :
573 : : // Find the single predecessor mentioned above.
574 : 10998 : basic_block pred_cfg_bb = single_pred (ebb->first_bb ()->cfg_bb ());
575 : 10998 : bb_info *pred_bb = this->bb (pred_cfg_bb);
576 : :
577 : 21996 : if (bitmap_set_bit (DF_LR_IN (ebb->first_bb ()->cfg_bb ()), regno))
578 : : {
579 : : // The register was not previously live on entry to EBB and
580 : : // might not have been live on exit from PRED_BB either.
581 : 17876 : bitmap_set_bit (DF_LR_OUT (pred_cfg_bb), regno);
582 : 8938 : add_live_out_use (pred_bb, def);
583 : : }
584 : : else
585 : : {
586 : : // The register was previously live in to EBB. Add live-out uses
587 : : // at the appropriate points.
588 : 2060 : insn_info *next_insn = nullptr;
589 : 2060 : if (def_info *next_def = phi->next_def ())
590 : 2056 : next_insn = next_def->insn ();
591 : 5805 : for (bb_info *bb : ebb->bbs ())
592 : : {
593 : 3767 : if ((next_insn && *next_insn <= *bb->end_insn ())
594 : 11307 : || !bitmap_bit_p (DF_LR_OUT (bb->cfg_bb ()), regno))
595 : : break;
596 : 3745 : add_live_out_use (bb, def);
597 : : }
598 : : }
599 : : }
600 : : return phi;
601 : : }
602 : :
603 : : // Create a bb_info for CFG_BB, given that no such structure currently exists.
604 : : bb_info *
605 : 49344938 : function_info::create_bb_info (basic_block cfg_bb)
606 : : {
607 : 49344938 : bb_info *bb = allocate<bb_info> (cfg_bb);
608 : 49344938 : gcc_checking_assert (!m_bbs[cfg_bb->index]);
609 : 49344938 : m_bbs[cfg_bb->index] = bb;
610 : 49344938 : return bb;
611 : : }
612 : :
613 : : // Add BB to the end of the list of blocks.
614 : : void
615 : 49344938 : function_info::append_bb (bb_info *bb)
616 : : {
617 : 49344938 : if (m_last_bb)
618 : 45378228 : m_last_bb->set_next_bb (bb);
619 : : else
620 : 3966710 : m_first_bb = bb;
621 : 49344938 : bb->set_prev_bb (m_last_bb);
622 : 49344938 : m_last_bb = bb;
623 : 49344938 : }
624 : :
625 : : // Calculate BI.potential_phi_regs and BI.potential_phi_regs_for_debug.
626 : : void
627 : 3966710 : function_info::calculate_potential_phi_regs (build_info &bi)
628 : : {
629 : 3966710 : auto *lr_info = DF_LR_BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
630 : 3966710 : bool is_debug = MAY_HAVE_DEBUG_INSNS;
631 : 561410762 : for (unsigned int regno = 0; regno < m_num_regs; ++regno)
632 : 557444052 : if (regno >= DF_REG_SIZE (DF)
633 : : // Exclude registers that have a single definition that dominates
634 : : // all uses. If the definition does not dominate all uses,
635 : : // the register will be exposed upwards to the entry block but
636 : : // will not be defined by the entry block.
637 : 557426039 : || DF_REG_DEF_COUNT (regno) > 1
638 : 953928252 : || (!bitmap_bit_p (&lr_info->def, regno)
639 : 365199894 : && bitmap_bit_p (&lr_info->out, regno)))
640 : : {
641 : 161209072 : bitmap_set_bit (bi.potential_phi_regs, regno);
642 : 161209072 : if (is_debug)
643 : 98080464 : bitmap_set_bit (bi.potential_phi_regs_for_debug, regno);
644 : : }
645 : 3966710 : }
646 : :
647 : : // Called while building SSA form using BI. Decide where phi nodes
648 : : // should be placed for each register and initialize BI.bb_phis accordingly.
649 : : void
650 : 3966710 : function_info::place_phis (build_info &bi)
651 : : {
652 : 3966710 : unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
653 : :
654 : : // Calculate dominance frontiers.
655 : 3966710 : auto_vec<bitmap_head> frontiers;
656 : 3966710 : frontiers.safe_grow_cleared (num_bb_indices);
657 : 54700958 : for (unsigned int i = 0; i < num_bb_indices; ++i)
658 : 50734248 : bitmap_initialize (&frontiers[i], &bitmap_default_obstack);
659 : 7933420 : compute_dominance_frontiers (frontiers.address ());
660 : :
661 : : // The normal dominance information doesn't calculate dominators for
662 : : // the exit block, so we don't get dominance frontiers for them either.
663 : : // Calculate them by hand.
664 : 15985826 : for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
665 : : {
666 : 4085696 : basic_block bb = e->src;
667 : 4976094 : while (bb != bi.exit_block_dominator)
668 : : {
669 : 890398 : bitmap_set_bit (&frontiers[bb->index], EXIT_BLOCK);
670 : 890398 : bb = get_immediate_dominator (CDI_DOMINATORS, bb);
671 : : }
672 : : }
673 : :
674 : : // In extreme cases, the number of live-in registers can be much
675 : : // greater than the number of phi nodes needed in a block (see PR98863).
676 : : // Try to reduce the number of operations involving live-in sets by using
677 : : // PENDING as a staging area: registers in PENDING need phi nodes if
678 : : // they are live on entry to the corresponding block, but do not need
679 : : // phi nodes otherwise.
680 : 3966710 : auto_vec<bitmap_head> unfiltered;
681 : 3966710 : unfiltered.safe_grow_cleared (num_bb_indices);
682 : 54700958 : for (unsigned int i = 0; i < num_bb_indices; ++i)
683 : 50734248 : bitmap_initialize (&unfiltered[i], &bitmap_default_obstack);
684 : :
685 : : // If block B1 defines R and if B2 is in the dominance frontier of B1,
686 : : // queue a possible phi node for R in B2.
687 : 3966710 : auto_bitmap worklist;
688 : 54700958 : for (unsigned int b1 = 0; b1 < num_bb_indices; ++b1)
689 : : {
690 : : // Only access DF information for blocks that are known to exist.
691 : 50734248 : if (bitmap_empty_p (&frontiers[b1]))
692 : 19244049 : continue;
693 : :
694 : : // Defs in B1 that are possibly in LR_IN in the dominance frontier
695 : : // blocks.
696 : 31490199 : auto_bitmap b1_def;
697 : 62980398 : bitmap_and (b1_def, &DF_LR_BB_INFO (BASIC_BLOCK_FOR_FN (m_fn, b1))->def,
698 : 62980398 : DF_LR_OUT (BASIC_BLOCK_FOR_FN (m_fn, b1)));
699 : :
700 : 31490199 : bitmap_iterator bmi;
701 : 31490199 : unsigned int b2;
702 : 81252905 : EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
703 : 49762706 : if (bitmap_ior_into (&unfiltered[b2], b1_def)
704 : 49762706 : && !bitmap_empty_p (&frontiers[b2]))
705 : : // Propagate the (potential) new phi node definitions in B2.
706 : 13982023 : bitmap_set_bit (worklist, b2);
707 : 31490199 : }
708 : :
709 : 10840769 : while (!bitmap_empty_p (worklist))
710 : : {
711 : 6874059 : unsigned int b1 = bitmap_first_set_bit (worklist);
712 : 6874059 : bitmap_clear_bit (worklist, b1);
713 : :
714 : : // Restrict the phi nodes to registers that are live on entry to
715 : : // the block.
716 : 6874059 : bitmap b1_in = DF_LR_IN (BASIC_BLOCK_FOR_FN (m_fn, b1));
717 : 6874059 : bitmap b1_phis = &bi.bb_phis[b1].regs;
718 : 6874059 : if (!bitmap_ior_and_into (b1_phis, &unfiltered[b1], b1_in))
719 : 1136671 : continue;
720 : :
721 : : // If block B1 has a phi node for R and if B2 is in the dominance
722 : : // frontier of B1, queue a possible phi node for R in B2.
723 : 5737388 : bitmap_iterator bmi;
724 : 5737388 : unsigned int b2;
725 : 16298299 : EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
726 : 10560911 : if (bitmap_ior_into (&unfiltered[b2], b1_phis)
727 : 10560911 : && !bitmap_empty_p (&frontiers[b2]))
728 : 1609964 : bitmap_set_bit (worklist, b2);
729 : : }
730 : :
731 : 3966710 : basic_block cfg_bb;
732 : 53311648 : FOR_ALL_BB_FN (cfg_bb, m_fn)
733 : : {
734 : : // Calculate the set of phi nodes for blocks that don't have any
735 : : // dominance frontiers. We only need to do this once per block.
736 : 49344938 : unsigned int i = cfg_bb->index;
737 : 49344938 : bb_phi_info &phis = bi.bb_phis[i];
738 : 49344938 : if (bitmap_empty_p (&frontiers[i]))
739 : 35709478 : bitmap_and (&phis.regs, &unfiltered[i], DF_LR_IN (cfg_bb));
740 : :
741 : : // Create an array that contains all phi inputs for this block.
742 : : // See the comment above the member variables for more information.
743 : 49344938 : phis.num_phis = bitmap_count_bits (&phis.regs);
744 : 49344938 : phis.num_preds = EDGE_COUNT (cfg_bb->preds);
745 : 49344938 : unsigned int num_inputs = phis.num_phis * phis.num_preds;
746 : 49344938 : if (num_inputs != 0)
747 : : {
748 : 6776545 : phis.inputs = XOBNEWVEC (&m_temp_obstack, set_info *, num_inputs);
749 : 6776545 : memset (phis.inputs, 0, num_inputs * sizeof (phis.inputs[0]));
750 : : }
751 : : }
752 : :
753 : : // Free the temporary bitmaps.
754 : 54700958 : for (unsigned int i = 0; i < num_bb_indices; ++i)
755 : : {
756 : 50734248 : bitmap_release (&frontiers[i]);
757 : 50734248 : bitmap_release (&unfiltered[i]);
758 : : }
759 : 3966710 : }
760 : :
761 : : // Called while building SSA form using BI, with BI.current_bb being
762 : : // the entry block.
763 : : //
764 : : // Create the entry block instructions and their definitions. The only
765 : : // useful instruction is the end instruction, which carries definitions
766 : : // for the values that are live on entry to the function. However, it
767 : : // seems simpler to create a head instruction too, rather than force all
768 : : // users of the block information to treat the entry block as a special case.
769 : : void
770 : 3966710 : function_info::add_entry_block_defs (build_info &bi)
771 : : {
772 : 3966710 : bb_info *bb = bi.current_bb;
773 : 3966710 : basic_block cfg_bb = bi.current_bb->cfg_bb ();
774 : 3966710 : auto *lr_info = DF_LR_BB_INFO (cfg_bb);
775 : :
776 : 3966710 : bb->set_head_insn (append_artificial_insn (bb));
777 : 3966710 : insn_info *insn = append_artificial_insn (bb);
778 : 3966710 : bb->set_end_insn (insn);
779 : :
780 : 3966710 : start_insn_accesses ();
781 : :
782 : : // Using LR to derive the liveness information means that we create an
783 : : // entry block definition for upwards exposed registers. These registers
784 : : // are sometimes genuinely uninitialized. However, some targets also
785 : : // create a pseudo PIC base register and only initialize it later.
786 : : // Handling that case correctly seems more important than optimizing
787 : : // uninitialized uses.
788 : 3966710 : unsigned int regno;
789 : 3966710 : bitmap_iterator in_bi;
790 : 22969580 : EXECUTE_IF_SET_IN_BITMAP (&lr_info->out, 0, regno, in_bi)
791 : : {
792 : 19002870 : auto *set = allocate<set_info> (insn, full_register (regno));
793 : 19002870 : append_def (set);
794 : 19002870 : m_temp_defs.safe_push (set);
795 : 19002870 : bi.record_reg_def (set);
796 : : }
797 : :
798 : : // Create a definition that reflects the state of memory on entry to
799 : : // the function.
800 : 3966710 : auto *set = allocate<set_info> (insn, memory);
801 : 3966710 : append_def (set);
802 : 3966710 : m_temp_defs.safe_push (set);
803 : 3966710 : bi.record_mem_def (set);
804 : :
805 : 3966710 : finish_insn_accesses (insn);
806 : 3966710 : }
807 : :
808 : : // Lazily calculate the value of BI.ebb_live_in_for_debug for BI.current_ebb.
809 : : void
810 : 1155370 : function_info::calculate_ebb_live_in_for_debug (build_info &bi)
811 : : {
812 : 1155370 : gcc_checking_assert (bitmap_empty_p (bi.tmp_ebb_live_in_for_debug));
813 : 1155370 : bi.ebb_live_in_for_debug = bi.tmp_ebb_live_in_for_debug;
814 : 2310740 : bitmap_and (bi.ebb_live_in_for_debug, bi.potential_phi_regs_for_debug,
815 : 2310740 : DF_LR_IN (bi.current_ebb->first_bb ()->cfg_bb ()));
816 : 1155370 : bitmap_tree_view (bi.ebb_live_in_for_debug);
817 : 1155370 : }
818 : :
819 : : // Called while building SSA form using BI. Create phi nodes for the
820 : : // current EBB.
821 : : void
822 : 26505268 : function_info::add_phi_nodes (build_info &bi)
823 : : {
824 : 26505268 : ebb_info *ebb = bi.current_ebb;
825 : 26505268 : basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
826 : :
827 : : // Create the register phis for this EBB.
828 : 26505268 : bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
829 : 26505268 : unsigned int num_preds = phis.num_preds;
830 : 26505268 : unsigned int regno;
831 : 26505268 : bitmap_iterator in_bi;
832 : 39228255 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, in_bi)
833 : : {
834 : 12722987 : gcc_checking_assert (bitmap_bit_p (bi.potential_phi_regs, regno));
835 : :
836 : : // Create an array of phi inputs, to be filled in later.
837 : 12722987 : auto *inputs = XOBNEWVEC (&m_obstack, access_info *, num_preds);
838 : 12722987 : memset (inputs, 0, sizeof (access_info *) * num_preds);
839 : :
840 : : // Later code works out the correct mode of the phi. Use BLKmode
841 : : // as a placeholder for now.
842 : 12722987 : phi_info *phi = create_phi (ebb, { E_BLKmode, regno },
843 : : inputs, num_preds);
844 : 12722987 : bi.record_reg_def (phi);
845 : : }
846 : :
847 : 26505268 : bitmap_copy (bi.ebb_def_regs, &phis.regs);
848 : :
849 : : // Collect the live-in memory definitions and record whether they're
850 : : // all the same.
851 : 26505268 : m_temp_defs.reserve (num_preds);
852 : 26505268 : set_info *mem_value = nullptr;
853 : 26505268 : bool mem_phi_is_degenerate = true;
854 : 26505268 : edge e;
855 : 26505268 : edge_iterator ei;
856 : 72395713 : FOR_EACH_EDGE (e, ei, cfg_bb->preds)
857 : : {
858 : 45890445 : bb_info *pred_bb = this->bb (e->src);
859 : 45890445 : if (pred_bb && pred_bb->head_insn ())
860 : : {
861 : 43233031 : mem_value = bi.bb_mem_live_out[pred_bb->index ()];
862 : 43233031 : m_temp_defs.quick_push (mem_value);
863 : 43233031 : if (mem_value != m_temp_defs[0])
864 : 14164167 : mem_phi_is_degenerate = false;
865 : : }
866 : : else
867 : : {
868 : 2657414 : m_temp_defs.quick_push (nullptr);
869 : 2657414 : mem_phi_is_degenerate = false;
870 : : }
871 : : }
872 : :
873 : : // Create a phi for memory, on the assumption that something in the
874 : : // EBB will need it.
875 : 26505268 : if (mem_phi_is_degenerate)
876 : : {
877 : 17893218 : access_info *input[] = { mem_value };
878 : 17893218 : mem_value = create_phi (ebb, memory, input, 1);
879 : : }
880 : : else
881 : : {
882 : 8612050 : obstack_grow (&m_obstack, m_temp_defs.address (),
883 : : num_preds * sizeof (access_info *));
884 : 8612050 : auto *inputs = static_cast<access_info **> (obstack_finish (&m_obstack));
885 : 8612050 : mem_value = create_phi (ebb, memory, inputs, num_preds);
886 : : }
887 : 26505268 : bi.record_mem_def (mem_value);
888 : 26505268 : m_temp_defs.truncate (0);
889 : 26505268 : }
890 : :
891 : : // Called while building SSA form using BI.
892 : : //
893 : : // If FLAGS is DF_REF_AT_TOP, create the head insn for BI.current_bb
894 : : // and populate its uses and definitions. If FLAGS is 0, do the same
895 : : // for the end insn.
896 : : void
897 : 90540576 : function_info::add_artificial_accesses (build_info &bi, df_ref_flags flags)
898 : : {
899 : 90540576 : bb_info *bb = bi.current_bb;
900 : 90540576 : basic_block cfg_bb = bb->cfg_bb ();
901 : 90540576 : auto *lr_info = DF_LR_BB_INFO (cfg_bb);
902 : 90540576 : df_ref ref;
903 : :
904 : 90540576 : insn_info *insn;
905 : 90540576 : if (flags == DF_REF_AT_TOP)
906 : : {
907 : 45270288 : if (cfg_bb->index == EXIT_BLOCK)
908 : 3858770 : insn = append_artificial_insn (bb);
909 : : else
910 : 41411518 : insn = append_artificial_insn (bb, bb_note (cfg_bb));
911 : 45270288 : bb->set_head_insn (insn);
912 : : }
913 : : else
914 : : {
915 : 45270288 : insn = append_artificial_insn (bb);
916 : 45270288 : bb->set_end_insn (insn);
917 : : }
918 : :
919 : 90540576 : start_insn_accesses ();
920 : :
921 : 90540576 : HARD_REG_SET added_regs = {};
922 : 477424292 : FOR_EACH_ARTIFICIAL_USE (ref, cfg_bb->index)
923 : 296343140 : if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
924 : : {
925 : 148171570 : unsigned int regno = DF_REF_REGNO (ref);
926 : 148171570 : machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
927 : 148171570 : if (HARD_REGISTER_NUM_P (regno))
928 : 148171570 : SET_HARD_REG_BIT (added_regs, regno);
929 : :
930 : : // A definition must be available.
931 : 148171570 : gcc_checking_assert (bitmap_bit_p (&lr_info->in, regno)
932 : : || (flags != DF_REF_AT_TOP
933 : : && bitmap_bit_p (&lr_info->def, regno)));
934 : 148171570 : m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, regno }));
935 : : }
936 : :
937 : : // Ensure that global registers and memory are live at the end of any
938 : : // block that has no successors, such as the exit block and non-local gotos.
939 : : // Global registers have to be singled out because they are not part of
940 : : // the DF artifical use list (they are instead treated as used within
941 : : // every block).
942 : 90540576 : if (flags == 0 && EDGE_COUNT (cfg_bb->succs) == 0)
943 : : {
944 : 552126492 : for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
945 : 546189648 : if (global_regs[i] && !TEST_HARD_REG_BIT (added_regs, i))
946 : : {
947 : 30 : auto mode = reg_raw_mode[i];
948 : 30 : m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, i }));
949 : : }
950 : :
951 : 5936844 : auto *use = allocate<use_info> (insn, memory, bi.current_mem_value ());
952 : 5936844 : add_use (use);
953 : 5936844 : m_temp_uses.safe_push (use);
954 : : }
955 : :
956 : 184195960 : FOR_EACH_ARTIFICIAL_DEF (ref, cfg_bb->index)
957 : 3114808 : if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
958 : : {
959 : 1557404 : unsigned int regno = DF_REF_REGNO (ref);
960 : 1557404 : machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
961 : 1557404 : resource_info resource { mode, regno };
962 : :
963 : : // We rely on the def set being correct.
964 : 1557404 : gcc_checking_assert (bitmap_bit_p (&lr_info->def, regno));
965 : :
966 : : // If the value isn't used later in the block and isn't live
967 : : // on exit, we could instead represent the definition as a
968 : : // clobber_info. However, that case should be relatively
969 : : // rare and set_info is any case more compact than clobber_info.
970 : 1557404 : set_info *def = allocate<set_info> (insn, resource);
971 : 1557404 : append_def (def);
972 : 1557404 : m_temp_defs.safe_push (def);
973 : 1557404 : bi.record_reg_def (def);
974 : : }
975 : :
976 : : // Model the effect of a memory clobber on an incoming edge by adding
977 : : // a fake definition of memory at the start of the block. We don't need
978 : : // to add a use of the phi node because memory is implicitly always live.
979 : 90540576 : if (flags == DF_REF_AT_TOP && has_abnormal_call_or_eh_pred_edge_p (cfg_bb))
980 : : {
981 : 780620 : set_info *def = allocate<set_info> (insn, memory);
982 : 780620 : append_def (def);
983 : 780620 : m_temp_defs.safe_push (def);
984 : 780620 : bi.record_mem_def (def);
985 : : }
986 : :
987 : 90540576 : finish_insn_accesses (insn);
988 : 90540576 : }
989 : :
990 : : // Called while building SSA form using BI. Create insn_infos for all
991 : : // relevant instructions in BI.current_bb.
992 : : void
993 : 41411518 : function_info::add_block_contents (build_info &bi)
994 : : {
995 : 41411518 : basic_block cfg_bb = bi.current_bb->cfg_bb ();
996 : 41411518 : rtx_insn *insn;
997 : 537087586 : FOR_BB_INSNS (cfg_bb, insn)
998 : 495676068 : if (INSN_P (insn))
999 : 421077690 : add_insn_to_block (bi, insn);
1000 : 41411518 : }
1001 : :
1002 : : // Called while building SSA form using BI. Record live-out register values
1003 : : // in the phi inputs of successor blocks and create live-out uses where
1004 : : // appropriate. Record the live-out memory value in BI.bb_mem_live_out.
1005 : : void
1006 : 49236998 : function_info::record_block_live_out (build_info &bi)
1007 : : {
1008 : 49236998 : bb_info *bb = bi.current_bb;
1009 : 49236998 : ebb_info *ebb = bi.current_ebb;
1010 : 49236998 : basic_block cfg_bb = bb->cfg_bb ();
1011 : :
1012 : : // Record the live-out register values in the phi inputs of
1013 : : // successor blocks.
1014 : 49236998 : edge e;
1015 : 49236998 : edge_iterator ei;
1016 : 113892463 : FOR_EACH_EDGE (e, ei, cfg_bb->succs)
1017 : : {
1018 : 64655465 : bb_phi_info &phis = bi.bb_phis[e->dest->index];
1019 : 64655465 : unsigned int input_i = e->dest_idx * phis.num_phis;
1020 : 64655465 : unsigned int regno;
1021 : 64655465 : bitmap_iterator out_bi;
1022 : 99674373 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, out_bi)
1023 : : {
1024 : 70037816 : phis.inputs[input_i]
1025 : 35018908 : = live_out_value (bb, bi.current_reg_value (regno));
1026 : 35018908 : input_i += 1;
1027 : : }
1028 : : }
1029 : :
1030 : : // Add the set of registers that were defined in this BB to the set
1031 : : // of potentially-live registers defined in the EBB.
1032 : 98473996 : bitmap_ior_into (bi.ebb_def_regs, &DF_LR_BB_INFO (cfg_bb)->def);
1033 : :
1034 : : // Iterate through the registers in LIVE_OUT and see whether we need
1035 : : // to add a live-out use for them.
1036 : 98314163 : auto record_live_out_regs = [&](bitmap live_out)
1037 : : {
1038 : 49077165 : unsigned int regno;
1039 : 49077165 : bitmap_iterator out_bi;
1040 : 124144397 : EXECUTE_IF_AND_IN_BITMAP (bi.ebb_def_regs, live_out, 0, regno, out_bi)
1041 : : {
1042 : 75067232 : set_info *value = live_out_value (bb, bi.current_reg_value (regno));
1043 : 75067232 : if (value && value->ebb () == ebb)
1044 : 75036410 : add_live_out_use (bb, value);
1045 : : }
1046 : 98314163 : };
1047 : :
1048 : 49236998 : if (bb == ebb->last_bb ())
1049 : : // All live-out registers might need live-out uses.
1050 : 60943956 : record_live_out_regs (DF_LR_OUT (cfg_bb));
1051 : : else
1052 : : // Registers might need live-out uses if they are live on entry
1053 : : // to a successor block in a different EBB.
1054 : 56135227 : FOR_EACH_EDGE (e, ei, cfg_bb->succs)
1055 : : {
1056 : 37370207 : bb_info *dest_bb = this->bb (e->dest);
1057 : 37370207 : if (dest_bb->ebb () != ebb || dest_bb == ebb->first_bb ())
1058 : 37210374 : record_live_out_regs (DF_LR_IN (e->dest));
1059 : : }
1060 : :
1061 : : // Record the live-out memory value.
1062 : 49236998 : bi.bb_mem_live_out[cfg_bb->index]
1063 : 49236998 : = live_out_value (bb, bi.current_mem_value ());
1064 : 49236998 : }
1065 : :
1066 : : // Add BB and its contents to the SSA information.
1067 : : void
1068 : 49344938 : function_info::start_block (build_info &bi, bb_info *bb)
1069 : : {
1070 : 49344938 : ebb_info *ebb = bb->ebb ();
1071 : :
1072 : : // We (need to) add all blocks from one EBB before moving on to the next.
1073 : 49344938 : bi.current_bb = bb;
1074 : 49344938 : if (bb == ebb->first_bb ())
1075 : 30579918 : bi.current_ebb = ebb;
1076 : : else
1077 : 18765020 : gcc_assert (bi.current_ebb == ebb);
1078 : :
1079 : : // Record the start of this block's definitions in the definitions stack.
1080 : 94723166 : bi.old_def_stack_limit.safe_push (bi.def_stack.length ());
1081 : :
1082 : : // Add the block itself.
1083 : 49344938 : append_bb (bb);
1084 : :
1085 : : // If the block starts an EBB, create the phi insn. This insn should exist
1086 : : // for all EBBs, even if they don't (yet) need phis.
1087 : 49344938 : if (bb == ebb->first_bb ())
1088 : 30579918 : ebb->set_phi_insn (append_artificial_insn (bb));
1089 : :
1090 : 49344938 : if (bb->index () == ENTRY_BLOCK)
1091 : : {
1092 : 3966710 : add_entry_block_defs (bi);
1093 : 3966710 : record_block_live_out (bi);
1094 : 3966710 : return;
1095 : : }
1096 : :
1097 : 45378228 : if (EDGE_COUNT (bb->cfg_bb ()->preds) == 0)
1098 : : {
1099 : : // Leave unreachable blocks empty, since there is no useful
1100 : : // liveness information for them, and anything they do will
1101 : : // be wasted work. In a cleaned-up cfg, the only unreachable
1102 : : // block we should see is the exit block of a noreturn function.
1103 : 107940 : bb->set_head_insn (append_artificial_insn (bb));
1104 : 107940 : bb->set_end_insn (append_artificial_insn (bb));
1105 : 107940 : return;
1106 : : }
1107 : :
1108 : : // If the block starts an EBB, create the phi nodes.
1109 : 45270288 : if (bb == ebb->first_bb ())
1110 : 26505268 : add_phi_nodes (bi);
1111 : :
1112 : : // Process the contents of the block.
1113 : 45270288 : add_artificial_accesses (bi, DF_REF_AT_TOP);
1114 : 45270288 : if (bb->index () != EXIT_BLOCK)
1115 : 41411518 : add_block_contents (bi);
1116 : 45270288 : add_artificial_accesses (bi, df_ref_flags ());
1117 : 45270288 : record_block_live_out (bi);
1118 : :
1119 : : // If we needed to calculate a live-in set for debug purposes,
1120 : : // reset it to null at the end of the EBB. Convert the underlying
1121 : : // bitmap to an empty list view, ready for the next calculation.
1122 : 45270288 : if (bi.ebb_live_in_for_debug && bb == ebb->last_bb ())
1123 : : {
1124 : 1155370 : bitmap_clear (bi.tmp_ebb_live_in_for_debug);
1125 : 1155370 : bitmap_list_view (bi.tmp_ebb_live_in_for_debug);
1126 : 1155370 : bi.ebb_live_in_for_debug = nullptr;
1127 : : }
1128 : : }
1129 : :
1130 : : // Finish adding BB and the blocks that it dominates to the SSA information.
1131 : : void
1132 : 49344938 : function_info::end_block (build_info &bi, bb_info *bb)
1133 : : {
1134 : : // Restore the register last_access information to the state it was
1135 : : // in before we started processing BB.
1136 : 49344938 : unsigned int old_limit = bi.old_def_stack_limit.pop ();
1137 : 285556650 : while (bi.def_stack.length () > old_limit)
1138 : : {
1139 : : // We pushed a definition in BB if it was the first dominating
1140 : : // definition (and so the previous entry was null). In other
1141 : : // cases we pushed the previous dominating definition.
1142 : 236211712 : def_info *def = bi.def_stack.pop ();
1143 : 236211712 : unsigned int regno = def->regno ();
1144 : 236211712 : if (def->bb () == bb)
1145 : 128394981 : def = nullptr;
1146 : 236211712 : bi.last_access[regno + 1] = def;
1147 : : }
1148 : 49344938 : }
1149 : :
1150 : : // Finish setting up the phi nodes for each block, now that we've added
1151 : : // the contents of all blocks.
1152 : : void
1153 : 3966710 : function_info::populate_phi_inputs (build_info &bi)
1154 : : {
1155 : 3966710 : auto_vec<phi_info *, 32> sorted_phis;
1156 : 65126546 : for (ebb_info *ebb : ebbs ())
1157 : : {
1158 : 30579918 : if (!ebb->first_phi ())
1159 : 6289232 : continue;
1160 : :
1161 : : // Get a sorted array of EBB's phi nodes.
1162 : 24290686 : basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
1163 : 24290686 : bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
1164 : 24290686 : sorted_phis.truncate (0);
1165 : 81276226 : for (phi_info *phi : ebb->phis ())
1166 : 56985540 : sorted_phis.safe_push (phi);
1167 : 24290686 : std::sort (sorted_phis.address (),
1168 : 48581372 : sorted_phis.address () + sorted_phis.length (),
1169 : : compare_access_infos);
1170 : :
1171 : : // Set the inputs of the non-degenerate register phis. All inputs
1172 : : // for one edge come before all inputs for the next edge.
1173 : 24290686 : set_info **inputs = phis.inputs;
1174 : 24290686 : unsigned int phi_i = 0;
1175 : 24290686 : bitmap_iterator bmi;
1176 : 24290686 : unsigned int regno;
1177 : 37013673 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, bmi)
1178 : : {
1179 : : // Skip intervening degenerate phis.
1180 : 15396656 : while (sorted_phis[phi_i]->regno () < regno)
1181 : 2673669 : phi_i += 1;
1182 : 12722987 : phi_info *phi = sorted_phis[phi_i];
1183 : 12722987 : gcc_assert (phi->regno () == regno);
1184 : 47741895 : for (unsigned int input_i = 0; input_i < phis.num_preds; ++input_i)
1185 : 35018908 : if (set_info *input = inputs[input_i * phis.num_phis])
1186 : : {
1187 : 34996901 : use_info *use = phi->input_use (input_i);
1188 : 34996901 : gcc_assert (!use->def ());
1189 : 34996901 : use->set_def (input);
1190 : 34996901 : add_use (use);
1191 : : }
1192 : 12722987 : phi_i += 1;
1193 : 12722987 : inputs += 1;
1194 : : }
1195 : :
1196 : : // Fill in the backedge inputs to any memory phi.
1197 : 24290686 : phi_info *mem_phi = sorted_phis.last ();
1198 : 24290686 : if (mem_phi->is_mem () && !mem_phi->is_degenerate ())
1199 : : {
1200 : 8612050 : edge e;
1201 : 8612050 : edge_iterator ei;
1202 : 33019131 : FOR_EACH_EDGE (e, ei, cfg_bb->preds)
1203 : : {
1204 : 24407081 : use_info *use = mem_phi->input_use (e->dest_idx);
1205 : 24407081 : if (!use->def ())
1206 : : {
1207 : 2657414 : use->set_def (bi.bb_mem_live_out[e->src->index]);
1208 : 2657414 : add_use (use);
1209 : : }
1210 : : }
1211 : : }
1212 : : }
1213 : 3966710 : }
1214 : :
1215 : : // Return true if it would be better to continue an EBB across NEW_EDGE
1216 : : // rather than across OLD_EDGE, given that both edges are viable candidates.
1217 : : // This is not a total ordering.
1218 : : static bool
1219 : 5165519 : better_ebb_edge_p (edge new_edge, edge old_edge)
1220 : : {
1221 : : // Prefer the likeliest edge.
1222 : 5165519 : if (new_edge->probability.initialized_p ()
1223 : 5164547 : && old_edge->probability.initialized_p ()
1224 : 10330066 : && !(old_edge->probability == new_edge->probability))
1225 : 4240413 : return old_edge->probability < new_edge->probability;
1226 : :
1227 : : // If both edges are equally likely, prefer a fallthru edge.
1228 : 925106 : if (new_edge->flags & EDGE_FALLTHRU)
1229 : : return true;
1230 : : if (old_edge->flags & EDGE_FALLTHRU)
1231 : : return false;
1232 : :
1233 : : // Otherwise just stick with OLD_EDGE.
1234 : : return false;
1235 : : }
1236 : :
1237 : : // Pick and return the next basic block in an EBB that currently ends with BB.
1238 : : // Return null if the EBB must end with BB.
1239 : : static basic_block
1240 : 49344938 : choose_next_block_in_ebb (basic_block bb)
1241 : : {
1242 : : // Although there's nothing in principle wrong with having an EBB that
1243 : : // starts with the entry block and includes later blocks, there's not
1244 : : // really much point either. Keeping the entry block separate means
1245 : : // that uses of arguments consistently occur through phi nodes, rather
1246 : : // than the arguments sometimes appearing to come from an EBB-local
1247 : : // definition instead.
1248 : 49344938 : if (bb->index == ENTRY_BLOCK)
1249 : : return nullptr;
1250 : :
1251 : 45378228 : bool optimize_for_speed_p = optimize_bb_for_speed_p (bb);
1252 : 45378228 : edge best_edge = nullptr;
1253 : 45378228 : edge e;
1254 : 45378228 : edge_iterator ei;
1255 : 106066983 : FOR_EACH_EDGE (e, ei, bb->succs)
1256 : 60688755 : if (!(e->flags & EDGE_COMPLEX)
1257 : 57057876 : && e->dest->index != EXIT_BLOCK
1258 : 114155139 : && single_pred_p (e->dest)
1259 : 25950791 : && optimize_for_speed_p == optimize_bb_for_speed_p (e->dest)
1260 : 84619294 : && (!best_edge || better_ebb_edge_p (e, best_edge)))
1261 : : best_edge = e;
1262 : :
1263 : 45378228 : return best_edge ? best_edge->dest : nullptr;
1264 : : }
1265 : :
1266 : : // Partition the function into extended basic blocks. Create the
1267 : : // associated ebb_infos and bb_infos, but don't add the bb_infos
1268 : : // to the function list yet.
1269 : : void
1270 : 3966710 : function_info::create_ebbs (build_info &bi)
1271 : : {
1272 : : // Compute the starting reverse postorder. We tweak this later to try
1273 : : // to get better EBB assignments.
1274 : 3966710 : auto *postorder = new int[n_basic_blocks_for_fn (m_fn)];
1275 : 3966710 : unsigned int postorder_num
1276 : 3966710 : = pre_and_rev_post_order_compute (nullptr, postorder, true);
1277 : 3966710 : gcc_assert (int (postorder_num) <= n_basic_blocks_for_fn (m_fn));
1278 : :
1279 : : // Iterate over the blocks in reverse postorder. In cases where
1280 : : // multiple possible orders exist, prefer orders that chain blocks
1281 : : // together into EBBs. If multiple possible EBBs exist, try to pick
1282 : : // the ones that are most likely to be profitable.
1283 : 3966710 : auto_vec<bb_info *, 16> bbs;
1284 : 3966710 : unsigned int next_bb_index = 0;
1285 : 53311648 : for (unsigned int i = 0; i < postorder_num; ++i)
1286 : 49344938 : if (!m_bbs[postorder[i]])
1287 : : {
1288 : : // Choose and create the blocks that should form the next EBB.
1289 : 30579918 : basic_block cfg_bb = BASIC_BLOCK_FOR_FN (m_fn, postorder[i]);
1290 : 49344938 : do
1291 : : {
1292 : : // Record the chosen block order in a new RPO.
1293 : 49344938 : bi.bb_to_rpo[cfg_bb->index] = next_bb_index++;
1294 : 49344938 : bbs.safe_push (create_bb_info (cfg_bb));
1295 : 49344938 : cfg_bb = choose_next_block_in_ebb (cfg_bb);
1296 : : }
1297 : 49344938 : while (cfg_bb);
1298 : :
1299 : : // Create the EBB itself.
1300 : 30579918 : auto *ebb = allocate<ebb_info> (bbs[0], bbs.last ());
1301 : 141084692 : for (bb_info *bb : bbs)
1302 : 49344938 : bb->set_ebb (ebb);
1303 : 30579918 : bbs.truncate (0);
1304 : : }
1305 : :
1306 : 3966710 : delete[] postorder;
1307 : 3966710 : }
1308 : :
1309 : : // Partition the function's blocks into EBBs and build SSA form for all
1310 : : // EBBs in the function.
1311 : : void
1312 : 3966710 : function_info::process_all_blocks ()
1313 : : {
1314 : 3966710 : auto temps = temp_watermark ();
1315 : 3966710 : unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
1316 : :
1317 : 3966710 : build_info bi (m_num_regs, num_bb_indices);
1318 : :
1319 : : // ??? There is no dominance information associated with the exit block,
1320 : : // so work out its immediate dominator using predecessor blocks.
1321 : 15985826 : for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
1322 : 4085696 : if (bi.exit_block_dominator)
1323 : 226926 : bi.exit_block_dominator
1324 : 226926 : = nearest_common_dominator (CDI_DOMINATORS,
1325 : : bi.exit_block_dominator, e->src);
1326 : : else
1327 : 3858770 : bi.exit_block_dominator = e->src;
1328 : :
1329 : 3966710 : calculate_potential_phi_regs (bi);
1330 : 3966710 : create_ebbs (bi);
1331 : 3966710 : place_phis (bi);
1332 : 3966710 : bb_walker (this, bi).walk (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
1333 : 3966710 : populate_phi_inputs (bi);
1334 : :
1335 : 3966710 : if (flag_checking)
1336 : : {
1337 : : // The definition stack should be empty and all register definitions
1338 : : // should be back in their original undefined state.
1339 : 7933292 : gcc_assert (bi.def_stack.is_empty ()
1340 : : && bi.old_def_stack_limit.is_empty ());
1341 : 561404077 : for (unsigned int regno = 0; regno < m_num_regs; ++regno)
1342 : 557437431 : gcc_assert (!bi.last_access[regno + 1]);
1343 : : }
1344 : 3966710 : }
1345 : :
1346 : : // Print a description of CALL_CLOBBERS to PP.
1347 : : void
1348 : 0 : rtl_ssa::pp_ebb_call_clobbers (pretty_printer *pp,
1349 : : const ebb_call_clobbers_info *call_clobbers)
1350 : : {
1351 : 0 : if (!call_clobbers)
1352 : 0 : pp_string (pp, "<null>");
1353 : : else
1354 : 0 : call_clobbers->print_full (pp);
1355 : 0 : }
1356 : :
1357 : : // Print a description of BB to PP.
1358 : : void
1359 : 0 : rtl_ssa::pp_bb (pretty_printer *pp, const bb_info *bb)
1360 : : {
1361 : 0 : if (!bb)
1362 : 0 : pp_string (pp, "<null>");
1363 : : else
1364 : 0 : bb->print_full (pp);
1365 : 0 : }
1366 : :
1367 : : // Print a description of EBB to PP
1368 : : void
1369 : 0 : rtl_ssa::pp_ebb (pretty_printer *pp, const ebb_info *ebb)
1370 : : {
1371 : 0 : if (!ebb)
1372 : 0 : pp_string (pp, "<null>");
1373 : : else
1374 : 0 : ebb->print_full (pp);
1375 : 0 : }
1376 : :
1377 : : // Print a description of CALL_CLOBBERS to FILE.
1378 : : void
1379 : 0 : dump (FILE *file, const ebb_call_clobbers_info *call_clobbers)
1380 : : {
1381 : 0 : dump_using (file, pp_ebb_call_clobbers, call_clobbers);
1382 : 0 : }
1383 : :
1384 : : // Print a description of BB to FILE.
1385 : : void
1386 : 0 : dump (FILE *file, const bb_info *bb)
1387 : : {
1388 : 0 : dump_using (file, pp_bb, bb);
1389 : 0 : }
1390 : :
1391 : : // Print a description of EBB to FILE.
1392 : : void
1393 : 0 : dump (FILE *file, const ebb_info *ebb)
1394 : : {
1395 : 0 : dump_using (file, pp_ebb, ebb);
1396 : 0 : }
1397 : :
1398 : : // Debug interfaces to the dump routines above.
1399 : 0 : void debug (const ebb_call_clobbers_info *x) { dump (stderr, x); }
1400 : 0 : void debug (const bb_info *x) { dump (stderr, x); }
1401 : 0 : void debug (const ebb_info *x) { dump (stderr, x); }
|