159ea4896b695f79eb2c7434e00f90cec9786fad
[cacao.git] / src / vm / jit / inline / inline.c
1 /* jit/inline.c - code inliner
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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: Dieter Thuernbeck
28
29    $Id: inline.c 846 2004-01-05 10:40:42Z twisti $
30
31 */
32
33
34 #include <stdio.h>
35 #include <string.h>
36 #include "inline.h"
37 #include "main.h"
38 #include "jit.h"
39 #include "parse.h"
40 #include "loader.h"
41 #include "tables.h"
42 #include "toolbox/loging.h"
43 #include "toolbox/memory.h"
44
45
46 // checked functions and macros: LOADCONST code_get OP1 BUILTIN block_insert bound_check ALIGN
47
48 // replace jcodelength loops with correct number after main for loop in parse()!
49
50 static list *inlining_stack;
51 //static list *inlining_patchlist;
52 bool isinlinedmethod;
53 int cumjcodelength;         /* cumulative immediate intruction length      */
54 static int cumlocals;
55 int cummaxstack;
56 int cumextablelength;
57 static int cummethods;
58 inlining_methodinfo *inlining_rootinfo;
59
60
61 void inlining_init(methodinfo *m)
62 {
63         inlining_stack = NULL;
64         //      inlining_patchlist = NULL;
65         isinlinedmethod = 0;
66         cumjcodelength = 0;
67         cumlocals = 0;
68         cumextablelength = 0;
69         cummaxstack = 0;
70         cummethods = 0;
71
72         inlining_stack = NEW(list);
73         list_init(inlining_stack, OFFSET(t_inlining_stacknode, linkage));
74         
75         inlining_rootinfo = inlining_analyse_method(m, 0, 0, 0, 0);
76         maxlocals = cumlocals;
77         maxstack = cummaxstack;
78 }
79
80
81 void inlining_cleanup()
82 {
83         FREE(inlining_stack, t_inlining_stacknode);
84 }
85
86
87 void inlining_push_compiler_variables(int i, int p, int nextp, int opcode, inlining_methodinfo *inlinfo) 
88 {
89         t_inlining_stacknode *new = NEW(t_inlining_stacknode);
90
91         new->i = i;
92         new->p = p;
93         new->nextp = nextp;
94         new->opcode = opcode;
95         new->method = method;
96         //      new->patchlist = inlining_patchlist;
97         new->inlinfo = inlinfo;
98         
99         list_addfirst(inlining_stack, new);
100         isinlinedmethod++;
101 }
102
103
104 void inlining_pop_compiler_variables(int *i, int *p, int *nextp, int *opcode, inlining_methodinfo **inlinfo) 
105 {
106         t_inlining_stacknode *tmp = (t_inlining_stacknode *) list_first(inlining_stack);
107
108         if (!isinlinedmethod) panic("Attempting to pop from inlining stack in toplevel method!\n");
109
110         *i = tmp->i;
111         *p = tmp->p;
112         *nextp = tmp->nextp;
113         *opcode = tmp->opcode;
114         *inlinfo = tmp->inlinfo;
115
116         method = tmp->method;
117         class = method->class;
118         jcodelength = method->jcodelength;
119         jcode = method->jcode;
120         //      inlining_patchlist = tmp->patchlist;
121
122         list_remove(inlining_stack, tmp);
123         FREE(tmp, t_inlining_stacknode);
124         isinlinedmethod--;
125 }
126
127
128 void inlining_set_compiler_variables_fun(methodinfo *m)
129 {
130         method = m;
131         class = m->class;
132         jcodelength = m->jcodelength;
133         jcode = m->jcode;
134         
135         //      inlining_patchlist = DNEW(list);
136         //      list_init(inlining_patchlist, OFFSET(t_patchlistnode, linkage));
137 }
138
139
140 /*void inlining_addpatch(instruction *iptr)  
141   {
142   t_patchlistnode *patch = DNEW(t_patchlistnode);
143   patch->iptr = iptr;
144   list_addlast(inlining_patchlist, patch);
145   }*/
146
147
148 classinfo *first_occurence(classinfo* class, utf* name, utf* desc)
149 {
150         classinfo *first = class;
151         
152         for (; class->super != NULL ; class = class->super) {
153                 if (class_findmethod(class->super, name, desc) != NULL) {
154                         first = class->super;
155                 }                       
156         }
157
158         return first;
159 }
160
161
162 bool is_unique_rec(classinfo *class, methodinfo *m, utf* name, utf* desc)
163 {
164         methodinfo *tmp = class_findmethod(class, name, desc);
165         if ((tmp != NULL) && (tmp != m))
166                 return false;
167
168         for (; class != NULL; class = class->nextsub) {
169                 if ((class->sub != NULL) && !is_unique_rec(class->sub, m, name, desc)) {
170                         return false; 
171                 }
172         }
173         return true;
174 }
175
176
177 bool is_unique_method(classinfo *class, methodinfo *m, utf* name, utf* desc)
178 {
179         classinfo *firstclass;
180         
181         /*      sprintf (logtext, "First occurence of: ");
182         utf_sprint (logtext+strlen(logtext), m->class->name);
183         strcpy (logtext+strlen(logtext), ".");
184         utf_sprint (logtext+strlen(logtext), m->name);
185         utf_sprint (logtext+strlen(logtext), m->descriptor);
186         dolog (); */
187         
188         firstclass = first_occurence(class, name, desc);
189         
190         /*      sprintf (logtext, "\nis in class:");
191         utf_sprint (logtext+strlen(logtext), firstclass->name);
192         dolog (); */
193
194         if (firstclass != class) return false;
195
196         return is_unique_rec(class, m, name, desc);
197 }
198
199
200 inlining_methodinfo *inlining_analyse_method(methodinfo *m, int level, int gp, int firstlocal, int maxstackdepth)
201 {
202         inlining_methodinfo *newnode = DNEW(inlining_methodinfo);
203         u1 *jcode = m->jcode;
204         int jcodelength = m->jcodelength;
205         int p;
206         int nextp;
207         int opcode;
208         int i;
209 /*      int lastlabel = 0; */
210         bool iswide = false, oldiswide;
211         bool *readonly = NULL;
212         int  *label_index = NULL;
213         bool isnotrootlevel = (level > 0);
214         bool isnotleaflevel = (level < INLINING_MAXDEPTH);
215
216         //      if (level == 0) gp = 0;
217         /*
218         sprintf (logtext, "Performing inlining analysis of: ");
219         utf_sprint (logtext+strlen(logtext), m->class->name);
220         strcpy (logtext+strlen(logtext), ".");
221         utf_sprint (logtext+strlen(logtext), m->name);
222         utf_sprint (logtext+strlen(logtext), m->descriptor);
223         dolog (); */
224
225         if (isnotrootlevel) {
226                 newnode->readonly = readonly = DMNEW(bool, m->maxlocals); //FIXME only paramcount entrys necessary
227                 for (i = 0; i < m->maxlocals; readonly[i++] = true);
228                 isnotrootlevel = true;
229
230         } else {
231                 readonly = NULL;
232         }
233         
234         label_index = DMNEW(int, jcodelength);
235
236         newnode->inlinedmethods = DNEW(list);
237         list_init(newnode->inlinedmethods, OFFSET(inlining_methodinfo, linkage));
238
239         newnode->method = m;
240         newnode->startgp = gp;
241         newnode->readonly = readonly;
242         newnode->label_index = label_index;
243         newnode->firstlocal = firstlocal;
244         cumjcodelength += jcodelength + m->paramcount + 1 + 5;
245
246         if ((firstlocal + m->maxlocals) > cumlocals) {
247                 cumlocals = firstlocal + m->maxlocals;
248         }
249
250         if ((maxstackdepth + m->maxstack) > cummaxstack) {
251                 cummaxstack = maxstackdepth + m->maxstack;
252         }
253
254         cumextablelength += m->exceptiontablelength;
255    
256
257         for (p = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
258                 opcode = code_get_u1 (p);
259                 nextp = p + jcommandsize[opcode];
260                 oldiswide = iswide;
261
262                 /* figure out nextp */
263
264                 switch (opcode) {
265                 case JAVA_ILOAD:
266                 case JAVA_LLOAD:
267                 case JAVA_FLOAD:
268                 case JAVA_DLOAD:
269                 case JAVA_ALOAD: 
270
271                 case JAVA_ISTORE:
272                 case JAVA_LSTORE:
273                 case JAVA_FSTORE:
274                 case JAVA_DSTORE:
275                 case JAVA_ASTORE: 
276
277                 case JAVA_RET:
278                         if (iswide) {
279                                 nextp = p + 3;
280                                 iswide = false;
281                         }
282                         break;
283
284                 case JAVA_IINC:
285                         if (iswide) {
286                                 nextp = p + 5;
287                                 iswide = false;
288                         }
289                         break;
290
291                 case JAVA_WIDE:
292                         iswide = true;
293                         nextp = p + 1;
294                         break;
295
296                 case JAVA_LOOKUPSWITCH:
297                         nextp = ALIGN((p + 1), 4) + 4;
298                         nextp += code_get_u4(nextp) * 8 + 4;
299                         break;
300
301                 case JAVA_TABLESWITCH:
302                         nextp = ALIGN((p + 1), 4) + 4;
303                         nextp += (code_get_u4(nextp+4) - code_get_u4(nextp) + 1) * 4 + 4;
304                         break;
305                 }
306
307                 /* detect readonly variables in inlined methods */
308                 
309                 if (isnotrootlevel) { 
310                         bool iswide = oldiswide;
311                         
312                         switch (opcode) {
313                         case JAVA_ISTORE:
314                         case JAVA_LSTORE:
315                         case JAVA_FSTORE:
316                         case JAVA_DSTORE:
317                         case JAVA_ASTORE: 
318                                 if (!iswide) {
319                                         i = code_get_u1(p + 1);
320
321                                 } else {
322                                         i = code_get_u2(p + 1);
323                                 }
324                                 readonly[i] = false;
325                                 break;
326
327                         case JAVA_ISTORE_0:
328                         case JAVA_LSTORE_0:
329                         case JAVA_FSTORE_0:
330                         case JAVA_ASTORE_0:
331                                 readonly[0] = false;
332                                 break;
333
334                         case JAVA_ISTORE_1:
335                         case JAVA_LSTORE_1:
336                         case JAVA_FSTORE_1:
337                         case JAVA_ASTORE_1:
338                                 readonly[1] = false;
339                                 break;
340
341                         case JAVA_ISTORE_2:
342                         case JAVA_LSTORE_2:
343                         case JAVA_FSTORE_2:
344                         case JAVA_ASTORE_2:
345                                 readonly[2] = false;
346                                 break;
347
348                         case JAVA_ISTORE_3:
349                         case JAVA_LSTORE_3:
350                         case JAVA_FSTORE_3:
351                         case JAVA_ASTORE_3:
352                                 readonly[3] = false;
353                                 break;
354
355                         case JAVA_IINC:
356                                 if (!iswide) {
357                                         i = code_get_u1(p + 1);
358
359                                 } else {
360                                         i = code_get_u2(p + 1);
361                                 }
362                                 readonly[i] = false;
363                                 break;
364                         }
365                 }
366
367                 /*              for (i=lastlabel; i<=p; i++) label_index[i] = gp; 
368                 //              printf("lastlabel=%d p=%d gp=%d\n",lastlabel, p, gp);
369                 lastlabel = p+1; */
370                 for (i = p; i < nextp; i++) label_index[i] = gp;
371
372                 if (isnotleaflevel) { 
373
374                         switch (opcode) {
375                         case JAVA_INVOKEVIRTUAL:
376                                 if (!inlinevirtuals)
377                                         break;
378                         case JAVA_INVOKESTATIC:
379                                 i = code_get_u2(p + 1);
380                                 {
381                                         constant_FMIref *imr;
382                                         methodinfo *imi;
383
384                                         imr = class_getconstant(m->class, i, CONSTANT_Methodref);
385                                         imi = class_fetchmethod(imr->class, imr->name, imr->descriptor);
386
387                                         if (opcode == JAVA_INVOKEVIRTUAL) {
388                                                 if (!is_unique_method(imi->class, imi, imr->name, imr->descriptor))
389                                                         break;
390                                         }
391
392                                         if ((cummethods < INLINING_MAXMETHODS) &&
393                                                 (!(imi->flags & ACC_NATIVE)) &&  
394                                                 (!inlineoutsiders || (class == imr->class)) && 
395                                                 (imi->jcodelength < INLINING_MAXCODESIZE) && 
396                                                 (imi->jcodelength > 0) && 
397                                                 (!inlineexceptions || (imi->exceptiontablelength == 0))) { //FIXME: eliminate empty methods?
398                                                 inlining_methodinfo *tmp;
399                                                 descriptor2types(imi);
400
401                                                 cummethods++;
402
403                                                 if (verbose) {
404                                                         char logtext[MAXLOGTEXT];
405                                                         sprintf(logtext, "Going to inline: ");
406                                                         utf_sprint(logtext  +strlen(logtext), imi->class->name);
407                                                         strcpy(logtext + strlen(logtext), ".");
408                                                         utf_sprint(logtext + strlen(logtext), imi->name);
409                                                         utf_sprint(logtext + strlen(logtext), imi->descriptor);
410                                                         log_text(logtext);
411                                                 }
412                                                 
413                                                 tmp = inlining_analyse_method(imi, level + 1, gp, firstlocal + m->maxlocals, maxstackdepth + m->maxstack);
414                                                 list_addlast(newnode->inlinedmethods, tmp);
415                                                 gp = tmp->stopgp;
416                                                 p = nextp;
417                                         }
418                                 }
419                                 break;
420                         }
421                 }  
422         } /* for */
423         
424         newnode->stopgp = gp;
425
426         /*
427         sprintf (logtext, "Result of inlining analysis of: ");
428         utf_sprint (logtext+strlen(logtext), m->class->name);
429         strcpy (logtext+strlen(logtext), ".");
430         utf_sprint (logtext+strlen(logtext), m->name);
431         utf_sprint (logtext+strlen(logtext), m->descriptor);
432         dolog ();
433         sprintf (logtext, "label_index[0..%d]->", jcodelength);
434         for (i=0; i<jcodelength; i++) sprintf (logtext, "%d:%d ", i, label_index[i]);
435         sprintf(logtext,"stopgp : %d\n",newnode->stopgp); */
436
437     return newnode;
438 }
439
440
441 /*
442  * These are local overrides for various environment variables in Emacs.
443  * Please do not remove this and leave it at the end of the file, where
444  * Emacs will automagically detect them.
445  * ---------------------------------------------------------------------
446  * Local variables:
447  * mode: c
448  * indent-tabs-mode: t
449  * c-basic-offset: 4
450  * tab-width: 4
451  * End:
452  */