1 /* jit/inline.c - code inliner
3 globals moved to structure and passed as parameter
5 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
6 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
7 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
8 P. Tomsich, J. Wenninger
10 This file is part of CACAO.
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2, or (at
15 your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 Contact: cacao@complang.tuwien.ac.at
29 Authors: Dieter Thuernbeck
31 $Id: inline.c 1415 2004-10-11 20:12:08Z jowenn $
42 #include "jit/inline.h"
44 #include "jit/parse.h"
45 #include "toolbox/logging.h"
46 #include "toolbox/memory.h"
49 utf_display(m->class->name); printf("."); fflush(stdout); \
50 method_display(m); fflush(stdout); \
52 #define IMETHINFO(m) \
53 utf_display(m->class->name); printf("."); fflush(stdout); \
54 method_display(m); fflush(stdout); \
55 printf("\tm->jcodelength=%i; ",m->jcodelength); fflush(stdout); \
56 printf("m->jcode=%p;\n",m->jcode); fflush(stdout); \
57 printf("\tm->maxlocals=%i; ",m->maxlocals); fflush(stdout); \
58 printf("m->maxstack=%i;\n",m->maxstack); fflush(stdout);
61 // checked functions and macros: LOADCONST code_get OP1 BUILTIN block_insert bound_check ALIGN
63 // replace jcodelength loops with correct number after main for loop in parse()!
65 /*-----------------------------------------------------------*/
66 /* just initialize global structure for non-inlining */
67 /*-----------------------------------------------------------*/
69 t_inlining_globals *inlining_init0(methodinfo *m,
70 t_inlining_globals *inline_env)
72 /* initialization for normal use in parse */
73 inlining_set_compiler_variables_fun(m, inline_env);
74 inline_env->isinlinedmethod = 0;
75 inline_env->cumjcodelength = m->jcodelength; /* for not inlining */
76 inline_env->cummaxstack = 0;
77 inline_env->cumextablelength = 0;
78 inline_env->cumlocals = m->maxlocals;
79 inline_env->cummethods = 0;//co not global or static-used only here?
80 inline_env->inlining_stack = NULL;
81 inline_env->inlining_rootinfo = NULL;
84 /*-----------------------------------------------------------*/
86 t_inlining_globals *inlining_init(methodinfo *m)
88 t_inlining_globals *inline_env = DNEW(t_inlining_globals);
89 inlining_init0(m,inline_env);
93 printf("\n-------- Inlining init for: "); fflush(stdout);
96 inline_env->cumjcodelength = 0;
97 inline_env->inlining_stack = NEW(list);
98 list_init(inline_env->inlining_stack,
99 OFFSET(t_inlining_stacknode, linkage));
100 /*------ analyze ------*/
101 if (DEBUGi==true) {print_t_inlining_globals(inline_env);}
102 inline_env->inlining_rootinfo
103 = inlining_analyse_method(m, 0, 0, 0, 0, inline_env);
104 if (DEBUGi==true) {print_t_inlining_globals(inline_env);}
105 /*---------------------*/
106 //if (inline_env->cummethods == 0) {
107 // inline_env = DNEW(t_inlining_globals);
108 // inlining_init0(m,inline_env);
109 // return inline_env;
112 printf("(l,s) (%i,%i) was (%i,%i)\n",
113 m->maxlocals, inline_env->cumlocals,
114 m->maxstack, inline_env->cummaxstack); fflush(stdout);
116 m->maxlocals = inline_env->cumlocals; //orig not used
117 m->maxstack = inline_env->cummaxstack; //orig global maxstack var!!
119 //panic("TEMP so can test just inline init\n");
125 void inlining_cleanup(t_inlining_globals *inline_env)
127 FREE(inline_env->inlining_stack, t_inlining_stacknode);
131 void inlining_push_compiler_variables(int i, int p, int nextp, int opcode, u2 lineindex,u2 currentline,u2 linepcchange,inlining_methodinfo *inlinfo, t_inlining_globals *inline_env)
133 t_inlining_stacknode *new = NEW(t_inlining_stacknode);
138 new->opcode = opcode;
139 new->method = inline_env->method;
140 new->lineindex=lineindex;
141 new->currentline=currentline;
142 new->linepcchange=linepcchange;
143 new->inlinfo = inlinfo;
144 list_addfirst(inline_env->inlining_stack, new);
145 inline_env->isinlinedmethod++;
149 void inlining_pop_compiler_variables(
150 int *i, int *p, int *nextp,
151 int *opcode, u2 *lineindex,
152 u2 *currentline,u2 *linepcchange,
153 inlining_methodinfo **inlinfo,
154 t_inlining_globals *inline_env)
156 t_inlining_stacknode *tmp
157 = (t_inlining_stacknode *) list_first(inline_env->inlining_stack);
159 if (!inline_env->isinlinedmethod) panic("Attempting to pop from inlining stack in toplevel method!\n");
164 *opcode = tmp->opcode;
166 *lineindex=tmp->lineindex;
167 *currentline=tmp->currentline;
168 *currentline=tmp->linepcchange;
170 *inlinfo = tmp->inlinfo;
172 inline_env->method = tmp->method; /*co*/
173 inline_env->class = inline_env->method->class; /*co*/
174 inline_env->jcodelength = inline_env->method->jcodelength; /*co*/
175 inline_env->jcode = inline_env->method->jcode; /*co*/
177 list_remove(inline_env->inlining_stack, tmp);
178 FREE(tmp, t_inlining_stacknode);
179 inline_env->isinlinedmethod--;
183 void inlining_set_compiler_variables_fun(methodinfo *m,
184 t_inlining_globals *inline_env)
187 inline_env->method = m; /*co*/
188 inline_env->class = m->class; /*co*/
189 inline_env->jcode = m->jcode; /*co*/
190 inline_env->jcodelength = m->jcodelength; /*co*/
194 classinfo *first_occurence(classinfo* class, utf* name, utf* desc)
196 classinfo *first = class;
198 for (; class->super != NULL ; class = class->super) {
199 if (class_findmethod(class->super, name, desc) != NULL) {
200 first = class->super;
208 bool is_unique_rec(classinfo *class, methodinfo *m, utf* name, utf* desc)
210 methodinfo *tmp = class_findmethod(class, name, desc);
211 if ((tmp != NULL) && (tmp != m))
214 for (; class != NULL; class = class->nextsub) {
215 if ((class->sub != NULL) && !is_unique_rec(class->sub, m, name, desc)) {
223 bool is_unique_method(classinfo *class, methodinfo *m, utf* name, utf* desc)
225 classinfo *firstclass;
227 /* sprintf (logtext, "First occurence of: ");
228 utf_sprint (logtext+strlen(logtext), m->class->name);
229 strcpy (logtext+strlen(logtext), ".");
230 utf_sprint (logtext+strlen(logtext), m->name);
231 utf_sprint (logtext+strlen(logtext), m->descriptor);
234 firstclass = first_occurence(class, name, desc);
236 /* sprintf (logtext, "\nis in class:");
237 utf_sprint (logtext+strlen(logtext), firstclass->name);
240 if (firstclass != class) return false;
242 return is_unique_rec(class, m, name, desc);
245 inlining_methodinfo *inlining_analyse_method(methodinfo *m,
247 int firstlocal, int maxstackdepth,
248 t_inlining_globals *inline_env)
250 inlining_methodinfo *newnode = DNEW(inlining_methodinfo);
251 /*u1 *jcode = m->jcode;*/
252 int jcodelength = m->jcodelength;
257 bool iswide = false, oldiswide;
258 bool *readonly = NULL;
259 int *label_index = NULL;
260 bool isnotrootlevel = (level > 0);
261 bool isnotleaflevel = (level < INLINING_MAXDEPTH);
263 // if (level == 0) gp = 0;
265 sprintf (logtext, "Performing inlining analysis of: ");
266 utf_sprint (logtext+strlen(logtext), m->class->name);
267 strcpy (logtext+strlen(logtext), ".");
268 utf_sprint (logtext+strlen(logtext), m->name);
269 utf_sprint (logtext+strlen(logtext), m->descriptor);
272 if (isnotrootlevel) {
273 newnode->readonly = readonly = DMNEW(bool, m->maxlocals); //FIXME only paramcount entrys necessary
274 for (i = 0; i < m->maxlocals; readonly[i++] = true);
275 isnotrootlevel = true;
281 label_index = DMNEW(int, jcodelength+200);
283 newnode->inlinedmethods = DNEW(list);
284 list_init(newnode->inlinedmethods, OFFSET(inlining_methodinfo, linkage));
287 newnode->startgp = gp;
288 newnode->readonly = readonly;
289 newnode->label_index = label_index;
290 newnode->firstlocal = firstlocal;
291 inline_env->cumjcodelength += jcodelength + m->paramcount + 1 + 5;
293 if ((firstlocal + m->maxlocals) > inline_env->cumlocals) {
294 inline_env->cumlocals = firstlocal + m->maxlocals;
297 if ((maxstackdepth + m->maxstack) > inline_env->cummaxstack) {
298 inline_env->cummaxstack = maxstackdepth + m->maxstack;
301 inline_env->cumextablelength += m->exceptiontablelength;
304 for (p = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
305 opcode = code_get_u1 (p,m);
306 nextp = p + jcommandsize[opcode];
309 /* figure out nextp */
343 case JAVA_LOOKUPSWITCH:
344 nextp = ALIGN((p + 1), 4) + 4;
345 nextp += code_get_u4(nextp,m) * 8 + 4;
348 case JAVA_TABLESWITCH:
349 nextp = ALIGN((p + 1), 4) + 4;
350 nextp += (code_get_u4(nextp+4,m) - code_get_u4(nextp,m) + 1) * 4 + 4;
354 /* detect readonly variables in inlined methods */
356 if (isnotrootlevel) {
357 bool iswide = oldiswide;
366 i = code_get_u1(p + 1,m);
369 i = code_get_u2(p + 1,m);
404 i = code_get_u1(p + 1,m);
407 i = code_get_u2(p + 1,m);
414 /* for (i=lastlabel; i<=p; i++) label_index[i] = gp;
415 // printf("lastlabel=%d p=%d gp=%d\n",lastlabel, p, gp);
417 for (i = p; i < nextp; i++) label_index[i] = gp;
419 if (isnotleaflevel) {
422 case JAVA_INVOKEVIRTUAL:
425 case JAVA_INVOKESTATIC:
426 i = code_get_u2(p + 1,m);
428 constant_FMIref *imr;
431 imr = class_getconstant(m->class, i, CONSTANT_Methodref);
433 if (!class_load(imr->class))
436 if (!class_link(imr->class))
439 imi = class_resolveclassmethod(imr->class,
446 panic("Exception thrown while parsing bytecode"); /* XXX should be passed on */
448 if (opcode == JAVA_INVOKEVIRTUAL) {
449 if (!is_unique_method(imi->class, imi, imr->name, imr->descriptor))
453 if (imi->flags & ACC_NATIVE) log_text("Native method,no inlining"); //DEBUG
454 if ((inline_env->cummethods < INLINING_MAXMETHODS) &&
455 (!(imi->flags & ACC_NATIVE)) &&
456 (!inlineoutsiders || (m->class == imr->class)) &&
457 (imi->jcodelength < INLINING_MAXCODESIZE) &&
458 (imi->jcodelength > 0) &&
459 (!inlineexceptions || (imi->exceptiontablelength == 0))) { //FIXME: eliminate empty methods?
460 inlining_methodinfo *tmp;
461 descriptor2types(imi);
463 inline_env->cummethods++;
466 char logtext[MAXLOGTEXT];
467 sprintf(logtext, "Going to inline: ");
468 utf_sprint(logtext +strlen(logtext), imi->class->name);
469 strcpy(logtext + strlen(logtext), ".");
470 utf_sprint(logtext + strlen(logtext), imi->name);
471 utf_sprint(logtext + strlen(logtext), imi->descriptor);
475 tmp =inlining_analyse_method(imi, level + 1, gp, firstlocal + m->maxlocals, maxstackdepth + m->maxstack, inline_env);
476 list_addlast(newnode->inlinedmethods, tmp);
486 newnode->stopgp = gp;
489 printf ("\nResult of inlining analysis of: ");
491 printf("was called by:\n"); fflush(stdout);
492 IMETHINFO(inline_env->method);
493 printf ("label_index[0..%d]->", jcodelength);
494 for (i=0;i<jcodelength; i++) printf ("%d:%d ", i, label_index[i]);
495 printf("stopgp : %d\n",newnode->stopgp);
501 /* --------------------------------------------------------------------*/
502 /* print_ functions: check inline structures contain what is expected */
503 /* --------------------------------------------------------------------*/
504 void print_t_inlining_globals (t_inlining_globals *g)
506 printf("\n------------\nt_inlining_globals struct for: \n\t");fflush(stdout);
508 printf("\tclass=");fflush(stdout);
509 utf_display(g->class->name);printf("\n");fflush(stdout);
511 printf("\tjcodelength=%i; jcode=%p;\n",g->jcodelength, g->jcode);
513 if (g->isinlinedmethod==true) {
514 printf("\tisinlinedmethod=true ");fflush(stdout);
517 printf("\tisinlinedmethod=false");fflush(stdout);
520 printf("\tcumjcodelength=%i ,cummaxstack=%i ,cumextablelength=%i ",
521 g->cumjcodelength, g->cummaxstack, g->cumextablelength);fflush(stdout);
522 printf("\tcumlocals=%i ,cummethods=%i \n",
523 g->cumlocals, g->cummethods);fflush(stdout);
525 printf("s>s>s> ");fflush(stdout);
526 print_inlining_stack (g->inlining_stack);
527 printf("i>i>i> "); fflush(stdout);
528 print_inlining_methodinfo(g->inlining_rootinfo);
529 printf("-------------------\n");fflush(stdout);
532 /* --------------------------------------------------------------------*/
533 void print_inlining_stack ( list *s)
536 printf("\n\tinlining_stack: NULL\n");
540 {/* print first element to see if get into stack */
541 t_inlining_stacknode *is;
542 printf("\n\tinlining_stack: NOT NULL\n");
546 printf("\n\tinlining_stack = init'd but EMPTY\n");
553 t_inlining_stacknode *is;
554 printf("\n\tinlining_stack: NOT NULL\n");
557 for (is=list_first(s);
559 is=list_next(s,is)) {
560 printf("\n\ti>--->inlining_stack entry: \n"); fflush(stdout);
561 METHINFO(is->method);
562 printf("i=%i, p=%i, nextp=%i, opcode=%i;\n",
563 is->i,is->p,is->nextp,is->opcode);fflush(stdout);
564 print_inlining_methodinfo(is->inlinfo);
570 /* --------------------------------------------------------------------*/
571 void print_inlining_methodinfo( inlining_methodinfo *r) {
573 printf("\n\tinlining_methodinfo: NULL\n");
577 if (r->method != NULL) {
578 utf_display(r->method->class->name); printf("."); fflush(stdout); \
579 method_display(r->method); fflush(stdout); \
582 printf("method is NULL!!!!!\n");fflush(stdout);
585 printf("\n\tinlining_methodinfo for:"); fflush(stdout);
586 if (r->readonly==NULL) {
587 printf("\treadonly==NULL ");fflush(stdout);
591 printf("\treadonly=");fflush(stdout);
592 for (i = 0; i < r->method->maxlocals; i++) {
593 if (r->readonly[i] == true)
602 printf("\tstartgp=%i; stopgp=%i; firstlocal=%i; label_index=%p;\n",
603 r->startgp, r->stopgp, r->firstlocal, r->label_index);
605 printf ("label_index[0..%d]->", r->method->jcodelength);
606 for (i=0; i<r->method->jcodelength; i++) printf ("%d:%d ", i, r->label_index[i]);
610 inlining_methodinfo *im;
611 for (im=list_first(r->inlinedmethods);
613 im=list_next(r->inlinedmethods,im)) {
620 * These are local overrides for various environment variables in Emacs.
621 * Please do not remove this and leave it at the end of the file, where
622 * Emacs will automagically detect them.
623 * ---------------------------------------------------------------------
626 * indent-tabs-mode: t