3 ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (language tree-il inline)
20 #:use-module (system base pmatch)
21 #:use-module (system base syntax)
22 #:use-module (language tree-il)
25 ;; Possible optimizations:
26 ;; * constant folding, propagation
27 ;; * procedure inlining
28 ;; * always when single call site
29 ;; * always for "trivial" procs
30 ;; * otherwise who knows
31 ;; * dead code elimination
32 ;; * degenerate case optimizations
35 (define (boolean-value x)
36 (let ((src (tree-il-src x)))
41 ;; FIXME: This is redundant with what the partial evaluator does.
42 ((<conditional> test consequent alternate)
43 (record-case (boolean-value test)
46 ((#t) (boolean-value consequent))
47 ((#f) (boolean-value alternate))
51 ((<application> src proc args)
53 ;; ((lambda (y ...) x) z ...) => (let ((y z) ...) x)
54 ((<primitive-ref> name)
58 ((,k ,l) (guard (const? l) (list? (const-exp l)))
60 ((null? (const-exp l))
63 (make-const #f (->bool ((case name
66 (else (error "unexpected member func" name)))
67 (const-exp k) (const-exp l)))))
69 (let lp ((elts (const-exp l)))
70 (let ((test (make-application
72 (make-primitive-ref #f (case name
75 (else (error "what"))))
76 (list k (make-const #f (car elts))))))
77 (if (null? (cdr elts))
83 (lp (cdr elts)))))))))
95 (make-const src (not (not exp))))
100 ;; This is a completely brain-dead optimization pass whose sole claim to
101 ;; fame is ((lambda () x)) => x.
105 ((<application> src proc args)
107 ;; ((lambda (y ...) x) z ...) => (let ((y z) ...) x)
109 (let lp ((lcase body))
112 ((<lambda-case> req opt rest kw inits gensyms body alternate)
113 (if (and (= (length gensyms) (length req) (length args)))
114 (let ((x (make-let src req gensyms args body)))
118 ((<primitive-ref> name)
122 ;; (call-with-values (lambda () foo) (lambda (a b . c) bar))
123 ;; => (let-values (((a b . c) foo)) bar)
125 ;; Note that this is a singly-binding form of let-values.
126 ;; Also note that Scheme's let-values expands into
127 ;; call-with-values, then here we reduce it to tree-il's
129 ((,producer ,consumer)
130 (guard (lambda? consumer)
131 (lambda-case? (lambda-body consumer))
132 (not (lambda-case-opt (lambda-body consumer)))
133 (not (lambda-case-kw (lambda-body consumer)))
134 (not (lambda-case-alternate (lambda-body consumer))))
137 (let ((x (make-application src producer '())))
139 (lambda-body consumer)))
146 ((<conditional> test consequent alternate)
147 (let ((btest (boolean-value test)))
148 (or (record-case btest
157 (make-conditional (conditional-src x)
158 btest consequent alternate)))))
160 ((<let> gensyms body)
161 (if (null? gensyms) body x))
163 ((<letrec> gensyms body)
164 (if (null? gensyms) body x))
166 ((<fix> gensyms body)
167 (if (null? gensyms) body x))
169 ((<lambda-case> req opt rest kw gensyms body alternate)
170 (define (args-compatible? args gensyms)
171 (let lp ((args args) (gensyms gensyms))
173 ((null? args) (null? gensyms))
175 ((and (lexical-ref? (car args))
176 (eq? (lexical-ref-gensym (car args)) (car gensyms)))
177 (lp (cdr args) (cdr gensyms)))
180 (and (not opt) (not kw) rest (not alternate)
182 ((<application> proc args)
183 ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
184 (and (primitive-ref? proc)
185 (eq? (primitive-ref-name proc) '@apply)
188 (args-compatible? (cdr args) gensyms)
189 (lambda-body (car args))))
192 ;; Actually the opposite of inlining -- if the prompt cannot be proven to
193 ;; be escape-only, ensure that its body is the application of a thunk.
194 ((<prompt> src tag body handler)
195 (define (escape-only? handler)
196 (and (pair? (lambda-case-req handler))
197 (let ((cont (car (lambda-case-gensyms handler))))
198 (tree-il-fold (lambda (leaf escape-only?)
201 (and (lexical-ref? leaf)
202 (eq? (lexical-ref-gensym leaf) cont)))))
203 (lambda (down escape-only?) escape-only?)
204 (lambda (up escape-only?) escape-only?)
206 (lambda-case-body handler)))))
207 (define (make-thunk body)
208 (make-lambda #f '() (make-lambda-case #f '() #f #f #f '() '() body #f)))
210 (if (or (and (application? body)
211 (lambda? (application-proc body))
212 (null? (application-args body)))
213 (escape-only? handler))
216 (make-application #f (make-thunk body) '())
220 (post-order! inline1 x))