Skip to content

Commit

Permalink
refactor renumber_ir_elements to allow separate ssa and label changem…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrevels committed Jul 26, 2018
1 parent 38c8a03 commit ec2f2e7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
38 changes: 27 additions & 11 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,32 +393,48 @@ function is_known_call(e::Expr, @nospecialize(func), src, spvals::SimpleVector,
return isa(f, Const) && f.val === func
end

function renumber_ir_elements!(body::Vector{Any}, changemap::Vector{Int}, preprocess::Bool = true)
if preprocess
for i = 2:length(changemap)
changemap[i] += changemap[i - 1]
function renumber_ir_elements!(body::Vector{Any}, changemap::Vector{Int})
return renumber_ir_elements!(body, changemap, changemap)
end

function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, labelchangemap::Vector{Int})
for i = 2:length(labelchangemap)
labelchangemap[i] += labelchangemap[i - 1]
end
if ssachangemap !== labelchangemap
for i = 2:length(ssachangemap)
ssachangemap[i] += ssachangemap[i - 1]
end
end
changemap[end] != 0 || return
(labelchangemap[end] != 0 && ssachangemap[end] != 0) || return
for i = 1:length(body)
el = body[i]
if isa(el, GotoNode)
body[i] = GotoNode(el.label + changemap[el.label])
body[i] = GotoNode(el.label + labelchangemap[el.label])
elseif isa(el, SSAValue)
body[i] = SSAValue(el.id + changemap[el.id])
body[i] = SSAValue(el.id + ssachangemap[el.id])
elseif isa(el, Expr)
if el.head === :gotoifnot
cond = el.args[1]
if isa(cond, SSAValue)
el.args[1] = SSAValue(cond.id + changemap[cond.id])
el.args[1] = SSAValue(cond.id + ssachangemap[cond.id])
end
tgt = el.args[2]::Int
el.args[2] = tgt + changemap[tgt]
el.args[2] = tgt + labelchangemap[tgt]
elseif el.head === :enter
tgt = el.args[1]::Int
el.args[1] = tgt + changemap[tgt]
el.args[1] = tgt + labelchangemap[tgt]
elseif !is_meta_expr_head(el.head)
renumber_ir_elements!(el.args, changemap, false)
if el.head === :(=) && el.args[2] isa Expr && !is_meta_expr_head(el.args[2].head)
el = el.args[2]::Expr
end
args = el.args
for i = 1:length(args)
el = args[i]
if isa(el, SSAValue)
args[i] = SSAValue(el.id + ssachangemap[el.id])
end
end
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1806,3 +1806,49 @@ function ig27907(::Type{T}, ::Type, N::Integer, offsets...) where {T}
end

@test ig27907(Int, Int, 1, 0) == 0

# issue #28279
function f28279(b::Bool)
i = 1
while i > b
i -= 1
end
if b end
return i + 1
end
code28279 = code_lowered(f28279, (Bool,))[1].code
oldcode28279 = deepcopy(code28279)
ssachangemap = fill(0, length(code28279))
labelchangemap = fill(0, length(code28279))
worklist = Int[]
let i
for i in 1:length(code28279)
stmt = code28279[i]
if Meta.isexpr(stmt, :gotoifnot)
push!(worklist, i)
ssachangemap[i] = 1
if i < length(code28279)
labelchangemap[i + 1] = 1
end
end
end
end
Core.Compiler.renumber_ir_elements!(code28279, ssachangemap, labelchangemap)
@test length(code28279) === length(oldcode28279)
offset = 1
let i
for i in 1:length(code28279)
if i == length(code28279)
@test Meta.isexpr(code28279[i], :return)
@test Meta.isexpr(oldcode28279[i], :return)
@test code28279[i].args[1].id == (oldcode28279[i].args[1].id + offset - 1)
elseif Meta.isexpr(code28279[i], :gotoifnot)
@test Meta.isexpr(oldcode28279[i], :gotoifnot)
@test code28279[i].args[1] == oldcode28279[i].args[1]
@test code28279[i].args[2] == (oldcode28279[i].args[2] + offset)
global offset += 1
else
@test code28279[i] == oldcode28279[i]
end
end
end

0 comments on commit ec2f2e7

Please sign in to comment.