summaryrefslogtreecommitdiff
path: root/chapter16/src
diff options
context:
space:
mode:
Diffstat (limited to 'chapter16/src')
-rw-r--r--chapter16/src/main/java/com/seaofnodes/simple/Parser.java2
-rw-r--r--chapter16/src/main/java/com/seaofnodes/simple/node/LoadNode.java3
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter15Test.java51
3 files changed, 55 insertions, 1 deletions
diff --git a/chapter16/src/main/java/com/seaofnodes/simple/Parser.java b/chapter16/src/main/java/com/seaofnodes/simple/Parser.java
index 41a3fcaa..b4cfaaf3 100644
--- a/chapter16/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter16/src/main/java/com/seaofnodes/simple/Parser.java
@@ -910,7 +910,7 @@ public class Parser {
// Arrays include control, as a proxy for a safety range check
// Structs don't need this; they only need a NPE check which is
// done via the type system.
- //if( base.isAry() ) load.setDef(0,ctrl());
+ if( base.isAry() ) load.setDef(0,ctrl());
return parsePostfix(load.peephole());
}
diff --git a/chapter16/src/main/java/com/seaofnodes/simple/node/LoadNode.java b/chapter16/src/main/java/com/seaofnodes/simple/node/LoadNode.java
index 8d604b28..ab53aa6a 100644
--- a/chapter16/src/main/java/com/seaofnodes/simple/node/LoadNode.java
+++ b/chapter16/src/main/java/com/seaofnodes/simple/node/LoadNode.java
@@ -122,6 +122,9 @@ public class LoadNode extends MemOpNode {
// else ptr.x = e1; : e1;
// val = ptr.x; ptr.x = val;
if( mem() instanceof MemPhiNode memphi && memphi.region()._type == Type.CONTROL && memphi.nIns()== 3 &&
+ // Retain control on both copies only if it dominates both paths.
+ // The Region itself, or a later range check, cannot move to its inputs.
+ (in(0)==null || cfg0().domLCA(memphi.region().idom(this),this)==cfg0()) &&
off() instanceof ConstantNode ) {
// Profit on RHS/Loop backedge
if( profit(memphi,2) ||
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index 63bdcce7..90d401f5 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter15Test.java
@@ -9,6 +9,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));
+ }
+
+
@Ignore
@Test
public void testJig() {