Update baseline symbols for hppa-linux.
[official-gcc.git] / gcc / tree-ssanames.cc
blob23387b90fe36477b2c69fb2eccb3c1dc5abb6ec7
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 /* Update the known bits of NAME.
470 Zero bits in MASK cover constant values. Set bits in MASK cover
471 unknown values. VALUE are the known bits. */
473 void
474 set_bitmask (tree name, const wide_int &value, const wide_int &mask)
476 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
478 int_range<2> r (TREE_TYPE (name));
479 r.update_bitmask (irange_bitmask (value, mask));
480 set_range_info (name, r);
483 /* Return a widest_int with potentially non-zero bits in SSA_NAME
484 NAME, the constant for INTEGER_CST, or -1 if unknown. */
486 wide_int
487 get_nonzero_bits (const_tree name)
489 if (TREE_CODE (name) == INTEGER_CST)
490 return wi::to_wide (name);
492 /* Use element_precision instead of TYPE_PRECISION so complex and
493 vector types get a non-zero precision. */
494 unsigned int precision = element_precision (TREE_TYPE (name));
495 if (POINTER_TYPE_P (TREE_TYPE (name)))
497 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
498 if (pi && pi->align)
499 return wi::shwi (-(HOST_WIDE_INT) pi->align
500 | (HOST_WIDE_INT) pi->misalign, precision);
501 return wi::shwi (-1, precision);
504 if (!range_info_p (name) || !irange::supports_p (TREE_TYPE (name)))
505 return wi::shwi (-1, precision);
507 int_range_max tmp;
508 range_info_get_range (name, tmp);
509 return tmp.get_nonzero_bits ();
512 /* Return TRUE is OP, an SSA_NAME has a range of values [0..1], false
513 otherwise.
515 This can be because it is a boolean type, any unsigned integral
516 type with a single bit of precision, or has known range of [0..1]
517 via range analysis. */
519 bool
520 ssa_name_has_boolean_range (tree op)
522 gcc_assert (TREE_CODE (op) == SSA_NAME);
524 /* Boolean types always have a range [0..1]. */
525 if (TREE_CODE (TREE_TYPE (op)) == BOOLEAN_TYPE)
526 return true;
528 /* An integral type with a single bit of precision. */
529 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
530 && TYPE_UNSIGNED (TREE_TYPE (op))
531 && TYPE_PRECISION (TREE_TYPE (op)) == 1)
532 return true;
534 /* An integral type with more precision, but the object
535 only takes on values [0..1] as determined by range
536 analysis. */
537 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
538 && (TYPE_PRECISION (TREE_TYPE (op)) > 1))
540 int_range<2> r;
541 if (get_range_query (cfun)->range_of_expr (r, op)
542 && r == range_true_and_false (TREE_TYPE (op)))
543 return true;
545 if (wi::eq_p (get_nonzero_bits (op), 1))
546 return true;
549 return false;
552 /* We no longer need the SSA_NAME expression VAR, release it so that
553 it may be reused.
555 Note it is assumed that no calls to make_ssa_name will be made
556 until all uses of the ssa name are released and that the only
557 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
558 other fields must be assumed clobbered. */
560 void
561 release_ssa_name_fn (struct function *fn, tree var)
563 if (!var)
564 return;
566 /* Never release the default definition for a symbol. It's a
567 special SSA name that should always exist once it's created. */
568 if (SSA_NAME_IS_DEFAULT_DEF (var))
569 return;
571 /* If VAR has been registered for SSA updating, don't remove it.
572 After update_ssa has run, the name will be released. */
573 if (name_registered_for_update_p (var))
575 release_ssa_name_after_update_ssa (var);
576 return;
579 /* release_ssa_name can be called multiple times on a single SSA_NAME.
580 However, it should only end up on our free list one time. We
581 keep a status bit in the SSA_NAME node itself to indicate it has
582 been put on the free list.
584 Note that once on the freelist you cannot reference the SSA_NAME's
585 defining statement. */
586 if (! SSA_NAME_IN_FREE_LIST (var))
588 int saved_ssa_name_version = SSA_NAME_VERSION (var);
589 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
591 if (MAY_HAVE_DEBUG_BIND_STMTS)
592 insert_debug_temp_for_var_def (NULL, var);
594 if (flag_checking)
595 verify_imm_links (stderr, var);
596 while (imm->next != imm)
597 delink_imm_use (imm->next);
599 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
600 memset (var, 0, tree_size (var));
602 imm->prev = imm;
603 imm->next = imm;
604 imm->loc.ssa_name = var;
606 /* First put back the right tree node so that the tree checking
607 macros do not complain. */
608 TREE_SET_CODE (var, SSA_NAME);
610 /* Restore the version number. */
611 SSA_NAME_VERSION (var) = saved_ssa_name_version;
613 /* Note this SSA_NAME is now in the first list. */
614 SSA_NAME_IN_FREE_LIST (var) = 1;
616 /* Put in a non-NULL TREE_TYPE so dumping code will not ICE
617 if it happens to come along a released SSA name and tries
618 to inspect its type. */
619 TREE_TYPE (var) = error_mark_node;
621 /* And finally queue it so that it will be put on the free list. */
622 vec_safe_push (FREE_SSANAMES_QUEUE (fn), var);
626 /* If the alignment of the pointer described by PI is known, return true and
627 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
628 respectively. Otherwise return false. */
630 bool
631 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
632 unsigned int *misalignp)
634 if (pi->align)
636 *alignp = pi->align;
637 *misalignp = pi->misalign;
638 return true;
640 else
641 return false;
644 /* State that the pointer described by PI has unknown alignment. */
646 void
647 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
649 pi->align = 0;
650 pi->misalign = 0;
653 /* Store the power-of-two byte alignment and the deviation from that
654 alignment of pointer described by PI to ALIOGN and MISALIGN
655 respectively. */
657 void
658 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
659 unsigned int misalign)
661 gcc_checking_assert (align != 0);
662 gcc_assert ((align & (align - 1)) == 0);
663 gcc_assert ((misalign & ~(align - 1)) == 0);
665 pi->align = align;
666 pi->misalign = misalign;
669 /* If pointer described by PI has known alignment, increase its known
670 misalignment by INCREMENT modulo its current alignment. */
672 void
673 adjust_ptr_info_misalignment (struct ptr_info_def *pi, poly_uint64 increment)
675 if (pi->align != 0)
677 increment += pi->misalign;
678 if (!known_misalignment (increment, pi->align, &pi->misalign))
680 pi->align = known_alignment (increment);
681 pi->misalign = 0;
686 /* Return the alias information associated with pointer T. It creates a
687 new instance if none existed. */
689 struct ptr_info_def *
690 get_ptr_info (tree t)
692 struct ptr_info_def *pi;
694 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
696 pi = SSA_NAME_PTR_INFO (t);
697 if (pi == NULL)
699 pi = ggc_cleared_alloc<ptr_info_def> ();
700 pt_solution_reset (&pi->pt);
701 mark_ptr_info_alignment_unknown (pi);
702 SSA_NAME_PTR_INFO (t) = pi;
705 return pi;
709 /* Creates a new SSA name using the template NAME tobe defined by
710 statement STMT in function FN. */
712 tree
713 copy_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
715 tree new_name;
717 if (SSA_NAME_VAR (name))
718 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
719 else
721 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
722 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
725 return new_name;
729 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
730 the SSA name NAME. */
732 void
733 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
735 struct ptr_info_def *new_ptr_info;
737 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
738 gcc_assert (!SSA_NAME_PTR_INFO (name));
740 if (!ptr_info)
741 return;
743 new_ptr_info = ggc_alloc<ptr_info_def> ();
744 *new_ptr_info = *ptr_info;
746 SSA_NAME_PTR_INFO (name) = new_ptr_info;
749 void
750 duplicate_ssa_name_range_info (tree name, tree src)
752 gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (src)));
753 gcc_checking_assert (!range_info_p (name));
755 if (range_info_p (src))
757 Value_Range src_range (TREE_TYPE (src));
758 range_info_get_range (src, src_range);
759 range_info_set_range (name, src_range);
764 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
765 in function FN. */
767 tree
768 duplicate_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
770 tree new_name = copy_ssa_name_fn (fn, name, stmt);
771 if (POINTER_TYPE_P (TREE_TYPE (name)))
773 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
775 if (old_ptr_info)
776 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
778 else if (range_info_p (name))
779 duplicate_ssa_name_range_info (new_name, name);
781 return new_name;
785 /* Reset all flow sensitive data on NAME such as range-info, nonzero
786 bits and alignment. */
788 void
789 reset_flow_sensitive_info (tree name)
791 if (POINTER_TYPE_P (TREE_TYPE (name)))
793 /* points-to info is not flow-sensitive. */
794 if (SSA_NAME_PTR_INFO (name))
796 /* [E]VRP can derive context sensitive alignment info and
797 non-nullness properties. We must reset both. */
798 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
799 SSA_NAME_PTR_INFO (name)->pt.null = 1;
802 else
803 SSA_NAME_RANGE_INFO (name) = NULL;
806 /* Clear all flow sensitive data from all statements and PHI definitions
807 in BB. */
809 void
810 reset_flow_sensitive_info_in_bb (basic_block bb)
812 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
813 gsi_next (&gsi))
815 gimple *stmt = gsi_stmt (gsi);
816 ssa_op_iter i;
817 tree op;
818 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
819 reset_flow_sensitive_info (op);
822 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
823 gsi_next (&gsi))
825 tree phi_def = gimple_phi_result (gsi.phi ());
826 reset_flow_sensitive_info (phi_def);
830 /* Release all the SSA_NAMEs created by STMT. */
832 void
833 release_defs (gimple *stmt)
835 tree def;
836 ssa_op_iter iter;
838 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
839 if (TREE_CODE (def) == SSA_NAME)
840 release_ssa_name (def);
844 /* Replace the symbol associated with SSA_NAME with SYM. */
846 void
847 replace_ssa_name_symbol (tree ssa_name, tree sym)
849 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
850 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
853 /* Release the vector of free SSA_NAMEs and compact the vector of SSA_NAMEs
854 that are live. */
856 static void
857 release_free_names_and_compact_live_names (function *fun)
859 unsigned i, j;
860 int n = vec_safe_length (FREE_SSANAMES (fun));
862 /* Now release the freelist. */
863 vec_free (FREE_SSANAMES (fun));
865 /* And compact the SSA number space. We make sure to not change the
866 relative order of SSA versions. */
867 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
869 tree name = ssa_name (i);
870 if (name)
872 if (i != j)
874 SSA_NAME_VERSION (name) = j;
875 (*fun->gimple_df->ssa_names)[j] = name;
877 j++;
880 fun->gimple_df->ssa_names->truncate (j);
882 statistics_counter_event (fun, "SSA names released", n);
883 statistics_counter_event (fun, "SSA name holes removed", i - j);
884 if (dump_file)
885 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
886 n, n * 100.0 / num_ssa_names, i - j);
889 /* Return SSA names that are unused to GGC memory and compact the SSA
890 version namespace. This is used to keep footprint of compiler during
891 interprocedural optimization. */
893 namespace {
895 const pass_data pass_data_release_ssa_names =
897 GIMPLE_PASS, /* type */
898 "release_ssa", /* name */
899 OPTGROUP_NONE, /* optinfo_flags */
900 TV_TREE_SSA_OTHER, /* tv_id */
901 PROP_ssa, /* properties_required */
902 0, /* properties_provided */
903 0, /* properties_destroyed */
904 TODO_remove_unused_locals, /* todo_flags_start */
905 0, /* todo_flags_finish */
908 class pass_release_ssa_names : public gimple_opt_pass
910 public:
911 pass_release_ssa_names (gcc::context *ctxt)
912 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
915 /* opt_pass methods: */
916 unsigned int execute (function *) final override;
918 }; // class pass_release_ssa_names
920 unsigned int
921 pass_release_ssa_names::execute (function *fun)
923 release_free_names_and_compact_live_names (fun);
924 return 0;
927 } // anon namespace
929 gimple_opt_pass *
930 make_pass_release_ssa_names (gcc::context *ctxt)
932 return new pass_release_ssa_names (ctxt);
935 /* Save and restore of flow sensitive information. */
937 /* Save off the flow sensitive info from NAME. */
939 void
940 flow_sensitive_info_storage::save (tree name)
942 gcc_assert (state == 0);
943 if (!POINTER_TYPE_P (TREE_TYPE (name)))
945 range_info = SSA_NAME_RANGE_INFO (name);
946 state = 1;
947 return;
949 state = -1;
950 auto ptr_info = SSA_NAME_PTR_INFO (name);
951 if (ptr_info)
953 align = ptr_info->align;
954 misalign = ptr_info->misalign;
955 null = SSA_NAME_PTR_INFO (name)->pt.null;
957 else
959 align = 0;
960 misalign = 0;
961 null = true;
965 /* Restore the flow sensitive info from NAME. */
967 void
968 flow_sensitive_info_storage::restore (tree name)
970 gcc_assert (state != 0);
971 if (!POINTER_TYPE_P (TREE_TYPE (name)))
973 gcc_assert (state == 1);
974 SSA_NAME_RANGE_INFO (name) = range_info;
975 return;
977 gcc_assert (state == -1);
978 auto ptr_info = SSA_NAME_PTR_INFO (name);
979 /* If there was no flow sensitive info on the pointer
980 just return, there is nothing to restore to. */
981 if (!ptr_info)
982 return;
983 if (align != 0)
984 set_ptr_info_alignment (ptr_info, align, misalign);
985 else
986 mark_ptr_info_alignment_unknown (ptr_info);
987 SSA_NAME_PTR_INFO (name)->pt.null = null;
990 /* Save off the flow sensitive info from NAME.
991 And reset the flow sensitive info of NAME. */
993 void
994 flow_sensitive_info_storage::save_and_clear (tree name)
996 save (name);
997 reset_flow_sensitive_info (name);
1000 /* Clear the storage. */
1001 void
1002 flow_sensitive_info_storage::clear_storage (void)
1004 state = 0;