summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-08-05 16:15:27 -0700
committerCliff Click <cliffc@acm.org>2026-10-08 16:52:27 -0700
commitfc06aa2bebec45aa93df5450ebf4c327f9f51496 (patch)
tree8788a35ac00f0d146d17b72e5491afd4082a1fb0
parentc54580d2b2685829dab0976ed8c2bcae59322f69 (diff)
downloadSimple-fc06aa2bebec45aa93df5450ebf4c327f9f51496.tar.gz
Simple-fc06aa2bebec45aa93df5450ebf4c327f9f51496.zip
Chapter 13: Global Code Motion
Co-authored-by: Thalia Archibald <thalia@archibald.dev> Co-authored-by: Dibyendu Majumdar <mobile@majumdar.org.uk> Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: tannal <tannal2409@gmail.com> Co-authored-by: hadha <helsgithub15@gmail.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: RobertObkircher <robert.obkircher@gmail.com> Co-authored-by: Robert Obkircher <robert.obkircher@gmail.com> Co-authored-by: Jun Kawasaki <jun784@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
-rw-r--r--Makefile111
-rw-r--r--README.ja.md2
-rw-r--r--README.md2
-rw-r--r--chapter13.iml (renamed from chapter12.iml)4
-rw-r--r--docs/chapter13/Makefile1
-rw-r--r--docs/chapter13/README.ja.md258
-rw-r--r--docs/chapter13/README.md375
-rw-r--r--docs/chapter13/graph1.gv79
-rw-r--r--docs/chapter13/graph1.json25
-rw-r--r--docs/chapter13/graph1.svg123
-rw-r--r--docs/chapter13/graph2.gv84
-rw-r--r--docs/chapter13/graph2.json25
-rw-r--r--docs/chapter13/graph2.svg123
-rw-r--r--docs/chapter13/graph3-snip.jpgbin0 -> 19681 bytes
-rw-r--r--docs/chapter13/graph3-snip.json17
-rw-r--r--docs/chapter13/graph3-snip.svg91
-rw-r--r--docs/chapter13/graph3.gv85
-rw-r--r--docs/chapter13/graph3.json26
-rw-r--r--docs/chapter13/graph3.svg128
-rw-r--r--docs/chapter13/graph4.gv110
-rw-r--r--docs/chapter13/graph4.json24
-rw-r--r--docs/chapter13/graph4.svg120
-rw-r--r--docs/chapter13/graph5.gv117
-rw-r--r--docs/chapter13/graph5.json24
-rw-r--r--docs/chapter13/graph5.svg120
-rw-r--r--docs/chapter13/graph6.gv117
-rw-r--r--docs/chapter13/graph6.json25
-rw-r--r--docs/chapter13/graph6.svg124
-rw-r--r--docs/chapter13/graph7.gv23
-rw-r--r--docs/chapter13/graph7.json16
-rw-r--r--docs/chapter13/graph7.svg78
-rw-r--r--docs/chapter13/graph8.gv45
-rw-r--r--docs/chapter13/graph8.json21
-rw-r--r--docs/chapter13/graph8.svg98
-rw-r--r--src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java275
-rw-r--r--src/main/java/com/seaofnodes/simple/IRPrinter.java7
-rw-r--r--src/main/java/com/seaofnodes/simple/Parser.java59
-rw-r--r--src/main/java/com/seaofnodes/simple/Utils.java7
-rw-r--r--src/main/java/com/seaofnodes/simple/node/AddNode.java1
-rw-r--r--src/main/java/com/seaofnodes/simple/node/CFGNode.java96
-rw-r--r--src/main/java/com/seaofnodes/simple/node/CProjNode.java62
-rw-r--r--src/main/java/com/seaofnodes/simple/node/CastNode.java1
-rw-r--r--src/main/java/com/seaofnodes/simple/node/ConstantNode.java1
-rw-r--r--src/main/java/com/seaofnodes/simple/node/IfNode.java18
-rw-r--r--src/main/java/com/seaofnodes/simple/node/LoadNode.java17
-rw-r--r--src/main/java/com/seaofnodes/simple/node/LoopNode.java63
-rw-r--r--src/main/java/com/seaofnodes/simple/node/MultiNode.java16
-rw-r--r--src/main/java/com/seaofnodes/simple/node/NeverNode.java22
-rw-r--r--src/main/java/com/seaofnodes/simple/node/NewNode.java1
-rw-r--r--src/main/java/com/seaofnodes/simple/node/Node.java58
-rw-r--r--src/main/java/com/seaofnodes/simple/node/OutNode.java7
-rw-r--r--src/main/java/com/seaofnodes/simple/node/PhiNode.java13
-rw-r--r--src/main/java/com/seaofnodes/simple/node/ProjNode.java18
-rw-r--r--src/main/java/com/seaofnodes/simple/node/RegionNode.java29
-rw-r--r--src/main/java/com/seaofnodes/simple/node/ReturnNode.java9
-rw-r--r--src/main/java/com/seaofnodes/simple/node/StartNode.java21
-rw-r--r--src/main/java/com/seaofnodes/simple/node/StopNode.java28
-rw-r--r--src/main/java/com/seaofnodes/simple/node/SubNode.java2
-rw-r--r--src/main/java/com/seaofnodes/simple/node/XCtrlNode.java14
-rw-r--r--src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java8
-rw-r--r--src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java2
-rw-r--r--src/main/java/com/seaofnodes/simple/type/TypeTuple.java12
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter01Test.java2
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter04Test.java2
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter05Test.java14
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter06Test.java16
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter07Test.java48
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter08Test.java25
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter09Test.java26
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter10Test.java30
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter12Test.java2
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter13Test.java532
-rw-r--r--src/test/java/com/seaofnodes/simple/FuzzerWrap.java2
-rw-r--r--src/test/java/com/seaofnodes/simple/TypeTest.java26
-rw-r--r--src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java3
-rw-r--r--src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java14
76 files changed, 3971 insertions, 259 deletions
diff --git a/Makefile b/Makefile
index 5ed21084..3f1e53ff 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,106 @@
-# Standalone compiler snapshot; shared rules also work in linearized checkouts.
-include $(firstword $(wildcard ../build-support/early-chapter.mk build-support/early-chapter.mk))
-test_cp += com.seaofnodes.simple.FuzzerWrap
+SHELL := /bin/bash
+# Shared packaging target; included after chapter source variables below.
+.DELETE_ON_ERROR:
-.PHONY: fuzzer
-fuzzer: $(test_classes)
- java -ea -cp "$(CLZDIR)/main$(SEP)$(CLZDIR)/test$(SEP)$(jars)" org.junit.runner.JUnitCore com.seaofnodes.simple.FuzzerWrap
+# for printing variable values
+# usage: make print-VARIABLE
+# > VARIABLE = value_of_variable
+print-% : ; @echo $* = $($*)
+
+# literal space
+space := $() $()
+
+# Decide OS-specific questions
+# jar-file seperator
+ifeq ($(OS),Windows_NT)
+ SEP = ;
+else
+ SEP = :
+endif
+# Find a reasonable ctags.
+CTAGS = $(shell which ctags)
+# Hack for MacOS: /usr/bin/ctags is unfriendly, so look for ctags from brew
+ifeq ($(UNAME),Darwin)
+ CTAGS = $(shell brew list ctags 2> /dev/null | grep bin/ctags)
+endif
+
+# Fun Args to javac. Mostly limit to java8 source definitions, and fairly
+# aggressive lint warnings.
+JAVAC_ARGS = -g
+
+# Source code
+SIMPLE := com/seaofnodes/simple
+SRC := src/main/java
+TST := src/test/java
+CLZDIR:= build/classes
+main_javas := $(wildcard $(SRC)/$(SIMPLE)/*java $(SRC)/$(SIMPLE)/*/*java)
+test_javas := $(wildcard $(TST)/$(SIMPLE)/*java $(TST)/$(SIMPLE)/*/*java)
+main_classes := $(patsubst $(SRC)/%java,$(CLZDIR)/main/%class,$(main_javas))
+include $(firstword $(wildcard ../graph/graph.mk graph/graph.mk))
+include $(firstword $(wildcard ../print/print.mk print/print.mk))
+test_classes := $(patsubst $(TST)/%java,$(CLZDIR)/test/%class,$(test_javas))
+test_cp := $(patsubst $(TST)/$(SIMPLE)/%.java,com.seaofnodes.simple.%,$(wildcard $(TST)/$(SIMPLE)/*Test.java))
+classes = $(main_classes) $(test_classes)
+# All the libraries
+libs = $(wildcard lib/*jar)
+jars = $(subst $(space),$(SEP),$(libs))
+
+
+default_targets := $(main_classes) $(test_classes)
+# Optionally add ctags to the default target if a reasonable one was found.
+ifneq ($(CTAGS),)
+default_targets += tags
+endif
+
+default: $(default_targets)
+
+# Compile just the out-of-date files
+$(main_classes): build/classes/main/%class: $(SRC)/%java $(graph_javas) $(print_javas)
+ @echo "compiling " $@ " because " $?
+ @[ -d $(CLZDIR)/main ] || mkdir -p $(CLZDIR)/main
+ @javac $(JAVAC_ARGS) -cp "$(CLZDIR)/main$(SEP)$(jars)" -sourcepath $(SRC) -d $(CLZDIR)/main $(main_javas)
+
+$(test_classes): $(CLZDIR)/test/%class: $(TST)/%java $(main_classes)
+ @echo "compiling " $@ " because " $?
+ @[ -d $(CLZDIR)/test ] || mkdir -p $(CLZDIR)/test
+ @javac $(JAVAC_ARGS) -cp "$(CLZDIR)/test$(SEP)$(CLZDIR)/main$(SEP)$(jars)" -sourcepath $(TST) -d $(CLZDIR)/test $(test_javas)
+
+# Base launch line for JVM tests
+JVM=nice java -ea -cp "build/classes/main${SEP}${jars}${SEP}$(CLZDIR)/test"
+
+tests: $(default_targets)
+ @echo "testing " $(test_cp)
+ @$(JVM) org.junit.runner.JUnitCore $(test_cp) com.seaofnodes.simple.FuzzerWrap
+
+fuzzer: $(default_targets)
+ @echo "fuzzing " $(test_cp)
+ @$(JVM) org.junit.runner.JUnitCore com.seaofnodes.simple.FuzzerWrap
+
+
+.PHONY: clean
+clean:
+ rm -rf build
+ rm -f TAGS
+ (find . -name "*~" -exec rm {} \; 2>/dev/null; exit 0)
+
+# Download libs from maven
+lib: lib/junit-4.12.jar lib/hamcrest-core-1.3.jar
+
+# Jars
+lib/junit-4.12.jar:
+ @[ -d lib ] || mkdir -p lib
+ @(cd lib; wget https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar)
+
+lib/hamcrest-core-1.3.jar:
+ @[ -d lib ] || mkdir -p lib
+ @(cd lib; wget https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar)
+
+# Build emacs tags (part of a tasty emacs ide experience)
+tags: $(main_javas) $(test_javas)
+ @rm -f TAGS
+ @$(CTAGS) -e --recurse=yes --extra=+q --fields=+fksaiS $(SRC) $(TST)
+
+include $(firstword $(wildcard ../build-support/chapter-release.mk build-support/chapter-release.mk))
+
+.PHONY: build
+build: $(main_classes)
diff --git a/README.ja.md b/README.ja.md
index 7d45a807..a35cc238 100644
--- a/README.ja.md
+++ b/README.ja.md
@@ -49,7 +49,7 @@ Simple 言語のスタイルは、C または Java のサブセットに倣っã�
* [第10章](docs/chapter10/README.ja.md):ユーザー定義の構造体、ポインタ、null 解析。SSA で表す単一のメモリ値。ロード、ストア、実行可能な評価器。
* [第11章](docs/chapter11/README.ja.md):同値類によるエイリアス解析。`MemMerge`、`MemPhi`、`BulkMemPhi` による必要時のメモリ分割。
* [第12章](docs/chapter12/README.ja.md):参照フィールド、前方参照、再帰的な構造体。
-* 第13章:大域的コード移動とスケジューリング。
+* [第13章](docs/chapter13/README.ja.md):大域的コード移動とスケジューリング。
* 第14章:数値型。浮動小数点数、狭い整数型、範囲、`f32` への丸め。
* 第15章:静的な長さを持つ一次元配列型。配列のロードとストア。
* 第16章:コンストラクタ。
diff --git a/README.md b/README.md
index 6aca0c35..38ed3a95 100644
--- a/README.md
+++ b/README.md
@@ -58,7 +58,7 @@ The Simple language is styled after a subset of C or Java.
* [Chapter 11](docs/chapter11/README.md): Equivalence class aliasing. Lazy memory
partitioning with `MemMerge`, `MemPhi`, and `BulkMemPhi`.
* [Chapter 12](docs/chapter12/README.md): Reference fields, forward references and recursive structs.
-* Chapter 13: Global Code Motion - Scheduling.
+* [Chapter 13](docs/chapter13/README.md): Global Code Motion - Scheduling.
* Chapter 14: Numeric types: floats, narrow integers, ranges and rounding to `f32`.
* Chapter 15: One dimensional static length array type, with array loads and stores.
* Chapter 16: Constructors
diff --git a/chapter12.iml b/chapter13.iml
index 28f6886a..6fa60f3f 100644
--- a/chapter12.iml
+++ b/chapter13.iml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
- <output url="file://$MODULE_DIR$/../build/idea/production/chapter12" />
- <output-test url="file://$MODULE_DIR$/../build/idea/test/chapter12" />
+ <output url="file://$MODULE_DIR$/../build/idea/production/chapter13" />
+ <output-test url="file://$MODULE_DIR$/../build/idea/test/chapter13" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
diff --git a/docs/chapter13/Makefile b/docs/chapter13/Makefile
new file mode 100644
index 00000000..6fa0f057
--- /dev/null
+++ b/docs/chapter13/Makefile
@@ -0,0 +1 @@
+include ../../graph/docs.mk
diff --git a/docs/chapter13/README.ja.md b/docs/chapter13/README.ja.md
new file mode 100644
index 00000000..27f78e4b
--- /dev/null
+++ b/docs/chapter13/README.ja.md
@@ -0,0 +1,258 @@
+# 第13章: 大域的コード移動
+
+[English](README.md) | 日本語
+
+[前: 第12章](../chapter12/README.ja.md) |
+[次: 第14章](../chapter14/README.ja.md)
+
+この文書は [原文](README.md) の日本語訳です。
+
+この章では、第10〜12章で構築した、参照フィールドと遅延的なメモリ分割を含むグラフをスケジュールします。移動可能な演算のスロット 0 の制御入力を選び、エイリアスする可能性がある Load と Store の順序を維持します。
+
+また、この章ではかなり詳しいグラフアルゴリズムをいくつか紹介します。グラフ理論を復習しておくとよいでしょう。
+
+文法は[第12章](../chapter12/12-grammar.ja.md)から変わりません。
+
+# 目次
+
+1. [概要](#概要)
+2. [スケジューリングの例を追う](#スケジューリングの例を追う)
+3. [ループのスケジューリング](#ループのスケジューリング)
+4. [大域的コード移動アルゴリズムの構成要素](#大域的コード移動アルゴリズムの構成要素)
+5. [SoN グラフ内の基本ブロックの識別](#son-グラフ内の基本ブロックの識別)
+6. [無限ループの処理](#無限ループの処理)
+7. [支配ノード](#支配ノード)
+8. [ループ深度](#ループ深度)
+9. [早期スケジュール](#早期スケジュール)
+10. [遅延スケジュール](#遅延スケジュール)
+11. [反依存の挿入](#反依存の挿入)
+12. [動画での解説](#動画での解説)
+
+[linear](https://github.com/SeaOfNodes/Simple/tree/linear) ブランチの線形な Git リビジョン履歴で[この章](https://github.com/SeaOfNodes/Simple/tree/linear-chapter13)を読み、前章との[差分](https://github.com/SeaOfNodes/Simple/compare/linear-chapter12...linear-chapter13)を見ることもできます。
+
+元の入力ソースプログラムは、物事が起こる順序を定義します。プログラムを Sea of Nodes 表現にパースし、さまざまな最適化を行うと、その順序は完全には維持されません。最適化された Sea of Nodes グラフを動かすのは、元のソースプログラムの命令列ではなく、ノード間の依存関係です。
+
+この章の目標は、最適化された Sea of Nodes グラフから、命令を実行するスケジュールをどう復元するかを見ることです。このスケジュールは、元のソースプログラムの意味論を保持する必要がありますが、それ以外は命令の実行を並べ替えてかまいません。
+
+スケジューリングアルゴリズムは SoN グラフ内の暗黙の基本ブロックをまたいで働くため、大域的コード移動アルゴリズムと呼びます。スケジューラは基本ブロックの境界を越えて命令を移動できます。
+
+このアルゴリズムの主な参考文献は GCM GVN 論文 [^1] です。ただし Simple の実装には、論文で説明された版と異なる点があります。
+
+複雑な話題なので、まず概要を示してから詳しく見ていきます。
+
+## 概要
+
+Sea of Nodes グラフには、2つの仮想的なグラフが含まれています。
+
+* Start、If、Region など、特定のノード型が表す制御フローグラフがあります。
+* 主として、値やメモリを生成・消費するノード間の定義と使用のエッジ(Def-Use)によって成り立つデータグラフがあります。
+
+スケジューリングの観点では、制御フローグラフは固定され、動かせません。一方、データ値やメモリを消費・生成するノードには、実行する時点にある程度の自由があります。スケジューリングアルゴリズムの目標は、これらのノードについて、最適かつ正しい配置を見つけることです。
+
+中心となるスケジューリングアルゴリズムは2段階で働きます。
+
+* 早期スケジュール: この段階では末端(Stop)から始めて、各 Node の「入力」を上向きに DFS でたどります。各データノードを、その入力に支配される最初の制御ブロックに配置します。
+* 遅延スケジュール: この段階では Stop から逆向きに、定義より先に使用をスケジュールします。そしてデータノードを、上で計算した最初のブロックと、すべての使用を支配する最後の制御ブロックの間に移動します。配置は、可能な限り浅いループの入れ子に置き、かつできるだけ制御に依存させるという条件に従います。さらに Load 命令の配置では、同じメモリ位置に対する Load と Store の順序を正しくする必要があります。
+
+## スケジューリングの例を追う
+
+各手順を詳しく説明する前に、例を追ってスケジューリング中に SoN グラフがどう変わるかを見ると理解の助けになります。
+
+説明のために、次のプログラムを使います。
+
+```java
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
+```
+
+まずスケジューリング前のグラフを見ましょう。
+
+この説明用の図は、精密な `S.f` の鎖と、Return が消費する最後の MemMerge を示します。変更されないデフォルトのメモリは Start から来ており、冗長なひとまとめの Phi は畳み込まれて消えています。第10章・第11章と同じく、Region とその Phi 束縛を含む制御フローの文脈は省略しています。Load や Store を制御する箇所には、小さく切り離された True/False 射影が残っています。第10章の模式的なソース上の制御とは異なり、これらの図は各段階の実際のスケジューリング入力を示します。入口ブロックと Loop の束縛は省略しています。
+
+![グラフ1](./graph1.svg)
+
+次の点に注目してください。
+
+* 制御ノードは動かせません。ここでは大部分を省略しています。
+* New ノードにはすでに制御入力があり、Phi は Region に束縛されています。これらの束縛は図示していなくても固定のままです。
+* ロード `.f` とストア `.f=` のノードはまだ「浮いて」います。この段階では制御入力がないため、スケジューリングでそのブロックを選ぶ必要があります。
+
+次に、早期スケジュール実行後のグラフを見ましょう。
+
+![グラフ2](./graph2.svg)
+
+早期スケジュールは、入力がそこで利用できるため、ロード `.f` とストア `.f=` を最初の基本ブロックに置きます。この入口ブロックへの制御束縛は省略しているので、メモリの図は変わっていないように見えます。
+
+以下のグラフは、遅延スケジューリング後のメモリとデータの依存関係に注目しています。Region を含む制御フローの骨組みは省略しています。Store #22 の制御入力は、独立した True 射影 #8 への赤いエッジとして見えます。Store から Load への反依存とメモリ Phi は残しています。
+
+![グラフ3](./graph3.svg)
+
+以下の抜粋は、グラフの主な変更を示します。
+
+![先行するロードの後に順序付けられたストア](./graph3-snip.svg)
+
+* ストア `.f=` は、ロード `.f` に対する反依存を持つようになりました。これにより、プログラムの意味論が要求する通り、ストアがロードより後にスケジュールされます。反依存は後で詳しく扱います。
+* ストア `.f=` が If ノードの True 分岐に束縛されたことにも注目してください。
+
+## ループのスケジューリング
+
+今度はループを含む別の例を示します。
+
+```java
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+```
+
+スケジューリング前の SoN グラフは次のようになります。
+
+![グラフ4](./graph4.svg)
+
+早期スケジュールを生成すると、次を得ます。
+
+![グラフ5](./graph5.svg)
+
+早期スケジュールは Store #31 をループ先頭に配置します(制御束縛は省略)。遅延スケジューリングは、それを `if (arg == 5)` の True 分岐 #15 に移します。赤い制御エッジがそれを示します。
+
+![グラフ6](./graph6.svg)
+
+最終的な実行スケジュールでは、式 `i/3` はループの外で実行できます。`arg` の元の値だけに依存するため、ループ不変だからです。
+
+## 大域的コード移動アルゴリズムの構成要素
+
+上では GCM アルゴリズムの構成要素のいくつかに触れました。ここで、まだ触れていないものも含めて列挙します。
+
+* SoN グラフの制御フローノードには、暗黙の基本ブロックがあります。GCM アルゴリズムでは、これらのノードをもっと明示的に認識する必要があります。
+* プログラムに無限ループがあると、Stop ノードから到達できないノードが生じ、アルゴリズムに問題をもたらします。対処するため、無限ループを発見し、ループから Stop ノードへのダミーエッジを作る必要があります。
+* Simple の SoN グラフではループをすでに識別しているため、ループ発見の手順は不要です。ただし、各 CFG ノードに関連するループ深度を計算する必要があります。
+* [第6章](../chapter06/README.ja.md)で支配ノードの概念を説明しました。支配ノードは GCM アルゴリズムの要なので、段階的な支配ノード発見アルゴリズムを、その要件を満たすように拡張します。
+* アルゴリズムの2段階、早期スケジューリングと遅延スケジューリングはすでに述べました。
+* さらに、特定の場面では、正しい実行順序を強制するため Load と Store の間に反依存エッジを追加する必要があります。
+* GCM アルゴリズムの実装を容易にするため、Node 階層にいくつか変更を加えます。これらは、前の章から引き継いだ Node 階層を概念的に変えるものではありません。
+
+## SoN グラフ内の基本ブロックの識別
+
+Sea of Nodes グラフは、プログラムの制御フローグラフをすでに捉えています。この情報は、制御ノードと、制御ノードからほかの型のノードへのエッジに暗黙に含まれます。
+
+復習すると、制御は Start ノードで、`$ctrl` という名前に束縛された射影を介して始まります。プログラム中を制御が流れるにつれて、この名前の束縛は更新され、制御が受け渡され、最終的に Stop ノードに到達します。
+
+ある制御ノードは基本ブロックを開始し、別のものは基本ブロック(BB)を終了すると認識すれば、CFG の基本ブロック構造を簡単に構築できます。
+
+すべての制御ノードを列挙します。
+
+| ノード | BB を開始する | BB を終了する |
+|---|---|---|
+| Start | はい。ただし `$ctrl` 射影を介してのみ | いいえ |
+| CProj | 入力制御が If ノードなら、はい | いいえ |
+| Region | はい | いいえ |
+| If | いいえ | はい |
+| Return | いいえ | はい |
+| Stop | はい | はい |
+
+この章における Node 階層の変更は次の通りです。
+
+* 上のすべての制御ノードは、基底クラス CFG Node を継承するようになります。これによって、制御フローノードの共通機能を基底クラスに置けます。
+* CProj ノードは通常の Proj ノードを継承し、次の場合に使います。
+ * Start からの `$ctrl` 射影
+ * If からの True と False の射影
+* Never ノードは If の特別なサブクラスで、後述するように無限ループの処理に使います。
+* XCtrl ノードは死んだ制御を表します。
+
+## 無限ループの処理
+
+無限ループを含むコードの例です。
+
+```java
+while (1) {}
+return 0;
+```
+
+まず、その結果のグラフを見ましょう。
+
+![グラフ7](./graph7.svg)
+
+次に、無限ループから Stop ノードへのエッジを挿入した後の、変更されたグラフを見てください。
+
+![グラフ8](./graph8.svg)
+
+実装は [`LoopNode.forceExit()`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L57) にあります。後退エッジの支配ノードの鎖をたどり、出口を探します。出口がなければ Never ノードと、決して通らない return 経路を挿入し、スケジューラの逆向きの探索でループに到達できるようにします。
+
+## 支配ノード
+
+Simple では支配ノードを段階的に計算します。ピープホール最適化でグラフの一部を削除することはあっても、新しい制御構造を導入することはないという性質を利用します。これにより、以下の単純な方法を使えます。
+
+CFG ノードはすべての制御ノードの基底クラスです。`_idepth` を通して、支配深度の保守的な近似を保持します。このフィールドは直近の支配ノードの深度を表すキャッシュ値です。初期値 `0` は未計算を意味し、要求時に深度を計算します。
+
+デフォルトの計算とキャッシュは [`CFGNode.idepth()`](../../src/main/java/com/seaofnodes/simple/node/CFGNode.java#L51) を参照してください。[`RegionNode`](../../src/main/java/com/seaofnodes/simple/node/RegionNode.java#L94)、[`LoopNode`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L31)、[`StartNode`](../../src/main/java/com/seaofnodes/simple/node/StartNode.java#L36)、[`StopNode`](../../src/main/java/com/seaofnodes/simple/node/StopNode.java#L58) のオーバーライドは、合流、ループ入口、グラフの端点を扱います。
+
+制御フローグラフの一部を削除すると、`_idepth` に飛びが生じますが、支配木を下るにつれて `_idepth` の値が増えるという不変条件は依然として正しく反映されます。
+
+最初の計算時にキャッシュする支配深度とともに、直近の支配ノードを得るメソッドを提供します。この値は、ある時点でのみ有効であり、グラフが変わると無効になるためキャッシュしません。
+
+[`CFGNode.idom()` と `domLCA()`](../../src/main/java/com/seaofnodes/simple/node/CFGNode.java#L60) は、デフォルトの直近の支配ノードと、2つの支配ノードの最小共通祖先を見つける探索を提供します。[`RegionNode.idom()`](../../src/main/java/com/seaofnodes/simple/node/RegionNode.java#L103) は流入する経路を組み合わせ、[`LoopNode.idom()`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L33) はループ入口を使います。Start と Stop に直近の支配ノードはありません。
+
+## ループ深度
+
+Simple の Sea of Nodes グラフは、Loop ノードによってループを明示的に識別します。言語が提供するループの作り方は `while` 文だけなので、汎用的なループ発見処理を実装する必要はありません。
+
+ただし、ループ深度を計算する必要はあります。支配深度の計算と似た方法で行います。
+
+[`CFGNode.loopDepth()`](../../src/main/java/com/seaofnodes/simple/node/CFGNode.java#L78) は制御入力の深度を継承し、[`RegionNode.loopDepth()`](../../src/main/java/com/seaofnodes/simple/node/RegionNode.java#L116) は流入する経路を使います。[`LoopNode.loopDepth()`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L35) は入れ子を1段増やし、後退エッジの支配ノードの鎖をたどってループ出口に印を付けます。[`StartNode`](../../src/main/java/com/seaofnodes/simple/node/StartNode.java#L41) と [`StopNode`](../../src/main/java/com/seaofnodes/simple/node/StopNode.java#L68) は最も外側の深度を 1 にします。
+
+## 早期スケジュール
+
+GCM アルゴリズム本体は、早期スケジュールの計算から始まります。末端(Stop)から各 Node の「入力」を上向きに DFS でたどり、各データノードを、その入力に支配される最初の制御ブロックに配置します。
+
+前提として、前述した方法で無限ループを修正しておく必要があります。
+
+実装は [`GlobalCodeMotion.schedEarly()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L62) から始まります。[`_rpo_cfg()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L85) は CFG の走査順序を構築し、[`_schedEarly()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L94) はデータ入力を再帰的にスケジュールします。制御をスケジュールする前にデータのサイクルへ入ることを避けるため、Phi 入力を通る再帰は飛ばします。
+
+既存の制御および Phi/Proj の束縛は維持します。浮いているノードには、最も深い入力ブロックを最も早い配置として与えます。MemMerge も、疎なエイリアステーブル内の存在しないエントリを無視して、同じ規則に従います。
+
+## 遅延スケジュール
+
+遅延スケジューリングは Stop から始め、ワークリストを使って定義より先に使用を配置します。CFG ノード、Phi、射影、割り当ては固定の配置を与えます。ほかのノードは、自分の使用がスケジュールされるまで待ちます。Load は、自分のエイリアスを上書きしたり合流したりできるメモリユーザーも待ちます。選ぶブロックは、早期配置とすべての使用の共通支配ノードとの間にあり、浅いループを優先し、その次に深い制御フローを優先します。
+
+実装は次の手順に分かれます。
+
+* [`schedLate()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L114) は配置テーブルを管理し、最終的な制御入力を設定します。[`breadth()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L129) はワークリストを駆動し、使用が完了すると定義と待機中のロードを起こします。
+* [`_doSchedLate()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L175) は使用の共通支配ノードを見つけ、ロードの反依存を考慮して、早期配置に向かってたどります。[`better()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L215) を呼び出し、候補ブロックを比較します。
+* [`use_block()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L202) は Phi の使用を、合流そのものではなく、対応する流入制御経路で扱います。
+
+## 反依存の挿入
+
+同じメモリ位置への Load と Store を正しく順序付けるため、以下のように Store から Load へのエッジを挿入します。Store は、先行する Load を待たなければなりません。これらのエッジは、通常 SoN で捉える Def-Use の依存関係を表さず、純粋にスケジューリングの制約として存在するため、反依存と呼びます。
+
+反依存は、遅延スケジュールの実行**中**に計算します。早期スケジュールと、Load をスケジュールする前に得られる Load の使用の遅延スケジュールに依存するためです。
+
+Load から後ろを見て、そのメモリ入力を Start のメモリ射影、メモリ Phi、または Store までたどります。最適化によって、MemMerge を通る Load の精密なスライスはすでに選択されています。そのメモリ定義のユーザーを調べ、Load のエイリアスを覆う Store とメモリ Phi だけを残します。Start の1つのメモリ射影は多数のエイリアスを供給する可能性があるので、メモリ入力を共有するだけで反依存があるとは限りません。
+
+MemMerge はスライスを包むだけで、どれも上書きしません。これを書き込みとして扱うと、Load 自身の結果に依存する集約を Load が待つことになり得ます。Load をスケジュールする準備ができたかどうかの判断と、反依存の計算には、同じエイリアスのフィルタを使います。完了したメモリユーザーは、共有するメモリ定義がすでにスケジュールされていても、待機中のロードを起こします。
+
+`schedLate` の途中なので、Load のすべてのユーザーの遅延スケジュールはすでに計算済みで、Load の使用の*最小共通祖先*、すなわち LCA または遅い位置を持っています。Load に影響し得るメモリ定義の集合を調べ、Store から Load への反依存を追加するか、Load の実効的な LCA を上げます。Phi のメモリ定義では Phi の入力を調べ、そのブロックに実効的な使用を置きます。これを Load の LCA を上げるために使います。ストアでも同じことを行いますが、Load の LCA と**同じ**ブロックのストアが見つかったら、同一ブロック内の順序を強制する反依存エッジを追加します。
+
+エイリアスのフィルタは [`antiUse()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L224)、Load の候補ブロックへの印付けと競合するメモリユーザーの調査は [`find_anti_dep()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L233) を参照してください。[`anti_dep()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L260) は、競合する演算の支配区間をたどり、Load の LCA を上げ、必要なら Store から Load へのエッジを追加します。評価器のスケジューラは GCM と独立してその Store を配置する可能性があるため、Store の区間全体をチェックします。
+
+## 動画での解説
+
+実装を詳しくたどるには、以下の動画をご覧ください。
+
+* [Coffee Compiler Club - 2024年6月21日](https://youtu.be/5Po0gxfE7LA?feature=shared)
+* [Coffee Compiler Club - 2024年6月26日](https://www.youtube.com/watch?v=HnN2YzmFb-8&t=0s)
+
+[^1]: Cliff Click. (1995).
+ Global Code Motion Global Value Numbering.
+
+次の[第14章](../chapter14/README.ja.md)では、このスケジュール済みコンパイラに浮動小数点演算と狭い数値型を追加します。
diff --git a/docs/chapter13/README.md b/docs/chapter13/README.md
new file mode 100644
index 00000000..9e3a1d69
--- /dev/null
+++ b/docs/chapter13/README.md
@@ -0,0 +1,375 @@
+# Chapter 13: Global Code Motion
+
+English | [日本語](README.ja.md)
+
+[Previous: Chapter 12](../chapter12/README.md) |
+[Next: Chapter 14](../chapter14/README.md)
+
+This chapter schedules the graphs built in Chapters 10-12, including
+reference fields and lazy memory partitions. It chooses a slot-0 control input
+for movable operations and preserves the ordering of potentially aliasing
+Loads and Stores.
+
+Also in this chapter, we'll be presenting several fairly detailed graph
+algorithms. You might want to brush up on your graph theory!
+
+The grammar is unchanged from [Chapter 12](../chapter12/12-grammar.md).
+
+# Table of Contents
+
+1. [High Level Overview](#high-level-overview)
+2. [Scheduling Walk Through](#scheduling-walk-through)
+3. [Scheduling a Loop](#scheduling-a-loop)
+4. [Components of the Global Code Motion Algorithm](#components-of-the-global-code-motion-algorithm)
+5. [Identification of Basic Blocks in SoN graph](#identification-of-basic-blocks-in-son-graph)
+6. [Handling Infinite Loops](#handling-infinite-loops)
+7. [Dominators](#dominators)
+8. [Loop depth](#loop-depth)
+9. [Early Schedule](#early-schedule)
+10. [Late Schedule](#late-schedule)
+11. [Inserting Anti Dependencies](#inserting-anti-dependencies)
+12. [Video Walk Through](#video-walk-through)
+
+You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter13) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter12...linear-chapter13) it to the previous chapter.
+
+
+The original input source program defines a sequence in which things happen. As
+we parse the program into Sea of Nodes representation and perform various
+optimizations, this sequence is not fully maintained. The optimized Sea of
+Nodes graph is driven by the dependencies between nodes rather than the
+sequence of instructions in the original source program.
+
+Our goal in this chapter is to look at how we can recover a schedule for executing instructions from an
+optimized Sea of Nodes graph. This schedule needs to preserve the semantics of the original source program,
+but is otherwise allowed to reorder the execution of instructions.
+
+Since the scheduling algorithm works across the implicit Basic Blocks in the SoN Graph,
+we call this a Global Code Motion algorithm; the scheduler can move instructions across Basic Block boundaries.
+
+The primary reference for this algorithm is the GCM GVN paper [^1]. However, the implementation in Simple has some
+differences compared to the version described in the paper.
+
+Since this is a complex topic, we will present a high level summary first and then delve into the details.
+
+## High Level Overview
+
+The Sea of Nodes graph has two virtual graphs within it.
+
+* There is a control flow graph, represented by certain node types, such as Start, If, Region, etc.
+* There is a data graph, primarily driven by Def-Use edges between nodes that produce or consume values or memory.
+
+From a scheduling point of view, the control flow graph is fixed, and immovable.
+However, the nodes that consume or produce data values/memory have some flexibility in terms of when they are executed. The goal of the
+scheduling algorithm is to find the best placement of these nodes so that they are both optimum and correct.
+
+At its core, the scheduling algorithm works in two phases:
+
+* Schedule Early - in this phase, we do an upward DFS walk on the "inputs" of each Node, starting from the bottom (Stop). We schedule each data node to the
+ first control block where they are dominated by their inputs.
+* Schedule Late - in this phase we work backwards from Stop, scheduling uses before definitions, and move data nodes to a block between the first block calculated above,
+ and the last control block where they dominate all their uses. The placement is subject to the condition that it is in the shallowest loop nest possible, and is as control dependent as possible.
+ Additionally, the placement of Load instructions must ensure correct ordering between Loads and Stores of the same memory location.
+
+## Scheduling Walk Through
+
+Before we describe the steps in detail, it is instructive to walk through an example and see the changes that occur to the SoN graph during scheduling.
+
+To motivate the discussion, we will use this example program.
+
+```java
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
+```
+
+First let's look at the graph before scheduling.
+
+These walkthrough diagrams show the precise `S.f` chain and the final
+MemMerge consumed by Return. The unchanged default memory comes from Start;
+any redundant bulk Phis have folded away. As in Chapters 10 and 11, the diagrams omit
+control-flow context, including Regions and their Phi bindings. Small,
+detached True/False projections remain where they gate a Load or Store.
+Unlike Chapter 10's schematic source gates, these figures show the actual
+scheduling inputs at each stage. Entry-block and Loop bindings are omitted.
+
+![Graph1](./graph1.svg)
+
+Observe that
+
+* Control nodes are immovable. Most are omitted here.
+* New nodes already have control inputs, and Phis are bound to Regions;
+ these bindings remain fixed even though they are not drawn.
+* The load `.f` and store `.f=` nodes are still "floating": they have no
+ control input at this stage, so scheduling must choose their blocks.
+
+Now, let's look at the graph after we run the early schedule.
+
+![Graph2](./graph2.svg)
+
+
+The early schedule puts the load `.f` and stores `.f=` in the first basic
+block, since their inputs are available there. These entry-block control
+bindings are omitted, so the memory view looks unchanged.
+
+The graph below focuses on memory and data dependencies after late scheduling.
+The control-flow scaffold, including the Region, is omitted. Store #22's
+control input remains visible as the red edge to the standalone True
+projection #8; the Store-to-Load anti-dependency and memory Phis are retained.
+
+![Graph3](./graph3.svg)
+
+The snip below shows the main changes in the graph:
+
+![Store ordered after the earlier load](./graph3-snip.svg)
+
+* The store `.f=` now has an anti-dependency on the load `.f`; this ensures that the store is scheduled after the load, as required by program semantics. We discuss anti-dependencies in detail later.
+* Observe also that the store `.f=` is now bound to the True branch of the If node.
+
+## Scheduling a Loop
+
+We show another example, this time involving a loop.
+
+```java
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+```
+
+The SoN graph prior to scheduling looks like this:
+
+![Graph4](./graph4.svg)
+
+Following early schedule generation, we get:
+
+![Graph5](./graph5.svg)
+
+The early schedule places Store #31 at the loop header (its control binding
+is omitted). Late scheduling moves it to the True branch #15 of
+`if (arg == 5)`, shown by the red control edge:
+
+![Graph6](./graph6.svg)
+
+The final execution schedule shows that the expression `i/3` can be performed outside the loop because it only depends on the
+original value of `arg`, and hence is loop invariant.
+
+## Components of the Global Code Motion Algorithm
+
+We have already alluded to several components of the GCM algorithm in passing above. Here we list them out as well as others we did not mention:
+
+* The SoN graph has implicit basic blocks in the control flow nodes. For the GCM algo, we need to recognize these nodes more explicitly.
+* When a program has an infinite loop, it poses a problem for the algo, as nodes can be unreachable from the Stop node. To work around this issue, we need to discover
+ infinite loops and create a dummy edge connecting the loop to the Stop node.
+* Loops are already identified in the Simple SoN graph, so we do not need a loop discovery step. However, we need to compute the loop depth associated with each CFG node.
+* In [Chapter 6](../chapter06/README.md) we explained the concept of Dominators. Dominators are key to the GCM algo, and we extend our incremental dominator discovery algo to
+ ensure that it meets the requirements of the algo.
+* We already mentioned the two phases of the algo - the Early Scheduling and the Late Scheduling.
+* In addition, we need to add anti-dependency edges between Loads and Stores in certain scenarios to enforce correct execution order.
+* There are a few changes to our Node hierarchy to help us implement the GCM algo more conveniently. These changes do not conceptually alter the Node hierarchy we inherited from the previous chapters.
+
+## Identification of Basic Blocks in SoN graph
+
+The Sea of Nodes graph already captures the program's control flow graph. This information is implicit in the control nodes and edges from control nodes to other types of nodes.
+
+To recap, Control starts at the Start node, via a projection that is bound to the name `$ctrl`. As the control flows in the program, this name binding gets updated, and control is
+passed around, until it reaches the Stop node.
+
+The Basic Block structure of the CFG can be easily constructed by recognizing that certain control nodes start a Basic Block, whereas certain others end a BB.
+
+Here is the list of all control nodes:
+
+| Node | Starts a BB | Ends a BB |
+|--------|-----------------------------------------|-----------|
+| Start | Yes but only via the `$ctrl` projection | No |
+| CProj | Yes if input control is an If node | No |
+| Region | Yes | No |
+| If | No | Yes |
+| Return | No | Yes |
+| Stop | Yes | Yes |
+
+Changes to Node hierarchy in this chapter:
+
+* All control nodes above now extend a base class CFG Node. This allows us to place common functionality of control flow nodes in the base class.
+* The CProj node extends the regular Proj node and is used in following cases:
+ * The `$ctrl` projection off Start
+ * The True and False projections off If.
+* The Never node is a special sub class of If that is used to handle infinite loops as explained later.
+* The XCtrl node represents a dead control.
+
+## Handling Infinite Loops
+
+Here is an example of code that contains an infinite loop:
+
+```java
+while (1) {}
+return 0;
+```
+
+First let's look at the graph resulting from this:
+
+![Graph7](./graph7.svg)
+
+Now, look at the modified graph after we insert an edge from the infinite loop to Stop node.
+
+![Graph8](./graph8.svg)
+
+The implementation is in
+[`LoopNode.forceExit()`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L57).
+It walks the backedge's dominator chain to look for an exit. If none exists,
+it inserts a Never node and a never-taken return path, making the loop
+reachable to the scheduler's backward walk.
+
+## Dominators
+
+In Simple, we compute Dominators incrementally. Our approach relies on the fact that we may delete parts of the graph during peepholes,
+but we never introduce new control structure via peepholes. This allows us to use a simple approach described below.
+
+The CFG node is the base class for all control nodes. It maintains a conservative approximation of dominator depth via `_idepth`. This field is a cached value representing
+the immediate dominator depth. Its initial value is `0`, which signifies that it has not yet been computed. The depth is computed on request.
+
+See [`CFGNode.idepth()`](../../src/main/java/com/seaofnodes/simple/node/CFGNode.java#L51)
+for the default computation and cache. The overrides in
+[`RegionNode`](../../src/main/java/com/seaofnodes/simple/node/RegionNode.java#L94),
+[`LoopNode`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L31),
+[`StartNode`](../../src/main/java/com/seaofnodes/simple/node/StartNode.java#L36), and
+[`StopNode`](../../src/main/java/com/seaofnodes/simple/node/StopNode.java#L58)
+handle merges, loop entries, and the graph's endpoints.
+
+If portions of the control flow graph are deleted, then there will be gaps in the `_idepth`, but it still correctly reflects the
+invariant that the value of `_idepth` increases as we go down the dominator tree.
+
+Alongside the dominator depth, which is cached on first compute, a method is provided to get the immediate Dominator node. This
+value is not cached as it is only valid at a point in time, and is invalidated as the graph changes.
+
+[`CFGNode.idom()` and `domLCA()`](../../src/main/java/com/seaofnodes/simple/node/CFGNode.java#L60)
+provide the default immediate dominator and the walk that finds the least
+common ancestor of two dominators.
+[`RegionNode.idom()`](../../src/main/java/com/seaofnodes/simple/node/RegionNode.java#L103)
+combines the incoming paths; [`LoopNode.idom()`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L33)
+uses the loop entry. Start and Stop have no immediate dominator.
+
+## Loop Depth
+
+Simple's Sea of Nodes graph identifies loops explicitly via Loop nodes. Since the language provides a single way to create a loop, using the `while` statement,
+it is not necessary to implement a generic loop discovery process.
+
+We do however need to compute a loop depth. This is done similarly to how we compute the dominator depth.
+
+[`CFGNode.loopDepth()`](../../src/main/java/com/seaofnodes/simple/node/CFGNode.java#L78)
+inherits depth from its control input, while
+[`RegionNode.loopDepth()`](../../src/main/java/com/seaofnodes/simple/node/RegionNode.java#L116)
+uses an incoming path.
+[`LoopNode.loopDepth()`](../../src/main/java/com/seaofnodes/simple/node/LoopNode.java#L35)
+adds a nesting level and walks the backedge's dominator chain to mark loop exits.
+[`StartNode`](../../src/main/java/com/seaofnodes/simple/node/StartNode.java#L41) and
+[`StopNode`](../../src/main/java/com/seaofnodes/simple/node/StopNode.java#L68)
+establish the outermost depth of 1.
+
+## Early Schedule
+
+The GCM algorithm proper starts with the computation of the early schedule, during which we do an upward DFS walk on the "inputs" of each Node, starting from the bottom (Stop). We schedule each data node to the
+first control block where they are dominated by their inputs.
+
+A pre-condition of this is to ensure that infinite loops have been fixed as described earlier.
+
+The implementation starts in
+[`GlobalCodeMotion.schedEarly()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L62).
+[`_rpo_cfg()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L85)
+builds the CFG traversal order, and
+[`_schedEarly()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L94)
+recursively schedules data inputs. It skips recursion through Phi inputs to
+avoid entering a data cycle before its control has been scheduled.
+
+Existing control and Phi/Proj bindings are preserved. Floating nodes receive
+the deepest input block as their earliest placement. MemMerge follows this
+same rule, ignoring absent entries in its sparse alias table.
+
+## Late Schedule
+
+Late scheduling starts at Stop and uses a worklist to place uses before their
+definitions. CFG nodes, Phis, projections, and allocations provide fixed
+placements. Other nodes wait until their uses have been scheduled; a Load also
+waits for memory users that can overwrite or merge its alias. The chosen block
+lies between the early placement and the common dominator of all uses, favoring
+shallower loops and then deeper control flow.
+
+The implementation is divided into these steps:
+
+* [`schedLate()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L114)
+ manages the placement tables and installs the final control inputs;
+ [`breadth()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L129)
+ drives the worklist and wakes definitions and waiting loads as uses finish.
+* [`_doSchedLate()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L175)
+ finds the common dominator of uses, accounts for load anti-dependencies, and
+ walks toward the early placement. It calls
+ [`better()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L215)
+ to compare candidate blocks.
+* [`use_block()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L202)
+ handles Phi uses at their matching incoming control paths, rather than at
+ the merge itself.
+
+## Inserting Anti Dependencies
+
+To ensure that Loads and Stores to the same memory location are correctly
+ordered, we insert an edge from the Store to the Load as described below: the
+Store must wait for the earlier Load. We call these edges anti-dependencies
+because they do not represent the Def-Use dependency that we normally capture
+in SoN, and are purely present as scheduling constraints.
+
+We compute anti-dependencies DURING running schedule late. This is because we rely on the early-schedule, and the late-schedule of the Load's uses (before scheduling the Load).
+
+Looking backwards from the Load, we follow its memory input to Start's memory
+projection, a memory Phi, or a Store. Optimization has already selected the
+Load's precise slice through any MemMerge. We inspect users of that memory
+definition, keeping only Stores and memory Phis that cover the Load's alias.
+Start's single memory projection may supply many aliases, so sharing a memory
+input alone does not imply an anti-dependency.
+
+MemMerge only packages slices; it does not overwrite any of them. Treating it
+as a write could make a Load wait for an aggregate which itself depends on the
+Load's result. The same alias filter is used both when deciding whether a Load
+is ready to schedule and when computing its anti-dependencies. A completed
+memory user wakes waiting loads even if their shared memory definition has
+already been scheduled.
+
+Since we're in the middle of `schedLate`, we have already computed all the late
+schedules of a Load's users, and we have the Loads *Least Common Ancestor* of
+uses, the LCA or late position. We inspect the set of mem-defs that might
+impact the Load, and either add an anti-dependency from Store to Load, or raise
+the Loads effective LCA. For Phi mem-defs, we look at the Phi inputs and place
+an effective use on that block; this will be used to raise the Load's LCA. For
+stores, we do the same - until/unless we find stores with the SAME block as the
+Load's LCA. Then we add the anti-dependency edge to force ordering within the
+same block.
+
+See [`antiUse()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L224)
+for the alias filter, and
+[`find_anti_dep()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L233)
+for marking the Load's candidate blocks and inspecting competing memory users.
+[`anti_dep()`](../../src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java#L260)
+walks the competing operation's dominator interval, raises the Load's LCA,
+and adds a Store-to-Load edge when required. It checks the Store's full interval
+because the evaluator's scheduler may place that Store independently of GCM.
+
+## Video Walk Through
+
+Please watch the following video for a detailed walk through of the implementation.
+
+* [Coffee Compiler Club - 21 June 2024](https://youtu.be/5Po0gxfE7LA?feature=shared).
+* [Coffee Compiler Club - 26 June 2024](https://www.youtube.com/watch?v=HnN2YzmFb-8&t=0s)
+
+[^1]: Cliff Click. (1995).
+ Global Code Motion Global Value Numbering.
+
+Next, [Chapter 14](../chapter14/README.md) adds floating-point arithmetic and
+narrow numeric types to this scheduled compiler.
diff --git a/docs/chapter13/graph1.gv b/docs/chapter13/graph1.gv
new file mode 100644
index 00000000..4d6ee1fe
--- /dev/null
+++ b/docs/chapter13/graph1.gv
@@ -0,0 +1,79 @@
+digraph chapter13 {
+/*
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">$mem</TD><TD PORT="p2">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ MemMerge [ shape=box label="MemMerge" ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ new9 [ label="new S" ];
+ Con_11 [ label="#2" ];
+ Store12 [ label=".f =" ];
+ new13 [ label="new S" ];
+ Store14 [ label=".f =" ];
+ Load16 [ label=".f" ];
+ If17 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_21 [ label="#1" ];
+ Store22 [ label=".f =" ];
+ Region23 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_224 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Return25 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Region23;Phi_224;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return25[taillabel=0 color=red];
+ new9 -> Start2:p0[taillabel=0 color=green];
+ Store12 -> Start2:p1[taillabel=1 color=blue];
+ Store12 -> new9[taillabel=2];
+ Store12 -> Con_11[taillabel=3];
+ new13 -> Start2:p0[taillabel=0 color=green];
+ Store14 -> Store12[taillabel=1 color=blue];
+ Store14 -> new13[taillabel=2];
+ Store14 -> Con_4[taillabel=3];
+ Load16 -> Store14[taillabel=1 color=blue];
+ Load16 -> new9[taillabel=2];
+ If17 -> Start2:p0[taillabel=0 color=red];
+ If17 -> Start2:p2[taillabel=1];
+ Store22 -> Store14[taillabel=1 color=blue];
+ Store22 -> new9[taillabel=2];
+ Store22 -> Con_21[taillabel=3];
+ Region23 -> If17:p0[taillabel=1 color=red];
+ Region23 -> If17:p1[taillabel=2 color=red];
+ Phi_224 -> Region23 [style=dotted taillabel=0];
+ Phi_224 -> Store22[taillabel=1 color=blue];
+ Phi_224 -> Store14[taillabel=2 color=blue constraint=false];
+ Return25 -> Region23[taillabel=0 color=red];
+ Return25 -> Load16[taillabel=2];
+ Return25 -> MemMerge[taillabel=1 color=blue];
+ MemMerge -> Start2:p1[taillabel=1 color=blue];
+ MemMerge -> Phi_224[taillabel=2 color=blue];
+}
diff --git a/docs/chapter13/graph1.json b/docs/chapter13/graph1.json
new file mode 100644
index 00000000..aac69ce2
--- /dev/null
+++ b/docs/chapter13/graph1.json
@@ -0,0 +1,25 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph1.svg",
+ "step": 1,
+ "roots": [25],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "MemMerge", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 224, "role": "MEM", "label": null}], "proj": null},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 6, "label": "$mem", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 2, "role": "MEM", "label": null}], "proj": {"par": 2, "idx": 1}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 11, "label": "#2", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 12, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 11, "role": "DATA", "label": null}], "proj": null},
+ {"id": 13, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 14, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 12, "role": "MEM", "label": null}, {"idx": 2, "def": 13, "role": "DATA", "label": null}, {"idx": 3, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 16, "label": ".f", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "#1", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 22, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}], "proj": null},
+ {"id": 25, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 1, "role": "MEM", "label": null}, {"idx": 2, "def": 16, "role": "DATA", "label": null}], "proj": null},
+ {"id": 224, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 22, "role": "MEM", "label": null}, {"idx": 2, "def": 14, "role": "MEM", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph1.svg b/docs/chapter13/graph1.svg
new file mode 100644
index 00000000..93a2a03f
--- /dev/null
+++ b/docs/chapter13/graph1.svg
@@ -0,0 +1,123 @@
+<svg aria-label="Compiler graph" width="565" height="766" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 565 766" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e1i1" d="M84,118 L84,536 Q84,544 92,544 L137,544 Q145,544 145,552 L145,582" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[1] → #6 · MEM</title></path><path class="edge MEM" id="e1i2" d="M175,534 L175,582" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[2] → #224 · MEM</title></path><path class="edge MEM" id="e12i1" d="M84,118 L84,130 Q84,138 92,138 L146,138 Q154,138 154,146 L154,166" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#12[1] → #6 · MEM</title></path><path class="edge DATA" id="e12i2" d="M242,118 L242,123 Q242,128 237,128 L186,128 Q178,128 178,136 L178,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#12[2] → #9 · DATA</title></path><path class="edge DATA" id="e12i3" d="M394,118 L394,130 Q394,138 386,138 L210,138 Q202,138 202,146 L202,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#12[3] → #11 · DATA</title></path><path class="edge MEM" id="e14i1" d="M166,222 L166,227 Q166,232 171,232 L298,232 Q306,232 306,240 L306,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#14[1] → #12 · MEM</title></path><path class="edge DATA" id="e14i2" d="M318,222 L318,227 Q318,232 323,232 L324,232 Q330,232 330,238 L330,270" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#14[2] → #13 · DATA</title></path><path class="edge DATA" id="e14i3" d="M470,222 L470,227 Q470,232 465,232 L362,232 Q354,232 354,240 L354,270" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#14[3] → #4 · DATA</title></path><path class="edge MEM" id="e16i1" d="M318,326 L318,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#16[1] → #14 · MEM</title></path><path class="edge DATA" id="e16i2" d="M242,118 L242,123 Q242,128 247,128 L532,128 Q540,128 540,136 L540,328 Q540,336 532,336 L356,336 Q348,336 348,344 L348,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#16[2] → #9 · DATA</title></path><path class="edge MEM" id="e22i1" d="M318,326 L318,338 Q318,346 310,346 L151,346 Q143,346 143,354 L143,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#22[1] → #14 · MEM</title></path><path class="edge DATA" id="e22i2" d="M242,118 L242,123 Q242,128 237,128 L103,128 Q95,128 95,136 L95,348 Q95,356 103,356 L159,356 Q167,356 167,364 L167,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[2] → #9 · DATA</title></path><path class="edge DATA" id="e22i3" d="M166,326 L166,331 Q166,336 171,336 L183,336 Q191,336 191,344 L191,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[3] → #21 · DATA</title></path><path class="edge MEM" id="e25i1" d="M145,638 L145,643 Q145,648 150,648 L280,648 Q288,648 288,656 L288,686" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#25[1] → #1 · MEM</title></path><path class="edge DATA" id="e25i2" d="M318,430 L318,686" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#25[2] → #16 · DATA</title></path><path class="edge MEM" id="e224i1" d="M155,430 L155,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#224[1] → #22 · MEM</title></path><path class="edge MEM" id="e224i2" d="M318,326 L318,338 Q318,346 310,346 L233,346 Q225,346 225,354 L225,432 Q225,440 217,440 L203,440 Q195,440 195,448 L195,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#224[2] → #14 · MEM</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(85,588)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemMerge</tspan><tspan class="node-id">  #1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 MemMerge
+Type not computed
+
+[0] → — DATA
+[1] → #6 MEM
+[2] → #224 MEM</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n2" data-id="2" transform="translate(24,24)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(410,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n6" data-id="6" transform="translate(24,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$mem</tspan><tspan class="node-id">  #6/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $mem
+Projection 1 of #2
+Type not computed
+
+[0] → #2 MEM</title></g><g class="node" id="n9" data-id="9" transform="translate(182,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n11" data-id="11" transform="translate(334,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#2</tspan><tspan class="node-id">  #11</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#11 #2
+Type not computed
+
+</title></g><g class="node" id="n12" data-id="12" transform="translate(106,172)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #12</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#12 .f =
+Type not computed
+
+[0] → — DATA
+[1] → #6 MEM
+[2] → #9 DATA
+[3] → #11 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n13" data-id="13" transform="translate(258,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #13</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#13 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n14" data-id="14" transform="translate(258,276)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #14</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#14 .f =
+Type not computed
+
+[0] → — DATA
+[1] → #12 MEM
+[2] → #13 DATA
+[3] → #4 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n16" data-id="16" transform="translate(258,380)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f</tspan><tspan class="node-id">  #16</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#16 .f
+Type not computed
+
+[0] → — DATA
+[1] → #14 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n21" data-id="21" transform="translate(106,276)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#1</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 #1
+Type not computed
+
+</title></g><g class="node" id="n22" data-id="22" transform="translate(95,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 .f =
+Type not computed
+
+[0] → — DATA
+[1] → #14 MEM
+[2] → #9 DATA
+[3] → #21 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n25" data-id="25" transform="translate(228,692)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #25</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#25 Return
+Type not computed
+
+[0] → — CTRL
+[1] → #1 MEM
+[2] → #16 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n224" data-id="224" transform="translate(115,484)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #224</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#224 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #22 MEM
+[2] → #14 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph2.gv b/docs/chapter13/graph2.gv
new file mode 100644
index 00000000..94d94dae
--- /dev/null
+++ b/docs/chapter13/graph2.gv
@@ -0,0 +1,84 @@
+digraph chapter13 {
+/*
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">$mem</TD><TD PORT="p2">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ MemMerge [ shape=box label="MemMerge" ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ new9 [ label="new S" ];
+ Con_11 [ label="#2" ];
+ Store12 [ label=".f =" ];
+ new13 [ label="new S" ];
+ Store14 [ label=".f =" ];
+ Load16 [ label=".f" ];
+ If17 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_21 [ label="#1" ];
+ Store22 [ label=".f =" ];
+ Region23 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_224 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Return25 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Region23;Phi_224;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return25[taillabel=0 color=red];
+ new9 -> Start2:p0[taillabel=0 color=green];
+ Store12 -> Start2:p0[taillabel=0 color=red];
+ Store12 -> Start2:p1[taillabel=1 color=blue];
+ Store12 -> new9[taillabel=2];
+ Store12 -> Con_11[taillabel=3];
+ new13 -> Start2:p0[taillabel=0 color=green];
+ Store14 -> Start2:p0[taillabel=0 color=red];
+ Store14 -> Store12[taillabel=1 color=blue];
+ Store14 -> new13[taillabel=2];
+ Store14 -> Con_4[taillabel=3];
+ Load16 -> Start2:p0[taillabel=0 color=red];
+ Load16 -> Store14[taillabel=1 color=blue];
+ Load16 -> new9[taillabel=2];
+ If17 -> Start2:p0[taillabel=0 color=red];
+ If17 -> Start2:p2[taillabel=1];
+ Store22 -> Start2:p0[taillabel=0 color=red];
+ Store22 -> Store14[taillabel=1 color=blue];
+ Store22 -> new9[taillabel=2];
+ Store22 -> Con_21[taillabel=3];
+ Region23 -> If17:p0[taillabel=1 color=red];
+ Region23 -> If17:p1[taillabel=2 color=red];
+ Phi_224 -> Region23 [style=dotted taillabel=0];
+ Phi_224 -> Store22[taillabel=1 color=blue];
+ Phi_224 -> Store14[taillabel=2 color=blue constraint=false];
+ Return25 -> Region23[taillabel=0 color=red];
+ Return25 -> Load16[taillabel=2];
+ Return25 -> MemMerge[taillabel=1 color=blue];
+ MemMerge -> Start2:p1[taillabel=1 color=blue];
+ MemMerge -> Phi_224[taillabel=2 color=blue];
+ MemMerge -> Region23[taillabel=0 color=green];
+}
diff --git a/docs/chapter13/graph2.json b/docs/chapter13/graph2.json
new file mode 100644
index 00000000..47689c72
--- /dev/null
+++ b/docs/chapter13/graph2.json
@@ -0,0 +1,25 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph2.svg",
+ "step": 1,
+ "roots": [25],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "MemMerge", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 224, "role": "MEM", "label": null}], "proj": null},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 6, "label": "$mem", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 2, "role": "MEM", "label": null}], "proj": {"par": 2, "idx": 1}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 11, "label": "#2", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 12, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 11, "role": "DATA", "label": null}], "proj": null},
+ {"id": 13, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 14, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 12, "role": "MEM", "label": null}, {"idx": 2, "def": 13, "role": "DATA", "label": null}, {"idx": 3, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 16, "label": ".f", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "#1", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 22, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}], "proj": null},
+ {"id": 25, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 1, "role": "MEM", "label": null}, {"idx": 2, "def": 16, "role": "DATA", "label": null}], "proj": null},
+ {"id": 224, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 22, "role": "MEM", "label": null}, {"idx": 2, "def": 14, "role": "MEM", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph2.svg b/docs/chapter13/graph2.svg
new file mode 100644
index 00000000..05210749
--- /dev/null
+++ b/docs/chapter13/graph2.svg
@@ -0,0 +1,123 @@
+<svg aria-label="Compiler graph" width="565" height="766" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 565 766" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e1i1" d="M84,118 L84,536 Q84,544 92,544 L137,544 Q145,544 145,552 L145,582" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[1] → #6 · MEM</title></path><path class="edge MEM" id="e1i2" d="M175,534 L175,582" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[2] → #224 · MEM</title></path><path class="edge MEM" id="e12i1" d="M84,118 L84,130 Q84,138 92,138 L146,138 Q154,138 154,146 L154,166" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#12[1] → #6 · MEM</title></path><path class="edge DATA" id="e12i2" d="M242,118 L242,123 Q242,128 237,128 L186,128 Q178,128 178,136 L178,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#12[2] → #9 · DATA</title></path><path class="edge DATA" id="e12i3" d="M394,118 L394,130 Q394,138 386,138 L210,138 Q202,138 202,146 L202,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#12[3] → #11 · DATA</title></path><path class="edge MEM" id="e14i1" d="M166,222 L166,227 Q166,232 171,232 L298,232 Q306,232 306,240 L306,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#14[1] → #12 · MEM</title></path><path class="edge DATA" id="e14i2" d="M318,222 L318,227 Q318,232 323,232 L324,232 Q330,232 330,238 L330,270" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#14[2] → #13 · DATA</title></path><path class="edge DATA" id="e14i3" d="M470,222 L470,227 Q470,232 465,232 L362,232 Q354,232 354,240 L354,270" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#14[3] → #4 · DATA</title></path><path class="edge MEM" id="e16i1" d="M318,326 L318,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#16[1] → #14 · MEM</title></path><path class="edge DATA" id="e16i2" d="M242,118 L242,123 Q242,128 247,128 L532,128 Q540,128 540,136 L540,328 Q540,336 532,336 L356,336 Q348,336 348,344 L348,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#16[2] → #9 · DATA</title></path><path class="edge MEM" id="e22i1" d="M318,326 L318,338 Q318,346 310,346 L151,346 Q143,346 143,354 L143,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#22[1] → #14 · MEM</title></path><path class="edge DATA" id="e22i2" d="M242,118 L242,123 Q242,128 237,128 L103,128 Q95,128 95,136 L95,348 Q95,356 103,356 L159,356 Q167,356 167,364 L167,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[2] → #9 · DATA</title></path><path class="edge DATA" id="e22i3" d="M166,326 L166,331 Q166,336 171,336 L183,336 Q191,336 191,344 L191,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[3] → #21 · DATA</title></path><path class="edge MEM" id="e25i1" d="M145,638 L145,643 Q145,648 150,648 L280,648 Q288,648 288,656 L288,686" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#25[1] → #1 · MEM</title></path><path class="edge DATA" id="e25i2" d="M318,430 L318,686" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#25[2] → #16 · DATA</title></path><path class="edge MEM" id="e224i1" d="M155,430 L155,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#224[1] → #22 · MEM</title></path><path class="edge MEM" id="e224i2" d="M318,326 L318,338 Q318,346 310,346 L233,346 Q225,346 225,354 L225,432 Q225,440 217,440 L203,440 Q195,440 195,448 L195,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#224[2] → #14 · MEM</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(85,588)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemMerge</tspan><tspan class="node-id">  #1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 MemMerge
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #224 MEM</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n2" data-id="2" transform="translate(24,24)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(410,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n6" data-id="6" transform="translate(24,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$mem</tspan><tspan class="node-id">  #6/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $mem
+Projection 1 of #2
+Type not computed
+
+[0] → #2 MEM</title></g><g class="node" id="n9" data-id="9" transform="translate(182,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n11" data-id="11" transform="translate(334,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#2</tspan><tspan class="node-id">  #11</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#11 #2
+Type not computed
+
+</title></g><g class="node" id="n12" data-id="12" transform="translate(106,172)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #12</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#12 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #9 DATA
+[3] → #11 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n13" data-id="13" transform="translate(258,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #13</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#13 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n14" data-id="14" transform="translate(258,276)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #14</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#14 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #12 MEM
+[2] → #13 DATA
+[3] → #4 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n16" data-id="16" transform="translate(258,380)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f</tspan><tspan class="node-id">  #16</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#16 .f
+Type not computed
+
+[0] → — CTRL
+[1] → #14 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n21" data-id="21" transform="translate(106,276)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#1</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 #1
+Type not computed
+
+</title></g><g class="node" id="n22" data-id="22" transform="translate(95,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #14 MEM
+[2] → #9 DATA
+[3] → #21 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n25" data-id="25" transform="translate(228,692)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #25</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#25 Return
+Type not computed
+
+[0] → — CTRL
+[1] → #1 MEM
+[2] → #16 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n224" data-id="224" transform="translate(115,484)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #224</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#224 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #22 MEM
+[2] → #14 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph3-snip.jpg b/docs/chapter13/graph3-snip.jpg
new file mode 100644
index 00000000..804a32c4
--- /dev/null
+++ b/docs/chapter13/graph3-snip.jpg
Binary files differ
diff --git a/docs/chapter13/graph3-snip.json b/docs/chapter13/graph3-snip.json
new file mode 100644
index 00000000..548dbdf3
--- /dev/null
+++ b/docs/chapter13/graph3-snip.json
@@ -0,0 +1,17 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph3-snip.svg",
+ "step": 1,
+ "roots": [22],
+ "scope": 0,
+ "nodes": [
+ {"id": 8, "label": "True", "type": null, "kind": "CTRL", "edges": [], "proj": {"par": 0, "idx": 0}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 14, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 0, "role": "MEM", "label": null}, {"idx": 2, "def": 0, "role": "DATA", "label": null}, {"idx": 3, "def": 0, "role": "DATA", "label": null}], "proj": null},
+ {"id": 16, "label": ".f", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "#1", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 22, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 8, "role": "CTRL", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}, {"idx": 4, "def": 16, "role": "DATA", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph3-snip.svg b/docs/chapter13/graph3-snip.svg
new file mode 100644
index 00000000..9f5218af
--- /dev/null
+++ b/docs/chapter13/graph3-snip.svg
@@ -0,0 +1,91 @@
+<svg aria-label="Compiler graph" width="427" height="314" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 427 314" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e16i1" d="M109,80 L109,92 Q109,100 117,100 L335,100 Q343,100 343,108 L343,128" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#16[1] → #14 · MEM</title></path><path class="edge DATA" id="e16i2" d="M261,80 L261,85 Q261,90 266,90 L365,90 Q373,90 373,98 L373,128" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#16[2] → #9 · DATA</title></path><path class="edge CTRL" id="e22i0" d="M62,184 L62,234" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#22[0] → #8 · CTRL</title></path><path class="edge MEM" id="e22i1" d="M109,80 L109,186 Q109,194 101,194 L90,194 Q82,194 82,202 L82,234" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#22[1] → #14 · MEM</title></path><path class="edge DATA" id="e22i2" d="M261,80 L261,85 Q261,90 256,90 L128,90 Q120,90 120,98 L120,196 Q120,204 112,204 L110,204 Q102,204 102,212 L102,234" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[2] → #9 · DATA</title></path><path class="edge DATA" id="e22i3" d="M191,184 L191,206 Q191,214 183,214 L130,214 Q122,214 122,222 L122,234" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[3] → #21 · DATA</title></path><path class="edge DATA" id="e22i4" d="M343,184 L343,216 Q343,224 335,224 L147,224 Q142,224 142,229 L142,234" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[4] → #16 · DATA</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n8" data-id="8" transform="translate(24,152)" style="opacity: 1;"><path class="box" d="M0,0H75V26H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="37.5" y="17"><tspan>True</tspan><tspan class="node-id">  #8/0</tspan></text><text class="type" text-anchor="middle" y="35" x="37.5"/><title>#8 True
+Projection 0 of #0
+Type not computed
+
+</title></g><g class="node" id="n9" data-id="9" transform="translate(201,30)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n14" data-id="14" transform="translate(49,30)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #14</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#14 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → — MEM
+[2] → — DATA
+[3] → — DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="white"/><circle class="port" cx="72" cy="-3" r="3" fill="white"/><circle class="port" cx="96" cy="-3" r="3" fill="white"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n16" data-id="16" transform="translate(283,134)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f</tspan><tspan class="node-id">  #16</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#16 .f
+Type not computed
+
+[0] → — CTRL
+[1] → #14 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n21" data-id="21" transform="translate(131,134)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#1</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 #1
+Type not computed
+
+</title></g><g class="node" id="n22" data-id="22" transform="translate(42,240)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 .f =
+Type not computed
+
+[0] → #8 CTRL
+[1] → #14 MEM
+[2] → #9 DATA
+[3] → #21 DATA
+[4] → #16 DATA</title><circle class="port" cx="20" cy="-3" r="3" fill="#536477"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><circle class="port" cx="100" cy="-3" r="3" fill="#536477"/><text class="slot" x="20" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="60" y="-10" text-anchor="middle">2</text><text class="slot" x="80" y="-10" text-anchor="middle">3</text><text class="slot" x="100" y="-10" text-anchor="middle">4</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph3.gv b/docs/chapter13/graph3.gv
new file mode 100644
index 00000000..53c9aafe
--- /dev/null
+++ b/docs/chapter13/graph3.gv
@@ -0,0 +1,85 @@
+digraph chapter13 {
+/*
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">$mem</TD><TD PORT="p2">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ MemMerge [ shape=box label="MemMerge" ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ new9 [ label="new S" ];
+ Con_11 [ label="#2" ];
+ Store12 [ label=".f =" ];
+ new13 [ label="new S" ];
+ Store14 [ label=".f =" ];
+ Load16 [ label=".f" ];
+ If17 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_21 [ label="#1" ];
+ Store22 [ label=".f =" ];
+ Region23 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_224 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Return25 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Region23;Phi_224;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return25[taillabel=0 color=red];
+ new9 -> Start2:p0[taillabel=0 color=green];
+ Store12 -> Start2:p0[taillabel=0 color=red];
+ Store12 -> Start2:p1[taillabel=1 color=blue];
+ Store12 -> new9[taillabel=2];
+ Store12 -> Con_11[taillabel=3];
+ new13 -> Start2:p0[taillabel=0 color=green];
+ Store14 -> Start2:p0[taillabel=0 color=red];
+ Store14 -> Store12[taillabel=1 color=blue];
+ Store14 -> new13[taillabel=2];
+ Store14 -> Con_4[taillabel=3];
+ Load16 -> Start2:p0[taillabel=0 color=red];
+ Load16 -> Store14[taillabel=1 color=blue];
+ Load16 -> new9[taillabel=2];
+ If17 -> Start2:p0[taillabel=0 color=red];
+ If17 -> Start2:p2[taillabel=1];
+ Store22 -> If17:p0[taillabel=0 color=red];
+ Store22 -> Store14[taillabel=1 color=blue];
+ Store22 -> new9[taillabel=2];
+ Store22 -> Con_21[taillabel=3];
+ Store22 -> Load16[taillabel=4];
+ Region23 -> If17:p0[taillabel=1 color=red];
+ Region23 -> If17:p1[taillabel=2 color=red];
+ Phi_224 -> Region23 [style=dotted taillabel=0];
+ Phi_224 -> Store22[taillabel=1 color=blue];
+ Phi_224 -> Store14[taillabel=2 color=blue constraint=false];
+ Return25 -> Region23[taillabel=0 color=red];
+ Return25 -> Load16[taillabel=2];
+ Return25 -> MemMerge[taillabel=1 color=blue];
+ MemMerge -> Start2:p1[taillabel=1 color=blue];
+ MemMerge -> Phi_224[taillabel=2 color=blue];
+ MemMerge -> Region23[taillabel=0 color=green];
+}
diff --git a/docs/chapter13/graph3.json b/docs/chapter13/graph3.json
new file mode 100644
index 00000000..e44eb1b1
--- /dev/null
+++ b/docs/chapter13/graph3.json
@@ -0,0 +1,26 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph3.svg",
+ "step": 1,
+ "roots": [25],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "MemMerge", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 224, "role": "MEM", "label": null}], "proj": null},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 6, "label": "$mem", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 2, "role": "MEM", "label": null}], "proj": {"par": 2, "idx": 1}},
+ {"id": 8, "label": "True", "type": null, "kind": "CTRL", "edges": [], "proj": {"par": 0, "idx": 0}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 11, "label": "#2", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 12, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 11, "role": "DATA", "label": null}], "proj": null},
+ {"id": 13, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 14, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 12, "role": "MEM", "label": null}, {"idx": 2, "def": 13, "role": "DATA", "label": null}, {"idx": 3, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 16, "label": ".f", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "#1", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 22, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 8, "role": "CTRL", "label": null}, {"idx": 1, "def": 14, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}, {"idx": 4, "def": 16, "role": "DATA", "label": null}], "proj": null},
+ {"id": 25, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 1, "role": "MEM", "label": null}, {"idx": 2, "def": 16, "role": "DATA", "label": null}], "proj": null},
+ {"id": 224, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 22, "role": "MEM", "label": null}, {"idx": 2, "def": 14, "role": "MEM", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph3.svg b/docs/chapter13/graph3.svg
new file mode 100644
index 00000000..0247b5c6
--- /dev/null
+++ b/docs/chapter13/graph3.svg
@@ -0,0 +1,128 @@
+<svg aria-label="Compiler graph" width="661" height="872" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 661 872" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e1i1" d="M84,118 L84,642 Q84,650 92,650 L387,650 Q395,650 395,658 L395,688" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[1] → #6 · MEM</title></path><path class="edge MEM" id="e1i2" d="M425,640 L425,688" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[2] → #224 · MEM</title></path><path class="edge MEM" id="e12i1" d="M84,118 L84,140 Q84,148 92,148 L242,148 Q250,148 250,156 L250,166" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#12[1] → #6 · MEM</title></path><path class="edge DATA" id="e12i2" d="M274,118 L274,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#12[2] → #9 · DATA</title></path><path class="edge DATA" id="e12i3" d="M426,118 L426,123 Q426,128 421,128 L306,128 Q298,128 298,136 L298,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#12[3] → #11 · DATA</title></path><path class="edge MEM" id="e14i1" d="M262,222 L262,227 Q262,232 267,232 L382,232 Q390,232 390,240 L390,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#14[1] → #12 · MEM</title></path><path class="edge DATA" id="e14i2" d="M414,222 L414,270" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#14[2] → #13 · DATA</title></path><path class="edge DATA" id="e14i3" d="M566,222 L566,227 Q566,232 561,232 L446,232 Q438,232 438,240 L438,270" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#14[3] → #4 · DATA</title></path><path class="edge MEM" id="e16i1" d="M402,326 L402,340 Q402,346 408,346 L408,346 Q414,346 414,352 L414,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#16[1] → #14 · MEM</title></path><path class="edge DATA" id="e16i2" d="M274,118 L274,130 Q274,138 282,138 L628,138 Q636,138 636,146 L636,328 Q636,336 628,336 L452,336 Q444,336 444,344 L444,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#16[2] → #9 · DATA</title></path><path class="edge CTRL" id="e22i0" d="M133,430 L133,480" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#22[0] → #8 · CTRL</title></path><path class="edge MEM" id="e22i1" d="M402,326 L402,338 Q402,346 394,346 L188,346 Q180,346 180,354 L180,432 Q180,440 172,440 L161,440 Q153,440 153,448 L153,480" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#22[1] → #14 · MEM</title></path><path class="edge DATA" id="e22i2" d="M274,118 L274,130 Q274,138 266,138 L199,138 Q191,138 191,146 L191,442 Q191,450 183,450 L181,450 Q173,450 173,458 L173,480" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[2] → #9 · DATA</title></path><path class="edge DATA" id="e22i3" d="M262,430 L262,452 Q262,460 254,460 L201,460 Q193,460 193,468 L193,480" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[3] → #21 · DATA</title></path><path class="edge DATA" id="e22i4" d="M414,430 L414,462 Q414,470 406,470 L218,470 Q213,470 213,475 L213,480" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#22[4] → #16 · DATA</title></path><path class="edge MEM" id="e25i1" d="M395,744 L395,749 Q395,754 400,754 L457,754 Q465,754 465,762 L465,792" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#25[1] → #1 · MEM</title></path><path class="edge DATA" id="e25i2" d="M414,430 L414,462 Q414,470 422,470 L487,470 Q495,470 495,478 L495,792" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#25[2] → #16 · DATA</title></path><path class="edge MEM" id="e224i1" d="M173,536 L173,541 Q173,546 178,546 L397,546 Q405,546 405,554 L405,584" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#224[1] → #22 · MEM</title></path><path class="edge MEM" id="e224i2" d="M402,326 L402,338 Q402,346 410,346 L476,346 Q484,346 484,354 L484,432 Q484,440 476,440 L453,440 Q445,440 445,448 L445,584" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#224[2] → #14 · MEM</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(335,694)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemMerge</tspan><tspan class="node-id">  #1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 MemMerge
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #224 MEM</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n2" data-id="2" transform="translate(24,24)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(506,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n6" data-id="6" transform="translate(24,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$mem</tspan><tspan class="node-id">  #6/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $mem
+Projection 1 of #2
+Type not computed
+
+[0] → #2 MEM</title></g><g class="node" id="n8" data-id="8" transform="translate(95,398)" style="opacity: 1;"><path class="box" d="M0,0H75V26H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="37.5" y="17"><tspan>True</tspan><tspan class="node-id">  #8/0</tspan></text><text class="type" text-anchor="middle" y="35" x="37.5"/><title>#8 True
+Projection 0 of #0
+Type not computed
+
+</title></g><g class="node" id="n9" data-id="9" transform="translate(214,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n11" data-id="11" transform="translate(366,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#2</tspan><tspan class="node-id">  #11</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#11 #2
+Type not computed
+
+</title></g><g class="node" id="n12" data-id="12" transform="translate(202,172)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #12</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#12 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #9 DATA
+[3] → #11 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n13" data-id="13" transform="translate(354,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #13</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#13 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n14" data-id="14" transform="translate(342,276)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #14</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#14 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #12 MEM
+[2] → #13 DATA
+[3] → #4 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n16" data-id="16" transform="translate(354,380)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f</tspan><tspan class="node-id">  #16</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#16 .f
+Type not computed
+
+[0] → — CTRL
+[1] → #14 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n21" data-id="21" transform="translate(202,380)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#1</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 #1
+Type not computed
+
+</title></g><g class="node" id="n22" data-id="22" transform="translate(113,486)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 .f =
+Type not computed
+
+[0] → #8 CTRL
+[1] → #14 MEM
+[2] → #9 DATA
+[3] → #21 DATA
+[4] → #16 DATA</title><circle class="port" cx="20" cy="-3" r="3" fill="#536477"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><circle class="port" cx="100" cy="-3" r="3" fill="#536477"/><text class="slot" x="20" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="60" y="-10" text-anchor="middle">2</text><text class="slot" x="80" y="-10" text-anchor="middle">3</text><text class="slot" x="100" y="-10" text-anchor="middle">4</text></g><g class="node" id="n25" data-id="25" transform="translate(405,798)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #25</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#25 Return
+Type not computed
+
+[0] → — CTRL
+[1] → #1 MEM
+[2] → #16 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n224" data-id="224" transform="translate(365,590)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #224</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#224 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #22 MEM
+[2] → #14 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph4.gv b/docs/chapter13/graph4.gv
new file mode 100644
index 00000000..d7082072
--- /dev/null
+++ b/docs/chapter13/graph4.gv
@@ -0,0 +1,110 @@
+digraph chapter13 {
+/*
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">$mem</TD><TD PORT="p2">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ MemMerge [ shape=box label="MemMerge" ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ new9 [ label="new S" ];
+ Store10 [ label=".f =" ];
+ Loop11 [ shape=box style=filled fillcolor=yellow label="Loop" ];
+ Phi_arg13 [ style=filled fillcolor=lightyellow label="&phi;_arg" ];
+ LT14 [ label="<" ];
+ If16 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Div21 [ label="//" ];
+ Con_22 [ label="#3" ];
+ EQ23 [ label="==" ];
+ Con_24 [ label="#5" ];
+ If25 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Phi_230 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Store31 [ label=".f =" ];
+ Region32 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_233 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Sub35 [ label="-" ];
+ Con_36 [ label="#1" ];
+ Return37 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Loop11;Phi_230;Phi_arg13;}
+ { rank=same; Region32;Phi_233;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return37[taillabel=0 color=red];
+ new9 -> Start2:p0[taillabel=0 color=green];
+ Store10 -> Start2:p1[taillabel=1 color=blue];
+ Store10 -> new9[taillabel=2];
+ Store10 -> Con_4[taillabel=3];
+ Loop11 -> Start2:p0[taillabel=1 color=red];
+ Loop11 -> Region32[taillabel=2 color=red constraint=false];
+ Phi_arg13 -> Loop11 [style=dotted taillabel=0];
+ Phi_arg13 -> Start2:p2[taillabel=1];
+ Phi_arg13 -> Sub35[taillabel=2 constraint=false];
+ LT14 -> Con_4[taillabel=1];
+ LT14 -> Phi_arg13[taillabel=2];
+ If16 -> Loop11[taillabel=0 color=red];
+ If16 -> LT14[taillabel=1];
+ Div21 -> Start2:p2[taillabel=1];
+ Div21 -> Con_22[taillabel=2];
+ EQ23 -> Phi_arg13[taillabel=1];
+ EQ23 -> Con_24[taillabel=2];
+ If25 -> If16:p0[taillabel=0 color=red];
+ If25 -> EQ23[taillabel=1];
+ Phi_230 -> Loop11 [style=dotted taillabel=0];
+ Phi_230 -> Store10[taillabel=1 color=blue];
+ Phi_230 -> Phi_233[taillabel=2 color=blue constraint=false];
+ Store31 -> Phi_230[taillabel=1 color=blue];
+ Store31 -> new9[taillabel=2];
+ Store31 -> Div21[taillabel=3];
+ Region32 -> If25:p0[taillabel=1 color=red];
+ Region32 -> If25:p1[taillabel=2 color=red];
+ Phi_233 -> Region32 [style=dotted taillabel=0];
+ Phi_233 -> Store31[taillabel=1 color=blue];
+ Phi_233 -> Phi_230[taillabel=2 color=blue constraint=false];
+ Sub35 -> Phi_arg13[taillabel=1];
+ Sub35 -> Con_36[taillabel=2];
+ Return37 -> If16:p1[taillabel=0 color=red];
+ Return37 -> new9[taillabel=2];
+ Return37 -> MemMerge[taillabel=1 color=blue];
+ MemMerge -> Start2:p1[taillabel=1 color=blue];
+ MemMerge -> Phi_230[taillabel=2 color=blue];
+}
diff --git a/docs/chapter13/graph4.json b/docs/chapter13/graph4.json
new file mode 100644
index 00000000..add08b6b
--- /dev/null
+++ b/docs/chapter13/graph4.json
@@ -0,0 +1,24 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph4.svg",
+ "step": 1,
+ "roots": [37],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "MemMerge", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 230, "role": "MEM", "label": null}], "proj": null},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 6, "label": "$mem", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 2, "role": "MEM", "label": null}], "proj": {"par": 2, "idx": 1}},
+ {"id": 7, "label": "arg", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 2, "role": "DATA", "label": null}], "proj": {"par": 2, "idx": 2}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 10, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "//", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 7, "role": "DATA", "label": null}, {"idx": 2, "def": 22, "role": "DATA", "label": null}], "proj": null},
+ {"id": 22, "label": "#3", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 31, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 230, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}], "proj": null},
+ {"id": 37, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 1, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 230, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 10, "role": "MEM", "label": null}, {"idx": 2, "def": 233, "role": "MEM", "label": null}], "proj": null},
+ {"id": 233, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 31, "role": "MEM", "label": null}, {"idx": 2, "def": 230, "role": "MEM", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph4.svg b/docs/chapter13/graph4.svg
new file mode 100644
index 00000000..9ca92879
--- /dev/null
+++ b/docs/chapter13/graph4.svg
@@ -0,0 +1,120 @@
+<svg aria-label="Compiler graph" width="780" height="568" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 780 568" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e1i1" d="M266,118 L266,123 Q266,128 261,128 L193,128 Q185,128 185,136 L185,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[1] → #6 · MEM</title></path><path class="edge MEM" id="e1i2" d="M259,326 L259,338 Q259,346 251,346 L223,346 Q215,346 215,354 L215,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[2] → #230 · MEM</title></path><path class="edge MEM" id="e10i1" d="M266,118 L266,123 Q266,128 261,128 L252,128 Q244,128 244,136 L244,166" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#10[1] → #6 · MEM</title></path><path class="edge DATA" id="e10i2" d="M114,118 L114,130 Q114,138 122,138 L260,138 Q268,138 268,146 L268,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#10[2] → #9 · DATA</title></path><path class="edge DATA" id="e10i3" d="M544,118 L544,123 Q544,128 539,128 L300,128 Q292,128 292,136 L292,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#10[3] → #4 · DATA</title></path><path class="edge DATA" id="e21i1" d="M386,118 L386,130 Q386,138 394,138 L400,138 Q408,138 408,146 L408,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[1] → #7 · DATA</title></path><path class="edge DATA" id="e21i2" d="M696,118 L696,130 Q696,138 688,138 L446,138 Q438,138 438,146 L438,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[2] → #22 · DATA</title></path><path class="edge MEM" id="e31i1" d="M259,326 L259,338 Q259,346 267,346 L306,346 Q314,346 314,354 L314,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#31[1] → #230 · MEM</title></path><path class="edge DATA" id="e31i2" d="M114,118 L114,328 Q114,336 122,336 L330,336 Q338,336 338,344 L338,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#31[2] → #9 · DATA</title></path><path class="edge DATA" id="e31i3" d="M408,222 L408,328 Q408,336 400,336 L370,336 Q362,336 362,344 L362,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#31[3] → #21 · DATA</title></path><path class="edge MEM" id="e37i1" d="M185,430 L185,435 Q185,440 180,440 L92,440 Q84,440 84,448 L84,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#37[1] → #1 · MEM</title></path><path class="edge DATA" id="e37i2" d="M114,118 L114,478" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#37[2] → #9 · DATA</title></path><path class="edge MEM" id="e230i1" d="M256,222 L256,227 Q256,232 251,232 L247,232 Q239,232 239,240 L239,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#230[1] → #10 · MEM</title></path><path class="edge MEM" id="e230i2" d="M326,534 L326,539 Q326,544 331,544 L388,544 Q396,544 396,536 L396,240 Q396,232 388,232 L287,232 Q279,232 279,240 L279,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#230[2] → #233 · MEM</title></path><path class="edge MEM" id="e233i1" d="M326,430 L326,435 Q326,440 321,440 L314,440 Q306,440 306,448 L306,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#233[1] → #31 · MEM</title></path><path class="edge MEM" id="e233i2" d="M259,326 L259,344 Q259,346 257,346 L257,346 Q255,346 255,348 L255,442 Q255,450 263,450 L338,450 Q346,450 346,458 L346,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#233[2] → #230 · MEM</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(125,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemMerge</tspan><tspan class="node-id">  #1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 MemMerge
+Type not computed
+
+[0] → — DATA
+[1] → #6 MEM
+[2] → #230 MEM</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n2" data-id="2" transform="translate(206,24)" style="opacity: 1;"><path class="box" d="M0,0H240V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="120" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="120"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(484,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n6" data-id="6" transform="translate(206,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$mem</tspan><tspan class="node-id">  #6/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $mem
+Projection 1 of #2
+Type not computed
+
+[0] → #2 MEM</title></g><g class="node" id="n7" data-id="7" transform="translate(326,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>arg</tspan><tspan class="node-id">  #7/2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#7 arg
+Projection 2 of #2
+Type not computed
+
+[0] → #2 DATA</title></g><g class="node" id="n9" data-id="9" transform="translate(54,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n10" data-id="10" transform="translate(196,172)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #10</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#10 .f =
+Type not computed
+
+[0] → — DATA
+[1] → #6 MEM
+[2] → #9 DATA
+[3] → #4 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n21" data-id="21" transform="translate(348,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>//</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 //
+Type not computed
+
+[0] → — DATA
+[1] → #7 DATA
+[2] → #22 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n22" data-id="22" transform="translate(636,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#3</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 #3
+Type not computed
+
+</title></g><g class="node" id="n31" data-id="31" transform="translate(266,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #31</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#31 .f =
+Type not computed
+
+[0] → — DATA
+[1] → #230 MEM
+[2] → #9 DATA
+[3] → #21 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n37" data-id="37" transform="translate(24,484)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #37</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#37 Return
+Type not computed
+
+[0] → — CTRL
+[1] → #1 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n230" data-id="230" transform="translate(199,276)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #230</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#230 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #10 MEM
+[2] → #233 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g><g class="node" id="n233" data-id="233" transform="translate(266,484)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #233</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#233 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #31 MEM
+[2] → #230 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph5.gv b/docs/chapter13/graph5.gv
new file mode 100644
index 00000000..8189f6dd
--- /dev/null
+++ b/docs/chapter13/graph5.gv
@@ -0,0 +1,117 @@
+digraph chapter13 {
+/*
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">$mem</TD><TD PORT="p2">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ MemMerge [ shape=box label="MemMerge" ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ new9 [ label="new S" ];
+ Store10 [ label=".f =" ];
+ Loop11 [ shape=box style=filled fillcolor=yellow label="Loop" ];
+ Phi_arg13 [ style=filled fillcolor=lightyellow label="&phi;_arg" ];
+ LT14 [ label="<" ];
+ If16 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Div21 [ label="//" ];
+ Con_22 [ label="#3" ];
+ EQ23 [ label="==" ];
+ Con_24 [ label="#5" ];
+ If25 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Phi_230 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Store31 [ label=".f =" ];
+ Region32 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_233 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Sub35 [ label="-" ];
+ Con_36 [ label="#1" ];
+ Return37 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Loop11;Phi_230;Phi_arg13;}
+ { rank=same; Region32;Phi_233;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return37[taillabel=0 color=red];
+ new9 -> Start2:p0[taillabel=0 color=green];
+ Store10 -> Start2:p0[taillabel=0 color=red];
+ Store10 -> Start2:p1[taillabel=1 color=blue];
+ Store10 -> new9[taillabel=2];
+ Store10 -> Con_4[taillabel=3];
+ Loop11 -> Start2:p0[taillabel=1 color=red];
+ Loop11 -> Region32[taillabel=2 color=red constraint=false];
+ Phi_arg13 -> Loop11 [style=dotted taillabel=0];
+ Phi_arg13 -> Start2:p2[taillabel=1];
+ Phi_arg13 -> Sub35[taillabel=2 constraint=false];
+ LT14 -> Loop11[taillabel=0 color=red];
+ LT14 -> Con_4[taillabel=1];
+ LT14 -> Phi_arg13[taillabel=2];
+ If16 -> Loop11[taillabel=0 color=red];
+ If16 -> LT14[taillabel=1];
+ Div21 -> Start2[taillabel=0 color=red];
+ Div21 -> Start2:p2[taillabel=1];
+ Div21 -> Con_22[taillabel=2];
+ EQ23 -> Loop11[taillabel=0 color=red];
+ EQ23 -> Phi_arg13[taillabel=1];
+ EQ23 -> Con_24[taillabel=2];
+ If25 -> If16:p0[taillabel=0 color=red];
+ If25 -> EQ23[taillabel=1];
+ Phi_230 -> Loop11 [style=dotted taillabel=0];
+ Phi_230 -> Store10[taillabel=1 color=blue];
+ Phi_230 -> Phi_233[taillabel=2 color=blue constraint=false];
+ Store31 -> Loop11[taillabel=0 color=red];
+ Store31 -> Phi_230[taillabel=1 color=blue];
+ Store31 -> new9[taillabel=2];
+ Store31 -> Div21[taillabel=3];
+ Region32 -> If25:p0[taillabel=1 color=red];
+ Region32 -> If25:p1[taillabel=2 color=red];
+ Phi_233 -> Region32 [style=dotted taillabel=0];
+ Phi_233 -> Store31[taillabel=1 color=blue];
+ Phi_233 -> Phi_230[taillabel=2 color=blue constraint=false];
+ Sub35 -> Loop11[taillabel=0 color=red];
+ Sub35 -> Phi_arg13[taillabel=1];
+ Sub35 -> Con_36[taillabel=2];
+ Return37 -> If16:p1[taillabel=0 color=red];
+ Return37 -> new9[taillabel=2];
+ Return37 -> MemMerge[taillabel=1 color=blue];
+ MemMerge -> Start2:p1[taillabel=1 color=blue];
+ MemMerge -> Phi_230[taillabel=2 color=blue];
+ MemMerge -> Loop11[taillabel=0 color=green];
+}
diff --git a/docs/chapter13/graph5.json b/docs/chapter13/graph5.json
new file mode 100644
index 00000000..6b1722f6
--- /dev/null
+++ b/docs/chapter13/graph5.json
@@ -0,0 +1,24 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph5.svg",
+ "step": 1,
+ "roots": [37],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "MemMerge", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 230, "role": "MEM", "label": null}], "proj": null},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 6, "label": "$mem", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 2, "role": "MEM", "label": null}], "proj": {"par": 2, "idx": 1}},
+ {"id": 7, "label": "arg", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 2, "role": "DATA", "label": null}], "proj": {"par": 2, "idx": 2}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 10, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "//", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 7, "role": "DATA", "label": null}, {"idx": 2, "def": 22, "role": "DATA", "label": null}], "proj": null},
+ {"id": 22, "label": "#3", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 31, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 230, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}], "proj": null},
+ {"id": 37, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 1, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 230, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 10, "role": "MEM", "label": null}, {"idx": 2, "def": 233, "role": "MEM", "label": null}], "proj": null},
+ {"id": 233, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 31, "role": "MEM", "label": null}, {"idx": 2, "def": 230, "role": "MEM", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph5.svg b/docs/chapter13/graph5.svg
new file mode 100644
index 00000000..c120550f
--- /dev/null
+++ b/docs/chapter13/graph5.svg
@@ -0,0 +1,120 @@
+<svg aria-label="Compiler graph" width="780" height="568" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 780 568" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e1i1" d="M266,118 L266,123 Q266,128 261,128 L193,128 Q185,128 185,136 L185,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[1] → #6 · MEM</title></path><path class="edge MEM" id="e1i2" d="M259,326 L259,338 Q259,346 251,346 L223,346 Q215,346 215,354 L215,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[2] → #230 · MEM</title></path><path class="edge MEM" id="e10i1" d="M266,118 L266,123 Q266,128 261,128 L252,128 Q244,128 244,136 L244,166" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#10[1] → #6 · MEM</title></path><path class="edge DATA" id="e10i2" d="M114,118 L114,130 Q114,138 122,138 L260,138 Q268,138 268,146 L268,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#10[2] → #9 · DATA</title></path><path class="edge DATA" id="e10i3" d="M544,118 L544,123 Q544,128 539,128 L300,128 Q292,128 292,136 L292,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#10[3] → #4 · DATA</title></path><path class="edge DATA" id="e21i1" d="M386,118 L386,130 Q386,138 394,138 L400,138 Q408,138 408,146 L408,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[1] → #7 · DATA</title></path><path class="edge DATA" id="e21i2" d="M696,118 L696,130 Q696,138 688,138 L446,138 Q438,138 438,146 L438,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[2] → #22 · DATA</title></path><path class="edge MEM" id="e31i1" d="M259,326 L259,338 Q259,346 267,346 L306,346 Q314,346 314,354 L314,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#31[1] → #230 · MEM</title></path><path class="edge DATA" id="e31i2" d="M114,118 L114,328 Q114,336 122,336 L330,336 Q338,336 338,344 L338,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#31[2] → #9 · DATA</title></path><path class="edge DATA" id="e31i3" d="M408,222 L408,328 Q408,336 400,336 L370,336 Q362,336 362,344 L362,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#31[3] → #21 · DATA</title></path><path class="edge MEM" id="e37i1" d="M185,430 L185,435 Q185,440 180,440 L92,440 Q84,440 84,448 L84,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#37[1] → #1 · MEM</title></path><path class="edge DATA" id="e37i2" d="M114,118 L114,478" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#37[2] → #9 · DATA</title></path><path class="edge MEM" id="e230i1" d="M256,222 L256,227 Q256,232 251,232 L247,232 Q239,232 239,240 L239,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#230[1] → #10 · MEM</title></path><path class="edge MEM" id="e230i2" d="M326,534 L326,539 Q326,544 331,544 L388,544 Q396,544 396,536 L396,240 Q396,232 388,232 L287,232 Q279,232 279,240 L279,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#230[2] → #233 · MEM</title></path><path class="edge MEM" id="e233i1" d="M326,430 L326,435 Q326,440 321,440 L314,440 Q306,440 306,448 L306,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#233[1] → #31 · MEM</title></path><path class="edge MEM" id="e233i2" d="M259,326 L259,344 Q259,346 257,346 L257,346 Q255,346 255,348 L255,442 Q255,450 263,450 L338,450 Q346,450 346,458 L346,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#233[2] → #230 · MEM</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(125,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemMerge</tspan><tspan class="node-id">  #1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 MemMerge
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #230 MEM</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n2" data-id="2" transform="translate(206,24)" style="opacity: 1;"><path class="box" d="M0,0H240V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="120" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="120"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(484,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n6" data-id="6" transform="translate(206,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$mem</tspan><tspan class="node-id">  #6/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $mem
+Projection 1 of #2
+Type not computed
+
+[0] → #2 MEM</title></g><g class="node" id="n7" data-id="7" transform="translate(326,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>arg</tspan><tspan class="node-id">  #7/2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#7 arg
+Projection 2 of #2
+Type not computed
+
+[0] → #2 DATA</title></g><g class="node" id="n9" data-id="9" transform="translate(54,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n10" data-id="10" transform="translate(196,172)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #10</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#10 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #9 DATA
+[3] → #4 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n21" data-id="21" transform="translate(348,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>//</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 //
+Type not computed
+
+[0] → — CTRL
+[1] → #7 DATA
+[2] → #22 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n22" data-id="22" transform="translate(636,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#3</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 #3
+Type not computed
+
+</title></g><g class="node" id="n31" data-id="31" transform="translate(266,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #31</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#31 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #230 MEM
+[2] → #9 DATA
+[3] → #21 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n37" data-id="37" transform="translate(24,484)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #37</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#37 Return
+Type not computed
+
+[0] → — CTRL
+[1] → #1 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n230" data-id="230" transform="translate(199,276)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #230</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#230 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #10 MEM
+[2] → #233 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g><g class="node" id="n233" data-id="233" transform="translate(266,484)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #233</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#233 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #31 MEM
+[2] → #230 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph6.gv b/docs/chapter13/graph6.gv
new file mode 100644
index 00000000..47165fdb
--- /dev/null
+++ b/docs/chapter13/graph6.gv
@@ -0,0 +1,117 @@
+digraph chapter13 {
+/*
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD><TD PORT="p1">$mem</TD><TD PORT="p2">arg</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ MemMerge [ shape=box label="MemMerge" ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ new9 [ label="new S" ];
+ Store10 [ label=".f =" ];
+ Loop11 [ shape=box style=filled fillcolor=yellow label="Loop" ];
+ Phi_arg13 [ style=filled fillcolor=lightyellow label="&phi;_arg" ];
+ LT14 [ label="<" ];
+ If16 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Div21 [ label="//" ];
+ Con_22 [ label="#3" ];
+ EQ23 [ label="==" ];
+ Con_24 [ label="#5" ];
+ If25 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">If</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Phi_230 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Store31 [ label=".f =" ];
+ Region32 [ shape=box style=filled fillcolor=yellow label="Region" ];
+ Phi_233 [ style=filled fillcolor=lightyellow label="MemPhi_2" ];
+ Sub35 [ label="-" ];
+ Con_36 [ label="#1" ];
+ Return37 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ { rank=same; Loop11;Phi_230;Phi_arg13;}
+ { rank=same; Region32;Phi_233;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Stop3 -> Return37[taillabel=0 color=red];
+ new9 -> Start2:p0[taillabel=0 color=green];
+ Store10 -> Start2:p0[taillabel=0 color=red];
+ Store10 -> Start2:p1[taillabel=1 color=blue];
+ Store10 -> new9[taillabel=2];
+ Store10 -> Con_4[taillabel=3];
+ Loop11 -> Start2:p0[taillabel=1 color=red];
+ Loop11 -> Region32[taillabel=2 color=red constraint=false];
+ Phi_arg13 -> Loop11 [style=dotted taillabel=0];
+ Phi_arg13 -> Start2:p2[taillabel=1];
+ Phi_arg13 -> Sub35[taillabel=2 constraint=false];
+ LT14 -> Loop11[taillabel=0 color=red];
+ LT14 -> Con_4[taillabel=1];
+ LT14 -> Phi_arg13[taillabel=2];
+ If16 -> Loop11[taillabel=0 color=red];
+ If16 -> LT14[taillabel=1];
+ Div21 -> Start2:p0[taillabel=0 color=red];
+ Div21 -> Start2:p2[taillabel=1];
+ Div21 -> Con_22[taillabel=2];
+ EQ23 -> If16:p0[taillabel=0 color=red];
+ EQ23 -> Phi_arg13[taillabel=1];
+ EQ23 -> Con_24[taillabel=2];
+ If25 -> If16:p0[taillabel=0 color=red];
+ If25 -> EQ23[taillabel=1];
+ Phi_230 -> Loop11 [style=dotted taillabel=0];
+ Phi_230 -> Store10[taillabel=1 color=blue];
+ Phi_230 -> Phi_233[taillabel=2 color=blue constraint=false];
+ Store31 -> If25:p0[taillabel=0 color=red];
+ Store31 -> Phi_230[taillabel=1 color=blue];
+ Store31 -> new9[taillabel=2];
+ Store31 -> Div21[taillabel=3];
+ Region32 -> If25:p0[taillabel=1 color=red];
+ Region32 -> If25:p1[taillabel=2 color=red];
+ Phi_233 -> Region32 [style=dotted taillabel=0];
+ Phi_233 -> Store31[taillabel=1 color=blue];
+ Phi_233 -> Phi_230[taillabel=2 color=blue constraint=false];
+ Sub35 -> Region32[taillabel=0 color=red];
+ Sub35 -> Phi_arg13[taillabel=1];
+ Sub35 -> Con_36[taillabel=2];
+ Return37 -> If16:p1[taillabel=0 color=red];
+ Return37 -> new9[taillabel=2];
+ Return37 -> MemMerge[taillabel=1 color=blue];
+ MemMerge -> Start2:p1[taillabel=1 color=blue];
+ MemMerge -> Phi_230[taillabel=2 color=blue];
+ MemMerge -> If16:p1[taillabel=0 color=green];
+}
diff --git a/docs/chapter13/graph6.json b/docs/chapter13/graph6.json
new file mode 100644
index 00000000..c63c717f
--- /dev/null
+++ b/docs/chapter13/graph6.json
@@ -0,0 +1,25 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph6.svg",
+ "step": 1,
+ "roots": [37],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "MemMerge", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 230, "role": "MEM", "label": null}], "proj": null},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 6, "label": "$mem", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 2, "role": "MEM", "label": null}], "proj": {"par": 2, "idx": 1}},
+ {"id": 7, "label": "arg", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 2, "role": "DATA", "label": null}], "proj": {"par": 2, "idx": 2}},
+ {"id": 9, "label": "new S", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 10, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 6, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 15, "label": "True", "type": null, "kind": "CTRL", "edges": [], "proj": {"par": 0, "idx": 0}},
+ {"id": 21, "label": "//", "type": null, "kind": "DATA", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 7, "role": "DATA", "label": null}, {"idx": 2, "def": 22, "role": "DATA", "label": null}], "proj": null},
+ {"id": 22, "label": "#3", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 31, "label": ".f =", "type": null, "kind": "MEM", "edges": [{"idx": 0, "def": 15, "role": "CTRL", "label": null}, {"idx": 1, "def": 230, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}, {"idx": 3, "def": 21, "role": "DATA", "label": null}], "proj": null},
+ {"id": 37, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 0, "role": "CTRL", "label": null}, {"idx": 1, "def": 1, "role": "MEM", "label": null}, {"idx": 2, "def": 9, "role": "DATA", "label": null}], "proj": null},
+ {"id": 230, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 10, "role": "MEM", "label": null}, {"idx": 2, "def": 233, "role": "MEM", "label": null}], "proj": null},
+ {"id": 233, "label": "MemPhi_2", "type": null, "kind": "PHI", "edges": [{"idx": 0, "def": 0, "role": "ASSOC", "label": null}, {"idx": 1, "def": 31, "role": "MEM", "label": null}, {"idx": 2, "def": 230, "role": "MEM", "label": null}], "proj": null}
+ ],
+ "groups": [
+ ]
+}
diff --git a/docs/chapter13/graph6.svg b/docs/chapter13/graph6.svg
new file mode 100644
index 00000000..d6dec88f
--- /dev/null
+++ b/docs/chapter13/graph6.svg
@@ -0,0 +1,124 @@
+<svg aria-label="Compiler graph" width="820" height="568" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 820 568" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"/><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge MEM" id="e1i1" d="M306,118 L306,123 Q306,128 301,128 L151,128 Q143,128 143,136 L143,328 Q143,336 135,336 L92,336 Q84,336 84,344 L84,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[1] → #6 · MEM</title></path><path class="edge MEM" id="e1i2" d="M331,326 L331,338 Q331,346 323,346 L122,346 Q114,346 114,354 L114,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#1[2] → #230 · MEM</title></path><path class="edge MEM" id="e10i1" d="M306,118 L306,123 Q306,128 301,128 L270,128 Q262,128 262,136 L262,166" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#10[1] → #6 · MEM</title></path><path class="edge DATA" id="e10i2" d="M154,118 L154,130 Q154,138 162,138 L278,138 Q286,138 286,146 L286,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#10[2] → #9 · DATA</title></path><path class="edge DATA" id="e10i3" d="M584,118 L584,130 Q584,138 576,138 L318,138 Q310,138 310,146 L310,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#10[3] → #4 · DATA</title></path><path class="edge DATA" id="e21i1" d="M426,118 L426,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[1] → #7 · DATA</title></path><path class="edge DATA" id="e21i2" d="M736,118 L736,140 Q736,148 728,148 L464,148 Q456,148 456,156 L456,166" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[2] → #22 · DATA</title></path><path class="edge CTRL" id="e31i0" d="M206,326 L206,374" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#31[0] → #15 · CTRL</title></path><path class="edge MEM" id="e31i1" d="M331,326 L331,338 Q331,346 323,346 L238,346 Q230,346 230,354 L230,374" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#31[1] → #230 · MEM</title></path><path class="edge DATA" id="e31i2" d="M154,118 L154,224 Q154,232 162,232 L249,232 Q257,232 257,240 L257,354.5 Q257,356 255.5,356 L255.5,356 Q254,356 254,357.5 L254,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#31[2] → #9 · DATA</title></path><path class="edge DATA" id="e31i3" d="M426,222 L426,234 Q426,242 418,242 L409,242 Q401,242 401,250 L401,348 Q401,356 393,356 L286,356 Q278,356 278,364 L278,374" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#31[3] → #21 · DATA</title></path><path class="edge MEM" id="e37i1" d="M84,430 L84,435 Q84,440 89,440 L116,440 Q124,440 124,448 L124,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#37[1] → #1 · MEM</title></path><path class="edge DATA" id="e37i2" d="M154,118 L154,478" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#37[2] → #9 · DATA</title></path><path class="edge MEM" id="e230i1" d="M274,222 L274,227 Q274,232 279,232 L303,232 Q311,232 311,240 L311,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#230[1] → #10 · MEM</title></path><path class="edge MEM" id="e230i2" d="M311,534 L311,539 Q311,544 316,544 L404,544 Q412,544 412,536 L412,240 Q412,232 404,232 L359,232 Q351,232 351,240 L351,270" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#230[2] → #233 · MEM</title></path><path class="edge MEM" id="e233i1" d="M242,430 L242,435 Q242,440 247,440 L283,440 Q291,440 291,448 L291,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#233[1] → #31 · MEM</title></path><path class="edge MEM" id="e233i2" d="M331,326 L331,478" stroke="#2867b2" marker-start="url(#arrow-MEM)" style="opacity: 1;"><title>#233[2] → #230 · MEM</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(24,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemMerge</tspan><tspan class="node-id">  #1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 MemMerge
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #230 MEM</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n2" data-id="2" transform="translate(246,24)" style="opacity: 1;"><path class="box" d="M0,0H240V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="120" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="120"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(524,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n6" data-id="6" transform="translate(246,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$mem</tspan><tspan class="node-id">  #6/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $mem
+Projection 1 of #2
+Type not computed
+
+[0] → #2 MEM</title></g><g class="node" id="n7" data-id="7" transform="translate(366,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>arg</tspan><tspan class="node-id">  #7/2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#7 arg
+Projection 2 of #2
+Type not computed
+
+[0] → #2 DATA</title></g><g class="node" id="n9" data-id="9" transform="translate(94,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>new S</tspan><tspan class="node-id">  #9</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#9 new S
+Type not computed
+
+[0] → — CTRL</title><circle class="port" cx="60" cy="-3" r="3" fill="white"/><text class="slot" x="60" y="-10" text-anchor="middle">0</text></g><g class="node" id="n10" data-id="10" transform="translate(214,172)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #10</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#10 .f =
+Type not computed
+
+[0] → — CTRL
+[1] → #6 MEM
+[2] → #9 DATA
+[3] → #4 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="white"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n15" data-id="15" transform="translate(165,294)" style="opacity: 1;"><path class="box" d="M0,0H82V26H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="41" y="17"><tspan>True</tspan><tspan class="node-id">  #15/0</tspan></text><text class="type" text-anchor="middle" y="35" x="41"/><title>#15 True
+Projection 0 of #0
+Type not computed
+
+</title></g><g class="node" id="n21" data-id="21" transform="translate(366,172)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>//</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 //
+Type not computed
+
+[0] → — CTRL
+[1] → #7 DATA
+[2] → #22 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n22" data-id="22" transform="translate(676,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#3</tspan><tspan class="node-id">  #22</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#22 #3
+Type not computed
+
+</title></g><g class="node" id="n31" data-id="31" transform="translate(182,380)" style="opacity: 1;"><path class="box" d="M10,0H110L120,10V34L110,44H10L0,34V10Z" fill="#dcecff"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>.f =</tspan><tspan class="node-id">  #31</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#31 .f =
+Type not computed
+
+[0] → #15 CTRL
+[1] → #230 MEM
+[2] → #9 DATA
+[3] → #21 DATA</title><circle class="port" cx="24" cy="-3" r="3" fill="#536477"/><circle class="port" cx="48" cy="-3" r="3" fill="#536477"/><circle class="port" cx="72" cy="-3" r="3" fill="#536477"/><circle class="port" cx="96" cy="-3" r="3" fill="#536477"/><text class="slot" x="24" y="-10" text-anchor="middle">0</text><text class="slot" x="48" y="-10" text-anchor="middle">1</text><text class="slot" x="72" y="-10" text-anchor="middle">2</text><text class="slot" x="96" y="-10" text-anchor="middle">3</text></g><g class="node" id="n37" data-id="37" transform="translate(64,484)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #37</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#37 Return
+Type not computed
+
+[0] → — CTRL
+[1] → #1 MEM
+[2] → #9 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n230" data-id="230" transform="translate(271,276)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #230</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#230 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #10 MEM
+[2] → #233 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g><g class="node" id="n233" data-id="233" transform="translate(251,484)" style="opacity: 1;"><path class="box" d="M12,0H108L120,22L108,44H12L0,22Z" fill="#f5e4fa"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>MemPhi_2</tspan><tspan class="node-id">  #233</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#233 MemPhi_2
+Type not computed
+
+[0] → — ASSOC
+[1] → #31 MEM
+[2] → #230 MEM</title><circle class="port" cx="0" cy="-3" r="3" fill="white"/><circle class="port" cx="40" cy="-3" r="3" fill="#536477"/><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><text class="slot" x="0" y="-10" text-anchor="middle">0</text><text class="slot" x="40" y="-10" text-anchor="middle">1</text><text class="slot" x="80" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"/><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph7.gv b/docs/chapter13/graph7.gv
new file mode 100644
index 00000000..b6507e53
--- /dev/null
+++ b/docs/chapter13/graph7.gv
@@ -0,0 +1,23 @@
+digraph chapter13 {
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Con_4 [ label="#0" ];
+ Loop8 [ shape=box style=filled fillcolor=yellow label="Loop" ];
+ { rank=same; Loop8;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Loop8 -> Start2:p0[taillabel=1 color=red];
+ Loop8 -> Loop8[taillabel=2 color=red constraint=false];
+}
diff --git a/docs/chapter13/graph7.json b/docs/chapter13/graph7.json
new file mode 100644
index 00000000..0e3efb9f
--- /dev/null
+++ b/docs/chapter13/graph7.json
@@ -0,0 +1,16 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph7.svg",
+ "step": 1,
+ "roots": [],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "$ctrl", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 2, "role": "CTRL", "label": null}], "proj": {"par": 2, "idx": 0}},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 8, "label": "Loop", "type": null, "kind": "LOOP", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 1, "role": "CTRL", "label": null}, {"idx": 2, "def": 8, "role": "CTRL", "label": null}], "proj": null}
+ ],
+ "groups": [
+ {"id": 8, "par": 0, "nodes": [8]}
+ ]
+}
diff --git a/docs/chapter13/graph7.svg b/docs/chapter13/graph7.svg
new file mode 100644
index 00000000..15d886dc
--- /dev/null
+++ b/docs/chapter13/graph7.svg
@@ -0,0 +1,78 @@
+<svg aria-label="Compiler graph" width="364" height="325" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 364 325" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"><g class="region" transform="translate(164,171)"><rect class="region-box" rx="8" width="176" height="130" fill="#f1f6fc"/></g></g><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge CTRL" id="e8i1" d="M236,118 L236,223" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#8[1] → #1 · CTRL</title></path><path class="edge CTRL" id="e8i2" d="M236,276 L236,281 Q236,286 241,286 L320,286 Q328,286 328,278 L328,222 Q328,214 320,214 L272,214 Q266,214 266,220 L266,226" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#8[2] → #8 · CTRL</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(176,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$ctrl</tspan><tspan class="node-id">  #1/0</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 $ctrl
+Projection 0 of #2
+Type not computed
+
+[0] → #2 CTRL</title></g><g class="node" id="n2" data-id="2" transform="translate(176,24)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n4" data-id="4" transform="translate(24,49)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n8" data-id="8" transform="translate(176,229)" style="opacity: 1;"><path class="box" d="M20,0H100Q120,0 120,20V24Q120,44 100,44H20Q0,44 0,24V20Q0,0 20,0Z" fill="#ffe0ab"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Loop</tspan><tspan class="node-id">  #8</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#8 Loop
+Type not computed
+
+[0] → — DATA
+[1] → #1 CTRL
+[2] → #8 CTRL</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"><g class="region-head" transform="translate(164,171)"><text class="label" x="26" y="17"><tspan>Loop</tspan><tspan class="node-id">  #8</tspan></text><title>Loop · 1 nodes</title><g class="fold-btn" role="button" tabindex="0" transform="translate(4,4)" aria-label="Fold region #8"><rect width="16" height="16" rx="2"/><text x="8" y="12" text-anchor="middle">−</text><title>Fold region</title></g></g></g><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/docs/chapter13/graph8.gv b/docs/chapter13/graph8.gv
new file mode 100644
index 00000000..442b6843
--- /dev/null
+++ b/docs/chapter13/graph8.gv
@@ -0,0 +1,45 @@
+digraph chapter13 {
+/*
+while (1) {}
+return 0;
+
+*/
+ rankdir=BT;
+ concentrate="true";
+ compound="true";
+ subgraph cluster_Nodes {
+ Never18 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Never</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">True</TD><TD PORT="p1" BGCOLOR="yellow">False</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Start2 [ shape=plaintext label=<
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
+ <TR><TD BGCOLOR="yellow">Start</TD></TR>
+ <TR><TD>
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
+ <TR><TD PORT="p0" BGCOLOR="yellow">$ctrl</TD></TR>
+ </TABLE>
+ </TD></TR>
+ </TABLE>>
+ ];
+ Stop3 [ shape=box style=filled fillcolor=yellow label="Stop" ];
+ Con_4 [ label="#0" ];
+ Return21 [ shape=box style=filled fillcolor=yellow label="Return" ];
+ Loop8 [ shape=box style=filled fillcolor=yellow label="Loop" ];
+ { rank=same; Loop8;}
+ }
+ edge [ fontname=Helvetica, fontsize=8 ];
+ Never18 -> Loop8[taillabel=0 color=red];
+ Never18 -> Con_4[taillabel=1];
+ Stop3 -> Return21[taillabel=0 color=red];
+ Return21 -> Never18:p0[taillabel=0 color=red];
+ Return21 -> Con_4[taillabel=2];
+ Loop8 -> Start2:p0[taillabel=1 color=red];
+ Loop8 -> Never18:p1[taillabel=2 color=red constraint=false];
+}
diff --git a/docs/chapter13/graph8.json b/docs/chapter13/graph8.json
new file mode 100644
index 00000000..b7d4f41c
--- /dev/null
+++ b/docs/chapter13/graph8.json
@@ -0,0 +1,21 @@
+{
+ "ver": 1,
+ "comp": "chapter13/docs/graph8.svg",
+ "step": 1,
+ "roots": [],
+ "scope": 0,
+ "nodes": [
+ {"id": 1, "label": "True", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 18, "role": "CTRL", "label": null}], "proj": {"par": 18, "idx": 0}},
+ {"id": 2, "label": "Start", "type": null, "kind": "CTRL", "edges": [], "proj": null},
+ {"id": 3, "label": "Stop", "type": null, "kind": "STOP", "edges": [{"idx": 0, "def": 21, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 4, "label": "#0", "type": null, "kind": "DATA", "edges": [], "proj": null},
+ {"id": 5, "label": "False", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 18, "role": "CTRL", "label": null}], "proj": {"par": 18, "idx": 1}},
+ {"id": 6, "label": "$ctrl", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 2, "role": "CTRL", "label": null}], "proj": {"par": 2, "idx": 0}},
+ {"id": 8, "label": "Loop", "type": null, "kind": "LOOP", "edges": [{"idx": 0, "def": 0, "role": "DATA", "label": null}, {"idx": 1, "def": 6, "role": "CTRL", "label": null}, {"idx": 2, "def": 5, "role": "CTRL", "label": null}], "proj": null},
+ {"id": 18, "label": "Never", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 8, "role": "CTRL", "label": null}, {"idx": 1, "def": 4, "role": "DATA", "label": null}], "proj": null},
+ {"id": 21, "label": "Return", "type": null, "kind": "CTRL", "edges": [{"idx": 0, "def": 1, "role": "CTRL", "label": null}, {"idx": 1, "def": 0, "role": "DATA", "label": null}, {"idx": 2, "def": 4, "role": "DATA", "label": null}], "proj": null}
+ ],
+ "groups": [
+ {"id": 8, "par": 0, "nodes": [1, 5, 8, 18]}
+ ]
+}
diff --git a/docs/chapter13/graph8.svg b/docs/chapter13/graph8.svg
new file mode 100644
index 00000000..d96a9a7d
--- /dev/null
+++ b/docs/chapter13/graph8.svg
@@ -0,0 +1,98 @@
+<svg aria-label="Compiler graph" width="431" height="676" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 431 676" style="font-family: sans-serif;"><style>
+ html, body { height:100%; margin:0; font-family:sans-serif; }
+ .viewer { height:100%; display:grid; grid-template-rows:auto minmax(0,1fr) auto; }
+ .source { padding:8px; border-bottom:1px solid #ccc; }
+ #program { width:min(600px,80vw); vertical-align:middle; }
+ #status { margin-top:4px; }
+ #graph { min-height:0; overflow:hidden; position:relative; background:#fafbfc; }
+ #elk { width:100%; height:100%; }
+ #graph svg { display:block; }
+ .tools { display:flex; flex-wrap:wrap; align-items:center; gap:14px; margin-top:8px; font-size:12px; }
+ .hint { color:#616c78; }
+ .ctrl { color:#b63838; } .mem { color:#2867b2; } .data { color:#536477; }
+ .edge { fill:none; stroke-width:1.5; }
+ .edge.CTRL { stroke-width:2; }
+ .edge.ASSOC { stroke-dasharray:4 4; }
+ .edge.PARSER { stroke-dasharray:5 4; }
+ .parser rect { fill:#f6f0ff; stroke:#7956a5; stroke-width:1.5; stroke-dasharray:5 4; }
+ .edge.selected { stroke-width:3.5; }
+ .edge.bundle { stroke-width:4; }
+ .edge.bundle.selected { stroke-width:6; }
+ .edge-hit { fill:none; stroke:transparent; stroke-width:12; vector-effect:non-scaling-stroke; pointer-events:stroke; cursor:pointer; }
+ .edge-jump { fill:transparent; pointer-events:all; cursor:pointer; }
+ .node { cursor:pointer; }
+ .box { stroke:#758191; stroke-width:1; }
+ .node.held .box { stroke-dasharray:5 3; }
+ .node.near .box { stroke:#c18221; stroke-width:2; }
+ .node.focus .box { stroke:#9c4900; stroke-width:3; }
+ .node.selected .box { stroke:#1569b9; stroke-width:3; }
+ .node.folded .box { stroke-width:4; }
+ .node.folding .box { fill:#f4ccd7; }
+ .fold-tag { font-size:9px; font-weight:700; fill:#963751; }
+ .peep-area { fill:#fff3d9; fill-opacity:.8; stroke:#c18221; stroke-width:2; pointer-events:none; }
+ .node.peep-edit .box { stroke:#9c4900; stroke-width:3; }
+ .node.error .box { stroke:#b42318; stroke-width:4; }
+ #event.error { color:#b42318; font-weight:600; white-space:pre-wrap; overflow-wrap:anywhere; }
+ .peep-ghosts { pointer-events:none; }
+ .region-box { stroke:#9da9b8; stroke-width:1.5; }
+ .fold-btn { cursor:pointer; }
+ .fold-btn rect { fill:white; stroke:#596674; }
+ .fold-btn text { font-size:14px; fill:#152330; }
+ .fold-btn:focus rect { stroke:#1569b9; stroke-width:2; }
+ .shortcut { cursor:pointer; }
+ .shortcut path { fill:none; stroke-width:1.5; }
+ .shortcut rect { fill:#fff8ec; stroke:#b63838; stroke-dasharray:3 2; }
+ .shortcut text { font-size:11px; fill:#783030; }
+ .shortcut.selected rect, .shortcut:focus rect { stroke-width:2.5; }
+ .label { font-size:12px; fill:#152330; font-weight:600; }
+ .node-id { font-size:10px; fill:#596674; font-weight:400; }
+ .type { font-size:11px; fill:#455768; }
+ .port { stroke:#536477; stroke-width:1; }
+ .row { stroke:#b6afcf; stroke-width:1; }
+ .bind { font-size:12px; fill:#152330; }
+ .slot { font-size:10px; fill:#364352; paint-order:stroke; stroke:#fafbfc; stroke-width:3; }
+ #detail { position:absolute; top:8px; right:8px; margin:0; padding:12px; max-width:320px;
+ max-height:45%; overflow:auto; white-space:pre-wrap; overflow-wrap:anywhere;
+ background:#fffffff2; border:1px solid #bac5d0; border-radius:4px; font-size:12px; }
+ .controls { display:flex; flex-wrap:wrap; justify-content:center; align-items:center; gap:6px; padding:6px; }
+ .button { color:white; background:black; font-size:20px; min-width:30px; }
+ .button:disabled { background:grey; }
+ .count { font-size:20px; min-width:35px; text-align:center; }
+ #N { width:6ch; }
+ #scrub { width:clamp(120px,35vw,600px); }
+ </style><defs><marker id="arrow-CTRL" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#b63838"/></marker><marker id="arrow-DATA" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#536477"/></marker><marker id="arrow-MEM" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#2867b2"/></marker><marker id="arrow-ASSOC" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#9396a3"/></marker><marker id="arrow-PARSER" viewBox="0 -4 8 8" refX="8" refY="0" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,-4L8,0L0,4Z" fill="#7956a5"/></marker></defs><rect width="100%" height="100%" fill="rgb(250, 251, 252)"/><g><g class="regions" style="opacity: 1;"><g class="region" transform="translate(24,171)"><rect class="region-box" rx="8" width="313" height="298" fill="#f1f6fc"/></g></g><rect class="peep-area" rx="36" style="opacity: 0;"/><g class="edges"><path class="edge CTRL" id="e3i0" d="M126,575 C126,599 215.5,602 215.5,626" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#3[0] → #21 · CTRL</title></path><path class="edge CTRL" id="e8i1" d="M116,118 L116,233" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#8[1] → #6 · CTRL</title></path><path class="edge CTRL" id="e8i2" d="M216,434 L216,439 Q216,444 221,444 L317,444 Q325,444 325,436 L325,232 Q325,224 317,224 L152,224 Q146,224 146,230 L146,236" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#8[2] → #5 · CTRL</title></path><path class="edge CTRL" id="e18i0" d="M116,289 L116,337" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#18[0] → #8 · CTRL</title></path><path class="edge DATA" id="e18i1" d="M347,118 L347,123 Q347,128 342,128 L310,128 Q302,128 302,136 L302,291 Q302,299 294,299 L204,299 Q196,299 196,307 L196,337" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#18[1] → #4 · DATA</title></path><path class="edge CTRL" id="e21i0" d="M96,437 L96,522" stroke="#b63838" marker-start="url(#arrow-CTRL)" style="opacity: 1;"><title>#21[0] → #1 · CTRL</title></path><path class="edge DATA" id="e21i2" d="M347,118 L347,476 Q347,484 339,484 L164,484 Q156,484 156,492 L156,522" stroke="#536477" marker-start="url(#arrow-DATA)" style="opacity: 1;"><title>#21[2] → #4 · DATA</title></path></g><g class="edge-hits"/><g class="parser" style="opacity: 1;"/><g class="nodes"><g class="node" id="n1" data-id="1" transform="translate(36,387)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>True</tspan><tspan class="node-id">  #1/0</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#1 True
+Projection 0 of #18
+Type not computed
+
+[0] → #18 CTRL</title></g><g class="node" id="n2" data-id="2" transform="translate(56,24)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Start</tspan><tspan class="node-id">  #2</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#2 Start
+Type not computed
+
+</title></g><g class="node" id="n3" data-id="3" transform="translate(179.5,626)" style="opacity: 1;"><path class="box" d="M0,0H72V26H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="36" y="17"><tspan>Stop</tspan><tspan class="node-id">  #3</tspan></text><text class="type" text-anchor="middle" y="35" x="36"/><title>#3 Stop
+Type not computed
+
+[0] → #21 CTRL</title><circle class="port" cx="36" cy="0" r="3" fill="#536477"/><text class="slot" x="36" y="-7" text-anchor="middle">0</text></g><g class="node" id="n4" data-id="4" transform="translate(287,68)" style="opacity: 1;"><path class="box" d="M14,0H106Q120,0 120,14V30Q120,44 106,44H14Q0,44 0,30V14Q0,0 14,0Z" fill="#edf3f7"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>#0</tspan><tspan class="node-id">  #4</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#4 #0
+Type not computed
+
+</title></g><g class="node" id="n5" data-id="5" transform="translate(156,387)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>False</tspan><tspan class="node-id">  #5/1</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#5 False
+Projection 1 of #18
+Type not computed
+
+[0] → #18 CTRL</title></g><g class="node" id="n6" data-id="6" transform="translate(56,68)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>$ctrl</tspan><tspan class="node-id">  #6/0</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#6 $ctrl
+Projection 0 of #2
+Type not computed
+
+[0] → #2 CTRL</title></g><g class="node" id="n8" data-id="8" transform="translate(56,239)" style="opacity: 1;"><path class="box" d="M20,0H100Q120,0 120,20V24Q120,44 100,44H20Q0,44 0,24V20Q0,0 20,0Z" fill="#ffe0ab"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Loop</tspan><tspan class="node-id">  #8</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#8 Loop
+Type not computed
+
+[0] → — DATA
+[1] → #6 CTRL
+[2] → #5 CTRL</title><circle class="port" cx="30" cy="-3" r="3" fill="white"/><circle class="port" cx="60" cy="-3" r="3" fill="#536477"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g><g class="node" id="n18" data-id="18" transform="translate(36,343)" style="opacity: 1;"><path class="box" d="M0,0H240V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="120" y="18"><tspan>Never</tspan><tspan class="node-id">  #18</tspan></text><text class="type" text-anchor="middle" y="35" x="120"/><title>#18 Never
+Type not computed
+
+[0] → #8 CTRL
+[1] → #4 DATA</title><circle class="port" cx="80" cy="-3" r="3" fill="#536477"/><circle class="port" cx="160" cy="-3" r="3" fill="#536477"/><text class="slot" x="80" y="-10" text-anchor="middle">0</text><text class="slot" x="160" y="-10" text-anchor="middle">1</text></g><g class="node" id="n21" data-id="21" transform="translate(66,528)" style="opacity: 1;"><path class="box" d="M0,0H120V44H0Z" fill="#fff1c4"/><text class="label" text-anchor="middle" x="60" y="18"><tspan>Return</tspan><tspan class="node-id">  #21</tspan></text><text class="type" text-anchor="middle" y="35" x="60"/><title>#21 Return
+Type not computed
+
+[0] → #1 CTRL
+[1] → — DATA
+[2] → #4 DATA</title><circle class="port" cx="30" cy="-3" r="3" fill="#536477"/><circle class="port" cx="60" cy="-3" r="3" fill="white"/><circle class="port" cx="90" cy="-3" r="3" fill="#536477"/><text class="slot" x="30" y="-10" text-anchor="middle">0</text><text class="slot" x="60" y="-10" text-anchor="middle">1</text><text class="slot" x="90" y="-10" text-anchor="middle">2</text></g></g><g class="region-heads" style="opacity: 1;"><g class="region-head" transform="translate(24,171)"><text class="label" x="26" y="17"><tspan>Loop</tspan><tspan class="node-id">  #8</tspan></text><title>Loop · 4 nodes</title><g class="fold-btn" role="button" tabindex="0" transform="translate(4,4)" aria-label="Fold region #8"><rect width="16" height="16" rx="2"/><text x="8" y="12" text-anchor="middle">−</text><title>Fold region</title></g></g></g><g class="shortcuts" style="opacity: 1;"/><g class="peep-ghosts" pointer-events="none"/></g></svg>
diff --git a/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java b/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
new file mode 100644
index 00000000..78a0dcde
--- /dev/null
+++ b/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
@@ -0,0 +1,275 @@
+ package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.IterPeeps.WorkList;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+import java.util.*;
+
+public abstract class GlobalCodeMotion {
+
+ // Arrange that the existing isCFG() Nodes form a valid CFG. The
+ // Node.use(0) is always a block tail (either IfNode or head of the
+ // following block). There are no unreachable infinite loops.
+ public static void buildCFG( StopNode stop ) {
+ schedEarly();
+ Parser.SCHEDULED = true;
+ schedLate( stop);
+ }
+
+ // ------------------------------------------------------------------------
+ // Backwards walk on the CFG only, looking for unreachable code - which has
+ // to be an infinite loop. Insert a bogus never-taken exit to Stop, so the
+ // loop becomes reachable. Also, set loop nesting depth
+ public static void fixLoops(StopNode stop) {
+ // Backwards walk from Stop, looking for unreachable code
+ BitSet visit = new BitSet();
+ HashSet<CFGNode> unreach = new HashSet<>();
+ unreach.add(Parser.START);
+ for( Node ret : stop._inputs )
+ ((ReturnNode)ret).walkUnreach(visit,unreach);
+ if( unreach.isEmpty() ) return;
+
+ // Forwards walk from unreachable, looking for loops with no exit test.
+ visit.clear();
+ for( CFGNode cfg : unreach )
+ walkInfinite(cfg,visit,stop);
+ // Set loop depth on remaining graph
+ unreach.clear();
+ visit.clear();
+ for( Node ret : stop._inputs )
+ ((ReturnNode)ret).walkUnreach(visit,unreach);
+ assert unreach.isEmpty();
+ }
+
+
+ // Forwards walk over previously unreachable, looking for loops with no
+ // exit test.
+ static private void walkInfinite( CFGNode n, BitSet visit, StopNode stop ) {
+ if( visit.get(n._nid) ) return; // Been there, done that
+ visit.set(n._nid);
+ if( n instanceof LoopNode loop )
+ loop.forceExit(stop);
+ for( Node use : n._outputs )
+ if( use instanceof CFGNode cfg )
+ walkInfinite(cfg,visit,stop);
+ }
+
+ // ------------------------------------------------------------------------
+ // Visit all nodes in CFG Reverse Post-Order, essentially defs before uses
+ // (except at loops). Since defs are visited first - and hoisted as early
+ // as possible, when we come to a use we place it just after its deepest
+ // input.
+ private static void schedEarly() {
+ ArrayList<CFGNode> rpo = new ArrayList<>();
+ BitSet visit = new BitSet();
+ _rpo_cfg(Parser.START, visit, rpo);
+ // Reverse Post-Order on CFG
+ for( int j=rpo.size()-1; j>=0; j-- ) {
+ CFGNode cfg = rpo.get(j);
+ 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 ) {
+ int len = cfg.nOuts();
+ for( int i=0; i<len; i++ )
+ if( cfg.out(i) instanceof PhiNode phi )
+ _schedEarly(phi,visit);
+ }
+ }
+ }
+
+ // Post-Order of CFG
+ private static void _rpo_cfg(Node n, BitSet visit, ArrayList<CFGNode> rpo) {
+ if( !(n instanceof CFGNode cfg) || visit.get(cfg._nid) )
+ return; // Been there, done that
+ visit.set(cfg._nid);
+ for( Node use : cfg._outputs )
+ _rpo_cfg(use,visit,rpo);
+ rpo.add(cfg);
+ }
+
+ private static void _schedEarly(Node n, BitSet visit) {
+ if( n==null || visit.get(n._nid) ) return; // Been there, done that
+ visit.set(n._nid);
+ // 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);
+ // 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
+ for( int i=1; i<n.nIns(); i++ )
+ 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
+ }
+ }
+
+ // ------------------------------------------------------------------------
+ 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,anti);
+
+ // Copy the best placement choice into the control slot
+ for( int i=0; i<late.length; i++ )
+ if( ns[i] != null )
+ ns[i].setDef(0,late[i]);
+ }
+
+ 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);
+ Node n;
+ outer:
+ while( (n = work.pop()) != null ) {
+ assert late[n._nid]==null; // No double visit
+ // These I know the late schedule of, and need to set early for loops
+ if( n instanceof CFGNode cfg ) late[n._nid] = cfg.blockHead() ? cfg : cfg.cfg(0);
+ else if( n instanceof PhiNode phi ) late[n._nid] = phi.region();
+ // These nodes have a fixed late placement at their original control.
+ else if( n instanceof ProjNode || n instanceof NewNode || n==Parser.ZERO || n instanceof CastNode ) late[n._nid] = n.cfg0();
+ else {
+
+ // All uses done?
+ for( Node use : n._outputs )
+ if( use!=null && late[use._nid]==null )
+ 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( antiUse(ld,memuse) && late[memuse._nid]==null )
+ continue outer;
+
+ // All uses done, schedule
+ _doSchedLate(n,ns,late,anti);
+ }
+
+ // 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, int[] anti) {
+ // Walk uses, gathering the LCA (Least Common Ancestor) of uses
+ CFGNode early = (CFGNode)n.in(0);
+ assert early != null;
+ CFGNode lca = null;
+ for( Node use : n._outputs )
+ lca = use_block(n,use, late).domLCA(lca,null);
+
+ // Loads may need anti-dependencies, raising their LCA
+ if( n instanceof LoadNode load )
+ 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
+ // deepest control flow.
+ CFGNode best = lca;
+ lca = lca.idom(); // Already found best for starting LCA
+ for( ; lca != early.idom(); lca = lca.idom() )
+ if( better(lca,best) )
+ best = lca;
+ assert !(best instanceof IfNode);
+ ns [n._nid] = n;
+ late[n._nid] = best;
+ }
+
+ // Block of use. Normally from late[] schedule, except for Phis, which go
+ // to the matching Region input.
+ private static CFGNode use_block(Node n, Node use, CFGNode[] late) {
+ if( !(use instanceof PhiNode phi) )
+ return late[use._nid];
+ CFGNode found=null;
+ for( int i=1; i<phi.nIns(); i++ )
+ if( phi.in(i)==n )
+ found = phi.region().cfg(i).domLCA(found,null); // Can be more than one matching input.
+ assert found!=null;
+ return found;
+ }
+
+
+ // Least loop depth first, then largest idepth
+ private static boolean better( CFGNode lca, CFGNode best ) {
+ return lca.loopDepth() < best.loopDepth() ||
+ lca instanceof NeverNode ||
+ lca.idepth() > best.idepth() ||
+ best instanceof IfNode;
+ }
+
+ // Only a store or memory Phi covering this alias can constrain a load.
+ // MemMerge packages slices without overwriting them.
+ private static boolean antiUse(LoadNode load, Node use) {
+ return switch( use ) {
+ case StoreNode st -> st._alias==load._alias;
+ case MemPhiNode phi -> phi._alias==load._alias;
+ case BulkMemPhiNode phi -> !phi.isSplit(load._alias);
+ default -> false;
+ };
+ }
+
+ 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() )
+ anti[cfg._nid] = load._nid;
+ // Walk load->mem uses, looking for Stores causing an anti-dep
+ for( Node mem : load.mem()._outputs ) {
+ if( !antiUse(load,mem) ) continue;
+ switch( mem ) {
+ case StoreNode st:
+ assert late[st._nid]!=null;
+ 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,anti);
+ break;
+ default: throw Utils.TODO();
+ }
+ }
+ return lca;
+ }
+
+ //
+ 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( anti[stblk._nid]==load._nid ) {
+ lca = stblk.domLCA(lca,null); // Raise Loads LCA
+ if( lca == stblk && st != null && Utils.find(st._inputs,load) == -1 ) // And if something moved,
+ st.addDef(load); // Add anti-dep as well
+ return lca; // Cap this stores' anti-dep to here
+ }
+ }
+ return lca;
+ }
+
+}
diff --git a/src/main/java/com/seaofnodes/simple/IRPrinter.java b/src/main/java/com/seaofnodes/simple/IRPrinter.java
index d5ab15fe..7e93e9da 100644
--- a/src/main/java/com/seaofnodes/simple/IRPrinter.java
+++ b/src/main/java/com/seaofnodes/simple/IRPrinter.java
@@ -8,7 +8,6 @@ public final class IRPrinter extends IRAdapter<Node> {
private static final com.seaofnodes.print.IRPrinter<Node> PRINT =
new com.seaofnodes.print.IRPrinter<>(new IRPrinter());
@Override public String type(Node n) { return n._type==null ? "" : n._type.str(); }
- @Override public int inputColumns() { return 3; }
@Override public String inputMark(Node n, Node def) { return n instanceof MemMergeNode && def instanceof MemMergeNode ? "^" : " "; }
@Override public Kind kind(Node n) {
if( n instanceof StartNode ) return Kind.START;
@@ -18,13 +17,15 @@ public final class IRPrinter extends IRAdapter<Node> {
if( n instanceof PhiNode ) return Kind.PHI;
if( n instanceof ReturnNode ) return Kind.RETURN;
if( n instanceof ConstantNode ) return Kind.CONSTANT;
- if( n instanceof ProjNode ) return n.isCFG() ? Kind.CPROJ : Kind.PROJ;
- if( n.isCFG() ) return Kind.CTRL;
+ if( n instanceof CProjNode ) return Kind.CPROJ;
+ if( n instanceof ProjNode ) return n instanceof CFGNode ? Kind.CPROJ : Kind.PROJ;
+ if( n instanceof CFGNode ) return Kind.CTRL;
if( n instanceof MultiNode ) return Kind.MULTI;
return Kind.DATA;
}
@Override public int index(Node n) {
if( n instanceof ProjNode p ) return p._idx;
+ if( n instanceof CProjNode p ) return p._idx;
return n._nid;
}
diff --git a/src/main/java/com/seaofnodes/simple/Parser.java b/src/main/java/com/seaofnodes/simple/Parser.java
index a25edeb1..3c8d8f40 100644
--- a/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/src/main/java/com/seaofnodes/simple/Parser.java
@@ -31,12 +31,18 @@ public class Parser {
*/
public static StartNode START;
+ public static ConstantNode ZERO;
+ public static XCtrlNode XCTRL;
+
public StopNode STOP;
// Field identities are independent of the memory graph and Start's tuple.
private final HashMap<String,Integer> _aliases = new HashMap<>();
private int _alias = 2;
+ // Debugger Printing.
+ public static boolean SCHEDULED; // True if debug printer can use schedule info
+
// The Lexer. Thin wrapper over a byte[] buffer with a cursor.
private final Lexer _lexer;
@@ -82,7 +88,7 @@ public class Parser {
ScopeNode _continueScope;
ScopeNode _breakScope;
- // Authoritative declarations, indexed by type name.
+ // Mapping from a type name to a Type.
public static HashMap<String, Type> TYPES = new HashMap<>();
public Parser(String source, TypeInteger arg) {
@@ -91,11 +97,14 @@ public class Parser {
IterPeeps.reset();
TYPES.clear();
TYPES.put("int",TypeInteger.BOT);
+ SCHEDULED = false;
_lexer = new Lexer(source);
_scope = new ScopeNode();
_continueScope = _breakScope = null;
START = new StartNode(new Type[]{ Type.CONTROL, TypeMem.BOT, arg });
STOP = new StopNode(source);
+ ZERO = new ConstantNode(TypeInteger.constant(0)).peephole().keep();
+ XCTRL= new XCtrlNode().peephole().keep();
}
public Parser(String source) {
@@ -118,8 +127,8 @@ public class Parser {
_xScopes.push(_scope);
// Enter a new scope for the initial control and arguments
_scope.push();
- _scope.define(ScopeNode.CTRL, Type.CONTROL , new ProjNode(START, 0, ScopeNode.CTRL).peephole());
- _scope.define(ScopeNode.ARG0, TypeInteger.BOT, new ProjNode(START, 2, ScopeNode.ARG0).peephole());
+ _scope.define(ScopeNode.CTRL, Type.CONTROL , new CProjNode(START, 0, ScopeNode.CTRL).peephole());
+ _scope.define(ScopeNode.ARG0, TypeInteger.BOT, new ProjNode(START, 2, ScopeNode.ARG0).peephole());
_scope.define("$mem", TypeMem.BOT, new ProjNode(START, 1, "$mem").peephole());
parseBlock();
if( ctrl()._type==Type.CONTROL )
@@ -187,7 +196,7 @@ public class Parser {
}
/**
- * Parse a complete struct declaration.
+ * Parse a struct declaration, and return the following statement.
* Only allowed in top level scope.
* Structs cannot be redefined.
*
@@ -252,8 +261,8 @@ public class Parser {
// IfNode takes current control and predicate
Node ifNode = new IfNode(ctrl(), pred).peephole();
// Setup projection nodes
- Node ifT = new ProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
- Node ifF = new ProjNode(ifNode.unkeep(), 1, "False").peephole();
+ Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
+ Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole();
// Clone the body Scope to create the break/exit Scope which accounts for any
// side effects in the predicate. The break/exit Scope will be the final
@@ -299,7 +308,7 @@ public class Parser {
private ScopeNode jumpTo(ScopeNode toScope) {
ScopeNode cur = _scope.dup();
- ctrl(new ConstantNode(Type.XCONTROL).peephole()); // Kill current scope
+ ctrl(XCTRL); // Kill current scope
// Prune nested lexical scopes that have depth > than the loop head
// We use _breakScope as a proxy for the loop head scope to obtain the depth
while( cur._scopes.size() > _breakScope._scopes.size() )
@@ -335,8 +344,8 @@ public class Parser {
// IfNode takes current control and predicate
Node ifNode = new IfNode(ctrl(), pred).peephole();
// Setup projection nodes
- Node ifT = new ProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
- Node ifF = new ProjNode(ifNode.unkeep(), 1, "False").peephole().keep();
+ Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
+ Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole().keep();
// In if true branch, the ifT proj node becomes the ctrl
// But first clone the scope and set it as current
int ndefs = _scope.nIns();
@@ -353,7 +362,7 @@ public class Parser {
// Parse the false side
_scope = fScope; // Restore scope, then parse else block if any
ifF.unkeep(); // fScope already owns the false control
- _scope.upcast(ifF,pred,true); // Up-cast predicate, also without an else
+ _scope.upcast(ifF,pred,true); // Up-cast predicate
if (matchx("else")) {
parseStatement();
fScope = _scope;
@@ -383,7 +392,7 @@ public class Parser {
private Node parseReturn() {
var expr = require(parseExpression(), ";");
Node ret = STOP.addReturn(new ReturnNode(ctrl(), expr, _scope).peephole());
- ctrl(new ConstantNode(Type.XCONTROL).peephole()); // Kill control
+ ctrl(XCTRL); // Kill control
return ret;
}
@@ -436,7 +445,8 @@ public class Parser {
return _scope.update(name,expr);
}
- // Parse a type-or-null
+ // Parse a "type id" and return "type" (and re-parse "id" in caller) or
+ // return null.
private Type type() {
int old = _lexer._position;
String tname = _lexer.matchId();
@@ -457,6 +467,7 @@ public class Parser {
return t instanceof TypeStruct obj ? TypeMemPtr.make(obj,nullable) : t;
}
+
/**
* Parse an expression of the form:
*
@@ -527,7 +538,7 @@ public class Parser {
* @return a multiply expression {@link Node}, never {@code null}
*/
private Node parseMultiplication() {
- var lhs = parseUnary();
+ var lhs = parseUnary(); boolean mul;
while( true ) {
if( false ) ;
else if( match("*") ) lhs = new MulNode(lhs,null);
@@ -562,10 +573,10 @@ public class Parser {
* @return a primary {@link Node}, never {@code null}
*/
private Node parsePrimary() {
- if( _lexer.isNumber(_lexer.peek()) ) return parseIntegerLiteral();
+ if( _lexer.isNumber(_lexer.peek()) ) return parseLiteral();
if( match("(") ) return require(parseExpression(), ")");
if( matchx("true" ) ) return new ConstantNode(TypeInteger.constant(1)).peephole();
- if( matchx("false") ) return new ConstantNode(TypeInteger.ZERO).peephole();
+ if( matchx("false") ) return ZERO;
if( matchx("null" ) ) return new ConstantNode(TypeMemPtr.NULLPTR).peephole();
if( matchx("new") ) {
String structName = requireId();
@@ -600,7 +611,7 @@ public class Parser {
private void store(String name, int alias, Type glb, Node ptr, Node val, boolean init) {
Node prior = mem().keep();
- Node st = new StoreNode(name,alias,glb,null,prior,ptr,val,init).peephole();
+ Node st = new StoreNode(name,alias,glb,ctrl(),prior,ptr,val,init).peephole();
mem(new MemMergeNode(prior,alias,st).peephole());
if( prior.unkeep().isUnused() ) prior.kill();
}
@@ -649,7 +660,7 @@ public class Parser {
* integerLiteral: [1-9][0-9]* | [0]
* </pre>
*/
- private ConstantNode parseIntegerLiteral() {
+ private ConstantNode parseLiteral() {
return (ConstantNode) new ConstantNode(_lexer.parseNumber()).peephole();
}
@@ -744,7 +755,16 @@ public class Parser {
* Return the next non-white-space character
*/
private void skipWhiteSpace() {
- while (isWhiteSpace()) _position++;
+ while( true ) {
+ if( isWhiteSpace() ) _position++;
+ // Skip // to end of line
+ else if( _position+2 < _input.length &&
+ _input[_position ] == '/' &&
+ _input[_position+1] == '/') {
+ _position += 2;
+ while( !isEOF() && _input[_position] != '\n' ) _position++;
+ } else break;
+ }
}
@@ -826,8 +846,9 @@ public class Parser {
return new String(_input, start, --_position - start);
}
+ //
private boolean isPunctuation(char ch) {
- return "=;[]<>(){}+-/*!".indexOf(ch) != -1;
+ return "=;[]<>()+-/*".indexOf(ch) != -1;
}
private String parsePunctuation() {
diff --git a/src/main/java/com/seaofnodes/simple/Utils.java b/src/main/java/com/seaofnodes/simple/Utils.java
index 4f2af769..f3ac25b1 100644
--- a/src/main/java/com/seaofnodes/simple/Utils.java
+++ b/src/main/java/com/seaofnodes/simple/Utils.java
@@ -55,11 +55,4 @@ public class Utils {
// Fold a 64bit hash into 32 bits
public static int fold( long x ) { return (int)((x>>32) ^ x); }
-
- public static boolean eq(String s0, String s1) {
- if( s0==s1 ) return true;
- assert s0==s0.intern();
- assert s1==s1.intern();
- return false;
- }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/AddNode.java b/src/main/java/com/seaofnodes/simple/node/AddNode.java
index e993a20e..eb7e2793 100644
--- a/src/main/java/com/seaofnodes/simple/node/AddNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -103,6 +103,7 @@ public class AddNode extends Node {
static Node phiCon(Node op, boolean rotate) {
Node lhs = op.in(1);
Node rhs = op.in(2);
+ if( rhs._type==TypeInteger.TOP ) return null;
// LHS is either a Phi of constants, or another op with Phi of constants
PhiNode lphi = pcon(lhs,op);
if( rotate && lphi==null && lhs.nIns() > 2 ) {
diff --git a/src/main/java/com/seaofnodes/simple/node/CFGNode.java b/src/main/java/com/seaofnodes/simple/node/CFGNode.java
new file mode 100644
index 00000000..c690a83c
--- /dev/null
+++ b/src/main/java/com/seaofnodes/simple/node/CFGNode.java
@@ -0,0 +1,96 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.*;
+import com.seaofnodes.simple.Utils;
+
+import java.util.BitSet;
+import java.util.HashSet;
+import java.util.HashMap;
+
+/** Control Flow Graph Nodes
+ * <p>
+ * CFG nodes have a immediate dominator depth (idepth) and a loop nesting
+ * depth(loop_depth).
+ * <p>
+ * idepth is computed lazily upon first request, and is valid even in the
+ * Parser, and is used by peepholes during parsing and afterward.
+ * <p>
+ * loop_depth is computed after optimization as part of scheduling.
+ *
+ */
+public abstract class CFGNode extends Node {
+
+ public CFGNode(Node... nodes) { super(nodes); }
+
+ @Override public boolean isCFG() { return true; }
+
+ public CFGNode cfg(int idx) { return (CFGNode)in(idx); }
+
+ // Block head is Start, Region, CProj, but not e.g. If, Return, Stop
+ public boolean blockHead() { return false; }
+
+ // Should be exactly 1 tail from a block head
+ public CFGNode blockTail() {
+ assert blockHead();
+ for( Node n : _outputs )
+ if( n instanceof CFGNode cfg )
+ return cfg;
+ return null;
+ }
+
+ // ------------------------------------------------------------------------
+ /**
+ * Immediate dominator tree depth, used to approximate a real IDOM during
+ * parsing where we do not have the whole program, and also peepholes
+ * change the CFG incrementally.
+ * <p>
+ * See {@link <a href="https://en.wikipedia.org/wiki/Dominator_(graph_theory)">...</a>}
+ */
+ public char _idepth;
+ public int idepth() { return _idepth!=0 ? _idepth : cacheIDepth(idom().idepth()+1); }
+ // Zero depth means uncached. Check before narrowing so overflow cannot wrap.
+ final int cacheIDepth(int depth) {
+ assert 0 <= depth && depth <= Character.MAX_VALUE : "Dominator depth exceeds 65535";
+ return _idepth = (char)depth;
+ }
+
+
+ // Return the immediate dominator of this Node and compute dom tree depth.
+ public CFGNode idom(Node dep) { return cfg(0); }
+ public final CFGNode idom() { return idom(null); }
+
+ // Return the LCA of two idoms
+ public CFGNode domLCA(CFGNode rhs, Node dep) {
+ if( rhs==null ) return this;
+ CFGNode lhs = this;
+ while( lhs != rhs ) {
+ if( lhs==null || rhs==null ) return null;
+ int comp = lhs.idepth() - rhs.idepth();
+ if( comp >= 0 ) lhs = (dep==null ? lhs : (CFGNode)lhs.addDep(dep)).idom();
+ if( comp <= 0 ) rhs = (dep==null ? rhs : (CFGNode)rhs.addDep(dep)).idom();
+ }
+ return lhs;
+ }
+
+ // Loop nesting depth
+ public int _loopDepth;
+ public int loopDepth() { return _loopDepth==0 ? (_loopDepth = cfg(0).loopDepth()) : _loopDepth; }
+
+ // ------------------------------------------------------------------------
+ // Support routines for Global Code Motion
+
+ // Tik-tok recursion pattern. This method is final, and every caller does
+ // this work.
+ public final void walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ if( visit.get(_nid) ) return;
+ visit.set(_nid);
+ _walkUnreach(visit,unreach);
+ unreach.remove(this); // Since we reached here... Node was not unreachable
+ }
+
+ // Tik-tok recursion pattern; not-final; callers override this.
+ void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ cfg(0).walkUnreach(visit,unreach);
+ }
+}
diff --git a/src/main/java/com/seaofnodes/simple/node/CProjNode.java b/src/main/java/com/seaofnodes/simple/node/CProjNode.java
new file mode 100644
index 00000000..ff5d1a76
--- /dev/null
+++ b/src/main/java/com/seaofnodes/simple/node/CProjNode.java
@@ -0,0 +1,62 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.print.ExprPrinter;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
+import com.seaofnodes.simple.type.TypeTuple;
+
+public class CProjNode extends CFGNode {
+
+ // Which slice of the incoming multipart value
+ public final int _idx;
+
+ // Debugging label
+ public final String _label;
+
+ public CProjNode(Node ctrl, int idx, String label) {
+ super(ctrl);
+ _idx = idx;
+ _label = label;
+ }
+
+ @Override public String label() { return _label; }
+
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) { return p.p(_label); }
+
+ @Override public boolean isMultiTail() { return in(0).isMultiHead(); }
+ @Override public boolean blockHead() { return true; }
+
+ public CFGNode ctrl() { return cfg(0); }
+
+ @Override
+ public Type compute() {
+ Type t = ctrl()._type;
+ return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ if ( ctrl() instanceof IfNode iff ) {
+ if ( iff._type instanceof TypeTuple tt && tt._types[1 - _idx] == Type.XCONTROL )
+ return iff.in(0); // We become our input control
+
+ // Flip a negating if-test, to remove the not
+ if( iff.pred().addDep(this) instanceof NotNode not )
+ return new CProjNode(new IfNode(iff.ctrl(),not.in(1)).peephole(),1-_idx,_idx==0 ? "False" : "True");
+ }
+ return null;
+ }
+
+ @Override
+ public Node getBlockStart() {
+ return ctrl() instanceof IfNode ? this : ctrl().getBlockStart();
+ }
+ @Override
+ boolean eq( Node n ) { return _idx == ((CProjNode)n)._idx; }
+
+ @Override
+ int hash() { return _idx; }
+
+}
diff --git a/src/main/java/com/seaofnodes/simple/node/CastNode.java b/src/main/java/com/seaofnodes/simple/node/CastNode.java
index c1faf210..9ade5a50 100644
--- a/src/main/java/com/seaofnodes/simple/node/CastNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/CastNode.java
@@ -20,6 +20,7 @@ public class CastNode extends Node {
@Override public String label() { return "("+_t.str()+")"; }
+
@Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
return p.p(label()).n(in(1));
}
diff --git a/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
index 426736ed..9ecd1152 100644
--- a/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
@@ -38,6 +38,7 @@ public class ConstantNode extends Node {
}
@Override public boolean isMultiTail() { return true; }
+ // The special forever-zero is pinned, all others can sink
@Override
public Type compute() { return _con; }
diff --git a/src/main/java/com/seaofnodes/simple/node/IfNode.java b/src/main/java/com/seaofnodes/simple/node/IfNode.java
index 45f695a5..70340f7f 100644
--- a/src/main/java/com/seaofnodes/simple/node/IfNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/IfNode.java
@@ -5,8 +5,10 @@ import com.seaofnodes.simple.IterPeeps;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import com.seaofnodes.simple.type.TypeTuple;
+import java.util.BitSet;
+import java.util.HashSet;
-public class IfNode extends MultiNode {
+public class IfNode extends CFGNode implements MultiNode {
public IfNode(Node ctrl, Node pred) {
super(ctrl, pred);
@@ -18,7 +20,6 @@ public class IfNode extends MultiNode {
@Override protected String format() { return "if( %1 )"; }
- @Override public boolean isCFG() { return true; }
@Override public boolean isMultiHead() { return true; }
public Node ctrl() { return in(0); }
@@ -48,11 +49,20 @@ public class IfNode extends MultiNode {
// Hunt up the immediate dominator tree. If we find an identical if
// test on either the true or false branch, that side wins.
if( !pred()._type.isHighOrConst() )
- for( Node dom = idom(), prior=this; dom!=null; prior = dom, dom = dom.idom() )
- if( dom.addDep(this) instanceof IfNode iff && iff.pred().addDep(this)==pred() && prior instanceof ProjNode prj ) {
+ for( CFGNode dom = idom(), prior=this; dom!=null; prior = dom, dom = dom.idom() )
+ if( dom.addDep(this) instanceof IfNode iff && iff.pred().addDep(this)==pred() && prior instanceof CProjNode prj ) {
setDef(1,new ConstantNode(TypeInteger.make(true,prj._idx==0?1:0)).peephole());
return this;
}
return null;
}
+
+ @Override void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ for( Node proj : _outputs )
+ if( ((CProjNode)proj)._loopDepth == 0 )
+ unreach.add((CProjNode)proj);
+ super._walkUnreach(visit,unreach);
+ }
+
+ @Override public Node getBlockStart() { return ctrl().getBlockStart(); }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/LoadNode.java b/src/main/java/com/seaofnodes/simple/node/LoadNode.java
index b2ee6939..277644f5 100644
--- a/src/main/java/com/seaofnodes/simple/node/LoadNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/LoadNode.java
@@ -5,7 +5,6 @@ import com.seaofnodes.print.ExprPrinter;
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.IterPeeps;
import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.Field;
/**
@@ -43,9 +42,7 @@ public class LoadNode extends MemOpNode {
return false;
}
- @Override
- public String label() { return "Load"; }
-
+ @Override public String label() { return _name; }
@Override
protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) { return p.p(".").p(_name); }
@@ -65,7 +62,7 @@ public class LoadNode extends MemOpNode {
// Simple Load-after-Store on same address.
if( mem() instanceof StoreNode st &&
ptr() == st.ptr() && _alias==st._alias ) { // Must check same object
- assert Utils.eq(_name,st._name); // Equiv class aliasing is perfect
+ assert _name.equals(st._name); // Equiv class aliasing is perfect
return st.val();
}
@@ -81,9 +78,13 @@ public class LoadNode extends MemOpNode {
if( profit(memphi,2) ||
// Else must not be a loop to count profit on LHS.
(!(memphi.region() instanceof LoopNode) && profit(memphi,1)) ) {
- Node ld1 = new LoadNode(_name,_alias,_declaredType,memphi.in(1),ptr()).peephole();
- Node ld2 = new LoadNode(_name,_alias,_declaredType,memphi.in(2),ptr()).peephole();
- return new PhiNode(_name,_type,memphi.region(),ld1,ld2);
+ if( ptr() instanceof NewNode || !(ptr() instanceof CastNode) ) {
+ if( !(ptr() instanceof NewNode) ) throw Utils.TODO(); // Validate
+
+ Node ld1 = new LoadNode(_name,_alias,_declaredType,memphi.in(1),ptr()).peephole();
+ Node ld2 = new LoadNode(_name,_alias,_declaredType,memphi.in(2),ptr()).peephole();
+ return new PhiNode(_name,_type,memphi.region(),ld1,ld2);
+ }
}
}
diff --git a/src/main/java/com/seaofnodes/simple/node/LoopNode.java b/src/main/java/com/seaofnodes/simple/node/LoopNode.java
index a0ba973e..a0e988dd 100644
--- a/src/main/java/com/seaofnodes/simple/node/LoopNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/LoopNode.java
@@ -1,15 +1,17 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
-
+import com.seaofnodes.simple.type.TypeInteger;
import java.util.BitSet;
+import java.util.HashSet;
public class LoopNode extends RegionNode {
-
public LoopNode( Node entry ) { super(null,entry,null); }
- Node entry() { return in(1); }
- Node back () { return in(2); }
+ public CFGNode entry() { return cfg(1); }
+ public CFGNode back () { return cfg(2); }
@Override
public String label() { return "Loop"; }
@@ -25,7 +27,54 @@ public class LoopNode extends RegionNode {
return inProgress() ? null : super.idealize();
}
- // Bypass Region idom, same as the default idom() using use entry in(1) instead of in(0)
- @Override int idepth() { return _idepth!=0 ? _idepth : cacheIDepth(entry().idepth()+1); }
- @Override Node idom() { return entry(); }
+ // Bypass Region idom, same as the default idom() using use in(1) instead of in(0)
+ @Override public int idepth() { return _idepth!=0 ? _idepth : cacheIDepth(entry().idepth()+1); }
+ // Bypass Region idom, same as the default idom() using use in(1) instead of in(0)
+ @Override public CFGNode idom(Node dep) { return entry(); }
+
+ @Override public int loopDepth() {
+ if( _loopDepth!=0 ) return _loopDepth; // Was already set
+ _loopDepth = entry()._loopDepth+1; // Entry depth plus one
+ // One-time tag loop exits
+ for( CFGNode idom = back(); idom!=this; idom = idom.idom() ) {
+ // Walk idom in loop, setting depth
+ idom._loopDepth = _loopDepth;
+ // Loop exit hits the CProj before the If, instead of jumping from
+ // Region directly to If.
+ if( idom instanceof CProjNode proj ) {
+ assert proj.in(0) instanceof IfNode; // Loop exit test
+ // Find the loop exit CProj, and set loop_depth
+ for( Node use : proj.in(0)._outputs )
+ if( use instanceof CProjNode proj2 && proj2 != idom )
+ proj2._loopDepth = _loopDepth-1;
+ }
+ }
+ return _loopDepth;
+ }
+
+ // 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 void forceExit( 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.
+ CFGNode x = back();
+ while( x != this ) {
+ if( x instanceof CProjNode exit && exit.in(0) instanceof IfNode iff ) {
+ Node other = iff.cproj(1-exit._idx);
+ if( !(other.out(0) instanceof LoopNode loop) || loop.entry()==exit )
+ return; // Found an exit, not an infinite loop
+ }
+ x = x.idom();
+ }
+ // Found a no-exit loop. Insert an exit
+ NeverNode iff = new NeverNode(back());
+ for( Node use : _outputs )
+ if( use instanceof PhiNode phi )
+ iff.addDef(use);
+ CProjNode t = new CProjNode(iff,0,"True" );
+ CProjNode f = new CProjNode(iff,1,"False");
+ setDef(2,f);
+ stop.addDef(new ReturnNode(t,Parser.ZERO,null));
+ }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/MultiNode.java b/src/main/java/com/seaofnodes/simple/node/MultiNode.java
index 2743bd19..ac9c0045 100644
--- a/src/main/java/com/seaofnodes/simple/node/MultiNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/MultiNode.java
@@ -1,14 +1,20 @@
package com.seaofnodes.simple.node;
-public abstract class MultiNode extends Node {
-
- public MultiNode(Node... inputs) { super(inputs); }
+public interface MultiNode extends OutNode {
// Find a projection by index
- ProjNode proj( int idx ) {
- for( Node out : _outputs )
+ default ProjNode proj( int idx ) {
+ for( Node out : outs() )
if( out instanceof ProjNode prj && prj._idx==idx )
return prj;
return null;
}
+
+ // Find a projection by index
+ default CProjNode cproj( int idx ) {
+ for( Node out : outs() )
+ if( out instanceof CProjNode prj && prj._idx==idx )
+ return prj;
+ return null;
+ }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/NeverNode.java b/src/main/java/com/seaofnodes/simple/node/NeverNode.java
new file mode 100644
index 00000000..7e42ccf9
--- /dev/null
+++ b/src/main/java/com/seaofnodes/simple/node/NeverNode.java
@@ -0,0 +1,22 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.print.ExprPrinter;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeTuple;
+
+
+// "Never true" for infinite loop exits
+public class NeverNode extends IfNode {
+ public NeverNode(Node ctrl) { super(ctrl,Parser.ZERO); }
+
+ @Override public String label() { return "Never"; }
+
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) { return p.p("Never"); }
+
+ @Override public Type compute() { return TypeTuple.IF_BOTH; }
+
+ @Override public Node idealize() { return null; }
+}
diff --git a/src/main/java/com/seaofnodes/simple/node/NewNode.java b/src/main/java/com/seaofnodes/simple/node/NewNode.java
index 857920bc..74ba5184 100644
--- a/src/main/java/com/seaofnodes/simple/node/NewNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/NewNode.java
@@ -22,6 +22,7 @@ public class NewNode extends Node {
return p.p("new ").p(_ptr._obj.str());
}
+
@Override
public Type compute() { return _ptr; }
diff --git a/src/main/java/com/seaofnodes/simple/node/Node.java b/src/main/java/com/seaofnodes/simple/node/Node.java
index 999e38fd..2c8596fa 100644
--- a/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -17,7 +17,7 @@ import java.util.function.Function;
* The Node class provides common functionality used by all subtypes.
* Subtypes of Node specialize by overriding methods.
*/
-public abstract class Node extends BaseNode<Node> implements Cloneable {
+public abstract class Node extends BaseNode<Node> implements OutNode, Cloneable {
/**
* Inputs to the node. These are use-def references to Nodes.
@@ -83,6 +83,7 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
*/
public Node in(int i) { return _inputs.get(i); }
public Node out(int i) { return _outputs.get(i); }
+ @Override public ArrayList<Node> outs() { return _outputs; }
public int nIns() { return _inputs.size(); }
@@ -90,7 +91,7 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
public boolean isUnused() { return nOuts() == 0; }
- public boolean isMem() { return false; }
+ public CFGNode cfg0() { return (CFGNode)in(0); }
/**
* Change a <em>def</em> into a Node. Keeps the edges correct, by removing
@@ -117,11 +118,11 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
// the new_def might get killed if the old node kills it recursively.
if( new_def != null )
new_def.addUse(this);
+ // Set the new_def over the old (killed) edge
+ _inputs.set(idx,new_def);
if( old_def != null && // If the old def exists, remove a def->use edge
old_def.delUse(this) ) // If we removed the last use, the old def is now dead
old_def.kill(); // Kill old def
- // Set the new_def over the old (killed) edge
- _inputs.set(idx,new_def);
moveDepsToWorklist();
// Return self for easy flow-coding
return new_def;
@@ -149,7 +150,7 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
* @param new_def the new definition, appended to the end of existing definitions
* @return new_def for flow coding
*/
- Node addDef(Node new_def) {
+ public Node addDef(Node new_def) {
unlock();
// Add use->def edge
_inputs.add(new_def);
@@ -269,8 +270,8 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
Type old = setType(compute());
// Replace constant computations from non-constants with a constant node
- if( !(this instanceof ConstantNode) && _type.isHighOrConst() )
- return new ConstantNode(_type).peepholeOpt();
+ if( !(this instanceof ConstantNode) && !(this instanceof XCtrlNode) && _type.isHighOrConst() )
+ return (_type==Type.XCONTROL ? new XCtrlNode() : new ConstantNode(_type)).peepholeOpt();
// Global Value Numbering
if( _hash==0 ) {
@@ -485,6 +486,17 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
int hash() { return 0; }
// ------------------------------------------------------------------------
+ //
+
+ /** Is this Node control-flow-graph related */
+
+ /** Is this Node Memory related */
+ public boolean isMem() { return false; }
+
+ /** Return block start from a isCFG() */
+ public Node getBlockStart() { return null; }
+
+ // ------------------------------------------------------------------------
// Peephole utilities
// Swap inputs without letting either input go dead during the swap.
@@ -512,38 +524,6 @@ public abstract class Node extends BaseNode<Node> implements Cloneable {
return true;
}
- /**
- * Immediate dominator tree depth, used to approximate a real IDOM depth
- * during parsing where we do not have the whole program, and also
- * peepholes change the CFG incrementally.
- * <p>
- * See {@link <a href="https://en.wikipedia.org/wiki/Dominator_(graph_theory)">...</a>}
- */
- public char _idepth; // IDOM depth approx; Zero is unset; non-zero is cached legit
-
- // Find the lowest common ancestor in the current dominator tree.
- Node domLCA(Node rhs) {
- if( rhs==null ) return this;
- Node lhs = this;
- while( lhs != rhs ) {
- if( lhs==null || rhs==null ) return null;
- int comp = lhs.idepth() - rhs.idepth();
- if( comp >= 0 ) lhs = lhs.idom();
- if( comp <= 0 ) rhs = rhs.idom();
- }
- return lhs;
- }
-
- int idepth() { return _idepth!=0 ? _idepth : cacheIDepth(idom().idepth()+1); }
- // Zero depth means uncached. Check before narrowing so overflow cannot wrap.
- final int cacheIDepth(int depth) {
- assert 0 <= depth && depth <= Character.MAX_VALUE : "Dominator depth exceeds 65535";
- return _idepth = (char)depth;
- }
-
- // Return the immediate dominator of this Node.
- Node idom() { return in(0); }
-
// Make a shallow copy (same class) of this Node, with given inputs and
// empty outputs and a new Node ID. The original inputs are ignored.
// Does not need to be implemented in isCFG() nodes.
diff --git a/src/main/java/com/seaofnodes/simple/node/OutNode.java b/src/main/java/com/seaofnodes/simple/node/OutNode.java
new file mode 100644
index 00000000..7e531145
--- /dev/null
+++ b/src/main/java/com/seaofnodes/simple/node/OutNode.java
@@ -0,0 +1,7 @@
+package com.seaofnodes.simple.node;
+
+import java.util.ArrayList;
+
+public interface OutNode {
+ ArrayList<Node> outs();
+}
diff --git a/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index 6995cc2f..8dfa04cf 100644
--- a/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -32,10 +32,9 @@ public class PhiNode extends Node {
return p.unchar(',').close();
}
- Node region() { return in(0); }
+ public CFGNode region() { return (CFGNode)in(0); }
@Override public boolean isMultiTail() { return true; }
- @Override
- public boolean isMem() { return _declaredType instanceof TypeMem; }
+ @Override public boolean isMem() { return _declaredType instanceof TypeMem; }
@Override
public Type compute() {
@@ -83,11 +82,11 @@ public class PhiNode extends Node {
if( in(2)._type == in(2)._type.makeInit() ) nullx = 2;
if( nullx != -1 ) {
Node val = in(3-nullx);
- if( region().idom() instanceof IfNode iff && iff.pred().addDep(this)==val ) {
+ if( region().idom(this) instanceof IfNode iff && iff.pred().addDep(this)==val ) {
// Must walk the idom on the null side to make sure we hit False.
- Node idom = region().in(nullx);
- while( idom.in(0) != iff ) idom = idom.idom();
- if( idom instanceof ProjNode proj && proj._idx==1 )
+ CFGNode idom = (CFGNode)region().in(nullx);
+ while( idom.nIns() > 0 && idom.in(0) != iff ) idom = idom.idom();
+ if( idom instanceof CProjNode proj && proj._idx==1 )
return val;
}
}
diff --git a/src/main/java/com/seaofnodes/simple/node/ProjNode.java b/src/main/java/com/seaofnodes/simple/node/ProjNode.java
index a2632110..e82c69b3 100644
--- a/src/main/java/com/seaofnodes/simple/node/ProjNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/ProjNode.java
@@ -15,7 +15,7 @@ public class ProjNode extends Node {
// Debugging label
public final String _label;
- public ProjNode(MultiNode ctrl, int idx, String label) {
+ public ProjNode(Node ctrl, int idx, String label) {
super(ctrl);
_idx = idx;
_label = label;
@@ -27,30 +27,16 @@ public class ProjNode extends Node {
@Override
protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) { return p.p(_label); }
- @Override public boolean isCFG() { return _idx==0 || ctrl() instanceof IfNode; }
@Override public boolean isMultiTail() { return in(0).isMultiHead(); }
@Override public boolean isMem() { return _type instanceof TypeMem; }
- public MultiNode ctrl() { return (MultiNode)in(0); }
-
@Override
public Type compute() {
Type t = in(0)._type;
return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM;
}
- @Override
- public Node idealize() {
- if ( ctrl() instanceof IfNode iff ) {
- if ( iff._type instanceof TypeTuple tt && tt._types[1 - _idx] == Type.XCONTROL )
- return iff.in(0); // We become our input control
-
- // Flip a negating if-test, to remove the not
- if( iff.pred().addDep(this) instanceof NotNode not )
- return new ProjNode((MultiNode)new IfNode(iff.ctrl(),not.in(1)).peephole(),1-_idx,_idx==0 ? "False" : "True");
- }
- return null;
- }
+ @Override public Node idealize() { return null; }
@Override
boolean eq( Node n ) { return _idx == ((ProjNode)n)._idx; }
diff --git a/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/src/main/java/com/seaofnodes/simple/node/RegionNode.java
index 8b52494a..cdfd10ad 100644
--- a/src/main/java/com/seaofnodes/simple/node/RegionNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -3,13 +3,15 @@ package com.seaofnodes.simple.node;
import com.seaofnodes.print.ExprPrinter;
import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
-
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.BitSet;
+import java.util.HashSet;
-public class RegionNode extends Node {
+public class RegionNode extends CFGNode {
public RegionNode(Node... nodes) { super(nodes); }
@@ -20,8 +22,8 @@ public class RegionNode extends Node {
return p.p(label()).p(_nid);
}
- @Override public boolean isCFG() { return true; }
@Override public boolean isMultiHead() { return true; }
+ @Override public boolean blockHead() { return true; }
@Override
public Type compute() {
@@ -57,7 +59,7 @@ public class RegionNode extends Node {
IterPeeps.addAll(phi._outputs);
}
}
- return isDead() ? new ConstantNode(Type.XCONTROL) : delDef(path);
+ return isDead() ? Parser.XCTRL : delDef(path);
}
// If down to a single input, become that input
if( nIns()==2 && !hasPhi() )
@@ -89,23 +91,30 @@ public class RegionNode extends Node {
}
// Immediate dominator of Region is a little more complicated.
- @Override int idepth() {
+ @Override public int idepth() {
if( _idepth!=0 ) return _idepth;
int depth=0;
for( Node n : _inputs )
if( n!=null )
- depth = Math.max(depth,n.idepth()+1);
+ depth = Math.max(depth,((CFGNode)n).idepth()+1);
return cacheIDepth(depth);
}
- @Override Node idom() {
- Node lca = null;
+ @Override public CFGNode idom(Node dep) {
+ CFGNode lca = null;
// Recompute from predecessors: CFG edits can change the dominator.
for( int i=1; i<nIns(); i++ )
- lca = in(i).domLCA(lca);
+ lca = cfg(i).domLCA(lca,dep);
return lca;
}
+ @Override void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ for( int i=1; i<nIns(); i++ )
+ cfg(i).walkUnreach(visit,unreach);
+ }
+
+ @Override public int loopDepth() { return _loopDepth==0 ? (_loopDepth = cfg(1).loopDepth()) : _loopDepth; }
+
// True if last input is null
public final boolean inProgress() {
return nIns()>1 && in(nIns()-1) == null;
@@ -115,4 +124,6 @@ public class RegionNode extends Node {
@Override boolean eq( Node n ) {
return !inProgress();
}
+
+ @Override public Node getBlockStart() { return this; }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
index ef4ddb3a..161cc7d6 100644
--- a/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -6,10 +6,11 @@ import com.seaofnodes.simple.type.*;
/** Keeps all preceding memory effects and the return value alive: {ctrl, $mem, value}. */
-public class ReturnNode extends Node {
+public class ReturnNode extends CFGNode {
public ReturnNode(Node ctrl, Node data, ScopeNode scope) {
- super(ctrl, scope.lookup("$mem"), data);
+ // A synthetic never-taken loop exit has no source scope.
+ super(ctrl, scope == null ? null : scope.lookup("$mem"), data);
}
public Node ctrl() { return in(0); }
@@ -26,8 +27,6 @@ public class ReturnNode extends Node {
return p.p(";");
}
- @Override public boolean isCFG() { return true; }
-
@Override
public Type compute() {
// Return exposes the complete memory state.
@@ -40,4 +39,6 @@ public class ReturnNode extends Node {
return ctrl();
return null;
}
+
+ @Override public Node getBlockStart() { return ctrl().getBlockStart(); }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/StartNode.java b/src/main/java/com/seaofnodes/simple/node/StartNode.java
index 3404f111..a5d4923f 100644
--- a/src/main/java/com/seaofnodes/simple/node/StartNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -3,10 +3,13 @@ package com.seaofnodes.simple.node;
import com.seaofnodes.print.ExprPrinter;
import com.seaofnodes.simple.type.*;
+import java.util.BitSet;
+import java.util.HashSet;
-/** The entry tuple: control, initial whole-memory state, and argument. */
-public class StartNode extends MultiNode {
- final TypeTuple _args;
+/** Start supplies control, whole memory, and the argument, in that order. */
+public class StartNode extends CFGNode implements MultiNode {
+
+ private final TypeTuple _args;
public StartNode(Type[] args) {
super();
@@ -20,8 +23,8 @@ public class StartNode extends MultiNode {
return p.p(label());
}
- @Override public boolean isCFG() { return true; }
@Override public boolean isMultiHead() { return true; }
+ @Override public boolean blockHead() { return true; }
@Override
public TypeTuple compute() { return _args; }
@@ -30,6 +33,12 @@ public class StartNode extends MultiNode {
public Node idealize() { return null; }
// No immediate dominator, and idepth==0
- @Override int idepth() { return 0; }
- @Override Node idom() { return null; }
+ @Override public int idepth() { return 0; }
+ @Override public CFGNode idom(Node dep) { return null; }
+
+ @Override void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) { }
+
+ @Override public int loopDepth() { return (_loopDepth=1); }
+
+ @Override public Node getBlockStart() { return this; }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/StopNode.java b/src/main/java/com/seaofnodes/simple/node/StopNode.java
index 4b4e9615..202afbd2 100644
--- a/src/main/java/com/seaofnodes/simple/node/StopNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/StopNode.java
@@ -6,9 +6,10 @@ import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.GlobalCodeMotion;
-public class StopNode extends Node {
+public class StopNode extends CFGNode {
public final String _src;
@@ -31,7 +32,7 @@ public class StopNode extends Node {
return p.p("]");
}
- @Override public boolean isCFG() { return true; }
+ @Override public boolean blockHead() { return true; }
// If a single Return, return it.
// Otherwise, null because ambiguous.
@@ -54,13 +55,23 @@ public class StopNode extends Node {
return null;
}
- @Override public Node idom() { return null; }
+ @Override public int idepth() {
+ if( _idepth!=0 ) return _idepth;
+ int d=0;
+ for( Node n : _inputs )
+ if( n!=null )
+ d = Math.max(d,((CFGNode)n).idepth()+1);
+ return cacheIDepth(d);
+ }
+ @Override public CFGNode idom(Node dep) { return null; }
+
+ @Override public int loopDepth() { return (_loopDepth=1); }
public Node addReturn(Node node) {
return addDef(node);
}
- public StopNode iterate() { return IterPeeps.iterate(this).typeCheck(); }
+ public StopNode iterate() { return IterPeeps.iterate(this).typeCheck().GCM(); }
public StopNode typeCheck() {
var obs = Parser.PARSER == null ? null : Parser.PARSER._obs;
String err = walk(n -> {
@@ -72,4 +83,13 @@ public class StopNode extends Node {
if( obs != null ) obs.phase("TypeCheck");
return this;
}
+ StopNode GCM() {
+ // Break infinite loops, forcing a Never-branch to exit
+ GlobalCodeMotion.fixLoops(this);
+
+ GlobalCodeMotion.buildCFG(this);
+ return this;
+ }
+
+ @Override public Node getBlockStart() { return this; }
}
diff --git a/src/main/java/com/seaofnodes/simple/node/SubNode.java b/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 6fa87a07..1589abe7 100644
--- a/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -1,6 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
@@ -27,6 +28,7 @@ public class SubNode extends Node {
if (i0.isConstant() && i1.isConstant())
return TypeInteger.constant(i0.value()-i1.value());
}
+
return TypeInteger.BOT;
}
diff --git a/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java b/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java
new file mode 100644
index 00000000..056dfba1
--- /dev/null
+++ b/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java
@@ -0,0 +1,14 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.print.ExprPrinter;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+
+public class XCtrlNode extends CFGNode {
+ public XCtrlNode( ) { super(Parser.START); }
+ @Override public String label() { return "Xctrl"; }
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) { return p.p("Xctrl"); }
+ @Override public Type compute() { return Type.XCONTROL; }
+ @Override public Node idealize() { return null; }
+}
diff --git a/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
index 92d970d8..e65963db 100644
--- a/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
+++ b/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -6,13 +6,13 @@ import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.type.TypeMem;
-/** Chapter 10's view of the IR; browser, transport and layout are shared. */
+/** Chapter 12's view of the IR; browser, transport and layout are shared. */
public class SimpleGraphAdapter extends GraphAdapter<Node> {
@Override protected String[] edgeNames(Node n) {
return n instanceof ScopeNode scope && n.nIns()!=0 ? scope.reverseNames() : null;
}
- @Override protected int projectionIndex(Node n) { return n instanceof ProjNode p ? p._idx : -1; }
+ @Override protected int projectionIndex(Node n) { return n instanceof ProjNode p ? p._idx : n instanceof CProjNode p ? p._idx : -1; }
@Override protected Kind kind(Node n) {
if( n instanceof StopNode ) return Kind.STOP;
@@ -25,9 +25,9 @@ public class SimpleGraphAdapter extends GraphAdapter<Node> {
}
@Override protected Role role(Node n, int i) {
- if( n instanceof ScopeNode || n instanceof ConstantNode ) return Role.ASSOC;
+ if( n instanceof ScopeNode || n instanceof ConstantNode || n instanceof XCtrlNode ) return Role.ASSOC;
if( n instanceof PhiNode ) return i == 0 ? Role.ASSOC : isMem(n) ? Role.MEM : Role.DATA;
- if( n instanceof ProjNode ) return n.isCFG() ? Role.CTRL : isMem(n) ? Role.MEM : Role.DATA;
+ if( n instanceof ProjNode || n instanceof CProjNode ) return n.isCFG() ? 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( i == 1 && (n instanceof MemOpNode || n instanceof ReturnNode) ) return Role.MEM;
diff --git a/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java b/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java
index 5c852f94..3aabd79f 100644
--- a/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java
+++ b/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java
@@ -7,7 +7,7 @@ import com.seaofnodes.simple.node.Node;
import java.util.ArrayList;
import java.io.IOException;
-/** Chapter 12's compilation context; capture and event bookkeeping are shared. */
+/** Chapter 13's compilation context; capture and event bookkeeping are shared. */
public class SimpleGraphObserver extends GraphCapture<Node> {
private Parser _parser;
private Node _ret;
diff --git a/src/main/java/com/seaofnodes/simple/type/TypeTuple.java b/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
index 0aa019a7..783f3f60 100644
--- a/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
+++ b/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
@@ -37,12 +37,11 @@ public class TypeTuple extends Type {
return make(ts);
}
- @Override
- public StringBuilder print(StringBuilder sb) {
- sb.append("[");
+ @Override public StringBuilder print(StringBuilder sb) {
+ sb.append("[ ");
for( Type t : _types )
- t.print(sb).append(",");
- sb.setLength(sb.length()-1);
+ t.print(sb).append(", ");
+ sb.setLength(sb.length()-2);
sb.append("]");
return sb;
}
@@ -58,8 +57,7 @@ public class TypeTuple extends Type {
for( Type t : _types )
t.typeName(sb).append(",");
sb.setLength(sb.length()-1);
- sb.append("]");
- return sb;
+ return sb.append("]");
}
public static final TypeTuple IF_BOTH = make(new Type[]{Type. CONTROL,Type. CONTROL});
diff --git a/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
index 89882b2f..c10dc510 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter01Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -15,7 +15,7 @@ public class Chapter01Test {
StartNode start = Parser.START;
ReturnNode ret = (ReturnNode)stop.in(0);
- assertTrue(ret.ctrl() instanceof ProjNode);
+ assertTrue(ret.ctrl() instanceof CProjNode);
Node expr = ret.expr();
if( expr instanceof ConstantNode con ) {
assertEquals(start,con.in(0));
diff --git a/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index edc62f50..14d31245 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -83,7 +83,7 @@ public class Chapter04Test {
Parser parser = new Parser("return arg; ");
StopNode stop = parser.parse();
ReturnNode ret = stop.ret();
- assertTrue(ret.in(0) instanceof ProjNode);
+ assertTrue(ret.in(0) instanceof CProjNode);
assertTrue(ret.in(1) instanceof ProjNode);
assertEquals(1, ((ProjNode)ret.in(1))._idx);
assertTrue(ret.in(2) instanceof ProjNode);
diff --git a/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/src/test/java/com/seaofnodes/simple/Chapter05Test.java
index 97cd9940..cd646126 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter05Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter05Test.java
@@ -22,7 +22,7 @@ else {
return a;""");
StopNode ret = parser.parse().iterate();
- assertEquals("return Phi(Region19,(arg+2),(arg-3));", ret.toString());
+ assertEquals("return Phi(Region21,(arg+2),(arg-3));", ret.toString());
}
@Test
@@ -37,7 +37,7 @@ if (arg == 1) {
}
return c;""", TypeInteger.BOT);
StopNode ret = parser.parse().iterate();
- assertEquals("return Phi(Region18,4,3);", ret.toString());
+ assertEquals("return Phi(Region20,4,3);", ret.toString());
}
@Test
@@ -65,7 +65,7 @@ else
b=a+1;
return a+b;""");
StopNode ret = parser.parse().iterate();
- assertEquals("return ((arg*2)+Phi(Region22,2,3));", ret.toString());
+ assertEquals("return ((arg*2)+Phi(Region24,2,3));", ret.toString());
}
@Test
@@ -80,7 +80,7 @@ else
a=b+1;
return a+b;""");
StopNode ret = parser.parse().iterate();
- assertEquals("return ((Phi(Region33,(arg*2),arg)+arg)+Phi(Region,4,5));", ret.toString());
+ assertEquals("return ((Phi(Region35,(arg*2),arg)+arg)+Phi(Region,4,5));", ret.toString());
}
@Test
@@ -100,7 +100,7 @@ else
return a;
""", TypeInteger.BOT);
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region37,Phi(Region23,2,3),Phi(Region35,4,5));", stop.toString());
+ assertEquals("return Phi(Region39,Phi(Region25,2,3),Phi(Region37,4,5));", stop.toString());
}
@Test
@@ -116,7 +116,7 @@ if( arg==0 )
return arg+a+b;
""", TypeInteger.BOT);
StopNode stop = parser.parse();
- assertEquals("return ((arg+Phi(Region16,1,0))+Phi(Region32,2,0));", stop.toString());
+ assertEquals("return ((arg+Phi(Region18,1,0))+Phi(Region34,2,0));", stop.toString());
}
@Test
@@ -130,7 +130,7 @@ if( arg==1 )
}
return a;""");
StopNode ret = parser.parse().iterate();
- assertEquals("return (arg==Phi(Region18,3,2));", ret.toString());
+ assertEquals("return (arg==Phi(Region20,3,2));", ret.toString());
}
@Test
diff --git a/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/src/test/java/com/seaofnodes/simple/Chapter06Test.java
index 9af11b68..43e46a5d 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter06Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter06Test.java
@@ -17,7 +17,7 @@ return 1;
""");
StopNode stop = parser.parse().iterate();
assertEquals("return 2;", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -30,7 +30,7 @@ if (arg)
return (arg < a) < 3;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return ((arg<Phi(Region15,2,1))<3);", stop.toString());
+ assertEquals("return ((arg<Phi(Region17,2,1))<3);", stop.toString());
}
@Test
@@ -46,7 +46,7 @@ return a;
""");
StopNode stop = parser.parse().iterate();
assertEquals("return 2;", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -65,7 +65,7 @@ else
b=5;
return b;""", TypeInteger.BOT);
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region38,42,5);", stop.toString());
+ assertEquals("return Phi(Region40,42,5);", stop.toString());
}
@Test
@@ -84,7 +84,7 @@ else
b=5;
return b;""", TypeInteger.BOT);
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region31,2,5);", stop.toString());
+ assertEquals("return Phi(Region33,2,5);", stop.toString());
}
@Test
@@ -144,7 +144,7 @@ else
return a;
""", TypeInteger.BOT);
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region42,3,Phi(Region40,4,5));", stop.toString());
+ assertEquals("return Phi(Region44,3,Phi(Region42,4,5));", stop.toString());
}
@Test
@@ -200,7 +200,7 @@ if( arg ) {
return a+b;
""");
StopNode ret = parser.parse().iterate();
- assertEquals("return Phi(Region27,4,1);", ret.toString());
+ assertEquals("return Phi(Region29,4,1);", ret.toString());
}
@@ -251,7 +251,7 @@ if( arg ) {
return a+b+c;
""");
StopNode ret = parser.parse().iterate();
- assertEquals("return (Phi(Region39,Phi(Region24,2,3),0)+Phi(Region,3,1));", ret.toString());
+ assertEquals("return (Phi(Region41,Phi(Region26,2,3),0)+Phi(Region,3,1));", ret.toString());
}
diff --git a/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/src/test/java/com/seaofnodes/simple/Chapter07Test.java
index ff0ca599..222982e7 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter07Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter07Test.java
@@ -18,8 +18,8 @@ public class Chapter07Test {
return arg;
""");
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop7,arg,(Phi_arg+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop9,arg,(Phi_arg+1));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -35,7 +35,7 @@ if(arg){}else{
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region25,1,Phi(Loop14,1,(Phi_a+1)));", stop.toString());
+ assertEquals("return Phi(Region27,1,Phi(Loop16,1,(Phi_a+1)));", stop.toString());
}
@Test
@@ -55,7 +55,7 @@ while(i < arg) {
return sum;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop9,0,Phi(Loop21,Phi_sum,(Phi_sum+Phi(Loop,0,(Phi_j+1)))));", stop.toString());
+ assertEquals("return Phi(Loop11,0,Phi(Loop23,Phi_sum,(Phi_sum+Phi(Loop,0,(Phi_j+1)))));", stop.toString());
}
@Test
@@ -71,8 +71,8 @@ while(a < 10) {
return b;
""");
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop9,2,Phi(Region28,Phi_b,4));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop11,2,Phi(Region30,Phi_b,4));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -90,8 +90,8 @@ while(a < 10) {
return b;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop9,2,(Phi(Region28,Phi_b,4)+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop11,2,(Phi(Region30,Phi_b,4)+1));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@@ -107,8 +107,8 @@ while(a < 10) {
return a;
""");
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,(Phi_a+3));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -123,8 +123,8 @@ while(a < 10) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,(Phi_a+3));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -136,8 +136,8 @@ while(arg) a = 2;
return a;
""");
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop8,1,2);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,2);", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -149,8 +149,8 @@ while(arg) a = 2;
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop8,1,2);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,2);", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -165,8 +165,8 @@ while(a < 10) {
return a;
""");
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,(Phi_a+3));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -181,8 +181,8 @@ while(a < 10) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,(Phi_a+3));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -198,8 +198,8 @@ while(a < 10) {
return a;
""");
StopNode stop = parser.parse();
- assertEquals("return Phi(Loop9,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop11,1,(Phi_a+3));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -215,8 +215,8 @@ while(a < 10) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop9,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop11,1,(Phi_a+3));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
}
diff --git a/src/test/java/com/seaofnodes/simple/Chapter08Test.java b/src/test/java/com/seaofnodes/simple/Chapter08Test.java
index 02c3a301..89ce8313 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter08Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter08Test.java
@@ -2,7 +2,6 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.*;
-import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Assert;
import org.junit.Test;
@@ -24,7 +23,7 @@ while(arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region38,Phi(Region26,Phi(Loop7,arg,(Phi_arg+1)),Add),Add);", stop.toString());
+ assertEquals("return Phi(Region38,Phi(Region27,Phi(Loop9,arg,(Phi_arg+1)),Add),Add);", stop.toString());
assertTrue(stop.ret().ctrl() instanceof RegionNode);
Assert.assertEquals(5L, Evaluator.evaluate(stop, 1));
Assert.assertEquals(10L, Evaluator.evaluate(stop, 6));
@@ -47,8 +46,8 @@ while(arg < 10) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop8,1,Phi(Region45,Phi_a,(Phi_a+1)));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop10,1,Phi(Region44,Phi_a,(Phi_a+1)));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@@ -66,7 +65,7 @@ while(arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region36,Phi(Loop7,arg,(Phi_arg+1)),Add);", stop.toString());
+ assertEquals("return Phi(Region36,Phi(Loop9,arg,(Phi_arg+1)),Add);", stop.toString());
assertTrue(stop.ret().ctrl() instanceof RegionNode);
}
@@ -82,7 +81,7 @@ while(arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region26,Phi(Loop7,arg,(Phi_arg+1)),Add);", stop.toString());
+ assertEquals("return Phi(Region27,Phi(Loop9,arg,(Phi_arg+1)),Add);", stop.toString());
assertTrue(stop.ret().ctrl() instanceof RegionNode);
}
@@ -101,8 +100,8 @@ while(arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop7,arg,(Phi_arg+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop9,arg,(Phi_arg+1));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@@ -118,8 +117,8 @@ while(arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop7,arg,(Phi_arg+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode);
+ assertEquals("return Phi(Loop9,arg,(Phi_arg+1));", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode);
}
@Test
@@ -186,7 +185,7 @@ while(arg < 10) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region29,Phi(Loop8,1,(Phi_a+1)),Add);", stop.toString());
+ assertEquals("return Phi(Region30,Phi(Loop10,1,(Phi_a+1)),Add);", stop.toString());
assertTrue(stop.ret().ctrl() instanceof RegionNode);
}
@@ -202,8 +201,8 @@ while(1) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return (Phi(Loop8,1,Add)+1);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof ProjNode prj && prj._idx==1 );
+ assertEquals("return (Phi(Loop10,1,Add)+1);", stop.toString());
+ assertTrue(stop.ret().ctrl() instanceof CProjNode prj && prj._idx==1 );
}
}
diff --git a/src/test/java/com/seaofnodes/simple/Chapter09Test.java b/src/test/java/com/seaofnodes/simple/Chapter09Test.java
index b1fdc459..49ee75a2 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter09Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter09Test.java
@@ -5,7 +5,6 @@ import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Assert;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Ignore;
@@ -70,7 +69,7 @@ while (arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop8,arg,(Phi_arg+2));", stop.toString());
+ assertEquals("return Phi(Loop10,arg,(Phi_arg+2));", stop.toString());
Assert.assertEquals(11L, Evaluator.evaluate(stop, 1));
}
@@ -87,7 +86,7 @@ while (arg < 10) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop9,arg,(Phi_arg+4));", stop.toString());
+ assertEquals("return Phi(Loop11,arg,(Phi_arg+4));", stop.toString());
Assert.assertEquals(13L, Evaluator.evaluate(stop, 1));
}
@@ -116,7 +115,7 @@ while (arg) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop15,arg,(Phi_arg+1));", stop.toString());
+ assertEquals("return Phi(Loop17,arg,(Phi_arg+1));", stop.toString());
}
@Test
@@ -134,7 +133,7 @@ while(v1+arg) {
}
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ assertEquals("Stop[ return 0; return 0; ]", stop.toString());
}
@Test
@@ -186,7 +185,7 @@ while(arg) {
return a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop10,0,(-(Phi_a+3)));", stop.toString());
+ assertEquals("return Phi(Loop12,0,(-(Phi_a+3)));", stop.toString());
}
@Test
@@ -200,7 +199,7 @@ int v0=0!=0<-0;
return -0+0+0;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ ]", stop.toString());
+ assertEquals("return 0;", stop.toString());
}
@@ -227,7 +226,7 @@ while(1) {
}
""");
StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ ]", stop.toString());
+ assertEquals("return 0;", stop.toString());
}
@Test
@@ -249,16 +248,17 @@ while(1) {
return 0!=0;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ ]", stop.toString());
+ assertEquals("return 0;", stop.toString());
}
@Test
public void testFuzz6() {
- Parser parser = new Parser("""
+ Parser parser = new Parser(
+"""
int v0=0;
while(0==1) while(v0)
v0=1+v0;
- """);
+""");
StopNode stop = parser.parse().iterate();
assertEquals("return 0;", stop.toString());
}
@@ -276,14 +276,14 @@ while(1)
return v1+v0;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ ]", stop.toString());
+ assertEquals("return 0;", stop.toString());
}
@Test
public void testFuzz8() {
Parser parser = new Parser("while(arg) arg = arg - 1; return arg;");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop7,arg,(Phi_arg-1));", stop.toString());
+ assertEquals("return Phi(Loop9,arg,(Phi_arg-1));", stop.toString());
}
@Test
diff --git a/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index e5d9b882..6649c0b0 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -2,15 +2,11 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.node.StopNode;
-import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.node.LoadNode;
import com.seaofnodes.simple.node.StoreNode;
import com.seaofnodes.simple.evaluator.Evaluator;
import java.util.BitSet;
import org.junit.Test;
-
-import java.util.Arrays;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -44,7 +40,7 @@ public class Chapter10Test {
"if (p != null) return p.x; return -1;",
"if (null != p) return p.x; return -1;",
"if (!!!!p) return p.x; return -1;",
- "if (!!!p) return -1; else return p.x;",
+ "if (!!!p) return -1; return p.x;",
"int b = !!p; if (b) return p.x + b - 1; return -1;"
} ) {
StopNode stop = new Parser(NULLABLE_POINT_SOURCE+body).parse().iterate();
@@ -169,7 +165,7 @@ while (arg) {
return bar.a;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop11,0,(Phi_a+2));", stop.toString());
+ assertEquals("return Phi(Loop13,0,(Phi_a+2));", stop.toString());
}
@Test
@@ -221,7 +217,7 @@ if( bar ) bar.a = 1;
return bar;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region31,(*void)Phi(Region18,null,new Bar),null);", stop.toString());
+ assertEquals("return Phi(Region33,(*void)Phi(Region20,null,new Bar),null);", stop.toString());
}
@Test
@@ -237,7 +233,7 @@ else bar.a = 1;
return rez;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region37,4,3);", stop.toString());
+ assertEquals("return Phi(Region39,4,3);", stop.toString());
}
@Test
@@ -254,7 +250,8 @@ while(arg) {
return ret;
""");
try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'v0'", e.getMessage()); }
+ catch( Exception e ) {
+ assertEquals("Might be null accessing 'v0'", e.getMessage()); }
}
@Test
@@ -290,7 +287,7 @@ while( i.x < i.len ) {
return sum;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop17,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString());
+ assertEquals("return Phi(Loop19,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString());
}
@@ -306,11 +303,12 @@ while(arg) {
v0.v0 = arg;
arg = arg-1;
if (arg==5) ret=v0;
+
}
return ret;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop11,new s0,Phi(Region35,new s0,Phi_ret));", stop.toString());
+ assertEquals("return Phi(Loop13,new s0,Phi(Region37,new s0,Phi_ret));", stop.toString());
}
@Test
@@ -323,11 +321,12 @@ while(arg) {
v0.v0 = arg;
arg = arg-1;
if (arg==5) ret=v0;
+
}
return ret;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop15,new s0,Phi(Region36,new s0,Phi_ret));", stop.toString());
+ assertEquals("return Phi(Loop17,new s0,Phi(Region38,new s0,Phi_ret));", stop.toString());
}
@@ -344,7 +343,7 @@ while(arg < 10) {
return ret;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop11,new s0,Phi(Region33,new s0,Phi_ret));", stop.toString());
+ assertEquals("return Phi(Loop13,new s0,Phi(Region35,new s0,Phi_ret));", stop.toString());
}
@Test
@@ -462,7 +461,6 @@ return 0;
assertEquals("return 0;", stop.toString());
}
-
@Test public void testReadBeforeStores() {
var stop = new Parser("""
struct S { int x; int y; }
@@ -549,8 +547,8 @@ return 0;
if (arg) s.x=arg+1; else s.x=arg+2;
return s;
""").parse().iterate();
- // Releasing the old aggregate exposes the overwritten initializing Store.
- assertEquals(1,countMemoryNodes(stop,StoreNode.class,new BitSet()));
+ // These chapters still bind struct Stores to branch control.
+ assertEquals(3,countMemoryNodes(stop,StoreNode.class,new BitSet()));
assertEquals(2L,((Evaluator.Obj)Evaluator.evaluate(stop,0)).fields()[0]);
assertEquals(4L,((Evaluator.Obj)Evaluator.evaluate(stop,3)).fields()[0]);
}
diff --git a/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 0d4f3c97..cbb50dba 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -29,7 +29,7 @@ while( arg ) {
return head.next.i;
""");
try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'next'",e.getMessage()); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
@Test
diff --git a/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/src/test/java/com/seaofnodes/simple/Chapter13Test.java
new file mode 100644
index 00000000..2929d1a4
--- /dev/null
+++ b/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -0,0 +1,532 @@
+package com.seaofnodes.simple;
+
+
+import com.seaofnodes.simple.evaluator.Evaluator;
+import com.seaofnodes.simple.node.StopNode;
+import org.junit.Test;
+
+
+import static org.junit.Assert.assertEquals;
+
+public class Chapter13Test {
+
+ // A placeholder test used to rapidly rotate through fuzzer produced issues
+ @Test
+ public void testFuzzer() {
+ Parser parser = new Parser(
+"""
+while(-----arg) {
+ while(-arg) {
+ while(-----arg*---0) {
+ int N=--false==false<--arg;
+ while(N<-4==N) {
+ N=N+N;
+ }
+ }
+ {
+ int kzEl_r7EYz=3*----0!=-arg<(37*--97!=0/arg==arg)<=-(--arg!=0);
+ while(kzEl_r7EYz<---1) {
+ kzEl_r7EYz=kzEl_r7EYz+kzEl_r7EYz*arg>=24;
+ if(7) continue;
+ kzEl_r7EYz=false;
+ if(-kzEl_r7EYz+arg<-3) continue;
+ else {
+ while(kzEl_r7EYz) continue;
+ arg=----3;
+ kzEl_r7EYz=-(-----arg*1>-kzEl_r7EYz);
+ int v2uF_4ba=9;
+ }
+ }
+ }
+ int pjcp7rv=arg;
+ {
+ int m2ya3=--arg-pjcp7rv;
+ while(m2ya3<-----pjcp7rv>=pjcp7rv+--pjcp7rv!=---pjcp7rv!=-arg<---(-m2ya3)>m2ya3<=-----true>14!=--m2ya3) {
+ m2ya3=m2ya3+-(--0<=false<pjcp7rv/arg<=-true);
+ while((--m2ya3)<--arg<-----arg*false!=arg<pjcp7rv*arg) {
+ pjcp7rv=-3+-arg;
+ break;
+ }
+ while(-3) {
+ while(67+0-61) m2ya3=m2ya3;
+ if(----pjcp7rv==---(---arg)) pjcp7rv=0!=-23;
+ else pjcp7rv=----89;
+ while(m2ya3!=m2ya3<pjcp7rv) m2ya3=1;
+ while(--arg<=pjcp7rv) pjcp7rv=-32<=-m2ya3+62;
+ }
+ break;
+ }
+ }
+ {
+ int arg=-1<-9!=(-arg>=-pjcp7rv>=arg<=arg);
+ while(arg<-arg) {
+ arg=arg+--arg;
+ {
+ while((--93)--pjcp7rv*-pjcp7rv+arg) arg=0-(6>=(--(---41/(0))!=--pjcp7rv)>=---2*33);
+ pjcp7rv=11;
+ int c=pjcp7rv;
+ if(--107) break;
+ else break;
+ }
+ }
+ }
+ pjcp7rv=pjcp7rv;
+ int aR0Wkua=-pjcp7rv==pjcp7rv;
+ {
+ if(-0!=-0) while(-true) break;
+ else {
+ while(aR0Wkua) pjcp7rv=-0/pjcp7rv;
+ pjcp7rv=pjcp7rv;
+ }
+ }
+ }
+ if(-arg) {
+ {
+ int Isdlt0fr91=0!=arg;
+ while(Isdlt0fr91<-Isdlt0fr91==-Isdlt0fr91+true) {
+ Isdlt0fr91=Isdlt0fr91+-1!=true;
+ if(-arg) if(--arg<=Isdlt0fr91) Isdlt0fr91=-Isdlt0fr91;
+ int i=---arg<---arg!=--40;
+ {
+ int jVX43Ln4Dm=-(6)==-Isdlt0fr91/----0;
+ while(jVX43Ln4Dm<--(3-3)) {
+ jVX43Ln4Dm=jVX43Ln4Dm+-0<25;
+ i=24<--(jVX43Ln4Dm);
+ }
+ }
+ i=(arg!=----false>=7);
+ int Te=5>-arg;
+ break;
+ }
+ }
+ arg=arg!=--true/arg;
+ {
+ int UX=arg;
+ while(UX<-3*arg) {
+ UX=UX+arg/-1;
+ arg=-3--(-UX--47<arg==--arg==arg)/arg--UX>=-arg;
+ if(---12) while(-UX) break;
+ break;
+ }
+ }
+ continue;
+ }
+ if(arg) arg=--0;
+ else if(1) continue;
+ int DtTlhjYq=19==arg;
+ {
+ arg=--DtTlhjYq<=(DtTlhjYq/---DtTlhjYq<=-DtTlhjYq);
+ if(---arg) continue;
+ else {
+ int Zsl=40;
+ while(Zsl<--Zsl--false>((Zsl))==Zsl!=--DtTlhjYq-DtTlhjYq-DtTlhjYq) {
+ Zsl=Zsl+arg;
+ }
+ }
+ if((arg)) if(-DtTlhjYq) {
+ }
+ else while(((2)==3*arg/DtTlhjYq)<=0==---arg) return false;
+ else {
+ int lwbF=arg>=0;
+ while(lwbF<57) {
+ lwbF=lwbF+--(-arg+lwbF)>=-2>=arg!=arg;
+ arg=----0+0;
+ while(49) break;
+ if(true) DtTlhjYq=-----arg;
+ DtTlhjYq=0!=-----(0);
+ }
+ }
+ arg=---arg!=DtTlhjYq/--0;
+ DtTlhjYq=-DtTlhjYq/(--arg<arg);
+ if(32) {
+ int q=-DtTlhjYq;
+ while(q<arg*(0*q)/0) {
+ q=q+DtTlhjYq;
+ DtTlhjYq=arg;
+ }
+ }
+ else if(--arg) if(15) break;
+ else arg=0==arg<=-55>DtTlhjYq;
+ else {
+ }
+ DtTlhjYq=-0;
+ {
+ while((0)<-arg) DtTlhjYq=-DtTlhjYq;
+ }
+ if(30/--(arg)>=0) DtTlhjYq=-0>=arg;
+ else {
+ int Y2wcIQJ=--DtTlhjYq*---11;
+ if(arg==arg) {
+ Y2wcIQJ=-----Y2wcIQJ;
+ Y2wcIQJ=Y2wcIQJ<7;
+ if(DtTlhjYq) arg=arg;
+ else break;
+ while(-arg!=-31==(false<=-15>=-DtTlhjYq+--6*-----Y2wcIQJ<=---arg!=-Y2wcIQJ<=---0<-arg*--Y2wcIQJ)) DtTlhjYq=arg!=3;
+ }
+ continue;
+ }
+ arg=DtTlhjYq;
+ arg=--3/(---arg)>=80>=-DtTlhjYq<=DtTlhjYq;
+ arg=51+---(-true)!=arg!=----1==-DtTlhjYq;
+ if(11<=arg) if((arg)+79<32+(-arg)) continue;
+ else if(true==DtTlhjYq) break;
+ break;
+ }
+}
+if(arg!=62==arg) {
+ while(---arg) {
+ {
+ int bhmDacPM=arg/arg;
+ while(bhmDacPM<arg) {
+ bhmDacPM=bhmDacPM+80*((-bhmDacPM)/---bhmDacPM>1)<bhmDacPM-2!=7;
+ arg=-0;
+ arg=bhmDacPM>=-arg+-arg;
+ break;
+ }
+ }
+ int arg=true;
+ arg=arg<=arg;
+ {
+ int KlC=30;
+ while(KlC<-2) {
+ KlC=KlC+-1!=25<=-KlC>KlC>----0;
+ int Ij=arg>=-arg<=---10;
+ {
+ int LAbgurVa2R=-arg!=Ij;
+ while(LAbgurVa2R<-true) {
+ LAbgurVa2R=LAbgurVa2R+Ij;
+ KlC=-LAbgurVa2R;
+ Ij=42*(-Ij>true);
+ while(Ij) arg=KlC;
+ KlC=-----7>-(--KlC<--7)==-LAbgurVa2R;
+ }
+ }
+ int a=Ij<=-KlC;
+ if(Ij) {
+ int r1W=a!=-false;
+ }
+ else {
+ arg=56>=Ij;
+ while(54) break;
+ arg=8>=-(a<--Ij)/a>=-a-KlC;
+ }
+ int Yt_T=KlC;
+ arg=KlC;
+ }
+ }
+ if(----false/--arg/12+-----0) if(-9+-arg) while(arg!=(arg)) arg=--1>=-71-arg!=---(--arg*11+102)/--arg;
+ else if(----arg) continue;
+ if(-arg-arg) {
+ arg=-(-arg)*--(-3)<(-7==-arg----arg);
+ {
+ int arg=arg;
+ while(arg<-0>arg==-arg-(-arg>1+11)) {
+ arg=arg+6-arg!=--0>---30>=---arg<=arg/(3)+---arg;
+ }
+ }
+ arg=-arg+arg;
+ arg=--47+-arg<1>arg*(-arg==-arg--0>3);
+ arg=(----arg)!=arg+--59;
+ arg=--4+arg==-(arg)-arg;
+ }
+ else {
+ while(--true-----true*-25+-18>arg>=-----(-arg)---13) {
+ arg=-0+--true<false;
+ continue;
+ }
+ arg=true<=arg;
+ break;
+ }
+ arg=arg*22<--(0+-arg);
+ {
+ continue;
+ }
+ }
+ arg=1*---((3<=true)==---2);
+ arg=7+-(-1);
+ return arg+--true;
+}
+else {
+ int o3ypEsewxM=-(---arg);
+ int vzXB=(-(---11<=--o3ypEsewxM>=--0))<=-0/-arg;
+ int L=-arg;
+ int B4mHcrf=---vzXB;
+ int oZWGdzZl=-o3ypEsewxM;
+ int K6tQqa1=11;
+ while(0!=--L) {
+ int sdZfTM=vzXB*true+K6tQqa1;
+ while(sdZfTM<false) {
+ sdZfTM=sdZfTM+arg;
+ sdZfTM=oZWGdzZl;
+ K6tQqa1=-K6tQqa1;
+ arg=---K6tQqa1;
+ while((3+-K6tQqa1)<oZWGdzZl) {
+ if(-----(-1)) sdZfTM=-false;
+ {}
+ }
+ K6tQqa1=vzXB!=-(B4mHcrf+-7>false);
+ sdZfTM=vzXB;
+ }
+ }
+ L=false;
+ while(31) B4mHcrf=arg;
+ oZWGdzZl=-0<--oZWGdzZl<=--2;
+ if(K6tQqa1>=20<=46) {
+ if(-1+-L) if(oZWGdzZl) {
+ int wmhv=-----0;
+ }
+ int arg=0;
+ o3ypEsewxM=---(oZWGdzZl);
+ return -oZWGdzZl/0;
+ }
+ else if(arg) {
+ int yKk5=B4mHcrf>=4>K6tQqa1<-B4mHcrf/o3ypEsewxM;
+ return -K6tQqa1;
+ }
+ else {
+ if(---0) {
+ {}
+ {}
+ }
+ return o3ypEsewxM;
+ }
+}
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 9; return 0; return 0; return 0; return 0; ]", stop.toString());
+ }
+
+
+ @Test
+ public void testPrimes() {
+ Parser parser = new Parser(
+"""
+if( arg < 2 ) return 0;
+int primeCount = 1;
+int prime = 3;
+while( prime <= arg ) {
+ int isPrime = 1;
+ // Check for even case, so the next loop need only check odds
+ if( (prime/2)*2 == prime )
+ continue;
+ // Check odds up to sqrt of prime
+ int j = 3;
+ while( j*j <= prime ) {
+ if( (prime/j)*j == prime ) {
+ isPrime = 0;
+ break;
+ }
+ j = j + 2;
+ }
+ if( isPrime )
+ primeCount = primeCount + 1;
+ prime = prime + 2;
+}
+return primeCount;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return Phi(Loop21,1,Phi(Region88,Phi_primeCount,Phi(Region82,(Phi_primeCount+1),Phi_primeCount))); ]", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 1)); // No primes 1 or below
+ assertEquals(1L, Evaluator.evaluate(stop, 2)); // 2
+ assertEquals(2L, Evaluator.evaluate(stop, 3)); // 2, 3
+ assertEquals(2L, Evaluator.evaluate(stop, 4)); // 2, 3
+ assertEquals(3L, Evaluator.evaluate(stop, 5)); // 2, 3, 5
+ assertEquals(4L, Evaluator.evaluate(stop, 10)); // 2, 3, 5, 7
+ }
+
+ @Test
+ public void testAntiDeps1() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return .f;", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps2() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+S t = new S;
+int i = 0;
+if (arg) {
+ if (arg+1) v = t;
+ i = v.f;
+} else {
+ v.f = 2;
+}
+return i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region37,.f,0);", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps3() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v0 = new S;
+S? v1;
+if (arg) v1 = new S;
+if (v1) {
+ v0.f = v1.f;
+} else {
+ v0.f = 2;
+}
+return v0;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return new S;", stop.toString());
+ }
+
+
+ @Test
+ public void testAntiDeps4() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+S t = new S;
+int i = v.f;
+if (arg+1) arg= 0;
+while (arg) v.f = 2;
+return i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return .f;", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps5() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+while(1) {
+ while(arg+1) { arg=arg-1; }
+ if (arg) break;
+ v.f = 2;
+}
+return v;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return new S;", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps6() {
+ Parser parser = new Parser(
+"""
+struct s { int v; }
+s ptr=new s;
+while( -arg )
+ ptr = new s;
+while(1)
+ arg = arg+ptr.v;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return 0; ]", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps7() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+S t = new S;
+int i = v.f;
+while (arg) {
+ v.f = arg;
+ arg = arg-1;
+}
+return i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return .f;", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps8() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+S t = new S;
+while(arg) {
+ arg=arg-1;
+ int f = v.f;
+ v.f = 2;
+ if (arg) arg = f;
+ else v.f = 3;
+}
+return arg;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop17,arg,Phi(Region41,.f,0));", stop.toString());
+ }
+
+ @Test
+ public void testAntiDeps9() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+S t = new S;
+if (arg) {
+ v.f=2;
+ int i=t.f;
+ v.f=i;
+}
+return v;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return new S;", stop.toString());
+ }
+
+ @Test
+ public void testExample2() {
+ Parser parser = new Parser(
+ """
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+ """);
+ StopNode stop = parser.parse().iterate();
+ //assertEquals("return new S;", stop.toString());
+ }
+
+ @Test
+ public void testSplit() {
+ Parser parser = new Parser(
+ """
+struct S { int f; }
+S s = new S;
+if( arg==0 ) s.f = 1;
+else if (arg == 1) s.f = 1;
+return s.f;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region43,1,Phi(Region38,1,0));", stop.toString());
+ assertEquals(1L, Evaluator.evaluate(stop, 0));
+ assertEquals(1L, Evaluator.evaluate(stop, 1));
+ assertEquals(0L, Evaluator.evaluate(stop, 2));
+ }
+}
diff --git a/src/test/java/com/seaofnodes/simple/FuzzerWrap.java b/src/test/java/com/seaofnodes/simple/FuzzerWrap.java
index 019140ad..52c2a014 100644
--- a/src/test/java/com/seaofnodes/simple/FuzzerWrap.java
+++ b/src/test/java/com/seaofnodes/simple/FuzzerWrap.java
@@ -29,8 +29,8 @@ public class FuzzerWrap {
fuzzer.fuzzPeepsRegression(seed);
}
- @Test
@Ignore
+ @Test
public void fuzzPeepsLarge() {
var fuzzer = new Fuzzer();
for (int i=0; i<1000000; i++)
diff --git a/src/test/java/com/seaofnodes/simple/TypeTest.java b/src/test/java/com/seaofnodes/simple/TypeTest.java
index 298fc6f8..9e8a1418 100644
--- a/src/test/java/com/seaofnodes/simple/TypeTest.java
+++ b/src/test/java/com/seaofnodes/simple/TypeTest.java
@@ -2,8 +2,8 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.type.*;
import org.junit.Assert;
+import org.junit.Ignore;
import org.junit.Test;
-
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
@@ -37,8 +37,8 @@ public class TypeTest {
Assert.assertEquals(TypeMem .BOT, m3.meet(m4));
Assert.assertEquals(m1, m1.glb());
- Assert.assertEquals(m1, m1.dual());
- Assert.assertEquals(m1, m1.glb().dual());
+ Assert.assertEquals(TypeMem.make(1), m1.dual());
+ Assert.assertEquals(m1.dual(), m1.glb().dual());
TypeMemPtr ptr1 = TypeMemPtr.make(s1);
Assert.assertEquals(s1, ptr1._obj);
@@ -128,10 +128,13 @@ public class TypeTest {
private static void check_symmetric( Type t0, Type t1 ) {
if( t1==t0 ) return;
Type mt = t0.meet(t1);
- Type ta = mt.dual().meet(t1.dual());
- Type tb = mt.dual().meet(t0.dual());
- assertSame(ta,t1.dual());
- assertSame(tb,t0.dual());
+ Type dm = mt.dual();
+ Type d0 = t0.dual();
+ Type d1 = t1.dual();
+ Type ta = dm.meet(d1);
+ Type tb = dm.meet(d0);
+ assertSame(ta,d1);
+ assertSame(tb,d0);
}
private static void assoc( Type t0, Type t1, Type t2 ) {
@@ -142,4 +145,13 @@ public class TypeTest {
assertSame(t01_2,t0_12);
}
+ // Test cyclic types and meets
+ @Test
+ public void testCyclic0() {
+ Type d0 = TypeStruct.S1.dual();
+ Type d1 = d0.dual();
+ assertSame(TypeStruct.S1,d1);
+ }
+
+
}
diff --git a/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
index ac7c5e09..355d906b 100644
--- a/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -140,7 +140,8 @@ public class Evaluator {
case StoreNode store -> store(store);
case MemMergeNode merge -> MEMORY;
case NewNode alloc -> alloc(alloc);
- case ProjNode proj -> ((Object[])val(proj.ctrl()))[proj._idx];
+ case CProjNode cproj -> ((Object[])val(cproj.ctrl()))[cproj._idx];
+ case ProjNode proj -> ((Object[])val( proj.in(0) ))[ proj._idx];
default -> throw new AssertionError("Unexpected node " + node);
};
}
diff --git a/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java b/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java
index 48ac0562..d16f4300 100644
--- a/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java
+++ b/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java
@@ -164,7 +164,7 @@ public class Scheduler {
* @return true if the node is not XCtrl.
*/
private static boolean isNotXCtrl(Node node) {
- return !(node instanceof ConstantNode c && c.compute() == Type.XCONTROL);
+ return !(node instanceof ConstantNode c && c.compute() == Type.XCONTROL) && !(node instanceof XCtrlNode);
}
/**
@@ -215,7 +215,7 @@ public class Scheduler {
private void refinePlacement(NodeData data, BasicBlock block) {
assert !isPinnedNode(data);
data.block = data.block == null ? block : dom(data.block, block);
- assert isValid(data) : data.node.uniqueName()+" at "+data.block.entry.uniqueName();
+ assert isValid(data);
}
/**
@@ -328,6 +328,8 @@ public class Scheduler {
for(int i=1; i<r.nIns(); i++) {
if (od(r.in(i)).map(d->d.block==null).orElse(false)) return false;
}
+ } else if (node instanceof XCtrlNode) {
+ return false;
} else {
assert d(node.in(0)).block != null;
}
@@ -403,7 +405,7 @@ public class Scheduler {
}
}
if (!(node instanceof ReturnNode))
- for (Node n:node._outputs) if (n.isCFG() && isCFGNodeReady(n) && d(n).block == null) queue.push(d(n));
+ for (Node n:node._outputs) if (n!=null && n.isCFG() && isCFGNodeReady(n) && d(n).block == null) queue.push(d(n));
var b = block;
for(var in:data.node._inputs) od(in).ifPresent(d->update(d, b));
}
@@ -449,7 +451,7 @@ public class Scheduler {
node = data.node;
assert node.isCFG();
if (!(node instanceof ReturnNode)) {
- for (var out : node._outputs) if (out.isCFG() && isNotXCtrl(out)) markAlive(cfgQueue, out, true);
+ for (var out : node._outputs) if (out!=null && out.isCFG() && isNotXCtrl(out)) markAlive(cfgQueue, out, true);
}
for (var in : node._inputs) if(in!=null && !in.isCFG() && isNotXCtrl(in)) markAlive(dataQueue, in, false);
}
@@ -503,7 +505,7 @@ public class Scheduler {
*/
private static Node findSingleCFGOut(Node node) {
if (node instanceof StartNode) {
- for(var n:node._outputs) if (n instanceof ProjNode p && p._idx==0) return p;
+ for(var n:node._outputs) if (n instanceof CProjNode p && p._idx==0) return p;
return null;
}
assert node._outputs.stream().filter(Node::isCFG).limit(2).count()<=1;
@@ -570,7 +572,7 @@ public class Scheduler {
switch(block.exit) {
case null: break;
case IfNode i:
- for(var out:i._outputs) if (out instanceof ProjNode p) block.next[p._idx] = blocks.get(p);
+ for(var out:i._outputs) if (out instanceof CProjNode p) block.next[p._idx] = blocks.get(p);
break;
case RegionNode r:
block.next[0] = blocks.get(r);