PR rtl-optimization/88018
[official-gcc.git] / gcc / tree-phinodes.c
blob16dcf67e2f07e03fa2993cf126a6bd9824ca1cbf
1 /* Generic routines for manipulating PHIs
2 Copyright (C) 2003-2018 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 "ssa.h"
27 #include "fold-const.h"
28 #include "gimple-iterator.h"
29 #include "tree-ssa.h"
31 /* Rewriting a function into SSA form can create a huge number of PHIs
32 many of which may be thrown away shortly after their creation if jumps
33 were threaded through PHI nodes.
35 While our garbage collection mechanisms will handle this situation, it
36 is extremely wasteful to create nodes and throw them away, especially
37 when the nodes can be reused.
39 For PR 8361, we can significantly reduce the number of nodes allocated
40 and thus the total amount of memory allocated by managing PHIs a
41 little. This additionally helps reduce the amount of work done by the
42 garbage collector. Similar results have been seen on a wider variety
43 of tests (such as the compiler itself).
45 PHI nodes have different sizes, so we can't have a single list of all
46 the PHI nodes as it would be too expensive to walk down that list to
47 find a PHI of a suitable size.
49 Instead we have an array of lists of free PHI nodes. The array is
50 indexed by the number of PHI alternatives that PHI node can hold.
51 Except for the last array member, which holds all remaining PHI
52 nodes.
54 So to find a free PHI node, we compute its index into the free PHI
55 node array and see if there are any elements with an exact match.
56 If so, then we are done. Otherwise, we test the next larger size
57 up and continue until we are in the last array element.
59 We do not actually walk members of the last array element. While it
60 might allow us to pick up a few reusable PHI nodes, it could potentially
61 be very expensive if the program has released a bunch of large PHI nodes,
62 but keeps asking for even larger PHI nodes. Experiments have shown that
63 walking the elements of the last array entry would result in finding less
64 than .1% additional reusable PHI nodes.
66 Note that we can never have less than two PHI argument slots. Thus,
67 the -2 on all the calculations below. */
69 #define NUM_BUCKETS 10
70 static GTY ((deletable (""))) vec<gimple *, va_gc> *free_phinodes[NUM_BUCKETS - 2];
71 static unsigned long free_phinode_count;
73 static int ideal_phi_node_len (int);
75 unsigned int phi_nodes_reused;
76 unsigned int phi_nodes_created;
78 /* Dump some simple statistics regarding the re-use of PHI nodes. */
80 void
81 phinodes_print_statistics (void)
83 fprintf (stderr, "PHI nodes allocated: %u%c\n",
84 SIZE_AMOUNT (phi_nodes_created));
85 fprintf (stderr, "PHI nodes reused: %u%c\n", SIZE_AMOUNT (phi_nodes_reused));
88 /* Allocate a PHI node with at least LEN arguments. If the free list
89 happens to contain a PHI node with LEN arguments or more, return
90 that one. */
92 static inline gphi *
93 allocate_phi_node (size_t len)
95 gphi *phi;
96 size_t bucket = NUM_BUCKETS - 2;
97 size_t size = sizeof (struct gphi)
98 + (len - 1) * sizeof (struct phi_arg_d);
100 if (free_phinode_count)
101 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
102 if (free_phinodes[bucket])
103 break;
105 /* If our free list has an element, then use it. */
106 if (bucket < NUM_BUCKETS - 2
107 && gimple_phi_capacity ((*free_phinodes[bucket])[0]) >= len)
109 free_phinode_count--;
110 phi = as_a <gphi *> (free_phinodes[bucket]->pop ());
111 if (free_phinodes[bucket]->is_empty ())
112 vec_free (free_phinodes[bucket]);
113 if (GATHER_STATISTICS)
114 phi_nodes_reused++;
116 else
118 phi = static_cast <gphi *> (ggc_internal_alloc (size));
119 if (GATHER_STATISTICS)
121 enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
122 phi_nodes_created++;
123 gimple_alloc_counts[(int) kind]++;
124 gimple_alloc_sizes[(int) kind] += size;
128 return phi;
131 /* Given LEN, the original number of requested PHI arguments, return
132 a new, "ideal" length for the PHI node. The "ideal" length rounds
133 the total size of the PHI node up to the next power of two bytes.
135 Rounding up will not result in wasting any memory since the size request
136 will be rounded up by the GC system anyway. [ Note this is not entirely
137 true since the original length might have fit on one of the special
138 GC pages. ] By rounding up, we may avoid the need to reallocate the
139 PHI node later if we increase the number of arguments for the PHI. */
141 static int
142 ideal_phi_node_len (int len)
144 size_t size, new_size;
145 int log2, new_len;
147 /* We do not support allocations of less than two PHI argument slots. */
148 if (len < 2)
149 len = 2;
151 /* Compute the number of bytes of the original request. */
152 size = sizeof (struct gphi)
153 + (len - 1) * sizeof (struct phi_arg_d);
155 /* Round it up to the next power of two. */
156 log2 = ceil_log2 (size);
157 new_size = 1 << log2;
159 /* Now compute and return the number of PHI argument slots given an
160 ideal size allocation. */
161 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
162 return new_len;
165 /* Return a PHI node with LEN argument slots for variable VAR. */
167 static gphi *
168 make_phi_node (tree var, int len)
170 gphi *phi;
171 int capacity, i;
173 capacity = ideal_phi_node_len (len);
175 phi = allocate_phi_node (capacity);
177 /* We need to clear the entire PHI node, including the argument
178 portion, because we represent a "missing PHI argument" by placing
179 NULL_TREE in PHI_ARG_DEF. */
180 memset (phi, 0, (sizeof (struct gphi)
181 - sizeof (struct phi_arg_d)
182 + sizeof (struct phi_arg_d) * len));
183 phi->code = GIMPLE_PHI;
184 gimple_init_singleton (phi);
185 phi->nargs = len;
186 phi->capacity = capacity;
187 if (!var)
189 else if (TREE_CODE (var) == SSA_NAME)
190 gimple_phi_set_result (phi, var);
191 else
192 gimple_phi_set_result (phi, make_ssa_name (var, phi));
194 for (i = 0; i < len; i++)
196 use_operand_p imm;
198 gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
199 imm = gimple_phi_arg_imm_use_ptr (phi, i);
200 imm->use = gimple_phi_arg_def_ptr (phi, i);
201 imm->prev = NULL;
202 imm->next = NULL;
203 imm->loc.stmt = phi;
206 return phi;
209 /* We no longer need PHI, release it so that it may be reused. */
211 static void
212 release_phi_node (gimple *phi)
214 size_t bucket;
215 size_t len = gimple_phi_capacity (phi);
216 size_t x;
218 for (x = 0; x < gimple_phi_num_args (phi); x++)
220 use_operand_p imm;
221 imm = gimple_phi_arg_imm_use_ptr (phi, x);
222 delink_imm_use (imm);
225 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
226 bucket -= 2;
227 vec_safe_push (free_phinodes[bucket], phi);
228 free_phinode_count++;
232 /* Resize an existing PHI node. The only way is up. Return the
233 possibly relocated phi. */
235 static gphi *
236 resize_phi_node (gphi *phi, size_t len)
238 size_t old_size, i;
239 gphi *new_phi;
241 gcc_assert (len > gimple_phi_capacity (phi));
243 /* The garbage collector will not look at the PHI node beyond the
244 first PHI_NUM_ARGS elements. Therefore, all we have to copy is a
245 portion of the PHI node currently in use. */
246 old_size = sizeof (struct gphi)
247 + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
249 new_phi = allocate_phi_node (len);
251 memcpy (new_phi, phi, old_size);
252 memset ((char *)new_phi + old_size, 0,
253 (sizeof (struct gphi)
254 - sizeof (struct phi_arg_d)
255 + sizeof (struct phi_arg_d) * len) - old_size);
257 for (i = 0; i < gimple_phi_num_args (new_phi); i++)
259 use_operand_p imm, old_imm;
260 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
261 old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
262 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
263 relink_imm_use_stmt (imm, old_imm, new_phi);
266 new_phi->capacity = len;
268 return new_phi;
271 /* Reserve PHI arguments for a new edge to basic block BB. */
273 void
274 reserve_phi_args_for_new_edge (basic_block bb)
276 size_t len = EDGE_COUNT (bb->preds);
277 size_t cap = ideal_phi_node_len (len + 4);
278 gphi_iterator gsi;
280 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
282 gphi *stmt = gsi.phi ();
284 if (len > gimple_phi_capacity (stmt))
286 gphi *new_phi = resize_phi_node (stmt, cap);
288 /* The result of the PHI is defined by this PHI node. */
289 SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
290 gsi_set_stmt (&gsi, new_phi);
292 release_phi_node (stmt);
293 stmt = new_phi;
296 stmt->nargs++;
298 /* We represent a "missing PHI argument" by placing NULL_TREE in
299 the corresponding slot. If PHI arguments were added
300 immediately after an edge is created, this zeroing would not
301 be necessary, but unfortunately this is not the case. For
302 example, the loop optimizer duplicates several basic blocks,
303 redirects edges, and then fixes up PHI arguments later in
304 batch. */
305 use_operand_p imm = gimple_phi_arg_imm_use_ptr (stmt, len - 1);
306 imm->use = gimple_phi_arg_def_ptr (stmt, len - 1);
307 imm->prev = NULL;
308 imm->next = NULL;
309 imm->loc.stmt = stmt;
310 SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
311 gimple_phi_arg_set_location (stmt, len - 1, UNKNOWN_LOCATION);
315 /* Adds PHI to BB. */
317 void
318 add_phi_node_to_bb (gphi *phi, basic_block bb)
320 gimple_seq seq = phi_nodes (bb);
321 /* Add the new PHI node to the list of PHI nodes for block BB. */
322 if (seq == NULL)
323 set_phi_nodes (bb, gimple_seq_alloc_with_stmt (phi));
324 else
326 gimple_seq_add_stmt (&seq, phi);
327 gcc_assert (seq == phi_nodes (bb));
330 /* Associate BB to the PHI node. */
331 gimple_set_bb (phi, bb);
335 /* Create a new PHI node for variable VAR at basic block BB. */
337 gphi *
338 create_phi_node (tree var, basic_block bb)
340 gphi *phi = make_phi_node (var, EDGE_COUNT (bb->preds));
342 add_phi_node_to_bb (phi, bb);
343 return phi;
347 /* Add a new argument to PHI node PHI. DEF is the incoming reaching
348 definition and E is the edge through which DEF reaches PHI. The new
349 argument is added at the end of the argument list.
350 If PHI has reached its maximum capacity, add a few slots. In this case,
351 PHI points to the reallocated phi node when we return. */
353 void
354 add_phi_arg (gphi *phi, tree def, edge e, location_t locus)
356 basic_block bb = e->dest;
358 gcc_assert (bb == gimple_bb (phi));
360 /* We resize PHI nodes upon edge creation. We should always have
361 enough room at this point. */
362 gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
364 /* We resize PHI nodes upon edge creation. We should always have
365 enough room at this point. */
366 gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
368 /* Copy propagation needs to know what object occur in abnormal
369 PHI nodes. This is a convenient place to record such information. */
370 if (e->flags & EDGE_ABNORMAL)
372 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
373 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
376 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
377 gimple_phi_arg_set_location (phi, e->dest_idx, locus);
381 /* Remove the Ith argument from PHI's argument list. This routine
382 implements removal by swapping the last alternative with the
383 alternative we want to delete and then shrinking the vector, which
384 is consistent with how we remove an edge from the edge vector. */
386 static void
387 remove_phi_arg_num (gphi *phi, int i)
389 int num_elem = gimple_phi_num_args (phi);
391 gcc_assert (i < num_elem);
393 /* Delink the item which is being removed. */
394 delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
396 /* If it is not the last element, move the last element
397 to the element we want to delete, resetting all the links. */
398 if (i != num_elem - 1)
400 use_operand_p old_p, new_p;
401 old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
402 new_p = gimple_phi_arg_imm_use_ptr (phi, i);
403 /* Set use on new node, and link into last element's place. */
404 *(new_p->use) = *(old_p->use);
405 relink_imm_use (new_p, old_p);
406 /* Move the location as well. */
407 gimple_phi_arg_set_location (phi, i,
408 gimple_phi_arg_location (phi, num_elem - 1));
411 /* Shrink the vector and return. Note that we do not have to clear
412 PHI_ARG_DEF because the garbage collector will not look at those
413 elements beyond the first PHI_NUM_ARGS elements of the array. */
414 phi->nargs--;
418 /* Remove all PHI arguments associated with edge E. */
420 void
421 remove_phi_args (edge e)
423 gphi_iterator gsi;
425 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
426 remove_phi_arg_num (gsi.phi (),
427 e->dest_idx);
431 /* Remove the PHI node pointed-to by iterator GSI from basic block BB. After
432 removal, iterator GSI is updated to point to the next PHI node in the
433 sequence. If RELEASE_LHS_P is true, the LHS of this PHI node is released
434 into the free pool of SSA names. */
436 void
437 remove_phi_node (gimple_stmt_iterator *gsi, bool release_lhs_p)
439 gimple *phi = gsi_stmt (*gsi);
441 if (release_lhs_p)
442 insert_debug_temps_for_defs (gsi);
444 gsi_remove (gsi, false);
446 /* If we are deleting the PHI node, then we should release the
447 SSA_NAME node so that it can be reused. */
448 release_phi_node (phi);
449 if (release_lhs_p)
450 release_ssa_name (gimple_phi_result (phi));
453 /* Remove all the phi nodes from BB. */
455 void
456 remove_phi_nodes (basic_block bb)
458 gphi_iterator gsi;
460 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
461 remove_phi_node (&gsi, true);
463 set_phi_nodes (bb, NULL);
466 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
467 NULL. */
469 tree
470 degenerate_phi_result (gphi *phi)
472 tree lhs = gimple_phi_result (phi);
473 tree val = NULL;
474 size_t i;
476 /* Ignoring arguments which are the same as LHS, if all the remaining
477 arguments are the same, then the PHI is a degenerate and has the
478 value of that common argument. */
479 for (i = 0; i < gimple_phi_num_args (phi); i++)
481 tree arg = gimple_phi_arg_def (phi, i);
483 if (arg == lhs)
484 continue;
485 else if (!arg)
486 break;
487 else if (!val)
488 val = arg;
489 else if (arg == val)
490 continue;
491 /* We bring in some of operand_equal_p not only to speed things
492 up, but also to avoid crashing when dereferencing the type of
493 a released SSA name. */
494 else if (TREE_CODE (val) != TREE_CODE (arg)
495 || TREE_CODE (val) == SSA_NAME
496 || !operand_equal_p (arg, val, 0))
497 break;
499 return (i == gimple_phi_num_args (phi) ? val : NULL);
502 /* Set PHI nodes of a basic block BB to SEQ. */
504 void
505 set_phi_nodes (basic_block bb, gimple_seq seq)
507 gimple_stmt_iterator i;
509 gcc_checking_assert (!(bb->flags & BB_RTL));
510 bb->il.gimple.phi_nodes = seq;
511 if (seq)
512 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
513 gimple_set_bb (gsi_stmt (i), bb);
516 #include "gt-tree-phinodes.h"