@@ -277,11 +277,12 @@ function transform!(ci, meth, nargs, sparams, N)
277
277
278
278
if length (cfg. blocks) != 1
279
279
ϕ = PhiNode ()
280
+
280
281
bb_start = length (ir. stmts)+ 1
281
282
push! (ir, NewInstruction (ϕ))
282
283
push! (ir, NewInstruction (ReturnNode (SSAValue (length (ir. stmts)))))
283
284
push! (ir. cfg, BasicBlock (StmtRange (bb_start, length (ir. stmts))))
284
- new_bb_idx = length (cfg. blocks)
285
+ new_bb_idx = length (ir . cfg. blocks)
285
286
286
287
for (bb, i) in bbidxiter (ir)
287
288
bb == new_bb_idx && break
@@ -301,6 +302,27 @@ function transform!(ci, meth, nargs, sparams, N)
301
302
302
303
ir = compact! (ir)
303
304
ir = split_critical_edges! (ir)
305
+
306
+ # If that resulted in the return not being the last block, fix that now.
307
+ # We keep things simple this way, such that the basic blocks in the
308
+ # forward and reverse are simply inverses of each other (i.e. the
309
+ # exist block needs to be last, since the entry block needs to be first
310
+ # in the reverse pass).
311
+
312
+ if ! isa (ir. stmts[end ][:inst ], ReturnNode)
313
+ new_bb_idx = length (ir. cfg. blocks)+ 1
314
+ for (bb, i) in bbidxiter (ir)
315
+ stmt = ir. stmts[i][:inst ]
316
+ if isa (stmt, ReturnNode)
317
+ ir[i] = NewInstruction (GotoNode (new_bb_idx))
318
+ push! (ir, NewInstruction (stmt))
319
+ push! (ir. cfg, BasicBlock (StmtRange (length (ir. stmts), length (ir. stmts))))
320
+ cfg_insert_edge! (ir. cfg, bb, new_bb_idx)
321
+ break
322
+ end
323
+ end
324
+ end
325
+
304
326
cfg = ir. cfg
305
327
306
328
# Now add a special control flow marker to every basic block
@@ -499,6 +521,7 @@ function transform!(ci, meth, nargs, sparams, N)
499
521
500
522
if isa (stmt, Core. ReturnNode)
501
523
accum! (stmt. val, Argument (2 ))
524
+ current_env = nothing
502
525
elseif isexpr (stmt, :call )
503
526
Δ = do_accum (SSAValue (i))
504
527
callee = retrieve_ctx_obj (current_env, i)
@@ -784,38 +807,41 @@ function transform!(ci, meth, nargs, sparams, N)
784
807
end
785
808
786
809
succs = cfg. blocks[active_bb]. succs
787
- if old_idx == last (orig_bb_ranges[active_bb]) && length (succs) != 0
788
- override = false
789
- if has_terminator[active_bb]
790
- terminator = compact[idx]
791
- compact[idx] = nothing
792
- override = true
793
- end
794
- function terminator_insert_node! (node)
795
- if override
796
- compact[idx] = node. stmt
797
- override = false
798
- return SSAValue (idx)
799
- else
800
- return insert_node_here! (compact, node, true )
810
+
811
+ if old_idx == last (orig_bb_ranges[active_bb])
812
+ if length (succs) != 0
813
+ override = false
814
+ if has_terminator[active_bb]
815
+ terminator = compact[idx]
816
+ compact[idx] = nothing
817
+ override = true
801
818
end
802
- end
803
- tup = terminator_insert_node! (
804
- effect_free (NewInstruction (Expr (:call , tuple, rev[orig_bb_ranges[active_bb]]. .. ), Any, Int32 (0 ))))
805
- for succ in succs
806
- preds = cfg. blocks[succ]. preds
807
- if length (preds) == 1
808
- val = tup
809
- else
810
- selector = findfirst (== (active_bb), preds)
811
- val = insert_node_here! (compact, effect_free (NewInstruction (Expr (:call , tuple, selector, tup), Any, Int32 (0 ))), true )
819
+ function terminator_insert_node! (node)
820
+ if override
821
+ compact[idx] = node. stmt
822
+ override = false
823
+ return SSAValue (idx)
824
+ else
825
+ return insert_node_here! (compact, node, true )
826
+ end
827
+ end
828
+ tup = terminator_insert_node! (
829
+ effect_free (NewInstruction (Expr (:call , tuple, rev[orig_bb_ranges[active_bb]]. .. ), Any, Int32 (0 ))))
830
+ for succ in succs
831
+ preds = cfg. blocks[succ]. preds
832
+ if length (preds) == 1
833
+ val = tup
834
+ else
835
+ selector = findfirst (== (active_bb), preds)
836
+ val = insert_node_here! (compact, effect_free (NewInstruction (Expr (:call , tuple, selector, tup), Any, Int32 (0 ))), true )
837
+ end
838
+ pn = phi_nodes[succ]
839
+ push! (pn. edges, active_bb)
840
+ push! (pn. values, val)
841
+ end
842
+ if has_terminator[active_bb]
843
+ insert_node_here! (compact, NewInstruction (terminator, Any, Int32 (0 )), true )
812
844
end
813
- pn = phi_nodes[succ]
814
- push! (pn. edges, active_bb)
815
- push! (pn. values, val)
816
- end
817
- if has_terminator[active_bb]
818
- insert_node_here! (compact, NewInstruction (terminator, Any, Int32 (0 )), true )
819
845
end
820
846
active_bb += 1
821
847
end
0 commit comments