belive: Avoid duplicate map lookups when calculating the liveness of a node in a...
[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  *
26  * The goals of the greedy (and ILP) algorithm here works by assuming that
27  * we want to change as many jumps to fallthroughs as possible (executed jumps
28  * actually, we have to look at the execution frequencies). The algorithms
29  * do this by collecting execution frequencies of all branches (which is easily
30  * possible when all critical edges are split) then removes critical edges where
31  * possible as we don't need and want them anymore now. The algorithms then try
32  * to change as many edges to fallthroughs as possible, this is done by setting
33  * a next and prev pointers on blocks. The greedy algorithm sorts the edges by
34  * execution frequencies and tries to transform them to fallthroughs in this order
35  */
36 #include "config.h"
37
38 #include "beblocksched.h"
39
40 #include <stdlib.h>
41
42 #include "array.h"
43 #include "pdeq.h"
44 #include "beirg.h"
45 #include "iredges.h"
46 #include "irgwalk.h"
47 #include "irnode_t.h"
48 #include "irgraph_t.h"
49 #include "irloop.h"
50 #include "irprintf.h"
51 #include "execfreq.h"
52 #include "irdump_t.h"
53 #include "irtools.h"
54 #include "util.h"
55 #include "debug.h"
56 #include "beirgmod.h"
57 #include "bemodule.h"
58 #include "be.h"
59 #include "error.h"
60
61 #include "lc_opts.h"
62 #include "lc_opts_enum.h"
63
64 #include "lpp.h"
65 #include "lpp_net.h"
66
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
68
69 typedef enum blocksched_algos_t {
70         BLOCKSCHED_NAIV, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
71 } blocksched_algos_t;
72
73 static int algo = BLOCKSCHED_GREEDY;
74
75 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
76         { "naiv",   BLOCKSCHED_NAIV },
77         { "greedy", BLOCKSCHED_GREEDY },
78         { "ilp",    BLOCKSCHED_ILP },
79         { NULL,     0 }
80 };
81
82 static lc_opt_enum_int_var_t algo_var = {
83         &algo, blockschedalgo_items
84 };
85
86 static const lc_opt_table_entry_t be_blocksched_options[] = {
87         LC_OPT_ENT_ENUM_INT ("blockscheduler", "the block scheduling algorithm", &algo_var),
88         LC_OPT_LAST
89 };
90
91 /*
92  *   ____                   _
93  *  / ___|_ __ ___  ___  __| |_   _
94  * | |  _| '__/ _ \/ _ \/ _` | | | |
95  * | |_| | | |  __/  __/ (_| | |_| |
96  *  \____|_|  \___|\___|\__,_|\__, |
97  *                            |___/
98  */
99
100 typedef struct blocksched_entry_t blocksched_entry_t;
101 struct blocksched_entry_t {
102         ir_node            *block;
103         blocksched_entry_t *next;
104         blocksched_entry_t *prev;
105 };
106
107 typedef struct edge_t edge_t;
108 struct edge_t {
109         ir_node *block;             /**< source block */
110         int     pos;                /**< number of cfg predecessor (target) */
111         double  execfreq;           /**< the frequency */
112         double  outedge_penalty_freq; /**< for edges leaving the loop this is the
113                                            penality when we make them a
114                                            fallthrough. */
115         int     highest_execfreq;   /**< flag that indicates whether this edge is
116                                          the edge with the highest execfreq pointing
117                                          away from this block */
118 };
119
120 typedef struct blocksched_env_t blocksched_env_t;
121 struct blocksched_env_t {
122         ir_graph       *irg;
123         struct obstack  obst;
124         edge_t         *edges;
125         pdeq           *worklist;
126         int            blockcount;
127 };
128
129 static blocksched_entry_t* get_blocksched_entry(const ir_node *block)
130 {
131         return (blocksched_entry_t*)get_irn_link(block);
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(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(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(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_base(const edge_t *e1, const edge_t *e2)
208 {
209         long nr1 = get_irn_node_nr(e1->block);
210         long nr2 = get_irn_node_nr(e2->block);
211         if (nr1 < nr2) {
212                 return 1;
213         } else if (nr1 > nr2) {
214                 return -1;
215         } else {
216                 if (e1->pos < e2->pos) {
217                         return 1;
218                 } else if (e1->pos > e2->pos) {
219                         return -1;
220                 } else {
221                         return 0;
222                 }
223         }
224 }
225
226 static int cmp_edges(const void *d1, const void *d2)
227 {
228         const edge_t *e1 = (const edge_t*)d1;
229         const edge_t *e2 = (const edge_t*)d2;
230         double        freq1 = e1->execfreq;
231         double        freq2 = e2->execfreq;
232         if (freq1 < freq2) {
233                 return 1;
234         } else if (freq1 > freq2) {
235                 return -1;
236         } else {
237                 return cmp_edges_base(e1, e2);
238         }
239 }
240
241 static int cmp_edges_outedge_penalty(const void *d1, const void *d2)
242 {
243         const edge_t *e1   = (const edge_t*)d1;
244         const edge_t *e2   = (const edge_t*)d2;
245         double        pen1 = e1->outedge_penalty_freq;
246         double        pen2 = e2->outedge_penalty_freq;
247         if (pen1 > pen2) {
248                 return 1;
249         } else if (pen1 < pen2) {
250                 return -1;
251         } else {
252                 return cmp_edges_base(e1, e2);
253         }
254 }
255
256 static void clear_loop_links(ir_loop *loop)
257 {
258         int i, n;
259
260         set_loop_link(loop, NULL);
261         n = get_loop_n_elements(loop);
262         for (i = 0; i < n; ++i) {
263                 loop_element elem = get_loop_element(loop, i);
264                 if (*elem.kind == k_ir_loop) {
265                         clear_loop_links(elem.son);
266                 }
267         }
268 }
269
270 static void coalesce_blocks(blocksched_env_t *env)
271 {
272         int i;
273         int edge_count = ARR_LEN(env->edges);
274         edge_t *edges = env->edges;
275
276         /* sort interblock edges by execution frequency */
277         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
278
279         /* run1: only look at jumps */
280         for (i = 0; i < edge_count; ++i) {
281                 const edge_t *edge  = &edges[i];
282                 ir_node      *block = edge->block;
283                 int           pos   = edge->pos;
284                 ir_node      *pred_block;
285                 blocksched_entry_t *entry, *pred_entry;
286
287                 /* only check edge with highest frequency */
288                 if (! edge->highest_execfreq)
289                         continue;
290
291                 /* the block might have been removed already... */
292                 if (is_Bad(get_Block_cfgpred(block, 0)))
293                         continue;
294
295                 pred_block = get_Block_cfgpred_block(block, pos);
296                 entry      = get_blocksched_entry(block);
297                 pred_entry = get_blocksched_entry(pred_block);
298
299                 if (pred_entry->next != NULL || entry->prev != NULL)
300                         continue;
301
302                 /* only coalesce jumps */
303                 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
304                         continue;
305
306                 /* schedule the 2 blocks behind each other */
307                 DB((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
308                            pred_entry->block, entry->block, edge->execfreq));
309                 pred_entry->next = entry;
310                 entry->prev      = pred_entry;
311         }
312
313         /* run2: pick loop fallthroughs */
314         clear_loop_links(get_irg_loop(env->irg));
315
316         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges_outedge_penalty);
317         for (i = 0; i < edge_count; ++i) {
318                 const edge_t *edge  = &edges[i];
319                 ir_node      *block = edge->block;
320                 int           pos   = edge->pos;
321                 ir_node      *pred_block;
322                 blocksched_entry_t *entry, *pred_entry;
323                 ir_loop      *loop;
324                 ir_loop      *outer_loop;
325
326                 /* already seen all loop outedges? */
327                 if (edge->outedge_penalty_freq == 0)
328                         break;
329
330                 /* the block might have been removed already... */
331                 if (is_Bad(get_Block_cfgpred(block, pos)))
332                         continue;
333
334                 pred_block = get_Block_cfgpred_block(block, pos);
335                 entry      = get_blocksched_entry(block);
336                 pred_entry = get_blocksched_entry(pred_block);
337
338                 if (pred_entry->next != NULL || entry->prev != NULL)
339                         continue;
340
341                 /* we want at most 1 outedge fallthrough per loop */
342                 loop = get_irn_loop(pred_block);
343                 if (get_loop_link(loop) != NULL)
344                         continue;
345
346                 /* schedule the 2 blocks behind each other */
347                 DB((dbg, LEVEL_1, "Coalesce (Loop Outedge) %+F -> %+F (%.3g)\n",
348                            pred_entry->block, entry->block, edge->execfreq));
349                 pred_entry->next = entry;
350                 entry->prev      = pred_entry;
351
352                 /* all loops left have an outedge now */
353                 outer_loop = get_irn_loop(block);
354                 do {
355                         /* we set loop link to loop to mark it */
356                         set_loop_link(loop, loop);
357                         loop = get_loop_outer_loop(loop);
358                 } while (loop != outer_loop);
359         }
360
361         /* sort interblock edges by execution frequency */
362         qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
363
364         /* run3: remaining edges */
365         for (i = 0; i < edge_count; ++i) {
366                 const edge_t *edge  = &edges[i];
367                 ir_node      *block = edge->block;
368                 int           pos   = edge->pos;
369                 ir_node      *pred_block;
370                 blocksched_entry_t *entry, *pred_entry;
371
372                 /* the block might have been removed already... */
373                 if (is_Bad(get_Block_cfgpred(block, pos)))
374                         continue;
375
376                 pred_block = get_Block_cfgpred_block(block, pos);
377                 entry      = get_blocksched_entry(block);
378                 pred_entry = get_blocksched_entry(pred_block);
379
380                 /* is 1 of the blocks already attached to another block? */
381                 if (pred_entry->next != NULL || entry->prev != NULL)
382                         continue;
383
384                 /* schedule the 2 blocks behind each other */
385                 DB((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
386                            pred_entry->block, entry->block, edge->execfreq));
387                 pred_entry->next = entry;
388                 entry->prev      = pred_entry;
389         }
390 }
391
392 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
393 {
394         ir_node            *block = entry->block;
395         ir_node            *succ  = NULL;
396         blocksched_entry_t *succ_entry;
397         double              best_succ_execfreq;
398
399         if (irn_visited_else_mark(block))
400                 return;
401
402         env->blockcount++;
403
404         DB((dbg, LEVEL_1, "Pick succ of %+F\n", block));
405
406         /* put all successors into the worklist */
407         foreach_block_succ(block, edge) {
408                 ir_node *succ_block = get_edge_src_irn(edge);
409
410                 if (irn_visited(succ_block))
411                         continue;
412
413                 /* we only need to put the first of a series of already connected
414                  * blocks into the worklist */
415                 succ_entry = get_blocksched_entry(succ_block);
416                 while (succ_entry->prev != NULL) {
417                         /* break cycles... */
418                         if (succ_entry->prev->block == succ_block) {
419                                 succ_entry->prev->next = NULL;
420                                 succ_entry->prev       = NULL;
421                                 break;
422                         }
423                         succ_entry = succ_entry->prev;
424                 }
425
426                 if (irn_visited(succ_entry->block))
427                         continue;
428
429                 DB((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
430                 pdeq_putr(env->worklist, succ_entry->block);
431         }
432
433         if (entry->next != NULL) {
434                 pick_block_successor(entry->next, env);
435                 return;
436         }
437
438         DB((dbg, LEVEL_1, "deciding...\n"));
439         best_succ_execfreq = -1;
440
441         /* no successor yet: pick the successor block with the highest execution
442          * frequency which has no predecessor yet */
443
444         foreach_block_succ(block, edge) {
445                 ir_node *succ_block = get_edge_src_irn(edge);
446
447                 if (irn_visited(succ_block))
448                         continue;
449
450                 succ_entry = get_blocksched_entry(succ_block);
451                 if (succ_entry->prev != NULL)
452                         continue;
453
454                 double execfreq = get_block_execfreq(succ_block);
455                 if (execfreq > best_succ_execfreq) {
456                         best_succ_execfreq = execfreq;
457                         succ = succ_block;
458                 }
459         }
460
461         if (succ == NULL) {
462                 DB((dbg, LEVEL_1, "pick from worklist\n"));
463
464                 do {
465                         if (pdeq_empty(env->worklist)) {
466                                 DB((dbg, LEVEL_1, "worklist empty\n"));
467                                 return;
468                         }
469                         succ = (ir_node*)pdeq_getl(env->worklist);
470                 } while (irn_visited(succ));
471         }
472
473         succ_entry       = get_blocksched_entry(succ);
474         entry->next      = succ_entry;
475         succ_entry->prev = entry;
476
477         pick_block_successor(succ_entry, env);
478 }
479
480 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
481 {
482         ir_graph           *irg        = env->irg;
483         ir_node            *startblock = get_irg_start_block(irg);
484         blocksched_entry_t *entry      = get_blocksched_entry(startblock);
485
486         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
487         inc_irg_visited(irg);
488
489         env->worklist = new_pdeq();
490         pick_block_successor(entry, env);
491         assert(pdeq_empty(env->worklist));
492         del_pdeq(env->worklist);
493
494         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
495
496         return entry;
497 }
498
499 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
500                                                                                 int count, struct obstack* obst)
501 {
502         int                i = 0;
503         ir_node            **block_list;
504         blocksched_entry_t *entry;
505         (void) env;
506
507         block_list = NEW_ARR_D(ir_node *, obst, count);
508         DB((dbg, LEVEL_1, "Blockschedule:\n"));
509
510         for (entry = first; entry != NULL; entry = entry->next) {
511                 assert(i < count);
512                 block_list[i++] = entry->block;
513                 DB((dbg, LEVEL_1, "\t%+F\n", entry->block));
514         }
515         assert(i == count);
516
517         return block_list;
518 }
519
520 static ir_node **create_block_schedule_greedy(ir_graph *irg)
521 {
522         blocksched_env_t   env;
523         blocksched_entry_t *start_entry;
524         ir_node            **block_list;
525
526         env.irg        = irg;
527         env.edges      = NEW_ARR_F(edge_t, 0);
528         env.worklist   = NULL;
529         env.blockcount = 0;
530         obstack_init(&env.obst);
531
532         assure_loopinfo(irg);
533
534         // collect edge execution frequencies
535         irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
536
537         (void)be_remove_empty_blocks(irg);
538
539         if (algo != BLOCKSCHED_NAIV)
540                 coalesce_blocks(&env);
541
542         start_entry = finish_block_schedule(&env);
543         block_list  = create_blocksched_array(&env, start_entry, env.blockcount,
544                                               be_get_be_obst(irg));
545
546         DEL_ARR_F(env.edges);
547         obstack_free(&env.obst, NULL);
548
549         return block_list;
550 }
551
552 /*
553  *  ___ _     ____
554  * |_ _| |   |  _ \
555  *  | || |   | |_) |
556  *  | || |___|  __/
557  * |___|_____|_|
558  *
559  */
560
561 typedef struct ilp_edge_t {
562         ir_node *block;   /**< source block */
563         int     pos;      /**< number of cfg predecessor (target) */
564         int     ilpvar;
565 } ilp_edge_t;
566
567 typedef struct blocksched_ilp_env_t {
568         blocksched_env_t env;
569         ilp_edge_t       *ilpedges;
570         lpp_t            *lpp;
571 } blocksched_ilp_env_t;
572
573 typedef struct blocksched_ilp_entry_t {
574         ir_node *block;
575         struct blocksched_entry_t *next;
576         struct blocksched_entry_t *prev;
577
578         int out_cst;
579 } blocksched_ilp_entry_t;
580
581 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
582 {
583         char       name[64];
584         ilp_edge_t edge;
585         int        edgeidx = ARR_LEN(env->ilpedges);
586
587         snprintf(name, sizeof(name), "edge%d", edgeidx);
588
589         edge.block  = block;
590         edge.pos    = pos;
591         edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
592
593         ARR_APP1(ilp_edge_t, env->ilpedges, edge);
594         return edgeidx;
595 }
596
597 static void collect_egde_frequency_ilp(ir_node *block, void *data)
598 {
599         blocksched_ilp_env_t *env        = (blocksched_ilp_env_t*)data;
600         ir_graph             *irg        = env->env.irg;
601         ir_node              *startblock = get_irg_start_block(irg);
602         int                  arity;
603         char                 name[64];
604         int                  out_count;
605         blocksched_ilp_entry_t *entry;
606
607         snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
608         out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
609
610         entry          = OALLOC(&env->env.obst, blocksched_ilp_entry_t);
611         entry->block   = block;
612         entry->next    = NULL;
613         entry->prev    = NULL;
614         entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater_equal, out_count - 1);
615         set_irn_link(block, entry);
616
617         if (block == startblock)
618                 return;
619
620         arity = get_irn_arity(block);
621         if (arity == 1) {
622                 double execfreq = get_block_execfreq(block);
623                 add_ilp_edge(block, 0, execfreq, env);
624         }
625         else {
626                 int i;
627                 int cst_idx;
628
629                 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
630                 cst_idx = lpp_add_cst_uniq(env->lpp, name, lpp_greater_equal, arity - 1);
631
632                 for (i = 0; i < arity; ++i) {
633                         double     execfreq;
634                         int        edgenum;
635                         ilp_edge_t *edge;
636                         ir_node    *pred_block = get_Block_cfgpred_block(block, i);
637
638                         execfreq = get_block_execfreq(pred_block);
639                         edgenum  = add_ilp_edge(block, i, execfreq, env);
640                         edge     = &env->ilpedges[edgenum];
641                         lpp_set_factor_fast(env->lpp, cst_idx, edge->ilpvar, 1.0);
642                 }
643         }
644 }
645
646 static blocksched_ilp_entry_t *get_blocksched_ilp_entry(const ir_node *block)
647 {
648         return (blocksched_ilp_entry_t*)get_irn_link(block);
649 }
650
651 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
652 {
653         int           edge_count = ARR_LEN(env->ilpedges);
654         int           i;
655
656         /* complete out constraints */
657         for (i = 0; i < edge_count; ++i) {
658                 const ilp_edge_t *edge  = &env->ilpedges[i];
659                 ir_node          *block = edge->block;
660                 ir_node          *pred;
661                 blocksched_ilp_entry_t *entry;
662
663                 /* the block might have been removed already... */
664                 if (is_Bad(get_Block_cfgpred(block, 0)))
665                         continue;
666
667                 pred  = get_Block_cfgpred_block(block, edge->pos);
668                 entry = get_blocksched_ilp_entry(pred);
669
670                 DB((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
671                                   pred, block, edge->pos));
672                 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
673         }
674
675         lpp_solve_net(env->lpp, be_options.ilp_server, be_options.ilp_solver);
676         assert(lpp_is_sol_valid(env->lpp));
677
678         /* Apply results to edges */
679         for (i = 0; i < edge_count; ++i) {
680                 const ilp_edge_t   *edge  = &env->ilpedges[i];
681                 ir_node            *block = edge->block;
682                 ir_node            *pred;
683                 int                is_jump;
684                 blocksched_entry_t *entry;
685                 blocksched_entry_t *pred_entry;
686
687                 /* the block might have been removed already... */
688                 if (is_Bad(get_Block_cfgpred(block, 0)))
689                         continue;
690
691                 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
692                 if (is_jump)
693                         continue;
694
695                 pred       = get_Block_cfgpred_block(block, edge->pos);
696                 entry      = get_blocksched_entry(block);
697                 pred_entry = get_blocksched_entry(pred);
698
699                 assert(entry->prev == NULL && pred_entry->next == NULL);
700                 entry->prev      = pred_entry;
701                 pred_entry->next = entry;
702         }
703 }
704
705 static ir_node **create_block_schedule_ilp(ir_graph *irg)
706 {
707         blocksched_ilp_env_t env;
708         blocksched_entry_t   *start_entry;
709         ir_node              **block_list;
710
711         env.env.irg        = irg;
712         env.env.worklist   = NULL;
713         env.env.blockcount = 0;
714         env.ilpedges       = NEW_ARR_F(ilp_edge_t, 0);
715         obstack_init(&env.env.obst);
716
717         env.lpp = lpp_new("blockschedule", lpp_minimize);
718         lpp_set_time_limit(env.lpp, 20);
719         lpp_set_log(env.lpp, stdout);
720
721         irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
722
723         (void)be_remove_empty_blocks(irg);
724         coalesce_blocks_ilp(&env);
725
726         start_entry = finish_block_schedule(&env.env);
727         block_list  = create_blocksched_array(&env.env, start_entry,
728                                               env.env.blockcount,
729                                               be_get_be_obst(irg));
730
731         DEL_ARR_F(env.ilpedges);
732         lpp_free(env.lpp);
733         obstack_free(&env.env.obst, NULL);
734
735         return block_list;
736 }
737
738 /*
739  *  __  __       _
740  * |  \/  | __ _(_)_ __
741  * | |\/| |/ _` | | '_ \
742  * | |  | | (_| | | | | |
743  * |_|  |_|\__,_|_|_| |_|
744  *
745  */
746 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched)
747 void be_init_blocksched(void)
748 {
749         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
750
751         lc_opt_add_table(be_grp, be_blocksched_options);
752
753         FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
754 }
755
756 ir_node **be_create_block_schedule(ir_graph *irg)
757 {
758         switch (algo) {
759         case BLOCKSCHED_GREEDY:
760         case BLOCKSCHED_NAIV:
761                 return create_block_schedule_greedy(irg);
762         case BLOCKSCHED_ILP:
763                 return create_block_schedule_ilp(irg);
764         }
765
766         panic("unknown blocksched algo");
767 }