gccrs: add test case to show our query-type system is working
[official-gcc.git] / gcc / tree-ssanames.cc
blob08aa166ef1766912c7253de75b02784d651af1c8
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 static ggc_vrange_allocator ggc_allocator;
76 static vrange_storage vstore (&ggc_allocator);
78 /* Return TRUE if NAME has global range info. */
80 inline bool
81 range_info_p (const_tree name)
83 return SSA_NAME_RANGE_INFO (name);
86 /* Return TRUE if R fits in the global range of NAME. */
88 inline bool
89 range_info_fits_p (tree name, const vrange &r)
91 gcc_checking_assert (range_info_p (name));
92 void *mem = SSA_NAME_RANGE_INFO (name);
93 return vrange_storage::fits_p (mem, r);
96 /* Allocate a new global range for NAME and set it to R. Return the
97 allocation slot. */
99 inline void *
100 range_info_alloc (tree name, const vrange &r)
102 void *mem = vstore.alloc_slot (r);
103 SSA_NAME_RANGE_INFO (name) = mem;
104 return mem;
107 /* Free storage allocated for the global range for NAME. */
109 inline void
110 range_info_free (tree name)
112 void *mem = SSA_NAME_RANGE_INFO (name);
113 vstore.free (mem);
116 /* Return the global range for NAME in R. */
118 inline void
119 range_info_get_range (tree name, vrange &r)
121 vstore.get_vrange (SSA_NAME_RANGE_INFO (name), r, TREE_TYPE (name));
124 /* Set the global range for NAME from R. Return TRUE if successfull,
125 or FALSE if we can't set a range of NAME's type. */
127 inline bool
128 range_info_set_range (tree name, const vrange &r)
130 if (!range_info_p (name) || !range_info_fits_p (name, r))
132 if (range_info_p (name))
133 range_info_free (name);
135 return range_info_alloc (name, r);
137 else
139 vstore.set_vrange (SSA_NAME_RANGE_INFO (name), r);
140 return true;
144 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
145 zero use default. */
147 void
148 init_ssanames (struct function *fn, int size)
150 if (!size)
151 vec_alloc (SSANAMES (fn), 50);
152 else
153 vec_safe_reserve (SSANAMES (fn), size, true);
155 /* Version 0 is special, so reserve the first slot in the table. Though
156 currently unused, we may use version 0 in alias analysis as part of
157 the heuristics used to group aliases when the alias sets are too
158 large.
160 We use vec::quick_push here because we know that SSA_NAMES has at
161 least 50 elements reserved in it. */
162 SSANAMES (fn)->quick_push (NULL_TREE);
163 FREE_SSANAMES (fn) = NULL;
164 FREE_SSANAMES_QUEUE (fn) = NULL;
166 fn->gimple_df->ssa_renaming_needed = 0;
167 fn->gimple_df->rename_vops = 0;
170 /* Finalize management of SSA_NAMEs. */
172 void
173 fini_ssanames (struct function *fn)
175 unsigned i;
176 tree name;
177 /* Some SSA names leak into global tree data structures so we can't simply
178 ggc_free them. But make sure to clear references to stmts since we now
179 ggc_free the CFG itself. */
180 FOR_EACH_VEC_SAFE_ELT (SSANAMES (fn), i, name)
181 if (name)
182 SSA_NAME_DEF_STMT (name) = NULL;
183 vec_free (SSANAMES (fn));
184 vec_free (FREE_SSANAMES (fn));
185 vec_free (FREE_SSANAMES_QUEUE (fn));
188 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
190 void
191 ssanames_print_statistics (void)
193 fprintf (stderr, "%-32s" PRsa (11) "\n", "SSA_NAME nodes allocated:",
194 SIZE_AMOUNT (ssa_name_nodes_created));
195 fprintf (stderr, "%-32s" PRsa (11) "\n", "SSA_NAME nodes reused:",
196 SIZE_AMOUNT (ssa_name_nodes_reused));
199 /* Verify the state of the SSA_NAME lists.
201 There must be no duplicates on the free list.
202 Every name on the free list must be marked as on the free list.
203 Any name on the free list must not appear in the IL.
204 No names can be leaked. */
206 DEBUG_FUNCTION void
207 verify_ssaname_freelists (struct function *fun)
209 if (!gimple_in_ssa_p (fun))
210 return;
212 auto_bitmap names_in_il;
214 /* Walk the entire IL noting every SSA_NAME we see. */
215 basic_block bb;
216 FOR_EACH_BB_FN (bb, fun)
218 tree t;
219 /* First note the result and arguments of PHI nodes. */
220 for (gphi_iterator gsi = gsi_start_phis (bb);
221 !gsi_end_p (gsi);
222 gsi_next (&gsi))
224 gphi *phi = gsi.phi ();
225 t = gimple_phi_result (phi);
226 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
228 for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
230 t = gimple_phi_arg_def (phi, i);
231 if (TREE_CODE (t) == SSA_NAME)
232 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
236 /* Then note the operands of each statement. */
237 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
238 !gsi_end_p (gsi);
239 gsi_next (&gsi))
241 ssa_op_iter iter;
242 gimple *stmt = gsi_stmt (gsi);
243 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, SSA_OP_ALL_OPERANDS)
244 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
248 /* Now walk the free list noting what we find there and verifying
249 there are no duplicates. */
250 auto_bitmap names_in_freelists;
251 if (FREE_SSANAMES (fun))
253 for (unsigned int i = 0; i < FREE_SSANAMES (fun)->length (); i++)
255 tree t = (*FREE_SSANAMES (fun))[i];
257 /* Verify that the name is marked as being in the free list. */
258 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
260 /* Verify the name has not already appeared in the free list and
261 note it in the list of names found in the free list. */
262 gcc_assert (!bitmap_bit_p (names_in_freelists, SSA_NAME_VERSION (t)));
263 bitmap_set_bit (names_in_freelists, SSA_NAME_VERSION (t));
267 /* Similarly for the names in the pending free list. */
268 if (FREE_SSANAMES_QUEUE (fun))
270 for (unsigned int i = 0; i < FREE_SSANAMES_QUEUE (fun)->length (); i++)
272 tree t = (*FREE_SSANAMES_QUEUE (fun))[i];
274 /* Verify that the name is marked as being in the free list. */
275 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
277 /* Verify the name has not already appeared in the free list and
278 note it in the list of names found in the free list. */
279 gcc_assert (!bitmap_bit_p (names_in_freelists, SSA_NAME_VERSION (t)));
280 bitmap_set_bit (names_in_freelists, SSA_NAME_VERSION (t));
284 /* If any name appears in both the IL and the freelists, then
285 something horrible has happened. */
286 bool intersect_p = bitmap_intersect_p (names_in_il, names_in_freelists);
287 gcc_assert (!intersect_p);
289 /* Names can be queued up for release if there is an ssa update
290 pending. Pretend we saw them in the IL. */
291 if (names_to_release)
292 bitmap_ior_into (names_in_il, names_to_release);
294 /* Function splitting can "lose" SSA_NAMEs in an effort to ensure that
295 debug/non-debug compilations have the same SSA_NAMEs. So for each
296 lost SSA_NAME, see if it's likely one from that wart. These will always
297 be marked as default definitions. So we loosely assume that anything
298 marked as a default definition isn't leaked by pretending they are
299 in the IL. */
300 for (unsigned int i = UNUSED_NAME_VERSION + 1; i < num_ssa_names; i++)
301 if (ssa_name (i) && SSA_NAME_IS_DEFAULT_DEF (ssa_name (i)))
302 bitmap_set_bit (names_in_il, i);
304 unsigned int i;
305 bitmap_iterator bi;
306 auto_bitmap all_names;
307 bitmap_set_range (all_names, UNUSED_NAME_VERSION + 1, num_ssa_names - 1);
308 bitmap_ior_into (names_in_il, names_in_freelists);
310 /* Any name not mentioned in the IL and not in the feelists
311 has been leaked. */
312 EXECUTE_IF_AND_COMPL_IN_BITMAP(all_names, names_in_il,
313 UNUSED_NAME_VERSION + 1, i, bi)
314 gcc_assert (!ssa_name (i));
317 /* Move all SSA_NAMEs from FREE_SSA_NAMES_QUEUE to FREE_SSA_NAMES.
319 We do not, but should have a mode to verify the state of the SSA_NAMEs
320 lists. In particular at this point every name must be in the IL,
321 on the free list or in the queue. Anything else is an error. */
323 void
324 flush_ssaname_freelist (void)
326 /* If there were any SSA names released reset the SCEV cache. */
327 if (! vec_safe_is_empty (FREE_SSANAMES_QUEUE (cfun)))
328 scev_reset_htab ();
329 vec_safe_splice (FREE_SSANAMES (cfun), FREE_SSANAMES_QUEUE (cfun));
330 vec_safe_truncate (FREE_SSANAMES_QUEUE (cfun), 0);
333 /* Initialize SSA_NAME_IMM_USE_NODE of a SSA NAME. */
335 void
336 init_ssa_name_imm_use (tree name)
338 use_operand_p imm;
339 imm = &(SSA_NAME_IMM_USE_NODE (name));
340 imm->use = NULL;
341 imm->prev = imm;
342 imm->next = imm;
343 imm->loc.ssa_name = name;
346 /* Return an SSA_NAME node for variable VAR defined in statement STMT
347 in function FN. STMT may be an empty statement for artificial
348 references (e.g., default definitions created when a variable is
349 used without a preceding definition). If VERISON is not zero then
350 allocate the SSA name with that version. */
352 tree
353 make_ssa_name_fn (struct function *fn, tree var, gimple *stmt,
354 unsigned int version)
356 tree t;
357 gcc_assert (VAR_P (var)
358 || TREE_CODE (var) == PARM_DECL
359 || TREE_CODE (var) == RESULT_DECL
360 || (TYPE_P (var) && is_gimple_reg_type (var)));
362 /* Get the specified SSA name version. */
363 if (version != 0)
365 t = make_node (SSA_NAME);
366 SSA_NAME_VERSION (t) = version;
367 if (version >= SSANAMES (fn)->length ())
368 vec_safe_grow_cleared (SSANAMES (fn), version + 1, true);
369 gcc_assert ((*SSANAMES (fn))[version] == NULL);
370 (*SSANAMES (fn))[version] = t;
371 ssa_name_nodes_created++;
373 /* If our free list has an element, then use it. */
374 else if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
376 t = FREE_SSANAMES (fn)->pop ();
377 ssa_name_nodes_reused++;
379 /* The node was cleared out when we put it on the free list, so
380 there is no need to do so again here. */
381 gcc_assert ((*SSANAMES (fn))[SSA_NAME_VERSION (t)] == NULL);
382 (*SSANAMES (fn))[SSA_NAME_VERSION (t)] = t;
384 else
386 t = make_node (SSA_NAME);
387 SSA_NAME_VERSION (t) = SSANAMES (fn)->length ();
388 vec_safe_push (SSANAMES (fn), t);
389 ssa_name_nodes_created++;
392 if (TYPE_P (var))
394 TREE_TYPE (t) = TYPE_MAIN_VARIANT (var);
395 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
397 else
399 TREE_TYPE (t) = TREE_TYPE (var);
400 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
402 SSA_NAME_DEF_STMT (t) = stmt;
403 if (POINTER_TYPE_P (TREE_TYPE (t)))
404 SSA_NAME_PTR_INFO (t) = NULL;
405 else
406 SSA_NAME_RANGE_INFO (t) = NULL;
408 SSA_NAME_IN_FREE_LIST (t) = 0;
409 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
410 init_ssa_name_imm_use (t);
412 return t;
415 /* Update the range information for NAME, intersecting into an existing
416 range if applicable. Return TRUE if the range was updated. */
418 bool
419 set_range_info (tree name, const vrange &r)
421 if (r.undefined_p () || r.varying_p ())
422 return false;
424 tree type = TREE_TYPE (name);
425 if (POINTER_TYPE_P (type))
427 if (r.nonzero_p ())
429 set_ptr_nonnull (name);
430 return true;
432 return false;
435 /* If a global range already exists, incorporate it. */
436 if (range_info_p (name))
438 Value_Range tmp (type);
439 range_info_get_range (name, tmp);
440 tmp.intersect (r);
441 if (tmp.undefined_p ())
442 return false;
444 return range_info_set_range (name, tmp);
446 return range_info_set_range (name, r);
449 /* Set nonnull attribute to pointer NAME. */
451 void
452 set_ptr_nonnull (tree name)
454 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
455 struct ptr_info_def *pi = get_ptr_info (name);
456 pi->pt.null = 0;
459 /* Update the non-zero bits bitmask of NAME. */
461 void
462 set_nonzero_bits (tree name, const wide_int_ref &mask)
464 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
466 int_range<2> r (TREE_TYPE (name));
467 r.set_nonzero_bits (mask);
468 set_range_info (name, r);
471 /* Return a widest_int with potentially non-zero bits in SSA_NAME
472 NAME, the constant for INTEGER_CST, or -1 if unknown. */
474 wide_int
475 get_nonzero_bits (const_tree name)
477 if (TREE_CODE (name) == INTEGER_CST)
478 return wi::to_wide (name);
480 /* Use element_precision instead of TYPE_PRECISION so complex and
481 vector types get a non-zero precision. */
482 unsigned int precision = element_precision (TREE_TYPE (name));
483 if (POINTER_TYPE_P (TREE_TYPE (name)))
485 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
486 if (pi && pi->align)
487 return wi::shwi (-(HOST_WIDE_INT) pi->align
488 | (HOST_WIDE_INT) pi->misalign, precision);
489 return wi::shwi (-1, precision);
492 if (!range_info_p (name) || !irange::supports_p (TREE_TYPE (name)))
493 return wi::shwi (-1, precision);
495 /* Optimization to get at the nonzero bits because we know the
496 storage type. This saves us measurable time compared to going
497 through vrange_storage. */
498 irange_storage_slot *ri
499 = static_cast <irange_storage_slot *> (SSA_NAME_RANGE_INFO (name));
500 return ri->get_nonzero_bits ();
503 /* Return TRUE is OP, an SSA_NAME has a range of values [0..1], false
504 otherwise.
506 This can be because it is a boolean type, any unsigned integral
507 type with a single bit of precision, or has known range of [0..1]
508 via range analysis. */
510 bool
511 ssa_name_has_boolean_range (tree op)
513 gcc_assert (TREE_CODE (op) == SSA_NAME);
515 /* Boolean types always have a range [0..1]. */
516 if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE)
517 return true;
519 /* An integral type with a single bit of precision. */
520 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
521 && TYPE_UNSIGNED (TREE_TYPE (op))
522 && TYPE_PRECISION (TREE_TYPE (op)) == 1)
523 return true;
525 /* An integral type with more precision, but the object
526 only takes on values [0..1] as determined by range
527 analysis. */
528 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
529 && (TYPE_PRECISION (TREE_TYPE (op)) > 1))
531 int_range<2> onezero (build_zero_cst (TREE_TYPE (op)),
532 build_one_cst (TREE_TYPE (op)));
533 int_range<2> r;
534 if (get_range_query (cfun)->range_of_expr (r, op) && r == onezero)
535 return true;
537 if (wi::eq_p (get_nonzero_bits (op), 1))
538 return true;
541 return false;
544 /* We no longer need the SSA_NAME expression VAR, release it so that
545 it may be reused.
547 Note it is assumed that no calls to make_ssa_name will be made
548 until all uses of the ssa name are released and that the only
549 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
550 other fields must be assumed clobbered. */
552 void
553 release_ssa_name_fn (struct function *fn, tree var)
555 if (!var)
556 return;
558 /* Never release the default definition for a symbol. It's a
559 special SSA name that should always exist once it's created. */
560 if (SSA_NAME_IS_DEFAULT_DEF (var))
561 return;
563 /* If VAR has been registered for SSA updating, don't remove it.
564 After update_ssa has run, the name will be released. */
565 if (name_registered_for_update_p (var))
567 release_ssa_name_after_update_ssa (var);
568 return;
571 /* release_ssa_name can be called multiple times on a single SSA_NAME.
572 However, it should only end up on our free list one time. We
573 keep a status bit in the SSA_NAME node itself to indicate it has
574 been put on the free list.
576 Note that once on the freelist you cannot reference the SSA_NAME's
577 defining statement. */
578 if (! SSA_NAME_IN_FREE_LIST (var))
580 int saved_ssa_name_version = SSA_NAME_VERSION (var);
581 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
583 if (MAY_HAVE_DEBUG_BIND_STMTS)
584 insert_debug_temp_for_var_def (NULL, var);
586 if (flag_checking)
587 verify_imm_links (stderr, var);
588 while (imm->next != imm)
589 delink_imm_use (imm->next);
591 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
592 memset (var, 0, tree_size (var));
594 imm->prev = imm;
595 imm->next = imm;
596 imm->loc.ssa_name = var;
598 /* First put back the right tree node so that the tree checking
599 macros do not complain. */
600 TREE_SET_CODE (var, SSA_NAME);
602 /* Restore the version number. */
603 SSA_NAME_VERSION (var) = saved_ssa_name_version;
605 /* Note this SSA_NAME is now in the first list. */
606 SSA_NAME_IN_FREE_LIST (var) = 1;
608 /* Put in a non-NULL TREE_TYPE so dumping code will not ICE
609 if it happens to come along a released SSA name and tries
610 to inspect its type. */
611 TREE_TYPE (var) = error_mark_node;
613 /* And finally queue it so that it will be put on the free list. */
614 vec_safe_push (FREE_SSANAMES_QUEUE (fn), var);
618 /* If the alignment of the pointer described by PI is known, return true and
619 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
620 respectively. Otherwise return false. */
622 bool
623 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
624 unsigned int *misalignp)
626 if (pi->align)
628 *alignp = pi->align;
629 *misalignp = pi->misalign;
630 return true;
632 else
633 return false;
636 /* State that the pointer described by PI has unknown alignment. */
638 void
639 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
641 pi->align = 0;
642 pi->misalign = 0;
645 /* Store the power-of-two byte alignment and the deviation from that
646 alignment of pointer described by PI to ALIOGN and MISALIGN
647 respectively. */
649 void
650 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
651 unsigned int misalign)
653 gcc_checking_assert (align != 0);
654 gcc_assert ((align & (align - 1)) == 0);
655 gcc_assert ((misalign & ~(align - 1)) == 0);
657 pi->align = align;
658 pi->misalign = misalign;
661 /* If pointer described by PI has known alignment, increase its known
662 misalignment by INCREMENT modulo its current alignment. */
664 void
665 adjust_ptr_info_misalignment (struct ptr_info_def *pi, poly_uint64 increment)
667 if (pi->align != 0)
669 increment += pi->misalign;
670 if (!known_misalignment (increment, pi->align, &pi->misalign))
672 pi->align = known_alignment (increment);
673 pi->misalign = 0;
678 /* Return the alias information associated with pointer T. It creates a
679 new instance if none existed. */
681 struct ptr_info_def *
682 get_ptr_info (tree t)
684 struct ptr_info_def *pi;
686 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
688 pi = SSA_NAME_PTR_INFO (t);
689 if (pi == NULL)
691 pi = ggc_cleared_alloc<ptr_info_def> ();
692 pt_solution_reset (&pi->pt);
693 mark_ptr_info_alignment_unknown (pi);
694 SSA_NAME_PTR_INFO (t) = pi;
697 return pi;
701 /* Creates a new SSA name using the template NAME tobe defined by
702 statement STMT in function FN. */
704 tree
705 copy_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
707 tree new_name;
709 if (SSA_NAME_VAR (name))
710 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
711 else
713 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
714 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
717 return new_name;
721 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
722 the SSA name NAME. */
724 void
725 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
727 struct ptr_info_def *new_ptr_info;
729 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
730 gcc_assert (!SSA_NAME_PTR_INFO (name));
732 if (!ptr_info)
733 return;
735 new_ptr_info = ggc_alloc<ptr_info_def> ();
736 *new_ptr_info = *ptr_info;
738 SSA_NAME_PTR_INFO (name) = new_ptr_info;
741 void
742 duplicate_ssa_name_range_info (tree name, tree src)
744 gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (src)));
745 gcc_checking_assert (!range_info_p (name));
747 if (range_info_p (src))
749 Value_Range src_range (TREE_TYPE (src));
750 range_info_get_range (src, src_range);
751 range_info_set_range (name, src_range);
756 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
757 in function FN. */
759 tree
760 duplicate_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
762 tree new_name = copy_ssa_name_fn (fn, name, stmt);
763 if (POINTER_TYPE_P (TREE_TYPE (name)))
765 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
767 if (old_ptr_info)
768 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
770 else if (range_info_p (name))
771 duplicate_ssa_name_range_info (new_name, name);
773 return new_name;
777 /* Reset all flow sensitive data on NAME such as range-info, nonzero
778 bits and alignment. */
780 void
781 reset_flow_sensitive_info (tree name)
783 if (POINTER_TYPE_P (TREE_TYPE (name)))
785 /* points-to info is not flow-sensitive. */
786 if (SSA_NAME_PTR_INFO (name))
788 /* [E]VRP can derive context sensitive alignment info and
789 non-nullness properties. We must reset both. */
790 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
791 SSA_NAME_PTR_INFO (name)->pt.null = 1;
794 else
795 SSA_NAME_RANGE_INFO (name) = NULL;
798 /* Clear all flow sensitive data from all statements and PHI definitions
799 in BB. */
801 void
802 reset_flow_sensitive_info_in_bb (basic_block bb)
804 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
805 gsi_next (&gsi))
807 gimple *stmt = gsi_stmt (gsi);
808 ssa_op_iter i;
809 tree op;
810 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
811 reset_flow_sensitive_info (op);
814 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
815 gsi_next (&gsi))
817 tree phi_def = gimple_phi_result (gsi.phi ());
818 reset_flow_sensitive_info (phi_def);
822 /* Release all the SSA_NAMEs created by STMT. */
824 void
825 release_defs (gimple *stmt)
827 tree def;
828 ssa_op_iter iter;
830 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
831 if (TREE_CODE (def) == SSA_NAME)
832 release_ssa_name (def);
836 /* Replace the symbol associated with SSA_NAME with SYM. */
838 void
839 replace_ssa_name_symbol (tree ssa_name, tree sym)
841 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
842 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
845 /* Release the vector of free SSA_NAMEs and compact the vector of SSA_NAMEs
846 that are live. */
848 static void
849 release_free_names_and_compact_live_names (function *fun)
851 unsigned i, j;
852 int n = vec_safe_length (FREE_SSANAMES (fun));
854 /* Now release the freelist. */
855 vec_free (FREE_SSANAMES (fun));
857 /* And compact the SSA number space. We make sure to not change the
858 relative order of SSA versions. */
859 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
861 tree name = ssa_name (i);
862 if (name)
864 if (i != j)
866 SSA_NAME_VERSION (name) = j;
867 (*fun->gimple_df->ssa_names)[j] = name;
869 j++;
872 fun->gimple_df->ssa_names->truncate (j);
874 statistics_counter_event (fun, "SSA names released", n);
875 statistics_counter_event (fun, "SSA name holes removed", i - j);
876 if (dump_file)
877 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
878 n, n * 100.0 / num_ssa_names, i - j);
881 /* Return SSA names that are unused to GGC memory and compact the SSA
882 version namespace. This is used to keep footprint of compiler during
883 interprocedural optimization. */
885 namespace {
887 const pass_data pass_data_release_ssa_names =
889 GIMPLE_PASS, /* type */
890 "release_ssa", /* name */
891 OPTGROUP_NONE, /* optinfo_flags */
892 TV_TREE_SSA_OTHER, /* tv_id */
893 PROP_ssa, /* properties_required */
894 0, /* properties_provided */
895 0, /* properties_destroyed */
896 TODO_remove_unused_locals, /* todo_flags_start */
897 0, /* todo_flags_finish */
900 class pass_release_ssa_names : public gimple_opt_pass
902 public:
903 pass_release_ssa_names (gcc::context *ctxt)
904 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
907 /* opt_pass methods: */
908 unsigned int execute (function *) final override;
910 }; // class pass_release_ssa_names
912 unsigned int
913 pass_release_ssa_names::execute (function *fun)
915 release_free_names_and_compact_live_names (fun);
916 return 0;
919 } // anon namespace
921 gimple_opt_pass *
922 make_pass_release_ssa_names (gcc::context *ctxt)
924 return new pass_release_ssa_names (ctxt);