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 7486 2007-03-08 13:50:07Z twisti $
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 = BLOCK_OF((bt).insindex); \
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)
1119 #if defined(STACK_VERBOSE)
1120 printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1122 /* mark targets of backward branches */
1123 if (b->nr <= sd->bptr->nr)
1124 b->bitflags |= BBFLAG_REPLACEMENT;
1126 if (b->flags < BBREACHED) {
1127 /* b is reached for the first time. Create its invars. */
1129 #if defined(STACK_VERBOSE)
1130 printf("reached L%03d for the first time\n", b->nr);
1133 stack_create_invars(sd, b, curstack, stackdepth);
1135 b->flags = BBREACHED;
1140 /* b has been reached before. Check that its invars match. */
1142 return stack_check_invars(sd, b, curstack, stackdepth);
1147 /* stack_mark_reached_from_outvars *********************************************
1149 Mark the given block reached and propagate the outvars of the current block
1150 and the current locals to it. This function specializes the target block,
1151 if necessary, and returns a pointer to the specialized target.
1154 sd...........stack analysis data
1155 b............the block to reach
1158 a pointer to (a specialized version of) the target
1159 NULL.........a VerifyError has been thrown
1161 *******************************************************************************/
1163 static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
1165 #if defined(STACK_VERBOSE)
1166 printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1168 /* mark targets of backward branches */
1169 if (b->nr <= sd->bptr->nr)
1170 b->bitflags |= BBFLAG_REPLACEMENT;
1172 if (b->flags < BBREACHED) {
1173 /* b is reached for the first time. Create its invars. */
1175 #if defined(STACK_VERBOSE)
1176 printf("reached L%03d for the first time\n", b->nr);
1179 stack_create_invars_from_outvars(sd, b);
1181 b->flags = BBREACHED;
1186 /* b has been reached before. Check that its invars match. */
1188 return stack_check_invars_from_outvars(sd, b);
1193 /* stack_reach_next_block ******************************************************
1195 Mark the following block reached and propagate the outvars of the current block
1196 and the current locals to it. This function specializes the target block,
1197 if necessary, and returns a pointer to the specialized target.
1200 sd...........stack analysis data
1203 a pointer to (a specialized version of) the following block
1204 NULL.........a VerifyError has been thrown
1206 *******************************************************************************/
1208 static bool stack_reach_next_block(stackdata_t *sd)
1213 tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
1214 tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
1218 if (tbptr != sd->bptr->next) {
1219 #if defined(STACK_VERBOSE)
1220 printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
1222 iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
1223 assert(iptr->opc == ICMD_NOP);
1224 iptr->opc = ICMD_GOTO;
1225 iptr->dst.block = tbptr;
1227 if (tbptr->flags < BBFINISHED)
1228 sd->repeat = true; /* XXX check if we really need to repeat */
1235 /* stack_reach_handlers ********************************************************
1237 Reach the exception handlers for the current block.
1240 sd...........stack analysis data
1243 true.........everything ok
1244 false........a VerifyError has been thrown
1246 *******************************************************************************/
1248 static bool stack_reach_handlers(stackdata_t *sd)
1253 #if defined(STACK_VERBOSE)
1254 printf("reaching exception handlers...\n");
1257 for (i=0; sd->handlers[i]; ++i) {
1258 tbptr = sd->handlers[i]->handler;
1260 tbptr->type = BBTYPE_EXH;
1261 tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
1263 /* reach (and specialize) the handler block */
1265 tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
1270 sd->handlers[i]->handler = tbptr;
1277 /* stack_reanalyse_block ******************************************************
1279 Re-analyse the current block. This is called if either the block itself
1280 has already been analysed before, or the current block is a clone of an
1281 already analysed block, and this clone is reached for the first time.
1282 In the latter case, this function does all that is necessary for fully
1283 cloning the block (cloning the instruction list and variables, etc.).
1286 sd...........stack analysis data
1289 true.........everything ok
1290 false........a VerifyError has been thrown
1292 *******************************************************************************/
1294 #define RELOCATE(index) \
1296 if ((index) >= blockvarstart) \
1297 (index) += blockvarshift; \
1298 else if ((index) >= invarstart) \
1299 (index) += invarshift; \
1302 bool stack_reanalyse_block(stackdata_t *sd)
1314 branch_target_t *table;
1315 lookup_target_t *lookup;
1317 bool cloneinstructions;
1318 exception_entry *ex;
1320 #if defined(STACK_VERBOSE)
1321 stack_verbose_block_enter(sd, true);
1328 assert(orig != NULL);
1330 /* clone the instruction list */
1332 cloneinstructions = true;
1334 assert(orig->iinstr);
1336 iptr = DMNEW(instruction, len + 1);
1338 MCOPY(iptr, orig->iinstr, instruction, len);
1339 iptr[len].opc = ICMD_NOP;
1341 iptr[len].flags.bits = 0;
1345 /* reserve space for the clone's block variables */
1347 stack_grow_variable_array(sd, orig->varcount);
1349 /* we already have the invars set */
1351 assert(b->indepth == orig->indepth);
1353 /* calculate relocation shifts for invars and block variables */
1355 if (orig->indepth) {
1356 invarstart = orig->invars[0];
1357 invarshift = b->invars[0] - invarstart;
1360 invarstart = INT_MAX;
1363 blockvarstart = orig->varstart;
1364 blockvarshift = sd->vartop - blockvarstart;
1366 /* copy block variables */
1368 b->varstart = sd->vartop;
1369 b->varcount = orig->varcount;
1370 sd->vartop += b->varcount;
1371 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1375 b->outdepth = orig->outdepth;
1376 b->outvars = DMNEW(s4, orig->outdepth);
1377 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1379 /* clone exception handlers */
1381 for (i=0; sd->handlers[i]; ++i) {
1382 ex = DNEW(exception_entry);
1383 ex->handler = sd->handlers[i]->handler;
1385 ex->end = b; /* XXX hack, see end of stack_analyse */
1386 ex->catchtype = sd->handlers[i]->catchtype;
1389 assert(sd->extableend->down == NULL);
1390 sd->extableend->down = ex;
1391 sd->extableend = ex;
1392 sd->jd->exceptiontablelength++;
1394 sd->handlers[i] = ex;
1398 cloneinstructions = false;
1401 invarstart = sd->vartop;
1402 blockvarstart = sd->vartop;
1407 /* find exception handlers for the cloned block */
1409 ex = sd->jd->exceptiontable;
1410 for (; ex != NULL; ex = ex->down) {
1411 /* XXX the cloned exception handlers have identical */
1412 /* start end end blocks. */
1413 if ((ex->start == b) && (ex->end == b)) {
1414 sd->handlers[len++] = ex;
1417 sd->handlers[len] = NULL;
1420 #if defined(STACK_VERBOSE)
1421 printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1422 printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1425 /* mark block as finished */
1427 b->flags = BBFINISHED;
1429 /* initialize locals at the start of this block */
1432 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1434 MCOPY(sd->javalocals, b->javalocals, s4, sd->maxlocals);
1436 /* reach exception handlers for this block */
1438 if (!stack_reach_handlers(sd))
1441 superblockend = false;
1443 for (len = b->icount; len--; iptr++) {
1444 #if defined(STACK_VERBOSE)
1445 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1449 switch (iptr->opc) {
1451 j = iptr->s1.varindex;
1453 #if defined(ENABLE_VERIFIER)
1454 if (sd->var[j].type != TYPE_RET) {
1455 exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1460 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1461 superblockend = true;
1465 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1466 RELOCATE(iptr->dst.varindex);
1467 superblockend = true;
1471 superblockend = true;
1474 case ICMD_CHECKNULL:
1475 case ICMD_PUTSTATICCONST:
1483 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1484 superblockend = true;
1487 /* pop 0 push 1 const */
1495 /* pop 0 push 1 load */
1502 RELOCATE(iptr->dst.varindex);
1515 RELOCATE(iptr->sx.s23.s2.varindex);
1516 RELOCATE(iptr->s1.varindex);
1517 RELOCATE(iptr->dst.varindex);
1530 RELOCATE(iptr->sx.s23.s3.varindex);
1531 RELOCATE(iptr->sx.s23.s2.varindex);
1532 RELOCATE(iptr->s1.varindex);
1535 /* pop 1 push 0 store */
1542 RELOCATE(iptr->s1.varindex);
1544 j = iptr->dst.varindex;
1545 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1546 i = iptr->sx.s23.s3.javaindex;
1547 if (iptr->flags.bits & INS_FLAG_RETADDR) {
1548 iptr->sx.s23.s2.retaddrnr =
1549 UNUSED - (1 + sd->var[j].vv.retaddr->nr);
1550 sd->javalocals[i] = iptr->sx.s23.s2.retaddrnr;
1553 sd->javalocals[i] = j;
1554 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
1555 sd->javalocals[i-1] = UNUSED;
1556 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
1557 sd->javalocals[i+1] = UNUSED;
1568 RELOCATE(iptr->s1.varindex);
1569 superblockend = true;
1572 case ICMD_PUTSTATIC:
1573 case ICMD_PUTFIELDCONST:
1575 RELOCATE(iptr->s1.varindex);
1578 /* pop 1 push 0 branch */
1581 case ICMD_IFNONNULL:
1596 RELOCATE(iptr->s1.varindex);
1597 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1600 /* pop 1 push 0 table branch */
1602 case ICMD_TABLESWITCH:
1603 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1605 if (cloneinstructions) {
1606 table = DMNEW(branch_target_t, i);
1607 MCOPY(table, iptr->dst.table, branch_target_t, i);
1608 iptr->dst.table = table;
1611 table = iptr->dst.table;
1614 RELOCATE(iptr->s1.varindex);
1616 table->block = stack_mark_reached_from_outvars(sd, table->block);
1619 superblockend = true;
1622 case ICMD_LOOKUPSWITCH:
1623 i = iptr->sx.s23.s2.lookupcount;
1624 if (cloneinstructions) {
1625 lookup = DMNEW(lookup_target_t, i);
1626 MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1627 iptr->dst.lookup = lookup;
1630 lookup = iptr->dst.lookup;
1632 RELOCATE(iptr->s1.varindex);
1634 lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1637 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1638 superblockend = true;
1641 case ICMD_MONITORENTER:
1642 case ICMD_MONITOREXIT:
1643 RELOCATE(iptr->s1.varindex);
1646 /* pop 2 push 0 branch */
1648 case ICMD_IF_ICMPEQ:
1649 case ICMD_IF_ICMPNE:
1650 case ICMD_IF_ICMPLT:
1651 case ICMD_IF_ICMPGE:
1652 case ICMD_IF_ICMPGT:
1653 case ICMD_IF_ICMPLE:
1655 case ICMD_IF_LCMPEQ:
1656 case ICMD_IF_LCMPNE:
1657 case ICMD_IF_LCMPLT:
1658 case ICMD_IF_LCMPGE:
1659 case ICMD_IF_LCMPGT:
1660 case ICMD_IF_LCMPLE:
1662 case ICMD_IF_FCMPEQ:
1663 case ICMD_IF_FCMPNE:
1665 case ICMD_IF_FCMPL_LT:
1666 case ICMD_IF_FCMPL_GE:
1667 case ICMD_IF_FCMPL_GT:
1668 case ICMD_IF_FCMPL_LE:
1670 case ICMD_IF_FCMPG_LT:
1671 case ICMD_IF_FCMPG_GE:
1672 case ICMD_IF_FCMPG_GT:
1673 case ICMD_IF_FCMPG_LE:
1675 case ICMD_IF_DCMPEQ:
1676 case ICMD_IF_DCMPNE:
1678 case ICMD_IF_DCMPL_LT:
1679 case ICMD_IF_DCMPL_GE:
1680 case ICMD_IF_DCMPL_GT:
1681 case ICMD_IF_DCMPL_LE:
1683 case ICMD_IF_DCMPG_LT:
1684 case ICMD_IF_DCMPG_GE:
1685 case ICMD_IF_DCMPG_GT:
1686 case ICMD_IF_DCMPG_LE:
1688 case ICMD_IF_ACMPEQ:
1689 case ICMD_IF_ACMPNE:
1690 RELOCATE(iptr->sx.s23.s2.varindex);
1691 RELOCATE(iptr->s1.varindex);
1692 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1698 case ICMD_IASTORECONST:
1699 case ICMD_LASTORECONST:
1700 case ICMD_AASTORECONST:
1701 case ICMD_BASTORECONST:
1702 case ICMD_CASTORECONST:
1703 case ICMD_SASTORECONST:
1705 RELOCATE(iptr->sx.s23.s2.varindex);
1706 RELOCATE(iptr->s1.varindex);
1709 /* pop 0 push 1 copy */
1713 RELOCATE(iptr->dst.varindex);
1714 RELOCATE(iptr->s1.varindex);
1715 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1757 RELOCATE(iptr->sx.s23.s2.varindex);
1758 RELOCATE(iptr->s1.varindex);
1759 RELOCATE(iptr->dst.varindex);
1764 case ICMD_CHECKCAST:
1765 case ICMD_ARRAYLENGTH:
1766 case ICMD_INSTANCEOF:
1768 case ICMD_ANEWARRAY:
1770 case ICMD_IADDCONST:
1771 case ICMD_ISUBCONST:
1772 case ICMD_IMULCONST:
1776 case ICMD_IANDCONST:
1778 case ICMD_IXORCONST:
1779 case ICMD_ISHLCONST:
1780 case ICMD_ISHRCONST:
1781 case ICMD_IUSHRCONST:
1782 case ICMD_LADDCONST:
1783 case ICMD_LSUBCONST:
1784 case ICMD_LMULCONST:
1788 case ICMD_LANDCONST:
1790 case ICMD_LXORCONST:
1791 case ICMD_LSHLCONST:
1792 case ICMD_LSHRCONST:
1793 case ICMD_LUSHRCONST:
1797 case ICMD_INT2SHORT:
1813 RELOCATE(iptr->s1.varindex);
1814 RELOCATE(iptr->dst.varindex);
1819 case ICMD_GETSTATIC:
1821 RELOCATE(iptr->dst.varindex);
1824 /* pop many push any */
1826 case ICMD_INVOKESTATIC:
1827 case ICMD_INVOKESPECIAL:
1828 case ICMD_INVOKEVIRTUAL:
1829 case ICMD_INVOKEINTERFACE:
1831 case ICMD_MULTIANEWARRAY:
1832 i = iptr->s1.argcount;
1833 if (cloneinstructions) {
1834 argp = DMNEW(s4, i);
1835 MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1836 iptr->sx.s23.s2.args = argp;
1839 argp = iptr->sx.s23.s2.args;
1846 RELOCATE(iptr->dst.varindex);
1850 exceptions_throw_internalerror("Unknown ICMD %d during stack re-analysis",
1855 #if defined(STACK_VERBOSE)
1856 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1861 /* relocate outvars */
1863 for (i=0; i<b->outdepth; ++i) {
1864 RELOCATE(b->outvars[i]);
1867 #if defined(STACK_VERBOSE)
1868 stack_verbose_block_exit(sd, superblockend);
1871 /* propagate to the next block */
1874 if (!stack_reach_next_block(sd))
1881 /* stack_change_to_tempvar *****************************************************
1883 Change the given stackslot to a TEMPVAR. This includes creating a new
1884 temporary variable and changing the dst.varindex of the creator of the
1885 stacklot to the new variable index. If this stackslot has been passed
1886 through ICMDs between the point of its creation and the current point,
1887 then the variable index is also changed in these ICMDs.
1890 sd...........stack analysis data
1891 sp...........stackslot to change
1892 ilimit.......instruction up to which to look for ICMDs passing-through
1893 the stackslot (exclusive). This may point exactly after the
1894 last instruction, in which case the search is done to the
1897 *******************************************************************************/
1899 static void stack_change_to_tempvar(stackdata_t *sd, stackptr sp,
1900 instruction *ilimit)
1908 oldindex = sp->varnum;
1910 /* create a new temporary variable */
1912 GET_NEW_VAR(*sd, newindex, sp->type);
1914 sd->var[newindex].flags = sp->flags;
1916 /* change the stackslot */
1918 sp->varnum = newindex;
1919 sp->varkind = TEMPVAR;
1921 /* change the dst.varindex of the stackslot's creator */
1924 sp->creator->dst.varindex = newindex;
1926 /* handle ICMDs this stackslot passed through, if any */
1928 if (sp->flags & PASSTHROUGH) {
1929 iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
1931 /* assert that the limit points to an ICMD, or after the last one */
1933 assert(ilimit >= sd->bptr->iinstr);
1934 assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
1936 /* find the stackdepth under sp plus one */
1937 /* Note: This number is usually known when this function is called, */
1938 /* but calculating it here is less error-prone and should not be */
1939 /* a performance problem. */
1941 for (depth = 0; sp != NULL; sp = sp->prev)
1944 /* iterate over all instructions in the range and replace */
1946 for (; iptr < ilimit; ++iptr) {
1947 switch (iptr->opc) {
1948 case ICMD_INVOKESTATIC:
1949 case ICMD_INVOKESPECIAL:
1950 case ICMD_INVOKEVIRTUAL:
1951 case ICMD_INVOKEINTERFACE:
1953 i = iptr->s1.argcount - depth;
1954 if (iptr->sx.s23.s2.args[i] == oldindex) {
1955 iptr->sx.s23.s2.args[i] = newindex;
1958 /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
1959 /* stackslot, it must be added in this switch! */
1966 /* stack_init_javalocals *******************************************************
1968 Initialize the mapping from Java locals to cacao variables at method entry.
1971 sd...........stack analysis data
1973 *******************************************************************************/
1975 static void stack_init_javalocals(stackdata_t *sd)
1984 jl = DMNEW(s4, sd->maxlocals);
1985 jd->basicblocks[0].javalocals = jl;
1987 for (i=0; i<sd->maxlocals; ++i)
1990 md = jd->m->parseddesc;
1992 for (i=0; i<md->paramcount; ++i) {
1993 t = md->paramtypes[i].type;
1994 jl[j] = jd->local_map[5*j + t];
1996 if (IS_2_WORD_TYPE(t))
2002 /* stack_analyse ***************************************************************
2004 Analyse_stack uses the intermediate code created by parse.c to
2005 build a model of the JVM operand stack for the current method.
2007 The following checks are performed:
2008 - check for operand stack underflow (before each instruction)
2009 - check for operand stack overflow (after[1] each instruction)
2010 - check for matching stack depth at merging points
2011 - check for matching basic types[2] at merging points
2012 - check basic types for instruction input (except for BUILTIN*
2013 opcodes, INVOKE* opcodes and MULTIANEWARRAY)
2015 [1]) Checking this after the instruction should be ok. parse.c
2016 counts the number of required stack slots in such a way that it is
2017 only vital that we don't exceed `maxstack` at basic block
2020 [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
2021 DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
2022 types are not discerned.
2024 *******************************************************************************/
2026 bool stack_analyse(jitdata *jd)
2028 methodinfo *m; /* method being analyzed */
2031 #if defined(ENABLE_SSA)
2035 stackptr curstack; /* current stack top */
2037 int opcode; /* opcode of current instruction */
2040 int len; /* # of instructions after the current one */
2041 bool superblockend; /* if true, no fallthrough to next block */
2042 bool deadcode; /* true if no live code has been reached */
2043 instruction *iptr; /* the current instruction */
2045 basicblock *original;
2046 exception_entry *ex;
2048 stackptr *last_store_boundary;
2049 stackptr coalescing_boundary;
2051 stackptr src1, src2, src3, src4, dst1, dst2;
2053 branch_target_t *table;
2054 lookup_target_t *lookup;
2055 #if defined(ENABLE_VERIFIER)
2056 int expectedtype; /* used by CHECK_BASIC_TYPE */
2058 builtintable_entry *bte;
2060 constant_FMIref *fmiref;
2061 #if defined(ENABLE_STATISTICS)
2062 int iteration_count; /* number of iterations of analysis */
2064 int new_index; /* used to get a new var index with GET_NEW_INDEX*/
2066 #if defined(STACK_VERBOSE)
2067 show_method(jd, SHOW_PARSE);
2070 /* get required compiler data - initialization */
2074 #if defined(ENABLE_SSA)
2078 /* initialize the stackdata_t struct */
2082 sd.varcount = jd->varcount;
2083 sd.vartop = jd->vartop;
2084 sd.localcount = jd->localcount;
2086 sd.varsallocated = sd.varcount;
2087 sd.maxlocals = m->maxlocals;
2088 sd.javalocals = DMNEW(s4, sd.maxlocals);
2089 sd.handlers = DMNEW(exception_entry *, jd->exceptiontablelength + 1);
2091 /* prepare the variable for exception handler stacks */
2092 /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2094 sd.exstack.type = TYPE_ADR;
2095 sd.exstack.prev = NULL;
2096 sd.exstack.varnum = sd.localcount;
2097 sd.var[sd.exstack.varnum].type = TYPE_ADR;
2099 #if defined(ENABLE_LSRA)
2100 m->maxlifetimes = 0;
2103 #if defined(ENABLE_STATISTICS)
2104 iteration_count = 0;
2107 /* find the last real basic block */
2109 sd.last_real_block = NULL;
2110 tbptr = jd->basicblocks;
2111 while (tbptr->next) {
2112 sd.last_real_block = tbptr;
2113 tbptr = tbptr->next;
2115 assert(sd.last_real_block);
2117 /* find the last exception handler */
2119 if (jd->exceptiontablelength)
2120 sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2122 sd.extableend = NULL;
2124 /* init jd->interface_map */
2126 jd->maxinterfaces = m->maxstack;
2127 jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2128 for (i = 0; i < m->maxstack * 5; i++)
2129 jd->interface_map[i].flags = UNUSED;
2131 last_store_boundary = DMNEW(stackptr, m->maxlocals);
2133 /* initialize flags and invars (none) of first block */
2135 jd->basicblocks[0].flags = BBREACHED;
2136 jd->basicblocks[0].invars = NULL;
2137 jd->basicblocks[0].indepth = 0;
2138 jd->basicblocks[0].inlocals =
2139 DMNEW(varinfo, jd->localcount + VERIFIER_EXTRA_LOCALS);
2140 MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo,
2141 jd->localcount + VERIFIER_EXTRA_LOCALS);
2143 /* initialize java local mapping of first block */
2145 stack_init_javalocals(&sd);
2147 /* stack analysis loop (until fixpoint reached) **************************/
2150 #if defined(ENABLE_STATISTICS)
2154 /* initialize loop over basic blocks */
2156 sd.bptr = jd->basicblocks;
2157 superblockend = true;
2159 curstack = NULL; stackdepth = 0;
2162 /* iterate over basic blocks *****************************************/
2164 for (; sd.bptr; sd.bptr = sd.bptr->next) {
2166 if (sd.bptr->flags == BBDELETED) {
2167 /* This block has been deleted - do nothing. */
2172 if (sd.bptr->flags == BBTYPECHECK_REACHED) {
2173 /* re-analyse a block because its input changed */
2175 if (!stack_reanalyse_block(&sd))
2177 superblockend = true; /* XXX */
2181 if (superblockend && (sd.bptr->flags < BBREACHED)) {
2182 /* This block has not been reached so far, and we */
2183 /* don't fall into it, so we'll have to iterate again. */
2189 if (sd.bptr->flags > BBREACHED) {
2190 /* This block is already finished. */
2192 superblockend = true;
2196 if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2197 /* This block is a clone and the original has not been */
2198 /* analysed, yet. Analyse it on the next iteration. */
2201 /* XXX superblockend? */
2205 /* This block has to be analysed now. */
2209 /* XXX The rest of this block is still indented one level too */
2210 /* much in order to avoid a giant diff by changing that. */
2212 /* We know that sd.bptr->flags == BBREACHED. */
2213 /* This block has been reached before. */
2215 assert(sd.bptr->flags == BBREACHED);
2216 stackdepth = sd.bptr->indepth;
2218 /* find exception handlers for this block */
2220 /* determine the active exception handlers for this block */
2221 /* XXX could use a faster algorithm with sorted lists or */
2224 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2227 ex = jd->exceptiontable;
2228 for (; ex != NULL; ex = ex->down) {
2229 if ((ex->start <= original) && (ex->end > original)) {
2230 sd.handlers[len++] = ex;
2233 sd.handlers[len] = NULL;
2236 /* reanalyse cloned block */
2238 if (sd.bptr->original) {
2239 if (!stack_reanalyse_block(&sd))
2244 /* reset the new pointer for allocating stackslots */
2248 /* create the instack of this block */
2250 curstack = stack_create_instack(&sd);
2252 /* initialize locals at the start of this block */
2254 if (sd.bptr->inlocals)
2255 MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2257 MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2259 /* set up local variables for analyzing this block */
2261 superblockend = false;
2262 len = sd.bptr->icount;
2263 iptr = sd.bptr->iinstr;
2265 /* mark the block as analysed */
2267 sd.bptr->flags = BBFINISHED;
2269 /* reset variables for dependency checking */
2271 coalescing_boundary = sd.new;
2272 for( i = 0; i < m->maxlocals; i++)
2273 last_store_boundary[i] = sd.new;
2275 /* remember the start of this block's variables */
2277 sd.bptr->varstart = sd.vartop;
2279 #if defined(STACK_VERBOSE)
2280 stack_verbose_block_enter(&sd, false);
2283 /* reach exception handlers for this block */
2285 if (!stack_reach_handlers(&sd))
2288 /* iterate over ICMDs ****************************************/
2290 while (--len >= 0) {
2292 #if defined(STACK_VERBOSE)
2293 stack_verbose_show_state(&sd, iptr, curstack);
2296 /* fetch the current opcode */
2300 /* automatically replace some ICMDs with builtins */
2302 #if defined(USEBUILTINTABLE)
2303 bte = builtintable_get_automatic(opcode);
2305 if (bte && bte->opcode == opcode) {
2306 iptr->opc = ICMD_BUILTIN;
2307 iptr->flags.bits &= INS_FLAG_ID_MASK;
2308 iptr->sx.s23.s3.bte = bte;
2309 /* iptr->line is already set */
2310 jd->isleafmethod = false;
2313 #endif /* defined(USEBUILTINTABLE) */
2315 /* main opcode switch *************************************/
2327 case ICMD_CHECKNULL:
2328 coalescing_boundary = sd.new;
2329 COUNT(count_check_null);
2332 iptr->dst.varindex = iptr->s1.varindex;
2336 j = iptr->s1.varindex =
2337 jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2339 #if defined(ENABLE_VERIFIER)
2340 if (sd.var[j].type != TYPE_RET) {
2341 exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2348 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2349 superblockend = true;
2353 COUNT(count_pcmd_return);
2356 superblockend = true;
2357 sd.jd->returncount++;
2358 sd.jd->returnblock = sd.bptr;
2362 /* pop 0 push 1 const */
2364 /************************** ICONST OPTIMIZATIONS **************************/
2367 COUNT(count_pcmd_load);
2371 switch (iptr[1].opc) {
2373 iptr->opc = ICMD_IADDCONST;
2377 iptr[1].opc = ICMD_NOP;
2378 OP1_1(TYPE_INT, TYPE_INT);
2379 COUNT(count_pcmd_op);
2383 iptr->opc = ICMD_ISUBCONST;
2384 goto icmd_iconst_tail;
2385 #if SUPPORT_CONST_MUL
2387 iptr->opc = ICMD_IMULCONST;
2388 goto icmd_iconst_tail;
2389 #else /* SUPPORT_CONST_MUL */
2391 if (iptr->sx.val.i == 0x00000002)
2393 else if (iptr->sx.val.i == 0x00000004)
2395 else if (iptr->sx.val.i == 0x00000008)
2397 else if (iptr->sx.val.i == 0x00000010)
2399 else if (iptr->sx.val.i == 0x00000020)
2401 else if (iptr->sx.val.i == 0x00000040)
2403 else if (iptr->sx.val.i == 0x00000080)
2405 else if (iptr->sx.val.i == 0x00000100)
2407 else if (iptr->sx.val.i == 0x00000200)
2409 else if (iptr->sx.val.i == 0x00000400)
2410 iptr->sx.val.i = 10;
2411 else if (iptr->sx.val.i == 0x00000800)
2412 iptr->sx.val.i = 11;
2413 else if (iptr->sx.val.i == 0x00001000)
2414 iptr->sx.val.i = 12;
2415 else if (iptr->sx.val.i == 0x00002000)
2416 iptr->sx.val.i = 13;
2417 else if (iptr->sx.val.i == 0x00004000)
2418 iptr->sx.val.i = 14;
2419 else if (iptr->sx.val.i == 0x00008000)
2420 iptr->sx.val.i = 15;
2421 else if (iptr->sx.val.i == 0x00010000)
2422 iptr->sx.val.i = 16;
2423 else if (iptr->sx.val.i == 0x00020000)
2424 iptr->sx.val.i = 17;
2425 else if (iptr->sx.val.i == 0x00040000)
2426 iptr->sx.val.i = 18;
2427 else if (iptr->sx.val.i == 0x00080000)
2428 iptr->sx.val.i = 19;
2429 else if (iptr->sx.val.i == 0x00100000)
2430 iptr->sx.val.i = 20;
2431 else if (iptr->sx.val.i == 0x00200000)
2432 iptr->sx.val.i = 21;
2433 else if (iptr->sx.val.i == 0x00400000)
2434 iptr->sx.val.i = 22;
2435 else if (iptr->sx.val.i == 0x00800000)
2436 iptr->sx.val.i = 23;
2437 else if (iptr->sx.val.i == 0x01000000)
2438 iptr->sx.val.i = 24;
2439 else if (iptr->sx.val.i == 0x02000000)
2440 iptr->sx.val.i = 25;
2441 else if (iptr->sx.val.i == 0x04000000)
2442 iptr->sx.val.i = 26;
2443 else if (iptr->sx.val.i == 0x08000000)
2444 iptr->sx.val.i = 27;
2445 else if (iptr->sx.val.i == 0x10000000)
2446 iptr->sx.val.i = 28;
2447 else if (iptr->sx.val.i == 0x20000000)
2448 iptr->sx.val.i = 29;
2449 else if (iptr->sx.val.i == 0x40000000)
2450 iptr->sx.val.i = 30;
2451 else if (iptr->sx.val.i == 0x80000000)
2452 iptr->sx.val.i = 31;
2456 iptr->opc = ICMD_IMULPOW2;
2457 goto icmd_iconst_tail;
2458 #endif /* SUPPORT_CONST_MUL */
2460 if (iptr->sx.val.i == 0x00000002)
2462 else if (iptr->sx.val.i == 0x00000004)
2464 else if (iptr->sx.val.i == 0x00000008)
2466 else if (iptr->sx.val.i == 0x00000010)
2468 else if (iptr->sx.val.i == 0x00000020)
2470 else if (iptr->sx.val.i == 0x00000040)
2472 else if (iptr->sx.val.i == 0x00000080)
2474 else if (iptr->sx.val.i == 0x00000100)
2476 else if (iptr->sx.val.i == 0x00000200)
2478 else if (iptr->sx.val.i == 0x00000400)
2479 iptr->sx.val.i = 10;
2480 else if (iptr->sx.val.i == 0x00000800)
2481 iptr->sx.val.i = 11;
2482 else if (iptr->sx.val.i == 0x00001000)
2483 iptr->sx.val.i = 12;
2484 else if (iptr->sx.val.i == 0x00002000)
2485 iptr->sx.val.i = 13;
2486 else if (iptr->sx.val.i == 0x00004000)
2487 iptr->sx.val.i = 14;
2488 else if (iptr->sx.val.i == 0x00008000)
2489 iptr->sx.val.i = 15;
2490 else if (iptr->sx.val.i == 0x00010000)
2491 iptr->sx.val.i = 16;
2492 else if (iptr->sx.val.i == 0x00020000)
2493 iptr->sx.val.i = 17;
2494 else if (iptr->sx.val.i == 0x00040000)
2495 iptr->sx.val.i = 18;
2496 else if (iptr->sx.val.i == 0x00080000)
2497 iptr->sx.val.i = 19;
2498 else if (iptr->sx.val.i == 0x00100000)
2499 iptr->sx.val.i = 20;
2500 else if (iptr->sx.val.i == 0x00200000)
2501 iptr->sx.val.i = 21;
2502 else if (iptr->sx.val.i == 0x00400000)
2503 iptr->sx.val.i = 22;
2504 else if (iptr->sx.val.i == 0x00800000)
2505 iptr->sx.val.i = 23;
2506 else if (iptr->sx.val.i == 0x01000000)
2507 iptr->sx.val.i = 24;
2508 else if (iptr->sx.val.i == 0x02000000)
2509 iptr->sx.val.i = 25;
2510 else if (iptr->sx.val.i == 0x04000000)
2511 iptr->sx.val.i = 26;
2512 else if (iptr->sx.val.i == 0x08000000)
2513 iptr->sx.val.i = 27;
2514 else if (iptr->sx.val.i == 0x10000000)
2515 iptr->sx.val.i = 28;
2516 else if (iptr->sx.val.i == 0x20000000)
2517 iptr->sx.val.i = 29;
2518 else if (iptr->sx.val.i == 0x40000000)
2519 iptr->sx.val.i = 30;
2520 else if (iptr->sx.val.i == 0x80000000)
2521 iptr->sx.val.i = 31;
2525 iptr->opc = ICMD_IDIVPOW2;
2526 goto icmd_iconst_tail;
2529 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2530 if ((iptr->sx.val.i == 0x00000002) ||
2531 (iptr->sx.val.i == 0x00000004) ||
2532 (iptr->sx.val.i == 0x00000008) ||
2533 (iptr->sx.val.i == 0x00000010) ||
2534 (iptr->sx.val.i == 0x00000020) ||
2535 (iptr->sx.val.i == 0x00000040) ||
2536 (iptr->sx.val.i == 0x00000080) ||
2537 (iptr->sx.val.i == 0x00000100) ||
2538 (iptr->sx.val.i == 0x00000200) ||
2539 (iptr->sx.val.i == 0x00000400) ||
2540 (iptr->sx.val.i == 0x00000800) ||
2541 (iptr->sx.val.i == 0x00001000) ||
2542 (iptr->sx.val.i == 0x00002000) ||
2543 (iptr->sx.val.i == 0x00004000) ||
2544 (iptr->sx.val.i == 0x00008000) ||
2545 (iptr->sx.val.i == 0x00010000) ||
2546 (iptr->sx.val.i == 0x00020000) ||
2547 (iptr->sx.val.i == 0x00040000) ||
2548 (iptr->sx.val.i == 0x00080000) ||
2549 (iptr->sx.val.i == 0x00100000) ||
2550 (iptr->sx.val.i == 0x00200000) ||
2551 (iptr->sx.val.i == 0x00400000) ||
2552 (iptr->sx.val.i == 0x00800000) ||
2553 (iptr->sx.val.i == 0x01000000) ||
2554 (iptr->sx.val.i == 0x02000000) ||
2555 (iptr->sx.val.i == 0x04000000) ||
2556 (iptr->sx.val.i == 0x08000000) ||
2557 (iptr->sx.val.i == 0x10000000) ||
2558 (iptr->sx.val.i == 0x20000000) ||
2559 (iptr->sx.val.i == 0x40000000) ||
2560 (iptr->sx.val.i == 0x80000000))
2562 iptr->opc = ICMD_IREMPOW2;
2563 iptr->sx.val.i -= 1;
2564 goto icmd_iconst_tail;
2567 #if SUPPORT_CONST_LOGICAL
2569 iptr->opc = ICMD_IANDCONST;
2570 goto icmd_iconst_tail;
2573 iptr->opc = ICMD_IORCONST;
2574 goto icmd_iconst_tail;
2577 iptr->opc = ICMD_IXORCONST;
2578 goto icmd_iconst_tail;
2580 #endif /* SUPPORT_CONST_LOGICAL */
2582 iptr->opc = ICMD_ISHLCONST;
2583 goto icmd_iconst_tail;
2586 iptr->opc = ICMD_ISHRCONST;
2587 goto icmd_iconst_tail;
2590 iptr->opc = ICMD_IUSHRCONST;
2591 goto icmd_iconst_tail;
2592 #if SUPPORT_LONG_SHIFT
2594 iptr->opc = ICMD_LSHLCONST;
2595 goto icmd_lconst_tail;
2598 iptr->opc = ICMD_LSHRCONST;
2599 goto icmd_lconst_tail;
2602 iptr->opc = ICMD_LUSHRCONST;
2603 goto icmd_lconst_tail;
2604 #endif /* SUPPORT_LONG_SHIFT */
2605 case ICMD_IF_ICMPEQ:
2606 iptr[1].opc = ICMD_IFEQ;
2610 /* set the constant for the following icmd */
2611 iptr[1].sx.val.i = iptr->sx.val.i;
2613 /* this instruction becomes a nop */
2614 iptr->opc = ICMD_NOP;
2617 case ICMD_IF_ICMPLT:
2618 iptr[1].opc = ICMD_IFLT;
2619 goto icmd_if_icmp_tail;
2621 case ICMD_IF_ICMPLE:
2622 iptr[1].opc = ICMD_IFLE;
2623 goto icmd_if_icmp_tail;
2625 case ICMD_IF_ICMPNE:
2626 iptr[1].opc = ICMD_IFNE;
2627 goto icmd_if_icmp_tail;
2629 case ICMD_IF_ICMPGT:
2630 iptr[1].opc = ICMD_IFGT;
2631 goto icmd_if_icmp_tail;
2633 case ICMD_IF_ICMPGE:
2634 iptr[1].opc = ICMD_IFGE;
2635 goto icmd_if_icmp_tail;
2637 #if SUPPORT_CONST_STORE
2642 # if SUPPORT_CONST_STORE_ZERO_ONLY
2643 if (iptr->sx.val.i != 0)
2646 switch (iptr[1].opc) {
2648 iptr->opc = ICMD_IASTORECONST;
2649 iptr->flags.bits |= INS_FLAG_CHECK;
2652 iptr->opc = ICMD_BASTORECONST;
2653 iptr->flags.bits |= INS_FLAG_CHECK;
2656 iptr->opc = ICMD_CASTORECONST;
2657 iptr->flags.bits |= INS_FLAG_CHECK;
2660 iptr->opc = ICMD_SASTORECONST;
2661 iptr->flags.bits |= INS_FLAG_CHECK;
2665 iptr[1].opc = ICMD_NOP;
2667 /* copy the constant to s3 */
2668 /* XXX constval -> astoreconstval? */
2669 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2670 OP2_0(TYPE_ADR, TYPE_INT);
2671 COUNT(count_pcmd_op);
2674 case ICMD_PUTSTATIC:
2676 # if SUPPORT_CONST_STORE_ZERO_ONLY
2677 if (iptr->sx.val.i != 0)
2680 /* XXX check field type? */
2682 /* copy the constant to s2 */
2683 /* XXX constval -> fieldconstval? */
2684 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2687 /* set the field reference (s3) */
2688 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2689 iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2690 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2691 fmiref = iptr->sx.s23.s3.uf->fieldref;
2694 fmiref = iptr[1].sx.s23.s3.fmiref;
2695 iptr->sx.s23.s3.fmiref = fmiref;
2698 #if defined(ENABLE_VERIFIER)
2699 expectedtype = fmiref->parseddesc.fd->type;
2700 switch (iptr[0].opc) {
2702 if (expectedtype != TYPE_INT)
2703 goto throw_stack_type_error;
2706 if (expectedtype != TYPE_LNG)
2707 goto throw_stack_type_error;
2710 if (expectedtype != TYPE_ADR)
2711 goto throw_stack_type_error;
2716 #endif /* defined(ENABLE_VERIFIER) */
2718 switch (iptr[1].opc) {
2719 case ICMD_PUTSTATIC:
2720 iptr->opc = ICMD_PUTSTATICCONST;
2724 iptr->opc = ICMD_PUTFIELDCONST;
2729 iptr[1].opc = ICMD_NOP;
2730 COUNT(count_pcmd_op);
2732 #endif /* SUPPORT_CONST_STORE */
2738 /* if we get here, the ICONST has been optimized */
2742 /* normal case of an unoptimized ICONST */
2746 /************************** LCONST OPTIMIZATIONS **************************/
2749 COUNT(count_pcmd_load);
2753 /* switch depending on the following instruction */
2755 switch (iptr[1].opc) {
2756 #if SUPPORT_LONG_ADD
2758 iptr->opc = ICMD_LADDCONST;
2762 /* instruction of type LONG -> LONG */
2763 iptr[1].opc = ICMD_NOP;
2764 OP1_1(TYPE_LNG, TYPE_LNG);
2765 COUNT(count_pcmd_op);
2769 iptr->opc = ICMD_LSUBCONST;
2770 goto icmd_lconst_tail;
2772 #endif /* SUPPORT_LONG_ADD */
2773 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2775 iptr->opc = ICMD_LMULCONST;
2776 goto icmd_lconst_tail;
2777 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2778 # if SUPPORT_LONG_SHIFT
2780 if (iptr->sx.val.l == 0x00000002)
2782 else if (iptr->sx.val.l == 0x00000004)
2784 else if (iptr->sx.val.l == 0x00000008)
2786 else if (iptr->sx.val.l == 0x00000010)
2788 else if (iptr->sx.val.l == 0x00000020)
2790 else if (iptr->sx.val.l == 0x00000040)
2792 else if (iptr->sx.val.l == 0x00000080)
2794 else if (iptr->sx.val.l == 0x00000100)
2796 else if (iptr->sx.val.l == 0x00000200)
2798 else if (iptr->sx.val.l == 0x00000400)
2799 iptr->sx.val.i = 10;
2800 else if (iptr->sx.val.l == 0x00000800)
2801 iptr->sx.val.i = 11;
2802 else if (iptr->sx.val.l == 0x00001000)
2803 iptr->sx.val.i = 12;
2804 else if (iptr->sx.val.l == 0x00002000)
2805 iptr->sx.val.i = 13;
2806 else if (iptr->sx.val.l == 0x00004000)
2807 iptr->sx.val.i = 14;
2808 else if (iptr->sx.val.l == 0x00008000)
2809 iptr->sx.val.i = 15;
2810 else if (iptr->sx.val.l == 0x00010000)
2811 iptr->sx.val.i = 16;
2812 else if (iptr->sx.val.l == 0x00020000)
2813 iptr->sx.val.i = 17;
2814 else if (iptr->sx.val.l == 0x00040000)
2815 iptr->sx.val.i = 18;
2816 else if (iptr->sx.val.l == 0x00080000)
2817 iptr->sx.val.i = 19;
2818 else if (iptr->sx.val.l == 0x00100000)
2819 iptr->sx.val.i = 20;
2820 else if (iptr->sx.val.l == 0x00200000)
2821 iptr->sx.val.i = 21;
2822 else if (iptr->sx.val.l == 0x00400000)
2823 iptr->sx.val.i = 22;
2824 else if (iptr->sx.val.l == 0x00800000)
2825 iptr->sx.val.i = 23;
2826 else if (iptr->sx.val.l == 0x01000000)
2827 iptr->sx.val.i = 24;
2828 else if (iptr->sx.val.l == 0x02000000)
2829 iptr->sx.val.i = 25;
2830 else if (iptr->sx.val.l == 0x04000000)
2831 iptr->sx.val.i = 26;
2832 else if (iptr->sx.val.l == 0x08000000)
2833 iptr->sx.val.i = 27;
2834 else if (iptr->sx.val.l == 0x10000000)
2835 iptr->sx.val.i = 28;
2836 else if (iptr->sx.val.l == 0x20000000)
2837 iptr->sx.val.i = 29;
2838 else if (iptr->sx.val.l == 0x40000000)
2839 iptr->sx.val.i = 30;
2840 else if (iptr->sx.val.l == 0x80000000)
2841 iptr->sx.val.i = 31;
2845 iptr->opc = ICMD_LMULPOW2;
2846 goto icmd_lconst_tail;
2847 # endif /* SUPPORT_LONG_SHIFT */
2848 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2849 #if SUPPORT_LONG_DIV_POW2
2851 if (iptr->sx.val.l == 0x00000002)
2853 else if (iptr->sx.val.l == 0x00000004)
2855 else if (iptr->sx.val.l == 0x00000008)
2857 else if (iptr->sx.val.l == 0x00000010)
2859 else if (iptr->sx.val.l == 0x00000020)
2861 else if (iptr->sx.val.l == 0x00000040)
2863 else if (iptr->sx.val.l == 0x00000080)
2865 else if (iptr->sx.val.l == 0x00000100)
2867 else if (iptr->sx.val.l == 0x00000200)
2869 else if (iptr->sx.val.l == 0x00000400)
2870 iptr->sx.val.i = 10;
2871 else if (iptr->sx.val.l == 0x00000800)
2872 iptr->sx.val.i = 11;
2873 else if (iptr->sx.val.l == 0x00001000)
2874 iptr->sx.val.i = 12;
2875 else if (iptr->sx.val.l == 0x00002000)
2876 iptr->sx.val.i = 13;
2877 else if (iptr->sx.val.l == 0x00004000)
2878 iptr->sx.val.i = 14;
2879 else if (iptr->sx.val.l == 0x00008000)
2880 iptr->sx.val.i = 15;
2881 else if (iptr->sx.val.l == 0x00010000)
2882 iptr->sx.val.i = 16;
2883 else if (iptr->sx.val.l == 0x00020000)
2884 iptr->sx.val.i = 17;
2885 else if (iptr->sx.val.l == 0x00040000)
2886 iptr->sx.val.i = 18;
2887 else if (iptr->sx.val.l == 0x00080000)
2888 iptr->sx.val.i = 19;
2889 else if (iptr->sx.val.l == 0x00100000)
2890 iptr->sx.val.i = 20;
2891 else if (iptr->sx.val.l == 0x00200000)
2892 iptr->sx.val.i = 21;
2893 else if (iptr->sx.val.l == 0x00400000)
2894 iptr->sx.val.i = 22;
2895 else if (iptr->sx.val.l == 0x00800000)
2896 iptr->sx.val.i = 23;
2897 else if (iptr->sx.val.l == 0x01000000)
2898 iptr->sx.val.i = 24;
2899 else if (iptr->sx.val.l == 0x02000000)
2900 iptr->sx.val.i = 25;
2901 else if (iptr->sx.val.l == 0x04000000)
2902 iptr->sx.val.i = 26;
2903 else if (iptr->sx.val.l == 0x08000000)
2904 iptr->sx.val.i = 27;
2905 else if (iptr->sx.val.l == 0x10000000)
2906 iptr->sx.val.i = 28;
2907 else if (iptr->sx.val.l == 0x20000000)
2908 iptr->sx.val.i = 29;
2909 else if (iptr->sx.val.l == 0x40000000)
2910 iptr->sx.val.i = 30;
2911 else if (iptr->sx.val.l == 0x80000000)
2912 iptr->sx.val.i = 31;
2916 iptr->opc = ICMD_LDIVPOW2;
2917 goto icmd_lconst_tail;
2918 #endif /* SUPPORT_LONG_DIV_POW2 */
2920 #if SUPPORT_LONG_REM_POW2
2922 if ((iptr->sx.val.l == 0x00000002) ||
2923 (iptr->sx.val.l == 0x00000004) ||
2924 (iptr->sx.val.l == 0x00000008) ||
2925 (iptr->sx.val.l == 0x00000010) ||
2926 (iptr->sx.val.l == 0x00000020) ||
2927 (iptr->sx.val.l == 0x00000040) ||
2928 (iptr->sx.val.l == 0x00000080) ||
2929 (iptr->sx.val.l == 0x00000100) ||
2930 (iptr->sx.val.l == 0x00000200) ||
2931 (iptr->sx.val.l == 0x00000400) ||
2932 (iptr->sx.val.l == 0x00000800) ||
2933 (iptr->sx.val.l == 0x00001000) ||
2934 (iptr->sx.val.l == 0x00002000) ||
2935 (iptr->sx.val.l == 0x00004000) ||
2936 (iptr->sx.val.l == 0x00008000) ||
2937 (iptr->sx.val.l == 0x00010000) ||
2938 (iptr->sx.val.l == 0x00020000) ||
2939 (iptr->sx.val.l == 0x00040000) ||
2940 (iptr->sx.val.l == 0x00080000) ||
2941 (iptr->sx.val.l == 0x00100000) ||
2942 (iptr->sx.val.l == 0x00200000) ||
2943 (iptr->sx.val.l == 0x00400000) ||
2944 (iptr->sx.val.l == 0x00800000) ||
2945 (iptr->sx.val.l == 0x01000000) ||
2946 (iptr->sx.val.l == 0x02000000) ||
2947 (iptr->sx.val.l == 0x04000000) ||
2948 (iptr->sx.val.l == 0x08000000) ||
2949 (iptr->sx.val.l == 0x10000000) ||
2950 (iptr->sx.val.l == 0x20000000) ||
2951 (iptr->sx.val.l == 0x40000000) ||
2952 (iptr->sx.val.l == 0x80000000))
2954 iptr->opc = ICMD_LREMPOW2;
2955 iptr->sx.val.l -= 1;
2956 goto icmd_lconst_tail;
2959 #endif /* SUPPORT_LONG_REM_POW2 */
2961 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2964 iptr->opc = ICMD_LANDCONST;
2965 goto icmd_lconst_tail;
2968 iptr->opc = ICMD_LORCONST;
2969 goto icmd_lconst_tail;
2972 iptr->opc = ICMD_LXORCONST;
2973 goto icmd_lconst_tail;
2974 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2976 #if SUPPORT_LONG_CMP_CONST
2978 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2981 /* switch on the instruction after LCONST - LCMP */
2983 switch (iptr[2].opc) {
2985 iptr->opc = ICMD_IF_LEQ;
2988 icmd_lconst_lcmp_tail:
2989 /* convert LCONST, LCMP, IFXX to IF_LXX */
2990 iptr->dst.insindex = iptr[2].dst.insindex;
2991 iptr[1].opc = ICMD_NOP;
2992 iptr[2].opc = ICMD_NOP;
2994 OP1_BRANCH(TYPE_LNG);
2996 COUNT(count_pcmd_bra);
2997 COUNT(count_pcmd_op);
3001 iptr->opc = ICMD_IF_LNE;
3002 goto icmd_lconst_lcmp_tail;
3005 iptr->opc = ICMD_IF_LLT;
3006 goto icmd_lconst_lcmp_tail;
3009 iptr->opc = ICMD_IF_LGT;
3010 goto icmd_lconst_lcmp_tail;
3013 iptr->opc = ICMD_IF_LLE;
3014 goto icmd_lconst_lcmp_tail;
3017 iptr->opc = ICMD_IF_LGE;
3018 goto icmd_lconst_lcmp_tail;
3022 } /* end switch on opcode after LCONST - LCMP */
3024 #endif /* SUPPORT_LONG_CMP_CONST */
3026 #if SUPPORT_CONST_STORE
3028 # if SUPPORT_CONST_STORE_ZERO_ONLY
3029 if (iptr->sx.val.l != 0)
3032 #if SIZEOF_VOID_P == 4
3033 /* the constant must fit into a ptrint */
3034 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3037 /* move the constant to s3 */
3038 iptr->sx.s23.s3.constval = iptr->sx.val.l;
3040 iptr->opc = ICMD_LASTORECONST;
3041 iptr->flags.bits |= INS_FLAG_CHECK;
3042 OP2_0(TYPE_ADR, TYPE_INT);
3044 iptr[1].opc = ICMD_NOP;
3045 COUNT(count_pcmd_op);
3048 case ICMD_PUTSTATIC:
3050 # if SUPPORT_CONST_STORE_ZERO_ONLY
3051 if (iptr->sx.val.l != 0)
3054 #if SIZEOF_VOID_P == 4
3055 /* the constant must fit into a ptrint */
3056 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3059 /* XXX check field type? */
3061 /* copy the constant to s2 */
3062 /* XXX constval -> fieldconstval? */
3063 iptr->sx.s23.s2.constval = iptr->sx.val.l;
3067 #endif /* SUPPORT_CONST_STORE */
3071 } /* end switch opcode after LCONST */
3073 /* if we get here, the LCONST has been optimized */
3077 /* the normal case of an unoptimized LCONST */
3081 /************************ END OF LCONST OPTIMIZATIONS *********************/
3084 COUNT(count_pcmd_load);
3089 COUNT(count_pcmd_load);
3093 /************************** ACONST OPTIMIZATIONS **************************/
3096 coalescing_boundary = sd.new;
3097 COUNT(count_pcmd_load);
3098 #if SUPPORT_CONST_STORE
3099 /* We can only optimize if the ACONST is resolved
3100 * and there is an instruction after it. */
3102 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3105 switch (iptr[1].opc) {
3107 /* We can only optimize for NULL values
3108 * here because otherwise a checkcast is
3110 if (iptr->sx.val.anyptr != NULL)
3113 /* copy the constant (NULL) to s3 */
3114 iptr->sx.s23.s3.constval = 0;
3115 iptr->opc = ICMD_AASTORECONST;
3116 iptr->flags.bits |= INS_FLAG_CHECK;
3117 OP2_0(TYPE_ADR, TYPE_INT);
3119 iptr[1].opc = ICMD_NOP;
3120 COUNT(count_pcmd_op);
3123 case ICMD_PUTSTATIC:
3125 # if SUPPORT_CONST_STORE_ZERO_ONLY
3126 if (iptr->sx.val.anyptr != NULL)
3129 /* XXX check field type? */
3130 /* copy the constant to s2 */
3131 /* XXX constval -> fieldconstval? */
3132 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3140 /* if we get here the ACONST has been optimized */
3144 #endif /* SUPPORT_CONST_STORE */
3149 /* pop 0 push 1 load */
3156 COUNT(count_load_instruction);
3157 i = opcode - ICMD_ILOAD; /* type */
3159 j = iptr->s1.varindex =
3160 jd->local_map[iptr->s1.varindex * 5 + i];
3162 #if defined(ENABLE_VERIFIER)
3163 if (sd.var[j].type == TYPE_RET) {
3164 exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3169 #if defined(ENABLE_SSA)
3171 GET_NEW_VAR(sd, new_index, i);
3188 coalescing_boundary = sd.new;
3189 iptr->flags.bits |= INS_FLAG_CHECK;
3190 COUNT(count_check_null);
3191 COUNT(count_check_bound);
3192 COUNT(count_pcmd_mem);
3193 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3200 coalescing_boundary = sd.new;
3201 iptr->flags.bits |= INS_FLAG_CHECK;
3202 COUNT(count_check_null);
3203 COUNT(count_check_bound);
3204 COUNT(count_pcmd_mem);
3205 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3208 /* pop 0 push 0 iinc */
3211 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3212 #if defined(ENABLE_SSA)
3215 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3219 last_store_boundary[iptr->s1.varindex] = sd.new;
3222 jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3227 if ((copy->varkind == LOCALVAR) &&
3228 (copy->varnum == iptr->s1.varindex))
3230 assert(IS_LOCALVAR(copy));
3236 #if defined(ENABLE_SSA)
3240 iptr->dst.varindex = iptr->s1.varindex;
3243 /* pop 1 push 0 store */
3252 i = opcode - ICMD_ISTORE; /* type */
3253 javaindex = iptr->dst.varindex;
3254 j = iptr->dst.varindex =
3255 jd->local_map[javaindex * 5 + i];
3257 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3259 iptr->sx.s23.s3.javaindex = javaindex;
3261 if (curstack->type == TYPE_RET) {
3262 iptr->flags.bits |= INS_FLAG_RETADDR;
3263 iptr->sx.s23.s2.retaddrnr =
3264 UNUSED - (1 + sd.var[j].vv.retaddr->nr);
3265 sd.javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
3268 sd.javalocals[javaindex] = j;
3270 /* invalidate the following javalocal for 2-word types */
3272 if (IS_2_WORD_TYPE(i)) {
3273 sd.javalocals[javaindex+1] = UNUSED;
3274 iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3277 /* invalidate 2-word types if second half was overwritten */
3279 if (javaindex > 0 && (i = sd.javalocals[javaindex-1]) != UNUSED) {
3280 if (IS_2_WORD_TYPE(sd.var[i].type)) {
3281 sd.javalocals[javaindex-1] = UNUSED;
3282 iptr->flags.bits |= INS_FLAG_KILL_PREV;
3286 #if defined(ENABLE_STATISTICS)
3289 i = sd.new - curstack;
3291 count_store_length[20]++;
3293 count_store_length[i]++;
3296 count_store_depth[10]++;
3298 count_store_depth[i]++;
3302 #if defined(ENABLE_SSA)
3305 /* check for conflicts as described in Figure 5.2 */
3307 copy = curstack->prev;
3310 if ((copy->varkind == LOCALVAR) &&
3311 (copy->varnum == j))
3313 copy->varkind = TEMPVAR;
3314 assert(IS_LOCALVAR(copy));
3321 /* if the variable is already coalesced, don't bother */
3323 /* We do not need to check against INOUT, as invars */
3324 /* are always before the coalescing boundary. */
3326 if (curstack->varkind == LOCALVAR)
3329 /* there is no STORE Lj while curstack is live */
3331 if (curstack < last_store_boundary[javaindex])
3332 goto assume_conflict;
3334 /* curstack must be after the coalescing boundary */
3336 if (curstack < coalescing_boundary)
3337 goto assume_conflict;
3339 /* there is no DEF LOCALVAR(j) while curstack is live */
3341 copy = sd.new; /* most recent stackslot created + 1 */
3342 while (--copy > curstack) {
3343 if (copy->varkind == LOCALVAR && copy->varnum == j)
3344 goto assume_conflict;
3347 /* coalesce the temporary variable with Lj */
3348 assert((curstack->varkind == TEMPVAR)
3349 || (curstack->varkind == UNDEFVAR));
3350 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3351 assert(!IS_INOUT(curstack));
3352 assert(!IS_PREALLOC(curstack));
3354 assert(curstack->creator);
3355 assert(curstack->creator->dst.varindex == curstack->varnum);
3356 assert(!(curstack->flags & PASSTHROUGH));
3357 RELEASE_INDEX(sd, curstack);
3358 curstack->varkind = LOCALVAR;
3359 curstack->varnum = j;
3360 curstack->creator->dst.varindex = j;
3363 /* revert the coalescing, if it has been done earlier */
3365 if ((curstack->varkind == LOCALVAR)
3366 && (curstack->varnum == j))
3368 assert(IS_LOCALVAR(curstack));
3369 SET_TEMPVAR(curstack);
3372 /* remember the stack boundary at this store */
3374 last_store_boundary[javaindex] = sd.new;
3375 #if defined(ENABLE_SSA)
3376 } /* if (ls != NULL) */
3379 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3382 STORE(opcode - ICMD_ISTORE, j);
3388 coalescing_boundary = sd.new;
3389 iptr->flags.bits |= INS_FLAG_CHECK;
3390 COUNT(count_check_null);
3391 COUNT(count_check_bound);
3392 COUNT(count_pcmd_mem);
3394 bte = builtintable_get_internal(BUILTIN_canstore);
3397 if (md->memuse > rd->memuse)
3398 rd->memuse = md->memuse;
3399 if (md->argintreguse > rd->argintreguse)
3400 rd->argintreguse = md->argintreguse;
3401 /* XXX non-leaf method? */
3403 /* make all stack variables saved */
3407 sd.var[copy->varnum].flags |= SAVEDVAR;
3408 /* in case copy->varnum is/will be a LOCALVAR */
3409 /* once and set back to a non LOCALVAR */
3410 /* the correct SAVEDVAR flag has to be */
3411 /* remembered in copy->flags, too */
3412 copy->flags |= SAVEDVAR;
3416 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3423 coalescing_boundary = sd.new;
3424 iptr->flags.bits |= INS_FLAG_CHECK;
3425 COUNT(count_check_null);
3426 COUNT(count_check_bound);
3427 COUNT(count_pcmd_mem);
3428 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3435 coalescing_boundary = sd.new;
3436 iptr->flags.bits |= INS_FLAG_CHECK;
3437 COUNT(count_check_null);
3438 COUNT(count_check_bound);
3439 COUNT(count_pcmd_mem);
3440 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3446 #ifdef ENABLE_VERIFIER
3449 if (IS_2_WORD_TYPE(curstack->type))
3450 goto throw_stack_category_error;
3461 coalescing_boundary = sd.new;
3462 /* Assert here that no LOCAL or INOUTS get */
3463 /* preallocated, since tha macros are not */
3464 /* available in md-abi.c! */
3465 if (IS_TEMPVAR(curstack))
3466 md_return_alloc(jd, curstack);
3467 COUNT(count_pcmd_return);
3468 OP1_0(opcode - ICMD_IRETURN);
3469 superblockend = true;
3470 sd.jd->returncount++;
3471 sd.jd->returnblock = sd.bptr;
3475 coalescing_boundary = sd.new;
3476 COUNT(count_check_null);
3478 curstack = NULL; stackdepth = 0;
3479 superblockend = true;
3482 case ICMD_PUTSTATIC:
3483 coalescing_boundary = sd.new;
3484 COUNT(count_pcmd_mem);
3485 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3486 OP1_0(fmiref->parseddesc.fd->type);
3489 /* pop 1 push 0 branch */
3492 case ICMD_IFNONNULL:
3493 COUNT(count_pcmd_bra);
3494 OP1_BRANCH(TYPE_ADR);
3504 COUNT(count_pcmd_bra);
3505 /* iptr->sx.val.i is set implicitly in parse by
3506 clearing the memory or from IF_ICMPxx
3509 OP1_BRANCH(TYPE_INT);
3510 /* iptr->sx.val.i = 0; */
3514 /* pop 0 push 0 branch */
3517 COUNT(count_pcmd_bra);
3520 superblockend = true;
3523 /* pop 1 push 0 table branch */
3525 case ICMD_TABLESWITCH:
3526 COUNT(count_pcmd_table);
3527 OP1_BRANCH(TYPE_INT);
3529 table = iptr->dst.table;
3530 BRANCH_TARGET(*table, tbptr);
3533 i = iptr->sx.s23.s3.tablehigh
3534 - iptr->sx.s23.s2.tablelow + 1;
3537 BRANCH_TARGET(*table, tbptr);
3540 superblockend = true;
3543 /* pop 1 push 0 table branch */
3545 case ICMD_LOOKUPSWITCH:
3546 COUNT(count_pcmd_table);
3547 OP1_BRANCH(TYPE_INT);
3549 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3551 lookup = iptr->dst.lookup;
3553 i = iptr->sx.s23.s2.lookupcount;
3556 BRANCH_TARGET(lookup->target, tbptr);
3559 superblockend = true;
3562 case ICMD_MONITORENTER:
3563 case ICMD_MONITOREXIT:
3564 coalescing_boundary = sd.new;
3565 COUNT(count_check_null);
3569 /* pop 2 push 0 branch */
3571 case ICMD_IF_ICMPEQ:
3572 case ICMD_IF_ICMPNE:
3573 case ICMD_IF_ICMPLT:
3574 case ICMD_IF_ICMPGE:
3575 case ICMD_IF_ICMPGT:
3576 case ICMD_IF_ICMPLE:
3577 COUNT(count_pcmd_bra);
3578 OP2_BRANCH(TYPE_INT, TYPE_INT);
3582 case ICMD_IF_ACMPEQ:
3583 case ICMD_IF_ACMPNE:
3584 COUNT(count_pcmd_bra);
3585 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3592 coalescing_boundary = sd.new;
3593 COUNT(count_check_null);
3594 COUNT(count_pcmd_mem);
3595 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3596 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3601 if (!IS_2_WORD_TYPE(curstack->type)) {
3603 #ifdef ENABLE_VERIFIER
3606 if (IS_2_WORD_TYPE(curstack->prev->type))
3607 goto throw_stack_category_error;
3610 OP2_0_ANY_ANY; /* pop two slots */
3613 iptr->opc = ICMD_POP;
3614 OP1_0_ANY; /* pop one (two-word) slot */
3618 /* pop 0 push 1 dup */
3621 #ifdef ENABLE_VERIFIER
3624 if (IS_2_WORD_TYPE(curstack->type))
3625 goto throw_stack_category_error;
3628 COUNT(count_dup_instruction);
3634 coalescing_boundary = sd.new - 1;
3639 if (IS_2_WORD_TYPE(curstack->type)) {
3641 iptr->opc = ICMD_DUP;
3646 /* ..., ????, cat1 */
3647 #ifdef ENABLE_VERIFIER
3649 if (IS_2_WORD_TYPE(curstack->prev->type))
3650 goto throw_stack_category_error;
3653 src1 = curstack->prev;
3656 COPY_UP(src1); iptr++; len--;
3659 coalescing_boundary = sd.new;
3663 /* pop 2 push 3 dup */
3666 #ifdef ENABLE_VERIFIER
3669 if (IS_2_WORD_TYPE(curstack->type) ||
3670 IS_2_WORD_TYPE(curstack->prev->type))
3671 goto throw_stack_category_error;
3676 src1 = curstack->prev;
3681 /* move non-temporary sources out of the way */
3682 if (!IS_TEMPVAR(src2)) {
3683 MOVE_TO_TEMP(src2); iptr++; len--;
3686 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3688 MOVE_UP(src1); iptr++; len--;
3689 MOVE_UP(src2); iptr++; len--;
3691 COPY_DOWN(curstack, dst1);
3693 coalescing_boundary = sd.new;
3698 if (IS_2_WORD_TYPE(curstack->type)) {
3699 /* ..., ????, cat2 */
3700 #ifdef ENABLE_VERIFIER
3702 if (IS_2_WORD_TYPE(curstack->prev->type))
3703 goto throw_stack_category_error;
3706 iptr->opc = ICMD_DUP_X1;
3710 /* ..., ????, cat1 */
3711 #ifdef ENABLE_VERIFIER
3714 if (IS_2_WORD_TYPE(curstack->prev->type)
3715 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3716 goto throw_stack_category_error;
3721 src1 = curstack->prev->prev;
3722 src2 = curstack->prev;
3724 POPANY; POPANY; POPANY;
3727 /* move non-temporary sources out of the way */
3728 if (!IS_TEMPVAR(src2)) {
3729 MOVE_TO_TEMP(src2); iptr++; len--;
3731 if (!IS_TEMPVAR(src3)) {
3732 MOVE_TO_TEMP(src3); iptr++; len--;
3735 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3736 DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3738 MOVE_UP(src1); iptr++; len--;
3739 MOVE_UP(src2); iptr++; len--;
3740 MOVE_UP(src3); iptr++; len--;
3742 COPY_DOWN(curstack, dst2); iptr++; len--;
3743 COPY_DOWN(curstack->prev, dst1);
3745 coalescing_boundary = sd.new;
3749 /* pop 3 push 4 dup */
3753 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3754 /* ..., cat2, ???? */
3755 #ifdef ENABLE_VERIFIER
3757 if (IS_2_WORD_TYPE(curstack->type))
3758 goto throw_stack_category_error;
3761 iptr->opc = ICMD_DUP_X1;
3765 /* ..., cat1, ???? */
3766 #ifdef ENABLE_VERIFIER
3769 if (IS_2_WORD_TYPE(curstack->type)
3770 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3771 goto throw_stack_category_error;
3775 src1 = curstack->prev->prev;
3776 src2 = curstack->prev;
3778 POPANY; POPANY; POPANY;
3781 /* move non-temporary sources out of the way */
3782 if (!IS_TEMPVAR(src2)) {
3783 MOVE_TO_TEMP(src2); iptr++; len--;
3785 if (!IS_TEMPVAR(src3)) {
3786 MOVE_TO_TEMP(src3); iptr++; len--;
3789 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3791 MOVE_UP(src1); iptr++; len--;
3792 MOVE_UP(src2); iptr++; len--;
3793 MOVE_UP(src3); iptr++; len--;
3795 COPY_DOWN(curstack, dst1);
3797 coalescing_boundary = sd.new;
3803 if (IS_2_WORD_TYPE(curstack->type)) {
3804 /* ..., ????, cat2 */
3805 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3806 /* ..., cat2, cat2 */
3807 iptr->opc = ICMD_DUP_X1;
3811 /* ..., cat1, cat2 */
3812 #ifdef ENABLE_VERIFIER
3815 if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3816 goto throw_stack_category_error;
3819 iptr->opc = ICMD_DUP_X2;
3825 /* ..., ????, ????, cat1 */
3827 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3828 /* ..., cat2, ????, cat1 */
3829 #ifdef ENABLE_VERIFIER
3831 if (IS_2_WORD_TYPE(curstack->prev->type))
3832 goto throw_stack_category_error;
3835 iptr->opc = ICMD_DUP2_X1;
3839 /* ..., cat1, ????, cat1 */
3840 #ifdef ENABLE_VERIFIER
3843 if (IS_2_WORD_TYPE(curstack->prev->type)
3844 || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3845 goto throw_stack_category_error;
3849 src1 = curstack->prev->prev->prev;
3850 src2 = curstack->prev->prev;
3851 src3 = curstack->prev;
3853 POPANY; POPANY; POPANY; POPANY;
3856 /* move non-temporary sources out of the way */
3857 if (!IS_TEMPVAR(src2)) {
3858 MOVE_TO_TEMP(src2); iptr++; len--;
3860 if (!IS_TEMPVAR(src3)) {
3861 MOVE_TO_TEMP(src3); iptr++; len--;
3863 if (!IS_TEMPVAR(src4)) {
3864 MOVE_TO_TEMP(src4); iptr++; len--;
3867 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3868 DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3870 MOVE_UP(src1); iptr++; len--;
3871 MOVE_UP(src2); iptr++; len--;
3872 MOVE_UP(src3); iptr++; len--;
3873 MOVE_UP(src4); iptr++; len--;
3875 COPY_DOWN(curstack, dst2); iptr++; len--;
3876 COPY_DOWN(curstack->prev, dst1);
3878 coalescing_boundary = sd.new;
3882 /* pop 2 push 2 swap */
3885 #ifdef ENABLE_VERIFIER
3888 if (IS_2_WORD_TYPE(curstack->type)
3889 || IS_2_WORD_TYPE(curstack->prev->type))
3890 goto throw_stack_category_error;
3894 src1 = curstack->prev;
3899 /* move non-temporary sources out of the way */
3900 if (!IS_TEMPVAR(src1)) {
3901 MOVE_TO_TEMP(src1); iptr++; len--;
3904 MOVE_UP(src2); iptr++; len--;
3907 coalescing_boundary = sd.new;
3914 coalescing_boundary = sd.new;
3915 #if !SUPPORT_DIVISION
3916 bte = iptr->sx.s23.s3.bte;
3919 if (md->memuse > rd->memuse)
3920 rd->memuse = md->memuse;
3921 if (md->argintreguse > rd->argintreguse)
3922 rd->argintreguse = md->argintreguse;
3924 /* make all stack variables saved */
3928 sd.var[copy->varnum].flags |= SAVEDVAR;
3929 copy->flags |= SAVEDVAR;
3934 #endif /* !SUPPORT_DIVISION */
3945 COUNT(count_pcmd_op);
3946 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3951 coalescing_boundary = sd.new;
3952 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3953 bte = iptr->sx.s23.s3.bte;
3956 if (md->memuse > rd->memuse)
3957 rd->memuse = md->memuse;
3958 if (md->argintreguse > rd->argintreguse)
3959 rd->argintreguse = md->argintreguse;
3960 /* XXX non-leaf method? */
3962 /* make all stack variables saved */
3966 sd.var[copy->varnum].flags |= SAVEDVAR;
3967 copy->flags |= SAVEDVAR;
3972 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3977 #if SUPPORT_LONG_LOGICAL
3981 #endif /* SUPPORT_LONG_LOGICAL */
3982 COUNT(count_pcmd_op);
3983 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3989 COUNT(count_pcmd_op);
3990 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3998 COUNT(count_pcmd_op);
3999 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
4007 COUNT(count_pcmd_op);
4008 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
4012 COUNT(count_pcmd_op);
4013 #if SUPPORT_LONG_CMP_CONST
4014 if ((len == 0) || (iptr[1].sx.val.i != 0))
4017 switch (iptr[1].opc) {
4019 iptr->opc = ICMD_IF_LCMPEQ;
4021 iptr->dst.insindex = iptr[1].dst.insindex;
4022 iptr[1].opc = ICMD_NOP;
4024 OP2_BRANCH(TYPE_LNG, TYPE_LNG);
4027 COUNT(count_pcmd_bra);
4030 iptr->opc = ICMD_IF_LCMPNE;
4031 goto icmd_lcmp_if_tail;
4033 iptr->opc = ICMD_IF_LCMPLT;
4034 goto icmd_lcmp_if_tail;
4036 iptr->opc = ICMD_IF_LCMPGT;
4037 goto icmd_lcmp_if_tail;
4039 iptr->opc = ICMD_IF_LCMPLE;
4040 goto icmd_lcmp_if_tail;
4042 iptr->opc = ICMD_IF_LCMPGE;
4043 goto icmd_lcmp_if_tail;
4049 #endif /* SUPPORT_LONG_CMP_CONST */
4050 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
4053 /* XXX why is this deactivated? */
4056 COUNT(count_pcmd_op);
4057 if ((len == 0) || (iptr[1].sx.val.i != 0))
4060 switch (iptr[1].opc) {
4062 iptr->opc = ICMD_IF_FCMPEQ;
4064 iptr->dst.insindex = iptr[1].dst.insindex;
4065 iptr[1].opc = ICMD_NOP;
4067 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4070 COUNT(count_pcmd_bra);
4073 iptr->opc = ICMD_IF_FCMPNE;
4074 goto icmd_if_fcmpl_tail;
4076 iptr->opc = ICMD_IF_FCMPL_LT;
4077 goto icmd_if_fcmpl_tail;
4079 iptr->opc = ICMD_IF_FCMPL_GT;
4080 goto icmd_if_fcmpl_tail;
4082 iptr->opc = ICMD_IF_FCMPL_LE;
4083 goto icmd_if_fcmpl_tail;
4085 iptr->opc = ICMD_IF_FCMPL_GE;
4086 goto icmd_if_fcmpl_tail;
4093 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4097 COUNT(count_pcmd_op);
4098 if ((len == 0) || (iptr[1].sx.val.i != 0))
4101 switch (iptr[1].opc) {
4103 iptr->opc = ICMD_IF_FCMPEQ;
4105 iptr->dst.insindex = iptr[1].dst.insindex;
4106 iptr[1].opc = ICMD_NOP;
4108 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4111 COUNT(count_pcmd_bra);
4114 iptr->opc = ICMD_IF_FCMPNE;
4115 goto icmd_if_fcmpg_tail;
4117 iptr->opc = ICMD_IF_FCMPG_LT;
4118 goto icmd_if_fcmpg_tail;
4120 iptr->opc = ICMD_IF_FCMPG_GT;
4121 goto icmd_if_fcmpg_tail;
4123 iptr->opc = ICMD_IF_FCMPG_LE;
4124 goto icmd_if_fcmpg_tail;
4126 iptr->opc = ICMD_IF_FCMPG_GE;
4127 goto icmd_if_fcmpg_tail;
4134 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4138 COUNT(count_pcmd_op);
4139 if ((len == 0) || (iptr[1].sx.val.i != 0))
4142 switch (iptr[1].opc) {
4144 iptr->opc = ICMD_IF_DCMPEQ;
4146 iptr->dst.insindex = iptr[1].dst.insindex;
4147 iptr[1].opc = ICMD_NOP;
4149 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4152 COUNT(count_pcmd_bra);
4155 iptr->opc = ICMD_IF_DCMPNE;
4156 goto icmd_if_dcmpl_tail;
4158 iptr->opc = ICMD_IF_DCMPL_LT;
4159 goto icmd_if_dcmpl_tail;
4161 iptr->opc = ICMD_IF_DCMPL_GT;
4162 goto icmd_if_dcmpl_tail;
4164 iptr->opc = ICMD_IF_DCMPL_LE;
4165 goto icmd_if_dcmpl_tail;
4167 iptr->opc = ICMD_IF_DCMPL_GE;
4168 goto icmd_if_dcmpl_tail;
4175 OPTT2_1(TYPE_DBL, TYPE_INT);
4179 COUNT(count_pcmd_op);
4180 if ((len == 0) || (iptr[1].sx.val.i != 0))
4183 switch (iptr[1].opc) {
4185 iptr->opc = ICMD_IF_DCMPEQ;
4187 iptr->dst.insindex = iptr[1].dst.insindex;
4188 iptr[1].opc = ICMD_NOP;
4190 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4193 COUNT(count_pcmd_bra);
4196 iptr->opc = ICMD_IF_DCMPNE;
4197 goto icmd_if_dcmpg_tail;
4199 iptr->opc = ICMD_IF_DCMPG_LT;
4200 goto icmd_if_dcmpg_tail;
4202 iptr->opc = ICMD_IF_DCMPG_GT;
4203 goto icmd_if_dcmpg_tail;
4205 iptr->opc = ICMD_IF_DCMPG_LE;
4206 goto icmd_if_dcmpg_tail;
4208 iptr->opc = ICMD_IF_DCMPG_GE;
4209 goto icmd_if_dcmpg_tail;
4216 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4221 COUNT(count_pcmd_op);
4222 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4227 COUNT(count_pcmd_op);
4228 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4237 case ICMD_INT2SHORT:
4238 COUNT(count_pcmd_op);
4239 OP1_1(TYPE_INT, TYPE_INT);
4242 COUNT(count_pcmd_op);
4243 OP1_1(TYPE_LNG, TYPE_LNG);
4246 COUNT(count_pcmd_op);
4247 OP1_1(TYPE_FLT, TYPE_FLT);
4250 COUNT(count_pcmd_op);
4251 OP1_1(TYPE_DBL, TYPE_DBL);
4255 COUNT(count_pcmd_op);
4256 OP1_1(TYPE_INT, TYPE_LNG);
4259 COUNT(count_pcmd_op);
4260 OP1_1(TYPE_INT, TYPE_FLT);
4263 COUNT(count_pcmd_op);
4264 OP1_1(TYPE_INT, TYPE_DBL);
4267 COUNT(count_pcmd_op);
4268 OP1_1(TYPE_LNG, TYPE_INT);
4271 COUNT(count_pcmd_op);
4272 OP1_1(TYPE_LNG, TYPE_FLT);
4275 COUNT(count_pcmd_op);
4276 OP1_1(TYPE_LNG, TYPE_DBL);
4279 COUNT(count_pcmd_op);
4280 OP1_1(TYPE_FLT, TYPE_INT);
4283 COUNT(count_pcmd_op);
4284 OP1_1(TYPE_FLT, TYPE_LNG);
4287 COUNT(count_pcmd_op);
4288 OP1_1(TYPE_FLT, TYPE_DBL);
4291 COUNT(count_pcmd_op);
4292 OP1_1(TYPE_DBL, TYPE_INT);
4295 COUNT(count_pcmd_op);
4296 OP1_1(TYPE_DBL, TYPE_LNG);
4299 COUNT(count_pcmd_op);
4300 OP1_1(TYPE_DBL, TYPE_FLT);
4303 case ICMD_CHECKCAST:
4304 coalescing_boundary = sd.new;
4305 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4306 /* array type cast-check */
4308 bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4311 if (md->memuse > rd->memuse)
4312 rd->memuse = md->memuse;
4313 if (md->argintreguse > rd->argintreguse)
4314 rd->argintreguse = md->argintreguse;
4316 /* make all stack variables saved */
4320 sd.var[copy->varnum].flags |= SAVEDVAR;
4321 copy->flags |= SAVEDVAR;
4325 OP1_1(TYPE_ADR, TYPE_ADR);
4328 case ICMD_INSTANCEOF:
4329 case ICMD_ARRAYLENGTH:
4330 coalescing_boundary = sd.new;
4331 OP1_1(TYPE_ADR, TYPE_INT);
4335 case ICMD_ANEWARRAY:
4336 coalescing_boundary = sd.new;
4337 OP1_1(TYPE_INT, TYPE_ADR);
4341 coalescing_boundary = sd.new;
4342 COUNT(count_check_null);
4343 COUNT(count_pcmd_mem);
4344 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4345 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4350 case ICMD_GETSTATIC:
4351 coalescing_boundary = sd.new;
4352 COUNT(count_pcmd_mem);
4353 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4354 OP0_1(fmiref->parseddesc.fd->type);
4358 coalescing_boundary = sd.new;
4365 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
4366 tbptr->type = BBTYPE_SBR;
4368 assert(sd.bptr->next); /* XXX exception */
4369 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4370 #if defined(ENABLE_VERIFIER)
4371 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4374 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4378 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4380 /* We need to check for overflow right here because
4381 * the pushed value is poped afterwards */
4384 superblockend = true;
4385 /* XXX should not be marked as interface, as it does not need to be */
4386 /* allocated. Same for the invar of the target. */
4389 /* pop many push any */
4393 bte = iptr->sx.s23.s3.bte;
4397 case ICMD_INVOKESTATIC:
4398 case ICMD_INVOKESPECIAL:
4399 case ICMD_INVOKEVIRTUAL:
4400 case ICMD_INVOKEINTERFACE:
4401 COUNT(count_pcmd_met);
4403 /* Check for functions to replace with builtin
4406 if (builtintable_replace_function(iptr))
4409 INSTRUCTION_GET_METHODDESC(iptr, md);
4410 /* XXX resurrect this COUNT? */
4411 /* if (lm->flags & ACC_STATIC) */
4412 /* {COUNT(count_check_null);} */
4416 coalescing_boundary = sd.new;
4420 if (md->memuse > rd->memuse)
4421 rd->memuse = md->memuse;
4422 if (md->argintreguse > rd->argintreguse)
4423 rd->argintreguse = md->argintreguse;
4424 if (md->argfltreguse > rd->argfltreguse)
4425 rd->argfltreguse = md->argfltreguse;
4429 iptr->s1.argcount = stackdepth;
4430 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4433 for (i-- ; i >= 0; i--) {
4434 iptr->sx.s23.s2.args[i] = copy->varnum;
4436 /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4437 /* -> won't help anyway */
4438 if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4440 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4441 /* If we pass float arguments in integer argument registers, we
4442 * are not allowed to precolor them here. Floats have to be moved
4443 * to this regs explicitly in codegen().
4444 * Only arguments that are passed by stack anyway can be precolored
4445 * (michi 2005/07/24) */
4446 if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4447 (!IS_FLT_DBL_TYPE(copy->type)
4448 || md->params[i].inmemory)) {
4450 if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4455 if (md->params[i].inmemory) {
4456 sd.var[copy->varnum].vv.regoff =
4457 md->params[i].regoff;
4458 sd.var[copy->varnum].flags |=
4462 if (IS_FLT_DBL_TYPE(copy->type)) {
4463 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4464 assert(0); /* XXX is this assert ok? */
4466 sd.var[copy->varnum].vv.regoff =
4467 rd->argfltregs[md->params[i].regoff];
4468 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4471 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4472 if (IS_2_WORD_TYPE(copy->type))
4473 sd.var[copy->varnum].vv.regoff =
4474 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4475 rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4478 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4479 sd.var[copy->varnum].vv.regoff =
4480 rd->argintregs[md->params[i].regoff];
4488 /* deal with live-through stack slots "under" the */
4494 iptr->sx.s23.s2.args[i++] = copy->varnum;
4495 sd.var[copy->varnum].flags |= SAVEDVAR;
4496 copy->flags |= SAVEDVAR | PASSTHROUGH;
4500 /* pop the arguments */
4509 /* push the return value */
4511 if (md->returntype.type != TYPE_VOID) {
4512 GET_NEW_VAR(sd, new_index, md->returntype.type);
4513 DST(md->returntype.type, new_index);
4518 case ICMD_MULTIANEWARRAY:
4519 coalescing_boundary = sd.new;
4520 if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4521 rd->argintreguse = MIN(3, INT_ARG_CNT);
4523 i = iptr->s1.argcount;
4527 iptr->sx.s23.s2.args = DMNEW(s4, i);
4529 #if defined(SPECIALMEMUSE)
4530 # if defined(__DARWIN__)
4531 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4532 rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4534 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4535 rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4538 # if defined(__I386__)
4539 if (rd->memuse < i + 3)
4540 rd->memuse = i + 3; /* n integer args spilled on stack */
4541 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4542 if (rd->memuse < i + 2)
4543 rd->memuse = i + 2; /* 4*4 bytes callee save space */
4546 rd->memuse = i; /* n integer args spilled on stack */
4547 # endif /* defined(__I386__) */
4551 /* check INT type here? Currently typecheck does this. */
4552 iptr->sx.s23.s2.args[i] = copy->varnum;
4553 if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4554 && (!IS_INOUT(copy))
4555 && (!IS_LOCALVAR(copy)) ) {
4556 copy->varkind = ARGVAR;
4557 sd.var[copy->varnum].flags |=
4558 INMEMORY & PREALLOC;
4559 #if defined(SPECIALMEMUSE)
4560 # if defined(__DARWIN__)
4561 sd.var[copy->varnum].vv.regoff = i +
4562 LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4564 sd.var[copy->varnum].vv.regoff = i +
4565 LA_SIZE_IN_POINTERS + 3;
4568 # if defined(__I386__)
4569 sd.var[copy->varnum].vv.regoff = i + 3;
4570 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4571 sd.var[copy->varnum].vv.regoff = i + 2;
4573 sd.var[copy->varnum].vv.regoff = i;
4574 # endif /* defined(__I386__) */
4575 #endif /* defined(SPECIALMEMUSE) */
4580 sd.var[copy->varnum].flags |= SAVEDVAR;
4581 copy->flags |= SAVEDVAR;
4585 i = iptr->s1.argcount;
4590 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4591 DST(TYPE_ADR, new_index);
4596 exceptions_throw_internalerror("Unknown ICMD %d during stack analysis",
4603 } /* while instructions */
4605 /* show state after last instruction */
4607 #if defined(STACK_VERBOSE)
4608 stack_verbose_show_state(&sd, NULL, curstack);
4611 /* stack slots at basic block end become interfaces */
4613 sd.bptr->outdepth = stackdepth;
4614 sd.bptr->outvars = DMNEW(s4, stackdepth);
4617 for (copy = curstack; copy; i--, copy = copy->prev) {
4621 /* with the new vars rd->interfaces will be removed */
4622 /* and all in and outvars have to be STACKVARS! */
4623 /* in the moment i.e. SWAP with in and out vars can */
4624 /* create an unresolvable conflict */
4629 v = sd.var + copy->varnum;
4632 /* do not allocate variables for returnAddresses */
4634 if (t != TYPE_RET) {
4635 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4636 /* no interface var until now for this depth and */
4638 jd->interface_map[i*5 + t].flags = v->flags;
4641 jd->interface_map[i*5 + t].flags |= v->flags;
4645 sd.bptr->outvars[i] = copy->varnum;
4648 /* check if interface slots at basic block begin must be saved */
4650 for (i=0; i<sd.bptr->indepth; ++i) {
4651 varinfo *v = sd.var + sd.bptr->invars[i];
4656 if (t != TYPE_RET) {
4657 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4658 /* no interface var until now for this depth and */
4660 jd->interface_map[i*5 + t].flags = v->flags;
4663 jd->interface_map[i*5 + t].flags |= v->flags;
4668 /* store the number of this block's variables */
4670 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4672 #if defined(STACK_VERBOSE)
4673 stack_verbose_block_exit(&sd, superblockend);
4676 /* reach the following block, if any */
4679 if (!stack_reach_next_block(&sd))
4684 } while (sd.repeat && !deadcode);
4686 /* reset locals of TYPE_RET|VOID to TYPE_ADR */
4688 /* A local variable may be used as both a returnAddress and a reference */
4689 /* type variable, as we do not split variables between these types when */
4690 /* renaming locals. While returnAddresses have been eliminated now, we */
4691 /* must assume that the variable is still used as TYPE_ADR. */
4692 /* The only way that a local can be TYPE_VOID at this point, is that it */
4693 /* was a TYPE_RET variable for which incompatible returnAddresses were */
4694 /* merged. Thus we must treat TYPE_VOID in the same way as TYPE_RET */
4696 /* XXX: It would be nice to remove otherwise unused returnAddress */
4697 /* variables from the local variable array, so they are not */
4698 /* allocated by simplereg. (For LSRA this is not needed). */
4700 for (i=0; i<sd.localcount; ++i) {
4701 if (sd.var[i].type == TYPE_RET || sd.var[i].type == TYPE_VOID)
4702 sd.var[i].type = TYPE_ADR;
4705 /* mark temporaries of TYPE_RET as PREALLOC to avoid allocation */
4707 for (i=sd.localcount; i<sd.vartop; ++i) {
4708 if (sd.var[i].type == TYPE_RET)
4709 sd.var[i].flags |= PREALLOC;
4712 /* XXX hack to fix up the ranges of the cloned single-block handlers */
4714 ex = jd->exceptiontable;
4715 for (; ex != NULL; ex = ex->down) {
4716 if (ex->start == ex->end) {
4717 assert(ex->end->next);
4718 ex->end = ex->end->next;
4722 /* store number of created variables */
4724 jd->vartop = sd.vartop;
4726 /* gather statistics *****************************************************/
4728 #if defined(ENABLE_STATISTICS)
4730 if (jd->basicblockcount > count_max_basic_blocks)
4731 count_max_basic_blocks = jd->basicblockcount;
4732 count_basic_blocks += jd->basicblockcount;
4733 if (jd->instructioncount > count_max_javainstr)
4734 count_max_javainstr = jd->instructioncount;
4735 count_javainstr += jd->instructioncount;
4736 if (jd->stackcount > count_upper_bound_new_stack)
4737 count_upper_bound_new_stack = jd->stackcount;
4738 if ((sd.new - jd->stack) > count_max_new_stack)
4739 count_max_new_stack = (sd.new - jd->stack);
4741 sd.bptr = jd->basicblocks;
4742 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4743 if (sd.bptr->flags > BBREACHED) {
4744 if (sd.bptr->indepth >= 10)
4745 count_block_stack[10]++;
4747 count_block_stack[sd.bptr->indepth]++;
4748 len = sd.bptr->icount;
4750 count_block_size_distribution[len]++;
4752 count_block_size_distribution[10]++;
4754 count_block_size_distribution[11]++;
4756 count_block_size_distribution[12]++;
4758 count_block_size_distribution[13]++;
4760 count_block_size_distribution[14]++;
4762 count_block_size_distribution[15]++;
4764 count_block_size_distribution[16]++;
4766 count_block_size_distribution[17]++;
4770 if (iteration_count == 1)
4771 count_analyse_iterations[0]++;
4772 else if (iteration_count == 2)
4773 count_analyse_iterations[1]++;
4774 else if (iteration_count == 3)
4775 count_analyse_iterations[2]++;
4776 else if (iteration_count == 4)
4777 count_analyse_iterations[3]++;
4779 count_analyse_iterations[4]++;
4781 if (jd->basicblockcount <= 5)
4782 count_method_bb_distribution[0]++;
4783 else if (jd->basicblockcount <= 10)
4784 count_method_bb_distribution[1]++;
4785 else if (jd->basicblockcount <= 15)
4786 count_method_bb_distribution[2]++;
4787 else if (jd->basicblockcount <= 20)
4788 count_method_bb_distribution[3]++;
4789 else if (jd->basicblockcount <= 30)
4790 count_method_bb_distribution[4]++;
4791 else if (jd->basicblockcount <= 40)
4792 count_method_bb_distribution[5]++;
4793 else if (jd->basicblockcount <= 50)
4794 count_method_bb_distribution[6]++;
4795 else if (jd->basicblockcount <= 75)
4796 count_method_bb_distribution[7]++;
4798 count_method_bb_distribution[8]++;
4800 #endif /* defined(ENABLE_STATISTICS) */
4802 /* everything's ok *******************************************************/
4806 /* goto labels for throwing verifier exceptions **************************/
4808 #if defined(ENABLE_VERIFIER)
4810 throw_stack_underflow:
4811 exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4814 throw_stack_overflow:
4815 exceptions_throw_verifyerror(m, "Stack size too large");
4818 throw_stack_type_error:
4819 exceptions_throw_verifyerror_for_stack(m, expectedtype);
4822 throw_stack_category_error:
4823 exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4830 /* stack_javalocals_store ******************************************************
4832 Model the effect of a ?STORE instruction upon the given javalocals array.
4835 iptr.............the ?STORE instruction
4836 javalocals.......the javalocals array to modify
4838 *******************************************************************************/
4840 void stack_javalocals_store(instruction *iptr, s4 *javalocals)
4842 s4 idx; /* index into the jd->var array */
4843 s4 j; /* java local index */
4845 idx = iptr->dst.varindex;
4846 j = iptr->sx.s23.s3.javaindex;
4849 if (iptr->flags.bits & INS_FLAG_RETADDR)
4850 javalocals[j] = iptr->sx.s23.s2.retaddrnr;
4852 javalocals[j] = idx;
4854 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
4855 javalocals[j-1] = UNUSED;
4857 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
4858 javalocals[j+1] = UNUSED;
4863 /* functions for verbose stack analysis output ********************************/
4865 #if defined(STACK_VERBOSE)
4866 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4868 printf("%c", show_jit_type_letters[v->type]);
4869 if (v->type == TYPE_RET) {
4870 printf("{L%03d}", v->vv.retaddr->nr);
4871 #if defined(ENABLE_VERIFIER)
4872 printf("{start=L%03d}", ((basicblock *)v->SBRSTART)->nr);
4878 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4880 assert(index >= 0 && index < sd->vartop);
4881 stack_verbose_show_varinfo(sd, sd->var + index);
4885 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4889 printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4891 for (i=0; i<bptr->indepth; ++i) {
4894 stack_verbose_show_variable(sd, bptr->invars[i]);
4899 printf("] javalocals ");
4900 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4901 printf(" inlocals [");
4902 if (bptr->inlocals) {
4903 for (i=0; i<sd->localcount; ++i) {
4906 stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4911 printf("] out:%d [", bptr->outdepth);
4912 if (bptr->outvars) {
4913 for (i=0; i<bptr->outdepth; ++i) {
4916 stack_verbose_show_variable(sd, bptr->outvars[i]);
4924 printf(" (clone of L%03d)", bptr->original->nr);
4926 basicblock *b = bptr->copied_to;
4928 printf(" (copied to ");
4929 for (; b; b = b->copied_to)
4930 printf("L%03d ", b->nr);
4937 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4941 printf("======================================== STACK %sANALYSE BLOCK ",
4942 (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4943 stack_verbose_show_block(sd, sd->bptr);
4946 if (sd->handlers[0]) {
4947 printf("HANDLERS: ");
4948 for (i=0; sd->handlers[i]; ++i) {
4949 printf("L%03d ", sd->handlers[i]->handler->nr);
4957 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4959 printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4960 stack_verbose_show_block(sd, sd->bptr);
4964 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackptr curstack)
4972 printf(" javalocals ");
4973 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4976 for(i = 0, sp = curstack; sp; sp = sp->prev)
4980 stack = MNEW(stackptr, depth);
4981 for(sp = curstack; sp; sp = sp->prev)
4984 for(i=0; i<depth; ++i) {
4988 v = &(sd->var[sp->varnum]);
4990 if (v->flags & INOUT)
4992 if (v->flags & PREALLOC)
4994 printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
4995 if (v->type == TYPE_RET) {
4996 printf("(L%03d)", v->vv.retaddr->nr);
5001 show_icmd(sd->jd, iptr, false, SHOW_PARSE);
5008 * These are local overrides for various environment variables in Emacs.
5009 * Please do not remove this and leave it at the end of the file, where
5010 * Emacs will automagically detect them.
5011 * ---------------------------------------------------------------------
5014 * indent-tabs-mode: t
5018 * vim:noexpandtab:sw=4:ts=4: