Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / compiler / policy.lisp
blobe4b3d98c4a899996a7186250fda1dd3e933575c0
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 *policy*)
18 (defvar *macro-policy* nil)
19 ;;; global policy restrictions as a POLICY object or nil
20 (!defvar *policy-restrictions* nil)
22 (defun restrict-compiler-policy (&optional quality (min 0))
23 #!+sb-doc
24 "Assign a minimum value to an optimization quality. QUALITY is the name of
25 the optimization quality to restrict, and MIN (defaulting to zero) is the
26 minimum allowed value.
28 Returns the alist describing the current policy restrictions.
30 If QUALITY is NIL or not given, nothing is done.
32 Otherwise, if MIN is zero or not given, any existing restrictions of QUALITY
33 are removed. If MIN is between one and three inclusive, it becomes the new
34 minimum value for the optimization quality: any future proclamations or
35 declarations of the quality with a value less then MIN behave as if the value
36 was MIN instead.
38 This is intended to be used interactively, to facilitate recompiling large
39 bodies of code with eg. a known minimum safety.
41 See also :POLICY option in WITH-COMPILATION-UNIT.
43 EXPERIMENTAL INTERFACE: Subject to change."
44 (declare (type policy-quality min))
45 (when quality
46 (unless (policy-quality-name-p quality)
47 (error "~S is not a policy quality" quality))
48 ;; The dynamic policy object is immutable, otherwise a construct like
49 ;; (let ((*policy-restrictions* *policy-restrictions*)) ...)
50 ;; could allow alterations inside the LET to leak out.
51 ;; The structure itself does not declare slots its read-only, because
52 ;; OPTIMIZE declaration processing uses it as a scratchpad.
53 (setf *policy-restrictions*
54 (acond (*policy-restrictions* (copy-structure it))
55 (t (make-policy 0 0))))
56 (alter-policy *policy-restrictions* (policy-quality-name-p quality)
57 min (plusp min)))
58 ;; Return dotted pairs, not elements that look declaration-like.
59 (if *policy-restrictions*
60 (mapc (lambda (x) (rplacd x (cadr x)))
61 (policy-to-decl-spec *policy-restrictions*))
62 '()))
64 (defstruct (policy-dependent-quality (:copier nil))
65 (name nil :type symbol :read-only t)
66 (expression nil :read-only t)
67 (getter nil :read-only t)
68 (values-documentation nil :read-only t))
70 ;;; names of recognized optimization policy qualities
71 (declaim (simple-vector **policy-primary-qualities**
72 **policy-dependent-qualities**))
73 (!defglobal **policy-primary-qualities**
74 #(;; ANSI standard qualities
75 compilation-speed
76 debug
77 safety
78 space
79 speed
80 ;; SBCL extensions
82 ;; FIXME: INHIBIT-WARNINGS is a misleading name for this.
83 ;; Perhaps BREVITY would be better. But the ideal name would
84 ;; have connotations of suppressing not warnings but only
85 ;; optimization-related notes, which is already mostly the
86 ;; behavior, and should probably become the exact behavior.
87 ;; Perhaps INHIBIT-NOTES?
88 inhibit-warnings))
89 (defglobal **policy-dependent-qualities** #())
90 (eval-when (:compile-toplevel :load-toplevel :execute)
91 (defconstant n-policy-primary-qualities (length **policy-primary-qualities**))
92 ;; 1 bit per quality is stored to indicate whether it was explicitly given
93 ;; a value in a lexical policy. In addition to the 5 ANSI-standard qualities,
94 ;; SBCL defines one more "primary" quality and 16 dependent qualities.
95 ;; Both kinds take up 1 bit in the mask of specified qualities.
96 (defconstant max-policy-qualities 32))
98 ;; Each primary and dependent quality policy is assigned a small integer index.
99 ;; The POLICY struct represents a set of policies in an order-insensitive way
100 ;; that facilitates quicker lookup than scanning an alist.
101 (defstruct (policy (:constructor make-policy
102 (primary-qualities &optional presence-bits)))
103 ;; Mask with a 1 for each quality that has an explicit value in this policy.
104 ;; Primary qualities fill the mask from left-to-right and dependent qualities
105 ;; from right-to-left.
106 ;; xc has trouble folding this MASK-FIELD, but it works when host-evaluated.
107 (presence-bits #.(mask-field
108 (byte n-policy-primary-qualities
109 (- max-policy-qualities n-policy-primary-qualities))
111 :type (unsigned-byte #.max-policy-qualities))
112 ;; For efficiency, primary qualities are segregated because there are few
113 ;; enough of them to fit in a fixnum.
114 (primary-qualities 0 :type (unsigned-byte #.(* 2 n-policy-primary-qualities)))
115 ;; 2 bits per dependent quality is a fixnum on 64-bit build, not on 32-bit.
116 ;; It would certainly be possible to constrain this to storing exactly
117 ;; the 16 currently defined dependent qualities,
118 ;; but that would be overly limiting.
119 (dependent-qualities 0
120 :type (unsigned-byte #.(* (- max-policy-qualities n-policy-primary-qualities)
121 2))))
123 ;; Return POLICY as a list suitable to the OPTIMIZE declaration.
124 ;; If FORCE-ALL then include qualities without an explicit value too.
125 (defun policy-to-decl-spec (policy &optional (raw t) force-all)
126 (loop with presence = (policy-presence-bits policy)
127 for index from (- n-policy-primary-qualities)
128 below (length **policy-dependent-qualities**)
129 when (or force-all (logbitp (mod index max-policy-qualities) presence))
130 collect
131 (list (if (minusp index)
132 (elt **policy-primary-qualities** (lognot index))
133 (policy-dependent-quality-name
134 (elt **policy-dependent-qualities** index)))
135 (if raw
136 ;; Raw values are insensitive to *POLICY-RESTRICTIONS*.
137 (%%policy-quality policy index)
138 ;; Otherwise take the adjusted quality.
139 (%policy-quality policy index)))))
141 ;; Return T if P1 and P2 are policies which are specified to be the same.
142 ;; A result of NIL does not imply that definitely P1 /= P2
143 ;; because a multitude of policies can be effectively equal.
144 ;; [Any dependent quality might be specified the same as its computed
145 ;; value in the absence of an explicit value.]
146 (defun policy= (p1 p2)
147 (or (and p1 p2
148 (= (policy-primary-qualities p1) (policy-primary-qualities p2))
149 (= (policy-dependent-qualities p1) (policy-dependent-qualities p2))
150 (= (policy-presence-bits p1) (policy-presence-bits p2)))
151 (and (null p1) (null p2))))
153 ;;; Is X the name of an optimization policy quality?
154 ;;; If it is, return the integer identifier for the quality name.
155 (defun policy-quality-name-p (x)
156 ;; Standard (and non-standard) primary qualities are numbered from -1 down.
157 (or (awhen (position x **policy-primary-qualities** :test #'eq)
158 (lognot it))
159 ;; Dependent qualities are numbered from 0 up.
160 (position x **policy-dependent-qualities**
161 :key #'policy-dependent-quality-name)))
163 ;; Destructively modify POLICY such that quality INDEX has VALUE,
164 ;; and the specified PRESENTP bit.
165 (defun alter-policy (policy index value &optional (presentp t))
166 (if (minusp index) ; a primary quality
167 (setf (ldb (byte 2 (* 2 (lognot index)))
168 (policy-primary-qualities policy)) value)
169 (setf (ldb (byte 2 (* 2 index))
170 (policy-dependent-qualities policy)) value))
171 ;; Some cross-compilation hosts can't execute (SETF (LOGBITP ...)).
172 (setf (ldb (byte 1 (mod index max-policy-qualities))
173 (policy-presence-bits policy)) (if presentp 1 0))
174 policy)
176 ;;; Is it deprecated?
177 (declaim (ftype function deprecation-warn))
178 (defun policy-quality-deprecation-warning (quality)
179 (case quality
180 ((stack-allocate-dynamic-extent stack-allocate-vector stack-allocate-value-cells)
181 (deprecation-warn :late "SBCL" "1.0.19.7" 'policy quality '*stack-allocate-dynamic-extent*
182 :runtime-error nil)
184 ((merge-tail-calls)
185 (deprecation-warn :early "SBCL" "1.0.53.74" 'policy quality nil :runtime-error nil)
187 (otherwise
188 nil)))
190 ;;; *POLICY* holds the current global compiler policy information, as
191 ;;; a POLICY object mapping from the compiler-assigned index (unique per
192 ;;; quality name) to quality value.
193 ;;; This used to be an alist, but tail-sharing was never really possible
194 ;;; because for deterministic comparison the list was always freshly
195 ;;; consed so that destructive sorting could be done for canonicalization.
196 (declaim (type policy *policy*)
197 (type (or policy null) *policy-restrictions*))
199 ;; ANSI-specified default of 1 for each quality.
200 (defglobal **baseline-policy** nil)
201 ;; Baseline policy altered with (TYPE-CHECK 0)
202 (defglobal **zero-typecheck-policy** nil)
203 #-sb-xc-host (declaim (type policy **baseline-policy**))
205 ;;; This is to be called early in cold init to set things up, and may
206 ;;; also be called again later in cold init in order to reset default
207 ;;; optimization policy back to default values after toplevel PROCLAIM
208 ;;; OPTIMIZE forms have messed with it.
209 (defun !policy-cold-init-or-resanify ()
210 (setq **baseline-policy**
211 (make-policy (loop for i below n-policy-primary-qualities
212 sum (ash #b01 (* i 2))))
213 **zero-typecheck-policy**
214 (alter-policy (copy-policy **baseline-policy**)
215 #-sb-xc (policy-quality-name-p 'type-check)
216 ;; Eval in the host since cold-init won't have
217 ;; executed any forms in 'policies.lisp'
218 #+sb-xc #.(policy-quality-name-p 'type-check)
221 ;; CMU CL didn't use 1 as the default for everything,
222 ;; but since ANSI says 1 is the ordinary value, we do.
223 (setf *policy* (copy-policy **baseline-policy**)))
225 ;;; Look up a named optimization quality in POLICY. This is only
226 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
227 ;;; it's an error if it's called for a quality which isn't defined.
228 (defun policy-quality (policy quality-name)
229 (%policy-quality policy
230 (the fixnum (policy-quality-name-p quality-name))))
232 (define-compiler-macro policy-quality (&whole form policy quality-name)
233 (acond ((and (constantp quality-name)
234 ;; CONSTANT-FORM-VALUE can not be called here when building
235 ;; the cross-compiler, but EVAL can safely be used
236 ;; since our own source code is known not to be screwy.
237 (policy-quality-name-p (#-sb-xc-host constant-form-value
238 #+sb-xc-host eval quality-name)))
239 `(%policy-quality ,policy ,it))
241 form)))
243 (macrolet ((extract-field (floor-expression-primary
244 floor-expression-dependent)
245 `(if (minusp index)
246 (let ((byte-pos (* (lognot index) 2)))
247 (max (ldb (byte 2 byte-pos)
248 (policy-primary-qualities policy))
249 ,floor-expression-primary))
250 (let ((byte-pos (* index 2)))
251 (max (if (logbitp index (policy-presence-bits policy))
252 (ldb (byte 2 byte-pos)
253 (policy-dependent-qualities policy))
255 ,floor-expression-dependent))))
256 (define-getter (name &body body)
257 `(defun ,name (policy index)
258 (declare (type policy policy)
259 (type (integer
260 #.(- n-policy-primary-qualities)
261 #.(- max-policy-qualities
262 n-policy-primary-qualities 1))
263 index))
264 ,@body)))
266 ;; Return the value for quality INDEX in POLICY, using *POLICY-RESTRICTIONS*
267 ;; Primary qualities are assumed to exist, however policy-restricting functions
268 ;; can create a POLICY that indicates absence of primary qualities.
269 ;; This does not affect RESTRICT-COMPILER-POLICY because a lower bound of 0
270 ;; can be assumed for everything. SET-MACRO-POLICY might care though.
271 (define-getter %policy-quality
272 (let ((floor *policy-restrictions*))
273 (macrolet ((quality-floor (get-byte)
274 `(if floor (ldb (byte 2 byte-pos) (,get-byte floor)) 0)))
275 (extract-field (quality-floor policy-primary-qualities)
276 (quality-floor policy-dependent-qualities)))))
278 ;; Return the unadjusted value for quality INDEX in POLICY.
279 ;; This is used for converting a policy to a list of elements for display
280 ;; and for verifying that after processing declarations, the new policy
281 ;; matches the given declarations, thus implying no ambiguity.
282 (define-getter %%policy-quality
283 (extract-field 0 0))) ; floor is always 0
285 ;;; Forward declaration of %COERCE-TO-POLICY.
286 ;;; Definition is in 'node' so that FUNCTIONAL and NODE types are defined.
287 ;;; Arg is declared of type T because the function explicitly checks it.
288 (declaim (ftype (function (t) (values policy &optional)) %coerce-to-policy))
290 ;;; syntactic sugar for querying optimization policy qualities
292 ;;; Evaluate EXPR in terms of the optimization policy associated with
293 ;;; THING. EXPR is a form which accesses optimization qualities by
294 ;;; referring to them by name, e.g. (> SPEED SPACE).
295 (defmacro policy (thing expr)
296 (let* ((n-policy (gensym "N-POLICY-"))
297 (binds (loop for name across **policy-primary-qualities**
298 for index downfrom -1
299 collect `(,name (%policy-quality ,n-policy ,index))))
300 (dependent-binds
301 (loop for info across **policy-dependent-qualities**
302 for name = (policy-dependent-quality-name info)
303 collect `(,name (let ((,name (policy-quality ,n-policy ',name)))
304 (if (= ,name 1)
305 ,(policy-dependent-quality-expression info)
306 ,name))))))
307 `(let* ((,n-policy (%coerce-to-policy ,thing)))
308 (declare (ignorable ,n-policy))
309 (symbol-macrolet (,@binds
310 ,@dependent-binds)
311 ,expr))))
313 ;;; Dependent qualities
314 (defmacro define-optimization-quality
315 (name expression &optional values-documentation documentation)
316 (declare (ignorable documentation))
317 `(eval-when (:compile-toplevel :load-toplevel :execute)
318 (let ((number (policy-quality-name-p ',name))
319 (item (make-policy-dependent-quality
320 :name ',name
321 :expression ',expression
322 ;; DESCRIBE-COMPILER-POLICY uses the getter
323 :getter (named-lambda ,(string name) (policy)
324 (policy policy ,expression))
325 :values-documentation ',values-documentation)))
326 (if number
327 (setf (svref **policy-dependent-qualities** number) item)
328 ;; This array is reallocated every time a policy is added,
329 ;; but that's fine - it's not a performance issue.
330 (let ((size (1+ (length **policy-dependent-qualities**))))
331 ;; Don't overrun the packed bit fields.
332 (when (> (+ n-policy-primary-qualities size) max-policy-qualities)
333 (error "Maximum number of policy qualities exceeded."))
334 (setf **policy-dependent-qualities**
335 (replace (make-array size :initial-element item)
336 **policy-dependent-qualities**)))))
337 #-sb-xc-host
338 ,@(when documentation `((setf (fdocumentation ',name 'optimize) ,documentation)))
339 ',name))
341 ;;; Return a new POLICY containing the policy information represented
342 ;;; by the optimize declaration SPEC. Any parameters not specified are
343 ;;; defaulted from the POLICY argument.
344 (declaim (ftype (function (list (or policy null)) (values policy list))
345 process-optimize-decl))
346 (defun process-optimize-decl (spec policy)
347 (let ((result (copy-policy (or policy **baseline-policy**)))
348 (specified-qualities))
349 ;; Add new entries from SPEC.
350 (dolist (q-and-v-or-just-q (cdr spec) (values result specified-qualities))
351 (multiple-value-bind (quality raw-value)
352 (if (atom q-and-v-or-just-q)
353 (values q-and-v-or-just-q 3)
354 (destructuring-bind (quality raw-value) q-and-v-or-just-q
355 (values quality raw-value)))
356 (let ((index (policy-quality-name-p quality)))
357 (cond ((not index)
358 (or (policy-quality-deprecation-warning quality)
359 (compiler-warn
360 "~@<Ignoring unknown optimization quality ~S in:~_ ~S~:>"
361 quality spec)))
362 ((not (typep raw-value 'policy-quality))
363 (compiler-warn
364 "~@<Ignoring bad optimization value ~S in:~_ ~S~:>"
365 raw-value spec))
367 ;; we can't do this yet, because CLOS macros expand
368 ;; into code containing INHIBIT-WARNINGS.
369 #+nil
370 (when (eql quality 'inhibit-warnings)
371 (compiler-style-warn "~S is deprecated: use ~S instead"
372 quality 'muffle-conditions))
373 (push (cons quality raw-value) specified-qualities)
374 (alter-policy result index raw-value))))))))
376 (defvar *macro-policy* nil)
377 ;; Set an alternate policy that is used to compile all code within DEFMACRO,
378 ;; MACROLET, DEFINE-COMPILER-MARO - whether they occur at toplevel or not -
379 ;; as well as execute all toplevel code in eval-when situation :COMPILE-TOPLEVEL,
380 ;; including such code as emitted into a '.cfasl' file.
381 ;; e.g. (SET-MACRO-POLICY '((SPEED 0) (SAFETY 3))) ensures full error checking
382 ;; regardless of prevailing local policy in situations such as
383 ;; (macrolet ((frob (a b) (declare (type (member :up :down) a)) ...)
385 ;; Todo: it would be nice to allow NOTINLINE, which can be broadly achieved by
386 ;; setting (SPEED 0), but nonetheless more targeted settings should be possible.
387 ;; Same for {UN}MUFFLE-CONDITIONS or anything else that can be proclaimed.
389 (defun set-macro-policy (list)
390 ;: Note that *MACRO-POLICY* does not represent absence of any primary quality,
391 ;; and therefore whenever it is injected into a macro, you get all baseline
392 ;; values of 1, augmented by the specified changes.
393 ;; There are two alternative behaviors that might make sense:
394 ;; - use the value of *POLICY* when SET-MACRO-POLICY is called as the baseline
395 ;; augmented by the specifiers in LIST
396 ;; - use the lexical policy at the time of expansion, augmented by LIST
397 ;; But most probably the current behavior is entirely reasonable.
398 (setq *macro-policy* (process-optimize-decl `(optimize ,@list)
399 **baseline-policy**)))
401 ;; Turn the macro policy into an OPTIMIZE declaration for insertion
402 ;; into a macro body for DEFMACRO, MACROLET, or DEFINE-COMPILER-MACRO.
403 ;; Note that despite it being a style-warning to insert a duplicate,
404 ;; we need no precaution against that even though users may write
405 ;; (DEFMACRO FOO (X) (DECLARE (OPTIMIZE (SAFETY 1))) ...)
406 ;; The expansion of macro-defining forms is such that the macro-policy
407 ;; appears in a different lexical scope from the user's declarations.
408 (defun macro-policy-decls ()
409 (and *macro-policy*
410 `((declare (optimize ,@(policy-to-decl-spec *macro-policy*))))))