Compiler support for specialised implicit value cells
[sbcl.git] / src / compiler / gtn.lisp
1 ;;;; This file contains the GTN pass in the compiler. GTN allocates
2 ;;;; the TNs that hold the values of lexical variables and determines
3 ;;;; the calling conventions and passing locations used in function
4 ;;;; calls.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
14
15 (in-package "SB!C")
16
17 ;;; We make a pass over the component's environments, assigning argument
18 ;;; passing locations and return conventions and TNs for local variables.
19 (defun gtn-analyze (component)
20   (setf (component-info component) (make-ir2-component))
21   (let ((funs (component-lambdas component)))
22     (dolist (fun funs)
23       (assign-ir2-physenv fun)
24       (assign-return-locations fun)
25       (assign-ir2-nlx-info fun)
26       (assign-lambda-var-tns fun nil)
27       (dolist (let (lambda-lets fun))
28         (assign-lambda-var-tns let t))))
29
30   (values))
31
32 ;;; We have to allocate the home TNs for variables before we can call
33 ;;; ASSIGN-IR2-PHYSENV so that we can close over TNs that haven't
34 ;;; had their home environment assigned yet. Here we evaluate the
35 ;;; DEBUG-INFO/SPEED tradeoff to determine how variables are
36 ;;; allocated. If SPEED is 3, then all variables are subject to
37 ;;; lifetime analysis. Otherwise, only LET-P variables are allocated
38 ;;; normally, and that can be inhibited by DEBUG-INFO = 3.
39 (defun assign-lambda-var-tns (fun let-p)
40   (declare (type clambda fun))
41   (dolist (var (lambda-vars fun))
42     (when (leaf-refs var)
43       (let* (ptype-info
44              (type (if (lambda-var-indirect var)
45                        (if (lambda-var-explicit-value-cell var)
46                            *backend-t-primitive-type*
47                            (or (first
48                                 (setf ptype-info
49                                       (primitive-type-indirect-cell-type
50                                        (primitive-type (leaf-type var)))))
51                                *backend-t-primitive-type*))
52                        (primitive-type (leaf-type var))))
53              (res (make-normal-tn type))
54              (node (lambda-bind fun))
55              (debug-variable-p (not (or (and let-p (policy node (< debug 3)))
56                                         (policy node (zerop debug))
57                                         (policy node (= speed 3))))))
58         (cond
59          ((and (lambda-var-indirect var)
60                (not (lambda-var-explicit-value-cell var)))
61           ;; Force closed-over indirect LAMBDA-VARs without explicit
62           ;; VALUE-CELLs to the stack, and make sure that they are
63           ;; live over the dynamic contour of the physenv.
64           (setf (tn-sc res) (if ptype-info
65                                 (second ptype-info)
66                                 (sc-or-lose 'sb!vm::control-stack)))
67           (physenv-live-tn res (lambda-physenv fun)))
68
69          (debug-variable-p
70           (physenv-debug-live-tn res (lambda-physenv fun))))
71
72         (setf (tn-leaf res) var)
73         (setf (leaf-info var) res))))
74   (values))
75
76 ;;; Give CLAMBDA an IR2-PHYSENV structure. (And in order to
77 ;;; properly initialize the new structure, we make the TNs which hold
78 ;;; environment values and the old-FP/return-PC.)
79 (defun assign-ir2-physenv (clambda)
80   (declare (type clambda clambda))
81   (let ((lambda-physenv (lambda-physenv clambda))
82         (reversed-ir2-physenv-alist nil))
83     ;; FIXME: should be MAPCAR, not DOLIST
84     (dolist (thing (physenv-closure lambda-physenv))
85       (let ((ptype (etypecase thing
86                      (lambda-var
87                       (if (lambda-var-indirect thing)
88                           *backend-t-primitive-type*
89                           (primitive-type (leaf-type thing))))
90                      (nlx-info *backend-t-primitive-type*)
91                      (clambda *backend-t-primitive-type*))))
92         (push (cons thing (make-normal-tn ptype))
93               reversed-ir2-physenv-alist)))
94
95     (let ((res (make-ir2-physenv
96                 :closure (nreverse reversed-ir2-physenv-alist)
97                 :return-pc-pass (make-return-pc-passing-location
98                                  (xep-p clambda)))))
99       (setf (physenv-info lambda-physenv) res)
100       (setf (ir2-physenv-old-fp res)
101             (make-old-fp-save-location lambda-physenv))
102       (setf (ir2-physenv-return-pc res)
103             (make-return-pc-save-location lambda-physenv))))
104
105   (values))
106
107 ;;; Return true if FUN's result is used in a tail-recursive full
108 ;;; call. We only consider explicit :FULL calls. It is assumed that
109 ;;; known calls are never part of a tail-recursive loop, so we don't
110 ;;; need to enforce tail-recursion. In any case, we don't know which
111 ;;; known calls will actually be full calls until after LTN.
112 (defun has-full-call-use (fun)
113   (declare (type clambda fun))
114   (let ((return (lambda-return fun)))
115     (and return
116          (do-uses (use (return-result return) nil)
117            (when (and (node-tail-p use)
118                       (basic-combination-p use)
119                       (eq (basic-combination-kind use) :full))
120              (return t))))))
121
122 ;;; Return true if we should use the standard (unknown) return
123 ;;; convention for a TAIL-SET. We use the standard return convention
124 ;;; when:
125 ;;; -- We must use the standard convention to preserve tail-recursion,
126 ;;;    since the TAIL-SET contains both an XEP and a TR full call.
127 ;;; -- It appears to be more efficient to use the standard convention,
128 ;;;    since there are no non-TR local calls that could benefit from
129 ;;;    a non-standard convention.
130 ;;; -- We're compiling with RETURN-FROM-FRAME instrumentation, which
131 ;;;    only works (on x86 and x86-64) for the standard convention.
132 (defun use-standard-returns (tails)
133   (declare (type tail-set tails))
134   (let ((funs (tail-set-funs tails)))
135     (or (and (find-if #'xep-p funs)
136              (find-if #'has-full-call-use funs))
137         (some (lambda (fun) (policy fun (>= insert-debug-catch 2))) funs)
138         (block punt
139           (dolist (fun funs t)
140             (dolist (ref (leaf-refs fun))
141               (let* ((lvar (node-lvar ref))
142                      (dest (and lvar (lvar-dest lvar))))
143                 (when (and (basic-combination-p dest)
144                            (not (node-tail-p dest))
145                            (eq (basic-combination-fun dest) lvar)
146                            (eq (basic-combination-kind dest) :local))
147                   (return-from punt nil)))))))))
148
149 ;;; If policy indicates, give an efficiency note about our inability to
150 ;;; use the known return convention. We try to find a function in the
151 ;;; tail set with non-constant return values to use as context. If
152 ;;; there is no such function, then be more vague.
153 (defun return-value-efficiency-note (tails)
154   (declare (type tail-set tails))
155   (let ((funs (tail-set-funs tails)))
156     (when (policy (lambda-bind (first funs))
157                   (> (max speed space)
158                      inhibit-warnings))
159       (dolist (fun funs
160                    (let ((*compiler-error-context* (lambda-bind (first funs))))
161                      (compiler-notify
162                       "Return value count mismatch prevents known return ~
163                        from these functions:~
164                        ~{~%  ~A~}"
165                       (mapcar #'leaf-source-name
166                               (remove-if-not #'leaf-has-source-name-p funs)))))
167         (let ((ret (lambda-return fun)))
168           (when ret
169             (let ((rtype (return-result-type ret)))
170               (multiple-value-bind (ignore count) (values-types rtype)
171                 (declare (ignore ignore))
172                 (when (eq count :unknown)
173                   (let ((*compiler-error-context* (lambda-bind fun)))
174                     (compiler-notify
175                      "Return type not fixed values, so can't use known return ~
176                       convention:~%  ~S"
177                      (type-specifier rtype)))
178                   (return)))))))))
179   (values))
180
181 ;;; Return a RETURN-INFO structure describing how we should return
182 ;;; from functions in the specified tail set. We use the unknown
183 ;;; values convention if the number of values is unknown, or if it is
184 ;;; a good idea for some other reason. Otherwise we allocate passing
185 ;;; locations for a fixed number of values.
186 (defun return-info-for-set (tails)
187   (declare (type tail-set tails))
188   (multiple-value-bind (types count) (values-types (tail-set-type tails))
189     (let ((ptypes (mapcar #'primitive-type types))
190           (use-standard (use-standard-returns tails)))
191       (when (and (eq count :unknown) (not use-standard)
192                  (not (eq (tail-set-type tails) *empty-type*)))
193         (return-value-efficiency-note tails))
194       (if (or (eq count :unknown) use-standard)
195           (make-return-info :kind :unknown
196                             :count count
197                             :types ptypes)
198           (make-return-info :kind :fixed
199                             :count count
200                             :types ptypes
201                             :locations (mapcar #'make-normal-tn ptypes))))))
202
203 ;;; If TAIL-SET doesn't have any INFO, then make a RETURN-INFO for it.
204 ;;; If we choose a return convention other than :UNKNOWN, and this
205 ;;; environment is for an XEP, then break tail recursion on the XEP
206 ;;; calls, since we must always use unknown values when returning from
207 ;;; an XEP.
208 (defun assign-return-locations (fun)
209   (declare (type clambda fun))
210   (let* ((tails (lambda-tail-set fun))
211          (returns (or (tail-set-info tails)
212                       (setf (tail-set-info tails)
213                             (return-info-for-set tails))))
214          (return (lambda-return fun)))
215     (when (and return
216                (not (eq (return-info-kind returns) :unknown))
217                (xep-p fun))
218       (do-uses (use (return-result return))
219         (setf (node-tail-p use) nil))))
220   (values))
221
222 ;;; Make an IR2-NLX-INFO structure for each NLX entry point recorded.
223 ;;; We call a VM supplied function to make the SAVE-SP restricted on
224 ;;; the stack. The NLX-ENTRY VOP's :FORCE-TO-STACK SAVE-P value
225 ;;; doesn't do this, since the SP is an argument to the VOP, and thus
226 ;;; isn't live afterwards.
227 (defun assign-ir2-nlx-info (fun)
228   (declare (type clambda fun))
229   (let ((physenv (lambda-physenv fun)))
230     (dolist (nlx (physenv-nlx-info physenv))
231       (setf (nlx-info-info nlx)
232             (make-ir2-nlx-info
233              :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
234                                  '(:block :tagbody))
235                      (if (nlx-info-safe-p nlx)
236                          (make-normal-tn *backend-t-primitive-type*)
237                          (make-stack-pointer-tn)))
238              :save-sp (make-nlx-sp-tn physenv)))))
239   (values))