From a996996a1ad776089a4cb9cbf7744f5f4e8dde6d Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 30 Sep 2026 16:41:07 -0700 Subject: Forward port mem stuff to ch21 Also a minor reg-alloc improvement for BrainFuck, ported up to ch25 --- .../java/com/seaofnodes/simple/Chapter20Test.java | 4 ++-- .../com/seaofnodes/simple/Chapter21AllocTest.java | 4 ++-- .../java/com/seaofnodes/simple/Chapter21Test.java | 3 ++- .../com/seaofnodes/simple/Chapter23AllocTest.java | 2 +- .../com/seaofnodes/simple/Chapter24AllocTest.java | 2 +- .../simple/codegen/RegAllocTestSupport.java | 25 ++++++++++++++++++++++ 6 files changed, 33 insertions(+), 7 deletions(-) (limited to 'chapter24/src/test') diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter20Test.java index 4e7cb156..5639f4f6 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter20Test.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter20Test.java @@ -115,7 +115,7 @@ val sqrt = { int x -> }; return sqrt(arg) + sqrt(arg+2); """; - testTarget(src,"x86_64_v2", "SystemV",48,null); + testTarget(src,"x86_64_v2", "SystemV",40,null); testTarget(src,"riscv" , "SystemV",17,null); testTarget(src,"arm" , "SystemV",18,null); } @@ -162,7 +162,7 @@ for( int i=0; i }; return sqrt(arg) + sqrt(arg+2); """; - Chapter21Test.testCPU(src,"x86_64_v2", "Win64" ,48,null); + Chapter21Test.testCPU(src,"x86_64_v2", "Win64" ,40,null); Chapter21Test.testCPU(src,"riscv" , "SystemV",19,null); Chapter21Test.testCPU(src,"arm" , "SystemV",19,null); } @@ -49,7 +49,7 @@ return sqrt(arg) + sqrt(arg+2); String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash21.smp")); Chapter21Test.testCPU(src,"x86_64_v2", "SystemV", 9,null); Chapter21Test.testCPU(src,"riscv" , "SystemV", 3,null); - Chapter21Test.testCPU(src,"arm" , "SystemV", 3,null); + Chapter21Test.testCPU(src,"arm" , "SystemV", 1,null); } } diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter21Test.java index dcdda33b..b57c7ddd 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter21Test.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter21Test.java @@ -196,6 +196,7 @@ public class Chapter21Test { @Test public void testCoalescing() { com.seaofnodes.simple.codegen.RegAllocTestSupport.coalescing(); } + @Test public void testCopyForward() throws Exception { com.seaofnodes.simple.codegen.RegAllocTestSupport.copyForward(); } @Test public void testNarrowStores() throws IOException { for( String type : new String[]{"i8","u8","i16","u16"} ) { @@ -392,7 +393,7 @@ val _hashCodeString = { String self -> """; testCPU(src,"x86_64_v2", "SystemV", 9,null); testCPU(src,"riscv" , "SystemV", 3,null); - testCPU(src,"arm" , "SystemV", 3,null); + testCPU(src,"arm" , "SystemV", 1,null); } @Test public void testStringExport() throws IOException { diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter23AllocTest.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter23AllocTest.java index cd8c43cf..7f4b86d5 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter23AllocTest.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter23AllocTest.java @@ -11,7 +11,7 @@ public class Chapter23AllocTest { String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp")); Chapter23Test.testCPU(src,"x86_64_v2", "SystemV", 9,null); Chapter23Test.testCPU(src,"riscv" , "SystemV", 3,null); - Chapter23Test.testCPU(src,"arm" , "SystemV", 3,null); + Chapter23Test.testCPU(src,"arm" , "SystemV", 1,null); } @Test diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter24AllocTest.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter24AllocTest.java index a320e21f..f8c88fd7 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter24AllocTest.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter24AllocTest.java @@ -25,7 +25,7 @@ val sqrt = { int x -> int cast_int = arg+2; return sqrt(arg) + sqrt(cast_int); """; - Chapter24Test.testCPU(src,"x86_64_v2", "Win64" ,48,null); + Chapter24Test.testCPU(src,"x86_64_v2", "Win64" ,40,null); Chapter24Test.testCPU(src,"riscv" , "SystemV",19,null); Chapter24Test.testCPU(src,"arm" , "SystemV",19,null); } diff --git a/chapter24/src/test/java/com/seaofnodes/simple/codegen/RegAllocTestSupport.java b/chapter24/src/test/java/com/seaofnodes/simple/codegen/RegAllocTestSupport.java index c0a26d60..0ccd9741 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/codegen/RegAllocTestSupport.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/codegen/RegAllocTestSupport.java @@ -246,6 +246,31 @@ public class RegAllocTestSupport { } } + public static void copyForward() throws Exception { + // Forwarding, fixed operand, two-address tie, intervening clobber, + // another user, and a scheduling-only edge. + for( int kind=0; kind<6; kind++ ) { + CodeGen code = graph(); + RegAlloc alloc = new RegAlloc(code); + Op def = new Op(A,null,null,false,code._start); + alloc.newLRG(def)._reg=0; + SplitNode copy = code._mach.split("test",(byte)0,null); + copy.setDef(0,code._start); copy.setDef(1,def); + alloc.newLRG(copy)._reg=1; + if( kind==3 ) new Op(null,null,A,false,code._start); + int twoAddress = kind==2 ? 1 : 0; + RegMask mask = kind==1 ? B : kind==5 ? null : new RegMask(3L); + Op use = new Op(null,mask,null,false,code._start,copy) { + @Override public int twoAddress() { return twoAddress; } + }; + if( kind==4 ) new Op(null,B,null,false,code._start,copy); + var post = RegAlloc.class.getDeclaredMethod("postColor"); + post.setAccessible(true); post.invoke(alloc); + assertSame("copy case "+kind,kind==0 ? def : copy,use.in(1)); + assertEquals(kind==0 ? 0 : 1,alloc._spills); + } + } + public static void coalescing() { // Merge, incompatible masks, interference, capacity, and adjacency remapping. for( int kind=0; kind<5; kind++ ) { -- cgit v1.2.3