ra_profits.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* R E G I S T E R A L L O C A T I O N
  7. *
  8. * R A _ P R O F I T S . C
  9. */
  10. #include <stdio.h>
  11. #include <em_reg.h>
  12. #include "../share/types.h"
  13. #include "../share/debug.h"
  14. #include "../share/lset.h"
  15. #include "../share/global.h"
  16. #include "ra.h"
  17. #include "ra_aux.h"
  18. #include "ra_profits.h"
  19. static bool test_cond(short cond, offset val)
  20. {
  21. switch(cond) {
  22. case DEFAULT:
  23. return TRUE;
  24. case FITBYTE:
  25. return val >= -128 && val < 128;
  26. case IN_0_63:
  27. return val >= 0 && val <= 63;
  28. case IN_0_8:
  29. return val >= 0 && val <= 8;
  30. }
  31. return FALSE;
  32. }
  33. static short map_value(struct cond_tab tab[], offset val, bool time)
  34. {
  35. cond_p p;
  36. for (p = &tab[0]; ; p++) {
  37. if (test_cond(p->mc_cond,val)) {
  38. return (time ? p->mc_tval : p->mc_sval);
  39. }
  40. }
  41. return 0;
  42. }
  43. static short index_value(struct cond_tab tab[], short n, bool time)
  44. {
  45. cond_p p;
  46. p = &tab[n];
  47. return (time ? p->mc_tval : p->mc_sval);
  48. }
  49. static void allocscore(short itemtyp, short localtyp, short size, offset off, short totyp,
  50. short *time_out, short *space_out)
  51. {
  52. cond_p m = (cond_p) 0;
  53. if (localtyp == reg_loop) localtyp = reg_any;
  54. if ( ( (size == ws) || (size ==ps) ) && (totyp == reg_pointer) ) {
  55. switch(itemtyp) {
  56. case LOCALVAR:
  57. m = alocaltab[localtyp][totyp];
  58. break;
  59. case LOCAL_ADDR:
  60. if (use_any_as_pointer || totyp == reg_pointer)
  61. m = alocaddrtab[localtyp][totyp];
  62. break;
  63. case CONST:
  64. m = aconsttab;
  65. break;
  66. case DCONST:
  67. m = aconsttab;
  68. break;
  69. case GLOBL_ADDR:
  70. if (use_any_as_pointer || totyp == reg_pointer)
  71. m = aglobaltab;
  72. break;
  73. case PROC_ADDR:
  74. if (use_any_as_pointer || totyp == reg_pointer)
  75. m = aproctab;
  76. break;
  77. }
  78. }
  79. *time_out = (m == (cond_p) 0 ? -1 : map_value(m,off,TRUE));
  80. *space_out = (m == (cond_p) 0 ? -1 : map_value(m,off,FALSE));
  81. /*
  82. fprintf(stderr,"itemtyp = %d, localtyp = %d off = %ld\n",itemtyp,localtyp,off);
  83. fprintf(stderr,"ALLOCSCORE = (%d,%d)\n",*time_out,*space_out);
  84. */
  85. }
  86. static void opening_cost(short itemtyp, short localtyp, offset off,
  87. short *time_out, short *space_out)
  88. {
  89. cond_p m;
  90. if (localtyp == reg_loop) localtyp = reg_any;
  91. switch(itemtyp) {
  92. case LOCALVAR:
  93. m = olocaltab[localtyp];
  94. break;
  95. case LOCAL_ADDR:
  96. m = olocaddrtab[localtyp];
  97. break;
  98. case CONST:
  99. m = oconsttab;
  100. break;
  101. case DCONST:
  102. m = oconsttab;
  103. break;
  104. case GLOBL_ADDR:
  105. m = oglobaltab;
  106. break;
  107. case PROC_ADDR:
  108. m = oproctab;
  109. break;
  110. }
  111. *time_out = (m == (cond_p) 0 ? 1000 : map_value(m,off,TRUE));
  112. *space_out = (m == (cond_p) 0 ? 1000 : map_value(m,off,FALSE));
  113. /*
  114. fprintf(stderr,"itemtyp = %d, localtyp = %d off = %ld\n",itemtyp,localtyp,off);
  115. fprintf(stderr,"OPEN_COST = (%d,%d)\n",*time_out,*space_out);
  116. */
  117. }
  118. void regsave_cost(short regs[], short *time_out, short *space_out)
  119. {
  120. /* Estimate the costs of saving and restoring the registers
  121. * The array regs contains the number of registers of every
  122. * possible type.
  123. */
  124. short n = regs[reg_any] + regs[reg_pointer] + regs[reg_float];
  125. /* #registers */
  126. *time_out = index_value(regsav_cost,n,TRUE);
  127. *space_out = index_value(regsav_cost,n,FALSE);
  128. /*
  129. fprintf(stderr,"REGSAVE COST, n=%d, (%d,%d)\n",n,*time_out,*space_out);
  130. */
  131. }
  132. static short dyn_inits(lset inits)
  133. {
  134. Lindex i;
  135. short sum = 0;
  136. bblock_p b;
  137. for (i = Lfirst(inits); i != (Lindex) 0; i = Lnext(i,inits)) {
  138. b = (bblock_p) Lelem(i);
  139. sum += loop_scale(Lnrelems(b->b_loops));
  140. }
  141. return sum;
  142. }
  143. void compute_profits(alloc_p alloclist, bool time_opt)
  144. {
  145. /* Compute the profits attribute of every allocation.
  146. * If the item of an allocation may be put in several types
  147. * of register, we choose only the most advanteagous one.
  148. */
  149. alloc_p alloc;
  150. short s,t,rtyp,maxsc;
  151. item_p item;
  152. short time,space,sc;
  153. short otime,ospace;
  154. offset off;
  155. short cnt,nr_inits;
  156. for (alloc = alloclist; alloc != (alloc_p) 0; alloc = alloc->al_next) {
  157. maxsc = 0;
  158. item = alloc->al_item;
  159. switch(item->it_type) {
  160. case LOCALVAR:
  161. case LOCAL_ADDR:
  162. case CONST:
  163. case DCONST:
  164. off = item->i_t.it_off;
  165. break;
  166. default:
  167. off = 0;
  168. }
  169. for (rtyp = item->it_regtype; ; rtyp = reg_any) {
  170. allocscore( item->it_type,
  171. item->it_regtype,
  172. item->it_size,
  173. off,
  174. rtyp,
  175. &time,
  176. &space);
  177. opening_cost( item->it_type,
  178. item->it_regtype,
  179. off,
  180. &otime,
  181. &ospace);
  182. nr_inits = Lnrelems(alloc->al_inits);
  183. s = alloc->al_susecount * space -
  184. nr_inits*ospace;
  185. #ifdef __STRANGE__
  186. if (!alloc->al_isloop && nr_inits > 0) {
  187. /* might lead to increase of execution time */
  188. cnt = 0;
  189. } else
  190. #endif
  191. {
  192. cnt = alloc->al_dusecount;
  193. }
  194. t = cnt * time - dyn_inits(alloc->al_inits) * otime;
  195. sc = (time_opt ? t : s);
  196. /*
  197. fprintf(stderr, "cnt: %d time: %d otime: %d t: %d s: %d score: %d\n", cnt, time, otime, t, s, sc);
  198. */
  199. if (sc > maxsc) {
  200. maxsc = sc;
  201. alloc->al_regtype = rtyp;
  202. alloc->al_profits = sc;
  203. }
  204. if (rtyp == reg_any) break;
  205. }
  206. }
  207. }