devirtualize-integers.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2017-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. ;;; Commentary:
  17. ;;;
  18. ;;; Some parts of programs operate on exact integers. An exact integer
  19. ;;; is either a fixnum or a bignum. It's often the case that if we know
  20. ;;; that a number is a fixnum, all operations on it can be unboxed in
  21. ;;; terms of s64 operations. But if there's a series of operations and
  22. ;;; each one works on either bignums or fixnums, then the mixing of
  23. ;;; fixnums and bignums through that one control and data flow path
  24. ;;; makes it impossible for the compiler to specialize operations to
  25. ;;; either type.
  26. ;;;
  27. ;;; This "integer devirtualization" pass tries to duplicate the control
  28. ;;; and data flow of exact integers into two flows: one for bignums and
  29. ;;; one for fixnums. This causes code growth, so it's something we need
  30. ;;; to be careful about.
  31. ;;;
  32. ;;; Code:
  33. (define-module (language cps devirtualize-integers)
  34. #:use-module (ice-9 match)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (language cps)
  37. #:use-module (language cps effects-analysis)
  38. #:use-module (language cps intmap)
  39. #:use-module (language cps intset)
  40. #:use-module (language cps utils)
  41. #:use-module (language cps with-cps)
  42. #:export (devirtualize-integers))
  43. ;; Compute a map from VAR -> COUNT, where COUNT indicates the number of
  44. ;; times in the source program that VAR is used.
  45. (define (compute-use-counts cps)
  46. (define (add-use use-counts var)
  47. (let ((count (1+ (intmap-ref use-counts var (lambda (_) 0)))))
  48. (intmap-add! use-counts var count (lambda (old new) new))))
  49. (define (add-uses use-counts vars)
  50. (match vars
  51. (() use-counts)
  52. ((var . vars) (add-uses (add-use use-counts var) vars))))
  53. (persistent-intmap
  54. (intmap-fold
  55. (lambda (label cont use-counts)
  56. (match cont
  57. (($ $kargs names vars term)
  58. (match term
  59. (($ $continue k src exp)
  60. (match exp
  61. ((or ($ $const) ($ $prim) ($ $fun) ($ $const-fun) ($ $code) ($ $rec))
  62. use-counts)
  63. (($ $values args)
  64. (add-uses use-counts args))
  65. (($ $call proc args)
  66. (add-uses (add-use use-counts proc) args))
  67. (($ $callk kfun proc args)
  68. (add-uses (if proc (add-use use-counts proc) use-counts) args))
  69. (($ $primcall name param args)
  70. (add-uses use-counts args))))
  71. (($ $branch kf kt src op param args)
  72. (add-uses use-counts args))
  73. (($ $prompt k kh src escape? tag)
  74. (add-use use-counts tag))
  75. (($ $throw src op param args)
  76. (add-uses use-counts args))))
  77. (_ use-counts)))
  78. cps
  79. (transient-intmap))))
  80. (define (bailout? cps label)
  81. (match (intmap-ref cps label)
  82. (($ $kargs _ _ ($ $throw)) #t)
  83. (_ #f)))
  84. (define (peel-trace cps label fx kexit use-counts)
  85. "For the graph starting at LABEL, try to peel out a trace that uses
  86. the variable FX. A peelable trace consists of effect-free terms, or
  87. terms that only have &type-check effect but which use FX or some
  88. variable that was defined using FX as an input. No variable defined in
  89. the trace should be referenced outside of it."
  90. (let peel-cont ((cps cps) (label label)
  91. (live-vars empty-intmap) ;; var -> pending refcount
  92. (fresh-vars empty-intmap) ;; old-name -> new name
  93. (vars-of-interest (intset-add empty-intset fx))
  94. (defs-of-interest? #f))
  95. (define (fail) (with-cps cps #f))
  96. (define (add-live-vars live-vars vars)
  97. (match vars
  98. (() live-vars)
  99. ((var . vars)
  100. (add-live-vars
  101. (let ((count (intmap-ref use-counts var (lambda (_) 0))))
  102. (if (zero? count)
  103. live-vars
  104. (intmap-add live-vars var count)))
  105. vars))))
  106. (define (subtract-uses live-vars vars)
  107. (match vars
  108. (() live-vars)
  109. ((var . vars)
  110. (subtract-uses
  111. (let ((count (intmap-ref live-vars var (lambda (_) #f))))
  112. (cond
  113. ((not count) live-vars)
  114. ((= count 1) (intmap-remove live-vars var))
  115. (else (intmap-replace live-vars var (1- count)))))
  116. vars))))
  117. (match (intmap-ref cps label)
  118. ;; We know the initial label is a $kargs, and we won't follow the
  119. ;; graph to get to $kreceive etc, so we can stop with these two
  120. ;; continuation kinds. (For our purposes, only $values can
  121. ;; continue to $ktail.)
  122. (($ $ktail) (fail))
  123. (($ $kargs names vars term)
  124. (let* ((vars-of-interest
  125. (if defs-of-interest?
  126. (fold1 (lambda (var set) (intset-add set var))
  127. vars vars-of-interest)
  128. vars-of-interest))
  129. (live-vars (add-live-vars live-vars vars))
  130. (fresh-vars (fold (lambda (var fresh-vars)
  131. (intmap-add fresh-vars var (fresh-var)))
  132. fresh-vars vars))
  133. (peeled-vars (map (lambda (var) (intmap-ref fresh-vars var))
  134. vars)))
  135. (define (rename-uses args)
  136. (map (lambda (arg) (intmap-ref fresh-vars arg (lambda (arg) arg)))
  137. args))
  138. (define (any-use-of-interest? args)
  139. (or-map (lambda (arg) (intset-ref vars-of-interest arg))
  140. args))
  141. (define (continue k live-vars defs-of-interest? can-terminate-trace?
  142. make-term)
  143. (define (stitch cps k)
  144. (with-cps cps
  145. (letk label* ($kargs names peeled-vars ,(make-term k)))
  146. label*))
  147. (define (terminate)
  148. (stitch cps k))
  149. (with-cps cps
  150. (let$ k* (peel-cont k live-vars fresh-vars vars-of-interest
  151. defs-of-interest?))
  152. ($ ((lambda (cps)
  153. (cond
  154. (k* (stitch cps k*))
  155. ((and can-terminate-trace? (eq? live-vars empty-intmap))
  156. (terminate))
  157. (else (fail))))))))
  158. (match term
  159. (($ $branch kf kt src op param args)
  160. ;; kt or k is kf; var of interest is in args
  161. (let* ((live-vars (subtract-uses live-vars args))
  162. (uses-of-interest? (any-use-of-interest? args))
  163. (defs-of-interest? #f) ;; Branches don't define values.
  164. (can-terminate-trace? uses-of-interest?)
  165. (peeled-args (rename-uses args)))
  166. (cond
  167. ((not uses-of-interest?)
  168. (fail))
  169. ((bailout? cps kt)
  170. (continue kf live-vars defs-of-interest? can-terminate-trace?
  171. (lambda (kf)
  172. (build-term
  173. ($branch kf kt src op param peeled-args)))))
  174. ((bailout? cps kf)
  175. (continue kt live-vars defs-of-interest? can-terminate-trace?
  176. (lambda (kt)
  177. (build-term
  178. ($branch kf kt src op param peeled-args)))))
  179. ((eq? live-vars empty-intmap)
  180. (with-cps cps
  181. (letk label*
  182. ($kargs names peeled-vars
  183. ($branch kf kt src op param peeled-args)))
  184. label*))
  185. (else
  186. (fail)))))
  187. (($ $continue k src exp)
  188. (match exp
  189. (($ $const)
  190. ;; fine.
  191. (continue k live-vars #f #f
  192. (lambda (k)
  193. (build-term ($continue k src ,exp)))))
  194. (($ $values args)
  195. (let ((uses-of-interest? (any-use-of-interest? args))
  196. (live-vars (subtract-uses live-vars args))
  197. (peeled-args (rename-uses args)))
  198. (continue k live-vars
  199. uses-of-interest? #f
  200. (lambda (k)
  201. (build-term
  202. ($continue k src ($values peeled-args)))))))
  203. (($ $primcall name param args)
  204. ;; exp is effect-free or var of interest in args
  205. (let* ((fx (expression-effects exp))
  206. (uses-of-interest? (any-use-of-interest? args))
  207. (live-vars (subtract-uses live-vars args))
  208. (peeled-args (rename-uses args)))
  209. ;; If the primcall uses a value of interest,
  210. ;; consider it for peeling even if it would cause a
  211. ;; type check; perhaps the peeling causes the type
  212. ;; check to go away.
  213. (if (or (eqv? fx &no-effects)
  214. (and uses-of-interest? (eqv? fx &type-check)))
  215. (continue k live-vars
  216. ;; Primcalls that use values of interest
  217. ;; define values of interest.
  218. uses-of-interest? #t
  219. (lambda (k)
  220. (build-term
  221. ($continue k src
  222. ($primcall name param ,peeled-args)))))
  223. (fail))))
  224. (_ (fail))))))))))
  225. (define (peel-traces-in-function cps body use-counts)
  226. (intset-fold
  227. (lambda (label cps)
  228. (match (intmap-ref cps label)
  229. ;; Traces start with a fixnum? predicate. We could expand this
  230. ;; in the future if we wanted to.
  231. (($ $kargs names vars ($ $branch kf kt src 'fixnum? #f (x)))
  232. (if (and (bailout? cps kf) #f)
  233. ;; Don't peel traces whose alternate is just a bailout.
  234. cps
  235. (with-cps cps
  236. (let$ kt (peel-trace kt x kf use-counts))
  237. ($ ((lambda (cps)
  238. (if kt
  239. (with-cps cps
  240. (setk label
  241. ($kargs names vars
  242. ($branch kf kt src 'fixnum? #f (x)))))
  243. cps)))))))
  244. (_ cps)))
  245. body
  246. cps))
  247. (define (devirtualize-integers cps)
  248. (let ((use-counts (compute-use-counts cps)))
  249. (with-fresh-name-state cps
  250. (intmap-fold
  251. (lambda (kfun body cps)
  252. (peel-traces-in-function cps body use-counts))
  253. (compute-reachable-functions cps)
  254. cps))))