Branch data Line data Source code
1 : : // Implementation of basic-block-related functions for RTL SSA -*- C++ -*-
2 : : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 : : //
4 : : // This file is part of GCC.
5 : : //
6 : : // GCC is free software; you can redistribute it and/or modify it under
7 : : // the terms of the GNU General Public License as published by the Free
8 : : // Software Foundation; either version 3, or (at your option) any later
9 : : // version.
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 : 3847662 : function_info::build_info::build_info (unsigned int num_regs,
43 : 3847662 : unsigned int num_bb_indices)
44 : 3847662 : : current_bb (nullptr),
45 : 3847662 : current_ebb (nullptr),
46 : 3847662 : last_access (num_regs + 1),
47 : 3847662 : ebb_live_in_for_debug (nullptr),
48 : 3847662 : potential_phi_regs (num_regs),
49 : 3847662 : bb_phis (num_bb_indices),
50 : 3847662 : bb_mem_live_out (num_bb_indices),
51 : 3847662 : bb_to_rpo (num_bb_indices),
52 : 7695324 : exit_block_dominator (nullptr)
53 : : {
54 : 3847662 : last_access.safe_grow_cleared (num_regs + 1);
55 : :
56 : 3847662 : 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 : 3847662 : bb_phis.quick_grow_cleared (num_bb_indices);
63 : 3847662 : bb_mem_live_out.quick_grow (num_bb_indices);
64 : 3847662 : bb_to_rpo.quick_grow (num_bb_indices);
65 : 3847662 : if (flag_checking)
66 : : {
67 : : // Can't do this for bb_phis because it has a constructor.
68 : 7695196 : memset (bb_mem_live_out.address (), 0xaf,
69 : 3847598 : num_bb_indices * sizeof (bb_mem_live_out[0]));
70 : 7695196 : 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 : 58964364 : for (bb_phi_info &info : bb_phis)
76 : 47421378 : bitmap_initialize (&info.regs, &bitmap_default_obstack);
77 : 3847662 : }
78 : :
79 : 3847662 : function_info::build_info::~build_info ()
80 : : {
81 : 58964364 : for (bb_phi_info &info : bb_phis)
82 : 47421378 : bitmap_release (&info.regs);
83 : 3847662 : }
84 : :
85 : : // A dom_walker for populating the basic blocks.
86 : 7695324 : 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 : 3847662 : 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 : 3847662 : m_function (function),
107 : 3847662 : m_bi (bi),
108 : 7695324 : m_exit_block_dominator (bi.exit_block_dominator)
109 : : {
110 : : // If the exit block is unreachable, process it last.
111 : 3847662 : if (!m_exit_block_dominator)
112 : 94500 : m_exit_block_dominator = ENTRY_BLOCK_PTR_FOR_FN (m_function->m_fn);
113 : 3847662 : }
114 : :
115 : : edge
116 : 46128912 : function_info::bb_walker::before_dom_children (basic_block bb)
117 : : {
118 : 46128912 : m_function->start_block (m_bi, m_function->bb (bb));
119 : 46128912 : return nullptr;
120 : : }
121 : :
122 : : void
123 : 46128912 : function_info::bb_walker::after_dom_children (basic_block bb)
124 : : {
125 : : // See the comment in the constructor for details.
126 : 46128912 : if (bb == m_exit_block_dominator)
127 : : {
128 : 3847662 : before_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
129 : 3847662 : after_dom_children (EXIT_BLOCK_PTR_FOR_FN (m_function->m_fn));
130 : : }
131 : 46128912 : m_function->end_block (m_bi, m_function->bb (bb));
132 : 46128912 : }
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 : 71092470 : 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 : 71092470 : 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 : 53239909 : use_info *use = def->last_nondebug_insn_use ();
319 : 30011971 : if (use && use->insn () == bb->end_insn ())
320 : : return;
321 : :
322 : : // Currently there is no need to maintain a backward link from the end
323 : : // instruction to the list of live-out uses. Such a list would be
324 : : // expensive to update if it was represented using the usual insn_info
325 : : // access arrays.
326 : 46839009 : use = allocate<use_info> (bb->end_insn (), def->resource (), def);
327 : 46839009 : use->set_is_live_out_use (true);
328 : 46839009 : add_use (use);
329 : : }
330 : :
331 : : // Return true if all nondebug uses of DEF are live-out uses.
332 : : static bool
333 : 10181235 : all_uses_are_live_out_uses (set_info *def)
334 : : {
335 : 10201765 : for (use_info *use : def->all_uses ())
336 : 12862084 : if (!use->is_in_debug_insn () && !use->is_live_out_use ())
337 : 10181235 : return false;
338 : : return true;
339 : : }
340 : :
341 : : // SET, if nonnull, is a definition of something that is live out from BB.
342 : : // Return the live-out value itself.
343 : : set_info *
344 : 149969964 : function_info::live_out_value (bb_info *bb, set_info *set)
345 : : {
346 : : // Degenerate phis only exist to provide a definition for uses in the
347 : : // same EBB. The live-out value is the same as the live-in value.
348 : 149969964 : if (auto *phi = safe_dyn_cast<phi_info *> (set))
349 : 39595804 : if (phi->is_degenerate ())
350 : : {
351 : 15876286 : set = phi->input_value (0);
352 : :
353 : : // Remove the phi if it turned out to be useless. This is
354 : : // mainly useful for memory, because we don't know ahead of time
355 : : // whether a block will use memory or not.
356 : 15876286 : if (bb == bb->ebb ()->last_bb () && all_uses_are_live_out_uses (phi))
357 : 3770723 : replace_phi (phi, set);
358 : : }
359 : :
360 : 149969964 : return set;
361 : : }
362 : :
363 : : // Add PHI to EBB and enter it into the function's hash table.
364 : : void
365 : 57229638 : function_info::append_phi (ebb_info *ebb, phi_info *phi)
366 : : {
367 : 57229638 : phi_info *first_phi = ebb->first_phi ();
368 : 57229638 : if (first_phi)
369 : 32476609 : first_phi->set_prev_phi (phi);
370 : 57229638 : phi->set_next_phi (first_phi);
371 : 57229638 : ebb->set_first_phi (phi);
372 : 57229638 : add_def (phi);
373 : 57229638 : }
374 : :
375 : : // Remove PHI from its current position in the SSA graph.
376 : : void
377 : 6670965 : function_info::remove_phi (phi_info *phi)
378 : : {
379 : 6670965 : phi_info *next = phi->next_phi ();
380 : 6670965 : phi_info *prev = phi->prev_phi ();
381 : :
382 : 6670965 : if (next)
383 : 890897 : next->set_prev_phi (prev);
384 : :
385 : 6670965 : if (prev)
386 : 3389466 : prev->set_next_phi (next);
387 : : else
388 : 3281499 : phi->ebb ()->set_first_phi (next);
389 : :
390 : 6670965 : remove_def (phi);
391 : 6670965 : phi->clear_phi_links ();
392 : 6670965 : }
393 : :
394 : : // Remove PHI from the SSA graph and free its memory.
395 : : void
396 : 6670965 : function_info::delete_phi (phi_info *phi)
397 : : {
398 : 6670965 : gcc_assert (!phi->has_any_uses ());
399 : :
400 : : // Remove the inputs to the phi.
401 : 20630777 : for (use_info *input : phi->inputs ())
402 : 7288847 : remove_use (input);
403 : :
404 : 6670965 : remove_phi (phi);
405 : :
406 : 6670965 : phi->set_next_phi (m_free_phis);
407 : 6670965 : m_free_phis = phi;
408 : 6670965 : }
409 : :
410 : : // If possible, remove PHI and replace all uses with NEW_VALUE.
411 : : void
412 : 37613695 : function_info::replace_phi (phi_info *phi, set_info *new_value)
413 : : {
414 : 40149478 : auto update_use = [&](use_info *use)
415 : : {
416 : 2535783 : remove_use (use);
417 : 2535783 : use->set_def (new_value);
418 : 2535783 : add_use (use);
419 : 40149478 : };
420 : :
421 : 37613695 : if (new_value)
422 : 75224202 : for (use_info *use : phi->nondebug_insn_uses ())
423 : 30974291 : if (!use->is_live_out_use ())
424 : : {
425 : : // We need to keep the phi around for its local uses.
426 : : // Turn it into a degenerate phi, if it isn't already.
427 : 30974289 : use_info *use = phi->input_use (0);
428 : 30974289 : if (use->def () != new_value)
429 : 478541 : update_use (use);
430 : :
431 : 30974289 : if (phi->is_degenerate ())
432 : 30974289 : return;
433 : :
434 : 765218 : phi->make_degenerate (use);
435 : :
436 : : // Redirect all phi users to NEW_VALUE.
437 : 33290703 : while (use_info *phi_use = phi->last_phi_use ())
438 : 1551196 : update_use (phi_use);
439 : :
440 : : return;
441 : : }
442 : :
443 : : // Replace the uses. We can discard uses that only existed for the
444 : : // sake of marking live-out values, since the resource is now transparent
445 : : // in the phi's EBB.
446 : 7148236 : while (use_info *use = phi->last_use ())
447 : 508830 : if (use->is_live_out_use ())
448 : 2784 : remove_use (use);
449 : : else
450 : 506046 : update_use (use);
451 : :
452 : 6639406 : delete_phi (phi);
453 : : }
454 : :
455 : : // Create and return a phi node for EBB. RESOURCE is the resource that
456 : : // the phi node sets (and thus that all the inputs set too). NUM_INPUTS
457 : : // is the number of inputs, which is 1 for a degenerate phi. INPUTS[I]
458 : : // is a set_info that gives the value of input I, or null if the value
459 : : // is either unknown or uninitialized. If NUM_INPUTS > 1, this array
460 : : // is allocated on the main obstack and can be reused for the use array.
461 : : //
462 : : // Add the created phi node to its basic block and enter it into the
463 : : // function's hash table.
464 : : phi_info *
465 : 57229638 : function_info::create_phi (ebb_info *ebb, resource_info resource,
466 : : access_info **inputs, unsigned int num_inputs)
467 : : {
468 : 57229638 : phi_info *phi = m_free_phis;
469 : 57229638 : if (phi)
470 : : {
471 : 3776217 : m_free_phis = phi->next_phi ();
472 : 3776217 : *phi = phi_info (ebb->phi_insn (), resource, phi->uid ());
473 : : }
474 : : else
475 : : {
476 : 53453421 : phi = allocate<phi_info> (ebb->phi_insn (), resource, m_next_phi_uid);
477 : 53453421 : m_next_phi_uid += 1;
478 : : }
479 : :
480 : : // Convert the array of set_infos into an array of use_infos. Also work
481 : : // out what mode the phi should have.
482 : 57229638 : machine_mode new_mode = resource.mode;
483 : 149870971 : for (unsigned int i = 0; i < num_inputs; ++i)
484 : : {
485 : 92641333 : auto *input = safe_as_a<set_info *> (inputs[i]);
486 : 92641333 : auto *use = allocate<use_info> (phi, resource, input);
487 : 92641333 : add_use (use);
488 : 92641333 : inputs[i] = use;
489 : 92641333 : if (input)
490 : 57425639 : new_mode = combine_modes (new_mode, input->mode ());
491 : : }
492 : :
493 : 57229638 : phi->set_inputs (use_array (inputs, num_inputs));
494 : 57229638 : phi->set_mode (new_mode);
495 : :
496 : 57229638 : append_phi (ebb, phi);
497 : :
498 : 57229638 : return phi;
499 : : }
500 : :
501 : : // Create and return a degenerate phi for EBB whose input comes from DEF.
502 : : // This is used in cases where DEF is known to be available on entry to
503 : : // EBB but was not previously used within it. If DEF is for a register,
504 : : // there are two cases:
505 : : //
506 : : // (1) DEF was already live on entry to EBB but was previously transparent
507 : : // within it.
508 : : //
509 : : // (2) DEF was not previously live on entry to EBB and is being made live
510 : : // by this update.
511 : : //
512 : : // At the moment, this function only handles the case in which EBB has a
513 : : // single predecessor block and DEF is defined in that block's EBB.
514 : : phi_info *
515 : 13171 : function_info::create_degenerate_phi (ebb_info *ebb, set_info *def)
516 : : {
517 : : // Allow the function to be called twice in succession for the same def.
518 : 13171 : def_lookup dl = find_def (def->resource (), ebb->phi_insn ());
519 : 13171 : if (set_info *set = dl.matching_set ())
520 : 642 : return as_a<phi_info *> (set);
521 : :
522 : 12529 : access_info *input = def;
523 : 12529 : phi_info *phi = create_phi (ebb, def->resource (), &input, 1);
524 : 12529 : if (def->is_reg ())
525 : : {
526 : 9366 : unsigned int regno = def->regno ();
527 : :
528 : : // Find the single predecessor mentioned above.
529 : 9366 : basic_block pred_cfg_bb = single_pred (ebb->first_bb ()->cfg_bb ());
530 : 9366 : bb_info *pred_bb = this->bb (pred_cfg_bb);
531 : :
532 : 18732 : if (!bitmap_set_bit (DF_LR_IN (ebb->first_bb ()->cfg_bb ()), regno))
533 : : {
534 : : // The register was not previously live on entry to EBB and
535 : : // might not have been live on exit from PRED_BB either.
536 : 4248 : if (bitmap_set_bit (DF_LR_OUT (pred_cfg_bb), regno))
537 : 0 : add_live_out_use (pred_bb, def);
538 : : }
539 : : else
540 : : {
541 : : // The register was previously live in to EBB. Add live-out uses
542 : : // at the appropriate points.
543 : 7242 : insn_info *next_insn = nullptr;
544 : 7242 : if (def_info *next_def = phi->next_def ())
545 : 4980 : next_insn = next_def->insn ();
546 : 7242 : for (bb_info *bb : ebb->bbs ())
547 : : {
548 : 4980 : if ((next_insn && *next_insn <= *bb->end_insn ())
549 : 15646 : || !bitmap_bit_p (DF_LR_OUT (bb->cfg_bb ()), regno))
550 : : break;
551 : 0 : add_live_out_use (bb, def);
552 : : }
553 : : }
554 : : }
555 : : return phi;
556 : : }
557 : :
558 : : // Create a bb_info for CFG_BB, given that no such structure currently exists.
559 : : bb_info *
560 : 46128912 : function_info::create_bb_info (basic_block cfg_bb)
561 : : {
562 : 46128912 : bb_info *bb = allocate<bb_info> (cfg_bb);
563 : 46128912 : gcc_checking_assert (!m_bbs[cfg_bb->index]);
564 : 46128912 : m_bbs[cfg_bb->index] = bb;
565 : 46128912 : return bb;
566 : : }
567 : :
568 : : // Add BB to the end of the list of blocks.
569 : : void
570 : 46128912 : function_info::append_bb (bb_info *bb)
571 : : {
572 : 46128912 : if (m_last_bb)
573 : 42281250 : m_last_bb->set_next_bb (bb);
574 : : else
575 : 3847662 : m_first_bb = bb;
576 : 46128912 : bb->set_prev_bb (m_last_bb);
577 : 46128912 : m_last_bb = bb;
578 : 46128912 : }
579 : :
580 : : // Calculate BI.potential_phi_regs and BI.potential_phi_regs_for_debug.
581 : : void
582 : 3847662 : function_info::calculate_potential_phi_regs (build_info &bi)
583 : : {
584 : 3847662 : auto *lr_info = DF_LR_BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
585 : 3847662 : bool is_debug = MAY_HAVE_DEBUG_INSNS;
586 : 541473805 : for (unsigned int regno = 0; regno < m_num_regs; ++regno)
587 : 537626143 : if (regno >= DF_REG_SIZE (DF)
588 : : // Exclude registers that have a single definition that dominates
589 : : // all uses. If the definition does not dominate all uses,
590 : : // the register will be exposed upwards to the entry block but
591 : : // will not be defined by the entry block.
592 : 537608419 : || DF_REG_DEF_COUNT (regno) > 1
593 : 921349367 : || (!bitmap_bit_p (&lr_info->def, regno)
594 : 353144243 : && bitmap_bit_p (&lr_info->out, regno)))
595 : : {
596 : 154136744 : bitmap_set_bit (bi.potential_phi_regs, regno);
597 : 154136744 : if (is_debug)
598 : 92055761 : bitmap_set_bit (bi.potential_phi_regs_for_debug, regno);
599 : : }
600 : 3847662 : }
601 : :
602 : : // Called while building SSA form using BI. Decide where phi nodes
603 : : // should be placed for each register and initialize BI.bb_phis accordingly.
604 : : void
605 : 3847662 : function_info::place_phis (build_info &bi)
606 : : {
607 : 3847662 : unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
608 : :
609 : : // Calculate dominance frontiers.
610 : 3847662 : auto_vec<bitmap_head> frontiers;
611 : 3847662 : frontiers.safe_grow_cleared (num_bb_indices);
612 : 51269040 : for (unsigned int i = 0; i < num_bb_indices; ++i)
613 : 47421378 : bitmap_initialize (&frontiers[i], &bitmap_default_obstack);
614 : 7695324 : compute_dominance_frontiers (frontiers.address ());
615 : :
616 : : // The normal dominance information doesn't calculate dominators for
617 : : // the exit block, so we don't get dominance frontiers for them either.
618 : : // Calculate them by hand.
619 : 15508422 : for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
620 : : {
621 : 3965436 : basic_block bb = e->src;
622 : 4798417 : while (bb != bi.exit_block_dominator)
623 : : {
624 : 832981 : bitmap_set_bit (&frontiers[bb->index], EXIT_BLOCK);
625 : 832981 : bb = get_immediate_dominator (CDI_DOMINATORS, bb);
626 : : }
627 : : }
628 : :
629 : : // In extreme cases, the number of live-in registers can be much
630 : : // greater than the number of phi nodes needed in a block (see PR98863).
631 : : // Try to reduce the number of operations involving live-in sets by using
632 : : // PENDING as a staging area: registers in PENDING need phi nodes if
633 : : // they are live on entry to the corresponding block, but do not need
634 : : // phi nodes otherwise.
635 : 3847662 : auto_vec<bitmap_head> unfiltered;
636 : 3847662 : unfiltered.safe_grow_cleared (num_bb_indices);
637 : 51269040 : for (unsigned int i = 0; i < num_bb_indices; ++i)
638 : 47421378 : bitmap_initialize (&unfiltered[i], &bitmap_default_obstack);
639 : :
640 : : // If block B1 defines R and if B2 is in the dominance frontier of B1,
641 : : // queue a possible phi node for R in B2.
642 : 3847662 : auto_bitmap worklist;
643 : 51269040 : for (unsigned int b1 = 0; b1 < num_bb_indices; ++b1)
644 : : {
645 : : // Only access DF information for blocks that are known to exist.
646 : 47421378 : if (bitmap_empty_p (&frontiers[b1]))
647 : 18507578 : continue;
648 : :
649 : : // Defs in B1 that are possibly in LR_IN in the dominance frontier
650 : : // blocks.
651 : 28913800 : auto_bitmap b1_def;
652 : 57827600 : bitmap_and (b1_def, &DF_LR_BB_INFO (BASIC_BLOCK_FOR_FN (m_fn, b1))->def,
653 : 57827600 : DF_LR_OUT (BASIC_BLOCK_FOR_FN (m_fn, b1)));
654 : :
655 : 28913800 : bitmap_iterator bmi;
656 : 28913800 : unsigned int b2;
657 : 74187139 : EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
658 : 45273339 : if (bitmap_ior_into (&unfiltered[b2], b1_def)
659 : 45273339 : && !bitmap_empty_p (&frontiers[b2]))
660 : : // Propagate the (potential) new phi node definitions in B2.
661 : 12771290 : bitmap_set_bit (worklist, b2);
662 : 28913800 : }
663 : :
664 : 10107635 : while (!bitmap_empty_p (worklist))
665 : : {
666 : 6259973 : unsigned int b1 = bitmap_first_set_bit (worklist);
667 : 6259973 : bitmap_clear_bit (worklist, b1);
668 : :
669 : : // Restrict the phi nodes to registers that are live on entry to
670 : : // the block.
671 : 6259973 : bitmap b1_in = DF_LR_IN (BASIC_BLOCK_FOR_FN (m_fn, b1));
672 : 6259973 : bitmap b1_phis = &bi.bb_phis[b1].regs;
673 : 6259973 : if (!bitmap_ior_and_into (b1_phis, &unfiltered[b1], b1_in))
674 : 1001999 : continue;
675 : :
676 : : // If block B1 has a phi node for R and if B2 is in the dominance
677 : : // frontier of B1, queue a possible phi node for R in B2.
678 : 5257974 : bitmap_iterator bmi;
679 : 5257974 : unsigned int b2;
680 : 14790711 : EXECUTE_IF_SET_IN_BITMAP (&frontiers[b1], 0, b2, bmi)
681 : 9532737 : if (bitmap_ior_into (&unfiltered[b2], b1_phis)
682 : 9532737 : && !bitmap_empty_p (&frontiers[b2]))
683 : 1435898 : bitmap_set_bit (worklist, b2);
684 : : }
685 : :
686 : 3847662 : basic_block cfg_bb;
687 : 49976574 : FOR_ALL_BB_FN (cfg_bb, m_fn)
688 : : {
689 : : // Calculate the set of phi nodes for blocks that don't have any
690 : : // dominance frontiers. We only need to do this once per block.
691 : 46128912 : unsigned int i = cfg_bb->index;
692 : 46128912 : bb_phi_info &phis = bi.bb_phis[i];
693 : 46128912 : if (bitmap_empty_p (&frontiers[i]))
694 : 34430224 : bitmap_and (&phis.regs, &unfiltered[i], DF_LR_IN (cfg_bb));
695 : :
696 : : // Create an array that contains all phi inputs for this block.
697 : : // See the comment above the member variables for more information.
698 : 46128912 : phis.num_phis = bitmap_count_bits (&phis.regs);
699 : 46128912 : phis.num_preds = EDGE_COUNT (cfg_bb->preds);
700 : 46128912 : unsigned int num_inputs = phis.num_phis * phis.num_preds;
701 : 46128912 : if (num_inputs != 0)
702 : : {
703 : 6257763 : phis.inputs = XOBNEWVEC (&m_temp_obstack, set_info *, num_inputs);
704 : 6257763 : memset (phis.inputs, 0, num_inputs * sizeof (phis.inputs[0]));
705 : : }
706 : : }
707 : :
708 : : // Free the temporary bitmaps.
709 : 51269040 : for (unsigned int i = 0; i < num_bb_indices; ++i)
710 : : {
711 : 47421378 : bitmap_release (&frontiers[i]);
712 : 47421378 : bitmap_release (&unfiltered[i]);
713 : : }
714 : 3847662 : }
715 : :
716 : : // Called while building SSA form using BI, with BI.current_bb being
717 : : // the entry block.
718 : : //
719 : : // Create the entry block instructions and their definitions. The only
720 : : // useful instruction is the end instruction, which carries definitions
721 : : // for the values that are live on entry to the function. However, it
722 : : // seems simpler to create a head instruction too, rather than force all
723 : : // users of the block information to treat the entry block as a special case.
724 : : void
725 : 3847662 : function_info::add_entry_block_defs (build_info &bi)
726 : : {
727 : 3847662 : bb_info *bb = bi.current_bb;
728 : 3847662 : basic_block cfg_bb = bi.current_bb->cfg_bb ();
729 : 3847662 : auto *lr_info = DF_LR_BB_INFO (cfg_bb);
730 : :
731 : 3847662 : bb->set_head_insn (append_artificial_insn (bb));
732 : 3847662 : insn_info *insn = append_artificial_insn (bb);
733 : 3847662 : bb->set_end_insn (insn);
734 : :
735 : 3847662 : start_insn_accesses ();
736 : :
737 : : // Using LR to derive the liveness information means that we create an
738 : : // entry block definition for upwards exposed registers. These registers
739 : : // are sometimes genuinely uninitialized. However, some targets also
740 : : // create a pseudo PIC base register and only initialize it later.
741 : : // Handling that case correctly seems more important than optimizing
742 : : // uninitialized uses.
743 : 3847662 : unsigned int regno;
744 : 3847662 : bitmap_iterator in_bi;
745 : 22303190 : EXECUTE_IF_SET_IN_BITMAP (&lr_info->out, 0, regno, in_bi)
746 : : {
747 : 18455528 : auto *set = allocate<set_info> (insn, full_register (regno));
748 : 18455528 : append_def (set);
749 : 18455528 : m_temp_defs.safe_push (set);
750 : 18455528 : bi.record_reg_def (set);
751 : : }
752 : :
753 : : // Create a definition that reflects the state of memory on entry to
754 : : // the function.
755 : 3847662 : auto *set = allocate<set_info> (insn, memory);
756 : 3847662 : append_def (set);
757 : 3847662 : m_temp_defs.safe_push (set);
758 : 3847662 : bi.record_mem_def (set);
759 : :
760 : 3847662 : finish_insn_accesses (insn);
761 : 3847662 : }
762 : :
763 : : // Lazily calculate the value of BI.ebb_live_in_for_debug for BI.current_ebb.
764 : : void
765 : 973691 : function_info::calculate_ebb_live_in_for_debug (build_info &bi)
766 : : {
767 : 973691 : gcc_checking_assert (bitmap_empty_p (bi.tmp_ebb_live_in_for_debug));
768 : 973691 : bi.ebb_live_in_for_debug = bi.tmp_ebb_live_in_for_debug;
769 : 1947382 : bitmap_and (bi.ebb_live_in_for_debug, bi.potential_phi_regs_for_debug,
770 : 1947382 : DF_LR_IN (bi.current_ebb->first_bb ()->cfg_bb ()));
771 : 973691 : bitmap_tree_view (bi.ebb_live_in_for_debug);
772 : 973691 : }
773 : :
774 : : // Called while building SSA form using BI. Create phi nodes for the
775 : : // current EBB.
776 : : void
777 : 24752121 : function_info::add_phi_nodes (build_info &bi)
778 : : {
779 : 24752121 : ebb_info *ebb = bi.current_ebb;
780 : 24752121 : basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
781 : :
782 : : // Create the register phis for this EBB.
783 : 24752121 : bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
784 : 24752121 : unsigned int num_preds = phis.num_preds;
785 : 24752121 : unsigned int regno;
786 : 24752121 : bitmap_iterator in_bi;
787 : 36630581 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, in_bi)
788 : : {
789 : 11878460 : gcc_checking_assert (bitmap_bit_p (bi.potential_phi_regs, regno));
790 : :
791 : : // Create an array of phi inputs, to be filled in later.
792 : 11878460 : auto *inputs = XOBNEWVEC (&m_obstack, access_info *, num_preds);
793 : 11878460 : memset (inputs, 0, sizeof (access_info *) * num_preds);
794 : :
795 : : // Later code works out the correct mode of the phi. Use BLKmode
796 : : // as a placeholder for now.
797 : 11878460 : phi_info *phi = create_phi (ebb, { E_BLKmode, regno },
798 : : inputs, num_preds);
799 : 11878460 : bi.record_reg_def (phi);
800 : : }
801 : :
802 : 24752121 : bitmap_copy (bi.ebb_def_regs, &phis.regs);
803 : :
804 : : // Collect the live-in memory definitions and record whether they're
805 : : // all the same.
806 : 24752121 : m_temp_defs.reserve (num_preds);
807 : 24752121 : set_info *mem_value = nullptr;
808 : 24752121 : bool mem_phi_is_degenerate = true;
809 : 24752121 : edge e;
810 : 24752121 : edge_iterator ei;
811 : 67365224 : FOR_EACH_EDGE (e, ei, cfg_bb->preds)
812 : : {
813 : 42613103 : bb_info *pred_bb = this->bb (e->src);
814 : 42613103 : if (pred_bb && pred_bb->head_insn ())
815 : : {
816 : 40192350 : mem_value = bi.bb_mem_live_out[pred_bb->index ()];
817 : 40192350 : m_temp_defs.quick_push (mem_value);
818 : 40192350 : if (mem_value != m_temp_defs[0])
819 : 12963003 : mem_phi_is_degenerate = false;
820 : : }
821 : : else
822 : : {
823 : 2420753 : m_temp_defs.quick_push (nullptr);
824 : 2420753 : mem_phi_is_degenerate = false;
825 : : }
826 : : }
827 : :
828 : : // Create a phi for memory, on the assumption that something in the
829 : : // EBB will need it.
830 : 24752121 : if (mem_phi_is_degenerate)
831 : : {
832 : 16859532 : access_info *input[] = { mem_value };
833 : 16859532 : mem_value = create_phi (ebb, memory, input, 1);
834 : : }
835 : : else
836 : : {
837 : 7892589 : obstack_grow (&m_obstack, m_temp_defs.address (),
838 : : num_preds * sizeof (access_info *));
839 : 7892589 : auto *inputs = static_cast<access_info **> (obstack_finish (&m_obstack));
840 : 7892589 : mem_value = create_phi (ebb, memory, inputs, num_preds);
841 : : }
842 : 24752121 : bi.record_mem_def (mem_value);
843 : 24752121 : m_temp_defs.truncate (0);
844 : 24752121 : }
845 : :
846 : : // Called while building SSA form using BI.
847 : : //
848 : : // If FLAGS is DF_REF_AT_TOP, create the head insn for BI.current_bb
849 : : // and populate its uses and definitions. If FLAGS is 0, do the same
850 : : // for the end insn.
851 : : void
852 : 84373500 : function_info::add_artificial_accesses (build_info &bi, df_ref_flags flags)
853 : : {
854 : 84373500 : bb_info *bb = bi.current_bb;
855 : 84373500 : basic_block cfg_bb = bb->cfg_bb ();
856 : 84373500 : auto *lr_info = DF_LR_BB_INFO (cfg_bb);
857 : 84373500 : df_ref ref;
858 : :
859 : 84373500 : insn_info *insn;
860 : 84373500 : if (flags == DF_REF_AT_TOP)
861 : : {
862 : 42186750 : if (cfg_bb->index == EXIT_BLOCK)
863 : 3753162 : insn = append_artificial_insn (bb);
864 : : else
865 : 38433588 : insn = append_artificial_insn (bb, bb_note (cfg_bb));
866 : 42186750 : bb->set_head_insn (insn);
867 : : }
868 : : else
869 : : {
870 : 42186750 : insn = append_artificial_insn (bb);
871 : 42186750 : bb->set_end_insn (insn);
872 : : }
873 : :
874 : 84373500 : start_insn_accesses ();
875 : :
876 : 84373500 : HARD_REG_SET added_regs = {};
877 : 445104716 : FOR_EACH_ARTIFICIAL_USE (ref, cfg_bb->index)
878 : 276357716 : if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
879 : : {
880 : 138178858 : unsigned int regno = DF_REF_REGNO (ref);
881 : 138178858 : machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
882 : 138178858 : if (HARD_REGISTER_NUM_P (regno))
883 : 138178858 : SET_HARD_REG_BIT (added_regs, regno);
884 : :
885 : : // A definition must be available.
886 : 138178858 : gcc_checking_assert (bitmap_bit_p (&lr_info->in, regno)
887 : : || (flags != DF_REF_AT_TOP
888 : : && bitmap_bit_p (&lr_info->def, regno)));
889 : 138178858 : m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, regno }));
890 : : }
891 : :
892 : : // Ensure that global registers and memory are live at the end of any
893 : : // block that has no successors, such as the exit block and non-local gotos.
894 : : // Global registers have to be singled out because they are not part of
895 : : // the DF artifical use list (they are instead treated as used within
896 : : // every block).
897 : 84373500 : if (flags == 0 && EDGE_COUNT (cfg_bb->succs) == 0)
898 : : {
899 : 531412509 : for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
900 : 525698396 : if (global_regs[i] && !TEST_HARD_REG_BIT (added_regs, i))
901 : : {
902 : 30 : auto mode = reg_raw_mode[i];
903 : 30 : m_temp_uses.safe_push (create_reg_use (bi, insn, { mode, i }));
904 : : }
905 : :
906 : 5714113 : auto *use = allocate<use_info> (insn, memory, bi.current_mem_value ());
907 : 5714113 : add_use (use);
908 : 5714113 : m_temp_uses.safe_push (use);
909 : : }
910 : :
911 : 171622160 : FOR_EACH_ARTIFICIAL_DEF (ref, cfg_bb->index)
912 : 2875160 : if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags)
913 : : {
914 : 1437580 : unsigned int regno = DF_REF_REGNO (ref);
915 : 1437580 : machine_mode mode = GET_MODE (DF_REF_REAL_REG (ref));
916 : 1437580 : resource_info resource { mode, regno };
917 : :
918 : : // We rely on the def set being correct.
919 : 1437580 : gcc_checking_assert (bitmap_bit_p (&lr_info->def, regno));
920 : :
921 : : // If the value isn't used later in the block and isn't live
922 : : // on exit, we could instead represent the definition as a
923 : : // clobber_info. However, that case should be relatively
924 : : // rare and set_info is any case more compact than clobber_info.
925 : 1437580 : set_info *def = allocate<set_info> (insn, resource);
926 : 1437580 : append_def (def);
927 : 1437580 : m_temp_defs.safe_push (def);
928 : 1437580 : bi.record_reg_def (def);
929 : : }
930 : :
931 : : // Model the effect of a memory clobber on an incoming edge by adding
932 : : // a fake definition of memory at the start of the block. We don't need
933 : : // to add a use of the phi node because memory is implicitly always live.
934 : 84373500 : if (flags == DF_REF_AT_TOP && has_abnormal_call_or_eh_pred_edge_p (cfg_bb))
935 : : {
936 : 720708 : set_info *def = allocate<set_info> (insn, memory);
937 : 720708 : append_def (def);
938 : 720708 : m_temp_defs.safe_push (def);
939 : 720708 : bi.record_mem_def (def);
940 : : }
941 : :
942 : 84373500 : finish_insn_accesses (insn);
943 : 84373500 : }
944 : :
945 : : // Called while building SSA form using BI. Create insn_infos for all
946 : : // relevant instructions in BI.current_bb.
947 : : void
948 : 38433588 : function_info::add_block_contents (build_info &bi)
949 : : {
950 : 38433588 : basic_block cfg_bb = bi.current_bb->cfg_bb ();
951 : 38433588 : rtx_insn *insn;
952 : 486196794 : FOR_BB_INSNS (cfg_bb, insn)
953 : 447763206 : if (INSN_P (insn))
954 : 378300175 : add_insn_to_block (bi, insn);
955 : 38433588 : }
956 : :
957 : : // Called while building SSA form using BI. Record live-out register values
958 : : // in the phi inputs of successor blocks and create live-out uses where
959 : : // appropriate. Record the live-out memory value in BI.bb_mem_live_out.
960 : : void
961 : 46034412 : function_info::record_block_live_out (build_info &bi)
962 : : {
963 : 46034412 : bb_info *bb = bi.current_bb;
964 : 46034412 : ebb_info *ebb = bi.current_ebb;
965 : 46034412 : basic_block cfg_bb = bb->cfg_bb ();
966 : :
967 : : // Record the live-out register values in the phi inputs of
968 : : // successor blocks.
969 : 46034412 : edge e;
970 : 46034412 : edge_iterator ei;
971 : 106082144 : FOR_EACH_EDGE (e, ei, cfg_bb->succs)
972 : : {
973 : 60047732 : bb_phi_info &phis = bi.bb_phis[e->dest->index];
974 : 60047732 : unsigned int input_i = e->dest_idx * phis.num_phis;
975 : 60047732 : unsigned int regno;
976 : 60047732 : bitmap_iterator out_bi;
977 : 92842673 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, out_bi)
978 : : {
979 : 65589882 : phis.inputs[input_i]
980 : 32794941 : = live_out_value (bb, bi.current_reg_value (regno));
981 : 32794941 : input_i += 1;
982 : : }
983 : : }
984 : :
985 : : // Add the set of registers that were defined in this BB to the set
986 : : // of potentially-live registers defined in the EBB.
987 : 92068824 : bitmap_ior_into (bi.ebb_def_regs, &DF_LR_BB_INFO (cfg_bb)->def);
988 : :
989 : : // Iterate through the registers in LIVE_OUT and see whether we need
990 : : // to add a live-out use for them.
991 : 91913586 : auto record_live_out_regs = [&](bitmap live_out)
992 : : {
993 : 45879174 : unsigned int regno;
994 : 45879174 : bitmap_iterator out_bi;
995 : 117019785 : EXECUTE_IF_AND_IN_BITMAP (bi.ebb_def_regs, live_out, 0, regno, out_bi)
996 : : {
997 : 71140611 : set_info *value = live_out_value (bb, bi.current_reg_value (regno));
998 : 71140611 : if (value && value->ebb () == ebb)
999 : 71092470 : add_live_out_use (bb, value);
1000 : : }
1001 : 91913586 : };
1002 : :
1003 : 46034412 : if (bb == ebb->last_bb ())
1004 : : // All live-out registers might need live-out uses.
1005 : 57199566 : record_live_out_regs (DF_LR_OUT (cfg_bb));
1006 : : else
1007 : : // Registers might need live-out uses if they are live on entry
1008 : : // to a successor block in a different EBB.
1009 : 52148649 : FOR_EACH_EDGE (e, ei, cfg_bb->succs)
1010 : : {
1011 : 34714020 : bb_info *dest_bb = this->bb (e->dest);
1012 : 34714020 : if (dest_bb->ebb () != ebb || dest_bb == ebb->first_bb ())
1013 : 34558782 : record_live_out_regs (DF_LR_IN (e->dest));
1014 : : }
1015 : :
1016 : : // Record the live-out memory value.
1017 : 46034412 : bi.bb_mem_live_out[cfg_bb->index]
1018 : 46034412 : = live_out_value (bb, bi.current_mem_value ());
1019 : 46034412 : }
1020 : :
1021 : : // Add BB and its contents to the SSA information.
1022 : : void
1023 : 46128912 : function_info::start_block (build_info &bi, bb_info *bb)
1024 : : {
1025 : 46128912 : ebb_info *ebb = bb->ebb ();
1026 : :
1027 : : // We (need to) add all blocks from one EBB before moving on to the next.
1028 : 46128912 : bi.current_bb = bb;
1029 : 46128912 : if (bb == ebb->first_bb ())
1030 : 28694283 : bi.current_ebb = ebb;
1031 : : else
1032 : 17434629 : gcc_assert (bi.current_ebb == ebb);
1033 : :
1034 : : // Record the start of this block's definitions in the definitions stack.
1035 : 88410162 : bi.old_def_stack_limit.safe_push (bi.def_stack.length ());
1036 : :
1037 : : // Add the block itself.
1038 : 46128912 : append_bb (bb);
1039 : :
1040 : : // If the block starts an EBB, create the phi insn. This insn should exist
1041 : : // for all EBBs, even if they don't (yet) need phis.
1042 : 46128912 : if (bb == ebb->first_bb ())
1043 : 28694283 : ebb->set_phi_insn (append_artificial_insn (bb));
1044 : :
1045 : 46128912 : if (bb->index () == ENTRY_BLOCK)
1046 : : {
1047 : 3847662 : add_entry_block_defs (bi);
1048 : 3847662 : record_block_live_out (bi);
1049 : 3847662 : return;
1050 : : }
1051 : :
1052 : 42281250 : if (EDGE_COUNT (bb->cfg_bb ()->preds) == 0)
1053 : : {
1054 : : // Leave unreachable blocks empty, since there is no useful
1055 : : // liveness information for them, and anything they do will
1056 : : // be wasted work. In a cleaned-up cfg, the only unreachable
1057 : : // block we should see is the exit block of a noreturn function.
1058 : 94500 : bb->set_head_insn (append_artificial_insn (bb));
1059 : 94500 : bb->set_end_insn (append_artificial_insn (bb));
1060 : 94500 : return;
1061 : : }
1062 : :
1063 : : // If the block starts an EBB, create the phi nodes.
1064 : 42186750 : if (bb == ebb->first_bb ())
1065 : 24752121 : add_phi_nodes (bi);
1066 : :
1067 : : // Process the contents of the block.
1068 : 42186750 : add_artificial_accesses (bi, DF_REF_AT_TOP);
1069 : 42186750 : if (bb->index () != EXIT_BLOCK)
1070 : 38433588 : add_block_contents (bi);
1071 : 42186750 : add_artificial_accesses (bi, df_ref_flags ());
1072 : 42186750 : record_block_live_out (bi);
1073 : :
1074 : : // If we needed to calculate a live-in set for debug purposes,
1075 : : // reset it to null at the end of the EBB. Convert the underlying
1076 : : // bitmap to an empty list view, ready for the next calculation.
1077 : 42186750 : if (bi.ebb_live_in_for_debug && bb == ebb->last_bb ())
1078 : : {
1079 : 973691 : bitmap_clear (bi.tmp_ebb_live_in_for_debug);
1080 : 973691 : bitmap_list_view (bi.tmp_ebb_live_in_for_debug);
1081 : 973691 : bi.ebb_live_in_for_debug = nullptr;
1082 : : }
1083 : : }
1084 : :
1085 : : // Finish adding BB and the blocks that it dominates to the SSA information.
1086 : : void
1087 : 46128912 : function_info::end_block (build_info &bi, bb_info *bb)
1088 : : {
1089 : : // Restore the register last_access information to the state it was
1090 : : // in before we started processing BB.
1091 : 46128912 : unsigned int old_limit = bi.old_def_stack_limit.pop ();
1092 : 269268881 : while (bi.def_stack.length () > old_limit)
1093 : : {
1094 : : // We pushed a definition in BB if it was the first dominating
1095 : : // definition (and so the previous entry was null). In other
1096 : : // cases we pushed the previous dominating definition.
1097 : 223139969 : def_info *def = bi.def_stack.pop ();
1098 : 223139969 : unsigned int regno = def->regno ();
1099 : 223139969 : if (def->bb () == bb)
1100 : 122749135 : def = nullptr;
1101 : 223139969 : bi.last_access[regno + 1] = def;
1102 : : }
1103 : 46128912 : }
1104 : :
1105 : : // Finish setting up the phi nodes for each block, now that we've added
1106 : : // the contents of all blocks.
1107 : : void
1108 : 3847662 : function_info::populate_phi_inputs (build_info &bi)
1109 : : {
1110 : 3847662 : auto_vec<phi_info *, 32> sorted_phis;
1111 : 61236228 : for (ebb_info *ebb : ebbs ())
1112 : : {
1113 : 28694283 : if (!ebb->first_phi ())
1114 : 6032149 : continue;
1115 : :
1116 : : // Get a sorted array of EBB's phi nodes.
1117 : 22662134 : basic_block cfg_bb = ebb->first_bb ()->cfg_bb ();
1118 : 22662134 : bb_phi_info &phis = bi.bb_phis[cfg_bb->index];
1119 : 22662134 : sorted_phis.truncate (0);
1120 : 76108520 : for (phi_info *phi : ebb->phis ())
1121 : 53446386 : sorted_phis.safe_push (phi);
1122 : 22662134 : std::sort (sorted_phis.address (),
1123 : 45324268 : sorted_phis.address () + sorted_phis.length (),
1124 : : compare_access_infos);
1125 : :
1126 : : // Set the inputs of the non-degenerate register phis. All inputs
1127 : : // for one edge come before all inputs for the next edge.
1128 : 22662134 : set_info **inputs = phis.inputs;
1129 : 22662134 : unsigned int phi_i = 0;
1130 : 22662134 : bitmap_iterator bmi;
1131 : 22662134 : unsigned int regno;
1132 : 34540594 : EXECUTE_IF_SET_IN_BITMAP (&phis.regs, 0, regno, bmi)
1133 : : {
1134 : : // Skip intervening degenerate phis.
1135 : 14449326 : while (sorted_phis[phi_i]->regno () < regno)
1136 : 2570866 : phi_i += 1;
1137 : 11878460 : phi_info *phi = sorted_phis[phi_i];
1138 : 11878460 : gcc_assert (phi->regno () == regno);
1139 : 44673401 : for (unsigned int input_i = 0; input_i < phis.num_preds; ++input_i)
1140 : 32794941 : if (set_info *input = inputs[input_i * phis.num_phis])
1141 : : {
1142 : 32758756 : use_info *use = phi->input_use (input_i);
1143 : 32758756 : gcc_assert (!use->def ());
1144 : 32758756 : use->set_def (input);
1145 : 32758756 : add_use (use);
1146 : : }
1147 : 11878460 : phi_i += 1;
1148 : 11878460 : inputs += 1;
1149 : : }
1150 : :
1151 : : // Fill in the backedge inputs to any memory phi.
1152 : 22662134 : phi_info *mem_phi = sorted_phis.last ();
1153 : 22662134 : if (mem_phi->is_mem () && !mem_phi->is_degenerate ())
1154 : : {
1155 : 7892589 : edge e;
1156 : 7892589 : edge_iterator ei;
1157 : 30280392 : FOR_EACH_EDGE (e, ei, cfg_bb->preds)
1158 : : {
1159 : 22387803 : use_info *use = mem_phi->input_use (e->dest_idx);
1160 : 22387803 : if (!use->def ())
1161 : : {
1162 : 2420753 : use->set_def (bi.bb_mem_live_out[e->src->index]);
1163 : 2420753 : add_use (use);
1164 : : }
1165 : : }
1166 : : }
1167 : : }
1168 : 3847662 : }
1169 : :
1170 : : // Return true if it would be better to continue an EBB across NEW_EDGE
1171 : : // rather than across OLD_EDGE, given that both edges are viable candidates.
1172 : : // This is not a total ordering.
1173 : : static bool
1174 : 4667571 : better_ebb_edge_p (edge new_edge, edge old_edge)
1175 : : {
1176 : : // Prefer the likeliest edge.
1177 : 4667571 : if (new_edge->probability.initialized_p ()
1178 : 4666607 : && old_edge->probability.initialized_p ()
1179 : 9334178 : && !(old_edge->probability == new_edge->probability))
1180 : 3881745 : return old_edge->probability < new_edge->probability;
1181 : :
1182 : : // If both edges are equally likely, prefer a fallthru edge.
1183 : 785826 : if (new_edge->flags & EDGE_FALLTHRU)
1184 : : return true;
1185 : : if (old_edge->flags & EDGE_FALLTHRU)
1186 : : return false;
1187 : :
1188 : : // Otherwise just stick with OLD_EDGE.
1189 : : return false;
1190 : : }
1191 : :
1192 : : // Pick and return the next basic block in an EBB that currently ends with BB.
1193 : : // Return null if the EBB must end with BB.
1194 : : static basic_block
1195 : 46128912 : choose_next_block_in_ebb (basic_block bb)
1196 : : {
1197 : : // Although there's nothing in principle wrong with having an EBB that
1198 : : // starts with the entry block and includes later blocks, there's not
1199 : : // really much point either. Keeping the entry block separate means
1200 : : // that uses of arguments consistently occur through phi nodes, rather
1201 : : // than the arguments sometimes appearing to come from an EBB-local
1202 : : // definition instead.
1203 : 46128912 : if (bb->index == ENTRY_BLOCK)
1204 : : return nullptr;
1205 : :
1206 : 42281250 : bool optimize_for_speed_p = optimize_bb_for_speed_p (bb);
1207 : 42281250 : edge best_edge = nullptr;
1208 : 42281250 : edge e;
1209 : 42281250 : edge_iterator ei;
1210 : 98481320 : FOR_EACH_EDGE (e, ei, bb->succs)
1211 : 56200070 : if (!(e->flags & EDGE_COMPLEX)
1212 : 52797126 : && e->dest->index != EXIT_BLOCK
1213 : 105497004 : && single_pred_p (e->dest)
1214 : 24007472 : && optimize_for_speed_p == optimize_bb_for_speed_p (e->dest)
1215 : 78302270 : && (!best_edge || better_ebb_edge_p (e, best_edge)))
1216 : : best_edge = e;
1217 : :
1218 : 42281250 : return best_edge ? best_edge->dest : nullptr;
1219 : : }
1220 : :
1221 : : // Partition the function into extended basic blocks. Create the
1222 : : // associated ebb_infos and bb_infos, but don't add the bb_infos
1223 : : // to the function list yet.
1224 : : void
1225 : 3847662 : function_info::create_ebbs (build_info &bi)
1226 : : {
1227 : : // Compute the starting reverse postorder. We tweak this later to try
1228 : : // to get better EBB assignments.
1229 : 3847662 : auto *postorder = new int[n_basic_blocks_for_fn (m_fn)];
1230 : 3847662 : unsigned int postorder_num
1231 : 3847662 : = pre_and_rev_post_order_compute (nullptr, postorder, true);
1232 : 3847662 : gcc_assert (int (postorder_num) <= n_basic_blocks_for_fn (m_fn));
1233 : :
1234 : : // Iterate over the blocks in reverse postorder. In cases where
1235 : : // multiple possible orders exist, prefer orders that chain blocks
1236 : : // together into EBBs. If multiple possible EBBs exist, try to pick
1237 : : // the ones that are most likely to be profitable.
1238 : 3847662 : auto_vec<bb_info *, 16> bbs;
1239 : 3847662 : unsigned int next_bb_index = 0;
1240 : 49976574 : for (unsigned int i = 0; i < postorder_num; ++i)
1241 : 46128912 : if (!m_bbs[postorder[i]])
1242 : : {
1243 : : // Choose and create the blocks that should form the next EBB.
1244 : 28694283 : basic_block cfg_bb = BASIC_BLOCK_FOR_FN (m_fn, postorder[i]);
1245 : 46128912 : do
1246 : : {
1247 : : // Record the chosen block order in a new RPO.
1248 : 46128912 : bi.bb_to_rpo[cfg_bb->index] = next_bb_index++;
1249 : 46128912 : bbs.safe_push (create_bb_info (cfg_bb));
1250 : 46128912 : cfg_bb = choose_next_block_in_ebb (cfg_bb);
1251 : : }
1252 : 46128912 : while (cfg_bb);
1253 : :
1254 : : // Create the EBB itself.
1255 : 28694283 : auto *ebb = allocate<ebb_info> (bbs[0], bbs.last ());
1256 : 132211761 : for (bb_info *bb : bbs)
1257 : 46128912 : bb->set_ebb (ebb);
1258 : 28694283 : bbs.truncate (0);
1259 : : }
1260 : :
1261 : 3847662 : delete[] postorder;
1262 : 3847662 : }
1263 : :
1264 : : // Partition the function's blocks into EBBs and build SSA form for all
1265 : : // EBBs in the function.
1266 : : void
1267 : 3847662 : function_info::process_all_blocks ()
1268 : : {
1269 : 3847662 : auto temps = temp_watermark ();
1270 : 3847662 : unsigned int num_bb_indices = last_basic_block_for_fn (m_fn);
1271 : :
1272 : 3847662 : build_info bi (m_num_regs, num_bb_indices);
1273 : :
1274 : : // ??? There is no dominance information associated with the exit block,
1275 : : // so work out its immediate dominator using predecessor blocks.
1276 : 15508422 : for (edge e : EXIT_BLOCK_PTR_FOR_FN (m_fn)->preds)
1277 : 3965436 : if (bi.exit_block_dominator)
1278 : 212274 : bi.exit_block_dominator
1279 : 212274 : = nearest_common_dominator (CDI_DOMINATORS,
1280 : : bi.exit_block_dominator, e->src);
1281 : : else
1282 : 3753162 : bi.exit_block_dominator = e->src;
1283 : :
1284 : 3847662 : calculate_potential_phi_regs (bi);
1285 : 3847662 : create_ebbs (bi);
1286 : 3847662 : place_phis (bi);
1287 : 3847662 : bb_walker (this, bi).walk (ENTRY_BLOCK_PTR_FOR_FN (m_fn));
1288 : 3847662 : populate_phi_inputs (bi);
1289 : :
1290 : 3847662 : if (flag_checking)
1291 : : {
1292 : : // The definition stack should be empty and all register definitions
1293 : : // should be back in their original undefined state.
1294 : 7695196 : gcc_assert (bi.def_stack.is_empty ()
1295 : : && bi.old_def_stack_limit.is_empty ());
1296 : 541467108 : for (unsigned int regno = 0; regno < m_num_regs; ++regno)
1297 : 537619510 : gcc_assert (!bi.last_access[regno + 1]);
1298 : : }
1299 : 3847662 : }
1300 : :
1301 : : // Print a description of CALL_CLOBBERS to PP.
1302 : : void
1303 : 0 : rtl_ssa::pp_ebb_call_clobbers (pretty_printer *pp,
1304 : : const ebb_call_clobbers_info *call_clobbers)
1305 : : {
1306 : 0 : if (!call_clobbers)
1307 : 0 : pp_string (pp, "<null>");
1308 : : else
1309 : 0 : call_clobbers->print_full (pp);
1310 : 0 : }
1311 : :
1312 : : // Print a description of BB to PP.
1313 : : void
1314 : 0 : rtl_ssa::pp_bb (pretty_printer *pp, const bb_info *bb)
1315 : : {
1316 : 0 : if (!bb)
1317 : 0 : pp_string (pp, "<null>");
1318 : : else
1319 : 0 : bb->print_full (pp);
1320 : 0 : }
1321 : :
1322 : : // Print a description of EBB to PP
1323 : : void
1324 : 0 : rtl_ssa::pp_ebb (pretty_printer *pp, const ebb_info *ebb)
1325 : : {
1326 : 0 : if (!ebb)
1327 : 0 : pp_string (pp, "<null>");
1328 : : else
1329 : 0 : ebb->print_full (pp);
1330 : 0 : }
1331 : :
1332 : : // Print a description of CALL_CLOBBERS to FILE.
1333 : : void
1334 : 0 : dump (FILE *file, const ebb_call_clobbers_info *call_clobbers)
1335 : : {
1336 : 0 : dump_using (file, pp_ebb_call_clobbers, call_clobbers);
1337 : 0 : }
1338 : :
1339 : : // Print a description of BB to FILE.
1340 : : void
1341 : 0 : dump (FILE *file, const bb_info *bb)
1342 : : {
1343 : 0 : dump_using (file, pp_bb, bb);
1344 : 0 : }
1345 : :
1346 : : // Print a description of EBB to FILE.
1347 : : void
1348 : 0 : dump (FILE *file, const ebb_info *ebb)
1349 : : {
1350 : 0 : dump_using (file, pp_ebb, ebb);
1351 : 0 : }
1352 : :
1353 : : // Debug interfaces to the dump routines above.
1354 : 0 : void debug (const ebb_call_clobbers_info *x) { dump (stderr, x); }
1355 : 0 : void debug (const bb_info *x) { dump (stderr, x); }
1356 : 0 : void debug (const ebb_info *x) { dump (stderr, x); }
|