@@ -1373,8 +1373,19 @@ function kill_edge!(ir::IRCode, from::Int, to::Int, callback=nothing)
13731373 kill_edge! (ir. cfg. blocks, from, to, callback)
13741374end
13751375
1376- # N.B.: from and to are non-renamed indices
1377- function kill_edge! (compact:: IncrementalCompact , active_bb:: Int , from:: Int , to:: Int )
1376+ @inline function compacted_stmt_range (compact:: IncrementalCompact , bb:: BasicBlock , active_bb:: Int , to:: Int )
1377+ to == active_bb && return StmtRange (first (bb. stmts), compact. result_idx - 1 )
1378+ return bb. stmts
1379+ end
1380+
1381+ """
1382+ kill_edge_terminator!(compact::IncrementalCompact, active_bb::Int, from::Int, to::Int)
1383+
1384+ Kill a CFG edge while compacting a terminator in `active_bb`. Assumes all PhiNode
1385+ block statements in `to` have already been processed, so the active BB may only
1386+ scan the compacted prefix when `to == active_bb`. `from` and `to` are non-renamed indices.
1387+ """
1388+ function kill_edge_terminator! (compact:: IncrementalCompact , active_bb:: Int , from:: Int , to:: Int )
13781389 # Note: We recursively kill as many edges as are obviously dead.
13791390 (; bb_rename_pred, bb_rename_succ, result_bbs, domtree) = compact. cfg_transform
13801391 preds = result_bbs[bb_rename_succ[to]]. preds
@@ -1390,7 +1401,7 @@ function kill_edge!(compact::IncrementalCompact, active_bb::Int, from::Int, to::
13901401 for succ in copy (to_succs)
13911402 new_succ = findfirst (x:: Int -> x== succ, bb_rename_pred)
13921403 new_succ === nothing && continue
1393- kill_edge ! (compact, active_bb, to, new_succ)
1404+ kill_edge_terminator ! (compact, active_bb, to, new_succ)
13941405 end
13951406 empty! (preds)
13961407 empty! (to_succs)
@@ -1412,8 +1423,9 @@ function kill_edge!(compact::IncrementalCompact, active_bb::Int, from::Int, to::
14121423 # Remove this edge from all phi nodes in `to` block
14131424 # NOTE: It is possible for `to` to contain only `nothing` statements,
14141425 # so we must be careful to stop at its last statement
1415- if to < active_bb
1416- stmts = result_bbs[bb_rename_succ[to]]. stmts
1426+ if to <= active_bb
1427+ bb = result_bbs[bb_rename_succ[to]]
1428+ stmts = compacted_stmt_range (compact, bb, active_bb, to)
14171429 idx = first (stmts)
14181430 while idx <= last (stmts)
14191431 stmt = compact. result[idx][:stmt ]
@@ -1493,14 +1505,14 @@ function process_node!(compact::IncrementalCompact, result_idx::Int, inst::Instr
14931505 if cond
14941506 ssa_rename[idx] = nothing
14951507 result[result_idx][:stmt ] = nothing
1496- kill_edge ! (compact, active_bb, active_bb, stmt. dest)
1508+ kill_edge_terminator ! (compact, active_bb, active_bb, stmt. dest)
14971509 # Don't increment result_idx => Drop this statement
14981510 else
14991511 label = bb_rename_succ[stmt. dest]
15001512 @assert label > 0
15011513 ssa_rename[idx] = SSAValue (result_idx)
15021514 result[result_idx][:stmt ] = GotoNode (label)
1503- kill_edge ! (compact, active_bb, active_bb, active_bb+ 1 )
1515+ kill_edge_terminator ! (compact, active_bb, active_bb, active_bb+ 1 )
15041516 result_idx += 1
15051517 end
15061518 else
@@ -1675,7 +1687,10 @@ function process_node!(compact::IncrementalCompact, result_idx::Int, inst::Instr
16751687 stmt = ssa_rename[stmt. id]
16761688 end
16771689 elseif isa (stmt, NewSSAValue)
1678- stmt = SSAValue (stmt. id)
1690+ if stmt. id > 0
1691+ # Negative ids reference new_new_nodes and must remain NewSSAValue.
1692+ stmt = SSAValue (stmt. id)
1693+ end
16791694 else
16801695 # Constant assign, replace uses of this ssa value with its result
16811696 end
0 commit comments