@@ -313,6 +313,7 @@ Instruction(is::InstructionStream) = Instruction(is, add_new_idx!(is))
313313 fldarray = getfield (getfield (node, :data ), fld)
314314 fldidx = getfield (node, :idx )
315315 (fld === :line ) && return (fldarray[3 fldidx- 2 ], fldarray[3 fldidx- 1 ], fldarray[3 fldidx- 0 ])
316+ (1 ≤ fldidx ≤ length (fldarray)) || throw (InvalidIRError ())
316317 return fldarray[fldidx]
317318end
318319@inline function setindex! (node:: Instruction , @nospecialize (val), fld:: Symbol )
@@ -481,11 +482,16 @@ function block_for_inst(ir::IRCode, inst::Int)
481482end
482483
483484function getindex (ir:: IRCode , s:: SSAValue )
485+ id = s. id
486+ (id ≥ 1 ) || throw (InvalidIRError ())
484487 nstmts = length (ir. stmts)
485- if s . id <= nstmts
486- return ir. stmts[s . id]
488+ if id <= nstmts
489+ return ir. stmts[id]
487490 else
488- return ir. new_nodes. stmts[s. id - nstmts]
491+ id -= nstmts
492+ stmts = ir. new_nodes. stmts
493+ (id ≤ length (stmts)) || throw (InvalidIRError ())
494+ return stmts[id]
489495 end
490496end
491497
@@ -801,12 +807,13 @@ end
801807types (ir:: Union{IRCode, IncrementalCompact} ) = TypesView (ir)
802808
803809function getindex (compact:: IncrementalCompact , ssa:: SSAValue )
804- @assert ssa. id < compact. result_idx
810+ ( 1 ≤ ssa. id ≤ compact. result_idx) || throw ( InvalidIRError ())
805811 return compact. result[ssa. id]
806812end
807813
808814function getindex (compact:: IncrementalCompact , ssa:: OldSSAValue )
809815 id = ssa. id
816+ (id ≥ 1 ) || throw (InvalidIRError ())
810817 if id < compact. idx
811818 new_idx = compact. ssa_rename[id]:: Int
812819 return compact. result[new_idx]
@@ -818,12 +825,15 @@ function getindex(compact::IncrementalCompact, ssa::OldSSAValue)
818825 return compact. ir. new_nodes. stmts[id]
819826 end
820827 id -= length (compact. ir. new_nodes)
828+ (id ≤ length (compact. pending_nodes. stmts)) || throw (InvalidIRError ())
821829 return compact. pending_nodes. stmts[id]
822830end
823831
824832function getindex (compact:: IncrementalCompact , ssa:: NewSSAValue )
825833 if ssa. id < 0
826- return compact. new_new_nodes. stmts[- ssa. id]
834+ stmts = compact. new_new_nodes. stmts
835+ (- ssa. id ≤ length (stmts)) || throw (InvalidIRError ())
836+ return stmts[- ssa. id]
827837 else
828838 return compact[SSAValue (ssa. id)]
829839 end
@@ -1069,6 +1079,7 @@ function getindex(view::TypesView, v::OldSSAValue)
10691079 id = v. id
10701080 ir = view. ir. ir
10711081 stmts = ir. stmts
1082+ (id ≥ 1 ) || throw (InvalidIRError ())
10721083 if id <= length (stmts)
10731084 return stmts[id][:type ]
10741085 end
@@ -1077,7 +1088,9 @@ function getindex(view::TypesView, v::OldSSAValue)
10771088 return ir. new_nodes. stmts[id][:type ]
10781089 end
10791090 id -= length (ir. new_nodes)
1080- return view. ir. pending_nodes. stmts[id][:type ]
1091+ stmts = view. ir. pending_nodes. stmts
1092+ (id ≤ length (stmts)) || throw (InvalidIRError ())
1093+ return stmts[id][:type ]
10811094end
10821095
10831096function kill_current_use! (compact:: IncrementalCompact , @nospecialize (val))
@@ -1204,20 +1217,27 @@ end
12041217
12051218getindex (view:: TypesView , idx:: SSAValue ) = getindex (view, idx. id)
12061219function getindex (view:: TypesView , idx:: Int )
1220+ (idx ≥ 1 ) || throw (InvalidIRError ())
12071221 if isa (view. ir, IncrementalCompact) && idx < view. ir. result_idx
12081222 return view. ir. result[idx][:type ]
12091223 elseif isa (view. ir, IncrementalCompact) && view. ir. renamed_new_nodes
12101224 if idx <= length (view. ir. result)
12111225 return view. ir. result[idx][:type ]
12121226 else
1213- return view. ir. new_new_nodes. stmts[idx - length (view. ir. result)][:type ]
1227+ idx -= length (view. ir. result)
1228+ stmts = view. ir. new_new_nodes. stmts
1229+ (idx ≤ length (stmts)) || throw (InvalidIRError ())
1230+ return stmts[idx][:type ]
12141231 end
12151232 else
12161233 ir = isa (view. ir, IncrementalCompact) ? view. ir. ir : view. ir
12171234 if idx <= length (ir. stmts)
12181235 return ir. stmts[idx][:type ]
12191236 else
1220- return ir. new_nodes. stmts[idx - length (ir. stmts)][:type ]
1237+ idx -= length (ir. stmts)
1238+ stmts = ir. new_nodes. stmts
1239+ (idx ≤ length (stmts)) || throw (InvalidIRError ())
1240+ return stmts[idx][:type ]
12211241 end
12221242 end
12231243end
0 commit comments