Initial revision
[sbcl.git] / src / compiler / copyprop.lisp
1 ;;;; This file implements the copy propagation phase of the compiler,
2 ;;;; which uses global flow analysis to eliminate unnecessary copying
3 ;;;; of variables.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
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.
13
14 (in-package "SB!C")
15
16 (file-comment
17   "$Header$")
18
19 ;;; In copy propagation, we manipulate sets of TNs. We only consider TNs whose
20 ;;; sole write is by a MOVE VOP. This allows us to use a degenerate version of
21 ;;; reaching definitions: since each such TN has only one definition, the TN
22 ;;; can stand for the definition. We can get away with this simplification,
23 ;;; since the TNs that would be subject to copy propagation are nearly always
24 ;;; single-writer (mostly temps allocated to ensure evaluation order is
25 ;;; perserved). Only TNs written by MOVEs are interesting, since all we do
26 ;;; with this information is delete spurious MOVEs.
27 ;;;
28 ;;; There are additional semantic constraints on whether a TN can be considered
29 ;;; to be a copy. See TN-IS-A-COPY-OF.
30 ;;;
31 ;;; If a TN is in the IN set for a block, that TN is a copy of a TN which still
32 ;;; has the same value it had at the time the move was done. Any reference
33 ;;; to a TN in the IN set can be replaced with a reference to the TN moved
34 ;;; from. When we delete all reads of such a TN, we can delete the MOVE VOP.
35 ;;; IN is computed as the intersection of OUT for all the predecessor blocks.
36 ;;;
37 ;;; In this flow analysis scheme, the KILL set is the set of all interesting
38 ;;; TNs where the copied TN is modified by the block (in any way.)
39 ;;;
40 ;;; GEN is the set of all interesting TNs that are copied in the block (whose
41 ;;; write appears in the block.)
42 ;;;
43 ;;; OUT is (union (difference IN KILL) GEN)
44
45 ;;; If TN is subject to copy propagation, then return the TN it is a copy
46 ;;; of, otherwise NIL.
47 ;;;
48 ;;; We also only consider TNs where neither the TN nor the copied TN are wired
49 ;;; or restricted. If we extended the life of a wired or restricted TN,
50 ;;; register allocation might fail, and we can't substitute arbitrary things
51 ;;; for references to wired or restricted TNs, since the reader may be
52 ;;; expencting the argument to be in a particular place (as in a passing
53 ;;; location.)
54 ;;;
55 ;;; The TN must be a :NORMAL TN. Other TNs might have hidden references or be
56 ;;; otherwise bizarre.
57 ;;;
58 ;;; A TN is also inelegible if it has interned name, policy is such that we
59 ;;; would dump it in the debug vars, and speed is not 3.
60 ;;;
61 ;;; The SCs of the TN's primitive types is a subset of the SCs of the copied
62 ;;; TN. Moves between TNs of different primitive type SCs may need to be
63 ;;; changed into coercions, so we can't squeeze them out. The reason for
64 ;;; testing for subset of the SCs instead of the same primitive type is
65 ;;; that this test lets T be substituted for LIST, POSITIVE-FIXNUM for FIXNUM,
66 ;;; etc. Note that more SCs implies fewer possible values, or a subtype
67 ;;; relationship, since more SCs implies more possible representations.
68 (defun tn-is-copy-of (tn)
69   (declare (type tn tn))
70   (declare (inline subsetp))
71   (let ((writes (tn-writes tn)))
72     (and (eq (tn-kind tn) :normal)
73          (not (tn-sc tn))               ; Not wired or restricted.
74          (and writes (null (tn-ref-next writes)))
75          (let ((vop (tn-ref-vop writes)))
76            (and (eq (vop-info-name (vop-info vop)) 'move)
77                 (let ((arg-tn (tn-ref-tn (vop-args vop))))
78                   (and (or (not (tn-sc arg-tn))
79                            (eq (tn-kind arg-tn) :constant))
80                        (subsetp (primitive-type-scs
81                                  (tn-primitive-type tn))
82                                 (primitive-type-scs
83                                  (tn-primitive-type arg-tn)))
84                        (let ((leaf (tn-leaf tn)))
85                          (or (not leaf)
86                              (not (symbol-package (leaf-name leaf)))
87                              (policy (vop-node vop)
88                                      (or (= speed 3) (< debug 2)))))
89                        arg-tn)))))))
90
91 ;;; Init the sets in Block for copy propagation. To find Gen, we just look
92 ;;; for MOVE vops, and then see whether the result is a eligible copy TN. To
93 ;;; find Kill, we must look at all VOP results, seeing whether any of the
94 ;;; reads of the written TN are copies for eligible TNs.
95 (defun init-copy-sets (block)
96   (declare (type cblock block))
97   (let ((kill (make-sset))
98         (gen (make-sset)))
99     (do ((vop (ir2-block-start-vop (block-info block)) (vop-next vop)))
100         ((null vop))
101       (unless (and (eq (vop-info-name (vop-info vop)) 'move)
102                    (let ((y (tn-ref-tn (vop-results vop))))
103                      (when (tn-is-copy-of y)
104                        (sset-adjoin y gen)
105                        t)))
106         (do ((res (vop-results vop) (tn-ref-across res)))
107             ((null res))
108           (let ((res-tn (tn-ref-tn res)))
109             (do ((read (tn-reads res-tn) (tn-ref-next read)))
110                 ((null read))
111               (let ((read-vop (tn-ref-vop read)))
112                 (when (eq (vop-info-name (vop-info read-vop)) 'move)
113                   (let ((y (tn-ref-tn (vop-results read-vop))))
114                     (when (tn-is-copy-of y)
115                       (sset-delete y gen)
116                       (sset-adjoin y kill))))))))))
117
118     (setf (block-out block) (copy-sset gen))
119     (setf (block-kill block) kill)
120     (setf (block-gen block) gen))
121   (values))
122
123 ;;; Do the flow analysis step for copy propagation on Block. We rely on OUT
124 ;;; being initialized to GEN, and use SSET-UNION-OF-DIFFERENCE to incrementally
125 ;;; build the union in OUT, rather than replacing OUT each time.
126 (defun copy-flow-analysis (block)
127   (declare (type cblock block))
128   (let* ((pred (block-pred block))
129          (in (copy-sset (block-out (first pred)))))
130     (dolist (pred-block (rest pred))
131       (sset-intersection in (block-out pred-block)))
132     (setf (block-in block) in)
133     (sset-union-of-difference (block-out block) in (block-kill block))))
134
135 (defevent copy-deleted-move "Copy propagation deleted a move.")
136
137 ;;; Return true if Arg is a reference to a TN that we can copy propagate to.
138 ;;; In addition to dealing with copy chains (as discussed below), we also throw
139 ;;; out references that are arguments to a local call, since IR2tran introduces
140 ;;; tempes in that context to preserve parallel assignment semantics.
141 (defun ok-copy-ref (vop arg in original-copy-of)
142   (declare (type vop vop) (type tn arg) (type sset in)
143            (type hash-table original-copy-of))
144   (and (sset-member arg in)
145        (do ((original (gethash arg original-copy-of)
146                       (gethash original original-copy-of)))
147            ((not original) t)
148          (unless (sset-member original in)
149            (return nil)))
150        (let ((info (vop-info vop)))
151          (not (and (eq (vop-info-move-args info) :local-call)
152                    (>= (or (position-in #'tn-ref-across arg (vop-args vop)
153                                         :key #'tn-ref-tn)
154                            (error "Couldn't find REF?"))
155                        (length (template-arg-types info))))))))
156
157 ;;; Make use of the result of flow analysis to eliminate copies. We scan
158 ;;; the VOPs in block, propagating copies and keeping our IN set in sync.
159 ;;;
160 ;;; Original-Copy-Of is an EQ hash table that we use to keep track of
161 ;;; renamings when there are copy chains, i.e. copies of copies. When we see
162 ;;; copy of a copy, we enter the first copy in the table with the second copy
163 ;;; as a key. When we see a reference to a TN in a copy chain, we can only
164 ;;; substitute the first copied TN for the reference when all intervening
165 ;;; copies in the copy chain are also available. Otherwise, we just leave the
166 ;;; reference alone. It is possible that we might have been able to reference
167 ;;; one of the intermediate copies instead, but that copy might have already
168 ;;; been deleted, since we delete the move immediately when the references go
169 ;;; to zero.
170 ;;;
171 ;;; To understand why we always can to the substitution when the copy chain
172 ;;; recorded in the Original-Copy-Of table hits NIL, note that we make an entry
173 ;;; in the table iff we change the arg of a copy. If an entry is not in the
174 ;;; table, it must be that we hit a move which *originally* referenced our
175 ;;; Copy-Of TN. If all the intervening copies reach our reference, then
176 ;;; Copy-Of must reach the reference.
177 ;;;
178 ;;; Note that due to our restricting copies to single-writer TNs, it will
179 ;;; always be the case that when the first copy in a chain reaches the
180 ;;; reference, all intervening copies reach also reach the reference. We
181 ;;; don't exploit this, since we have to work backward from the last copy.
182 ;;;
183 ;;; In this discussion, we are really only playing with the tail of the true
184 ;;; copy chain for which all of the copies have already had PROPAGATE-COPIES
185 ;;; done on them. But, because we do this pass in DFO, it is virtually always
186 ;;; the case that we will process earlier copies before later ones. In
187 ;;; perverse cases (non-reducible flow graphs), we just miss some optimization
188 ;;; opportinities.
189 (defun propagate-copies (block original-copy-of)
190   (declare (type cblock block) (type hash-table original-copy-of))
191   (let ((in (block-in block)))
192     (do ((vop (ir2-block-start-vop (block-info block)) (vop-next vop)))
193         ((null vop))
194       (let ((this-copy (and (eq (vop-info-name (vop-info vop)) 'move)
195                             (let ((y (tn-ref-tn (vop-results vop))))
196                               (when (tn-is-copy-of y) y)))))
197         ;; Substitute copied TN for copy when we find a reference to a copy.
198         ;; If the copy is left with no reads, delete the move to the copy.
199         (do ((arg-ref (vop-args vop) (tn-ref-across arg-ref)))
200             ((null arg-ref))
201           (let* ((arg (tn-ref-tn arg-ref))
202                  (copy-of (tn-is-copy-of arg)))
203             (when (and copy-of (ok-copy-ref vop arg in original-copy-of))
204               (when this-copy
205                 (setf (gethash this-copy original-copy-of) arg))
206               (change-tn-ref-tn arg-ref copy-of)
207               (when (null (tn-reads arg))
208                 (event copy-deleted-move)
209                 (delete-vop (tn-ref-vop (tn-writes arg)))))))
210         ;; Kill any elements in IN that are copies of a TN we are clobbering.
211         (do ((res-ref (vop-results vop) (tn-ref-across res-ref)))
212             ((null res-ref))
213           (do-sset-elements (tn in)
214             (when (eq (tn-is-copy-of tn) (tn-ref-tn res-ref))
215               (sset-delete tn in))))
216         ;; If this VOP is a copy, add the copy TN to IN.
217         (when this-copy (sset-adjoin this-copy in)))))
218
219   (values))
220
221 ;;; Do copy propagation on Component by initializing the flow analysis sets,
222 ;;; doing flow analysis, and then propagating copies using the results.
223 (defun copy-propagate (component)
224   (setf (block-out (component-head component)) (make-sset))
225   (do-blocks (block component)
226     (init-copy-sets block))
227
228   (loop
229     (let ((did-something nil))
230       (do-blocks (block component)
231         (when (copy-flow-analysis block)
232           (setq did-something t)))
233       (unless did-something (return))))
234
235   (let ((original-copies (make-hash-table :test 'eq)))
236     (do-blocks (block component)
237       (propagate-copies block original-copies)))
238
239   (values))