typo fixed
[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  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32
33 #include "besched_t.h"
34 #include "belistsched.h"
35
36 /**
37  * The random selector:
38  * Just assure that branches are executed last, otherwise select a random node
39  */
40 static ir_node *random_select(void *block_env, ir_nodeset_t *ready_set,
41                               ir_nodeset_t *live_set)
42 {
43         ir_nodeset_iterator_t iter;
44         const arch_env_t *arch_env = block_env;
45         ir_node          *irn      = NULL;
46         int only_branches_left = 1;
47         (void) live_set;
48
49         /* assure that branches and constants are executed last */
50         ir_nodeset_iterator_init(&iter, ready_set);
51         while( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
52                 if (! arch_irn_class_is(arch_env, irn, branch)) {
53                         only_branches_left = 0;
54                         break;
55                 }
56         }
57
58         if (only_branches_left) {
59                 /* at last: schedule branches */
60                 ir_nodeset_iterator_init(&iter, ready_set);
61                 irn = ir_nodeset_iterator_next(&iter);
62         } else {
63                 do {
64                         /* take 1 random node */
65                         int n = rand() % ir_nodeset_size(ready_set);
66                         int i = 0;
67                         ir_nodeset_iterator_init(&iter, ready_set);
68                         while ((irn = ir_nodeset_iterator_next(&iter)) != NULL) {
69                                 if(i == n) {
70                                         break;
71                                 }
72                                 ++i;
73                         }
74                 } while(arch_irn_class_is(arch_env, irn, branch));
75         }
76
77         return irn;
78 }
79
80 static void *random_init_graph(const list_sched_selector_t *vtab, const be_irg_t *birg)
81 {
82         (void) vtab;
83         /* Using time(NULL) as a seed here gives really random results,
84            but is NOT deterministic which makes debugging impossible.
85            Moreover no-one want non-deterministic compilers ... */
86         srand(0x4711);
87         return (void *) be_get_birg_arch_env(birg);
88 }
89
90 static void *random_init_block(void *graph_env, ir_node *block)
91 {
92         (void) block;
93         return graph_env;
94 }
95
96 const list_sched_selector_t random_selector = {
97         random_init_graph,
98         random_init_block,
99         random_select,
100         NULL,                /* to_appear_in_schedule */
101         NULL,                /* node_ready */
102         NULL,                /* node_selected */
103         NULL,                /* exectime */
104         NULL,                /* latency */
105         NULL,                /* finish_block */
106         NULL                 /* finish_graph */
107 };