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