c++: fix throwing cleanup with label
[official-gcc.git] / gcc / tree-ssanames.cc
blob5fdb6a37e9fa2b538d8d8a5fb26e365a8071677f
1 /* Generic routines for manipulating SSA_NAME expressions
2 Copyright (C) 2003-2023 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 "tree-pass.h"
27 #include "ssa.h"
28 #include "gimple-iterator.h"
29 #include "stor-layout.h"
30 #include "tree-into-ssa.h"
31 #include "tree-ssa.h"
32 #include "cfgloop.h"
33 #include "tree-scalar-evolution.h"
34 #include "value-query.h"
35 #include "value-range-storage.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
75 /* Return TRUE if NAME has global range info. */
77 inline bool
78 range_info_p (const_tree name)
80 return SSA_NAME_RANGE_INFO (name);
83 /* Return TRUE if R fits in the global range of NAME. */
85 inline bool
86 range_info_fits_p (tree name, const vrange &r)
88 gcc_checking_assert (range_info_p (name));
89 vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
90 return mem->fits_p (r);
93 /* Allocate a new global range for NAME and set it to R. Return the
94 allocation slot. */
96 inline void *
97 range_info_alloc (tree name, const vrange &r)
99 vrange_storage *mem = ggc_alloc_vrange_storage (r);
100 SSA_NAME_RANGE_INFO (name) = mem;
101 return mem;
104 /* Free storage allocated for the global range for NAME. */
106 inline void
107 range_info_free (tree name)
109 vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
110 ggc_free (mem);
113 /* Return the global range for NAME in R. */
115 inline void
116 range_info_get_range (const_tree name, vrange &r)
118 SSA_NAME_RANGE_INFO (name)->get_vrange (r, TREE_TYPE (name));
121 /* Set the global range for NAME from R. Return TRUE if successfull,
122 or FALSE if we can't set a range of NAME's type. */
124 inline bool
125 range_info_set_range (tree name, const vrange &r)
127 if (!range_info_p (name) || !range_info_fits_p (name, r))
129 if (range_info_p (name))
130 range_info_free (name);
132 return range_info_alloc (name, r);
134 else
136 SSA_NAME_RANGE_INFO (name)->set_vrange (r);
137 return true;
141 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
142 zero use default. */
144 void
145 init_ssanames (struct function *fn, int size)
147 if (!size)
148 vec_alloc (SSANAMES (fn), 50);
149 else
150 vec_safe_reserve (SSANAMES (fn), size, true);
152 /* Version 0 is special, so reserve the first slot in the table. Though
153 currently unused, we may use version 0 in alias analysis as part of
154 the heuristics used to group aliases when the alias sets are too
155 large.
157 We use vec::quick_push here because we know that SSA_NAMES has at
158 least 50 elements reserved in it. */
159 SSANAMES (fn)->quick_push (NULL_TREE);
160 FREE_SSANAMES (fn) = NULL;
161 FREE_SSANAMES_QUEUE (fn) = NULL;
163 fn->gimple_df->ssa_renaming_needed = 0;
164 fn->gimple_df->rename_vops = 0;
167 /* Finalize management of SSA_NAMEs. */
169 void
170 fini_ssanames (struct function *fn)
172 unsigned i;
173 tree name;
174 /* Some SSA names leak into global tree data structures so we can't simply
175 ggc_free them. But make sure to clear references to stmts since we now
176 ggc_free the CFG itself. */
177 FOR_EACH_VEC_SAFE_ELT (SSANAMES (fn), i, name)
178 if (name)
179 SSA_NAME_DEF_STMT (name) = NULL;
180 vec_free (SSANAMES (fn));
181 vec_free (FREE_SSANAMES (fn));
182 vec_free (FREE_SSANAMES_QUEUE (fn));
185 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
187 void
188 ssanames_print_statistics (void)
190 fprintf (stderr, "%-32s" PRsa (11) "\n", "SSA_NAME nodes allocated:",
191 SIZE_AMOUNT (ssa_name_nodes_created));
192 fprintf (stderr, "%-32s" PRsa (11) "\n", "SSA_NAME nodes reused:",
193 SIZE_AMOUNT (ssa_name_nodes_reused));
196 /* Verify the state of the SSA_NAME lists.
198 There must be no duplicates on the free list.
199 Every name on the free list must be marked as on the free list.
200 Any name on the free list must not appear in the IL.
201 No names can be leaked. */
203 DEBUG_FUNCTION void
204 verify_ssaname_freelists (struct function *fun)
206 if (!gimple_in_ssa_p (fun))
207 return;
209 auto_bitmap names_in_il;
211 /* Walk the entire IL noting every SSA_NAME we see. */
212 basic_block bb;
213 FOR_EACH_BB_FN (bb, fun)
215 tree t;
216 /* First note the result and arguments of PHI nodes. */
217 for (gphi_iterator gsi = gsi_start_phis (bb);
218 !gsi_end_p (gsi);
219 gsi_next (&gsi))
221 gphi *phi = gsi.phi ();
222 t = gimple_phi_result (phi);
223 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
225 for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
227 t = gimple_phi_arg_def (phi, i);
228 if (TREE_CODE (t) == SSA_NAME)
229 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
233 /* Then note the operands of each statement. */
234 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
235 !gsi_end_p (gsi);
236 gsi_next (&gsi))
238 ssa_op_iter iter;
239 gimple *stmt = gsi_stmt (gsi);
240 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, SSA_OP_ALL_OPERANDS)
241 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
245 /* Now walk the free list noting what we find there and verifying
246 there are no duplicates. */
247 auto_bitmap names_in_freelists;
248 if (FREE_SSANAMES (fun))
250 for (unsigned int i = 0; i < FREE_SSANAMES (fun)->length (); i++)
252 tree t = (*FREE_SSANAMES (fun))[i];
254 /* Verify that the name is marked as being in the free list. */
255 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
257 /* Verify the name has not already appeared in the free list and
258 note it in the list of names found in the free list. */
259 gcc_assert (!bitmap_bit_p (names_in_freelists, SSA_NAME_VERSION (t)));
260 bitmap_set_bit (names_in_freelists, SSA_NAME_VERSION (t));
264 /* Similarly for the names in the pending free list. */
265 if (FREE_SSANAMES_QUEUE (fun))
267 for (unsigned int i = 0; i < FREE_SSANAMES_QUEUE (fun)->length (); i++)
269 tree t = (*FREE_SSANAMES_QUEUE (fun))[i];
271 /* Verify that the name is marked as being in the free list. */
272 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
274 /* Verify the name has not already appeared in the free list and
275 note it in the list of names found in the free list. */
276 gcc_assert (!bitmap_bit_p (names_in_freelists, SSA_NAME_VERSION (t)));
277 bitmap_set_bit (names_in_freelists, SSA_NAME_VERSION (t));
281 /* If any name appears in both the IL and the freelists, then
282 something horrible has happened. */
283 bool intersect_p = bitmap_intersect_p (names_in_il, names_in_freelists);
284 gcc_assert (!intersect_p);
286 /* Names can be queued up for release if there is an ssa update
287 pending. Pretend we saw them in the IL. */
288 if (names_to_release)
289 bitmap_ior_into (names_in_il, names_to_release);
291 /* Function splitting can "lose" SSA_NAMEs in an effort to ensure that
292 debug/non-debug compilations have the same SSA_NAMEs. So for each
293 lost SSA_NAME, see if it's likely one from that wart. These will always
294 be marked as default definitions. So we loosely assume that anything
295 marked as a default definition isn't leaked by pretending they are
296 in the IL. */
297 for (unsigned int i = UNUSED_NAME_VERSION + 1; i < num_ssa_names; i++)
298 if (ssa_name (i) && SSA_NAME_IS_DEFAULT_DEF (ssa_name (i)))
299 bitmap_set_bit (names_in_il, i);
301 unsigned int i;
302 bitmap_iterator bi;
303 auto_bitmap all_names;
304 bitmap_set_range (all_names, UNUSED_NAME_VERSION + 1, num_ssa_names - 1);
305 bitmap_ior_into (names_in_il, names_in_freelists);
307 /* Any name not mentioned in the IL and not in the feelists
308 has been leaked. */
309 EXECUTE_IF_AND_COMPL_IN_BITMAP(all_names, names_in_il,
310 UNUSED_NAME_VERSION + 1, i, bi)
311 gcc_assert (!ssa_name (i));
314 /* Move all SSA_NAMEs from FREE_SSA_NAMES_QUEUE to FREE_SSA_NAMES.
316 We do not, but should have a mode to verify the state of the SSA_NAMEs
317 lists. In particular at this point every name must be in the IL,
318 on the free list or in the queue. Anything else is an error. */
320 void
321 flush_ssaname_freelist (void)
323 /* If there were any SSA names released reset the SCEV cache. */
324 if (! vec_safe_is_empty (FREE_SSANAMES_QUEUE (cfun)))
325 scev_reset_htab ();
326 vec_safe_splice (FREE_SSANAMES (cfun), FREE_SSANAMES_QUEUE (cfun));
327 vec_safe_truncate (FREE_SSANAMES_QUEUE (cfun), 0);
330 /* Initialize SSA_NAME_IMM_USE_NODE of a SSA NAME. */
332 void
333 init_ssa_name_imm_use (tree name)
335 use_operand_p imm;
336 imm = &(SSA_NAME_IMM_USE_NODE (name));
337 imm->use = NULL;
338 imm->prev = imm;
339 imm->next = imm;
340 imm->loc.ssa_name = name;
343 /* Return an SSA_NAME node for variable VAR defined in statement STMT
344 in function FN. STMT may be an empty statement for artificial
345 references (e.g., default definitions created when a variable is
346 used without a preceding definition). If VERISON is not zero then
347 allocate the SSA name with that version. */
349 tree
350 make_ssa_name_fn (struct function *fn, tree var, gimple *stmt,
351 unsigned int version)
353 tree t;
354 gcc_assert (VAR_P (var)
355 || TREE_CODE (var) == PARM_DECL
356 || TREE_CODE (var) == RESULT_DECL
357 || (TYPE_P (var) && is_gimple_reg_type (var)));
359 /* Get the specified SSA name version. */
360 if (version != 0)
362 t = make_node (SSA_NAME);
363 SSA_NAME_VERSION (t) = version;
364 if (version >= SSANAMES (fn)->length ())
365 vec_safe_grow_cleared (SSANAMES (fn), version + 1, true);
366 gcc_assert ((*SSANAMES (fn))[version] == NULL);
367 (*SSANAMES (fn))[version] = t;
368 ssa_name_nodes_created++;
370 /* If our free list has an element, then use it. */
371 else if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
373 t = FREE_SSANAMES (fn)->pop ();
374 ssa_name_nodes_reused++;
376 /* The node was cleared out when we put it on the free list, so
377 there is no need to do so again here. */
378 gcc_assert ((*SSANAMES (fn))[SSA_NAME_VERSION (t)] == NULL);
379 (*SSANAMES (fn))[SSA_NAME_VERSION (t)] = t;
381 else
383 t = make_node (SSA_NAME);
384 SSA_NAME_VERSION (t) = SSANAMES (fn)->length ();
385 vec_safe_push (SSANAMES (fn), t);
386 ssa_name_nodes_created++;
389 if (TYPE_P (var))
391 TREE_TYPE (t) = TYPE_MAIN_VARIANT (var);
392 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
394 else
396 TREE_TYPE (t) = TREE_TYPE (var);
397 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
399 SSA_NAME_DEF_STMT (t) = stmt;
400 if (POINTER_TYPE_P (TREE_TYPE (t)))
401 SSA_NAME_PTR_INFO (t) = NULL;
402 else
403 SSA_NAME_RANGE_INFO (t) = NULL;
405 SSA_NAME_IN_FREE_LIST (t) = 0;
406 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
407 init_ssa_name_imm_use (t);
409 return t;
412 /* Update the range information for NAME, intersecting into an existing
413 range if applicable. Return TRUE if the range was updated. */
415 bool
416 set_range_info (tree name, const vrange &r)
418 if (r.undefined_p () || r.varying_p ())
419 return false;
421 tree type = TREE_TYPE (name);
422 if (POINTER_TYPE_P (type))
424 if (r.nonzero_p ())
426 set_ptr_nonnull (name);
427 return true;
429 return false;
432 /* If a global range already exists, incorporate it. */
433 if (range_info_p (name))
435 Value_Range tmp (type);
436 range_info_get_range (name, tmp);
437 tmp.intersect (r);
438 if (tmp.undefined_p ())
439 return false;
441 return range_info_set_range (name, tmp);
443 return range_info_set_range (name, r);
446 /* Set nonnull attribute to pointer NAME. */
448 void
449 set_ptr_nonnull (tree name)
451 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
452 struct ptr_info_def *pi = get_ptr_info (name);
453 pi->pt.null = 0;
456 /* Update the non-zero bits bitmask of NAME. */
458 void
459 set_nonzero_bits (tree name, const wide_int &mask)
461 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
463 int_range<2> r (TREE_TYPE (name));
464 r.set_nonzero_bits (mask);
465 set_range_info (name, r);
468 /* Return a widest_int with potentially non-zero bits in SSA_NAME
469 NAME, the constant for INTEGER_CST, or -1 if unknown. */
471 wide_int
472 get_nonzero_bits (const_tree name)
474 if (TREE_CODE (name) == INTEGER_CST)
475 return wi::to_wide (name);
477 /* Use element_precision instead of TYPE_PRECISION so complex and
478 vector types get a non-zero precision. */
479 unsigned int precision = element_precision (TREE_TYPE (name));
480 if (POINTER_TYPE_P (TREE_TYPE (name)))
482 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
483 if (pi && pi->align)
484 return wi::shwi (-(HOST_WIDE_INT) pi->align
485 | (HOST_WIDE_INT) pi->misalign, precision);
486 return wi::shwi (-1, precision);
489 if (!range_info_p (name) || !irange::supports_p (TREE_TYPE (name)))
490 return wi::shwi (-1, precision);
492 int_range_max tmp;
493 range_info_get_range (name, tmp);
494 return tmp.get_nonzero_bits ();
497 /* Return TRUE is OP, an SSA_NAME has a range of values [0..1], false
498 otherwise.
500 This can be because it is a boolean type, any unsigned integral
501 type with a single bit of precision, or has known range of [0..1]
502 via range analysis. */
504 bool
505 ssa_name_has_boolean_range (tree op)
507 gcc_assert (TREE_CODE (op) == SSA_NAME);
509 /* Boolean types always have a range [0..1]. */
510 if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE)
511 return true;
513 /* An integral type with a single bit of precision. */
514 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
515 && TYPE_UNSIGNED (TREE_TYPE (op))
516 && TYPE_PRECISION (TREE_TYPE (op)) == 1)
517 return true;
519 /* An integral type with more precision, but the object
520 only takes on values [0..1] as determined by range
521 analysis. */
522 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
523 && (TYPE_PRECISION (TREE_TYPE (op)) > 1))
525 int_range<2> r;
526 if (get_range_query (cfun)->range_of_expr (r, op)
527 && r == range_true_and_false (TREE_TYPE (op)))
528 return true;
530 if (wi::eq_p (get_nonzero_bits (op), 1))
531 return true;
534 return false;
537 /* We no longer need the SSA_NAME expression VAR, release it so that
538 it may be reused.
540 Note it is assumed that no calls to make_ssa_name will be made
541 until all uses of the ssa name are released and that the only
542 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
543 other fields must be assumed clobbered. */
545 void
546 release_ssa_name_fn (struct function *fn, tree var)
548 if (!var)
549 return;
551 /* Never release the default definition for a symbol. It's a
552 special SSA name that should always exist once it's created. */
553 if (SSA_NAME_IS_DEFAULT_DEF (var))
554 return;
556 /* If VAR has been registered for SSA updating, don't remove it.
557 After update_ssa has run, the name will be released. */
558 if (name_registered_for_update_p (var))
560 release_ssa_name_after_update_ssa (var);
561 return;
564 /* release_ssa_name can be called multiple times on a single SSA_NAME.
565 However, it should only end up on our free list one time. We
566 keep a status bit in the SSA_NAME node itself to indicate it has
567 been put on the free list.
569 Note that once on the freelist you cannot reference the SSA_NAME's
570 defining statement. */
571 if (! SSA_NAME_IN_FREE_LIST (var))
573 int saved_ssa_name_version = SSA_NAME_VERSION (var);
574 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
576 if (MAY_HAVE_DEBUG_BIND_STMTS)
577 insert_debug_temp_for_var_def (NULL, var);
579 if (flag_checking)
580 verify_imm_links (stderr, var);
581 while (imm->next != imm)
582 delink_imm_use (imm->next);
584 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
585 memset (var, 0, tree_size (var));
587 imm->prev = imm;
588 imm->next = imm;
589 imm->loc.ssa_name = var;
591 /* First put back the right tree node so that the tree checking
592 macros do not complain. */
593 TREE_SET_CODE (var, SSA_NAME);
595 /* Restore the version number. */
596 SSA_NAME_VERSION (var) = saved_ssa_name_version;
598 /* Note this SSA_NAME is now in the first list. */
599 SSA_NAME_IN_FREE_LIST (var) = 1;
601 /* Put in a non-NULL TREE_TYPE so dumping code will not ICE
602 if it happens to come along a released SSA name and tries
603 to inspect its type. */
604 TREE_TYPE (var) = error_mark_node;
606 /* And finally queue it so that it will be put on the free list. */
607 vec_safe_push (FREE_SSANAMES_QUEUE (fn), var);
611 /* If the alignment of the pointer described by PI is known, return true and
612 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
613 respectively. Otherwise return false. */
615 bool
616 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
617 unsigned int *misalignp)
619 if (pi->align)
621 *alignp = pi->align;
622 *misalignp = pi->misalign;
623 return true;
625 else
626 return false;
629 /* State that the pointer described by PI has unknown alignment. */
631 void
632 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
634 pi->align = 0;
635 pi->misalign = 0;
638 /* Store the power-of-two byte alignment and the deviation from that
639 alignment of pointer described by PI to ALIOGN and MISALIGN
640 respectively. */
642 void
643 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
644 unsigned int misalign)
646 gcc_checking_assert (align != 0);
647 gcc_assert ((align & (align - 1)) == 0);
648 gcc_assert ((misalign & ~(align - 1)) == 0);
650 pi->align = align;
651 pi->misalign = misalign;
654 /* If pointer described by PI has known alignment, increase its known
655 misalignment by INCREMENT modulo its current alignment. */
657 void
658 adjust_ptr_info_misalignment (struct ptr_info_def *pi, poly_uint64 increment)
660 if (pi->align != 0)
662 increment += pi->misalign;
663 if (!known_misalignment (increment, pi->align, &pi->misalign))
665 pi->align = known_alignment (increment);
666 pi->misalign = 0;
671 /* Return the alias information associated with pointer T. It creates a
672 new instance if none existed. */
674 struct ptr_info_def *
675 get_ptr_info (tree t)
677 struct ptr_info_def *pi;
679 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
681 pi = SSA_NAME_PTR_INFO (t);
682 if (pi == NULL)
684 pi = ggc_cleared_alloc<ptr_info_def> ();
685 pt_solution_reset (&pi->pt);
686 mark_ptr_info_alignment_unknown (pi);
687 SSA_NAME_PTR_INFO (t) = pi;
690 return pi;
694 /* Creates a new SSA name using the template NAME tobe defined by
695 statement STMT in function FN. */
697 tree
698 copy_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
700 tree new_name;
702 if (SSA_NAME_VAR (name))
703 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
704 else
706 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
707 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
710 return new_name;
714 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
715 the SSA name NAME. */
717 void
718 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
720 struct ptr_info_def *new_ptr_info;
722 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
723 gcc_assert (!SSA_NAME_PTR_INFO (name));
725 if (!ptr_info)
726 return;
728 new_ptr_info = ggc_alloc<ptr_info_def> ();
729 *new_ptr_info = *ptr_info;
731 SSA_NAME_PTR_INFO (name) = new_ptr_info;
734 void
735 duplicate_ssa_name_range_info (tree name, tree src)
737 gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (src)));
738 gcc_checking_assert (!range_info_p (name));
740 if (range_info_p (src))
742 Value_Range src_range (TREE_TYPE (src));
743 range_info_get_range (src, src_range);
744 range_info_set_range (name, src_range);
749 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
750 in function FN. */
752 tree
753 duplicate_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
755 tree new_name = copy_ssa_name_fn (fn, name, stmt);
756 if (POINTER_TYPE_P (TREE_TYPE (name)))
758 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
760 if (old_ptr_info)
761 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
763 else if (range_info_p (name))
764 duplicate_ssa_name_range_info (new_name, name);
766 return new_name;
770 /* Reset all flow sensitive data on NAME such as range-info, nonzero
771 bits and alignment. */
773 void
774 reset_flow_sensitive_info (tree name)
776 if (POINTER_TYPE_P (TREE_TYPE (name)))
778 /* points-to info is not flow-sensitive. */
779 if (SSA_NAME_PTR_INFO (name))
781 /* [E]VRP can derive context sensitive alignment info and
782 non-nullness properties. We must reset both. */
783 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
784 SSA_NAME_PTR_INFO (name)->pt.null = 1;
787 else
788 SSA_NAME_RANGE_INFO (name) = NULL;
791 /* Clear all flow sensitive data from all statements and PHI definitions
792 in BB. */
794 void
795 reset_flow_sensitive_info_in_bb (basic_block bb)
797 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
798 gsi_next (&gsi))
800 gimple *stmt = gsi_stmt (gsi);
801 ssa_op_iter i;
802 tree op;
803 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
804 reset_flow_sensitive_info (op);
807 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
808 gsi_next (&gsi))
810 tree phi_def = gimple_phi_result (gsi.phi ());
811 reset_flow_sensitive_info (phi_def);
815 /* Release all the SSA_NAMEs created by STMT. */
817 void
818 release_defs (gimple *stmt)
820 tree def;
821 ssa_op_iter iter;
823 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
824 if (TREE_CODE (def) == SSA_NAME)
825 release_ssa_name (def);
829 /* Replace the symbol associated with SSA_NAME with SYM. */
831 void
832 replace_ssa_name_symbol (tree ssa_name, tree sym)
834 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
835 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
838 /* Release the vector of free SSA_NAMEs and compact the vector of SSA_NAMEs
839 that are live. */
841 static void
842 release_free_names_and_compact_live_names (function *fun)
844 unsigned i, j;
845 int n = vec_safe_length (FREE_SSANAMES (fun));
847 /* Now release the freelist. */
848 vec_free (FREE_SSANAMES (fun));
850 /* And compact the SSA number space. We make sure to not change the
851 relative order of SSA versions. */
852 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
854 tree name = ssa_name (i);
855 if (name)
857 if (i != j)
859 SSA_NAME_VERSION (name) = j;
860 (*fun->gimple_df->ssa_names)[j] = name;
862 j++;
865 fun->gimple_df->ssa_names->truncate (j);
867 statistics_counter_event (fun, "SSA names released", n);
868 statistics_counter_event (fun, "SSA name holes removed", i - j);
869 if (dump_file)
870 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
871 n, n * 100.0 / num_ssa_names, i - j);
874 /* Return SSA names that are unused to GGC memory and compact the SSA
875 version namespace. This is used to keep footprint of compiler during
876 interprocedural optimization. */
878 namespace {
880 const pass_data pass_data_release_ssa_names =
882 GIMPLE_PASS, /* type */
883 "release_ssa", /* name */
884 OPTGROUP_NONE, /* optinfo_flags */
885 TV_TREE_SSA_OTHER, /* tv_id */
886 PROP_ssa, /* properties_required */
887 0, /* properties_provided */
888 0, /* properties_destroyed */
889 TODO_remove_unused_locals, /* todo_flags_start */
890 0, /* todo_flags_finish */
893 class pass_release_ssa_names : public gimple_opt_pass
895 public:
896 pass_release_ssa_names (gcc::context *ctxt)
897 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
900 /* opt_pass methods: */
901 unsigned int execute (function *) final override;
903 }; // class pass_release_ssa_names
905 unsigned int
906 pass_release_ssa_names::execute (function *fun)
908 release_free_names_and_compact_live_names (fun);
909 return 0;
912 } // anon namespace
914 gimple_opt_pass *
915 make_pass_release_ssa_names (gcc::context *ctxt)
917 return new pass_release_ssa_names (ctxt);