Added beirgmod and benode
[libfirm] / ir / be / becopyopt.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                12.04.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include "irprog.h"
12
13 #include "xmalloc.h"
14 #include "bechordal_t.h"
15 #include "becopyopt.h"
16 #include "becopystat.h"
17
18 #define DEBUG_LVL 0 //SET_LEVEL_1
19 static firm_dbg_module_t *dbg = NULL;
20
21 #define is_curr_reg_class(irn) (arch_get_irn_reg_class(co->env, irn, arch_pos_make_out(0)) == co->cls)
22
23 /**
24  * Builds an optimization unit for a given optimizable irn (root).
25  * This opt-unit is inserted in the main structure co.
26  * If an arg of root itself is optimizable process this arg before with a
27  * recursive call. For handling this situation and loops co->root is used
28  * to remember all roots.
29  */
30 static void co_append_unit(copy_opt_t *co, ir_node *root) {
31         int i, arity;
32         unit_t *unit;
33         DBG((dbg, LEVEL_1, "\t  Root: %n\n", root));
34         /* check if we encountered this root earlier */
35         if (pset_find_ptr(co->roots, root))
36                 return;
37         pset_insert_ptr(co->roots, root);
38
39         assert(is_curr_reg_class(root) && "node is in wrong register class!");
40
41         /* init unit */
42         arity = get_irn_arity(root);
43         unit = xcalloc(1, sizeof(*unit));
44         unit->co = co;
45         unit->interf = 0;
46         unit->node_count = 1;
47         unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
48         unit->nodes[0] = root;
49         INIT_LIST_HEAD(&unit->queue);
50
51         /* check all args */
52         for (i=0; i<arity; ++i) {
53                 ir_node *arg = get_irn_n(root, i);
54                 assert(is_curr_reg_class(arg) && "Argument not in same register class.");
55                 if (arg != root) {
56                         if (!nodes_interfere(co->chordal_env, root, arg)) {
57                                 DBG((dbg, LEVEL_1, "\t  Member: %n\n", arg));
58                                 if (is_optimizable(arg))
59                                         co_append_unit(co, arg);
60                                 unit->nodes[unit->node_count++] = arg;
61                         } else
62                                 unit->interf++;
63                 }
64         }
65         unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
66         list_add_tail(&unit->units, &co->units);
67         /* Init mis_size to node_count. So get_lower_bound returns correct results.
68          * - Now it can be called even before the heuristic has run.
69          * - And it will return correct results for units with nodecount 1 which are
70          * not optimized during the heuristic and have therefor delivered wrong results for get_lower_bound
71          */
72         unit->mis_size = unit->node_count;
73
74 }
75
76 static void co_collect_in_block(ir_node *block, void *env) {
77         copy_opt_t *co = env;
78         struct list_head *head = get_block_border_head(co->chordal_env, block);
79         border_t *curr;
80
81         list_for_each_entry_reverse(border_t, curr, head, list)
82                 if (curr->is_def && curr->is_real && is_optimizable(curr->irn))
83                         co_append_unit(co, curr->irn);
84 }
85
86 static void co_collect_units(copy_opt_t *co) {
87         DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
88         co->roots = pset_new_ptr(64);
89         dom_tree_walk_irg(co->irg, co_collect_in_block, NULL, co);
90         del_pset(co->roots);
91 }
92
93 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env,
94     const arch_env_t *env, const arch_register_class_t *cls) {
95         const char *s1, *s2, *s3;
96         int len;
97         copy_opt_t *co;
98
99         dbg = firm_dbg_register("ir.be.copyopt");
100         firm_dbg_set_mask(dbg, DEBUG_LVL);
101
102         co = xcalloc(1, sizeof(*co));
103   co->chordal_env = chordal_env;
104         co->irg = chordal_env->irg;
105         co->env = env;
106 //      co->isa = env->isa;
107         co->cls = cls;
108
109         s1 = get_irp_prog_name();
110         s2 = get_entity_name(get_irg_entity(co->irg));
111         s3 = cls->name;
112         len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
113         co->name = xmalloc(len);
114         if (!strcmp(co->name, DEBUG_IRG))
115                 firm_dbg_set_mask(dbg, -1);
116         snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
117
118         INIT_LIST_HEAD(&co->units);
119         co_collect_units(co);
120         return co;
121 }
122
123 void free_copy_opt(copy_opt_t *co) {
124         unit_t *curr, *tmp;
125         xfree(co->name);
126         list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
127                 xfree(curr->nodes);
128                 xfree(curr);
129         }
130 }
131
132 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
133         int i, max;
134         for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
135                 ir_node *n = get_irn_out(irn, i);
136                 if (is_optimizable(n) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
137                         return 1;
138         }
139         return 0;
140 }
141
142
143 int co_get_copy_count(const copy_opt_t *co) {
144         int i, res = 0;
145         unit_t *curr;
146         list_for_each_entry(unit_t, curr, &co->units, units) {
147                 int root_col = get_irn_col(co, curr->nodes[0]);
148                 res += curr->interf;
149                 for (i=1; i<curr->node_count; ++i)
150                         if (root_col != get_irn_col(co, curr->nodes[i]))
151                                 res++;
152         }
153         return res;
154 }
155
156 int co_get_lower_bound(const copy_opt_t *co) {
157         int res = 0;
158         unit_t *curr;
159         list_for_each_entry(unit_t, curr, &co->units, units)
160                 res += curr->interf + curr->node_count - curr->mis_size;
161         return res;
162 }
163
164 int co_get_interferer_count(const copy_opt_t *co) {
165         int res = 0;
166         unit_t *curr;
167         list_for_each_entry(unit_t, curr, &co->units, units)
168                 res += curr->interf;
169         return res;
170 }
171
172 /**
173  * Needed for result checking
174  */
175 static void co_collect_for_checker(ir_node *block, void *env) {
176         copy_opt_t *co = env;
177         struct list_head *head = get_block_border_head(co->chordal_env, block);
178         border_t *curr;
179
180         list_for_each_entry_reverse(border_t, curr, head, list)
181                 if (curr->is_def && curr->is_real && is_curr_reg_class(curr->irn))
182                         obstack_ptr_grow(&co->ob, curr->irn);
183 }
184
185 /**
186  * This O(n^2) checker checks, if two ifg-connected nodes have the same color.
187  */
188 void co_check_allocation(copy_opt_t *co) {
189         ir_node **nodes, *n1, *n2;
190         int i, o;
191
192         obstack_init(&co->ob);
193         dom_tree_walk_irg(co->irg, co_collect_for_checker, NULL, co);
194         obstack_ptr_grow(&co->ob, NULL);
195
196         nodes = (ir_node **) obstack_finish(&co->ob);
197         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
198                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
199                         if (nodes_interfere(co->chordal_env, n1, n2)
200           && get_irn_col(co, n1) == get_irn_col(co, n2)) {
201
202                                 DBG((dbg, 0, "Error: %n in %n  and  %n in %n have the same color.\n", n1, get_nodes_block(n1), n2, get_nodes_block(n2)));
203                                 assert(0 && "Interfering values have the same color!");
204                         }
205         }
206         obstack_free(&co->ob, NULL);
207         DBG((dbg, 2, "The checker seems to be happy!\n"));
208 }