Line data Source code
1 : /* Rewrite a program in Normal form into SSA.
2 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
3 : Contributed by Diego Novillo <dnovillo@redhat.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "gimple-pretty-print.h"
31 : #include "diagnostic-core.h"
32 : #include "langhooks.h"
33 : #include "cfganal.h"
34 : #include "gimple-iterator.h"
35 : #include "tree-cfg.h"
36 : #include "tree-into-ssa.h"
37 : #include "tree-dfa.h"
38 : #include "tree-ssa.h"
39 : #include "domwalk.h"
40 : #include "statistics.h"
41 : #include "stringpool.h"
42 : #include "attribs.h"
43 : #include "asan.h"
44 : #include "attr-fnspec.h"
45 :
46 : #define PERCENT(x,y) ((float)(x) * 100.0 / (float)(y))
47 :
48 : /* This file builds the SSA form for a function as described in:
49 : R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
50 : Computing Static Single Assignment Form and the Control Dependence
51 : Graph. ACM Transactions on Programming Languages and Systems,
52 : 13(4):451-490, October 1991. */
53 :
54 : /* Structure to map a variable VAR to the set of blocks that contain
55 : definitions for VAR. */
56 : struct def_blocks
57 : {
58 : /* Blocks that contain definitions of VAR. Bit I will be set if the
59 : Ith block contains a definition of VAR. */
60 : bitmap def_blocks;
61 :
62 : /* Blocks that contain a PHI node for VAR. */
63 : bitmap phi_blocks;
64 :
65 : /* Blocks where VAR is live-on-entry. Similar semantics as
66 : DEF_BLOCKS. */
67 : bitmap livein_blocks;
68 : };
69 :
70 : /* Stack of trees used to restore the global currdefs to its original
71 : state after completing rewriting of a block and its dominator
72 : children. Its elements have the following properties:
73 :
74 : - An SSA_NAME (N) indicates that the current definition of the
75 : underlying variable should be set to the given SSA_NAME. If the
76 : symbol associated with the SSA_NAME is not a GIMPLE register, the
77 : next slot in the stack must be a _DECL node (SYM). In this case,
78 : the name N in the previous slot is the current reaching
79 : definition for SYM.
80 :
81 : - A _DECL node indicates that the underlying variable has no
82 : current definition.
83 :
84 : - A NULL node at the top entry is used to mark the last slot
85 : associated with the current block. */
86 : static vec<tree> block_defs_stack;
87 :
88 :
89 : /* Set of existing SSA names being replaced by update_ssa. */
90 : static sbitmap old_ssa_names;
91 :
92 : /* Set of new SSA names being added by update_ssa. Note that both
93 : NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
94 : the operations done on them are presence tests. */
95 : static sbitmap new_ssa_names;
96 :
97 : static sbitmap interesting_blocks;
98 :
99 : /* Set of SSA names that have been marked to be released after they
100 : were registered in the replacement table. They will be finally
101 : released after we finish updating the SSA web. */
102 : bitmap names_to_release;
103 :
104 : /* The bitmap of blocks with PHIs to rewrite. */
105 : static bitmap blocks_with_phis_to_rewrite;
106 :
107 : /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
108 : to grow as the callers to create_new_def_for will create new names on
109 : the fly.
110 : FIXME. Currently set to 1/3 to avoid frequent reallocations but still
111 : need to find a reasonable growth strategy. */
112 : #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
113 :
114 :
115 : /* The function the SSA updating data structures have been initialized for.
116 : NULL if they need to be initialized by create_new_def_for. */
117 : static struct function *update_ssa_initialized_fn = NULL;
118 :
119 : /* It is advantageous to avoid things like life analysis for variables which
120 : do not need PHI nodes. This enum describes whether or not a particular
121 : variable may need a PHI node. */
122 :
123 : enum need_phi_state {
124 : /* This is the default. If we are still in this state after finding
125 : all the definition and use sites, then we will assume the variable
126 : needs PHI nodes. This is probably an overly conservative assumption. */
127 : NEED_PHI_STATE_UNKNOWN,
128 :
129 : /* This state indicates that we have seen one or more sets of the
130 : variable in a single basic block and that the sets dominate all
131 : uses seen so far. If after finding all definition and use sites
132 : we are still in this state, then the variable does not need any
133 : PHI nodes. */
134 : NEED_PHI_STATE_NO,
135 :
136 : /* This state indicates that we have either seen multiple definitions of
137 : the variable in multiple blocks, or that we encountered a use in a
138 : block that was not dominated by the block containing the set(s) of
139 : this variable. This variable is assumed to need PHI nodes. */
140 : NEED_PHI_STATE_MAYBE
141 : };
142 :
143 : /* Information stored for both SSA names and decls. */
144 : struct common_info
145 : {
146 : /* This field indicates whether or not the variable may need PHI nodes.
147 : See the enum's definition for more detailed information about the
148 : states. */
149 : enum need_phi_state need_phi_state : 2;
150 :
151 : /* The current reaching definition replacing this var. */
152 : tree current_def;
153 :
154 : /* Definitions for this var. */
155 : struct def_blocks def_blocks;
156 : };
157 :
158 : /* Information stored for decls. */
159 : struct var_info
160 : {
161 : /* The variable. */
162 : tree var;
163 :
164 : /* Information stored for both SSA names and decls. */
165 : common_info info;
166 : };
167 :
168 :
169 : /* VAR_INFOS hashtable helpers. */
170 :
171 : struct var_info_hasher : free_ptr_hash <var_info>
172 : {
173 : static inline hashval_t hash (const value_type &);
174 : static inline bool equal (const value_type &, const compare_type &);
175 : };
176 :
177 : inline hashval_t
178 1510343360 : var_info_hasher::hash (const value_type &p)
179 : {
180 1510343360 : return DECL_UID (p->var);
181 : }
182 :
183 : inline bool
184 2036443777 : var_info_hasher::equal (const value_type &p1, const compare_type &p2)
185 : {
186 2036443777 : return p1->var == p2->var;
187 : }
188 :
189 :
190 : /* Each entry in VAR_INFOS contains an element of type STRUCT
191 : VAR_INFO_D. */
192 : static hash_table<var_info_hasher> *var_infos;
193 :
194 :
195 : /* Information stored for SSA names. */
196 : struct ssa_name_info
197 : {
198 : /* Age of this record (so that info_for_ssa_name table can be cleared
199 : quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
200 : are assumed to be null. */
201 : unsigned age;
202 :
203 : /* Replacement mappings, allocated from update_ssa_obstack. */
204 : bitmap repl_set;
205 :
206 : /* Information stored for both SSA names and decls. */
207 : common_info info;
208 : };
209 :
210 : static vec<ssa_name_info *> info_for_ssa_name;
211 : static unsigned current_info_for_ssa_name_age;
212 :
213 : static bitmap_obstack update_ssa_obstack;
214 :
215 : /* The set of blocks affected by update_ssa. */
216 : static bitmap blocks_to_update;
217 :
218 : /* The main entry point to the SSA renamer (rewrite_blocks) may be
219 : called several times to do different, but related, tasks.
220 : Initially, we need it to rename the whole program into SSA form.
221 : At other times, we may need it to only rename into SSA newly
222 : exposed symbols. Finally, we can also call it to incrementally fix
223 : an already built SSA web. */
224 : enum rewrite_mode {
225 : /* Convert the whole function into SSA form. */
226 : REWRITE_ALL,
227 :
228 : /* Incrementally update the SSA web by replacing existing SSA
229 : names with new ones. See update_ssa for details. */
230 : REWRITE_UPDATE,
231 : REWRITE_UPDATE_REGION
232 : };
233 :
234 : /* The set of symbols we ought to re-write into SSA form in update_ssa. */
235 : static bitmap symbols_to_rename_set;
236 : static vec<tree> symbols_to_rename;
237 :
238 : /* Mark SYM for renaming. */
239 :
240 : static void
241 145992427 : mark_for_renaming (tree sym)
242 : {
243 145992427 : if (!symbols_to_rename_set)
244 2541965 : symbols_to_rename_set = BITMAP_ALLOC (NULL);
245 145992427 : if (bitmap_set_bit (symbols_to_rename_set, DECL_UID (sym)))
246 8426200 : symbols_to_rename.safe_push (sym);
247 145992427 : }
248 :
249 : /* Return true if SYM is marked for renaming. */
250 :
251 : static bool
252 397159170 : marked_for_renaming (tree sym)
253 : {
254 397159170 : if (!symbols_to_rename_set || sym == NULL_TREE)
255 : return false;
256 209821887 : return bitmap_bit_p (symbols_to_rename_set, DECL_UID (sym));
257 : }
258 :
259 :
260 : /* Return true if STMT needs to be rewritten. When renaming a subset
261 : of the variables, not all statements will be processed. This is
262 : decided in mark_def_sites. */
263 :
264 : static inline bool
265 980027589 : rewrite_uses_p (gimple *stmt)
266 : {
267 980027589 : return gimple_visited_p (stmt);
268 : }
269 :
270 :
271 : /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
272 :
273 : static inline void
274 872167489 : set_rewrite_uses (gimple *stmt, bool rewrite_p)
275 : {
276 872167489 : gimple_set_visited (stmt, rewrite_p);
277 : }
278 :
279 :
280 : /* Return true if the DEFs created by statement STMT should be
281 : registered when marking new definition sites. This is slightly
282 : different than rewrite_uses_p: it's used by update_ssa to
283 : distinguish statements that need to have both uses and defs
284 : processed from those that only need to have their defs processed.
285 : Statements that define new SSA names only need to have their defs
286 : registered, but they don't need to have their uses renamed. */
287 :
288 : static inline bool
289 707047330 : register_defs_p (gimple *stmt)
290 : {
291 494074790 : return gimple_plf (stmt, GF_PLF_1) != 0;
292 : }
293 :
294 :
295 : /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
296 :
297 : static inline void
298 800826741 : set_register_defs (gimple *stmt, bool register_defs_p)
299 : {
300 800826741 : gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
301 : }
302 :
303 :
304 : /* Get the information associated with NAME. */
305 :
306 : static inline ssa_name_info *
307 268804053 : get_ssa_name_ann (tree name)
308 : {
309 268804053 : unsigned ver = SSA_NAME_VERSION (name);
310 268804053 : unsigned len = info_for_ssa_name.length ();
311 268734378 : struct ssa_name_info *info;
312 :
313 : /* Re-allocate the vector at most once per update/into-SSA. */
314 268734378 : if (ver >= len)
315 4493834 : info_for_ssa_name.safe_grow_cleared (num_ssa_names, true);
316 :
317 : /* But allocate infos lazily. */
318 268804053 : info = info_for_ssa_name[ver];
319 268804053 : if (!info)
320 : {
321 9168744 : info = XCNEW (struct ssa_name_info);
322 9168744 : info->age = current_info_for_ssa_name_age;
323 9168744 : info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
324 9168744 : info_for_ssa_name[ver] = info;
325 : }
326 :
327 268804053 : if (info->age < current_info_for_ssa_name_age)
328 : {
329 18957429 : info->age = current_info_for_ssa_name_age;
330 18957429 : info->repl_set = NULL;
331 18957429 : info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
332 18957429 : info->info.current_def = NULL_TREE;
333 18957429 : info->info.def_blocks.def_blocks = NULL;
334 18957429 : info->info.def_blocks.phi_blocks = NULL;
335 18957429 : info->info.def_blocks.livein_blocks = NULL;
336 : }
337 :
338 268804053 : return info;
339 : }
340 :
341 : /* Return and allocate the auxiliary information for DECL. */
342 :
343 : static inline var_info *
344 698613179 : get_var_info (tree decl)
345 : {
346 698613179 : var_info vi;
347 698613179 : var_info **slot;
348 698613179 : vi.var = decl;
349 698613179 : slot = var_infos->find_slot_with_hash (&vi, DECL_UID (decl), INSERT);
350 698613179 : if (*slot == NULL)
351 : {
352 26644394 : var_info *v = XCNEW (var_info);
353 26644394 : v->var = decl;
354 26644394 : *slot = v;
355 26644394 : return v;
356 : }
357 : return *slot;
358 : }
359 :
360 :
361 : /* Clears info for SSA names. */
362 :
363 : static void
364 3976003 : clear_ssa_name_info (void)
365 : {
366 3976003 : current_info_for_ssa_name_age++;
367 :
368 : /* If current_info_for_ssa_name_age wraps we use stale information.
369 : Assert that this does not happen. */
370 3976003 : gcc_assert (current_info_for_ssa_name_age != 0);
371 3976003 : }
372 :
373 :
374 : /* Get access to the auxiliary information stored per SSA name or decl. */
375 :
376 : static inline common_info *
377 879703405 : get_common_info (tree var)
378 : {
379 879703405 : if (TREE_CODE (var) == SSA_NAME)
380 189693587 : return &get_ssa_name_ann (var)->info;
381 : else
382 690009818 : return &get_var_info (var)->info;
383 : }
384 :
385 :
386 : /* Return the current definition for VAR. */
387 :
388 : tree
389 978700 : get_current_def (tree var)
390 : {
391 978700 : return get_common_info (var)->current_def;
392 : }
393 :
394 :
395 : /* Sets current definition of VAR to DEF. */
396 :
397 : void
398 0 : set_current_def (tree var, tree def)
399 : {
400 0 : get_common_info (var)->current_def = def;
401 0 : }
402 :
403 : /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
404 : all statements in basic block BB. */
405 :
406 : static void
407 81889843 : initialize_flags_in_bb (basic_block bb)
408 : {
409 81889843 : gimple *stmt;
410 81889843 : gimple_stmt_iterator gsi;
411 :
412 118201009 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
413 : {
414 36311166 : gimple *phi = gsi_stmt (gsi);
415 36311166 : set_rewrite_uses (phi, false);
416 36311166 : set_register_defs (phi, false);
417 : }
418 :
419 727382164 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
420 : {
421 563602478 : stmt = gsi_stmt (gsi);
422 :
423 : /* We are going to use the operand cache API, such as
424 : SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
425 : cache for each statement should be up-to-date. */
426 1124580287 : gcc_checking_assert (!gimple_modified_p (stmt));
427 563602478 : set_rewrite_uses (stmt, false);
428 563602478 : set_register_defs (stmt, false);
429 : }
430 81889843 : }
431 :
432 : /* Mark block BB as interesting for update_ssa. */
433 :
434 : static void
435 561715158 : mark_block_for_update (basic_block bb)
436 : {
437 561715158 : gcc_checking_assert (blocks_to_update != NULL);
438 561715158 : if (!bitmap_set_bit (blocks_to_update, bb->index))
439 : return;
440 81889843 : initialize_flags_in_bb (bb);
441 : }
442 :
443 : /* Return the set of blocks where variable VAR is defined and the blocks
444 : where VAR is live on entry (livein). If no entry is found in
445 : DEF_BLOCKS, a new one is created and returned. */
446 :
447 : static inline def_blocks *
448 358620753 : get_def_blocks_for (common_info *info)
449 : {
450 358620753 : def_blocks *db_p = &info->def_blocks;
451 358620753 : if (!db_p->def_blocks)
452 : {
453 45854161 : db_p->def_blocks = BITMAP_ALLOC (&update_ssa_obstack);
454 45854161 : db_p->phi_blocks = BITMAP_ALLOC (&update_ssa_obstack);
455 45854161 : db_p->livein_blocks = BITMAP_ALLOC (&update_ssa_obstack);
456 : }
457 :
458 358620753 : return db_p;
459 : }
460 :
461 :
462 : /* Mark block BB as the definition site for variable VAR. PHI_P is true if
463 : VAR is defined by a PHI node. */
464 :
465 : static void
466 130962584 : set_def_block (tree var, basic_block bb, bool phi_p)
467 : {
468 130962584 : def_blocks *db_p;
469 130962584 : common_info *info;
470 :
471 130962584 : info = get_common_info (var);
472 130962584 : db_p = get_def_blocks_for (info);
473 :
474 : /* Set the bit corresponding to the block where VAR is defined. */
475 130962584 : bitmap_set_bit (db_p->def_blocks, bb->index);
476 130962584 : if (phi_p)
477 23556134 : bitmap_set_bit (db_p->phi_blocks, bb->index);
478 :
479 : /* Keep track of whether or not we may need to insert PHI nodes.
480 :
481 : If we are in the UNKNOWN state, then this is the first definition
482 : of VAR. Additionally, we have not seen any uses of VAR yet, so
483 : we do not need a PHI node for this variable at this time (i.e.,
484 : transition to NEED_PHI_STATE_NO).
485 :
486 : If we are in any other state, then we either have multiple definitions
487 : of this variable occurring in different blocks or we saw a use of the
488 : variable which was not dominated by the block containing the
489 : definition(s). In this case we may need a PHI node, so enter
490 : state NEED_PHI_STATE_MAYBE. */
491 130962584 : if (info->need_phi_state == NEED_PHI_STATE_UNKNOWN)
492 : info->need_phi_state = NEED_PHI_STATE_NO;
493 : else
494 94802824 : info->need_phi_state = NEED_PHI_STATE_MAYBE;
495 130962584 : }
496 :
497 :
498 : /* Mark block BB as having VAR live at the entry to BB. */
499 :
500 : static void
501 98084157 : set_livein_block (tree var, basic_block bb)
502 : {
503 98084157 : common_info *info;
504 98084157 : def_blocks *db_p;
505 :
506 98084157 : info = get_common_info (var);
507 98084157 : db_p = get_def_blocks_for (info);
508 :
509 : /* Set the bit corresponding to the block where VAR is live in. */
510 98084157 : bitmap_set_bit (db_p->livein_blocks, bb->index);
511 :
512 : /* Keep track of whether or not we may need to insert PHI nodes.
513 :
514 : If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
515 : by the single block containing the definition(s) of this variable. If
516 : it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
517 : NEED_PHI_STATE_MAYBE. */
518 98084157 : if (info->need_phi_state == NEED_PHI_STATE_NO)
519 : {
520 10815040 : int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
521 :
522 10815040 : if (def_block_index == -1
523 21630080 : || ! dominated_by_p (CDI_DOMINATORS, bb,
524 10815040 : BASIC_BLOCK_FOR_FN (cfun, def_block_index)))
525 89437 : info->need_phi_state = NEED_PHI_STATE_MAYBE;
526 : }
527 : else
528 87269117 : info->need_phi_state = NEED_PHI_STATE_MAYBE;
529 98084157 : }
530 :
531 :
532 : /* Return true if NAME is in OLD_SSA_NAMES. */
533 :
534 : static inline bool
535 152082108 : is_old_name (tree name)
536 : {
537 152082108 : unsigned ver = SSA_NAME_VERSION (name);
538 152082108 : if (!old_ssa_names)
539 : return false;
540 150575358 : return (ver < SBITMAP_SIZE (old_ssa_names)
541 150575358 : && bitmap_bit_p (old_ssa_names, ver));
542 : }
543 :
544 :
545 : /* Return true if NAME is in NEW_SSA_NAMES. */
546 :
547 : static inline bool
548 71649950 : is_new_name (tree name)
549 : {
550 71649950 : unsigned ver = SSA_NAME_VERSION (name);
551 71649950 : if (!new_ssa_names)
552 : return false;
553 70143200 : return (ver < SBITMAP_SIZE (new_ssa_names)
554 70143200 : && bitmap_bit_p (new_ssa_names, ver));
555 : }
556 :
557 :
558 : /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
559 :
560 : static inline bitmap
561 30153651 : names_replaced_by (tree new_tree)
562 : {
563 60177274 : return get_ssa_name_ann (new_tree)->repl_set;
564 : }
565 :
566 :
567 : /* Add OLD to REPL_TBL[NEW_TREE].SET. */
568 :
569 : static inline void
570 18146173 : add_to_repl_tbl (tree new_tree, tree old)
571 : {
572 18146173 : bitmap *set = &get_ssa_name_ann (new_tree)->repl_set;
573 18146173 : if (!*set)
574 18146173 : *set = BITMAP_ALLOC (&update_ssa_obstack);
575 18146173 : bitmap_set_bit (*set, SSA_NAME_VERSION (old));
576 18146173 : }
577 :
578 : /* Debugging aid to fence old_ssa_names changes when iterating over it. */
579 : static bool iterating_old_ssa_names;
580 :
581 : /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
582 : represents the set of names O_1 ... O_j replaced by N_i. This is
583 : used by update_ssa and its helpers to introduce new SSA names in an
584 : already formed SSA web. */
585 :
586 : static void
587 18146173 : add_new_name_mapping (tree new_tree, tree old)
588 : {
589 : /* OLD and NEW_TREE must be different SSA names for the same symbol. */
590 61504000 : gcc_checking_assert (new_tree != old
591 : && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
592 :
593 : /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
594 : caller may have created new names since the set was created. */
595 18146173 : if (SBITMAP_SIZE (new_ssa_names) <= SSA_NAME_VERSION (new_tree))
596 : {
597 151848 : unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
598 75924 : new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
599 : }
600 18146173 : if (SBITMAP_SIZE (old_ssa_names) <= SSA_NAME_VERSION (old))
601 : {
602 111 : gcc_assert (!iterating_old_ssa_names);
603 222 : unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
604 111 : old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
605 : }
606 :
607 : /* Update the REPL_TBL table. */
608 18146173 : add_to_repl_tbl (new_tree, old);
609 :
610 : /* If OLD had already been registered as a new name, then all the
611 : names that OLD replaces should also be replaced by NEW_TREE. */
612 18146173 : if (is_new_name (old))
613 130028 : bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
614 :
615 : /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
616 : respectively. */
617 18146173 : if (iterating_old_ssa_names)
618 3173901 : gcc_assert (bitmap_bit_p (old_ssa_names, SSA_NAME_VERSION (old)));
619 : else
620 14972272 : bitmap_set_bit (old_ssa_names, SSA_NAME_VERSION (old));
621 18146173 : bitmap_set_bit (new_ssa_names, SSA_NAME_VERSION (new_tree));
622 18146173 : }
623 :
624 :
625 : /* Call back for walk_dominator_tree used to collect definition sites
626 : for every variable in the function. For every statement S in block
627 : BB:
628 :
629 : 1- Variables defined by S in the DEFS of S are marked in the bitmap
630 : KILLS.
631 :
632 : 2- If S uses a variable VAR and there is no preceding kill of VAR,
633 : then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
634 :
635 : This information is used to determine which variables are live
636 : across block boundaries to reduce the number of PHI nodes
637 : we create. */
638 :
639 : static void
640 67798828 : mark_def_sites (basic_block bb, gimple *stmt, bitmap kills)
641 : {
642 67798828 : tree def;
643 67798828 : use_operand_p use_p;
644 67798828 : ssa_op_iter iter;
645 :
646 : /* Since this is the first time that we rewrite the program into SSA
647 : form, force an operand scan on every statement. */
648 67798828 : update_stmt (stmt);
649 :
650 67798828 : gcc_checking_assert (blocks_to_update == NULL);
651 67798828 : set_register_defs (stmt, false);
652 67798828 : set_rewrite_uses (stmt, false);
653 :
654 67798828 : if (is_gimple_debug (stmt))
655 : {
656 2369807 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
657 : {
658 0 : tree sym = USE_FROM_PTR (use_p);
659 0 : gcc_checking_assert (DECL_P (sym));
660 0 : set_rewrite_uses (stmt, true);
661 : }
662 2369807 : if (rewrite_uses_p (stmt))
663 0 : bitmap_set_bit (interesting_blocks, bb->index);
664 2369807 : return;
665 : }
666 :
667 : /* If a variable is used before being set, then the variable is live
668 : across a block boundary, so mark it live-on-entry to BB. */
669 149901904 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
670 : {
671 84472883 : tree sym = USE_FROM_PTR (use_p);
672 84472883 : if (TREE_CODE (sym) == SSA_NAME)
673 20953495 : continue;
674 63519388 : gcc_checking_assert (DECL_P (sym));
675 63519388 : if (!bitmap_bit_p (kills, DECL_UID (sym)))
676 37009168 : set_livein_block (sym, bb);
677 63519388 : set_rewrite_uses (stmt, true);
678 : }
679 :
680 : /* Now process the defs. Mark BB as the definition block and add
681 : each def to the set of killed symbols. */
682 122835263 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
683 : {
684 57406242 : if (TREE_CODE (def) == SSA_NAME)
685 20642580 : continue;
686 36763662 : gcc_checking_assert (DECL_P (def));
687 36763662 : set_def_block (def, bb, false);
688 36763662 : bitmap_set_bit (kills, DECL_UID (def));
689 36763662 : set_register_defs (stmt, true);
690 : }
691 :
692 : /* If we found the statement interesting then also mark the block BB
693 : as interesting. */
694 65429021 : if (rewrite_uses_p (stmt) || register_defs_p (stmt))
695 54503089 : bitmap_set_bit (interesting_blocks, bb->index);
696 : }
697 :
698 : /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
699 : in the dfs numbering of the dominance tree. */
700 :
701 : struct dom_dfsnum
702 : {
703 : /* Basic block whose index this entry corresponds to. */
704 : unsigned bb_index;
705 :
706 : /* The dfs number of this node. */
707 : unsigned dfs_num;
708 : };
709 :
710 : /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
711 : for qsort. */
712 :
713 : static int
714 2244281133 : cmp_dfsnum (const void *a, const void *b)
715 : {
716 2244281133 : const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
717 2244281133 : const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
718 :
719 2244281133 : return (int) da->dfs_num - (int) db->dfs_num;
720 : }
721 :
722 : /* Among the intervals starting at the N points specified in DEFS, find
723 : the one that contains S, and return its bb_index. */
724 :
725 : static unsigned
726 36669959 : find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
727 : {
728 36669959 : unsigned f = 0, t = n, m;
729 :
730 260579309 : while (t > f + 1)
731 : {
732 187239391 : m = (f + t) / 2;
733 187239391 : if (defs[m].dfs_num <= s)
734 : f = m;
735 : else
736 136973726 : t = m;
737 : }
738 :
739 36669959 : return defs[f].bb_index;
740 : }
741 :
742 : /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
743 : KILLS is a bitmap of blocks where the value is defined before any use. */
744 :
745 : static void
746 19149854 : prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
747 : {
748 19149854 : bitmap_iterator bi;
749 19149854 : unsigned i, b, p, u, top;
750 19149854 : bitmap live_phis;
751 19149854 : basic_block def_bb, use_bb;
752 19149854 : edge e;
753 19149854 : edge_iterator ei;
754 19149854 : bitmap to_remove;
755 19149854 : struct dom_dfsnum *defs;
756 19149854 : unsigned n_defs, adef;
757 :
758 19149854 : if (bitmap_empty_p (uses))
759 : {
760 4238119 : bitmap_clear (phis);
761 15840242 : return;
762 : }
763 :
764 : /* The phi must dominate a use, or an argument of a live phi. Also, we
765 : do not create any phi nodes in def blocks, unless they are also livein. */
766 14911735 : to_remove = BITMAP_ALLOC (NULL);
767 14911735 : bitmap_and_compl (to_remove, kills, uses);
768 14911735 : bitmap_and_compl_into (phis, to_remove);
769 14911735 : if (bitmap_empty_p (phis))
770 : {
771 7364004 : BITMAP_FREE (to_remove);
772 7364004 : return;
773 : }
774 :
775 : /* We want to remove the unnecessary phi nodes, but we do not want to compute
776 : liveness information, as that may be linear in the size of CFG, and if
777 : there are lot of different variables to rewrite, this may lead to quadratic
778 : behavior.
779 :
780 : Instead, we basically emulate standard dce. We put all uses to worklist,
781 : then for each of them find the nearest def that dominates them. If this
782 : def is a phi node, we mark it live, and if it was not live before, we
783 : add the predecessors of its basic block to the worklist.
784 :
785 : To quickly locate the nearest def that dominates use, we use dfs numbering
786 : of the dominance tree (that is already available in order to speed up
787 : queries). For each def, we have the interval given by the dfs number on
788 : entry to and on exit from the corresponding subtree in the dominance tree.
789 : The nearest dominator for a given use is the smallest of these intervals
790 : that contains entry and exit dfs numbers for the basic block with the use.
791 : If we store the bounds for all the uses to an array and sort it, we can
792 : locate the nearest dominating def in logarithmic time by binary search.*/
793 7547731 : bitmap_ior (to_remove, kills, phis);
794 7547731 : n_defs = bitmap_count_bits (to_remove);
795 7547731 : adef = 2 * n_defs + 1;
796 7547731 : defs = XNEWVEC (struct dom_dfsnum, adef);
797 7547731 : defs[0].bb_index = 1;
798 7547731 : defs[0].dfs_num = 0;
799 7547731 : struct dom_dfsnum *head = defs + 1, *tail = defs + adef;
800 55820492 : EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
801 : {
802 48272761 : def_bb = BASIC_BLOCK_FOR_FN (cfun, i);
803 48272761 : head->bb_index = i;
804 48272761 : head->dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
805 48272761 : head++, tail--;
806 48272761 : tail->bb_index = i;
807 48272761 : tail->dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
808 : }
809 7547731 : gcc_checking_assert (head == tail);
810 7547731 : BITMAP_FREE (to_remove);
811 7547731 : qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
812 7547731 : gcc_assert (defs[0].bb_index == 1);
813 :
814 : /* Now each DEFS entry contains the number of the basic block to that the
815 : dfs number corresponds. Change them to the number of basic block that
816 : corresponds to the interval following the dfs number. Also, for the
817 : dfs_out numbers, increase the dfs number by one (so that it corresponds
818 : to the start of the following interval, not to the end of the current
819 : one). We use WORKLIST as a stack. */
820 7547731 : auto_vec<int> worklist (n_defs + 1);
821 7547731 : worklist.quick_push (1);
822 7547731 : top = 1;
823 7547731 : n_defs = 1;
824 104093253 : for (i = 1; i < adef; i++)
825 : {
826 96545522 : b = defs[i].bb_index;
827 96545522 : if (b == top)
828 : {
829 : /* This is a closing element. Interval corresponding to the top
830 : of the stack after removing it follows. */
831 48272761 : worklist.pop ();
832 48272761 : top = worklist[worklist.length () - 1];
833 48272761 : defs[n_defs].bb_index = top;
834 48272761 : defs[n_defs].dfs_num = defs[i].dfs_num + 1;
835 : }
836 : else
837 : {
838 : /* Opening element. Nothing to do, just push it to the stack and move
839 : it to the correct position. */
840 48272761 : defs[n_defs].bb_index = defs[i].bb_index;
841 48272761 : defs[n_defs].dfs_num = defs[i].dfs_num;
842 48272761 : worklist.quick_push (b);
843 48272761 : top = b;
844 : }
845 :
846 : /* If this interval starts at the same point as the previous one, cancel
847 : the previous one. */
848 96545522 : if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
849 12556876 : defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
850 : else
851 83988646 : n_defs++;
852 : }
853 7547731 : worklist.pop ();
854 7547731 : gcc_assert (worklist.is_empty ());
855 :
856 : /* Now process the uses. */
857 7547731 : live_phis = BITMAP_ALLOC (NULL);
858 45440855 : EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
859 : {
860 37893124 : worklist.safe_push (i);
861 : }
862 :
863 51956424 : while (!worklist.is_empty ())
864 : {
865 44408693 : b = worklist.pop ();
866 44408693 : if (b == ENTRY_BLOCK)
867 6158 : continue;
868 :
869 : /* If there is a phi node in USE_BB, it is made live. Otherwise,
870 : find the def that dominates the immediate dominator of USE_BB
871 : (the kill in USE_BB does not dominate the use). */
872 44402535 : if (bitmap_bit_p (phis, b))
873 : p = b;
874 : else
875 : {
876 36669959 : use_bb = get_immediate_dominator (CDI_DOMINATORS,
877 36669959 : BASIC_BLOCK_FOR_FN (cfun, b));
878 36669959 : p = find_dfsnum_interval (defs, n_defs,
879 : bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
880 36669959 : if (!bitmap_bit_p (phis, p))
881 25266391 : continue;
882 : }
883 :
884 : /* If the phi node is already live, there is nothing to do. */
885 19136144 : if (!bitmap_set_bit (live_phis, p))
886 9276730 : continue;
887 :
888 : /* Add the new uses to the worklist. */
889 9859414 : def_bb = BASIC_BLOCK_FOR_FN (cfun, p);
890 34909288 : FOR_EACH_EDGE (e, ei, def_bb->preds)
891 : {
892 25049874 : u = e->src->index;
893 25049874 : if (bitmap_bit_p (uses, u))
894 10624554 : continue;
895 :
896 : /* In case there is a kill directly in the use block, do not record
897 : the use (this is also necessary for correctness, as we assume that
898 : uses dominated by a def directly in their block have been filtered
899 : out before). */
900 14425320 : if (bitmap_bit_p (kills, u))
901 7909751 : continue;
902 :
903 6515569 : bitmap_set_bit (uses, u);
904 6515569 : worklist.safe_push (u);
905 : }
906 : }
907 :
908 7547731 : bitmap_copy (phis, live_phis);
909 7547731 : BITMAP_FREE (live_phis);
910 7547731 : free (defs);
911 7547731 : }
912 :
913 : /* Return the set of blocks where variable VAR is defined and the blocks
914 : where VAR is live on entry (livein). Return NULL, if no entry is
915 : found in DEF_BLOCKS. */
916 :
917 : static inline def_blocks *
918 34797222 : find_def_blocks_for (tree var)
919 : {
920 69594444 : def_blocks *p = &get_common_info (var)->def_blocks;
921 34797222 : if (!p->def_blocks)
922 0 : return NULL;
923 : return p;
924 : }
925 :
926 :
927 : /* Marks phi node PHI in basic block BB for rewrite. */
928 :
929 : static void
930 40300548 : mark_phi_for_rewrite (basic_block bb, gphi *phi)
931 : {
932 40300548 : if (rewrite_uses_p (phi))
933 : return;
934 :
935 25917764 : set_rewrite_uses (phi, true);
936 :
937 25917764 : if (!blocks_with_phis_to_rewrite)
938 : return;
939 :
940 21416536 : bitmap_set_bit (blocks_with_phis_to_rewrite, bb->index);
941 : }
942 :
943 : /* Insert PHI nodes for variable VAR using the iterated dominance
944 : frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
945 : function assumes that the caller is incrementally updating the
946 : existing SSA form, in which case VAR may be an SSA name instead of
947 : a symbol.
948 :
949 : PHI_INSERTION_POINTS is updated to reflect nodes that already had a
950 : PHI node for VAR. On exit, only the nodes that received a PHI node
951 : for VAR will be present in PHI_INSERTION_POINTS. */
952 :
953 : static void
954 19149854 : insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
955 : {
956 19149854 : unsigned bb_index;
957 19149854 : edge e;
958 19149854 : gphi *phi;
959 19149854 : basic_block bb;
960 19149854 : bitmap_iterator bi;
961 19149854 : def_blocks *def_map = find_def_blocks_for (var);
962 :
963 : /* Remove the blocks where we already have PHI nodes for VAR. */
964 19149854 : bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
965 :
966 : /* Remove obviously useless phi nodes. */
967 19149854 : prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
968 : def_map->livein_blocks);
969 :
970 : /* And insert the PHI nodes. */
971 29009268 : EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
972 : {
973 9859414 : bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
974 9859414 : if (update_p)
975 5358186 : mark_block_for_update (bb);
976 :
977 9859414 : if (dump_file && (dump_flags & TDF_DETAILS))
978 : {
979 908 : fprintf (dump_file, "creating PHI node in block #%d for ", bb_index);
980 908 : print_generic_expr (dump_file, var, TDF_SLIM);
981 908 : fprintf (dump_file, "\n");
982 : }
983 9859414 : phi = NULL;
984 :
985 9859414 : if (TREE_CODE (var) == SSA_NAME)
986 : {
987 : /* If we are rewriting SSA names, create the LHS of the PHI
988 : node by duplicating VAR. This is useful in the case of
989 : pointers, to also duplicate pointer attributes (alias
990 : information, in particular). */
991 3173901 : edge_iterator ei;
992 3173901 : tree new_lhs;
993 :
994 3173901 : gcc_checking_assert (update_p);
995 3173901 : new_lhs = duplicate_ssa_name (var, NULL);
996 3173901 : phi = create_phi_node (new_lhs, bb);
997 3173901 : add_new_name_mapping (new_lhs, var);
998 :
999 : /* Add VAR to every argument slot of PHI. We need VAR in
1000 : every argument so that rewrite_update_phi_arguments knows
1001 : which name is this PHI node replacing. If VAR is a
1002 : symbol marked for renaming, this is not necessary, the
1003 : renamer will use the symbol on the LHS to get its
1004 : reaching definition. */
1005 10389542 : FOR_EACH_EDGE (e, ei, bb->preds)
1006 7215641 : add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
1007 : }
1008 : else
1009 : {
1010 6685513 : tree tracked_var;
1011 :
1012 6685513 : gcc_checking_assert (DECL_P (var));
1013 6685513 : phi = create_phi_node (var, bb);
1014 :
1015 6685513 : tracked_var = target_for_debug_bind (var);
1016 6685513 : if (tracked_var)
1017 : {
1018 695997 : gimple *note = gimple_build_debug_bind (tracked_var,
1019 : PHI_RESULT (phi),
1020 : phi);
1021 695997 : gimple_stmt_iterator si = gsi_after_labels (bb);
1022 695997 : gsi_insert_before (&si, note, GSI_SAME_STMT);
1023 : }
1024 : }
1025 :
1026 : /* Mark this PHI node as interesting for update_ssa. */
1027 9859414 : set_register_defs (phi, true);
1028 9859414 : mark_phi_for_rewrite (bb, phi);
1029 : }
1030 19149854 : }
1031 :
1032 : /* Sort var_infos after DECL_UID of their var. */
1033 :
1034 : static int
1035 68290214 : insert_phi_nodes_compare_var_infos (const void *a, const void *b)
1036 : {
1037 68290214 : const var_info *defa = *(var_info * const *)a;
1038 68290214 : const var_info *defb = *(var_info * const *)b;
1039 68290214 : if (DECL_UID (defa->var) < DECL_UID (defb->var))
1040 : return -1;
1041 : else
1042 31933904 : return 1;
1043 : }
1044 :
1045 : /* Insert PHI nodes at the dominance frontier of blocks with variable
1046 : definitions. DFS contains the dominance frontier information for
1047 : the flowgraph. */
1048 :
1049 : static void
1050 3001200 : insert_phi_nodes (bitmap_head *dfs)
1051 : {
1052 3001200 : hash_table<var_info_hasher>::iterator hi;
1053 3001200 : unsigned i;
1054 3001200 : var_info *info;
1055 :
1056 : /* When the gimplifier introduces SSA names it cannot easily avoid
1057 : situations where abnormal edges added by CFG construction break
1058 : the use-def dominance requirement. For this case rewrite SSA
1059 : names with broken use-def dominance out-of-SSA and register them
1060 : for PHI insertion. We only need to do this if abnormal edges
1061 : can appear in the function. */
1062 3001200 : tree name;
1063 3001200 : if (cfun->calls_setjmp
1064 2999508 : || cfun->has_nonlocal_label)
1065 9233 : FOR_EACH_SSA_NAME (i, name, cfun)
1066 : {
1067 7017 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1068 7017 : if (SSA_NAME_IS_DEFAULT_DEF (name))
1069 0 : continue;
1070 :
1071 7017 : basic_block def_bb = gimple_bb (def_stmt);
1072 7017 : imm_use_iterator it;
1073 7017 : gimple *use_stmt;
1074 7017 : bool need_phis = false;
1075 14094 : FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
1076 : {
1077 7077 : basic_block use_bb = gimple_bb (use_stmt);
1078 7077 : if (use_bb != def_bb
1079 7077 : && ! dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1080 : need_phis = true;
1081 7017 : }
1082 7017 : if (need_phis)
1083 : {
1084 103 : tree var = create_tmp_reg (TREE_TYPE (name));
1085 103 : use_operand_p use_p;
1086 206 : FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
1087 : {
1088 103 : basic_block use_bb = gimple_bb (use_stmt);
1089 206 : FOR_EACH_IMM_USE_ON_STMT (use_p, it)
1090 103 : SET_USE (use_p, var);
1091 103 : update_stmt (use_stmt);
1092 103 : set_livein_block (var, use_bb);
1093 103 : set_rewrite_uses (use_stmt, true);
1094 103 : bitmap_set_bit (interesting_blocks, use_bb->index);
1095 103 : }
1096 103 : def_operand_p def_p;
1097 103 : ssa_op_iter dit;
1098 206 : FOR_EACH_SSA_DEF_OPERAND (def_p, def_stmt, dit, SSA_OP_DEF)
1099 103 : if (DEF_FROM_PTR (def_p) == name)
1100 103 : SET_DEF (def_p, var);
1101 103 : update_stmt (def_stmt);
1102 103 : set_def_block (var, def_bb, false);
1103 103 : set_register_defs (def_stmt, true);
1104 103 : bitmap_set_bit (interesting_blocks, def_bb->index);
1105 103 : release_ssa_name (name);
1106 : }
1107 : }
1108 :
1109 3001200 : auto_vec<var_info *> vars (var_infos->elements ());
1110 21219394 : FOR_EACH_HASH_TABLE_ELEMENT (*var_infos, info, var_info_p, hi)
1111 18218194 : if (info->info.need_phi_state != NEED_PHI_STATE_NO)
1112 9474270 : vars.quick_push (info);
1113 :
1114 : /* Do two stages to avoid code generation differences for UID
1115 : differences but no UID ordering differences. */
1116 3001200 : vars.qsort (insert_phi_nodes_compare_var_infos);
1117 :
1118 15474853 : FOR_EACH_VEC_ELT (vars, i, info)
1119 : {
1120 9474270 : bitmap idf = compute_idf (info->info.def_blocks.def_blocks, dfs);
1121 9474270 : insert_phi_nodes_for (info->var, idf, false);
1122 9474270 : BITMAP_FREE (idf);
1123 : }
1124 3001200 : }
1125 :
1126 :
1127 : /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1128 : register DEF (an SSA_NAME) to be a new definition for SYM. */
1129 :
1130 : static void
1131 41264993 : register_new_def (tree def, tree sym)
1132 : {
1133 41264993 : common_info *info = get_common_info (sym);
1134 41264993 : tree currdef;
1135 :
1136 : /* If this variable is set in a single basic block and all uses are
1137 : dominated by the set(s) in that single basic block, then there is
1138 : no reason to record anything for this variable in the block local
1139 : definition stacks. Doing so just wastes time and memory.
1140 :
1141 : This is the same test to prune the set of variables which may
1142 : need PHI nodes. So we just use that information since it's already
1143 : computed and available for us to use. */
1144 41264993 : if (info->need_phi_state == NEED_PHI_STATE_NO)
1145 : {
1146 8743924 : info->current_def = def;
1147 8743924 : return;
1148 : }
1149 :
1150 32521069 : currdef = info->current_def;
1151 :
1152 : /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1153 : SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1154 : in the stack so that we know which symbol is being defined by
1155 : this SSA name when we unwind the stack. */
1156 32521069 : if (currdef && !is_gimple_reg (sym))
1157 21906585 : block_defs_stack.safe_push (sym);
1158 :
1159 : /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1160 : stack is later used by the dominator tree callbacks to restore
1161 : the reaching definitions for all the variables defined in the
1162 : block after a recursive visit to all its immediately dominated
1163 : blocks. If there is no current reaching definition, then just
1164 : record the underlying _DECL node. */
1165 39166749 : block_defs_stack.safe_push (currdef ? currdef : sym);
1166 :
1167 : /* Set the current reaching definition for SYM to be DEF. */
1168 32521069 : info->current_def = def;
1169 : }
1170 :
1171 :
1172 : /* Perform a depth-first traversal of the dominator tree looking for
1173 : variables to rename. BB is the block where to start searching.
1174 : Renaming is a five step process:
1175 :
1176 : 1- Every definition made by PHI nodes at the start of the blocks is
1177 : registered as the current definition for the corresponding variable.
1178 :
1179 : 2- Every statement in BB is rewritten. USE and VUSE operands are
1180 : rewritten with their corresponding reaching definition. DEF and
1181 : VDEF targets are registered as new definitions.
1182 :
1183 : 3- All the PHI nodes in successor blocks of BB are visited. The
1184 : argument corresponding to BB is replaced with its current reaching
1185 : definition.
1186 :
1187 : 4- Recursively rewrite every dominator child block of BB.
1188 :
1189 : 5- Restore (in reverse order) the current reaching definition for every
1190 : new definition introduced in this block. This is done so that when
1191 : we return from the recursive call, all the current reaching
1192 : definitions are restored to the names that were valid in the
1193 : dominator parent of BB. */
1194 :
1195 : /* Return the current definition for variable VAR. If none is found,
1196 : create a new SSA name to act as the zeroth definition for VAR. */
1197 :
1198 : static tree
1199 227562548 : get_reaching_def (tree var)
1200 : {
1201 227562548 : common_info *info = get_common_info (var);
1202 227562548 : tree currdef;
1203 :
1204 : /* Lookup the current reaching definition for VAR. */
1205 227562548 : currdef = info->current_def;
1206 :
1207 : /* If there is no reaching definition for VAR, create and register a
1208 : default definition for it (if needed). */
1209 227562548 : if (currdef == NULL_TREE)
1210 : {
1211 21795667 : tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1212 : if (! sym)
1213 2 : sym = create_tmp_reg (TREE_TYPE (var));
1214 21795665 : currdef = get_or_create_ssa_default_def (cfun, sym);
1215 : }
1216 :
1217 : /* Return the current reaching definition for VAR, or the default
1218 : definition, if we had to create one. */
1219 227562548 : return currdef;
1220 : }
1221 :
1222 :
1223 : /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1224 :
1225 : static void
1226 0 : rewrite_debug_stmt_uses (gimple *stmt)
1227 : {
1228 0 : use_operand_p use_p;
1229 0 : ssa_op_iter iter;
1230 0 : bool update = false;
1231 :
1232 0 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1233 : {
1234 0 : tree var = USE_FROM_PTR (use_p), def;
1235 0 : common_info *info = get_common_info (var);
1236 0 : gcc_checking_assert (DECL_P (var));
1237 0 : def = info->current_def;
1238 0 : if (!def)
1239 : {
1240 0 : if (TREE_CODE (var) == PARM_DECL
1241 0 : && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
1242 : {
1243 0 : gimple_stmt_iterator gsi
1244 : =
1245 0 : gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1246 0 : int lim;
1247 : /* Search a few source bind stmts at the start of first bb to
1248 : see if a DEBUG_EXPR_DECL can't be reused. */
1249 0 : for (lim = 32;
1250 0 : !gsi_end_p (gsi) && lim > 0;
1251 0 : gsi_next (&gsi), lim--)
1252 : {
1253 0 : gimple *gstmt = gsi_stmt (gsi);
1254 0 : if (!gimple_debug_source_bind_p (gstmt))
1255 : break;
1256 0 : if (gimple_debug_source_bind_get_value (gstmt) == var)
1257 : {
1258 0 : def = gimple_debug_source_bind_get_var (gstmt);
1259 0 : if (TREE_CODE (def) == DEBUG_EXPR_DECL)
1260 : break;
1261 : else
1262 : def = NULL_TREE;
1263 : }
1264 : }
1265 : /* If not, add a new source bind stmt. */
1266 0 : if (def == NULL_TREE)
1267 : {
1268 0 : gimple *def_temp;
1269 0 : def = build_debug_expr_decl (TREE_TYPE (var));
1270 : /* FIXME: Is setting the mode really necessary? */
1271 0 : SET_DECL_MODE (def, DECL_MODE (var));
1272 0 : def_temp = gimple_build_debug_source_bind (def, var, NULL);
1273 0 : gsi =
1274 0 : gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1275 0 : gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
1276 : }
1277 0 : update = true;
1278 : }
1279 : }
1280 : else
1281 : {
1282 : /* Check if info->current_def can be trusted. */
1283 0 : basic_block bb = gimple_bb (stmt);
1284 0 : basic_block def_bb
1285 0 : = SSA_NAME_IS_DEFAULT_DEF (def)
1286 0 : ? NULL : gimple_bb (SSA_NAME_DEF_STMT (def));
1287 :
1288 : /* If definition is in current bb, it is fine. */
1289 0 : if (bb == def_bb)
1290 : ;
1291 : /* If definition bb doesn't dominate the current bb,
1292 : it can't be used. */
1293 0 : else if (def_bb && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
1294 : def = NULL;
1295 : /* If there is just one definition and dominates the current
1296 : bb, it is fine. */
1297 0 : else if (info->need_phi_state == NEED_PHI_STATE_NO)
1298 : ;
1299 : else
1300 : {
1301 0 : def_blocks *db_p = get_def_blocks_for (info);
1302 :
1303 : /* If there are some non-debug uses in the current bb,
1304 : it is fine. */
1305 0 : if (bitmap_bit_p (db_p->livein_blocks, bb->index))
1306 : ;
1307 : /* Otherwise give up for now. */
1308 : else
1309 : def = NULL;
1310 : }
1311 : }
1312 0 : if (def == NULL)
1313 : {
1314 0 : gimple_debug_bind_reset_value (stmt);
1315 0 : update_stmt (stmt);
1316 0 : return;
1317 : }
1318 0 : SET_USE (use_p, def);
1319 : }
1320 0 : if (update)
1321 0 : update_stmt (stmt);
1322 : }
1323 :
1324 : /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1325 : the block with its immediate reaching definitions. Update the current
1326 : definition of a variable when a new real or virtual definition is found. */
1327 :
1328 : static void
1329 69818445 : rewrite_stmt (gimple_stmt_iterator *si)
1330 : {
1331 69818445 : use_operand_p use_p;
1332 69818445 : def_operand_p def_p;
1333 69818445 : ssa_op_iter iter;
1334 69818445 : gimple *stmt = gsi_stmt (*si);
1335 :
1336 : /* If mark_def_sites decided that we don't need to rewrite this
1337 : statement, ignore it. */
1338 69818445 : gcc_assert (blocks_to_update == NULL);
1339 69818445 : if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1340 15315323 : return;
1341 :
1342 54503122 : if (dump_file && (dump_flags & TDF_DETAILS))
1343 : {
1344 4 : fprintf (dump_file, "Renaming statement ");
1345 4 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1346 4 : fprintf (dump_file, "\n");
1347 : }
1348 :
1349 : /* Step 1. Rewrite USES in the statement. */
1350 54503122 : if (rewrite_uses_p (stmt))
1351 : {
1352 49741514 : if (is_gimple_debug (stmt))
1353 0 : rewrite_debug_stmt_uses (stmt);
1354 : else
1355 123264649 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1356 : {
1357 73523135 : tree var = USE_FROM_PTR (use_p);
1358 73523135 : if (TREE_CODE (var) == SSA_NAME)
1359 10003644 : continue;
1360 63519491 : gcc_checking_assert (DECL_P (var));
1361 63519491 : SET_USE (use_p, get_reaching_def (var));
1362 : }
1363 : }
1364 :
1365 : /* Step 2. Register the statement's DEF operands. */
1366 54503122 : if (register_defs_p (stmt))
1367 72845098 : FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1368 : {
1369 38027772 : tree var = DEF_FROM_PTR (def_p);
1370 38027772 : tree name;
1371 38027772 : tree tracked_var;
1372 :
1373 38027772 : if (TREE_CODE (var) == SSA_NAME)
1374 1264007 : continue;
1375 36763765 : gcc_checking_assert (DECL_P (var));
1376 :
1377 36763765 : if (gimple_clobber_p (stmt)
1378 36763765 : && is_gimple_reg (var))
1379 : {
1380 : /* If we rewrite a DECL into SSA form then drop its
1381 : clobber stmts and replace uses with a new default def. */
1382 146212 : gcc_checking_assert (VAR_P (var) && !gimple_vdef (stmt));
1383 73106 : gsi_replace (si, gimple_build_nop (), true);
1384 73106 : register_new_def (get_or_create_ssa_default_def (cfun, var), var);
1385 73106 : break;
1386 : }
1387 :
1388 36690659 : name = make_ssa_name (var, stmt);
1389 36690659 : SET_DEF (def_p, name);
1390 36690659 : register_new_def (DEF_FROM_PTR (def_p), var);
1391 :
1392 : /* Do not insert debug stmts if the stmt ends the BB. */
1393 36690659 : if (stmt_ends_bb_p (stmt))
1394 4001273 : continue;
1395 :
1396 32689386 : tracked_var = target_for_debug_bind (var);
1397 32689386 : if (tracked_var)
1398 : {
1399 1923870 : gimple *note = gimple_build_debug_bind (tracked_var, name, stmt);
1400 1923870 : gsi_insert_after (si, note, GSI_SAME_STMT);
1401 : }
1402 : }
1403 : }
1404 :
1405 :
1406 : /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1407 : PHI nodes. For every PHI node found, add a new argument containing the
1408 : current reaching definition for the variable and the edge through which
1409 : that definition is reaching the PHI node. */
1410 :
1411 : static void
1412 20760144 : rewrite_add_phi_arguments (basic_block bb)
1413 : {
1414 20760144 : edge e;
1415 20760144 : edge_iterator ei;
1416 :
1417 47900380 : FOR_EACH_EDGE (e, ei, bb->succs)
1418 : {
1419 27140236 : gphi *phi;
1420 27140236 : gphi_iterator gsi;
1421 :
1422 39295572 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
1423 12155336 : gsi_next (&gsi))
1424 : {
1425 12155336 : tree currdef, res;
1426 12155336 : location_t loc;
1427 :
1428 12155336 : phi = gsi.phi ();
1429 12155336 : res = gimple_phi_result (phi);
1430 12155336 : currdef = get_reaching_def (SSA_NAME_VAR (res));
1431 : /* Virtual operand PHI args do not need a location. */
1432 24310672 : if (virtual_operand_p (res))
1433 : loc = UNKNOWN_LOCATION;
1434 : else
1435 4479928 : loc = gimple_location (SSA_NAME_DEF_STMT (currdef));
1436 12155336 : add_phi_arg (phi, currdef, e, loc);
1437 : }
1438 : }
1439 20760144 : }
1440 :
1441 6002400 : class rewrite_dom_walker : public dom_walker
1442 : {
1443 : public:
1444 3001200 : rewrite_dom_walker (cdi_direction direction)
1445 6002400 : : dom_walker (direction, ALL_BLOCKS, NULL) {}
1446 :
1447 : edge before_dom_children (basic_block) final override;
1448 : void after_dom_children (basic_block) final override;
1449 : };
1450 :
1451 : /* SSA Rewriting Step 1. Initialization, create a block local stack
1452 : of reaching definitions for new SSA names produced in this block
1453 : (BLOCK_DEFS). Register new definitions for every PHI node in the
1454 : block. */
1455 :
1456 : edge
1457 20760144 : rewrite_dom_walker::before_dom_children (basic_block bb)
1458 : {
1459 20760144 : if (dump_file && (dump_flags & TDF_DETAILS))
1460 6 : fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1461 :
1462 : /* Mark the unwind point for this block. */
1463 20760144 : block_defs_stack.safe_push (NULL_TREE);
1464 :
1465 : /* Step 1. Register new definitions for every PHI node in the block.
1466 : Conceptually, all the PHI nodes are executed in parallel and each PHI
1467 : node introduces a new version for the associated variable. */
1468 25261372 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1469 4501228 : gsi_next (&gsi))
1470 : {
1471 4501228 : tree result = gimple_phi_result (gsi_stmt (gsi));
1472 4501228 : register_new_def (result, SSA_NAME_VAR (result));
1473 : }
1474 :
1475 : /* Step 2. Rewrite every variable used in each statement in the block
1476 : with its immediate reaching definitions. Update the current definition
1477 : of a variable when a new real or virtual definition is found. */
1478 20760144 : if (bitmap_bit_p (interesting_blocks, bb->index))
1479 104569189 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1480 69818445 : gsi_next (&gsi))
1481 69818445 : rewrite_stmt (&gsi);
1482 :
1483 : /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1484 : For every PHI node found, add a new argument containing the current
1485 : reaching definition for the variable and the edge through which that
1486 : definition is reaching the PHI node. */
1487 20760144 : rewrite_add_phi_arguments (bb);
1488 :
1489 20760144 : return NULL;
1490 : }
1491 :
1492 :
1493 :
1494 : /* Called after visiting all the statements in basic block BB and all
1495 : of its dominator children. Restore CURRDEFS to its original value. */
1496 :
1497 : void
1498 20760144 : rewrite_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
1499 : {
1500 : /* Restore CURRDEFS to its original state. */
1501 53281213 : while (block_defs_stack.length () > 0)
1502 : {
1503 53281213 : tree tmp = block_defs_stack.pop ();
1504 53281213 : tree saved_def, var;
1505 :
1506 53281213 : if (tmp == NULL_TREE)
1507 : break;
1508 :
1509 32521069 : if (TREE_CODE (tmp) == SSA_NAME)
1510 : {
1511 : /* If we recorded an SSA_NAME, then make the SSA_NAME the
1512 : current definition of its underlying variable. Note that
1513 : if the SSA_NAME is not for a GIMPLE register, the symbol
1514 : being defined is stored in the next slot in the stack.
1515 : This mechanism is needed because an SSA name for a
1516 : non-register symbol may be the definition for more than
1517 : one symbol (e.g., SFTs, aliased variables, etc). */
1518 25875389 : saved_def = tmp;
1519 25875389 : var = SSA_NAME_VAR (saved_def);
1520 25875389 : if (!is_gimple_reg (var))
1521 21906585 : var = block_defs_stack.pop ();
1522 : }
1523 : else
1524 : {
1525 : /* If we recorded anything else, it must have been a _DECL
1526 : node and its current reaching definition must have been
1527 : NULL. */
1528 : saved_def = NULL;
1529 : var = tmp;
1530 : }
1531 :
1532 32521069 : get_common_info (var)->current_def = saved_def;
1533 : }
1534 20760144 : }
1535 :
1536 :
1537 : /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1538 :
1539 : DEBUG_FUNCTION void
1540 0 : debug_decl_set (bitmap set)
1541 : {
1542 0 : dump_decl_set (stderr, set);
1543 0 : fprintf (stderr, "\n");
1544 0 : }
1545 :
1546 :
1547 : /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1548 : stack up to a maximum of N levels. If N is -1, the whole stack is
1549 : dumped. New levels are created when the dominator tree traversal
1550 : used for renaming enters a new sub-tree. */
1551 :
1552 : void
1553 0 : dump_defs_stack (FILE *file, int n)
1554 : {
1555 0 : int i, j;
1556 :
1557 0 : fprintf (file, "\n\nRenaming stack");
1558 0 : if (n > 0)
1559 0 : fprintf (file, " (up to %d levels)", n);
1560 0 : fprintf (file, "\n\n");
1561 :
1562 0 : i = 1;
1563 0 : fprintf (file, "Level %d (current level)\n", i);
1564 0 : for (j = (int) block_defs_stack.length () - 1; j >= 0; j--)
1565 : {
1566 0 : tree name, var;
1567 :
1568 0 : name = block_defs_stack[j];
1569 0 : if (name == NULL_TREE)
1570 : {
1571 0 : i++;
1572 0 : if (n > 0 && i > n)
1573 : break;
1574 0 : fprintf (file, "\nLevel %d\n", i);
1575 0 : continue;
1576 : }
1577 :
1578 0 : if (DECL_P (name))
1579 : {
1580 : var = name;
1581 : name = NULL_TREE;
1582 : }
1583 : else
1584 : {
1585 0 : var = SSA_NAME_VAR (name);
1586 0 : if (!is_gimple_reg (var))
1587 : {
1588 0 : j--;
1589 0 : var = block_defs_stack[j];
1590 : }
1591 : }
1592 :
1593 0 : fprintf (file, " Previous CURRDEF (");
1594 0 : print_generic_expr (file, var);
1595 0 : fprintf (file, ") = ");
1596 0 : if (name)
1597 0 : print_generic_expr (file, name);
1598 : else
1599 0 : fprintf (file, "<NIL>");
1600 0 : fprintf (file, "\n");
1601 : }
1602 0 : }
1603 :
1604 :
1605 : /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1606 : stack up to a maximum of N levels. If N is -1, the whole stack is
1607 : dumped. New levels are created when the dominator tree traversal
1608 : used for renaming enters a new sub-tree. */
1609 :
1610 : DEBUG_FUNCTION void
1611 0 : debug_defs_stack (int n)
1612 : {
1613 0 : dump_defs_stack (stderr, n);
1614 0 : }
1615 :
1616 :
1617 : /* Dump the current reaching definition of every symbol to FILE. */
1618 :
1619 : void
1620 0 : dump_currdefs (FILE *file)
1621 : {
1622 0 : if (symbols_to_rename.is_empty ())
1623 : return;
1624 :
1625 0 : fprintf (file, "\n\nCurrent reaching definitions\n\n");
1626 0 : for (tree var : symbols_to_rename)
1627 : {
1628 0 : common_info *info = get_common_info (var);
1629 0 : fprintf (file, "CURRDEF (");
1630 0 : print_generic_expr (file, var);
1631 0 : fprintf (file, ") = ");
1632 0 : if (info->current_def)
1633 0 : print_generic_expr (file, info->current_def);
1634 : else
1635 0 : fprintf (file, "<NIL>");
1636 0 : fprintf (file, "\n");
1637 : }
1638 : }
1639 :
1640 :
1641 : /* Dump the current reaching definition of every symbol to stderr. */
1642 :
1643 : DEBUG_FUNCTION void
1644 0 : debug_currdefs (void)
1645 : {
1646 0 : dump_currdefs (stderr);
1647 0 : }
1648 :
1649 :
1650 : /* Dump SSA information to FILE. */
1651 :
1652 : void
1653 0 : dump_tree_ssa (FILE *file)
1654 : {
1655 0 : const char *funcname
1656 0 : = lang_hooks.decl_printable_name (current_function_decl, 2);
1657 :
1658 0 : fprintf (file, "SSA renaming information for %s\n\n", funcname);
1659 :
1660 0 : dump_var_infos (file);
1661 0 : dump_defs_stack (file, -1);
1662 0 : dump_currdefs (file);
1663 0 : dump_tree_ssa_stats (file);
1664 0 : }
1665 :
1666 :
1667 : /* Dump SSA information to stderr. */
1668 :
1669 : DEBUG_FUNCTION void
1670 0 : debug_tree_ssa (void)
1671 : {
1672 0 : dump_tree_ssa (stderr);
1673 0 : }
1674 :
1675 :
1676 : /* Dump statistics for the hash table HTAB. */
1677 :
1678 : static void
1679 230 : htab_statistics (FILE *file, const hash_table<var_info_hasher> &htab)
1680 : {
1681 460 : fprintf (file, "size " HOST_SIZE_T_PRINT_DEC ", " HOST_SIZE_T_PRINT_DEC
1682 : " elements, %f collision/search ratio\n",
1683 230 : (fmt_size_t) htab.size (),
1684 230 : (fmt_size_t) htab.elements (),
1685 : htab.collisions ());
1686 230 : }
1687 :
1688 :
1689 : /* Dump SSA statistics on FILE. */
1690 :
1691 : void
1692 230 : dump_tree_ssa_stats (FILE *file)
1693 : {
1694 230 : if (var_infos)
1695 : {
1696 230 : fprintf (file, "\nHash table statistics:\n");
1697 230 : fprintf (file, " var_infos: ");
1698 230 : htab_statistics (file, *var_infos);
1699 230 : fprintf (file, "\n");
1700 : }
1701 230 : }
1702 :
1703 :
1704 : /* Dump SSA statistics on stderr. */
1705 :
1706 : DEBUG_FUNCTION void
1707 0 : debug_tree_ssa_stats (void)
1708 : {
1709 0 : dump_tree_ssa_stats (stderr);
1710 0 : }
1711 :
1712 :
1713 : /* Callback for htab_traverse to dump the VAR_INFOS hash table. */
1714 :
1715 : int
1716 0 : debug_var_infos_r (var_info **slot, FILE *file)
1717 : {
1718 0 : var_info *info = *slot;
1719 :
1720 0 : fprintf (file, "VAR: ");
1721 0 : print_generic_expr (file, info->var, dump_flags);
1722 0 : bitmap_print (file, info->info.def_blocks.def_blocks,
1723 : ", DEF_BLOCKS: { ", "}");
1724 0 : bitmap_print (file, info->info.def_blocks.livein_blocks,
1725 : ", LIVEIN_BLOCKS: { ", "}");
1726 0 : bitmap_print (file, info->info.def_blocks.phi_blocks,
1727 : ", PHI_BLOCKS: { ", "}\n");
1728 :
1729 0 : return 1;
1730 : }
1731 :
1732 :
1733 : /* Dump the VAR_INFOS hash table on FILE. */
1734 :
1735 : void
1736 0 : dump_var_infos (FILE *file)
1737 : {
1738 0 : fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
1739 0 : if (var_infos)
1740 0 : var_infos->traverse <FILE *, debug_var_infos_r> (file);
1741 0 : }
1742 :
1743 :
1744 : /* Dump the VAR_INFOS hash table on stderr. */
1745 :
1746 : DEBUG_FUNCTION void
1747 0 : debug_var_infos (void)
1748 : {
1749 0 : dump_var_infos (stderr);
1750 0 : }
1751 :
1752 :
1753 : /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1754 :
1755 : static inline void
1756 91979060 : register_new_update_single (tree new_name, tree old_name)
1757 : {
1758 91979060 : common_info *info = get_common_info (old_name);
1759 91979060 : tree currdef = info->current_def;
1760 :
1761 : /* Push the current reaching definition into BLOCK_DEFS_STACK.
1762 : This stack is later used by the dominator tree callbacks to
1763 : restore the reaching definitions for all the variables
1764 : defined in the block after a recursive visit to all its
1765 : immediately dominated blocks. */
1766 91979060 : block_defs_stack.reserve (2);
1767 91979060 : block_defs_stack.quick_push (currdef);
1768 91979060 : block_defs_stack.quick_push (old_name);
1769 :
1770 : /* Set the current reaching definition for OLD_NAME to be
1771 : NEW_NAME. */
1772 91979060 : info->current_def = new_name;
1773 91979060 : }
1774 :
1775 :
1776 : /* Register NEW_NAME to be the new reaching definition for all the
1777 : names in OLD_NAMES. Used by the incremental SSA update routines to
1778 : replace old SSA names with new ones. */
1779 :
1780 : static inline void
1781 17620475 : register_new_update_set (tree new_name, bitmap old_names)
1782 : {
1783 17620475 : bitmap_iterator bi;
1784 17620475 : unsigned i;
1785 :
1786 35370959 : EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1787 17750484 : register_new_update_single (new_name, ssa_name (i));
1788 17620475 : }
1789 :
1790 :
1791 :
1792 : /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1793 : it is a symbol marked for renaming, replace it with USE_P's current
1794 : reaching definition. */
1795 :
1796 : static inline void
1797 162060825 : maybe_replace_use (use_operand_p use_p)
1798 : {
1799 162060825 : tree rdef = NULL_TREE;
1800 162060825 : tree use = USE_FROM_PTR (use_p);
1801 292455227 : tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1802 :
1803 162060825 : if (marked_for_renaming (sym))
1804 82524502 : rdef = get_reaching_def (sym);
1805 79536323 : else if (is_old_name (use))
1806 26027620 : rdef = get_reaching_def (use);
1807 :
1808 162060825 : if (rdef && rdef != use)
1809 50803680 : SET_USE (use_p, rdef);
1810 162060825 : }
1811 :
1812 :
1813 : /* Same as maybe_replace_use, but without introducing default stmts,
1814 : returning false to indicate a need to do so. */
1815 :
1816 : static inline bool
1817 6506248 : maybe_replace_use_in_debug_stmt (use_operand_p use_p)
1818 : {
1819 6506248 : tree rdef = NULL_TREE;
1820 6506248 : tree use = USE_FROM_PTR (use_p);
1821 12835335 : tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1822 :
1823 6506248 : if (marked_for_renaming (sym))
1824 177161 : rdef = get_var_info (sym)->info.current_def;
1825 6329087 : else if (is_old_name (use))
1826 : {
1827 6287890 : rdef = get_ssa_name_ann (use)->info.current_def;
1828 : /* We can't assume that, if there's no current definition, the
1829 : default one should be used. It could be the case that we've
1830 : rearranged blocks so that the earlier definition no longer
1831 : dominates the use. */
1832 6316138 : if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use))
1833 : rdef = use;
1834 : }
1835 : else
1836 : rdef = use;
1837 :
1838 6506248 : if (rdef && rdef != use)
1839 3969484 : SET_USE (use_p, rdef);
1840 :
1841 6506248 : return rdef != NULL_TREE;
1842 : }
1843 :
1844 :
1845 : /* If DEF has x_5 = ASAN_POISON () as its current def, add
1846 : ASAN_POISON_USE (x_5) stmt before GSI to denote the stmt writes into
1847 : a poisoned (out of scope) variable. */
1848 :
1849 : static void
1850 71218 : maybe_add_asan_poison_write (tree def, gimple_stmt_iterator *gsi)
1851 : {
1852 71218 : tree cdef = get_current_def (def);
1853 71218 : if (cdef != NULL
1854 58679 : && TREE_CODE (cdef) == SSA_NAME
1855 129897 : && gimple_call_internal_p (SSA_NAME_DEF_STMT (cdef), IFN_ASAN_POISON))
1856 : {
1857 9 : gcall *call
1858 9 : = gimple_build_call_internal (IFN_ASAN_POISON_USE, 1, cdef);
1859 9 : gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
1860 9 : gsi_insert_before (gsi, call, GSI_SAME_STMT);
1861 : }
1862 71218 : }
1863 :
1864 :
1865 : /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1866 : or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1867 : register it as the current definition for the names replaced by
1868 : DEF_P. Returns whether the statement should be removed. */
1869 :
1870 : static inline bool
1871 70287124 : maybe_register_def (def_operand_p def_p, gimple *stmt,
1872 : gimple_stmt_iterator gsi)
1873 : {
1874 70287124 : tree def = DEF_FROM_PTR (def_p);
1875 117207810 : tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1876 70287124 : bool to_delete = false;
1877 :
1878 : /* If DEF is a naked symbol that needs renaming, create a new
1879 : name for it. */
1880 70287124 : if (marked_for_renaming (sym))
1881 : {
1882 57589605 : if (DECL_P (def))
1883 : {
1884 23366438 : if (gimple_clobber_p (stmt) && is_gimple_reg (sym))
1885 : {
1886 2082120 : tree defvar;
1887 2082120 : if (VAR_P (sym))
1888 : defvar = sym;
1889 : else
1890 7 : defvar = create_tmp_reg (TREE_TYPE (sym));
1891 : /* Replace clobber stmts with a default def. This new use of a
1892 : default definition may make it look like SSA_NAMEs have
1893 : conflicting lifetimes, so we need special code to let them
1894 : coalesce properly. */
1895 2082120 : to_delete = true;
1896 2082120 : def = get_or_create_ssa_default_def (cfun, defvar);
1897 : }
1898 : else
1899 : {
1900 21284318 : if (asan_sanitize_use_after_scope ())
1901 71218 : maybe_add_asan_poison_write (def, &gsi);
1902 21284318 : def = make_ssa_name (def, stmt);
1903 : }
1904 23366438 : SET_DEF (def_p, def);
1905 :
1906 23366438 : tree tracked_var = target_for_debug_bind (sym);
1907 23366438 : if (tracked_var)
1908 : {
1909 : /* If stmt ends the bb, insert the debug stmt on the non-EH
1910 : edge(s) from the stmt. */
1911 3262779 : if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt))
1912 : {
1913 304 : basic_block bb = gsi_bb (gsi);
1914 304 : edge_iterator ei;
1915 304 : edge e, ef = NULL;
1916 912 : FOR_EACH_EDGE (e, ei, bb->succs)
1917 608 : if (!(e->flags & EDGE_EH))
1918 : {
1919 : /* asm goto can have multiple non-EH edges from the
1920 : stmt. Insert on all of them where it is
1921 : possible. */
1922 305 : gcc_checking_assert (!ef || (gimple_code (stmt)
1923 : == GIMPLE_ASM));
1924 305 : ef = e;
1925 : /* If there are other predecessors to ef->dest, then
1926 : there must be PHI nodes for the modified
1927 : variable, and therefore there will be debug bind
1928 : stmts after the PHI nodes. The debug bind notes
1929 : we'd insert would force the creation of a new
1930 : block (diverging codegen) and be redundant with
1931 : the post-PHI bind stmts, so don't add them.
1932 :
1933 : As for the exit edge, there wouldn't be redundant
1934 : bind stmts, but there wouldn't be a PC to bind
1935 : them to either, so avoid diverging the CFG. */
1936 305 : if (e
1937 880 : && single_pred_p (e->dest)
1938 272 : && gimple_seq_empty_p (phi_nodes (e->dest))
1939 262 : && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1940 : {
1941 : /* If there were PHI nodes in the node, we'd
1942 : have to make sure the value we're binding
1943 : doesn't need rewriting. But there shouldn't
1944 : be PHI nodes in a single-predecessor block,
1945 : so we just add the note. */
1946 262 : gimple *note
1947 262 : = gimple_build_debug_bind (tracked_var, def,
1948 : stmt);
1949 262 : gsi_insert_on_edge_immediate (ef, note);
1950 : }
1951 : }
1952 : }
1953 : else
1954 : {
1955 3078174 : gimple *note
1956 3078174 : = gimple_build_debug_bind (tracked_var, def, stmt);
1957 3078174 : gsi_insert_after (&gsi, note, GSI_SAME_STMT);
1958 : }
1959 : }
1960 : }
1961 :
1962 57589605 : register_new_update_single (def, sym);
1963 : }
1964 : else
1965 : {
1966 : /* If DEF is a new name, register it as a new definition
1967 : for all the names replaced by DEF. */
1968 12697519 : if (is_new_name (def))
1969 5288768 : register_new_update_set (def, names_replaced_by (def));
1970 :
1971 : /* If DEF is an old name, register DEF as a new
1972 : definition for itself. */
1973 12697519 : if (is_old_name (def))
1974 4050329 : register_new_update_single (def, def);
1975 : }
1976 :
1977 70287124 : return to_delete;
1978 : }
1979 :
1980 :
1981 : /* Update every variable used in the statement pointed-to by SI. The
1982 : statement is assumed to be in SSA form already. Names in
1983 : OLD_SSA_NAMES used by SI will be updated to their current reaching
1984 : definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1985 : will be registered as a new definition for their corresponding name
1986 : in OLD_SSA_NAMES. Returns whether STMT should be removed. */
1987 :
1988 : static bool
1989 566980792 : rewrite_update_stmt (gimple *stmt, gimple_stmt_iterator gsi)
1990 : {
1991 566980792 : use_operand_p use_p;
1992 566980792 : def_operand_p def_p;
1993 566980792 : ssa_op_iter iter;
1994 :
1995 : /* Only update marked statements. */
1996 566980792 : if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1997 : return false;
1998 :
1999 116800257 : if (dump_file && (dump_flags & TDF_DETAILS))
2000 : {
2001 53109 : fprintf (dump_file, "Updating SSA information for statement ");
2002 53109 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2003 : }
2004 :
2005 : /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
2006 : symbol is marked for renaming. */
2007 116800257 : if (rewrite_uses_p (stmt))
2008 : {
2009 108670473 : if (is_gimple_debug (stmt))
2010 : {
2011 6443756 : bool failed = false;
2012 :
2013 12921546 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
2014 6506248 : if (!maybe_replace_use_in_debug_stmt (use_p))
2015 : {
2016 : failed = true;
2017 : break;
2018 : }
2019 :
2020 6443756 : if (failed)
2021 : {
2022 : /* DOM sometimes threads jumps in such a way that a
2023 : debug stmt ends up referencing a SSA variable that no
2024 : longer dominates the debug stmt, but such that all
2025 : incoming definitions refer to the same definition in
2026 : an earlier dominator. We could try to recover that
2027 : definition somehow, but this will have to do for now.
2028 :
2029 : Introducing a default definition, which is what
2030 : maybe_replace_use() would do in such cases, may
2031 : modify code generation, for the otherwise-unused
2032 : default definition would never go away, modifying SSA
2033 : version numbers all over. */
2034 28458 : gimple_debug_bind_reset_value (stmt);
2035 28458 : update_stmt (stmt);
2036 : }
2037 : }
2038 : else
2039 : {
2040 264287542 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
2041 162060825 : maybe_replace_use (use_p);
2042 : }
2043 : }
2044 :
2045 : /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
2046 : Also register definitions for names whose underlying symbol is
2047 : marked for renaming. */
2048 116800257 : bool to_delete = false;
2049 116800257 : if (register_defs_p (stmt))
2050 137072053 : FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
2051 70287124 : to_delete |= maybe_register_def (def_p, stmt, gsi);
2052 :
2053 : return to_delete;
2054 : }
2055 :
2056 :
2057 : /* Visit all the successor blocks of BB looking for PHI nodes. For
2058 : every PHI node found, check if any of its arguments is in
2059 : OLD_SSA_NAMES. If so, and if the argument has a current reaching
2060 : definition, replace it. */
2061 :
2062 : static void
2063 81889523 : rewrite_update_phi_arguments (basic_block bb)
2064 : {
2065 81889523 : edge e;
2066 81889523 : edge_iterator ei;
2067 :
2068 193662294 : FOR_EACH_EDGE (e, ei, bb->succs)
2069 : {
2070 111772771 : if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
2071 77180911 : continue;
2072 :
2073 34591860 : for (auto gsi = gsi_start_phis (e->dest);
2074 98417457 : !gsi_end_p (gsi); gsi_next(&gsi))
2075 : {
2076 63825597 : tree arg, lhs_sym, reaching_def = NULL;
2077 63825597 : use_operand_p arg_p;
2078 63825597 : gphi *phi = *gsi;
2079 63825597 : if (!rewrite_uses_p (*gsi))
2080 16937980 : continue;
2081 :
2082 46887617 : arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
2083 46887617 : arg = USE_FROM_PTR (arg_p);
2084 :
2085 46887617 : if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
2086 403669 : continue;
2087 :
2088 46483948 : lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
2089 :
2090 46483948 : if (arg == NULL_TREE)
2091 : {
2092 : /* When updating a PHI node for a recently introduced
2093 : symbol we may find NULL arguments. That's why we
2094 : take the symbol from the LHS of the PHI node. */
2095 5679012 : reaching_def = get_reaching_def (lhs_sym);
2096 : }
2097 : else
2098 : {
2099 81572372 : tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
2100 :
2101 40804936 : if (marked_for_renaming (sym))
2102 16681988 : reaching_def = get_reaching_def (sym);
2103 24122948 : else if (is_old_name (arg))
2104 20974599 : reaching_def = get_reaching_def (arg);
2105 : }
2106 :
2107 : /* Update the argument if there is a reaching def different
2108 : from arg. */
2109 46483948 : if (reaching_def && reaching_def != arg)
2110 : {
2111 17609891 : location_t locus;
2112 17609891 : int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
2113 :
2114 17609891 : SET_USE (arg_p, reaching_def);
2115 :
2116 : /* Virtual operands do not need a location. */
2117 35219782 : if (virtual_operand_p (reaching_def))
2118 : locus = UNKNOWN_LOCATION;
2119 : /* If SSA update didn't insert this PHI the argument
2120 : might have a location already, keep that. */
2121 8350143 : else if (gimple_phi_arg_has_location (phi, arg_i))
2122 : locus = gimple_phi_arg_location (phi, arg_i);
2123 : else
2124 : {
2125 7525307 : gimple *stmt = SSA_NAME_DEF_STMT (reaching_def);
2126 7525307 : gphi *other_phi = dyn_cast <gphi *> (stmt);
2127 :
2128 : /* Single element PHI nodes behave like copies, so get the
2129 : location from the phi argument. */
2130 5450566 : if (other_phi
2131 5450566 : && gimple_phi_num_args (other_phi) == 1)
2132 2945065 : locus = gimple_phi_arg_location (other_phi, 0);
2133 : else
2134 4580242 : locus = gimple_location (stmt);
2135 : }
2136 :
2137 17609891 : gimple_phi_arg_set_location (phi, arg_i, locus);
2138 : }
2139 :
2140 46483948 : if (e->flags & EDGE_ABNORMAL)
2141 8349 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2142 : }
2143 : }
2144 81889523 : }
2145 :
2146 3941082 : class rewrite_update_dom_walker : public dom_walker
2147 : {
2148 : public:
2149 3941082 : rewrite_update_dom_walker (cdi_direction direction, int in_region_flag = -1)
2150 : : dom_walker (direction, ALL_BLOCKS, (int *)(uintptr_t)-1),
2151 623905 : m_in_region_flag (in_region_flag) {}
2152 :
2153 : edge before_dom_children (basic_block) final override;
2154 : void after_dom_children (basic_block) final override;
2155 :
2156 : int m_in_region_flag;
2157 : };
2158 :
2159 : /* Initialization of block data structures for the incremental SSA
2160 : update pass. Create a block local stack of reaching definitions
2161 : for new SSA names produced in this block (BLOCK_DEFS). Register
2162 : new definitions for every PHI node in the block. */
2163 :
2164 : edge
2165 108701418 : rewrite_update_dom_walker::before_dom_children (basic_block bb)
2166 : {
2167 108701418 : bool is_abnormal_phi;
2168 :
2169 108701418 : if (dump_file && (dump_flags & TDF_DETAILS))
2170 34952 : fprintf (dump_file, "Registering new PHI nodes in block #%d\n",
2171 : bb->index);
2172 :
2173 : /* Mark the unwind point for this block. */
2174 108701418 : block_defs_stack.safe_push (NULL_TREE);
2175 :
2176 108701418 : if (m_in_region_flag != -1
2177 10892749 : && !(bb->flags & m_in_region_flag))
2178 1187008 : return STOP;
2179 :
2180 107514410 : if (!bitmap_bit_p (blocks_to_update, bb->index))
2181 : return NULL;
2182 :
2183 : /* Mark the LHS if any of the arguments flows through an abnormal
2184 : edge. */
2185 81889523 : is_abnormal_phi = bb_has_abnormal_pred (bb);
2186 :
2187 : /* If any of the PHI nodes is a replacement for a name in
2188 : OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2189 : register it as a new definition for its corresponding name. Also
2190 : register definitions for names whose underlying symbols are
2191 : marked for renaming. */
2192 123558684 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2193 41669161 : gsi_next (&gsi))
2194 : {
2195 41669161 : tree lhs, lhs_sym;
2196 41669161 : gphi *phi = gsi.phi ();
2197 :
2198 41669161 : if (!register_defs_p (phi))
2199 16802098 : continue;
2200 :
2201 24867063 : lhs = gimple_phi_result (phi);
2202 24867063 : lhs_sym = SSA_NAME_VAR (lhs);
2203 :
2204 24867063 : if (marked_for_renaming (lhs_sym))
2205 7885444 : register_new_update_single (lhs, lhs_sym);
2206 : else
2207 : {
2208 :
2209 : /* If LHS is a new name, register a new definition for all
2210 : the names replaced by LHS. */
2211 16981619 : if (is_new_name (lhs))
2212 12331707 : register_new_update_set (lhs, names_replaced_by (lhs));
2213 :
2214 : /* If LHS is an OLD name, register it as a new definition
2215 : for itself. */
2216 16981619 : if (is_old_name (lhs))
2217 4703198 : register_new_update_single (lhs, lhs);
2218 : }
2219 :
2220 24867063 : if (is_abnormal_phi)
2221 3013 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
2222 : }
2223 :
2224 : /* Step 2. Rewrite every variable used in each statement in the block. */
2225 730759838 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2226 566980792 : if (rewrite_update_stmt (gsi_stmt (gsi), gsi))
2227 2082120 : gsi_remove (&gsi, true);
2228 : else
2229 564898672 : gsi_next (&gsi);
2230 :
2231 : /* Step 3. Update PHI nodes. */
2232 81889523 : rewrite_update_phi_arguments (bb);
2233 :
2234 81889523 : return NULL;
2235 : }
2236 :
2237 : /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2238 : the current reaching definition of every name re-written in BB to
2239 : the original reaching definition before visiting BB. This
2240 : unwinding must be done in the opposite order to what is done in
2241 : register_new_update_set. */
2242 :
2243 : void
2244 108701418 : rewrite_update_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
2245 : {
2246 200680478 : while (block_defs_stack.length () > 0)
2247 : {
2248 200680478 : tree var = block_defs_stack.pop ();
2249 200680478 : tree saved_def;
2250 :
2251 : /* NULL indicates the unwind stop point for this block (see
2252 : rewrite_update_enter_block). */
2253 200680478 : if (var == NULL)
2254 : return;
2255 :
2256 91979060 : saved_def = block_defs_stack.pop ();
2257 91979060 : get_common_info (var)->current_def = saved_def;
2258 : }
2259 : }
2260 :
2261 :
2262 : /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2263 : form.
2264 :
2265 : ENTRY indicates the block where to start. Every block dominated by
2266 : ENTRY will be rewritten.
2267 :
2268 : WHAT indicates what actions will be taken by the renamer (see enum
2269 : rewrite_mode).
2270 :
2271 : REGION is a SEME region of interesting blocks for the dominator walker
2272 : to process. If this set is invalid, then all the nodes dominated
2273 : by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2274 : are not present in BLOCKS are ignored. */
2275 :
2276 : static void
2277 6942282 : rewrite_blocks (basic_block entry, enum rewrite_mode what)
2278 : {
2279 6942282 : block_defs_stack.create (10);
2280 :
2281 : /* Recursively walk the dominator tree rewriting each statement in
2282 : each basic block. */
2283 6942282 : if (what == REWRITE_ALL)
2284 3001200 : rewrite_dom_walker (CDI_DOMINATORS).walk (entry);
2285 3941082 : else if (what == REWRITE_UPDATE)
2286 3317177 : rewrite_update_dom_walker (CDI_DOMINATORS).walk (entry);
2287 623905 : else if (what == REWRITE_UPDATE_REGION)
2288 : {
2289 : /* First mark all blocks in the SEME region dominated by
2290 : entry and exited by blocks not backwards reachable from
2291 : blocks_to_update. Optimize for dense blocks_to_update
2292 : so instead of seeding the worklist with a copy of
2293 : blocks_to_update treat those blocks explicit. */
2294 623905 : auto_bb_flag in_region (cfun);
2295 623905 : auto_vec<basic_block, 64> extra_rgn;
2296 623905 : bitmap_iterator bi;
2297 623905 : unsigned int idx;
2298 6360045 : EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, idx, bi)
2299 : {
2300 5736140 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2301 5736140 : bb->flags |= in_region;
2302 : }
2303 623905 : auto_bitmap worklist;
2304 6360045 : EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, idx, bi)
2305 : {
2306 5736140 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2307 5736140 : if (bb != entry)
2308 : {
2309 5265301 : edge_iterator ei;
2310 5265301 : edge e;
2311 12862203 : FOR_EACH_EDGE (e, ei, bb->preds)
2312 : {
2313 7596902 : if ((e->src->flags & in_region)
2314 7596902 : || dominated_by_p (CDI_DOMINATORS, e->src, bb))
2315 5982641 : continue;
2316 1614261 : bitmap_set_bit (worklist, e->src->index);
2317 : }
2318 : }
2319 : }
2320 4593506 : while (!bitmap_empty_p (worklist))
2321 : {
2322 3969601 : int idx = bitmap_clear_first_set_bit (worklist);
2323 3969601 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2324 3969601 : bb->flags |= in_region;
2325 3969601 : extra_rgn.safe_push (bb);
2326 3969601 : if (bb != entry)
2327 : {
2328 3816535 : edge_iterator ei;
2329 3816535 : edge e;
2330 8733850 : FOR_EACH_EDGE (e, ei, bb->preds)
2331 : {
2332 7381991 : if ((e->src->flags & in_region)
2333 4917315 : || dominated_by_p (CDI_DOMINATORS, e->src, bb))
2334 2464676 : continue;
2335 2452639 : bitmap_set_bit (worklist, e->src->index);
2336 : }
2337 : }
2338 : }
2339 623905 : rewrite_update_dom_walker (CDI_DOMINATORS, in_region).walk (entry);
2340 6360045 : EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, idx, bi)
2341 : {
2342 5736140 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2343 5736140 : bb->flags &= ~in_region;
2344 : }
2345 5841316 : for (auto bb : extra_rgn)
2346 3969601 : bb->flags &= ~in_region;
2347 623905 : }
2348 : else
2349 0 : gcc_unreachable ();
2350 :
2351 : /* Debugging dumps. */
2352 6942282 : if (dump_file && (dump_flags & TDF_STATS))
2353 : {
2354 492 : dump_dfa_stats (dump_file);
2355 492 : if (var_infos)
2356 230 : dump_tree_ssa_stats (dump_file);
2357 : }
2358 :
2359 6942282 : block_defs_stack.release ();
2360 6942282 : }
2361 :
2362 : class mark_def_dom_walker : public dom_walker
2363 : {
2364 : public:
2365 : mark_def_dom_walker (cdi_direction direction);
2366 : ~mark_def_dom_walker ();
2367 :
2368 : edge before_dom_children (basic_block) final override;
2369 :
2370 : private:
2371 : /* Notice that this bitmap is indexed using variable UIDs, so it must be
2372 : large enough to accommodate all the variables referenced in the
2373 : function, not just the ones we are renaming. */
2374 : bitmap m_kills;
2375 : };
2376 :
2377 3001200 : mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction)
2378 3001200 : : dom_walker (direction, ALL_BLOCKS, NULL), m_kills (BITMAP_ALLOC (NULL))
2379 : {
2380 3001200 : }
2381 :
2382 3001200 : mark_def_dom_walker::~mark_def_dom_walker ()
2383 : {
2384 3001200 : BITMAP_FREE (m_kills);
2385 3001200 : }
2386 :
2387 : /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2388 : at the start of each block, and call mark_def_sites for each statement. */
2389 :
2390 : edge
2391 20760144 : mark_def_dom_walker::before_dom_children (basic_block bb)
2392 : {
2393 20760144 : gimple_stmt_iterator gsi;
2394 :
2395 20760144 : bitmap_clear (m_kills);
2396 109319116 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2397 67798828 : mark_def_sites (bb, gsi_stmt (gsi), m_kills);
2398 20760144 : return NULL;
2399 : }
2400 :
2401 : /* Initialize internal data needed during renaming. */
2402 :
2403 : static void
2404 3001200 : init_ssa_renamer (void)
2405 : {
2406 3001200 : cfun->gimple_df->in_ssa_p = false;
2407 :
2408 : /* Allocate memory for the DEF_BLOCKS hash table. */
2409 3001200 : gcc_assert (!var_infos);
2410 6002400 : var_infos = new hash_table<var_info_hasher>
2411 5244814 : (vec_safe_length (cfun->local_decls));
2412 :
2413 3001200 : bitmap_obstack_initialize (&update_ssa_obstack);
2414 3001200 : }
2415 :
2416 :
2417 : /* Deallocate internal data structures used by the renamer. */
2418 :
2419 : static void
2420 6977203 : fini_ssa_renamer (void)
2421 : {
2422 6977203 : delete var_infos;
2423 6977203 : var_infos = NULL;
2424 :
2425 6977203 : bitmap_obstack_release (&update_ssa_obstack);
2426 :
2427 6977203 : cfun->gimple_df->ssa_renaming_needed = 0;
2428 6977203 : cfun->gimple_df->rename_vops = 0;
2429 6977203 : cfun->gimple_df->in_ssa_p = true;
2430 6977203 : }
2431 :
2432 : /* Main entry point into the SSA builder. The renaming process
2433 : proceeds in four main phases:
2434 :
2435 : 1- Compute dominance frontier and immediate dominators, needed to
2436 : insert PHI nodes and rename the function in dominator tree
2437 : order.
2438 :
2439 : 2- Find and mark all the blocks that define variables.
2440 :
2441 : 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2442 :
2443 : 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2444 :
2445 : Steps 3 and 4 are done using the dominator tree walker
2446 : (walk_dominator_tree). */
2447 :
2448 : namespace {
2449 :
2450 : const pass_data pass_data_build_ssa =
2451 : {
2452 : GIMPLE_PASS, /* type */
2453 : "ssa", /* name */
2454 : OPTGROUP_NONE, /* optinfo_flags */
2455 : TV_TREE_INTO_SSA, /* tv_id */
2456 : PROP_cfg, /* properties_required */
2457 : PROP_ssa, /* properties_provided */
2458 : 0, /* properties_destroyed */
2459 : 0, /* todo_flags_start */
2460 : TODO_remove_unused_locals, /* todo_flags_finish */
2461 : };
2462 :
2463 : class pass_build_ssa : public gimple_opt_pass
2464 : {
2465 : public:
2466 294599 : pass_build_ssa (gcc::context *ctxt)
2467 589198 : : gimple_opt_pass (pass_data_build_ssa, ctxt)
2468 : {}
2469 :
2470 : /* opt_pass methods: */
2471 3001629 : bool gate (function *fun) final override
2472 : {
2473 : /* Do nothing for functions that were produced already in SSA form. */
2474 3001629 : return !(fun->curr_properties & PROP_ssa);
2475 : }
2476 :
2477 : unsigned int execute (function *) final override;
2478 :
2479 : }; // class pass_build_ssa
2480 :
2481 : unsigned int
2482 3001200 : pass_build_ssa::execute (function *fun)
2483 : {
2484 3001200 : bitmap_head *dfs;
2485 3001200 : basic_block bb;
2486 :
2487 : /* Increase the set of variables we can rewrite into SSA form
2488 : by clearing TREE_ADDRESSABLE and transform the IL to support this. */
2489 3001200 : if (optimize)
2490 2544735 : execute_update_addresses_taken ();
2491 :
2492 : /* Initialize operand data structures. */
2493 3001200 : init_ssa_operands (fun);
2494 :
2495 : /* Initialize internal data needed by the renamer. */
2496 3001200 : init_ssa_renamer ();
2497 :
2498 : /* Initialize the set of interesting blocks. The callback
2499 : mark_def_sites will add to this set those blocks that the renamer
2500 : should process. */
2501 3001200 : interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
2502 3001200 : bitmap_clear (interesting_blocks);
2503 :
2504 : /* Initialize dominance frontier. */
2505 3001200 : dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (fun));
2506 20760144 : FOR_EACH_BB_FN (bb, fun)
2507 17758944 : bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
2508 :
2509 : /* 1- Compute dominance frontiers. */
2510 3001200 : calculate_dominance_info (CDI_DOMINATORS);
2511 3001200 : compute_dominance_frontiers (dfs);
2512 :
2513 : /* 2- Find and mark definition sites. */
2514 3001200 : mark_def_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
2515 :
2516 : /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2517 3001200 : insert_phi_nodes (dfs);
2518 :
2519 : /* 4- Rename all the blocks. */
2520 3001200 : rewrite_blocks (ENTRY_BLOCK_PTR_FOR_FN (fun), REWRITE_ALL);
2521 :
2522 : /* Free allocated memory. */
2523 20760144 : FOR_EACH_BB_FN (bb, fun)
2524 17758944 : bitmap_clear (&dfs[bb->index]);
2525 3001200 : free (dfs);
2526 :
2527 3001200 : sbitmap_free (interesting_blocks);
2528 3001200 : interesting_blocks = NULL;
2529 :
2530 3001200 : fini_ssa_renamer ();
2531 :
2532 : /* Try to get rid of all gimplifier generated temporaries by making
2533 : its SSA names anonymous. This way we can garbage collect them
2534 : all after removing unused locals which we do in our TODO. */
2535 3001200 : unsigned i;
2536 3001200 : tree name;
2537 :
2538 75469566 : FOR_EACH_SSA_NAME (i, name, cfun)
2539 : {
2540 69443151 : if (SSA_NAME_IS_DEFAULT_DEF (name))
2541 7525716 : continue;
2542 61917435 : tree decl = SSA_NAME_VAR (name);
2543 41191930 : if (decl
2544 41191930 : && VAR_P (decl)
2545 40876620 : && !VAR_DECL_IS_VIRTUAL_OPERAND (decl)
2546 15910898 : && DECL_IGNORED_P (decl))
2547 22826692 : SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl));
2548 : }
2549 :
2550 : /* Initialize SSA_NAME_POINTS_TO_READONLY_MEMORY. */
2551 3001200 : tree fnspec_tree
2552 3001200 : = lookup_attribute ("fn spec",
2553 3001200 : TYPE_ATTRIBUTES (TREE_TYPE (fun->decl)));
2554 3001200 : if (fnspec_tree)
2555 : {
2556 87860 : attr_fnspec fnspec (TREE_VALUE (TREE_VALUE (fnspec_tree)));
2557 87860 : unsigned i = 0;
2558 87860 : for (tree arg = DECL_ARGUMENTS (cfun->decl);
2559 197827 : arg; arg = DECL_CHAIN (arg), ++i)
2560 : {
2561 117385 : if (!fnspec.arg_specified_p (i))
2562 : break;
2563 109967 : if (fnspec.arg_readonly_p (i))
2564 : {
2565 31692 : tree name = ssa_default_def (fun, arg);
2566 31692 : if (name)
2567 24150 : SSA_NAME_POINTS_TO_READONLY_MEMORY (name) = 1;
2568 : }
2569 : }
2570 : }
2571 :
2572 3001200 : return 0;
2573 : }
2574 :
2575 : } // anon namespace
2576 :
2577 : gimple_opt_pass *
2578 294599 : make_pass_build_ssa (gcc::context *ctxt)
2579 : {
2580 294599 : return new pass_build_ssa (ctxt);
2581 : }
2582 :
2583 :
2584 : /* Mark the definition of VAR at STMT and BB as interesting for the
2585 : renamer. BLOCKS is the set of blocks that need updating. */
2586 :
2587 : static void
2588 86491090 : mark_def_interesting (tree var, gimple *stmt, basic_block bb,
2589 : bool insert_phi_p)
2590 : {
2591 86491090 : gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index));
2592 86491090 : set_register_defs (stmt, true);
2593 :
2594 86491090 : if (insert_phi_p)
2595 : {
2596 81825028 : bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
2597 :
2598 81825028 : set_def_block (var, bb, is_phi_p);
2599 :
2600 : /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2601 : site for both itself and all the old names replaced by it. */
2602 81825028 : if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2603 : {
2604 12243782 : bitmap_iterator bi;
2605 12243782 : unsigned i;
2606 12243782 : bitmap set = names_replaced_by (var);
2607 12243782 : if (set)
2608 24617573 : EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2609 12373791 : set_def_block (ssa_name (i), bb, is_phi_p);
2610 : }
2611 : }
2612 86491090 : }
2613 :
2614 :
2615 : /* Mark the use of VAR at STMT and BB as interesting for the
2616 : renamer. INSERT_PHI_P is true if we are going to insert new PHI
2617 : nodes. */
2618 :
2619 : static inline void
2620 145458896 : mark_use_interesting (tree var, gimple *stmt, basic_block bb,
2621 : bool insert_phi_p)
2622 : {
2623 145458896 : basic_block def_bb = gimple_bb (stmt);
2624 :
2625 145458896 : mark_block_for_update (def_bb);
2626 145458896 : mark_block_for_update (bb);
2627 :
2628 145458896 : if (gimple_code (stmt) == GIMPLE_PHI)
2629 30441134 : mark_phi_for_rewrite (def_bb, as_a <gphi *> (stmt));
2630 : else
2631 : {
2632 115017762 : set_rewrite_uses (stmt, true);
2633 :
2634 115017762 : if (is_gimple_debug (stmt))
2635 : return;
2636 : }
2637 :
2638 : /* If VAR has not been defined in BB, then it is live-on-entry
2639 : to BB. Note that we cannot just use the block holding VAR's
2640 : definition because if VAR is one of the names in OLD_SSA_NAMES,
2641 : it will have several definitions (itself and all the names that
2642 : replace it). */
2643 138993547 : if (insert_phi_p)
2644 : {
2645 129574012 : def_blocks *db_p = get_def_blocks_for (get_common_info (var));
2646 129574012 : if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2647 61074886 : set_livein_block (var, bb);
2648 : }
2649 : }
2650 :
2651 : /* Processing statements in BB that reference symbols in SSA operands.
2652 : This is very similar to mark_def_sites, but the scan handles
2653 : statements whose operands may already be SSA names.
2654 :
2655 : If INSERT_PHI_P is true, mark those uses as live in the
2656 : corresponding block. This is later used by the PHI placement
2657 : algorithm to make PHI pruning decisions.
2658 :
2659 : FIXME. Most of this would be unnecessary if we could associate a
2660 : symbol to all the SSA names that reference it. But that
2661 : sounds like it would be expensive to maintain. Still, it
2662 : would be interesting to see if it makes better sense to do
2663 : that. */
2664 :
2665 : static void
2666 59959250 : prepare_block_for_update_1 (basic_block bb, bool insert_phi_p)
2667 : {
2668 59959250 : edge e;
2669 59959250 : edge_iterator ei;
2670 :
2671 59959250 : mark_block_for_update (bb);
2672 :
2673 : /* Process PHI nodes marking interesting those that define or use
2674 : the symbols that we are interested in. */
2675 74257455 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
2676 14298205 : gsi_next (&si))
2677 : {
2678 14298205 : gphi *phi = si.phi ();
2679 14298205 : tree lhs_sym, lhs = gimple_phi_result (phi);
2680 :
2681 22895251 : if (TREE_CODE (lhs) == SSA_NAME
2682 14298205 : && (! virtual_operand_p (lhs)
2683 6461281 : || ! cfun->gimple_df->rename_vops))
2684 8597046 : continue;
2685 :
2686 11402318 : lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2687 5701159 : mark_for_renaming (lhs_sym);
2688 5701159 : mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2689 :
2690 : /* Mark the uses in phi nodes as interesting. It would be more correct
2691 : to process the arguments of the phi nodes of the successor edges of
2692 : BB at the end of prepare_block_for_update, however, that turns out
2693 : to be significantly more expensive. Doing it here is conservatively
2694 : correct -- it may only cause us to believe a value to be live in a
2695 : block that also contains its definition, and thus insert a few more
2696 : phi nodes for it. */
2697 22383262 : FOR_EACH_EDGE (e, ei, bb->preds)
2698 16682103 : mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2699 : }
2700 :
2701 : /* Process the statements. */
2702 520141406 : for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
2703 400222906 : gsi_next (&si))
2704 : {
2705 400222906 : gimple *stmt;
2706 400222906 : ssa_op_iter i;
2707 400222906 : use_operand_p use_p;
2708 400222906 : def_operand_p def_p;
2709 :
2710 400222906 : stmt = gsi_stmt (si);
2711 :
2712 400222906 : if (cfun->gimple_df->rename_vops
2713 517455434 : && gimple_vuse (stmt))
2714 : {
2715 73397084 : tree use = gimple_vuse (stmt);
2716 124255163 : tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2717 73397084 : mark_for_renaming (sym);
2718 73397084 : mark_use_interesting (sym, stmt, bb, insert_phi_p);
2719 : }
2720 :
2721 568929828 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
2722 : {
2723 168706922 : tree use = USE_FROM_PTR (use_p);
2724 168706922 : if (!DECL_P (use))
2725 159402343 : continue;
2726 9304579 : mark_for_renaming (use);
2727 9304579 : mark_use_interesting (use, stmt, bb, insert_phi_p);
2728 : }
2729 :
2730 400222906 : if (cfun->gimple_df->rename_vops
2731 517455434 : && gimple_vdef (stmt))
2732 : {
2733 47914238 : tree def = gimple_vdef (stmt);
2734 82137405 : tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2735 47914238 : mark_for_renaming (sym);
2736 47914238 : mark_def_interesting (sym, stmt, bb, insert_phi_p);
2737 : }
2738 :
2739 481171803 : FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
2740 : {
2741 80948897 : tree def = DEF_FROM_PTR (def_p);
2742 80948897 : if (!DECL_P (def))
2743 71273530 : continue;
2744 9675367 : mark_for_renaming (def);
2745 9675367 : mark_def_interesting (def, stmt, bb, insert_phi_p);
2746 : }
2747 : }
2748 :
2749 59959250 : }
2750 :
2751 : /* Do a dominator walk starting at BB processing statements that
2752 : reference symbols in SSA operands. This is very similar to
2753 : mark_def_sites, but the scan handles statements whose operands may
2754 : already be SSA names.
2755 :
2756 : If INSERT_PHI_P is true, mark those uses as live in the
2757 : corresponding block. This is later used by the PHI placement
2758 : algorithm to make PHI pruning decisions.
2759 :
2760 : FIXME. Most of this would be unnecessary if we could associate a
2761 : symbol to all the SSA names that reference it. But that
2762 : sounds like it would be expensive to maintain. Still, it
2763 : would be interesting to see if it makes better sense to do
2764 : that. */
2765 : static void
2766 2542191 : prepare_block_for_update (basic_block bb, bool insert_phi_p)
2767 : {
2768 2542191 : size_t sp = 0;
2769 2542191 : basic_block *worklist;
2770 :
2771 : /* Allocate the worklist. */
2772 2542191 : worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
2773 : /* Add the BB to the worklist. */
2774 2542191 : worklist[sp++] = bb;
2775 :
2776 62501441 : while (sp)
2777 : {
2778 59959250 : basic_block bb;
2779 59959250 : basic_block son;
2780 :
2781 : /* Pick a block from the worklist. */
2782 59959250 : bb = worklist[--sp];
2783 :
2784 59959250 : prepare_block_for_update_1 (bb, insert_phi_p);
2785 :
2786 : /* Now add all the blocks dominated by BB to the worklist. */
2787 59959250 : for (son = first_dom_son (CDI_DOMINATORS, bb);
2788 117376309 : son;
2789 57417059 : son = next_dom_son (CDI_DOMINATORS, son))
2790 57417059 : worklist[sp++] = son;
2791 : }
2792 2542191 : free (worklist);
2793 2542191 : }
2794 :
2795 : /* Helper for prepare_names_to_update. Mark all the use sites for
2796 : NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2797 : prepare_names_to_update. */
2798 :
2799 : static void
2800 9550705 : prepare_use_sites_for (tree name, bool insert_phi_p)
2801 : {
2802 9550705 : use_operand_p use_p;
2803 9550705 : imm_use_iterator iter;
2804 :
2805 : /* If we rename virtual operands do not update them. */
2806 9550705 : if (virtual_operand_p (name)
2807 9550705 : && cfun->gimple_df->rename_vops)
2808 2295 : return;
2809 :
2810 55623540 : FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2811 : {
2812 46075130 : gimple *stmt = USE_STMT (use_p);
2813 46075130 : basic_block bb = gimple_bb (stmt);
2814 :
2815 46075130 : if (gimple_code (stmt) == GIMPLE_PHI)
2816 : {
2817 13759031 : int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2818 13759031 : edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), ix);
2819 13759031 : mark_use_interesting (name, stmt, e->src, insert_phi_p);
2820 : }
2821 : else
2822 : {
2823 : /* For regular statements, mark this as an interesting use
2824 : for NAME. */
2825 32316099 : mark_use_interesting (name, stmt, bb, insert_phi_p);
2826 : }
2827 9548410 : }
2828 : }
2829 :
2830 :
2831 : /* Helper for prepare_names_to_update. Mark the definition site for
2832 : NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2833 : prepare_names_to_update. */
2834 :
2835 : static void
2836 23204089 : prepare_def_site_for (tree name, bool insert_phi_p)
2837 : {
2838 23204089 : gimple *stmt;
2839 23204089 : basic_block bb;
2840 :
2841 26986487 : gcc_checking_assert (names_to_release == NULL
2842 : || !bitmap_bit_p (names_to_release,
2843 : SSA_NAME_VERSION (name)));
2844 :
2845 : /* If we rename virtual operands do not update them. */
2846 23204089 : if (virtual_operand_p (name)
2847 23204089 : && cfun->gimple_df->rename_vops)
2848 : return;
2849 :
2850 23200326 : stmt = SSA_NAME_DEF_STMT (name);
2851 23200326 : bb = gimple_bb (stmt);
2852 23200326 : if (bb)
2853 : {
2854 23200326 : gcc_checking_assert (bb->index < last_basic_block_for_fn (cfun));
2855 23200326 : mark_block_for_update (bb);
2856 23200326 : mark_def_interesting (name, stmt, bb, insert_phi_p);
2857 : }
2858 : }
2859 :
2860 :
2861 : /* Mark definition and use sites of names in NEW_SSA_NAMES and
2862 : OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2863 : PHI nodes for newly created names. */
2864 :
2865 : static void
2866 1400455 : prepare_names_to_update (bool insert_phi_p)
2867 : {
2868 1400455 : unsigned i = 0;
2869 1400455 : bitmap_iterator bi;
2870 1400455 : sbitmap_iterator sbi;
2871 :
2872 : /* If a name N from NEW_SSA_NAMES is also marked to be released,
2873 : remove it from NEW_SSA_NAMES so that we don't try to visit its
2874 : defining basic block (which most likely doesn't exist). Notice
2875 : that we cannot do the same with names in OLD_SSA_NAMES because we
2876 : want to replace existing instances. */
2877 1400455 : if (names_to_release)
2878 1023557 : EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2879 852456 : bitmap_clear_bit (new_ssa_names, i);
2880 :
2881 : /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2882 : names may be considered to be live-in on blocks that contain
2883 : definitions for their replacements. */
2884 17248974 : EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
2885 14448064 : prepare_def_site_for (ssa_name (i), insert_phi_p);
2886 :
2887 : /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2888 : OLD_SSA_NAMES, but we have to ignore its definition site. */
2889 12351615 : EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
2890 : {
2891 9550705 : if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2892 8756025 : prepare_def_site_for (ssa_name (i), insert_phi_p);
2893 9550705 : prepare_use_sites_for (ssa_name (i), insert_phi_p);
2894 : }
2895 1400455 : }
2896 :
2897 :
2898 : /* Dump all the names replaced by NAME to FILE. */
2899 :
2900 : void
2901 29338 : dump_names_replaced_by (FILE *file, tree name)
2902 : {
2903 29338 : unsigned i;
2904 29338 : bitmap old_set;
2905 29338 : bitmap_iterator bi;
2906 :
2907 29338 : print_generic_expr (file, name);
2908 29338 : fprintf (file, " -> { ");
2909 :
2910 29338 : old_set = names_replaced_by (name);
2911 58686 : EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2912 : {
2913 29348 : print_generic_expr (file, ssa_name (i));
2914 29348 : fprintf (file, " ");
2915 : }
2916 :
2917 29338 : fprintf (file, "}\n");
2918 29338 : }
2919 :
2920 :
2921 : /* Dump all the names replaced by NAME to stderr. */
2922 :
2923 : DEBUG_FUNCTION void
2924 0 : debug_names_replaced_by (tree name)
2925 : {
2926 0 : dump_names_replaced_by (stderr, name);
2927 0 : }
2928 :
2929 :
2930 : /* Dump SSA update information to FILE. */
2931 :
2932 : void
2933 6505 : dump_update_ssa (FILE *file)
2934 : {
2935 6505 : unsigned i = 0;
2936 6505 : bitmap_iterator bi;
2937 :
2938 6505 : if (!need_ssa_update_p (cfun))
2939 0 : return;
2940 :
2941 6505 : if (new_ssa_names && !bitmap_empty_p (new_ssa_names))
2942 : {
2943 2319 : sbitmap_iterator sbi;
2944 :
2945 2319 : fprintf (file, "\nSSA replacement table\n");
2946 2319 : fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2947 : "O_1, ..., O_j\n\n");
2948 :
2949 33976 : EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
2950 29338 : dump_names_replaced_by (file, ssa_name (i));
2951 : }
2952 :
2953 6505 : if (symbols_to_rename_set && !bitmap_empty_p (symbols_to_rename_set))
2954 : {
2955 4290 : fprintf (file, "\nSymbols to be put in SSA form\n");
2956 4290 : dump_decl_set (file, symbols_to_rename_set);
2957 4290 : fprintf (file, "\n");
2958 : }
2959 :
2960 6505 : if (names_to_release && !bitmap_empty_p (names_to_release))
2961 : {
2962 28 : fprintf (file, "\nSSA names to release after updating the SSA web\n\n");
2963 98 : EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2964 : {
2965 70 : print_generic_expr (file, ssa_name (i));
2966 70 : fprintf (file, " ");
2967 : }
2968 28 : fprintf (file, "\n");
2969 : }
2970 : }
2971 :
2972 :
2973 : /* Dump SSA update information to stderr. */
2974 :
2975 : DEBUG_FUNCTION void
2976 0 : debug_update_ssa (void)
2977 : {
2978 0 : dump_update_ssa (stderr);
2979 0 : }
2980 :
2981 :
2982 : /* Initialize data structures used for incremental SSA updates. */
2983 :
2984 : static void
2985 3976003 : init_update_ssa (struct function *fn)
2986 : {
2987 : /* Reserve more space than the current number of names. The calls to
2988 : add_new_name_mapping are typically done after creating new SSA
2989 : names, so we'll need to reallocate these arrays. */
2990 7952006 : old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2991 3976003 : bitmap_clear (old_ssa_names);
2992 :
2993 7952006 : new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2994 3976003 : bitmap_clear (new_ssa_names);
2995 :
2996 3976003 : bitmap_obstack_initialize (&update_ssa_obstack);
2997 :
2998 3976003 : names_to_release = NULL;
2999 3976003 : update_ssa_initialized_fn = fn;
3000 3976003 : }
3001 :
3002 :
3003 : /* Deallocate data structures used for incremental SSA updates. */
3004 :
3005 : void
3006 3976003 : delete_update_ssa (void)
3007 : {
3008 3976003 : unsigned i;
3009 3976003 : bitmap_iterator bi;
3010 :
3011 3976003 : sbitmap_free (old_ssa_names);
3012 3976003 : old_ssa_names = NULL;
3013 :
3014 3976003 : sbitmap_free (new_ssa_names);
3015 3976003 : new_ssa_names = NULL;
3016 :
3017 3976003 : BITMAP_FREE (symbols_to_rename_set);
3018 3976003 : symbols_to_rename_set = NULL;
3019 3976003 : symbols_to_rename.release ();
3020 :
3021 3976003 : if (names_to_release)
3022 : {
3023 1023557 : EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
3024 852456 : release_ssa_name (ssa_name (i));
3025 171101 : BITMAP_FREE (names_to_release);
3026 : }
3027 :
3028 3976003 : clear_ssa_name_info ();
3029 :
3030 3976003 : fini_ssa_renamer ();
3031 :
3032 3976003 : BITMAP_FREE (blocks_with_phis_to_rewrite);
3033 3976003 : BITMAP_FREE (blocks_to_update);
3034 :
3035 3976003 : update_ssa_initialized_fn = NULL;
3036 3976003 : }
3037 :
3038 :
3039 : /* Create a new name for OLD_NAME in statement STMT and replace the
3040 : operand pointed to by DEF_P with the newly created name. If DEF_P
3041 : is NULL then STMT should be a GIMPLE assignment.
3042 : Return the new name and register the replacement mapping <NEW, OLD> in
3043 : update_ssa's tables. */
3044 :
3045 : tree
3046 14972272 : create_new_def_for (tree old_name, gimple *stmt, def_operand_p def)
3047 : {
3048 14972272 : tree new_name;
3049 :
3050 14972272 : timevar_push (TV_TREE_SSA_INCREMENTAL);
3051 :
3052 14972272 : if (!update_ssa_initialized_fn)
3053 1435258 : init_update_ssa (cfun);
3054 :
3055 14972272 : gcc_assert (update_ssa_initialized_fn == cfun);
3056 :
3057 14972272 : new_name = duplicate_ssa_name (old_name, stmt);
3058 14972272 : if (def)
3059 14971999 : SET_DEF (def, new_name);
3060 : else
3061 273 : gimple_assign_set_lhs (stmt, new_name);
3062 :
3063 14972272 : if (gimple_code (stmt) == GIMPLE_PHI)
3064 : {
3065 9289994 : basic_block bb = gimple_bb (stmt);
3066 :
3067 : /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
3068 9289994 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = bb_has_abnormal_pred (bb);
3069 : }
3070 :
3071 14972272 : add_new_name_mapping (new_name, old_name);
3072 :
3073 : /* For the benefit of passes that will be updating the SSA form on
3074 : their own, set the current reaching definition of OLD_NAME to be
3075 : NEW_NAME. */
3076 14972272 : get_ssa_name_ann (old_name)->info.current_def = new_name;
3077 :
3078 14972272 : timevar_pop (TV_TREE_SSA_INCREMENTAL);
3079 :
3080 14972272 : return new_name;
3081 : }
3082 :
3083 :
3084 : /* Mark virtual operands of FN for renaming by update_ssa. */
3085 :
3086 : void
3087 406026 : mark_virtual_operands_for_renaming (struct function *fn)
3088 : {
3089 406026 : fn->gimple_df->ssa_renaming_needed = 1;
3090 406026 : fn->gimple_df->rename_vops = 1;
3091 406026 : }
3092 :
3093 : /* Replace all uses of NAME by underlying variable and mark it
3094 : for renaming. This assumes the defining statement of NAME is
3095 : going to be removed. */
3096 :
3097 : void
3098 354525 : mark_virtual_operand_for_renaming (tree name)
3099 : {
3100 354525 : tree name_var = SSA_NAME_VAR (name);
3101 354525 : bool used = false;
3102 354525 : imm_use_iterator iter;
3103 354525 : use_operand_p use_p;
3104 354525 : gimple *stmt;
3105 :
3106 354525 : gcc_assert (VAR_DECL_IS_VIRTUAL_OPERAND (name_var));
3107 484367 : FOR_EACH_IMM_USE_STMT (stmt, iter, name)
3108 : {
3109 259720 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3110 129860 : SET_USE (use_p, name_var);
3111 129842 : used = true;
3112 354525 : }
3113 354525 : if (used)
3114 110998 : mark_virtual_operands_for_renaming (cfun);
3115 354525 : }
3116 :
3117 : /* Replace all uses of the virtual PHI result by its underlying variable
3118 : and mark it for renaming. This assumes the PHI node is going to be
3119 : removed. */
3120 :
3121 : void
3122 64130 : mark_virtual_phi_result_for_renaming (gphi *phi)
3123 : {
3124 64130 : if (dump_file && (dump_flags & TDF_DETAILS))
3125 : {
3126 50 : fprintf (dump_file, "Marking result for renaming : ");
3127 50 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
3128 50 : fprintf (dump_file, "\n");
3129 : }
3130 :
3131 64130 : mark_virtual_operand_for_renaming (gimple_phi_result (phi));
3132 64130 : }
3133 :
3134 : /* Return true if there is any work to be done by update_ssa
3135 : for function FN. */
3136 :
3137 : bool
3138 1618453450 : need_ssa_update_p (struct function *fn)
3139 : {
3140 1618453450 : gcc_assert (fn != NULL);
3141 1618453450 : return (update_ssa_initialized_fn == fn
3142 1618453450 : || (fn->gimple_df && fn->gimple_df->ssa_renaming_needed));
3143 : }
3144 :
3145 : /* Return true if name N has been registered in the replacement table. */
3146 :
3147 : bool
3148 214245709 : name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
3149 : {
3150 214245709 : if (!update_ssa_initialized_fn)
3151 : return false;
3152 :
3153 5282527 : gcc_assert (update_ssa_initialized_fn == cfun);
3154 :
3155 5282527 : return is_new_name (n) || is_old_name (n);
3156 : }
3157 :
3158 :
3159 : /* Mark NAME to be released after update_ssa has finished. */
3160 :
3161 : void
3162 852456 : release_ssa_name_after_update_ssa (tree name)
3163 : {
3164 852456 : gcc_assert (cfun && update_ssa_initialized_fn == cfun);
3165 :
3166 852456 : if (names_to_release == NULL)
3167 171101 : names_to_release = BITMAP_ALLOC (NULL);
3168 :
3169 852456 : bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
3170 852456 : }
3171 :
3172 :
3173 : /* Insert new PHI nodes to replace VAR. DFS contains dominance
3174 : frontier information.
3175 :
3176 : This is slightly different than the regular PHI insertion
3177 : algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
3178 : real names (i.e., GIMPLE registers) are inserted:
3179 :
3180 : - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
3181 : nodes inside the region affected by the block that defines VAR
3182 : and the blocks that define all its replacements. All these
3183 : definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
3184 :
3185 : First, we compute the entry point to the region (ENTRY). This is
3186 : given by the nearest common dominator to all the definition
3187 : blocks. When computing the iterated dominance frontier (IDF), any
3188 : block not strictly dominated by ENTRY is ignored.
3189 :
3190 : We then call the standard PHI insertion algorithm with the pruned
3191 : IDF.
3192 :
3193 : - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3194 : names is not pruned. PHI nodes are inserted at every IDF block. */
3195 :
3196 : static void
3197 15647368 : insert_updated_phi_nodes_for (tree var, bitmap_head *dfs,
3198 : unsigned update_flags)
3199 : {
3200 15647368 : basic_block entry;
3201 15647368 : def_blocks *db;
3202 15647368 : bitmap pruned_idf;
3203 15647368 : bitmap_iterator bi;
3204 15647368 : unsigned i;
3205 :
3206 15647368 : if (TREE_CODE (var) == SSA_NAME)
3207 7221214 : gcc_checking_assert (is_old_name (var));
3208 : else
3209 8426154 : gcc_checking_assert (marked_for_renaming (var));
3210 :
3211 : /* Get all the definition sites for VAR. */
3212 15647368 : db = find_def_blocks_for (var);
3213 :
3214 : /* No need to do anything if there were no definitions to VAR. */
3215 15644590 : if (db == NULL || bitmap_empty_p (db->def_blocks))
3216 297168 : return;
3217 :
3218 : /* Compute the initial iterated dominance frontier. */
3219 15350200 : pruned_idf = compute_idf (db->def_blocks, dfs);
3220 :
3221 15350200 : if (TREE_CODE (var) == SSA_NAME)
3222 : {
3223 7218466 : if (update_flags == TODO_update_ssa)
3224 : {
3225 : /* If doing regular SSA updates for GIMPLE registers, we are
3226 : only interested in IDF blocks dominated by the nearest
3227 : common dominator of all the definition blocks. */
3228 7218466 : entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
3229 : db->def_blocks);
3230 7218466 : if (entry != single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
3231 : {
3232 6824004 : unsigned to_remove = ~0U;
3233 33102139 : EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3234 : {
3235 26278135 : if (to_remove != ~0U)
3236 : {
3237 11517551 : bitmap_clear_bit (pruned_idf, to_remove);
3238 11517551 : to_remove = ~0U;
3239 : }
3240 26278135 : if (BASIC_BLOCK_FOR_FN (cfun, i) == entry
3241 26278135 : || !dominated_by_p (CDI_DOMINATORS,
3242 : BASIC_BLOCK_FOR_FN (cfun, i), entry))
3243 : to_remove = i;
3244 : }
3245 6824004 : if (to_remove != ~0U)
3246 4330116 : bitmap_clear_bit (pruned_idf, to_remove);
3247 : }
3248 : }
3249 : else
3250 : /* Otherwise, do not prune the IDF for VAR. */
3251 0 : gcc_checking_assert (update_flags == TODO_update_ssa_full_phi);
3252 : }
3253 : /* Otherwise, VAR is a symbol that needs to be put into SSA form
3254 : for the first time, so we need to compute the full IDF for
3255 : it. */
3256 :
3257 15350200 : if (!bitmap_empty_p (pruned_idf))
3258 : {
3259 : /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3260 : are included in the region to be updated. The feeding blocks
3261 : are important to guarantee that the PHI arguments are renamed
3262 : properly. */
3263 :
3264 : /* FIXME, this is not needed if we are updating symbols. We are
3265 : already starting at the ENTRY block anyway. */
3266 37953299 : EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3267 : {
3268 28277715 : edge e;
3269 28277715 : edge_iterator ei;
3270 28277715 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
3271 :
3272 28277715 : mark_block_for_update (bb);
3273 182285101 : FOR_EACH_EDGE (e, ei, bb->preds)
3274 154007386 : if (e->src->index >= NUM_FIXED_BLOCKS)
3275 154001889 : mark_block_for_update (e->src);
3276 : }
3277 :
3278 9675584 : insert_phi_nodes_for (var, pruned_idf, true);
3279 : }
3280 :
3281 15350200 : BITMAP_FREE (pruned_idf);
3282 : }
3283 :
3284 : /* Sort symbols_to_rename after their DECL_UID. */
3285 :
3286 : static int
3287 118999755 : insert_updated_phi_nodes_compare_uids (const void *a, const void *b)
3288 : {
3289 118999755 : const_tree syma = *(const const_tree *)a;
3290 118999755 : const_tree symb = *(const const_tree *)b;
3291 118999755 : if (DECL_UID (syma) == DECL_UID (symb))
3292 : return 0;
3293 118999755 : return DECL_UID (syma) < DECL_UID (symb) ? -1 : 1;
3294 : }
3295 :
3296 : /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3297 : existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3298 :
3299 : 1- The names in OLD_SSA_NAMES dominated by the definitions of
3300 : NEW_SSA_NAMES are all re-written to be reached by the
3301 : appropriate definition from NEW_SSA_NAMES.
3302 :
3303 : 2- If needed, new PHI nodes are added to the iterated dominance
3304 : frontier of the blocks where each of NEW_SSA_NAMES are defined.
3305 :
3306 : The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3307 : calling create_new_def_for to create new defs for names that the
3308 : caller wants to replace.
3309 :
3310 : The caller creates the new names to be inserted and the names that need
3311 : to be replaced by calling create_new_def_for for each old definition
3312 : to be replaced. Note that the function assumes that the
3313 : new defining statement has already been inserted in the IL.
3314 :
3315 : For instance, given the following code:
3316 :
3317 : 1 L0:
3318 : 2 x_1 = PHI (0, x_5)
3319 : 3 if (x_1 < 10)
3320 : 4 if (x_1 > 7)
3321 : 5 y_2 = 0
3322 : 6 else
3323 : 7 y_3 = x_1 + x_7
3324 : 8 endif
3325 : 9 x_5 = x_1 + 1
3326 : 10 goto L0;
3327 : 11 endif
3328 :
3329 : Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3330 :
3331 : 1 L0:
3332 : 2 x_1 = PHI (0, x_5)
3333 : 3 if (x_1 < 10)
3334 : 4 x_10 = ...
3335 : 5 if (x_1 > 7)
3336 : 6 y_2 = 0
3337 : 7 else
3338 : 8 x_11 = ...
3339 : 9 y_3 = x_1 + x_7
3340 : 10 endif
3341 : 11 x_5 = x_1 + 1
3342 : 12 goto L0;
3343 : 13 endif
3344 :
3345 : We want to replace all the uses of x_1 with the new definitions of
3346 : x_10 and x_11. Note that the only uses that should be replaced are
3347 : those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3348 : *not* be replaced (this is why we cannot just mark symbol 'x' for
3349 : renaming).
3350 :
3351 : Additionally, we may need to insert a PHI node at line 11 because
3352 : that is a merge point for x_10 and x_11. So the use of x_1 at line
3353 : 11 will be replaced with the new PHI node. The insertion of PHI
3354 : nodes is optional. They are not strictly necessary to preserve the
3355 : SSA form, and depending on what the caller inserted, they may not
3356 : even be useful for the optimizers. UPDATE_FLAGS controls various
3357 : aspects of how update_ssa operates, see the documentation for
3358 : TODO_update_ssa*. */
3359 :
3360 : void
3361 63576526 : update_ssa (unsigned update_flags)
3362 : {
3363 63576526 : basic_block bb, start_bb;
3364 63576526 : bitmap_iterator bi;
3365 63576526 : unsigned i = 0;
3366 63576526 : bool insert_phi_p;
3367 63576526 : sbitmap_iterator sbi;
3368 63576526 : tree sym;
3369 :
3370 : /* Only one update flag should be set. */
3371 63576526 : gcc_assert (update_flags == TODO_update_ssa
3372 : || update_flags == TODO_update_ssa_no_phi
3373 : || update_flags == TODO_update_ssa_full_phi
3374 : || update_flags == TODO_update_ssa_only_virtuals);
3375 :
3376 63576526 : if (!need_ssa_update_p (cfun))
3377 59635326 : return;
3378 :
3379 3941200 : if (flag_checking)
3380 : {
3381 3941123 : timevar_push (TV_TREE_STMT_VERIFY);
3382 :
3383 3941123 : bool err = false;
3384 :
3385 127555506 : FOR_EACH_BB_FN (bb, cfun)
3386 : {
3387 123614383 : gimple_stmt_iterator gsi;
3388 1164775138 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3389 : {
3390 917546372 : gimple *stmt = gsi_stmt (gsi);
3391 :
3392 917546372 : ssa_op_iter i;
3393 917546372 : use_operand_p use_p;
3394 1488288171 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
3395 : {
3396 570741799 : tree use = USE_FROM_PTR (use_p);
3397 570741799 : if (TREE_CODE (use) != SSA_NAME)
3398 31843487 : continue;
3399 :
3400 538898312 : if (SSA_NAME_IN_FREE_LIST (use))
3401 : {
3402 0 : error ("statement uses released SSA name");
3403 0 : debug_gimple_stmt (stmt);
3404 0 : fprintf (stderr, "The use of ");
3405 0 : print_generic_expr (stderr, use);
3406 0 : fprintf (stderr," should have been replaced\n");
3407 0 : err = true;
3408 : }
3409 : }
3410 : }
3411 : }
3412 :
3413 3941123 : if (err)
3414 0 : internal_error ("cannot update SSA form");
3415 :
3416 3941123 : timevar_pop (TV_TREE_STMT_VERIFY);
3417 : }
3418 :
3419 3941200 : timevar_push (TV_TREE_SSA_INCREMENTAL);
3420 :
3421 3941200 : if (dump_file && (dump_flags & TDF_DETAILS))
3422 2757 : fprintf (dump_file, "\nUpdating SSA:\n");
3423 :
3424 3941200 : if (!update_ssa_initialized_fn)
3425 2540745 : init_update_ssa (cfun);
3426 1400455 : else if (update_flags == TODO_update_ssa_only_virtuals)
3427 : {
3428 : /* If we only need to update virtuals, remove all the mappings for
3429 : real names before proceeding. The caller is responsible for
3430 : having dealt with the name mappings before calling update_ssa. */
3431 0 : bitmap_clear (old_ssa_names);
3432 0 : bitmap_clear (new_ssa_names);
3433 : }
3434 :
3435 3941200 : gcc_assert (update_ssa_initialized_fn == cfun);
3436 :
3437 3941200 : blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
3438 3941200 : bitmap_tree_view (blocks_with_phis_to_rewrite);
3439 3941200 : blocks_to_update = BITMAP_ALLOC (NULL);
3440 3941200 : bitmap_tree_view (blocks_to_update);
3441 :
3442 3941200 : insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
3443 :
3444 : /* Ensure that the dominance information is up-to-date and when we
3445 : are going to compute dominance frontiers fast queries are possible. */
3446 3941200 : if (insert_phi_p || dom_info_state (CDI_DOMINATORS) == DOM_NONE)
3447 3317295 : calculate_dominance_info (CDI_DOMINATORS);
3448 :
3449 : /* If there are names defined in the replacement table, prepare
3450 : definition and use sites for all the names in NEW_SSA_NAMES and
3451 : OLD_SSA_NAMES. */
3452 3941200 : if (!bitmap_empty_p (new_ssa_names))
3453 : {
3454 1400455 : statistics_counter_event (cfun, "Incremental SSA update", 1);
3455 :
3456 1400455 : prepare_names_to_update (insert_phi_p);
3457 :
3458 : /* If all the names in NEW_SSA_NAMES had been marked for
3459 : removal, and there are no symbols to rename, then there's
3460 : nothing else to do. */
3461 1400455 : if (bitmap_empty_p (new_ssa_names)
3462 1400455 : && !cfun->gimple_df->ssa_renaming_needed)
3463 118 : goto done;
3464 : }
3465 :
3466 : /* Next, determine the block at which to start the renaming process. */
3467 3941082 : if (cfun->gimple_df->ssa_renaming_needed)
3468 : {
3469 2542191 : statistics_counter_event (cfun, "Symbol to SSA rewrite", 1);
3470 :
3471 : /* If we rename bare symbols initialize the mapping to
3472 : auxiliary info we need to keep track of. */
3473 2542191 : var_infos = new hash_table<var_info_hasher> (47);
3474 :
3475 : /* If we have to rename some symbols from scratch, we need to
3476 : start the process at the root of the CFG. FIXME, it should
3477 : be possible to determine the nearest block that had a
3478 : definition for each of the symbols that are marked for
3479 : updating. For now this seems more work than it's worth. */
3480 2542191 : start_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
3481 :
3482 : /* Traverse the CFG looking for existing definitions and uses of
3483 : symbols in SSA operands. Mark interesting blocks and
3484 : statements and set local live-in information for the PHI
3485 : placement heuristics. */
3486 2542191 : prepare_block_for_update (start_bb, insert_phi_p);
3487 :
3488 2542191 : bitmap_list_view (blocks_to_update);
3489 :
3490 2542191 : tree name;
3491 :
3492 2542191 : if (flag_checking)
3493 179010532 : FOR_EACH_SSA_NAME (i, name, cfun)
3494 : {
3495 265694680 : if (virtual_operand_p (name))
3496 48640520 : continue;
3497 :
3498 : /* For all but virtual operands, which do not have SSA names
3499 : with overlapping life ranges, ensure that symbols marked
3500 : for renaming do not have existing SSA names associated with
3501 : them as we do not re-write them out-of-SSA before going
3502 : into SSA for the remaining symbol uses. */
3503 148699330 : if (marked_for_renaming (SSA_NAME_VAR (name)))
3504 : {
3505 0 : fprintf (stderr, "Existing SSA name for symbol marked for "
3506 : "renaming: ");
3507 0 : print_generic_expr (stderr, name, TDF_SLIM);
3508 0 : fprintf (stderr, "\n");
3509 0 : internal_error ("SSA corruption");
3510 : }
3511 : }
3512 : }
3513 : else
3514 : {
3515 1398891 : bitmap_list_view (blocks_to_update);
3516 :
3517 : /* Otherwise, the entry block to the region is the nearest
3518 : common dominator for the blocks in BLOCKS. */
3519 1398891 : start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3520 : blocks_to_update);
3521 : }
3522 :
3523 : /* If requested, insert PHI nodes at the iterated dominance frontier
3524 : of every block, creating new definitions for names in OLD_SSA_NAMES
3525 : and for symbols found. */
3526 3941082 : if (insert_phi_p)
3527 : {
3528 3317177 : bitmap_head *dfs;
3529 :
3530 : /* If the caller requested PHI nodes to be added, compute
3531 : dominance frontiers. */
3532 3317177 : dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
3533 106302922 : FOR_EACH_BB_FN (bb, cfun)
3534 102985745 : bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
3535 3317177 : compute_dominance_frontiers (dfs);
3536 :
3537 3317177 : bitmap_tree_view (blocks_to_update);
3538 :
3539 : /* insert_update_phi_nodes_for will call add_new_name_mapping
3540 : when inserting new PHI nodes, but it will not add any
3541 : new members to OLD_SSA_NAMES. */
3542 3317177 : iterating_old_ssa_names = true;
3543 3317177 : sbitmap_iterator sbi;
3544 13855568 : EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
3545 7221214 : insert_updated_phi_nodes_for (ssa_name (i), dfs, update_flags);
3546 3317177 : iterating_old_ssa_names = false;
3547 :
3548 3317177 : symbols_to_rename.qsort (insert_updated_phi_nodes_compare_uids);
3549 11743331 : FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
3550 8426154 : insert_updated_phi_nodes_for (sym, dfs, update_flags);
3551 :
3552 3317177 : bitmap_list_view (blocks_to_update);
3553 :
3554 106302922 : FOR_EACH_BB_FN (bb, cfun)
3555 102985745 : bitmap_clear (&dfs[bb->index]);
3556 3317177 : free (dfs);
3557 :
3558 : /* Insertion of PHI nodes may have added blocks to the region.
3559 : We need to re-compute START_BB to include the newly added
3560 : blocks. */
3561 3317177 : if (start_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3562 775032 : start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3563 : blocks_to_update);
3564 : }
3565 :
3566 : /* Reset the current definition for name and symbol before renaming
3567 : the sub-graph. */
3568 17432644 : EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
3569 9550480 : get_ssa_name_ann (ssa_name (i))->info.current_def = NULL_TREE;
3570 :
3571 12367282 : FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
3572 8426200 : get_var_info (sym)->info.current_def = NULL_TREE;
3573 :
3574 : /* Now start the renaming process at START_BB. When not inserting PHIs
3575 : and thus we are avoiding work on all blocks, try to confine the
3576 : rewriting domwalk to the affected region, otherwise it's not worth it. */
3577 4564987 : rewrite_blocks (start_bb,
3578 : insert_phi_p ? REWRITE_UPDATE : REWRITE_UPDATE_REGION);
3579 :
3580 : /* Debugging dumps. */
3581 3941082 : if (dump_file)
3582 : {
3583 6505 : int c;
3584 6505 : unsigned i;
3585 :
3586 6505 : dump_update_ssa (dump_file);
3587 :
3588 6505 : fprintf (dump_file, "Incremental SSA update started at block: %d\n",
3589 : start_bb->index);
3590 :
3591 6505 : c = 0;
3592 53673 : EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3593 47168 : c++;
3594 6505 : fprintf (dump_file, "Number of blocks in CFG: %d\n",
3595 6505 : last_basic_block_for_fn (cfun));
3596 6505 : fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n",
3597 6505 : c, PERCENT (c, last_basic_block_for_fn (cfun)));
3598 :
3599 6505 : if (dump_flags & TDF_DETAILS)
3600 : {
3601 2757 : fprintf (dump_file, "Affected blocks:");
3602 28588 : EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3603 25831 : fprintf (dump_file, " %u", i);
3604 2757 : fprintf (dump_file, "\n");
3605 : }
3606 :
3607 6505 : fprintf (dump_file, "\n\n");
3608 : }
3609 :
3610 : /* Free allocated memory. */
3611 3934577 : done:
3612 3941200 : delete_update_ssa ();
3613 :
3614 3941200 : timevar_pop (TV_TREE_SSA_INCREMENTAL);
3615 : }
|