X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcompiler%2Fconstraint.lisp;h=27f2feb087eb35f9d0f0c597eccea793d797f4d2;hb=8f31e3b32926c61b13240c447637d4bb9af10cdc;hp=de2beecd3c7f33f75fb9f5e4e3bde480cda0be52;hpb=da5a7ccd58c2bf3c5287a11fb41e01403e5745e8;p=sbcl.git diff --git a/src/compiler/constraint.lisp b/src/compiler/constraint.lisp index de2beec..27f2feb 100644 --- a/src/compiler/constraint.lisp +++ b/src/compiler/constraint.lisp @@ -158,7 +158,7 @@ #-sb-xc-host ignore #-sb-xc-host constraint-universe-end) (let* ((constraint-universe #+sb-xc-host '*constraint-universe* - #-sb-xc-host (gensym)) + #-sb-xc-host (sb!xc:gensym "UNIVERSE")) (with-array-data #+sb-xc-host '(progn) #-sb-xc-host `(with-array-data @@ -321,31 +321,79 @@ (defconsetop conset-intersection bit-and) (defconsetop conset-difference bit-andc2))) +;;; Constraints are hash-consed. Unfortunately, types aren't, so we have +;;; to over-approximate and then linear search through the potential hits. +;;; LVARs can only be found in EQL (not-p = NIL) constraints, while constant +;;; and lambda-vars can only be found in EQL constraints. + (defun find-constraint (kind x y not-p) (declare (type lambda-var x) (type constraint-y y) (type boolean not-p)) (etypecase y (ctype - (do-conset-elements (con (lambda-var-constraints x) nil) - (when (and (eq (constraint-kind con) kind) - (eq (constraint-not-p con) not-p) - (type= (constraint-y con) y)) - (return con)))) - ((or lvar constant) - (do-conset-elements (con (lambda-var-constraints x) nil) - (when (and (eq (constraint-kind con) kind) - (eq (constraint-not-p con) not-p) - (eq (constraint-y con) y)) - (return con)))) - (lambda-var - (do-conset-elements (con (lambda-var-constraints x) nil) - (when (and (eq (constraint-kind con) kind) - (eq (constraint-not-p con) not-p) - (let ((cx (constraint-x con))) - (eq (if (eq cx x) - (constraint-y con) - cx) - y))) - (return con)))))) + (awhen (lambda-var-ctype-constraints x) + (dolist (con (gethash (sb!kernel::type-class-info y) it) nil) + (when (and (eq (constraint-kind con) kind) + (eq (constraint-not-p con) not-p) + (type= (constraint-y con) y)) + (return-from find-constraint con))) + nil)) + (lvar + (awhen (lambda-var-eq-constraints x) + (gethash y it))) + ((or constant lambda-var) + (awhen (lambda-var-eq-constraints x) + (let ((cache (gethash y it))) + (declare (type list cache)) + (if not-p (cdr cache) (car cache))))))) + +;;; The most common operations on consets are iterating through the constraints +;;; that are related to a certain variable in a given conset. Storing the +;;; constraints related to each variable in vectors allows us to easily iterate +;;; through the intersection of such constraints and the constraints in a conset. +;;; +;;; EQL-var constraints assert that two lambda-vars are EQL. +;;; Private constraints assert that a lambda-var is EQL or not EQL to a constant. +;;; Inheritable constraints are constraints that may be propagated to EQL +;;; lambda-vars (along with EQL-var constraints). +;;; +;;; Lambda-var -- lvar EQL constraints only serve one purpose: remember whether +;;; an lvar is (only) written to by a ref to that lambda-var, and aren't ever +;;; propagated. + +(defun register-constraint (x con y) + (declare (type lambda-var x) (type constraint con) (type constraint-y y)) + (conset-adjoin con (lambda-var-constraints x)) + (macrolet ((ensuref (place default) + `(or ,place (setf ,place ,default))) + (ensure-hash (place) + `(ensuref ,place (make-hash-table))) + (ensure-vec (place) + `(ensuref ,place (make-array 8 :adjustable t :fill-pointer 0)))) + (etypecase y + (ctype + (let ((index (ensure-hash (lambda-var-ctype-constraints x))) + (vec (ensure-vec (lambda-var-inheritable-constraints x)))) + (push con (gethash (sb!kernel::type-class-info y) index)) + (vector-push-extend con vec))) + (lvar + (let ((index (ensure-hash (lambda-var-eq-constraints x)))) + (setf (gethash y index) con))) + ((or constant lambda-var) + (let* ((index (ensure-hash (lambda-var-eq-constraints x))) + (cons (ensuref (gethash y index) (list nil)))) + (if (constraint-not-p con) + (setf (cdr cons) con) + (setf (car cons) con))) + (typecase y + (constant + (let ((vec (ensure-vec (lambda-var-private-constraints x)))) + (vector-push-extend con vec))) + (lambda-var + (let ((vec (if (constraint-not-p con) + (ensure-vec (lambda-var-inheritable-constraints x)) + (ensure-vec (lambda-var-eql-var-constraints x))))) + (vector-push-extend con vec))))))) + nil) ;;; Return a constraint for the specified arguments. We only create a ;;; new constraint if there isn't already an equivalent old one, @@ -357,10 +405,10 @@ (let ((new (make-constraint (length *constraint-universe*) kind x y not-p))) (vector-push-extend new *constraint-universe* - (* 2 (length *constraint-universe*))) - (conset-adjoin new (lambda-var-constraints x)) + (1+ (length *constraint-universe*))) + (register-constraint x new y) (when (lambda-var-p y) - (conset-adjoin new (lambda-var-constraints y))) + (register-constraint y new x)) new))) ;;; If REF is to a LAMBDA-VAR with CONSTRAINTs (i.e. we can do flow @@ -387,26 +435,70 @@ ((cast-p use) (ok-lvar-lambda-var (cast-value use) constraints))))) +(defmacro do-conset-constraints-intersection ((symbol (conset constraints) &optional result) + &body body) + (let ((min (gensym "MIN")) + (max (gensym "MAX"))) + (once-only ((conset conset) + (constraints constraints)) + `(flet ((body (,symbol) + (declare (type constraint ,symbol)) + ,@body)) + (when ,constraints + (let ((,min (conset-min ,conset)) + (,max (conset-max ,conset))) + (map nil (lambda (constraint) + (declare (type constraint constraint)) + (let ((number (constraint-number constraint))) + (when (and (<= ,min number) + (< number ,max) + (conset-member constraint ,conset)) + (body constraint)))) + ,constraints))) + ,result)))) + (defmacro do-eql-vars ((symbol (var constraints) &optional result) &body body) - (once-only ((var var)) - `(let ((,symbol ,var)) - (flet ((body-fun () + (once-only ((var var) + (constraints constraints)) + `(flet ((body-fun (,symbol) + ,@body)) + (body-fun ,var) + (do-conset-constraints-intersection + (con (,constraints (lambda-var-eql-var-constraints ,var)) ,result) + (let ((x (constraint-x con)) + (y (constraint-y con))) + (body-fun (if (eq ,var x) y x))))))) + +(defmacro do-inheritable-constraints ((symbol (conset variable) &optional result) + &body body) + (once-only ((conset conset) + (variable variable)) + `(block nil + (flet ((body-fun (,symbol) ,@body)) - (body-fun) - (do-conset-elements (con ,constraints ,result) - (let ((other (and (eq (constraint-kind con) 'eql) - (eq (constraint-not-p con) nil) - (cond ((eq ,var (constraint-x con)) - (constraint-y con)) - ((eq ,var (constraint-y con)) - (constraint-x con)) - (t - nil))))) - (when other - (setq ,symbol other) - (when (lambda-var-p ,symbol) - (body-fun))))))))) + (do-conset-constraints-intersection + (con (,conset (lambda-var-inheritable-constraints ,variable))) + (body-fun con)) + (do-conset-constraints-intersection + (con (,conset (lambda-var-eql-var-constraints ,variable)) ,result) + (body-fun con)))))) +(defmacro do-propagatable-constraints ((symbol (conset variable) &optional result) + &body body) + (once-only ((conset conset) + (variable variable)) + `(block nil + (flet ((body-fun (,symbol) + ,@body)) + (do-conset-constraints-intersection + (con (,conset (lambda-var-private-constraints ,variable))) + (body-fun con)) + (do-conset-constraints-intersection + (con (,conset (lambda-var-eql-var-constraints ,variable))) + (body-fun con)) + (do-conset-constraints-intersection + (con (,conset (lambda-var-inheritable-constraints ,variable)) ,result) + (body-fun con)))))) ;;;; Searching constraints ;;; Add the indicated test constraint to BLOCK. We don't add the @@ -468,7 +560,8 @@ (ok-lvar-lambda-var (first args) constraints) (if (ctype-p val) val - (specifier-type val)) + (let ((*compiler-error-context* use)) + (specifier-type val))) nil))))) ((eq eql) (let* ((arg1 (first args)) @@ -490,7 +583,11 @@ (var2 (add 'eql var1 var2 nil)) ((constant-lvar-p arg2) - (add 'eql var1 (ref-leaf (principal-lvar-use arg2)) + (add 'eql var1 + (let ((use (principal-lvar-use arg2))) + (if (ref-p use) + (ref-leaf use) + (find-constant (lvar-value arg2)))) nil)) (t (add-test-constraint 'typep var1 (lvar-type arg2) @@ -599,11 +696,29 @@ (modified-numeric-type x :low new-bound) (modified-numeric-type x :high new-bound))))) +;;; Return true if LEAF is "visible" from NODE. +(defun leaf-visible-from-node-p (leaf node) + (cond + ((lambda-var-p leaf) + ;; A LAMBDA-VAR is visible iif it is homed in a CLAMBDA that is an + ;; ancestor for NODE. + (let ((leaf-lambda (lambda-var-home leaf))) + (loop for lambda = (node-home-lambda node) + then (lambda-parent lambda) + while lambda + when (eq lambda leaf-lambda) + return t))) + ;; FIXME: Check on FUNCTIONALs (CLAMBDAs and OPTIONAL-DISPATCHes), + ;; not just LAMBDA-VARs. + (t + ;; Assume everything else is globally visible. + t))) + ;;; Given the set of CONSTRAINTS for a variable and the current set of ;;; restrictions from flow analysis IN, set the type for REF ;;; accordingly. -(defun constrain-ref-type (ref constraints in) - (declare (type ref ref) (type conset constraints in)) +(defun constrain-ref-type (ref in) + (declare (type ref ref) (type conset in)) ;; KLUDGE: The NOT-SET and NOT-FPZ here are so that we don't need to ;; cons up endless union types when propagating large number of EQL ;; constraints -- eg. from large CASE forms -- instead we just @@ -620,15 +735,13 @@ (not-fpz nil) (not-res *empty-type*) (leaf (ref-leaf ref))) + (declare (type lambda-var leaf)) (flet ((note-not (x) (if (fp-zero-p x) (push x not-fpz) (when (or constrain-symbols (null x) (not (symbolp x))) (add-to-xset x not-set))))) - ;; KLUDGE: the implementations of DO-CONSET-INTERSECTION will - ;; probably run faster when the smaller set comes first, so - ;; don't change the order here. - (do-conset-intersection (con constraints in) + (do-propagatable-constraints (con (in leaf)) (let* ((x (constraint-x con)) (y (constraint-y con)) (not-p (constraint-not-p con)) @@ -642,24 +755,25 @@ (setq not-res (type-union not-res other))) (setq res (type-approx-intersection2 res other)))) (eql - (unless (lvar-p other) - (let ((other-type (leaf-type other))) - (if not-p - (when (and (constant-p other) - (member-type-p other-type)) - (note-not (constant-value other))) - (let ((leaf-type (leaf-type leaf))) - (cond - ((or (constant-p other) - (and (leaf-refs other) ; protect from + (let ((other-type (leaf-type other))) + (if not-p + (when (and (constant-p other) + (member-type-p other-type)) + (note-not (constant-value other))) + (let ((leaf-type (leaf-type leaf))) + (cond + ((or (constant-p other) + (and (leaf-refs other) ; protect from ; deleted vars - (csubtypep other-type leaf-type) - (not (type= other-type leaf-type)))) - (change-ref-leaf ref other) - (when (constant-p other) (return))) - (t - (setq res (type-approx-intersection2 - res other-type))))))))) + (csubtypep other-type leaf-type) + (not (type= other-type leaf-type)) + ;; Don't change to a LEAF not visible here. + (leaf-visible-from-node-p other ref))) + (change-ref-leaf ref other) + (when (constant-p other) (return))) + (t + (setq res (type-approx-intersection2 + res other-type)))))))) ((< >) (cond ((and (integer-type-p res) (integer-type-p y)) @@ -699,20 +813,16 @@ ;;; Copy all CONSTRAINTS involving FROM-VAR - except the (EQL VAR ;;; LVAR) ones - to all of the variables in the VARS list. (defun inherit-constraints (vars from-var constraints target) - (do-conset-elements (con constraints) - ;; Constant substitution is controversial. - (unless (constant-p (constraint-y con)) + (do-inheritable-constraints (con (constraints from-var)) + (let ((eq-x (eq from-var (constraint-x con))) + (eq-y (eq from-var (constraint-y con)))) (dolist (var vars) - (let ((eq-x (eq from-var (constraint-x con))) - (eq-y (eq from-var (constraint-y con)))) - (when (or (and eq-x (not (lvar-p (constraint-y con)))) - eq-y) - (conset-adjoin (find-or-create-constraint - (constraint-kind con) - (if eq-x var (constraint-x con)) - (if eq-y var (constraint-y con)) - (constraint-not-p con)) - target))))))) + (conset-adjoin (find-or-create-constraint + (constraint-kind con) + (if eq-x var (constraint-x con)) + (if eq-y var (constraint-y con)) + (constraint-not-p con)) + target))))) ;; Add an (EQL LAMBDA-VAR LAMBDA-VAR) constraint on VAR1 and VAR2 and ;; inherit each other's constraints. @@ -756,34 +866,34 @@ for var in (lambda-vars fun) and val in (combination-args call) when (and val (lambda-var-constraints var)) - do (let* ((type (lvar-type val)) - (con (find-or-create-constraint 'typep var type - nil))) - (conset-adjoin con gen)) - (maybe-add-eql-var-var-constraint var val gen))))) + do (let ((type (lvar-type val))) + (unless (eq type *universal-type*) + (let ((con (find-or-create-constraint 'typep var type nil))) + (conset-adjoin con gen)))) + (maybe-add-eql-var-var-constraint var val gen))))) (ref (when (ok-ref-lambda-var node) (maybe-add-eql-var-lvar-constraint node gen) (when preprocess-refs-p - (let* ((var (ref-leaf node)) - (con (lambda-var-constraints var))) - (constrain-ref-type node con gen))))) + (constrain-ref-type node gen)))) (cast (let ((lvar (cast-value node))) (let ((var (ok-lvar-lambda-var lvar gen))) (when var (let ((atype (single-value-type (cast-derived-type node)))) ;FIXME - (do-eql-vars (var (var gen)) - (let ((con (find-or-create-constraint 'typep var atype nil))) - (conset-adjoin con gen)))))))) + (unless (eq atype *universal-type*) + (do-eql-vars (var (var gen)) + (let ((con (find-or-create-constraint 'typep var atype nil))) + (conset-adjoin con gen))))))))) (cset (binding* ((var (set-var node)) (nil (lambda-var-p var) :exit-if-null) (cons (lambda-var-constraints var) :exit-if-null)) (conset-difference gen cons) - (let* ((type (single-value-type (node-derived-type node))) - (con (find-or-create-constraint 'typep var type nil))) - (conset-adjoin con gen)) + (let ((type (single-value-type (node-derived-type node)))) + (unless (eq type *universal-type*) + (let ((con (find-or-create-constraint 'typep var type nil))) + (conset-adjoin con gen)))) (maybe-add-eql-var-var-constraint var (set-value node) gen))))) gen)