0.6.9.16:
[sbcl.git] / src / compiler / policy.lisp
1 ;;;; compiler optimization policy stuff
2
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.
11
12 (in-package "SB!C")
13
14 ;;; a value for an optimization declaration
15 (def!type policy-quality () '(rational 0 3))
16
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
21 ;;; instead.
22 (def!type policy () 'list)
23
24 ;;; names of recognized optimization qualities which don't have
25 ;;; special defaulting behavior
26 (defvar *policy-basic-qualities*) ; (initialized at cold init)
27
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))).
30 #|
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*)
35 |#
36
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)|#))
41
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.
46 ;;;
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
52
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
60           compilation-speed
61           debug
62           safety
63           space
64           speed
65           ;; SBCL extensions
66           ;;
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?
73           inhibit-warnings))
74   #|
75   (setf *policy-defaulting-qualities*
76         '((interface-speed . speed)
77           (interface-safety . safety)))
78   |#
79   (setf *default-policy*
80         (mapcar (lambda (name)
81                   ;; CMU CL didn't use 1 as the default for everything,
82                   ;; but since ANSI says 1 is the ordinary value, we do.
83                   (cons name 1))
84                 *policy-basic-qualities*))
85   (setf *default-interface-policy*
86         *default-policy*))
87 ;;; On the cross-compilation host, we initialize immediately (not
88 ;;; waiting for "cold init", since cold init doesn't exist on
89 ;;; cross-compilation host).
90 #+sb-xc-host (!policy-cold-init-or-resanify)
91
92 ;;; Is X the name of an optimization quality?
93 (defun policy-quality-name-p (x)
94   (memq x *policy-basic-qualities*))
95
96 ;;; Look up a named optimization quality in POLICY. This is only
97 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
98 ;;; it's an error if it's called for a quality which isn't defined.
99 ;;;
100 ;;; FIXME: After this is debugged, it should get a DEFKNOWN.
101 #+nil (declaim (ftype (function (policy symbol) policy-quality)))
102 (defun policy-quality (policy quality-name)
103   (let ((acons (assoc quality-name policy)))
104     (unless acons
105       (error "Argh! no such optimization quality ~S in~%  ~S"
106              quality-name policy))
107     (let ((result (cdr acons)))
108       (unless (typep result '(rational 0 3))
109         (error "Argh! bogus optimization quality ~S" acons))
110       result)))
111
112 ;;; Return a list of symbols naming the optimization qualities which
113 ;;; appear in EXPR.
114 (defun policy-qualities-used-by (expr)
115   (let ((result nil))
116     (labels ((recurse (x)
117                (if (listp x)
118                    (map nil #'recurse x)
119                    (when (policy-quality-name-p x)
120                      (pushnew x result)))))
121       (recurse expr)
122       result)))
123
124 ;;; syntactic sugar for querying optimization policy qualities
125 ;;;
126 ;;; Evaluate EXPR in terms of the current optimization policy for
127 ;;; NODE, or if NODE is NIL, in terms of the current policy as defined
128 ;;; by *DEFAULT-POLICY* and *CURRENT-POLICY*. (Using NODE=NIL is only
129 ;;; well-defined during IR1 conversion.)
130 ;;;
131 ;;; EXPR is a form which accesses the policy values by referring to
132 ;;; them by name, e.g. (> SPEED SPACE).
133 (defmacro policy (node expr)
134   (let* ((n-policy (gensym))
135          (used-qualities (policy-qualities-used-by expr))
136          (binds (mapcar (lambda (name)
137                           `(,name (policy-quality ,n-policy ',name)))
138                         used-qualities)))
139     `(let* ((,n-policy (lexenv-policy ,(if node
140                                            `(node-lexenv ,node)
141                                            '*lexenv*)))
142             ,@binds)
143        ,expr)))