loop-instrumentation.scm 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2016, 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. ;;; A pass to add "instrument-loop" primcalls at loop headers.
  19. ;;;
  20. ;;; Code:
  21. (define-module (language cps loop-instrumentation)
  22. #:use-module (ice-9 match)
  23. #:use-module (language cps)
  24. #:use-module (language cps utils)
  25. #:use-module (language cps with-cps)
  26. #:use-module (language cps intmap)
  27. #:use-module (language cps intset)
  28. #:use-module (language cps renumber)
  29. #:export (add-loop-instrumentation))
  30. (define (compute-loop-headers cps)
  31. (define (maybe-add-header label k headers)
  32. "Add K to headers if it is a target of a backward branch."
  33. (if (<= k label)
  34. (intset-add! headers k)
  35. headers))
  36. (define (visit-cont label cont headers)
  37. (match cont
  38. (($ $kargs names vars ($ $continue k))
  39. (maybe-add-header label k headers))
  40. (($ $kargs names vars ($ $branch kf kt))
  41. (maybe-add-header label kf (maybe-add-header label kt headers)))
  42. (_ headers)))
  43. (persistent-intset (intmap-fold visit-cont cps empty-intset)))
  44. (define (add-loop-instrumentation cps)
  45. (define (add-instrumentation label cps)
  46. (match (intmap-ref cps label)
  47. (($ $kargs names vars term)
  48. (with-cps cps
  49. (letk k ($kargs () () ,term))
  50. (setk label
  51. ($kargs names vars
  52. ($continue k #f
  53. ($primcall 'instrument-loop #f ()))))))))
  54. (let* ((cps (renumber cps))
  55. (headers (compute-loop-headers cps)))
  56. (with-fresh-name-state cps
  57. (persistent-intmap (intset-fold add-instrumentation headers cps)))))