1 ;;;; This file implements the constraint propagation phase of the
2 ;;;; compiler, which uses global flow analysis to obtain dynamic type
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
18 ;;; -- MV-BIND, :ASSIGNMENT
20 ;;; Note: The functions in this file that accept constraint sets are
21 ;;; actually receiving the constraint sets associated with nodes,
22 ;;; blocks, and lambda-vars. It might be make CP easier to understand
23 ;;; and work on if these functions traded in nodes, blocks, and
24 ;;; lambda-vars directly.
28 ;;; -- Constraint propagation badly interacts with bottom-up type
29 ;;; inference. Consider
31 ;;; (defun foo (n &aux (i 42))
32 ;;; (declare (optimize speed))
33 ;;; (declare (fixnum n)
34 ;;; #+nil (type (integer 0) i))
38 ;;; (when (>= i n) (go :exit))
43 ;;; In this case CP cannot even infer that I is of class INTEGER.
45 ;;; -- In the above example if we place the check after SETQ, CP will
46 ;;; fail to infer (< I FIXNUM): it does not understand that this
47 ;;; constraint follows from (TYPEP I (INTEGER 0 0)).
51 ;;; *CONSTRAINT-UNIVERSE* gets bound in IR1-PHASES to a fresh,
52 ;;; zero-length, non-zero-total-size vector-with-fill-pointer.
53 (declaim (type (and vector (not simple-vector)) *constraint-universe*))
54 (defvar *constraint-universe*)
56 (deftype constraint-y () '(or ctype lvar lambda-var constant))
58 (defstruct (constraint
59 (:include sset-element)
60 (:constructor make-constraint (number kind x y not-p))
62 ;; the kind of constraint we have:
65 ;; X is a LAMBDA-VAR and Y is a CTYPE. The value of X is
66 ;; constrained to be of type Y.
69 ;; X is a lambda-var and Y is a CTYPE. The relation holds
70 ;; between X and some object of type Y.
73 ;; X is a LAMBDA-VAR and Y is a LVAR, a LAMBDA-VAR or a CONSTANT.
74 ;; The relation is asserted to hold.
75 (kind nil :type (member typep < > eql))
76 ;; The operands to the relation.
77 (x nil :type lambda-var)
78 (y nil :type constraint-y)
79 ;; If true, negates the sense of the constraint, so the relation
81 (not-p nil :type boolean))
83 ;;; Historically, CMUCL and SBCL have used a sparse set implementation
84 ;;; for which most operations are O(n) (see sset.lisp), but at the
85 ;;; cost of at least a full word of pointer for each constraint set
86 ;;; element. Using bit-vectors instead of pointer structures saves a
87 ;;; lot of space and thus GC time (particularly on 64-bit machines),
88 ;;; and saves time on copy, union, intersection, and difference
89 ;;; operations; but makes iteration slower. Circa September 2008,
90 ;;; switching to bit-vectors gave a modest (5-10%) improvement in real
91 ;;; compile time for most Lisp systems, and as much as 20-30% for some
92 ;;; particularly CP-dependent systems.
94 ;;; It's bad to leave commented code in files, but if some clever
95 ;;; person comes along and makes SSETs better than bit-vectors as sets
96 ;;; for constraint propagation, or if bit-vectors on some XC host
97 ;;; really lose compared to SSETs, here's the conset API as a wrapper
101 (deftype conset () 'sset)
102 (declaim (ftype (sfunction (conset) boolean) conset-empty))
103 (declaim (ftype (sfunction (conset) conset) copy-conset))
104 (declaim (ftype (sfunction (constraint conset) boolean) conset-member))
105 (declaim (ftype (sfunction (constraint conset) boolean) conset-adjoin))
106 (declaim (ftype (sfunction (conset conset) boolean) conset=))
107 (declaim (ftype (sfunction (conset conset) (values)) conset-union))
108 (declaim (ftype (sfunction (conset conset) (values)) conset-intersection))
109 (declaim (ftype (sfunction (conset conset) (values)) conset-difference))
110 (defun make-conset () (make-sset))
111 (defmacro do-conset-elements ((constraint conset &optional result) &body body)
112 `(do-sset-elements (,constraint ,conset ,result) ,@body))
113 (defmacro do-conset-intersection
114 ((constraint conset1 conset2 &optional result) &body body)
115 `(do-conset-elements (,constraint ,conset1 ,result)
116 (when (conset-member ,constraint ,conset2)
118 (defun conset-empty (conset) (sset-empty conset))
119 (defun copy-conset (conset) (copy-sset conset))
120 (defun conset-member (constraint conset) (sset-member constraint conset))
121 (defun conset-adjoin (constraint conset) (sset-adjoin constraint conset))
122 (defun conset= (conset1 conset2) (sset= conset1 conset2))
123 ;; Note: CP doesn't ever care whether union, intersection, and
124 ;; difference change the first set. (This is an important degree of
125 ;; freedom, since some ways of implementing sets lose a great deal
126 ;; when these operations are required to track changes.)
127 (defun conset-union (conset1 conset2)
128 (sset-union conset1 conset2) (values))
129 (defun conset-intersection (conset1 conset2)
130 (sset-intersection conset1 conset2) (values))
131 (defun conset-difference (conset1 conset2)
132 (sset-difference conset1 conset2) (values)))
135 ;; This is performance critical for the compiler, and benefits
136 ;; from the following declarations. Probably you'll want to
137 ;; disable these declarations when debugging consets.
138 (declare #-sb-xc-host (optimize (speed 3) (safety 0) (space 0)))
139 (declaim (inline %constraint-number))
140 (defun %constraint-number (constraint)
141 (sset-element-number constraint))
143 (:constructor make-conset ())
144 (:copier %copy-conset))
146 ;; FIXME: make POWER-OF-TWO-CEILING available earlier?
147 (ash 1 (integer-length (1- (length *constraint-universe*))))
148 :element-type 'bit :initial-element 0)
149 :type simple-bit-vector)
150 ;; Bit-vectors win over lightweight hashes for copy, union,
151 ;; intersection, difference, but lose for iteration if you iterate
152 ;; over the whole vector. Tracking extrema helps a bit.
154 (max 0 :type fixnum))
156 (defun conset-empty (conset)
157 (or (= (conset-min conset) (conset-max conset))
158 ;; TODO: I bet FIND on bit-vectors can be optimized, if it
160 (not (find 1 (conset-vector conset)
161 :start (conset-min conset)
162 ;; By inspection, supplying :END here breaks the
163 ;; build with a "full call to
164 ;; DATA-VECTOR-REF-WITH-OFFSET" in the
165 ;; cross-compiler. If that should change, add
166 ;; :end (conset-max conset)
169 (defun copy-conset (conset)
170 (let ((ret (%copy-conset conset)))
171 (setf (conset-vector ret) (copy-seq (conset-vector conset)))
174 (defun %conset-grow (conset new-size)
175 (declare (type index new-size))
176 (setf (conset-vector conset)
177 (replace (the simple-bit-vector
179 (ash 1 (integer-length (1- new-size)))
182 (the simple-bit-vector
183 (conset-vector conset)))))
185 (declaim (inline conset-grow))
186 (defun conset-grow (conset new-size)
187 (declare (type index new-size))
188 (when (< (length (conset-vector conset)) new-size)
189 (%conset-grow conset new-size))
192 (defun conset-member (constraint conset)
193 (let ((number (%constraint-number constraint))
194 (vector (conset-vector conset)))
195 (when (< number (length vector))
196 (plusp (sbit vector number)))))
198 (defun conset-adjoin (constraint conset)
199 (let ((number (%constraint-number constraint)))
200 (conset-grow conset (1+ number))
201 (setf (sbit (conset-vector conset) number) 1)
202 (setf (conset-min conset) (min number (conset-min conset)))
203 (when (>= number (conset-max conset))
204 (setf (conset-max conset) (1+ number))))
207 (defun conset= (conset1 conset2)
208 (let* ((vector1 (conset-vector conset1))
209 (vector2 (conset-vector conset2))
210 (length1 (length vector1))
211 (length2 (length vector2)))
212 (if (= length1 length2)
213 ;; When the lengths are the same, we can rely on EQUAL being
214 ;; nicely optimized on bit-vectors.
215 (equal vector1 vector2)
216 (multiple-value-bind (shorter longer)
217 (if (< length1 length2)
218 (values vector1 vector2)
219 (values vector2 vector1))
220 ;; FIXME: make MISMATCH fast on bit-vectors.
221 (dotimes (index (length shorter))
222 (when (/= (sbit vector1 index) (sbit vector2 index))
223 (return-from conset= nil)))
224 (if (find 1 longer :start (length shorter))
229 ((defconsetop (name bit-op)
230 `(defun ,name (conset-1 conset-2)
231 (declare (optimize (speed 3) (safety 0)))
232 (let* ((size-1 (length (conset-vector conset-1)))
233 (size-2 (length (conset-vector conset-2)))
234 (new-size (max size-1 size-2)))
235 (conset-grow conset-1 new-size)
236 (conset-grow conset-2 new-size))
237 (let ((vector1 (conset-vector conset-1))
238 (vector2 (conset-vector conset-2)))
239 (declare (simple-bit-vector vector1 vector2))
240 (setf (conset-vector conset-1) (,bit-op vector1 vector2 t))
241 ;; Update the extrema.
244 `(setf (conset-min conset-1)
245 (min (conset-min conset-1)
246 (conset-min conset-2))
247 (conset-max conset-1)
248 (max (conset-max conset-1)
249 (conset-max conset-2))))
250 ((conset-intersection)
251 `(let ((start (max (conset-min conset-1)
252 (conset-min conset-2)))
253 (end (min (conset-max conset-1)
254 (conset-max conset-2))))
255 (setf (conset-min conset-1)
258 (or (position 1 (conset-vector conset-1)
259 :start start :end end)
261 (conset-max conset-1)
266 1 (conset-vector conset-1)
267 :start start :end end :from-end t)))
272 `(setf (conset-min conset-1)
273 (or (position 1 (conset-vector conset-1)
274 :start (conset-min conset-1)
275 :end (conset-max conset-1))
277 (conset-max conset-1)
280 1 (conset-vector conset-1)
281 :start (conset-min conset-1)
282 :end (conset-max conset-1)
288 (defconsetop conset-union bit-ior)
289 (defconsetop conset-intersection bit-and)
290 (defconsetop conset-difference bit-andc2)))
292 ;;; Constraints are hash-consed. Unfortunately, types aren't, so we have
293 ;;; to over-approximate and then linear search through the potential hits.
294 ;;; LVARs can only be found in EQL (not-p = NIL) constraints, while constant
295 ;;; and lambda-vars can only be found in EQL constraints.
296 (defun find-constraint (kind x y not-p)
297 (declare (type lambda-var x) (type constraint-y y) (type boolean not-p))
300 (awhen (lambda-var-ctype-constraints x)
301 (dolist (con (gethash (sb!kernel::type-class-info y) it) nil)
302 (when (and (eq (constraint-kind con) kind)
303 (eq (constraint-not-p con) not-p)
304 (type= (constraint-y con) y))
305 (return-from find-constraint con)))
308 (awhen (lambda-var-eq-constraints x)
310 ((or constant lambda-var)
311 (awhen (lambda-var-eq-constraints x)
312 (let ((cache (gethash y it)))
313 (declare (type list cache))
314 (if not-p (cdr cache) (car cache)))))))
316 ;;; The most common operations on consets are iterating through the constraints
317 ;;; that are related to a certain variable in a given conset. Storing the
318 ;;; constraints related to each variable in vectors allows us to easily iterate
319 ;;; through the intersection of such constraints and the constraints in a conset.
321 ;;; EQL-var constraints assert that two lambda-vars are EQL.
322 ;;; Private constraints assert that a lambda-var is EQL or not EQL to a constant.
323 ;;; Inheritable constraints are constraints that may be propagated to EQL
324 ;;; lambda-vars (along with EQL-var constraints).
326 ;;; Lambda-var -- lvar EQL constraints only serve one purpose: remember whether
327 ;;; an lvar is (only) written to by a ref to that lambda-var, and aren't ever
330 ;;; Finally, the lambda-var conset is only used to track the whole set of
331 ;;; constraints associated with a given lambda-var, and thus easily delete
332 ;;; such constraints from a conset.
333 (defun register-constraint (x con y)
334 (declare (type lambda-var x) (type constraint con) (type constraint-y y))
335 (conset-adjoin con (lambda-var-constraints x))
336 (macrolet ((ensuref (place default)
337 `(or ,place (setf ,place ,default)))
339 `(ensuref ,place (make-hash-table)))
341 `(ensuref ,place (make-array 8 :adjustable t :fill-pointer 0))))
344 (let ((index (ensure-hash (lambda-var-ctype-constraints x)))
345 (vec (ensure-vec (lambda-var-inheritable-constraints x))))
346 (push con (gethash (sb!kernel::type-class-info y) index))
347 (vector-push-extend con vec)))
349 (let ((index (ensure-hash (lambda-var-eq-constraints x))))
350 (setf (gethash y index) con)))
351 ((or constant lambda-var)
352 (let* ((index (ensure-hash (lambda-var-eq-constraints x)))
353 (cons (ensuref (gethash y index) (list nil))))
354 (if (constraint-not-p con)
355 (setf (cdr cons) con)
356 (setf (car cons) con)))
359 (let ((vec (ensure-vec (lambda-var-private-constraints x))))
360 (vector-push-extend con vec)))
362 (let ((vec (if (constraint-not-p con)
363 (ensure-vec (lambda-var-inheritable-constraints x))
364 (ensure-vec (lambda-var-eql-var-constraints x)))))
365 (vector-push-extend con vec)))))))
368 ;;; Return a constraint for the specified arguments. We only create a
369 ;;; new constraint if there isn't already an equivalent old one,
370 ;;; guaranteeing that all equivalent constraints are EQ. This
371 ;;; shouldn't be called on LAMBDA-VARs with no CONSTRAINTS set.
372 (defun find-or-create-constraint (kind x y not-p)
373 (declare (type lambda-var x) (type constraint-y y) (type boolean not-p))
374 (or (find-constraint kind x y not-p)
375 (let ((new (make-constraint (length *constraint-universe*)
377 (vector-push-extend new *constraint-universe*
378 (1+ (length *constraint-universe*)))
379 (register-constraint x new y)
380 (when (lambda-var-p y)
381 (register-constraint y new x))
384 ;;; Actual conset interface
386 ;;; Constraint propagation needs to iterate over the set of lambda-vars known to
387 ;;; be EQL to a given variable (including itself), via DO-EQL-VARS.
389 ;;; It also has to iterate through constraints that are inherited by EQL variables
390 ;;; (DO-INHERITABLE-CONSTRAINTS), and through constraints used by
391 ;;; CONSTRAIN-REF-TYPE (to derive the type of a REF to a lambda-var).
393 ;;; Consets must keep track of which lvars are EQL to a given lambda-var (result
394 ;;; from a REF to the lambda-var): CONSET-LVAR-LAMBDA-VAR-EQL-P and
395 ;;; CONSET-ADD-LVAR-LAMBDA-VAR-EQL. This, as all other constraints, must of
396 ;;; course be cleared when a lambda-var's constraints are dropped because of
399 ;;; Consets must be able to add constraints to a given lambda-var
400 ;;; (CONSET-ADD-CONSTRAINT), and to the set of variables EQL to a given
401 ;;; lambda-var (CONSET-ADD-CONSTRAINT-TO-EQL).
403 ;;; When a lambda-var is assigned to, all the constraints involving that variable
404 ;;; must be dropped: constraint propagation is flow-sensitive, so the constraints
405 ;;; relate to the variable at a given range of program point. In such cases,
406 ;;; constraint propagation calls CONSET-CLEAR-LAMBDA-VAR.
408 ;;; Finally, one of the main strengths of constraint propagation in SBCL is the
409 ;;; tracking of EQL variables to help constraint propagation. When two variables
410 ;;; are known to be EQL (e.g. after a branch), ADD-EQL-VAR-VAR-CONSTRAINT is
411 ;;; called to add the EQL constraint, but also have each equality class inherit
412 ;;; the other's (inheritable) constraints.
414 ;;; On top of that, we have the usual bulk set operations: intersection, copy,
415 ;;; equality or emptiness testing. There's also union, but that's only an
416 ;;; optimisation to avoid useless copies in ADD-TEST-CONSTRAINTS and
417 ;;; FIND-BLOCK-TYPE-CONSTRAINTS.
418 (defmacro do-conset-constraints-intersection ((symbol (conset constraints) &optional result)
420 (let ((min (gensym "MIN"))
421 (max (gensym "MAX")))
422 (once-only ((conset conset)
423 (constraints constraints))
424 `(flet ((body (,symbol)
425 (declare (type constraint ,symbol))
428 (let ((,min (conset-min ,conset))
429 (,max (conset-max ,conset)))
430 (map nil (lambda (constraint)
431 (declare (type constraint constraint))
432 (let ((number (constraint-number constraint)))
433 (when (and (<= ,min number)
435 (conset-member constraint ,conset))
440 (defmacro do-eql-vars ((symbol (var constraints) &optional result) &body body)
441 (once-only ((var var)
442 (constraints constraints))
443 `(flet ((body-fun (,symbol)
446 (do-conset-constraints-intersection
447 (con (,constraints (lambda-var-eql-var-constraints ,var)) ,result)
448 (let ((x (constraint-x con))
449 (y (constraint-y con)))
450 (body-fun (if (eq ,var x) y x)))))))
452 (defmacro do-inheritable-constraints ((symbol (conset variable) &optional result)
454 (once-only ((conset conset)
457 (flet ((body-fun (,symbol)
459 (do-conset-constraints-intersection
460 (con (,conset (lambda-var-inheritable-constraints ,variable)))
462 (do-conset-constraints-intersection
463 (con (,conset (lambda-var-eql-var-constraints ,variable)) ,result)
466 (defmacro do-propagatable-constraints ((symbol (conset variable) &optional result)
468 (once-only ((conset conset)
471 (flet ((body-fun (,symbol)
473 (do-conset-constraints-intersection
474 (con (,conset (lambda-var-private-constraints ,variable)))
476 (do-conset-constraints-intersection
477 (con (,conset (lambda-var-eql-var-constraints ,variable)))
479 (do-conset-constraints-intersection
480 (con (,conset (lambda-var-inheritable-constraints ,variable)) ,result)
483 (declaim (inline conset-lvar-lambda-var-eql-p conset-add-lvar-lambda-var-eql))
484 (defun conset-lvar-lambda-var-eql-p (conset lvar lambda-var)
485 (let ((constraint (find-constraint 'eql lambda-var lvar nil)))
487 (conset-member constraint conset))))
489 (defun conset-add-lvar-lambda-var-eql (conset lvar lambda-var)
490 (let ((constraint (find-or-create-constraint 'eql lambda-var lvar nil)))
491 (conset-adjoin constraint conset)))
493 (declaim (inline conset-add-constraint conset-add-constraint-to-eql))
494 (defun conset-add-constraint (conset kind x y not-p)
495 (declare (type conset conset)
497 (conset-adjoin (find-or-create-constraint kind x y not-p)
500 (defun conset-add-constraint-to-eql (conset kind x y not-p &optional (target conset))
501 (declare (type conset target conset)
503 (do-eql-vars (x (x conset))
504 (conset-add-constraint target kind x y not-p)))
506 (declaim (inline conset-clear-lambda-var))
507 (defun conset-clear-lambda-var (conset var)
508 (conset-difference conset (lambda-var-constraints var)))
510 ;;; Copy all CONSTRAINTS involving FROM-VAR - except the (EQL VAR
511 ;;; LVAR) ones - to all of the variables in the VARS list.
512 (defun inherit-constraints (vars from-var constraints target)
513 (do-inheritable-constraints (con (constraints from-var))
514 (let ((eq-x (eq from-var (constraint-x con)))
515 (eq-y (eq from-var (constraint-y con))))
517 (conset-add-constraint target
518 (constraint-kind con)
519 (if eq-x var (constraint-x con))
520 (if eq-y var (constraint-y con))
521 (constraint-not-p con))))))
523 ;; Add an (EQL LAMBDA-VAR LAMBDA-VAR) constraint on VAR1 and VAR2 and
524 ;; inherit each other's constraints.
525 (defun add-eql-var-var-constraint (var1 var2 constraints
526 &optional (target constraints))
527 (let ((constraint (find-or-create-constraint 'eql var1 var2 nil)))
528 (unless (conset-member constraint target)
529 (conset-adjoin constraint target)
530 (collect ((eql1) (eql2))
531 (do-eql-vars (var1 (var1 constraints))
533 (do-eql-vars (var2 (var2 constraints))
535 (inherit-constraints (eql1) var2 constraints target)
536 (inherit-constraints (eql2) var1 constraints target))
539 ;;; If REF is to a LAMBDA-VAR with CONSTRAINTs (i.e. we can do flow
540 ;;; analysis on it), then return the LAMBDA-VAR, otherwise NIL.
541 #!-sb-fluid (declaim (inline ok-ref-lambda-var))
542 (defun ok-ref-lambda-var (ref)
543 (declare (type ref ref))
544 (let ((leaf (ref-leaf ref)))
545 (when (and (lambda-var-p leaf)
546 (lambda-var-constraints leaf))
549 ;;; See if LVAR's single USE is a REF to a LAMBDA-VAR and they are EQL
550 ;;; according to CONSTRAINTS. Return LAMBDA-VAR if so.
551 (defun ok-lvar-lambda-var (lvar constraints)
552 (declare (type lvar lvar))
553 (let ((use (lvar-uses lvar)))
555 (let ((lambda-var (ok-ref-lambda-var use)))
557 (conset-lvar-lambda-var-eql-p constraints lvar lambda-var)
560 (ok-lvar-lambda-var (cast-value use) constraints)))))
561 ;;;; Searching constraints
563 ;;; Add the indicated test constraint to BLOCK. We don't add the
564 ;;; constraint if the block has multiple predecessors, since it only
565 ;;; holds on this particular path.
566 (defun precise-add-test-constraint (fun x y not-p constraints target)
567 (if (and (eq 'eql fun) (lambda-var-p y) (not not-p))
568 (add-eql-var-var-constraint x y constraints target)
569 (conset-add-constraint-to-eql constraints fun x y not-p target))
572 (defun add-test-constraint (quick-p fun x y not-p constraints target)
574 (conset-add-constraint target fun x y not-p))
576 (precise-add-test-constraint fun x y not-p constraints target))))
577 ;;; Add complementary constraints to the consequent and alternative
578 ;;; blocks of IF. We do nothing if X is NIL.
579 (declaim (inline precise-add-test-constraint quick-add-complement-constraints))
580 (defun precise-add-complement-constraints (fun x y not-p constraints
581 consequent-constraints
582 alternative-constraints)
584 (precise-add-test-constraint fun x y not-p constraints
585 consequent-constraints)
586 (precise-add-test-constraint fun x y (not not-p) constraints
587 alternative-constraints))
590 (defun quick-add-complement-constraints (fun x y not-p
591 consequent-constraints
592 alternative-constraints)
594 (conset-add-constraint consequent-constraints fun x y not-p)
595 (conset-add-constraint alternative-constraints fun x y (not not-p)))
598 (defun add-complement-constraints (quick-p fun x y not-p constraints
599 consequent-constraints
600 alternative-constraints)
602 (quick-add-complement-constraints fun x y not-p
603 consequent-constraints
604 alternative-constraints)
605 (precise-add-complement-constraints fun x y not-p constraints
606 consequent-constraints
607 alternative-constraints)))
609 ;;; Add test constraints to the consequent and alternative blocks of
610 ;;; the test represented by USE.
611 (defun add-test-constraints (use if constraints)
612 (declare (type node use) (type cif if))
613 ;; Note: Even if we do (IF test exp exp) => (PROGN test exp)
614 ;; optimization, the *MAX-OPTIMIZE-ITERATIONS* cutoff means that we
615 ;; can't guarantee that the optimization will be done, so we still
616 ;; need to avoid barfing on this case.
617 (unless (eq (if-consequent if) (if-alternative if))
618 (let ((consequent-constraints (make-conset))
619 (alternative-constraints (make-conset))
620 (quick-p (policy if (> compilation-speed speed))))
621 (macrolet ((add (fun x y not-p)
622 `(add-complement-constraints quick-p
625 consequent-constraints
626 alternative-constraints)))
629 (add 'typep (ok-lvar-lambda-var (ref-lvar use) constraints)
630 (specifier-type 'null) t))
632 (unless (eq (combination-kind use)
634 (let ((name (lvar-fun-name
635 (basic-combination-fun use)))
636 (args (basic-combination-args use)))
638 ((%typep %instance-typep)
639 (let ((type (second args)))
640 (when (constant-lvar-p type)
641 (let ((val (lvar-value type)))
643 (ok-lvar-lambda-var (first args) constraints)
646 (let ((*compiler-error-context* use))
647 (specifier-type val)))
650 (let* ((arg1 (first args))
651 (var1 (ok-lvar-lambda-var arg1 constraints))
653 (var2 (ok-lvar-lambda-var arg2 constraints)))
654 ;; The code below assumes that the constant is the
655 ;; second argument in case of variable to constant
656 ;; comparision which is sometimes true (see source
657 ;; transformations for EQ, EQL and CHAR=). Fixing
658 ;; that would result in more constant substitutions
659 ;; which is not a universally good thing, thus the
660 ;; unnatural asymmetry of the tests.
663 (add-test-constraint quick-p
664 'typep var2 (lvar-type arg1)
666 consequent-constraints)))
668 (add 'eql var1 var2 nil))
669 ((constant-lvar-p arg2)
671 (let ((use (principal-lvar-use arg2)))
674 (find-constant (lvar-value arg2))))
677 (add-test-constraint quick-p
678 'typep var1 (lvar-type arg2)
680 consequent-constraints)))))
682 (let* ((arg1 (first args))
683 (var1 (ok-lvar-lambda-var arg1 constraints))
685 (var2 (ok-lvar-lambda-var arg2 constraints)))
687 (add name var1 (lvar-type arg2) nil))
689 (add (if (eq name '<) '> '<) var2 (lvar-type arg1) nil))))
691 (let ((ptype (gethash name *backend-predicate-types*)))
693 (add 'typep (ok-lvar-lambda-var (first args) constraints)
695 (values consequent-constraints alternative-constraints))))
697 ;;;; Applying constraints
699 ;;; Return true if X is an integer NUMERIC-TYPE.
700 (defun integer-type-p (x)
701 (declare (type ctype x))
702 (and (numeric-type-p x)
703 (eq (numeric-type-class x) 'integer)
704 (eq (numeric-type-complexp x) :real)))
706 ;;; Given that an inequality holds on values of type X and Y, return a
707 ;;; new type for X. If GREATER is true, then X was greater than Y,
708 ;;; otherwise less. If OR-EQUAL is true, then the inequality was
709 ;;; inclusive, i.e. >=.
711 ;;; If GREATER (or not), then we max (or min) in Y's lower (or upper)
712 ;;; bound into X and return that result. If not OR-EQUAL, we can go
713 ;;; one greater (less) than Y's bound.
714 (defun constrain-integer-type (x y greater or-equal)
715 (declare (type numeric-type x y))
722 (if greater (numeric-type-low x) (numeric-type-high x))))
723 (let* ((x-bound (bound x))
724 (y-bound (exclude (bound y)))
725 (new-bound (cond ((not x-bound) y-bound)
726 ((not y-bound) x-bound)
727 (greater (max x-bound y-bound))
728 (t (min x-bound y-bound)))))
730 (modified-numeric-type x :low new-bound)
731 (modified-numeric-type x :high new-bound)))))
733 ;;; Return true if X is a float NUMERIC-TYPE.
734 (defun float-type-p (x)
735 (declare (type ctype x))
736 (and (numeric-type-p x)
737 (eq (numeric-type-class x) 'float)
738 (eq (numeric-type-complexp x) :real)))
740 ;;; Exactly the same as CONSTRAIN-INTEGER-TYPE, but for float numbers.
741 (defun constrain-float-type (x y greater or-equal)
742 (declare (type numeric-type x y))
743 (declare (ignorable x y greater or-equal)) ; for CROSS-FLOAT-INFINITY-KLUDGE
745 (aver (eql (numeric-type-class x) 'float))
746 (aver (eql (numeric-type-class y) 'float))
747 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
749 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
750 (labels ((exclude (x)
758 (if greater (numeric-type-low x) (numeric-type-high x)))
763 (= (type-bound-number x) (type-bound-number ref)))
764 ;; X is tighter if REF is not an open bound and X is
765 (and (not (consp ref)) (consp x)))
767 (< (type-bound-number ref) (type-bound-number x)))
769 (> (type-bound-number ref) (type-bound-number x))))))
770 (let* ((x-bound (bound x))
771 (y-bound (exclude (bound y)))
772 (new-bound (cond ((not x-bound)
776 ((tighter-p y-bound x-bound)
781 (modified-numeric-type x :low new-bound)
782 (modified-numeric-type x :high new-bound)))))
784 ;;; Return true if LEAF is "visible" from NODE.
785 (defun leaf-visible-from-node-p (leaf node)
788 ;; A LAMBDA-VAR is visible iif it is homed in a CLAMBDA that is an
789 ;; ancestor for NODE.
790 (let ((leaf-lambda (lambda-var-home leaf)))
791 (loop for lambda = (node-home-lambda node)
792 then (lambda-parent lambda)
794 when (eq lambda leaf-lambda)
796 ;; FIXME: Check on FUNCTIONALs (CLAMBDAs and OPTIONAL-DISPATCHes),
797 ;; not just LAMBDA-VARs.
799 ;; Assume everything else is globally visible.
802 ;;; Given the set of CONSTRAINTS for a variable and the current set of
803 ;;; restrictions from flow analysis IN, set the type for REF
805 (defun constrain-ref-type (ref in)
806 (declare (type ref ref) (type conset in))
807 ;; KLUDGE: The NOT-SET and NOT-FPZ here are so that we don't need to
808 ;; cons up endless union types when propagating large number of EQL
809 ;; constraints -- eg. from large CASE forms -- instead we just
810 ;; directly accumulate one XSET, and a set of fp zeroes, which we at
811 ;; the end turn into a MEMBER-TYPE.
813 ;; Since massive symbol cases are an especially atrocious pattern
814 ;; and the (NOT (MEMBER ...ton of symbols...)) will never turn into
815 ;; a more useful type, don't propagate their negation except for NIL
816 ;; unless SPEED > COMPILATION-SPEED.
817 (let ((res (single-value-type (node-derived-type ref)))
818 (constrain-symbols (policy ref (> speed compilation-speed)))
819 (not-set (alloc-xset))
821 (not-res *empty-type*)
822 (leaf (ref-leaf ref)))
823 (declare (type lambda-var leaf))
827 (when (or constrain-symbols (null x) (not (symbolp x)))
828 (add-to-xset x not-set)))))
829 (do-propagatable-constraints (con (in leaf))
830 (let* ((x (constraint-x con))
831 (y (constraint-y con))
832 (not-p (constraint-not-p con))
833 (other (if (eq x leaf) y x))
834 (kind (constraint-kind con)))
838 (if (member-type-p other)
839 (mapc-member-type-members #'note-not other)
840 (setq not-res (type-union not-res other)))
841 (setq res (type-approx-intersection2 res other))))
843 (let ((other-type (leaf-type other)))
845 (when (and (constant-p other)
846 (member-type-p other-type))
847 (note-not (constant-value other)))
848 (let ((leaf-type (leaf-type leaf)))
850 ((or (constant-p other)
851 (and (leaf-refs other) ; protect from
853 (csubtypep other-type leaf-type)
854 (not (type= other-type leaf-type))
855 ;; Don't change to a LEAF not visible here.
856 (leaf-visible-from-node-p other ref)))
857 (change-ref-leaf ref other)
858 (when (constant-p other) (return)))
860 (setq res (type-approx-intersection2
861 res other-type))))))))
864 ((and (integer-type-p res) (integer-type-p y))
865 (let ((greater (eq kind '>)))
866 (let ((greater (if not-p (not greater) greater)))
868 (constrain-integer-type res y greater not-p)))))
869 ((and (float-type-p res) (float-type-p y))
870 (let ((greater (eq kind '>)))
871 (let ((greater (if not-p (not greater) greater)))
873 (constrain-float-type res y greater not-p)))))))))))
874 (cond ((and (if-p (node-dest ref))
875 (or (xset-member-p nil not-set)
876 (csubtypep (specifier-type 'null) not-res)))
877 (setf (node-derived-type ref) *wild-type*)
878 (change-ref-leaf ref (find-constant t)))
881 (type-union not-res (make-member-type :xset not-set :fp-zeroes not-fpz)))
882 (derive-node-type ref
883 (make-single-value-type
884 (or (type-difference res not-res)
886 (maybe-terminate-block ref nil))))
891 (defun maybe-add-eql-var-lvar-constraint (ref gen)
892 (let ((lvar (ref-lvar ref))
893 (leaf (ref-leaf ref)))
894 (when (and (lambda-var-p leaf) lvar)
895 (conset-add-lvar-lambda-var-eql gen lvar leaf))))
897 ;; Add an (EQL LAMBDA-VAR LAMBDA-VAR) constraint on VAR and LVAR's
898 ;; LAMBDA-VAR if possible.
899 (defun maybe-add-eql-var-var-constraint (var lvar constraints
900 &optional (target constraints))
901 (declare (type lambda-var var) (type lvar lvar))
902 (let ((lambda-var (ok-lvar-lambda-var lvar constraints)))
904 (add-eql-var-var-constraint var lambda-var constraints target))))
906 ;;; Local propagation
907 ;;; -- [TODO: For any LAMBDA-VAR ref with a type check, add that
909 ;;; -- For any LAMBDA-VAR set, delete all constraints on that var; add
910 ;;; a type constraint based on the new value type.
911 (declaim (ftype (function (cblock conset boolean)
913 constraint-propagate-in-block))
914 (defun constraint-propagate-in-block (block gen preprocess-refs-p)
915 (do-nodes (node lvar block)
918 (let ((fun (bind-lambda node)))
919 (when (eq (functional-kind fun) :let)
920 (loop with call = (lvar-dest (node-lvar (first (lambda-refs fun))))
921 for var in (lambda-vars fun)
922 and val in (combination-args call)
923 when (and val (lambda-var-constraints var))
924 do (let ((type (lvar-type val)))
925 (unless (eq type *universal-type*)
926 (conset-add-constraint gen 'typep var type nil)))
927 (maybe-add-eql-var-var-constraint var val gen)))))
929 (when (ok-ref-lambda-var node)
930 (maybe-add-eql-var-lvar-constraint node gen)
931 (when preprocess-refs-p
932 (constrain-ref-type node gen))))
934 (let ((lvar (cast-value node)))
935 (let ((var (ok-lvar-lambda-var lvar gen)))
937 (let ((atype (single-value-type (cast-derived-type node)))) ;FIXME
938 (unless (eq atype *universal-type*)
939 (conset-add-constraint-to-eql gen 'typep var atype nil)))))))
941 (binding* ((var (set-var node))
942 (nil (lambda-var-p var) :exit-if-null)
943 (nil (lambda-var-constraints var) :exit-if-null))
944 (when (policy node (and (= speed 3) (> speed compilation-speed)))
945 (let ((type (lambda-var-type var)))
946 (unless (eql *universal-type* type)
947 (do-eql-vars (other (var gen))
948 (unless (eql other var)
949 (conset-add-constraint gen 'typep other type nil))))))
950 (conset-clear-lambda-var gen var)
951 (let ((type (single-value-type (node-derived-type node))))
952 (unless (eq type *universal-type*)
953 (conset-add-constraint gen 'typep var type nil)))
954 (unless (policy node (> compilation-speed speed))
955 (maybe-add-eql-var-var-constraint var (set-value node) gen))))))
958 (defun constraint-propagate-if (block gen)
959 (let ((node (block-last block)))
961 (let ((use (lvar-uses (if-test node))))
963 (add-test-constraints use node gen))))))
965 ;;; Starting from IN compute OUT and (consequent/alternative
966 ;;; constraints if the block ends with and IF). Return the list of
967 ;;; successors that may need to be recomputed.
968 (defun find-block-type-constraints (block final-pass-p)
969 (declare (type cblock block))
970 (let ((gen (constraint-propagate-in-block
974 (copy-conset (block-in block)))
976 (setf (block-gen block) gen)
977 (multiple-value-bind (consequent-constraints alternative-constraints)
978 (constraint-propagate-if block gen)
979 (if consequent-constraints
980 (let* ((node (block-last block))
981 (old-consequent-constraints (if-consequent-constraints node))
982 (old-alternative-constraints (if-alternative-constraints node))
984 ;; Add the consequent and alternative constraints to GEN.
985 (cond ((conset-empty consequent-constraints)
986 (setf (if-consequent-constraints node) gen)
987 (setf (if-alternative-constraints node) gen))
989 (setf (if-consequent-constraints node) (copy-conset gen))
990 (conset-union (if-consequent-constraints node)
991 consequent-constraints)
992 (setf (if-alternative-constraints node) gen)
993 (conset-union (if-alternative-constraints node)
994 alternative-constraints)))
995 ;; Has the consequent been changed?
996 (unless (and old-consequent-constraints
997 (conset= (if-consequent-constraints node)
998 old-consequent-constraints))
999 (push (if-consequent node) succ))
1000 ;; Has the alternative been changed?
1001 (unless (and old-alternative-constraints
1002 (conset= (if-alternative-constraints node)
1003 old-alternative-constraints))
1004 (push (if-alternative node) succ))
1007 (unless (and (block-out block)
1008 (conset= gen (block-out block)))
1009 (setf (block-out block) gen)
1010 (block-succ block))))))
1012 ;;; Deliver the results of constraint propagation to REFs in BLOCK.
1013 ;;; During this pass, we also do local constraint propagation by
1014 ;;; adding in constraints as we see them during the pass through the
1016 (defun use-result-constraints (block)
1017 (declare (type cblock block))
1018 (constraint-propagate-in-block block (block-in block) t))
1020 ;;; Give an empty constraints set to any var that doesn't have one and
1021 ;;; isn't a set closure var. Since a var that we previously rejected
1022 ;;; looks identical to one that is new, so we optimistically keep
1023 ;;; hoping that vars stop being closed over or lose their sets.
1024 (defun init-var-constraints (component)
1025 (declare (type component component))
1026 (dolist (fun (component-lambdas component))
1028 (dolist (var (lambda-vars x))
1029 (unless (lambda-var-constraints var)
1030 (when (or (null (lambda-var-sets var))
1031 (not (closure-var-p var)))
1032 (setf (lambda-var-constraints var) (make-conset)))))))
1034 (dolist (let (lambda-lets fun))
1037 ;;; Return the constraints that flow from PRED to SUCC. This is
1038 ;;; BLOCK-OUT unless PRED ends with an IF and test constraints were
1040 (defun block-out-for-successor (pred succ)
1041 (declare (type cblock pred succ))
1042 (let ((last (block-last pred)))
1043 (or (when (if-p last)
1044 (cond ((eq succ (if-consequent last))
1045 (if-consequent-constraints last))
1046 ((eq succ (if-alternative last))
1047 (if-alternative-constraints last))))
1050 (defun compute-block-in (block)
1052 (dolist (pred (block-pred block))
1053 ;; If OUT has not been calculated, assume it to be the universal
1055 (let ((out (block-out-for-successor pred block)))
1058 (conset-intersection in out)
1059 (setq in (copy-conset out))))))
1060 (or in (make-conset))))
1062 (defun update-block-in (block)
1063 (let ((in (compute-block-in block)))
1064 (cond ((and (block-in block) (conset= in (block-in block)))
1067 (setf (block-in block) in)))))
1069 ;;; Return two lists: one of blocks that precede all loops and
1070 ;;; therefore require only one constraint propagation pass and the
1071 ;;; rest. This implementation does not find all such blocks.
1073 ;;; A more complete implementation would be:
1075 ;;; (do-blocks (block component)
1076 ;;; (if (every #'(lambda (pred)
1077 ;;; (or (member pred leading-blocks)
1078 ;;; (eq pred head)))
1079 ;;; (block-pred block))
1080 ;;; (push block leading-blocks)
1081 ;;; (push block rest-of-blocks)))
1083 ;;; Trailing blocks that succeed all loops could be found and handled
1084 ;;; similarly. In practice though, these more complex solutions are
1085 ;;; slightly worse performancewise.
1086 (defun leading-component-blocks (component)
1087 (declare (type component component))
1088 (flet ((loopy-p (block)
1089 (let ((n (block-number block)))
1090 (dolist (pred (block-pred block))
1091 (unless (< n (block-number pred))
1093 (let ((leading-blocks ())
1096 (do-blocks (block component)
1097 (when (and (not seen-loop-p) (loopy-p block))
1098 (setq seen-loop-p t))
1100 (push block rest-of-blocks)
1101 (push block leading-blocks)))
1102 (values (nreverse leading-blocks) (nreverse rest-of-blocks)))))
1104 ;;; Append OBJ to the end of LIST as if by NCONC but only if it is not
1105 ;;; a member already.
1106 (defun nconc-new (obj list)
1107 (do ((x list (cdr x))
1111 (setf (cdr prev) (list obj))
1114 (when (eql (car x) obj)
1115 (return-from nconc-new list))))
1117 (defun find-and-propagate-constraints (component)
1118 (let ((blocks-to-process ()))
1119 (flet ((enqueue (blocks)
1120 (dolist (block blocks)
1121 (setq blocks-to-process (nconc-new block blocks-to-process)))))
1122 (multiple-value-bind (leading-blocks rest-of-blocks)
1123 (leading-component-blocks component)
1124 ;; Update every block once to account for changes in the
1125 ;; IR1. The constraints of the lead blocks cannot be changed
1126 ;; after the first pass so we might as well use them and skip
1127 ;; USE-RESULT-CONSTRAINTS later.
1128 (dolist (block leading-blocks)
1129 (setf (block-in block) (compute-block-in block))
1130 (find-block-type-constraints block t))
1131 (setq blocks-to-process (copy-list rest-of-blocks))
1132 ;; The rest of the blocks.
1133 (dolist (block rest-of-blocks)
1134 (aver (eq block (pop blocks-to-process)))
1135 (setf (block-in block) (compute-block-in block))
1136 (enqueue (find-block-type-constraints block nil)))
1137 ;; Propagate constraints
1138 (loop for block = (pop blocks-to-process)
1140 (unless (eq block (component-tail component))
1141 (when (update-block-in block)
1142 (enqueue (find-block-type-constraints block nil)))))
1145 (defun constraint-propagate (component)
1146 (declare (type component component))
1147 (init-var-constraints component)
1149 (unless (block-out (component-head component))
1150 (setf (block-out (component-head component)) (make-conset)))
1152 (dolist (block (find-and-propagate-constraints component))
1153 (unless (block-delete-p block)
1154 (use-result-constraints block)))