ra_items.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 _ I T E M S . C
  9. */
  10. #include <em_mnem.h>
  11. #include <em_spec.h>
  12. #include <em_pseu.h>
  13. #include <em_reg.h>
  14. #include "../share/types.h"
  15. #include "../share/debug.h"
  16. #include "../share/def.h"
  17. #include "../share/global.h"
  18. #include "../share/lset.h"
  19. #include "../share/aux.h"
  20. #include "../share/alloc.h"
  21. #include "ra.h"
  22. #include "ra_aux.h"
  23. #include "ra_items.h"
  24. #include "itemtab.h"
  25. /* Maps EM mnemonics onto item types, e.g. op_lol -> LOCALVAR, op_ldc->DCONST,
  26. * generated from em_mmen.h and itemtab.src files.
  27. */
  28. #define SMALL_CONSTANT(c) (c >= 0 && c <= 8)
  29. /* prevent small constants from being put in a register */
  30. static void clean_tab(item_p items[])
  31. {
  32. int t;
  33. for (t = 0; t < NRITEMTYPES;t++) {
  34. items[t] = (item_p) 0;
  35. }
  36. }
  37. short item_type(line_p l)
  38. {
  39. int instr = INSTR(l);
  40. int t;
  41. if (instr < sp_fmnem || instr > sp_lmnem) return NO_ITEM;
  42. t = itemtab[instr - sp_fmnem].id_type;
  43. if (t == CONST && SMALL_CONSTANT(off_set(l))) return NO_ITEM;
  44. return t;
  45. }
  46. bool is_item(line_p l)
  47. {
  48. return item_type(l) != NO_ITEM;
  49. }
  50. item_p item_of(offset off, item_p items[])
  51. {
  52. item_p x;
  53. for (x = items[LOCALVAR]; x != (item_p) 0; x = x->it_next) {
  54. if (off == x->i_t.it_off) {
  55. if (!x->it_desirable) break;
  56. /* don't put this item in reg */
  57. return x;
  58. }
  59. }
  60. return (item_p) 0;
  61. }
  62. void fill_item(item_p item, line_p l)
  63. {
  64. item->it_type = item_type(l);
  65. item->it_desirable = TRUE;
  66. switch(item->it_type) {
  67. case GLOBL_ADDR:
  68. item->i_t.it_obj = OBJ(l);
  69. break;
  70. case PROC_ADDR:
  71. item->i_t.it_proc = PROC(l);
  72. break;
  73. default:
  74. item->i_t.it_off = off_set(l);
  75. }
  76. }
  77. static bool desirable(line_p l)
  78. {
  79. /* See if it is really desirable to put the item of line l
  80. * in a register. We do not put an item in a register if it
  81. * is used as 'address of array descriptor' of an array
  82. * instruction.
  83. */
  84. if (l->l_next != (line_p) 0) {
  85. switch(INSTR(l->l_next)) {
  86. case op_aar:
  87. case op_lar:
  88. case op_sar:
  89. return FALSE;
  90. }
  91. }
  92. return TRUE;
  93. }
  94. static int cmp_items(item_p a, item_p b)
  95. {
  96. /* This routine defines the <, = and > relations between items,
  97. * used to sort them for fast lookup.
  98. */
  99. offset n1,n2;
  100. switch(a->it_type) {
  101. case GLOBL_ADDR:
  102. assert(b->it_type == GLOBL_ADDR);
  103. n1 = (offset) a->i_t.it_obj->o_id;
  104. n2 = (offset) b->i_t.it_obj->o_id;
  105. break;
  106. case PROC_ADDR:
  107. assert(b->it_type == PROC_ADDR);
  108. n1 = (offset) a->i_t.it_proc->p_id;
  109. n2 = (offset) b->i_t.it_proc->p_id;
  110. break;
  111. default:
  112. n1 = a->i_t.it_off;
  113. n2 = b->i_t.it_off;
  114. }
  115. return (n1 == n2 ? 0 : (n1 > n2 ? 1 : -1));
  116. }
  117. bool same_item(item_p a, item_p b)
  118. {
  119. return cmp_items(a,b) == 0;
  120. }
  121. static bool lt_item(item_p a, item_p b)
  122. {
  123. return cmp_items(a,b) == -1;
  124. }
  125. /* build_itemlist()
  126. *
  127. * Build a list of all items used in the current procedure. An item
  128. * is anything that can be put in a register (a local variable, a constant,
  129. * the address of a local or global variable).
  130. * For each type of item we use a sorted list containing all items of
  131. * that type found so far.
  132. * A local variable is only considered to be an item if there is a
  133. * register message for it (indicating it is never accessed indirectly).
  134. * For each item, we keep track of all places where it is used
  135. * (either fetched or stored into). The usage of a local variable is also
  136. * considered to be a usage of its address.
  137. */
  138. static item_p items[NRITEMTYPES]; /* items[i] points to the list of type i */
  139. static short reg_type(item_p item)
  140. {
  141. /* See which type of register the item should best be assigned to */
  142. switch(item->it_type) {
  143. case LOCALVAR:
  144. return regv_type(item->i_t.it_off);
  145. /* use type mentioned in reg. message for local */
  146. case LOCAL_ADDR:
  147. case GLOBL_ADDR:
  148. case PROC_ADDR:
  149. return reg_pointer;
  150. case CONST:
  151. case DCONST:
  152. return reg_any;
  153. default: assert(FALSE);
  154. }
  155. /* NOTREACHED */
  156. return 0;
  157. }
  158. static short item_size(item_p item)
  159. {
  160. /* Determine the size of the item (in bytes) */
  161. switch(item->it_type) {
  162. case LOCALVAR:
  163. return regv_size(item->i_t.it_off);
  164. /* use size mentioned in reg. message for local */
  165. case LOCAL_ADDR:
  166. case GLOBL_ADDR:
  167. case PROC_ADDR:
  168. return ps; /* pointer size */
  169. case CONST:
  170. return ws; /* word size */
  171. case DCONST:
  172. return 2 * ws; /* 2 * word size */
  173. default: assert(FALSE);
  174. }
  175. /* NOTREACHED */
  176. return 0;
  177. }
  178. static void init_item(item_p a, item_p b)
  179. {
  180. a->it_type = b->it_type;
  181. switch(a->it_type) {
  182. case GLOBL_ADDR:
  183. a->i_t.it_obj = b->i_t.it_obj;
  184. break;
  185. case PROC_ADDR:
  186. a->i_t.it_proc = b->i_t.it_proc;
  187. break;
  188. default:
  189. a->i_t.it_off = b->i_t.it_off;
  190. }
  191. a->it_usage = Lempty_set();
  192. a->it_regtype = reg_type(b);
  193. a->it_size = item_size(b);
  194. a->it_desirable = b->it_desirable;
  195. }
  196. static void add_item(item_p item, time_p t, item_p items[])
  197. {
  198. /* See if there was already a list element for item. In any
  199. * case record the fact that item is used at 't'.
  200. */
  201. item_p x, *q;
  202. q = &items[item->it_type]; /* each type has its own list */
  203. for (x = *q; x != (item_p) 0; x = *q) {
  204. if (same_item(x,item)) {
  205. /* found */
  206. if (!item->it_desirable) {
  207. x->it_desirable = FALSE;
  208. }
  209. Ladd(t,&x->it_usage);
  210. return; /* done */
  211. }
  212. if (lt_item(item,x)) break;
  213. q = &x->it_next;
  214. }
  215. /* not found, allocate new item; q points to it_next field of
  216. * the item after which the new item should be put.
  217. */
  218. x = newitem();
  219. x->it_next = *q;
  220. *q = x;
  221. init_item(x,item);
  222. Ladd(t,&x->it_usage);
  223. }
  224. static void add_usage(line_p l, bblock_p b, item_p items[])
  225. {
  226. /* An item is used at line l. Add it to the list of items.
  227. * A local variable is only considered to be an item, if
  228. * there is a register message for it; else its address
  229. * is also considered to be an item.
  230. */
  231. struct item thisitem;
  232. fill_item(&thisitem,l); /* fill in some fields */
  233. if (!desirable(l)) {
  234. thisitem.it_desirable = FALSE; /* don't put item in reg. */
  235. }
  236. if (thisitem.it_type == LOCALVAR && !is_regvar(thisitem.i_t.it_off)) {
  237. /* Use address of local instead of local itself */
  238. thisitem.it_type = LOCAL_ADDR;
  239. thisitem.it_regtype = reg_pointer;
  240. }
  241. add_item(&thisitem,cons_time(l,b),items);
  242. }
  243. void build_itemlist(proc_p p, item_p items[], int *nrinstr_out)
  244. {
  245. /* Make a list of all items used in procedure p.
  246. * An item is anything that can be put in a register,
  247. * such as a local variable, a constant etc.
  248. * As a side effect, determine the number of instructions of p.
  249. */
  250. register line_p l;
  251. register bblock_p b;
  252. register cnt= 0;
  253. clean_tab(items);
  254. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  255. for (l = b->b_start; l != (line_p) 0; l = l->l_next) {
  256. if (is_item(l)) {
  257. add_usage(l,b,items);
  258. }
  259. cnt++;
  260. }
  261. }
  262. *nrinstr_out = cnt;
  263. }