summaryrefslogtreecommitdiff
path: root/chapter19/src/main/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-02 11:20:00 -0700
committerCliff Click <cliffc@acm.org>2026-10-02 11:20:00 -0700
commitd5352268a0a1355262eed78e3263c18486fb9d8a (patch)
treef28474bb1e4333867214834e6abdeea729be68c7 /chapter19/src/main/java/com/seaofnodes
parentbd0ef17d0f9563ed9579fe060b785211e8378006 (diff)
downloadSimple-d5352268a0a1355262eed78e3263c18486fb9d8a.tar.gz
Simple-d5352268a0a1355262eed78e3263c18486fb9d8a.zip
Fix bug loop tree depth on inf loops
Diffstat (limited to 'chapter19/src/main/java/com/seaofnodes')
-rw-r--r--chapter19/src/main/java/com/seaofnodes/simple/node/CFGNode.java10
-rw-r--r--chapter19/src/main/java/com/seaofnodes/simple/node/LoopNode.java9
2 files changed, 15 insertions, 4 deletions
diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/CFGNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/CFGNode.java
index d57c3e8c..383f18d0 100644
--- a/chapter19/src/main/java/com/seaofnodes/simple/node/CFGNode.java
+++ b/chapter19/src/main/java/com/seaofnodes/simple/node/CFGNode.java
@@ -106,7 +106,7 @@ public abstract class CFGNode extends Node {
// ------------------------------------------------------------------------
// Tag all CFG Nodes with their containing LoopNode; LoopNodes themselves
// also refer to *their* containing LoopNode, as well as have their depth.
- // Start is a LoopNode which contains all at depth 1.
+ // Start contains the function bodies at depth 0; real loops add depth.
public void buildLoopTree(StopNode stop) {
_ltree = stop._ltree = Parser.XCTRL._ltree = new LoopTree((StartNode)this);
_bltWalk(2,null,stop, new BitSet());
@@ -115,6 +115,14 @@ public abstract class CFGNode extends Node {
// Pre-walked?
if( _pre!=0 ) return pre;
_pre = pre++;
+ // Seed the function's outer tree before walking its body. Synthetic
+ // exits can be added before the post-order walk reaches the entry.
+ if( this instanceof FunNode ) _ltree = stop._ltree;
+ if( this instanceof ReturnNode ) {
+ _ltree = fun._ltree;
+ post.set(_nid);
+ return pre; // Stop/Start links describe the outside world, not a loop.
+ }
// Pre-walk
for( Node use : _outputs )
if( use instanceof CFGNode usecfg && !skip( usecfg ) )
diff --git a/chapter19/src/main/java/com/seaofnodes/simple/node/LoopNode.java b/chapter19/src/main/java/com/seaofnodes/simple/node/LoopNode.java
index 5482a737..db353c1d 100644
--- a/chapter19/src/main/java/com/seaofnodes/simple/node/LoopNode.java
+++ b/chapter19/src/main/java/com/seaofnodes/simple/node/LoopNode.java
@@ -31,7 +31,7 @@ public class LoopNode extends RegionNode {
// If this is an unreachable loop, it may not have an exit. If it does not
// (i.e., infinite loop), force an exit to make it reachable.
- public StopNode forceExit( FunNode fun, StopNode stop ) {
+ public FunNode forceExit( FunNode fun, StopNode stop ) {
// Walk the backedge, then immediate dominator tree util we hit this
// Loop again. If we ever hit a CProj from an If (as opposed to
// directly on the If) we found our exit.
@@ -40,7 +40,7 @@ public class LoopNode extends RegionNode {
if( x instanceof CProjNode exit && exit.in(0) instanceof IfNode iff ) {
CFGNode other = iff.cproj(1-exit._idx);
if( other!=null && other.loopDepth() < loopDepth() )
- return stop; // Found an exit, not an infinite loop
+ return fun; // Found an exit, not an infinite loop
}
x = x.idom();
}
@@ -57,6 +57,8 @@ public class LoopNode extends RegionNode {
// XCtrl or a bunch of prior NeverNode exits.
Node top = new ConstantNode(Type.TOP).peephole();
ReturnNode ret = fun.ret();
+ iff._ltree = f._ltree = _ltree;
+ ret._ltree = t._ltree = fun._ltree;
Node ctrl = ret.ctrl(), mem = ret.mem(), expr = ret.expr();
if( ctrl._type != Type.XCONTROL ) {
// Perfect aligned exit?
@@ -77,10 +79,11 @@ public class LoopNode extends RegionNode {
mem = top;
expr = top;
}
+ ((CFGNode)ctrl)._ltree = fun._ltree;
ret.setDef(0,ctrl);
ret.setDef(1,mem );
ret.setDef(2,expr);
- return stop;
+ return fun;
}
}