summaryrefslogtreecommitdiff
path: root/chapter07
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-27 08:52:12 -0700
committerCliff Click <cliffc@acm.org>2026-09-27 08:52:12 -0700
commitc5ef4e52fd7d183861b33476db999fa04f777607 (patch)
treefae4a9fb155a2e60b4b4390cd0ad3d30851257c8 /chapter07
parent4391fcc03a214e6296e19ca42487de023ab12f6a (diff)
downloadSimple-c5ef4e52fd7d183861b33476db999fa04f777607.tar.gz
Simple-c5ef4e52fd7d183861b33476db999fa04f777607.zip
Remove -disablePeephole
Diffstat (limited to 'chapter07')
-rw-r--r--chapter07/README.md8
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/Node.java11
-rw-r--r--chapter07/src/test/java/com/seaofnodes/simple/Chapter02Test.java4
-rw-r--r--chapter07/src/test/java/com/seaofnodes/simple/Chapter03Test.java9
-rw-r--r--chapter07/src/test/java/com/seaofnodes/simple/Chapter07Test.java18
5 files changed, 9 insertions, 41 deletions
diff --git a/chapter07/README.md b/chapter07/README.md
index 29f08b5f..d701278a 100644
--- a/chapter07/README.md
+++ b/chapter07/README.md
@@ -77,15 +77,15 @@ In [Chapter 8](../chapter08) we will implement a "lazy phi" approach that create
## New Node Types
Our list of nodes remains the same as in [Chapter 5](../chapter05/README.md), however, we create a subtype of `Region` named `Loop` to better
-encapsulate some of the additional logic required. A key aspect of this is to temporarily disable peepholes of the `Region` and any phis
-created until we complete parsing the loop body. This is because our phis are not fully constructed until the loop end.
+encapsulate some of the additional logic required. Loop and Phi peepholes defer rewrites that need the back edge
+until we complete parsing the loop body. Other nodes continue to optimize normally.
## Detailed Steps
1. We start by creating a new subclass of `Region`, the `Loop`. The `Loop` gets two control inputs,
the first is the entry point, i.e. the current binding to `$ctrl`, and second (`null`) is a placeholder for the back edge that is
- set after loop is parsed. The absence of a back edge is used as an indicator to switch off peepholes of the region and
- associated phis.
+ set after the loop is parsed. A missing back edge marks the Loop and associated Phis as incomplete,
+ so their peepholes wait for the inputs needed by each rewrite.
```java
ctrl(new LoopNode(ctrl(),null).peephole());
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java
index 0cc9f208..921292cb 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -279,12 +279,6 @@ public abstract class Node {
// Graph-based optimizations
/**
- * We allow disabling peephole opt so that we can observe the
- * full graph, vs the optimized graph.
- */
- public static boolean _disablePeephole = false;
-
- /**
* Try to peephole at this node and return a better replacement Node if
* possible. We compute a {@link Type} and then check and replace:
* <ul>
@@ -313,9 +307,6 @@ public abstract class Node {
// Compute initial or improved Type
Type type = _type = compute();
- if (_disablePeephole)
- return null; // Peephole optimizations turned off
-
// Replace constant computations from non-constants with a constant node
if (!(this instanceof ConstantNode) && type.isConstant())
return deadCodeElim(new ConstantNode(type).peephole());
@@ -467,7 +458,7 @@ public abstract class Node {
* Used to allow repeating tests in the same JVM. This just resets the
* Node unique id generator, and is done as part of making a new Parser.
*/
- public static void reset() { UNIQUE_ID = 1; _disablePeephole=false; }
+ public static void reset() { UNIQUE_ID = 1; }
/**
* Debugging utility to find a Node by index
diff --git a/chapter07/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter07/src/test/java/com/seaofnodes/simple/Chapter02Test.java
index 47cee096..a6075b75 100644
--- a/chapter07/src/test/java/com/seaofnodes/simple/Chapter02Test.java
+++ b/chapter07/src/test/java/com/seaofnodes/simple/Chapter02Test.java
@@ -11,10 +11,8 @@ public class Chapter02Test {
@Test
public void testParseGrammar() {
Parser parser = new Parser("return 1+2*3+-5;");
- Node._disablePeephole = true; // disable peephole so we can observe full graph
StopNode ret = parser.parse();
- assertEquals("return (1+((2*3)+(-5)));", ret.print());
- Node._disablePeephole = false;
+ assertEquals("return 2;", ret.print());
}
@Test
diff --git a/chapter07/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter07/src/test/java/com/seaofnodes/simple/Chapter03Test.java
index 1e67cef5..12201ca0 100644
--- a/chapter07/src/test/java/com/seaofnodes/simple/Chapter03Test.java
+++ b/chapter07/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -30,15 +30,6 @@ public class Chapter03Test {
}
@Test
- public void testVarScopeNoPeephole() {
- Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
- Node._disablePeephole = true;
- StopNode ret = parser.parse();
- Node._disablePeephole = false;
- assertEquals("return (1+3);", ret.print());
- }
-
- @Test
public void testVarDist() {
Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1);");
StopNode ret = parser.parse();
diff --git a/chapter07/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/chapter07/src/test/java/com/seaofnodes/simple/Chapter07Test.java
index 189fcf66..fb8dbcca 100644
--- a/chapter07/src/test/java/com/seaofnodes/simple/Chapter07Test.java
+++ b/chapter07/src/test/java/com/seaofnodes/simple/Chapter07Test.java
@@ -17,11 +17,9 @@ public class Chapter07Test {
}
return arg;
""");
- Node._disablePeephole = true;
StopNode stop = parser.parse();
assertEquals("return Phi(Loop6,arg,(Phi_arg+1));", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
- Node._disablePeephole = false;
}
@Test
@@ -72,11 +70,9 @@ while(a < 10) {
}
return b;
""");
- Node._disablePeephole = true;
StopNode stop = parser.parse();
assertEquals("return Phi(Loop8,2,Phi(Region27,Phi_b,4));", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
- Node._disablePeephole = false;
}
@Test
@@ -110,11 +106,9 @@ while(a < 10) {
}
return a;
""");
- Node._disablePeephole = true;
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop7,1,((Phi_a+1)+2));", stop.toString());
+ assertEquals("return Phi(Loop7,1,(Phi_a+3));", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
- Node._disablePeephole = false;
}
@Test
@@ -141,11 +135,9 @@ int a = 1;
while(arg) a = 2;
return a;
""");
- Node._disablePeephole = true;
StopNode stop = parser.parse();
assertEquals("return Phi(Loop7,1,2);", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
- Node._disablePeephole = false;
}
@Test
@@ -172,11 +164,9 @@ while(a < 10) {
}
return a;
""");
- Node._disablePeephole = true;
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop7,1,((Phi_a+1)+2));", stop.toString());
+ assertEquals("return Phi(Loop7,1,(Phi_a+3));", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
- Node._disablePeephole = false;
}
@Test
@@ -207,11 +197,9 @@ while(a < 10) {
}
return a;
""");
- Node._disablePeephole = true;
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop8,1,((Phi_a+1)+2));", stop.toString());
+ assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
- Node._disablePeephole = false;
}
@Test