optimize.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; Tree-il optimizer
  2. ;; Copyright (C) 2009, 2010-2015, 2018-2021 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 (make-optimizer opts)
  27. (define-syntax lookup
  28. (syntax-rules ()
  29. ((lookup kw id)
  30. (lookup kw id id))
  31. ((lookup kw submodule proc)
  32. (and (assq-ref opts kw)
  33. (module-ref (resolve-interface '(language tree-il submodule))
  34. 'proc)))))
  35. (let ((verify (or (lookup #:verify-tree-il? debug verify-tree-il)
  36. (lambda (exp) exp)))
  37. (modulify (lookup #:resolve-free-vars? resolve-free-vars))
  38. (resolve (lookup #:resolve-primitives? primitives resolve-primitives))
  39. (expand (lookup #:expand-primitives? primitives expand-primitives))
  40. (letrectify (lookup #:letrectify? letrectify))
  41. (seal? (assq-ref opts #:seal-private-bindings?))
  42. (xinline? (assq-ref opts #:cross-module-inlining?))
  43. (peval (lookup #:partial-eval? peval))
  44. (eta-expand (lookup #:eta-expand? eta-expand))
  45. (inlinables (lookup #:inlinable-exports? inlinable-exports)))
  46. (define-syntax-rule (run-pass! (proc exp arg ...))
  47. (when proc (set! exp (verify (proc exp arg ...)))))
  48. (lambda (exp env)
  49. (verify exp)
  50. (run-pass! (modulify exp))
  51. (run-pass! (resolve exp env))
  52. (run-pass! (expand exp))
  53. (run-pass! (letrectify exp #:seal-private-bindings? seal?))
  54. (run-pass! (fix-letrec exp))
  55. (run-pass! (peval exp env #:cross-module-inlining? xinline?))
  56. (run-pass! (eta-expand exp))
  57. (run-pass! (inlinables exp))
  58. exp)))
  59. (define (optimize x env opts)
  60. ((make-optimizer opts) x env))
  61. (define (tree-il-optimizations)
  62. (available-optimizations 'tree-il))
  63. (define (make-lowerer optimization-level opts)
  64. (define (kw-arg-ref args kw default)
  65. (match (memq kw args)
  66. ((_ val . _) val)
  67. (_ default)))
  68. (define (enabled-for-level? level) (<= level optimization-level))
  69. (make-optimizer
  70. (let lp ((all-opts (tree-il-optimizations)))
  71. (match all-opts
  72. (() '())
  73. (((kw level) . all-opts)
  74. (acons kw (kw-arg-ref opts kw (enabled-for-level? level))
  75. (lp all-opts)))))))