summaryrefslogtreecommitdiff
path: root/chapter33/src/test
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-12-04 14:24:50 -0800
committerCliff Click <cliffc@acm.org>2026-12-04 14:24:50 -0800
commit2711be2c12980124b89e6c30a66a62f6d19a76cd (patch)
treea081ccb2423399af0a3c60c410818093567ec520 /chapter33/src/test
parent89d973c785f86974c52c64ee746bf3d58751fa1d (diff)
downloadSimple-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/test')
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java2
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java52
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/WorklistTest.java19
3 files changed, 72 insertions, 1 deletions
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index 738a4d15..b2d773ee 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -116,7 +116,7 @@ if (v1) {
return v0.f;
""");
code.driver(CodeGen.Phase.FinalizeIR);
- assertEquals("return Phi(Region,((.f<<32)>>32),2);", code.print());
+ assertEquals("return Phi(Region,.f,2);", code.print());
assertEquals("2",Eval2.eval(code,0));
assertEquals("0",Eval2.eval(code,1));
}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
index b625f989..b243beb2 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
@@ -96,4 +96,56 @@ public class Chapter33Test {
assertFalse("Expression lookahead created a type: "+name,name.endsWith(".foo"));
}
+ @Test public void testRepeatedSignExtension() {
+ for( int bits : new int[]{8,16,32} ) {
+ int shift = 64-bits;
+ String source = "i"+bits+" x=arg; return (x<<"+shift+")>>"+shift+";";
+ for( long seed : new long[]{1,123,126,456} ) {
+ CodeGen code = new CodeGen(source,seed,true).driver(CodeGen.Phase.FinalizeIR);
+ Node result = code.expr();
+ assertTrue(code.print(),result instanceof SarNode);
+ assertTrue(code.print(),result.in(1) instanceof ShlNode);
+ assertTrue("Only the original narrowing remains: "+code.print(),
+ result.in(1).in(1) instanceof ParmNode);
+ for( long arg : new long[]{0,1,-1,127,128,255,32767,32768,65535,
+ 2147483647L,2147483648L,4294967295L,Long.MIN_VALUE,Long.MAX_VALUE} )
+ assertEquals(Long.toString((arg<<shift)>>shift),Eval2.eval(code,arg));
+ }
+ }
+ }
+
+ @Test public void testSignExtensionKeepsNecessaryShifts() {
+ String[] sources = {
+ "u32 x=arg; return (x<<32)>>32;",
+ "i32 x=arg; return (x<<32)>>33;",
+ "i32 x=arg; return (x<<33)>>32;"
+ };
+ for( int i=0; i<sources.length; i++ ) {
+ CodeGen code = new CodeGen(sources[i]).driver(CodeGen.Phase.FinalizeIR);
+ for( long arg : new long[]{0,1,-1,2147483647L,2147483648L,4294967295L,Long.MIN_VALUE,Long.MAX_VALUE} ) {
+ long x = (int)arg;
+ long expected = i==0 ? x : i==1 ? (x<<32)>>33 : (x<<33)>>32;
+ assertEquals(Long.toString(expected),Eval2.eval(code,arg));
+ }
+ }
+ }
+
+ @Test public void testNarrowLoadBranchPhi() {
+ String source="""
+ struct Box { int n; };
+ val box=new Box;
+ if( arg ) box.n=arg;
+ else box.n=2;
+ return box.n;
+ """;
+ for( long seed : new long[]{1,123,126,456} ) {
+ CodeGen code=new CodeGen(source,seed,true).driver(CodeGen.Phase.FinalizeIR);
+ Node result=code.expr();
+ assertTrue("No second narrowing around the merged i32 values: "+code.print(),result instanceof PhiNode);
+ assertTrue(result._type.isa(TypeInteger.I32));
+ for( long arg : new long[]{0,1,-1,2147483647L,2147483648L,-2147483649L,4294967296L,Long.MIN_VALUE,Long.MAX_VALUE} )
+ assertEquals(Integer.toString(arg==0 ? 2 : (int)arg),Eval2.eval(code,arg));
+ }
+ }
+
}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/WorklistTest.java b/chapter33/src/test/java/com/seaofnodes/simple/WorklistTest.java
index fce15418..19d95f92 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/WorklistTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/WorklistTest.java
@@ -7,6 +7,25 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class WorklistTest {
+ @Test public void testSignExtensionAfterInputNarrows() {
+ CodeGen code = new CodeGen("");
+ Node value = code.con(TypeInteger.BOT);
+ Node shift = code.con(32);
+ ShlNode shl = new ShlNode(null,value,shift).init();
+ SarNode sar = new SarNode(null,shl,shift).init();
+ sar.keep();
+ code._iter._work.clear();
+ assertNull(sar.idealize());
+ assertFalse(code._iter._work.on(sar));
+ value.setType(TypeInteger.I32);
+ assertTrue(code._iter._work.on(shl));
+ assertNotSame("Shl propagates the narrower signed range",shl._type,shl.compute());
+ shl.setType(shl.compute());
+ assertTrue("The narrowing must wake the outer extension",code._iter._work.on(sar));
+ assertSame(value,sar.idealize());
+ sar.unkeep();
+ }
+
@Test public void testGuardMergeAfterPredicateRewrite() { guardMergeAfterPredicateRewrite(false); }
@Test public void testGuardMergeAfterPredicateSubsume() { guardMergeAfterPredicateRewrite(true); }