summaryrefslogtreecommitdiff
path: root/chapter15/src/test
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-19 09:33:28 -0700
committerCliff Click <cliffc@acm.org>2026-10-19 09:33:28 -0700
commitef8d9c4630db6af79c25526c8e80c54a48df4e39 (patch)
treec21474ceab3af7d8661ae93f7fc49002d96fcd43 /chapter15/src/test
parent300d2a9c3672acacc8bdd8d5370a424651a00571 (diff)
downloadSimple-ef8d9c4630db6af79c25526c8e80c54a48df4e39.tar.gz
Simple-ef8d9c4630db6af79c25526c8e80c54a48df4e39.zip
Fix memory on Load splitting up
Diffstat (limited to 'chapter15/src/test')
-rw-r--r--chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java55
1 files changed, 54 insertions, 1 deletions
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index d0e02eeb..5000a994 100644
--- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java
@@ -12,6 +12,57 @@ import static org.junit.Assert.fail;
public class Chapter15Test {
+ @Test public void testArrayLoadDominatingControl() {
+ String src = """
+ int[] a=new int[arg+1]; a[arg]=7;
+ if(a#>1) {
+ if(arg==2) a[1]=9;
+ return a[1];
+ }
+ return 42;
+ """;
+ StopNode stop = new Parser(src).parse();
+ var load = stop.walk(n -> n instanceof com.seaofnodes.simple.node.LoadNode ld && ld._name.equals("[]") ? ld : null);
+ org.junit.Assert.assertNotNull(load);
+ // The parser uses current control as a placeholder for a range check.
+ // Model an explicit check already performed before the inner diamond:
+ // both predecessors are below the a#>1 true projection.
+ var region = (com.seaofnodes.simple.node.RegionNode)load.in(0);
+ var checked = region.idom().cfg(0);
+ org.junit.Assert.assertTrue(checked instanceof com.seaofnodes.simple.node.CProjNode);
+ load.setDef(0,checked);
+ IterPeeps.iterate(stop).typeCheck();
+ org.junit.Assert.assertTrue("Common dominating control permits Phi splitting",load.isDead());
+ var split = stop.walk(n -> n instanceof com.seaofnodes.simple.node.LoadNode ld && ld._name.equals("[]") ? ld : null);
+ org.junit.Assert.assertNotNull(split);
+ org.junit.Assert.assertSame("The surviving read retains the earlier check",checked,split.in(0));
+ stop.iterate();
+ assertEquals(42L,Evaluator.evaluate(stop,0));
+ assertEquals(7L,Evaluator.evaluate(stop,1));
+ assertEquals(9L,Evaluator.evaluate(stop,2));
+ }
+
+
+ @Test public void testArrayLoadControl() {
+ // With arg==0, a has length 1 and a[1] must not execute. The memory
+ // merge must not pull the read above its array-length guard.
+ String src = """
+ int n=1; if(arg) n=2;
+ int[] a=new int[n]; a[arg]=7;
+ if(arg) a[1]=9;
+ if(a#>1) return a[1];
+ return 42;
+ """;
+ StopNode stop = new Parser(src).parse();
+ var load = stop.walk(n -> n instanceof com.seaofnodes.simple.node.LoadNode ld && ld._name.equals("[]") ? ld : null);
+ org.junit.Assert.assertNotNull(load);
+ org.junit.Assert.assertNotNull("Element loads carry their range-check control",load.in(0));
+ stop.iterate();
+ assertEquals(42L,Evaluator.evaluate(stop,0));
+ assertEquals(9L,Evaluator.evaluate(stop,1));
+ }
+
+
@Test public void testPartialAllocationMemory() {
StopNode stop = new Parser("""
struct Other { int a; int b; int c; }
@@ -99,7 +150,9 @@ if (arg) {
return b[0] + b[1];
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region72,1,0);", stop.toString());
+ // Controlled element loads stay below the merge until their range
+ // checks can be proved redundant.
+ assertEquals("return (.[]+.[]);", stop.toString());
assertEquals(0L, Evaluator.evaluate(stop, 0));
assertEquals(1L, Evaluator.evaluate(stop, 1));
}