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. #if 0
  139. static item_p items[NRITEMTYPES]; /* items[i] points to the list of type i */
  140. #endif
  141. static short reg_type(item_p item)
  142. {
  143. /* See which type of register the item should best be assigned to */
  144. switch(item->it_type) {
  145. case LOCALVAR:
  146. return regv_type(item->i_t.it_off);
  147. /* use type mentioned in reg. message for local */
  148. case LOCAL_ADDR:
  149. case GLOBL_ADDR:
  150. case PROC_ADDR:
  151. return reg_pointer;
  152. case CONST:
  153. case DCONST:
  154. return reg_any;
  155. default: assert(FALSE);
  156. }
  157. /* NOTREACHED */
  158. return 0;
  159. }
  160. static short item_size(item_p item)
  161. {
  162. /* Determine the size of the item (in bytes) */
  163. switch(item->it_type) {
  164. case LOCALVAR:
  165. return regv_size(item->i_t.it_off);
  166. /* use size mentioned in reg. message for local */
  167. case LOCAL_ADDR:
  168. case GLOBL_ADDR:
  169. case PROC_ADDR:
  170. return ps; /* pointer size */
  171. case CONST:
  172. return ws; /* word size */
  173. case DCONST:
  174. return 2 * ws; /* 2 * word size */
  175. default: assert(FALSE);
  176. }
  177. /* NOTREACHED */
  178. return 0;
  179. }
  180. static void init_item(item_p a, item_p b)
  181. {
  182. a->it_type = b->it_type;
  183. switch(a->it_type) {
  184. case GLOBL_ADDR:
  185. a->i_t.it_obj = b->i_t.it_obj;
  186. break;
  187. case PROC_ADDR:
  188. a->i_t.it_proc = b->i_t.it_proc;
  189. break;
  190. default:
  191. a->i_t.it_off = b->i_t.it_off;
  192. }
  193. a->it_usage = Lempty_set();
  194. a->it_regtype = reg_type(b);
  195. a->it_size = item_size(b);
  196. a->it_desirable = b->it_desirable;
  197. }
  198. static void add_item(item_p item, time_p t, item_p items[])
  199. {
  200. /* See if there was already a list element for item. In any
  201. * case record the fact that item is used at 't'.
  202. */
  203. item_p x, *q;
  204. q = &items[item->it_type]; /* each type has its own list */
  205. for (x = *q; x != (item_p) 0; x = *q) {
  206. if (same_item(x,item)) {
  207. /* found */
  208. if (!item->it_desirable) {
  209. x->it_desirable = FALSE;
  210. }
  211. Ladd(t,&x->it_usage);
  212. return; /* done */
  213. }
  214. if (lt_item(item,x)) break;
  215. q = &x->it_next;
  216. }
  217. /* not found, allocate new item; q points to it_next field of
  218. * the item after which the new item should be put.
  219. */
  220. x = newitem();
  221. x->it_next = *q;
  222. *q = x;
  223. init_item(x,item);
  224. Ladd(t,&x->it_usage);
  225. }
  226. static void add_usage(line_p l, bblock_p b, item_p items[])
  227. {
  228. /* An item is used at line l. Add it to the list of items.
  229. * A local variable is only considered to be an item, if
  230. * there is a register message for it; else its address
  231. * is also considered to be an item.
  232. */
  233. struct item thisitem;
  234. fill_item(&thisitem,l); /* fill in some fields */
  235. if (!desirable(l)) {
  236. thisitem.it_desirable = FALSE; /* don't put item in reg. */
  237. }
  238. if (thisitem.it_type == LOCALVAR && !is_regvar(thisitem.i_t.it_off)) {
  239. /* Use address of local instead of local itself */
  240. thisitem.it_type = LOCAL_ADDR;
  241. thisitem.it_regtype = reg_pointer;
  242. }
  243. add_item(&thisitem,cons_time(l,b),items);
  244. }
  245. void build_itemlist(proc_p p, item_p items[], int *nrinstr_out)
  246. {
  247. /* Make a list of all items used in procedure p.
  248. * An item is anything that can be put in a register,
  249. * such as a local variable, a constant etc.
  250. * As a side effect, determine the number of instructions of p.
  251. */
  252. register line_p l;
  253. register bblock_p b;
  254. register cnt= 0;
  255. clean_tab(items);
  256. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  257. for (l = b->b_start; l != (line_p) 0; l = l->l_next) {
  258. if (is_item(l)) {
  259. add_usage(l,b,items);
  260. }
  261. cnt++;
  262. }
  263. }
  264. *nrinstr_out = cnt;
  265. }