summaryrefslogtreecommitdiff
path: root/chapter09/src
diff options
context:
space:
mode:
authorHsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com>2026-09-26 06:35:22 +0800
committerGitHub <noreply@github.com>2026-09-25 15:35:22 -0700
commitdd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7 (patch)
tree8b71264016c53914912e1cd30527b1bf86ed9bf3 /chapter09/src
parente922879e9dfa402934e2b5860900661b1169492c (diff)
downloadSimple-dd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7.tar.gz
Simple-dd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7.zip
Extend and backport SubNode peephole (#254)
* Extend and backport SubNode peephole * Add tests * Restrict subtraction zero peepholes to integer operands Require resolved integer mode in chapter 25 so zero subtraction preserves floating-point signed zero. Keep nonnumeric operands in the graph for subtraction diagnostics. Add signed-zero coverage from chapter 12 and null-subtraction diagnostics from chapter 22. Confirmed the regressions fail on the original PR. Full Make suites pass in chapters 4-25 (5462 tests), including fresh chapter 25 sys.o compilation. --------- Co-authored-by: Cliff Click <cliffc@acm.org>
Diffstat (limited to 'chapter09/src')
-rw-r--r--chapter09/src/main/java/com/seaofnodes/simple/node/SubNode.java19
-rw-r--r--chapter09/src/test/java/com/seaofnodes/simple/Chapter04Test.java28
2 files changed, 47 insertions, 0 deletions
diff --git a/chapter09/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter09/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 59cd31f0..eeec5e1d 100644
--- a/chapter09/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter09/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -37,6 +37,25 @@ public class SubNode extends Node {
@Override
public Node idealize() {
+ Node lhs = in(1);
+ Node rhs = in(2);
+ Type t1 = lhs._type;
+ Type t2 = rhs._type;
+
+ // Keep invalid operands for type checking.
+ if( t1 instanceof TypeInteger x && t2 instanceof TypeInteger y ) {
+ if( y.isConstant() && y.value()==0 ) return lhs;
+ if( x.isConstant() && x.value()==0 ) return new MinusNode(rhs);
+ }
+
+ // x - (-y) is x+y
+ if( rhs instanceof MinusNode minus )
+ return new AddNode(lhs,minus.in(1));
+
+ // (-x) - y is -(x+y)
+ if( lhs instanceof MinusNode minus )
+ return new MinusNode(new AddNode(minus.in(1),rhs).peephole());
+
return null;
}
diff --git a/chapter09/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter09/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 2e2abb25..f1603cc4 100644
--- a/chapter09/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter09/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -51,6 +51,34 @@ public class Chapter04Test {
}
@Test
+ public void testSub0() {
+ Parser parser = new Parser("return arg-0;");
+ StopNode ret = parser.parse();
+ assertEquals("return arg;", ret.print());
+ }
+
+ @Test
+ public void test0Sub() {
+ Parser parser = new Parser("return 0-arg;");
+ StopNode ret = parser.parse();
+ assertEquals("return (-arg);", ret.print());
+ }
+
+ @Test
+ public void testSubNeg() {
+ Parser parser = new Parser("return 3--arg;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg+3);", ret.print());
+ }
+
+ @Test
+ public void testNegSub() {
+ Parser parser = new Parser("return -arg-3;");
+ StopNode ret = parser.parse();
+ assertEquals("return (-(arg+3));", ret.print());
+ }
+
+ @Test
public void testVarArg() {
Parser parser = new Parser("return arg; ");
StopNode stop = parser.parse();