OSDN Git Service

2004-05-21 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / tree-phinodes.c
1 /* Generic routines for manipulating PHIs
2    Copyright (C) 2003 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 2, 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 COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20                                                                                 
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"
32
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.  
36
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.
40
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).
46
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. 
50
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.
54    
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.
58
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.
63
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.
68
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. 
75
76    Note that we can never have less than two PHI argument slots.  Thus,
77    the -2 on all the calculations below.  */
78
79 #define NUM_BUCKETS 10
80 static GTY ((deletable (""))) tree free_phinodes[NUM_BUCKETS - 2];
81 static unsigned long free_phinode_count;
82
83 static int ideal_phi_node_len (int);
84 static void resize_phi_node (tree *, int);
85
86 #ifdef GATHER_STATISTICS
87 unsigned int phi_nodes_reused;
88 unsigned int phi_nodes_created;
89 #endif
90
91 /* Initialize management of PHIs.  */
92
93 void
94 init_phinodes (void)
95 {
96   int i;
97
98   for (i = 0; i < NUM_BUCKETS - 2; i++)
99     free_phinodes[i] = NULL;
100   free_phinode_count = 0;
101 }
102
103 /* Finalize management of PHIs.  */
104
105 void
106 fini_phinodes (void)
107 {
108   int i;
109
110   for (i = 0; i < NUM_BUCKETS - 2; i++)
111     free_phinodes[i] = NULL;
112   free_phinode_count = 0;
113 }
114
115 /* Dump some simple statistics regarding the re-use of PHI nodes.  */
116
117 #ifdef GATHER_STATISTICS
118 void
119 phinodes_print_statistics (void)
120 {
121   fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
122   fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
123 }
124 #endif
125
126 /* Given LEN, the original number of requested PHI arguments, return
127    a new, "ideal" length for the PHI node.  The "ideal" length rounds
128    the total size of the PHI node up to the next power of two bytes.
129
130    Rounding up will not result in wasting any memory since the size request
131    will be rounded up by the GC system anyway.  [ Note this is not entirely
132    true since the original length might have fit on one of the special
133    GC pages. ]  By rounding up, we may avoid the need to reallocate the
134    PHI node later if we increase the number of arguments for the PHI.  */
135
136 static int
137 ideal_phi_node_len (int len)
138 {
139   size_t size, new_size;
140   int log2, new_len;
141
142   /* We do not support allocations of less than two PHI argument slots.  */
143   if (len < 2)
144     len = 2;
145
146   /* Compute the number of bytes of the original request.  */
147   size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
148
149   /* Round it up to the next power of two.  */
150   log2 = ceil_log2 (size);
151   new_size = 1 << log2;
152   
153   /* Now compute and return the number of PHI argument slots given an 
154      ideal size allocation.  */
155   new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
156   return new_len;
157 }
158
159 /* Return a PHI node for variable VAR defined in statement STMT.
160    STMT may be an empty statement for artificial references (e.g., default
161    definitions created when a variable is used without a preceding
162    definition).  */
163
164 tree
165 make_phi_node (tree var, int len)
166 {
167   tree phi;
168   int size;
169   int bucket = NUM_BUCKETS - 2;
170
171   len = ideal_phi_node_len (len);
172
173   size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
174
175   if (free_phinode_count)
176     for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
177       if (free_phinodes[bucket])
178         break;
179
180   /* If our free list has an element, then use it.  */
181   if (bucket < NUM_BUCKETS - 2
182       && PHI_ARG_CAPACITY (free_phinodes[bucket]) >= len)
183     {
184       free_phinode_count--;
185       phi = free_phinodes[bucket];
186       free_phinodes[bucket] = TREE_CHAIN (free_phinodes[bucket]);
187 #ifdef GATHER_STATISTICS
188       phi_nodes_reused++;
189 #endif
190     }
191   else
192     {
193       phi = ggc_alloc (size);
194 #ifdef GATHER_STATISTICS
195       phi_nodes_created++;
196       tree_node_counts[(int) phi_kind]++;
197       tree_node_sizes[(int) phi_kind] += size;
198 #endif
199
200     }
201
202   memset (phi, 0, size);
203   TREE_SET_CODE (phi, PHI_NODE);
204   PHI_ARG_CAPACITY (phi) = len;
205   if (TREE_CODE (var) == SSA_NAME)
206     PHI_RESULT (phi) = var;
207   else
208     PHI_RESULT (phi) = make_ssa_name (var, phi);
209
210   return phi;
211 }
212
213 /* We no longer need PHI, release it so that it may be reused.  */
214
215 void
216 release_phi_node (tree phi)
217 {
218   int bucket;
219   int len = PHI_ARG_CAPACITY (phi);
220
221   bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
222   bucket -= 2;
223   TREE_CHAIN (phi) = free_phinodes[bucket];
224   free_phinodes[bucket] = phi;
225   free_phinode_count++;
226 }
227
228 /* Resize an existing PHI node.  The only way is up.  Return the
229    possibly relocated phi.  */
230                                                                                 
231 static void
232 resize_phi_node (tree *phi, int len)
233 {
234   int size, old_size;
235   tree new_phi;
236   int i, old_len, bucket = NUM_BUCKETS - 2;
237                                                                                 
238 #ifdef ENABLE_CHECKING
239   if (len < PHI_ARG_CAPACITY (*phi))
240     abort ();
241 #endif
242                                                                                 
243   /* Note that OLD_SIZE is guaranteed to be smaller than SIZE.  */
244   old_size = (sizeof (struct tree_phi_node)
245              + (PHI_ARG_CAPACITY (*phi) - 1) * sizeof (struct phi_arg_d));
246   size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
247
248   if (free_phinode_count)
249     for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
250       if (free_phinodes[bucket])
251         break;
252
253   /* If our free list has an element, then use it.  */
254   if (bucket < NUM_BUCKETS - 2
255       && PHI_ARG_CAPACITY (free_phinodes[bucket]) >= len)
256     {
257       free_phinode_count--;
258       new_phi = free_phinodes[bucket];
259       free_phinodes[bucket] = TREE_CHAIN (free_phinodes[bucket]);
260 #ifdef GATHER_STATISTICS
261       phi_nodes_reused++;
262 #endif
263     }
264   else
265     {
266       new_phi = ggc_alloc (size);
267 #ifdef GATHER_STATISTICS
268       phi_nodes_created++;
269       tree_node_counts[(int) phi_kind]++;
270       tree_node_sizes[(int) phi_kind] += size;
271 #endif
272     }
273
274   memcpy (new_phi, *phi, old_size);
275
276   old_len = PHI_ARG_CAPACITY (new_phi);
277   PHI_ARG_CAPACITY (new_phi) = len;
278                                                                                 
279   for (i = old_len; i < len; i++)
280     {
281       PHI_ARG_DEF (new_phi, i) = NULL_TREE;
282       PHI_ARG_EDGE (new_phi, i) = NULL;
283       PHI_ARG_NONZERO (new_phi, i) = false;
284     }
285
286   *phi = new_phi;
287 }
288
289 /* Create a new PHI node for variable VAR at basic block BB.  */
290
291 tree
292 create_phi_node (tree var, basic_block bb)
293 {
294   tree phi;
295
296   phi = make_phi_node (var, bb_ann (bb)->num_preds);
297
298   /* This is a new phi node, so note that is has not yet been
299      rewritten. */
300   PHI_REWRITTEN (phi) = 0;
301
302   /* Add the new PHI node to the list of PHI nodes for block BB.  */
303   TREE_CHAIN (phi) = phi_nodes (bb);
304   bb_ann (bb)->phi_nodes = phi;
305
306   /* Associate BB to the PHI node.  */
307   set_bb_for_stmt (phi, bb);
308
309   return phi;
310 }
311
312 /* Add a new argument to PHI node PHI.  DEF is the incoming reaching
313    definition and E is the edge through which DEF reaches PHI.  The new
314    argument is added at the end of the argument list.
315    If PHI has reached its maximum capacity, add a few slots.  In this case,
316    PHI points to the reallocated phi node when we return.  */
317
318 void
319 add_phi_arg (tree *phi, tree def, edge e)
320 {
321   int i = PHI_NUM_ARGS (*phi);
322
323   if (i >= PHI_ARG_CAPACITY (*phi))
324     {
325       tree old_phi = *phi;
326
327       /* Resize the phi.  Unfortunately, this may also relocate it.  */
328       resize_phi_node (phi, ideal_phi_node_len (i + 4));
329
330       /* The result of the phi is defined by this phi node.  */
331       SSA_NAME_DEF_STMT (PHI_RESULT (*phi)) = *phi;
332
333       /* If the PHI was relocated, update the PHI chains appropriately and
334          release the old PHI node.  */
335       if (*phi != old_phi)
336         {
337           release_phi_node (old_phi);
338
339           /* Update the list head if replacing the first listed phi.  */
340           if (phi_nodes (e->dest) == old_phi)
341             bb_ann (e->dest)->phi_nodes = *phi;
342           else
343             {
344               /* Traverse the list looking for the phi node to chain to.  */
345               tree p;
346
347               for (p = phi_nodes (e->dest);
348                    p && TREE_CHAIN (p) != old_phi;
349                    p = TREE_CHAIN (p))
350                 ;
351
352               if (!p)
353                 abort ();
354
355               TREE_CHAIN (p) = *phi;
356             }
357         }
358     }
359
360   /* Copy propagation needs to know what object occur in abnormal
361      PHI nodes.  This is a convenient place to record such information.  */
362   if (e->flags & EDGE_ABNORMAL)
363     {
364       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
365       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (*phi)) = 1;
366     }
367
368   PHI_ARG_DEF (*phi, i) = def;
369   PHI_ARG_EDGE (*phi, i) = e;
370   PHI_ARG_NONZERO (*phi, i) = false;
371   PHI_NUM_ARGS (*phi)++;
372 }
373
374 /* Remove a PHI argument from PHI.  BLOCK is the predecessor block where
375    the PHI argument is coming from.  */
376
377 void
378 remove_phi_arg (tree phi, basic_block block)
379 {
380   int i, num_elem = PHI_NUM_ARGS (phi);
381
382   for (i = 0; i < num_elem; i++)
383     {
384       basic_block src_bb;
385
386       src_bb = PHI_ARG_EDGE (phi, i)->src;
387
388       if (src_bb == block)
389         {
390           remove_phi_arg_num (phi, i);
391           return;
392         }
393     }
394 }
395
396
397 /* Remove the Ith argument from PHI's argument list.  This routine assumes
398    ordering of alternatives in the vector is not important and implements
399    removal by swapping the last alternative with the alternative we want to
400    delete, then shrinking the vector.  */
401
402 void
403 remove_phi_arg_num (tree phi, int i)
404 {
405   int num_elem = PHI_NUM_ARGS (phi);
406
407   /* If we are not at the last element, switch the last element
408      with the element we want to delete.  */
409   if (i != num_elem - 1)
410     {
411       PHI_ARG_DEF (phi, i) = PHI_ARG_DEF (phi, num_elem - 1);
412       PHI_ARG_EDGE (phi, i) = PHI_ARG_EDGE (phi, num_elem - 1);
413       PHI_ARG_NONZERO (phi, i) = PHI_ARG_NONZERO (phi, num_elem - 1);
414     }
415
416   /* Shrink the vector and return.  */
417   PHI_ARG_DEF (phi, num_elem - 1) = NULL_TREE;
418   PHI_ARG_EDGE (phi, num_elem - 1) = NULL;
419   PHI_ARG_NONZERO (phi, num_elem - 1) = false;
420   PHI_NUM_ARGS (phi)--;
421
422   /* If we removed the last PHI argument, then go ahead and
423      remove the PHI node.  */
424   if (PHI_NUM_ARGS (phi) == 0)
425     remove_phi_node (phi, NULL, bb_for_stmt (phi));
426 }
427
428 /* Remove PHI node PHI from basic block BB.  If PREV is non-NULL, it is
429    used as the node immediately before PHI in the linked list.  */
430
431 void
432 remove_phi_node (tree phi, tree prev, basic_block bb)
433 {
434   if (prev)
435     {
436       /* Rewire the list if we are given a PREV pointer.  */
437       TREE_CHAIN (prev) = TREE_CHAIN (phi);
438
439       /* If we are deleting the PHI node, then we should release the
440          SSA_NAME node so that it can be reused.  */
441       release_ssa_name (PHI_RESULT (phi));
442       release_phi_node (phi);
443     }
444   else if (phi == phi_nodes (bb))
445     {
446       /* Update the list head if removing the first element.  */
447       bb_ann (bb)->phi_nodes = TREE_CHAIN (phi);
448
449       /* If we are deleting the PHI node, then we should release the
450          SSA_NAME node so that it can be reused.  */
451       release_ssa_name (PHI_RESULT (phi));
452       release_phi_node (phi);
453     }
454   else
455     {
456       /* Traverse the list looking for the node to remove.  */
457       tree prev, t;
458       prev = NULL_TREE;
459       for (t = phi_nodes (bb); t && t != phi; t = TREE_CHAIN (t))
460         prev = t;
461       if (t)
462         remove_phi_node (t, prev, bb);
463     }
464 }
465
466
467 /* Remove all the PHI nodes for variables in the VARS bitmap.  */
468
469 void
470 remove_all_phi_nodes_for (bitmap vars)
471 {
472   basic_block bb;
473
474   FOR_EACH_BB (bb)
475     {
476       /* Build a new PHI list for BB without variables in VARS.  */
477       tree phi, new_phi_list, last_phi, next;
478
479       last_phi = new_phi_list = NULL_TREE;
480       for (phi = phi_nodes (bb), next = NULL; phi; phi = next)
481         {
482           tree var = SSA_NAME_VAR (PHI_RESULT (phi));
483
484           next = TREE_CHAIN (phi);
485           /* Only add PHI nodes for variables not in VARS.  */
486           if (!bitmap_bit_p (vars, var_ann (var)->uid))
487             {
488               /* If we're not removing this PHI node, then it must have
489                  been rewritten by a previous call into the SSA rewriter.
490                  Note that fact in PHI_REWRITTEN.  */
491               PHI_REWRITTEN (phi) = 1;
492
493               if (new_phi_list == NULL_TREE)
494                 new_phi_list = last_phi = phi;
495               else
496                 {
497                   TREE_CHAIN (last_phi) = phi;
498                   last_phi = phi;
499                 }
500             }
501           else
502             {
503               /* If we are deleting the PHI node, then we should release the
504                  SSA_NAME node so that it can be reused.  */
505               release_ssa_name (PHI_RESULT (phi));
506               release_phi_node (phi);
507             }
508         }
509
510       /* Make sure the last node in the new list has no successors.  */
511       if (last_phi)
512         TREE_CHAIN (last_phi) = NULL_TREE;
513       bb_ann (bb)->phi_nodes = new_phi_list;
514
515 #if defined ENABLE_CHECKING
516       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
517         {
518           tree var = SSA_NAME_VAR (PHI_RESULT (phi));
519           if (bitmap_bit_p (vars, var_ann (var)->uid))
520             abort ();
521         }
522 #endif
523     }
524 }
525
526
527 #include "gt-tree-phinodes.h"
528