sr.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /* S T R E N G T H R E D U C T I O N */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "../share/types.h"
  11. #include "sr.h"
  12. #include "../share/debug.h"
  13. #include "../share/global.h"
  14. #include "../share/files.h"
  15. #include "../share/get.h"
  16. #include "../share/put.h"
  17. #include "../share/lset.h"
  18. #include "../share/map.h"
  19. #include "../share/alloc.h"
  20. #include "../share/go.h"
  21. #include "../share/aux.h"
  22. #include "sr_aux.h"
  23. #include "sr_iv.h"
  24. /* Strength reduction tries to change expensive operators occurring
  25. * in a loop into cheaper operators. The expensive operators considered
  26. * are multiplication, left-shift and array referencing.
  27. * The transformations can be expressed in C as:
  28. *
  29. * [1]: for (i = e1; i <= e2; i++)
  30. * print(118*i);
  31. * becomes:
  32. * for (i = e1, t = 118*e1; i <= e2; i++, t += 118)
  33. * print(t);
  34. *
  35. * [2]: for (i = e1; i <= e2; i++)
  36. * print(a[i]);
  37. * becomes:
  38. * for (i = e1, p = &a[i]; i <= e2; i++, p++)
  39. * print(*p);
  40. * The latter optimization is suppressed if array bound checking
  41. * is required.
  42. */
  43. /* Machine and/or language dependent parameters: */
  44. int ovfl_harmful;
  45. int arrbound_harmful;
  46. int sli_threshold;
  47. int Ssr; /* #optimizations found */
  48. static int sr_machinit(void *param)
  49. {
  50. /* Read target machine dependent information */
  51. char s[100];
  52. FILE *f = (FILE*)param;
  53. for (;;) {
  54. while(getc(f) != '\n');
  55. fscanf(f,"%s",s);
  56. if (strcmp(s,"%%SR") == 0)break;
  57. }
  58. fscanf(f,"%d",&ovfl_harmful);
  59. fscanf(f,"%d",&arrbound_harmful);
  60. fscanf(f,"%d",&sli_threshold);
  61. return 0;
  62. }
  63. static void del_ivs(lset ivs)
  64. {
  65. /* Delete the set of iv structs */
  66. Lindex i;
  67. for (i = Lfirst(ivs); i != (Lindex) 0; i = Lnext(i,ivs)) {
  68. oldiv(Lelem(i));
  69. }
  70. Ldeleteset(ivs);
  71. }
  72. static void do_loop(loop_p loop)
  73. {
  74. lset ivs, vars;
  75. OUTTRACE("going to process loop %d",loop->lp_id);
  76. induc_vars(loop,&ivs, &vars);
  77. /* Build a set of iv_structs, one for every induction
  78. * variable of the loop, i.e. a variable i that
  79. * is changed only by i := i + c, where c is a loop constant.
  80. * Also detects variables that are changed (including induction
  81. * variables!).
  82. */
  83. OUTTRACE("loop has %d induction variables",Lnrelems(ivs));
  84. if (Lnrelems(ivs) > 0) {
  85. strength_reduction(loop,ivs,vars);
  86. /* Perform strength reduction. Reduce:
  87. * iv * c to addition
  88. * a[iv] to indirection (*p)
  89. * (unless array bound checking is required)
  90. */
  91. }
  92. del_ivs(ivs);
  93. Ldeleteset(vars);
  94. }
  95. static void loopblocks(proc_p p)
  96. {
  97. /* Compute the LP_BLOCKS sets for all loops of p */
  98. bblock_p b;
  99. Lindex i;
  100. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  101. for (i = Lfirst(b->b_loops); i != (Lindex) 0;
  102. i = Lnext(i,b->b_loops)) {
  103. Ladd(b,&(((loop_p) Lelem(i))->LP_BLOCKS));
  104. }
  105. }
  106. }
  107. static void opt_proc(proc_p p)
  108. {
  109. /* Optimize all loops of one procedure. We first do all
  110. * outer loops at the lowest nesting level and proceed
  111. * in the inwards direction.
  112. */
  113. Lindex i;
  114. loop_p lp,outermost;
  115. int min_level;
  116. for (;;) {
  117. min_level = 1000;
  118. outermost = 0;
  119. for (i = Lfirst(p->p_loops); i != (Lindex) 0;
  120. i = Lnext(i,p->p_loops)) {
  121. lp = (loop_p) Lelem(i);
  122. if (!lp->LP_DONE && lp->lp_level < min_level) {
  123. min_level = lp->lp_level;
  124. outermost = lp;
  125. }
  126. }
  127. if (! outermost) break;
  128. do_loop(outermost);
  129. outermost->LP_DONE = TRUE;
  130. OUTTRACE("loop %d processed",outermost->lp_id);
  131. }
  132. }
  133. static bblock_p header(loop_p lp)
  134. {
  135. /* Try to determine the 'header' block of loop lp.
  136. * If 'e' is the entry block of loop L, then block 'b' is
  137. * called the header block of L, iff:
  138. * SUCC(b) = {e} & b dominates e.
  139. * If lp has no header block, 0 is returned.
  140. */
  141. bblock_p x = lp->lp_entry->b_idom;
  142. if ( (x != NULL) && (Lnrelems(x->b_succ) == 1) &&
  143. ((bblock_p) Lelem(Lfirst(x->b_succ)) == lp->lp_entry) ) {
  144. return x;
  145. }
  146. return NULL;
  147. }
  148. static void sr_extproc(proc_p p)
  149. {
  150. /* Allocate the extended data structures for procedure p */
  151. loop_p lp;
  152. Lindex pi;
  153. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  154. pi = Lnext(pi,p->p_loops)) {
  155. lp = (loop_p) Lelem(pi);
  156. lp->lp_extend = newsrlpx();
  157. lp->LP_HEADER = header(lp);
  158. if (lp->LP_HEADER) {
  159. lp->LP_INSTR = last_instr(lp->LP_HEADER);
  160. }
  161. }
  162. }
  163. static void sr_cleanproc(proc_p p)
  164. {
  165. /* Remove the extended data structures for procedure p */
  166. loop_p lp;
  167. Lindex pi;
  168. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  169. pi = Lnext(pi,p->p_loops)) {
  170. lp = (loop_p) Lelem(pi);
  171. oldsrlpx(lp->lp_extend);
  172. }
  173. }
  174. static int sr_optimize(void *param)
  175. {
  176. proc_p p = (proc_p)param;
  177. if (IS_ENTERED_WITH_GTO(p)) return 0;
  178. sr_extproc(p);
  179. loopblocks(p);
  180. opt_proc(p);
  181. sr_cleanproc(p);
  182. return 0;
  183. }
  184. int main(int argc, char *argv[])
  185. {
  186. go(argc,argv,no_action,sr_optimize,sr_machinit,no_action);
  187. report("strength reductions",Ssr);
  188. exit(0);
  189. }