change in_expr_list_p to in_insn_list_p
[official-gcc.git] / gcc / tree-ssanames.c
blob9c39f65e6b6ba4a2fe5ce834e7860d685ae7c51b
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 "tm.h"
24 #include "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "wide-int.h"
32 #include "inchash.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "predict.h"
37 #include "hard-reg-set.h"
38 #include "input.h"
39 #include "function.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimple-ssa.h"
47 #include "tree-phinodes.h"
48 #include "ssa-iterators.h"
49 #include "stringpool.h"
50 #include "tree-ssanames.h"
51 #include "tree-into-ssa.h"
52 #include "tree-ssa.h"
53 #include "tree-pass.h"
55 /* Rewriting a function into SSA form can create a huge number of SSA_NAMEs,
56 many of which may be thrown away shortly after their creation if jumps
57 were threaded through PHI nodes.
59 While our garbage collection mechanisms will handle this situation, it
60 is extremely wasteful to create nodes and throw them away, especially
61 when the nodes can be reused.
63 For PR 8361, we can significantly reduce the number of nodes allocated
64 and thus the total amount of memory allocated by managing SSA_NAMEs a
65 little. This additionally helps reduce the amount of work done by the
66 garbage collector. Similar results have been seen on a wider variety
67 of tests (such as the compiler itself).
69 Right now we maintain our free list on a per-function basis. It may
70 or may not make sense to maintain the free list for the duration of
71 a compilation unit.
73 External code should rely solely upon HIGHEST_SSA_VERSION and the
74 externally defined functions. External code should not know about
75 the details of the free list management.
77 External code should also not assume the version number on nodes is
78 monotonically increasing. We reuse the version number when we
79 reuse an SSA_NAME expression. This helps keep arrays and bitmaps
80 more compact. */
83 /* Version numbers with special meanings. We start allocating new version
84 numbers after the special ones. */
85 #define UNUSED_NAME_VERSION 0
87 unsigned int ssa_name_nodes_reused;
88 unsigned int ssa_name_nodes_created;
90 #define FREE_SSANAMES(fun) (fun)->gimple_df->free_ssanames
93 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
94 zero use default. */
96 void
97 init_ssanames (struct function *fn, int size)
99 if (size < 50)
100 size = 50;
102 vec_alloc (SSANAMES (fn), size);
104 /* Version 0 is special, so reserve the first slot in the table. Though
105 currently unused, we may use version 0 in alias analysis as part of
106 the heuristics used to group aliases when the alias sets are too
107 large.
109 We use vec::quick_push here because we know that SSA_NAMES has at
110 least 50 elements reserved in it. */
111 SSANAMES (fn)->quick_push (NULL_TREE);
112 FREE_SSANAMES (fn) = NULL;
114 fn->gimple_df->ssa_renaming_needed = 0;
115 fn->gimple_df->rename_vops = 0;
118 /* Finalize management of SSA_NAMEs. */
120 void
121 fini_ssanames (void)
123 vec_free (SSANAMES (cfun));
124 vec_free (FREE_SSANAMES (cfun));
127 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
129 void
130 ssanames_print_statistics (void)
132 fprintf (stderr, "SSA_NAME nodes allocated: %u\n", ssa_name_nodes_created);
133 fprintf (stderr, "SSA_NAME nodes reused: %u\n", ssa_name_nodes_reused);
136 /* Return an SSA_NAME node for variable VAR defined in statement STMT
137 in function FN. STMT may be an empty statement for artificial
138 references (e.g., default definitions created when a variable is
139 used without a preceding definition). */
141 tree
142 make_ssa_name_fn (struct function *fn, tree var, gimple stmt)
144 tree t;
145 use_operand_p imm;
147 gcc_assert (TREE_CODE (var) == VAR_DECL
148 || TREE_CODE (var) == PARM_DECL
149 || TREE_CODE (var) == RESULT_DECL
150 || (TYPE_P (var) && is_gimple_reg_type (var)));
152 /* If our free list has an element, then use it. */
153 if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
155 t = FREE_SSANAMES (fn)->pop ();
156 if (GATHER_STATISTICS)
157 ssa_name_nodes_reused++;
159 /* The node was cleared out when we put it on the free list, so
160 there is no need to do so again here. */
161 gcc_assert ((*SSANAMES (fn))[SSA_NAME_VERSION (t)] == NULL);
162 (*SSANAMES (fn))[SSA_NAME_VERSION (t)] = t;
164 else
166 t = make_node (SSA_NAME);
167 SSA_NAME_VERSION (t) = SSANAMES (fn)->length ();
168 vec_safe_push (SSANAMES (fn), t);
169 if (GATHER_STATISTICS)
170 ssa_name_nodes_created++;
173 if (TYPE_P (var))
175 TREE_TYPE (t) = var;
176 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
178 else
180 TREE_TYPE (t) = TREE_TYPE (var);
181 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
183 SSA_NAME_DEF_STMT (t) = stmt;
184 if (POINTER_TYPE_P (TREE_TYPE (t)))
185 SSA_NAME_PTR_INFO (t) = NULL;
186 else
187 SSA_NAME_RANGE_INFO (t) = NULL;
189 SSA_NAME_IN_FREE_LIST (t) = 0;
190 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
191 imm = &(SSA_NAME_IMM_USE_NODE (t));
192 imm->use = NULL;
193 imm->prev = imm;
194 imm->next = imm;
195 imm->loc.ssa_name = t;
197 return t;
200 /* Store range information RANGE_TYPE, MIN, and MAX to tree ssa_name NAME. */
202 void
203 set_range_info (tree name, enum value_range_type range_type,
204 const wide_int_ref &min, const wide_int_ref &max)
206 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
207 gcc_assert (range_type == VR_RANGE || range_type == VR_ANTI_RANGE);
208 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
209 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
211 /* Allocate if not available. */
212 if (ri == NULL)
214 size_t size = (sizeof (range_info_def)
215 + trailing_wide_ints <3>::extra_size (precision));
216 ri = static_cast<range_info_def *> (ggc_internal_alloc (size));
217 ri->ints.set_precision (precision);
218 SSA_NAME_RANGE_INFO (name) = ri;
219 ri->set_nonzero_bits (wi::shwi (-1, precision));
222 /* Record the range type. */
223 if (SSA_NAME_RANGE_TYPE (name) != range_type)
224 SSA_NAME_ANTI_RANGE_P (name) = (range_type == VR_ANTI_RANGE);
226 /* Set the values. */
227 ri->set_min (min);
228 ri->set_max (max);
230 /* If it is a range, try to improve nonzero_bits from the min/max. */
231 if (range_type == VR_RANGE)
233 wide_int xorv = ri->get_min () ^ ri->get_max ();
234 if (xorv != 0)
235 xorv = wi::mask (precision - wi::clz (xorv), false, precision);
236 ri->set_nonzero_bits (ri->get_nonzero_bits () & (ri->get_min () | xorv));
241 /* Gets range information MIN, MAX and returns enum value_range_type
242 corresponding to tree ssa_name NAME. enum value_range_type returned
243 is used to determine if MIN and MAX are valid values. */
245 enum value_range_type
246 get_range_info (const_tree name, wide_int *min, wide_int *max)
248 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
249 gcc_assert (min && max);
250 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
252 /* Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
253 with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision. */
254 if (!ri || (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (name)))
255 > 2 * HOST_BITS_PER_WIDE_INT))
256 return VR_VARYING;
258 *min = ri->get_min ();
259 *max = ri->get_max ();
260 return SSA_NAME_RANGE_TYPE (name);
263 /* Change non-zero bits bitmask of NAME. */
265 void
266 set_nonzero_bits (tree name, const wide_int_ref &mask)
268 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
269 if (SSA_NAME_RANGE_INFO (name) == NULL)
270 set_range_info (name, VR_RANGE,
271 TYPE_MIN_VALUE (TREE_TYPE (name)),
272 TYPE_MAX_VALUE (TREE_TYPE (name)));
273 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
274 ri->set_nonzero_bits (mask);
277 /* Return a widest_int with potentially non-zero bits in SSA_NAME
278 NAME, or -1 if unknown. */
280 wide_int
281 get_nonzero_bits (const_tree name)
283 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
284 if (POINTER_TYPE_P (TREE_TYPE (name)))
286 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
287 if (pi && pi->align)
288 return wi::shwi (-(HOST_WIDE_INT) pi->align
289 | (HOST_WIDE_INT) pi->misalign, precision);
290 return wi::shwi (-1, precision);
293 range_info_def *ri = SSA_NAME_RANGE_INFO (name);
294 if (!ri)
295 return wi::shwi (-1, precision);
297 return ri->get_nonzero_bits ();
300 /* We no longer need the SSA_NAME expression VAR, release it so that
301 it may be reused.
303 Note it is assumed that no calls to make_ssa_name will be made
304 until all uses of the ssa name are released and that the only
305 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
306 other fields must be assumed clobbered. */
308 void
309 release_ssa_name_fn (struct function *fn, tree var)
311 if (!var)
312 return;
314 /* Never release the default definition for a symbol. It's a
315 special SSA name that should always exist once it's created. */
316 if (SSA_NAME_IS_DEFAULT_DEF (var))
317 return;
319 /* If VAR has been registered for SSA updating, don't remove it.
320 After update_ssa has run, the name will be released. */
321 if (name_registered_for_update_p (var))
323 release_ssa_name_after_update_ssa (var);
324 return;
327 /* release_ssa_name can be called multiple times on a single SSA_NAME.
328 However, it should only end up on our free list one time. We
329 keep a status bit in the SSA_NAME node itself to indicate it has
330 been put on the free list.
332 Note that once on the freelist you can not reference the SSA_NAME's
333 defining statement. */
334 if (! SSA_NAME_IN_FREE_LIST (var))
336 tree saved_ssa_name_var = SSA_NAME_VAR (var);
337 int saved_ssa_name_version = SSA_NAME_VERSION (var);
338 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
340 if (MAY_HAVE_DEBUG_STMTS)
341 insert_debug_temp_for_var_def (NULL, var);
343 #ifdef ENABLE_CHECKING
344 verify_imm_links (stderr, var);
345 #endif
346 while (imm->next != imm)
347 delink_imm_use (imm->next);
349 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
350 memset (var, 0, tree_size (var));
352 imm->prev = imm;
353 imm->next = imm;
354 imm->loc.ssa_name = var;
356 /* First put back the right tree node so that the tree checking
357 macros do not complain. */
358 TREE_SET_CODE (var, SSA_NAME);
360 /* Restore the version number. */
361 SSA_NAME_VERSION (var) = saved_ssa_name_version;
363 /* Hopefully this can go away once we have the new incremental
364 SSA updating code installed. */
365 SET_SSA_NAME_VAR_OR_IDENTIFIER (var, saved_ssa_name_var);
367 /* Note this SSA_NAME is now in the first list. */
368 SSA_NAME_IN_FREE_LIST (var) = 1;
370 /* And finally put it on the free list. */
371 vec_safe_push (FREE_SSANAMES (fn), var);
375 /* If the alignment of the pointer described by PI is known, return true and
376 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
377 respectively. Otherwise return false. */
379 bool
380 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
381 unsigned int *misalignp)
383 if (pi->align)
385 *alignp = pi->align;
386 *misalignp = pi->misalign;
387 return true;
389 else
390 return false;
393 /* State that the pointer described by PI has unknown alignment. */
395 void
396 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
398 pi->align = 0;
399 pi->misalign = 0;
402 /* Store the the power-of-two byte alignment and the deviation from that
403 alignment of pointer described by PI to ALIOGN and MISALIGN
404 respectively. */
406 void
407 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
408 unsigned int misalign)
410 gcc_checking_assert (align != 0);
411 gcc_assert ((align & (align - 1)) == 0);
412 gcc_assert ((misalign & ~(align - 1)) == 0);
414 pi->align = align;
415 pi->misalign = misalign;
418 /* If pointer described by PI has known alignment, increase its known
419 misalignment by INCREMENT modulo its current alignment. */
421 void
422 adjust_ptr_info_misalignment (struct ptr_info_def *pi,
423 unsigned int increment)
425 if (pi->align != 0)
427 pi->misalign += increment;
428 pi->misalign &= (pi->align - 1);
432 /* Return the alias information associated with pointer T. It creates a
433 new instance if none existed. */
435 struct ptr_info_def *
436 get_ptr_info (tree t)
438 struct ptr_info_def *pi;
440 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
442 pi = SSA_NAME_PTR_INFO (t);
443 if (pi == NULL)
445 pi = ggc_cleared_alloc<ptr_info_def> ();
446 pt_solution_reset (&pi->pt);
447 mark_ptr_info_alignment_unknown (pi);
448 SSA_NAME_PTR_INFO (t) = pi;
451 return pi;
455 /* Creates a new SSA name using the template NAME tobe defined by
456 statement STMT in function FN. */
458 tree
459 copy_ssa_name_fn (struct function *fn, tree name, gimple stmt)
461 tree new_name;
463 if (SSA_NAME_VAR (name))
464 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
465 else
467 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
468 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
471 return new_name;
475 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
476 the SSA name NAME. */
478 void
479 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
481 struct ptr_info_def *new_ptr_info;
483 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
484 gcc_assert (!SSA_NAME_PTR_INFO (name));
486 if (!ptr_info)
487 return;
489 new_ptr_info = ggc_alloc<ptr_info_def> ();
490 *new_ptr_info = *ptr_info;
492 SSA_NAME_PTR_INFO (name) = new_ptr_info;
495 /* Creates a duplicate of the range_info_def at RANGE_INFO of type
496 RANGE_TYPE for use by the SSA name NAME. */
497 void
498 duplicate_ssa_name_range_info (tree name, enum value_range_type range_type,
499 struct range_info_def *range_info)
501 struct range_info_def *new_range_info;
503 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
504 gcc_assert (!SSA_NAME_RANGE_INFO (name));
505 gcc_assert (!SSA_NAME_ANTI_RANGE_P (name));
507 if (!range_info)
508 return;
510 unsigned int precision = TYPE_PRECISION (TREE_TYPE (name));
511 size_t size = (sizeof (range_info_def)
512 + trailing_wide_ints <3>::extra_size (precision));
513 new_range_info = static_cast<range_info_def *> (ggc_internal_alloc (size));
514 memcpy (new_range_info, range_info, size);
516 gcc_assert (range_type == VR_RANGE || range_type == VR_ANTI_RANGE);
517 SSA_NAME_ANTI_RANGE_P (name) = (range_type == VR_ANTI_RANGE);
518 SSA_NAME_RANGE_INFO (name) = new_range_info;
523 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
524 in function FN. */
526 tree
527 duplicate_ssa_name_fn (struct function *fn, tree name, gimple stmt)
529 tree new_name = copy_ssa_name_fn (fn, name, stmt);
530 if (POINTER_TYPE_P (TREE_TYPE (name)))
532 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
534 if (old_ptr_info)
535 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
537 else
539 struct range_info_def *old_range_info = SSA_NAME_RANGE_INFO (name);
541 if (old_range_info)
542 duplicate_ssa_name_range_info (new_name, SSA_NAME_RANGE_TYPE (name),
543 old_range_info);
546 return new_name;
550 /* Release all the SSA_NAMEs created by STMT. */
552 void
553 release_defs (gimple stmt)
555 tree def;
556 ssa_op_iter iter;
558 /* Make sure that we are in SSA. Otherwise, operand cache may point
559 to garbage. */
560 gcc_assert (gimple_in_ssa_p (cfun));
562 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
563 if (TREE_CODE (def) == SSA_NAME)
564 release_ssa_name (def);
568 /* Replace the symbol associated with SSA_NAME with SYM. */
570 void
571 replace_ssa_name_symbol (tree ssa_name, tree sym)
573 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
574 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
577 /* Return SSA names that are unused to GGC memory and compact the SSA
578 version namespace. This is used to keep footprint of compiler during
579 interprocedural optimization. */
581 namespace {
583 const pass_data pass_data_release_ssa_names =
585 GIMPLE_PASS, /* type */
586 "release_ssa", /* name */
587 OPTGROUP_NONE, /* optinfo_flags */
588 TV_TREE_SSA_OTHER, /* tv_id */
589 PROP_ssa, /* properties_required */
590 0, /* properties_provided */
591 0, /* properties_destroyed */
592 TODO_remove_unused_locals, /* todo_flags_start */
593 0, /* todo_flags_finish */
596 class pass_release_ssa_names : public gimple_opt_pass
598 public:
599 pass_release_ssa_names (gcc::context *ctxt)
600 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
603 /* opt_pass methods: */
604 virtual unsigned int execute (function *);
606 }; // class pass_release_ssa_names
608 unsigned int
609 pass_release_ssa_names::execute (function *fun)
611 unsigned i, j;
612 int n = vec_safe_length (FREE_SSANAMES (fun));
614 /* Now release the freelist. */
615 vec_free (FREE_SSANAMES (fun));
617 /* And compact the SSA number space. We make sure to not change the
618 relative order of SSA versions. */
619 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
621 tree name = ssa_name (i);
622 if (name)
624 if (i != j)
626 SSA_NAME_VERSION (name) = j;
627 (*fun->gimple_df->ssa_names)[j] = name;
629 j++;
632 fun->gimple_df->ssa_names->truncate (j);
634 statistics_counter_event (fun, "SSA names released", n);
635 statistics_counter_event (fun, "SSA name holes removed", i - j);
636 if (dump_file)
637 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
638 n, n * 100.0 / num_ssa_names, i - j);
639 return 0;
642 } // anon namespace
644 gimple_opt_pass *
645 make_pass_release_ssa_names (gcc::context *ctxt)
647 return new pass_release_ssa_names (ctxt);