bo.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. /* B R A N C H O P T I M I Z A T I O N
  7. *
  8. * B O . C
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <em_mnem.h>
  13. #include <em_pseu.h>
  14. #include <em_spec.h>
  15. #include <em_flag.h>
  16. #include "../share/types.h"
  17. #include "../share/debug.h"
  18. #include "../share/global.h"
  19. #include "../share/files.h"
  20. #include "../share/get.h"
  21. #include "../share/put.h"
  22. #include "../share/lset.h"
  23. #include "../share/map.h"
  24. #include "../share/alloc.h"
  25. #include "../share/aux.h"
  26. #include "../share/def.h"
  27. #include "../share/go.h"
  28. extern char em_flag[];
  29. #define LP_BLOCKS lp_extend->lpx_ra.lpx_blocks
  30. #define newbolpx() (lpext_p) newstruct(lpext_ra)
  31. #define oldbolpx(x) oldstruct(lpext_ra,x)
  32. static int Sbo; /* #optimizations found */
  33. #define DLINK(l1,l2) l1->l_next=l2; l2->l_prev=l1
  34. /* This module performs some very simple branch optimizations.
  35. *
  36. * I) Look for pairs of basic blocks (B1,B2), such that
  37. * SUCC(b1) = {B2} and
  38. * PRED(B2) = {B1}.
  39. * In this case B1 and B2 can be combined into one block.
  40. * This optimization is mainly succesful:
  41. * 1) for switch statements in C, as the C compiler generates a branch
  42. * over the entire switch.
  43. * 2) for return statements, if the only way to return from a procedure
  44. * is via a return statement somewhere in the middle of the procedure.
  45. * II) Optimize while statements. Transformations like:
  46. * 1: jmp 2
  47. * tst cond 1:
  48. * beq 2f S
  49. * S 2:
  50. * jmp 1 tst cond
  51. * 2: bneq 1
  52. * are done by this optimization.
  53. */
  54. static line_p last_code(line_p lines, bool skip_pseu)
  55. {
  56. /* Determine the last line of a list */
  57. line_p l;
  58. for (l = lines; l->l_next != (line_p) 0; l = l->l_next);
  59. if (skip_pseu) {
  60. while (l && (INSTR(l) < sp_fmnem || INSTR(l) > sp_lmnem)) l = PREV(l);
  61. }
  62. return l;
  63. }
  64. static short cc_tab[12] =
  65. {op_blt,op_zlt,op_ble,op_zle,op_beq,op_zeq,
  66. op_zne,op_bne,op_zgt,op_bgt,op_zge,op_bge};
  67. static short rev_cond(short cond)
  68. {
  69. int i;
  70. for (i = 0; i < 12; i++) {
  71. if (cond == cc_tab[i]) return cc_tab[11-i];
  72. }
  73. return op_nop;
  74. }
  75. static bool is_bcc(line_p l)
  76. {
  77. return rev_cond(INSTR(l)) != op_nop;
  78. }
  79. static void bo_optloop(proc_p p, bblock_p b, bblock_p x, line_p bra, line_p bcc)
  80. {
  81. bblock_p prevb,n;
  82. line_p l;
  83. if (b->b_start == bra) {
  84. b->b_start = (line_p) 0;
  85. } else {
  86. PREV(bra)->l_next = (line_p) 0;
  87. }
  88. PREV(bra) = (line_p) 0;
  89. bcc->l_instr = rev_cond(INSTR(bcc));
  90. n = x->b_next;
  91. l = n->b_start;
  92. if (l == (line_p) 0 || INSTR(l) != op_lab) {
  93. l = newline(OPINSTRLAB);
  94. l->l_instr = op_lab;
  95. INSTRLAB(l) = freshlabel();
  96. if (n->b_start != (line_p) 0) {
  97. DLINK(l,n->b_start);
  98. }
  99. n->b_start = l;
  100. }
  101. INSTRLAB(bcc) = INSTRLAB(l);
  102. for (prevb = p->p_start; prevb != (bblock_p) 0 && prevb->b_next != x;
  103. prevb = prevb->b_next);
  104. if (prevb == (bblock_p) 0) {
  105. p->p_start = x->b_next;
  106. } else {
  107. prevb->b_next = x->b_next;
  108. l = last_instr(prevb);
  109. if (l == (line_p) 0) {
  110. prevb->b_start = bra;
  111. } else {
  112. if ((em_flag[INSTR(l)-sp_fmnem]&EM_FLO) == FLO_T) {
  113. oldline(bra);
  114. } else {
  115. appnd_line(bra,l);
  116. }
  117. }
  118. }
  119. x->b_next = b->b_next;
  120. b->b_next = x;
  121. }
  122. static void bo_tryloop(proc_p p, lset loop)
  123. {
  124. Lindex i,j;
  125. bblock_p b,x;
  126. line_p bra,bcc;
  127. for (i = Lfirst(loop); i != (Lindex) 0; i = Lnext(i,loop)) {
  128. b = (bblock_p) Lelem(i);
  129. if (b->b_next != (bblock_p) 0 && !Lis_elem(b->b_next,loop)) {
  130. j = Lfirst(b->b_succ);
  131. if (j != (Lindex) 0 &&
  132. (bra = last_instr(b)) != (line_p) 0 &&
  133. INSTR(bra) == op_bra) {
  134. x = (bblock_p) Lelem(j); /* single successor */
  135. if (Lis_elem(b->b_next,x->b_succ) &&
  136. is_bcc((bcc = last_instr(x)))) {
  137. OUTVERBOSE("branch optimization proc %d block %d\n", curproc->p_id,x->b_id);
  138. Sbo++;
  139. bo_optloop(p,b,x,bra,bcc);
  140. return;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. static void bo_loops(proc_p p)
  147. {
  148. Lindex i;
  149. loop_p lp;
  150. for (i = Lfirst(p->p_loops); i != (Lindex) 0; i = Lnext(i,p->p_loops)) {
  151. lp = (loop_p) (Lelem(i));
  152. bo_tryloop(p,lp->LP_BLOCKS);
  153. }
  154. }
  155. static void mv_code(bblock_p b1, bblock_p b2)
  156. {
  157. line_p l,x;
  158. l = last_code(b2->b_start,TRUE);
  159. assert(INSTR(l) == op_bra);
  160. DLINK(l,b1->b_start);
  161. x = l->l_next;
  162. rm_line(l,b2);
  163. if (INSTR(x) == op_lab) {
  164. rm_line(x,b2);
  165. }
  166. }
  167. void bo_switch(bblock_p b)
  168. {
  169. bblock_p s,x;
  170. Lindex i;
  171. line_p l,bra;
  172. if (Lnrelems(b->b_succ) == 1) {
  173. s = (bblock_p) Lelem(Lfirst(b->b_succ));
  174. if (b->b_start != (line_p) 0 &&
  175. s->b_start != (line_p) 0 &&
  176. Lnrelems(s->b_pred) == 1 &&
  177. (bra = last_code(b->b_start,TRUE)) != (line_p) 0 &&
  178. INSTR(bra) == op_bra &&
  179. (s->b_next == (bblock_p) 0 ||
  180. !Lis_elem(s->b_next,s->b_succ) ||
  181. ((bra = last_code(s->b_start, TRUE)) != (line_p) 0 &&
  182. (em_flag[INSTR(bra)-sp_fmnem]&EM_FLO) == FLO_T))) {
  183. l = last_code(s->b_start,FALSE);
  184. if (INSTR(l) == ps_end) {
  185. if (PREV(l) == (line_p) 0) return;
  186. PREV(l)->l_next = (line_p) 0;
  187. PREV(l) = (line_p) 0;
  188. } else {
  189. l = (line_p) 0;
  190. }
  191. OUTVERBOSE("branch optimization in proc %d, block %d",curproc->p_id,b->b_id);
  192. Sbo++;
  193. Ldeleteset(b->b_succ);
  194. b->b_succ = s->b_succ;
  195. Ldeleteset(s->b_pred);
  196. s->b_succ = Lempty_set();
  197. s->b_pred = Lempty_set();
  198. for (i = Lfirst(b->b_succ); i != (Lindex) 0;
  199. i = Lnext(i,b->b_succ)) {
  200. x = (bblock_p) Lelem(i);
  201. Lremove(s,&x->b_pred);
  202. Ladd(b,&x->b_pred);
  203. if (x->b_idom == s) {
  204. x->b_idom = b;
  205. }
  206. }
  207. mv_code(s,b);
  208. s->b_start = l;
  209. }
  210. }
  211. }
  212. static void bo_extproc(proc_p p)
  213. {
  214. /* Allocate the extended data structures for procedure p */
  215. loop_p lp;
  216. Lindex pi;
  217. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  218. pi = Lnext(pi,p->p_loops)) {
  219. lp = (loop_p) Lelem(pi);
  220. lp->lp_extend = newbolpx();
  221. }
  222. }
  223. static void loop_blocks(proc_p p)
  224. {
  225. /* Compute the LP_BLOCKS sets for all loops of p */
  226. bblock_p b;
  227. Lindex i;
  228. for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
  229. for (i = Lfirst(b->b_loops); i != (Lindex) 0;
  230. i = Lnext(i,b->b_loops)) {
  231. Ladd(b,&(((loop_p) Lelem(i))->LP_BLOCKS));
  232. }
  233. }
  234. }
  235. static void bo_cleanproc(proc_p p)
  236. {
  237. /* Allocate the extended data structures for procedure p */
  238. loop_p lp;
  239. Lindex pi;
  240. bblock_p b;
  241. for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
  242. pi = Lnext(pi,p->p_loops)) {
  243. lp = (loop_p) Lelem(pi);
  244. oldbolpx(lp->lp_extend);
  245. }
  246. }
  247. int bo_optimize(void *param)
  248. {
  249. proc_p p = (proc_p)param;
  250. bblock_p b;
  251. if (IS_ENTERED_WITH_GTO(p)) return 0;
  252. bo_extproc(p);
  253. loop_blocks(p);
  254. bo_loops(p);
  255. for (b = p->p_start; b != 0; b = b->b_next) {
  256. bo_switch(b);
  257. }
  258. bo_cleanproc(p);
  259. return 0;
  260. }
  261. int main(int argc, char *argv[])
  262. {
  263. go(argc, argv, no_action, bo_optimize, no_action, no_action);
  264. report("branch optimizations", Sbo);
  265. exit(0);
  266. return 0;
  267. }