Skip to content

Commit 31c9d76

Browse files
committed
[ownership] Make ForwardingOperand truly a loose wrapper around an operand.
This implies making -> and * return an Operand * instead of an OwnershipForwardingInst. So one can thus do: ForwardingOperand op; op.myForwardingOperandMethod(); op->myOperandMethod();
1 parent 939ae01 commit 31c9d76

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,24 @@ class ForwardingOperand {
8282
public:
8383
static Optional<ForwardingOperand> get(Operand *use);
8484

85-
Operand *getUse() const { return use; }
8685
OwnershipConstraint getOwnershipConstraint() const {
8786
// We use a force unwrap since a ForwardingOperand should always have an
8887
// ownership constraint.
8988
return use->getOwnershipConstraint();
9089
}
90+
9191
ValueOwnershipKind getOwnershipKind() const;
9292
void setOwnershipKind(ValueOwnershipKind newKind) const;
9393
void replaceOwnershipKind(ValueOwnershipKind oldKind,
9494
ValueOwnershipKind newKind) const;
9595

96-
const OwnershipForwardingInst *operator->() const {
97-
return cast<OwnershipForwardingInst>(use->getUser());
98-
}
99-
OwnershipForwardingInst *operator->() {
100-
return cast<OwnershipForwardingInst>(use->getUser());
101-
}
102-
const OwnershipForwardingInst &operator*() const {
103-
return *cast<OwnershipForwardingInst>(use->getUser());
104-
}
105-
OwnershipForwardingInst &operator*() {
106-
return *cast<OwnershipForwardingInst>(use->getUser());
107-
}
96+
const Operand *operator->() const { return use; }
97+
98+
Operand *operator->() { return use; }
99+
100+
const Operand &operator*() const { return *use; }
101+
102+
Operand &operator*() { return *use; }
108103

109104
/// Call \p visitor with each value that contains the final forwarded
110105
/// ownership of. E.x.: result of a unchecked_ref_cast, phi arguments of a

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,35 @@ Optional<ForwardingOperand> ForwardingOperand::get(Operand *use) {
900900
}
901901

902902
ValueOwnershipKind ForwardingOperand::getOwnershipKind() const {
903-
return (*this)->getOwnershipKind();
903+
auto *user = use->getUser();
904+
905+
// NOTE: This if chain is meant to be a covered switch, so make sure to return
906+
// in each if itself since we have an unreachable at the bottom to ensure if a
907+
// new subclass of OwnershipForwardingInst is added
908+
if (auto *ofsvi = dyn_cast<AllArgOwnershipForwardingSingleValueInst>(user))
909+
return ofsvi->getOwnershipKind();
910+
911+
if (auto *ofsvi = dyn_cast<FirstArgOwnershipForwardingSingleValueInst>(user))
912+
return ofsvi->getOwnershipKind();
913+
914+
if (auto *ofci = dyn_cast<OwnershipForwardingConversionInst>(user))
915+
return ofci->getOwnershipKind();
916+
917+
if (auto *ofseib = dyn_cast<OwnershipForwardingSelectEnumInstBase>(user))
918+
return ofseib->getOwnershipKind();
919+
920+
if (auto *ofmvi =
921+
dyn_cast<OwnershipForwardingMultipleValueInstruction>(user)) {
922+
assert(ofmvi->getNumOperands() == 1);
923+
return ofmvi->getOwnershipKind();
924+
}
925+
926+
if (auto *ofti = dyn_cast<OwnershipForwardingTermInst>(user)) {
927+
assert(ofti->getNumOperands() == 1);
928+
return ofti->getOwnershipKind();
929+
}
930+
931+
llvm_unreachable("Unhandled forwarding inst?!");
904932
}
905933

906934
void ForwardingOperand::setOwnershipKind(ValueOwnershipKind newKind) const {

0 commit comments

Comments
 (0)