Skip to content

Commit

Permalink
Move BasicBlock to separate source file
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
yhls committed Aug 3, 2020
1 parent 0b3c63c commit ff20764
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
32 changes: 32 additions & 0 deletions base/compiler/ssair/basicblock.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

"""
Like UnitRange{Int}, but can handle the `last` field, being temporarily
< first (this can happen during compacting)
"""
struct StmtRange <: AbstractUnitRange{Int}
start::Int
stop::Int
end

first(r::StmtRange) = r.start
last(r::StmtRange) = r.stop
iterate(r::StmtRange, state=0) = (last(r) - first(r) < state) ? nothing : (first(r) + state, state + 1)

StmtRange(range::UnitRange{Int}) = StmtRange(first(range), last(range))

struct BasicBlock
stmts::StmtRange
preds::Vector{Int}
succs::Vector{Int}
end

function BasicBlock(stmts::StmtRange)
return BasicBlock(stmts, Int[], Int[])
end

function BasicBlock(old_bb, stmts)
return BasicBlock(stmts, old_bb.preds, old_bb.succs)
end

copy(bb::BasicBlock) = BasicBlock(bb.stmts, copy(bb.preds), copy(bb.succs))
3 changes: 2 additions & 1 deletion base/compiler/ssair/driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ else
end
end

include("compiler/ssair/ir.jl")
include("compiler/ssair/basicblock.jl")
include("compiler/ssair/domtree.jl")
include("compiler/ssair/ir.jl")
include("compiler/ssair/slot2ssa.jl")
include("compiler/ssair/queries.jl")
include("compiler/ssair/passes.jl")
Expand Down
31 changes: 2 additions & 29 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,11 @@ Core.PhiNode() = Core.PhiNode(Any[], Any[])

isterminator(@nospecialize(stmt)) = isa(stmt, GotoNode) || isa(stmt, GotoIfNot) || isa(stmt, ReturnNode)

"""
Like UnitRange{Int}, but can handle the `last` field, being temporarily
< first (this can happen during compacting)
"""
struct StmtRange <: AbstractUnitRange{Int}
start::Int
stop::Int
end
first(r::StmtRange) = r.start
last(r::StmtRange) = r.stop
iterate(r::StmtRange, state=0) = (last(r) - first(r) < state) ? nothing : (first(r) + state, state + 1)

StmtRange(range::UnitRange{Int}) = StmtRange(first(range), last(range))

struct BasicBlock
stmts::StmtRange
preds::Vector{Int}
succs::Vector{Int}
end
function BasicBlock(stmts::StmtRange)
return BasicBlock(stmts, Int[], Int[])
end
function BasicBlock(old_bb, stmts)
return BasicBlock(stmts, old_bb.preds, old_bb.succs)
end
copy(bb::BasicBlock) = BasicBlock(bb.stmts, copy(bb.preds), copy(bb.succs))

struct CFG
blocks::Vector{BasicBlock}
index::Vector{Int} # map from instruction => basic-block number
# TODO: make this O(1) instead of O(log(n_blocks))?
domtree # TODO type
domtree::DomTree
end

function CFG(blocks::Vector{BasicBlock}, index::Vector{Int})
Expand Down Expand Up @@ -527,7 +500,7 @@ mutable struct IncrementalCompact
ir::IRCode
result::InstructionStream
result_bbs::Vector{BasicBlock}
result_domtree # TODO type
result_domtree::DomTree

ssa_rename::Vector{Any}
# `bb_rename_pred` is for renaming predecessors to the blocks they have
Expand Down

0 comments on commit ff20764

Please sign in to comment.