/* * Print each class */ static void print_partition(partition_t *p) { int32_t i, j, n; int32_t x, *aux; n = p->nelems; aux = (int32_t *) safe_malloc(n * sizeof(int32_t)); for (i=0; i<n; i++) { aux[i] = partition_find(p, i); } for (i=0; i<n; i++) { x = aux[i]; if (x >= 0) { printf("class[%2"PRId32"]: %2"PRId32, x, i); aux[i] = -1; for (j=i+1; j<n; j++) { if (aux[j] == x) { printf(" %2"PRId32, j); aux[j] = -1; } } printf("\n"); } } safe_free(aux); }
static unsigned int rename_ssa_copies (void) { var_map map; basic_block bb; gimple_stmt_iterator gsi; tree var, part_var; gimple stmt, phi; unsigned x; FILE *debug; bool updated = false; memset (&stats, 0, sizeof (stats)); if (dump_file && (dump_flags & TDF_DETAILS)) debug = dump_file; else debug = NULL; map = init_var_map (num_ssa_names); FOR_EACH_BB (bb) { /* Scan for real copies. */ for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { stmt = gsi_stmt (gsi); if (gimple_assign_ssa_name_copy_p (stmt)) { tree lhs = gimple_assign_lhs (stmt); tree rhs = gimple_assign_rhs1 (stmt); updated |= copy_rename_partition_coalesce (map, lhs, rhs, debug); } } } FOR_EACH_BB (bb) { /* Treat PHI nodes as copies between the result and each argument. */ for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { size_t i; tree res; phi = gsi_stmt (gsi); res = gimple_phi_result (phi); /* Do not process virtual SSA_NAMES. */ if (virtual_operand_p (res)) continue; /* Make sure to only use the same partition for an argument as the result but never the other way around. */ if (SSA_NAME_VAR (res) && !DECL_IGNORED_P (SSA_NAME_VAR (res))) for (i = 0; i < gimple_phi_num_args (phi); i++) { tree arg = PHI_ARG_DEF (phi, i); if (TREE_CODE (arg) == SSA_NAME) updated |= copy_rename_partition_coalesce (map, res, arg, debug); } /* Else if all arguments are in the same partition try to merge it with the result. */ else { int all_p_same = -1; int p = -1; for (i = 0; i < gimple_phi_num_args (phi); i++) { tree arg = PHI_ARG_DEF (phi, i); if (TREE_CODE (arg) != SSA_NAME) { all_p_same = 0; break; } else if (all_p_same == -1) { p = partition_find (map->var_partition, SSA_NAME_VERSION (arg)); all_p_same = 1; } else if (all_p_same == 1 && p != partition_find (map->var_partition, SSA_NAME_VERSION (arg))) { all_p_same = 0; break; } } if (all_p_same == 1) updated |= copy_rename_partition_coalesce (map, res, PHI_ARG_DEF (phi, 0), debug); } } } if (debug) dump_var_map (debug, map); /* Now one more pass to make all elements of a partition share the same root variable. */ for (x = 1; x < num_ssa_names; x++) { part_var = partition_to_var (map, x); if (!part_var) continue; var = ssa_name (x); if (SSA_NAME_VAR (var) == SSA_NAME_VAR (part_var)) continue; if (debug) { fprintf (debug, "Coalesced "); print_generic_expr (debug, var, TDF_SLIM); fprintf (debug, " to "); print_generic_expr (debug, part_var, TDF_SLIM); fprintf (debug, "\n"); } stats.coalesced++; replace_ssa_name_symbol (var, SSA_NAME_VAR (part_var)); } statistics_counter_event (cfun, "copies coalesced", stats.coalesced); delete_var_map (map); return updated ? TODO_remove_unused_locals : 0; }
static bool copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug) { int p1, p2, p3; tree root1, root2; tree rep1, rep2; bool ign1, ign2, abnorm; gcc_assert (TREE_CODE (var1) == SSA_NAME); gcc_assert (TREE_CODE (var2) == SSA_NAME); register_ssa_partition (map, var1); register_ssa_partition (map, var2); p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1)); p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2)); if (debug) { fprintf (debug, "Try : "); print_generic_expr (debug, var1, TDF_SLIM); fprintf (debug, "(P%d) & ", p1); print_generic_expr (debug, var2, TDF_SLIM); fprintf (debug, "(P%d)", p2); } gcc_assert (p1 != NO_PARTITION); gcc_assert (p2 != NO_PARTITION); if (p1 == p2) { if (debug) fprintf (debug, " : Already coalesced.\n"); return false; } rep1 = partition_to_var (map, p1); rep2 = partition_to_var (map, p2); root1 = SSA_NAME_VAR (rep1); root2 = SSA_NAME_VAR (rep2); if (!root1 && !root2) return false; /* Don't coalesce if one of the variables occurs in an abnormal PHI. */ abnorm = (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep1) || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep2)); if (abnorm) { if (debug) fprintf (debug, " : Abnormal PHI barrier. No coalesce.\n"); return false; } /* Partitions already have the same root, simply merge them. */ if (root1 == root2) { p1 = partition_union (map->var_partition, p1, p2); if (debug) fprintf (debug, " : Same root, coalesced --> P%d.\n", p1); return false; } /* Never attempt to coalesce 2 different parameters. */ if ((root1 && TREE_CODE (root1) == PARM_DECL) && (root2 && TREE_CODE (root2) == PARM_DECL)) { if (debug) fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n"); return false; } if ((root1 && TREE_CODE (root1) == RESULT_DECL) != (root2 && TREE_CODE (root2) == RESULT_DECL)) { if (debug) fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n"); return false; } ign1 = !root1 || (TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1)); ign2 = !root2 || (TREE_CODE (root2) == VAR_DECL && DECL_IGNORED_P (root2)); /* Refrain from coalescing user variables, if requested. */ if (!ign1 && !ign2) { if (flag_ssa_coalesce_vars && DECL_FROM_INLINE (root2)) ign2 = true; else if (flag_ssa_coalesce_vars && DECL_FROM_INLINE (root1)) ign1 = true; else if (flag_ssa_coalesce_vars != 2) { if (debug) fprintf (debug, " : 2 different USER vars. No coalesce.\n"); return false; } else ign2 = true; } /* If both values have default defs, we can't coalesce. If only one has a tag, make sure that variable is the new root partition. */ if (root1 && ssa_default_def (cfun, root1)) { if (root2 && ssa_default_def (cfun, root2)) { if (debug) fprintf (debug, " : 2 default defs. No coalesce.\n"); return false; } else { ign2 = true; ign1 = false; } } else if (root2 && ssa_default_def (cfun, root2)) { ign1 = true; ign2 = false; } /* Do not coalesce if we cannot assign a symbol to the partition. */ if (!(!ign2 && root2) && !(!ign1 && root1)) { if (debug) fprintf (debug, " : Choosen variable has no root. No coalesce.\n"); return false; } /* Don't coalesce if the new chosen root variable would be read-only. If both ign1 && ign2, then the root var of the larger partition wins, so reject in that case if any of the root vars is TREE_READONLY. Otherwise reject only if the root var, on which replace_ssa_name_symbol will be called below, is readonly. */ if (((root1 && TREE_READONLY (root1)) && ign2) || ((root2 && TREE_READONLY (root2)) && ign1)) { if (debug) fprintf (debug, " : Readonly variable. No coalesce.\n"); return false; } /* Don't coalesce if the two variables aren't type compatible . */ if (!types_compatible_p (TREE_TYPE (var1), TREE_TYPE (var2)) /* There is a disconnect between the middle-end type-system and VRP, avoid coalescing enum types with different bounds. */ || ((TREE_CODE (TREE_TYPE (var1)) == ENUMERAL_TYPE || TREE_CODE (TREE_TYPE (var2)) == ENUMERAL_TYPE) && TREE_TYPE (var1) != TREE_TYPE (var2))) { if (debug) fprintf (debug, " : Incompatible types. No coalesce.\n"); return false; } /* Merge the two partitions. */ p3 = partition_union (map->var_partition, p1, p2); /* Set the root variable of the partition to the better choice, if there is one. */ if (!ign2 && root2) replace_ssa_name_symbol (partition_to_var (map, p3), root2); else if (!ign1 && root1) replace_ssa_name_symbol (partition_to_var (map, p3), root1); else gcc_unreachable (); if (debug) { fprintf (debug, " --> P%d ", p3); print_generic_expr (debug, SSA_NAME_VAR (partition_to_var (map, p3)), TDF_SLIM); fprintf (debug, "\n"); } return true; }
static bool copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug) { int p1, p2, p3; tree root1, root2; tree rep1, rep2; bool ign1, ign2, abnorm; gcc_assert (TREE_CODE (var1) == SSA_NAME); gcc_assert (TREE_CODE (var2) == SSA_NAME); register_ssa_partition (map, var1); register_ssa_partition (map, var2); p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1)); p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2)); if (debug) { fprintf (debug, "Try : "); print_generic_expr (debug, var1, TDF_SLIM); fprintf (debug, "(P%d) & ", p1); print_generic_expr (debug, var2, TDF_SLIM); fprintf (debug, "(P%d)", p2); } gcc_assert (p1 != NO_PARTITION); gcc_assert (p2 != NO_PARTITION); rep1 = partition_to_var (map, p1); rep2 = partition_to_var (map, p2); root1 = SSA_NAME_VAR (rep1); root2 = SSA_NAME_VAR (rep2); if (p1 == p2) { if (debug) fprintf (debug, " : Already coalesced.\n"); return false; } /* Don't coalesce if one of the variables occurs in an abnormal PHI. */ abnorm = (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep1) || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep2)); if (abnorm) { if (debug) fprintf (debug, " : Abnormal PHI barrier. No coalesce.\n"); return false; } /* Partitions already have the same root, simply merge them. */ if (root1 == root2) { p1 = partition_union (map->var_partition, p1, p2); if (debug) fprintf (debug, " : Same root, coalesced --> P%d.\n", p1); return false; } /* Never attempt to coalesce 2 difference parameters. */ if (TREE_CODE (root1) == PARM_DECL && TREE_CODE (root2) == PARM_DECL) { if (debug) fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n"); return false; } if ((TREE_CODE (root1) == RESULT_DECL) != (TREE_CODE (root2) == RESULT_DECL)) { if (debug) fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n"); return false; } ign1 = TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1); ign2 = TREE_CODE (root2) == VAR_DECL && DECL_IGNORED_P (root2); /* Never attempt to coalesce 2 user variables unless one is an inline variable. */ if (!ign1 && !ign2) { if (DECL_FROM_INLINE (root2)) ign2 = true; else if (DECL_FROM_INLINE (root1)) ign1 = true; else { if (debug) fprintf (debug, " : 2 different USER vars. No coalesce.\n"); return false; } } /* If both values have default defs, we can't coalesce. If only one has a tag, make sure that variable is the new root partition. */ if (gimple_default_def (cfun, root1)) { if (gimple_default_def (cfun, root2)) { if (debug) fprintf (debug, " : 2 default defs. No coalesce.\n"); return false; } else { ign2 = true; ign1 = false; } } else if (gimple_default_def (cfun, root2)) { ign1 = true; ign2 = false; } /* Don't coalesce if the two variables aren't type compatible. */ if (!types_compatible_p (TREE_TYPE (root1), TREE_TYPE (root2))) { if (debug) fprintf (debug, " : Incompatible types. No coalesce.\n"); return false; } /* Merge the two partitions. */ p3 = partition_union (map->var_partition, p1, p2); /* Set the root variable of the partition to the better choice, if there is one. */ if (!ign2) replace_ssa_name_symbol (partition_to_var (map, p3), root2); else if (!ign1) replace_ssa_name_symbol (partition_to_var (map, p3), root1); if (debug) { fprintf (debug, " --> P%d ", p3); print_generic_expr (debug, SSA_NAME_VAR (partition_to_var (map, p3)), TDF_SLIM); fprintf (debug, "\n"); } return true; }
int main() { uint32_t i; int32_t x, y, rx, ry; init_partition(&partition, 0); printf("\n*** Initial partition ***\n"); print_partition_details(&partition); for (i=0; i<40; i++) { x = random() % N; printf("--- testing %"PRId32" ---\n", x); y = partition_find(&partition, x); if (y < 0) { printf("item %"PRId32" not present; adding it\n", x); partition_add(&partition, x); } else { printf("root[%"PRId32"] = %"PRId32"\n", x, y); } } printf("\n*** Partition ***\n"); print_partition_details(&partition); printf("\n"); print_partition(&partition); printf("\n"); for (x=0; x<N; x++) { aux[x] = partition_find(&partition, x); } for (i=1; i<10; i++) { do { x = random() % N; } while (aux[x] < 0); do { y = random() % N; } while (x == y || aux[y] < 0); x = partition_find(&partition, x); y = partition_find(&partition, y); if (x != y) { printf("--- Merging %"PRId32" and %"PRId32" ---\n", x, y); partition_merge(&partition, x, y); } } printf("\n"); print_partition(&partition); reset_partition(&partition); for (i=0; i<100; i++) { x = random() % 300; rx = partition_find(&partition, x); if (rx < 0) { partition_add(&partition, x); rx = x; } y = random() % 300; ry = partition_find(&partition, y); if (ry < 0) { partition_add(&partition, y); ry = y; } if (rx != ry) { partition_merge(&partition, rx, ry); } } printf("\n\n"); print_partition(&partition); delete_partition(&partition); return 0; }