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 846 2004-01-05 10:40:42Z twisti $
42 #include "toolbox/loging.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 maxlocals = cumlocals;
77 maxstack = cummaxstack;
81 void inlining_cleanup()
83 FREE(inlining_stack, t_inlining_stacknode);
87 void inlining_push_compiler_variables(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(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;
116 method = tmp->method;
117 class = method->class;
118 jcodelength = method->jcodelength;
119 jcode = method->jcode;
120 // inlining_patchlist = tmp->patchlist;
122 list_remove(inlining_stack, tmp);
123 FREE(tmp, t_inlining_stacknode);
128 void inlining_set_compiler_variables_fun(methodinfo *m)
132 jcodelength = m->jcodelength;
135 // inlining_patchlist = DNEW(list);
136 // list_init(inlining_patchlist, OFFSET(t_patchlistnode, linkage));
140 /*void inlining_addpatch(instruction *iptr)
142 t_patchlistnode *patch = DNEW(t_patchlistnode);
144 list_addlast(inlining_patchlist, patch);
148 classinfo *first_occurence(classinfo* class, utf* name, utf* desc)
150 classinfo *first = class;
152 for (; class->super != NULL ; class = class->super) {
153 if (class_findmethod(class->super, name, desc) != NULL) {
154 first = class->super;
162 bool is_unique_rec(classinfo *class, methodinfo *m, utf* name, utf* desc)
164 methodinfo *tmp = class_findmethod(class, name, desc);
165 if ((tmp != NULL) && (tmp != m))
168 for (; class != NULL; class = class->nextsub) {
169 if ((class->sub != NULL) && !is_unique_rec(class->sub, m, name, desc)) {
177 bool is_unique_method(classinfo *class, methodinfo *m, utf* name, utf* desc)
179 classinfo *firstclass;
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);
188 firstclass = first_occurence(class, name, desc);
190 /* sprintf (logtext, "\nis in class:");
191 utf_sprint (logtext+strlen(logtext), firstclass->name);
194 if (firstclass != class) return false;
196 return is_unique_rec(class, m, name, desc);
200 inlining_methodinfo *inlining_analyse_method(methodinfo *m, int level, int gp, int firstlocal, int maxstackdepth)
202 inlining_methodinfo *newnode = DNEW(inlining_methodinfo);
203 u1 *jcode = m->jcode;
204 int jcodelength = m->jcodelength;
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);
216 // if (level == 0) gp = 0;
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);
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;
234 label_index = DMNEW(int, jcodelength);
236 newnode->inlinedmethods = DNEW(list);
237 list_init(newnode->inlinedmethods, OFFSET(inlining_methodinfo, linkage));
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;
246 if ((firstlocal + m->maxlocals) > cumlocals) {
247 cumlocals = firstlocal + m->maxlocals;
250 if ((maxstackdepth + m->maxstack) > cummaxstack) {
251 cummaxstack = maxstackdepth + m->maxstack;
254 cumextablelength += m->exceptiontablelength;
257 for (p = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
258 opcode = code_get_u1 (p);
259 nextp = p + jcommandsize[opcode];
262 /* figure out nextp */
296 case JAVA_LOOKUPSWITCH:
297 nextp = ALIGN((p + 1), 4) + 4;
298 nextp += code_get_u4(nextp) * 8 + 4;
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;
307 /* detect readonly variables in inlined methods */
309 if (isnotrootlevel) {
310 bool iswide = oldiswide;
319 i = code_get_u1(p + 1);
322 i = code_get_u2(p + 1);
357 i = code_get_u1(p + 1);
360 i = code_get_u2(p + 1);
367 /* for (i=lastlabel; i<=p; i++) label_index[i] = gp;
368 // printf("lastlabel=%d p=%d gp=%d\n",lastlabel, p, gp);
370 for (i = p; i < nextp; i++) label_index[i] = gp;
372 if (isnotleaflevel) {
375 case JAVA_INVOKEVIRTUAL:
378 case JAVA_INVOKESTATIC:
379 i = code_get_u2(p + 1);
381 constant_FMIref *imr;
384 imr = class_getconstant(m->class, i, CONSTANT_Methodref);
385 imi = class_fetchmethod(imr->class, imr->name, imr->descriptor);
387 if (opcode == JAVA_INVOKEVIRTUAL) {
388 if (!is_unique_method(imi->class, imi, imr->name, imr->descriptor))
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);
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);
413 tmp = inlining_analyse_method(imi, level + 1, gp, firstlocal + m->maxlocals, maxstackdepth + m->maxstack);
414 list_addlast(newnode->inlinedmethods, tmp);
424 newnode->stopgp = gp;
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);
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); */
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 * ---------------------------------------------------------------------
448 * indent-tabs-mode: t