CACAO
lsra.cpp
Go to the documentation of this file.
1 /* src/vm/jit/allocator/lsra.cpp - linear scan register allocator
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
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.
12 
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.
17 
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
21  02110-1301, USA.
22 
23 */
24 
25 
26 #include "config.h"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <limits.h>
32 
33 #include "arch.hpp"
34 #include "md-abi.hpp"
35 
36 #include "vm/types.hpp"
37 
38 #include "mm/memory.hpp"
39 
40 #include "toolbox/logging.hpp"
41 
42 #include "vm/jit/builtin.hpp"
43 #include "vm/exceptions.hpp"
44 #include "vm/resolve.hpp"
45 #include "vm/options.hpp"
46 #include "vm/statistics.hpp"
47 
48 #include "vm/jit/abi.hpp"
49 #include "vm/jit/reg.hpp"
50 
53 
54 STAT_DECLARE_VAR(int,count_locals_conflicts,0)
55 
56 #ifdef USAGE_COUNT
57 extern char **prof_m_names;
58 extern u4 **prof_bb_freq;
59 extern int prof_size;
60 #endif
61 
62 /* function prototypes */
63 void lsra_init(jitdata *);
64 void lsra_setup(jitdata *);
65 void lsra_main(jitdata *);
66 
67 void lsra_reg_setup(jitdata *, struct lsra_register *, struct lsra_register * );
69 void _lsra_main( jitdata *, int *, int, struct lsra_register *, int *);
71  struct lsra_register *);
72 void spill_at_intervall(jitdata *, struct lifetime *);
73 void lsra_add_active(struct lifetime *, struct lifetime **, int *);
75  struct lsra_register *, struct lifetime **,
76  int *);
77 void _spill_at_intervall(struct lifetime *, struct lifetime **, int *);
78 
79 void lsra_alloc(jitdata *, int *, int, int *);
80 int lsra_getmem(struct lifetime *, struct freemem *, int *);
81 struct freemem *lsra_getnewmem(int *);
82 void lsra_setflags(int *, int);
83 
84 #ifdef LSRA_DEBUG_VERBOSE
86 void print_lifetimes(jitdata *, int *, int);
87 #endif
88 
89 #if !defined(LV)
91 void lsra_join_lifetimes(jitdata *, int);
92 
93 void _lsra_new_stack( lsradata *, stackelement_t* , int , int, int);
94 void _lsra_from_stack(lsradata *, stackelement_t* , int , int, int);
95 void lsra_add_ss(struct lifetime *, stackelement_t* );
96 void lsra_usage_local(lsradata *, s4 , int , int , int , int );
97 #endif
98 
99 
100 
101 bool lsra(jitdata *jd)
102 {
103 #if defined(ENABLE_STATISTICS)
104  lsradata *ls;
105  int locals_start;
106  int i,j;
107 #endif
108 #if defined(LSRA_DEBUG_CHECK)
109  methodinfo *m;
110  int b_index;
111  stackelement_t* in,out;
112  int ind, outd;
113 #endif
114 
115 #if defined(LSRA_DEBUG_CHECK)
116  m = jd->m;
117  b_index = 0;
118  while (b_index < m->basicblockcount ) {
119 
120  if (m->basicblocks[b_index].flags >= basicblock::REACHED) {
121 
122  in=m->basicblocks[b_index].instack;
123  ind=m->basicblocks[b_index].indepth;
124  for (;ind != 0;in=in->prev, ind--) {
125  /* ARGVAR or LOCALVAR in instack is ok*/
126 #if 0
127  if (in->varkind == ARGVAR) printf("ARGVAR in instack: \n");
128  if (in->varkind == LOCALVAR) printf("LOCALVAR in instack\n");
129 #endif
130  }
131  out=m->basicblocks[b_index].outstack;
132  outd=m->basicblocks[b_index].outdepth;
133  for (;outd != 0;out=out->prev, outd--) {
134  if (out->varkind == ARGVAR)
135  { log_text("ARGVAR in outstack\n"); assert(0); }
136  if (out->varkind == LOCALVAR)
137  { log_text("LOCALVAR in outstack\n"); assert(0); }
138  }
139  }
140  b_index++;
141  }
142 #endif
143 
144  jd->ls = DNEW(lsradata);
145  lsra_init(jd);
146  lsra_setup(jd);
147 
148 #if defined(ENABLE_STATISTICS)
149  /* find conflicts between locals for statistics */
150  ls = jd->ls;
151  /* local Variable Lifetimes are at the end of the lifetime array and */
152  /* have v_index >= 0 */
153  for (locals_start = ls->lifetimecount-1; (locals_start >=0) &&
154  (ls->lifetime[ls->lt_used[locals_start]].v_index >= 0);
155  locals_start--);
156  for (i=locals_start + 1; i < ls->lifetimecount; i++)
157  for (j=i+1; j < ls->lifetimecount; j++)
158  if ( !((ls->lifetime[ls->lt_used[i]].i_end
159  < ls->lifetime[ls->lt_used[j]].i_start)
160  || (ls->lifetime[ls->lt_used[j]].i_end <
161  ls->lifetime[ls->lt_used[i]].i_start)) ) {
162  count_locals_conflicts += 2;
163  STATISTICS(count_locals_conflicts += 2);
164  }
165 #endif
166  /* Run LSRA */
167  lsra_main(jd);
168 
169  /* everything's ok */
170 
171  return true;
172 }
173 
174 /* sort Basic Blocks using Depth First Search in reverse post order in */
175 /* ls->sorted. */
176 void lsra_DFS(jitdata *jd) {
177  int *stack;
178  int *visited;
179  int stack_top;
180  bool not_finished;
181  int i,p;
182  struct _list *succ;
183 
184  methodinfo *m;
185  lsradata *ls;
186 
187  m = jd->m;
188  ls = jd->ls;
189 
190  stack = DMNEW( int, m->basicblockcount + 1);
191  visited = (int *)DMNEW( int, m->basicblockcount + 1);
192  for (i = 0; i <= m->basicblockcount; i++) {
193  visited[i] = 0;
194  ls->sorted[i]=-1;
195  ls->sorted_rev[i]=-1;
196  }
197 
198  stack[0] = 0; /* start with Block 0 */
199  stack_top = 1;
200  visited[0] = ls->num_pred[0]; /* Start Block is handled right and can be */
201  /* put in sorted */
202  p = 0;
203  not_finished = true;
204  while (not_finished) {
205  while (stack_top != 0) {
206  stack_top--;
207  i = stack[stack_top];
208  ls->sorted[p]=i;
209  p++; /*--;*/
210  for (succ = ls->succ[i]; succ != NULL; succ = succ->next) {
211  visited[succ->value]++;
212  if (visited[succ->value] == ls->num_pred[succ->value]) {
213  /* push the node on the stack, only if all ancestors have */
214  /* been visited */
215  stack[stack_top] = succ->value;
216  stack_top++;
217  }
218  }
219  }
220  not_finished = false;
221  for (i=1; i <= m->basicblockcount; i++) {
222  /* search for visited blocks, which have not reached the num_pred */
223  /* and put them on the stack -> happens with backedges */
224  if ((visited[i] != 0) && (visited[i] < ls->num_pred[i])) {
225  stack[stack_top] = i;
226  stack_top++;
227  visited[i] = ls->num_pred[i];
228  not_finished=true;
229  break;
230  }
231  }
232  }
233 }
234 
235 void lsra_get_backedges_(lsradata *ls, int basicblockcount) {
236  struct _list *s;
237  struct _backedge *n;
238  struct _backedge *_backedges;
239  int i;
240 
241 
242  _backedges = NULL;
243 
244  /* now look for backedges */
245  ls->backedge_count = 0;
246  for(i=0; i < basicblockcount; i++) {
247  if (ls->sorted[i] != -1)
248  for(s=ls->succ[ls->sorted[i]]; s != NULL; s=s->next) {
249  if (i >= ls->sorted_rev[s->value]) {
250  n=DNEW(struct _backedge);
251  n->start = max(i, ls->sorted_rev[s->value]);
252  n->end = min(i, ls->sorted_rev[s->value]);
253  n->next = _backedges;
254  _backedges = n;
255  ls->backedge_count++;
256  }
257  }
258  }
259  /* put _backedges in ls->backedge array */
260  ls->backedge = DMNEW(struct _backedge *, ls->backedge_count);
261  for (n=_backedges, i=0; n != NULL; n=n->next, i++) {
262  ls->backedge[i] = n;
263  ls->backedge[i]->nesting = 1;
264  }
265 }
266 
268  methodinfo *m;
269  lsradata *ls;
270  int i,j, end;
271  struct _backedge *n;
272 
273  m = jd->m;
274  ls = jd->ls;
275 
276  for (i=0; i <= m->basicblockcount; i++)
277  if (ls->sorted[i] != -1)
278  ls->sorted_rev[ls->sorted[i]]=i;
279 
280  lsra_get_backedges_(ls, m->basicblockcount + 1);
281  /* - sort backedge by increasing end: */
282  for (i=0; i < ls->backedge_count; i++)
283  for (j=i+1; j < ls->backedge_count; j++)
284  if ((ls->backedge[i]->end > ls->backedge[j]->end) || /* -> swap */
285  ((ls->backedge[i]->end == ls->backedge[j]->end) &&
286  (ls->backedge[i]->start > ls->backedge[j]->start) )) {
287  n=ls->backedge[i];
288  ls->backedge[i]=ls->backedge[j];
289  ls->backedge[j]=n;
290  }
291 
292  /* create ls->nesting */
293  /* look for nesting depth (overlapping backedges*/
294  for (i=0; i < ls->backedge_count - 1; i++) {
295  for (j = i + 1; (j < ls->backedge_count) &&
296  (ls->backedge[i]->start >= ls->backedge[j]->end); j++)
297  ls->backedge[j]->nesting += ls->backedge[i]->nesting;
298  }
299 
300  i = 0;
301  j = 0;
302  while ( (i < m->basicblockcount + 1) ) {
303  if (j < ls->backedge_count) {
304  while ( i < ls->backedge[j]->end ) {
305  ls->nesting[i] = 0;
306  i++;
307  }
308  if ( (j+1) < ls->backedge_count)
309  end = min(ls->backedge[j]->start, ls->backedge[j+1]->end - 1);
310  else
311  end = ls->backedge[j]->start;
312  while (i <= end) {
313  ls->nesting[i] = ls->backedge[j]->nesting;
314  i++;
315  }
316  j++;
317  } else {
318  ls->nesting[i] = 0;
319  i++;
320  }
321  }
322 
323 #ifdef LSRA_DEBUG_VERBOSE
324  if (compileverbose) {
325  printf("sorted: \n");
326  for (i=0; i < ls->backedge_count; i++)
327  printf("Backedge: %i - %i, %i - %i\n", ls->sorted[ls->backedge[i]->start], ls->sorted[ls->backedge[i]->end], ls->backedge[i]->start, ls->backedge[i]->end);
328  printf("Nesting Level \n");
329  for (i=0; i<m->basicblockcount; i++) printf(" %3li", ls->nesting[i]);
330  printf("\n");
331  }
332 #endif
333  for (i=0; i <= m->basicblockcount; i++) {
334  ls->sorted_rev[i] = -1;
335  ls->nesting[i] = 1+ls->nesting[i]*ls->nesting[i]*10;
336  }
337 }
338 
340  methodinfo *m;
341  lsradata *ls;
342  struct _list **next;
343  struct _backedge *n;
344  int i,j;
345  bool merged;
346 
347  m = jd->m;
348  ls = jd->ls;
349 
350  /* first remove artificial end basicblock from ls->sorted, succ and pred */
351  j=-1;
352  for (i=0; i < m->basicblockcount; i++) {
353  for (next=&(ls->succ[i]); *next != NULL; next=&((*next)->next)) {
354  if ( (*next)->value == m->basicblockcount ) {
355  /* artificial end bb found */
356  *next = (*next)->next;
357  if (*next == NULL) break;
358  }
359  }
360  for (next=&(ls->pred[i]); *next != NULL; next=&((*next)->next)) {
361  if ( (*next)->value == m->basicblockcount ) {
362  /* artificial end bb found */
363  *next = (*next)->next;
364  if (*next == NULL) break;
365  }
366  }
367  if (j==-1)
368  if (ls->sorted[i] == m->basicblockcount) j=i;
369  }
370 
371  /* if an artificial end block was removed -> change ls->sorted accordingly*/
372  if (j!=-1)
373  for (i=j+1; i <= m->basicblockcount; i++) {
374  ls->sorted[i-1] = ls->sorted[i];
375  ls->nesting[i-1] = ls->nesting[i];
376  }
377 
378  for (i=0; i < m->basicblockcount; i++)
379  if (ls->sorted[i] != -1)
380  ls->sorted_rev[ls->sorted[i]]=i;
381 
382  lsra_get_backedges_(ls, m->basicblockcount);
383 
384  /* - sort backedge by increasing start */
385  for (i=0; i < ls->backedge_count; i++)
386  for (j=i+1; j < ls->backedge_count; j++)
387  if (ls->backedge[i]->start > ls->backedge[j]->start) {
388  /* -> swap */
389  n = ls->backedge[i];
390  ls->backedge[i] = ls->backedge[j];
391  ls->backedge[j] = n;
392  }
393 
394 #ifdef LSRA_DEBUG_VERBOSE
395  if (compileverbose) {
396  printf("sorted: \n");
397  for (i=0; i < ls->backedge_count; i++)
398  printf("Backedge: %i - %i, %i - %i\n",
399  ls->sorted[ls->backedge[i]->start],
400  ls->sorted[ls->backedge[i]->end], ls->backedge[i]->start,
401  ls->backedge[i]->end);
402  printf("Nesting Level \n");
403  for (i=0; i<m->basicblockcount; i++) printf(" %3li", ls->nesting[i]);
404  printf("\n");
405  }
406 #endif
407 
408  merged = true;
409  /* - merge overlapping backedges */
410  while (merged) {
411  merged = false;
412  for (i=0; i < ls->backedge_count-1; i++) {
413  if (ls->backedge[i] != NULL) {
414  for (j = i + 1; (j < ls->backedge_count) && (ls->backedge[j] == NULL); j++ );
415  if (j != ls->backedge_count) {
416  if (ls->backedge[i]->start >= ls->backedge[j]->end) {
417  merged = true;
418  /* overlapping -> merge */
419  ls->backedge[j]->end = min (ls->backedge[j]->end,
420  ls->backedge[i]->end);
421  ls->backedge[i] = NULL;
422  }
423  }
424  }
425  }
426  }
427 #ifdef LSRA_DEBUG_VERBOSE
428  if (compileverbose) {
429  printf("merged: \n");
430  for (i = 0; i < ls->backedge_count; i++)
431  if (ls->backedge[i] != NULL)
432  printf("Backedge: %i - %i, %i - %i\n",
433  ls->sorted[ls->backedge[i]->start],
434  ls->sorted[ls->backedge[i]->end],
435  ls->backedge[i]->start, ls->backedge[i]->end);
436  }
437 #endif
438  /* - remove backedge[] == NULL from array */
439 
440  for (j = ls->backedge_count - 1; ((j>=0) && (ls->backedge[j] == NULL));
441  j--);
442  i=j;
443  while (i >= 0) {
444  if (ls->backedge[i] == NULL) { /* swap backedge[i] and backedge[j]*/
445  n=ls->backedge[j];
446  ls->backedge[j] = ls->backedge[i];
447  ls->backedge[i] = n;
448  i--;
449  j--;
450  ls->backedge_count--;
451  } else i--;
452  }
453 #ifdef LSRA_DEBUG_VERBOSE
454  if (compileverbose) {
455  printf("ready: \n");
456  for (i=0; i < ls->backedge_count; i++)
457  printf("Backedge: %i - %i, %i - %i\n",
458  ls->sorted[ls->backedge[i]->start],
459  ls->sorted[ls->backedge[i]->end],ls->backedge[i]->start,
460  ls->backedge[i]->end);
461  }
462 #endif
463 }
464 
465 void lsra_add_cfg(jitdata *jd, int from, int to) {
466  struct _list *n;
467  methodinfo *m;
468  lsradata *ls;
469 
470  m = jd->m;
471  ls = jd->ls;
472 
473  /* ignore Empty, Deleted,... Basic Blocks as target */
474  /* TODO: Setup BasicBlock array before to avoid this */
475  /* best together with using the basicblock list, so lsra works */
476  /* with opt_loops, too */
477  for (;(to < m->basicblockcount) && (m->basicblocks[to].flags < basicblock::REACHED); to++);
478 
479  for (n=ls->succ[from]; (n!= NULL) && (n->value != to); n=n->next);
480  if (n != NULL) return; /* edge from->to already existing */
481 
482  n=DNEW(struct _list);
483 
484  n->value=to;
485  n->next=ls->succ[from];
486  ls->succ[from]=n;
487 
488  n=DNEW(struct _list);
489  n->value=from;
490  n->next=ls->pred[to];
491  ls->pred[to]=n;
492  ls->num_pred[to]++;
493 }
494 
495 /* add Edges from guarded Areas to Exception handlers in the CFG */
497  methodinfo *m;
498  lsradata *ls;
499  int i;
500  exceptiontable *ex;
501 
502 
503  m = jd->m;
504  ls = jd->ls;
505 
506  ex = jd->exceptiontable;
507 
508  /* add cfg edges from all bb of a try block to the start of the according */
509  /* exception handler to ensure the right order after depthfirst search */
510 
511 #ifdef LSRA_DEBUG_VERBOSE
512  if (compileverbose)
513  printf("ExTable(%i): ", jd->exceptiontablelength);
514 #endif
515 
516  for (; ex != NULL; ex = ex->down) {
517 
518 #ifdef LSRA_DEBUG_VERBOSE
519  if (compileverbose) {
520  printf("[%i-%i]->%i ",ex->start->nr, ex->end->nr,
521  ex->handler->nr);
522  if (ex->handler->nr >= m->basicblockcount) {
523  log_text("Exceptionhandler Basicblocknummer invalid\n");
524  abort();
525  }
526  if (m->basicblocks[ex->handler->nr].flags < basicblock::REACHED) {
527  log_text("Exceptionhandler Basicblocknummer not reachable\n");
528  abort();
529  }
530  if (ex->start->nr > ex->end->nr) {
531  log_text("Guarded Area starts after its end\n");
532  abort();
533  }
534  }
535 #endif
536  /* loop all valid Basic Blocks of the guarded area and add CFG edges */
537  /* to the appropriate handler */
538  for (i=ex->start->nr; (i <= ex->end->nr) &&
539  (i < m->basicblockcount); i++)
540  if (m->basicblocks[i].flags >= basicblock::REACHED)
541  lsra_add_cfg(jd, i, ex->handler->nr);
542  }
543 #ifdef LSRA_DEBUG_VERBOSE
544  if (compileverbose) {
545  printf("\n");
546  }
547 #endif
548 }
549 
550 void lsra_add_jsr(jitdata *jd, int from, int to) {
551  methodinfo *m;
552  lsradata *ls;
553  struct _sbr *sbr, *n;
554  struct _list *ret;
555 
556  ls = jd->ls;
557  m = jd->m;
558 
559  /* ignore Empty, Deleted,... Basic Blocks as target */
560  /* TODO: Setup BasicBlock array before to avoid this */
561  /* best together with using the basicblock list, so lsra works */
562  /* with opt_loops, too */
563  for (; (to < m->basicblockcount) && (m->basicblocks[to].flags < basicblock::REACHED);
564  to++);
565 #ifdef LSRA_DEBUG_CHECK
566  if (to == m->basicblockcount)
567  { log_text("Invalid subroutine start index\n"); assert(0); }
568 #endif
569 
570  lsra_add_cfg(jd, from, to);
571 
572  /* from + 1 ist the return Basic Block Index */
573  for (from++; (from < m->basicblockcount) &&
574  (m->basicblocks[from].flags < basicblock::REACHED); from++);
575 #ifdef LSRA_DEBUG_CHECK
576  if (from == m->basicblockcount)
577  { log_text("Invalid return basic block index for jsr\n"); assert(0); }
578 #endif
579 
580  /* add subroutine info in ls->sbr.next */
581 
582  /* search for right place to insert */
583  for (sbr = &(ls->sbr); (sbr->next != NULL) && (sbr->next->header < to); sbr=sbr->next);
584 
585  if ((sbr->next!= NULL) && (sbr->next->header == to)) {
586  /* Entry for this sub already exist */
587  sbr = sbr->next;
588  } else {
589  /* make new Entry and insert it in ls->sbr.next */
590  n = DNEW( struct _sbr );
591  n->header = to;
592  n->ret = NULL;
593 
594  n->next = sbr->next;
595  sbr->next = n;
596 
597  sbr = n;
598  }
599 
600  /* now insert return adress in sbr->ret */
601  ret = DNEW( struct _list);
602  ret->value = from;
603  ret->next = sbr->ret;
604  sbr->ret = ret;
605 }
606 
607 void lsra_add_sub( jitdata *jd, int b_index, struct _list *ret,
608  bool *visited )
609 {
610  methodinfo *m;
611  lsradata *ls;
612  struct _list *l;
613  instruction *ip;
614  bool next_block;
615 
616  m = jd->m;
617  ls = jd->ls;
618 
619  /* break at virtual End Block */
620  if (b_index != m->basicblockcount) {
621  visited[b_index] = true;
622  next_block = false;
623 
624  if (m->basicblocks[b_index].flags < basicblock::REACHED)
625  next_block = true;
626  if (!next_block && !(m->basicblocks[b_index].icount))
627  next_block = true;
628 
629  if (!next_block) {
630  ip = m->basicblocks[b_index].iinstr
631  + m->basicblocks[b_index].icount -1;
632 
633  if (ip->opc == ICMD_JSR) /* nested Subroutines */
634  next_block = true;
635  }
636 
637  if (!next_block) {
638  if (ip->opc == ICMD_RET) {
639  /* subroutine return found -> add return adresses to CFG */
640  for (l = ret; l != NULL; l = l->next)
641  lsra_add_cfg(jd, b_index, l->value);
642  } else { /* follow CFG */
643  for ( l = ls->succ[b_index]; l != NULL; l = l->next)
644  if (!visited[l->value])
645  lsra_add_sub(jd, l->value, ret, visited);
646  }
647  } else { /* fall through to next block */
648  if (!visited[b_index + 1])
649  lsra_add_sub(jd, b_index + 1, ret, visited);
650  }
651  }
652 }
653 
654 /* Add subroutines from ls->sbr list to CFG */
656  methodinfo *m;
657  lsradata *ls;
658  struct _sbr *sbr;
659  bool *visited;
660  int i;
661 #ifdef LSRA_DEBUG_VERBOSE
662  struct _list *ret;
663 #endif
664 
665  m = jd->m;
666  ls = jd->ls;
667 
668  visited = (bool *)DMNEW(int, m->basicblockcount + 1);
669  for (i=0; i <= m->basicblockcount; i++) visited[i] = false;
670  for (sbr = ls->sbr.next; sbr != NULL; sbr=sbr->next) {
671 
672 #ifdef LSRA_DEBUG_VERBOSE
673  if (compileverbose) {
674  printf("Subroutine Header: %3i Return Adresses:",sbr->header);
675  for (ret = sbr->ret; ret != NULL; ret = ret->next)
676  printf(" %3i", ret->value);
677  printf("\n");
678  }
679 #endif
680  lsra_add_sub(jd, sbr->header, sbr->ret, visited );
681  }
682 }
683 
684 /* Generate the Control Flow Graph */
685 /* ( pred,succ,num_pred of lsradata structure) */
686 
688  methodinfo *m;
689  lsradata *ls;
690  instruction *ip;
691  s4 *s4ptr;
692  int high, low, count;
693  int b_index, len;
694 
695  m = jd->m;
696  ls = jd->ls;
697 
698  b_index=0;
699  while (b_index < m->basicblockcount ) {
700  if ((m->basicblocks[b_index].flags >= basicblock::REACHED) &&
701  (len = m->basicblocks[b_index].icount)) {
702  /* block is valid and contains instructions */
703 
704  /* set ip to last instruction */
705  ip = m->basicblocks[b_index].iinstr +
706  m->basicblocks[b_index].icount -1;
707  while ((len>0) && (ip->opc == ICMD_NOP)) {
708  len--;
709  ip--;
710  }
711  switch (ip->opc) { /* check type of last instruction */
712  case ICMD_RETURN:
713  case ICMD_IRETURN:
714  case ICMD_LRETURN:
715  case ICMD_FRETURN:
716  case ICMD_DRETURN:
717  case ICMD_ARETURN:
718  case ICMD_ATHROW:
719  lsra_add_cfg(jd, b_index, m->basicblockcount);
720  break; /* function returns -> end of graph */
721 
722  case ICMD_IFNULL:
723  case ICMD_IFNONNULL:
724  case ICMD_IFEQ:
725  case ICMD_IFNE:
726  case ICMD_IFLT:
727  case ICMD_IFGE:
728  case ICMD_IFGT:
729  case ICMD_IFLE:
730  case ICMD_IF_LEQ:
731  case ICMD_IF_LNE:
732  case ICMD_IF_LLT:
733  case ICMD_IF_LGE:
734  case ICMD_IF_LGT:
735  case ICMD_IF_LLE:
736  case ICMD_IF_ICMPEQ:
737  case ICMD_IF_ICMPNE:
738  case ICMD_IF_ICMPLT:
739  case ICMD_IF_ICMPGE:
740  case ICMD_IF_ICMPGT:
741  case ICMD_IF_ICMPLE:
742  case ICMD_IF_LCMPEQ:
743  case ICMD_IF_LCMPNE:
744  case ICMD_IF_LCMPLT:
745  case ICMD_IF_LCMPGE:
746  case ICMD_IF_LCMPGT:
747  case ICMD_IF_LCMPLE:
748  case ICMD_IF_ACMPEQ:
749  case ICMD_IF_ACMPNE: /* branch -> add next block */
750  lsra_add_cfg(jd, b_index, b_index+1);
751  /* fall throu -> add branch target */
752 
753  case ICMD_GOTO:
754  lsra_add_cfg(jd, b_index, m->basicblockindex[ip->op1]);
755  break; /* visit branch (goto) target */
756 
757  case ICMD_TABLESWITCH: /* switch statement */
758  s4ptr = ip->val.a;
759 
760  lsra_add_cfg(jd, b_index, m->basicblockindex[*s4ptr]);
761 
762  s4ptr++;
763  low = *s4ptr;
764  s4ptr++;
765  high = *s4ptr;
766 
767  count = (high-low+1);
768 
769  while (--count >= 0) {
770  s4ptr++;
771  lsra_add_cfg(jd, b_index,
772  m->basicblockindex[*s4ptr]);
773  }
774  break;
775 
776  case ICMD_LOOKUPSWITCH: /* switch statement */
777  s4ptr = ip->val.a;
778 
779  lsra_add_cfg(jd, b_index, m->basicblockindex[*s4ptr]);
780 
781  ++s4ptr;
782  count = *s4ptr++;
783 
784  while (--count >= 0) {
785  lsra_add_cfg(jd, b_index,
786  m->basicblockindex[s4ptr[1]]);
787  s4ptr += 2;
788  }
789  break;
790 
791  case ICMD_JSR:
792  lsra_add_jsr(jd, b_index, m->basicblockindex[ip->op1]);
793  break;
794 
795  case ICMD_RET:
796  break;
797 
798  default:
799  lsra_add_cfg(jd, b_index, b_index + 1 );
800  break;
801  } /* switch (ip->opc)*/
802  } /* if ((m->basicblocks[blockIndex].icount)&& */
803  /* (m->basicblocks[b_index].flags >= basicblock::REACHED)) */
804  b_index++;
805  } /* while (b_index < m->basicblockcount ) */
806 }
807 
808 void lsra_init(jitdata *jd) {
809  methodinfo *m;
810  lsradata *ls;
811  int i;
812 
813  ls = jd->ls;
814  m = jd->m;
815 
816  /* Init LSRA Data Structures */
817  /* allocate lifetimes for all Basicblocks */
818  /* + 1 for an artificial exit node */
819  /* which is needed as "start" point for the reverse postorder sorting */
820  ls->pred = DMNEW(struct _list *, m->basicblockcount+1);
821  ls->succ = DMNEW(struct _list *, m->basicblockcount+1);
822  ls->sorted = DMNEW(int , m->basicblockcount+1);
823  ls->sorted_rev = DMNEW(int , m->basicblockcount+1);
824  ls->num_pred = DMNEW(int , m->basicblockcount+1);
825  ls->nesting = DMNEW(long , m->basicblockcount+1);
826  for (i=0; i<m->basicblockcount; i++) {
827  ls->pred[i]=NULL;
828  ls->succ[i]=NULL;
829  ls->sorted[i]=-1;
830  ls->sorted_rev[i]=-1;
831  ls->num_pred[i]=0;
832  ls->nesting[i] = 1;
833  }
834  ls->pred[m->basicblockcount]=NULL;
835  ls->succ[m->basicblockcount]=NULL;
836  ls->sorted[m->basicblockcount]=-1;
837  ls->sorted_rev[m->basicblockcount]=-1;
838  ls->num_pred[m->basicblockcount]=0;
839 
840  ls->sbr.next = NULL;
841 
842  ls->v_index = -1;
843 }
844 
845 void lsra_setup(jitdata *jd) {
846  methodinfo *m;
847  codegendata *cd;
848  lsradata *ls;
849 
850 #ifdef LSRA_DEBUG_VERBOSE
851  basicblock *bptr;
852  struct _list *nl;
853 #endif
854 
855  int i;
856 
857 #if !defined(LV)
858  registerdata *rd;
859 
860  rd = jd->rd;
861 #endif
862 
863  ls = jd->ls;
864  m = jd->m;
865  cd = jd->cd;
866 
867 #if defined(ENABLE_LOOP)
868  /* Loop optimization "destroys" the basicblock array */
869  /* TODO: work with the basicblock list */
870  if (opt_loops) {
871  log_text("lsra not possible with loop optimization\n");
872  abort();
873  }
874 #endif /* defined(ENABLE_LOOP) */
875 
876  /* Setup LSRA Data structures */
877 
878  /* Generate the Control Flow Graph */
879  lsra_make_cfg(jd);
880  /* gather nesting before adding of Exceptions and Subroutines!!! */
881 
882 #ifdef USAGE_COUNT
883  lsra_DFS(jd);
884  lsra_get_nesting(jd);
885 #endif
886 
887 #ifdef LSRA_DEBUG_VERBOSE
888  if (compileverbose) {
889  printf("Successors:\n");
890  for (i=0; i < m->basicblockcount; i++) {
891  printf("%3i->: ",i);
892  for (nl=ls->succ[i]; nl!= NULL; nl=nl->next)
893  printf("%3i ",nl->value);
894  printf("\n");
895  }
896  printf("Predecessors:\n");
897  for (i=0; i < m->basicblockcount; i++) {
898  printf("%3i->: ",i);
899  for (nl=ls->pred[i]; nl!= NULL; nl=nl->next)
900  printf("%3i ",nl->value);
901  printf("\n");
902  }
903  printf("Sorted: ");
904  for (i=0; i < m->basicblockcount; i++) printf("%3i ", ls->sorted[i]);
905  printf("\n");
906  printf("Sorted_rev: ");
907  for (i=0; i < m->basicblockcount; i++) printf("%3i ", ls->sorted_rev[i]);
908  printf("\n");
909  }
910 #endif
911 
912  /* add subroutines before exceptions! They "destroy" the CFG */
913  lsra_add_subs(jd);
915 
916  /* generate reverse post order sort */
917  lsra_DFS(jd);
918 
919  /* setup backedge and nested data structures*/
920  lsra_get_backedges(jd);
921 
922  liveness_init(jd);
923 
924  ls->lifetimecount = ls->maxlifetimes + jd->maxlocals * (TYPE_ADR+1);
925  ls->lifetime = DMNEW(struct lifetime, ls->lifetimecount);
926  ls->lt_used = DMNEW(int, ls->lifetimecount);
927  ls->lt_int = DMNEW(int, ls->lifetimecount);
928  ls->lt_int_count = 0;
929  ls->lt_flt = DMNEW(int, ls->lifetimecount);
930  ls->lt_flt_count = 0;
931  ls->lt_mem = DMNEW(int, ls->lifetimecount);
932  ls->lt_mem_count = 0;
933 
934  for (i=0; i < ls->lifetimecount; i++) ls->lifetime[i].type = -1;
935 
936 #ifdef LSRA_DEBUG_VERBOSE
937  if (compileverbose) {
938  printf("Successors:\n");
939  for (i=0; i < m->basicblockcount; i++) {
940  printf("%3i->: ",i);
941  for (nl=ls->succ[i]; nl!= NULL; nl=nl->next)
942  printf("%3i ",nl->value);
943  printf("\n");
944  }
945  printf("Predecessors:\n");
946  for (i=0; i < m->basicblockcount; i++) {
947  printf("%3i->: ",i);
948  for (nl=ls->pred[i]; nl!= NULL; nl=nl->next)
949  printf("%3i ",nl->value);
950  printf("\n");
951  }
952  printf("Sorted: ");
953  for (i=0; i < m->basicblockcount; i++) printf("%3i ", ls->sorted[i]);
954  printf("\n");
955  printf("Sorted_rev: ");
956  for (i=0; i < m->basicblockcount; i++) printf("%3i ", ls->sorted_rev[i]);
957  printf("\n");
958  }
959 #endif
960 
961 
962 #ifdef LSRA_DEBUG_CHECK
963  /* compare m->basicblocks[] with the list basicblocks->next */
964  i=0;
965  bptr = m->basicblocks;
966  while (bptr != NULL) {
967  if (i > m->basicblockcount){
968  { log_text("linked bb list does not correspond with bb array(1)\n");
969  assert(0); }
970  }
971  if (bptr != &(m->basicblocks[i])){
972  { log_text("linked bb list does not correspond with bb array(2)\n");
973  assert(0); }
974  }
975 
976  i++;
977  bptr=bptr->next;
978  }
979  if (i<m->basicblockcount){
980  { log_text("linked bb list does not correspond with bb array(3)\n");
981  assert(0); }
982  }
983 
984 #endif
985 
986  liveness_setup(jd);
987 
988 #if defined(LV)
989  liveness(jd);
990 #else /* LV */
991  {
992  int p, t;
993  methoddesc *md = m->parseddesc;
994 
995  /* Create Stack Slot lifetimes over all basic blocks */
996  for (i=m->basicblockcount-1; i >= 0; i--) {
997  if (ls->sorted[i] != -1) {
999  lsra_join_lifetimes(jd, ls->sorted[i]);
1000  }
1001  }
1002 
1003  /* Parameter initialisiation for locals [0 .. paramcount[ */
1004  /* -> add local var write access at (bb=0,iindex=-1) */
1005  /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
1006  /* this needs a special treatment, wenn lifetimes get extended */
1007  /* over backedges, since this parameter initialisation happens */
1008  /* outside of Basic Block 0 !!!! */
1009  /* this could have been avoided by marking the read access with -1,0 */
1010 
1011  for (p = 0, i = 0; p < md->paramcount; p++) {
1012  t = md->paramtypes[p].type;
1013 
1014  if (rd->locals[i][t].type >= 0)
1015  /* Param to Local init happens before normal Code */
1016  lsra_usage_local(ls, i, t, 0, -1, LSRA_STORE);
1017  i++;
1018  /* increment local counter a second time */
1019  /* for 2 word types */
1020  if (IS_2_WORD_TYPE(t))
1021  i++;
1022  } /* end for */
1023  }
1024 #endif /* LV */
1025 
1027 
1028 #ifdef LSRA_DEBUG_VERBOSE
1029  if (compileverbose)
1030  printf("Basicblockcount: %4i\n",m->basicblockcount);
1031 #endif
1032 }
1033 
1034 
1035 void lsra_reg_setup(jitdata *jd, struct lsra_register *int_reg,
1036  struct lsra_register *flt_reg ) {
1037  int i, j, iarg, farg;
1038  int int_sav_top;
1039  int flt_sav_top;
1040  bool *fltarg_used, *intarg_used;
1041  methoddesc *md;
1042  methodinfo *m;
1043  registerdata *rd;
1044 
1045  m = jd->m;
1046  rd = jd->rd;
1047  md = m->parseddesc;
1048 
1049  int_reg->nregdesc = nregdescint;
1050  flt_reg->nregdesc = nregdescfloat;
1051  if (code_is_leafmethod(code)) {
1052  /* Temp and Argumentregister can be used as saved registers */
1053 
1054  int_reg->sav_top = INT_ARG_CNT + INT_TMP_CNT + INT_SAV_CNT;
1055  int_reg->sav_reg = DMNEW(int, int_reg->sav_top);
1056  int_reg->tmp_reg = NULL;
1057  int_reg->tmp_top = -1;
1058  flt_reg->sav_top = FLT_ARG_CNT + FLT_TMP_CNT + FLT_SAV_CNT;
1059  flt_reg->sav_reg = DMNEW(int, flt_reg->sav_top);
1060  flt_reg->tmp_reg = NULL;
1061  flt_reg->tmp_top = -1;
1062 
1063  /* additionaly precolour registers for Local Variables acting as */
1064  /* Parameters */
1065 
1066  farg = FLT_ARG_CNT;
1067  iarg = INT_ARG_CNT;
1068 
1069  intarg_used = DMNEW(bool, INT_ARG_CNT);
1070  for (i=0; i < INT_ARG_CNT; i++)
1071  intarg_used[i]=false;
1072 
1073  fltarg_used = DMNEW(bool, FLT_ARG_CNT);
1074  for (i=0; i < FLT_ARG_CNT; i++)
1075  fltarg_used[i]=false;
1076 
1077  int_sav_top=int_reg->sav_top;
1078  flt_sav_top=flt_reg->sav_top;
1079 
1080  for (i=0; (i < md->paramcount); i++) {
1081  if (!md->params[i].inmemory) {
1082  if (IS_INT_LNG_TYPE(md->paramtypes[i].type)) {
1083 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1084  if (IS_2_WORD_TYPE(md->paramtypes[i].type)) {
1085  int_reg->sav_reg[--int_sav_top] =
1086  rd->argintregs[GET_HIGH_REG(md->params[i].regoff)];
1087  intarg_used[GET_HIGH_REG(md->params[i].regoff)]=true;
1088  /*used -> don't copy later on */
1089  int_reg->sav_reg[--int_sav_top] =
1090  rd->argintregs[GET_LOW_REG(md->params[i].regoff)];
1091  intarg_used[GET_LOW_REG(md->params[i].regoff)]=true;
1092  /*used -> don't copy later on */
1093  } else
1094 #endif
1095  { /* !IS_2_WORD_TYPE(md->paramtypes[i].type */
1096  int_reg->sav_reg[--int_sav_top] =
1097  rd->argintregs[md->params[i].regoff];
1098  intarg_used[md->params[i].regoff]=true;
1099  /*used -> don't copy later on */
1100  }
1101  }
1102 #if !defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
1103  /* do not precolour float arguments if they are passed in */
1104  /* integer registers. But these integer argument registers */
1105  /* still be used in the method! */
1106  else { /* IS_FLT_DBL_TYPE(md->paramtypes[i].type */
1107  flt_reg->sav_reg[--flt_sav_top] =
1108  rd->argfltregs[md->params[i].regoff];
1109  fltarg_used[md->params[i].regoff]=true;
1110  }
1111 #endif
1112 
1113  }
1114  }
1115 
1116  /* copy rest of argument registers to flt_reg->sav_reg and */
1117  /* int_reg->sav_reg; */
1118  for (i=0; i < INT_ARG_CNT; i++)
1119  if (!intarg_used[i])
1120  int_reg->sav_reg[--int_sav_top]=rd->argintregs[i];
1121  for (i=0; i < FLT_ARG_CNT; i++)
1122  if (!fltarg_used[i])
1123  flt_reg->sav_reg[--flt_sav_top]=rd->argfltregs[i];
1124 
1125  /* copy temp registers to flt_reg->sav_reg and int_reg->sav_reg */
1126  for (i=0; i < INT_TMP_CNT; i++)
1127  int_reg->sav_reg[--int_sav_top]=rd->tmpintregs[i];
1128  for (i=0; i < FLT_TMP_CNT; i++)
1129  flt_reg->sav_reg[--flt_sav_top]=rd->tmpfltregs[i];
1130 
1131  } else {
1132  /* non leaf method -> use Argument Registers [arg[int|flt]reguse */
1133  /* ... [INT|FLT]_ARG_CNT[ as temp reg */
1134  /* divide temp and saved registers */
1135  int argintreguse, argfltreguse;
1136 #ifdef LV
1137  /* with Locals as non SAVEDVAR, the used arg[int|flt] as in params */
1138  /* of the method itself have to be regarded, or mismatch before */
1139  /* block 0 with parameter copy could happen! */
1140  argintreguse = max(rd->argintreguse, md->argintreguse);
1141  argfltreguse = max(rd->argfltreguse, md->argfltreguse);
1142 #else
1143  argintreguse = rd->argintreguse;
1144  argfltreguse = rd->argfltreguse;
1145 #endif
1146  int_sav_top = int_reg->sav_top = INT_SAV_CNT;
1147  int_reg->sav_reg = DMNEW(int, int_reg->sav_top);
1148  int_reg->tmp_top = INT_TMP_CNT +
1149  max(0, (INT_ARG_CNT - argintreguse));
1150  int_reg->tmp_reg = DMNEW(int, int_reg->tmp_top);
1151 
1152  flt_sav_top =flt_reg->sav_top = FLT_SAV_CNT;
1153  flt_reg->sav_reg = DMNEW(int, flt_reg->sav_top);
1154  flt_reg->tmp_top = FLT_TMP_CNT +
1155  max(0 , (FLT_ARG_CNT - argfltreguse));
1156  flt_reg->tmp_reg = DMNEW(int, flt_reg->tmp_top);
1157 
1158  /* copy temp and unused argument registers to flt_reg->tmp_reg and */
1159  /* int_reg->tmp_reg */
1160  for (i=0; i < INT_TMP_CNT; i++)
1161  int_reg->tmp_reg[i]=rd->tmpintregs[i];
1162  for (j=argintreguse; j < INT_ARG_CNT; j++, i++)
1163  int_reg->tmp_reg[i]=rd->argintregs[j];
1164  for (i=0; i < FLT_TMP_CNT; i++)
1165  flt_reg->tmp_reg[i]=rd->tmpfltregs[i];
1166  for (j=argfltreguse; j < FLT_ARG_CNT; j++, i++)
1167  flt_reg->tmp_reg[i]=rd->argfltregs[j];
1168  }
1169 
1170  /* now copy saved registers to flt_reg->sav_reg and int_reg->sav_reg */
1171  for (i = INT_SAV_CNT-1; i >= 0; i--)
1172  int_reg->sav_reg[--int_sav_top]=rd->savintregs[i];
1173  for (i = FLT_SAV_CNT-1; i >= 0; i--)
1174  flt_reg->sav_reg[--flt_sav_top]=rd->savfltregs[i];
1175  /* done */
1176 }
1177 
1178 void lsra_insertion_sort( struct lsradata *ls, int *a, int lo, int hi) {
1179  int i,j,t,tmp;
1180 
1181  for (i=lo+1; i<=hi; i++) {
1182  j=i;
1183  t=ls->lifetime[a[j]].i_start;
1184  tmp = a[j];
1185  while ((j>lo) && (ls->lifetime[a[j-1]].i_start > t)) {
1186  a[j]=a[j-1];
1187  j--;
1188  }
1189  a[j]=tmp;
1190  }
1191 }
1192 
1193 void lsra_qsort( struct lsradata *ls, int *a, int lo, int hi) {
1194  int i,j,x,tmp;
1195  if (lo < hi) {
1196  if ( (lo+5)<hi) {
1197  i = lo;
1198  j = hi;
1199  x = ls->lifetime[a[(lo+hi)/2]].i_start;
1200 
1201  while (i <= j) {
1202  while (ls->lifetime[a[i]].i_start < x) i++;
1203  while (ls->lifetime[a[j]].i_start > x) j--;
1204  if (i <= j) {
1205  /* exchange a[i], a[j] */
1206  tmp = a[i];
1207  a[i] = a[j];
1208  a[j] = tmp;
1209 
1210  i++;
1211  j--;
1212  }
1213  }
1214 
1215  if (lo < j) lsra_qsort( ls, a, lo, j);
1216  if (i < hi) lsra_qsort( ls, a, i, hi);
1217  } else
1218  lsra_insertion_sort(ls, a, lo, hi);
1219  }
1220 }
1221 
1222 void lsra_param_sort(struct lsradata *ls, int *lifetime, int lifetime_count) {
1223 
1224  int param_count;
1225  int i,j,tmp;
1226 
1227  /* count number of parameters ( .i_start == -1) */
1228  for (param_count=0; (param_count < lifetime_count) &&
1229  (ls->lifetime[lifetime[param_count]].i_start == -1); param_count++);
1230 
1231  if (param_count > 0) {
1232  /* now sort the parameters by v_index */
1233  for (i=0; i < param_count -1; i++)
1234  for (j=i+1; j < param_count; j++)
1235  if ( ls->lifetime[lifetime[i]].v_index >
1236  ls->lifetime[lifetime[j]].v_index) {
1237  /* swap */
1238  tmp = lifetime[i];
1239  lifetime[i]=lifetime[j];
1240  lifetime[j]=tmp;
1241  }
1242  }
1243 }
1244 
1245 void lsra_main(jitdata *jd) {
1246 #ifdef LSRA_DEBUG_VERBOSE
1247  int i;
1248 #endif
1249  int lsra_mem_use;
1250  int lsra_reg_use;
1251  struct lsra_register flt_reg, int_reg;
1252  lsradata *ls;
1253  registerdata *rd;
1254 #if defined(__I386__)
1255  methodinfo *m;
1256 
1257  m = jd->m;
1258 #endif
1259  ls = jd->ls;
1260  rd = jd->rd;
1261 
1262 /* sort lifetimes by increasing start */
1263  lsra_qsort( ls, ls->lt_mem, 0, ls->lt_mem_count - 1);
1264  lsra_qsort( ls, ls->lt_int, 0, ls->lt_int_count - 1);
1265  lsra_qsort( ls, ls->lt_flt, 0, ls->lt_flt_count - 1);
1266 /* sort local vars used as parameter */
1267  lsra_param_sort( ls, ls->lt_int, ls->lt_int_count);
1268  lsra_param_sort( ls, ls->lt_flt, ls->lt_flt_count);
1269  lsra_reg_setup(jd, &int_reg, &flt_reg);
1270 
1271 #ifdef LSRA_DEBUG_VERBOSE
1272  if (compileverbose) {
1273  printf("INTSAV REG: ");
1274  for (i=0; i<int_reg.sav_top; i++)
1275  printf("%2i ",int_reg.sav_reg[i]);
1276  printf("\nINTTMP REG: ");
1277  for (i=0; i<int_reg.tmp_top; i++)
1278  printf("%2i ",int_reg.tmp_reg[i]);
1279  printf("\nFLTSAV REG: ");
1280  for (i=0; i<flt_reg.sav_top; i++)
1281  printf("%2i ",flt_reg.sav_reg[i]);
1282  printf("\nFLTTMP REG: ");
1283  for (i=0; i<flt_reg.tmp_top; i++)
1284  printf("%2i ",flt_reg.tmp_reg[i]);
1285  printf("\n");
1286  }
1287 #endif
1288  ls->active_tmp = DMNEW( struct lifetime *, max(INT_REG_CNT, FLT_REG_CNT));
1289  ls->active_sav = DMNEW( struct lifetime *, max(INT_REG_CNT, FLT_REG_CNT));
1290 
1291  lsra_reg_use=INT_SAV_CNT; /* init to no saved reg used... */
1292  _lsra_main(jd, ls->lt_int, ls->lt_int_count, &int_reg, &lsra_reg_use);
1293  if (lsra_reg_use > INT_SAV_CNT)
1294  lsra_reg_use=INT_SAV_CNT;
1295  rd->savintreguse = lsra_reg_use;
1296 
1297  lsra_reg_use = FLT_SAV_CNT; /* no saved reg used... */
1298  _lsra_main(jd, ls->lt_flt, ls->lt_flt_count, &flt_reg, &lsra_reg_use);
1299  if (lsra_reg_use > FLT_SAV_CNT)
1300  lsra_reg_use=FLT_SAV_CNT;
1301  rd->savfltreguse=lsra_reg_use;
1302 
1303  /* rd->memuse was already set in stack.c to allocate stack space for */
1304  /* passing arguments to called methods */
1305 #if defined(__I386__)
1306  if (checksync && code_is_synchronized(code)) {
1307  /* reserve 0(%esp) for Monitorenter/exit Argument on i386 */
1308  if (rd->memuse < 1)
1309  rd->memuse = 1;
1310  }
1311 #endif
1312 
1313  lsra_mem_use = rd->memuse; /* Init with memuse from stack.c */
1314 
1315  lsra_alloc(jd, ls->lt_mem, ls->lt_mem_count, &lsra_mem_use);
1316  lsra_alloc(jd, ls->lt_int, ls->lt_int_count, &lsra_mem_use);
1317  lsra_alloc(jd, ls->lt_flt, ls->lt_flt_count, &lsra_mem_use);
1318 
1319  rd->memuse=lsra_mem_use;
1320 
1321 #ifdef LSRA_DEBUG_VERBOSE
1322  if (compileverbose) {
1323  printf("Int RA complete \n");
1324  printf("Lifetimes after splitting int: \n");
1325  print_lifetimes(jd, ls->lt_int, ls->lt_int_count);
1326 
1327  printf("Flt RA complete \n");
1328  printf("Lifetimes after splitting flt:\n");
1329  print_lifetimes(jd, ls->lt_flt, ls->lt_flt_count);
1330 
1331  printf("Rest RA complete \n");
1332  printf("Lifetimes after leftt:\n");
1333  print_lifetimes(jd, ls->lt_mem, ls->lt_mem_count);
1334  }
1335 #endif
1336 }
1337 
1338 void lsra_alloc(jitdata *jd, int *lifet, int lifetimecount, int *mem_use)
1339 {
1340  int flags,regoff;
1341  struct lifetime *lt;
1342  struct freemem *fmem;
1343  struct stackslot *n;
1344  int lt_index;
1345  registerdata *rd;
1346  lsradata *ls;
1347 
1348  rd = jd->rd;
1349  ls = jd->ls;
1350 
1351  fmem = DNEW(struct freemem);
1352  fmem->off = -1;
1353  fmem->next = NULL;
1354 
1355  for (lt_index = 0; lt_index < lifetimecount; lt_index ++) {
1356  lt = &(ls->lifetime[lifet[lt_index]]);
1357 #ifdef LSRA_MEMORY
1358  lt->reg = -1;
1359 #endif
1360  if (lt->reg == -1) {
1361  flags = INMEMORY;
1362  regoff = lsra_getmem(lt, fmem, mem_use);
1363  } else {
1364  flags = lt->savedvar;
1365  regoff = lt->reg;
1366  }
1367 
1368  if (lt->v_index < 0) {
1369  for (n = lt->local_ss; n != NULL; n = n->next) {
1370  lsra_setflags(&(n->s->flags), flags);
1371  n->s->regoff = regoff;
1372  }
1373  } else { /* local var */
1374  if (rd->locals[lt->v_index][lt->type].type >= 0) {
1375  rd->locals[lt->v_index][lt->type].flags = flags;
1376  rd->locals[lt->v_index][lt->type].regoff = regoff;
1377  } else {
1378  log_text("Type Data mismatch\n");
1379  abort();
1380  }
1381  }
1382  lt->reg = regoff;
1383  }
1384 }
1385 
1386 void lsra_setflags(int *flags, int newflags)
1387 {
1388  if ( newflags & INMEMORY)
1389  *flags |= INMEMORY;
1390  else
1391  *flags &= ~INMEMORY;
1392 
1393  if (newflags & SAVEDVAR)
1394  *flags |= SAVEDVAR;
1395 }
1396 
1397 int lsra_getmem(struct lifetime *lt, struct freemem *fmem, int *mem_use)
1398 {
1399  struct freemem *fm, *p;
1400 
1401  /* no Memory Slot allocated till now or all are still live */
1402  if ((fmem->next == NULL) || (fmem->next->end > lt->i_start)) {
1403  fm=lsra_getnewmem(mem_use);
1404  } else {
1405  /* Memoryslot free */
1406  fm = fmem->next;
1407  fmem->next = fm->next;
1408  fm->next = NULL;
1409  }
1410  fm->end = lt->i_end;
1411  for (p = fmem; (p->next != NULL) && (p->next->end < fm->end); p = p->next);
1412  fm->next = p->next;
1413  p->next = fm;
1414  return fm->off;
1415 }
1416 
1417 struct freemem *lsra_getnewmem(int *mem_use)
1418 {
1419  struct freemem *fm;
1420 
1421  fm = DNEW(struct freemem);
1422  fm->next = NULL;
1423  fm->off = *mem_use;
1424  (*mem_use)++;
1425  return fm;
1426 }
1427 
1428 void _lsra_main(jitdata *jd, int *lifet, int lifetimecount,
1429  struct lsra_register *reg, int *reg_use)
1430 {
1431  struct lifetime *lt;
1432  int lt_index;
1433  int reg_index;
1434  int regsneeded;
1435  bool temp; /* reg from temp registers (true) or saved registers (false) */
1436  methodinfo *m;
1437  lsradata *ls;
1438 
1439  m = jd->m;
1440  ls = jd->ls;
1441 
1442 #if !defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1443  regsneeded = 0;
1444 #endif
1445  if ((reg->tmp_top+reg->sav_top) == 0) {
1446 
1447  /* no registers available */
1448  for (lt_index = 0; lt_index < lifetimecount; lt_index++)
1449  ls->lifetime[lifet[lt_index]].reg = -1;
1450  return;
1451  }
1452 
1453  ls->active_tmp_top = 0;
1454  ls->active_sav_top = 0;
1455 
1456  for (lt_index = 0; lt_index < lifetimecount; lt_index++) {
1457  lt = &(ls->lifetime[lifet[lt_index]]);
1458 
1459 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1460  regsneeded = (lt->type == TYPE_LNG)?1:0;
1461 #endif
1462 
1463  lsra_expire_old_intervalls(jd, lt, reg);
1464  reg_index = -1;
1465  temp = false;
1466  if (lt->savedvar || code_is_leafmethod(code)) {
1467  /* use Saved Reg (in case of leafmethod all regs are saved regs) */
1468  if (reg->sav_top > regsneeded) {
1469 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1470  if (regsneeded)
1471  reg_index = PACK_REGS(reg->sav_reg[--reg->sav_top],
1472  reg->sav_reg[--reg->sav_top]);
1473  else
1474 #endif
1475 
1476  reg_index = reg->sav_reg[--reg->sav_top];
1477  }
1478  } else { /* use Temp Reg or if none is free a Saved Reg */
1479  if (reg->tmp_top > regsneeded) {
1480  temp = true;
1481 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1482  if (regsneeded)
1483  reg_index = PACK_REGS(reg->tmp_reg[--reg->tmp_top],
1484  reg->tmp_reg[--reg->tmp_top]);
1485  else
1486 #endif
1487  reg_index = reg->tmp_reg[--reg->tmp_top];
1488  }
1489  else
1490  if (reg->sav_top > regsneeded) {
1491 
1492 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1493  if (regsneeded)
1494  reg_index = PACK_REGS(reg->sav_reg[--reg->sav_top],
1495  reg->sav_reg[--reg->sav_top]);
1496  else
1497 #endif
1498  reg_index = reg->sav_reg[--reg->sav_top];
1499  }
1500  }
1501  if (reg_index == -1) /* no reg is available anymore... -> spill */
1502  spill_at_intervall(jd, lt);
1503  else {
1504  lt->reg = reg_index;
1505  if (temp)
1506  lsra_add_active(lt, ls->active_tmp, &(ls->active_tmp_top));
1507  else {
1508  if (reg->sav_top<*reg_use) *reg_use=reg->sav_top;
1509  lsra_add_active(lt, ls->active_sav, &(ls->active_sav_top));
1510  }
1511  }
1512  }
1513 }
1514 
1515 void lsra_add_active(struct lifetime *lt, struct lifetime **active,
1516  int *active_top)
1517 {
1518  int i, j;
1519 
1520  for(i = 0; (i < *active_top) && (active[i]->i_end < lt->i_end); i++);
1521  for(j = *active_top; j > i; j--) active[j] = active[j-1];
1522  (*active_top)++;
1523  active[i] = lt;
1524 }
1525 
1527  struct lsra_register *reg)
1528 {
1529  _lsra_expire_old_intervalls(jd, lt, reg, jd->ls->active_tmp,
1530  &(jd->ls->active_tmp_top));
1531  _lsra_expire_old_intervalls(jd, lt, reg, jd->ls->active_sav,
1532  &(jd->ls->active_sav_top));
1533 }
1534 
1536  struct lsra_register *reg,
1537  struct lifetime **active, int *active_top)
1538 {
1539  int i, j, k;
1540 
1541  for(i = 0; i < *active_top; i++) {
1542  if (active[i]->i_end > lt->i_start) break;
1543 
1544  /* make active[i]->reg available again */
1545  if (code_is_leafmethod(code)) {
1546  /* leafmethod -> don't care about type -> put all again into */
1547  /* reg->sav_reg */
1548 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1549  if (active[i]->type == TYPE_LNG) {
1550  reg->sav_reg[reg->sav_top++] = GET_LOW_REG(active[i]->reg);
1551  reg->sav_reg[reg->sav_top++] = GET_HIGH_REG(active[i]->reg);
1552  } else
1553 #endif
1554  reg->sav_reg[reg->sav_top++] = active[i]->reg;
1555  } else {
1556  /* no leafmethod -> distinguish between temp and saved register */
1557 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1558  if (active[i]->type == TYPE_LNG) {
1559  /* no temp and saved regs are packed together, so looking at */
1560  /* LOW_REG is sufficient */
1561  if ( reg->nregdesc[ GET_LOW_REG(active[i]->reg)] == REG_SAV) {
1562  reg->sav_reg[reg->sav_top++] = GET_LOW_REG(active[i]->reg);
1563  reg->sav_reg[reg->sav_top++] = GET_HIGH_REG(active[i]->reg);
1564  } else {
1565  reg->tmp_reg[reg->tmp_top++] = GET_LOW_REG(active[i]->reg);
1566  reg->tmp_reg[reg->tmp_top++] = GET_HIGH_REG(active[i]->reg);
1567  }
1568  } else
1569 #endif
1570  if ( reg->nregdesc[active[i]->reg] == REG_SAV) {
1571  reg->sav_reg[reg->sav_top++] = active[i]->reg;
1572  } else {
1573  reg->tmp_reg[reg->tmp_top++] = active[i]->reg;
1574  }
1575  }
1576  }
1577 
1578  /* active[0..i[ is to be removed */
1579  /* -> move [i..*active_top[ to [0..*active_top-i[ */
1580  for(k = 0, j = i; (j < *active_top); k++,j++)
1581  active[k] = active[j];
1582 
1583  (*active_top) -= i;
1584 
1585 }
1586 
1587 void spill_at_intervall(jitdata *jd, struct lifetime *lt )
1588 {
1589  if (lt->savedvar || code_is_leafmethod(code)) {
1590  _spill_at_intervall(lt, jd->ls->active_sav, &(jd->ls->active_sav_top));
1591  } else {
1592  _spill_at_intervall(lt, jd->ls->active_tmp, &(jd->ls->active_tmp_top));
1593  if (lt->reg == -1) { /* no tmp free anymore */
1594  _spill_at_intervall(lt, jd->ls->active_sav,
1595  &(jd->ls->active_sav_top));
1596  }
1597  }
1598 }
1599 
1600 void _spill_at_intervall(struct lifetime *lt, struct lifetime **active,
1601  int *active_top)
1602 {
1603  int i, j;
1604 #ifdef USAGE_COUNT_EXACT
1605  int u_min, i_min;
1606 #endif
1607 
1608  if (*active_top == 0) {
1609  lt->reg=-1;
1610  return;
1611  }
1612 
1613  i = *active_top - 1;
1614 #if defined(USAGE_COUNT_EXACT)
1615  /* find intervall which ends later or equal than than lt and has the lowest
1616  usagecount lower than lt */
1617  i_min = -1;
1618  u_min = lt->usagecount;
1619  for (; (i >= 0) && (active[i]->i_end >= lt->i_end); i--) {
1620  if (active[i]->usagecount < u_min) {
1621  u_min = active[i]->usagecount;
1622  i_min = i;
1623  }
1624  }
1625 
1626  if (i_min != -1) {
1627  i = i_min;
1628 #else
1629 # if defined(USAGE_COUNT) && !defined(USAGE_COUNT_EXACT)
1630  if ((active[i]->i_end >= lt->i_end)
1631  && (active[i]->usagecount < lt->usagecount)) {
1632 # else /* "normal" LSRA heuristic */
1633  /* get last intervall from active */
1634  if (active[i]->i_end > lt->i_end) {
1635 # endif
1636 #endif
1637 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1638  /* Don't spill between one and two word int types */
1639  if ((active[i]->type == TYPE_LNG) != (lt->type == TYPE_LNG))
1640  return;
1641 #endif
1642  lt->reg = active[i]->reg;
1643  active[i]->reg=-1;
1644 
1645  (*active_top)--;
1646  for (j = i; j < *active_top; j++)
1647  active[j] = active[j + 1];
1648 
1649  lsra_add_active(lt, active, active_top);
1650  } else {
1651  lt->reg=-1;
1652  }
1653 }
1654 
1656  methodinfo *m;
1657  lsradata *ls;
1658  codegendata *cd;
1659  struct lifetime *lt;
1660 #if defined(LSRA_DEBUG_VERBOSE) || !defined(LV)
1661  int i;
1662 #endif
1663  int lt_index;
1664  int lifetimecount;
1665  int flags; /* 0 INMEMORY -> ls->lt_mem */
1666  /* 1 INTREG -> ls->lt_int */
1667  /* 2 FLTREG -> ls->lt_flt */
1668 
1669 
1670  m = jd->m;
1671  ls = jd->ls;
1672  cd = jd->cd;
1673 
1674 #ifdef LSRA_DEBUG_VERBOSE
1675  if (compileverbose) {
1676  printf("icount_block: ");
1677  for (i=0; i < m->basicblockcount; i++)
1678  printf("(%3i-%3i) ",i, ls->icount_block[i]);
1679  printf("\n");
1680  }
1681 #endif
1682 
1683  /* extend lifetime over backedges (for the lsra version without exact
1684  liveness analysis)
1685  now iterate through lifetimes and expand them */
1686 
1687  lifetimecount = 0;
1688  for(lt_index = 0 ;lt_index < ls->lifetimecount; lt_index++) {
1689  if ( ls->lifetime[lt_index].type != -1) { /* used lifetime */
1690  /* remember lt_index in lt_sorted */
1691  ls->lt_used[lifetimecount++] = lt_index;
1692  lt = &(ls->lifetime[lt_index]);
1693 #if defined(SUPPORT_COMBINE_INTEGER_REGISTERS)
1694  /* prevent conflicts between lifetimes of type long by increasing
1695  the lifetime by one instruction
1696  i.e.(ri/rj) ...
1697  (rk/rl) ICMD_LNEG
1698  with i==l and/or j==k
1699  to resolve this during codegeneration a temporary register
1700  would be needed */
1701  if (lt->type == TYPE_LNG)
1702  lt->i_last_use++;
1703 #endif
1704 
1705 /* distribute lifetimes to lt_int, lt_flt and lt_mem */
1706 
1707  lt->reg = -1;
1708 
1709  switch (lt->type) {
1710  case TYPE_LNG:
1711  flags = 1;
1712  break;
1713  case TYPE_INT:
1714  case TYPE_ADR:
1715  flags=1;
1716  break;
1717  case TYPE_DBL:
1718  case TYPE_FLT:
1719 #if defined(__I386__)
1720  /*
1721  * for i386 put all floats in memory
1722  */
1723  flags=0;
1724  break;
1725 #endif
1726  flags=2;
1727  break;
1728  default:
1729  { log_text("Unknown Type\n"); assert(0); }
1730  }
1731 
1732  switch (flags) {
1733  case 0: /* lt_used[lt_used_index] -> lt_rest */
1734  ls->lt_mem[ ls->lt_mem_count++ ] = lt_index;
1735  break;
1736  case 1: /* l->lifetimes -> lt_int */
1737  ls->lt_int[ ls->lt_int_count++ ] = lt_index;
1738  break;
1739  case 2: /* l->lifetimes -> lt_flt */
1740  ls->lt_flt[ ls->lt_flt_count++ ] = lt_index;
1741  break;
1742  }
1743 
1744 
1745  if (lt->i_first_def == INT_MAX) {
1746 #ifdef LSRA_DEBUG_VERBOSE
1747  printf("Warning: var not defined! vi: %i start: %i end: %i\n",
1748  lt->v_index, lt->i_start, lt->i_end);
1749 #endif
1750  lt->bb_first_def = 0;
1751  lt->i_first_def = 0;
1752  }
1753 
1754  lt->i_start = lt->i_first_def;
1755 
1756  if (lt->i_last_use == -2) {
1757 #ifdef LSRA_DEBUG_VERBOSE
1758  printf("Warning: Var not used! vi: %i start: %i end: %i\n",
1759  lt->v_index, lt->i_start, lt->i_end);
1760 #endif
1761  lt->bb_last_use = lt->bb_first_def;
1762  lt->i_last_use = lt->i_first_def;
1763  }
1764 
1765  lt->i_end = lt->i_last_use;
1766 
1767 #ifdef LSRA_DEBUG_VERBOSE
1768  if (lt->i_start > lt->i_end)
1769  printf("Warning: last use before first def! vi: %i start: %i end: %i\n", lt->v_index, lt->i_start, lt->i_end);
1770 #endif
1771 
1772 #if !defined(LV)
1773  if ((lt->bb_first_def != lt->bb_last_use) ||
1774  (lt->i_first_def == -1)) {
1775  /* Lifetime goes over more than one Basic Block -> */
1776  /* check for necessary extension over backedges */
1777  /* see lsra_get_backedges */
1778  /* Arguments are set "before" Block 0, so they have */
1779  /* a lifespan of more then one block, too */
1780 
1781  for (i=0; i < ls->backedge_count; i++) {
1782  if (!( (lt->bb_first_def > ls->backedge[i]->start) ||
1783  (lt->bb_last_use < ls->backedge[i]->end) )) {
1784  /* Live intervall intersects with a backedge */
1785  /* if (lt->bb_first_def <= ls->backedge[i]->start) */
1786  if (lt->bb_last_use <= ls->backedge[i]->start)
1787  lt->i_end =
1788  ls->icount_block[ls->backedge[i]->start] +
1789  m->basicblocks[ls->sorted[ls->backedge[i]->start]].icount;
1790  }
1791  }
1792  }
1793 #endif /* !defined(LV) */
1794 
1795 #ifdef USAGE_PER_INSTR
1796  lt->usagecount = lt->usagecount / ( lt->i_end - lt->i_start + 1);
1797 #endif
1798  }
1799  }
1800  ls->lifetimecount = lifetimecount;
1801 }
1802 
1803 #ifdef LSRA_DEBUG_VERBOSE
1804 void print_lifetimes(jitdata *jd, int *lt, int lifetimecount)
1805 {
1806  struct lifetime *n;
1807  int lt_index;
1808  int type,flags,regoff,varkind;
1809 
1810  lsradata *ls;
1811  registerdata *rd;
1812 
1813  ls = jd->ls;
1814  rd = jd->rd;
1815 
1816  for (lt_index = 0; lt_index < lifetimecount; lt_index++) {
1817  n = &(ls->lifetime[lt[lt_index]]);
1818  if (n->savedvar == SAVEDVAR)
1819  printf("S");
1820  else
1821  printf(" ");
1822  if (n->v_index < 0) { /* stackslot */
1823  type = n->local_ss->s->type;
1824  flags=n->local_ss->s->flags;
1825  regoff=n->local_ss->s->regoff;
1826  varkind=n->local_ss->s->varkind;
1827  } else { /* local var */
1828  if (rd->locals[n->v_index][n->type].type>=0) {
1829  type = rd->locals[n->v_index][n->type].type;
1830  flags=rd->locals[n->v_index][n->type].flags;
1831  regoff=rd->locals[n->v_index][n->type].regoff;
1832  varkind=-1;
1833  } else
1834  { log_text("Type Data mismatch 3\n"); assert(0); }
1835  }
1836 #if !defined(LV)
1837  printf("i_Start: %3i(%3i,%3i) i_stop: %3i(%3i,%3i) reg: %3i VI: %3i type: %3i flags: %3i varkind: %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,regoff,n->v_index,type,flags, varkind, n->usagecount, n->flags);
1838 #else
1839  printf("i_Start: %3i i_stop: %3i reg: %3i VI: %3i type: %3i flags: %3i varkind: %3i usage: %3li ltflags: %xi \n",n->i_start, n->i_end, regoff,n->v_index,type,flags, varkind, n->usagecount, n->flags);
1840 #endif
1841  }
1842  printf( "%3i Lifetimes printed \n",lt_index);
1843 }
1844 #endif
1845 
1846 
1847 
1848 /******************************************************************************
1849 Helpers for first LSRA Version without exact Liveness Analysis
1850  *****************************************************************************/
1851 
1852 #if !defined(LV)
1853 bool lsra_join_ss( struct lsradata *ls, struct stackelement *in,
1854  struct stackelement *out, int join_flag) {
1855  struct lifetime *lt, *lto;
1856  struct stackslot *ss, *ss_last;
1857 
1858 
1859  if (in->varnum != out->varnum) {
1860  lt = &(ls->lifetime[-in->varnum - 1]);
1861 
1862 #ifdef LSRA_DEBUG_CHECK
1863  if (join_flag == JOIN_BB)
1864  if (lt->type == -1) {
1865  log_text("lsra_join_ss: lifetime for instack not found\n");
1866  assert(0);
1867  }
1868 #endif
1869 
1870  if (out->varnum >= 0) { /* no lifetime for this slot till now */
1871  lsra_add_ss(lt, out);
1872  } else {
1873  lto = &(ls->lifetime[-out->varnum - 1]);
1874  if ((join_flag == JOIN_DUP) || (join_flag == JOIN_OP))
1875  if ( (lt->flags & JOIN_BB) || (lto->flags & JOIN_BB)) {
1876  return false;
1877  }
1878  if (join_flag == JOIN_DUP)
1879  if ( (lt->flags & JOIN_OP) || (lto->flags & JOIN_OP)) {
1880  return false;
1881  }
1882 #ifdef LSRA_DEBUG_CHECK
1883  if (lto->type == -1) {
1884  log_text("lsra_join_ss: lifetime for outstack not found\n");
1885  abort();
1886  }
1887 #endif
1888 #ifdef LSRA_DEBUG_CHECK
1889  if (lto->type != lt->type) {
1890  log_text("lsra_join_ss: in/out stack type mismatch\n");
1891  abort();
1892  }
1893 #endif
1894 
1895  lt->flags |= JOINING;
1896 
1897  /* take Lifetime lto out of ls->lifetimes */
1898  lto->type = -1;
1899 
1900  /* merge lto into lt of in */
1901 
1902  ss_last = ss = lto->local_ss;
1903  while (ss != NULL) {
1904  ss_last = ss;
1905  ss->s->varnum = lt->v_index;
1906  ss = ss->next;
1907  }
1908  if (ss_last != NULL) {
1909  ss_last->next = lt->local_ss;
1910  lt->local_ss = lto->local_ss;
1911  }
1912 
1913  lt->savedvar |= lto->savedvar;
1914  lt->flags |= lto->flags | join_flag;
1915  lt->usagecount += lto->usagecount;
1916 
1917  /*join of i_first_def and i_last_use */
1918  if (lto->i_first_def < lt->i_first_def) {
1919  lt->i_first_def = lto->i_first_def;
1920  }
1921  if (lto->i_last_use > lt->i_last_use) {
1922  lt->i_last_use = lto->i_last_use;
1923  }
1924  }
1925  }
1926  return true;
1927 }
1928 
1929 /* join instack of Basic Block b_index with outstack of predecessors */
1930 void lsra_join_lifetimes(jitdata *jd,int b_index) {
1931  methodinfo *m;
1932  lsradata *ls;
1933  struct stackelement *in, *i, *out;
1934  struct _list *pred;
1935 
1936  m = jd->m;
1937  ls = jd->ls;
1938 
1939  /* do not join instack of Exception Handler */
1940  if (m->basicblocks[b_index].type == basicblock::TYPE_EXH)
1941  return;
1942  in=m->basicblocks[b_index].instack;
1943  /* do not join first instack element of a subroutine header */
1944  if (m->basicblocks[b_index].type == basicblock::TYPE_SBR)
1945  in=in->prev;
1946 
1947  if (in != NULL) {
1948  for (pred = ls->pred[b_index]; pred != NULL; pred = pred->next) {
1949  out = m->basicblocks[pred->value].outstack;
1950  for (i=in; (i != NULL); i = i->prev, out=out->prev) {
1951  lsra_join_ss(ls, i, out, JOIN_BB);
1952  }
1953  }
1954  }
1955 }
1956 
1957 struct stackslot *lsra_make_ss(stackelement_t* s, int bb_index)
1958 {
1959  struct stackslot *ss;
1960 
1961  ss = DNEW(struct stackslot);
1962  ss->bb = bb_index;
1963  ss->s = s;
1964  return ss;
1965 }
1966 
1967 void lsra_add_ss(struct lifetime *lt, stackelement_t* s) {
1968  struct stackslot *ss;
1969 
1970  /* Stackslot not in list? */
1971  if (s->varnum != lt->v_index) {
1972  ss = DNEW(struct stackslot);
1973  ss->s = s;
1974  ss->s->varnum = lt->v_index;
1975  ss->next = lt->local_ss;
1976  lt->local_ss = ss;
1977  if (s != NULL)
1978  lt->savedvar |= s->flags & SAVEDVAR;
1979  if (s != NULL)
1980  lt->type = s->type;
1981  }
1982 }
1983 
1985  struct lifetime *n;
1986 
1987  if (s->varnum >= 0) { /* new stackslot lifetime */
1988 #ifdef LSRA_DEBUG_CHECK_VERBOSE
1989  if (-ls->v_index - 1 >= ls->maxlifetimes) {
1990  printf("%i %i\n", -ls->v_index - 1, ls->maxlifetimes);
1991  }
1992 #endif
1993  _LSRA_ASSERT(-ls->v_index - 1 < ls->maxlifetimes);
1994 
1995  n = &(ls->lifetime[-ls->v_index - 1]);
1996  n->type = s->type;
1997  n->v_index = ls->v_index--;
1998  n->usagecount = 0;
1999 
2000  n->bb_last_use = -1;
2001  n->bb_first_def = -1;
2002  n->i_last_use = -2; /* At -1 param init happens, so -2 is below all
2003  possible instruction indices */
2004  n->i_first_def = INT_MAX;
2005  n->local_ss = NULL;
2006  n->savedvar = 0;
2007  n->flags = 0;
2008  } else
2009  n = &(ls->lifetime[-s->varnum - 1]);
2010 
2011  lsra_add_ss( n, s);
2012  return n;
2013 }
2014 
2015 #define IS_TEMP_VAR(s) (((s)->varkind != ARGVAR) && ((s)->varkind != LOCALVAR))
2016 
2017 #define lsra_join_3_stack(ls, dst, src1, src2, join_type) \
2018  if ( IS_TEMP_VAR(dst) ) { \
2019  join_ret = false; \
2020  if ( IS_TEMP_VAR(src1) && ((src1)->type == (dst)->type)) { \
2021  join_ret = lsra_join_ss(ls, dst, src1, join_type); \
2022  } \
2023  if ((!join_ret) && IS_TEMP_VAR(src2) && ((src2)->type == (dst)->type)) { \
2024  lsra_join_ss(ls, dst, src2, join_type); \
2025  } \
2026  }
2027 
2028 #define lsra_join_2_stack(ls, dst, src, join_type) \
2029  if ( IS_TEMP_VAR(dst) ) { \
2030  if ( (IS_TEMP_VAR(src)) && ((src)->type == (dst)->type)) { \
2031  lsra_join_ss(ls, dst, src, join_type); \
2032  } \
2033  }
2034 
2035 #define lsra_join_dup(ls, s1, s2, s3) { \
2036  if (IS_TEMP_VAR(s1)) { \
2037  join_ret = false; \
2038  if (IS_TEMP_VAR(s2)) \
2039  join_ret = lsra_join_ss(ls, s1, s2, JOIN); \
2040  /* undangerous join!*/\
2041  if (IS_TEMP_VAR(s3)) { \
2042  if (join_ret) /* first join succesfull -> second of type */ \
2043  /* JOIN_DUP */ \
2044  lsra_join_ss(ls, s1, s3, JOIN_DUP); \
2045  else \
2046  lsra_join_ss(ls, s1, s3, JOIN); /* first join did not */ \
2047  /* happen -> second undangerous */ \
2048  } \
2049  } \
2050  if (IS_TEMP_VAR(s2) && IS_TEMP_VAR(s3)) \
2051  lsra_join_ss(ls, s2, s3, JOIN_DUP); \
2052  }
2053 
2054 #define lsra_new_stack(ls, s, block, instr) \
2055  if ((s)->varkind != ARGVAR) _lsra_new_stack(ls, s, block, instr, LSRA_STORE)
2056 void _lsra_new_stack(lsradata *ls, stackelement_t* s, int block, int instr, int store)
2057 {
2058  struct lifetime *n;
2059 
2060  if (s->varkind == LOCALVAR) {
2061  lsra_usage_local(ls, s->varnum, s->type, block, instr, LSRA_STORE);
2062  } else /* if (s->varkind != ARGVAR) */ {
2063 
2064  n=get_ss_lifetime(ls, s);
2065 
2066  if (store == LSRA_BB_IN)
2067  n->flags |= JOIN_BB;
2068  /* remember first def -> overwrite everytime */
2069  n->bb_first_def = ls->sorted_rev[block];
2070  n->i_first_def = ls->icount_block[ls->sorted_rev[block]] + instr;
2071 
2072  n->usagecount+=ls->nesting[ls->sorted_rev[block]];
2073  }
2074 }
2075 
2076 #define lsra_from_stack(ls, s, block, instr) \
2077  if ((s)->varkind != ARGVAR) _lsra_from_stack(ls, s, block, instr, LSRA_LOAD)
2078 #define lsra_pop_from_stack(ls, s, block, instr) \
2079  if ((s)->varkind != ARGVAR) _lsra_from_stack(ls, s, block, instr, LSRA_POP)
2080 void _lsra_from_stack(lsradata *ls, stackelement_t* s, int block, int instr, int store)
2081 {
2082  struct lifetime *n;
2083 
2084  if (s->varkind == LOCALVAR) {
2085  lsra_usage_local(ls, s->varnum, s->type, block, instr, LSRA_LOAD);
2086  } else /* if (s->varkind != ARGVAR) */ {
2087  if (s->varkind == STACKVAR )
2088  /* No STACKVARS possible with lsra! */
2089  s->varkind = TEMPVAR;
2090 
2091  n=get_ss_lifetime(ls, s);
2092 
2093  if (store == LSRA_BB_OUT)
2094  n->flags |= JOIN_BB;
2095  if (n->flags & JOINING)
2096  n->flags &= ~JOINING;
2097  n->usagecount+=ls->nesting[ls->sorted_rev[block]];
2098 
2099  /* remember last USE, so only write, if USE Field is undefined (==-1) */
2100  if (n->bb_last_use == -1) {
2101  n->bb_last_use = ls->sorted_rev[block];
2102  n->i_last_use = ls->icount_block[ls->sorted_rev[block]] + instr;
2103  }
2104  }
2105 }
2106 
2107 void lsra_usage_local(lsradata *ls, s4 v_index, int type, int block, int instr,
2108  int store)
2109 {
2110  struct lifetime *n;
2111 
2112  n = &(ls->lifetime[ ls->maxlifetimes + v_index * (TYPE_ADR+1) + type]);
2113 
2114  if (n->type == -1) { /* new local lifetime */
2115  n->local_ss=NULL;
2116  n->v_index=v_index;
2117  n->type=type;
2118  n->savedvar = SAVEDVAR;
2119  n->flags = 0;
2120  n->usagecount = 0;
2121 
2122  n->bb_last_use = -1;
2123  n->bb_first_def = -1;
2124  n->i_last_use = -2;
2125  n->i_first_def = INT_MAX;
2126  }
2127  n->usagecount+=ls->nesting[ls->sorted_rev[block]];
2128  /* add access at (block, instr) to instruction list */
2129  /* remember last USE, so only write, if USE Field is undefined (==-1) */
2130  /* count store as use, too -> defined and not used vars would overwrite */
2131  /* other vars */
2132  if (n->bb_last_use == -1) {
2133  n->bb_last_use = ls->sorted_rev[block];
2134  n->i_last_use = ls->icount_block[ls->sorted_rev[block]] + instr;
2135  }
2136  if (store == LSRA_STORE) {
2137  /* store == LSRA_STORE, remember first def -> overwrite everytime */
2138  n->bb_first_def = ls->sorted_rev[block];
2139  n->i_first_def = ls->icount_block[ls->sorted_rev[block]] + instr;
2140  }
2141 }
2142 
2143 #ifdef LSRA_DEBUG_VERBOSE
2145 {
2146  while (s!=NULL) {
2147  printf("%p(R%3i N%3i K%3i T%3i F%3i) ",(void *)s,s->regoff, s->varnum,
2148  s->varkind, s->type, s->flags);
2149  s=s->prev;
2150  }
2151  printf("\n");
2152 }
2153 #endif
2154 
2155 
2157 {
2158 /* methodinfo *lm; */
2159  builtintable_entry *bte;
2160  methoddesc *md;
2161  int i;
2162  int opcode;
2163  int iindex;
2164  stackelement_t* src;
2165  stackelement_t* dst;
2166  instruction *iptr;
2167  bool join_ret; /* for lsra_join* Macros */
2168  methodinfo *m;
2169  lsradata *ls;
2170 
2171  m = jd->m;
2172  ls = jd->ls;
2173 
2174  /* get instruction count for BB and remember the max instruction count */
2175  /* of all BBs */
2176  iindex = m->basicblocks[b_index].icount - 1;
2177 
2178  src = m->basicblocks[b_index].instack;
2179  if (m->basicblocks[b_index].type != basicblock::TYPE_STD) {
2180  lsra_new_stack(ls, src, b_index, 0);
2181  src = src->prev;
2182  }
2183  for (;src != NULL; src=src->prev) {
2184 /*******************************************************************************
2185 Check this - ? For every incoming Stack Slot a lifetime has to be created ?
2186 *******************************************************************************/
2187  _lsra_new_stack(ls, src, b_index, 0, LSRA_BB_IN);
2188  }
2189  src = m->basicblocks[b_index].outstack;
2190  for (;src != NULL; src=src->prev) {
2191  _lsra_from_stack(ls, src, b_index, iindex, LSRA_BB_OUT);
2192  }
2193 
2194  /* set iptr to last instruction of BB */
2195  iptr = m->basicblocks[b_index].iinstr + iindex;
2196 
2197  for (;iindex >= 0; iindex--, iptr--) {
2198 
2199  /* get source and destination Stack for the current instruction */
2200  /* destination stack is available as iptr->dst */
2201 
2202  dst = iptr->dst;
2203 
2204  /* source stack is either the destination stack of the previos */
2205  /* instruction, or the basicblock instack for the first instruction */
2206 
2207  if (iindex) /* != 0 is > 0 here, since iindex ist always >= 0 */
2208  src=(iptr-1)->dst;
2209  else
2210  src=m->basicblocks[b_index].instack;
2211  opcode = iptr->opc;
2212 
2213 
2214  switch (opcode) {
2215 
2216  /* pop 0 push 0 */
2217  case ICMD_RET:
2218  /* local read (return adress) */
2219  lsra_usage_local(ls, iptr->op1, TYPE_ADR, b_index, iindex,
2220  LSRA_LOAD);
2221  break;
2222  case ICMD_NOP:
2223 /* case ICMD_ELSE_ICONST: */
2224  case ICMD_CHECKNULL:
2225  case ICMD_JSR:
2226  case ICMD_RETURN:
2227  case ICMD_GOTO:
2228  case ICMD_PUTSTATICCONST:
2229  case ICMD_INLINE_START:
2230  case ICMD_INLINE_END:
2231  case ICMD_INLINE_GOTO:
2232  break;
2233 
2234  case ICMD_IINC:
2235  /* local = local+<const> */
2236  lsra_usage_local(ls, iptr->op1, TYPE_INT, b_index, iindex,
2237  LSRA_LOAD);
2238  lsra_usage_local(ls, iptr->op1, TYPE_INT, b_index, iindex,
2239  LSRA_STORE);
2240  break;
2241 
2242  /* pop 0 push 1 const: const->stack */
2243  case ICMD_ICONST:
2244  case ICMD_LCONST:
2245  case ICMD_FCONST:
2246  case ICMD_DCONST:
2247  case ICMD_ACONST:
2248  /* new stack slot */
2249  lsra_new_stack(ls, dst, b_index, iindex);
2250  break;
2251 
2252  /* pop 0 push 1 load: local->stack */
2253  case ICMD_ILOAD:
2254  case ICMD_LLOAD:
2255  case ICMD_FLOAD:
2256  case ICMD_DLOAD:
2257  case ICMD_ALOAD:
2258  if (dst->varkind != LOCALVAR) {
2259  /* local->value on stack */
2260  lsra_usage_local(ls, iptr->op1, opcode - ICMD_ILOAD, b_index,
2261  iindex, LSRA_LOAD);
2262  lsra_new_stack(ls, dst, b_index, iindex); /* value->stack */
2263  } else /* if (dst->varnum != iptr->op1) */ {
2264  /* local -> local */
2265  lsra_usage_local(ls, iptr->op1, opcode - ICMD_ILOAD, b_index,
2266  iindex,LSRA_LOAD); /* local->value */
2267  lsra_usage_local(ls, dst->varnum, opcode - ICMD_ILOAD, b_index,
2268  iindex, LSRA_STORE); /* local->value */
2269  }
2270 
2271  break;
2272 
2273  /* pop 2 push 1 */
2274  /* Stack(arrayref,index)->stack */
2275  case ICMD_IALOAD:
2276  case ICMD_LALOAD:
2277  case ICMD_FALOAD:
2278  case ICMD_DALOAD:
2279  case ICMD_AALOAD:
2280 
2281  case ICMD_BALOAD:
2282  case ICMD_CALOAD:
2283  case ICMD_SALOAD:
2284  /* stack->index */
2285  lsra_from_stack(ls, src, b_index, iindex);
2286  /* stack->arrayref */
2287  lsra_from_stack(ls, src->prev, b_index, iindex);
2288  /* arrayref[index]->stack */
2289  lsra_new_stack(ls, dst, b_index, iindex);
2290  break;
2291 
2292  /* pop 3 push 0 */
2293  /* stack(arrayref,index,value)->arrayref[index]=value */
2294  case ICMD_IASTORE:
2295  case ICMD_LASTORE:
2296  case ICMD_FASTORE:
2297  case ICMD_DASTORE:
2298  case ICMD_AASTORE:
2299 
2300  case ICMD_BASTORE:
2301  case ICMD_CASTORE:
2302  case ICMD_SASTORE:
2303 
2304  lsra_from_stack(ls, src,b_index, iindex); /* stack -> value */
2305  lsra_from_stack(ls, src->prev, b_index, iindex); /* stack -> index*/
2306  /* stack -> arrayref */
2307  lsra_from_stack(ls, src->prev->prev, b_index, iindex);
2308  break;
2309 
2310  /* pop 1 push 0 store: stack -> local */
2311  case ICMD_ISTORE:
2312  case ICMD_LSTORE:
2313  case ICMD_FSTORE:
2314  case ICMD_DSTORE:
2315  case ICMD_ASTORE:
2316  if (src->varkind != LOCALVAR) {
2317  lsra_from_stack(ls, src, b_index, iindex); /* stack -> value */
2318  lsra_usage_local(ls, iptr->op1, opcode-ICMD_ISTORE, b_index,
2319  iindex, LSRA_STORE); /* local->value */
2320  } else /* if (src->varnum != iptr->op1) */ {
2321  lsra_usage_local(ls, iptr->op1, opcode-ICMD_ISTORE, b_index,
2322  iindex, LSRA_STORE); /* local->value */
2323  lsra_usage_local(ls, src->varnum, opcode-ICMD_ISTORE, b_index,
2324  iindex, LSRA_LOAD); /* local->value */
2325  }
2326  break;
2327 
2328  /* pop 1 push 0 */
2329  case ICMD_POP: /* throw away a stackslot */
2330  /* TODO: check if used anyway (DUP...) and change codegen to */
2331  /* ignore this stackslot */
2332  lsra_pop_from_stack(ls, src, b_index, iindex);
2333  break;
2334 
2335  /* pop 1 push 0 */
2336  case ICMD_IRETURN:
2337  case ICMD_LRETURN:
2338  case ICMD_FRETURN:
2339  case ICMD_DRETURN:
2340  case ICMD_ARETURN: /* stack(value) -> [empty] */
2341 
2342  case ICMD_ATHROW: /* stack(objref) -> undefined */
2343 
2344  case ICMD_PUTSTATIC: /* stack(value) -> static_field */
2345  case ICMD_PUTFIELDCONST:
2346 
2347  /* pop 1 push 0 branch */
2348  case ICMD_IFNULL: /* stack(value) -> branch? */
2349  case ICMD_IFNONNULL:
2350 
2351  case ICMD_IFEQ:
2352  case ICMD_IFNE:
2353  case ICMD_IFLT:
2354  case ICMD_IFGE:
2355  case ICMD_IFGT:
2356  case ICMD_IFLE:
2357 
2358  case ICMD_IF_LEQ:
2359  case ICMD_IF_LNE:
2360  case ICMD_IF_LLT:
2361  case ICMD_IF_LGE:
2362  case ICMD_IF_LGT:
2363  case ICMD_IF_LLE:
2364 
2365  /* pop 1 push 0 table branch */
2366  case ICMD_TABLESWITCH:
2367  case ICMD_LOOKUPSWITCH:
2368 
2369  case ICMD_MONITORENTER:
2370  case ICMD_MONITOREXIT:
2371  lsra_from_stack(ls, src, b_index, iindex); /* stack -> value */
2372  break;
2373 
2374  /* pop 2 push 0 */
2375  case ICMD_POP2: /* throw away 2 stackslots */
2376  /* TODO: check if used anyway (DUP...) and change codegen to */
2377  /* ignore this stackslot */
2378  lsra_pop_from_stack(ls, src, b_index, iindex);
2379  lsra_pop_from_stack(ls, src->prev, b_index, iindex);
2380  break;
2381 
2382  /* pop 2 push 0 branch */
2383 
2384  case ICMD_IF_ICMPEQ: /* stack (v1,v2) -> branch(v1,v2) */
2385  case ICMD_IF_ICMPNE:
2386  case ICMD_IF_ICMPLT:
2387  case ICMD_IF_ICMPGE:
2388  case ICMD_IF_ICMPGT:
2389  case ICMD_IF_ICMPLE:
2390 
2391  case ICMD_IF_LCMPEQ:
2392  case ICMD_IF_LCMPNE:
2393  case ICMD_IF_LCMPLT:
2394  case ICMD_IF_LCMPGE:
2395  case ICMD_IF_LCMPGT:
2396  case ICMD_IF_LCMPLE:
2397 
2398  case ICMD_IF_ACMPEQ:
2399  case ICMD_IF_ACMPNE:
2400 
2401  /* pop 2 push 0 */
2402  case ICMD_PUTFIELD: /* stack(objref,value) -> objref = value */
2403 
2404  case ICMD_IASTORECONST:
2405  case ICMD_LASTORECONST:
2406  case ICMD_AASTORECONST:
2407  case ICMD_BASTORECONST:
2408  case ICMD_CASTORECONST:
2409  case ICMD_SASTORECONST:
2410  lsra_from_stack(ls, src, b_index, iindex); /* stack -> value*/
2411  lsra_from_stack(ls, src->prev, b_index, iindex);
2412  break;
2413 
2414  /* pop 0 push 1 dup */
2415  case ICMD_DUP: /* src == dst->prev, src -> dst */
2416  /* lsra_from_stack(ls, src,b_index,iindex);*/
2417  lsra_new_stack(ls, dst, b_index, iindex);
2418 
2419 #ifdef JOIN_DUP_STACK
2420  /* src is identical to dst->prev */
2421  lsra_join_2_stack(ls, src, dst, JOIN_DUP);
2422 #endif
2423  break;
2424 
2425  /* pop 0 push 2 dup */
2426  case ICMD_DUP2:
2427  /* lsra_from_stack(ls, src,b_index, iindex); */
2428  /* lsra_from_stack(ls, src->prev, b_index, iindex); */
2429  lsra_new_stack(ls, dst->prev, b_index, iindex);
2430  lsra_new_stack(ls, dst, b_index, iindex);
2431 
2432 #ifdef JOIN_DUP_STACK
2433  lsra_join_2_stack(ls, src, dst, JOIN_DUP);
2434  lsra_join_2_stack(ls, src->prev, dst->prev, JOIN_DUP);
2435  /* src is identical to dst->prev->prev */
2436  /* src->prev is identical to dst->prev->prev->prev */
2437 #endif
2438  break;
2439 
2440  /* pop 2 push 3 dup */
2441  case ICMD_DUP_X1:
2442  lsra_from_stack(ls, src, b_index, iindex+1);
2443  lsra_from_stack(ls, src->prev, b_index, iindex+1);
2444  lsra_new_stack(ls, dst->prev->prev, b_index, iindex);
2445  lsra_new_stack(ls, dst->prev, b_index, iindex);
2446  lsra_new_stack(ls, dst, b_index, iindex);
2447 #ifdef JOIN_DUP_STACK
2448  lsra_join_dup(ls, src, dst, dst->prev->prev);
2449  lsra_join_2_stack(ls, src->prev, dst->prev, JOIN);
2450 #endif
2451  break;
2452 
2453  /* pop 3 push 4 dup */
2454  case ICMD_DUP_X2:
2455  lsra_from_stack(ls, src,b_index, iindex+1);
2456  lsra_from_stack(ls, src->prev, b_index, iindex+1);
2457  lsra_from_stack(ls, src->prev->prev, b_index, iindex+1);
2458  lsra_new_stack(ls, dst->prev->prev->prev, b_index, iindex);
2459  lsra_new_stack(ls, dst->prev->prev, b_index, iindex);
2460  lsra_new_stack(ls, dst->prev, b_index, iindex);
2461  lsra_new_stack(ls, dst, b_index, iindex);
2462 
2463 #ifdef JOIN_DUP_STACK
2464  lsra_join_dup(ls, src, dst, dst->prev->prev->prev);
2465  lsra_join_2_stack(ls, src->prev, dst->prev, JOIN);
2466  lsra_join_2_stack(ls, src->prev->prev, dst->prev->prev, JOIN);
2467 #endif
2468  break;
2469 
2470  /* pop 3 push 5 dup */
2471  case ICMD_DUP2_X1:
2472  lsra_from_stack(ls, src, b_index, iindex+1);
2473  lsra_from_stack(ls, src->prev, b_index, iindex+1);
2474  lsra_from_stack(ls, src->prev->prev, b_index, iindex+1);
2475  lsra_new_stack(ls, dst->prev->prev->prev->prev, b_index, iindex);
2476  lsra_new_stack(ls, dst->prev->prev->prev, b_index, iindex);
2477  lsra_new_stack(ls, dst->prev->prev, b_index, iindex);
2478  lsra_new_stack(ls, dst->prev, b_index, iindex);
2479  lsra_new_stack(ls, dst, b_index, iindex);
2480 
2481 #ifdef JOIN_DUP_STACK
2482  lsra_join_dup(ls, src, dst, dst->prev->prev->prev);
2483  lsra_join_dup(ls, src->prev, dst->prev,
2484  dst->prev->prev->prev->prev);
2485  lsra_join_2_stack(ls, src->prev->prev, dst->prev->prev, JOIN);
2486 #endif
2487  break;
2488 
2489  /* pop 4 push 6 dup */
2490  case ICMD_DUP2_X2:
2491  lsra_from_stack(ls, src, b_index, iindex+1);
2492  lsra_from_stack(ls, src->prev, b_index, iindex+1);
2493  lsra_from_stack(ls, src->prev->prev, b_index, iindex+1);
2494  lsra_from_stack(ls, src->prev->prev->prev, b_index, iindex+1);
2495  lsra_new_stack(ls, dst->prev->prev->prev->prev->prev, b_index,
2496  iindex);
2497  lsra_new_stack(ls, dst->prev->prev->prev->prev, b_index, iindex);
2498  lsra_new_stack(ls, dst->prev->prev->prev, b_index, iindex);
2499  lsra_new_stack(ls, dst->prev->prev, b_index, iindex);
2500  lsra_new_stack(ls, dst->prev, b_index, iindex);
2501  lsra_new_stack(ls, dst, b_index, iindex);
2502 
2503 #ifdef JOIN_DUP_STACK
2504  lsra_join_dup(ls, src, dst, dst->prev->prev->prev->prev);
2505  lsra_join_dup(ls, src->prev, dst->prev,
2506  dst->prev->prev->prev->prev->prev);
2507  lsra_join_2_stack(ls, src->prev->prev, dst->prev->prev, JOIN);
2508  lsra_join_2_stack(ls, src->prev->prev->prev, dst->prev->prev->prev,
2509  JOIN);
2510 #endif
2511  break;
2512 
2513  /* pop 2 push 2 swap */
2514  case ICMD_SWAP:
2515  lsra_from_stack(ls, src, b_index, iindex+1);
2516  lsra_from_stack(ls, src->prev, b_index, iindex+1);
2517  lsra_new_stack(ls, dst->prev, b_index, iindex);
2518  lsra_new_stack(ls, dst, b_index, iindex);
2519 
2520  lsra_join_2_stack(ls, src->prev, dst, JOIN);
2521  lsra_join_2_stack(ls, src, dst->prev, JOIN);
2522 
2523  break;
2524 
2525  /* pop 2 push 1 */
2526 
2527  case ICMD_LADD:
2528  case ICMD_LSUB:
2529  case ICMD_LMUL:
2530 
2531  case ICMD_LOR:
2532  case ICMD_LAND:
2533  case ICMD_LXOR:
2534 
2535  case ICMD_LSHL:
2536  case ICMD_LSHR:
2537  case ICMD_LUSHR:
2538 
2539  case ICMD_IADD:
2540  case ICMD_IMUL:
2541 
2542  case ICMD_ISHL:
2543  case ICMD_ISHR:
2544  case ICMD_IUSHR:
2545  case ICMD_IAND:
2546  case ICMD_IOR:
2547  case ICMD_IXOR:
2548 
2549 
2550  case ICMD_FADD:
2551  case ICMD_FSUB:
2552  case ICMD_FMUL:
2553 
2554  case ICMD_DADD:
2555  case ICMD_DSUB:
2556  case ICMD_DMUL:
2557  case ICMD_DDIV:
2558  case ICMD_DREM:
2559  lsra_from_stack(ls, src, b_index, iindex);
2560  lsra_from_stack(ls, src->prev, b_index, iindex);
2561  lsra_new_stack(ls, dst, b_index, iindex);
2562 #ifdef JOIN_DEST_STACK
2563  lsra_join_3_stack(ls, dst, src->prev, src, JOIN_OP);
2564 #endif
2565  break;
2566 
2567  case ICMD_ISUB:
2568  lsra_from_stack(ls, src, b_index, iindex);
2569  lsra_from_stack(ls, src->prev,b_index,iindex);
2570  lsra_new_stack(ls, dst, b_index, iindex);
2571 #ifdef JOIN_DEST_STACK
2572  lsra_join_2_stack(ls, src, dst, JOIN_OP);
2573 #endif
2574  break;
2575 
2576  case ICMD_LDIV:
2577  case ICMD_LREM:
2578 
2579  case ICMD_IDIV:
2580  case ICMD_IREM:
2581 
2582  case ICMD_FDIV:
2583  case ICMD_FREM:
2584 
2585  case ICMD_LCMP:
2586  case ICMD_FCMPL:
2587  case ICMD_FCMPG:
2588  case ICMD_DCMPL:
2589  case ICMD_DCMPG:
2590  lsra_from_stack(ls, src, b_index, iindex);
2591  lsra_from_stack(ls, src->prev, b_index, iindex);
2592  lsra_new_stack(ls, dst, b_index, iindex);
2593  break;
2594 
2595  /* pop 1 push 1 */
2596  case ICMD_LADDCONST:
2597  case ICMD_LSUBCONST:
2598  case ICMD_LMULCONST:
2599  case ICMD_LMULPOW2:
2600  case ICMD_LDIVPOW2:
2601  case ICMD_LREMPOW2:
2602  case ICMD_LANDCONST:
2603  case ICMD_LORCONST:
2604  case ICMD_LXORCONST:
2605  case ICMD_LSHLCONST:
2606  case ICMD_LSHRCONST:
2607  case ICMD_LUSHRCONST:
2608 
2609  case ICMD_IADDCONST:
2610  case ICMD_ISUBCONST:
2611  case ICMD_IMULCONST:
2612  case ICMD_IMULPOW2:
2613  case ICMD_IDIVPOW2:
2614  case ICMD_IREMPOW2:
2615  case ICMD_IANDCONST:
2616  case ICMD_IORCONST:
2617  case ICMD_IXORCONST:
2618  case ICMD_ISHLCONST:
2619  case ICMD_ISHRCONST:
2620  case ICMD_IUSHRCONST:
2621 
2622 /* case ICMD_IFEQ_ICONST: */
2623 /* case ICMD_IFNE_ICONST: */
2624 /* case ICMD_IFLT_ICONST: */
2625 /* case ICMD_IFGE_ICONST: */
2626 /* case ICMD_IFGT_ICONST: */
2627 /* case ICMD_IFLE_ICONST: */
2628 
2629  case ICMD_INEG:
2630  case ICMD_INT2BYTE:
2631  case ICMD_INT2CHAR:
2632  case ICMD_INT2SHORT:
2633  case ICMD_LNEG:
2634  case ICMD_FNEG:
2635  case ICMD_DNEG:
2636 
2637  case ICMD_I2L:
2638  case ICMD_I2F:
2639  case ICMD_I2D:
2640  case ICMD_L2I:
2641  case ICMD_L2F:
2642  case ICMD_L2D:
2643  case ICMD_F2I:
2644  case ICMD_F2L:
2645  case ICMD_F2D:
2646  case ICMD_D2I:
2647  case ICMD_D2L:
2648  case ICMD_D2F:
2649 
2650  case ICMD_CHECKCAST:
2651  lsra_from_stack(ls, src, b_index, iindex);
2652  lsra_new_stack(ls, dst, b_index, iindex);
2653 #ifdef JOIN_DEST_STACK
2654  lsra_join_2_stack(ls, src, dst, JOIN_OP);
2655 #endif
2656  break;
2657 
2658  /* TODO: check if for these ICMDs JOIN_DEST_STACK works, too! */
2659  case ICMD_ARRAYLENGTH:
2660  case ICMD_INSTANCEOF:
2661 
2662  case ICMD_NEWARRAY:
2663  case ICMD_ANEWARRAY:
2664 
2665  case ICMD_GETFIELD:
2666  lsra_from_stack(ls, src, b_index, iindex);
2667  lsra_new_stack(ls, dst, b_index, iindex);
2668  break;
2669 
2670  /* pop 0 push 1 */
2671  case ICMD_GETSTATIC:
2672 
2673  case ICMD_NEW:
2674  lsra_new_stack(ls, dst, b_index, iindex);
2675  break;
2676 
2677  /* pop many push any */
2678 
2679  case ICMD_INVOKESTATIC:
2680  case ICMD_INVOKESPECIAL:
2681  case ICMD_INVOKEVIRTUAL:
2682  case ICMD_INVOKEINTERFACE:
2683  INSTRUCTION_GET_METHODDESC(iptr,md);
2684  i = md->paramcount;
2685  while (--i >= 0) {
2686  lsra_from_stack(ls, src, b_index, iindex);
2687  src = src->prev;
2688  }
2689  if (md->returntype.type != TYPE_VOID)
2690  lsra_new_stack(ls, dst, b_index, iindex);
2691  break;
2692 
2693 
2694  case ICMD_BUILTIN:
2695  bte = iptr->val.a;
2696  md = bte->md;
2697  i = md->paramcount;
2698  while (--i >= 0) {
2699  lsra_from_stack(ls, src, b_index, iindex);
2700  src = src->prev;
2701  }
2702  if (md->returntype.type != TYPE_VOID)
2703  lsra_new_stack(ls, dst, b_index, iindex);
2704  break;
2705 
2706  case ICMD_MULTIANEWARRAY:
2707  i = iptr->op1;
2708  while (--i >= 0) {
2709  lsra_from_stack(ls, src, b_index, iindex);
2710  src = src->prev;
2711  }
2712  lsra_new_stack(ls, dst, b_index, iindex);
2713  break;
2714 
2715  default:
2716  exceptions_throw_internalerror("Unknown ICMD %d during register allocation",
2717  iptr->opc);
2718  return;
2719  } /* switch */
2720  }
2721 }
2722 #endif /* defined(LV) */
2723 
2724 
2725 /*
2726  * These are local overrides for various environment variables in Emacs.
2727  * Please do not remove this and leave it at the end of the file, where
2728  * Emacs will automagically detect them.
2729  * ---------------------------------------------------------------------
2730  * Local variables:
2731  * mode: c++
2732  * indent-tabs-mode: t
2733  * c-basic-offset: 4
2734  * tab-width: 4
2735  * End:
2736  * vim:noexpandtab:sw=4:ts=4:
2737  */
int start
Definition: lsra.hpp:88
void lsra_add_cfg(jitdata *jd, int from, int to)
Definition: lsra.cpp:465
val_operand_t val
int i_last_use
Definition: lsra.hpp:107
#define GET_HIGH_REG(a)
int end
Definition: lsra.hpp:190
struct freemem * next
Definition: lsra.hpp:191
int savedvar
Definition: lsra.hpp:101
void lsra_add_ss(struct lifetime *, stackelement_t *)
Definition: lsra.cpp:1967
int argintreguse
Definition: reg.hpp:86
s4 exceptiontablelength
Definition: jit.hpp:167
struct _backedge ** backedge
Definition: lsra.hpp:158
int end
Definition: lsra.hpp:89
void lsra_make_cfg(jitdata *jd)
Definition: lsra.cpp:687
#define STATISTICS(x)
Wrapper for statistics only code.
Definition: statistics.hpp:975
#define lsra_join_dup(ls, s1, s2, s3)
Definition: lsra.cpp:2035
int * lt_int
Definition: lsra.hpp:171
int bb_last_use
Definition: lsra.hpp:105
Definition: jit.hpp:126
paramdesc * params
Definition: descriptor.hpp:164
int header
Definition: lsra.hpp:143
methoddesc * md
Definition: builtin.hpp:71
#define LSRA_STORE
Definition: lsra.hpp:63
#define max(a, b)
Definition: lsra.hpp:80
int bb_first_def
Definition: lsra.hpp:106
int lt_flt_count
Definition: lsra.hpp:174
int * savintregs
Definition: reg.hpp:71
int lifetimecount
Definition: lsra.hpp:169
exception_entry * exceptiontable
Definition: jit.hpp:168
#define REG_SAV
Definition: jit.hpp:442
#define IS_INT_LNG_TYPE(a)
Definition: global.hpp:130
argument_type from
void lsra_calc_lifetime_length(jitdata *)
Definition: lsra.cpp:1655
s4 maxlocals
Definition: jit.hpp:162
basicblock * next
Definition: jit.hpp:337
void lsra_add_sub(jitdata *jd, int b_index, struct _list *ret, bool *visited)
Definition: lsra.cpp:607
void lsra_setup(jitdata *)
Definition: lsra.cpp:845
struct _list ** succ
Definition: lsra.hpp:152
Definition: lsra.hpp:82
int sav_top
Definition: lsra.hpp:133
int value
Definition: lsra.hpp:83
s4 nregdescint[]
Definition: md-abi.cpp:41
void lsra_alloc(jitdata *, int *, int, int *)
Definition: lsra.cpp:1338
void lsra_qsort(struct lsradata *ls, int *a, int lo, int hi)
Definition: lsra.cpp:1193
int v_index
Definition: lsra.hpp:97
codegendata * cd
Definition: jit.hpp:129
int * lt_flt
Definition: lsra.hpp:173
void _spill_at_intervall(struct lifetime *, struct lifetime **, int *)
Definition: lsra.cpp:1600
int32_t flags
Definition: stack.hpp:67
#define lsra_pop_from_stack(ls, s, block, instr)
Definition: lsra.cpp:2078
Definition: stack.hpp:59
struct stackslot * lsra_make_ss(stackelement_t *s, int bb_index)
Definition: lsra.cpp:1957
#define LSRA_BB_IN
Definition: lsra.hpp:61
struct lifetime * lifetime
Definition: lsra.hpp:168
#define JOIN_OP
Definition: lsra.hpp:71
int * lt_mem
Definition: lsra.hpp:175
#define INT_REG_CNT
Definition: md-abi.hpp:72
int i_start
Definition: lsra.hpp:95
int * tmpfltregs
Definition: reg.hpp:72
int savintreguse
Definition: reg.hpp:88
void lsra_DFS(jitdata *jd)
Definition: lsra.cpp:176
void lsra_get_backedges_(lsradata *ls, int basicblockcount)
Definition: lsra.cpp:235
#define JOIN_DUP
Definition: lsra.hpp:70
stackelement_t * prev
Definition: stack.hpp:64
int * lt_used
Definition: lsra.hpp:170
#define lsra_new_stack(ls, s, block, instr)
Definition: lsra.cpp:2054
void lsra_reg_setup(jitdata *, struct lsra_register *, struct lsra_register *)
Definition: lsra.cpp:1035
struct _list * ret
Definition: lsra.hpp:144
#define lsra_join_2_stack(ls, dst, src, join_type)
Definition: lsra.cpp:2028
void lsra_main(jitdata *)
Definition: lsra.cpp:1245
int bb
Definition: lsra.hpp:125
int * nregdesc
Definition: lsra.hpp:132
int v_index
Definition: lsra.hpp:183
void lsra_add_jsr(jitdata *jd, int from, int to)
Definition: lsra.cpp:550
static int code_is_leafmethod(codeinfo *code)
Definition: code.hpp:151
stackelement_t * s
Definition: lsra.hpp:124
#define DNEW(type)
Definition: dumpmemory.hpp:370
struct freemem * lsra_getnewmem(int *)
Definition: lsra.cpp:1417
struct stackslot * next
Definition: lsra.hpp:126
void lsra_init(jitdata *)
Definition: lsra.cpp:808
typedesc paramtypes[1]
Definition: descriptor.hpp:167
#define INT_SAV_CNT
Definition: md-abi.hpp:73
#define IS_2_WORD_TYPE(a)
Definition: global.hpp:132
void spill_at_intervall(jitdata *, struct lifetime *)
Definition: lsra.cpp:1587
struct _backedge * next
Definition: lsra.hpp:91
int reg
Definition: lsra.hpp:100
#define GET_LOW_REG(a)
void lsra_add_active(struct lifetime *, struct lifetime **, int *)
Definition: lsra.cpp:1515
int nesting
Definition: lsra.hpp:90
BeginInst *& block
struct lifetime ** active_sav
Definition: lsra.hpp:179
int maxlifetimes
Definition: lsra.hpp:165
void lsra_setflags(int *, int)
Definition: lsra.cpp:1386
#define FLT_TMP_CNT
Definition: md-abi.hpp:82
struct _sbr sbr
Definition: lsra.hpp:161
void _lsra_from_stack(lsradata *, stackelement_t *, int, int, int)
Definition: lsra.cpp:2080
long * nesting
Definition: lsra.hpp:163
#define abort
Definition: md-asm.hpp:112
int lt_int_count
Definition: lsra.hpp:172
dst_operand_t dst
Instruction::InstID tmp[]
Definition: Matcher.cpp:55
bool compileverbose
Definition: options.cpp:82
void liveness_setup(jitdata *jd)
Definition: liveness.cpp:182
int prof_size
struct _list * next
Definition: lsra.hpp:84
alloc::list< PassInfo::IDTy >::type & stack
This file contains the statistics framework.
#define min(a, b)
Definition: lsra.hpp:79
void lsra_param_sort(struct lsradata *ls, int *lifetime, int lifetime_count)
Definition: lsra.cpp:1222
int i_end
Definition: lsra.hpp:96
bool checksync
Definition: options.cpp:90
#define JOIN_BB
Definition: lsra.hpp:69
#define _LSRA_ASSERT(a)
Definition: lsra.hpp:42
void exceptions_throw_internalerror(const char *message,...)
Definition: exceptions.cpp:805
#define INT_ARG_CNT
Definition: md-abi.hpp:74
bool lsra(jitdata *jd)
Definition: lsra.cpp:101
void lsra_get_backedges(jitdata *jd)
Definition: lsra.cpp:339
int lsra_getmem(struct lifetime *, struct freemem *, int *)
Definition: lsra.cpp:1397
int flags
Definition: lsra.hpp:102
int regoff
Definition: lsra.hpp:79
MIIterator i
void liveness(jitdata *jd)
Definition: liveness.cpp:406
typedesc returntype
Definition: descriptor.hpp:166
struct _sbr * next
Definition: lsra.hpp:145
int32_t s4
Definition: types.hpp:45
int argfltreguse
Definition: reg.hpp:89
void lsra_usage_local(lsradata *, s4, int, int, int, int)
Definition: lsra.cpp:2107
int * savfltregs
Definition: reg.hpp:73
VariableKind varkind
Definition: stack.hpp:68
registerdata * rd
Definition: jit.hpp:130
struct lifetime * get_ss_lifetime(lsradata *ls, stackelement_t *s)
Definition: lsra.cpp:1984
int * sorted
Definition: lsra.hpp:155
#define lsra_join_3_stack(ls, dst, src1, src2, join_type)
Definition: lsra.cpp:2017
#define JOIN
Definition: lsra.hpp:68
#define JOINING
Definition: lsra.hpp:75
void lsra_dump_stack(stackelement_t *)
Definition: lsra.cpp:2144
void _lsra_main(jitdata *, int *, int, struct lsra_register *, int *)
Definition: lsra.cpp:1428
void lsra_add_subs(jitdata *jd)
Definition: lsra.cpp:655
#define PACK_REGS(low, high)
int * sav_reg
Definition: lsra.hpp:130
#define LSRA_BB_OUT
Definition: lsra.hpp:62
int type
Definition: lsra.hpp:98
int savfltreguse
Definition: reg.hpp:91
void liveness_init(jitdata *jd)
Definition: liveness.cpp:267
bool inmemory
Definition: descriptor.hpp:151
void _lsra_expire_old_intervalls(jitdata *, struct lifetime *, struct lsra_register *, struct lifetime **, int *)
Definition: lsra.cpp:1535
#define INSTRUCTION_GET_METHODDESC(iptr, md)
long usagecount
Definition: lsra.hpp:99
#define lsra_from_stack(ls, s, block, instr)
Definition: lsra.cpp:2076
int * sorted_rev
Definition: lsra.hpp:156
uint32_t u4
Definition: types.hpp:46
#define INT_TMP_CNT
Definition: md-abi.hpp:75
#define FLT_SAV_CNT
Definition: md-abi.hpp:80
void lsra_get_nesting(jitdata *jd)
Definition: lsra.cpp:267
methoddesc * parseddesc
Definition: method.hpp:78
int memuse
Definition: reg.hpp:84
u4 ** prof_bb_freq
int * icount_block
Definition: lsra.hpp:185
int * tmpintregs
Definition: reg.hpp:70
Definition: builtin.hpp:60
void lsra_add_exceptions(jitdata *jd)
Definition: lsra.cpp:496
methodinfo * m
Definition: jit.hpp:127
int backedge_count
Definition: lsra.hpp:159
#define FLT_ARG_CNT
Definition: md-abi.hpp:81
Definition: lsra.hpp:142
int tmp_top
Definition: lsra.hpp:134
int lt_mem_count
Definition: lsra.hpp:177
void lsra_insertion_sort(struct lsradata *ls, int *a, int lo, int hi)
Definition: lsra.cpp:1178
#define DMNEW(type, num)
Definition: dumpmemory.hpp:371
int * tmp_reg
Definition: lsra.hpp:131
static int code_is_synchronized(codeinfo *code)
Definition: code.hpp:173
#define LSRA_LOAD
Definition: lsra.hpp:64
OStream & out()
Definition: OStream.cpp:39
int off
Definition: lsra.hpp:189
bool lsra_join_ss(struct lsradata *ls, struct stackelement *in, struct stackelement *out, int join_flag)
Definition: lsra.cpp:1853
void print_lifetimes(jitdata *, int *, int)
Definition: lsra.cpp:1804
#define LSRA_DEBUG_VERBOSE
Definition: lsra.hpp:34
void lsra_expire_old_intervalls(jitdata *, struct lifetime *, struct lsra_register *)
Definition: lsra.cpp:1526
static java_object_t * next
Definition: copy.c:43
int * num_pred
Definition: lsra.hpp:154
struct stackslot * local_ss
Definition: lsra.hpp:103
struct lifetime ** active_tmp
Definition: lsra.hpp:179
int active_sav_top
Definition: lsra.hpp:180
void _lsra_new_stack(lsradata *, stackelement_t *, int, int, int)
Definition: lsra.cpp:2056
s4 nregdescfloat[]
Definition: md-abi.cpp:98
s4 flags
Definition: method.hpp:70
void lsra_join_lifetimes(jitdata *, int)
Definition: lsra.cpp:1930
int32_t varnum
Definition: stack.hpp:69
void lsra_scan_registers_canditates(jitdata *, int)
Definition: lsra.cpp:2156
Nl nl
Definition: OStream.cpp:56
#define log_text(s)
Definition: logging.hpp:170
#define STAT_DECLARE_VAR(type, var, init)
Declare an external statistics variable.
Definition: statistics.hpp:963
uint32_t regoff
Definition: descriptor.hpp:153
#define ip
Definition: md-asm.hpp:59
#define printf(...)
Definition: ssa2.cpp:40
char ** prof_m_names
struct _list ** pred
Definition: lsra.hpp:153
#define FLT_REG_CNT
Definition: md-abi.hpp:79
int active_tmp_top
Definition: lsra.hpp:180
int i_first_def
Definition: lsra.hpp:108