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