bescripts: Remove unused execution unit specification.
[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         int only_branches_left = 1;
41         (void)block_env;
42
43         /* assure that branches and constants are executed last */
44         foreach_ir_nodeset(ready_set, irn, iter) {
45                 if (!is_cfop(irn)) {
46                         only_branches_left = 0;
47                         break;
48                 }
49         }
50
51         ir_node *rand_node = NULL;
52         if (only_branches_left) {
53                 /* at last: schedule branches */
54                 rand_node = ir_nodeset_first(ready_set);
55         } else {
56                 do {
57                         /* take 1 random node */
58                         int n = rand() % ir_nodeset_size(ready_set);
59                         int i = 0;
60                         foreach_ir_nodeset(ready_set, irn, iter) {
61                                 rand_node = irn;
62                                 if (i == n) {
63                                         break;
64                                 }
65                                 ++i;
66                         }
67                 } while (is_cfop(rand_node));
68         }
69
70         return rand_node;
71 }
72
73 static void *random_init_graph(ir_graph *irg)
74 {
75         (void)irg;
76         /* TODO: add commandline option for the seed */
77         srand(0x4711);
78         return NULL;
79 }
80
81 static void *random_init_block(void *graph_env, ir_node *block)
82 {
83         (void)graph_env;
84         (void)block;
85         return NULL;
86 }
87
88 static void sched_random(ir_graph *irg)
89 {
90         static const list_sched_selector_t random_selector = {
91                 random_init_graph,
92                 random_init_block,
93                 random_select,
94                 NULL,                /* node_ready */
95                 NULL,                /* node_selected */
96                 NULL,                /* finish_block */
97                 NULL                 /* finish_graph */
98         };
99         be_list_sched_graph(irg, &random_selector);
100 }
101
102 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_rand)
103 void be_init_sched_rand(void)
104 {
105         be_register_scheduler("random", sched_random);
106 }