1e4b17023SJohn Marino /* Generic routines for manipulating SSA_NAME expressions
2e4b17023SJohn Marino Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010
3e4b17023SJohn Marino Free Software Foundation, Inc.
4e4b17023SJohn Marino
5e4b17023SJohn Marino This file is part of GCC.
6e4b17023SJohn Marino
7e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
8e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
9e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
10e4b17023SJohn Marino any later version.
11e4b17023SJohn Marino
12e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
13e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15e4b17023SJohn Marino GNU General Public License for more details.
16e4b17023SJohn Marino
17e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20e4b17023SJohn Marino
21e4b17023SJohn Marino #include "config.h"
22e4b17023SJohn Marino #include "system.h"
23e4b17023SJohn Marino #include "coretypes.h"
24e4b17023SJohn Marino #include "tm.h"
25e4b17023SJohn Marino #include "tree.h"
26e4b17023SJohn Marino #include "tree-flow.h"
27e4b17023SJohn Marino #include "tree-pass.h"
28e4b17023SJohn Marino
29e4b17023SJohn Marino /* Rewriting a function into SSA form can create a huge number of SSA_NAMEs,
30e4b17023SJohn Marino many of which may be thrown away shortly after their creation if jumps
31e4b17023SJohn Marino were threaded through PHI nodes.
32e4b17023SJohn Marino
33e4b17023SJohn Marino While our garbage collection mechanisms will handle this situation, it
34e4b17023SJohn Marino is extremely wasteful to create nodes and throw them away, especially
35e4b17023SJohn Marino when the nodes can be reused.
36e4b17023SJohn Marino
37e4b17023SJohn Marino For PR 8361, we can significantly reduce the number of nodes allocated
38e4b17023SJohn Marino and thus the total amount of memory allocated by managing SSA_NAMEs a
39e4b17023SJohn Marino little. This additionally helps reduce the amount of work done by the
40e4b17023SJohn Marino garbage collector. Similar results have been seen on a wider variety
41e4b17023SJohn Marino of tests (such as the compiler itself).
42e4b17023SJohn Marino
43e4b17023SJohn Marino Right now we maintain our free list on a per-function basis. It may
44e4b17023SJohn Marino or may not make sense to maintain the free list for the duration of
45e4b17023SJohn Marino a compilation unit.
46e4b17023SJohn Marino
47e4b17023SJohn Marino External code should rely solely upon HIGHEST_SSA_VERSION and the
48e4b17023SJohn Marino externally defined functions. External code should not know about
49e4b17023SJohn Marino the details of the free list management.
50e4b17023SJohn Marino
51e4b17023SJohn Marino External code should also not assume the version number on nodes is
52e4b17023SJohn Marino monotonically increasing. We reuse the version number when we
53e4b17023SJohn Marino reuse an SSA_NAME expression. This helps keep arrays and bitmaps
54e4b17023SJohn Marino more compact.
55e4b17023SJohn Marino
56e4b17023SJohn Marino We could also use a zone allocator for these objects since they have
57e4b17023SJohn Marino a very well defined lifetime. If someone wants to experiment with that
58e4b17023SJohn Marino this is the place to try it. */
59e4b17023SJohn Marino
60e4b17023SJohn Marino /* Version numbers with special meanings. We start allocating new version
61e4b17023SJohn Marino numbers after the special ones. */
62e4b17023SJohn Marino #define UNUSED_NAME_VERSION 0
63e4b17023SJohn Marino
64e4b17023SJohn Marino #ifdef GATHER_STATISTICS
65e4b17023SJohn Marino unsigned int ssa_name_nodes_reused;
66e4b17023SJohn Marino unsigned int ssa_name_nodes_created;
67e4b17023SJohn Marino #endif
68e4b17023SJohn Marino
69e4b17023SJohn Marino /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
70e4b17023SJohn Marino zero use default. */
71e4b17023SJohn Marino
72e4b17023SJohn Marino void
init_ssanames(struct function * fn,int size)73e4b17023SJohn Marino init_ssanames (struct function *fn, int size)
74e4b17023SJohn Marino {
75e4b17023SJohn Marino if (size < 50)
76e4b17023SJohn Marino size = 50;
77e4b17023SJohn Marino
78e4b17023SJohn Marino SSANAMES (fn) = VEC_alloc (tree, gc, size);
79e4b17023SJohn Marino
80e4b17023SJohn Marino /* Version 0 is special, so reserve the first slot in the table. Though
81e4b17023SJohn Marino currently unused, we may use version 0 in alias analysis as part of
82e4b17023SJohn Marino the heuristics used to group aliases when the alias sets are too
83e4b17023SJohn Marino large.
84e4b17023SJohn Marino
85e4b17023SJohn Marino We use VEC_quick_push here because we know that SSA_NAMES has at
86e4b17023SJohn Marino least 50 elements reserved in it. */
87e4b17023SJohn Marino VEC_quick_push (tree, SSANAMES (fn), NULL_TREE);
88e4b17023SJohn Marino FREE_SSANAMES (fn) = NULL;
89e4b17023SJohn Marino
90e4b17023SJohn Marino SYMS_TO_RENAME (fn) = BITMAP_GGC_ALLOC ();
91e4b17023SJohn Marino }
92e4b17023SJohn Marino
93e4b17023SJohn Marino /* Finalize management of SSA_NAMEs. */
94e4b17023SJohn Marino
95e4b17023SJohn Marino void
fini_ssanames(void)96e4b17023SJohn Marino fini_ssanames (void)
97e4b17023SJohn Marino {
98e4b17023SJohn Marino VEC_free (tree, gc, SSANAMES (cfun));
99e4b17023SJohn Marino VEC_free (tree, gc, FREE_SSANAMES (cfun));
100e4b17023SJohn Marino }
101e4b17023SJohn Marino
102e4b17023SJohn Marino /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
103e4b17023SJohn Marino
104e4b17023SJohn Marino #ifdef GATHER_STATISTICS
105e4b17023SJohn Marino void
ssanames_print_statistics(void)106e4b17023SJohn Marino ssanames_print_statistics (void)
107e4b17023SJohn Marino {
108e4b17023SJohn Marino fprintf (stderr, "SSA_NAME nodes allocated: %u\n", ssa_name_nodes_created);
109e4b17023SJohn Marino fprintf (stderr, "SSA_NAME nodes reused: %u\n", ssa_name_nodes_reused);
110e4b17023SJohn Marino }
111e4b17023SJohn Marino #endif
112e4b17023SJohn Marino
113e4b17023SJohn Marino /* Return an SSA_NAME node for variable VAR defined in statement STMT
114e4b17023SJohn Marino in function FN. STMT may be an empty statement for artificial
115e4b17023SJohn Marino references (e.g., default definitions created when a variable is
116e4b17023SJohn Marino used without a preceding definition). */
117e4b17023SJohn Marino
118e4b17023SJohn Marino tree
make_ssa_name_fn(struct function * fn,tree var,gimple stmt)119e4b17023SJohn Marino make_ssa_name_fn (struct function *fn, tree var, gimple stmt)
120e4b17023SJohn Marino {
121e4b17023SJohn Marino tree t;
122e4b17023SJohn Marino use_operand_p imm;
123e4b17023SJohn Marino
124e4b17023SJohn Marino gcc_assert (DECL_P (var));
125e4b17023SJohn Marino
126e4b17023SJohn Marino /* If our free list has an element, then use it. */
127e4b17023SJohn Marino if (!VEC_empty (tree, FREE_SSANAMES (fn)))
128e4b17023SJohn Marino {
129e4b17023SJohn Marino t = VEC_pop (tree, FREE_SSANAMES (fn));
130e4b17023SJohn Marino #ifdef GATHER_STATISTICS
131e4b17023SJohn Marino ssa_name_nodes_reused++;
132e4b17023SJohn Marino #endif
133e4b17023SJohn Marino
134e4b17023SJohn Marino /* The node was cleared out when we put it on the free list, so
135e4b17023SJohn Marino there is no need to do so again here. */
136*95d28233SJohn Marino gcc_assert (VEC_index (tree, SSANAMES (fn), SSA_NAME_VERSION (t)) == NULL);
137e4b17023SJohn Marino VEC_replace (tree, SSANAMES (fn), SSA_NAME_VERSION (t), t);
138e4b17023SJohn Marino }
139e4b17023SJohn Marino else
140e4b17023SJohn Marino {
141e4b17023SJohn Marino t = make_node (SSA_NAME);
142e4b17023SJohn Marino SSA_NAME_VERSION (t) = VEC_length (tree, SSANAMES (fn));
143e4b17023SJohn Marino VEC_safe_push (tree, gc, SSANAMES (fn), t);
144e4b17023SJohn Marino #ifdef GATHER_STATISTICS
145e4b17023SJohn Marino ssa_name_nodes_created++;
146e4b17023SJohn Marino #endif
147e4b17023SJohn Marino }
148e4b17023SJohn Marino
149e4b17023SJohn Marino TREE_TYPE (t) = TREE_TYPE (var);
150e4b17023SJohn Marino SSA_NAME_VAR (t) = var;
151e4b17023SJohn Marino SSA_NAME_DEF_STMT (t) = stmt;
152e4b17023SJohn Marino SSA_NAME_PTR_INFO (t) = NULL;
153e4b17023SJohn Marino SSA_NAME_IN_FREE_LIST (t) = 0;
154e4b17023SJohn Marino SSA_NAME_IS_DEFAULT_DEF (t) = 0;
155e4b17023SJohn Marino imm = &(SSA_NAME_IMM_USE_NODE (t));
156e4b17023SJohn Marino imm->use = NULL;
157e4b17023SJohn Marino imm->prev = imm;
158e4b17023SJohn Marino imm->next = imm;
159e4b17023SJohn Marino imm->loc.ssa_name = t;
160e4b17023SJohn Marino
161e4b17023SJohn Marino return t;
162e4b17023SJohn Marino }
163e4b17023SJohn Marino
164e4b17023SJohn Marino
165e4b17023SJohn Marino /* We no longer need the SSA_NAME expression VAR, release it so that
166e4b17023SJohn Marino it may be reused.
167e4b17023SJohn Marino
168e4b17023SJohn Marino Note it is assumed that no calls to make_ssa_name will be made
169e4b17023SJohn Marino until all uses of the ssa name are released and that the only
170e4b17023SJohn Marino use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
171e4b17023SJohn Marino other fields must be assumed clobbered. */
172e4b17023SJohn Marino
173e4b17023SJohn Marino void
release_ssa_name(tree var)174e4b17023SJohn Marino release_ssa_name (tree var)
175e4b17023SJohn Marino {
176e4b17023SJohn Marino if (!var)
177e4b17023SJohn Marino return;
178e4b17023SJohn Marino
179e4b17023SJohn Marino /* Never release the default definition for a symbol. It's a
180e4b17023SJohn Marino special SSA name that should always exist once it's created. */
181e4b17023SJohn Marino if (SSA_NAME_IS_DEFAULT_DEF (var))
182e4b17023SJohn Marino return;
183e4b17023SJohn Marino
184e4b17023SJohn Marino /* If VAR has been registered for SSA updating, don't remove it.
185e4b17023SJohn Marino After update_ssa has run, the name will be released. */
186e4b17023SJohn Marino if (name_registered_for_update_p (var))
187e4b17023SJohn Marino {
188e4b17023SJohn Marino release_ssa_name_after_update_ssa (var);
189e4b17023SJohn Marino return;
190e4b17023SJohn Marino }
191e4b17023SJohn Marino
192e4b17023SJohn Marino /* release_ssa_name can be called multiple times on a single SSA_NAME.
193e4b17023SJohn Marino However, it should only end up on our free list one time. We
194e4b17023SJohn Marino keep a status bit in the SSA_NAME node itself to indicate it has
195e4b17023SJohn Marino been put on the free list.
196e4b17023SJohn Marino
197e4b17023SJohn Marino Note that once on the freelist you can not reference the SSA_NAME's
198e4b17023SJohn Marino defining statement. */
199e4b17023SJohn Marino if (! SSA_NAME_IN_FREE_LIST (var))
200e4b17023SJohn Marino {
201e4b17023SJohn Marino tree saved_ssa_name_var = SSA_NAME_VAR (var);
202e4b17023SJohn Marino int saved_ssa_name_version = SSA_NAME_VERSION (var);
203e4b17023SJohn Marino use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
204e4b17023SJohn Marino
205e4b17023SJohn Marino if (MAY_HAVE_DEBUG_STMTS)
206e4b17023SJohn Marino insert_debug_temp_for_var_def (NULL, var);
207e4b17023SJohn Marino
208e4b17023SJohn Marino #ifdef ENABLE_CHECKING
209e4b17023SJohn Marino verify_imm_links (stderr, var);
210e4b17023SJohn Marino #endif
211e4b17023SJohn Marino while (imm->next != imm)
212e4b17023SJohn Marino delink_imm_use (imm->next);
213e4b17023SJohn Marino
214e4b17023SJohn Marino VEC_replace (tree, SSANAMES (cfun),
215e4b17023SJohn Marino SSA_NAME_VERSION (var), NULL_TREE);
216e4b17023SJohn Marino memset (var, 0, tree_size (var));
217e4b17023SJohn Marino
218e4b17023SJohn Marino imm->prev = imm;
219e4b17023SJohn Marino imm->next = imm;
220e4b17023SJohn Marino imm->loc.ssa_name = var;
221e4b17023SJohn Marino
222e4b17023SJohn Marino /* First put back the right tree node so that the tree checking
223e4b17023SJohn Marino macros do not complain. */
224e4b17023SJohn Marino TREE_SET_CODE (var, SSA_NAME);
225e4b17023SJohn Marino
226e4b17023SJohn Marino /* Restore the version number. */
227e4b17023SJohn Marino SSA_NAME_VERSION (var) = saved_ssa_name_version;
228e4b17023SJohn Marino
229e4b17023SJohn Marino /* Hopefully this can go away once we have the new incremental
230e4b17023SJohn Marino SSA updating code installed. */
231e4b17023SJohn Marino SSA_NAME_VAR (var) = saved_ssa_name_var;
232e4b17023SJohn Marino
233e4b17023SJohn Marino /* Note this SSA_NAME is now in the first list. */
234e4b17023SJohn Marino SSA_NAME_IN_FREE_LIST (var) = 1;
235e4b17023SJohn Marino
236e4b17023SJohn Marino /* And finally put it on the free list. */
237e4b17023SJohn Marino VEC_safe_push (tree, gc, FREE_SSANAMES (cfun), var);
238e4b17023SJohn Marino }
239e4b17023SJohn Marino }
240e4b17023SJohn Marino
241e4b17023SJohn Marino
242e4b17023SJohn Marino /* Return the alias information associated with pointer T. It creates a
243e4b17023SJohn Marino new instance if none existed. */
244e4b17023SJohn Marino
245e4b17023SJohn Marino struct ptr_info_def *
get_ptr_info(tree t)246e4b17023SJohn Marino get_ptr_info (tree t)
247e4b17023SJohn Marino {
248e4b17023SJohn Marino struct ptr_info_def *pi;
249e4b17023SJohn Marino
250e4b17023SJohn Marino gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
251e4b17023SJohn Marino
252e4b17023SJohn Marino pi = SSA_NAME_PTR_INFO (t);
253e4b17023SJohn Marino if (pi == NULL)
254e4b17023SJohn Marino {
255e4b17023SJohn Marino pi = ggc_alloc_cleared_ptr_info_def ();
256e4b17023SJohn Marino pt_solution_reset (&pi->pt);
257e4b17023SJohn Marino pi->align = 1;
258e4b17023SJohn Marino pi->misalign = 0;
259e4b17023SJohn Marino SSA_NAME_PTR_INFO (t) = pi;
260e4b17023SJohn Marino }
261e4b17023SJohn Marino
262e4b17023SJohn Marino return pi;
263e4b17023SJohn Marino }
264e4b17023SJohn Marino
265e4b17023SJohn Marino /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
266e4b17023SJohn Marino the SSA name NAME. */
267e4b17023SJohn Marino
268e4b17023SJohn Marino void
duplicate_ssa_name_ptr_info(tree name,struct ptr_info_def * ptr_info)269e4b17023SJohn Marino duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
270e4b17023SJohn Marino {
271e4b17023SJohn Marino struct ptr_info_def *new_ptr_info;
272e4b17023SJohn Marino
273e4b17023SJohn Marino gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
274e4b17023SJohn Marino gcc_assert (!SSA_NAME_PTR_INFO (name));
275e4b17023SJohn Marino
276e4b17023SJohn Marino if (!ptr_info)
277e4b17023SJohn Marino return;
278e4b17023SJohn Marino
279e4b17023SJohn Marino new_ptr_info = ggc_alloc_ptr_info_def ();
280e4b17023SJohn Marino *new_ptr_info = *ptr_info;
281e4b17023SJohn Marino
282e4b17023SJohn Marino SSA_NAME_PTR_INFO (name) = new_ptr_info;
283e4b17023SJohn Marino }
284e4b17023SJohn Marino
285e4b17023SJohn Marino
286e4b17023SJohn Marino /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT. */
287e4b17023SJohn Marino
288e4b17023SJohn Marino tree
duplicate_ssa_name(tree name,gimple stmt)289e4b17023SJohn Marino duplicate_ssa_name (tree name, gimple stmt)
290e4b17023SJohn Marino {
291e4b17023SJohn Marino tree new_name = make_ssa_name (SSA_NAME_VAR (name), stmt);
292e4b17023SJohn Marino struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
293e4b17023SJohn Marino
294e4b17023SJohn Marino if (old_ptr_info)
295e4b17023SJohn Marino duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
296e4b17023SJohn Marino
297e4b17023SJohn Marino return new_name;
298e4b17023SJohn Marino }
299e4b17023SJohn Marino
300e4b17023SJohn Marino
301e4b17023SJohn Marino /* Release all the SSA_NAMEs created by STMT. */
302e4b17023SJohn Marino
303e4b17023SJohn Marino void
release_defs(gimple stmt)304e4b17023SJohn Marino release_defs (gimple stmt)
305e4b17023SJohn Marino {
306e4b17023SJohn Marino tree def;
307e4b17023SJohn Marino ssa_op_iter iter;
308e4b17023SJohn Marino
309e4b17023SJohn Marino /* Make sure that we are in SSA. Otherwise, operand cache may point
310e4b17023SJohn Marino to garbage. */
311e4b17023SJohn Marino gcc_assert (gimple_in_ssa_p (cfun));
312e4b17023SJohn Marino
313e4b17023SJohn Marino FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
314e4b17023SJohn Marino if (TREE_CODE (def) == SSA_NAME)
315e4b17023SJohn Marino release_ssa_name (def);
316e4b17023SJohn Marino }
317e4b17023SJohn Marino
318e4b17023SJohn Marino
319e4b17023SJohn Marino /* Replace the symbol associated with SSA_NAME with SYM. */
320e4b17023SJohn Marino
321e4b17023SJohn Marino void
replace_ssa_name_symbol(tree ssa_name,tree sym)322e4b17023SJohn Marino replace_ssa_name_symbol (tree ssa_name, tree sym)
323e4b17023SJohn Marino {
324e4b17023SJohn Marino SSA_NAME_VAR (ssa_name) = sym;
325e4b17023SJohn Marino TREE_TYPE (ssa_name) = TREE_TYPE (sym);
326e4b17023SJohn Marino }
327e4b17023SJohn Marino
328e4b17023SJohn Marino /* Return SSA names that are unused to GGC memory. This is used to keep
329e4b17023SJohn Marino footprint of compiler during interprocedural optimization.
330e4b17023SJohn Marino As a side effect the SSA_NAME_VERSION number reuse is reduced
331e4b17023SJohn Marino so this function should not be used too often. */
332e4b17023SJohn Marino static unsigned int
release_dead_ssa_names(void)333e4b17023SJohn Marino release_dead_ssa_names (void)
334e4b17023SJohn Marino {
335e4b17023SJohn Marino tree t;
336e4b17023SJohn Marino int n = VEC_length (tree, FREE_SSANAMES (cfun));
337e4b17023SJohn Marino referenced_var_iterator rvi;
338e4b17023SJohn Marino
339e4b17023SJohn Marino /* Current defs point to various dead SSA names that in turn point to
340e4b17023SJohn Marino eventually dead variables so a bunch of memory is held live. */
341e4b17023SJohn Marino FOR_EACH_REFERENCED_VAR (cfun, t, rvi)
342e4b17023SJohn Marino set_current_def (t, NULL);
343e4b17023SJohn Marino /* Now release the freelist. */
344e4b17023SJohn Marino VEC_free (tree, gc, FREE_SSANAMES (cfun));
345e4b17023SJohn Marino FREE_SSANAMES (cfun) = NULL;
346e4b17023SJohn Marino
347e4b17023SJohn Marino statistics_counter_event (cfun, "SSA names released", n);
348e4b17023SJohn Marino if (dump_file)
349e4b17023SJohn Marino fprintf (dump_file, "Released %i names, %.2f%%\n",
350e4b17023SJohn Marino n, n * 100.0 / num_ssa_names);
351e4b17023SJohn Marino return 0;
352e4b17023SJohn Marino }
353e4b17023SJohn Marino
354e4b17023SJohn Marino struct gimple_opt_pass pass_release_ssa_names =
355e4b17023SJohn Marino {
356e4b17023SJohn Marino {
357e4b17023SJohn Marino GIMPLE_PASS,
358e4b17023SJohn Marino "release_ssa", /* name */
359e4b17023SJohn Marino NULL, /* gate */
360e4b17023SJohn Marino release_dead_ssa_names, /* execute */
361e4b17023SJohn Marino NULL, /* sub */
362e4b17023SJohn Marino NULL, /* next */
363e4b17023SJohn Marino 0, /* static_pass_number */
364e4b17023SJohn Marino TV_TREE_SSA_OTHER, /* tv_id */
365e4b17023SJohn Marino PROP_ssa, /* properties_required */
366e4b17023SJohn Marino 0, /* properties_provided */
367e4b17023SJohn Marino 0, /* properties_destroyed */
368e4b17023SJohn Marino 0, /* todo_flags_start */
369e4b17023SJohn Marino 0 /* todo_flags_finish */
370e4b17023SJohn Marino }
371e4b17023SJohn Marino };
372