From 58354d27c7f3f2d8f75522fe67875a2d57fcae9b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 31 Jul 2023 07:10:27 -0700 Subject: compiler: bugfix: dangling rlinks after dead code elimination Discovered while experimenting with new optimizations. * stdlib/optimize.tl (basic-blocks :postinit): Pass t argument to new parameter of basic-blocks link-graph. (basic-blocks link-graph): New parameter indicating whether this is the first call; if false, we reset all the links. (basic-blocks elim-dead-code): This no longer has to reset the links before calling link-graph. But now calls link-graph one more time after the dead code removal so that no dead blocks appear in the graph. --- stdlib/optimize.tl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/stdlib/optimize.tl b/stdlib/optimize.tl index 3ca7f07c..7b8d6587 100644 --- a/stdlib/optimize.tl +++ b/stdlib/optimize.tl @@ -87,7 +87,7 @@ insns @1 label (car @1)) lparts)) (mapdo (do set [bb.hash @1.label] @1) bb.list)) - bb.(link-graph)) + bb.(link-graph t)) (:method num-blocks (bb) (len bb.list)) @@ -129,7 +129,13 @@ ,*cases)) ,list)) -(defmeth basic-blocks link-graph (bb) +(defmeth basic-blocks link-graph (bb : first-time) + (unless first-time + (each ((bl bb.list)) + (set bl.links nil + bl.next nil + bl.prev nil + bl.rlinks nil))) (each* ((bl bb.list) (nxbl (append (cdr bl) '(nil)))) (let* ((code bl.insns) @@ -600,11 +606,6 @@ (set bb.list (joinbl bb.list)))) (defmeth basic-blocks elim-dead-code (bb) - (each ((bl bb.list)) - (set bl.links nil - bl.next nil - bl.prev nil - bl.rlinks nil)) bb.(link-graph) (let* ((visited (hash :eq-based))) (labels ((visit (bl) @@ -633,7 +634,8 @@ (del [visited bl]))) (if rep (upd bb.list (keep-if visited)))))) - bb.(join-blocks)) + bb.(join-blocks) + bb.(link-graph)) (defmeth basic-blocks merge-jump-thunks (bb) (let* ((candidates (mappend [andf [chain .links len (op eql 1)] -- cgit v1.2.3