Bugfixes
[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  0
25 #define CHECK_RESULTS      1
26 #define COUNT_COPY_SAVINGS 1
27 #define DUMP_OPT_DIFF      1
28
29 #define DUMP_IRG_PHI_STAT  1
30 #define DUMP_DIR_PHI_STAT  1
31 #define DUMP_ALL_PHI_STAT  1
32
33 #define PHI_STAT_FILE "all.phistat"
34 #define ENV_PHI_STAT "PHI_STAT"
35
36 static firm_dbg_module_t *dbgphi = NULL;
37
38 static void phi_node_walker(ir_node *node, void *env) {
39         if (is_Phi(node) && mode_is_datab(get_irn_mode(node)))
40                 pset_insert_ptr((pset *)env, node);
41 }
42
43
44 static void node_collector(ir_node *node, void *env) {
45         if (!is_Block(node) && is_allocatable_irn(node))
46                 obstack_ptr_grow(env, node);
47 }
48
49
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         obstack_ptr_grow(&ob, NULL);
58         nodes = (ir_node **) obstack_finish(&ob);
59         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i])
60                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
61                         if (phi_ops_interfere(n1, n2) && get_irn_color(n1) == get_irn_color(n2)) {
62                                 DBG((dbgphi, 1, "Ouch!\n   %n in %n\n   %n in %n\n", n1, get_nodes_block(n1), n2, get_nodes_block(n2)));
63                                 assert(0 && "Interfering values have the same color!");
64                         }
65
66         obstack_free(&ob, NULL);
67 }
68
69
70 /* TODO: how to count copies in case of phi swapping */
71 static void count_copies(pset *all_phi_nodes, int *copies, int *inevitable) {
72         int i, max, phi_color;
73         ir_node *phi;
74
75         for (phi = pset_first(all_phi_nodes); phi; phi = pset_next(all_phi_nodes)) {
76                 phi_color = get_irn_color(phi);
77                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
78                         ir_node *arg = get_irn_n(phi, i);
79                         if (phi_color != get_irn_color(arg))
80                                 (*copies)++;
81                         if (phi_ops_interfere(phi, arg))
82                                 (*inevitable)++;
83                 }
84         }
85 }
86
87
88 void be_phi_opt(ir_graph* irg) {
89         pset *all_phi_nodes, *all_phi_classes;
90         int before, after, inevitable;
91
92         DBG((dbgphi, 1, "\n\n=======================> IRG: %s\n\n", get_entity_name(get_irg_entity(irg))));
93
94
95         /* get all phi nodes */
96         DBG((dbgphi, 1, "-----------------------> Collecting phi nodes <-----------------------\n"));
97         all_phi_nodes = pset_new_ptr(64);
98         irg_walk_graph(irg, phi_node_walker, NULL, all_phi_nodes);
99
100
101         /* get all phi congruence classes */
102         DBG((dbgphi, 1, "-----------------------> Collecting phi classes <---------------------\n"));
103         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
104
105
106         /* do some statistics */
107         if (DO_PHI_STATISTICS) {
108                 DBG((dbgphi, 1, "-----------------------> Collecting phi stats <-----------------------\n"));
109                 phi_stat_reset();
110                 phi_stat_collect(irg, all_phi_nodes, all_phi_classes);
111                 if (DUMP_IRG_PHI_STAT) {
112                         char buf[1024];
113                         snprintf(buf, sizeof(buf), "%s.phistat", get_entity_name(get_irg_entity(irg)));
114                         /*phi_stat_dump(buf);*/
115                         phi_stat_dump_pretty(buf);
116                 }
117                 if (DUMP_DIR_PHI_STAT)
118                         phi_stat_update(PHI_STAT_FILE);
119                 if (DUMP_ALL_PHI_STAT)
120                         phi_stat_update(getenv(ENV_PHI_STAT));
121         }
122
123
124         /* try to coalesce the colors of each phi class */
125         DBG((dbgphi, 1, "-----------------------> Coalescing <---------------------------------\n"));
126         compute_outs(irg);
127         compute_doms(irg);
128
129         if (COUNT_COPY_SAVINGS) {
130                 before = 0;
131                 count_copies(all_phi_nodes, &before, &inevitable);
132         }
133
134         if (CHECK_RESULTS)
135                 check_result(irg);
136
137         if (DUMP_OPT_DIFF)
138                 dump_allocated_irg(irg, "-before");
139
140         be_phi_coalesce(all_phi_classes);
141
142         if (DUMP_OPT_DIFF)
143                 dump_allocated_irg(irg, "-opt");
144
145         if (CHECK_RESULTS)
146                 check_result(irg);
147
148         if (COUNT_COPY_SAVINGS) {
149                 after = 0;
150                 inevitable = 0;
151                 count_copies(all_phi_nodes, &after, &inevitable);
152                 DBG((dbgphi, 1, "Irg: %s. Copies form %d to %d. %d phi-interfers\n", get_entity_name(get_irg_entity(irg)), before, after, inevitable));
153         }
154         free_dom_and_peace(irg);
155 }
156
157
158 void be_phi_opt_init(void) {
159         dbgphi = firm_dbg_register("ir.be.phiopt");
160         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
161
162         phi_class_init();
163         be_phi_coal_init();
164 }