5162eec0d929926a434057f5d27e30505494d9ba
[libfirm] / ir / be / beschedrand.c
1 /*
2  * Copyright (C) 1995-2007 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  * Trivial node selector.
22  * @author Matthias Braun
23  * @date   29.08.2006
24  * @cvs-id $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdlib.h>
31
32 #include "besched_t.h"
33 #include "belistsched.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         const arch_env_t *arch_env = block_env;
44         ir_node          *irn      = NULL;
45         int only_branches_left = 1;
46
47         /* assure that branches and constants are executed last */
48         ir_nodeset_iterator_init(&iter, ready_set);
49         while( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
50                 if (! arch_irn_class_is(arch_env, irn, branch)) {
51                         only_branches_left = 0;
52                         break;
53                 }
54         }
55
56         if(only_branches_left) {
57                 /* at last: schedule branches */
58                 ir_nodeset_iterator_init(&iter, ready_set);
59                 irn = ir_nodeset_iterator_next(&iter);
60         } else {
61                 do {
62                         // take 1 random node
63                         int n = rand() % ir_nodeset_size(ready_set);
64                         int i = 0;
65                         ir_nodeset_iterator_init(&iter, ready_set);
66                         while( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
67                                 if(i == n) {
68                                         break;
69                                 }
70                                 ++i;
71                         }
72                 } while(arch_irn_class_is(arch_env, irn, branch));
73         }
74
75         return irn;
76 }
77
78 static void *random_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
79 {
80         /* Using time(NULL) as a seed here gives really random results,
81            but is NOT deterministic which makes debugging impossible.
82            Moreover no-one want non-deterministic compilers ... */
83         srand(0x4711);
84         return (void *)arch_env;
85 }
86
87 static void *random_init_block(void *graph_env, ir_node *bl)
88 {
89         return graph_env;
90 }
91
92 static const list_sched_selector_t random_selector_struct = {
93         random_init_graph,
94         random_init_block,
95         random_select,
96         NULL,                /* to_appear_in_schedule */
97         NULL,                /* node_ready */
98         NULL,                /* node_selected */
99         NULL,                /* exectime */
100         NULL,                /* latency */
101         NULL,                /* finish_block */
102         NULL                 /* finish_graph */
103 };
104
105 const list_sched_selector_t *random_selector = &random_selector_struct;