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