@@ -319,8 +319,6 @@ static bool isWrittenTo(Context &ctx, LoadInst *load,
319
319
// Top Level Entrypoint
320
320
// ===----------------------------------------------------------------------===//
321
321
322
- // Convert a load [copy] from unique storage [read] that has all uses that can
323
- // accept a guaranteed parameter to a load_borrow.
324
322
bool SemanticARCOptVisitor::visitLoadInst (LoadInst *li) {
325
323
// This optimization can use more complex analysis. We should do some
326
324
// experiments before enabling this by default as a guaranteed optimization.
@@ -334,9 +332,19 @@ bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
334
332
if (li->getOwnershipQualifier () != LoadOwnershipQualifier::Copy)
335
333
return false ;
336
334
337
- // Ok, we have our load [copy]. Make sure its value is truly a dead live range
338
- // implying it is only ever consumed by destroy_value instructions. If it is
339
- // consumed, we need to pass off a +1 value, so bail.
335
+ // Ok, we have our load [copy]. Try to optimize.
336
+ return performLoadCopyToLoadBorrowOptimization (li);
337
+ }
338
+
339
+ // Convert a load [copy] from unique storage [read] that has all uses that can
340
+ // accept a guaranteed parameter to a load_borrow.
341
+ bool SemanticARCOptVisitor::performLoadCopyToLoadBorrowOptimization (
342
+ LoadInst *li) {
343
+ assert (li->getOwnershipQualifier () == LoadOwnershipQualifier::Copy);
344
+
345
+ // Make sure its value is truly a dead live range implying it is only ever
346
+ // consumed by destroy_value instructions. If it is consumed, we need to pass
347
+ // off a +1 value, so bail.
340
348
//
341
349
// FIXME: We should consider if it is worth promoting a load [copy]
342
350
// -> load_borrow if we can put a copy_value on a cold path and thus
@@ -355,8 +363,8 @@ bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
355
363
// load_borrow.
356
364
auto *lbi =
357
365
SILBuilderWithScope (li).createLoadBorrow (li->getLoc (), li->getOperand ());
358
-
359
366
lr.insertEndBorrowsAtDestroys (lbi, getDeadEndBlocks (), ctx.lifetimeFrontier );
360
- std::move (lr).convertToGuaranteedAndRAUW (lbi, getCallbacks ());
367
+ SILValue replacement = lbi;
368
+ std::move (lr).convertToGuaranteedAndRAUW (replacement, getCallbacks ());
361
369
return true ;
362
370
}
0 commit comments