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 7420 2007-02-28 22:30:31Z edwin $
40 #include "mm/memory.h"
42 #include "native/native.h"
44 #include "toolbox/logging.h"
46 #include "vm/global.h"
47 #include "vm/builtin.h"
48 #include "vm/stringlocal.h"
51 #include "vm/jit/abi.h"
52 #include "vm/jit/cfg.h"
53 #include "vm/jit/codegen-common.h"
54 #include "vm/jit/parse.h"
55 #include "vm/jit/show.h"
57 #if defined(ENABLE_DISASSEMBLER)
58 # include "vm/jit/disass.h"
61 #include "vm/jit/jit.h"
62 #include "vm/jit/stack.h"
64 #if defined(ENABLE_SSA)
65 # include "vm/jit/optimizing/lsra.h"
66 # include "vm/jit/optimizing/ssa.h"
67 #elif defined(ENABLE_LSRA)
68 # include "vm/jit/allocator/lsra.h"
71 #include "vmcore/options.h"
72 #include "vmcore/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 dv->type = TYPE_VOID;
718 if (b->flags >= BBFINISHED)
719 b->flags = BBTYPECHECK_REACHED;
720 sd->repeat = true; /* This is very rare, so just repeat */
724 #endif /* defined(ENABLE_VERIFIER) */
728 /* stack_create_invars *********************************************************
730 Create the invars for the given basic block. Also make a copy of the locals.
733 sd...........stack analysis data
734 b............block to create the invars for
735 curstack.....current stack top
736 stackdepth...current stack depth
738 This function creates STACKDEPTH invars and sets their types to the
739 types to the types of the corresponding slot in the current stack.
741 *******************************************************************************/
743 static void stack_create_invars(stackdata_t *sd, basicblock *b,
744 stackptr curstack, int stackdepth)
752 assert(sd->vartop + stackdepth <= sd->varcount);
754 b->indepth = stackdepth;
755 b->invars = DMNEW(s4, stackdepth);
757 /* allocate the variable indices */
758 index = (sd->vartop += stackdepth);
761 for (sp = curstack; i--; sp = sp->prev) {
762 b->invars[i] = --index;
763 dv = sd->var + index;
764 sv = sd->var + sp->varnum;
766 COPY_VAL_AND_TYPE_VAR(sv, dv);
769 stack_create_locals(sd, b);
773 /* stack_create_invars_from_outvars ********************************************
775 Create the invars for the given basic block. Also make a copy of the locals.
776 Types are propagated from the outvars of the current block.
779 sd...........stack analysis data
780 b............block to create the invars for
782 *******************************************************************************/
784 static void stack_create_invars_from_outvars(stackdata_t *sd, basicblock *b)
790 n = sd->bptr->outdepth;
791 assert(sd->vartop + n <= sd->varcount);
794 b->invars = DMNEW(s4, n);
797 dv = sd->var + sd->vartop;
799 /* allocate the invars */
801 for (i=0; i<n; ++i, ++dv) {
802 sv = sd->var + sd->bptr->outvars[i];
803 b->invars[i] = sd->vartop++;
805 COPY_VAL_AND_TYPE_VAR(sv, dv);
809 stack_create_locals(sd, b);
813 /* stack_check_invars **********************************************************
815 Check the current stack against the invars of the given basic block.
816 Depth and types must match.
819 sd...........stack analysis data
820 b............block which invars to check against
821 curstack.....current stack top
822 stackdepth...current stack depth
826 NULL.........a VerifyError has been thrown
828 *******************************************************************************/
830 static basicblock * stack_check_invars(stackdata_t *sd, basicblock *b,
831 stackptr curstack, int stackdepth)
840 #if defined(STACK_VERBOSE)
841 printf("stack_check_invars(L%03d)\n", b->nr);
844 /* find original of b */
849 #if defined(STACK_VERBOSE)
850 printf("original is L%03d\n", orig->nr);
855 #if defined(ENABLE_VERIFIER)
856 if (i != stackdepth) {
857 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
865 #if defined(STACK_VERBOSE)
866 printf("checking against ");
867 stack_verbose_show_block(sd, b); printf("\n");
871 for (i = orig->indepth; i--; sp = sp->prev) {
872 dv = sd->var + b->invars[i];
873 sv = sd->var + sp->varnum;
875 #if defined(ENABLE_VERIFIER)
876 if (dv->type != sp->type) {
877 exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
882 if (sp->type == TYPE_RET) {
883 #if defined(ENABLE_VERIFIER)
884 if (dv->SBRSTART != sv->SBRSTART) {
885 exceptions_throw_verifyerror(sd->m, "Mismatched stack types");
889 if (dv->vv.retaddr != sv->vv.retaddr) {
891 /* don't break! have to check the remaining stackslots */
897 for (i=0; i<sd->localcount; ++i) {
898 dv = b->inlocals + i;
900 if (sv->type == TYPE_RET && dv->type == TYPE_RET) {
902 #if defined(ENABLE_VERIFIER)
903 (sv->SBRSTART == dv->SBRSTART) &&
905 (sv->vv.retaddr != dv->vv.retaddr))
915 /* XXX cascading collapse? */
917 stack_merge_locals(sd, b);
919 #if defined(STACK_VERBOSE)
920 printf("------> using L%03d\n", b->nr);
924 } while ((b = b->copied_to) != NULL);
926 b = stack_clone_block(sd, orig);
930 stack_create_invars(sd, b, curstack, stackdepth);
935 /* stack_check_invars_from_outvars *********************************************
937 Check the outvars of the current block against the invars of the given block.
938 Depth and types must match.
941 sd...........stack analysis data
942 b............block which invars to check against
946 NULL.........a VerifyError has been thrown
948 *******************************************************************************/
950 static basicblock * stack_check_invars_from_outvars(stackdata_t *sd, basicblock *b)
958 #if defined(STACK_VERBOSE)
959 printf("stack_check_invars_from_outvars(L%03d)\n", b->nr);
962 /* find original of b */
967 #if defined(STACK_VERBOSE)
968 printf("original is L%03d\n", orig->nr);
972 n = sd->bptr->outdepth;
974 #if defined(ENABLE_VERIFIER)
976 exceptions_throw_verifyerror(sd->m, "Stack depth mismatch");
984 #if defined(STACK_VERBOSE)
985 printf("checking against ");
986 stack_verbose_show_block(sd, b); printf("\n");
990 dv = sd->var + b->invars[0];
992 for (i=0; i<n; ++i, ++dv) {
993 sv = sd->var + sd->bptr->outvars[i];
995 #if defined(ENABLE_VERIFIER)
996 if (sv->type != dv->type) {
997 exceptions_throw_verifyerror_for_stack(sd->m, dv->type);
1002 if (dv->type == TYPE_RET) {
1003 #if defined(ENABLE_VERIFIER)
1004 if (sv->SBRSTART != dv->SBRSTART) {
1005 exceptions_throw_verifyerror(sd->m, "Mismatched stack types");
1009 if (sv->vv.retaddr != dv->vv.retaddr) {
1011 /* don't break! have to check the remaining stackslots */
1018 for (i=0; i<sd->localcount; ++i) {
1019 dv = b->inlocals + i;
1022 #if defined(ENABLE_VERIFIER)
1023 (sv->SBRSTART == dv->SBRSTART) &&
1025 (sv->type == TYPE_RET && dv->type == TYPE_RET))
1027 if (sv->vv.retaddr != dv->vv.retaddr) {
1036 /* XXX cascading collapse? */
1038 stack_merge_locals(sd, b);
1040 #if defined(STACK_VERBOSE)
1041 printf("------> using L%03d\n", b->nr);
1045 } while ((b = b->copied_to) != NULL);
1047 b = stack_clone_block(sd, orig);
1051 stack_create_invars_from_outvars(sd, b);
1056 /* stack_create_instack ********************************************************
1058 Create the instack of the current basic block.
1061 sd...........stack analysis data
1064 the current stack top at the start of the basic block.
1066 *******************************************************************************/
1068 static stackptr stack_create_instack(stackdata_t *sd)
1074 if ((depth = sd->bptr->indepth) == 0)
1077 sp = (sd->new += depth);
1081 index = sd->bptr->invars[depth];
1083 sp->type = sd->var[index].type;
1087 sp->varkind = STACKVAR;
1091 /* return the top of the created stack */
1096 /* stack_mark_reached **********************************************************
1098 Mark the given block reached and propagate the current stack and locals to
1099 it. This function specializes the target block, if necessary, and returns
1100 a pointer to the specialized target.
1103 sd...........stack analysis data
1104 b............the block to reach
1105 curstack.....the current stack top
1106 stackdepth...the current stack depth
1109 a pointer to (a specialized version of) the target
1110 NULL.........a VerifyError has been thrown
1112 *******************************************************************************/
1114 static basicblock *stack_mark_reached(stackdata_t *sd, basicblock *b, stackptr curstack, int stackdepth)
1116 #if defined(STACK_VERBOSE)
1117 printf("stack_mark_reached(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1119 /* mark targets of backward branches */
1120 if (b->nr <= sd->bptr->nr)
1121 b->bitflags |= BBFLAG_REPLACEMENT;
1123 if (b->flags < BBREACHED) {
1124 /* b is reached for the first time. Create its invars. */
1126 #if defined(STACK_VERBOSE)
1127 printf("reached L%03d for the first time\n", b->nr);
1130 stack_create_invars(sd, b, curstack, stackdepth);
1132 b->flags = BBREACHED;
1137 /* b has been reached before. Check that its invars match. */
1139 return stack_check_invars(sd, b, curstack, stackdepth);
1144 /* stack_mark_reached_from_outvars *********************************************
1146 Mark the given block reached and propagate the outvars of the current block
1147 and the current locals to it. This function specializes the target block,
1148 if necessary, and returns a pointer to the specialized target.
1151 sd...........stack analysis data
1152 b............the block to reach
1155 a pointer to (a specialized version of) the target
1156 NULL.........a VerifyError has been thrown
1158 *******************************************************************************/
1160 static basicblock *stack_mark_reached_from_outvars(stackdata_t *sd, basicblock *b)
1162 #if defined(STACK_VERBOSE)
1163 printf("stack_mark_reached_from_outvars(L%03d from L%03d)\n", b->nr, sd->bptr->nr);
1165 /* mark targets of backward branches */
1166 if (b->nr <= sd->bptr->nr)
1167 b->bitflags |= BBFLAG_REPLACEMENT;
1169 if (b->flags < BBREACHED) {
1170 /* b is reached for the first time. Create its invars. */
1172 #if defined(STACK_VERBOSE)
1173 printf("reached L%03d for the first time\n", b->nr);
1176 stack_create_invars_from_outvars(sd, b);
1178 b->flags = BBREACHED;
1183 /* b has been reached before. Check that its invars match. */
1185 return stack_check_invars_from_outvars(sd, b);
1190 /* stack_reach_next_block ******************************************************
1192 Mark the following block reached and propagate the outvars of the current block
1193 and the current locals to it. This function specializes the target block,
1194 if necessary, and returns a pointer to the specialized target.
1197 sd...........stack analysis data
1200 a pointer to (a specialized version of) the following block
1201 NULL.........a VerifyError has been thrown
1203 *******************************************************************************/
1205 static bool stack_reach_next_block(stackdata_t *sd)
1210 tbptr = (sd->bptr->original) ? sd->bptr->original : sd->bptr;
1211 tbptr = stack_mark_reached_from_outvars(sd, tbptr->next);
1215 if (tbptr != sd->bptr->next) {
1216 #if defined(STACK_VERBOSE)
1217 printf("NEXT IS NON-CONSEQUITIVE L%03d\n", tbptr->nr);
1219 iptr = sd->bptr->iinstr + sd->bptr->icount - 1;
1220 assert(iptr->opc == ICMD_NOP);
1221 iptr->opc = ICMD_GOTO;
1222 iptr->dst.block = tbptr;
1224 if (tbptr->flags < BBFINISHED)
1225 sd->repeat = true; /* XXX check if we really need to repeat */
1232 /* stack_reach_handlers ********************************************************
1234 Reach the exception handlers for the current block.
1237 sd...........stack analysis data
1240 true.........everything ok
1241 false........a VerifyError has been thrown
1243 *******************************************************************************/
1245 static bool stack_reach_handlers(stackdata_t *sd)
1250 #if defined(STACK_VERBOSE)
1251 printf("reaching exception handlers...\n");
1254 for (i=0; sd->handlers[i]; ++i) {
1255 tbptr = sd->handlers[i]->handler;
1257 tbptr->type = BBTYPE_EXH;
1258 tbptr->predecessorcount = CFG_UNKNOWN_PREDECESSORS;
1260 /* reach (and specialize) the handler block */
1262 tbptr = stack_mark_reached(sd, tbptr, &(sd->exstack), 1);
1267 sd->handlers[i]->handler = tbptr;
1274 /* stack_reanalyse_block ******************************************************
1276 Re-analyse the current block. This is called if either the block itself
1277 has already been analysed before, or the current block is a clone of an
1278 already analysed block, and this clone is reached for the first time.
1279 In the latter case, this function does all that is necessary for fully
1280 cloning the block (cloning the instruction list and variables, etc.).
1283 sd...........stack analysis data
1286 true.........everything ok
1287 false........a VerifyError has been thrown
1289 *******************************************************************************/
1291 #define RELOCATE(index) \
1293 if ((index) >= blockvarstart) \
1294 (index) += blockvarshift; \
1295 else if ((index) >= invarstart) \
1296 (index) += invarshift; \
1299 bool stack_reanalyse_block(stackdata_t *sd)
1311 branch_target_t *table;
1312 lookup_target_t *lookup;
1314 bool cloneinstructions;
1315 exception_entry *ex;
1317 #if defined(STACK_VERBOSE)
1318 stack_verbose_block_enter(sd, true);
1325 assert(orig != NULL);
1327 /* clone the instruction list */
1329 cloneinstructions = true;
1331 assert(orig->iinstr);
1333 iptr = DMNEW(instruction, len + 1);
1335 MCOPY(iptr, orig->iinstr, instruction, len);
1336 iptr[len].opc = ICMD_NOP;
1338 iptr[len].flags.bits = 0;
1342 /* reserve space for the clone's block variables */
1344 stack_grow_variable_array(sd, orig->varcount);
1346 /* we already have the invars set */
1348 assert(b->indepth == orig->indepth);
1350 /* calculate relocation shifts for invars and block variables */
1352 if (orig->indepth) {
1353 invarstart = orig->invars[0];
1354 invarshift = b->invars[0] - invarstart;
1357 invarstart = INT_MAX;
1360 blockvarstart = orig->varstart;
1361 blockvarshift = sd->vartop - blockvarstart;
1363 /* copy block variables */
1365 b->varstart = sd->vartop;
1366 b->varcount = orig->varcount;
1367 sd->vartop += b->varcount;
1368 MCOPY(sd->var + b->varstart, sd->var + orig->varstart, varinfo, b->varcount);
1372 b->outdepth = orig->outdepth;
1373 b->outvars = DMNEW(s4, orig->outdepth);
1374 MCOPY(b->outvars, orig->outvars, s4, orig->outdepth);
1376 /* clone exception handlers */
1378 for (i=0; sd->handlers[i]; ++i) {
1379 ex = DNEW(exception_entry);
1380 ex->handler = sd->handlers[i]->handler;
1382 ex->end = b; /* XXX hack, see end of stack_analyse */
1383 ex->catchtype = sd->handlers[i]->catchtype;
1386 assert(sd->extableend->down == NULL);
1387 sd->extableend->down = ex;
1388 sd->extableend = ex;
1389 sd->jd->exceptiontablelength++;
1391 sd->handlers[i] = ex;
1395 cloneinstructions = false;
1398 invarstart = sd->vartop;
1399 blockvarstart = sd->vartop;
1404 /* find exception handlers for the cloned block */
1406 ex = sd->jd->exceptiontable;
1407 for (; ex != NULL; ex = ex->down) {
1408 /* XXX the cloned exception handlers have identical */
1409 /* start end end blocks. */
1410 if ((ex->start == b) && (ex->end == b)) {
1411 sd->handlers[len++] = ex;
1414 sd->handlers[len] = NULL;
1417 #if defined(STACK_VERBOSE)
1418 printf("invarstart = %d, blockvarstart = %d\n", invarstart, blockvarstart);
1419 printf("invarshift = %d, blockvarshift = %d\n", invarshift, blockvarshift);
1422 /* mark block as finished */
1424 b->flags = BBFINISHED;
1426 /* initialize locals at the start of this block */
1429 MCOPY(sd->var, b->inlocals, varinfo, sd->localcount);
1431 MCOPY(sd->javalocals, b->javalocals, s4, sd->maxlocals);
1433 /* reach exception handlers for this block */
1435 if (!stack_reach_handlers(sd))
1438 superblockend = false;
1440 for (len = b->icount; len--; iptr++) {
1441 #if defined(STACK_VERBOSE)
1442 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1446 switch (iptr->opc) {
1448 j = iptr->s1.varindex;
1450 #if defined(ENABLE_VERIFIER)
1451 if (sd->var[j].type != TYPE_RET) {
1452 exceptions_throw_verifyerror(sd->m, "RET with non-returnAddress value");
1457 iptr->dst.block = stack_mark_reached_from_outvars(sd, sd->var[j].vv.retaddr);
1458 superblockend = true;
1462 iptr->sx.s23.s3.jsrtarget.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.jsrtarget.block);
1463 RELOCATE(iptr->dst.varindex);
1464 superblockend = true;
1468 superblockend = true;
1471 case ICMD_CHECKNULL:
1472 case ICMD_PUTSTATICCONST:
1480 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1481 superblockend = true;
1484 /* pop 0 push 1 const */
1492 /* pop 0 push 1 load */
1499 RELOCATE(iptr->dst.varindex);
1512 RELOCATE(iptr->sx.s23.s2.varindex);
1513 RELOCATE(iptr->s1.varindex);
1514 RELOCATE(iptr->dst.varindex);
1527 RELOCATE(iptr->sx.s23.s3.varindex);
1528 RELOCATE(iptr->sx.s23.s2.varindex);
1529 RELOCATE(iptr->s1.varindex);
1532 /* pop 1 push 0 store */
1539 RELOCATE(iptr->s1.varindex);
1541 j = iptr->dst.varindex;
1542 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, j);
1543 i = iptr->sx.s23.s3.javaindex;
1544 if (iptr->flags.bits & INS_FLAG_RETADDR) {
1545 iptr->sx.s23.s2.retaddrnr =
1546 UNUSED - (1 + sd->var[j].vv.retaddr->nr);
1547 sd->javalocals[i] = iptr->sx.s23.s2.retaddrnr;
1550 sd->javalocals[i] = j;
1551 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
1552 sd->javalocals[i-1] = UNUSED;
1553 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
1554 sd->javalocals[i+1] = UNUSED;
1565 RELOCATE(iptr->s1.varindex);
1566 superblockend = true;
1569 case ICMD_PUTSTATIC:
1570 case ICMD_PUTFIELDCONST:
1572 RELOCATE(iptr->s1.varindex);
1575 /* pop 1 push 0 branch */
1578 case ICMD_IFNONNULL:
1593 RELOCATE(iptr->s1.varindex);
1594 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1597 /* pop 1 push 0 table branch */
1599 case ICMD_TABLESWITCH:
1600 i = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1 + 1;
1602 if (cloneinstructions) {
1603 table = DMNEW(branch_target_t, i);
1604 MCOPY(table, iptr->dst.table, branch_target_t, i);
1605 iptr->dst.table = table;
1608 table = iptr->dst.table;
1611 RELOCATE(iptr->s1.varindex);
1613 table->block = stack_mark_reached_from_outvars(sd, table->block);
1616 superblockend = true;
1619 case ICMD_LOOKUPSWITCH:
1620 i = iptr->sx.s23.s2.lookupcount;
1621 if (cloneinstructions) {
1622 lookup = DMNEW(lookup_target_t, i);
1623 MCOPY(lookup, iptr->dst.lookup, lookup_target_t, i);
1624 iptr->dst.lookup = lookup;
1627 lookup = iptr->dst.lookup;
1629 RELOCATE(iptr->s1.varindex);
1631 lookup->target.block = stack_mark_reached_from_outvars(sd, lookup->target.block);
1634 iptr->sx.s23.s3.lookupdefault.block = stack_mark_reached_from_outvars(sd, iptr->sx.s23.s3.lookupdefault.block);
1635 superblockend = true;
1638 case ICMD_MONITORENTER:
1639 case ICMD_MONITOREXIT:
1640 RELOCATE(iptr->s1.varindex);
1643 /* pop 2 push 0 branch */
1645 case ICMD_IF_ICMPEQ:
1646 case ICMD_IF_ICMPNE:
1647 case ICMD_IF_ICMPLT:
1648 case ICMD_IF_ICMPGE:
1649 case ICMD_IF_ICMPGT:
1650 case ICMD_IF_ICMPLE:
1652 case ICMD_IF_LCMPEQ:
1653 case ICMD_IF_LCMPNE:
1654 case ICMD_IF_LCMPLT:
1655 case ICMD_IF_LCMPGE:
1656 case ICMD_IF_LCMPGT:
1657 case ICMD_IF_LCMPLE:
1659 case ICMD_IF_FCMPEQ:
1660 case ICMD_IF_FCMPNE:
1662 case ICMD_IF_FCMPL_LT:
1663 case ICMD_IF_FCMPL_GE:
1664 case ICMD_IF_FCMPL_GT:
1665 case ICMD_IF_FCMPL_LE:
1667 case ICMD_IF_FCMPG_LT:
1668 case ICMD_IF_FCMPG_GE:
1669 case ICMD_IF_FCMPG_GT:
1670 case ICMD_IF_FCMPG_LE:
1672 case ICMD_IF_DCMPEQ:
1673 case ICMD_IF_DCMPNE:
1675 case ICMD_IF_DCMPL_LT:
1676 case ICMD_IF_DCMPL_GE:
1677 case ICMD_IF_DCMPL_GT:
1678 case ICMD_IF_DCMPL_LE:
1680 case ICMD_IF_DCMPG_LT:
1681 case ICMD_IF_DCMPG_GE:
1682 case ICMD_IF_DCMPG_GT:
1683 case ICMD_IF_DCMPG_LE:
1685 case ICMD_IF_ACMPEQ:
1686 case ICMD_IF_ACMPNE:
1687 RELOCATE(iptr->sx.s23.s2.varindex);
1688 RELOCATE(iptr->s1.varindex);
1689 iptr->dst.block = stack_mark_reached_from_outvars(sd, iptr->dst.block);
1695 case ICMD_IASTORECONST:
1696 case ICMD_LASTORECONST:
1697 case ICMD_AASTORECONST:
1698 case ICMD_BASTORECONST:
1699 case ICMD_CASTORECONST:
1700 case ICMD_SASTORECONST:
1702 RELOCATE(iptr->sx.s23.s2.varindex);
1703 RELOCATE(iptr->s1.varindex);
1706 /* pop 0 push 1 copy */
1710 RELOCATE(iptr->dst.varindex);
1711 RELOCATE(iptr->s1.varindex);
1712 COPY_VAL_AND_TYPE(*sd, iptr->s1.varindex, iptr->dst.varindex);
1754 RELOCATE(iptr->sx.s23.s2.varindex);
1755 RELOCATE(iptr->s1.varindex);
1756 RELOCATE(iptr->dst.varindex);
1761 case ICMD_CHECKCAST:
1762 case ICMD_ARRAYLENGTH:
1763 case ICMD_INSTANCEOF:
1765 case ICMD_ANEWARRAY:
1767 case ICMD_IADDCONST:
1768 case ICMD_ISUBCONST:
1769 case ICMD_IMULCONST:
1773 case ICMD_IANDCONST:
1775 case ICMD_IXORCONST:
1776 case ICMD_ISHLCONST:
1777 case ICMD_ISHRCONST:
1778 case ICMD_IUSHRCONST:
1779 case ICMD_LADDCONST:
1780 case ICMD_LSUBCONST:
1781 case ICMD_LMULCONST:
1785 case ICMD_LANDCONST:
1787 case ICMD_LXORCONST:
1788 case ICMD_LSHLCONST:
1789 case ICMD_LSHRCONST:
1790 case ICMD_LUSHRCONST:
1794 case ICMD_INT2SHORT:
1810 RELOCATE(iptr->s1.varindex);
1811 RELOCATE(iptr->dst.varindex);
1816 case ICMD_GETSTATIC:
1818 RELOCATE(iptr->dst.varindex);
1821 /* pop many push any */
1823 case ICMD_INVOKESTATIC:
1824 case ICMD_INVOKESPECIAL:
1825 case ICMD_INVOKEVIRTUAL:
1826 case ICMD_INVOKEINTERFACE:
1828 case ICMD_MULTIANEWARRAY:
1829 i = iptr->s1.argcount;
1830 if (cloneinstructions) {
1831 argp = DMNEW(s4, i);
1832 MCOPY(argp, iptr->sx.s23.s2.args, s4, i);
1833 iptr->sx.s23.s2.args = argp;
1836 argp = iptr->sx.s23.s2.args;
1843 RELOCATE(iptr->dst.varindex);
1847 exceptions_throw_internalerror("Unknown ICMD %d during stack re-analysis",
1852 #if defined(STACK_VERBOSE)
1853 show_icmd(sd->jd, iptr, false, SHOW_STACK);
1858 /* relocate outvars */
1860 for (i=0; i<b->outdepth; ++i) {
1861 RELOCATE(b->outvars[i]);
1864 #if defined(STACK_VERBOSE)
1865 stack_verbose_block_exit(sd, superblockend);
1868 /* propagate to the next block */
1871 if (!stack_reach_next_block(sd))
1878 /* stack_change_to_tempvar *****************************************************
1880 Change the given stackslot to a TEMPVAR. This includes creating a new
1881 temporary variable and changing the dst.varindex of the creator of the
1882 stacklot to the new variable index. If this stackslot has been passed
1883 through ICMDs between the point of its creation and the current point,
1884 then the variable index is also changed in these ICMDs.
1887 sd...........stack analysis data
1888 sp...........stackslot to change
1889 ilimit.......instruction up to which to look for ICMDs passing-through
1890 the stackslot (exclusive). This may point exactly after the
1891 last instruction, in which case the search is done to the
1894 *******************************************************************************/
1896 static void stack_change_to_tempvar(stackdata_t *sd, stackptr sp,
1897 instruction *ilimit)
1905 oldindex = sp->varnum;
1907 /* create a new temporary variable */
1909 GET_NEW_VAR(*sd, newindex, sp->type);
1911 sd->var[newindex].flags = sp->flags;
1913 /* change the stackslot */
1915 sp->varnum = newindex;
1916 sp->varkind = TEMPVAR;
1918 /* change the dst.varindex of the stackslot's creator */
1921 sp->creator->dst.varindex = newindex;
1923 /* handle ICMDs this stackslot passed through, if any */
1925 if (sp->flags & PASSTHROUGH) {
1926 iptr = (sp->creator) ? (sp->creator + 1) : sd->bptr->iinstr;
1928 /* assert that the limit points to an ICMD, or after the last one */
1930 assert(ilimit >= sd->bptr->iinstr);
1931 assert(ilimit <= sd->bptr->iinstr + sd->bptr->icount);
1933 /* find the stackdepth under sp plus one */
1934 /* Note: This number is usually known when this function is called, */
1935 /* but calculating it here is less error-prone and should not be */
1936 /* a performance problem. */
1938 for (depth = 0; sp != NULL; sp = sp->prev)
1941 /* iterate over all instructions in the range and replace */
1943 for (; iptr < ilimit; ++iptr) {
1944 switch (iptr->opc) {
1945 case ICMD_INVOKESTATIC:
1946 case ICMD_INVOKESPECIAL:
1947 case ICMD_INVOKEVIRTUAL:
1948 case ICMD_INVOKEINTERFACE:
1950 i = iptr->s1.argcount - depth;
1951 if (iptr->sx.s23.s2.args[i] == oldindex) {
1952 iptr->sx.s23.s2.args[i] = newindex;
1955 /* IMPORTANT: If any ICMD sets the PASSTHROUGH flag of a */
1956 /* stackslot, it must be added in this switch! */
1963 /* stack_init_javalocals *******************************************************
1965 Initialize the mapping from Java locals to cacao variables at method entry.
1968 sd...........stack analysis data
1970 *******************************************************************************/
1972 static void stack_init_javalocals(stackdata_t *sd)
1981 jl = DMNEW(s4, sd->maxlocals);
1982 jd->basicblocks[0].javalocals = jl;
1984 for (i=0; i<sd->maxlocals; ++i)
1987 md = jd->m->parseddesc;
1989 for (i=0; i<md->paramcount; ++i) {
1990 t = md->paramtypes[i].type;
1991 jl[j] = jd->local_map[5*j + t];
1993 if (IS_2_WORD_TYPE(t))
1999 /* stack_analyse ***************************************************************
2001 Analyse_stack uses the intermediate code created by parse.c to
2002 build a model of the JVM operand stack for the current method.
2004 The following checks are performed:
2005 - check for operand stack underflow (before each instruction)
2006 - check for operand stack overflow (after[1] each instruction)
2007 - check for matching stack depth at merging points
2008 - check for matching basic types[2] at merging points
2009 - check basic types for instruction input (except for BUILTIN*
2010 opcodes, INVOKE* opcodes and MULTIANEWARRAY)
2012 [1]) Checking this after the instruction should be ok. parse.c
2013 counts the number of required stack slots in such a way that it is
2014 only vital that we don't exceed `maxstack` at basic block
2017 [2]) 'basic types' means the distinction between INT, LONG, FLOAT,
2018 DOUBLE and ADDRESS types. Subtypes of INT and different ADDRESS
2019 types are not discerned.
2021 *******************************************************************************/
2023 bool stack_analyse(jitdata *jd)
2025 methodinfo *m; /* method being analyzed */
2028 #if defined(ENABLE_SSA)
2032 stackptr curstack; /* current stack top */
2034 int opcode; /* opcode of current instruction */
2037 int len; /* # of instructions after the current one */
2038 bool superblockend; /* if true, no fallthrough to next block */
2039 bool deadcode; /* true if no live code has been reached */
2040 instruction *iptr; /* the current instruction */
2042 basicblock *original;
2043 exception_entry *ex;
2045 stackptr *last_store_boundary;
2046 stackptr coalescing_boundary;
2048 stackptr src1, src2, src3, src4, dst1, dst2;
2050 branch_target_t *table;
2051 lookup_target_t *lookup;
2052 #if defined(ENABLE_VERIFIER)
2053 int expectedtype; /* used by CHECK_BASIC_TYPE */
2055 builtintable_entry *bte;
2057 constant_FMIref *fmiref;
2058 #if defined(ENABLE_STATISTICS)
2059 int iteration_count; /* number of iterations of analysis */
2061 int new_index; /* used to get a new var index with GET_NEW_INDEX*/
2063 #if defined(STACK_VERBOSE)
2064 show_method(jd, SHOW_PARSE);
2067 /* get required compiler data - initialization */
2071 #if defined(ENABLE_SSA)
2075 /* initialize the stackdata_t struct */
2079 sd.varcount = jd->varcount;
2080 sd.vartop = jd->vartop;
2081 sd.localcount = jd->localcount;
2083 sd.varsallocated = sd.varcount;
2084 sd.maxlocals = m->maxlocals;
2085 sd.javalocals = DMNEW(s4, sd.maxlocals);
2086 sd.handlers = DMNEW(exception_entry *, jd->exceptiontablelength + 1);
2088 /* prepare the variable for exception handler stacks */
2089 /* (has been reserved by STACK_EXTRA_VARS, or VERIFIER_EXTRA_VARS) */
2091 sd.exstack.type = TYPE_ADR;
2092 sd.exstack.prev = NULL;
2093 sd.exstack.varnum = sd.localcount;
2094 sd.var[sd.exstack.varnum].type = TYPE_ADR;
2096 #if defined(ENABLE_LSRA)
2097 m->maxlifetimes = 0;
2100 #if defined(ENABLE_STATISTICS)
2101 iteration_count = 0;
2104 /* find the last real basic block */
2106 sd.last_real_block = NULL;
2107 tbptr = jd->basicblocks;
2108 while (tbptr->next) {
2109 sd.last_real_block = tbptr;
2110 tbptr = tbptr->next;
2112 assert(sd.last_real_block);
2114 /* find the last exception handler */
2116 if (jd->exceptiontablelength)
2117 sd.extableend = jd->exceptiontable + jd->exceptiontablelength - 1;
2119 sd.extableend = NULL;
2121 /* init jd->interface_map */
2123 jd->maxinterfaces = m->maxstack;
2124 jd->interface_map = DMNEW(interface_info, m->maxstack * 5);
2125 for (i = 0; i < m->maxstack * 5; i++)
2126 jd->interface_map[i].flags = UNUSED;
2128 last_store_boundary = DMNEW(stackptr, m->maxlocals);
2130 /* initialize flags and invars (none) of first block */
2132 jd->basicblocks[0].flags = BBREACHED;
2133 jd->basicblocks[0].invars = NULL;
2134 jd->basicblocks[0].indepth = 0;
2135 jd->basicblocks[0].inlocals =
2136 DMNEW(varinfo, jd->localcount + VERIFIER_EXTRA_LOCALS);
2137 MCOPY(jd->basicblocks[0].inlocals, jd->var, varinfo,
2138 jd->localcount + VERIFIER_EXTRA_LOCALS);
2140 /* initialize java local mapping of first block */
2142 stack_init_javalocals(&sd);
2144 /* stack analysis loop (until fixpoint reached) **************************/
2147 #if defined(ENABLE_STATISTICS)
2151 /* initialize loop over basic blocks */
2153 sd.bptr = jd->basicblocks;
2154 superblockend = true;
2156 curstack = NULL; stackdepth = 0;
2159 /* iterate over basic blocks *****************************************/
2161 for (; sd.bptr; sd.bptr = sd.bptr->next) {
2163 if (sd.bptr->flags == BBDELETED) {
2164 /* This block has been deleted - do nothing. */
2169 if (sd.bptr->flags == BBTYPECHECK_REACHED) {
2170 /* re-analyse a block because its input changed */
2172 if (!stack_reanalyse_block(&sd))
2174 superblockend = true; /* XXX */
2178 if (superblockend && (sd.bptr->flags < BBREACHED)) {
2179 /* This block has not been reached so far, and we */
2180 /* don't fall into it, so we'll have to iterate again. */
2186 if (sd.bptr->flags > BBREACHED) {
2187 /* This block is already finished. */
2189 superblockend = true;
2193 if (sd.bptr->original && sd.bptr->original->flags < BBFINISHED) {
2194 /* This block is a clone and the original has not been */
2195 /* analysed, yet. Analyse it on the next iteration. */
2198 /* XXX superblockend? */
2202 /* This block has to be analysed now. */
2206 /* XXX The rest of this block is still indented one level too */
2207 /* much in order to avoid a giant diff by changing that. */
2209 /* We know that sd.bptr->flags == BBREACHED. */
2210 /* This block has been reached before. */
2212 assert(sd.bptr->flags == BBREACHED);
2213 stackdepth = sd.bptr->indepth;
2215 /* find exception handlers for this block */
2217 /* determine the active exception handlers for this block */
2218 /* XXX could use a faster algorithm with sorted lists or */
2221 original = (sd.bptr->original) ? sd.bptr->original : sd.bptr;
2224 ex = jd->exceptiontable;
2225 for (; ex != NULL; ex = ex->down) {
2226 if ((ex->start <= original) && (ex->end > original)) {
2227 sd.handlers[len++] = ex;
2230 sd.handlers[len] = NULL;
2233 /* reanalyse cloned block */
2235 if (sd.bptr->original) {
2236 if (!stack_reanalyse_block(&sd))
2241 /* reset the new pointer for allocating stackslots */
2245 /* create the instack of this block */
2247 curstack = stack_create_instack(&sd);
2249 /* initialize locals at the start of this block */
2251 if (sd.bptr->inlocals)
2252 MCOPY(sd.var, sd.bptr->inlocals, varinfo, sd.localcount);
2254 MCOPY(sd.javalocals, sd.bptr->javalocals, s4, sd.maxlocals);
2256 /* set up local variables for analyzing this block */
2258 superblockend = false;
2259 len = sd.bptr->icount;
2260 iptr = sd.bptr->iinstr;
2262 /* mark the block as analysed */
2264 sd.bptr->flags = BBFINISHED;
2266 /* reset variables for dependency checking */
2268 coalescing_boundary = sd.new;
2269 for( i = 0; i < m->maxlocals; i++)
2270 last_store_boundary[i] = sd.new;
2272 /* remember the start of this block's variables */
2274 sd.bptr->varstart = sd.vartop;
2276 #if defined(STACK_VERBOSE)
2277 stack_verbose_block_enter(&sd, false);
2280 /* reach exception handlers for this block */
2282 if (!stack_reach_handlers(&sd))
2285 /* iterate over ICMDs ****************************************/
2287 while (--len >= 0) {
2289 #if defined(STACK_VERBOSE)
2290 stack_verbose_show_state(&sd, iptr, curstack);
2293 /* fetch the current opcode */
2297 /* automatically replace some ICMDs with builtins */
2299 #if defined(USEBUILTINTABLE)
2300 bte = builtintable_get_automatic(opcode);
2302 if (bte && bte->opcode == opcode) {
2303 iptr->opc = ICMD_BUILTIN;
2304 iptr->flags.bits &= INS_FLAG_ID_MASK;
2305 iptr->sx.s23.s3.bte = bte;
2306 /* iptr->line is already set */
2307 jd->isleafmethod = false;
2310 #endif /* defined(USEBUILTINTABLE) */
2312 /* main opcode switch *************************************/
2324 case ICMD_CHECKNULL:
2325 coalescing_boundary = sd.new;
2326 COUNT(count_check_null);
2329 iptr->dst.varindex = iptr->s1.varindex;
2333 j = iptr->s1.varindex =
2334 jd->local_map[iptr->s1.varindex * 5 + TYPE_ADR];
2336 #if defined(ENABLE_VERIFIER)
2337 if (sd.var[j].type != TYPE_RET) {
2338 exceptions_throw_verifyerror(m, "RET with non-returnAddress value");
2345 iptr->dst.block = stack_mark_reached(&sd, sd.var[j].vv.retaddr, curstack, stackdepth);
2346 superblockend = true;
2350 COUNT(count_pcmd_return);
2353 superblockend = true;
2354 sd.jd->returncount++;
2355 sd.jd->returnblock = sd.bptr;
2359 /* pop 0 push 1 const */
2361 /************************** ICONST OPTIMIZATIONS **************************/
2364 COUNT(count_pcmd_load);
2368 switch (iptr[1].opc) {
2370 iptr->opc = ICMD_IADDCONST;
2374 iptr[1].opc = ICMD_NOP;
2375 OP1_1(TYPE_INT, TYPE_INT);
2376 COUNT(count_pcmd_op);
2380 iptr->opc = ICMD_ISUBCONST;
2381 goto icmd_iconst_tail;
2382 #if SUPPORT_CONST_MUL
2384 iptr->opc = ICMD_IMULCONST;
2385 goto icmd_iconst_tail;
2386 #else /* SUPPORT_CONST_MUL */
2388 if (iptr->sx.val.i == 0x00000002)
2390 else if (iptr->sx.val.i == 0x00000004)
2392 else if (iptr->sx.val.i == 0x00000008)
2394 else if (iptr->sx.val.i == 0x00000010)
2396 else if (iptr->sx.val.i == 0x00000020)
2398 else if (iptr->sx.val.i == 0x00000040)
2400 else if (iptr->sx.val.i == 0x00000080)
2402 else if (iptr->sx.val.i == 0x00000100)
2404 else if (iptr->sx.val.i == 0x00000200)
2406 else if (iptr->sx.val.i == 0x00000400)
2407 iptr->sx.val.i = 10;
2408 else if (iptr->sx.val.i == 0x00000800)
2409 iptr->sx.val.i = 11;
2410 else if (iptr->sx.val.i == 0x00001000)
2411 iptr->sx.val.i = 12;
2412 else if (iptr->sx.val.i == 0x00002000)
2413 iptr->sx.val.i = 13;
2414 else if (iptr->sx.val.i == 0x00004000)
2415 iptr->sx.val.i = 14;
2416 else if (iptr->sx.val.i == 0x00008000)
2417 iptr->sx.val.i = 15;
2418 else if (iptr->sx.val.i == 0x00010000)
2419 iptr->sx.val.i = 16;
2420 else if (iptr->sx.val.i == 0x00020000)
2421 iptr->sx.val.i = 17;
2422 else if (iptr->sx.val.i == 0x00040000)
2423 iptr->sx.val.i = 18;
2424 else if (iptr->sx.val.i == 0x00080000)
2425 iptr->sx.val.i = 19;
2426 else if (iptr->sx.val.i == 0x00100000)
2427 iptr->sx.val.i = 20;
2428 else if (iptr->sx.val.i == 0x00200000)
2429 iptr->sx.val.i = 21;
2430 else if (iptr->sx.val.i == 0x00400000)
2431 iptr->sx.val.i = 22;
2432 else if (iptr->sx.val.i == 0x00800000)
2433 iptr->sx.val.i = 23;
2434 else if (iptr->sx.val.i == 0x01000000)
2435 iptr->sx.val.i = 24;
2436 else if (iptr->sx.val.i == 0x02000000)
2437 iptr->sx.val.i = 25;
2438 else if (iptr->sx.val.i == 0x04000000)
2439 iptr->sx.val.i = 26;
2440 else if (iptr->sx.val.i == 0x08000000)
2441 iptr->sx.val.i = 27;
2442 else if (iptr->sx.val.i == 0x10000000)
2443 iptr->sx.val.i = 28;
2444 else if (iptr->sx.val.i == 0x20000000)
2445 iptr->sx.val.i = 29;
2446 else if (iptr->sx.val.i == 0x40000000)
2447 iptr->sx.val.i = 30;
2448 else if (iptr->sx.val.i == 0x80000000)
2449 iptr->sx.val.i = 31;
2453 iptr->opc = ICMD_IMULPOW2;
2454 goto icmd_iconst_tail;
2455 #endif /* SUPPORT_CONST_MUL */
2457 if (iptr->sx.val.i == 0x00000002)
2459 else if (iptr->sx.val.i == 0x00000004)
2461 else if (iptr->sx.val.i == 0x00000008)
2463 else if (iptr->sx.val.i == 0x00000010)
2465 else if (iptr->sx.val.i == 0x00000020)
2467 else if (iptr->sx.val.i == 0x00000040)
2469 else if (iptr->sx.val.i == 0x00000080)
2471 else if (iptr->sx.val.i == 0x00000100)
2473 else if (iptr->sx.val.i == 0x00000200)
2475 else if (iptr->sx.val.i == 0x00000400)
2476 iptr->sx.val.i = 10;
2477 else if (iptr->sx.val.i == 0x00000800)
2478 iptr->sx.val.i = 11;
2479 else if (iptr->sx.val.i == 0x00001000)
2480 iptr->sx.val.i = 12;
2481 else if (iptr->sx.val.i == 0x00002000)
2482 iptr->sx.val.i = 13;
2483 else if (iptr->sx.val.i == 0x00004000)
2484 iptr->sx.val.i = 14;
2485 else if (iptr->sx.val.i == 0x00008000)
2486 iptr->sx.val.i = 15;
2487 else if (iptr->sx.val.i == 0x00010000)
2488 iptr->sx.val.i = 16;
2489 else if (iptr->sx.val.i == 0x00020000)
2490 iptr->sx.val.i = 17;
2491 else if (iptr->sx.val.i == 0x00040000)
2492 iptr->sx.val.i = 18;
2493 else if (iptr->sx.val.i == 0x00080000)
2494 iptr->sx.val.i = 19;
2495 else if (iptr->sx.val.i == 0x00100000)
2496 iptr->sx.val.i = 20;
2497 else if (iptr->sx.val.i == 0x00200000)
2498 iptr->sx.val.i = 21;
2499 else if (iptr->sx.val.i == 0x00400000)
2500 iptr->sx.val.i = 22;
2501 else if (iptr->sx.val.i == 0x00800000)
2502 iptr->sx.val.i = 23;
2503 else if (iptr->sx.val.i == 0x01000000)
2504 iptr->sx.val.i = 24;
2505 else if (iptr->sx.val.i == 0x02000000)
2506 iptr->sx.val.i = 25;
2507 else if (iptr->sx.val.i == 0x04000000)
2508 iptr->sx.val.i = 26;
2509 else if (iptr->sx.val.i == 0x08000000)
2510 iptr->sx.val.i = 27;
2511 else if (iptr->sx.val.i == 0x10000000)
2512 iptr->sx.val.i = 28;
2513 else if (iptr->sx.val.i == 0x20000000)
2514 iptr->sx.val.i = 29;
2515 else if (iptr->sx.val.i == 0x40000000)
2516 iptr->sx.val.i = 30;
2517 else if (iptr->sx.val.i == 0x80000000)
2518 iptr->sx.val.i = 31;
2522 iptr->opc = ICMD_IDIVPOW2;
2523 goto icmd_iconst_tail;
2526 /*log_text("stack.c: ICMD_ICONST/ICMD_IREM");*/
2527 if ((iptr->sx.val.i == 0x00000002) ||
2528 (iptr->sx.val.i == 0x00000004) ||
2529 (iptr->sx.val.i == 0x00000008) ||
2530 (iptr->sx.val.i == 0x00000010) ||
2531 (iptr->sx.val.i == 0x00000020) ||
2532 (iptr->sx.val.i == 0x00000040) ||
2533 (iptr->sx.val.i == 0x00000080) ||
2534 (iptr->sx.val.i == 0x00000100) ||
2535 (iptr->sx.val.i == 0x00000200) ||
2536 (iptr->sx.val.i == 0x00000400) ||
2537 (iptr->sx.val.i == 0x00000800) ||
2538 (iptr->sx.val.i == 0x00001000) ||
2539 (iptr->sx.val.i == 0x00002000) ||
2540 (iptr->sx.val.i == 0x00004000) ||
2541 (iptr->sx.val.i == 0x00008000) ||
2542 (iptr->sx.val.i == 0x00010000) ||
2543 (iptr->sx.val.i == 0x00020000) ||
2544 (iptr->sx.val.i == 0x00040000) ||
2545 (iptr->sx.val.i == 0x00080000) ||
2546 (iptr->sx.val.i == 0x00100000) ||
2547 (iptr->sx.val.i == 0x00200000) ||
2548 (iptr->sx.val.i == 0x00400000) ||
2549 (iptr->sx.val.i == 0x00800000) ||
2550 (iptr->sx.val.i == 0x01000000) ||
2551 (iptr->sx.val.i == 0x02000000) ||
2552 (iptr->sx.val.i == 0x04000000) ||
2553 (iptr->sx.val.i == 0x08000000) ||
2554 (iptr->sx.val.i == 0x10000000) ||
2555 (iptr->sx.val.i == 0x20000000) ||
2556 (iptr->sx.val.i == 0x40000000) ||
2557 (iptr->sx.val.i == 0x80000000))
2559 iptr->opc = ICMD_IREMPOW2;
2560 iptr->sx.val.i -= 1;
2561 goto icmd_iconst_tail;
2564 #if SUPPORT_CONST_LOGICAL
2566 iptr->opc = ICMD_IANDCONST;
2567 goto icmd_iconst_tail;
2570 iptr->opc = ICMD_IORCONST;
2571 goto icmd_iconst_tail;
2574 iptr->opc = ICMD_IXORCONST;
2575 goto icmd_iconst_tail;
2577 #endif /* SUPPORT_CONST_LOGICAL */
2579 iptr->opc = ICMD_ISHLCONST;
2580 goto icmd_iconst_tail;
2583 iptr->opc = ICMD_ISHRCONST;
2584 goto icmd_iconst_tail;
2587 iptr->opc = ICMD_IUSHRCONST;
2588 goto icmd_iconst_tail;
2589 #if SUPPORT_LONG_SHIFT
2591 iptr->opc = ICMD_LSHLCONST;
2592 goto icmd_lconst_tail;
2595 iptr->opc = ICMD_LSHRCONST;
2596 goto icmd_lconst_tail;
2599 iptr->opc = ICMD_LUSHRCONST;
2600 goto icmd_lconst_tail;
2601 #endif /* SUPPORT_LONG_SHIFT */
2602 case ICMD_IF_ICMPEQ:
2603 iptr[1].opc = ICMD_IFEQ;
2607 /* set the constant for the following icmd */
2608 iptr[1].sx.val.i = iptr->sx.val.i;
2610 /* this instruction becomes a nop */
2611 iptr->opc = ICMD_NOP;
2614 case ICMD_IF_ICMPLT:
2615 iptr[1].opc = ICMD_IFLT;
2616 goto icmd_if_icmp_tail;
2618 case ICMD_IF_ICMPLE:
2619 iptr[1].opc = ICMD_IFLE;
2620 goto icmd_if_icmp_tail;
2622 case ICMD_IF_ICMPNE:
2623 iptr[1].opc = ICMD_IFNE;
2624 goto icmd_if_icmp_tail;
2626 case ICMD_IF_ICMPGT:
2627 iptr[1].opc = ICMD_IFGT;
2628 goto icmd_if_icmp_tail;
2630 case ICMD_IF_ICMPGE:
2631 iptr[1].opc = ICMD_IFGE;
2632 goto icmd_if_icmp_tail;
2634 #if SUPPORT_CONST_STORE
2639 # if SUPPORT_CONST_STORE_ZERO_ONLY
2640 if (iptr->sx.val.i != 0)
2643 switch (iptr[1].opc) {
2645 iptr->opc = ICMD_IASTORECONST;
2646 iptr->flags.bits |= INS_FLAG_CHECK;
2649 iptr->opc = ICMD_BASTORECONST;
2650 iptr->flags.bits |= INS_FLAG_CHECK;
2653 iptr->opc = ICMD_CASTORECONST;
2654 iptr->flags.bits |= INS_FLAG_CHECK;
2657 iptr->opc = ICMD_SASTORECONST;
2658 iptr->flags.bits |= INS_FLAG_CHECK;
2662 iptr[1].opc = ICMD_NOP;
2664 /* copy the constant to s3 */
2665 /* XXX constval -> astoreconstval? */
2666 iptr->sx.s23.s3.constval = iptr->sx.val.i;
2667 OP2_0(TYPE_ADR, TYPE_INT);
2668 COUNT(count_pcmd_op);
2671 case ICMD_PUTSTATIC:
2673 # if SUPPORT_CONST_STORE_ZERO_ONLY
2674 if (iptr->sx.val.i != 0)
2677 /* XXX check field type? */
2679 /* copy the constant to s2 */
2680 /* XXX constval -> fieldconstval? */
2681 iptr->sx.s23.s2.constval = iptr->sx.val.i;
2684 /* set the field reference (s3) */
2685 if (iptr[1].flags.bits & INS_FLAG_UNRESOLVED) {
2686 iptr->sx.s23.s3.uf = iptr[1].sx.s23.s3.uf;
2687 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
2688 fmiref = iptr->sx.s23.s3.uf->fieldref;
2691 fmiref = iptr[1].sx.s23.s3.fmiref;
2692 iptr->sx.s23.s3.fmiref = fmiref;
2695 #if defined(ENABLE_VERIFIER)
2696 expectedtype = fmiref->parseddesc.fd->type;
2697 switch (iptr[0].opc) {
2699 if (expectedtype != TYPE_INT)
2700 goto throw_stack_type_error;
2703 if (expectedtype != TYPE_LNG)
2704 goto throw_stack_type_error;
2707 if (expectedtype != TYPE_ADR)
2708 goto throw_stack_type_error;
2713 #endif /* defined(ENABLE_VERIFIER) */
2715 switch (iptr[1].opc) {
2716 case ICMD_PUTSTATIC:
2717 iptr->opc = ICMD_PUTSTATICCONST;
2721 iptr->opc = ICMD_PUTFIELDCONST;
2726 iptr[1].opc = ICMD_NOP;
2727 COUNT(count_pcmd_op);
2729 #endif /* SUPPORT_CONST_STORE */
2735 /* if we get here, the ICONST has been optimized */
2739 /* normal case of an unoptimized ICONST */
2743 /************************** LCONST OPTIMIZATIONS **************************/
2746 COUNT(count_pcmd_load);
2750 /* switch depending on the following instruction */
2752 switch (iptr[1].opc) {
2753 #if SUPPORT_LONG_ADD
2755 iptr->opc = ICMD_LADDCONST;
2759 /* instruction of type LONG -> LONG */
2760 iptr[1].opc = ICMD_NOP;
2761 OP1_1(TYPE_LNG, TYPE_LNG);
2762 COUNT(count_pcmd_op);
2766 iptr->opc = ICMD_LSUBCONST;
2767 goto icmd_lconst_tail;
2769 #endif /* SUPPORT_LONG_ADD */
2770 #if SUPPORT_LONG_MUL && SUPPORT_CONST_MUL
2772 iptr->opc = ICMD_LMULCONST;
2773 goto icmd_lconst_tail;
2774 #else /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2775 # if SUPPORT_LONG_SHIFT
2777 if (iptr->sx.val.l == 0x00000002)
2779 else if (iptr->sx.val.l == 0x00000004)
2781 else if (iptr->sx.val.l == 0x00000008)
2783 else if (iptr->sx.val.l == 0x00000010)
2785 else if (iptr->sx.val.l == 0x00000020)
2787 else if (iptr->sx.val.l == 0x00000040)
2789 else if (iptr->sx.val.l == 0x00000080)
2791 else if (iptr->sx.val.l == 0x00000100)
2793 else if (iptr->sx.val.l == 0x00000200)
2795 else if (iptr->sx.val.l == 0x00000400)
2796 iptr->sx.val.i = 10;
2797 else if (iptr->sx.val.l == 0x00000800)
2798 iptr->sx.val.i = 11;
2799 else if (iptr->sx.val.l == 0x00001000)
2800 iptr->sx.val.i = 12;
2801 else if (iptr->sx.val.l == 0x00002000)
2802 iptr->sx.val.i = 13;
2803 else if (iptr->sx.val.l == 0x00004000)
2804 iptr->sx.val.i = 14;
2805 else if (iptr->sx.val.l == 0x00008000)
2806 iptr->sx.val.i = 15;
2807 else if (iptr->sx.val.l == 0x00010000)
2808 iptr->sx.val.i = 16;
2809 else if (iptr->sx.val.l == 0x00020000)
2810 iptr->sx.val.i = 17;
2811 else if (iptr->sx.val.l == 0x00040000)
2812 iptr->sx.val.i = 18;
2813 else if (iptr->sx.val.l == 0x00080000)
2814 iptr->sx.val.i = 19;
2815 else if (iptr->sx.val.l == 0x00100000)
2816 iptr->sx.val.i = 20;
2817 else if (iptr->sx.val.l == 0x00200000)
2818 iptr->sx.val.i = 21;
2819 else if (iptr->sx.val.l == 0x00400000)
2820 iptr->sx.val.i = 22;
2821 else if (iptr->sx.val.l == 0x00800000)
2822 iptr->sx.val.i = 23;
2823 else if (iptr->sx.val.l == 0x01000000)
2824 iptr->sx.val.i = 24;
2825 else if (iptr->sx.val.l == 0x02000000)
2826 iptr->sx.val.i = 25;
2827 else if (iptr->sx.val.l == 0x04000000)
2828 iptr->sx.val.i = 26;
2829 else if (iptr->sx.val.l == 0x08000000)
2830 iptr->sx.val.i = 27;
2831 else if (iptr->sx.val.l == 0x10000000)
2832 iptr->sx.val.i = 28;
2833 else if (iptr->sx.val.l == 0x20000000)
2834 iptr->sx.val.i = 29;
2835 else if (iptr->sx.val.l == 0x40000000)
2836 iptr->sx.val.i = 30;
2837 else if (iptr->sx.val.l == 0x80000000)
2838 iptr->sx.val.i = 31;
2842 iptr->opc = ICMD_LMULPOW2;
2843 goto icmd_lconst_tail;
2844 # endif /* SUPPORT_LONG_SHIFT */
2845 #endif /* SUPPORT_LONG_MUL && SUPPORT_CONST_MUL */
2846 #if SUPPORT_LONG_DIV_POW2
2848 if (iptr->sx.val.l == 0x00000002)
2850 else if (iptr->sx.val.l == 0x00000004)
2852 else if (iptr->sx.val.l == 0x00000008)
2854 else if (iptr->sx.val.l == 0x00000010)
2856 else if (iptr->sx.val.l == 0x00000020)
2858 else if (iptr->sx.val.l == 0x00000040)
2860 else if (iptr->sx.val.l == 0x00000080)
2862 else if (iptr->sx.val.l == 0x00000100)
2864 else if (iptr->sx.val.l == 0x00000200)
2866 else if (iptr->sx.val.l == 0x00000400)
2867 iptr->sx.val.i = 10;
2868 else if (iptr->sx.val.l == 0x00000800)
2869 iptr->sx.val.i = 11;
2870 else if (iptr->sx.val.l == 0x00001000)
2871 iptr->sx.val.i = 12;
2872 else if (iptr->sx.val.l == 0x00002000)
2873 iptr->sx.val.i = 13;
2874 else if (iptr->sx.val.l == 0x00004000)
2875 iptr->sx.val.i = 14;
2876 else if (iptr->sx.val.l == 0x00008000)
2877 iptr->sx.val.i = 15;
2878 else if (iptr->sx.val.l == 0x00010000)
2879 iptr->sx.val.i = 16;
2880 else if (iptr->sx.val.l == 0x00020000)
2881 iptr->sx.val.i = 17;
2882 else if (iptr->sx.val.l == 0x00040000)
2883 iptr->sx.val.i = 18;
2884 else if (iptr->sx.val.l == 0x00080000)
2885 iptr->sx.val.i = 19;
2886 else if (iptr->sx.val.l == 0x00100000)
2887 iptr->sx.val.i = 20;
2888 else if (iptr->sx.val.l == 0x00200000)
2889 iptr->sx.val.i = 21;
2890 else if (iptr->sx.val.l == 0x00400000)
2891 iptr->sx.val.i = 22;
2892 else if (iptr->sx.val.l == 0x00800000)
2893 iptr->sx.val.i = 23;
2894 else if (iptr->sx.val.l == 0x01000000)
2895 iptr->sx.val.i = 24;
2896 else if (iptr->sx.val.l == 0x02000000)
2897 iptr->sx.val.i = 25;
2898 else if (iptr->sx.val.l == 0x04000000)
2899 iptr->sx.val.i = 26;
2900 else if (iptr->sx.val.l == 0x08000000)
2901 iptr->sx.val.i = 27;
2902 else if (iptr->sx.val.l == 0x10000000)
2903 iptr->sx.val.i = 28;
2904 else if (iptr->sx.val.l == 0x20000000)
2905 iptr->sx.val.i = 29;
2906 else if (iptr->sx.val.l == 0x40000000)
2907 iptr->sx.val.i = 30;
2908 else if (iptr->sx.val.l == 0x80000000)
2909 iptr->sx.val.i = 31;
2913 iptr->opc = ICMD_LDIVPOW2;
2914 goto icmd_lconst_tail;
2915 #endif /* SUPPORT_LONG_DIV_POW2 */
2917 #if SUPPORT_LONG_REM_POW2
2919 if ((iptr->sx.val.l == 0x00000002) ||
2920 (iptr->sx.val.l == 0x00000004) ||
2921 (iptr->sx.val.l == 0x00000008) ||
2922 (iptr->sx.val.l == 0x00000010) ||
2923 (iptr->sx.val.l == 0x00000020) ||
2924 (iptr->sx.val.l == 0x00000040) ||
2925 (iptr->sx.val.l == 0x00000080) ||
2926 (iptr->sx.val.l == 0x00000100) ||
2927 (iptr->sx.val.l == 0x00000200) ||
2928 (iptr->sx.val.l == 0x00000400) ||
2929 (iptr->sx.val.l == 0x00000800) ||
2930 (iptr->sx.val.l == 0x00001000) ||
2931 (iptr->sx.val.l == 0x00002000) ||
2932 (iptr->sx.val.l == 0x00004000) ||
2933 (iptr->sx.val.l == 0x00008000) ||
2934 (iptr->sx.val.l == 0x00010000) ||
2935 (iptr->sx.val.l == 0x00020000) ||
2936 (iptr->sx.val.l == 0x00040000) ||
2937 (iptr->sx.val.l == 0x00080000) ||
2938 (iptr->sx.val.l == 0x00100000) ||
2939 (iptr->sx.val.l == 0x00200000) ||
2940 (iptr->sx.val.l == 0x00400000) ||
2941 (iptr->sx.val.l == 0x00800000) ||
2942 (iptr->sx.val.l == 0x01000000) ||
2943 (iptr->sx.val.l == 0x02000000) ||
2944 (iptr->sx.val.l == 0x04000000) ||
2945 (iptr->sx.val.l == 0x08000000) ||
2946 (iptr->sx.val.l == 0x10000000) ||
2947 (iptr->sx.val.l == 0x20000000) ||
2948 (iptr->sx.val.l == 0x40000000) ||
2949 (iptr->sx.val.l == 0x80000000))
2951 iptr->opc = ICMD_LREMPOW2;
2952 iptr->sx.val.l -= 1;
2953 goto icmd_lconst_tail;
2956 #endif /* SUPPORT_LONG_REM_POW2 */
2958 #if SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL
2961 iptr->opc = ICMD_LANDCONST;
2962 goto icmd_lconst_tail;
2965 iptr->opc = ICMD_LORCONST;
2966 goto icmd_lconst_tail;
2969 iptr->opc = ICMD_LXORCONST;
2970 goto icmd_lconst_tail;
2971 #endif /* SUPPORT_LONG_LOGICAL && SUPPORT_CONST_LOGICAL */
2973 #if SUPPORT_LONG_CMP_CONST
2975 if ((len <= 1) || (iptr[2].sx.val.i != 0))
2978 /* switch on the instruction after LCONST - LCMP */
2980 switch (iptr[2].opc) {
2982 iptr->opc = ICMD_IF_LEQ;
2985 icmd_lconst_lcmp_tail:
2986 /* convert LCONST, LCMP, IFXX to IF_LXX */
2987 iptr->dst.insindex = iptr[2].dst.insindex;
2988 iptr[1].opc = ICMD_NOP;
2989 iptr[2].opc = ICMD_NOP;
2991 OP1_BRANCH(TYPE_LNG);
2993 COUNT(count_pcmd_bra);
2994 COUNT(count_pcmd_op);
2998 iptr->opc = ICMD_IF_LNE;
2999 goto icmd_lconst_lcmp_tail;
3002 iptr->opc = ICMD_IF_LLT;
3003 goto icmd_lconst_lcmp_tail;
3006 iptr->opc = ICMD_IF_LGT;
3007 goto icmd_lconst_lcmp_tail;
3010 iptr->opc = ICMD_IF_LLE;
3011 goto icmd_lconst_lcmp_tail;
3014 iptr->opc = ICMD_IF_LGE;
3015 goto icmd_lconst_lcmp_tail;
3019 } /* end switch on opcode after LCONST - LCMP */
3021 #endif /* SUPPORT_LONG_CMP_CONST */
3023 #if SUPPORT_CONST_STORE
3025 # if SUPPORT_CONST_STORE_ZERO_ONLY
3026 if (iptr->sx.val.l != 0)
3029 #if SIZEOF_VOID_P == 4
3030 /* the constant must fit into a ptrint */
3031 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3034 /* move the constant to s3 */
3035 iptr->sx.s23.s3.constval = iptr->sx.val.l;
3037 iptr->opc = ICMD_LASTORECONST;
3038 iptr->flags.bits |= INS_FLAG_CHECK;
3039 OP2_0(TYPE_ADR, TYPE_INT);
3041 iptr[1].opc = ICMD_NOP;
3042 COUNT(count_pcmd_op);
3045 case ICMD_PUTSTATIC:
3047 # if SUPPORT_CONST_STORE_ZERO_ONLY
3048 if (iptr->sx.val.l != 0)
3051 #if SIZEOF_VOID_P == 4
3052 /* the constant must fit into a ptrint */
3053 if (iptr->sx.val.l < -0x80000000L || iptr->sx.val.l >= 0x80000000L)
3056 /* XXX check field type? */
3058 /* copy the constant to s2 */
3059 /* XXX constval -> fieldconstval? */
3060 iptr->sx.s23.s2.constval = iptr->sx.val.l;
3064 #endif /* SUPPORT_CONST_STORE */
3068 } /* end switch opcode after LCONST */
3070 /* if we get here, the LCONST has been optimized */
3074 /* the normal case of an unoptimized LCONST */
3078 /************************ END OF LCONST OPTIMIZATIONS *********************/
3081 COUNT(count_pcmd_load);
3086 COUNT(count_pcmd_load);
3090 /************************** ACONST OPTIMIZATIONS **************************/
3093 coalescing_boundary = sd.new;
3094 COUNT(count_pcmd_load);
3095 #if SUPPORT_CONST_STORE
3096 /* We can only optimize if the ACONST is resolved
3097 * and there is an instruction after it. */
3099 if ((len == 0) || (iptr->flags.bits & INS_FLAG_UNRESOLVED))
3102 switch (iptr[1].opc) {
3104 /* We can only optimize for NULL values
3105 * here because otherwise a checkcast is
3107 if (iptr->sx.val.anyptr != NULL)
3110 /* copy the constant (NULL) to s3 */
3111 iptr->sx.s23.s3.constval = 0;
3112 iptr->opc = ICMD_AASTORECONST;
3113 iptr->flags.bits |= INS_FLAG_CHECK;
3114 OP2_0(TYPE_ADR, TYPE_INT);
3116 iptr[1].opc = ICMD_NOP;
3117 COUNT(count_pcmd_op);
3120 case ICMD_PUTSTATIC:
3122 # if SUPPORT_CONST_STORE_ZERO_ONLY
3123 if (iptr->sx.val.anyptr != NULL)
3126 /* XXX check field type? */
3127 /* copy the constant to s2 */
3128 /* XXX constval -> fieldconstval? */
3129 iptr->sx.s23.s2.constval = (ptrint) iptr->sx.val.anyptr;
3137 /* if we get here the ACONST has been optimized */
3141 #endif /* SUPPORT_CONST_STORE */
3146 /* pop 0 push 1 load */
3153 COUNT(count_load_instruction);
3154 i = opcode - ICMD_ILOAD; /* type */
3156 j = iptr->s1.varindex =
3157 jd->local_map[iptr->s1.varindex * 5 + i];
3159 #if defined(ENABLE_VERIFIER)
3160 if (sd.var[j].type == TYPE_RET) {
3161 exceptions_throw_verifyerror(m, "forbidden load of returnAddress");
3166 #if defined(ENABLE_SSA)
3168 GET_NEW_VAR(sd, new_index, i);
3185 coalescing_boundary = sd.new;
3186 iptr->flags.bits |= INS_FLAG_CHECK;
3187 COUNT(count_check_null);
3188 COUNT(count_check_bound);
3189 COUNT(count_pcmd_mem);
3190 OP2_1(TYPE_ADR, TYPE_INT, opcode - ICMD_IALOAD);
3197 coalescing_boundary = sd.new;
3198 iptr->flags.bits |= INS_FLAG_CHECK;
3199 COUNT(count_check_null);
3200 COUNT(count_check_bound);
3201 COUNT(count_pcmd_mem);
3202 OP2_1(TYPE_ADR, TYPE_INT, TYPE_INT);
3205 /* pop 0 push 0 iinc */
3208 STATISTICS_STACKDEPTH_DISTRIBUTION(count_store_depth);
3209 #if defined(ENABLE_SSA)
3212 jd->local_map[iptr->s1.varindex * 5 +TYPE_INT];
3216 last_store_boundary[iptr->s1.varindex] = sd.new;
3219 jd->local_map[iptr->s1.varindex * 5 + TYPE_INT];
3224 if ((copy->varkind == LOCALVAR) &&
3225 (copy->varnum == iptr->s1.varindex))
3227 assert(IS_LOCALVAR(copy));
3233 #if defined(ENABLE_SSA)
3237 iptr->dst.varindex = iptr->s1.varindex;
3240 /* pop 1 push 0 store */
3249 i = opcode - ICMD_ISTORE; /* type */
3250 javaindex = iptr->dst.varindex;
3251 j = iptr->dst.varindex =
3252 jd->local_map[javaindex * 5 + i];
3254 COPY_VAL_AND_TYPE(sd, curstack->varnum, j);
3256 iptr->sx.s23.s3.javaindex = javaindex;
3258 if (curstack->type == TYPE_RET) {
3259 iptr->flags.bits |= INS_FLAG_RETADDR;
3260 iptr->sx.s23.s2.retaddrnr =
3261 UNUSED - (1 + sd.var[j].vv.retaddr->nr);
3262 sd.javalocals[javaindex] = iptr->sx.s23.s2.retaddrnr;
3265 sd.javalocals[javaindex] = j;
3267 /* invalidate the following javalocal for 2-word types */
3269 if (IS_2_WORD_TYPE(i)) {
3270 sd.javalocals[javaindex+1] = UNUSED;
3271 iptr->flags.bits |= INS_FLAG_KILL_NEXT;
3274 /* invalidate 2-word types if second half was overwritten */
3276 if (javaindex > 0 && (i = sd.javalocals[javaindex-1]) != UNUSED) {
3277 if (IS_2_WORD_TYPE(sd.var[i].type)) {
3278 sd.javalocals[javaindex-1] = UNUSED;
3279 iptr->flags.bits |= INS_FLAG_KILL_PREV;
3283 #if defined(ENABLE_STATISTICS)
3286 i = sd.new - curstack;
3288 count_store_length[20]++;
3290 count_store_length[i]++;
3293 count_store_depth[10]++;
3295 count_store_depth[i]++;
3299 #if defined(ENABLE_SSA)
3302 /* check for conflicts as described in Figure 5.2 */
3304 copy = curstack->prev;
3307 if ((copy->varkind == LOCALVAR) &&
3308 (copy->varnum == j))
3310 copy->varkind = TEMPVAR;
3311 assert(IS_LOCALVAR(copy));
3318 /* if the variable is already coalesced, don't bother */
3320 /* We do not need to check against INOUT, as invars */
3321 /* are always before the coalescing boundary. */
3323 if (curstack->varkind == LOCALVAR)
3326 /* there is no STORE Lj while curstack is live */
3328 if (curstack < last_store_boundary[javaindex])
3329 goto assume_conflict;
3331 /* curstack must be after the coalescing boundary */
3333 if (curstack < coalescing_boundary)
3334 goto assume_conflict;
3336 /* there is no DEF LOCALVAR(j) while curstack is live */
3338 copy = sd.new; /* most recent stackslot created + 1 */
3339 while (--copy > curstack) {
3340 if (copy->varkind == LOCALVAR && copy->varnum == j)
3341 goto assume_conflict;
3344 /* coalesce the temporary variable with Lj */
3345 assert((curstack->varkind == TEMPVAR)
3346 || (curstack->varkind == UNDEFVAR));
3347 assert(!IS_LOCALVAR(curstack)); /* XXX correct? */
3348 assert(!IS_INOUT(curstack));
3349 assert(!IS_PREALLOC(curstack));
3351 assert(curstack->creator);
3352 assert(curstack->creator->dst.varindex == curstack->varnum);
3353 assert(!(curstack->flags & PASSTHROUGH));
3354 RELEASE_INDEX(sd, curstack);
3355 curstack->varkind = LOCALVAR;
3356 curstack->varnum = j;
3357 curstack->creator->dst.varindex = j;
3360 /* revert the coalescing, if it has been done earlier */
3362 if ((curstack->varkind == LOCALVAR)
3363 && (curstack->varnum == j))
3365 assert(IS_LOCALVAR(curstack));
3366 SET_TEMPVAR(curstack);
3369 /* remember the stack boundary at this store */
3371 last_store_boundary[javaindex] = sd.new;
3372 #if defined(ENABLE_SSA)
3373 } /* if (ls != NULL) */
3376 if (opcode == ICMD_ASTORE && curstack->type == TYPE_RET)
3379 STORE(opcode - ICMD_ISTORE, j);
3385 coalescing_boundary = sd.new;
3386 iptr->flags.bits |= INS_FLAG_CHECK;
3387 COUNT(count_check_null);
3388 COUNT(count_check_bound);
3389 COUNT(count_pcmd_mem);
3391 bte = builtintable_get_internal(BUILTIN_canstore);
3394 if (md->memuse > rd->memuse)
3395 rd->memuse = md->memuse;
3396 if (md->argintreguse > rd->argintreguse)
3397 rd->argintreguse = md->argintreguse;
3398 /* XXX non-leaf method? */
3400 /* make all stack variables saved */
3404 sd.var[copy->varnum].flags |= SAVEDVAR;
3405 /* in case copy->varnum is/will be a LOCALVAR */
3406 /* once and set back to a non LOCALVAR */
3407 /* the correct SAVEDVAR flag has to be */
3408 /* remembered in copy->flags, too */
3409 copy->flags |= SAVEDVAR;
3413 OP3_0(TYPE_ADR, TYPE_INT, TYPE_ADR);
3420 coalescing_boundary = sd.new;
3421 iptr->flags.bits |= INS_FLAG_CHECK;
3422 COUNT(count_check_null);
3423 COUNT(count_check_bound);
3424 COUNT(count_pcmd_mem);
3425 OP3_0(TYPE_ADR, TYPE_INT, opcode - ICMD_IASTORE);
3432 coalescing_boundary = sd.new;
3433 iptr->flags.bits |= INS_FLAG_CHECK;
3434 COUNT(count_check_null);
3435 COUNT(count_check_bound);
3436 COUNT(count_pcmd_mem);
3437 OP3_0(TYPE_ADR, TYPE_INT, TYPE_INT);
3443 #ifdef ENABLE_VERIFIER
3446 if (IS_2_WORD_TYPE(curstack->type))
3447 goto throw_stack_category_error;
3458 coalescing_boundary = sd.new;
3459 /* Assert here that no LOCAL or INOUTS get */
3460 /* preallocated, since tha macros are not */
3461 /* available in md-abi.c! */
3462 if (IS_TEMPVAR(curstack))
3463 md_return_alloc(jd, curstack);
3464 COUNT(count_pcmd_return);
3465 OP1_0(opcode - ICMD_IRETURN);
3466 superblockend = true;
3467 sd.jd->returncount++;
3468 sd.jd->returnblock = sd.bptr;
3472 coalescing_boundary = sd.new;
3473 COUNT(count_check_null);
3475 curstack = NULL; stackdepth = 0;
3476 superblockend = true;
3479 case ICMD_PUTSTATIC:
3480 coalescing_boundary = sd.new;
3481 COUNT(count_pcmd_mem);
3482 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3483 OP1_0(fmiref->parseddesc.fd->type);
3486 /* pop 1 push 0 branch */
3489 case ICMD_IFNONNULL:
3490 COUNT(count_pcmd_bra);
3491 OP1_BRANCH(TYPE_ADR);
3501 COUNT(count_pcmd_bra);
3502 /* iptr->sx.val.i is set implicitly in parse by
3503 clearing the memory or from IF_ICMPxx
3506 OP1_BRANCH(TYPE_INT);
3507 /* iptr->sx.val.i = 0; */
3511 /* pop 0 push 0 branch */
3514 COUNT(count_pcmd_bra);
3517 superblockend = true;
3520 /* pop 1 push 0 table branch */
3522 case ICMD_TABLESWITCH:
3523 COUNT(count_pcmd_table);
3524 OP1_BRANCH(TYPE_INT);
3526 table = iptr->dst.table;
3527 BRANCH_TARGET(*table, tbptr);
3530 i = iptr->sx.s23.s3.tablehigh
3531 - iptr->sx.s23.s2.tablelow + 1;
3534 BRANCH_TARGET(*table, tbptr);
3537 superblockend = true;
3540 /* pop 1 push 0 table branch */
3542 case ICMD_LOOKUPSWITCH:
3543 COUNT(count_pcmd_table);
3544 OP1_BRANCH(TYPE_INT);
3546 BRANCH_TARGET(iptr->sx.s23.s3.lookupdefault, tbptr);
3548 lookup = iptr->dst.lookup;
3550 i = iptr->sx.s23.s2.lookupcount;
3553 BRANCH_TARGET(lookup->target, tbptr);
3556 superblockend = true;
3559 case ICMD_MONITORENTER:
3560 case ICMD_MONITOREXIT:
3561 coalescing_boundary = sd.new;
3562 COUNT(count_check_null);
3566 /* pop 2 push 0 branch */
3568 case ICMD_IF_ICMPEQ:
3569 case ICMD_IF_ICMPNE:
3570 case ICMD_IF_ICMPLT:
3571 case ICMD_IF_ICMPGE:
3572 case ICMD_IF_ICMPGT:
3573 case ICMD_IF_ICMPLE:
3574 COUNT(count_pcmd_bra);
3575 OP2_BRANCH(TYPE_INT, TYPE_INT);
3579 case ICMD_IF_ACMPEQ:
3580 case ICMD_IF_ACMPNE:
3581 COUNT(count_pcmd_bra);
3582 OP2_BRANCH(TYPE_ADR, TYPE_ADR);
3589 coalescing_boundary = sd.new;
3590 COUNT(count_check_null);
3591 COUNT(count_pcmd_mem);
3592 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
3593 OP2_0(TYPE_ADR, fmiref->parseddesc.fd->type);
3598 if (!IS_2_WORD_TYPE(curstack->type)) {
3600 #ifdef ENABLE_VERIFIER
3603 if (IS_2_WORD_TYPE(curstack->prev->type))
3604 goto throw_stack_category_error;
3607 OP2_0_ANY_ANY; /* pop two slots */
3610 iptr->opc = ICMD_POP;
3611 OP1_0_ANY; /* pop one (two-word) slot */
3615 /* pop 0 push 1 dup */
3618 #ifdef ENABLE_VERIFIER
3621 if (IS_2_WORD_TYPE(curstack->type))
3622 goto throw_stack_category_error;
3625 COUNT(count_dup_instruction);
3631 coalescing_boundary = sd.new - 1;
3636 if (IS_2_WORD_TYPE(curstack->type)) {
3638 iptr->opc = ICMD_DUP;
3643 /* ..., ????, cat1 */
3644 #ifdef ENABLE_VERIFIER
3646 if (IS_2_WORD_TYPE(curstack->prev->type))
3647 goto throw_stack_category_error;
3650 src1 = curstack->prev;
3653 COPY_UP(src1); iptr++; len--;
3656 coalescing_boundary = sd.new;
3660 /* pop 2 push 3 dup */
3663 #ifdef ENABLE_VERIFIER
3666 if (IS_2_WORD_TYPE(curstack->type) ||
3667 IS_2_WORD_TYPE(curstack->prev->type))
3668 goto throw_stack_category_error;
3673 src1 = curstack->prev;
3678 /* move non-temporary sources out of the way */
3679 if (!IS_TEMPVAR(src2)) {
3680 MOVE_TO_TEMP(src2); iptr++; len--;
3683 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3685 MOVE_UP(src1); iptr++; len--;
3686 MOVE_UP(src2); iptr++; len--;
3688 COPY_DOWN(curstack, dst1);
3690 coalescing_boundary = sd.new;
3695 if (IS_2_WORD_TYPE(curstack->type)) {
3696 /* ..., ????, cat2 */
3697 #ifdef ENABLE_VERIFIER
3699 if (IS_2_WORD_TYPE(curstack->prev->type))
3700 goto throw_stack_category_error;
3703 iptr->opc = ICMD_DUP_X1;
3707 /* ..., ????, cat1 */
3708 #ifdef ENABLE_VERIFIER
3711 if (IS_2_WORD_TYPE(curstack->prev->type)
3712 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3713 goto throw_stack_category_error;
3718 src1 = curstack->prev->prev;
3719 src2 = curstack->prev;
3721 POPANY; POPANY; POPANY;
3724 /* move non-temporary sources out of the way */
3725 if (!IS_TEMPVAR(src2)) {
3726 MOVE_TO_TEMP(src2); iptr++; len--;
3728 if (!IS_TEMPVAR(src3)) {
3729 MOVE_TO_TEMP(src3); iptr++; len--;
3732 DUP_SLOT(src2); dst1 = curstack; stackdepth++;
3733 DUP_SLOT(src3); dst2 = curstack; stackdepth++;
3735 MOVE_UP(src1); iptr++; len--;
3736 MOVE_UP(src2); iptr++; len--;
3737 MOVE_UP(src3); iptr++; len--;
3739 COPY_DOWN(curstack, dst2); iptr++; len--;
3740 COPY_DOWN(curstack->prev, dst1);
3742 coalescing_boundary = sd.new;
3746 /* pop 3 push 4 dup */
3750 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3751 /* ..., cat2, ???? */
3752 #ifdef ENABLE_VERIFIER
3754 if (IS_2_WORD_TYPE(curstack->type))
3755 goto throw_stack_category_error;
3758 iptr->opc = ICMD_DUP_X1;
3762 /* ..., cat1, ???? */
3763 #ifdef ENABLE_VERIFIER
3766 if (IS_2_WORD_TYPE(curstack->type)
3767 || IS_2_WORD_TYPE(curstack->prev->prev->type))
3768 goto throw_stack_category_error;
3772 src1 = curstack->prev->prev;
3773 src2 = curstack->prev;
3775 POPANY; POPANY; POPANY;
3778 /* move non-temporary sources out of the way */
3779 if (!IS_TEMPVAR(src2)) {
3780 MOVE_TO_TEMP(src2); iptr++; len--;
3782 if (!IS_TEMPVAR(src3)) {
3783 MOVE_TO_TEMP(src3); iptr++; len--;
3786 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3788 MOVE_UP(src1); iptr++; len--;
3789 MOVE_UP(src2); iptr++; len--;
3790 MOVE_UP(src3); iptr++; len--;
3792 COPY_DOWN(curstack, dst1);
3794 coalescing_boundary = sd.new;
3800 if (IS_2_WORD_TYPE(curstack->type)) {
3801 /* ..., ????, cat2 */
3802 if (IS_2_WORD_TYPE(curstack->prev->type)) {
3803 /* ..., cat2, cat2 */
3804 iptr->opc = ICMD_DUP_X1;
3808 /* ..., cat1, cat2 */
3809 #ifdef ENABLE_VERIFIER
3812 if (IS_2_WORD_TYPE(curstack->prev->prev->type))
3813 goto throw_stack_category_error;
3816 iptr->opc = ICMD_DUP_X2;
3822 /* ..., ????, ????, cat1 */
3824 if (IS_2_WORD_TYPE(curstack->prev->prev->type)) {
3825 /* ..., cat2, ????, cat1 */
3826 #ifdef ENABLE_VERIFIER
3828 if (IS_2_WORD_TYPE(curstack->prev->type))
3829 goto throw_stack_category_error;
3832 iptr->opc = ICMD_DUP2_X1;
3836 /* ..., cat1, ????, cat1 */
3837 #ifdef ENABLE_VERIFIER
3840 if (IS_2_WORD_TYPE(curstack->prev->type)
3841 || IS_2_WORD_TYPE(curstack->prev->prev->prev->type))
3842 goto throw_stack_category_error;
3846 src1 = curstack->prev->prev->prev;
3847 src2 = curstack->prev->prev;
3848 src3 = curstack->prev;
3850 POPANY; POPANY; POPANY; POPANY;
3853 /* move non-temporary sources out of the way */
3854 if (!IS_TEMPVAR(src2)) {
3855 MOVE_TO_TEMP(src2); iptr++; len--;
3857 if (!IS_TEMPVAR(src3)) {
3858 MOVE_TO_TEMP(src3); iptr++; len--;
3860 if (!IS_TEMPVAR(src4)) {
3861 MOVE_TO_TEMP(src4); iptr++; len--;
3864 DUP_SLOT(src3); dst1 = curstack; stackdepth++;
3865 DUP_SLOT(src4); dst2 = curstack; stackdepth++;
3867 MOVE_UP(src1); iptr++; len--;
3868 MOVE_UP(src2); iptr++; len--;
3869 MOVE_UP(src3); iptr++; len--;
3870 MOVE_UP(src4); iptr++; len--;
3872 COPY_DOWN(curstack, dst2); iptr++; len--;
3873 COPY_DOWN(curstack->prev, dst1);
3875 coalescing_boundary = sd.new;
3879 /* pop 2 push 2 swap */
3882 #ifdef ENABLE_VERIFIER
3885 if (IS_2_WORD_TYPE(curstack->type)
3886 || IS_2_WORD_TYPE(curstack->prev->type))
3887 goto throw_stack_category_error;
3891 src1 = curstack->prev;
3896 /* move non-temporary sources out of the way */
3897 if (!IS_TEMPVAR(src1)) {
3898 MOVE_TO_TEMP(src1); iptr++; len--;
3901 MOVE_UP(src2); iptr++; len--;
3904 coalescing_boundary = sd.new;
3911 coalescing_boundary = sd.new;
3912 #if !SUPPORT_DIVISION
3913 bte = iptr->sx.s23.s3.bte;
3916 if (md->memuse > rd->memuse)
3917 rd->memuse = md->memuse;
3918 if (md->argintreguse > rd->argintreguse)
3919 rd->argintreguse = md->argintreguse;
3921 /* make all stack variables saved */
3925 sd.var[copy->varnum].flags |= SAVEDVAR;
3926 copy->flags |= SAVEDVAR;
3931 #endif /* !SUPPORT_DIVISION */
3942 COUNT(count_pcmd_op);
3943 OP2_1(TYPE_INT, TYPE_INT, TYPE_INT);
3948 coalescing_boundary = sd.new;
3949 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
3950 bte = iptr->sx.s23.s3.bte;
3953 if (md->memuse > rd->memuse)
3954 rd->memuse = md->memuse;
3955 if (md->argintreguse > rd->argintreguse)
3956 rd->argintreguse = md->argintreguse;
3957 /* XXX non-leaf method? */
3959 /* make all stack variables saved */
3963 sd.var[copy->varnum].flags |= SAVEDVAR;
3964 copy->flags |= SAVEDVAR;
3969 #endif /* !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV) */
3974 #if SUPPORT_LONG_LOGICAL
3978 #endif /* SUPPORT_LONG_LOGICAL */
3979 COUNT(count_pcmd_op);
3980 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_LNG);
3986 COUNT(count_pcmd_op);
3987 OP2_1(TYPE_LNG, TYPE_INT, TYPE_LNG);
3995 COUNT(count_pcmd_op);
3996 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_FLT);
4004 COUNT(count_pcmd_op);
4005 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_DBL);
4009 COUNT(count_pcmd_op);
4010 #if SUPPORT_LONG_CMP_CONST
4011 if ((len == 0) || (iptr[1].sx.val.i != 0))
4014 switch (iptr[1].opc) {
4016 iptr->opc = ICMD_IF_LCMPEQ;
4018 iptr->dst.insindex = iptr[1].dst.insindex;
4019 iptr[1].opc = ICMD_NOP;
4021 OP2_BRANCH(TYPE_LNG, TYPE_LNG);
4024 COUNT(count_pcmd_bra);
4027 iptr->opc = ICMD_IF_LCMPNE;
4028 goto icmd_lcmp_if_tail;
4030 iptr->opc = ICMD_IF_LCMPLT;
4031 goto icmd_lcmp_if_tail;
4033 iptr->opc = ICMD_IF_LCMPGT;
4034 goto icmd_lcmp_if_tail;
4036 iptr->opc = ICMD_IF_LCMPLE;
4037 goto icmd_lcmp_if_tail;
4039 iptr->opc = ICMD_IF_LCMPGE;
4040 goto icmd_lcmp_if_tail;
4046 #endif /* SUPPORT_LONG_CMP_CONST */
4047 OP2_1(TYPE_LNG, TYPE_LNG, TYPE_INT);
4050 /* XXX why is this deactivated? */
4053 COUNT(count_pcmd_op);
4054 if ((len == 0) || (iptr[1].sx.val.i != 0))
4057 switch (iptr[1].opc) {
4059 iptr->opc = ICMD_IF_FCMPEQ;
4061 iptr->dst.insindex = iptr[1].dst.insindex;
4062 iptr[1].opc = ICMD_NOP;
4064 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4067 COUNT(count_pcmd_bra);
4070 iptr->opc = ICMD_IF_FCMPNE;
4071 goto icmd_if_fcmpl_tail;
4073 iptr->opc = ICMD_IF_FCMPL_LT;
4074 goto icmd_if_fcmpl_tail;
4076 iptr->opc = ICMD_IF_FCMPL_GT;
4077 goto icmd_if_fcmpl_tail;
4079 iptr->opc = ICMD_IF_FCMPL_LE;
4080 goto icmd_if_fcmpl_tail;
4082 iptr->opc = ICMD_IF_FCMPL_GE;
4083 goto icmd_if_fcmpl_tail;
4090 OPTT2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4094 COUNT(count_pcmd_op);
4095 if ((len == 0) || (iptr[1].sx.val.i != 0))
4098 switch (iptr[1].opc) {
4100 iptr->opc = ICMD_IF_FCMPEQ;
4102 iptr->dst.insindex = iptr[1].dst.insindex;
4103 iptr[1].opc = ICMD_NOP;
4105 OP2_BRANCH(TYPE_FLT, TYPE_FLT);
4108 COUNT(count_pcmd_bra);
4111 iptr->opc = ICMD_IF_FCMPNE;
4112 goto icmd_if_fcmpg_tail;
4114 iptr->opc = ICMD_IF_FCMPG_LT;
4115 goto icmd_if_fcmpg_tail;
4117 iptr->opc = ICMD_IF_FCMPG_GT;
4118 goto icmd_if_fcmpg_tail;
4120 iptr->opc = ICMD_IF_FCMPG_LE;
4121 goto icmd_if_fcmpg_tail;
4123 iptr->opc = ICMD_IF_FCMPG_GE;
4124 goto icmd_if_fcmpg_tail;
4131 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4135 COUNT(count_pcmd_op);
4136 if ((len == 0) || (iptr[1].sx.val.i != 0))
4139 switch (iptr[1].opc) {
4141 iptr->opc = ICMD_IF_DCMPEQ;
4143 iptr->dst.insindex = iptr[1].dst.insindex;
4144 iptr[1].opc = ICMD_NOP;
4146 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4149 COUNT(count_pcmd_bra);
4152 iptr->opc = ICMD_IF_DCMPNE;
4153 goto icmd_if_dcmpl_tail;
4155 iptr->opc = ICMD_IF_DCMPL_LT;
4156 goto icmd_if_dcmpl_tail;
4158 iptr->opc = ICMD_IF_DCMPL_GT;
4159 goto icmd_if_dcmpl_tail;
4161 iptr->opc = ICMD_IF_DCMPL_LE;
4162 goto icmd_if_dcmpl_tail;
4164 iptr->opc = ICMD_IF_DCMPL_GE;
4165 goto icmd_if_dcmpl_tail;
4172 OPTT2_1(TYPE_DBL, TYPE_INT);
4176 COUNT(count_pcmd_op);
4177 if ((len == 0) || (iptr[1].sx.val.i != 0))
4180 switch (iptr[1].opc) {
4182 iptr->opc = ICMD_IF_DCMPEQ;
4184 iptr->dst.insindex = iptr[1].dst.insindex;
4185 iptr[1].opc = ICMD_NOP;
4187 OP2_BRANCH(TYPE_DBL, TYPE_DBL);
4190 COUNT(count_pcmd_bra);
4193 iptr->opc = ICMD_IF_DCMPNE;
4194 goto icmd_if_dcmpg_tail;
4196 iptr->opc = ICMD_IF_DCMPG_LT;
4197 goto icmd_if_dcmpg_tail;
4199 iptr->opc = ICMD_IF_DCMPG_GT;
4200 goto icmd_if_dcmpg_tail;
4202 iptr->opc = ICMD_IF_DCMPG_LE;
4203 goto icmd_if_dcmpg_tail;
4205 iptr->opc = ICMD_IF_DCMPG_GE;
4206 goto icmd_if_dcmpg_tail;
4213 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4218 COUNT(count_pcmd_op);
4219 OP2_1(TYPE_FLT, TYPE_FLT, TYPE_INT);
4224 COUNT(count_pcmd_op);
4225 OP2_1(TYPE_DBL, TYPE_DBL, TYPE_INT);
4234 case ICMD_INT2SHORT:
4235 COUNT(count_pcmd_op);
4236 OP1_1(TYPE_INT, TYPE_INT);
4239 COUNT(count_pcmd_op);
4240 OP1_1(TYPE_LNG, TYPE_LNG);
4243 COUNT(count_pcmd_op);
4244 OP1_1(TYPE_FLT, TYPE_FLT);
4247 COUNT(count_pcmd_op);
4248 OP1_1(TYPE_DBL, TYPE_DBL);
4252 COUNT(count_pcmd_op);
4253 OP1_1(TYPE_INT, TYPE_LNG);
4256 COUNT(count_pcmd_op);
4257 OP1_1(TYPE_INT, TYPE_FLT);
4260 COUNT(count_pcmd_op);
4261 OP1_1(TYPE_INT, TYPE_DBL);
4264 COUNT(count_pcmd_op);
4265 OP1_1(TYPE_LNG, TYPE_INT);
4268 COUNT(count_pcmd_op);
4269 OP1_1(TYPE_LNG, TYPE_FLT);
4272 COUNT(count_pcmd_op);
4273 OP1_1(TYPE_LNG, TYPE_DBL);
4276 COUNT(count_pcmd_op);
4277 OP1_1(TYPE_FLT, TYPE_INT);
4280 COUNT(count_pcmd_op);
4281 OP1_1(TYPE_FLT, TYPE_LNG);
4284 COUNT(count_pcmd_op);
4285 OP1_1(TYPE_FLT, TYPE_DBL);
4288 COUNT(count_pcmd_op);
4289 OP1_1(TYPE_DBL, TYPE_INT);
4292 COUNT(count_pcmd_op);
4293 OP1_1(TYPE_DBL, TYPE_LNG);
4296 COUNT(count_pcmd_op);
4297 OP1_1(TYPE_DBL, TYPE_FLT);
4300 case ICMD_CHECKCAST:
4301 coalescing_boundary = sd.new;
4302 if (iptr->flags.bits & INS_FLAG_ARRAY) {
4303 /* array type cast-check */
4305 bte = builtintable_get_internal(BUILTIN_arraycheckcast);
4308 if (md->memuse > rd->memuse)
4309 rd->memuse = md->memuse;
4310 if (md->argintreguse > rd->argintreguse)
4311 rd->argintreguse = md->argintreguse;
4313 /* make all stack variables saved */
4317 sd.var[copy->varnum].flags |= SAVEDVAR;
4318 copy->flags |= SAVEDVAR;
4322 OP1_1(TYPE_ADR, TYPE_ADR);
4325 case ICMD_INSTANCEOF:
4326 case ICMD_ARRAYLENGTH:
4327 coalescing_boundary = sd.new;
4328 OP1_1(TYPE_ADR, TYPE_INT);
4332 case ICMD_ANEWARRAY:
4333 coalescing_boundary = sd.new;
4334 OP1_1(TYPE_INT, TYPE_ADR);
4338 coalescing_boundary = sd.new;
4339 COUNT(count_check_null);
4340 COUNT(count_pcmd_mem);
4341 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4342 OP1_1(TYPE_ADR, fmiref->parseddesc.fd->type);
4347 case ICMD_GETSTATIC:
4348 coalescing_boundary = sd.new;
4349 COUNT(count_pcmd_mem);
4350 INSTRUCTION_GET_FIELDREF(iptr, fmiref);
4351 OP0_1(fmiref->parseddesc.fd->type);
4355 coalescing_boundary = sd.new;
4362 tbptr = BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex);
4363 tbptr->type = BBTYPE_SBR;
4365 assert(sd.bptr->next); /* XXX exception */
4366 sd.var[curstack->varnum].vv.retaddr = sd.bptr->next;
4367 #if defined(ENABLE_VERIFIER)
4368 sd.var[curstack->varnum].SBRSTART = (void*) tbptr;
4371 tbptr = stack_mark_reached(&sd, tbptr, curstack, stackdepth);
4375 iptr->sx.s23.s3.jsrtarget.block = tbptr;
4377 /* We need to check for overflow right here because
4378 * the pushed value is poped afterwards */
4381 superblockend = true;
4382 /* XXX should not be marked as interface, as it does not need to be */
4383 /* allocated. Same for the invar of the target. */
4386 /* pop many push any */
4390 bte = iptr->sx.s23.s3.bte;
4394 case ICMD_INVOKESTATIC:
4395 case ICMD_INVOKESPECIAL:
4396 case ICMD_INVOKEVIRTUAL:
4397 case ICMD_INVOKEINTERFACE:
4398 COUNT(count_pcmd_met);
4400 /* Check for functions to replace with builtin
4403 if (builtintable_replace_function(iptr))
4406 INSTRUCTION_GET_METHODDESC(iptr, md);
4407 /* XXX resurrect this COUNT? */
4408 /* if (lm->flags & ACC_STATIC) */
4409 /* {COUNT(count_check_null);} */
4413 coalescing_boundary = sd.new;
4417 if (md->memuse > rd->memuse)
4418 rd->memuse = md->memuse;
4419 if (md->argintreguse > rd->argintreguse)
4420 rd->argintreguse = md->argintreguse;
4421 if (md->argfltreguse > rd->argfltreguse)
4422 rd->argfltreguse = md->argfltreguse;
4426 iptr->s1.argcount = stackdepth;
4427 iptr->sx.s23.s2.args = DMNEW(s4, stackdepth);
4430 for (i-- ; i >= 0; i--) {
4431 iptr->sx.s23.s2.args[i] = copy->varnum;
4433 /* do not change STACKVARs or LOCALVARS to ARGVAR*/
4434 /* -> won't help anyway */
4435 if (!(IS_INOUT(copy) || IS_LOCALVAR(copy))) {
4437 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4438 /* If we pass float arguments in integer argument registers, we
4439 * are not allowed to precolor them here. Floats have to be moved
4440 * to this regs explicitly in codegen().
4441 * Only arguments that are passed by stack anyway can be precolored
4442 * (michi 2005/07/24) */
4443 if (!(sd.var[copy->varnum].flags & SAVEDVAR) &&
4444 (!IS_FLT_DBL_TYPE(copy->type)
4445 || md->params[i].inmemory)) {
4447 if (!(sd.var[copy->varnum].flags & SAVEDVAR)) {
4452 if (md->params[i].inmemory) {
4453 sd.var[copy->varnum].vv.regoff =
4454 md->params[i].regoff;
4455 sd.var[copy->varnum].flags |=
4459 if (IS_FLT_DBL_TYPE(copy->type)) {
4460 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
4461 assert(0); /* XXX is this assert ok? */
4463 sd.var[copy->varnum].vv.regoff =
4464 rd->argfltregs[md->params[i].regoff];
4465 #endif /* SUPPORT_PASS_FLOATARGS_IN_INTREGS */
4468 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
4469 if (IS_2_WORD_TYPE(copy->type))
4470 sd.var[copy->varnum].vv.regoff =
4471 PACK_REGS( rd->argintregs[GET_LOW_REG(md->params[i].regoff)],
4472 rd->argintregs[GET_HIGH_REG(md->params[i].regoff)]);
4475 #endif /* SUPPORT_COMBINE_INTEGER_REGISTERS */
4476 sd.var[copy->varnum].vv.regoff =
4477 rd->argintregs[md->params[i].regoff];
4485 /* deal with live-through stack slots "under" the */
4491 iptr->sx.s23.s2.args[i++] = copy->varnum;
4492 sd.var[copy->varnum].flags |= SAVEDVAR;
4493 copy->flags |= SAVEDVAR | PASSTHROUGH;
4497 /* pop the arguments */
4506 /* push the return value */
4508 if (md->returntype.type != TYPE_VOID) {
4509 GET_NEW_VAR(sd, new_index, md->returntype.type);
4510 DST(md->returntype.type, new_index);
4515 case ICMD_MULTIANEWARRAY:
4516 coalescing_boundary = sd.new;
4517 if (rd->argintreguse < MIN(3, INT_ARG_CNT))
4518 rd->argintreguse = MIN(3, INT_ARG_CNT);
4520 i = iptr->s1.argcount;
4524 iptr->sx.s23.s2.args = DMNEW(s4, i);
4526 #if defined(SPECIALMEMUSE)
4527 # if defined(__DARWIN__)
4528 if (rd->memuse < (i + INT_ARG_CNT +LA_SIZE_IN_POINTERS))
4529 rd->memuse = i + LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4531 if (rd->memuse < (i + LA_SIZE_IN_POINTERS + 3))
4532 rd->memuse = i + LA_SIZE_IN_POINTERS + 3;
4535 # if defined(__I386__)
4536 if (rd->memuse < i + 3)
4537 rd->memuse = i + 3; /* n integer args spilled on stack */
4538 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4539 if (rd->memuse < i + 2)
4540 rd->memuse = i + 2; /* 4*4 bytes callee save space */
4543 rd->memuse = i; /* n integer args spilled on stack */
4544 # endif /* defined(__I386__) */
4548 /* check INT type here? Currently typecheck does this. */
4549 iptr->sx.s23.s2.args[i] = copy->varnum;
4550 if (!(sd.var[copy->varnum].flags & SAVEDVAR)
4551 && (!IS_INOUT(copy))
4552 && (!IS_LOCALVAR(copy)) ) {
4553 copy->varkind = ARGVAR;
4554 sd.var[copy->varnum].flags |=
4555 INMEMORY & PREALLOC;
4556 #if defined(SPECIALMEMUSE)
4557 # if defined(__DARWIN__)
4558 sd.var[copy->varnum].vv.regoff = i +
4559 LA_SIZE_IN_POINTERS + INT_ARG_CNT;
4561 sd.var[copy->varnum].vv.regoff = i +
4562 LA_SIZE_IN_POINTERS + 3;
4565 # if defined(__I386__)
4566 sd.var[copy->varnum].vv.regoff = i + 3;
4567 # elif defined(__MIPS__) && SIZEOF_VOID_P == 4
4568 sd.var[copy->varnum].vv.regoff = i + 2;
4570 sd.var[copy->varnum].vv.regoff = i;
4571 # endif /* defined(__I386__) */
4572 #endif /* defined(SPECIALMEMUSE) */
4577 sd.var[copy->varnum].flags |= SAVEDVAR;
4578 copy->flags |= SAVEDVAR;
4582 i = iptr->s1.argcount;
4587 GET_NEW_VAR(sd, new_index, TYPE_ADR);
4588 DST(TYPE_ADR, new_index);
4593 exceptions_throw_internalerror("Unknown ICMD %d during stack analysis",
4600 } /* while instructions */
4602 /* show state after last instruction */
4604 #if defined(STACK_VERBOSE)
4605 stack_verbose_show_state(&sd, NULL, curstack);
4608 /* stack slots at basic block end become interfaces */
4610 sd.bptr->outdepth = stackdepth;
4611 sd.bptr->outvars = DMNEW(s4, stackdepth);
4614 for (copy = curstack; copy; i--, copy = copy->prev) {
4618 /* with the new vars rd->interfaces will be removed */
4619 /* and all in and outvars have to be STACKVARS! */
4620 /* in the moment i.e. SWAP with in and out vars can */
4621 /* create an unresolvable conflict */
4626 v = sd.var + copy->varnum;
4629 /* do not allocate variables for returnAddresses */
4631 if (t != TYPE_RET) {
4632 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4633 /* no interface var until now for this depth and */
4635 jd->interface_map[i*5 + t].flags = v->flags;
4638 jd->interface_map[i*5 + t].flags |= v->flags;
4642 sd.bptr->outvars[i] = copy->varnum;
4645 /* check if interface slots at basic block begin must be saved */
4647 for (i=0; i<sd.bptr->indepth; ++i) {
4648 varinfo *v = sd.var + sd.bptr->invars[i];
4653 if (t != TYPE_RET) {
4654 if (jd->interface_map[i*5 + t].flags == UNUSED) {
4655 /* no interface var until now for this depth and */
4657 jd->interface_map[i*5 + t].flags = v->flags;
4660 jd->interface_map[i*5 + t].flags |= v->flags;
4665 /* store the number of this block's variables */
4667 sd.bptr->varcount = sd.vartop - sd.bptr->varstart;
4669 #if defined(STACK_VERBOSE)
4670 stack_verbose_block_exit(&sd, superblockend);
4673 /* reach the following block, if any */
4676 if (!stack_reach_next_block(&sd))
4681 } while (sd.repeat && !deadcode);
4683 /* reset locals of TYPE_RET|VOID to TYPE_ADR */
4685 /* A local variable may be used as both a returnAddress and a reference */
4686 /* type variable, as we do not split variables between these types when */
4687 /* renaming locals. While returnAddresses have been eliminated now, we */
4688 /* must assume that the variable is still used as TYPE_ADR. */
4689 /* The only way that a local can be TYPE_VOID at this point, is that it */
4690 /* was a TYPE_RET variable for which incompatible returnAddresses were */
4691 /* merged. Thus we must treat TYPE_VOID in the same way as TYPE_RET */
4693 /* XXX: It would be nice to remove otherwise unused returnAddress */
4694 /* variables from the local variable array, so they are not */
4695 /* allocated by simplereg. (For LSRA this is not needed). */
4697 for (i=0; i<sd.localcount; ++i) {
4698 if (sd.var[i].type == TYPE_RET || sd.var[i].type == TYPE_VOID)
4699 sd.var[i].type = TYPE_ADR;
4702 /* mark temporaries of TYPE_RET as PREALLOC to avoid allocation */
4704 for (i=sd.localcount; i<sd.vartop; ++i) {
4705 if (sd.var[i].type == TYPE_RET)
4706 sd.var[i].flags |= PREALLOC;
4709 /* XXX hack to fix up the ranges of the cloned single-block handlers */
4711 ex = jd->exceptiontable;
4712 for (; ex != NULL; ex = ex->down) {
4713 if (ex->start == ex->end) {
4714 assert(ex->end->next);
4715 ex->end = ex->end->next;
4719 /* store number of created variables */
4721 jd->vartop = sd.vartop;
4723 /* gather statistics *****************************************************/
4725 #if defined(ENABLE_STATISTICS)
4727 if (jd->basicblockcount > count_max_basic_blocks)
4728 count_max_basic_blocks = jd->basicblockcount;
4729 count_basic_blocks += jd->basicblockcount;
4730 if (jd->instructioncount > count_max_javainstr)
4731 count_max_javainstr = jd->instructioncount;
4732 count_javainstr += jd->instructioncount;
4733 if (jd->stackcount > count_upper_bound_new_stack)
4734 count_upper_bound_new_stack = jd->stackcount;
4735 if ((sd.new - jd->stack) > count_max_new_stack)
4736 count_max_new_stack = (sd.new - jd->stack);
4738 sd.bptr = jd->basicblocks;
4739 for (; sd.bptr; sd.bptr = sd.bptr->next) {
4740 if (sd.bptr->flags > BBREACHED) {
4741 if (sd.bptr->indepth >= 10)
4742 count_block_stack[10]++;
4744 count_block_stack[sd.bptr->indepth]++;
4745 len = sd.bptr->icount;
4747 count_block_size_distribution[len]++;
4749 count_block_size_distribution[10]++;
4751 count_block_size_distribution[11]++;
4753 count_block_size_distribution[12]++;
4755 count_block_size_distribution[13]++;
4757 count_block_size_distribution[14]++;
4759 count_block_size_distribution[15]++;
4761 count_block_size_distribution[16]++;
4763 count_block_size_distribution[17]++;
4767 if (iteration_count == 1)
4768 count_analyse_iterations[0]++;
4769 else if (iteration_count == 2)
4770 count_analyse_iterations[1]++;
4771 else if (iteration_count == 3)
4772 count_analyse_iterations[2]++;
4773 else if (iteration_count == 4)
4774 count_analyse_iterations[3]++;
4776 count_analyse_iterations[4]++;
4778 if (jd->basicblockcount <= 5)
4779 count_method_bb_distribution[0]++;
4780 else if (jd->basicblockcount <= 10)
4781 count_method_bb_distribution[1]++;
4782 else if (jd->basicblockcount <= 15)
4783 count_method_bb_distribution[2]++;
4784 else if (jd->basicblockcount <= 20)
4785 count_method_bb_distribution[3]++;
4786 else if (jd->basicblockcount <= 30)
4787 count_method_bb_distribution[4]++;
4788 else if (jd->basicblockcount <= 40)
4789 count_method_bb_distribution[5]++;
4790 else if (jd->basicblockcount <= 50)
4791 count_method_bb_distribution[6]++;
4792 else if (jd->basicblockcount <= 75)
4793 count_method_bb_distribution[7]++;
4795 count_method_bb_distribution[8]++;
4797 #endif /* defined(ENABLE_STATISTICS) */
4799 /* everything's ok *******************************************************/
4803 /* goto labels for throwing verifier exceptions **************************/
4805 #if defined(ENABLE_VERIFIER)
4807 throw_stack_underflow:
4808 exceptions_throw_verifyerror(m, "Unable to pop operand off an empty stack");
4811 throw_stack_overflow:
4812 exceptions_throw_verifyerror(m, "Stack size too large");
4815 throw_stack_type_error:
4816 exceptions_throw_verifyerror_for_stack(m, expectedtype);
4819 throw_stack_category_error:
4820 exceptions_throw_verifyerror(m, "Attempt to split long or double on the stack");
4827 /* stack_javalocals_store ******************************************************
4829 Model the effect of a ?STORE instruction upon the given javalocals array.
4832 iptr.............the ?STORE instruction
4833 javalocals.......the javalocals array to modify
4835 *******************************************************************************/
4837 void stack_javalocals_store(instruction *iptr, s4 *javalocals)
4839 s4 idx; /* index into the jd->var array */
4840 s4 j; /* java local index */
4842 idx = iptr->dst.varindex;
4843 j = iptr->sx.s23.s3.javaindex;
4846 if (iptr->flags.bits & INS_FLAG_RETADDR)
4847 javalocals[j] = iptr->sx.s23.s2.retaddrnr;
4849 javalocals[j] = idx;
4851 if (iptr->flags.bits & INS_FLAG_KILL_PREV)
4852 javalocals[j-1] = UNUSED;
4854 if (iptr->flags.bits & INS_FLAG_KILL_NEXT)
4855 javalocals[j+1] = UNUSED;
4860 /* functions for verbose stack analysis output ********************************/
4862 #if defined(STACK_VERBOSE)
4863 static void stack_verbose_show_varinfo(stackdata_t *sd, varinfo *v)
4865 printf("%c", show_jit_type_letters[v->type]);
4866 if (v->type == TYPE_RET)
4867 printf("{L%03d}", v->vv.retaddr->nr);
4871 static void stack_verbose_show_variable(stackdata_t *sd, s4 index)
4873 assert(index >= 0 && index < sd->vartop);
4874 stack_verbose_show_varinfo(sd, sd->var + index);
4878 static void stack_verbose_show_block(stackdata_t *sd, basicblock *bptr)
4882 printf("L%03d type:%d in:%d [", bptr->nr, bptr->type, bptr->indepth);
4884 for (i=0; i<bptr->indepth; ++i) {
4887 stack_verbose_show_variable(sd, bptr->invars[i]);
4892 printf("] javalocals ");
4893 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4894 printf(" inlocals [");
4895 if (bptr->inlocals) {
4896 for (i=0; i<sd->localcount; ++i) {
4899 stack_verbose_show_varinfo(sd, bptr->inlocals + i);
4904 printf("] out:%d [", bptr->outdepth);
4905 if (bptr->outvars) {
4906 for (i=0; i<bptr->outdepth; ++i) {
4909 stack_verbose_show_variable(sd, bptr->outvars[i]);
4917 printf(" (clone of L%03d)", bptr->original->nr);
4919 basicblock *b = bptr->copied_to;
4921 printf(" (copied to ");
4922 for (; b; b = b->copied_to)
4923 printf("L%03d ", b->nr);
4930 static void stack_verbose_block_enter(stackdata_t *sd, bool reanalyse)
4934 printf("======================================== STACK %sANALYSE BLOCK ",
4935 (reanalyse) ? ((sd->bptr->iinstr == NULL) ? "CLONE-" : "RE-") : "");
4936 stack_verbose_show_block(sd, sd->bptr);
4939 if (sd->handlers[0]) {
4940 printf("HANDLERS: ");
4941 for (i=0; sd->handlers[i]; ++i) {
4942 printf("L%03d ", sd->handlers[i]->handler->nr);
4950 static void stack_verbose_block_exit(stackdata_t *sd, bool superblockend)
4952 printf("%s ", (superblockend) ? "SUPERBLOCKEND" : "END");
4953 stack_verbose_show_block(sd, sd->bptr);
4957 static void stack_verbose_show_state(stackdata_t *sd, instruction *iptr, stackptr curstack)
4965 printf(" javalocals ");
4966 show_javalocals_array(sd->jd, sd->javalocals, sd->maxlocals, SHOW_STACK);
4969 for(i = 0, sp = curstack; sp; sp = sp->prev)
4973 stack = MNEW(stackptr, depth);
4974 for(sp = curstack; sp; sp = sp->prev)
4977 for(i=0; i<depth; ++i) {
4981 v = &(sd->var[sp->varnum]);
4983 if (v->flags & INOUT)
4985 if (v->flags & PREALLOC)
4987 printf("%d:%c", sp->varnum, show_jit_type_letters[sp->type]);
4988 if (v->type == TYPE_RET) {
4989 printf("(L%03d)", v->vv.retaddr->nr);
4994 show_icmd(sd->jd, iptr, false, SHOW_PARSE);
5001 * These are local overrides for various environment variables in Emacs.
5002 * Please do not remove this and leave it at the end of the file, where
5003 * Emacs will automagically detect them.
5004 * ---------------------------------------------------------------------
5007 * indent-tabs-mode: t
5011 * vim:noexpandtab:sw=4:ts=4: