11# This file is a part of Julia. License is MIT: https://julialang.org/license
22
3+ const UnoptSlot = Union{SlotNumber, TypedSlot}
4+
35mutable struct SlotInfo
46 defs:: Vector{Int}
57 uses:: Vector{Int}
@@ -15,24 +17,24 @@ function scan_entry!(result::Vector{SlotInfo}, idx::Int, @nospecialize(stmt))
1517 push! (result[slot_id (stmt. slot)]. defs, idx)
1618 return
1719 elseif isexpr (stmt, :(= ))
18- if isa (stmt. args[1 ], SlotNumber)
19- push! (result[slot_id (stmt. args[1 ])]. defs, idx)
20+ arg1 = stmt. args[1 ]
21+ if isa (arg1, SlotNumber)
22+ push! (result[slot_id (arg1)]. defs, idx)
2023 end
2124 stmt = stmt. args[2 ]
2225 end
23- if isa (stmt, Union{SlotNumber, TypedSlot} )
26+ if isa (stmt, UnoptSlot )
2427 push! (result[slot_id (stmt)]. uses, idx)
2528 return
2629 end
2730 for op in userefs (stmt)
2831 val = op[]
29- if isa (val, Union{SlotNumber, TypedSlot} )
32+ if isa (val, UnoptSlot )
3033 push! (result[slot_id (val)]. uses, idx)
3134 end
3235 end
3336end
3437
35-
3638function scan_slot_def_use (nargs:: Int , ci:: CodeInfo , code:: Vector{Any} )
3739 nslots = length (ci. slotflags)
3840 result = SlotInfo[SlotInfo () for i = 1 : nslots]
@@ -62,13 +64,12 @@ function renumber_ssa!(@nospecialize(stmt), ssanums::Vector{SSAValue}, new_ssa::
6264 return ssamap (val-> renumber_ssa (val, ssanums, new_ssa), stmt)
6365end
6466
65- function make_ssa! (ci:: CodeInfo , code:: Vector{Any} , idx, slot, @nospecialize (typ))
66- (idx == 0 ) && return Argument (slot)
67+ function make_ssa! (ci:: CodeInfo , code:: Vector{Any} , idx:: Int , @nospecialize (typ))
6768 stmt = code[idx]
6869 @assert isexpr (stmt, :(= ))
6970 code[idx] = stmt. args[2 ]
7071 (ci. ssavaluetypes:: Vector{Any} )[idx] = typ
71- idx
72+ return SSAValue ( idx)
7273end
7374
7475function new_to_regular (@nospecialize (stmt), new_offset:: Int )
@@ -82,7 +83,7 @@ function new_to_regular(@nospecialize(stmt), new_offset::Int)
8283 return urs[]
8384end
8485
85- function fixup_slot! (ir:: IRCode , ci:: CodeInfo , idx:: Int , slot:: Int , @nospecialize ( stmt:: Union{SlotNumber, TypedSlot} ) , @nospecialize (ssa))
86+ function fixup_slot! (ir:: IRCode , ci:: CodeInfo , idx:: Int , slot:: Int , stmt:: UnoptSlot , @nospecialize (ssa))
8687 # We don't really have the information here to get rid of these.
8788 # We'll do so later
8889 if ssa === UNDEF_TOKEN
@@ -103,34 +104,34 @@ function fixup_slot!(ir::IRCode, ci::CodeInfo, idx::Int, slot::Int, @nospecializ
103104 @assert false # unreachable
104105end
105106
106- function fixemup! (cond, rename , ir:: IRCode , ci:: CodeInfo , idx:: Int , @nospecialize (stmt))
107- if isa (stmt, Union{SlotNumber, TypedSlot} ) && cond (stmt)
108- return fixup_slot! (ir, ci, idx, slot_id (stmt), stmt, rename (stmt))
107+ function fixemup! (@specialize (slot_filter), @specialize (rename_slot) , ir:: IRCode , ci:: CodeInfo , idx:: Int , @nospecialize (stmt))
108+ if isa (stmt, UnoptSlot ) && slot_filter (stmt)
109+ return fixup_slot! (ir, ci, idx, slot_id (stmt), stmt, rename_slot (stmt))
109110 end
110111 if isexpr (stmt, :(= ))
111- stmt. args[2 ] = fixemup! (cond, rename , ir, ci, idx, stmt. args[2 ])
112+ stmt. args[2 ] = fixemup! (slot_filter, rename_slot , ir, ci, idx, stmt. args[2 ])
112113 return stmt
113114 end
114115 if isa (stmt, PhiNode)
115116 for i = 1 : length (stmt. edges)
116117 isassigned (stmt. values, i) || continue
117118 val = stmt. values[i]
118- isa (val, Union{SlotNumber, TypedSlot} ) || continue
119- cond (val) || continue
119+ isa (val, UnoptSlot ) || continue
120+ slot_filter (val) || continue
120121 bb_idx = block_for_inst (ir. cfg, Int (stmt. edges[i]))
121122 from_bb_terminator = last (ir. cfg. blocks[bb_idx]. stmts)
122- stmt. values[i] = fixup_slot! (ir, ci, from_bb_terminator, slot_id (val), val, rename (val))
123+ stmt. values[i] = fixup_slot! (ir, ci, from_bb_terminator, slot_id (val), val, rename_slot (val))
123124 end
124125 return stmt
125126 end
126127 if isexpr (stmt, :isdefined )
127128 val = stmt. args[1 ]
128- if isa (val, Union{SlotNumber, TypedSlot} )
129+ if isa (val, UnoptSlot )
129130 slot = slot_id (val)
130131 if (ci. slotflags[slot] & SLOT_USEDUNDEF) == 0
131132 return true
132133 else
133- ssa = rename (val)
134+ ssa = rename_slot (val)
134135 if ssa === UNDEF_TOKEN
135136 return false
136137 elseif ! isa (ssa, SSAValue) && ! isa (ssa, NewSSAValue)
@@ -145,8 +146,8 @@ function fixemup!(cond, rename, ir::IRCode, ci::CodeInfo, idx::Int, @nospecializ
145146 urs = userefs (stmt)
146147 for op in urs
147148 val = op[]
148- if isa (val, Union{SlotNumber, TypedSlot} ) && cond (val)
149- x = fixup_slot! (ir, ci, idx, slot_id (val), val, rename (val))
149+ if isa (val, UnoptSlot ) && slot_filter (val)
150+ x = fixup_slot! (ir, ci, idx, slot_id (val), val, rename_slot (val))
150151 # We inserted an undef error node. Delete subsequent statement
151152 # to avoid confusing the optimizer
152153 if x === UNDEF_TOKEN
@@ -171,12 +172,12 @@ end
171172
172173function fixup_uses! (ir:: IRCode , ci:: CodeInfo , code:: Vector{Any} , uses:: Vector{Int} , slot:: Int , @nospecialize (ssa))
173174 for use in uses
174- code[use] = fixemup! (stmt -> slot_id (stmt )== slot, stmt-> ssa, ir, ci, use, code[use])
175+ code[use] = fixemup! (x :: UnoptSlot -> slot_id (x )== slot, stmt:: UnoptSlot -> ssa, ir, ci, use, code[use])
175176 end
176177end
177178
178179function rename_uses! (ir:: IRCode , ci:: CodeInfo , idx:: Int , @nospecialize (stmt), renames:: Vector{Any} )
179- return fixemup! (stmt-> true , stmt-> renames[slot_id (stmt)], ir, ci, idx, stmt)
180+ return fixemup! (stmt:: UnoptSlot -> true , stmt:: UnoptSlot -> renames[slot_id (stmt)], ir, ci, idx, stmt)
180181end
181182
182183function strip_trailing_junk! (ci:: CodeInfo , code:: Vector{Any} , info:: Vector{CallInfo} )
@@ -655,7 +656,7 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, domtree::DomTree,
655656 else
656657 val = code[slot. defs[]]. args[2 ]
657658 typ = typ_for_val (val, ci, ir. sptypes, slot. defs[], slottypes)
658- ssaval = SSAValue ( make_ssa! (ci, code, slot. defs[], idx, typ) )
659+ ssaval = make_ssa! (ci, code, slot. defs[], typ)
659660 fixup_uses! (ir, ci, code, slot. uses, idx, ssaval)
660661 end
661662 continue
@@ -784,30 +785,33 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, domtree::DomTree,
784785 end
785786 code[idx] = stmt
786787 # Record a store
787- if isexpr (stmt, :(= )) && isa (stmt. args[1 ], SlotNumber)
788- id = slot_id (stmt. args[1 ])
789- val = stmt. args[2 ]
790- typ = typ_for_val (val, ci, ir. sptypes, idx, slottypes)
791- # Having UNDEF_TOKEN appear on the RHS is possible if we're on a dead branch.
792- # Do something reasonable here, by marking the LHS as undef as well.
793- if val != = UNDEF_TOKEN
794- incoming_vals[id] = SSAValue (make_ssa! (ci, code, idx, id, typ):: Int )
795- else
796- code[idx] = nothing
797- incoming_vals[id] = UNDEF_TOKEN
798- end
799- enter_block = item
800- while haskey (exc_handlers, enter_block)
801- (; enter_block, leave_block) = exc_handlers[enter_block]
802- cidx = findfirst ((; slot):: NewPhiCNode -> slot_id (slot)== id, new_phic_nodes[leave_block])
803- if cidx != = nothing
804- node = UpsilonNode (incoming_vals[id])
805- if incoming_vals[id] === UNDEF_TOKEN
806- node = UpsilonNode ()
807- typ = MaybeUndef (Union{})
788+ if isexpr (stmt, :(= ))
789+ arg1 = stmt. args[1 ]
790+ if isa (arg1, SlotNumber)
791+ id = slot_id (arg1)
792+ val = stmt. args[2 ]
793+ typ = typ_for_val (val, ci, ir. sptypes, idx, slottypes)
794+ # Having UNDEF_TOKEN appear on the RHS is possible if we're on a dead branch.
795+ # Do something reasonable here, by marking the LHS as undef as well.
796+ if val != = UNDEF_TOKEN
797+ incoming_vals[id] = make_ssa! (ci, code, idx, typ)
798+ else
799+ code[idx] = nothing
800+ incoming_vals[id] = UNDEF_TOKEN
801+ end
802+ enter_block = item
803+ while haskey (exc_handlers, enter_block)
804+ (; enter_block, leave_block) = exc_handlers[enter_block]
805+ cidx = findfirst ((; slot):: NewPhiCNode -> slot_id (slot)== id, new_phic_nodes[leave_block])
806+ if cidx != = nothing
807+ node = UpsilonNode (incoming_vals[id])
808+ if incoming_vals[id] === UNDEF_TOKEN
809+ node = UpsilonNode ()
810+ typ = MaybeUndef (Union{})
811+ end
812+ push! (new_phic_nodes[leave_block][cidx]. node. values,
813+ NewSSAValue (insert_node! (ir, idx, NewInstruction (node, typ), true ). id - length (ir. stmts)))
808814 end
809- push! (new_phic_nodes[leave_block][cidx]. node. values,
810- NewSSAValue (insert_node! (ir, idx, NewInstruction (node, typ), true ). id - length (ir. stmts)))
811815 end
812816 end
813817 end
0 commit comments