summaryrefslogtreecommitdiff
path: root/chapter33/src
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-12-04 14:16:29 -0800
committerCliff Click <cliffc@acm.org>2026-12-04 14:16:29 -0800
commit4c7e14994856e5d3218dd7d909d6a848be15753d (patch)
tree5d5b7944543177a83e08ed17bc1bc61cf111b9f5 /chapter33/src
parentfd21b1a62f0fb1a0efd503769b2e5f3a81747af0 (diff)
downloadSimple-4c7e14994856e5d3218dd7d909d6a848be15753d.tar.gz
Simple-4c7e14994856e5d3218dd7d909d6a848be15753d.zip
Extract the shared WorkList and accept dead-self peephole results
Move the deduplicating node worklist into util, including traversal marks. Skip replacement processing when a peephole has already killed its returned self. Add regressions for traversal/requeue behavior and a rewrite that subsumes itself. Validation: the dead-self regression fails at the old assertion; make -k -j8 tests passes with both regressions (821 executions).
Diffstat (limited to 'chapter33/src')
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java2
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/IterPeeps.java86
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/ListScheduler.java2
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java2
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/node/Node.java1
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/util/WorkList.java78
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java44
7 files changed, 128 insertions, 87 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java
index 83c0677d..d7837c43 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java
@@ -1,6 +1,6 @@
package com.seaofnodes.simple.codegen;
-import com.seaofnodes.simple.codegen.IterPeeps.WorkList;
+import com.seaofnodes.simple.util.WorkList;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.Ary;
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/IterPeeps.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/IterPeeps.java
index fe935a69..54ccef09 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/IterPeeps.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/IterPeeps.java
@@ -3,11 +3,10 @@ package com.seaofnodes.simple.codegen;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import com.seaofnodes.simple.util.Ary;
+import com.seaofnodes.simple.util.WorkList;
import com.seaofnodes.simple.node.*;
-import java.util.Arrays;
import java.util.BitSet;
import java.util.IdentityHashMap;
-import java.util.Random;
/**
* The IterPeeps runs after parsing. It iterates the peepholes to a fixed point
@@ -111,8 +110,7 @@ public class IterPeeps {
Node x = n.peepholeOpt();
if( n instanceof CallEndNode cend )
_workInline.push(cend);
- if( x != null ) {
- assert !x.isDead(); // Peepholes return alive answers
+ if( x != null && !x.isDead() ) {
// peepholeOpt can return brand-new nodes, needing an initial type set
if( x._type==null ) x.setType(x.compute());
// Changes require neighbors onto the worklist
@@ -275,84 +273,4 @@ public class IterPeeps {
return cfg==null ? "null" : cfg.getClass().getSimpleName()+"#"+cfg._nid+" "+cfg;
}
- /**
- * Classic WorkList, with a fast add/remove, dup removal, random pull.
- * The Node's nid is used to check membership in the worklist.
- */
- @SuppressWarnings("unchecked")
- public static class WorkList<E extends Node> {
-
- private Node[] _es;
- private int _len;
- private final BitSet _on; // Bit set if Node._nid is on WorkList
- private final Random _R; // For randomizing pull from the WorkList
- private final long _seed;
-
- /* Useful stat - how many nodes are processed in the post parse iterative opt */
- private long _totalWork = 0;
-
- public WorkList() { this(123); }
- WorkList(long seed) {
- _es = new Node[1];
- _len=0;
- _on = new BitSet();
- _seed = seed;
- _R = new Random();
- _R.setSeed(_seed);
- }
-
- /**
- * Pushes a Node on the WorkList, ensuring no duplicates
- * If Node is null it will not be added.
- */
- public E push( E x ) {
- if( x==null ) return null;
- int idx = x._nid;
- if( !_on.get(idx) ) {
- _on.set(idx);
- if( _len==_es.length )
- _es = Arrays.copyOf(_es,_len<<1);
- _es[_len++] = x;
- _totalWork++;
- }
- return x;
- }
-
- public void addAll( Ary<E> ary ) {
- for( E n : ary )
- push(n);
- }
- public void addAll( E[] es ) {
- for( E n : es )
- push(n);
- }
-
-
- /**
- * True if Node is on the WorkList
- */
- public boolean on( E x ) { return _on.get(x._nid); }
-
- /**
- * Removes a random Node from the WorkList; null if WorkList is empty
- */
- public E pop() {
- if( _len == 0 ) return null;
- int idx = _R.nextInt(_len);
- E x = (E)_es[idx];
- _es[idx] = _es[--_len]; // Compress array
- _es[_len] = null;
- _on.clear(x._nid);
- return x;
- }
-
- public boolean isEmpty() { return _len==0; }
- public void clear() {
- Arrays.fill(_es,0,_len,null);
- _len = 0;
- _on.clear();
- _R.setSeed(_seed);
- _totalWork = 0;
- }
- }
}
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/ListScheduler.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/ListScheduler.java
index f90d8d4e..cdd09c1a 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/ListScheduler.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/ListScheduler.java
@@ -1,6 +1,6 @@
package com.seaofnodes.simple.codegen;
-import com.seaofnodes.simple.codegen.IterPeeps.WorkList;
+import com.seaofnodes.simple.util.WorkList;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.Ary;
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java
index 6d653835..4d6fb84d 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java
@@ -2,7 +2,7 @@ package com.seaofnodes.simple.codegen;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.parser.Parser;
-import com.seaofnodes.simple.codegen.IterPeeps.WorkList;
+import com.seaofnodes.simple.util.WorkList;
import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.Ary;
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter33/src/main/java/com/seaofnodes/simple/node/Node.java
index ad4f47e0..e52b7762 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -550,6 +550,7 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
* <ul>
* <li>If NO change is made, return {@code null}
* <li>If ANY change is made, return not-null; this can be {@code this}
+ * <li>A rewrite which replaces its own uses may return a dead {@code this} to report progress.
* <li>The returned Node does NOT call {@link #peephole} on itself; the {@link #peephole} call will recursively peephole it.
* <li>Any NEW nodes that are not directly returned DO call {@link #peephole}.
* </ul>
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/util/WorkList.java b/chapter33/src/main/java/com/seaofnodes/simple/util/WorkList.java
new file mode 100644
index 00000000..a4330df7
--- /dev/null
+++ b/chapter33/src/main/java/com/seaofnodes/simple/util/WorkList.java
@@ -0,0 +1,78 @@
+package com.seaofnodes.simple.util;
+
+import com.seaofnodes.simple.node.Node;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Random;
+
+/** Deduplicated node worklist with seeded random removal. Membership uses
+ * Node IDs, never Node.hashCode (which participates in GVN).
+ * Use pop() for iterative solvers and popOnce() for graph traversals. */
+public class WorkList<E extends Node> {
+ private Node[] _es;
+ private int _len;
+ private final BitSet _on; // Queued nodes, plus visited nodes with popOnce().
+ private final Random _R;
+ private final long _seed;
+ private long _totalWork = 0;
+
+ public WorkList() { this(123); }
+ public WorkList(long seed) {
+ _es = new Node[1];
+ _on = new BitSet();
+ _seed = seed;
+ _R = new Random(seed);
+ }
+
+ /** Add a node unless already marked. Null is ignored. */
+ public E push(E x) {
+ if( x==null ) return null;
+ int idx = x._nid;
+ if( !_on.get(idx) ) {
+ _on.set(idx);
+ if( _len==_es.length )
+ _es = Arrays.copyOf(_es,_len<<1);
+ _es[_len++] = x;
+ _totalWork++;
+ }
+ return x;
+ }
+
+ public void addAll(Ary<E> ary) {
+ for( E n : ary ) push(n);
+ }
+ public void addAll(E[] es) {
+ for( E n : es ) push(n);
+ }
+
+ /** Queued membership; popOnce() also retains already-visited membership. */
+ public boolean on(E x) { return _on.get(x._nid); }
+
+ /** Remove a random node and allow it to be queued again. Null when empty. */
+ public E pop() {
+ E x = popOnce();
+ if( x!=null ) _on.clear(x._nid);
+ return x;
+ }
+
+ /** Remove a random node, retaining its mark to prevent revisits in cyclic
+ * graph walks. clear() starts a fresh traversal. Null when empty. */
+ @SuppressWarnings("unchecked")
+ public E popOnce() {
+ if( _len==0 ) return null;
+ int idx = _R.nextInt(_len);
+ E x = (E)_es[idx];
+ _es[idx] = _es[--_len];
+ _es[_len] = null;
+ return x;
+ }
+
+ public boolean isEmpty() { return _len==0; }
+ public void clear() {
+ Arrays.fill(_es,0,_len,null);
+ _len = 0;
+ _on.clear();
+ _R.setSeed(_seed);
+ _totalWork = 0;
+ }
+}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
new file mode 100644
index 00000000..99dc4452
--- /dev/null
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.util.WorkList;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/** Regressions for the independent compiler fixes introduced in this chapter. */
+public class Chapter33Test {
+ @Test public void testWorkListRequeueAndTraversal() {
+ CodeGen code = new CodeGen("return arg;").parse();
+ Node arg = code.expr();
+ WorkList<Node> work = new WorkList<>();
+ work.push(null);
+ work.push(arg);
+ work.push(arg);
+ assertSame(arg,work.pop());
+ assertNull(work.pop());
+ work.push(arg);
+ assertSame(arg,work.popOnce());
+ work.push(arg);
+ assertNull("A traversal must not revisit a cycle",work.popOnce());
+ work.clear();
+ work.push(arg);
+ assertSame(arg,work.popOnce());
+ }
+
+ @Test public void testPeepholeMayReturnDeadSelf() {
+ CodeGen code = new CodeGen("return arg;").parse();
+ Node replacement = code.expr();
+ // A rewrite may replace its own uses and thereby kill itself before
+ // returning self to report progress to the iterative peephole driver.
+ Node rewritten = new AddNode(replacement,code.con(1)) {
+ @Override public Node idealize() {
+ subsume(in(1));
+ return this;
+ }
+ }.init();
+ code.driver(CodeGen.Phase.TypeCheck);
+ assertTrue(rewritten.isDead());
+ assertEquals("42",Eval2.eval(code,42));
+ }
+}