Fix for ICE with -g on testcase with incomplete types.
[official-gcc.git] / gcc / tree-ssanames.c
blob90d75b68714806f367e5618a470ecfcaa41e394b
1 /* Generic routines for manipulating SSA_NAME expressions
2 Copyright (C) 2003-2015 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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "gimple-iterator.h"
27 #include "hard-reg-set.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "internal-fn.h"
33 #include "tree-into-ssa.h"
34 #include "tree-ssa.h"
35 #include "tree-pass.h"
37 /* Rewriting a function into SSA form can create a huge number of SSA_NAMEs,
38 many of which may be thrown away shortly after their creation if jumps
39 were threaded through PHI nodes.
41 While our garbage collection mechanisms will handle this situation, it
42 is extremely wasteful to create nodes and throw them away, especially
43 when the nodes can be reused.
45 For PR 8361, we can significantly reduce the number of nodes allocated
46 and thus the total amount of memory allocated by managing SSA_NAMEs a
47 little. This additionally helps reduce the amount of work done by the
48 garbage collector. Similar results have been seen on a wider variety
49 of tests (such as the compiler itself).
51 Right now we maintain our free list on a per-function basis. It may
52 or may not make sense to maintain the free list for the duration of
53 a compilation unit.
55 External code should rely solely upon HIGHEST_SSA_VERSION and the
56 externally defined functions. External code should not know about
57 the details of the free list management.
59 External code should also not assume the version number on nodes is
60 monotonically increasing. We reuse the version number when we
61 reuse an SSA_NAME expression. This helps keep arrays and bitmaps
62 more compact. */
65 /* Version numbers with special meanings. We start allocating new version
66 numbers after the special ones. */
67 #define UNUSED_NAME_VERSION 0
69 unsigned int ssa_name_nodes_reused;
70 unsigned int ssa_name_nodes_created;
72 #define FREE_SSANAMES(fun) (fun)->gimple_df->free_ssanames
73 #define FREE_SSANAMES_QUEUE(fun) (fun)->gimple_df->free_ssanames_queue
76 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
77 zero use default. */
79 void
80 init_ssanames (struct function *fn, int size)
82 if (size < 50)
83 size = 50;
85 vec_alloc (SSANAMES (fn), size);
87 /* Version 0 is special, so reserve the first slot in the table. Though
88 currently unused, we may use version 0 in alias analysis as part of
89 the heuristics used to group aliases when the alias sets are too
90 large.
92 We use vec::quick_push here because we know that SSA_NAMES has at
93 least 50 elements reserved in it. */
94 SSANAMES (fn)->quick_push (NULL_TREE);
95 FREE_SSANAMES (fn) = NULL;
96 FREE_SSANAMES_QUEUE (fn) = NULL;
98 fn->gimple_df->ssa_renaming_needed = 0;
99 fn->gimple_df->rename_vops = 0;
102 /* Finalize management of SSA_NAMEs. */
104 void
105 fini_ssanames (struct function *fn)
107 vec_free (SSANAMES (fn));
108 vec_free (FREE_SSANAMES (fn));
109 vec_free (FREE_SSANAMES_QUEUE (fn));
112 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
114 void
115 ssanames_print_statistics (void)
117 fprintf (stderr, "SSA_NAME nodes allocated: %u\n", ssa_name_nodes_created);
118 fprintf (stderr, "SSA_NAME nodes reused: %u\n", ssa_name_nodes_reused);
121 /* Move all SSA_NAMEs from FREE_SSA_NAMES_QUEUE to FREE_SSA_NAMES.
123 We do not, but should have a mode to verify the state of the SSA_NAMEs
124 lists. In particular at this point every name must be in the IL,
125 on the free list or in the queue. Anything else is an error. */
127 void
128 flush_ssaname_freelist (void)
130 vec_safe_splice (FREE_SSANAMES (cfun), FREE_SSANAMES_QUEUE (cfun));
131 vec_safe_truncate (FREE_SSANAMES_QUEUE (cfun), 0);
134 /* Return an SSA_NAME node for variable VAR defined in statement STMT
135 in function FN. STMT may be an empty statement for artificial
136 references (e.g., default definitions created when a variable is
137 used without a preceding definition). */
139 tree
140 make_ssa_name_fn (struct function *fn, tree var, gimple *stmt)
142 tree t;
143 use_operand_p imm;
145 gcc_assert (TREE_CODE (var) == VAR_DECL
146 || TREE_CODE (var) == PARM_DECL
147 || TREE_CODE (var) == RESULT_DECL
148 || (TYPE_P (var) && is_gimple_reg_type (var)));
150 /* If our free list has an element, then use it. */
151 if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
153 t = FREE_SSANAMES (fn)->pop ();
154 ssa_name_nodes_reused++;
156 /* The node was cleared out when we put it on the free list, so
157 there is no need to do so again here. */
158 gcc_assert ((*SSANAMES (fn))[SSA_NAME_VERSION (t)] == NULL);
159 (*SSANAMES (fn))[SSA_NAME_VERSION (t)] = t;
161 else
163 t = make_node (SSA_NAME);
164 SSA_NAME_VERSION (t) = SSANAMES (fn)->length ();
165 vec_safe_push (SSANAMES (fn), t);
166 ssa_name_nodes_created++;
169 if (TYPE_P (var))
171 TREE_TYPE (t) = var;
172 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
174 else
176 TREE_TYPE (t) = TREE_TYPE (var);
177 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
179 SSA_NAME_DEF_STMT (t) = stmt;
180 if (POINTER_TYPE_P (TREE_TYPE (t)))
181 SSA_NAME_PTR_INFO (t) = NULL;
182 else
183 SSA_NAME_RANGE_INFO (t) = NULL;
185 SSA_NAME_IN_FREE_LIST (t) = 0;
186 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
187 imm = &(SSA_NAME_IMM_USE_NODE (t));
188 imm->use = NULL;
189 imm->prev = imm;
190 imm->next = imm;
191 imm->loc.ssa_name = t;
193 return t;
196 /* Store range information RANGE_TYPE, MIN, and MAX to tree ssa_name NAME. */
198 void
199 set_range_info (tree name, enum value_range_type range_type,
200 const wide_int_ref &min, const wide_int_ref &max)
202 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
203 gcc_assert (range_type == VR_RANGE || range_type == VR_ANTI_RANGE);
204 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
205 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
207 /* Allocate if not available. */
208 if (ri == NULL)
210 size_t size = (sizeof (range_info_def)
211 + trailing_wide_ints <3>::extra_size (precision));
212 ri = static_cast<range_info_def *> (ggc_internal_alloc (size));
213 ri->ints.set_precision (precision);
214 SSA_NAME_RANGE_INFO (name) = ri;
215 ri->set_nonzero_bits (wi::shwi (-1, precision));
218 /* Record the range type. */
219 if (SSA_NAME_RANGE_TYPE (name) != range_type)
220 SSA_NAME_ANTI_RANGE_P (name) = (range_type == VR_ANTI_RANGE);
222 /* Set the values. */
223 ri->set_min (min);
224 ri->set_max (max);
226 /* If it is a range, try to improve nonzero_bits from the min/max. */
227 if (range_type == VR_RANGE)
229 wide_int xorv = ri->get_min () ^ ri->get_max ();
230 if (xorv != 0)
231 xorv = wi::mask (precision - wi::clz (xorv), false, precision);
232 ri->set_nonzero_bits (ri->get_nonzero_bits () & (ri->get_min () | xorv));
237 /* Gets range information MIN, MAX and returns enum value_range_type
238 corresponding to tree ssa_name NAME. enum value_range_type returned
239 is used to determine if MIN and MAX are valid values. */
241 enum value_range_type
242 get_range_info (const_tree name, wide_int *min, wide_int *max)
244 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
245 gcc_assert (min && max);
246 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
248 /* Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
249 with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision. */
250 if (!ri || (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (name)))
251 > 2 * HOST_BITS_PER_WIDE_INT))
252 return VR_VARYING;
254 *min = ri->get_min ();
255 *max = ri->get_max ();
256 return SSA_NAME_RANGE_TYPE (name);
259 /* Change non-zero bits bitmask of NAME. */
261 void
262 set_nonzero_bits (tree name, const wide_int_ref &mask)
264 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
265 if (SSA_NAME_RANGE_INFO (name) == NULL)
266 set_range_info (name, VR_RANGE,
267 TYPE_MIN_VALUE (TREE_TYPE (name)),
268 TYPE_MAX_VALUE (TREE_TYPE (name)));
269 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
270 ri->set_nonzero_bits (mask);
273 /* Return a widest_int with potentially non-zero bits in SSA_NAME
274 NAME, or -1 if unknown. */
276 wide_int
277 get_nonzero_bits (const_tree name)
279 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
280 if (POINTER_TYPE_P (TREE_TYPE (name)))
282 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
283 if (pi && pi->align)
284 return wi::shwi (-(HOST_WIDE_INT) pi->align
285 | (HOST_WIDE_INT) pi->misalign, precision);
286 return wi::shwi (-1, precision);
289 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
290 if (!ri)
291 return wi::shwi (-1, precision);
293 return ri->get_nonzero_bits ();
296 /* We no longer need the SSA_NAME expression VAR, release it so that
297 it may be reused.
299 Note it is assumed that no calls to make_ssa_name will be made
300 until all uses of the ssa name are released and that the only
301 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
302 other fields must be assumed clobbered. */
304 void
305 release_ssa_name_fn (struct function *fn, tree var)
307 if (!var)
308 return;
310 /* Never release the default definition for a symbol. It's a
311 special SSA name that should always exist once it's created. */
312 if (SSA_NAME_IS_DEFAULT_DEF (var))
313 return;
315 /* If VAR has been registered for SSA updating, don't remove it.
316 After update_ssa has run, the name will be released. */
317 if (name_registered_for_update_p (var))
319 release_ssa_name_after_update_ssa (var);
320 return;
323 /* release_ssa_name can be called multiple times on a single SSA_NAME.
324 However, it should only end up on our free list one time. We
325 keep a status bit in the SSA_NAME node itself to indicate it has
326 been put on the free list.
328 Note that once on the freelist you can not reference the SSA_NAME's
329 defining statement. */
330 if (! SSA_NAME_IN_FREE_LIST (var))
332 tree saved_ssa_name_var = SSA_NAME_VAR (var);
333 int saved_ssa_name_version = SSA_NAME_VERSION (var);
334 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
336 if (MAY_HAVE_DEBUG_STMTS)
337 insert_debug_temp_for_var_def (NULL, var);
339 if (flag_checking)
340 verify_imm_links (stderr, var);
341 while (imm->next != imm)
342 delink_imm_use (imm->next);
344 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
345 memset (var, 0, tree_size (var));
347 imm->prev = imm;
348 imm->next = imm;
349 imm->loc.ssa_name = var;
351 /* First put back the right tree node so that the tree checking
352 macros do not complain. */
353 TREE_SET_CODE (var, SSA_NAME);
355 /* Restore the version number. */
356 SSA_NAME_VERSION (var) = saved_ssa_name_version;
358 /* Hopefully this can go away once we have the new incremental
359 SSA updating code installed. */
360 SET_SSA_NAME_VAR_OR_IDENTIFIER (var, saved_ssa_name_var);
362 /* Note this SSA_NAME is now in the first list. */
363 SSA_NAME_IN_FREE_LIST (var) = 1;
365 /* And finally queue it so that it will be put on the free list. */
366 vec_safe_push (FREE_SSANAMES_QUEUE (fn), var);
370 /* If the alignment of the pointer described by PI is known, return true and
371 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
372 respectively. Otherwise return false. */
374 bool
375 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
376 unsigned int *misalignp)
378 if (pi->align)
380 *alignp = pi->align;
381 *misalignp = pi->misalign;
382 return true;
384 else
385 return false;
388 /* State that the pointer described by PI has unknown alignment. */
390 void
391 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
393 pi->align = 0;
394 pi->misalign = 0;
397 /* Store the power-of-two byte alignment and the deviation from that
398 alignment of pointer described by PI to ALIOGN and MISALIGN
399 respectively. */
401 void
402 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
403 unsigned int misalign)
405 gcc_checking_assert (align != 0);
406 gcc_assert ((align & (align - 1)) == 0);
407 gcc_assert ((misalign & ~(align - 1)) == 0);
409 pi->align = align;
410 pi->misalign = misalign;
413 /* If pointer described by PI has known alignment, increase its known
414 misalignment by INCREMENT modulo its current alignment. */
416 void
417 adjust_ptr_info_misalignment (struct ptr_info_def *pi,
418 unsigned int increment)
420 if (pi->align != 0)
422 pi->misalign += increment;
423 pi->misalign &= (pi->align - 1);
427 /* Return the alias information associated with pointer T. It creates a
428 new instance if none existed. */
430 struct ptr_info_def *
431 get_ptr_info (tree t)
433 struct ptr_info_def *pi;
435 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
437 pi = SSA_NAME_PTR_INFO (t);
438 if (pi == NULL)
440 pi = ggc_cleared_alloc<ptr_info_def> ();
441 pt_solution_reset (&pi->pt);
442 mark_ptr_info_alignment_unknown (pi);
443 SSA_NAME_PTR_INFO (t) = pi;
446 return pi;
450 /* Creates a new SSA name using the template NAME tobe defined by
451 statement STMT in function FN. */
453 tree
454 copy_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
456 tree new_name;
458 if (SSA_NAME_VAR (name))
459 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
460 else
462 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
463 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
466 return new_name;
470 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
471 the SSA name NAME. */
473 void
474 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
476 struct ptr_info_def *new_ptr_info;
478 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
479 gcc_assert (!SSA_NAME_PTR_INFO (name));
481 if (!ptr_info)
482 return;
484 new_ptr_info = ggc_alloc<ptr_info_def> ();
485 *new_ptr_info = *ptr_info;
487 SSA_NAME_PTR_INFO (name) = new_ptr_info;
490 /* Creates a duplicate of the range_info_def at RANGE_INFO of type
491 RANGE_TYPE for use by the SSA name NAME. */
492 void
493 duplicate_ssa_name_range_info (tree name, enum value_range_type range_type,
494 struct range_info_def *range_info)
496 struct range_info_def *new_range_info;
498 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
499 gcc_assert (!SSA_NAME_RANGE_INFO (name));
501 if (!range_info)
502 return;
504 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
505 size_t size = (sizeof (range_info_def)
506 + trailing_wide_ints <3>::extra_size (precision));
507 new_range_info = static_cast<range_info_def *> (ggc_internal_alloc (size));
508 memcpy (new_range_info, range_info, size);
510 gcc_assert (range_type == VR_RANGE || range_type == VR_ANTI_RANGE);
511 SSA_NAME_ANTI_RANGE_P (name) = (range_type == VR_ANTI_RANGE);
512 SSA_NAME_RANGE_INFO (name) = new_range_info;
517 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
518 in function FN. */
520 tree
521 duplicate_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
523 tree new_name = copy_ssa_name_fn (fn, name, stmt);
524 if (POINTER_TYPE_P (TREE_TYPE (name)))
526 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
528 if (old_ptr_info)
529 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
531 else
533 struct range_info_def *old_range_info = SSA_NAME_RANGE_INFO (name);
535 if (old_range_info)
536 duplicate_ssa_name_range_info (new_name, SSA_NAME_RANGE_TYPE (name),
537 old_range_info);
540 return new_name;
544 /* Reset all flow sensitive data on NAME such as range-info, nonzero
545 bits and alignment. */
547 void
548 reset_flow_sensitive_info (tree name)
550 if (POINTER_TYPE_P (TREE_TYPE (name)))
552 /* points-to info is not flow-sensitive. */
553 if (SSA_NAME_PTR_INFO (name))
554 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
556 else
557 SSA_NAME_RANGE_INFO (name) = NULL;
560 /* Clear all flow sensitive data from all statements and PHI definitions
561 in BB. */
563 void
564 reset_flow_sensitive_info_in_bb (basic_block bb)
566 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
567 gsi_next (&gsi))
569 gimple *stmt = gsi_stmt (gsi);
570 ssa_op_iter i;
571 tree op;
572 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
573 reset_flow_sensitive_info (op);
576 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
577 gsi_next (&gsi))
579 tree phi_def = gimple_phi_result (gsi.phi ());
580 reset_flow_sensitive_info (phi_def);
584 /* Release all the SSA_NAMEs created by STMT. */
586 void
587 release_defs (gimple *stmt)
589 tree def;
590 ssa_op_iter iter;
592 /* Make sure that we are in SSA. Otherwise, operand cache may point
593 to garbage. */
594 gcc_assert (gimple_in_ssa_p (cfun));
596 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
597 if (TREE_CODE (def) == SSA_NAME)
598 release_ssa_name (def);
602 /* Replace the symbol associated with SSA_NAME with SYM. */
604 void
605 replace_ssa_name_symbol (tree ssa_name, tree sym)
607 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
608 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
611 /* Return SSA names that are unused to GGC memory and compact the SSA
612 version namespace. This is used to keep footprint of compiler during
613 interprocedural optimization. */
615 namespace {
617 const pass_data pass_data_release_ssa_names =
619 GIMPLE_PASS, /* type */
620 "release_ssa", /* name */
621 OPTGROUP_NONE, /* optinfo_flags */
622 TV_TREE_SSA_OTHER, /* tv_id */
623 PROP_ssa, /* properties_required */
624 0, /* properties_provided */
625 0, /* properties_destroyed */
626 TODO_remove_unused_locals, /* todo_flags_start */
627 0, /* todo_flags_finish */
630 class pass_release_ssa_names : public gimple_opt_pass
632 public:
633 pass_release_ssa_names (gcc::context *ctxt)
634 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
637 /* opt_pass methods: */
638 virtual unsigned int execute (function *);
640 }; // class pass_release_ssa_names
642 unsigned int
643 pass_release_ssa_names::execute (function *fun)
645 unsigned i, j;
646 int n = vec_safe_length (FREE_SSANAMES (fun));
648 /* Now release the freelist. */
649 vec_free (FREE_SSANAMES (fun));
651 /* And compact the SSA number space. We make sure to not change the
652 relative order of SSA versions. */
653 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
655 tree name = ssa_name (i);
656 if (name)
658 if (i != j)
660 SSA_NAME_VERSION (name) = j;
661 (*fun->gimple_df->ssa_names)[j] = name;
663 j++;
666 fun->gimple_df->ssa_names->truncate (j);
668 statistics_counter_event (fun, "SSA names released", n);
669 statistics_counter_event (fun, "SSA name holes removed", i - j);
670 if (dump_file)
671 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
672 n, n * 100.0 / num_ssa_names, i - j);
673 return 0;
676 } // anon namespace
678 gimple_opt_pass *
679 make_pass_release_ssa_names (gcc::context *ctxt)
681 return new pass_release_ssa_names (ctxt);