Skip to content

Commit bacfdbd

Browse files
committed
Add ForwardingOperation::visitForwardedValues api
1 parent 79ca1eb commit bacfdbd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/swift/SIL/InstWrappers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ class ForwardingOperation {
311311
/// Return true if the forwarded value is address-only either before or after
312312
/// forwarding.
313313
bool isAddressOnly() const;
314+
315+
// Call \p visitor on all forwarded results of the current forwarding
316+
// operation.
317+
bool visitForwardedValues(function_ref<bool(SILValue)> visitor);
314318
};
315319
} // end namespace swift
316320

lib/SIL/Utils/InstWrappers.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,28 @@ bool ForwardingOperation::isAddressOnly() const {
7070
// If any of the operands are address-only, then the aggregate must be.
7171
return aggregate->getType().isAddressOnly(*forwardingInst->getFunction());
7272
}
73+
74+
bool ForwardingOperation::visitForwardedValues(
75+
function_ref<bool(SILValue)> visitor) {
76+
if (auto *svi = dyn_cast<SingleValueInstruction>(forwardingInst)) {
77+
return visitor(svi);
78+
}
79+
if (auto *mvri = dyn_cast<MultipleValueInstruction>(forwardingInst)) {
80+
return llvm::all_of(mvri->getResults(), [&](SILValue value) {
81+
if (value->getOwnershipKind() == OwnershipKind::None)
82+
return true;
83+
return visitor(value);
84+
});
85+
}
86+
auto *ti = cast<TermInst>(forwardingInst);
87+
assert(ti->mayHaveTerminatorResult());
88+
return llvm::all_of(ti->getSuccessorBlocks(), [&](SILBasicBlock *succBlock) {
89+
// If we do not have any arguments, then continue.
90+
if (succBlock->args_empty())
91+
return true;
92+
93+
auto args = succBlock->getSILPhiArguments();
94+
assert(args.size() == 1 && "Transforming terminator with multiple args?!");
95+
return visitor(args[0]);
96+
});
97+
}

0 commit comments

Comments
 (0)