summaryrefslogtreecommitdiff
path: root/chapter04
diff options
context:
space:
mode:
Diffstat (limited to 'chapter04')
-rw-r--r--chapter04/README.md4
-rw-r--r--chapter04/src/main/java/com/seaofnodes/simple/node/SubNode.java19
-rw-r--r--chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java28
3 files changed, 51 insertions, 0 deletions
diff --git a/chapter04/README.md b/chapter04/README.md
index 2c7f7e9c..07085e80 100644
--- a/chapter04/README.md
+++ b/chapter04/README.md
@@ -305,6 +305,7 @@ Here is a list of peepholes introduced in this Chapter, more will be introduced
| Before | After | Description |
|-----------------------|-----------------------|------------------------------------------------|
| (arg + 0 ) | arg | Add of zero identity |
+| (arg - 0 ) | arg | Sub of zero identity |
| (arg * 1 ) | arg | Multiple of one identity |
| (con + arg) | (arg + con) | Move constants to right, to encourage folding |
| (con * arg) | (arg * con) | Move constants to right, to encourage folding |
@@ -312,6 +313,9 @@ Here is a list of peepholes introduced in this Chapter, more will be introduced
| ((arg1 + con) + arg2) | ((arg1 + arg2) + con) | Move constants to right, to encourage folding |
| (arg + arg) | (arg * 2) | Sum-of-products form |
| (arg - arg) | 0 | Sub of same is 0
+| (0 - arg) | (-arg) | Sub from 0 is negation
+| (arg1 - (-arg2)) | (arg1 + arg2) | Sub of negation is add
+| ((-arg1) - arg2) | -(arg1 + arg2) | Sub from negation is negation of sum
| (con / 1) | con | Division by one
| (arg == arg) | 1 | Compare of same
| (arg != arg) | 0 | Compare of same
diff --git a/chapter04/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter04/src/main/java/com/seaofnodes/simple/node/SubNode.java
index f5ad663d..c831a798 100644
--- a/chapter04/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter04/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -32,6 +32,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/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 28912d5d..74682310 100644
--- a/chapter04/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter04/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;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return arg;", ret.print());
+ }
+
+ @Test
+ public void test0Sub() {
+ Parser parser = new Parser("return 0-arg;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return (-arg);", ret.print());
+ }
+
+ @Test
+ public void testSubNeg() {
+ Parser parser = new Parser("return 3--arg;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return (arg+3);", ret.print());
+ }
+
+ @Test
+ public void testNegSub() {
+ Parser parser = new Parser("return -arg-3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return (-(arg+3));", ret.print());
+ }
+
+ @Test
public void testVarArg() {
Parser parser = new Parser("return arg; ", TypeInteger.BOT);
ReturnNode ret = parser.parse();