cs.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /* C O M M O N S U B E X P R E S S I O N E L I M I N A T I O N */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "../share/types.h"
  10. #include "../share/lset.h"
  11. #include "../share/debug.h"
  12. #include "../share/go.h"
  13. #include "cs.h"
  14. #include "cs_aux.h"
  15. #include "cs_avail.h"
  16. #include "cs_debug.h"
  17. #include "cs_elim.h"
  18. #include "cs_entity.h"
  19. #include "cs_profit.h"
  20. #include "cs_stack.h"
  21. #include "cs_vnm.h"
  22. int Scs; /* Number of optimizations found. */
  23. STATIC cs_clear()
  24. {
  25. clr_avails();
  26. clr_entities();
  27. clr_stack();
  28. start_valnum();
  29. }
  30. STATIC cs_optimize(p)
  31. proc_p p;
  32. {
  33. /* Optimize all basic blocks of one procedure. */
  34. register bblock_p rbp, bdone;
  35. if (IS_ENTERED_WITH_GTO(p)) return;
  36. avails = (avail_p) 0;
  37. entities = Lempty_set();
  38. cs_clear();
  39. rbp = p->p_start;
  40. while (rbp != (bblock_p) 0) {
  41. /* First we build a list of common expressions with the
  42. * value numbering algorithm. We take blocks in textual order
  43. * as long as the next block can only be reached through the
  44. * block we have just done. Note that if a block is preceded
  45. * by itself, the number of predecessors is greater than 1,
  46. * but the previous block can still be its immediate dominator.
  47. */
  48. do { vnm(rbp); bdone = rbp;
  49. OUTTRACE("basic block %d processed", bdone->b_id);
  50. rbp = rbp->b_next;
  51. } while (rbp != (bblock_p) 0 && rbp->b_idom == bdone &&
  52. Lnrelems(rbp->b_pred) == 1
  53. );
  54. OUTTRACE("value numbering completed", 0);
  55. OUTAVAILS(); OUTENTITIES();
  56. /* Now we put out the instructions without common
  57. * subexpressions but with the use of temporaries,
  58. * which will be local variables of procedure p.
  59. */
  60. eliminate(p);
  61. cs_clear();
  62. }
  63. }
  64. main(argc, argv)
  65. int argc;
  66. char *argv[];
  67. {
  68. Scs = 0;
  69. go(argc, argv, no_action, cs_optimize, cs_machinit, no_action);
  70. report("Duplicate expressions eliminated", Scs);
  71. exit(0);
  72. }