implemented a function to retrieve estimated costs of an op
[libfirm] / ir / be / bechordal_main.c
1 /**
2  * @file   bechordal_main.c
3  * @date   29.11.2005
4  * @author Sebastian Hack
5  *
6  * Copyright (C) 2005 Universitaet Karlsruhe
7  * Released under the GPL
8  *
9  * Driver for the chordal register allocator.
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include "obst.h"
16 #include "pset.h"
17 #include "list.h"
18 #include "bitset.h"
19 #include "iterator.h"
20 #include "firm_config.h"
21
22 #ifdef WITH_LIBCORE
23 #include <libcore/lc_opts.h>
24 #include <libcore/lc_opts_enum.h>
25 #include <libcore/lc_timing.h>
26 #endif /* WITH_LIBCORE */
27
28 #include "irmode_t.h"
29 #include "irgraph_t.h"
30 #include "irprintf_t.h"
31 #include "irgwalk.h"
32 #include "irdump.h"
33 #include "irdom.h"
34 #include "irbitset.h"
35 #include "debug.h"
36 #include "xmalloc.h"
37
38 #include "bechordal_t.h"
39 #include "beabi.h"
40 #include "beutil.h"
41 #include "besched.h"
42 #include "benumb_t.h"
43 #include "besched_t.h"
44 #include "belive_t.h"
45 #include "bearch.h"
46 #include "beifg_t.h"
47 #include "beifg_impl.h"
48
49 #include "bespillbelady.h"
50 #include "bespillmorgan.h"
51 #include "belower.h"
52
53 #ifdef WITH_ILP
54 #include "bespillilp.h"
55 #include "bespillremat.h"
56 #include "bespillappel.h"
57 #endif /* WITH_ILP */
58
59 #include "becopystat.h"
60 #include "becopyopt.h"
61 #include "bessadestr.h"
62 #include "beverify.h"
63
64
65 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
66         const arch_env_t *arch_env = chordal_env->birg->main_env->arch_env;
67         struct obstack ob;
68         pmap_entry *pme;
69         ir_node **nodes, *n1, *n2;
70         int i, o;
71         DEBUG_ONLY(firm_dbg_module_t *dbg = chordal_env->dbg;)
72
73         /* Collect all irns */
74         obstack_init(&ob);
75         pmap_foreach(chordal_env->border_heads, pme) {
76                 border_t *curr;
77                 struct list_head *head = pme->value;
78                 list_for_each_entry(border_t, curr, head, list)
79                         if (curr->is_def && curr->is_real)
80                                 if (arch_get_irn_reg_class(arch_env, curr->irn, -1) == chordal_env->cls)
81                                         obstack_ptr_grow(&ob, curr->irn);
82         }
83         obstack_ptr_grow(&ob, NULL);
84         nodes = (ir_node **) obstack_finish(&ob);
85
86         /* Check them */
87         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
88                 const arch_register_t *n1_reg, *n2_reg;
89
90                 n1_reg = arch_get_irn_register(arch_env, n1);
91                 if (!arch_reg_is_allocatable(arch_env, n1, -1, n1_reg)) {
92                         DBG((dbg, 0, "Register %s assigned to %+F is not allowed\n", n1_reg->name, n1));
93                         assert(0 && "Register constraint does not hold");
94                 }
95                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
96                         n2_reg = arch_get_irn_register(arch_env, n2);
97                         if (values_interfere(n1, n2) && n1_reg == n2_reg) {
98                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
99                                 assert(0 && "Interfering values have the same color!");
100                         }
101                 }
102         }
103         obstack_free(&ob, NULL);
104 }
105
106 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
107 {
108         if(env->ifg)
109                 return be_ifg_connected(env->ifg, a, b);
110         else
111                 return values_interfere(a, b);
112 }
113
114
115 static be_ra_chordal_opts_t options = {
116         BE_CH_DUMP_NONE,
117         BE_CH_SPILL_BELADY,
118         BE_CH_COPYMIN_HEUR1,
119         BE_CH_IFG_STD,
120         BE_CH_LOWER_PERM_SWAP,
121         BE_CH_VRFY_WARN,
122 };
123
124 #ifdef WITH_LIBCORE
125 static const lc_opt_enum_int_items_t spill_items[] = {
126         { "morgan", BE_CH_SPILL_MORGAN },
127         { "belady", BE_CH_SPILL_BELADY },
128 #ifdef WITH_ILP
129         { "ilp",    BE_CH_SPILL_ILP },
130         { "remat",  BE_CH_SPILL_REMAT },
131         { "appel",  BE_CH_SPILL_APPEL },
132 #endif
133         { NULL, 0 }
134 };
135
136 static const lc_opt_enum_int_items_t copymin_items[] = {
137         { "none",  BE_CH_COPYMIN_NONE      },
138         { "heur1", BE_CH_COPYMIN_HEUR1     },
139         { "heur2", BE_CH_COPYMIN_HEUR2     },
140         { "stat",  BE_CH_COPYMIN_STAT      },
141         { "park",  BE_CH_COPYMIN_PARK_MOON },
142 #ifdef WITH_ILP
143         { "ilp1",  BE_CH_COPYMIN_ILP1 },
144         { "ilp2",  BE_CH_COPYMIN_ILP2 },
145 #endif
146         { NULL, 0 }
147 };
148
149 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
150         { "std",  BE_CH_IFG_STD },
151         { "fast", BE_CH_IFG_FAST },
152         { NULL, 0 }
153 };
154
155 static const lc_opt_enum_int_items_t lower_perm_items[] = {
156         { "copy", BE_CH_LOWER_PERM_COPY },
157         { "swap", BE_CH_LOWER_PERM_SWAP },
158         { NULL, 0 }
159 };
160
161 static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
162         { NULL, 0 }
163 };
164
165 static const lc_opt_enum_int_items_t dump_items[] = {
166         { "spill",    BE_CH_DUMP_SPILL     },
167         { "live",     BE_CH_DUMP_LIVE      },
168         { "color",    BE_CH_DUMP_COLOR     },
169         { "copymin",  BE_CH_DUMP_COPYMIN   },
170         { "ssadestr", BE_CH_DUMP_SSADESTR  },
171         { "tree",     BE_CH_DUMP_TREE_INTV },
172         { "constr",   BE_CH_DUMP_CONSTR    },
173         { "lower",    BE_CH_DUMP_LOWER     },
174         { "all",      BE_CH_DUMP_ALL       },
175         { NULL, 0 }
176 };
177
178 static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
179         { "off",    BE_CH_VRFY_OFF    },
180         { "warn",   BE_CH_VRFY_WARN   },
181         { "assert", BE_CH_VRFY_ASSERT },
182         { NULL, 0 }
183 };
184
185 static lc_opt_enum_int_var_t spill_var = {
186         &options.spill_method, spill_items
187 };
188
189 static lc_opt_enum_int_var_t copymin_var = {
190         &options.copymin_method, copymin_items
191 };
192
193 static lc_opt_enum_int_var_t ifg_flavor_var = {
194         &options.ifg_flavor, ifg_flavor_items
195 };
196
197 static lc_opt_enum_int_var_t lower_perm_var = {
198         &options.lower_perm_opt, lower_perm_items
199 };
200
201 static lc_opt_enum_int_var_t dump_var = {
202         &options.dump_flags, dump_items
203 };
204
205 static lc_opt_enum_int_var_t be_ch_vrfy_var = {
206         &options.vrfy_option, be_ch_vrfy_items
207 };
208
209 static const lc_opt_table_entry_t be_chordal_options[] = {
210         LC_OPT_ENT_ENUM_MASK("spill",   "spill method (belady, ilp, remat or appel)", &spill_var),
211         LC_OPT_ENT_ENUM_PTR ("copymin", "copymin method (none, heur1, heur2, ilp1, ilp2 or stat)", &copymin_var),
212         LC_OPT_ENT_ENUM_PTR ("ifg",     "interference graph flavour (std or fast)", &ifg_flavor_var),
213         LC_OPT_ENT_ENUM_PTR ("perm",    "perm lowering options (copy or swap)", &lower_perm_var),
214         LC_OPT_ENT_ENUM_MASK("dump",    "select dump phases", &dump_var),
215         LC_OPT_ENT_ENUM_PTR ("vrfy",    "verify options (off, warn, assert)", &be_ch_vrfy_var),
216         { NULL }
217 };
218
219 static void be_ra_chordal_register_options(lc_opt_entry_t *grp)
220 {
221         static int run_once = 0;
222         lc_opt_entry_t *chordal_grp;
223
224         if (! run_once) {
225                 run_once    = 1;
226                 chordal_grp = lc_opt_get_grp(grp, "chordal");
227
228                 lc_opt_add_table(chordal_grp, be_chordal_options);
229         }
230
231         co_register_options(chordal_grp);
232 }
233 #endif /* WITH_LIBCORE */
234
235 static void dump(unsigned mask, ir_graph *irg,
236                                  const arch_register_class_t *cls,
237                                  const char *suffix,
238                                  void (*dump_func)(ir_graph *, const char *))
239 {
240         if((options.dump_flags & mask) == mask) {
241                 if (cls) {
242                         char buf[256];
243                         snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
244                         be_dump(irg, buf, dump_func);
245                 }
246                 else
247                         be_dump(irg, suffix, dump_func);
248         }
249 }
250
251 static void put_ignore_colors(be_chordal_env_t *chordal_env)
252 {
253         int n_colors = chordal_env->cls->n_regs;
254         int i;
255
256         bitset_clear_all(chordal_env->ignore_colors);
257         be_abi_put_ignore_regs(chordal_env->birg->abi, chordal_env->cls, chordal_env->ignore_colors);
258         for(i = 0; i < n_colors; ++i)
259                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
260                         bitset_set(chordal_env->ignore_colors, i);
261 }
262
263 FILE *be_chordal_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
264 {
265         char buf[1024];
266
267         ir_snprintf(buf, sizeof(buf), "%s%F_%s.%s", prefix, env->irg, env->cls->name, suffix);
268         return fopen(buf, "wt");
269 }
270
271 void check_ifg_implementations(be_chordal_env_t *chordal_env)
272 {
273         FILE *f;
274
275         f = be_chordal_open(chordal_env, "std", "log");
276         chordal_env->ifg = be_ifg_std_new(chordal_env);
277         be_ifg_check_sorted(chordal_env->ifg, f);
278         fclose(f);
279
280         f = be_chordal_open(chordal_env, "list", "log");
281         chordal_env->ifg = be_ifg_list_new(chordal_env);
282         be_ifg_check_sorted(chordal_env->ifg, f);
283         fclose(f);
284
285         f = be_chordal_open(chordal_env, "clique", "log");
286         chordal_env->ifg = be_ifg_clique_new(chordal_env);
287         be_ifg_check_sorted(chordal_env->ifg, f);
288         fclose(f);
289
290         f = be_chordal_open(chordal_env, "pointer", "log");
291         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
292         be_ifg_check_sorted(chordal_env->ifg, f);
293         fclose(f);
294
295         chordal_env->ifg = NULL;
296 };
297
298 static void be_ra_chordal_main(const be_irg_t *bi)
299 {
300         const be_main_env_t *main_env = bi->main_env;
301         const arch_isa_t    *isa      = arch_env_get_isa(main_env->arch_env);
302         ir_graph            *irg      = bi->irg;
303         copy_opt_t          *co;
304
305         int j, m;
306         be_chordal_env_t chordal_env;
307
308         //lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
309         //unsigned long elapsed_milisec = 0;
310
311         compute_doms(irg);
312
313         chordal_env.opts          = &options;
314         chordal_env.irg           = irg;
315         chordal_env.birg          = bi;
316         chordal_env.dom_front     = be_compute_dominance_frontiers(irg);
317         FIRM_DBG_REGISTER(chordal_env.dbg, "firm.be.chordal");
318
319         obstack_init(&chordal_env.obst);
320
321         /* Perform the following for each register class. */
322         for(j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
323                 chordal_env.cls           = arch_isa_get_reg_class(isa, j);
324                 chordal_env.border_heads  = pmap_create();
325                 chordal_env.ignore_colors = bitset_malloc(chordal_env.cls->n_regs);
326
327                 /* put all ignore registers into the ignore register set. */
328                 put_ignore_colors(&chordal_env);
329
330                 be_liveness(irg);
331                 dump(BE_CH_DUMP_LIVE, irg, chordal_env.cls, "-live", dump_ir_block_graph_sched);
332
333                 /* spilling */
334                 switch(options.spill_method) {
335                 case BE_CH_SPILL_MORGAN:
336                         be_spill_morgan(&chordal_env);
337                         break;
338                 case BE_CH_SPILL_BELADY:
339                         be_spill_belady(&chordal_env);
340                         break;
341 #ifdef WITH_ILP
342                 case BE_CH_SPILL_ILP:
343                         be_spill_ilp(&chordal_env);
344                         break;
345                 case BE_CH_SPILL_REMAT:
346                         be_spill_remat(&chordal_env);
347                         break;
348                 case BE_CH_SPILL_APPEL:
349                         be_spill_appel(&chordal_env);
350                         break;
351 #endif /* WITH_ILP */
352                 default:
353                         fprintf(stderr, "no valid spiller selected. falling back to belady\n");
354                         be_spill_belady(&chordal_env);
355                 }
356                 dump(BE_CH_DUMP_SPILL, irg, chordal_env.cls, "-spill", dump_ir_block_graph_sched);
357                 be_abi_fix_stack_nodes(bi->abi);
358
359                 /* verify schedule and register pressure */
360                 if (options.vrfy_option == BE_CH_VRFY_WARN) {
361                         be_verify_schedule(irg);
362                         be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg);
363                 }
364                 else if (options.vrfy_option == BE_CH_VRFY_ASSERT) {
365                         assert(be_verify_schedule(irg) && "Schedule verification failed");
366                         assert(be_verify_register_pressure(chordal_env.birg->main_env->arch_env, chordal_env.cls, irg)
367                                 && "Register pressure verification failed");
368                 }
369
370                 /* Color the graph. */
371                 be_liveness(irg);
372                 be_ra_chordal_color(&chordal_env);
373                 dump(BE_CH_DUMP_CONSTR, irg, chordal_env.cls, "-color", dump_ir_block_graph_sched);
374
375                 /* Check the implementations of the ifg */
376                 //check_ifg_implementations(&chordal_env);
377
378                 /* Build the interference graph. */
379                 chordal_env.ifg = be_ifg_std_new(&chordal_env);
380                 //be_ifg_check(chordal_env.ifg);
381                 //be_ifg_check_sorted(chordal_env.ifg);
382
383
384                 /* start timer */
385                 //lc_timer_reset_and_start(timer);
386
387                 /* copy minimization */
388                 co = NULL;
389                 if (options.copymin_method != BE_CH_COPYMIN_NONE && options.copymin_method != BE_CH_COPYMIN_STAT) {
390                         co = new_copy_opt(&chordal_env, co_get_costs_loop_depth);
391                         co_build_ou_structure(co);
392                         co_build_graph_structure(co);
393                 }
394
395                 /* stop timer */
396                 //lc_timer_stop(timer);
397                 //elapsed_milisec = lc_timer_elapsed_msec(timer);
398
399                 //ir_printf("%u\n", elapsed_milisec);
400
401                 switch(options.copymin_method) {
402                         case BE_CH_COPYMIN_HEUR1:
403                                 co_solve_heuristic(co);
404                                 break;
405                         case BE_CH_COPYMIN_HEUR2:
406                                 co_solve_heuristic_new(co);
407                                 break;
408                         case BE_CH_COPYMIN_PARK_MOON:
409                                 co_solve_park_moon(co);
410                                 break;
411                         case BE_CH_COPYMIN_STAT:
412                                 co_compare_solvers(&chordal_env);
413                                 break;
414 #ifdef WITH_ILP
415                         case BE_CH_COPYMIN_ILP1:
416                                 printf("FIXME: %s:%d ILP1 not yet implemented!\n", __FILE__, __LINE__);
417                                 co_solve_ilp1(co, 60.0);
418                                 break;
419                         case BE_CH_COPYMIN_ILP2:
420                                 co_solve_ilp2(co, 60.0);
421                                 break;
422 #endif /* WITH_ILP */
423                         case BE_CH_COPYMIN_NONE:
424                         default:
425                                 break;
426                 }
427
428                 if (co) {
429                         co_free_graph_structure(co);
430                         co_free_ou_structure(co);
431                         free_copy_opt(co);
432                 }
433
434                 dump(BE_CH_DUMP_COPYMIN, irg, chordal_env.cls, "-copymin", dump_ir_block_graph_sched);
435                 be_ra_chordal_check(&chordal_env);
436
437                 /* ssa destruction */
438                 be_ssa_destruction(&chordal_env);
439                 dump(BE_CH_DUMP_SSADESTR, irg, chordal_env.cls, "-ssadestr", dump_ir_block_graph_sched);
440                 be_ssa_destruction_check(&chordal_env);
441                 be_ra_chordal_check(&chordal_env);
442
443                 copystat_dump(irg);
444
445                 be_ifg_free(chordal_env.ifg);
446                 pmap_destroy(chordal_env.border_heads);
447                 bitset_free(chordal_env.ignore_colors);
448         }
449
450         be_compute_spill_offsets(&chordal_env);
451
452         dump(BE_CH_DUMP_LOWER, irg, NULL, "-spilloff", dump_ir_block_graph_sched);
453
454         lower_nodes_after_ra(&chordal_env, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
455         dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
456
457         obstack_free(&chordal_env.obst, NULL);
458         be_free_dominance_frontiers(chordal_env.dom_front);
459 }
460
461 const be_ra_t be_ra_chordal_allocator = {
462 #ifdef WITH_LIBCORE
463         be_ra_chordal_register_options,
464 #endif
465         be_ra_chordal_main
466 };