tree-phinodes.c (add_phi_arg): Take "tree" instead of "tree *" as the first argument.
[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 "hashtab.h"
45 #include "tree-dump.h"
46 #include "tree-pass.h"
47 #include "cfgloop.h"
48 #include "domwalk.h"
49 #include "ggc.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 that contain a phi node for VAR. */
70 bitmap phi_blocks;
71
72 /* Blocks where VAR is live-on-entry. Similar semantics as
73 DEF_BLOCKS. */
74 bitmap livein_blocks;
75 };
76
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;
85
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.
88
89 This vector is used in two contexts. The first is rewriting of _DECL
90 nodes into SSA_NAMEs. In that context it's elements have the
91 following properties:
92
93 An SSA_NAME indicates that the current definition of the underlying
94 variable should be set to the given SSA_NAME.
95
96 A _DECL node indicates that the underlying variable has no current
97 definition.
98
99 A NULL node is used to mark the last node associated with the
100 current block.
101
102
103 This vector is also used when rewriting an SSA_NAME which has multiple
104 definition sites into multiple SSA_NAMEs. In that context entries come
105 in pairs.
106
107 The top entry is an SSA_NAME and the top-1 entry is the
108 current value for that SSA_NAME.
109
110 A NULL node at the top entry is used to mark the last node associated
111 with the current block. */
112 static VEC(tree_on_heap) *block_defs_stack;
113
114 /* Basic block vectors used in this file ought to be allocated in the heap. */
115 DEF_VEC_MALLOC_P(basic_block);
116
117 /* Global data to attach to the main dominator walk structure. */
118 struct mark_def_sites_global_data
119 {
120 /* This sbitmap contains the variables which are set before they
121 are used in a basic block. We keep it as a global variable
122 solely to avoid the overhead of allocating and deallocating
123 the bitmap. */
124 sbitmap kills;
125
126 /* Bitmap of names to rename. */
127 sbitmap names_to_rename;
128 };
129
130 /* Information stored for ssa names. */
131
132 struct ssa_name_info
133 {
134 /* This field indicates whether or not the variable may need PHI nodes.
135 See the enum's definition for more detailed information about the
136 states. */
137 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
138
139 /* The actual definition of the ssa name. */
140 tree current_def;
141 };
142
143 /* Local functions. */
144 static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
145 static void rewrite_initialize_block (struct dom_walk_data *, basic_block);
146 static void rewrite_add_phi_arguments (struct dom_walk_data *, basic_block);
147 static void mark_def_sites (struct dom_walk_data *walk_data,
148 basic_block bb, block_stmt_iterator);
149 static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
150 basic_block bb);
151 static void set_def_block (tree, basic_block, bool, bool);
152 static void set_livein_block (tree, basic_block);
153 static bool prepare_use_operand_for_rename (use_operand_p, size_t *uid_p);
154 static bool prepare_def_operand_for_rename (tree def, size_t *uid_p);
155 static void insert_phi_nodes (bitmap *, bitmap);
156 static void rewrite_stmt (struct dom_walk_data *, basic_block,
157 block_stmt_iterator);
158 static inline void rewrite_operand (use_operand_p);
159 static void insert_phi_nodes_for (tree, bitmap *, VEC(basic_block) *);
160 static tree get_reaching_def (tree);
161 static hashval_t def_blocks_hash (const void *);
162 static int def_blocks_eq (const void *, const void *);
163 static void def_blocks_free (void *);
164 static int debug_def_blocks_r (void **, void *);
165 static inline struct def_blocks_d *get_def_blocks_for (tree);
166 static inline struct def_blocks_d *find_def_blocks_for (tree);
167 static void htab_statistics (FILE *, htab_t);
168
169 /* Get the information associated with NAME. */
170
171 static inline struct ssa_name_info *
172 get_ssa_name_ann (tree name)
173 {
174 if (!SSA_NAME_AUX (name))
175 SSA_NAME_AUX (name) = xcalloc (1, sizeof (struct ssa_name_info));
176
177 return SSA_NAME_AUX (name);
178 }
179
180 /* Gets phi_state field for VAR. */
181
182 static inline enum need_phi_state
183 get_phi_state (tree var)
184 {
185 if (TREE_CODE (var) == SSA_NAME)
186 return get_ssa_name_ann (var)->need_phi_state;
187 else
188 return var_ann (var)->need_phi_state;
189 }
190
191 /* Sets phi_state field for VAR to STATE. */
192
193 static inline void
194 set_phi_state (tree var, enum need_phi_state state)
195 {
196 if (TREE_CODE (var) == SSA_NAME)
197 get_ssa_name_ann (var)->need_phi_state = state;
198 else
199 var_ann (var)->need_phi_state = state;
200 }
201
202 /* Return the current definition for VAR. */
203
204 static inline tree
205 get_current_def (tree var)
206 {
207 if (TREE_CODE (var) == SSA_NAME)
208 return get_ssa_name_ann (var)->current_def;
209 else
210 return var_ann (var)->current_def;
211 }
212
213 /* Sets current definition of VAR to DEF. */
214
215 static inline void
216 set_current_def (tree var, tree def)
217 {
218 if (TREE_CODE (var) == SSA_NAME)
219 get_ssa_name_ann (var)->current_def = def;
220 else
221 var_ann (var)->current_def = def;
222 }
223
224 /* Compute global livein information given the set of blockx where
225 an object is locally live at the start of the block (LIVEIN)
226 and the set of blocks where the object is defined (DEF_BLOCKS).
227
228 Note: This routine augments the existing local livein information
229 to include global livein (i.e., it modifies the underlying bitmap
230 for LIVEIN). */
231
232 void
233 compute_global_livein (bitmap livein, bitmap def_blocks)
234 {
235 basic_block bb, *worklist, *tos;
236 unsigned i;
237 bitmap_iterator bi;
238
239 tos = worklist
240 = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
241
242 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
243 {
244 *tos++ = BASIC_BLOCK (i);
245 }
246
247 /* Iterate until the worklist is empty. */
248 while (tos != worklist)
249 {
250 edge e;
251 edge_iterator ei;
252
253 /* Pull a block off the worklist. */
254 bb = *--tos;
255
256 /* For each predecessor block. */
257 FOR_EACH_EDGE (e, ei, bb->preds)
258 {
259 basic_block pred = e->src;
260 int pred_index = pred->index;
261
262 /* None of this is necessary for the entry block. */
263 if (pred != ENTRY_BLOCK_PTR
264 && ! bitmap_bit_p (livein, pred_index)
265 && ! bitmap_bit_p (def_blocks, pred_index))
266 {
267 *tos++ = pred;
268 bitmap_set_bit (livein, pred_index);
269 }
270 }
271 }
272
273 free (worklist);
274 }
275
276
277 /* Block initialization routine for mark_def_sites. Clear the
278 KILLS bitmap at the start of each block. */
279
280 static void
281 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
282 basic_block bb ATTRIBUTE_UNUSED)
283 {
284 struct mark_def_sites_global_data *gd = walk_data->global_data;
285 sbitmap kills = gd->kills;
286
287 sbitmap_zero (kills);
288 }
289
290 /* Block initialization routine for mark_def_sites. Clear the
291 KILLS bitmap at the start of each block. */
292
293 static void
294 ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
295 basic_block bb)
296 {
297 struct mark_def_sites_global_data *gd = walk_data->global_data;
298 sbitmap kills = gd->kills;
299 tree phi, def;
300 unsigned def_uid;
301
302 sbitmap_zero (kills);
303
304 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
305 {
306 def = PHI_RESULT (phi);
307 def_uid = SSA_NAME_VERSION (def);
308
309 if (!TEST_BIT (gd->names_to_rename, def_uid))
310 continue;
311
312 set_def_block (def, bb, true, true);
313 SET_BIT (kills, def_uid);
314 }
315 }
316
317 /* Marks ssa names used as arguments of phis at the end of BB. */
318
319 static void
320 ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb)
321 {
322 struct mark_def_sites_global_data *gd = walk_data->global_data;
323 sbitmap kills = gd->kills;
324 edge e;
325 tree phi, use;
326 unsigned uid;
327 edge_iterator ei;
328
329 FOR_EACH_EDGE (e, ei, bb->succs)
330 {
331 if (e->dest == EXIT_BLOCK_PTR)
332 continue;
333
334 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
335 {
336 use = PHI_ARG_DEF_FROM_EDGE (phi, e);
337 if (TREE_CODE (use) != SSA_NAME)
338 continue;
339
340 uid = SSA_NAME_VERSION (use);
341
342 if (TEST_BIT (gd->names_to_rename, uid)
343 && !TEST_BIT (kills, uid))
344 set_livein_block (use, bb);
345 }
346 }
347 }
348
349 /* Call back for walk_dominator_tree used to collect definition sites
350 for every variable in the function. For every statement S in block
351 BB:
352
353 1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
354 WALK_DATA->GLOBAL_DATA->KILLS.
355
356 2- If S uses a variable VAR and there is no preceding kill of VAR,
357 then it is marked in marked in the LIVEIN_BLOCKS bitmap
358 associated with VAR.
359
360 This information is used to determine which variables are live
361 across block boundaries to reduce the number of PHI nodes
362 we create. */
363
364 static void
365 mark_def_sites (struct dom_walk_data *walk_data,
366 basic_block bb,
367 block_stmt_iterator bsi)
368 {
369 struct mark_def_sites_global_data *gd = walk_data->global_data;
370 sbitmap kills = gd->kills;
371 size_t uid;
372 tree stmt, def;
373 use_operand_p use_p;
374 def_operand_p def_p;
375 ssa_op_iter iter;
376
377 /* Mark all the blocks that have definitions for each variable in the
378 VARS_TO_RENAME bitmap. */
379 stmt = bsi_stmt (bsi);
380 get_stmt_operands (stmt);
381
382 /* If a variable is used before being set, then the variable is live
383 across a block boundary, so mark it live-on-entry to BB. */
384
385 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE | SSA_OP_VMUSTDEFKILL)
386 {
387 if (prepare_use_operand_for_rename (use_p, &uid)
388 && !TEST_BIT (kills, uid))
389 set_livein_block (USE_FROM_PTR (use_p), bb);
390 }
391
392 /* Note that virtual definitions are irrelevant for computing KILLS
393 because a V_MAY_DEF does not constitute a killing definition of the
394 variable. However, the operand of a virtual definitions is a use
395 of the variable, so it may cause the variable to be considered
396 live-on-entry. */
397
398 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
399 {
400 if (prepare_use_operand_for_rename (use_p, &uid))
401 {
402 /* If we do not already have an SSA_NAME for our destination,
403 then set the destination to the source. */
404 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
405 SET_DEF (def_p, USE_FROM_PTR (use_p));
406
407 set_livein_block (USE_FROM_PTR (use_p), bb);
408 set_def_block (DEF_FROM_PTR (def_p), bb, false, false);
409 }
410 }
411
412 /* Now process the virtual must-defs made by this statement. */
413 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
414 {
415 if (prepare_def_operand_for_rename (def, &uid))
416 {
417 set_def_block (def, bb, false, false);
418 SET_BIT (kills, uid);
419 }
420 }
421
422 }
423
424 /* Ditto, but works over ssa names. */
425
426 static void
427 ssa_mark_def_sites (struct dom_walk_data *walk_data,
428 basic_block bb,
429 block_stmt_iterator bsi)
430 {
431 struct mark_def_sites_global_data *gd = walk_data->global_data;
432 sbitmap kills = gd->kills;
433 size_t uid, def_uid;
434 tree stmt, use, def;
435 ssa_op_iter iter;
436
437 /* Mark all the blocks that have definitions for each variable in the
438 names_to_rename bitmap. */
439 stmt = bsi_stmt (bsi);
440 get_stmt_operands (stmt);
441
442 /* If a variable is used before being set, then the variable is live
443 across a block boundary, so mark it live-on-entry to BB. */
444 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
445 {
446 uid = SSA_NAME_VERSION (use);
447
448 if (TEST_BIT (gd->names_to_rename, uid)
449 && !TEST_BIT (kills, uid))
450 set_livein_block (use, bb);
451 }
452
453 /* Now process the definition made by this statement. Mark the
454 variables in KILLS. */
455 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
456 {
457 def_uid = SSA_NAME_VERSION (def);
458
459 if (TEST_BIT (gd->names_to_rename, def_uid))
460 {
461 set_def_block (def, bb, false, true);
462 SET_BIT (kills, def_uid);
463 }
464 }
465 }
466
467 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
468 VAR is defined by a phi node. SSA_P is true if we are called from
469 rewrite_ssa_into_ssa. */
470
471 static void
472 set_def_block (tree var, basic_block bb, bool phi_p, bool ssa_p)
473 {
474 struct def_blocks_d *db_p;
475 enum need_phi_state state;
476
477 if (!ssa_p
478 && TREE_CODE (var) == SSA_NAME)
479 var = SSA_NAME_VAR (var);
480
481 state = get_phi_state (var);
482 db_p = get_def_blocks_for (var);
483
484 /* Set the bit corresponding to the block where VAR is defined. */
485 bitmap_set_bit (db_p->def_blocks, bb->index);
486 if (phi_p)
487 bitmap_set_bit (db_p->phi_blocks, bb->index);
488
489 /* Keep track of whether or not we may need to insert phi nodes.
490
491 If we are in the UNKNOWN state, then this is the first definition
492 of VAR. Additionally, we have not seen any uses of VAR yet, so
493 we do not need a phi node for this variable at this time (i.e.,
494 transition to NEED_PHI_STATE_NO).
495
496 If we are in any other state, then we either have multiple definitions
497 of this variable occurring in different blocks or we saw a use of the
498 variable which was not dominated by the block containing the
499 definition(s). In this case we may need a PHI node, so enter
500 state NEED_PHI_STATE_MAYBE. */
501 if (state == NEED_PHI_STATE_UNKNOWN)
502 set_phi_state (var, NEED_PHI_STATE_NO);
503 else
504 set_phi_state (var, NEED_PHI_STATE_MAYBE);
505 }
506
507
508 /* Mark block BB as having VAR live at the entry to BB. */
509
510 static void
511 set_livein_block (tree var, basic_block bb)
512 {
513 struct def_blocks_d *db_p;
514 enum need_phi_state state = get_phi_state (var);
515
516 db_p = get_def_blocks_for (var);
517
518 /* Set the bit corresponding to the block where VAR is live in. */
519 bitmap_set_bit (db_p->livein_blocks, bb->index);
520
521 /* Keep track of whether or not we may need to insert phi nodes.
522
523 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
524 by the single block containing the definition(s) of this variable. If
525 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
526 NEED_PHI_STATE_MAYBE. */
527 if (state == NEED_PHI_STATE_NO)
528 {
529 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
530
531 if (def_block_index == -1
532 || ! dominated_by_p (CDI_DOMINATORS, bb,
533 BASIC_BLOCK (def_block_index)))
534 set_phi_state (var, NEED_PHI_STATE_MAYBE);
535 }
536 else
537 set_phi_state (var, NEED_PHI_STATE_MAYBE);
538 }
539
540
541 /* If the use operand pointed to by OP_P needs to be renamed, then strip away
542 any SSA_NAME wrapping the operand, set *UID_P to the underlying variable's
543 uid, and return true. Otherwise return false. If the operand was an
544 SSA_NAME, change it to the stripped name. */
545
546 static bool
547 prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p)
548 {
549 tree use = USE_FROM_PTR (op_p);
550 tree var = (TREE_CODE (use) != SSA_NAME) ? use : SSA_NAME_VAR (use);
551 *uid_p = var_ann (var)->uid;
552
553 /* Ignore variables that don't need to be renamed. */
554 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
555 return false;
556
557 /* The variable needs to be renamed. If this is a use which already
558 has an SSA_NAME, then strip it off.
559
560 By not throwing away SSA_NAMEs on assignments, we avoid a lot of
561 useless churn of SSA_NAMEs without having to overly complicate the
562 renamer. */
563 if (TREE_CODE (use) == SSA_NAME)
564 SET_USE (op_p, var);
565
566 return true;
567 }
568
569 /* If the def variable DEF needs to be renamed, then strip away any SSA_NAME
570 wrapping the operand, set *UID_P to the underlying variable's uid and return
571 true. Otherwise return false. */
572
573 static bool
574 prepare_def_operand_for_rename (tree def, size_t *uid_p)
575 {
576 tree var = (TREE_CODE (def) != SSA_NAME) ? def : SSA_NAME_VAR (def);
577 *uid_p = var_ann (var)->uid;
578
579 /* Ignore variables that don't need to be renamed. */
580 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
581 return false;
582
583 return true;
584 }
585
586 /* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
587 at the dominance frontier (DFS) of blocks defining VAR.
588 WORK_STACK is the vector used to implement the worklist of basic
589 blocks. */
590
591 static inline
592 void insert_phi_nodes_1 (tree var, bitmap *dfs, VEC(basic_block) *work_stack)
593 {
594 if (get_phi_state (var) != NEED_PHI_STATE_NO)
595 insert_phi_nodes_for (var, dfs, work_stack);
596 }
597
598 /* Insert PHI nodes at the dominance frontier of blocks with variable
599 definitions. DFS contains the dominance frontier information for
600 the flowgraph. PHI nodes will only be inserted at the dominance
601 frontier of definition blocks for variables whose NEED_PHI_STATE
602 annotation is marked as ``maybe'' or ``unknown'' (computed by
603 mark_def_sites). If NAMES_TO_RENAME is not NULL, do the same but
604 for ssa name rewriting. */
605
606 static void
607 insert_phi_nodes (bitmap *dfs, bitmap names_to_rename)
608 {
609 unsigned i;
610 VEC(basic_block) *work_stack;
611 bitmap_iterator bi;
612
613 timevar_push (TV_TREE_INSERT_PHI_NODES);
614
615 /* Vector WORK_STACK is a stack of CFG blocks. Each block that contains
616 an assignment or PHI node will be pushed to this stack. */
617 work_stack = VEC_alloc (basic_block, last_basic_block);
618
619 /* Iterate over all variables in VARS_TO_RENAME. For each variable, add
620 to the work list all the blocks that have a definition for the
621 variable. PHI nodes will be added to the dominance frontier blocks of
622 each definition block. */
623 if (names_to_rename)
624 {
625 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
626 {
627 if (ssa_name (i))
628 insert_phi_nodes_1 (ssa_name (i), dfs, work_stack);
629 }
630 }
631 else if (vars_to_rename)
632 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
633 {
634 insert_phi_nodes_1 (referenced_var (i), dfs, work_stack);
635 }
636 else
637 for (i = 0; i < num_referenced_vars; i++)
638 insert_phi_nodes_1 (referenced_var (i), dfs, work_stack);
639
640 VEC_free (basic_block, work_stack);
641
642 timevar_pop (TV_TREE_INSERT_PHI_NODES);
643 }
644
645
646 /* Perform a depth-first traversal of the dominator tree looking for
647 variables to rename. BB is the block where to start searching.
648 Renaming is a five step process:
649
650 1- Every definition made by PHI nodes at the start of the blocks is
651 registered as the current definition for the corresponding variable.
652
653 2- Every statement in BB is rewritten. USE and VUSE operands are
654 rewritten with their corresponding reaching definition. DEF and
655 VDEF targets are registered as new definitions.
656
657 3- All the PHI nodes in successor blocks of BB are visited. The
658 argument corresponding to BB is replaced with its current reaching
659 definition.
660
661 4- Recursively rewrite every dominator child block of BB.
662
663 5- Restore (in reverse order) the current reaching definition for every
664 new definition introduced in this block. This is done so that when
665 we return from the recursive call, all the current reaching
666 definitions are restored to the names that were valid in the
667 dominator parent of BB. */
668
669 /* SSA Rewriting Step 1. Initialization, create a block local stack
670 of reaching definitions for new SSA names produced in this block
671 (BLOCK_DEFS). Register new definitions for every PHI node in the
672 block. */
673
674 static void
675 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
676 basic_block bb)
677 {
678 tree phi;
679
680 if (dump_file && (dump_flags & TDF_DETAILS))
681 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
682
683 /* Mark the unwind point for this block. */
684 VEC_safe_push (tree_on_heap, block_defs_stack, NULL_TREE);
685
686 /* Step 1. Register new definitions for every PHI node in the block.
687 Conceptually, all the PHI nodes are executed in parallel and each PHI
688 node introduces a new version for the associated variable. */
689 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
690 {
691 tree result = PHI_RESULT (phi);
692
693 register_new_def (result, &block_defs_stack);
694 }
695 }
696
697 /* Register DEF (an SSA_NAME) to be a new definition for the original
698 ssa name VAR and push VAR's current reaching definition
699 into the stack pointed by BLOCK_DEFS_P. */
700
701 static void
702 ssa_register_new_def (tree var, tree def)
703 {
704 tree currdef;
705
706 /* If this variable is set in a single basic block and all uses are
707 dominated by the set(s) in that single basic block, then there is
708 nothing to do. TODO we should not be called at all, and just
709 keep the original name. */
710 if (get_phi_state (var) == NEED_PHI_STATE_NO)
711 {
712 set_current_def (var, def);
713 return;
714 }
715
716 currdef = get_current_def (var);
717
718 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
719 later used by the dominator tree callbacks to restore the reaching
720 definitions for all the variables defined in the block after a recursive
721 visit to all its immediately dominated blocks. */
722 VEC_safe_push (tree_on_heap, block_defs_stack, currdef);
723 VEC_safe_push (tree_on_heap, block_defs_stack, var);
724
725 /* Set the current reaching definition for VAR to be DEF. */
726 set_current_def (var, def);
727 }
728
729 /* Ditto, for rewriting ssa names. */
730
731 static void
732 ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
733 {
734 tree phi, new_name;
735 sbitmap names_to_rename = walk_data->global_data;
736 edge e;
737 bool abnormal_phi;
738 edge_iterator ei;
739
740 if (dump_file && (dump_flags & TDF_DETAILS))
741 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
742
743 /* Mark the unwind point for this block. */
744 VEC_safe_push (tree_on_heap, block_defs_stack, NULL_TREE);
745
746 FOR_EACH_EDGE (e, ei, bb->preds)
747 if (e->flags & EDGE_ABNORMAL)
748 break;
749 abnormal_phi = (e != NULL);
750
751 /* Step 1. Register new definitions for every PHI node in the block.
752 Conceptually, all the PHI nodes are executed in parallel and each PHI
753 node introduces a new version for the associated variable. */
754 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
755 {
756 tree result = PHI_RESULT (phi);
757
758 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result)))
759 {
760 new_name = duplicate_ssa_name (result, phi);
761 SET_PHI_RESULT (phi, new_name);
762
763 if (abnormal_phi)
764 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
765 }
766 else
767 new_name = result;
768
769 ssa_register_new_def (result, new_name);
770 }
771 }
772
773 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
774 PHI nodes. For every PHI node found, add a new argument containing the
775 current reaching definition for the variable and the edge through which
776 that definition is reaching the PHI node. */
777
778 static void
779 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
780 basic_block bb)
781 {
782 edge e;
783 edge_iterator ei;
784
785 FOR_EACH_EDGE (e, ei, bb->succs)
786 {
787 tree phi;
788
789 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
790 {
791 tree currdef;
792
793 /* If this PHI node has already been rewritten, then there is
794 nothing to do for this PHI or any following PHIs since we
795 always add new PHI nodes at the start of the PHI chain. */
796 if (PHI_REWRITTEN (phi))
797 break;
798
799 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
800 add_phi_arg (phi, currdef, e);
801 }
802 }
803 }
804
805 /* Rewrite existing virtual PHI arguments so that they have the correct
806 reaching definitions. BB is the basic block whose successors contain the
807 phi nodes we want to add arguments for. */
808
809 static void
810 rewrite_virtual_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
811 basic_block bb)
812 {
813 edge e;
814 use_operand_p op;
815 edge_iterator ei;
816
817 FOR_EACH_EDGE (e, ei, bb->succs)
818 {
819 tree phi;
820
821 if (e->dest == EXIT_BLOCK_PTR)
822 continue;
823
824 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
825 {
826 tree result = PHI_RESULT (phi);
827 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
828
829 if (is_gimple_reg (result)
830 || !bitmap_bit_p (vars_to_rename,
831 var_ann (SSA_NAME_VAR (result))->uid))
832 continue;
833
834 SET_USE (op, get_reaching_def (SSA_NAME_VAR (result)));
835 if (e->flags & EDGE_ABNORMAL)
836 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
837 }
838 }
839 }
840
841 /* Ditto, for ssa name rewriting. */
842
843 static void
844 ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb)
845 {
846 edge e;
847 sbitmap names_to_rename = walk_data->global_data;
848 use_operand_p op;
849 edge_iterator ei;
850
851 FOR_EACH_EDGE (e, ei, bb->succs)
852 {
853 tree phi;
854
855 if (e->dest == EXIT_BLOCK_PTR)
856 continue;
857
858 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
859 {
860 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
861 if (TREE_CODE (USE_FROM_PTR (op)) != SSA_NAME)
862 continue;
863
864 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (op))))
865 continue;
866
867 SET_USE (op, get_reaching_def (USE_FROM_PTR (op)));
868 if (e->flags & EDGE_ABNORMAL)
869 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
870 }
871 }
872 }
873
874
875 /* Similar to restore_vars_to_original_value, except that it restores
876 CURRDEFS to its original value. */
877 static void
878 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
879 basic_block bb ATTRIBUTE_UNUSED)
880 {
881 /* Restore CURRDEFS to its original state. */
882 while (VEC_length (tree_on_heap, block_defs_stack) > 0)
883 {
884 tree tmp = VEC_pop (tree_on_heap, block_defs_stack);
885 tree saved_def, var;
886
887 if (tmp == NULL_TREE)
888 break;
889
890 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
891 definition of its underlying variable. If we recorded anything
892 else, it must have been an _DECL node and its current reaching
893 definition must have been NULL. */
894 if (TREE_CODE (tmp) == SSA_NAME)
895 {
896 saved_def = tmp;
897 var = SSA_NAME_VAR (saved_def);
898 }
899 else
900 {
901 saved_def = NULL;
902 var = tmp;
903 }
904
905 set_current_def (var, saved_def);
906 }
907 }
908
909 /* Ditto, for rewriting ssa names. */
910
911 static void
912 ssa_rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
913 basic_block bb ATTRIBUTE_UNUSED)
914 {
915
916 /* Step 5. Restore the current reaching definition for each variable
917 referenced in the block (in reverse order). */
918 while (VEC_length (tree_on_heap, block_defs_stack) > 0)
919 {
920 tree var = VEC_pop (tree_on_heap, block_defs_stack);
921 tree saved_def;
922
923 if (var == NULL)
924 break;
925
926 saved_def = VEC_pop (tree_on_heap, block_defs_stack);
927
928 set_current_def (var, saved_def);
929 }
930 }
931
932 /* Dump SSA information to FILE. */
933
934 void
935 dump_tree_ssa (FILE *file)
936 {
937 basic_block bb;
938 const char *funcname
939 = lang_hooks.decl_printable_name (current_function_decl, 2);
940
941 fprintf (file, "SSA information for %s\n\n", funcname);
942
943 FOR_EACH_BB (bb)
944 {
945 dump_bb (bb, file, 0);
946 fputs (" ", file);
947 print_generic_stmt (file, phi_nodes (bb), dump_flags);
948 fputs ("\n\n", file);
949 }
950 }
951
952
953 /* Dump SSA information to stderr. */
954
955 void
956 debug_tree_ssa (void)
957 {
958 dump_tree_ssa (stderr);
959 }
960
961
962 /* Dump SSA statistics on FILE. */
963
964 void
965 dump_tree_ssa_stats (FILE *file)
966 {
967 fprintf (file, "\nHash table statistics:\n");
968
969 fprintf (file, " def_blocks: ");
970 htab_statistics (file, def_blocks);
971
972 fprintf (file, "\n");
973 }
974
975
976 /* Dump SSA statistics on stderr. */
977
978 void
979 debug_tree_ssa_stats (void)
980 {
981 dump_tree_ssa_stats (stderr);
982 }
983
984
985 /* Dump statistics for the hash table HTAB. */
986
987 static void
988 htab_statistics (FILE *file, htab_t htab)
989 {
990 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
991 (long) htab_size (htab),
992 (long) htab_elements (htab),
993 htab_collisions (htab));
994 }
995
996
997 /* Insert PHI nodes for variable VAR using the dominance frontier
998 information given in DFS. WORK_STACK is the vector used to
999 implement the worklist of basic blocks. */
1000
1001 static void
1002 insert_phi_nodes_for (tree var, bitmap *dfs, VEC(basic_block) *work_stack)
1003 {
1004 struct def_blocks_d *def_map;
1005 bitmap phi_insertion_points;
1006 unsigned bb_index;
1007 edge e;
1008 tree phi;
1009 basic_block bb;
1010 bitmap_iterator bi;
1011
1012 def_map = find_def_blocks_for (var);
1013 if (def_map == NULL)
1014 return;
1015
1016 phi_insertion_points = BITMAP_XMALLOC ();
1017
1018 EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index, bi)
1019 {
1020 VEC_quick_push (basic_block, work_stack, BASIC_BLOCK (bb_index));
1021 }
1022
1023 /* Pop a block off the worklist, add every block that appears in
1024 the original block's dfs that we have not already processed to
1025 the worklist. Iterate until the worklist is empty. Blocks
1026 which are added to the worklist are potential sites for
1027 PHI nodes.
1028
1029 The iteration step could be done during PHI insertion just as
1030 easily. We do it here for historical reasons -- we used to have
1031 a heuristic which used the potential PHI insertion points to
1032 determine if fully pruned or semi pruned SSA form was appropriate.
1033
1034 We now always use fully pruned SSA form. */
1035 while (VEC_length (basic_block, work_stack) > 0)
1036 {
1037 unsigned dfs_index;
1038 bitmap_iterator bi;
1039
1040 bb = VEC_pop (basic_block, work_stack);
1041 bb_index = bb->index;
1042
1043 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
1044 phi_insertion_points,
1045 0, dfs_index, bi)
1046 {
1047 basic_block bb = BASIC_BLOCK (dfs_index);
1048
1049 VEC_quick_push (basic_block, work_stack, bb);
1050 bitmap_set_bit (phi_insertion_points, dfs_index);
1051 }
1052 }
1053
1054 /* Remove the blocks where we already have the phis. */
1055 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1056
1057 /* Now compute global livein for this variable. Note this modifies
1058 def_map->livein_blocks. */
1059 compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
1060
1061 /* And insert the PHI nodes. */
1062 EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
1063 0, bb_index, bi)
1064 {
1065 bb = BASIC_BLOCK (bb_index);
1066
1067 phi = create_phi_node (var, bb);
1068
1069 /* If we are rewriting ssa names, add also the phi arguments. */
1070 if (TREE_CODE (var) == SSA_NAME)
1071 {
1072 edge_iterator ei;
1073 FOR_EACH_EDGE (e, ei, bb->preds)
1074 add_phi_arg (phi, var, e);
1075 }
1076 }
1077
1078 BITMAP_XFREE (phi_insertion_points);
1079 }
1080
1081 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1082 the block with its immediate reaching definitions. Update the current
1083 definition of a variable when a new real or virtual definition is found. */
1084
1085 static void
1086 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1087 basic_block bb ATTRIBUTE_UNUSED,
1088 block_stmt_iterator si)
1089 {
1090 stmt_ann_t ann;
1091 tree stmt;
1092 use_operand_p use_p;
1093 def_operand_p def_p;
1094 ssa_op_iter iter;
1095
1096 stmt = bsi_stmt (si);
1097 ann = stmt_ann (stmt);
1098
1099 if (dump_file && (dump_flags & TDF_DETAILS))
1100 {
1101 fprintf (dump_file, "Renaming statement ");
1102 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1103 fprintf (dump_file, "\n");
1104 }
1105
1106 /* We have just scanned the code for operands. No statement should
1107 be modified. */
1108 gcc_assert (!ann->modified);
1109
1110 /* Step 1. Rewrite USES and VUSES in the statement. */
1111 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
1112 rewrite_operand (use_p);
1113
1114 /* Step 2. Register the statement's DEF and VDEF operands. */
1115 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1116 {
1117 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
1118 SET_DEF (def_p, make_ssa_name (DEF_FROM_PTR (def_p), stmt));
1119
1120 /* FIXME: We shouldn't be registering new defs if the variable
1121 doesn't need to be renamed. */
1122 register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
1123 }
1124 }
1125
1126 /* Ditto, for rewriting ssa names. */
1127
1128 static void
1129 ssa_rewrite_stmt (struct dom_walk_data *walk_data,
1130 basic_block bb ATTRIBUTE_UNUSED,
1131 block_stmt_iterator si)
1132 {
1133 stmt_ann_t ann;
1134 tree stmt, var;
1135 ssa_op_iter iter;
1136 use_operand_p use_p;
1137 def_operand_p def_p;
1138 sbitmap names_to_rename = walk_data->global_data;
1139
1140 stmt = bsi_stmt (si);
1141 ann = stmt_ann (stmt);
1142
1143 if (dump_file && (dump_flags & TDF_DETAILS))
1144 {
1145 fprintf (dump_file, "Renaming statement ");
1146 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1147 fprintf (dump_file, "\n");
1148 }
1149
1150 /* We have just scanned the code for operands. No statement should
1151 be modified. */
1152 gcc_assert (!ann->modified);
1153
1154 /* Step 1. Rewrite USES and VUSES in the statement. */
1155 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
1156 {
1157 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (use_p))))
1158 SET_USE (use_p, get_reaching_def (USE_FROM_PTR (use_p)));
1159 }
1160
1161 /* Step 2. Register the statement's DEF and VDEF operands. */
1162 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1163 {
1164 var = DEF_FROM_PTR (def_p);
1165
1166 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (var)))
1167 continue;
1168
1169 SET_DEF (def_p, duplicate_ssa_name (var, stmt));
1170 ssa_register_new_def (var, DEF_FROM_PTR (def_p));
1171 }
1172 }
1173
1174 /* Replace the operand pointed by OP_P with its immediate reaching
1175 definition. */
1176
1177 static inline void
1178 rewrite_operand (use_operand_p op_p)
1179 {
1180 if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
1181 SET_USE (op_p, get_reaching_def (USE_FROM_PTR (op_p)));
1182 }
1183
1184 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1185 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1186 into the stack pointed by BLOCK_DEFS_P. */
1187
1188 void
1189 register_new_def (tree def, VEC (tree_on_heap) **block_defs_p)
1190 {
1191 tree var = SSA_NAME_VAR (def);
1192 tree currdef;
1193
1194 /* If this variable is set in a single basic block and all uses are
1195 dominated by the set(s) in that single basic block, then there is
1196 no reason to record anything for this variable in the block local
1197 definition stacks. Doing so just wastes time and memory.
1198
1199 This is the same test to prune the set of variables which may
1200 need PHI nodes. So we just use that information since it's already
1201 computed and available for us to use. */
1202 if (get_phi_state (var) == NEED_PHI_STATE_NO)
1203 {
1204 set_current_def (var, def);
1205 return;
1206 }
1207
1208 currdef = get_current_def (var);
1209
1210 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
1211 later used by the dominator tree callbacks to restore the reaching
1212 definitions for all the variables defined in the block after a recursive
1213 visit to all its immediately dominated blocks. If there is no current
1214 reaching definition, then just record the underlying _DECL node. */
1215 VEC_safe_push (tree_on_heap, *block_defs_p, currdef ? currdef : var);
1216
1217 /* Set the current reaching definition for VAR to be DEF. */
1218 set_current_def (var, def);
1219 }
1220
1221 /* Return the current definition for variable VAR. If none is found,
1222 create a new SSA name to act as the zeroth definition for VAR. If VAR
1223 is call clobbered and there exists a more recent definition of
1224 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
1225 has been clobbered by a function call since its last assignment. */
1226
1227 static tree
1228 get_reaching_def (tree var)
1229 {
1230 tree default_d, currdef_var, avar;
1231
1232 /* Lookup the current reaching definition for VAR. */
1233 default_d = NULL_TREE;
1234 currdef_var = get_current_def (var);
1235
1236 /* If there is no reaching definition for VAR, create and register a
1237 default definition for it (if needed). */
1238 if (currdef_var == NULL_TREE)
1239 {
1240 if (TREE_CODE (var) == SSA_NAME)
1241 avar = SSA_NAME_VAR (var);
1242 else
1243 avar = var;
1244
1245 default_d = default_def (avar);
1246 if (default_d == NULL_TREE)
1247 {
1248 default_d = make_ssa_name (avar, build_empty_stmt ());
1249 set_default_def (avar, default_d);
1250 }
1251 set_current_def (var, default_d);
1252 }
1253
1254 /* Return the current reaching definition for VAR, or the default
1255 definition, if we had to create one. */
1256 return (currdef_var) ? currdef_var : default_d;
1257 }
1258
1259
1260 /* Hashing and equality functions for DEF_BLOCKS. */
1261
1262 static hashval_t
1263 def_blocks_hash (const void *p)
1264 {
1265 return htab_hash_pointer
1266 ((const void *)((const struct def_blocks_d *)p)->var);
1267 }
1268
1269 static int
1270 def_blocks_eq (const void *p1, const void *p2)
1271 {
1272 return ((const struct def_blocks_d *)p1)->var
1273 == ((const struct def_blocks_d *)p2)->var;
1274 }
1275
1276 /* Free memory allocated by one entry in DEF_BLOCKS. */
1277
1278 static void
1279 def_blocks_free (void *p)
1280 {
1281 struct def_blocks_d *entry = p;
1282 BITMAP_XFREE (entry->def_blocks);
1283 BITMAP_XFREE (entry->phi_blocks);
1284 BITMAP_XFREE (entry->livein_blocks);
1285 free (entry);
1286 }
1287
1288
1289 /* Dump the DEF_BLOCKS hash table on stderr. */
1290
1291 void
1292 debug_def_blocks (void)
1293 {
1294 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1295 }
1296
1297 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1298
1299 static int
1300 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1301 {
1302 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1303
1304 fprintf (stderr, "VAR: ");
1305 print_generic_expr (stderr, db_p->var, dump_flags);
1306 bitmap_print (stderr, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1307 bitmap_print (stderr, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}\n");
1308
1309 return 1;
1310 }
1311
1312
1313 /* Return the set of blocks where variable VAR is defined and the blocks
1314 where VAR is live on entry (livein). Return NULL, if no entry is
1315 found in DEF_BLOCKS. */
1316
1317 static inline struct def_blocks_d *
1318 find_def_blocks_for (tree var)
1319 {
1320 struct def_blocks_d dm;
1321 dm.var = var;
1322 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1323 }
1324
1325
1326 /* Return the set of blocks where variable VAR is defined and the blocks
1327 where VAR is live on entry (livein). If no entry is found in
1328 DEF_BLOCKS, a new one is created and returned. */
1329
1330 static inline struct def_blocks_d *
1331 get_def_blocks_for (tree var)
1332 {
1333 struct def_blocks_d db, *db_p;
1334 void **slot;
1335
1336 db.var = var;
1337 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
1338 if (*slot == NULL)
1339 {
1340 db_p = xmalloc (sizeof (*db_p));
1341 db_p->var = var;
1342 db_p->def_blocks = BITMAP_XMALLOC ();
1343 db_p->phi_blocks = BITMAP_XMALLOC ();
1344 db_p->livein_blocks = BITMAP_XMALLOC ();
1345 *slot = (void *) db_p;
1346 }
1347 else
1348 db_p = (struct def_blocks_d *) *slot;
1349
1350 return db_p;
1351 }
1352
1353 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
1354 process will cause us to lose the name memory tags that may have
1355 been associated with the various SSA_NAMEs of V. This means that
1356 the variables aliased to those name tags also need to be renamed
1357 again.
1358
1359 FIXME 1- We should either have a better scheme for renaming
1360 pointers that doesn't lose name tags or re-run alias
1361 analysis to recover points-to information.
1362
1363 2- Currently we just invalidate *all* the name tags. This
1364 should be more selective. */
1365
1366 static void
1367 invalidate_name_tags (bitmap vars_to_rename)
1368 {
1369 unsigned i;
1370 bool rename_name_tags_p;
1371 bitmap_iterator bi;
1372
1373 rename_name_tags_p = false;
1374 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
1375 {
1376 if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i))))
1377 {
1378 rename_name_tags_p = true;
1379 break;
1380 }
1381 }
1382
1383 if (rename_name_tags_p)
1384 for (i = 0; i < num_referenced_vars; i++)
1385 {
1386 var_ann_t ann = var_ann (referenced_var (i));
1387
1388 if (ann->mem_tag_kind == NAME_TAG)
1389 {
1390 size_t j;
1391 varray_type may_aliases = ann->may_aliases;
1392
1393 bitmap_set_bit (vars_to_rename, ann->uid);
1394 if (ann->may_aliases)
1395 for (j = 0; j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
1396 {
1397 tree var = VARRAY_TREE (may_aliases, j);
1398 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1399 }
1400 }
1401 }
1402 }
1403
1404 /* Rewrite the actual blocks, statements, and phi arguments, to be in SSA
1405 form. FIX_VIRTUAL_PHIS is true if we should only be fixing up virtual
1406 phi arguments, instead of adding new phi arguments for just added phi
1407 nodes. */
1408
1409
1410 static void
1411 rewrite_blocks (bool fix_virtual_phis)
1412 {
1413 struct dom_walk_data walk_data;
1414
1415 /* Rewrite all the basic blocks in the program. */
1416 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1417
1418 /* Setup callbacks for the generic dominator tree walker. */
1419 walk_data.walk_stmts_backward = false;
1420 walk_data.dom_direction = CDI_DOMINATORS;
1421 walk_data.initialize_block_local_data = NULL;
1422 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1423 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1424 walk_data.before_dom_children_after_stmts = NULL;
1425 if (!fix_virtual_phis)
1426 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1427 else
1428 walk_data.before_dom_children_after_stmts = rewrite_virtual_phi_arguments;
1429
1430 walk_data.after_dom_children_before_stmts = NULL;
1431 walk_data.after_dom_children_walk_stmts = NULL;
1432 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1433 walk_data.global_data = NULL;
1434 walk_data.block_local_data_size = 0;
1435
1436 block_defs_stack = VEC_alloc (tree_on_heap, 10);
1437
1438 /* Initialize the dominator walker. */
1439 init_walk_dominator_tree (&walk_data);
1440
1441 /* Recursively walk the dominator tree rewriting each statement in
1442 each basic block. */
1443 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1444
1445 /* Finalize the dominator walker. */
1446 fini_walk_dominator_tree (&walk_data);
1447
1448 htab_delete (def_blocks);
1449
1450 VEC_free (tree_on_heap, block_defs_stack);
1451 block_defs_stack = NULL;
1452
1453 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1454 }
1455
1456 /* Mark the definition site blocks for each variable, so that we know where
1457 the variable is actually live. */
1458
1459 static void
1460 mark_def_site_blocks (void)
1461 {
1462 size_t i;
1463 struct dom_walk_data walk_data;
1464 struct mark_def_sites_global_data mark_def_sites_global_data;
1465
1466 /* Allocate memory for the DEF_BLOCKS hash table. */
1467 def_blocks = htab_create (VARRAY_ACTIVE_SIZE (referenced_vars),
1468 def_blocks_hash, def_blocks_eq, def_blocks_free);
1469
1470 for (i = 0; i < num_referenced_vars; i++)
1471 set_current_def (referenced_var (i), NULL_TREE);
1472
1473 /* Ensure that the dominance information is OK. */
1474 calculate_dominance_info (CDI_DOMINATORS);
1475
1476
1477 /* Setup callbacks for the generic dominator tree walker to find and
1478 mark definition sites. */
1479 walk_data.walk_stmts_backward = false;
1480 walk_data.dom_direction = CDI_DOMINATORS;
1481 walk_data.initialize_block_local_data = NULL;
1482 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
1483 walk_data.before_dom_children_walk_stmts = mark_def_sites;
1484 walk_data.before_dom_children_after_stmts = NULL;
1485 walk_data.after_dom_children_before_stmts = NULL;
1486 walk_data.after_dom_children_walk_stmts = NULL;
1487 walk_data.after_dom_children_after_stmts = NULL;
1488
1489 /* Notice that this bitmap is indexed using variable UIDs, so it must be
1490 large enough to accommodate all the variables referenced in the
1491 function, not just the ones we are renaming. */
1492 mark_def_sites_global_data.kills = sbitmap_alloc (num_referenced_vars);
1493 walk_data.global_data = &mark_def_sites_global_data;
1494
1495 /* We do not have any local data. */
1496 walk_data.block_local_data_size = 0;
1497
1498 /* Initialize the dominator walker. */
1499 init_walk_dominator_tree (&walk_data);
1500
1501 /* Recursively walk the dominator tree. */
1502 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1503
1504 /* Finalize the dominator walker. */
1505 fini_walk_dominator_tree (&walk_data);
1506
1507 /* We no longer need this bitmap, clear and free it. */
1508 sbitmap_free (mark_def_sites_global_data.kills);
1509
1510 }
1511 /* Main entry point into the SSA builder. The renaming process
1512 proceeds in five main phases:
1513
1514 1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1515 those variables are removed from the flow graph so that they can
1516 be computed again.
1517
1518 2- Compute dominance frontier and immediate dominators, needed to
1519 insert PHI nodes and rename the function in dominator tree
1520 order.
1521
1522 3- Find and mark all the blocks that define variables
1523 (mark_def_site_blocks).
1524
1525 4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1526
1527 5- Rename all the blocks (rewrite_blocks) and statements in the program.
1528
1529 Steps 3 and 5 are done using the dominator tree walker
1530 (walk_dominator_tree).
1531
1532 ALL is true if all variables should be renamed (otherwise just those
1533 mentioned in vars_to_rename are taken into account). */
1534
1535 void
1536 rewrite_into_ssa (bool all)
1537 {
1538 bitmap *dfs;
1539 basic_block bb;
1540 bitmap old_vars_to_rename = vars_to_rename;
1541
1542 timevar_push (TV_TREE_SSA_OTHER);
1543
1544 if (all)
1545 vars_to_rename = NULL;
1546 else
1547 {
1548 /* Initialize the array of variables to rename. */
1549 gcc_assert (vars_to_rename);
1550
1551 if (bitmap_empty_p (vars_to_rename))
1552 {
1553 timevar_pop (TV_TREE_SSA_OTHER);
1554 return;
1555 }
1556
1557 invalidate_name_tags (vars_to_rename);
1558
1559 /* Now remove all the existing PHI nodes (if any) for the variables
1560 that we are about to rename into SSA. */
1561 remove_all_phi_nodes_for (vars_to_rename);
1562 }
1563
1564 mark_def_site_blocks ();
1565
1566 /* Initialize dominance frontier and immediate dominator bitmaps.
1567 Also count the number of predecessors for each block. Doing so
1568 can save significant time during PHI insertion for large graphs. */
1569 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1570 FOR_EACH_BB (bb)
1571 dfs[bb->index] = BITMAP_XMALLOC ();
1572
1573 /* Compute dominance frontiers. */
1574 compute_dominance_frontiers (dfs);
1575
1576 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1577 insert_phi_nodes (dfs, NULL);
1578
1579 rewrite_blocks (false);
1580
1581 /* Debugging dumps. */
1582 if (dump_file && (dump_flags & TDF_STATS))
1583 {
1584 dump_dfa_stats (dump_file);
1585 dump_tree_ssa_stats (dump_file);
1586 }
1587
1588 /* Free allocated memory. */
1589 FOR_EACH_BB (bb)
1590 BITMAP_XFREE (dfs[bb->index]);
1591 free (dfs);
1592
1593 vars_to_rename = old_vars_to_rename;
1594 timevar_pop (TV_TREE_SSA_OTHER);
1595 }
1596
1597 /* Rewrite the def-def chains so that they have the correct reaching
1598 definitions. */
1599
1600 void
1601 rewrite_def_def_chains (void)
1602 {
1603 /* Ensure that the dominance information is OK. */
1604 calculate_dominance_info (CDI_DOMINATORS);
1605 mark_def_site_blocks ();
1606 rewrite_blocks (true);
1607
1608 }
1609 /* The marked ssa names may have more than one definition;
1610 add phi nodes and rewrite them to fix this. */
1611
1612 void
1613 rewrite_ssa_into_ssa (void)
1614 {
1615 bitmap *dfs;
1616 basic_block bb;
1617 struct dom_walk_data walk_data;
1618 struct mark_def_sites_global_data mark_def_sites_global_data;
1619 unsigned i;
1620 sbitmap snames_to_rename;
1621 tree name;
1622 bitmap to_rename;
1623 bitmap_iterator bi;
1624
1625 if (!any_marked_for_rewrite_p ())
1626 return;
1627 to_rename = marked_ssa_names ();
1628
1629 timevar_push (TV_TREE_SSA_OTHER);
1630
1631 /* Allocate memory for the DEF_BLOCKS hash table. */
1632 def_blocks = htab_create (num_ssa_names,
1633 def_blocks_hash, def_blocks_eq, def_blocks_free);
1634
1635 /* Initialize dominance frontier and immediate dominator bitmaps.
1636 Also count the number of predecessors for each block. Doing so
1637 can save significant time during PHI insertion for large graphs. */
1638 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1639 FOR_EACH_BB (bb)
1640 dfs[bb->index] = BITMAP_XMALLOC ();
1641
1642 /* Ensure that the dominance information is OK. */
1643 calculate_dominance_info (CDI_DOMINATORS);
1644
1645 /* Compute dominance frontiers. */
1646 compute_dominance_frontiers (dfs);
1647
1648 /* Setup callbacks for the generic dominator tree walker to find and
1649 mark definition sites. */
1650 walk_data.walk_stmts_backward = false;
1651 walk_data.dom_direction = CDI_DOMINATORS;
1652 walk_data.initialize_block_local_data = NULL;
1653 walk_data.before_dom_children_before_stmts
1654 = ssa_mark_def_sites_initialize_block;
1655 walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites;
1656 walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses;
1657 walk_data.after_dom_children_before_stmts = NULL;
1658 walk_data.after_dom_children_walk_stmts = NULL;
1659 walk_data.after_dom_children_after_stmts = NULL;
1660
1661 snames_to_rename = sbitmap_alloc (num_ssa_names);
1662 sbitmap_zero (snames_to_rename);
1663 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1664 {
1665 SET_BIT (snames_to_rename, i);
1666 }
1667
1668 mark_def_sites_global_data.kills = sbitmap_alloc (num_ssa_names);
1669 mark_def_sites_global_data.names_to_rename = snames_to_rename;
1670 walk_data.global_data = &mark_def_sites_global_data;
1671
1672 block_defs_stack = VEC_alloc (tree_on_heap, 10);
1673
1674 /* We do not have any local data. */
1675 walk_data.block_local_data_size = 0;
1676
1677 /* Initialize the dominator walker. */
1678 init_walk_dominator_tree (&walk_data);
1679
1680 /* Recursively walk the dominator tree. */
1681 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1682
1683 /* Finalize the dominator walker. */
1684 fini_walk_dominator_tree (&walk_data);
1685
1686 /* We no longer need this bitmap, clear and free it. */
1687 sbitmap_free (mark_def_sites_global_data.kills);
1688
1689 for (i = 1; i < num_ssa_names; i++)
1690 if (ssa_name (i))
1691 set_current_def (ssa_name (i), NULL_TREE);
1692
1693 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1694 insert_phi_nodes (dfs, to_rename);
1695
1696 /* Rewrite all the basic blocks in the program. */
1697 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1698
1699 /* Setup callbacks for the generic dominator tree walker. */
1700 walk_data.walk_stmts_backward = false;
1701 walk_data.dom_direction = CDI_DOMINATORS;
1702 walk_data.initialize_block_local_data = NULL;
1703 walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block;
1704 walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt;
1705 walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments;
1706 walk_data.after_dom_children_before_stmts = NULL;
1707 walk_data.after_dom_children_walk_stmts = NULL;
1708 walk_data.after_dom_children_after_stmts = ssa_rewrite_finalize_block;
1709 walk_data.global_data = snames_to_rename;
1710 walk_data.block_local_data_size = 0;
1711
1712 /* Initialize the dominator walker. */
1713 init_walk_dominator_tree (&walk_data);
1714
1715 /* Recursively walk the dominator tree rewriting each statement in
1716 each basic block. */
1717 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1718
1719 /* Finalize the dominator walker. */
1720 fini_walk_dominator_tree (&walk_data);
1721
1722 unmark_all_for_rewrite ();
1723
1724 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1725 {
1726 release_ssa_name (ssa_name (i));
1727 }
1728
1729 sbitmap_free (snames_to_rename);
1730
1731 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1732
1733 /* Debugging dumps. */
1734 if (dump_file && (dump_flags & TDF_STATS))
1735 {
1736 dump_dfa_stats (dump_file);
1737 dump_tree_ssa_stats (dump_file);
1738 }
1739
1740 /* Free allocated memory. */
1741 FOR_EACH_BB (bb)
1742 BITMAP_XFREE (dfs[bb->index]);
1743 free (dfs);
1744
1745 htab_delete (def_blocks);
1746
1747 for (i = 1; i < num_ssa_names; i++)
1748 {
1749 name = ssa_name (i);
1750 if (!name || !SSA_NAME_AUX (name))
1751 continue;
1752
1753 free (SSA_NAME_AUX (name));
1754 SSA_NAME_AUX (name) = NULL;
1755 }
1756
1757 BITMAP_XFREE (to_rename);
1758
1759 VEC_free (tree_on_heap, block_defs_stack);
1760 block_defs_stack = NULL;
1761 timevar_pop (TV_TREE_SSA_OTHER);
1762 }
1763
1764 /* Rewrites all variables into ssa. */
1765
1766 static void
1767 rewrite_all_into_ssa (void)
1768 {
1769 rewrite_into_ssa (true);
1770 }
1771
1772 struct tree_opt_pass pass_build_ssa =
1773 {
1774 "ssa", /* name */
1775 NULL, /* gate */
1776 rewrite_all_into_ssa, /* execute */
1777 NULL, /* sub */
1778 NULL, /* next */
1779 0, /* static_pass_number */
1780 0, /* tv_id */
1781 PROP_cfg | PROP_referenced_vars, /* properties_required */
1782 PROP_ssa, /* properties_provided */
1783 0, /* properties_destroyed */
1784 0, /* todo_flags_start */
1785 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
1786 0 /* letter */
1787 };