Skip to content

Commit 140b70c

Browse files
oxinaboxvchuravy
authored andcommitted
Add some comments to domtree code (#32719)
Co-Authored-By: Valentin Churavy <[email protected]>
1 parent d8b5118 commit 140b70c

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

base/compiler/ssair/domtree.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3+
"Represents a Basic Block, in the DomTree"
34
struct DomTreeNode
5+
# How deep we are in the DomTree
46
level::Int
7+
# The BB indices in the CFG for all Basic Blocks we immediately dominate
58
children::Vector{Int}
69
end
710
DomTreeNode() = DomTreeNode(1, Vector{Int}())
811

12+
"Data structure that encodes which basic block dominates which."
913
struct DomTree
14+
# Which basic block immediately dominates each basic block (ordered by BB indices)
15+
# Note: this is the inverse of the nodes, children field
1016
idoms::Vector{Int}
17+
18+
# The nodes in the tree (ordered by BB indices)
1119
nodes::Vector{DomTreeNode}
1220
end
1321

1422
"""
15-
Checks if bb1 dominates bb2
23+
Checks if bb1 dominates bb2.
24+
bb1 and bb2 are indexes into the CFG blocks.
25+
bb1 dominates bb2 if the only way to enter bb2 is via bb1.
26+
(Other blocks may be in between, e.g bb1->bbX->bb2).
1627
"""
1728
function dominates(domtree::DomTree, bb1::Int, bb2::Int)
1829
bb1 == bb2 && return true
@@ -38,11 +49,13 @@ function update_level!(domtree::Vector{DomTreeNode}, node::Int, level::Int)
3849
end
3950
end
4051

52+
"Iterable data structure that walks though all dominated blocks"
4153
struct DominatedBlocks
4254
domtree::DomTree
4355
worklist::Vector{Int}
4456
end
4557

58+
"Returns an iterator that walks through all blocks dominated by the basic block at index `root`"
4659
function dominated(domtree::DomTree, root::Int)
4760
doms = DominatedBlocks(domtree, Vector{Int}())
4861
push!(doms.worklist, root)
@@ -222,7 +235,7 @@ begin
222235
v::DFSNumber, last_linked::DFSNumber)
223236
# TODO: There is a smarter way to do this
224237
u = ancestors[v]
225-
worklist = Tuple{Int, Int}[(u,v)]
238+
worklist = Tuple{DFSNumber, DFSNumber}[(u,v)]
226239
@assert u < v
227240
while !isempty(worklist)
228241
u, v = last(worklist)
@@ -241,6 +254,11 @@ begin
241254
end
242255

243256
"""
257+
SNCA(cfg::CFG)
258+
259+
Determines a map from basic blocks to the block which immediately dominate them.
260+
Expressed as indexes into `cfg.blocks`.
261+
244262
The main Semi-NCA algrithm. Matches Figure 2.8 in [LG05].
245263
Note that the pseudocode in [LG05] is not entirely accurate.
246264
The best way to understand what's happening is to read [LT79], then the
@@ -253,7 +271,7 @@ begin
253271
# the paper doesn't make that clear). The rational for this is Lemma
254272
# 2.4 in [LG05] (i.e. Theorem 4 in ). Note however, that we don't
255273
# ever look at `semi` until it is fully initialized, so we could leave
256-
# it unitialized here if we wanted to.
274+
# it uninitialized here if we wanted to.
257275
state = Node[ Node(typemax(DFSNumber), w) for w in preorder(D) ]
258276
# Initialize idoms to parents. Note that while idoms are eventually
259277
# BB indexed, we keep it DFS indexed until a final post-processing
@@ -302,6 +320,7 @@ begin
302320
end
303321
idoms_dfs[v] = idom
304322
end
323+
# Reexpress the idom relationship in BB indexing
305324
idoms_bb = Int[ (i == 1 || D.reverse[i] == 0) ? 0 : D.numbering[idoms_dfs[D.reverse[i]]] for i = 1:length(cfg.blocks) ]
306325
idoms_bb
307326
end

0 commit comments

Comments
 (0)