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