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
22 ;;; -- Constraint propagation badly interacts with bottom-up type
23 ;;; inference. Consider
25 ;;; (defun foo (n &aux (i 42))
26 ;;; (declare (optimize speed))
27 ;;; (declare (fixnum n)
28 ;;; #+nil (type (integer 0) i))
32 ;;; (when (>= i n) (go :exit))
37 ;;; In this case CP cannot even infer that I is of class INTEGER.
39 ;;; -- In the above example if we place the check after SETQ, CP will
40 ;;; fail to infer (< I FIXNUM): is does not understand that this
41 ;;; constraint follows from (TYPEP I (INTEGER 0 0)).
45 ;;; -- this code does not check whether SET appears between REF and a
48 ;;; -- type check is assumed to be inserted immediately after a node
49 ;;; producing the value; it disagrees with the rest of Python (bug
54 (defstruct (constraint
55 (:include sset-element)
56 (:constructor make-constraint (number kind x y not-p))
58 ;; the kind of constraint we have:
61 ;; X is a LAMBDA-VAR and Y is a CTYPE. The value of X is
62 ;; constrained to be of type Y.
65 ;; X is a lambda-var and Y is a CTYPE. The relation holds
66 ;; between X and some object of type Y.
69 ;; X is a LAMBDA-VAR Y is a LAMBDA-VAR or a CONSTANT. The
70 ;; relation is asserted to hold.
71 (kind nil :type (member typep < > eql))
72 ;; The operands to the relation.
73 (x nil :type lambda-var)
74 (y nil :type (or ctype lambda-var constant))
75 ;; If true, negates the sense of the constraint, so the relation
77 (not-p nil :type boolean))
79 (defvar *constraint-number*)
81 ;;; Return a constraint for the specified arguments. We only create a
82 ;;; new constraint if there isn't already an equivalent old one,
83 ;;; guaranteeing that all equivalent constraints are EQ. This
84 ;;; shouldn't be called on LAMBDA-VARs with no CONSTRAINTS set.
85 (defun find-constraint (kind x y not-p)
86 (declare (type lambda-var x) (type (or constant lambda-var ctype) y)
90 (do-sset-elements (con (lambda-var-constraints x) nil)
91 (when (and (eq (constraint-kind con) kind)
92 (eq (constraint-not-p con) not-p)
93 (type= (constraint-y con) y))
96 (do-sset-elements (con (lambda-var-constraints x) nil)
97 (when (and (eq (constraint-kind con) kind)
98 (eq (constraint-not-p con) not-p)
99 (eq (constraint-y con) y))
102 (do-sset-elements (con (lambda-var-constraints x) nil)
103 (when (and (eq (constraint-kind con) kind)
104 (eq (constraint-not-p con) not-p)
105 (let ((cx (constraint-x con)))
111 (let ((new (make-constraint (incf *constraint-number*) kind x y not-p)))
112 (sset-adjoin new (lambda-var-constraints x))
113 (when (lambda-var-p y)
114 (sset-adjoin new (lambda-var-constraints y)))
117 ;;; If REF is to a LAMBDA-VAR with CONSTRAINTs (i.e. we can do flow
118 ;;; analysis on it), then return the LAMBDA-VAR, otherwise NIL.
119 #!-sb-fluid (declaim (inline ok-ref-lambda-var))
120 (defun ok-ref-lambda-var (ref)
121 (declare (type ref ref))
122 (let ((leaf (ref-leaf ref)))
123 (when (and (lambda-var-p leaf)
124 (lambda-var-constraints leaf))
127 ;;; If CONT's USE is a REF, then return OK-REF-LAMBDA-VAR of the USE,
129 #!-sb-fluid (declaim (inline ok-cont-lambda-var))
130 (defun ok-cont-lambda-var (cont)
131 (declare (type continuation cont))
132 (let ((use (continuation-use cont)))
134 (ok-ref-lambda-var use))))
136 ;;;; Searching constraints
138 ;;; Add the indicated test constraint to BLOCK, marking the block as
139 ;;; having a new assertion when the constriant was not already
140 ;;; present. We don't add the constraint if the block has multiple
141 ;;; predecessors, since it only holds on this particular path.
142 (defun add-test-constraint (block fun x y not-p)
143 (unless (rest (block-pred block))
144 (let ((con (find-constraint fun x y not-p))
145 (old (or (block-test-constraint block)
146 (setf (block-test-constraint block) (make-sset)))))
147 (when (sset-adjoin con old)
148 (setf (block-type-asserted block) t))))
151 ;;; Add complementary constraints to the consequent and alternative
152 ;;; blocks of IF. We do nothing if X is NIL.
153 (defun add-complement-constraints (if fun x y not-p)
155 ;; Note: Even if we do (IF test exp exp) => (PROGN test exp)
156 ;; optimization, the *MAX-OPTIMIZE-ITERATIONS* cutoff means
157 ;; that we can't guarantee that the optimization will be
158 ;; done, so we still need to avoid barfing on this case.
159 (not (eq (if-consequent if)
160 (if-alternative if))))
161 (add-test-constraint (if-consequent if) fun x y not-p)
162 (add-test-constraint (if-alternative if) fun x y (not not-p)))
165 ;;; Add test constraints to the consequent and alternative blocks of
166 ;;; the test represented by USE.
167 (defun add-test-constraints (use if)
168 (declare (type node use) (type cif if))
171 (add-complement-constraints if 'typep (ok-ref-lambda-var use)
172 (specifier-type 'null) t))
174 (unless (eq (combination-kind use)
176 (let ((name (continuation-fun-name
177 (basic-combination-fun use)))
178 (args (basic-combination-args use)))
180 ((%typep %instance-typep)
181 (let ((type (second args)))
182 (when (constant-continuation-p type)
183 (let ((val (continuation-value type)))
184 (add-complement-constraints if 'typep
185 (ok-cont-lambda-var (first args))
188 (specifier-type val))
191 (let* ((var1 (ok-cont-lambda-var (first args)))
193 (var2 (ok-cont-lambda-var arg2)))
196 (add-complement-constraints if 'eql var1 var2 nil))
197 ((constant-continuation-p arg2)
198 (add-complement-constraints if 'eql var1
200 (continuation-use arg2))
203 (let* ((arg1 (first args))
204 (var1 (ok-cont-lambda-var arg1))
206 (var2 (ok-cont-lambda-var arg2)))
208 (add-complement-constraints if name var1 (continuation-type arg2)
211 (add-complement-constraints if (if (eq name '<) '> '<)
212 var2 (continuation-type arg1)
215 (let ((ptype (gethash name *backend-predicate-types*)))
217 (add-complement-constraints if 'typep
218 (ok-cont-lambda-var (first args))
222 ;;; Set the TEST-CONSTRAINT in the successors of BLOCK according to
223 ;;; the condition it tests.
224 (defun find-test-constraints (block)
225 (declare (type cblock block))
226 (let ((last (block-last block)))
228 (let ((use (continuation-use (if-test last))))
230 (add-test-constraints use last)))))
232 (setf (block-test-modified block) nil)
235 ;;;; Applying constraints
237 ;;; Return true if X is an integer NUMERIC-TYPE.
238 (defun integer-type-p (x)
239 (declare (type ctype x))
240 (and (numeric-type-p x)
241 (eq (numeric-type-class x) 'integer)
242 (eq (numeric-type-complexp x) :real)))
244 ;;; Given that an inequality holds on values of type X and Y, return a
245 ;;; new type for X. If GREATER is true, then X was greater than Y,
246 ;;; otherwise less. If OR-EQUAL is true, then the inequality was
247 ;;; inclusive, i.e. >=.
249 ;;; If GREATER (or not), then we max (or min) in Y's lower (or upper)
250 ;;; bound into X and return that result. If not OR-EQUAL, we can go
251 ;;; one greater (less) than Y's bound.
252 (defun constrain-integer-type (x y greater or-equal)
253 (declare (type numeric-type x y))
260 (if greater (numeric-type-low x) (numeric-type-high x))))
261 (let* ((x-bound (bound x))
262 (y-bound (exclude (bound y)))
263 (new-bound (cond ((not x-bound) y-bound)
264 ((not y-bound) x-bound)
265 (greater (max x-bound y-bound))
266 (t (min x-bound y-bound)))))
268 (modified-numeric-type x :low new-bound)
269 (modified-numeric-type x :high new-bound)))))
271 ;;; Return true if X is a float NUMERIC-TYPE.
272 (defun float-type-p (x)
273 (declare (type ctype x))
274 (and (numeric-type-p x)
275 (eq (numeric-type-class x) 'float)
276 (eq (numeric-type-complexp x) :real)))
278 ;;; Exactly the same as CONSTRAIN-INTEGER-TYPE, but for float numbers.
279 (defun constrain-float-type (x y greater or-equal)
280 (declare (type numeric-type x y))
281 (declare (ignorable x y greater or-equal)) ; for CROSS-FLOAT-INFINITY-KLUDGE
283 (aver (eql (numeric-type-class x) 'float))
284 (aver (eql (numeric-type-class y) 'float))
285 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
287 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
288 (labels ((exclude (x)
300 (if greater (numeric-type-low x) (numeric-type-high x)))
301 (max-lower-bound (x y)
302 ;; Both X and Y are not null. Find the max.
303 (let ((res (max (type-bound-number x) (type-bound-number y))))
304 ;; An open lower bound is greater than a close
305 ;; lower bound because the open bound doesn't
306 ;; contain the bound, so choose an open lower
308 (set-bound res (or (consp x) (consp y)))))
309 (min-upper-bound (x y)
310 ;; Same as above, but for the min of upper bounds
311 ;; Both X and Y are not null. Find the min.
312 (let ((res (min (type-bound-number x) (type-bound-number y))))
313 ;; An open upper bound is less than a closed
314 ;; upper bound because the open bound doesn't
315 ;; contain the bound, so choose an open lower
317 (set-bound res (or (consp x) (consp y))))))
318 (let* ((x-bound (bound x))
319 (y-bound (exclude (bound y)))
320 (new-bound (cond ((not x-bound)
325 (max-lower-bound x-bound y-bound))
327 (min-upper-bound x-bound y-bound)))))
329 (modified-numeric-type x :low new-bound)
330 (modified-numeric-type x :high new-bound)))))
332 ;;; Given the set of CONSTRAINTS for a variable and the current set of
333 ;;; restrictions from flow analysis IN, set the type for REF
335 (defun constrain-ref-type (ref constraints in)
336 (declare (type ref ref) (type sset constraints in))
337 (let ((var-cons (copy-sset constraints)))
338 (sset-intersection var-cons in)
339 (let ((res (single-value-type (node-derived-type ref)))
340 (not-res *empty-type*)
341 (leaf (ref-leaf ref)))
342 (do-sset-elements (con var-cons)
343 (let* ((x (constraint-x con))
344 (y (constraint-y con))
345 (not-p (constraint-not-p con))
346 (other (if (eq x leaf) y x))
347 (kind (constraint-kind con)))
351 (setq not-res (type-union not-res other))
352 (setq res (type-approx-intersection2 res other))))
354 (let ((other-type (leaf-type other)))
356 (when (and (constant-p other)
357 (member-type-p other-type))
358 (setq not-res (type-union not-res other-type)))
359 (let ((leaf-type (leaf-type leaf)))
360 (when (or (constant-p other)
361 (and (csubtypep other-type leaf-type)
362 (not (type= other-type leaf-type))))
363 (change-ref-leaf ref other)
364 (when (constant-p other) (return)))))))
366 (cond ((and (integer-type-p res) (integer-type-p y))
367 (let ((greater (eq kind '>)))
368 (let ((greater (if not-p (not greater) greater)))
370 (constrain-integer-type res y greater not-p)))))
371 ((and (float-type-p res) (float-type-p y))
372 (let ((greater (eq kind '>)))
373 (let ((greater (if not-p (not greater) greater)))
375 (constrain-float-type res y greater not-p)))))
378 (let* ((cont (node-cont ref))
379 (dest (continuation-dest cont)))
380 (cond ((and (if-p dest)
381 (csubtypep (specifier-type 'null) not-res))
382 (setf (node-derived-type ref) *wild-type*)
383 (change-ref-leaf ref (find-constant t)))
385 (derive-node-type ref
386 (make-single-value-type
387 (or (type-difference res not-res)
394 ;;; Local propagation
395 ;;; -- [TODO: For any LAMBDA-VAR ref with a type check, add that
397 ;;; -- For any LAMBDA-VAR set, delete all constraints on that var; add
398 ;;; a type constraint based on the new value type.
399 (declaim (ftype (function (cblock sset
400 &key (:ref-preprocessor function)
401 (:set-preprocessor function))
403 constraint-propagate-in-block))
404 (defun constraint-propagate-in-block
405 (block gen &key ref-preprocessor set-preprocessor)
407 (let ((test (block-test-constraint block)))
409 (sset-union gen test)))
411 (do-nodes (node cont block)
414 (let ((fun (bind-lambda node)))
415 (when (eq (functional-kind fun) :let)
416 (loop with call = (continuation-dest
417 (node-cont (first (lambda-refs fun))))
418 for var in (lambda-vars fun)
419 and val in (combination-args call)
421 (lambda-var-constraints var)
422 ;; if VAR has no SETs, type inference is
423 ;; fully performed by IR1 optimizer
424 (lambda-var-sets var))
425 do (let* ((type (continuation-type val))
426 (con (find-constraint 'typep var type nil)))
427 (sset-adjoin con gen))))))
429 (let ((var (ok-ref-lambda-var node)))
431 (when ref-preprocessor
432 (funcall ref-preprocessor node gen))
433 (let ((dest (continuation-dest cont)))
435 (let* ((atype (single-value-type (cast-derived-type dest))) ; FIXME
436 (con (find-constraint 'typep var atype nil)))
437 (sset-adjoin con gen)))))))
439 (let ((var (set-var node)))
440 (when (lambda-var-p var)
441 (when set-preprocessor
442 (funcall set-preprocessor var))
443 (let ((cons (lambda-var-constraints var)))
445 (sset-difference gen cons)
446 (let* ((type (single-value-type (node-derived-type node)))
447 (con (find-constraint 'typep var type nil)))
448 (sset-adjoin con gen)))))))))
452 ;;; BLOCK-KILL is just a list of the LAMBDA-VARs killed, so we must
453 ;;; compute the kill set when there are any vars killed. We bum this a
454 ;;; bit by special-casing when only one var is killed, and just using
455 ;;; that var's constraints as the kill set. This set could possibly be
456 ;;; precomputed, but it would have to be invalidated whenever any
457 ;;; constraint is added, which would be a pain.
458 (defun compute-block-out (block)
459 (declare (type cblock block))
460 (let ((in (block-in block))
461 (kill (block-kill block))
462 (out (copy-sset (block-gen block))))
466 (let ((con (lambda-var-constraints (first kill))))
468 (sset-union-of-difference out in con)
469 (sset-union out in))))
471 (let ((kill-set (make-sset)))
473 (let ((con (lambda-var-constraints var)))
475 (sset-union kill-set con))))
476 (sset-union-of-difference out in kill-set))))
479 ;;; Compute the initial flow analysis sets for BLOCK:
480 ;;; -- Compute IN/OUT sets; if OUT of a predecessor is not yet
481 ;;; computed, assume it to be a universal set (this is only
482 ;;; possible in a loop)
484 ;;; Return T if we have found a loop.
485 (defun find-block-type-constraints (block)
486 (declare (type cblock block))
487 (collect ((kill nil adjoin))
488 (let ((gen (constraint-propagate-in-block
490 :set-preprocessor (lambda (var)
492 (setf (block-gen block) gen)
493 (setf (block-kill block) (kill))
494 (setf (block-type-asserted block) nil)
495 (let* ((n (block-number block))
496 (pred (block-pred block))
500 (cond ((> (block-number b) n)
502 (sset-intersection in (block-out b))
503 (setq in (copy-sset (block-out b)))))
504 (t (setq loop-p t))))
506 (bug "Unreachable code is found or flow graph is not ~
507 properly depth-first ordered."))
508 (setf (block-in block) in)
509 (setf (block-out block) (compute-block-out block))
512 ;;; BLOCK-IN becomes the intersection of the OUT of the predecessors.
514 ;;; gen U (in - kill)
516 ;;; Return True if we have done something.
517 (defun flow-propagate-constraints (block)
518 (let* ((pred (block-pred block))
519 (in (progn (aver pred)
520 (let ((res (copy-sset (block-out (first pred)))))
521 (dolist (b (rest pred))
522 (sset-intersection res (block-out b)))
524 (setf (block-in block) in)
525 (let ((out (compute-block-out block)))
526 (if (sset= out (block-out block))
528 (setf (block-out block) out)))))
530 ;;; Deliver the results of constraint propagation to REFs in BLOCK.
531 ;;; During this pass, we also do local constraint propagation by
532 ;;; adding in constraints as we seem them during the pass through the
534 (defun use-result-constraints (block)
535 (declare (type cblock block))
536 (constraint-propagate-in-block
537 block (block-in block)
538 :ref-preprocessor (lambda (node cons)
539 (let ((var (ref-leaf node)))
540 (when (lambda-var-p var)
541 (let ((con (lambda-var-constraints var)))
543 (constrain-ref-type node con cons))))))))
545 ;;; Give an empty constraints set to any var that doesn't have one and
546 ;;; isn't a set closure var. Since a var that we previously rejected
547 ;;; looks identical to one that is new, so we optimistically keep
548 ;;; hoping that vars stop being closed over or lose their sets.
549 (defun init-var-constraints (component)
550 (declare (type component component))
551 (dolist (fun (component-lambdas component))
553 (dolist (var (lambda-vars x))
554 (unless (lambda-var-constraints var)
555 (when (or (null (lambda-var-sets var))
556 (not (closure-var-p var)))
557 (setf (lambda-var-constraints var) (make-sset)))))))
559 (dolist (let (lambda-lets fun))
562 ;;; How many blocks does COMPONENT have?
563 (defun component-n-blocks (component)
565 (declare (type index result))
566 (do-blocks (block component :both)
570 (defun constraint-propagate (component &aux (loop-p nil))
571 (declare (type component component))
572 (init-var-constraints component)
574 (do-blocks (block component)
575 (when (block-test-modified block)
576 (find-test-constraints block)))
578 (unless (block-out (component-head component))
579 (setf (block-out (component-head component)) (make-sset)))
581 (do-blocks (block component)
582 (when (find-block-type-constraints block)
586 (let (;; If we have to propagate changes more than this many times,
587 ;; something is wrong.
588 (max-n-changes-remaining (component-n-blocks component)))
589 (declare (type fixnum max-n-changes-remaining))
590 (loop (aver (>= max-n-changes-remaining 0))
591 (decf max-n-changes-remaining)
592 (let ((did-something nil))
593 (do-blocks (block component)
594 (when (flow-propagate-constraints block)
595 (setq did-something t)))
596 (unless did-something
599 (do-blocks (block component)
600 (use-result-constraints block))