fix more cparser warnings, cleanup some libcore code
[libfirm] / ir / be / beblocksched.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Block-scheduling strategies.
23  * @author      Matthias Braun, Christoph Mallon
24  * @date        27.09.2006
25  * @version     $Id$
26  *
27  * The goals of the greedy (and ILP) algorithm here works by assuming that
28  * we want to change as many jumps to fallthroughs as possible (executed jumps
29  * actually, we have to look at the execution frequencies). The algorithms
30  * do this by collecting execution frequencies of all branches (which is easily
31  * possible when all critical edges are split) then removes critical edges where
32  * possible as we don't need and want them anymore now. The algorithms then try
33  * to change as many edges to fallthroughs as possible, this is done by setting
34  * a next and prev pointers on blocks. The greedy algorithm sorts the edges by
35  * execution frequencies and tries to transform them to fallthroughs in this order
36  */
37 #include "config.h"
38
39 #include "beblocksched.h"
40
41 #include <stdlib.h>
42
43 #include "array.h"
44 #include "pdeq.h"
45
46 #include "iredges.h"
47 #include "irgwalk.h"
48 #include "irnode_t.h"
49 #include "irgraph_t.h"
50 #include "irloop.h"
51 #include "irprintf.h"
52 #include "execfreq.h"
53 #include "irdump_t.h"
54 #include "irtools.h"
55 #include "debug.h"
56 #include "beirgmod.h"
57 #include "bemodule.h"
58 #include "be.h"
59 #include "error.h"
60
61 #include "lc_opts.h"
62 #include "lc_opts_enum.h"
63
64 #include "lpp.h"
65 #include "lpp_net.h"
66
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
68
69 typedef enum blocksched_algos_t {
70         BLOCKSCHED_NAIV, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
71 } blocksched_algos_t;
72
73 static int algo = BLOCKSCHED_GREEDY;
74
75 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
76         { "naiv",   BLOCKSCHED_NAIV },
77         { "greedy", BLOCKSCHED_GREEDY },
78         { "ilp",    BLOCKSCHED_ILP },
79         { NULL,     0 }
80 };
81
82 static lc_opt_enum_int_var_t algo_var = {
83         &algo, blockschedalgo_items
84 };
85
86 static const lc_opt_table_entry_t be_blocksched_options[] = {
87         LC_OPT_ENT_ENUM_INT ("blockscheduler", "the block scheduling algorithm", &algo_var),
88         LC_OPT_LAST
89 };
90
91 /*
92  *   ____                   _
93  *  / ___|_ __ ___  ___  __| |_   _
94  * | |  _| '__/ _ \/ _ \/ _` | | | |
95  * | |_| | | |  __/  __/ (_| | |_| |
96  *  \____|_|  \___|\___|\__,_|\__, |
97  *                            |___/
98  */
99
100 typedef struct blocksched_entry_t blocksched_entry_t;
101 struct blocksched_entry_t {
102         ir_node            *block;
103         blocksched_entry_t *next;
104         blocksched_entry_t *prev;
105 };
106
107 typedef struct edge_t edge_t;
108 struct edge_t {
109         ir_node *block;             /**< source block */
110         int     pos;                /**< number of cfg predecessor (target) */
111         double  execfreq;           /**< the frequency */
112         double  outedge_penalty_freq; /**< for edges leaving the loop this is the
113                                            penality when we make them a
114                                            fallthrough. */
115         int     highest_execfreq;   /**< flag that indicates whether this edge is
116                                          the edge with the highest execfreq pointing
117                                          away from this block */
118 };
119
120 typedef struct blocksched_env_t blocksched_env_t;
121 struct blocksched_env_t {
122         ir_graph       *irg;
123         struct obstack *obst;
124         ir_exec_freq   *execfreqs;
125         edge_t         *edges;
126         pdeq           *worklist;
127         int            blockcount;
128 };
129
130 /**
131  * Collect cfg frequencies of all edges between blocks.
132  * Also determines edge with highest frequency.
133  */
134 static void collect_egde_frequency(ir_node *block, void *data)
135 {
136         blocksched_env_t   *env = (blocksched_env_t*)data;
137         int                arity;
138         edge_t             edge;
139         blocksched_entry_t *entry;
140         ir_loop            *loop;
141
142         memset(&edge, 0, sizeof(edge));
143
144         entry = OALLOCZ(env->obst, blocksched_entry_t);
145         entry->block = block;
146         set_irn_link(block, entry);
147
148         loop = get_irn_loop(block);
149
150         arity = get_Block_n_cfgpreds(block);
151
152         if (arity == 0) {
153                 /* must be the start block (or end-block for endless loops),
154                  * everything else is dead code and should be removed by now */
155                 assert(block == get_irg_start_block(env->irg)
156                                 || block == get_irg_end_block(env->irg));
157                 /* nothing to do here */
158                 return;
159         } else if (arity == 1) {
160                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
161                 ir_loop *pred_loop  = get_irn_loop(pred_block);
162                 float    freq       = (float)get_block_execfreq(env->execfreqs, block);
163
164                 /* is it an edge leaving a loop */
165                 if (get_loop_depth(pred_loop) > get_loop_depth(loop)) {
166                         float pred_freq = (float)get_block_execfreq(env->execfreqs, pred_block);
167                         edge.outedge_penalty_freq = -(pred_freq - freq);
168                 }
169
170                 edge.block            = block;
171                 edge.pos              = 0;
172                 edge.execfreq         = freq;
173                 edge.highest_execfreq = 1;
174                 ARR_APP1(edge_t, env->edges, edge);
175         } else {
176                 int    i;
177                 double highest_execfreq = -1.0;
178                 int    highest_edge_num = -1;
179
180                 edge.block = block;
181                 for (i = 0; i < arity; ++i) {
182                         double  execfreq;
183                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
184
185                         execfreq = get_block_execfreq(env->execfreqs, pred_block);
186
187                         edge.pos              = i;
188                         edge.execfreq         = execfreq;
189                         edge.highest_execfreq = 0;
190                         ARR_APP1(edge_t, env->edges, edge);
191
192                         if (execfreq > highest_execfreq) {
193                                 highest_execfreq = execfreq;
194                                 highest_edge_num = ARR_LEN(env->edges) - 1;
195                         }
196                 }
197
198                 if (highest_edge_num >= 0)
199                         env->edges[highest_edge_num].highest_execfreq = 1;
200         }
201 }
202
203 static int cmp_edges(const void *d1, const void *d2)
204 {
205         const edge_t *e1 = (const edge_t*)d1;
206         const edge_t *e2 = (const edge_t*)d2;
207
208         return QSORT_CMP(e2->execfreq, e1->execfreq);
209 }
210
211 static int cmp_edges_outedge_penalty(const void *d1, const void *d2)
212 {
213         const edge_t *e1 = (const edge_t*)d1;
214         const edge_t *e2 = (const edge_t*)d2;
215         /* reverse sorting as penalties are negative */
216         return QSORT_CMP(e1->outedge_penalty_freq, e2->outedge_penalty_freq);
217 }
218
219 static void clear_loop_links(ir_loop *loop)
220 {
221         int i, n;
222
223         set_loop_link(loop, NULL);
224         n = get_loop_n_elements(loop);
225         for (i = 0; i < n; ++i) {
226                 loop_element elem = get_loop_element(loop, i);
227                 if (*elem.kind == k_ir_loop) {
228                         clear_loop_links(elem.son);
229                 }
230         }
231 }
232
233 static void coalesce_blocks(blocksched_env_t *env)
234 {
235         int i;
236         int edge_count = ARR_LEN(env->edges);
237         edge_t *edges = env->edges;
238
239         /* sort interblock edges by execution frequency */
240         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
241
242         /* run1: only look at jumps */
243         for (i = 0; i < edge_count; ++i) {
244                 const edge_t *edge  = &edges[i];
245                 ir_node      *block = edge->block;
246                 int           pos   = edge->pos;
247                 ir_node      *pred_block;
248                 blocksched_entry_t *entry, *pred_entry;
249
250                 /* only check edge with highest frequency */
251                 if (! edge->highest_execfreq)
252                         continue;
253
254                 /* the block might have been removed already... */
255                 if (is_Bad(get_Block_cfgpred(block, 0)))
256                         continue;
257
258                 pred_block = get_Block_cfgpred_block(block, pos);
259                 entry      = (blocksched_entry_t*)get_irn_link(block);
260                 pred_entry = (blocksched_entry_t*)get_irn_link(pred_block);
261
262                 if (pred_entry->next != NULL || entry->prev != NULL)
263                         continue;
264
265                 /* only coalesce jumps */
266                 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
267                         continue;
268
269                 /* schedule the 2 blocks behind each other */
270                 DB((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
271                            pred_entry->block, entry->block, edge->execfreq));
272                 pred_entry->next = entry;
273                 entry->prev      = pred_entry;
274         }
275
276         /* run2: pick loop fallthroughs */
277         clear_loop_links(get_irg_loop(env->irg));
278
279         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges_outedge_penalty);
280         for (i = 0; i < edge_count; ++i) {
281                 const edge_t *edge  = &edges[i];
282                 ir_node      *block = edge->block;
283                 int           pos   = edge->pos;
284                 ir_node      *pred_block;
285                 blocksched_entry_t *entry, *pred_entry;
286                 ir_loop      *loop;
287                 ir_loop      *outer_loop;
288
289                 /* already seen all loop outedges? */
290                 if (edge->outedge_penalty_freq == 0)
291                         break;
292
293                 /* the block might have been removed already... */
294                 if (is_Bad(get_Block_cfgpred(block, pos)))
295                         continue;
296
297                 pred_block = get_Block_cfgpred_block(block, pos);
298                 entry      = (blocksched_entry_t*)get_irn_link(block);
299                 pred_entry = (blocksched_entry_t*)get_irn_link(pred_block);
300
301                 if (pred_entry->next != NULL || entry->prev != NULL)
302                         continue;
303
304                 /* we want at most 1 outedge fallthrough per loop */
305                 loop = get_irn_loop(pred_block);
306                 if (get_loop_link(loop) != NULL)
307                         continue;
308
309                 /* schedule the 2 blocks behind each other */
310                 DB((dbg, LEVEL_1, "Coalesce (Loop Outedge) %+F -> %+F (%.3g)\n",
311                            pred_entry->block, entry->block, edge->execfreq));
312                 pred_entry->next = entry;
313                 entry->prev      = pred_entry;
314
315                 /* all loops left have an outedge now */
316                 outer_loop = get_irn_loop(block);
317                 do {
318                         /* we set loop link to loop to mark it */
319                         set_loop_link(loop, loop);
320                         loop = get_loop_outer_loop(loop);
321                 } while (loop != outer_loop);
322         }
323
324         /* sort interblock edges by execution frequency */
325         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
326
327         /* run3: remaining edges */
328         for (i = 0; i < edge_count; ++i) {
329                 const edge_t *edge  = &edges[i];
330                 ir_node      *block = edge->block;
331                 int           pos   = edge->pos;
332                 ir_node      *pred_block;
333                 blocksched_entry_t *entry, *pred_entry;
334
335                 /* the block might have been removed already... */
336                 if (is_Bad(get_Block_cfgpred(block, pos)))
337                         continue;
338
339                 pred_block = get_Block_cfgpred_block(block, pos);
340                 entry      = (blocksched_entry_t*)get_irn_link(block);
341                 pred_entry = (blocksched_entry_t*)get_irn_link(pred_block);
342
343                 /* is 1 of the blocks already attached to another block? */
344                 if (pred_entry->next != NULL || entry->prev != NULL)
345                         continue;
346
347                 /* schedule the 2 blocks behind each other */
348                 DB((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
349                            pred_entry->block, entry->block, edge->execfreq));
350                 pred_entry->next = entry;
351                 entry->prev      = pred_entry;
352         }
353 }
354
355 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
356 {
357         ir_node            *block = entry->block;
358         ir_node            *succ  = NULL;
359         blocksched_entry_t *succ_entry;
360         const ir_edge_t    *edge;
361         double             best_succ_execfreq;
362
363         if (irn_visited_else_mark(block))
364                 return;
365
366         env->blockcount++;
367
368         DB((dbg, LEVEL_1, "Pick succ of %+F\n", block));
369
370         /* put all successors into the worklist */
371         foreach_block_succ(block, edge) {
372                 ir_node *succ_block = get_edge_src_irn(edge);
373
374                 if (irn_visited(succ_block))
375                         continue;
376
377                 /* we only need to put the first of a series of already connected
378                  * blocks into the worklist */
379                 succ_entry = (blocksched_entry_t*)get_irn_link(succ_block);
380                 while (succ_entry->prev != NULL) {
381                         /* break cycles... */
382                         if (succ_entry->prev->block == succ_block) {
383                                 succ_entry->prev->next = NULL;
384                                 succ_entry->prev       = NULL;
385                                 break;
386                         }
387                         succ_entry = succ_entry->prev;
388                 }
389
390                 if (irn_visited(succ_entry->block))
391                         continue;
392
393                 DB((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
394                 pdeq_putr(env->worklist, succ_entry->block);
395         }
396
397         if (entry->next != NULL) {
398                 pick_block_successor(entry->next, env);
399                 return;
400         }
401
402         DB((dbg, LEVEL_1, "deciding...\n"));
403         best_succ_execfreq = -1;
404
405         /* no successor yet: pick the successor block with the highest execution
406          * frequency which has no predecessor yet */
407
408         foreach_block_succ(block, edge) {
409                 ir_node *succ_block = get_edge_src_irn(edge);
410                 double  execfreq;
411
412                 if (irn_visited(succ_block))
413                         continue;
414
415                 succ_entry = (blocksched_entry_t*)get_irn_link(succ_block);
416                 if (succ_entry->prev != NULL)
417                         continue;
418
419                 execfreq = get_block_execfreq(env->execfreqs, succ_block);
420                 if (execfreq > best_succ_execfreq) {
421                         best_succ_execfreq = execfreq;
422                         succ = succ_block;
423                 }
424         }
425
426         if (succ == NULL) {
427                 DB((dbg, LEVEL_1, "pick from worklist\n"));
428
429                 do {
430                         if (pdeq_empty(env->worklist)) {
431                                 DB((dbg, LEVEL_1, "worklist empty\n"));
432                                 return;
433                         }
434                         succ = (ir_node*)pdeq_getl(env->worklist);
435                 } while (irn_visited(succ));
436         }
437
438         succ_entry       = (blocksched_entry_t*)get_irn_link(succ);
439         entry->next      = succ_entry;
440         succ_entry->prev = entry;
441
442         pick_block_successor(succ_entry, env);
443 }
444
445 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
446 {
447         ir_graph           *irg        = env->irg;
448         ir_node            *startblock = get_irg_start_block(irg);
449         blocksched_entry_t *entry      = (blocksched_entry_t*)get_irn_link(startblock);
450
451         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
452         inc_irg_visited(irg);
453
454         env->worklist = new_pdeq();
455         pick_block_successor(entry, env);
456         assert(pdeq_empty(env->worklist));
457         del_pdeq(env->worklist);
458
459         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
460
461         return entry;
462 }
463
464 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
465                                                                                 int count, struct obstack* obst)
466 {
467         int                i = 0;
468         ir_node            **block_list;
469         blocksched_entry_t *entry;
470         (void) env;
471
472         block_list = NEW_ARR_D(ir_node *, obst, count);
473         DB((dbg, LEVEL_1, "Blockschedule:\n"));
474
475         for (entry = first; entry != NULL; entry = entry->next) {
476                 assert(i < count);
477                 block_list[i++] = entry->block;
478                 DB((dbg, LEVEL_1, "\t%+F\n", entry->block));
479         }
480         assert(i == count);
481
482         return block_list;
483 }
484
485 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
486 {
487         blocksched_env_t   env;
488         struct obstack     obst;
489         blocksched_entry_t *start_entry;
490         ir_node            **block_list;
491
492         obstack_init(&obst);
493
494         env.irg        = irg;
495         env.obst       = &obst;
496         env.execfreqs  = execfreqs;
497         env.edges      = NEW_ARR_F(edge_t, 0);
498         env.worklist   = NULL;
499         env.blockcount = 0;
500
501         /* make sure loopinfo is up-to-date */
502         if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
503                 construct_cf_backedges(irg);
504         }
505
506         // collect edge execution frequencies
507         irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
508
509         (void)be_remove_empty_blocks(irg);
510
511         if (algo != BLOCKSCHED_NAIV)
512                 coalesce_blocks(&env);
513
514         start_entry = finish_block_schedule(&env);
515         block_list  = create_blocksched_array(&env, start_entry, env.blockcount,
516                                               be_get_be_obst(irg));
517
518         DEL_ARR_F(env.edges);
519         obstack_free(&obst, NULL);
520
521         return block_list;
522 }
523
524 /*
525  *  ___ _     ____
526  * |_ _| |   |  _ \
527  *  | || |   | |_) |
528  *  | || |___|  __/
529  * |___|_____|_|
530  *
531  */
532
533 typedef struct ilp_edge_t {
534         ir_node *block;   /**< source block */
535         int     pos;      /**< number of cfg predecessor (target) */
536         int     ilpvar;
537 } ilp_edge_t;
538
539 typedef struct blocksched_ilp_env_t {
540         blocksched_env_t env;
541         ilp_edge_t       *ilpedges;
542         lpp_t            *lpp;
543 } blocksched_ilp_env_t;
544
545 typedef struct blocksched_ilp_entry_t {
546         ir_node *block;
547         struct blocksched_entry_t *next;
548         struct blocksched_entry_t *prev;
549
550         int out_cst;
551 } blocksched_ilp_entry_t;
552
553 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
554 {
555         char       name[64];
556         ilp_edge_t edge;
557         int        edgeidx = ARR_LEN(env->ilpedges);
558
559         snprintf(name, sizeof(name), "edge%d", edgeidx);
560
561         edge.block  = block;
562         edge.pos    = pos;
563         edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
564
565         ARR_APP1(ilp_edge_t, env->ilpedges, edge);
566         return edgeidx;
567 }
568
569 static void collect_egde_frequency_ilp(ir_node *block, void *data)
570 {
571         blocksched_ilp_env_t *env        = data;
572         ir_graph             *irg        = env->env.irg;
573         ir_node              *startblock = get_irg_start_block(irg);
574         int                  arity;
575         lpp_cst_t            cst;
576         char                 name[64];
577         int                  out_count;
578         blocksched_ilp_entry_t *entry;
579
580         snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
581         out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
582
583         entry          = OALLOC(env->env.obst, blocksched_ilp_entry_t);
584         entry->block   = block;
585         entry->next    = NULL;
586         entry->prev    = NULL;
587         entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
588         set_irn_link(block, entry);
589
590         if (block == startblock)
591                 return;
592
593         arity = get_irn_arity(block);
594         if (arity == 1) {
595                 double execfreq = get_block_execfreq(env->env.execfreqs, block);
596                 add_ilp_edge(block, 0, execfreq, env);
597         }
598         else {
599                 int i;
600
601                 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
602                 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
603
604                 for (i = 0; i < arity; ++i) {
605                         double     execfreq;
606                         int        edgenum;
607                         ilp_edge_t *edge;
608                         ir_node    *pred_block = get_Block_cfgpred_block(block, i);
609
610                         execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
611                         edgenum  = add_ilp_edge(block, i, execfreq, env);
612                         edge     = &env->ilpedges[edgenum];
613                         lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
614                 }
615         }
616 }
617
618
619 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
620 {
621         int           edge_count = ARR_LEN(env->ilpedges);
622         be_options_t *options    = be_get_irg_options(env->env.irg);
623         int           i;
624
625         /* complete out constraints */
626         for (i = 0; i < edge_count; ++i) {
627                 const ilp_edge_t *edge  = &env->ilpedges[i];
628                 ir_node          *block = edge->block;
629                 ir_node          *pred;
630                 blocksched_ilp_entry_t *entry;
631
632                 /* the block might have been removed already... */
633                 if (is_Bad(get_Block_cfgpred(block, 0)))
634                         continue;
635
636                 pred  = get_Block_cfgpred_block(block, edge->pos);
637                 entry = get_irn_link(pred);
638
639                 DB((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
640                                   pred, block, edge->pos));
641                 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
642         }
643
644         lpp_solve_net(env->lpp, options->ilp_server, options->ilp_solver);
645         assert(lpp_is_sol_valid(env->lpp));
646
647         /* Apply results to edges */
648         for (i = 0; i < edge_count; ++i) {
649                 const ilp_edge_t   *edge  = &env->ilpedges[i];
650                 ir_node            *block = edge->block;
651                 ir_node            *pred;
652                 int                is_jump;
653                 blocksched_entry_t *entry;
654                 blocksched_entry_t *pred_entry;
655
656                 /* the block might have been removed already... */
657                 if (is_Bad(get_Block_cfgpred(block, 0)))
658                         continue;
659
660                 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
661                 if (is_jump)
662                         continue;
663
664                 pred       = get_Block_cfgpred_block(block, edge->pos);
665                 entry      = get_irn_link(block);
666                 pred_entry = get_irn_link(pred);
667
668                 assert(entry->prev == NULL && pred_entry->next == NULL);
669                 entry->prev      = pred_entry;
670                 pred_entry->next = entry;
671         }
672 }
673
674 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
675 {
676         blocksched_ilp_env_t env;
677         struct obstack       obst;
678         blocksched_entry_t   *start_entry;
679         ir_node              **block_list;
680
681         obstack_init(&obst);
682
683         env.env.irg        = irg;
684         env.env.obst       = &obst;
685         env.env.execfreqs  = execfreqs;
686         env.env.worklist   = NULL;
687         env.env.blockcount = 0;
688         env.ilpedges       = NEW_ARR_F(ilp_edge_t, 0);
689
690         env.lpp = new_lpp("blockschedule", lpp_minimize);
691         lpp_set_time_limit(env.lpp, 20);
692         lpp_set_log(env.lpp, stdout);
693
694         irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
695
696         (void)be_remove_empty_blocks(irg);
697         coalesce_blocks_ilp(&env);
698
699         start_entry = finish_block_schedule(&env.env);
700         block_list  = create_blocksched_array(&env.env, start_entry,
701                                               env.env.blockcount,
702                                               be_get_be_obst(irg));
703
704         DEL_ARR_F(env.ilpedges);
705         free_lpp(env.lpp);
706         obstack_free(&obst, NULL);
707
708         return block_list;
709 }
710
711 /*
712  *  __  __       _
713  * |  \/  | __ _(_)_ __
714  * | |\/| |/ _` | | '_ \
715  * | |  | | (_| | | | | |
716  * |_|  |_|\__,_|_|_| |_|
717  *
718  */
719 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched)
720 void be_init_blocksched(void)
721 {
722         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
723
724         lc_opt_add_table(be_grp, be_blocksched_options);
725
726         FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
727 }
728
729 ir_node **be_create_block_schedule(ir_graph *irg)
730 {
731         ir_exec_freq *execfreqs = be_get_irg_exec_freq(irg);
732
733         switch (algo) {
734         case BLOCKSCHED_GREEDY:
735         case BLOCKSCHED_NAIV:
736                 return create_block_schedule_greedy(irg, execfreqs);
737         case BLOCKSCHED_ILP:
738                 return create_block_schedule_ilp(irg, execfreqs);
739         }
740
741         panic("unknown blocksched algo");
742 }