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