Improved phicoal/phiopt.
[libfirm] / ir / be / bephiopt.c
1 /**
2  * @author Daniel Grund
3  * @date 04.01.2005
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include "pset.h"
10 #include "obst.h"
11 #include "irgraph.h"
12 #include "irnode.h"
13 #include "irgwalk.h"
14 #include "irouts.h"
15 #include "irdom.h"
16
17 #include "bera_t.h"
18 #include "bephiopt.h"
19 #include "phiclass_t.h"
20 #include "bephicoal_t.h"
21 #include "phistat.h"
22
23 #define DEBUG_LVL SET_LEVEL_1
24 #define DO_PHI_STATISTICS
25 #define CHECK_RESULTS
26 #define COUNT_COPY_SAVINGS
27
28 #undef DUMP_IRG_PHI_STAT
29 #define DUMP_DIR_PHI_STAT
30 #define DUMP_ALL_PHI_STAT
31
32 #define PHI_STAT_FILE "all.phistat"
33 #define ENV_PHI_STAT "PHI_STAT"
34
35 static firm_dbg_module_t *dbgphi = NULL;
36
37 static void phi_node_walker(ir_node *node, void *env) {
38         if (is_Phi(node) && mode_is_datab(get_irn_mode(node)))
39                 pset_insert_ptr((pset *)env, node);
40 }
41
42
43 static void node_collector(ir_node *node, void *env) {
44         if (!is_Block(node) && is_allocatable_irn(node))
45                 obstack_ptr_grow(env, node);
46 }
47
48
49 #ifdef CHECK_RESULTS
50 static void check_result(ir_graph *irg) {
51         struct obstack ob;
52         ir_node **nodes, *n1, *n2;
53         int i, o;
54
55         obstack_init(&ob);
56         irg_walk_graph(irg, node_collector, NULL, &ob);
57         nodes = (ir_node **) obstack_finish(&ob);
58         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i])
59                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
60                         if (phi_ops_interfere(n1, n2) && get_irn_color(n1) == get_irn_color(n2)) {
61                                 DBG((dbgphi, 1, "Ouch! %n -- %n\n", n1, n2));
62                                 assert(0 && "Interfering values have the same color!");
63                         }
64
65         obstack_free(&ob, NULL);
66 }
67 #endif
68
69
70 #ifdef COUNT_COPY_SAVINGS
71 /* TODO: how to count copies in case of phi swapping */
72 static int count_copies(pset *all_phi_nodes) {
73         int i, max, phi_color, res = 0;
74         ir_node *phi;
75
76         for (phi = pset_first(all_phi_nodes); phi; phi = pset_next(all_phi_nodes)) {
77                 phi_color = get_irn_color(phi);
78                 for (i = 0, max = get_irn_arity(phi); i < max; ++i)
79                         if (phi_color != get_irn_color(get_irn_n(phi, i)))
80                                 res++;
81         }
82         return res;
83 }
84 #endif
85
86
87 void be_phi_opt(ir_graph* irg) {
88         pset *all_phi_nodes, *all_phi_classes;
89 #ifdef COUNT_COPY_SAVINGS
90         int before, after;
91 #endif
92
93         DBG((dbgphi, 1, "\n\n=======================> IRG: %s\n\n", get_entity_name(get_irg_entity(irg))));
94
95
96         /* get all phi nodes */
97         DBG((dbgphi, 1, "-----------------------> Collecting phi nodes <-----------------------\n"));
98         all_phi_nodes = pset_new_ptr(64);
99         irg_walk_graph(irg, phi_node_walker, NULL, all_phi_nodes);
100
101
102         /* get all phi congruence classes */
103         DBG((dbgphi, 1, "-----------------------> Collecting phi classes <---------------------\n"));
104         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
105
106
107         /* do some statistics */
108 #ifdef DO_PHI_STATISTICS
109         DBG((dbgphi, 1, "-----------------------> Collecting phi stats <-----------------------\n"));
110         phi_stat_reset();
111         phi_stat_collect(irg, all_phi_nodes, all_phi_classes);
112 #  ifdef DUMP_IRG_PHI_STAT
113         {
114                 char buf[1024];
115                 snprintf(buf, sizeof(buf), "%s.phistat", get_entity_name(get_irg_entity(irg)));
116                 /*phi_stat_dump(buf);*/
117                 phi_stat_dump_pretty(buf);
118         }
119 #  endif
120 #  ifdef DUMP_DIR_PHI_STAT
121         phi_stat_update(PHI_STAT_FILE);
122 #  endif
123 #  ifdef DUMP_ALL_PHI_STAT
124         phi_stat_update(getenv(ENV_PHI_STAT));
125 #  endif
126 #endif
127
128
129         /* try to coalesce the colors of each phi class */
130         DBG((dbgphi, 1, "-----------------------> Coalescing <---------------------------------\n"));
131         compute_outs(irg);
132         compute_doms(irg);
133         check_result(irg);
134 #ifdef COUNT_COPY_SAVINGS
135         before = count_copies(all_phi_nodes);
136 #endif
137         be_phi_coalesce(all_phi_classes);
138 #ifdef CHECK_RESULTS
139         check_result(irg);
140 #endif
141 #ifdef COUNT_COPY_SAVINGS
142         after = count_copies(all_phi_nodes);
143         DBG((dbgphi, 1, "Irg: %s. Copies form %d to %d\n", get_entity_name(get_irg_entity(irg)), before, after));
144 #endif
145         free_dom_and_peace(irg);
146 }
147
148
149 void be_phi_opt_init(void) {
150         dbgphi = firm_dbg_register("ir.be.phiopt");
151         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
152
153         phi_class_init();
154         be_phi_coal_init();
155 }