1.0.6.13: minor fix to the compiler's interval-arithmetic
[sbcl/simd.git] / src / compiler / gtn.lisp
blob1a22dd005c955597f9e1f13582fa62987fb1542f
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 (defun use-standard-returns (tails)
112 (declare (type tail-set tails))
113 (let ((funs (tail-set-funs tails)))
114 (or (and (find-if #'xep-p funs)
115 (find-if #'has-full-call-use funs))
116 (block punt
117 (dolist (fun funs t)
118 (dolist (ref (leaf-refs fun))
119 (let* ((lvar (node-lvar ref))
120 (dest (and lvar (lvar-dest lvar))))
121 (when (and (basic-combination-p dest)
122 (not (node-tail-p dest))
123 (eq (basic-combination-fun dest) lvar)
124 (eq (basic-combination-kind dest) :local))
125 (return-from punt nil)))))))))
127 ;;; If policy indicates, give an efficiency note about our inability to
128 ;;; use the known return convention. We try to find a function in the
129 ;;; tail set with non-constant return values to use as context. If
130 ;;; there is no such function, then be more vague.
131 (defun return-value-efficiency-note (tails)
132 (declare (type tail-set tails))
133 (let ((funs (tail-set-funs tails)))
134 (when (policy (lambda-bind (first funs))
135 (> (max speed space)
136 inhibit-warnings))
137 (dolist (fun funs
138 (let ((*compiler-error-context* (lambda-bind (first funs))))
139 (compiler-notify
140 "Return value count mismatch prevents known return ~
141 from these functions:~
142 ~{~% ~A~}"
143 (mapcar #'leaf-source-name
144 (remove-if-not #'leaf-has-source-name-p funs)))))
145 (let ((ret (lambda-return fun)))
146 (when ret
147 (let ((rtype (return-result-type ret)))
148 (multiple-value-bind (ignore count) (values-types rtype)
149 (declare (ignore ignore))
150 (when (eq count :unknown)
151 (let ((*compiler-error-context* (lambda-bind fun)))
152 (compiler-notify
153 "Return type not fixed values, so can't use known return ~
154 convention:~% ~S"
155 (type-specifier rtype)))
156 (return)))))))))
157 (values))
159 ;;; Return a RETURN-INFO structure describing how we should return
160 ;;; from functions in the specified tail set. We use the unknown
161 ;;; values convention if the number of values is unknown, or if it is
162 ;;; a good idea for some other reason. Otherwise we allocate passing
163 ;;; locations for a fixed number of values.
164 (defun return-info-for-set (tails)
165 (declare (type tail-set tails))
166 (multiple-value-bind (types count) (values-types (tail-set-type tails))
167 (let ((ptypes (mapcar #'primitive-type types))
168 (use-standard (use-standard-returns tails)))
169 (when (and (eq count :unknown) (not use-standard)
170 (not (eq (tail-set-type tails) *empty-type*)))
171 (return-value-efficiency-note tails))
172 (if (or (eq count :unknown) use-standard)
173 (make-return-info :kind :unknown
174 :count count
175 :types ptypes)
176 (make-return-info :kind :fixed
177 :count count
178 :types ptypes
179 :locations (mapcar #'make-normal-tn ptypes))))))
181 ;;; If TAIL-SET doesn't have any INFO, then make a RETURN-INFO for it.
182 ;;; If we choose a return convention other than :UNKNOWN, and this
183 ;;; environment is for an XEP, then break tail recursion on the XEP
184 ;;; calls, since we must always use unknown values when returning from
185 ;;; an XEP.
186 (defun assign-return-locations (fun)
187 (declare (type clambda fun))
188 (let* ((tails (lambda-tail-set fun))
189 (returns (or (tail-set-info tails)
190 (setf (tail-set-info tails)
191 (return-info-for-set tails))))
192 (return (lambda-return fun)))
193 (when (and return
194 (not (eq (return-info-kind returns) :unknown))
195 (xep-p fun))
196 (do-uses (use (return-result return))
197 (setf (node-tail-p use) nil))))
198 (values))
200 ;;; Make an IR2-NLX-INFO structure for each NLX entry point recorded.
201 ;;; We call a VM supplied function to make the SAVE-SP restricted on
202 ;;; the stack. The NLX-ENTRY VOP's :FORCE-TO-STACK SAVE-P value
203 ;;; doesn't do this, since the SP is an argument to the VOP, and thus
204 ;;; isn't live afterwards.
205 (defun assign-ir2-nlx-info (fun)
206 (declare (type clambda fun))
207 (let ((physenv (lambda-physenv fun)))
208 (dolist (nlx (physenv-nlx-info physenv))
209 (setf (nlx-info-info nlx)
210 (make-ir2-nlx-info
211 :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
212 '(:block :tagbody))
213 (if (nlx-info-safe-p nlx)
214 (make-normal-tn *backend-t-primitive-type*)
215 (make-stack-pointer-tn)))
216 :save-sp (make-nlx-sp-tn physenv)))))
217 (values))