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