* config/arm/symbian.h (STARTFILE_SPEC): Remove crt*.o.
[official-gcc.git] / gcc / tree-into-ssa.c
blob841938db1a91065b0141f4590b70a29446fb8fe1
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 varray 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 varray 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 varray_type block_defs_stack;
114 /* Global data to attach to the main dominator walk structure. */
115 struct mark_def_sites_global_data
117 /* This sbitmap contains the variables which are set before they
118 are used in a basic block. We keep it as a global variable
119 solely to avoid the overhead of allocating and deallocating
120 the bitmap. */
121 sbitmap kills;
123 /* Bitmap of names to rename. */
124 sbitmap names_to_rename;
127 /* Information stored for ssa names. */
129 struct ssa_name_info
131 /* This field indicates whether or not the variable may need PHI nodes.
132 See the enum's definition for more detailed information about the
133 states. */
134 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
136 /* The actual definition of the ssa name. */
137 tree current_def;
140 /* Local functions. */
141 static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
142 static void rewrite_initialize_block (struct dom_walk_data *, basic_block);
143 static void rewrite_add_phi_arguments (struct dom_walk_data *, basic_block);
144 static void mark_def_sites (struct dom_walk_data *walk_data,
145 basic_block bb, block_stmt_iterator);
146 static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
147 basic_block bb);
148 static void set_def_block (tree, basic_block, bool, bool);
149 static void set_livein_block (tree, basic_block);
150 static bool prepare_use_operand_for_rename (use_operand_p, size_t *uid_p);
151 static bool prepare_def_operand_for_rename (tree def, size_t *uid_p);
152 static void insert_phi_nodes (bitmap *, bitmap);
153 static void rewrite_stmt (struct dom_walk_data *, basic_block,
154 block_stmt_iterator);
155 static inline void rewrite_operand (use_operand_p);
156 static void insert_phi_nodes_for (tree, bitmap *, varray_type *);
157 static tree get_reaching_def (tree);
158 static hashval_t def_blocks_hash (const void *);
159 static int def_blocks_eq (const void *, const void *);
160 static void def_blocks_free (void *);
161 static int debug_def_blocks_r (void **, void *);
162 static inline struct def_blocks_d *get_def_blocks_for (tree);
163 static inline struct def_blocks_d *find_def_blocks_for (tree);
164 static void htab_statistics (FILE *, htab_t);
166 /* Get the information associated with NAME. */
168 static inline struct ssa_name_info *
169 get_ssa_name_ann (tree name)
171 if (!SSA_NAME_AUX (name))
172 SSA_NAME_AUX (name) = xcalloc (1, sizeof (struct ssa_name_info));
174 return SSA_NAME_AUX (name);
177 /* Gets phi_state field for VAR. */
179 static inline enum need_phi_state
180 get_phi_state (tree var)
182 if (TREE_CODE (var) == SSA_NAME)
183 return get_ssa_name_ann (var)->need_phi_state;
184 else
185 return var_ann (var)->need_phi_state;
188 /* Sets phi_state field for VAR to STATE. */
190 static inline void
191 set_phi_state (tree var, enum need_phi_state state)
193 if (TREE_CODE (var) == SSA_NAME)
194 get_ssa_name_ann (var)->need_phi_state = state;
195 else
196 var_ann (var)->need_phi_state = state;
199 /* Return the current definition for VAR. */
201 static inline tree
202 get_current_def (tree var)
204 if (TREE_CODE (var) == SSA_NAME)
205 return get_ssa_name_ann (var)->current_def;
206 else
207 return var_ann (var)->current_def;
210 /* Sets current definition of VAR to DEF. */
212 static inline void
213 set_current_def (tree var, tree def)
215 if (TREE_CODE (var) == SSA_NAME)
216 get_ssa_name_ann (var)->current_def = def;
217 else
218 var_ann (var)->current_def = def;
221 /* Compute global livein information given the set of blockx where
222 an object is locally live at the start of the block (LIVEIN)
223 and the set of blocks where the object is defined (DEF_BLOCKS).
225 Note: This routine augments the existing local livein information
226 to include global livein (i.e., it modifies the underlying bitmap
227 for LIVEIN). */
229 void
230 compute_global_livein (bitmap livein, bitmap def_blocks)
232 basic_block bb, *worklist, *tos;
233 int i;
234 bitmap_iterator bi;
236 tos = worklist
237 = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
239 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
241 *tos++ = BASIC_BLOCK (i);
244 /* Iterate until the worklist is empty. */
245 while (tos != worklist)
247 edge e;
248 edge_iterator ei;
250 /* Pull a block off the worklist. */
251 bb = *--tos;
253 /* For each predecessor block. */
254 FOR_EACH_EDGE (e, ei, bb->preds)
256 basic_block pred = e->src;
257 int pred_index = pred->index;
259 /* None of this is necessary for the entry block. */
260 if (pred != ENTRY_BLOCK_PTR
261 && ! bitmap_bit_p (livein, pred_index)
262 && ! bitmap_bit_p (def_blocks, pred_index))
264 *tos++ = pred;
265 bitmap_set_bit (livein, pred_index);
270 free (worklist);
274 /* Block initialization routine for mark_def_sites. Clear the
275 KILLS bitmap at the start of each block. */
277 static void
278 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
279 basic_block bb ATTRIBUTE_UNUSED)
281 struct mark_def_sites_global_data *gd = walk_data->global_data;
282 sbitmap kills = gd->kills;
284 sbitmap_zero (kills);
287 /* Block initialization routine for mark_def_sites. Clear the
288 KILLS bitmap at the start of each block. */
290 static void
291 ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
292 basic_block bb)
294 struct mark_def_sites_global_data *gd = walk_data->global_data;
295 sbitmap kills = gd->kills;
296 tree phi, def;
297 unsigned def_uid;
299 sbitmap_zero (kills);
301 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
303 def = PHI_RESULT (phi);
304 def_uid = SSA_NAME_VERSION (def);
306 if (!TEST_BIT (gd->names_to_rename, def_uid))
307 continue;
309 set_def_block (def, bb, true, true);
310 SET_BIT (kills, def_uid);
314 /* Marks ssa names used as arguments of phis at the end of BB. */
316 static void
317 ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb)
319 struct mark_def_sites_global_data *gd = walk_data->global_data;
320 sbitmap kills = gd->kills;
321 edge e;
322 tree phi, use;
323 unsigned uid;
324 edge_iterator ei;
326 FOR_EACH_EDGE (e, ei, bb->succs)
328 if (e->dest == EXIT_BLOCK_PTR)
329 continue;
331 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
333 use = PHI_ARG_DEF_FROM_EDGE (phi, e);
334 if (TREE_CODE (use) != SSA_NAME)
335 continue;
337 uid = SSA_NAME_VERSION (use);
339 if (TEST_BIT (gd->names_to_rename, uid)
340 && !TEST_BIT (kills, uid))
341 set_livein_block (use, bb);
346 /* Call back for walk_dominator_tree used to collect definition sites
347 for every variable in the function. For every statement S in block
350 1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
351 WALK_DATA->GLOBAL_DATA->KILLS.
353 2- If S uses a variable VAR and there is no preceding kill of VAR,
354 then it is marked in marked in the LIVEIN_BLOCKS bitmap
355 associated with VAR.
357 This information is used to determine which variables are live
358 across block boundaries to reduce the number of PHI nodes
359 we create. */
361 static void
362 mark_def_sites (struct dom_walk_data *walk_data,
363 basic_block bb,
364 block_stmt_iterator bsi)
366 struct mark_def_sites_global_data *gd = walk_data->global_data;
367 sbitmap kills = gd->kills;
368 size_t uid;
369 tree stmt, def;
370 use_operand_p use_p;
371 def_operand_p def_p;
372 ssa_op_iter iter;
374 /* Mark all the blocks that have definitions for each variable in the
375 VARS_TO_RENAME bitmap. */
376 stmt = bsi_stmt (bsi);
377 get_stmt_operands (stmt);
379 /* If a variable is used before being set, then the variable is live
380 across a block boundary, so mark it live-on-entry to BB. */
382 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
384 if (prepare_use_operand_for_rename (use_p, &uid)
385 && !TEST_BIT (kills, uid))
386 set_livein_block (USE_FROM_PTR (use_p), bb);
389 /* Note that virtual definitions are irrelevant for computing KILLS
390 because a V_MAY_DEF does not constitute a killing definition of the
391 variable. However, the operand of a virtual definitions is a use
392 of the variable, so it may cause the variable to be considered
393 live-on-entry. */
395 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
397 if (prepare_use_operand_for_rename (use_p, &uid))
399 /* If we do not already have an SSA_NAME for our destination,
400 then set the destination to the source. */
401 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
402 SET_DEF (def_p, USE_FROM_PTR (use_p));
404 set_livein_block (USE_FROM_PTR (use_p), bb);
405 set_def_block (DEF_FROM_PTR (def_p), bb, false, false);
409 /* Now process the virtual must-defs made by this statement. */
410 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
412 if (prepare_def_operand_for_rename (def, &uid))
414 set_def_block (def, bb, false, false);
415 SET_BIT (kills, uid);
421 /* Ditto, but works over ssa names. */
423 static void
424 ssa_mark_def_sites (struct dom_walk_data *walk_data,
425 basic_block bb,
426 block_stmt_iterator bsi)
428 struct mark_def_sites_global_data *gd = walk_data->global_data;
429 sbitmap kills = gd->kills;
430 size_t uid, def_uid;
431 tree stmt, use, def;
432 ssa_op_iter iter;
434 /* Mark all the blocks that have definitions for each variable in the
435 names_to_rename bitmap. */
436 stmt = bsi_stmt (bsi);
437 get_stmt_operands (stmt);
439 /* If a variable is used before being set, then the variable is live
440 across a block boundary, so mark it live-on-entry to BB. */
441 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES)
443 uid = SSA_NAME_VERSION (use);
445 if (TEST_BIT (gd->names_to_rename, uid)
446 && !TEST_BIT (kills, uid))
447 set_livein_block (use, bb);
450 /* Now process the definition made by this statement. Mark the
451 variables in KILLS. */
452 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
454 def_uid = SSA_NAME_VERSION (def);
456 if (TEST_BIT (gd->names_to_rename, def_uid))
458 set_def_block (def, bb, false, true);
459 SET_BIT (kills, def_uid);
464 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
465 VAR is defined by a phi node. SSA_P is true if we are called from
466 rewrite_ssa_into_ssa. */
468 static void
469 set_def_block (tree var, basic_block bb, bool phi_p, bool ssa_p)
471 struct def_blocks_d *db_p;
472 enum need_phi_state state;
474 if (!ssa_p
475 && TREE_CODE (var) == SSA_NAME)
476 var = SSA_NAME_VAR (var);
478 state = get_phi_state (var);
479 db_p = get_def_blocks_for (var);
481 /* Set the bit corresponding to the block where VAR is defined. */
482 bitmap_set_bit (db_p->def_blocks, bb->index);
483 if (phi_p)
484 bitmap_set_bit (db_p->phi_blocks, bb->index);
486 /* Keep track of whether or not we may need to insert phi nodes.
488 If we are in the UNKNOWN state, then this is the first definition
489 of VAR. Additionally, we have not seen any uses of VAR yet, so
490 we do not need a phi node for this variable at this time (i.e.,
491 transition to NEED_PHI_STATE_NO).
493 If we are in any other state, then we either have multiple definitions
494 of this variable occurring in different blocks or we saw a use of the
495 variable which was not dominated by the block containing the
496 definition(s). In this case we may need a PHI node, so enter
497 state NEED_PHI_STATE_MAYBE. */
498 if (state == NEED_PHI_STATE_UNKNOWN)
499 set_phi_state (var, NEED_PHI_STATE_NO);
500 else
501 set_phi_state (var, NEED_PHI_STATE_MAYBE);
505 /* Mark block BB as having VAR live at the entry to BB. */
507 static void
508 set_livein_block (tree var, basic_block bb)
510 struct def_blocks_d *db_p;
511 enum need_phi_state state = get_phi_state (var);
513 db_p = get_def_blocks_for (var);
515 /* Set the bit corresponding to the block where VAR is live in. */
516 bitmap_set_bit (db_p->livein_blocks, bb->index);
518 /* Keep track of whether or not we may need to insert phi nodes.
520 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
521 by the single block containing the definition(s) of this variable. If
522 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
523 NEED_PHI_STATE_MAYBE. */
524 if (state == NEED_PHI_STATE_NO)
526 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
528 if (def_block_index == -1
529 || ! dominated_by_p (CDI_DOMINATORS, bb,
530 BASIC_BLOCK (def_block_index)))
531 set_phi_state (var, NEED_PHI_STATE_MAYBE);
533 else
534 set_phi_state (var, NEED_PHI_STATE_MAYBE);
538 /* If the use operand pointed to by OP_P needs to be renamed, then strip away
539 any SSA_NAME wrapping the operand, set *UID_P to the underlying variable's
540 uid, and return true. Otherwise return false. If the operand was an
541 SSA_NAME, change it to the stripped name. */
543 static bool
544 prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p)
546 tree use = USE_FROM_PTR (op_p);
547 tree var = (TREE_CODE (use) != SSA_NAME) ? use : SSA_NAME_VAR (use);
548 *uid_p = var_ann (var)->uid;
550 /* Ignore variables that don't need to be renamed. */
551 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
552 return false;
554 /* The variable needs to be renamed. If this is a use which already
555 has an SSA_NAME, then strip it off.
557 By not throwing away SSA_NAMEs on assignments, we avoid a lot of
558 useless churn of SSA_NAMEs without having to overly complicate the
559 renamer. */
560 if (TREE_CODE (use) == SSA_NAME)
561 SET_USE (op_p, var);
563 return true;
566 /* If the def variable DEF needs to be renamed, then strip away any SSA_NAME
567 wrapping the operand, set *UID_P to the underlying variable's uid and return
568 true. Otherwise return false. */
570 static bool
571 prepare_def_operand_for_rename (tree def, size_t *uid_p)
573 tree var = (TREE_CODE (def) != SSA_NAME) ? def : SSA_NAME_VAR (def);
574 *uid_p = var_ann (var)->uid;
576 /* Ignore variables that don't need to be renamed. */
577 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
578 return false;
580 return true;
583 /* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
584 at the dominance frontier (DFS) of blocks defining VAR.
585 WORK_STACK is the varray used to implement the worklist of basic
586 blocks. */
588 static inline
589 void insert_phi_nodes_1 (tree var, bitmap *dfs, varray_type *work_stack)
591 if (get_phi_state (var) != NEED_PHI_STATE_NO)
592 insert_phi_nodes_for (var, dfs, work_stack);
595 /* Insert PHI nodes at the dominance frontier of blocks with variable
596 definitions. DFS contains the dominance frontier information for
597 the flowgraph. PHI nodes will only be inserted at the dominance
598 frontier of definition blocks for variables whose NEED_PHI_STATE
599 annotation is marked as ``maybe'' or ``unknown'' (computed by
600 mark_def_sites). If NAMES_TO_RENAME is not NULL, do the same but
601 for ssa name rewriting. */
603 static void
604 insert_phi_nodes (bitmap *dfs, bitmap names_to_rename)
606 size_t i;
607 varray_type work_stack;
608 bitmap_iterator bi;
610 timevar_push (TV_TREE_INSERT_PHI_NODES);
612 /* Array WORK_STACK is a stack of CFG blocks. Each block that contains
613 an assignment or PHI node will be pushed to this stack. */
614 VARRAY_GENERIC_PTR_NOGC_INIT (work_stack, last_basic_block, "work_stack");
616 /* Iterate over all variables in VARS_TO_RENAME. For each variable, add
617 to the work list all the blocks that have a definition for the
618 variable. PHI nodes will be added to the dominance frontier blocks of
619 each definition block. */
620 if (names_to_rename)
622 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
624 if (ssa_name (i))
625 insert_phi_nodes_1 (ssa_name (i), dfs, &work_stack);
628 else if (vars_to_rename)
629 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
631 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
633 else
634 for (i = 0; i < num_referenced_vars; i++)
635 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
637 VARRAY_FREE (work_stack);
639 timevar_pop (TV_TREE_INSERT_PHI_NODES);
643 /* Perform a depth-first traversal of the dominator tree looking for
644 variables to rename. BB is the block where to start searching.
645 Renaming is a five step process:
647 1- Every definition made by PHI nodes at the start of the blocks is
648 registered as the current definition for the corresponding variable.
650 2- Every statement in BB is rewritten. USE and VUSE operands are
651 rewritten with their corresponding reaching definition. DEF and
652 VDEF targets are registered as new definitions.
654 3- All the PHI nodes in successor blocks of BB are visited. The
655 argument corresponding to BB is replaced with its current reaching
656 definition.
658 4- Recursively rewrite every dominator child block of BB.
660 5- Restore (in reverse order) the current reaching definition for every
661 new definition introduced in this block. This is done so that when
662 we return from the recursive call, all the current reaching
663 definitions are restored to the names that were valid in the
664 dominator parent of BB. */
666 /* SSA Rewriting Step 1. Initialization, create a block local stack
667 of reaching definitions for new SSA names produced in this block
668 (BLOCK_DEFS). Register new definitions for every PHI node in the
669 block. */
671 static void
672 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
673 basic_block bb)
675 tree phi;
677 if (dump_file && (dump_flags & TDF_DETAILS))
678 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
680 /* Mark the unwind point for this block. */
681 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
683 /* Step 1. Register new definitions for every PHI node in the block.
684 Conceptually, all the PHI nodes are executed in parallel and each PHI
685 node introduces a new version for the associated variable. */
686 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
688 tree result = PHI_RESULT (phi);
690 register_new_def (result, &block_defs_stack);
694 /* Register DEF (an SSA_NAME) to be a new definition for the original
695 ssa name VAR and push VAR's current reaching definition
696 into the stack pointed by BLOCK_DEFS_P. */
698 static void
699 ssa_register_new_def (tree var, tree def)
701 tree currdef;
703 /* If this variable is set in a single basic block and all uses are
704 dominated by the set(s) in that single basic block, then there is
705 nothing to do. TODO we should not be called at all, and just
706 keep the original name. */
707 if (get_phi_state (var) == NEED_PHI_STATE_NO)
709 set_current_def (var, def);
710 return;
713 currdef = get_current_def (var);
715 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
716 later used by the dominator tree callbacks to restore the reaching
717 definitions for all the variables defined in the block after a recursive
718 visit to all its immediately dominated blocks. */
719 VARRAY_PUSH_TREE (block_defs_stack, currdef);
720 VARRAY_PUSH_TREE (block_defs_stack, var);
722 /* Set the current reaching definition for VAR to be DEF. */
723 set_current_def (var, def);
726 /* Ditto, for rewriting ssa names. */
728 static void
729 ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
731 tree phi, new_name;
732 sbitmap names_to_rename = walk_data->global_data;
733 edge e;
734 bool abnormal_phi;
735 edge_iterator ei;
737 if (dump_file && (dump_flags & TDF_DETAILS))
738 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
740 /* Mark the unwind point for this block. */
741 VARRAY_PUSH_TREE (block_defs_stack, NULL_TREE);
743 FOR_EACH_EDGE (e, ei, bb->preds)
744 if (e->flags & EDGE_ABNORMAL)
745 break;
746 abnormal_phi = (e != NULL);
748 /* Step 1. Register new definitions for every PHI node in the block.
749 Conceptually, all the PHI nodes are executed in parallel and each PHI
750 node introduces a new version for the associated variable. */
751 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
753 tree result = PHI_RESULT (phi);
755 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result)))
757 new_name = duplicate_ssa_name (result, phi);
758 SET_PHI_RESULT (phi, new_name);
760 if (abnormal_phi)
761 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
763 else
764 new_name = result;
766 ssa_register_new_def (result, new_name);
770 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
771 PHI nodes. For every PHI node found, add a new argument containing the
772 current reaching definition for the variable and the edge through which
773 that definition is reaching the PHI node. */
775 static void
776 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
777 basic_block bb)
779 edge e;
780 edge_iterator ei;
782 FOR_EACH_EDGE (e, ei, bb->succs)
784 tree phi;
786 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
788 tree currdef;
790 /* If this PHI node has already been rewritten, then there is
791 nothing to do for this PHI or any following PHIs since we
792 always add new PHI nodes at the start of the PHI chain. */
793 if (PHI_REWRITTEN (phi))
794 break;
796 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
797 add_phi_arg (&phi, currdef, e);
802 /* Ditto, for ssa name rewriting. */
804 static void
805 ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb)
807 edge e;
808 sbitmap names_to_rename = walk_data->global_data;
809 use_operand_p op;
810 edge_iterator ei;
812 FOR_EACH_EDGE (e, ei, bb->succs)
814 tree phi;
816 if (e->dest == EXIT_BLOCK_PTR)
817 continue;
819 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
821 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
822 if (TREE_CODE (USE_FROM_PTR (op)) != SSA_NAME)
823 continue;
825 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (op))))
826 continue;
828 SET_USE (op, get_reaching_def (USE_FROM_PTR (op)));
829 if (e->flags & EDGE_ABNORMAL)
830 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
836 /* Similar to restore_vars_to_original_value, except that it restores
837 CURRDEFS to its original value. */
838 static void
839 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
840 basic_block bb ATTRIBUTE_UNUSED)
842 /* Restore CURRDEFS to its original state. */
843 while (VARRAY_ACTIVE_SIZE (block_defs_stack) > 0)
845 tree tmp = VARRAY_TOP_TREE (block_defs_stack);
846 tree saved_def, var;
848 VARRAY_POP (block_defs_stack);
850 if (tmp == NULL_TREE)
851 break;
853 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
854 definition of its underlying variable. If we recorded anything
855 else, it must have been an _DECL node and its current reaching
856 definition must have been NULL. */
857 if (TREE_CODE (tmp) == SSA_NAME)
859 saved_def = tmp;
860 var = SSA_NAME_VAR (saved_def);
862 else
864 saved_def = NULL;
865 var = tmp;
868 set_current_def (var, saved_def);
872 /* Ditto, for rewriting ssa names. */
874 static void
875 ssa_rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
876 basic_block bb ATTRIBUTE_UNUSED)
879 /* Step 5. Restore the current reaching definition for each variable
880 referenced in the block (in reverse order). */
881 while (VARRAY_ACTIVE_SIZE (block_defs_stack) > 0)
883 tree var = VARRAY_TOP_TREE (block_defs_stack);
884 tree saved_def;
886 VARRAY_POP (block_defs_stack);
888 if (var == NULL)
889 break;
891 saved_def = VARRAY_TOP_TREE (block_defs_stack);
892 VARRAY_POP (block_defs_stack);
894 set_current_def (var, saved_def);
898 /* Dump SSA information to FILE. */
900 void
901 dump_tree_ssa (FILE *file)
903 basic_block bb;
904 const char *funcname
905 = lang_hooks.decl_printable_name (current_function_decl, 2);
907 fprintf (file, "SSA information for %s\n\n", funcname);
909 FOR_EACH_BB (bb)
911 dump_bb (bb, file, 0);
912 fputs (" ", file);
913 print_generic_stmt (file, phi_nodes (bb), dump_flags);
914 fputs ("\n\n", file);
919 /* Dump SSA information to stderr. */
921 void
922 debug_tree_ssa (void)
924 dump_tree_ssa (stderr);
928 /* Dump SSA statistics on FILE. */
930 void
931 dump_tree_ssa_stats (FILE *file)
933 fprintf (file, "\nHash table statistics:\n");
935 fprintf (file, " def_blocks: ");
936 htab_statistics (file, def_blocks);
938 fprintf (file, "\n");
942 /* Dump SSA statistics on stderr. */
944 void
945 debug_tree_ssa_stats (void)
947 dump_tree_ssa_stats (stderr);
951 /* Dump statistics for the hash table HTAB. */
953 static void
954 htab_statistics (FILE *file, htab_t htab)
956 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
957 (long) htab_size (htab),
958 (long) htab_elements (htab),
959 htab_collisions (htab));
963 /* Insert PHI nodes for variable VAR using the dominance frontier
964 information given in DFS. WORK_STACK is the varray used to
965 implement the worklist of basic blocks. */
967 static void
968 insert_phi_nodes_for (tree var, bitmap *dfs, varray_type *work_stack)
970 struct def_blocks_d *def_map;
971 bitmap phi_insertion_points;
972 int bb_index;
973 edge e;
974 tree phi;
975 basic_block bb;
976 bitmap_iterator bi;
978 def_map = find_def_blocks_for (var);
979 if (def_map == NULL)
980 return;
982 phi_insertion_points = BITMAP_XMALLOC ();
984 EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index, bi)
986 VARRAY_PUSH_GENERIC_PTR_NOGC (*work_stack, BASIC_BLOCK (bb_index));
989 /* Pop a block off the worklist, add every block that appears in
990 the original block's dfs that we have not already processed to
991 the worklist. Iterate until the worklist is empty. Blocks
992 which are added to the worklist are potential sites for
993 PHI nodes.
995 The iteration step could be done during PHI insertion just as
996 easily. We do it here for historical reasons -- we used to have
997 a heuristic which used the potential PHI insertion points to
998 determine if fully pruned or semi pruned SSA form was appropriate.
1000 We now always use fully pruned SSA form. */
1001 while (VARRAY_ACTIVE_SIZE (*work_stack) > 0)
1003 int dfs_index;
1004 bitmap_iterator bi;
1006 bb = VARRAY_TOP_GENERIC_PTR_NOGC (*work_stack);
1007 bb_index = bb->index;
1009 VARRAY_POP (*work_stack);
1011 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
1012 phi_insertion_points,
1013 0, dfs_index, bi)
1015 basic_block bb = BASIC_BLOCK (dfs_index);
1017 VARRAY_PUSH_GENERIC_PTR_NOGC (*work_stack, bb);
1018 bitmap_set_bit (phi_insertion_points, dfs_index);
1022 /* Remove the blocks where we already have the phis. */
1023 bitmap_operation (phi_insertion_points, phi_insertion_points,
1024 def_map->phi_blocks, BITMAP_AND_COMPL);
1026 /* Now compute global livein for this variable. Note this modifies
1027 def_map->livein_blocks. */
1028 compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
1030 /* And insert the PHI nodes. */
1031 EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
1032 0, bb_index, bi)
1034 bb = BASIC_BLOCK (bb_index);
1036 phi = create_phi_node (var, bb);
1038 /* If we are rewriting ssa names, add also the phi arguments. */
1039 if (TREE_CODE (var) == SSA_NAME)
1041 edge_iterator ei;
1042 FOR_EACH_EDGE (e, ei, bb->preds)
1043 add_phi_arg (&phi, var, e);
1047 BITMAP_XFREE (phi_insertion_points);
1050 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1051 the block with its immediate reaching definitions. Update the current
1052 definition of a variable when a new real or virtual definition is found. */
1054 static void
1055 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1056 basic_block bb ATTRIBUTE_UNUSED,
1057 block_stmt_iterator si)
1059 stmt_ann_t ann;
1060 tree stmt;
1061 use_operand_p use_p;
1062 def_operand_p def_p;
1063 ssa_op_iter iter;
1065 stmt = bsi_stmt (si);
1066 ann = stmt_ann (stmt);
1068 if (dump_file && (dump_flags & TDF_DETAILS))
1070 fprintf (dump_file, "Renaming statement ");
1071 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1072 fprintf (dump_file, "\n");
1075 /* We have just scanned the code for operands. No statement should
1076 be modified. */
1077 gcc_assert (!ann->modified);
1079 /* Step 1. Rewrite USES and VUSES in the statement. */
1080 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1081 rewrite_operand (use_p);
1083 /* Step 2. Register the statement's DEF and VDEF operands. */
1084 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1086 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
1087 SET_DEF (def_p, make_ssa_name (DEF_FROM_PTR (def_p), stmt));
1089 /* FIXME: We shouldn't be registering new defs if the variable
1090 doesn't need to be renamed. */
1091 register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
1095 /* Ditto, for rewriting ssa names. */
1097 static void
1098 ssa_rewrite_stmt (struct dom_walk_data *walk_data,
1099 basic_block bb ATTRIBUTE_UNUSED,
1100 block_stmt_iterator si)
1102 stmt_ann_t ann;
1103 tree stmt, var;
1104 ssa_op_iter iter;
1105 use_operand_p use_p;
1106 def_operand_p def_p;
1107 sbitmap names_to_rename = walk_data->global_data;
1109 stmt = bsi_stmt (si);
1110 ann = stmt_ann (stmt);
1112 if (dump_file && (dump_flags & TDF_DETAILS))
1114 fprintf (dump_file, "Renaming statement ");
1115 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1116 fprintf (dump_file, "\n");
1119 /* We have just scanned the code for operands. No statement should
1120 be modified. */
1121 gcc_assert (!ann->modified);
1123 /* Step 1. Rewrite USES and VUSES in the statement. */
1124 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1126 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (use_p))))
1127 SET_USE (use_p, get_reaching_def (USE_FROM_PTR (use_p)));
1130 /* Step 2. Register the statement's DEF and VDEF operands. */
1131 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1133 var = DEF_FROM_PTR (def_p);
1135 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (var)))
1136 continue;
1138 SET_DEF (def_p, duplicate_ssa_name (var, stmt));
1139 ssa_register_new_def (var, DEF_FROM_PTR (def_p));
1143 /* Replace the operand pointed by OP_P with its immediate reaching
1144 definition. */
1146 static inline void
1147 rewrite_operand (use_operand_p op_p)
1149 if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
1150 SET_USE (op_p, get_reaching_def (USE_FROM_PTR (op_p)));
1153 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1154 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1155 into the stack pointed by BLOCK_DEFS_P. */
1157 void
1158 register_new_def (tree def, varray_type *block_defs_p)
1160 tree var = SSA_NAME_VAR (def);
1161 tree currdef;
1163 /* If this variable is set in a single basic block and all uses are
1164 dominated by the set(s) in that single basic block, then there is
1165 no reason to record anything for this variable in the block local
1166 definition stacks. Doing so just wastes time and memory.
1168 This is the same test to prune the set of variables which may
1169 need PHI nodes. So we just use that information since it's already
1170 computed and available for us to use. */
1171 if (get_phi_state (var) == NEED_PHI_STATE_NO)
1173 set_current_def (var, def);
1174 return;
1177 currdef = get_current_def (var);
1179 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
1180 later used by the dominator tree callbacks to restore the reaching
1181 definitions for all the variables defined in the block after a recursive
1182 visit to all its immediately dominated blocks. If there is no current
1183 reaching definition, then just record the underlying _DECL node. */
1184 VARRAY_PUSH_TREE (*block_defs_p, currdef ? currdef : var);
1186 /* Set the current reaching definition for VAR to be DEF. */
1187 set_current_def (var, def);
1190 /* Return the current definition for variable VAR. If none is found,
1191 create a new SSA name to act as the zeroth definition for VAR. If VAR
1192 is call clobbered and there exists a more recent definition of
1193 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
1194 has been clobbered by a function call since its last assignment. */
1196 static tree
1197 get_reaching_def (tree var)
1199 tree default_d, currdef_var, avar;
1201 /* Lookup the current reaching definition for VAR. */
1202 default_d = NULL_TREE;
1203 currdef_var = get_current_def (var);
1205 /* If there is no reaching definition for VAR, create and register a
1206 default definition for it (if needed). */
1207 if (currdef_var == NULL_TREE)
1209 if (TREE_CODE (var) == SSA_NAME)
1210 avar = SSA_NAME_VAR (var);
1211 else
1212 avar = var;
1214 default_d = default_def (avar);
1215 if (default_d == NULL_TREE)
1217 default_d = make_ssa_name (avar, build_empty_stmt ());
1218 set_default_def (avar, default_d);
1220 set_current_def (var, default_d);
1223 /* Return the current reaching definition for VAR, or the default
1224 definition, if we had to create one. */
1225 return (currdef_var) ? currdef_var : default_d;
1229 /* Hashing and equality functions for DEF_BLOCKS. */
1231 static hashval_t
1232 def_blocks_hash (const void *p)
1234 return htab_hash_pointer
1235 ((const void *)((const struct def_blocks_d *)p)->var);
1238 static int
1239 def_blocks_eq (const void *p1, const void *p2)
1241 return ((const struct def_blocks_d *)p1)->var
1242 == ((const struct def_blocks_d *)p2)->var;
1245 /* Free memory allocated by one entry in DEF_BLOCKS. */
1247 static void
1248 def_blocks_free (void *p)
1250 struct def_blocks_d *entry = p;
1251 BITMAP_XFREE (entry->def_blocks);
1252 BITMAP_XFREE (entry->phi_blocks);
1253 BITMAP_XFREE (entry->livein_blocks);
1254 free (entry);
1258 /* Dump the DEF_BLOCKS hash table on stderr. */
1260 void
1261 debug_def_blocks (void)
1263 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1266 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1268 static int
1269 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1271 unsigned long i;
1272 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1273 bitmap_iterator bi;
1275 fprintf (stderr, "VAR: ");
1276 print_generic_expr (stderr, db_p->var, dump_flags);
1277 fprintf (stderr, ", DEF_BLOCKS: { ");
1278 EXECUTE_IF_SET_IN_BITMAP (db_p->def_blocks, 0, i, bi)
1280 fprintf (stderr, "%ld ", i);
1282 fprintf (stderr, "}");
1283 fprintf (stderr, ", LIVEIN_BLOCKS: { ");
1284 EXECUTE_IF_SET_IN_BITMAP (db_p->livein_blocks, 0, i, bi)
1286 fprintf (stderr, "%ld ", i);
1288 fprintf (stderr, "}\n");
1290 return 1;
1294 /* Return the set of blocks where variable VAR is defined and the blocks
1295 where VAR is live on entry (livein). Return NULL, if no entry is
1296 found in DEF_BLOCKS. */
1298 static inline struct def_blocks_d *
1299 find_def_blocks_for (tree var)
1301 struct def_blocks_d dm;
1302 dm.var = var;
1303 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1307 /* Return the set of blocks where variable VAR is defined and the blocks
1308 where VAR is live on entry (livein). If no entry is found in
1309 DEF_BLOCKS, a new one is created and returned. */
1311 static inline struct def_blocks_d *
1312 get_def_blocks_for (tree var)
1314 struct def_blocks_d db, *db_p;
1315 void **slot;
1317 db.var = var;
1318 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
1319 if (*slot == NULL)
1321 db_p = xmalloc (sizeof (*db_p));
1322 db_p->var = var;
1323 db_p->def_blocks = BITMAP_XMALLOC ();
1324 db_p->phi_blocks = BITMAP_XMALLOC ();
1325 db_p->livein_blocks = BITMAP_XMALLOC ();
1326 *slot = (void *) db_p;
1328 else
1329 db_p = (struct def_blocks_d *) *slot;
1331 return db_p;
1334 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
1335 process will cause us to lose the name memory tags that may have
1336 been associated with the various SSA_NAMEs of V. This means that
1337 the variables aliased to those name tags also need to be renamed
1338 again.
1340 FIXME 1- We should either have a better scheme for renaming
1341 pointers that doesn't lose name tags or re-run alias
1342 analysis to recover points-to information.
1344 2- Currently we just invalidate *all* the name tags. This
1345 should be more selective. */
1347 static void
1348 invalidate_name_tags (bitmap vars_to_rename)
1350 size_t i;
1351 bool rename_name_tags_p;
1352 bitmap_iterator bi;
1354 rename_name_tags_p = false;
1355 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
1357 if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i))))
1359 rename_name_tags_p = true;
1360 break;
1364 if (rename_name_tags_p)
1365 for (i = 0; i < num_referenced_vars; i++)
1367 var_ann_t ann = var_ann (referenced_var (i));
1369 if (ann->mem_tag_kind == NAME_TAG)
1371 size_t j;
1372 varray_type may_aliases = ann->may_aliases;
1374 bitmap_set_bit (vars_to_rename, ann->uid);
1375 if (ann->may_aliases)
1376 for (j = 0; j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
1378 tree var = VARRAY_TREE (may_aliases, j);
1379 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1386 /* Main entry point into the SSA builder. The renaming process
1387 proceeds in five main phases:
1389 1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1390 those variables are removed from the flow graph so that they can
1391 be computed again.
1393 2- Compute dominance frontier and immediate dominators, needed to
1394 insert PHI nodes and rename the function in dominator tree
1395 order.
1397 3- Find and mark all the blocks that define variables
1398 (mark_def_sites).
1400 4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1402 5- Rename all the blocks (rewrite_initialize_block,
1403 rewrite_add_phi_arguments) and statements in the program
1404 (rewrite_stmt).
1406 Steps 3 and 5 are done using the dominator tree walker
1407 (walk_dominator_tree).
1409 ALL is true if all variables should be renamed (otherwise just those
1410 mentioned in vars_to_rename are taken into account). */
1412 void
1413 rewrite_into_ssa (bool all)
1415 bitmap *dfs;
1416 basic_block bb;
1417 struct dom_walk_data walk_data;
1418 struct mark_def_sites_global_data mark_def_sites_global_data;
1419 bitmap old_vars_to_rename = vars_to_rename;
1420 unsigned i;
1422 timevar_push (TV_TREE_SSA_OTHER);
1424 if (all)
1425 vars_to_rename = NULL;
1426 else
1428 /* Initialize the array of variables to rename. */
1429 gcc_assert (vars_to_rename);
1431 if (bitmap_first_set_bit (vars_to_rename) < 0)
1433 timevar_pop (TV_TREE_SSA_OTHER);
1434 return;
1437 invalidate_name_tags (vars_to_rename);
1439 /* Now remove all the existing PHI nodes (if any) for the variables
1440 that we are about to rename into SSA. */
1441 remove_all_phi_nodes_for (vars_to_rename);
1444 /* Allocate memory for the DEF_BLOCKS hash table. */
1445 def_blocks = htab_create (VARRAY_ACTIVE_SIZE (referenced_vars),
1446 def_blocks_hash, def_blocks_eq, def_blocks_free);
1448 /* Initialize dominance frontier and immediate dominator bitmaps.
1449 Also count the number of predecessors for each block. Doing so
1450 can save significant time during PHI insertion for large graphs. */
1451 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1452 FOR_EACH_BB (bb)
1453 dfs[bb->index] = BITMAP_XMALLOC ();
1455 for (i = 0; i < num_referenced_vars; i++)
1456 set_current_def (referenced_var (i), NULL_TREE);
1458 /* Ensure that the dominance information is OK. */
1459 calculate_dominance_info (CDI_DOMINATORS);
1461 /* Compute dominance frontiers. */
1462 compute_dominance_frontiers (dfs);
1464 /* Setup callbacks for the generic dominator tree walker to find and
1465 mark definition sites. */
1466 walk_data.walk_stmts_backward = false;
1467 walk_data.dom_direction = CDI_DOMINATORS;
1468 walk_data.initialize_block_local_data = NULL;
1469 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
1470 walk_data.before_dom_children_walk_stmts = mark_def_sites;
1471 walk_data.before_dom_children_after_stmts = NULL;
1472 walk_data.after_dom_children_before_stmts = NULL;
1473 walk_data.after_dom_children_walk_stmts = NULL;
1474 walk_data.after_dom_children_after_stmts = NULL;
1476 /* Notice that this bitmap is indexed using variable UIDs, so it must be
1477 large enough to accommodate all the variables referenced in the
1478 function, not just the ones we are renaming. */
1479 mark_def_sites_global_data.kills = sbitmap_alloc (num_referenced_vars);
1480 walk_data.global_data = &mark_def_sites_global_data;
1482 /* We do not have any local data. */
1483 walk_data.block_local_data_size = 0;
1485 /* Initialize the dominator walker. */
1486 init_walk_dominator_tree (&walk_data);
1488 /* Recursively walk the dominator tree. */
1489 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1491 /* Finalize the dominator walker. */
1492 fini_walk_dominator_tree (&walk_data);
1494 /* We no longer need this bitmap, clear and free it. */
1495 sbitmap_free (mark_def_sites_global_data.kills);
1497 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1498 insert_phi_nodes (dfs, NULL);
1500 /* Rewrite all the basic blocks in the program. */
1501 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1503 /* Setup callbacks for the generic dominator tree walker. */
1504 walk_data.walk_stmts_backward = false;
1505 walk_data.dom_direction = CDI_DOMINATORS;
1506 walk_data.initialize_block_local_data = NULL;
1507 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1508 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1509 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1510 walk_data.after_dom_children_before_stmts = NULL;
1511 walk_data.after_dom_children_walk_stmts = NULL;
1512 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1513 walk_data.global_data = NULL;
1514 walk_data.block_local_data_size = 0;
1516 VARRAY_TREE_INIT (block_defs_stack, 10, "Block DEFS Stack");
1518 /* Initialize the dominator walker. */
1519 init_walk_dominator_tree (&walk_data);
1521 /* Recursively walk the dominator tree rewriting each statement in
1522 each basic block. */
1523 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1525 /* Finalize the dominator walker. */
1526 fini_walk_dominator_tree (&walk_data);
1528 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1530 /* Debugging dumps. */
1531 if (dump_file && (dump_flags & TDF_STATS))
1533 dump_dfa_stats (dump_file);
1534 dump_tree_ssa_stats (dump_file);
1537 /* Free allocated memory. */
1538 FOR_EACH_BB (bb)
1539 BITMAP_XFREE (dfs[bb->index]);
1540 free (dfs);
1542 htab_delete (def_blocks);
1544 vars_to_rename = old_vars_to_rename;
1545 timevar_pop (TV_TREE_SSA_OTHER);
1548 /* The marked ssa names may have more than one definition;
1549 add phi nodes and rewrite them to fix this. */
1551 void
1552 rewrite_ssa_into_ssa (void)
1554 bitmap *dfs;
1555 basic_block bb;
1556 struct dom_walk_data walk_data;
1557 struct mark_def_sites_global_data mark_def_sites_global_data;
1558 unsigned i;
1559 sbitmap snames_to_rename;
1560 tree name;
1561 bitmap to_rename;
1562 bitmap_iterator bi;
1564 if (!any_marked_for_rewrite_p ())
1565 return;
1566 to_rename = marked_ssa_names ();
1568 timevar_push (TV_TREE_SSA_OTHER);
1570 /* Allocate memory for the DEF_BLOCKS hash table. */
1571 def_blocks = htab_create (num_ssa_names,
1572 def_blocks_hash, def_blocks_eq, def_blocks_free);
1574 /* Initialize dominance frontier and immediate dominator bitmaps.
1575 Also count the number of predecessors for each block. Doing so
1576 can save significant time during PHI insertion for large graphs. */
1577 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1578 FOR_EACH_BB (bb)
1579 dfs[bb->index] = BITMAP_XMALLOC ();
1581 /* Ensure that the dominance information is OK. */
1582 calculate_dominance_info (CDI_DOMINATORS);
1584 /* Compute dominance frontiers. */
1585 compute_dominance_frontiers (dfs);
1587 /* Setup callbacks for the generic dominator tree walker to find and
1588 mark definition sites. */
1589 walk_data.walk_stmts_backward = false;
1590 walk_data.dom_direction = CDI_DOMINATORS;
1591 walk_data.initialize_block_local_data = NULL;
1592 walk_data.before_dom_children_before_stmts
1593 = ssa_mark_def_sites_initialize_block;
1594 walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites;
1595 walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses;
1596 walk_data.after_dom_children_before_stmts = NULL;
1597 walk_data.after_dom_children_walk_stmts = NULL;
1598 walk_data.after_dom_children_after_stmts = NULL;
1600 snames_to_rename = sbitmap_alloc (num_ssa_names);
1601 sbitmap_zero (snames_to_rename);
1602 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1604 SET_BIT (snames_to_rename, i);
1607 mark_def_sites_global_data.kills = sbitmap_alloc (num_ssa_names);
1608 mark_def_sites_global_data.names_to_rename = snames_to_rename;
1609 walk_data.global_data = &mark_def_sites_global_data;
1611 VARRAY_TREE_INIT (block_defs_stack, 10, "Block DEFS Stack");
1613 /* We do not have any local data. */
1614 walk_data.block_local_data_size = 0;
1616 /* Initialize the dominator walker. */
1617 init_walk_dominator_tree (&walk_data);
1619 /* Recursively walk the dominator tree. */
1620 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1622 /* Finalize the dominator walker. */
1623 fini_walk_dominator_tree (&walk_data);
1625 /* We no longer need this bitmap, clear and free it. */
1626 sbitmap_free (mark_def_sites_global_data.kills);
1628 for (i = 1; i < num_ssa_names; i++)
1629 if (ssa_name (i))
1630 set_current_def (ssa_name (i), NULL_TREE);
1632 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1633 insert_phi_nodes (dfs, to_rename);
1635 /* Rewrite all the basic blocks in the program. */
1636 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1638 /* Setup callbacks for the generic dominator tree walker. */
1639 walk_data.walk_stmts_backward = false;
1640 walk_data.dom_direction = CDI_DOMINATORS;
1641 walk_data.initialize_block_local_data = NULL;
1642 walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block;
1643 walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt;
1644 walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments;
1645 walk_data.after_dom_children_before_stmts = NULL;
1646 walk_data.after_dom_children_walk_stmts = NULL;
1647 walk_data.after_dom_children_after_stmts = ssa_rewrite_finalize_block;
1648 walk_data.global_data = snames_to_rename;
1649 walk_data.block_local_data_size = 0;
1651 /* Initialize the dominator walker. */
1652 init_walk_dominator_tree (&walk_data);
1654 /* Recursively walk the dominator tree rewriting each statement in
1655 each basic block. */
1656 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1658 /* Finalize the dominator walker. */
1659 fini_walk_dominator_tree (&walk_data);
1661 unmark_all_for_rewrite ();
1663 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1665 release_ssa_name (ssa_name (i));
1668 sbitmap_free (snames_to_rename);
1670 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1672 /* Debugging dumps. */
1673 if (dump_file && (dump_flags & TDF_STATS))
1675 dump_dfa_stats (dump_file);
1676 dump_tree_ssa_stats (dump_file);
1679 /* Free allocated memory. */
1680 FOR_EACH_BB (bb)
1681 BITMAP_XFREE (dfs[bb->index]);
1682 free (dfs);
1684 htab_delete (def_blocks);
1686 for (i = 1; i < num_ssa_names; i++)
1688 name = ssa_name (i);
1689 if (!name || !SSA_NAME_AUX (name))
1690 continue;
1692 free (SSA_NAME_AUX (name));
1693 SSA_NAME_AUX (name) = NULL;
1696 BITMAP_XFREE (to_rename);
1697 timevar_pop (TV_TREE_SSA_OTHER);
1700 /* Rewrites all variables into ssa. */
1702 static void
1703 rewrite_all_into_ssa (void)
1705 rewrite_into_ssa (true);
1708 struct tree_opt_pass pass_build_ssa =
1710 "ssa", /* name */
1711 NULL, /* gate */
1712 rewrite_all_into_ssa, /* execute */
1713 NULL, /* sub */
1714 NULL, /* next */
1715 0, /* static_pass_number */
1716 0, /* tv_id */
1717 PROP_cfg | PROP_referenced_vars, /* properties_required */
1718 PROP_ssa, /* properties_provided */
1719 0, /* properties_destroyed */
1720 0, /* todo_flags_start */
1721 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
1722 0 /* letter */