+(locally
+ ;; This is performance critical for the compiler, and benefits
+ ;; from the following declarations. Probably you'll want to
+ ;; disable these declarations when debugging consets.
+ (declare #-sb-xc-host (optimize (speed 3) (safety 0) (space 0)))
+ (declaim (inline %constraint-number))
+ (defun %constraint-number (constraint)
+ (sset-element-number constraint))
+ (defstruct (conset
+ (:constructor make-conset ())
+ (:copier %copy-conset))
+ (vector (make-array
+ ;; FIXME: make POWER-OF-TWO-CEILING available earlier?
+ (ash 1 (integer-length (1- (length *constraint-universe*))))
+ :element-type 'bit :initial-element 0)
+ :type simple-bit-vector)
+ ;; Bit-vectors win over lightweight hashes for copy, union,
+ ;; intersection, difference, but lose for iteration if you iterate
+ ;; over the whole vector. Tracking extrema helps a bit.
+ (min 0 :type fixnum)
+ (max 0 :type fixnum))
+
+ (defun conset-empty (conset)
+ (or (= (conset-min conset) (conset-max conset))
+ ;; TODO: I bet FIND on bit-vectors can be optimized, if it
+ ;; isn't.
+ (not (find 1 (conset-vector conset)
+ :start (conset-min conset)
+ ;; By inspection, supplying :END here breaks the
+ ;; build with a "full call to
+ ;; DATA-VECTOR-REF-WITH-OFFSET" in the
+ ;; cross-compiler. If that should change, add
+ ;; :end (conset-max conset)
+ ))))
+
+ (defun copy-conset (conset)
+ (let ((ret (%copy-conset conset)))
+ (setf (conset-vector ret) (copy-seq (conset-vector conset)))
+ ret))
+
+ (defun %conset-grow (conset new-size)
+ (declare (type index new-size))
+ (setf (conset-vector conset)
+ (replace (the simple-bit-vector
+ (make-array
+ (ash 1 (integer-length (1- new-size)))
+ :element-type 'bit
+ :initial-element 0))
+ (the simple-bit-vector
+ (conset-vector conset)))))
+
+ (declaim (inline conset-grow))
+ (defun conset-grow (conset new-size)
+ (declare (type index new-size))
+ (when (< (length (conset-vector conset)) new-size)
+ (%conset-grow conset new-size))
+ (values))
+
+ (defun conset-member (constraint conset)
+ (let ((number (%constraint-number constraint))
+ (vector (conset-vector conset)))
+ (when (< number (length vector))
+ (plusp (sbit vector number)))))
+
+ (defun conset-adjoin (constraint conset)
+ (let ((number (%constraint-number constraint)))
+ (conset-grow conset (1+ number))
+ (setf (sbit (conset-vector conset) number) 1)
+ (setf (conset-min conset) (min number (conset-min conset)))
+ (when (>= number (conset-max conset))
+ (setf (conset-max conset) (1+ number))))
+ conset)
+
+ (defun conset= (conset1 conset2)
+ (let* ((vector1 (conset-vector conset1))
+ (vector2 (conset-vector conset2))
+ (length1 (length vector1))
+ (length2 (length vector2)))
+ (if (= length1 length2)
+ ;; When the lengths are the same, we can rely on EQUAL being
+ ;; nicely optimized on bit-vectors.
+ (equal vector1 vector2)
+ (multiple-value-bind (shorter longer)
+ (if (< length1 length2)
+ (values vector1 vector2)
+ (values vector2 vector1))
+ ;; FIXME: make MISMATCH fast on bit-vectors.
+ (dotimes (index (length shorter))
+ (when (/= (sbit vector1 index) (sbit vector2 index))
+ (return-from conset= nil)))
+ (if (find 1 longer :start (length shorter))
+ nil
+ t)))))
+
+ (macrolet
+ ((defconsetop (name bit-op)
+ `(defun ,name (conset-1 conset-2)
+ (declare (optimize (speed 3) (safety 0)))
+ (let* ((size-1 (length (conset-vector conset-1)))
+ (size-2 (length (conset-vector conset-2)))
+ (new-size (max size-1 size-2)))
+ (conset-grow conset-1 new-size)
+ (conset-grow conset-2 new-size))
+ (let ((vector1 (conset-vector conset-1))
+ (vector2 (conset-vector conset-2)))
+ (declare (simple-bit-vector vector1 vector2))
+ (setf (conset-vector conset-1) (,bit-op vector1 vector2 t))
+ ;; Update the extrema.
+ ,(ecase name
+ ((conset-union)
+ `(setf (conset-min conset-1)
+ (min (conset-min conset-1)
+ (conset-min conset-2))
+ (conset-max conset-1)
+ (max (conset-max conset-1)
+ (conset-max conset-2))))
+ ((conset-intersection)
+ `(let ((start (max (conset-min conset-1)
+ (conset-min conset-2)))
+ (end (min (conset-max conset-1)
+ (conset-max conset-2))))
+ (setf (conset-min conset-1)
+ (if (> start end)
+ 0
+ (or (position 1 (conset-vector conset-1)
+ :start start :end end)
+ 0))
+ (conset-max conset-1)
+ (if (> start end)
+ 0
+ (let ((position
+ (position
+ 1 (conset-vector conset-1)
+ :start start :end end :from-end t)))
+ (if position
+ (1+ position)
+ 0))))))
+ ((conset-difference)
+ `(setf (conset-min conset-1)
+ (or (position 1 (conset-vector conset-1)
+ :start (conset-min conset-1)
+ :end (conset-max conset-1))
+ 0)
+ (conset-max conset-1)
+ (let ((position
+ (position
+ 1 (conset-vector conset-1)
+ :start (conset-min conset-1)
+ :end (conset-max conset-1)
+ :from-end t)))
+ (if position
+ (1+ position)
+ 0))))))
+ (values))))
+ (defconsetop conset-union bit-ior)
+ (defconsetop conset-intersection bit-and)
+ (defconsetop conset-difference bit-andc2)))
+\f
+;;; 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.