|
16 | 16 | #include "swift/SIL/InstructionUtils.h" |
17 | 17 | #include "swift/SIL/LinearLifetimeChecker.h" |
18 | 18 | #include "swift/SIL/Projection.h" |
| 19 | +#include "swift/SIL/MemAccessUtils.h" |
19 | 20 | #include "swift/SIL/SILArgument.h" |
20 | 21 | #include "swift/SIL/SILInstruction.h" |
21 | 22 |
|
@@ -787,6 +788,46 @@ bool InteriorPointerOperand::findTransitiveUsesForAddress( |
787 | 788 | return foundError; |
788 | 789 | } |
789 | 790 |
|
| 791 | +// Determine whether \p address may be in a borrow scope and if so record the |
| 792 | +// InteriorPointerOperand that produces the address from a guaranteed value. |
| 793 | +BorrowedAddress::BorrowedAddress(SILValue address) { |
| 794 | + assert(address->getType().isAddress()); |
| 795 | + |
| 796 | + auto storageWithBase = AccessedStorageWithBase::compute(address); |
| 797 | + switch (storageWithBase.storage.getKind()) { |
| 798 | + case AccessedStorage::Argument: |
| 799 | + case AccessedStorage::Stack: |
| 800 | + case AccessedStorage::Global: |
| 801 | + // Unidentified storage is from an "escaped pointer", so it need not be |
| 802 | + // restricted to a borrow scope. |
| 803 | + case AccessedStorage::Unidentified: |
| 804 | + this->mayBeBorrowed = false; |
| 805 | + return; |
| 806 | + case AccessedStorage::Box: |
| 807 | + case AccessedStorage::Yield: |
| 808 | + case AccessedStorage::Class: |
| 809 | + case AccessedStorage::Tail: |
| 810 | + // The base object might be within a borrow scope. |
| 811 | + break; |
| 812 | + case AccessedStorage::Nested: |
| 813 | + llvm_unreachable("unexpected storage"); |
| 814 | + }; |
| 815 | + auto base = storageWithBase.base; |
| 816 | + if (!base) |
| 817 | + return; // bail on unexpected patterns |
| 818 | + |
| 819 | + auto intPtrOp = InteriorPointerOperand::inferFromResult(base); |
| 820 | + if (!intPtrOp) |
| 821 | + return; |
| 822 | + |
| 823 | + SILValue nonAddressValue = intPtrOp.operand->get(); |
| 824 | + if (nonAddressValue->getOwnershipKind() != OwnershipKind::Guaranteed) { |
| 825 | + this->mayBeBorrowed = false; |
| 826 | + return; |
| 827 | + } |
| 828 | + this->interiorPointerOp = intPtrOp; |
| 829 | +} |
| 830 | + |
790 | 831 | //===----------------------------------------------------------------------===// |
791 | 832 | // Owned Value Introducers |
792 | 833 | //===----------------------------------------------------------------------===// |
|
0 commit comments