Debian Codegraph @

gcc/tree-ssanames.cc

raw
1 /* Generic routines for manipulating SSA_NAME expressions
2 Copyright (C) 2003-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
37
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.
41
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.
45
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).
51
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.
55
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.
59
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. */
64
65
66 /* Version numbers with special meanings. We start allocating new version
67 numbers after the special ones. */
68 #define UNUSED_NAME_VERSION 0
69
70 unsigned int ssa_name_nodes_reused;
71 unsigned int ssa_name_nodes_created;
72
73 #define FREE_SSANAMES(fun) (fun)->gimple_df->free_ssanames
74 #define FREE_SSANAMES_QUEUE(fun) (fun)->gimple_df->free_ssanames_queue
75
76 /* Return TRUE if NAME has global range info. */
77
78 inline bool
79 range_info_p (const_tree name)
80 {
81 return SSA_NAME_RANGE_INFO (name);
82 }
83
84 /* Return TRUE if R fits in the global range of NAME. */
85
86 inline bool
87 range_info_fits_p (tree name, const vrange &r)
88 {
89 gcc_checking_assert (range_info_p (name));
90 vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
91 return mem->fits_p (r);
92 }
93
94 /* Allocate a new global range for NAME and set it to R. Return the
95 allocation slot. */
96
97 inline void *
98 range_info_alloc (tree name, const vrange &r)
99 {
100 vrange_storage *mem = ggc_alloc_vrange_storage (r);
101 SSA_NAME_RANGE_INFO (name) = mem;
102 return mem;
103 }
104
105 /* Free storage allocated for the global range for NAME. */
106
107 inline void
108 range_info_free (tree name)
109 {
110 vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
111 ggc_free (mem);
112 }
113
114 /* Return the global range for NAME in R. */
115
116 inline void
117 range_info_get_range (const_tree name, vrange &r)
118 {
119 SSA_NAME_RANGE_INFO (name)->get_vrange (r, TREE_TYPE (name));
120 }
121
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. */
124
125 inline bool
126 range_info_set_range (tree name, const vrange &r)
127 {
128 if (!range_info_p (name) || !range_info_fits_p (name, r))
129 {
130 if (range_info_p (name))
131 range_info_free (name);
132
133 return range_info_alloc (name, r);
134 }
135 else
136 {
137 SSA_NAME_RANGE_INFO (name)->set_vrange (r);
138 return true;
139 }
140 }
141
142 /* Initialize management of SSA_NAMEs to default SIZE. If SIZE is
143 zero use default. */
144
145 void
146 init_ssanames (struct function *fn, int size)
147 {
148 if (!size)
149 vec_alloc (SSANAMES (fn), 50);
150 else
151 vec_safe_reserve (SSANAMES (fn), size, true);
152
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.
157
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;
163
164 fn->gimple_df->ssa_renaming_needed = 0;
165 fn->gimple_df->rename_vops = 0;
166 }
167
168 /* Finalize management of SSA_NAMEs. */
169
170 void
171 fini_ssanames (struct function *fn)
172 {
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));
184 }
185
186 /* Dump some simple statistics regarding the re-use of SSA_NAME nodes. */
187
188 void
189 ssanames_print_statistics (void)
190 {
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));
195 }
196
197 /* Verify the state of the SSA_NAME lists.
198
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. */
203
204 DEBUG_FUNCTION void
205 verify_ssaname_freelists (struct function *fun)
206 {
207 if (!gimple_in_ssa_p (fun))
208 return;
209
210 auto_bitmap names_in_il;
211
212 /* Walk the entire IL noting every SSA_NAME we see. */
213 basic_block bb;
214 FOR_EACH_BB_FN (bb, fun)
215 {
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))
221 {
222 gphi *phi = gsi.phi ();
223 t = gimple_phi_result (phi);
224 bitmap_set_bit (names_in_il, SSA_NAME_VERSION (t));
225
226 for (unsigned int i = 0; i < gimple_phi_num_args (phi); i++)
227 {
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));
231 }
232 }
233
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))
238 {
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));
243 }
244 }
245
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))
250 {
251 for (unsigned int i = 0; i < FREE_SSANAMES (fun)->length (); i++)
252 {
253 tree t = (*FREE_SSANAMES (fun))[i];
254
255 /* Verify that the name is marked as being in the free list. */
256 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
257
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));
262 }
263 }
264
265 /* Similarly for the names in the pending free list. */
266 if (FREE_SSANAMES_QUEUE (fun))
267 {
268 for (unsigned int i = 0; i < FREE_SSANAMES_QUEUE (fun)->length (); i++)
269 {
270 tree t = (*FREE_SSANAMES_QUEUE (fun))[i];
271
272 /* Verify that the name is marked as being in the free list. */
273 gcc_assert (SSA_NAME_IN_FREE_LIST (t));
274
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));
279 }
280 }
281
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);
286
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);
291
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);
301
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);
307
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));
313 }
314
315 /* Move all SSA_NAMEs from FREE_SSA_NAMES_QUEUE to FREE_SSA_NAMES.
316
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. */
320
321 void
322 flush_ssaname_freelist (void)
323 {
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);
329 }
330
331 /* Initialize SSA_NAME_IMM_USE_NODE of a SSA NAME. */
332
333 void
334 init_ssa_name_imm_use (tree name)
335 {
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;
342 }
343
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. */
349
350 tree
351 make_ssa_name_fn (struct function *fn, tree var, gimple *stmt,
352 unsigned int version)
353 {
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)));
359
360 /* Get the specified SSA name version. */
361 if (version != 0)
362 {
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++;
370 }
371 /* If our free list has an element, then use it. */
372 else if (!vec_safe_is_empty (FREE_SSANAMES (fn)))
373 {
374 t = FREE_SSANAMES (fn)->pop ();
375 ssa_name_nodes_reused++;
376
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;
381 }
382 else
383 {
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++;
388 }
389
390 if (TYPE_P (var))
391 {
392 TREE_TYPE (t) = TYPE_MAIN_VARIANT (var);
393 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, NULL_TREE);
394 }
395 else
396 {
397 TREE_TYPE (t) = TREE_TYPE (var);
398 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
399 }
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;
405
406 SSA_NAME_IN_FREE_LIST (t) = 0;
407 SSA_NAME_IS_DEFAULT_DEF (t) = 0;
408 init_ssa_name_imm_use (t);
409
410 return t;
411 }
412
413 /* Update the range information for NAME, intersecting into an existing
414 range if applicable. Return TRUE if the range was updated. */
415
416 bool
417 set_range_info (tree name, const vrange &r)
418 {
419 if (r.undefined_p () || r.varying_p ())
420 return false;
421
422 // Pick up the current range, or VARYING if none.
423 tree type = TREE_TYPE (name);
424 if (POINTER_TYPE_P (type))
425 {
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;
432 }
433 else
434 {
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;
445 }
446 if (dump_file)
447 {
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);
455 }
456 return true;
457 }
458
459 /* Set nonnull attribute to pointer NAME. */
460
461 void
462 set_ptr_nonnull (tree name)
463 {
464 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
465 struct ptr_info_def *pi = get_ptr_info (name);
466 pi->pt.null = 0;
467 }
468
469 /* Update the non-zero bits bitmask of NAME. */
470
471 void
472 set_nonzero_bits (tree name, const wide_int &mask)
473 {
474 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
475
476 int_range<2> r (TREE_TYPE (name));
477 r.set_nonzero_bits (mask);
478 set_range_info (name, r);
479 }
480
481 /* Update the known bits of NAME.
482
483 Zero bits in MASK cover constant values. Set bits in MASK cover
484 unknown values. VALUE are the known bits. */
485
486 void
487 set_bitmask (tree name, const wide_int &value, const wide_int &mask)
488 {
489 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (name)));
490
491 int_range<2> r (TREE_TYPE (name));
492 r.update_bitmask (irange_bitmask (value, mask));
493 set_range_info (name, r);
494 }
495
496 /* Return a wide_int with potentially non-zero bits in SSA_NAME
497 NAME, the constant for INTEGER_CST, or -1 if unknown. */
498
499 static wide_int
500 get_nonzero_bits_1 (const_tree name)
501 {
502 if (TREE_CODE (name) == INTEGER_CST)
503 return wi::to_wide (name);
504
505 if (POLY_INT_CST_P (name))
506 return -known_alignment (wi::to_poly_wide (name));
507
508 /* Use element_precision instead of TYPE_PRECISION so complex and
509 vector types get a non-zero precision. */
510 unsigned int precision = element_precision (TREE_TYPE (name));
511 if (TREE_CODE (name) != SSA_NAME)
512 return wi::shwi (-1, precision);
513
514 if (POINTER_TYPE_P (TREE_TYPE (name)))
515 {
516 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
517 if (pi && pi->align)
518 return wi::shwi (-(HOST_WIDE_INT) pi->align
519 | (HOST_WIDE_INT) pi->misalign, precision);
520 return wi::shwi (-1, precision);
521 }
522
523 if (!range_info_p (name) || !irange::supports_p (TREE_TYPE (name)))
524 return wi::shwi (-1, precision);
525
526 int_range_max tmp;
527 range_info_get_range (name, tmp);
528 return tmp.get_nonzero_bits ();
529 }
530
531 /* Return a wide_int with potentially non-zero bits in SSA_NAME
532 NAME, the constant for INTEGER_CST, or -1 if unknown.
533 In addition to what get_nonzero_bits_1 handles, this handles one
534 level of BIT_AND_EXPR, either as a def_stmt or tree directly. */
535
536 wide_int
537 get_nonzero_bits (const_tree name)
538 {
539 if (TREE_CODE (name) == BIT_AND_EXPR)
540 return (get_nonzero_bits_1 (TREE_OPERAND (name, 0))
541 & get_nonzero_bits_1 (TREE_OPERAND (name, 1)));
542 if (TREE_CODE (name) == SSA_NAME)
543 {
544 gimple *g = SSA_NAME_DEF_STMT (name);
545 if (g
546 && is_gimple_assign (g)
547 && gimple_assign_rhs_code (g) == BIT_AND_EXPR)
548 return (get_nonzero_bits_1 (name)
549 & get_nonzero_bits_1 (gimple_assign_rhs1 (g))
550 & get_nonzero_bits_1 (gimple_assign_rhs2 (g)));
551 }
552 return get_nonzero_bits_1 (name);
553 }
554
555 /* Return a wide_int with known non-zero bits in SSA_NAME
556 NAME (bits whose values aren't known are also clear), the constant
557 for INTEGER_CST, or 0 if unknown. */
558
559 static wide_int
560 get_known_nonzero_bits_1 (const_tree name)
561 {
562 if (TREE_CODE (name) == INTEGER_CST)
563 return wi::to_wide (name);
564
565 /* Use element_precision instead of TYPE_PRECISION so complex and
566 vector types get a non-zero precision. */
567 unsigned int precision = element_precision (TREE_TYPE (name));
568 if (TREE_CODE (name) != SSA_NAME || POINTER_TYPE_P (TREE_TYPE (name)))
569 return wi::shwi (0, precision);
570
571 if (!range_info_p (name) || !irange::supports_p (TREE_TYPE (name)))
572 return wi::shwi (0, precision);
573
574 int_range_max tmp;
575 range_info_get_range (name, tmp);
576 if (tmp.undefined_p ())
577 return wi::shwi (0, precision);
578 irange_bitmask bm = tmp.get_bitmask ();
579 return bm.value () & ~bm.mask ();
580 }
581
582 /* Return a wide_int with known non-zero bits in SSA_NAME
583 NAME, the constant for INTEGER_CST, or -1 if unknown.
584 In addition to what get_known_nonzero_bits_1 handles, this handles one
585 level of BIT_IOR_EXPR, either as a def_stmt or tree directly. */
586
587 wide_int
588 get_known_nonzero_bits (const_tree name)
589 {
590 if (TREE_CODE (name) == BIT_IOR_EXPR)
591 return (get_known_nonzero_bits_1 (TREE_OPERAND (name, 0))
592 | get_known_nonzero_bits_1 (TREE_OPERAND (name, 1)));
593 if (TREE_CODE (name) == SSA_NAME)
594 {
595 gimple *g = SSA_NAME_DEF_STMT (name);
596 if (g
597 && is_gimple_assign (g)
598 && gimple_assign_rhs_code (g) == BIT_IOR_EXPR)
599 return (get_known_nonzero_bits_1 (name)
600 | get_known_nonzero_bits_1 (gimple_assign_rhs1 (g))
601 | get_known_nonzero_bits_1 (gimple_assign_rhs2 (g)));
602 }
603 return get_known_nonzero_bits_1 (name);
604 }
605
606 /* Return TRUE is OP, an SSA_NAME has a range of values [0..1], false
607 otherwise.
608
609 This can be because it is a boolean type, any unsigned integral
610 type with a single bit of precision, or has known range of [0..1]
611 via range analysis. */
612
613 bool
614 ssa_name_has_boolean_range (tree op)
615 {
616 gcc_assert (TREE_CODE (op) == SSA_NAME);
617
618 /* An integral type with a single bit of precision. */
619 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
620 && TYPE_UNSIGNED (TREE_TYPE (op))
621 && TYPE_PRECISION (TREE_TYPE (op)) == 1)
622 return true;
623
624 /* An integral type with more precision, but the object
625 only takes on values [0..1] as determined by range
626 analysis. */
627 if (INTEGRAL_TYPE_P (TREE_TYPE (op))
628 && (TYPE_PRECISION (TREE_TYPE (op)) > 1))
629 {
630 int_range<2> r;
631 if (get_range_query (cfun)->range_of_expr (r, op)
632 && r == range_true_and_false (TREE_TYPE (op)))
633 return true;
634
635 if (wi::eq_p (get_nonzero_bits (op), 1))
636 return true;
637 }
638
639 return false;
640 }
641
642 /* We no longer need the SSA_NAME expression VAR, release it so that
643 it may be reused.
644
645 Note it is assumed that no calls to make_ssa_name will be made
646 until all uses of the ssa name are released and that the only
647 use of the SSA_NAME expression is to check its SSA_NAME_VAR. All
648 other fields must be assumed clobbered. */
649
650 void
651 release_ssa_name_fn (struct function *fn, tree var)
652 {
653 if (!var)
654 return;
655
656 /* Never release the default definition for a symbol. It's a
657 special SSA name that should always exist once it's created. */
658 if (SSA_NAME_IS_DEFAULT_DEF (var))
659 return;
660
661 /* If VAR has been registered for SSA updating, don't remove it.
662 After update_ssa has run, the name will be released. */
663 if (name_registered_for_update_p (var))
664 {
665 release_ssa_name_after_update_ssa (var);
666 return;
667 }
668
669 /* release_ssa_name can be called multiple times on a single SSA_NAME.
670 However, it should only end up on our free list one time. We
671 keep a status bit in the SSA_NAME node itself to indicate it has
672 been put on the free list.
673
674 Note that once on the freelist you cannot reference the SSA_NAME's
675 defining statement. */
676 if (! SSA_NAME_IN_FREE_LIST (var))
677 {
678 int saved_ssa_name_version = SSA_NAME_VERSION (var);
679 use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (var));
680
681 if (MAY_HAVE_DEBUG_BIND_STMTS)
682 insert_debug_temp_for_var_def (NULL, var);
683
684 if (flag_checking)
685 verify_imm_links (stderr, var);
686 while (imm->next != imm)
687 delink_imm_use (imm->next);
688
689 (*SSANAMES (fn))[SSA_NAME_VERSION (var)] = NULL_TREE;
690 memset (var, 0, tree_size (var));
691
692 imm->prev = imm;
693 imm->next = imm;
694 imm->loc.ssa_name = var;
695
696 /* First put back the right tree node so that the tree checking
697 macros do not complain. */
698 TREE_SET_CODE (var, SSA_NAME);
699
700 /* Restore the version number. */
701 SSA_NAME_VERSION (var) = saved_ssa_name_version;
702
703 /* Note this SSA_NAME is now in the first list. */
704 SSA_NAME_IN_FREE_LIST (var) = 1;
705
706 /* Put in a non-NULL TREE_TYPE so dumping code will not ICE
707 if it happens to come along a released SSA name and tries
708 to inspect its type. */
709 TREE_TYPE (var) = error_mark_node;
710
711 /* And finally queue it so that it will be put on the free list. */
712 vec_safe_push (FREE_SSANAMES_QUEUE (fn), var);
713 }
714 }
715
716 /* If the alignment of the pointer described by PI is known, return true and
717 store the alignment and the deviation from it into *ALIGNP and *MISALIGNP
718 respectively. Otherwise return false. */
719
720 bool
721 get_ptr_info_alignment (struct ptr_info_def *pi, unsigned int *alignp,
722 unsigned int *misalignp)
723 {
724 if (pi->align)
725 {
726 *alignp = pi->align;
727 *misalignp = pi->misalign;
728 return true;
729 }
730 else
731 return false;
732 }
733
734 /* State that the pointer described by PI has unknown alignment. */
735
736 void
737 mark_ptr_info_alignment_unknown (struct ptr_info_def *pi)
738 {
739 pi->align = 0;
740 pi->misalign = 0;
741 }
742
743 /* Store the power-of-two byte alignment and the deviation from that
744 alignment of pointer described by PI to ALIOGN and MISALIGN
745 respectively. */
746
747 void
748 set_ptr_info_alignment (struct ptr_info_def *pi, unsigned int align,
749 unsigned int misalign)
750 {
751 gcc_checking_assert (align != 0);
752 gcc_assert ((align & (align - 1)) == 0);
753 gcc_assert ((misalign & ~(align - 1)) == 0);
754
755 pi->align = align;
756 pi->misalign = misalign;
757 }
758
759 /* If pointer described by PI has known alignment, increase its known
760 misalignment by INCREMENT modulo its current alignment. */
761
762 void
763 adjust_ptr_info_misalignment (struct ptr_info_def *pi, poly_uint64 increment)
764 {
765 if (pi->align != 0)
766 {
767 increment += pi->misalign;
768 if (!known_misalignment (increment, pi->align, &pi->misalign))
769 {
770 pi->align = known_alignment (increment);
771 pi->misalign = 0;
772 }
773 }
774 }
775
776 /* Return the alias information associated with pointer T. It creates a
777 new instance if none existed. */
778
779 struct ptr_info_def *
780 get_ptr_info (tree t)
781 {
782 struct ptr_info_def *pi;
783
784 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
785
786 pi = SSA_NAME_PTR_INFO (t);
787 if (pi == NULL)
788 {
789 pi = ggc_cleared_alloc<ptr_info_def> ();
790 pt_solution_reset (&pi->pt);
791 mark_ptr_info_alignment_unknown (pi);
792 SSA_NAME_PTR_INFO (t) = pi;
793 }
794
795 return pi;
796 }
797
798
799 /* Creates a new SSA name using the template NAME tobe defined by
800 statement STMT in function FN. */
801
802 tree
803 copy_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
804 {
805 tree new_name;
806
807 if (SSA_NAME_VAR (name))
808 new_name = make_ssa_name_fn (fn, SSA_NAME_VAR (name), stmt);
809 else
810 {
811 new_name = make_ssa_name_fn (fn, TREE_TYPE (name), stmt);
812 SET_SSA_NAME_VAR_OR_IDENTIFIER (new_name, SSA_NAME_IDENTIFIER (name));
813 }
814
815 return new_name;
816 }
817
818
819 /* Creates a duplicate of the ptr_info_def at PTR_INFO for use by
820 the SSA name NAME. */
821
822 void
823 duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
824 {
825 struct ptr_info_def *new_ptr_info;
826
827 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name)));
828 gcc_assert (!SSA_NAME_PTR_INFO (name));
829
830 if (!ptr_info)
831 return;
832
833 new_ptr_info = ggc_alloc<ptr_info_def> ();
834 *new_ptr_info = *ptr_info;
835
836 SSA_NAME_PTR_INFO (name) = new_ptr_info;
837 }
838
839 void
840 duplicate_ssa_name_range_info (tree name, tree src)
841 {
842 gcc_checking_assert (!POINTER_TYPE_P (TREE_TYPE (src)));
843 gcc_checking_assert (!range_info_p (name));
844
845 if (range_info_p (src))
846 {
847 value_range src_range (TREE_TYPE (src));
848 range_info_get_range (src, src_range);
849 range_info_set_range (name, src_range);
850 }
851 }
852
853 /* For a SSA copy DEST = SRC duplicate SSA info present on DEST to SRC
854 to preserve it in case DEST is eliminated to SRC. */
855
856 void
857 maybe_duplicate_ssa_info_at_copy (tree dest, tree src)
858 {
859 /* While points-to info is flow-insensitive we have to avoid copying
860 info from not executed regions invoking UB to dominating defs. */
861 if (gimple_bb (SSA_NAME_DEF_STMT (src))
862 != gimple_bb (SSA_NAME_DEF_STMT (dest)))
863 return;
864
865 if (POINTER_TYPE_P (TREE_TYPE (dest))
866 && SSA_NAME_PTR_INFO (dest)
867 && ! SSA_NAME_PTR_INFO (src))
868 duplicate_ssa_name_ptr_info (src, SSA_NAME_PTR_INFO (dest));
869 else if (INTEGRAL_TYPE_P (TREE_TYPE (dest))
870 && SSA_NAME_RANGE_INFO (dest)
871 && ! SSA_NAME_RANGE_INFO (src))
872 duplicate_ssa_name_range_info (src, dest);
873 }
874
875
876 /* Creates a duplicate of a ssa name NAME tobe defined by statement STMT
877 in function FN. */
878
879 tree
880 duplicate_ssa_name_fn (struct function *fn, tree name, gimple *stmt)
881 {
882 tree new_name = copy_ssa_name_fn (fn, name, stmt);
883 if (POINTER_TYPE_P (TREE_TYPE (name)))
884 {
885 struct ptr_info_def *old_ptr_info = SSA_NAME_PTR_INFO (name);
886
887 if (old_ptr_info)
888 duplicate_ssa_name_ptr_info (new_name, old_ptr_info);
889 }
890 else if (range_info_p (name))
891 duplicate_ssa_name_range_info (new_name, name);
892
893 return new_name;
894 }
895
896
897 /* Reset all flow sensitive data on NAME such as range-info, nonzero
898 bits and alignment. */
899
900 void
901 reset_flow_sensitive_info (tree name)
902 {
903 if (POINTER_TYPE_P (TREE_TYPE (name)))
904 {
905 /* points-to info is not flow-sensitive. */
906 if (SSA_NAME_PTR_INFO (name))
907 {
908 /* [E]VRP can derive context sensitive alignment info and
909 non-nullness properties. We must reset both. */
910 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (name));
911 SSA_NAME_PTR_INFO (name)->pt.null = 1;
912 }
913 }
914 else
915 SSA_NAME_RANGE_INFO (name) = NULL;
916 }
917
918 /* Clear all flow sensitive data from all statements and PHI definitions
919 in BB. */
920
921 void
922 reset_flow_sensitive_info_in_bb (basic_block bb)
923 {
924 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
925 gsi_next (&gsi))
926 {
927 gimple *stmt = gsi_stmt (gsi);
928 ssa_op_iter i;
929 tree op;
930 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
931 reset_flow_sensitive_info (op);
932 }
933
934 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
935 gsi_next (&gsi))
936 {
937 tree phi_def = gimple_phi_result (gsi.phi ());
938 reset_flow_sensitive_info (phi_def);
939 }
940 }
941
942 /* Release all the SSA_NAMEs created by STMT. */
943
944 void
945 release_defs (gimple *stmt)
946 {
947 tree def;
948 ssa_op_iter iter;
949
950 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
951 if (TREE_CODE (def) == SSA_NAME)
952 release_ssa_name (def);
953 }
954
955
956 /* Replace the symbol associated with SSA_NAME with SYM. */
957
958 void
959 replace_ssa_name_symbol (tree ssa_name, tree sym)
960 {
961 SET_SSA_NAME_VAR_OR_IDENTIFIER (ssa_name, sym);
962 TREE_TYPE (ssa_name) = TREE_TYPE (sym);
963 }
964
965 /* Release the vector of free SSA_NAMEs and compact the vector of SSA_NAMEs
966 that are live. */
967
968 static void
969 release_free_names_and_compact_live_names (function *fun)
970 {
971 unsigned i, j;
972 int n = vec_safe_length (FREE_SSANAMES (fun));
973
974 /* Now release the freelist. */
975 vec_free (FREE_SSANAMES (fun));
976
977 /* And compact the SSA number space. We make sure to not change the
978 relative order of SSA versions. */
979 for (i = 1, j = 1; i < fun->gimple_df->ssa_names->length (); ++i)
980 {
981 tree name = ssa_name (i);
982 if (name)
983 {
984 if (i != j)
985 {
986 SSA_NAME_VERSION (name) = j;
987 (*fun->gimple_df->ssa_names)[j] = name;
988 }
989 j++;
990 }
991 }
992 fun->gimple_df->ssa_names->truncate (j);
993
994 statistics_counter_event (fun, "SSA names released", n);
995 statistics_counter_event (fun, "SSA name holes removed", i - j);
996 if (dump_file)
997 fprintf (dump_file, "Released %i names, %.2f%%, removed %i holes\n",
998 n, n * 100.0 / num_ssa_names, i - j);
999 }
1000
1001 /* Return SSA names that are unused to GGC memory and compact the SSA
1002 version namespace. This is used to keep footprint of compiler during
1003 interprocedural optimization. */
1004
1005 namespace {
1006
1007 const pass_data pass_data_release_ssa_names =
1008 {
1009 GIMPLE_PASS, /* type */
1010 "release_ssa", /* name */
1011 OPTGROUP_NONE, /* optinfo_flags */
1012 TV_TREE_SSA_OTHER, /* tv_id */
1013 PROP_ssa, /* properties_required */
1014 0, /* properties_provided */
1015 0, /* properties_destroyed */
1016 TODO_remove_unused_locals, /* todo_flags_start */
1017 0, /* todo_flags_finish */
1018 };
1019
1020 class pass_release_ssa_names : public gimple_opt_pass
1021 {
1022 public:
1023 pass_release_ssa_names (gcc::context *ctxt)
1024 : gimple_opt_pass (pass_data_release_ssa_names, ctxt)
1025 {}
1026
1027 /* opt_pass methods: */
1028 unsigned int execute (function *) final override;
1029
1030 }; // class pass_release_ssa_names
1031
1032 unsigned int
1033 pass_release_ssa_names::execute (function *fun)
1034 {
1035 release_free_names_and_compact_live_names (fun);
1036 return 0;
1037 }
1038
1039 } // anon namespace
1040
1041 gimple_opt_pass *
1042 make_pass_release_ssa_names (gcc::context *ctxt)
1043 {
1044 return new pass_release_ssa_names (ctxt);
1045 }
1046
1047 /* Save and restore of flow sensitive information. */
1048
1049 /* Save off the flow sensitive info from NAME. */
1050
1051 void
1052 flow_sensitive_info_storage::save (tree name)
1053 {
1054 gcc_assert (state == 0);
1055 if (!POINTER_TYPE_P (TREE_TYPE (name)))
1056 {
1057 range_info = SSA_NAME_RANGE_INFO (name);
1058 state = 1;
1059 return;
1060 }
1061 state = -1;
1062 auto ptr_info = SSA_NAME_PTR_INFO (name);
1063 if (ptr_info)
1064 {
1065 align = ptr_info->align;
1066 misalign = ptr_info->misalign;
1067 null = SSA_NAME_PTR_INFO (name)->pt.null;
1068 }
1069 else
1070 {
1071 align = 0;
1072 misalign = 0;
1073 null = true;
1074 }
1075 }
1076
1077 /* Restore the flow sensitive info from NAME. */
1078
1079 void
1080 flow_sensitive_info_storage::restore (tree name)
1081 {
1082 gcc_assert (state != 0);
1083 if (!POINTER_TYPE_P (TREE_TYPE (name)))
1084 {
1085 gcc_assert (state == 1);
1086 SSA_NAME_RANGE_INFO (name) = range_info;
1087 return;
1088 }
1089 gcc_assert (state == -1);
1090 auto ptr_info = SSA_NAME_PTR_INFO (name);
1091 /* If there was no flow sensitive info on the pointer
1092 just return, there is nothing to restore to. */
1093 if (!ptr_info)
1094 return;
1095 if (align != 0)
1096 set_ptr_info_alignment (ptr_info, align, misalign);
1097 else
1098 mark_ptr_info_alignment_unknown (ptr_info);
1099 SSA_NAME_PTR_INFO (name)->pt.null = null;
1100 }
1101
1102 /* Save off the flow sensitive info from NAME.
1103 And reset the flow sensitive info of NAME. */
1104
1105 void
1106 flow_sensitive_info_storage::save_and_clear (tree name)
1107 {
1108 save (name);
1109 reset_flow_sensitive_info (name);
1110 }
1111
1112 /* Clear the storage. */
1113 void
1114 flow_sensitive_info_storage::clear_storage (void)
1115 {
1116 state = 0;
1117 }