1 /* src/vm/jit/optimizing/lsra.inc - linear scan register allocator
3 Copyright (C) 2005, 2006 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Ullrich
39 #include "mm/memory.h"
41 #include "toolbox/bitvector.h"
43 #include "vmcore/statistics.h"
44 #include "vmcore/options.h"
45 #include "vmcore/method.h"
47 #include "vm/jit/abi.h"
48 #include "vm/jit/reg.h"
49 #include "vm/jit/jit.h"
52 #include "vm/jit/optimizing/graph.h"
53 #include "vm/jit/optimizing/lifetimes.h"
54 #include "vm/jit/optimizing/ssa.h"
56 #include "vm/jit/optimizing/lsra.h"
59 # include "vm/resolve.h"
60 #include "vm/builtin.h"
64 #include "toolbox/logging.h"
66 extern const char *string_java_lang_InternalError;
67 /* function prototypes */
68 void lsra_init(jitdata *);
69 graphdata *lsra_setup(jitdata *);
70 void lsra_main(jitdata *);
71 #ifdef LSRA_DEBUG_VERBOSE
72 void lsra_dump_stack(stackptr );
73 void print_lifetimes(jitdata *, int *, int);
74 void print_all_lifetimes(jitdata *);
76 void lsra_reg_setup(jitdata *,struct lsra_register *,
77 struct lsra_register *);
79 void lsra_calc_lifetime_length(jitdata *);
81 void _lsra_main( jitdata *, int *, int, struct lsra_register *,
83 void lsra_expire_old_intervalls(jitdata *, struct lifetime *,
84 struct lsra_register *);
85 void spill_at_intervall(jitdata *, struct lifetime *);
86 void lsra_add_active(struct lifetime *, struct lifetime **, int *);
87 void _lsra_expire_old_intervalls(jitdata *, struct lifetime *,
88 struct lsra_register *,
89 struct lifetime **, int *);
90 void _spill_at_intervall(struct lifetime *, struct lifetime **, int *);
91 void lsra_alloc(jitdata *, int *, int,
93 int lsra_getmem(struct lifetime *, struct freemem *, int *);
94 struct freemem *lsra_getnewmem(int *);
96 void lsra(jitdata *jd) {
102 #if defined(ENABLE_STATISTICS)
106 #if defined(LSRA_DEBUG_CHECK)
119 #if defined(LSRA_DEBUG_CHECK)
122 while (b_index < jd->basicblockcount ) {
124 if (jd->basicblocks[b_index].flags >= BBREACHED) {
126 in=m->basicblocks[b_index].instack;
127 ind=m->basicblocks[b_index].indepth;
128 for (;ind != 0;in=in->prev, ind--) {
129 /* ARGVAR or LOCALVAR in instack is ok*/
130 #if defined(LSRA_DEBUG_VERBOSE)
131 if (compileverbose) {
132 if (in->varkind == ARGVAR) printf("ARGVAR in instack: \n");
133 if (in->varkind == LOCALVAR)
134 printf("LOCALVAR in instack\n");
138 out=m->basicblocks[b_index].outstack;
139 outd=m->basicblocks[b_index].outdepth;
140 for (;outd != 0;out=out->prev, outd--) {
141 if (out->varkind == ARGVAR)
142 { log_text("ARGVAR in outstack\n"); assert(0); }
143 if (out->varkind == LOCALVAR)
144 { log_text("LOCALVAR in outstack\n"); assert(0); }
152 #if defined(LSRA_DEBUG_CHECK) || defined(LSRA_DEBUG_VERBOSE)
153 #if defined(LSRA_DEBUG_VERBOSE)
154 if (compileverbose) {
155 printf("%s %s ",m->class->name->text, m->name->text);
156 if (jd->isleafmethod)
157 printf("**Leafmethod**");
161 if (strcmp(m->class->name->text,"java/lang/String")==0)
162 if (strcmp(m->name->text,"toLowerCase")==0)
163 #if defined(LSRA_DEBUG_VERBOSE)
165 printf("12-------------------12\n");
167 { int dummy=1; dummy++; }
174 #if defined(ENABLE_STATISTICS)
175 /* find conflicts between locals for statistics */
177 /* local Variable Lifetimes are at the end of the lifetime array and */
178 /* have v_index >= 0 */
179 for (locals_start = ls->lifetimecount-1; (locals_start >=0) &&
180 (ls->lifetime[ls->lt_used[locals_start]].v_index >= 0);
182 for (i=locals_start + 1; i < ls->lifetimecount; i++)
183 for (j=i+1; j < ls->lifetimecount; j++)
184 if ( !((ls->lifetime[ls->lt_used[i]].i_end
185 < ls->lifetime[ls->lt_used[j]].i_start)
186 || (ls->lifetime[ls->lt_used[j]].i_end <
187 ls->lifetime[ls->lt_used[i]].i_start)) )
188 count_locals_conflicts += 2;
194 test_lifetimes( jd, gd );
200 void lsra_init(jitdata *jd)
202 lsradata *ls = jd->ls;
204 /* Init LSRA Data Structures */
205 /* allocate lifetimes for all Basicblocks */
210 graphdata *lsra_setup(jitdata *jd)
218 #if defined(ENABLE_LOOPS)
219 /* Loop optimization "destroys" the basicblock array */
220 /* TODO: work with the basicblock list */
222 log_text("lsra not possible with loop optimization\n");
234 /* Setup LSRA Data structures */
236 /* Generate the Control Flow Graph */
237 /* Add one for a Basic Block 0 to be inserted, so lateron */
238 /* with SSA Parameter initialization is handled right */
239 gd = graph_init(jd->basicblockcount + 1);
240 graph_make_cfg(jd, gd);
242 lt_lifeness_analysis(jd, gd);
244 #ifdef LSRA_DEBUG_VERBOSE
245 if (compileverbose) {
246 printf("Lifetimes after LifenessAnalyse: \n");
247 print_all_lifetimes(jd);
251 lsra_calc_lifetime_length(jd);
253 #ifdef LSRA_DEBUG_VERBOSE
254 if (compileverbose) {
255 printf("Basicblockcount: %4i\n",ls->basicblockcount);
261 void lsra_reg_setup(jitdata *jd,
262 struct lsra_register *int_reg,
263 struct lsra_register *flt_reg ) {
264 int i, j, iarg, farg;
267 bool *fltarg_used, *intarg_used;
276 int_reg->nregdesc = nregdescint;
277 flt_reg->nregdesc = nregdescfloat;
278 if (jd->isleafmethod) {
279 /* Temp and Argumentregister can be used as saved registers */
281 int_reg->sav_top = INT_ARG_CNT + INT_TMP_CNT + INT_SAV_CNT;
282 int_reg->sav_reg = DMNEW(int, int_reg->sav_top);
283 int_reg->tmp_reg = NULL;
284 int_reg->tmp_top = -1;
285 flt_reg->sav_top = FLT_ARG_CNT + FLT_TMP_CNT + FLT_SAV_CNT;
286 flt_reg->sav_reg = DMNEW(int, flt_reg->sav_top);
287 flt_reg->tmp_reg = NULL;
288 flt_reg->tmp_top = -1;
290 /* additionaly precolour registers for Local Variables acting as */
296 intarg_used = DMNEW(bool, INT_ARG_CNT);
297 for (i=0; i < INT_ARG_CNT; i++)
298 intarg_used[i]=false;
300 fltarg_used = DMNEW(bool, FLT_ARG_CNT);
301 for (i=0; i < FLT_ARG_CNT; i++)
302 fltarg_used[i]=false;
304 int_sav_top=int_reg->sav_top;
305 flt_sav_top=flt_reg->sav_top;
307 for (i=0; (i < md->paramcount); i++) {
308 if (!md->params[i].inmemory) {
309 if (IS_INT_LNG_TYPE(md->paramtypes[i].type)) {
310 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
311 if (IS_2_WORD_TYPE(md->paramtypes[i].type)) {
312 int_reg->sav_reg[--int_sav_top] =
313 GET_HIGH_REG(md->params[i].regoff);
314 intarg_used[GET_HIGH_REG(md->params[i].regoff)]=true;
315 /*used -> don't copy later on */
316 int_reg->sav_reg[--int_sav_top] =
317 GET_LOW_REG(md->params[i].regoff);
318 intarg_used[GET_LOW_REG(md->params[i].regoff)]=true;
319 /*used -> don't copy later on */
322 { /* !IS_2_WORD_TYPE(md->paramtypes[i].type */
323 int_reg->sav_reg[--int_sav_top] =
324 md->params[i].regoff;
325 intarg_used[md->params[i].regoff]=true;
326 /*used -> don't copy later on */
329 #if !defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
330 /* do not precolour float arguments if they are passed in */
331 /* integer registers. But these integer argument registers */
332 /* still be used in the method! */
333 else { /* IS_FLT_DBL_TYPE(md->paramtypes[i].type */
334 flt_reg->sav_reg[--flt_sav_top] =
335 md->params[i].regoff;
336 fltarg_used[md->params[i].regoff]=true;
343 /* copy rest of argument registers to flt_reg->sav_reg and */
344 /* int_reg->sav_reg; */
345 for (i=0; i < INT_ARG_CNT; i++)
347 int_reg->sav_reg[--int_sav_top]=i;
348 for (i=0; i < FLT_ARG_CNT; i++)
350 flt_reg->sav_reg[--flt_sav_top]=i;
352 /* copy temp registers to flt_reg->sav_reg and int_reg->sav_reg */
353 for (i=0; i < INT_TMP_CNT; i++)
354 int_reg->sav_reg[--int_sav_top]=rd->tmpintregs[i];
355 for (i=0; i < FLT_TMP_CNT; i++)
356 flt_reg->sav_reg[--flt_sav_top]=rd->tmpfltregs[i];
359 /* non leaf method -> use Argument Registers [arg[int|flt]reguse */
360 /* ... [INT|FLT]_ARG_CNT[ as temp reg */
361 /* divide temp and saved registers */
363 int argintreguse, argfltreguse;
365 /* with Locals as non SAVEDVAR, the used arg[int|flt] as in params */
366 /* of the method itself have to be regarded, or mismatch before */
367 /* block 0 with parameter copy could happen! */
369 argintreguse = max(rd->argintreguse, md->argintreguse);
370 argfltreguse = max(rd->argfltreguse, md->argfltreguse);
372 int_sav_top = int_reg->sav_top = INT_SAV_CNT;
373 int_reg->sav_reg = DMNEW(int, int_reg->sav_top);
374 int_reg->tmp_top = INT_TMP_CNT +
375 max(0, (INT_ARG_CNT - argintreguse));
376 int_reg->tmp_reg = DMNEW(int, int_reg->tmp_top);
378 flt_sav_top =flt_reg->sav_top = FLT_SAV_CNT;
379 flt_reg->sav_reg = DMNEW(int, flt_reg->sav_top);
380 flt_reg->tmp_top = FLT_TMP_CNT +
381 max(0 , (FLT_ARG_CNT - argfltreguse));
382 flt_reg->tmp_reg = DMNEW(int, flt_reg->tmp_top);
384 /* copy temp and unused argument registers to flt_reg->tmp_reg and */
385 /* int_reg->tmp_reg */
387 for (i=0; i < INT_TMP_CNT; i++)
388 int_reg->tmp_reg[i]=rd->tmpintregs[i];
390 /* quick and dirty patch for the drop of rd->argxxxreg[] - but will */
391 /* work anyhow on i386, !! has to be made "real" for other archs */
393 for (j = argintreguse; j < INT_ARG_CNT; j++, i++)
394 int_reg->tmp_reg[i]=j;
395 for (i=0; i < FLT_TMP_CNT; i++)
396 flt_reg->tmp_reg[i]=rd->tmpfltregs[i];
398 /* quick and dirty patch for the drop of rd->argxxxreg[] - but will */
399 /* work anyhow on i386, !! has to be made "real" for other archs */
401 for (j = argfltreguse; j < FLT_ARG_CNT; j++, i++)
402 flt_reg->tmp_reg[i]=j;
405 /* now copy saved registers to flt_reg->sav_reg and int_reg->sav_reg */
406 for (i = INT_SAV_CNT-1; i >= 0; i--)
407 int_reg->sav_reg[--int_sav_top]=rd->savintregs[i];
408 for (i = FLT_SAV_CNT-1; i >= 0; i--)
409 flt_reg->sav_reg[--flt_sav_top]=rd->savfltregs[i];
413 void lsra_insertion( struct lsradata *ls, int *a, int lo, int hi) {
416 for (i=lo+1; i<=hi; i++) {
418 t=ls->lifetime[a[j]].i_start;
420 while ((j>lo) && (ls->lifetime[a[j-1]].i_start > t)) {
428 void lsra_qsort( struct lsradata *ls, int *a, int lo, int hi) {
434 x = ls->lifetime[a[(lo+hi)/2]].i_start;
437 while (ls->lifetime[a[i]].i_start < x) i++;
438 while (ls->lifetime[a[j]].i_start > x) j--;
440 /* exchange a[i], a[j] */
450 if (lo < j) lsra_qsort( ls, a, lo, j);
451 if (i < hi) lsra_qsort( ls, a, i, hi);
453 lsra_insertion(ls, a, lo, hi);
457 void lsra_param_sort(struct lsradata *ls, int *lifetime, int lifetime_count) {
462 /* count number of parameters ( .i_start == -1) */
463 for (param_count=0; (param_count < lifetime_count) &&
464 /* (ls->lifetime[lifetime[param_count]].i_start == -1); param_count++); */
465 (ls->lifetime[lifetime[param_count]].i_start == 0); param_count++);
467 if (param_count > 0) {
468 /* now sort the parameters by v_index */
469 for (i=0; i < param_count -1; i++)
470 for (j=i+1; j < param_count; j++)
471 if ( ls->lifetime[lifetime[i]].v_index >
472 ls->lifetime[lifetime[j]].v_index) {
475 lifetime[i]=lifetime[j];
481 void lsra_main(jitdata *jd)
483 #ifdef LSRA_DEBUG_VERBOSE
488 struct lsra_register flt_reg, int_reg;
497 /* sort lifetimes by increasing start */
498 lsra_qsort( ls, ls->lt_mem, 0, ls->lt_mem_count - 1);
499 lsra_qsort( ls, ls->lt_int, 0, ls->lt_int_count - 1);
500 lsra_qsort( ls, ls->lt_flt, 0, ls->lt_flt_count - 1);
501 /* sort local vars used as parameter */
502 lsra_param_sort( ls, ls->lt_int, ls->lt_int_count);
503 lsra_param_sort( ls, ls->lt_flt, ls->lt_flt_count);
504 lsra_reg_setup(jd, &int_reg, &flt_reg);
506 #ifdef LSRA_DEBUG_VERBOSE
507 if (compileverbose) {
508 printf("INTSAV REG: ");
509 for (i=0; i<int_reg.sav_top; i++)
510 printf("%2i ",int_reg.sav_reg[i]);
511 printf("\nINTTMP REG: ");
512 for (i=0; i<int_reg.tmp_top; i++)
513 printf("%2i ",int_reg.tmp_reg[i]);
514 printf("\nFLTSAV REG: ");
515 for (i=0; i<flt_reg.sav_top; i++)
516 printf("%2i ",flt_reg.sav_reg[i]);
517 printf("\nFLTTMP REG: ");
518 for (i=0; i<flt_reg.tmp_top; i++)
519 printf("%2i ",flt_reg.tmp_reg[i]);
524 ls->active_tmp = DMNEW( struct lifetime *, max(INT_REG_CNT, FLT_REG_CNT));
525 ls->active_sav = DMNEW( struct lifetime *, max(INT_REG_CNT, FLT_REG_CNT));
527 lsra_reg_use=INT_SAV_CNT; /* init to no saved reg used... */
528 _lsra_main(jd, ls->lt_int, ls->lt_int_count, &int_reg,
530 if (lsra_reg_use > INT_SAV_CNT) lsra_reg_use=INT_SAV_CNT;
531 rd->savintreguse = lsra_reg_use;
533 lsra_reg_use = FLT_SAV_CNT; /* no saved reg used... */
535 _lsra_main(jd, ls->lt_flt, ls->lt_flt_count, &flt_reg, &lsra_reg_use);
536 if (lsra_reg_use > FLT_SAV_CNT) lsra_reg_use=FLT_SAV_CNT;
538 rd->savfltreguse=lsra_reg_use;
540 /* rd->memuse was already set in stack.c to allocate stack space for */
541 /* passing arguments to called methods */
542 #if defined(__I386__)
543 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
544 /* reserve 0(%esp) for Monitorenter/exit Argument on i386 */
550 lsra_mem_use = rd->memuse; /* Init with memuse from stack.c */
552 lsra_alloc(jd, ls->lt_mem, ls->lt_mem_count, &lsra_mem_use);
553 lsra_alloc(jd, ls->lt_int, ls->lt_int_count, &lsra_mem_use);
554 lsra_alloc(jd, ls->lt_flt, ls->lt_flt_count, &lsra_mem_use);
556 rd->memuse=lsra_mem_use;
558 #ifdef LSRA_DEBUG_VERBOSE
559 if (compileverbose) {
560 printf("Int RA complete \n");
561 printf("Lifetimes after splitting int: \n");
562 print_lifetimes(jd, ls->lt_int, ls->lt_int_count);
564 printf("Flt RA complete \n");
565 printf("Lifetimes after splitting flt:\n");
566 print_lifetimes(jd, ls->lt_flt, ls->lt_flt_count);
568 printf("Rest RA complete \n");
569 printf("Lifetimes after leftt:\n");
570 print_lifetimes(jd, ls->lt_mem, ls->lt_mem_count);
572 printf("jd->varcount: %i jd->vartop %i\n", jd->varcount, jd->vartop);
577 void lsra_alloc(jitdata *jd, int *lifet, int lifetimecount, int *mem_use)
581 struct freemem *fmem;
583 #ifdef HAS_4BYTE_STACKSLOT
584 struct freemem *fmem_2;
594 fmem=DNEW(struct freemem);
597 #ifdef HAS_4BYTE_STACKSLOT
598 fmem_2=DNEW(struct freemem);
603 for (lt_index = 0; lt_index < lifetimecount; lt_index ++) {
604 lt = ls->lifetime + lifet[lt_index];
608 if (lt->regoff == -1) {
610 #ifdef HAS_4BYTE_STACKSLOT
611 if (IS_2_WORD_TYPE(lt->type))
612 regoff = lsra_getmem(lt, fmem_2, mem_use);
615 regoff = lsra_getmem(lt, fmem, mem_use);
617 flags = lt->savedvar;
622 VAR(lt->v_index)->vv.regoff = regoff;
623 VAR(lt->v_index)->flags = flags;
627 int lsra_getmem(struct lifetime *lt, struct freemem *fmem, int *mem_use)
629 struct freemem *fm, *p;
631 /* no memmory allocated till now, or all other are still live */
632 if ((fmem->next == NULL) || (fmem->next->end > lt->i_start)) {
634 #ifdef HAS_4BYTE_STACKSLOT
635 if (IS_2_WORD_TYPE(lt->type))
636 if ( (*mem_use)&1 ) /* align memory location for 2 Word Types */
638 fm=lsra_getnewmem(mem_use);
639 if (IS_2_WORD_TYPE(lt->type))
640 /* allocate a second following Slot for 2 Word Types */
643 fm=lsra_getnewmem(mem_use);
646 /* Speicherstelle frei */
652 for (p=fmem; (p->next!=NULL) && (p->next->end < fm->end); p=p->next);
658 struct freemem *lsra_getnewmem(int *mem_use)
662 fm=DNEW(struct freemem);
669 void _lsra_main( jitdata *jd, int *lifet, int lifetimecount,
670 struct lsra_register *reg, int *reg_use)
676 bool temp; /* reg from temp registers (true) or saved registers (false) */
683 #if !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
686 if ((reg->tmp_top+reg->sav_top) == 0) {
688 /* no registers available */
689 for (lt_index = 0; lt_index < lifetimecount; lt_index++)
690 ls->lifetime[lifet[lt_index]].regoff = -1;
694 ls->active_tmp_top = 0;
695 ls->active_sav_top = 0;
697 for (lt_index = 0; lt_index < lifetimecount; lt_index++) {
698 lt = &(ls->lifetime[lifet[lt_index]]);
700 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
701 regsneeded = (lt->type == TYPE_LNG)?1:0;
704 lsra_expire_old_intervalls(jd, lt, reg);
708 lt->savedvar = SAVEDVAR;
710 if (lt->savedvar || jd->isleafmethod) {
711 /* use Saved Reg (in case of leafmethod all regs are saved regs) */
712 if (reg->sav_top > regsneeded) {
713 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
715 reg_index = PACK_REGS(reg->sav_reg[--reg->sav_top],
716 reg->sav_reg[--reg->sav_top]);
720 reg_index = reg->sav_reg[--reg->sav_top];
722 } else { /* use Temp Reg or if none is free a Saved Reg */
723 if (reg->tmp_top > regsneeded) {
725 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
727 reg_index = PACK_REGS(reg->tmp_reg[--reg->tmp_top],
728 reg->tmp_reg[--reg->tmp_top]);
731 reg_index = reg->tmp_reg[--reg->tmp_top];
734 if (reg->sav_top > regsneeded) {
736 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
738 reg_index = PACK_REGS(reg->sav_reg[--reg->sav_top],
739 reg->sav_reg[--reg->sav_top]);
742 reg_index = reg->sav_reg[--reg->sav_top];
745 if (reg_index == -1) /* no reg is available anymore... -> spill */
746 spill_at_intervall(jd, lt);
748 lt->regoff = reg_index;
750 lsra_add_active(lt, ls->active_tmp, &(ls->active_tmp_top));
752 if (reg->sav_top<*reg_use) *reg_use=reg->sav_top;
753 lsra_add_active(lt, ls->active_sav, &(ls->active_sav_top));
759 void lsra_add_active(struct lifetime *lt, struct lifetime **active,
764 for(i = 0; (i < *active_top) && (active[i]->i_end < lt->i_end); i++);
766 for(j = *active_top; j > i; j--) active[j] = active[j-1];
774 void lsra_expire_old_intervalls(jitdata *jd,
775 struct lifetime *lt, struct lsra_register *reg)
777 _lsra_expire_old_intervalls(jd, lt, reg, jd->ls->active_tmp,
778 &(jd->ls->active_tmp_top));
779 _lsra_expire_old_intervalls(jd, lt, reg, jd->ls->active_sav,
780 &(jd->ls->active_sav_top));
783 void _lsra_expire_old_intervalls(jitdata *jd, struct lifetime *lt,
784 struct lsra_register *reg,
785 struct lifetime **active, int *active_top)
789 for(i = 0; i < *active_top; i++) {
790 if (active[i]->i_end > lt->i_start) break;
792 /* make active[i]->reg available again */
793 if (jd->isleafmethod) {
794 /* leafmethod -> don't care about type -> put all again into */
796 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
797 if (active[i]->type == TYPE_LNG) {
798 reg->sav_reg[reg->sav_top++] = GET_LOW_REG(active[i]->reg);
799 reg->sav_reg[reg->sav_top++] = GET_HIGH_REG(active[i]->reg);
802 reg->sav_reg[reg->sav_top++] = active[i]->regoff;
804 /* no leafmethod -> distinguish between temp and saved register */
805 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
806 if (active[i]->type == TYPE_LNG) {
807 /* no temp and saved regs are packed together, so looking at */
808 /* LOW_REG is sufficient */
809 if ( reg->nregdesc[ GET_LOW_REG(active[i]->reg)] == REG_SAV) {
810 reg->sav_reg[reg->sav_top++] = GET_LOW_REG(active[i]->reg);
811 reg->sav_reg[reg->sav_top++] = GET_HIGH_REG(active[i]->reg);
813 reg->tmp_reg[reg->tmp_top++] = GET_LOW_REG(active[i]->reg);
814 reg->tmp_reg[reg->tmp_top++] = GET_HIGH_REG(active[i]->reg);
818 if ( reg->nregdesc[active[i]->regoff] == REG_SAV) {
819 reg->sav_reg[reg->sav_top++] = active[i]->regoff;
821 reg->tmp_reg[reg->tmp_top++] = active[i]->regoff;
826 /* active[0..i[ is to be removed */
827 /* -> move [i..*active_top[ to [0..*active_top-i[ */
828 for(k = 0, j = i; (j < *active_top); k++,j++)
829 active[k] = active[j];
835 void spill_at_intervall(jitdata *jd, struct lifetime *lt )
841 if (lt->savedvar || jd->isleafmethod) {
842 _spill_at_intervall(lt, ls->active_sav, &(ls->active_sav_top));
844 _spill_at_intervall(lt, ls->active_tmp, &(ls->active_tmp_top));
845 if (lt->regoff == -1) { /* kein tmp mehr frei gewesen */
846 _spill_at_intervall(lt, ls->active_sav, &(ls->active_sav_top));
851 void _spill_at_intervall(struct lifetime *lt, struct lifetime **active,
858 #endif /* USAGE_COUNT */
861 if (*active_top == 0) {
869 /* find intervall which ends later or equal than than lt and has the */
870 /* lowest usagecount lower than lt */
872 u_min = lt->usagecount;
873 for (; (i >= 0) && (active[i]->i_end >= lt->i_end); i--) {
874 if (active[i]->usagecount < u_min) {
875 u_min = active[i]->usagecount;
883 if ((active[i]->i_end >= lt->i_end) &&
884 (active[i]->usagecount < lt->usagecount)) {
885 #else /* USAGE_COUNT */
886 /* get last intervall from active */
887 if (active[i]->i_end > lt->i_end) {
888 #endif /* USAGE_COUNT */
890 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
891 /* Don't spill between one and two word int types */
892 if ((active[i]->type == TYPE_LNG) != (lt->type == TYPE_LNG))
896 lt->regoff = active[i]->regoff;
897 active[i]->regoff = -1;
900 for (j = i; j < *active_top; j++)
901 active[j] = active[j + 1];
903 lsra_add_active(lt, active, active_top);
909 void lsra_calc_lifetime_length(jitdata *jd)
915 int *icount_block, icount;
916 int flags; /* 0 INMEMORY -> ls->lt_mem */
917 /* 1 INTREG -> ls->lt_int */
918 /* 2 FLTREG -> ls->lt_flt */
924 icount_block = DMNEW(int, ls->basicblockcount);
925 icount_block[0] = icount = 0 /* + ls->max_vars_with_indices + 1 */;
926 for (i=1; i < ls->basicblockcount; i++) {
927 if (ls->sorted[i-1] != -1)
928 icount += ls->basicblocks[ls->sorted[i-1]]->icount + 1 +
929 ls->varcount_with_indices;
930 if (ls->sorted[i] != -1)
931 icount_block[i] = icount;
934 #ifdef LSRA_DEBUG_VERBOSE
935 if (compileverbose) {
936 printf("icount_block: ");
937 for (i=0; i < ls->basicblockcount; i++)
938 printf("(%3i-%3i) ",i, icount_block[i]);
944 for(lt_index = 0 ;lt_index < ls->lifetimecount; lt_index++) {
945 if ( ls->lifetime[lt_index].type != -1) { /* used lifetime */
946 /* remember lt_index in lt_sorted */
947 ls->lt_used[lifetimecount ++] = lt_index;
948 lt = &(ls->lifetime[lt_index]);
950 /* compute lt->bb_first_def, i_first_def, bb_last_use and */
953 _LSRA_ASSERT(lt->def != NULL);
954 /* _LSRA_ASSERT(lt->use != NULL);*/
958 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
959 /* prevent conflicts between lifetimes of type long by increasing */
960 /* the lifetime by one instruction */
961 /* i.e.(ri/rj) ... */
962 /* (rk/rl) ICMD_LNEG */
963 /* with i==l and/or j==k */
964 /* to resolve this during codegeneration a temporary register */
965 /* would be needed */
966 if (lt->type == TYPE_LNG)
970 /* distribute lifetimes to lt_int, lt_flt and lt_mem */
976 #if defined(HAS_4BYTE_STACKSLOT) && !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
989 #if defined(__I386__)
991 * for i386 put all floats in memory
999 { log_text("Unknown Type\n"); exit(1); }
1003 case 0: /* lt_used[lt_used_index] -> lt_rest */
1004 ls->lt_mem[ ls->lt_mem_count++ ] = lt_index;
1006 case 1: /* l->lifetimes -> lt_int */
1007 ls->lt_int[ ls->lt_int_count++ ] = lt_index;
1009 case 2: /* l->lifetimes -> lt_flt */
1010 ls->lt_flt[ ls->lt_flt_count++ ] = lt_index;
1015 if (lt->bb_first_def < -1) {
1016 printf("--------- Warning: variable not defined!------------------vi: %i start: %i end: %i\n", lt->v_index, lt->i_start, lt->i_end);
1017 lt->bb_first_def = 0;
1018 lt->i_first_def = 0;
1021 lt->i_start = icount_block[lt->bb_first_def] + lt->i_first_def;
1023 if (lt->bb_last_use == -1) {
1024 /* unused Vars are not regarded by lifeness_analysis! */
1025 _LSRA_ASSERT(lt->def != NULL)
1026 _LSRA_ASSERT(lt->def->next == NULL)
1028 if (compileverbose) {
1029 printf("--------- Warning: variable not used! ---------");
1030 printf("vi: %i start: %i end: %i\n", lt->v_index,
1031 lt->i_start, lt->i_end);
1033 lt->bb_last_use = lt->bb_first_def;
1034 lt->i_last_use = lt->i_first_def;
1037 lt->i_end = icount_block[lt->bb_last_use] + lt->i_last_use;
1039 if (lt->i_start < 0)
1040 printf("------- Error: Lt %3i Vi %4i invalid!\n",lt_index, lt->v_index);
1042 if (lt->i_start > lt->i_end)
1043 printf("--------- Warning: last use before first def! vi: %i start: %i end: %i\n", lt->v_index, lt->i_start, lt->i_end);
1045 #ifdef USAGE_PER_INSTR
1046 lt->usagecount = lt->usagecount / ( lt->i_end - lt->i_start + 1);
1050 ls->lifetimecount = lifetimecount;
1053 #ifdef LSRA_DEBUG_VERBOSE
1054 void print_lifetimes(jitdata *jd, int *lt, int lifetimecount)
1064 for (lt_index = 0; lt_index < lifetimecount; lt_index++) {
1065 n = ls->lifetime + lt[lt_index];
1066 printf("i_Start: %3i(%3i,%3i) i_stop: %3i(%3i,%3i) reg: %3i VI: %3i type: %3i flags: %3i usage: %3li ltflags: %xi \n",n->i_start, ls->sorted[n->bb_first_def], n->i_first_def,n->i_end, ls->sorted[n->bb_last_use], n->i_last_use,n->regoff,n->v_index,n->type,n->flags, n->usagecount, n->flags);
1068 printf( "%3i Lifetimes printed \n",lt_index);
1071 void print_all_lifetimes(jitdata *jd)
1081 for (lt_index = 0; lt_index < ls->lifetimecount; lt_index++) {
1082 n = &(ls->lifetime[lt_index]);
1083 if (n->type != -1) {
1084 printf("i_Start: %3i(%3i,%3i) i_stop: %3i(%3i,%3i) VI: %3i type: %3i flags: %3i usage: %3li ltflags: %xi \n",n->i_start, ls->sorted[n->bb_first_def], n->i_first_def,n->i_end, ls->sorted[n->bb_last_use], n->i_last_use,n->v_index,n->type,n->flags, n->usagecount, n->flags);
1087 printf( "%3i Lifetimes printed \n",lt_index);
1092 * These are local overrides for various environment variables in Emacs.
1093 * Please do not remove this and leave it at the end of the file, where
1094 * Emacs will automagically detect them.
1095 * ---------------------------------------------------------------------
1098 * indent-tabs-mode: t