make firm compilable with a c++ compiler
[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 = (blocksched_env_t*)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 = OALLOCZ(env->obst, blocksched_entry_t);
149         entry->block = block;
150         set_irn_link(block, entry);
151
152         loop = get_irn_loop(block);
153
154         arity = get_Block_n_cfgpreds(block);
155
156         if (arity == 0) {
157                 /* must be the start block (or end-block for endless loops),
158                  * everything else is dead code and should be removed by now */
159                 assert(block == get_irg_start_block(env->irg)
160                                 || block == get_irg_end_block(env->irg));
161                 /* nothing to do here */
162                 return;
163         } else if (arity == 1) {
164                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
165                 ir_loop *pred_loop  = get_irn_loop(pred_block);
166                 float    freq       = (float)get_block_execfreq(env->execfreqs, block);
167
168                 /* is it an edge leaving a loop */
169                 if (get_loop_depth(pred_loop) > get_loop_depth(loop)) {
170                         float pred_freq = (float)get_block_execfreq(env->execfreqs, pred_block);
171                         edge.outedge_penalty_freq = -(pred_freq - freq);
172                 }
173
174                 edge.block            = block;
175                 edge.pos              = 0;
176                 edge.execfreq         = freq;
177                 edge.highest_execfreq = 1;
178                 ARR_APP1(edge_t, env->edges, edge);
179         } else {
180                 int    i;
181                 double highest_execfreq = -1.0;
182                 int    highest_edge_num = -1;
183
184                 edge.block = block;
185                 for (i = 0; i < arity; ++i) {
186                         double  execfreq;
187                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
188
189                         execfreq = get_block_execfreq(env->execfreqs, pred_block);
190
191                         edge.pos              = i;
192                         edge.execfreq         = execfreq;
193                         edge.highest_execfreq = 0;
194                         ARR_APP1(edge_t, env->edges, edge);
195
196                         if (execfreq > highest_execfreq) {
197                                 highest_execfreq = execfreq;
198                                 highest_edge_num = ARR_LEN(env->edges) - 1;
199                         }
200                 }
201
202                 if (highest_edge_num >= 0)
203                         env->edges[highest_edge_num].highest_execfreq = 1;
204         }
205 }
206
207 static int cmp_edges(const void *d1, const void *d2)
208 {
209         const edge_t *e1 = (const edge_t*)d1;
210         const edge_t *e2 = (const edge_t*)d2;
211
212         return QSORT_CMP(e2->execfreq, e1->execfreq);
213 }
214
215 static int cmp_edges_outedge_penalty(const void *d1, const void *d2)
216 {
217         const edge_t *e1 = (const edge_t*)d1;
218         const edge_t *e2 = (const edge_t*)d2;
219         /* reverse sorting as penalties are negative */
220         return QSORT_CMP(e1->outedge_penalty_freq, e2->outedge_penalty_freq);
221 }
222
223 static void clear_loop_links(ir_loop *loop)
224 {
225         int i, n;
226
227         set_loop_link(loop, NULL);
228         n = get_loop_n_elements(loop);
229         for (i = 0; i < n; ++i) {
230                 loop_element elem = get_loop_element(loop, i);
231                 if (*elem.kind == k_ir_loop) {
232                         clear_loop_links(elem.son);
233                 }
234         }
235 }
236
237 static void coalesce_blocks(blocksched_env_t *env)
238 {
239         int i;
240         int edge_count = ARR_LEN(env->edges);
241         edge_t *edges = env->edges;
242
243         /* sort interblock edges by execution frequency */
244         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
245
246         /* run1: only look at jumps */
247         for (i = 0; i < edge_count; ++i) {
248                 const edge_t *edge  = &edges[i];
249                 ir_node      *block = edge->block;
250                 int           pos   = edge->pos;
251                 ir_node      *pred_block;
252                 blocksched_entry_t *entry, *pred_entry;
253
254                 /* only check edge with highest frequency */
255                 if (! edge->highest_execfreq)
256                         continue;
257
258                 /* the block might have been removed already... */
259                 if (is_Bad(get_Block_cfgpred(block, 0)))
260                         continue;
261
262                 pred_block = get_Block_cfgpred_block(block, pos);
263                 entry      = (blocksched_entry_t*)get_irn_link(block);
264                 pred_entry = (blocksched_entry_t*)get_irn_link(pred_block);
265
266                 if (pred_entry->next != NULL || entry->prev != NULL)
267                         continue;
268
269                 /* only coalesce jumps */
270                 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
271                         continue;
272
273                 /* schedule the 2 blocks behind each other */
274                 DB((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
275                            pred_entry->block, entry->block, edge->execfreq));
276                 pred_entry->next = entry;
277                 entry->prev      = pred_entry;
278         }
279
280         /* run2: pick loop fallthroughs */
281         clear_loop_links(get_irg_loop(env->irg));
282
283         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges_outedge_penalty);
284         for (i = 0; i < edge_count; ++i) {
285                 const edge_t *edge  = &edges[i];
286                 ir_node      *block = edge->block;
287                 int           pos   = edge->pos;
288                 ir_node      *pred_block;
289                 blocksched_entry_t *entry, *pred_entry;
290                 ir_loop      *loop;
291                 ir_loop      *outer_loop;
292
293                 /* already seen all loop outedges? */
294                 if (edge->outedge_penalty_freq == 0)
295                         break;
296
297                 /* the block might have been removed already... */
298                 if (is_Bad(get_Block_cfgpred(block, pos)))
299                         continue;
300
301                 pred_block = get_Block_cfgpred_block(block, pos);
302                 entry      = (blocksched_entry_t*)get_irn_link(block);
303                 pred_entry = (blocksched_entry_t*)get_irn_link(pred_block);
304
305                 if (pred_entry->next != NULL || entry->prev != NULL)
306                         continue;
307
308                 /* we want at most 1 outedge fallthrough per loop */
309                 loop = get_irn_loop(pred_block);
310                 if (get_loop_link(loop) != NULL)
311                         continue;
312
313                 /* schedule the 2 blocks behind each other */
314                 DB((dbg, LEVEL_1, "Coalesce (Loop Outedge) %+F -> %+F (%.3g)\n",
315                            pred_entry->block, entry->block, edge->execfreq));
316                 pred_entry->next = entry;
317                 entry->prev      = pred_entry;
318
319                 /* all loops left have an outedge now */
320                 outer_loop = get_irn_loop(block);
321                 do {
322                         /* we set loop link to loop to mark it */
323                         set_loop_link(loop, loop);
324                         loop = get_loop_outer_loop(loop);
325                 } while (loop != outer_loop);
326         }
327
328         /* sort interblock edges by execution frequency */
329         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
330
331         /* run3: remaining edges */
332         for (i = 0; i < edge_count; ++i) {
333                 const edge_t *edge  = &edges[i];
334                 ir_node      *block = edge->block;
335                 int           pos   = edge->pos;
336                 ir_node      *pred_block;
337                 blocksched_entry_t *entry, *pred_entry;
338
339                 /* the block might have been removed already... */
340                 if (is_Bad(get_Block_cfgpred(block, pos)))
341                         continue;
342
343                 pred_block = get_Block_cfgpred_block(block, pos);
344                 entry      = (blocksched_entry_t*)get_irn_link(block);
345                 pred_entry = (blocksched_entry_t*)get_irn_link(pred_block);
346
347                 /* is 1 of the blocks already attached to another block? */
348                 if (pred_entry->next != NULL || entry->prev != NULL)
349                         continue;
350
351                 /* schedule the 2 blocks behind each other */
352                 DB((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
353                            pred_entry->block, entry->block, edge->execfreq));
354                 pred_entry->next = entry;
355                 entry->prev      = pred_entry;
356         }
357 }
358
359 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
360 {
361         ir_node            *block = entry->block;
362         ir_node            *succ  = NULL;
363         blocksched_entry_t *succ_entry;
364         const ir_edge_t    *edge;
365         double             best_succ_execfreq;
366
367         if (irn_visited_else_mark(block))
368                 return;
369
370         env->blockcount++;
371
372         DB((dbg, LEVEL_1, "Pick succ of %+F\n", block));
373
374         /* put all successors into the worklist */
375         foreach_block_succ(block, edge) {
376                 ir_node *succ_block = get_edge_src_irn(edge);
377
378                 if (irn_visited(succ_block))
379                         continue;
380
381                 /* we only need to put the first of a series of already connected
382                  * blocks into the worklist */
383                 succ_entry = (blocksched_entry_t*)get_irn_link(succ_block);
384                 while (succ_entry->prev != NULL) {
385                         /* break cycles... */
386                         if (succ_entry->prev->block == succ_block) {
387                                 succ_entry->prev->next = NULL;
388                                 succ_entry->prev       = NULL;
389                                 break;
390                         }
391                         succ_entry = succ_entry->prev;
392                 };
393
394                 if (irn_visited(succ_entry->block))
395                         continue;
396
397                 DB((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
398                 pdeq_putr(env->worklist, succ_entry->block);
399         }
400
401         if (entry->next != NULL) {
402                 pick_block_successor(entry->next, env);
403                 return;
404         }
405
406         DB((dbg, LEVEL_1, "deciding...\n"));
407         best_succ_execfreq = -1;
408
409         /* no successor yet: pick the successor block with the highest execution
410          * frequency which has no predecessor yet */
411
412         foreach_block_succ(block, edge) {
413                 ir_node *succ_block = get_edge_src_irn(edge);
414                 double  execfreq;
415
416                 if (irn_visited(succ_block))
417                         continue;
418
419                 succ_entry = (blocksched_entry_t*)get_irn_link(succ_block);
420                 if (succ_entry->prev != NULL)
421                         continue;
422
423                 execfreq = get_block_execfreq(env->execfreqs, succ_block);
424                 if (execfreq > best_succ_execfreq) {
425                         best_succ_execfreq = execfreq;
426                         succ = succ_block;
427                 }
428         }
429
430         if (succ == NULL) {
431                 DB((dbg, LEVEL_1, "pick from worklist\n"));
432
433                 do {
434                         if (pdeq_empty(env->worklist)) {
435                                 DB((dbg, LEVEL_1, "worklist empty\n"));
436                                 return;
437                         }
438                         succ = (ir_node*)pdeq_getl(env->worklist);
439                 } while (irn_visited(succ));
440         }
441
442         succ_entry       = (blocksched_entry_t*)get_irn_link(succ);
443         entry->next      = succ_entry;
444         succ_entry->prev = entry;
445
446         pick_block_successor(succ_entry, env);
447 }
448
449 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
450 {
451         ir_graph           *irg        = env->irg;
452         ir_node            *startblock = get_irg_start_block(irg);
453         blocksched_entry_t *entry      = (blocksched_entry_t*)get_irn_link(startblock);
454
455         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
456         inc_irg_visited(irg);
457
458         env->worklist = new_pdeq();
459         pick_block_successor(entry, env);
460         assert(pdeq_empty(env->worklist));
461         del_pdeq(env->worklist);
462
463         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
464
465         return entry;
466 }
467
468 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
469                                                                                 int count, struct obstack* obst)
470 {
471         int                i = 0;
472         ir_node            **block_list;
473         blocksched_entry_t *entry;
474         (void) env;
475
476         block_list = NEW_ARR_D(ir_node *, obst, count);
477         DB((dbg, LEVEL_1, "Blockschedule:\n"));
478
479         for (entry = first; entry != NULL; entry = entry->next) {
480                 assert(i < count);
481                 block_list[i++] = entry->block;
482                 DB((dbg, LEVEL_1, "\t%+F\n", entry->block));
483         }
484         assert(i == count);
485
486         return block_list;
487 }
488
489 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
490 {
491         blocksched_env_t   env;
492         struct obstack     obst;
493         blocksched_entry_t *start_entry;
494         ir_node            **block_list;
495
496         obstack_init(&obst);
497
498         env.irg        = irg;
499         env.obst       = &obst;
500         env.execfreqs  = execfreqs;
501         env.edges      = NEW_ARR_F(edge_t, 0);
502         env.worklist   = NULL;
503         env.blockcount = 0;
504
505         /* make sure loopinfo is up-to-date */
506         if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
507                 construct_cf_backedges(irg);
508         }
509
510         // collect edge execution frequencies
511         irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
512
513         (void)be_remove_empty_blocks(irg);
514
515         if (algo != BLOCKSCHED_NAIV)
516                 coalesce_blocks(&env);
517
518         start_entry = finish_block_schedule(&env);
519         block_list  = create_blocksched_array(&env, start_entry, env.blockcount,
520                                               be_get_be_obst(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          = OALLOC(env->env.obst, blocksched_ilp_entry_t);
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           edge_count = ARR_LEN(env->ilpedges);
627         be_options_t *options    = be_get_irg_options(env->env.irg);
628         int           i;
629
630         /* complete out constraints */
631         for (i = 0; i < edge_count; ++i) {
632                 const ilp_edge_t *edge  = &env->ilpedges[i];
633                 ir_node          *block = edge->block;
634                 ir_node          *pred;
635                 blocksched_ilp_entry_t *entry;
636
637                 /* the block might have been removed already... */
638                 if (is_Bad(get_Block_cfgpred(block, 0)))
639                         continue;
640
641                 pred  = get_Block_cfgpred_block(block, edge->pos);
642                 entry = get_irn_link(pred);
643
644                 DB((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
645                                   pred, block, edge->pos));
646                 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
647         }
648
649         lpp_solve_net(env->lpp, options->ilp_server, options->ilp_solver);
650         assert(lpp_is_sol_valid(env->lpp));
651
652         /* Apply results to edges */
653         for (i = 0; i < edge_count; ++i) {
654                 const ilp_edge_t   *edge  = &env->ilpedges[i];
655                 ir_node            *block = edge->block;
656                 ir_node            *pred;
657                 int                is_jump;
658                 blocksched_entry_t *entry;
659                 blocksched_entry_t *pred_entry;
660
661                 /* the block might have been removed already... */
662                 if (is_Bad(get_Block_cfgpred(block, 0)))
663                         continue;
664
665                 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
666                 if (is_jump)
667                         continue;
668
669                 pred       = get_Block_cfgpred_block(block, edge->pos);
670                 entry      = get_irn_link(block);
671                 pred_entry = get_irn_link(pred);
672
673                 assert(entry->prev == NULL && pred_entry->next == NULL);
674                 entry->prev      = pred_entry;
675                 pred_entry->next = entry;
676         }
677 }
678
679 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
680 {
681         blocksched_ilp_env_t env;
682         struct obstack       obst;
683         blocksched_entry_t   *start_entry;
684         ir_node              **block_list;
685
686         obstack_init(&obst);
687
688         env.env.irg        = irg;
689         env.env.obst       = &obst;
690         env.env.execfreqs  = execfreqs;
691         env.env.worklist   = NULL;
692         env.env.blockcount = 0;
693         env.ilpedges       = NEW_ARR_F(ilp_edge_t, 0);
694
695         env.lpp = new_lpp("blockschedule", lpp_minimize);
696         lpp_set_time_limit(env.lpp, 20);
697         lpp_set_log(env.lpp, stdout);
698
699         irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
700
701         (void)be_remove_empty_blocks(irg);
702         coalesce_blocks_ilp(&env);
703
704         start_entry = finish_block_schedule(&env.env);
705         block_list  = create_blocksched_array(&env.env, start_entry,
706                                               env.env.blockcount,
707                                               be_get_be_obst(irg));
708
709         DEL_ARR_F(env.ilpedges);
710         free_lpp(env.lpp);
711         obstack_free(&obst, NULL);
712
713         return block_list;
714 }
715 #endif /* WITH_ILP */
716
717 /*
718  *  __  __       _
719  * |  \/  | __ _(_)_ __
720  * | |\/| |/ _` | | '_ \
721  * | |  | | (_| | | | | |
722  * |_|  |_|\__,_|_|_| |_|
723  *
724  */
725 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched);
726 void be_init_blocksched(void)
727 {
728         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
729         lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "blocksched");
730
731         lc_opt_add_table(blocksched_grp, be_blocksched_options);
732
733         FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
734 }
735
736 ir_node **be_create_block_schedule(ir_graph *irg)
737 {
738         ir_exec_freq *execfreqs = be_get_irg_exec_freq(irg);
739
740         switch (algo) {
741         case BLOCKSCHED_GREEDY:
742         case BLOCKSCHED_NAIV:
743                 return create_block_schedule_greedy(irg, execfreqs);
744 #ifdef WITH_ILP
745         case BLOCKSCHED_ILP:
746                 return create_block_schedule_ilp(irg, execfreqs);
747 #endif /* WITH_ILP */
748         }
749
750         panic("unknown blocksched algo");
751 }