8524abb2ca54150bf02e6b78c3c87fc8153f154f
[libfirm] / ir / be / beschedrand.c
1 /**
2  * Trivial node selector.
3  * @author Christian Wuerdig
4  * @date   29.08.2006
5  * @cvs-id $Id$
6  */
7
8 #include <stdlib.h>
9
10 #include "besched_t.h"
11 #include "belistsched.h"
12
13 /**
14  * The random selector:
15  * Just assure that branches are executed last, otherwise select a random node
16  */
17 static ir_node *random_select(void *block_env, nodeset *ready_set, nodeset *live_set)
18 {
19         const arch_env_t *arch_env = block_env;
20         ir_node          *irn      = NULL;
21         int only_branches_left = 1;
22
23         /* assure that branches and constants are executed last */
24         for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
25                 if (! arch_irn_class_is(arch_env, irn, branch)) {
26                         only_branches_left = 0;
27                         nodeset_break(ready_set);
28                         break;
29                 }
30         }
31
32         if(only_branches_left) {
33                 /* at last: schedule branches */
34                 irn = nodeset_first(ready_set);
35                 nodeset_break(ready_set);
36         } else {
37                 do {
38                         // take 1 random node
39                         int n = rand() % pset_count(ready_set);
40                         int i = 0;
41                         for(irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
42                                 if(i == n) {
43                                         nodeset_break(ready_set);
44                                         break;
45                                 }
46                                 ++i;
47                         }
48                 } while(arch_irn_class_is(arch_env, irn, branch));
49         }
50
51         return irn;
52 }
53
54 static void *random_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
55 {
56         srand(time(0));
57         return (void *)arch_env;
58 }
59
60 static void *random_init_block(void *graph_env, ir_node *bl)
61 {
62         return graph_env;
63 }
64
65 static const list_sched_selector_t random_selector_struct = {
66         random_init_graph,
67         random_init_block,
68         random_select,
69         NULL,                /* to_appear_in_schedule */
70         NULL,                /* node_ready */
71         NULL,                /* node_selected */
72         NULL,                /* exectime */
73         NULL,                /* latency */
74         NULL,                /* finish_block */
75         NULL                 /* finish_graph */
76 };
77
78 const list_sched_selector_t *random_selector = &random_selector_struct;