@@ -1117,12 +1117,13 @@ function replace_unreachable!(@nospecialize(job::CompilerJob), f::LLVM.Function)
1117
1117
exit_block = last (exit_blocks)
1118
1118
ret = terminator (exit_block)
1119
1119
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
1122
1124
return_block = exit_block
1123
1125
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
1126
1127
return_block = BasicBlock (f, " ret" )
1127
1128
move_after (return_block, exit_block)
1128
1129
@@ -1134,16 +1135,19 @@ function replace_unreachable!(@nospecialize(job::CompilerJob), f::LLVM.Function)
1134
1135
delete! (exit_block, ret)
1135
1136
position! (builder, return_block)
1136
1137
insert! (builder, ret)
1138
+ end
1137
1139
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))
1146
1149
end
1150
+ operands (ret)[1 ] = phi
1147
1151
end
1148
1152
1149
1153
# replace the unreachable with a branch to the return block
0 commit comments