1 /* src/vm/optimizing/ssa3.c
4 CACAOVM - Verein zu Foerderung der freien virtuellen Machine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 SSA transformation PROTOTYPE based on:
26 Adding Static Single Assignment Form and a Graph Coloring Register
27 Allocator to the Java Hotspot Client Compiler, 2000
28 (http://www.ssw.uni-linz.ac.at/Research/Reports/Report15.html)
32 * Adapt for exception handling [done]
33 * Eliminiation of redundand PHI functions. [in progress]
34 * Handle also inout variables. [done]
35 * Create PHI functions lazyly, when they are used for the first time
36 (I suspect that currently PHIs are created that are never used).
37 * REWRITE. The code was never designed for producion. [done]
38 * Unify access to phi args.
41 #include "vm/jit/jit.h"
42 #include "vm/global.h"
43 #include "mm/memory.h"
44 #include "mm/dumpmemory.h"
45 #include "toolbox/list.h"
47 <<<<<<< /data3/hg/src/vm/jit/optimizing/ssa3.c.orig.39076766
51 #define ELIMINATE_NOP_LOAD_STORE
54 /* #define SSA_VERBOSE */
57 __attribute__((always_inline))
60 /*** phi ********************************************************************/
64 PHI_FLAG_REDUNDANT_ALL,
65 PHI_FLAG_REDUNDANT_ONE
68 static inline void phi_set_flag(instruction *iptr, phi_flags_t flag) {
69 iptr->flags.bits |= (1 << flag);
72 static inline void phi_clear_flag(instruction *iptr, phi_flags_t flag) {
73 iptr->flags.bits &= ~(1 << flag);
76 static inline bool phi_has_flag(const instruction *iptr, phi_flags_t flag) {
77 return (iptr->flags.bits & (1 << flag)) != 0;
80 static inline instruction *phi_get_subst(instruction *iptr) {
81 return iptr->sx.s23.s2.iargs[-1];
84 static inline bool phi_has_subst(const instruction *iptr) {
85 return (iptr->sx.s23.s2.iargs[-1] != NULL);
89 static inline void phi_init(instruction *iptr, unsigned argcount, s4 index) {
94 iptr->dst.varindex = 0;
95 iptr->s1.argcount = argcount;
96 iptr->sx.s23.s2.iargs = DMNEW(instruction *, argcount + 1);
97 iptr->sx.s23.s2.iargs += 1;
98 iptr->sx.s23.s3.javaindex = index;
100 /* subst field - If non-null, the phi function shall be replaced by the
101 value produced by the subst instruction. */
102 iptr->sx.s23.s2.iargs[-1] = NULL;
105 for (i = 0; i < argcount; ++i) {
106 iptr->sx.s23.s2.iargs[i] = (instruction *)0x7fffffff;
111 #define phi_assert_opc(iptr) assert(iptr->opc == ICMD_PHI)
113 #define phi_assert_arg(iptr, arg) assert(arg < iptr->s1.argcount)
115 static inline s4 phi_arg_count(const instruction *iptr) {
116 phi_assert_opc(iptr);
117 return iptr->s1.argcount;
120 static inline instruction *phi_get_arg(const instruction *iptr, unsigned arg) {
121 phi_assert_opc(iptr);
122 phi_assert_arg(iptr, arg);
123 return iptr->sx.s23.s2.iargs[arg];
126 static inline s4 phi_get_arg_var(const instruction *iptr, unsigned arg) {
127 phi_assert_opc(iptr);
128 phi_assert_arg(iptr, arg);
129 return iptr->sx.s23.s2.iargs[arg]->dst.varindex;
132 static inline void phi_set_all_args(instruction *iptr, instruction *value) {
134 phi_assert_opc(iptr);
135 for (i = 0; i < iptr->s1.argcount; ++i) {
136 iptr->sx.s23.s2.iargs[i] = value;
140 static inline s4 phi_get_index(const instruction *iptr) {
141 phi_assert_opc(iptr);
142 return iptr->sx.s23.s3.javaindex;
145 static inline s4 phi_get_dst(const instruction *iptr) {
146 phi_assert_opc(iptr);
147 return iptr->dst.varindex;
150 static inline void phi_set_dst(instruction *iptr, s4 dst) {
151 phi_assert_opc(iptr);
152 iptr->dst.varindex = dst;
155 static inline bool phi_get_used(const instruction *iptr) {
156 phi_assert_opc(iptr);
157 return phi_has_flag(iptr, PHI_FLAG_USED);
160 static void phi_set_used(instruction *iptr) {
161 phi_assert_opc(iptr);
162 if (! phi_has_flag(iptr, PHI_FLAG_USED)) {
163 phi_set_flag(iptr, PHI_FLAG_USED);
164 /* TODO recurse into arguments */
168 static instruction *phi_resolve_use(instruction *use) {
170 while (use->opc == ICMD_PHI) {
171 if (phi_has_subst(use)) {
172 use = phi_get_subst(use);
181 static inline void phi_set_arg(instruction *iptr, unsigned arg, instruction *value) {
182 phi_assert_opc(iptr);
183 phi_assert_arg(iptr, arg);
184 assert(value != NULL);
185 iptr->sx.s23.s2.iargs[arg] = value;
188 static inline bool phi_is_redundant(const instruction *iptr) {
190 phi_has_flag(iptr, PHI_FLAG_REDUNDANT_ONE) ||
191 phi_has_flag(iptr, PHI_FLAG_REDUNDANT_ALL)
195 static inline void phi_create_copy(instruction *iptr, unsigned arg, instruction *copy) {
196 phi_assert_opc(iptr);
197 phi_assert_arg(iptr, arg);
198 copy->dst.varindex = phi_get_dst(iptr);
199 copy->s1.varindex = phi_resolve_use(phi_get_arg(iptr, arg))->dst.varindex;
200 if (copy->dst.varindex == copy->s1.varindex || phi_is_redundant(iptr)) {
201 copy->opc = ICMD_NOP;
203 copy->opc = ICMD_COPY;
208 static void phi_print(const instruction *iptr) {
211 printf("%d = phi(", iptr->dst.varindex);
212 for (i = 0; i < iptr->s1.argcount; ++i) {
213 use = phi_resolve_use(iptr->sx.s23.s2.iargs[i]);
215 printf("%d, ", use->dst.varindex);
224 #define FOR_EACH_PHI_USE_CAST(iptr, it, cast) \
226 (it) = cast (iptr)->sx.s23.s2.iargs; \
227 (it) != cast (iptr)->sx.s23.s2.iargs + (iptr)->s1.argcount; \
231 #define FOR_EACH_PHI_USE(iptr, it) \
232 FOR_EACH_PHI_USE_CAST(iptr, it, )
234 #define FOR_EACH_PHI_USE_CONST(iptr, it) \
235 FOR_EACH_PHI_USE_CAST(iptr, it, (const instruction *))
237 static void phi_calculate_redundancy(instruction *iptr) {
239 s4 dst = iptr->dst.varindex;
243 unsigned num_different = 0;
244 instruction *different;
246 if (phi_is_redundant(iptr)) return; /* XXX */
248 /* x = phi(x, y, x, x) ... PHI_FLAG_REDUNDANT_ONE
249 x = phi(x, x, x, x) ... PHI_FLAG_REDUNDANT_ALL */
251 FOR_EACH_PHI_USE(iptr, ituse) {
252 iuse = phi_resolve_use(*ituse);
255 use = iuse->dst.varindex;
260 if (num_different >= 2) {
261 phi_clear_flag(iptr, PHI_FLAG_REDUNDANT_ONE);
262 phi_clear_flag(iptr, PHI_FLAG_REDUNDANT_ALL);
267 if (num_different == 1) {
268 /* Set the subst field of the instruction.
269 I.e. the instruction will be replaced by the value produced by y. */
270 iptr->sx.s23.s2.iargs[-1] = different;
272 phi_set_flag(iptr, PHI_FLAG_REDUNDANT_ONE);
273 phi_clear_flag(iptr, PHI_FLAG_REDUNDANT_ALL);
274 } else if (num_different == 0) {
276 /*iptr->sx.s23.s2.iargs[-1] = iptr->sx.s23.s2.iargs[0];*/
278 phi_clear_flag(iptr, PHI_FLAG_REDUNDANT_ONE);
279 phi_clear_flag(iptr, PHI_FLAG_REDUNDANT_ALL);
285 /*** goto *******************************************************************/
287 static inline void goto_init(instruction *iptr, basicblock *dst) {
288 iptr->opc = ICMD_GOTO;
289 iptr->dst.block = dst;
292 /*** instruction ***********************************************************/
294 static void instruction_get_uses(const instruction *iptr, s4 *buf, s4 **puses, unsigned *puses_count) {
295 unsigned uses_count = 0;
297 switch (icmd_table[iptr->opc].dataflow) {
300 buf[2] = iptr->sx.s23.s3.varindex;
305 buf[1] = iptr->sx.s23.s2.varindex;
312 buf[0] = iptr->s1.varindex;
315 *puses_count = uses_count;
323 *puses = iptr->sx.s23.s2.args;
324 *puses_count = iptr->s1.argcount;
334 static inline void instruction_set_uses(instruction *iptr, s4 *buf, s4 *uses, unsigned uses_count) {
336 switch (uses_count) {
338 iptr->sx.s23.s3.varindex = buf[2];
340 iptr->sx.s23.s2.varindex = buf[1];
342 iptr->s1.varindex = buf[0];
347 /*** vars *******************************************************************/
349 #define VARS_CATEGORY_SHIFT 29
350 #define VARS_INDEX_MASK 0x1FFFFFFF
352 #define VARS_CATEGORY_LOCAL 0
353 #define VARS_CATEGORY_STACK 1
354 #define VARS_CATEGORY_OTHERS 2
356 #define VAR_TYPE_SUBSTITUED 666
359 varinfo items[9000]; /* XXX hardcoded max */
365 static inline unsigned vars_add_item(vars_t *vs, const varinfo *item) {
366 unsigned i = vs->count;
369 vs->items[i] = *item;
370 return (vs->category << VARS_CATEGORY_SHIFT) | i;
373 static inline unsigned vars_add(vars_t *vs) {
374 unsigned i = vs->count;
377 return (vs->category << VARS_CATEGORY_SHIFT) | i;
380 static inline varinfo *vars_back(vars_t *vs) {
381 assert(vs->count > 0);
382 return vs->items + (vs->count - 1);
385 static inline void vars_init(vars_t *vs, unsigned category) {
386 vs->max = sizeof(vs->items) / sizeof(vs->items[0]);
388 assert((category & 0x3) == category);
389 vs->category = category;
392 static inline unsigned vars_get_category(unsigned varindex) {
393 return (varindex >> VARS_CATEGORY_SHIFT);
396 static inline unsigned vars_get_index(unsigned varindex) {
397 return (varindex & VARS_INDEX_MASK);
400 static void vars_subst(vars_t *vs, unsigned varindex, unsigned replacementindex) {
401 varindex = vars_get_index(varindex);
402 replacementindex = vars_get_index(replacementindex);
404 vs->items[varindex].type = VAR_TYPE_SUBSTITUED;
405 vs->items[varindex].vv.regoff = replacementindex;
408 static unsigned vars_resolve_subst(const vars_t *vs, unsigned varindex) {
410 unsigned loop_ctr = 0;
412 varindex = vars_get_index(varindex);
414 if (vs->items[varindex].type == VAR_TYPE_SUBSTITUED) /*fprintf(stderr, "*")*/;
416 while (vs->items[varindex].type == VAR_TYPE_SUBSTITUED) {
417 assert(loop_ctr++ != vs->count);
418 varindex = vs->items[varindex].vv.regoff;
421 return (vs->category << VARS_CATEGORY_SHIFT) | varindex ;
424 static void vars_copy_to_final(vars_t *vs, varinfo *dst) {
428 for (it = vs->items; it != vs->items + vs->count; ++it, ++dst) {
434 /* Eliminate VAR_TYPE_SUBSTITUED as it leads to problems. */
436 if (dst->type == VAR_TYPE_SUBSTITUED) {
437 subst = vars_get_index(vars_resolve_subst(vs, it - vs->items));
438 dst->type = vs->items[subst].type;
444 /*** phis *******************************************************************/
452 static inline void phis_init(phis_t *ps, unsigned max) {
455 ps->items = DMNEW(instruction, max);
458 static inline instruction *phis_add(phis_t *ps) {
459 unsigned i = ps->count;
462 return ps->items + i;
465 static inline instruction *phis_get(const phis_t *ps, unsigned i) {
466 assert(i < ps->count);
467 return ps->items + i;
470 static inline bool phis_contains(const phis_t *ps, const instruction *phi) {
471 return (ps->items <= phi) && (phi < (ps->items + ps->max));
474 #define FOR_EACH_PHI_FUNCTION_(ps, it) \
475 for ((it) = (ps)->items; (it) != (ps)->items + (ps)->count; ++(it)) \
477 #define FOR_EACH_PHI_FUNCTION(ps, it) \
478 FOR_EACH_PHI_FUNCTION_(ps, it) if (!phi_is_redundant((it)))
481 FIXME() inline void phis_print(const phis_t *ps) {
482 const instruction *iptr;
483 FOR_EACH_PHI_FUNCTION(ps, iptr) {
489 /*** state_array ************************************************************/
496 static inline void state_array_init(state_array_t *sa, unsigned count) {
501 static inline bool state_array_has_items(const state_array_t *sa) {
502 return (sa->items != NULL);
505 static inline s4 state_array_get_var(const state_array_t *sa, unsigned index) {
506 assert(index < sa->count);
507 assert(sa->items[index]);
508 return sa->items[index]->dst.varindex;
511 static inline instruction *state_array_get(const state_array_t *sa, unsigned index) {
512 assert(index < sa->count);
513 return sa->items[index];
516 static inline void state_array_set(const state_array_t *sa, unsigned index, instruction *value) {
517 assert(index < sa->count);
518 sa->items[index] = value;
521 static inline void state_array_copy(state_array_t *sa, state_array_t *other) {
522 assert(sa->count == other->count);
523 MCOPY(sa->items, other->items, instruction *, sa->count);
526 #define state_array_assert_items(sa) assert(state_array_has_items(sa) || (sa->count == 0))
527 #define state_array_assert_no_items(sa) assert(! state_array_has_items(sa))
529 static inline void state_array_allocate_items(state_array_t *sa) {
530 sa->items = DMNEW(instruction *, sa->count);
531 MZERO(sa->items, instruction *, sa->count);
534 /*** basicblock_chain *******************************************************/
539 } basicblock_chain_t;
541 static void basicblock_chain_init(basicblock_chain_t *bbc) {
546 #define basicblock_chain_clear basicblock_chain_init
548 static void basicblock_chain_add(basicblock_chain_t *bbc, basicblock *bb) {
549 if (bbc->first == NULL) {
550 assert(bbc->last == NULL);
551 assert(bb->next == NULL);
555 assert(bbc->last->next == NULL);
556 bbc->last->next = bb;
561 static inline basicblock *basicblock_chain_front(basicblock_chain_t *bbc) {
566 static inline basicblock *basicblock_chain_back(basicblock_chain_t *bbc) {
571 static inline bool basicblock_chain_empty(const basicblock_chain_t *bbc) {
572 return bbc->first == NULL;
575 /*** exception_entry_chain ***************************************************/
578 exception_entry *first;
579 exception_entry *last;
580 } exception_entry_chain_t;
582 static void exception_entry_chain_init(exception_entry_chain_t *eec) {
587 #define exception_entry_chain_clear exception_entry_chain_init
589 static void exception_entry_chain_add(exception_entry_chain_t *eec, exception_entry *ee) {
590 if (eec->first == NULL) {
594 eec->last->next = ee;
595 eec->last->down = ee;
600 static inline bool exception_entry_chain_empty(const exception_entry_chain_t *eec) {
601 return eec->first == NULL;
604 static inline exception_entry *exception_entry_chain_back(exception_entry_chain_t *eec) {
609 static inline exception_entry *exception_entry_chain_front(exception_entry_chain_t *eec) {
614 /*** traversal **************************************************************/
617 unsigned (*var_num_to_index)(void *vp, s4 var);
618 varinfo *(*index_to_initial_var)(void *vp, unsigned index);
619 varinfo *(*var_num_to_varinfo)(void *vp, s4 var);
620 unsigned (*variables_count)(void *vp);
625 state_array_t *state_array;
627 traversal_ops_t *ops;
630 /*** basicblock_info ********************************************************/
632 typedef struct basicblock_info {
636 unsigned backward_branches;
641 basicblock_chain_t *subbasicblocks;
643 unsigned complete_predecessors;
645 #if defined(SSA_VERIFY)
646 unsigned num_phi_elimination;
651 /*** traversal **************************************************************/
653 void traversal_init(traversal_t *t, unsigned count, void *ops_vp, traversal_ops_t *ops) {
654 t->phis = DNEW(phis_t);
655 phis_init(t->phis, count);
657 t->state_array = DNEW(state_array_t);
658 state_array_init(t->state_array, count);
664 instruction *traversal_create_phi(traversal_t *t, vars_t *v, unsigned argcount, s4 index) {
665 instruction *phi = phis_add(t->phis);
668 phi_init(phi, argcount, index);
669 dst = vars_add_item(v, t->ops->index_to_initial_var(t->ops_vp, index));
670 phi_set_dst(phi, dst);
672 state_array_set(t->state_array, index, phi);
677 static void traversal_rename_def(traversal_t *t, vars_t *vars, instruction *iptr) {
681 state_array_assert_items(t->state_array);
683 v = t->ops->var_num_to_varinfo(t->ops_vp, iptr->dst.varindex);
684 index = t->ops->var_num_to_index(t->ops_vp, iptr->dst.varindex);
686 iptr->dst.varindex = vars_add_item(vars, v);
687 state_array_set(t->state_array, index, iptr);
690 static void traversal_rename_use(traversal_t *t, s4 *puse) {
693 state_array_assert_items(t->state_array);
695 index = t->ops->var_num_to_index(t->ops_vp, *puse);
697 assert(state_array_get(t->state_array, index));
698 *puse = state_array_get_var(t->state_array, index);
701 static inline unsigned traversal_variables_count(traversal_t *t) {
702 return t->ops->variables_count(t->ops_vp);
705 unsigned local_var_num_to_index(void *vp, s4 var) {
706 return (unsigned)var;
709 varinfo *local_index_to_initial_var(void *vp, unsigned index) {
710 jitdata *jd = (jitdata *)vp;
711 return jd->var + index;
714 varinfo *local_var_num_to_varinfo(void *vp, s4 var) {
715 jitdata *jd = (jitdata *)vp;
716 return jd->var + var;
719 unsigned local_variables_count(void *vp) {
720 jitdata *jd = (jitdata *)vp;
721 return jd->localcount;
724 traversal_ops_t traversal_local_ops = {
725 local_var_num_to_index,
726 local_index_to_initial_var,
727 local_var_num_to_varinfo,
728 local_variables_count
731 unsigned inout_var_num_to_index(void *vp, s4 var) {
732 basicblock *bb = (basicblock *)vp;
735 for (i = 0; i < bb->indepth; ++i) {
736 if (bb->invars[i] == var) {
741 for (i = 0; i < bb->outdepth; ++i) {
742 if (bb->outvars[i] == var) {
750 varinfo *inout_index_to_initial_var(void *vp, unsigned index) {
751 basicblock *bb = (basicblock *)vp;
752 jitdata *jd = (jitdata *)(((basicblock_info_t *)bb->vp)->locals->ops_vp); /* evil hack */
753 assert(index < bb->indepth);
754 return jd->var + bb->invars[index];
757 varinfo *inout_var_num_to_varinfo(void *vp, s4 var) {
758 basicblock *bb = (basicblock *)vp;
759 jitdata *jd = (jitdata *)(((basicblock_info_t *)bb->vp)->locals->ops_vp); /* evil hack */
760 return jd->var + var;
763 unsigned inout_variables_count(void *vp) {
764 basicblock *bb = (basicblock *)vp;
768 traversal_ops_t traversal_inout_ops = {
769 inout_var_num_to_index,
770 inout_index_to_initial_var,
771 inout_var_num_to_varinfo,
772 inout_variables_count
775 /*** basicblock_info ********************************************************/
777 void basicblock_info_init(basicblock_info_t *bbi, basicblock *bb, jitdata *jd) {
778 MZERO(bbi, basicblock_info_t, 1);
780 bbi->locals = DNEW(traversal_t);
781 traversal_init(bbi->locals, jd->localcount, jd, &traversal_local_ops);
783 bbi->stack = DNEW(traversal_t);
784 traversal_init(bbi->stack, jd->maxinterfaces, bb, &traversal_inout_ops);
786 bbi->subbasicblocks = DNEW(basicblock_chain_t);
787 basicblock_chain_init(bbi->subbasicblocks);
790 /*** basicblock *************************************************************/
792 static inline basicblock_info_t *basicblock_info(basicblock *bb) {
793 return (basicblock_info_t *)(bb->vp);
796 #define bb_info basicblock_info
798 static unsigned basicblock_get_predecessor_count(basicblock *bb) {
800 basicblock_info_t *bbi = bb_info(bb);
803 ret = bb->predecessorcount;
805 FOR_EACH_EXPREDECESSOR(bb, itpred) {
806 ret += (*itpred)->exouts;
812 static unsigned basicblock_get_predecessor_index(basicblock *from, basicblock *to) {
816 FOR_EACH_PREDECESSOR(to, itpred) {
817 if (*itpred == from) break;
821 assert(j != to->predecessorcount);
826 static unsigned basicblock_get_ex_predecessor_index(basicblock *from, unsigned pei, basicblock *to) {
830 j = to->predecessorcount;
832 FOR_EACH_EXPREDECESSOR(to, itpred) {
833 if ((*itpred)->nr == from->nr) {
837 j += (*itpred)->exouts;
844 /*** ssa_info ***************************************************************/
846 typedef struct ssa_info {
855 basicblock_chain_t *new_blocks;
857 } ssa_info, ssa_info_t;
859 void ssa_info_init(ssa_info_t *ssa, jitdata *jd) {
862 ssa->locals = DNEW(vars_t);
863 vars_init(ssa->locals, VARS_CATEGORY_LOCAL);
865 /* Initialize first version of locals, that is always available. */
866 MCOPY(ssa->locals->items, jd->var, varinfo, jd->localcount);
867 ssa->locals->count = jd->localcount;
869 ssa->stack = DNEW(vars_t);
870 vars_init(ssa->stack, VARS_CATEGORY_STACK);
872 ssa->others = DNEW(vars_t);
873 vars_init(ssa->others, VARS_CATEGORY_OTHERS);
875 ssa->new_blocks = DNEW(basicblock_chain_t);
876 basicblock_chain_init(ssa->new_blocks);
879 /*** others_mapping *********************************************************/
881 static inline void others_mapping_set(ssa_info *ssa, s4 var, s4 new_var) {
882 ssa->jd->var[var].vv.regoff = new_var;
885 static inline s4 others_mapping_get(const ssa_info *ssa, s4 var) {
886 return ssa->jd->var[var].vv.regoff;
889 /*** code *******************************************************************/
891 void fix_exception_handlers(jitdata *jd) {
893 basicblock *exh = NULL;
899 basicblock_chain_t chain;
900 basicblock *last = NULL;
901 basicblock *marker = NULL;
903 if (jd->exceptiontablelength == 0) {
907 basicblock_chain_init(&chain);
909 /* We need to allocate new iovars */
911 avail_vars = (jd->varcount - jd->vartop);
912 add_vars = jd->exceptiontablelength;
914 if (add_vars > avail_vars) {
915 add_vars -= avail_vars;
916 jd->var = DMREALLOC(jd->var, varinfo, jd->varcount, jd->varcount + add_vars);
917 jd->varcount += add_vars;
920 /* For each exception handler block, create one block with a prologue block */
922 FOR_EACH_BASICBLOCK(jd, bptr) {
923 if (bptr->type == BBTYPE_EXH) {
925 bptr->type = BBTYPE_STD;
926 bptr->predecessorcount = 0; /* legacy */
928 /* Create basicblock with 2 instructions */
930 exh = DNEW(basicblock);
931 MZERO(exh, basicblock, 1);
933 iptr = DMNEW(instruction, 2);
934 MZERO(iptr, instruction, 2);
936 /* Create new outvar */
938 assert(jd->vartop < jd->varcount);
941 jd->var[v].type = TYPE_ADR;
942 jd->var[v].flags = INOUT;
944 exh->nr = jd->basicblockcount;
945 jd->basicblockcount += 1;
947 exh->type = BBTYPE_EXH;
951 exh->outvars = DNEW(s4);
953 exh->predecessorcount = -1; /* legacy */
954 exh->flags = BBFINISHED;
956 basicblock_chain_add(&chain, exh);
960 iptr->opc = ICMD_GETEXCEPTION;
961 iptr->dst.varindex = v;
964 /* Goto real exception handler */
966 goto_init(iptr, bptr);
973 if (bptr->next == NULL) {
980 /* Put the chain of exception handlers between just before the last
981 basic block (end marker). */
983 if (! basicblock_chain_empty(&chain)) {
984 marker->nr = jd->basicblockcount;
985 basicblock_chain_back(&chain)->next = marker;
986 last->next = basicblock_chain_front(&chain);
988 assert(last->nr + 1 == basicblock_chain_front(&chain)->nr);
989 assert(marker->nr == jd->basicblockcount);
992 /* Replace all exception handlers in exception table with their respective
995 for (ee = jd->exceptiontable; ee; ee = ee->down) {
996 assert(ee->handler->vp);
997 ee->handler = ee->handler->vp;
1002 /*** ssa_enter ***************************************************************/
1004 static void ssa_enter_mark_loops_intern(basicblock *bb, unsigned num_branches) {
1005 basicblock_info_t *bbi = bb_info(bb);
1006 basicblock **itsucc;
1008 if (! bbi->visited) {
1009 bbi->visited = true;
1011 FOR_EACH_SUCCESSOR(bb, itsucc) {
1012 /* There is a single branch from bb into the successor. */
1013 ssa_enter_mark_loops_intern(*itsucc, 1);
1015 FOR_EACH_EXHANDLER(bb, itsucc) {
1016 /* For exception handler type successors,
1017 we count a branch into the exception handler from every PEI. */
1018 ssa_enter_mark_loops_intern(*itsucc, bb->exouts);
1020 bbi->active = false;
1021 } else if (bbi->active) {
1022 bbi->backward_branches += num_branches;
1026 static inline void ssa_enter_mark_loops(basicblock *bb) {
1027 ssa_enter_mark_loops_intern(bb, 1);
1030 static void ssa_enter_merge(
1034 unsigned predecessor_index,
1038 basicblock_info_t *dsti = bb_info(bdst);
1039 unsigned predecessor_count = basicblock_get_predecessor_count(bdst);
1044 /* We are merging for the first time into this block. */
1046 if (! state_array_has_items(dst->state_array)) {
1048 state_array_allocate_items(dst->state_array);
1050 if (dsti->backward_branches > 0) {
1051 /* Loop header, create phi functions for all variables. */
1052 for (i = 0; i < traversal_variables_count(dst); ++i) {
1053 phi = traversal_create_phi(dst, vdst, predecessor_count, i);
1054 /* No need to init, they will only be filled in later. */
1057 state_array_copy(dst->state_array, src->state_array);
1062 /* We are merging another block. */
1064 /* Process every variable. */
1066 for (i = 0; i < traversal_variables_count(dst); ++i) {
1067 if (dsti->traversed) {
1069 /* Back edge, all phi functions are already created.
1070 We only need to set their arguments. */
1073 phis_get(dst->phis, i),
1075 state_array_get(src->state_array, i)
1078 } else if (state_array_get(dst->state_array, i) != state_array_get(src->state_array, i)) {
1080 /* A different definition of this var reaches the block.
1081 We need to create a phi function. */
1083 if (phis_contains(dst->phis, state_array_get(dst->state_array, i))) {
1084 /* There is already a phi function created for this var.
1085 No need to create one. */
1087 /* Create a new phi function.
1088 Set all arguments to old value in state array. */
1089 old = state_array_get(dst->state_array, i);
1090 phi = traversal_create_phi(dst, vdst, predecessor_count, i);
1091 phi_set_all_args(phi, old);
1094 /* Set argument of phi function. */
1097 state_array_get(dst->state_array, i),
1099 state_array_get(src->state_array, i)
1105 static void ssa_enter_process_block(ssa_info *ssa, basicblock *bb);
1106 static bool ssa_enter_eliminate_redundant_phis(traversal_t *t, vars_t *vs, basicblock_info_t *bbi);
1108 #if defined(SSA_VERIFY)
1109 static void ssa_enter_verify_no_redundant_phis(ssa_info_t *ssa) {
1111 basicblock_info_t *bbi;
1113 FOR_EACH_BASICBLOCK(ssa->jd, bptr) {
1114 if (basicblock_reached(bptr)) {
1115 bbi = bb_info(bptr);
1116 FOR_EACH_PHI_FUNCTION(bbi->locals->phis, itph) {
1117 if (! phi_is_redundant(itph)) {
1118 phi_calculate_redundancy(itph);
1119 assert(! phi_is_redundant(itph));
1122 FOR_EACH_PHI_FUNCTION(bbi->stack->phis, itph) {
1123 if (! phi_is_redundant(itph)) {
1124 phi_calculate_redundancy(itph);
1125 assert(! phi_is_redundant(itph));
1133 static void ssa_enter_traverse(ssa_info_t *ssa, basicblock *bb) {
1134 basicblock **itsucc;
1135 basicblock_info_t *succi;
1136 basicblock_info_t *bbi = bb_info(bb);
1137 unsigned predecessor_count;
1141 ssa_enter_process_block(ssa, bb);
1145 FOR_EACH_SUCCESSOR(bb, itsucc) {
1147 succi = bb_info(*itsucc);
1153 basicblock_get_predecessor_index(bb, *itsucc),
1161 basicblock_get_predecessor_index(bb, *itsucc),
1165 succi->complete_predecessors += 1;
1167 predecessor_count = basicblock_get_predecessor_count(*itsucc);
1170 succi->complete_predecessors ==
1171 (predecessor_count - succi->backward_branches)
1173 ssa_enter_traverse(ssa, *itsucc);
1177 (succi->complete_predecessors == predecessor_count) &&
1178 (succi->backward_branches > 0)
1180 #if defined(SSA_VERIFY)
1181 assert(succi->num_phi_elimination < 2);
1182 succi->num_phi_elimination += 1;
1184 ssa_enter_eliminate_redundant_phis(succi->locals, ssa->locals, succi);
1185 ssa_enter_eliminate_redundant_phis(succi->stack, ssa->stack, succi);
1189 FOR_EACH_EXHANDLER(bb, itsucc) {
1191 succi = bb_info(*itsucc);
1193 succi->complete_predecessors += bb->exouts; /* XXX this might be 0 */
1195 predecessor_count = basicblock_get_predecessor_count(*itsucc);
1198 succi->complete_predecessors ==
1199 (predecessor_count - succi->backward_branches)
1201 ssa_enter_traverse(ssa, *itsucc);
1205 (succi->complete_predecessors == predecessor_count) &&
1206 (succi->backward_branches > 0)
1208 #if defined(SSA_VERIFY)
1209 assert(succi->num_phi_elimination < 2);
1210 succi->num_phi_elimination += 1;
1212 ssa_enter_eliminate_redundant_phis(succi->locals, ssa->locals, succi);
1213 ssa_enter_eliminate_redundant_phis(succi->stack, ssa->stack, succi);
1220 static void ssa_enter_process_pei(ssa_info *ssa, basicblock *bb, unsigned pei) {
1221 basicblock_info_t *bbi = bb_info(bb);
1222 basicblock_info_t *succi;
1223 basicblock **itsucc;
1225 FOR_EACH_EXHANDLER(bb, itsucc) {
1226 succi = bb_info(*itsucc);
1232 basicblock_get_ex_predecessor_index(bb, pei, *itsucc),
1238 static FIXME(bool) ssa_enter_eliminate_redundant_phis(traversal_t *t, vars_t *vs, basicblock_info_t *FIXME(bbi)) {
1243 FOR_EACH_PHI_FUNCTION(t->phis, itph) {
1245 phi_calculate_redundancy(itph);
1247 /* If the phi function is redundant,
1248 make the variable it defines point to the value defined by the substituing
1251 if (phi_is_redundant(itph)) {
1252 vars_subst(vs, itph->dst.varindex, phi_get_subst(itph)->dst.varindex);
1253 assert(bbi->backward_branches > 0);
1262 static void ssa_enter_post_eliminate_redundand_phi(
1268 phi_calculate_redundancy(phi);
1269 phi_set_flag(PHI_FLAG_REDUNDANCY_CHECKED);
1271 /* if redundancy changed and phi function escapes block */
1273 /* for each successor */
1276 static void ssa_enter_post_eliminate_redundant_phis(ssa_info_t *ssa) {
1278 basicblock_info_t *bbi;
1281 FOR_EACH_BASICBLOCK(ssa->jd, bptr) {
1282 bbi = bb_info(bptr);
1284 if (bbi->backward_branches > 0) {
1285 /* Redundant phi functions have the left hand side as operand.
1286 This can happen by definition only in loop headers. */
1288 FOR_EACH_PHI_FUNCTION(bbi->locals, itph) {
1289 if (! phi_has_flag(PHI_FLAG_REDUNDANCY_CHECKED)) {
1290 /* Calculate redundancy? */
1292 /* If yes recurse? */
1296 FOR_EACH_PHI_FUNCTION(bbi->stack, itph) {
1303 static void ssa_enter_init_locals(state_array_t *sa) {
1307 for (i = 0; i < sa->count; ++i) {
1308 iptr = DNEW(instruction);
1309 iptr->opc = ICMD_NOP;
1310 iptr->dst.varindex = i;
1311 state_array_set(sa, i, iptr);
1315 static void ssa_enter_process_block(ssa_info *ssa, basicblock *bb) {
1316 basicblock_info_t *bbi = bb_info(bb);
1321 unsigned uses_count;
1324 /* Every basic block can be traversed only once. */
1326 assert(! bbi->traversed);
1327 bbi->traversed = true;
1329 /* The root basicblock needs special treatment. */
1331 if (bb->predecessorcount == 0 && bb->type != BBTYPE_EXH) {
1332 state_array_assert_no_items(bbi->locals->state_array);
1333 state_array_allocate_items(bbi->locals->state_array);
1334 ssa_enter_init_locals(bbi->locals->state_array);
1336 state_array_assert_no_items(bbi->stack->state_array);
1337 state_array_allocate_items(bbi->stack->state_array);
1340 /* Exception handlers have a clean stack. */
1342 if (bb->type == BBTYPE_EXH) {
1343 state_array_assert_no_items(bbi->stack->state_array);
1344 state_array_allocate_items(bbi->stack->state_array);
1347 /* Some in/out vars get marked as INOUT in simplereg,
1348 and are not marked at this point.
1349 Mark them manually. */
1351 for (ituse = bb->invars; ituse != bb->invars + bb->indepth; ++ituse) {
1352 ssa->jd->var[*ituse].flags |= INOUT;
1353 ssa->jd->var[*ituse].flags &= ~PREALLOC;
1356 for (ituse = bb->outvars; ituse != bb->outvars + bb->outdepth; ++ituse) {
1357 ssa->jd->var[*ituse].flags |= INOUT;
1358 ssa->jd->var[*ituse].flags &= ~PREALLOC;
1361 /* Process instructions */
1363 state_array_assert_items(bbi->locals->state_array);
1365 FOR_EACH_INSTRUCTION(bb, iptr) {
1367 #if defined(ELIMINATE_NOP_LOAD_STORE)
1369 /* Kill NOP instructions of the form:
1372 As they create a lot of unnecessary definitions.
1373 For performance, definitely eliminate them. However, keeping them is a
1378 (icmd_table[iptr->opc].dataflow == DF_LOAD) ||
1379 (icmd_table[iptr->opc].dataflow == DF_STORE)
1381 if (iptr->dst.varindex == iptr->s1.varindex) {
1382 iptr->opc = ICMD_NOP;
1388 if (icmd_table[iptr->opc].flags & ICMDTABLE_PEI) {
1389 ssa_enter_process_pei(ssa, bb, pei++);
1392 instruction_get_uses(iptr, ssa->s_buf, &uses, &uses_count);
1394 for (ituse = uses; ituse != uses + uses_count; ++ituse) {
1395 if (var_is_local(ssa->jd, *ituse)) {
1396 traversal_rename_use(
1400 } else if (var_is_inout(ssa->jd, *ituse)) {
1401 traversal_rename_use(
1406 *ituse = others_mapping_get(ssa, *ituse);
1410 instruction_set_uses(iptr, ssa->s_buf, uses, uses_count);
1412 if (instruction_has_dst(iptr)) {
1413 if (var_is_local(ssa->jd, iptr->dst.varindex)) {
1414 traversal_rename_def(
1419 } else if (var_is_inout(ssa->jd, iptr->dst.varindex)) {
1420 traversal_rename_def(
1426 old = iptr->dst.varindex;
1427 iptr->dst.varindex = vars_add_item(
1429 ssa->jd->var + iptr->dst.varindex
1431 others_mapping_set(ssa, old, iptr->dst.varindex);
1439 [locals.........................][interaces][others]
1440 [original locals][version > 1 ]
1441 <--------------- new locals --------------->
1444 static void ssa_enter_export_variables(ssa_info *ssa) {
1449 jitdata *jd = ssa->jd;
1451 vartop = ssa->locals->count + ssa->stack->count + ssa->others->count;
1452 vars = DMNEW(varinfo, vartop);
1454 vars_copy_to_final(ssa->locals, vars);
1455 vars_copy_to_final(ssa->stack, vars + ssa->locals->count);
1456 vars_copy_to_final(ssa->others, vars + ssa->locals->count + ssa->stack->count);
1459 jd->vartop = jd->varcount = vartop;
1461 /* Grow local map to accomodate all new locals and iovars.
1462 But keep the local map for version 1 of locals, that contains the holes. */
1464 jd->local_map = DMREALLOC(
1468 5 * (jd->maxlocals + ssa->locals->count + ssa->stack->count - jd->localcount)
1471 it = jd->local_map + (jd->maxlocals * 5); /* start adding entries here */
1473 /* Add version > 1 of all locals */
1475 for (i = jd->localcount; i < ssa->locals->count; ++i) {
1476 for (j = 0; j < 5; ++j) {
1477 if (jd->var[i].type != j) {
1486 /* Add all io vars. */
1488 for (i = ssa->locals->count; i < ssa->locals->count + ssa->stack->count; ++i) {
1489 for (j = 0; j < 5; ++j) {
1490 if (jd->var[i].type != j) {
1501 jd->maxlocals += (ssa->locals->count + ssa->stack->count - jd->localcount);
1502 jd->localcount = ssa->locals->count + ssa->stack->count;
1504 /* Eliminate interfaces. */
1506 jd->maxinterfaces = 0;
1511 static inline void ssa_enter_eliminate_category(ssa_info_t *ssa, s4 *pvar) {
1512 switch (vars_get_category(*pvar)) {
1513 case VARS_CATEGORY_LOCAL:
1514 *pvar = vars_get_index(vars_resolve_subst(ssa->locals, *pvar));
1516 case VARS_CATEGORY_STACK:
1517 *pvar = vars_get_index(vars_resolve_subst(ssa->stack, *pvar)) + ssa->locals->count;
1519 case VARS_CATEGORY_OTHERS:
1520 *pvar = vars_get_index(*pvar) + ssa->locals->count + ssa->stack->count;
1526 void ssa_enter_eliminate_categories(ssa_info_t *ssa) {
1530 unsigned uses_count;
1531 basicblock_info_t *bbi;
1534 FOR_EACH_BASICBLOCK(ssa->jd, bb) {
1542 FOR_EACH_PHI_FUNCTION(bbi->locals->phis, itph) {
1543 ssa_enter_eliminate_category(ssa, &(itph->dst.varindex));
1546 FOR_EACH_PHI_FUNCTION(bbi->stack->phis, itph) {
1547 ssa_enter_eliminate_category(ssa, &(itph->dst.varindex));
1551 FOR_EACH_INSTRUCTION(bb, iptr) {
1552 if (instruction_has_dst(iptr)) {
1553 ssa_enter_eliminate_category(ssa, &(iptr->dst.varindex));
1555 instruction_get_uses(iptr, ssa->s_buf, &uses, &uses_count);
1556 for (ituse = uses; ituse != uses + uses_count; ++ituse) {
1557 ssa_enter_eliminate_category(ssa, ituse);
1559 instruction_set_uses(iptr, ssa->s_buf, uses, uses_count);
1564 static void ssa_enter_create_phi_graph(ssa_info *ssa) {
1566 basicblock_info_t *bbi;
1568 instruction **ituse;
1570 char path[PATH_MAX], *ppath;
1573 snprintf(path, PATH_MAX, "|tmp|graphs|%s.%s.dot", ssa->jd->m->class->name->text, ssa->jd->m->name->text);
1574 for (ppath = path; *ppath; ++ppath) {
1575 if (*ppath == '|') *ppath = '/';
1576 else if (*ppath == '/') *ppath = '.';
1579 f = fopen(path, "w");
1581 if (f == NULL) return;
1583 fprintf(f, "digraph G {\n");
1585 FOR_EACH_BASICBLOCK(ssa->jd, bptr) {
1586 bbi = bb_info(bptr);
1588 FOR_EACH_PHI_FUNCTION(bbi->locals->phis, itph) {
1590 FOR_EACH_PHI_USE(itph, ituse) {
1591 if ((*ituse)->opc == ICMD_PHI) {
1592 fprintf(f, "%d -> %d;\n", (*ituse)->dst.varindex, itph->dst.varindex);
1605 static basicblock *ssa_leave_create_transition_block_intern(
1609 unsigned predecessor_index,
1610 unsigned reserved_insns
1615 basicblock_info_t *toi;
1619 /* Create basicblock and instruction array. */
1621 bb = DNEW(basicblock);
1622 MZERO(bb, basicblock, 1);
1624 bb->nr = ssa->jd->basicblockcount;
1625 ssa->jd->basicblockcount += 1;
1627 bb->type = BBTYPE_STD;
1630 toi->locals->phis->count +
1631 toi->stack->phis->count +
1633 bb->iinstr = DMNEW(instruction, bb->icount);
1634 MZERO(bb->iinstr, instruction, bb->icount);
1636 /* Populate instruction array. */
1638 iptr = bb->iinstr + reserved_insns;
1640 /* Add phi moves. */
1642 FOR_EACH_PHI_FUNCTION(toi->locals->phis, itph) {
1643 phi_create_copy(itph, predecessor_index, iptr++);
1646 FOR_EACH_PHI_FUNCTION(toi->stack->phis, itph) {
1647 phi_create_copy(itph, predecessor_index, iptr++);
1650 /* Add goto to real block. */
1652 goto_init(iptr, to);
1654 /* Add basicblock to chain of newly created basicblocks. */
1656 basicblock_chain_add(ssa->new_blocks, bb);
1661 static inline basicblock *ssa_leave_create_transition_block(
1666 return ssa_leave_create_transition_block_intern(
1670 basicblock_get_predecessor_index(from, to),
1675 static void ssa_leave_create_fallthrough(ssa_info *ssa, basicblock *bptr) {
1676 unsigned predecessor_index;
1677 basicblock_info_t *toi;
1682 if (bptr->next == NULL) {
1683 /* No fallthrough. */
1687 predecessor_index = basicblock_get_predecessor_index(bptr, bptr->next);
1688 toi = bb_info(bptr->next);
1692 /* Resize instruction array to accomodate all phi moves. */
1694 icount = bptr->icount + toi->locals->phis->count + toi->stack->phis->count;
1696 bptr->iinstr = DMREALLOC(
1703 iptr = bptr->iinstr + bptr->icount;
1704 bptr->icount = icount;
1706 /* Create phi moves. */
1708 FOR_EACH_PHI_FUNCTION(toi->locals->phis, itph) {
1709 phi_create_copy(itph, predecessor_index, iptr++);
1712 FOR_EACH_PHI_FUNCTION(toi->stack->phis, itph) {
1713 phi_create_copy(itph, predecessor_index, iptr++);
1717 static void ssa_leave_create_phi_moves(ssa_info *ssa) {
1720 basicblock *last = NULL;
1723 branch_target_t *table;
1724 lookup_target_t *lookup;
1725 bool has_fallthrough;
1727 FOR_EACH_BASICBLOCK(ssa->jd, bptr) {
1729 if (bptr->next == NULL) {
1733 if (bptr->vp == NULL) {
1737 if (! basicblock_reached(bptr)) {
1741 has_fallthrough = true;
1743 for (iptr = bptr->iinstr; iptr != bptr->iinstr + bptr->icount; ++iptr) {
1744 switch (icmd_table[iptr->opc].controlflow) {
1749 ssa_leave_create_transition_block(ssa, bptr, iptr->dst.block);
1752 table = iptr->dst.table;
1753 l = iptr->sx.s23.s2.tablelow;
1754 i = iptr->sx.s23.s3.tablehigh;
1756 i += 1; /* default */
1759 ssa_leave_create_transition_block(ssa, bptr, table->block);
1764 lookup = iptr->dst.lookup;
1765 i = iptr->sx.s23.s2.lookupcount;
1767 lookup->target.block =
1768 ssa_leave_create_transition_block(ssa, bptr, lookup->target.block);
1771 iptr->sx.s23.s3.lookupdefault.block =
1772 ssa_leave_create_transition_block(ssa, bptr, iptr->sx.s23.s3.lookupdefault.block);
1775 iptr->sx.s23.s3.jsrtarget.block =
1776 ssa_leave_create_transition_block(ssa, bptr, iptr->sx.s23.s3.jsrtarget.block);
1781 (iptr->opc == ICMD_GOTO) ||
1782 (iptr->opc == ICMD_JSR) ||
1783 (iptr->opc == ICMD_RET) ||
1784 icmd_table[iptr->opc].controlflow == CF_END ||
1785 (iptr->opc == ICMD_TABLESWITCH) ||
1786 (iptr->opc == ICMD_LOOKUPSWITCH)
1788 has_fallthrough = false;
1789 } else if (iptr->opc != ICMD_NOP) {
1790 has_fallthrough = true;
1795 if (bptr->next == NULL) {
1799 if (! basicblock_reached(bptr->next)) {
1803 if (has_fallthrough) {
1804 ssa_leave_create_fallthrough(ssa, bptr);
1808 /* Add chain of new basic blocks */
1810 if (last != NULL && ! basicblock_chain_empty(ssa->new_blocks)) {
1811 last->next = basicblock_chain_front(ssa->new_blocks);
1816 static basicblock *ssa_leave_split_basicblock_at(ssa_info *ssa, basicblock *bptr, instruction *iptr) {
1818 basicblock_info_t *bbi = bb_info(bptr);
1819 unsigned iidx = iptr - bptr->iinstr;
1820 basicblock *newblock;
1821 basicblock *tosplit;
1825 assert(iidx < bptr->icount);
1828 /* If there are no subbasicblocks yet, we initialize the first one to be a
1829 copy of the original basicblock. */
1831 if (basicblock_chain_empty(bbi->subbasicblocks)) {
1832 newblock = DNEW(basicblock);
1834 newblock->next = NULL;
1835 newblock->vp = NULL;
1836 basicblock_chain_add(bbi->subbasicblocks, newblock);
1839 /* Find the subbasicblock that will be split:
1840 the one that cointains iptr. */
1842 tosplit = basicblock_chain_front(bbi->subbasicblocks);
1845 while (tosplit->next && (iidx >= (pos + tosplit->icount))) {
1846 assert(bptr->nr == tosplit->nr);
1847 pos += tosplit->icount;
1848 tosplit = tosplit->next;
1851 assert(bptr->nr == tosplit->nr);
1853 /* Calculate number of instructions left in block to split. */
1855 ileft = iptr - tosplit->iinstr + 1;
1856 assert(ileft <= tosplit->icount);
1858 /* If there are any instructions left in the block to split, split */
1860 if (ileft < tosplit->icount) {
1861 newblock = DNEW(basicblock);
1862 *newblock = *tosplit;
1864 tosplit->next = newblock;
1865 tosplit->icount = ileft;
1867 newblock->icount -= ileft;
1868 newblock->iinstr += ileft;
1870 assert(tosplit->nr == bptr->nr);
1871 assert(newblock->nr == bptr->nr);
1872 assert(newblock->next == NULL);
1874 if (newblock->next == NULL) {
1875 bbi->subbasicblocks->last = newblock;
1879 /* We won't break pointers/references to bptr.
1880 So return bptr instread of the first fragment.
1881 Later, we will put the first fragment into the memory used by bptr.
1884 if (tosplit == basicblock_chain_front(bbi->subbasicblocks)) {
1891 static basicblock *ssa_leave_create_transition_exception_handler(
1899 /* From is a try block, to is an exception handler prologue. */
1901 /* Remove old prologue. */
1903 to->flags = BBDELETED;
1905 /* Create new exception handler. */
1907 exh = ssa_leave_create_transition_block_intern(
1911 basicblock_get_ex_predecessor_index(from, pei, to),
1914 exh->type = BBTYPE_EXH;
1916 /* Copy goto to real exception handler at the end of the exception handler
1919 assert(to->iinstr[to->icount - 1].opc == ICMD_GOTO);
1920 assert(exh->iinstr[exh->icount - 1].opc == ICMD_GOTO);
1921 exh->iinstr[exh->icount - 1] = to->iinstr[to->icount - 1];
1923 /* Copy getexception from the old prologue. */
1925 assert(to->iinstr[0].opc == ICMD_GETEXCEPTION);
1926 exh->iinstr[0] = to->iinstr[0];
1931 static exception_entry *ssa_leave_create_transition_exception_entry(
1934 basicblock *handler,
1935 classref_or_classinfo catchtype
1938 exception_entry *ee = DNEW(exception_entry);
1939 basicblock_info_t *fromi = bb_info(from);
1943 /* If the try block has subbasicblocks, the next block is the next fragment,
1944 not the successor block. */
1946 if (fromi != NULL) {
1947 ee->end = basicblock_chain_front(fromi->subbasicblocks)->next;
1949 ee->end = from->next;
1951 ee->handler = handler;
1952 ee->catchtype = catchtype;
1959 static void ssa_leave_create_exceptional_phi_moves(ssa_info *ssa) {
1962 exception_entry *ite;
1963 exception_entry_chain_t chain;
1964 classref_or_classinfo catchtype;
1969 exception_entry *ee;
1970 basicblock *last = NULL;
1972 if (! basicblock_chain_empty(ssa->new_blocks)) {
1973 last = basicblock_chain_back(ssa->new_blocks);
1976 basicblock_chain_clear(ssa->new_blocks);
1978 for (ite = ssa->jd->exceptiontable; ite; ite = ite->down) {
1979 bptr = ite->handler;
1980 catchtype = ite->catchtype;
1981 exception_entry_chain_init(&chain);
1982 for (ittry = ite->start; ittry != ite->end; ittry = ittry->next) {
1983 if (basicblock_reached(ittry)) {
1984 /* Dead code does not have a basicblock_info_t associated. */
1986 FOR_EACH_INSTRUCTION(ittry, iptr) {
1987 if (icmd_table[iptr->opc].flags & ICMDTABLE_PEI) {
1988 /* try is basicblock fragment till (including) the pei */
1989 try = ssa_leave_split_basicblock_at(ssa, ittry, iptr);
1990 /* ee is handler for try */
1991 exh = ssa_leave_create_transition_exception_handler(
1994 ee = ssa_leave_create_transition_exception_entry(
1995 ssa, try, exh, catchtype
1997 exception_entry_chain_add(&chain, ee);
1999 ssa->jd->exceptiontablelength += 1;
2004 if (! exception_entry_chain_empty(&chain)) {
2005 exception_entry_chain_back(&chain)->down = ite->down;
2006 exception_entry_chain_back(&chain)->next = ite->next;
2007 /* Replace original exception entry by first new one. */
2008 *ite = *exception_entry_chain_front(&chain);
2009 /* Set current iteration position to last newly created one. */
2010 ite = exception_entry_chain_back(&chain);
2015 for (last = ssa->jd->basicblocks; last->next != NULL; last = last->next);
2018 if (last != NULL && ! basicblock_chain_empty(ssa->new_blocks)) {
2019 last->next = basicblock_chain_front(ssa->new_blocks);
2023 void yssa(jitdata *jd) {
2025 basicblock_info_t *iti;
2029 printf("=============== [ before %s ] =========================\n", jd->m->name->text);
2031 printf("=============== [ /before ] =========================\n");
2034 ssa = DNEW(ssa_info);
2036 ssa_info_init(ssa, jd);
2038 FOR_EACH_BASICBLOCK(jd, it) {
2039 if (basicblock_reached(it)) {
2040 iti = DNEW(basicblock_info_t);
2041 basicblock_info_init(iti, it, jd);
2048 ssa_enter_mark_loops(jd->basicblocks);
2050 ssa_enter_traverse(ssa, jd->basicblocks);
2052 ssa_enter_eliminate_categories(ssa);
2054 ssa_enter_export_variables(ssa);
2056 ssa_enter_verify_no_redundant_phis(ssa);
2058 /*ssa_enter_create_phi_graph(ssa);*/
2060 ssa_leave_create_phi_moves(ssa);
2062 ssa_leave_create_exceptional_phi_moves(ssa);
2065 printf("=============== [ after ] =========================\n");
2067 printf("=============== [ /after ] =========================\n");
2072 void eliminate_subbasicblocks(jitdata *jd) {
2073 basicblock *bptr, *next;
2074 basicblock_info_t *bbi;
2076 FOR_EACH_BASICBLOCK(jd, bptr) {
2077 bbi = bb_info(bptr);
2079 if (! basicblock_chain_empty(bbi->subbasicblocks)) {
2081 /* Copy first subblock, to keep pointers intact. */
2082 *bptr = *basicblock_chain_front(bbi->subbasicblocks);
2083 bptr = basicblock_chain_back(bbi->subbasicblocks);
2090 printf("=============== [ elim ] =========================\n");
2092 printf("=============== [ /elim ] =========================\n");
2097 * These are local overrides for various environment variables in Emacs.
2098 * Please do not remove this and leave it at the end of the file, where
2099 * Emacs will automagically detect them.
2100 * ---------------------------------------------------------------------
2103 * indent-tabs-mode: t
2107 * vim:noexpandtab:sw=4:ts=4: