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 () '(rational 0 3))
17 ;;; CMU CL used a special STRUCTURE-OBJECT type POLICY to represent
18 ;;; the state of optimization policy at any point in compilation. This
19 ;;; became a little unwieldy, especially because of cold init issues
20 ;;; for structures and structure accessors, so in SBCL we use an alist
22 (def!type policy () 'list)
24 ;;; names of recognized optimization qualities which don't have
25 ;;; special defaulting behavior
26 (defvar *policy-basic-qualities*) ; (initialized at cold init)
28 ;;; FIXME: I'd like to get rid of DECLAIM OPTIMIZE-INTERFACE in favor
29 ;;; of e.g. (DECLAIM (OPTIMIZE (INTERFACE-SPEED 2) (INTERFACE-SAFETY 3))).
31 ;;; a list of conses (DEFAULTING-QUALITY . DEFAULT-QUALITY) of qualities
32 ;;; which default to other qualities when undefined, e.g. interface
33 ;;; speed defaulting to basic speed
34 (defvar *policy-defaulting-qualities*)
37 (defun optimization-quality-p (name)
38 (or (member name *policy-basic-qualities*)
39 ;; FIXME: Uncomment this when OPTIMIZE-INTERFACE goes away.
40 #|(member name *policy-defaulting-qualities* :key #'car)|#))
42 ;;; *DEFAULT-POLICY* holds the current global compiler policy
43 ;;; information, as an alist mapping from optimization quality name to
44 ;;; quality value. Inside the scope of declarations, new entries are
45 ;;; added at the head of the alist.
47 ;;; *DEFAULT-INTERFACE-POLICY* holds any values specified by an
48 ;;; OPTIMIZE-INTERFACE declaration.
49 (declaim (type policy *default-policy* *default-interface-policy*))
50 (defvar *default-policy*) ; initialized in cold init
51 (defvar *default-interface-policy*) ; initialized in cold init
53 ;;; This is to be called early in cold init to set things up, and may
54 ;;; also be called again later in cold init in order to reset default
55 ;;; optimization policy back to default values after toplevel PROCLAIM
56 ;;; OPTIMIZE forms have messed with it.
57 (defun !policy-cold-init-or-resanify ()
58 (setf *policy-basic-qualities*
59 '(;; ANSI standard qualities
67 ;; FIXME: INHIBIT-WARNINGS is a misleading name for this.
68 ;; Perhaps BREVITY would be better. But the ideal name would
69 ;; have connotations of suppressing not warnings but only
70 ;; optimization-related notes, which is already mostly the
71 ;; behavior, and should probably become the exact behavior.
72 ;; Perhaps INHIBIT-NOTES?
74 (setf *policy-defaulting-qualities*
75 '((interface-speed . speed)
76 (interface-safety . safety)))
77 (setf *default-policy*
78 (mapcar (lambda (name)
79 ;; CMU CL didn't use 1 as the default for everything,
80 ;; but since ANSI says 1 is the ordinary value, we do.
82 *policy-basic-qualities*))
83 (setf *default-interface-policy*
85 ;;; On the cross-compilation host, we initialize immediately (not
86 ;;; waiting for "cold init", since cold init doesn't exist on
87 ;;; cross-compilation host).
88 #+sb-xc-host (!policy-cold-init-or-resanify)
90 ;;; Is X the name of an optimization quality?
91 (defun policy-quality-name-p (x)
92 (memq x *policy-basic-qualities*))
94 ;;; Look up a named optimization quality in POLICY. This is only
95 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
96 ;;; it's an error if it's called for a quality which isn't defined.
98 ;;; FIXME: After this is debugged, it should get a DEFKNOWN.
99 #+nil (declaim (ftype (function (policy symbol) policy-quality)))
100 (defun policy-quality (policy quality-name)
101 (let ((acons (assoc quality-name policy)))
103 (error "Argh! no such optimization quality ~S in~% ~S"
104 quality-name policy))
105 (let ((result (cdr acons)))
106 (unless (typep result '(rational 0 3))
107 (error "Argh! bogus optimization quality ~S" acons))
110 ;;; Return a list of symbols naming the optimization qualities which
112 (defun policy-qualities-used-by (expr)
114 (labels ((recurse (x)
116 (map nil #'recurse x)
117 (when (policy-quality-name-p x)
118 (pushnew x result)))))
122 ;;; syntactic sugar for querying optimization policy qualities
124 ;;; Evaluate EXPR in terms of the current optimization policy for
125 ;;; NODE, or if NODE is NIL, in terms of the current policy as defined
126 ;;; by *DEFAULT-POLICY* and *CURRENT-POLICY*. (Using NODE=NIL is only
127 ;;; well-defined during IR1 conversion.)
129 ;;; EXPR is a form which accesses the policy values by referring to
130 ;;; them by name, e.g. (> SPEED SPACE).
131 (defmacro policy (node expr)
132 (let* ((n-policy (gensym))
133 (used-qualities (policy-qualities-used-by expr))
134 (binds (mapcar (lambda (name)
135 `(,name (policy-quality ,n-policy ',name)))
137 (/show "in compile-time POLICY" expr binds)
138 `(let* ((,n-policy (lexenv-policy ,(if node
142 ;;(/show "in run-time POLICY" ,@used-qualities)