Skip to content

Commit 62c3eb9

Browse files
committed
Don't split blocks when we don't need to.
We can re-use the existing exit block if it only contains returns.
1 parent ca31a94 commit 62c3eb9

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/metal.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,13 @@ function replace_unreachable!(@nospecialize(job::CompilerJob), f::LLVM.Function)
11171117
exit_block = last(exit_blocks)
11181118
ret = terminator(exit_block)
11191119

1120-
if first(instructions(exit_block)) == ret && isempty(operands(ret))
1121-
# if the exit block contains only `ret void`, we can branch to it directly.
1120+
# create a return block with only the return instruction, so that we only have to
1121+
# care about any values returned, and not about any other SSA value in the block.
1122+
if first(instructions(exit_block)) == ret
1123+
# we can reuse the exit block if it only contains the return
11221124
return_block = exit_block
11231125
else
1124-
# split the exit block right before the ret, so that we only have to care about
1125-
# the value that's returned, and not about any other SSA value in the block.
1126+
# split the exit block right before the ret
11261127
return_block = BasicBlock(f, "ret")
11271128
move_after(return_block, exit_block)
11281129

@@ -1134,16 +1135,19 @@ function replace_unreachable!(@nospecialize(job::CompilerJob), f::LLVM.Function)
11341135
delete!(exit_block, ret)
11351136
position!(builder, return_block)
11361137
insert!(builder, ret)
1138+
end
11371139

1138-
# if we're returning a value, insert a phi and update the return
1139-
if !isempty(operands(ret))
1140-
position!(builder, ret)
1141-
# XXX: support aggregate returns?
1142-
val = only(operands(ret))
1143-
phi = phi!(builder, value_type(val))
1144-
push!(incoming(phi), (val, exit_block))
1145-
operands(ret)[1] = phi
1140+
# when returning a value, add a phi node to the return block, so that we can later
1141+
# add incoming undef values when branching from `unreachable` blocks
1142+
if !isempty(operands(ret))
1143+
position!(builder, ret)
1144+
# XXX: support aggregate returns?
1145+
val = only(operands(ret))
1146+
phi = phi!(builder, value_type(val))
1147+
for pred in predecessors(return_block)
1148+
push!(incoming(phi), (val, pred))
11461149
end
1150+
operands(ret)[1] = phi
11471151
end
11481152

11491153
# replace the unreachable with a branch to the return block

0 commit comments

Comments
 (0)