diff options
| author | Cliff Click <cliffc@acm.org> | 2026-12-04 14:24:50 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-12-04 14:24:50 -0800 |
| commit | 2711be2c12980124b89e6c30a66a62f6d19a76cd (patch) | |
| tree | a081ccb2423399af0a3c60c410818093567ec520 /chapter33/src/main | |
| parent | 89d973c785f86974c52c64ee746bf3d58751fa1d (diff) | |
| download | Simple-2711be2c12980124b89e6c30a66a62f6d19a76cd.tar.gz Simple-2711be2c12980124b89e6c30a66a62f6d19a76cd.zip | |
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.
Diffstat (limited to 'chapter33/src/main')
| -rw-r--r-- | chapter33/src/main/java/com/seaofnodes/simple/node/SarNode.java | 14 |
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) |
