1 /* src/vm/jit/stack.c - stack analysis
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: stack.c 7736 2007-04-17 19:24:05Z edwin $
40 #include "mm/memory.h"
42 #include "native/native.h"
44 #include "toolbox/logging.h"
46 #include "vm/global.h"
47 #include "vm/builtin.h"
48 #include "vm/stringlocal.h"
51 #include "vm/jit/abi.h"
52 #include "vm/jit/cfg.h"
53 #include "vm/jit/codegen-common.h"
54 #include "vm/jit/parse.h"
55 #include "vm/jit/show.h"
57 #if defined(ENABLE_DISASSEMBLER)
58 # include "vm/jit/disass.h"
61 #include "vm/jit/jit.h"
62 #include "vm/jit/stack.h"
64 #if defined(ENABLE_SSA)
65 # include "vm/jit/optimizing/lsra.h"
66 # include "vm/jit/optimizing/ssa.h"
67 #elif defined(ENABLE_LSRA)
68 # include "vm/jit/allocator/lsra.h"
71 #include "vmcore/options.h"
72 #include "vm/resolve.h"
74 #if defined(ENABLE_STATISTICS)
75 # include "vmcore/statistics.h"
78 /*#define STACK_VERBOSE*/
81 /* macro for saving #ifdefs ***************************************************/
83 #if defined(ENABLE_STATISTICS)
84 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr) \
87 if (stackdepth >= 10) \
88 count_store_depth[10]++; \
90 count_store_depth[stackdepth]++; \
93 #else /* !defined(ENABLE_STATISTICS) */
94 #define STATISTICS_STACKDEPTH_DISTRIBUTION(distr)
98 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
101 /* For returnAddresses we use a field of the typeinfo to store from which */
102 /* subroutine the returnAddress will return, if used. */
103 /* XXX It would be nicer to use typeinfo.typeclass, but the verifier seems */
104 /* to need it initialised to NULL. This should be investigated. */
106 #if defined(ENABLE_VERIFIER)
107 #define SBRSTART typeinfo.elementclass.any
111 /* stackdata_t *****************************************************************
113 This struct holds internal data during stack analysis.
115 *******************************************************************************/
117 typedef struct stackdata_t stackdata_t;
120 basicblock *bptr; /* the current basic block being analysed */
121 stackptr new; /* next free stackelement */
122 s4 vartop; /* next free variable index */
123 s4 localcount; /* number of locals (at the start of var) */
124 s4 varcount; /* maximum number of variables expected */
125 s4 varsallocated; /* total number of variables allocated */
126 s4 maxlocals; /* max. number of Java locals */
127 varinfo *var; /* variable array (same as jd->var) */
128 s4 *javalocals; /* map from Java locals to jd->var indices */
129 methodinfo *m; /* the method being analysed */
130 jitdata *jd; /* current jitdata */
131 basicblock *last_real_block; /* the last block before the empty one */
132 bool repeat; /* if true, iterate the analysis again */
133 exception_entry **handlers; /* exception handlers for the current block */
134 exception_entry *extableend; /* points to the last exception entry */
135 stackelement exstack; /* instack for exception handlers */
139 /* macros for allocating/releasing variable indices *****************/
141 #define GET_NEW_INDEX(sd, new_varindex) \
143 assert((sd).vartop < (sd).varcount); \
144 (new_varindex) = ((sd).vartop)++; \
147 /* Not implemented now - could be used to reuse varindices. */
148 /* Pay attention to not release a localvar once implementing it! */
149 #define RELEASE_INDEX(sd, varindex)
151 #define GET_NEW_VAR(sd, newvarindex, newtype) \
153 GET_NEW_INDEX((sd), (newvarindex)); \
154 (sd).var[newvarindex].type = (newtype); \
158 /* macros for querying variable properties **************************/
160 #define IS_INOUT(sp) \
161 (sd.var[(sp)->varnum].flags & INOUT)
163 #define IS_PREALLOC(sp) \
164 (sd.var[(sp)->varnum].flags & PREALLOC)
166 #define IS_TEMPVAR(sp) \
167 ( ((sp)->varnum >= sd.localcount) \
168 && !(sd.var[(sp)->varnum].flags & (INOUT | PREALLOC)) )
171 #define IS_LOCALVAR_SD(sd, sp) \
172 ((sp)->varnum < (sd).localcount)
174 #define IS_LOCALVAR(sp) \
175 IS_LOCALVAR_SD(sd, (sp))
178 /* macros for setting variable properties ****************************/
180 #define SET_TEMPVAR(sp) \
182 if (IS_LOCALVAR((sp))) { \
183 stack_change_to_tempvar(&sd, (sp), iptr); \
185 sd.var[(sp)->varnum].flags &= ~(INOUT | PREALLOC); \
188 #define SET_PREALLOC(sp) \
190 assert(!IS_LOCALVAR((sp))); \
191 sd.var[(sp)->varnum].flags |= PREALLOC; \
195 /* macros for source operands ***************************************/
198 (iptr->s1.varindex = -1)
200 #define USE_S1(type1) \
203 CHECK_BASIC_TYPE(type1, curstack->type); \
204 iptr->s1.varindex = curstack->varnum; \
210 iptr->s1.varindex = curstack->varnum; \
213 #define USE_S1_S2(type1, type2) \
216 CHECK_BASIC_TYPE(type1, curstack->prev->type); \
217 CHECK_BASIC_TYPE(type2, curstack->type); \
218 iptr->sx.s23.s2.varindex = curstack->varnum; \
219 iptr->s1.varindex = curstack->prev->varnum; \
222 #define USE_S1_S2_ANY_ANY \
225 iptr->sx.s23.s2.varindex = curstack->varnum; \
226 iptr->s1.varindex = curstack->prev->varnum; \
229 #define USE_S1_S2_S3(type1, type2, type3) \
232 CHECK_BASIC_TYPE(type1, curstack->prev->prev->type); \
233 CHECK_BASIC_TYPE(type2, curstack->prev->type); \
234 CHECK_BASIC_TYPE(type3, curstack->type); \
235 iptr->sx.s23.s3.varindex = curstack->varnum; \
236 iptr->sx.s23.s2.varindex = curstack->prev->varnum; \
237 iptr->s1.varindex = curstack->prev->prev->varnum; \
240 /* The POPANY macro does NOT check stackdepth, or set stackdepth! */
243 if (curstack->varkind == UNDEFVAR) \
244 curstack->varkind = TEMPVAR; \
245 curstack = curstack->prev; \
248 #define POP_S1(type1) \
251 if (curstack->varkind == UNDEFVAR) \
252 curstack->varkind = TEMPVAR; \
253 curstack = curstack->prev; \
259 if (curstack->varkind == UNDEFVAR) \
260 curstack->varkind = TEMPVAR; \
261 curstack = curstack->prev; \
264 #define POP_S1_S2(type1, type2) \
266 USE_S1_S2(type1, type2); \
267 if (curstack->varkind == UNDEFVAR) \
268 curstack->varkind = TEMPVAR; \
269 if (curstack->prev->varkind == UNDEFVAR) \
270 curstack->prev->varkind = TEMPVAR; \
271 curstack = curstack->prev->prev; \
274 #define POP_S1_S2_ANY_ANY \
277 if (curstack->varkind == UNDEFVAR) \
278 curstack->varkind = TEMPVAR; \
279 if (curstack->prev->varkind == UNDEFVAR) \
280 curstack->prev->varkind = TEMPVAR; \
281 curstack = curstack->prev->prev; \
284 #define POP_S1_S2_S3(type1, type2, type3) \
286 USE_S1_S2_S3(type1, type2, type3); \
287 if (curstack->varkind == UNDEFVAR) \
288 curstack->varkind = TEMPVAR; \
289 if (curstack->prev->varkind == UNDEFVAR) \
290 curstack->prev->varkind = TEMPVAR; \
291 if (curstack->prev->prev->varkind == UNDEFVAR) \
292 curstack->prev->prev->varkind = TEMPVAR; \
293 curstack = curstack->prev->prev->prev; \
300 /* macros for setting the destination operand ***********************/
303 (iptr->dst.varindex = -1)
305 #define DST(typed, index) \
307 NEWSTACKn((typed),(index)); \
308 curstack->creator = iptr; \
309 iptr->dst.varindex = (index); \
312 #define DST_LOCALVAR(typed, index) \
314 NEWSTACK((typed), LOCALVAR, (index)); \
315 curstack->creator = iptr; \
316 iptr->dst.varindex = (index); \
320 /* macro for propagating constant values ****************************/
322 #if defined(ENABLE_VERIFIER)
323 #define COPY_VAL_AND_TYPE_VAR(sv, dv) \
325 if (((dv)->type = (sv)->type) == TYPE_RET) { \
326 (dv)->vv = (sv)->vv; \
327 (dv)->SBRSTART = (sv)->SBRSTART; \
331 #define COPY_VAL_AND_TYPE_VAR(sv, dv) \
333 (dv)->type = (sv)->type; \
334 if (((dv)->type = (sv)->type) == TYPE_RET) { \
335 (dv)->vv = (sv)->vv; \
340 #define COPY_VAL_AND_TYPE(sd, sindex, dindex) \
341 COPY_VAL_AND_TYPE_VAR((sd).var + (sindex), (sd).var + (dindex))
344 /* stack modelling macros *******************************************/
346 #define OP0_1(typed) \
349 GET_NEW_VAR(sd, new_index, (typed)); \
350 DST((typed), new_index); \
361 #define OP1_BRANCH(type1) \
367 #define OP1_1(type1, typed) \
370 GET_NEW_VAR(sd, new_index, (typed)); \
371 DST(typed, new_index); \
374 #define OP2_1(type1, type2, typed) \
376 POP_S1_S2(type1, type2); \
377 GET_NEW_VAR(sd, new_index, (typed)); \
378 DST(typed, new_index); \
393 #define OP1_0(type1) \
400 #define OP2_0(type1, type2) \
402 POP_S1_S2(type1, type2); \
407 #define OP2_BRANCH(type1, type2) \
409 POP_S1_S2(type1, type2); \
413 #define OP2_0_ANY_ANY \
420 #define OP3_0(type1, type2, type3) \
422 POP_S1_S2_S3(type1, type2, type3); \
427 #define LOAD(type1, index) \
429 DST_LOCALVAR(type1, index); \
433 #define STORE(type1, index) \
440 /* macros for DUP elimination ***************************************/
442 /* XXX replace NEW_VAR with NEW_INDEX */
443 #define DUP_SLOT(sp) \
445 GET_NEW_VAR(sd, new_index, (sp)->type); \
446 COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
447 NEWSTACK((sp)->type, TEMPVAR, new_index); \
450 /* does not check input stackdepth */
451 #define MOVE_UP(sp) \
453 iptr->opc = ICMD_MOVE; \
454 iptr->s1.varindex = (sp)->varnum; \
456 curstack->creator = iptr; \
457 iptr->dst.varindex = curstack->varnum; \
461 /* does not check input stackdepth */
462 #define COPY_UP(sp) \
465 iptr->opc = ICMD_COPY; \
466 iptr->s1.varindex = (sp)->varnum; \
468 curstack->creator = iptr; \
469 iptr->dst.varindex = curstack->varnum; \
473 #define COPY_DOWN(s, d) \
476 iptr->opc = ICMD_COPY; \
477 iptr->s1.varindex = (s)->varnum; \
478 iptr->dst.varindex = (d)->varnum; \
479 (d)->creator = iptr; \
482 #define MOVE_TO_TEMP(sp) \
484 GET_NEW_INDEX(sd, new_index); \
485 iptr->opc = ICMD_MOVE; \
486 iptr->s1.varindex = (sp)->varnum; \
487 iptr->dst.varindex = new_index; \
488 COPY_VAL_AND_TYPE(sd, (sp)->varnum, new_index); \
489 (sp)->varnum = new_index; \
490 (sp)->varkind = TEMPVAR; \
493 /* macros for branching / reaching basic blocks *********************/
495 #define BRANCH_TARGET(bt, tempbptr) \
497 tempbptr = (bt).block; \
498 tempbptr = stack_mark_reached(&sd, tempbptr, curstack, \
500 if (tempbptr == NULL) \
502 (bt).block = tempbptr; \
505 #define BRANCH(tempbptr) \
506 BRANCH_TARGET(iptr->dst, tempbptr)
509 /* forward declarations *******************************************************/
511 static void stack_create_invars(stackdata_t *sd, basicblock *b,
512 stackptr curstack, int stackdepth);
513 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b);
515 #if defined(STACK_VERBOSE)
516 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v);
517 static void stack_verbose_show_variable(stackdata_t *sd, s4 index);
518 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr);
519 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse);
520 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend);
521 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr,
526 /* stack_init ******************************************************************
528 Initialized the stack analysis subsystem (called by jit_init).
530 *******************************************************************************/
532 bool stack_init(void)
538 /* stack_grow_variable_array ***************************************************
540 Grow the variable array so the given number of additional variables fits in.
541 The number is added to `varcount`, which is the maximum number of variables
542 we expect to need at this point. The actual number of variables
543 (`varsallocated`) may be larger than that, in order to avoid too many
547 sd...........stack analysis data
548 num..........number of additional variables
550 *******************************************************************************/
552 static void stack_grow_variable_array(stackdata_t *sd, s4 num)
558 if (sd->varcount + num > sd->varsallocated) {
559 newsize = 2*sd->varsallocated + num;
561 sd->var = DMREALLOC(sd->var, varinfo, sd->varsallocated, newsize);
562 MZERO(sd->var + sd->varsallocated, varinfo, (newsize - sd->varsallocated));
563 sd->varsallocated = newsize;
564 sd->jd->var = sd->var;
568 sd->jd->varcount += num;
570 assert(sd->varcount <= sd->varsallocated);
574 /* stack_append_block **********************************************************
576 Append the given block after the last real block of the method (before
577 the pseudo-block at the end).
580 sd...........stack analysis data
581 b............the block to append
583 *******************************************************************************/
585 static void stack_append_block(stackdata_t *sd, basicblock *b)
587 #if defined(STACK_VERBOSE)
588 printf("APPENDING BLOCK L%0d\n", b->nr);
591 b->next = sd->last_real_block->next;
592 sd->last_real_block->next = b;
593 sd->last_real_block = b;
594 b->nr = sd->jd->basicblockcount++;
595 b->next->nr = b->nr + 1;
599 /* stack_clone_block ***********************************************************
601 Create a copy of the given block and insert it at the end of the method.
603 CAUTION: This function does not copy the any variables or the instruction
604 list. It _does_, however, reserve space for the block's invars in the
608 sd...........stack analysis data
609 b............the block to clone
612 a pointer to the copy
614 *******************************************************************************/
616 static basicblock * stack_clone_block(stackdata_t *sd, basicblock *b)
620 clone = DNEW(basicblock);
623 clone->iinstr = NULL;
624 clone->inlocals = NULL;
625 clone->javalocals = NULL;
626 clone->invars = NULL;
628 clone->original = (b->original) ? b->original : b;
629 clone->copied_to = clone->original->copied_to;
630 clone->original->copied_to = clone;
632 clone->flags = BBREACHED;
634 stack_append_block(sd, clone);
636 /* reserve space for the invars of the clone */
638 stack_grow_variable_array(sd, b->indepth);
640 #if defined(STACK_VERBOSE)
641 printf("cloning block L%03d ------> L%03d\n", b->nr, clone->nr);
648 /* stack_create_locals *********************************************************
650 Create the local variables for the start of the given basic block.
653 sd...........stack analysis data
654 b............block to create the locals for
656 *******************************************************************************/
658 static void stack_create_locals(stackdata_t *sd, basicblock *b)
664 /* copy the current state of the local variables */
665 /* (one extra local is needed by the verifier) */
667 dv = DMNEW(varinfo, sd->localcount + VERIFIER_EXTRA_LOCALS);
669 for (i=0; i<sd->localcount; ++i)
672 /* the current map from java locals to cacao variables */
674 jl = DMNEW(s4, sd->maxlocals);
676 MCOPY(jl, sd->javalocals, s4, sd->maxlocals);
680 /* stack_merge_locals **********************************************************
682 Merge local variables at the beginning of the given basic block.
685 sd...........stack analysis data
686 b............the block that is reached
688 *******************************************************************************/
690 static void stack_merge_locals(stackdata_t *sd, basicblock *b)
696 /* If a javalocal is mapped to different cacao locals along the */
697 /* incoming control-flow edges, it becomes undefined. */
699 for (i=0; i<sd->maxlocals; ++i) {
700 if (b->javalocals[i] != UNUSED && b->javalocals[i] != sd->javalocals[i]) {
701 b->javalocals[i] = UNUSED;
702 if (b->flags >= BBFINISHED)
703 b->flags = BBTYPECHECK_REACHED;
704 if (b->nr <= sd->bptr->nr)
709 #if defined(ENABLE_VERIFIER)
711 for (i=0; i<sd->localcount; ++i) {
712 dv = b->inlocals + i;
714 if ((sv->type == TYPE_RET && dv->type == TYPE_RET)
715 && (sv->SBRSTART != dv->SBRSTART))
717 #if defined(STACK_VERBOSE)
718 printf("JSR MISMATCH: setting variable %d to VOID\n", i);
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)
1121 #if defined(STACK_VERBOSE)
1122 printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1125 /* mark targets of backward branches */
1127 if (b->nr <= sd->bptr->nr)
1128 b->bitflags |= BBFLAG_REPLACEMENT;
1130 if (b->flags < BBREACHED) {
1131 /* b is reached for the first time. Create its invars. */
1133 #if defined(STACK_VERBOSE)
1134 printf("reached L%03d for the first time\n", b->nr);
1137 stack_create_invars(sd, b, curstack, stackdepth);
1139 b->flags = BBREACHED;
1144 /* b has been reached before. Check that its invars match. */
1146 return stack_check_invars(sd, b, curstack, stackdepth);
1151 /* stack_mark_reached_from_outvars *********************************************
1153 Mark the given block reached and propagate the outvars of the current block
1154 and the current locals to it. This function specializes the target block,
1155 if necessary, and returns a pointer to the specialized target.
1158 sd...........stack analysis data
1159 b............the block to reach
1162 a pointer to (a specialized version of) the target
1163 NULL.........a VerifyError has been thrown
1165 *******************************************************************************/
1167 static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
1171 #if defined(STACK_VERBOSE)
1172 printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1175 /* mark targets of backward branches */
1177 if (b->nr <= sd->bptr->nr)
1178 b->bitflags |= BBFLAG_REPLACEMENT;
1180 if (b->flags < BBREACHED) {
1181 /* b is reached for the first time. Create its invars. */
1183 #if defined(STACK_VERBOSE)
1184 printf("reached L%03d for the first time\n", b->nr);
1187 stack_create_invars_from_outvars(sd, b);
1189 b->flags = BBREACHED;
1194 /* b has been reached before. Check that its invars match. */
1196 return stack_check_invars_from_outvars(sd, b);
1201 /* stack_reach_next_block ******************************************************
1203 Mark the following block reached and propagate the outvars of the
1204 current block and the current locals to it. This function
1205 specializes the target block, if necessary, and returns a pointer
1206 to the specialized target.
1209 sd...........stack analysis data
1212 a pointer to (a specialized version of) the following block
1213 NULL.........a VerifyError has been thrown
1215 *******************************************************************************/
1217 static bool stack_reach_next_block(stackdata_t *sd)
1222 tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
1223 tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
1228 if (tbptr != sd->bptr->next) {
1229 #if defined(STACK_VERBOSE)
1230 printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
1232 iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
1233 assert(iptr->opc == ICMD_NOP);
1234 iptr->opc = ICMD_GOTO;
1235 iptr->dst.block = tbptr;
1237 if (tbptr->flags < BBFINISHED)
1238 sd->repeat = true; /* XXX check if we really need to repeat */
1245 /* stack_reach_handlers ********************************************************
1247 Reach the exception handlers for the current block.
1250 sd...........stack analysis data
1253 true.........everything ok
1254 false........a VerifyError has been thrown
1256 *******************************************************************************/
1258 static bool stack_reach_handlers(stackdata_t *sd)
1263 #if defined(STACK_VERBOSE)
1264 printf("reaching exception handlers...\n");
1267 for (i=0; sd->handlers[i]; ++i) {
1268 tbptr = sd->handlers[i]->handler;
1270 tbptr->type = BBTYPE_EXH;
1271 tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
1273 /* reach (and specialize) the handler block */
1275 tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
1280 sd->handlers[i]->handler = tbptr;
1287 /* stack_reanalyse_block ******************************************************
1289 Re-analyse the current block. This is called if either the block itself
1290 has already been analysed before, or the current block is a clone of an
1291 already analysed block, and this clone is reached for the first time.
1292 In the latter case, this function does all that is necessary for fully
1293 cloning the block (cloning the instruction list and variables, etc.).
1296 sd...........stack analysis data
1299 true.........everything ok
1300 false........a VerifyError has been thrown
1302 *******************************************************************************/
1304 #define RELOCATE(index) \
1306 if ((index) >= blockvarstart) \
1307 (index) += blockvarshift; \
1308 else if ((index) >= invarstart) \
1309 (index) += invarshift; \
1312 bool stack_reanalyse_block(stackdata_t *sd)
1324 branch_target_t *table;
1325 lookup_target_t *lookup;
1327 bool cloneinstructions;
1328 exception_entry *ex;
1330 #if defined(STACK_VERBOSE)
1331 stack_verbose_block_enter(sd, true);
1338 assert(orig != NULL);
1340 /* clone the instruction list */
1342 cloneinstructions = true;
1344 assert(orig->iinstr);
1346 iptr = DMNEW(instruction, len + 1);
1348 MCOPY(iptr, orig->iinstr, instruction, len);
1349 iptr[len].opc = ICMD_NOP;
1351 iptr[len].flags.bits = 0;
1355 /* reserve space for the clone's block variables */
1357 stack_grow_variable_array(sd, orig->varcount);
1359 /* we already have the invars set */
1361 assert(b->indepth == orig->indepth);
1363 /* calculate relocation shifts for invars and block variables */
1365 if (orig->indepth) {
1366 invarstart = orig->invars[0];
1367 invarshift = b->invars[0] - invarstart;
1370 invarstart = INT_MAX;
1373 blockvarstart = orig->varstart;
1374 blockvarshift = sd->vartop - blockvarstart;
1376 /* copy block variables */
1378 b->varstart = sd->vartop;
1379 b->varcount = orig->varcount;
1380 sd->vartop += b->varcount;
1381 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1385 b->outdepth = orig->outdepth;
1386 b->outvars = DMNEW(s4, orig->outdepth);
1387 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1389 /* clone exception handlers */
1391 for (i=0; sd->handlers[i]; ++i) {
1392 ex = DNEW(exception_entry);
1393 ex->handler = sd->handlers[i]->handler;
1395 ex->end = b; /* XXX hack, see end of stack_analyse */
1396 ex->catchtype = sd->handlers[i]->catchtype;
1399 assert(sd->extableend->down == NULL);
1400 sd->extableend->down = ex;
1401 sd->extableend = ex;
1402 sd->jd->exceptiontablelength++;
1404 sd->handlers[i] = ex;
1408 cloneinstructions = false;
1411 invarstart = sd->vartop;
1412 blockvarstart = sd->vartop;
1417 /* find exception handlers for the cloned block */
1419 ex = sd->jd->exceptiontable;
1420 for (; ex != NULL; ex = ex->down) {
1421 /* XXX the cloned exception handlers have identical */
1422 /* start end end blocks. */
1423 if ((ex->start == b) && (ex->end == b)) {
1424 sd->handlers[len++] = ex;
1427 sd->handlers[len] = NULL;
1430 #if defined(STACK_VERBOSE)
1431 printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1432 printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1435 /* mark block as finished */
1437 b->flags = BBFINISHED;
1439 /* initialize locals at the start of this block */
1442 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1444 MCOPY(sd->javalocals, b->javalocals, s4, sd->maxlocals);
1446 /* reach exception handlers for this block */
1448 if (!stack_reach_handlers(sd))
1451 superblockend = false;
1453 for (len = b->icount; len--; iptr++) {
1454 #if defined(STACK_VERBOSE)
1455 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1459 switch (iptr->opc) {
1461 j = iptr->s1.varindex;
1463 #if defined(ENABLE_VERIFIER)
1464 if (sd->var[j].type != TYPE_RET) {
1465 exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1470 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1471 superblockend = true;
1475 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1476 RELOCATE(iptr->dst.varindex);
1477 superblockend = true;
1481 superblockend = true;
1484 case ICMD_CHECKNULL:
1485 case ICMD_PUTSTATICCONST:
1493 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1494 superblockend = true;
1497 /* pop 0 push 1 const */
1505 /* pop 0 push 1 load */
1512 RELOCATE(iptr->dst.varindex);
1525 RELOCATE(iptr->sx.s23.s2.varindex);
1526 RELOCATE(iptr->s1.varindex);
1527 RELOCATE(iptr->dst.varindex);
1540 RELOCATE(iptr->sx.s23.s3.varindex);
1541 RELOCATE(iptr->sx.s23.s2.varindex);
1542 RELOCATE(iptr->s1.varindex);
1545 /* pop 1 push 0 store */
1552 RELOCATE(iptr->s1.varindex);
1554 j = iptr->dst.varindex;
1555 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1556 i = iptr->sx.s23.s3.javaindex;
1557 if (iptr->flags.bits & INS_FLAG_RETADDR) {
1558 iptr->sx.s23.s2.retaddrnr =
1559 UNUSED - (1 + sd->var[j].vv.retaddr->nr);
1560 sd->javalocals[i] = iptr->sx.s23.s2.retaddrnr;
1563 sd->javalocals[i] = j;
1564 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
1565 sd->javalocals[i-1] = UNUSED;
1566 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
1567 sd->javalocals[i+1] = UNUSED;
1578 RELOCATE(iptr->s1.varindex);
1579 superblockend = true;
1582 case ICMD_PUTSTATIC:
1583 case ICMD_PUTFIELDCONST:
1585 RELOCATE(iptr->s1.varindex);
1588 /* pop 1 push 0 branch */
1591 case ICMD_IFNONNULL:
1606 RELOCATE(iptr->s1.varindex);
1607 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1610 /* pop 1 push 0 table branch */
1612 case ICMD_TABLESWITCH:
1613 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1615 if (cloneinstructions) {
1616 table = DMNEW(branch_target_t, i);
1617 MCOPY(table, iptr->dst.table, branch_target_t, i);
1618 iptr->dst.table = table;
1621 table = iptr->dst.table;
1624 RELOCATE(iptr->s1.varindex);
1626 table->block = stack_mark_reached_from_outvars(sd, table->block);
1629 superblockend = true;
1632 case ICMD_LOOKUPSWITCH:
1633 i = iptr->sx.s23.s2.lookupcount;
1634 if (cloneinstructions) {
1635 lookup = DMNEW(lookup_target_t, i);
1636 MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1637 iptr->dst.lookup = lookup;
1640 lookup = iptr->dst.lookup;
1642 RELOCATE(iptr->s1.varindex);
1644 lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1647 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1648 superblockend = true;
1651 case ICMD_MONITORENTER:
1652 case ICMD_MONITOREXIT:
1653 RELOCATE(iptr->s1.varindex);
1656 /* pop 2 push 0 branch */
1658 case ICMD_IF_ICMPEQ:
1659 case ICMD_IF_ICMPNE:
1660 case ICMD_IF_ICMPLT:
1661 case ICMD_IF_ICMPGE:
1662 case ICMD_IF_ICMPGT:
1663 case ICMD_IF_ICMPLE:
1665 case ICMD_IF_LCMPEQ:
1666 case ICMD_IF_LCMPNE:
1667 case ICMD_IF_LCMPLT:
1668 case ICMD_IF_LCMPGE:
1669 case ICMD_IF_LCMPGT:
1670 case ICMD_IF_LCMPLE:
1672 case ICMD_IF_FCMPEQ:
1673 case ICMD_IF_FCMPNE:
1675 case ICMD_IF_FCMPL_LT:
1676 case ICMD_IF_FCMPL_GE:
1677 case ICMD_IF_FCMPL_GT:
1678 case ICMD_IF_FCMPL_LE:
1680 case ICMD_IF_FCMPG_LT:
1681 case ICMD_IF_FCMPG_GE:
1682 case ICMD_IF_FCMPG_GT:
1683 case ICMD_IF_FCMPG_LE:
1685 case ICMD_IF_DCMPEQ:
1686 case ICMD_IF_DCMPNE:
1688 case ICMD_IF_DCMPL_LT:
1689 case ICMD_IF_DCMPL_GE:
1690 case ICMD_IF_DCMPL_GT:
1691 case ICMD_IF_DCMPL_LE:
1693 case ICMD_IF_DCMPG_LT:
1694 case ICMD_IF_DCMPG_GE:
1695 case ICMD_IF_DCMPG_GT:
1696 case ICMD_IF_DCMPG_LE:
1698 case ICMD_IF_ACMPEQ:
1699 case ICMD_IF_ACMPNE:
1700 RELOCATE(iptr->sx.s23.s2.varindex);
1701 RELOCATE(iptr->s1.varindex);
1702 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1708 case ICMD_IASTORECONST:
1709 case ICMD_LASTORECONST:
1710 case ICMD_AASTORECONST:
1711 case ICMD_BASTORECONST:
1712 case ICMD_CASTORECONST:
1713 case ICMD_SASTORECONST:
1715 RELOCATE(iptr->sx.s23.s2.varindex);
1716 RELOCATE(iptr->s1.varindex);
1719 /* pop 0 push 1 copy */
1723 RELOCATE(iptr->dst.varindex);
1724 RELOCATE(iptr->s1.varindex);
1725 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1767 RELOCATE(iptr->sx.s23.s2.varindex);
1768 RELOCATE(iptr->s1.varindex);
1769 RELOCATE(iptr->dst.varindex);
1774 case ICMD_CHECKCAST:
1775 case ICMD_ARRAYLENGTH:
1776 case ICMD_INSTANCEOF:
1778 case ICMD_ANEWARRAY:
1780 case ICMD_IADDCONST:
1781 case ICMD_ISUBCONST:
1782 case ICMD_IMULCONST:
1786 case ICMD_IANDCONST:
1788 case ICMD_IXORCONST:
1789 case ICMD_ISHLCONST:
1790 case ICMD_ISHRCONST:
1791 case ICMD_IUSHRCONST:
1792 case ICMD_LADDCONST:
1793 case ICMD_LSUBCONST:
1794 case ICMD_LMULCONST:
1798 case ICMD_LANDCONST:
1800 case ICMD_LXORCONST:
1801 case ICMD_LSHLCONST:
1802 case ICMD_LSHRCONST:
1803 case ICMD_LUSHRCONST:
1807 case ICMD_INT2SHORT:
1823 RELOCATE(iptr->s1.varindex);
1824 RELOCATE(iptr->dst.varindex);
1829 case ICMD_GETSTATIC:
1831 RELOCATE(iptr->dst.varindex);
1834 /* pop many push any */
1836 case ICMD_INVOKESTATIC:
1837 case ICMD_INVOKESPECIAL:
1838 case ICMD_INVOKEVIRTUAL:
1839 case ICMD_INVOKEINTERFACE:
1841 case ICMD_MULTIANEWARRAY:
1842 i = iptr->s1.argcount;
1843 if (cloneinstructions) {
1844 argp = DMNEW(s4, i);
1845 MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1846 iptr->sx.s23.s2.args = argp;
1849 argp = iptr->sx.s23.s2.args;
1856 RELOCATE(iptr->dst.varindex);
1860 exceptions_throw_internalerror("Unknown ICMD %d during stack re-analysis",
1865 #if defined(STACK_VERBOSE)
1866 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1871 /* relocate outvars */
1873 for (i=0; i<b->outdepth; ++i) {
1874 RELOCATE(b->outvars[i]);
1877 #if defined(STACK_VERBOSE)
1878 stack_verbose_block_exit(sd, superblockend);
1881 /* propagate to the next block */
1884 if (!stack_reach_next_block(sd))
1891 /* stack_change_to_tempvar *****************************************************
1893 Change the given stackslot to a TEMPVAR. This includes creating a new
1894 temporary variable and changing the dst.varindex of the creator of the
1895 stacklot to the new variable index. If this stackslot has been passed
1896 through ICMDs between the point of its creation and the current point,
1897 then the variable index is also changed in these ICMDs.
1900 sd...........stack analysis data
1901 sp...........stackslot to change
1902 ilimit.......instruction up to which to look for ICMDs passing-through
1903 the stackslot (exclusive). This may point exactly after the
1904 last instruction, in which case the search is done to the
1907 *******************************************************************************/
1909 static void stack_change_to_tempvar(stackdata_t *sd, stackptr sp,
1910 instruction *ilimit)
1918 oldindex = sp->varnum;
1920 /* create a new temporary variable */
1922 GET_NEW_VAR(*sd, newindex, sp->type);
1924 sd->var[newindex].flags = sp->flags;
1926 /* change the stackslot */
1928 sp->varnum = newindex;
1929 sp->varkind = TEMPVAR;
1931 /* change the dst.varindex of the stackslot's creator */
1934 sp->creator->dst.varindex = newindex;
1936 /* handle ICMDs this stackslot passed through, if any */
1938 if (sp->flags & PASSTHROUGH) {
1939 iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
1941 /* assert that the limit points to an ICMD, or after the last one */
1943 assert(ilimit >= sd->bptr->iinstr);
1944 assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
1946 /* find the stackdepth under sp plus one */
1947 /* Note: This number is usually known when this function is called, */
1948 /* but calculating it here is less error-prone and should not be */
1949 /* a performance problem. */
1951 for (depth = 0; sp != NULL; sp = sp->prev)
1954 /* iterate over all instructions in the range and replace */
1956 for (; iptr < ilimit; ++iptr) {
1957 switch (iptr->opc) {
1958 case ICMD_INVOKESTATIC:
1959 case ICMD_INVOKESPECIAL:
1960 case ICMD_INVOKEVIRTUAL:
1961 case ICMD_INVOKEINTERFACE:
1963 i = iptr->s1.argcount - depth;
1964 if (iptr->sx.s23.s2.args[i] == oldindex) {
1965 iptr->sx.s23.s2.args[i] = newindex;
1968 /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
1969 /* stackslot, it must be added in this switch! */
1976 /* stack_init_javalocals *******************************************************
1978 Initialize the mapping from Java locals to cacao variables at method entry.
1981 sd...........stack analysis data
1983 *******************************************************************************/
1985 static void stack_init_javalocals(stackdata_t *sd)
1994 jl = DMNEW(s4, sd->maxlocals);
1995 jd->basicblocks[0].javalocals = jl;
1997 for (i=0; i<sd->maxlocals; ++i)
2000 md = jd->m->parseddesc;
2002 for (i=0; i<md->paramcount; ++i) {
2003 t = md->paramtypes[i].type;
2004 jl[j] = jd->local_map[5*j + t];
2006 if (IS_2_WORD_TYPE(t))
2012 /* stack_analyse ***************************************************************
2014 Analyse_stack uses the intermediate code created by parse.c to
2015 build a model of the JVM operand stack for the current method.
2017 The following checks are performed:
2018 - check for operand stack underflow (before each instruction)
2019 - check for operand stack overflow (after[1] each instruction)
2020 - check for matching stack depth at merging points
2021 - check for matching basic types[2] at merging points
2022 - check basic types for instruction input (except for BUILTIN*
2023 opcodes, INVOKE* opcodes and MULTIANEWARRAY)
2025 [1]) Checking this after the instruction should be ok. parse.c
2026 counts the number of required stack slots in such a way that it is
2027 only vital that we don't exceed `maxstack` at basic block
2030 [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
2031 DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
2032 types are not discerned.
2034 *******************************************************************************/
2036 bool stack_analyse(jitdata *jd)
2038 methodinfo *m; /* method being analyzed */
2041 #if defined(ENABLE_SSA)
2045 stackptr curstack; /* current stack top */
2047 int opcode; /* opcode of current instruction */
2050 int len; /* # of instructions after the current one */
2051 bool superblockend; /* if true, no fallthrough to next block */
2052 bool deadcode; /* true if no live code has been reached */
2053 instruction *iptr; /* the current instruction */
2055 basicblock *original;
2056 exception_entry *ex;
2058 stackptr *last_store_boundary;
2059 stackptr coalescing_boundary;
2061 stackptr src1, src2, src3, src4, dst1, dst2;
2063 branch_target_t *table;
2064 lookup_target_t *lookup;
2065 #if defined(ENABLE_VERIFIER)
2066 int expectedtype; /* used by CHECK_BASIC_TYPE */
2068 builtintable_entry *bte;
2070 constant_FMIref *fmiref;
2071 #if defined(ENABLE_STATISTICS)
2072 int iteration_count; /* number of iterations of analysis */
2074 int new_index; /* used to get a new var index with GET_NEW_INDEX*/
2076 #if defined(STACK_VERBOSE)
2077 show_method(jd, SHOW_PARSE);
2080 /* get required compiler data - initialization */
2084 #if defined(ENABLE_SSA)
2088 /* initialize the stackdata_t struct */
2092 sd.varcount = jd->varcount;
2093 sd.vartop = jd->vartop;
2094 sd.localcount = jd->localcount;
2096 sd.varsallocated = sd.varcount;
2097 sd.maxlocals = m->maxlocals;
2098 sd.javalocals = DMNEW(s4, sd.maxlocals);
2099 sd.handlers = DMNEW(exception_entry *, jd->exceptiontablelength + 1);
2101 /* prepare the variable for exception handler stacks */
2102 /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2104 sd.exstack.type = TYPE_ADR;
2105 sd.exstack.prev = NULL;
2106 sd.exstack.varnum = sd.localcount;
2107 sd.var[sd.exstack.varnum].type = TYPE_ADR;
2109 #if defined(ENABLE_LSRA)
2110 m->maxlifetimes = 0;
2113 #if defined(ENABLE_STATISTICS)
2114 iteration_count = 0;
2117 /* find the last real basic block */
2119 sd.last_real_block = NULL;
2120 tbptr = jd->basicblocks;
2121 while (tbptr->next) {
2122 sd.last_real_block = tbptr;
2123 tbptr = tbptr->next;
2125 assert(sd.last_real_block);
2127 /* find the last exception handler */
2129 if (jd->exceptiontablelength)
2130 sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2132 sd.extableend = NULL;
2134 /* init jd->interface_map */
2136 jd->maxinterfaces = m->maxstack;
2137 jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2138 for (i = 0; i < m->maxstack * 5; i++)
2139 jd->interface_map[i].flags = UNUSED;
2141 last_store_boundary = DMNEW(stackptr, m->maxlocals);
2143 /* initialize flags and invars (none) of first block */
2145 jd->basicblocks[0].flags = BBREACHED;
2146 jd->basicblocks[0].invars = NULL;
2147 jd->basicblocks[0].indepth = 0;
2148 jd->basicblocks[0].inlocals =
2149 DMNEW(varinfo, jd->localcount + VERIFIER_EXTRA_LOCALS);
2150 MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo,
2151 jd->localcount + VERIFIER_EXTRA_LOCALS);
2153 /* initialize java local mapping of first block */
2155 stack_init_javalocals(&sd);
2157 /* stack analysis loop (until fixpoint reached) **************************/
2160 #if defined(ENABLE_STATISTICS)
2164 /* initialize loop over basic blocks */
2166 sd.bptr = jd->basicblocks;
2167 superblockend = true;
2173 /* iterate over basic blocks *****************************************/
2175 for (; sd.bptr; sd.bptr = sd.bptr->next) {
2177 if (sd.bptr->flags == BBDELETED) {
2178 /* This block has been deleted - do nothing. */
2183 if (sd.bptr->flags == BBTYPECHECK_REACHED) {
2184 /* re-analyse a block because its input changed */
2188 if (!stack_reanalyse_block(&sd))
2191 superblockend = true; /* XXX */
2195 if (superblockend && (sd.bptr->flags < BBREACHED)) {
2196 /* This block has not been reached so far, and we
2197 don't fall into it, so we'll have to iterate
2204 if (sd.bptr->flags > BBREACHED) {
2205 /* This block is already finished. */
2207 superblockend = true;
2211 if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2212 /* This block is a clone and the original has not been
2213 analysed, yet. Analyse it on the next
2217 /* XXX superblockend? */
2221 /* This block has to be analysed now. */
2225 /* XXX The rest of this block is still indented one level too */
2226 /* much in order to avoid a giant diff by changing that. */
2228 /* We know that sd.bptr->flags == BBREACHED. */
2229 /* This block has been reached before. */
2231 assert(sd.bptr->flags == BBREACHED);
2232 stackdepth = sd.bptr->indepth;
2234 /* find exception handlers for this block */
2236 /* determine the active exception handlers for this block */
2237 /* XXX could use a faster algorithm with sorted lists or */
2240 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2243 ex = jd->exceptiontable;
2244 for (; ex != NULL; ex = ex->down) {
2245 if ((ex->start <= original) && (ex->end > original)) {
2246 sd.handlers[len++] = ex;
2249 sd.handlers[len] = NULL;
2252 /* reanalyse cloned block */
2254 if (sd.bptr->original) {
2255 if (!stack_reanalyse_block(&sd))
2260 /* reset the new pointer for allocating stackslots */
2264 /* create the instack of this block */
2266 curstack = stack_create_instack(&sd);
2268 /* initialize locals at the start of this block */
2270 if (sd.bptr->inlocals)
2271 MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2273 MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2275 /* set up local variables for analyzing this block */
2277 superblockend = false;
2278 len = sd.bptr->icount;
2279 iptr = sd.bptr->iinstr;
2281 /* mark the block as analysed */
2283 sd.bptr->flags = BBFINISHED;
2285 /* reset variables for dependency checking */
2287 coalescing_boundary = sd.new;
2288 for( i = 0; i < m->maxlocals; i++)
2289 last_store_boundary[i] = sd.new;
2291 /* remember the start of this block's variables */
2293 sd.bptr->varstart = sd.vartop;
2295 #if defined(STACK_VERBOSE)
2296 stack_verbose_block_enter(&sd, false);
2299 /* reach exception handlers for this block */
2301 if (!stack_reach_handlers(&sd))
2304 /* iterate over ICMDs ****************************************/
2306 while (--len >= 0) {
2308 #if defined(STACK_VERBOSE)
2309 stack_verbose_show_state(&sd, iptr, curstack);
2312 /* fetch the current opcode */
2316 /* automatically replace some ICMDs with builtins */
2318 bte = builtintable_get_automatic(opcode);
2320 if ((bte != NULL) && (bte->opcode == opcode)) {
2321 iptr->opc = ICMD_BUILTIN;
2322 iptr->flags.bits &= INS_FLAG_ID_MASK;
2323 iptr->sx.s23.s3.bte = bte;
2324 /* iptr->line is already set */
2325 jd->isleafmethod = false;
2329 /* main opcode switch *************************************/
2341 case ICMD_CHECKNULL:
2342 coalescing_boundary = sd.new;
2343 COUNT(count_check_null);
2346 iptr->dst.varindex = iptr->s1.varindex;
2350 j = iptr->s1.varindex =
2351 jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2353 #if defined(ENABLE_VERIFIER)
2354 if (sd.var[j].type != TYPE_RET) {
2355 exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2362 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2363 superblockend = true;
2367 COUNT(count_pcmd_return);
2370 superblockend = true;
2371 sd.jd->returncount++;
2372 sd.jd->returnblock = sd.bptr;
2376 /* pop 0 push 1 const */
2378 /************************** ICONST OPTIMIZATIONS **************************/
2381 COUNT(count_pcmd_load);
2385 switch (iptr[1].opc) {
2387 iptr->opc = ICMD_IADDCONST;
2391 iptr[1].opc = ICMD_NOP;
2392 OP1_1(TYPE_INT, TYPE_INT);
2393 COUNT(count_pcmd_op);
2397 iptr->opc = ICMD_ISUBCONST;
2398 goto icmd_iconst_tail;
2399 #if SUPPORT_CONST_MUL
2401 iptr->opc = ICMD_IMULCONST;
2402 goto icmd_iconst_tail;
2403 #else /* SUPPORT_CONST_MUL */
2405 if (iptr->sx.val.i == 0x00000002)
2407 else if (iptr->sx.val.i == 0x00000004)
2409 else if (iptr->sx.val.i == 0x00000008)
2411 else if (iptr->sx.val.i == 0x00000010)
2413 else if (iptr->sx.val.i == 0x00000020)
2415 else if (iptr->sx.val.i == 0x00000040)
2417 else if (iptr->sx.val.i == 0x00000080)
2419 else if (iptr->sx.val.i == 0x00000100)
2421 else if (iptr->sx.val.i == 0x00000200)
2423 else if (iptr->sx.val.i == 0x00000400)
2424 iptr->sx.val.i = 10;
2425 else if (iptr->sx.val.i == 0x00000800)
2426 iptr->sx.val.i = 11;
2427 else if (iptr->sx.val.i == 0x00001000)
2428 iptr->sx.val.i = 12;
2429 else if (iptr->sx.val.i == 0x00002000)
2430 iptr->sx.val.i = 13;
2431 else if (iptr->sx.val.i == 0x00004000)
2432 iptr->sx.val.i = 14;
2433 else if (iptr->sx.val.i == 0x00008000)
2434 iptr->sx.val.i = 15;
2435 else if (iptr->sx.val.i == 0x00010000)
2436 iptr->sx.val.i = 16;
2437 else if (iptr->sx.val.i == 0x00020000)
2438 iptr->sx.val.i = 17;
2439 else if (iptr->sx.val.i == 0x00040000)
2440 iptr->sx.val.i = 18;
2441 else if (iptr->sx.val.i == 0x00080000)
2442 iptr->sx.val.i = 19;
2443 else if (iptr->sx.val.i == 0x00100000)
2444 iptr->sx.val.i = 20;
2445 else if (iptr->sx.val.i == 0x00200000)
2446 iptr->sx.val.i = 21;
2447 else if (iptr->sx.val.i == 0x00400000)
2448 iptr->sx.val.i = 22;
2449 else if (iptr->sx.val.i == 0x00800000)
2450 iptr->sx.val.i = 23;
2451 else if (iptr->sx.val.i == 0x01000000)
2452 iptr->sx.val.i = 24;
2453 else if (iptr->sx.val.i == 0x02000000)
2454 iptr->sx.val.i = 25;
2455 else if (iptr->sx.val.i == 0x04000000)
2456 iptr->sx.val.i = 26;
2457 else if (iptr->sx.val.i == 0x08000000)
2458 iptr->sx.val.i = 27;
2459 else if (iptr->sx.val.i == 0x10000000)
2460 iptr->sx.val.i = 28;
2461 else if (iptr->sx.val.i == 0x20000000)
2462 iptr->sx.val.i = 29;
2463 else if (iptr->sx.val.i == 0x40000000)
2464 iptr->sx.val.i = 30;
2465 else if (iptr->sx.val.i == 0x80000000)
2466 iptr->sx.val.i = 31;
2470 iptr->opc = ICMD_IMULPOW2;
2471 goto icmd_iconst_tail;
2472 #endif /* SUPPORT_CONST_MUL */
2474 if (iptr->sx.val.i == 0x00000002)
2476 else if (iptr->sx.val.i == 0x00000004)
2478 else if (iptr->sx.val.i == 0x00000008)
2480 else if (iptr->sx.val.i == 0x00000010)
2482 else if (iptr->sx.val.i == 0x00000020)
2484 else if (iptr->sx.val.i == 0x00000040)
2486 else if (iptr->sx.val.i == 0x00000080)
2488 else if (iptr->sx.val.i == 0x00000100)
2490 else if (iptr->sx.val.i == 0x00000200)
2492 else if (iptr->sx.val.i == 0x00000400)
2493 iptr->sx.val.i = 10;
2494 else if (iptr->sx.val.i == 0x00000800)
2495 iptr->sx.val.i = 11;
2496 else if (iptr->sx.val.i == 0x00001000)
2497 iptr->sx.val.i = 12;
2498 else if (iptr->sx.val.i == 0x00002000)
2499 iptr->sx.val.i = 13;
2500 else if (iptr->sx.val.i == 0x00004000)
2501 iptr->sx.val.i = 14;
2502 else if (iptr->sx.val.i == 0x00008000)
2503 iptr->sx.val.i = 15;
2504 else if (iptr->sx.val.i == 0x00010000)
2505 iptr->sx.val.i = 16;
2506 else if (iptr->sx.val.i == 0x00020000)
2507 iptr->sx.val.i = 17;
2508 else if (iptr->sx.val.i == 0x00040000)
2509 iptr->sx.val.i = 18;
2510 else if (iptr->sx.val.i == 0x00080000)
2511 iptr->sx.val.i = 19;
2512 else if (iptr->sx.val.i == 0x00100000)
2513 iptr->sx.val.i = 20;
2514 else if (iptr->sx.val.i == 0x00200000)
2515 iptr->sx.val.i = 21;
2516 else if (iptr->sx.val.i == 0x00400000)
2517 iptr->sx.val.i = 22;
2518 else if (iptr->sx.val.i == 0x00800000)
2519 iptr->sx.val.i = 23;
2520 else if (iptr->sx.val.i == 0x01000000)
2521 iptr->sx.val.i = 24;
2522 else if (iptr->sx.val.i == 0x02000000)
2523 iptr->sx.val.i = 25;
2524 else if (iptr->sx.val.i == 0x04000000)
2525 iptr->sx.val.i = 26;
2526 else if (iptr->sx.val.i == 0x08000000)
2527 iptr->sx.val.i = 27;
2528 else if (iptr->sx.val.i == 0x10000000)
2529 iptr->sx.val.i = 28;
2530 else if (iptr->sx.val.i == 0x20000000)
2531 iptr->sx.val.i = 29;
2532 else if (iptr->sx.val.i == 0x40000000)
2533 iptr->sx.val.i = 30;
2534 else if (iptr->sx.val.i == 0x80000000)
2535 iptr->sx.val.i = 31;
2539 iptr->opc = ICMD_IDIVPOW2;
2540 goto icmd_iconst_tail;
2543 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2544 if ((iptr->sx.val.i == 0x00000002) ||
2545 (iptr->sx.val.i == 0x00000004) ||
2546 (iptr->sx.val.i == 0x00000008) ||
2547 (iptr->sx.val.i == 0x00000010) ||
2548 (iptr->sx.val.i == 0x00000020) ||
2549 (iptr->sx.val.i == 0x00000040) ||
2550 (iptr->sx.val.i == 0x00000080) ||
2551 (iptr->sx.val.i == 0x00000100) ||
2552 (iptr->sx.val.i == 0x00000200) ||
2553 (iptr->sx.val.i == 0x00000400) ||
2554 (iptr->sx.val.i == 0x00000800) ||
2555 (iptr->sx.val.i == 0x00001000) ||
2556 (iptr->sx.val.i == 0x00002000) ||
2557 (iptr->sx.val.i == 0x00004000) ||
2558 (iptr->sx.val.i == 0x00008000) ||
2559 (iptr->sx.val.i == 0x00010000) ||
2560 (iptr->sx.val.i == 0x00020000) ||
2561 (iptr->sx.val.i == 0x00040000) ||
2562 (iptr->sx.val.i == 0x00080000) ||
2563 (iptr->sx.val.i == 0x00100000) ||
2564 (iptr->sx.val.i == 0x00200000) ||
2565 (iptr->sx.val.i == 0x00400000) ||
2566 (iptr->sx.val.i == 0x00800000) ||
2567 (iptr->sx.val.i == 0x01000000) ||
2568 (iptr->sx.val.i == 0x02000000) ||
2569 (iptr->sx.val.i == 0x04000000) ||
2570 (iptr->sx.val.i == 0x08000000) ||
2571 (iptr->sx.val.i == 0x10000000) ||
2572 (iptr->sx.val.i == 0x20000000) ||
2573 (iptr->sx.val.i == 0x40000000) ||
2574 (iptr->sx.val.i == 0x80000000))
2576 iptr->opc = ICMD_IREMPOW2;
2577 iptr->sx.val.i -= 1;
2578 goto icmd_iconst_tail;
2581 #if SUPPORT_CONST_LOGICAL
2583 iptr->opc = ICMD_IANDCONST;
2584 goto icmd_iconst_tail;
2587 iptr->opc = ICMD_IORCONST;
2588 goto icmd_iconst_tail;
2591 iptr->opc = ICMD_IXORCONST;
2592 goto icmd_iconst_tail;
2594 #endif /* SUPPORT_CONST_LOGICAL */
2596 iptr->opc = ICMD_ISHLCONST;
2597 goto icmd_iconst_tail;
2600 iptr->opc = ICMD_ISHRCONST;
2601 goto icmd_iconst_tail;
2604 iptr->opc = ICMD_IUSHRCONST;
2605 goto icmd_iconst_tail;
2606 #if SUPPORT_LONG_SHIFT
2608 iptr->opc = ICMD_LSHLCONST;
2609 goto icmd_lconst_tail;
2612 iptr->opc = ICMD_LSHRCONST;
2613 goto icmd_lconst_tail;
2616 iptr->opc = ICMD_LUSHRCONST;
2617 goto icmd_lconst_tail;
2618 #endif /* SUPPORT_LONG_SHIFT */
2619 case ICMD_IF_ICMPEQ:
2620 iptr[1].opc = ICMD_IFEQ;
2624 /* set the constant for the following icmd */
2625 iptr[1].sx.val.i = iptr->sx.val.i;
2627 /* this instruction becomes a nop */
2628 iptr->opc = ICMD_NOP;
2631 case ICMD_IF_ICMPLT:
2632 iptr[1].opc = ICMD_IFLT;
2633 goto icmd_if_icmp_tail;
2635 case ICMD_IF_ICMPLE:
2636 iptr[1].opc = ICMD_IFLE;
2637 goto icmd_if_icmp_tail;
2639 case ICMD_IF_ICMPNE:
2640 iptr[1].opc = ICMD_IFNE;
2641 goto icmd_if_icmp_tail;
2643 case ICMD_IF_ICMPGT:
2644 iptr[1].opc = ICMD_IFGT;
2645 goto icmd_if_icmp_tail;
2647 case ICMD_IF_ICMPGE:
2648 iptr[1].opc = ICMD_IFGE;
2649 goto icmd_if_icmp_tail;
2651 #if SUPPORT_CONST_STORE
2656 # if SUPPORT_CONST_STORE_ZERO_ONLY
2657 if (iptr->sx.val.i != 0)
2660 switch (iptr[1].opc) {
2662 iptr->opc = ICMD_IASTORECONST;
2663 iptr->flags.bits |= INS_FLAG_CHECK;
2666 iptr->opc = ICMD_BASTORECONST;
2667 iptr->flags.bits |= INS_FLAG_CHECK;
2670 iptr->opc = ICMD_CASTORECONST;
2671 iptr->flags.bits |= INS_FLAG_CHECK;
2674 iptr->opc = ICMD_SASTORECONST;
2675 iptr->flags.bits |= INS_FLAG_CHECK;
2679 iptr[1].opc = ICMD_NOP;
2681 /* copy the constant to s3 */
2682 /* XXX constval -> astoreconstval? */
2683 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2684 OP2_0(TYPE_ADR, TYPE_INT);
2685 COUNT(count_pcmd_op);
2688 case ICMD_PUTSTATIC:
2690 # if SUPPORT_CONST_STORE_ZERO_ONLY
2691 if (iptr->sx.val.i != 0)
2694 /* XXX check field type? */
2696 /* copy the constant to s2 */
2697 /* XXX constval -> fieldconstval? */
2698 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2701 /* set the field reference (s3) */
2702 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2703 iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2704 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2705 fmiref = iptr->sx.s23.s3.uf->fieldref;
2708 fmiref = iptr[1].sx.s23.s3.fmiref;
2709 iptr->sx.s23.s3.fmiref = fmiref;
2712 #if defined(ENABLE_VERIFIER)
2713 expectedtype = fmiref->parseddesc.fd->type;
2714 switch (iptr[0].opc) {
2716 if (expectedtype != TYPE_INT)
2717 goto throw_stack_type_error;
2720 if (expectedtype != TYPE_LNG)
2721 goto throw_stack_type_error;
2724 if (expectedtype != TYPE_ADR)
2725 goto throw_stack_type_error;
2730 #endif /* defined(ENABLE_VERIFIER) */
2732 switch (iptr[1].opc) {
2733 case ICMD_PUTSTATIC:
2734 iptr->opc = ICMD_PUTSTATICCONST;
2738 iptr->opc = ICMD_PUTFIELDCONST;
2743 iptr[1].opc = ICMD_NOP;
2744 COUNT(count_pcmd_op);
2746 #endif /* SUPPORT_CONST_STORE */
2752 /* if we get here, the ICONST has been optimized */
2756 /* normal case of an unoptimized ICONST */
2760 /************************** LCONST OPTIMIZATIONS **************************/
2763 COUNT(count_pcmd_load);
2767 /* switch depending on the following instruction */
2769 switch (iptr[1].opc) {
2770 #if SUPPORT_LONG_ADD
2772 iptr->opc = ICMD_LADDCONST;
2776 /* instruction of type LONG -> LONG */
2777 iptr[1].opc = ICMD_NOP;
2778 OP1_1(TYPE_LNG, TYPE_LNG);
2779 COUNT(count_pcmd_op);
2783 iptr->opc = ICMD_LSUBCONST;
2784 goto icmd_lconst_tail;
2786 #endif /* SUPPORT_LONG_ADD */
2787 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2789 iptr->opc = ICMD_LMULCONST;
2790 goto icmd_lconst_tail;
2791 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2792 # if SUPPORT_LONG_SHIFT
2794 if (iptr->sx.val.l == 0x00000002)
2796 else if (iptr->sx.val.l == 0x00000004)
2798 else if (iptr->sx.val.l == 0x00000008)
2800 else if (iptr->sx.val.l == 0x00000010)
2802 else if (iptr->sx.val.l == 0x00000020)
2804 else if (iptr->sx.val.l == 0x00000040)
2806 else if (iptr->sx.val.l == 0x00000080)
2808 else if (iptr->sx.val.l == 0x00000100)
2810 else if (iptr->sx.val.l == 0x00000200)
2812 else if (iptr->sx.val.l == 0x00000400)
2813 iptr->sx.val.i = 10;
2814 else if (iptr->sx.val.l == 0x00000800)
2815 iptr->sx.val.i = 11;
2816 else if (iptr->sx.val.l == 0x00001000)
2817 iptr->sx.val.i = 12;
2818 else if (iptr->sx.val.l == 0x00002000)
2819 iptr->sx.val.i = 13;
2820 else if (iptr->sx.val.l == 0x00004000)
2821 iptr->sx.val.i = 14;
2822 else if (iptr->sx.val.l == 0x00008000)
2823 iptr->sx.val.i = 15;
2824 else if (iptr->sx.val.l == 0x00010000)
2825 iptr->sx.val.i = 16;
2826 else if (iptr->sx.val.l == 0x00020000)
2827 iptr->sx.val.i = 17;
2828 else if (iptr->sx.val.l == 0x00040000)
2829 iptr->sx.val.i = 18;
2830 else if (iptr->sx.val.l == 0x00080000)
2831 iptr->sx.val.i = 19;
2832 else if (iptr->sx.val.l == 0x00100000)
2833 iptr->sx.val.i = 20;
2834 else if (iptr->sx.val.l == 0x00200000)
2835 iptr->sx.val.i = 21;
2836 else if (iptr->sx.val.l == 0x00400000)
2837 iptr->sx.val.i = 22;
2838 else if (iptr->sx.val.l == 0x00800000)
2839 iptr->sx.val.i = 23;
2840 else if (iptr->sx.val.l == 0x01000000)
2841 iptr->sx.val.i = 24;
2842 else if (iptr->sx.val.l == 0x02000000)
2843 iptr->sx.val.i = 25;
2844 else if (iptr->sx.val.l == 0x04000000)
2845 iptr->sx.val.i = 26;
2846 else if (iptr->sx.val.l == 0x08000000)
2847 iptr->sx.val.i = 27;
2848 else if (iptr->sx.val.l == 0x10000000)
2849 iptr->sx.val.i = 28;
2850 else if (iptr->sx.val.l == 0x20000000)
2851 iptr->sx.val.i = 29;
2852 else if (iptr->sx.val.l == 0x40000000)
2853 iptr->sx.val.i = 30;
2854 else if (iptr->sx.val.l == 0x80000000)
2855 iptr->sx.val.i = 31;
2859 iptr->opc = ICMD_LMULPOW2;
2860 goto icmd_lconst_tail;
2861 # endif /* SUPPORT_LONG_SHIFT */
2862 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2863 #if SUPPORT_LONG_DIV_POW2
2865 if (iptr->sx.val.l == 0x00000002)
2867 else if (iptr->sx.val.l == 0x00000004)
2869 else if (iptr->sx.val.l == 0x00000008)
2871 else if (iptr->sx.val.l == 0x00000010)
2873 else if (iptr->sx.val.l == 0x00000020)
2875 else if (iptr->sx.val.l == 0x00000040)
2877 else if (iptr->sx.val.l == 0x00000080)
2879 else if (iptr->sx.val.l == 0x00000100)
2881 else if (iptr->sx.val.l == 0x00000200)
2883 else if (iptr->sx.val.l == 0x00000400)
2884 iptr->sx.val.i = 10;
2885 else if (iptr->sx.val.l == 0x00000800)
2886 iptr->sx.val.i = 11;
2887 else if (iptr->sx.val.l == 0x00001000)
2888 iptr->sx.val.i = 12;
2889 else if (iptr->sx.val.l == 0x00002000)
2890 iptr->sx.val.i = 13;
2891 else if (iptr->sx.val.l == 0x00004000)
2892 iptr->sx.val.i = 14;
2893 else if (iptr->sx.val.l == 0x00008000)
2894 iptr->sx.val.i = 15;
2895 else if (iptr->sx.val.l == 0x00010000)
2896 iptr->sx.val.i = 16;
2897 else if (iptr->sx.val.l == 0x00020000)
2898 iptr->sx.val.i = 17;
2899 else if (iptr->sx.val.l == 0x00040000)
2900 iptr->sx.val.i = 18;
2901 else if (iptr->sx.val.l == 0x00080000)
2902 iptr->sx.val.i = 19;
2903 else if (iptr->sx.val.l == 0x00100000)
2904 iptr->sx.val.i = 20;
2905 else if (iptr->sx.val.l == 0x00200000)
2906 iptr->sx.val.i = 21;
2907 else if (iptr->sx.val.l == 0x00400000)
2908 iptr->sx.val.i = 22;
2909 else if (iptr->sx.val.l == 0x00800000)
2910 iptr->sx.val.i = 23;
2911 else if (iptr->sx.val.l == 0x01000000)
2912 iptr->sx.val.i = 24;
2913 else if (iptr->sx.val.l == 0x02000000)
2914 iptr->sx.val.i = 25;
2915 else if (iptr->sx.val.l == 0x04000000)
2916 iptr->sx.val.i = 26;
2917 else if (iptr->sx.val.l == 0x08000000)
2918 iptr->sx.val.i = 27;
2919 else if (iptr->sx.val.l == 0x10000000)
2920 iptr->sx.val.i = 28;
2921 else if (iptr->sx.val.l == 0x20000000)
2922 iptr->sx.val.i = 29;
2923 else if (iptr->sx.val.l == 0x40000000)
2924 iptr->sx.val.i = 30;
2925 else if (iptr->sx.val.l == 0x80000000)
2926 iptr->sx.val.i = 31;
2930 iptr->opc = ICMD_LDIVPOW2;
2931 goto icmd_lconst_tail;
2932 #endif /* SUPPORT_LONG_DIV_POW2 */
2934 #if SUPPORT_LONG_REM_POW2
2936 if ((iptr->sx.val.l == 0x00000002) ||
2937 (iptr->sx.val.l == 0x00000004) ||
2938 (iptr->sx.val.l == 0x00000008) ||
2939 (iptr->sx.val.l == 0x00000010) ||
2940 (iptr->sx.val.l == 0x00000020) ||
2941 (iptr->sx.val.l == 0x00000040) ||
2942 (iptr->sx.val.l == 0x00000080) ||
2943 (iptr->sx.val.l == 0x00000100) ||
2944 (iptr->sx.val.l == 0x00000200) ||
2945 (iptr->sx.val.l == 0x00000400) ||
2946 (iptr->sx.val.l == 0x00000800) ||
2947 (iptr->sx.val.l == 0x00001000) ||
2948 (iptr->sx.val.l == 0x00002000) ||
2949 (iptr->sx.val.l == 0x00004000) ||
2950 (iptr->sx.val.l == 0x00008000) ||
2951 (iptr->sx.val.l == 0x00010000) ||
2952 (iptr->sx.val.l == 0x00020000) ||
2953 (iptr->sx.val.l == 0x00040000) ||
2954 (iptr->sx.val.l == 0x00080000) ||
2955 (iptr->sx.val.l == 0x00100000) ||
2956 (iptr->sx.val.l == 0x00200000) ||
2957 (iptr->sx.val.l == 0x00400000) ||
2958 (iptr->sx.val.l == 0x00800000) ||
2959 (iptr->sx.val.l == 0x01000000) ||
2960 (iptr->sx.val.l == 0x02000000) ||
2961 (iptr->sx.val.l == 0x04000000) ||
2962 (iptr->sx.val.l == 0x08000000) ||
2963 (iptr->sx.val.l == 0x10000000) ||
2964 (iptr->sx.val.l == 0x20000000) ||
2965 (iptr->sx.val.l == 0x40000000) ||
2966 (iptr->sx.val.l == 0x80000000))
2968 iptr->opc = ICMD_LREMPOW2;
2969 iptr->sx.val.l -= 1;
2970 goto icmd_lconst_tail;
2973 #endif /* SUPPORT_LONG_REM_POW2 */
2975 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2978 iptr->opc = ICMD_LANDCONST;
2979 goto icmd_lconst_tail;
2982 iptr->opc = ICMD_LORCONST;
2983 goto icmd_lconst_tail;
2986 iptr->opc = ICMD_LXORCONST;
2987 goto icmd_lconst_tail;
2988 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2990 #if SUPPORT_LONG_CMP_CONST
2992 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2995 /* switch on the instruction after LCONST - LCMP */
2997 switch (iptr[2].opc) {
2999 iptr->opc = ICMD_IF_LEQ;
3002 icmd_lconst_lcmp_tail:
3003 /* convert LCONST, LCMP, IFXX to IF_LXX */
3004 iptr->dst.block = iptr[2].dst.block;
3005 iptr[1].opc = ICMD_NOP;
3006 iptr[2].opc = ICMD_NOP;
3008 OP1_BRANCH(TYPE_LNG);
3010 COUNT(count_pcmd_bra);
3011 COUNT(count_pcmd_op);
3015 iptr->opc = ICMD_IF_LNE;
3016 goto icmd_lconst_lcmp_tail;
3019 iptr->opc = ICMD_IF_LLT;
3020 goto icmd_lconst_lcmp_tail;
3023 iptr->opc = ICMD_IF_LGT;
3024 goto icmd_lconst_lcmp_tail;
3027 iptr->opc = ICMD_IF_LLE;
3028 goto icmd_lconst_lcmp_tail;
3031 iptr->opc = ICMD_IF_LGE;
3032 goto icmd_lconst_lcmp_tail;
3036 } /* end switch on opcode after LCONST - LCMP */
3038 #endif /* SUPPORT_LONG_CMP_CONST */
3040 #if SUPPORT_CONST_STORE
3042 # if SUPPORT_CONST_STORE_ZERO_ONLY
3043 if (iptr->sx.val.l != 0)
3046 #if SIZEOF_VOID_P == 4
3047 /* the constant must fit into a ptrint */
3048 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3051 /* move the constant to s3 */
3052 iptr->sx.s23.s3.constval = iptr->sx.val.l;
3054 iptr->opc = ICMD_LASTORECONST;
3055 iptr->flags.bits |= INS_FLAG_CHECK;
3056 OP2_0(TYPE_ADR, TYPE_INT);
3058 iptr[1].opc = ICMD_NOP;
3059 COUNT(count_pcmd_op);
3062 case ICMD_PUTSTATIC:
3064 # if SUPPORT_CONST_STORE_ZERO_ONLY
3065 if (iptr->sx.val.l != 0)
3068 #if SIZEOF_VOID_P == 4
3069 /* the constant must fit into a ptrint */
3070 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3073 /* XXX check field type? */
3075 /* copy the constant to s2 */
3076 /* XXX constval -> fieldconstval? */
3077 iptr->sx.s23.s2.constval = iptr->sx.val.l;
3081 #endif /* SUPPORT_CONST_STORE */
3085 } /* end switch opcode after LCONST */
3087 /* if we get here, the LCONST has been optimized */
3091 /* the normal case of an unoptimized LCONST */
3095 /************************ END OF LCONST OPTIMIZATIONS *********************/
3098 COUNT(count_pcmd_load);
3103 COUNT(count_pcmd_load);
3107 /************************** ACONST OPTIMIZATIONS **************************/
3110 coalescing_boundary = sd.new;
3111 COUNT(count_pcmd_load);
3112 #if SUPPORT_CONST_STORE
3113 /* We can only optimize if the ACONST is resolved
3114 * and there is an instruction after it. */
3116 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3119 switch (iptr[1].opc) {
3121 /* We can only optimize for NULL values
3122 * here because otherwise a checkcast is
3124 if (iptr->sx.val.anyptr != NULL)
3127 /* copy the constant (NULL) to s3 */
3128 iptr->sx.s23.s3.constval = 0;
3129 iptr->opc = ICMD_AASTORECONST;
3130 iptr->flags.bits |= INS_FLAG_CHECK;
3131 OP2_0(TYPE_ADR, TYPE_INT);
3133 iptr[1].opc = ICMD_NOP;
3134 COUNT(count_pcmd_op);
3137 case ICMD_PUTSTATIC:
3139 # if SUPPORT_CONST_STORE_ZERO_ONLY
3140 if (iptr->sx.val.anyptr != NULL)
3143 /* XXX check field type? */
3144 /* copy the constant to s2 */
3145 /* XXX constval -> fieldconstval? */
3146 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3154 /* if we get here the ACONST has been optimized */
3158 #endif /* SUPPORT_CONST_STORE */
3163 /* pop 0 push 1 load */
3170 COUNT(count_load_instruction);
3171 i = opcode - ICMD_ILOAD; /* type */
3173 j = iptr->s1.varindex =
3174 jd->local_map[iptr->s1.varindex * 5 + i];
3176 #if defined(ENABLE_VERIFIER)
3177 if (sd.var[j].type == TYPE_RET) {
3178 exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3183 #if defined(ENABLE_SSA)
3185 GET_NEW_VAR(sd, new_index, i);
3202 coalescing_boundary = sd.new;
3203 iptr->flags.bits |= INS_FLAG_CHECK;
3204 COUNT(count_check_null);
3205 COUNT(count_check_bound);
3206 COUNT(count_pcmd_mem);
3207 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3214 coalescing_boundary = sd.new;
3215 iptr->flags.bits |= INS_FLAG_CHECK;
3216 COUNT(count_check_null);
3217 COUNT(count_check_bound);
3218 COUNT(count_pcmd_mem);
3219 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3222 /* pop 0 push 0 iinc */
3225 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3226 #if defined(ENABLE_SSA)
3229 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3233 last_store_boundary[iptr->s1.varindex] = sd.new;
3236 jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3241 if ((copy->varkind == LOCALVAR) &&
3242 (copy->varnum == iptr->s1.varindex))
3244 assert(IS_LOCALVAR(copy));
3250 #if defined(ENABLE_SSA)
3254 iptr->dst.varindex = iptr->s1.varindex;
3257 /* pop 1 push 0 store */
3266 i = opcode - ICMD_ISTORE; /* type */
3267 javaindex = iptr->dst.varindex;
3268 j = iptr->dst.varindex =
3269 jd->local_map[javaindex * 5 + i];
3271 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3273 iptr->sx.s23.s3.javaindex = javaindex;
3275 if (curstack->type == TYPE_RET) {
3276 iptr->flags.bits |= INS_FLAG_RETADDR;
3277 iptr->sx.s23.s2.retaddrnr =
3278 UNUSED - (1 + sd.var[j].vv.retaddr->nr);
3279 sd.javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
3282 sd.javalocals[javaindex] = j;
3284 /* invalidate the following javalocal for 2-word types */
3286 if (IS_2_WORD_TYPE(i)) {
3287 sd.javalocals[javaindex+1] = UNUSED;
3288 iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3291 /* invalidate 2-word types if second half was overwritten */
3293 if (javaindex > 0 && (i = sd.javalocals[javaindex-1]) >= 0) {
3294 if (IS_2_WORD_TYPE(sd.var[i].type)) {
3295 sd.javalocals[javaindex-1] = UNUSED;
3296 iptr->flags.bits |= INS_FLAG_KILL_PREV;
3300 #if defined(ENABLE_STATISTICS)
3303 i = sd.new - curstack;
3305 count_store_length[20]++;
3307 count_store_length[i]++;
3310 count_store_depth[10]++;
3312 count_store_depth[i]++;
3316 #if defined(ENABLE_SSA)
3319 /* check for conflicts as described in Figure 5.2 */
3321 copy = curstack->prev;
3324 if ((copy->varkind == LOCALVAR) &&
3325 (copy->varnum == j))
3327 copy->varkind = TEMPVAR;
3328 assert(IS_LOCALVAR(copy));
3335 /* if the variable is already coalesced, don't bother */
3337 /* We do not need to check against INOUT, as invars */
3338 /* are always before the coalescing boundary. */
3340 if (curstack->varkind == LOCALVAR)
3343 /* there is no STORE Lj while curstack is live */
3345 if (curstack < last_store_boundary[javaindex])
3346 goto assume_conflict;
3348 /* curstack must be after the coalescing boundary */
3350 if (curstack < coalescing_boundary)
3351 goto assume_conflict;
3353 /* there is no DEF LOCALVAR(j) while curstack is live */
3355 copy = sd.new; /* most recent stackslot created + 1 */
3356 while (--copy > curstack) {
3357 if (copy->varkind == LOCALVAR && copy->varnum == j)
3358 goto assume_conflict;
3361 /* coalesce the temporary variable with Lj */
3362 assert((curstack->varkind == TEMPVAR)
3363 || (curstack->varkind == UNDEFVAR));
3364 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3365 assert(!IS_INOUT(curstack));
3366 assert(!IS_PREALLOC(curstack));
3368 assert(curstack->creator);
3369 assert(curstack->creator->dst.varindex == curstack->varnum);
3370 assert(!(curstack->flags & PASSTHROUGH));
3371 RELEASE_INDEX(sd, curstack);
3372 curstack->varkind = LOCALVAR;
3373 curstack->varnum = j;
3374 curstack->creator->dst.varindex = j;
3377 /* revert the coalescing, if it has been done earlier */
3379 if ((curstack->varkind == LOCALVAR)
3380 && (curstack->varnum == j))
3382 assert(IS_LOCALVAR(curstack));
3383 SET_TEMPVAR(curstack);
3386 /* remember the stack boundary at this store */
3388 last_store_boundary[javaindex] = sd.new;
3389 #if defined(ENABLE_SSA)
3390 } /* if (ls != NULL) */
3393 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3396 STORE(opcode - ICMD_ISTORE, j);
3402 coalescing_boundary = sd.new;
3403 iptr->flags.bits |= INS_FLAG_CHECK;
3404 COUNT(count_check_null);
3405 COUNT(count_check_bound);
3406 COUNT(count_pcmd_mem);
3408 bte = builtintable_get_internal(BUILTIN_canstore);
3411 if (md->memuse > rd->memuse)
3412 rd->memuse = md->memuse;
3413 if (md->argintreguse > rd->argintreguse)
3414 rd->argintreguse = md->argintreguse;
3415 /* XXX non-leaf method? */
3417 /* make all stack variables saved */
3421 sd.var[copy->varnum].flags |= SAVEDVAR;
3422 /* in case copy->varnum is/will be a LOCALVAR */
3423 /* once and set back to a non LOCALVAR */
3424 /* the correct SAVEDVAR flag has to be */
3425 /* remembered in copy->flags, too */
3426 copy->flags |= SAVEDVAR;
3430 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3437 coalescing_boundary = sd.new;
3438 iptr->flags.bits |= INS_FLAG_CHECK;
3439 COUNT(count_check_null);
3440 COUNT(count_check_bound);
3441 COUNT(count_pcmd_mem);
3442 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3449 coalescing_boundary = sd.new;
3450 iptr->flags.bits |= INS_FLAG_CHECK;
3451 COUNT(count_check_null);
3452 COUNT(count_check_bound);
3453 COUNT(count_pcmd_mem);
3454 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3460 #ifdef ENABLE_VERIFIER
3463 if (IS_2_WORD_TYPE(curstack->type))
3464 goto throw_stack_category_error;
3475 coalescing_boundary = sd.new;
3476 /* Assert here that no LOCAL or INOUTS get */
3477 /* preallocated, since tha macros are not */
3478 /* available in md-abi.c! */
3479 if (IS_TEMPVAR(curstack))
3480 md_return_alloc(jd, curstack);
3481 COUNT(count_pcmd_return);
3482 OP1_0(opcode - ICMD_IRETURN);
3483 superblockend = true;
3484 sd.jd->returncount++;
3485 sd.jd->returnblock = sd.bptr;
3489 coalescing_boundary = sd.new;
3490 COUNT(count_check_null);
3492 curstack = NULL; stackdepth = 0;
3493 superblockend = true;
3496 case ICMD_PUTSTATIC:
3497 coalescing_boundary = sd.new;
3498 COUNT(count_pcmd_mem);
3499 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3500 OP1_0(fmiref->parseddesc.fd->type);
3503 /* pop 1 push 0 branch */
3506 case ICMD_IFNONNULL:
3507 COUNT(count_pcmd_bra);
3508 OP1_BRANCH(TYPE_ADR);
3518 COUNT(count_pcmd_bra);
3519 /* iptr->sx.val.i is set implicitly in parse by
3520 clearing the memory or from IF_ICMPxx
3523 OP1_BRANCH(TYPE_INT);
3524 /* iptr->sx.val.i = 0; */
3528 /* pop 0 push 0 branch */
3531 COUNT(count_pcmd_bra);
3534 superblockend = true;
3537 /* pop 1 push 0 table branch */
3539 case ICMD_TABLESWITCH:
3540 COUNT(count_pcmd_table);
3541 OP1_BRANCH(TYPE_INT);
3543 table = iptr->dst.table;
3544 BRANCH_TARGET(*table, tbptr);
3547 i = iptr->sx.s23.s3.tablehigh
3548 - iptr->sx.s23.s2.tablelow + 1;
3551 BRANCH_TARGET(*table, tbptr);
3554 superblockend = true;
3557 /* pop 1 push 0 table branch */
3559 case ICMD_LOOKUPSWITCH:
3560 COUNT(count_pcmd_table);
3561 OP1_BRANCH(TYPE_INT);
3563 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3565 lookup = iptr->dst.lookup;
3567 i = iptr->sx.s23.s2.lookupcount;
3570 BRANCH_TARGET(lookup->target, tbptr);
3573 superblockend = true;
3576 case ICMD_MONITORENTER:
3577 case ICMD_MONITOREXIT:
3578 coalescing_boundary = sd.new;
3579 COUNT(count_check_null);
3583 /* pop 2 push 0 branch */
3585 case ICMD_IF_ICMPEQ:
3586 case ICMD_IF_ICMPNE:
3587 case ICMD_IF_ICMPLT:
3588 case ICMD_IF_ICMPGE:
3589 case ICMD_IF_ICMPGT:
3590 case ICMD_IF_ICMPLE:
3591 COUNT(count_pcmd_bra);
3592 OP2_BRANCH(TYPE_INT, TYPE_INT);
3596 case ICMD_IF_ACMPEQ:
3597 case ICMD_IF_ACMPNE:
3598 COUNT(count_pcmd_bra);
3599 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3606 coalescing_boundary = sd.new;
3607 COUNT(count_check_null);
3608 COUNT(count_pcmd_mem);
3609 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3610 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3615 if (!IS_2_WORD_TYPE(curstack->type)) {
3617 #ifdef ENABLE_VERIFIER
3620 if (IS_2_WORD_TYPE(curstack->prev->type))
3621 goto throw_stack_category_error;
3624 OP2_0_ANY_ANY; /* pop two slots */
3627 iptr->opc = ICMD_POP;
3628 OP1_0_ANY; /* pop one (two-word) slot */
3632 /* pop 0 push 1 dup */
3635 #ifdef ENABLE_VERIFIER
3638 if (IS_2_WORD_TYPE(curstack->type))
3639 goto throw_stack_category_error;
3642 COUNT(count_dup_instruction);
3648 coalescing_boundary = sd.new - 1;
3653 if (IS_2_WORD_TYPE(curstack->type)) {
3655 iptr->opc = ICMD_DUP;
3660 /* ..., ????, cat1 */
3661 #ifdef ENABLE_VERIFIER
3663 if (IS_2_WORD_TYPE(curstack->prev->type))
3664 goto throw_stack_category_error;
3667 src1 = curstack->prev;
3670 COPY_UP(src1); iptr++; len--;
3673 coalescing_boundary = sd.new;
3677 /* pop 2 push 3 dup */
3680 #ifdef ENABLE_VERIFIER
3683 if (IS_2_WORD_TYPE(curstack->type) ||
3684 IS_2_WORD_TYPE(curstack->prev->type))
3685 goto throw_stack_category_error;
3690 src1 = curstack->prev;
3695 /* move non-temporary sources out of the way */
3696 if (!IS_TEMPVAR(src2)) {
3697 MOVE_TO_TEMP(src2); iptr++; len--;
3700 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3702 MOVE_UP(src1); iptr++; len--;
3703 MOVE_UP(src2); iptr++; len--;
3705 COPY_DOWN(curstack, dst1);
3707 coalescing_boundary = sd.new;
3712 if (IS_2_WORD_TYPE(curstack->type)) {
3713 /* ..., ????, cat2 */
3714 #ifdef ENABLE_VERIFIER
3716 if (IS_2_WORD_TYPE(curstack->prev->type))
3717 goto throw_stack_category_error;
3720 iptr->opc = ICMD_DUP_X1;
3724 /* ..., ????, cat1 */
3725 #ifdef ENABLE_VERIFIER
3728 if (IS_2_WORD_TYPE(curstack->prev->type)
3729 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3730 goto throw_stack_category_error;
3735 src1 = curstack->prev->prev;
3736 src2 = curstack->prev;
3738 POPANY; POPANY; POPANY;
3741 /* move non-temporary sources out of the way */
3742 if (!IS_TEMPVAR(src2)) {
3743 MOVE_TO_TEMP(src2); iptr++; len--;
3745 if (!IS_TEMPVAR(src3)) {
3746 MOVE_TO_TEMP(src3); iptr++; len--;
3749 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3750 DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3752 MOVE_UP(src1); iptr++; len--;
3753 MOVE_UP(src2); iptr++; len--;
3754 MOVE_UP(src3); iptr++; len--;
3756 COPY_DOWN(curstack, dst2); iptr++; len--;
3757 COPY_DOWN(curstack->prev, dst1);
3759 coalescing_boundary = sd.new;
3763 /* pop 3 push 4 dup */
3767 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3768 /* ..., cat2, ???? */
3769 #ifdef ENABLE_VERIFIER
3771 if (IS_2_WORD_TYPE(curstack->type))
3772 goto throw_stack_category_error;
3775 iptr->opc = ICMD_DUP_X1;
3779 /* ..., cat1, ???? */
3780 #ifdef ENABLE_VERIFIER
3783 if (IS_2_WORD_TYPE(curstack->type)
3784 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3785 goto throw_stack_category_error;
3789 src1 = curstack->prev->prev;
3790 src2 = curstack->prev;
3792 POPANY; POPANY; POPANY;
3795 /* move non-temporary sources out of the way */
3796 if (!IS_TEMPVAR(src2)) {
3797 MOVE_TO_TEMP(src2); iptr++; len--;
3799 if (!IS_TEMPVAR(src3)) {
3800 MOVE_TO_TEMP(src3); iptr++; len--;
3803 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3805 MOVE_UP(src1); iptr++; len--;
3806 MOVE_UP(src2); iptr++; len--;
3807 MOVE_UP(src3); iptr++; len--;
3809 COPY_DOWN(curstack, dst1);
3811 coalescing_boundary = sd.new;
3817 if (IS_2_WORD_TYPE(curstack->type)) {
3818 /* ..., ????, cat2 */
3819 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3820 /* ..., cat2, cat2 */
3821 iptr->opc = ICMD_DUP_X1;
3825 /* ..., cat1, cat2 */
3826 #ifdef ENABLE_VERIFIER
3829 if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3830 goto throw_stack_category_error;
3833 iptr->opc = ICMD_DUP_X2;
3839 /* ..., ????, ????, cat1 */
3841 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3842 /* ..., cat2, ????, cat1 */
3843 #ifdef ENABLE_VERIFIER
3845 if (IS_2_WORD_TYPE(curstack->prev->type))
3846 goto throw_stack_category_error;
3849 iptr->opc = ICMD_DUP2_X1;
3853 /* ..., cat1, ????, cat1 */
3854 #ifdef ENABLE_VERIFIER
3857 if (IS_2_WORD_TYPE(curstack->prev->type)
3858 || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3859 goto throw_stack_category_error;
3863 src1 = curstack->prev->prev->prev;
3864 src2 = curstack->prev->prev;
3865 src3 = curstack->prev;
3867 POPANY; POPANY; POPANY; POPANY;
3870 /* move non-temporary sources out of the way */
3871 if (!IS_TEMPVAR(src2)) {
3872 MOVE_TO_TEMP(src2); iptr++; len--;
3874 if (!IS_TEMPVAR(src3)) {
3875 MOVE_TO_TEMP(src3); iptr++; len--;
3877 if (!IS_TEMPVAR(src4)) {
3878 MOVE_TO_TEMP(src4); iptr++; len--;
3881 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3882 DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3884 MOVE_UP(src1); iptr++; len--;
3885 MOVE_UP(src2); iptr++; len--;
3886 MOVE_UP(src3); iptr++; len--;
3887 MOVE_UP(src4); iptr++; len--;
3889 COPY_DOWN(curstack, dst2); iptr++; len--;
3890 COPY_DOWN(curstack->prev, dst1);
3892 coalescing_boundary = sd.new;
3896 /* pop 2 push 2 swap */
3899 #ifdef ENABLE_VERIFIER
3902 if (IS_2_WORD_TYPE(curstack->type)
3903 || IS_2_WORD_TYPE(curstack->prev->type))
3904 goto throw_stack_category_error;
3908 src1 = curstack->prev;
3913 /* move non-temporary sources out of the way */
3914 if (!IS_TEMPVAR(src1)) {
3915 MOVE_TO_TEMP(src1); iptr++; len--;
3918 MOVE_UP(src2); iptr++; len--;
3921 coalescing_boundary = sd.new;
3928 coalescing_boundary = sd.new;
3929 #if !SUPPORT_DIVISION
3930 bte = iptr->sx.s23.s3.bte;
3933 if (md->memuse > rd->memuse)
3934 rd->memuse = md->memuse;
3935 if (md->argintreguse > rd->argintreguse)
3936 rd->argintreguse = md->argintreguse;
3938 /* make all stack variables saved */
3942 sd.var[copy->varnum].flags |= SAVEDVAR;
3943 copy->flags |= SAVEDVAR;
3948 #endif /* !SUPPORT_DIVISION */
3959 COUNT(count_pcmd_op);
3960 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3965 coalescing_boundary = sd.new;
3966 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3967 bte = iptr->sx.s23.s3.bte;
3970 if (md->memuse > rd->memuse)
3971 rd->memuse = md->memuse;
3972 if (md->argintreguse > rd->argintreguse)
3973 rd->argintreguse = md->argintreguse;
3974 /* XXX non-leaf method? */
3976 /* make all stack variables saved */
3980 sd.var[copy->varnum].flags |= SAVEDVAR;
3981 copy->flags |= SAVEDVAR;
3986 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3991 #if SUPPORT_LONG_LOGICAL
3995 #endif /* SUPPORT_LONG_LOGICAL */
3996 COUNT(count_pcmd_op);
3997 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
4003 COUNT(count_pcmd_op);
4004 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
4012 COUNT(count_pcmd_op);
4013 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
4021 COUNT(count_pcmd_op);
4022 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
4026 COUNT(count_pcmd_op);
4027 #if SUPPORT_LONG_CMP_CONST
4028 if ((len == 0) || (iptr[1].sx.val.i != 0))
4031 switch (iptr[1].opc) {
4033 iptr->opc = ICMD_IF_LCMPEQ;
4035 iptr->dst.block = iptr[1].dst.block;
4036 iptr[1].opc = ICMD_NOP;
4038 OP2_BRANCH(TYPE_LNG, TYPE_LNG);
4041 COUNT(count_pcmd_bra);
4044 iptr->opc = ICMD_IF_LCMPNE;
4045 goto icmd_lcmp_if_tail;
4047 iptr->opc = ICMD_IF_LCMPLT;
4048 goto icmd_lcmp_if_tail;
4050 iptr->opc = ICMD_IF_LCMPGT;
4051 goto icmd_lcmp_if_tail;
4053 iptr->opc = ICMD_IF_LCMPLE;
4054 goto icmd_lcmp_if_tail;
4056 iptr->opc = ICMD_IF_LCMPGE;
4057 goto icmd_lcmp_if_tail;
4063 #endif /* SUPPORT_LONG_CMP_CONST */
4064 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
4067 /* XXX why is this deactivated? */
4070 COUNT(count_pcmd_op);
4071 if ((len == 0) || (iptr[1].sx.val.i != 0))
4074 switch (iptr[1].opc) {
4076 iptr->opc = ICMD_IF_FCMPEQ;
4078 iptr->dst.block = iptr[1].dst.block;
4079 iptr[1].opc = ICMD_NOP;
4081 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4084 COUNT(count_pcmd_bra);
4087 iptr->opc = ICMD_IF_FCMPNE;
4088 goto icmd_if_fcmpl_tail;
4090 iptr->opc = ICMD_IF_FCMPL_LT;
4091 goto icmd_if_fcmpl_tail;
4093 iptr->opc = ICMD_IF_FCMPL_GT;
4094 goto icmd_if_fcmpl_tail;
4096 iptr->opc = ICMD_IF_FCMPL_LE;
4097 goto icmd_if_fcmpl_tail;
4099 iptr->opc = ICMD_IF_FCMPL_GE;
4100 goto icmd_if_fcmpl_tail;
4107 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4111 COUNT(count_pcmd_op);
4112 if ((len == 0) || (iptr[1].sx.val.i != 0))
4115 switch (iptr[1].opc) {
4117 iptr->opc = ICMD_IF_FCMPEQ;
4119 iptr->dst.block = iptr[1].dst.block;
4120 iptr[1].opc = ICMD_NOP;
4122 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4125 COUNT(count_pcmd_bra);
4128 iptr->opc = ICMD_IF_FCMPNE;
4129 goto icmd_if_fcmpg_tail;
4131 iptr->opc = ICMD_IF_FCMPG_LT;
4132 goto icmd_if_fcmpg_tail;
4134 iptr->opc = ICMD_IF_FCMPG_GT;
4135 goto icmd_if_fcmpg_tail;
4137 iptr->opc = ICMD_IF_FCMPG_LE;
4138 goto icmd_if_fcmpg_tail;
4140 iptr->opc = ICMD_IF_FCMPG_GE;
4141 goto icmd_if_fcmpg_tail;
4148 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4152 COUNT(count_pcmd_op);
4153 if ((len == 0) || (iptr[1].sx.val.i != 0))
4156 switch (iptr[1].opc) {
4158 iptr->opc = ICMD_IF_DCMPEQ;
4160 iptr->dst.block = iptr[1].dst.block;
4161 iptr[1].opc = ICMD_NOP;
4163 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4166 COUNT(count_pcmd_bra);
4169 iptr->opc = ICMD_IF_DCMPNE;
4170 goto icmd_if_dcmpl_tail;
4172 iptr->opc = ICMD_IF_DCMPL_LT;
4173 goto icmd_if_dcmpl_tail;
4175 iptr->opc = ICMD_IF_DCMPL_GT;
4176 goto icmd_if_dcmpl_tail;
4178 iptr->opc = ICMD_IF_DCMPL_LE;
4179 goto icmd_if_dcmpl_tail;
4181 iptr->opc = ICMD_IF_DCMPL_GE;
4182 goto icmd_if_dcmpl_tail;
4189 OPTT2_1(TYPE_DBL, TYPE_INT);
4193 COUNT(count_pcmd_op);
4194 if ((len == 0) || (iptr[1].sx.val.i != 0))
4197 switch (iptr[1].opc) {
4199 iptr->opc = ICMD_IF_DCMPEQ;
4201 iptr->dst.block = iptr[1].dst.block;
4202 iptr[1].opc = ICMD_NOP;
4204 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4207 COUNT(count_pcmd_bra);
4210 iptr->opc = ICMD_IF_DCMPNE;
4211 goto icmd_if_dcmpg_tail;
4213 iptr->opc = ICMD_IF_DCMPG_LT;
4214 goto icmd_if_dcmpg_tail;
4216 iptr->opc = ICMD_IF_DCMPG_GT;
4217 goto icmd_if_dcmpg_tail;
4219 iptr->opc = ICMD_IF_DCMPG_LE;
4220 goto icmd_if_dcmpg_tail;
4222 iptr->opc = ICMD_IF_DCMPG_GE;
4223 goto icmd_if_dcmpg_tail;
4230 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4235 COUNT(count_pcmd_op);
4236 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4241 COUNT(count_pcmd_op);
4242 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4251 case ICMD_INT2SHORT:
4252 COUNT(count_pcmd_op);
4253 OP1_1(TYPE_INT, TYPE_INT);
4256 COUNT(count_pcmd_op);
4257 OP1_1(TYPE_LNG, TYPE_LNG);
4260 COUNT(count_pcmd_op);
4261 OP1_1(TYPE_FLT, TYPE_FLT);
4264 COUNT(count_pcmd_op);
4265 OP1_1(TYPE_DBL, TYPE_DBL);
4269 COUNT(count_pcmd_op);
4270 OP1_1(TYPE_INT, TYPE_LNG);
4273 COUNT(count_pcmd_op);
4274 OP1_1(TYPE_INT, TYPE_FLT);
4277 COUNT(count_pcmd_op);
4278 OP1_1(TYPE_INT, TYPE_DBL);
4281 COUNT(count_pcmd_op);
4282 OP1_1(TYPE_LNG, TYPE_INT);
4285 COUNT(count_pcmd_op);
4286 OP1_1(TYPE_LNG, TYPE_FLT);
4289 COUNT(count_pcmd_op);
4290 OP1_1(TYPE_LNG, TYPE_DBL);
4293 COUNT(count_pcmd_op);
4294 OP1_1(TYPE_FLT, TYPE_INT);
4297 COUNT(count_pcmd_op);
4298 OP1_1(TYPE_FLT, TYPE_LNG);
4301 COUNT(count_pcmd_op);
4302 OP1_1(TYPE_FLT, TYPE_DBL);
4305 COUNT(count_pcmd_op);
4306 OP1_1(TYPE_DBL, TYPE_INT);
4309 COUNT(count_pcmd_op);
4310 OP1_1(TYPE_DBL, TYPE_LNG);
4313 COUNT(count_pcmd_op);
4314 OP1_1(TYPE_DBL, TYPE_FLT);
4317 case ICMD_CHECKCAST:
4318 coalescing_boundary = sd.new;
4319 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4320 /* array type cast-check */
4322 bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4325 if (md->memuse > rd->memuse)
4326 rd->memuse = md->memuse;
4327 if (md->argintreguse > rd->argintreguse)
4328 rd->argintreguse = md->argintreguse;
4330 /* make all stack variables saved */
4334 sd.var[copy->varnum].flags |= SAVEDVAR;
4335 copy->flags |= SAVEDVAR;
4339 OP1_1(TYPE_ADR, TYPE_ADR);
4342 case ICMD_INSTANCEOF:
4343 case ICMD_ARRAYLENGTH:
4344 coalescing_boundary = sd.new;
4345 OP1_1(TYPE_ADR, TYPE_INT);
4349 case ICMD_ANEWARRAY:
4350 coalescing_boundary = sd.new;
4351 OP1_1(TYPE_INT, TYPE_ADR);
4355 coalescing_boundary = sd.new;
4356 COUNT(count_check_null);
4357 COUNT(count_pcmd_mem);
4358 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4359 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4364 case ICMD_GETSTATIC:
4365 coalescing_boundary = sd.new;
4366 COUNT(count_pcmd_mem);
4367 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4368 OP0_1(fmiref->parseddesc.fd->type);
4372 coalescing_boundary = sd.new;
4379 tbptr = iptr->sx.s23.s3.jsrtarget.block;
4380 tbptr->type = BBTYPE_SBR;
4382 assert(sd.bptr->next); /* XXX exception */
4383 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4384 #if defined(ENABLE_VERIFIER)
4385 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4388 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4392 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4394 /* We need to check for overflow right here because
4395 * the pushed value is poped afterwards */
4398 superblockend = true;
4399 /* XXX should not be marked as interface, as it does not need to be */
4400 /* allocated. Same for the invar of the target. */
4403 /* pop many push any */
4407 bte = iptr->sx.s23.s3.bte;
4411 case ICMD_INVOKESTATIC:
4412 case ICMD_INVOKESPECIAL:
4413 case ICMD_INVOKEVIRTUAL:
4414 case ICMD_INVOKEINTERFACE:
4415 COUNT(count_pcmd_met);
4417 /* Check for functions to replace with builtin
4420 if (builtintable_replace_function(iptr))
4423 INSTRUCTION_GET_METHODDESC(iptr, md);
4424 /* XXX resurrect this COUNT? */
4425 /* if (lm->flags & ACC_STATIC) */
4426 /* {COUNT(count_check_null);} */
4430 coalescing_boundary = sd.new;
4434 if (md->memuse > rd->memuse)
4435 rd->memuse = md->memuse;
4436 if (md->argintreguse > rd->argintreguse)
4437 rd->argintreguse = md->argintreguse;
4438 if (md->argfltreguse > rd->argfltreguse)
4439 rd->argfltreguse = md->argfltreguse;
4443 iptr->s1.argcount = stackdepth;
4444 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4447 for (i-- ; i >= 0; i--) {
4448 iptr->sx.s23.s2.args[i] = copy->varnum;
4450 /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4451 /* -> won't help anyway */
4452 if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4454 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4455 /* If we pass float arguments in integer argument registers, we
4456 * are not allowed to precolor them here. Floats have to be moved
4457 * to this regs explicitly in codegen().
4458 * Only arguments that are passed by stack anyway can be precolored
4459 * (michi 2005/07/24) */
4460 if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4461 (!IS_FLT_DBL_TYPE(copy->type)
4462 || md->params[i].inmemory)) {
4464 if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4469 if (md->params[i].inmemory) {
4470 sd.var[copy->varnum].vv.regoff =
4471 md->params[i].regoff;
4472 sd.var[copy->varnum].flags |=
4476 if (IS_FLT_DBL_TYPE(copy->type)) {
4477 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4478 assert(0); /* XXX is this assert ok? */
4480 sd.var[copy->varnum].vv.regoff =
4481 md->params[i].regoff;
4482 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4485 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4486 if (IS_2_WORD_TYPE(copy->type))
4487 sd.var[copy->varnum].vv.regoff =
4488 PACK_REGS(GET_LOW_REG(md->params[i].regoff),
4489 GET_HIGH_REG(md->params[i].regoff));
4492 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4493 sd.var[copy->varnum].vv.regoff =
4494 md->params[i].regoff;
4502 /* deal with live-through stack slots "under" the */
4508 iptr->sx.s23.s2.args[i++] = copy->varnum;
4509 sd.var[copy->varnum].flags |= SAVEDVAR;
4510 copy->flags |= SAVEDVAR | PASSTHROUGH;
4514 /* pop the arguments */
4523 /* push the return value */
4525 if (md->returntype.type != TYPE_VOID) {
4526 GET_NEW_VAR(sd, new_index, md->returntype.type);
4527 DST(md->returntype.type, new_index);
4532 case ICMD_MULTIANEWARRAY:
4533 coalescing_boundary = sd.new;
4534 if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4535 rd->argintreguse = MIN(3, INT_ARG_CNT);
4537 i = iptr->s1.argcount;
4541 iptr->sx.s23.s2.args = DMNEW(s4, i);
4543 #if defined(SPECIALMEMUSE)
4544 # if defined(__DARWIN__)
4545 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4546 rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4548 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4549 rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4552 # if defined(__I386__)
4553 if (rd->memuse < i + 3)
4554 rd->memuse = i + 3; /* n integer args spilled on stack */
4555 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4556 if (rd->memuse < i + 2)
4557 rd->memuse = i + 2; /* 4*4 bytes callee save space */
4560 rd->memuse = i; /* n integer args spilled on stack */
4561 # endif /* defined(__I386__) */
4565 /* check INT type here? Currently typecheck does this. */
4566 iptr->sx.s23.s2.args[i] = copy->varnum;
4567 if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4568 && (!IS_INOUT(copy))
4569 && (!IS_LOCALVAR(copy)) ) {
4570 copy->varkind = ARGVAR;
4571 sd.var[copy->varnum].flags |=
4572 INMEMORY & PREALLOC;
4573 #if defined(SPECIALMEMUSE)
4574 # if defined(__DARWIN__)
4575 sd.var[copy->varnum].vv.regoff = i +
4576 LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4578 sd.var[copy->varnum].vv.regoff = i +
4579 LA_SIZE_IN_POINTERS + 3;
4582 # if defined(__I386__)
4583 sd.var[copy->varnum].vv.regoff = i + 3;
4584 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4585 sd.var[copy->varnum].vv.regoff = i + 2;
4587 sd.var[copy->varnum].vv.regoff = i;
4588 # endif /* defined(__I386__) */
4589 #endif /* defined(SPECIALMEMUSE) */
4594 sd.var[copy->varnum].flags |= SAVEDVAR;
4595 copy->flags |= SAVEDVAR;
4599 i = iptr->s1.argcount;
4604 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4605 DST(TYPE_ADR, new_index);
4610 exceptions_throw_internalerror("Unknown ICMD %d during stack analysis",
4617 } /* while instructions */
4619 /* show state after last instruction */
4621 #if defined(STACK_VERBOSE)
4622 stack_verbose_show_state(&sd, NULL, curstack);
4625 /* stack slots at basic block end become interfaces */
4627 sd.bptr->outdepth = stackdepth;
4628 sd.bptr->outvars = DMNEW(s4, stackdepth);
4631 for (copy = curstack; copy; i--, copy = copy->prev) {
4635 /* with the new vars rd->interfaces will be removed */
4636 /* and all in and outvars have to be STACKVARS! */
4637 /* in the moment i.e. SWAP with in and out vars can */
4638 /* create an unresolvable conflict */
4643 v = sd.var + copy->varnum;
4646 /* do not allocate variables for returnAddresses */
4648 if (t != TYPE_RET) {
4649 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4650 /* no interface var until now for this depth and */
4652 jd->interface_map[i*5 + t].flags = v->flags;
4655 jd->interface_map[i*5 + t].flags |= v->flags;
4659 sd.bptr->outvars[i] = copy->varnum;
4662 /* check if interface slots at basic block begin must be saved */
4664 for (i=0; i<sd.bptr->indepth; ++i) {
4665 varinfo *v = sd.var + sd.bptr->invars[i];
4670 if (t != TYPE_RET) {
4671 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4672 /* no interface var until now for this depth and */
4674 jd->interface_map[i*5 + t].flags = v->flags;
4677 jd->interface_map[i*5 + t].flags |= v->flags;
4682 /* store the number of this block's variables */
4684 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4686 #if defined(STACK_VERBOSE)
4687 stack_verbose_block_exit(&sd, superblockend);
4690 /* reach the following block, if any */
4693 if (!stack_reach_next_block(&sd))
4698 } while (sd.repeat && !deadcode);
4700 /* reset locals of TYPE_RET|VOID to TYPE_ADR */
4702 /* A local variable may be used as both a returnAddress and a reference */
4703 /* type variable, as we do not split variables between these types when */
4704 /* renaming locals. While returnAddresses have been eliminated now, we */
4705 /* must assume that the variable is still used as TYPE_ADR. */
4706 /* The only way that a local can be TYPE_VOID at this point, is that it */
4707 /* was a TYPE_RET variable for which incompatible returnAddresses were */
4708 /* merged. Thus we must treat TYPE_VOID in the same way as TYPE_RET */
4710 /* XXX: It would be nice to remove otherwise unused returnAddress */
4711 /* variables from the local variable array, so they are not */
4712 /* allocated by simplereg. (For LSRA this is not needed). */
4714 for (i=0; i<sd.localcount; ++i) {
4715 if (sd.var[i].type == TYPE_RET || sd.var[i].type == TYPE_VOID)
4716 sd.var[i].type = TYPE_ADR;
4719 /* mark temporaries of TYPE_RET as PREALLOC to avoid allocation */
4721 for (i=sd.localcount; i<sd.vartop; ++i) {
4722 if (sd.var[i].type == TYPE_RET)
4723 sd.var[i].flags |= PREALLOC;
4726 /* XXX hack to fix up the ranges of the cloned single-block handlers */
4728 ex = jd->exceptiontable;
4729 for (; ex != NULL; ex = ex->down) {
4730 if (ex->start == ex->end) {
4731 assert(ex->end->next);
4732 ex->end = ex->end->next;
4736 /* store number of created variables */
4738 jd->vartop = sd.vartop;
4740 /* gather statistics *****************************************************/
4742 #if defined(ENABLE_STATISTICS)
4744 if (jd->basicblockcount > count_max_basic_blocks)
4745 count_max_basic_blocks = jd->basicblockcount;
4746 count_basic_blocks += jd->basicblockcount;
4747 if (jd->instructioncount > count_max_javainstr)
4748 count_max_javainstr = jd->instructioncount;
4749 count_javainstr += jd->instructioncount;
4750 if (jd->stackcount > count_upper_bound_new_stack)
4751 count_upper_bound_new_stack = jd->stackcount;
4752 if ((sd.new - jd->stack) > count_max_new_stack)
4753 count_max_new_stack = (sd.new - jd->stack);
4755 sd.bptr = jd->basicblocks;
4756 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4757 if (sd.bptr->flags > BBREACHED) {
4758 if (sd.bptr->indepth >= 10)
4759 count_block_stack[10]++;
4761 count_block_stack[sd.bptr->indepth]++;
4762 len = sd.bptr->icount;
4764 count_block_size_distribution[len]++;
4766 count_block_size_distribution[10]++;
4768 count_block_size_distribution[11]++;
4770 count_block_size_distribution[12]++;
4772 count_block_size_distribution[13]++;
4774 count_block_size_distribution[14]++;
4776 count_block_size_distribution[15]++;
4778 count_block_size_distribution[16]++;
4780 count_block_size_distribution[17]++;
4784 if (iteration_count == 1)
4785 count_analyse_iterations[0]++;
4786 else if (iteration_count == 2)
4787 count_analyse_iterations[1]++;
4788 else if (iteration_count == 3)
4789 count_analyse_iterations[2]++;
4790 else if (iteration_count == 4)
4791 count_analyse_iterations[3]++;
4793 count_analyse_iterations[4]++;
4795 if (jd->basicblockcount <= 5)
4796 count_method_bb_distribution[0]++;
4797 else if (jd->basicblockcount <= 10)
4798 count_method_bb_distribution[1]++;
4799 else if (jd->basicblockcount <= 15)
4800 count_method_bb_distribution[2]++;
4801 else if (jd->basicblockcount <= 20)
4802 count_method_bb_distribution[3]++;
4803 else if (jd->basicblockcount <= 30)
4804 count_method_bb_distribution[4]++;
4805 else if (jd->basicblockcount <= 40)
4806 count_method_bb_distribution[5]++;
4807 else if (jd->basicblockcount <= 50)
4808 count_method_bb_distribution[6]++;
4809 else if (jd->basicblockcount <= 75)
4810 count_method_bb_distribution[7]++;
4812 count_method_bb_distribution[8]++;
4814 #endif /* defined(ENABLE_STATISTICS) */
4816 /* everything's ok *******************************************************/
4820 /* goto labels for throwing verifier exceptions **************************/
4822 #if defined(ENABLE_VERIFIER)
4824 throw_stack_underflow:
4825 exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4828 throw_stack_overflow:
4829 exceptions_throw_verifyerror(m, "Stack size too large");
4832 throw_stack_type_error:
4833 exceptions_throw_verifyerror_for_stack(m, expectedtype);
4836 throw_stack_category_error:
4837 exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4844 /* stack_javalocals_store ******************************************************
4846 Model the effect of a ?STORE instruction upon the given javalocals array.
4849 iptr.............the ?STORE instruction
4850 javalocals.......the javalocals array to modify
4852 *******************************************************************************/
4854 void stack_javalocals_store(instruction *iptr, s4 *javalocals)
4856 s4 idx; /* index into the jd->var array */
4857 s4 j; /* java local index */
4859 idx = iptr->dst.varindex;
4860 j = iptr->sx.s23.s3.javaindex;
4863 if (iptr->flags.bits & INS_FLAG_RETADDR)
4864 javalocals[j] = iptr->sx.s23.s2.retaddrnr;
4866 javalocals[j] = idx;
4868 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
4869 javalocals[j-1] = UNUSED;
4871 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
4872 javalocals[j+1] = UNUSED;
4877 /* functions for verbose stack analysis output ********************************/
4879 #if defined(STACK_VERBOSE)
4880 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4882 printf("%c", show_jit_type_letters[v->type]);
4883 if (v->type == TYPE_RET) {
4884 printf("{L%03d}", v->vv.retaddr->nr);
4885 #if defined(ENABLE_VERIFIER)
4886 printf("{start=L%03d}", ((basicblock *)v->SBRSTART)->nr);
4892 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4894 assert(index >= 0 && index < sd->vartop);
4895 stack_verbose_show_varinfo(sd, sd->var + index);
4899 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4903 printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4905 for (i=0; i<bptr->indepth; ++i) {
4908 stack_verbose_show_variable(sd, bptr->invars[i]);
4913 printf("] javalocals ");
4914 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4915 printf(" inlocals [");
4916 if (bptr->inlocals) {
4917 for (i=0; i<sd->localcount; ++i) {
4920 stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4925 printf("] out:%d [", bptr->outdepth);
4926 if (bptr->outvars) {
4927 for (i=0; i<bptr->outdepth; ++i) {
4930 stack_verbose_show_variable(sd, bptr->outvars[i]);
4938 printf(" (clone of L%03d)", bptr->original->nr);
4940 basicblock *b = bptr->copied_to;
4942 printf(" (copied to ");
4943 for (; b; b = b->copied_to)
4944 printf("L%03d ", b->nr);
4951 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4955 printf("======================================== STACK %sANALYSE BLOCK ",
4956 (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4957 stack_verbose_show_block(sd, sd->bptr);
4960 if (sd->handlers[0]) {
4961 printf("HANDLERS: ");
4962 for (i=0; sd->handlers[i]; ++i) {
4963 printf("L%03d ", sd->handlers[i]->handler->nr);
4971 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4973 printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4974 stack_verbose_show_block(sd, sd->bptr);
4978 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackptr curstack)
4986 printf(" javalocals ");
4987 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4990 for(i = 0, sp = curstack; sp; sp = sp->prev)
4994 stack = MNEW(stackptr, depth);
4995 for(sp = curstack; sp; sp = sp->prev)
4998 for(i=0; i<depth; ++i) {
5002 v = &(sd->var[sp->varnum]);
5004 if (v->flags & INOUT)
5006 if (v->flags & PREALLOC)
5008 printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
5009 if (v->type == TYPE_RET) {
5010 printf("(L%03d)", v->vv.retaddr->nr);
5015 show_icmd(sd->jd, iptr, false, SHOW_PARSE);
5022 * These are local overrides for various environment variables in Emacs.
5023 * Please do not remove this and leave it at the end of the file, where
5024 * Emacs will automagically detect them.
5025 * ---------------------------------------------------------------------
5028 * indent-tabs-mode: t
5032 * vim:noexpandtab:sw=4:ts=4: