cs_profit.c 5.7 KB

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