@@ -264,24 +264,16 @@ struct OwnershipLifetimeExtender {
264
264
CopyValueInst *
265
265
OwnershipLifetimeExtender::createPlusOneCopy (SILValue value,
266
266
SILInstruction *consumingPoint) {
267
- auto *newValInsertPt = value->getDefiningInsertionPoint ();
268
- assert (newValInsertPt);
269
- CopyValueInst *copy;
270
- if (!isa<SILArgument>(value)) {
271
- SILBuilderWithScope::insertAfter (newValInsertPt, [&](SILBuilder &builder) {
272
- copy = builder.createCopyValue (builder.getInsertionPointLoc (), value);
273
- });
274
- } else {
275
- SILBuilderWithScope builder (newValInsertPt);
276
- copy = builder.createCopyValue (newValInsertPt->getLoc (), value);
277
- }
267
+ auto *copyPoint = value->getNextInstruction ();
268
+ auto loc = copyPoint->getLoc ();
269
+ auto *copy = SILBuilderWithScope (copyPoint).createCopyValue (loc, value);
278
270
279
271
auto &callbacks = ctx.callbacks ;
280
272
callbacks.createdNewInst (copy);
281
273
282
274
auto *result = copy;
283
275
findJointPostDominatingSet (
284
- newValInsertPt ->getParent (), consumingPoint->getParent (),
276
+ copyPoint ->getParent (), consumingPoint->getParent (),
285
277
// inputBlocksFoundDuringWalk.
286
278
[&](SILBasicBlock *loopBlock) {
287
279
// This must be consumingPoint->getParent() since we only have one
@@ -291,10 +283,16 @@ OwnershipLifetimeExtender::createPlusOneCopy(SILValue value,
291
283
assert (loopBlock == consumingPoint->getParent ());
292
284
auto front = loopBlock->begin ();
293
285
SILBuilderWithScope newBuilder (front);
286
+
287
+ // Create an extra copy when the consuming point is inside a
288
+ // loop and both copyPoint and the destroy points are outside the
289
+ // loop. This copy will be consumed in the same block. The original
290
+ // value will be destroyed on all paths exiting the loop.
291
+ //
292
+ // Since copyPoint dominates consumingPoint, it must be outside the
293
+ // loop. Otherwise backward traversal would have stopped at copyPoint.
294
294
result = newBuilder.createCopyValue (front->getLoc (), copy);
295
295
callbacks.createdNewInst (result);
296
-
297
- llvm_unreachable (" Should never visit this!" );
298
296
},
299
297
// Input blocks in joint post dom set. We don't care about thse.
300
298
[&](SILBasicBlock *postDomBlock) {
0 commit comments