2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-phinodes.c
blobbf024ac976c3f259decede6a8ad223c1e3b1e6a9
1 /* Generic routines for manipulating PHIs
2 Copyright (C) 2003-2013 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 "tm.h"
24 #include "tree.h"
25 #include "ggc.h"
26 #include "basic-block.h"
27 #include "gimple.h"
28 #include "gimple-iterator.h"
29 #include "gimple-ssa.h"
30 #include "tree-phinodes.h"
31 #include "ssa-iterators.h"
32 #include "stringpool.h"
33 #include "tree-ssanames.h"
34 #include "tree-ssa.h"
35 #include "diagnostic-core.h"
37 /* Rewriting a function into SSA form can create a huge number of PHIs
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 PHIs 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 PHI nodes have different sizes, so we can't have a single list of all
52 the PHI nodes as it would be too expensive to walk down that list to
53 find a PHI of a suitable size.
55 Instead we have an array of lists of free PHI nodes. The array is
56 indexed by the number of PHI alternatives that PHI node can hold.
57 Except for the last array member, which holds all remaining PHI
58 nodes.
60 So to find a free PHI node, we compute its index into the free PHI
61 node array and see if there are any elements with an exact match.
62 If so, then we are done. Otherwise, we test the next larger size
63 up and continue until we are in the last array element.
65 We do not actually walk members of the last array element. While it
66 might allow us to pick up a few reusable PHI nodes, it could potentially
67 be very expensive if the program has released a bunch of large PHI nodes,
68 but keeps asking for even larger PHI nodes. Experiments have shown that
69 walking the elements of the last array entry would result in finding less
70 than .1% additional reusable PHI nodes.
72 Note that we can never have less than two PHI argument slots. Thus,
73 the -2 on all the calculations below. */
75 #define NUM_BUCKETS 10
76 static GTY ((deletable (""))) vec<gimple, va_gc> *free_phinodes[NUM_BUCKETS - 2];
77 static unsigned long free_phinode_count;
79 static int ideal_phi_node_len (int);
81 unsigned int phi_nodes_reused;
82 unsigned int phi_nodes_created;
84 /* Dump some simple statistics regarding the re-use of PHI nodes. */
86 void
87 phinodes_print_statistics (void)
89 fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
90 fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
93 /* Allocate a PHI node with at least LEN arguments. If the free list
94 happens to contain a PHI node with LEN arguments or more, return
95 that one. */
97 static inline gimple_statement_phi *
98 allocate_phi_node (size_t len)
100 gimple_statement_phi *phi;
101 size_t bucket = NUM_BUCKETS - 2;
102 size_t size = sizeof (struct gimple_statement_phi)
103 + (len - 1) * sizeof (struct phi_arg_d);
105 if (free_phinode_count)
106 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
107 if (free_phinodes[bucket])
108 break;
110 /* If our free list has an element, then use it. */
111 if (bucket < NUM_BUCKETS - 2
112 && gimple_phi_capacity ((*free_phinodes[bucket])[0]) >= len)
114 free_phinode_count--;
115 phi = as_a <gimple_statement_phi> (free_phinodes[bucket]->pop ());
116 if (free_phinodes[bucket]->is_empty ())
117 vec_free (free_phinodes[bucket]);
118 if (GATHER_STATISTICS)
119 phi_nodes_reused++;
121 else
123 phi = static_cast <gimple_statement_phi *> (
124 ggc_internal_alloc_stat (size MEM_STAT_INFO));
125 if (GATHER_STATISTICS)
127 enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
128 phi_nodes_created++;
129 gimple_alloc_counts[(int) kind]++;
130 gimple_alloc_sizes[(int) kind] += size;
134 return phi;
137 /* Given LEN, the original number of requested PHI arguments, return
138 a new, "ideal" length for the PHI node. The "ideal" length rounds
139 the total size of the PHI node up to the next power of two bytes.
141 Rounding up will not result in wasting any memory since the size request
142 will be rounded up by the GC system anyway. [ Note this is not entirely
143 true since the original length might have fit on one of the special
144 GC pages. ] By rounding up, we may avoid the need to reallocate the
145 PHI node later if we increase the number of arguments for the PHI. */
147 static int
148 ideal_phi_node_len (int len)
150 size_t size, new_size;
151 int log2, new_len;
153 /* We do not support allocations of less than two PHI argument slots. */
154 if (len < 2)
155 len = 2;
157 /* Compute the number of bytes of the original request. */
158 size = sizeof (struct gimple_statement_phi)
159 + (len - 1) * sizeof (struct phi_arg_d);
161 /* Round it up to the next power of two. */
162 log2 = ceil_log2 (size);
163 new_size = 1 << log2;
165 /* Now compute and return the number of PHI argument slots given an
166 ideal size allocation. */
167 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
168 return new_len;
171 /* Return a PHI node with LEN argument slots for variable VAR. */
173 static gimple
174 make_phi_node (tree var, int len)
176 gimple_statement_phi *phi;
177 int capacity, i;
179 capacity = ideal_phi_node_len (len);
181 phi = allocate_phi_node (capacity);
183 /* We need to clear the entire PHI node, including the argument
184 portion, because we represent a "missing PHI argument" by placing
185 NULL_TREE in PHI_ARG_DEF. */
186 memset (phi, 0, (sizeof (struct gimple_statement_phi)
187 - sizeof (struct phi_arg_d)
188 + sizeof (struct phi_arg_d) * len));
189 phi->code = GIMPLE_PHI;
190 gimple_init_singleton (phi);
191 phi->nargs = len;
192 phi->capacity = capacity;
193 if (!var)
195 else if (TREE_CODE (var) == SSA_NAME)
196 gimple_phi_set_result (phi, var);
197 else
198 gimple_phi_set_result (phi, make_ssa_name (var, phi));
200 for (i = 0; i < capacity; i++)
202 use_operand_p imm;
204 gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
205 imm = gimple_phi_arg_imm_use_ptr (phi, i);
206 imm->use = gimple_phi_arg_def_ptr (phi, i);
207 imm->prev = NULL;
208 imm->next = NULL;
209 imm->loc.stmt = phi;
212 return phi;
215 /* We no longer need PHI, release it so that it may be reused. */
217 void
218 release_phi_node (gimple phi)
220 size_t bucket;
221 size_t len = gimple_phi_capacity (phi);
222 size_t x;
224 for (x = 0; x < gimple_phi_num_args (phi); x++)
226 use_operand_p imm;
227 imm = gimple_phi_arg_imm_use_ptr (phi, x);
228 delink_imm_use (imm);
231 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
232 bucket -= 2;
233 vec_safe_push (free_phinodes[bucket], phi);
234 free_phinode_count++;
238 /* Resize an existing PHI node. The only way is up. Return the
239 possibly relocated phi. */
241 static gimple_statement_phi *
242 resize_phi_node (gimple_statement_phi *phi, size_t len)
244 size_t old_size, i;
245 gimple_statement_phi *new_phi;
247 gcc_assert (len > gimple_phi_capacity (phi));
249 /* The garbage collector will not look at the PHI node beyond the
250 first PHI_NUM_ARGS elements. Therefore, all we have to copy is a
251 portion of the PHI node currently in use. */
252 old_size = sizeof (struct gimple_statement_phi)
253 + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
255 new_phi = allocate_phi_node (len);
257 memcpy (new_phi, phi, old_size);
259 for (i = 0; i < gimple_phi_num_args (new_phi); i++)
261 use_operand_p imm, old_imm;
262 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
263 old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
264 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
265 relink_imm_use_stmt (imm, old_imm, new_phi);
268 new_phi->capacity = len;
270 for (i = gimple_phi_num_args (new_phi); i < len; i++)
272 use_operand_p imm;
274 gimple_phi_arg_set_location (new_phi, i, UNKNOWN_LOCATION);
275 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
276 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
277 imm->prev = NULL;
278 imm->next = NULL;
279 imm->loc.stmt = new_phi;
282 return new_phi;
285 /* Reserve PHI arguments for a new edge to basic block BB. */
287 void
288 reserve_phi_args_for_new_edge (basic_block bb)
290 size_t len = EDGE_COUNT (bb->preds);
291 size_t cap = ideal_phi_node_len (len + 4);
292 gimple_stmt_iterator gsi;
294 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
296 gimple_statement_phi *stmt =
297 as_a <gimple_statement_phi> (gsi_stmt (gsi));
299 if (len > gimple_phi_capacity (stmt))
301 gimple_statement_phi *new_phi = resize_phi_node (stmt, cap);
303 /* The result of the PHI is defined by this PHI node. */
304 SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
305 gsi_set_stmt (&gsi, new_phi);
307 release_phi_node (stmt);
308 stmt = new_phi;
311 /* We represent a "missing PHI argument" by placing NULL_TREE in
312 the corresponding slot. If PHI arguments were added
313 immediately after an edge is created, this zeroing would not
314 be necessary, but unfortunately this is not the case. For
315 example, the loop optimizer duplicates several basic blocks,
316 redirects edges, and then fixes up PHI arguments later in
317 batch. */
318 SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
319 gimple_phi_arg_set_location (stmt, len - 1, UNKNOWN_LOCATION);
321 stmt->nargs++;
325 /* Adds PHI to BB. */
327 void
328 add_phi_node_to_bb (gimple phi, basic_block bb)
330 gimple_seq seq = phi_nodes (bb);
331 /* Add the new PHI node to the list of PHI nodes for block BB. */
332 if (seq == NULL)
333 set_phi_nodes (bb, gimple_seq_alloc_with_stmt (phi));
334 else
336 gimple_seq_add_stmt (&seq, phi);
337 gcc_assert (seq == phi_nodes (bb));
340 /* Associate BB to the PHI node. */
341 gimple_set_bb (phi, bb);
345 /* Create a new PHI node for variable VAR at basic block BB. */
347 gimple
348 create_phi_node (tree var, basic_block bb)
350 gimple phi = make_phi_node (var, EDGE_COUNT (bb->preds));
352 add_phi_node_to_bb (phi, bb);
353 return phi;
357 /* Add a new argument to PHI node PHI. DEF is the incoming reaching
358 definition and E is the edge through which DEF reaches PHI. The new
359 argument is added at the end of the argument list.
360 If PHI has reached its maximum capacity, add a few slots. In this case,
361 PHI points to the reallocated phi node when we return. */
363 void
364 add_phi_arg (gimple phi, tree def, edge e, source_location locus)
366 basic_block bb = e->dest;
368 gcc_assert (bb == gimple_bb (phi));
370 /* We resize PHI nodes upon edge creation. We should always have
371 enough room at this point. */
372 gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
374 /* We resize PHI nodes upon edge creation. We should always have
375 enough room at this point. */
376 gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
378 /* Copy propagation needs to know what object occur in abnormal
379 PHI nodes. This is a convenient place to record such information. */
380 if (e->flags & EDGE_ABNORMAL)
382 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
383 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
386 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
387 gimple_phi_arg_set_location (phi, e->dest_idx, locus);
391 /* Remove the Ith argument from PHI's argument list. This routine
392 implements removal by swapping the last alternative with the
393 alternative we want to delete and then shrinking the vector, which
394 is consistent with how we remove an edge from the edge vector. */
396 static void
397 remove_phi_arg_num (gimple_statement_phi *phi, int i)
399 int num_elem = gimple_phi_num_args (phi);
401 gcc_assert (i < num_elem);
403 /* Delink the item which is being removed. */
404 delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
406 /* If it is not the last element, move the last element
407 to the element we want to delete, resetting all the links. */
408 if (i != num_elem - 1)
410 use_operand_p old_p, new_p;
411 old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
412 new_p = gimple_phi_arg_imm_use_ptr (phi, i);
413 /* Set use on new node, and link into last element's place. */
414 *(new_p->use) = *(old_p->use);
415 relink_imm_use (new_p, old_p);
416 /* Move the location as well. */
417 gimple_phi_arg_set_location (phi, i,
418 gimple_phi_arg_location (phi, num_elem - 1));
421 /* Shrink the vector and return. Note that we do not have to clear
422 PHI_ARG_DEF because the garbage collector will not look at those
423 elements beyond the first PHI_NUM_ARGS elements of the array. */
424 phi->nargs--;
428 /* Remove all PHI arguments associated with edge E. */
430 void
431 remove_phi_args (edge e)
433 gimple_stmt_iterator gsi;
435 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
436 remove_phi_arg_num (as_a <gimple_statement_phi> (gsi_stmt (gsi)),
437 e->dest_idx);
441 /* Remove the PHI node pointed-to by iterator GSI from basic block BB. After
442 removal, iterator GSI is updated to point to the next PHI node in the
443 sequence. If RELEASE_LHS_P is true, the LHS of this PHI node is released
444 into the free pool of SSA names. */
446 void
447 remove_phi_node (gimple_stmt_iterator *gsi, bool release_lhs_p)
449 gimple phi = gsi_stmt (*gsi);
451 if (release_lhs_p)
452 insert_debug_temps_for_defs (gsi);
454 gsi_remove (gsi, false);
456 /* If we are deleting the PHI node, then we should release the
457 SSA_NAME node so that it can be reused. */
458 release_phi_node (phi);
459 if (release_lhs_p)
460 release_ssa_name (gimple_phi_result (phi));
463 /* Remove all the phi nodes from BB. */
465 void
466 remove_phi_nodes (basic_block bb)
468 gimple_stmt_iterator gsi;
470 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
471 remove_phi_node (&gsi, true);
473 set_phi_nodes (bb, NULL);
476 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
477 NULL. */
479 tree
480 degenerate_phi_result (gimple phi)
482 tree lhs = gimple_phi_result (phi);
483 tree val = NULL;
484 size_t i;
486 /* Ignoring arguments which are the same as LHS, if all the remaining
487 arguments are the same, then the PHI is a degenerate and has the
488 value of that common argument. */
489 for (i = 0; i < gimple_phi_num_args (phi); i++)
491 tree arg = gimple_phi_arg_def (phi, i);
493 if (arg == lhs)
494 continue;
495 else if (!arg)
496 break;
497 else if (!val)
498 val = arg;
499 else if (arg == val)
500 continue;
501 /* We bring in some of operand_equal_p not only to speed things
502 up, but also to avoid crashing when dereferencing the type of
503 a released SSA name. */
504 else if (TREE_CODE (val) != TREE_CODE (arg)
505 || TREE_CODE (val) == SSA_NAME
506 || !operand_equal_p (arg, val, 0))
507 break;
509 return (i == gimple_phi_num_args (phi) ? val : NULL);
512 /* Set PHI nodes of a basic block BB to SEQ. */
514 void
515 set_phi_nodes (basic_block bb, gimple_seq seq)
517 gimple_stmt_iterator i;
519 gcc_checking_assert (!(bb->flags & BB_RTL));
520 bb->il.gimple.phi_nodes = seq;
521 if (seq)
522 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
523 gimple_set_bb (gsi_stmt (i), bb);
526 #include "gt-tree-phinodes.h"