]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-phinodes.c
revert: tree.h (phi_arg_d): New field.
[thirdparty/gcc.git] / gcc / tree-phinodes.c
CommitLineData
6de9cd9a 1/* Generic routines for manipulating PHIs
7c475d11
JM
2 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
50d895a7 4
6de9cd9a 5This file is part of GCC.
50d895a7 6
6de9cd9a
DN
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9dcd6f09 9the Free Software Foundation; either version 3, or (at your option)
6de9cd9a 10any later version.
50d895a7 11
6de9cd9a
DN
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.
50d895a7 16
6de9cd9a 17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
50d895a7 20
6de9cd9a
DN
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
6de9cd9a
DN
26#include "ggc.h"
27#include "basic-block.h"
28#include "tree-flow.h"
718f9c0f 29#include "diagnostic-core.h"
726a989a 30#include "gimple.h"
6de9cd9a
DN
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
50d895a7 34 were threaded through PHI nodes.
6de9cd9a
DN
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
6de9cd9a
DN
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.
50d895a7 49
6de9cd9a
DN
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
50d895a7 69 than .1% additional reusable PHI nodes.
6de9cd9a
DN
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
726a989a 75static GTY ((deletable (""))) VEC(gimple,gc) *free_phinodes[NUM_BUCKETS - 2];
6de9cd9a
DN
76static unsigned long free_phinode_count;
77
78static int ideal_phi_node_len (int);
6de9cd9a
DN
79
80#ifdef GATHER_STATISTICS
81unsigned int phi_nodes_reused;
82unsigned int phi_nodes_created;
83#endif
84
6de9cd9a
DN
85/* Dump some simple statistics regarding the re-use of PHI nodes. */
86
87#ifdef GATHER_STATISTICS
88void
89phinodes_print_statistics (void)
90{
91 fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
92 fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
93}
94#endif
95
8c3babed
KH
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. */
99
726a989a
RB
100static inline gimple
101allocate_phi_node (size_t len)
8c3babed 102{
726a989a
RB
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);
8c3babed
KH
107
108 if (free_phinode_count)
109 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
110 if (free_phinodes[bucket])
111 break;
112
113 /* If our free list has an element, then use it. */
114 if (bucket < NUM_BUCKETS - 2
726a989a
RB
115 && gimple_phi_capacity (VEC_index (gimple, free_phinodes[bucket], 0))
116 >= len)
8c3babed
KH
117 {
118 free_phinode_count--;
726a989a
RB
119 phi = VEC_pop (gimple, free_phinodes[bucket]);
120 if (VEC_empty (gimple, free_phinodes[bucket]))
121 VEC_free (gimple, gc, free_phinodes[bucket]);
8c3babed
KH
122#ifdef GATHER_STATISTICS
123 phi_nodes_reused++;
124#endif
125 }
126 else
127 {
a9429e29 128 phi = ggc_alloc_gimple_statement_d (size);
8c3babed
KH
129#ifdef GATHER_STATISTICS
130 phi_nodes_created++;
726a989a
RB
131 {
132 enum gimple_alloc_kind kind = gimple_alloc_kind (GIMPLE_PHI);
133 gimple_alloc_counts[(int) kind]++;
134 gimple_alloc_sizes[(int) kind] += size;
135 }
8c3babed
KH
136#endif
137 }
138
139 return phi;
140}
141
6de9cd9a
DN
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.
145
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. */
151
152static int
153ideal_phi_node_len (int len)
154{
155 size_t size, new_size;
156 int log2, new_len;
157
158 /* We do not support allocations of less than two PHI argument slots. */
159 if (len < 2)
160 len = 2;
161
162 /* Compute the number of bytes of the original request. */
726a989a
RB
163 size = sizeof (struct gimple_statement_phi)
164 + (len - 1) * sizeof (struct phi_arg_d);
6de9cd9a
DN
165
166 /* Round it up to the next power of two. */
167 log2 = ceil_log2 (size);
168 new_size = 1 << log2;
50d895a7
KH
169
170 /* Now compute and return the number of PHI argument slots given an
6de9cd9a
DN
171 ideal size allocation. */
172 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
173 return new_len;
174}
175
0bca51f0 176/* Return a PHI node with LEN argument slots for variable VAR. */
6de9cd9a 177
4021ad55 178static gimple
6de9cd9a
DN
179make_phi_node (tree var, int len)
180{
726a989a 181 gimple phi;
f430bae8 182 int capacity, i;
6de9cd9a 183
405f403a 184 capacity = ideal_phi_node_len (len);
6de9cd9a 185
405f403a 186 phi = allocate_phi_node (capacity);
6de9cd9a 187
6b66c718
KH
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. */
726a989a
RB
191 memset (phi, 0, (sizeof (struct gimple_statement_phi)
192 - sizeof (struct phi_arg_d)
6b66c718 193 + sizeof (struct phi_arg_d) * len));
726a989a 194 phi->gsbase.code = GIMPLE_PHI;
355a7673 195 gimple_init_singleton (phi);
726a989a
RB
196 phi->gimple_phi.nargs = len;
197 phi->gimple_phi.capacity = capacity;
6de9cd9a 198 if (TREE_CODE (var) == SSA_NAME)
726a989a 199 gimple_phi_set_result (phi, var);
6de9cd9a 200 else
726a989a 201 gimple_phi_set_result (phi, make_ssa_name (var, phi));
6de9cd9a 202
f430bae8
AM
203 for (i = 0; i < capacity; i++)
204 {
f47c96aa 205 use_operand_p imm;
f5045c96
AM
206
207 gimple_phi_arg_set_location (phi, i, UNKNOWN_LOCATION);
726a989a
RB
208 imm = gimple_phi_arg_imm_use_ptr (phi, i);
209 imm->use = gimple_phi_arg_def_ptr (phi, i);
f430bae8
AM
210 imm->prev = NULL;
211 imm->next = NULL;
726a989a 212 imm->loc.stmt = phi;
f430bae8 213 }
9b3b55a1 214
6de9cd9a
DN
215 return phi;
216}
217
218/* We no longer need PHI, release it so that it may be reused. */
219
220void
726a989a 221release_phi_node (gimple phi)
6de9cd9a 222{
726a989a
RB
223 size_t bucket;
224 size_t len = gimple_phi_capacity (phi);
225 size_t x;
f430bae8 226
726a989a 227 for (x = 0; x < gimple_phi_num_args (phi); x++)
f430bae8 228 {
f47c96aa 229 use_operand_p imm;
726a989a 230 imm = gimple_phi_arg_imm_use_ptr (phi, x);
f430bae8
AM
231 delink_imm_use (imm);
232 }
6de9cd9a
DN
233
234 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
235 bucket -= 2;
726a989a 236 VEC_safe_push (gimple, gc, free_phinodes[bucket], phi);
6de9cd9a
DN
237 free_phinode_count++;
238}
239
726a989a 240
6de9cd9a
DN
241/* Resize an existing PHI node. The only way is up. Return the
242 possibly relocated phi. */
50d895a7 243
355a7673
MM
244static gimple
245resize_phi_node (gimple phi, size_t len)
6de9cd9a 246{
726a989a
RB
247 size_t old_size, i;
248 gimple new_phi;
1e128c5f 249
355a7673 250 gcc_assert (len > gimple_phi_capacity (phi));
1e128c5f 251
57f60923
KH
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. */
726a989a 255 old_size = sizeof (struct gimple_statement_phi)
355a7673 256 + (gimple_phi_num_args (phi) - 1) * sizeof (struct phi_arg_d);
6de9cd9a 257
8c3babed 258 new_phi = allocate_phi_node (len);
6de9cd9a 259
355a7673 260 memcpy (new_phi, phi, old_size);
6de9cd9a 261
726a989a 262 for (i = 0; i < gimple_phi_num_args (new_phi); i++)
f430bae8 263 {
f47c96aa 264 use_operand_p imm, old_imm;
726a989a 265 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
355a7673 266 old_imm = gimple_phi_arg_imm_use_ptr (phi, i);
726a989a 267 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
f430bae8
AM
268 relink_imm_use_stmt (imm, old_imm, new_phi);
269 }
270
726a989a 271 new_phi->gimple_phi.capacity = len;
50d895a7 272
726a989a 273 for (i = gimple_phi_num_args (new_phi); i < len; i++)
f430bae8 274 {
f47c96aa 275 use_operand_p imm;
f5045c96
AM
276
277 gimple_phi_arg_set_location (new_phi, i, UNKNOWN_LOCATION);
726a989a
RB
278 imm = gimple_phi_arg_imm_use_ptr (new_phi, i);
279 imm->use = gimple_phi_arg_def_ptr (new_phi, i);
f430bae8
AM
280 imm->prev = NULL;
281 imm->next = NULL;
726a989a 282 imm->loc.stmt = new_phi;
f430bae8
AM
283 }
284
355a7673 285 return new_phi;
6de9cd9a
DN
286}
287
a100ac1e
KH
288/* Reserve PHI arguments for a new edge to basic block BB. */
289
290void
291reserve_phi_args_for_new_edge (basic_block bb)
292{
726a989a
RB
293 size_t len = EDGE_COUNT (bb->preds);
294 size_t cap = ideal_phi_node_len (len + 4);
295 gimple_stmt_iterator gsi;
a100ac1e 296
726a989a 297 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
a100ac1e 298 {
355a7673 299 gimple stmt = gsi_stmt (gsi);
726a989a 300
355a7673 301 if (len > gimple_phi_capacity (stmt))
a100ac1e 302 {
355a7673 303 gimple new_phi = resize_phi_node (stmt, cap);
a100ac1e 304
726a989a 305 /* The result of the PHI is defined by this PHI node. */
355a7673
MM
306 SSA_NAME_DEF_STMT (gimple_phi_result (new_phi)) = new_phi;
307 gsi_set_stmt (&gsi, new_phi);
a100ac1e 308
355a7673
MM
309 release_phi_node (stmt);
310 stmt = new_phi;
a100ac1e 311 }
6b66c718
KH
312
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. */
355a7673 320 SET_PHI_ARG_DEF (stmt, len - 1, NULL_TREE);
6b66c718 321
355a7673 322 stmt->gimple_phi.nargs++;
a100ac1e
KH
323 }
324}
325
f8bf9252 326/* Adds PHI to BB. */
9b3b55a1 327
b8698a0f 328void
f8bf9252 329add_phi_node_to_bb (gimple phi, basic_block bb)
6de9cd9a 330{
355a7673 331 gimple_seq seq = phi_nodes (bb);
6de9cd9a 332 /* Add the new PHI node to the list of PHI nodes for block BB. */
355a7673
MM
333 if (seq == NULL)
334 set_phi_nodes (bb, gimple_seq_alloc_with_stmt (phi));
335 else
336 {
337 gimple_seq_add_stmt (&seq, phi);
338 gcc_assert (seq == phi_nodes (bb));
339 }
6de9cd9a
DN
340
341 /* Associate BB to the PHI node. */
726a989a 342 gimple_set_bb (phi, bb);
6de9cd9a 343
f8bf9252
SP
344}
345
346/* Create a new PHI node for variable VAR at basic block BB. */
347
348gimple
349create_phi_node (tree var, basic_block bb)
350{
351 gimple phi = make_phi_node (var, EDGE_COUNT (bb->preds));
352
353 add_phi_node_to_bb (phi, bb);
6de9cd9a
DN
354 return phi;
355}
356
9b3b55a1 357
6de9cd9a
DN
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. */
363
364void
9e227d60 365add_phi_arg (gimple phi, tree def, edge e, source_location locus)
6de9cd9a 366{
bc2dce32 367 basic_block bb = e->dest;
6de9cd9a 368
726a989a 369 gcc_assert (bb == gimple_bb (phi));
bc2dce32 370
a100ac1e
KH
371 /* We resize PHI nodes upon edge creation. We should always have
372 enough room at this point. */
726a989a 373 gcc_assert (gimple_phi_num_args (phi) <= gimple_phi_capacity (phi));
6b66c718
KH
374
375 /* We resize PHI nodes upon edge creation. We should always have
376 enough room at this point. */
726a989a 377 gcc_assert (e->dest_idx < gimple_phi_num_args (phi));
6de9cd9a
DN
378
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)
382 {
383 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
d2e398df 384 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
6de9cd9a
DN
385 }
386
d2e398df 387 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
f5045c96 388 gimple_phi_arg_set_location (phi, e->dest_idx, locus);
6de9cd9a
DN
389}
390
9b3b55a1 391
8930ce20
KH
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. */
6de9cd9a 396
61ea20f5 397static void
726a989a 398remove_phi_arg_num (gimple phi, int i)
6de9cd9a 399{
726a989a 400 int num_elem = gimple_phi_num_args (phi);
6de9cd9a 401
40b554a3
SB
402 gcc_assert (i < num_elem);
403
afd83fe4 404 /* Delink the item which is being removed. */
726a989a 405 delink_imm_use (gimple_phi_arg_imm_use_ptr (phi, i));
afd83fe4
AM
406
407 /* If it is not the last element, move the last element
408 to the element we want to delete, resetting all the links. */
6de9cd9a
DN
409 if (i != num_elem - 1)
410 {
afd83fe4 411 use_operand_p old_p, new_p;
726a989a
RB
412 old_p = gimple_phi_arg_imm_use_ptr (phi, num_elem - 1);
413 new_p = gimple_phi_arg_imm_use_ptr (phi, i);
bca50406 414 /* Set use on new node, and link into last element's place. */
afd83fe4
AM
415 *(new_p->use) = *(old_p->use);
416 relink_imm_use (new_p, old_p);
f5045c96 417 /* Move the location as well. */
b8698a0f 418 gimple_phi_arg_set_location (phi, i,
f5045c96 419 gimple_phi_arg_location (phi, num_elem - 1));
6de9cd9a
DN
420 }
421
54699c02 422 /* Shrink the vector and return. Note that we do not have to clear
357e7a82
JL
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. */
726a989a 425 phi->gimple_phi.nargs--;
6de9cd9a
DN
426}
427
9b3b55a1 428
b31997c0
KH
429/* Remove all PHI arguments associated with edge E. */
430
431void
432remove_phi_args (edge e)
433{
726a989a 434 gimple_stmt_iterator gsi;
b31997c0 435
726a989a
RB
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);
b31997c0
KH
438}
439
9b3b55a1 440
726a989a
RB
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. */
6de9cd9a
DN
445
446void
726a989a 447remove_phi_node (gimple_stmt_iterator *gsi, bool release_lhs_p)
6de9cd9a 448{
726a989a 449 gimple phi = gsi_stmt (*gsi);
cd6549e8
AO
450
451 if (release_lhs_p)
452 insert_debug_temps_for_defs (gsi);
453
726a989a 454 gsi_remove (gsi, false);
4430da7f
KH
455
456 /* If we are deleting the PHI node, then we should release the
457 SSA_NAME node so that it can be reused. */
4430da7f 458 release_phi_node (phi);
9b3b55a1 459 if (release_lhs_p)
726a989a 460 release_ssa_name (gimple_phi_result (phi));
5ae71719 461}
6de9cd9a 462
81b822d5
SP
463/* Remove all the phi nodes from BB. */
464
465void
466remove_phi_nodes (basic_block bb)
467{
468 gimple_stmt_iterator gsi;
469
470 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
471 remove_phi_node (&gsi, true);
472
473 set_phi_nodes (bb, NULL);
474}
475
6de9cd9a 476#include "gt-tree-phinodes.h"