summaryrefslogtreecommitdiff
path: root/chapter17/src
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-20 10:04:48 -0700
committerCliff Click <cliffc@acm.org>2026-09-20 10:04:48 -0700
commitdb3ca780c26b78cbe129a5c1bdf0d8ff7d867d1b (patch)
treeaa12f702c8b558446b06944ed5301dc826245c8f /chapter17/src
parent6e59fc1b71f2098c8baf90a6c54e5a33b7291e94 (diff)
downloadSimple-db3ca780c26b78cbe129a5c1bdf0d8ff7d867d1b.tar.gz
Simple-db3ca780c26b78cbe129a5c1bdf0d8ff7d867d1b.zip
Unify GCM across chapters
Remove the _anti field. Remove the isPinned() call. Add a copyEmpty call.
Diffstat (limited to 'chapter17/src')
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java75
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/node/CFGNode.java3
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/node/CastNode.java1
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/node/Node.java2
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/node/PhiNode.java1
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/node/ProjNode.java1
6 files changed, 42 insertions, 41 deletions
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java b/chapter17/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
index 8cfbe509..ee6e1f1a 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
@@ -32,6 +32,9 @@ public abstract class GlobalCodeMotion {
cfg.loopDepth();
for( Node n : cfg._inputs )
_schedEarly(n,visit);
+ // In dead infinite loops, entire code blocks may be unreachable
+ // from below. Reach down from the CFG to their Phis so their
+ // inputs are scheduled too.
if( cfg instanceof RegionNode )
for( Node phi : cfg._outputs )
if( phi instanceof PhiNode )
@@ -53,20 +56,17 @@ public abstract class GlobalCodeMotion {
if( n==null || visit.get(n._nid) ) return; // Been there, done that
assert !(n instanceof CFGNode);
visit.set(n._nid);
- // Schedule not-pinned not-CFG inputs before self. Since skipping
- // Pinned, this never walks the backedge of Phis (and thus spins around
- // a data-only loop, eventually attempting relying on some pre-visited-
- // not-post-visited data op with no scheduled control.
+ // Schedule inputs first, except Phis: following their backedges would
+ // enter a data cycle before its control has been scheduled.
for( Node def : n._inputs )
if( def!=null && !(def instanceof PhiNode) )
_schedEarly(def,visit);
- // If not-pinned (e.g. constants, projections, phi) and not-CFG
- if( !n.isPinned() ) {
+ // An existing edge 0 already supplies control (or a Phi/Proj binding).
+ if( n.in(0)==null ) {
// Schedule at deepest input
CFGNode early = Parser.START; // Maximally early, lowest idepth
- if( n.in(0) instanceof CFGNode cfg ) early = cfg;
for( int i=1; i<n.nIns(); i++ )
- if( n.in(i).cfg0().idepth() > early.idepth() )
+ if( n.in(i)!=null && n.in(i).cfg0().idepth() > early.idepth() )
early = n.in(i).cfg0(); // Latest/deepest input
n.setDef(0,early); // First place this can go
}
@@ -76,8 +76,11 @@ public abstract class GlobalCodeMotion {
private static void schedLate( StopNode stop) {
CFGNode[] late = new CFGNode[Node.UID()];
Node[] ns = new Node[Node.UID()];
+ // Record Load NIDs at all their CFG block choices, then check against
+ // Store block choices to force a Load above an anti-dependent Store.
+ int[] anti = new int[Node.UID()];
// Breadth-first scheduling
- breadth(stop,ns,late);
+ breadth(stop,ns,late,anti);
// Copy the best placement choice into the control slot
for( int i=0; i<late.length; i++ )
@@ -85,7 +88,7 @@ public abstract class GlobalCodeMotion {
ns[i].setDef(0,late[i]);
}
- private static void breadth(Node stop, Node[] ns, CFGNode[] late) {
+ private static void breadth(Node stop, Node[] ns, CFGNode[] late, int[] anti) {
// Things on the worklist have some (but perhaps not all) uses done.
WorkList<Node> work = new WorkList<>();
work.push(stop);
@@ -115,22 +118,26 @@ public abstract class GlobalCodeMotion {
continue outer;
// All uses done, schedule
- _doSchedLate(n,ns,late);
+ _doSchedLate(n,ns,late,anti);
}
- // Walk all inputs and put on worklist, as their last-use might now be done
- for( Node def : n._inputs )
- if( def!=null && late[def._nid]==null ) {
- work.push(def);
- // if the def has a load use, maybe the load can fire
- for( Node ld : def._outputs )
- if( ld instanceof LoadNode && late[ld._nid]==null )
- work.push(ld);
- }
+ // A use just finished; reconsider its inputs and waiting loads,
+ // even when the shared memory input was already scheduled.
+ 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);
+ }
+ if( n instanceof LoopNode loop )
+ for( Node phi : loop._outputs )
+ if( phi instanceof PhiNode && late[phi._nid]==null )
+ work.push(phi);
}
}
- private static void _doSchedLate(Node n, Node[] ns, CFGNode[] late) {
+ private static void _doSchedLate(Node n, Node[] ns, CFGNode[] late, int[] anti) {
// Walk uses, gathering the LCA (Least Common Ancestor) of uses
CFGNode early = n.in(0) instanceof CFGNode cfg ? cfg : n.in(0).cfg0();
assert early != null;
@@ -141,7 +148,7 @@ public abstract class GlobalCodeMotion {
// Loads may need anti-dependencies, raising their LCA
if( n instanceof LoadNode load )
- lca = find_anti_dep(lca,load,early,late);
+ lca = find_anti_dep(lca,load,early,late,anti);
// Walk up from the LCA to the early, looking for best place. This is
// the lowest execution frequency, approximated by least loop depth and
@@ -164,8 +171,7 @@ public abstract class GlobalCodeMotion {
CFGNode found=null;
for( int i=1; i<phi.nIns(); i++ )
if( phi.in(i)==n )
- if( found==null ) found = phi.region().cfg(i);
- else Utils.TODO(); // Can be more than once
+ found = phi.region().cfg(i).domLCA(found,null); // Can be more than one matching input.
assert found!=null;
return found;
}
@@ -174,31 +180,33 @@ public abstract class GlobalCodeMotion {
// Least loop depth first, then largest idepth
private static boolean better( CFGNode lca, CFGNode best ) {
return lca.loopDepth() < best.loopDepth() ||
- (lca.idepth() > best.idepth() || best instanceof IfNode);
+ lca instanceof NeverNode ||
+ lca.idepth() > best.idepth() ||
+ best instanceof IfNode;
}
- private static CFGNode find_anti_dep(CFGNode lca, LoadNode load, CFGNode early, CFGNode[] late) {
+ private static CFGNode find_anti_dep(CFGNode lca, LoadNode 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() )
- cfg._anti = load._nid;
+ anti[cfg._nid] = load._nid;
// Walk load->mem uses, looking for Stores causing an anti-dep
for( Node mem : load.mem()._outputs ) {
switch( mem ) {
case StoreNode st:
assert late[st._nid]!=null;
- lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st);
+ lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st,anti);
break;
case NewNode st:
assert late[st._nid]!=null;
- lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st);
+ lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st,anti);
break;
case PhiNode phi:
// Repeat anti-dep for matching Phi inputs.
// No anti-dep edges but may raise the LCA.
for( int i=1; i<phi.nIns(); i++ )
if( phi.in(i)==load.mem() )
- lca = anti_dep(load,phi.region().cfg(i),load.mem().cfg0(),lca,null);
+ lca = anti_dep(load,phi.region().cfg(i),load.mem().cfg0(),lca,null,anti);
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
@@ -211,11 +219,12 @@ public abstract class GlobalCodeMotion {
}
//
- private static CFGNode anti_dep( LoadNode load, CFGNode stblk, CFGNode defblk, CFGNode lca, Node st ) {
- // Walk store blocks "reach" from its scheduled location to its earliest
+ 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.
for( ; stblk != defblk.idom(); stblk = stblk.idom() ) {
// Store and Load overlap, need anti-dependence
- if( stblk._anti==load._nid ) {
+ 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
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/node/CFGNode.java b/chapter17/src/main/java/com/seaofnodes/simple/node/CFGNode.java
index 8e063205..e85fc6ae 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/node/CFGNode.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/node/CFGNode.java
@@ -62,9 +62,6 @@ public abstract class CFGNode extends Node {
return lhs;
}
- // Anti-dependence field support
- public int _anti; // Per-CFG field to help find anti-deps
-
// ------------------------------------------------------------------------
// Loop nesting
public LoopNode loop() { return _ltree._head; }
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/node/CastNode.java b/chapter17/src/main/java/com/seaofnodes/simple/node/CastNode.java
index 462d6cba..38d6a126 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/node/CastNode.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/node/CastNode.java
@@ -21,7 +21,6 @@ public class CastNode extends Node {
@Override
public String uniqueName() { return "Cast_" + _nid; }
- @Override public boolean isPinned() { return true; }
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter17/src/main/java/com/seaofnodes/simple/node/Node.java
index 09180899..08033df0 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -544,8 +544,6 @@ public abstract class Node implements OutNode {
/** Is this Node Memory related */
public boolean isMem() { return false; }
- /** Pinned in the schedule; these are data nodes whose input#0 is not allowed to change */
- public boolean isPinned() { return false; }
// Semantic change to the graph (so NOT a peephole), used by the Parser.
// If any input is a float, flip to a float-flavored opcode and widen any
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter17/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index cb266ca3..4962f4f4 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -38,7 +38,6 @@ public class PhiNode extends Node {
public CFGNode region() { return (CFGNode)in(0); }
@Override public boolean isMultiTail() { return true; }
@Override public boolean isMem() { return _declaredType instanceof TypeMem; }
- @Override public boolean isPinned() { return true; }
@Override
public Type compute() {
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/node/ProjNode.java b/chapter17/src/main/java/com/seaofnodes/simple/node/ProjNode.java
index 73c1fa15..cd57b85c 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/node/ProjNode.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/node/ProjNode.java
@@ -29,7 +29,6 @@ public class ProjNode extends Node {
@Override public boolean isMultiTail() { return in(0).isMultiHead(); }
@Override public boolean isMem() { return _type instanceof TypeMem; }
- @Override public boolean isPinned() { return true; }
@Override
public Type compute() {