Skip to content

Commit ddc526f

Browse files
Start to replace IterPeeps
1 parent 3d15519 commit ddc526f

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.compilerprogramming.ezlang.compiler;
2+
3+
import com.compilerprogramming.ezlang.compiler.codegen.CodeGen;
4+
import com.compilerprogramming.ezlang.compiler.node.Node;
5+
import com.compilerprogramming.ezlang.compiler.node.StopNode;
6+
7+
import java.util.ArrayList;
8+
import java.util.BitSet;
9+
import java.util.List;
10+
import java.util.function.Consumer;
11+
12+
public class IterPeeps2 {
13+
14+
static void dfs(Node root, Consumer<Node> consumer, BitSet visited) {
15+
visited.set(root._nid);
16+
/* For each successor node */
17+
for (int i = 0; i < root.nOuts(); i++) {
18+
Node S = root.out(i);
19+
if (S == null)
20+
continue;
21+
if (!visited.get(S._nid))
22+
dfs(S, consumer, visited);
23+
}
24+
consumer.accept(root);
25+
}
26+
27+
public static List<Node> rpo(Node root) {
28+
List<Node> nodes = new ArrayList<>();
29+
// note add below prepends
30+
dfs(root, (n)->nodes.add(0,n), new BitSet());
31+
return nodes;
32+
}
33+
34+
public void iterate(CodeGen code) {
35+
final Node startNode = code._start; // We need the start node to get to the full graph
36+
int iter = 1;
37+
int count = 0; // Count of nodes we peepholed
38+
for (;;iter++) {
39+
List<Node> rpos = rpo(startNode);
40+
count += rpos.size();
41+
boolean progress = false;
42+
for (int i = 0; i < rpos.size(); i++) {
43+
Node n = rpos.get(i);
44+
if (n.isDead()) continue;
45+
Node x = n.peepholeOpt();
46+
if (x != null) {
47+
progress = true;
48+
if (x.isDead()) continue;
49+
// peepholeOpt can return brand-new nodes, needing an initial type set
50+
if( x._type==null ) x.setType(x.compute());
51+
if (x != n) n.subsume(x);
52+
}
53+
if( n.isUnused() && !(n instanceof StopNode) )
54+
n.kill(); // Just plain dead
55+
}
56+
if (!progress) break;
57+
}
58+
System.out.println("Completed in " + iter + " iterations; peepholed " + count + " nodes");
59+
// if( show )
60+
// System.out.println(new GraphVisualizer().generateDotOutput(stopNode,null,null));
61+
}
62+
}

‎seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public CodeGen( String src, TypeInteger arg, long workListSeed ) {
5151
_stop = new StopNode(src);
5252
_src = src;
5353
_arg = arg;
54-
_iter = new IterPeeps(workListSeed);
54+
//_iter = new IterPeeps(workListSeed);
55+
_iter = new IterPeeps2();
5556
_main = makeFun(TypeTuple.MAIN, Type.BOTTOM);
5657
P = new Compiler(this,arg);
5758
}
@@ -180,7 +181,7 @@ public CodeGen parse() {
180181

181182
// ---------------------------
182183
// Iterator peepholes.
183-
public final IterPeeps _iter;
184+
public final IterPeeps2 _iter;
184185
// Stop tracking deps while assertin
185186
public boolean _midAssert;
186187
// Statistics on peepholes
@@ -206,8 +207,8 @@ public CodeGen opto() {
206207
// loop unroll, peel, RCE, etc
207208
return this;
208209
}
209-
public <N extends Node> N add( N n ) { return _iter.add(n); }
210-
public void addAll( Ary<Node> ary ) { _iter.addAll(ary); }
210+
public <N extends Node> N add( N n ) { /* return _iter.add(n); */ return n; }
211+
public void addAll( Ary<Node> ary ) { /* _iter.addAll(ary); */ }
211212

212213

213214
// ---------------------------

0 commit comments

Comments
 (0)