Skip to content

Commit

Permalink
some very minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed May 30, 2022
1 parent 339ce44 commit 973ff33
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
1 change: 1 addition & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)

for currpc in bbstart:bbend
frame.currpc = currpc
empty_backedges!(frame, currpc)
stmt = frame.src.code[currpc]
# If we're at the end of the basic block ...
if currpc == bbend
Expand Down
10 changes: 8 additions & 2 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ function add_backedge!(li::MethodInstance, caller::InferenceState)
edges = caller.stmt_edges[caller.currpc] = []
end
push!(edges, li)
nothing
return nothing
end

# used to temporarily accumulate our no method errors to later add as backedges in the callee method table
Expand All @@ -480,7 +480,13 @@ function add_mt_backedge!(mt::Core.MethodTable, @nospecialize(typ), caller::Infe
end
push!(edges, mt)
push!(edges, typ)
nothing
return nothing
end

function empty_backedges!(frame::InferenceState, currpc::Int = frame.currpc)
edges = frame.stmt_edges[currpc]
edges === nothing || empty!(edges)
return nothing
end

function print_callstack(sv::InferenceState)
Expand Down
34 changes: 15 additions & 19 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -687,33 +687,29 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
nslots = length(slotflags)
undefs = fill(false, nslots)

# eliminate GotoIfNot if either of branch target is unreachable
if run_optimizer
for idx = 1:nexpr
stmt = body[idx]
if isa(stmt, GotoIfNot) && widenconst(argextype(stmt.cond, src, sv.sptypes)) === Bool
# replace live GotoIfNot with:
# - GotoNode if the fallthrough target is unreachable
# - no-op if the branch target is unreachable
if !was_reached(sv, idx+1)
body[idx] = GotoNode(stmt.dest)
elseif !was_reached(sv, stmt.dest)
body[idx] = nothing
end
end
end
end

# this statement traversal does three things:
# this statement traversal does five things:
# 1. introduce temporary `TypedSlot`s that are supposed to be replaced with π-nodes later
# 2. mark used-undef slots (required by the `slot2reg` conversion)
# 3. mark unreached statements for a bulk code deletion (see issue #7836)
# 4. widen `Conditional`s and remove `NOT_FOUND` from `ssavaluetypes`
# NOTE: because of 4, `was_reached` will no longer be available after this point
# NOTE because of this, `was_reached` will no longer be available after this point
# 5. eliminate GotoIfNot if either branch target is unreachable
changemap = nothing # initialized if there is any dead region
for i = 1:nexpr
expr = body[i]
if was_reached(sv, i)
if run_optimizer
if isa(expr, GotoIfNot) && widenconst(argextype(expr.cond, src, sv.sptypes)) === Bool
# 5: replace this live GotoIfNot with:
# - GotoNode if the fallthrough target is unreachable
# - no-op if the branch target is unreachable
if !was_reached(sv, i+1)
expr = GotoNode(expr.dest)
elseif !was_reached(sv, expr.dest)
expr = nothing
end
end
end
body[i] = annotate_slot_load!(undefs, i, sv, expr) # 1&2
ssavaluetypes[i] = widenconditional(ssavaluetypes[i]) # 4
else # i.e. any runtime execution will never reach this statement
Expand Down

0 comments on commit 973ff33

Please sign in to comment.