Skip to content

Commit 82ad91b

Browse files
committed
BIR: Define function for removing unused constants etc from modules
This allows the generator to be more flexible and put in constants that aren't actually used, as long as it does the cleanup. I'm doing that for the generation from Maclina bytecode.
1 parent 41fe5e4 commit 82ad91b

2 files changed

Lines changed: 39 additions & 26 deletions

File tree

BIR/graph-modifications.lisp

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@
6969
(defmethod add-definition ((datum variable) (definition instruction))
7070
(set:nadjoinf (writers datum) definition))
7171

72+
(defgeneric remove-if-unused (value module))
73+
(defmethod remove-if-unused ((value constant) module)
74+
(when (set:empty-set-p (readers value))
75+
(set:nremovef (constants module) value)
76+
(remhash (constant-value value) (constant-table module))))
77+
(defmethod remove-if-unused ((value function-cell) module)
78+
(when (set:empty-set-p (readers value))
79+
(set:nremovef (constants module) value)
80+
(remhash (function-name value) (function-cell-table module))))
81+
(defmethod remove-if-unused ((value variable-cell) module)
82+
(when (set:empty-set-p (readers value))
83+
(set:nremovef (constants module) value)
84+
(remhash (variable-name value) (variable-cell-table module))))
85+
(defmethod remove-if-unused ((ltv load-time-value) module)
86+
(when (set:empty-set-p (readers ltv))
87+
(set:nremovef (constants module) ltv)))
88+
(defmethod remove-if-unused ((value function) module)
89+
(when (and (null (enclose value))
90+
(set:empty-set-p (local-calls value)))
91+
(clean-up-function value)))
92+
7293
(defmethod (setf outputs) :before (new-outputs (inst instruction))
7394
(dolist (output (outputs inst))
7495
(remove-definition output inst))
@@ -144,6 +165,15 @@
144165
(unlink-instruction movant)
145166
(insert-instruction-after movant existing))
146167

168+
;;; Remove any constants etc. that are unused. This is important to do if
169+
;;; the process of generating the module can create constants that are never
170+
;;; given a constant-reference instruction (analogously ltv-reference, etc.),
171+
;;; the deletion of which would usually clean up unused constants.
172+
;;; Verification requires unused constants to be deleted.
173+
(defun remove-unused-values (module)
174+
(set:doset (constant (constants module))
175+
(remove-if-unused constant module)))
176+
147177
;;; Remove backpointers to an instruction, etc.
148178
(defgeneric clean-up-instruction (instruction)
149179
(:method-combination progn)
@@ -201,38 +231,23 @@
201231
(defmethod clean-up-instruction progn ((inst constant-reference))
202232
(let ((constant (first (inputs inst))))
203233
(set:nremovef (readers constant) inst)
204-
(when (set:empty-set-p (readers constant))
205-
(let ((module (module (function inst))))
206-
(set:nremovef (constants module) constant)
207-
(remhash (constant-value constant) (constant-table module))))))
234+
(remove-if-unused constant (module (function inst)))))
208235
(defmethod clean-up-instruction progn ((inst constant-fdefinition))
209236
(let ((constant (first (inputs inst))))
210237
(set:nremovef (readers constant) inst)
211-
(when (set:empty-set-p (readers constant))
212-
(let ((module (module (function inst))))
213-
(set:nremovef (constants module) constant)
214-
(remhash (function-name constant) (function-cell-table module))))))
238+
(remove-if-unused constant (module (function inst)))))
215239
(defmethod clean-up-instruction progn ((inst constant-symbol-value))
216240
(let ((constant (first (inputs inst))))
217241
(set:nremovef (readers constant) inst)
218-
(when (set:empty-set-p (readers constant))
219-
(let ((module (module (function inst))))
220-
(set:nremovef (constants module) constant)
221-
(remhash (variable-name constant) (variable-cell-table module))))))
242+
(remove-if-unused constant (module (function inst)))))
222243
(defmethod clean-up-instruction progn ((inst set-constant-symbol-value))
223244
(let ((constant (first (inputs inst))))
224245
(set:nremovef (readers constant) inst)
225-
(when (set:empty-set-p (readers constant))
226-
(let ((module (module (function inst))))
227-
(set:nremovef (constants module) constant)
228-
(remhash (variable-name constant) (variable-cell-table module))))))
246+
(remove-if-unused constant (module (function inst)))))
229247
(defmethod clean-up-instruction progn ((inst constant-bind))
230248
(let ((constant (first (inputs inst))))
231249
(set:nremovef (readers constant) inst)
232-
(when (set:empty-set-p (readers constant))
233-
(let ((module (module (function inst))))
234-
(set:nremovef (constants module) constant)
235-
(remhash (variable-name constant) (variable-cell-table module))))))
250+
(remove-if-unused constant (module (function inst)))))
236251
(defmethod clean-up-instruction progn ((inst load-time-value-reference))
237252
(let ((ltv (first (inputs inst))))
238253
(set:nremovef (readers ltv) inst)
@@ -241,15 +256,12 @@
241256
(defmethod clean-up-instruction progn ((inst enclose))
242257
(let ((code (code inst)))
243258
(setf (enclose code) nil)
244-
(when (set:empty-set-p (local-calls code))
245-
(clean-up-function code))))
259+
(remove-if-unused code (module code))))
246260
(defmethod clean-up-instruction progn ((inst abstract-local-call))
247261
(let* ((code (callee inst))
248262
(local-calls (local-calls code)))
249263
(set:nremovef local-calls inst)
250-
(when (and (null (enclose code))
251-
(set:empty-set-p local-calls))
252-
(clean-up-function code))))
264+
(remove-if-unused code (module code))))
253265
(defmethod clean-up-instruction progn ((inst unwind))
254266
(set:nremovef (entrances (destination inst)) (iblock inst))
255267
(set:nremovef (unwinds (come-from inst)) inst))

BIR/packages.lisp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
#:delete-instruction
6767
#:replace-uses #:replace-terminator
6868
#:split-block-after #:delete-iblock #:maybe-delete-iblock
69-
#:clean-up-iblock #:merge-successor-if-possible #:delete-iblock-if-empty)
69+
#:clean-up-iblock #:merge-successor-if-possible #:delete-iblock-if-empty
70+
#:remove-unused-values)
7071
(:export #:map-lambda-list)
7172
(:export #:verify)
7273
(:export #:unused-variable #:type-conflict)

0 commit comments

Comments
 (0)