some workaround to avoid condeval creating Phibs which not all backends like
[libfirm] / ir / be / beschedrand.c
1 /**
2  * Trivial node selector.
3  * @author Matthias Braun
4  * @date   29.08.2006
5  * @cvs-id $Id$
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "besched_t.h"
14 #include "belistsched.h"
15
16 /**
17  * The random selector:
18  * Just assure that branches are executed last, otherwise select a random node
19  */
20 static ir_node *random_select(void *block_env, ir_nodeset_t *ready_set,
21                               ir_nodeset_t *live_set)
22 {
23         ir_nodeset_iterator_t iter;
24         const arch_env_t *arch_env = block_env;
25         ir_node          *irn      = NULL;
26         int only_branches_left = 1;
27
28         /* assure that branches and constants are executed last */
29         ir_nodeset_iterator_init(&iter, ready_set);
30         while( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
31                 if (! arch_irn_class_is(arch_env, irn, branch)) {
32                         only_branches_left = 0;
33                         break;
34                 }
35         }
36
37         if(only_branches_left) {
38                 /* at last: schedule branches */
39                 ir_nodeset_iterator_init(&iter, ready_set);
40                 irn = ir_nodeset_iterator_next(&iter);
41         } else {
42                 do {
43                         // take 1 random node
44                         int n = rand() % ir_nodeset_size(ready_set);
45                         int i = 0;
46                         ir_nodeset_iterator_init(&iter, ready_set);
47                         while( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
48                                 if(i == n) {
49                                         break;
50                                 }
51                                 ++i;
52                         }
53                 } while(arch_irn_class_is(arch_env, irn, branch));
54         }
55
56         return irn;
57 }
58
59 static void *random_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
60 {
61         /* Using time(NULL) as a seed here gives really random results,
62            but is NOT deterministic which makes debugging impossible.
63            Moreover no-one want non-deterministic compilers ... */
64         srand(0x4711);
65         return (void *)arch_env;
66 }
67
68 static void *random_init_block(void *graph_env, ir_node *bl)
69 {
70         return graph_env;
71 }
72
73 static const list_sched_selector_t random_selector_struct = {
74         random_init_graph,
75         random_init_block,
76         random_select,
77         NULL,                /* to_appear_in_schedule */
78         NULL,                /* node_ready */
79         NULL,                /* node_selected */
80         NULL,                /* exectime */
81         NULL,                /* latency */
82         NULL,                /* finish_block */
83         NULL                 /* finish_graph */
84 };
85
86 const list_sched_selector_t *random_selector = &random_selector_struct;