1.0.19.13: Fix WITH-ALIEN for non-x86oids.
[sbcl/eslaughter.git] / src / compiler / gtn.lisp
blob2752accd211eda8bbd3d6ca9d9b1d0472a6bc75f
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* ((type (if (lambda-var-indirect var)
44 *backend-t-primitive-type*
45 (primitive-type (leaf-type var))))
46 (temp (make-normal-tn type))
47 (node (lambda-bind fun))
48 (res (if (or (and let-p (policy node (< debug 3)))
49 (policy node (zerop debug))
50 (policy node (= speed 3)))
51 temp
52 (physenv-debug-live-tn temp (lambda-physenv fun)))))
53 (setf (tn-leaf res) var)
54 (setf (leaf-info var) res))))
55 (values))
57 ;;; Give CLAMBDA an IR2-PHYSENV structure. (And in order to
58 ;;; properly initialize the new structure, we make the TNs which hold
59 ;;; environment values and the old-FP/return-PC.)
60 (defun assign-ir2-physenv (clambda)
61 (declare (type clambda clambda))
62 (let ((lambda-physenv (lambda-physenv clambda))
63 (reversed-ir2-physenv-alist nil))
64 ;; FIXME: should be MAPCAR, not DOLIST
65 (dolist (thing (physenv-closure lambda-physenv))
66 (let ((ptype (etypecase thing
67 (lambda-var
68 (if (lambda-var-indirect thing)
69 *backend-t-primitive-type*
70 (primitive-type (leaf-type thing))))
71 (nlx-info *backend-t-primitive-type*)
72 (clambda *backend-t-primitive-type*))))
73 (push (cons thing (make-normal-tn ptype))
74 reversed-ir2-physenv-alist)))
76 (let ((res (make-ir2-physenv
77 :closure (nreverse reversed-ir2-physenv-alist)
78 :return-pc-pass (make-return-pc-passing-location
79 (xep-p clambda)))))
80 (setf (physenv-info lambda-physenv) res)
81 (setf (ir2-physenv-old-fp res)
82 (make-old-fp-save-location lambda-physenv))
83 (setf (ir2-physenv-return-pc res)
84 (make-return-pc-save-location lambda-physenv))))
86 (values))
88 ;;; Return true if FUN's result is used in a tail-recursive full
89 ;;; call. We only consider explicit :FULL calls. It is assumed that
90 ;;; known calls are never part of a tail-recursive loop, so we don't
91 ;;; need to enforce tail-recursion. In any case, we don't know which
92 ;;; known calls will actually be full calls until after LTN.
93 (defun has-full-call-use (fun)
94 (declare (type clambda fun))
95 (let ((return (lambda-return fun)))
96 (and return
97 (do-uses (use (return-result return) nil)
98 (when (and (node-tail-p use)
99 (basic-combination-p use)
100 (eq (basic-combination-kind use) :full))
101 (return t))))))
103 ;;; Return true if we should use the standard (unknown) return
104 ;;; convention for a TAIL-SET. We use the standard return convention
105 ;;; when:
106 ;;; -- We must use the standard convention to preserve tail-recursion,
107 ;;; since the TAIL-SET contains both an XEP and a TR full call.
108 ;;; -- It appears to be more efficient to use the standard convention,
109 ;;; since there are no non-TR local calls that could benefit from
110 ;;; a non-standard convention.
111 ;;; -- We're compiling with RETURN-FROM-FRAME instrumentation, which
112 ;;; only works (on x86 and x86-64) for the standard convention.
113 (defun use-standard-returns (tails)
114 (declare (type tail-set tails))
115 (let ((funs (tail-set-funs tails)))
116 (or (and (find-if #'xep-p funs)
117 (find-if #'has-full-call-use funs))
118 (some (lambda (fun) (policy fun (>= insert-debug-catch 2))) funs)
119 (block punt
120 (dolist (fun funs t)
121 (dolist (ref (leaf-refs fun))
122 (let* ((lvar (node-lvar ref))
123 (dest (and lvar (lvar-dest lvar))))
124 (when (and (basic-combination-p dest)
125 (not (node-tail-p dest))
126 (eq (basic-combination-fun dest) lvar)
127 (eq (basic-combination-kind dest) :local))
128 (return-from punt nil)))))))))
130 ;;; If policy indicates, give an efficiency note about our inability to
131 ;;; use the known return convention. We try to find a function in the
132 ;;; tail set with non-constant return values to use as context. If
133 ;;; there is no such function, then be more vague.
134 (defun return-value-efficiency-note (tails)
135 (declare (type tail-set tails))
136 (let ((funs (tail-set-funs tails)))
137 (when (policy (lambda-bind (first funs))
138 (> (max speed space)
139 inhibit-warnings))
140 (dolist (fun funs
141 (let ((*compiler-error-context* (lambda-bind (first funs))))
142 (compiler-notify
143 "Return value count mismatch prevents known return ~
144 from these functions:~
145 ~{~% ~A~}"
146 (mapcar #'leaf-source-name
147 (remove-if-not #'leaf-has-source-name-p funs)))))
148 (let ((ret (lambda-return fun)))
149 (when ret
150 (let ((rtype (return-result-type ret)))
151 (multiple-value-bind (ignore count) (values-types rtype)
152 (declare (ignore ignore))
153 (when (eq count :unknown)
154 (let ((*compiler-error-context* (lambda-bind fun)))
155 (compiler-notify
156 "Return type not fixed values, so can't use known return ~
157 convention:~% ~S"
158 (type-specifier rtype)))
159 (return)))))))))
160 (values))
162 ;;; Return a RETURN-INFO structure describing how we should return
163 ;;; from functions in the specified tail set. We use the unknown
164 ;;; values convention if the number of values is unknown, or if it is
165 ;;; a good idea for some other reason. Otherwise we allocate passing
166 ;;; locations for a fixed number of values.
167 (defun return-info-for-set (tails)
168 (declare (type tail-set tails))
169 (multiple-value-bind (types count) (values-types (tail-set-type tails))
170 (let ((ptypes (mapcar #'primitive-type types))
171 (use-standard (use-standard-returns tails)))
172 (when (and (eq count :unknown) (not use-standard)
173 (not (eq (tail-set-type tails) *empty-type*)))
174 (return-value-efficiency-note tails))
175 (if (or (eq count :unknown) use-standard)
176 (make-return-info :kind :unknown
177 :count count
178 :types ptypes)
179 (make-return-info :kind :fixed
180 :count count
181 :types ptypes
182 :locations (mapcar #'make-normal-tn ptypes))))))
184 ;;; If TAIL-SET doesn't have any INFO, then make a RETURN-INFO for it.
185 ;;; If we choose a return convention other than :UNKNOWN, and this
186 ;;; environment is for an XEP, then break tail recursion on the XEP
187 ;;; calls, since we must always use unknown values when returning from
188 ;;; an XEP.
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 (not (eq (return-info-kind returns) :unknown))
198 (xep-p fun))
199 (do-uses (use (return-result return))
200 (setf (node-tail-p use) nil))))
201 (values))
203 ;;; Make an IR2-NLX-INFO structure for each NLX entry point recorded.
204 ;;; We call a VM supplied function to make the SAVE-SP restricted on
205 ;;; the stack. The NLX-ENTRY VOP's :FORCE-TO-STACK SAVE-P value
206 ;;; doesn't do this, since the SP is an argument to the VOP, and thus
207 ;;; isn't live afterwards.
208 (defun assign-ir2-nlx-info (fun)
209 (declare (type clambda fun))
210 (let ((physenv (lambda-physenv fun)))
211 (dolist (nlx (physenv-nlx-info physenv))
212 (setf (nlx-info-info nlx)
213 (make-ir2-nlx-info
214 :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
215 '(:block :tagbody))
216 (if (nlx-info-safe-p nlx)
217 (make-normal-tn *backend-t-primitive-type*)
218 (make-stack-pointer-tn)))
219 :save-sp (make-nlx-sp-tn physenv)))))
220 (values))