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-environment 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-Environment so that we can close over TNs that haven't had their
34 ;;; home environment assigned yet. Here we evaluate the DEBUG-INFO/SPEED
35 ;;; tradeoff to determine how variables are allocated. If SPEED is 3, then all
36 ;;; variables are subject to lifetime analysis. Otherwise, only Let-P variables
37 ;;; are allocated normally, and that can be inhibited by DEBUG-INFO = 3.
38 (defun assign-lambda-var-tns (fun let-p)
39 (declare (type clambda fun))
40 (dolist (var (lambda-vars fun))
42 (let* ((type (if (lambda-var-indirect var)
43 *backend-t-primitive-type*
44 (primitive-type (leaf-type var))))
45 (temp (make-normal-tn type))
46 (node (lambda-bind fun))
47 (res (if (or (and let-p (policy node (< debug 3)))
48 (policy node (zerop debug))
49 (policy node (= speed 3)))
51 (environment-debug-live-tn temp
52 (lambda-environment fun)))))
53 (setf (tn-leaf res) var)
54 (setf (leaf-info var) res))))
57 ;;; Give an IR2-Environment structure to Fun. We make the TNs which hold
58 ;;; environment values and the old-FP/return-PC.
59 (defun assign-ir2-environment (fun)
60 (declare (type clambda fun))
61 (let ((env (lambda-environment fun)))
63 (dolist (thing (environment-closure env))
64 (let ((ptype (etypecase thing
66 (if (lambda-var-indirect thing)
67 *backend-t-primitive-type*
68 (primitive-type (leaf-type thing))))
69 (nlx-info *backend-t-primitive-type*))))
70 (env (cons thing (make-normal-tn ptype)))))
72 (let ((res (make-ir2-environment
74 :return-pc-pass (make-return-pc-passing-location
75 (external-entry-point-p fun)))))
76 (setf (environment-info env) res)
77 (setf (ir2-environment-old-fp res)
78 (make-old-fp-save-location env))
79 (setf (ir2-environment-return-pc res)
80 (make-return-pc-save-location env)))))
84 ;;; Return true if Fun's result continuation is used in a TR full call. We
85 ;;; only consider explicit :Full calls. It is assumed that known calls are
86 ;;; never part of a tail-recursive loop, so we don't need to enforce
87 ;;; tail-recursion. In any case, we don't know which known calls will
88 ;;; actually be full calls until after LTN.
89 (defun has-full-call-use (fun)
90 (declare (type clambda fun))
91 (let ((return (lambda-return fun)))
93 (do-uses (use (return-result return) nil)
94 (when (and (node-tail-p use)
95 (basic-combination-p use)
96 (eq (basic-combination-kind use) :full))
99 ;;; Return true if we should use the standard (unknown) return convention
100 ;;; for a tail-set. We use the standard return convention when:
101 ;;; -- We must use the standard convention to preserve tail-recursion, since
102 ;;; the tail-set contains both an XEP and a TR full call.
103 ;;; -- It appears to be more efficient to use the standard convention, since
104 ;;; there are no non-TR local calls that could benefit from a non-standard
106 (defun use-standard-returns (tails)
107 (declare (type tail-set tails))
108 (let ((funs (tail-set-functions tails)))
109 (or (and (find-if #'external-entry-point-p funs)
110 (find-if #'has-full-call-use funs))
113 (dolist (ref (leaf-refs fun))
114 (let* ((cont (node-cont ref))
115 (dest (continuation-dest cont)))
117 (not (node-tail-p dest))
118 (basic-combination-p dest)
119 (eq (basic-combination-fun dest) cont)
120 (eq (basic-combination-kind dest) :local))
121 (return-from punt nil)))))))))
123 ;;; If policy indicates, give an efficency note about our inability to use
124 ;;; the known return convention. We try to find a function in the tail set
125 ;;; with non-constant return values to use as context. If there is no such
126 ;;; function, then be more vague.
127 (defun return-value-efficency-note (tails)
128 (declare (type tail-set tails))
129 (let ((funs (tail-set-functions tails)))
130 (when (policy (lambda-bind (first funs)) (> (max speed space)
133 (let ((*compiler-error-context* (lambda-bind (first funs))))
135 "Return value count mismatch prevents known return ~
136 from these functions:~
138 (remove nil (mapcar #'leaf-name funs)))))
139 (let ((ret (lambda-return fun)))
141 (let ((rtype (return-result-type ret)))
142 (multiple-value-bind (ignore count) (values-types rtype)
143 (declare (ignore ignore))
144 (when (eq count :unknown)
145 (let ((*compiler-error-context* (lambda-bind fun)))
147 "Return type not fixed values, so can't use known return ~
149 (type-specifier rtype)))
153 ;;; Return a Return-Info structure describing how we should return from
154 ;;; functions in the specified tail set. We use the unknown values convention
155 ;;; if the number of values is unknown, or if it is a good idea for some other
156 ;;; reason. Otherwise we allocate passing locations for a fixed number of
158 (defun return-info-for-set (tails)
159 (declare (type tail-set tails))
160 (multiple-value-bind (types count) (values-types (tail-set-type tails))
161 (let ((ptypes (mapcar #'primitive-type types))
162 (use-standard (use-standard-returns tails)))
163 (when (and (eq count :unknown) (not use-standard))
164 (return-value-efficency-note tails))
165 (if (or (eq count :unknown) use-standard)
166 (make-return-info :kind :unknown
169 (make-return-info :kind :fixed
172 :locations (mapcar #'make-normal-tn ptypes))))))
174 ;;; If Tail-Set doesn't have any Info, then make a Return-Info for it. If
175 ;;; we choose a return convention other than :Unknown, and this environment is
176 ;;; for an XEP, then break tail recursion on the XEP calls, since we must
177 ;;; always use unknown values when returning from an XEP.
178 (defun assign-return-locations (fun)
179 (declare (type clambda fun))
180 (let* ((tails (lambda-tail-set fun))
181 (returns (or (tail-set-info tails)
182 (setf (tail-set-info tails)
183 (return-info-for-set tails))))
184 (return (lambda-return fun)))
186 (not (eq (return-info-kind returns) :unknown))
187 (external-entry-point-p fun))
188 (do-uses (use (return-result return))
189 (setf (node-tail-p use) nil))))
192 ;;; Make an IR2-NLX-Info structure for each NLX entry point recorded. We
193 ;;; call a VM supplied function to make the Save-SP restricted on the stack.
194 ;;; The NLX-Entry VOP's :Force-To-Stack Save-P value doesn't do this, since the
195 ;;; SP is an argument to the VOP, and thus isn't live afterwards.
196 (defun assign-ir2-nlx-info (fun)
197 (declare (type clambda fun))
198 (let ((env (lambda-environment fun)))
199 (dolist (nlx (environment-nlx-info env))
200 (setf (nlx-info-info nlx)
202 :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
204 (make-normal-tn *backend-t-primitive-type*))
205 :save-sp (make-nlx-sp-tn env)))))