e2520f80e6977bb573eefb9c4a4fd7a4014d8079
[sbcl.git] / src / compiler / gtn.lisp
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.
5
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.
14
15 (in-package "SB!C")
16
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-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))))
29
30   (values))
31
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))
41     (when (leaf-refs var)
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)))
50                       temp
51                       (environment-debug-live-tn temp
52                                                  (lambda-environment fun)))))
53         (setf (tn-leaf res) var)
54         (setf (leaf-info var) res))))
55   (values))
56
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)))
62     (collect ((env))
63       (dolist (thing (environment-closure env))
64         (let ((ptype (etypecase thing
65                        (lambda-var
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)))))
71
72       (let ((res (make-ir2-environment
73                   :environment (env)
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)))))
81
82   (values))
83
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)))
92     (and return
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))
97              (return t))))))
98
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
105 ;;;    convention.
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))
111         (block punt
112           (dolist (fun funs t)
113             (dolist (ref (leaf-refs fun))
114               (let* ((cont (node-cont ref))
115                      (dest (continuation-dest cont)))
116                 (when (and dest
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)))))))))
122
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))
131                   (> (max speed space)
132                      inhibit-warnings))
133       (dolist (fun funs
134                    (let ((*compiler-error-context* (lambda-bind (first funs))))
135                      (compiler-note
136                       "Return value count mismatch prevents known return ~
137                        from these functions:~
138                        ~{~%  ~A~}"
139                       (remove nil (mapcar #'leaf-name funs)))))
140         (let ((ret (lambda-return fun)))
141           (when ret
142             (let ((rtype (return-result-type ret)))
143               (multiple-value-bind (ignore count) (values-types rtype)
144                 (declare (ignore ignore))
145                 (when (eq count :unknown)
146                   (let ((*compiler-error-context* (lambda-bind fun)))
147                     (compiler-note
148                      "Return type not fixed values, so can't use known return ~
149                       convention:~%  ~S"
150                      (type-specifier rtype)))
151                   (return)))))))))
152   (values))
153
154 ;;; Return a Return-Info structure describing how we should return from
155 ;;; functions in the specified tail set. We use the unknown values convention
156 ;;; if the number of values is unknown, or if it is a good idea for some other
157 ;;; reason. Otherwise we allocate passing locations for a fixed number of
158 ;;; values.
159 (defun return-info-for-set (tails)
160   (declare (type tail-set tails))
161   (multiple-value-bind (types count) (values-types (tail-set-type tails))
162     (let ((ptypes (mapcar #'primitive-type types))
163           (use-standard (use-standard-returns tails)))
164       (when (and (eq count :unknown) (not use-standard))
165         (return-value-efficency-note tails))
166       (if (or (eq count :unknown) use-standard)
167           (make-return-info :kind :unknown
168                             :count count
169                             :types ptypes)
170           (make-return-info :kind :fixed
171                             :count count
172                             :types ptypes
173                             :locations (mapcar #'make-normal-tn ptypes))))))
174
175 ;;; If Tail-Set doesn't have any Info, then make a Return-Info for it. If
176 ;;; we choose a return convention other than :Unknown, and this environment is
177 ;;; for an XEP, then break tail recursion on the XEP calls, since we must
178 ;;; always use unknown values when returning from an XEP.
179 (defun assign-return-locations (fun)
180   (declare (type clambda fun))
181   (let* ((tails (lambda-tail-set fun))
182          (returns (or (tail-set-info tails)
183                       (setf (tail-set-info tails)
184                             (return-info-for-set tails))))
185          (return (lambda-return fun)))
186     (when (and return
187                (not (eq (return-info-kind returns) :unknown))
188                (external-entry-point-p fun))
189       (do-uses (use (return-result return))
190         (setf (node-tail-p use) nil))))
191   (values))
192
193 ;;; Make an IR2-NLX-Info structure for each NLX entry point recorded. We
194 ;;; call a VM supplied function to make the Save-SP restricted on the stack.
195 ;;; The NLX-Entry VOP's :Force-To-Stack Save-P value doesn't do this, since the
196 ;;; SP is an argument to the VOP, and thus isn't live afterwards.
197 (defun assign-ir2-nlx-info (fun)
198   (declare (type clambda fun))
199   (let ((env (lambda-environment fun)))
200     (dolist (nlx (environment-nlx-info env))
201       (setf (nlx-info-info nlx)
202             (make-ir2-nlx-info
203              :home (when (member (cleanup-kind (nlx-info-cleanup nlx))
204                                  '(:block :tagbody))
205                      (make-normal-tn *backend-t-primitive-type*))
206              :save-sp (make-nlx-sp-tn env)))))
207   (values))