diff --git a/include/swift/SIL/SILInstruction.h b/include/swift/SIL/SILInstruction.h index ce2d922c404..4b99d342472 100644 --- a/include/swift/SIL/SILInstruction.h +++ b/include/swift/SIL/SILInstruction.h @@ -523,12 +523,20 @@ public: private: /// Predicate used to filter OperandValueRange. struct OperandToValue; + /// Predicate used to filter TransformedOperandValueRange. + struct OperandToTransformedValue; public: using OperandValueRange = OptionalTransformRange, OperandToValue>; + using TransformedOperandValueRange = + OptionalTransformRange, OperandToTransformedValue>; + OperandValueRange getOperandValues(bool skipTypeDependentOperands = false) const; + TransformedOperandValueRange + getOperandValues(std::function transformFn, + bool skipTypeDependentOperands) const; SILValue getOperand(unsigned Num) const { return getAllOperands()[Num].get(); @@ -727,6 +735,24 @@ struct SILInstruction::OperandToValue { } }; +struct SILInstruction::OperandToTransformedValue { + const SILInstruction &i; + std::function transformFn; + bool skipTypeDependentOps; + + OperandToTransformedValue(const SILInstruction &i, + std::function transformFn, + bool skipTypeDependentOps) + : i(i), transformFn(transformFn), + skipTypeDependentOps(skipTypeDependentOps) {} + + Optional operator()(const Operand &use) const { + if (skipTypeDependentOps && i.isTypeDependentOperand(use)) + return None; + return transformFn(use.get()); + } +}; + inline auto SILInstruction::getOperandValues(bool skipTypeDependentOperands) const -> OperandValueRange { @@ -734,6 +760,15 @@ SILInstruction::getOperandValues(bool skipTypeDependentOperands) const OperandToValue(*this, skipTypeDependentOperands)); } +inline auto +SILInstruction::getOperandValues(std::function transformFn, + bool skipTypeDependentOperands) const + -> TransformedOperandValueRange { + return TransformedOperandValueRange( + getAllOperands(), + OperandToTransformedValue(*this, transformFn, skipTypeDependentOperands)); +} + struct SILInstruction::OperandToType { const SILInstruction &i;