2007-05-27 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / tree-phinodes.c
blobe77f4884d727d6a81ceb58f30ce3590f092999ff
1 /* Generic routines for manipulating PHIs
2 Copyright (C) 2003, 2005 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "varray.h"
28 #include "ggc.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "toplev.h"
33 /* Rewriting a function into SSA form can create a huge number of PHIs
34 many of which may be thrown away shortly after their creation if jumps
35 were threaded through PHI nodes.
37 While our garbage collection mechanisms will handle this situation, it
38 is extremely wasteful to create nodes and throw them away, especially
39 when the nodes can be reused.
41 For PR 8361, we can significantly reduce the number of nodes allocated
42 and thus the total amount of memory allocated by managing PHIs a
43 little. This additionally helps reduce the amount of work done by the
44 garbage collector. Similar results have been seen on a wider variety
45 of tests (such as the compiler itself).
47 Right now we maintain our free list on a per-function basis. It may
48 or may not make sense to maintain the free list for the duration of
49 a compilation unit.
51 We could also use a zone allocator for these objects since they have
52 a very well defined lifetime. If someone wants to experiment with that
53 this is the place to try it.
55 PHI nodes have different sizes, so we can't have a single list of all
56 the PHI nodes as it would be too expensive to walk down that list to
57 find a PHI of a suitable size.
59 Instead we have an array of lists of free PHI nodes. The array is
60 indexed by the number of PHI alternatives that PHI node can hold.
61 Except for the last array member, which holds all remaining PHI
62 nodes.
64 So to find a free PHI node, we compute its index into the free PHI
65 node array and see if there are any elements with an exact match.
66 If so, then we are done. Otherwise, we test the next larger size
67 up and continue until we are in the last array element.
69 We do not actually walk members of the last array element. While it
70 might allow us to pick up a few reusable PHI nodes, it could potentially
71 be very expensive if the program has released a bunch of large PHI nodes,
72 but keeps asking for even larger PHI nodes. Experiments have shown that
73 walking the elements of the last array entry would result in finding less
74 than .1% additional reusable PHI nodes.
76 Note that we can never have less than two PHI argument slots. Thus,
77 the -2 on all the calculations below. */
79 #define NUM_BUCKETS 10
80 static GTY ((deletable (""))) tree free_phinodes[NUM_BUCKETS - 2];
81 static unsigned long free_phinode_count;
83 static int ideal_phi_node_len (int);
84 static void resize_phi_node (tree *, int);
86 #ifdef GATHER_STATISTICS
87 unsigned int phi_nodes_reused;
88 unsigned int phi_nodes_created;
89 #endif
91 /* Initialize management of PHIs. */
93 void
94 init_phinodes (void)
96 int i;
98 for (i = 0; i < NUM_BUCKETS - 2; i++)
99 free_phinodes[i] = NULL;
100 free_phinode_count = 0;
103 /* Finalize management of PHIs. */
105 void
106 fini_phinodes (void)
108 int i;
110 for (i = 0; i < NUM_BUCKETS - 2; i++)
111 free_phinodes[i] = NULL;
112 free_phinode_count = 0;
115 /* Dump some simple statistics regarding the re-use of PHI nodes. */
117 #ifdef GATHER_STATISTICS
118 void
119 phinodes_print_statistics (void)
121 fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
122 fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
124 #endif
126 /* Allocate a PHI node with at least LEN arguments. If the free list
127 happens to contain a PHI node with LEN arguments or more, return
128 that one. */
130 static inline tree
131 allocate_phi_node (int len)
133 tree phi;
134 int bucket = NUM_BUCKETS - 2;
135 int size = (sizeof (struct tree_phi_node)
136 + (len - 1) * sizeof (struct phi_arg_d));
138 if (free_phinode_count)
139 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
140 if (free_phinodes[bucket])
141 break;
143 /* If our free list has an element, then use it. */
144 if (bucket < NUM_BUCKETS - 2
145 && PHI_ARG_CAPACITY (free_phinodes[bucket]) >= len)
147 free_phinode_count--;
148 phi = free_phinodes[bucket];
149 free_phinodes[bucket] = PHI_CHAIN (free_phinodes[bucket]);
150 #ifdef GATHER_STATISTICS
151 phi_nodes_reused++;
152 #endif
154 else
156 phi = ggc_alloc (size);
157 #ifdef GATHER_STATISTICS
158 phi_nodes_created++;
159 tree_node_counts[(int) phi_kind]++;
160 tree_node_sizes[(int) phi_kind] += size;
161 #endif
164 return phi;
167 /* Given LEN, the original number of requested PHI arguments, return
168 a new, "ideal" length for the PHI node. The "ideal" length rounds
169 the total size of the PHI node up to the next power of two bytes.
171 Rounding up will not result in wasting any memory since the size request
172 will be rounded up by the GC system anyway. [ Note this is not entirely
173 true since the original length might have fit on one of the special
174 GC pages. ] By rounding up, we may avoid the need to reallocate the
175 PHI node later if we increase the number of arguments for the PHI. */
177 static int
178 ideal_phi_node_len (int len)
180 size_t size, new_size;
181 int log2, new_len;
183 /* We do not support allocations of less than two PHI argument slots. */
184 if (len < 2)
185 len = 2;
187 /* Compute the number of bytes of the original request. */
188 size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
190 /* Round it up to the next power of two. */
191 log2 = ceil_log2 (size);
192 new_size = 1 << log2;
194 /* Now compute and return the number of PHI argument slots given an
195 ideal size allocation. */
196 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
197 return new_len;
201 /* Return a PHI node with LEN argument slots for variable VAR. */
203 static tree
204 make_phi_node (tree var, int len)
206 tree phi;
207 int capacity, i;
209 capacity = ideal_phi_node_len (len);
211 phi = allocate_phi_node (capacity);
213 /* We need to clear the entire PHI node, including the argument
214 portion, because we represent a "missing PHI argument" by placing
215 NULL_TREE in PHI_ARG_DEF. */
216 memset (phi, 0, (sizeof (struct tree_phi_node) - sizeof (struct phi_arg_d)
217 + sizeof (struct phi_arg_d) * len));
218 TREE_SET_CODE (phi, PHI_NODE);
219 PHI_NUM_ARGS (phi) = len;
220 PHI_ARG_CAPACITY (phi) = capacity;
221 if (TREE_CODE (var) == SSA_NAME)
222 SET_PHI_RESULT (phi, var);
223 else
224 SET_PHI_RESULT (phi, make_ssa_name (var, phi));
226 for (i = 0; i < capacity; i++)
228 use_operand_p imm;
229 imm = &(PHI_ARG_IMM_USE_NODE (phi, i));
230 imm->use = &(PHI_ARG_DEF_TREE (phi, i));
231 imm->prev = NULL;
232 imm->next = NULL;
233 imm->stmt = phi;
236 return phi;
239 /* We no longer need PHI, release it so that it may be reused. */
241 void
242 release_phi_node (tree phi)
244 int bucket;
245 int len = PHI_ARG_CAPACITY (phi);
246 int x;
248 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
250 use_operand_p imm;
251 imm = &(PHI_ARG_IMM_USE_NODE (phi, x));
252 delink_imm_use (imm);
255 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
256 bucket -= 2;
257 PHI_CHAIN (phi) = free_phinodes[bucket];
258 free_phinodes[bucket] = phi;
259 free_phinode_count++;
262 /* Resize an existing PHI node. The only way is up. Return the
263 possibly relocated phi. */
265 static void
266 resize_phi_node (tree *phi, int len)
268 int old_size, i;
269 tree new_phi;
271 gcc_assert (len > PHI_ARG_CAPACITY (*phi));
273 /* The garbage collector will not look at the PHI node beyond the
274 first PHI_NUM_ARGS elements. Therefore, all we have to copy is a
275 portion of the PHI node currently in use. */
276 old_size = (sizeof (struct tree_phi_node)
277 + (PHI_NUM_ARGS (*phi) - 1) * sizeof (struct phi_arg_d));
279 new_phi = allocate_phi_node (len);
281 memcpy (new_phi, *phi, old_size);
283 for (i = 0; i < PHI_NUM_ARGS (new_phi); i++)
285 use_operand_p imm, old_imm;
286 imm = &(PHI_ARG_IMM_USE_NODE (new_phi, i));
287 old_imm = &(PHI_ARG_IMM_USE_NODE (*phi, i));
288 imm->use = &(PHI_ARG_DEF_TREE (new_phi, i));
289 relink_imm_use_stmt (imm, old_imm, new_phi);
292 PHI_ARG_CAPACITY (new_phi) = len;
294 for (i = PHI_NUM_ARGS (new_phi); i < len; i++)
296 use_operand_p imm;
297 imm = &(PHI_ARG_IMM_USE_NODE (new_phi, i));
298 imm->use = &(PHI_ARG_DEF_TREE (new_phi, i));
299 imm->prev = NULL;
300 imm->next = NULL;
301 imm->stmt = new_phi;
304 *phi = new_phi;
307 /* Reserve PHI arguments for a new edge to basic block BB. */
309 void
310 reserve_phi_args_for_new_edge (basic_block bb)
312 tree *loc;
313 int len = EDGE_COUNT (bb->preds);
314 int cap = ideal_phi_node_len (len + 4);
316 for (loc = phi_nodes_ptr (bb);
317 *loc;
318 loc = &PHI_CHAIN (*loc))
320 if (len > PHI_ARG_CAPACITY (*loc))
322 tree old_phi = *loc;
324 resize_phi_node (loc, cap);
326 /* The result of the phi is defined by this phi node. */
327 SSA_NAME_DEF_STMT (PHI_RESULT (*loc)) = *loc;
329 release_phi_node (old_phi);
332 /* We represent a "missing PHI argument" by placing NULL_TREE in
333 the corresponding slot. If PHI arguments were added
334 immediately after an edge is created, this zeroing would not
335 be necessary, but unfortunately this is not the case. For
336 example, the loop optimizer duplicates several basic blocks,
337 redirects edges, and then fixes up PHI arguments later in
338 batch. */
339 SET_PHI_ARG_DEF (*loc, len - 1, NULL_TREE);
341 PHI_NUM_ARGS (*loc)++;
346 /* Create a new PHI node for variable VAR at basic block BB. */
348 tree
349 create_phi_node (tree var, basic_block bb)
351 tree phi;
353 phi = make_phi_node (var, EDGE_COUNT (bb->preds));
355 /* Add the new PHI node to the list of PHI nodes for block BB. */
356 PHI_CHAIN (phi) = phi_nodes (bb);
357 set_phi_nodes (bb, phi);
359 /* Associate BB to the PHI node. */
360 set_bb_for_stmt (phi, bb);
362 return phi;
366 /* Add a new argument to PHI node PHI. DEF is the incoming reaching
367 definition and E is the edge through which DEF reaches PHI. The new
368 argument is added at the end of the argument list.
369 If PHI has reached its maximum capacity, add a few slots. In this case,
370 PHI points to the reallocated phi node when we return. */
372 void
373 add_phi_arg (tree phi, tree def, edge e)
375 basic_block bb = e->dest;
377 gcc_assert (bb == bb_for_stmt (phi));
379 /* We resize PHI nodes upon edge creation. We should always have
380 enough room at this point. */
381 gcc_assert (PHI_NUM_ARGS (phi) <= PHI_ARG_CAPACITY (phi));
383 /* We resize PHI nodes upon edge creation. We should always have
384 enough room at this point. */
385 gcc_assert (e->dest_idx < (unsigned int) PHI_NUM_ARGS (phi));
387 /* Copy propagation needs to know what object occur in abnormal
388 PHI nodes. This is a convenient place to record such information. */
389 if (e->flags & EDGE_ABNORMAL)
391 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
392 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
395 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
399 /* Remove the Ith argument from PHI's argument list. This routine
400 implements removal by swapping the last alternative with the
401 alternative we want to delete and then shrinking the vector, which
402 is consistent with how we remove an edge from the edge vector. */
404 static void
405 remove_phi_arg_num (tree phi, int i)
407 int num_elem = PHI_NUM_ARGS (phi);
409 gcc_assert (i < num_elem);
411 /* Delink the item which is being removed. */
412 delink_imm_use (&(PHI_ARG_IMM_USE_NODE (phi, i)));
414 /* If it is not the last element, move the last element
415 to the element we want to delete, resetting all the links. */
416 if (i != num_elem - 1)
418 use_operand_p old_p, new_p;
419 old_p = &PHI_ARG_IMM_USE_NODE (phi, num_elem - 1);
420 new_p = &PHI_ARG_IMM_USE_NODE (phi, i);
421 /* Set use on new node, and link into last element's place. */
422 *(new_p->use) = *(old_p->use);
423 relink_imm_use (new_p, old_p);
426 /* Shrink the vector and return. Note that we do not have to clear
427 PHI_ARG_DEF because the garbage collector will not look at those
428 elements beyond the first PHI_NUM_ARGS elements of the array. */
429 PHI_NUM_ARGS (phi)--;
433 /* Remove all PHI arguments associated with edge E. */
435 void
436 remove_phi_args (edge e)
438 tree phi;
440 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
441 remove_phi_arg_num (phi, e->dest_idx);
445 /* Remove PHI node PHI from basic block BB. If PREV is non-NULL, it is
446 used as the node immediately before PHI in the linked list. If
447 RELEASE_LHS_P is true, the LHS of this PHI node is released into
448 the free pool of SSA names. */
450 void
451 remove_phi_node (tree phi, tree prev, bool release_lhs_p)
453 tree *loc;
455 if (prev)
457 loc = &PHI_CHAIN (prev);
459 else
461 for (loc = phi_nodes_ptr (bb_for_stmt (phi));
462 *loc != phi;
463 loc = &PHI_CHAIN (*loc))
467 /* Remove PHI from the chain. */
468 *loc = PHI_CHAIN (phi);
470 /* If we are deleting the PHI node, then we should release the
471 SSA_NAME node so that it can be reused. */
472 release_phi_node (phi);
473 if (release_lhs_p)
474 release_ssa_name (PHI_RESULT (phi));
478 /* Reverse the order of PHI nodes in the chain PHI.
479 Return the new head of the chain (old last PHI node). */
481 tree
482 phi_reverse (tree phi)
484 tree prev = NULL_TREE, next;
485 for (; phi; phi = next)
487 next = PHI_CHAIN (phi);
488 PHI_CHAIN (phi) = prev;
489 prev = phi;
491 return prev;
494 #include "gt-tree-phinodes.h"