4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 #include "bechordal_t.h"
22 #include "becopyopt.h"
23 #include "becopystat.h"
25 #define DEBUG_LVL 0 //SET_LEVEL_1
26 static firm_dbg_module_t *dbg = NULL;
28 #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)
30 #define MIN(a,b) ((a<b)?(a):(b))
31 #define MAX(a,b) ((a<b)?(b):(a))
34 * Computes the weight of a 'max independent set' wrt. ifg-edges only
35 * (no coloring conflicts, no register constraints)
36 * @return The costs of such a mis
37 * NOTE: Code adapted from becopyheur
38 * BETTER: Here we can be sure having a chordal graph to work on,
39 * so, for 'larger' opt-units we could use a special algorithm.
41 static int ou_max_ind_set_costs(unit_t *ou) {
43 int max, pos, curr_weight, best_weight = 0;
46 irns = alloca((ou->node_count-1) * sizeof(*irns));
47 curr = bitset_alloca(ou->node_count-1);
49 /* brute force the best set */
51 while ((max = bitset_popcnt(curr)) != 0) {
52 /* check if curr is a stable set */
53 int i, o, is_stable_set = 1;
54 bitset_foreach(curr, pos)
55 irns[pos] = ou->nodes[1+pos];
57 for(o=i+1; o<max; ++o) /* !!!!! difference to qnode_max_ind_set(): NOT o=i */
58 if (nodes_interfere(ou->co->chordal_env, irns[i], irns[o])) {
64 /* calc current weigth */
66 bitset_foreach(curr, pos)
67 curr_weight += ou->costs[1+pos];
70 if (curr_weight > best_weight)
71 best_weight = curr_weight;
80 * Builds an optimization unit for a given optimizable irn (root).
81 * This opt-unit is inserted in the main structure co.
82 * If an arg of root itself is optimizable process this arg before with a
83 * recursive call. For handling this situation and loops co->root is used
84 * to remember all roots.
86 static void co_append_unit(copy_opt_t *co, ir_node *root) {
89 struct list_head *tmp;
91 DBG((dbg, LEVEL_1, "\t Root: %n %N\n", root, root));
92 /* check if we encountered this root earlier */
93 if (pset_find_ptr(co->roots, root))
95 pset_insert_ptr(co->roots, root);
97 assert(is_curr_reg_class(root) && "node is in wrong register class!");
100 arity = get_irn_arity(root);
101 unit = xcalloc(1, sizeof(*unit));
103 unit->node_count = 1;
104 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
105 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
106 unit->nodes[0] = root;
107 unit->complete_costs = 0;
109 INIT_LIST_HEAD(&unit->queue);
113 for (i=0; i<arity; ++i) {
114 ir_node *arg = get_irn_n(root, i);
115 assert(is_curr_reg_class(arg) && "Argument not in same register class.");
118 if (nodes_interfere(co->chordal_env, root, arg))
119 assert(0 && "root and arg interfere");
120 DBG((dbg, LEVEL_1, "\t Member: %n %N\n", arg, arg));
122 /* check if arg has occurred at a prior position in the arg/list */
123 for (o=0; o<unit->node_count; ++o)
124 if (unit->nodes[o] == arg) {
129 if (!arg_pos) { /* a new argument */
130 /* insert node, set costs */
131 unit->nodes[unit->node_count] = arg;
132 unit->costs[unit->node_count] = co->get_costs(root, arg, i);
134 } else { /* arg has occured before in same phi */
135 /* increase costs for existing arg */
136 unit->costs[arg_pos] = co->get_costs(root, arg, i);
140 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
141 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
142 } else if (is_Copy(co->chordal_env->arch_env, root)) {
143 assert(!nodes_interfere(co->chordal_env, root, get_Copy_src(root)));
144 unit->nodes[1] = get_Copy_src(root);
145 unit->costs[1] = co->get_costs(root, unit->nodes[1], -1);
146 unit->node_count = 2;
147 unit->nodes = xrealloc(unit->nodes, 2 * sizeof(*unit->nodes));
148 unit->costs = xrealloc(unit->costs, 2 * sizeof(*unit->costs));
150 assert(0 && "This is not an optimizable node!");
151 /* TODO add ou's for 2-addr-code instructions */
154 for(i=1; i<unit->node_count; ++i) {
155 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
156 unit->complete_costs += unit->costs[i];
159 /* insert according to average costs */
161 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
163 list_add(&unit->units, tmp);
165 /* Init ifg_mis_size to node_count. So get_lower_bound returns correct results. */
166 unit->minimal_costs = unit->complete_costs - ou_max_ind_set_costs(unit);
169 static void co_collect_in_block(ir_node *block, void *env) {
170 copy_opt_t *co = env;
171 struct list_head *head = get_block_border_head(co->chordal_env, block);
174 list_for_each_entry_reverse(border_t, curr, head, list)
175 if (curr->is_def && curr->is_real && is_optimizable(co->chordal_env->arch_env, curr->irn))
176 co_append_unit(co, curr->irn);
179 static void co_collect_units(copy_opt_t *co) {
180 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
181 co->roots = pset_new_ptr(64);
182 dom_tree_walk_irg(co->chordal_env->irg, co_collect_in_block, NULL, co);
186 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
187 const char *s1, *s2, *s3;
191 dbg = firm_dbg_register("ir.be.copyopt");
192 firm_dbg_set_mask(dbg, DEBUG_LVL);
194 co = xcalloc(1, sizeof(*co));
195 co->chordal_env = chordal_env;
196 co->get_costs = get_costs;
198 s1 = get_irp_prog_name();
199 s2 = get_entity_name(get_irg_entity(co->chordal_env->irg));
200 s3 = chordal_env->cls->name;
201 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
202 co->name = xmalloc(len);
203 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
204 if (!strcmp(co->name, DEBUG_IRG))
205 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
207 firm_dbg_set_mask(dbg, DEBUG_LVL);
209 INIT_LIST_HEAD(&co->units);
210 co_collect_units(co);
214 void free_copy_opt(copy_opt_t *co) {
217 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
223 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
225 for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
226 ir_node *n = get_irn_out(irn, i);
227 if ((is_Phi(n) || is_Perm(co->chordal_env->arch_env, n)) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
233 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
236 ir_node *root_block = get_nodes_block(root);
238 assert(pos==-1 || is_Phi(root));
240 /* a perm places the copy in the same block as it resides */
241 loop = get_irn_loop(root_block);
243 /* for phis the copies are placed in the corresponding pred-block */
244 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
247 cost = 2*get_loop_depth(loop);
251 int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
255 int co_get_copy_costs(const copy_opt_t *co) {
258 list_for_each_entry(unit_t, curr, &co->units, units) {
259 int root_col = get_irn_col(co, curr->nodes[0]);
260 for (i=1; i<curr->node_count; ++i)
261 if (root_col != get_irn_col(co, curr->nodes[i])) {
262 DBG((dbg, LEVEL_1, " %n %N\n", curr->nodes[i], curr->nodes[i]));
263 res += curr->costs[i];
269 int co_get_lower_bound(const copy_opt_t *co) {
272 list_for_each_entry(unit_t, curr, &co->units, units)
273 res += curr->minimal_costs;