optimize.scm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; Tree-il optimizer
  2. ;; Copyright (C) 2009, 2010-2015, 2018-2021, 2024 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. ;;; Code:
  17. (define-module (language tree-il optimize)
  18. #:use-module (ice-9 match)
  19. ;; FIXME: Perhaps allow bootstrap builds to skip fix-letrec, because
  20. ;; it imports intset, intmap, etc.
  21. #:use-module (language tree-il fix-letrec)
  22. #:use-module (system base optimize)
  23. #:export (optimize
  24. make-lowerer
  25. tree-il-optimizations))
  26. (define-syntax-rule (lazy-ref mod proc)
  27. (module-ref (resolve-interface 'mod) 'proc))
  28. (define (dump-optimized-tree-il exp env)
  29. ((lazy-ref (ice-9 pretty-print) pretty-print)
  30. ((lazy-ref (language scheme decompile-tree-il) decompile-tree-il)
  31. exp env '()))
  32. exp)
  33. (define (make-optimizer opts)
  34. (define-syntax lookup
  35. (syntax-rules ()
  36. ((lookup kw id)
  37. (lookup kw id id))
  38. ((lookup kw submodule proc)
  39. (and (assq-ref opts kw)
  40. (lazy-ref (language tree-il submodule) proc)))))
  41. (let ((verify (or (lookup #:verify-tree-il? debug verify-tree-il)
  42. (lambda (exp) exp)))
  43. (modulify (lookup #:resolve-free-vars? resolve-free-vars))
  44. (resolve (lookup #:resolve-primitives? primitives resolve-primitives))
  45. (expand (lookup #:expand-primitives? primitives expand-primitives))
  46. (letrectify (lookup #:letrectify? letrectify))
  47. (seal? (assq-ref opts #:seal-private-bindings?))
  48. (xinline? (assq-ref opts #:cross-module-inlining?))
  49. (demux (lookup #:demux-lambda? demux-lambda))
  50. (peval (lookup #:partial-eval? peval))
  51. (eta-expand (lookup #:eta-expand? eta-expand))
  52. (inlinables (lookup #:inlinable-exports? inlinable-exports)))
  53. (define-syntax-rule (run-pass! (proc exp arg ...))
  54. (when proc (set! exp (verify (proc exp arg ...)))))
  55. (lambda (exp env)
  56. (verify exp)
  57. (run-pass! (modulify exp))
  58. (run-pass! (resolve exp env))
  59. (run-pass! (expand exp))
  60. (run-pass! (letrectify exp #:seal-private-bindings? seal?))
  61. (run-pass! (demux exp))
  62. (run-pass! (fix-letrec exp))
  63. (run-pass! (peval exp env #:cross-module-inlining? xinline?))
  64. (run-pass! (eta-expand exp))
  65. (run-pass! (inlinables exp))
  66. (when (assq-ref opts #:dump-optimized-tree-il?)
  67. (dump-optimized-tree-il exp env))
  68. exp)))
  69. (define (optimize x env opts)
  70. ((make-optimizer opts) x env))
  71. (define (tree-il-optimizations)
  72. (available-optimizations 'tree-il))
  73. (define (tree-il-options)
  74. (cons* '(#:dump-optimized-tree-il? #f)
  75. '(#:verify-tree-il? #f)
  76. (tree-il-optimizations)))
  77. (define (make-lowerer optimization-level opts)
  78. (define (kw-arg-ref args kw default)
  79. (match (memq kw args)
  80. ((_ val . _) val)
  81. (_ default)))
  82. (define (enabled-for-level? level) (and level (<= level optimization-level)))
  83. (make-optimizer
  84. (let lp ((all-opts (tree-il-options)))
  85. (match all-opts
  86. (() '())
  87. (((kw level) . all-opts)
  88. (acons kw (kw-arg-ref opts kw (enabled-for-level? level))
  89. (lp all-opts)))))))