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
))
44 (type (if (lambda-var-indirect var
)
45 (if (lambda-var-explicit-value-cell var
)
46 *backend-t-primitive-type
*
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))))))
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
66 (sc-or-lose 'sb
!vm
::control-stack
)))
67 (physenv-live-tn res
(lambda-physenv fun
)))
70 (physenv-debug-live-tn res
(lambda-physenv fun
))))
72 (setf (tn-leaf res
) var
)
73 (setf (leaf-info var
) res
))))
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
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
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
))))
107 ;;; Return true if we should use the standard (unknown) return
108 ;;; convention for a TAIL-SET. We use the standard return convention
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
)
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
))
145 (let ((*compiler-error-context
* (lambda-bind (first funs
))))
147 "Return value count mismatch prevents known return ~
148 from these functions:~
150 (mapcar #'leaf-source-name
151 (remove-if-not #'leaf-has-source-name-p funs
)))))
152 (let ((ret (lambda-return fun
)))
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
)))
160 "Return type not fixed values, so can't use known return ~
162 (type-specifier rtype
)))
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
183 (make-return-info :kind
:fixed
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
)))
198 (aver (eq (return-info-kind returns
) :unknown
))))
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
)
212 :home
(when (member (cleanup-kind (nlx-info-cleanup nlx
))
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
)))))