@@ -11,16 +11,21 @@ function getfield_elim_pass!(ir::IRCode)
1111 compact = IncrementalCompact (ir)
1212 insertions = Vector {Any} ()
1313 for (idx, stmt) in compact
14+ # Step 1: Check whether the statement we're looking at is a getfield
1415 isa (stmt, Expr) || continue
1516 is_known_call (stmt, getfield, ir, ir. mod) || continue
1617 isa (stmt. args[2 ], SSAValue) || continue
18+ # # Normalize the field argument to getfield
1719 field = stmt. args[3 ]
1820 isa (field, QuoteNode) && (field = field. value)
1921 isa (field, Union{Int, Symbol}) || continue
2022 orig_defidx = defidx = stmt. args[2 ]. id
23+
24+ # Step 2: Figure out what the struct is defined as
2125 def = compact[defidx]
2226 typeconstraint = types (compact)[defidx]
2327 phi_locs = Tuple{Int, Int}[]
28+ # # Track definitions through PiNode/PhiNode
2429 while true
2530 if isa (def, PiNode)
2631 typeconstraint = typeintersect (typeconstraint, def. typ)
@@ -60,6 +65,8 @@ function getfield_elim_pass!(ir::IRCode)
6065 end
6166 break
6267 end
68+ # Step 3: Check if the definition we eventually end up at is either
69+ # a tuple(...) call or Expr(:new) and perform replacement.
6370 if isa (def, Expr) && is_known_call (def, tuple, ir, ir. mod) && isa (field, Int) && 1 <= field < length (def. args)
6471 forwarded = def. args[1 + field]
6572 elseif isexpr (def, :new )
@@ -79,6 +86,7 @@ function getfield_elim_pass!(ir::IRCode)
7986 else
8087 continue
8188 end
89+ # Step 4: Remember any phinodes we need to insert
8290 if ! isempty (phi_locs) && isa (forwarded, SSAValue)
8391 # TODO : We have have to use BB ids for phi_locs
8492 # to avoid index invalidation.
0 commit comments