OSDN Git Service

* tree-phinodes.c (remove_phi_arg_num): Do not zero the
[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 /* 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.  */
129
130 static inline tree
131 allocate_phi_node (int len)
132 {
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));
137
138   if (free_phinode_count)
139     for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
140       if (free_phinodes[bucket])
141         break;
142
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)
146     {
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
153     }
154   else
155     {
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
162     }
163
164   return phi;
165 }
166
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.
170
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.  */
176
177 static int
178 ideal_phi_node_len (int len)
179 {
180   size_t size, new_size;
181   int log2, new_len;
182
183   /* We do not support allocations of less than two PHI argument slots.  */
184   if (len < 2)
185     len = 2;
186
187   /* Compute the number of bytes of the original request.  */
188   size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
189
190   /* Round it up to the next power of two.  */
191   log2 = ceil_log2 (size);
192   new_size = 1 << log2;
193
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;
198 }
199
200 /* Return a PHI node for variable VAR defined in statement STMT.
201    STMT may be an empty statement for artificial references (e.g., default
202    definitions created when a variable is used without a preceding
203    definition).  */
204
205 tree
206 make_phi_node (tree var, int len)
207 {
208   tree phi;
209
210   len = ideal_phi_node_len (len);
211
212   phi = allocate_phi_node (len);
213
214   /* We do not have to clear a part of the PHI node that stores PHI
215      arguments, which is safe because we tell the garbage collector to
216      scan up to num_args elements in the array of PHI arguments.  In
217      other words, the garbage collector will not follow garbage
218      pointers in the unused portion of the array.  */
219   memset (phi, 0, sizeof (struct tree_phi_node) - sizeof (struct phi_arg_d));
220   TREE_SET_CODE (phi, PHI_NODE);
221   PHI_ARG_CAPACITY (phi) = len;
222   TREE_TYPE (phi) = TREE_TYPE (var);
223   if (TREE_CODE (var) == SSA_NAME)
224     SET_PHI_RESULT (phi, var);
225   else
226     SET_PHI_RESULT (phi, make_ssa_name (var, phi));
227
228   return phi;
229 }
230
231 /* We no longer need PHI, release it so that it may be reused.  */
232
233 void
234 release_phi_node (tree phi)
235 {
236   int bucket;
237   int len = PHI_ARG_CAPACITY (phi);
238
239   bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
240   bucket -= 2;
241   PHI_CHAIN (phi) = free_phinodes[bucket];
242   free_phinodes[bucket] = phi;
243   free_phinode_count++;
244 }
245
246 /* Resize an existing PHI node.  The only way is up.  Return the
247    possibly relocated phi.  */
248
249 static void
250 resize_phi_node (tree *phi, int len)
251 {
252   int old_size;
253   tree new_phi;
254
255   gcc_assert (len >= PHI_ARG_CAPACITY (*phi));
256
257   /* Note that OLD_SIZE is guaranteed to be smaller than SIZE.  */
258   old_size = (sizeof (struct tree_phi_node)
259              + (PHI_ARG_CAPACITY (*phi) - 1) * sizeof (struct phi_arg_d));
260
261   new_phi = allocate_phi_node (len);
262
263   memcpy (new_phi, *phi, old_size);
264
265   PHI_ARG_CAPACITY (new_phi) = len;
266
267   *phi = new_phi;
268 }
269
270 /* Create a new PHI node for variable VAR at basic block BB.  */
271
272 tree
273 create_phi_node (tree var, basic_block bb)
274 {
275   tree phi;
276
277   phi = make_phi_node (var, EDGE_COUNT (bb->preds));
278
279   /* Add the new PHI node to the list of PHI nodes for block BB.  */
280   PHI_CHAIN (phi) = phi_nodes (bb);
281   bb_ann (bb)->phi_nodes = phi;
282
283   /* Associate BB to the PHI node.  */
284   set_bb_for_stmt (phi, bb);
285
286   return phi;
287 }
288
289 /* Add a new argument to PHI node PHI.  DEF is the incoming reaching
290    definition and E is the edge through which DEF reaches PHI.  The new
291    argument is added at the end of the argument list.
292    If PHI has reached its maximum capacity, add a few slots.  In this case,
293    PHI points to the reallocated phi node when we return.  */
294
295 void
296 add_phi_arg (tree *phi, tree def, edge e)
297 {
298   int i = PHI_NUM_ARGS (*phi);
299
300   if (i >= PHI_ARG_CAPACITY (*phi))
301     {
302       tree old_phi = *phi;
303       basic_block bb;
304
305       /* Resize the phi.  Unfortunately, this will relocate it.  */
306       resize_phi_node (phi, ideal_phi_node_len (i + 4));
307
308       /* resize_phi_node will necessarily relocate the phi.  */
309       gcc_assert (*phi != old_phi);
310
311       /* The result of the phi is defined by this phi node.  */
312       SSA_NAME_DEF_STMT (PHI_RESULT (*phi)) = *phi;
313
314       /* Extract the basic block for the PHI from the PHI's annotation
315          rather than the edge.  This works better as the edge's
316          destination may not currently be the block with the PHI node
317          if we are in the process of threading the edge to a new
318          destination.  */
319       bb = bb_for_stmt (*phi);
320
321       release_phi_node (old_phi);
322
323       /* Update the list head if replacing the first listed phi.  */
324       if (phi_nodes (bb) == old_phi)
325         bb_ann (bb)->phi_nodes = *phi;
326       else
327         {
328           /* Traverse the list looking for the phi node to chain to.  */
329           tree p;
330
331           for (p = phi_nodes (bb);
332                p && PHI_CHAIN (p) != old_phi;
333                p = PHI_CHAIN (p))
334             ;
335
336           gcc_assert (p);
337           PHI_CHAIN (p) = *phi;
338         }
339     }
340
341   /* Copy propagation needs to know what object occur in abnormal
342      PHI nodes.  This is a convenient place to record such information.  */
343   if (e->flags & EDGE_ABNORMAL)
344     {
345       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
346       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (*phi)) = 1;
347     }
348
349   SET_PHI_ARG_DEF (*phi, i, def);
350   PHI_ARG_EDGE (*phi, i) = e;
351   PHI_ARG_NONZERO (*phi, i) = false;
352   PHI_NUM_ARGS (*phi)++;
353 }
354
355 /* Remove a PHI argument from PHI.  BLOCK is the predecessor block where
356    the PHI argument is coming from.  */
357
358 void
359 remove_phi_arg (tree phi, basic_block block)
360 {
361   int i, num_elem = PHI_NUM_ARGS (phi);
362
363   for (i = 0; i < num_elem; i++)
364     {
365       basic_block src_bb;
366
367       src_bb = PHI_ARG_EDGE (phi, i)->src;
368
369       if (src_bb == block)
370         {
371           remove_phi_arg_num (phi, i);
372           return;
373         }
374     }
375 }
376
377
378 /* Remove the Ith argument from PHI's argument list.  This routine assumes
379    ordering of alternatives in the vector is not important and implements
380    removal by swapping the last alternative with the alternative we want to
381    delete, then shrinking the vector.  */
382
383 void
384 remove_phi_arg_num (tree phi, int i)
385 {
386   int num_elem = PHI_NUM_ARGS (phi);
387
388   gcc_assert (i < num_elem);
389
390   /* If we are not at the last element, switch the last element
391      with the element we want to delete.  */
392   if (i != num_elem - 1)
393     {
394       SET_PHI_ARG_DEF (phi, i, PHI_ARG_DEF (phi, num_elem - 1));
395       PHI_ARG_EDGE (phi, i) = PHI_ARG_EDGE (phi, num_elem - 1);
396       PHI_ARG_NONZERO (phi, i) = PHI_ARG_NONZERO (phi, num_elem - 1);
397     }
398
399   /* Shrink the vector and return.  Note that we do not have to clear
400      PHI_ARG_DEF, PHI_ARG_EDGE, or PHI_ARG_NONZERO because the garbage
401      collector will not look at those elements beyond the first
402      PHI_NUM_ARGS elements of the array.  */
403   PHI_NUM_ARGS (phi)--;
404 }
405
406 /* Remove PHI node PHI from basic block BB.  If PREV is non-NULL, it is
407    used as the node immediately before PHI in the linked list.  */
408
409 void
410 remove_phi_node (tree phi, tree prev, basic_block bb)
411 {
412   if (prev)
413     {
414       /* Rewire the list if we are given a PREV pointer.  */
415       PHI_CHAIN (prev) = PHI_CHAIN (phi);
416
417       /* If we are deleting the PHI node, then we should release the
418          SSA_NAME node so that it can be reused.  */
419       release_ssa_name (PHI_RESULT (phi));
420       release_phi_node (phi);
421     }
422   else if (phi == phi_nodes (bb))
423     {
424       /* Update the list head if removing the first element.  */
425       bb_ann (bb)->phi_nodes = PHI_CHAIN (phi);
426
427       /* If we are deleting the PHI node, then we should release the
428          SSA_NAME node so that it can be reused.  */
429       release_ssa_name (PHI_RESULT (phi));
430       release_phi_node (phi);
431     }
432   else
433     {
434       /* Traverse the list looking for the node to remove.  */
435       tree prev, t;
436       prev = NULL_TREE;
437       for (t = phi_nodes (bb); t && t != phi; t = PHI_CHAIN (t))
438         prev = t;
439       if (t)
440         remove_phi_node (t, prev, bb);
441     }
442 }
443
444
445 /* Remove all the PHI nodes for variables in the VARS bitmap.  */
446
447 void
448 remove_all_phi_nodes_for (bitmap vars)
449 {
450   basic_block bb;
451
452   FOR_EACH_BB (bb)
453     {
454       /* Build a new PHI list for BB without variables in VARS.  */
455       tree phi, new_phi_list, last_phi, next;
456
457       last_phi = new_phi_list = NULL_TREE;
458       for (phi = phi_nodes (bb), next = NULL; phi; phi = next)
459         {
460           tree var = SSA_NAME_VAR (PHI_RESULT (phi));
461
462           next = PHI_CHAIN (phi);
463           /* Only add PHI nodes for variables not in VARS.  */
464           if (!bitmap_bit_p (vars, var_ann (var)->uid))
465             {
466               /* If we're not removing this PHI node, then it must have
467                  been rewritten by a previous call into the SSA rewriter.
468                  Note that fact in PHI_REWRITTEN.  */
469               PHI_REWRITTEN (phi) = 1;
470
471               if (new_phi_list == NULL_TREE)
472                 new_phi_list = last_phi = phi;
473               else
474                 {
475                   PHI_CHAIN (last_phi) = phi;
476                   last_phi = phi;
477                 }
478             }
479           else
480             {
481               /* If we are deleting the PHI node, then we should release the
482                  SSA_NAME node so that it can be reused.  */
483               release_ssa_name (PHI_RESULT (phi));
484               release_phi_node (phi);
485             }
486         }
487
488       /* Make sure the last node in the new list has no successors.  */
489       if (last_phi)
490         PHI_CHAIN (last_phi) = NULL_TREE;
491       bb_ann (bb)->phi_nodes = new_phi_list;
492
493 #if defined ENABLE_CHECKING
494       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
495         {
496           tree var = SSA_NAME_VAR (PHI_RESULT (phi));
497           gcc_assert (!bitmap_bit_p (vars, var_ann (var)->uid));
498         }
499 #endif
500     }
501 }
502
503
504 #include "gt-tree-phinodes.h"
505