4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 #include "bechordal_t.h"
18 #include "becopyopt.h"
19 #include "becopystat.h"
21 #define DEBUG_LVL 0 //SET_LEVEL_1
22 static firm_dbg_module_t *dbg = NULL;
24 #define is_curr_reg_class(irn) (arch_get_irn_reg_class(co->chordal_env->arch_env, irn, arch_pos_make_out(0)) == co->chordal_env->cls)
26 #define MIN(a,b) ((a<b)?(a):(b))
29 * Computes a 'max independent set' wrt. ifg-edges only (no coloring conflicts, no register constraints)
30 * @return The size of such a mis
31 * NOTE: Code adapted from becopyheur
32 * BETTER: Here we can be sure having a chordal graph to work on, so for 'larger' opt-units we
33 * could use a special algorithm.
35 static int get_ifg_mis_size(unit_t *ou) {
36 int all_size, curr_size, i, o;
38 ir_node **curr, **all = alloca(ou->node_count * sizeof(*all));
40 /* all contains all nodes */
42 for (i=0; i<ou->node_count; ++i)
43 all[all_size++] = ou->nodes[i];
45 /* which[i] says which element to take out of all[] and put into curr[i] */
46 which = alloca(all_size*sizeof(*which));
47 for (curr_size=0; curr_size<all_size; ++curr_size)
48 which[curr_size] = curr_size;
50 /* stores the currently examined set */
51 curr = alloca(all_size*sizeof(*curr));
53 while (1) { /* this loop will terminate because at least a single node will be a max indep. set */
54 /* build current set */
55 for (i=0; i<curr_size; ++i)
56 curr[i] = all[which[all_size-curr_size+i]];
58 /* check current set */
59 for (i=0; i<curr_size; ++i)
60 for (o=i+1; o<curr_size; ++o)
61 if (nodes_interfere(ou->co->chordal_env, curr[i], curr[o]))
64 /* We had no conflict. This is the (one) max indep. set */
68 /* We had a conflict. Generate next set */
69 if (which[all_size-curr_size+1] == all_size-curr_size+1) {
71 for (i=0; i<curr_size; ++i)
72 which[all_size-curr_size+i] = i;
79 } while (!(which[pos] = (which[pos]+1) % all_size));
81 for (i=pos+1; i<all_size; ++i)
82 which[i] = MIN(which[i-1]+1, all_size-1);
85 for (i=all_size-curr_size; i<all_size-1; ++i)
86 if (which[i]>=which[i+1]) {
93 assert(0 && "How did you get here?");
97 * Builds an optimization unit for a given optimizable irn (root).
98 * This opt-unit is inserted in the main structure co.
99 * If an arg of root itself is optimizable process this arg before with a
100 * recursive call. For handling this situation and loops co->root is used
101 * to remember all roots.
103 static void co_append_unit(copy_opt_t *co, ir_node *root) {
106 DBG((dbg, LEVEL_1, "\t Root: %n %N\n", root, root));
107 /* check if we encountered this root earlier */
108 if (pset_find_ptr(co->roots, root))
110 pset_insert_ptr(co->roots, root);
112 assert(is_curr_reg_class(root) && "node is in wrong register class!");
115 arity = get_irn_arity(root);
116 unit = xcalloc(1, sizeof(*unit));
119 unit->node_count = 1;
120 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
121 unit->nodes[0] = root;
122 INIT_LIST_HEAD(&unit->queue);
126 for (i=0; i<arity; ++i) {
127 ir_node *arg = get_irn_n(root, i);
128 assert(is_curr_reg_class(arg) && "Argument not in same register class.");
130 if (!nodes_interfere(co->chordal_env, root, arg)) {
131 DBG((dbg, LEVEL_1, "\t Member: %n %N\n", arg, arg));
132 if (is_optimizable(arg))
133 co_append_unit(co, arg);
134 unit->nodes[unit->node_count++] = arg;
139 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
140 } else if (is_Copy(root)) {
141 assert(!nodes_interfere(co->chordal_env, root, get_Copy_src(root)));
142 unit->nodes[unit->node_count++] = get_Copy_src(root);
143 unit->nodes = xrealloc(unit->nodes, 2 * sizeof(*unit->nodes));
145 assert(0 && "This is not an optimizable node!");
147 list_add_tail(&unit->units, &co->units);
148 /* Init ifg_mis_size to node_count. So get_lower_bound returns correct results. */
149 unit->ifg_mis_size = get_ifg_mis_size(unit);
152 static void co_collect_in_block(ir_node *block, void *env) {
153 copy_opt_t *co = env;
154 struct list_head *head = get_block_border_head(co->chordal_env, block);
157 list_for_each_entry_reverse(border_t, curr, head, list)
158 if (curr->is_def && curr->is_real && is_optimizable(curr->irn))
159 co_append_unit(co, curr->irn);
162 static void co_collect_units(copy_opt_t *co) {
163 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
164 co->roots = pset_new_ptr(64);
165 dom_tree_walk_irg(co->chordal_env->irg, co_collect_in_block, NULL, co);
169 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env) {
170 const char *s1, *s2, *s3;
174 dbg = firm_dbg_register("ir.be.copyopt");
175 firm_dbg_set_mask(dbg, DEBUG_LVL);
177 co = xcalloc(1, sizeof(*co));
178 co->chordal_env = chordal_env;
180 s1 = get_irp_prog_name();
181 s2 = get_entity_name(get_irg_entity(co->chordal_env->irg));
182 s3 = chordal_env->cls->name;
183 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
184 co->name = xmalloc(len);
185 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
186 if (!strcmp(co->name, DEBUG_IRG))
187 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
189 firm_dbg_set_mask(dbg, DEBUG_LVL);
191 INIT_LIST_HEAD(&co->units);
192 co_collect_units(co);
196 void free_copy_opt(copy_opt_t *co) {
199 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
205 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
207 for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
208 ir_node *n = get_irn_out(irn, i);
209 if ((is_Phi(n) || is_Perm(n)) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
215 int co_get_copy_count(const copy_opt_t *co) {
218 list_for_each_entry(unit_t, curr, &co->units, units) {
219 int root_col = get_irn_col(co, curr->nodes[0]);
221 DBG((dbg, LEVEL_1, "%n %N has %d intf\n", curr->nodes[0], curr->nodes[0], curr->interf));
222 for (i=1; i<curr->node_count; ++i)
223 if (root_col != get_irn_col(co, curr->nodes[i])) {
224 DBG((dbg, LEVEL_1, " %n %N\n", curr->nodes[i], curr->nodes[i]));
231 int co_get_lower_bound(const copy_opt_t *co) {
234 list_for_each_entry(unit_t, curr, &co->units, units)
235 res += curr->interf + curr->node_count - curr->ifg_mis_size;
239 int co_get_interferer_count(const copy_opt_t *co) {
242 list_for_each_entry(unit_t, curr, &co->units, units)
248 * Needed for result checking
250 static void co_collect_for_checker(ir_node *block, void *env) {
251 copy_opt_t *co = env;
252 struct list_head *head = get_block_border_head(co->chordal_env, block);
255 list_for_each_entry_reverse(border_t, curr, head, list)
256 if (curr->is_def && curr->is_real && is_curr_reg_class(curr->irn))
257 obstack_ptr_grow(&co->ob, curr->irn);
261 * This O(n^2) checker checks if
262 * two ifg-connected nodes have the same color
263 * register constraint are satisfied
265 void co_check_allocation(copy_opt_t *co) {
266 ir_node **nodes, *n1, *n2;
269 obstack_init(&co->ob);
270 dom_tree_walk_irg(co->chordal_env->irg, co_collect_for_checker, NULL, co);
271 obstack_ptr_grow(&co->ob, NULL);
273 nodes = (ir_node **) obstack_finish(&co->ob);
274 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
275 assert(arch_reg_is_allocatable(co->chordal_env->arch_env, n1, arch_pos_make_out(0),
276 arch_get_irn_register(co->chordal_env->arch_env, n1, 0)) && "Constraint does not hold");
277 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
278 if (nodes_interfere(co->chordal_env, n1, n2)
279 && get_irn_col(co, n1) == get_irn_col(co, n2)) {
280 DBG((dbg, 0, "Error in graph %s: %n %d and %n %d have the same color %d.\n", co->name, n1, get_irn_graph_nr(n1), n2, get_irn_graph_nr(n2), get_irn_col(co, n1)));
281 assert(0 && "Interfering values have the same color!");
284 obstack_free(&co->ob, NULL);
285 DBG((dbg, 2, "The checker seems to be happy!\n"));