Add compiler optimization to get rid of IR-FETCHVARs when unneeded

It replaces the IR-FETCHVAR's result with the variable being
fetched. This only works when the only use is in an operation that
does not require a separate fetch be performed, such as those
implemented as CPU instructions.
This commit is contained in:
John Lorentzson 2025-07-01 13:25:42 +02:00
parent b7c4a37483
commit 7aff9db800

View file

@ -169,6 +169,31 @@ though I'm pretty sure it can't anyway.")
(loop :for (new . old) :in replacements (loop :for (new . old) :in replacements
:do (setf (inputs call) (substitute new old (inputs call))))))))) :do (setf (inputs call) (substitute new old (inputs call)))))))))
(defun optim-direct-variable-use (iblock)
"Removes unnecessary uses of IR-FETCHVAR.
Some operations do not require their operands to be copied before use. CPU
instructions, for example. This optimization pass goes through and removes
IR-FETCHVAR instructions that would serve no purpose in compiled code."
;; TODO: Add more instructions to the candidates
(let ((candidates-type '(or ir-test-equal ir-plus ir-minus))
(to-remove '()))
(do-instructions (inst iblock)
#+(or)
(when (equalp (name iblock) "else")
(break "~A ~A" inst
(if (typep inst 'ir-fetchvar)
(format nil "~A ~A ~A" (input inst) (output inst) (users (output inst)))
"")))
(when (typep inst 'ir-fetchvar)
(let ((result (output inst))
(src (input inst)))
(when (and (not (typep (output inst) 'ir-reusable))
(typep (user result) candidates-type))
(let ((user (user (output inst))))
(setf (inputs user) (substitute src result (inputs user)))
(push inst to-remove))))))
(mapc #'delete-instruction to-remove)))
(defun compute-lifetime-knowledge (start-iblock) (defun compute-lifetime-knowledge (start-iblock)
(do-iblocks (iblock start-iblock) (do-iblocks (iblock start-iblock)
(do-instructions (inst iblock) (do-instructions (inst iblock)