Skip to content

Commit 8d5ff75

Browse files
authored
Merge pull request swiftlang#33725 from gottesmm/pr-9d3036face5e432d0ac446965da3a4de0b7b84aa
[semantic-arc] Extract out borrow scope optimizations into its own file.
2 parents 185a301 + 7c12c7f commit 8d5ff75

File tree

3 files changed

+60
-39
lines changed

3 files changed

+60
-39
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- BorrowScopeOpts.cpp ----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// \file
14+
///
15+
/// Optimizations that attempt to simplify and or eliminate borrow scopes. Today
16+
/// we only eliminate scopes, but we could also eliminate redundant scopes by
17+
/// converting struct_extract operations to use destructure operations.
18+
///
19+
//===----------------------------------------------------------------------===//
20+
21+
#include "SemanticARCOptVisitor.h"
22+
23+
using namespace swift;
24+
using namespace swift::semanticarc;
25+
26+
bool SemanticARCOptVisitor::visitBeginBorrowInst(BeginBorrowInst *bbi) {
27+
auto kind = bbi->getOperand().getOwnershipKind();
28+
SmallVector<EndBorrowInst *, 16> endBorrows;
29+
for (auto *op : bbi->getUses()) {
30+
if (!op->isConsumingUse()) {
31+
// Make sure that this operand can accept our arguments kind.
32+
auto map = op->getOwnershipKindMap();
33+
if (map.canAcceptKind(kind))
34+
continue;
35+
return false;
36+
}
37+
38+
// Otherwise, this borrow is being consumed. See if our consuming inst is an
39+
// end_borrow. If it isn't, then return false, this scope is
40+
// needed. Otherwise, add the end_borrow to our list of end borrows.
41+
auto *ebi = dyn_cast<EndBorrowInst>(op->getUser());
42+
if (!ebi) {
43+
return false;
44+
}
45+
endBorrows.push_back(ebi);
46+
}
47+
48+
// At this point, we know that the begin_borrow's operand can be
49+
// used as an argument to all non-end borrow uses. Eliminate the
50+
// begin borrow and end borrows.
51+
while (!endBorrows.empty()) {
52+
auto *ebi = endBorrows.pop_back_val();
53+
eraseInstruction(ebi);
54+
}
55+
56+
eraseAndRAUWSingleValueInstruction(bbi, bbi->getOperand());
57+
return true;
58+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target_sources(swiftSILOptimizer PRIVATE
22
SemanticARCOpts.cpp
33
OwnershipLiveRange.cpp
4-
LoadCopyToLoadBorrowOpt.cpp)
4+
LoadCopyToLoadBorrowOpt.cpp
5+
BorrowScopeOpts.cpp)

lib/SILOptimizer/SemanticARC/SemanticARCOpts.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -376,44 +376,6 @@ bool SemanticARCOptVisitor::processWorklist() {
376376
return madeChange;
377377
}
378378

379-
//===----------------------------------------------------------------------===//
380-
// Redundant Borrow Scope Elimination
381-
//===----------------------------------------------------------------------===//
382-
383-
bool SemanticARCOptVisitor::visitBeginBorrowInst(BeginBorrowInst *bbi) {
384-
auto kind = bbi->getOperand().getOwnershipKind();
385-
SmallVector<EndBorrowInst *, 16> endBorrows;
386-
for (auto *op : bbi->getUses()) {
387-
if (!op->isConsumingUse()) {
388-
// Make sure that this operand can accept our arguments kind.
389-
auto map = op->getOwnershipKindMap();
390-
if (map.canAcceptKind(kind))
391-
continue;
392-
return false;
393-
}
394-
395-
// Otherwise, this borrow is being consumed. See if our consuming inst is an
396-
// end_borrow. If it isn't, then return false, this scope is
397-
// needed. Otherwise, add the end_borrow to our list of end borrows.
398-
auto *ebi = dyn_cast<EndBorrowInst>(op->getUser());
399-
if (!ebi) {
400-
return false;
401-
}
402-
endBorrows.push_back(ebi);
403-
}
404-
405-
// At this point, we know that the begin_borrow's operand can be
406-
// used as an argument to all non-end borrow uses. Eliminate the
407-
// begin borrow and end borrows.
408-
while (!endBorrows.empty()) {
409-
auto *ebi = endBorrows.pop_back_val();
410-
eraseInstruction(ebi);
411-
}
412-
413-
eraseAndRAUWSingleValueInstruction(bbi, bbi->getOperand());
414-
return true;
415-
}
416-
417379
//===----------------------------------------------------------------------===//
418380
// CopyValue Optimizations Elimination
419381
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)