optimize.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;;; Tree-il optimizer
  2. ;; Copyright (C) 2009, 2010-2015, 2018 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 primitives)
  20. #:use-module (language tree-il peval)
  21. #:use-module (language tree-il fix-letrec)
  22. #:use-module (language tree-il debug)
  23. #:use-module (ice-9 match)
  24. #:export (optimize
  25. tree-il-optimizations))
  26. (define (kw-arg-ref args kw default)
  27. (match (memq kw args)
  28. ((_ val . _) val)
  29. (_ default)))
  30. (define *debug?* #f)
  31. (define (maybe-verify x)
  32. (if *debug?*
  33. (verify-tree-il x)
  34. x))
  35. (define (optimize x env opts)
  36. (define-syntax-rule (run-pass pass kw default)
  37. (when (kw-arg-ref opts kw default)
  38. (set! x (maybe-verify (pass x)))))
  39. (define (resolve* x) (resolve-primitives x env))
  40. (define (peval* x) (peval x env))
  41. (maybe-verify x)
  42. (run-pass resolve* #:resolve-primitives? #t)
  43. (run-pass expand-primitives #:expand-primitives? #t)
  44. (run-pass peval* #:partial-eval? #t)
  45. (run-pass fix-letrec #:fix-letrec? #t)
  46. x)
  47. (define (tree-il-optimizations)
  48. ;; Avoid resolve-primitives until -O2, when CPS optimizations kick in.
  49. ;; Otherwise, inlining the primcalls during Tree-IL->CPS compilation
  50. ;; will result in a lot of code that will never get optimized nicely.
  51. '((#:resolve-primitives? 2)
  52. (#:expand-primitives? 1)
  53. (#:partial-eval? 1)
  54. (#:fix-letrec? 1)))