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