[ownership] Refactor checked conversion from ArrayRef<SILInstruction *> -> ArrayRef<BranchPropagatedUser> onto BranchPropagatedUser.

A branch propagated user that isn't a cond_br is layout compatible with a
SILInstruction *. This helper function converts from ArrayRef<SILInstruction *>
-> ArrayRef<BranchPropagatedUser> but also in asserts builds checks that our
invariant (namely all of the 'SILInstruction *' are not cond_br.
This commit is contained in:
Michael Gottesman
2020-01-28 13:24:41 -08:00
parent 758f6e0184
commit 70a417c3f5
2 changed files with 15 additions and 18 deletions

View File

@@ -108,6 +108,18 @@ public:
llvm::PointerLikeTypeTraits<InnerTy>::NumLowBitsAvailable
};
static ArrayRef<BranchPropagatedUser>
convertFromInstArray(ArrayRef<SILInstruction *> instArray) {
assert(llvm::all_of(
instArray,
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
"Passed cond branch to a non-BranchPropagatedUser API");
auto *castData =
reinterpret_cast<const BranchPropagatedUser *>(instArray.data());
ArrayRef<BranchPropagatedUser> castArray(castData, instArray.size());
return castArray;
}
private:
BranchPropagatedUser(SILInstruction *inst) : user(inst) {
assert(!isa<CondBranchInst>(inst));

View File

@@ -187,24 +187,9 @@ public:
bool validateLifetime(SILValue value,
ArrayRef<SILInstruction *> consumingUses,
ArrayRef<SILInstruction *> nonConsumingUses) {
assert(llvm::all_of(
consumingUses,
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
"Passed cond branch to a non-BranchPropagatedUser API");
assert(llvm::all_of(
nonConsumingUses,
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
"Passed cond branch to a non-BranchPropagatedUser API");
auto *consumingUsesCast =
reinterpret_cast<const BranchPropagatedUser *>(consumingUses.data());
auto *nonConsumingUsesCast =
reinterpret_cast<const BranchPropagatedUser *>(nonConsumingUses.data());
ArrayRef<BranchPropagatedUser> consumingUsesCastArray(consumingUsesCast,
consumingUses.size());
ArrayRef<BranchPropagatedUser> nonConsumingUsesCastArray(
nonConsumingUsesCast, nonConsumingUses.size());
return validateLifetime(value, consumingUsesCastArray,
nonConsumingUsesCastArray);
return validateLifetime(
value, BranchPropagatedUser::convertFromInstArray(consumingUses),
BranchPropagatedUser::convertFromInstArray(nonConsumingUses));
}
};