From 2711be2c12980124b89e6c30a66a62f6d19a76cd Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 4 Dec 2026 14:24:50 -0800 Subject: Eliminate repeated sign extension of already narrowed integers Fold matching left/right shifts when their input already has the signed result type. Cover i8/i16/i32 boundaries, unequal shifts, unsigned input, branch Phis, and worklist propagation after narrowing. Update the affected Chapter13 IR expectation; retain all spill baselines. Validation: the repeated-extension test fails before the fix. make -k -j8 tests passes (829 executions), and Chapter33Test plus WorklistTest pass with simple.assert.expensive=1. --- .../src/main/java/com/seaofnodes/simple/node/SarNode.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'chapter33/src/main/java/com') 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) -- cgit v1.2.3