Line data Source code
1 : // Implementation of basic-block-related functions for RTL SSA -*- C++ -*-
2 : // Copyright (C) 2020-2026 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 5942962 : function_info::build_info::build_info (unsigned int num_regs,
43 5942962 : unsigned int num_bb_indices)
44 5942962 : : current_bb (nullptr),
45 5942962 : current_ebb (nullptr),
46 5942962 : last_access (num_regs + 1),
47 5942962 : ebb_live_in_for_debug (nullptr),
48 5942962 : potential_phi_regs (num_regs),
49 5942962 : bb_phis (num_bb_indices),
50 5942962 : bb_mem_live_out (num_bb_indices),
51 5942962 : bb_to_rpo (num_bb_indices),
52 11885924 : exit_block_dominator (nullptr)
53 : {
54 5942962 : last_access.safe_grow_cleared (num_regs + 1);
55 :
56 5942962 : 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 5942962 : bb_phis.quick_grow_cleared (num_bb_indices);
63 5942962 : bb_mem_live_out.quick_grow (num_bb_indices);
64 5942962 : bb_to_rpo.quick_grow (num_bb_indices);
65 5942962 : if (flag_checking)
66 : {
67 : // Can't do this for bb_phis because it has a constructor.
68 11885740 : memset (bb_mem_live_out.address (), 0xaf,
69 5942870 : num_bb_indices * sizeof (bb_mem_live_out[0]));
70 11885740 : 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 94563204 : for (bb_phi_info &info : bb_phis)
76 76734318 : bitmap_initialize (&info.regs, &bitmap_default_obstack);
77 5942962 : }
78 :
79 5942962 : function_info::build_info::~build_info ()
80 : {
81 94563204 : for (bb_phi_info &info : bb_phis)
82 76734318 : bitmap_release (&info.regs);
83 5942962 : }
84 :
85 : // A dom_walker for populating the basic blocks.
86 11885924 : 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 5942962 : 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 5942962 : m_function (function),
107 5942962 : m_bi (bi),
108 11885924 : m_exit_block_dominator (bi.exit_block_dominator)
109 : {
110 : // If the exit block is unreachable, process it last.
111 5942962 : if (!m_exit_block_dominator)
112 158226 : m_exit_block_dominator = ENTRY_BLOCK_PTR_FOR_FN (m_function->m_fn);
113 5942962 : }
114 :
115 : edge
116 74730939 : function_info::bb_walker::before_dom_children (basic_block bb)
117 : {
118 74730939 : m_function->start_block (m_bi, m_function->bb (bb));
119 74730939 : return nullptr;
120 : }
121 :
122 : void
123 74730939 : function_info::bb_walker::after_dom_children (basic_block bb)
124 : {
125 : // See the comment in the constructor for details.
126 74730939 : if (bb == m_exit_block_dominator)
127 : {
128 5942962 : before_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
129 5942962 : after_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
130 : }
131 74730939 : m_function->end_block (m_bi, m_function->bb (bb));
132 74730939 : }
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 119904051 : 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 119904051 : 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 91964095 : 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 77418828 : auto *use = allocate<use_info> (bb->end_insn (), def->resource (), def);
326 77418828 : use->set_is_live_out_use (true);
327 77418828 : add_use (use);
328 : }
329 :
330 : // Return true if all nondebug uses of DEF are live-out uses.
331 : static bool
332 16067355 : all_uses_are_live_out_uses (set_info *def)
333 : {
334 16097821 : for (use_info *use : def->all_uses ())
335 20207364 : if (!use->is_in_debug_insn () && !use->is_live_out_use ())
336 16067355 : 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 242220654 : 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 242220654 : if (auto *phi = safe_dyn_cast<phi_info *> (set))
348 61752799 : if (phi->is_degenerate ())
349 : {
350 25176833 : 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 25176833 : if (bb == bb->ebb ()->last_bb () && all_uses_are_live_out_uses (phi))
356 5994139 : replace_phi (phi, set);
357 : }
358 :
359 242220654 : 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 23853181 : 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 23853181 : set_info *def = use->def ();
372 23853181 : if (def
373 23853181 : && use->is_reg ()
374 21148654 : && is_single_dominating_def (def)
375 35342969 : && 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 5132162 : auto ebb_cfg_bb = def->ebb ()->first_bb ()->cfg_bb ();
383 5132162 : if (single_pred_p (ebb_cfg_bb))
384 : {
385 3613109 : bb_info *pred_bb = this->bb (single_pred (ebb_cfg_bb));
386 3613109 : if (pred_bb->ebb () == def->ebb ())
387 : {
388 0 : add_live_out_use (pred_bb, def);
389 0 : return;
390 : }
391 : }
392 5132162 : add_live_out_use (def->bb (), def);
393 5132162 : return;
394 : }
395 : }
396 :
397 : // Add PHI to EBB and enter it into the function's hash table.
398 : void
399 100849445 : function_info::append_phi (ebb_info *ebb, phi_info *phi)
400 : {
401 100849445 : phi_info *first_phi = ebb->first_phi ();
402 100849445 : if (first_phi)
403 60972658 : first_phi->set_prev_phi (phi);
404 100849445 : phi->set_next_phi (first_phi);
405 100849445 : ebb->set_first_phi (phi);
406 100849445 : add_def (phi);
407 100849445 : }
408 :
409 : // Remove PHI from its current position in the SSA graph.
410 : void
411 10635051 : function_info::remove_phi (phi_info *phi)
412 : {
413 10635051 : phi_info *next = phi->next_phi ();
414 10635051 : phi_info *prev = phi->prev_phi ();
415 :
416 10635051 : if (next)
417 1282249 : next->set_prev_phi (prev);
418 :
419 10635051 : if (prev)
420 6099118 : prev->set_next_phi (next);
421 : else
422 4535933 : phi->ebb ()->set_first_phi (next);
423 :
424 10635051 : remove_def (phi);
425 10635051 : phi->clear_phi_links ();
426 10635051 : }
427 :
428 : // Remove PHI from the SSA graph and free its memory.
429 : void
430 10635051 : function_info::delete_phi (phi_info *phi)
431 : {
432 10635051 : gcc_assert (!phi->has_any_uses ());
433 :
434 : // Remove the inputs to the phi.
435 32865275 : for (use_info *input : phi->inputs ())
436 11595173 : remove_use (input);
437 :
438 10635051 : remove_phi (phi);
439 :
440 10635051 : phi->set_next_phi (m_free_phis);
441 10635051 : m_free_phis = phi;
442 10635051 : }
443 :
444 : // If possible, remove PHI and replace all uses with NEW_VALUE.
445 : void
446 68756864 : function_info::replace_phi (phi_info *phi, set_info *new_value)
447 : {
448 71925193 : auto update_use = [&](use_info *use)
449 : {
450 3168329 : remove_use (use);
451 3168329 : use->set_def (new_value);
452 3168329 : add_use (use);
453 71925193 : };
454 :
455 68756864 : if (new_value)
456 137530247 : for (use_info *use : phi->nondebug_insn_uses ())
457 58161854 : 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 58147818 : use_info *single_use = nullptr;
462 176263129 : for (auto *use : phi->inputs ())
463 59967493 : if (!single_use)
464 : single_use = use;
465 1819675 : else if (use->def () == new_value)
466 : {
467 1552004 : remove_use (single_use);
468 1552004 : single_use = use;
469 : }
470 : else
471 267671 : remove_use (use);
472 :
473 58147818 : if (single_use->def () != new_value)
474 0 : update_use (single_use);
475 :
476 58147818 : if (phi->is_degenerate ())
477 58147818 : return;
478 :
479 1500247 : phi->make_degenerate (single_use);
480 :
481 : // Redirect all phi users to NEW_VALUE.
482 61977337 : while (use_info *phi_use = phi->last_phi_use ())
483 2329272 : 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 11461353 : while (use_info *use = phi->last_use ())
492 852307 : if (use->is_live_out_use ())
493 13250 : remove_use (use);
494 : else
495 839057 : update_use (use);
496 :
497 10609046 : 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 100849445 : function_info::create_phi (ebb_info *ebb, resource_info resource,
511 : access_info **inputs, unsigned int num_inputs)
512 : {
513 100849445 : phi_info *phi = m_free_phis;
514 100849445 : if (phi)
515 : {
516 5996669 : m_free_phis = phi->next_phi ();
517 5996669 : *phi = phi_info (ebb->phi_insn (), resource, phi->uid ());
518 : }
519 : else
520 : {
521 94852776 : phi = allocate<phi_info> (ebb->phi_insn (), resource, m_next_phi_uid);
522 94852776 : 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 100849445 : machine_mode new_mode = resource.mode;
528 258611254 : for (unsigned int i = 0; i < num_inputs; ++i)
529 : {
530 157761809 : auto *input = safe_as_a<set_info *> (inputs[i]);
531 157761809 : auto *use = allocate<use_info> (phi, resource, input);
532 157761809 : add_use (use);
533 157761809 : inputs[i] = use;
534 157761809 : if (input)
535 101191204 : new_mode = combine_modes (new_mode, input->mode ());
536 : }
537 :
538 100849445 : phi->set_inputs (use_array (inputs, num_inputs));
539 100849445 : phi->set_mode (new_mode);
540 :
541 100849445 : append_phi (ebb, phi);
542 :
543 100849445 : 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 13581 : 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 13581 : def_lookup dl = find_def (def->resource (), ebb->phi_insn ());
564 13581 : if (set_info *set = dl.matching_set ())
565 1018 : return as_a<phi_info *> (set);
566 :
567 12563 : access_info *input = def;
568 12563 : phi_info *phi = create_phi (ebb, def->resource (), &input, 1);
569 12563 : if (def->is_reg ())
570 : {
571 11342 : unsigned int regno = def->regno ();
572 :
573 : // Find the single predecessor mentioned above.
574 11342 : basic_block pred_cfg_bb = single_pred (ebb->first_bb ()->cfg_bb ());
575 11342 : bb_info *pred_bb = this->bb (pred_cfg_bb);
576 :
577 22684 : 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 17644 : bitmap_set_bit (DF_LR_OUT (pred_cfg_bb), regno);
582 8822 : 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 2520 : insn_info *next_insn = nullptr;
589 2520 : if (def_info *next_def = phi->next_def ())
590 2493 : next_insn = next_def->insn ();
591 6453 : for (bb_info *bb : ebb->bbs ())
592 : {
593 3931 : if ((next_insn && *next_insn <= *bb->end_insn ())
594 11847 : || !bitmap_bit_p (DF_LR_OUT (bb->cfg_bb ()), regno))
595 : break;
596 3933 : 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 74730939 : function_info::create_bb_info (basic_block cfg_bb)
606 : {
607 74730939 : bb_info *bb = allocate<bb_info> (cfg_bb);
608 74730939 : gcc_checking_assert (!m_bbs[cfg_bb->index]);
609 74730939 : m_bbs[cfg_bb->index] = bb;
610 74730939 : return bb;
611 : }
612 :
613 : // Add BB to the end of the list of blocks.
614 : void
615 74730939 : function_info::append_bb (bb_info *bb)
616 : {
617 74730939 : if (m_last_bb)
618 68787977 : m_last_bb->set_next_bb (bb);
619 : else
620 5942962 : m_first_bb = bb;
621 74730939 : bb->set_prev_bb (m_last_bb);
622 74730939 : m_last_bb = bb;
623 74730939 : }
624 :
625 : // Calculate BI.potential_phi_regs and BI.potential_phi_regs_for_debug.
626 : void
627 5942962 : function_info::calculate_potential_phi_regs (build_info &bi)
628 : {
629 5942962 : auto *lr_info = DF_LR_BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
630 5942962 : bool is_debug = MAY_HAVE_DEBUG_INSNS;
631 849337300 : for (unsigned int regno = 0; regno < m_num_regs; ++regno)
632 843394338 : 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 843369158 : || DF_REG_DEF_COUNT (regno) > 1
638 1440158424 : || (!bitmap_bit_p (&lr_info->def, regno)
639 552089687 : && bitmap_bit_p (&lr_info->out, regno)))
640 : {
641 246970013 : bitmap_set_bit (bi.potential_phi_regs, regno);
642 246970013 : if (is_debug)
643 152007247 : bitmap_set_bit (bi.potential_phi_regs_for_debug, regno);
644 : }
645 5942962 : }
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 5942962 : function_info::place_phis (build_info &bi)
651 : {
652 5942962 : unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
653 :
654 : // Calculate dominance frontiers.
655 5942962 : auto_vec<bitmap_head> frontiers;
656 5942962 : frontiers.safe_grow_cleared (num_bb_indices);
657 82677280 : for (unsigned int i = 0; i < num_bb_indices; ++i)
658 76734318 : bitmap_initialize (&frontiers[i], &bitmap_default_obstack);
659 11885924 : 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 24019024 : for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
665 : {
666 6190138 : basic_block bb = e->src;
667 7799997 : while (bb != bi.exit_block_dominator)
668 : {
669 1609859 : bitmap_set_bit (&frontiers[bb->index], EXIT_BLOCK);
670 1609859 : 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 5942962 : auto_vec<bitmap_head> unfiltered;
681 5942962 : unfiltered.safe_grow_cleared (num_bb_indices);
682 82677280 : for (unsigned int i = 0; i < num_bb_indices; ++i)
683 76734318 : 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 5942962 : auto_bitmap worklist;
688 82677280 : for (unsigned int b1 = 0; b1 < num_bb_indices; ++b1)
689 : {
690 : // Only access DF information for blocks that are known to exist.
691 76734318 : if (bitmap_empty_p (&frontiers[b1]))
692 28702738 : continue;
693 :
694 : // Defs in B1 that are possibly in LR_IN in the dominance frontier
695 : // blocks.
696 48031580 : auto_bitmap b1_def;
697 96063160 : bitmap_and (b1_def, &DF_LR_BB_INFO (BASIC_BLOCK_FOR_FN (m_fn, b1))->def,
698 96063160 : DF_LR_OUT (BASIC_BLOCK_FOR_FN (m_fn, b1)));
699 :
700 48031580 : bitmap_iterator bmi;
701 48031580 : unsigned int b2;
702 126140829 : EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
703 78109249 : if (bitmap_ior_into (&unfiltered[b2], b1_def)
704 78109249 : && !bitmap_empty_p (&frontiers[b2]))
705 : // Propagate the (potential) new phi node definitions in B2.
706 20800139 : bitmap_set_bit (worklist, b2);
707 48031580 : }
708 :
709 16319243 : while (!bitmap_empty_p (worklist))
710 : {
711 10376281 : unsigned int b1 = bitmap_first_set_bit (worklist);
712 10376281 : bitmap_clear_bit (worklist, b1);
713 :
714 : // Restrict the phi nodes to registers that are live on entry to
715 : // the block.
716 10376281 : bitmap b1_in = DF_LR_IN (BASIC_BLOCK_FOR_FN (m_fn, b1));
717 10376281 : bitmap b1_phis = &bi.bb_phis[b1].regs;
718 10376281 : if (!bitmap_ior_and_into (b1_phis, &unfiltered[b1], b1_in))
719 1802538 : 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 8573743 : bitmap_iterator bmi;
724 8573743 : unsigned int b2;
725 24443994 : EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
726 15870251 : if (bitmap_ior_into (&unfiltered[b2], b1_phis)
727 15870251 : && !bitmap_empty_p (&frontiers[b2]))
728 2365729 : bitmap_set_bit (worklist, b2);
729 : }
730 :
731 5942962 : basic_block cfg_bb;
732 80673901 : 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 74730939 : unsigned int i = cfg_bb->index;
737 74730939 : bb_phi_info &phis = bi.bb_phis[i];
738 74730939 : if (bitmap_empty_p (&frontiers[i]))
739 53398718 : 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 74730939 : phis.num_phis = bitmap_count_bits (&phis.regs);
744 74730939 : phis.num_preds = EDGE_COUNT (cfg_bb->preds);
745 74730939 : unsigned int num_inputs = phis.num_phis * phis.num_preds;
746 74730939 : if (num_inputs != 0)
747 : {
748 10380561 : phis.inputs = XOBNEWVEC (&m_temp_obstack, set_info *, num_inputs);
749 10380561 : memset (phis.inputs, 0, num_inputs * sizeof (phis.inputs[0]));
750 : }
751 : }
752 :
753 : // Free the temporary bitmaps.
754 82677280 : for (unsigned int i = 0; i < num_bb_indices; ++i)
755 : {
756 76734318 : bitmap_release (&frontiers[i]);
757 76734318 : bitmap_release (&unfiltered[i]);
758 : }
759 5942962 : }
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 5942962 : function_info::add_entry_block_defs (build_info &bi)
771 : {
772 5942962 : bb_info *bb = bi.current_bb;
773 5942962 : basic_block cfg_bb = bi.current_bb->cfg_bb ();
774 5942962 : auto *lr_info = DF_LR_BB_INFO (cfg_bb);
775 :
776 5942962 : bb->set_head_insn (append_artificial_insn (bb));
777 5942962 : insn_info *insn = append_artificial_insn (bb);
778 5942962 : bb->set_end_insn (insn);
779 :
780 5942962 : 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 5942962 : unsigned int regno;
789 5942962 : bitmap_iterator in_bi;
790 33913358 : EXECUTE_IF_SET_IN_BITMAP (&lr_info->out, 0, regno, in_bi)
791 : {
792 27970396 : auto *set = allocate<set_info> (insn, full_register (regno));
793 27970396 : append_def (set);
794 27970396 : m_temp_defs.safe_push (set);
795 27970396 : bi.record_reg_def (set);
796 : }
797 :
798 : // Create a definition that reflects the state of memory on entry to
799 : // the function.
800 5942962 : auto *set = allocate<set_info> (insn, memory);
801 5942962 : append_def (set);
802 5942962 : m_temp_defs.safe_push (set);
803 5942962 : bi.record_mem_def (set);
804 :
805 5942962 : finish_insn_accesses (insn);
806 5942962 : }
807 :
808 : // Lazily calculate the value of BI.ebb_live_in_for_debug for BI.current_ebb.
809 : void
810 2135111 : function_info::calculate_ebb_live_in_for_debug (build_info &bi)
811 : {
812 2135111 : gcc_checking_assert (bitmap_empty_p (bi.tmp_ebb_live_in_for_debug));
813 2135111 : bi.ebb_live_in_for_debug = bi.tmp_ebb_live_in_for_debug;
814 4270222 : bitmap_and (bi.ebb_live_in_for_debug, bi.potential_phi_regs_for_debug,
815 4270222 : DF_LR_IN (bi.current_ebb->first_bb ()->cfg_bb ()));
816 2135111 : bitmap_tree_view (bi.ebb_live_in_for_debug);
817 2135111 : }
818 :
819 : // Called while building SSA form using BI. Create phi nodes for the
820 : // current EBB.
821 : void
822 39876204 : function_info::add_phi_nodes (build_info &bi)
823 : {
824 39876204 : ebb_info *ebb = bi.current_ebb;
825 39876204 : basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
826 :
827 : // Create the register phis for this EBB.
828 39876204 : bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
829 39876204 : unsigned int num_preds = phis.num_preds;
830 39876204 : unsigned int regno;
831 39876204 : bitmap_iterator in_bi;
832 59518010 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, in_bi)
833 : {
834 19641806 : 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 19641806 : auto *inputs = XOBNEWVEC (&m_obstack, access_info *, num_preds);
838 19641806 : 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 19641806 : phi_info *phi = create_phi (ebb, { E_BLKmode, regno },
843 : inputs, num_preds);
844 19641806 : bi.record_reg_def (phi);
845 : }
846 :
847 39876204 : 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 39876204 : m_temp_defs.reserve (num_preds);
852 39876204 : set_info *mem_value = nullptr;
853 39876204 : bool mem_phi_is_degenerate = true;
854 39876204 : edge e;
855 39876204 : edge_iterator ei;
856 109041771 : FOR_EACH_EDGE (e, ei, cfg_bb->preds)
857 : {
858 69165567 : bb_info *pred_bb = this->bb (e->src);
859 69165567 : if (pred_bb && pred_bb->head_insn ())
860 : {
861 65195202 : mem_value = bi.bb_mem_live_out[pred_bb->index ()];
862 65195202 : m_temp_defs.quick_push (mem_value);
863 65195202 : if (mem_value != m_temp_defs[0])
864 21573067 : mem_phi_is_degenerate = false;
865 : }
866 : else
867 : {
868 3970365 : m_temp_defs.quick_push (nullptr);
869 3970365 : 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 39876204 : if (mem_phi_is_degenerate)
876 : {
877 26649907 : access_info *input[] = { mem_value };
878 26649907 : mem_value = create_phi (ebb, memory, input, 1);
879 : }
880 : else
881 : {
882 13226297 : obstack_grow (&m_obstack, m_temp_defs.address (),
883 : num_preds * sizeof (access_info *));
884 13226297 : auto *inputs = static_cast<access_info **> (obstack_finish (&m_obstack));
885 13226297 : mem_value = create_phi (ebb, memory, inputs, num_preds);
886 : }
887 39876204 : bi.record_mem_def (mem_value);
888 39876204 : m_temp_defs.truncate (0);
889 39876204 : }
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 137259502 : function_info::add_artificial_accesses (build_info &bi, df_ref_flags flags)
898 : {
899 137259502 : bb_info *bb = bi.current_bb;
900 137259502 : basic_block cfg_bb = bb->cfg_bb ();
901 137259502 : auto *lr_info = DF_LR_BB_INFO (cfg_bb);
902 137259502 : df_ref ref;
903 :
904 137259502 : insn_info *insn;
905 137259502 : if (flags == DF_REF_AT_TOP)
906 : {
907 68629751 : if (cfg_bb->index == EXIT_BLOCK)
908 5784736 : insn = append_artificial_insn (bb);
909 : else
910 62845015 : insn = append_artificial_insn (bb, bb_note (cfg_bb));
911 68629751 : bb->set_head_insn (insn);
912 : }
913 : else
914 : {
915 68629751 : insn = append_artificial_insn (bb);
916 68629751 : bb->set_end_insn (insn);
917 : }
918 :
919 137259502 : start_insn_accesses ();
920 :
921 137259502 : HARD_REG_SET added_regs = {};
922 692741300 : FOR_EACH_ARTIFICIAL_USE (ref, cfg_bb->index)
923 418222296 : if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
924 : {
925 209111148 : unsigned int regno = DF_REF_REGNO (ref);
926 209111148 : machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
927 209111148 : if (HARD_REGISTER_NUM_P (regno))
928 209111148 : SET_HARD_REG_BIT (added_regs, regno);
929 :
930 : // A definition must be available.
931 209111148 : 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 209111148 : 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 137259502 : if (flags == 0 && EDGE_COUNT (cfg_bb->succs) == 0)
943 : {
944 815240976 : for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
945 806474944 : if (global_regs[i] && !TEST_HARD_REG_BIT (added_regs, i))
946 : {
947 44 : auto mode = reg_raw_mode[i];
948 44 : m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, i }));
949 : }
950 :
951 8766032 : auto *use = allocate<use_info> (insn, memory, bi.current_mem_value ());
952 8766032 : add_use (use);
953 8766032 : m_temp_uses.safe_push (use);
954 : }
955 :
956 278796584 : FOR_EACH_ARTIFICIAL_DEF (ref, cfg_bb->index)
957 4277580 : if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
958 : {
959 2138790 : unsigned int regno = DF_REF_REGNO (ref);
960 2138790 : machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
961 2138790 : resource_info resource { mode, regno };
962 :
963 : // We rely on the def set being correct.
964 2138790 : 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 2138790 : set_info *def = allocate<set_info> (insn, resource);
971 2138790 : append_def (def);
972 2138790 : m_temp_defs.safe_push (def);
973 2138790 : 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 137259502 : if (flags == DF_REF_AT_TOP && has_abnormal_call_or_eh_pred_edge_p (cfg_bb))
980 : {
981 1072173 : set_info *def = allocate<set_info> (insn, memory);
982 1072173 : append_def (def);
983 1072173 : m_temp_defs.safe_push (def);
984 1072173 : bi.record_mem_def (def);
985 : }
986 :
987 137259502 : finish_insn_accesses (insn);
988 137259502 : }
989 :
990 : // Called while building SSA form using BI. Create insn_infos for all
991 : // relevant instructions in BI.current_bb.
992 : void
993 62845015 : function_info::add_block_contents (build_info &bi)
994 : {
995 62845015 : basic_block cfg_bb = bi.current_bb->cfg_bb ();
996 62845015 : rtx_insn *insn;
997 820169992 : FOR_BB_INSNS (cfg_bb, insn)
998 757324977 : if (INSN_P (insn))
999 638269004 : add_insn_to_block (bi, insn);
1000 62845015 : }
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 74572713 : function_info::record_block_live_out (build_info &bi)
1007 : {
1008 74572713 : bb_info *bb = bi.current_bb;
1009 74572713 : ebb_info *ebb = bi.current_ebb;
1010 74572713 : basic_block cfg_bb = bb->cfg_bb ();
1011 :
1012 : // Record the live-out register values in the phi inputs of
1013 : // successor blocks.
1014 74572713 : edge e;
1015 74572713 : edge_iterator ei;
1016 172491827 : FOR_EACH_EDGE (e, ei, cfg_bb->succs)
1017 : {
1018 97919114 : bb_phi_info &phis = bi.bb_phis[e->dest->index];
1019 97919114 : unsigned int input_i = e->dest_idx * phis.num_phis;
1020 97919114 : unsigned int regno;
1021 97919114 : bitmap_iterator out_bi;
1022 150519354 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, out_bi)
1023 : {
1024 105200480 : phis.inputs[input_i]
1025 52600240 : = live_out_value (bb, bi.current_reg_value (regno));
1026 52600240 : 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 149145426 : 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 148530472 : auto record_live_out_regs = [&](bitmap live_out)
1037 : {
1038 73957759 : unsigned int regno;
1039 73957759 : bitmap_iterator out_bi;
1040 189005460 : EXECUTE_IF_AND_IN_BITMAP (bi.ebb_def_regs, live_out, 0, regno, out_bi)
1041 : {
1042 115047701 : set_info *value = live_out_value (bb, bi.current_reg_value (regno));
1043 115047701 : if (value && value->ebb () == ebb)
1044 114759134 : add_live_out_use (bb, value);
1045 : }
1046 148530472 : };
1047 :
1048 74572713 : if (bb == ebb->last_bb ())
1049 : // All live-out registers might need live-out uses.
1050 91638332 : 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 85645687 : FOR_EACH_EDGE (e, ei, cfg_bb->succs)
1055 : {
1056 56892140 : bb_info *dest_bb = this->bb (e->dest);
1057 56892140 : if (dest_bb->ebb () != ebb || dest_bb == ebb->first_bb ())
1058 56277186 : record_live_out_regs (DF_LR_IN (e->dest));
1059 : }
1060 :
1061 : // Record the live-out memory value.
1062 74572713 : bi.bb_mem_live_out[cfg_bb->index]
1063 74572713 : = live_out_value (bb, bi.current_mem_value ());
1064 74572713 : }
1065 :
1066 : // Add BB and its contents to the SSA information.
1067 : void
1068 74730939 : function_info::start_block (build_info &bi, bb_info *bb)
1069 : {
1070 74730939 : ebb_info *ebb = bb->ebb ();
1071 :
1072 : // We (need to) add all blocks from one EBB before moving on to the next.
1073 74730939 : bi.current_bb = bb;
1074 74730939 : if (bb == ebb->first_bb ())
1075 45977392 : bi.current_ebb = ebb;
1076 : else
1077 28753547 : gcc_assert (bi.current_ebb == ebb);
1078 :
1079 : // Record the start of this block's definitions in the definitions stack.
1080 143518916 : bi.old_def_stack_limit.safe_push (bi.def_stack.length ());
1081 :
1082 : // Add the block itself.
1083 74730939 : 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 74730939 : if (bb == ebb->first_bb ())
1088 45977392 : ebb->set_phi_insn (append_artificial_insn (bb));
1089 :
1090 74730939 : if (bb->index () == ENTRY_BLOCK)
1091 : {
1092 5942962 : add_entry_block_defs (bi);
1093 5942962 : record_block_live_out (bi);
1094 5942962 : return;
1095 : }
1096 :
1097 68787977 : 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 158226 : bb->set_head_insn (append_artificial_insn (bb));
1104 158226 : bb->set_end_insn (append_artificial_insn (bb));
1105 158226 : return;
1106 : }
1107 :
1108 : // If the block starts an EBB, create the phi nodes.
1109 68629751 : if (bb == ebb->first_bb ())
1110 39876204 : add_phi_nodes (bi);
1111 :
1112 : // Process the contents of the block.
1113 68629751 : add_artificial_accesses (bi, DF_REF_AT_TOP);
1114 68629751 : if (bb->index () != EXIT_BLOCK)
1115 62845015 : add_block_contents (bi);
1116 68629751 : add_artificial_accesses (bi, df_ref_flags ());
1117 68629751 : 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 68629751 : if (bi.ebb_live_in_for_debug && bb == ebb->last_bb ())
1123 : {
1124 2135111 : bitmap_clear (bi.tmp_ebb_live_in_for_debug);
1125 2135111 : bitmap_list_view (bi.tmp_ebb_live_in_for_debug);
1126 2135111 : 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 74730939 : 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 74730939 : unsigned int old_limit = bi.old_def_stack_limit.pop ();
1137 425681864 : 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 350950925 : def_info *def = bi.def_stack.pop ();
1143 350950925 : unsigned int regno = def->regno ();
1144 350950925 : if (def->bb () == bb)
1145 175573689 : def = nullptr;
1146 350950925 : bi.last_access[regno + 1] = def;
1147 : }
1148 74730939 : }
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 5942962 : function_info::populate_phi_inputs (build_info &bi)
1154 : {
1155 5942962 : auto_vec<phi_info *, 32> sorted_phis;
1156 97897746 : for (ebb_info *ebb : ebbs ())
1157 : {
1158 45977392 : if (!ebb->first_phi ())
1159 8959000 : continue;
1160 :
1161 : // Get a sorted array of EBB's phi nodes.
1162 37018392 : basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
1163 37018392 : bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
1164 37018392 : sorted_phis.truncate (0);
1165 131861135 : for (phi_info *phi : ebb->phis ())
1166 94842743 : sorted_phis.safe_push (phi);
1167 37018392 : std::sort (sorted_phis.address (),
1168 74036784 : 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 37018392 : set_info **inputs = phis.inputs;
1174 37018392 : unsigned int phi_i = 0;
1175 37018392 : bitmap_iterator bmi;
1176 37018392 : unsigned int regno;
1177 56660198 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, bmi)
1178 : {
1179 : // Skip intervening degenerate phis.
1180 24776407 : while (sorted_phis[phi_i]->regno () < regno)
1181 5134601 : phi_i += 1;
1182 19641806 : phi_info *phi = sorted_phis[phi_i];
1183 19641806 : gcc_assert (phi->regno () == regno);
1184 72242046 : for (unsigned int input_i = 0; input_i < phis.num_preds; ++input_i)
1185 52600240 : if (set_info *input = inputs[input_i * phis.num_phis])
1186 : {
1187 52577130 : use_info *use = phi->input_use (input_i);
1188 52577130 : gcc_assert (!use->def ());
1189 52577130 : use->set_def (input);
1190 52577130 : add_use (use);
1191 : }
1192 19641806 : phi_i += 1;
1193 19641806 : inputs += 1;
1194 : }
1195 :
1196 : // Fill in the backedge inputs to any memory phi.
1197 37018392 : phi_info *mem_phi = sorted_phis.last ();
1198 37018392 : if (mem_phi->is_mem () && !mem_phi->is_degenerate ())
1199 : {
1200 13226297 : edge e;
1201 13226297 : edge_iterator ei;
1202 50406524 : FOR_EACH_EDGE (e, ei, cfg_bb->preds)
1203 : {
1204 37180227 : use_info *use = mem_phi->input_use (e->dest_idx);
1205 37180227 : if (!use->def ())
1206 : {
1207 3970365 : use->set_def (bi.bb_mem_live_out[e->src->index]);
1208 3970365 : add_use (use);
1209 : }
1210 : }
1211 : }
1212 : }
1213 5942962 : }
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 7572106 : better_ebb_edge_p (edge new_edge, edge old_edge)
1220 : {
1221 : // Prefer the likeliest edge.
1222 7572106 : if (new_edge->probability.initialized_p ()
1223 7570545 : && old_edge->probability.initialized_p ()
1224 15142651 : && !(old_edge->probability == new_edge->probability))
1225 6206139 : return old_edge->probability < new_edge->probability;
1226 :
1227 : // If both edges are equally likely, prefer a fallthru edge.
1228 1365967 : 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 74730939 : 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 74730939 : if (bb->index == ENTRY_BLOCK)
1249 : return nullptr;
1250 :
1251 68787977 : bool optimize_for_speed_p = optimize_bb_for_speed_p (bb);
1252 68787977 : edge best_edge = nullptr;
1253 68787977 : edge e;
1254 68787977 : edge_iterator ei;
1255 160764129 : FOR_EACH_EDGE (e, ei, bb->succs)
1256 91976152 : if (!(e->flags & EDGE_COMPLEX)
1257 86870500 : && e->dest->index != EXIT_BLOCK
1258 173427957 : && single_pred_p (e->dest)
1259 39508604 : && optimize_for_speed_p == optimize_bb_for_speed_p (e->dest)
1260 128301805 : && (!best_edge || better_ebb_edge_p (e, best_edge)))
1261 : best_edge = e;
1262 :
1263 68787977 : 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 5942962 : 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 5942962 : auto *postorder = new int[n_basic_blocks_for_fn (m_fn)];
1275 5942962 : unsigned int postorder_num
1276 5942962 : = pre_and_rev_post_order_compute (nullptr, postorder, true);
1277 5942962 : 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 5942962 : auto_vec<bb_info *, 16> bbs;
1284 5942962 : unsigned int next_bb_index = 0;
1285 80673901 : for (unsigned int i = 0; i < postorder_num; ++i)
1286 74730939 : if (!m_bbs[postorder[i]])
1287 : {
1288 : // Choose and create the blocks that should form the next EBB.
1289 45977392 : basic_block cfg_bb = BASIC_BLOCK_FOR_FN (m_fn, postorder[i]);
1290 74730939 : do
1291 : {
1292 : // Record the chosen block order in a new RPO.
1293 74730939 : bi.bb_to_rpo[cfg_bb->index] = next_bb_index++;
1294 74730939 : bbs.safe_push (create_bb_info (cfg_bb));
1295 74730939 : cfg_bb = choose_next_block_in_ebb (cfg_bb);
1296 : }
1297 74730939 : while (cfg_bb);
1298 :
1299 : // Create the EBB itself.
1300 45977392 : auto *ebb = allocate<ebb_info> (bbs[0], bbs.last ());
1301 212663115 : for (bb_info *bb : bbs)
1302 74730939 : bb->set_ebb (ebb);
1303 45977392 : bbs.truncate (0);
1304 : }
1305 :
1306 5942962 : delete[] postorder;
1307 5942962 : }
1308 :
1309 : // Partition the function's blocks into EBBs and build SSA form for all
1310 : // EBBs in the function.
1311 : void
1312 5942962 : function_info::process_all_blocks ()
1313 : {
1314 5942962 : auto temps = temp_watermark ();
1315 5942962 : unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
1316 :
1317 5942962 : 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 24019024 : for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
1322 6190138 : if (bi.exit_block_dominator)
1323 405402 : bi.exit_block_dominator
1324 405402 : = nearest_common_dominator (CDI_DOMINATORS,
1325 : bi.exit_block_dominator, e->src);
1326 : else
1327 5784736 : bi.exit_block_dominator = e->src;
1328 :
1329 5942962 : calculate_potential_phi_regs (bi);
1330 5942962 : create_ebbs (bi);
1331 5942962 : place_phis (bi);
1332 5942962 : bb_walker (this, bi).walk (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
1333 5942962 : populate_phi_inputs (bi);
1334 :
1335 5942962 : 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 11885740 : gcc_assert (bi.def_stack.is_empty ()
1340 : && bi.old_def_stack_limit.is_empty ());
1341 849327637 : for (unsigned int regno = 0; regno < m_num_regs; ++regno)
1342 843384767 : gcc_assert (!bi.last_access[regno + 1]);
1343 : }
1344 5942962 : }
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); }
|