|
24 | 24 | using namespace swift;
|
25 | 25 | using namespace swift::semanticarc;
|
26 | 26 |
|
27 |
| -/// Whether the provided lexical begin_borrow instruction is redundant. |
28 |
| -/// |
29 |
| -/// A begin_borrow [lexical] is redundant if the borrowed value's lifetime is |
30 |
| -/// otherwise guaranteed. That happens if: |
31 |
| -/// - the value is a guaranteed argument to the function |
32 |
| -/// - the value is itself a begin_borrow [lexical] |
33 |
| -static bool isRedundantLexicalBeginBorrow(BeginBorrowInst *bbi) { |
34 |
| - assert(bbi->isLexical()); |
35 |
| - auto value = bbi->getOperand(); |
36 |
| - if (auto *outerBBI = dyn_cast<BeginBorrowInst>(value)) { |
37 |
| - return outerBBI->isLexical(); |
38 |
| - } |
39 |
| - if (auto *arg = dyn_cast<SILFunctionArgument>(value)) { |
40 |
| - return arg->getOwnershipKind() == OwnershipKind::Guaranteed; |
41 |
| - } |
42 |
| - return false; |
43 |
| -} |
44 |
| - |
45 | 27 | bool SemanticARCOptVisitor::visitBeginBorrowInst(BeginBorrowInst *bbi) {
|
46 | 28 | // Quickly check if we are supposed to perform this transformation.
|
47 | 29 | if (!ctx.shouldPerform(ARCTransformKind::RedundantBorrowScopeElimPeephole))
|
48 | 30 | return false;
|
49 | 31 |
|
50 |
| - // Lexical borrow scopes must remain in order to ensure that value lifetimes |
51 |
| - // are not observably shortened. |
52 |
| - if (bbi->isLexical()) { |
53 |
| - if (!isRedundantLexicalBeginBorrow(bbi)) |
54 |
| - return false; |
| 32 | + // Non-redundant, lexical borrow scopes must remain in order to ensure that |
| 33 | + // value lifetimes are not observably shortened. |
| 34 | + if (bbi->isLexical() && !isNestedLexicalBeginBorrow(bbi)) { |
| 35 | + return false; |
55 | 36 | }
|
56 | 37 |
|
57 | 38 | auto kind = bbi->getOperand().getOwnershipKind();
|
|
0 commit comments