Skip to content

Commit b81a20c

Browse files
authored
optimize compute_basic_blocks a bit (JuliaLang#45364)
By scalar-folding `basic_blocks_starts(stmts)::BitSet`
1 parent 7b421f0 commit b81a20c

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

base/compiler/ssair/ir.jl

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838

3939
block_for_inst(cfg::CFG, inst::Int) = block_for_inst(cfg.index, inst)
4040

41-
function basic_blocks_starts(stmts::Vector{Any})
41+
@inline function basic_blocks_starts(stmts::Vector{Any})
4242
jump_dests = BitSet()
4343
push!(jump_dests, 1) # function entry point
4444
# First go through and compute jump destinations
@@ -85,15 +85,14 @@ function basic_blocks_starts(stmts::Vector{Any})
8585
end
8686

8787
function compute_basic_blocks(stmts::Vector{Any})
88-
bb_starts = basic_blocks_starts(stmts)
8988
# Compute ranges
89+
bb_starts = basic_blocks_starts(stmts) # ::BitSet and already sorted
9090
pop!(bb_starts, 1)
91-
basic_block_index = sort!(collect(bb_starts); alg=QuickSort)
92-
blocks = BasicBlock[]
93-
sizehint!(blocks, length(basic_block_index))
91+
basic_block_index = Int[bb for bb in bb_starts]
92+
blocks = Vector{BasicBlock}(undef, length(basic_block_index))
9493
let first = 1
95-
for last in basic_block_index
96-
push!(blocks, BasicBlock(StmtRange(first, last - 1)))
94+
for (i, last) in enumerate(basic_block_index)
95+
blocks[i] = BasicBlock(StmtRange(first, last - 1))
9796
first = last
9897
end
9998
end
@@ -120,16 +119,14 @@ function compute_basic_blocks(stmts::Vector{Any})
120119
push!(blocks[block′].preds, num)
121120
push!(b.succs, block′)
122121
end
123-
elseif isa(terminator, Expr)
124-
if terminator.head === :enter
125-
# :enter gets a virtual edge to the exception handler and
126-
# the exception handler gets a virtual edge from outside
127-
# the function.
128-
block′ = block_for_inst(basic_block_index, terminator.args[1]::Int)
129-
push!(blocks[block′].preds, num)
130-
push!(blocks[block′].preds, 0)
131-
push!(b.succs, block′)
132-
end
122+
elseif isexpr(terminator, :enter)
123+
# :enter gets a virtual edge to the exception handler and
124+
# the exception handler gets a virtual edge from outside
125+
# the function.
126+
block′ = block_for_inst(basic_block_index, terminator.args[1]::Int)
127+
push!(blocks[block′].preds, num)
128+
push!(blocks[block′].preds, 0)
129+
push!(b.succs, block′)
133130
end
134131
# statement fall-through
135132
if num + 1 <= length(blocks)

base/compiler/ssair/passes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse
998998
else
999999
phiblocks = iterated_dominance_frontier(ir.cfg, ldu, get(lazydomtree))
10001000
end
1001-
allblocks = sort(vcat(phiblocks, ldu.def_bbs))
1001+
allblocks = sort!(vcat(phiblocks, ldu.def_bbs); alg=QuickSort)
10021002
blocks[fidx] = phiblocks, allblocks
10031003
if fidx + 1 > length(defexpr.args)
10041004
for i = 1:length(du.uses)

0 commit comments

Comments
 (0)