[SemanticARC] NFC: Extracted opt from visit.

In preparation to attempt two variations of the optimization, added
performLoadCopyToLoadBorrowOptimization and call it from visitLoadInst.
This commit is contained in:
Nate Chandler
2023-04-25 10:55:27 -07:00
parent b686e81085
commit 8287d2e9ef
2 changed files with 16 additions and 7 deletions

View File

@@ -319,8 +319,6 @@ static bool isWrittenTo(Context &ctx, LoadInst *load,
// Top Level Entrypoint
//===----------------------------------------------------------------------===//
// Convert a load [copy] from unique storage [read] that has all uses that can
// accept a guaranteed parameter to a load_borrow.
bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
// This optimization can use more complex analysis. We should do some
// experiments before enabling this by default as a guaranteed optimization.
@@ -334,9 +332,19 @@ bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
if (li->getOwnershipQualifier() != LoadOwnershipQualifier::Copy)
return false;
// Ok, we have our load [copy]. Make sure its value is truly a dead live range
// implying it is only ever consumed by destroy_value instructions. If it is
// consumed, we need to pass off a +1 value, so bail.
// Ok, we have our load [copy]. Try to optimize.
return performLoadCopyToLoadBorrowOptimization(li);
}
// Convert a load [copy] from unique storage [read] that has all uses that can
// accept a guaranteed parameter to a load_borrow.
bool SemanticARCOptVisitor::performLoadCopyToLoadBorrowOptimization(
LoadInst *li) {
assert(li->getOwnershipQualifier() == LoadOwnershipQualifier::Copy);
// Make sure its value is truly a dead live range implying it is only ever
// consumed by destroy_value instructions. If it is consumed, we need to pass
// off a +1 value, so bail.
//
// FIXME: We should consider if it is worth promoting a load [copy]
// -> load_borrow if we can put a copy_value on a cold path and thus
@@ -355,8 +363,8 @@ bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
// load_borrow.
auto *lbi =
SILBuilderWithScope(li).createLoadBorrow(li->getLoc(), li->getOperand());
lr.insertEndBorrowsAtDestroys(lbi, getDeadEndBlocks(), ctx.lifetimeFrontier);
std::move(lr).convertToGuaranteedAndRAUW(lbi, getCallbacks());
SILValue replacement = lbi;
std::move(lr).convertToGuaranteedAndRAUW(replacement, getCallbacks());
return true;
}