23 #include "phiclass_t.h"
24 #include "bephicoal_t.h"
27 #define DEBUG_LVL SET_LEVEL_1
28 #define DO_PHI_STATISTICS 0
29 #define CHECK_RESULTS 1
30 #define COUNT_COPY_SAVINGS 1
31 #define DUMP_OPT_DIFF 1
33 #define DUMP_IRG_PHI_STAT 1
34 #define DUMP_DIR_PHI_STAT 1
35 #define DUMP_ALL_PHI_STAT 1
37 #define PHI_STAT_FILE "dir.phistat"
38 #define ENV_PHI_STAT "PHI_STAT"
40 static firm_dbg_module_t *dbgphi = NULL;
42 static void phi_node_walker(ir_node *node, void *env) {
43 if (is_Phi(node) && mode_is_datab(get_irn_mode(node)))
44 pset_insert_ptr((pset *)env, node);
48 static void node_collector(ir_node *node, void *env) {
49 struct obstack *obst = env;
50 if (!is_Block(node) && is_allocatable_irn(node))
51 obstack_ptr_grow(obst, node);
55 static void check_result(ir_graph *irg) {
57 ir_node **nodes, *n1, *n2;
61 irg_walk_graph(irg, node_collector, NULL, &ob);
62 obstack_ptr_grow(&ob, NULL);
63 nodes = (ir_node **) obstack_finish(&ob);
64 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i])
65 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
66 if (phi_ops_interfere(n1, n2) && get_irn_color(n1) == get_irn_color(n2)) {
67 DBG((dbgphi, 1, "Ouch!\n %n in %n\n %n in %n\n", n1, get_nodes_block(n1), n2, get_nodes_block(n2)));
68 assert(0 && "Interfering values have the same color!");
71 obstack_free(&ob, NULL);
75 /* TODO: how to count copies in case of phi swapping */
76 static void count_copies(pset *all_phi_nodes, int *copies, int *inevitable) {
77 int i, max, phi_color;
80 for (phi = pset_first(all_phi_nodes); phi; phi = pset_next(all_phi_nodes)) {
81 phi_color = get_irn_color(phi);
82 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
83 ir_node *arg = get_irn_n(phi, i);
84 if (phi_color != get_irn_color(arg))
86 if (phi_ops_interfere(phi, arg))
93 void be_phi_opt(ir_graph* irg) {
94 pset *all_phi_nodes, *all_phi_classes;
95 int before, after, inevitable;
97 DBG((dbgphi, 1, "\n\n=======================> IRG: %s\n\n", get_entity_name(get_irg_entity(irg))));
100 /* get all phi nodes */
101 DBG((dbgphi, 1, "-----------------------> Collecting phi nodes <-----------------------\n"));
102 all_phi_nodes = pset_new_ptr(64);
103 irg_walk_graph(irg, phi_node_walker, NULL, all_phi_nodes);
106 /* get all phi congruence classes */
107 DBG((dbgphi, 1, "-----------------------> Collecting phi classes <---------------------\n"));
108 all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
111 /* do some statistics */
112 if (DO_PHI_STATISTICS) {
113 DBG((dbgphi, 1, "-----------------------> Collecting phi stats <-----------------------\n"));
115 phi_stat_collect(irg, all_phi_nodes, all_phi_classes);
116 if (DUMP_IRG_PHI_STAT) {
118 snprintf(buf, sizeof(buf), "%s.phistat", get_entity_name(get_irg_entity(irg)));
119 /*phi_stat_dump(buf);*/
120 phi_stat_dump_pretty(buf);
122 if (DUMP_DIR_PHI_STAT)
123 phi_stat_update(PHI_STAT_FILE);
124 if (DUMP_ALL_PHI_STAT)
125 phi_stat_update(getenv(ENV_PHI_STAT));
129 /* try to coalesce the colors of each phi class */
130 DBG((dbgphi, 1, "-----------------------> Coalescing <---------------------------------\n"));
134 if (COUNT_COPY_SAVINGS) {
136 count_copies(all_phi_nodes, &before, &inevitable);
143 dump_allocated_irg(irg, "-before");
145 be_phi_coalesce(all_phi_classes);
148 dump_allocated_irg(irg, "-opt");
153 if (COUNT_COPY_SAVINGS) {
156 count_copies(all_phi_nodes, &after, &inevitable);
157 DBG((dbgphi, 1, "Irg: %s. Copies form %d to %d. %d phi-interfers\n", get_entity_name(get_irg_entity(irg)), before, after, inevitable));
159 free_dom_and_peace(irg);
163 void be_phi_opt_init(void) {
164 dbgphi = firm_dbg_register("ir.be.phiopt");
165 firm_dbg_set_mask(dbgphi, DEBUG_LVL);