summaryrefslogtreecommitdiff
path: root/chapter20/src/main/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-30 09:06:50 -0700
committerCliff Click <cliffc@acm.org>2026-09-30 09:06:50 -0700
commitf01ee0522e63b0266314b4e88e524ea2da95cf0d (patch)
tree679036250cf5bc135d482da39b14be98a6c1791f /chapter20/src/main/java/com/seaofnodes
parent612555ee37f733ff3f902b2ab149b318db334aec (diff)
downloadSimple-f01ee0522e63b0266314b4e88e524ea2da95cf0d.tar.gz
Simple-f01ee0522e63b0266314b4e88e524ea2da95cf0d.zip
ch 20 forward port memory
Diffstat (limited to 'chapter20/src/main/java/com/seaofnodes')
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/Parser.java105
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java54
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java240
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/LoadNode.java68
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java190
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/MemOpNode.java33
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/MemPhiNode.java44
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/NewNode.java58
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/Node.java14
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/PhiNode.java102
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/RegionNode.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/ReturnNode.java12
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/ScopeNode.java75
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/StartNode.java11
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/StoreNode.java28
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/MemOpARM.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/NewARM.java6
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/StoreARM.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/MemOpRISC.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/NewRISC.java6
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/StoreRISC.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/riscv.java2
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/MemOpX86.java4
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/NewX86.java6
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java7
-rw-r--r--chapter20/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java2
27 files changed, 688 insertions, 391 deletions
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/Parser.java b/chapter20/src/main/java/com/seaofnodes/simple/Parser.java
index af047850..16018fba 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/Parser.java
@@ -138,7 +138,7 @@ public class Parser {
_scope.define(ScopeNode.ARG0, TypeInteger.BOT, false, null, _lexer);
ctrl(XCTRL);
- _scope.mem(new MemMergeNode(false));
+ _scope.mem(con(TypeMem.BOT));
// Parse whole program, as-if function header "{ int arg -> body }"
parseFunctionBody(_code._main,loc(),"arg");
@@ -178,7 +178,7 @@ public class Parser {
// Build a multi-exit return point for all function returns
RegionNode r = new RegionNode((Lexer)null,null,null).init();
assert r.inProgress();
- PhiNode rmem = new PhiNode(ScopeNode.MEM0,TypeMem.BOT,r,null).init();
+ PhiNode rmem = new BulkMemPhiNode(ScopeNode.MEM0,r,null).init();
PhiNode rrez = new PhiNode(ScopeNode.ARG0,Type.BOTTOM,r,null).init();
ReturnNode ret = new ReturnNode(r, rmem, rrez, rpc, fun).init();
fun.setRet(ret);
@@ -190,11 +190,8 @@ public class Parser {
// external calls.
_scope.push(ScopeNode.Kind.Function);
ctrl(fun); // Scope control from function
- // Private mem alias tracking per function
- MemMergeNode mem = new MemMergeNode(true);
- mem.addDef(null); // Alias#0
- mem.addDef(new ParmNode(ScopeNode.MEM0,1,TypeMem.BOT,fun,con(TypeMem.BOT)).peephole()); // All aliases
- _scope.mem(mem);
+ // Unknown callers supply whole memory; field aliases are discovered lazily.
+ _scope.mem(new ParmNode(ScopeNode.MEM0,1,TypeMem.BOT,fun,con(TypeMem.BOT)).peephole());
// All args, "as-if" called externally
for( int i=0; i<ids.length; i++ ) {
Type t = sig.arg(i);
@@ -208,7 +205,7 @@ public class Parser {
// Last expression is the return
if( ctrl()._type==Type.CONTROL )
- fun.addReturn(ctrl(), _scope.mem().merge(), last);
+ fun.addReturn(ctrl(), _scope.mem(), last);
// Pop off the inProgress node on the multi-exit Region merge
assert r.inProgress();
@@ -568,9 +565,8 @@ public class Parser {
*/
private Node parseReturn() {
var expr = require(parseAsgn(), ";");
- // Need default memory, since it can be lazy, need to force
- // a non-lazy Phi
- _fun.addReturn(ctrl(), _scope.mem().merge(), expr);
+ // Force the lazy whole-memory Phi before closing this exit.
+ _fun.addReturn(ctrl(), _scope.mem(), expr);
ctrl(XCTRL); // Kill control
return expr;
}
@@ -1183,29 +1179,31 @@ public class Parser {
Field[] fs = obj._fields;
if( fs==null )
throw error("Unknown struct type '" + obj._name + "'");
- int len = fs.length;
- Node[] ns = new Node[2+len];
- ns[0] = ctrl(); // Control in slot 0
- // Total allocated length in bytes
- ns[1] = size;
- // Memory aliases for every field
- for( int i = 0; i < len; i++ )
- ns[2+i] = memAlias(fs[i]._alias);
- Node nnn = new NewNode(TypeMemPtr.make(obj), ns).peephole().keep();
- for( int i = 0; i < len; i++ )
- memAlias(fs[i]._alias, new ProjNode(nnn,i+2,memName(fs[i]._alias)).peephole());
- Node ptr = new ProjNode(nnn.unkeep(),1,obj._name).peephole().keep();
-
- // Initial nonzero values for every field
- for( int i = 0; i < len; i++ ) {
- Node val = init.get( i + idx );
+ Node prior = mem().keep();
+ MemMergeNode input = new MemMergeNode((Node)null);
+ for( Field f : fs ) input.alias(f._alias,prior);
+ // Allocation zeroes its fields; nonzero initializers become explicit Stores.
+ Node[] ns = new Node[3];
+ ns[0] = ctrl();
+ ns[1] = input.peephole();
+ ns[2] = size;
+ Node nnn = new NewNode(TypeMemPtr.make(obj),ns).peephole().keep();
+ Node ptr = new ProjNode(nnn,0,obj._name).peephole().keep();
+ Node out = new ProjNode(nnn,1,"$mem").peephole().keep();
+ MemMergeNode after = new MemMergeNode(prior);
+ for( Field f : fs ) after.alias(f._alias,out);
+ mem(after.peephole());
+ out.unkeep();
+ nnn.unkeep();
+ prior.unkeep();
+ for( int i=0; i<fs.length; i++ ) {
+ Node val = init.get(idx+i);
if( val._type != val._type.makeZero() ) {
- Node mem = memAlias(fs[i]._alias);
- Node st = new StoreNode(loc(),fs[i]._fname,fs[i]._alias,fs[i]._type,mem,ptr,con(obj.offset(i)),val,true).peephole();
- memAlias(fs[i]._alias,st);
+ Field f = fs[i];
+ Node st = new StoreNode(loc(),f._fname,f._alias,f._type,mem(),ptr,con(obj.offset(i)),val,true).peephole();
+ mem(new MemMergeNode(mem(),f._alias,st).peephole());
}
}
-
return ptr.unkeep();
}
@@ -1218,13 +1216,18 @@ public class Parser {
return newStruct(ary,size,0,ALTMP);
}
- // We set up memory aliases by inserting special vars in the scope these
- // variables are prefixed by $ so they cannot be referenced in Simple code.
- // Using vars has the benefit that all the existing machinery of scoping
- // and phis work as expected
- private Node memAlias(int alias ) { return _scope.mem(alias ); }
- private void memAlias(int alias, Node st) { _scope.mem(alias, st); }
- public static String memName(int alias) { return ("$"+alias).intern(); }
+ // Memory is one hidden SSA variable, including across branches and loops.
+ private Node mem() { return _scope.mem(); }
+ private void mem(Node n) { _scope.mem(n); }
+
+ private void store(String name, int alias, Type glb, Node ptr, Node off, Node val, boolean init, Node ctrl) {
+ Node prior = mem().keep();
+ Node st = new StoreNode(loc(),name,alias,glb,prior,ptr,off,val,init);
+ st.setDef(0,ctrl);
+ st = st.peephole();
+ mem(new MemMergeNode(prior,alias,st).peephole());
+ prior.unkeep();
+ }
/**
* Parse postfix expression; this can be a field expression, an array
@@ -1267,6 +1270,7 @@ public class Parser {
if( tf instanceof TypeMemPtr ftmp && ftmp.isFRef() )
tf = ftmp.makeFrom(((TypeMemPtr)(TYPES.get(ftmp._obj._name)))._obj);
+ expr.keep();
// Field offset; fixed for structs, computed for arrays
Node off = (name.equals("[]") // If field is an array body
// Array index math
@@ -1279,16 +1283,11 @@ public class Parser {
Node val = parseAsgn().keep();
Node lift = liftExpr( val, tf, f._final );
- Node st = new StoreNode(loc(), name, f._alias, tf, memAlias(f._alias), expr, off.unkeep(), lift, false);
- // 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() ) st.setDef(0,ctrl());
- memAlias(f._alias, st.peephole());
+ store(name,f._alias,tf,expr.unkeep(),off.unkeep(),lift,false,base.isAry() ? ctrl() : null);
return val.unkeep(); // "obj.a = expr" returns the expression while updating memory
}
- Node load = new LoadNode(loc(),name, f._alias, tf, memAlias(f._alias), expr.keep(), off);
+ Node load = new LoadNode(loc(),name, f._alias, tf, mem(), expr, off);
// 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.
@@ -1299,12 +1298,7 @@ public class Parser {
char ch = _lexer.matchOperAssign();
if( ch!=0 ) {
Node op = opAssign(ch,load,f._final,tf,name);
- Node st = new StoreNode(loc(), name, f._alias, tf, memAlias(f._alias), expr.unkeep(), off, op, false);
- // 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() ) st.setDef(0,ctrl());
- memAlias(f._alias, peep(st));
+ store(name,f._alias,tf,expr.unkeep(),off,op,false,base.isAry() ? ctrl() : null);
load = postfix(ch) ? load.unkeep() : op;
// And use the original loaded value as the result
} else expr.unkill();
@@ -1385,7 +1379,7 @@ public class Parser {
}
// Control & memory after parsing args
args.set(0,ctrl().keep());
- args.set(1,_scope.mem().merge().keep());
+ args.set(1,_scope.mem().keep());
args.push(expr); // Function pointer
// Unkeep them all
for( Node arg : args )
@@ -1405,11 +1399,8 @@ public class Parser {
CallEndNode cend = (CallEndNode)new CallEndNode(call).peephole();
// Control from CallEnd
ctrl(new CProjNode(cend,0,ScopeNode.CTRL).peephole());
- // Memory from CallEnd
- MemMergeNode mem = new MemMergeNode(true);
- mem.addDef(null); // Alias#0
- mem.addDef(new ProjNode(cend,1,ScopeNode.MEM0).peephole());
- _scope.mem(mem);
+ // A call may update any alias, so its memory result is the new whole state.
+ _scope.mem(new ProjNode(cend,1,ScopeNode.MEM0).peephole());
// Call result
return new ProjNode(cend,2,"#2").peephole();
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java b/chapter20/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java
index 4ebd86c0..52906afb 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/codegen/GlobalCodeMotion.java
@@ -51,7 +51,7 @@ public abstract class GlobalCodeMotion {
var cons = new ArrayList<Node>();
for( Node con : start._outputs )
if( con!=null && !(con instanceof CFGNode) &&
- (con.isConst() || con instanceof MachNode mach && mach.outregmap()!=null) ) {
+ (con.isConst() || (con instanceof MachNode mach && mach.outregmap()!=null)) ) {
globals.put(con,true);
cons.add(con.keep()); // Preserve original inputs while rewiring users.
}
@@ -172,15 +172,9 @@ public abstract class GlobalCodeMotion {
continue outer; // Nope, await all uses done
// Loads need their memory inputs' uses also done
- if( n instanceof LoadNode ld )
- for( Node memuse : ld.mem()._outputs )
- if( late[memuse._nid]==null &&
- // New makes new memory, never crushes load memory
- !(memuse instanceof NewNode) &&
- // Load-use directly defines memory
- (memuse._type instanceof TypeMem ||
- // Load-use indirectly defines memory
- (memuse._type instanceof TypeTuple tt && tt._types[ld._alias] instanceof TypeMem)) )
+ if( n instanceof MemOpNode ld && !ld.isMem() )
+ for( Node memuse : ld.antiDeps() )
+ if( late[memuse._nid]==null )
continue outer;
// All uses done, schedule
@@ -192,9 +186,7 @@ public abstract class GlobalCodeMotion {
for( Node def : n._inputs ) {
if( def==null ) continue;
if( late[def._nid]==null ) work.push(def);
- for( Node out : def._outputs )
- if( out instanceof LoadNode ld && late[ld._nid]==null )
- work.push(ld);
+ wakeLoads(def,late,work,new BitSet());
}
if( n instanceof LoopNode loop )
for( Node phi : loop._outputs )
@@ -213,7 +205,7 @@ public abstract class GlobalCodeMotion {
lca = use_block(n,use, late).domLCA(lca,null);
// Loads may need anti-dependencies, raising their LCA
- if( n instanceof LoadNode load )
+ if( n instanceof MemOpNode load && !load.isMem() )
lca = find_anti_dep(lca,load,early,late,anti);
// Walk up from the LCA to the early, looking for best place. This is
@@ -253,15 +245,26 @@ public abstract class GlobalCodeMotion {
best instanceof IfNode;
}
- private static CFGNode find_anti_dep(CFGNode lca, LoadNode load, CFGNode early, CFGNode[] late, int[] anti) {
+ // An allocation may consume a load's memory through a partial aggregate.
+ private static void wakeLoads(Node def, CFGNode[] late, WorkList<Node> work, BitSet visit) {
+ if( visit.get(def._nid) ) return;
+ visit.set(def._nid);
+ for( Node out : def._outputs )
+ if( out instanceof MemOpNode ld && !ld.isMem() && late[ld._nid]==null ) work.push(ld);
+ if( def instanceof MemMergeNode )
+ for( int i=1; i<def.nIns(); i++ )
+ if( def.in(i)!=null ) wakeLoads(def.in(i),late,work,visit);
+ }
+
+ private static CFGNode find_anti_dep(CFGNode lca, MemOpNode load, CFGNode early, CFGNode[] late, int[] anti) {
// We could skip final-field loads here.
// Walk LCA->early, flagging Load's block location choices
for( CFGNode cfg=lca; early!=null && cfg!=early.idom(); cfg = cfg.idom() )
anti[cfg._nid] = load._nid;
// Walk load->mem uses, looking for Stores causing an anti-dep
- for( Node mem : load.mem()._outputs ) {
+ for( Node mem : load.antiDeps() ) {
switch( mem ) {
- case StoreNode st:
+ case MemOpNode st:
assert late[st._nid]!=null;
lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st,anti);
break;
@@ -276,11 +279,10 @@ public abstract class GlobalCodeMotion {
if( phi.in(i)==load.mem() )
lca = anti_dep(load,phi.region().cfg(i),load.mem().cfg0(),lca,null,anti);
break;
- case NewNode st: break;
- case LoadNode ld: break; // Loads do not cause anti-deps on other loads
- case ReturnNode ret: break; // Load must already be ahead of Return
- case MemMergeNode ret: break; // Mem uses now on ScopeMin
- case NeverNode never: break;
+ case NewNode st:
+ assert late[st._nid]!=null;
+ lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st,anti);
+ break;
default: throw Utils.TODO();
}
}
@@ -288,15 +290,13 @@ public abstract class GlobalCodeMotion {
}
//
- private static CFGNode anti_dep( LoadNode load, CFGNode stblk, CFGNode defblk, CFGNode lca, Node st, int[] anti ) {
- // Preserve the full store range for the earlier evaluator scheduler.
- // It places nodes independently of GCM and may hoist this store.
+ private static CFGNode anti_dep( MemOpNode load, CFGNode stblk, CFGNode defblk, CFGNode lca, Node st, int[] anti ) {
for( ; stblk != defblk.idom(); stblk = stblk.idom() ) {
// Store and Load overlap, need anti-dependence
if( anti[stblk._nid]==load._nid ) {
lca = stblk.domLCA(lca,null); // Raise Loads LCA
- if( lca == stblk && st != null && st._inputs.find(load) == -1 ) // And if something moved,
- st.addDef(load); // Add anti-dep as well
+ if( lca == stblk && st != null && !(st instanceof CallNode) && st._inputs.find(load) == -1 ) // And if something moved,
+ st.addDef(load); // Add anti-dep as well; Calls already end their block.
return lca; // Cap this stores' anti-dep to here
}
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java
new file mode 100644
index 00000000..e9d4b897
--- /dev/null
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java
@@ -0,0 +1,240 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
+import java.util.BitSet;
+
+/**
+ * A Phi for bulk memory. It covers all aliases except {@link #_aliases};
+ * those aliases must be represented by parallel precise MemPhis at the same
+ * program slice.
+ */
+public class BulkMemPhiNode extends PhiNode {
+
+ private static final BitSet EMPTY = new BitSet();
+
+ // Excluded Aliases; empty means NO exclusions.
+ // Each split clones the exclusions; published sets are never mutated.
+ public final BitSet _aliases;
+
+ public BulkMemPhiNode(String label, Node... inputs) {
+ this(label,EMPTY,inputs);
+ }
+
+ private BulkMemPhiNode(String label, BitSet aliases, Node... inputs) {
+ super(label,TypeMem.BOT,inputs);
+ _aliases = aliases;
+ }
+
+ public boolean isSplit(int alias) { return _aliases.get(alias); }
+
+ public BulkMemPhiNode(BulkMemPhiNode phi) { super(phi); _aliases = phi._aliases; }
+
+ @Override public String label() { return "BulkPhi"+_aliases; }
+
+ @Override
+ public Type compute() {
+ Type t = super.compute();
+ return t instanceof TypeMem mem ? mem :
+ t==Type.BOTTOM ? TypeMem.BOT :
+ TypeMem.TOP;
+ }
+
+ @Override
+ public Node idealize() {
+ Node progress = super.idealize();
+ if( progress != null ) return progress;
+ if( ((RegionNode)region()).inProgress() || region().nIns()<=1 )
+ return null; // Input is in-progress
+
+ // "Peek through" a MemMerge that covers this alias set on its default
+ for( int i=1; i<nIns(); i++ )
+ if( in(i) instanceof MemMergeNode mmm && canPeek(mmm) ) {
+ setDef(i,CodeGen.CODE.add(mmm.in(1)));
+ return this;
+ }
+
+ // If any input or output uses or defines a specific alias we cover,
+ // make a private MemPhi for it, and add it to the excluded list.
+ for( int i=1; i<nIns(); i++ ) {
+ int alias = inputAlias(in(i));
+ if( alias!=0 )
+ return slice(alias);
+ }
+
+ for( int i=0; i<_outputs.size(); i++ ) {
+ int alias = outputAlias(_outputs.get(i));
+ if( alias!=0 )
+ return slice(alias);
+ }
+
+ return null;
+ }
+
+ static public boolean checkMem(Node n, int alias) {
+ for( Node m : n._inputs )
+ // Bulk memory supplies this alias
+ if( m instanceof BulkMemPhiNode bulk ) {
+ if( bulk._aliases.get(alias) )
+ return false;
+ } else if( m instanceof MemMergeNode mmm &&
+ mmm.in(1) instanceof BulkMemPhiNode bulk &&
+ bulk._aliases.get(alias) &&
+ (alias >= mmm.nIns() || mmm.in(alias)==null) )
+ return false;
+ return true;
+ }
+
+
+ // True if the MemMerge has no precise slice still covered by this Phi.
+ private boolean canPeek(MemMergeNode mmm) {
+ return mmm.in(1)!=null && missingAlias(mmm)==0;
+ }
+
+ // Return an alias required by an input but still covered by this Phi, or 0.
+ private int inputAlias(Node n) {
+ return switch(n) {
+ case ProjNode proj -> 0;
+ case ParmNode parm -> 0;
+ case ConstantNode con -> 0;
+ case MemMergeNode mmm -> missingAlias(mmm);
+ case MemOpNode mem -> unsplit(mem._alias);
+ case BulkMemPhiNode bulk -> missingAlias(bulk);
+ default -> throw new AssertionError("Unexpected memory input "+n);
+ };
+ }
+
+ // Return an alias required by a user but still covered by this Phi, or 0.
+ private int outputAlias(Node use) {
+ // This decision depends backwards on an immediate user's alias/set.
+ // Normal graph-neighbor enqueueing flows from defs to uses, so retain
+ // an explicit forward dependency to revisit this Phi when the user
+ // sharpens.
+ use.addDepForwards(this);
+ return switch(use) {
+ case ScopeNode scope -> 0;
+ case MemMergeNode mmm -> missingAlias(mmm);
+ case BulkMemPhiNode bulk -> missingAlias(bulk);
+ case MemOpNode mem -> unsplit(mem._alias);
+ case MemPhiNode phi -> unsplit(phi._alias);
+ default -> {
+ assert use instanceof ReturnNode || use instanceof CallNode || use instanceof ParmNode
+ : "Unexpected bulk-memory user "+use;
+ yield 0;
+ }
+ };
+ }
+
+ // Alias zero/one is bulk, and an already excluded precise alias cannot
+ // trigger another split of this Phi.
+ private int unsplit(int alias) {
+ if( alias <= 1 ) return 0;
+ assert !_aliases.get(alias);
+ return alias;
+ }
+
+ // First alias excluded by the neighboring bulk Phi but not by this one.
+ private int missingAlias(BulkMemPhiNode bulk) {
+ for( int alias = bulk._aliases.nextSetBit(0); alias >= 0; alias = bulk._aliases.nextSetBit(alias+1) )
+ if( !_aliases.get(alias) )
+ return alias;
+ return 0;
+ }
+
+ // Every explicit slot requests that alias, including a partial allocation
+ // input whose slot still points to this bulk Phi.
+ private int missingAlias(MemMergeNode mmm) {
+ for( int alias=2; alias<mmm.nIns(); alias++ )
+ if( mmm.in(alias)!=null &&
+ mmm.alias(alias)!=mmm.in(1) &&
+ !_aliases.get(alias) )
+ return alias;
+ return 0;
+ }
+
+ // Slice out given alias
+ private MemMergeNode slice( int alias ) {
+ assert !_aliases.get(alias);
+ // Inputs are the same; MemPhi will sharpen his own inputs. If a
+ // parallel precise Phi already exists, reuse it instead of building a
+ // duplicate for the same Region/alias pair.
+ Node mem = precisePhi(alias);
+
+ BitSet aliases = ((BitSet)_aliases.clone());
+ aliases.set(alias);
+ BulkMemPhiNode bphi = new BulkMemPhiNode(_label,aliases);
+ for( int i=0; i<nIns(); i++ )
+ bphi.addDef(in(i));
+ // Installing bphi creates a new bulk user with a different exclusion
+ // set. Queue the original memory inputs before bphi itself can peep
+ // away and hide those neighbor relationships.
+ for( int i=1; i<bphi.nIns(); i++ )
+ CodeGen.CODE.add(bphi.in(i));
+ // Do not peephole bphi here, as BulkMemPhi can recursively start a
+ // second bulk rewrite before this one has finished. Let the worklist
+ // discover any further splits
+ bphi.setType(TypeMem.BOT);
+ Node bulk = bphi;
+ CodeGen.CODE.add(bulk);
+ CodeGen.CODE.add(mem);
+ return aggregate(bulk,alias,mem);
+ }
+
+ // Build aggregate memory over a bulk Phi, preserving every precise slice
+ // which the bulk has already excluded. The named alias is newly supplied
+ // (or replaces its old slice).
+ MemMergeNode aggregate(Node bulk, int alias, Node precise) {
+ MemMergeNode mmm = new MemMergeNode(bulk);
+ for( int old = _aliases.nextSetBit(0); old >= 0; old = _aliases.nextSetBit(old+1) )
+ { assert old != alias; mmm.alias(old,precisePhi(old));}
+ mmm.alias(alias,precise);
+ assert checkMerge(mmm);
+ return mmm;
+ }
+
+ // Find or make the precise Phi parallel to this bulk Phi at the same merge.
+ private Node precisePhi(int alias) {
+ MemPhiNode mphi = _findPhi(alias);
+ if( mphi!=null ) return mphi;
+
+ mphi = new MemPhiNode("$"+alias,alias);
+ mphi.addDef(region());
+ // Due to cycles, must set before calling peephole
+ mphi.setType(TypeMem.make(alias,Type.BOTTOM));
+ for( int i=1; i<nIns(); i++ )
+ mphi.addDef(CodeGen.CODE.add(preciseInput(in(i),alias)));
+ return CodeGen.CODE.add(mphi);
+ }
+
+ MemPhiNode _findPhi(int alias) {
+ for( Node use : region()._outputs )
+ if( use instanceof MemPhiNode mphi && mphi._alias==alias )
+ return mphi;
+ return null;
+ }
+
+ // Select an alias already split out of a predecessor's bulk memory.
+ private Node preciseInput(Node mem, int alias) {
+ if( mem instanceof MemMergeNode mmm )
+ return mmm.alias(alias);
+ if( mem instanceof BulkMemPhiNode bulk && bulk.isSplit(alias) )
+ return bulk.precisePhi(alias);
+ return mem;
+ }
+
+ // Every alias excluded by a bulk default must be supplied explicitly.
+ static boolean checkMerge(MemMergeNode mmm) {
+ if( !(mmm.in(1) instanceof BulkMemPhiNode bulk) ) return true;
+ for( int alias = bulk._aliases.nextSetBit(0); alias >= 0; alias = bulk._aliases.nextSetBit(alias+1) )
+ if( alias >= mmm.nIns() || mmm.in(alias)==null )
+ return false;
+ return true;
+ }
+
+ @Override public boolean eq(Node n) {
+ return _aliases.equals(((BulkMemPhiNode)n)._aliases) && super.eq(n);
+ }
+
+ @Override int hash() { return _aliases.hashCode(); }
+}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/LoadNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/LoadNode.java
index 94c3e337..76125e39 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/LoadNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/LoadNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.*;
@@ -23,6 +24,25 @@ public class LoadNode extends MemOpNode {
super(loc, name, alias, glb, mem, ptr, off);
}
+ @Override boolean canDrop(MemOpNode other, Node dep) {
+ return super.canDrop(other,dep) && !clobbered(dep);
+ }
+
+ // Check only immediate memory users. Writers clobber memory; Phis and
+ // aggregates might lead to a clobber, so stop rather than search further.
+ private boolean clobbered(Node dep) {
+ Node mem = mem();
+ mem.addDepForwards(dep);
+ for( Node use : mem._outputs ) {
+ if( use==null ) continue;
+ use.addDepForwards(dep);
+ if( use instanceof StoreNode || use instanceof PhiNode ||
+ use instanceof MemMergeNode ||
+ use instanceof NewNode || use instanceof CallNode ) return true;
+ }
+ return false;
+ }
+
// Debugger label
@Override public String label() { return "ld_"+mlabel(); }
@Override
@@ -30,32 +50,32 @@ public class LoadNode extends MemOpNode {
@Override
public Type compute() {
- if( mem()._type instanceof TypeMem mem ) {
- // Update declared forward ref to the actual
- if( _declaredType.isFRef() && mem._t instanceof TypeMemPtr tmp && !tmp.isFRef() )
- _declaredType = tmp;
- // No lifting if ptr might null-check
- if( err()==null )
- return _declaredType.join(mem._t);
- }
- return _declaredType;
+ Type t = MemMergeNode.contents(mem(),_alias,this);
+ // Update declared forward ref to the actual.
+ if( _declaredType.isFRef() && t instanceof TypeMemPtr tmp && !tmp.isFRef() )
+ _declaredType = tmp;
+ return err()==null ? _declaredType.join(t) : _declaredType;
}
@Override
public Node idealize() {
+ if( mem() instanceof MemMergeNode merge ) {
+ setDef(1,CodeGen.CODE.add(merge.alias(_alias)));
+ return this;
+ }
Node ptr = ptr();
Node mem = mem();
// Simple Load-after-Store on same address.
- if( mem instanceof StoreNode st &&
- ptr == st.ptr() && off() == st.off() ) { // Must check same object
+ if( mem() instanceof StoreNode st &&
+ _alias==st._alias && ptr == st.ptr() && off() == st.off() ) { // Must check same object
assert _name.equals(st._name); // Equiv class aliasing is perfect
return extend(st.val());
}
// Simple Load-after-New on same address.
if( mem instanceof ProjNode p && p.in(0) instanceof NewNode nnn &&
- ptr == nnn.proj(1) ) // Must check same object
+ ptr == nnn.proj(0) ) // Must check same object
return zero(nnn); // Load zero from new
// Uplift control to a prior dominating load.
@@ -72,12 +92,16 @@ public class LoadNode extends MemOpNode {
ptr = ro.in(1);
outer:
while( true ) {
+ mem.addDep(this);
switch( mem ) {
+ case MemMergeNode merge:
+ mem = merge.alias(_alias);
+ break;
case StoreNode st:
- if( ptr == st.ptr().addDep(this) && off() == st.off() )
+ if( _alias==st._alias && ptr == st.ptr().addDep(this) && off() == st.off() )
return extend(castRO(st.val())); // Proved equal
// Can we prove unequal? Offsets do not overlap?
- if( !off()._type.join(st.off()._type).isHigh() && // Offsets overlap
+ if( _alias==st._alias && !off()._type.join(st.off()._type).isHigh() && // Offsets overlap
!neverAlias(ptr,st.ptr()) ) // And might alias
break outer; // Cannot tell, stop trying
// Pointers cannot overlap
@@ -95,17 +119,16 @@ public class LoadNode extends MemOpNode {
return zero(nnn1);
if( !(ptr instanceof ProjNode pproj && pproj.in(0) instanceof NewNode) )
break outer; // Cannot tell, ptr not related to New
- mem = nnn1.in(nnn1.findAlias(_alias));// Bypass unrelated New
+ mem = nnn1.mem();// Bypass unrelated New
break;
case StartNode start: break outer;
case CallEndNode cend: break outer; // TODO: Bypass no-alias call
default: throw Utils.TODO();
}
break;
- case MemMergeNode merge: mem = merge.alias(_alias); break;
default:
- throw Utils.TODO();
+ break outer;
}
}
@@ -116,7 +139,7 @@ public class LoadNode extends MemOpNode {
// if( pred ) ptr.x = e0; val = pred ? e0
// else ptr.x = e1; : e1;
// val = ptr.x; ptr.x = val;
- if( mem() instanceof PhiNode memphi && memphi.region()._type == Type.CONTROL && memphi.nIns()== 3 &&
+ if( mem() instanceof MemPhiNode memphi && memphi.region()._type == Type.CONTROL && memphi.nIns()== 3 &&
// Offset can be hoisted
off() instanceof ConstantNode &&
// Pointer can be hoisted
@@ -145,7 +168,9 @@ public class LoadNode extends MemOpNode {
private Node ld( int idx ) {
Node mem = mem(), ptr = ptr();
- return new LoadNode(_loc,_name,_alias,_declaredType,mem.in(idx),ptr instanceof PhiNode && ptr.in(0)==mem.in(0) ? ptr.in(idx) : ptr, off()).peephole();
+ LoadNode ld = new LoadNode(_loc,_name,_alias,_declaredType,mem.in(idx),ptr instanceof PhiNode && ptr.in(0)==mem.in(0) ? ptr.in(idx) : ptr,off());
+ ld.setDef(0,in(0));
+ return ld.peephole();
}
private static boolean neverAlias( Node ptr1, Node ptr2 ) {
@@ -178,9 +203,9 @@ public class LoadNode extends MemOpNode {
private boolean profit(PhiNode phi, int idx) {
Node px = phi.in(idx);
if( px==null ) return false;
- if( px._type instanceof TypeMem mem && mem._t.isHighOrConst() ) return true;
- if( px instanceof StoreNode st1 && ptr()==st1.ptr() && off()==st1.off() ) return true;
px.addDep(this);
+ if( px._type instanceof TypeMem mem && mem._t.isHighOrConst() ) return true;
+ if( px instanceof StoreNode st1 && _alias==st1._alias && ptr()==st1.ptr() && off()==st1.off() ) return true;
return false;
}
@@ -205,4 +230,5 @@ public class LoadNode extends MemOpNode {
Node shl = new ShlNode(null,val,shf.keep()).peephole();
return new SarNode(null,shl,shf.unkeep());
}
+
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java
index adf83aab..31c76a93 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java
@@ -5,151 +5,85 @@ import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.type.*;
import java.util.*;
-/**
- * Memory Merge - a merge of many aliases into a "fat memory". All aliases
- * are here, but most will be lazy - take the default fat memory.
+/** A default slice and explicit alias overrides. A null default means partial
+ * memory: absent aliases are not covered, rather than having unknown contents.
*/
public class MemMergeNode extends Node {
+ public MemMergeNode(MemMergeNode mem) { super(mem); }
- /*
- * In-Progress means this is being used by the Parser to track memory
- * aliases. No optimizations are allowed. When no longer "in progress"
- * normal peeps work.
- */
- public final boolean _inProgress;
-
- public MemMergeNode( boolean inProgress) { _type = TypeMem.BOT; _inProgress = inProgress; }
- public MemMergeNode(MemMergeNode mem) { super(mem); _inProgress = false; }
-
-
- // If being used by a Scope, this is "in progress" from the Parser.
- // Otherwise, it's a memory merge
- boolean inProgress() { return _inProgress; }
-
- @Override public String label() { return "ALLMEM"; }
- @Override public boolean isMem() { return true; }
-
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- sb.append(_inProgress ? "Merge[" : "MEM[ ");
- for( int j=2; j<nIns(); j++ ) {
- sb.append(j);
- sb.append(":");
- Node n = in(j);
- while( n instanceof ScopeNode loop ) {
- sb.append("Lazy_");
- n = loop.mem().alias(j);
- }
- if( n==null ) sb.append("___ ");
- else n._print0(sb, visited).append(" ");
- }
- sb.setLength(sb.length()-1);
- return sb.append("]");
- }
-
- // Make a memory merge: no longer a Scope really, tracking memory state but
- // not related to the parser in any way.
- public Node merge() {
- // Force default memory to not be lazy
- MemMergeNode merge = new MemMergeNode(false);
- for( Node n : _inputs )
- merge.addDef(n);
- merge._mem(1,null);
- return merge.peephole();
- }
-
-
- @Override public Type compute() { return TypeMem.BOT; }
-
- @Override public Node idealize() {
- if( inProgress() ) return null;
-
- // If not merging any memory (all memory is just the default)
- if( nIns()==2 )
- return in(1); // Become default memory
-
- return null;
+ public MemMergeNode(Node bulk) { super(null,bulk); _type = TypeMem.BOT; }
+ public MemMergeNode(Node bulk, int alias, Node precise) {
+ this(bulk);
+ alias(alias,precise);
}
- public Node in( Var v ) { return in(v._idx); }
-
- public Node alias( int alias ) {
- return in(alias<nIns() && in(alias)!=null ? alias : 1);
+ public Node alias(int alias) {
+ Node n = alias < nIns() ? in(alias) : null;
+ assert n!=null || !(in(1) instanceof BulkMemPhiNode bulk) || !bulk.isSplit(alias);
+ n = n==null ? in(1) : n;
+ assert n!=null : "Alias not covered: "+alias;
+ return n;
}
- Node alias( int alias, Node st ) {
- while( alias >= nIns() ) addDef(null);
- return setDef(alias,st);
+ public void alias(int alias, Node mem) {
+ assert alias > 1;
+ while( nIns()<=alias ) addDef(null);
+ setDef(alias,mem);
}
- // Read or update from memory.
- // A shared implementation allows us to create lazy phis both during
- // lookups and updates; the lazy phi creation is part of chapter 8.
- Node _mem( int alias, Node st ) {
- // Memory projections are made lazily; if one does not exist
- // then it must be START.proj(1)
- Node old = alias(alias);
- if( old instanceof ScopeNode loop ) {
- MemMergeNode loopmem = loop.mem();
- Node memdef = loopmem.alias(alias);
- // Lazy phi!
- old = memdef instanceof PhiNode phi && loop.ctrl()==phi.region()
- // Loop already has a real Phi, use it
- ? memdef
- // Set real Phi in the loop head
- // The phi takes its one input (no backedge yet) from a recursive
- // lookup, which might have insert a Phi in every loop nest.
- : loopmem.alias(alias, new PhiNode(Parser.memName(alias), TypeMem.BOT,loop.ctrl(),loopmem._mem(alias,null),null).peephole() );
- alias(alias,old);
+ // Scalar contents of one alias. New zeroes the allocated fields;
+ // join it with the incoming contents for all previously allocated objects.
+ // Phis use their cached types, so this query does not recurse around loops.
+ static Type contents(Node mem, int alias, Node dep) {
+ mem.addDep(dep);
+ if( mem instanceof MemMergeNode merge )
+ return contents(merge.alias(alias),alias,dep);
+ if( mem instanceof ProjNode proj && proj.in(0) instanceof NewNode nnn ) {
+ assert proj._idx==1 && nnn.field(alias)!=null;
+ nnn.addDep(dep);
+ return contents(nnn.mem(),alias,dep).meet(nnn.field(alias)._type.makeZero());
}
- // Memory projections are made lazily; expand as needed
- return st==null ? old : alias(alias,st); // Not lazy, so this is the answer
- }
-
-
- void _merge( MemMergeNode that, RegionNode r) {
- int len = Math.max(nIns(),that.nIns());
- for( int i = 2; i < len; i++)
- if( alias(i) != that.alias(i) ) { // No need for redundant Phis
- // If we are in lazy phi mode we need to a lookup
- // by alias as it will trigger a phi creation
- Node lhs = this._mem(i,null);
- Node rhs = that._mem(i,null);
- alias(i, new PhiNode(Parser.memName(i), lhs._type.glb().meet(rhs._type.glb()), r, lhs, rhs).peephole());
- }
+ // Function parameters and call results may contain arbitrary heap values.
+ // Their bulk memory types do not imply empty or zero-filled storage.
+ if( mem._type==Type.TOP || mem._type==TypeMem.TOP ) return Type.TOP;
+ if( mem._type instanceof TypeMem mt && mt._alias==alias ) return mt._t;
+ return Type.BOTTOM;
}
- // Fill in the backedge of any inserted Phis
- void _endLoopMem( ScopeNode scope, MemMergeNode back, MemMergeNode exit ) {
- Node exit_def = exit.alias(1);
- for( int i=1; i<nIns(); i++ ) {
- if( in(i) instanceof PhiNode phi && phi.region()==scope.ctrl() ) {
- assert phi.in(2)==null;
- phi.setDef(2,back.in(i)==scope ? phi : back.in(i)); // Fill backedge
+ @Override public String label() { return "MemMerge"; }
+ @Override public boolean isMem() { return true; }
+ @Override public Type compute() { return TypeMem.BOT; }
+ @Override StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ sb.append("MEM[");
+ for( int i=1; i<nIns(); i++ )
+ if( in(i)!=null ) {
+ sb.append(i==1 ? "default:" : i+":");
+ in(i)._print0(sb,visited).append(" ");
}
- if( exit_def == scope ) // Replace a lazy-phi on the exit path also
- exit.alias(i,in(i));
- }
+ return sb.append("]");
}
- // Now one-time do a useless-phi removal
- void _useless( ) {
+ @Override public Node idealize() {
+ boolean progress=false, allDefault=true;
for( int i=2; i<nIns(); i++ ) {
- if( in(i) instanceof PhiNode phi ) {
- // Do an eager useless-phi removal
- Node in = phi.peephole();
- CodeGen.CODE.addAll(phi._outputs);
- phi.moveDepsToWorklist();
- if( in != phi ) {
- if( !phi.iskeep() ) // Keeping phi around for parser elsewhere
- phi.subsume(in);
- setDef(i,in); // Set the update back into Scope
- }
+ if( in(i)!=null && in(i)==in(1) ) { setDef(i,null); progress=true; }
+ if( in(i) instanceof MemMergeNode mem ) {
+ setDef(i,CodeGen.CODE.add(mem.alias(i)));
+ progress=true;
}
+ if( in(i)!=null ) allDefault=false;
}
- }
-
- @Override boolean eq( Node n ) {
- return this==n || !_inProgress;
+ if( allDefault && in(1)!=null ) return in(1);
+
+ if( in(1) instanceof MemMergeNode mem ) {
+ for( int i=2; i<mem.nIns(); i++ )
+ if( mem.in(i)!=null && (i>=nIns() || in(i)==null) )
+ alias(i,mem.in(i));
+ CodeGen.CODE.add(mem.in(1));
+ setDef(1,mem.in(1));
+ return this;
+ }
+ assert BulkMemPhiNode.checkMerge(this);
+ return progress ? this : null;
}
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/MemOpNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
index cba4dc1b..41760124 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
@@ -6,6 +6,7 @@ import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeMemPtr;
import java.lang.StringBuilder;
import java.util.BitSet;
+import java.util.ArrayList;
/**
* Convenience common base for Load and Store.
@@ -51,6 +52,13 @@ public abstract class MemOpNode extends Node {
public StringBuilder _printMach( StringBuilder sb, BitSet visited ) { throw Utils.TODO(); }
+ // Extra conditions for factoring matching memory operations through a Phi.
+ // The caller has already checked the opcode, input shape and control.
+ boolean canDrop(MemOpNode other, Node dep) {
+ ptr().addDep(dep);
+ return _alias==other._alias && _declaredType==other._declaredType && err()==null;
+ }
+
@Override
boolean eq(Node n) {
MemOpNode mem = (MemOpNode)n; // Invariant
@@ -71,4 +79,29 @@ public abstract class MemOpNode extends Node {
return null;
return Parser.error( "Might be null accessing '" + _name + "'",_loc);
}
+ // Memory writers which must follow this read. A packaging node is not a
+ // write, but can hide New's input, so follow this alias through aggregates.
+ public ArrayList<Node> antiDeps() {
+ ArrayList<Node> deps = new ArrayList<>();
+ antiDeps(mem(),deps,new BitSet());
+ return deps;
+ }
+
+ private void antiDeps(Node mem, ArrayList<Node> deps, BitSet visit) {
+ if( visit.get(mem._nid) ) return;
+ visit.set(mem._nid);
+ for( Node use : mem._outputs ) {
+ if( use instanceof MemMergeNode merge ) {
+ Node slice = _alias<merge.nIns() ? merge.in(_alias) : null;
+ if( (slice==mem) || (slice==null && merge.in(1)==mem) )
+ antiDeps(merge,deps,visit);
+ } else if( use instanceof CallNode ||
+ (use instanceof MemOpNode st && st.isMem() && st._alias==_alias) ||
+ (use instanceof NewNode nnn && nnn.field(_alias)!=null) ||
+ (use instanceof MemPhiNode phi && phi._alias==_alias) ||
+ (use instanceof BulkMemPhiNode phi && !phi.isSplit(_alias)) ) {
+ if( !deps.contains(use) ) deps.add(use);
+ }
+ }
+ }
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/MemPhiNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/MemPhiNode.java
new file mode 100644
index 00000000..26572ce1
--- /dev/null
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/MemPhiNode.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
+
+/** A Phi for one field alias, parallel to a bulk Phi at the same Region. */
+public class MemPhiNode extends PhiNode {
+ public final int _alias;
+
+ public MemPhiNode(String label, int alias, Node... inputs) {
+ super(label,TypeMem.make(alias,Type.BOTTOM),inputs);
+ assert alias > 1;
+ _alias = alias;
+ }
+
+ public MemPhiNode(MemPhiNode phi) { super(phi); _alias = phi._alias; }
+
+ @Override public String label() { return "MemPhi_"+_alias; }
+
+ @Override public Type compute() {
+ assert BulkMemPhiNode.checkMem(this,_alias);
+ if( !(region() instanceof RegionNode r) )
+ return region()._type==Type.XCONTROL ? TypeMem.TOP : _type;
+ if( r.inProgress() ) return _declaredType;
+ Type t = Type.TOP;
+ for( int i=1; i<nIns(); i++ )
+ if( r.in(i).addDep(this)._type!=Type.XCONTROL )
+ t = t.meet(MemMergeNode.contents(in(i),_alias,this));
+ return TypeMem.make(_alias,t);
+ }
+
+ @Override public Node idealize() {
+ for( int i=1; i<nIns(); i++ )
+ if( in(i) instanceof MemMergeNode mem ) {
+ setDef(i,CodeGen.CODE.add(mem.alias(_alias)));
+ return this;
+ }
+ return super.idealize();
+ }
+
+ @Override boolean eq(Node n) { return _alias==((MemPhiNode)n)._alias && super.eq(n); }
+ @Override int hash() { return _alias; }
+}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/NewNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/NewNode.java
index abce1601..b9c9de71 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/NewNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/NewNode.java
@@ -1,37 +1,37 @@
package com.seaofnodes.simple.node;
import com.seaofnodes.simple.type.*;
-import com.seaofnodes.simple.Utils;
import java.util.BitSet;
-/**
- * Allocation! Allocate a chunk of memory, and pre-zero it.
- * The inputs include control and size, and ALL aliases being set.
- * The output is large tuple, one for every alias plus the created pointer.
- * New is expected to be followed by projections for every alias.
+/** Allocate a zeroed object. Inputs {ctrl, $mem, size};
+ * results {ptr, $mem}.
+ * The memory input and result cover only the aliases in the allocated struct.
*/
public class NewNode extends Node implements MultiNode {
public final TypeMemPtr _ptr;
- public final int _len;
public NewNode(TypeMemPtr ptr, Node... nodes) {
super(nodes);
assert !ptr.nullable();
_ptr = ptr;
- _len = ptr._obj._fields.length;
- // Control in slot 0
- assert nodes[0]._type==Type.CONTROL || nodes[0]._type == Type.XCONTROL;
- // Malloc-length in slot 1
- assert nodes[1]._type instanceof TypeInteger || nodes[1]._type==Type.NIL;
- for( int i=0; i<_len; i++ )
- // Memory slices for all fields.
- assert nodes[2+i]._type.isa(TypeMem.BOT);
+ assert nodes.length==3;
+ assert nodes[0]._type==Type.CONTROL || nodes[0]._type==Type.XCONTROL;
+ assert nodes[1]._type instanceof TypeMem;
+ assert nodes[2]._type instanceof TypeInteger || nodes[2]._type==Type.NIL;
}
- public NewNode(NewNode nnn) { super(nnn); _ptr = nnn._ptr; _len = nnn._len; }
+ public Node mem() { return in(1); }
+ public Node size() { return in(2); }
+
+ public Field field(int alias) {
+ for( Field f : _ptr._obj._fields )
+ if( f._alias==alias ) return f;
+ return null;
+ }
+
+ public NewNode(NewNode nnn) { super(nnn); _ptr = nnn._ptr; }
- public Node mem (int idx) { return in(idx+2); }
@Override public String label() { return "new_"+(_ptr._obj.isAry() ? "ary_"+_ptr._obj._fields[1]._type.str() : _ptr._obj.str()); }
@Override
@@ -40,32 +40,10 @@ public class NewNode extends Node implements MultiNode {
return sb.append(_ptr._obj.str());
}
- int findAlias(int alias) {
- int idx = _ptr._obj.findAlias(alias);
- assert idx!= -1; // Error, caller should be calling
- return idx+2; // Skip ctrl, size
- }
-
- // 0 - ctrl
- // 1 - byte size
- // 2-len+2 - aliases, one per field
- // len+2 - 2*len+2 - initial values, one per field
- public Node size() { return in(1); }
@Override
public TypeTuple compute() {
- Field[] fs = _ptr._obj._fields;
- Type[] ts = new Type[fs.length+2];
- ts[0] = Type.CONTROL;
- ts[1] = _ptr;
- for( int i=0; i<fs.length; i++ ) {
- Type mt = in(i+2)._type;
- TypeMem mem = mt==Type.TOP ? TypeMem.TOP : (TypeMem)mt;
- Type tfld = mem._t.meet(mem._t.makeZero());
- Type tfld2 = tfld.join(fs[i]._type);
- ts[i+2] = TypeMem.make(fs[i]._alias,tfld2);
- }
- return TypeTuple.make(ts);
+ return TypeTuple.make(_ptr,TypeMem.BOT);
}
@Override
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter20/src/main/java/com/seaofnodes/simple/node/Node.java
index 65dbbd76..aecac75c 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -230,6 +230,7 @@ public abstract class Node implements Cloneable {
// Error is 'use' does not exist; ok for 'use' to be null.
protected boolean delUse( Node use ) {
_outputs.del(_outputs.find(use));
+ moveDepsToWorklist(); // User-count and anti-dependence queries can now change.
return _outputs.isEmpty();
}
@@ -288,6 +289,7 @@ public abstract class Node implements Cloneable {
n.unlock();
int idx = n._inputs.find(this);
n._inputs.set(idx,nnn);
+ n.moveDepsToWorklist(); // Rewiring can change a dependent query without changing type.
nnn.addUse(n);
CODE.addAll(n._outputs);
}
@@ -539,6 +541,14 @@ public abstract class Node implements Cloneable {
* being added must benefit from this node being peepholed.
*/
Node addDep( Node dep ) {
+ return addDep(dep,false);
+ }
+
+ // A bulk Phi inspects its users' partitions. Def-to-use propagation does
+ // not revisit that producer when an immediate user's partition changes.
+ Node addDepForwards(Node dep) { return addDep(dep,true); }
+
+ private Node addDep(Node dep, boolean forwards) {
// Running peepholes during the big assert cannot have side effects
// like adding dependencies.
if( CODE._midAssert ) return this;
@@ -547,8 +557,8 @@ public abstract class Node implements Cloneable {
if( dep == null ) return this;
if( _deps==null ) _deps = new Ary<>(Node.class);
if( _deps .find(dep) != -1 ) return this; // Already on list
- if( _inputs .find(dep) != -1 ) return this; // No need for deps on immediate neighbors
- if( _outputs.find(dep) != -1 ) return this;
+ if( !forwards && _inputs .find(dep) != -1 ) return this; // No need for deps on immediate neighbors
+ if( !forwards && _outputs.find(dep) != -1 ) return this;
_deps.add(dep);
return this;
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index 944e7ed6..52298345 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -24,6 +24,10 @@ public class PhiNode extends Node {
addDef(sample);
}
+ public static PhiNode make(String label, Type type, Node... inputs) {
+ return type instanceof TypeMem ? new BulkMemPhiNode(label,inputs) : new PhiNode(label,type,inputs);
+ }
+
@Override public String label() { return "Phi_"+MemOpNode.mlabel(_label); }
@Override
@@ -77,32 +81,9 @@ public class PhiNode extends Node {
if( r.in(i)._type == Type.XCONTROL )
return null;
- // Pull "down" a common data op. One less op in the world. One more
- // Phi, but Phis do not make code.
- // Phi(op(A,B),op(Q,R),op(X,Y)) becomes
- // op(Phi(A,Q,X), Phi(B,R,Y)).
- Node op = in(1);
- if( !isMem() && op.nIns()==3 && op.in(0)==null && same_op() ) {
- assert !(op instanceof CFGNode);
- Node[] lhss = new Node[nIns()];
- Node[] rhss = new Node[nIns()];
- lhss[0] = rhss[0] = in(0); // Set Region
- for( int i=1; i<nIns(); i++ ) {
- lhss[i] = in(i).in(1);
- rhss[i] = in(i).in(2);
- }
- Node phi_lhs = new PhiNode(_label, _declaredType,lhss).peephole();
- Node phi_rhs = new PhiNode(_label, _declaredType,rhss).peephole();
- Node down = op.copy(phi_lhs,phi_rhs);
- // Test not running backwards, which can happen for e.g. And's
- if( down.compute().isa(compute()) )
- return down;
- in(1).in(1).addDep(this);
- in(1).in(2).addDep(this);
- in(2).in(1).addDep(this);
- in(2).in(2).addDep(this);
- down.kill();
- }
+ // 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;
// If merging Phi(N, cast(N)) - we are losing the cast JOIN effects, so just remove.
if( nIns()==3 ) {
@@ -134,12 +115,75 @@ public class PhiNode extends Node {
}
private boolean same_op() {
- for( int i=2; i<nIns(); i++ )
- if( in(1).getClass() != in(i).getClass() )
- return false;
+ Node op = in(1);
+ if( op instanceof CFGNode || op instanceof ConstantNode || op instanceof PhiNode ||
+ op instanceof ProjNode || op instanceof NewNode || op instanceof ScopeNode || op instanceof MemMergeNode ) return false;
+ // A bulk Phi must split its aliases before factoring precise Stores.
+ if( this instanceof BulkMemPhiNode ) return false;
+ // Bulk splitting currently identifies parallel slices by Region/alias.
+ // Factoring can introduce another slice at a different memory point;
+ // wait until that Region's bulk partitioning has finished.
+ if( op instanceof MemOpNode )
+ for( Node use : region()._outputs )
+ if( use instanceof BulkMemPhiNode ) {
+ use.addDepForwards(this);
+ return false;
+ }
+ Node busy = null;
+ for( int i=1; i<nIns(); i++ ) {
+ Node n = in(i);
+ if( region().in(i).addDep(this)._type==Type.XCONTROL ||
+ op.getClass()!=n.getClass() || n.nIns()!=op.nIns() || n.in(0)!=null || !op.eq(n) ) return false;
+ if( n instanceof MemOpNode mem && !mem.canDrop((MemOpNode)op,this) ) return false;
+ // Moving a Store must remove the old effect, not duplicate it.
+ // Allow one shared arm: the other arms disappear into the single
+ // factored operation, usually reducing the total operation count.
+ n.addDepForwards(this);
+ if( n.nOuts()>1 ) {
+ for( Node use : n._outputs )
+ if( use!=null && use!=this ) use.addDepForwards(this);
+ if( n instanceof StoreNode || busy!=null ) return false;
+ busy=n;
+ }
+ for( int j=1; j<n.nIns(); j++ )
+ if( n.in(j) instanceof ScopeNode || ((n.in(j)==null) != (op.in(j)==null)) ) return false;
+ }
return true;
}
+ private Node drop_same_op() {
+ Node op = in(1), cp = op.copyEmpty();
+ cp._type = null;
+ cp.addDef(null);
+ for( int j=1; j<op.nIns(); j++ ) {
+ Node x = op.in(j);
+ boolean different = false;
+ for( int i=2; i<nIns(); i++ )
+ if( in(i).in(j)!=x ) different=true;
+ if( different ) {
+ Node[] ins = new Node[nIns()];
+ ins[0] = region();
+ Type t = Type.TOP;
+ for( int i=1; i<nIns(); i++ ) {
+ ins[i] = in(i).in(j);
+ t = t.meet(ins[i]._type);
+ }
+ PhiNode phi = j==1 && op instanceof MemOpNode mem
+ ? new MemPhiNode(_label,mem._alias,ins)
+ : PhiNode.make(_label,t.glb(),ins);
+ x = phi.peephole();
+ }
+ cp.addDef(x);
+ }
+ // Factoring must not widen the result (e.g. correlated And operands).
+ if( cp.compute().isa(compute()) ) return cp;
+ for( int i=1; i<nIns(); i++ )
+ for( int j=1; j<in(i).nIns(); j++ )
+ if( in(i).in(j)!=null ) in(i).in(j).addDep(this);
+ cp.kill();
+ return null;
+ }
+
/**
* If only single unique input, return it
*/
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/RegionNode.java
index 166ef4bf..f28952e0 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/RegionNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -47,7 +47,7 @@ public class RegionNode extends CFGNode {
if( !hasPhi() && // No Phi users, just a control user
in(1) instanceof CProjNode p1 &&
in(2) instanceof CProjNode p2 &&
- p1.in(0).addDep(this)==p2.in(0).addDep(this) &&
+ p1.addDepForwards(this).in(0)==p2.addDepForwards(this).in(0) &&
p1.in(0) instanceof IfNode iff ) {
// Replace with the iff.ctrl directly
if( nIns()==3 ) return iff.ctrl();
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
index b767b69b..7858671c 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -6,15 +6,7 @@ import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.*;
import java.util.BitSet;
-/**
- * The Return node has two inputs. The first input is a control node and the
- * second is the data node that supplies the return value.
- * <p>
- * In this presentation, Return functions as a Stop node, since multiple <code>return</code> statements are not possible.
- * The Stop node will be introduced in Chapter 6 when we implement <code>if</code> statements.
- * <p>
- * The Return's output is the value from the data node.
- */
+/** Keeps all preceding memory effects and the return value alive: {ctrl, $mem, value, rpc}. */
public class ReturnNode extends CFGNode {
public FunNode _fun;
@@ -49,7 +41,7 @@ public class ReturnNode extends CFGNode {
public Type compute() {
if( inProgress () ) return TypeTuple.RET; // In progress
// Inlining can delete the entry while callers still use this return.
- return TypeTuple.make(ctrl()._type,mem()._type,expr()._type);
+ return TypeTuple.make(ctrl()._type,TypeMem.BOT,expr()._type);
}
@Override public Node idealize() {
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/ScopeNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
index 13ddc53a..406be11f 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
@@ -7,9 +7,9 @@ import java.util.*;
/**
* The Scope node is purely a parser helper - it tracks names to nodes with a
- * stack of variables.
+ * stack of variable records.
*/
-public class ScopeNode extends MemMergeNode {
+public class ScopeNode extends Node {
/**
* The control is a name that binds to the currently active control
@@ -19,6 +19,11 @@ public class ScopeNode extends MemMergeNode {
public static final String ARG0 = "arg";
public static final String MEM0 = "$mem";
+ public Node in(Var v) { return in(v._idx); }
+
+ @Override public Type compute() { return Type.BOTTOM; }
+ @Override public Node idealize() { return null; }
+
// All active/live variables in all nested scopes, all run together
public final Ary<Var> _vars;
@@ -34,7 +39,7 @@ public class ScopeNode extends MemMergeNode {
// A new ScopeNode
public ScopeNode() {
- super(true);
+ _type = Type.BOTTOM;
_vars = new Ary<>(Var .class);
_lexSize= new Ary<>(Integer.class);
_kinds = new Ary<>(Kind .class);
@@ -70,7 +75,7 @@ public class ScopeNode extends MemMergeNode {
public Node ctrl() { return in(0); }
- public MemMergeNode mem() { return (MemMergeNode)in(1); }
+ public Node mem() { return in(lookup(MEM0)); }
/**
* The ctrl of a ScopeNode is always bound to the currently active
@@ -83,7 +88,7 @@ public class ScopeNode extends MemMergeNode {
* @return Node that was bound
*/
public <N extends Node> N ctrl(N n) { return setDef(0,n); }
- public Node mem(Node n) { return setDef(1,n); }
+ public Node mem(Node n) { update(MEM0,n); return n; }
public void push(Kind kind) {
assert _lexSize._len==_kinds._len;
@@ -166,12 +171,6 @@ public class ScopeNode extends MemMergeNode {
return true;
}
- // Read from memory
- public Node mem( int alias ) { return mem()._mem(alias,null); }
- // Write to memory
- public void mem( int alias, Node st ) { mem()._mem(alias,st); }
-
-
/**
* Lookup a name in all scopes starting from most deeply nested.
*
@@ -207,7 +206,7 @@ public class ScopeNode extends MemMergeNode {
// Set real Phi in the loop head
// The phi takes its one input (no backedge yet) from a recursive
// lookup, which might have insert a Phi in every loop nest.
- : loop.setDef(v._idx,new PhiNode(v._name, v.lazyGLB(), loop.ctrl(), loop.in(loop.update(v,null)._idx),null).peephole());
+ : loop.setDef(v._idx,PhiNode.make(v._name, v.lazyGLB(), loop.ctrl(), loop.in(loop.update(v,null)._idx),null).peephole());
setDef(v._idx,old);
}
assert !v._final || st==null;
@@ -246,20 +245,8 @@ public class ScopeNode extends MemMergeNode {
n.keep();
dup.addDef(ctrl()); // Control input is just copied
- // Memory input is a shallow copy
- MemMergeNode memdup = new MemMergeNode(true), mem = mem();
- memdup.addDef(null);
- memdup.addDef(loop ? this : mem.in(1));
- for( int i=2; i<mem.nIns(); i++ )
- // For lazy phis on loops we use a sentinel
- // that will trigger phi creation on update
- memdup.addDef(loop ? this : mem.in(i));
- dup.addDef(memdup);
-
- // Copy of other inputs
- for( int i=2; i<nIns(); i++ )
- // For lazy phis on loops we use a sentinel
- // that will trigger phi creation on update
+ // Memory is one binding, with the same lazy loop Phi as scalar names.
+ for( int i=1; i<nIns(); i++ )
dup.addDef(loop && !_vars.at(i)._final ? this : in(i));
return dup;
}
@@ -274,22 +261,21 @@ public class ScopeNode extends MemMergeNode {
*/
public RegionNode mergeScopes(ScopeNode that, Parser.Lexer loc) {
RegionNode r = ctrl(new RegionNode(loc,null,ctrl(), that.ctrl()).keep());
- mem()._merge(that.mem(),r);
- this ._merge(that ,r);
+ _merge(that,r);
that.kill(); // Kill merged scope
CodeGen.CODE.add(r);
return r.unkeep();
}
private void _merge(ScopeNode that, RegionNode r) {
- for( int i = 2; i < nIns(); i++)
+ for( int i = 1; i < nIns(); i++)
if( in(i) != that.in(i) ) { // No need for redundant Phis
// If we are in lazy phi mode we need to a lookup
// by name as it will trigger a phi creation
Var v = _vars.at(i);
Node lhs = this.in(this.update(v,null));
Node rhs = that.in(that.update(v,null));
- setDef(i, new PhiNode(v._name, v.type(), r, lhs, rhs).peephole());
+ setDef(i, PhiNode.make(v._name, v.type(), r, lhs, rhs).peephole());
}
}
@@ -314,21 +300,15 @@ public class ScopeNode extends MemMergeNode {
assert ctrl instanceof LoopNode loop && loop.inProgress();
ctrl.setDef(2,back.ctrl());
- mem()._endLoopMem( this, back.mem(), exit.mem() );
- this ._endLoop ( this, back , exit );
+ _endLoop(this,back,exit);
back.kill(); // Loop backedge is dead
// Now one-time do a useless-phi removal
- mem()._useless();
- this ._useless();
-
- // The exit mem's lazy default value had been the loop top,
- // now it goes back to predating the loop.
- exit.mem().setDef(1,mem().in(1));
+ _useless();
}
// Fill in the backedge of any inserted Phis
void _endLoop( ScopeNode scope, Node back, Node exit ) {
- for( int i=2; i<nIns(); i++ ) {
+ for( int i=1; i<nIns(); i++ ) {
if( _vars.at(i)._final ) continue; // Final vars did not get modified in the loop
if( _vars.at(i).type().isHighOrConst() ) continue; // Cannot lift higher than a constant, so no Phi
if( back.in(i) != scope ) {
@@ -342,6 +322,23 @@ public class ScopeNode extends MemMergeNode {
}
+ // Now one-time do a useless-phi removal
+ void _useless( ) {
+ for( int i=1; i<nIns(); i++ ) {
+ if( in(i) instanceof PhiNode phi ) {
+ // Do an eager useless-phi removal
+ Node in = phi.peephole();
+ CodeGen.CODE.addAll(phi._outputs);
+ phi.moveDepsToWorklist();
+ if( in != phi ) {
+ if( !phi.iskeep() ) // Keeping phi around for parser elsewhere
+ phi.subsume(in);
+ setDef(i,in); // Set the update back into Scope
+ }
+ }
+ }
+ }
+
// Up-casting: using the results of an If to improve a value.
// E.g. "if( ptr ) ptr.field;" is legal because ptr is known not-null.
public void addGuards( Node ctrl, Node pred, boolean invert ) {
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/StartNode.java
index 22a1750d..2783ab93 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/StartNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -3,18 +3,11 @@ package com.seaofnodes.simple.node;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.*;
-import java.util.Arrays;
import java.util.BitSet;
import java.util.HashSet;
import static com.seaofnodes.simple.Utils.TODO;
-/**
- * The Start node represents the start of the function.
- *
- * Start initially has 1 input (arg) from outside and the initial control.
- * In ch10 we also add mem aliases as structs get defined; each field in struct
- * adds a distinct alias to Start's tuple.
- */
+/** Start supplies control, whole memory, and the argument, in that order. */
public class StartNode extends LoopNode implements MultiNode {
final Type _arg;
@@ -45,7 +38,7 @@ public class StartNode extends LoopNode implements MultiNode {
@Override public TypeTuple compute() {
- return TypeTuple.make(Type.CONTROL,TypeMem.TOP,_arg);
+ return TypeTuple.make(Type.CONTROL,TypeMem.BOT,_arg);
}
@Override public Node idealize() { return null; }
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/StoreNode.java b/chapter20/src/main/java/com/seaofnodes/simple/node/StoreNode.java
index cb374c58..4833df39 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/StoreNode.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/StoreNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.codegen.*;
import com.seaofnodes.simple.*;
import com.seaofnodes.simple.type.*;
import java.util.BitSet;
@@ -25,6 +26,10 @@ public class StoreNode extends MemOpNode {
}
// Debugger label
+ @Override boolean canDrop(MemOpNode other, Node dep) {
+ return super.canDrop(other,dep) && _init==((StoreNode)other)._init;
+ }
+
@Override public String label() { return "st_"+mlabel(); }
@Override public boolean isMem() { return true; }
@@ -38,28 +43,25 @@ public class StoreNode extends MemOpNode {
@Override
public Type compute() {
Type val = val()._type;
- TypeMem mem = (TypeMem)mem()._type; // Invariant
- if( mem == TypeMem.TOP ) return TypeMem.TOP;
- Type t = Type.BOTTOM; // No idea on field contents
- // Same alias, lift val to the declared type and then meet into other fields
- if( mem._alias == _alias ) {
- // Update declared forward ref to the actual
- if( _declaredType.isFRef() && val instanceof TypeMemPtr tmp && !tmp.isFRef() )
- _declaredType = tmp;
- val = val.join(_declaredType);
- t = val.meet(mem._t);
- }
+ if( _declaredType.isFRef() && val instanceof TypeMemPtr tmp && !tmp.isFRef() )
+ _declaredType = tmp;
+ Type t = val.join(_declaredType).meet(MemMergeNode.contents(mem(),_alias,this));
return TypeMem.make(_alias,t);
}
@Override
public Node idealize() {
+ if( mem() instanceof MemMergeNode merge ) {
+ setDef(1,CodeGen.CODE.add(merge.alias(_alias)));
+ return this;
+ }
+
// Simple store-after-store on same address. Should pick up the
// required init-store being stomped by a first user store.
if( mem() instanceof StoreNode st &&
- ptr()==st.ptr() && // Must check same object
- off()==st.off() && // And same offset (could be "same alias" but this handles arrays to same index)
+ ptr()==st.ptr() && _alias==st._alias && // Must check same object
+ off()==st.off() && // And same offset
ptr()._type instanceof TypeMemPtr && // No bother if weird dead pointers
// Must have exactly one use of "this" or you get weird
// non-serializable memory effects in the worse case.
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/MemOpARM.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/MemOpARM.java
index adffa501..e874ce15 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/MemOpARM.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/MemOpARM.java
@@ -34,6 +34,8 @@ public class MemOpARM extends MemOpNode implements MachNode {
@Override public StringBuilder _printMach(StringBuilder sb, BitSet visited) { return sb.append(".").append(_name); }
+ @Override public boolean isMem() { return _type instanceof TypeMem; }
+
@Override public String label() { return op(); }
@Override public Type compute() { throw Utils.TODO(); }
@Override public Node idealize() { throw Utils.TODO(); }
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/NewARM.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/NewARM.java
index bd88fa83..c145f348 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/NewARM.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/NewARM.java
@@ -12,9 +12,9 @@ public class NewARM extends NewNode implements MachNode {
// A pre-zeroed chunk of memory.
NewARM(NewNode nnn) { super(nnn); }
// Size and pointer result in standard calling convention; null for all the
- // memory aliases edges
- @Override public RegMask regmap(int i) { return i == 1 ? arm. X0_MASK : null; }
- @Override public RegMask outregmap(int i) { return i == 1 ? arm. X0_MASK : null; }
+ // memory and ordering edges
+ @Override public RegMask regmap(int i) { return i == 2 ? arm. X0_MASK : null; }
+ @Override public RegMask outregmap(int i) { return i == 0 ? arm. X0_MASK : null; }
@Override public RegMask outregmap() { return null; }
// Encoding is appended into the byte array; size is returned
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/StoreARM.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/StoreARM.java
index 49045b28..d78fdcad 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/StoreARM.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/StoreARM.java
@@ -34,7 +34,7 @@ public class StoreARM extends MemOpARM {
if( i==2 ) return arm.RMASK;
if( i==3 ) return arm.RMASK;
if( i==4 ) return arm.RMASK;
- throw Utils.TODO();
+ return null; // Trailing anti-dependence edges
}
// Register mask allowed as a result. 0 for no register.
@Override public RegMask outregmap() { return null; }
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java
index fd207157..009af342 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/arm/arm.java
@@ -204,6 +204,8 @@ public class arm extends Machine {
case NewNode nnn -> new NewARM(nnn);
case OrNode or -> or(or);
case ParmNode parm -> new ParmARM(parm);
+ case BulkMemPhiNode phi -> new BulkMemPhiNode(phi);
+ case MemPhiNode phi -> new MemPhiNode(phi);
case PhiNode phi -> new PhiNode(phi);
case ProjNode prj -> prj(prj);
case ReadOnlyNode read -> new ReadOnlyNode(read);
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/MemOpRISC.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/MemOpRISC.java
index bcd7924e..01f51b57 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/MemOpRISC.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/MemOpRISC.java
@@ -33,6 +33,8 @@ public class MemOpRISC extends MemOpNode implements MachNode {
@Override public StringBuilder _printMach(StringBuilder sb, BitSet visited) { return sb.append(".").append(_name); }
+ @Override public boolean isMem() { return _type instanceof TypeMem; }
+
@Override public String label() { return op(); }
@Override public Type compute() { throw Utils.TODO(); }
@Override public Node idealize() { throw Utils.TODO(); }
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/NewRISC.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/NewRISC.java
index 53ad0af2..e8d32570 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/NewRISC.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/NewRISC.java
@@ -10,9 +10,9 @@ public class NewRISC extends NewNode implements MachNode {
// A pre-zeroed chunk of memory.
NewRISC( NewNode nnn ) { super(nnn); }
// Size and pointer result in standard calling convention; null for all the
- // memory aliases edges
- @Override public RegMask regmap(int i) { return i == 1 ? riscv.A0_MASK : null; }
- @Override public RegMask outregmap(int i) { return i == 1 ? riscv.A0_MASK : null; }
+ // memory and ordering edges
+ @Override public RegMask regmap(int i) { return i == 2 ? riscv.A0_MASK : null; }
+ @Override public RegMask outregmap(int i) { return i == 0 ? riscv.A0_MASK : null; }
@Override public RegMask outregmap() { return null; }
// Encoding is appended into the byte array; size is returned
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/StoreRISC.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/StoreRISC.java
index c57cf11d..22e443fd 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/StoreRISC.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/StoreRISC.java
@@ -34,7 +34,7 @@ public class StoreRISC extends MemOpRISC {
if( i==2 ) return riscv.RMASK;
if( i==3 ) return riscv.RMASK;
if( i==4 ) return riscv.RMASK;
- throw Utils.TODO();
+ return null; // Trailing anti-dependence edges
}
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/riscv.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/riscv.java
index 83706cbf..daf33f29 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/riscv.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/riscv/riscv.java
@@ -206,6 +206,8 @@ public class riscv extends Machine {
case NewNode nnn -> new NewRISC(nnn);
case OrNode or -> or(or);
case ParmNode parm -> new ParmRISC(parm);
+ case BulkMemPhiNode phi -> new BulkMemPhiNode(phi);
+ case MemPhiNode phi -> new MemPhiNode(phi);
case PhiNode phi -> new PhiNode(phi);
case ProjNode prj -> prj(prj);
case ReadOnlyNode read -> new ReadOnlyNode(read);
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/MemOpX86.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/MemOpX86.java
index f65fe054..1a771081 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/MemOpX86.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/MemOpX86.java
@@ -57,6 +57,8 @@ public abstract class MemOpX86 extends MemOpNode implements MachNode {
return sb.append(".").append(_name);
}
+ @Override public boolean isMem() { return _type instanceof TypeMem; }
+
@Override public String label() { return op(); }
@Override public Type compute() { throw Utils.TODO(); }
@Override public Node idealize() { throw Utils.TODO(); }
@@ -67,7 +69,7 @@ public abstract class MemOpX86 extends MemOpNode implements MachNode {
if( i==2 ) return x86_64_v2.RMASK; // base in GPR
if( i==3 ) return x86_64_v2.RMASK; // index in GPR
if( i==4 ) return _sz >= '4' ? x86_64_v2.MEM_MASK : x86_64_v2.RMASK; // Narrow stores need GPRs
- throw Utils.TODO();
+ return null; // Trailing anti-dependence edges
}
@Override public int encoding(ByteArrayOutputStream bytes) { throw Utils.TODO(); }
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/NewX86.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/NewX86.java
index cd47c64b..f4803b2e 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/NewX86.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/NewX86.java
@@ -10,9 +10,9 @@ public class NewX86 extends NewNode implements MachNode {
// A pre-zeroed chunk of memory.
NewX86( NewNode nnn ) { super(nnn); }
// Size and pointer result in standard calling convention; null for all the
- // memory aliases edges
- @Override public RegMask regmap(int i) { return i == 1 ? x86_64_v2.RDI_MASK : null; }
- @Override public RegMask outregmap(int i) { return i == 1 ? x86_64_v2.RAX_MASK : null; }
+ // memory and ordering edges
+ @Override public RegMask regmap(int i) { return i == 2 ? x86_64_v2.RDI_MASK : null; }
+ @Override public RegMask outregmap(int i) { return i == 0 ? x86_64_v2.RAX_MASK : null; }
@Override public RegMask outregmap() { return null; }
// Encoding is appended into the byte array; size is returned
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java
index aacd2352..57771d6a 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/x86_64_v2.java
@@ -235,6 +235,8 @@ public class x86_64_v2 extends Machine {
case NewNode nnn -> new NewX86(nnn);
case OrNode or -> or(or);
case ParmNode parm -> new ParmX86(parm);
+ case BulkMemPhiNode phi -> new BulkMemPhiNode(phi);
+ case MemPhiNode phi -> new MemPhiNode(phi);
case PhiNode phi -> new PhiNode(phi);
case ProjNode prj -> prj(prj);
case ReadOnlyNode read -> new ReadOnlyNode(read);
@@ -261,11 +263,12 @@ public class x86_64_v2 extends Machine {
private Node add( AddNode add ) {
Node lhs = add.in(1);
Node rhs = add.in(2);
+ // The other addend must occupy the result register, even if constant.
if( lhs instanceof LoadNode ld && ld.nOuts()==1 )
- return new AddMemX86(add,address(ld),ld.ptr(),idx,off,scale, imm(rhs),val);
+ return new AddMemX86(add,address(ld),ld.ptr(),idx,off,scale, 0,rhs);
if( rhs instanceof LoadNode ld && ld.nOuts()==1 )
- throw Utils.TODO(); // Swap load sides
+ return new AddMemX86(add,address(ld),ld.ptr(),idx,off,scale, 0,lhs);
// Attempt a full LEA-style break down.
// Returns one of AddX86, AddIX86, LeaX86, or LHS
diff --git a/chapter20/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter20/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
index d0df57fa..7a2025da 100644
--- a/chapter20/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
+++ b/chapter20/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -67,7 +67,7 @@ public class SimpleGraphAdapter extends GraphAdapter<Node> {
return n instanceof CFGNode ? Role.CTRL : isMem(n) ? Role.MEM : Role.DATA;
if( n instanceof RegionNode && i == 0 ) return Role.ASSOC;
if( i == 0 || n instanceof RegionNode || n instanceof StopNode ) return Role.CTRL;
- if( n instanceof MemMergeNode || i == 1 && (n instanceof MemOpNode || n instanceof ReturnNode || n instanceof CallNode) ) return Role.MEM;
+ if( n instanceof MemMergeNode || (i == 1 && (n instanceof MemOpNode || n instanceof ReturnNode || n instanceof CallNode || n instanceof NewNode)) ) return Role.MEM;
Node def = n.in(i);
if( def != null && isMem(def) ) return Role.MEM;
return Role.DATA;