1 /* src/vm/jit/stack.c - stack analysis
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: Andreas Krall
32 $Id: stack.c 6055 2006-11-27 14:39:58Z edwin $
47 #include "mm/memory.h"
49 #include "native/native.h"
51 #include "toolbox/logging.h"
53 #include "vm/global.h"
54 #include "vm/builtin.h"
55 #include "vm/options.h"
56 #include "vm/resolve.h"
57 #include "vm/statistics.h"
58 #include "vm/stringlocal.h"
61 #include "vm/jit/abi.h"
62 #include "vm/jit/cfg.h"
63 #include "vm/jit/codegen-common.h"
64 #include "vm/jit/parse.h"
65 #include "vm/jit/show.h"
67 #if defined(ENABLE_DISASSEMBLER)
68 # include "vm/jit/disass.h"
71 #include "vm/jit/jit.h"
72 #include "vm/jit/stack.h"
74 #if defined(ENABLE_SSA)
75 # include "vm/jit/optimizing/lsra.h"
76 # include "vm/jit/optimizing/ssa.h"
77 #elif defined(ENABLE_LSRA)
78 # include "vm/jit/allocator/lsra.h"
81 /*#define STACK_VERBOSE*/
84 /* macro for saving #ifdefs ***************************************************/
86 #if defined(ENABLE_STATISTICS)
87 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr) \
90 if (stackdepth >= 10) \
91 count_store_depth[10]++; \
93 count_store_depth[stackdepth]++; \
96 #else /* !defined(ENABLE_STATISTICS) */
97 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)
101 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
104 /* For returnAddresses we use a field of the typeinfo to store from which */
105 /* subroutine the returnAddress will return, if used. */
106 /* XXX It would be nicer to use typeinfo.typeclass, but the verifier seems */
107 /* to need it initialised to NULL. This should be investigated. */
109 #if defined(ENABLE_VERIFIER)
110 #define SBRSTART typeinfo.elementclass.any
114 /* stackdata_t *****************************************************************
116 This struct holds internal data during stack analysis.
118 *******************************************************************************/
120 typedef struct stackdata_t stackdata_t;
123 basicblock *bptr; /* the current basic block being analysed */
124 stackptr new; /* next free stackelement */
125 s4 vartop; /* next free variable index */
126 s4 localcount; /* number of locals (at the start of var) */
127 s4 varcount; /* maximum number of variables expected */
128 s4 varsallocated; /* total number of variables allocated */
129 s4 maxlocals; /* max. number of Java locals */
130 varinfo *var; /* variable array (same as jd->var) */
131 s4 *javalocals; /* map from Java locals to jd->var indices */
132 methodinfo *m; /* the method being analysed */
133 jitdata *jd; /* current jitdata */
134 basicblock *last_real_block; /* the last block before the empty one */
135 bool repeat; /* if true, iterate the analysis again */
136 exception_entry **handlers; /* exception handlers for the current block */
137 exception_entry *extableend; /* points to the last exception entry */
138 stackelement exstack; /* instack for exception handlers */
142 /* macros for allocating/releasing variable indices *****************/
144 #define GET_NEW_INDEX(sd, new_varindex) \
146 assert((sd).vartop < (sd).varcount); \
147 (new_varindex) = ((sd).vartop)++; \
150 /* Not implemented now - could be used to reuse varindices. */
151 /* Pay attention to not release a localvar once implementing it! */
152 #define RELEASE_INDEX(sd, varindex)
154 #define GET_NEW_VAR(sd, newvarindex, newtype) \
156 GET_NEW_INDEX((sd), (newvarindex)); \
157 (sd).var[newvarindex].type = (newtype); \
161 /* macros for querying variable properties **************************/
163 #define IS_INOUT(sp) \
164 (sd.var[(sp)->varnum].flags & INOUT)
166 #define IS_PREALLOC(sp) \
167 (sd.var[(sp)->varnum].flags & PREALLOC)
169 #define IS_TEMPVAR(sp) \
170 ( ((sp)->varnum >= sd.localcount) \
171 && !(sd.var[(sp)->varnum].flags & (INOUT | PREALLOC)) )
174 #define IS_LOCALVAR_SD(sd, sp) \
175 ((sp)->varnum < (sd).localcount)
177 #define IS_LOCALVAR(sp) \
178 IS_LOCALVAR_SD(sd, (sp))
181 /* macros for setting variable properties ****************************/
183 #define SET_TEMPVAR(sp) \
185 if (IS_LOCALVAR((sp))) { \
186 stack_change_to_tempvar(&sd, (sp), iptr); \
188 sd.var[(sp)->varnum].flags &= ~(INOUT | PREALLOC); \
191 #define SET_PREALLOC(sp) \
193 assert(!IS_LOCALVAR((sp))); \
194 sd.var[(sp)->varnum].flags |= PREALLOC; \
198 /* macros for source operands ***************************************/
201 (iptr->s1.varindex = -1)
203 #define USE_S1(type1) \
206 CHECK_BASIC_TYPE(type1, curstack->type); \
207 iptr->s1.varindex = curstack->varnum; \
213 iptr->s1.varindex = curstack->varnum; \
216 #define USE_S1_S2(type1, type2) \
219 CHECK_BASIC_TYPE(type1, curstack->prev->type); \
220 CHECK_BASIC_TYPE(type2, curstack->type); \
221 iptr->sx.s23.s2.varindex = curstack->varnum; \
222 iptr->s1.varindex = curstack->prev->varnum; \
225 #define USE_S1_S2_ANY_ANY \
228 iptr->sx.s23.s2.varindex = curstack->varnum; \
229 iptr->s1.varindex = curstack->prev->varnum; \
232 #define USE_S1_S2_S3(type1, type2, type3) \
235 CHECK_BASIC_TYPE(type1, curstack->prev->prev->type); \
236 CHECK_BASIC_TYPE(type2, curstack->prev->type); \
237 CHECK_BASIC_TYPE(type3, curstack->type); \
238 iptr->sx.s23.s3.varindex = curstack->varnum; \
239 iptr->sx.s23.s2.varindex = curstack->prev->varnum; \
240 iptr->s1.varindex = curstack->prev->prev->varnum; \
243 /* The POPANY macro does NOT check stackdepth, or set stackdepth! */
246 if (curstack->varkind == UNDEFVAR) \
247 curstack->varkind = TEMPVAR; \
248 curstack = curstack->prev; \
251 #define POP_S1(type1) \
254 if (curstack->varkind == UNDEFVAR) \
255 curstack->varkind = TEMPVAR; \
256 curstack = curstack->prev; \
262 if (curstack->varkind == UNDEFVAR) \
263 curstack->varkind = TEMPVAR; \
264 curstack = curstack->prev; \
267 #define POP_S1_S2(type1, type2) \
269 USE_S1_S2(type1, type2); \
270 if (curstack->varkind == UNDEFVAR) \
271 curstack->varkind = TEMPVAR; \
272 if (curstack->prev->varkind == UNDEFVAR) \
273 curstack->prev->varkind = TEMPVAR; \
274 curstack = curstack->prev->prev; \
277 #define POP_S1_S2_ANY_ANY \
280 if (curstack->varkind == UNDEFVAR) \
281 curstack->varkind = TEMPVAR; \
282 if (curstack->prev->varkind == UNDEFVAR) \
283 curstack->prev->varkind = TEMPVAR; \
284 curstack = curstack->prev->prev; \
287 #define POP_S1_S2_S3(type1, type2, type3) \
289 USE_S1_S2_S3(type1, type2, type3); \
290 if (curstack->varkind == UNDEFVAR) \
291 curstack->varkind = TEMPVAR; \
292 if (curstack->prev->varkind == UNDEFVAR) \
293 curstack->prev->varkind = TEMPVAR; \
294 if (curstack->prev->prev->varkind == UNDEFVAR) \
295 curstack->prev->prev->varkind = TEMPVAR; \
296 curstack = curstack->prev->prev->prev; \
303 /* macros for setting the destination operand ***********************/
306 (iptr->dst.varindex = -1)
308 #define DST(typed, index) \
310 NEWSTACKn((typed),(index)); \
311 curstack->creator = iptr; \
312 iptr->dst.varindex = (index); \
315 #define DST_LOCALVAR(typed, index) \
317 NEWSTACK((typed), LOCALVAR, (index)); \
318 curstack->creator = iptr; \
319 iptr->dst.varindex = (index); \
323 /* macro for propagating constant values ****************************/
325 #if defined(ENABLE_VERIFIER)
326 #define COPY_VAL_AND_TYPE_VAR(sv, dv) \
328 if (((dv)->type = (sv)->type) == TYPE_RET) { \
329 (dv)->vv = (sv)->vv; \
330 (dv)->SBRSTART = (sv)->SBRSTART; \
334 #define COPY_VAL_AND_TYPE_VAR(sv, dv) \
336 (dv)->type = (sv)->type; \
337 if (((dv)->type = (sv)->type) == TYPE_RET) { \
338 (dv)->vv = (sv)->vv; \
343 #define COPY_VAL_AND_TYPE(sd, sindex, dindex) \
344 COPY_VAL_AND_TYPE_VAR((sd).var + (sindex), (sd).var + (dindex))
347 /* stack modelling macros *******************************************/
349 #define OP0_1(typed) \
352 GET_NEW_VAR(sd, new_index, (typed)); \
353 DST((typed), new_index); \
364 #define OP1_BRANCH(type1) \
370 #define OP1_1(type1, typed) \
373 GET_NEW_VAR(sd, new_index, (typed)); \
374 DST(typed, new_index); \
377 #define OP2_1(type1, type2, typed) \
379 POP_S1_S2(type1, type2); \
380 GET_NEW_VAR(sd, new_index, (typed)); \
381 DST(typed, new_index); \
396 #define OP1_0(type1) \
403 #define OP2_0(type1, type2) \
405 POP_S1_S2(type1, type2); \
410 #define OP2_BRANCH(type1, type2) \
412 POP_S1_S2(type1, type2); \
416 #define OP2_0_ANY_ANY \
423 #define OP3_0(type1, type2, type3) \
425 POP_S1_S2_S3(type1, type2, type3); \
430 #define LOAD(type1, index) \
432 DST_LOCALVAR(type1, index); \
436 #define STORE(type1, index) \
443 /* macros for DUP elimination ***************************************/
445 /* XXX replace NEW_VAR with NEW_INDEX */
446 #define DUP_SLOT(sp) \
448 GET_NEW_VAR(sd, new_index, (sp)->type); \
449 COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
450 NEWSTACK((sp)->type, TEMPVAR, new_index); \
453 /* does not check input stackdepth */
454 #define MOVE_UP(sp) \
456 iptr->opc = ICMD_MOVE; \
457 iptr->s1.varindex = (sp)->varnum; \
459 curstack->creator = iptr; \
460 iptr->dst.varindex = curstack->varnum; \
464 /* does not check input stackdepth */
465 #define COPY_UP(sp) \
468 iptr->opc = ICMD_COPY; \
469 iptr->s1.varindex = (sp)->varnum; \
471 curstack->creator = iptr; \
472 iptr->dst.varindex = curstack->varnum; \
476 #define COPY_DOWN(s, d) \
479 iptr->opc = ICMD_COPY; \
480 iptr->s1.varindex = (s)->varnum; \
481 iptr->dst.varindex = (d)->varnum; \
482 (d)->creator = iptr; \
485 #define MOVE_TO_TEMP(sp) \
487 GET_NEW_INDEX(sd, new_index); \
488 iptr->opc = ICMD_MOVE; \
489 iptr->s1.varindex = (sp)->varnum; \
490 iptr->dst.varindex = new_index; \
491 COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
492 (sp)->varnum = new_index; \
493 (sp)->varkind = TEMPVAR; \
496 /* macros for branching / reaching basic blocks *********************/
498 #define BRANCH_TARGET(bt, tempbptr) \
500 tempbptr = BLOCK_OF((bt).insindex); \
501 tempbptr = stack_mark_reached(&sd, tempbptr, curstack, \
503 if (tempbptr == NULL) \
505 (bt).block = tempbptr; \
508 #define BRANCH(tempbptr) \
509 BRANCH_TARGET(iptr->dst, tempbptr)
512 /* forward declarations *******************************************************/
514 static void stack_create_invars(stackdata_t *sd, basicblock *b,
515 stackptr curstack, int stackdepth);
516 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b);
518 #if defined(STACK_VERBOSE)
519 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v);
520 static void stack_verbose_show_variable(stackdata_t *sd, s4 index);
521 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr);
522 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse);
523 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend);
524 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr,
529 /* stack_init ******************************************************************
531 Initialized the stack analysis subsystem (called by jit_init).
533 *******************************************************************************/
535 bool stack_init(void)
541 /* stack_grow_variable_array ***************************************************
543 Grow the variable array so the given number of additional variables fits in.
544 The number is added to `varcount`, which is the maximum number of variables
545 we expect to need at this point. The actual number of variables
546 (`varsallocated`) may be larger than that, in order to avoid too many
550 sd...........stack analysis data
551 num..........number of additional variables
553 *******************************************************************************/
555 static void stack_grow_variable_array(stackdata_t *sd, s4 num)
561 if (sd->varcount + num > sd->varsallocated) {
562 newsize = 2*sd->varsallocated + num;
564 sd->var = DMREALLOC(sd->var, varinfo, sd->varsallocated, newsize);
565 MZERO(sd->var + sd->varsallocated, varinfo, (newsize - sd->varsallocated));
566 sd->varsallocated = newsize;
567 sd->jd->var = sd->var;
571 sd->jd->varcount += num;
573 assert(sd->varcount <= sd->varsallocated);
577 /* stack_append_block **********************************************************
579 Append the given block after the last real block of the method (before
580 the pseudo-block at the end).
583 sd...........stack analysis data
584 b............the block to append
586 *******************************************************************************/
588 static void stack_append_block(stackdata_t *sd, basicblock *b)
590 #if defined(STACK_VERBOSE)
591 printf("APPENDING BLOCK L%0d\n", b->nr);
594 b->next = sd->last_real_block->next;
595 sd->last_real_block->next = b;
596 sd->last_real_block = b;
597 b->nr = sd->jd->basicblockcount++;
598 b->next->nr = b->nr + 1;
602 /* stack_clone_block ***********************************************************
604 Create a copy of the given block and insert it at the end of the method.
606 CAUTION: This function does not copy the any variables or the instruction
607 list. It _does_, however, reserve space for the block's invars in the
611 sd...........stack analysis data
612 b............the block to clone
615 a pointer to the copy
617 *******************************************************************************/
619 static basicblock * stack_clone_block(stackdata_t *sd, basicblock *b)
623 clone = DNEW(basicblock);
626 clone->iinstr = NULL;
627 clone->inlocals = NULL;
628 clone->javalocals = NULL;
629 clone->invars = NULL;
631 clone->original = (b->original) ? b->original : b;
632 clone->copied_to = clone->original->copied_to;
633 clone->original->copied_to = clone;
635 clone->flags = BBREACHED;
637 stack_append_block(sd, clone);
639 /* reserve space for the invars of the clone */
641 stack_grow_variable_array(sd, b->indepth);
643 #if defined(STACK_VERBOSE)
644 printf("cloning block L%03d ------> L%03d\n", b->nr, clone->nr);
651 /* stack_create_locals *********************************************************
653 Create the local variables for the start of the given basic block.
656 sd...........stack analysis data
657 b............block to create the locals for
659 *******************************************************************************/
661 static void stack_create_locals(stackdata_t *sd, basicblock *b)
667 /* copy the current state of the local variables */
668 /* (one extra local is needed by the verifier) */
670 dv = DMNEW(varinfo, sd->localcount + VERIFIER_EXTRA_LOCALS);
672 for (i=0; i<sd->localcount; ++i)
675 /* the current map from java locals to cacao variables */
677 jl = DMNEW(s4, sd->maxlocals);
679 MCOPY(jl, sd->javalocals, s4, sd->maxlocals);
683 /* stack_merge_locals **********************************************************
685 Merge local variables at the beginning of the given basic block.
688 sd...........stack analysis data
689 b............the block that is reached
691 *******************************************************************************/
693 static void stack_merge_locals(stackdata_t *sd, basicblock *b)
699 /* If a javalocal is mapped to different cacao locals along the */
700 /* incoming control-flow edges, it becomes undefined. */
702 for (i=0; i<sd->maxlocals; ++i) {
703 if (b->javalocals[i] != UNUSED && b->javalocals[i] != sd->javalocals[i]) {
704 b->javalocals[i] = UNUSED;
705 if (b->flags >= BBFINISHED)
706 b->flags = BBTYPECHECK_REACHED;
707 if (b->nr <= sd->bptr->nr)
712 #if defined(ENABLE_VERIFIER)
714 for (i=0; i<sd->localcount; ++i) {
715 dv = b->inlocals + i;
717 if ((sv->type == TYPE_RET && dv->type == TYPE_RET)
718 && (sv->SBRSTART != dv->SBRSTART))
720 dv->type = TYPE_VOID;
721 if (b->flags >= BBFINISHED)
722 b->flags = BBTYPECHECK_REACHED;
723 sd->repeat = true; /* This is very rare, so just repeat */
727 #endif /* defined(ENABLE_VERIFIER) */
731 /* stack_create_invars *********************************************************
733 Create the invars for the given basic block. Also make a copy of the locals.
736 sd...........stack analysis data
737 b............block to create the invars for
738 curstack.....current stack top
739 stackdepth...current stack depth
741 This function creates STACKDEPTH invars and sets their types to the
742 types to the types of the corresponding slot in the current stack.
744 *******************************************************************************/
746 static void stack_create_invars(stackdata_t *sd, basicblock *b,
747 stackptr curstack, int stackdepth)
755 assert(sd->vartop + stackdepth <= sd->varcount);
757 b->indepth = stackdepth;
758 b->invars = DMNEW(s4, stackdepth);
760 /* allocate the variable indices */
761 index = (sd->vartop += stackdepth);
764 for (sp = curstack; i--; sp = sp->prev) {
765 b->invars[i] = --index;
766 dv = sd->var + index;
767 sv = sd->var + sp->varnum;
769 COPY_VAL_AND_TYPE_VAR(sv, dv);
772 stack_create_locals(sd, b);
776 /* stack_create_invars_from_outvars ********************************************
778 Create the invars for the given basic block. Also make a copy of the locals.
779 Types are propagated from the outvars of the current block.
782 sd...........stack analysis data
783 b............block to create the invars for
785 *******************************************************************************/
787 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b)
793 n = sd->bptr->outdepth;
794 assert(sd->vartop + n <= sd->varcount);
797 b->invars = DMNEW(s4, n);
800 dv = sd->var + sd->vartop;
802 /* allocate the invars */
804 for (i=0; i<n; ++i, ++dv) {
805 sv = sd->var + sd->bptr->outvars[i];
806 b->invars[i] = sd->vartop++;
808 COPY_VAL_AND_TYPE_VAR(sv, dv);
812 stack_create_locals(sd, b);
816 /* stack_check_invars **********************************************************
818 Check the current stack against the invars of the given basic block.
819 Depth and types must match.
822 sd...........stack analysis data
823 b............block which invars to check against
824 curstack.....current stack top
825 stackdepth...current stack depth
829 NULL.........a VerifyError has been thrown
831 *******************************************************************************/
833 static basicblock * stack_check_invars(stackdata_t *sd, basicblock *b,
834 stackptr curstack, int stackdepth)
843 #if defined(STACK_VERBOSE)
844 printf("stack_check_invars(L%03d)\n", b->nr);
847 /* find original of b */
852 #if defined(STACK_VERBOSE)
853 printf("original is L%03d\n", orig->nr);
858 #if defined(ENABLE_VERIFIER)
859 if (i != stackdepth) {
860 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
868 #if defined(STACK_VERBOSE)
869 printf("checking against ");
870 stack_verbose_show_block(sd, b); printf("\n");
874 for (i = orig->indepth; i--; sp = sp->prev) {
875 dv = sd->var + b->invars[i];
876 sv = sd->var + sp->varnum;
878 #if defined(ENABLE_VERIFIER)
879 if (dv->type != sp->type) {
880 exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
885 if (sp->type == TYPE_RET) {
886 #if defined(ENABLE_VERIFIER)
887 if (dv->SBRSTART != sv->SBRSTART) {
888 exceptions_throw_verifyerror(sd->m, "Mismatched stack types");
892 if (dv->vv.retaddr != sv->vv.retaddr) {
894 /* don't break! have to check the remaining stackslots */
900 for (i=0; i<sd->localcount; ++i) {
901 dv = b->inlocals + i;
903 if (sv->type == TYPE_RET && dv->type == TYPE_RET) {
905 #if defined(ENABLE_VERIFIER)
906 (sv->SBRSTART == dv->SBRSTART) &&
908 (sv->vv.retaddr != dv->vv.retaddr))
918 /* XXX cascading collapse? */
920 stack_merge_locals(sd, b);
922 #if defined(STACK_VERBOSE)
923 printf("------> using L%03d\n", b->nr);
927 } while ((b = b->copied_to) != NULL);
929 b = stack_clone_block(sd, orig);
933 stack_create_invars(sd, b, curstack, stackdepth);
938 /* stack_check_invars_from_outvars *********************************************
940 Check the outvars of the current block against the invars of the given block.
941 Depth and types must match.
944 sd...........stack analysis data
945 b............block which invars to check against
949 NULL.........a VerifyError has been thrown
951 *******************************************************************************/
953 static basicblock * stack_check_invars_from_outvars(stackdata_t *sd, basicblock *b)
961 #if defined(STACK_VERBOSE)
962 printf("stack_check_invars_from_outvars(L%03d)\n", b->nr);
965 /* find original of b */
970 #if defined(STACK_VERBOSE)
971 printf("original is L%03d\n", orig->nr);
975 n = sd->bptr->outdepth;
977 #if defined(ENABLE_VERIFIER)
979 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
987 #if defined(STACK_VERBOSE)
988 printf("checking against ");
989 stack_verbose_show_block(sd, b); printf("\n");
993 dv = sd->var + b->invars[0];
995 for (i=0; i<n; ++i, ++dv) {
996 sv = sd->var + sd->bptr->outvars[i];
998 #if defined(ENABLE_VERIFIER)
999 if (sv->type != dv->type) {
1000 exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
1005 if (dv->type == TYPE_RET) {
1006 #if defined(ENABLE_VERIFIER)
1007 if (sv->SBRSTART != dv->SBRSTART) {
1008 exceptions_throw_verifyerror(sd->m, "Mismatched stack types");
1012 if (sv->vv.retaddr != dv->vv.retaddr) {
1014 /* don't break! have to check the remaining stackslots */
1021 for (i=0; i<sd->localcount; ++i) {
1022 dv = b->inlocals + i;
1025 #if defined(ENABLE_VERIFIER)
1026 (sv->SBRSTART == dv->SBRSTART) &&
1028 (sv->type == TYPE_RET && dv->type == TYPE_RET))
1030 if (sv->vv.retaddr != dv->vv.retaddr) {
1039 /* XXX cascading collapse? */
1041 stack_merge_locals(sd, b);
1043 #if defined(STACK_VERBOSE)
1044 printf("------> using L%03d\n", b->nr);
1048 } while ((b = b->copied_to) != NULL);
1050 b = stack_clone_block(sd, orig);
1054 stack_create_invars_from_outvars(sd, b);
1059 /* stack_create_instack ********************************************************
1061 Create the instack of the current basic block.
1064 sd...........stack analysis data
1067 the current stack top at the start of the basic block.
1069 *******************************************************************************/
1071 static stackptr stack_create_instack(stackdata_t *sd)
1077 if ((depth = sd->bptr->indepth) == 0)
1080 sp = (sd->new += depth);
1084 index = sd->bptr->invars[depth];
1086 sp->type = sd->var[index].type;
1090 sp->varkind = STACKVAR;
1094 /* return the top of the created stack */
1099 /* stack_mark_reached **********************************************************
1101 Mark the given block reached and propagate the current stack and locals to
1102 it. This function specializes the target block, if necessary, and returns
1103 a pointer to the specialized target.
1106 sd...........stack analysis data
1107 b............the block to reach
1108 curstack.....the current stack top
1109 stackdepth...the current stack depth
1112 a pointer to (a specialized version of) the target
1113 NULL.........a VerifyError has been thrown
1115 *******************************************************************************/
1117 static basicblock *stack_mark_reached(stackdata_t *sd, basicblock *b, stackptr curstack, int stackdepth)
1119 #if defined(STACK_VERBOSE)
1120 printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1122 /* mark targets of backward branches */
1123 if (b->nr <= sd->bptr->nr)
1124 b->bitflags |= BBFLAG_REPLACEMENT;
1126 if (b->flags < BBREACHED) {
1127 /* b is reached for the first time. Create its invars. */
1129 #if defined(STACK_VERBOSE)
1130 printf("reached L%03d for the first time\n", b->nr);
1133 stack_create_invars(sd, b, curstack, stackdepth);
1135 b->flags = BBREACHED;
1140 /* b has been reached before. Check that its invars match. */
1142 return stack_check_invars(sd, b, curstack, stackdepth);
1147 /* stack_mark_reached_from_outvars *********************************************
1149 Mark the given block reached and propagate the outvars of the current block
1150 and the current locals to it. This function specializes the target block,
1151 if necessary, and returns a pointer to the specialized target.
1154 sd...........stack analysis data
1155 b............the block to reach
1158 a pointer to (a specialized version of) the target
1159 NULL.........a VerifyError has been thrown
1161 *******************************************************************************/
1163 static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
1165 #if defined(STACK_VERBOSE)
1166 printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1168 /* mark targets of backward branches */
1169 if (b->nr <= sd->bptr->nr)
1170 b->bitflags |= BBFLAG_REPLACEMENT;
1172 if (b->flags < BBREACHED) {
1173 /* b is reached for the first time. Create its invars. */
1175 #if defined(STACK_VERBOSE)
1176 printf("reached L%03d for the first time\n", b->nr);
1179 stack_create_invars_from_outvars(sd, b);
1181 b->flags = BBREACHED;
1186 /* b has been reached before. Check that its invars match. */
1188 return stack_check_invars_from_outvars(sd, b);
1193 /* stack_reach_next_block ******************************************************
1195 Mark the following block reached and propagate the outvars of the current block
1196 and the current locals to it. This function specializes the target block,
1197 if necessary, and returns a pointer to the specialized target.
1200 sd...........stack analysis data
1203 a pointer to (a specialized version of) the following block
1204 NULL.........a VerifyError has been thrown
1206 *******************************************************************************/
1208 static bool stack_reach_next_block(stackdata_t *sd)
1213 tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
1214 tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
1218 if (tbptr != sd->bptr->next) {
1219 #if defined(STACK_VERBOSE)
1220 printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
1222 iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
1223 assert(iptr->opc == ICMD_NOP);
1224 iptr->opc = ICMD_GOTO;
1225 iptr->dst.block = tbptr;
1227 if (tbptr->flags < BBFINISHED)
1228 sd->repeat = true; /* XXX check if we really need to repeat */
1235 /* stack_reach_handlers ********************************************************
1237 Reach the exception handlers for the current block.
1240 sd...........stack analysis data
1243 true.........everything ok
1244 false........a VerifyError has been thrown
1246 *******************************************************************************/
1248 static bool stack_reach_handlers(stackdata_t *sd)
1253 #if defined(STACK_VERBOSE)
1254 printf("reaching exception handlers...\n");
1257 for (i=0; sd->handlers[i]; ++i) {
1258 tbptr = sd->handlers[i]->handler;
1260 tbptr->type = BBTYPE_EXH;
1261 tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
1263 /* reach (and specialize) the handler block */
1265 tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
1270 sd->handlers[i]->handler = tbptr;
1277 /* stack_reanalyse_block ******************************************************
1279 Re-analyse the current block. This is called if either the block itself
1280 has already been analysed before, or the current block is a clone of an
1281 already analysed block, and this clone is reached for the first time.
1282 In the latter case, this function does all that is necessary for fully
1283 cloning the block (cloning the instruction list and variables, etc.).
1286 sd...........stack analysis data
1289 true.........everything ok
1290 false........a VerifyError has been thrown
1292 *******************************************************************************/
1294 #define RELOCATE(index) \
1296 if ((index) >= blockvarstart) \
1297 (index) += blockvarshift; \
1298 else if ((index) >= invarstart) \
1299 (index) += invarshift; \
1302 bool stack_reanalyse_block(stackdata_t *sd)
1314 branch_target_t *table;
1315 lookup_target_t *lookup;
1317 bool cloneinstructions;
1318 exception_entry *ex;
1320 #if defined(STACK_VERBOSE)
1321 stack_verbose_block_enter(sd, true);
1328 assert(orig != NULL);
1330 /* clone the instruction list */
1332 cloneinstructions = true;
1334 assert(orig->iinstr);
1336 iptr = DMNEW(instruction, len + 1);
1338 MCOPY(iptr, orig->iinstr, instruction, len);
1339 iptr[len].opc = ICMD_NOP;
1343 /* reserve space for the clone's block variables */
1345 stack_grow_variable_array(sd, orig->varcount);
1347 /* we already have the invars set */
1349 assert(b->indepth == orig->indepth);
1351 /* calculate relocation shifts for invars and block variables */
1353 if (orig->indepth) {
1354 invarstart = orig->invars[0];
1355 invarshift = b->invars[0] - invarstart;
1358 invarstart = INT_MAX;
1361 blockvarstart = orig->varstart;
1362 blockvarshift = sd->vartop - blockvarstart;
1364 /* copy block variables */
1366 b->varstart = sd->vartop;
1367 b->varcount = orig->varcount;
1368 sd->vartop += b->varcount;
1369 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1373 b->outdepth = orig->outdepth;
1374 b->outvars = DMNEW(s4, orig->outdepth);
1375 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1377 /* clone exception handlers */
1379 for (i=0; sd->handlers[i]; ++i) {
1380 ex = DNEW(exception_entry);
1381 ex->handler = sd->handlers[i]->handler;
1383 ex->end = b; /* XXX hack, see end of stack_analyse */
1384 ex->catchtype = sd->handlers[i]->catchtype;
1387 assert(sd->extableend->down == NULL);
1388 sd->extableend->down = ex;
1389 sd->extableend = ex;
1390 sd->jd->exceptiontablelength++;
1392 sd->handlers[i] = ex;
1396 cloneinstructions = false;
1399 invarstart = sd->vartop;
1400 blockvarstart = sd->vartop;
1405 /* find exception handlers for the cloned block */
1407 ex = sd->jd->exceptiontable;
1408 for (; ex != NULL; ex = ex->down) {
1409 /* XXX the cloned exception handlers have identical */
1410 /* start end end blocks. */
1411 if ((ex->start == b) && (ex->end == b)) {
1412 sd->handlers[len++] = ex;
1415 sd->handlers[len] = NULL;
1418 #if defined(STACK_VERBOSE)
1419 printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1420 printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1423 /* mark block as finished */
1425 b->flags = BBFINISHED;
1427 /* initialize locals at the start of this block */
1430 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1432 MCOPY(sd->javalocals, b->javalocals, s4, sd->maxlocals);
1434 /* reach exception handlers for this block */
1436 if (!stack_reach_handlers(sd))
1439 superblockend = false;
1441 for (len = b->icount; len--; iptr++) {
1442 #if defined(STACK_VERBOSE)
1443 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1447 switch (iptr->opc) {
1449 j = iptr->s1.varindex;
1451 #if defined(ENABLE_VERIFIER)
1452 if (sd->var[j].type != TYPE_RET) {
1453 exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1458 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1459 superblockend = true;
1463 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1464 RELOCATE(iptr->dst.varindex);
1465 superblockend = true;
1469 superblockend = true;
1472 case ICMD_CHECKNULL:
1473 case ICMD_PUTSTATICCONST:
1481 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1482 superblockend = true;
1485 /* pop 0 push 1 const */
1493 /* pop 0 push 1 load */
1500 RELOCATE(iptr->dst.varindex);
1513 RELOCATE(iptr->sx.s23.s2.varindex);
1514 RELOCATE(iptr->s1.varindex);
1515 RELOCATE(iptr->dst.varindex);
1528 RELOCATE(iptr->sx.s23.s3.varindex);
1529 RELOCATE(iptr->sx.s23.s2.varindex);
1530 RELOCATE(iptr->s1.varindex);
1533 /* pop 1 push 0 store */
1540 RELOCATE(iptr->s1.varindex);
1542 j = iptr->dst.varindex;
1543 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1544 i = iptr->sx.s23.s3.javaindex;
1545 if (iptr->flags.bits & INS_FLAG_RETADDR) {
1546 iptr->sx.s23.s2.retaddrnr =
1547 UNUSED - (1 + sd->var[j].vv.retaddr->nr);
1548 sd->javalocals[i] = iptr->sx.s23.s2.retaddrnr;
1551 sd->javalocals[i] = j;
1552 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
1553 sd->javalocals[i-1] = UNUSED;
1554 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
1555 sd->javalocals[i+1] = UNUSED;
1566 RELOCATE(iptr->s1.varindex);
1567 superblockend = true;
1570 case ICMD_PUTSTATIC:
1571 case ICMD_PUTFIELDCONST:
1573 RELOCATE(iptr->s1.varindex);
1576 /* pop 1 push 0 branch */
1579 case ICMD_IFNONNULL:
1594 RELOCATE(iptr->s1.varindex);
1595 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1598 /* pop 1 push 0 table branch */
1600 case ICMD_TABLESWITCH:
1601 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1603 if (cloneinstructions) {
1604 table = DMNEW(branch_target_t, i);
1605 MCOPY(table, iptr->dst.table, branch_target_t, i);
1606 iptr->dst.table = table;
1609 table = iptr->dst.table;
1612 RELOCATE(iptr->s1.varindex);
1614 table->block = stack_mark_reached_from_outvars(sd, table->block);
1617 superblockend = true;
1620 case ICMD_LOOKUPSWITCH:
1621 i = iptr->sx.s23.s2.lookupcount;
1622 if (cloneinstructions) {
1623 lookup = DMNEW(lookup_target_t, i);
1624 MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1625 iptr->dst.lookup = lookup;
1628 lookup = iptr->dst.lookup;
1630 RELOCATE(iptr->s1.varindex);
1632 lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1635 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1636 superblockend = true;
1639 case ICMD_MONITORENTER:
1640 case ICMD_MONITOREXIT:
1641 RELOCATE(iptr->s1.varindex);
1644 /* pop 2 push 0 branch */
1646 case ICMD_IF_ICMPEQ:
1647 case ICMD_IF_ICMPNE:
1648 case ICMD_IF_ICMPLT:
1649 case ICMD_IF_ICMPGE:
1650 case ICMD_IF_ICMPGT:
1651 case ICMD_IF_ICMPLE:
1653 case ICMD_IF_LCMPEQ:
1654 case ICMD_IF_LCMPNE:
1655 case ICMD_IF_LCMPLT:
1656 case ICMD_IF_LCMPGE:
1657 case ICMD_IF_LCMPGT:
1658 case ICMD_IF_LCMPLE:
1660 case ICMD_IF_FCMPEQ:
1661 case ICMD_IF_FCMPNE:
1663 case ICMD_IF_FCMPL_LT:
1664 case ICMD_IF_FCMPL_GE:
1665 case ICMD_IF_FCMPL_GT:
1666 case ICMD_IF_FCMPL_LE:
1668 case ICMD_IF_FCMPG_LT:
1669 case ICMD_IF_FCMPG_GE:
1670 case ICMD_IF_FCMPG_GT:
1671 case ICMD_IF_FCMPG_LE:
1673 case ICMD_IF_DCMPEQ:
1674 case ICMD_IF_DCMPNE:
1676 case ICMD_IF_DCMPL_LT:
1677 case ICMD_IF_DCMPL_GE:
1678 case ICMD_IF_DCMPL_GT:
1679 case ICMD_IF_DCMPL_LE:
1681 case ICMD_IF_DCMPG_LT:
1682 case ICMD_IF_DCMPG_GE:
1683 case ICMD_IF_DCMPG_GT:
1684 case ICMD_IF_DCMPG_LE:
1686 case ICMD_IF_ACMPEQ:
1687 case ICMD_IF_ACMPNE:
1688 RELOCATE(iptr->sx.s23.s2.varindex);
1689 RELOCATE(iptr->s1.varindex);
1690 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1696 case ICMD_IASTORECONST:
1697 case ICMD_LASTORECONST:
1698 case ICMD_AASTORECONST:
1699 case ICMD_BASTORECONST:
1700 case ICMD_CASTORECONST:
1701 case ICMD_SASTORECONST:
1703 RELOCATE(iptr->sx.s23.s2.varindex);
1704 RELOCATE(iptr->s1.varindex);
1707 /* pop 0 push 1 copy */
1711 RELOCATE(iptr->dst.varindex);
1712 RELOCATE(iptr->s1.varindex);
1713 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1755 RELOCATE(iptr->sx.s23.s2.varindex);
1756 RELOCATE(iptr->s1.varindex);
1757 RELOCATE(iptr->dst.varindex);
1762 case ICMD_CHECKCAST:
1763 case ICMD_ARRAYLENGTH:
1764 case ICMD_INSTANCEOF:
1766 case ICMD_ANEWARRAY:
1768 case ICMD_IADDCONST:
1769 case ICMD_ISUBCONST:
1770 case ICMD_IMULCONST:
1774 case ICMD_IANDCONST:
1776 case ICMD_IXORCONST:
1777 case ICMD_ISHLCONST:
1778 case ICMD_ISHRCONST:
1779 case ICMD_IUSHRCONST:
1780 case ICMD_LADDCONST:
1781 case ICMD_LSUBCONST:
1782 case ICMD_LMULCONST:
1786 case ICMD_LANDCONST:
1788 case ICMD_LXORCONST:
1789 case ICMD_LSHLCONST:
1790 case ICMD_LSHRCONST:
1791 case ICMD_LUSHRCONST:
1795 case ICMD_INT2SHORT:
1811 RELOCATE(iptr->s1.varindex);
1812 RELOCATE(iptr->dst.varindex);
1817 case ICMD_GETSTATIC:
1819 RELOCATE(iptr->dst.varindex);
1822 /* pop many push any */
1824 case ICMD_INVOKESTATIC:
1825 case ICMD_INVOKESPECIAL:
1826 case ICMD_INVOKEVIRTUAL:
1827 case ICMD_INVOKEINTERFACE:
1829 case ICMD_MULTIANEWARRAY:
1830 i = iptr->s1.argcount;
1831 if (cloneinstructions) {
1832 argp = DMNEW(s4, i);
1833 MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1834 iptr->sx.s23.s2.args = argp;
1837 argp = iptr->sx.s23.s2.args;
1844 RELOCATE(iptr->dst.varindex);
1849 new_internalerror("Unknown ICMD %d during stack re-analysis",
1854 #if defined(STACK_VERBOSE)
1855 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1860 /* relocate outvars */
1862 for (i=0; i<b->outdepth; ++i) {
1863 RELOCATE(b->outvars[i]);
1866 #if defined(STACK_VERBOSE)
1867 stack_verbose_block_exit(sd, superblockend);
1870 /* propagate to the next block */
1873 if (!stack_reach_next_block(sd))
1880 /* stack_change_to_tempvar *****************************************************
1882 Change the given stackslot to a TEMPVAR. This includes creating a new
1883 temporary variable and changing the dst.varindex of the creator of the
1884 stacklot to the new variable index. If this stackslot has been passed
1885 through ICMDs between the point of its creation and the current point,
1886 then the variable index is also changed in these ICMDs.
1889 sd...........stack analysis data
1890 sp...........stackslot to change
1891 ilimit.......instruction up to which to look for ICMDs passing-through
1892 the stackslot (exclusive). This may point exactly after the
1893 last instruction, in which case the search is done to the
1896 *******************************************************************************/
1898 static void stack_change_to_tempvar(stackdata_t *sd, stackptr sp,
1899 instruction *ilimit)
1907 oldindex = sp->varnum;
1909 /* create a new temporary variable */
1911 GET_NEW_VAR(*sd, newindex, sp->type);
1913 sd->var[newindex].flags = sp->flags;
1915 /* change the stackslot */
1917 sp->varnum = newindex;
1918 sp->varkind = TEMPVAR;
1920 /* change the dst.varindex of the stackslot's creator */
1923 sp->creator->dst.varindex = newindex;
1925 /* handle ICMDs this stackslot passed through, if any */
1927 if (sp->flags & PASSTHROUGH) {
1928 iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
1930 /* assert that the limit points to an ICMD, or after the last one */
1932 assert(ilimit >= sd->bptr->iinstr);
1933 assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
1935 /* find the stackdepth under sp plus one */
1936 /* Note: This number is usually known when this function is called, */
1937 /* but calculating it here is less error-prone and should not be */
1938 /* a performance problem. */
1940 for (depth = 0; sp != NULL; sp = sp->prev)
1943 /* iterate over all instructions in the range and replace */
1945 for (; iptr < ilimit; ++iptr) {
1946 switch (iptr->opc) {
1947 case ICMD_INVOKESTATIC:
1948 case ICMD_INVOKESPECIAL:
1949 case ICMD_INVOKEVIRTUAL:
1950 case ICMD_INVOKEINTERFACE:
1952 i = iptr->s1.argcount - depth;
1953 if (iptr->sx.s23.s2.args[i] == oldindex) {
1954 iptr->sx.s23.s2.args[i] = newindex;
1957 /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
1958 /* stackslot, it must be added in this switch! */
1965 /* stack_init_javalocals *******************************************************
1967 Initialize the mapping from Java locals to cacao variables at method entry.
1970 sd...........stack analysis data
1972 *******************************************************************************/
1974 static void stack_init_javalocals(stackdata_t *sd)
1983 jl = DMNEW(s4, sd->maxlocals);
1984 jd->basicblocks[0].javalocals = jl;
1986 for (i=0; i<sd->maxlocals; ++i)
1989 md = jd->m->parseddesc;
1991 for (i=0; i<md->paramcount; ++i) {
1992 t = md->paramtypes[i].type;
1993 jl[j] = jd->local_map[5*j + t];
1995 if (IS_2_WORD_TYPE(t))
2001 /* stack_analyse ***************************************************************
2003 Analyse_stack uses the intermediate code created by parse.c to
2004 build a model of the JVM operand stack for the current method.
2006 The following checks are performed:
2007 - check for operand stack underflow (before each instruction)
2008 - check for operand stack overflow (after[1] each instruction)
2009 - check for matching stack depth at merging points
2010 - check for matching basic types[2] at merging points
2011 - check basic types for instruction input (except for BUILTIN*
2012 opcodes, INVOKE* opcodes and MULTIANEWARRAY)
2014 [1]) Checking this after the instruction should be ok. parse.c
2015 counts the number of required stack slots in such a way that it is
2016 only vital that we don't exceed `maxstack` at basic block
2019 [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
2020 DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
2021 types are not discerned.
2023 *******************************************************************************/
2025 bool stack_analyse(jitdata *jd)
2027 methodinfo *m; /* method being analyzed */
2030 #if defined(ENABLE_SSA)
2034 stackptr curstack; /* current stack top */
2036 int opcode; /* opcode of current instruction */
2039 int len; /* # of instructions after the current one */
2040 bool superblockend; /* if true, no fallthrough to next block */
2041 bool deadcode; /* true if no live code has been reached */
2042 instruction *iptr; /* the current instruction */
2044 basicblock *original;
2045 exception_entry *ex;
2047 stackptr *last_store_boundary;
2048 stackptr coalescing_boundary;
2050 stackptr src1, src2, src3, src4, dst1, dst2;
2052 branch_target_t *table;
2053 lookup_target_t *lookup;
2054 #if defined(ENABLE_VERIFIER)
2055 int expectedtype; /* used by CHECK_BASIC_TYPE */
2057 builtintable_entry *bte;
2059 constant_FMIref *fmiref;
2060 #if defined(ENABLE_STATISTICS)
2061 int iteration_count; /* number of iterations of analysis */
2063 int new_index; /* used to get a new var index with GET_NEW_INDEX*/
2065 #if defined(STACK_VERBOSE)
2066 show_method(jd, SHOW_PARSE);
2069 /* get required compiler data - initialization */
2073 #if defined(ENABLE_SSA)
2077 /* initialize the stackdata_t struct */
2081 sd.varcount = jd->varcount;
2082 sd.vartop = jd->vartop;
2083 sd.localcount = jd->localcount;
2085 sd.varsallocated = sd.varcount;
2086 sd.maxlocals = m->maxlocals;
2087 sd.javalocals = DMNEW(s4, sd.maxlocals);
2088 sd.handlers = DMNEW(exception_entry *, jd->exceptiontablelength + 1);
2090 /* prepare the variable for exception handler stacks */
2091 /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2093 sd.exstack.type = TYPE_ADR;
2094 sd.exstack.prev = NULL;
2095 sd.exstack.varnum = sd.localcount;
2096 sd.var[sd.exstack.varnum].type = TYPE_ADR;
2098 #if defined(ENABLE_LSRA)
2099 m->maxlifetimes = 0;
2102 #if defined(ENABLE_STATISTICS)
2103 iteration_count = 0;
2106 /* find the last real basic block */
2108 sd.last_real_block = NULL;
2109 tbptr = jd->basicblocks;
2110 while (tbptr->next) {
2111 sd.last_real_block = tbptr;
2112 tbptr = tbptr->next;
2114 assert(sd.last_real_block);
2116 /* find the last exception handler */
2118 if (jd->exceptiontablelength)
2119 sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2121 sd.extableend = NULL;
2123 /* init jd->interface_map */
2125 jd->maxinterfaces = m->maxstack;
2126 jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2127 for (i = 0; i < m->maxstack * 5; i++)
2128 jd->interface_map[i].flags = UNUSED;
2130 last_store_boundary = DMNEW(stackptr, m->maxlocals);
2132 /* initialize flags and invars (none) of first block */
2134 jd->basicblocks[0].flags = BBREACHED;
2135 jd->basicblocks[0].invars = NULL;
2136 jd->basicblocks[0].indepth = 0;
2137 jd->basicblocks[0].inlocals =
2138 DMNEW(varinfo, jd->localcount + VERIFIER_EXTRA_LOCALS);
2139 MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo,
2140 jd->localcount + VERIFIER_EXTRA_LOCALS);
2142 /* initialize java local mapping of first block */
2144 stack_init_javalocals(&sd);
2146 /* stack analysis loop (until fixpoint reached) **************************/
2149 #if defined(ENABLE_STATISTICS)
2153 /* initialize loop over basic blocks */
2155 sd.bptr = jd->basicblocks;
2156 superblockend = true;
2158 curstack = NULL; stackdepth = 0;
2161 /* iterate over basic blocks *****************************************/
2163 for (; sd.bptr; sd.bptr = sd.bptr->next) {
2165 if (sd.bptr->flags == BBDELETED) {
2166 /* This block has been deleted - do nothing. */
2171 if (sd.bptr->flags == BBTYPECHECK_REACHED) {
2172 /* re-analyse a block because its input changed */
2174 if (!stack_reanalyse_block(&sd))
2176 superblockend = true; /* XXX */
2180 if (superblockend && (sd.bptr->flags < BBREACHED)) {
2181 /* This block has not been reached so far, and we */
2182 /* don't fall into it, so we'll have to iterate again. */
2188 if (sd.bptr->flags > BBREACHED) {
2189 /* This block is already finished. */
2191 superblockend = true;
2195 if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2196 /* This block is a clone and the original has not been */
2197 /* analysed, yet. Analyse it on the next iteration. */
2200 /* XXX superblockend? */
2204 /* This block has to be analysed now. */
2208 /* XXX The rest of this block is still indented one level too */
2209 /* much in order to avoid a giant diff by changing that. */
2211 /* We know that sd.bptr->flags == BBREACHED. */
2212 /* This block has been reached before. */
2214 assert(sd.bptr->flags == BBREACHED);
2215 stackdepth = sd.bptr->indepth;
2217 /* find exception handlers for this block */
2219 /* determine the active exception handlers for this block */
2220 /* XXX could use a faster algorithm with sorted lists or */
2223 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2226 ex = jd->exceptiontable;
2227 for (; ex != NULL; ex = ex->down) {
2228 if ((ex->start <= original) && (ex->end > original)) {
2229 sd.handlers[len++] = ex;
2232 sd.handlers[len] = NULL;
2235 /* reanalyse cloned block */
2237 if (sd.bptr->original) {
2238 if (!stack_reanalyse_block(&sd))
2243 /* reset the new pointer for allocating stackslots */
2247 /* create the instack of this block */
2249 curstack = stack_create_instack(&sd);
2251 /* initialize locals at the start of this block */
2253 if (sd.bptr->inlocals)
2254 MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2256 MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2258 /* set up local variables for analyzing this block */
2260 superblockend = false;
2261 len = sd.bptr->icount;
2262 iptr = sd.bptr->iinstr;
2264 /* mark the block as analysed */
2266 sd.bptr->flags = BBFINISHED;
2268 /* reset variables for dependency checking */
2270 coalescing_boundary = sd.new;
2271 for( i = 0; i < m->maxlocals; i++)
2272 last_store_boundary[i] = sd.new;
2274 /* remember the start of this block's variables */
2276 sd.bptr->varstart = sd.vartop;
2278 #if defined(STACK_VERBOSE)
2279 stack_verbose_block_enter(&sd, false);
2282 /* reach exception handlers for this block */
2284 if (!stack_reach_handlers(&sd))
2287 /* iterate over ICMDs ****************************************/
2289 while (--len >= 0) {
2291 #if defined(STACK_VERBOSE)
2292 stack_verbose_show_state(&sd, iptr, curstack);
2295 /* fetch the current opcode */
2299 /* automatically replace some ICMDs with builtins */
2301 #if defined(USEBUILTINTABLE)
2302 bte = builtintable_get_automatic(opcode);
2304 if (bte && bte->opcode == opcode) {
2305 iptr->opc = ICMD_BUILTIN;
2306 iptr->flags.bits &= INS_FLAG_ID_MASK;
2307 iptr->sx.s23.s3.bte = bte;
2308 /* iptr->line is already set */
2309 jd->isleafmethod = false;
2312 #endif /* defined(USEBUILTINTABLE) */
2314 /* main opcode switch *************************************/
2326 case ICMD_CHECKNULL:
2327 coalescing_boundary = sd.new;
2328 COUNT(count_check_null);
2331 iptr->dst.varindex = iptr->s1.varindex;
2335 j = iptr->s1.varindex =
2336 jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2338 #if defined(ENABLE_VERIFIER)
2339 if (sd.var[j].type != TYPE_RET) {
2340 exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2347 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2348 superblockend = true;
2352 COUNT(count_pcmd_return);
2355 superblockend = true;
2356 sd.jd->returncount++;
2357 sd.jd->returnblock = sd.bptr;
2361 /* pop 0 push 1 const */
2363 /************************** ICONST OPTIMIZATIONS **************************/
2366 COUNT(count_pcmd_load);
2370 switch (iptr[1].opc) {
2372 iptr->opc = ICMD_IADDCONST;
2376 iptr[1].opc = ICMD_NOP;
2377 OP1_1(TYPE_INT, TYPE_INT);
2378 COUNT(count_pcmd_op);
2382 iptr->opc = ICMD_ISUBCONST;
2383 goto icmd_iconst_tail;
2384 #if SUPPORT_CONST_MUL
2386 iptr->opc = ICMD_IMULCONST;
2387 goto icmd_iconst_tail;
2388 #else /* SUPPORT_CONST_MUL */
2390 if (iptr->sx.val.i == 0x00000002)
2392 else if (iptr->sx.val.i == 0x00000004)
2394 else if (iptr->sx.val.i == 0x00000008)
2396 else if (iptr->sx.val.i == 0x00000010)
2398 else if (iptr->sx.val.i == 0x00000020)
2400 else if (iptr->sx.val.i == 0x00000040)
2402 else if (iptr->sx.val.i == 0x00000080)
2404 else if (iptr->sx.val.i == 0x00000100)
2406 else if (iptr->sx.val.i == 0x00000200)
2408 else if (iptr->sx.val.i == 0x00000400)
2409 iptr->sx.val.i = 10;
2410 else if (iptr->sx.val.i == 0x00000800)
2411 iptr->sx.val.i = 11;
2412 else if (iptr->sx.val.i == 0x00001000)
2413 iptr->sx.val.i = 12;
2414 else if (iptr->sx.val.i == 0x00002000)
2415 iptr->sx.val.i = 13;
2416 else if (iptr->sx.val.i == 0x00004000)
2417 iptr->sx.val.i = 14;
2418 else if (iptr->sx.val.i == 0x00008000)
2419 iptr->sx.val.i = 15;
2420 else if (iptr->sx.val.i == 0x00010000)
2421 iptr->sx.val.i = 16;
2422 else if (iptr->sx.val.i == 0x00020000)
2423 iptr->sx.val.i = 17;
2424 else if (iptr->sx.val.i == 0x00040000)
2425 iptr->sx.val.i = 18;
2426 else if (iptr->sx.val.i == 0x00080000)
2427 iptr->sx.val.i = 19;
2428 else if (iptr->sx.val.i == 0x00100000)
2429 iptr->sx.val.i = 20;
2430 else if (iptr->sx.val.i == 0x00200000)
2431 iptr->sx.val.i = 21;
2432 else if (iptr->sx.val.i == 0x00400000)
2433 iptr->sx.val.i = 22;
2434 else if (iptr->sx.val.i == 0x00800000)
2435 iptr->sx.val.i = 23;
2436 else if (iptr->sx.val.i == 0x01000000)
2437 iptr->sx.val.i = 24;
2438 else if (iptr->sx.val.i == 0x02000000)
2439 iptr->sx.val.i = 25;
2440 else if (iptr->sx.val.i == 0x04000000)
2441 iptr->sx.val.i = 26;
2442 else if (iptr->sx.val.i == 0x08000000)
2443 iptr->sx.val.i = 27;
2444 else if (iptr->sx.val.i == 0x10000000)
2445 iptr->sx.val.i = 28;
2446 else if (iptr->sx.val.i == 0x20000000)
2447 iptr->sx.val.i = 29;
2448 else if (iptr->sx.val.i == 0x40000000)
2449 iptr->sx.val.i = 30;
2450 else if (iptr->sx.val.i == 0x80000000)
2451 iptr->sx.val.i = 31;
2455 iptr->opc = ICMD_IMULPOW2;
2456 goto icmd_iconst_tail;
2457 #endif /* SUPPORT_CONST_MUL */
2459 if (iptr->sx.val.i == 0x00000002)
2461 else if (iptr->sx.val.i == 0x00000004)
2463 else if (iptr->sx.val.i == 0x00000008)
2465 else if (iptr->sx.val.i == 0x00000010)
2467 else if (iptr->sx.val.i == 0x00000020)
2469 else if (iptr->sx.val.i == 0x00000040)
2471 else if (iptr->sx.val.i == 0x00000080)
2473 else if (iptr->sx.val.i == 0x00000100)
2475 else if (iptr->sx.val.i == 0x00000200)
2477 else if (iptr->sx.val.i == 0x00000400)
2478 iptr->sx.val.i = 10;
2479 else if (iptr->sx.val.i == 0x00000800)
2480 iptr->sx.val.i = 11;
2481 else if (iptr->sx.val.i == 0x00001000)
2482 iptr->sx.val.i = 12;
2483 else if (iptr->sx.val.i == 0x00002000)
2484 iptr->sx.val.i = 13;
2485 else if (iptr->sx.val.i == 0x00004000)
2486 iptr->sx.val.i = 14;
2487 else if (iptr->sx.val.i == 0x00008000)
2488 iptr->sx.val.i = 15;
2489 else if (iptr->sx.val.i == 0x00010000)
2490 iptr->sx.val.i = 16;
2491 else if (iptr->sx.val.i == 0x00020000)
2492 iptr->sx.val.i = 17;
2493 else if (iptr->sx.val.i == 0x00040000)
2494 iptr->sx.val.i = 18;
2495 else if (iptr->sx.val.i == 0x00080000)
2496 iptr->sx.val.i = 19;
2497 else if (iptr->sx.val.i == 0x00100000)
2498 iptr->sx.val.i = 20;
2499 else if (iptr->sx.val.i == 0x00200000)
2500 iptr->sx.val.i = 21;
2501 else if (iptr->sx.val.i == 0x00400000)
2502 iptr->sx.val.i = 22;
2503 else if (iptr->sx.val.i == 0x00800000)
2504 iptr->sx.val.i = 23;
2505 else if (iptr->sx.val.i == 0x01000000)
2506 iptr->sx.val.i = 24;
2507 else if (iptr->sx.val.i == 0x02000000)
2508 iptr->sx.val.i = 25;
2509 else if (iptr->sx.val.i == 0x04000000)
2510 iptr->sx.val.i = 26;
2511 else if (iptr->sx.val.i == 0x08000000)
2512 iptr->sx.val.i = 27;
2513 else if (iptr->sx.val.i == 0x10000000)
2514 iptr->sx.val.i = 28;
2515 else if (iptr->sx.val.i == 0x20000000)
2516 iptr->sx.val.i = 29;
2517 else if (iptr->sx.val.i == 0x40000000)
2518 iptr->sx.val.i = 30;
2519 else if (iptr->sx.val.i == 0x80000000)
2520 iptr->sx.val.i = 31;
2524 iptr->opc = ICMD_IDIVPOW2;
2525 goto icmd_iconst_tail;
2528 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2529 if ((iptr->sx.val.i == 0x00000002) ||
2530 (iptr->sx.val.i == 0x00000004) ||
2531 (iptr->sx.val.i == 0x00000008) ||
2532 (iptr->sx.val.i == 0x00000010) ||
2533 (iptr->sx.val.i == 0x00000020) ||
2534 (iptr->sx.val.i == 0x00000040) ||
2535 (iptr->sx.val.i == 0x00000080) ||
2536 (iptr->sx.val.i == 0x00000100) ||
2537 (iptr->sx.val.i == 0x00000200) ||
2538 (iptr->sx.val.i == 0x00000400) ||
2539 (iptr->sx.val.i == 0x00000800) ||
2540 (iptr->sx.val.i == 0x00001000) ||
2541 (iptr->sx.val.i == 0x00002000) ||
2542 (iptr->sx.val.i == 0x00004000) ||
2543 (iptr->sx.val.i == 0x00008000) ||
2544 (iptr->sx.val.i == 0x00010000) ||
2545 (iptr->sx.val.i == 0x00020000) ||
2546 (iptr->sx.val.i == 0x00040000) ||
2547 (iptr->sx.val.i == 0x00080000) ||
2548 (iptr->sx.val.i == 0x00100000) ||
2549 (iptr->sx.val.i == 0x00200000) ||
2550 (iptr->sx.val.i == 0x00400000) ||
2551 (iptr->sx.val.i == 0x00800000) ||
2552 (iptr->sx.val.i == 0x01000000) ||
2553 (iptr->sx.val.i == 0x02000000) ||
2554 (iptr->sx.val.i == 0x04000000) ||
2555 (iptr->sx.val.i == 0x08000000) ||
2556 (iptr->sx.val.i == 0x10000000) ||
2557 (iptr->sx.val.i == 0x20000000) ||
2558 (iptr->sx.val.i == 0x40000000) ||
2559 (iptr->sx.val.i == 0x80000000))
2561 iptr->opc = ICMD_IREMPOW2;
2562 iptr->sx.val.i -= 1;
2563 goto icmd_iconst_tail;
2566 #if SUPPORT_CONST_LOGICAL
2568 iptr->opc = ICMD_IANDCONST;
2569 goto icmd_iconst_tail;
2572 iptr->opc = ICMD_IORCONST;
2573 goto icmd_iconst_tail;
2576 iptr->opc = ICMD_IXORCONST;
2577 goto icmd_iconst_tail;
2579 #endif /* SUPPORT_CONST_LOGICAL */
2581 iptr->opc = ICMD_ISHLCONST;
2582 goto icmd_iconst_tail;
2585 iptr->opc = ICMD_ISHRCONST;
2586 goto icmd_iconst_tail;
2589 iptr->opc = ICMD_IUSHRCONST;
2590 goto icmd_iconst_tail;
2591 #if SUPPORT_LONG_SHIFT
2593 iptr->opc = ICMD_LSHLCONST;
2594 goto icmd_lconst_tail;
2597 iptr->opc = ICMD_LSHRCONST;
2598 goto icmd_lconst_tail;
2601 iptr->opc = ICMD_LUSHRCONST;
2602 goto icmd_lconst_tail;
2603 #endif /* SUPPORT_LONG_SHIFT */
2604 case ICMD_IF_ICMPEQ:
2605 iptr[1].opc = ICMD_IFEQ;
2609 /* set the constant for the following icmd */
2610 iptr[1].sx.val.i = iptr->sx.val.i;
2612 /* this instruction becomes a nop */
2613 iptr->opc = ICMD_NOP;
2616 case ICMD_IF_ICMPLT:
2617 iptr[1].opc = ICMD_IFLT;
2618 goto icmd_if_icmp_tail;
2620 case ICMD_IF_ICMPLE:
2621 iptr[1].opc = ICMD_IFLE;
2622 goto icmd_if_icmp_tail;
2624 case ICMD_IF_ICMPNE:
2625 iptr[1].opc = ICMD_IFNE;
2626 goto icmd_if_icmp_tail;
2628 case ICMD_IF_ICMPGT:
2629 iptr[1].opc = ICMD_IFGT;
2630 goto icmd_if_icmp_tail;
2632 case ICMD_IF_ICMPGE:
2633 iptr[1].opc = ICMD_IFGE;
2634 goto icmd_if_icmp_tail;
2636 #if SUPPORT_CONST_STORE
2641 # if SUPPORT_CONST_STORE_ZERO_ONLY
2642 if (iptr->sx.val.i != 0)
2645 switch (iptr[1].opc) {
2647 iptr->opc = ICMD_IASTORECONST;
2648 iptr->flags.bits |= INS_FLAG_CHECK;
2651 iptr->opc = ICMD_BASTORECONST;
2652 iptr->flags.bits |= INS_FLAG_CHECK;
2655 iptr->opc = ICMD_CASTORECONST;
2656 iptr->flags.bits |= INS_FLAG_CHECK;
2659 iptr->opc = ICMD_SASTORECONST;
2660 iptr->flags.bits |= INS_FLAG_CHECK;
2664 iptr[1].opc = ICMD_NOP;
2666 /* copy the constant to s3 */
2667 /* XXX constval -> astoreconstval? */
2668 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2669 OP2_0(TYPE_ADR, TYPE_INT);
2670 COUNT(count_pcmd_op);
2673 case ICMD_PUTSTATIC:
2675 # if SUPPORT_CONST_STORE_ZERO_ONLY
2676 if (iptr->sx.val.i != 0)
2679 /* XXX check field type? */
2681 /* copy the constant to s2 */
2682 /* XXX constval -> fieldconstval? */
2683 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2686 /* set the field reference (s3) */
2687 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2688 iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2689 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2690 fmiref = iptr->sx.s23.s3.uf->fieldref;
2693 fmiref = iptr[1].sx.s23.s3.fmiref;
2694 iptr->sx.s23.s3.fmiref = fmiref;
2697 #if defined(ENABLE_VERIFIER)
2698 expectedtype = fmiref->parseddesc.fd->type;
2699 switch (iptr[0].opc) {
2701 if (expectedtype != TYPE_INT)
2702 goto throw_stack_type_error;
2705 if (expectedtype != TYPE_LNG)
2706 goto throw_stack_type_error;
2709 if (expectedtype != TYPE_ADR)
2710 goto throw_stack_type_error;
2715 #endif /* defined(ENABLE_VERIFIER) */
2717 switch (iptr[1].opc) {
2718 case ICMD_PUTSTATIC:
2719 iptr->opc = ICMD_PUTSTATICCONST;
2723 iptr->opc = ICMD_PUTFIELDCONST;
2728 iptr[1].opc = ICMD_NOP;
2729 COUNT(count_pcmd_op);
2731 #endif /* SUPPORT_CONST_STORE */
2737 /* if we get here, the ICONST has been optimized */
2741 /* normal case of an unoptimized ICONST */
2745 /************************** LCONST OPTIMIZATIONS **************************/
2748 COUNT(count_pcmd_load);
2752 /* switch depending on the following instruction */
2754 switch (iptr[1].opc) {
2755 #if SUPPORT_LONG_ADD
2757 iptr->opc = ICMD_LADDCONST;
2761 /* instruction of type LONG -> LONG */
2762 iptr[1].opc = ICMD_NOP;
2763 OP1_1(TYPE_LNG, TYPE_LNG);
2764 COUNT(count_pcmd_op);
2768 iptr->opc = ICMD_LSUBCONST;
2769 goto icmd_lconst_tail;
2771 #endif /* SUPPORT_LONG_ADD */
2772 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2774 iptr->opc = ICMD_LMULCONST;
2775 goto icmd_lconst_tail;
2776 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2777 # if SUPPORT_LONG_SHIFT
2779 if (iptr->sx.val.l == 0x00000002)
2781 else if (iptr->sx.val.l == 0x00000004)
2783 else if (iptr->sx.val.l == 0x00000008)
2785 else if (iptr->sx.val.l == 0x00000010)
2787 else if (iptr->sx.val.l == 0x00000020)
2789 else if (iptr->sx.val.l == 0x00000040)
2791 else if (iptr->sx.val.l == 0x00000080)
2793 else if (iptr->sx.val.l == 0x00000100)
2795 else if (iptr->sx.val.l == 0x00000200)
2797 else if (iptr->sx.val.l == 0x00000400)
2798 iptr->sx.val.i = 10;
2799 else if (iptr->sx.val.l == 0x00000800)
2800 iptr->sx.val.i = 11;
2801 else if (iptr->sx.val.l == 0x00001000)
2802 iptr->sx.val.i = 12;
2803 else if (iptr->sx.val.l == 0x00002000)
2804 iptr->sx.val.i = 13;
2805 else if (iptr->sx.val.l == 0x00004000)
2806 iptr->sx.val.i = 14;
2807 else if (iptr->sx.val.l == 0x00008000)
2808 iptr->sx.val.i = 15;
2809 else if (iptr->sx.val.l == 0x00010000)
2810 iptr->sx.val.i = 16;
2811 else if (iptr->sx.val.l == 0x00020000)
2812 iptr->sx.val.i = 17;
2813 else if (iptr->sx.val.l == 0x00040000)
2814 iptr->sx.val.i = 18;
2815 else if (iptr->sx.val.l == 0x00080000)
2816 iptr->sx.val.i = 19;
2817 else if (iptr->sx.val.l == 0x00100000)
2818 iptr->sx.val.i = 20;
2819 else if (iptr->sx.val.l == 0x00200000)
2820 iptr->sx.val.i = 21;
2821 else if (iptr->sx.val.l == 0x00400000)
2822 iptr->sx.val.i = 22;
2823 else if (iptr->sx.val.l == 0x00800000)
2824 iptr->sx.val.i = 23;
2825 else if (iptr->sx.val.l == 0x01000000)
2826 iptr->sx.val.i = 24;
2827 else if (iptr->sx.val.l == 0x02000000)
2828 iptr->sx.val.i = 25;
2829 else if (iptr->sx.val.l == 0x04000000)
2830 iptr->sx.val.i = 26;
2831 else if (iptr->sx.val.l == 0x08000000)
2832 iptr->sx.val.i = 27;
2833 else if (iptr->sx.val.l == 0x10000000)
2834 iptr->sx.val.i = 28;
2835 else if (iptr->sx.val.l == 0x20000000)
2836 iptr->sx.val.i = 29;
2837 else if (iptr->sx.val.l == 0x40000000)
2838 iptr->sx.val.i = 30;
2839 else if (iptr->sx.val.l == 0x80000000)
2840 iptr->sx.val.i = 31;
2844 iptr->opc = ICMD_LMULPOW2;
2845 goto icmd_lconst_tail;
2846 # endif /* SUPPORT_LONG_SHIFT */
2847 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2848 #if SUPPORT_LONG_DIV_POW2
2850 if (iptr->sx.val.l == 0x00000002)
2852 else if (iptr->sx.val.l == 0x00000004)
2854 else if (iptr->sx.val.l == 0x00000008)
2856 else if (iptr->sx.val.l == 0x00000010)
2858 else if (iptr->sx.val.l == 0x00000020)
2860 else if (iptr->sx.val.l == 0x00000040)
2862 else if (iptr->sx.val.l == 0x00000080)
2864 else if (iptr->sx.val.l == 0x00000100)
2866 else if (iptr->sx.val.l == 0x00000200)
2868 else if (iptr->sx.val.l == 0x00000400)
2869 iptr->sx.val.i = 10;
2870 else if (iptr->sx.val.l == 0x00000800)
2871 iptr->sx.val.i = 11;
2872 else if (iptr->sx.val.l == 0x00001000)
2873 iptr->sx.val.i = 12;
2874 else if (iptr->sx.val.l == 0x00002000)
2875 iptr->sx.val.i = 13;
2876 else if (iptr->sx.val.l == 0x00004000)
2877 iptr->sx.val.i = 14;
2878 else if (iptr->sx.val.l == 0x00008000)
2879 iptr->sx.val.i = 15;
2880 else if (iptr->sx.val.l == 0x00010000)
2881 iptr->sx.val.i = 16;
2882 else if (iptr->sx.val.l == 0x00020000)
2883 iptr->sx.val.i = 17;
2884 else if (iptr->sx.val.l == 0x00040000)
2885 iptr->sx.val.i = 18;
2886 else if (iptr->sx.val.l == 0x00080000)
2887 iptr->sx.val.i = 19;
2888 else if (iptr->sx.val.l == 0x00100000)
2889 iptr->sx.val.i = 20;
2890 else if (iptr->sx.val.l == 0x00200000)
2891 iptr->sx.val.i = 21;
2892 else if (iptr->sx.val.l == 0x00400000)
2893 iptr->sx.val.i = 22;
2894 else if (iptr->sx.val.l == 0x00800000)
2895 iptr->sx.val.i = 23;
2896 else if (iptr->sx.val.l == 0x01000000)
2897 iptr->sx.val.i = 24;
2898 else if (iptr->sx.val.l == 0x02000000)
2899 iptr->sx.val.i = 25;
2900 else if (iptr->sx.val.l == 0x04000000)
2901 iptr->sx.val.i = 26;
2902 else if (iptr->sx.val.l == 0x08000000)
2903 iptr->sx.val.i = 27;
2904 else if (iptr->sx.val.l == 0x10000000)
2905 iptr->sx.val.i = 28;
2906 else if (iptr->sx.val.l == 0x20000000)
2907 iptr->sx.val.i = 29;
2908 else if (iptr->sx.val.l == 0x40000000)
2909 iptr->sx.val.i = 30;
2910 else if (iptr->sx.val.l == 0x80000000)
2911 iptr->sx.val.i = 31;
2915 iptr->opc = ICMD_LDIVPOW2;
2916 goto icmd_lconst_tail;
2917 #endif /* SUPPORT_LONG_DIV_POW2 */
2919 #if SUPPORT_LONG_REM_POW2
2921 if ((iptr->sx.val.l == 0x00000002) ||
2922 (iptr->sx.val.l == 0x00000004) ||
2923 (iptr->sx.val.l == 0x00000008) ||
2924 (iptr->sx.val.l == 0x00000010) ||
2925 (iptr->sx.val.l == 0x00000020) ||
2926 (iptr->sx.val.l == 0x00000040) ||
2927 (iptr->sx.val.l == 0x00000080) ||
2928 (iptr->sx.val.l == 0x00000100) ||
2929 (iptr->sx.val.l == 0x00000200) ||
2930 (iptr->sx.val.l == 0x00000400) ||
2931 (iptr->sx.val.l == 0x00000800) ||
2932 (iptr->sx.val.l == 0x00001000) ||
2933 (iptr->sx.val.l == 0x00002000) ||
2934 (iptr->sx.val.l == 0x00004000) ||
2935 (iptr->sx.val.l == 0x00008000) ||
2936 (iptr->sx.val.l == 0x00010000) ||
2937 (iptr->sx.val.l == 0x00020000) ||
2938 (iptr->sx.val.l == 0x00040000) ||
2939 (iptr->sx.val.l == 0x00080000) ||
2940 (iptr->sx.val.l == 0x00100000) ||
2941 (iptr->sx.val.l == 0x00200000) ||
2942 (iptr->sx.val.l == 0x00400000) ||
2943 (iptr->sx.val.l == 0x00800000) ||
2944 (iptr->sx.val.l == 0x01000000) ||
2945 (iptr->sx.val.l == 0x02000000) ||
2946 (iptr->sx.val.l == 0x04000000) ||
2947 (iptr->sx.val.l == 0x08000000) ||
2948 (iptr->sx.val.l == 0x10000000) ||
2949 (iptr->sx.val.l == 0x20000000) ||
2950 (iptr->sx.val.l == 0x40000000) ||
2951 (iptr->sx.val.l == 0x80000000))
2953 iptr->opc = ICMD_LREMPOW2;
2954 iptr->sx.val.l -= 1;
2955 goto icmd_lconst_tail;
2958 #endif /* SUPPORT_LONG_REM_POW2 */
2960 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2963 iptr->opc = ICMD_LANDCONST;
2964 goto icmd_lconst_tail;
2967 iptr->opc = ICMD_LORCONST;
2968 goto icmd_lconst_tail;
2971 iptr->opc = ICMD_LXORCONST;
2972 goto icmd_lconst_tail;
2973 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2975 #if SUPPORT_LONG_CMP_CONST
2977 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2980 /* switch on the instruction after LCONST - LCMP */
2982 switch (iptr[2].opc) {
2984 iptr->opc = ICMD_IF_LEQ;
2987 icmd_lconst_lcmp_tail:
2988 /* convert LCONST, LCMP, IFXX to IF_LXX */
2989 iptr->dst.insindex = iptr[2].dst.insindex;
2990 iptr[1].opc = ICMD_NOP;
2991 iptr[2].opc = ICMD_NOP;
2993 OP1_BRANCH(TYPE_LNG);
2995 COUNT(count_pcmd_bra);
2996 COUNT(count_pcmd_op);
3000 iptr->opc = ICMD_IF_LNE;
3001 goto icmd_lconst_lcmp_tail;
3004 iptr->opc = ICMD_IF_LLT;
3005 goto icmd_lconst_lcmp_tail;
3008 iptr->opc = ICMD_IF_LGT;
3009 goto icmd_lconst_lcmp_tail;
3012 iptr->opc = ICMD_IF_LLE;
3013 goto icmd_lconst_lcmp_tail;
3016 iptr->opc = ICMD_IF_LGE;
3017 goto icmd_lconst_lcmp_tail;
3021 } /* end switch on opcode after LCONST - LCMP */
3023 #endif /* SUPPORT_LONG_CMP_CONST */
3025 #if SUPPORT_CONST_STORE
3027 # if SUPPORT_CONST_STORE_ZERO_ONLY
3028 if (iptr->sx.val.l != 0)
3031 #if SIZEOF_VOID_P == 4
3032 /* the constant must fit into a ptrint */
3033 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3036 /* move the constant to s3 */
3037 iptr->sx.s23.s3.constval = iptr->sx.val.l;
3039 iptr->opc = ICMD_LASTORECONST;
3040 iptr->flags.bits |= INS_FLAG_CHECK;
3041 OP2_0(TYPE_ADR, TYPE_INT);
3043 iptr[1].opc = ICMD_NOP;
3044 COUNT(count_pcmd_op);
3047 case ICMD_PUTSTATIC:
3049 # if SUPPORT_CONST_STORE_ZERO_ONLY
3050 if (iptr->sx.val.l != 0)
3053 #if SIZEOF_VOID_P == 4
3054 /* the constant must fit into a ptrint */
3055 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3058 /* XXX check field type? */
3060 /* copy the constant to s2 */
3061 /* XXX constval -> fieldconstval? */
3062 iptr->sx.s23.s2.constval = iptr->sx.val.l;
3066 #endif /* SUPPORT_CONST_STORE */
3070 } /* end switch opcode after LCONST */
3072 /* if we get here, the LCONST has been optimized */
3076 /* the normal case of an unoptimized LCONST */
3080 /************************ END OF LCONST OPTIMIZATIONS *********************/
3083 COUNT(count_pcmd_load);
3088 COUNT(count_pcmd_load);
3092 /************************** ACONST OPTIMIZATIONS **************************/
3095 coalescing_boundary = sd.new;
3096 COUNT(count_pcmd_load);
3097 #if SUPPORT_CONST_STORE
3098 /* We can only optimize if the ACONST is resolved
3099 * and there is an instruction after it. */
3101 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3104 switch (iptr[1].opc) {
3106 /* We can only optimize for NULL values
3107 * here because otherwise a checkcast is
3109 if (iptr->sx.val.anyptr != NULL)
3112 /* copy the constant (NULL) to s3 */
3113 iptr->sx.s23.s3.constval = 0;
3114 iptr->opc = ICMD_AASTORECONST;
3115 iptr->flags.bits |= INS_FLAG_CHECK;
3116 OP2_0(TYPE_ADR, TYPE_INT);
3118 iptr[1].opc = ICMD_NOP;
3119 COUNT(count_pcmd_op);
3122 case ICMD_PUTSTATIC:
3124 # if SUPPORT_CONST_STORE_ZERO_ONLY
3125 if (iptr->sx.val.anyptr != NULL)
3128 /* XXX check field type? */
3129 /* copy the constant to s2 */
3130 /* XXX constval -> fieldconstval? */
3131 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3139 /* if we get here the ACONST has been optimized */
3143 #endif /* SUPPORT_CONST_STORE */
3148 /* pop 0 push 1 load */
3155 COUNT(count_load_instruction);
3156 i = opcode - ICMD_ILOAD; /* type */
3158 j = iptr->s1.varindex =
3159 jd->local_map[iptr->s1.varindex * 5 + i];
3161 #if defined(ENABLE_VERIFIER)
3162 if (sd.var[j].type == TYPE_RET) {
3163 exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3168 #if defined(ENABLE_SSA)
3170 GET_NEW_VAR(sd, new_index, i);
3187 coalescing_boundary = sd.new;
3188 iptr->flags.bits |= INS_FLAG_CHECK;
3189 COUNT(count_check_null);
3190 COUNT(count_check_bound);
3191 COUNT(count_pcmd_mem);
3192 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3199 coalescing_boundary = sd.new;
3200 iptr->flags.bits |= INS_FLAG_CHECK;
3201 COUNT(count_check_null);
3202 COUNT(count_check_bound);
3203 COUNT(count_pcmd_mem);
3204 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3207 /* pop 0 push 0 iinc */
3210 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3211 #if defined(ENABLE_SSA)
3214 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3218 last_store_boundary[iptr->s1.varindex] = sd.new;
3221 jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3226 if ((copy->varkind == LOCALVAR) &&
3227 (copy->varnum == iptr->s1.varindex))
3229 assert(IS_LOCALVAR(copy));
3235 #if defined(ENABLE_SSA)
3239 iptr->dst.varindex = iptr->s1.varindex;
3242 /* pop 1 push 0 store */
3251 i = opcode - ICMD_ISTORE; /* type */
3252 javaindex = iptr->dst.varindex;
3253 j = iptr->dst.varindex =
3254 jd->local_map[javaindex * 5 + i];
3256 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3258 iptr->sx.s23.s3.javaindex = javaindex;
3260 if (curstack->type == TYPE_RET) {
3261 iptr->flags.bits |= INS_FLAG_RETADDR;
3262 iptr->sx.s23.s2.retaddrnr =
3263 UNUSED - (1 + sd.var[j].vv.retaddr->nr);
3264 sd.javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
3267 sd.javalocals[javaindex] = j;
3269 /* invalidate the following javalocal for 2-word types */
3271 if (IS_2_WORD_TYPE(i)) {
3272 sd.javalocals[javaindex+1] = UNUSED;
3273 iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3276 /* invalidate 2-word types if second half was overwritten */
3278 if (javaindex > 0 && (i = sd.javalocals[javaindex-1]) != UNUSED) {
3279 if (IS_2_WORD_TYPE(sd.var[i].type)) {
3280 sd.javalocals[javaindex-1] = UNUSED;
3281 iptr->flags.bits |= INS_FLAG_KILL_PREV;
3285 #if defined(ENABLE_STATISTICS)
3288 i = sd.new - curstack;
3290 count_store_length[20]++;
3292 count_store_length[i]++;
3295 count_store_depth[10]++;
3297 count_store_depth[i]++;
3301 #if defined(ENABLE_SSA)
3304 /* check for conflicts as described in Figure 5.2 */
3306 copy = curstack->prev;
3309 if ((copy->varkind == LOCALVAR) &&
3310 (copy->varnum == j))
3312 copy->varkind = TEMPVAR;
3313 assert(IS_LOCALVAR(copy));
3320 /* if the variable is already coalesced, don't bother */
3322 /* We do not need to check against INOUT, as invars */
3323 /* are always before the coalescing boundary. */
3325 if (curstack->varkind == LOCALVAR)
3328 /* there is no STORE Lj while curstack is live */
3330 if (curstack < last_store_boundary[javaindex])
3331 goto assume_conflict;
3333 /* curstack must be after the coalescing boundary */
3335 if (curstack < coalescing_boundary)
3336 goto assume_conflict;
3338 /* there is no DEF LOCALVAR(j) while curstack is live */
3340 copy = sd.new; /* most recent stackslot created + 1 */
3341 while (--copy > curstack) {
3342 if (copy->varkind == LOCALVAR && copy->varnum == j)
3343 goto assume_conflict;
3346 /* coalesce the temporary variable with Lj */
3347 assert((curstack->varkind == TEMPVAR)
3348 || (curstack->varkind == UNDEFVAR));
3349 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3350 assert(!IS_INOUT(curstack));
3351 assert(!IS_PREALLOC(curstack));
3353 assert(curstack->creator);
3354 assert(curstack->creator->dst.varindex == curstack->varnum);
3355 assert(!(curstack->flags & PASSTHROUGH));
3356 RELEASE_INDEX(sd, curstack);
3357 curstack->varkind = LOCALVAR;
3358 curstack->varnum = j;
3359 curstack->creator->dst.varindex = j;
3362 /* revert the coalescing, if it has been done earlier */
3364 if ((curstack->varkind == LOCALVAR)
3365 && (curstack->varnum == j))
3367 assert(IS_LOCALVAR(curstack));
3368 SET_TEMPVAR(curstack);
3371 /* remember the stack boundary at this store */
3373 last_store_boundary[javaindex] = sd.new;
3374 #if defined(ENABLE_SSA)
3375 } /* if (ls != NULL) */
3378 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3381 STORE(opcode - ICMD_ISTORE, j);
3387 coalescing_boundary = sd.new;
3388 iptr->flags.bits |= INS_FLAG_CHECK;
3389 COUNT(count_check_null);
3390 COUNT(count_check_bound);
3391 COUNT(count_pcmd_mem);
3393 bte = builtintable_get_internal(BUILTIN_canstore);
3396 if (md->memuse > rd->memuse)
3397 rd->memuse = md->memuse;
3398 if (md->argintreguse > rd->argintreguse)
3399 rd->argintreguse = md->argintreguse;
3400 /* XXX non-leaf method? */
3402 /* make all stack variables saved */
3406 sd.var[copy->varnum].flags |= SAVEDVAR;
3407 /* in case copy->varnum is/will be a LOCALVAR */
3408 /* once and set back to a non LOCALVAR */
3409 /* the correct SAVEDVAR flag has to be */
3410 /* remembered in copy->flags, too */
3411 copy->flags |= SAVEDVAR;
3415 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3422 coalescing_boundary = sd.new;
3423 iptr->flags.bits |= INS_FLAG_CHECK;
3424 COUNT(count_check_null);
3425 COUNT(count_check_bound);
3426 COUNT(count_pcmd_mem);
3427 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3434 coalescing_boundary = sd.new;
3435 iptr->flags.bits |= INS_FLAG_CHECK;
3436 COUNT(count_check_null);
3437 COUNT(count_check_bound);
3438 COUNT(count_pcmd_mem);
3439 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3445 #ifdef ENABLE_VERIFIER
3448 if (IS_2_WORD_TYPE(curstack->type))
3449 goto throw_stack_category_error;
3460 coalescing_boundary = sd.new;
3461 /* Assert here that no LOCAL or INOUTS get */
3462 /* preallocated, since tha macros are not */
3463 /* available in md-abi.c! */
3464 if (IS_TEMPVAR(curstack))
3465 md_return_alloc(jd, curstack);
3466 COUNT(count_pcmd_return);
3467 OP1_0(opcode - ICMD_IRETURN);
3468 superblockend = true;
3469 sd.jd->returncount++;
3470 sd.jd->returnblock = sd.bptr;
3474 coalescing_boundary = sd.new;
3475 COUNT(count_check_null);
3477 curstack = NULL; stackdepth = 0;
3478 superblockend = true;
3481 case ICMD_PUTSTATIC:
3482 coalescing_boundary = sd.new;
3483 COUNT(count_pcmd_mem);
3484 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3485 OP1_0(fmiref->parseddesc.fd->type);
3488 /* pop 1 push 0 branch */
3491 case ICMD_IFNONNULL:
3492 COUNT(count_pcmd_bra);
3493 OP1_BRANCH(TYPE_ADR);
3503 COUNT(count_pcmd_bra);
3504 /* iptr->sx.val.i is set implicitly in parse by
3505 clearing the memory or from IF_ICMPxx
3508 OP1_BRANCH(TYPE_INT);
3509 /* iptr->sx.val.i = 0; */
3513 /* pop 0 push 0 branch */
3516 COUNT(count_pcmd_bra);
3519 superblockend = true;
3522 /* pop 1 push 0 table branch */
3524 case ICMD_TABLESWITCH:
3525 COUNT(count_pcmd_table);
3526 OP1_BRANCH(TYPE_INT);
3528 table = iptr->dst.table;
3529 BRANCH_TARGET(*table, tbptr);
3532 i = iptr->sx.s23.s3.tablehigh
3533 - iptr->sx.s23.s2.tablelow + 1;
3536 BRANCH_TARGET(*table, tbptr);
3539 superblockend = true;
3542 /* pop 1 push 0 table branch */
3544 case ICMD_LOOKUPSWITCH:
3545 COUNT(count_pcmd_table);
3546 OP1_BRANCH(TYPE_INT);
3548 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3550 lookup = iptr->dst.lookup;
3552 i = iptr->sx.s23.s2.lookupcount;
3555 BRANCH_TARGET(lookup->target, tbptr);
3558 superblockend = true;
3561 case ICMD_MONITORENTER:
3562 case ICMD_MONITOREXIT:
3563 coalescing_boundary = sd.new;
3564 COUNT(count_check_null);
3568 /* pop 2 push 0 branch */
3570 case ICMD_IF_ICMPEQ:
3571 case ICMD_IF_ICMPNE:
3572 case ICMD_IF_ICMPLT:
3573 case ICMD_IF_ICMPGE:
3574 case ICMD_IF_ICMPGT:
3575 case ICMD_IF_ICMPLE:
3576 COUNT(count_pcmd_bra);
3577 OP2_BRANCH(TYPE_INT, TYPE_INT);
3581 case ICMD_IF_ACMPEQ:
3582 case ICMD_IF_ACMPNE:
3583 COUNT(count_pcmd_bra);
3584 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3591 coalescing_boundary = sd.new;
3592 COUNT(count_check_null);
3593 COUNT(count_pcmd_mem);
3594 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3595 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3600 if (!IS_2_WORD_TYPE(curstack->type)) {
3602 #ifdef ENABLE_VERIFIER
3605 if (IS_2_WORD_TYPE(curstack->prev->type))
3606 goto throw_stack_category_error;
3609 OP2_0_ANY_ANY; /* pop two slots */
3612 iptr->opc = ICMD_POP;
3613 OP1_0_ANY; /* pop one (two-word) slot */
3617 /* pop 0 push 1 dup */
3620 #ifdef ENABLE_VERIFIER
3623 if (IS_2_WORD_TYPE(curstack->type))
3624 goto throw_stack_category_error;
3627 COUNT(count_dup_instruction);
3633 coalescing_boundary = sd.new - 1;
3638 if (IS_2_WORD_TYPE(curstack->type)) {
3640 iptr->opc = ICMD_DUP;
3645 /* ..., ????, cat1 */
3646 #ifdef ENABLE_VERIFIER
3648 if (IS_2_WORD_TYPE(curstack->prev->type))
3649 goto throw_stack_category_error;
3652 src1 = curstack->prev;
3655 COPY_UP(src1); iptr++; len--;
3658 coalescing_boundary = sd.new;
3662 /* pop 2 push 3 dup */
3665 #ifdef ENABLE_VERIFIER
3668 if (IS_2_WORD_TYPE(curstack->type) ||
3669 IS_2_WORD_TYPE(curstack->prev->type))
3670 goto throw_stack_category_error;
3675 src1 = curstack->prev;
3680 /* move non-temporary sources out of the way */
3681 if (!IS_TEMPVAR(src2)) {
3682 MOVE_TO_TEMP(src2); iptr++; len--;
3685 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3687 MOVE_UP(src1); iptr++; len--;
3688 MOVE_UP(src2); iptr++; len--;
3690 COPY_DOWN(curstack, dst1);
3692 coalescing_boundary = sd.new;
3697 if (IS_2_WORD_TYPE(curstack->type)) {
3698 /* ..., ????, cat2 */
3699 #ifdef ENABLE_VERIFIER
3701 if (IS_2_WORD_TYPE(curstack->prev->type))
3702 goto throw_stack_category_error;
3705 iptr->opc = ICMD_DUP_X1;
3709 /* ..., ????, cat1 */
3710 #ifdef ENABLE_VERIFIER
3713 if (IS_2_WORD_TYPE(curstack->prev->type)
3714 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3715 goto throw_stack_category_error;
3720 src1 = curstack->prev->prev;
3721 src2 = curstack->prev;
3723 POPANY; POPANY; POPANY;
3726 /* move non-temporary sources out of the way */
3727 if (!IS_TEMPVAR(src2)) {
3728 MOVE_TO_TEMP(src2); iptr++; len--;
3730 if (!IS_TEMPVAR(src3)) {
3731 MOVE_TO_TEMP(src3); iptr++; len--;
3734 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3735 DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3737 MOVE_UP(src1); iptr++; len--;
3738 MOVE_UP(src2); iptr++; len--;
3739 MOVE_UP(src3); iptr++; len--;
3741 COPY_DOWN(curstack, dst2); iptr++; len--;
3742 COPY_DOWN(curstack->prev, dst1);
3744 coalescing_boundary = sd.new;
3748 /* pop 3 push 4 dup */
3752 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3753 /* ..., cat2, ???? */
3754 #ifdef ENABLE_VERIFIER
3756 if (IS_2_WORD_TYPE(curstack->type))
3757 goto throw_stack_category_error;
3760 iptr->opc = ICMD_DUP_X1;
3764 /* ..., cat1, ???? */
3765 #ifdef ENABLE_VERIFIER
3768 if (IS_2_WORD_TYPE(curstack->type)
3769 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3770 goto throw_stack_category_error;
3774 src1 = curstack->prev->prev;
3775 src2 = curstack->prev;
3777 POPANY; POPANY; POPANY;
3780 /* move non-temporary sources out of the way */
3781 if (!IS_TEMPVAR(src2)) {
3782 MOVE_TO_TEMP(src2); iptr++; len--;
3784 if (!IS_TEMPVAR(src3)) {
3785 MOVE_TO_TEMP(src3); iptr++; len--;
3788 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3790 MOVE_UP(src1); iptr++; len--;
3791 MOVE_UP(src2); iptr++; len--;
3792 MOVE_UP(src3); iptr++; len--;
3794 COPY_DOWN(curstack, dst1);
3796 coalescing_boundary = sd.new;
3802 if (IS_2_WORD_TYPE(curstack->type)) {
3803 /* ..., ????, cat2 */
3804 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3805 /* ..., cat2, cat2 */
3806 iptr->opc = ICMD_DUP_X1;
3810 /* ..., cat1, cat2 */
3811 #ifdef ENABLE_VERIFIER
3814 if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3815 goto throw_stack_category_error;
3818 iptr->opc = ICMD_DUP_X2;
3824 /* ..., ????, ????, cat1 */
3826 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3827 /* ..., cat2, ????, cat1 */
3828 #ifdef ENABLE_VERIFIER
3830 if (IS_2_WORD_TYPE(curstack->prev->type))
3831 goto throw_stack_category_error;
3834 iptr->opc = ICMD_DUP2_X1;
3838 /* ..., cat1, ????, cat1 */
3839 #ifdef ENABLE_VERIFIER
3842 if (IS_2_WORD_TYPE(curstack->prev->type)
3843 || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3844 goto throw_stack_category_error;
3848 src1 = curstack->prev->prev->prev;
3849 src2 = curstack->prev->prev;
3850 src3 = curstack->prev;
3852 POPANY; POPANY; POPANY; POPANY;
3855 /* move non-temporary sources out of the way */
3856 if (!IS_TEMPVAR(src2)) {
3857 MOVE_TO_TEMP(src2); iptr++; len--;
3859 if (!IS_TEMPVAR(src3)) {
3860 MOVE_TO_TEMP(src3); iptr++; len--;
3862 if (!IS_TEMPVAR(src4)) {
3863 MOVE_TO_TEMP(src4); iptr++; len--;
3866 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3867 DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3869 MOVE_UP(src1); iptr++; len--;
3870 MOVE_UP(src2); iptr++; len--;
3871 MOVE_UP(src3); iptr++; len--;
3872 MOVE_UP(src4); iptr++; len--;
3874 COPY_DOWN(curstack, dst2); iptr++; len--;
3875 COPY_DOWN(curstack->prev, dst1);
3877 coalescing_boundary = sd.new;
3881 /* pop 2 push 2 swap */
3884 #ifdef ENABLE_VERIFIER
3887 if (IS_2_WORD_TYPE(curstack->type)
3888 || IS_2_WORD_TYPE(curstack->prev->type))
3889 goto throw_stack_category_error;
3893 src1 = curstack->prev;
3898 /* move non-temporary sources out of the way */
3899 if (!IS_TEMPVAR(src1)) {
3900 MOVE_TO_TEMP(src1); iptr++; len--;
3903 MOVE_UP(src2); iptr++; len--;
3906 coalescing_boundary = sd.new;
3913 coalescing_boundary = sd.new;
3914 #if !SUPPORT_DIVISION
3915 bte = iptr->sx.s23.s3.bte;
3918 if (md->memuse > rd->memuse)
3919 rd->memuse = md->memuse;
3920 if (md->argintreguse > rd->argintreguse)
3921 rd->argintreguse = md->argintreguse;
3923 /* make all stack variables saved */
3927 sd.var[copy->varnum].flags |= SAVEDVAR;
3928 copy->flags |= SAVEDVAR;
3933 #endif /* !SUPPORT_DIVISION */
3944 COUNT(count_pcmd_op);
3945 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3950 coalescing_boundary = sd.new;
3951 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3952 bte = iptr->sx.s23.s3.bte;
3955 if (md->memuse > rd->memuse)
3956 rd->memuse = md->memuse;
3957 if (md->argintreguse > rd->argintreguse)
3958 rd->argintreguse = md->argintreguse;
3959 /* XXX non-leaf method? */
3961 /* make all stack variables saved */
3965 sd.var[copy->varnum].flags |= SAVEDVAR;
3966 copy->flags |= SAVEDVAR;
3971 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3976 #if SUPPORT_LONG_LOGICAL
3980 #endif /* SUPPORT_LONG_LOGICAL */
3981 COUNT(count_pcmd_op);
3982 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3988 COUNT(count_pcmd_op);
3989 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3997 COUNT(count_pcmd_op);
3998 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
4006 COUNT(count_pcmd_op);
4007 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
4011 COUNT(count_pcmd_op);
4012 #if SUPPORT_LONG_CMP_CONST
4013 if ((len == 0) || (iptr[1].sx.val.i != 0))
4016 switch (iptr[1].opc) {
4018 iptr->opc = ICMD_IF_LCMPEQ;
4020 iptr->dst.insindex = iptr[1].dst.insindex;
4021 iptr[1].opc = ICMD_NOP;
4023 OP2_BRANCH(TYPE_LNG, TYPE_LNG);
4026 COUNT(count_pcmd_bra);
4029 iptr->opc = ICMD_IF_LCMPNE;
4030 goto icmd_lcmp_if_tail;
4032 iptr->opc = ICMD_IF_LCMPLT;
4033 goto icmd_lcmp_if_tail;
4035 iptr->opc = ICMD_IF_LCMPGT;
4036 goto icmd_lcmp_if_tail;
4038 iptr->opc = ICMD_IF_LCMPLE;
4039 goto icmd_lcmp_if_tail;
4041 iptr->opc = ICMD_IF_LCMPGE;
4042 goto icmd_lcmp_if_tail;
4048 #endif /* SUPPORT_LONG_CMP_CONST */
4049 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
4052 /* XXX why is this deactivated? */
4055 COUNT(count_pcmd_op);
4056 if ((len == 0) || (iptr[1].sx.val.i != 0))
4059 switch (iptr[1].opc) {
4061 iptr->opc = ICMD_IF_FCMPEQ;
4063 iptr->dst.insindex = iptr[1].dst.insindex;
4064 iptr[1].opc = ICMD_NOP;
4066 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4069 COUNT(count_pcmd_bra);
4072 iptr->opc = ICMD_IF_FCMPNE;
4073 goto icmd_if_fcmpl_tail;
4075 iptr->opc = ICMD_IF_FCMPL_LT;
4076 goto icmd_if_fcmpl_tail;
4078 iptr->opc = ICMD_IF_FCMPL_GT;
4079 goto icmd_if_fcmpl_tail;
4081 iptr->opc = ICMD_IF_FCMPL_LE;
4082 goto icmd_if_fcmpl_tail;
4084 iptr->opc = ICMD_IF_FCMPL_GE;
4085 goto icmd_if_fcmpl_tail;
4092 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4096 COUNT(count_pcmd_op);
4097 if ((len == 0) || (iptr[1].sx.val.i != 0))
4100 switch (iptr[1].opc) {
4102 iptr->opc = ICMD_IF_FCMPEQ;
4104 iptr->dst.insindex = iptr[1].dst.insindex;
4105 iptr[1].opc = ICMD_NOP;
4107 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4110 COUNT(count_pcmd_bra);
4113 iptr->opc = ICMD_IF_FCMPNE;
4114 goto icmd_if_fcmpg_tail;
4116 iptr->opc = ICMD_IF_FCMPG_LT;
4117 goto icmd_if_fcmpg_tail;
4119 iptr->opc = ICMD_IF_FCMPG_GT;
4120 goto icmd_if_fcmpg_tail;
4122 iptr->opc = ICMD_IF_FCMPG_LE;
4123 goto icmd_if_fcmpg_tail;
4125 iptr->opc = ICMD_IF_FCMPG_GE;
4126 goto icmd_if_fcmpg_tail;
4133 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4137 COUNT(count_pcmd_op);
4138 if ((len == 0) || (iptr[1].sx.val.i != 0))
4141 switch (iptr[1].opc) {
4143 iptr->opc = ICMD_IF_DCMPEQ;
4145 iptr->dst.insindex = iptr[1].dst.insindex;
4146 iptr[1].opc = ICMD_NOP;
4148 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4151 COUNT(count_pcmd_bra);
4154 iptr->opc = ICMD_IF_DCMPNE;
4155 goto icmd_if_dcmpl_tail;
4157 iptr->opc = ICMD_IF_DCMPL_LT;
4158 goto icmd_if_dcmpl_tail;
4160 iptr->opc = ICMD_IF_DCMPL_GT;
4161 goto icmd_if_dcmpl_tail;
4163 iptr->opc = ICMD_IF_DCMPL_LE;
4164 goto icmd_if_dcmpl_tail;
4166 iptr->opc = ICMD_IF_DCMPL_GE;
4167 goto icmd_if_dcmpl_tail;
4174 OPTT2_1(TYPE_DBL, TYPE_INT);
4178 COUNT(count_pcmd_op);
4179 if ((len == 0) || (iptr[1].sx.val.i != 0))
4182 switch (iptr[1].opc) {
4184 iptr->opc = ICMD_IF_DCMPEQ;
4186 iptr->dst.insindex = iptr[1].dst.insindex;
4187 iptr[1].opc = ICMD_NOP;
4189 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4192 COUNT(count_pcmd_bra);
4195 iptr->opc = ICMD_IF_DCMPNE;
4196 goto icmd_if_dcmpg_tail;
4198 iptr->opc = ICMD_IF_DCMPG_LT;
4199 goto icmd_if_dcmpg_tail;
4201 iptr->opc = ICMD_IF_DCMPG_GT;
4202 goto icmd_if_dcmpg_tail;
4204 iptr->opc = ICMD_IF_DCMPG_LE;
4205 goto icmd_if_dcmpg_tail;
4207 iptr->opc = ICMD_IF_DCMPG_GE;
4208 goto icmd_if_dcmpg_tail;
4215 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4220 COUNT(count_pcmd_op);
4221 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4226 COUNT(count_pcmd_op);
4227 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4236 case ICMD_INT2SHORT:
4237 COUNT(count_pcmd_op);
4238 OP1_1(TYPE_INT, TYPE_INT);
4241 COUNT(count_pcmd_op);
4242 OP1_1(TYPE_LNG, TYPE_LNG);
4245 COUNT(count_pcmd_op);
4246 OP1_1(TYPE_FLT, TYPE_FLT);
4249 COUNT(count_pcmd_op);
4250 OP1_1(TYPE_DBL, TYPE_DBL);
4254 COUNT(count_pcmd_op);
4255 OP1_1(TYPE_INT, TYPE_LNG);
4258 COUNT(count_pcmd_op);
4259 OP1_1(TYPE_INT, TYPE_FLT);
4262 COUNT(count_pcmd_op);
4263 OP1_1(TYPE_INT, TYPE_DBL);
4266 COUNT(count_pcmd_op);
4267 OP1_1(TYPE_LNG, TYPE_INT);
4270 COUNT(count_pcmd_op);
4271 OP1_1(TYPE_LNG, TYPE_FLT);
4274 COUNT(count_pcmd_op);
4275 OP1_1(TYPE_LNG, TYPE_DBL);
4278 COUNT(count_pcmd_op);
4279 OP1_1(TYPE_FLT, TYPE_INT);
4282 COUNT(count_pcmd_op);
4283 OP1_1(TYPE_FLT, TYPE_LNG);
4286 COUNT(count_pcmd_op);
4287 OP1_1(TYPE_FLT, TYPE_DBL);
4290 COUNT(count_pcmd_op);
4291 OP1_1(TYPE_DBL, TYPE_INT);
4294 COUNT(count_pcmd_op);
4295 OP1_1(TYPE_DBL, TYPE_LNG);
4298 COUNT(count_pcmd_op);
4299 OP1_1(TYPE_DBL, TYPE_FLT);
4302 case ICMD_CHECKCAST:
4303 coalescing_boundary = sd.new;
4304 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4305 /* array type cast-check */
4307 bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4310 if (md->memuse > rd->memuse)
4311 rd->memuse = md->memuse;
4312 if (md->argintreguse > rd->argintreguse)
4313 rd->argintreguse = md->argintreguse;
4315 /* make all stack variables saved */
4319 sd.var[copy->varnum].flags |= SAVEDVAR;
4320 copy->flags |= SAVEDVAR;
4324 OP1_1(TYPE_ADR, TYPE_ADR);
4327 case ICMD_INSTANCEOF:
4328 case ICMD_ARRAYLENGTH:
4329 coalescing_boundary = sd.new;
4330 OP1_1(TYPE_ADR, TYPE_INT);
4334 case ICMD_ANEWARRAY:
4335 coalescing_boundary = sd.new;
4336 OP1_1(TYPE_INT, TYPE_ADR);
4340 coalescing_boundary = sd.new;
4341 COUNT(count_check_null);
4342 COUNT(count_pcmd_mem);
4343 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4344 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4349 case ICMD_GETSTATIC:
4350 coalescing_boundary = sd.new;
4351 COUNT(count_pcmd_mem);
4352 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4353 OP0_1(fmiref->parseddesc.fd->type);
4357 coalescing_boundary = sd.new;
4364 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
4365 tbptr->type = BBTYPE_SBR;
4367 assert(sd.bptr->next); /* XXX exception */
4368 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4369 #if defined(ENABLE_VERIFIER)
4370 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4373 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4377 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4379 /* We need to check for overflow right here because
4380 * the pushed value is poped afterwards */
4383 superblockend = true;
4384 /* XXX should not be marked as interface, as it does not need to be */
4385 /* allocated. Same for the invar of the target. */
4388 /* pop many push any */
4392 bte = iptr->sx.s23.s3.bte;
4396 case ICMD_INVOKESTATIC:
4397 case ICMD_INVOKESPECIAL:
4398 case ICMD_INVOKEVIRTUAL:
4399 case ICMD_INVOKEINTERFACE:
4400 COUNT(count_pcmd_met);
4402 /* Check for functions to replace with builtin
4405 if (builtintable_replace_function(iptr))
4408 INSTRUCTION_GET_METHODDESC(iptr, md);
4409 /* XXX resurrect this COUNT? */
4410 /* if (lm->flags & ACC_STATIC) */
4411 /* {COUNT(count_check_null);} */
4415 coalescing_boundary = sd.new;
4419 if (md->memuse > rd->memuse)
4420 rd->memuse = md->memuse;
4421 if (md->argintreguse > rd->argintreguse)
4422 rd->argintreguse = md->argintreguse;
4423 if (md->argfltreguse > rd->argfltreguse)
4424 rd->argfltreguse = md->argfltreguse;
4428 iptr->s1.argcount = stackdepth;
4429 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4432 for (i-- ; i >= 0; i--) {
4433 iptr->sx.s23.s2.args[i] = copy->varnum;
4435 /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4436 /* -> won't help anyway */
4437 if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4439 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4440 /* If we pass float arguments in integer argument registers, we
4441 * are not allowed to precolor them here. Floats have to be moved
4442 * to this regs explicitly in codegen().
4443 * Only arguments that are passed by stack anyway can be precolored
4444 * (michi 2005/07/24) */
4445 if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4446 (!IS_FLT_DBL_TYPE(copy->type)
4447 || md->params[i].inmemory)) {
4449 if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4454 if (md->params[i].inmemory) {
4455 sd.var[copy->varnum].vv.regoff =
4456 md->params[i].regoff;
4457 sd.var[copy->varnum].flags |=
4461 if (IS_FLT_DBL_TYPE(copy->type)) {
4462 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4463 assert(0); /* XXX is this assert ok? */
4465 sd.var[copy->varnum].vv.regoff =
4466 rd->argfltregs[md->params[i].regoff];
4467 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4470 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4471 if (IS_2_WORD_TYPE(copy->type))
4472 sd.var[copy->varnum].vv.regoff =
4473 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4474 rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4477 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4478 sd.var[copy->varnum].vv.regoff =
4479 rd->argintregs[md->params[i].regoff];
4487 /* deal with live-through stack slots "under" the */
4493 iptr->sx.s23.s2.args[i++] = copy->varnum;
4494 sd.var[copy->varnum].flags |= SAVEDVAR;
4495 copy->flags |= SAVEDVAR | PASSTHROUGH;
4499 /* pop the arguments */
4508 /* push the return value */
4510 if (md->returntype.type != TYPE_VOID) {
4511 GET_NEW_VAR(sd, new_index, md->returntype.type);
4512 DST(md->returntype.type, new_index);
4517 case ICMD_MULTIANEWARRAY:
4518 coalescing_boundary = sd.new;
4519 if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4520 rd->argintreguse = MIN(3, INT_ARG_CNT);
4522 i = iptr->s1.argcount;
4526 iptr->sx.s23.s2.args = DMNEW(s4, i);
4528 #if defined(SPECIALMEMUSE)
4529 # if defined(__DARWIN__)
4530 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4531 rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4533 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4534 rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4537 # if defined(__I386__)
4538 if (rd->memuse < i + 3)
4539 rd->memuse = i + 3; /* n integer args spilled on stack */
4540 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4541 if (rd->memuse < i + 2)
4542 rd->memuse = i + 2; /* 4*4 bytes callee save space */
4545 rd->memuse = i; /* n integer args spilled on stack */
4546 # endif /* defined(__I386__) */
4550 /* check INT type here? Currently typecheck does this. */
4551 iptr->sx.s23.s2.args[i] = copy->varnum;
4552 if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4553 && (!IS_INOUT(copy))
4554 && (!IS_LOCALVAR(copy)) ) {
4555 copy->varkind = ARGVAR;
4556 sd.var[copy->varnum].flags |=
4557 INMEMORY & PREALLOC;
4558 #if defined(SPECIALMEMUSE)
4559 # if defined(__DARWIN__)
4560 sd.var[copy->varnum].vv.regoff = i +
4561 LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4563 sd.var[copy->varnum].vv.regoff = i +
4564 LA_SIZE_IN_POINTERS + 3;
4567 # if defined(__I386__)
4568 sd.var[copy->varnum].vv.regoff = i + 3;
4569 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4570 sd.var[copy->varnum].vv.regoff = i + 2;
4572 sd.var[copy->varnum].vv.regoff = i;
4573 # endif /* defined(__I386__) */
4574 #endif /* defined(SPECIALMEMUSE) */
4579 sd.var[copy->varnum].flags |= SAVEDVAR;
4580 copy->flags |= SAVEDVAR;
4584 i = iptr->s1.argcount;
4589 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4590 DST(TYPE_ADR, new_index);
4596 new_internalerror("Unknown ICMD %d", opcode);
4602 } /* while instructions */
4604 /* show state after last instruction */
4606 #if defined(STACK_VERBOSE)
4607 stack_verbose_show_state(&sd, NULL, curstack);
4610 /* stack slots at basic block end become interfaces */
4612 sd.bptr->outdepth = stackdepth;
4613 sd.bptr->outvars = DMNEW(s4, stackdepth);
4616 for (copy = curstack; copy; i--, copy = copy->prev) {
4620 /* with the new vars rd->interfaces will be removed */
4621 /* and all in and outvars have to be STACKVARS! */
4622 /* in the moment i.e. SWAP with in and out vars can */
4623 /* create an unresolvable conflict */
4628 v = sd.var + copy->varnum;
4631 /* do not allocate variables for returnAddresses */
4633 if (t != TYPE_RET) {
4634 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4635 /* no interface var until now for this depth and */
4637 jd->interface_map[i*5 + t].flags = v->flags;
4640 jd->interface_map[i*5 + t].flags |= v->flags;
4644 sd.bptr->outvars[i] = copy->varnum;
4647 /* check if interface slots at basic block begin must be saved */
4649 for (i=0; i<sd.bptr->indepth; ++i) {
4650 varinfo *v = sd.var + sd.bptr->invars[i];
4655 if (t != TYPE_RET) {
4656 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4657 /* no interface var until now for this depth and */
4659 jd->interface_map[i*5 + t].flags = v->flags;
4662 jd->interface_map[i*5 + t].flags |= v->flags;
4667 /* store the number of this block's variables */
4669 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4671 #if defined(STACK_VERBOSE)
4672 stack_verbose_block_exit(&sd, superblockend);
4675 /* reach the following block, if any */
4678 if (!stack_reach_next_block(&sd))
4683 } while (sd.repeat && !deadcode);
4685 /* reset locals of TYPE_RET to TYPE_ADR */
4687 for (i=0; i<sd.localcount; ++i) {
4688 if (sd.var[i].type == TYPE_RET)
4689 sd.var[i].type = TYPE_ADR;
4692 /* mark temporaries of TYPE_RET as PREALLOC to avoid allocation */
4694 for (i=sd.localcount; i<sd.vartop; ++i) {
4695 if (sd.var[i].type == TYPE_RET)
4696 sd.var[i].flags |= PREALLOC;
4699 /* XXX hack to fix up the ranges of the cloned single-block handlers */
4701 ex = jd->exceptiontable;
4702 for (; ex != NULL; ex = ex->down) {
4703 if (ex->start == ex->end) {
4704 assert(ex->end->next);
4705 ex->end = ex->end->next;
4709 /* store number of created variables */
4711 jd->vartop = sd.vartop;
4713 /* gather statistics *****************************************************/
4715 #if defined(ENABLE_STATISTICS)
4717 if (jd->basicblockcount > count_max_basic_blocks)
4718 count_max_basic_blocks = jd->basicblockcount;
4719 count_basic_blocks += jd->basicblockcount;
4720 if (jd->instructioncount > count_max_javainstr)
4721 count_max_javainstr = jd->instructioncount;
4722 count_javainstr += jd->instructioncount;
4723 if (jd->stackcount > count_upper_bound_new_stack)
4724 count_upper_bound_new_stack = jd->stackcount;
4725 if ((sd.new - jd->stack) > count_max_new_stack)
4726 count_max_new_stack = (sd.new - jd->stack);
4728 sd.bptr = jd->basicblocks;
4729 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4730 if (sd.bptr->flags > BBREACHED) {
4731 if (sd.bptr->indepth >= 10)
4732 count_block_stack[10]++;
4734 count_block_stack[sd.bptr->indepth]++;
4735 len = sd.bptr->icount;
4737 count_block_size_distribution[len]++;
4739 count_block_size_distribution[10]++;
4741 count_block_size_distribution[11]++;
4743 count_block_size_distribution[12]++;
4745 count_block_size_distribution[13]++;
4747 count_block_size_distribution[14]++;
4749 count_block_size_distribution[15]++;
4751 count_block_size_distribution[16]++;
4753 count_block_size_distribution[17]++;
4757 if (iteration_count == 1)
4758 count_analyse_iterations[0]++;
4759 else if (iteration_count == 2)
4760 count_analyse_iterations[1]++;
4761 else if (iteration_count == 3)
4762 count_analyse_iterations[2]++;
4763 else if (iteration_count == 4)
4764 count_analyse_iterations[3]++;
4766 count_analyse_iterations[4]++;
4768 if (jd->basicblockcount <= 5)
4769 count_method_bb_distribution[0]++;
4770 else if (jd->basicblockcount <= 10)
4771 count_method_bb_distribution[1]++;
4772 else if (jd->basicblockcount <= 15)
4773 count_method_bb_distribution[2]++;
4774 else if (jd->basicblockcount <= 20)
4775 count_method_bb_distribution[3]++;
4776 else if (jd->basicblockcount <= 30)
4777 count_method_bb_distribution[4]++;
4778 else if (jd->basicblockcount <= 40)
4779 count_method_bb_distribution[5]++;
4780 else if (jd->basicblockcount <= 50)
4781 count_method_bb_distribution[6]++;
4782 else if (jd->basicblockcount <= 75)
4783 count_method_bb_distribution[7]++;
4785 count_method_bb_distribution[8]++;
4787 #endif /* defined(ENABLE_STATISTICS) */
4789 /* everything's ok *******************************************************/
4793 /* goto labels for throwing verifier exceptions **************************/
4795 #if defined(ENABLE_VERIFIER)
4797 throw_stack_underflow:
4798 exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4801 throw_stack_overflow:
4802 exceptions_throw_verifyerror(m, "Stack size too large");
4805 throw_stack_type_error:
4806 exceptions_throw_verifyerror_for_stack(m, expectedtype);
4809 throw_stack_category_error:
4810 exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4817 /* functions for verbose stack analysis output ********************************/
4819 #if defined(STACK_VERBOSE)
4820 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4822 printf("%c", show_jit_type_letters[v->type]);
4823 if (v->type == TYPE_RET)
4824 printf("{L%03d}", v->vv.retaddr->nr);
4828 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4830 assert(index >= 0 && index < sd->vartop);
4831 stack_verbose_show_varinfo(sd, sd->var + index);
4835 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4839 printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4841 for (i=0; i<bptr->indepth; ++i) {
4844 stack_verbose_show_variable(sd, bptr->invars[i]);
4849 printf("] javalocals ");
4850 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4851 printf(" inlocals [");
4852 if (bptr->inlocals) {
4853 for (i=0; i<sd->localcount; ++i) {
4856 stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4861 printf("] out:%d [", bptr->outdepth);
4862 if (bptr->outvars) {
4863 for (i=0; i<bptr->outdepth; ++i) {
4866 stack_verbose_show_variable(sd, bptr->outvars[i]);
4874 printf(" (clone of L%03d)", bptr->original->nr);
4876 basicblock *b = bptr->copied_to;
4878 printf(" (copied to ");
4879 for (; b; b = b->copied_to)
4880 printf("L%03d ", b->nr);
4887 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4891 printf("======================================== STACK %sANALYSE BLOCK ",
4892 (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4893 stack_verbose_show_block(sd, sd->bptr);
4896 if (sd->handlers[0]) {
4897 printf("HANDLERS: ");
4898 for (i=0; sd->handlers[i]; ++i) {
4899 printf("L%03d ", sd->handlers[i]->handler->nr);
4907 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4909 printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4910 stack_verbose_show_block(sd, sd->bptr);
4914 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackptr curstack)
4922 printf(" javalocals ");
4923 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4926 for(i = 0, sp = curstack; sp; sp = sp->prev)
4930 stack = MNEW(stackptr, depth);
4931 for(sp = curstack; sp; sp = sp->prev)
4934 for(i=0; i<depth; ++i) {
4938 v = &(sd->var[sp->varnum]);
4940 if (v->flags & INOUT)
4942 if (v->flags & PREALLOC)
4944 printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
4945 if (v->type == TYPE_RET) {
4946 printf("(L%03d)", v->vv.retaddr->nr);
4951 show_icmd(sd->jd, iptr, false, SHOW_PARSE);
4958 * These are local overrides for various environment variables in Emacs.
4959 * Please do not remove this and leave it at the end of the file, where
4960 * Emacs will automagically detect them.
4961 * ---------------------------------------------------------------------
4964 * indent-tabs-mode: t
4968 * vim:noexpandtab:sw=4:ts=4: