Skip to content

Commit b64850a

Browse files
committed
working
1 parent e2e780b commit b64850a

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

src/codeedges.jl

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@ struct CodeEdges
369369
preds::Vector{Vector{Int}}
370370
succs::Vector{Vector{Int}}
371371
byname::Dict{GlobalRef,Variable}
372+
slotassigns::Vector{Vector{Int}}
372373
end
373-
CodeEdges(n::Integer) = CodeEdges([Int[] for i = 1:n], [Int[] for i = 1:n], Dict{GlobalRef,Variable}())
374+
CodeEdges(nstmts::Integer, nslots::Integer) = CodeEdges([Int[] for i = 1:nstmts], [Int[] for i = 1:nstmts], Dict{GlobalRef,Variable}(), [Int[] for i = 1:nslots])
374375

375376
function Base.show(io::IO, edges::CodeEdges)
376377
println(io, "CodeEdges:")
@@ -389,7 +390,7 @@ end
389390

390391

391392
"""
392-
edges = CodeEdges(src::CodeInfo)
393+
edges = CodeEdges(mod::Module, src::CodeInfo)
393394
394395
Analyze `src` and determine the chain of dependencies.
395396
@@ -410,7 +411,10 @@ function CodeEdges(src::CodeInfo, cl::CodeLinks)
410411
# Replace/add named intermediates (slot & named-variable references) with statement numbers
411412
nstmts, nslots = length(src.code), length(src.slotnames)
412413
marked, slothandled = BitSet(), fill(false, nslots) # working storage during resolution
413-
edges = CodeEdges(nstmts)
414+
edges = CodeEdges(nstmts, nslots)
415+
for (edge_s, link_s) in zip(edges.slotassigns, cl.slotassigns)
416+
append!(edge_s, link_s)
417+
end
414418
emptylink = Links()
415419
emptylist = Int[]
416420
for (i, stmt) in enumerate(src.code)
@@ -640,9 +644,9 @@ function lines_required!(isrequired::AbstractVector{Union{Bool,Symbol}}, objs, s
640644
iter = 0
641645
while changed
642646
changed = false
643-
@show iter
644-
print_with_code(stdout, src, isrequired)
645-
println()
647+
# @show iter
648+
# print_with_code(stdout, src, isrequired)
649+
# println()
646650

647651
# Handle ssa predecessors
648652
changed |= add_ssa_preds!(isrequired, src, edges, norequire)
@@ -730,6 +734,17 @@ end
730734
## Add control-flow
731735

732736
iscf(stmt) = isa(stmt, Core.GotoNode) || isa(stmt, Core.GotoIfNot) || isa(stmt, Core.ReturnNode)
737+
function jumps_back(src, i)
738+
stmt = src.code[i]
739+
(isa(stmt, Core.GotoNode) && stmt.label < i ||
740+
isa(stmt, Core.GotoIfNot) && stmt.dest < i) && return true
741+
if isa(stmt, Core.GotoIfNot) && i < length(src.code)
742+
stmt = src.code[i+1]
743+
isa(stmt, Core.GotoNode) && return stmt.label < i
744+
end
745+
return false
746+
end
747+
733748
function markcf!(isrequired, src, i)
734749
stmt = src.code[i]
735750
@assert iscf(stmt)
@@ -783,7 +798,7 @@ function add_control_flow!(isrequired, src, cfg, domtree, postdomtree)
783798
r = rng(bb)
784799
if block_internals_needed(isrequired, src, r)
785800
needed[ibb] = true
786-
# Check this blocks precessors and mark any that are just control-flow
801+
# Check this block's precessors and mark any that are just control-flow
787802
for p in bb.preds
788803
r = rng(blocks[p])
789804
if length(r) == 1 && iscf(src.code[r[1]])
@@ -814,21 +829,25 @@ function add_control_flow!(isrequired, src, cfg, domtree, postdomtree)
814829
while jbb != 0
815830
if ispredecessor(blocks, jbb, ibb, empty!(cache)) # is post-dominator jbb also a predecessor of ibb? If so we have a loop.
816831
pdbb = blocks[jbb]
817-
idxlast = rng(pdbb)[end]
818-
stmt = src.code[idxlast]
819-
if iscf(stmt)
820-
if isrequired[idxlast] != true
821-
_changed = true
822-
if isa(stmt, Core.ReturnNode) && isrequired[idxlast] != :exit
823-
isrequired[idxlast] = :exit
824-
else
825-
isrequired[idxlast] = true
826-
if isa(stmt, Core.GotoIfNot) && idxlast < length(isrequired) && isrequired[idxlast+1] != true && iscf(src.code[idxlast+1])
827-
isrequired[idxlast+1] = true
832+
r = rng(pdbb)
833+
# if block_internals_needed(isrequired, src, r)
834+
idxlast = rng(pdbb)[end]
835+
stmt = src.code[idxlast]
836+
if iscf(stmt) && jumps_back(src, idxlast)
837+
if isrequired[idxlast] != true
838+
_changed = true
839+
if isa(stmt, Core.ReturnNode) && isrequired[idxlast] != :exit
840+
isrequired[idxlast] = :exit
841+
else
842+
isrequired[idxlast] = true
843+
if isa(stmt, Core.GotoIfNot) && idxlast < length(isrequired) && isrequired[idxlast+1] != true && iscf(src.code[idxlast+1])
844+
isrequired[idxlast+1] = true
845+
end
828846
end
847+
break
829848
end
830849
end
831-
end
850+
# end
832851
end
833852
jbb = postdomtree.idoms_bb[jbb]
834853
end
@@ -867,10 +886,14 @@ function add_control_flow!(isrequired, src, cfg, domtree, postdomtree)
867886
# Mark the ipostdom's predecessors...
868887
for k in blocks[ipbb].preds
869888
idxlast = rng(blocks[k])[end]
870-
if iscf(src.code[idxlast])
889+
stmt = src.code[idxlast]
890+
if iscf(stmt)
871891
if markcf!(isrequired, src, idxlast)
872892
changed = true
873893
ok = true
894+
if isa(stmt, Core.GotoIfNot) && idxlast < length(isrequired) && isrequired[idxlast+1] != true && iscf(src.code[idxlast+1])
895+
isrequired[idxlast+1] = true
896+
end
874897
end
875898
end
876899
end

test/codeedges.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ end
488488
stmts = src.code
489489
edges = CodeEdges(m, src)
490490

491-
isrq = lines_required!(istypedef.(stmts), src, edges)
491+
isrq = LoweredCodeUtils.initialize_isrequired(length(stmts))
492+
copyto!(isrq, istypedef.(stmts))
493+
lines_required!(isrq, src, edges)
492494
frame = Frame(m, src)
493495
selective_eval_fromstart!(frame, isrq, #=toplevel=#true)
494496

0 commit comments

Comments
 (0)