1 /* Generic routines for manipulating SSA_NAME expressions
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "stor-layout.h"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
32 #include "gimple-ssa.h"
33 #include "tree-phinodes.h"
34 #include "ssa-iterators.h"
35 #include "stringpool.h"
36 #include "tree-ssanames.h"
37 #include "tree-into-ssa.h"
39 #include "tree-pass.h"
41 /* Rewriting a function into SSA form can create a huge number of SSA_NAMEs,
42 many of which may be thrown away shortly after their creation if jumps
43 were threaded through PHI nodes.
45 While our garbage collection mechanisms will handle this situation, it
46 is extremely wasteful to create nodes and throw them away, especially
47 when the nodes can be reused.
49 For PR 8361, we can significantly reduce the number of nodes allocated
50 and thus the total amount of memory allocated by managing SSA_NAMEs a
51 little. This additionally helps reduce the amount of work done by the
52 garbage collector. Similar results have been seen on a wider variety
53 of tests (such as the compiler itself).
55 Right now we maintain our free list on a per-function basis. It may
56 or may not make sense to maintain the free list for the duration of
59 External code should rely solely upon HIGHEST_SSA_VERSION and the
60 externally defined functions. External code should not know about
61 the details of the free list management.
63 External code should also not assume the version number on nodes is
64 monotonically increasing. We reuse the version number when we
65 reuse an SSA_NAME expression. This helps keep arrays and bitmaps
69 /* Version numbers with special meanings. We start allocating new version
70 numbers after the special ones. */
71 #define UNUSED_NAME_VERSION 0
73 unsigned int ssa_name_nodes_reused
;
74 unsigned int ssa_name_nodes_created
;
76 #define FREE_SSANAMES(fun) (fun)->gimple_df->free_ssanames
79 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
83 init_ssanames (struct function
*fn
, int size
)
88 vec_alloc (SSANAMES (fn
), size
);
90 /* Version 0 is special, so reserve the first slot in the table. Though
91 currently unused, we may use version 0 in alias analysis as part of
92 the heuristics used to group aliases when the alias sets are too
95 We use vec::quick_push here because we know that SSA_NAMES has at
96 least 50 elements reserved in it. */
97 SSANAMES (fn
)->quick_push (NULL_TREE
);
98 FREE_SSANAMES (fn
) = NULL
;
100 fn
->gimple_df
->ssa_renaming_needed
= 0;
101 fn
->gimple_df
->rename_vops
= 0;
104 /* Finalize management of SSA_NAMEs. */
109 vec_free (SSANAMES (cfun
));
110 vec_free (FREE_SSANAMES (cfun
));
113 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
116 ssanames_print_statistics (void)
118 fprintf (stderr
, "SSA_NAME nodes allocated: %u\n", ssa_name_nodes_created
);
119 fprintf (stderr
, "SSA_NAME nodes reused: %u\n", ssa_name_nodes_reused
);
122 /* Return an SSA_NAME node for variable VAR defined in statement STMT
123 in function FN. STMT may be an empty statement for artificial
124 references (e.g., default definitions created when a variable is
125 used without a preceding definition). */
128 make_ssa_name_fn (struct function
*fn
, tree var
, gimple stmt
)
133 gcc_assert (TREE_CODE (var
) == VAR_DECL
134 || TREE_CODE (var
) == PARM_DECL
135 || TREE_CODE (var
) == RESULT_DECL
136 || (TYPE_P (var
) && is_gimple_reg_type (var
)));
138 /* If our free list has an element, then use it. */
139 if (!vec_safe_is_empty (FREE_SSANAMES (fn
)))
141 t
= FREE_SSANAMES (fn
)->pop ();
142 if (GATHER_STATISTICS
)
143 ssa_name_nodes_reused
++;
145 /* The node was cleared out when we put it on the free list, so
146 there is no need to do so again here. */
147 gcc_assert ((*SSANAMES (fn
))[SSA_NAME_VERSION (t
)] == NULL
);
148 (*SSANAMES (fn
))[SSA_NAME_VERSION (t
)] = t
;
152 t
= make_node (SSA_NAME
);
153 SSA_NAME_VERSION (t
) = SSANAMES (fn
)->length ();
154 vec_safe_push (SSANAMES (fn
), t
);
155 if (GATHER_STATISTICS
)
156 ssa_name_nodes_created
++;
162 SET_SSA_NAME_VAR_OR_IDENTIFIER (t
, NULL_TREE
);
166 TREE_TYPE (t
) = TREE_TYPE (var
);
167 SET_SSA_NAME_VAR_OR_IDENTIFIER (t
, var
);
169 SSA_NAME_DEF_STMT (t
) = stmt
;
170 if (POINTER_TYPE_P (TREE_TYPE (t
)))
171 SSA_NAME_PTR_INFO (t
) = NULL
;
173 SSA_NAME_RANGE_INFO (t
) = NULL
;
175 SSA_NAME_IN_FREE_LIST (t
) = 0;
176 SSA_NAME_IS_DEFAULT_DEF (t
) = 0;
177 imm
= &(SSA_NAME_IMM_USE_NODE (t
));
181 imm
->loc
.ssa_name
= t
;
186 /* Store range information RANGE_TYPE, MIN, and MAX to tree ssa_name NAME. */
189 set_range_info (tree name
, enum value_range_type range_type
,
190 const wide_int_ref
&min
, const wide_int_ref
&max
)
192 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name
)));
193 gcc_assert (range_type
== VR_RANGE
|| range_type
== VR_ANTI_RANGE
);
194 range_info_def
*ri
= SSA_NAME_RANGE_INFO (name
);
195 unsigned int precision
= TYPE_PRECISION (TREE_TYPE (name
));
197 /* Allocate if not available. */
200 size_t size
= (sizeof (range_info_def
)
201 + trailing_wide_ints
<3>::extra_size (precision
));
202 ri
= ggc_alloc_range_info_def (size
);
203 ri
->ints
.set_precision (precision
);
204 SSA_NAME_RANGE_INFO (name
) = ri
;
205 ri
->set_nonzero_bits (wi::shwi (-1, precision
));
208 /* Record the range type. */
209 if (SSA_NAME_RANGE_TYPE (name
) != range_type
)
210 SSA_NAME_ANTI_RANGE_P (name
) = (range_type
== VR_ANTI_RANGE
);
212 /* Set the values. */
216 /* If it is a range, try to improve nonzero_bits from the min/max. */
217 if (range_type
== VR_RANGE
)
219 wide_int xorv
= ri
->get_min () ^ ri
->get_max ();
221 xorv
= wi::mask (precision
- wi::clz (xorv
), false, precision
);
222 ri
->set_nonzero_bits (ri
->get_nonzero_bits () & (ri
->get_min () | xorv
));
227 /* Gets range information MIN, MAX and returns enum value_range_type
228 corresponding to tree ssa_name NAME. enum value_range_type returned
229 is used to determine if MIN and MAX are valid values. */
231 enum value_range_type
232 get_range_info (const_tree name
, wide_int
*min
, wide_int
*max
)
234 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name
)));
235 gcc_assert (min
&& max
);
236 range_info_def
*ri
= SSA_NAME_RANGE_INFO (name
);
238 /* Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
239 with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision. */
240 if (!ri
|| (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (name
)))
241 > 2 * HOST_BITS_PER_WIDE_INT
))
244 *min
= ri
->get_min ();
245 *max
= ri
->get_max ();
246 return SSA_NAME_RANGE_TYPE (name
);
249 /* Change non-zero bits bitmask of NAME. */
252 set_nonzero_bits (tree name
, const wide_int_ref
&mask
)
254 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name
)));
255 if (SSA_NAME_RANGE_INFO (name
) == NULL
)
256 set_range_info (name
, VR_RANGE
,
257 TYPE_MIN_VALUE (TREE_TYPE (name
)),
258 TYPE_MAX_VALUE (TREE_TYPE (name
)));
259 range_info_def
*ri
= SSA_NAME_RANGE_INFO (name
);
260 ri
->set_nonzero_bits (mask
);
263 /* Return a widest_int with potentially non-zero bits in SSA_NAME
264 NAME, or -1 if unknown. */
267 get_nonzero_bits (const_tree name
)
269 unsigned int precision
= TYPE_PRECISION (TREE_TYPE (name
));
270 if (POINTER_TYPE_P (TREE_TYPE (name
)))
272 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (name
);
274 return wi::shwi (-(int) pi
->align
| pi
->misalign
, precision
);
275 return wi::shwi (-1, precision
);
278 range_info_def
*ri
= SSA_NAME_RANGE_INFO (name
);
280 return wi::shwi (-1, precision
);
282 return ri
->get_nonzero_bits ();
285 /* We no longer need the SSA_NAME expression VAR, release it so that
288 Note it is assumed that no calls to make_ssa_name will be made
289 until all uses of the ssa name are released and that the only
290 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
291 other fields must be assumed clobbered. */
294 release_ssa_name_fn (struct function
*fn
, tree var
)
299 /* Never release the default definition for a symbol. It's a
300 special SSA name that should always exist once it's created. */
301 if (SSA_NAME_IS_DEFAULT_DEF (var
))
304 /* If VAR has been registered for SSA updating, don't remove it.
305 After update_ssa has run, the name will be released. */
306 if (name_registered_for_update_p (var
))
308 release_ssa_name_after_update_ssa (var
);
312 /* release_ssa_name can be called multiple times on a single SSA_NAME.
313 However, it should only end up on our free list one time. We
314 keep a status bit in the SSA_NAME node itself to indicate it has
315 been put on the free list.
317 Note that once on the freelist you can not reference the SSA_NAME's
318 defining statement. */
319 if (! SSA_NAME_IN_FREE_LIST (var
))
321 tree saved_ssa_name_var
= SSA_NAME_VAR (var
);
322 int saved_ssa_name_version
= SSA_NAME_VERSION (var
);
323 use_operand_p imm
= &(SSA_NAME_IMM_USE_NODE (var
));
325 if (MAY_HAVE_DEBUG_STMTS
)
326 insert_debug_temp_for_var_def (NULL
, var
);
328 #ifdef ENABLE_CHECKING
329 verify_imm_links (stderr
, var
);
331 while (imm
->next
!= imm
)
332 delink_imm_use (imm
->next
);
334 (*SSANAMES (fn
))[SSA_NAME_VERSION (var
)] = NULL_TREE
;
335 memset (var
, 0, tree_size (var
));
339 imm
->loc
.ssa_name
= var
;
341 /* First put back the right tree node so that the tree checking
342 macros do not complain. */
343 TREE_SET_CODE (var
, SSA_NAME
);
345 /* Restore the version number. */
346 SSA_NAME_VERSION (var
) = saved_ssa_name_version
;
348 /* Hopefully this can go away once we have the new incremental
349 SSA updating code installed. */
350 SET_SSA_NAME_VAR_OR_IDENTIFIER (var
, saved_ssa_name_var
);
352 /* Note this SSA_NAME is now in the first list. */
353 SSA_NAME_IN_FREE_LIST (var
) = 1;
355 /* And finally put it on the free list. */
356 vec_safe_push (FREE_SSANAMES (fn
), var
);
360 /* If the alignment of the pointer described by PI is known, return true and
361 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
362 respectively. Otherwise return false. */
365 get_ptr_info_alignment (struct ptr_info_def
*pi
, unsigned int *alignp
,
366 unsigned int *misalignp
)
371 *misalignp
= pi
->misalign
;
378 /* State that the pointer described by PI has unknown alignment. */
381 mark_ptr_info_alignment_unknown (struct ptr_info_def
*pi
)
387 /* Store the the power-of-two byte alignment and the deviation from that
388 alignment of pointer described by PI to ALIOGN and MISALIGN
392 set_ptr_info_alignment (struct ptr_info_def
*pi
, unsigned int align
,
393 unsigned int misalign
)
395 gcc_checking_assert (align
!= 0);
396 gcc_assert ((align
& (align
- 1)) == 0);
397 gcc_assert ((misalign
& ~(align
- 1)) == 0);
400 pi
->misalign
= misalign
;
403 /* If pointer described by PI has known alignment, increase its known
404 misalignment by INCREMENT modulo its current alignment. */
407 adjust_ptr_info_misalignment (struct ptr_info_def
*pi
,
408 unsigned int increment
)
412 pi
->misalign
+= increment
;
413 pi
->misalign
&= (pi
->align
- 1);
417 /* Return the alias information associated with pointer T. It creates a
418 new instance if none existed. */
420 struct ptr_info_def
*
421 get_ptr_info (tree t
)
423 struct ptr_info_def
*pi
;
425 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t
)));
427 pi
= SSA_NAME_PTR_INFO (t
);
430 pi
= ggc_alloc_cleared_ptr_info_def ();
431 pt_solution_reset (&pi
->pt
);
432 mark_ptr_info_alignment_unknown (pi
);
433 SSA_NAME_PTR_INFO (t
) = pi
;
440 /* Creates a new SSA name using the template NAME tobe defined by
441 statement STMT in function FN. */
444 copy_ssa_name_fn (struct function
*fn
, tree name
, gimple stmt
)
448 if (SSA_NAME_VAR (name
))
449 new_name
= make_ssa_name_fn (fn
, SSA_NAME_VAR (name
), stmt
);
452 new_name
= make_ssa_name_fn (fn
, TREE_TYPE (name
), stmt
);
453 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name
, SSA_NAME_IDENTIFIER (name
));
460 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
461 the SSA name NAME. */
464 duplicate_ssa_name_ptr_info (tree name
, struct ptr_info_def
*ptr_info
)
466 struct ptr_info_def
*new_ptr_info
;
468 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name
)));
469 gcc_assert (!SSA_NAME_PTR_INFO (name
));
474 new_ptr_info
= ggc_alloc_ptr_info_def ();
475 *new_ptr_info
= *ptr_info
;
477 SSA_NAME_PTR_INFO (name
) = new_ptr_info
;
480 /* Creates a duplicate of the range_info_def at RANGE_INFO of type
481 RANGE_TYPE for use by the SSA name NAME. */
483 duplicate_ssa_name_range_info (tree name
, enum value_range_type range_type
,
484 struct range_info_def
*range_info
)
486 struct range_info_def
*new_range_info
;
488 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name
)));
489 gcc_assert (!SSA_NAME_RANGE_INFO (name
));
490 gcc_assert (!SSA_NAME_ANTI_RANGE_P (name
));
495 unsigned int precision
= TYPE_PRECISION (TREE_TYPE (name
));
496 size_t size
= (sizeof (range_info_def
)
497 + trailing_wide_ints
<3>::extra_size (precision
));
498 new_range_info
= ggc_alloc_range_info_def (size
);
499 memcpy (new_range_info
, range_info
, size
);
501 gcc_assert (range_type
== VR_RANGE
|| range_type
== VR_ANTI_RANGE
);
502 SSA_NAME_ANTI_RANGE_P (name
) = (range_type
== VR_ANTI_RANGE
);
503 SSA_NAME_RANGE_INFO (name
) = new_range_info
;
508 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
512 duplicate_ssa_name_fn (struct function
*fn
, tree name
, gimple stmt
)
514 tree new_name
= copy_ssa_name_fn (fn
, name
, stmt
);
515 if (POINTER_TYPE_P (TREE_TYPE (name
)))
517 struct ptr_info_def
*old_ptr_info
= SSA_NAME_PTR_INFO (name
);
520 duplicate_ssa_name_ptr_info (new_name
, old_ptr_info
);
524 struct range_info_def
*old_range_info
= SSA_NAME_RANGE_INFO (name
);
527 duplicate_ssa_name_range_info (new_name
, SSA_NAME_RANGE_TYPE (name
),
535 /* Release all the SSA_NAMEs created by STMT. */
538 release_defs (gimple stmt
)
543 /* Make sure that we are in SSA. Otherwise, operand cache may point
545 gcc_assert (gimple_in_ssa_p (cfun
));
547 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
548 if (TREE_CODE (def
) == SSA_NAME
)
549 release_ssa_name (def
);
553 /* Replace the symbol associated with SSA_NAME with SYM. */
556 replace_ssa_name_symbol (tree ssa_name
, tree sym
)
558 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name
, sym
);
559 TREE_TYPE (ssa_name
) = TREE_TYPE (sym
);
562 /* Return SSA names that are unused to GGC memory and compact the SSA
563 version namespace. This is used to keep footprint of compiler during
564 interprocedural optimization. */
568 const pass_data pass_data_release_ssa_names
=
570 GIMPLE_PASS
, /* type */
571 "release_ssa", /* name */
572 OPTGROUP_NONE
, /* optinfo_flags */
573 true, /* has_execute */
574 TV_TREE_SSA_OTHER
, /* tv_id */
575 PROP_ssa
, /* properties_required */
576 0, /* properties_provided */
577 0, /* properties_destroyed */
578 TODO_remove_unused_locals
, /* todo_flags_start */
579 0, /* todo_flags_finish */
582 class pass_release_ssa_names
: public gimple_opt_pass
585 pass_release_ssa_names (gcc::context
*ctxt
)
586 : gimple_opt_pass (pass_data_release_ssa_names
, ctxt
)
589 /* opt_pass methods: */
590 virtual unsigned int execute (function
*);
592 }; // class pass_release_ssa_names
595 pass_release_ssa_names::execute (function
*fun
)
598 int n
= vec_safe_length (FREE_SSANAMES (fun
));
600 /* Now release the freelist. */
601 vec_free (FREE_SSANAMES (fun
));
603 /* And compact the SSA number space. We make sure to not change the
604 relative order of SSA versions. */
605 for (i
= 1, j
= 1; i
< fun
->gimple_df
->ssa_names
->length (); ++i
)
607 tree name
= ssa_name (i
);
612 SSA_NAME_VERSION (name
) = j
;
613 (*fun
->gimple_df
->ssa_names
)[j
] = name
;
618 fun
->gimple_df
->ssa_names
->truncate (j
);
620 statistics_counter_event (fun
, "SSA names released", n
);
621 statistics_counter_event (fun
, "SSA name holes removed", i
- j
);
623 fprintf (dump_file
, "Released %i names, %.2f%%, removed %i holes\n",
624 n
, n
* 100.0 / num_ssa_names
, i
- j
);
631 make_pass_release_ssa_names (gcc::context
*ctxt
)
633 return new pass_release_ssa_names (ctxt
);