optimize.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; Tree-il optimizer
  2. ;; Copyright (C) 2009, 2010-2015, 2018-2020 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 (language tree-il)
  19. #:use-module (language tree-il debug)
  20. #:use-module (language tree-il eta-expand)
  21. #:use-module (language tree-il fix-letrec)
  22. #:use-module (language tree-il letrectify)
  23. #:use-module (language tree-il peval)
  24. #:use-module (language tree-il primitives)
  25. #:use-module (ice-9 match)
  26. #:export (optimize
  27. tree-il-optimizations))
  28. (define (kw-arg-ref args kw default)
  29. (match (memq kw args)
  30. ((_ val . _) val)
  31. (_ default)))
  32. (define *debug?* #f)
  33. (define (maybe-verify x)
  34. (if *debug?*
  35. (verify-tree-il x)
  36. x))
  37. (define (optimize x env opts)
  38. (define-syntax-rule (run-pass pass kw default)
  39. (when (kw-arg-ref opts kw default)
  40. (set! x (maybe-verify (pass x)))))
  41. (define (resolve* x) (resolve-primitives x env))
  42. (define (peval* x) (peval x env))
  43. (define (letrectify* x)
  44. (let ((seal? (kw-arg-ref opts #:seal-private-bindings? #f)))
  45. (letrectify x #:seal-private-bindings? seal?)))
  46. (maybe-verify x)
  47. (run-pass resolve* #:resolve-primitives? #t)
  48. (run-pass expand-primitives #:expand-primitives? #t)
  49. (run-pass letrectify* #:letrectify? #t)
  50. (set! x (fix-letrec x))
  51. (run-pass peval* #:partial-eval? #t)
  52. (run-pass eta-expand #:eta-expand? #t)
  53. x)
  54. (define (tree-il-optimizations)
  55. ;; Avoid resolve-primitives until -O2, when CPS optimizations kick in.
  56. ;; Otherwise, inlining the primcalls during Tree-IL->CPS compilation
  57. ;; will result in a lot of code that will never get optimized nicely.
  58. ;; Similarly letrectification is great for generated code quality, but
  59. ;; as it gives the compiler more to work with, it increases compile
  60. ;; time enough that we reserve it for -O2. Also, this makes -O1 avoid
  61. ;; assumptions about top-level values, in the same way that avoiding
  62. ;; resolve-primitives does.
  63. '((#:resolve-primitives? 2)
  64. (#:expand-primitives? 1)
  65. (#:letrectify? 2)
  66. (#:seal-private-bindings? 3)
  67. (#:partial-eval? 1)
  68. (#:eta-expand? 2)))