]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-phinodes.c
2012-08-07 Richard Guenther <rguenther@suse.de>
[thirdparty/gcc.git] / gcc / tree-phinodes.c
CommitLineData
4ee9c684 1/* Generic routines for manipulating PHIs
92468061 2 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
de5f9bd2 4
4ee9c684 5This file is part of GCC.
de5f9bd2 6
4ee9c684 7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
8c4c00c1 9the Free Software Foundation; either version 3, or (at your option)
4ee9c684 10any later version.
de5f9bd2 11
4ee9c684 12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
de5f9bd2 16
4ee9c684 17You should have received a copy of the GNU General Public License
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
de5f9bd2 20
4ee9c684 21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
4ee9c684 26#include "ggc.h"
27#include "basic-block.h"
28#include "tree-flow.h"
0b205f4c 29#include "diagnostic-core.h"
75a70cf9 30#include "gimple.h"
4ee9c684 31
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
de5f9bd2 34 were threaded through PHI nodes.
4ee9c684 35
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.
39
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).
45
4ee9c684 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.
de5f9bd2 49
4ee9c684 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
de5f9bd2 69 than .1% additional reusable PHI nodes.
4ee9c684 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
75a70cf9 75static GTY ((deletable (""))) VEC(gimple,gc) *free_phinodes[NUM_BUCKETS - 2];
4ee9c684 76static unsigned long free_phinode_count;
77
78static int ideal_phi_node_len (int);
4ee9c684 79
4ee9c684 80unsigned int phi_nodes_reused;
81unsigned int phi_nodes_created;
4ee9c684 82
4ee9c684 83/* Dump some simple statistics regarding the re-use of PHI nodes. */
84
4ee9c684 85void
86phinodes_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}
4ee9c684 91
c1ba1d09 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
75a70cf9 96static inline gimple
97allocate_phi_node (size_t len)
c1ba1d09 98{
75a70cf9 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);
c1ba1d09 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
75a70cf9 111 && gimple_phi_capacity (VEC_index (gimple, free_phinodes[bucket], 0))
112 >= len)
c1ba1d09 113 {
114 free_phinode_count--;
75a70cf9 115 phi = VEC_pop (gimple, free_phinodes[bucket]);
116 if (VEC_empty (gimple, free_phinodes[bucket]))
117 VEC_free (gimple, gc, free_phinodes[bucket]);
ecd52ea9 118 if (GATHER_STATISTICS)
119 phi_nodes_reused++;
c1ba1d09 120 }
121 else
122 {
ba72912a 123 phi = ggc_alloc_gimple_statement_d (size);
ecd52ea9 124 if (GATHER_STATISTICS)
75a70cf9 125 {
126 enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
ecd52ea9 127 phi_nodes_created++;
128 gimple_alloc_counts[(int) kind]++;
129 gimple_alloc_sizes[(int) kind] += size;
75a70cf9 130 }
c1ba1d09 131 }
132
133 return phi;
134}
135
4ee9c684 136/* Given LEN, the original number of requested PHI arguments, return
137 a new, "ideal" length for the PHI node. The "ideal" length rounds
138 the total size of the PHI node up to the next power of two bytes.
139
140 Rounding up will not result in wasting any memory since the size request
141 will be rounded up by the GC system anyway. [ Note this is not entirely
142 true since the original length might have fit on one of the special
143 GC pages. ] By rounding up, we may avoid the need to reallocate the
144 PHI node later if we increase the number of arguments for the PHI. */
145
146static int
147ideal_phi_node_len (int len)
148{
149 size_t size, new_size;
150 int log2, new_len;
151
152 /* We do not support allocations of less than two PHI argument slots. */
153 if (len < 2)
154 len = 2;
155
156 /* Compute the number of bytes of the original request. */
75a70cf9 157 size = sizeof (struct gimple_statement_phi)
158 + (len - 1) * sizeof (struct phi_arg_d);
4ee9c684 159
160 /* Round it up to the next power of two. */
161 log2 = ceil_log2 (size);
162 new_size = 1 << log2;
de5f9bd2 163
164 /* Now compute and return the number of PHI argument slots given an
4ee9c684 165 ideal size allocation. */
166 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
167 return new_len;
168}
169
88dbf20f 170/* Return a PHI node with LEN argument slots for variable VAR. */
4ee9c684 171
8abed11e 172static gimple
4ee9c684 173make_phi_node (tree var, int len)
174{
75a70cf9 175 gimple phi;
22aa74c4 176 int capacity, i;
4ee9c684 177
08d1df96 178 capacity = ideal_phi_node_len (len);
4ee9c684 179
08d1df96 180 phi = allocate_phi_node (capacity);
4ee9c684 181
cc890649 182 /* We need to clear the entire PHI node, including the argument
183 portion, because we represent a "missing PHI argument" by placing
184 NULL_TREE in PHI_ARG_DEF. */
75a70cf9 185 memset (phi, 0, (sizeof (struct gimple_statement_phi)
186 - sizeof (struct phi_arg_d)
cc890649 187 + sizeof (struct phi_arg_d) * len));
75a70cf9 188 phi->gsbase.code = GIMPLE_PHI;
e3a19533 189 gimple_init_singleton (phi);
75a70cf9 190 phi->gimple_phi.nargs = len;
191 phi->gimple_phi.capacity = capacity;
9c06f260 192 if (!var)
193 ;
194 else if (TREE_CODE (var) == SSA_NAME)
75a70cf9 195 gimple_phi_set_result (phi, var);
4ee9c684 196 else
75a70cf9 197 gimple_phi_set_result (phi, make_ssa_name (var, phi));
4ee9c684 198
22aa74c4 199 for (i = 0; i < capacity; i++)
200 {
b66731e8 201 use_operand_p imm;
efbcb6de 202
203 gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
75a70cf9 204 imm = gimple_phi_arg_imm_use_ptr (phi, i);
205 imm->use = gimple_phi_arg_def_ptr (phi, i);
22aa74c4 206 imm->prev = NULL;
207 imm->next = NULL;
75a70cf9 208 imm->loc.stmt = phi;
22aa74c4 209 }
17889f9d 210
4ee9c684 211 return phi;
212}
213
214/* We no longer need PHI, release it so that it may be reused. */
215
216void
75a70cf9 217release_phi_node (gimple phi)
4ee9c684 218{
75a70cf9 219 size_t bucket;
220 size_t len = gimple_phi_capacity (phi);
221 size_t x;
22aa74c4 222
75a70cf9 223 for (x = 0; x < gimple_phi_num_args (phi); x++)
22aa74c4 224 {
b66731e8 225 use_operand_p imm;
75a70cf9 226 imm = gimple_phi_arg_imm_use_ptr (phi, x);
22aa74c4 227 delink_imm_use (imm);
228 }
4ee9c684 229
230 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
231 bucket -= 2;
75a70cf9 232 VEC_safe_push (gimple, gc, free_phinodes[bucket], phi);
4ee9c684 233 free_phinode_count++;
234}
235
75a70cf9 236
4ee9c684 237/* Resize an existing PHI node. The only way is up. Return the
238 possibly relocated phi. */
de5f9bd2 239
e3a19533 240static gimple
241resize_phi_node (gimple phi, size_t len)
4ee9c684 242{
75a70cf9 243 size_t old_size, i;
244 gimple new_phi;
8c0963c4 245
e3a19533 246 gcc_assert (len > gimple_phi_capacity (phi));
8c0963c4 247
ee98f167 248 /* The garbage collector will not look at the PHI node beyond the
249 first PHI_NUM_ARGS elements. Therefore, all we have to copy is a
250 portion of the PHI node currently in use. */
75a70cf9 251 old_size = sizeof (struct gimple_statement_phi)
e3a19533 252 + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
4ee9c684 253
c1ba1d09 254 new_phi = allocate_phi_node (len);
4ee9c684 255
e3a19533 256 memcpy (new_phi, phi, old_size);
4ee9c684 257
75a70cf9 258 for (i = 0; i < gimple_phi_num_args (new_phi); i++)
22aa74c4 259 {
b66731e8 260 use_operand_p imm, old_imm;
75a70cf9 261 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
e3a19533 262 old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
75a70cf9 263 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
22aa74c4 264 relink_imm_use_stmt (imm, old_imm, new_phi);
265 }
266
75a70cf9 267 new_phi->gimple_phi.capacity = len;
de5f9bd2 268
75a70cf9 269 for (i = gimple_phi_num_args (new_phi); i < len; i++)
22aa74c4 270 {
b66731e8 271 use_operand_p imm;
efbcb6de 272
273 gimple_phi_arg_set_location (new_phi, i, UNKNOWN_LOCATION);
75a70cf9 274 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
275 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
22aa74c4 276 imm->prev = NULL;
277 imm->next = NULL;
75a70cf9 278 imm->loc.stmt = new_phi;
22aa74c4 279 }
280
e3a19533 281 return new_phi;
4ee9c684 282}
283
a77b4cde 284/* Reserve PHI arguments for a new edge to basic block BB. */
285
286void
287reserve_phi_args_for_new_edge (basic_block bb)
288{
75a70cf9 289 size_t len = EDGE_COUNT (bb->preds);
290 size_t cap = ideal_phi_node_len (len + 4);
291 gimple_stmt_iterator gsi;
a77b4cde 292
75a70cf9 293 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
a77b4cde 294 {
e3a19533 295 gimple stmt = gsi_stmt (gsi);
75a70cf9 296
e3a19533 297 if (len > gimple_phi_capacity (stmt))
a77b4cde 298 {
e3a19533 299 gimple new_phi = resize_phi_node (stmt, cap);
a77b4cde 300
75a70cf9 301 /* The result of the PHI is defined by this PHI node. */
e3a19533 302 SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
303 gsi_set_stmt (&gsi, new_phi);
a77b4cde 304
e3a19533 305 release_phi_node (stmt);
306 stmt = new_phi;
a77b4cde 307 }
cc890649 308
309 /* We represent a "missing PHI argument" by placing NULL_TREE in
310 the corresponding slot. If PHI arguments were added
311 immediately after an edge is created, this zeroing would not
312 be necessary, but unfortunately this is not the case. For
313 example, the loop optimizer duplicates several basic blocks,
314 redirects edges, and then fixes up PHI arguments later in
315 batch. */
e3a19533 316 SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
cc890649 317
e3a19533 318 stmt->gimple_phi.nargs++;
a77b4cde 319 }
320}
321
255b6be7 322/* Adds PHI to BB. */
17889f9d 323
48e1416a 324void
255b6be7 325add_phi_node_to_bb (gimple phi, basic_block bb)
4ee9c684 326{
e3a19533 327 gimple_seq seq = phi_nodes (bb);
4ee9c684 328 /* Add the new PHI node to the list of PHI nodes for block BB. */
e3a19533 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 }
4ee9c684 336
337 /* Associate BB to the PHI node. */
75a70cf9 338 gimple_set_bb (phi, bb);
4ee9c684 339
255b6be7 340}
341
342/* Create a new PHI node for variable VAR at basic block BB. */
343
344gimple
345create_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);
4ee9c684 350 return phi;
351}
352
17889f9d 353
4ee9c684 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
360void
60d535d2 361add_phi_arg (gimple phi, tree def, edge e, source_location locus)
4ee9c684 362{
2a6e956a 363 basic_block bb = e->dest;
4ee9c684 364
75a70cf9 365 gcc_assert (bb == gimple_bb (phi));
2a6e956a 366
a77b4cde 367 /* We resize PHI nodes upon edge creation. We should always have
368 enough room at this point. */
75a70cf9 369 gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
cc890649 370
371 /* We resize PHI nodes upon edge creation. We should always have
372 enough room at this point. */
75a70cf9 373 gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
4ee9c684 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;
04cd6268 380 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
4ee9c684 381 }
382
04cd6268 383 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
efbcb6de 384 gimple_phi_arg_set_location (phi, e->dest_idx, locus);
4ee9c684 385}
386
17889f9d 387
519d40d1 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. */
4ee9c684 392
a632e132 393static void
75a70cf9 394remove_phi_arg_num (gimple phi, int i)
4ee9c684 395{
75a70cf9 396 int num_elem = gimple_phi_num_args (phi);
4ee9c684 397
13e51728 398 gcc_assert (i < num_elem);
399
66c8f3a9 400 /* Delink the item which is being removed. */
75a70cf9 401 delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
66c8f3a9 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. */
4ee9c684 405 if (i != num_elem - 1)
406 {
66c8f3a9 407 use_operand_p old_p, new_p;
75a70cf9 408 old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
409 new_p = gimple_phi_arg_imm_use_ptr (phi, i);
9fdf9cf6 410 /* Set use on new node, and link into last element's place. */
66c8f3a9 411 *(new_p->use) = *(old_p->use);
412 relink_imm_use (new_p, old_p);
efbcb6de 413 /* Move the location as well. */
48e1416a 414 gimple_phi_arg_set_location (phi, i,
efbcb6de 415 gimple_phi_arg_location (phi, num_elem - 1));
4ee9c684 416 }
417
36fddb4b 418 /* Shrink the vector and return. Note that we do not have to clear
6b34676b 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. */
75a70cf9 421 phi->gimple_phi.nargs--;
4ee9c684 422}
423
17889f9d 424
1af6dc83 425/* Remove all PHI arguments associated with edge E. */
426
427void
428remove_phi_args (edge e)
429{
75a70cf9 430 gimple_stmt_iterator gsi;
1af6dc83 431
75a70cf9 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);
1af6dc83 434}
435
17889f9d 436
75a70cf9 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. */
4ee9c684 441
442void
75a70cf9 443remove_phi_node (gimple_stmt_iterator *gsi, bool release_lhs_p)
4ee9c684 444{
75a70cf9 445 gimple phi = gsi_stmt (*gsi);
41ad616d 446
447 if (release_lhs_p)
448 insert_debug_temps_for_defs (gsi);
449
75a70cf9 450 gsi_remove (gsi, false);
1e49fef2 451
452 /* If we are deleting the PHI node, then we should release the
453 SSA_NAME node so that it can be reused. */
1e49fef2 454 release_phi_node (phi);
17889f9d 455 if (release_lhs_p)
75a70cf9 456 release_ssa_name (gimple_phi_result (phi));
214ed29c 457}
4ee9c684 458
899e6126 459/* Remove all the phi nodes from BB. */
460
461void
462remove_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
4ee9c684 472#include "gt-tree-phinodes.h"