type-checks.scm 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014, 2015 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. ;;; Commentary:
  17. ;;;
  18. ;;; This pass kills dead expressions: code that has no side effects, and
  19. ;;; whose value is unused. It does so by marking all live values, and
  20. ;;; then discarding other values as dead. This happens recursively
  21. ;;; through procedures, so it should be possible to elide dead
  22. ;;; procedures as well.
  23. ;;;
  24. ;;; Code:
  25. (define-module (language cps type-checks)
  26. #:use-module (ice-9 match)
  27. #:use-module (language cps)
  28. #:use-module (language cps effects-analysis)
  29. #:use-module (language cps types)
  30. #:use-module (language cps intmap)
  31. #:export (elide-type-checks
  32. compute-effects/elide-type-checks))
  33. (define (elide-type-checks conts kfun effects)
  34. "Elide &type-check effects from EFFECTS for the function starting at
  35. KFUN where we can prove that no assertion will be raised at run-time."
  36. (let ((types (infer-types conts kfun)))
  37. (define (visit-primcall effects fx label name args)
  38. (if (primcall-types-check? types label name args)
  39. (intmap-replace! effects label (logand fx (lognot &type-check)))
  40. effects))
  41. (persistent-intmap
  42. (intmap-fold (lambda (label types effects)
  43. (let ((fx (intmap-ref effects label)))
  44. (cond
  45. ((causes-all-effects? fx) effects)
  46. ((causes-effect? fx &type-check)
  47. (match (intmap-ref conts label)
  48. (($ $kargs _ _ exp)
  49. (match exp
  50. (($ $continue k src ($ $primcall name args))
  51. (visit-primcall effects fx label name args))
  52. (($ $continue k src
  53. ($ $branch _ ($primcall name args)))
  54. (visit-primcall effects fx label name args))
  55. (_ effects)))
  56. (_ effects)))
  57. (else effects))))
  58. types
  59. effects))))
  60. (define (compute-effects/elide-type-checks conts)
  61. (intmap-fold (lambda (label cont effects)
  62. (match cont
  63. (($ $kfun) (elide-type-checks conts label effects))
  64. (_ effects)))
  65. conts
  66. (compute-effects conts)))