type-checks.scm 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014, 2015, 2017, 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. ;;; 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 param args)
  38. (if (primcall-types-check? types label name param 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 names vars
  49. ($ $continue k src ($ $primcall name param args)))
  50. (visit-primcall effects fx label name param args))
  51. (($ $kargs names vars
  52. ($ $branch kf kt src name param args))
  53. (visit-primcall effects fx label name param args))
  54. (_ effects)))
  55. (else effects))))
  56. types
  57. effects))))
  58. (define (compute-effects/elide-type-checks conts)
  59. (intmap-fold (lambda (label cont effects)
  60. (match cont
  61. (($ $kfun) (elide-type-checks conts label effects))
  62. (_ effects)))
  63. conts
  64. (compute-effects conts)))