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 1205 2004-06-25 06:18:44Z carolyn $
37 #include "jit/inline.h"
39 #include "jit/parse.h"
42 #include "toolbox/logging.h"
43 #include "toolbox/memory.h"
46 // checked functions and macros: LOADCONST code_get OP1 BUILTIN block_insert bound_check ALIGN
48 // replace jcodelength loops with correct number after main for loop in parse()!
50 static list *inlining_stack;
51 //static list *inlining_patchlist;
53 int cumjcodelength; /* cumulative immediate intruction length */
57 static int cummethods;
58 inlining_methodinfo *inlining_rootinfo;
61 void inlining_init(methodinfo *m)
63 inlining_stack = NULL;
64 // inlining_patchlist = NULL;
72 inlining_stack = NEW(list);
73 list_init(inlining_stack, OFFSET(t_inlining_stacknode, linkage));
75 inlining_rootinfo = inlining_analyse_method(m, 0, 0, 0, 0);
76 m->maxlocals = cumlocals;
77 m->maxstack = cummaxstack;
81 void inlining_cleanup()
83 FREE(inlining_stack, t_inlining_stacknode);
87 void inlining_push_compiler_variables(methodinfo *m, int i, int p, int nextp, int opcode, inlining_methodinfo *inlinfo)
89 t_inlining_stacknode *new = NEW(t_inlining_stacknode);
96 // new->patchlist = inlining_patchlist;
97 new->inlinfo = inlinfo;
99 list_addfirst(inlining_stack, new);
104 void inlining_pop_compiler_variables(methodinfo *m, int *i, int *p, int *nextp, int *opcode, inlining_methodinfo **inlinfo)
106 t_inlining_stacknode *tmp = (t_inlining_stacknode *) list_first(inlining_stack);
108 if (!isinlinedmethod) panic("Attempting to pop from inlining stack in toplevel method!\n");
113 *opcode = tmp->opcode;
114 *inlinfo = tmp->inlinfo;
117 /* method = tmp->method; */
118 /* class = method->class; */
119 /* jcodelength = method->jcodelength; */
120 /* jcode = method->jcode; */
121 // inlining_patchlist = tmp->patchlist;
123 list_remove(inlining_stack, tmp);
124 FREE(tmp, t_inlining_stacknode);
129 void inlining_set_compiler_variables_fun(methodinfo *m)
133 /* class = m->class; */
134 /* jcodelength = m->jcodelength; */
135 /* jcode = m->jcode; */
137 // inlining_patchlist = DNEW(list);
138 // list_init(inlining_patchlist, OFFSET(t_patchlistnode, linkage));
142 /*void inlining_addpatch(instruction *iptr)
144 t_patchlistnode *patch = DNEW(t_patchlistnode);
146 list_addlast(inlining_patchlist, patch);
150 classinfo *first_occurence(classinfo* class, utf* name, utf* desc)
152 classinfo *first = class;
154 for (; class->super != NULL ; class = class->super) {
155 if (class_findmethod(class->super, name, desc) != NULL) {
156 first = class->super;
164 bool is_unique_rec(classinfo *class, methodinfo *m, utf* name, utf* desc)
166 methodinfo *tmp = class_findmethod(class, name, desc);
167 if ((tmp != NULL) && (tmp != m))
170 for (; class != NULL; class = class->nextsub) {
171 if ((class->sub != NULL) && !is_unique_rec(class->sub, m, name, desc)) {
179 bool is_unique_method(classinfo *class, methodinfo *m, utf* name, utf* desc)
181 classinfo *firstclass;
183 /* sprintf (logtext, "First occurence of: ");
184 utf_sprint (logtext+strlen(logtext), m->class->name);
185 strcpy (logtext+strlen(logtext), ".");
186 utf_sprint (logtext+strlen(logtext), m->name);
187 utf_sprint (logtext+strlen(logtext), m->descriptor);
190 firstclass = first_occurence(class, name, desc);
192 /* sprintf (logtext, "\nis in class:");
193 utf_sprint (logtext+strlen(logtext), firstclass->name);
196 if (firstclass != class) return false;
198 return is_unique_rec(class, m, name, desc);
202 inlining_methodinfo *inlining_analyse_method(methodinfo *m, int level, int gp, int firstlocal, int maxstackdepth)
204 inlining_methodinfo *newnode = DNEW(inlining_methodinfo);
205 u1 *jcode = m->jcode;
206 int jcodelength = m->jcodelength;
211 /* int lastlabel = 0; */
212 bool iswide = false, oldiswide;
213 bool *readonly = NULL;
214 int *label_index = NULL;
215 bool isnotrootlevel = (level > 0);
216 bool isnotleaflevel = (level < INLINING_MAXDEPTH);
218 // if (level == 0) gp = 0;
220 sprintf (logtext, "Performing inlining analysis of: ");
221 utf_sprint (logtext+strlen(logtext), m->class->name);
222 strcpy (logtext+strlen(logtext), ".");
223 utf_sprint (logtext+strlen(logtext), m->name);
224 utf_sprint (logtext+strlen(logtext), m->descriptor);
227 if (isnotrootlevel) {
228 newnode->readonly = readonly = DMNEW(bool, m->maxlocals); //FIXME only paramcount entrys necessary
229 for (i = 0; i < m->maxlocals; readonly[i++] = true);
230 isnotrootlevel = true;
236 label_index = DMNEW(int, jcodelength);
238 newnode->inlinedmethods = DNEW(list);
239 list_init(newnode->inlinedmethods, OFFSET(inlining_methodinfo, linkage));
242 newnode->startgp = gp;
243 newnode->readonly = readonly;
244 newnode->label_index = label_index;
245 newnode->firstlocal = firstlocal;
246 cumjcodelength += jcodelength + m->paramcount + 1 + 5;
248 if ((firstlocal + m->maxlocals) > cumlocals) {
249 cumlocals = firstlocal + m->maxlocals;
252 if ((maxstackdepth + m->maxstack) > cummaxstack) {
253 cummaxstack = maxstackdepth + m->maxstack;
256 cumextablelength += m->exceptiontablelength;
259 for (p = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
260 opcode = code_get_u1 (p);
261 nextp = p + jcommandsize[opcode];
264 /* figure out nextp */
298 case JAVA_LOOKUPSWITCH:
299 nextp = ALIGN((p + 1), 4) + 4;
300 nextp += code_get_u4(nextp) * 8 + 4;
303 case JAVA_TABLESWITCH:
304 nextp = ALIGN((p + 1), 4) + 4;
305 nextp += (code_get_u4(nextp+4) - code_get_u4(nextp) + 1) * 4 + 4;
309 /* detect readonly variables in inlined methods */
311 if (isnotrootlevel) {
312 bool iswide = oldiswide;
321 i = code_get_u1(p + 1);
324 i = code_get_u2(p + 1);
359 i = code_get_u1(p + 1);
362 i = code_get_u2(p + 1);
369 /* for (i=lastlabel; i<=p; i++) label_index[i] = gp;
370 // printf("lastlabel=%d p=%d gp=%d\n",lastlabel, p, gp);
372 for (i = p; i < nextp; i++) label_index[i] = gp;
374 if (isnotleaflevel) {
377 case JAVA_INVOKEVIRTUAL:
380 case JAVA_INVOKESTATIC:
381 i = code_get_u2(p + 1);
383 constant_FMIref *imr;
386 imr = class_getconstant(m->class, i, CONSTANT_Methodref);
388 if (!class_load(imr->class))
391 if (!class_link(imr->class))
394 imi = class_resolveclassmethod(imr->class,
401 panic("Exception thrown while parsing bytecode"); /* XXX should be passed on */
403 if (opcode == JAVA_INVOKEVIRTUAL) {
404 if (!is_unique_method(imi->class, imi, imr->name, imr->descriptor))
408 if ((cummethods < INLINING_MAXMETHODS) &&
409 (!(imi->flags & ACC_NATIVE)) &&
410 (!inlineoutsiders || (m->class == imr->class)) &&
411 (imi->jcodelength < INLINING_MAXCODESIZE) &&
412 (imi->jcodelength > 0) &&
413 (!inlineexceptions || (imi->exceptiontablelength == 0))) { //FIXME: eliminate empty methods?
414 inlining_methodinfo *tmp;
415 descriptor2types(imi);
420 char logtext[MAXLOGTEXT];
421 sprintf(logtext, "Going to inline: ");
422 utf_sprint(logtext +strlen(logtext), imi->class->name);
423 strcpy(logtext + strlen(logtext), ".");
424 utf_sprint(logtext + strlen(logtext), imi->name);
425 utf_sprint(logtext + strlen(logtext), imi->descriptor);
429 tmp = inlining_analyse_method(imi, level + 1, gp, firstlocal + m->maxlocals, maxstackdepth + m->maxstack);
430 list_addlast(newnode->inlinedmethods, tmp);
440 newnode->stopgp = gp;
443 sprintf (logtext, "Result of inlining analysis of: ");
444 utf_sprint (logtext+strlen(logtext), m->class->name);
445 strcpy (logtext+strlen(logtext), ".");
446 utf_sprint (logtext+strlen(logtext), m->name);
447 utf_sprint (logtext+strlen(logtext), m->descriptor);
449 sprintf (logtext, "label_index[0..%d]->", jcodelength);
450 for (i=0; i<jcodelength; i++) sprintf (logtext, "%d:%d ", i, label_index[i]);
451 sprintf(logtext,"stopgp : %d\n",newnode->stopgp); */
458 * These are local overrides for various environment variables in Emacs.
459 * Please do not remove this and leave it at the end of the file, where
460 * Emacs will automagically detect them.
461 * ---------------------------------------------------------------------
464 * indent-tabs-mode: t