sr.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "sr_reduce.h"
  25. /* Strength reduction tries to change expensive operators occurring
  26. * in a loop into cheaper operators. The expensive operators considered
  27. * are multiplication, left-shift and array referencing.
  28. * The transformations can be expressed in C as:
  29. *
  30. * [1]: for (i = e1; i <= e2; i++)
  31. * print(118*i);
  32. * becomes:
  33. * for (i = e1, t = 118*e1; i <= e2; i++, t += 118)
  34. * print(t);
  35. *
  36. * [2]: for (i = e1; i <= e2; i++)
  37. * print(a[i]);
  38. * becomes:
  39. * for (i = e1, p = &a[i]; i <= e2; i++, p++)
  40. * print(*p);
  41. * The latter optimization is suppressed if array bound checking
  42. * is required.
  43. */
  44. /* Machine and/or language dependent parameters: */
  45. int ovfl_harmful;
  46. int arrbound_harmful;
  47. int sli_threshold;
  48. int Ssr; /* #optimizations found */
  49. static int sr_machinit(void *param)
  50. {
  51. /* Read target machine dependent information */
  52. char s[100];
  53. FILE *f = (FILE*)param;
  54. for (;;) {
  55. while(getc(f) != '\n');
  56. fscanf(f,"%s",s);
  57. if (strcmp(s,"%%SR") == 0)break;
  58. }
  59. fscanf(f,"%d",&ovfl_harmful);
  60. fscanf(f,"%d",&arrbound_harmful);
  61. fscanf(f,"%d",&sli_threshold);
  62. return 0;
  63. }
  64. static void del_ivs(lset ivs)
  65. {
  66. /* Delete the set of iv structs */
  67. Lindex i;
  68. for (i = Lfirst(ivs); i != (Lindex) 0; i = Lnext(i,ivs)) {
  69. oldiv(Lelem(i));
  70. }
  71. Ldeleteset(ivs);
  72. }
  73. static void do_loop(loop_p loop)
  74. {
  75. lset ivs, vars;
  76. OUTTRACE("going to process loop %d",loop->lp_id);
  77. induc_vars(loop,&ivs, &vars);
  78. /* Build a set of iv_structs, one for every induction
  79. * variable of the loop, i.e. a variable i that
  80. * is changed only by i := i + c, where c is a loop constant.
  81. * Also detects variables that are changed (including induction
  82. * variables!).
  83. */
  84. OUTTRACE("loop has %d induction variables",Lnrelems(ivs));
  85. if (Lnrelems(ivs) > 0) {
  86. strength_reduction(loop,ivs,vars);
  87. /* Perform strength reduction. Reduce:
  88. * iv * c to addition
  89. * a[iv] to indirection (*p)
  90. * (unless array bound checking is required)
  91. */
  92. }
  93. del_ivs(ivs);
  94. Ldeleteset(vars);
  95. }
  96. static void loopblocks(proc_p p)
  97. {
  98. /* Compute the LP_BLOCKS sets for all loops of p */
  99. bblock_p b;
  100. Lindex i;
  101. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  102. for (i = Lfirst(b->b_loops); i != (Lindex) 0;
  103. i = Lnext(i,b->b_loops)) {
  104. Ladd(b,&(((loop_p) Lelem(i))->LP_BLOCKS));
  105. }
  106. }
  107. }
  108. static void opt_proc(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. outermost = 0;
  120. for (i = Lfirst(p->p_loops); i != (Lindex) 0;
  121. i = Lnext(i,p->p_loops)) {
  122. lp = (loop_p) Lelem(i);
  123. if (!lp->LP_DONE && lp->lp_level < min_level) {
  124. min_level = lp->lp_level;
  125. outermost = lp;
  126. }
  127. }
  128. if (! outermost) break;
  129. do_loop(outermost);
  130. outermost->LP_DONE = TRUE;
  131. OUTTRACE("loop %d processed",outermost->lp_id);
  132. }
  133. }
  134. static bblock_p header(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 != NULL) && (Lnrelems(x->b_succ) == 1) &&
  144. ((bblock_p) Lelem(Lfirst(x->b_succ)) == lp->lp_entry) ) {
  145. return x;
  146. }
  147. return NULL;
  148. }
  149. static void sr_extproc(proc_p p)
  150. {
  151. /* Allocate the extended data structures for procedure p */
  152. loop_p lp;
  153. Lindex pi;
  154. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  155. pi = Lnext(pi,p->p_loops)) {
  156. lp = (loop_p) Lelem(pi);
  157. lp->lp_extend = newsrlpx();
  158. lp->LP_HEADER = header(lp);
  159. if (lp->LP_HEADER) {
  160. lp->LP_INSTR = last_instr(lp->LP_HEADER);
  161. }
  162. }
  163. }
  164. static void sr_cleanproc(proc_p p)
  165. {
  166. /* Remove the extended data structures for procedure p */
  167. loop_p lp;
  168. Lindex pi;
  169. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  170. pi = Lnext(pi,p->p_loops)) {
  171. lp = (loop_p) Lelem(pi);
  172. oldsrlpx(lp->lp_extend);
  173. }
  174. }
  175. static int sr_optimize(void *param)
  176. {
  177. proc_p p = (proc_p)param;
  178. if (IS_ENTERED_WITH_GTO(p)) return 0;
  179. sr_extproc(p);
  180. loopblocks(p);
  181. opt_proc(p);
  182. sr_cleanproc(p);
  183. return 0;
  184. }
  185. int main(int argc, char *argv[])
  186. {
  187. go(argc,argv,no_action,sr_optimize,sr_machinit,no_action);
  188. report("strength reductions",Ssr);
  189. exit(0);
  190. }