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.
16 (defstruct (constraint
17 (:include sset-element)
18 (:constructor make-constraint (number kind x y not-p))
20 ;; the kind of constraint we have:
23 ;; X is a LAMBDA-VAR and Y is a CTYPE. The value of X is
24 ;; constrained to be of type Y.
27 ;; X is a lambda-var and Y is a CTYPE. The relation holds
28 ;; between X and some object of type Y.
31 ;; X is a LAMBDA-VAR Y is a LAMBDA-VAR or a CONSTANT. The
32 ;; relation is asserted to hold.
33 (kind nil :type (member typep < > eql))
34 ;; The operands to the relation.
35 (x nil :type lambda-var)
36 (y nil :type (or ctype lambda-var constant))
37 ;; If true, negates the sense of the constraint, so the relation
39 (not-p nil :type boolean))
41 (defvar *constraint-number*)
43 ;;; Return a constraint for the specified arguments. We only create a
44 ;;; new constraint if there isn't already an equivalent old one,
45 ;;; guaranteeing that all equivalent constraints are EQ. This
46 ;;; shouldn't be called on LAMBDA-VARs with no CONSTRAINTS set.
47 (defun find-constraint (kind x y not-p)
48 (declare (type lambda-var x) (type (or constant lambda-var ctype) y)
52 (do-sset-elements (con (lambda-var-constraints x) nil)
53 (when (and (eq (constraint-kind con) kind)
54 (eq (constraint-not-p con) not-p)
55 (type= (constraint-y con) y))
58 (do-sset-elements (con (lambda-var-constraints x) nil)
59 (when (and (eq (constraint-kind con) kind)
60 (eq (constraint-not-p con) not-p)
61 (eq (constraint-y con) y))
64 (do-sset-elements (con (lambda-var-constraints x) nil)
65 (when (and (eq (constraint-kind con) kind)
66 (eq (constraint-not-p con) not-p)
67 (let ((cx (constraint-x con)))
73 (let ((new (make-constraint (incf *constraint-number*) kind x y not-p)))
74 (sset-adjoin new (lambda-var-constraints x))
75 (when (lambda-var-p y)
76 (sset-adjoin new (lambda-var-constraints y)))
79 ;;; If REF is to a LAMBDA-VAR with CONSTRAINTs (i.e. we can do flow
80 ;;; analysis on it), then return the LAMBDA-VAR, otherwise NIL.
81 #!-sb-fluid (declaim (inline ok-ref-lambda-var))
82 (defun ok-ref-lambda-var (ref)
83 (declare (type ref ref))
84 (let ((leaf (ref-leaf ref)))
85 (when (and (lambda-var-p leaf)
86 (lambda-var-constraints leaf))
89 ;;; If CONT's USE is a REF, then return OK-REF-LAMBDA-VAR of the USE,
91 #!-sb-fluid (declaim (inline ok-cont-lambda-var))
92 (defun ok-cont-lambda-var (cont)
93 (declare (type continuation cont))
94 (let ((use (continuation-use cont)))
96 (ok-ref-lambda-var use))))
98 ;;; Add the indicated test constraint to BLOCK, marking the block as
99 ;;; having a new assertion when the constriant was not already
100 ;;; present. We don't add the constraint if the block has multiple
101 ;;; predecessors, since it only holds on this particular path.
102 (defun add-test-constraint (block fun x y not-p)
103 (unless (rest (block-pred block))
104 (let ((con (find-constraint fun x y not-p))
105 (old (or (block-test-constraint block)
106 (setf (block-test-constraint block) (make-sset)))))
107 (when (sset-adjoin con old)
108 (setf (block-type-asserted block) t))))
111 ;;; Add complementary constraints to the consequent and alternative
112 ;;; blocks of IF. We do nothing if X is NIL.
113 #!-sb-fluid (declaim (inline add-complement-constraints))
114 (defun add-complement-constraints (if fun x y not-p)
116 (add-test-constraint (if-consequent if) fun x y not-p)
117 (add-test-constraint (if-alternative if) fun x y (not not-p)))
120 ;;; Add test constraints to the consequent and alternative blocks of
121 ;;; the test represented by USE.
122 (defun add-test-constraints (use if)
123 (declare (type node use) (type cif if))
126 (add-complement-constraints if 'typep (ok-ref-lambda-var use)
127 (specifier-type 'null) t))
129 (unless (eq (combination-kind use)
131 (let ((name (continuation-fun-name
132 (basic-combination-fun use)))
133 (args (basic-combination-args use)))
135 ((%typep %instance-typep)
136 (let ((type (second args)))
137 (when (constant-continuation-p type)
138 (let ((val (continuation-value type)))
139 (add-complement-constraints if 'typep
140 (ok-cont-lambda-var (first args))
143 (specifier-type val))
146 (let* ((var1 (ok-cont-lambda-var (first args)))
148 (var2 (ok-cont-lambda-var arg2)))
151 (add-complement-constraints if 'eql var1 var2 nil))
152 ((constant-continuation-p arg2)
153 (add-complement-constraints if 'eql var1
155 (continuation-use arg2))
158 (let* ((arg1 (first args))
159 (var1 (ok-cont-lambda-var arg1))
161 (var2 (ok-cont-lambda-var arg2)))
163 (add-complement-constraints if name var1 (continuation-type arg2)
166 (add-complement-constraints if (if (eq name '<) '> '<)
167 var2 (continuation-type arg1)
170 (let ((ptype (gethash name *backend-predicate-types*)))
172 (add-complement-constraints if 'typep
173 (ok-cont-lambda-var (first args))
177 ;;; Set the TEST-CONSTRAINT in the successors of BLOCK according to
178 ;;; the condition it tests.
179 (defun find-test-constraints (block)
180 (declare (type cblock block))
181 (let ((last (block-last block)))
183 (let ((use (continuation-use (if-test last))))
185 (add-test-constraints use last)))))
187 (setf (block-test-modified block) nil)
190 ;;; Compute the initial flow analysis sets for BLOCK:
191 ;;; -- For any lambda-var ref with a type check, add that constraint.
192 ;;; -- For any LAMBDA-VAR set, delete all constraints on that var, and add
193 ;;; those constraints to the set nuked by this block.
194 (defun find-block-type-constraints (block)
195 (declare (type cblock block))
196 (let ((gen (make-sset)))
197 (collect ((kill nil adjoin))
199 (let ((test (block-test-constraint block)))
201 (sset-union gen test)))
203 (do-nodes (node cont block)
206 (when (continuation-type-check cont)
207 (let ((var (ok-ref-lambda-var node)))
209 (let* ((atype (continuation-derived-type cont))
210 (con (find-constraint 'typep var atype nil)))
211 (sset-adjoin con gen))))))
213 (let ((var (set-var node)))
214 (when (lambda-var-p var)
216 (let ((cons (lambda-var-constraints var)))
218 (sset-difference gen cons))))))))
220 (setf (block-in block) nil)
221 (setf (block-gen block) gen)
222 (setf (block-kill-list block) (kill))
223 (setf (block-out block) (copy-sset gen))
224 (setf (block-type-asserted block) nil)
227 ;;; Return true if X is an integer NUMERIC-TYPE.
228 (defun integer-type-p (x)
229 (declare (type ctype x))
230 (and (numeric-type-p x)
231 (eq (numeric-type-class x) 'integer)
232 (eq (numeric-type-complexp x) :real)))
234 ;;; Given that an inequality holds on values of type X and Y, return a
235 ;;; new type for X. If GREATER is true, then X was greater than Y,
236 ;;; otherwise less. If OR-EQUAL is true, then the inequality was
237 ;;; inclusive, i.e. >=.
239 ;;; If GREATER (or not), then we max (or min) in Y's lower (or upper)
240 ;;; bound into X and return that result. If not OR-EQUAL, we can go
241 ;;; one greater (less) than Y's bound.
242 (defun constrain-integer-type (x y greater or-equal)
243 (declare (type numeric-type x y))
250 (if greater (numeric-type-low x) (numeric-type-high x))))
251 (let* ((x-bound (bound x))
252 (y-bound (exclude (bound y)))
253 (new-bound (cond ((not x-bound) y-bound)
254 ((not y-bound) x-bound)
255 (greater (max x-bound y-bound))
256 (t (min x-bound y-bound)))))
258 (modified-numeric-type x :low new-bound)
259 (modified-numeric-type x :high new-bound)))))
261 ;;; Return true if X is a float NUMERIC-TYPE.
262 (defun float-type-p (x)
263 (declare (type ctype x))
264 (and (numeric-type-p x)
265 (eq (numeric-type-class x) 'float)
266 (eq (numeric-type-complexp x) :real)))
268 ;;; Exactly the same as CONSTRAIN-INTEGER-TYPE, but for float numbers.
269 (defun constrain-float-type (x y greater or-equal)
270 (declare (type numeric-type x y))
271 (declare (ignorable x y greater or-equal)) ; for CROSS-FLOAT-INFINITY-KLUDGE
273 (aver (eql (numeric-type-class x) 'float))
274 (aver (eql (numeric-type-class y) 'float))
275 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
277 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
278 (labels ((exclude (x)
290 (if greater (numeric-type-low x) (numeric-type-high x)))
291 (max-lower-bound (x y)
292 ;; Both X and Y are not null. Find the max.
293 (let ((res (max (type-bound-number x) (type-bound-number y))))
294 ;; An open lower bound is greater than a close
295 ;; lower bound because the open bound doesn't
296 ;; contain the bound, so choose an open lower
298 (set-bound res (or (consp x) (consp y)))))
299 (min-upper-bound (x y)
300 ;; Same as above, but for the min of upper bounds
301 ;; Both X and Y are not null. Find the min.
302 (let ((res (min (type-bound-number x) (type-bound-number y))))
303 ;; An open upper bound is less than a closed
304 ;; upper bound because the open bound doesn't
305 ;; contain the bound, so choose an open lower
307 (set-bound res (or (consp x) (consp y))))))
308 (let* ((x-bound (bound x))
309 (y-bound (exclude (bound y)))
310 (new-bound (cond ((not x-bound)
315 (max-lower-bound x-bound y-bound))
317 (min-upper-bound x-bound y-bound)))))
319 (modified-numeric-type x :low new-bound)
320 (modified-numeric-type x :high new-bound)))))
322 ;;; Given the set of CONSTRAINTS for a variable and the current set of
323 ;;; restrictions from flow analysis IN, set the type for REF
325 (defun constrain-ref-type (ref constraints in)
326 (declare (type ref ref) (type sset constraints in))
327 (let ((var-cons (copy-sset constraints)))
328 (sset-intersection var-cons in)
329 (let ((res (single-value-type (node-derived-type ref)))
330 (not-res *empty-type*)
331 (leaf (ref-leaf ref)))
332 (do-sset-elements (con var-cons)
333 (let* ((x (constraint-x con))
334 (y (constraint-y con))
335 (not-p (constraint-not-p con))
336 (other (if (eq x leaf) y x))
337 (kind (constraint-kind con)))
341 (setq not-res (type-union not-res other))
342 (setq res (type-approx-intersection2 res other))))
344 (let ((other-type (leaf-type other)))
346 (when (and (constant-p other)
347 (member-type-p other-type))
348 (setq not-res (type-union not-res other-type)))
349 (let ((leaf-type (leaf-type leaf)))
350 (when (or (constant-p other)
351 (and (csubtypep other-type leaf-type)
352 (not (type= other-type leaf-type))))
353 (change-ref-leaf ref other)
354 (when (constant-p other) (return)))))))
356 (cond ((and (integer-type-p res) (integer-type-p y))
357 (let ((greater (eq kind '>)))
358 (let ((greater (if not-p (not greater) greater)))
360 (constrain-integer-type res y greater not-p)))))
361 ((and (float-type-p res) (float-type-p y))
362 (let ((greater (eq kind '>)))
363 (let ((greater (if not-p (not greater) greater)))
365 (constrain-float-type res y greater not-p)))))
368 (let* ((cont (node-cont ref))
369 (dest (continuation-dest cont)))
370 (cond ((and (if-p dest)
371 (csubtypep (specifier-type 'null) not-res)
372 (eq (continuation-asserted-type cont) *wild-type*))
373 (setf (node-derived-type ref) *wild-type*)
374 (change-ref-leaf ref (find-constant t)))
376 (derive-node-type ref (or (type-difference res not-res)
381 ;;; Deliver the results of constraint propagation to REFs in BLOCK.
382 ;;; During this pass, we also do local constraint propagation by
383 ;;; adding in constraints as we seem them during the pass through the
385 (defun use-result-constraints (block)
386 (declare (type cblock block))
387 (let ((in (block-in block)))
389 (let ((test (block-test-constraint block)))
391 (sset-union in test)))
393 (do-nodes (node cont block)
396 (let ((var (ref-leaf node)))
397 (when (lambda-var-p var)
398 (let ((con (lambda-var-constraints var)))
400 (constrain-ref-type node con in)
401 (when (continuation-type-check cont)
403 (find-constraint 'typep var
404 (continuation-asserted-type cont)
408 (let ((var (set-var node)))
409 (when (lambda-var-p var)
410 (let ((cons (lambda-var-constraints var)))
412 (sset-difference in cons))))))))))
414 ;;; Return true if VAR would have to be closed over if environment
415 ;;; analysis ran now (i.e. if there are any uses that have a different
416 ;;; home lambda than VAR's home.)
417 (defun closure-var-p (var)
418 (declare (type lambda-var var))
419 (let ((home (lambda-home (lambda-var-home var))))
422 (unless (eq (node-home-lambda node) home)
424 (or (frob (leaf-refs var))
425 (frob (basic-var-sets var))))))
427 ;;; Give an empty constraints set to any var that doesn't have one and
428 ;;; isn't a set closure var. Since a var that we previously rejected
429 ;;; looks identical to one that is new, so we optimistically keep
430 ;;; hoping that vars stop being closed over or lose their sets.
431 (defun init-var-constraints (component)
432 (declare (type component component))
433 (dolist (fun (component-lambdas component))
435 (dolist (var (lambda-vars x))
436 (unless (lambda-var-constraints var)
437 (when (or (null (lambda-var-sets var))
438 (not (closure-var-p var)))
439 (setf (lambda-var-constraints var) (make-sset)))))))
441 (dolist (let (lambda-lets fun))
444 ;;; BLOCK-IN becomes the intersection of the OUT of the predecessors.
446 ;;; out U (in - kill)
448 ;;; BLOCK-KILL-LIST is just a list of the LAMBDA-VARs killed, so we must
449 ;;; compute the kill set when there are any vars killed. We bum this a
450 ;;; bit by special-casing when only one var is killed, and just using
451 ;;; that var's constraints as the kill set. This set could possibly be
452 ;;; precomputed, but it would have to be invalidated whenever any
453 ;;; constraint is added, which would be a pain.
454 (defun flow-propagate-constraints (block)
455 (let* ((pred (block-pred block))
457 (let ((res (copy-sset (block-out (first pred)))))
458 (dolist (b (rest pred))
459 (sset-intersection res (block-out b)))
462 (let ((*compiler-error-context* (block-last block)))
464 "unreachable code in constraint ~
465 propagation -- apparent compiler bug"))
467 (kill-list (block-kill-list block))
468 (out (block-out block)))
470 (setf (block-in block) in)
471 (cond ((null kill-list)
472 (sset-union (block-out block) in))
473 ((null (rest kill-list))
474 (let ((con (lambda-var-constraints (first kill-list))))
476 (sset-union-of-difference out in con)
477 (sset-union out in))))
479 (let ((kill-set (make-sset)))
480 (dolist (var kill-list)
481 (let ((con (lambda-var-constraints var)))
483 (sset-union kill-set con))))
484 (sset-union-of-difference (block-out block) in kill-set))))))
486 ;;; How many blocks does COMPONENT have?
487 (defun component-n-blocks (component)
489 (declare (type index result))
490 (do-blocks (block component :both)
494 (defun constraint-propagate (component)
495 (declare (type component component))
496 (init-var-constraints component)
498 (do-blocks (block component)
499 (when (block-test-modified block)
500 (find-test-constraints block)))
502 (do-blocks (block component)
503 (cond ((block-type-asserted block)
504 (find-block-type-constraints block))
506 (setf (block-in block) nil)
507 (setf (block-out block) (copy-sset (block-gen block))))))
509 (setf (block-out (component-head component)) (make-sset))
511 (let (;; If we have to propagate changes more than this many times,
512 ;; something is wrong.
513 (max-n-changes-remaining (component-n-blocks component)))
514 (declare (type fixnum max-n-changes-remaining))
515 (loop (aver (plusp max-n-changes-remaining))
516 (decf max-n-changes-remaining)
517 (let ((did-something nil))
518 (do-blocks (block component)
519 (when (flow-propagate-constraints block)
520 (setq did-something t)))
521 (unless did-something
524 (do-blocks (block component)
525 (use-result-constraints block))