optimize.scm 2.8 KB

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