PR target/19236
[official-gcc.git] / gcc / tree-into-ssa.c
blob0e7e5e12feca9b9e3dd1c2820accc0cd85eac49c
1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "errors.h"
35 #include "expr.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "bitmap.h"
39 #include "tree-flow.h"
40 #include "tree-gimple.h"
41 #include "tree-inline.h"
42 #include "varray.h"
43 #include "timevar.h"
44 #include "hashtab.h"
45 #include "tree-dump.h"
46 #include "tree-pass.h"
47 #include "cfgloop.h"
48 #include "domwalk.h"
49 #include "ggc.h"
51 /* This file builds the SSA form for a function as described in:
52 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
53 Computing Static Single Assignment Form and the Control Dependence
54 Graph. ACM Transactions on Programming Languages and Systems,
55 13(4):451-490, October 1991. */
58 /* Structure to map a variable VAR to the set of blocks that contain
59 definitions for VAR. */
60 struct def_blocks_d
62 /* The variable. */
63 tree var;
65 /* Blocks that contain definitions of VAR. Bit I will be set if the
66 Ith block contains a definition of VAR. */
67 bitmap def_blocks;
69 /* Blocks that contain a phi node for VAR. */
70 bitmap phi_blocks;
72 /* Blocks where VAR is live-on-entry. Similar semantics as
73 DEF_BLOCKS. */
74 bitmap livein_blocks;
77 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
78 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
79 basic blocks where VAR is defined (assigned a new value). It also
80 contains a bitmap of all the blocks where VAR is live-on-entry
81 (i.e., there is a use of VAR in block B without a preceding
82 definition in B). The live-on-entry information is used when
83 computing PHI pruning heuristics. */
84 static htab_t def_blocks;
86 /* Stack of trees used to restore the global currdefs to its original
87 state after completing rewriting of a block and its dominator children.
89 This vector is used in two contexts. The first is rewriting of _DECL
90 nodes into SSA_NAMEs. In that context it's elements have the
91 following properties:
93 An SSA_NAME indicates that the current definition of the underlying
94 variable should be set to the given SSA_NAME.
96 A _DECL node indicates that the underlying variable has no current
97 definition.
99 A NULL node is used to mark the last node associated with the
100 current block.
103 This vector is also used when rewriting an SSA_NAME which has multiple
104 definition sites into multiple SSA_NAMEs. In that context entries come
105 in pairs.
107 The top entry is an SSA_NAME and the top-1 entry is the
108 current value for that SSA_NAME.
110 A NULL node at the top entry is used to mark the last node associated
111 with the current block. */
112 static VEC(tree_on_heap) *block_defs_stack;
114 /* Basic block vectors used in this file ought to be allocated in the heap. */
115 DEF_VEC_MALLOC_P(basic_block);
117 /* Global data to attach to the main dominator walk structure. */
118 struct mark_def_sites_global_data
120 /* This sbitmap contains the variables which are set before they
121 are used in a basic block. We keep it as a global variable
122 solely to avoid the overhead of allocating and deallocating
123 the bitmap. */
124 sbitmap kills;
126 /* Bitmap of names to rename. */
127 sbitmap names_to_rename;
130 /* Information stored for ssa names. */
132 struct ssa_name_info
134 /* This field indicates whether or not the variable may need PHI nodes.
135 See the enum's definition for more detailed information about the
136 states. */
137 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
139 /* The actual definition of the ssa name. */
140 tree current_def;
143 /* Local functions. */
144 static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
145 static void rewrite_initialize_block (struct dom_walk_data *, basic_block);
146 static void rewrite_add_phi_arguments (struct dom_walk_data *, basic_block);
147 static void mark_def_sites (struct dom_walk_data *walk_data,
148 basic_block bb, block_stmt_iterator);
149 static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
150 basic_block bb);
151 static void set_def_block (tree, basic_block, bool, bool);
152 static void set_livein_block (tree, basic_block);
153 static bool prepare_use_operand_for_rename (use_operand_p, size_t *uid_p);
154 static bool prepare_def_operand_for_rename (tree def, size_t *uid_p);
155 static void insert_phi_nodes (bitmap *, bitmap);
156 static void rewrite_stmt (struct dom_walk_data *, basic_block,
157 block_stmt_iterator);
158 static inline void rewrite_operand (use_operand_p);
159 static void insert_phi_nodes_for (tree, bitmap *, VEC(basic_block) **);
160 static tree get_reaching_def (tree);
161 static hashval_t def_blocks_hash (const void *);
162 static int def_blocks_eq (const void *, const void *);
163 static void def_blocks_free (void *);
164 static int debug_def_blocks_r (void **, void *);
165 static inline struct def_blocks_d *get_def_blocks_for (tree);
166 static inline struct def_blocks_d *find_def_blocks_for (tree);
167 static void htab_statistics (FILE *, htab_t);
169 /* Use TREE_VISITED to keep track of which statements we want to
170 rename. When renaming a subset of the variables, not all
171 statements will be processed. This is decided in mark_def_sites. */
172 #define REWRITE_THIS_STMT(T) TREE_VISITED (T)
175 /* Get the information associated with NAME. */
177 static inline struct ssa_name_info *
178 get_ssa_name_ann (tree name)
180 if (!SSA_NAME_AUX (name))
181 SSA_NAME_AUX (name) = xcalloc (1, sizeof (struct ssa_name_info));
183 return SSA_NAME_AUX (name);
186 /* Gets phi_state field for VAR. */
188 static inline enum need_phi_state
189 get_phi_state (tree var)
191 if (TREE_CODE (var) == SSA_NAME)
192 return get_ssa_name_ann (var)->need_phi_state;
193 else
194 return var_ann (var)->need_phi_state;
197 /* Sets phi_state field for VAR to STATE. */
199 static inline void
200 set_phi_state (tree var, enum need_phi_state state)
202 if (TREE_CODE (var) == SSA_NAME)
203 get_ssa_name_ann (var)->need_phi_state = state;
204 else
205 var_ann (var)->need_phi_state = state;
208 /* Return the current definition for VAR. */
210 static inline tree
211 get_current_def (tree var)
213 if (TREE_CODE (var) == SSA_NAME)
214 return get_ssa_name_ann (var)->current_def;
215 else
216 return var_ann (var)->current_def;
219 /* Sets current definition of VAR to DEF. */
221 static inline void
222 set_current_def (tree var, tree def)
224 if (TREE_CODE (var) == SSA_NAME)
225 get_ssa_name_ann (var)->current_def = def;
226 else
227 var_ann (var)->current_def = def;
230 /* Compute global livein information given the set of blockx where
231 an object is locally live at the start of the block (LIVEIN)
232 and the set of blocks where the object is defined (DEF_BLOCKS).
234 Note: This routine augments the existing local livein information
235 to include global livein (i.e., it modifies the underlying bitmap
236 for LIVEIN). */
238 void
239 compute_global_livein (bitmap livein, bitmap def_blocks)
241 basic_block bb, *worklist, *tos;
242 unsigned i;
243 bitmap_iterator bi;
245 tos = worklist
246 = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
248 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
250 *tos++ = BASIC_BLOCK (i);
253 /* Iterate until the worklist is empty. */
254 while (tos != worklist)
256 edge e;
257 edge_iterator ei;
259 /* Pull a block off the worklist. */
260 bb = *--tos;
262 /* For each predecessor block. */
263 FOR_EACH_EDGE (e, ei, bb->preds)
265 basic_block pred = e->src;
266 int pred_index = pred->index;
268 /* None of this is necessary for the entry block. */
269 if (pred != ENTRY_BLOCK_PTR
270 && ! bitmap_bit_p (livein, pred_index)
271 && ! bitmap_bit_p (def_blocks, pred_index))
273 *tos++ = pred;
274 bitmap_set_bit (livein, pred_index);
279 free (worklist);
283 /* Block initialization routine for mark_def_sites. Clear the
284 KILLS bitmap at the start of each block. */
286 static void
287 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
288 basic_block bb ATTRIBUTE_UNUSED)
290 struct mark_def_sites_global_data *gd = walk_data->global_data;
291 sbitmap kills = gd->kills;
293 sbitmap_zero (kills);
296 /* Block initialization routine for mark_def_sites. Clear the
297 KILLS bitmap at the start of each block. */
299 static void
300 ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
301 basic_block bb)
303 struct mark_def_sites_global_data *gd = walk_data->global_data;
304 sbitmap kills = gd->kills;
305 tree phi, def;
306 unsigned def_uid;
308 sbitmap_zero (kills);
310 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
312 def = PHI_RESULT (phi);
313 def_uid = SSA_NAME_VERSION (def);
315 if (!TEST_BIT (gd->names_to_rename, def_uid))
316 continue;
318 set_def_block (def, bb, true, true);
319 SET_BIT (kills, def_uid);
323 /* Marks ssa names used as arguments of phis at the end of BB. */
325 static void
326 ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb)
328 struct mark_def_sites_global_data *gd = walk_data->global_data;
329 sbitmap kills = gd->kills;
330 edge e;
331 tree phi, use;
332 unsigned uid;
333 edge_iterator ei;
335 FOR_EACH_EDGE (e, ei, bb->succs)
337 if (e->dest == EXIT_BLOCK_PTR)
338 continue;
340 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
342 use = PHI_ARG_DEF_FROM_EDGE (phi, e);
343 if (TREE_CODE (use) != SSA_NAME)
344 continue;
346 uid = SSA_NAME_VERSION (use);
348 if (TEST_BIT (gd->names_to_rename, uid)
349 && !TEST_BIT (kills, uid))
350 set_livein_block (use, bb);
355 /* Call back for walk_dominator_tree used to collect definition sites
356 for every variable in the function. For every statement S in block
359 1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
360 WALK_DATA->GLOBAL_DATA->KILLS.
362 2- If S uses a variable VAR and there is no preceding kill of VAR,
363 then it is marked in marked in the LIVEIN_BLOCKS bitmap
364 associated with VAR.
366 This information is used to determine which variables are live
367 across block boundaries to reduce the number of PHI nodes
368 we create. */
370 static void
371 mark_def_sites (struct dom_walk_data *walk_data,
372 basic_block bb,
373 block_stmt_iterator bsi)
375 struct mark_def_sites_global_data *gd = walk_data->global_data;
376 sbitmap kills = gd->kills;
377 size_t uid;
378 tree stmt, def;
379 use_operand_p use_p;
380 def_operand_p def_p;
381 ssa_op_iter iter;
383 /* Mark all the blocks that have definitions for each variable in the
384 VARS_TO_RENAME bitmap. */
385 stmt = bsi_stmt (bsi);
386 get_stmt_operands (stmt);
388 REWRITE_THIS_STMT (stmt) = 0;
390 /* If a variable is used before being set, then the variable is live
391 across a block boundary, so mark it live-on-entry to BB. */
393 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
394 SSA_OP_USE | SSA_OP_VUSE | SSA_OP_VMUSTDEFKILL)
396 if (prepare_use_operand_for_rename (use_p, &uid))
398 REWRITE_THIS_STMT (stmt) = 1;
399 if (!TEST_BIT (kills, uid))
400 set_livein_block (USE_FROM_PTR (use_p), bb);
404 /* Note that virtual definitions are irrelevant for computing KILLS
405 because a V_MAY_DEF does not constitute a killing definition of the
406 variable. However, the operand of a virtual definitions is a use
407 of the variable, so it may cause the variable to be considered
408 live-on-entry. */
409 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
411 if (prepare_use_operand_for_rename (use_p, &uid))
413 /* If we do not already have an SSA_NAME for our destination,
414 then set the destination to the source. */
415 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
416 SET_DEF (def_p, USE_FROM_PTR (use_p));
418 set_livein_block (USE_FROM_PTR (use_p), bb);
419 set_def_block (DEF_FROM_PTR (def_p), bb, false, false);
420 REWRITE_THIS_STMT (stmt) = 1;
424 /* Now process the defs and must-defs made by this statement. */
425 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
427 if (prepare_def_operand_for_rename (def, &uid))
429 set_def_block (def, bb, false, false);
430 SET_BIT (kills, uid);
431 REWRITE_THIS_STMT (stmt) = 1;
437 /* Same as mark_def_sites, but works over SSA names. */
439 static void
440 ssa_mark_def_sites (struct dom_walk_data *walk_data,
441 basic_block bb,
442 block_stmt_iterator bsi)
444 struct mark_def_sites_global_data *gd = walk_data->global_data;
445 sbitmap kills = gd->kills;
446 size_t uid, def_uid;
447 tree stmt, use, def;
448 ssa_op_iter iter;
450 /* Mark all the blocks that have definitions for each variable in the
451 names_to_rename bitmap. */
452 stmt = bsi_stmt (bsi);
453 get_stmt_operands (stmt);
455 /* If a variable is used before being set, then the variable is live
456 across a block boundary, so mark it live-on-entry to BB. */
457 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
459 uid = SSA_NAME_VERSION (use);
461 if (TEST_BIT (gd->names_to_rename, uid)
462 && !TEST_BIT (kills, uid))
463 set_livein_block (use, bb);
466 /* Now process the definition made by this statement. Mark the
467 variables in KILLS. */
468 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
470 def_uid = SSA_NAME_VERSION (def);
472 if (TEST_BIT (gd->names_to_rename, def_uid))
474 set_def_block (def, bb, false, true);
475 SET_BIT (kills, def_uid);
480 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
481 VAR is defined by a phi node. SSA_P is true if we are called from
482 rewrite_ssa_into_ssa. */
484 static void
485 set_def_block (tree var, basic_block bb, bool phi_p, bool ssa_p)
487 struct def_blocks_d *db_p;
488 enum need_phi_state state;
490 if (!ssa_p
491 && TREE_CODE (var) == SSA_NAME)
492 var = SSA_NAME_VAR (var);
494 state = get_phi_state (var);
495 db_p = get_def_blocks_for (var);
497 /* Set the bit corresponding to the block where VAR is defined. */
498 bitmap_set_bit (db_p->def_blocks, bb->index);
499 if (phi_p)
500 bitmap_set_bit (db_p->phi_blocks, bb->index);
502 /* Keep track of whether or not we may need to insert phi nodes.
504 If we are in the UNKNOWN state, then this is the first definition
505 of VAR. Additionally, we have not seen any uses of VAR yet, so
506 we do not need a phi node for this variable at this time (i.e.,
507 transition to NEED_PHI_STATE_NO).
509 If we are in any other state, then we either have multiple definitions
510 of this variable occurring in different blocks or we saw a use of the
511 variable which was not dominated by the block containing the
512 definition(s). In this case we may need a PHI node, so enter
513 state NEED_PHI_STATE_MAYBE. */
514 if (state == NEED_PHI_STATE_UNKNOWN)
515 set_phi_state (var, NEED_PHI_STATE_NO);
516 else
517 set_phi_state (var, NEED_PHI_STATE_MAYBE);
521 /* Mark block BB as having VAR live at the entry to BB. */
523 static void
524 set_livein_block (tree var, basic_block bb)
526 struct def_blocks_d *db_p;
527 enum need_phi_state state = get_phi_state (var);
529 db_p = get_def_blocks_for (var);
531 /* Set the bit corresponding to the block where VAR is live in. */
532 bitmap_set_bit (db_p->livein_blocks, bb->index);
534 /* Keep track of whether or not we may need to insert phi nodes.
536 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
537 by the single block containing the definition(s) of this variable. If
538 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
539 NEED_PHI_STATE_MAYBE. */
540 if (state == NEED_PHI_STATE_NO)
542 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
544 if (def_block_index == -1
545 || ! dominated_by_p (CDI_DOMINATORS, bb,
546 BASIC_BLOCK (def_block_index)))
547 set_phi_state (var, NEED_PHI_STATE_MAYBE);
549 else
550 set_phi_state (var, NEED_PHI_STATE_MAYBE);
554 /* If the use operand pointed to by OP_P needs to be renamed, then strip away
555 any SSA_NAME wrapping the operand, set *UID_P to the underlying variable's
556 uid, and return true. Otherwise return false. If the operand was an
557 SSA_NAME, change it to the stripped name. */
559 static bool
560 prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p)
562 tree use = USE_FROM_PTR (op_p);
563 tree var = (TREE_CODE (use) != SSA_NAME) ? use : SSA_NAME_VAR (use);
564 *uid_p = var_ann (var)->uid;
566 /* Ignore variables that don't need to be renamed. */
567 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
568 return false;
570 /* The variable needs to be renamed. If this is a use which already
571 has an SSA_NAME, then strip it off.
573 By not throwing away SSA_NAMEs on assignments, we avoid a lot of
574 useless churn of SSA_NAMEs without having to overly complicate the
575 renamer. */
576 if (TREE_CODE (use) == SSA_NAME)
577 SET_USE (op_p, var);
579 return true;
582 /* If the def variable DEF needs to be renamed, then strip away any SSA_NAME
583 wrapping the operand, set *UID_P to the underlying variable's uid and return
584 true. Otherwise return false. */
586 static bool
587 prepare_def_operand_for_rename (tree def, size_t *uid_p)
589 tree var = (TREE_CODE (def) != SSA_NAME) ? def : SSA_NAME_VAR (def);
590 *uid_p = var_ann (var)->uid;
592 /* Ignore variables that don't need to be renamed. */
593 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
594 return false;
596 return true;
599 /* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
600 at the dominance frontier (DFS) of blocks defining VAR.
601 WORK_STACK is the vector used to implement the worklist of basic
602 blocks. */
604 static inline void
605 insert_phi_nodes_1 (tree var, bitmap *dfs, VEC(basic_block) **work_stack)
607 if (get_phi_state (var) != NEED_PHI_STATE_NO)
608 insert_phi_nodes_for (var, dfs, work_stack);
611 /* Insert PHI nodes at the dominance frontier of blocks with variable
612 definitions. DFS contains the dominance frontier information for
613 the flowgraph. PHI nodes will only be inserted at the dominance
614 frontier of definition blocks for variables whose NEED_PHI_STATE
615 annotation is marked as ``maybe'' or ``unknown'' (computed by
616 mark_def_sites). If NAMES_TO_RENAME is not NULL, do the same but
617 for ssa name rewriting. */
619 static void
620 insert_phi_nodes (bitmap *dfs, bitmap names_to_rename)
622 unsigned i;
623 VEC(basic_block) *work_stack;
624 bitmap_iterator bi;
626 timevar_push (TV_TREE_INSERT_PHI_NODES);
628 /* Vector WORK_STACK is a stack of CFG blocks. Each block that contains
629 an assignment or PHI node will be pushed to this stack. */
630 work_stack = VEC_alloc (basic_block, n_basic_blocks);
632 /* Iterate over all variables in VARS_TO_RENAME. For each variable, add
633 to the work list all the blocks that have a definition for the
634 variable. PHI nodes will be added to the dominance frontier blocks of
635 each definition block. */
636 if (names_to_rename)
638 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
640 if (ssa_name (i))
641 insert_phi_nodes_1 (ssa_name (i), dfs, &work_stack);
644 else if (vars_to_rename)
645 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
647 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
649 else
650 for (i = 0; i < num_referenced_vars; i++)
651 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
653 VEC_free (basic_block, work_stack);
655 timevar_pop (TV_TREE_INSERT_PHI_NODES);
659 /* Perform a depth-first traversal of the dominator tree looking for
660 variables to rename. BB is the block where to start searching.
661 Renaming is a five step process:
663 1- Every definition made by PHI nodes at the start of the blocks is
664 registered as the current definition for the corresponding variable.
666 2- Every statement in BB is rewritten. USE and VUSE operands are
667 rewritten with their corresponding reaching definition. DEF and
668 VDEF targets are registered as new definitions.
670 3- All the PHI nodes in successor blocks of BB are visited. The
671 argument corresponding to BB is replaced with its current reaching
672 definition.
674 4- Recursively rewrite every dominator child block of BB.
676 5- Restore (in reverse order) the current reaching definition for every
677 new definition introduced in this block. This is done so that when
678 we return from the recursive call, all the current reaching
679 definitions are restored to the names that were valid in the
680 dominator parent of BB. */
682 /* SSA Rewriting Step 1. Initialization, create a block local stack
683 of reaching definitions for new SSA names produced in this block
684 (BLOCK_DEFS). Register new definitions for every PHI node in the
685 block. */
687 static void
688 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
689 basic_block bb)
691 tree phi;
693 if (dump_file && (dump_flags & TDF_DETAILS))
694 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
696 /* Mark the unwind point for this block. */
697 VEC_safe_push (tree_on_heap, block_defs_stack, NULL_TREE);
699 /* Step 1. Register new definitions for every PHI node in the block.
700 Conceptually, all the PHI nodes are executed in parallel and each PHI
701 node introduces a new version for the associated variable. */
702 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
704 tree result = PHI_RESULT (phi);
706 register_new_def (result, &block_defs_stack);
710 /* Register DEF (an SSA_NAME) to be a new definition for the original
711 ssa name VAR and push VAR's current reaching definition
712 into the stack pointed by BLOCK_DEFS_P. */
714 static void
715 ssa_register_new_def (tree var, tree def)
717 tree currdef;
719 /* If this variable is set in a single basic block and all uses are
720 dominated by the set(s) in that single basic block, then there is
721 nothing to do. TODO we should not be called at all, and just
722 keep the original name. */
723 if (get_phi_state (var) == NEED_PHI_STATE_NO)
725 set_current_def (var, def);
726 return;
729 currdef = get_current_def (var);
731 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
732 later used by the dominator tree callbacks to restore the reaching
733 definitions for all the variables defined in the block after a recursive
734 visit to all its immediately dominated blocks. */
735 VEC_safe_push (tree_on_heap, block_defs_stack, currdef);
736 VEC_safe_push (tree_on_heap, block_defs_stack, var);
738 /* Set the current reaching definition for VAR to be DEF. */
739 set_current_def (var, def);
742 /* Ditto, for rewriting ssa names. */
744 static void
745 ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
747 tree phi, new_name;
748 sbitmap names_to_rename = walk_data->global_data;
749 edge e;
750 bool abnormal_phi;
751 edge_iterator ei;
753 if (dump_file && (dump_flags & TDF_DETAILS))
754 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
756 /* Mark the unwind point for this block. */
757 VEC_safe_push (tree_on_heap, block_defs_stack, NULL_TREE);
759 FOR_EACH_EDGE (e, ei, bb->preds)
760 if (e->flags & EDGE_ABNORMAL)
761 break;
762 abnormal_phi = (e != NULL);
764 /* Step 1. Register new definitions for every PHI node in the block.
765 Conceptually, all the PHI nodes are executed in parallel and each PHI
766 node introduces a new version for the associated variable. */
767 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
769 tree result = PHI_RESULT (phi);
771 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result)))
773 new_name = duplicate_ssa_name (result, phi);
774 SET_PHI_RESULT (phi, new_name);
776 if (abnormal_phi)
777 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
779 else
780 new_name = result;
782 ssa_register_new_def (result, new_name);
786 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
787 PHI nodes. For every PHI node found, add a new argument containing the
788 current reaching definition for the variable and the edge through which
789 that definition is reaching the PHI node. */
791 static void
792 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
793 basic_block bb)
795 edge e;
796 edge_iterator ei;
798 FOR_EACH_EDGE (e, ei, bb->succs)
800 tree phi;
802 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
804 tree currdef;
806 /* If this PHI node has already been rewritten, then there is
807 nothing to do for this PHI or any following PHIs since we
808 always add new PHI nodes at the start of the PHI chain. */
809 if (PHI_REWRITTEN (phi))
810 break;
812 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
813 add_phi_arg (phi, currdef, e);
818 /* Rewrite existing virtual PHI arguments so that they have the correct
819 reaching definitions. BB is the basic block whose successors contain the
820 phi nodes we want to add arguments for. */
822 static void
823 rewrite_virtual_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
824 basic_block bb)
826 edge e;
827 use_operand_p op;
828 edge_iterator ei;
830 FOR_EACH_EDGE (e, ei, bb->succs)
832 tree phi;
834 if (e->dest == EXIT_BLOCK_PTR)
835 continue;
837 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
839 tree result = PHI_RESULT (phi);
840 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
842 if (is_gimple_reg (result)
843 || !bitmap_bit_p (vars_to_rename,
844 var_ann (SSA_NAME_VAR (result))->uid))
845 continue;
847 SET_USE (op, get_reaching_def (SSA_NAME_VAR (result)));
848 if (e->flags & EDGE_ABNORMAL)
849 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
854 /* Ditto, for ssa name rewriting. */
856 static void
857 ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb)
859 edge e;
860 sbitmap names_to_rename = walk_data->global_data;
861 use_operand_p op;
862 edge_iterator ei;
864 FOR_EACH_EDGE (e, ei, bb->succs)
866 tree phi;
868 if (e->dest == EXIT_BLOCK_PTR)
869 continue;
871 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
873 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
874 if (TREE_CODE (USE_FROM_PTR (op)) != SSA_NAME)
875 continue;
877 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (op))))
878 continue;
880 SET_USE (op, get_reaching_def (USE_FROM_PTR (op)));
881 if (e->flags & EDGE_ABNORMAL)
882 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
888 /* Similar to restore_vars_to_original_value, except that it restores
889 CURRDEFS to its original value. */
890 static void
891 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
892 basic_block bb ATTRIBUTE_UNUSED)
894 /* Restore CURRDEFS to its original state. */
895 while (VEC_length (tree_on_heap, block_defs_stack) > 0)
897 tree tmp = VEC_pop (tree_on_heap, block_defs_stack);
898 tree saved_def, var;
900 if (tmp == NULL_TREE)
901 break;
903 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
904 definition of its underlying variable. If we recorded anything
905 else, it must have been an _DECL node and its current reaching
906 definition must have been NULL. */
907 if (TREE_CODE (tmp) == SSA_NAME)
909 saved_def = tmp;
910 var = SSA_NAME_VAR (saved_def);
912 else
914 saved_def = NULL;
915 var = tmp;
918 set_current_def (var, saved_def);
922 /* Ditto, for rewriting ssa names. */
924 static void
925 ssa_rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
926 basic_block bb ATTRIBUTE_UNUSED)
929 /* Step 5. Restore the current reaching definition for each variable
930 referenced in the block (in reverse order). */
931 while (VEC_length (tree_on_heap, block_defs_stack) > 0)
933 tree var = VEC_pop (tree_on_heap, block_defs_stack);
934 tree saved_def;
936 if (var == NULL)
937 break;
939 saved_def = VEC_pop (tree_on_heap, block_defs_stack);
941 set_current_def (var, saved_def);
945 /* Dump SSA information to FILE. */
947 void
948 dump_tree_ssa (FILE *file)
950 basic_block bb;
951 const char *funcname
952 = lang_hooks.decl_printable_name (current_function_decl, 2);
954 fprintf (file, "SSA information for %s\n\n", funcname);
956 FOR_EACH_BB (bb)
958 dump_bb (bb, file, 0);
959 fputs (" ", file);
960 print_generic_stmt (file, phi_nodes (bb), dump_flags);
961 fputs ("\n\n", file);
966 /* Dump SSA information to stderr. */
968 void
969 debug_tree_ssa (void)
971 dump_tree_ssa (stderr);
975 /* Dump SSA statistics on FILE. */
977 void
978 dump_tree_ssa_stats (FILE *file)
980 fprintf (file, "\nHash table statistics:\n");
982 fprintf (file, " def_blocks: ");
983 htab_statistics (file, def_blocks);
985 fprintf (file, "\n");
989 /* Dump SSA statistics on stderr. */
991 void
992 debug_tree_ssa_stats (void)
994 dump_tree_ssa_stats (stderr);
998 /* Dump statistics for the hash table HTAB. */
1000 static void
1001 htab_statistics (FILE *file, htab_t htab)
1003 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1004 (long) htab_size (htab),
1005 (long) htab_elements (htab),
1006 htab_collisions (htab));
1010 /* Insert PHI nodes for variable VAR using the dominance frontier
1011 information given in DFS. WORK_STACK is the vector used to
1012 implement the worklist of basic blocks. */
1014 static void
1015 insert_phi_nodes_for (tree var, bitmap *dfs, VEC(basic_block) **work_stack)
1017 struct def_blocks_d *def_map;
1018 bitmap phi_insertion_points;
1019 unsigned bb_index;
1020 edge e;
1021 tree phi;
1022 basic_block bb;
1023 bitmap_iterator bi;
1025 def_map = find_def_blocks_for (var);
1026 if (def_map == NULL)
1027 return;
1029 phi_insertion_points = BITMAP_XMALLOC ();
1031 EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index, bi)
1033 VEC_safe_push (basic_block, *work_stack, BASIC_BLOCK (bb_index));
1036 /* Pop a block off the worklist, add every block that appears in
1037 the original block's dfs that we have not already processed to
1038 the worklist. Iterate until the worklist is empty. Blocks
1039 which are added to the worklist are potential sites for
1040 PHI nodes.
1042 The iteration step could be done during PHI insertion just as
1043 easily. We do it here for historical reasons -- we used to have
1044 a heuristic which used the potential PHI insertion points to
1045 determine if fully pruned or semi pruned SSA form was appropriate.
1047 We now always use fully pruned SSA form. */
1048 while (VEC_length (basic_block, *work_stack) > 0)
1050 unsigned dfs_index;
1051 bitmap_iterator bi;
1053 bb = VEC_pop (basic_block, *work_stack);
1054 bb_index = bb->index;
1056 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
1057 phi_insertion_points,
1058 0, dfs_index, bi)
1060 basic_block bb = BASIC_BLOCK (dfs_index);
1062 /* Use a safe push because if there is a definition of VAR
1063 in every basic block, then WORK_STACK may eventually have
1064 more than N_BASIC_BLOCK entries. */
1065 VEC_safe_push (basic_block, *work_stack, bb);
1066 bitmap_set_bit (phi_insertion_points, dfs_index);
1070 /* Remove the blocks where we already have the phis. */
1071 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1073 /* Now compute global livein for this variable. Note this modifies
1074 def_map->livein_blocks. */
1075 compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
1077 /* And insert the PHI nodes. */
1078 EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
1079 0, bb_index, bi)
1081 bb = BASIC_BLOCK (bb_index);
1083 phi = create_phi_node (var, bb);
1085 /* If we are rewriting ssa names, add also the phi arguments. */
1086 if (TREE_CODE (var) == SSA_NAME)
1088 edge_iterator ei;
1089 FOR_EACH_EDGE (e, ei, bb->preds)
1090 add_phi_arg (phi, var, e);
1094 BITMAP_XFREE (phi_insertion_points);
1097 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1098 the block with its immediate reaching definitions. Update the current
1099 definition of a variable when a new real or virtual definition is found. */
1101 static void
1102 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1103 basic_block bb ATTRIBUTE_UNUSED,
1104 block_stmt_iterator si)
1106 stmt_ann_t ann;
1107 tree stmt;
1108 use_operand_p use_p;
1109 def_operand_p def_p;
1110 ssa_op_iter iter;
1112 stmt = bsi_stmt (si);
1113 ann = stmt_ann (stmt);
1115 /* If mark_def_sites decided that we don't need to rewrite this
1116 statement, ignore it. */
1117 if (!REWRITE_THIS_STMT (stmt))
1118 return;
1120 if (dump_file && (dump_flags & TDF_DETAILS))
1122 fprintf (dump_file, "Renaming statement ");
1123 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1124 fprintf (dump_file, "\n");
1127 /* We have just scanned the code for operands. No statement should
1128 be modified. */
1129 gcc_assert (!ann->modified);
1131 /* Step 1. Rewrite USES and VUSES in the statement. */
1132 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
1133 rewrite_operand (use_p);
1135 /* Step 2. Register the statement's DEF and VDEF operands. */
1136 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1138 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
1139 SET_DEF (def_p, make_ssa_name (DEF_FROM_PTR (def_p), stmt));
1141 /* FIXME: We shouldn't be registering new defs if the variable
1142 doesn't need to be renamed. */
1143 register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
1148 /* Same as rewrite_stmt, for rewriting ssa names. */
1150 static void
1151 ssa_rewrite_stmt (struct dom_walk_data *walk_data,
1152 basic_block bb ATTRIBUTE_UNUSED,
1153 block_stmt_iterator si)
1155 stmt_ann_t ann;
1156 tree stmt, var;
1157 ssa_op_iter iter;
1158 use_operand_p use_p;
1159 def_operand_p def_p;
1160 sbitmap names_to_rename = walk_data->global_data;
1162 stmt = bsi_stmt (si);
1163 ann = stmt_ann (stmt);
1165 if (dump_file && (dump_flags & TDF_DETAILS))
1167 fprintf (dump_file, "Renaming statement ");
1168 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1169 fprintf (dump_file, "\n");
1172 /* We have just scanned the code for operands. No statement should
1173 be modified. */
1174 gcc_assert (!ann->modified);
1176 /* Step 1. Rewrite USES and VUSES in the statement. */
1177 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
1179 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (use_p))))
1180 SET_USE (use_p, get_reaching_def (USE_FROM_PTR (use_p)));
1183 /* Step 2. Register the statement's DEF and VDEF operands. */
1184 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1186 var = DEF_FROM_PTR (def_p);
1188 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (var)))
1189 continue;
1191 SET_DEF (def_p, duplicate_ssa_name (var, stmt));
1192 ssa_register_new_def (var, DEF_FROM_PTR (def_p));
1196 /* Replace the operand pointed by OP_P with its immediate reaching
1197 definition. */
1199 static inline void
1200 rewrite_operand (use_operand_p op_p)
1202 tree var = USE_FROM_PTR (op_p);
1203 if (TREE_CODE (var) != SSA_NAME)
1204 SET_USE (op_p, get_reaching_def (var));
1205 else
1207 #if defined ENABLE_CHECKING
1208 /* If we get to this point, VAR is an SSA_NAME. If VAR's symbol
1209 was marked for renaming, make sure that its reaching
1210 definition is VAR itself. Otherwise, something has gone
1211 wrong. */
1212 tree sym = SSA_NAME_VAR (var);
1213 if (bitmap_bit_p (vars_to_rename, var_ann (sym)->uid))
1214 gcc_assert (var == get_reaching_def (SSA_NAME_VAR (var)));
1215 #endif
1219 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1220 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1221 into the stack pointed by BLOCK_DEFS_P. */
1223 void
1224 register_new_def (tree def, VEC (tree_on_heap) **block_defs_p)
1226 tree var = SSA_NAME_VAR (def);
1227 tree currdef;
1229 /* If this variable is set in a single basic block and all uses are
1230 dominated by the set(s) in that single basic block, then there is
1231 no reason to record anything for this variable in the block local
1232 definition stacks. Doing so just wastes time and memory.
1234 This is the same test to prune the set of variables which may
1235 need PHI nodes. So we just use that information since it's already
1236 computed and available for us to use. */
1237 if (get_phi_state (var) == NEED_PHI_STATE_NO)
1239 set_current_def (var, def);
1240 return;
1243 currdef = get_current_def (var);
1245 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
1246 later used by the dominator tree callbacks to restore the reaching
1247 definitions for all the variables defined in the block after a recursive
1248 visit to all its immediately dominated blocks. If there is no current
1249 reaching definition, then just record the underlying _DECL node. */
1250 VEC_safe_push (tree_on_heap, *block_defs_p, currdef ? currdef : var);
1252 /* Set the current reaching definition for VAR to be DEF. */
1253 set_current_def (var, def);
1256 /* Return the current definition for variable VAR. If none is found,
1257 create a new SSA name to act as the zeroth definition for VAR. If VAR
1258 is call clobbered and there exists a more recent definition of
1259 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
1260 has been clobbered by a function call since its last assignment. */
1262 static tree
1263 get_reaching_def (tree var)
1265 tree default_d, currdef_var, avar;
1267 /* Lookup the current reaching definition for VAR. */
1268 default_d = NULL_TREE;
1269 currdef_var = get_current_def (var);
1271 /* If there is no reaching definition for VAR, create and register a
1272 default definition for it (if needed). */
1273 if (currdef_var == NULL_TREE)
1275 if (TREE_CODE (var) == SSA_NAME)
1276 avar = SSA_NAME_VAR (var);
1277 else
1278 avar = var;
1280 default_d = default_def (avar);
1281 if (default_d == NULL_TREE)
1283 default_d = make_ssa_name (avar, build_empty_stmt ());
1284 set_default_def (avar, default_d);
1286 set_current_def (var, default_d);
1289 /* Return the current reaching definition for VAR, or the default
1290 definition, if we had to create one. */
1291 return (currdef_var) ? currdef_var : default_d;
1295 /* Hashing and equality functions for DEF_BLOCKS. */
1297 static hashval_t
1298 def_blocks_hash (const void *p)
1300 return htab_hash_pointer
1301 ((const void *)((const struct def_blocks_d *)p)->var);
1304 static int
1305 def_blocks_eq (const void *p1, const void *p2)
1307 return ((const struct def_blocks_d *)p1)->var
1308 == ((const struct def_blocks_d *)p2)->var;
1311 /* Free memory allocated by one entry in DEF_BLOCKS. */
1313 static void
1314 def_blocks_free (void *p)
1316 struct def_blocks_d *entry = p;
1317 BITMAP_XFREE (entry->def_blocks);
1318 BITMAP_XFREE (entry->phi_blocks);
1319 BITMAP_XFREE (entry->livein_blocks);
1320 free (entry);
1324 /* Dump the DEF_BLOCKS hash table on stderr. */
1326 void
1327 debug_def_blocks (void)
1329 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1332 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1334 static int
1335 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1337 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1339 fprintf (stderr, "VAR: ");
1340 print_generic_expr (stderr, db_p->var, dump_flags);
1341 bitmap_print (stderr, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1342 bitmap_print (stderr, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}\n");
1344 return 1;
1348 /* Return the set of blocks where variable VAR is defined and the blocks
1349 where VAR is live on entry (livein). Return NULL, if no entry is
1350 found in DEF_BLOCKS. */
1352 static inline struct def_blocks_d *
1353 find_def_blocks_for (tree var)
1355 struct def_blocks_d dm;
1356 dm.var = var;
1357 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1361 /* Return the set of blocks where variable VAR is defined and the blocks
1362 where VAR is live on entry (livein). If no entry is found in
1363 DEF_BLOCKS, a new one is created and returned. */
1365 static inline struct def_blocks_d *
1366 get_def_blocks_for (tree var)
1368 struct def_blocks_d db, *db_p;
1369 void **slot;
1371 db.var = var;
1372 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
1373 if (*slot == NULL)
1375 db_p = xmalloc (sizeof (*db_p));
1376 db_p->var = var;
1377 db_p->def_blocks = BITMAP_XMALLOC ();
1378 db_p->phi_blocks = BITMAP_XMALLOC ();
1379 db_p->livein_blocks = BITMAP_XMALLOC ();
1380 *slot = (void *) db_p;
1382 else
1383 db_p = (struct def_blocks_d *) *slot;
1385 return db_p;
1388 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
1389 process will cause us to lose the name memory tags that may have
1390 been associated with the various SSA_NAMEs of V. This means that
1391 the variables aliased to those name tags also need to be renamed
1392 again.
1394 FIXME 1- We should either have a better scheme for renaming
1395 pointers that doesn't lose name tags or re-run alias
1396 analysis to recover points-to information.
1398 2- Currently we just invalidate *all* the name tags. This
1399 should be more selective. */
1401 static void
1402 invalidate_name_tags (bitmap vars_to_rename)
1404 unsigned i;
1405 bool rename_name_tags_p;
1406 bitmap_iterator bi;
1408 rename_name_tags_p = false;
1409 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
1411 if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i))))
1413 rename_name_tags_p = true;
1414 break;
1418 if (rename_name_tags_p)
1419 for (i = 0; i < num_referenced_vars; i++)
1421 var_ann_t ann = var_ann (referenced_var (i));
1423 if (ann->mem_tag_kind == NAME_TAG)
1425 size_t j;
1426 varray_type may_aliases = ann->may_aliases;
1428 bitmap_set_bit (vars_to_rename, ann->uid);
1429 if (ann->may_aliases)
1430 for (j = 0; j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
1432 tree var = VARRAY_TREE (may_aliases, j);
1433 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1439 /* Rewrite the actual blocks, statements, and phi arguments, to be in SSA
1440 form. FIX_VIRTUAL_PHIS is true if we should only be fixing up virtual
1441 phi arguments, instead of adding new phi arguments for just added phi
1442 nodes. */
1445 static void
1446 rewrite_blocks (bool fix_virtual_phis)
1448 struct dom_walk_data walk_data;
1450 /* Rewrite all the basic blocks in the program. */
1451 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1453 /* Setup callbacks for the generic dominator tree walker. */
1454 walk_data.walk_stmts_backward = false;
1455 walk_data.dom_direction = CDI_DOMINATORS;
1456 walk_data.initialize_block_local_data = NULL;
1457 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1458 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1459 walk_data.before_dom_children_after_stmts = NULL;
1460 if (!fix_virtual_phis)
1461 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1462 else
1463 walk_data.before_dom_children_after_stmts = rewrite_virtual_phi_arguments;
1465 walk_data.after_dom_children_before_stmts = NULL;
1466 walk_data.after_dom_children_walk_stmts = NULL;
1467 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1468 walk_data.global_data = NULL;
1469 walk_data.block_local_data_size = 0;
1471 block_defs_stack = VEC_alloc (tree_on_heap, 10);
1473 /* Initialize the dominator walker. */
1474 init_walk_dominator_tree (&walk_data);
1476 /* Recursively walk the dominator tree rewriting each statement in
1477 each basic block. */
1478 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1480 /* Finalize the dominator walker. */
1481 fini_walk_dominator_tree (&walk_data);
1483 htab_delete (def_blocks);
1485 VEC_free (tree_on_heap, block_defs_stack);
1486 block_defs_stack = NULL;
1488 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1491 /* Mark the definition site blocks for each variable, so that we know where
1492 the variable is actually live. */
1494 static void
1495 mark_def_site_blocks (void)
1497 size_t i;
1498 struct dom_walk_data walk_data;
1499 struct mark_def_sites_global_data mark_def_sites_global_data;
1501 /* Allocate memory for the DEF_BLOCKS hash table. */
1502 def_blocks = htab_create (VARRAY_ACTIVE_SIZE (referenced_vars),
1503 def_blocks_hash, def_blocks_eq, def_blocks_free);
1505 for (i = 0; i < num_referenced_vars; i++)
1506 set_current_def (referenced_var (i), NULL_TREE);
1508 /* Ensure that the dominance information is OK. */
1509 calculate_dominance_info (CDI_DOMINATORS);
1512 /* Setup callbacks for the generic dominator tree walker to find and
1513 mark definition sites. */
1514 walk_data.walk_stmts_backward = false;
1515 walk_data.dom_direction = CDI_DOMINATORS;
1516 walk_data.initialize_block_local_data = NULL;
1517 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
1518 walk_data.before_dom_children_walk_stmts = mark_def_sites;
1519 walk_data.before_dom_children_after_stmts = NULL;
1520 walk_data.after_dom_children_before_stmts = NULL;
1521 walk_data.after_dom_children_walk_stmts = NULL;
1522 walk_data.after_dom_children_after_stmts = NULL;
1524 /* Notice that this bitmap is indexed using variable UIDs, so it must be
1525 large enough to accommodate all the variables referenced in the
1526 function, not just the ones we are renaming. */
1527 mark_def_sites_global_data.kills = sbitmap_alloc (num_referenced_vars);
1528 walk_data.global_data = &mark_def_sites_global_data;
1530 /* We do not have any local data. */
1531 walk_data.block_local_data_size = 0;
1533 /* Initialize the dominator walker. */
1534 init_walk_dominator_tree (&walk_data);
1536 /* Recursively walk the dominator tree. */
1537 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1539 /* Finalize the dominator walker. */
1540 fini_walk_dominator_tree (&walk_data);
1542 /* We no longer need this bitmap, clear and free it. */
1543 sbitmap_free (mark_def_sites_global_data.kills);
1547 /* Main entry point into the SSA builder. The renaming process
1548 proceeds in five main phases:
1550 1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1551 those variables are removed from the flow graph so that they can
1552 be computed again.
1554 2- Compute dominance frontier and immediate dominators, needed to
1555 insert PHI nodes and rename the function in dominator tree
1556 order.
1558 3- Find and mark all the blocks that define variables
1559 (mark_def_site_blocks).
1561 4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1563 5- Rename all the blocks (rewrite_blocks) and statements in the program.
1565 Steps 3 and 5 are done using the dominator tree walker
1566 (walk_dominator_tree).
1568 ALL is true if all variables should be renamed (otherwise just those
1569 mentioned in vars_to_rename are taken into account). */
1571 void
1572 rewrite_into_ssa (bool all)
1574 bitmap *dfs;
1575 basic_block bb;
1576 bitmap old_vars_to_rename = vars_to_rename;
1578 timevar_push (TV_TREE_SSA_OTHER);
1580 if (all)
1581 vars_to_rename = NULL;
1582 else
1584 /* Initialize the array of variables to rename. */
1585 gcc_assert (vars_to_rename);
1587 if (bitmap_empty_p (vars_to_rename))
1589 timevar_pop (TV_TREE_SSA_OTHER);
1590 return;
1593 invalidate_name_tags (vars_to_rename);
1595 /* Now remove all the existing PHI nodes (if any) for the variables
1596 that we are about to rename into SSA. */
1597 remove_all_phi_nodes_for (vars_to_rename);
1600 mark_def_site_blocks ();
1602 /* Initialize dominance frontier and immediate dominator bitmaps.
1603 Also count the number of predecessors for each block. Doing so
1604 can save significant time during PHI insertion for large graphs. */
1605 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1606 FOR_EACH_BB (bb)
1607 dfs[bb->index] = BITMAP_XMALLOC ();
1609 /* Compute dominance frontiers. */
1610 compute_dominance_frontiers (dfs);
1612 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1613 insert_phi_nodes (dfs, NULL);
1615 rewrite_blocks (false);
1617 /* Debugging dumps. */
1618 if (dump_file && (dump_flags & TDF_STATS))
1620 dump_dfa_stats (dump_file);
1621 dump_tree_ssa_stats (dump_file);
1624 /* Free allocated memory. */
1625 FOR_EACH_BB (bb)
1626 BITMAP_XFREE (dfs[bb->index]);
1627 free (dfs);
1629 vars_to_rename = old_vars_to_rename;
1630 timevar_pop (TV_TREE_SSA_OTHER);
1633 /* Rewrite the def-def chains so that they have the correct reaching
1634 definitions. */
1636 void
1637 rewrite_def_def_chains (void)
1639 /* Ensure that the dominance information is OK. */
1640 calculate_dominance_info (CDI_DOMINATORS);
1641 mark_def_site_blocks ();
1642 rewrite_blocks (true);
1645 /* The marked ssa names may have more than one definition;
1646 add phi nodes and rewrite them to fix this. */
1648 void
1649 rewrite_ssa_into_ssa (void)
1651 bitmap *dfs;
1652 basic_block bb;
1653 struct dom_walk_data walk_data;
1654 struct mark_def_sites_global_data mark_def_sites_global_data;
1655 unsigned i;
1656 sbitmap snames_to_rename;
1657 tree name;
1658 bitmap to_rename;
1659 bitmap_iterator bi;
1661 if (!any_marked_for_rewrite_p ())
1662 return;
1663 to_rename = marked_ssa_names ();
1665 timevar_push (TV_TREE_SSA_OTHER);
1667 /* Allocate memory for the DEF_BLOCKS hash table. */
1668 def_blocks = htab_create (num_ssa_names,
1669 def_blocks_hash, def_blocks_eq, def_blocks_free);
1671 /* Initialize dominance frontier and immediate dominator bitmaps.
1672 Also count the number of predecessors for each block. Doing so
1673 can save significant time during PHI insertion for large graphs. */
1674 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1675 FOR_EACH_BB (bb)
1676 dfs[bb->index] = BITMAP_XMALLOC ();
1678 /* Ensure that the dominance information is OK. */
1679 calculate_dominance_info (CDI_DOMINATORS);
1681 /* Compute dominance frontiers. */
1682 compute_dominance_frontiers (dfs);
1684 /* Setup callbacks for the generic dominator tree walker to find and
1685 mark definition sites. */
1686 walk_data.walk_stmts_backward = false;
1687 walk_data.dom_direction = CDI_DOMINATORS;
1688 walk_data.initialize_block_local_data = NULL;
1689 walk_data.before_dom_children_before_stmts
1690 = ssa_mark_def_sites_initialize_block;
1691 walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites;
1692 walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses;
1693 walk_data.after_dom_children_before_stmts = NULL;
1694 walk_data.after_dom_children_walk_stmts = NULL;
1695 walk_data.after_dom_children_after_stmts = NULL;
1697 snames_to_rename = sbitmap_alloc (num_ssa_names);
1698 sbitmap_zero (snames_to_rename);
1699 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1701 SET_BIT (snames_to_rename, i);
1704 mark_def_sites_global_data.kills = sbitmap_alloc (num_ssa_names);
1705 mark_def_sites_global_data.names_to_rename = snames_to_rename;
1706 walk_data.global_data = &mark_def_sites_global_data;
1708 block_defs_stack = VEC_alloc (tree_on_heap, 10);
1710 /* We do not have any local data. */
1711 walk_data.block_local_data_size = 0;
1713 /* Initialize the dominator walker. */
1714 init_walk_dominator_tree (&walk_data);
1716 /* Recursively walk the dominator tree. */
1717 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1719 /* Finalize the dominator walker. */
1720 fini_walk_dominator_tree (&walk_data);
1722 /* We no longer need this bitmap, clear and free it. */
1723 sbitmap_free (mark_def_sites_global_data.kills);
1725 for (i = 1; i < num_ssa_names; i++)
1726 if (ssa_name (i))
1727 set_current_def (ssa_name (i), NULL_TREE);
1729 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1730 insert_phi_nodes (dfs, to_rename);
1732 /* Rewrite all the basic blocks in the program. */
1733 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1735 /* Setup callbacks for the generic dominator tree walker. */
1736 walk_data.walk_stmts_backward = false;
1737 walk_data.dom_direction = CDI_DOMINATORS;
1738 walk_data.initialize_block_local_data = NULL;
1739 walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block;
1740 walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt;
1741 walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments;
1742 walk_data.after_dom_children_before_stmts = NULL;
1743 walk_data.after_dom_children_walk_stmts = NULL;
1744 walk_data.after_dom_children_after_stmts = ssa_rewrite_finalize_block;
1745 walk_data.global_data = snames_to_rename;
1746 walk_data.block_local_data_size = 0;
1748 /* Initialize the dominator walker. */
1749 init_walk_dominator_tree (&walk_data);
1751 /* Recursively walk the dominator tree rewriting each statement in
1752 each basic block. */
1753 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1755 /* Finalize the dominator walker. */
1756 fini_walk_dominator_tree (&walk_data);
1758 unmark_all_for_rewrite ();
1760 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1762 /* Free SSA_NAME_AUX. We don't have to zero it because
1763 release_ssa_name will. */
1764 if (SSA_NAME_AUX (ssa_name (i)))
1765 free (SSA_NAME_AUX (ssa_name (i)));
1767 release_ssa_name (ssa_name (i));
1770 sbitmap_free (snames_to_rename);
1772 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1774 /* Debugging dumps. */
1775 if (dump_file && (dump_flags & TDF_STATS))
1777 dump_dfa_stats (dump_file);
1778 dump_tree_ssa_stats (dump_file);
1781 /* Free allocated memory. */
1782 FOR_EACH_BB (bb)
1783 BITMAP_XFREE (dfs[bb->index]);
1784 free (dfs);
1786 htab_delete (def_blocks);
1788 for (i = 1; i < num_ssa_names; i++)
1790 name = ssa_name (i);
1791 if (!name || !SSA_NAME_AUX (name))
1792 continue;
1794 free (SSA_NAME_AUX (name));
1795 SSA_NAME_AUX (name) = NULL;
1798 BITMAP_XFREE (to_rename);
1800 VEC_free (tree_on_heap, block_defs_stack);
1801 block_defs_stack = NULL;
1802 timevar_pop (TV_TREE_SSA_OTHER);
1805 /* Rewrites all variables into ssa. */
1807 static void
1808 rewrite_all_into_ssa (void)
1810 rewrite_into_ssa (true);
1813 struct tree_opt_pass pass_build_ssa =
1815 "ssa", /* name */
1816 NULL, /* gate */
1817 rewrite_all_into_ssa, /* execute */
1818 NULL, /* sub */
1819 NULL, /* next */
1820 0, /* static_pass_number */
1821 0, /* tv_id */
1822 PROP_cfg | PROP_referenced_vars, /* properties_required */
1823 PROP_ssa, /* properties_provided */
1824 0, /* properties_destroyed */
1825 0, /* todo_flags_start */
1826 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
1827 0 /* letter */