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 Dec 15, 2019
1 parent a2aa3c0 commit b3f91d6
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 @@ -23,38 +23,11 @@ struct ReturnNode
ReturnNode() = new()
end

"""
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 @@ -491,7 +464,7 @@ mutable struct IncrementalCompact
result_lines::Vector{Int32}
result_flags::Vector{UInt8}
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 b3f91d6

Please sign in to comment.