Move SFUNCTION type earlier, use it more.
[sbcl.git] / src / compiler / policy.lisp
bloba08c8eb1e444a2b457c7787592a02621f80f2b19
1 ;;;; compiler optimization policy stuff
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!C")
14 ;;; a value for an optimization declaration
15 (def!type policy-quality () '(integer 0 3))
17 (defvar *macro-policy* nil)
18 ;;; global policy restrictions as a POLICY object or nil
19 (!defvar *policy-restrictions* nil)
21 (defun restrict-compiler-policy (&optional quality (min 0))
22 #!+sb-doc
23 "Assign a minimum value to an optimization quality. QUALITY is the name of
24 the optimization quality to restrict, and MIN (defaulting to zero) is the
25 minimum allowed value.
27 Returns the alist describing the current policy restrictions.
29 If QUALITY is NIL or not given, nothing is done.
31 Otherwise, if MIN is zero or not given, any existing restrictions of QUALITY
32 are removed. If MIN is between one and three inclusive, it becomes the new
33 minimum value for the optimization quality: any future proclamations or
34 declarations of the quality with a value less then MIN behave as if the value
35 was MIN instead.
37 This is intended to be used interactively, to facilitate recompiling large
38 bodies of code with eg. a known minimum safety.
40 See also :POLICY option in WITH-COMPILATION-UNIT.
42 EXPERIMENTAL INTERFACE: Subject to change."
43 (declare (type policy-quality min))
44 (when quality
45 (unless (policy-quality-name-p quality)
46 (error "~S is not a policy quality" quality))
47 ;; The dynamic policy object is immutable, otherwise a construct like
48 ;; (let ((*policy-restrictions* *policy-restrictions*)) ...)
49 ;; could allow alterations inside the LET to leak out.
50 ;; The structure itself does not declare slots its read-only, because
51 ;; OPTIMIZE declaration processing uses it as a scratchpad.
52 (setf *policy-restrictions*
53 (acond (*policy-restrictions* (copy-structure it))
54 (t (make-policy 0 0))))
55 (alter-policy *policy-restrictions* (policy-quality-name-p quality)
56 min (plusp min)))
57 ;; Return dotted pairs, not elements that look declaration-like.
58 (if *policy-restrictions*
59 (mapc (lambda (x) (rplacd x (cadr x)))
60 (policy-to-decl-spec *policy-restrictions*))
61 '()))
63 (defstruct (policy-dependent-quality (:copier nil))
64 (name nil :type symbol :read-only t)
65 (expression nil :read-only t)
66 (getter nil :read-only t)
67 (values-documentation nil :read-only t))
69 ;;; names of recognized optimization policy qualities
70 (declaim (simple-vector **policy-dependent-qualities**))
71 (defglobal **policy-dependent-qualities** #())
73 ;; Return POLICY as a list suitable to the OPTIMIZE declaration.
74 ;; If FORCE-ALL then include qualities without an explicit value too.
75 (defun policy-to-decl-spec (policy &optional (raw t) force-all)
76 (loop with presence = (policy-presence-bits policy)
77 for index from (- n-policy-primary-qualities)
78 below (length **policy-dependent-qualities**)
79 when (or force-all (logbitp (mod index max-policy-qualities) presence))
80 collect
81 (list (if (minusp index)
82 (elt **policy-primary-qualities** (lognot index))
83 (policy-dependent-quality-name
84 (elt **policy-dependent-qualities** index)))
85 (if raw
86 ;; Raw values are insensitive to *POLICY-RESTRICTIONS*.
87 (%%policy-quality policy index)
88 ;; Otherwise take the adjusted quality.
89 (%policy-quality policy index)))))
91 ;; Return T if P1 and P2 are policies which are specified to be the same.
92 ;; A result of NIL does not imply that definitely P1 /= P2
93 ;; because a multitude of policies can be effectively equal.
94 ;; [Any dependent quality might be specified the same as its computed
95 ;; value in the absence of an explicit value.]
96 (defun policy= (p1 p2)
97 (or (and p1 p2
98 (= (policy-primary-qualities p1) (policy-primary-qualities p2))
99 (= (policy-dependent-qualities p1) (policy-dependent-qualities p2))
100 (= (policy-presence-bits p1) (policy-presence-bits p2)))
101 (and (null p1) (null p2))))
103 ;;; Is X the name of an optimization policy quality?
104 ;;; If it is, return the integer identifier for the quality name.
105 (defun policy-quality-name-p (x)
106 ;; Standard (and non-standard) primary qualities are numbered from -1 down.
107 (or (awhen (position x **policy-primary-qualities** :test #'eq)
108 (lognot it))
109 ;; Dependent qualities are numbered from 0 up.
110 (position x **policy-dependent-qualities**
111 :key #'policy-dependent-quality-name)))
113 ;; Destructively modify POLICY such that quality INDEX has VALUE,
114 ;; and the specified PRESENTP bit.
115 (defun alter-policy (policy index value &optional (presentp t))
116 (if (minusp index) ; a primary quality
117 (setf (ldb (byte 2 (* 2 (lognot index)))
118 (policy-primary-qualities policy)) value)
119 (setf (ldb (byte 2 (* 2 index))
120 (policy-dependent-qualities policy)) value))
121 ;; Some cross-compilation hosts can't execute (SETF (LOGBITP ...)).
122 (setf (ldb (byte 1 (mod index max-policy-qualities))
123 (policy-presence-bits policy)) (if presentp 1 0))
124 policy)
126 ;;; Is it deprecated?
127 (declaim (ftype function deprecation-warn))
128 (defun policy-quality-deprecation-warning (quality)
129 (case quality
130 ((stack-allocate-dynamic-extent stack-allocate-vector stack-allocate-value-cells)
131 (deprecation-warn :late "SBCL" "1.0.19.7" 'policy quality '*stack-allocate-dynamic-extent*
132 :runtime-error nil)
134 ((merge-tail-calls)
135 (deprecation-warn :early "SBCL" "1.0.53.74" 'policy quality nil :runtime-error nil)
137 (otherwise
138 nil)))
140 ;;; *POLICY* holds the current global compiler policy information, as
141 ;;; a POLICY object mapping from the compiler-assigned index (unique per
142 ;;; quality name) to quality value.
143 ;;; This used to be an alist, but tail-sharing was never really possible
144 ;;; because for deterministic comparison the list was always freshly
145 ;;; consed so that destructive sorting could be done for canonicalization.
146 (declaim (type policy *policy*)
147 (type (or policy null) *policy-restrictions*))
149 ;; ANSI-specified default of 1 for each quality.
150 (defglobal **baseline-policy** nil)
151 ;; Baseline policy altered with (TYPE-CHECK 0)
152 (defglobal **zero-typecheck-policy** nil)
153 #-sb-xc-host (declaim (type policy **baseline-policy**))
155 ;;; This is to be called early in cold init to set things up, and may
156 ;;; also be called again later in cold init in order to reset default
157 ;;; optimization policy back to default values after toplevel PROCLAIM
158 ;;; OPTIMIZE forms have messed with it.
159 (defun !policy-cold-init-or-resanify ()
160 (setq **baseline-policy**
161 (make-policy (loop for i below n-policy-primary-qualities
162 sum (ash #b01 (* i 2))))
163 **zero-typecheck-policy**
164 (alter-policy (copy-policy **baseline-policy**)
165 #-sb-xc (policy-quality-name-p 'type-check)
166 ;; Eval in the host since cold-init won't have
167 ;; executed any forms in 'policies.lisp'
168 #+sb-xc #.(policy-quality-name-p 'type-check)
171 ;; CMU CL didn't use 1 as the default for everything,
172 ;; but since ANSI says 1 is the ordinary value, we do.
173 (setf *policy* (copy-policy **baseline-policy**)))
175 ;;; Look up a named optimization quality in POLICY. This is only
176 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
177 ;;; it's an error if it's called for a quality which isn't defined.
178 (defun policy-quality (policy quality-name)
179 (%policy-quality policy
180 (the fixnum (policy-quality-name-p quality-name))))
182 (define-compiler-macro policy-quality (&whole form policy quality-name)
183 (acond ((and (constantp quality-name)
184 ;; CONSTANT-FORM-VALUE can not be called here when building
185 ;; the cross-compiler, but EVAL can safely be used
186 ;; since our own source code is known not to be screwy.
187 (policy-quality-name-p (#-sb-xc-host constant-form-value
188 #+sb-xc-host eval quality-name)))
189 `(%policy-quality ,policy ,it))
191 form)))
193 (macrolet ((extract-field (floor-expression-primary
194 floor-expression-dependent)
195 `(if (minusp index)
196 (let ((byte-pos (* (lognot index) 2)))
197 (max (ldb (byte 2 byte-pos)
198 (policy-primary-qualities policy))
199 ,floor-expression-primary))
200 (let ((byte-pos (* index 2)))
201 (max (if (logbitp index (policy-presence-bits policy))
202 (ldb (byte 2 byte-pos)
203 (policy-dependent-qualities policy))
205 ,floor-expression-dependent))))
206 (define-getter (name &body body)
207 `(defun ,name (policy index)
208 (declare (type policy policy)
209 (type (integer
210 #.(- n-policy-primary-qualities)
211 #.(- max-policy-qualities
212 n-policy-primary-qualities 1))
213 index))
214 ,@body)))
216 ;; Return the value for quality INDEX in POLICY, using *POLICY-RESTRICTIONS*
217 ;; Primary qualities are assumed to exist, however policy-restricting functions
218 ;; can create a POLICY that indicates absence of primary qualities.
219 ;; This does not affect RESTRICT-COMPILER-POLICY because a lower bound of 0
220 ;; can be assumed for everything. SET-MACRO-POLICY might care though.
221 (define-getter %policy-quality
222 (let ((floor *policy-restrictions*))
223 (macrolet ((quality-floor (get-byte)
224 `(if floor (ldb (byte 2 byte-pos) (,get-byte floor)) 0)))
225 (extract-field (quality-floor policy-primary-qualities)
226 (quality-floor policy-dependent-qualities)))))
228 ;; Return the unadjusted value for quality INDEX in POLICY.
229 ;; This is used for converting a policy to a list of elements for display
230 ;; and for verifying that after processing declarations, the new policy
231 ;; matches the given declarations, thus implying no ambiguity.
232 (define-getter %%policy-quality
233 (extract-field 0 0))) ; floor is always 0
235 ;;; Forward declaration of %COERCE-TO-POLICY.
236 ;;; Definition is in 'node' so that FUNCTIONAL and NODE types are defined.
237 ;;; Arg is declared of type T because the function explicitly checks it.
238 (declaim (ftype (sfunction (t) policy) %coerce-to-policy))
240 ;;; syntactic sugar for querying optimization policy qualities
242 ;;; Evaluate EXPR in terms of the optimization policy associated with
243 ;;; THING. EXPR is a form which accesses optimization qualities by
244 ;;; referring to them by name, e.g. (> SPEED SPACE).
245 (defmacro policy (thing expr &optional (coercion-fn '%coerce-to-policy))
246 (let* ((n-policy (make-symbol "P"))
247 (binds (loop for name across **policy-primary-qualities**
248 for index downfrom -1
249 collect `(,name (%policy-quality ,n-policy ,index))))
250 (dependent-binds
251 (loop for info across **policy-dependent-qualities**
252 for name = (policy-dependent-quality-name info)
253 collect `(,name (let ((,name (policy-quality ,n-policy ',name)))
254 (if (= ,name 1)
255 ,(policy-dependent-quality-expression info)
256 ,name))))))
257 `(let ((,n-policy (,coercion-fn ,thing)))
258 ;; FIXME: automatically inserted IGNORABLE decls are
259 ;; often suggestive of poor style, as is this one.
260 (declare (ignorable ,n-policy))
261 (symbol-macrolet (,@binds ,@dependent-binds)
262 ,expr))))
264 ;;; Dependent qualities
265 (defmacro define-optimization-quality
266 (name expression &optional values-documentation documentation)
267 (declare (ignorable documentation))
268 `(eval-when (:compile-toplevel :load-toplevel :execute)
269 (let ((number (policy-quality-name-p ',name))
270 (item (make-policy-dependent-quality
271 :name ',name
272 :expression ',expression
273 ;; DESCRIBE-COMPILER-POLICY uses the getter
274 :getter (named-lambda ,(string name) (policy)
275 (policy policy ,expression))
276 :values-documentation ',values-documentation)))
277 (if number
278 (setf (svref **policy-dependent-qualities** number) item)
279 ;; This array is reallocated every time a policy is added,
280 ;; but that's fine - it's not a performance issue.
281 (let ((size (1+ (length **policy-dependent-qualities**))))
282 ;; Don't overrun the packed bit fields.
283 (when (> (+ n-policy-primary-qualities size) max-policy-qualities)
284 (error "Maximum number of policy qualities exceeded."))
285 (setf **policy-dependent-qualities**
286 (replace (make-array size :initial-element item)
287 **policy-dependent-qualities**)))))
288 #-sb-xc-host
289 ,@(when documentation `((setf (fdocumentation ',name 'optimize) ,documentation)))
290 ',name))
292 ;;; Return a new POLICY containing the policy information represented
293 ;;; by the optimize declaration SPEC. Any parameters not specified are
294 ;;; defaulted from the POLICY argument.
295 (declaim (ftype (function (list (or policy null)) (values policy list))
296 process-optimize-decl))
297 (defun process-optimize-decl (spec policy)
298 (let ((result (copy-policy (or policy **baseline-policy**)))
299 (specified-qualities))
300 ;; Add new entries from SPEC.
301 (dolist (q-and-v-or-just-q (cdr spec) (values result specified-qualities))
302 (multiple-value-bind (quality raw-value)
303 (if (atom q-and-v-or-just-q)
304 (values q-and-v-or-just-q 3)
305 (destructuring-bind (quality raw-value) q-and-v-or-just-q
306 (values quality raw-value)))
307 (let ((index (policy-quality-name-p quality)))
308 (cond ((not index)
309 (or (policy-quality-deprecation-warning quality)
310 (compiler-warn
311 "~@<Ignoring unknown optimization quality ~S in:~_ ~S~:>"
312 quality spec)))
313 ((not (typep raw-value 'policy-quality))
314 (compiler-warn
315 "~@<Ignoring bad optimization value ~S in:~_ ~S~:>"
316 raw-value spec))
318 ;; we can't do this yet, because CLOS macros expand
319 ;; into code containing INHIBIT-WARNINGS.
320 #+nil
321 (when (eql quality 'inhibit-warnings)
322 (compiler-style-warn "~S is deprecated: use ~S instead"
323 quality 'muffle-conditions))
324 (push (cons quality raw-value) specified-qualities)
325 (alter-policy result index raw-value))))))))
327 (defvar *macro-policy* nil)
328 ;; Set an alternate policy that is used to compile all code within DEFMACRO,
329 ;; MACROLET, DEFINE-COMPILER-MARO - whether they occur at toplevel or not -
330 ;; as well as execute all toplevel code in eval-when situation :COMPILE-TOPLEVEL,
331 ;; including such code as emitted into a '.cfasl' file.
332 ;; e.g. (SET-MACRO-POLICY '((SPEED 0) (SAFETY 3))) ensures full error checking
333 ;; regardless of prevailing local policy in situations such as
334 ;; (macrolet ((frob (a b) (declare (type (member :up :down) a)) ...)
336 ;; Todo: it would be nice to allow NOTINLINE, which can be broadly achieved by
337 ;; setting (SPEED 0), but nonetheless more targeted settings should be possible.
338 ;; Same for {UN}MUFFLE-CONDITIONS or anything else that can be proclaimed.
340 (defun set-macro-policy (list)
341 ;: Note that *MACRO-POLICY* does not represent absence of any primary quality,
342 ;; and therefore whenever it is injected into a macro, you get all baseline
343 ;; values of 1, augmented by the specified changes.
344 ;; There are two alternative behaviors that might make sense:
345 ;; - use the value of *POLICY* when SET-MACRO-POLICY is called as the baseline
346 ;; augmented by the specifiers in LIST
347 ;; - use the lexical policy at the time of expansion, augmented by LIST
348 ;; But most probably the current behavior is entirely reasonable.
349 (setq *macro-policy* (process-optimize-decl `(optimize ,@list)
350 **baseline-policy**)))
352 ;; Turn the macro policy into an OPTIMIZE declaration for insertion
353 ;; into a macro body for DEFMACRO, MACROLET, or DEFINE-COMPILER-MACRO.
354 ;; Note that despite it being a style-warning to insert a duplicate,
355 ;; we need no precaution against that even though users may write
356 ;; (DEFMACRO FOO (X) (DECLARE (OPTIMIZE (SAFETY 1))) ...)
357 ;; The expansion of macro-defining forms is such that the macro-policy
358 ;; appears in a different lexical scope from the user's declarations.
359 (defun macro-policy-decls ()
360 (and *macro-policy*
361 `((declare (optimize ,@(policy-to-decl-spec *macro-policy*))))))