From 0dcd3513a633479ce228f24826db4f09e4c0fc8b Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 16 Nov 2026 14:25:14 -0800 Subject: Phi fix Fix Phi/Add refactoring peephole to canonicalize regardless of peephole order. Treat class fcns as having `self` be the class pointer --- .../java/com/seaofnodes/simple/node/PhiNode.java | 37 +++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'chapter17b/src/main/java') diff --git a/chapter17b/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter17b/src/main/java/com/seaofnodes/simple/node/PhiNode.java index 942748e7..836722d8 100644 --- a/chapter17b/src/main/java/com/seaofnodes/simple/node/PhiNode.java +++ b/chapter17b/src/main/java/com/seaofnodes/simple/node/PhiNode.java @@ -3,6 +3,7 @@ package com.seaofnodes.simple.node; import com.seaofnodes.print.ExprPrinter; import com.seaofnodes.simple.type.Type; +import com.seaofnodes.simple.type.TypeInteger; import com.seaofnodes.simple.type.TypeMem; import com.seaofnodes.simple.Utils; @@ -66,7 +67,8 @@ public class PhiNode extends Node { // Phi(op(a,b,...),op(c,d,...)) -> op(Phi(a,c),Phi(b,d),...). Node down; - if( same_op() && (down=drop_same_op())!=null ) return down; + Node op = factoringOp(); + if( same_op(op) && (down=drop_same_op(op))!=null ) return down; // If merging Phi(N, cast(N)) - we are losing the cast JOIN effects, so just remove. if( nIns()==3 ) { @@ -95,8 +97,26 @@ public class PhiNode extends Node { return null; } - private boolean same_op() { + // View x*2 and x<<1 as x+x only while matching mixed Phi arms. Do not + // expand the graph into Adds that would immediately strength-reduce again. + private Node factoringOp() { Node op = in(1); + if( doubled(op) ) + for( int i=2; i