cs_profit.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <stdio.h>
  7. #include <em_mnem.h>
  8. #include <em_spec.h>
  9. #include "../share/types.h"
  10. #include "../share/debug.h"
  11. #include "../share/global.h"
  12. #include "../share/aux.h"
  13. #include "../share/cset.h"
  14. #include "../share/lset.h"
  15. #include "cs.h"
  16. #include "cs_aux.h"
  17. #include "cs_debug.h"
  18. #include "cs_avail.h"
  19. #include "cs_partit.h"
  20. STATIC cset addr_modes;
  21. STATIC cset cheaps;
  22. STATIC cset forbidden;
  23. STATIC cset sli_counts;
  24. STATIC short LX_threshold;
  25. STATIC short AR_limit;
  26. STATIC get_instrs(f, s_p)
  27. FILE *f;
  28. cset *s_p;
  29. {
  30. /* Read a set of integers from inputfile f into *s_p.
  31. * Such a set must be delimited by a negative number.
  32. */
  33. int instr;
  34. fscanf(f, "%d", &instr);
  35. while (instr >= 0) {
  36. Cadd((Celem_t) instr, s_p);
  37. fscanf(f, "%d", &instr);
  38. }
  39. }
  40. STATIC choose_cset(f, s_p, max)
  41. FILE *f;
  42. cset *s_p;
  43. {
  44. /* Read two compact sets of integers from inputfile f.
  45. * Choose the first if we optimize with respect to time,
  46. * the second if we optimize with respect to space, as
  47. * indicated by time_space_ratio.
  48. */
  49. cset cs1, cs2; /* Two dummy sets. */
  50. *s_p = Cempty_set((short) max);
  51. cs1 = Cempty_set((short) max);
  52. get_instrs(f, &cs1);
  53. cs2 = Cempty_set((short) max);
  54. get_instrs(f, &cs2);
  55. Ccopy_set(time_space_ratio >= 50 ? cs1 : cs2, s_p);
  56. Cdeleteset(cs1); Cdeleteset(cs2);
  57. }
  58. cs_machinit(f)
  59. FILE *f;
  60. {
  61. char s[100];
  62. int time, space;
  63. /* Find piece that is relevant for this phase. */
  64. do {
  65. while (getc(f) != '\n');
  66. fscanf(f, "%s", s);
  67. } while (strcmp(s, "%%CS"));
  68. /* Choose a set of instructions which must only be eliminated
  69. * if they are at the root of another expression.
  70. */
  71. choose_cset(f, &addr_modes, sp_lmnem);
  72. /* Choose a set of cheap instructions; i.e. instructions that
  73. * are cheaper than a move to save the result of such an
  74. * instruction.
  75. */
  76. choose_cset(f, &cheaps, sp_lmnem);
  77. /* Read how many lexical levels back an LXL/LXA instruction
  78. * must at least look before it will be eliminated.
  79. */
  80. fscanf(f, "%d %d", &time, &space);
  81. LX_threshold = time_space_ratio >= 50 ? time : space;
  82. /* Read what the size of an array-element may be,
  83. * before we think that it is to big to replace
  84. * a LAR/SAR of it by AAR LOI/STI <size>.
  85. */
  86. fscanf(f, "%d", &space);
  87. AR_limit = space;
  88. /* Read for what counts we must not eliminate an SLI instruction
  89. * when it is part of an array-index computation.
  90. */
  91. choose_cset(f, &sli_counts, 8 * ws);
  92. /* Read a set of instructions which we do not want to eliminate.
  93. * Note: only instructions need be given that may in principle
  94. * be eliminated, but for which better code can be generated
  95. * when they stay, and with which is not dealt in the common
  96. * decision routines.
  97. */
  98. choose_cset(f, &forbidden, sp_lmnem);
  99. }
  100. STATIC bool sli_no_eliminate(lnp)
  101. line_p lnp;
  102. {
  103. /* Return whether the SLI-instruction in lnp is part of
  104. * an array-index computation, and should not be eliminated.
  105. */
  106. offset cst;
  107. return lnp->l_prev != (line_p) 0 && INSTR(lnp->l_prev) == op_loc &&
  108. lnp->l_next != (line_p) 0 && INSTR(lnp->l_next) == op_ads &&
  109. ((cst = off_set(lnp->l_prev)), cst == (Celem_t) cst) &&
  110. Cis_elem((Celem_t) cst, sli_counts)
  111. ;
  112. }
  113. STATIC bool gains(avp)
  114. avail_p avp;
  115. {
  116. /* Return whether we can gain something, when we eliminate
  117. * an expression such as in avp. We just glue together some
  118. * heuristics with some user-supplied stuff.
  119. */
  120. if (Cis_elem(avp->av_instr & BMASK, forbidden))
  121. return FALSE;
  122. if (avp->av_instr == (byte) op_lxa || avp->av_instr == (byte) op_lxl)
  123. return off_set(avp->av_found) >= LX_threshold;
  124. if (avp->av_instr == (byte) op_sli || avp->av_instr == (byte) op_slu)
  125. return ! sli_no_eliminate(avp->av_found);
  126. if (avp->av_instr == (byte) op_ads &&
  127. avp->av_found->l_prev &&
  128. ( INSTR(avp->av_found->l_prev) == op_sli ||
  129. INSTR(avp->av_found->l_prev) == op_slu))
  130. return ! sli_no_eliminate(avp->av_found->l_prev);
  131. if (Cis_elem(avp->av_instr & BMASK, addr_modes))
  132. return instrgroup(avp->av_found->l_prev) != SIMPLE_LOAD;
  133. if (Cis_elem(avp->av_instr & BMASK, cheaps))
  134. return avp->av_saveloc != (entity_p) 0;
  135. return TRUE;
  136. }
  137. STATIC bool okay_lines(avp, ocp)
  138. avail_p avp;
  139. occur_p ocp;
  140. {
  141. register line_p lnp, next;
  142. offset sz;
  143. for (lnp = ocp->oc_lfirst; lnp != (line_p) 0; lnp = next) {
  144. next = lnp != ocp->oc_llast ? lnp->l_next : (line_p) 0;
  145. if (INSTR(lnp) < sp_fmnem || INSTR(lnp) > sp_lmnem)
  146. return FALSE;
  147. if (!stack_group(INSTR(lnp))) {
  148. /* Check for SAR-instruction. */
  149. if (INSTR(lnp) != op_sar || next != (line_p) 0)
  150. return FALSE;
  151. }
  152. }
  153. /* All lines in this occurrence can in principle be eliminated;
  154. * no stores, messages, calls etc.
  155. * We now check whether it is desirable to treat a LAR or a SAR
  156. * as an AAR LOI/STI. This depends on the size of the array-elements.
  157. */
  158. if (INSTR(ocp->oc_llast) == op_lar || INSTR(ocp->oc_llast) == op_sar) {
  159. sz = array_elemsize(avp->av_othird);
  160. if (sz == UNKNOWN_SIZE) return FALSE;
  161. if (avp->av_instr == (byte) op_aar && time_space_ratio < 50) {
  162. return sz <= AR_limit;
  163. }
  164. }
  165. return TRUE;
  166. }
  167. bool desirable(avp)
  168. avail_p avp;
  169. {
  170. register Lindex i, next;
  171. if (!gains(avp)) {
  172. OUTTRACE("no gain", 0);
  173. SHOWAVAIL(avp);
  174. return FALSE;
  175. }
  176. /* Walk through the occurrences to see whether it is okay to
  177. * eliminate them. If not, remove them from the set.
  178. */
  179. for (i = Lfirst(avp->av_occurs); i != (Lindex) 0; i = next) {
  180. next = Lnext(i, avp->av_occurs);
  181. if (!okay_lines(avp, occ_elem(i))) {
  182. OUTTRACE("may not eliminate", 0);
  183. # ifdef TRACE
  184. SHOWOCCUR(occ_elem(i));
  185. # endif
  186. oldoccur(occ_elem(i));
  187. Lremove(Lelem(i), &avp->av_occurs);
  188. }
  189. }
  190. return Lnrelems(avp->av_occurs) > 0;
  191. }