0ec5e241a60e148d9feafbc2376ee0e13ed237cf
[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         DBG((dbg, 1, "The checker seems to be happy!\n"));
63 }
64
65 void be_copy_opt_init(void) {
66         dbg = firm_dbg_register("ir.be.copyopt");
67         firm_dbg_set_mask(dbg, DEBUG_LVL);
68 }
69
70 void be_copy_opt(ir_graph* irg) {
71         copy_opt_t *co;
72
73         DBG((dbg, LEVEL_1, "\nIRG: %s\n\n", get_entity_name(get_irg_entity(irg))));
74         check_results(irg);
75         co = new_copy_opt(irg);
76
77 #ifdef DO_STAT
78         irg_stat_t *stats = new_irg_stat(co);
79         irg_stat_count(stats, co, 0);
80 #endif
81
82 #ifdef DO_HEUR
83         co_heur_opt(co);
84         check_results(irg);
85 #ifdef DO_STAT
86         irg_stat_count(stats, co, 1);
87 #endif
88 #endif
89
90 #ifdef DO_ILP
91         co_ilp_opt(co);
92         check_results(irg);
93 #ifdef DO_STAT
94         irg_stat_count(stats, co, 2);
95 #endif
96 #endif
97
98 #ifdef DO_STAT
99         irg_stat_print(stats);
100         all_stat_dump();
101 #endif
102
103         free_copy_opt(co);
104 }
105
106 void be_copy_opt_done(ir_graph* irg) {
107 }