1 ;;;; compiler optimization policy stuff
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;; a value for an optimization declaration
15 (def!type policy-quality () '(integer 0 3))
17 ;;; global policy restrictions
18 (defvar *policy-restrictions* nil)
20 (defun restrict-compiler-policy (&optional quality (min 0))
22 "Assing a minimum value to an optimization quality. QUALITY is the name of
23 the optimization quality to restrict, and MIN (defaulting to zero) is the
24 minimum allowed value.
26 Returns the alist describing the current policy restrictions.
28 If QUALITY is NIL or not given, nothing is done.
30 Otherwise, if MIN is zero or not given, any existing restrictions of QUALITY
31 are removed. If MIN is between one and three inclusive, it becomes the new
32 minimum value for the optimization quality: any future proclamations or
33 declarations of the quality with a value less then MIN behave as if the value
36 This is intended to be used interactively, to facilitate recompiling large
37 bodies of code with eg. a known minimum safety.
39 EXPERIMENTAL INTERFACE: Subject to change."
40 (declare (policy-quality min))
42 (aver (policy-quality-name-p quality))
44 (setf *policy-restrictions*
45 (remove quality *policy-restrictions* :key #'car))
46 (let ((cell (assoc quality *policy-restrictions*)))
49 (push (cons quality min) *policy-restrictions*)))))
50 *policy-restrictions*)
52 ;;; CMU CL used a special STRUCTURE-OBJECT type POLICY to represent
53 ;;; the state of optimization policy at any point in compilation. This
54 ;;; was a natural choice, but in SBCL it became a little troublesome
55 ;;; because of stupid technicalities involving the cold initialization
56 ;;; of structure LAYOUTs and structure accessors, so now we just use
58 (def!type policy () 'list)
60 (defstruct policy-dependent-quality
66 ;;; names of recognized optimization policy qualities
67 (defvar *policy-qualities*) ; (initialized at cold init)
68 (defvar *policy-dependent-qualities* nil) ; alist of POLICY-DEPENDENT-QUALITYs
70 ;;; Is X the name of an optimization policy quality?
71 (defun policy-quality-name-p (x)
72 (or (memq x *policy-qualities*)
73 (assq x *policy-dependent-qualities*)))
75 ;;; *POLICY* holds the current global compiler policy information, as
76 ;;; an alist mapping from optimization quality name to quality value.
77 ;;; Inside the scope of declarations, new entries are added at the
78 ;;; head of the alist.
79 (declaim (type policy *policy*))
80 (defvar *policy*) ; initialized in cold init
82 ;;; This is to be called early in cold init to set things up, and may
83 ;;; also be called again later in cold init in order to reset default
84 ;;; optimization policy back to default values after toplevel PROCLAIM
85 ;;; OPTIMIZE forms have messed with it.
86 (defun !policy-cold-init-or-resanify ()
87 (setf *policy-qualities*
88 '(;; ANSI standard qualities
96 ;; FIXME: INHIBIT-WARNINGS is a misleading name for this.
97 ;; Perhaps BREVITY would be better. But the ideal name would
98 ;; have connotations of suppressing not warnings but only
99 ;; optimization-related notes, which is already mostly the
100 ;; behavior, and should probably become the exact behavior.
101 ;; Perhaps INHIBIT-NOTES?
104 (mapcar (lambda (name)
105 ;; CMU CL didn't use 1 as the default for
106 ;; everything, but since ANSI says 1 is the ordinary
110 (setf *policy-restrictions* nil)
111 ;; not actually POLICY, but very similar
112 (setf *handled-conditions* nil
113 *disabled-package-locks* nil))
115 ;;; On the cross-compilation host, we initialize immediately (not
116 ;;; waiting for "cold init", since cold init doesn't exist on
117 ;;; cross-compilation host).
118 #+sb-xc-host (!policy-cold-init-or-resanify)
120 ;;; Look up a named optimization quality in POLICY. This is only
121 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
122 ;;; it's an error if it's called for a quality which isn't defined.
123 (defun policy-quality (policy quality-name)
124 (aver (policy-quality-name-p quality-name))
125 (%policy-quality policy quality-name))
127 (defun %policy-quality (policy quality-name)
128 (let* ((acons (assoc quality-name policy))
129 (min (or (cdr (assoc quality-name *policy-restrictions*)) 0))
130 (result (or (cdr acons) 1)))
133 ;;; syntactic sugar for querying optimization policy qualities
135 ;;; Evaluate EXPR in terms of the optimization policy associated with
136 ;;; THING. EXPR is a form which accesses optimization qualities by
137 ;;; referring to them by name, e.g. (> SPEED SPACE).
138 (defmacro policy (thing expr)
139 (let* ((n-policy (gensym "N-POLICY-"))
140 (binds (mapcar (lambda (name)
141 `(,name (policy-quality ,n-policy ',name)))
144 (loop for (name . info) in *policy-dependent-qualities*
145 collect `(,name (let ((,name (policy-quality ,n-policy ',name)))
147 ,(policy-dependent-quality-expression info)
149 `(let* ((,n-policy (%coerce-to-policy ,thing)))
150 (declare (ignorable ,n-policy))
151 (symbol-macrolet (,@binds
155 ;;; Dependent qualities
156 (defmacro define-optimization-quality
157 (name expression &optional values-documentation documentation)
158 (declare (ignorable documentation))
159 `(eval-when (:compile-toplevel :load-toplevel :execute)
160 (let ((acons (assoc ',name *policy-dependent-qualities*))
161 (item (make-policy-dependent-quality
163 :expression ',expression
164 :getter (lambda (policy) (policy policy ,expression))
165 :values-documentation ',values-documentation)))
167 (setf (cdr acons) item)
168 (setf *policy-dependent-qualities*
169 (nconc *policy-dependent-qualities* (list `(,',name . ,item))))))
171 ,@(when documentation `((setf (fdocumentation ',name 'optimize) ,documentation)))