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