diff options
| author | fuhsnn <66062782+fuhsnn@users.noreply.github.com> | 2026-09-25 07:25:49 +0800 |
|---|---|---|
| committer | fuhsnn <66062782+fuhsnn@users.noreply.github.com> | 2026-09-25 11:27:48 +0800 |
| commit | 349c23d0527569284a8bec67f27130c4358e056c (patch) | |
| tree | b404934d6562db8e1e7c8abdf8d30cd496abea84 /chapter17 | |
| parent | e9e02b18ee3223f2cfe1990be00674f9868e1a26 (diff) | |
| download | Simple-349c23d0527569284a8bec67f27130c4358e056c.tar.gz Simple-349c23d0527569284a8bec67f27130c4358e056c.zip | |
Extend and backport SubNode peephole
Diffstat (limited to 'chapter17')
| -rw-r--r-- | chapter17/src/main/java/com/seaofnodes/simple/node/SubNode.java | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter17/src/main/java/com/seaofnodes/simple/node/SubNode.java index 24499cbc..044d0c62 100644 --- a/chapter17/src/main/java/com/seaofnodes/simple/node/SubNode.java +++ b/chapter17/src/main/java/com/seaofnodes/simple/node/SubNode.java @@ -42,13 +42,24 @@ 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; + + if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 ) + return lhs; + + if( t1.isConstant() && t1 instanceof TypeInteger i && i.value()==0 ) + return new MinusNode(rhs); + // x - (-y) is x+y - if( in(2) instanceof MinusNode minus ) - return new AddNode(in(1),minus.in(1)); + if( rhs instanceof MinusNode minus ) + return new AddNode(lhs,minus.in(1)); // (-x) - y is -(x+y) - if( in(1) instanceof MinusNode minus ) - return new MinusNode(new AddNode(minus.in(1),in(2)).peephole()); + if( lhs instanceof MinusNode minus ) + return new MinusNode(new AddNode(minus.in(1),rhs).peephole()); return null; } |
