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