From 70a417c3f567f603594e38e7b1337b8ff5ee6b88 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 28 Jan 2020 13:24:41 -0800 Subject: [PATCH] [ownership] Refactor checked conversion from ArrayRef -> ArrayRef onto BranchPropagatedUser. A branch propagated user that isn't a cond_br is layout compatible with a SILInstruction *. This helper function converts from ArrayRef -> ArrayRef but also in asserts builds checks that our invariant (namely all of the 'SILInstruction *' are not cond_br. --- include/swift/SIL/BranchPropagatedUser.h | 12 ++++++++++++ include/swift/SIL/OwnershipUtils.h | 21 +++------------------ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/include/swift/SIL/BranchPropagatedUser.h b/include/swift/SIL/BranchPropagatedUser.h index 5e00ebf4ab9b5..1c85fb22e2637 100644 --- a/include/swift/SIL/BranchPropagatedUser.h +++ b/include/swift/SIL/BranchPropagatedUser.h @@ -108,6 +108,18 @@ class BranchPropagatedUser { llvm::PointerLikeTypeTraits::NumLowBitsAvailable }; + static ArrayRef + convertFromInstArray(ArrayRef instArray) { + assert(llvm::all_of( + instArray, + [](SILInstruction *i) { return !isa(i); }) && + "Passed cond branch to a non-BranchPropagatedUser API"); + auto *castData = + reinterpret_cast(instArray.data()); + ArrayRef castArray(castData, instArray.size()); + return castArray; + } + private: BranchPropagatedUser(SILInstruction *inst) : user(inst) { assert(!isa(inst)); diff --git a/include/swift/SIL/OwnershipUtils.h b/include/swift/SIL/OwnershipUtils.h index 8f72bb8ef4d0e..88d2a566c55e1 100644 --- a/include/swift/SIL/OwnershipUtils.h +++ b/include/swift/SIL/OwnershipUtils.h @@ -187,24 +187,9 @@ class LinearLifetimeChecker { bool validateLifetime(SILValue value, ArrayRef consumingUses, ArrayRef nonConsumingUses) { - assert(llvm::all_of( - consumingUses, - [](SILInstruction *i) { return !isa(i); }) && - "Passed cond branch to a non-BranchPropagatedUser API"); - assert(llvm::all_of( - nonConsumingUses, - [](SILInstruction *i) { return !isa(i); }) && - "Passed cond branch to a non-BranchPropagatedUser API"); - auto *consumingUsesCast = - reinterpret_cast(consumingUses.data()); - auto *nonConsumingUsesCast = - reinterpret_cast(nonConsumingUses.data()); - ArrayRef consumingUsesCastArray(consumingUsesCast, - consumingUses.size()); - ArrayRef nonConsumingUsesCastArray( - nonConsumingUsesCast, nonConsumingUses.size()); - return validateLifetime(value, consumingUsesCastArray, - nonConsumingUsesCastArray); + return validateLifetime( + value, BranchPropagatedUser::convertFromInstArray(consumingUses), + BranchPropagatedUser::convertFromInstArray(nonConsumingUses)); } };