cs.c 1.8 KB

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