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
6 ;;;; This software is part of the SBCL system. See the README file for
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.
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)))
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))))
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))
43 (let* ((type (if (lambda-var-indirect var)
44 *backend-t-primitive-type*
45 (primitive-type (leaf-type var))))
46 (res (make-normal-tn type))
47 (node (lambda-bind fun))
48 (debug-variable-p (not (or (and let-p (policy node (< debug 3)))
49 (policy node (zerop debug))
50 (policy node (= speed 3))))))
52 ((and (lambda-var-indirect var)
53 (not (lambda-var-explicit-value-cell var)))
54 ;; Force closed-over indirect LAMBDA-VARs without explicit
55 ;; VALUE-CELLs to the stack, and make sure that they are
56 ;; live over the dynamic contour of the physenv.
57 (setf (tn-sc res) (svref *backend-sc-numbers*
58 sb!vm:control-stack-sc-number))
59 (physenv-live-tn res (lambda-physenv fun)))
62 (physenv-debug-live-tn res (lambda-physenv fun))))
64 (setf (tn-leaf res) var)
65 (setf (leaf-info var) res))))
68 ;;; Give CLAMBDA an IR2-PHYSENV structure. (And in order to
69 ;;; properly initialize the new structure, we make the TNs which hold
70 ;;; environment values and the old-FP/return-PC.)
71 (defun assign-ir2-physenv (clambda)
72 (declare (type clambda clambda))
73 (let ((lambda-physenv (lambda-physenv clambda))
74 (reversed-ir2-physenv-alist nil))
75 ;; FIXME: should be MAPCAR, not DOLIST
76 (dolist (thing (physenv-closure lambda-physenv))
77 (let ((ptype (etypecase thing
79 (if (lambda-var-indirect thing)
80 *backend-t-primitive-type*
81 (primitive-type (leaf-type thing))))
82 (nlx-info *backend-t-primitive-type*)
83 (clambda *backend-t-primitive-type*))))
84 (push (cons thing (make-normal-tn ptype))
85 reversed-ir2-physenv-alist)))
87 (let ((res (make-ir2-physenv
88 :closure (nreverse reversed-ir2-physenv-alist)
89 :return-pc-pass (make-return-pc-passing-location
91 (setf (physenv-info lambda-physenv) res)
92 (setf (ir2-physenv-old-fp res)
93 (make-old-fp-save-location lambda-physenv))
94 (setf (ir2-physenv-return-pc res)
95 (make-return-pc-save-location lambda-physenv))))
99 ;;; Return true if FUN's result is used in a tail-recursive full
100 ;;; call. We only consider explicit :FULL calls. It is assumed that
101 ;;; known calls are never part of a tail-recursive loop, so we don't
102 ;;; need to enforce tail-recursion. In any case, we don't know which
103 ;;; known calls will actually be full calls until after LTN.
104 (defun has-full-call-use (fun)
105 (declare (type clambda fun))
106 (let ((return (lambda-return fun)))
108 (do-uses (use (return-result return) nil)
109 (when (and (node-tail-p use)
110 (basic-combination-p use)
111 (eq (basic-combination-kind use) :full))
114 ;;; Return true if we should use the standard (unknown) return
115 ;;; convention for a TAIL-SET. We use the standard return convention
117 ;;; -- We must use the standard convention to preserve tail-recursion,
118 ;;; since the TAIL-SET contains both an XEP and a TR full call.
119 ;;; -- It appears to be more efficient to use the standard convention,
120 ;;; since there are no non-TR local calls that could benefit from
121 ;;; a non-standard convention.
122 ;;; -- We're compiling with RETURN-FROM-FRAME instrumentation, which
123 ;;; only works (on x86 and x86-64) for the standard convention.
124 (defun use-standard-returns (tails)
125 (declare (type tail-set tails))
126 (let ((funs (tail-set-funs tails)))
127 (or (and (find-if #'xep-p funs)
128 (find-if #'has-full-call-use funs))
129 (some (lambda (fun) (policy fun (>= insert-debug-catch 2))) funs)
132 (dolist (ref (leaf-refs fun))
133 (let* ((lvar (node-lvar ref))
134 (dest (and lvar (lvar-dest lvar))))
135 (when (and (basic-combination-p dest)
136 (not (node-tail-p dest))
137 (eq (basic-combination-fun dest) lvar)
138 (eq (basic-combination-kind dest) :local))
139 (return-from punt nil)))))))))
141 ;;; If policy indicates, give an efficiency note about our inability to
142 ;;; use the known return convention. We try to find a function in the
143 ;;; tail set with non-constant return values to use as context. If
144 ;;; there is no such function, then be more vague.
145 (defun return-value-efficiency-note (tails)
146 (declare (type tail-set tails))
147 (let ((funs (tail-set-funs tails)))
148 (when (policy (lambda-bind (first funs))
152 (let ((*compiler-error-context* (lambda-bind (first funs))))
154 "Return value count mismatch prevents known return ~
155 from these functions:~
157 (mapcar #'leaf-source-name
158 (remove-if-not #'leaf-has-source-name-p funs)))))
159 (let ((ret (lambda-return fun)))
161 (let ((rtype (return-result-type ret)))
162 (multiple-value-bind (ignore count) (values-types rtype)
163 (declare (ignore ignore))
164 (when (eq count :unknown)
165 (let ((*compiler-error-context* (lambda-bind fun)))
167 "Return type not fixed values, so can't use known return ~
169 (type-specifier rtype)))
173 ;;; Return a RETURN-INFO structure describing how we should return
174 ;;; from functions in the specified tail set. We use the unknown
175 ;;; values convention if the number of values is unknown, or if it is
176 ;;; a good idea for some other reason. Otherwise we allocate passing
177 ;;; locations for a fixed number of values.
178 (defun return-info-for-set (tails)
179 (declare (type tail-set tails))
180 (multiple-value-bind (types count) (values-types (tail-set-type tails))
181 (let ((ptypes (mapcar #'primitive-type types))
182 (use-standard (use-standard-returns tails)))
183 (when (and (eq count :unknown) (not use-standard)
184 (not (eq (tail-set-type tails) *empty-type*)))
185 (return-value-efficiency-note tails))
186 (if (or (eq count :unknown) use-standard)
187 (make-return-info :kind :unknown
190 (make-return-info :kind :fixed
193 :locations (mapcar #'make-normal-tn ptypes))))))
195 ;;; If TAIL-SET doesn't have any INFO, then make a RETURN-INFO for it.
196 ;;; If we choose a return convention other than :UNKNOWN, and this
197 ;;; environment is for an XEP, then break tail recursion on the XEP
198 ;;; calls, since we must always use unknown values when returning from
200 (defun assign-return-locations (fun)
201 (declare (type clambda fun))
202 (let* ((tails (lambda-tail-set fun))
203 (returns (or (tail-set-info tails)
204 (setf (tail-set-info tails)
205 (return-info-for-set tails))))
206 (return (lambda-return fun)))
208 (not (eq (return-info-kind returns) :unknown))
210 (do-uses (use (return-result return))
211 (setf (node-tail-p use) nil))))
214 ;;; Make an IR2-NLX-INFO structure for each NLX entry point recorded.
215 ;;; We call a VM supplied function to make the SAVE-SP restricted on
216 ;;; the stack. The NLX-ENTRY VOP's :FORCE-TO-STACK SAVE-P value
217 ;;; doesn't do this, since the SP is an argument to the VOP, and thus
218 ;;; isn't live afterwards.
219 (defun assign-ir2-nlx-info (fun)
220 (declare (type clambda fun))
221 (let ((physenv (lambda-physenv fun)))
222 (dolist (nlx (physenv-nlx-info physenv))
223 (setf (nlx-info-info nlx)
225 :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
227 (if (nlx-info-safe-p nlx)
228 (make-normal-tn *backend-t-primitive-type*)
229 (make-stack-pointer-tn)))
230 :save-sp (make-nlx-sp-tn physenv)))))