Skip to content

Commit f7852e3

Browse files
yhlsvchuravy
authored andcommitted
Move BasicBlock to separate source file
This is so we can add type declarations to fields in ir.jl that are domtrees, by breaking the dependency loop between domtree.jl (uses basic blocks but defines domtrees) and ir.jl (uses domtrees but defined basic blocks).
1 parent 12df875 commit f7852e3

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

base/compiler/ssair/basicblock.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
"""
4+
Like UnitRange{Int}, but can handle the `last` field, being temporarily
5+
< first (this can happen during compacting)
6+
"""
7+
struct StmtRange <: AbstractUnitRange{Int}
8+
start::Int
9+
stop::Int
10+
end
11+
12+
first(r::StmtRange) = r.start
13+
last(r::StmtRange) = r.stop
14+
iterate(r::StmtRange, state=0) = (last(r) - first(r) < state) ? nothing : (first(r) + state, state + 1)
15+
16+
StmtRange(range::UnitRange{Int}) = StmtRange(first(range), last(range))
17+
18+
struct BasicBlock
19+
stmts::StmtRange
20+
preds::Vector{Int}
21+
succs::Vector{Int}
22+
end
23+
24+
function BasicBlock(stmts::StmtRange)
25+
return BasicBlock(stmts, Int[], Int[])
26+
end
27+
28+
function BasicBlock(old_bb, stmts)
29+
return BasicBlock(stmts, old_bb.preds, old_bb.succs)
30+
end
31+
32+
copy(bb::BasicBlock) = BasicBlock(bb.stmts, copy(bb.preds), copy(bb.succs))

base/compiler/ssair/driver.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ else
1010
end
1111
end
1212

13-
include("compiler/ssair/ir.jl")
13+
include("compiler/ssair/basicblock.jl")
1414
include("compiler/ssair/domtree.jl")
15+
include("compiler/ssair/ir.jl")
1516
include("compiler/ssair/slot2ssa.jl")
1617
include("compiler/ssair/queries.jl")
1718
include("compiler/ssair/passes.jl")

base/compiler/ssair/ir.jl

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,14 @@
33
@inline isexpr(@nospecialize(stmt), head::Symbol) = isa(stmt, Expr) && stmt.head === head
44
Core.PhiNode() = Core.PhiNode(Int32[], Any[])
55

6-
"""
7-
Like UnitRange{Int}, but can handle the `last` field, being temporarily
8-
< first (this can happen during compacting)
9-
"""
10-
struct StmtRange <: AbstractUnitRange{Int}
11-
start::Int
12-
stop::Int
13-
end
14-
first(r::StmtRange) = r.start
15-
last(r::StmtRange) = r.stop
16-
iterate(r::StmtRange, state=0) = (last(r) - first(r) < state) ? nothing : (first(r) + state, state + 1)
17-
18-
StmtRange(range::UnitRange{Int}) = StmtRange(first(range), last(range))
19-
20-
struct BasicBlock
21-
stmts::StmtRange
22-
preds::Vector{Int}
23-
succs::Vector{Int}
24-
end
25-
function BasicBlock(stmts::StmtRange)
26-
return BasicBlock(stmts, Int[], Int[])
27-
end
28-
function BasicBlock(old_bb, stmts)
29-
return BasicBlock(stmts, old_bb.preds, old_bb.succs)
30-
end
31-
copy(bb::BasicBlock) = BasicBlock(bb.stmts, copy(bb.preds), copy(bb.succs))
6+
isterminator(@nospecialize(stmt)) = isa(stmt, GotoNode) || isa(stmt, GotoIfNot) || isa(stmt, ReturnNode)
327

338
struct CFG
349
blocks::Vector{BasicBlock}
3510
index::Vector{Int} # map from instruction => basic-block number
3611
# TODO: make this O(1) instead of O(log(n_blocks))?
3712
end
13+
3814
copy(c::CFG) = CFG(BasicBlock[copy(b) for b in c.blocks], copy(c.index))
3915

4016
function cfg_insert_edge!(cfg::CFG, from::Int, to::Int)

0 commit comments

Comments
 (0)