@@ -19,11 +19,27 @@ struct SSADefUse
1919end
2020SSADefUse () = SSADefUse (Int[], Int[], Int[])
2121
22- try_compute_fieldidx_expr (typ:: DataType , expr:: Expr ) = try_compute_fieldidx_args (typ, expr. args)
23- function try_compute_fieldidx_args (typ:: DataType , args:: Vector{Any} )
24- field = args[3 ]
25- isa (field, QuoteNode) && (field = field. value)
22+ function try_compute_field_stmt (compact:: IncrementalCompact , stmt:: Expr )
23+ field = stmt. args[3 ]
24+ # fields are usually literals, handle them manually
25+ if isa (field, QuoteNode)
26+ field = field. value
27+ elseif isa (field, Int)
28+ # try to resolve other constants, e.g. global reference
29+ else
30+ field = compact_exprtype (compact, field)
31+ if isa (field, Const)
32+ field = field. val
33+ else
34+ return nothing
35+ end
36+ end
2637 isa (field, Union{Int, Symbol}) || return nothing
38+ return field
39+ end
40+
41+ function try_compute_fieldidx_stmt (compact:: IncrementalCompact , stmt:: Expr , typ:: DataType )
42+ field = try_compute_field_stmt (compact, stmt)
2743 return try_compute_fieldidx (typ, field)
2844end
2945
@@ -636,10 +652,8 @@ function getfield_elim_pass!(ir::IRCode)
636652 else
637653 continue
638654 end
639- # # Normalize the field argument to getfield/setfield
640- field = stmt. args[3 ]
641- isa (field, QuoteNode) && (field = field. value)
642- isa (field, Union{Int, Symbol}) || continue
655+ field = try_compute_field_stmt (compact, stmt)
656+ field === nothing && continue
643657
644658 struct_typ = unwrap_unionall (widenconst (compact_exprtype (compact, stmt. args[2 ])))
645659 if isa (struct_typ, Union) && struct_typ <: Tuple
@@ -779,13 +793,13 @@ function getfield_elim_pass!(ir::IRCode)
779793 # it would have been deleted. That's fine, just ignore
780794 # the use in that case.
781795 stmt === nothing && continue
782- field = try_compute_fieldidx_expr (typ , stmt)
796+ field = try_compute_fieldidx_stmt (compact , stmt:: Expr , typ )
783797 field === nothing && (ok = false ; break )
784798 push! (fielddefuse[field]. uses, use)
785799 end
786800 ok || continue
787801 for use in defuse. defs
788- field = try_compute_fieldidx_expr (typ , ir[SSAValue (use)])
802+ field = try_compute_fieldidx_stmt (compact , ir[SSAValue (use)]:: Expr , typ )
789803 field === nothing && (ok = false ; break )
790804 push! (fielddefuse[field]. defs, use)
791805 end
0 commit comments