ra_profits.c 5.1 KB

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