peval.scm 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. ;;; Tree-IL partial evaluator
  2. ;; Copyright (C) 2011-2014, 2017 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. (define-module (language tree-il peval)
  17. #:use-module (language tree-il)
  18. #:use-module (language tree-il primitives)
  19. #:use-module (language tree-il effects)
  20. #:use-module (ice-9 vlist)
  21. #:use-module (ice-9 match)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 control)
  27. #:export (peval))
  28. ;;;
  29. ;;; Partial evaluation is Guile's most important source-to-source
  30. ;;; optimization pass. It performs copy propagation, dead code
  31. ;;; elimination, inlining, and constant folding, all while preserving
  32. ;;; the order of effects in the residual program.
  33. ;;;
  34. ;;; For more on partial evaluation, see William Cook’s excellent
  35. ;;; tutorial on partial evaluation at DSL 2011, called “Build your own
  36. ;;; partial evaluator in 90 minutes”[0].
  37. ;;;
  38. ;;; Our implementation of this algorithm was heavily influenced by
  39. ;;; Waddell and Dybvig's paper, "Fast and Effective Procedure Inlining",
  40. ;;; IU CS Dept. TR 484.
  41. ;;;
  42. ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
  43. ;;;
  44. ;; First, some helpers.
  45. ;;
  46. (define-syntax *logging* (identifier-syntax #f))
  47. ;; For efficiency we define *logging* to inline to #f, so that the call
  48. ;; to log* gets optimized out. If you want to log, uncomment these
  49. ;; lines:
  50. ;;
  51. ;; (define %logging #f)
  52. ;; (define-syntax *logging* (identifier-syntax %logging))
  53. ;;
  54. ;; Then you can change %logging at runtime.
  55. (define-syntax log
  56. (syntax-rules (quote)
  57. ((log 'event arg ...)
  58. (if (and *logging*
  59. (or (eq? *logging* #t)
  60. (memq 'event *logging*)))
  61. (log* 'event arg ...)))))
  62. (define (log* event . args)
  63. (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
  64. 'pretty-print)))
  65. (pp `(log ,event . ,args))
  66. (newline)
  67. (values)))
  68. (define (tree-il-any proc exp)
  69. (let/ec k
  70. (tree-il-fold (lambda (exp res)
  71. (let ((res (proc exp)))
  72. (if res (k res) #f)))
  73. (lambda (exp res) #f)
  74. #f exp)))
  75. (define (vlist-any proc vlist)
  76. (let ((len (vlist-length vlist)))
  77. (let lp ((i 0))
  78. (and (< i len)
  79. (or (proc (vlist-ref vlist i))
  80. (lp (1+ i)))))))
  81. (define (singly-valued-expression? exp)
  82. (match exp
  83. (($ <const>) #t)
  84. (($ <void>) #t)
  85. (($ <lexical-ref>) #t)
  86. (($ <primitive-ref>) #t)
  87. (($ <module-ref>) #t)
  88. (($ <toplevel-ref>) #t)
  89. (($ <primcall> _ (? singly-valued-primitive?)) #t)
  90. (($ <primcall> _ 'values (val)) #t)
  91. (($ <lambda>) #t)
  92. (($ <conditional> _ test consequent alternate)
  93. (and (singly-valued-expression? consequent)
  94. (singly-valued-expression? alternate)))
  95. (else #f)))
  96. (define (truncate-values x)
  97. "Discard all but the first value of X."
  98. (if (singly-valued-expression? x)
  99. x
  100. (make-primcall (tree-il-src x) 'values (list x))))
  101. ;; Peval will do a one-pass analysis on the source program to determine
  102. ;; the set of assigned lexicals, and to identify unreferenced and
  103. ;; singly-referenced lexicals.
  104. ;;
  105. (define-record-type <var>
  106. (make-var name gensym refcount set?)
  107. var?
  108. (name var-name)
  109. (gensym var-gensym)
  110. (refcount var-refcount set-var-refcount!)
  111. (set? var-set? set-var-set?!))
  112. (define* (build-var-table exp #:optional (table vlist-null))
  113. (tree-il-fold
  114. (lambda (exp res)
  115. (match exp
  116. (($ <lexical-ref> src name gensym)
  117. (let ((var (cdr (vhash-assq gensym res))))
  118. (set-var-refcount! var (1+ (var-refcount var)))
  119. res))
  120. (($ <lambda-case> src req opt rest kw init gensyms body alt)
  121. (fold (lambda (name sym res)
  122. (vhash-consq sym (make-var name sym 0 #f) res))
  123. res
  124. (append req (or opt '()) (if rest (list rest) '())
  125. (match kw
  126. ((aok? (kw name sym) ...) name)
  127. (_ '())))
  128. gensyms))
  129. (($ <let> src names gensyms vals body)
  130. (fold (lambda (name sym res)
  131. (vhash-consq sym (make-var name sym 0 #f) res))
  132. res names gensyms))
  133. (($ <letrec> src in-order? names gensyms vals body)
  134. (fold (lambda (name sym res)
  135. (vhash-consq sym (make-var name sym 0 #f) res))
  136. res names gensyms))
  137. (($ <fix> src names gensyms vals body)
  138. (fold (lambda (name sym res)
  139. (vhash-consq sym (make-var name sym 0 #f) res))
  140. res names gensyms))
  141. (($ <lexical-set> src name gensym exp)
  142. (set-var-set?! (cdr (vhash-assq gensym res)) #t)
  143. res)
  144. (_ res)))
  145. (lambda (exp res) res)
  146. table exp))
  147. ;; Counters are data structures used to limit the effort that peval
  148. ;; spends on particular inlining attempts. Each call site in the source
  149. ;; program is allocated some amount of effort. If peval exceeds the
  150. ;; effort counter while attempting to inline a call site, it aborts the
  151. ;; inlining attempt and residualizes a call instead.
  152. ;;
  153. ;; As there is a fixed number of call sites, that makes `peval' O(N) in
  154. ;; the number of call sites in the source program.
  155. ;;
  156. ;; Counters should limit the size of the residual program as well, but
  157. ;; currently this is not implemented.
  158. ;;
  159. ;; At the top level, before seeing any peval call, there is no counter,
  160. ;; because inlining will terminate as there is no recursion. When peval
  161. ;; sees a call at the top level, it will make a new counter, allocating
  162. ;; it some amount of effort and size.
  163. ;;
  164. ;; This top-level effort counter effectively "prints money". Within a
  165. ;; toplevel counter, no more effort is printed ex nihilo; for a nested
  166. ;; inlining attempt to proceed, effort must be transferred from the
  167. ;; toplevel counter to the nested counter.
  168. ;;
  169. ;; Via `data' and `prev', counters form a linked list, terminating in a
  170. ;; toplevel counter. In practice `data' will be the a pointer to the
  171. ;; source expression of the procedure being inlined.
  172. ;;
  173. ;; In this way peval can detect a recursive inlining attempt, by walking
  174. ;; back on the `prev' links looking for matching `data'. Recursive
  175. ;; counters receive a more limited effort allocation, as we don't want
  176. ;; to spend all of the effort for a toplevel inlining site on loops.
  177. ;; Also, recursive counters don't need a prompt at each inlining site:
  178. ;; either the call chain folds entirely, or it will be residualized at
  179. ;; its original call.
  180. ;;
  181. (define-record-type <counter>
  182. (%make-counter effort size continuation recursive? data prev)
  183. counter?
  184. (effort effort-counter)
  185. (size size-counter)
  186. (continuation counter-continuation)
  187. (recursive? counter-recursive? set-counter-recursive?!)
  188. (data counter-data)
  189. (prev counter-prev))
  190. (define (abort-counter c)
  191. ((counter-continuation c)))
  192. (define (record-effort! c)
  193. (let ((e (effort-counter c)))
  194. (if (zero? (variable-ref e))
  195. (abort-counter c)
  196. (variable-set! e (1- (variable-ref e))))))
  197. (define (record-size! c)
  198. (let ((s (size-counter c)))
  199. (if (zero? (variable-ref s))
  200. (abort-counter c)
  201. (variable-set! s (1- (variable-ref s))))))
  202. (define (find-counter data counter)
  203. (and counter
  204. (if (eq? data (counter-data counter))
  205. counter
  206. (find-counter data (counter-prev counter)))))
  207. (define* (transfer! from to #:optional
  208. (effort (variable-ref (effort-counter from)))
  209. (size (variable-ref (size-counter from))))
  210. (define (transfer-counter! from-v to-v amount)
  211. (let* ((from-balance (variable-ref from-v))
  212. (to-balance (variable-ref to-v))
  213. (amount (min amount from-balance)))
  214. (variable-set! from-v (- from-balance amount))
  215. (variable-set! to-v (+ to-balance amount))))
  216. (transfer-counter! (effort-counter from) (effort-counter to) effort)
  217. (transfer-counter! (size-counter from) (size-counter to) size))
  218. (define (make-top-counter effort-limit size-limit continuation data)
  219. (%make-counter (make-variable effort-limit)
  220. (make-variable size-limit)
  221. continuation
  222. #t
  223. data
  224. #f))
  225. (define (make-nested-counter continuation data current)
  226. (let ((c (%make-counter (make-variable 0)
  227. (make-variable 0)
  228. continuation
  229. #f
  230. data
  231. current)))
  232. (transfer! current c)
  233. c))
  234. (define (make-recursive-counter effort-limit size-limit orig current)
  235. (let ((c (%make-counter (make-variable 0)
  236. (make-variable 0)
  237. (counter-continuation orig)
  238. #t
  239. (counter-data orig)
  240. current)))
  241. (transfer! current c effort-limit size-limit)
  242. c))
  243. ;; Operand structures allow bindings to be processed lazily instead of
  244. ;; eagerly. By doing so, hopefully we can get process them in a way
  245. ;; appropriate to their use contexts. Operands also prevent values from
  246. ;; being visited multiple times, wasting effort.
  247. ;;
  248. ;; TODO: Record value size in operand structure?
  249. ;;
  250. (define-record-type <operand>
  251. (%make-operand var sym visit source visit-count use-count
  252. copyable? residual-value constant-value alias)
  253. operand?
  254. (var operand-var)
  255. (sym operand-sym)
  256. (visit %operand-visit)
  257. (source operand-source)
  258. (visit-count operand-visit-count set-operand-visit-count!)
  259. (use-count operand-use-count set-operand-use-count!)
  260. (copyable? operand-copyable? set-operand-copyable?!)
  261. (residual-value operand-residual-value %set-operand-residual-value!)
  262. (constant-value operand-constant-value set-operand-constant-value!)
  263. (alias operand-alias set-operand-alias!))
  264. (define* (make-operand var sym #:optional source visit alias)
  265. ;; Bind SYM to VAR, with value SOURCE. Unassigned bound operands are
  266. ;; considered copyable until we prove otherwise. If we have a source
  267. ;; expression, truncate it to one value. Copy propagation does not
  268. ;; work on multiply-valued expressions.
  269. (let ((source (and=> source truncate-values)))
  270. (%make-operand var sym visit source 0 0
  271. (and source (not (var-set? var))) #f #f
  272. (and (not (var-set? var)) alias))))
  273. (define* (make-bound-operands vars syms sources visit #:optional aliases)
  274. (if aliases
  275. (map (lambda (name sym source alias)
  276. (make-operand name sym source visit alias))
  277. vars syms sources aliases)
  278. (map (lambda (name sym source)
  279. (make-operand name sym source visit #f))
  280. vars syms sources)))
  281. (define (make-unbound-operands vars syms)
  282. (map make-operand vars syms))
  283. (define (set-operand-residual-value! op val)
  284. (%set-operand-residual-value!
  285. op
  286. (match val
  287. (($ <primcall> src 'values (first))
  288. ;; The continuation of a residualized binding does not need the
  289. ;; introduced `values' node, so undo the effects of truncation.
  290. first)
  291. (else
  292. val))))
  293. (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
  294. ;; Peval is O(N) in call sites of the source program. However,
  295. ;; visiting an operand can introduce new call sites. If we visit an
  296. ;; operand outside a counter -- i.e., outside an inlining attempt --
  297. ;; this can lead to divergence. So, if we are visiting an operand to
  298. ;; try to copy it, and there is no counter, make a new one.
  299. ;;
  300. ;; This will only happen at most as many times as there are lexical
  301. ;; references in the source program.
  302. (and (zero? (operand-visit-count op))
  303. (dynamic-wind
  304. (lambda ()
  305. (set-operand-visit-count! op (1+ (operand-visit-count op))))
  306. (lambda ()
  307. (and (operand-source op)
  308. (if (or counter (and (not effort-limit) (not size-limit)))
  309. ((%operand-visit op) (operand-source op) counter ctx)
  310. (let/ec k
  311. (define (abort)
  312. ;; If we abort when visiting the value in a
  313. ;; fresh context, we won't succeed in any future
  314. ;; attempt, so don't try to copy it again.
  315. (set-operand-copyable?! op #f)
  316. (k #f))
  317. ((%operand-visit op)
  318. (operand-source op)
  319. (make-top-counter effort-limit size-limit abort op)
  320. ctx)))))
  321. (lambda ()
  322. (set-operand-visit-count! op (1- (operand-visit-count op)))))))
  323. ;; A helper for constant folding.
  324. ;;
  325. (define (types-check? primitive-name args)
  326. (case primitive-name
  327. ((values) #t)
  328. ((not pair? null? list? symbol? vector? struct?)
  329. (= (length args) 1))
  330. ((eq? eqv? equal?)
  331. (= (length args) 2))
  332. ;; FIXME: add more cases?
  333. (else #f)))
  334. (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
  335. #:key
  336. (operator-size-limit 40)
  337. (operand-size-limit 20)
  338. (value-size-limit 10)
  339. (effort-limit 500)
  340. (recursive-effort-limit 100))
  341. "Partially evaluate EXP in compilation environment CENV, with
  342. top-level bindings from ENV and return the resulting expression."
  343. ;; This is a simple partial evaluator. It effectively performs
  344. ;; constant folding, copy propagation, dead code elimination, and
  345. ;; inlining.
  346. ;; TODO:
  347. ;;
  348. ;; Propagate copies across toplevel bindings, if we can prove the
  349. ;; bindings to be immutable.
  350. ;;
  351. ;; Specialize lambda expressions with invariant arguments.
  352. (define local-toplevel-env
  353. ;; The top-level environment of the module being compiled.
  354. (let ()
  355. (define (env-folder x env)
  356. (match x
  357. (($ <toplevel-define> _ name)
  358. (vhash-consq name #t env))
  359. (($ <seq> _ head tail)
  360. (env-folder tail (env-folder head env)))
  361. (_ env)))
  362. (env-folder exp vlist-null)))
  363. (define (local-toplevel? name)
  364. (vhash-assq name local-toplevel-env))
  365. ;; gensym -> <var>
  366. ;; renamed-term -> original-term
  367. ;;
  368. (define store (build-var-table exp))
  369. (define (record-new-temporary! name sym refcount)
  370. (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
  371. (define (lookup-var sym)
  372. (let ((v (vhash-assq sym store)))
  373. (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
  374. (define (fresh-gensyms vars)
  375. (map (lambda (var)
  376. (let ((new (gensym (string-append (symbol->string (var-name var))
  377. " "))))
  378. (set! store (vhash-consq new var store))
  379. new))
  380. vars))
  381. (define (fresh-temporaries ls)
  382. (map (lambda (elt)
  383. (let ((new (gensym "tmp ")))
  384. (record-new-temporary! 'tmp new 1)
  385. new))
  386. ls))
  387. (define (assigned-lexical? sym)
  388. (var-set? (lookup-var sym)))
  389. (define (lexical-refcount sym)
  390. (var-refcount (lookup-var sym)))
  391. (define (with-temporaries src exps refcount can-copy? k)
  392. (let* ((pairs (map (match-lambda
  393. ((and exp (? can-copy?))
  394. (cons #f exp))
  395. (exp
  396. (let ((sym (gensym "tmp ")))
  397. (record-new-temporary! 'tmp sym refcount)
  398. (cons sym exp))))
  399. exps))
  400. (tmps (filter car pairs)))
  401. (match tmps
  402. (() (k exps))
  403. (tmps
  404. (make-let src
  405. (make-list (length tmps) 'tmp)
  406. (map car tmps)
  407. (map cdr tmps)
  408. (k (map (match-lambda
  409. ((#f . val) val)
  410. ((sym . _)
  411. (make-lexical-ref #f 'tmp sym)))
  412. pairs)))))))
  413. (define (make-begin0 src first second)
  414. (make-let-values
  415. src
  416. first
  417. (let ((vals (gensym "vals ")))
  418. (record-new-temporary! 'vals vals 1)
  419. (make-lambda-case
  420. #f
  421. '() #f 'vals #f '() (list vals)
  422. (make-seq
  423. src
  424. second
  425. (make-primcall #f 'apply
  426. (list
  427. (make-primitive-ref #f 'values)
  428. (make-lexical-ref #f 'vals vals))))
  429. #f))))
  430. ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
  431. ;; from it to ORIG.
  432. ;;
  433. (define (record-source-expression! orig new)
  434. (set! store (vhash-consq new (source-expression orig) store))
  435. new)
  436. ;; Find the source expression corresponding to NEW. Used to detect
  437. ;; recursive inlining attempts.
  438. ;;
  439. (define (source-expression new)
  440. (let ((x (vhash-assq new store)))
  441. (if x (cdr x) new)))
  442. (define (record-operand-use op)
  443. (set-operand-use-count! op (1+ (operand-use-count op))))
  444. (define (unrecord-operand-uses op n)
  445. (let ((count (- (operand-use-count op) n)))
  446. (when (zero? count)
  447. (set-operand-residual-value! op #f))
  448. (set-operand-use-count! op count)))
  449. (define* (residualize-lexical op #:optional ctx val)
  450. (log 'residualize op)
  451. (record-operand-use op)
  452. (if (memq ctx '(value values))
  453. (set-operand-residual-value! op val))
  454. (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
  455. (define (fold-constants src name args ctx)
  456. (define (apply-primitive name args)
  457. ;; todo: further optimize commutative primitives
  458. (catch #t
  459. (lambda ()
  460. (call-with-values
  461. (lambda ()
  462. (case name
  463. ((eq? eqv?)
  464. ;; Constants will be deduplicated later, but eq?
  465. ;; folding can happen now. Anticipate the
  466. ;; deduplication by using equal? instead of eq?.
  467. ;; Same for eqv?.
  468. (apply equal? args))
  469. (else
  470. (apply (module-ref the-scm-module name) args))))
  471. (lambda results
  472. (values #t results))))
  473. (lambda _
  474. (values #f '()))))
  475. (define (make-values src values)
  476. (match values
  477. ((single) single) ; 1 value
  478. ((_ ...) ; 0, or 2 or more values
  479. (make-primcall src 'values values))))
  480. (define (residualize-call)
  481. (make-primcall src name args))
  482. (cond
  483. ((every const? args)
  484. (let-values (((success? values)
  485. (apply-primitive name (map const-exp args))))
  486. (log 'fold success? values name args)
  487. (if success?
  488. (case ctx
  489. ((effect) (make-void src))
  490. ((test)
  491. ;; Values truncation: only take the first
  492. ;; value.
  493. (if (pair? values)
  494. (make-const src (car values))
  495. (make-values src '())))
  496. (else
  497. (make-values src (map (cut make-const src <>) values))))
  498. (residualize-call))))
  499. ((and (eq? ctx 'effect) (types-check? name args))
  500. (make-void #f))
  501. (else
  502. (residualize-call))))
  503. (define (inline-values src exp nmin nmax consumer)
  504. (let loop ((exp exp))
  505. (match exp
  506. ;; Some expression types are always singly-valued.
  507. ((or ($ <const>)
  508. ($ <void>)
  509. ($ <lambda>)
  510. ($ <lexical-ref>)
  511. ($ <toplevel-ref>)
  512. ($ <module-ref>)
  513. ($ <primitive-ref>)
  514. ($ <lexical-set>) ; FIXME: these set! expressions
  515. ($ <toplevel-set>) ; could return zero values in
  516. ($ <toplevel-define>) ; the future
  517. ($ <module-set>) ;
  518. ($ <primcall> src (? singly-valued-primitive?)))
  519. (and (<= nmin 1) (or (not nmax) (>= nmax 1))
  520. (make-call src (make-lambda #f '() consumer) (list exp))))
  521. ;; Statically-known number of values.
  522. (($ <primcall> src 'values vals)
  523. (and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals)))
  524. (make-call src (make-lambda #f '() consumer) vals)))
  525. ;; Not going to copy code into both branches.
  526. (($ <conditional>) #f)
  527. ;; Bail on other applications.
  528. (($ <call>) #f)
  529. (($ <primcall>) #f)
  530. ;; Bail on prompt and abort.
  531. (($ <prompt>) #f)
  532. (($ <abort>) #f)
  533. ;; Propagate to tail positions.
  534. (($ <let> src names gensyms vals body)
  535. (let ((body (loop body)))
  536. (and body
  537. (make-let src names gensyms vals body))))
  538. (($ <letrec> src in-order? names gensyms vals body)
  539. (let ((body (loop body)))
  540. (and body
  541. (make-letrec src in-order? names gensyms vals body))))
  542. (($ <fix> src names gensyms vals body)
  543. (let ((body (loop body)))
  544. (and body
  545. (make-fix src names gensyms vals body))))
  546. (($ <let-values> src exp
  547. ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
  548. (let ((body (loop body)))
  549. (and body
  550. (make-let-values src exp
  551. (make-lambda-case src2 req opt rest kw
  552. inits gensyms body #f)))))
  553. (($ <seq> src head tail)
  554. (let ((tail (loop tail)))
  555. (and tail (make-seq src head tail)))))))
  556. (define compute-effects
  557. (make-effects-analyzer assigned-lexical?))
  558. (define (constant-expression? x)
  559. ;; Return true if X is constant, for the purposes of copying or
  560. ;; elision---i.e., if it is known to have no effects, does not
  561. ;; allocate storage for a mutable object, and does not access
  562. ;; mutable data (like `car' or toplevel references).
  563. (constant? (compute-effects x)))
  564. (define (prune-bindings ops in-order? body counter ctx build-result)
  565. ;; This helper handles both `let' and `letrec'/`fix'. In the latter
  566. ;; cases we need to make sure that if referenced binding A needs
  567. ;; as-yet-unreferenced binding B, that B is processed for value.
  568. ;; Likewise if C, when processed for effect, needs otherwise
  569. ;; unreferenced D, then D needs to be processed for value too.
  570. ;;
  571. (define (referenced? op)
  572. ;; When we visit lambdas in operator context, we just copy them,
  573. ;; as we will process their body later. However this does have
  574. ;; the problem that any free var referenced by the lambda is not
  575. ;; marked as needing residualization. Here we hack around this
  576. ;; and treat all bindings as referenced if we are in operator
  577. ;; context.
  578. (or (eq? ctx 'operator)
  579. (not (zero? (operand-use-count op)))))
  580. ;; values := (op ...)
  581. ;; effects := (op ...)
  582. (define (residualize values effects)
  583. ;; Note, values and effects are reversed.
  584. (cond
  585. (in-order?
  586. (let ((values (filter operand-residual-value ops)))
  587. (if (null? values)
  588. body
  589. (build-result (map (compose var-name operand-var) values)
  590. (map operand-sym values)
  591. (map operand-residual-value values)
  592. body))))
  593. (else
  594. (let ((body
  595. (if (null? effects)
  596. body
  597. (let ((effect-vals (map operand-residual-value effects)))
  598. (list->seq #f (reverse (cons body effect-vals)))))))
  599. (if (null? values)
  600. body
  601. (let ((values (reverse values)))
  602. (build-result (map (compose var-name operand-var) values)
  603. (map operand-sym values)
  604. (map operand-residual-value values)
  605. body)))))))
  606. ;; old := (bool ...)
  607. ;; values := (op ...)
  608. ;; effects := ((op . value) ...)
  609. (let prune ((old (map referenced? ops)) (values '()) (effects '()))
  610. (let lp ((ops* ops) (values values) (effects effects))
  611. (cond
  612. ((null? ops*)
  613. (let ((new (map referenced? ops)))
  614. (if (not (equal? new old))
  615. (prune new values '())
  616. (residualize values
  617. (map (lambda (op val)
  618. (set-operand-residual-value! op val)
  619. op)
  620. (map car effects) (map cdr effects))))))
  621. (else
  622. (let ((op (car ops*)))
  623. (cond
  624. ((memq op values)
  625. (lp (cdr ops*) values effects))
  626. ((operand-residual-value op)
  627. (lp (cdr ops*) (cons op values) effects))
  628. ((referenced? op)
  629. (set-operand-residual-value! op (visit-operand op counter 'value))
  630. (lp (cdr ops*) (cons op values) effects))
  631. (else
  632. (lp (cdr ops*)
  633. values
  634. (let ((effect (visit-operand op counter 'effect)))
  635. (if (void? effect)
  636. effects
  637. (acons op effect effects))))))))))))
  638. (define (small-expression? x limit)
  639. (let/ec k
  640. (tree-il-fold
  641. (lambda (x res) ; down
  642. (1+ res))
  643. (lambda (x res) ; up
  644. (if (< res limit)
  645. res
  646. (k #f)))
  647. 0 x)
  648. #t))
  649. (define (extend-env sym op env)
  650. (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
  651. (let loop ((exp exp)
  652. (env vlist-null) ; vhash of gensym -> <operand>
  653. (counter #f) ; inlined call stack
  654. (ctx 'values)) ; effect, value, values, test, operator, or call
  655. (define (lookup var)
  656. (cond
  657. ((vhash-assq var env) => cdr)
  658. (else (error "unbound var" var))))
  659. ;; Find a value referenced a specific number of times. This is a hack
  660. ;; that's used for propagating fresh data structures like rest lists and
  661. ;; prompt tags. Usually we wouldn't copy consed data, but we can do so in
  662. ;; some special cases like `apply' or prompts if we can account
  663. ;; for all of its uses.
  664. ;;
  665. ;; You don't want to use this in general because it introduces a slight
  666. ;; nonlinearity by running peval again (though with a small effort and size
  667. ;; counter).
  668. ;;
  669. (define (find-definition x n-aliases)
  670. (cond
  671. ((lexical-ref? x)
  672. (cond
  673. ((lookup (lexical-ref-gensym x))
  674. => (lambda (op)
  675. (if (var-set? (operand-var op))
  676. (values #f #f)
  677. (let ((y (or (operand-residual-value op)
  678. (visit-operand op counter 'value 10 10)
  679. (operand-source op))))
  680. (cond
  681. ((and (lexical-ref? y)
  682. (= (lexical-refcount (lexical-ref-gensym x)) 1))
  683. ;; X is a simple alias for Y. Recurse, regardless of
  684. ;; the number of aliases we were expecting.
  685. (find-definition y n-aliases))
  686. ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
  687. ;; We found a definition that is aliased the right
  688. ;; number of times. We still recurse in case it is a
  689. ;; lexical.
  690. (values (find-definition y 1)
  691. op))
  692. (else
  693. ;; We can't account for our aliases.
  694. (values #f #f)))))))
  695. (else
  696. ;; A formal parameter. Can't say anything about that.
  697. (values #f #f))))
  698. ((= n-aliases 1)
  699. ;; Not a lexical: success, but only if we are looking for an
  700. ;; unaliased value.
  701. (values x #f))
  702. (else (values #f #f))))
  703. (define (visit exp ctx)
  704. (loop exp env counter ctx))
  705. (define (for-value exp) (visit exp 'value))
  706. (define (for-values exp) (visit exp 'values))
  707. (define (for-test exp) (visit exp 'test))
  708. (define (for-effect exp) (visit exp 'effect))
  709. (define (for-call exp) (visit exp 'call))
  710. (define (for-tail exp) (visit exp ctx))
  711. (if counter
  712. (record-effort! counter))
  713. (log 'visit ctx (and=> counter effort-counter)
  714. (unparse-tree-il exp))
  715. (match exp
  716. (($ <const>)
  717. (case ctx
  718. ((effect) (make-void #f))
  719. (else exp)))
  720. (($ <void>)
  721. (case ctx
  722. ((test) (make-const #f #t))
  723. (else exp)))
  724. (($ <lexical-ref> _ _ gensym)
  725. (log 'begin-copy gensym)
  726. (let lp ((op (lookup gensym)))
  727. (cond
  728. ((eq? ctx 'effect)
  729. (log 'lexical-for-effect gensym)
  730. (make-void #f))
  731. ((operand-alias op)
  732. ;; This is an unassigned operand that simply aliases some
  733. ;; other operand. Recurse to avoid residualizing the leaf
  734. ;; binding.
  735. => lp)
  736. ((eq? ctx 'call)
  737. ;; Don't propagate copies if we are residualizing a call.
  738. (log 'residualize-lexical-call gensym op)
  739. (residualize-lexical op))
  740. ((var-set? (operand-var op))
  741. ;; Assigned lexicals don't copy-propagate.
  742. (log 'assigned-var gensym op)
  743. (residualize-lexical op))
  744. ((not (operand-copyable? op))
  745. ;; We already know that this operand is not copyable.
  746. (log 'not-copyable gensym op)
  747. (residualize-lexical op))
  748. ((and=> (operand-constant-value op)
  749. (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
  750. ;; A cache hit.
  751. (let ((val (operand-constant-value op)))
  752. (log 'memoized-constant gensym val)
  753. (for-tail val)))
  754. ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
  755. recursive-effort-limit operand-size-limit)
  756. =>
  757. ;; If we end up deciding to residualize this value instead of
  758. ;; copying it, save that residualized value.
  759. (lambda (val)
  760. (cond
  761. ((not (constant-expression? val))
  762. (log 'not-constant gensym op)
  763. ;; At this point, ctx is operator, test, or value. A
  764. ;; value that is non-constant in one context will be
  765. ;; non-constant in the others, so it's safe to record
  766. ;; that here, and avoid future visits.
  767. (set-operand-copyable?! op #f)
  768. (residualize-lexical op ctx val))
  769. ((or (const? val)
  770. (void? val)
  771. (primitive-ref? val))
  772. ;; Always propagate simple values that cannot lead to
  773. ;; code bloat.
  774. (log 'copy-simple gensym val)
  775. ;; It could be this constant is the result of folding.
  776. ;; If that is the case, cache it. This helps loop
  777. ;; unrolling get farther.
  778. (if (or (eq? ctx 'value) (eq? ctx 'values))
  779. (begin
  780. (log 'memoize-constant gensym val)
  781. (set-operand-constant-value! op val)))
  782. val)
  783. ((= 1 (var-refcount (operand-var op)))
  784. ;; Always propagate values referenced only once.
  785. (log 'copy-single gensym val)
  786. val)
  787. ;; FIXME: do demand-driven size accounting rather than
  788. ;; these heuristics.
  789. ((eq? ctx 'operator)
  790. ;; A pure expression in the operator position. Inline
  791. ;; if it's a lambda that's small enough.
  792. (if (and (lambda? val)
  793. (small-expression? val operator-size-limit))
  794. (begin
  795. (log 'copy-operator gensym val)
  796. val)
  797. (begin
  798. (log 'too-big-for-operator gensym val)
  799. (residualize-lexical op ctx val))))
  800. (else
  801. ;; A pure expression, processed for call or for value.
  802. ;; Don't inline lambdas, because they will probably won't
  803. ;; fold because we don't know the operator.
  804. (if (and (small-expression? val value-size-limit)
  805. (not (tree-il-any lambda? val)))
  806. (begin
  807. (log 'copy-value gensym val)
  808. val)
  809. (begin
  810. (log 'too-big-or-has-lambda gensym val)
  811. (residualize-lexical op ctx val)))))))
  812. (else
  813. ;; Visit failed. Either the operand isn't bound, as in
  814. ;; lambda formal parameters, or the copy was aborted.
  815. (log 'unbound-or-aborted gensym op)
  816. (residualize-lexical op)))))
  817. (($ <lexical-set> src name gensym exp)
  818. (let ((op (lookup gensym)))
  819. (if (zero? (var-refcount (operand-var op)))
  820. (let ((exp (for-effect exp)))
  821. (if (void? exp)
  822. exp
  823. (make-seq src exp (make-void #f))))
  824. (begin
  825. (record-operand-use op)
  826. (make-lexical-set src name (operand-sym op) (for-value exp))))))
  827. (($ <let> src
  828. (names ... rest)
  829. (gensyms ... rest-sym)
  830. (vals ... ($ <primcall> _ 'list rest-args))
  831. ($ <primcall> asrc 'apply
  832. (proc args ...
  833. ($ <lexical-ref> _
  834. (? (cut eq? <> rest))
  835. (? (lambda (sym)
  836. (and (eq? sym rest-sym)
  837. (= (lexical-refcount sym) 1))))))))
  838. (let* ((tmps (make-list (length rest-args) 'tmp))
  839. (tmp-syms (fresh-temporaries tmps)))
  840. (for-tail
  841. (make-let src
  842. (append names tmps)
  843. (append gensyms tmp-syms)
  844. (append vals rest-args)
  845. (make-call
  846. asrc
  847. proc
  848. (append args
  849. (map (cut make-lexical-ref #f <> <>)
  850. tmps tmp-syms)))))))
  851. (($ <let> src names gensyms vals body)
  852. (define (lookup-alias exp)
  853. ;; It's very common for macros to introduce something like:
  854. ;;
  855. ;; ((lambda (x y) ...) x-exp y-exp)
  856. ;;
  857. ;; In that case you might end up trying to inline something like:
  858. ;;
  859. ;; (let ((x x-exp) (y y-exp)) ...)
  860. ;;
  861. ;; But if x-exp is itself a lexical-ref that aliases some much
  862. ;; larger expression, perhaps it will fail to inline due to
  863. ;; size. However we don't want to introduce a useless alias
  864. ;; (in this case, x). So if the RHS of a let expression is a
  865. ;; lexical-ref, we record that expression. If we end up having
  866. ;; to residualize X, then instead we residualize X-EXP, as long
  867. ;; as it isn't assigned.
  868. ;;
  869. (match exp
  870. (($ <lexical-ref> _ _ sym)
  871. (let ((op (lookup sym)))
  872. (and (not (var-set? (operand-var op))) op)))
  873. (_ #f)))
  874. (let* ((vars (map lookup-var gensyms))
  875. (new (fresh-gensyms vars))
  876. (ops (make-bound-operands vars new vals
  877. (lambda (exp counter ctx)
  878. (loop exp env counter ctx))
  879. (map lookup-alias vals)))
  880. (env (fold extend-env env gensyms ops))
  881. (body (loop body env counter ctx)))
  882. (match body
  883. (($ <const>)
  884. (for-tail (list->seq src (append vals (list body)))))
  885. (($ <lexical-ref> _ _ (? (lambda (sym) (memq sym new)) sym))
  886. (let ((pairs (map cons new vals)))
  887. ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
  888. (for-tail
  889. (list->seq
  890. src
  891. (append (map cdr (alist-delete sym pairs eq?))
  892. (list (assq-ref pairs sym)))))))
  893. ((and ($ <conditional> src*
  894. ($ <lexical-ref> _ _ sym) ($ <lexical-ref> _ _ sym) alt)
  895. (? (lambda (_)
  896. (case ctx
  897. ((test effect)
  898. (and (equal? (list sym) new)
  899. (= (lexical-refcount sym) 2)))
  900. (else #f)))))
  901. ;; (let ((x EXP)) (if x x ALT)) -> (if EXP #t ALT) in test context
  902. (make-conditional src* (visit-operand (car ops) counter 'test)
  903. (make-const src* #t) alt))
  904. (_
  905. ;; Only include bindings for which lexical references
  906. ;; have been residualized.
  907. (prune-bindings ops #f body counter ctx
  908. (lambda (names gensyms vals body)
  909. (if (null? names) (error "what!" names))
  910. (make-let src names gensyms vals body)))))))
  911. (($ <letrec> src in-order? names gensyms vals body)
  912. ;; Note the difference from the `let' case: here we use letrec*
  913. ;; so that the `visit' procedure for the new operands closes over
  914. ;; an environment that includes the operands. Also we don't try
  915. ;; to elide aliases, because we can't sensibly reduce something
  916. ;; like (letrec ((a b) (b a)) a).
  917. (letrec* ((visit (lambda (exp counter ctx)
  918. (loop exp env* counter ctx)))
  919. (vars (map lookup-var gensyms))
  920. (new (fresh-gensyms vars))
  921. (ops (make-bound-operands vars new vals visit))
  922. (env* (fold extend-env env gensyms ops))
  923. (body* (visit body counter ctx)))
  924. (if (and (const? body*) (every constant-expression? vals))
  925. ;; We may have folded a loop completely, even though there
  926. ;; might be cyclical references between the bound values.
  927. ;; Handle this degenerate case specially.
  928. body*
  929. (prune-bindings ops in-order? body* counter ctx
  930. (lambda (names gensyms vals body)
  931. (make-letrec src in-order?
  932. names gensyms vals body))))))
  933. (($ <fix> src names gensyms vals body)
  934. (letrec* ((visit (lambda (exp counter ctx)
  935. (loop exp env* counter ctx)))
  936. (vars (map lookup-var gensyms))
  937. (new (fresh-gensyms vars))
  938. (ops (make-bound-operands vars new vals visit))
  939. (env* (fold extend-env env gensyms ops))
  940. (body* (visit body counter ctx)))
  941. (if (const? body*)
  942. body*
  943. (prune-bindings ops #f body* counter ctx
  944. (lambda (names gensyms vals body)
  945. (make-fix src names gensyms vals body))))))
  946. (($ <let-values> lv-src producer consumer)
  947. ;; Peval the producer, then try to inline the consumer into
  948. ;; the producer. If that succeeds, peval again. Otherwise
  949. ;; reconstruct the let-values, pevaling the consumer.
  950. (let ((producer (for-values producer)))
  951. (or (match consumer
  952. ((and ($ <lambda-case> src () #f rest #f () (rest-sym) body #f)
  953. (? (lambda _ (singly-valued-expression? producer))))
  954. (let ((tmp (gensym "tmp ")))
  955. (record-new-temporary! 'tmp tmp 1)
  956. (for-tail
  957. (make-let
  958. src (list 'tmp) (list tmp) (list producer)
  959. (make-let
  960. src (list rest) (list rest-sym)
  961. (list
  962. (make-primcall #f 'list
  963. (list (make-lexical-ref #f 'tmp tmp))))
  964. body)))))
  965. (($ <lambda-case> src req opt rest #f inits gensyms body #f)
  966. (let* ((nmin (length req))
  967. (nmax (and (not rest) (+ nmin (if opt (length opt) 0)))))
  968. (cond
  969. ((inline-values lv-src producer nmin nmax consumer)
  970. => for-tail)
  971. (else #f))))
  972. (_ #f))
  973. (make-let-values lv-src producer (for-tail consumer)))))
  974. (($ <toplevel-ref> src (? effect-free-primitive? name))
  975. exp)
  976. (($ <toplevel-ref>)
  977. ;; todo: open private local bindings.
  978. exp)
  979. (($ <module-ref> src module (? effect-free-primitive? name) #f)
  980. (let ((module (false-if-exception
  981. (resolve-module module #:ensure #f))))
  982. (if (module? module)
  983. (let ((var (module-variable module name)))
  984. (if (eq? var (module-variable the-scm-module name))
  985. (make-primitive-ref src name)
  986. exp))
  987. exp)))
  988. (($ <module-ref>)
  989. exp)
  990. (($ <module-set> src mod name public? exp)
  991. (make-module-set src mod name public? (for-value exp)))
  992. (($ <toplevel-define> src name exp)
  993. (make-toplevel-define src name (for-value exp)))
  994. (($ <toplevel-set> src name exp)
  995. (make-toplevel-set src name (for-value exp)))
  996. (($ <primitive-ref>)
  997. (case ctx
  998. ((effect) (make-void #f))
  999. ((test) (make-const #f #t))
  1000. (else exp)))
  1001. (($ <conditional> src condition subsequent alternate)
  1002. (define (call-with-failure-thunk exp proc)
  1003. (match exp
  1004. (($ <call> _ _ ()) (proc exp))
  1005. (($ <primcall> _ _ ()) (proc exp))
  1006. (($ <const>) (proc exp))
  1007. (($ <void>) (proc exp))
  1008. (($ <lexical-ref>) (proc exp))
  1009. (_
  1010. (let ((t (gensym "failure-")))
  1011. (record-new-temporary! 'failure t 2)
  1012. (make-let
  1013. src (list 'failure) (list t)
  1014. (list
  1015. (make-lambda
  1016. #f '()
  1017. (make-lambda-case #f '() #f #f #f '() '() exp #f)))
  1018. (proc (make-call #f (make-lexical-ref #f 'failure t)
  1019. '())))))))
  1020. (define (simplify-conditional c)
  1021. (match c
  1022. ;; Swap the arms of (if (not FOO) A B), to simplify.
  1023. (($ <conditional> src ($ <primcall> _ 'not (pred))
  1024. subsequent alternate)
  1025. (simplify-conditional
  1026. (make-conditional src pred alternate subsequent)))
  1027. ;; In the following four cases, we try to expose the test to
  1028. ;; the conditional. This will let the CPS conversion avoid
  1029. ;; reifying boolean literals in some cases.
  1030. (($ <conditional> src ($ <let> src* names vars vals body)
  1031. subsequent alternate)
  1032. (make-let src* names vars vals
  1033. (simplify-conditional
  1034. (make-conditional src body subsequent alternate))))
  1035. (($ <conditional> src
  1036. ($ <letrec> src* in-order? names vars vals body)
  1037. subsequent alternate)
  1038. (make-letrec src* in-order? names vars vals
  1039. (simplify-conditional
  1040. (make-conditional src body subsequent alternate))))
  1041. (($ <conditional> src ($ <fix> src* names vars vals body)
  1042. subsequent alternate)
  1043. (make-fix src* names vars vals
  1044. (simplify-conditional
  1045. (make-conditional src body subsequent alternate))))
  1046. (($ <conditional> src ($ <seq> src* head tail)
  1047. subsequent alternate)
  1048. (make-seq src* head
  1049. (simplify-conditional
  1050. (make-conditional src tail subsequent alternate))))
  1051. ;; Special cases for common tests in the predicates of chains
  1052. ;; of if expressions.
  1053. (($ <conditional> src
  1054. ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
  1055. inner-subsequent
  1056. alternate)
  1057. (let lp ((alternate alternate))
  1058. (match alternate
  1059. ;; Lift a common repeated test out of a chain of if
  1060. ;; expressions.
  1061. (($ <conditional> _ (? (cut tree-il=? outer-test <>))
  1062. other-subsequent alternate)
  1063. (make-conditional
  1064. src outer-test
  1065. (simplify-conditional
  1066. (make-conditional src* inner-test inner-subsequent
  1067. other-subsequent))
  1068. alternate))
  1069. ;; Likewise, but punching through any surrounding
  1070. ;; failure continuations.
  1071. (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
  1072. (make-let
  1073. let-src (list name) (list sym) (list thunk)
  1074. (lp body)))
  1075. ;; Otherwise, rotate AND tests to expose a simple
  1076. ;; condition in the front. Although this may result in
  1077. ;; lexically binding failure thunks, the thunks will be
  1078. ;; compiled to labels allocation, so there's no actual
  1079. ;; code growth.
  1080. (_
  1081. (call-with-failure-thunk
  1082. alternate
  1083. (lambda (failure)
  1084. (make-conditional
  1085. src outer-test
  1086. (simplify-conditional
  1087. (make-conditional src* inner-test inner-subsequent failure))
  1088. failure)))))))
  1089. (_ c)))
  1090. (match (for-test condition)
  1091. (($ <const> _ val)
  1092. (if val
  1093. (for-tail subsequent)
  1094. (for-tail alternate)))
  1095. (c
  1096. (simplify-conditional
  1097. (make-conditional src c (for-tail subsequent)
  1098. (for-tail alternate))))))
  1099. (($ <primcall> src 'call-with-values
  1100. (producer
  1101. ($ <lambda> _ _
  1102. (and consumer
  1103. ;; No optional or kwargs.
  1104. ($ <lambda-case>
  1105. _ req #f rest #f () gensyms body #f)))))
  1106. (for-tail (make-let-values src (make-call src producer '())
  1107. consumer)))
  1108. (($ <primcall> src 'dynamic-wind (w thunk u))
  1109. (for-tail
  1110. (with-temporaries
  1111. src (list w u) 2 constant-expression?
  1112. (match-lambda
  1113. ((w u)
  1114. (make-seq
  1115. src
  1116. (make-seq
  1117. src
  1118. (make-conditional
  1119. src
  1120. ;; fixme: introduce logic to fold thunk?
  1121. (make-primcall src 'thunk? (list u))
  1122. (make-call src w '())
  1123. (make-primcall
  1124. src 'throw
  1125. (list
  1126. (make-const #f 'wrong-type-arg)
  1127. (make-const #f "dynamic-wind")
  1128. (make-const #f "Wrong type (expecting thunk): ~S")
  1129. (make-primcall #f 'list (list u))
  1130. (make-primcall #f 'list (list u)))))
  1131. (make-primcall src 'wind (list w u)))
  1132. (make-begin0 src
  1133. (make-call src thunk '())
  1134. (make-seq src
  1135. (make-primcall src 'unwind '())
  1136. (make-call src u '())))))))))
  1137. (($ <primcall> src 'with-fluid* (f v thunk))
  1138. (for-tail
  1139. (with-temporaries
  1140. src (list f v thunk) 1 constant-expression?
  1141. (match-lambda
  1142. ((f v thunk)
  1143. (make-seq src
  1144. (make-primcall src 'push-fluid (list f v))
  1145. (make-begin0 src
  1146. (make-call src thunk '())
  1147. (make-primcall src 'pop-fluid '()))))))))
  1148. (($ <primcall> src 'with-dynamic-state (state thunk))
  1149. (for-tail
  1150. (with-temporaries
  1151. src (list state thunk) 1 constant-expression?
  1152. (match-lambda
  1153. ((state thunk)
  1154. (make-seq src
  1155. (make-primcall src 'push-dynamic-state (list state))
  1156. (make-begin0 src
  1157. (make-call src thunk '())
  1158. (make-primcall src 'pop-dynamic-state
  1159. '()))))))))
  1160. (($ <primcall> src 'values exps)
  1161. (cond
  1162. ((null? exps)
  1163. (if (eq? ctx 'effect)
  1164. (make-void #f)
  1165. exp))
  1166. (else
  1167. (let ((vals (map for-value exps)))
  1168. (if (and (case ctx
  1169. ((value test effect) #t)
  1170. (else (null? (cdr vals))))
  1171. (every singly-valued-expression? vals))
  1172. (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
  1173. (make-primcall src 'values vals))))))
  1174. (($ <primcall> src 'apply (proc args ... tail))
  1175. (let lp ((tail* (find-definition tail 1)) (speculative? #t))
  1176. (define (copyable? x)
  1177. ;; Inlining a result from find-definition effectively copies it,
  1178. ;; relying on the let-pruning to remove its original binding. We
  1179. ;; shouldn't copy non-constant expressions.
  1180. (or (not speculative?) (constant-expression? x)))
  1181. (match tail*
  1182. (($ <const> _ (args* ...))
  1183. (let ((args* (map (cut make-const #f <>) args*)))
  1184. (for-tail (make-call src proc (append args args*)))))
  1185. (($ <primcall> _ 'cons
  1186. ((and head (? copyable?)) (and tail (? copyable?))))
  1187. (for-tail (make-primcall src 'apply
  1188. (cons proc
  1189. (append args (list head tail))))))
  1190. (($ <primcall> _ 'list
  1191. (and args* ((? copyable?) ...)))
  1192. (for-tail (make-call src proc (append args args*))))
  1193. (tail*
  1194. (if speculative?
  1195. (lp (for-value tail) #f)
  1196. (let ((args (append (map for-value args) (list tail*))))
  1197. (make-primcall src 'apply
  1198. (cons (for-value proc) args))))))))
  1199. (($ <primcall> src (? constructor-primitive? name) args)
  1200. (cond
  1201. ((and (memq ctx '(effect test))
  1202. (match (cons name args)
  1203. ((or ('cons _ _)
  1204. ('list . _)
  1205. ('vector . _)
  1206. ('make-prompt-tag)
  1207. ('make-prompt-tag ($ <const> _ (? string?))))
  1208. #t)
  1209. (_ #f)))
  1210. ;; Some expressions can be folded without visiting the
  1211. ;; arguments for value.
  1212. (let ((res (if (eq? ctx 'effect)
  1213. (make-void #f)
  1214. (make-const #f #t))))
  1215. (for-tail (list->seq src (append args (list res))))))
  1216. (else
  1217. (match (cons name (map for-value args))
  1218. (('cons x ($ <const> _ (? (cut eq? <> '()))))
  1219. (make-primcall src 'list (list x)))
  1220. (('cons x ($ <primcall> _ 'list elts))
  1221. (make-primcall src 'list (cons x elts)))
  1222. (('list)
  1223. (make-const src '()))
  1224. (('vector)
  1225. (make-const src '#()))
  1226. ((name . args)
  1227. (make-primcall src name args))))))
  1228. (($ <primcall> src 'thunk? (proc))
  1229. (case ctx
  1230. ((effect)
  1231. (for-tail (make-seq src proc (make-void src))))
  1232. (else
  1233. (match (for-value proc)
  1234. (($ <lambda> _ _ ($ <lambda-case> _ req))
  1235. (for-tail (make-const src (null? req))))
  1236. (proc
  1237. (match (find-definition proc 2)
  1238. (($ <lambda> _ _ ($ <lambda-case> _ req))
  1239. (for-tail (make-const src (null? req))))
  1240. (_
  1241. (make-primcall src 'thunk? (list proc)))))))))
  1242. (($ <primcall> src name args)
  1243. (match (cons name (map for-value args))
  1244. ;; FIXME: these for-tail recursions could take place outside
  1245. ;; an effort counter.
  1246. (('car ($ <primcall> src 'cons (head tail)))
  1247. (for-tail (make-seq src tail head)))
  1248. (('cdr ($ <primcall> src 'cons (head tail)))
  1249. (for-tail (make-seq src head tail)))
  1250. (('car ($ <primcall> src 'list (head . tail)))
  1251. (for-tail (list->seq src (append tail (list head)))))
  1252. (('cdr ($ <primcall> src 'list (head . tail)))
  1253. (for-tail (make-seq src head (make-primcall #f 'list tail))))
  1254. (('car ($ <const> src (head . tail)))
  1255. (for-tail (make-const src head)))
  1256. (('cdr ($ <const> src (head . tail)))
  1257. (for-tail (make-const src tail)))
  1258. (((or 'memq 'memv) k ($ <const> _ (elts ...)))
  1259. ;; FIXME: factor
  1260. (case ctx
  1261. ((effect)
  1262. (for-tail
  1263. (make-seq src k (make-void #f))))
  1264. ((test)
  1265. (cond
  1266. ((const? k)
  1267. ;; A shortcut. The `else' case would handle it, but
  1268. ;; this way is faster.
  1269. (let ((member (case name ((memq) memq) ((memv) memv))))
  1270. (make-const #f (and (member (const-exp k) elts) #t))))
  1271. ((null? elts)
  1272. (for-tail
  1273. (make-seq src k (make-const #f #f))))
  1274. (else
  1275. (let ((t (gensym "t "))
  1276. (eq (if (eq? name 'memq) 'eq? 'eqv?)))
  1277. (record-new-temporary! 't t (length elts))
  1278. (for-tail
  1279. (make-let
  1280. src (list 't) (list t) (list k)
  1281. (let lp ((elts elts))
  1282. (define test
  1283. (make-primcall #f eq
  1284. (list (make-lexical-ref #f 't t)
  1285. (make-const #f (car elts)))))
  1286. (if (null? (cdr elts))
  1287. test
  1288. (make-conditional src test
  1289. (make-const #f #t)
  1290. (lp (cdr elts)))))))))))
  1291. (else
  1292. (cond
  1293. ((const? k)
  1294. (let ((member (case name ((memq) memq) ((memv) memv))))
  1295. (make-const #f (member (const-exp k) elts))))
  1296. ((null? elts)
  1297. (for-tail (make-seq src k (make-const #f #f))))
  1298. (else
  1299. (make-primcall src name (list k (make-const #f elts))))))))
  1300. (((? equality-primitive?)
  1301. ($ <lexical-ref> _ _ sym) ($ <lexical-ref> _ _ sym))
  1302. (for-tail (make-const #f #t)))
  1303. (('logbit? ($ <const> src2
  1304. (? (lambda (bit)
  1305. (and (exact-integer? bit)
  1306. (<= 0 bit (logcount most-positive-fixnum))))
  1307. bit))
  1308. val)
  1309. (for-tail
  1310. (make-primcall src 'logtest
  1311. (list (make-const src2 (ash 1 bit)) val))))
  1312. (('logtest a b)
  1313. (for-tail
  1314. (make-primcall
  1315. src
  1316. 'not
  1317. (list
  1318. (make-primcall src 'eq?
  1319. (list (make-primcall src 'logand (list a b))
  1320. (make-const src 0)))))))
  1321. (((? effect-free-primitive?) . args)
  1322. (fold-constants src name args ctx))
  1323. ((name . args)
  1324. (make-primcall src name args))))
  1325. (($ <call> src orig-proc orig-args)
  1326. ;; todo: augment the global env with specialized functions
  1327. (let revisit-proc ((proc (visit orig-proc 'operator)))
  1328. (match proc
  1329. (($ <primitive-ref> _ name)
  1330. (for-tail
  1331. (expand-primcall (make-primcall src name orig-args))))
  1332. (($ <lambda> _ _
  1333. ($ <lambda-case> _ req opt rest #f inits gensyms body #f))
  1334. ;; Simple case: no keyword arguments.
  1335. ;; todo: handle the more complex cases
  1336. (let* ((nargs (length orig-args))
  1337. (nreq (length req))
  1338. (opt (or opt '()))
  1339. (rest (if rest (list rest) '()))
  1340. (nopt (length opt))
  1341. (key (source-expression proc)))
  1342. (define (singly-referenced-lambda? orig-proc)
  1343. (match orig-proc
  1344. (($ <lambda>) #t)
  1345. (($ <lexical-ref> _ _ sym)
  1346. (and (not (assigned-lexical? sym))
  1347. (= (lexical-refcount sym) 1)
  1348. (singly-referenced-lambda?
  1349. (operand-source (lookup sym)))))
  1350. (_ #f)))
  1351. (define (inlined-call)
  1352. (let ((req-vals (list-head orig-args nreq))
  1353. (opt-vals (let lp ((args (drop orig-args nreq))
  1354. (inits inits)
  1355. (out '()))
  1356. (match inits
  1357. (() (reverse out))
  1358. ((init . inits)
  1359. (match args
  1360. (()
  1361. (lp '() inits (cons init out)))
  1362. ((arg . args)
  1363. (lp args inits (cons arg out))))))))
  1364. (rest-vals (cond
  1365. ((> nargs (+ nreq nopt))
  1366. (list (make-primcall
  1367. #f 'list
  1368. (drop orig-args (+ nreq nopt)))))
  1369. ((null? rest) '())
  1370. (else (list (make-const #f '()))))))
  1371. (if (>= nargs (+ nreq nopt))
  1372. (make-let src
  1373. (append req opt rest)
  1374. gensyms
  1375. (append req-vals opt-vals rest-vals)
  1376. body)
  1377. ;; The default initializers of optional arguments
  1378. ;; may refer to earlier arguments, so in the general
  1379. ;; case we must expand into a series of nested let
  1380. ;; expressions.
  1381. ;;
  1382. ;; In the generated code, the outermost let
  1383. ;; expression will bind all required arguments, as
  1384. ;; well as the empty rest argument, if any. Each
  1385. ;; optional argument will be bound within an inner
  1386. ;; let.
  1387. (make-let src
  1388. (append req rest)
  1389. (append (list-head gensyms nreq)
  1390. (last-pair gensyms))
  1391. (append req-vals rest-vals)
  1392. (fold-right (lambda (var gensym val body)
  1393. (make-let src
  1394. (list var)
  1395. (list gensym)
  1396. (list val)
  1397. body))
  1398. body
  1399. opt
  1400. (list-head (drop gensyms nreq) nopt)
  1401. opt-vals)))))
  1402. (cond
  1403. ((or (< nargs nreq) (and (not rest) (> nargs (+ nreq nopt))))
  1404. ;; An error, or effecting arguments.
  1405. (make-call src (for-call orig-proc) (map for-value orig-args)))
  1406. ((or (and=> (find-counter key counter) counter-recursive?)
  1407. (singly-referenced-lambda? orig-proc))
  1408. ;; A recursive call, or a lambda in the operator
  1409. ;; position of the source expression. Process again in
  1410. ;; tail context.
  1411. ;;
  1412. ;; In the recursive case, mark intervening counters as
  1413. ;; recursive, so we can handle a toplevel counter that
  1414. ;; recurses mutually with some other procedure.
  1415. ;; Otherwise, the next time we see the other procedure,
  1416. ;; the effort limit would be clamped to 100.
  1417. ;;
  1418. (let ((found (find-counter key counter)))
  1419. (if (and found (counter-recursive? found))
  1420. (let lp ((counter counter))
  1421. (if (not (eq? counter found))
  1422. (begin
  1423. (set-counter-recursive?! counter #t)
  1424. (lp (counter-prev counter)))))))
  1425. (log 'inline-recurse key)
  1426. (loop (inlined-call) env counter ctx))
  1427. (else
  1428. ;; An integration at the top-level, the first
  1429. ;; recursion of a recursive procedure, or a nested
  1430. ;; integration of a procedure that hasn't been seen
  1431. ;; yet.
  1432. (log 'inline-begin exp)
  1433. (let/ec k
  1434. (define (abort)
  1435. (log 'inline-abort exp)
  1436. (k (make-call src (for-call orig-proc)
  1437. (map for-value orig-args))))
  1438. (define new-counter
  1439. (cond
  1440. ;; These first two cases will transfer effort
  1441. ;; from the current counter into the new
  1442. ;; counter.
  1443. ((find-counter key counter)
  1444. => (lambda (prev)
  1445. (make-recursive-counter recursive-effort-limit
  1446. operand-size-limit
  1447. prev counter)))
  1448. (counter
  1449. (make-nested-counter abort key counter))
  1450. ;; This case opens a new account, effectively
  1451. ;; printing money. It should only do so once
  1452. ;; for each call site in the source program.
  1453. (else
  1454. (make-top-counter effort-limit operand-size-limit
  1455. abort key))))
  1456. (define result
  1457. (loop (inlined-call) env new-counter ctx))
  1458. (if counter
  1459. ;; The nested inlining attempt succeeded.
  1460. ;; Deposit the unspent effort and size back
  1461. ;; into the current counter.
  1462. (transfer! new-counter counter))
  1463. (log 'inline-end result exp)
  1464. result)))))
  1465. (($ <let> _ _ _ vals _)
  1466. ;; Attempt to inline `let' in the operator position.
  1467. ;;
  1468. ;; We have to re-visit the proc in value mode, since the
  1469. ;; `let' bindings might have been introduced or renamed,
  1470. ;; whereas the lambda (if any) in operator position has not
  1471. ;; been renamed.
  1472. (if (or (and-map constant-expression? vals)
  1473. (and-map constant-expression? orig-args))
  1474. ;; The arguments and the let-bound values commute.
  1475. (match (for-value orig-proc)
  1476. (($ <let> lsrc names syms vals body)
  1477. (log 'inline-let orig-proc)
  1478. (for-tail
  1479. (make-let lsrc names syms vals
  1480. (make-call src body orig-args))))
  1481. ;; It's possible for a `let' to go away after the
  1482. ;; visit due to the fact that visiting a procedure in
  1483. ;; value context will prune unused bindings, whereas
  1484. ;; visiting in operator mode can't because it doesn't
  1485. ;; traverse through lambdas. In that case re-visit
  1486. ;; the procedure.
  1487. (proc (revisit-proc proc)))
  1488. (make-call src (for-call orig-proc)
  1489. (map for-value orig-args))))
  1490. (_
  1491. (make-call src (for-call orig-proc) (map for-value orig-args))))))
  1492. (($ <lambda> src meta body)
  1493. (case ctx
  1494. ((effect) (make-void #f))
  1495. ((test) (make-const #f #t))
  1496. ((operator) exp)
  1497. (else (record-source-expression!
  1498. exp
  1499. (make-lambda src meta (and body (for-values body)))))))
  1500. (($ <lambda-case> src req opt rest kw inits gensyms body alt)
  1501. (define (lift-applied-lambda body gensyms)
  1502. (and (not opt) rest (not kw)
  1503. (match body
  1504. (($ <primcall> _ 'apply
  1505. (($ <lambda> _ _ (and lcase ($ <lambda-case> _ req1)))
  1506. ($ <lexical-ref> _ _ sym)
  1507. ...))
  1508. (and (equal? sym gensyms)
  1509. (not (lambda-case-alternate lcase))
  1510. (<= (length req) (length req1))
  1511. (every (lambda (s)
  1512. (= (lexical-refcount s) 1))
  1513. sym)
  1514. lcase))
  1515. (_ #f))))
  1516. (let* ((vars (map lookup-var gensyms))
  1517. (new (fresh-gensyms vars))
  1518. (env (fold extend-env env gensyms
  1519. (make-unbound-operands vars new)))
  1520. (new-sym (lambda (old)
  1521. (operand-sym (cdr (vhash-assq old env)))))
  1522. (body (loop body env counter ctx)))
  1523. (or
  1524. ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
  1525. (lift-applied-lambda body new)
  1526. (make-lambda-case src req opt rest
  1527. (match kw
  1528. ((aok? (kw name old) ...)
  1529. (cons aok? (map list kw name (map new-sym old))))
  1530. (_ #f))
  1531. (map (cut loop <> env counter 'value) inits)
  1532. new
  1533. body
  1534. (and alt (for-tail alt))))))
  1535. (($ <seq> src head tail)
  1536. (let ((head (for-effect head))
  1537. (tail (for-tail tail)))
  1538. (if (void? head)
  1539. tail
  1540. (make-seq src
  1541. (if (and (seq? head)
  1542. (void? (seq-tail head)))
  1543. (seq-head head)
  1544. head)
  1545. tail))))
  1546. (($ <prompt> src escape-only? tag body handler)
  1547. (define (make-prompt-tag? x)
  1548. (match x
  1549. (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
  1550. #t)
  1551. (_ #f)))
  1552. (let ((tag (for-value tag))
  1553. (body (if escape-only? (for-tail body) (for-value body))))
  1554. (cond
  1555. ((find-definition tag 1)
  1556. (lambda (val op)
  1557. (make-prompt-tag? val))
  1558. => (lambda (val op)
  1559. ;; There is no way that an <abort> could know the tag
  1560. ;; for this <prompt>, so we can elide the <prompt>
  1561. ;; entirely.
  1562. (unrecord-operand-uses op 1)
  1563. (for-tail (if escape-only? body (make-call src body '())))))
  1564. (else
  1565. (let ((handler (for-value handler)))
  1566. (define (escape-only-handler? handler)
  1567. (match handler
  1568. (($ <lambda> _ _
  1569. ($ <lambda-case> _ (_ . _) _ _ _ _ (k . _) body #f))
  1570. (not (tree-il-any
  1571. (match-lambda
  1572. (($ <lexical-ref> _ _ (? (cut eq? <> k))) #t)
  1573. (_ #f))
  1574. body)))
  1575. (else #f)))
  1576. (if (and (not escape-only?) (escape-only-handler? handler))
  1577. ;; Prompt transitioning to escape-only; transition body
  1578. ;; to be an expression.
  1579. (for-tail
  1580. (make-prompt src #t tag (make-call #f body '()) handler))
  1581. (make-prompt src escape-only? tag body handler)))))))
  1582. (($ <abort> src tag args tail)
  1583. (make-abort src (for-value tag) (map for-value args)
  1584. (for-value tail))))))