* arm.h (TARGET_CPU_CPP_BUILTINS): Remove Maverick support.
[official-gcc.git] / gcc / tree-phinodes.c
blob6408a52529806c8f9bb7b1d8ca58f09602e34ed5
1 /* Generic routines for manipulating PHIs
2 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "ggc.h"
27 #include "basic-block.h"
28 #include "tree-flow.h"
29 #include "diagnostic-core.h"
30 #include "gimple.h"
32 /* Rewriting a function into SSA form can create a huge number of PHIs
33 many of which may be thrown away shortly after their creation if jumps
34 were threaded through PHI nodes.
36 While our garbage collection mechanisms will handle this situation, it
37 is extremely wasteful to create nodes and throw them away, especially
38 when the nodes can be reused.
40 For PR 8361, we can significantly reduce the number of nodes allocated
41 and thus the total amount of memory allocated by managing PHIs a
42 little. This additionally helps reduce the amount of work done by the
43 garbage collector. Similar results have been seen on a wider variety
44 of tests (such as the compiler itself).
46 We could also use a zone allocator for these objects since they have
47 a very well defined lifetime. If someone wants to experiment with that
48 this is the place to try it.
50 PHI nodes have different sizes, so we can't have a single list of all
51 the PHI nodes as it would be too expensive to walk down that list to
52 find a PHI of a suitable size.
54 Instead we have an array of lists of free PHI nodes. The array is
55 indexed by the number of PHI alternatives that PHI node can hold.
56 Except for the last array member, which holds all remaining PHI
57 nodes.
59 So to find a free PHI node, we compute its index into the free PHI
60 node array and see if there are any elements with an exact match.
61 If so, then we are done. Otherwise, we test the next larger size
62 up and continue until we are in the last array element.
64 We do not actually walk members of the last array element. While it
65 might allow us to pick up a few reusable PHI nodes, it could potentially
66 be very expensive if the program has released a bunch of large PHI nodes,
67 but keeps asking for even larger PHI nodes. Experiments have shown that
68 walking the elements of the last array entry would result in finding less
69 than .1% additional reusable PHI nodes.
71 Note that we can never have less than two PHI argument slots. Thus,
72 the -2 on all the calculations below. */
74 #define NUM_BUCKETS 10
75 static GTY ((deletable (""))) VEC(gimple,gc) *free_phinodes[NUM_BUCKETS - 2];
76 static unsigned long free_phinode_count;
78 static int ideal_phi_node_len (int);
80 #ifdef GATHER_STATISTICS
81 unsigned int phi_nodes_reused;
82 unsigned int phi_nodes_created;
83 #endif
85 /* Dump some simple statistics regarding the re-use of PHI nodes. */
87 #ifdef GATHER_STATISTICS
88 void
89 phinodes_print_statistics (void)
91 fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
92 fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
94 #endif
96 /* Allocate a PHI node with at least LEN arguments. If the free list
97 happens to contain a PHI node with LEN arguments or more, return
98 that one. */
100 static inline gimple
101 allocate_phi_node (size_t len)
103 gimple phi;
104 size_t bucket = NUM_BUCKETS - 2;
105 size_t size = sizeof (struct gimple_statement_phi)
106 + (len - 1) * sizeof (struct phi_arg_d);
108 if (free_phinode_count)
109 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
110 if (free_phinodes[bucket])
111 break;
113 /* If our free list has an element, then use it. */
114 if (bucket < NUM_BUCKETS - 2
115 && gimple_phi_capacity (VEC_index (gimple, free_phinodes[bucket], 0))
116 >= len)
118 free_phinode_count--;
119 phi = VEC_pop (gimple, free_phinodes[bucket]);
120 if (VEC_empty (gimple, free_phinodes[bucket]))
121 VEC_free (gimple, gc, free_phinodes[bucket]);
122 #ifdef GATHER_STATISTICS
123 phi_nodes_reused++;
124 #endif
126 else
128 phi = ggc_alloc_gimple_statement_d (size);
129 #ifdef GATHER_STATISTICS
130 phi_nodes_created++;
132 enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
133 gimple_alloc_counts[(int) kind]++;
134 gimple_alloc_sizes[(int) kind] += size;
136 #endif
139 return phi;
142 /* Given LEN, the original number of requested PHI arguments, return
143 a new, "ideal" length for the PHI node. The "ideal" length rounds
144 the total size of the PHI node up to the next power of two bytes.
146 Rounding up will not result in wasting any memory since the size request
147 will be rounded up by the GC system anyway. [ Note this is not entirely
148 true since the original length might have fit on one of the special
149 GC pages. ] By rounding up, we may avoid the need to reallocate the
150 PHI node later if we increase the number of arguments for the PHI. */
152 static int
153 ideal_phi_node_len (int len)
155 size_t size, new_size;
156 int log2, new_len;
158 /* We do not support allocations of less than two PHI argument slots. */
159 if (len < 2)
160 len = 2;
162 /* Compute the number of bytes of the original request. */
163 size = sizeof (struct gimple_statement_phi)
164 + (len - 1) * sizeof (struct phi_arg_d);
166 /* Round it up to the next power of two. */
167 log2 = ceil_log2 (size);
168 new_size = 1 << log2;
170 /* Now compute and return the number of PHI argument slots given an
171 ideal size allocation. */
172 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
173 return new_len;
176 /* Return a PHI node with LEN argument slots for variable VAR. */
178 static gimple
179 make_phi_node (tree var, int len)
181 gimple phi;
182 int capacity, i;
184 capacity = ideal_phi_node_len (len);
186 phi = allocate_phi_node (capacity);
188 /* We need to clear the entire PHI node, including the argument
189 portion, because we represent a "missing PHI argument" by placing
190 NULL_TREE in PHI_ARG_DEF. */
191 memset (phi, 0, (sizeof (struct gimple_statement_phi)
192 - sizeof (struct phi_arg_d)
193 + sizeof (struct phi_arg_d) * len));
194 phi->gsbase.code = GIMPLE_PHI;
195 gimple_init_singleton (phi);
196 phi->gimple_phi.nargs = len;
197 phi->gimple_phi.capacity = capacity;
198 if (TREE_CODE (var) == SSA_NAME)
199 gimple_phi_set_result (phi, var);
200 else
201 gimple_phi_set_result (phi, make_ssa_name (var, phi));
203 for (i = 0; i < capacity; i++)
205 use_operand_p imm;
207 gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
208 imm = gimple_phi_arg_imm_use_ptr (phi, i);
209 imm->use = gimple_phi_arg_def_ptr (phi, i);
210 imm->prev = NULL;
211 imm->next = NULL;
212 imm->loc.stmt = phi;
215 return phi;
218 /* We no longer need PHI, release it so that it may be reused. */
220 void
221 release_phi_node (gimple phi)
223 size_t bucket;
224 size_t len = gimple_phi_capacity (phi);
225 size_t x;
227 for (x = 0; x < gimple_phi_num_args (phi); x++)
229 use_operand_p imm;
230 imm = gimple_phi_arg_imm_use_ptr (phi, x);
231 delink_imm_use (imm);
234 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
235 bucket -= 2;
236 VEC_safe_push (gimple, gc, free_phinodes[bucket], phi);
237 free_phinode_count++;
241 /* Resize an existing PHI node. The only way is up. Return the
242 possibly relocated phi. */
244 static gimple
245 resize_phi_node (gimple phi, size_t len)
247 size_t old_size, i;
248 gimple new_phi;
250 gcc_assert (len > gimple_phi_capacity (phi));
252 /* The garbage collector will not look at the PHI node beyond the
253 first PHI_NUM_ARGS elements. Therefore, all we have to copy is a
254 portion of the PHI node currently in use. */
255 old_size = sizeof (struct gimple_statement_phi)
256 + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
258 new_phi = allocate_phi_node (len);
260 memcpy (new_phi, phi, old_size);
262 for (i = 0; i < gimple_phi_num_args (new_phi); i++)
264 use_operand_p imm, old_imm;
265 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
266 old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
267 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
268 relink_imm_use_stmt (imm, old_imm, new_phi);
271 new_phi->gimple_phi.capacity = len;
273 for (i = gimple_phi_num_args (new_phi); i < len; i++)
275 use_operand_p imm;
277 gimple_phi_arg_set_location (new_phi, i, UNKNOWN_LOCATION);
278 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
279 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
280 imm->prev = NULL;
281 imm->next = NULL;
282 imm->loc.stmt = new_phi;
285 return new_phi;
288 /* Reserve PHI arguments for a new edge to basic block BB. */
290 void
291 reserve_phi_args_for_new_edge (basic_block bb)
293 size_t len = EDGE_COUNT (bb->preds);
294 size_t cap = ideal_phi_node_len (len + 4);
295 gimple_stmt_iterator gsi;
297 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
299 gimple stmt = gsi_stmt (gsi);
301 if (len > gimple_phi_capacity (stmt))
303 gimple new_phi = resize_phi_node (stmt, cap);
305 /* The result of the PHI is defined by this PHI node. */
306 SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
307 gsi_set_stmt (&gsi, new_phi);
309 release_phi_node (stmt);
310 stmt = new_phi;
313 /* We represent a "missing PHI argument" by placing NULL_TREE in
314 the corresponding slot. If PHI arguments were added
315 immediately after an edge is created, this zeroing would not
316 be necessary, but unfortunately this is not the case. For
317 example, the loop optimizer duplicates several basic blocks,
318 redirects edges, and then fixes up PHI arguments later in
319 batch. */
320 SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
322 stmt->gimple_phi.nargs++;
326 /* Adds PHI to BB. */
328 void
329 add_phi_node_to_bb (gimple phi, basic_block bb)
331 gimple_seq seq = phi_nodes (bb);
332 /* Add the new PHI node to the list of PHI nodes for block BB. */
333 if (seq == NULL)
334 set_phi_nodes (bb, gimple_seq_alloc_with_stmt (phi));
335 else
337 gimple_seq_add_stmt (&seq, phi);
338 gcc_assert (seq == phi_nodes (bb));
341 /* Associate BB to the PHI node. */
342 gimple_set_bb (phi, bb);
346 /* Create a new PHI node for variable VAR at basic block BB. */
348 gimple
349 create_phi_node (tree var, basic_block bb)
351 gimple phi = make_phi_node (var, EDGE_COUNT (bb->preds));
353 add_phi_node_to_bb (phi, bb);
354 return phi;
358 /* Add a new argument to PHI node PHI. DEF is the incoming reaching
359 definition and E is the edge through which DEF reaches PHI. The new
360 argument is added at the end of the argument list.
361 If PHI has reached its maximum capacity, add a few slots. In this case,
362 PHI points to the reallocated phi node when we return. */
364 void
365 add_phi_arg (gimple phi, tree def, edge e, source_location locus)
367 basic_block bb = e->dest;
369 gcc_assert (bb == gimple_bb (phi));
371 /* We resize PHI nodes upon edge creation. We should always have
372 enough room at this point. */
373 gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
375 /* We resize PHI nodes upon edge creation. We should always have
376 enough room at this point. */
377 gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
379 /* Copy propagation needs to know what object occur in abnormal
380 PHI nodes. This is a convenient place to record such information. */
381 if (e->flags & EDGE_ABNORMAL)
383 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
384 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
387 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
388 gimple_phi_arg_set_location (phi, e->dest_idx, locus);
392 /* Remove the Ith argument from PHI's argument list. This routine
393 implements removal by swapping the last alternative with the
394 alternative we want to delete and then shrinking the vector, which
395 is consistent with how we remove an edge from the edge vector. */
397 static void
398 remove_phi_arg_num (gimple phi, int i)
400 int num_elem = gimple_phi_num_args (phi);
402 gcc_assert (i < num_elem);
404 /* Delink the item which is being removed. */
405 delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
407 /* If it is not the last element, move the last element
408 to the element we want to delete, resetting all the links. */
409 if (i != num_elem - 1)
411 use_operand_p old_p, new_p;
412 old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
413 new_p = gimple_phi_arg_imm_use_ptr (phi, i);
414 /* Set use on new node, and link into last element's place. */
415 *(new_p->use) = *(old_p->use);
416 relink_imm_use (new_p, old_p);
417 /* Move the location as well. */
418 gimple_phi_arg_set_location (phi, i,
419 gimple_phi_arg_location (phi, num_elem - 1));
422 /* Shrink the vector and return. Note that we do not have to clear
423 PHI_ARG_DEF because the garbage collector will not look at those
424 elements beyond the first PHI_NUM_ARGS elements of the array. */
425 phi->gimple_phi.nargs--;
429 /* Remove all PHI arguments associated with edge E. */
431 void
432 remove_phi_args (edge e)
434 gimple_stmt_iterator gsi;
436 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
437 remove_phi_arg_num (gsi_stmt (gsi), 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 #include "gt-tree-phinodes.h"