@@ -554,29 +554,36 @@ function run_passes(ci::CodeInfo, sv::OptimizationState, caller::InferenceResult
554554end
555555
556556function convert_to_ircode (ci:: CodeInfo , sv:: OptimizationState )
557- code = copy_exprargs (ci. code)
557+ linetable = ci. linetable
558+ if ! isa (linetable, Vector{LineInfoNode})
559+ linetable = collect (LineInfoNode, linetable:: Vector{Any} ):: Vector{LineInfoNode}
560+ end
561+
562+ # check if coverage mode is enabled
558563 coverage = coverage_enabled (sv. mod)
559- # Go through and add an unreachable node after every
560- # Union{} call. Then reindex labels.
561- idx = 1
562- oldidx = 1
563- changemap = fill (0 , length (code))
564- prevloc = zero (eltype (ci. codelocs))
565- stmtinfo = sv. stmt_info
566- codelocs = ci. codelocs
567- ssavaluetypes = ci. ssavaluetypes:: Vector{Any}
568- ssaflags = ci. ssaflags
569564 if ! coverage && JLOptions (). code_coverage == 3 # path-specific coverage mode
570- for line in ci. linetable
571- line = line:: LineInfoNode
565+ for line in linetable
572566 if is_file_tracked (line. file)
573567 # if any line falls in a tracked file enable coverage for all
574568 coverage = true
575569 break
576570 end
577571 end
578572 end
579- labelmap = coverage ? fill (0 , length (code)) : changemap
573+
574+ # Go through and add an unreachable node after every
575+ # Union{} call. Then reindex labels.
576+ code = copy_exprargs (ci. code)
577+ stmtinfo = sv. stmt_info
578+ codelocs = ci. codelocs
579+ ssavaluetypes = ci. ssavaluetypes:: Vector{Any}
580+ ssaflags = ci. ssaflags
581+ meta = Expr[]
582+ idx = 1
583+ oldidx = 1
584+ ssachangemap = fill (0 , length (code))
585+ labelchangemap = coverage ? fill (0 , length (code)) : ssachangemap
586+ prevloc = zero (eltype (ci. codelocs))
580587 while idx <= length (code)
581588 codeloc = codelocs[idx]
582589 if coverage && codeloc != prevloc && codeloc != 0
@@ -586,9 +593,9 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
586593 insert! (ssavaluetypes, idx, Nothing)
587594 insert! (stmtinfo, idx, nothing )
588595 insert! (ssaflags, idx, IR_FLAG_NULL)
589- changemap [oldidx] += 1
590- if oldidx < length (labelmap )
591- labelmap [oldidx + 1 ] += 1
596+ ssachangemap [oldidx] += 1
597+ if oldidx < length (labelchangemap )
598+ labelchangemap [oldidx + 1 ] += 1
592599 end
593600 idx += 1
594601 prevloc = codeloc
@@ -601,30 +608,27 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
601608 insert! (ssavaluetypes, idx + 1 , Union{})
602609 insert! (stmtinfo, idx + 1 , nothing )
603610 insert! (ssaflags, idx + 1 , ssaflags[idx])
604- if oldidx < length (changemap )
605- changemap [oldidx + 1 ] += 1
606- coverage && (labelmap [oldidx + 1 ] += 1 )
611+ if oldidx < length (ssachangemap )
612+ ssachangemap [oldidx + 1 ] += 1
613+ coverage && (labelchangemap [oldidx + 1 ] += 1 )
607614 end
608615 idx += 1
609616 end
610617 end
611618 idx += 1
612619 oldidx += 1
613620 end
614- renumber_ir_elements! (code, changemap, labelmap)
615621
616- meta = Expr[]
622+ renumber_ir_elements! (code, ssachangemap, labelchangemap)
623+
617624 for i = 1 : length (code)
618625 code[i] = process_meta! (meta, code[i])
619626 end
620627 strip_trailing_junk! (ci, code, stmtinfo)
621- cfg = compute_basic_blocks (code)
622628 types = Any[]
623629 stmts = InstructionStream (code, types, stmtinfo, codelocs, ssaflags)
624- linetable = ci. linetable
625- isa (linetable, Vector{LineInfoNode}) || (linetable = collect (LineInfoNode, linetable:: Vector{Any} ))
626- ir = IRCode (stmts, cfg, linetable, sv. slottypes, meta, sv. sptypes)
627- return ir
630+ cfg = compute_basic_blocks (code)
631+ return IRCode (stmts, cfg, linetable, sv. slottypes, meta, sv. sptypes)
628632end
629633
630634function process_meta! (meta:: Vector{Expr} , @nospecialize stmt)
@@ -788,31 +792,33 @@ function statement_costs!(cost::Vector{Int}, body::Vector{Any}, src::Union{CodeI
788792 return maxcost
789793end
790794
791- function renumber_ir_elements! (body:: Vector{Any} , changemap :: Vector{Int} )
792- return renumber_ir_elements! (body, changemap, changemap )
795+ function renumber_ir_elements! (body:: Vector{Any} , ssachangemap :: Vector{Int} )
796+ return renumber_ir_elements! (body, ssachangemap, ssachangemap )
793797end
794798
795- function cumsum_ssamap! (ssamap:: Vector{Int} )
799+ function cumsum_ssamap! (ssachangemap:: Vector{Int} )
800+ any_change = false
796801 rel_change = 0
797- for i = 1 : length (ssamap)
798- rel_change += ssamap[i]
799- if ssamap[i] == - 1
802+ for i = 1 : length (ssachangemap)
803+ val = ssachangemap[i]
804+ any_change |= val ≠ 0
805+ rel_change += val
806+ if val == - 1
800807 # Keep a marker that this statement was deleted
801- ssamap [i] = typemin (Int)
808+ ssachangemap [i] = typemin (Int)
802809 else
803- ssamap [i] = rel_change
810+ ssachangemap [i] = rel_change
804811 end
805812 end
813+ return any_change
806814end
807815
808816function renumber_ir_elements! (body:: Vector{Any} , ssachangemap:: Vector{Int} , labelchangemap:: Vector{Int} )
809- cumsum_ssamap! (labelchangemap)
817+ any_change = cumsum_ssamap! (labelchangemap)
810818 if ssachangemap != = labelchangemap
811- cumsum_ssamap! (ssachangemap)
812- end
813- if labelchangemap[end ] == 0 && ssachangemap[end ] == 0
814- return
819+ any_change |= cumsum_ssamap! (ssachangemap)
815820 end
821+ any_change || return
816822 for i = 1 : length (body)
817823 el = body[i]
818824 if isa (el, GotoNode)
@@ -822,7 +828,8 @@ function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, lab
822828 if isa (cond, SSAValue)
823829 cond = SSAValue (cond. id + ssachangemap[cond. id])
824830 end
825- body[i] = GotoIfNot (cond, el. dest + labelchangemap[el. dest])
831+ was_deleted = labelchangemap[el. dest] == typemin (Int)
832+ body[i] = was_deleted ? cond : GotoIfNot (cond, el. dest + labelchangemap[el. dest])
826833 elseif isa (el, ReturnNode)
827834 if isdefined (el, :val )
828835 val = el. val
0 commit comments