1 /* jit/inline.c - code inliner
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Dieter Thuernbeck
29 $Id: inline.c 724 2003-12-09 18:56:11Z edwin $
41 #include "toolbox/loging.h"
42 #include "toolbox/memory.h"
45 // checked functions and macros: LOADCONST code_get OP1 BUILTIN block_insert bound_check ALIGN
47 // replace jcodelength loops with correct number after main for loop in parse()!
49 static list *inlining_stack;
50 //static list *inlining_patchlist;
52 int cumjcodelength; /* cumulative immediate intruction length */
56 static int cummethods;
57 inlining_methodinfo *inlining_rootinfo;
60 void inlining_init(methodinfo *m)
62 inlining_stack = NULL;
63 // inlining_patchlist = NULL;
71 inlining_stack = NEW(list);
72 list_init(inlining_stack, OFFSET(t_inlining_stacknode, linkage));
74 inlining_rootinfo = inlining_analyse_method(m, 0, 0, 0, 0);
75 maxlocals = cumlocals;
76 maxstack = cummaxstack;
80 void inlining_cleanup()
82 FREE(inlining_stack, t_inlining_stacknode);
86 void inlining_push_compiler_variables(int i, int p, int nextp, int opcode, inlining_methodinfo *inlinfo)
88 t_inlining_stacknode *new = NEW(t_inlining_stacknode);
95 // new->patchlist = inlining_patchlist;
96 new->inlinfo = inlinfo;
98 list_addfirst(inlining_stack, new);
103 void inlining_pop_compiler_variables(int *i, int *p, int *nextp, int *opcode, inlining_methodinfo **inlinfo)
105 t_inlining_stacknode *tmp = (t_inlining_stacknode *) list_first(inlining_stack);
107 if (!isinlinedmethod) panic("Attempting to pop from inlining stack in toplevel method!\n");
112 *opcode = tmp->opcode;
113 *inlinfo = tmp->inlinfo;
115 method = tmp->method;
116 class = method->class;
117 jcodelength = method->jcodelength;
118 jcode = method->jcode;
119 // inlining_patchlist = tmp->patchlist;
121 list_remove(inlining_stack, tmp);
122 FREE(tmp, t_inlining_stacknode);
127 void inlining_set_compiler_variables_fun(methodinfo *m)
131 jcodelength = m->jcodelength;
134 // inlining_patchlist = DNEW(list);
135 // list_init(inlining_patchlist, OFFSET(t_patchlistnode, linkage));
139 /*void inlining_addpatch(instruction *iptr)
141 t_patchlistnode *patch = DNEW(t_patchlistnode);
143 list_addlast(inlining_patchlist, patch);
147 classinfo *first_occurence(classinfo* class, utf* name, utf* desc)
149 classinfo *first = class;
151 for (; class->super != NULL ; class = class->super) {
152 if (class_findmethod(class->super, name, desc) != NULL) {
153 first = class->super;
161 bool is_unique_rec(classinfo *class, methodinfo *m, utf* name, utf* desc)
163 methodinfo *tmp = class_findmethod(class, name, desc);
164 if ((tmp != NULL) && (tmp != m))
167 for (; class != NULL; class = class->nextsub) {
168 if ((class->sub != NULL) && !is_unique_rec(class->sub, m, name, desc)) {
176 bool is_unique_method(classinfo *class, methodinfo *m, utf* name, utf* desc)
178 classinfo *firstclass;
180 /* sprintf (logtext, "First occurence of: ");
181 utf_sprint (logtext+strlen(logtext), m->class->name);
182 strcpy (logtext+strlen(logtext), ".");
183 utf_sprint (logtext+strlen(logtext), m->name);
184 utf_sprint (logtext+strlen(logtext), m->descriptor);
187 firstclass = first_occurence(class, name, desc);
189 /* sprintf (logtext, "\nis in class:");
190 utf_sprint (logtext+strlen(logtext), firstclass->name);
193 if (firstclass != class) return false;
195 return is_unique_rec(class, m, name, desc);
199 inlining_methodinfo *inlining_analyse_method(methodinfo *m, int level, int gp, int firstlocal, int maxstackdepth)
201 inlining_methodinfo *newnode = DNEW(inlining_methodinfo);
202 u1 *jcode = m->jcode;
203 int jcodelength = m->jcodelength;
208 /* int lastlabel = 0; */
209 bool iswide = false, oldiswide;
210 bool *readonly = NULL;
211 int *label_index = NULL;
212 bool isnotrootlevel = (level > 0);
213 bool isnotleaflevel = (level < INLINING_MAXDEPTH);
215 // if (level == 0) gp = 0;
217 sprintf (logtext, "Performing inlining analysis of: ");
218 utf_sprint (logtext+strlen(logtext), m->class->name);
219 strcpy (logtext+strlen(logtext), ".");
220 utf_sprint (logtext+strlen(logtext), m->name);
221 utf_sprint (logtext+strlen(logtext), m->descriptor);
224 if (isnotrootlevel) {
225 newnode->readonly = readonly = DMNEW(bool, m->maxlocals); //FIXME only paramcount entrys necessary
226 for (i = 0; i < m->maxlocals; readonly[i++] = true);
227 isnotrootlevel = true;
233 label_index = DMNEW(int, jcodelength);
235 newnode->inlinedmethods = DNEW(list);
236 list_init(newnode->inlinedmethods, OFFSET(inlining_methodinfo, linkage));
239 newnode->startgp = gp;
240 newnode->readonly = readonly;
241 newnode->label_index = label_index;
242 newnode->firstlocal = firstlocal;
243 cumjcodelength += jcodelength + m->paramcount + 1 + 5;
245 if ((firstlocal + m->maxlocals) > cumlocals) {
246 cumlocals = firstlocal + m->maxlocals;
249 if ((maxstackdepth + m->maxstack) > cummaxstack) {
250 cummaxstack = maxstackdepth + m->maxstack;
253 cumextablelength += m->exceptiontablelength;
256 for (p = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
257 opcode = code_get_u1 (p);
258 nextp = p + jcommandsize[opcode];
261 /* figure out nextp */
295 case JAVA_LOOKUPSWITCH:
296 nextp = ALIGN((p + 1), 4) + 4;
297 nextp += code_get_u4(nextp) * 8 + 4;
300 case JAVA_TABLESWITCH:
301 nextp = ALIGN((p + 1), 4) + 4;
302 nextp += (code_get_u4(nextp+4) - code_get_u4(nextp) + 1) * 4 + 4;
306 /* detect readonly variables in inlined methods */
308 if (isnotrootlevel) {
309 bool iswide = oldiswide;
318 i = code_get_u1(p + 1);
321 i = code_get_u2(p + 1);
356 i = code_get_u1(p + 1);
359 i = code_get_u2(p + 1);
366 /* for (i=lastlabel; i<=p; i++) label_index[i] = gp;
367 // printf("lastlabel=%d p=%d gp=%d\n",lastlabel, p, gp);
369 for (i = p; i < nextp; i++) label_index[i] = gp;
371 if (isnotleaflevel) {
374 case JAVA_INVOKEVIRTUAL:
377 case JAVA_INVOKESTATIC:
378 i = code_get_u2(p + 1);
380 constant_FMIref *imr;
383 imr = class_getconstant(m->class, i, CONSTANT_Methodref);
384 imi = class_fetchmethod(imr->class, imr->name, imr->descriptor);
386 if (opcode == JAVA_INVOKEVIRTUAL) {
387 if (!is_unique_method(imi->class, imi, imr->name, imr->descriptor))
391 if ((cummethods < INLINING_MAXMETHODS) &&
392 (!(imi->flags & ACC_NATIVE)) &&
393 (!inlineoutsiders || (class == imr->class)) &&
394 (imi->jcodelength < INLINING_MAXCODESIZE) &&
395 (imi->jcodelength > 0) &&
396 (!inlineexceptions || (imi->exceptiontablelength == 0))) { //FIXME: eliminate empty methods?
397 inlining_methodinfo *tmp;
398 descriptor2types(imi);
403 char logtext[MAXLOGTEXT];
404 sprintf(logtext, "Going to inline: ");
405 utf_sprint(logtext +strlen(logtext), imi->class->name);
406 strcpy(logtext + strlen(logtext), ".");
407 utf_sprint(logtext + strlen(logtext), imi->name);
408 utf_sprint(logtext + strlen(logtext), imi->descriptor);
412 tmp = inlining_analyse_method(imi, level + 1, gp, firstlocal + m->maxlocals, maxstackdepth + m->maxstack);
413 list_addlast(newnode->inlinedmethods, tmp);
423 newnode->stopgp = gp;
426 sprintf (logtext, "Result of inlining analysis of: ");
427 utf_sprint (logtext+strlen(logtext), m->class->name);
428 strcpy (logtext+strlen(logtext), ".");
429 utf_sprint (logtext+strlen(logtext), m->name);
430 utf_sprint (logtext+strlen(logtext), m->descriptor);
432 sprintf (logtext, "label_index[0..%d]->", jcodelength);
433 for (i=0; i<jcodelength; i++) sprintf (logtext, "%d:%d ", i, label_index[i]);
434 sprintf(logtext,"stopgp : %d\n",newnode->stopgp); */
441 * These are local overrides for various environment variables in Emacs.
442 * Please do not remove this and leave it at the end of the file, where
443 * Emacs will automagically detect them.
444 * ---------------------------------------------------------------------
447 * indent-tabs-mode: t