rework schedulers to register similar like regallocators/spillers
[libfirm] / ir / be / beschedrand.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       Random node selector.
23  * @author      Matthias Braun
24  * @date        29.08.2006
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "besched.h"
32 #include "belistsched.h"
33 #include "bemodule.h"
34
35 /**
36  * The random selector:
37  * Just assure that branches are executed last, otherwise select a random node
38  */
39 static ir_node *random_select(void *block_env, ir_nodeset_t *ready_set,
40                               ir_nodeset_t *live_set)
41 {
42         ir_nodeset_iterator_t iter;
43         ir_node          *irn      = NULL;
44         int only_branches_left = 1;
45         (void)block_env;
46         (void)live_set;
47
48         /* assure that branches and constants are executed last */
49         ir_nodeset_iterator_init(&iter, ready_set);
50         while ( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
51                 if (!is_cfop(irn)) {
52                         only_branches_left = 0;
53                         break;
54                 }
55         }
56
57         if (only_branches_left) {
58                 /* at last: schedule branches */
59                 ir_nodeset_iterator_init(&iter, ready_set);
60                 irn = ir_nodeset_iterator_next(&iter);
61         } else {
62                 do {
63                         /* take 1 random node */
64                         int n = rand() % ir_nodeset_size(ready_set);
65                         int i = 0;
66                         ir_nodeset_iterator_init(&iter, ready_set);
67                         while ((irn = ir_nodeset_iterator_next(&iter)) != NULL) {
68                                 if (i == n) {
69                                         break;
70                                 }
71                                 ++i;
72                         }
73                 } while (is_cfop(irn));
74         }
75
76         return irn;
77 }
78
79 static void *random_init_graph(ir_graph *irg)
80 {
81         (void)irg;
82         /* TODO: add commandline option for the seed */
83         srand(0x4711);
84         return NULL;
85 }
86
87 static void *random_init_block(void *graph_env, ir_node *block)
88 {
89         (void)graph_env;
90         (void)block;
91         return NULL;
92 }
93
94 static void sched_random(ir_graph *irg)
95 {
96         static const list_sched_selector_t random_selector = {
97                 random_init_graph,
98                 random_init_block,
99                 random_select,
100                 NULL,                /* node_ready */
101                 NULL,                /* node_selected */
102                 NULL,                /* finish_block */
103                 NULL                 /* finish_graph */
104         };
105         be_list_sched_graph(irg, &random_selector);
106 }
107
108 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_rand);
109 void be_init_sched_rand(void)
110 {
111         be_register_scheduler("random", sched_random);
112 }