]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-phinodes.c
gimple-walk.h: New File.
[thirdparty/gcc.git] / gcc / tree-phinodes.c
1 /* Generic routines for manipulating PHIs
2 Copyright (C) 2003-2013 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
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 "tree-ssanames.h"
33 #include "tree-ssa.h"
34 #include "diagnostic-core.h"
35
36 /* Rewriting a function into SSA form can create a huge number of PHIs
37 many of which may be thrown away shortly after their creation if jumps
38 were threaded through PHI nodes.
39
40 While our garbage collection mechanisms will handle this situation, it
41 is extremely wasteful to create nodes and throw them away, especially
42 when the nodes can be reused.
43
44 For PR 8361, we can significantly reduce the number of nodes allocated
45 and thus the total amount of memory allocated by managing PHIs a
46 little. This additionally helps reduce the amount of work done by the
47 garbage collector. Similar results have been seen on a wider variety
48 of tests (such as the compiler itself).
49
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.
53
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.
58
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.
63
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.
70
71 Note that we can never have less than two PHI argument slots. Thus,
72 the -2 on all the calculations below. */
73
74 #define NUM_BUCKETS 10
75 static GTY ((deletable (""))) vec<gimple, va_gc> *free_phinodes[NUM_BUCKETS - 2];
76 static unsigned long free_phinode_count;
77
78 static int ideal_phi_node_len (int);
79
80 unsigned int phi_nodes_reused;
81 unsigned int phi_nodes_created;
82
83 /* Dump some simple statistics regarding the re-use of PHI nodes. */
84
85 void
86 phinodes_print_statistics (void)
87 {
88 fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
89 fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
90 }
91
92 /* Allocate a PHI node with at least LEN arguments. If the free list
93 happens to contain a PHI node with LEN arguments or more, return
94 that one. */
95
96 static inline gimple
97 allocate_phi_node (size_t len)
98 {
99 gimple phi;
100 size_t bucket = NUM_BUCKETS - 2;
101 size_t size = sizeof (struct gimple_statement_phi)
102 + (len - 1) * sizeof (struct phi_arg_d);
103
104 if (free_phinode_count)
105 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
106 if (free_phinodes[bucket])
107 break;
108
109 /* If our free list has an element, then use it. */
110 if (bucket < NUM_BUCKETS - 2
111 && gimple_phi_capacity ((*free_phinodes[bucket])[0]) >= len)
112 {
113 free_phinode_count--;
114 phi = free_phinodes[bucket]->pop ();
115 if (free_phinodes[bucket]->is_empty ())
116 vec_free (free_phinodes[bucket]);
117 if (GATHER_STATISTICS)
118 phi_nodes_reused++;
119 }
120 else
121 {
122 phi = ggc_alloc_gimple_statement_d (size);
123 if (GATHER_STATISTICS)
124 {
125 enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
126 phi_nodes_created++;
127 gimple_alloc_counts[(int) kind]++;
128 gimple_alloc_sizes[(int) kind] += size;
129 }
130 }
131
132 return phi;
133 }
134
135 /* Given LEN, the original number of requested PHI arguments, return
136 a new, "ideal" length for the PHI node. The "ideal" length rounds
137 the total size of the PHI node up to the next power of two bytes.
138
139 Rounding up will not result in wasting any memory since the size request
140 will be rounded up by the GC system anyway. [ Note this is not entirely
141 true since the original length might have fit on one of the special
142 GC pages. ] By rounding up, we may avoid the need to reallocate the
143 PHI node later if we increase the number of arguments for the PHI. */
144
145 static int
146 ideal_phi_node_len (int len)
147 {
148 size_t size, new_size;
149 int log2, new_len;
150
151 /* We do not support allocations of less than two PHI argument slots. */
152 if (len < 2)
153 len = 2;
154
155 /* Compute the number of bytes of the original request. */
156 size = sizeof (struct gimple_statement_phi)
157 + (len - 1) * sizeof (struct phi_arg_d);
158
159 /* Round it up to the next power of two. */
160 log2 = ceil_log2 (size);
161 new_size = 1 << log2;
162
163 /* Now compute and return the number of PHI argument slots given an
164 ideal size allocation. */
165 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
166 return new_len;
167 }
168
169 /* Return a PHI node with LEN argument slots for variable VAR. */
170
171 static gimple
172 make_phi_node (tree var, int len)
173 {
174 gimple phi;
175 int capacity, i;
176
177 capacity = ideal_phi_node_len (len);
178
179 phi = allocate_phi_node (capacity);
180
181 /* We need to clear the entire PHI node, including the argument
182 portion, because we represent a "missing PHI argument" by placing
183 NULL_TREE in PHI_ARG_DEF. */
184 memset (phi, 0, (sizeof (struct gimple_statement_phi)
185 - sizeof (struct phi_arg_d)
186 + sizeof (struct phi_arg_d) * len));
187 phi->gsbase.code = GIMPLE_PHI;
188 gimple_init_singleton (phi);
189 phi->gimple_phi.nargs = len;
190 phi->gimple_phi.capacity = capacity;
191 if (!var)
192 ;
193 else if (TREE_CODE (var) == SSA_NAME)
194 gimple_phi_set_result (phi, var);
195 else
196 gimple_phi_set_result (phi, make_ssa_name (var, phi));
197
198 for (i = 0; i < capacity; i++)
199 {
200 use_operand_p imm;
201
202 gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
203 imm = gimple_phi_arg_imm_use_ptr (phi, i);
204 imm->use = gimple_phi_arg_def_ptr (phi, i);
205 imm->prev = NULL;
206 imm->next = NULL;
207 imm->loc.stmt = phi;
208 }
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 (gimple phi)
217 {
218 size_t bucket;
219 size_t len = gimple_phi_capacity (phi);
220 size_t x;
221
222 for (x = 0; x < gimple_phi_num_args (phi); x++)
223 {
224 use_operand_p imm;
225 imm = gimple_phi_arg_imm_use_ptr (phi, x);
226 delink_imm_use (imm);
227 }
228
229 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
230 bucket -= 2;
231 vec_safe_push (free_phinodes[bucket], phi);
232 free_phinode_count++;
233 }
234
235
236 /* Resize an existing PHI node. The only way is up. Return the
237 possibly relocated phi. */
238
239 static gimple
240 resize_phi_node (gimple phi, size_t len)
241 {
242 size_t old_size, i;
243 gimple new_phi;
244
245 gcc_assert (len > gimple_phi_capacity (phi));
246
247 /* The garbage collector will not look at the PHI node beyond the
248 first PHI_NUM_ARGS elements. Therefore, all we have to copy is a
249 portion of the PHI node currently in use. */
250 old_size = sizeof (struct gimple_statement_phi)
251 + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
252
253 new_phi = allocate_phi_node (len);
254
255 memcpy (new_phi, phi, old_size);
256
257 for (i = 0; i < gimple_phi_num_args (new_phi); i++)
258 {
259 use_operand_p imm, old_imm;
260 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
261 old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
262 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
263 relink_imm_use_stmt (imm, old_imm, new_phi);
264 }
265
266 new_phi->gimple_phi.capacity = len;
267
268 for (i = gimple_phi_num_args (new_phi); i < len; i++)
269 {
270 use_operand_p imm;
271
272 gimple_phi_arg_set_location (new_phi, i, UNKNOWN_LOCATION);
273 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
274 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
275 imm->prev = NULL;
276 imm->next = NULL;
277 imm->loc.stmt = new_phi;
278 }
279
280 return new_phi;
281 }
282
283 /* Reserve PHI arguments for a new edge to basic block BB. */
284
285 void
286 reserve_phi_args_for_new_edge (basic_block bb)
287 {
288 size_t len = EDGE_COUNT (bb->preds);
289 size_t cap = ideal_phi_node_len (len + 4);
290 gimple_stmt_iterator gsi;
291
292 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
293 {
294 gimple stmt = gsi_stmt (gsi);
295
296 if (len > gimple_phi_capacity (stmt))
297 {
298 gimple new_phi = resize_phi_node (stmt, cap);
299
300 /* The result of the PHI is defined by this PHI node. */
301 SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
302 gsi_set_stmt (&gsi, new_phi);
303
304 release_phi_node (stmt);
305 stmt = new_phi;
306 }
307
308 /* We represent a "missing PHI argument" by placing NULL_TREE in
309 the corresponding slot. If PHI arguments were added
310 immediately after an edge is created, this zeroing would not
311 be necessary, but unfortunately this is not the case. For
312 example, the loop optimizer duplicates several basic blocks,
313 redirects edges, and then fixes up PHI arguments later in
314 batch. */
315 SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
316 gimple_phi_arg_set_location (stmt, len - 1, UNKNOWN_LOCATION);
317
318 stmt->gimple_phi.nargs++;
319 }
320 }
321
322 /* Adds PHI to BB. */
323
324 void
325 add_phi_node_to_bb (gimple phi, basic_block bb)
326 {
327 gimple_seq seq = phi_nodes (bb);
328 /* Add the new PHI node to the list of PHI nodes for block BB. */
329 if (seq == NULL)
330 set_phi_nodes (bb, gimple_seq_alloc_with_stmt (phi));
331 else
332 {
333 gimple_seq_add_stmt (&seq, phi);
334 gcc_assert (seq == phi_nodes (bb));
335 }
336
337 /* Associate BB to the PHI node. */
338 gimple_set_bb (phi, bb);
339
340 }
341
342 /* Create a new PHI node for variable VAR at basic block BB. */
343
344 gimple
345 create_phi_node (tree var, basic_block bb)
346 {
347 gimple phi = make_phi_node (var, EDGE_COUNT (bb->preds));
348
349 add_phi_node_to_bb (phi, bb);
350 return phi;
351 }
352
353
354 /* Add a new argument to PHI node PHI. DEF is the incoming reaching
355 definition and E is the edge through which DEF reaches PHI. The new
356 argument is added at the end of the argument list.
357 If PHI has reached its maximum capacity, add a few slots. In this case,
358 PHI points to the reallocated phi node when we return. */
359
360 void
361 add_phi_arg (gimple phi, tree def, edge e, source_location locus)
362 {
363 basic_block bb = e->dest;
364
365 gcc_assert (bb == gimple_bb (phi));
366
367 /* We resize PHI nodes upon edge creation. We should always have
368 enough room at this point. */
369 gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
370
371 /* We resize PHI nodes upon edge creation. We should always have
372 enough room at this point. */
373 gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
374
375 /* Copy propagation needs to know what object occur in abnormal
376 PHI nodes. This is a convenient place to record such information. */
377 if (e->flags & EDGE_ABNORMAL)
378 {
379 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
380 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
381 }
382
383 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
384 gimple_phi_arg_set_location (phi, e->dest_idx, locus);
385 }
386
387
388 /* Remove the Ith argument from PHI's argument list. This routine
389 implements removal by swapping the last alternative with the
390 alternative we want to delete and then shrinking the vector, which
391 is consistent with how we remove an edge from the edge vector. */
392
393 static void
394 remove_phi_arg_num (gimple phi, int i)
395 {
396 int num_elem = gimple_phi_num_args (phi);
397
398 gcc_assert (i < num_elem);
399
400 /* Delink the item which is being removed. */
401 delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
402
403 /* If it is not the last element, move the last element
404 to the element we want to delete, resetting all the links. */
405 if (i != num_elem - 1)
406 {
407 use_operand_p old_p, new_p;
408 old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
409 new_p = gimple_phi_arg_imm_use_ptr (phi, i);
410 /* Set use on new node, and link into last element's place. */
411 *(new_p->use) = *(old_p->use);
412 relink_imm_use (new_p, old_p);
413 /* Move the location as well. */
414 gimple_phi_arg_set_location (phi, i,
415 gimple_phi_arg_location (phi, num_elem - 1));
416 }
417
418 /* Shrink the vector and return. Note that we do not have to clear
419 PHI_ARG_DEF because the garbage collector will not look at those
420 elements beyond the first PHI_NUM_ARGS elements of the array. */
421 phi->gimple_phi.nargs--;
422 }
423
424
425 /* Remove all PHI arguments associated with edge E. */
426
427 void
428 remove_phi_args (edge e)
429 {
430 gimple_stmt_iterator gsi;
431
432 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
433 remove_phi_arg_num (gsi_stmt (gsi), e->dest_idx);
434 }
435
436
437 /* Remove the PHI node pointed-to by iterator GSI from basic block BB. After
438 removal, iterator GSI is updated to point to the next PHI node in the
439 sequence. If RELEASE_LHS_P is true, the LHS of this PHI node is released
440 into the free pool of SSA names. */
441
442 void
443 remove_phi_node (gimple_stmt_iterator *gsi, bool release_lhs_p)
444 {
445 gimple phi = gsi_stmt (*gsi);
446
447 if (release_lhs_p)
448 insert_debug_temps_for_defs (gsi);
449
450 gsi_remove (gsi, false);
451
452 /* If we are deleting the PHI node, then we should release the
453 SSA_NAME node so that it can be reused. */
454 release_phi_node (phi);
455 if (release_lhs_p)
456 release_ssa_name (gimple_phi_result (phi));
457 }
458
459 /* Remove all the phi nodes from BB. */
460
461 void
462 remove_phi_nodes (basic_block bb)
463 {
464 gimple_stmt_iterator gsi;
465
466 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
467 remove_phi_node (&gsi, true);
468
469 set_phi_nodes (bb, NULL);
470 }
471
472 /* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
473 NULL. */
474
475 tree
476 degenerate_phi_result (gimple phi)
477 {
478 tree lhs = gimple_phi_result (phi);
479 tree val = NULL;
480 size_t i;
481
482 /* Ignoring arguments which are the same as LHS, if all the remaining
483 arguments are the same, then the PHI is a degenerate and has the
484 value of that common argument. */
485 for (i = 0; i < gimple_phi_num_args (phi); i++)
486 {
487 tree arg = gimple_phi_arg_def (phi, i);
488
489 if (arg == lhs)
490 continue;
491 else if (!arg)
492 break;
493 else if (!val)
494 val = arg;
495 else if (arg == val)
496 continue;
497 /* We bring in some of operand_equal_p not only to speed things
498 up, but also to avoid crashing when dereferencing the type of
499 a released SSA name. */
500 else if (TREE_CODE (val) != TREE_CODE (arg)
501 || TREE_CODE (val) == SSA_NAME
502 || !operand_equal_p (arg, val, 0))
503 break;
504 }
505 return (i == gimple_phi_num_args (phi) ? val : NULL);
506 }
507
508 /* Set PHI nodes of a basic block BB to SEQ. */
509
510 void
511 set_phi_nodes (basic_block bb, gimple_seq seq)
512 {
513 gimple_stmt_iterator i;
514
515 gcc_checking_assert (!(bb->flags & BB_RTL));
516 bb->il.gimple.phi_nodes = seq;
517 if (seq)
518 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
519 gimple_set_bb (gsi_stmt (i), bb);
520 }
521
522 #include "gt-tree-phinodes.h"