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 5921 2006-11-05 21:27:37Z 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 */
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 */
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:
1478 case ICMD_INLINE_START:
1479 case ICMD_INLINE_END:
1480 case ICMD_INLINE_GOTO:
1484 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1485 superblockend = true;
1488 /* pop 0 push 1 const */
1496 /* pop 0 push 1 load */
1503 RELOCATE(iptr->dst.varindex);
1516 RELOCATE(iptr->sx.s23.s2.varindex);
1517 RELOCATE(iptr->s1.varindex);
1518 RELOCATE(iptr->dst.varindex);
1531 RELOCATE(iptr->sx.s23.s3.varindex);
1532 RELOCATE(iptr->sx.s23.s2.varindex);
1533 RELOCATE(iptr->s1.varindex);
1536 /* pop 1 push 0 store */
1543 RELOCATE(iptr->s1.varindex);
1545 j = iptr->dst.varindex;
1546 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1547 i = iptr->sx.s23.s3.javaindex;
1548 if (iptr->flags.bits & INS_FLAG_RETADDR)
1549 sd->javalocals[i] = UNUSED;
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 */
2173 if (!stack_reanalyse_block(&sd))
2175 superblockend = true; /* XXX */
2179 if (superblockend && (sd.bptr->flags < BBREACHED)) {
2180 /* This block has not been reached so far, and we */
2181 /* don't fall into it, so we'll have to iterate again. */
2187 if (sd.bptr->flags > BBREACHED) {
2188 /* This block is already finished. */
2190 superblockend = true;
2194 if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2195 /* This block is a clone and the original has not been */
2196 /* analysed, yet. Analyse it on the next iteration. */
2199 /* XXX superblockend? */
2203 /* This block has to be analysed now. */
2205 /* XXX The rest of this block is still indented one level too */
2206 /* much in order to avoid a giant diff by changing that. */
2208 /* We know that sd.bptr->flags == BBREACHED. */
2209 /* This block has been reached before. */
2211 assert(sd.bptr->flags == BBREACHED);
2212 stackdepth = sd.bptr->indepth;
2214 /* find exception handlers for this block */
2216 /* determine the active exception handlers for this block */
2217 /* XXX could use a faster algorithm with sorted lists or */
2220 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2223 ex = jd->exceptiontable;
2224 for (; ex != NULL; ex = ex->down) {
2225 if ((ex->start <= original) && (ex->end > original)) {
2226 sd.handlers[len++] = ex;
2229 sd.handlers[len] = NULL;
2232 /* reanalyse cloned block */
2234 if (sd.bptr->original) {
2235 if (!stack_reanalyse_block(&sd))
2240 /* reset the new pointer for allocating stackslots */
2244 /* create the instack of this block */
2246 curstack = stack_create_instack(&sd);
2248 /* initialize locals at the start of this block */
2250 if (sd.bptr->inlocals)
2251 MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2253 MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2255 /* set up local variables for analyzing this block */
2258 superblockend = false;
2259 len = sd.bptr->icount;
2260 iptr = sd.bptr->iinstr;
2262 /* mark the block as analysed */
2264 sd.bptr->flags = BBFINISHED;
2266 /* reset variables for dependency checking */
2268 coalescing_boundary = sd.new;
2269 for( i = 0; i < m->maxlocals; i++)
2270 last_store_boundary[i] = sd.new;
2272 /* remember the start of this block's variables */
2274 sd.bptr->varstart = sd.vartop;
2276 #if defined(STACK_VERBOSE)
2277 stack_verbose_block_enter(&sd, false);
2280 /* reach exception handlers for this block */
2282 if (!stack_reach_handlers(&sd))
2285 /* iterate over ICMDs ****************************************/
2287 while (--len >= 0) {
2289 #if defined(STACK_VERBOSE)
2290 stack_verbose_show_state(&sd, iptr, curstack);
2293 /* fetch the current opcode */
2297 /* automatically replace some ICMDs with builtins */
2299 #if defined(USEBUILTINTABLE)
2300 bte = builtintable_get_automatic(opcode);
2302 if (bte && bte->opcode == opcode) {
2303 iptr->opc = ICMD_BUILTIN;
2304 iptr->flags.bits = 0;
2305 iptr->sx.s23.s3.bte = bte;
2306 /* iptr->line is already set */
2307 jd->isleafmethod = false;
2310 #endif /* defined(USEBUILTINTABLE) */
2312 /* main opcode switch *************************************/
2324 case ICMD_CHECKNULL:
2325 coalescing_boundary = sd.new;
2326 COUNT(count_check_null);
2329 iptr->dst.varindex = iptr->s1.varindex;
2333 j = iptr->s1.varindex =
2334 jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2336 #if defined(ENABLE_VERIFIER)
2337 if (sd.var[j].type != TYPE_RET) {
2338 exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2345 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2346 superblockend = true;
2350 COUNT(count_pcmd_return);
2353 superblockend = true;
2354 sd.jd->returncount++;
2355 sd.jd->returnblock = sd.bptr;
2359 /* pop 0 push 1 const */
2361 /************************** ICONST OPTIMIZATIONS **************************/
2364 COUNT(count_pcmd_load);
2368 switch (iptr[1].opc) {
2370 iptr->opc = ICMD_IADDCONST;
2374 iptr[1].opc = ICMD_NOP;
2375 OP1_1(TYPE_INT, TYPE_INT);
2376 COUNT(count_pcmd_op);
2380 iptr->opc = ICMD_ISUBCONST;
2381 goto icmd_iconst_tail;
2382 #if SUPPORT_CONST_MUL
2384 iptr->opc = ICMD_IMULCONST;
2385 goto icmd_iconst_tail;
2386 #else /* SUPPORT_CONST_MUL */
2388 if (iptr->sx.val.i == 0x00000002)
2390 else if (iptr->sx.val.i == 0x00000004)
2392 else if (iptr->sx.val.i == 0x00000008)
2394 else if (iptr->sx.val.i == 0x00000010)
2396 else if (iptr->sx.val.i == 0x00000020)
2398 else if (iptr->sx.val.i == 0x00000040)
2400 else if (iptr->sx.val.i == 0x00000080)
2402 else if (iptr->sx.val.i == 0x00000100)
2404 else if (iptr->sx.val.i == 0x00000200)
2406 else if (iptr->sx.val.i == 0x00000400)
2407 iptr->sx.val.i = 10;
2408 else if (iptr->sx.val.i == 0x00000800)
2409 iptr->sx.val.i = 11;
2410 else if (iptr->sx.val.i == 0x00001000)
2411 iptr->sx.val.i = 12;
2412 else if (iptr->sx.val.i == 0x00002000)
2413 iptr->sx.val.i = 13;
2414 else if (iptr->sx.val.i == 0x00004000)
2415 iptr->sx.val.i = 14;
2416 else if (iptr->sx.val.i == 0x00008000)
2417 iptr->sx.val.i = 15;
2418 else if (iptr->sx.val.i == 0x00010000)
2419 iptr->sx.val.i = 16;
2420 else if (iptr->sx.val.i == 0x00020000)
2421 iptr->sx.val.i = 17;
2422 else if (iptr->sx.val.i == 0x00040000)
2423 iptr->sx.val.i = 18;
2424 else if (iptr->sx.val.i == 0x00080000)
2425 iptr->sx.val.i = 19;
2426 else if (iptr->sx.val.i == 0x00100000)
2427 iptr->sx.val.i = 20;
2428 else if (iptr->sx.val.i == 0x00200000)
2429 iptr->sx.val.i = 21;
2430 else if (iptr->sx.val.i == 0x00400000)
2431 iptr->sx.val.i = 22;
2432 else if (iptr->sx.val.i == 0x00800000)
2433 iptr->sx.val.i = 23;
2434 else if (iptr->sx.val.i == 0x01000000)
2435 iptr->sx.val.i = 24;
2436 else if (iptr->sx.val.i == 0x02000000)
2437 iptr->sx.val.i = 25;
2438 else if (iptr->sx.val.i == 0x04000000)
2439 iptr->sx.val.i = 26;
2440 else if (iptr->sx.val.i == 0x08000000)
2441 iptr->sx.val.i = 27;
2442 else if (iptr->sx.val.i == 0x10000000)
2443 iptr->sx.val.i = 28;
2444 else if (iptr->sx.val.i == 0x20000000)
2445 iptr->sx.val.i = 29;
2446 else if (iptr->sx.val.i == 0x40000000)
2447 iptr->sx.val.i = 30;
2448 else if (iptr->sx.val.i == 0x80000000)
2449 iptr->sx.val.i = 31;
2453 iptr->opc = ICMD_IMULPOW2;
2454 goto icmd_iconst_tail;
2455 #endif /* SUPPORT_CONST_MUL */
2457 if (iptr->sx.val.i == 0x00000002)
2459 else if (iptr->sx.val.i == 0x00000004)
2461 else if (iptr->sx.val.i == 0x00000008)
2463 else if (iptr->sx.val.i == 0x00000010)
2465 else if (iptr->sx.val.i == 0x00000020)
2467 else if (iptr->sx.val.i == 0x00000040)
2469 else if (iptr->sx.val.i == 0x00000080)
2471 else if (iptr->sx.val.i == 0x00000100)
2473 else if (iptr->sx.val.i == 0x00000200)
2475 else if (iptr->sx.val.i == 0x00000400)
2476 iptr->sx.val.i = 10;
2477 else if (iptr->sx.val.i == 0x00000800)
2478 iptr->sx.val.i = 11;
2479 else if (iptr->sx.val.i == 0x00001000)
2480 iptr->sx.val.i = 12;
2481 else if (iptr->sx.val.i == 0x00002000)
2482 iptr->sx.val.i = 13;
2483 else if (iptr->sx.val.i == 0x00004000)
2484 iptr->sx.val.i = 14;
2485 else if (iptr->sx.val.i == 0x00008000)
2486 iptr->sx.val.i = 15;
2487 else if (iptr->sx.val.i == 0x00010000)
2488 iptr->sx.val.i = 16;
2489 else if (iptr->sx.val.i == 0x00020000)
2490 iptr->sx.val.i = 17;
2491 else if (iptr->sx.val.i == 0x00040000)
2492 iptr->sx.val.i = 18;
2493 else if (iptr->sx.val.i == 0x00080000)
2494 iptr->sx.val.i = 19;
2495 else if (iptr->sx.val.i == 0x00100000)
2496 iptr->sx.val.i = 20;
2497 else if (iptr->sx.val.i == 0x00200000)
2498 iptr->sx.val.i = 21;
2499 else if (iptr->sx.val.i == 0x00400000)
2500 iptr->sx.val.i = 22;
2501 else if (iptr->sx.val.i == 0x00800000)
2502 iptr->sx.val.i = 23;
2503 else if (iptr->sx.val.i == 0x01000000)
2504 iptr->sx.val.i = 24;
2505 else if (iptr->sx.val.i == 0x02000000)
2506 iptr->sx.val.i = 25;
2507 else if (iptr->sx.val.i == 0x04000000)
2508 iptr->sx.val.i = 26;
2509 else if (iptr->sx.val.i == 0x08000000)
2510 iptr->sx.val.i = 27;
2511 else if (iptr->sx.val.i == 0x10000000)
2512 iptr->sx.val.i = 28;
2513 else if (iptr->sx.val.i == 0x20000000)
2514 iptr->sx.val.i = 29;
2515 else if (iptr->sx.val.i == 0x40000000)
2516 iptr->sx.val.i = 30;
2517 else if (iptr->sx.val.i == 0x80000000)
2518 iptr->sx.val.i = 31;
2522 iptr->opc = ICMD_IDIVPOW2;
2523 goto icmd_iconst_tail;
2526 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2527 if ((iptr->sx.val.i == 0x00000002) ||
2528 (iptr->sx.val.i == 0x00000004) ||
2529 (iptr->sx.val.i == 0x00000008) ||
2530 (iptr->sx.val.i == 0x00000010) ||
2531 (iptr->sx.val.i == 0x00000020) ||
2532 (iptr->sx.val.i == 0x00000040) ||
2533 (iptr->sx.val.i == 0x00000080) ||
2534 (iptr->sx.val.i == 0x00000100) ||
2535 (iptr->sx.val.i == 0x00000200) ||
2536 (iptr->sx.val.i == 0x00000400) ||
2537 (iptr->sx.val.i == 0x00000800) ||
2538 (iptr->sx.val.i == 0x00001000) ||
2539 (iptr->sx.val.i == 0x00002000) ||
2540 (iptr->sx.val.i == 0x00004000) ||
2541 (iptr->sx.val.i == 0x00008000) ||
2542 (iptr->sx.val.i == 0x00010000) ||
2543 (iptr->sx.val.i == 0x00020000) ||
2544 (iptr->sx.val.i == 0x00040000) ||
2545 (iptr->sx.val.i == 0x00080000) ||
2546 (iptr->sx.val.i == 0x00100000) ||
2547 (iptr->sx.val.i == 0x00200000) ||
2548 (iptr->sx.val.i == 0x00400000) ||
2549 (iptr->sx.val.i == 0x00800000) ||
2550 (iptr->sx.val.i == 0x01000000) ||
2551 (iptr->sx.val.i == 0x02000000) ||
2552 (iptr->sx.val.i == 0x04000000) ||
2553 (iptr->sx.val.i == 0x08000000) ||
2554 (iptr->sx.val.i == 0x10000000) ||
2555 (iptr->sx.val.i == 0x20000000) ||
2556 (iptr->sx.val.i == 0x40000000) ||
2557 (iptr->sx.val.i == 0x80000000))
2559 iptr->opc = ICMD_IREMPOW2;
2560 iptr->sx.val.i -= 1;
2561 goto icmd_iconst_tail;
2564 #if SUPPORT_CONST_LOGICAL
2566 iptr->opc = ICMD_IANDCONST;
2567 goto icmd_iconst_tail;
2570 iptr->opc = ICMD_IORCONST;
2571 goto icmd_iconst_tail;
2574 iptr->opc = ICMD_IXORCONST;
2575 goto icmd_iconst_tail;
2577 #endif /* SUPPORT_CONST_LOGICAL */
2579 iptr->opc = ICMD_ISHLCONST;
2580 goto icmd_iconst_tail;
2583 iptr->opc = ICMD_ISHRCONST;
2584 goto icmd_iconst_tail;
2587 iptr->opc = ICMD_IUSHRCONST;
2588 goto icmd_iconst_tail;
2589 #if SUPPORT_LONG_SHIFT
2591 iptr->opc = ICMD_LSHLCONST;
2592 goto icmd_lconst_tail;
2595 iptr->opc = ICMD_LSHRCONST;
2596 goto icmd_lconst_tail;
2599 iptr->opc = ICMD_LUSHRCONST;
2600 goto icmd_lconst_tail;
2601 #endif /* SUPPORT_LONG_SHIFT */
2602 case ICMD_IF_ICMPEQ:
2603 iptr[1].opc = ICMD_IFEQ;
2607 /* set the constant for the following icmd */
2608 iptr[1].sx.val.i = iptr->sx.val.i;
2610 /* this instruction becomes a nop */
2611 iptr->opc = ICMD_NOP;
2614 case ICMD_IF_ICMPLT:
2615 iptr[1].opc = ICMD_IFLT;
2616 goto icmd_if_icmp_tail;
2618 case ICMD_IF_ICMPLE:
2619 iptr[1].opc = ICMD_IFLE;
2620 goto icmd_if_icmp_tail;
2622 case ICMD_IF_ICMPNE:
2623 iptr[1].opc = ICMD_IFNE;
2624 goto icmd_if_icmp_tail;
2626 case ICMD_IF_ICMPGT:
2627 iptr[1].opc = ICMD_IFGT;
2628 goto icmd_if_icmp_tail;
2630 case ICMD_IF_ICMPGE:
2631 iptr[1].opc = ICMD_IFGE;
2632 goto icmd_if_icmp_tail;
2634 #if SUPPORT_CONST_STORE
2639 # if SUPPORT_CONST_STORE_ZERO_ONLY
2640 if (iptr->sx.val.i != 0)
2643 switch (iptr[1].opc) {
2645 iptr->opc = ICMD_IASTORECONST;
2646 iptr->flags.bits |= INS_FLAG_CHECK;
2649 iptr->opc = ICMD_BASTORECONST;
2650 iptr->flags.bits |= INS_FLAG_CHECK;
2653 iptr->opc = ICMD_CASTORECONST;
2654 iptr->flags.bits |= INS_FLAG_CHECK;
2657 iptr->opc = ICMD_SASTORECONST;
2658 iptr->flags.bits |= INS_FLAG_CHECK;
2662 iptr[1].opc = ICMD_NOP;
2664 /* copy the constant to s3 */
2665 /* XXX constval -> astoreconstval? */
2666 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2667 OP2_0(TYPE_ADR, TYPE_INT);
2668 COUNT(count_pcmd_op);
2671 case ICMD_PUTSTATIC:
2673 # if SUPPORT_CONST_STORE_ZERO_ONLY
2674 if (iptr->sx.val.i != 0)
2677 /* XXX check field type? */
2679 /* copy the constant to s2 */
2680 /* XXX constval -> fieldconstval? */
2681 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2684 /* set the field reference (s3) */
2685 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2686 iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2687 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2688 fmiref = iptr->sx.s23.s3.uf->fieldref;
2691 fmiref = iptr[1].sx.s23.s3.fmiref;
2692 iptr->sx.s23.s3.fmiref = fmiref;
2695 #if defined(ENABLE_VERIFIER)
2696 expectedtype = fmiref->parseddesc.fd->type;
2697 switch (iptr[0].opc) {
2699 if (expectedtype != TYPE_INT)
2700 goto throw_stack_type_error;
2703 if (expectedtype != TYPE_LNG)
2704 goto throw_stack_type_error;
2707 if (expectedtype != TYPE_ADR)
2708 goto throw_stack_type_error;
2713 #endif /* defined(ENABLE_VERIFIER) */
2715 switch (iptr[1].opc) {
2716 case ICMD_PUTSTATIC:
2717 iptr->opc = ICMD_PUTSTATICCONST;
2721 iptr->opc = ICMD_PUTFIELDCONST;
2726 iptr[1].opc = ICMD_NOP;
2727 COUNT(count_pcmd_op);
2729 #endif /* SUPPORT_CONST_STORE */
2735 /* if we get here, the ICONST has been optimized */
2739 /* normal case of an unoptimized ICONST */
2743 /************************** LCONST OPTIMIZATIONS **************************/
2746 COUNT(count_pcmd_load);
2750 /* switch depending on the following instruction */
2752 switch (iptr[1].opc) {
2753 #if SUPPORT_LONG_ADD
2755 iptr->opc = ICMD_LADDCONST;
2759 /* instruction of type LONG -> LONG */
2760 iptr[1].opc = ICMD_NOP;
2761 OP1_1(TYPE_LNG, TYPE_LNG);
2762 COUNT(count_pcmd_op);
2766 iptr->opc = ICMD_LSUBCONST;
2767 goto icmd_lconst_tail;
2769 #endif /* SUPPORT_LONG_ADD */
2770 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2772 iptr->opc = ICMD_LMULCONST;
2773 goto icmd_lconst_tail;
2774 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2775 # if SUPPORT_LONG_SHIFT
2777 if (iptr->sx.val.l == 0x00000002)
2779 else if (iptr->sx.val.l == 0x00000004)
2781 else if (iptr->sx.val.l == 0x00000008)
2783 else if (iptr->sx.val.l == 0x00000010)
2785 else if (iptr->sx.val.l == 0x00000020)
2787 else if (iptr->sx.val.l == 0x00000040)
2789 else if (iptr->sx.val.l == 0x00000080)
2791 else if (iptr->sx.val.l == 0x00000100)
2793 else if (iptr->sx.val.l == 0x00000200)
2795 else if (iptr->sx.val.l == 0x00000400)
2796 iptr->sx.val.i = 10;
2797 else if (iptr->sx.val.l == 0x00000800)
2798 iptr->sx.val.i = 11;
2799 else if (iptr->sx.val.l == 0x00001000)
2800 iptr->sx.val.i = 12;
2801 else if (iptr->sx.val.l == 0x00002000)
2802 iptr->sx.val.i = 13;
2803 else if (iptr->sx.val.l == 0x00004000)
2804 iptr->sx.val.i = 14;
2805 else if (iptr->sx.val.l == 0x00008000)
2806 iptr->sx.val.i = 15;
2807 else if (iptr->sx.val.l == 0x00010000)
2808 iptr->sx.val.i = 16;
2809 else if (iptr->sx.val.l == 0x00020000)
2810 iptr->sx.val.i = 17;
2811 else if (iptr->sx.val.l == 0x00040000)
2812 iptr->sx.val.i = 18;
2813 else if (iptr->sx.val.l == 0x00080000)
2814 iptr->sx.val.i = 19;
2815 else if (iptr->sx.val.l == 0x00100000)
2816 iptr->sx.val.i = 20;
2817 else if (iptr->sx.val.l == 0x00200000)
2818 iptr->sx.val.i = 21;
2819 else if (iptr->sx.val.l == 0x00400000)
2820 iptr->sx.val.i = 22;
2821 else if (iptr->sx.val.l == 0x00800000)
2822 iptr->sx.val.i = 23;
2823 else if (iptr->sx.val.l == 0x01000000)
2824 iptr->sx.val.i = 24;
2825 else if (iptr->sx.val.l == 0x02000000)
2826 iptr->sx.val.i = 25;
2827 else if (iptr->sx.val.l == 0x04000000)
2828 iptr->sx.val.i = 26;
2829 else if (iptr->sx.val.l == 0x08000000)
2830 iptr->sx.val.i = 27;
2831 else if (iptr->sx.val.l == 0x10000000)
2832 iptr->sx.val.i = 28;
2833 else if (iptr->sx.val.l == 0x20000000)
2834 iptr->sx.val.i = 29;
2835 else if (iptr->sx.val.l == 0x40000000)
2836 iptr->sx.val.i = 30;
2837 else if (iptr->sx.val.l == 0x80000000)
2838 iptr->sx.val.i = 31;
2842 iptr->opc = ICMD_LMULPOW2;
2843 goto icmd_lconst_tail;
2844 # endif /* SUPPORT_LONG_SHIFT */
2845 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2846 #if SUPPORT_LONG_DIV_POW2
2848 if (iptr->sx.val.l == 0x00000002)
2850 else if (iptr->sx.val.l == 0x00000004)
2852 else if (iptr->sx.val.l == 0x00000008)
2854 else if (iptr->sx.val.l == 0x00000010)
2856 else if (iptr->sx.val.l == 0x00000020)
2858 else if (iptr->sx.val.l == 0x00000040)
2860 else if (iptr->sx.val.l == 0x00000080)
2862 else if (iptr->sx.val.l == 0x00000100)
2864 else if (iptr->sx.val.l == 0x00000200)
2866 else if (iptr->sx.val.l == 0x00000400)
2867 iptr->sx.val.i = 10;
2868 else if (iptr->sx.val.l == 0x00000800)
2869 iptr->sx.val.i = 11;
2870 else if (iptr->sx.val.l == 0x00001000)
2871 iptr->sx.val.i = 12;
2872 else if (iptr->sx.val.l == 0x00002000)
2873 iptr->sx.val.i = 13;
2874 else if (iptr->sx.val.l == 0x00004000)
2875 iptr->sx.val.i = 14;
2876 else if (iptr->sx.val.l == 0x00008000)
2877 iptr->sx.val.i = 15;
2878 else if (iptr->sx.val.l == 0x00010000)
2879 iptr->sx.val.i = 16;
2880 else if (iptr->sx.val.l == 0x00020000)
2881 iptr->sx.val.i = 17;
2882 else if (iptr->sx.val.l == 0x00040000)
2883 iptr->sx.val.i = 18;
2884 else if (iptr->sx.val.l == 0x00080000)
2885 iptr->sx.val.i = 19;
2886 else if (iptr->sx.val.l == 0x00100000)
2887 iptr->sx.val.i = 20;
2888 else if (iptr->sx.val.l == 0x00200000)
2889 iptr->sx.val.i = 21;
2890 else if (iptr->sx.val.l == 0x00400000)
2891 iptr->sx.val.i = 22;
2892 else if (iptr->sx.val.l == 0x00800000)
2893 iptr->sx.val.i = 23;
2894 else if (iptr->sx.val.l == 0x01000000)
2895 iptr->sx.val.i = 24;
2896 else if (iptr->sx.val.l == 0x02000000)
2897 iptr->sx.val.i = 25;
2898 else if (iptr->sx.val.l == 0x04000000)
2899 iptr->sx.val.i = 26;
2900 else if (iptr->sx.val.l == 0x08000000)
2901 iptr->sx.val.i = 27;
2902 else if (iptr->sx.val.l == 0x10000000)
2903 iptr->sx.val.i = 28;
2904 else if (iptr->sx.val.l == 0x20000000)
2905 iptr->sx.val.i = 29;
2906 else if (iptr->sx.val.l == 0x40000000)
2907 iptr->sx.val.i = 30;
2908 else if (iptr->sx.val.l == 0x80000000)
2909 iptr->sx.val.i = 31;
2913 iptr->opc = ICMD_LDIVPOW2;
2914 goto icmd_lconst_tail;
2915 #endif /* SUPPORT_LONG_DIV_POW2 */
2917 #if SUPPORT_LONG_REM_POW2
2919 if ((iptr->sx.val.l == 0x00000002) ||
2920 (iptr->sx.val.l == 0x00000004) ||
2921 (iptr->sx.val.l == 0x00000008) ||
2922 (iptr->sx.val.l == 0x00000010) ||
2923 (iptr->sx.val.l == 0x00000020) ||
2924 (iptr->sx.val.l == 0x00000040) ||
2925 (iptr->sx.val.l == 0x00000080) ||
2926 (iptr->sx.val.l == 0x00000100) ||
2927 (iptr->sx.val.l == 0x00000200) ||
2928 (iptr->sx.val.l == 0x00000400) ||
2929 (iptr->sx.val.l == 0x00000800) ||
2930 (iptr->sx.val.l == 0x00001000) ||
2931 (iptr->sx.val.l == 0x00002000) ||
2932 (iptr->sx.val.l == 0x00004000) ||
2933 (iptr->sx.val.l == 0x00008000) ||
2934 (iptr->sx.val.l == 0x00010000) ||
2935 (iptr->sx.val.l == 0x00020000) ||
2936 (iptr->sx.val.l == 0x00040000) ||
2937 (iptr->sx.val.l == 0x00080000) ||
2938 (iptr->sx.val.l == 0x00100000) ||
2939 (iptr->sx.val.l == 0x00200000) ||
2940 (iptr->sx.val.l == 0x00400000) ||
2941 (iptr->sx.val.l == 0x00800000) ||
2942 (iptr->sx.val.l == 0x01000000) ||
2943 (iptr->sx.val.l == 0x02000000) ||
2944 (iptr->sx.val.l == 0x04000000) ||
2945 (iptr->sx.val.l == 0x08000000) ||
2946 (iptr->sx.val.l == 0x10000000) ||
2947 (iptr->sx.val.l == 0x20000000) ||
2948 (iptr->sx.val.l == 0x40000000) ||
2949 (iptr->sx.val.l == 0x80000000))
2951 iptr->opc = ICMD_LREMPOW2;
2952 iptr->sx.val.l -= 1;
2953 goto icmd_lconst_tail;
2956 #endif /* SUPPORT_LONG_REM_POW2 */
2958 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2961 iptr->opc = ICMD_LANDCONST;
2962 goto icmd_lconst_tail;
2965 iptr->opc = ICMD_LORCONST;
2966 goto icmd_lconst_tail;
2969 iptr->opc = ICMD_LXORCONST;
2970 goto icmd_lconst_tail;
2971 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2973 #if SUPPORT_LONG_CMP_CONST
2975 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2978 /* switch on the instruction after LCONST - LCMP */
2980 switch (iptr[2].opc) {
2982 iptr->opc = ICMD_IF_LEQ;
2985 icmd_lconst_lcmp_tail:
2986 /* convert LCONST, LCMP, IFXX to IF_LXX */
2987 iptr->dst.insindex = iptr[2].dst.insindex;
2988 iptr[1].opc = ICMD_NOP;
2989 iptr[2].opc = ICMD_NOP;
2991 OP1_BRANCH(TYPE_LNG);
2993 COUNT(count_pcmd_bra);
2994 COUNT(count_pcmd_op);
2998 iptr->opc = ICMD_IF_LNE;
2999 goto icmd_lconst_lcmp_tail;
3002 iptr->opc = ICMD_IF_LLT;
3003 goto icmd_lconst_lcmp_tail;
3006 iptr->opc = ICMD_IF_LGT;
3007 goto icmd_lconst_lcmp_tail;
3010 iptr->opc = ICMD_IF_LLE;
3011 goto icmd_lconst_lcmp_tail;
3014 iptr->opc = ICMD_IF_LGE;
3015 goto icmd_lconst_lcmp_tail;
3019 } /* end switch on opcode after LCONST - LCMP */
3021 #endif /* SUPPORT_LONG_CMP_CONST */
3023 #if SUPPORT_CONST_STORE
3025 # if SUPPORT_CONST_STORE_ZERO_ONLY
3026 if (iptr->sx.val.l != 0)
3029 #if SIZEOF_VOID_P == 4
3030 /* the constant must fit into a ptrint */
3031 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3034 /* move the constant to s3 */
3035 iptr->sx.s23.s3.constval = iptr->sx.val.l;
3037 iptr->opc = ICMD_LASTORECONST;
3038 iptr->flags.bits |= INS_FLAG_CHECK;
3039 OP2_0(TYPE_ADR, TYPE_INT);
3041 iptr[1].opc = ICMD_NOP;
3042 COUNT(count_pcmd_op);
3045 case ICMD_PUTSTATIC:
3047 # if SUPPORT_CONST_STORE_ZERO_ONLY
3048 if (iptr->sx.val.l != 0)
3051 #if SIZEOF_VOID_P == 4
3052 /* the constant must fit into a ptrint */
3053 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3056 /* XXX check field type? */
3058 /* copy the constant to s2 */
3059 /* XXX constval -> fieldconstval? */
3060 iptr->sx.s23.s2.constval = iptr->sx.val.l;
3064 #endif /* SUPPORT_CONST_STORE */
3068 } /* end switch opcode after LCONST */
3070 /* if we get here, the LCONST has been optimized */
3074 /* the normal case of an unoptimized LCONST */
3078 /************************ END OF LCONST OPTIMIZATIONS *********************/
3081 COUNT(count_pcmd_load);
3086 COUNT(count_pcmd_load);
3090 /************************** ACONST OPTIMIZATIONS **************************/
3093 coalescing_boundary = sd.new;
3094 COUNT(count_pcmd_load);
3095 #if SUPPORT_CONST_STORE
3096 /* We can only optimize if the ACONST is resolved
3097 * and there is an instruction after it. */
3099 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3102 switch (iptr[1].opc) {
3104 /* We can only optimize for NULL values
3105 * here because otherwise a checkcast is
3107 if (iptr->sx.val.anyptr != NULL)
3110 /* copy the constant (NULL) to s3 */
3111 iptr->sx.s23.s3.constval = 0;
3112 iptr->opc = ICMD_AASTORECONST;
3113 iptr->flags.bits |= INS_FLAG_CHECK;
3114 OP2_0(TYPE_ADR, TYPE_INT);
3116 iptr[1].opc = ICMD_NOP;
3117 COUNT(count_pcmd_op);
3120 case ICMD_PUTSTATIC:
3122 # if SUPPORT_CONST_STORE_ZERO_ONLY
3123 if (iptr->sx.val.anyptr != NULL)
3126 /* XXX check field type? */
3127 /* copy the constant to s2 */
3128 /* XXX constval -> fieldconstval? */
3129 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3137 /* if we get here the ACONST has been optimized */
3141 #endif /* SUPPORT_CONST_STORE */
3146 /* pop 0 push 1 load */
3153 COUNT(count_load_instruction);
3154 i = opcode - ICMD_ILOAD; /* type */
3156 j = iptr->s1.varindex =
3157 jd->local_map[iptr->s1.varindex * 5 + i];
3159 #if defined(ENABLE_VERIFIER)
3160 if (sd.var[j].type == TYPE_RET) {
3161 exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3166 #if defined(ENABLE_SSA)
3168 GET_NEW_VAR(sd, new_index, i);
3185 coalescing_boundary = sd.new;
3186 iptr->flags.bits |= INS_FLAG_CHECK;
3187 COUNT(count_check_null);
3188 COUNT(count_check_bound);
3189 COUNT(count_pcmd_mem);
3190 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3197 coalescing_boundary = sd.new;
3198 iptr->flags.bits |= INS_FLAG_CHECK;
3199 COUNT(count_check_null);
3200 COUNT(count_check_bound);
3201 COUNT(count_pcmd_mem);
3202 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3205 /* pop 0 push 0 iinc */
3208 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3209 #if defined(ENABLE_SSA)
3212 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3216 last_store_boundary[iptr->s1.varindex] = sd.new;
3219 jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3224 if ((copy->varkind == LOCALVAR) &&
3225 (copy->varnum == iptr->s1.varindex))
3227 assert(IS_LOCALVAR(copy));
3233 #if defined(ENABLE_SSA)
3237 iptr->dst.varindex = iptr->s1.varindex;
3240 /* pop 1 push 0 store */
3249 i = opcode - ICMD_ISTORE; /* type */
3250 javaindex = iptr->dst.varindex;
3251 j = iptr->dst.varindex =
3252 jd->local_map[javaindex * 5 + i];
3254 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3256 iptr->sx.s23.s3.javaindex = javaindex;
3258 if (curstack->type == TYPE_RET) {
3259 sd.javalocals[javaindex] = UNUSED;
3260 iptr->flags.bits = INS_FLAG_RETADDR;
3263 sd.javalocals[javaindex] = j;
3265 /* invalidate the following javalocal for 2-word types */
3267 if (IS_2_WORD_TYPE(i)) {
3268 sd.javalocals[javaindex+1] = UNUSED;
3269 iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3272 /* invalidate 2-word types if second half was overwritten */
3274 if (javaindex > 0 && (i = sd.javalocals[javaindex-1]) != UNUSED) {
3275 if (IS_2_WORD_TYPE(sd.var[i].type)) {
3276 sd.javalocals[javaindex-1] = UNUSED;
3277 iptr->flags.bits |= INS_FLAG_KILL_PREV;
3281 #if defined(ENABLE_STATISTICS)
3284 i = sd.new - curstack;
3286 count_store_length[20]++;
3288 count_store_length[i]++;
3291 count_store_depth[10]++;
3293 count_store_depth[i]++;
3297 #if defined(ENABLE_SSA)
3300 /* check for conflicts as described in Figure 5.2 */
3302 copy = curstack->prev;
3305 if ((copy->varkind == LOCALVAR) &&
3306 (copy->varnum == j))
3308 copy->varkind = TEMPVAR;
3309 assert(IS_LOCALVAR(copy));
3316 /* if the variable is already coalesced, don't bother */
3318 /* We do not need to check against INOUT, as invars */
3319 /* are always before the coalescing boundary. */
3321 if (curstack->varkind == LOCALVAR)
3324 /* there is no STORE Lj while curstack is live */
3326 if (curstack < last_store_boundary[javaindex])
3327 goto assume_conflict;
3329 /* curstack must be after the coalescing boundary */
3331 if (curstack < coalescing_boundary)
3332 goto assume_conflict;
3334 /* there is no DEF LOCALVAR(j) while curstack is live */
3336 copy = sd.new; /* most recent stackslot created + 1 */
3337 while (--copy > curstack) {
3338 if (copy->varkind == LOCALVAR && copy->varnum == j)
3339 goto assume_conflict;
3342 /* coalesce the temporary variable with Lj */
3343 assert((curstack->varkind == TEMPVAR)
3344 || (curstack->varkind == UNDEFVAR));
3345 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3346 assert(!IS_INOUT(curstack));
3347 assert(!IS_PREALLOC(curstack));
3349 assert(curstack->creator);
3350 assert(curstack->creator->dst.varindex == curstack->varnum);
3351 assert(!(curstack->flags & PASSTHROUGH));
3352 RELEASE_INDEX(sd, curstack);
3353 curstack->varkind = LOCALVAR;
3354 curstack->varnum = j;
3355 curstack->creator->dst.varindex = j;
3358 /* revert the coalescing, if it has been done earlier */
3360 if ((curstack->varkind == LOCALVAR)
3361 && (curstack->varnum == j))
3363 assert(IS_LOCALVAR(curstack));
3364 SET_TEMPVAR(curstack);
3367 /* remember the stack boundary at this store */
3369 last_store_boundary[javaindex] = sd.new;
3370 #if defined(ENABLE_SSA)
3371 } /* if (ls != NULL) */
3374 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3377 STORE(opcode - ICMD_ISTORE, j);
3383 coalescing_boundary = sd.new;
3384 iptr->flags.bits |= INS_FLAG_CHECK;
3385 COUNT(count_check_null);
3386 COUNT(count_check_bound);
3387 COUNT(count_pcmd_mem);
3389 bte = builtintable_get_internal(BUILTIN_canstore);
3392 if (md->memuse > rd->memuse)
3393 rd->memuse = md->memuse;
3394 if (md->argintreguse > rd->argintreguse)
3395 rd->argintreguse = md->argintreguse;
3396 /* XXX non-leaf method? */
3398 /* make all stack variables saved */
3402 sd.var[copy->varnum].flags |= SAVEDVAR;
3403 /* in case copy->varnum is/will be a LOCALVAR */
3404 /* once and set back to a non LOCALVAR */
3405 /* the correct SAVEDVAR flag has to be */
3406 /* remembered in copy->flags, too */
3407 copy->flags |= SAVEDVAR;
3411 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3418 coalescing_boundary = sd.new;
3419 iptr->flags.bits |= INS_FLAG_CHECK;
3420 COUNT(count_check_null);
3421 COUNT(count_check_bound);
3422 COUNT(count_pcmd_mem);
3423 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3430 coalescing_boundary = sd.new;
3431 iptr->flags.bits |= INS_FLAG_CHECK;
3432 COUNT(count_check_null);
3433 COUNT(count_check_bound);
3434 COUNT(count_pcmd_mem);
3435 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3441 #ifdef ENABLE_VERIFIER
3444 if (IS_2_WORD_TYPE(curstack->type))
3445 goto throw_stack_category_error;
3456 coalescing_boundary = sd.new;
3457 /* Assert here that no LOCAL or INOUTS get */
3458 /* preallocated, since tha macros are not */
3459 /* available in md-abi.c! */
3460 if (IS_TEMPVAR(curstack))
3461 md_return_alloc(jd, curstack);
3462 COUNT(count_pcmd_return);
3463 OP1_0(opcode - ICMD_IRETURN);
3464 superblockend = true;
3465 sd.jd->returncount++;
3466 sd.jd->returnblock = sd.bptr;
3470 coalescing_boundary = sd.new;
3471 COUNT(count_check_null);
3473 curstack = NULL; stackdepth = 0;
3474 superblockend = true;
3477 case ICMD_PUTSTATIC:
3478 coalescing_boundary = sd.new;
3479 COUNT(count_pcmd_mem);
3480 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3481 OP1_0(fmiref->parseddesc.fd->type);
3484 /* pop 1 push 0 branch */
3487 case ICMD_IFNONNULL:
3488 COUNT(count_pcmd_bra);
3489 OP1_BRANCH(TYPE_ADR);
3499 COUNT(count_pcmd_bra);
3500 /* iptr->sx.val.i is set implicitly in parse by
3501 clearing the memory or from IF_ICMPxx
3504 OP1_BRANCH(TYPE_INT);
3505 /* iptr->sx.val.i = 0; */
3509 /* pop 0 push 0 branch */
3512 COUNT(count_pcmd_bra);
3515 superblockend = true;
3518 /* pop 1 push 0 table branch */
3520 case ICMD_TABLESWITCH:
3521 COUNT(count_pcmd_table);
3522 OP1_BRANCH(TYPE_INT);
3524 table = iptr->dst.table;
3525 BRANCH_TARGET(*table, tbptr);
3528 i = iptr->sx.s23.s3.tablehigh
3529 - iptr->sx.s23.s2.tablelow + 1;
3532 BRANCH_TARGET(*table, tbptr);
3535 superblockend = true;
3538 /* pop 1 push 0 table branch */
3540 case ICMD_LOOKUPSWITCH:
3541 COUNT(count_pcmd_table);
3542 OP1_BRANCH(TYPE_INT);
3544 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3546 lookup = iptr->dst.lookup;
3548 i = iptr->sx.s23.s2.lookupcount;
3551 BRANCH_TARGET(lookup->target, tbptr);
3554 superblockend = true;
3557 case ICMD_MONITORENTER:
3558 case ICMD_MONITOREXIT:
3559 coalescing_boundary = sd.new;
3560 COUNT(count_check_null);
3564 /* pop 2 push 0 branch */
3566 case ICMD_IF_ICMPEQ:
3567 case ICMD_IF_ICMPNE:
3568 case ICMD_IF_ICMPLT:
3569 case ICMD_IF_ICMPGE:
3570 case ICMD_IF_ICMPGT:
3571 case ICMD_IF_ICMPLE:
3572 COUNT(count_pcmd_bra);
3573 OP2_BRANCH(TYPE_INT, TYPE_INT);
3577 case ICMD_IF_ACMPEQ:
3578 case ICMD_IF_ACMPNE:
3579 COUNT(count_pcmd_bra);
3580 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3587 coalescing_boundary = sd.new;
3588 COUNT(count_check_null);
3589 COUNT(count_pcmd_mem);
3590 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3591 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3596 if (!IS_2_WORD_TYPE(curstack->type)) {
3598 #ifdef ENABLE_VERIFIER
3601 if (IS_2_WORD_TYPE(curstack->prev->type))
3602 goto throw_stack_category_error;
3605 OP2_0_ANY_ANY; /* pop two slots */
3608 iptr->opc = ICMD_POP;
3609 OP1_0_ANY; /* pop one (two-word) slot */
3613 /* pop 0 push 1 dup */
3616 #ifdef ENABLE_VERIFIER
3619 if (IS_2_WORD_TYPE(curstack->type))
3620 goto throw_stack_category_error;
3623 COUNT(count_dup_instruction);
3629 coalescing_boundary = sd.new - 1;
3634 if (IS_2_WORD_TYPE(curstack->type)) {
3636 iptr->opc = ICMD_DUP;
3641 /* ..., ????, cat1 */
3642 #ifdef ENABLE_VERIFIER
3644 if (IS_2_WORD_TYPE(curstack->prev->type))
3645 goto throw_stack_category_error;
3648 src1 = curstack->prev;
3651 COPY_UP(src1); iptr++; len--;
3654 coalescing_boundary = sd.new;
3658 /* pop 2 push 3 dup */
3661 #ifdef ENABLE_VERIFIER
3664 if (IS_2_WORD_TYPE(curstack->type) ||
3665 IS_2_WORD_TYPE(curstack->prev->type))
3666 goto throw_stack_category_error;
3671 src1 = curstack->prev;
3676 /* move non-temporary sources out of the way */
3677 if (!IS_TEMPVAR(src2)) {
3678 MOVE_TO_TEMP(src2); iptr++; len--;
3681 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3683 MOVE_UP(src1); iptr++; len--;
3684 MOVE_UP(src2); iptr++; len--;
3686 COPY_DOWN(curstack, dst1);
3688 coalescing_boundary = sd.new;
3693 if (IS_2_WORD_TYPE(curstack->type)) {
3694 /* ..., ????, cat2 */
3695 #ifdef ENABLE_VERIFIER
3697 if (IS_2_WORD_TYPE(curstack->prev->type))
3698 goto throw_stack_category_error;
3701 iptr->opc = ICMD_DUP_X1;
3705 /* ..., ????, cat1 */
3706 #ifdef ENABLE_VERIFIER
3709 if (IS_2_WORD_TYPE(curstack->prev->type)
3710 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3711 goto throw_stack_category_error;
3716 src1 = curstack->prev->prev;
3717 src2 = curstack->prev;
3719 POPANY; POPANY; POPANY;
3722 /* move non-temporary sources out of the way */
3723 if (!IS_TEMPVAR(src2)) {
3724 MOVE_TO_TEMP(src2); iptr++; len--;
3726 if (!IS_TEMPVAR(src3)) {
3727 MOVE_TO_TEMP(src3); iptr++; len--;
3730 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3731 DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3733 MOVE_UP(src1); iptr++; len--;
3734 MOVE_UP(src2); iptr++; len--;
3735 MOVE_UP(src3); iptr++; len--;
3737 COPY_DOWN(curstack, dst2); iptr++; len--;
3738 COPY_DOWN(curstack->prev, dst1);
3740 coalescing_boundary = sd.new;
3744 /* pop 3 push 4 dup */
3748 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3749 /* ..., cat2, ???? */
3750 #ifdef ENABLE_VERIFIER
3752 if (IS_2_WORD_TYPE(curstack->type))
3753 goto throw_stack_category_error;
3756 iptr->opc = ICMD_DUP_X1;
3760 /* ..., cat1, ???? */
3761 #ifdef ENABLE_VERIFIER
3764 if (IS_2_WORD_TYPE(curstack->type)
3765 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3766 goto throw_stack_category_error;
3770 src1 = curstack->prev->prev;
3771 src2 = curstack->prev;
3773 POPANY; POPANY; POPANY;
3776 /* move non-temporary sources out of the way */
3777 if (!IS_TEMPVAR(src2)) {
3778 MOVE_TO_TEMP(src2); iptr++; len--;
3780 if (!IS_TEMPVAR(src3)) {
3781 MOVE_TO_TEMP(src3); iptr++; len--;
3784 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3786 MOVE_UP(src1); iptr++; len--;
3787 MOVE_UP(src2); iptr++; len--;
3788 MOVE_UP(src3); iptr++; len--;
3790 COPY_DOWN(curstack, dst1);
3792 coalescing_boundary = sd.new;
3798 if (IS_2_WORD_TYPE(curstack->type)) {
3799 /* ..., ????, cat2 */
3800 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3801 /* ..., cat2, cat2 */
3802 iptr->opc = ICMD_DUP_X1;
3806 /* ..., cat1, cat2 */
3807 #ifdef ENABLE_VERIFIER
3810 if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3811 goto throw_stack_category_error;
3814 iptr->opc = ICMD_DUP_X2;
3820 /* ..., ????, ????, cat1 */
3822 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3823 /* ..., cat2, ????, cat1 */
3824 #ifdef ENABLE_VERIFIER
3826 if (IS_2_WORD_TYPE(curstack->prev->type))
3827 goto throw_stack_category_error;
3830 iptr->opc = ICMD_DUP2_X1;
3834 /* ..., cat1, ????, cat1 */
3835 #ifdef ENABLE_VERIFIER
3838 if (IS_2_WORD_TYPE(curstack->prev->type)
3839 || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3840 goto throw_stack_category_error;
3844 src1 = curstack->prev->prev->prev;
3845 src2 = curstack->prev->prev;
3846 src3 = curstack->prev;
3848 POPANY; POPANY; POPANY; POPANY;
3851 /* move non-temporary sources out of the way */
3852 if (!IS_TEMPVAR(src2)) {
3853 MOVE_TO_TEMP(src2); iptr++; len--;
3855 if (!IS_TEMPVAR(src3)) {
3856 MOVE_TO_TEMP(src3); iptr++; len--;
3858 if (!IS_TEMPVAR(src4)) {
3859 MOVE_TO_TEMP(src4); iptr++; len--;
3862 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3863 DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3865 MOVE_UP(src1); iptr++; len--;
3866 MOVE_UP(src2); iptr++; len--;
3867 MOVE_UP(src3); iptr++; len--;
3868 MOVE_UP(src4); iptr++; len--;
3870 COPY_DOWN(curstack, dst2); iptr++; len--;
3871 COPY_DOWN(curstack->prev, dst1);
3873 coalescing_boundary = sd.new;
3877 /* pop 2 push 2 swap */
3880 #ifdef ENABLE_VERIFIER
3883 if (IS_2_WORD_TYPE(curstack->type)
3884 || IS_2_WORD_TYPE(curstack->prev->type))
3885 goto throw_stack_category_error;
3889 src1 = curstack->prev;
3894 /* move non-temporary sources out of the way */
3895 if (!IS_TEMPVAR(src1)) {
3896 MOVE_TO_TEMP(src1); iptr++; len--;
3899 MOVE_UP(src2); iptr++; len--;
3902 coalescing_boundary = sd.new;
3909 coalescing_boundary = sd.new;
3910 #if !SUPPORT_DIVISION
3911 bte = iptr->sx.s23.s3.bte;
3914 if (md->memuse > rd->memuse)
3915 rd->memuse = md->memuse;
3916 if (md->argintreguse > rd->argintreguse)
3917 rd->argintreguse = md->argintreguse;
3919 /* make all stack variables saved */
3923 sd.var[copy->varnum].flags |= SAVEDVAR;
3924 copy->flags |= SAVEDVAR;
3929 #endif /* !SUPPORT_DIVISION */
3940 COUNT(count_pcmd_op);
3941 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3946 coalescing_boundary = sd.new;
3947 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3948 bte = iptr->sx.s23.s3.bte;
3951 if (md->memuse > rd->memuse)
3952 rd->memuse = md->memuse;
3953 if (md->argintreguse > rd->argintreguse)
3954 rd->argintreguse = md->argintreguse;
3955 /* XXX non-leaf method? */
3957 /* make all stack variables saved */
3961 sd.var[copy->varnum].flags |= SAVEDVAR;
3962 copy->flags |= SAVEDVAR;
3967 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3972 #if SUPPORT_LONG_LOGICAL
3976 #endif /* SUPPORT_LONG_LOGICAL */
3977 COUNT(count_pcmd_op);
3978 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3984 COUNT(count_pcmd_op);
3985 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3993 COUNT(count_pcmd_op);
3994 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
4002 COUNT(count_pcmd_op);
4003 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
4007 COUNT(count_pcmd_op);
4008 #if SUPPORT_LONG_CMP_CONST
4009 if ((len == 0) || (iptr[1].sx.val.i != 0))
4012 switch (iptr[1].opc) {
4014 iptr->opc = ICMD_IF_LCMPEQ;
4016 iptr->dst.insindex = iptr[1].dst.insindex;
4017 iptr[1].opc = ICMD_NOP;
4019 OP2_BRANCH(TYPE_LNG, TYPE_LNG);
4022 COUNT(count_pcmd_bra);
4025 iptr->opc = ICMD_IF_LCMPNE;
4026 goto icmd_lcmp_if_tail;
4028 iptr->opc = ICMD_IF_LCMPLT;
4029 goto icmd_lcmp_if_tail;
4031 iptr->opc = ICMD_IF_LCMPGT;
4032 goto icmd_lcmp_if_tail;
4034 iptr->opc = ICMD_IF_LCMPLE;
4035 goto icmd_lcmp_if_tail;
4037 iptr->opc = ICMD_IF_LCMPGE;
4038 goto icmd_lcmp_if_tail;
4044 #endif /* SUPPORT_LONG_CMP_CONST */
4045 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
4048 /* XXX why is this deactivated? */
4051 COUNT(count_pcmd_op);
4052 if ((len == 0) || (iptr[1].sx.val.i != 0))
4055 switch (iptr[1].opc) {
4057 iptr->opc = ICMD_IF_FCMPEQ;
4059 iptr->dst.insindex = iptr[1].dst.insindex;
4060 iptr[1].opc = ICMD_NOP;
4062 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4065 COUNT(count_pcmd_bra);
4068 iptr->opc = ICMD_IF_FCMPNE;
4069 goto icmd_if_fcmpl_tail;
4071 iptr->opc = ICMD_IF_FCMPL_LT;
4072 goto icmd_if_fcmpl_tail;
4074 iptr->opc = ICMD_IF_FCMPL_GT;
4075 goto icmd_if_fcmpl_tail;
4077 iptr->opc = ICMD_IF_FCMPL_LE;
4078 goto icmd_if_fcmpl_tail;
4080 iptr->opc = ICMD_IF_FCMPL_GE;
4081 goto icmd_if_fcmpl_tail;
4088 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4092 COUNT(count_pcmd_op);
4093 if ((len == 0) || (iptr[1].sx.val.i != 0))
4096 switch (iptr[1].opc) {
4098 iptr->opc = ICMD_IF_FCMPEQ;
4100 iptr->dst.insindex = iptr[1].dst.insindex;
4101 iptr[1].opc = ICMD_NOP;
4103 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4106 COUNT(count_pcmd_bra);
4109 iptr->opc = ICMD_IF_FCMPNE;
4110 goto icmd_if_fcmpg_tail;
4112 iptr->opc = ICMD_IF_FCMPG_LT;
4113 goto icmd_if_fcmpg_tail;
4115 iptr->opc = ICMD_IF_FCMPG_GT;
4116 goto icmd_if_fcmpg_tail;
4118 iptr->opc = ICMD_IF_FCMPG_LE;
4119 goto icmd_if_fcmpg_tail;
4121 iptr->opc = ICMD_IF_FCMPG_GE;
4122 goto icmd_if_fcmpg_tail;
4129 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4133 COUNT(count_pcmd_op);
4134 if ((len == 0) || (iptr[1].sx.val.i != 0))
4137 switch (iptr[1].opc) {
4139 iptr->opc = ICMD_IF_DCMPEQ;
4141 iptr->dst.insindex = iptr[1].dst.insindex;
4142 iptr[1].opc = ICMD_NOP;
4144 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4147 COUNT(count_pcmd_bra);
4150 iptr->opc = ICMD_IF_DCMPNE;
4151 goto icmd_if_dcmpl_tail;
4153 iptr->opc = ICMD_IF_DCMPL_LT;
4154 goto icmd_if_dcmpl_tail;
4156 iptr->opc = ICMD_IF_DCMPL_GT;
4157 goto icmd_if_dcmpl_tail;
4159 iptr->opc = ICMD_IF_DCMPL_LE;
4160 goto icmd_if_dcmpl_tail;
4162 iptr->opc = ICMD_IF_DCMPL_GE;
4163 goto icmd_if_dcmpl_tail;
4170 OPTT2_1(TYPE_DBL, TYPE_INT);
4174 COUNT(count_pcmd_op);
4175 if ((len == 0) || (iptr[1].sx.val.i != 0))
4178 switch (iptr[1].opc) {
4180 iptr->opc = ICMD_IF_DCMPEQ;
4182 iptr->dst.insindex = iptr[1].dst.insindex;
4183 iptr[1].opc = ICMD_NOP;
4185 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4188 COUNT(count_pcmd_bra);
4191 iptr->opc = ICMD_IF_DCMPNE;
4192 goto icmd_if_dcmpg_tail;
4194 iptr->opc = ICMD_IF_DCMPG_LT;
4195 goto icmd_if_dcmpg_tail;
4197 iptr->opc = ICMD_IF_DCMPG_GT;
4198 goto icmd_if_dcmpg_tail;
4200 iptr->opc = ICMD_IF_DCMPG_LE;
4201 goto icmd_if_dcmpg_tail;
4203 iptr->opc = ICMD_IF_DCMPG_GE;
4204 goto icmd_if_dcmpg_tail;
4211 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4216 COUNT(count_pcmd_op);
4217 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4222 COUNT(count_pcmd_op);
4223 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4232 case ICMD_INT2SHORT:
4233 COUNT(count_pcmd_op);
4234 OP1_1(TYPE_INT, TYPE_INT);
4237 COUNT(count_pcmd_op);
4238 OP1_1(TYPE_LNG, TYPE_LNG);
4241 COUNT(count_pcmd_op);
4242 OP1_1(TYPE_FLT, TYPE_FLT);
4245 COUNT(count_pcmd_op);
4246 OP1_1(TYPE_DBL, TYPE_DBL);
4250 COUNT(count_pcmd_op);
4251 OP1_1(TYPE_INT, TYPE_LNG);
4254 COUNT(count_pcmd_op);
4255 OP1_1(TYPE_INT, TYPE_FLT);
4258 COUNT(count_pcmd_op);
4259 OP1_1(TYPE_INT, TYPE_DBL);
4262 COUNT(count_pcmd_op);
4263 OP1_1(TYPE_LNG, TYPE_INT);
4266 COUNT(count_pcmd_op);
4267 OP1_1(TYPE_LNG, TYPE_FLT);
4270 COUNT(count_pcmd_op);
4271 OP1_1(TYPE_LNG, TYPE_DBL);
4274 COUNT(count_pcmd_op);
4275 OP1_1(TYPE_FLT, TYPE_INT);
4278 COUNT(count_pcmd_op);
4279 OP1_1(TYPE_FLT, TYPE_LNG);
4282 COUNT(count_pcmd_op);
4283 OP1_1(TYPE_FLT, TYPE_DBL);
4286 COUNT(count_pcmd_op);
4287 OP1_1(TYPE_DBL, TYPE_INT);
4290 COUNT(count_pcmd_op);
4291 OP1_1(TYPE_DBL, TYPE_LNG);
4294 COUNT(count_pcmd_op);
4295 OP1_1(TYPE_DBL, TYPE_FLT);
4298 case ICMD_CHECKCAST:
4299 coalescing_boundary = sd.new;
4300 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4301 /* array type cast-check */
4303 bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4306 if (md->memuse > rd->memuse)
4307 rd->memuse = md->memuse;
4308 if (md->argintreguse > rd->argintreguse)
4309 rd->argintreguse = md->argintreguse;
4311 /* make all stack variables saved */
4315 sd.var[copy->varnum].flags |= SAVEDVAR;
4316 copy->flags |= SAVEDVAR;
4320 OP1_1(TYPE_ADR, TYPE_ADR);
4323 case ICMD_INSTANCEOF:
4324 case ICMD_ARRAYLENGTH:
4325 coalescing_boundary = sd.new;
4326 OP1_1(TYPE_ADR, TYPE_INT);
4330 case ICMD_ANEWARRAY:
4331 coalescing_boundary = sd.new;
4332 OP1_1(TYPE_INT, TYPE_ADR);
4336 coalescing_boundary = sd.new;
4337 COUNT(count_check_null);
4338 COUNT(count_pcmd_mem);
4339 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4340 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4345 case ICMD_GETSTATIC:
4346 coalescing_boundary = sd.new;
4347 COUNT(count_pcmd_mem);
4348 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4349 OP0_1(fmiref->parseddesc.fd->type);
4353 coalescing_boundary = sd.new;
4360 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
4361 tbptr->type = BBTYPE_SBR;
4363 assert(sd.bptr->next); /* XXX exception */
4364 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4365 #if defined(ENABLE_VERIFIER)
4366 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4369 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4373 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4375 /* We need to check for overflow right here because
4376 * the pushed value is poped afterwards */
4379 superblockend = true;
4380 /* XXX should not be marked as interface, as it does not need to be */
4381 /* allocated. Same for the invar of the target. */
4384 /* pop many push any */
4388 bte = iptr->sx.s23.s3.bte;
4392 case ICMD_INVOKESTATIC:
4393 case ICMD_INVOKESPECIAL:
4394 case ICMD_INVOKEVIRTUAL:
4395 case ICMD_INVOKEINTERFACE:
4396 COUNT(count_pcmd_met);
4398 /* Check for functions to replace with builtin
4401 if (builtintable_replace_function(iptr))
4404 INSTRUCTION_GET_METHODDESC(iptr, md);
4405 /* XXX resurrect this COUNT? */
4406 /* if (lm->flags & ACC_STATIC) */
4407 /* {COUNT(count_check_null);} */
4411 coalescing_boundary = sd.new;
4415 if (md->memuse > rd->memuse)
4416 rd->memuse = md->memuse;
4417 if (md->argintreguse > rd->argintreguse)
4418 rd->argintreguse = md->argintreguse;
4419 if (md->argfltreguse > rd->argfltreguse)
4420 rd->argfltreguse = md->argfltreguse;
4424 iptr->s1.argcount = stackdepth;
4425 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4428 for (i-- ; i >= 0; i--) {
4429 iptr->sx.s23.s2.args[i] = copy->varnum;
4431 /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4432 /* -> won't help anyway */
4433 if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4435 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4436 /* If we pass float arguments in integer argument registers, we
4437 * are not allowed to precolor them here. Floats have to be moved
4438 * to this regs explicitly in codegen().
4439 * Only arguments that are passed by stack anyway can be precolored
4440 * (michi 2005/07/24) */
4441 if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4442 (!IS_FLT_DBL_TYPE(copy->type)
4443 || md->params[i].inmemory)) {
4445 if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4450 if (md->params[i].inmemory) {
4451 sd.var[copy->varnum].vv.regoff =
4452 md->params[i].regoff;
4453 sd.var[copy->varnum].flags |=
4457 if (IS_FLT_DBL_TYPE(copy->type)) {
4458 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4459 assert(0); /* XXX is this assert ok? */
4461 sd.var[copy->varnum].vv.regoff =
4462 rd->argfltregs[md->params[i].regoff];
4463 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4466 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4467 if (IS_2_WORD_TYPE(copy->type))
4468 sd.var[copy->varnum].vv.regoff =
4469 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4470 rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4473 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4474 sd.var[copy->varnum].vv.regoff =
4475 rd->argintregs[md->params[i].regoff];
4483 /* deal with live-through stack slots "under" the */
4489 iptr->sx.s23.s2.args[i++] = copy->varnum;
4490 sd.var[copy->varnum].flags |= SAVEDVAR;
4491 copy->flags |= SAVEDVAR | PASSTHROUGH;
4495 /* pop the arguments */
4504 /* push the return value */
4506 if (md->returntype.type != TYPE_VOID) {
4507 GET_NEW_VAR(sd, new_index, md->returntype.type);
4508 DST(md->returntype.type, new_index);
4513 case ICMD_INLINE_START:
4514 case ICMD_INLINE_END:
4519 case ICMD_MULTIANEWARRAY:
4520 coalescing_boundary = sd.new;
4521 if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4522 rd->argintreguse = MIN(3, INT_ARG_CNT);
4524 i = iptr->s1.argcount;
4528 iptr->sx.s23.s2.args = DMNEW(s4, i);
4530 #if defined(SPECIALMEMUSE)
4531 # if defined(__DARWIN__)
4532 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4533 rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4535 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4536 rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4539 # if defined(__I386__)
4540 if (rd->memuse < i + 3)
4541 rd->memuse = i + 3; /* n integer args spilled on stack */
4542 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4543 if (rd->memuse < i + 2)
4544 rd->memuse = i + 2; /* 4*4 bytes callee save space */
4547 rd->memuse = i; /* n integer args spilled on stack */
4548 # endif /* defined(__I386__) */
4552 /* check INT type here? Currently typecheck does this. */
4553 iptr->sx.s23.s2.args[i] = copy->varnum;
4554 if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4555 && (!IS_INOUT(copy))
4556 && (!IS_LOCALVAR(copy)) ) {
4557 copy->varkind = ARGVAR;
4558 sd.var[copy->varnum].flags |=
4559 INMEMORY & PREALLOC;
4560 #if defined(SPECIALMEMUSE)
4561 # if defined(__DARWIN__)
4562 sd.var[copy->varnum].vv.regoff = i +
4563 LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4565 sd.var[copy->varnum].vv.regoff = i +
4566 LA_SIZE_IN_POINTERS + 3;
4569 # if defined(__I386__)
4570 sd.var[copy->varnum].vv.regoff = i + 3;
4571 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4572 sd.var[copy->varnum].vv.regoff = i + 2;
4574 sd.var[copy->varnum].vv.regoff = i;
4575 # endif /* defined(__I386__) */
4576 #endif /* defined(SPECIALMEMUSE) */
4581 sd.var[copy->varnum].flags |= SAVEDVAR;
4582 copy->flags |= SAVEDVAR;
4586 i = iptr->s1.argcount;
4591 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4592 DST(TYPE_ADR, new_index);
4598 new_internalerror("Unknown ICMD %d", opcode);
4604 } /* while instructions */
4606 /* stack slots at basic block end become interfaces */
4608 sd.bptr->outdepth = stackdepth;
4609 sd.bptr->outvars = DMNEW(s4, stackdepth);
4612 for (copy = curstack; copy; i--, copy = copy->prev) {
4616 /* with the new vars rd->interfaces will be removed */
4617 /* and all in and outvars have to be STACKVARS! */
4618 /* in the moment i.e. SWAP with in and out vars can */
4619 /* create an unresolvable conflict */
4626 v = sd.var + copy->varnum;
4629 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4630 /* no interface var until now for this depth and */
4632 jd->interface_map[i*5 + t].flags = v->flags;
4635 jd->interface_map[i*5 + t].flags |= v->flags;
4638 sd.bptr->outvars[i] = copy->varnum;
4641 /* check if interface slots at basic block begin must be saved */
4643 for (i=0; i<sd.bptr->indepth; ++i) {
4644 varinfo *v = sd.var + sd.bptr->invars[i];
4651 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4652 /* no interface var until now for this depth and */
4654 jd->interface_map[i*5 + t].flags = v->flags;
4657 jd->interface_map[i*5 + t].flags |= v->flags;
4661 /* store the number of this block's variables */
4663 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4665 #if defined(STACK_VERBOSE)
4666 stack_verbose_block_exit(&sd, superblockend);
4669 /* reach the following block, if any */
4672 if (!stack_reach_next_block(&sd))
4677 } while (sd.repeat && !deadcode);
4679 /* XXX reset TYPE_RET to TYPE_ADR */
4681 for (i=0; i<sd.vartop; ++i) {
4682 if (sd.var[i].type == TYPE_RET)
4683 sd.var[i].type = TYPE_ADR;
4686 /* XXX hack to fix up the ranges of the cloned single-block handlers */
4688 ex = jd->exceptiontable;
4689 for (; ex != NULL; ex = ex->down) {
4690 if (ex->start == ex->end) {
4691 assert(ex->end->next);
4692 ex->end = ex->end->next;
4696 /* store number of created variables */
4698 jd->vartop = sd.vartop;
4700 /* gather statistics *****************************************************/
4702 #if defined(ENABLE_STATISTICS)
4704 if (jd->basicblockcount > count_max_basic_blocks)
4705 count_max_basic_blocks = jd->basicblockcount;
4706 count_basic_blocks += jd->basicblockcount;
4707 if (jd->instructioncount > count_max_javainstr)
4708 count_max_javainstr = jd->instructioncount;
4709 count_javainstr += jd->instructioncount;
4710 if (jd->stackcount > count_upper_bound_new_stack)
4711 count_upper_bound_new_stack = jd->stackcount;
4712 if ((sd.new - jd->stack) > count_max_new_stack)
4713 count_max_new_stack = (sd.new - jd->stack);
4715 sd.bptr = jd->basicblocks;
4716 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4717 if (sd.bptr->flags > BBREACHED) {
4718 if (sd.bptr->indepth >= 10)
4719 count_block_stack[10]++;
4721 count_block_stack[sd.bptr->indepth]++;
4722 len = sd.bptr->icount;
4724 count_block_size_distribution[len]++;
4726 count_block_size_distribution[10]++;
4728 count_block_size_distribution[11]++;
4730 count_block_size_distribution[12]++;
4732 count_block_size_distribution[13]++;
4734 count_block_size_distribution[14]++;
4736 count_block_size_distribution[15]++;
4738 count_block_size_distribution[16]++;
4740 count_block_size_distribution[17]++;
4744 if (iteration_count == 1)
4745 count_analyse_iterations[0]++;
4746 else if (iteration_count == 2)
4747 count_analyse_iterations[1]++;
4748 else if (iteration_count == 3)
4749 count_analyse_iterations[2]++;
4750 else if (iteration_count == 4)
4751 count_analyse_iterations[3]++;
4753 count_analyse_iterations[4]++;
4755 if (jd->basicblockcount <= 5)
4756 count_method_bb_distribution[0]++;
4757 else if (jd->basicblockcount <= 10)
4758 count_method_bb_distribution[1]++;
4759 else if (jd->basicblockcount <= 15)
4760 count_method_bb_distribution[2]++;
4761 else if (jd->basicblockcount <= 20)
4762 count_method_bb_distribution[3]++;
4763 else if (jd->basicblockcount <= 30)
4764 count_method_bb_distribution[4]++;
4765 else if (jd->basicblockcount <= 40)
4766 count_method_bb_distribution[5]++;
4767 else if (jd->basicblockcount <= 50)
4768 count_method_bb_distribution[6]++;
4769 else if (jd->basicblockcount <= 75)
4770 count_method_bb_distribution[7]++;
4772 count_method_bb_distribution[8]++;
4774 #endif /* defined(ENABLE_STATISTICS) */
4776 /* everything's ok *******************************************************/
4780 /* goto labels for throwing verifier exceptions **************************/
4782 #if defined(ENABLE_VERIFIER)
4784 throw_stack_underflow:
4785 exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4788 throw_stack_overflow:
4789 exceptions_throw_verifyerror(m, "Stack size too large");
4792 throw_stack_type_error:
4793 exceptions_throw_verifyerror_for_stack(m, expectedtype);
4796 throw_stack_category_error:
4797 exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4804 /* functions for verbose stack analysis output ********************************/
4806 #if defined(STACK_VERBOSE)
4807 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4809 printf("%c", show_jit_type_letters[v->type]);
4810 if (v->type == TYPE_RET)
4811 printf("{L%03d}", v->vv.retaddr->nr);
4815 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4817 assert(index >= 0 && index < sd->vartop);
4818 stack_verbose_show_varinfo(sd, sd->var + index);
4822 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4826 printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4828 for (i=0; i<bptr->indepth; ++i) {
4831 stack_verbose_show_variable(sd, bptr->invars[i]);
4836 printf("] javalocals [");
4837 for (i=0; i<sd->maxlocals; ++i) {
4840 if (sd->javalocals[i] == UNUSED)
4843 printf("%d", sd->javalocals[i]);
4845 printf("] inlocals [");
4846 if (bptr->inlocals) {
4847 for (i=0; i<sd->localcount; ++i) {
4850 stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4855 printf("] out:%d [", bptr->outdepth);
4856 if (bptr->outvars) {
4857 for (i=0; i<bptr->outdepth; ++i) {
4860 stack_verbose_show_variable(sd, bptr->outvars[i]);
4868 printf(" (clone of L%03d)", bptr->original->nr);
4870 basicblock *b = bptr->copied_to;
4872 printf(" (copied to ");
4873 for (; b; b = b->copied_to)
4874 printf("L%03d ", b->nr);
4881 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4885 printf("======================================== STACK %sANALYSE BLOCK ",
4886 (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4887 stack_verbose_show_block(sd, sd->bptr);
4890 if (sd->handlers[0]) {
4891 printf("HANDLERS: ");
4892 for (i=0; sd->handlers[i]; ++i) {
4893 printf("L%03d ", sd->handlers[i]->handler->nr);
4901 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4903 printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4904 stack_verbose_show_block(sd, sd->bptr);
4908 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackptr curstack)
4916 printf(" javalocals [");
4917 for (i=0; i<sd->maxlocals; ++i) {
4920 if (sd->javalocals[i] == UNUSED)
4923 printf("%d:%c", sd->javalocals[i],
4924 show_jit_type_letters[sd->var[sd->javalocals[i]].type]);
4927 printf("] stack [");
4929 for(i = 0, sp = curstack; sp; sp = sp->prev)
4933 stack = MNEW(stackptr, depth);
4934 for(sp = curstack; sp; sp = sp->prev)
4937 for(i=0; i<depth; ++i) {
4941 v = &(sd->var[sp->varnum]);
4943 if (v->flags & INOUT)
4945 if (v->flags & PREALLOC)
4947 printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
4950 show_icmd(sd->jd, iptr, false, SHOW_PARSE);
4957 * These are local overrides for various environment variables in Emacs.
4958 * Please do not remove this and leave it at the end of the file, where
4959 * Emacs will automagically detect them.
4960 * ---------------------------------------------------------------------
4963 * indent-tabs-mode: t
4967 * vim:noexpandtab:sw=4:ts=4: