Fix handling of ICF_NOVOPS in ipa-modref
[official-gcc.git] / gcc / tree-ssanames.cc
blob4f83fcbb51714d8a37102925c85179bba7fa6275
1 /* Generic routines for manipulating SSA_NAME expressions
2 Copyright (C) 2003-2024 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-pretty-print.h"
29 #include "gimple-iterator.h"
30 #include "stor-layout.h"
31 #include "tree-into-ssa.h"
32 #include "tree-ssa.h"
33 #include "cfgloop.h"
34 #include "tree-scalar-evolution.h"
35 #include "value-query.h"
36 #include "value-range-storage.h"
38 /* Rewriting a function into SSA form can create a huge number of SSA_NAMEs,
39 many of which may be thrown away shortly after their creation if jumps
40 were threaded through PHI nodes.
42 While our garbage collection mechanisms will handle this situation, it
43 is extremely wasteful to create nodes and throw them away, especially
44 when the nodes can be reused.
46 For PR 8361, we can significantly reduce the number of nodes allocated
47 and thus the total amount of memory allocated by managing SSA_NAMEs a
48 little. This additionally helps reduce the amount of work done by the
49 garbage collector. Similar results have been seen on a wider variety
50 of tests (such as the compiler itself).
52 Right now we maintain our free list on a per-function basis. It may
53 or may not make sense to maintain the free list for the duration of
54 a compilation unit.
56 External code should rely solely upon HIGHEST_SSA_VERSION and the
57 externally defined functions. External code should not know about
58 the details of the free list management.
60 External code should also not assume the version number on nodes is
61 monotonically increasing. We reuse the version number when we
62 reuse an SSA_NAME expression. This helps keep arrays and bitmaps
63 more compact. */
66 /* Version numbers with special meanings. We start allocating new version
67 numbers after the special ones. */
68 #define UNUSED_NAME_VERSION 0
70 unsigned int ssa_name_nodes_reused;
71 unsigned int ssa_name_nodes_created;
73 #define FREE_SSANAMES(fun) (fun)->gimple_df->free_ssanames
74 #define FREE_SSANAMES_QUEUE(fun) (fun)->gimple_df->free_ssanames_queue
76 /* Return TRUE if NAME has global range info. */
78 inline bool
79 range_info_p (const_tree name)
81 return SSA_NAME_RANGE_INFO (name);
84 /* Return TRUE if R fits in the global range of NAME. */
86 inline bool
87 range_info_fits_p (tree name, const vrange &r)
89 gcc_checking_assert (range_info_p (name));
90 vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
91 return mem->fits_p (r);
94 /* Allocate a new global range for NAME and set it to R. Return the
95 allocation slot. */
97 inline void *
98 range_info_alloc (tree name, const vrange &r)
100 vrange_storage *mem = ggc_alloc_vrange_storage (r);
101 SSA_NAME_RANGE_INFO (name) = mem;
102 return mem;
105 /* Free storage allocated for the global range for NAME. */
107 inline void
108 range_info_free (tree name)
110 vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
111 ggc_free (mem);
114 /* Return the global range for NAME in R. */
116 inline void
117 range_info_get_range (const_tree name, vrange &r)
119 SSA_NAME_RANGE_INFO (name)->get_vrange (r, TREE_TYPE (name));
122 /* Set the global range for NAME from R. Return TRUE if successfull,
123 or FALSE if we can't set a range of NAME's type. */
125 inline bool
126 range_info_set_range (tree name, const vrange &r)
128 if (!range_info_p (name) || !range_info_fits_p (name, r))
130 if (range_info_p (name))
131 range_info_free (name);
133 return range_info_alloc (name, r);
135 else
137 SSA_NAME_RANGE_INFO (name)->set_vrange (r);
138 return true;
142 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
143 zero use default. */
145 void
146 init_ssanames (struct function *fn, int size)
148 if (!size)
149 vec_alloc (SSANAMES (fn), 50);
150 else
151 vec_safe_reserve (SSANAMES (fn), size, true);
153 /* Version 0 is special, so reserve the first slot in the table. Though
154 currently unused, we may use version 0 in alias analysis as part of
155 the heuristics used to group aliases when the alias sets are too
156 large.
158 We use vec::quick_push here because we know that SSA_NAMES has at
159 least 50 elements reserved in it. */
160 SSANAMES (fn)->quick_push (NULL_TREE);
161 FREE_SSANAMES (fn) = NULL;
162 FREE_SSANAMES_QUEUE (fn) = NULL;
164 fn->gimple_df->ssa_renaming_needed = 0;
165 fn->gimple_df->rename_vops = 0;
168 /* Finalize management of SSA_NAMEs. */
170 void
171 fini_ssanames (struct function *fn)
173 unsigned i;
174 tree name;
175 /* Some SSA names leak into global tree data structures so we can't simply
176 ggc_free them. But make sure to clear references to stmts since we now
177 ggc_free the CFG itself. */
178 FOR_EACH_VEC_SAFE_ELT (SSANAMES (fn), i, name)
179 if (name)
180 SSA_NAME_DEF_STMT (name) = NULL;
181 vec_free (SSANAMES (fn));
182 vec_free (FREE_SSANAMES (fn));
183 vec_free (FREE_SSANAMES_QUEUE (fn));
186 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
188 void
189 ssanames_print_statistics (void)
191 fprintf (stderr, "%-32s" PRsa (11) "\n", "SSA_NAME nodes allocated:",
192 SIZE_AMOUNT (ssa_name_nodes_created));
193 fprintf (stderr, "%-32s" PRsa (11) "\n", "SSA_NAME nodes reused:",
194 SIZE_AMOUNT (ssa_name_nodes_reused));
197 /* Verify the state of the SSA_NAME lists.
199 There must be no duplicates on the free list.
200 Every name on the free list must be marked as on the free list.
201 Any name on the free list must not appear in the IL.
202 No names can be leaked. */
204 DEBUG_FUNCTION void
205 verify_ssaname_freelists (struct function *fun)
207 if (!gimple_in_ssa_p (fun))
208 return;
210 auto_bitmap names_in_il;
212 /* Walk the entire IL noting every SSA_NAME we see. */
213 basic_block bb;
214 FOR_EACH_BB_FN (bb, fun)
216 tree t;
217 /* First note the result and arguments of PHI nodes. */
218 for (gphi_iterator gsi = gsi_start_phis (bb);
219 !gsi_end_p (gsi);
220 gsi_next (&gsi))
222 gphi *phi = gsi.phi ();
223 t = gimple_phi_result (phi);
224 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
226 for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
228 t = gimple_phi_arg_def (phi, i);
229 if (TREE_CODE (t) == SSA_NAME)
230 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
234 /* Then note the operands of each statement. */
235 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
236 !gsi_end_p (gsi);
237 gsi_next (&gsi))
239 ssa_op_iter iter;
240 gimple *stmt = gsi_stmt (gsi);
241 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, SSA_OP_ALL_OPERANDS)
242 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
246 /* Now walk the free list noting what we find there and verifying
247 there are no duplicates. */
248 auto_bitmap names_in_freelists;
249 if (FREE_SSANAMES (fun))
251 for (unsigned int i = 0; i < FREE_SSANAMES (fun)->length (); i++)
253 tree t = (*FREE_SSANAMES (fun))[i];
255 /* Verify that the name is marked as being in the free list. */
256 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
258 /* Verify the name has not already appeared in the free list and
259 note it in the list of names found in the free list. */
260 gcc_assert (!bitmap_bit_p (names_in_freelists, SSA_NAME_VERSION (t)));
261 bitmap_set_bit (names_in_freelists, SSA_NAME_VERSION (t));
265 /* Similarly for the names in the pending free list. */
266 if (FREE_SSANAMES_QUEUE (fun))
268 for (unsigned int i = 0; i < FREE_SSANAMES_QUEUE (fun)->length (); i++)
270 tree t = (*FREE_SSANAMES_QUEUE (fun))[i];
272 /* Verify that the name is marked as being in the free list. */
273 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
275 /* Verify the name has not already appeared in the free list and
276 note it in the list of names found in the free list. */
277 gcc_assert (!bitmap_bit_p (names_in_freelists, SSA_NAME_VERSION (t)));
278 bitmap_set_bit (names_in_freelists, SSA_NAME_VERSION (t));
282 /* If any name appears in both the IL and the freelists, then
283 something horrible has happened. */
284 bool intersect_p = bitmap_intersect_p (names_in_il, names_in_freelists);
285 gcc_assert (!intersect_p);
287 /* Names can be queued up for release if there is an ssa update
288 pending. Pretend we saw them in the IL. */
289 if (names_to_release)
290 bitmap_ior_into (names_in_il, names_to_release);
292 /* Function splitting can "lose" SSA_NAMEs in an effort to ensure that
293 debug/non-debug compilations have the same SSA_NAMEs. So for each
294 lost SSA_NAME, see if it's likely one from that wart. These will always
295 be marked as default definitions. So we loosely assume that anything
296 marked as a default definition isn't leaked by pretending they are
297 in the IL. */
298 for (unsigned int i = UNUSED_NAME_VERSION + 1; i < num_ssa_names; i++)
299 if (ssa_name (i) && SSA_NAME_IS_DEFAULT_DEF (ssa_name (i)))
300 bitmap_set_bit (names_in_il, i);
302 unsigned int i;
303 bitmap_iterator bi;
304 auto_bitmap all_names;
305 bitmap_set_range (all_names, UNUSED_NAME_VERSION + 1, num_ssa_names - 1);
306 bitmap_ior_into (names_in_il, names_in_freelists);
308 /* Any name not mentioned in the IL and not in the feelists
309 has been leaked. */
310 EXECUTE_IF_AND_COMPL_IN_BITMAP(all_names, names_in_il,
311 UNUSED_NAME_VERSION + 1, i, bi)
312 gcc_assert (!ssa_name (i));
315 /* Move all SSA_NAMEs from FREE_SSA_NAMES_QUEUE to FREE_SSA_NAMES.
317 We do not, but should have a mode to verify the state of the SSA_NAMEs
318 lists. In particular at this point every name must be in the IL,
319 on the free list or in the queue. Anything else is an error. */
321 void
322 flush_ssaname_freelist (void)
324 /* If there were any SSA names released reset the SCEV cache. */
325 if (! vec_safe_is_empty (FREE_SSANAMES_QUEUE (cfun)))
326 scev_reset_htab ();
327 vec_safe_splice (FREE_SSANAMES (cfun), FREE_SSANAMES_QUEUE (cfun));
328 vec_safe_truncate (FREE_SSANAMES_QUEUE (cfun), 0);
331 /* Initialize SSA_NAME_IMM_USE_NODE of a SSA NAME. */
333 void
334 init_ssa_name_imm_use (tree name)
336 use_operand_p imm;
337 imm = &(SSA_NAME_IMM_USE_NODE (name));
338 imm->use = NULL;
339 imm->prev = imm;
340 imm->next = imm;
341 imm->loc.ssa_name = name;
344 /* Return an SSA_NAME node for variable VAR defined in statement STMT
345 in function FN. STMT may be an empty statement for artificial
346 references (e.g., default definitions created when a variable is
347 used without a preceding definition). If VERISON is not zero then
348 allocate the SSA name with that version. */
350 tree
351 make_ssa_name_fn (struct function *fn, tree var, gimple *stmt,
352 unsigned int version)
354 tree t;
355 gcc_assert (VAR_P (var)
356 || TREE_CODE (var) == PARM_DECL
357 || TREE_CODE (var) == RESULT_DECL
358 || (TYPE_P (var) && is_gimple_reg_type (var)));
360 /* Get the specified SSA name version. */
361 if (version != 0)
363 t = make_node (SSA_NAME);
364 SSA_NAME_VERSION (t) = version;
365 if (version >= SSANAMES (fn)->length ())
366 vec_safe_grow_cleared (SSANAMES (fn), version + 1, true);
367 gcc_assert ((*SSANAMES (fn))[version] == NULL);
368 (*SSANAMES (fn))[version] = t;
369 ssa_name_nodes_created++;
371 /* If our free list has an element, then use it. */
372 else if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
374 t = FREE_SSANAMES (fn)->pop ();
375 ssa_name_nodes_reused++;
377 /* The node was cleared out when we put it on the free list, so
378 there is no need to do so again here. */
379 gcc_assert ((*SSANAMES (fn))[SSA_NAME_VERSION (t)] == NULL);
380 (*SSANAMES (fn))[SSA_NAME_VERSION (t)] = t;
382 else
384 t = make_node (SSA_NAME);
385 SSA_NAME_VERSION (t) = SSANAMES (fn)->length ();
386 vec_safe_push (SSANAMES (fn), t);
387 ssa_name_nodes_created++;
390 if (TYPE_P (var))
392 TREE_TYPE (t) = TYPE_MAIN_VARIANT (var);
393 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
395 else
397 TREE_TYPE (t) = TREE_TYPE (var);
398 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
400 SSA_NAME_DEF_STMT (t) = stmt;
401 if (POINTER_TYPE_P (TREE_TYPE (t)))
402 SSA_NAME_PTR_INFO (t) = NULL;
403 else
404 SSA_NAME_RANGE_INFO (t) = NULL;
406 SSA_NAME_IN_FREE_LIST (t) = 0;
407 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
408 init_ssa_name_imm_use (t);
410 return t;
413 /* Update the range information for NAME, intersecting into an existing
414 range if applicable. Return TRUE if the range was updated. */
416 bool
417 set_range_info (tree name, const vrange &r)
419 if (r.undefined_p () || r.varying_p ())
420 return false;
422 // Pick up the current range, or VARYING if none.
423 tree type = TREE_TYPE (name);
424 if (POINTER_TYPE_P (type))
426 struct ptr_info_def *pi = get_ptr_info (name);
427 // If R is nonnull and pi is not, set nonnull.
428 if (r.nonzero_p () && (!pi || pi->pt.null))
429 set_ptr_nonnull (name);
430 else
431 return false;
433 else
435 value_range tmp (type);
436 if (range_info_p (name))
437 range_info_get_range (name, tmp);
438 else
439 tmp.set_varying (type);
440 // If the result doesn't change, or is undefined, return false.
441 if (!tmp.intersect (r) || tmp.undefined_p ())
442 return false;
443 if (!range_info_set_range (name, tmp))
444 return false;
446 if (dump_file)
448 value_range tmp (type);
449 fprintf (dump_file, "Global Exported: ");
450 print_generic_expr (dump_file, name, TDF_SLIM);
451 fprintf (dump_file, " = ");
452 gimple_range_global (tmp, name);
453 tmp.dump (dump_file);
454 fputc ('\n', dump_file);
456 return true;
459 /* Set nonnull attribute to pointer NAME. */
461 void
462 set_ptr_nonnull (tree name)
464 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
465 struct ptr_info_def *pi = get_ptr_info (name);
466 pi->pt.null = 0;
469 /* Update the non-zero bits bitmask of NAME. */
471 void
472 set_nonzero_bits (tree name, const wide_int &mask)
474 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
476 int_range<2> r (TREE_TYPE (name));
477 r.set_nonzero_bits (mask);
478 set_range_info (name, r);
481 /* Update the known bits of NAME.
483 Zero bits in MASK cover constant values. Set bits in MASK cover
484 unknown values. VALUE are the known bits. */
486 void
487 set_bitmask (tree name, const wide_int &value, const wide_int &mask)
489 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
491 int_range<2> r (TREE_TYPE (name));
492 r.update_bitmask (irange_bitmask (value, mask));
493 set_range_info (name, r);
496 /* Return a widest_int with potentially non-zero bits in SSA_NAME
497 NAME, the constant for INTEGER_CST, or -1 if unknown. */
499 wide_int
500 get_nonzero_bits (const_tree name)
502 if (TREE_CODE (name) == INTEGER_CST)
503 return wi::to_wide (name);
505 /* Use element_precision instead of TYPE_PRECISION so complex and
506 vector types get a non-zero precision. */
507 unsigned int precision = element_precision (TREE_TYPE (name));
508 if (POINTER_TYPE_P (TREE_TYPE (name)))
510 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
511 if (pi && pi->align)
512 return wi::shwi (-(HOST_WIDE_INT) pi->align
513 | (HOST_WIDE_INT) pi->misalign, precision);
514 return wi::shwi (-1, precision);
517 if (!range_info_p (name) || !irange::supports_p (TREE_TYPE (name)))
518 return wi::shwi (-1, precision);
520 int_range_max tmp;
521 range_info_get_range (name, tmp);
522 return tmp.get_nonzero_bits ();
525 /* Return TRUE is OP, an SSA_NAME has a range of values [0..1], false
526 otherwise.
528 This can be because it is a boolean type, any unsigned integral
529 type with a single bit of precision, or has known range of [0..1]
530 via range analysis. */
532 bool
533 ssa_name_has_boolean_range (tree op)
535 gcc_assert (TREE_CODE (op) == SSA_NAME);
537 /* An integral type with a single bit of precision. */
538 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
539 && TYPE_UNSIGNED (TREE_TYPE (op))
540 && TYPE_PRECISION (TREE_TYPE (op)) == 1)
541 return true;
543 /* An integral type with more precision, but the object
544 only takes on values [0..1] as determined by range
545 analysis. */
546 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
547 && (TYPE_PRECISION (TREE_TYPE (op)) > 1))
549 int_range<2> r;
550 if (get_range_query (cfun)->range_of_expr (r, op)
551 && r == range_true_and_false (TREE_TYPE (op)))
552 return true;
554 if (wi::eq_p (get_nonzero_bits (op), 1))
555 return true;
558 return false;
561 /* We no longer need the SSA_NAME expression VAR, release it so that
562 it may be reused.
564 Note it is assumed that no calls to make_ssa_name will be made
565 until all uses of the ssa name are released and that the only
566 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
567 other fields must be assumed clobbered. */
569 void
570 release_ssa_name_fn (struct function *fn, tree var)
572 if (!var)
573 return;
575 /* Never release the default definition for a symbol. It's a
576 special SSA name that should always exist once it's created. */
577 if (SSA_NAME_IS_DEFAULT_DEF (var))
578 return;
580 /* If VAR has been registered for SSA updating, don't remove it.
581 After update_ssa has run, the name will be released. */
582 if (name_registered_for_update_p (var))
584 release_ssa_name_after_update_ssa (var);
585 return;
588 /* release_ssa_name can be called multiple times on a single SSA_NAME.
589 However, it should only end up on our free list one time. We
590 keep a status bit in the SSA_NAME node itself to indicate it has
591 been put on the free list.
593 Note that once on the freelist you cannot reference the SSA_NAME's
594 defining statement. */
595 if (! SSA_NAME_IN_FREE_LIST (var))
597 int saved_ssa_name_version = SSA_NAME_VERSION (var);
598 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
600 if (MAY_HAVE_DEBUG_BIND_STMTS)
601 insert_debug_temp_for_var_def (NULL, var);
603 if (flag_checking)
604 verify_imm_links (stderr, var);
605 while (imm->next != imm)
606 delink_imm_use (imm->next);
608 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
609 memset (var, 0, tree_size (var));
611 imm->prev = imm;
612 imm->next = imm;
613 imm->loc.ssa_name = var;
615 /* First put back the right tree node so that the tree checking
616 macros do not complain. */
617 TREE_SET_CODE (var, SSA_NAME);
619 /* Restore the version number. */
620 SSA_NAME_VERSION (var) = saved_ssa_name_version;
622 /* Note this SSA_NAME is now in the first list. */
623 SSA_NAME_IN_FREE_LIST (var) = 1;
625 /* Put in a non-NULL TREE_TYPE so dumping code will not ICE
626 if it happens to come along a released SSA name and tries
627 to inspect its type. */
628 TREE_TYPE (var) = error_mark_node;
630 /* And finally queue it so that it will be put on the free list. */
631 vec_safe_push (FREE_SSANAMES_QUEUE (fn), var);
635 /* If the alignment of the pointer described by PI is known, return true and
636 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
637 respectively. Otherwise return false. */
639 bool
640 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
641 unsigned int *misalignp)
643 if (pi->align)
645 *alignp = pi->align;
646 *misalignp = pi->misalign;
647 return true;
649 else
650 return false;
653 /* State that the pointer described by PI has unknown alignment. */
655 void
656 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
658 pi->align = 0;
659 pi->misalign = 0;
662 /* Store the power-of-two byte alignment and the deviation from that
663 alignment of pointer described by PI to ALIOGN and MISALIGN
664 respectively. */
666 void
667 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
668 unsigned int misalign)
670 gcc_checking_assert (align != 0);
671 gcc_assert ((align & (align - 1)) == 0);
672 gcc_assert ((misalign & ~(align - 1)) == 0);
674 pi->align = align;
675 pi->misalign = misalign;
678 /* If pointer described by PI has known alignment, increase its known
679 misalignment by INCREMENT modulo its current alignment. */
681 void
682 adjust_ptr_info_misalignment (struct ptr_info_def *pi, poly_uint64 increment)
684 if (pi->align != 0)
686 increment += pi->misalign;
687 if (!known_misalignment (increment, pi->align, &pi->misalign))
689 pi->align = known_alignment (increment);
690 pi->misalign = 0;
695 /* Return the alias information associated with pointer T. It creates a
696 new instance if none existed. */
698 struct ptr_info_def *
699 get_ptr_info (tree t)
701 struct ptr_info_def *pi;
703 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
705 pi = SSA_NAME_PTR_INFO (t);
706 if (pi == NULL)
708 pi = ggc_cleared_alloc<ptr_info_def> ();
709 pt_solution_reset (&pi->pt);
710 mark_ptr_info_alignment_unknown (pi);
711 SSA_NAME_PTR_INFO (t) = pi;
714 return pi;
718 /* Creates a new SSA name using the template NAME tobe defined by
719 statement STMT in function FN. */
721 tree
722 copy_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
724 tree new_name;
726 if (SSA_NAME_VAR (name))
727 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
728 else
730 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
731 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
734 return new_name;
738 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
739 the SSA name NAME. */
741 void
742 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
744 struct ptr_info_def *new_ptr_info;
746 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
747 gcc_assert (!SSA_NAME_PTR_INFO (name));
749 if (!ptr_info)
750 return;
752 new_ptr_info = ggc_alloc<ptr_info_def> ();
753 *new_ptr_info = *ptr_info;
755 SSA_NAME_PTR_INFO (name) = new_ptr_info;
758 void
759 duplicate_ssa_name_range_info (tree name, tree src)
761 gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (src)));
762 gcc_checking_assert (!range_info_p (name));
764 if (range_info_p (src))
766 value_range src_range (TREE_TYPE (src));
767 range_info_get_range (src, src_range);
768 range_info_set_range (name, src_range);
772 /* For a SSA copy DEST = SRC duplicate SSA info present on DEST to SRC
773 to preserve it in case DEST is eliminated to SRC. */
775 void
776 maybe_duplicate_ssa_info_at_copy (tree dest, tree src)
778 /* While points-to info is flow-insensitive we have to avoid copying
779 info from not executed regions invoking UB to dominating defs. */
780 if (gimple_bb (SSA_NAME_DEF_STMT (src))
781 != gimple_bb (SSA_NAME_DEF_STMT (dest)))
782 return;
784 if (POINTER_TYPE_P (TREE_TYPE (dest))
785 && SSA_NAME_PTR_INFO (dest)
786 && ! SSA_NAME_PTR_INFO (src))
787 duplicate_ssa_name_ptr_info (src, SSA_NAME_PTR_INFO (dest));
788 else if (INTEGRAL_TYPE_P (TREE_TYPE (dest))
789 && SSA_NAME_RANGE_INFO (dest)
790 && ! SSA_NAME_RANGE_INFO (src))
791 duplicate_ssa_name_range_info (src, dest);
795 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
796 in function FN. */
798 tree
799 duplicate_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
801 tree new_name = copy_ssa_name_fn (fn, name, stmt);
802 if (POINTER_TYPE_P (TREE_TYPE (name)))
804 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
806 if (old_ptr_info)
807 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
809 else if (range_info_p (name))
810 duplicate_ssa_name_range_info (new_name, name);
812 return new_name;
816 /* Reset all flow sensitive data on NAME such as range-info, nonzero
817 bits and alignment. */
819 void
820 reset_flow_sensitive_info (tree name)
822 if (POINTER_TYPE_P (TREE_TYPE (name)))
824 /* points-to info is not flow-sensitive. */
825 if (SSA_NAME_PTR_INFO (name))
827 /* [E]VRP can derive context sensitive alignment info and
828 non-nullness properties. We must reset both. */
829 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
830 SSA_NAME_PTR_INFO (name)->pt.null = 1;
833 else
834 SSA_NAME_RANGE_INFO (name) = NULL;
837 /* Clear all flow sensitive data from all statements and PHI definitions
838 in BB. */
840 void
841 reset_flow_sensitive_info_in_bb (basic_block bb)
843 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
844 gsi_next (&gsi))
846 gimple *stmt = gsi_stmt (gsi);
847 ssa_op_iter i;
848 tree op;
849 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
850 reset_flow_sensitive_info (op);
853 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
854 gsi_next (&gsi))
856 tree phi_def = gimple_phi_result (gsi.phi ());
857 reset_flow_sensitive_info (phi_def);
861 /* Release all the SSA_NAMEs created by STMT. */
863 void
864 release_defs (gimple *stmt)
866 tree def;
867 ssa_op_iter iter;
869 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
870 if (TREE_CODE (def) == SSA_NAME)
871 release_ssa_name (def);
875 /* Replace the symbol associated with SSA_NAME with SYM. */
877 void
878 replace_ssa_name_symbol (tree ssa_name, tree sym)
880 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
881 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
884 /* Release the vector of free SSA_NAMEs and compact the vector of SSA_NAMEs
885 that are live. */
887 static void
888 release_free_names_and_compact_live_names (function *fun)
890 unsigned i, j;
891 int n = vec_safe_length (FREE_SSANAMES (fun));
893 /* Now release the freelist. */
894 vec_free (FREE_SSANAMES (fun));
896 /* And compact the SSA number space. We make sure to not change the
897 relative order of SSA versions. */
898 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
900 tree name = ssa_name (i);
901 if (name)
903 if (i != j)
905 SSA_NAME_VERSION (name) = j;
906 (*fun->gimple_df->ssa_names)[j] = name;
908 j++;
911 fun->gimple_df->ssa_names->truncate (j);
913 statistics_counter_event (fun, "SSA names released", n);
914 statistics_counter_event (fun, "SSA name holes removed", i - j);
915 if (dump_file)
916 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
917 n, n * 100.0 / num_ssa_names, i - j);
920 /* Return SSA names that are unused to GGC memory and compact the SSA
921 version namespace. This is used to keep footprint of compiler during
922 interprocedural optimization. */
924 namespace {
926 const pass_data pass_data_release_ssa_names =
928 GIMPLE_PASS, /* type */
929 "release_ssa", /* name */
930 OPTGROUP_NONE, /* optinfo_flags */
931 TV_TREE_SSA_OTHER, /* tv_id */
932 PROP_ssa, /* properties_required */
933 0, /* properties_provided */
934 0, /* properties_destroyed */
935 TODO_remove_unused_locals, /* todo_flags_start */
936 0, /* todo_flags_finish */
939 class pass_release_ssa_names : public gimple_opt_pass
941 public:
942 pass_release_ssa_names (gcc::context *ctxt)
943 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
946 /* opt_pass methods: */
947 unsigned int execute (function *) final override;
949 }; // class pass_release_ssa_names
951 unsigned int
952 pass_release_ssa_names::execute (function *fun)
954 release_free_names_and_compact_live_names (fun);
955 return 0;
958 } // anon namespace
960 gimple_opt_pass *
961 make_pass_release_ssa_names (gcc::context *ctxt)
963 return new pass_release_ssa_names (ctxt);
966 /* Save and restore of flow sensitive information. */
968 /* Save off the flow sensitive info from NAME. */
970 void
971 flow_sensitive_info_storage::save (tree name)
973 gcc_assert (state == 0);
974 if (!POINTER_TYPE_P (TREE_TYPE (name)))
976 range_info = SSA_NAME_RANGE_INFO (name);
977 state = 1;
978 return;
980 state = -1;
981 auto ptr_info = SSA_NAME_PTR_INFO (name);
982 if (ptr_info)
984 align = ptr_info->align;
985 misalign = ptr_info->misalign;
986 null = SSA_NAME_PTR_INFO (name)->pt.null;
988 else
990 align = 0;
991 misalign = 0;
992 null = true;
996 /* Restore the flow sensitive info from NAME. */
998 void
999 flow_sensitive_info_storage::restore (tree name)
1001 gcc_assert (state != 0);
1002 if (!POINTER_TYPE_P (TREE_TYPE (name)))
1004 gcc_assert (state == 1);
1005 SSA_NAME_RANGE_INFO (name) = range_info;
1006 return;
1008 gcc_assert (state == -1);
1009 auto ptr_info = SSA_NAME_PTR_INFO (name);
1010 /* If there was no flow sensitive info on the pointer
1011 just return, there is nothing to restore to. */
1012 if (!ptr_info)
1013 return;
1014 if (align != 0)
1015 set_ptr_info_alignment (ptr_info, align, misalign);
1016 else
1017 mark_ptr_info_alignment_unknown (ptr_info);
1018 SSA_NAME_PTR_INFO (name)->pt.null = null;
1021 /* Save off the flow sensitive info from NAME.
1022 And reset the flow sensitive info of NAME. */
1024 void
1025 flow_sensitive_info_storage::save_and_clear (tree name)
1027 save (name);
1028 reset_flow_sensitive_info (name);
1031 /* Clear the storage. */
1032 void
1033 flow_sensitive_info_storage::clear_storage (void)
1035 state = 0;