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