optimize.scm 5.7 KB

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