amd64: small changes w.r.t. stack alignment.
[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 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "besched.h"
32 #include "belistsched.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                               ir_nodeset_t *live_set)
40 {
41         ir_nodeset_iterator_t iter;
42         ir_node          *irn      = NULL;
43         int only_branches_left = 1;
44         (void)block_env;
45         (void)live_set;
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 (!is_cfop(irn)) {
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 (is_cfop(irn));
73         }
74
75         return irn;
76 }
77
78 static void *random_init_graph(const list_sched_selector_t *vtab, const be_irg_t *birg)
79 {
80         (void)vtab;
81         (void)birg;
82         /* Using time(NULL) as a seed here gives really random results,
83            but is NOT deterministic which makes debugging impossible.
84            Moreover no-one want non-deterministic compilers ... */
85         srand(0x4711);
86         return NULL;
87 }
88
89 static void *random_init_block(void *graph_env, ir_node *block)
90 {
91         (void)graph_env;
92         (void)block;
93         return NULL;
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 };