Skip to content

Commit 05224aa

Browse files
committed
Improve the SILInstruction::getOperandValues() API.
Add NonTypeDependentOperandToValue predicate for composability. Add a getNonTypeDependentOperandValues(), which can be used as a functor. The skipTypeDependentOperands parameter complicated the API.
1 parent 2325293 commit 05224aa

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -567,19 +567,30 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
567567
bool maySuspend() const;
568568

569569
private:
570-
/// Predicate used to filter OperandValueRange.
570+
/// Functor for Operand::get()
571571
struct OperandToValue;
572-
/// Predicate used to filter TransformedOperandValueRange.
572+
/// Functor for Operand::get()
573+
struct OperandRefToValue;
574+
/// Predicate to filter NonTypeDependentOperandValueRange
575+
struct NonTypeDependentOperandToValue;
576+
/// Predicate to filter TransformedOperandValueRange.
573577
struct OperandToTransformedValue;
574578

575579
public:
576-
using OperandValueRange =
577-
OptionalTransformRange<ArrayRef<Operand>, OperandToValue>;
580+
using OperandValueRange = TransformRange<ArrayRef<Operand*>, OperandToValue>;
581+
using OperandRefValueRange =
582+
TransformRange<ArrayRef<Operand>, OperandRefToValue>;
583+
using NonTypeDependentOperandValueRange =
584+
OptionalTransformRange<ArrayRef<Operand>, NonTypeDependentOperandToValue>;
578585
using TransformedOperandValueRange =
579-
OptionalTransformRange<ArrayRef<Operand>, OperandToTransformedValue>;
586+
OptionalTransformRange<ArrayRef<Operand>, OperandToTransformedValue>;
587+
588+
static OperandValueRange getOperandValues(ArrayRef<Operand*> operands);
589+
590+
OperandRefValueRange getOperandValues() const;
591+
592+
NonTypeDependentOperandValueRange getNonTypeDependentOperandValues() const;
580593

581-
OperandValueRange
582-
getOperandValues(bool skipTypeDependentOperands = false) const;
583594
TransformedOperandValueRange
584595
getOperandValues(std::function<SILValue(SILValue)> transformFn,
585596
bool skipTypeDependentOperands) const;
@@ -879,14 +890,24 @@ inline const SILNode *SILNode::instAsNode(const SILInstruction *inst) {
879890

880891

881892
struct SILInstruction::OperandToValue {
893+
SILValue operator()(const Operand *use) const {
894+
return use->get();
895+
}
896+
};
897+
898+
struct SILInstruction::OperandRefToValue {
899+
SILValue operator()(const Operand &use) const {
900+
return use.get();
901+
}
902+
};
903+
904+
struct SILInstruction::NonTypeDependentOperandToValue {
882905
const SILInstruction &i;
883-
bool skipTypeDependentOps;
884906

885-
OperandToValue(const SILInstruction &i, bool skipTypeDependentOps)
886-
: i(i), skipTypeDependentOps(skipTypeDependentOps) {}
907+
NonTypeDependentOperandToValue(const SILInstruction &i): i(i) {}
887908

888909
Optional<SILValue> operator()(const Operand &use) const {
889-
if (skipTypeDependentOps && i.isTypeDependentOperand(use))
910+
if (i.isTypeDependentOperand(use))
890911
return None;
891912
return use.get();
892913
}
@@ -910,11 +931,21 @@ struct SILInstruction::OperandToTransformedValue {
910931
}
911932
};
912933

934+
inline SILInstruction::OperandValueRange
935+
SILInstruction::getOperandValues(ArrayRef<Operand*> operands) {
936+
return OperandValueRange(operands, OperandToValue());
937+
}
938+
939+
inline auto
940+
SILInstruction::getOperandValues() const -> OperandRefValueRange {
941+
return OperandRefValueRange(getAllOperands(), OperandRefToValue());
942+
}
943+
913944
inline auto
914-
SILInstruction::getOperandValues(bool skipTypeDependentOperands) const
915-
-> OperandValueRange {
916-
return OperandValueRange(getAllOperands(),
917-
OperandToValue(*this, skipTypeDependentOperands));
945+
SILInstruction::getNonTypeDependentOperandValues() const
946+
-> NonTypeDependentOperandValueRange {
947+
return NonTypeDependentOperandValueRange(getAllOperands(),
948+
NonTypeDependentOperandToValue(*this));
918949
}
919950

920951
inline auto

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ bool swift::getAllBorrowIntroducingValues(SILValue inputValue,
10551055
// instruction
10561056
if (isForwardingBorrow(value)) {
10571057
if (auto *i = value->getDefiningInstruction()) {
1058-
llvm::copy(i->getOperandValues(true /*skip type dependent ops*/),
1058+
llvm::copy(i->getNonTypeDependentOperandValues(),
10591059
std::back_inserter(worklist));
10601060
continue;
10611061
}
@@ -1100,7 +1100,7 @@ BorrowedValue swift::getSingleBorrowIntroducingValue(SILValue inputValue) {
11001100
// instruction
11011101
if (isForwardingBorrow(currentValue)) {
11021102
if (auto *i = currentValue->getDefiningInstruction()) {
1103-
auto instOps = i->getOperandValues(true /*ignore type dependent ops*/);
1103+
auto instOps = i->getNonTypeDependentOperandValues();
11041104
// If we have multiple incoming values, return .None. We can't handle
11051105
// this.
11061106
auto begin = instOps.begin();
@@ -1160,7 +1160,7 @@ bool swift::getAllOwnedValueIntroducers(
11601160
// instruction
11611161
if (isForwardingConsume(value)) {
11621162
if (auto *i = value->getDefiningInstruction()) {
1163-
llvm::copy(i->getOperandValues(true /*skip type dependent ops*/),
1163+
llvm::copy(i->getNonTypeDependentOperandValues(),
11641164
std::back_inserter(worklist));
11651165
continue;
11661166
}
@@ -1201,7 +1201,7 @@ OwnedValueIntroducer swift::getSingleOwnedValueIntroducer(SILValue inputValue) {
12011201
// instruction
12021202
if (isForwardingConsume(currentValue)) {
12031203
if (auto *i = currentValue->getDefiningInstruction()) {
1204-
auto instOps = i->getOperandValues(true /*ignore type dependent ops*/);
1204+
auto instOps = i->getNonTypeDependentOperandValues();
12051205
// If we have multiple incoming values, return .None. We can't handle
12061206
// this.
12071207
auto begin = instOps.begin();

lib/SILOptimizer/SemanticARC/OwnershipLiveRange.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ OwnershipLiveRange::OwnershipLiveRange(SILValue value)
7575
auto *ti = dyn_cast<TermInst>(user);
7676
if ((ti && !ti->isTransformationTerminator()) ||
7777
!canOpcodeForwardGuaranteedValues(op) ||
78-
1 != count_if(user->getOperandValues(
79-
true /*ignore type dependent operands*/),
78+
1 != count_if(user->getNonTypeDependentOperandValues(),
8079
[&](SILValue v) {
8180
return v.getOwnershipKind() == OwnershipKind::Owned;
8281
})) {

0 commit comments

Comments
 (0)