Lots of changes: Removed all file for phi-opt. Generic approach is now
[libfirm] / ir / be / becopyoptmain.c
1 /**
2  * Main file for the optimization reducing the copies needed for:
3  * - phi coalescing
4  * - register-constrained nodes
5  *
6  * Contains a simple checker for this optimization.
7  * By request some statistics are collected too.
8  *
9  * @author Daniel Grund
10  * @date 11.04.2005
11  */
12
13 #include "debug.h"
14 #include "list.h"
15 #include "obst.h"
16 #include "irgraph.h"
17 #include "irnode.h"
18 #include "irgwalk.h"
19
20 #include "bera_t.h"
21 #include "becopyoptmain.h"
22 #include "becopyopt.h"
23 #include "becopystat.h"
24
25 #define DO_HEUR
26 #define DO_ILP
27 #undef DO_STAT
28
29 #define DEBUG_LVL SET_LEVEL_1
30 static firm_dbg_module_t *dbg = NULL;
31
32 /**
33  * Needed for result checking
34  */
35 static void allocatable_collector(ir_node *node, void *env) {
36         struct obstack *obst = env;
37         if (!is_Block(node) && is_allocatable_irn(node))
38                 obstack_ptr_grow(obst, node);
39 }
40
41 /**
42  * This O(n^2) checker checks, if two ifg-connected nodes have the same color.
43  */
44 static void check_results(ir_graph *irg) {
45         struct obstack ob;
46         ir_node **nodes, *n1, *n2;
47         int i, o;
48
49         obstack_init(&ob);
50         irg_walk_graph(irg, allocatable_collector, NULL, &ob);
51         obstack_ptr_grow(&ob, NULL);
52         nodes = (ir_node **) obstack_finish(&ob);
53         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
54                 assert(! (is_allocatable_irn(n1) && get_irn_color(n1) == NO_COLOR));
55                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
56                         if (phi_ops_interfere(n1, n2) && get_irn_color(n1) == get_irn_color(n2)) {
57                                 DBG((dbg, 0, "Error: %n in %n  and  %n in %n\n", n1, get_nodes_block(n1), n2, get_nodes_block(n2)));
58                                 assert(0 && "Interfering values have the same color!");
59                         }
60         }
61         obstack_free(&ob, NULL);
62 }
63
64 void be_copy_opt_init(void) {
65         dbg = firm_dbg_register("ir.be.copyopt");
66         firm_dbg_set_mask(dbg, DEBUG_LVL);
67 }
68
69 void be_copy_opt(ir_graph* irg) {
70         copy_opt_t *co;
71
72         check_results(irg);
73         co = new_copy_opt(irg);
74
75 #ifdef DO_STAT
76         irg_stat_t *stats = new_irg_stat(co);
77         irg_stat_count(stats, co, 0);
78 #endif
79
80 #ifdef DO_HEUR
81         co_heur_opt(co);
82         check_results(irg);
83 #ifdef DO_STAT
84         irg_stat_count(stats, co, 1);
85 #endif
86 #endif
87
88 #ifdef DO_ILP
89         co_ilp_opt(co);
90         check_results(irg);
91 #ifdef DO_STAT
92         irg_stat_count(stats, co, 2);
93 #endif
94 #endif
95
96 #ifdef DO_STAT
97         irg_stat_print(stats);
98         all_stat_dump();
99 #endif
100
101         free_copy_opt(co);
102 }
103
104 void be_copy_opt_done(ir_graph* irg) {
105 }