flow.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Id$";
  3. #endif
  4. #include "param.h"
  5. #include "types.h"
  6. #include "tes.h"
  7. #include <em_flag.h>
  8. #include <em_spec.h>
  9. #include <em_mnem.h>
  10. #include "alloc.h"
  11. #include "line.h"
  12. #include "proinf.h"
  13. #include "optim.h"
  14. #include "ext.h"
  15. /*
  16. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  17. * See the copyright notice in the ACK home directory, in the file "Copyright".
  18. *
  19. * Author: Hans van Staveren
  20. */
  21. flow() {
  22. findreach(); /* determine reachable labels */
  23. cleaninstrs(); /* throw away unreachable code */
  24. }
  25. findreach() {
  26. register num_p *npp,np;
  27. reach(instrs);
  28. for(npp=curpro.numhash;npp< &curpro.numhash[NNUMHASH]; npp++)
  29. for(np= *npp; np != (num_p) 0 ; np = np->n_next)
  30. if (np->n_flags&NUMDATA) {
  31. np->n_repl->n_flags |= NUMREACH;
  32. np->n_repl->n_jumps++;
  33. if (!(np->n_flags&NUMSCAN)) {
  34. np->n_flags |= NUMSCAN;
  35. if (np->n_line) {
  36. reach(np->n_line->l_next);
  37. continue;
  38. }
  39. if (!(np->n_repl->n_flags&NUMSCAN)) {
  40. np->n_repl->n_flags |= NUMSCAN;
  41. if (np->n_repl->n_line)
  42. reach(np->n_repl->n_line->l_next);
  43. }
  44. }
  45. }
  46. }
  47. reach(lnp) register line_p lnp; {
  48. register num_p np;
  49. for (;lnp != (line_p) 0; lnp = lnp->l_next) {
  50. if(lnp->l_optyp == OPNUMLAB) {
  51. /*
  52. * Branch instruction or label
  53. */
  54. np = lnp->l_a.la_np;
  55. if ((lnp->l_instr&BMASK) != op_lab)
  56. lnp->l_a.la_np = np = np->n_repl;
  57. np->n_flags |= NUMREACH;
  58. if (!(np->n_flags&NUMSCAN)) {
  59. np->n_flags |= NUMSCAN;
  60. if (np->n_line)
  61. reach(np->n_line->l_next);
  62. else {
  63. np = np->n_repl;
  64. np->n_flags |= NUMREACH;
  65. if (!(np->n_flags & NUMSCAN)) {
  66. np->n_flags |= NUMSCAN;
  67. if (np->n_line)
  68. reach(np->n_line->l_next);
  69. }
  70. }
  71. }
  72. if ((lnp->l_instr&BMASK) == op_lab)
  73. return;
  74. else
  75. np->n_jumps++;
  76. }
  77. if ((lnp->l_instr & BMASK) > sp_lmnem) continue;
  78. if ((em_flag[(lnp->l_instr&BMASK)-sp_fmnem]&EM_FLO)==FLO_T)
  79. return;
  80. }
  81. }
  82. cleaninstrs() {
  83. register line_p *lpp,lp,*lastbra;
  84. bool reachable,superfluous;
  85. int instr;
  86. lpp = &instrs; lastbra = (line_p *) 0; reachable = TRUE;
  87. while ((lp = *lpp) != (line_p) 0) {
  88. instr = lp->l_instr&BMASK;
  89. if (instr == op_lab) {
  90. if ((lp->l_a.la_np->n_flags&NUMREACH) != 0) {
  91. reachable = TRUE;
  92. if (lastbra != (line_p *) 0
  93. && (*lastbra)->l_next == lp
  94. && (*lastbra)->l_a.la_np->n_repl==lp->l_a.la_np) {
  95. oldline(*lastbra);
  96. OPTIM(O_BRALAB);
  97. lpp = lastbra;
  98. *lpp = lp;
  99. lastbra = (line_p *) 0;
  100. lp->l_a.la_np->n_jumps--;
  101. }
  102. }
  103. if ( lp->l_a.la_np->n_repl != lp->l_a.la_np ||
  104. ((lp->l_a.la_np->n_flags&NUMDATA)==0 &&
  105. lp->l_a.la_np->n_jumps == 0))
  106. superfluous = TRUE;
  107. else
  108. superfluous = FALSE;
  109. } else
  110. superfluous = FALSE;
  111. if ( (!reachable) || superfluous) {
  112. if (instr == op_lab) {
  113. lp->l_a.la_np->n_line = 0;
  114. }
  115. else if (instr > sp_lmnem) {
  116. /* leave pseudo's */
  117. lpp = &lp->l_next;
  118. continue;
  119. }
  120. lp = lp->l_next;
  121. oldline(*lpp);
  122. OPTIM(O_UNREACH);
  123. *lpp = lp;
  124. } else {
  125. if ( instr <= sp_lmnem &&
  126. (em_flag[instr-sp_fmnem]&EM_FLO)==FLO_T) {
  127. reachable = FALSE;
  128. if ((lp->l_instr&BMASK) == op_bra)
  129. lastbra = lpp;
  130. }
  131. lpp = &lp->l_next;
  132. }
  133. }
  134. }