Save.
[cacao.git] / src / vm / jit / schedule / schedule.c
1 /* vm/jit/schedule/schedule.c - architecture independent instruction scheduler
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: schedule.c 1963 2005-02-23 17:03:53Z twisti $
32
33 */
34
35
36 #include <stdio.h>
37
38 #include "disass.h"
39
40 #include "mm/memory.h"
41 #include "vm/jit/schedule/schedule.h"
42
43
44 scheduledata *schedule_setup(registerdata *rd)
45 {
46         scheduledata *sd;
47
48         sd = DNEW(scheduledata);
49
50         sd->intregs_read_dep = DMNEW(minstruction*, rd->intregsnum);
51         sd->intregs_write_dep = DMNEW(minstruction*, rd->intregsnum);
52
53         sd->fltregs_read_dep = DMNEW(minstruction*, rd->fltregsnum);
54         sd->fltregs_write_dep = DMNEW(minstruction*, rd->fltregsnum);
55
56         /* XXX: memory is currently only one cell */
57 /*      sd->memory_write_dep; */
58
59         /* clear all pointers */
60
61         MSET(sd->intregs_read_dep, 0, minstruction*, rd->intregsnum);
62         MSET(sd->intregs_write_dep, 0, minstruction*, rd->intregsnum);
63
64         MSET(sd->fltregs_read_dep, 0, minstruction*, rd->fltregsnum);
65         MSET(sd->fltregs_write_dep, 0, minstruction*, rd->fltregsnum);
66
67         sd->memory_write_dep = NULL;
68
69         return sd;
70 }
71
72
73 minstruction *schedule_prepend_minstruction(minstruction *mi)
74 {
75         minstruction *tmpmi;
76
77         /* add new instruction in front of the list */
78
79         tmpmi = DNEW(minstruction);
80
81         tmpmi->latency = 0;
82         tmpmi->priority = 0;
83         tmpmi->opdep[0] = NULL;
84         tmpmi->opdep[1] = NULL;
85         tmpmi->opdep[2] = NULL;
86         tmpmi->issinknode = true;           /* initially all nodes are sink nodes */
87
88         tmpmi->next = mi;                   /* link to next instruction           */
89
90         return tmpmi;
91 }
92
93
94 /* schedule_calc_priority ******************************************************
95
96    Calculates the current node's priority by getting highest priority
97    of the dependency nodes, adding this nodes latency plus 1 (for the
98    instruction itself).
99
100 *******************************************************************************/
101
102 void schedule_calc_priority(minstruction *mi)
103 {
104         s4 i;
105         s4 pathpriority;
106
107         /* check all dependencies for their priority */
108
109         pathpriority = 0;
110
111         for (i = 0; i < 3; i++) {
112                 if (mi->opdep[i]) {
113                         if (mi->opdep[i]->priority > pathpriority)
114                                 pathpriority = mi->opdep[i]->priority;
115
116                         /* dependent node is non-sink node */
117
118                         mi->opdep[i]->issinknode = false;
119                 }
120         }
121
122         mi->priority = pathpriority + mi->latency + 1;
123 }
124
125
126 /* schedule_do_schedule ********************************************************
127
128    XXX
129
130 *******************************************************************************/
131
132 void schedule_do_schedule(minstruction *mi)
133 {
134         minstruction *rootmi;
135         sinknode     *rootsn;
136         sinknode     *sn;
137         s4            i;
138         s4            icount;               /* number of basic block instruction  */
139
140         /* initialize variables */
141
142         rootmi = mi;
143         rootsn = NULL;
144         icount = 0;
145
146         /* find all sink nodes */
147
148         while (mi) {
149                 icount++;
150
151                 /* if current node is a sink node, add it to the sink node list */
152
153                 if (mi->issinknode) {
154                         sn = DNEW(sinknode);
155                         sn->mi = mi;
156                         sn->next = rootsn;
157                         rootsn = sn;
158                 }
159
160                 mi = mi->next;
161         }
162
163
164         /* walk through the instructions and do the actual scheduling */
165
166         for (i = 0; i < icount; i++) {
167                 sn = rootsn;
168
169                 /* first, find the highest priority path */
170
171                 while (sn)
172                         sn = sn->next;
173         }
174
175
176         printf("bb start ---\n");
177
178         mi = rootmi;
179
180         while (mi) {
181                 printf("%p: ", mi);
182
183 /*              disassinstr(&tmpmi->instr); */
184                 printf("%05x", mi->instr);
185
186                 printf("   --> %d, %d, %d:   op1=%p, op2=%p, op3=%p\n", mi->issinknode, mi->latency, mi->priority, mi->opdep[0], mi->opdep[1], mi->opdep[2]);
187
188                 mi = mi->next;
189         }
190         printf("bb end ---\n\n");
191 }
192
193
194 minstruction *schedule_append_minstruction(minstruction *mi)
195 {
196         minstruction *tmpmi;
197
198         /* add new instruction to the list */
199
200         tmpmi = DNEW(minstruction);
201         tmpmi->opdep[0] = NULL;
202         tmpmi->opdep[1] = NULL;
203         tmpmi->opdep[2] = NULL;
204         tmpmi->next = NULL;
205         mi->next = tmpmi;
206
207         return tmpmi;
208 }
209
210
211 /*
212  * These are local overrides for various environment variables in Emacs.
213  * Please do not remove this and leave it at the end of the file, where
214  * Emacs will automagically detect them.
215  * ---------------------------------------------------------------------
216  * Local variables:
217  * mode: c
218  * indent-tabs-mode: t
219  * c-basic-offset: 4
220  * tab-width: 4
221  * End:
222  */