00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "system.h"
00024 #include "coretypes.h"
00025 #include "tm.h"
00026 #include "tree.h"
00027 #include "flags.h"
00028 #include "basic-block.h"
00029 #include "function.h"
00030 #include "diagnostic.h"
00031 #include "bitmap.h"
00032 #include "tree-flow.h"
00033 #include "tree-gimple.h"
00034 #include "tree-inline.h"
00035 #include "timevar.h"
00036 #include "hashtab.h"
00037 #include "tree-dump.h"
00038 #include "tree-ssa-live.h"
00039 #include "tree-pass.h"
00040 #include "langhooks.h"
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
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 static void
00113 copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
00114 {
00115 int p1, p2, p3;
00116 tree root1, root2;
00117 tree rep1, rep2;
00118 var_ann_t ann1, ann2, ann3;
00119 bool ign1, ign2, abnorm;
00120
00121 gcc_assert (TREE_CODE (var1) == SSA_NAME);
00122 gcc_assert (TREE_CODE (var2) == SSA_NAME);
00123
00124 register_ssa_partition (map, var1, false);
00125 register_ssa_partition (map, var2, true);
00126
00127 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
00128 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
00129
00130 if (debug)
00131 {
00132 fprintf (debug, "Try : ");
00133 print_generic_expr (debug, var1, TDF_SLIM);
00134 fprintf (debug, "(P%d) & ", p1);
00135 print_generic_expr (debug, var2, TDF_SLIM);
00136 fprintf (debug, "(P%d)", p2);
00137 }
00138
00139 gcc_assert (p1 != NO_PARTITION);
00140 gcc_assert (p2 != NO_PARTITION);
00141
00142 rep1 = partition_to_var (map, p1);
00143 rep2 = partition_to_var (map, p2);
00144 root1 = SSA_NAME_VAR (rep1);
00145 root2 = SSA_NAME_VAR (rep2);
00146
00147 ann1 = var_ann (root1);
00148 ann2 = var_ann (root2);
00149
00150 if (p1 == p2)
00151 {
00152 if (debug)
00153 fprintf (debug, " : Already coalesced.\n");
00154 return;
00155 }
00156
00157
00158 abnorm = (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep1)
00159 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep2));
00160 if (abnorm)
00161 {
00162 if (debug)
00163 fprintf (debug, " : Abnormal PHI barrier. No coalesce.\n");
00164 return;
00165 }
00166
00167
00168 if (root1 == root2)
00169 {
00170 p1 = partition_union (map->var_partition, p1, p2);
00171 if (debug)
00172 fprintf (debug, " : Same root, coalesced --> P%d.\n", p1);
00173 return;
00174 }
00175
00176
00177 if (TREE_CODE (root1) == PARM_DECL && TREE_CODE (root2) == PARM_DECL)
00178 {
00179 if (debug)
00180 fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n");
00181 return;
00182 }
00183
00184 if ((TREE_CODE (root1) == RESULT_DECL) != (TREE_CODE (root2) == RESULT_DECL))
00185 {
00186 if (debug)
00187 fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n");
00188 return;
00189 }
00190
00191 ign1 = TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1);
00192 ign2 = TREE_CODE (root2) == VAR_DECL && DECL_IGNORED_P (root2);
00193
00194
00195
00196 if (!ign1 && !ign2)
00197 {
00198 if (DECL_FROM_INLINE (root2))
00199 ign2 = true;
00200 else if (DECL_FROM_INLINE (root1))
00201 ign1 = true;
00202 else
00203 {
00204 if (debug)
00205 fprintf (debug, " : 2 different USER vars. No coalesce.\n");
00206 return;
00207 }
00208 }
00209
00210
00211 if (ann1->type_mem_tag && ann2->type_mem_tag
00212 && ann1->type_mem_tag != ann2->type_mem_tag)
00213 {
00214 if (debug)
00215 fprintf (debug, " : 2 memory tags. No coalesce.\n");
00216 return;
00217 }
00218
00219
00220
00221 if (default_def (root1))
00222 {
00223 if (default_def (root2))
00224 {
00225 if (debug)
00226 fprintf (debug, " : 2 default defs. No coalesce.\n");
00227 return;
00228 }
00229 else
00230 {
00231 ign2 = true;
00232 ign1 = false;
00233 }
00234 }
00235 else if (default_def (root2))
00236 {
00237 ign1 = true;
00238 ign2 = false;
00239 }
00240
00241
00242 if (!lang_hooks.types_compatible_p (TREE_TYPE (root1), TREE_TYPE (root2)))
00243 {
00244 if (debug)
00245 fprintf (debug, " : Incompatible types. No coalesce.\n");
00246 return;
00247 }
00248
00249
00250 if (POINTER_TYPE_P (TREE_TYPE (root1))
00251 && POINTER_TYPE_P (TREE_TYPE (root2))
00252 && get_alias_set (TREE_TYPE (TREE_TYPE (root1)))
00253 != get_alias_set (TREE_TYPE (TREE_TYPE (root2))))
00254 {
00255 if (debug)
00256 fprintf (debug, " : 2 different alasing sets. No coalesce.\n");
00257 return;
00258 }
00259
00260
00261
00262 p3 = partition_union (map->var_partition, p1, p2);
00263
00264
00265
00266 if (!ign2)
00267 replace_ssa_name_symbol (partition_to_var (map, p3), root2);
00268 else if (!ign1)
00269 replace_ssa_name_symbol (partition_to_var (map, p3), root1);
00270
00271
00272 ann3 = var_ann (SSA_NAME_VAR (partition_to_var (map, p3)));
00273 if (ann1->type_mem_tag)
00274 ann3->type_mem_tag = ann1->type_mem_tag;
00275 else
00276 ann3->type_mem_tag = ann2->type_mem_tag;
00277
00278 if (debug)
00279 {
00280 fprintf (debug, " --> P%d ", p3);
00281 print_generic_expr (debug, SSA_NAME_VAR (partition_to_var (map, p3)),
00282 TDF_SLIM);
00283 fprintf (debug, "\n");
00284 }
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 static void
00295 rename_ssa_copies (void)
00296 {
00297 var_map map;
00298 basic_block bb;
00299 block_stmt_iterator bsi;
00300 tree phi, stmt, var, part_var;
00301 unsigned x;
00302 FILE *debug;
00303
00304 if (dump_file && (dump_flags & TDF_DETAILS))
00305 debug = dump_file;
00306 else
00307 debug = NULL;
00308
00309 map = init_var_map (num_ssa_names + 1);
00310
00311 FOR_EACH_BB (bb)
00312 {
00313
00314 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
00315 {
00316 stmt = bsi_stmt (bsi);
00317 if (TREE_CODE (stmt) == MODIFY_EXPR)
00318 {
00319 tree lhs = TREE_OPERAND (stmt, 0);
00320 tree rhs = TREE_OPERAND (stmt, 1);
00321
00322 if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
00323 copy_rename_partition_coalesce (map, lhs, rhs, debug);
00324 }
00325 }
00326 }
00327
00328 FOR_EACH_BB (bb)
00329 {
00330
00331 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
00332 {
00333 int i;
00334 tree res = PHI_RESULT (phi);
00335
00336
00337 if (!is_gimple_reg (SSA_NAME_VAR (res)))
00338 continue;
00339
00340 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
00341 {
00342 tree arg = PHI_ARG_DEF (phi, i);
00343 if (TREE_CODE (arg) == SSA_NAME)
00344 copy_rename_partition_coalesce (map, res, arg, debug);
00345 }
00346 }
00347 }
00348
00349 if (debug)
00350 dump_var_map (debug, map);
00351
00352
00353
00354
00355 for (x = 1; x <= num_ssa_names; x++)
00356 {
00357 part_var = partition_to_var (map, x);
00358 if (!part_var)
00359 continue;
00360 var = map->partition_to_var[x];
00361 if (debug)
00362 {
00363 if (SSA_NAME_VAR (var) != SSA_NAME_VAR (part_var))
00364 {
00365 fprintf (debug, "Coalesced ");
00366 print_generic_expr (debug, var, TDF_SLIM);
00367 fprintf (debug, " to ");
00368 print_generic_expr (debug, part_var, TDF_SLIM);
00369 fprintf (debug, "\n");
00370 }
00371 }
00372 replace_ssa_name_symbol (var, SSA_NAME_VAR (part_var));
00373 }
00374
00375 delete_var_map (map);
00376 }
00377
00378
00379
00380 static bool
00381 gate_copyrename (void)
00382 {
00383 return flag_tree_copyrename != 0;
00384 }
00385
00386 struct tree_opt_pass pass_rename_ssa_copies =
00387 {
00388 "copyrename",
00389 gate_copyrename,
00390 rename_ssa_copies,
00391 NULL,
00392 NULL,
00393 0,
00394 TV_TREE_COPY_RENAME,
00395 PROP_cfg | PROP_ssa | PROP_alias,
00396 0,
00397 0,
00398 0,
00399 TODO_dump_func | TODO_verify_ssa,
00400 0
00401 };
00402