cs_profit.c 5.7 KB

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