peval.scm 68 KB

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