Skip to content

Commit 283aee5

Browse files
committed
some very minor optimizations
1 parent 9338c91 commit 283aee5

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
22972297

22982298
for currpc in bbstart:bbend
22992299
frame.currpc = currpc
2300+
empty_backedges!(frame, currpc)
23002301
stmt = frame.src.code[currpc]
23012302
# If we're at the end of the basic block ...
23022303
if currpc == bbend

base/compiler/inferencestate.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ function add_backedge!(li::MethodInstance, caller::InferenceState)
468468
edges = caller.stmt_edges[caller.currpc] = []
469469
end
470470
push!(edges, li)
471-
nothing
471+
return nothing
472472
end
473473

474474
# used to temporarily accumulate our no method errors to later add as backedges in the callee method table
@@ -480,7 +480,13 @@ function add_mt_backedge!(mt::Core.MethodTable, @nospecialize(typ), caller::Infe
480480
end
481481
push!(edges, mt)
482482
push!(edges, typ)
483-
nothing
483+
return nothing
484+
end
485+
486+
function empty_backedges!(frame::InferenceState, currpc::Int = frame.currpc)
487+
edges = frame.stmt_edges[currpc]
488+
edges === nothing || empty!(edges)
489+
return nothing
484490
end
485491

486492
function print_callstack(sv::InferenceState)

base/compiler/typeinfer.jl

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -684,33 +684,29 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
684684
nslots = length(slotflags)
685685
undefs = fill(false, nslots)
686686

687-
# eliminate GotoIfNot if either of branch target is unreachable
688-
if run_optimizer
689-
for idx = 1:nexpr
690-
stmt = body[idx]
691-
if isa(stmt, GotoIfNot) && widenconst(argextype(stmt.cond, src, sv.sptypes)) === Bool
692-
# replace live GotoIfNot with:
693-
# - GotoNode if the fallthrough target is unreachable
694-
# - no-op if the branch target is unreachable
695-
if !was_reached(sv, idx+1)
696-
body[idx] = GotoNode(stmt.dest)
697-
elseif !was_reached(sv, stmt.dest)
698-
body[idx] = nothing
699-
end
700-
end
701-
end
702-
end
703-
704-
# this statement traversal does three things:
687+
# this statement traversal does five things:
705688
# 1. introduce temporary `TypedSlot`s that are supposed to be replaced with π-nodes later
706689
# 2. mark used-undef slots (required by the `slot2reg` conversion)
707690
# 3. mark unreached statements for a bulk code deletion (see issue #7836)
708691
# 4. widen `Conditional`s and remove `NOT_FOUND` from `ssavaluetypes`
709-
# NOTE: because of 4, `was_reached` will no longer be available after this point
692+
# NOTE because of this, `was_reached` will no longer be available after this point
693+
# 5. eliminate GotoIfNot if either branch target is unreachable
710694
changemap = nothing # initialized if there is any dead region
711695
for i = 1:nexpr
712696
expr = body[i]
713697
if was_reached(sv, i)
698+
if run_optimizer
699+
if isa(expr, GotoIfNot) && widenconst(argextype(expr.cond, src, sv.sptypes)) === Bool
700+
# 5: replace this live GotoIfNot with:
701+
# - GotoNode if the fallthrough target is unreachable
702+
# - no-op if the branch target is unreachable
703+
if !was_reached(sv, i+1)
704+
expr = GotoNode(expr.dest)
705+
elseif !was_reached(sv, expr.dest)
706+
expr = nothing
707+
end
708+
end
709+
end
714710
body[i] = annotate_slot_load!(undefs, i, sv, expr) # 1&2
715711
ssavaluetypes[i] = widenconditional(ssavaluetypes[i]) # 4
716712
else # i.e. any runtime execution will never reach this statement

0 commit comments

Comments
 (0)