From 9f38300fdeb866cc41efec700bd4a3f641dd4c2f Mon Sep 17 00:00:00 2001 From: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Date: Mon, 30 Oct 2023 21:54:03 +0100 Subject: Some improvements for chapter5 (#22) * Return null when the node cannot be copied * Make add_def package private again * Phi node can have more then two data inputs in theory * Use swap12 * Use addUse --- chapter05/src/main/java/com/seaofnodes/simple/Parser.java | 2 +- .../src/main/java/com/seaofnodes/simple/node/MulNode.java | 8 ++------ chapter05/src/main/java/com/seaofnodes/simple/node/Node.java | 10 +++++----- .../src/main/java/com/seaofnodes/simple/node/PhiNode.java | 10 +++++++++- .../src/main/java/com/seaofnodes/simple/node/StopNode.java | 5 +++++ 5 files changed, 22 insertions(+), 13 deletions(-) (limited to 'chapter05') diff --git a/chapter05/src/main/java/com/seaofnodes/simple/Parser.java b/chapter05/src/main/java/com/seaofnodes/simple/Parser.java index 9993c48c..70e093f3 100644 --- a/chapter05/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter05/src/main/java/com/seaofnodes/simple/Parser.java @@ -195,7 +195,7 @@ public class Parser { */ private Node parseReturn() { var expr = require(parseExpression(), ";"); - Node ret = STOP.add_def(new ReturnNode(ctrl(), expr).peephole()); + Node ret = STOP.addReturn(new ReturnNode(ctrl(), expr).peephole()); ctrl(null); // Kill control return ret; } diff --git a/chapter05/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter05/src/main/java/com/seaofnodes/simple/node/MulNode.java index dcc21b2e..5f6c1ad8 100644 --- a/chapter05/src/main/java/com/seaofnodes/simple/node/MulNode.java +++ b/chapter05/src/main/java/com/seaofnodes/simple/node/MulNode.java @@ -42,12 +42,8 @@ public class MulNode extends Node { return lhs; // Move constants to RHS: con*arg becomes arg*con - if ( t1.isConstant() && !t2.isConstant() ) { - Node tmp = in(1); // Swap inputs without letting either input go dead during the swap - _inputs.set(1,in(2)); - _inputs.set(2,tmp); - return this; - } + if ( t1.isConstant() && !t2.isConstant() ) + return swap12(); return null; } diff --git a/chapter05/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter05/src/main/java/com/seaofnodes/simple/node/Node.java index e34b4cd5..db8f5680 100644 --- a/chapter05/src/main/java/com/seaofnodes/simple/node/Node.java +++ b/chapter05/src/main/java/com/seaofnodes/simple/node/Node.java @@ -59,7 +59,7 @@ public abstract class Node { _outputs = new ArrayList<>(); for( Node n : _inputs ) if( n != null ) - n._outputs.add( this ); + n.addUse( this ); } // Easy reading label for debugger, e.g. "Add" or "Region" or "EQ" @@ -194,7 +194,7 @@ public abstract class Node { // This needs to happen before removing the old node's def->use edge as // the new_def might get killed if the old node kills it recursively. if( new_def != null ) - new_def._outputs.add(this); + new_def.addUse(this); if( old_def != null && // If the old def exists, remove a def->use edge old_def.delUse(this) ) // If we removed the last use, the old def is now dead old_def.kill(); // Kill old def @@ -211,12 +211,12 @@ public abstract class Node { * @param new_def the new definition, appended to the end of existing definitions * @return new_def for flow coding */ - public Node add_def(Node new_def) { + Node add_def(Node new_def) { // Add use->def edge _inputs.add(new_def); // If new def is not null, add the corresponding def->use edge if( new_def != null ) - new_def._outputs.add(this); + new_def.addUse(this); return new_def; } @@ -352,7 +352,7 @@ public abstract class Node { // Make a shallow copy (same class) of this Node, with given inputs and // empty outputs and a new Node ID. The original inputs are ignored. // Does not need to be implemented in isCFG() nodes. - Node copy(Node lhs, Node rhs) { throw new RuntimeException("TODO"); } + Node copy(Node lhs, Node rhs) { return null; } /** * Used to allow repeating tests in the same JVM. This just resets the diff --git a/chapter05/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter05/src/main/java/com/seaofnodes/simple/node/PhiNode.java index 5eb110e7..656e6182 100644 --- a/chapter05/src/main/java/com/seaofnodes/simple/node/PhiNode.java +++ b/chapter05/src/main/java/com/seaofnodes/simple/node/PhiNode.java @@ -31,7 +31,7 @@ public class PhiNode extends Node { @Override public Node idealize() { // Remove a "junk" Phi: Phi(x,x) is just x - if( in(1)==in(2) ) + if( same_inputs() ) return in(1); // Pull "down" a common data op. One less op in the world. One more @@ -61,4 +61,12 @@ public class PhiNode extends Node { return false; return true; } + + private boolean same_inputs() { + for( int i=2; i