diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-05 11:30:22 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-05 11:30:22 -0700 |
| commit | 3b8ad2977ef3911aed7c681037e4640dca59749b (patch) | |
| tree | a5abc5de804f060ec127e11409563cd85b375691 /chapter19/src/main/java/com | |
| parent | 9ec90998c7a968c18338c6ca4eddcb553f610169 (diff) | |
| download | Simple-3b8ad2977ef3911aed7c681037e4640dca59749b.tar.gz Simple-3b8ad2977ef3911aed7c681037e4640dca59749b.zip | |
Require matching types to use algebraic identies
Fixes some bugs `flit x; return x ^ 0;` should give an error instead of returning the plain `x`
Diffstat (limited to 'chapter19/src/main/java/com')
10 files changed, 10 insertions, 10 deletions
diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/AddNode.java index 3a39ce9b..2dfedbb5 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/AddNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/AddNode.java @@ -42,7 +42,7 @@ public class AddNode extends Node { // Add of 0. We do not check for (0+x) because this will already // canonicalize to (x+0) - if( t2 == TypeInteger.ZERO ) + if( t2 == TypeInteger.ZERO && lhs._type instanceof TypeInteger ) return lhs; // Add of same to a multiply by 2 diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/AndNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/AndNode.java index 8a85ec59..7cb16c71 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/AndNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/AndNode.java @@ -37,7 +37,7 @@ public class AndNode extends LogicalNode { // And of -1. We do not check for (-1&x) because this will already // canonicalize to (x&-1) - if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==-1 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==-1 && lhs._type instanceof TypeInteger ) return lhs; // Move constants to RHS: con*arg becomes arg*con diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/DivNode.java index f0ee21e1..70631c0a 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/DivNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/DivNode.java @@ -29,7 +29,7 @@ public class DivNode extends Node { @Override public Node idealize() { // Div of 1. - if( in(2)._type == TypeInteger.TRUE ) + if( in(2)._type == TypeInteger.TRUE && in(1)._type instanceof TypeInteger ) return in(1); return null; } diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/MinusNode.java index 20220915..66e26163 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/MinusNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/MinusNode.java @@ -25,7 +25,7 @@ public class MinusNode extends Node { @Override public Node idealize() { // -(-x) is x - if( in(1) instanceof MinusNode minus ) + if( in(1) instanceof MinusNode minus && addDep(minus.in(1))._type instanceof TypeInteger ) return minus.in(1); return null; diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/MulNode.java index 104a5581..720f15d7 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/MulNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/MulNode.java @@ -43,7 +43,7 @@ public class MulNode extends Node { // Mul of 1. We do not check for (1*x) because this will already // canonicalize to (x*1) long c = i.value(); - if( c==1 ) return lhs; + if( c==1 ) return lhs._type instanceof TypeInteger ? lhs : null; if( c==0 ) return Parser.ZERO; // Mul by a power of 2, +/-1. Bit patterns more complex than this // are unlikely to win on an X86 vs the normal "imul", and so diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/OrNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/OrNode.java index 5a87ce09..1cd8bafc 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/OrNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/OrNode.java @@ -34,7 +34,7 @@ public class OrNode extends LogicalNode { // Or of 0. We do not check for (0|x) because this will already // canonicalize to (x|0) - if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 && lhs._type instanceof TypeInteger ) return lhs; // Move constants to RHS: con*arg becomes arg*con diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/SarNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/SarNode.java index 47091fed..d321da42 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/SarNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/SarNode.java @@ -37,7 +37,7 @@ public class SarNode extends LogicalNode { Type t2 = rhs._type; // Sar of 0. - if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 && lhs._type instanceof TypeInteger ) return lhs; // TODO: x >> 3 >> (y ? 1 : 2) ==> x >> (y ? 4 : 5) diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/ShlNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/ShlNode.java index 2342e7f5..84c19247 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/ShlNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/ShlNode.java @@ -33,7 +33,7 @@ public class ShlNode extends LogicalNode { if( rhs._type instanceof TypeInteger shl && shl.isConstant() ) { // Shl of 0. - if( (shl.value()&63)==0 ) + if( (shl.value()&63)==0 && lhs._type instanceof TypeInteger ) return lhs; // (x + c) << i => (x << i) + (c << i) if( lhs instanceof AddNode add && add.addDep(this).in(2)._type instanceof TypeInteger c && c.isConstant() ) { diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/ShrNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/ShrNode.java index 926bc993..587e1631 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/ShrNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/ShrNode.java @@ -33,7 +33,7 @@ public class ShrNode extends LogicalNode { Type t2 = rhs._type; // Shr of 0. - if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 && lhs._type instanceof TypeInteger ) return lhs; // TODO: x >>> 3 >>> (y ? 1 : 2) ==> x >>> (y ? 4 : 5) diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/XorNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/XorNode.java index ef022a2c..034e352a 100644 --- a/chapter19/src/main/java/com/seaofnodes/simple/node/XorNode.java +++ b/chapter19/src/main/java/com/seaofnodes/simple/node/XorNode.java @@ -33,7 +33,7 @@ public class XorNode extends LogicalNode { // Xor of 0. We do not check for (0^x) because this will already // canonicalize to (x^0) - if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 && lhs._type instanceof TypeInteger ) return lhs; // Move constants to RHS: con*arg becomes arg*con |
