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 6206 2006-12-16 20:59:26Z edwin $
47 #include "mm/memory.h"
49 #include "native/native.h"
51 #include "toolbox/logging.h"
53 #include "vm/global.h"
54 #include "vm/builtin.h"
55 #include "vm/options.h"
56 #include "vm/resolve.h"
57 #include "vm/statistics.h"
58 #include "vm/stringlocal.h"
61 #include "vm/jit/abi.h"
62 #include "vm/jit/cfg.h"
63 #include "vm/jit/codegen-common.h"
64 #include "vm/jit/parse.h"
65 #include "vm/jit/show.h"
67 #if defined(ENABLE_DISASSEMBLER)
68 # include "vm/jit/disass.h"
71 #include "vm/jit/jit.h"
72 #include "vm/jit/stack.h"
74 #if defined(ENABLE_SSA)
75 # include "vm/jit/optimizing/lsra.h"
76 # include "vm/jit/optimizing/ssa.h"
77 #elif defined(ENABLE_LSRA)
78 # include "vm/jit/allocator/lsra.h"
81 /*#define STACK_VERBOSE*/
84 /* macro for saving #ifdefs ***************************************************/
86 #if defined(ENABLE_STATISTICS)
87 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr) \
90 if (stackdepth >= 10) \
91 count_store_depth[10]++; \
93 count_store_depth[stackdepth]++; \
96 #else /* !defined(ENABLE_STATISTICS) */
97 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)
101 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
104 /* For returnAddresses we use a field of the typeinfo to store from which */
105 /* subroutine the returnAddress will return, if used. */
106 /* XXX It would be nicer to use typeinfo.typeclass, but the verifier seems */
107 /* to need it initialised to NULL. This should be investigated. */
109 #if defined(ENABLE_VERIFIER)
110 #define SBRSTART typeinfo.elementclass.any
114 /* stackdata_t *****************************************************************
116 This struct holds internal data during stack analysis.
118 *******************************************************************************/
120 typedef struct stackdata_t stackdata_t;
123 basicblock *bptr; /* the current basic block being analysed */
124 stackptr new; /* next free stackelement */
125 s4 vartop; /* next free variable index */
126 s4 localcount; /* number of locals (at the start of var) */
127 s4 varcount; /* maximum number of variables expected */
128 s4 varsallocated; /* total number of variables allocated */
129 s4 maxlocals; /* max. number of Java locals */
130 varinfo *var; /* variable array (same as jd->var) */
131 s4 *javalocals; /* map from Java locals to jd->var indices */
132 methodinfo *m; /* the method being analysed */
133 jitdata *jd; /* current jitdata */
134 basicblock *last_real_block; /* the last block before the empty one */
135 bool repeat; /* if true, iterate the analysis again */
136 exception_entry **handlers; /* exception handlers for the current block */
137 exception_entry *extableend; /* points to the last exception entry */
138 stackelement exstack; /* instack for exception handlers */
142 /* macros for allocating/releasing variable indices *****************/
144 #define GET_NEW_INDEX(sd, new_varindex) \
146 assert((sd).vartop < (sd).varcount); \
147 (new_varindex) = ((sd).vartop)++; \
150 /* Not implemented now - could be used to reuse varindices. */
151 /* Pay attention to not release a localvar once implementing it! */
152 #define RELEASE_INDEX(sd, varindex)
154 #define GET_NEW_VAR(sd, newvarindex, newtype) \
156 GET_NEW_INDEX((sd), (newvarindex)); \
157 (sd).var[newvarindex].type = (newtype); \
161 /* macros for querying variable properties **************************/
163 #define IS_INOUT(sp) \
164 (sd.var[(sp)->varnum].flags & INOUT)
166 #define IS_PREALLOC(sp) \
167 (sd.var[(sp)->varnum].flags & PREALLOC)
169 #define IS_TEMPVAR(sp) \
170 ( ((sp)->varnum >= sd.localcount) \
171 && !(sd.var[(sp)->varnum].flags & (INOUT | PREALLOC)) )
174 #define IS_LOCALVAR_SD(sd, sp) \
175 ((sp)->varnum < (sd).localcount)
177 #define IS_LOCALVAR(sp) \
178 IS_LOCALVAR_SD(sd, (sp))
181 /* macros for setting variable properties ****************************/
183 #define SET_TEMPVAR(sp) \
185 if (IS_LOCALVAR((sp))) { \
186 stack_change_to_tempvar(&sd, (sp), iptr); \
188 sd.var[(sp)->varnum].flags &= ~(INOUT | PREALLOC); \
191 #define SET_PREALLOC(sp) \
193 assert(!IS_LOCALVAR((sp))); \
194 sd.var[(sp)->varnum].flags |= PREALLOC; \
198 /* macros for source operands ***************************************/
201 (iptr->s1.varindex = -1)
203 #define USE_S1(type1) \
206 CHECK_BASIC_TYPE(type1, curstack->type); \
207 iptr->s1.varindex = curstack->varnum; \
213 iptr->s1.varindex = curstack->varnum; \
216 #define USE_S1_S2(type1, type2) \
219 CHECK_BASIC_TYPE(type1, curstack->prev->type); \
220 CHECK_BASIC_TYPE(type2, curstack->type); \
221 iptr->sx.s23.s2.varindex = curstack->varnum; \
222 iptr->s1.varindex = curstack->prev->varnum; \
225 #define USE_S1_S2_ANY_ANY \
228 iptr->sx.s23.s2.varindex = curstack->varnum; \
229 iptr->s1.varindex = curstack->prev->varnum; \
232 #define USE_S1_S2_S3(type1, type2, type3) \
235 CHECK_BASIC_TYPE(type1, curstack->prev->prev->type); \
236 CHECK_BASIC_TYPE(type2, curstack->prev->type); \
237 CHECK_BASIC_TYPE(type3, curstack->type); \
238 iptr->sx.s23.s3.varindex = curstack->varnum; \
239 iptr->sx.s23.s2.varindex = curstack->prev->varnum; \
240 iptr->s1.varindex = curstack->prev->prev->varnum; \
243 /* The POPANY macro does NOT check stackdepth, or set stackdepth! */
246 if (curstack->varkind == UNDEFVAR) \
247 curstack->varkind = TEMPVAR; \
248 curstack = curstack->prev; \
251 #define POP_S1(type1) \
254 if (curstack->varkind == UNDEFVAR) \
255 curstack->varkind = TEMPVAR; \
256 curstack = curstack->prev; \
262 if (curstack->varkind == UNDEFVAR) \
263 curstack->varkind = TEMPVAR; \
264 curstack = curstack->prev; \
267 #define POP_S1_S2(type1, type2) \
269 USE_S1_S2(type1, type2); \
270 if (curstack->varkind == UNDEFVAR) \
271 curstack->varkind = TEMPVAR; \
272 if (curstack->prev->varkind == UNDEFVAR) \
273 curstack->prev->varkind = TEMPVAR; \
274 curstack = curstack->prev->prev; \
277 #define POP_S1_S2_ANY_ANY \
280 if (curstack->varkind == UNDEFVAR) \
281 curstack->varkind = TEMPVAR; \
282 if (curstack->prev->varkind == UNDEFVAR) \
283 curstack->prev->varkind = TEMPVAR; \
284 curstack = curstack->prev->prev; \
287 #define POP_S1_S2_S3(type1, type2, type3) \
289 USE_S1_S2_S3(type1, type2, type3); \
290 if (curstack->varkind == UNDEFVAR) \
291 curstack->varkind = TEMPVAR; \
292 if (curstack->prev->varkind == UNDEFVAR) \
293 curstack->prev->varkind = TEMPVAR; \
294 if (curstack->prev->prev->varkind == UNDEFVAR) \
295 curstack->prev->prev->varkind = TEMPVAR; \
296 curstack = curstack->prev->prev->prev; \
303 /* macros for setting the destination operand ***********************/
306 (iptr->dst.varindex = -1)
308 #define DST(typed, index) \
310 NEWSTACKn((typed),(index)); \
311 curstack->creator = iptr; \
312 iptr->dst.varindex = (index); \
315 #define DST_LOCALVAR(typed, index) \
317 NEWSTACK((typed), LOCALVAR, (index)); \
318 curstack->creator = iptr; \
319 iptr->dst.varindex = (index); \
323 /* macro for propagating constant values ****************************/
325 #if defined(ENABLE_VERIFIER)
326 #define COPY_VAL_AND_TYPE_VAR(sv, dv) \
328 if (((dv)->type = (sv)->type) == TYPE_RET) { \
329 (dv)->vv = (sv)->vv; \
330 (dv)->SBRSTART = (sv)->SBRSTART; \
334 #define COPY_VAL_AND_TYPE_VAR(sv, dv) \
336 (dv)->type = (sv)->type; \
337 if (((dv)->type = (sv)->type) == TYPE_RET) { \
338 (dv)->vv = (sv)->vv; \
343 #define COPY_VAL_AND_TYPE(sd, sindex, dindex) \
344 COPY_VAL_AND_TYPE_VAR((sd).var + (sindex), (sd).var + (dindex))
347 /* stack modelling macros *******************************************/
349 #define OP0_1(typed) \
352 GET_NEW_VAR(sd, new_index, (typed)); \
353 DST((typed), new_index); \
364 #define OP1_BRANCH(type1) \
370 #define OP1_1(type1, typed) \
373 GET_NEW_VAR(sd, new_index, (typed)); \
374 DST(typed, new_index); \
377 #define OP2_1(type1, type2, typed) \
379 POP_S1_S2(type1, type2); \
380 GET_NEW_VAR(sd, new_index, (typed)); \
381 DST(typed, new_index); \
396 #define OP1_0(type1) \
403 #define OP2_0(type1, type2) \
405 POP_S1_S2(type1, type2); \
410 #define OP2_BRANCH(type1, type2) \
412 POP_S1_S2(type1, type2); \
416 #define OP2_0_ANY_ANY \
423 #define OP3_0(type1, type2, type3) \
425 POP_S1_S2_S3(type1, type2, type3); \
430 #define LOAD(type1, index) \
432 DST_LOCALVAR(type1, index); \
436 #define STORE(type1, index) \
443 /* macros for DUP elimination ***************************************/
445 /* XXX replace NEW_VAR with NEW_INDEX */
446 #define DUP_SLOT(sp) \
448 GET_NEW_VAR(sd, new_index, (sp)->type); \
449 COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
450 NEWSTACK((sp)->type, TEMPVAR, new_index); \
453 /* does not check input stackdepth */
454 #define MOVE_UP(sp) \
456 iptr->opc = ICMD_MOVE; \
457 iptr->s1.varindex = (sp)->varnum; \
459 curstack->creator = iptr; \
460 iptr->dst.varindex = curstack->varnum; \
464 /* does not check input stackdepth */
465 #define COPY_UP(sp) \
468 iptr->opc = ICMD_COPY; \
469 iptr->s1.varindex = (sp)->varnum; \
471 curstack->creator = iptr; \
472 iptr->dst.varindex = curstack->varnum; \
476 #define COPY_DOWN(s, d) \
479 iptr->opc = ICMD_COPY; \
480 iptr->s1.varindex = (s)->varnum; \
481 iptr->dst.varindex = (d)->varnum; \
482 (d)->creator = iptr; \
485 #define MOVE_TO_TEMP(sp) \
487 GET_NEW_INDEX(sd, new_index); \
488 iptr->opc = ICMD_MOVE; \
489 iptr->s1.varindex = (sp)->varnum; \
490 iptr->dst.varindex = new_index; \
491 COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
492 (sp)->varnum = new_index; \
493 (sp)->varkind = TEMPVAR; \
496 /* macros for branching / reaching basic blocks *********************/
498 #define BRANCH_TARGET(bt, tempbptr) \
500 tempbptr = BLOCK_OF((bt).insindex); \
501 tempbptr = stack_mark_reached(&sd, tempbptr, curstack, \
503 if (tempbptr == NULL) \
505 (bt).block = tempbptr; \
508 #define BRANCH(tempbptr) \
509 BRANCH_TARGET(iptr->dst, tempbptr)
512 /* forward declarations *******************************************************/
514 static void stack_create_invars(stackdata_t *sd, basicblock *b,
515 stackptr curstack, int stackdepth);
516 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b);
518 #if defined(STACK_VERBOSE)
519 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v);
520 static void stack_verbose_show_variable(stackdata_t *sd, s4 index);
521 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr);
522 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse);
523 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend);
524 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr,
529 /* stack_init ******************************************************************
531 Initialized the stack analysis subsystem (called by jit_init).
533 *******************************************************************************/
535 bool stack_init(void)
541 /* stack_grow_variable_array ***************************************************
543 Grow the variable array so the given number of additional variables fits in.
544 The number is added to `varcount`, which is the maximum number of variables
545 we expect to need at this point. The actual number of variables
546 (`varsallocated`) may be larger than that, in order to avoid too many
550 sd...........stack analysis data
551 num..........number of additional variables
553 *******************************************************************************/
555 static void stack_grow_variable_array(stackdata_t *sd, s4 num)
561 if (sd->varcount + num > sd->varsallocated) {
562 newsize = 2*sd->varsallocated + num;
564 sd->var = DMREALLOC(sd->var, varinfo, sd->varsallocated, newsize);
565 MZERO(sd->var + sd->varsallocated, varinfo, (newsize - sd->varsallocated));
566 sd->varsallocated = newsize;
567 sd->jd->var = sd->var;
571 sd->jd->varcount += num;
573 assert(sd->varcount <= sd->varsallocated);
577 /* stack_append_block **********************************************************
579 Append the given block after the last real block of the method (before
580 the pseudo-block at the end).
583 sd...........stack analysis data
584 b............the block to append
586 *******************************************************************************/
588 static void stack_append_block(stackdata_t *sd, basicblock *b)
590 #if defined(STACK_VERBOSE)
591 printf("APPENDING BLOCK L%0d\n", b->nr);
594 b->next = sd->last_real_block->next;
595 sd->last_real_block->next = b;
596 sd->last_real_block = b;
597 b->nr = sd->jd->basicblockcount++;
598 b->next->nr = b->nr + 1;
602 /* stack_clone_block ***********************************************************
604 Create a copy of the given block and insert it at the end of the method.
606 CAUTION: This function does not copy the any variables or the instruction
607 list. It _does_, however, reserve space for the block's invars in the
611 sd...........stack analysis data
612 b............the block to clone
615 a pointer to the copy
617 *******************************************************************************/
619 static basicblock * stack_clone_block(stackdata_t *sd, basicblock *b)
623 clone = DNEW(basicblock);
626 clone->iinstr = NULL;
627 clone->inlocals = NULL;
628 clone->javalocals = NULL;
629 clone->invars = NULL;
631 clone->original = (b->original) ? b->original : b;
632 clone->copied_to = clone->original->copied_to;
633 clone->original->copied_to = clone;
635 clone->flags = BBREACHED;
637 stack_append_block(sd, clone);
639 /* reserve space for the invars of the clone */
641 stack_grow_variable_array(sd, b->indepth);
643 #if defined(STACK_VERBOSE)
644 printf("cloning block L%03d ------> L%03d\n", b->nr, clone->nr);
651 /* stack_create_locals *********************************************************
653 Create the local variables for the start of the given basic block.
656 sd...........stack analysis data
657 b............block to create the locals for
659 *******************************************************************************/
661 static void stack_create_locals(stackdata_t *sd, basicblock *b)
667 /* copy the current state of the local variables */
668 /* (one extra local is needed by the verifier) */
670 dv = DMNEW(varinfo, sd->localcount + VERIFIER_EXTRA_LOCALS);
672 for (i=0; i<sd->localcount; ++i)
675 /* the current map from java locals to cacao variables */
677 jl = DMNEW(s4, sd->maxlocals);
679 MCOPY(jl, sd->javalocals, s4, sd->maxlocals);
683 /* stack_merge_locals **********************************************************
685 Merge local variables at the beginning of the given basic block.
688 sd...........stack analysis data
689 b............the block that is reached
691 *******************************************************************************/
693 static void stack_merge_locals(stackdata_t *sd, basicblock *b)
699 /* If a javalocal is mapped to different cacao locals along the */
700 /* incoming control-flow edges, it becomes undefined. */
702 for (i=0; i<sd->maxlocals; ++i) {
703 if (b->javalocals[i] != UNUSED && b->javalocals[i] != sd->javalocals[i]) {
704 b->javalocals[i] = UNUSED;
705 if (b->flags >= BBFINISHED)
706 b->flags = BBTYPECHECK_REACHED;
707 if (b->nr <= sd->bptr->nr)
712 #if defined(ENABLE_VERIFIER)
714 for (i=0; i<sd->localcount; ++i) {
715 dv = b->inlocals + i;
717 if ((sv->type == TYPE_RET && dv->type == TYPE_RET)
718 && (sv->SBRSTART != dv->SBRSTART))
720 dv->type = TYPE_VOID;
721 if (b->flags >= BBFINISHED)
722 b->flags = BBTYPECHECK_REACHED;
723 sd->repeat = true; /* This is very rare, so just repeat */
727 #endif /* defined(ENABLE_VERIFIER) */
731 /* stack_create_invars *********************************************************
733 Create the invars for the given basic block. Also make a copy of the locals.
736 sd...........stack analysis data
737 b............block to create the invars for
738 curstack.....current stack top
739 stackdepth...current stack depth
741 This function creates STACKDEPTH invars and sets their types to the
742 types to the types of the corresponding slot in the current stack.
744 *******************************************************************************/
746 static void stack_create_invars(stackdata_t *sd, basicblock *b,
747 stackptr curstack, int stackdepth)
755 assert(sd->vartop + stackdepth <= sd->varcount);
757 b->indepth = stackdepth;
758 b->invars = DMNEW(s4, stackdepth);
760 /* allocate the variable indices */
761 index = (sd->vartop += stackdepth);
764 for (sp = curstack; i--; sp = sp->prev) {
765 b->invars[i] = --index;
766 dv = sd->var + index;
767 sv = sd->var + sp->varnum;
769 COPY_VAL_AND_TYPE_VAR(sv, dv);
772 stack_create_locals(sd, b);
776 /* stack_create_invars_from_outvars ********************************************
778 Create the invars for the given basic block. Also make a copy of the locals.
779 Types are propagated from the outvars of the current block.
782 sd...........stack analysis data
783 b............block to create the invars for
785 *******************************************************************************/
787 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b)
793 n = sd->bptr->outdepth;
794 assert(sd->vartop + n <= sd->varcount);
797 b->invars = DMNEW(s4, n);
800 dv = sd->var + sd->vartop;
802 /* allocate the invars */
804 for (i=0; i<n; ++i, ++dv) {
805 sv = sd->var + sd->bptr->outvars[i];
806 b->invars[i] = sd->vartop++;
808 COPY_VAL_AND_TYPE_VAR(sv, dv);
812 stack_create_locals(sd, b);
816 /* stack_check_invars **********************************************************
818 Check the current stack against the invars of the given basic block.
819 Depth and types must match.
822 sd...........stack analysis data
823 b............block which invars to check against
824 curstack.....current stack top
825 stackdepth...current stack depth
829 NULL.........a VerifyError has been thrown
831 *******************************************************************************/
833 static basicblock * stack_check_invars(stackdata_t *sd, basicblock *b,
834 stackptr curstack, int stackdepth)
843 #if defined(STACK_VERBOSE)
844 printf("stack_check_invars(L%03d)\n", b->nr);
847 /* find original of b */
852 #if defined(STACK_VERBOSE)
853 printf("original is L%03d\n", orig->nr);
858 #if defined(ENABLE_VERIFIER)
859 if (i != stackdepth) {
860 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
868 #if defined(STACK_VERBOSE)
869 printf("checking against ");
870 stack_verbose_show_block(sd, b); printf("\n");
874 for (i = orig->indepth; i--; sp = sp->prev) {
875 dv = sd->var + b->invars[i];
876 sv = sd->var + sp->varnum;
878 #if defined(ENABLE_VERIFIER)
879 if (dv->type != sp->type) {
880 exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
885 if (sp->type == TYPE_RET) {
886 #if defined(ENABLE_VERIFIER)
887 if (dv->SBRSTART != sv->SBRSTART) {
888 exceptions_throw_verifyerror(sd->m, "Mismatched stack types");
892 if (dv->vv.retaddr != sv->vv.retaddr) {
894 /* don't break! have to check the remaining stackslots */
900 for (i=0; i<sd->localcount; ++i) {
901 dv = b->inlocals + i;
903 if (sv->type == TYPE_RET && dv->type == TYPE_RET) {
905 #if defined(ENABLE_VERIFIER)
906 (sv->SBRSTART == dv->SBRSTART) &&
908 (sv->vv.retaddr != dv->vv.retaddr))
918 /* XXX cascading collapse? */
920 stack_merge_locals(sd, b);
922 #if defined(STACK_VERBOSE)
923 printf("------> using L%03d\n", b->nr);
927 } while ((b = b->copied_to) != NULL);
929 b = stack_clone_block(sd, orig);
933 stack_create_invars(sd, b, curstack, stackdepth);
938 /* stack_check_invars_from_outvars *********************************************
940 Check the outvars of the current block against the invars of the given block.
941 Depth and types must match.
944 sd...........stack analysis data
945 b............block which invars to check against
949 NULL.........a VerifyError has been thrown
951 *******************************************************************************/
953 static basicblock * stack_check_invars_from_outvars(stackdata_t *sd, basicblock *b)
961 #if defined(STACK_VERBOSE)
962 printf("stack_check_invars_from_outvars(L%03d)\n", b->nr);
965 /* find original of b */
970 #if defined(STACK_VERBOSE)
971 printf("original is L%03d\n", orig->nr);
975 n = sd->bptr->outdepth;
977 #if defined(ENABLE_VERIFIER)
979 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
987 #if defined(STACK_VERBOSE)
988 printf("checking against ");
989 stack_verbose_show_block(sd, b); printf("\n");
993 dv = sd->var + b->invars[0];
995 for (i=0; i<n; ++i, ++dv) {
996 sv = sd->var + sd->bptr->outvars[i];
998 #if defined(ENABLE_VERIFIER)
999 if (sv->type != dv->type) {
1000 exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
1005 if (dv->type == TYPE_RET) {
1006 #if defined(ENABLE_VERIFIER)
1007 if (sv->SBRSTART != dv->SBRSTART) {
1008 exceptions_throw_verifyerror(sd->m, "Mismatched stack types");
1012 if (sv->vv.retaddr != dv->vv.retaddr) {
1014 /* don't break! have to check the remaining stackslots */
1021 for (i=0; i<sd->localcount; ++i) {
1022 dv = b->inlocals + i;
1025 #if defined(ENABLE_VERIFIER)
1026 (sv->SBRSTART == dv->SBRSTART) &&
1028 (sv->type == TYPE_RET && dv->type == TYPE_RET))
1030 if (sv->vv.retaddr != dv->vv.retaddr) {
1039 /* XXX cascading collapse? */
1041 stack_merge_locals(sd, b);
1043 #if defined(STACK_VERBOSE)
1044 printf("------> using L%03d\n", b->nr);
1048 } while ((b = b->copied_to) != NULL);
1050 b = stack_clone_block(sd, orig);
1054 stack_create_invars_from_outvars(sd, b);
1059 /* stack_create_instack ********************************************************
1061 Create the instack of the current basic block.
1064 sd...........stack analysis data
1067 the current stack top at the start of the basic block.
1069 *******************************************************************************/
1071 static stackptr stack_create_instack(stackdata_t *sd)
1077 if ((depth = sd->bptr->indepth) == 0)
1080 sp = (sd->new += depth);
1084 index = sd->bptr->invars[depth];
1086 sp->type = sd->var[index].type;
1090 sp->varkind = STACKVAR;
1094 /* return the top of the created stack */
1099 /* stack_mark_reached **********************************************************
1101 Mark the given block reached and propagate the current stack and locals to
1102 it. This function specializes the target block, if necessary, and returns
1103 a pointer to the specialized target.
1106 sd...........stack analysis data
1107 b............the block to reach
1108 curstack.....the current stack top
1109 stackdepth...the current stack depth
1112 a pointer to (a specialized version of) the target
1113 NULL.........a VerifyError has been thrown
1115 *******************************************************************************/
1117 static basicblock *stack_mark_reached(stackdata_t *sd, basicblock *b, stackptr curstack, int stackdepth)
1119 #if defined(STACK_VERBOSE)
1120 printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1122 /* mark targets of backward branches */
1123 if (b->nr <= sd->bptr->nr)
1124 b->bitflags |= BBFLAG_REPLACEMENT;
1126 if (b->flags < BBREACHED) {
1127 /* b is reached for the first time. Create its invars. */
1129 #if defined(STACK_VERBOSE)
1130 printf("reached L%03d for the first time\n", b->nr);
1133 stack_create_invars(sd, b, curstack, stackdepth);
1135 b->flags = BBREACHED;
1140 /* b has been reached before. Check that its invars match. */
1142 return stack_check_invars(sd, b, curstack, stackdepth);
1147 /* stack_mark_reached_from_outvars *********************************************
1149 Mark the given block reached and propagate the outvars of the current block
1150 and the current locals to it. This function specializes the target block,
1151 if necessary, and returns a pointer to the specialized target.
1154 sd...........stack analysis data
1155 b............the block to reach
1158 a pointer to (a specialized version of) the target
1159 NULL.........a VerifyError has been thrown
1161 *******************************************************************************/
1163 static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
1165 #if defined(STACK_VERBOSE)
1166 printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1168 /* mark targets of backward branches */
1169 if (b->nr <= sd->bptr->nr)
1170 b->bitflags |= BBFLAG_REPLACEMENT;
1172 if (b->flags < BBREACHED) {
1173 /* b is reached for the first time. Create its invars. */
1175 #if defined(STACK_VERBOSE)
1176 printf("reached L%03d for the first time\n", b->nr);
1179 stack_create_invars_from_outvars(sd, b);
1181 b->flags = BBREACHED;
1186 /* b has been reached before. Check that its invars match. */
1188 return stack_check_invars_from_outvars(sd, b);
1193 /* stack_reach_next_block ******************************************************
1195 Mark the following block reached and propagate the outvars of the current block
1196 and the current locals to it. This function specializes the target block,
1197 if necessary, and returns a pointer to the specialized target.
1200 sd...........stack analysis data
1203 a pointer to (a specialized version of) the following block
1204 NULL.........a VerifyError has been thrown
1206 *******************************************************************************/
1208 static bool stack_reach_next_block(stackdata_t *sd)
1213 tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
1214 tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
1218 if (tbptr != sd->bptr->next) {
1219 #if defined(STACK_VERBOSE)
1220 printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
1222 iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
1223 assert(iptr->opc == ICMD_NOP);
1224 iptr->opc = ICMD_GOTO;
1225 iptr->dst.block = tbptr;
1227 if (tbptr->flags < BBFINISHED)
1228 sd->repeat = true; /* XXX check if we really need to repeat */
1235 /* stack_reach_handlers ********************************************************
1237 Reach the exception handlers for the current block.
1240 sd...........stack analysis data
1243 true.........everything ok
1244 false........a VerifyError has been thrown
1246 *******************************************************************************/
1248 static bool stack_reach_handlers(stackdata_t *sd)
1253 #if defined(STACK_VERBOSE)
1254 printf("reaching exception handlers...\n");
1257 for (i=0; sd->handlers[i]; ++i) {
1258 tbptr = sd->handlers[i]->handler;
1260 tbptr->type = BBTYPE_EXH;
1261 tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
1263 /* reach (and specialize) the handler block */
1265 tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
1270 sd->handlers[i]->handler = tbptr;
1277 /* stack_reanalyse_block ******************************************************
1279 Re-analyse the current block. This is called if either the block itself
1280 has already been analysed before, or the current block is a clone of an
1281 already analysed block, and this clone is reached for the first time.
1282 In the latter case, this function does all that is necessary for fully
1283 cloning the block (cloning the instruction list and variables, etc.).
1286 sd...........stack analysis data
1289 true.........everything ok
1290 false........a VerifyError has been thrown
1292 *******************************************************************************/
1294 #define RELOCATE(index) \
1296 if ((index) >= blockvarstart) \
1297 (index) += blockvarshift; \
1298 else if ((index) >= invarstart) \
1299 (index) += invarshift; \
1302 bool stack_reanalyse_block(stackdata_t *sd)
1314 branch_target_t *table;
1315 lookup_target_t *lookup;
1317 bool cloneinstructions;
1318 exception_entry *ex;
1320 #if defined(STACK_VERBOSE)
1321 stack_verbose_block_enter(sd, true);
1328 assert(orig != NULL);
1330 /* clone the instruction list */
1332 cloneinstructions = true;
1334 assert(orig->iinstr);
1336 iptr = DMNEW(instruction, len + 1);
1338 MCOPY(iptr, orig->iinstr, instruction, len);
1339 iptr[len].opc = ICMD_NOP;
1341 iptr[len].flags.bits = 0;
1345 /* reserve space for the clone's block variables */
1347 stack_grow_variable_array(sd, orig->varcount);
1349 /* we already have the invars set */
1351 assert(b->indepth == orig->indepth);
1353 /* calculate relocation shifts for invars and block variables */
1355 if (orig->indepth) {
1356 invarstart = orig->invars[0];
1357 invarshift = b->invars[0] - invarstart;
1360 invarstart = INT_MAX;
1363 blockvarstart = orig->varstart;
1364 blockvarshift = sd->vartop - blockvarstart;
1366 /* copy block variables */
1368 b->varstart = sd->vartop;
1369 b->varcount = orig->varcount;
1370 sd->vartop += b->varcount;
1371 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1375 b->outdepth = orig->outdepth;
1376 b->outvars = DMNEW(s4, orig->outdepth);
1377 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1379 /* clone exception handlers */
1381 for (i=0; sd->handlers[i]; ++i) {
1382 ex = DNEW(exception_entry);
1383 ex->handler = sd->handlers[i]->handler;
1385 ex->end = b; /* XXX hack, see end of stack_analyse */
1386 ex->catchtype = sd->handlers[i]->catchtype;
1389 assert(sd->extableend->down == NULL);
1390 sd->extableend->down = ex;
1391 sd->extableend = ex;
1392 sd->jd->exceptiontablelength++;
1394 sd->handlers[i] = ex;
1398 cloneinstructions = false;
1401 invarstart = sd->vartop;
1402 blockvarstart = sd->vartop;
1407 /* find exception handlers for the cloned block */
1409 ex = sd->jd->exceptiontable;
1410 for (; ex != NULL; ex = ex->down) {
1411 /* XXX the cloned exception handlers have identical */
1412 /* start end end blocks. */
1413 if ((ex->start == b) && (ex->end == b)) {
1414 sd->handlers[len++] = ex;
1417 sd->handlers[len] = NULL;
1420 #if defined(STACK_VERBOSE)
1421 printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1422 printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1425 /* mark block as finished */
1427 b->flags = BBFINISHED;
1429 /* initialize locals at the start of this block */
1432 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1434 MCOPY(sd->javalocals, b->javalocals, s4, sd->maxlocals);
1436 /* reach exception handlers for this block */
1438 if (!stack_reach_handlers(sd))
1441 superblockend = false;
1443 for (len = b->icount; len--; iptr++) {
1444 #if defined(STACK_VERBOSE)
1445 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1449 switch (iptr->opc) {
1451 j = iptr->s1.varindex;
1453 #if defined(ENABLE_VERIFIER)
1454 if (sd->var[j].type != TYPE_RET) {
1455 exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1460 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1461 superblockend = true;
1465 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1466 RELOCATE(iptr->dst.varindex);
1467 superblockend = true;
1471 superblockend = true;
1474 case ICMD_CHECKNULL:
1475 case ICMD_PUTSTATICCONST:
1483 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1484 superblockend = true;
1487 /* pop 0 push 1 const */
1495 /* pop 0 push 1 load */
1502 RELOCATE(iptr->dst.varindex);
1515 RELOCATE(iptr->sx.s23.s2.varindex);
1516 RELOCATE(iptr->s1.varindex);
1517 RELOCATE(iptr->dst.varindex);
1530 RELOCATE(iptr->sx.s23.s3.varindex);
1531 RELOCATE(iptr->sx.s23.s2.varindex);
1532 RELOCATE(iptr->s1.varindex);
1535 /* pop 1 push 0 store */
1542 RELOCATE(iptr->s1.varindex);
1544 j = iptr->dst.varindex;
1545 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1546 i = iptr->sx.s23.s3.javaindex;
1547 if (iptr->flags.bits & INS_FLAG_RETADDR) {
1548 iptr->sx.s23.s2.retaddrnr =
1549 UNUSED - (1 + sd->var[j].vv.retaddr->nr);
1550 sd->javalocals[i] = iptr->sx.s23.s2.retaddrnr;
1553 sd->javalocals[i] = j;
1554 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
1555 sd->javalocals[i-1] = UNUSED;
1556 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
1557 sd->javalocals[i+1] = UNUSED;
1568 RELOCATE(iptr->s1.varindex);
1569 superblockend = true;
1572 case ICMD_PUTSTATIC:
1573 case ICMD_PUTFIELDCONST:
1575 RELOCATE(iptr->s1.varindex);
1578 /* pop 1 push 0 branch */
1581 case ICMD_IFNONNULL:
1596 RELOCATE(iptr->s1.varindex);
1597 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1600 /* pop 1 push 0 table branch */
1602 case ICMD_TABLESWITCH:
1603 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1605 if (cloneinstructions) {
1606 table = DMNEW(branch_target_t, i);
1607 MCOPY(table, iptr->dst.table, branch_target_t, i);
1608 iptr->dst.table = table;
1611 table = iptr->dst.table;
1614 RELOCATE(iptr->s1.varindex);
1616 table->block = stack_mark_reached_from_outvars(sd, table->block);
1619 superblockend = true;
1622 case ICMD_LOOKUPSWITCH:
1623 i = iptr->sx.s23.s2.lookupcount;
1624 if (cloneinstructions) {
1625 lookup = DMNEW(lookup_target_t, i);
1626 MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1627 iptr->dst.lookup = lookup;
1630 lookup = iptr->dst.lookup;
1632 RELOCATE(iptr->s1.varindex);
1634 lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1637 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1638 superblockend = true;
1641 case ICMD_MONITORENTER:
1642 case ICMD_MONITOREXIT:
1643 RELOCATE(iptr->s1.varindex);
1646 /* pop 2 push 0 branch */
1648 case ICMD_IF_ICMPEQ:
1649 case ICMD_IF_ICMPNE:
1650 case ICMD_IF_ICMPLT:
1651 case ICMD_IF_ICMPGE:
1652 case ICMD_IF_ICMPGT:
1653 case ICMD_IF_ICMPLE:
1655 case ICMD_IF_LCMPEQ:
1656 case ICMD_IF_LCMPNE:
1657 case ICMD_IF_LCMPLT:
1658 case ICMD_IF_LCMPGE:
1659 case ICMD_IF_LCMPGT:
1660 case ICMD_IF_LCMPLE:
1662 case ICMD_IF_FCMPEQ:
1663 case ICMD_IF_FCMPNE:
1665 case ICMD_IF_FCMPL_LT:
1666 case ICMD_IF_FCMPL_GE:
1667 case ICMD_IF_FCMPL_GT:
1668 case ICMD_IF_FCMPL_LE:
1670 case ICMD_IF_FCMPG_LT:
1671 case ICMD_IF_FCMPG_GE:
1672 case ICMD_IF_FCMPG_GT:
1673 case ICMD_IF_FCMPG_LE:
1675 case ICMD_IF_DCMPEQ:
1676 case ICMD_IF_DCMPNE:
1678 case ICMD_IF_DCMPL_LT:
1679 case ICMD_IF_DCMPL_GE:
1680 case ICMD_IF_DCMPL_GT:
1681 case ICMD_IF_DCMPL_LE:
1683 case ICMD_IF_DCMPG_LT:
1684 case ICMD_IF_DCMPG_GE:
1685 case ICMD_IF_DCMPG_GT:
1686 case ICMD_IF_DCMPG_LE:
1688 case ICMD_IF_ACMPEQ:
1689 case ICMD_IF_ACMPNE:
1690 RELOCATE(iptr->sx.s23.s2.varindex);
1691 RELOCATE(iptr->s1.varindex);
1692 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1698 case ICMD_IASTORECONST:
1699 case ICMD_LASTORECONST:
1700 case ICMD_AASTORECONST:
1701 case ICMD_BASTORECONST:
1702 case ICMD_CASTORECONST:
1703 case ICMD_SASTORECONST:
1705 RELOCATE(iptr->sx.s23.s2.varindex);
1706 RELOCATE(iptr->s1.varindex);
1709 /* pop 0 push 1 copy */
1713 RELOCATE(iptr->dst.varindex);
1714 RELOCATE(iptr->s1.varindex);
1715 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1757 RELOCATE(iptr->sx.s23.s2.varindex);
1758 RELOCATE(iptr->s1.varindex);
1759 RELOCATE(iptr->dst.varindex);
1764 case ICMD_CHECKCAST:
1765 case ICMD_ARRAYLENGTH:
1766 case ICMD_INSTANCEOF:
1768 case ICMD_ANEWARRAY:
1770 case ICMD_IADDCONST:
1771 case ICMD_ISUBCONST:
1772 case ICMD_IMULCONST:
1776 case ICMD_IANDCONST:
1778 case ICMD_IXORCONST:
1779 case ICMD_ISHLCONST:
1780 case ICMD_ISHRCONST:
1781 case ICMD_IUSHRCONST:
1782 case ICMD_LADDCONST:
1783 case ICMD_LSUBCONST:
1784 case ICMD_LMULCONST:
1788 case ICMD_LANDCONST:
1790 case ICMD_LXORCONST:
1791 case ICMD_LSHLCONST:
1792 case ICMD_LSHRCONST:
1793 case ICMD_LUSHRCONST:
1797 case ICMD_INT2SHORT:
1813 RELOCATE(iptr->s1.varindex);
1814 RELOCATE(iptr->dst.varindex);
1819 case ICMD_GETSTATIC:
1821 RELOCATE(iptr->dst.varindex);
1824 /* pop many push any */
1826 case ICMD_INVOKESTATIC:
1827 case ICMD_INVOKESPECIAL:
1828 case ICMD_INVOKEVIRTUAL:
1829 case ICMD_INVOKEINTERFACE:
1831 case ICMD_MULTIANEWARRAY:
1832 i = iptr->s1.argcount;
1833 if (cloneinstructions) {
1834 argp = DMNEW(s4, i);
1835 MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1836 iptr->sx.s23.s2.args = argp;
1839 argp = iptr->sx.s23.s2.args;
1846 RELOCATE(iptr->dst.varindex);
1851 new_internalerror("Unknown ICMD %d during stack re-analysis",
1856 #if defined(STACK_VERBOSE)
1857 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1862 /* relocate outvars */
1864 for (i=0; i<b->outdepth; ++i) {
1865 RELOCATE(b->outvars[i]);
1868 #if defined(STACK_VERBOSE)
1869 stack_verbose_block_exit(sd, superblockend);
1872 /* propagate to the next block */
1875 if (!stack_reach_next_block(sd))
1882 /* stack_change_to_tempvar *****************************************************
1884 Change the given stackslot to a TEMPVAR. This includes creating a new
1885 temporary variable and changing the dst.varindex of the creator of the
1886 stacklot to the new variable index. If this stackslot has been passed
1887 through ICMDs between the point of its creation and the current point,
1888 then the variable index is also changed in these ICMDs.
1891 sd...........stack analysis data
1892 sp...........stackslot to change
1893 ilimit.......instruction up to which to look for ICMDs passing-through
1894 the stackslot (exclusive). This may point exactly after the
1895 last instruction, in which case the search is done to the
1898 *******************************************************************************/
1900 static void stack_change_to_tempvar(stackdata_t *sd, stackptr sp,
1901 instruction *ilimit)
1909 oldindex = sp->varnum;
1911 /* create a new temporary variable */
1913 GET_NEW_VAR(*sd, newindex, sp->type);
1915 sd->var[newindex].flags = sp->flags;
1917 /* change the stackslot */
1919 sp->varnum = newindex;
1920 sp->varkind = TEMPVAR;
1922 /* change the dst.varindex of the stackslot's creator */
1925 sp->creator->dst.varindex = newindex;
1927 /* handle ICMDs this stackslot passed through, if any */
1929 if (sp->flags & PASSTHROUGH) {
1930 iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
1932 /* assert that the limit points to an ICMD, or after the last one */
1934 assert(ilimit >= sd->bptr->iinstr);
1935 assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
1937 /* find the stackdepth under sp plus one */
1938 /* Note: This number is usually known when this function is called, */
1939 /* but calculating it here is less error-prone and should not be */
1940 /* a performance problem. */
1942 for (depth = 0; sp != NULL; sp = sp->prev)
1945 /* iterate over all instructions in the range and replace */
1947 for (; iptr < ilimit; ++iptr) {
1948 switch (iptr->opc) {
1949 case ICMD_INVOKESTATIC:
1950 case ICMD_INVOKESPECIAL:
1951 case ICMD_INVOKEVIRTUAL:
1952 case ICMD_INVOKEINTERFACE:
1954 i = iptr->s1.argcount - depth;
1955 if (iptr->sx.s23.s2.args[i] == oldindex) {
1956 iptr->sx.s23.s2.args[i] = newindex;
1959 /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
1960 /* stackslot, it must be added in this switch! */
1967 /* stack_init_javalocals *******************************************************
1969 Initialize the mapping from Java locals to cacao variables at method entry.
1972 sd...........stack analysis data
1974 *******************************************************************************/
1976 static void stack_init_javalocals(stackdata_t *sd)
1985 jl = DMNEW(s4, sd->maxlocals);
1986 jd->basicblocks[0].javalocals = jl;
1988 for (i=0; i<sd->maxlocals; ++i)
1991 md = jd->m->parseddesc;
1993 for (i=0; i<md->paramcount; ++i) {
1994 t = md->paramtypes[i].type;
1995 jl[j] = jd->local_map[5*j + t];
1997 if (IS_2_WORD_TYPE(t))
2003 /* stack_analyse ***************************************************************
2005 Analyse_stack uses the intermediate code created by parse.c to
2006 build a model of the JVM operand stack for the current method.
2008 The following checks are performed:
2009 - check for operand stack underflow (before each instruction)
2010 - check for operand stack overflow (after[1] each instruction)
2011 - check for matching stack depth at merging points
2012 - check for matching basic types[2] at merging points
2013 - check basic types for instruction input (except for BUILTIN*
2014 opcodes, INVOKE* opcodes and MULTIANEWARRAY)
2016 [1]) Checking this after the instruction should be ok. parse.c
2017 counts the number of required stack slots in such a way that it is
2018 only vital that we don't exceed `maxstack` at basic block
2021 [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
2022 DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
2023 types are not discerned.
2025 *******************************************************************************/
2027 bool stack_analyse(jitdata *jd)
2029 methodinfo *m; /* method being analyzed */
2032 #if defined(ENABLE_SSA)
2036 stackptr curstack; /* current stack top */
2038 int opcode; /* opcode of current instruction */
2041 int len; /* # of instructions after the current one */
2042 bool superblockend; /* if true, no fallthrough to next block */
2043 bool deadcode; /* true if no live code has been reached */
2044 instruction *iptr; /* the current instruction */
2046 basicblock *original;
2047 exception_entry *ex;
2049 stackptr *last_store_boundary;
2050 stackptr coalescing_boundary;
2052 stackptr src1, src2, src3, src4, dst1, dst2;
2054 branch_target_t *table;
2055 lookup_target_t *lookup;
2056 #if defined(ENABLE_VERIFIER)
2057 int expectedtype; /* used by CHECK_BASIC_TYPE */
2059 builtintable_entry *bte;
2061 constant_FMIref *fmiref;
2062 #if defined(ENABLE_STATISTICS)
2063 int iteration_count; /* number of iterations of analysis */
2065 int new_index; /* used to get a new var index with GET_NEW_INDEX*/
2067 #if defined(STACK_VERBOSE)
2068 show_method(jd, SHOW_PARSE);
2071 /* get required compiler data - initialization */
2075 #if defined(ENABLE_SSA)
2079 /* initialize the stackdata_t struct */
2083 sd.varcount = jd->varcount;
2084 sd.vartop = jd->vartop;
2085 sd.localcount = jd->localcount;
2087 sd.varsallocated = sd.varcount;
2088 sd.maxlocals = m->maxlocals;
2089 sd.javalocals = DMNEW(s4, sd.maxlocals);
2090 sd.handlers = DMNEW(exception_entry *, jd->exceptiontablelength + 1);
2092 /* prepare the variable for exception handler stacks */
2093 /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2095 sd.exstack.type = TYPE_ADR;
2096 sd.exstack.prev = NULL;
2097 sd.exstack.varnum = sd.localcount;
2098 sd.var[sd.exstack.varnum].type = TYPE_ADR;
2100 #if defined(ENABLE_LSRA)
2101 m->maxlifetimes = 0;
2104 #if defined(ENABLE_STATISTICS)
2105 iteration_count = 0;
2108 /* find the last real basic block */
2110 sd.last_real_block = NULL;
2111 tbptr = jd->basicblocks;
2112 while (tbptr->next) {
2113 sd.last_real_block = tbptr;
2114 tbptr = tbptr->next;
2116 assert(sd.last_real_block);
2118 /* find the last exception handler */
2120 if (jd->exceptiontablelength)
2121 sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2123 sd.extableend = NULL;
2125 /* init jd->interface_map */
2127 jd->maxinterfaces = m->maxstack;
2128 jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2129 for (i = 0; i < m->maxstack * 5; i++)
2130 jd->interface_map[i].flags = UNUSED;
2132 last_store_boundary = DMNEW(stackptr, m->maxlocals);
2134 /* initialize flags and invars (none) of first block */
2136 jd->basicblocks[0].flags = BBREACHED;
2137 jd->basicblocks[0].invars = NULL;
2138 jd->basicblocks[0].indepth = 0;
2139 jd->basicblocks[0].inlocals =
2140 DMNEW(varinfo, jd->localcount + VERIFIER_EXTRA_LOCALS);
2141 MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo,
2142 jd->localcount + VERIFIER_EXTRA_LOCALS);
2144 /* initialize java local mapping of first block */
2146 stack_init_javalocals(&sd);
2148 /* stack analysis loop (until fixpoint reached) **************************/
2151 #if defined(ENABLE_STATISTICS)
2155 /* initialize loop over basic blocks */
2157 sd.bptr = jd->basicblocks;
2158 superblockend = true;
2160 curstack = NULL; stackdepth = 0;
2163 /* iterate over basic blocks *****************************************/
2165 for (; sd.bptr; sd.bptr = sd.bptr->next) {
2167 if (sd.bptr->flags == BBDELETED) {
2168 /* This block has been deleted - do nothing. */
2173 if (sd.bptr->flags == BBTYPECHECK_REACHED) {
2174 /* re-analyse a block because its input changed */
2176 if (!stack_reanalyse_block(&sd))
2178 superblockend = true; /* XXX */
2182 if (superblockend && (sd.bptr->flags < BBREACHED)) {
2183 /* This block has not been reached so far, and we */
2184 /* don't fall into it, so we'll have to iterate again. */
2190 if (sd.bptr->flags > BBREACHED) {
2191 /* This block is already finished. */
2193 superblockend = true;
2197 if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2198 /* This block is a clone and the original has not been */
2199 /* analysed, yet. Analyse it on the next iteration. */
2202 /* XXX superblockend? */
2206 /* This block has to be analysed now. */
2210 /* XXX The rest of this block is still indented one level too */
2211 /* much in order to avoid a giant diff by changing that. */
2213 /* We know that sd.bptr->flags == BBREACHED. */
2214 /* This block has been reached before. */
2216 assert(sd.bptr->flags == BBREACHED);
2217 stackdepth = sd.bptr->indepth;
2219 /* find exception handlers for this block */
2221 /* determine the active exception handlers for this block */
2222 /* XXX could use a faster algorithm with sorted lists or */
2225 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2228 ex = jd->exceptiontable;
2229 for (; ex != NULL; ex = ex->down) {
2230 if ((ex->start <= original) && (ex->end > original)) {
2231 sd.handlers[len++] = ex;
2234 sd.handlers[len] = NULL;
2237 /* reanalyse cloned block */
2239 if (sd.bptr->original) {
2240 if (!stack_reanalyse_block(&sd))
2245 /* reset the new pointer for allocating stackslots */
2249 /* create the instack of this block */
2251 curstack = stack_create_instack(&sd);
2253 /* initialize locals at the start of this block */
2255 if (sd.bptr->inlocals)
2256 MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2258 MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2260 /* set up local variables for analyzing this block */
2262 superblockend = false;
2263 len = sd.bptr->icount;
2264 iptr = sd.bptr->iinstr;
2266 /* mark the block as analysed */
2268 sd.bptr->flags = BBFINISHED;
2270 /* reset variables for dependency checking */
2272 coalescing_boundary = sd.new;
2273 for( i = 0; i < m->maxlocals; i++)
2274 last_store_boundary[i] = sd.new;
2276 /* remember the start of this block's variables */
2278 sd.bptr->varstart = sd.vartop;
2280 #if defined(STACK_VERBOSE)
2281 stack_verbose_block_enter(&sd, false);
2284 /* reach exception handlers for this block */
2286 if (!stack_reach_handlers(&sd))
2289 /* iterate over ICMDs ****************************************/
2291 while (--len >= 0) {
2293 #if defined(STACK_VERBOSE)
2294 stack_verbose_show_state(&sd, iptr, curstack);
2297 /* fetch the current opcode */
2301 /* automatically replace some ICMDs with builtins */
2303 #if defined(USEBUILTINTABLE)
2304 bte = builtintable_get_automatic(opcode);
2306 if (bte && bte->opcode == opcode) {
2307 iptr->opc = ICMD_BUILTIN;
2308 iptr->flags.bits &= INS_FLAG_ID_MASK;
2309 iptr->sx.s23.s3.bte = bte;
2310 /* iptr->line is already set */
2311 jd->isleafmethod = false;
2314 #endif /* defined(USEBUILTINTABLE) */
2316 /* main opcode switch *************************************/
2328 case ICMD_CHECKNULL:
2329 coalescing_boundary = sd.new;
2330 COUNT(count_check_null);
2333 iptr->dst.varindex = iptr->s1.varindex;
2337 j = iptr->s1.varindex =
2338 jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2340 #if defined(ENABLE_VERIFIER)
2341 if (sd.var[j].type != TYPE_RET) {
2342 exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2349 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2350 superblockend = true;
2354 COUNT(count_pcmd_return);
2357 superblockend = true;
2358 sd.jd->returncount++;
2359 sd.jd->returnblock = sd.bptr;
2363 /* pop 0 push 1 const */
2365 /************************** ICONST OPTIMIZATIONS **************************/
2368 COUNT(count_pcmd_load);
2372 switch (iptr[1].opc) {
2374 iptr->opc = ICMD_IADDCONST;
2378 iptr[1].opc = ICMD_NOP;
2379 OP1_1(TYPE_INT, TYPE_INT);
2380 COUNT(count_pcmd_op);
2384 iptr->opc = ICMD_ISUBCONST;
2385 goto icmd_iconst_tail;
2386 #if SUPPORT_CONST_MUL
2388 iptr->opc = ICMD_IMULCONST;
2389 goto icmd_iconst_tail;
2390 #else /* SUPPORT_CONST_MUL */
2392 if (iptr->sx.val.i == 0x00000002)
2394 else if (iptr->sx.val.i == 0x00000004)
2396 else if (iptr->sx.val.i == 0x00000008)
2398 else if (iptr->sx.val.i == 0x00000010)
2400 else if (iptr->sx.val.i == 0x00000020)
2402 else if (iptr->sx.val.i == 0x00000040)
2404 else if (iptr->sx.val.i == 0x00000080)
2406 else if (iptr->sx.val.i == 0x00000100)
2408 else if (iptr->sx.val.i == 0x00000200)
2410 else if (iptr->sx.val.i == 0x00000400)
2411 iptr->sx.val.i = 10;
2412 else if (iptr->sx.val.i == 0x00000800)
2413 iptr->sx.val.i = 11;
2414 else if (iptr->sx.val.i == 0x00001000)
2415 iptr->sx.val.i = 12;
2416 else if (iptr->sx.val.i == 0x00002000)
2417 iptr->sx.val.i = 13;
2418 else if (iptr->sx.val.i == 0x00004000)
2419 iptr->sx.val.i = 14;
2420 else if (iptr->sx.val.i == 0x00008000)
2421 iptr->sx.val.i = 15;
2422 else if (iptr->sx.val.i == 0x00010000)
2423 iptr->sx.val.i = 16;
2424 else if (iptr->sx.val.i == 0x00020000)
2425 iptr->sx.val.i = 17;
2426 else if (iptr->sx.val.i == 0x00040000)
2427 iptr->sx.val.i = 18;
2428 else if (iptr->sx.val.i == 0x00080000)
2429 iptr->sx.val.i = 19;
2430 else if (iptr->sx.val.i == 0x00100000)
2431 iptr->sx.val.i = 20;
2432 else if (iptr->sx.val.i == 0x00200000)
2433 iptr->sx.val.i = 21;
2434 else if (iptr->sx.val.i == 0x00400000)
2435 iptr->sx.val.i = 22;
2436 else if (iptr->sx.val.i == 0x00800000)
2437 iptr->sx.val.i = 23;
2438 else if (iptr->sx.val.i == 0x01000000)
2439 iptr->sx.val.i = 24;
2440 else if (iptr->sx.val.i == 0x02000000)
2441 iptr->sx.val.i = 25;
2442 else if (iptr->sx.val.i == 0x04000000)
2443 iptr->sx.val.i = 26;
2444 else if (iptr->sx.val.i == 0x08000000)
2445 iptr->sx.val.i = 27;
2446 else if (iptr->sx.val.i == 0x10000000)
2447 iptr->sx.val.i = 28;
2448 else if (iptr->sx.val.i == 0x20000000)
2449 iptr->sx.val.i = 29;
2450 else if (iptr->sx.val.i == 0x40000000)
2451 iptr->sx.val.i = 30;
2452 else if (iptr->sx.val.i == 0x80000000)
2453 iptr->sx.val.i = 31;
2457 iptr->opc = ICMD_IMULPOW2;
2458 goto icmd_iconst_tail;
2459 #endif /* SUPPORT_CONST_MUL */
2461 if (iptr->sx.val.i == 0x00000002)
2463 else if (iptr->sx.val.i == 0x00000004)
2465 else if (iptr->sx.val.i == 0x00000008)
2467 else if (iptr->sx.val.i == 0x00000010)
2469 else if (iptr->sx.val.i == 0x00000020)
2471 else if (iptr->sx.val.i == 0x00000040)
2473 else if (iptr->sx.val.i == 0x00000080)
2475 else if (iptr->sx.val.i == 0x00000100)
2477 else if (iptr->sx.val.i == 0x00000200)
2479 else if (iptr->sx.val.i == 0x00000400)
2480 iptr->sx.val.i = 10;
2481 else if (iptr->sx.val.i == 0x00000800)
2482 iptr->sx.val.i = 11;
2483 else if (iptr->sx.val.i == 0x00001000)
2484 iptr->sx.val.i = 12;
2485 else if (iptr->sx.val.i == 0x00002000)
2486 iptr->sx.val.i = 13;
2487 else if (iptr->sx.val.i == 0x00004000)
2488 iptr->sx.val.i = 14;
2489 else if (iptr->sx.val.i == 0x00008000)
2490 iptr->sx.val.i = 15;
2491 else if (iptr->sx.val.i == 0x00010000)
2492 iptr->sx.val.i = 16;
2493 else if (iptr->sx.val.i == 0x00020000)
2494 iptr->sx.val.i = 17;
2495 else if (iptr->sx.val.i == 0x00040000)
2496 iptr->sx.val.i = 18;
2497 else if (iptr->sx.val.i == 0x00080000)
2498 iptr->sx.val.i = 19;
2499 else if (iptr->sx.val.i == 0x00100000)
2500 iptr->sx.val.i = 20;
2501 else if (iptr->sx.val.i == 0x00200000)
2502 iptr->sx.val.i = 21;
2503 else if (iptr->sx.val.i == 0x00400000)
2504 iptr->sx.val.i = 22;
2505 else if (iptr->sx.val.i == 0x00800000)
2506 iptr->sx.val.i = 23;
2507 else if (iptr->sx.val.i == 0x01000000)
2508 iptr->sx.val.i = 24;
2509 else if (iptr->sx.val.i == 0x02000000)
2510 iptr->sx.val.i = 25;
2511 else if (iptr->sx.val.i == 0x04000000)
2512 iptr->sx.val.i = 26;
2513 else if (iptr->sx.val.i == 0x08000000)
2514 iptr->sx.val.i = 27;
2515 else if (iptr->sx.val.i == 0x10000000)
2516 iptr->sx.val.i = 28;
2517 else if (iptr->sx.val.i == 0x20000000)
2518 iptr->sx.val.i = 29;
2519 else if (iptr->sx.val.i == 0x40000000)
2520 iptr->sx.val.i = 30;
2521 else if (iptr->sx.val.i == 0x80000000)
2522 iptr->sx.val.i = 31;
2526 iptr->opc = ICMD_IDIVPOW2;
2527 goto icmd_iconst_tail;
2530 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2531 if ((iptr->sx.val.i == 0x00000002) ||
2532 (iptr->sx.val.i == 0x00000004) ||
2533 (iptr->sx.val.i == 0x00000008) ||
2534 (iptr->sx.val.i == 0x00000010) ||
2535 (iptr->sx.val.i == 0x00000020) ||
2536 (iptr->sx.val.i == 0x00000040) ||
2537 (iptr->sx.val.i == 0x00000080) ||
2538 (iptr->sx.val.i == 0x00000100) ||
2539 (iptr->sx.val.i == 0x00000200) ||
2540 (iptr->sx.val.i == 0x00000400) ||
2541 (iptr->sx.val.i == 0x00000800) ||
2542 (iptr->sx.val.i == 0x00001000) ||
2543 (iptr->sx.val.i == 0x00002000) ||
2544 (iptr->sx.val.i == 0x00004000) ||
2545 (iptr->sx.val.i == 0x00008000) ||
2546 (iptr->sx.val.i == 0x00010000) ||
2547 (iptr->sx.val.i == 0x00020000) ||
2548 (iptr->sx.val.i == 0x00040000) ||
2549 (iptr->sx.val.i == 0x00080000) ||
2550 (iptr->sx.val.i == 0x00100000) ||
2551 (iptr->sx.val.i == 0x00200000) ||
2552 (iptr->sx.val.i == 0x00400000) ||
2553 (iptr->sx.val.i == 0x00800000) ||
2554 (iptr->sx.val.i == 0x01000000) ||
2555 (iptr->sx.val.i == 0x02000000) ||
2556 (iptr->sx.val.i == 0x04000000) ||
2557 (iptr->sx.val.i == 0x08000000) ||
2558 (iptr->sx.val.i == 0x10000000) ||
2559 (iptr->sx.val.i == 0x20000000) ||
2560 (iptr->sx.val.i == 0x40000000) ||
2561 (iptr->sx.val.i == 0x80000000))
2563 iptr->opc = ICMD_IREMPOW2;
2564 iptr->sx.val.i -= 1;
2565 goto icmd_iconst_tail;
2568 #if SUPPORT_CONST_LOGICAL
2570 iptr->opc = ICMD_IANDCONST;
2571 goto icmd_iconst_tail;
2574 iptr->opc = ICMD_IORCONST;
2575 goto icmd_iconst_tail;
2578 iptr->opc = ICMD_IXORCONST;
2579 goto icmd_iconst_tail;
2581 #endif /* SUPPORT_CONST_LOGICAL */
2583 iptr->opc = ICMD_ISHLCONST;
2584 goto icmd_iconst_tail;
2587 iptr->opc = ICMD_ISHRCONST;
2588 goto icmd_iconst_tail;
2591 iptr->opc = ICMD_IUSHRCONST;
2592 goto icmd_iconst_tail;
2593 #if SUPPORT_LONG_SHIFT
2595 iptr->opc = ICMD_LSHLCONST;
2596 goto icmd_lconst_tail;
2599 iptr->opc = ICMD_LSHRCONST;
2600 goto icmd_lconst_tail;
2603 iptr->opc = ICMD_LUSHRCONST;
2604 goto icmd_lconst_tail;
2605 #endif /* SUPPORT_LONG_SHIFT */
2606 case ICMD_IF_ICMPEQ:
2607 iptr[1].opc = ICMD_IFEQ;
2611 /* set the constant for the following icmd */
2612 iptr[1].sx.val.i = iptr->sx.val.i;
2614 /* this instruction becomes a nop */
2615 iptr->opc = ICMD_NOP;
2618 case ICMD_IF_ICMPLT:
2619 iptr[1].opc = ICMD_IFLT;
2620 goto icmd_if_icmp_tail;
2622 case ICMD_IF_ICMPLE:
2623 iptr[1].opc = ICMD_IFLE;
2624 goto icmd_if_icmp_tail;
2626 case ICMD_IF_ICMPNE:
2627 iptr[1].opc = ICMD_IFNE;
2628 goto icmd_if_icmp_tail;
2630 case ICMD_IF_ICMPGT:
2631 iptr[1].opc = ICMD_IFGT;
2632 goto icmd_if_icmp_tail;
2634 case ICMD_IF_ICMPGE:
2635 iptr[1].opc = ICMD_IFGE;
2636 goto icmd_if_icmp_tail;
2638 #if SUPPORT_CONST_STORE
2643 # if SUPPORT_CONST_STORE_ZERO_ONLY
2644 if (iptr->sx.val.i != 0)
2647 switch (iptr[1].opc) {
2649 iptr->opc = ICMD_IASTORECONST;
2650 iptr->flags.bits |= INS_FLAG_CHECK;
2653 iptr->opc = ICMD_BASTORECONST;
2654 iptr->flags.bits |= INS_FLAG_CHECK;
2657 iptr->opc = ICMD_CASTORECONST;
2658 iptr->flags.bits |= INS_FLAG_CHECK;
2661 iptr->opc = ICMD_SASTORECONST;
2662 iptr->flags.bits |= INS_FLAG_CHECK;
2666 iptr[1].opc = ICMD_NOP;
2668 /* copy the constant to s3 */
2669 /* XXX constval -> astoreconstval? */
2670 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2671 OP2_0(TYPE_ADR, TYPE_INT);
2672 COUNT(count_pcmd_op);
2675 case ICMD_PUTSTATIC:
2677 # if SUPPORT_CONST_STORE_ZERO_ONLY
2678 if (iptr->sx.val.i != 0)
2681 /* XXX check field type? */
2683 /* copy the constant to s2 */
2684 /* XXX constval -> fieldconstval? */
2685 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2688 /* set the field reference (s3) */
2689 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2690 iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2691 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2692 fmiref = iptr->sx.s23.s3.uf->fieldref;
2695 fmiref = iptr[1].sx.s23.s3.fmiref;
2696 iptr->sx.s23.s3.fmiref = fmiref;
2699 #if defined(ENABLE_VERIFIER)
2700 expectedtype = fmiref->parseddesc.fd->type;
2701 switch (iptr[0].opc) {
2703 if (expectedtype != TYPE_INT)
2704 goto throw_stack_type_error;
2707 if (expectedtype != TYPE_LNG)
2708 goto throw_stack_type_error;
2711 if (expectedtype != TYPE_ADR)
2712 goto throw_stack_type_error;
2717 #endif /* defined(ENABLE_VERIFIER) */
2719 switch (iptr[1].opc) {
2720 case ICMD_PUTSTATIC:
2721 iptr->opc = ICMD_PUTSTATICCONST;
2725 iptr->opc = ICMD_PUTFIELDCONST;
2730 iptr[1].opc = ICMD_NOP;
2731 COUNT(count_pcmd_op);
2733 #endif /* SUPPORT_CONST_STORE */
2739 /* if we get here, the ICONST has been optimized */
2743 /* normal case of an unoptimized ICONST */
2747 /************************** LCONST OPTIMIZATIONS **************************/
2750 COUNT(count_pcmd_load);
2754 /* switch depending on the following instruction */
2756 switch (iptr[1].opc) {
2757 #if SUPPORT_LONG_ADD
2759 iptr->opc = ICMD_LADDCONST;
2763 /* instruction of type LONG -> LONG */
2764 iptr[1].opc = ICMD_NOP;
2765 OP1_1(TYPE_LNG, TYPE_LNG);
2766 COUNT(count_pcmd_op);
2770 iptr->opc = ICMD_LSUBCONST;
2771 goto icmd_lconst_tail;
2773 #endif /* SUPPORT_LONG_ADD */
2774 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2776 iptr->opc = ICMD_LMULCONST;
2777 goto icmd_lconst_tail;
2778 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2779 # if SUPPORT_LONG_SHIFT
2781 if (iptr->sx.val.l == 0x00000002)
2783 else if (iptr->sx.val.l == 0x00000004)
2785 else if (iptr->sx.val.l == 0x00000008)
2787 else if (iptr->sx.val.l == 0x00000010)
2789 else if (iptr->sx.val.l == 0x00000020)
2791 else if (iptr->sx.val.l == 0x00000040)
2793 else if (iptr->sx.val.l == 0x00000080)
2795 else if (iptr->sx.val.l == 0x00000100)
2797 else if (iptr->sx.val.l == 0x00000200)
2799 else if (iptr->sx.val.l == 0x00000400)
2800 iptr->sx.val.i = 10;
2801 else if (iptr->sx.val.l == 0x00000800)
2802 iptr->sx.val.i = 11;
2803 else if (iptr->sx.val.l == 0x00001000)
2804 iptr->sx.val.i = 12;
2805 else if (iptr->sx.val.l == 0x00002000)
2806 iptr->sx.val.i = 13;
2807 else if (iptr->sx.val.l == 0x00004000)
2808 iptr->sx.val.i = 14;
2809 else if (iptr->sx.val.l == 0x00008000)
2810 iptr->sx.val.i = 15;
2811 else if (iptr->sx.val.l == 0x00010000)
2812 iptr->sx.val.i = 16;
2813 else if (iptr->sx.val.l == 0x00020000)
2814 iptr->sx.val.i = 17;
2815 else if (iptr->sx.val.l == 0x00040000)
2816 iptr->sx.val.i = 18;
2817 else if (iptr->sx.val.l == 0x00080000)
2818 iptr->sx.val.i = 19;
2819 else if (iptr->sx.val.l == 0x00100000)
2820 iptr->sx.val.i = 20;
2821 else if (iptr->sx.val.l == 0x00200000)
2822 iptr->sx.val.i = 21;
2823 else if (iptr->sx.val.l == 0x00400000)
2824 iptr->sx.val.i = 22;
2825 else if (iptr->sx.val.l == 0x00800000)
2826 iptr->sx.val.i = 23;
2827 else if (iptr->sx.val.l == 0x01000000)
2828 iptr->sx.val.i = 24;
2829 else if (iptr->sx.val.l == 0x02000000)
2830 iptr->sx.val.i = 25;
2831 else if (iptr->sx.val.l == 0x04000000)
2832 iptr->sx.val.i = 26;
2833 else if (iptr->sx.val.l == 0x08000000)
2834 iptr->sx.val.i = 27;
2835 else if (iptr->sx.val.l == 0x10000000)
2836 iptr->sx.val.i = 28;
2837 else if (iptr->sx.val.l == 0x20000000)
2838 iptr->sx.val.i = 29;
2839 else if (iptr->sx.val.l == 0x40000000)
2840 iptr->sx.val.i = 30;
2841 else if (iptr->sx.val.l == 0x80000000)
2842 iptr->sx.val.i = 31;
2846 iptr->opc = ICMD_LMULPOW2;
2847 goto icmd_lconst_tail;
2848 # endif /* SUPPORT_LONG_SHIFT */
2849 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2850 #if SUPPORT_LONG_DIV_POW2
2852 if (iptr->sx.val.l == 0x00000002)
2854 else if (iptr->sx.val.l == 0x00000004)
2856 else if (iptr->sx.val.l == 0x00000008)
2858 else if (iptr->sx.val.l == 0x00000010)
2860 else if (iptr->sx.val.l == 0x00000020)
2862 else if (iptr->sx.val.l == 0x00000040)
2864 else if (iptr->sx.val.l == 0x00000080)
2866 else if (iptr->sx.val.l == 0x00000100)
2868 else if (iptr->sx.val.l == 0x00000200)
2870 else if (iptr->sx.val.l == 0x00000400)
2871 iptr->sx.val.i = 10;
2872 else if (iptr->sx.val.l == 0x00000800)
2873 iptr->sx.val.i = 11;
2874 else if (iptr->sx.val.l == 0x00001000)
2875 iptr->sx.val.i = 12;
2876 else if (iptr->sx.val.l == 0x00002000)
2877 iptr->sx.val.i = 13;
2878 else if (iptr->sx.val.l == 0x00004000)
2879 iptr->sx.val.i = 14;
2880 else if (iptr->sx.val.l == 0x00008000)
2881 iptr->sx.val.i = 15;
2882 else if (iptr->sx.val.l == 0x00010000)
2883 iptr->sx.val.i = 16;
2884 else if (iptr->sx.val.l == 0x00020000)
2885 iptr->sx.val.i = 17;
2886 else if (iptr->sx.val.l == 0x00040000)
2887 iptr->sx.val.i = 18;
2888 else if (iptr->sx.val.l == 0x00080000)
2889 iptr->sx.val.i = 19;
2890 else if (iptr->sx.val.l == 0x00100000)
2891 iptr->sx.val.i = 20;
2892 else if (iptr->sx.val.l == 0x00200000)
2893 iptr->sx.val.i = 21;
2894 else if (iptr->sx.val.l == 0x00400000)
2895 iptr->sx.val.i = 22;
2896 else if (iptr->sx.val.l == 0x00800000)
2897 iptr->sx.val.i = 23;
2898 else if (iptr->sx.val.l == 0x01000000)
2899 iptr->sx.val.i = 24;
2900 else if (iptr->sx.val.l == 0x02000000)
2901 iptr->sx.val.i = 25;
2902 else if (iptr->sx.val.l == 0x04000000)
2903 iptr->sx.val.i = 26;
2904 else if (iptr->sx.val.l == 0x08000000)
2905 iptr->sx.val.i = 27;
2906 else if (iptr->sx.val.l == 0x10000000)
2907 iptr->sx.val.i = 28;
2908 else if (iptr->sx.val.l == 0x20000000)
2909 iptr->sx.val.i = 29;
2910 else if (iptr->sx.val.l == 0x40000000)
2911 iptr->sx.val.i = 30;
2912 else if (iptr->sx.val.l == 0x80000000)
2913 iptr->sx.val.i = 31;
2917 iptr->opc = ICMD_LDIVPOW2;
2918 goto icmd_lconst_tail;
2919 #endif /* SUPPORT_LONG_DIV_POW2 */
2921 #if SUPPORT_LONG_REM_POW2
2923 if ((iptr->sx.val.l == 0x00000002) ||
2924 (iptr->sx.val.l == 0x00000004) ||
2925 (iptr->sx.val.l == 0x00000008) ||
2926 (iptr->sx.val.l == 0x00000010) ||
2927 (iptr->sx.val.l == 0x00000020) ||
2928 (iptr->sx.val.l == 0x00000040) ||
2929 (iptr->sx.val.l == 0x00000080) ||
2930 (iptr->sx.val.l == 0x00000100) ||
2931 (iptr->sx.val.l == 0x00000200) ||
2932 (iptr->sx.val.l == 0x00000400) ||
2933 (iptr->sx.val.l == 0x00000800) ||
2934 (iptr->sx.val.l == 0x00001000) ||
2935 (iptr->sx.val.l == 0x00002000) ||
2936 (iptr->sx.val.l == 0x00004000) ||
2937 (iptr->sx.val.l == 0x00008000) ||
2938 (iptr->sx.val.l == 0x00010000) ||
2939 (iptr->sx.val.l == 0x00020000) ||
2940 (iptr->sx.val.l == 0x00040000) ||
2941 (iptr->sx.val.l == 0x00080000) ||
2942 (iptr->sx.val.l == 0x00100000) ||
2943 (iptr->sx.val.l == 0x00200000) ||
2944 (iptr->sx.val.l == 0x00400000) ||
2945 (iptr->sx.val.l == 0x00800000) ||
2946 (iptr->sx.val.l == 0x01000000) ||
2947 (iptr->sx.val.l == 0x02000000) ||
2948 (iptr->sx.val.l == 0x04000000) ||
2949 (iptr->sx.val.l == 0x08000000) ||
2950 (iptr->sx.val.l == 0x10000000) ||
2951 (iptr->sx.val.l == 0x20000000) ||
2952 (iptr->sx.val.l == 0x40000000) ||
2953 (iptr->sx.val.l == 0x80000000))
2955 iptr->opc = ICMD_LREMPOW2;
2956 iptr->sx.val.l -= 1;
2957 goto icmd_lconst_tail;
2960 #endif /* SUPPORT_LONG_REM_POW2 */
2962 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2965 iptr->opc = ICMD_LANDCONST;
2966 goto icmd_lconst_tail;
2969 iptr->opc = ICMD_LORCONST;
2970 goto icmd_lconst_tail;
2973 iptr->opc = ICMD_LXORCONST;
2974 goto icmd_lconst_tail;
2975 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2977 #if SUPPORT_LONG_CMP_CONST
2979 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2982 /* switch on the instruction after LCONST - LCMP */
2984 switch (iptr[2].opc) {
2986 iptr->opc = ICMD_IF_LEQ;
2989 icmd_lconst_lcmp_tail:
2990 /* convert LCONST, LCMP, IFXX to IF_LXX */
2991 iptr->dst.insindex = iptr[2].dst.insindex;
2992 iptr[1].opc = ICMD_NOP;
2993 iptr[2].opc = ICMD_NOP;
2995 OP1_BRANCH(TYPE_LNG);
2997 COUNT(count_pcmd_bra);
2998 COUNT(count_pcmd_op);
3002 iptr->opc = ICMD_IF_LNE;
3003 goto icmd_lconst_lcmp_tail;
3006 iptr->opc = ICMD_IF_LLT;
3007 goto icmd_lconst_lcmp_tail;
3010 iptr->opc = ICMD_IF_LGT;
3011 goto icmd_lconst_lcmp_tail;
3014 iptr->opc = ICMD_IF_LLE;
3015 goto icmd_lconst_lcmp_tail;
3018 iptr->opc = ICMD_IF_LGE;
3019 goto icmd_lconst_lcmp_tail;
3023 } /* end switch on opcode after LCONST - LCMP */
3025 #endif /* SUPPORT_LONG_CMP_CONST */
3027 #if SUPPORT_CONST_STORE
3029 # if SUPPORT_CONST_STORE_ZERO_ONLY
3030 if (iptr->sx.val.l != 0)
3033 #if SIZEOF_VOID_P == 4
3034 /* the constant must fit into a ptrint */
3035 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3038 /* move the constant to s3 */
3039 iptr->sx.s23.s3.constval = iptr->sx.val.l;
3041 iptr->opc = ICMD_LASTORECONST;
3042 iptr->flags.bits |= INS_FLAG_CHECK;
3043 OP2_0(TYPE_ADR, TYPE_INT);
3045 iptr[1].opc = ICMD_NOP;
3046 COUNT(count_pcmd_op);
3049 case ICMD_PUTSTATIC:
3051 # if SUPPORT_CONST_STORE_ZERO_ONLY
3052 if (iptr->sx.val.l != 0)
3055 #if SIZEOF_VOID_P == 4
3056 /* the constant must fit into a ptrint */
3057 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3060 /* XXX check field type? */
3062 /* copy the constant to s2 */
3063 /* XXX constval -> fieldconstval? */
3064 iptr->sx.s23.s2.constval = iptr->sx.val.l;
3068 #endif /* SUPPORT_CONST_STORE */
3072 } /* end switch opcode after LCONST */
3074 /* if we get here, the LCONST has been optimized */
3078 /* the normal case of an unoptimized LCONST */
3082 /************************ END OF LCONST OPTIMIZATIONS *********************/
3085 COUNT(count_pcmd_load);
3090 COUNT(count_pcmd_load);
3094 /************************** ACONST OPTIMIZATIONS **************************/
3097 coalescing_boundary = sd.new;
3098 COUNT(count_pcmd_load);
3099 #if SUPPORT_CONST_STORE
3100 /* We can only optimize if the ACONST is resolved
3101 * and there is an instruction after it. */
3103 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3106 switch (iptr[1].opc) {
3108 /* We can only optimize for NULL values
3109 * here because otherwise a checkcast is
3111 if (iptr->sx.val.anyptr != NULL)
3114 /* copy the constant (NULL) to s3 */
3115 iptr->sx.s23.s3.constval = 0;
3116 iptr->opc = ICMD_AASTORECONST;
3117 iptr->flags.bits |= INS_FLAG_CHECK;
3118 OP2_0(TYPE_ADR, TYPE_INT);
3120 iptr[1].opc = ICMD_NOP;
3121 COUNT(count_pcmd_op);
3124 case ICMD_PUTSTATIC:
3126 # if SUPPORT_CONST_STORE_ZERO_ONLY
3127 if (iptr->sx.val.anyptr != NULL)
3130 /* XXX check field type? */
3131 /* copy the constant to s2 */
3132 /* XXX constval -> fieldconstval? */
3133 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3141 /* if we get here the ACONST has been optimized */
3145 #endif /* SUPPORT_CONST_STORE */
3150 /* pop 0 push 1 load */
3157 COUNT(count_load_instruction);
3158 i = opcode - ICMD_ILOAD; /* type */
3160 j = iptr->s1.varindex =
3161 jd->local_map[iptr->s1.varindex * 5 + i];
3163 #if defined(ENABLE_VERIFIER)
3164 if (sd.var[j].type == TYPE_RET) {
3165 exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3170 #if defined(ENABLE_SSA)
3172 GET_NEW_VAR(sd, new_index, i);
3189 coalescing_boundary = sd.new;
3190 iptr->flags.bits |= INS_FLAG_CHECK;
3191 COUNT(count_check_null);
3192 COUNT(count_check_bound);
3193 COUNT(count_pcmd_mem);
3194 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3201 coalescing_boundary = sd.new;
3202 iptr->flags.bits |= INS_FLAG_CHECK;
3203 COUNT(count_check_null);
3204 COUNT(count_check_bound);
3205 COUNT(count_pcmd_mem);
3206 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3209 /* pop 0 push 0 iinc */
3212 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3213 #if defined(ENABLE_SSA)
3216 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3220 last_store_boundary[iptr->s1.varindex] = sd.new;
3223 jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3228 if ((copy->varkind == LOCALVAR) &&
3229 (copy->varnum == iptr->s1.varindex))
3231 assert(IS_LOCALVAR(copy));
3237 #if defined(ENABLE_SSA)
3241 iptr->dst.varindex = iptr->s1.varindex;
3244 /* pop 1 push 0 store */
3253 i = opcode - ICMD_ISTORE; /* type */
3254 javaindex = iptr->dst.varindex;
3255 j = iptr->dst.varindex =
3256 jd->local_map[javaindex * 5 + i];
3258 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3260 iptr->sx.s23.s3.javaindex = javaindex;
3262 if (curstack->type == TYPE_RET) {
3263 iptr->flags.bits |= INS_FLAG_RETADDR;
3264 iptr->sx.s23.s2.retaddrnr =
3265 UNUSED - (1 + sd.var[j].vv.retaddr->nr);
3266 sd.javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
3269 sd.javalocals[javaindex] = j;
3271 /* invalidate the following javalocal for 2-word types */
3273 if (IS_2_WORD_TYPE(i)) {
3274 sd.javalocals[javaindex+1] = UNUSED;
3275 iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3278 /* invalidate 2-word types if second half was overwritten */
3280 if (javaindex > 0 && (i = sd.javalocals[javaindex-1]) != UNUSED) {
3281 if (IS_2_WORD_TYPE(sd.var[i].type)) {
3282 sd.javalocals[javaindex-1] = UNUSED;
3283 iptr->flags.bits |= INS_FLAG_KILL_PREV;
3287 #if defined(ENABLE_STATISTICS)
3290 i = sd.new - curstack;
3292 count_store_length[20]++;
3294 count_store_length[i]++;
3297 count_store_depth[10]++;
3299 count_store_depth[i]++;
3303 #if defined(ENABLE_SSA)
3306 /* check for conflicts as described in Figure 5.2 */
3308 copy = curstack->prev;
3311 if ((copy->varkind == LOCALVAR) &&
3312 (copy->varnum == j))
3314 copy->varkind = TEMPVAR;
3315 assert(IS_LOCALVAR(copy));
3322 /* if the variable is already coalesced, don't bother */
3324 /* We do not need to check against INOUT, as invars */
3325 /* are always before the coalescing boundary. */
3327 if (curstack->varkind == LOCALVAR)
3330 /* there is no STORE Lj while curstack is live */
3332 if (curstack < last_store_boundary[javaindex])
3333 goto assume_conflict;
3335 /* curstack must be after the coalescing boundary */
3337 if (curstack < coalescing_boundary)
3338 goto assume_conflict;
3340 /* there is no DEF LOCALVAR(j) while curstack is live */
3342 copy = sd.new; /* most recent stackslot created + 1 */
3343 while (--copy > curstack) {
3344 if (copy->varkind == LOCALVAR && copy->varnum == j)
3345 goto assume_conflict;
3348 /* coalesce the temporary variable with Lj */
3349 assert((curstack->varkind == TEMPVAR)
3350 || (curstack->varkind == UNDEFVAR));
3351 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3352 assert(!IS_INOUT(curstack));
3353 assert(!IS_PREALLOC(curstack));
3355 assert(curstack->creator);
3356 assert(curstack->creator->dst.varindex == curstack->varnum);
3357 assert(!(curstack->flags & PASSTHROUGH));
3358 RELEASE_INDEX(sd, curstack);
3359 curstack->varkind = LOCALVAR;
3360 curstack->varnum = j;
3361 curstack->creator->dst.varindex = j;
3364 /* revert the coalescing, if it has been done earlier */
3366 if ((curstack->varkind == LOCALVAR)
3367 && (curstack->varnum == j))
3369 assert(IS_LOCALVAR(curstack));
3370 SET_TEMPVAR(curstack);
3373 /* remember the stack boundary at this store */
3375 last_store_boundary[javaindex] = sd.new;
3376 #if defined(ENABLE_SSA)
3377 } /* if (ls != NULL) */
3380 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3383 STORE(opcode - ICMD_ISTORE, j);
3389 coalescing_boundary = sd.new;
3390 iptr->flags.bits |= INS_FLAG_CHECK;
3391 COUNT(count_check_null);
3392 COUNT(count_check_bound);
3393 COUNT(count_pcmd_mem);
3395 bte = builtintable_get_internal(BUILTIN_canstore);
3398 if (md->memuse > rd->memuse)
3399 rd->memuse = md->memuse;
3400 if (md->argintreguse > rd->argintreguse)
3401 rd->argintreguse = md->argintreguse;
3402 /* XXX non-leaf method? */
3404 /* make all stack variables saved */
3408 sd.var[copy->varnum].flags |= SAVEDVAR;
3409 /* in case copy->varnum is/will be a LOCALVAR */
3410 /* once and set back to a non LOCALVAR */
3411 /* the correct SAVEDVAR flag has to be */
3412 /* remembered in copy->flags, too */
3413 copy->flags |= SAVEDVAR;
3417 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3424 coalescing_boundary = sd.new;
3425 iptr->flags.bits |= INS_FLAG_CHECK;
3426 COUNT(count_check_null);
3427 COUNT(count_check_bound);
3428 COUNT(count_pcmd_mem);
3429 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3436 coalescing_boundary = sd.new;
3437 iptr->flags.bits |= INS_FLAG_CHECK;
3438 COUNT(count_check_null);
3439 COUNT(count_check_bound);
3440 COUNT(count_pcmd_mem);
3441 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3447 #ifdef ENABLE_VERIFIER
3450 if (IS_2_WORD_TYPE(curstack->type))
3451 goto throw_stack_category_error;
3462 coalescing_boundary = sd.new;
3463 /* Assert here that no LOCAL or INOUTS get */
3464 /* preallocated, since tha macros are not */
3465 /* available in md-abi.c! */
3466 if (IS_TEMPVAR(curstack))
3467 md_return_alloc(jd, curstack);
3468 COUNT(count_pcmd_return);
3469 OP1_0(opcode - ICMD_IRETURN);
3470 superblockend = true;
3471 sd.jd->returncount++;
3472 sd.jd->returnblock = sd.bptr;
3476 coalescing_boundary = sd.new;
3477 COUNT(count_check_null);
3479 curstack = NULL; stackdepth = 0;
3480 superblockend = true;
3483 case ICMD_PUTSTATIC:
3484 coalescing_boundary = sd.new;
3485 COUNT(count_pcmd_mem);
3486 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3487 OP1_0(fmiref->parseddesc.fd->type);
3490 /* pop 1 push 0 branch */
3493 case ICMD_IFNONNULL:
3494 COUNT(count_pcmd_bra);
3495 OP1_BRANCH(TYPE_ADR);
3505 COUNT(count_pcmd_bra);
3506 /* iptr->sx.val.i is set implicitly in parse by
3507 clearing the memory or from IF_ICMPxx
3510 OP1_BRANCH(TYPE_INT);
3511 /* iptr->sx.val.i = 0; */
3515 /* pop 0 push 0 branch */
3518 COUNT(count_pcmd_bra);
3521 superblockend = true;
3524 /* pop 1 push 0 table branch */
3526 case ICMD_TABLESWITCH:
3527 COUNT(count_pcmd_table);
3528 OP1_BRANCH(TYPE_INT);
3530 table = iptr->dst.table;
3531 BRANCH_TARGET(*table, tbptr);
3534 i = iptr->sx.s23.s3.tablehigh
3535 - iptr->sx.s23.s2.tablelow + 1;
3538 BRANCH_TARGET(*table, tbptr);
3541 superblockend = true;
3544 /* pop 1 push 0 table branch */
3546 case ICMD_LOOKUPSWITCH:
3547 COUNT(count_pcmd_table);
3548 OP1_BRANCH(TYPE_INT);
3550 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3552 lookup = iptr->dst.lookup;
3554 i = iptr->sx.s23.s2.lookupcount;
3557 BRANCH_TARGET(lookup->target, tbptr);
3560 superblockend = true;
3563 case ICMD_MONITORENTER:
3564 case ICMD_MONITOREXIT:
3565 coalescing_boundary = sd.new;
3566 COUNT(count_check_null);
3570 /* pop 2 push 0 branch */
3572 case ICMD_IF_ICMPEQ:
3573 case ICMD_IF_ICMPNE:
3574 case ICMD_IF_ICMPLT:
3575 case ICMD_IF_ICMPGE:
3576 case ICMD_IF_ICMPGT:
3577 case ICMD_IF_ICMPLE:
3578 COUNT(count_pcmd_bra);
3579 OP2_BRANCH(TYPE_INT, TYPE_INT);
3583 case ICMD_IF_ACMPEQ:
3584 case ICMD_IF_ACMPNE:
3585 COUNT(count_pcmd_bra);
3586 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3593 coalescing_boundary = sd.new;
3594 COUNT(count_check_null);
3595 COUNT(count_pcmd_mem);
3596 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3597 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3602 if (!IS_2_WORD_TYPE(curstack->type)) {
3604 #ifdef ENABLE_VERIFIER
3607 if (IS_2_WORD_TYPE(curstack->prev->type))
3608 goto throw_stack_category_error;
3611 OP2_0_ANY_ANY; /* pop two slots */
3614 iptr->opc = ICMD_POP;
3615 OP1_0_ANY; /* pop one (two-word) slot */
3619 /* pop 0 push 1 dup */
3622 #ifdef ENABLE_VERIFIER
3625 if (IS_2_WORD_TYPE(curstack->type))
3626 goto throw_stack_category_error;
3629 COUNT(count_dup_instruction);
3635 coalescing_boundary = sd.new - 1;
3640 if (IS_2_WORD_TYPE(curstack->type)) {
3642 iptr->opc = ICMD_DUP;
3647 /* ..., ????, cat1 */
3648 #ifdef ENABLE_VERIFIER
3650 if (IS_2_WORD_TYPE(curstack->prev->type))
3651 goto throw_stack_category_error;
3654 src1 = curstack->prev;
3657 COPY_UP(src1); iptr++; len--;
3660 coalescing_boundary = sd.new;
3664 /* pop 2 push 3 dup */
3667 #ifdef ENABLE_VERIFIER
3670 if (IS_2_WORD_TYPE(curstack->type) ||
3671 IS_2_WORD_TYPE(curstack->prev->type))
3672 goto throw_stack_category_error;
3677 src1 = curstack->prev;
3682 /* move non-temporary sources out of the way */
3683 if (!IS_TEMPVAR(src2)) {
3684 MOVE_TO_TEMP(src2); iptr++; len--;
3687 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3689 MOVE_UP(src1); iptr++; len--;
3690 MOVE_UP(src2); iptr++; len--;
3692 COPY_DOWN(curstack, dst1);
3694 coalescing_boundary = sd.new;
3699 if (IS_2_WORD_TYPE(curstack->type)) {
3700 /* ..., ????, cat2 */
3701 #ifdef ENABLE_VERIFIER
3703 if (IS_2_WORD_TYPE(curstack->prev->type))
3704 goto throw_stack_category_error;
3707 iptr->opc = ICMD_DUP_X1;
3711 /* ..., ????, cat1 */
3712 #ifdef ENABLE_VERIFIER
3715 if (IS_2_WORD_TYPE(curstack->prev->type)
3716 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3717 goto throw_stack_category_error;
3722 src1 = curstack->prev->prev;
3723 src2 = curstack->prev;
3725 POPANY; POPANY; POPANY;
3728 /* move non-temporary sources out of the way */
3729 if (!IS_TEMPVAR(src2)) {
3730 MOVE_TO_TEMP(src2); iptr++; len--;
3732 if (!IS_TEMPVAR(src3)) {
3733 MOVE_TO_TEMP(src3); iptr++; len--;
3736 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3737 DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3739 MOVE_UP(src1); iptr++; len--;
3740 MOVE_UP(src2); iptr++; len--;
3741 MOVE_UP(src3); iptr++; len--;
3743 COPY_DOWN(curstack, dst2); iptr++; len--;
3744 COPY_DOWN(curstack->prev, dst1);
3746 coalescing_boundary = sd.new;
3750 /* pop 3 push 4 dup */
3754 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3755 /* ..., cat2, ???? */
3756 #ifdef ENABLE_VERIFIER
3758 if (IS_2_WORD_TYPE(curstack->type))
3759 goto throw_stack_category_error;
3762 iptr->opc = ICMD_DUP_X1;
3766 /* ..., cat1, ???? */
3767 #ifdef ENABLE_VERIFIER
3770 if (IS_2_WORD_TYPE(curstack->type)
3771 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3772 goto throw_stack_category_error;
3776 src1 = curstack->prev->prev;
3777 src2 = curstack->prev;
3779 POPANY; POPANY; POPANY;
3782 /* move non-temporary sources out of the way */
3783 if (!IS_TEMPVAR(src2)) {
3784 MOVE_TO_TEMP(src2); iptr++; len--;
3786 if (!IS_TEMPVAR(src3)) {
3787 MOVE_TO_TEMP(src3); iptr++; len--;
3790 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3792 MOVE_UP(src1); iptr++; len--;
3793 MOVE_UP(src2); iptr++; len--;
3794 MOVE_UP(src3); iptr++; len--;
3796 COPY_DOWN(curstack, dst1);
3798 coalescing_boundary = sd.new;
3804 if (IS_2_WORD_TYPE(curstack->type)) {
3805 /* ..., ????, cat2 */
3806 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3807 /* ..., cat2, cat2 */
3808 iptr->opc = ICMD_DUP_X1;
3812 /* ..., cat1, cat2 */
3813 #ifdef ENABLE_VERIFIER
3816 if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3817 goto throw_stack_category_error;
3820 iptr->opc = ICMD_DUP_X2;
3826 /* ..., ????, ????, cat1 */
3828 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3829 /* ..., cat2, ????, cat1 */
3830 #ifdef ENABLE_VERIFIER
3832 if (IS_2_WORD_TYPE(curstack->prev->type))
3833 goto throw_stack_category_error;
3836 iptr->opc = ICMD_DUP2_X1;
3840 /* ..., cat1, ????, cat1 */
3841 #ifdef ENABLE_VERIFIER
3844 if (IS_2_WORD_TYPE(curstack->prev->type)
3845 || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3846 goto throw_stack_category_error;
3850 src1 = curstack->prev->prev->prev;
3851 src2 = curstack->prev->prev;
3852 src3 = curstack->prev;
3854 POPANY; POPANY; POPANY; POPANY;
3857 /* move non-temporary sources out of the way */
3858 if (!IS_TEMPVAR(src2)) {
3859 MOVE_TO_TEMP(src2); iptr++; len--;
3861 if (!IS_TEMPVAR(src3)) {
3862 MOVE_TO_TEMP(src3); iptr++; len--;
3864 if (!IS_TEMPVAR(src4)) {
3865 MOVE_TO_TEMP(src4); iptr++; len--;
3868 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3869 DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3871 MOVE_UP(src1); iptr++; len--;
3872 MOVE_UP(src2); iptr++; len--;
3873 MOVE_UP(src3); iptr++; len--;
3874 MOVE_UP(src4); iptr++; len--;
3876 COPY_DOWN(curstack, dst2); iptr++; len--;
3877 COPY_DOWN(curstack->prev, dst1);
3879 coalescing_boundary = sd.new;
3883 /* pop 2 push 2 swap */
3886 #ifdef ENABLE_VERIFIER
3889 if (IS_2_WORD_TYPE(curstack->type)
3890 || IS_2_WORD_TYPE(curstack->prev->type))
3891 goto throw_stack_category_error;
3895 src1 = curstack->prev;
3900 /* move non-temporary sources out of the way */
3901 if (!IS_TEMPVAR(src1)) {
3902 MOVE_TO_TEMP(src1); iptr++; len--;
3905 MOVE_UP(src2); iptr++; len--;
3908 coalescing_boundary = sd.new;
3915 coalescing_boundary = sd.new;
3916 #if !SUPPORT_DIVISION
3917 bte = iptr->sx.s23.s3.bte;
3920 if (md->memuse > rd->memuse)
3921 rd->memuse = md->memuse;
3922 if (md->argintreguse > rd->argintreguse)
3923 rd->argintreguse = md->argintreguse;
3925 /* make all stack variables saved */
3929 sd.var[copy->varnum].flags |= SAVEDVAR;
3930 copy->flags |= SAVEDVAR;
3935 #endif /* !SUPPORT_DIVISION */
3946 COUNT(count_pcmd_op);
3947 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3952 coalescing_boundary = sd.new;
3953 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3954 bte = iptr->sx.s23.s3.bte;
3957 if (md->memuse > rd->memuse)
3958 rd->memuse = md->memuse;
3959 if (md->argintreguse > rd->argintreguse)
3960 rd->argintreguse = md->argintreguse;
3961 /* XXX non-leaf method? */
3963 /* make all stack variables saved */
3967 sd.var[copy->varnum].flags |= SAVEDVAR;
3968 copy->flags |= SAVEDVAR;
3973 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3978 #if SUPPORT_LONG_LOGICAL
3982 #endif /* SUPPORT_LONG_LOGICAL */
3983 COUNT(count_pcmd_op);
3984 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3990 COUNT(count_pcmd_op);
3991 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3999 COUNT(count_pcmd_op);
4000 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
4008 COUNT(count_pcmd_op);
4009 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
4013 COUNT(count_pcmd_op);
4014 #if SUPPORT_LONG_CMP_CONST
4015 if ((len == 0) || (iptr[1].sx.val.i != 0))
4018 switch (iptr[1].opc) {
4020 iptr->opc = ICMD_IF_LCMPEQ;
4022 iptr->dst.insindex = iptr[1].dst.insindex;
4023 iptr[1].opc = ICMD_NOP;
4025 OP2_BRANCH(TYPE_LNG, TYPE_LNG);
4028 COUNT(count_pcmd_bra);
4031 iptr->opc = ICMD_IF_LCMPNE;
4032 goto icmd_lcmp_if_tail;
4034 iptr->opc = ICMD_IF_LCMPLT;
4035 goto icmd_lcmp_if_tail;
4037 iptr->opc = ICMD_IF_LCMPGT;
4038 goto icmd_lcmp_if_tail;
4040 iptr->opc = ICMD_IF_LCMPLE;
4041 goto icmd_lcmp_if_tail;
4043 iptr->opc = ICMD_IF_LCMPGE;
4044 goto icmd_lcmp_if_tail;
4050 #endif /* SUPPORT_LONG_CMP_CONST */
4051 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
4054 /* XXX why is this deactivated? */
4057 COUNT(count_pcmd_op);
4058 if ((len == 0) || (iptr[1].sx.val.i != 0))
4061 switch (iptr[1].opc) {
4063 iptr->opc = ICMD_IF_FCMPEQ;
4065 iptr->dst.insindex = iptr[1].dst.insindex;
4066 iptr[1].opc = ICMD_NOP;
4068 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4071 COUNT(count_pcmd_bra);
4074 iptr->opc = ICMD_IF_FCMPNE;
4075 goto icmd_if_fcmpl_tail;
4077 iptr->opc = ICMD_IF_FCMPL_LT;
4078 goto icmd_if_fcmpl_tail;
4080 iptr->opc = ICMD_IF_FCMPL_GT;
4081 goto icmd_if_fcmpl_tail;
4083 iptr->opc = ICMD_IF_FCMPL_LE;
4084 goto icmd_if_fcmpl_tail;
4086 iptr->opc = ICMD_IF_FCMPL_GE;
4087 goto icmd_if_fcmpl_tail;
4094 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4098 COUNT(count_pcmd_op);
4099 if ((len == 0) || (iptr[1].sx.val.i != 0))
4102 switch (iptr[1].opc) {
4104 iptr->opc = ICMD_IF_FCMPEQ;
4106 iptr->dst.insindex = iptr[1].dst.insindex;
4107 iptr[1].opc = ICMD_NOP;
4109 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4112 COUNT(count_pcmd_bra);
4115 iptr->opc = ICMD_IF_FCMPNE;
4116 goto icmd_if_fcmpg_tail;
4118 iptr->opc = ICMD_IF_FCMPG_LT;
4119 goto icmd_if_fcmpg_tail;
4121 iptr->opc = ICMD_IF_FCMPG_GT;
4122 goto icmd_if_fcmpg_tail;
4124 iptr->opc = ICMD_IF_FCMPG_LE;
4125 goto icmd_if_fcmpg_tail;
4127 iptr->opc = ICMD_IF_FCMPG_GE;
4128 goto icmd_if_fcmpg_tail;
4135 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4139 COUNT(count_pcmd_op);
4140 if ((len == 0) || (iptr[1].sx.val.i != 0))
4143 switch (iptr[1].opc) {
4145 iptr->opc = ICMD_IF_DCMPEQ;
4147 iptr->dst.insindex = iptr[1].dst.insindex;
4148 iptr[1].opc = ICMD_NOP;
4150 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4153 COUNT(count_pcmd_bra);
4156 iptr->opc = ICMD_IF_DCMPNE;
4157 goto icmd_if_dcmpl_tail;
4159 iptr->opc = ICMD_IF_DCMPL_LT;
4160 goto icmd_if_dcmpl_tail;
4162 iptr->opc = ICMD_IF_DCMPL_GT;
4163 goto icmd_if_dcmpl_tail;
4165 iptr->opc = ICMD_IF_DCMPL_LE;
4166 goto icmd_if_dcmpl_tail;
4168 iptr->opc = ICMD_IF_DCMPL_GE;
4169 goto icmd_if_dcmpl_tail;
4176 OPTT2_1(TYPE_DBL, TYPE_INT);
4180 COUNT(count_pcmd_op);
4181 if ((len == 0) || (iptr[1].sx.val.i != 0))
4184 switch (iptr[1].opc) {
4186 iptr->opc = ICMD_IF_DCMPEQ;
4188 iptr->dst.insindex = iptr[1].dst.insindex;
4189 iptr[1].opc = ICMD_NOP;
4191 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4194 COUNT(count_pcmd_bra);
4197 iptr->opc = ICMD_IF_DCMPNE;
4198 goto icmd_if_dcmpg_tail;
4200 iptr->opc = ICMD_IF_DCMPG_LT;
4201 goto icmd_if_dcmpg_tail;
4203 iptr->opc = ICMD_IF_DCMPG_GT;
4204 goto icmd_if_dcmpg_tail;
4206 iptr->opc = ICMD_IF_DCMPG_LE;
4207 goto icmd_if_dcmpg_tail;
4209 iptr->opc = ICMD_IF_DCMPG_GE;
4210 goto icmd_if_dcmpg_tail;
4217 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4222 COUNT(count_pcmd_op);
4223 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4228 COUNT(count_pcmd_op);
4229 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4238 case ICMD_INT2SHORT:
4239 COUNT(count_pcmd_op);
4240 OP1_1(TYPE_INT, TYPE_INT);
4243 COUNT(count_pcmd_op);
4244 OP1_1(TYPE_LNG, TYPE_LNG);
4247 COUNT(count_pcmd_op);
4248 OP1_1(TYPE_FLT, TYPE_FLT);
4251 COUNT(count_pcmd_op);
4252 OP1_1(TYPE_DBL, TYPE_DBL);
4256 COUNT(count_pcmd_op);
4257 OP1_1(TYPE_INT, TYPE_LNG);
4260 COUNT(count_pcmd_op);
4261 OP1_1(TYPE_INT, TYPE_FLT);
4264 COUNT(count_pcmd_op);
4265 OP1_1(TYPE_INT, TYPE_DBL);
4268 COUNT(count_pcmd_op);
4269 OP1_1(TYPE_LNG, TYPE_INT);
4272 COUNT(count_pcmd_op);
4273 OP1_1(TYPE_LNG, TYPE_FLT);
4276 COUNT(count_pcmd_op);
4277 OP1_1(TYPE_LNG, TYPE_DBL);
4280 COUNT(count_pcmd_op);
4281 OP1_1(TYPE_FLT, TYPE_INT);
4284 COUNT(count_pcmd_op);
4285 OP1_1(TYPE_FLT, TYPE_LNG);
4288 COUNT(count_pcmd_op);
4289 OP1_1(TYPE_FLT, TYPE_DBL);
4292 COUNT(count_pcmd_op);
4293 OP1_1(TYPE_DBL, TYPE_INT);
4296 COUNT(count_pcmd_op);
4297 OP1_1(TYPE_DBL, TYPE_LNG);
4300 COUNT(count_pcmd_op);
4301 OP1_1(TYPE_DBL, TYPE_FLT);
4304 case ICMD_CHECKCAST:
4305 coalescing_boundary = sd.new;
4306 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4307 /* array type cast-check */
4309 bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4312 if (md->memuse > rd->memuse)
4313 rd->memuse = md->memuse;
4314 if (md->argintreguse > rd->argintreguse)
4315 rd->argintreguse = md->argintreguse;
4317 /* make all stack variables saved */
4321 sd.var[copy->varnum].flags |= SAVEDVAR;
4322 copy->flags |= SAVEDVAR;
4326 OP1_1(TYPE_ADR, TYPE_ADR);
4329 case ICMD_INSTANCEOF:
4330 case ICMD_ARRAYLENGTH:
4331 coalescing_boundary = sd.new;
4332 OP1_1(TYPE_ADR, TYPE_INT);
4336 case ICMD_ANEWARRAY:
4337 coalescing_boundary = sd.new;
4338 OP1_1(TYPE_INT, TYPE_ADR);
4342 coalescing_boundary = sd.new;
4343 COUNT(count_check_null);
4344 COUNT(count_pcmd_mem);
4345 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4346 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4351 case ICMD_GETSTATIC:
4352 coalescing_boundary = sd.new;
4353 COUNT(count_pcmd_mem);
4354 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4355 OP0_1(fmiref->parseddesc.fd->type);
4359 coalescing_boundary = sd.new;
4366 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
4367 tbptr->type = BBTYPE_SBR;
4369 assert(sd.bptr->next); /* XXX exception */
4370 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4371 #if defined(ENABLE_VERIFIER)
4372 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4375 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4379 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4381 /* We need to check for overflow right here because
4382 * the pushed value is poped afterwards */
4385 superblockend = true;
4386 /* XXX should not be marked as interface, as it does not need to be */
4387 /* allocated. Same for the invar of the target. */
4390 /* pop many push any */
4394 bte = iptr->sx.s23.s3.bte;
4398 case ICMD_INVOKESTATIC:
4399 case ICMD_INVOKESPECIAL:
4400 case ICMD_INVOKEVIRTUAL:
4401 case ICMD_INVOKEINTERFACE:
4402 COUNT(count_pcmd_met);
4404 /* Check for functions to replace with builtin
4407 if (builtintable_replace_function(iptr))
4410 INSTRUCTION_GET_METHODDESC(iptr, md);
4411 /* XXX resurrect this COUNT? */
4412 /* if (lm->flags & ACC_STATIC) */
4413 /* {COUNT(count_check_null);} */
4417 coalescing_boundary = sd.new;
4421 if (md->memuse > rd->memuse)
4422 rd->memuse = md->memuse;
4423 if (md->argintreguse > rd->argintreguse)
4424 rd->argintreguse = md->argintreguse;
4425 if (md->argfltreguse > rd->argfltreguse)
4426 rd->argfltreguse = md->argfltreguse;
4430 iptr->s1.argcount = stackdepth;
4431 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4434 for (i-- ; i >= 0; i--) {
4435 iptr->sx.s23.s2.args[i] = copy->varnum;
4437 /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4438 /* -> won't help anyway */
4439 if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4441 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4442 /* If we pass float arguments in integer argument registers, we
4443 * are not allowed to precolor them here. Floats have to be moved
4444 * to this regs explicitly in codegen().
4445 * Only arguments that are passed by stack anyway can be precolored
4446 * (michi 2005/07/24) */
4447 if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4448 (!IS_FLT_DBL_TYPE(copy->type)
4449 || md->params[i].inmemory)) {
4451 if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4456 if (md->params[i].inmemory) {
4457 sd.var[copy->varnum].vv.regoff =
4458 md->params[i].regoff;
4459 sd.var[copy->varnum].flags |=
4463 if (IS_FLT_DBL_TYPE(copy->type)) {
4464 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4465 assert(0); /* XXX is this assert ok? */
4467 sd.var[copy->varnum].vv.regoff =
4468 rd->argfltregs[md->params[i].regoff];
4469 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4472 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4473 if (IS_2_WORD_TYPE(copy->type))
4474 sd.var[copy->varnum].vv.regoff =
4475 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4476 rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4479 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4480 sd.var[copy->varnum].vv.regoff =
4481 rd->argintregs[md->params[i].regoff];
4489 /* deal with live-through stack slots "under" the */
4495 iptr->sx.s23.s2.args[i++] = copy->varnum;
4496 sd.var[copy->varnum].flags |= SAVEDVAR;
4497 copy->flags |= SAVEDVAR | PASSTHROUGH;
4501 /* pop the arguments */
4510 /* push the return value */
4512 if (md->returntype.type != TYPE_VOID) {
4513 GET_NEW_VAR(sd, new_index, md->returntype.type);
4514 DST(md->returntype.type, new_index);
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 /* show state after last instruction */
4608 #if defined(STACK_VERBOSE)
4609 stack_verbose_show_state(&sd, NULL, curstack);
4612 /* stack slots at basic block end become interfaces */
4614 sd.bptr->outdepth = stackdepth;
4615 sd.bptr->outvars = DMNEW(s4, stackdepth);
4618 for (copy = curstack; copy; i--, copy = copy->prev) {
4622 /* with the new vars rd->interfaces will be removed */
4623 /* and all in and outvars have to be STACKVARS! */
4624 /* in the moment i.e. SWAP with in and out vars can */
4625 /* create an unresolvable conflict */
4630 v = sd.var + copy->varnum;
4633 /* do not allocate variables for returnAddresses */
4635 if (t != TYPE_RET) {
4636 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4637 /* no interface var until now for this depth and */
4639 jd->interface_map[i*5 + t].flags = v->flags;
4642 jd->interface_map[i*5 + t].flags |= v->flags;
4646 sd.bptr->outvars[i] = copy->varnum;
4649 /* check if interface slots at basic block begin must be saved */
4651 for (i=0; i<sd.bptr->indepth; ++i) {
4652 varinfo *v = sd.var + sd.bptr->invars[i];
4657 if (t != TYPE_RET) {
4658 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4659 /* no interface var until now for this depth and */
4661 jd->interface_map[i*5 + t].flags = v->flags;
4664 jd->interface_map[i*5 + t].flags |= v->flags;
4669 /* store the number of this block's variables */
4671 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4673 #if defined(STACK_VERBOSE)
4674 stack_verbose_block_exit(&sd, superblockend);
4677 /* reach the following block, if any */
4680 if (!stack_reach_next_block(&sd))
4685 } while (sd.repeat && !deadcode);
4687 /* reset locals of TYPE_RET to TYPE_ADR */
4689 for (i=0; i<sd.localcount; ++i) {
4690 if (sd.var[i].type == TYPE_RET)
4691 sd.var[i].type = TYPE_ADR;
4694 /* mark temporaries of TYPE_RET as PREALLOC to avoid allocation */
4696 for (i=sd.localcount; i<sd.vartop; ++i) {
4697 if (sd.var[i].type == TYPE_RET)
4698 sd.var[i].flags |= PREALLOC;
4701 /* XXX hack to fix up the ranges of the cloned single-block handlers */
4703 ex = jd->exceptiontable;
4704 for (; ex != NULL; ex = ex->down) {
4705 if (ex->start == ex->end) {
4706 assert(ex->end->next);
4707 ex->end = ex->end->next;
4711 /* store number of created variables */
4713 jd->vartop = sd.vartop;
4715 /* gather statistics *****************************************************/
4717 #if defined(ENABLE_STATISTICS)
4719 if (jd->basicblockcount > count_max_basic_blocks)
4720 count_max_basic_blocks = jd->basicblockcount;
4721 count_basic_blocks += jd->basicblockcount;
4722 if (jd->instructioncount > count_max_javainstr)
4723 count_max_javainstr = jd->instructioncount;
4724 count_javainstr += jd->instructioncount;
4725 if (jd->stackcount > count_upper_bound_new_stack)
4726 count_upper_bound_new_stack = jd->stackcount;
4727 if ((sd.new - jd->stack) > count_max_new_stack)
4728 count_max_new_stack = (sd.new - jd->stack);
4730 sd.bptr = jd->basicblocks;
4731 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4732 if (sd.bptr->flags > BBREACHED) {
4733 if (sd.bptr->indepth >= 10)
4734 count_block_stack[10]++;
4736 count_block_stack[sd.bptr->indepth]++;
4737 len = sd.bptr->icount;
4739 count_block_size_distribution[len]++;
4741 count_block_size_distribution[10]++;
4743 count_block_size_distribution[11]++;
4745 count_block_size_distribution[12]++;
4747 count_block_size_distribution[13]++;
4749 count_block_size_distribution[14]++;
4751 count_block_size_distribution[15]++;
4753 count_block_size_distribution[16]++;
4755 count_block_size_distribution[17]++;
4759 if (iteration_count == 1)
4760 count_analyse_iterations[0]++;
4761 else if (iteration_count == 2)
4762 count_analyse_iterations[1]++;
4763 else if (iteration_count == 3)
4764 count_analyse_iterations[2]++;
4765 else if (iteration_count == 4)
4766 count_analyse_iterations[3]++;
4768 count_analyse_iterations[4]++;
4770 if (jd->basicblockcount <= 5)
4771 count_method_bb_distribution[0]++;
4772 else if (jd->basicblockcount <= 10)
4773 count_method_bb_distribution[1]++;
4774 else if (jd->basicblockcount <= 15)
4775 count_method_bb_distribution[2]++;
4776 else if (jd->basicblockcount <= 20)
4777 count_method_bb_distribution[3]++;
4778 else if (jd->basicblockcount <= 30)
4779 count_method_bb_distribution[4]++;
4780 else if (jd->basicblockcount <= 40)
4781 count_method_bb_distribution[5]++;
4782 else if (jd->basicblockcount <= 50)
4783 count_method_bb_distribution[6]++;
4784 else if (jd->basicblockcount <= 75)
4785 count_method_bb_distribution[7]++;
4787 count_method_bb_distribution[8]++;
4789 #endif /* defined(ENABLE_STATISTICS) */
4791 /* everything's ok *******************************************************/
4795 /* goto labels for throwing verifier exceptions **************************/
4797 #if defined(ENABLE_VERIFIER)
4799 throw_stack_underflow:
4800 exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4803 throw_stack_overflow:
4804 exceptions_throw_verifyerror(m, "Stack size too large");
4807 throw_stack_type_error:
4808 exceptions_throw_verifyerror_for_stack(m, expectedtype);
4811 throw_stack_category_error:
4812 exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4819 /* stack_javalocals_store ******************************************************
4821 Model the effect of a ?STORE instruction upon the given javalocals array.
4824 iptr.............the ?STORE instruction
4825 javalocals.......the javalocals array to modify
4827 *******************************************************************************/
4829 void stack_javalocals_store(instruction *iptr, s4 *javalocals)
4831 s4 idx; /* index into the jd->var array */
4832 s4 j; /* java local index */
4834 idx = iptr->dst.varindex;
4835 j = iptr->sx.s23.s3.javaindex;
4838 if (iptr->flags.bits & INS_FLAG_RETADDR)
4839 javalocals[j] = iptr->sx.s23.s2.retaddrnr;
4841 javalocals[j] = idx;
4843 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
4844 javalocals[j-1] = UNUSED;
4846 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
4847 javalocals[j+1] = UNUSED;
4852 /* functions for verbose stack analysis output ********************************/
4854 #if defined(STACK_VERBOSE)
4855 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4857 printf("%c", show_jit_type_letters[v->type]);
4858 if (v->type == TYPE_RET)
4859 printf("{L%03d}", v->vv.retaddr->nr);
4863 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4865 assert(index >= 0 && index < sd->vartop);
4866 stack_verbose_show_varinfo(sd, sd->var + index);
4870 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4874 printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4876 for (i=0; i<bptr->indepth; ++i) {
4879 stack_verbose_show_variable(sd, bptr->invars[i]);
4884 printf("] javalocals ");
4885 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4886 printf(" inlocals [");
4887 if (bptr->inlocals) {
4888 for (i=0; i<sd->localcount; ++i) {
4891 stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4896 printf("] out:%d [", bptr->outdepth);
4897 if (bptr->outvars) {
4898 for (i=0; i<bptr->outdepth; ++i) {
4901 stack_verbose_show_variable(sd, bptr->outvars[i]);
4909 printf(" (clone of L%03d)", bptr->original->nr);
4911 basicblock *b = bptr->copied_to;
4913 printf(" (copied to ");
4914 for (; b; b = b->copied_to)
4915 printf("L%03d ", b->nr);
4922 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4926 printf("======================================== STACK %sANALYSE BLOCK ",
4927 (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4928 stack_verbose_show_block(sd, sd->bptr);
4931 if (sd->handlers[0]) {
4932 printf("HANDLERS: ");
4933 for (i=0; sd->handlers[i]; ++i) {
4934 printf("L%03d ", sd->handlers[i]->handler->nr);
4942 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4944 printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4945 stack_verbose_show_block(sd, sd->bptr);
4949 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackptr curstack)
4957 printf(" javalocals ");
4958 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4961 for(i = 0, sp = curstack; sp; sp = sp->prev)
4965 stack = MNEW(stackptr, depth);
4966 for(sp = curstack; sp; sp = sp->prev)
4969 for(i=0; i<depth; ++i) {
4973 v = &(sd->var[sp->varnum]);
4975 if (v->flags & INOUT)
4977 if (v->flags & PREALLOC)
4979 printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
4980 if (v->type == TYPE_RET) {
4981 printf("(L%03d)", v->vv.retaddr->nr);
4986 show_icmd(sd->jd, iptr, false, SHOW_PARSE);
4993 * These are local overrides for various environment variables in Emacs.
4994 * Please do not remove this and leave it at the end of the file, where
4995 * Emacs will automagically detect them.
4996 * ---------------------------------------------------------------------
4999 * indent-tabs-mode: t
5003 * vim:noexpandtab:sw=4:ts=4: