sr.c 4.8 KB

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