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