summaryrefslogtreecommitdiff
path: root/chapter33/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'chapter33/src/main/java')
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/node/SarNode.java14
1 files changed, 11 insertions, 3 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/SarNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/SarNode.java
index 2e70f345..854a0d9b 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/node/SarNode.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/node/SarNode.java
@@ -26,9 +26,17 @@ public class SarNode extends ArithNode {
Type t2 = rhs._type;
- // Sar of 0.
- if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 && forceInt(lhs) && lhs._type instanceof TypeInteger )
- return lhs;
+ if( t2 instanceof TypeInteger i && i.isConstant() && forceInt(lhs) && lhs._type instanceof TypeInteger ) {
+ int shift = (int)(i.value()&63);
+ if( shift==0 ) return lhs;
+ // A value already in the signed range needs no further extension.
+ if( lhs instanceof ShlNode && lhs.in(2)._type instanceof TypeInteger j &&
+ j.isConstant() && (j.value()&63)==shift ) {
+ Node value = lhs.in(1);
+ if( value._type instanceof TypeInteger && value._type==_type )
+ return value;
+ }
+ }
// TODO: x >> 3 >> (y ? 1 : 2) ==> x >> (y ? 4 : 5)