optimize.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014, 2015, 2017, 2018 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Commentary:
  17. ;;;
  18. ;;; Optimizations on CPS.
  19. ;;;
  20. ;;; Code:
  21. (define-module (language cps optimize)
  22. #:use-module (ice-9 match)
  23. #:use-module (language cps contification)
  24. #:use-module (language cps cse)
  25. #:use-module (language cps devirtualize-integers)
  26. #:use-module (language cps dce)
  27. #:use-module (language cps licm)
  28. #:use-module (language cps peel-loops)
  29. #:use-module (language cps prune-top-level-scopes)
  30. #:use-module (language cps rotate-loops)
  31. #:use-module (language cps self-references)
  32. #:use-module (language cps simplify)
  33. #:use-module (language cps specialize-primcalls)
  34. #:use-module (language cps specialize-numbers)
  35. #:use-module (language cps type-fold)
  36. #:use-module (language cps verify)
  37. #:export (optimize-higher-order-cps
  38. optimize-first-order-cps
  39. cps-optimizations))
  40. (define (kw-arg-ref args kw default)
  41. (match (memq kw args)
  42. ((_ val . _) val)
  43. (_ default)))
  44. (define *debug?* #f)
  45. (define (maybe-verify program)
  46. (if *debug?*
  47. (verify program)
  48. program))
  49. (define-syntax-rule (define-optimizer optimize (pass kw default) ...)
  50. (define* (optimize program #:optional (opts '()))
  51. ;; This series of assignments to `program' used to be a series of
  52. ;; let* bindings of `program', as you would imagine. In compiled
  53. ;; code this is fine because the compiler is able to allocate all
  54. ;; let*-bound variable to the same slot, which also means that the
  55. ;; garbage collector doesn't have to retain so many copies of the
  56. ;; term being optimized. However during bootstrap, the interpreter
  57. ;; doesn't do this optimization, leading to excessive data retention
  58. ;; as the terms are rewritten. To marginally improve bootstrap
  59. ;; memory usage, here we use set! instead. The compiler should
  60. ;; produce the same code in any case, though currently it does not
  61. ;; because it doesn't do escape analysis on the box created for the
  62. ;; set!.
  63. (maybe-verify program)
  64. (set! program
  65. (if (kw-arg-ref opts kw default)
  66. (maybe-verify (pass program))
  67. program))
  68. ...
  69. (maybe-verify program)))
  70. ;; Passes that are needed:
  71. ;;
  72. ;; * Abort contification: turning abort primcalls into continuation
  73. ;; calls, and eliding prompts if possible.
  74. ;;
  75. (define-optimizer optimize-higher-order-cps
  76. ;; FIXME: split-rec call temporarily moved to compile-bytecode and run
  77. ;; unconditionally, because closure conversion requires it. Move the
  78. ;; pass back here when that's fixed.
  79. ;;
  80. ;; (split-rec #:split-rec? #t)
  81. (eliminate-dead-code #:eliminate-dead-code? #t)
  82. (prune-top-level-scopes #:prune-top-level-scopes? #t)
  83. (simplify #:simplify? #t)
  84. (contify #:contify? #t)
  85. (simplify #:simplify? #t)
  86. (devirtualize-integers #:devirtualize-integers? #t)
  87. (peel-loops #:peel-loops? #t)
  88. (eliminate-common-subexpressions #:cse? #t)
  89. (type-fold #:type-fold? #t)
  90. (resolve-self-references #:resolve-self-references? #t)
  91. (eliminate-dead-code #:eliminate-dead-code? #t)
  92. (simplify #:simplify? #t))
  93. (define-optimizer optimize-first-order-cps
  94. (specialize-numbers #:specialize-numbers? #t)
  95. (hoist-loop-invariant-code #:licm? #t)
  96. (specialize-primcalls #:specialize-primcalls? #t)
  97. (eliminate-common-subexpressions #:cse? #t)
  98. (eliminate-dead-code #:eliminate-dead-code? #t)
  99. ;; Running simplify here enables rotate-loops to do a better job.
  100. (simplify #:simplify? #t)
  101. (rotate-loops #:rotate-loops? #t)
  102. (simplify #:simplify? #t))
  103. (define (cps-optimizations)
  104. '( ;; (#:split-rec? #t)
  105. (#:simplify? 2)
  106. (#:eliminate-dead-code? 2)
  107. (#:prune-top-level-scopes? 2)
  108. (#:contify? 2)
  109. (#:specialize-primcalls? 2)
  110. (#:peel-loops? 2)
  111. (#:cse? 2)
  112. (#:type-fold? 2)
  113. (#:resolve-self-references? 2)
  114. (#:devirtualize-integers? 2)
  115. (#:specialize-numbers? 2)
  116. (#:licm? 2)
  117. (#:rotate-loops? 2)
  118. ;; This one is used by the slot allocator.
  119. (#:precolor-calls? 2)))