Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / gtn.lisp
blob3d8580e2232cda839a8e40e16c870a5ab5ad5429
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.
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.
15 (in-package "SB!C")
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))))
30 (values))
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)))
69 (debug-variable-p
70 (physenv-debug-live-tn res (lambda-physenv fun))))
72 (setf (tn-leaf res) var)
73 (setf (leaf-info var) res))))
74 (values))
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)))
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))))
105 (values))
107 ;;; Return true if we should use the standard (unknown) return
108 ;;; convention for a TAIL-SET. We use the standard return convention
109 ;;; when:
110 ;;; -- If it has an XEP.
111 ;;; it could break the tail call in this case, but it usually
112 ;;; doesn't produce better code and makes for worse debugging.
113 ;;; -- It appears to be more efficient to use the standard convention,
114 ;;; since there are no non-TR local calls that could benefit from
115 ;;; a non-standard convention.
116 ;;; -- We're compiling with RETURN-FROM-FRAME instrumentation, which
117 ;;; only works (on x86, x86-64, arm) for the standard convention.
118 (defun use-standard-returns (tails)
119 (declare (type tail-set tails))
120 (let ((funs (tail-set-funs tails)))
121 (or (find-if #'xep-p funs)
122 (some (lambda (fun) (policy fun (>= insert-debug-catch 2))) funs)
123 (block punt
124 (dolist (fun funs t)
125 (dolist (ref (leaf-refs fun))
126 (let* ((lvar (node-lvar ref))
127 (dest (and lvar (lvar-dest lvar))))
128 (when (and (basic-combination-p dest)
129 (not (node-tail-p dest))
130 (eq (basic-combination-fun dest) lvar)
131 (eq (basic-combination-kind dest) :local))
132 (return-from punt nil)))))))))
134 ;;; If policy indicates, give an efficiency note about our inability to
135 ;;; use the known return convention. We try to find a function in the
136 ;;; tail set with non-constant return values to use as context. If
137 ;;; there is no such function, then be more vague.
138 (defun return-value-efficiency-note (tails)
139 (declare (type tail-set tails))
140 (let ((funs (tail-set-funs tails)))
141 (when (policy (lambda-bind (first funs))
142 (> (max speed space)
143 inhibit-warnings))
144 (dolist (fun funs
145 (let ((*compiler-error-context* (lambda-bind (first funs))))
146 (compiler-notify
147 "Return value count mismatch prevents known return ~
148 from these functions:~
149 ~{~% ~A~}"
150 (mapcar #'leaf-source-name
151 (remove-if-not #'leaf-has-source-name-p funs)))))
152 (let ((ret (lambda-return fun)))
153 (when ret
154 (let ((rtype (return-result-type ret)))
155 (multiple-value-bind (ignore count) (values-types rtype)
156 (declare (ignore ignore))
157 (when (eq count :unknown)
158 (let ((*compiler-error-context* (lambda-bind fun)))
159 (compiler-notify
160 "Return type not fixed values, so can't use known return ~
161 convention:~% ~S"
162 (type-specifier rtype)))
163 (return)))))))))
164 (values))
166 ;;; Return a RETURN-INFO structure describing how we should return
167 ;;; from functions in the specified tail set. We use the unknown
168 ;;; values convention if the number of values is unknown, or if it is
169 ;;; a good idea for some other reason. Otherwise we allocate passing
170 ;;; locations for a fixed number of values.
171 (defun return-info-for-set (tails)
172 (declare (type tail-set tails))
173 (multiple-value-bind (types count) (values-types (tail-set-type tails))
174 (let ((ptypes (mapcar #'primitive-type types))
175 (use-standard (use-standard-returns tails)))
176 (when (and (eq count :unknown) (not use-standard)
177 (not (eq (tail-set-type tails) *empty-type*)))
178 (return-value-efficiency-note tails))
179 (if (or (eq count :unknown) use-standard)
180 (make-return-info :kind :unknown
181 :count count
182 :types ptypes)
183 (make-return-info :kind :fixed
184 :count count
185 :types ptypes
186 :locations (mapcar #'make-normal-tn ptypes))))))
188 ;;; If TAIL-SET doesn't have any INFO, then make a RETURN-INFO for it.
189 (defun assign-return-locations (fun)
190 (declare (type clambda fun))
191 (let* ((tails (lambda-tail-set fun))
192 (returns (or (tail-set-info tails)
193 (setf (tail-set-info tails)
194 (return-info-for-set tails))))
195 (return (lambda-return fun)))
196 (when (and return
197 (xep-p fun))
198 (aver (eq (return-info-kind returns) :unknown))))
199 (values))
201 ;;; Make an IR2-NLX-INFO structure for each NLX entry point recorded.
202 ;;; We call a VM supplied function to make the SAVE-SP restricted on
203 ;;; the stack. The NLX-ENTRY VOP's :FORCE-TO-STACK SAVE-P value
204 ;;; doesn't do this, since the SP is an argument to the VOP, and thus
205 ;;; isn't live afterwards.
206 (defun assign-ir2-nlx-info (fun)
207 (declare (type clambda fun))
208 (let ((physenv (lambda-physenv fun)))
209 (dolist (nlx (physenv-nlx-info physenv))
210 (setf (nlx-info-info nlx)
211 (make-ir2-nlx-info
212 :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
213 '(:block :tagbody))
214 (if (nlx-info-safe-p nlx)
215 (make-normal-tn *backend-t-primitive-type*)
216 (make-stack-pointer-tn)))
217 :save-sp (make-nlx-sp-tn physenv)))))
218 (values))