From bb3147d5b212d675ca2575ac1b571aaaf478cf5e Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 15 Sep 2024 13:59:55 -0700 Subject: Folding loads before phis needs more work (#116) * Folding loads before phis needs more work Have to check load alias & offset/field match. Could work for stores as well, will need to check more inputs * Preserve SAR/SHL sharpness with variant shifts * phi-node cloning honors unrelated argument types * bool can never do phiCon * dont fold shifts outside the range 0-63 * Now allowed to fold Loads in Phi Because honors glb() of inputs * Correct shr of mixed sign * yank load merge again needs an anti-dep check * Stop phi-merge if it loses precision * wrap bug --- .../java/com/seaofnodes/simple/Chapter06Test.java | 2 +- .../java/com/seaofnodes/simple/Chapter14Test.java | 108 +++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) (limited to 'chapter14/src/test/java/com') diff --git a/chapter14/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/chapter14/src/test/java/com/seaofnodes/simple/Chapter06Test.java index cddf0ae2..516dbf09 100644 --- a/chapter14/src/test/java/com/seaofnodes/simple/Chapter06Test.java +++ b/chapter14/src/test/java/com/seaofnodes/simple/Chapter06Test.java @@ -65,7 +65,7 @@ else b=5; return b;""", TypeInteger.BOT); StopNode stop = parser.parse().iterate(true); - assertEquals("return Phi(Region39,42,5);", stop.toString()); + assertEquals("return Phi(Region34,42,5);", stop.toString()); } @Test diff --git a/chapter14/src/test/java/com/seaofnodes/simple/Chapter14Test.java b/chapter14/src/test/java/com/seaofnodes/simple/Chapter14Test.java index 7d6fecf9..0464f3ec 100644 --- a/chapter14/src/test/java/com/seaofnodes/simple/Chapter14Test.java +++ b/chapter14/src/test/java/com/seaofnodes/simple/Chapter14Test.java @@ -157,6 +157,19 @@ return j; assertEquals(1L, Evaluator.evaluate(stop, 1)); } + @Test + public void testWrapShr() { + Parser parser = new Parser( +""" +return (arg >>> 1)==0; +"""); + StopNode stop = parser.parse(false).iterate(true); + assertEquals("return (!(arg>>>1));", stop.toString()); + assertEquals(1L, Evaluator.evaluate(stop, 0)); + assertEquals(1L, Evaluator.evaluate(stop, 1)); + assertEquals(0L, Evaluator.evaluate(stop, 2)); + } + @Test public void testOr() { Parser parser = new Parser( @@ -180,6 +193,101 @@ return arg; catch( Exception e ) { assertEquals("Cannot '&' FltBot",e.getMessage()); } } + @Test + public void testLoadBug() { + Parser parser = new Parser( +""" +struct A { int i; } +struct B { int i; } +A a = new A; +A t1 = new A; +B b = new B; +B t2 = new B; +int i; +if (arg) i = a.i; +else i = b.i; +return i; +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return Phi(Region31,.i,.i);", stop.toString()); + assertEquals(0L, Evaluator.evaluate(stop, 0)); + } + + @Test + public void testBug2() { + Parser parser = new Parser( +""" +int z = 0; +while (1) { + int j; + if (arg&3) { + j = arg >> 2; + } else { + j = (arg >> 3)+z; + } + return j+1; +} +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return ((arg>>Phi(Region32,2,3))+1);", stop.toString()); + assertEquals(1L, Evaluator.evaluate(stop, 0)); + assertEquals(1L, Evaluator.evaluate(stop, 1)); + assertEquals(2L, Evaluator.evaluate(stop, 7)); + assertEquals(2L, Evaluator.evaluate(stop, 8)); + } + + @Test + public void testBug3() { + Parser parser = new Parser( +""" +flt f = arg; +bool b; +if (arg&3) b = f == 1.0; +else b = f == 2.0; +if (arg&5) b = arg == 1; +return b; +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return Phi(Region37,(arg==1),((flt)arg==Phi(Region23,1.0,2.0)));", stop.toString()); + assertEquals(1L, Evaluator.evaluate(stop, 1)); + assertEquals(0L, Evaluator.evaluate(stop, 2)); + assertEquals(0L, Evaluator.evaluate(stop, 3)); + } + + @Test + public void testBug4() { + Parser parser = new Parser( +""" +int i; +if (arg&7) i=3; +else i=2; +return (arg == i) == 1; +"""); + StopNode stop = parser.parse(false).iterate(true); + assertEquals("return (arg==Phi(Region18,3,2));", stop.toString()); + assertEquals(0L, Evaluator.evaluate(stop, 0)); + assertEquals(0L, Evaluator.evaluate(stop, 1)); + assertEquals(0L, Evaluator.evaluate(stop, 2)); + assertEquals(1L, Evaluator.evaluate(stop, 3)); + } + + @Test + public void testBug5() { + Parser parser = new Parser( +""" +int d = -1; +while (1) { + int i = (arg & 7) + (9223372036854775807 - 7); + if (arg & 1) i = ((arg&3) + (9223372036854775807 - 3)) & d; + return i + -1; +} +"""); + StopNode stop = parser.parse(false).iterate(true); + assertEquals("return (Phi(Region43,((arg&3)+9223372036854775804),((arg&7)+9223372036854775800))+-1);", stop.toString()); + assertEquals(0x7FFFFFFFFFFFFFF7L, Evaluator.evaluate(stop, 0)); + assertEquals(0x7FFFFFFFFFFFFFFCL, Evaluator.evaluate(stop, 1)); + } + @Test public void testTypes() { Parser parser = new Parser( -- cgit v1.2.3