00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #include "system.h"
00023 #include "coretypes.h"
00024 #include "tm.h"
00025 #include "tree.h"
00026 #include "rtl.h"
00027 #include "varray.h"
00028 #include "ggc.h"
00029 #include "basic-block.h"
00030 #include "tree-flow.h"
00031 #include "toplev.h"
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 #define NUM_BUCKETS 10
00080 static GTY ((deletable (""))) tree free_phinodes[NUM_BUCKETS - 2];
00081 static unsigned long free_phinode_count;
00082
00083 static int ideal_phi_node_len (int);
00084 static void resize_phi_node (tree *, int);
00085
00086 #ifdef GATHER_STATISTICS
00087 unsigned int phi_nodes_reused;
00088 unsigned int phi_nodes_created;
00089 #endif
00090
00091
00092
00093 void
00094 init_phinodes (void)
00095 {
00096 int i;
00097
00098 for (i = 0; i < NUM_BUCKETS - 2; i++)
00099 free_phinodes[i] = NULL;
00100 free_phinode_count = 0;
00101 }
00102
00103
00104
00105 void
00106 fini_phinodes (void)
00107 {
00108 int i;
00109
00110 for (i = 0; i < NUM_BUCKETS - 2; i++)
00111 free_phinodes[i] = NULL;
00112 free_phinode_count = 0;
00113 }
00114
00115
00116
00117 #ifdef GATHER_STATISTICS
00118 void
00119 phinodes_print_statistics (void)
00120 {
00121 fprintf (stderr, "PHI nodes allocated: %u\n", phi_nodes_created);
00122 fprintf (stderr, "PHI nodes reused: %u\n", phi_nodes_reused);
00123 }
00124 #endif
00125
00126
00127
00128
00129
00130 static inline tree
00131 allocate_phi_node (int len)
00132 {
00133 tree phi;
00134 int bucket = NUM_BUCKETS - 2;
00135 int size = (sizeof (struct tree_phi_node)
00136 + (len - 1) * sizeof (struct phi_arg_d));
00137
00138 if (free_phinode_count)
00139 for (bucket = len - 2; bucket < NUM_BUCKETS - 2; bucket++)
00140 if (free_phinodes[bucket])
00141 break;
00142
00143
00144 if (bucket < NUM_BUCKETS - 2
00145 && PHI_ARG_CAPACITY (free_phinodes[bucket]) >= len)
00146 {
00147 free_phinode_count--;
00148 phi = free_phinodes[bucket];
00149 free_phinodes[bucket] = PHI_CHAIN (free_phinodes[bucket]);
00150 #ifdef GATHER_STATISTICS
00151 phi_nodes_reused++;
00152 #endif
00153 }
00154 else
00155 {
00156 phi = ggc_alloc (size);
00157 #ifdef GATHER_STATISTICS
00158 phi_nodes_created++;
00159 tree_node_counts[(int) phi_kind]++;
00160 tree_node_sizes[(int) phi_kind] += size;
00161 #endif
00162 }
00163
00164 return phi;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 static int
00178 ideal_phi_node_len (int len)
00179 {
00180 size_t size, new_size;
00181 int log2, new_len;
00182
00183
00184 if (len < 2)
00185 len = 2;
00186
00187
00188 size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
00189
00190
00191 log2 = ceil_log2 (size);
00192 new_size = 1 << log2;
00193
00194
00195
00196 new_len = len + (new_size - size) / sizeof (struct phi_arg_d);
00197 return new_len;
00198 }
00199
00200
00201
00202
00203
00204
00205 static tree
00206 make_phi_node (tree var, int len)
00207 {
00208 tree phi;
00209 int capacity;
00210
00211 capacity = ideal_phi_node_len (len);
00212
00213 phi = allocate_phi_node (capacity);
00214
00215
00216
00217
00218 memset (phi, 0, (sizeof (struct tree_phi_node) - sizeof (struct phi_arg_d)
00219 + sizeof (struct phi_arg_d) * len));
00220 TREE_SET_CODE (phi, PHI_NODE);
00221 PHI_NUM_ARGS (phi) = len;
00222 PHI_ARG_CAPACITY (phi) = capacity;
00223 TREE_TYPE (phi) = TREE_TYPE (var);
00224 if (TREE_CODE (var) == SSA_NAME)
00225 SET_PHI_RESULT (phi, var);
00226 else
00227 SET_PHI_RESULT (phi, make_ssa_name (var, phi));
00228
00229 return phi;
00230 }
00231
00232
00233
00234 void
00235 release_phi_node (tree phi)
00236 {
00237 int bucket;
00238 int len = PHI_ARG_CAPACITY (phi);
00239
00240 bucket = len > NUM_BUCKETS - 1 ? NUM_BUCKETS - 1 : len;
00241 bucket -= 2;
00242 PHI_CHAIN (phi) = free_phinodes[bucket];
00243 free_phinodes[bucket] = phi;
00244 free_phinode_count++;
00245 }
00246
00247
00248
00249
00250 static void
00251 resize_phi_node (tree *phi, int len)
00252 {
00253 int old_size;
00254 tree new_phi;
00255
00256 gcc_assert (len > PHI_ARG_CAPACITY (*phi));
00257
00258
00259
00260
00261 old_size = (sizeof (struct tree_phi_node)
00262 + (PHI_NUM_ARGS (*phi) - 1) * sizeof (struct phi_arg_d));
00263
00264 new_phi = allocate_phi_node (len);
00265
00266 memcpy (new_phi, *phi, old_size);
00267
00268 PHI_ARG_CAPACITY (new_phi) = len;
00269
00270 *phi = new_phi;
00271 }
00272
00273
00274
00275 void
00276 reserve_phi_args_for_new_edge (basic_block bb)
00277 {
00278 tree *loc;
00279 int len = EDGE_COUNT (bb->preds);
00280 int cap = ideal_phi_node_len (len + 4);
00281
00282 for (loc = &(bb_ann (bb)->phi_nodes);
00283 *loc;
00284 loc = &PHI_CHAIN (*loc))
00285 {
00286 if (len > PHI_ARG_CAPACITY (*loc))
00287 {
00288 tree old_phi = *loc;
00289
00290 resize_phi_node (loc, cap);
00291
00292
00293 SSA_NAME_DEF_STMT (PHI_RESULT (*loc)) = *loc;
00294
00295 release_phi_node (old_phi);
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305 SET_PHI_ARG_DEF (*loc, len - 1, NULL_TREE);
00306
00307 PHI_NUM_ARGS (*loc)++;
00308 }
00309 }
00310
00311
00312
00313 tree
00314 create_phi_node (tree var, basic_block bb)
00315 {
00316 tree phi;
00317
00318 phi = make_phi_node (var, EDGE_COUNT (bb->preds));
00319
00320
00321 PHI_CHAIN (phi) = phi_nodes (bb);
00322 bb_ann (bb)->phi_nodes = phi;
00323
00324
00325 set_bb_for_stmt (phi, bb);
00326
00327 return phi;
00328 }
00329
00330
00331
00332
00333
00334
00335
00336 void
00337 add_phi_arg (tree phi, tree def, edge e)
00338 {
00339 basic_block bb = e->dest;
00340
00341 gcc_assert (bb == bb_for_stmt (phi));
00342
00343
00344
00345 gcc_assert (PHI_NUM_ARGS (phi) <= PHI_ARG_CAPACITY (phi));
00346
00347
00348
00349 gcc_assert (e->dest_idx < (unsigned int) PHI_NUM_ARGS (phi));
00350
00351
00352
00353 if (e->flags & EDGE_ABNORMAL)
00354 {
00355 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def) = 1;
00356 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
00357 }
00358
00359 SET_PHI_ARG_DEF (phi, e->dest_idx, def);
00360 PHI_ARG_NONZERO (phi, e->dest_idx) = false;
00361 }
00362
00363
00364
00365
00366
00367
00368 static void
00369 remove_phi_arg_num (tree phi, int i)
00370 {
00371 int num_elem = PHI_NUM_ARGS (phi);
00372
00373 gcc_assert (i < num_elem);
00374
00375
00376
00377 if (i != num_elem - 1)
00378 {
00379 SET_PHI_ARG_DEF (phi, i, PHI_ARG_DEF (phi, num_elem - 1));
00380 PHI_ARG_NONZERO (phi, i) = PHI_ARG_NONZERO (phi, num_elem - 1);
00381 }
00382
00383
00384
00385
00386
00387 PHI_NUM_ARGS (phi)--;
00388 }
00389
00390
00391
00392 void
00393 remove_phi_args (edge e)
00394 {
00395 tree phi;
00396
00397 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
00398 remove_phi_arg_num (phi, e->dest_idx);
00399 }
00400
00401
00402
00403
00404 void
00405 remove_phi_node (tree phi, tree prev, basic_block bb)
00406 {
00407 if (prev)
00408 {
00409
00410 PHI_CHAIN (prev) = PHI_CHAIN (phi);
00411
00412
00413
00414 release_ssa_name (PHI_RESULT (phi));
00415 release_phi_node (phi);
00416 }
00417 else if (phi == phi_nodes (bb))
00418 {
00419
00420 bb_ann (bb)->phi_nodes = PHI_CHAIN (phi);
00421
00422
00423
00424 release_ssa_name (PHI_RESULT (phi));
00425 release_phi_node (phi);
00426 }
00427 else
00428 {
00429
00430 tree prev, t;
00431 prev = NULL_TREE;
00432 for (t = phi_nodes (bb); t && t != phi; t = PHI_CHAIN (t))
00433 prev = t;
00434 if (t)
00435 remove_phi_node (t, prev, bb);
00436 }
00437 }
00438
00439
00440
00441
00442 void
00443 remove_all_phi_nodes_for (bitmap vars)
00444 {
00445 basic_block bb;
00446
00447 FOR_EACH_BB (bb)
00448 {
00449
00450 tree phi, new_phi_list, next;
00451 tree *lastp = &new_phi_list;
00452
00453 for (phi = phi_nodes (bb); phi; phi = next)
00454 {
00455 tree var = SSA_NAME_VAR (PHI_RESULT (phi));
00456
00457 next = PHI_CHAIN (phi);
00458
00459 if (!bitmap_bit_p (vars, var_ann (var)->uid))
00460 {
00461
00462
00463
00464 PHI_REWRITTEN (phi) = 1;
00465
00466 *lastp = phi;
00467 lastp = &PHI_CHAIN (phi);
00468 }
00469 else
00470 {
00471
00472
00473 release_ssa_name (PHI_RESULT (phi));
00474 release_phi_node (phi);
00475 }
00476 }
00477
00478
00479 *lastp = NULL;
00480 bb_ann (bb)->phi_nodes = new_phi_list;
00481
00482 #if defined ENABLE_CHECKING
00483 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00484 {
00485 tree var = SSA_NAME_VAR (PHI_RESULT (phi));
00486 gcc_assert (!bitmap_bit_p (vars, var_ann (var)->uid));
00487 }
00488 #endif
00489 }
00490 }
00491
00492
00493
00494
00495 tree
00496 phi_reverse (tree phi)
00497 {
00498 tree prev = NULL_TREE, next;
00499 for (; phi; phi = next)
00500 {
00501 next = PHI_CHAIN (phi);
00502 PHI_CHAIN (phi) = prev;
00503 prev = phi;
00504 }
00505 return prev;
00506 }
00507
00508 #include "gt-tree-phinodes.h"
00509