1 /* src/vm/jit/inline/inline.c - code inliner
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Edwin Steiner
31 $Id: inline.c 4662 2006-03-21 00:13:45Z edwin $
44 #include "mm/memory.h"
45 #include "toolbox/logging.h"
46 #include "vm/global.h"
47 #include "vm/options.h"
48 #include "vm/statistics.h"
49 #include "vm/jit/jit.h"
50 #include "vm/jit/parse.h"
51 #include "vm/jit/inline/inline.h"
52 #include "vm/jit/loop/loop.h"
55 #include "vm/initialize.h"
56 #include "vm/method.h"
57 #include "vm/jit/jit.h"
59 #include "vm/jit/reg.h"
60 #include "vm/jit/stack.h"
62 #include "vm/jit/verify/typecheck.h"
64 #if defined(USE_THREADS)
65 # if defined(NATIVE_THREADS)
66 # include "threads/native/threads.h"
68 # include "threads/green/threads.h"
73 bool inline_debug_log = 0;
74 bool inline_debug_log_names = 0;
75 int inline_debug_start_counter = 0;
76 int inline_debug_max_size = INT_MAX;
77 int inline_debug_min_size = 0;
78 int inline_debug_end_counter = INT_MAX;
79 int inline_count_methods = 0;
80 #define DOLOG(code) do{ if (inline_debug_log) { code; } }while(0)
85 typedef struct inline_node inline_node;
86 typedef struct inline_target_ref inline_target_ref;
87 typedef struct inline_context inline_context;
88 typedef struct inline_stack_translation inline_stack_translation;
89 typedef struct inline_block_map inline_block_map;
95 inline_node *children;
100 /* info about the call site (if depth > 0)*/
102 basicblock *callerblock;
103 instruction *callerins;
105 stackptr n_callerstack;
106 int n_callerstackdepth;
107 stackptr o_callerstack;
109 /* info about the callee */
111 int prolog_instructioncount;
112 int instructioncount;
115 /* cumulative values */
116 int cumul_instructioncount;
117 int cumul_stackcount;
118 int cumul_basicblockcount;
121 int cumul_exceptiontablelength;
124 instruction *inlined_iinstr;
125 instruction *inlined_iinstr_cursor;
126 stackptr n_inlined_stack;
127 stackptr n_inlined_stack_cursor;
128 basicblock *inlined_basicblocks;
129 basicblock *inlined_basicblocks_cursor;
132 registerdata *regdata;
135 inline_target_ref *refs;
136 instruction *inline_start_instruction;
139 struct inline_target_ref {
140 inline_target_ref *next;
145 struct inline_stack_translation {
150 struct inline_block_map {
156 struct inline_context {
157 int next_block_number;
159 inline_block_map *blockmap;
162 stackptr o_translationlimit; /* if a stackptr is smaller than this, look it up in the table */
163 stackptr n_debug_stackbase;
164 inline_stack_translation *stacktranslationstart;
166 inline_stack_translation stacktranslation[1];
169 #include "inline_debug.c"
171 void inline_print_stats()
173 printf("inlined callers: %d\n",inline_count_methods);
176 static bool inline_jit_compile_intern(methodinfo *m, codegendata *cd, registerdata *rd,
184 /* XXX initialize the static function's class */
186 m->isleafmethod = true;
188 /* call the compiler passes ***********************************************/
190 /* call parse pass */
192 DOLOG( log_message_class("Parsing ", m->class) );
197 /* call stack analysis pass */
199 if (!analyse_stack(m, cd, rd)) {
206 static bool inline_jit_compile(inline_node *iln)
219 #if defined(USE_THREADS)
220 /* enter a monitor on the method */
221 builtin_monitorenter((java_objectheader *) m);
224 /* XXX dont parse these a second time because parse is not idempotent */
225 for (i=0; i<m->jcodelength; ++i) {
226 if (m->jcode[i] == JAVA_TABLESWITCH || m->jcode[i] == JAVA_LOOKUPSWITCH) {
232 /* allocate memory */
233 cd = DNEW(codegendata);
234 rd = DNEW(registerdata);
237 #if defined(ENABLE_JIT)
238 # if defined(ENABLE_INTRP)
241 /* initialize the register allocator */
245 /* setup the codegendata memory */
247 codegen_setup(m, cd);
249 /* now call internal compile function */
251 r = inline_jit_compile_intern(m, cd, rd, ld);
257 /* free some memory */
261 #if defined(ENABLE_JIT)
262 # if defined(ENABLE_INTRP)
271 #if defined(USE_THREADS)
272 /* leave the monitor */
273 builtin_monitorexit((java_objectheader *) m );
279 static void insert_inline_node(inline_node *parent,inline_node *child)
284 assert(parent && child);
286 child->parent = parent;
288 first = parent->children;
290 /* insert as only node */
291 parent->children = child;
297 /* {there is at least one child already there} */
300 while (succ->callerpc < child->callerpc) {
303 /* insert as last node */
304 child->prev = first->prev;
306 child->prev->next = child;
307 child->next->prev = child;
312 assert(succ->callerpc > child->callerpc);
314 /* insert before succ */
316 child->prev = succ->prev;
318 child->prev->next = child;
319 child->next->prev = child;
322 static stackptr relocate_stack_ptr_intern(inline_node *iln,stackptr o_link,ptrint curreloc)
324 inline_stack_translation *tr;
327 /* XXX should limit range in both directions */
328 if (o_link < iln->ctx->o_translationlimit) {
329 /* this stack slot is from an earlier chunk, we must look it up */
330 tr = iln->ctx->stacktranslationstart;
331 while (tr >= iln->ctx->stacktranslation) {
332 if (o_link == tr->o_sp) {
333 DOLOG(printf("\t\t\ttable lookup %p -> %d\n",(void*)o_link,DEBUG_SLOT(tr->n_sp)));
338 DOLOG(debug_dump_inline_context(iln));
339 DOLOG(printf("\t\tFAILED TO TRANSLATE: %p\n",(void*)o_link));
343 /* this stack slot it in the most recent chunk */
345 DOLOG( printf("\t\t\toffset %d\n",curreloc) );
346 return (stackptr) ((u1*)o_link + curreloc);
349 return iln->n_callerstack;
352 /* XXX for debugging */
353 static stackptr relocate_stack_ptr(inline_node *iln,stackptr o_link,ptrint curreloc)
357 new = relocate_stack_ptr_intern(iln,o_link,curreloc);
359 printf("\t\treloc %p -> %d (%p)\t(translimit=%p)\n",
360 (void*)o_link,DEBUG_SLOT(new),(void*)new,(void*)iln->ctx->o_translationlimit)
365 static void emit_instruction(inline_node *iln,instruction *ins,ptrint curreloc,stackptr o_curstack)
370 inline_target_ref *ref;
374 for (i=0; i<iln->depth; ++i)
378 n_ins = (iln->inlined_iinstr_cursor++);
379 assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
383 switch (n_ins[0].opc) {
384 /****************************************/
385 /* VARIABLE ACCESS */
399 n_ins[0].op1 += iln->localsoffset;
432 ref = DNEW(inline_target_ref);
433 ref->ref = (basicblock **) &(n_ins[0].target);
434 ref->next = iln->refs;
438 /****************************************/
443 n_ins[0].opc = ICMD_GOTO;
455 n_ins[0].opc = ICMD_INLINE_GOTO;
456 n_ins[0].dst = o_curstack;
458 n_ins[0].target = (void *) (ptrint) (iln->depth + 0x333); /* XXX */
459 ref = DNEW(inline_target_ref);
460 ref->ref = (basicblock **) &(n_ins[0].target);
461 ref->next = iln->refs;
467 n_ins[0].dst = relocate_stack_ptr(iln,n_ins[0].dst,curreloc);
470 static stackptr emit_inlining_prolog(inline_node *iln,inline_node *callee,stackptr n_curstack,instruction *o_iptr)
480 assert(iln && callee && o_iptr && o_iptr->method == iln->m);
483 md = calleem->parseddesc;
485 localindex = callee->localsoffset + md->paramslots;
486 depth = stack_depth(n_curstack) - 1; /* XXX inefficient */
487 for (i=md->paramcount-1; i>=0; --i) {
490 n_ins = (iln->inlined_iinstr_cursor++);
491 assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
493 type = md->paramtypes[i].type;
495 localindex -= IS_2_WORD_TYPE(type) ? 2 : 1;
496 assert(callee->regdata);
498 DOLOG( printf("prologlocal %d type=%d lofs=%d in ",
499 localindex - callee->localsoffset,
500 callee->regdata->locals[localindex - callee->localsoffset][type].type,callee->localsoffset);
501 method_println(callee->m); );
503 if (callee->regdata->locals[localindex - callee->localsoffset][type].type >= 0) {
504 n_ins->opc = ICMD_ISTORE + type;
505 n_ins->op1 = localindex;
508 n_ins->opc = IS_2_WORD_TYPE(type) ? ICMD_POP2 : ICMD_POP;
510 n_ins->method = iln->m;
511 n_ins->line = o_iptr->line;
513 if (n_curstack->varkind == ARGVAR) {
514 n_curstack->varkind = TEMPVAR;
515 n_curstack->varnum = depth;
516 n_curstack->flags &= ~INMEMORY;
518 n_curstack = n_curstack->prev;
519 n_ins->dst = n_curstack;
523 n_ins = (iln->inlined_iinstr_cursor++);
524 assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
526 n_ins->opc = ICMD_INLINE_START;
527 n_ins->method = callee->m;
528 n_ins->dst = n_curstack;
529 n_ins->line = o_iptr->line;
530 n_ins->target = NULL; /* ease debugging */
531 iln->inline_start_instruction = n_ins;
536 static void emit_inlining_epilog(inline_node *iln,inline_node *callee,stackptr n_curstack,instruction *o_iptr)
540 assert(iln && callee && o_iptr);
541 assert(iln->inline_start_instruction);
543 n_ins = (iln->inlined_iinstr_cursor++);
544 assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
546 n_ins->opc = ICMD_INLINE_END;
547 n_ins->method = callee->m;
548 n_ins->dst = n_curstack;
549 n_ins->line = o_iptr->line;
550 n_ins->target = iln->inline_start_instruction; /* needed for line number table creation */
553 static void rewrite_stack(inline_node *iln,stackptr o_first,stackptr o_last,ptrint curreloc)
563 DOLOG(printf("rewrite_stack: no stack slots\n"));
569 assert(o_first <= o_last);
571 n = o_last - o_first + 1;
575 n_sp = iln->n_inlined_stack_cursor;
578 printf("rewrite_stack: rewriting %d stack slots (%p,%p) -> (%d,%d)\n",
579 n,(void*)o_first,(void*)o_last,DEBUG_SLOT(n_sp),
580 DEBUG_SLOT(n_sp+n-1))
583 DOLOG( printf("o_first = "); debug_dump_stack(o_first); printf("\n") );
584 DOLOG( printf("o_last = "); debug_dump_stack(o_last); printf("\n") );
586 while (o_sp <= o_last) {
589 n_sp->prev = relocate_stack_ptr(iln,n_sp->prev,curreloc);
590 switch (n_sp->varkind) {
591 case STACKVAR: n_sp->varnum += iln->n_callerstackdepth; break;
592 case LOCALVAR: n_sp->varnum += iln->localsoffset; break;
598 DOLOG( printf("n_sp = "); debug_dump_stack(n_sp-1); printf("\n") );
600 iln->n_inlined_stack_cursor = n_sp;
603 static void inline_resolve_block_refs(inline_target_ref **refs,basicblock *o_bptr,basicblock *n_bptr)
605 inline_target_ref *ref;
606 inline_target_ref *prev;
611 if (*(ref->ref) == o_bptr) {
613 if ((ptrint) o_bptr < (0x333+100)) { /* XXX */
614 printf("resolving RETURN block reference %p -> new L%03d (%p)\n",
615 (void*)o_bptr,n_bptr->debug_nr,(void*)n_bptr);
618 printf("resolving block reference old L%03d (%p) -> new L%03d (%p)\n",
619 o_bptr->debug_nr,(void*)o_bptr,n_bptr->debug_nr,(void*)n_bptr);
623 *(ref->ref) = n_bptr;
625 prev->next = ref->next;
638 static basicblock * create_block(inline_node *iln,basicblock *o_bptr,inline_target_ref **refs,int indepth)
644 assert(iln && o_bptr && refs);
646 n_bptr = iln->inlined_basicblocks_cursor++;
649 memset(n_bptr,0,sizeof(basicblock));
652 n_bptr->type = BBTYPE_STD; /* XXX not necessary */
653 n_bptr->iinstr = iln->inlined_iinstr_cursor;
654 n_bptr->next = n_bptr+1;
655 n_bptr->debug_nr = iln->ctx->next_block_number++;
656 n_bptr->indepth = indepth;
659 /* allocate stackslots */
660 iln->n_inlined_stack_cursor += indepth;
661 n_sp = iln->n_inlined_stack_cursor - 1;
662 n_bptr->instack = n_sp;
664 /* link the stack elements */
665 for (i=indepth-1; i>=0; --i) {
666 n_sp->varkind = STACKVAR;
668 n_sp->prev = (i) ? n_sp-1 : NULL;
669 n_sp->flags = 0; /* XXX */
674 inline_resolve_block_refs(refs,o_bptr,n_bptr);
679 static void fill_translation_table(inline_node *iln,stackptr o_sp,stackptr n_sp,int n_depth)
684 printf("fill_translation_table (newdepth=%d):\n",n_depth);
685 printf("\tos_sp = "); debug_dump_stack(o_sp); printf("\n");
686 printf("\tns_sp = "); debug_dump_stack(n_sp); printf("\n");
689 /* we must translate all stack slots that were present before the call XXX */
690 /* and the instack of the block */
691 iln->ctx->stacktranslationstart = iln->ctx->stacktranslation + (n_depth - 1);
693 /* fill the translation table */
700 iln->ctx->stacktranslation[i].o_sp = o_sp;
701 iln->ctx->stacktranslation[i].n_sp = n_sp;
702 n_sp->flags |= (o_sp->flags & SAVEDVAR); /* XXX correct? */
703 n_sp->type = o_sp->type; /* XXX we overwrite this anyway with STACKVAR, right? */
711 assert(iln->ctx->stacktranslation[i].o_sp);
712 iln->ctx->stacktranslation[i].n_sp = n_sp;
713 n_sp->flags |= SAVEDVAR; /* XXX this is too conservative */
722 static void rewrite_method(inline_node *iln)
732 stackptr o_nexttorewrite;
733 stackptr o_lasttorewrite;
734 inline_node *nextcall;
737 inline_block_map *bm;
743 nextcall = iln->children;
745 /* set memory cursors */
746 iln->inlined_iinstr_cursor = iln->inlined_iinstr;
747 iln->n_inlined_stack_cursor = iln->n_inlined_stack;
748 iln->inlined_basicblocks_cursor = iln->inlined_basicblocks;
750 /* loop over basic blocks */
751 o_bptr = iln->m->basicblocks;
752 for (; o_bptr; o_bptr = o_bptr->next) {
754 if (o_bptr->flags < BBREACHED) {
756 printf("skipping old L%03d (flags=%d,type=%d,os=%p,oid=%d,ois=%p,cursor=%d,curreloc=%d,callerstack=%d) of ",
757 o_bptr->debug_nr,o_bptr->flags,o_bptr->type,
758 (void*)o_bptr->stack,o_bptr->indepth,(void*)o_bptr->instack,
759 DEBUG_SLOT(iln->n_inlined_stack_cursor),curreloc,
760 DEBUG_SLOT(iln->n_callerstack));
761 method_println(iln->m);
764 n_bptr = create_block(iln,o_bptr,&(iln->refs),o_bptr->indepth + iln->n_callerstackdepth);
765 n_bptr->type = o_bptr->type;
766 /* enter it in the blockmap */
767 iln->ctx->blockmap[iln->ctx->blockmap_index].iln = iln;
768 iln->ctx->blockmap[iln->ctx->blockmap_index].o_block = o_bptr;
769 iln->ctx->blockmap[iln->ctx->blockmap_index++].n_block = n_bptr;
770 n_bptr->flags = o_bptr->flags;
774 assert(o_bptr->stack);
776 len = o_bptr->icount;
777 o_iptr = o_bptr->iinstr;
780 printf("rewriting old L%03d (flags=%d,type=%d,os=%p,oid=%d,ois=%p,cursor=%d,curreloc=%d,callerstack=%d) of ",
781 o_bptr->debug_nr,o_bptr->flags,o_bptr->type,
782 (void*)o_bptr->stack,o_bptr->indepth,(void*)o_bptr->instack,
783 DEBUG_SLOT(iln->n_inlined_stack_cursor),curreloc,
784 DEBUG_SLOT(iln->n_callerstack));
785 method_println(iln->m);
786 printf("o_instack: ");debug_dump_stack(o_bptr->instack);printf("\n");
787 printf("o_callerstack: ");debug_dump_stack(iln->o_callerstack);printf("\n");
790 o_curstack = o_bptr->instack;
792 /* create an inlined clone of this block */
793 n_bptr = create_block(iln,o_bptr,&(iln->refs),o_bptr->indepth + iln->n_callerstackdepth);
794 n_bptr->type = o_bptr->type;
795 n_bptr->flags = o_bptr->flags;
797 /* enter it in the blockmap */
798 iln->ctx->blockmap[iln->ctx->blockmap_index].iln = iln;
799 iln->ctx->blockmap[iln->ctx->blockmap_index].o_block = o_bptr;
800 iln->ctx->blockmap[iln->ctx->blockmap_index++].n_block = n_bptr;
802 DOLOG( debug_dump_inline_context(iln) );
804 if (iln->n_callerstackdepth)
805 iln->n_callerstack = n_bptr->instack-o_bptr->indepth;
807 iln->n_callerstack = NULL;
808 fill_translation_table(iln,iln->o_callerstack,iln->n_callerstack,iln->n_callerstackdepth);
809 fill_translation_table(iln,o_bptr->instack,n_bptr->instack,n_bptr->indepth);
810 iln->ctx->o_translationlimit = o_bptr->stack;
812 DOLOG( debug_dump_inline_context(iln) );
814 /* calculate the stack element relocation */
815 curreloc = (u1*)iln->n_inlined_stack_cursor - (u1*)o_bptr->stack;
816 DOLOG( printf("curreloc <- %d = %p - %p\n",curreloc,(void*)iln->n_inlined_stack_cursor,(void*)(u1*)o_bptr->stack) );
818 o_nexttorewrite = o_bptr->stack;
819 o_lasttorewrite = o_bptr->stack-1;
820 assert(o_nexttorewrite);
827 DOLOG( printf("o_curstack = "); debug_dump_stack(o_curstack); show_icmd(o_iptr,false); printf(", dst = "); debug_dump_stack(o_dst); printf("\n") );
829 if (nextcall && o_iptr == nextcall->callerins) {
831 /* rewrite stack elements produced so far in this block */
832 if (o_nexttorewrite <= o_lasttorewrite) {
833 rewrite_stack(iln, o_nexttorewrite, o_lasttorewrite, curreloc);
836 /* write the inlining prolog */
837 n_sp = emit_inlining_prolog(iln,nextcall,relocate_stack_ptr(iln,o_curstack,curreloc),o_iptr);
838 icount += nextcall->m->parseddesc->paramcount + 1; /* XXX prolog instructions */
840 /* find the first stack slot under the arguments of the invocation */
842 for (i=0; i < nextcall->m->parseddesc->paramcount; ++i) {
846 nextcall->o_callerstack = o_sp;
848 /* see how deep the new stack is after the arguments have been removed */
849 i = stack_depth(n_sp);
850 assert(i == stack_depth(nextcall->o_callerstack) + iln->n_callerstackdepth);
852 /* end current block */
853 n_bptr->icount = icount;
854 n_bptr->outstack = n_sp;
855 n_bptr->outdepth = i;
857 /* caller stack depth for the callee */
858 assert(nextcall->n_callerstackdepth == i);
860 /* set memory pointers in the callee */
861 nextcall->inlined_iinstr = iln->inlined_iinstr_cursor;
862 nextcall->n_inlined_stack = iln->n_inlined_stack_cursor;
863 nextcall->inlined_basicblocks = iln->inlined_basicblocks_cursor;
866 DOLOG( printf("entering inline "); show_icmd(o_iptr,false); printf("\n") );
867 rewrite_method(nextcall);
868 DOLOG( printf("leaving inline "); show_icmd(o_iptr,false); printf("\n") );
870 /* skip stack slots used by the inlined callee */
871 curreloc += (u1*)nextcall->n_inlined_stack_cursor - (u1*)iln->n_inlined_stack_cursor;
873 /* update memory cursors */
874 assert(nextcall->inlined_iinstr_cursor == iln->inlined_iinstr_cursor + nextcall->cumul_instructioncount);
875 /*assert(nextcall->n_inlined_stack_cursor == iln->n_inlined_stack_cursor + nextcall->cumul_stackcount);*/
876 assert(nextcall->inlined_basicblocks_cursor == iln->inlined_basicblocks_cursor + nextcall->cumul_basicblockcount);
877 iln->inlined_iinstr_cursor = nextcall->inlined_iinstr_cursor;
878 iln->n_inlined_stack_cursor = nextcall->n_inlined_stack_cursor;
879 iln->inlined_basicblocks_cursor = nextcall->inlined_basicblocks_cursor;
881 /* start new block */
882 i = (nextcall->m->parseddesc->returntype.type == TYPE_VOID) ? 0 : 1; /* number of return slots */
883 assert(i == 0 || i == 1);
884 n_bptr = create_block(iln,(void*) (ptrint) (nextcall->depth + 0x333) /*XXX*/,
885 &(nextcall->refs),nextcall->n_callerstackdepth + i);
886 n_bptr->flags = o_bptr->flags;
889 /* skip allocated stack slots */
890 curreloc += sizeof(stackelement) * (n_bptr->indepth - i);
892 /* fill the translation table for the slots present before the call */
893 n_sp = n_bptr->instack;
894 fill_translation_table(iln,nextcall->o_callerstack,(i) ? n_sp->prev : n_sp,nextcall->n_callerstackdepth);
896 /* the return slot */
900 fill_translation_table(iln,o_dst,n_sp,nextcall->n_callerstackdepth + 1);
902 o_nexttorewrite = o_dst + 1;
903 o_lasttorewrite = o_dst;
906 /* the next chunk of stack slots start with (including) the slots produced */
907 /* by the invocation */
908 o_nexttorewrite = o_lasttorewrite + 1;
909 o_lasttorewrite = o_nexttorewrite - 1;
912 DOLOG( debug_dump_inline_context(iln) );
913 iln->ctx->o_translationlimit = o_nexttorewrite;
915 /* emit inlining epilog */
916 emit_inlining_epilog(iln,nextcall,n_sp,o_iptr);
917 icount++; /* XXX epilog instructions */
919 /* proceed to next call */
920 nextcall = nextcall->next;
923 printf("resuming old L%03d (flags=%d,type=%d,os=%p,oid=%d,ois=%p,cursor=%d,curreloc=%d,callerstack=%d) of ",
924 o_bptr->debug_nr,o_bptr->flags,o_bptr->type,
925 (void*)o_bptr->stack,o_bptr->indepth,(void*)o_bptr->instack,
926 DEBUG_SLOT(iln->n_inlined_stack_cursor),curreloc,
927 DEBUG_SLOT(iln->n_callerstack));
928 method_println(iln->m);
932 emit_instruction(iln,o_iptr,curreloc,o_curstack);
935 if (o_dst > o_lasttorewrite)
936 o_lasttorewrite = o_dst;
939 DOLOG( printf("o_dst = %p\n",(void*)o_dst) );
944 /* end of basic block */
945 /* rewrite stack after last call */
946 if (o_nexttorewrite <= o_lasttorewrite) {
947 rewrite_stack(iln,o_nexttorewrite,o_lasttorewrite,curreloc);
949 n_bptr->outstack = relocate_stack_ptr(iln,o_bptr->outstack,curreloc);
950 n_bptr->outdepth = iln->n_callerstackdepth + o_bptr->outdepth;
951 assert(n_bptr->outdepth == stack_depth(n_bptr->outstack));
953 if (n_bptr->outstack) {
955 n_bptr->outstack += curreloc;
958 n_bptr->icount = icount;
960 n_iptr = iln->inlined_iinstr_cursor - 1;
961 if (n_iptr->opc == ICMD_INLINE_GOTO) {
962 DOLOG( printf("creating stack slot for ICMD_INLINE_GOTO\n") );
963 n_sp = iln->n_inlined_stack_cursor++;
965 *n_sp = *n_iptr->dst;
966 n_sp->prev = iln->n_callerstack;
969 n_bptr->outdepth = iln->n_callerstackdepth + 1;
970 n_bptr->outstack = n_sp;
974 /* end of basic blocks */
979 bm = iln->ctx->blockmap;
980 for (i=0; i<iln->ctx->blockmap_index; ++i, ++bm) {
981 assert(bm->iln && bm->o_block && bm->n_block);
984 inline_resolve_block_refs(&(iln->refs),iln->ctx->blockmap[i].o_block,iln->ctx->blockmap[i].n_block);
989 inline_target_ref *ref;
992 if (!iln->depth || *(ref->ref) != (void*) (ptrint) (0x333 + iln->depth) /* XXX */) {
993 DOLOG( printf("XXX REMAINING REF at depth %d: %p\n",iln->depth,(void*)*(ref->ref)) );
1002 static basicblock * inline_map_block(inline_node *iln,basicblock *o_block,inline_node *targetiln)
1004 inline_block_map *bm;
1005 inline_block_map *bmend;
1013 bm = iln->ctx->blockmap;
1014 bmend = bm + iln->ctx->blockmap_index;
1016 while (bm < bmend) {
1017 assert(bm->iln && bm->o_block && bm->n_block);
1018 if (bm->o_block == o_block && bm->iln == targetiln)
1024 return NULL; /* not reached */
1027 static exceptiontable * inline_exception_tables(inline_node *iln,exceptiontable *n_extable,exceptiontable **prevextable)
1035 assert(prevextable);
1037 child = iln->children;
1040 n_extable = inline_exception_tables(child,n_extable,prevextable);
1041 child = child->next;
1042 } while (child != iln->children);
1045 et = iln->m->exceptiontable;
1046 for (i=0; i<iln->m->exceptiontablelength; ++i) {
1048 memset(n_extable,0,sizeof(exceptiontable));
1049 n_extable->startpc = et->startpc;
1050 n_extable->endpc = et->endpc;
1051 n_extable->handlerpc = et->handlerpc;
1052 n_extable->start = inline_map_block(iln,et->start,iln);
1053 n_extable->end = inline_map_block(iln,et->end,iln);
1054 n_extable->handler = inline_map_block(iln,et->handler,iln);
1055 n_extable->catchtype = et->catchtype;
1058 (*prevextable)->down = n_extable;
1060 *prevextable = n_extable;
1069 static void inline_locals(inline_node *iln,registerdata *rd)
1078 child = iln->children;
1081 inline_locals(child,rd);
1082 child = child->next;
1083 } while (child != iln->children);
1086 assert(iln->regdata);
1088 for (i=0; i<iln->m->maxlocals; ++i) {
1089 for (t=TYPE_INT; t<=TYPE_ADR; ++t) {
1090 DOLOG( printf("local %d type=%d in ",i,iln->regdata->locals[i][t].type); method_println(iln->m); );
1091 if (iln->regdata->locals[i][t].type >= 0) {
1092 rd->locals[iln->localsoffset + i][t].type = iln->regdata->locals[i][t].type;
1093 rd->locals[iln->localsoffset + i][t].flags |= iln->regdata->locals[i][t].flags;
1098 if (iln->regdata->memuse > rd->memuse)
1099 rd->memuse = iln->regdata->memuse;
1100 if (iln->regdata->argintreguse > rd->argintreguse)
1101 rd->argintreguse = iln->regdata->argintreguse;
1102 if (iln->regdata->argfltreguse > rd->argfltreguse)
1103 rd->argfltreguse = iln->regdata->argfltreguse;
1106 static void inline_stack_interfaces(inline_node *iln,registerdata *rd)
1115 assert(rd->interfaces);
1117 bptr = iln->inlined_basicblocks;
1118 for (i=0; i<iln->cumul_basicblockcount; ++i, ++bptr) {
1119 DOLOG( printf("INLINE STACK INTERFACE block L%03d\n",bptr->debug_nr) );
1120 DOLOG( printf("\toutstack = ");debug_dump_stack(bptr->outstack);printf("\n") );
1121 DOLOG( printf("\tinstack = ");debug_dump_stack(bptr->outstack);printf("\n") );
1123 assert(bptr->outdepth == stack_depth(bptr->outstack));
1124 assert(bptr->indepth == stack_depth(bptr->instack));
1126 sp = bptr->outstack;
1127 d = bptr->outdepth - 1;
1129 if ((sp->varkind == STACKVAR) && (sp->varnum > d)) {
1130 sp->varkind = TEMPVAR;
1133 sp->varkind = STACKVAR;
1136 DOLOG( printf("INLINE STACK INTERFACE L%03d outstack d=%d varkind=%d varnum=%d type=%d flags=%01x\n",
1137 bptr->debug_nr,d,sp->varkind,sp->varnum,sp->type,sp->flags) );
1138 rd->interfaces[d][sp->type].type = sp->type;
1139 rd->interfaces[d][sp->type].flags |= sp->flags;
1145 d = bptr->indepth - 1;
1147 rd->interfaces[d][sp->type].type = sp->type;
1148 if (sp->varkind == STACKVAR && (sp->flags & SAVEDVAR)) {
1149 rd->interfaces[d][sp->type].flags |= SAVEDVAR;
1151 DOLOG( printf("INLINE STACK INTERFACE L%03d instack d=%d varkind=%d varnum=%d type=%d flags=%01x\n",
1152 bptr->debug_nr,d,sp->varkind,sp->varnum,sp->type,sp->flags) );
1159 static bool inline_inline_intern(methodinfo *m, codegendata *cd, registerdata *rd, inline_node *iln)
1165 stackptr o_curstack;
1166 int opcode; /* invocation opcode */
1168 inline_node *calleenode;
1169 inline_node *active;
1176 iln->cumul_maxstack = iln->n_callerstackdepth + m->maxstack + 1 /* XXX builtins */;
1177 iln->cumul_maxlocals = iln->localsoffset + m->maxlocals;
1178 iln->cumul_exceptiontablelength += m->exceptiontablelength;
1180 bptr = m->basicblocks;
1181 for (; bptr; bptr = bptr->next) {
1183 iln->cumul_basicblockcount++;
1185 if (bptr->flags < BBREACHED)
1188 assert(bptr->stack);
1191 iptr = bptr->iinstr;
1192 o_curstack = bptr->instack;
1194 iln->instructioncount += len;
1195 iln->cumul_instructioncount += len;
1198 printf("ADD INSTRUCTIONS [%d]: %d, count=%d, cumulcount=%d\n",
1199 iln->depth,len,iln->instructioncount,iln->cumul_instructioncount);
1202 while (--len >= 0) {
1209 /* XXX we cannot deal with IINC's stack hacking */
1212 case ICMD_LOOKUPSWITCH:
1213 case ICMD_TABLESWITCH:
1214 /* XXX these are not implemented, yet. */
1217 /****************************************/
1220 case ICMD_INVOKEVIRTUAL:
1221 case ICMD_INVOKESPECIAL:
1222 case ICMD_INVOKESTATIC:
1223 case ICMD_INVOKEINTERFACE:
1224 callee = (methodinfo *) iptr[0].val.a;
1227 if ((callee->flags & (ACC_STATIC | ACC_FINAL | ACC_PRIVATE) || opcode == ICMD_INVOKESPECIAL)
1228 && !(callee->flags & (ACC_NATIVE | ACC_SYNCHRONIZED)))
1230 if (iln->depth < 3) {
1231 for (active = iln; active; active = active->parent) {
1232 if (callee == active->m) {
1233 DOLOG( printf("RECURSIVE!\n") );
1238 calleenode = DNEW(inline_node);
1239 memset(calleenode,0,sizeof(inline_node));
1241 calleenode->ctx = iln->ctx;
1242 calleenode->m = callee;
1244 if (!inline_jit_compile(calleenode))
1247 calleenode->depth = iln->depth+1;
1248 calleenode->callerblock = bptr;
1249 calleenode->callerins = iptr;
1250 calleenode->callerpc = iptr - m->basicblocks->iinstr;
1252 calleenode->localsoffset = iln->localsoffset + m->maxlocals;
1253 calleenode->prolog_instructioncount = callee->parseddesc->paramcount;
1255 calleenode->stackcount = callee->stackcount;
1256 calleenode->cumul_stackcount = callee->stackcount;
1258 /* see how deep the stack is below the arguments */
1260 for (i=0; sp; sp = sp->prev)
1262 calleenode->n_callerstackdepth = iln->n_callerstackdepth + i - callee->parseddesc->paramcount;
1264 insert_inline_node(iln,calleenode);
1266 if (!inline_inline_intern(callee,cd,rd,calleenode))
1269 iln->cumul_instructioncount += calleenode->prolog_instructioncount;
1270 iln->cumul_instructioncount += calleenode->cumul_instructioncount - 1/*invoke*/ + 2 /*INLINE_START|END*/;
1271 iln->cumul_stackcount += calleenode->cumul_stackcount;
1272 iln->cumul_basicblockcount += calleenode->cumul_basicblockcount + 1/*XXX*/;
1273 iln->cumul_exceptiontablelength += calleenode->cumul_exceptiontablelength;
1274 if (calleenode->cumul_maxstack > iln->cumul_maxstack)
1275 iln->cumul_maxstack = calleenode->cumul_maxstack;
1276 if (calleenode->cumul_maxlocals > iln->cumul_maxlocals)
1277 iln->cumul_maxlocals = calleenode->cumul_maxlocals;
1290 /* end of basic block */
1296 static bool test_inlining(inline_node *iln,codegendata *cd,registerdata *rd,
1297 methodinfo **resultmethod, codegendata **resultcd, registerdata **resultrd)
1302 methodinfo *n_method;
1303 exceptiontable *n_ext;
1304 exceptiontable *prevext;
1308 static int debug_verify_inlined_code = 1;
1309 static int debug_compile_inlined_code_counter = 0;
1311 assert(iln && cd && rd && resultmethod && resultcd && resultrd);
1313 *resultmethod = iln->m;
1318 if (debug_compile_inlined_code_counter >5)
1322 n_ins = DMNEW(instruction,iln->cumul_instructioncount);
1323 iln->inlined_iinstr = n_ins;
1325 n_stack = DMNEW(stackelement,iln->cumul_stackcount + 1000 /* XXX */);
1326 iln->n_inlined_stack = n_stack;
1327 iln->ctx->n_debug_stackbase = n_stack;
1329 n_bb = DMNEW(basicblock,iln->cumul_basicblockcount);
1330 iln->inlined_basicblocks = n_bb;
1332 iln->ctx->blockmap = DMNEW(inline_block_map,iln->cumul_basicblockcount);
1334 rewrite_method(iln);
1336 if (iln->cumul_exceptiontablelength) {
1337 n_ext = DMNEW(exceptiontable,iln->cumul_exceptiontablelength);
1339 inline_exception_tables(iln,n_ext,&prevext);
1341 prevext->down = NULL;
1347 /*******************************************************************************/
1349 n_method = NEW(methodinfo);
1350 memcpy(n_method,iln->m,sizeof(methodinfo));
1351 n_method->maxstack = iln->cumul_maxstack; /* XXX put into cd,rd */
1352 n_method->maxlocals = iln->cumul_maxlocals;
1353 n_method->basicblockcount = iln->cumul_basicblockcount;
1354 n_method->basicblocks = iln->inlined_basicblocks;
1355 n_method->basicblockindex = NULL;
1356 n_method->instructioncount = iln->cumul_instructioncount;
1357 n_method->instructions = iln->inlined_iinstr;
1358 n_method->stackcount = iln->cumul_stackcount + 1000 /* XXX */;
1359 n_method->stack = iln->n_inlined_stack;
1361 n_method->exceptiontablelength = iln->cumul_exceptiontablelength;
1362 n_method->exceptiontable = n_ext;
1363 n_method->linenumbercount = 0;
1365 n_cd = DNEW(codegendata);
1366 memcpy(n_cd,cd,sizeof(codegendata));
1367 n_cd->method = n_method;
1368 n_cd->maxstack = n_method->maxstack;
1369 n_cd->maxlocals = n_method->maxlocals;
1370 n_cd->exceptiontablelength = n_method->exceptiontablelength;
1371 n_cd->exceptiontable = n_method->exceptiontable;
1373 n_rd = DNEW(registerdata);
1374 reg_setup(n_method, n_rd);
1377 inline_locals(iln,n_rd);
1378 DOLOG( printf("INLINING STACK INTERFACES FOR "); method_println(iln->m) );
1379 inline_stack_interfaces(iln,n_rd);
1381 if (debug_verify_inlined_code) {
1382 debug_verify_inlined_code = 0;
1383 DOLOG( printf("VERIFYING INLINED RESULT...\n") );
1384 if (!typecheck(n_method,n_cd,n_rd)) {
1385 *exceptionptr = NULL;
1386 DOLOG( printf("XXX INLINED RESULT DID NOT PASS VERIFIER XXX\n") );
1390 DOLOG( printf("VERIFICATION PASSED.\n") );
1392 debug_verify_inlined_code = 1;
1396 if (n_method->instructioncount >= inline_debug_min_size && n_method->instructioncount <= inline_debug_max_size) {
1397 if (debug_compile_inlined_code_counter >= inline_debug_start_counter
1398 && debug_compile_inlined_code_counter <= inline_debug_end_counter)
1401 (strcmp(n_method->class->name->text,"java/lang/reflect/Array") == 0 &&
1402 strcmp(n_method->name->text,"<clinit>") == 0 &&
1403 strcmp(n_method->descriptor->text,"()V") == 0)
1405 (strcmp(n_method->class->name->text,"java/lang/VMClassLoader") == 0 &&
1406 strcmp(n_method->name->text,"getSystemClassLoader") == 0 &&
1407 strcmp(n_method->descriptor->text,"()Ljava/lang/ClassLoader;") == 0)
1413 *resultmethod = n_method;
1416 inline_count_methods++;
1417 if (inline_debug_log_names)
1418 method_println(n_method);
1421 printf("==== %d.INLINE ==================================================================\n",debug_compile_inlined_code_counter);
1422 method_println(n_method);
1423 show_icmd_method(iln->m,cd,rd);
1424 dump_inline_tree(iln);
1425 show_icmd_method(n_method,n_cd,n_rd);
1426 debug_dump_inlined_code(iln,n_method,n_cd,n_rd);
1427 printf("-------- DONE -----------------------------------------------------------\n");
1432 debug_compile_inlined_code_counter++;
1437 bool inline_inline(methodinfo *m, codegendata *cd, registerdata *rd,
1438 methodinfo **resultmethod, codegendata **resultcd, registerdata **resultrd)
1443 printf("==== INLINE ==================================================================\n");
1447 iln = DNEW(inline_node);
1448 memset(iln,0,sizeof(inline_node));
1450 iln->ctx = (inline_context *) DMNEW(u1,sizeof(inline_context) + sizeof(inline_stack_translation) * 1000 /* XXX */);
1451 memset(iln->ctx,0,sizeof(inline_context));
1452 iln->ctx->stacktranslationstart = iln->ctx->stacktranslation - 1;
1455 /* we cannot use m->instructioncount because it may be greater than
1456 * the actual number of instructions in the basic blocks. */
1457 iln->instructioncount = 0;
1458 iln->cumul_instructioncount = 0;
1460 iln->stackcount = m->stackcount;
1461 iln->cumul_stackcount = m->stackcount;
1463 if (inline_inline_intern(m,cd,rd,iln)) {
1466 printf("==== TEST INLINE =============================================================\n");
1471 test_inlining(iln,cd,rd,resultmethod,resultcd,resultrd);
1475 printf("-------- DONE -----------------------------------------------------------\n");
1483 * These are local overrides for various environment variables in Emacs.
1484 * Please do not remove this and leave it at the end of the file, where
1485 * Emacs will automagically detect them.
1486 * ---------------------------------------------------------------------
1489 * indent-tabs-mode: t
1493 * vim:noexpandtab:sw=4:ts=4: