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