tree-cfg.c (tree_make_forwarder_block): Use SET_PHI_RESULT.
[gcc.git] / gcc / tree-into-ssa.c
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>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "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 "tree-alias-common.h"
45 #include "hashtab.h"
46 #include "tree-dump.h"
47 #include "tree-pass.h"
48 #include "cfgloop.h"
49 #include "domwalk.h"
50
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. */
56
57
58 /* Structure to map a variable VAR to the set of blocks that contain
59 definitions for VAR. */
60 struct def_blocks_d
61 {
62 /* The variable. */
63 tree var;
64
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;
68
69 /* Blocks where VAR is live-on-entry. Similar semantics as
70 DEF_BLOCKS. */
71 bitmap livein_blocks;
72 };
73
74 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
75 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
76 basic blocks where VAR is defined (assigned a new value). It also
77 contains a bitmap of all the blocks where VAR is live-on-entry
78 (i.e., there is a use of VAR in block B without a preceding
79 definition in B). The live-on-entry information is used when
80 computing PHI pruning heuristics. */
81 static htab_t def_blocks;
82
83 /* Global data to attach to the main dominator walk structure. */
84 struct mark_def_sites_global_data
85 {
86 /* This sbitmap contains the variables which are set before they
87 are used in a basic block. We keep it as a global variable
88 solely to avoid the overhead of allocating and deallocating
89 the bitmap. */
90 sbitmap kills;
91 };
92
93 struct rewrite_block_data
94 {
95 varray_type block_defs;
96 };
97
98
99 /* Local functions. */
100 static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
101 static void rewrite_initialize_block_local_data (struct dom_walk_data *,
102 basic_block, bool);
103 static void rewrite_initialize_block (struct dom_walk_data *, basic_block);
104 static void rewrite_add_phi_arguments (struct dom_walk_data *, basic_block);
105 static void mark_def_sites (struct dom_walk_data *walk_data,
106 basic_block bb, block_stmt_iterator);
107 static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
108 basic_block bb);
109 static void compute_global_livein (bitmap, bitmap);
110 static void set_def_block (tree, basic_block);
111 static void set_livein_block (tree, basic_block);
112 static bool prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p);
113 static bool prepare_def_operand_for_rename (tree def, size_t *uid_p);
114 static void insert_phi_nodes (bitmap *);
115 static void rewrite_stmt (struct dom_walk_data *, basic_block,
116 block_stmt_iterator);
117 static inline void rewrite_operand (use_operand_p);
118 static void insert_phi_nodes_for (tree, bitmap *, varray_type *);
119 static tree get_reaching_def (tree);
120 static hashval_t def_blocks_hash (const void *);
121 static int def_blocks_eq (const void *, const void *);
122 static void def_blocks_free (void *);
123 static int debug_def_blocks_r (void **, void *);
124 static inline struct def_blocks_d *get_def_blocks_for (tree);
125 static inline struct def_blocks_d *find_def_blocks_for (tree);
126 static void htab_statistics (FILE *, htab_t);
127
128 /* Compute global livein information given the set of blockx where
129 an object is locally live at the start of the block (LIVEIN)
130 and the set of blocks where the object is defined (DEF_BLOCKS).
131
132 Note: This routine augments the existing local livein information
133 to include global livein (i.e., it modifies the underlying bitmap
134 for LIVEIN). */
135
136 static void
137 compute_global_livein (bitmap livein, bitmap def_blocks)
138 {
139 basic_block bb, *worklist, *tos;
140 int i;
141
142 tos = worklist
143 = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
144
145 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i,
146 {
147 *tos++ = BASIC_BLOCK (i);
148 });
149
150 /* Iterate until the worklist is empty. */
151 while (tos != worklist)
152 {
153 edge e;
154
155 /* Pull a block off the worklist. */
156 bb = *--tos;
157
158 /* For each predecessor block. */
159 for (e = bb->pred; e; e = e->pred_next)
160 {
161 basic_block pred = e->src;
162 int pred_index = pred->index;
163
164 /* None of this is necessary for the entry block. */
165 if (pred != ENTRY_BLOCK_PTR
166 && ! bitmap_bit_p (livein, pred_index)
167 && ! bitmap_bit_p (def_blocks, pred_index))
168 {
169 *tos++ = pred;
170 bitmap_set_bit (livein, pred_index);
171 }
172 }
173 }
174
175 free (worklist);
176 }
177
178
179 /* Block initialization routine for mark_def_sites. Clear the
180 KILLS bitmap at the start of each block. */
181
182 static void
183 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
184 basic_block bb ATTRIBUTE_UNUSED)
185 {
186 struct mark_def_sites_global_data *gd = walk_data->global_data;
187 sbitmap kills = gd->kills;
188
189 sbitmap_zero (kills);
190 }
191
192
193 /* Call back for walk_dominator_tree used to collect definition sites
194 for every variable in the function. For every statement S in block
195 BB:
196
197 1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
198 WALK_DATA->GLOBAL_DATA->KILLS.
199
200 2- If S uses a variable VAR and there is no preceding kill of VAR,
201 then it is marked in marked in the LIVEIN_BLOCKS bitmap
202 associated with VAR.
203
204 This information is used to determine which variables are live
205 across block boundaries to reduce the number of PHI nodes
206 we create. */
207
208 static void
209 mark_def_sites (struct dom_walk_data *walk_data,
210 basic_block bb,
211 block_stmt_iterator bsi)
212 {
213 struct mark_def_sites_global_data *gd = walk_data->global_data;
214 sbitmap kills = gd->kills;
215 v_may_def_optype v_may_defs;
216 v_must_def_optype v_must_defs;
217 vuse_optype vuses;
218 def_optype defs;
219 use_optype uses;
220 size_t i, uid;
221 tree stmt;
222 stmt_ann_t ann;
223
224 /* Mark all the blocks that have definitions for each variable in the
225 VARS_TO_RENAME bitmap. */
226 stmt = bsi_stmt (bsi);
227 get_stmt_operands (stmt);
228 ann = stmt_ann (stmt);
229
230 /* If a variable is used before being set, then the variable is live
231 across a block boundary, so mark it live-on-entry to BB. */
232 uses = USE_OPS (ann);
233 for (i = 0; i < NUM_USES (uses); i++)
234 {
235 use_operand_p use_p = USE_OP_PTR (uses, i);
236
237 if (prepare_use_operand_for_rename (use_p, &uid)
238 && !TEST_BIT (kills, uid))
239 set_livein_block (USE_FROM_PTR (use_p), bb);
240 }
241
242 /* Similarly for virtual uses. */
243 vuses = VUSE_OPS (ann);
244 for (i = 0; i < NUM_VUSES (vuses); i++)
245 {
246 use_operand_p use_p = VUSE_OP_PTR (vuses, i);
247
248 if (prepare_use_operand_for_rename (use_p, &uid))
249 set_livein_block (USE_FROM_PTR (use_p), bb);
250 }
251
252 /* Note that virtual definitions are irrelevant for computing KILLS
253 because a V_MAY_DEF does not constitute a killing definition of the
254 variable. However, the operand of a virtual definitions is a use
255 of the variable, so it may cause the variable to be considered
256 live-on-entry. */
257 v_may_defs = V_MAY_DEF_OPS (ann);
258 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
259 {
260 use_operand_p use_p = V_MAY_DEF_OP_PTR (v_may_defs, i);
261 if (prepare_use_operand_for_rename (use_p, &uid))
262 {
263 /* If we do not already have an SSA_NAME for our destination,
264 then set the destination to the source. */
265 if (TREE_CODE (V_MAY_DEF_RESULT (v_may_defs, i)) != SSA_NAME)
266 SET_V_MAY_DEF_RESULT (v_may_defs, i, USE_FROM_PTR (use_p));
267
268 set_livein_block (USE_FROM_PTR (use_p), bb);
269 set_def_block (V_MAY_DEF_RESULT (v_may_defs, i), bb);
270 }
271 }
272
273 /* Now process the virtual must-defs made by this statement. */
274 v_must_defs = V_MUST_DEF_OPS (ann);
275 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
276 {
277 tree def = V_MUST_DEF_OP (v_must_defs, i);
278
279 if (prepare_def_operand_for_rename (def, &uid))
280 {
281 set_def_block (def, bb);
282 SET_BIT (kills, uid);
283 }
284 }
285
286 /* Now process the definition made by this statement. Mark the
287 variables in KILLS. */
288 defs = DEF_OPS (ann);
289 for (i = 0; i < NUM_DEFS (defs); i++)
290 {
291 tree def = DEF_OP (defs, i);
292
293 if (prepare_def_operand_for_rename (def, &uid))
294 {
295 set_def_block (def, bb);
296 SET_BIT (kills, uid);
297 }
298 }
299 }
300
301
302 /* Mark block BB as the definition site for variable VAR. */
303
304 static void
305 set_def_block (tree var, basic_block bb)
306 {
307 struct def_blocks_d *db_p;
308 enum need_phi_state state;
309
310 if (TREE_CODE (var) == SSA_NAME)
311 var = SSA_NAME_VAR (var);
312
313 state = var_ann (var)->need_phi_state;
314 db_p = get_def_blocks_for (var);
315
316 /* Set the bit corresponding to the block where VAR is defined. */
317 bitmap_set_bit (db_p->def_blocks, bb->index);
318
319 /* Keep track of whether or not we may need to insert phi nodes.
320
321 If we are in the UNKNOWN state, then this is the first definition
322 of VAR. Additionally, we have not seen any uses of VAR yet, so
323 we do not need a phi node for this variable at this time (i.e.,
324 transition to NEED_PHI_STATE_NO).
325
326 If we are in any other state, then we either have multiple definitions
327 of this variable occurring in different blocks or we saw a use of the
328 variable which was not dominated by the block containing the
329 definition(s). In this case we may need a PHI node, so enter
330 state NEED_PHI_STATE_MAYBE. */
331 if (state == NEED_PHI_STATE_UNKNOWN)
332 var_ann (var)->need_phi_state = NEED_PHI_STATE_NO;
333 else
334 var_ann (var)->need_phi_state = NEED_PHI_STATE_MAYBE;
335 }
336
337
338 /* Mark block BB as having VAR live at the entry to BB. */
339
340 static void
341 set_livein_block (tree var, basic_block bb)
342 {
343 struct def_blocks_d *db_p;
344 enum need_phi_state state = var_ann (var)->need_phi_state;
345
346 db_p = get_def_blocks_for (var);
347
348 /* Set the bit corresponding to the block where VAR is live in. */
349 bitmap_set_bit (db_p->livein_blocks, bb->index);
350
351 /* Keep track of whether or not we may need to insert phi nodes.
352
353 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
354 by the single block containing the definition(s) of this variable. If
355 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
356 NEED_PHI_STATE_MAYBE. */
357 if (state == NEED_PHI_STATE_NO)
358 {
359 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
360
361 if (def_block_index == -1
362 || ! dominated_by_p (CDI_DOMINATORS, bb,
363 BASIC_BLOCK (def_block_index)))
364 var_ann (var)->need_phi_state = NEED_PHI_STATE_MAYBE;
365 }
366 else
367 var_ann (var)->need_phi_state = NEED_PHI_STATE_MAYBE;
368 }
369
370
371 /* If the use operand pointed to by OP_P needs to be renamed, then strip away
372 any SSA_NAME wrapping the operand, set *UID_P to the underlying variable's
373 uid, and return true. Otherwise return false. If the operand was an
374 SSA_NAME, change it to the stipped name. */
375
376 static bool
377 prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p)
378 {
379 tree use = USE_FROM_PTR (op_p);
380 tree var = (TREE_CODE (use) != SSA_NAME) ? use : SSA_NAME_VAR (use);
381 *uid_p = var_ann (var)->uid;
382
383 /* Ignore variables that don't need to be renamed. */
384 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
385 return false;
386
387 /* The variable needs to be renamed. If this is a use which already
388 has an SSA_NAME, then strip it off.
389
390 By not throwing away SSA_NAMEs on assignments, we avoid a lot of
391 useless churn of SSA_NAMEs without having to overly complicate the
392 renamer. */
393 if (TREE_CODE (use) == SSA_NAME)
394 SET_USE (op_p, var);
395
396 return true;
397 }
398
399 /* If the def variable DEF needs to be renamed, then strip away any SSA_NAME
400 wrapping the operand, set *UID_P to the underlying variable's uid and return
401 true. Otherwise return false. */
402
403 static bool
404 prepare_def_operand_for_rename (tree def, size_t *uid_p)
405 {
406 tree var = (TREE_CODE (def) != SSA_NAME) ? def : SSA_NAME_VAR (def);
407 *uid_p = var_ann (var)->uid;
408
409 /* Ignore variables that don't need to be renamed. */
410 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
411 return false;
412
413 return true;
414 }
415
416 /* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
417 at the dominance frontier (DFS) of blocks defining VAR. */
418
419 static inline
420 void insert_phi_nodes_1 (tree var, bitmap *dfs, varray_type *work_stack)
421 {
422 var_ann_t ann = var_ann (var);
423 if (ann->need_phi_state != NEED_PHI_STATE_NO)
424 insert_phi_nodes_for (var, dfs, work_stack);
425 }
426
427
428 /* Insert PHI nodes at the dominance frontier of blocks with variable
429 definitions. DFS contains the dominance frontier information for
430 the flowgraph. PHI nodes will only be inserted at the dominance
431 frontier of definition blocks for variables whose NEED_PHI_STATE
432 annotation is marked as ``maybe'' or ``unknown'' (computed by
433 mark_def_sites). */
434
435 static void
436 insert_phi_nodes (bitmap *dfs)
437 {
438 size_t i;
439 varray_type work_stack;
440
441 timevar_push (TV_TREE_INSERT_PHI_NODES);
442
443 /* Array WORK_STACK is a stack of CFG blocks. Each block that contains
444 an assignment or PHI node will be pushed to this stack. */
445 VARRAY_BB_INIT (work_stack, last_basic_block, "work_stack");
446
447 /* Iterate over all variables in VARS_TO_RENAME. For each variable, add
448 to the work list all the blocks that have a definition for the
449 variable. PHI nodes will be added to the dominance frontier blocks of
450 each definition block. */
451 if (vars_to_rename)
452 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i,
453 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack));
454 else
455 for (i = 0; i < num_referenced_vars; i++)
456 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
457
458 timevar_pop (TV_TREE_INSERT_PHI_NODES);
459 }
460
461
462 /* Perform a depth-first traversal of the dominator tree looking for
463 variables to rename. BB is the block where to start searching.
464 Renaming is a five step process:
465
466 1- Every definition made by PHI nodes at the start of the blocks is
467 registered as the current definition for the corresponding variable.
468
469 2- Every statement in BB is rewritten. USE and VUSE operands are
470 rewritten with their corresponding reaching definition. DEF and
471 VDEF targets are registered as new definitions.
472
473 3- All the PHI nodes in successor blocks of BB are visited. The
474 argument corresponding to BB is replaced with its current reaching
475 definition.
476
477 4- Recursively rewrite every dominator child block of BB.
478
479 5- Restore (in reverse order) the current reaching definition for every
480 new definition introduced in this block. This is done so that when
481 we return from the recursive call, all the current reaching
482 definitions are restored to the names that were valid in the
483 dominator parent of BB. */
484
485 /* Initialize the local stacks.
486
487 BLOCK_DEFS is used to save all the existing reaching definitions for
488 the new SSA names introduced in this block. Before registering a
489 new definition for a variable, the existing reaching definition is
490 pushed into this stack so that we can restore it in Step 5. */
491
492 static void
493 rewrite_initialize_block_local_data (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
494 basic_block bb ATTRIBUTE_UNUSED,
495 bool recycled ATTRIBUTE_UNUSED)
496 {
497 #ifdef ENABLE_CHECKING
498 struct rewrite_block_data *bd
499 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
500
501 /* We get cleared memory from the allocator, so if the memory is
502 not cleared, then we are re-using a previously allocated entry. In
503 that case, we can also re-use the underlying virtual arrays. Just
504 make sure we clear them before using them! */
505 if (recycled && bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
506 abort ();
507 #endif
508 }
509
510
511 /* SSA Rewriting Step 1. Initialization, create a block local stack
512 of reaching definitions for new SSA names produced in this block
513 (BLOCK_DEFS). Register new definitions for every PHI node in the
514 block. */
515
516 static void
517 rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
518 {
519 tree phi;
520 struct rewrite_block_data *bd
521 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
522
523 if (dump_file && (dump_flags & TDF_DETAILS))
524 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
525
526 /* Step 1. Register new definitions for every PHI node in the block.
527 Conceptually, all the PHI nodes are executed in parallel and each PHI
528 node introduces a new version for the associated variable. */
529 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
530 {
531 tree result = PHI_RESULT (phi);
532
533 register_new_def (result, &bd->block_defs);
534 }
535 }
536
537
538 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
539 PHI nodes. For every PHI node found, add a new argument containing the
540 current reaching definition for the variable and the edge through which
541 that definition is reaching the PHI node. */
542
543 static void
544 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
545 basic_block bb)
546 {
547 edge e;
548
549 for (e = bb->succ; e; e = e->succ_next)
550 {
551 tree phi;
552
553 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
554 {
555 tree currdef;
556
557 /* If this PHI node has already been rewritten, then there is
558 nothing to do for this PHI or any following PHIs since we
559 always add new PHI nodes at the start of the PHI chain. */
560 if (PHI_REWRITTEN (phi))
561 break;
562
563 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
564 add_phi_arg (&phi, currdef, e);
565 }
566 }
567 }
568
569 /* SSA Rewriting Step 5. Restore the current reaching definition for each
570 variable referenced in the block (in reverse order). */
571
572 static void
573 rewrite_finalize_block (struct dom_walk_data *walk_data,
574 basic_block bb ATTRIBUTE_UNUSED)
575 {
576 struct rewrite_block_data *bd
577 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
578
579 /* Step 5. Restore the current reaching definition for each variable
580 referenced in the block (in reverse order). */
581 while (bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
582 {
583 tree tmp = VARRAY_TOP_TREE (bd->block_defs);
584 tree saved_def, var;
585
586 VARRAY_POP (bd->block_defs);
587 if (TREE_CODE (tmp) == SSA_NAME)
588 {
589 saved_def = tmp;
590 var = SSA_NAME_VAR (saved_def);
591 }
592 else
593 {
594 saved_def = NULL;
595 var = tmp;
596 }
597
598 var_ann (var)->current_def = saved_def;
599 }
600 }
601
602
603 /* Dump SSA information to FILE. */
604
605 void
606 dump_tree_ssa (FILE *file)
607 {
608 basic_block bb;
609 const char *funcname
610 = lang_hooks.decl_printable_name (current_function_decl, 2);
611
612 fprintf (file, "SSA information for %s\n\n", funcname);
613
614 FOR_EACH_BB (bb)
615 {
616 dump_bb (bb, file, 0);
617 fputs (" ", file);
618 print_generic_stmt (file, phi_nodes (bb), dump_flags);
619 fputs ("\n\n", file);
620 }
621 }
622
623
624 /* Dump SSA information to stderr. */
625
626 void
627 debug_tree_ssa (void)
628 {
629 dump_tree_ssa (stderr);
630 }
631
632
633 /* Dump SSA statistics on FILE. */
634
635 void
636 dump_tree_ssa_stats (FILE *file)
637 {
638 fprintf (file, "\nHash table statistics:\n");
639
640 fprintf (file, " def_blocks: ");
641 htab_statistics (file, def_blocks);
642
643 fprintf (file, "\n");
644 }
645
646
647 /* Dump SSA statistics on stderr. */
648
649 void
650 debug_tree_ssa_stats (void)
651 {
652 dump_tree_ssa_stats (stderr);
653 }
654
655
656 /* Dump statistics for the hash table HTAB. */
657
658 static void
659 htab_statistics (FILE *file, htab_t htab)
660 {
661 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
662 (long) htab_size (htab),
663 (long) htab_elements (htab),
664 htab_collisions (htab));
665 }
666
667
668 /* Insert PHI nodes for variable VAR using the dominance frontier
669 information given in DFS. */
670
671 static void
672 insert_phi_nodes_for (tree var, bitmap *dfs, varray_type *work_stack)
673 {
674 struct def_blocks_d *def_map;
675 bitmap phi_insertion_points;
676 int bb_index;
677
678 def_map = find_def_blocks_for (var);
679 if (def_map == NULL)
680 return;
681
682 phi_insertion_points = BITMAP_XMALLOC ();
683
684 EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index,
685 {
686 VARRAY_PUSH_BB (*work_stack, BASIC_BLOCK (bb_index));
687 });
688
689 /* Pop a block off the worklist, add every block that appears in
690 the original block's dfs that we have not already processed to
691 the worklist. Iterate until the worklist is empty. Blocks
692 which are added to the worklist are potential sites for
693 PHI nodes.
694
695 The iteration step could be done during PHI insertion just as
696 easily. We do it here for historical reasons -- we used to have
697 a heuristic which used the potential PHI insertion points to
698 determine if fully pruned or semi pruned SSA form was appropriate.
699
700 We now always use fully pruned SSA form. */
701 while (VARRAY_ACTIVE_SIZE (*work_stack) > 0)
702 {
703 basic_block bb = VARRAY_TOP_BB (*work_stack);
704 int bb_index = bb->index;
705 int dfs_index;
706
707 VARRAY_POP (*work_stack);
708
709 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
710 phi_insertion_points,
711 0, dfs_index,
712 {
713 basic_block bb = BASIC_BLOCK (dfs_index);
714
715 VARRAY_PUSH_BB (*work_stack, bb);
716 bitmap_set_bit (phi_insertion_points, dfs_index);
717 });
718 }
719
720 /* Now compute global livein for this variable. Note this modifies
721 def_map->livein_blocks. */
722 compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
723
724 /* And insert the PHI nodes. */
725 EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
726 0, bb_index,
727 {
728 create_phi_node (var, BASIC_BLOCK (bb_index));
729 });
730
731 BITMAP_XFREE (phi_insertion_points);
732 }
733
734 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
735 the block with its immediate reaching definitions. Update the current
736 definition of a variable when a new real or virtual definition is found. */
737
738 static void
739 rewrite_stmt (struct dom_walk_data *walk_data,
740 basic_block bb ATTRIBUTE_UNUSED,
741 block_stmt_iterator si)
742 {
743 size_t i;
744 stmt_ann_t ann;
745 tree stmt;
746 vuse_optype vuses;
747 v_may_def_optype v_may_defs;
748 v_must_def_optype v_must_defs;
749 def_optype defs;
750 use_optype uses;
751 struct rewrite_block_data *bd;
752
753 bd = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
754
755 stmt = bsi_stmt (si);
756 ann = stmt_ann (stmt);
757
758 if (dump_file && (dump_flags & TDF_DETAILS))
759 {
760 fprintf (dump_file, "Renaming statement ");
761 print_generic_stmt (dump_file, stmt, TDF_SLIM);
762 fprintf (dump_file, "\n");
763 }
764
765 #if defined ENABLE_CHECKING
766 /* We have just scanned the code for operands. No statement should
767 be modified. */
768 if (ann->modified)
769 abort ();
770 #endif
771
772 defs = DEF_OPS (ann);
773 uses = USE_OPS (ann);
774 vuses = VUSE_OPS (ann);
775 v_may_defs = V_MAY_DEF_OPS (ann);
776 v_must_defs = V_MUST_DEF_OPS (ann);
777
778 /* Step 1. Rewrite USES and VUSES in the statement. */
779 for (i = 0; i < NUM_USES (uses); i++)
780 rewrite_operand (USE_OP_PTR (uses, i));
781
782 /* Rewrite virtual uses in the statement. */
783 for (i = 0; i < NUM_VUSES (vuses); i++)
784 rewrite_operand (VUSE_OP_PTR (vuses, i));
785
786 /* Step 2. Register the statement's DEF and VDEF operands. */
787 for (i = 0; i < NUM_DEFS (defs); i++)
788 {
789 def_operand_p def_p = DEF_OP_PTR (defs, i);
790
791 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
792 SET_DEF (def_p, make_ssa_name (DEF_FROM_PTR (def_p), stmt));
793
794 /* FIXME: We shouldn't be registering new defs if the variable
795 doesn't need to be renamed. */
796 register_new_def (DEF_FROM_PTR (def_p), &bd->block_defs);
797 }
798
799 /* Register new virtual definitions made by the statement. */
800 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
801 {
802 rewrite_operand (V_MAY_DEF_OP_PTR (v_may_defs, i));
803
804 if (TREE_CODE (V_MAY_DEF_RESULT (v_may_defs, i)) != SSA_NAME)
805 SET_V_MAY_DEF_RESULT (v_may_defs, i,
806 make_ssa_name (V_MAY_DEF_RESULT (v_may_defs, i),
807 stmt));
808
809 /* FIXME: We shouldn't be registering new defs if the variable
810 doesn't need to be renamed. */
811 register_new_def (V_MAY_DEF_RESULT (v_may_defs, i), &bd->block_defs);
812 }
813
814 /* Register new virtual mustdefs made by the statement. */
815 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
816 {
817 def_operand_p v_must_def_p = V_MUST_DEF_OP_PTR (v_must_defs, i);
818
819 if (TREE_CODE (DEF_FROM_PTR (v_must_def_p)) != SSA_NAME)
820 SET_DEF (v_must_def_p,
821 make_ssa_name (DEF_FROM_PTR (v_must_def_p), stmt));
822
823 /* FIXME: We shouldn't be registering new mustdefs if the variable
824 doesn't need to be renamed. */
825 register_new_def (DEF_FROM_PTR (v_must_def_p), &bd->block_defs);
826 }
827
828 }
829
830
831 /* Replace the use operand pointed by OP_P with its immediate reaching
832 definition. */
833
834 static inline void
835 rewrite_operand (use_operand_p op_p)
836 {
837 if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
838 SET_USE (op_p, get_reaching_def (USE_FROM_PTR (op_p)));
839 }
840
841
842 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
843 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
844 into the stack pointed by BLOCK_DEFS_P. */
845
846 void
847 register_new_def (tree def, varray_type *block_defs_p)
848 {
849 tree var = SSA_NAME_VAR (def);
850 tree currdef;
851
852 /* If this variable is set in a single basic block and all uses are
853 dominated by the set(s) in that single basic block, then there is
854 no reason to record anything for this variable in the block local
855 definition stacks. Doing so just wastes time and memory.
856
857 This is the same test to prune the set of variables which may
858 need PHI nodes. So we just use that information since it's already
859 computed and available for us to use. */
860 if (var_ann (var)->need_phi_state == NEED_PHI_STATE_NO)
861 {
862 var_ann (var)->current_def = def;
863 return;
864 }
865
866 currdef = var_ann (var)->current_def;
867 if (! *block_defs_p)
868 VARRAY_TREE_INIT (*block_defs_p, 20, "block_defs");
869
870 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
871 later used by the dominator tree callbacks to restore the reaching
872 definitions for all the variables defined in the block after a recursive
873 visit to all its immediately dominated blocks. If there is no current
874 reaching definition, then just record the underlying _DECL node. */
875 VARRAY_PUSH_TREE (*block_defs_p, currdef ? currdef : var);
876
877 /* Set the current reaching definition for VAR to be DEF. */
878 var_ann (var)->current_def = def;
879 }
880
881
882 /* Return the current definition for variable VAR. If none is found,
883 create a new SSA name to act as the zeroth definition for VAR. If VAR
884 is call clobbered and there exists a more recent definition of
885 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
886 has been clobbered by a function call since its last assignment. */
887
888 static tree
889 get_reaching_def (tree var)
890 {
891 tree default_d, currdef_var;
892
893 /* Lookup the current reaching definition for VAR. */
894 default_d = NULL_TREE;
895 currdef_var = var_ann (var)->current_def;
896
897 /* If there is no reaching definition for VAR, create and register a
898 default definition for it (if needed). */
899 if (currdef_var == NULL_TREE)
900 {
901 default_d = default_def (var);
902 if (default_d == NULL_TREE)
903 {
904 default_d = make_ssa_name (var, build_empty_stmt ());
905 set_default_def (var, default_d);
906 }
907 var_ann (var)->current_def = default_d;
908 }
909
910 /* Return the current reaching definition for VAR, or the default
911 definition, if we had to create one. */
912 return (currdef_var) ? currdef_var : default_d;
913 }
914
915
916 /* Hashing and equality functions for DEF_BLOCKS. */
917
918 static hashval_t
919 def_blocks_hash (const void *p)
920 {
921 return htab_hash_pointer
922 ((const void *)((const struct def_blocks_d *)p)->var);
923 }
924
925 static int
926 def_blocks_eq (const void *p1, const void *p2)
927 {
928 return ((const struct def_blocks_d *)p1)->var
929 == ((const struct def_blocks_d *)p2)->var;
930 }
931
932 /* Free memory allocated by one entry in DEF_BLOCKS. */
933
934 static void
935 def_blocks_free (void *p)
936 {
937 struct def_blocks_d *entry = p;
938 BITMAP_XFREE (entry->def_blocks);
939 BITMAP_XFREE (entry->livein_blocks);
940 free (entry);
941 }
942
943
944 /* Dump the DEF_BLOCKS hash table on stderr. */
945
946 void
947 debug_def_blocks (void)
948 {
949 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
950 }
951
952 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
953
954 static int
955 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
956 {
957 unsigned long i;
958 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
959
960 fprintf (stderr, "VAR: ");
961 print_generic_expr (stderr, db_p->var, dump_flags);
962 fprintf (stderr, ", DEF_BLOCKS: { ");
963 EXECUTE_IF_SET_IN_BITMAP (db_p->def_blocks, 0, i,
964 fprintf (stderr, "%ld ", i));
965 fprintf (stderr, "}");
966 fprintf (stderr, ", LIVEIN_BLOCKS: { ");
967 EXECUTE_IF_SET_IN_BITMAP (db_p->livein_blocks, 0, i,
968 fprintf (stderr, "%ld ", i));
969 fprintf (stderr, "}\n");
970
971 return 1;
972 }
973
974
975 /* Return the set of blocks where variable VAR is defined and the blocks
976 where VAR is live on entry (livein). Return NULL, if no entry is
977 found in DEF_BLOCKS. */
978
979 static inline struct def_blocks_d *
980 find_def_blocks_for (tree var)
981 {
982 struct def_blocks_d dm;
983 dm.var = var;
984 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
985 }
986
987
988 /* Return the set of blocks where variable VAR is defined and the blocks
989 where VAR is live on entry (livein). If no entry is found in
990 DEF_BLOCKS, a new one is created and returned. */
991
992 static inline struct def_blocks_d *
993 get_def_blocks_for (tree var)
994 {
995 struct def_blocks_d db, *db_p;
996 void **slot;
997
998 db.var = var;
999 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
1000 if (*slot == NULL)
1001 {
1002 db_p = xmalloc (sizeof (*db_p));
1003 db_p->var = var;
1004 db_p->def_blocks = BITMAP_XMALLOC ();
1005 db_p->livein_blocks = BITMAP_XMALLOC ();
1006 *slot = (void *) db_p;
1007 }
1008 else
1009 db_p = (struct def_blocks_d *) *slot;
1010
1011 return db_p;
1012 }
1013
1014 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
1015 process will cause us to lose the name memory tags that may have
1016 been associated with the various SSA_NAMEs of V. This means that
1017 the variables aliased to those name tags also need to be renamed
1018 again.
1019
1020 FIXME 1- We should either have a better scheme for renaming
1021 pointers that doesn't lose name tags or re-run alias
1022 analysis to recover points-to information.
1023
1024 2- Currently we just invalidate *all* the name tags. This
1025 should be more selective. */
1026
1027 static void
1028 invalidate_name_tags (bitmap vars_to_rename)
1029 {
1030 size_t i;
1031 bool rename_name_tags_p;
1032
1033 rename_name_tags_p = false;
1034 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i,
1035 if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i))))
1036 {
1037 rename_name_tags_p = true;
1038 break;
1039 });
1040
1041 if (rename_name_tags_p)
1042 for (i = 0; i < num_referenced_vars; i++)
1043 {
1044 var_ann_t ann = var_ann (referenced_var (i));
1045
1046 if (ann->mem_tag_kind == NAME_TAG)
1047 {
1048 size_t j;
1049 varray_type may_aliases = ann->may_aliases;
1050
1051 bitmap_set_bit (vars_to_rename, ann->uid);
1052 if (ann->may_aliases)
1053 for (j = 0; j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
1054 {
1055 tree var = VARRAY_TREE (may_aliases, j);
1056 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1057 }
1058 }
1059 }
1060 }
1061
1062
1063 /* Main entry point into the SSA builder. The renaming process
1064 proceeds in five main phases:
1065
1066 1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1067 those variables are removed from the flow graph so that they can
1068 be computed again.
1069
1070 2- Compute dominance frontier and immediate dominators, needed to
1071 insert PHI nodes and rename the function in dominator tree
1072 order.
1073
1074 3- Find and mark all the blocks that define variables
1075 (mark_def_sites).
1076
1077 4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1078
1079 5- Rename all the blocks (rewrite_initialize_block,
1080 rewrite_add_phi_arguments) and statements in the program
1081 (rewrite_stmt).
1082
1083 Steps 3 and 5 are done using the dominator tree walker
1084 (walk_dominator_tree). */
1085
1086 void
1087 rewrite_into_ssa (void)
1088 {
1089 bitmap *dfs;
1090 basic_block bb;
1091 struct dom_walk_data walk_data;
1092 struct mark_def_sites_global_data mark_def_sites_global_data;
1093 unsigned int i;
1094
1095 timevar_push (TV_TREE_SSA_OTHER);
1096
1097 /* Initialize the array of variables to rename. */
1098 if (vars_to_rename != NULL)
1099 {
1100 invalidate_name_tags (vars_to_rename);
1101
1102 /* Now remove all the existing PHI nodes (if any) for the variables
1103 that we are about to rename into SSA. */
1104 remove_all_phi_nodes_for (vars_to_rename);
1105 }
1106
1107 /* Allocate memory for the DEF_BLOCKS hash table. */
1108 def_blocks = htab_create (VARRAY_ACTIVE_SIZE (referenced_vars),
1109 def_blocks_hash, def_blocks_eq, def_blocks_free);
1110
1111 /* Initialize dominance frontier and immediate dominator bitmaps.
1112 Also count the number of predecessors for each block. Doing so
1113 can save significant time during PHI insertion for large graphs. */
1114 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1115 FOR_EACH_BB (bb)
1116 {
1117 edge e;
1118 int count = 0;
1119
1120 for (e = bb->pred; e; e = e->pred_next)
1121 count++;
1122
1123 bb_ann (bb)->num_preds = count;
1124 dfs[bb->index] = BITMAP_XMALLOC ();
1125 }
1126
1127 for (i = 0; i < num_referenced_vars; i++)
1128 var_ann (referenced_var (i))->current_def = NULL;
1129
1130 /* Ensure that the dominance information is OK. */
1131 calculate_dominance_info (CDI_DOMINATORS);
1132
1133 /* Compute dominance frontiers. */
1134 compute_dominance_frontiers (dfs);
1135
1136 /* Setup callbacks for the generic dominator tree walker to find and
1137 mark definition sites. */
1138 walk_data.walk_stmts_backward = false;
1139 walk_data.dom_direction = CDI_DOMINATORS;
1140 walk_data.initialize_block_local_data = NULL;
1141 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
1142 walk_data.before_dom_children_walk_stmts = mark_def_sites;
1143 walk_data.before_dom_children_after_stmts = NULL;
1144 walk_data.after_dom_children_before_stmts = NULL;
1145 walk_data.after_dom_children_walk_stmts = NULL;
1146 walk_data.after_dom_children_after_stmts = NULL;
1147
1148 /* Notice that this bitmap is indexed using variable UIDs, so it must be
1149 large enough to accommodate all the variables referenced in the
1150 function, not just the ones we are renaming. */
1151 mark_def_sites_global_data.kills = sbitmap_alloc (num_referenced_vars);
1152 walk_data.global_data = &mark_def_sites_global_data;
1153
1154 /* We do not have any local data. */
1155 walk_data.block_local_data_size = 0;
1156
1157 /* Initialize the dominator walker. */
1158 init_walk_dominator_tree (&walk_data);
1159
1160 /* Recursively walk the dominator tree. */
1161 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1162
1163 /* Finalize the dominator walker. */
1164 fini_walk_dominator_tree (&walk_data);
1165
1166 /* We no longer need this bitmap, clear and free it. */
1167 sbitmap_free (mark_def_sites_global_data.kills);
1168
1169 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1170 insert_phi_nodes (dfs);
1171
1172 /* Rewrite all the basic blocks in the program. */
1173 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1174
1175 /* Setup callbacks for the generic dominator tree walker. */
1176 walk_data.walk_stmts_backward = false;
1177 walk_data.dom_direction = CDI_DOMINATORS;
1178 walk_data.initialize_block_local_data = rewrite_initialize_block_local_data;
1179 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1180 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1181 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1182 walk_data.after_dom_children_before_stmts = NULL;
1183 walk_data.after_dom_children_walk_stmts = NULL;
1184 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1185 walk_data.global_data = NULL;
1186 walk_data.block_local_data_size = sizeof (struct rewrite_block_data);
1187
1188 /* Initialize the dominator walker. */
1189 init_walk_dominator_tree (&walk_data);
1190
1191 /* Recursively walk the dominator tree rewriting each statement in
1192 each basic block. */
1193 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1194
1195 /* Finalize the dominator walker. */
1196 fini_walk_dominator_tree (&walk_data);
1197
1198 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1199
1200 /* Debugging dumps. */
1201 if (dump_file && (dump_flags & TDF_STATS))
1202 {
1203 dump_dfa_stats (dump_file);
1204 dump_tree_ssa_stats (dump_file);
1205 }
1206
1207 /* Free allocated memory. */
1208 FOR_EACH_BB (bb)
1209 BITMAP_XFREE (dfs[bb->index]);
1210 free (dfs);
1211
1212 htab_delete (def_blocks);
1213
1214 timevar_pop (TV_TREE_SSA_OTHER);
1215 }
1216
1217 struct tree_opt_pass pass_build_ssa =
1218 {
1219 "ssa", /* name */
1220 NULL, /* gate */
1221 rewrite_into_ssa, /* execute */
1222 NULL, /* sub */
1223 NULL, /* next */
1224 0, /* static_pass_number */
1225 0, /* tv_id */
1226 PROP_cfg | PROP_referenced_vars, /* properties_required */
1227 PROP_ssa, /* properties_provided */
1228 0, /* properties_destroyed */
1229 0, /* todo_flags_start */
1230 TODO_dump_func | TODO_verify_ssa /* todo_flags_finish */
1231 };